arm-smmu.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. * IOMMU API for ARM architected SMMU implementations.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Copyright (C) 2013 ARM Limited
  18. *
  19. * Author: Will Deacon <will.deacon@arm.com>
  20. *
  21. * This driver currently supports:
  22. * - SMMUv1 and v2 implementations
  23. * - Stream-matching and stream-indexing
  24. * - v7/v8 long-descriptor format
  25. * - Non-secure access to the SMMU
  26. * - Context fault reporting
  27. */
  28. #define pr_fmt(fmt) "arm-smmu: " fmt
  29. #include <linux/atomic.h>
  30. #include <linux/delay.h>
  31. #include <linux/dma-iommu.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/err.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/io.h>
  36. #include <linux/io-64-nonatomic-hi-lo.h>
  37. #include <linux/iommu.h>
  38. #include <linux/iopoll.h>
  39. #include <linux/module.h>
  40. #include <linux/of.h>
  41. #include <linux/of_address.h>
  42. #include <linux/of_device.h>
  43. #include <linux/of_iommu.h>
  44. #include <linux/pci.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/slab.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/amba/bus.h>
  49. #include "io-pgtable.h"
  50. /* Maximum number of context banks per SMMU */
  51. #define ARM_SMMU_MAX_CBS 128
  52. /* SMMU global address space */
  53. #define ARM_SMMU_GR0(smmu) ((smmu)->base)
  54. #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
  55. /*
  56. * SMMU global address space with conditional offset to access secure
  57. * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
  58. * nsGFSYNR0: 0x450)
  59. */
  60. #define ARM_SMMU_GR0_NS(smmu) \
  61. ((smmu)->base + \
  62. ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
  63. ? 0x400 : 0))
  64. /*
  65. * Some 64-bit registers only make sense to write atomically, but in such
  66. * cases all the data relevant to AArch32 formats lies within the lower word,
  67. * therefore this actually makes more sense than it might first appear.
  68. */
  69. #ifdef CONFIG_64BIT
  70. #define smmu_write_atomic_lq writeq_relaxed
  71. #else
  72. #define smmu_write_atomic_lq writel_relaxed
  73. #endif
  74. /* Configuration registers */
  75. #define ARM_SMMU_GR0_sCR0 0x0
  76. #define sCR0_CLIENTPD (1 << 0)
  77. #define sCR0_GFRE (1 << 1)
  78. #define sCR0_GFIE (1 << 2)
  79. #define sCR0_GCFGFRE (1 << 4)
  80. #define sCR0_GCFGFIE (1 << 5)
  81. #define sCR0_USFCFG (1 << 10)
  82. #define sCR0_VMIDPNE (1 << 11)
  83. #define sCR0_PTM (1 << 12)
  84. #define sCR0_FB (1 << 13)
  85. #define sCR0_VMID16EN (1 << 31)
  86. #define sCR0_BSU_SHIFT 14
  87. #define sCR0_BSU_MASK 0x3
  88. /* Auxiliary Configuration register */
  89. #define ARM_SMMU_GR0_sACR 0x10
  90. /* Identification registers */
  91. #define ARM_SMMU_GR0_ID0 0x20
  92. #define ARM_SMMU_GR0_ID1 0x24
  93. #define ARM_SMMU_GR0_ID2 0x28
  94. #define ARM_SMMU_GR0_ID3 0x2c
  95. #define ARM_SMMU_GR0_ID4 0x30
  96. #define ARM_SMMU_GR0_ID5 0x34
  97. #define ARM_SMMU_GR0_ID6 0x38
  98. #define ARM_SMMU_GR0_ID7 0x3c
  99. #define ARM_SMMU_GR0_sGFSR 0x48
  100. #define ARM_SMMU_GR0_sGFSYNR0 0x50
  101. #define ARM_SMMU_GR0_sGFSYNR1 0x54
  102. #define ARM_SMMU_GR0_sGFSYNR2 0x58
  103. #define ID0_S1TS (1 << 30)
  104. #define ID0_S2TS (1 << 29)
  105. #define ID0_NTS (1 << 28)
  106. #define ID0_SMS (1 << 27)
  107. #define ID0_ATOSNS (1 << 26)
  108. #define ID0_PTFS_NO_AARCH32 (1 << 25)
  109. #define ID0_PTFS_NO_AARCH32S (1 << 24)
  110. #define ID0_CTTW (1 << 14)
  111. #define ID0_NUMIRPT_SHIFT 16
  112. #define ID0_NUMIRPT_MASK 0xff
  113. #define ID0_NUMSIDB_SHIFT 9
  114. #define ID0_NUMSIDB_MASK 0xf
  115. #define ID0_NUMSMRG_SHIFT 0
  116. #define ID0_NUMSMRG_MASK 0xff
  117. #define ID1_PAGESIZE (1 << 31)
  118. #define ID1_NUMPAGENDXB_SHIFT 28
  119. #define ID1_NUMPAGENDXB_MASK 7
  120. #define ID1_NUMS2CB_SHIFT 16
  121. #define ID1_NUMS2CB_MASK 0xff
  122. #define ID1_NUMCB_SHIFT 0
  123. #define ID1_NUMCB_MASK 0xff
  124. #define ID2_OAS_SHIFT 4
  125. #define ID2_OAS_MASK 0xf
  126. #define ID2_IAS_SHIFT 0
  127. #define ID2_IAS_MASK 0xf
  128. #define ID2_UBS_SHIFT 8
  129. #define ID2_UBS_MASK 0xf
  130. #define ID2_PTFS_4K (1 << 12)
  131. #define ID2_PTFS_16K (1 << 13)
  132. #define ID2_PTFS_64K (1 << 14)
  133. #define ID2_VMID16 (1 << 15)
  134. #define ID7_MAJOR_SHIFT 4
  135. #define ID7_MAJOR_MASK 0xf
  136. /* Global TLB invalidation */
  137. #define ARM_SMMU_GR0_TLBIVMID 0x64
  138. #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
  139. #define ARM_SMMU_GR0_TLBIALLH 0x6c
  140. #define ARM_SMMU_GR0_sTLBGSYNC 0x70
  141. #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
  142. #define sTLBGSTATUS_GSACTIVE (1 << 0)
  143. #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
  144. /* Stream mapping registers */
  145. #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
  146. #define SMR_VALID (1 << 31)
  147. #define SMR_MASK_SHIFT 16
  148. #define SMR_ID_SHIFT 0
  149. #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
  150. #define S2CR_CBNDX_SHIFT 0
  151. #define S2CR_CBNDX_MASK 0xff
  152. #define S2CR_TYPE_SHIFT 16
  153. #define S2CR_TYPE_MASK 0x3
  154. enum arm_smmu_s2cr_type {
  155. S2CR_TYPE_TRANS,
  156. S2CR_TYPE_BYPASS,
  157. S2CR_TYPE_FAULT,
  158. };
  159. #define S2CR_PRIVCFG_SHIFT 24
  160. #define S2CR_PRIVCFG_MASK 0x3
  161. enum arm_smmu_s2cr_privcfg {
  162. S2CR_PRIVCFG_DEFAULT,
  163. S2CR_PRIVCFG_DIPAN,
  164. S2CR_PRIVCFG_UNPRIV,
  165. S2CR_PRIVCFG_PRIV,
  166. };
  167. /* Context bank attribute registers */
  168. #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
  169. #define CBAR_VMID_SHIFT 0
  170. #define CBAR_VMID_MASK 0xff
  171. #define CBAR_S1_BPSHCFG_SHIFT 8
  172. #define CBAR_S1_BPSHCFG_MASK 3
  173. #define CBAR_S1_BPSHCFG_NSH 3
  174. #define CBAR_S1_MEMATTR_SHIFT 12
  175. #define CBAR_S1_MEMATTR_MASK 0xf
  176. #define CBAR_S1_MEMATTR_WB 0xf
  177. #define CBAR_TYPE_SHIFT 16
  178. #define CBAR_TYPE_MASK 0x3
  179. #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
  180. #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
  181. #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
  182. #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
  183. #define CBAR_IRPTNDX_SHIFT 24
  184. #define CBAR_IRPTNDX_MASK 0xff
  185. #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
  186. #define CBA2R_RW64_32BIT (0 << 0)
  187. #define CBA2R_RW64_64BIT (1 << 0)
  188. #define CBA2R_VMID_SHIFT 16
  189. #define CBA2R_VMID_MASK 0xffff
  190. /* Translation context bank */
  191. #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
  192. #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
  193. #define ARM_SMMU_CB_SCTLR 0x0
  194. #define ARM_SMMU_CB_ACTLR 0x4
  195. #define ARM_SMMU_CB_RESUME 0x8
  196. #define ARM_SMMU_CB_TTBCR2 0x10
  197. #define ARM_SMMU_CB_TTBR0 0x20
  198. #define ARM_SMMU_CB_TTBR1 0x28
  199. #define ARM_SMMU_CB_TTBCR 0x30
  200. #define ARM_SMMU_CB_CONTEXTIDR 0x34
  201. #define ARM_SMMU_CB_S1_MAIR0 0x38
  202. #define ARM_SMMU_CB_S1_MAIR1 0x3c
  203. #define ARM_SMMU_CB_PAR 0x50
  204. #define ARM_SMMU_CB_FSR 0x58
  205. #define ARM_SMMU_CB_FAR 0x60
  206. #define ARM_SMMU_CB_FSYNR0 0x68
  207. #define ARM_SMMU_CB_S1_TLBIVA 0x600
  208. #define ARM_SMMU_CB_S1_TLBIASID 0x610
  209. #define ARM_SMMU_CB_S1_TLBIVAL 0x620
  210. #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
  211. #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
  212. #define ARM_SMMU_CB_ATS1PR 0x800
  213. #define ARM_SMMU_CB_ATSR 0x8f0
  214. #define SCTLR_S1_ASIDPNE (1 << 12)
  215. #define SCTLR_CFCFG (1 << 7)
  216. #define SCTLR_CFIE (1 << 6)
  217. #define SCTLR_CFRE (1 << 5)
  218. #define SCTLR_E (1 << 4)
  219. #define SCTLR_AFE (1 << 2)
  220. #define SCTLR_TRE (1 << 1)
  221. #define SCTLR_M (1 << 0)
  222. #define ARM_MMU500_ACTLR_CPRE (1 << 1)
  223. #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
  224. #define CB_PAR_F (1 << 0)
  225. #define ATSR_ACTIVE (1 << 0)
  226. #define RESUME_RETRY (0 << 0)
  227. #define RESUME_TERMINATE (1 << 0)
  228. #define TTBCR2_SEP_SHIFT 15
  229. #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT)
  230. #define TTBRn_ASID_SHIFT 48
  231. #define FSR_MULTI (1 << 31)
  232. #define FSR_SS (1 << 30)
  233. #define FSR_UUT (1 << 8)
  234. #define FSR_ASF (1 << 7)
  235. #define FSR_TLBLKF (1 << 6)
  236. #define FSR_TLBMCF (1 << 5)
  237. #define FSR_EF (1 << 4)
  238. #define FSR_PF (1 << 3)
  239. #define FSR_AFF (1 << 2)
  240. #define FSR_TF (1 << 1)
  241. #define FSR_IGN (FSR_AFF | FSR_ASF | \
  242. FSR_TLBMCF | FSR_TLBLKF)
  243. #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
  244. FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
  245. #define FSYNR0_WNR (1 << 4)
  246. static int force_stage;
  247. module_param(force_stage, int, S_IRUGO);
  248. MODULE_PARM_DESC(force_stage,
  249. "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
  250. static bool disable_bypass;
  251. module_param(disable_bypass, bool, S_IRUGO);
  252. MODULE_PARM_DESC(disable_bypass,
  253. "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
  254. enum arm_smmu_arch_version {
  255. ARM_SMMU_V1,
  256. ARM_SMMU_V1_64K,
  257. ARM_SMMU_V2,
  258. };
  259. enum arm_smmu_implementation {
  260. GENERIC_SMMU,
  261. ARM_MMU500,
  262. CAVIUM_SMMUV2,
  263. };
  264. struct arm_smmu_s2cr {
  265. struct iommu_group *group;
  266. int count;
  267. enum arm_smmu_s2cr_type type;
  268. enum arm_smmu_s2cr_privcfg privcfg;
  269. u8 cbndx;
  270. };
  271. #define s2cr_init_val (struct arm_smmu_s2cr){ \
  272. .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
  273. }
  274. struct arm_smmu_smr {
  275. u16 mask;
  276. u16 id;
  277. bool valid;
  278. };
  279. struct arm_smmu_master_cfg {
  280. struct arm_smmu_device *smmu;
  281. s16 smendx[];
  282. };
  283. #define INVALID_SMENDX -1
  284. #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
  285. #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
  286. #define fwspec_smendx(fw, i) \
  287. (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
  288. #define for_each_cfg_sme(fw, i, idx) \
  289. for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
  290. struct arm_smmu_device {
  291. struct device *dev;
  292. void __iomem *base;
  293. unsigned long size;
  294. unsigned long pgshift;
  295. #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
  296. #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
  297. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
  298. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
  299. #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
  300. #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
  301. #define ARM_SMMU_FEAT_VMID16 (1 << 6)
  302. #define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
  303. #define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
  304. #define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
  305. #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
  306. #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
  307. u32 features;
  308. #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
  309. u32 options;
  310. enum arm_smmu_arch_version version;
  311. enum arm_smmu_implementation model;
  312. u32 num_context_banks;
  313. u32 num_s2_context_banks;
  314. DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
  315. atomic_t irptndx;
  316. u32 num_mapping_groups;
  317. u16 streamid_mask;
  318. u16 smr_mask_mask;
  319. struct arm_smmu_smr *smrs;
  320. struct arm_smmu_s2cr *s2crs;
  321. struct mutex stream_map_mutex;
  322. unsigned long va_size;
  323. unsigned long ipa_size;
  324. unsigned long pa_size;
  325. unsigned long pgsize_bitmap;
  326. u32 num_global_irqs;
  327. u32 num_context_irqs;
  328. unsigned int *irqs;
  329. u32 cavium_id_base; /* Specific to Cavium */
  330. };
  331. enum arm_smmu_context_fmt {
  332. ARM_SMMU_CTX_FMT_NONE,
  333. ARM_SMMU_CTX_FMT_AARCH64,
  334. ARM_SMMU_CTX_FMT_AARCH32_L,
  335. ARM_SMMU_CTX_FMT_AARCH32_S,
  336. };
  337. struct arm_smmu_cfg {
  338. u8 cbndx;
  339. u8 irptndx;
  340. u32 cbar;
  341. enum arm_smmu_context_fmt fmt;
  342. };
  343. #define INVALID_IRPTNDX 0xff
  344. #define ARM_SMMU_CB_ASID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx)
  345. #define ARM_SMMU_CB_VMID(smmu, cfg) ((u16)(smmu)->cavium_id_base + (cfg)->cbndx + 1)
  346. enum arm_smmu_domain_stage {
  347. ARM_SMMU_DOMAIN_S1 = 0,
  348. ARM_SMMU_DOMAIN_S2,
  349. ARM_SMMU_DOMAIN_NESTED,
  350. };
  351. struct arm_smmu_domain {
  352. struct arm_smmu_device *smmu;
  353. struct io_pgtable_ops *pgtbl_ops;
  354. spinlock_t pgtbl_lock;
  355. struct arm_smmu_cfg cfg;
  356. enum arm_smmu_domain_stage stage;
  357. struct mutex init_mutex; /* Protects smmu pointer */
  358. struct iommu_domain domain;
  359. };
  360. struct arm_smmu_option_prop {
  361. u32 opt;
  362. const char *prop;
  363. };
  364. static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
  365. static bool using_legacy_binding, using_generic_binding;
  366. static struct arm_smmu_option_prop arm_smmu_options[] = {
  367. { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
  368. { 0, NULL},
  369. };
  370. static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
  371. {
  372. return container_of(dom, struct arm_smmu_domain, domain);
  373. }
  374. static void parse_driver_options(struct arm_smmu_device *smmu)
  375. {
  376. int i = 0;
  377. do {
  378. if (of_property_read_bool(smmu->dev->of_node,
  379. arm_smmu_options[i].prop)) {
  380. smmu->options |= arm_smmu_options[i].opt;
  381. dev_notice(smmu->dev, "option %s\n",
  382. arm_smmu_options[i].prop);
  383. }
  384. } while (arm_smmu_options[++i].opt);
  385. }
  386. static struct device_node *dev_get_dev_node(struct device *dev)
  387. {
  388. if (dev_is_pci(dev)) {
  389. struct pci_bus *bus = to_pci_dev(dev)->bus;
  390. while (!pci_is_root_bus(bus))
  391. bus = bus->parent;
  392. return of_node_get(bus->bridge->parent->of_node);
  393. }
  394. return of_node_get(dev->of_node);
  395. }
  396. static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
  397. {
  398. *((__be32 *)data) = cpu_to_be32(alias);
  399. return 0; /* Continue walking */
  400. }
  401. static int __find_legacy_master_phandle(struct device *dev, void *data)
  402. {
  403. struct of_phandle_iterator *it = *(void **)data;
  404. struct device_node *np = it->node;
  405. int err;
  406. of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
  407. "#stream-id-cells", 0)
  408. if (it->node == np) {
  409. *(void **)data = dev;
  410. return 1;
  411. }
  412. it->node = np;
  413. return err == -ENOENT ? 0 : err;
  414. }
  415. static struct platform_driver arm_smmu_driver;
  416. static struct iommu_ops arm_smmu_ops;
  417. static int arm_smmu_register_legacy_master(struct device *dev,
  418. struct arm_smmu_device **smmu)
  419. {
  420. struct device *smmu_dev;
  421. struct device_node *np;
  422. struct of_phandle_iterator it;
  423. void *data = &it;
  424. u32 *sids;
  425. __be32 pci_sid;
  426. int err;
  427. np = dev_get_dev_node(dev);
  428. if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
  429. of_node_put(np);
  430. return -ENODEV;
  431. }
  432. it.node = np;
  433. err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
  434. __find_legacy_master_phandle);
  435. smmu_dev = data;
  436. of_node_put(np);
  437. if (err == 0)
  438. return -ENODEV;
  439. if (err < 0)
  440. return err;
  441. if (dev_is_pci(dev)) {
  442. /* "mmu-masters" assumes Stream ID == Requester ID */
  443. pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
  444. &pci_sid);
  445. it.cur = &pci_sid;
  446. it.cur_count = 1;
  447. }
  448. err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
  449. &arm_smmu_ops);
  450. if (err)
  451. return err;
  452. sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
  453. if (!sids)
  454. return -ENOMEM;
  455. *smmu = dev_get_drvdata(smmu_dev);
  456. of_phandle_iterator_args(&it, sids, it.cur_count);
  457. err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
  458. kfree(sids);
  459. return err;
  460. }
  461. static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
  462. {
  463. int idx;
  464. do {
  465. idx = find_next_zero_bit(map, end, start);
  466. if (idx == end)
  467. return -ENOSPC;
  468. } while (test_and_set_bit(idx, map));
  469. return idx;
  470. }
  471. static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
  472. {
  473. clear_bit(idx, map);
  474. }
  475. /* Wait for any pending TLB invalidations to complete */
  476. static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
  477. {
  478. int count = 0;
  479. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  480. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
  481. while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
  482. & sTLBGSTATUS_GSACTIVE) {
  483. cpu_relax();
  484. if (++count == TLB_LOOP_TIMEOUT) {
  485. dev_err_ratelimited(smmu->dev,
  486. "TLB sync timed out -- SMMU may be deadlocked\n");
  487. return;
  488. }
  489. udelay(1);
  490. }
  491. }
  492. static void arm_smmu_tlb_sync(void *cookie)
  493. {
  494. struct arm_smmu_domain *smmu_domain = cookie;
  495. __arm_smmu_tlb_sync(smmu_domain->smmu);
  496. }
  497. static void arm_smmu_tlb_inv_context(void *cookie)
  498. {
  499. struct arm_smmu_domain *smmu_domain = cookie;
  500. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  501. struct arm_smmu_device *smmu = smmu_domain->smmu;
  502. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  503. void __iomem *base;
  504. if (stage1) {
  505. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  506. writel_relaxed(ARM_SMMU_CB_ASID(smmu, cfg),
  507. base + ARM_SMMU_CB_S1_TLBIASID);
  508. } else {
  509. base = ARM_SMMU_GR0(smmu);
  510. writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg),
  511. base + ARM_SMMU_GR0_TLBIVMID);
  512. }
  513. __arm_smmu_tlb_sync(smmu);
  514. }
  515. static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  516. size_t granule, bool leaf, void *cookie)
  517. {
  518. struct arm_smmu_domain *smmu_domain = cookie;
  519. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  520. struct arm_smmu_device *smmu = smmu_domain->smmu;
  521. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  522. void __iomem *reg;
  523. if (stage1) {
  524. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  525. reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
  526. if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
  527. iova &= ~12UL;
  528. iova |= ARM_SMMU_CB_ASID(smmu, cfg);
  529. do {
  530. writel_relaxed(iova, reg);
  531. iova += granule;
  532. } while (size -= granule);
  533. } else {
  534. iova >>= 12;
  535. iova |= (u64)ARM_SMMU_CB_ASID(smmu, cfg) << 48;
  536. do {
  537. writeq_relaxed(iova, reg);
  538. iova += granule >> 12;
  539. } while (size -= granule);
  540. }
  541. } else if (smmu->version == ARM_SMMU_V2) {
  542. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  543. reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
  544. ARM_SMMU_CB_S2_TLBIIPAS2;
  545. iova >>= 12;
  546. do {
  547. smmu_write_atomic_lq(iova, reg);
  548. iova += granule >> 12;
  549. } while (size -= granule);
  550. } else {
  551. reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
  552. writel_relaxed(ARM_SMMU_CB_VMID(smmu, cfg), reg);
  553. }
  554. }
  555. static struct iommu_gather_ops arm_smmu_gather_ops = {
  556. .tlb_flush_all = arm_smmu_tlb_inv_context,
  557. .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
  558. .tlb_sync = arm_smmu_tlb_sync,
  559. };
  560. static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  561. {
  562. u32 fsr, fsynr;
  563. unsigned long iova;
  564. struct iommu_domain *domain = dev;
  565. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  566. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  567. struct arm_smmu_device *smmu = smmu_domain->smmu;
  568. void __iomem *cb_base;
  569. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  570. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  571. if (!(fsr & FSR_FAULT))
  572. return IRQ_NONE;
  573. fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
  574. iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR);
  575. dev_err_ratelimited(smmu->dev,
  576. "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cb=%d\n",
  577. fsr, iova, fsynr, cfg->cbndx);
  578. writel(fsr, cb_base + ARM_SMMU_CB_FSR);
  579. return IRQ_HANDLED;
  580. }
  581. static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
  582. {
  583. u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
  584. struct arm_smmu_device *smmu = dev;
  585. void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
  586. gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
  587. gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
  588. gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
  589. gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
  590. if (!gfsr)
  591. return IRQ_NONE;
  592. dev_err_ratelimited(smmu->dev,
  593. "Unexpected global fault, this could be serious\n");
  594. dev_err_ratelimited(smmu->dev,
  595. "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
  596. gfsr, gfsynr0, gfsynr1, gfsynr2);
  597. writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
  598. return IRQ_HANDLED;
  599. }
  600. static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
  601. struct io_pgtable_cfg *pgtbl_cfg)
  602. {
  603. u32 reg, reg2;
  604. u64 reg64;
  605. bool stage1;
  606. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  607. struct arm_smmu_device *smmu = smmu_domain->smmu;
  608. void __iomem *cb_base, *gr1_base;
  609. gr1_base = ARM_SMMU_GR1(smmu);
  610. stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  611. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  612. if (smmu->version > ARM_SMMU_V1) {
  613. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
  614. reg = CBA2R_RW64_64BIT;
  615. else
  616. reg = CBA2R_RW64_32BIT;
  617. /* 16-bit VMIDs live in CBA2R */
  618. if (smmu->features & ARM_SMMU_FEAT_VMID16)
  619. reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBA2R_VMID_SHIFT;
  620. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
  621. }
  622. /* CBAR */
  623. reg = cfg->cbar;
  624. if (smmu->version < ARM_SMMU_V2)
  625. reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
  626. /*
  627. * Use the weakest shareability/memory types, so they are
  628. * overridden by the ttbcr/pte.
  629. */
  630. if (stage1) {
  631. reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
  632. (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
  633. } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
  634. /* 8-bit VMIDs live in CBAR */
  635. reg |= ARM_SMMU_CB_VMID(smmu, cfg) << CBAR_VMID_SHIFT;
  636. }
  637. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
  638. /* TTBRs */
  639. if (stage1) {
  640. u16 asid = ARM_SMMU_CB_ASID(smmu, cfg);
  641. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  642. reg = pgtbl_cfg->arm_v7s_cfg.ttbr[0];
  643. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0);
  644. reg = pgtbl_cfg->arm_v7s_cfg.ttbr[1];
  645. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1);
  646. writel_relaxed(asid, cb_base + ARM_SMMU_CB_CONTEXTIDR);
  647. } else {
  648. reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
  649. reg64 |= (u64)asid << TTBRn_ASID_SHIFT;
  650. writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
  651. reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
  652. reg64 |= (u64)asid << TTBRn_ASID_SHIFT;
  653. writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR1);
  654. }
  655. } else {
  656. reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
  657. writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0);
  658. }
  659. /* TTBCR */
  660. if (stage1) {
  661. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  662. reg = pgtbl_cfg->arm_v7s_cfg.tcr;
  663. reg2 = 0;
  664. } else {
  665. reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
  666. reg2 = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
  667. reg2 |= TTBCR2_SEP_UPSTREAM;
  668. }
  669. if (smmu->version > ARM_SMMU_V1)
  670. writel_relaxed(reg2, cb_base + ARM_SMMU_CB_TTBCR2);
  671. } else {
  672. reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
  673. }
  674. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
  675. /* MAIRs (stage-1 only) */
  676. if (stage1) {
  677. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
  678. reg = pgtbl_cfg->arm_v7s_cfg.prrr;
  679. reg2 = pgtbl_cfg->arm_v7s_cfg.nmrr;
  680. } else {
  681. reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
  682. reg2 = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
  683. }
  684. writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
  685. writel_relaxed(reg2, cb_base + ARM_SMMU_CB_S1_MAIR1);
  686. }
  687. /* SCTLR */
  688. reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | SCTLR_M;
  689. if (stage1)
  690. reg |= SCTLR_S1_ASIDPNE;
  691. #ifdef __BIG_ENDIAN
  692. reg |= SCTLR_E;
  693. #endif
  694. writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
  695. }
  696. static int arm_smmu_init_domain_context(struct iommu_domain *domain,
  697. struct arm_smmu_device *smmu)
  698. {
  699. int irq, start, ret = 0;
  700. unsigned long ias, oas;
  701. struct io_pgtable_ops *pgtbl_ops;
  702. struct io_pgtable_cfg pgtbl_cfg;
  703. enum io_pgtable_fmt fmt;
  704. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  705. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  706. mutex_lock(&smmu_domain->init_mutex);
  707. if (smmu_domain->smmu)
  708. goto out_unlock;
  709. /*
  710. * Mapping the requested stage onto what we support is surprisingly
  711. * complicated, mainly because the spec allows S1+S2 SMMUs without
  712. * support for nested translation. That means we end up with the
  713. * following table:
  714. *
  715. * Requested Supported Actual
  716. * S1 N S1
  717. * S1 S1+S2 S1
  718. * S1 S2 S2
  719. * S1 S1 S1
  720. * N N N
  721. * N S1+S2 S2
  722. * N S2 S2
  723. * N S1 S1
  724. *
  725. * Note that you can't actually request stage-2 mappings.
  726. */
  727. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
  728. smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
  729. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
  730. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  731. /*
  732. * Choosing a suitable context format is even more fiddly. Until we
  733. * grow some way for the caller to express a preference, and/or move
  734. * the decision into the io-pgtable code where it arguably belongs,
  735. * just aim for the closest thing to the rest of the system, and hope
  736. * that the hardware isn't esoteric enough that we can't assume AArch64
  737. * support to be a superset of AArch32 support...
  738. */
  739. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
  740. cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
  741. if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
  742. !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
  743. (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
  744. (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
  745. cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
  746. if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
  747. (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
  748. ARM_SMMU_FEAT_FMT_AARCH64_16K |
  749. ARM_SMMU_FEAT_FMT_AARCH64_4K)))
  750. cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
  751. if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
  752. ret = -EINVAL;
  753. goto out_unlock;
  754. }
  755. switch (smmu_domain->stage) {
  756. case ARM_SMMU_DOMAIN_S1:
  757. cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
  758. start = smmu->num_s2_context_banks;
  759. ias = smmu->va_size;
  760. oas = smmu->ipa_size;
  761. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
  762. fmt = ARM_64_LPAE_S1;
  763. } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
  764. fmt = ARM_32_LPAE_S1;
  765. ias = min(ias, 32UL);
  766. oas = min(oas, 40UL);
  767. } else {
  768. fmt = ARM_V7S;
  769. ias = min(ias, 32UL);
  770. oas = min(oas, 32UL);
  771. }
  772. break;
  773. case ARM_SMMU_DOMAIN_NESTED:
  774. /*
  775. * We will likely want to change this if/when KVM gets
  776. * involved.
  777. */
  778. case ARM_SMMU_DOMAIN_S2:
  779. cfg->cbar = CBAR_TYPE_S2_TRANS;
  780. start = 0;
  781. ias = smmu->ipa_size;
  782. oas = smmu->pa_size;
  783. if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
  784. fmt = ARM_64_LPAE_S2;
  785. } else {
  786. fmt = ARM_32_LPAE_S2;
  787. ias = min(ias, 40UL);
  788. oas = min(oas, 40UL);
  789. }
  790. break;
  791. default:
  792. ret = -EINVAL;
  793. goto out_unlock;
  794. }
  795. ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
  796. smmu->num_context_banks);
  797. if (ret < 0)
  798. goto out_unlock;
  799. cfg->cbndx = ret;
  800. if (smmu->version < ARM_SMMU_V2) {
  801. cfg->irptndx = atomic_inc_return(&smmu->irptndx);
  802. cfg->irptndx %= smmu->num_context_irqs;
  803. } else {
  804. cfg->irptndx = cfg->cbndx;
  805. }
  806. pgtbl_cfg = (struct io_pgtable_cfg) {
  807. .pgsize_bitmap = smmu->pgsize_bitmap,
  808. .ias = ias,
  809. .oas = oas,
  810. .tlb = &arm_smmu_gather_ops,
  811. .iommu_dev = smmu->dev,
  812. };
  813. smmu_domain->smmu = smmu;
  814. pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
  815. if (!pgtbl_ops) {
  816. ret = -ENOMEM;
  817. goto out_clear_smmu;
  818. }
  819. /* Update the domain's page sizes to reflect the page table format */
  820. domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
  821. domain->geometry.aperture_end = (1UL << ias) - 1;
  822. domain->geometry.force_aperture = true;
  823. /* Initialise the context bank with our page table cfg */
  824. arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
  825. /*
  826. * Request context fault interrupt. Do this last to avoid the
  827. * handler seeing a half-initialised domain state.
  828. */
  829. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  830. ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
  831. IRQF_SHARED, "arm-smmu-context-fault", domain);
  832. if (ret < 0) {
  833. dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
  834. cfg->irptndx, irq);
  835. cfg->irptndx = INVALID_IRPTNDX;
  836. }
  837. mutex_unlock(&smmu_domain->init_mutex);
  838. /* Publish page table ops for map/unmap */
  839. smmu_domain->pgtbl_ops = pgtbl_ops;
  840. return 0;
  841. out_clear_smmu:
  842. smmu_domain->smmu = NULL;
  843. out_unlock:
  844. mutex_unlock(&smmu_domain->init_mutex);
  845. return ret;
  846. }
  847. static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
  848. {
  849. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  850. struct arm_smmu_device *smmu = smmu_domain->smmu;
  851. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  852. void __iomem *cb_base;
  853. int irq;
  854. if (!smmu)
  855. return;
  856. /*
  857. * Disable the context bank and free the page tables before freeing
  858. * it.
  859. */
  860. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  861. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  862. if (cfg->irptndx != INVALID_IRPTNDX) {
  863. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  864. devm_free_irq(smmu->dev, irq, domain);
  865. }
  866. free_io_pgtable_ops(smmu_domain->pgtbl_ops);
  867. __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
  868. }
  869. static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
  870. {
  871. struct arm_smmu_domain *smmu_domain;
  872. if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
  873. return NULL;
  874. /*
  875. * Allocate the domain and initialise some of its data structures.
  876. * We can't really do anything meaningful until we've added a
  877. * master.
  878. */
  879. smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
  880. if (!smmu_domain)
  881. return NULL;
  882. if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
  883. iommu_get_dma_cookie(&smmu_domain->domain))) {
  884. kfree(smmu_domain);
  885. return NULL;
  886. }
  887. mutex_init(&smmu_domain->init_mutex);
  888. spin_lock_init(&smmu_domain->pgtbl_lock);
  889. return &smmu_domain->domain;
  890. }
  891. static void arm_smmu_domain_free(struct iommu_domain *domain)
  892. {
  893. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  894. /*
  895. * Free the domain resources. We assume that all devices have
  896. * already been detached.
  897. */
  898. iommu_put_dma_cookie(domain);
  899. arm_smmu_destroy_domain_context(domain);
  900. kfree(smmu_domain);
  901. }
  902. static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
  903. {
  904. struct arm_smmu_smr *smr = smmu->smrs + idx;
  905. u32 reg = smr->id << SMR_ID_SHIFT | smr->mask << SMR_MASK_SHIFT;
  906. if (smr->valid)
  907. reg |= SMR_VALID;
  908. writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx));
  909. }
  910. static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
  911. {
  912. struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
  913. u32 reg = (s2cr->type & S2CR_TYPE_MASK) << S2CR_TYPE_SHIFT |
  914. (s2cr->cbndx & S2CR_CBNDX_MASK) << S2CR_CBNDX_SHIFT |
  915. (s2cr->privcfg & S2CR_PRIVCFG_MASK) << S2CR_PRIVCFG_SHIFT;
  916. writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx));
  917. }
  918. static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
  919. {
  920. arm_smmu_write_s2cr(smmu, idx);
  921. if (smmu->smrs)
  922. arm_smmu_write_smr(smmu, idx);
  923. }
  924. static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
  925. {
  926. struct arm_smmu_smr *smrs = smmu->smrs;
  927. int i, free_idx = -ENOSPC;
  928. /* Stream indexing is blissfully easy */
  929. if (!smrs)
  930. return id;
  931. /* Validating SMRs is... less so */
  932. for (i = 0; i < smmu->num_mapping_groups; ++i) {
  933. if (!smrs[i].valid) {
  934. /*
  935. * Note the first free entry we come across, which
  936. * we'll claim in the end if nothing else matches.
  937. */
  938. if (free_idx < 0)
  939. free_idx = i;
  940. continue;
  941. }
  942. /*
  943. * If the new entry is _entirely_ matched by an existing entry,
  944. * then reuse that, with the guarantee that there also cannot
  945. * be any subsequent conflicting entries. In normal use we'd
  946. * expect simply identical entries for this case, but there's
  947. * no harm in accommodating the generalisation.
  948. */
  949. if ((mask & smrs[i].mask) == mask &&
  950. !((id ^ smrs[i].id) & ~smrs[i].mask))
  951. return i;
  952. /*
  953. * If the new entry has any other overlap with an existing one,
  954. * though, then there always exists at least one stream ID
  955. * which would cause a conflict, and we can't allow that risk.
  956. */
  957. if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
  958. return -EINVAL;
  959. }
  960. return free_idx;
  961. }
  962. static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
  963. {
  964. if (--smmu->s2crs[idx].count)
  965. return false;
  966. smmu->s2crs[idx] = s2cr_init_val;
  967. if (smmu->smrs)
  968. smmu->smrs[idx].valid = false;
  969. return true;
  970. }
  971. static int arm_smmu_master_alloc_smes(struct device *dev)
  972. {
  973. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  974. struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
  975. struct arm_smmu_device *smmu = cfg->smmu;
  976. struct arm_smmu_smr *smrs = smmu->smrs;
  977. struct iommu_group *group;
  978. int i, idx, ret;
  979. mutex_lock(&smmu->stream_map_mutex);
  980. /* Figure out a viable stream map entry allocation */
  981. for_each_cfg_sme(fwspec, i, idx) {
  982. u16 sid = fwspec->ids[i];
  983. u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
  984. if (idx != INVALID_SMENDX) {
  985. ret = -EEXIST;
  986. goto out_err;
  987. }
  988. ret = arm_smmu_find_sme(smmu, sid, mask);
  989. if (ret < 0)
  990. goto out_err;
  991. idx = ret;
  992. if (smrs && smmu->s2crs[idx].count == 0) {
  993. smrs[idx].id = sid;
  994. smrs[idx].mask = mask;
  995. smrs[idx].valid = true;
  996. }
  997. smmu->s2crs[idx].count++;
  998. cfg->smendx[i] = (s16)idx;
  999. }
  1000. group = iommu_group_get_for_dev(dev);
  1001. if (!group)
  1002. group = ERR_PTR(-ENOMEM);
  1003. if (IS_ERR(group)) {
  1004. ret = PTR_ERR(group);
  1005. goto out_err;
  1006. }
  1007. iommu_group_put(group);
  1008. /* It worked! Now, poke the actual hardware */
  1009. for_each_cfg_sme(fwspec, i, idx) {
  1010. arm_smmu_write_sme(smmu, idx);
  1011. smmu->s2crs[idx].group = group;
  1012. }
  1013. mutex_unlock(&smmu->stream_map_mutex);
  1014. return 0;
  1015. out_err:
  1016. while (i--) {
  1017. arm_smmu_free_sme(smmu, cfg->smendx[i]);
  1018. cfg->smendx[i] = INVALID_SMENDX;
  1019. }
  1020. mutex_unlock(&smmu->stream_map_mutex);
  1021. return ret;
  1022. }
  1023. static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec)
  1024. {
  1025. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  1026. struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
  1027. int i, idx;
  1028. mutex_lock(&smmu->stream_map_mutex);
  1029. for_each_cfg_sme(fwspec, i, idx) {
  1030. if (arm_smmu_free_sme(smmu, idx))
  1031. arm_smmu_write_sme(smmu, idx);
  1032. cfg->smendx[i] = INVALID_SMENDX;
  1033. }
  1034. mutex_unlock(&smmu->stream_map_mutex);
  1035. }
  1036. static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
  1037. struct iommu_fwspec *fwspec)
  1038. {
  1039. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1040. struct arm_smmu_s2cr *s2cr = smmu->s2crs;
  1041. enum arm_smmu_s2cr_type type = S2CR_TYPE_TRANS;
  1042. u8 cbndx = smmu_domain->cfg.cbndx;
  1043. int i, idx;
  1044. for_each_cfg_sme(fwspec, i, idx) {
  1045. if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
  1046. continue;
  1047. s2cr[idx].type = type;
  1048. s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
  1049. s2cr[idx].cbndx = cbndx;
  1050. arm_smmu_write_s2cr(smmu, idx);
  1051. }
  1052. return 0;
  1053. }
  1054. static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  1055. {
  1056. int ret;
  1057. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1058. struct arm_smmu_device *smmu;
  1059. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1060. if (!fwspec || fwspec->ops != &arm_smmu_ops) {
  1061. dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
  1062. return -ENXIO;
  1063. }
  1064. /*
  1065. * FIXME: The arch/arm DMA API code tries to attach devices to its own
  1066. * domains between of_xlate() and add_device() - we have no way to cope
  1067. * with that, so until ARM gets converted to rely on groups and default
  1068. * domains, just say no (but more politely than by dereferencing NULL).
  1069. * This should be at least a WARN_ON once that's sorted.
  1070. */
  1071. if (!fwspec->iommu_priv)
  1072. return -ENODEV;
  1073. smmu = fwspec_smmu(fwspec);
  1074. /* Ensure that the domain is finalised */
  1075. ret = arm_smmu_init_domain_context(domain, smmu);
  1076. if (ret < 0)
  1077. return ret;
  1078. /*
  1079. * Sanity check the domain. We don't support domains across
  1080. * different SMMUs.
  1081. */
  1082. if (smmu_domain->smmu != smmu) {
  1083. dev_err(dev,
  1084. "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
  1085. dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
  1086. return -EINVAL;
  1087. }
  1088. /* Looks ok, so add the device to the domain */
  1089. return arm_smmu_domain_add_master(smmu_domain, fwspec);
  1090. }
  1091. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  1092. phys_addr_t paddr, size_t size, int prot)
  1093. {
  1094. int ret;
  1095. unsigned long flags;
  1096. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1097. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1098. if (!ops)
  1099. return -ENODEV;
  1100. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1101. ret = ops->map(ops, iova, paddr, size, prot);
  1102. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1103. return ret;
  1104. }
  1105. static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  1106. size_t size)
  1107. {
  1108. size_t ret;
  1109. unsigned long flags;
  1110. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1111. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1112. if (!ops)
  1113. return 0;
  1114. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1115. ret = ops->unmap(ops, iova, size);
  1116. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1117. return ret;
  1118. }
  1119. static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
  1120. dma_addr_t iova)
  1121. {
  1122. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1123. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1124. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1125. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1126. struct device *dev = smmu->dev;
  1127. void __iomem *cb_base;
  1128. u32 tmp;
  1129. u64 phys;
  1130. unsigned long va;
  1131. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1132. /* ATS1 registers can only be written atomically */
  1133. va = iova & ~0xfffUL;
  1134. if (smmu->version == ARM_SMMU_V2)
  1135. smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR);
  1136. else /* Register is only 32-bit in v1 */
  1137. writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
  1138. if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
  1139. !(tmp & ATSR_ACTIVE), 5, 50)) {
  1140. dev_err(dev,
  1141. "iova to phys timed out on %pad. Falling back to software table walk.\n",
  1142. &iova);
  1143. return ops->iova_to_phys(ops, iova);
  1144. }
  1145. phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR);
  1146. if (phys & CB_PAR_F) {
  1147. dev_err(dev, "translation fault!\n");
  1148. dev_err(dev, "PAR = 0x%llx\n", phys);
  1149. return 0;
  1150. }
  1151. return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
  1152. }
  1153. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  1154. dma_addr_t iova)
  1155. {
  1156. phys_addr_t ret;
  1157. unsigned long flags;
  1158. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1159. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1160. if (!ops)
  1161. return 0;
  1162. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1163. if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
  1164. smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1165. ret = arm_smmu_iova_to_phys_hard(domain, iova);
  1166. } else {
  1167. ret = ops->iova_to_phys(ops, iova);
  1168. }
  1169. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1170. return ret;
  1171. }
  1172. static bool arm_smmu_capable(enum iommu_cap cap)
  1173. {
  1174. switch (cap) {
  1175. case IOMMU_CAP_CACHE_COHERENCY:
  1176. /*
  1177. * Return true here as the SMMU can always send out coherent
  1178. * requests.
  1179. */
  1180. return true;
  1181. case IOMMU_CAP_INTR_REMAP:
  1182. return true; /* MSIs are just memory writes */
  1183. case IOMMU_CAP_NOEXEC:
  1184. return true;
  1185. default:
  1186. return false;
  1187. }
  1188. }
  1189. static int arm_smmu_match_node(struct device *dev, void *data)
  1190. {
  1191. return dev->of_node == data;
  1192. }
  1193. static struct arm_smmu_device *arm_smmu_get_by_node(struct device_node *np)
  1194. {
  1195. struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
  1196. np, arm_smmu_match_node);
  1197. put_device(dev);
  1198. return dev ? dev_get_drvdata(dev) : NULL;
  1199. }
  1200. static int arm_smmu_add_device(struct device *dev)
  1201. {
  1202. struct arm_smmu_device *smmu;
  1203. struct arm_smmu_master_cfg *cfg;
  1204. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1205. int i, ret;
  1206. if (using_legacy_binding) {
  1207. ret = arm_smmu_register_legacy_master(dev, &smmu);
  1208. fwspec = dev->iommu_fwspec;
  1209. if (ret)
  1210. goto out_free;
  1211. } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
  1212. smmu = arm_smmu_get_by_node(to_of_node(fwspec->iommu_fwnode));
  1213. } else {
  1214. return -ENODEV;
  1215. }
  1216. ret = -EINVAL;
  1217. for (i = 0; i < fwspec->num_ids; i++) {
  1218. u16 sid = fwspec->ids[i];
  1219. u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
  1220. if (sid & ~smmu->streamid_mask) {
  1221. dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
  1222. sid, smmu->streamid_mask);
  1223. goto out_free;
  1224. }
  1225. if (mask & ~smmu->smr_mask_mask) {
  1226. dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
  1227. sid, smmu->smr_mask_mask);
  1228. goto out_free;
  1229. }
  1230. }
  1231. ret = -ENOMEM;
  1232. cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
  1233. GFP_KERNEL);
  1234. if (!cfg)
  1235. goto out_free;
  1236. cfg->smmu = smmu;
  1237. fwspec->iommu_priv = cfg;
  1238. while (i--)
  1239. cfg->smendx[i] = INVALID_SMENDX;
  1240. ret = arm_smmu_master_alloc_smes(dev);
  1241. if (ret)
  1242. goto out_free;
  1243. return 0;
  1244. out_free:
  1245. if (fwspec)
  1246. kfree(fwspec->iommu_priv);
  1247. iommu_fwspec_free(dev);
  1248. return ret;
  1249. }
  1250. static void arm_smmu_remove_device(struct device *dev)
  1251. {
  1252. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1253. if (!fwspec || fwspec->ops != &arm_smmu_ops)
  1254. return;
  1255. arm_smmu_master_free_smes(fwspec);
  1256. iommu_group_remove_device(dev);
  1257. kfree(fwspec->iommu_priv);
  1258. iommu_fwspec_free(dev);
  1259. }
  1260. static struct iommu_group *arm_smmu_device_group(struct device *dev)
  1261. {
  1262. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1263. struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
  1264. struct iommu_group *group = NULL;
  1265. int i, idx;
  1266. for_each_cfg_sme(fwspec, i, idx) {
  1267. if (group && smmu->s2crs[idx].group &&
  1268. group != smmu->s2crs[idx].group)
  1269. return ERR_PTR(-EINVAL);
  1270. group = smmu->s2crs[idx].group;
  1271. }
  1272. if (group)
  1273. return group;
  1274. if (dev_is_pci(dev))
  1275. group = pci_device_group(dev);
  1276. else
  1277. group = generic_device_group(dev);
  1278. return group;
  1279. }
  1280. static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
  1281. enum iommu_attr attr, void *data)
  1282. {
  1283. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1284. switch (attr) {
  1285. case DOMAIN_ATTR_NESTING:
  1286. *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
  1287. return 0;
  1288. default:
  1289. return -ENODEV;
  1290. }
  1291. }
  1292. static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
  1293. enum iommu_attr attr, void *data)
  1294. {
  1295. int ret = 0;
  1296. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1297. mutex_lock(&smmu_domain->init_mutex);
  1298. switch (attr) {
  1299. case DOMAIN_ATTR_NESTING:
  1300. if (smmu_domain->smmu) {
  1301. ret = -EPERM;
  1302. goto out_unlock;
  1303. }
  1304. if (*(int *)data)
  1305. smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
  1306. else
  1307. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  1308. break;
  1309. default:
  1310. ret = -ENODEV;
  1311. }
  1312. out_unlock:
  1313. mutex_unlock(&smmu_domain->init_mutex);
  1314. return ret;
  1315. }
  1316. static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
  1317. {
  1318. u32 fwid = 0;
  1319. if (args->args_count > 0)
  1320. fwid |= (u16)args->args[0];
  1321. if (args->args_count > 1)
  1322. fwid |= (u16)args->args[1] << SMR_MASK_SHIFT;
  1323. return iommu_fwspec_add_ids(dev, &fwid, 1);
  1324. }
  1325. static struct iommu_ops arm_smmu_ops = {
  1326. .capable = arm_smmu_capable,
  1327. .domain_alloc = arm_smmu_domain_alloc,
  1328. .domain_free = arm_smmu_domain_free,
  1329. .attach_dev = arm_smmu_attach_dev,
  1330. .map = arm_smmu_map,
  1331. .unmap = arm_smmu_unmap,
  1332. .map_sg = default_iommu_map_sg,
  1333. .iova_to_phys = arm_smmu_iova_to_phys,
  1334. .add_device = arm_smmu_add_device,
  1335. .remove_device = arm_smmu_remove_device,
  1336. .device_group = arm_smmu_device_group,
  1337. .domain_get_attr = arm_smmu_domain_get_attr,
  1338. .domain_set_attr = arm_smmu_domain_set_attr,
  1339. .of_xlate = arm_smmu_of_xlate,
  1340. .pgsize_bitmap = -1UL, /* Restricted during device attach */
  1341. };
  1342. static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
  1343. {
  1344. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1345. void __iomem *cb_base;
  1346. int i;
  1347. u32 reg, major;
  1348. /* clear global FSR */
  1349. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  1350. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  1351. /*
  1352. * Reset stream mapping groups: Initial values mark all SMRn as
  1353. * invalid and all S2CRn as bypass unless overridden.
  1354. */
  1355. for (i = 0; i < smmu->num_mapping_groups; ++i)
  1356. arm_smmu_write_sme(smmu, i);
  1357. /*
  1358. * Before clearing ARM_MMU500_ACTLR_CPRE, need to
  1359. * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
  1360. * bit is only present in MMU-500r2 onwards.
  1361. */
  1362. reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7);
  1363. major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK;
  1364. if ((smmu->model == ARM_MMU500) && (major >= 2)) {
  1365. reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR);
  1366. reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
  1367. writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR);
  1368. }
  1369. /* Make sure all context banks are disabled and clear CB_FSR */
  1370. for (i = 0; i < smmu->num_context_banks; ++i) {
  1371. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
  1372. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  1373. writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
  1374. /*
  1375. * Disable MMU-500's not-particularly-beneficial next-page
  1376. * prefetcher for the sake of errata #841119 and #826419.
  1377. */
  1378. if (smmu->model == ARM_MMU500) {
  1379. reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR);
  1380. reg &= ~ARM_MMU500_ACTLR_CPRE;
  1381. writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR);
  1382. }
  1383. }
  1384. /* Invalidate the TLB, just in case */
  1385. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
  1386. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
  1387. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1388. /* Enable fault reporting */
  1389. reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
  1390. /* Disable TLB broadcasting. */
  1391. reg |= (sCR0_VMIDPNE | sCR0_PTM);
  1392. /* Enable client access, handling unmatched streams as appropriate */
  1393. reg &= ~sCR0_CLIENTPD;
  1394. if (disable_bypass)
  1395. reg |= sCR0_USFCFG;
  1396. else
  1397. reg &= ~sCR0_USFCFG;
  1398. /* Disable forced broadcasting */
  1399. reg &= ~sCR0_FB;
  1400. /* Don't upgrade barriers */
  1401. reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
  1402. if (smmu->features & ARM_SMMU_FEAT_VMID16)
  1403. reg |= sCR0_VMID16EN;
  1404. /* Push the button */
  1405. __arm_smmu_tlb_sync(smmu);
  1406. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1407. }
  1408. static int arm_smmu_id_size_to_bits(int size)
  1409. {
  1410. switch (size) {
  1411. case 0:
  1412. return 32;
  1413. case 1:
  1414. return 36;
  1415. case 2:
  1416. return 40;
  1417. case 3:
  1418. return 42;
  1419. case 4:
  1420. return 44;
  1421. case 5:
  1422. default:
  1423. return 48;
  1424. }
  1425. }
  1426. static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
  1427. {
  1428. unsigned long size;
  1429. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1430. u32 id;
  1431. bool cttw_dt, cttw_reg;
  1432. int i;
  1433. dev_notice(smmu->dev, "probing hardware configuration...\n");
  1434. dev_notice(smmu->dev, "SMMUv%d with:\n",
  1435. smmu->version == ARM_SMMU_V2 ? 2 : 1);
  1436. /* ID0 */
  1437. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
  1438. /* Restrict available stages based on module parameter */
  1439. if (force_stage == 1)
  1440. id &= ~(ID0_S2TS | ID0_NTS);
  1441. else if (force_stage == 2)
  1442. id &= ~(ID0_S1TS | ID0_NTS);
  1443. if (id & ID0_S1TS) {
  1444. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  1445. dev_notice(smmu->dev, "\tstage 1 translation\n");
  1446. }
  1447. if (id & ID0_S2TS) {
  1448. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  1449. dev_notice(smmu->dev, "\tstage 2 translation\n");
  1450. }
  1451. if (id & ID0_NTS) {
  1452. smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
  1453. dev_notice(smmu->dev, "\tnested translation\n");
  1454. }
  1455. if (!(smmu->features &
  1456. (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
  1457. dev_err(smmu->dev, "\tno translation support!\n");
  1458. return -ENODEV;
  1459. }
  1460. if ((id & ID0_S1TS) &&
  1461. ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
  1462. smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
  1463. dev_notice(smmu->dev, "\taddress translation ops\n");
  1464. }
  1465. /*
  1466. * In order for DMA API calls to work properly, we must defer to what
  1467. * the DT says about coherency, regardless of what the hardware claims.
  1468. * Fortunately, this also opens up a workaround for systems where the
  1469. * ID register value has ended up configured incorrectly.
  1470. */
  1471. cttw_dt = of_dma_is_coherent(smmu->dev->of_node);
  1472. cttw_reg = !!(id & ID0_CTTW);
  1473. if (cttw_dt)
  1474. smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
  1475. if (cttw_dt || cttw_reg)
  1476. dev_notice(smmu->dev, "\t%scoherent table walk\n",
  1477. cttw_dt ? "" : "non-");
  1478. if (cttw_dt != cttw_reg)
  1479. dev_notice(smmu->dev,
  1480. "\t(IDR0.CTTW overridden by dma-coherent property)\n");
  1481. /* Max. number of entries we have for stream matching/indexing */
  1482. size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK);
  1483. smmu->streamid_mask = size - 1;
  1484. if (id & ID0_SMS) {
  1485. u32 smr;
  1486. smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
  1487. size = (id >> ID0_NUMSMRG_SHIFT) & ID0_NUMSMRG_MASK;
  1488. if (size == 0) {
  1489. dev_err(smmu->dev,
  1490. "stream-matching supported, but no SMRs present!\n");
  1491. return -ENODEV;
  1492. }
  1493. /*
  1494. * SMR.ID bits may not be preserved if the corresponding MASK
  1495. * bits are set, so check each one separately. We can reject
  1496. * masters later if they try to claim IDs outside these masks.
  1497. */
  1498. smr = smmu->streamid_mask << SMR_ID_SHIFT;
  1499. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
  1500. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
  1501. smmu->streamid_mask = smr >> SMR_ID_SHIFT;
  1502. smr = smmu->streamid_mask << SMR_MASK_SHIFT;
  1503. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
  1504. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
  1505. smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT;
  1506. /* Zero-initialised to mark as invalid */
  1507. smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
  1508. GFP_KERNEL);
  1509. if (!smmu->smrs)
  1510. return -ENOMEM;
  1511. dev_notice(smmu->dev,
  1512. "\tstream matching with %lu register groups, mask 0x%x",
  1513. size, smmu->smr_mask_mask);
  1514. }
  1515. /* s2cr->type == 0 means translation, so initialise explicitly */
  1516. smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
  1517. GFP_KERNEL);
  1518. if (!smmu->s2crs)
  1519. return -ENOMEM;
  1520. for (i = 0; i < size; i++)
  1521. smmu->s2crs[i] = s2cr_init_val;
  1522. smmu->num_mapping_groups = size;
  1523. mutex_init(&smmu->stream_map_mutex);
  1524. if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
  1525. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
  1526. if (!(id & ID0_PTFS_NO_AARCH32S))
  1527. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
  1528. }
  1529. /* ID1 */
  1530. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
  1531. smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
  1532. /* Check for size mismatch of SMMU address space from mapped region */
  1533. size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
  1534. size *= 2 << smmu->pgshift;
  1535. if (smmu->size != size)
  1536. dev_warn(smmu->dev,
  1537. "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
  1538. size, smmu->size);
  1539. smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
  1540. smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
  1541. if (smmu->num_s2_context_banks > smmu->num_context_banks) {
  1542. dev_err(smmu->dev, "impossible number of S2 context banks!\n");
  1543. return -ENODEV;
  1544. }
  1545. dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
  1546. smmu->num_context_banks, smmu->num_s2_context_banks);
  1547. /*
  1548. * Cavium CN88xx erratum #27704.
  1549. * Ensure ASID and VMID allocation is unique across all SMMUs in
  1550. * the system.
  1551. */
  1552. if (smmu->model == CAVIUM_SMMUV2) {
  1553. smmu->cavium_id_base =
  1554. atomic_add_return(smmu->num_context_banks,
  1555. &cavium_smmu_context_count);
  1556. smmu->cavium_id_base -= smmu->num_context_banks;
  1557. }
  1558. /* ID2 */
  1559. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
  1560. size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
  1561. smmu->ipa_size = size;
  1562. /* The output mask is also applied for bypass */
  1563. size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
  1564. smmu->pa_size = size;
  1565. if (id & ID2_VMID16)
  1566. smmu->features |= ARM_SMMU_FEAT_VMID16;
  1567. /*
  1568. * What the page table walker can address actually depends on which
  1569. * descriptor format is in use, but since a) we don't know that yet,
  1570. * and b) it can vary per context bank, this will have to do...
  1571. */
  1572. if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
  1573. dev_warn(smmu->dev,
  1574. "failed to set DMA mask for table walker\n");
  1575. if (smmu->version < ARM_SMMU_V2) {
  1576. smmu->va_size = smmu->ipa_size;
  1577. if (smmu->version == ARM_SMMU_V1_64K)
  1578. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
  1579. } else {
  1580. size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
  1581. smmu->va_size = arm_smmu_id_size_to_bits(size);
  1582. if (id & ID2_PTFS_4K)
  1583. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
  1584. if (id & ID2_PTFS_16K)
  1585. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
  1586. if (id & ID2_PTFS_64K)
  1587. smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
  1588. }
  1589. /* Now we've corralled the various formats, what'll it do? */
  1590. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
  1591. smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
  1592. if (smmu->features &
  1593. (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
  1594. smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
  1595. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
  1596. smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
  1597. if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
  1598. smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
  1599. if (arm_smmu_ops.pgsize_bitmap == -1UL)
  1600. arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
  1601. else
  1602. arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
  1603. dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
  1604. smmu->pgsize_bitmap);
  1605. if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
  1606. dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
  1607. smmu->va_size, smmu->ipa_size);
  1608. if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
  1609. dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
  1610. smmu->ipa_size, smmu->pa_size);
  1611. return 0;
  1612. }
  1613. struct arm_smmu_match_data {
  1614. enum arm_smmu_arch_version version;
  1615. enum arm_smmu_implementation model;
  1616. };
  1617. #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
  1618. static struct arm_smmu_match_data name = { .version = ver, .model = imp }
  1619. ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
  1620. ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
  1621. ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
  1622. ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
  1623. ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
  1624. static const struct of_device_id arm_smmu_of_match[] = {
  1625. { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
  1626. { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
  1627. { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
  1628. { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
  1629. { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
  1630. { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
  1631. { },
  1632. };
  1633. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  1634. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  1635. {
  1636. const struct arm_smmu_match_data *data;
  1637. struct resource *res;
  1638. struct arm_smmu_device *smmu;
  1639. struct device *dev = &pdev->dev;
  1640. int num_irqs, i, err;
  1641. bool legacy_binding;
  1642. legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
  1643. if (legacy_binding && !using_generic_binding) {
  1644. if (!using_legacy_binding)
  1645. pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
  1646. using_legacy_binding = true;
  1647. } else if (!legacy_binding && !using_legacy_binding) {
  1648. using_generic_binding = true;
  1649. } else {
  1650. dev_err(dev, "not probing due to mismatched DT properties\n");
  1651. return -ENODEV;
  1652. }
  1653. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  1654. if (!smmu) {
  1655. dev_err(dev, "failed to allocate arm_smmu_device\n");
  1656. return -ENOMEM;
  1657. }
  1658. smmu->dev = dev;
  1659. data = of_device_get_match_data(dev);
  1660. smmu->version = data->version;
  1661. smmu->model = data->model;
  1662. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1663. smmu->base = devm_ioremap_resource(dev, res);
  1664. if (IS_ERR(smmu->base))
  1665. return PTR_ERR(smmu->base);
  1666. smmu->size = resource_size(res);
  1667. if (of_property_read_u32(dev->of_node, "#global-interrupts",
  1668. &smmu->num_global_irqs)) {
  1669. dev_err(dev, "missing #global-interrupts property\n");
  1670. return -ENODEV;
  1671. }
  1672. num_irqs = 0;
  1673. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
  1674. num_irqs++;
  1675. if (num_irqs > smmu->num_global_irqs)
  1676. smmu->num_context_irqs++;
  1677. }
  1678. if (!smmu->num_context_irqs) {
  1679. dev_err(dev, "found %d interrupts but expected at least %d\n",
  1680. num_irqs, smmu->num_global_irqs + 1);
  1681. return -ENODEV;
  1682. }
  1683. smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
  1684. GFP_KERNEL);
  1685. if (!smmu->irqs) {
  1686. dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
  1687. return -ENOMEM;
  1688. }
  1689. for (i = 0; i < num_irqs; ++i) {
  1690. int irq = platform_get_irq(pdev, i);
  1691. if (irq < 0) {
  1692. dev_err(dev, "failed to get irq index %d\n", i);
  1693. return -ENODEV;
  1694. }
  1695. smmu->irqs[i] = irq;
  1696. }
  1697. err = arm_smmu_device_cfg_probe(smmu);
  1698. if (err)
  1699. return err;
  1700. parse_driver_options(smmu);
  1701. if (smmu->version == ARM_SMMU_V2 &&
  1702. smmu->num_context_banks != smmu->num_context_irqs) {
  1703. dev_err(dev,
  1704. "found only %d context interrupt(s) but %d required\n",
  1705. smmu->num_context_irqs, smmu->num_context_banks);
  1706. return -ENODEV;
  1707. }
  1708. for (i = 0; i < smmu->num_global_irqs; ++i) {
  1709. err = devm_request_irq(smmu->dev, smmu->irqs[i],
  1710. arm_smmu_global_fault,
  1711. IRQF_SHARED,
  1712. "arm-smmu global fault",
  1713. smmu);
  1714. if (err) {
  1715. dev_err(dev, "failed to request global IRQ %d (%u)\n",
  1716. i, smmu->irqs[i]);
  1717. return err;
  1718. }
  1719. }
  1720. of_iommu_set_ops(dev->of_node, &arm_smmu_ops);
  1721. platform_set_drvdata(pdev, smmu);
  1722. arm_smmu_device_reset(smmu);
  1723. /* Oh, for a proper bus abstraction */
  1724. if (!iommu_present(&platform_bus_type))
  1725. bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  1726. #ifdef CONFIG_ARM_AMBA
  1727. if (!iommu_present(&amba_bustype))
  1728. bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  1729. #endif
  1730. #ifdef CONFIG_PCI
  1731. if (!iommu_present(&pci_bus_type)) {
  1732. pci_request_acs();
  1733. bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
  1734. }
  1735. #endif
  1736. return 0;
  1737. }
  1738. static int arm_smmu_device_remove(struct platform_device *pdev)
  1739. {
  1740. struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
  1741. if (!smmu)
  1742. return -ENODEV;
  1743. if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
  1744. dev_err(&pdev->dev, "removing device with active domains!\n");
  1745. /* Turn the thing off */
  1746. writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1747. return 0;
  1748. }
  1749. static struct platform_driver arm_smmu_driver = {
  1750. .driver = {
  1751. .name = "arm-smmu",
  1752. .of_match_table = of_match_ptr(arm_smmu_of_match),
  1753. },
  1754. .probe = arm_smmu_device_dt_probe,
  1755. .remove = arm_smmu_device_remove,
  1756. };
  1757. static int __init arm_smmu_init(void)
  1758. {
  1759. static bool registered;
  1760. int ret = 0;
  1761. if (!registered) {
  1762. ret = platform_driver_register(&arm_smmu_driver);
  1763. registered = !ret;
  1764. }
  1765. return ret;
  1766. }
  1767. static void __exit arm_smmu_exit(void)
  1768. {
  1769. return platform_driver_unregister(&arm_smmu_driver);
  1770. }
  1771. subsys_initcall(arm_smmu_init);
  1772. module_exit(arm_smmu_exit);
  1773. static int __init arm_smmu_of_init(struct device_node *np)
  1774. {
  1775. int ret = arm_smmu_init();
  1776. if (ret)
  1777. return ret;
  1778. if (!of_platform_device_create(np, NULL, platform_bus_type.dev_root))
  1779. return -ENODEV;
  1780. return 0;
  1781. }
  1782. IOMMU_OF_DECLARE(arm_smmuv1, "arm,smmu-v1", arm_smmu_of_init);
  1783. IOMMU_OF_DECLARE(arm_smmuv2, "arm,smmu-v2", arm_smmu_of_init);
  1784. IOMMU_OF_DECLARE(arm_mmu400, "arm,mmu-400", arm_smmu_of_init);
  1785. IOMMU_OF_DECLARE(arm_mmu401, "arm,mmu-401", arm_smmu_of_init);
  1786. IOMMU_OF_DECLARE(arm_mmu500, "arm,mmu-500", arm_smmu_of_init);
  1787. IOMMU_OF_DECLARE(cavium_smmuv2, "cavium,smmu-v2", arm_smmu_of_init);
  1788. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
  1789. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  1790. MODULE_LICENSE("GPL v2");