arm-smmu.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  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. * - 4k and 64k pages, with contiguous pte hints.
  27. * - Up to 42-bit addressing (dependent on VA_BITS)
  28. * - Context fault reporting
  29. */
  30. #define pr_fmt(fmt) "arm-smmu: " fmt
  31. #include <linux/delay.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/iommu.h>
  37. #include <linux/mm.h>
  38. #include <linux/module.h>
  39. #include <linux/of.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/amba/bus.h>
  44. #include <asm/pgalloc.h>
  45. /* Maximum number of stream IDs assigned to a single device */
  46. #define MAX_MASTER_STREAMIDS 8
  47. /* Maximum number of context banks per SMMU */
  48. #define ARM_SMMU_MAX_CBS 128
  49. /* Maximum number of mapping groups per SMMU */
  50. #define ARM_SMMU_MAX_SMRS 128
  51. /* SMMU global address space */
  52. #define ARM_SMMU_GR0(smmu) ((smmu)->base)
  53. #define ARM_SMMU_GR1(smmu) ((smmu)->base + (smmu)->pagesize)
  54. /* Page table bits */
  55. #define ARM_SMMU_PTE_XN (((pteval_t)3) << 53)
  56. #define ARM_SMMU_PTE_CONT (((pteval_t)1) << 52)
  57. #define ARM_SMMU_PTE_AF (((pteval_t)1) << 10)
  58. #define ARM_SMMU_PTE_SH_NS (((pteval_t)0) << 8)
  59. #define ARM_SMMU_PTE_SH_OS (((pteval_t)2) << 8)
  60. #define ARM_SMMU_PTE_SH_IS (((pteval_t)3) << 8)
  61. #define ARM_SMMU_PTE_PAGE (((pteval_t)3) << 0)
  62. #if PAGE_SIZE == SZ_4K
  63. #define ARM_SMMU_PTE_CONT_ENTRIES 16
  64. #elif PAGE_SIZE == SZ_64K
  65. #define ARM_SMMU_PTE_CONT_ENTRIES 32
  66. #else
  67. #define ARM_SMMU_PTE_CONT_ENTRIES 1
  68. #endif
  69. #define ARM_SMMU_PTE_CONT_SIZE (PAGE_SIZE * ARM_SMMU_PTE_CONT_ENTRIES)
  70. #define ARM_SMMU_PTE_CONT_MASK (~(ARM_SMMU_PTE_CONT_SIZE - 1))
  71. /* Stage-1 PTE */
  72. #define ARM_SMMU_PTE_AP_UNPRIV (((pteval_t)1) << 6)
  73. #define ARM_SMMU_PTE_AP_RDONLY (((pteval_t)2) << 6)
  74. #define ARM_SMMU_PTE_ATTRINDX_SHIFT 2
  75. #define ARM_SMMU_PTE_nG (((pteval_t)1) << 11)
  76. /* Stage-2 PTE */
  77. #define ARM_SMMU_PTE_HAP_FAULT (((pteval_t)0) << 6)
  78. #define ARM_SMMU_PTE_HAP_READ (((pteval_t)1) << 6)
  79. #define ARM_SMMU_PTE_HAP_WRITE (((pteval_t)2) << 6)
  80. #define ARM_SMMU_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2)
  81. #define ARM_SMMU_PTE_MEMATTR_NC (((pteval_t)0x5) << 2)
  82. #define ARM_SMMU_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2)
  83. /* Configuration registers */
  84. #define ARM_SMMU_GR0_sCR0 0x0
  85. #define sCR0_CLIENTPD (1 << 0)
  86. #define sCR0_GFRE (1 << 1)
  87. #define sCR0_GFIE (1 << 2)
  88. #define sCR0_GCFGFRE (1 << 4)
  89. #define sCR0_GCFGFIE (1 << 5)
  90. #define sCR0_USFCFG (1 << 10)
  91. #define sCR0_VMIDPNE (1 << 11)
  92. #define sCR0_PTM (1 << 12)
  93. #define sCR0_FB (1 << 13)
  94. #define sCR0_BSU_SHIFT 14
  95. #define sCR0_BSU_MASK 0x3
  96. /* Identification registers */
  97. #define ARM_SMMU_GR0_ID0 0x20
  98. #define ARM_SMMU_GR0_ID1 0x24
  99. #define ARM_SMMU_GR0_ID2 0x28
  100. #define ARM_SMMU_GR0_ID3 0x2c
  101. #define ARM_SMMU_GR0_ID4 0x30
  102. #define ARM_SMMU_GR0_ID5 0x34
  103. #define ARM_SMMU_GR0_ID6 0x38
  104. #define ARM_SMMU_GR0_ID7 0x3c
  105. #define ARM_SMMU_GR0_sGFSR 0x48
  106. #define ARM_SMMU_GR0_sGFSYNR0 0x50
  107. #define ARM_SMMU_GR0_sGFSYNR1 0x54
  108. #define ARM_SMMU_GR0_sGFSYNR2 0x58
  109. #define ARM_SMMU_GR0_PIDR0 0xfe0
  110. #define ARM_SMMU_GR0_PIDR1 0xfe4
  111. #define ARM_SMMU_GR0_PIDR2 0xfe8
  112. #define ID0_S1TS (1 << 30)
  113. #define ID0_S2TS (1 << 29)
  114. #define ID0_NTS (1 << 28)
  115. #define ID0_SMS (1 << 27)
  116. #define ID0_PTFS_SHIFT 24
  117. #define ID0_PTFS_MASK 0x2
  118. #define ID0_PTFS_V8_ONLY 0x2
  119. #define ID0_CTTW (1 << 14)
  120. #define ID0_NUMIRPT_SHIFT 16
  121. #define ID0_NUMIRPT_MASK 0xff
  122. #define ID0_NUMSMRG_SHIFT 0
  123. #define ID0_NUMSMRG_MASK 0xff
  124. #define ID1_PAGESIZE (1 << 31)
  125. #define ID1_NUMPAGENDXB_SHIFT 28
  126. #define ID1_NUMPAGENDXB_MASK 7
  127. #define ID1_NUMS2CB_SHIFT 16
  128. #define ID1_NUMS2CB_MASK 0xff
  129. #define ID1_NUMCB_SHIFT 0
  130. #define ID1_NUMCB_MASK 0xff
  131. #define ID2_OAS_SHIFT 4
  132. #define ID2_OAS_MASK 0xf
  133. #define ID2_IAS_SHIFT 0
  134. #define ID2_IAS_MASK 0xf
  135. #define ID2_UBS_SHIFT 8
  136. #define ID2_UBS_MASK 0xf
  137. #define ID2_PTFS_4K (1 << 12)
  138. #define ID2_PTFS_16K (1 << 13)
  139. #define ID2_PTFS_64K (1 << 14)
  140. #define PIDR2_ARCH_SHIFT 4
  141. #define PIDR2_ARCH_MASK 0xf
  142. /* Global TLB invalidation */
  143. #define ARM_SMMU_GR0_STLBIALL 0x60
  144. #define ARM_SMMU_GR0_TLBIVMID 0x64
  145. #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
  146. #define ARM_SMMU_GR0_TLBIALLH 0x6c
  147. #define ARM_SMMU_GR0_sTLBGSYNC 0x70
  148. #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
  149. #define sTLBGSTATUS_GSACTIVE (1 << 0)
  150. #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
  151. /* Stream mapping registers */
  152. #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
  153. #define SMR_VALID (1 << 31)
  154. #define SMR_MASK_SHIFT 16
  155. #define SMR_MASK_MASK 0x7fff
  156. #define SMR_ID_SHIFT 0
  157. #define SMR_ID_MASK 0x7fff
  158. #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
  159. #define S2CR_CBNDX_SHIFT 0
  160. #define S2CR_CBNDX_MASK 0xff
  161. #define S2CR_TYPE_SHIFT 16
  162. #define S2CR_TYPE_MASK 0x3
  163. #define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
  164. #define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
  165. #define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
  166. /* Context bank attribute registers */
  167. #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
  168. #define CBAR_VMID_SHIFT 0
  169. #define CBAR_VMID_MASK 0xff
  170. #define CBAR_S1_BPSHCFG_SHIFT 8
  171. #define CBAR_S1_BPSHCFG_MASK 3
  172. #define CBAR_S1_BPSHCFG_NSH 3
  173. #define CBAR_S1_MEMATTR_SHIFT 12
  174. #define CBAR_S1_MEMATTR_MASK 0xf
  175. #define CBAR_S1_MEMATTR_WB 0xf
  176. #define CBAR_TYPE_SHIFT 16
  177. #define CBAR_TYPE_MASK 0x3
  178. #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
  179. #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
  180. #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
  181. #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
  182. #define CBAR_IRPTNDX_SHIFT 24
  183. #define CBAR_IRPTNDX_MASK 0xff
  184. #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
  185. #define CBA2R_RW64_32BIT (0 << 0)
  186. #define CBA2R_RW64_64BIT (1 << 0)
  187. /* Translation context bank */
  188. #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
  189. #define ARM_SMMU_CB(smmu, n) ((n) * (smmu)->pagesize)
  190. #define ARM_SMMU_CB_SCTLR 0x0
  191. #define ARM_SMMU_CB_RESUME 0x8
  192. #define ARM_SMMU_CB_TTBCR2 0x10
  193. #define ARM_SMMU_CB_TTBR0_LO 0x20
  194. #define ARM_SMMU_CB_TTBR0_HI 0x24
  195. #define ARM_SMMU_CB_TTBCR 0x30
  196. #define ARM_SMMU_CB_S1_MAIR0 0x38
  197. #define ARM_SMMU_CB_FSR 0x58
  198. #define ARM_SMMU_CB_FAR_LO 0x60
  199. #define ARM_SMMU_CB_FAR_HI 0x64
  200. #define ARM_SMMU_CB_FSYNR0 0x68
  201. #define ARM_SMMU_CB_S1_TLBIASID 0x610
  202. #define SCTLR_S1_ASIDPNE (1 << 12)
  203. #define SCTLR_CFCFG (1 << 7)
  204. #define SCTLR_CFIE (1 << 6)
  205. #define SCTLR_CFRE (1 << 5)
  206. #define SCTLR_E (1 << 4)
  207. #define SCTLR_AFE (1 << 2)
  208. #define SCTLR_TRE (1 << 1)
  209. #define SCTLR_M (1 << 0)
  210. #define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
  211. #define RESUME_RETRY (0 << 0)
  212. #define RESUME_TERMINATE (1 << 0)
  213. #define TTBCR_EAE (1 << 31)
  214. #define TTBCR_PASIZE_SHIFT 16
  215. #define TTBCR_PASIZE_MASK 0x7
  216. #define TTBCR_TG0_4K (0 << 14)
  217. #define TTBCR_TG0_64K (1 << 14)
  218. #define TTBCR_SH0_SHIFT 12
  219. #define TTBCR_SH0_MASK 0x3
  220. #define TTBCR_SH_NS 0
  221. #define TTBCR_SH_OS 2
  222. #define TTBCR_SH_IS 3
  223. #define TTBCR_ORGN0_SHIFT 10
  224. #define TTBCR_IRGN0_SHIFT 8
  225. #define TTBCR_RGN_MASK 0x3
  226. #define TTBCR_RGN_NC 0
  227. #define TTBCR_RGN_WBWA 1
  228. #define TTBCR_RGN_WT 2
  229. #define TTBCR_RGN_WB 3
  230. #define TTBCR_SL0_SHIFT 6
  231. #define TTBCR_SL0_MASK 0x3
  232. #define TTBCR_SL0_LVL_2 0
  233. #define TTBCR_SL0_LVL_1 1
  234. #define TTBCR_T1SZ_SHIFT 16
  235. #define TTBCR_T0SZ_SHIFT 0
  236. #define TTBCR_SZ_MASK 0xf
  237. #define TTBCR2_SEP_SHIFT 15
  238. #define TTBCR2_SEP_MASK 0x7
  239. #define TTBCR2_PASIZE_SHIFT 0
  240. #define TTBCR2_PASIZE_MASK 0x7
  241. /* Common definitions for PASize and SEP fields */
  242. #define TTBCR2_ADDR_32 0
  243. #define TTBCR2_ADDR_36 1
  244. #define TTBCR2_ADDR_40 2
  245. #define TTBCR2_ADDR_42 3
  246. #define TTBCR2_ADDR_44 4
  247. #define TTBCR2_ADDR_48 5
  248. #define TTBRn_HI_ASID_SHIFT 16
  249. #define MAIR_ATTR_SHIFT(n) ((n) << 3)
  250. #define MAIR_ATTR_MASK 0xff
  251. #define MAIR_ATTR_DEVICE 0x04
  252. #define MAIR_ATTR_NC 0x44
  253. #define MAIR_ATTR_WBRWA 0xff
  254. #define MAIR_ATTR_IDX_NC 0
  255. #define MAIR_ATTR_IDX_CACHE 1
  256. #define MAIR_ATTR_IDX_DEV 2
  257. #define FSR_MULTI (1 << 31)
  258. #define FSR_SS (1 << 30)
  259. #define FSR_UUT (1 << 8)
  260. #define FSR_ASF (1 << 7)
  261. #define FSR_TLBLKF (1 << 6)
  262. #define FSR_TLBMCF (1 << 5)
  263. #define FSR_EF (1 << 4)
  264. #define FSR_PF (1 << 3)
  265. #define FSR_AFF (1 << 2)
  266. #define FSR_TF (1 << 1)
  267. #define FSR_IGN (FSR_AFF | FSR_ASF | FSR_TLBMCF | \
  268. FSR_TLBLKF)
  269. #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
  270. FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
  271. #define FSYNR0_WNR (1 << 4)
  272. struct arm_smmu_smr {
  273. u8 idx;
  274. u16 mask;
  275. u16 id;
  276. };
  277. struct arm_smmu_master {
  278. struct device_node *of_node;
  279. /*
  280. * The following is specific to the master's position in the
  281. * SMMU chain.
  282. */
  283. struct rb_node node;
  284. int num_streamids;
  285. u16 streamids[MAX_MASTER_STREAMIDS];
  286. /*
  287. * We only need to allocate these on the root SMMU, as we
  288. * configure unmatched streams to bypass translation.
  289. */
  290. struct arm_smmu_smr *smrs;
  291. };
  292. struct arm_smmu_device {
  293. struct device *dev;
  294. struct device_node *parent_of_node;
  295. void __iomem *base;
  296. unsigned long size;
  297. unsigned long pagesize;
  298. #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
  299. #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
  300. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
  301. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
  302. #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
  303. u32 features;
  304. int version;
  305. u32 num_context_banks;
  306. u32 num_s2_context_banks;
  307. DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
  308. atomic_t irptndx;
  309. u32 num_mapping_groups;
  310. DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
  311. unsigned long input_size;
  312. unsigned long s1_output_size;
  313. unsigned long s2_output_size;
  314. u32 num_global_irqs;
  315. u32 num_context_irqs;
  316. unsigned int *irqs;
  317. struct list_head list;
  318. struct rb_root masters;
  319. };
  320. struct arm_smmu_cfg {
  321. struct arm_smmu_device *smmu;
  322. u8 cbndx;
  323. u8 irptndx;
  324. u32 cbar;
  325. pgd_t *pgd;
  326. };
  327. #define INVALID_IRPTNDX 0xff
  328. #define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
  329. #define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
  330. struct arm_smmu_domain {
  331. /*
  332. * A domain can span across multiple, chained SMMUs and requires
  333. * all devices within the domain to follow the same translation
  334. * path.
  335. */
  336. struct arm_smmu_device *leaf_smmu;
  337. struct arm_smmu_cfg root_cfg;
  338. phys_addr_t output_mask;
  339. spinlock_t lock;
  340. };
  341. static DEFINE_SPINLOCK(arm_smmu_devices_lock);
  342. static LIST_HEAD(arm_smmu_devices);
  343. static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
  344. struct device_node *dev_node)
  345. {
  346. struct rb_node *node = smmu->masters.rb_node;
  347. while (node) {
  348. struct arm_smmu_master *master;
  349. master = container_of(node, struct arm_smmu_master, node);
  350. if (dev_node < master->of_node)
  351. node = node->rb_left;
  352. else if (dev_node > master->of_node)
  353. node = node->rb_right;
  354. else
  355. return master;
  356. }
  357. return NULL;
  358. }
  359. static int insert_smmu_master(struct arm_smmu_device *smmu,
  360. struct arm_smmu_master *master)
  361. {
  362. struct rb_node **new, *parent;
  363. new = &smmu->masters.rb_node;
  364. parent = NULL;
  365. while (*new) {
  366. struct arm_smmu_master *this;
  367. this = container_of(*new, struct arm_smmu_master, node);
  368. parent = *new;
  369. if (master->of_node < this->of_node)
  370. new = &((*new)->rb_left);
  371. else if (master->of_node > this->of_node)
  372. new = &((*new)->rb_right);
  373. else
  374. return -EEXIST;
  375. }
  376. rb_link_node(&master->node, parent, new);
  377. rb_insert_color(&master->node, &smmu->masters);
  378. return 0;
  379. }
  380. static int register_smmu_master(struct arm_smmu_device *smmu,
  381. struct device *dev,
  382. struct of_phandle_args *masterspec)
  383. {
  384. int i;
  385. struct arm_smmu_master *master;
  386. master = find_smmu_master(smmu, masterspec->np);
  387. if (master) {
  388. dev_err(dev,
  389. "rejecting multiple registrations for master device %s\n",
  390. masterspec->np->name);
  391. return -EBUSY;
  392. }
  393. if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
  394. dev_err(dev,
  395. "reached maximum number (%d) of stream IDs for master device %s\n",
  396. MAX_MASTER_STREAMIDS, masterspec->np->name);
  397. return -ENOSPC;
  398. }
  399. master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
  400. if (!master)
  401. return -ENOMEM;
  402. master->of_node = masterspec->np;
  403. master->num_streamids = masterspec->args_count;
  404. for (i = 0; i < master->num_streamids; ++i)
  405. master->streamids[i] = masterspec->args[i];
  406. return insert_smmu_master(smmu, master);
  407. }
  408. static struct arm_smmu_device *find_parent_smmu(struct arm_smmu_device *smmu)
  409. {
  410. struct arm_smmu_device *parent;
  411. if (!smmu->parent_of_node)
  412. return NULL;
  413. spin_lock(&arm_smmu_devices_lock);
  414. list_for_each_entry(parent, &arm_smmu_devices, list)
  415. if (parent->dev->of_node == smmu->parent_of_node)
  416. goto out_unlock;
  417. parent = NULL;
  418. dev_warn(smmu->dev,
  419. "Failed to find SMMU parent despite parent in DT\n");
  420. out_unlock:
  421. spin_unlock(&arm_smmu_devices_lock);
  422. return parent;
  423. }
  424. static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
  425. {
  426. int idx;
  427. do {
  428. idx = find_next_zero_bit(map, end, start);
  429. if (idx == end)
  430. return -ENOSPC;
  431. } while (test_and_set_bit(idx, map));
  432. return idx;
  433. }
  434. static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
  435. {
  436. clear_bit(idx, map);
  437. }
  438. /* Wait for any pending TLB invalidations to complete */
  439. static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
  440. {
  441. int count = 0;
  442. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  443. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
  444. while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
  445. & sTLBGSTATUS_GSACTIVE) {
  446. cpu_relax();
  447. if (++count == TLB_LOOP_TIMEOUT) {
  448. dev_err_ratelimited(smmu->dev,
  449. "TLB sync timed out -- SMMU may be deadlocked\n");
  450. return;
  451. }
  452. udelay(1);
  453. }
  454. }
  455. static void arm_smmu_tlb_inv_context(struct arm_smmu_cfg *cfg)
  456. {
  457. struct arm_smmu_device *smmu = cfg->smmu;
  458. void __iomem *base = ARM_SMMU_GR0(smmu);
  459. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  460. if (stage1) {
  461. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  462. writel_relaxed(ARM_SMMU_CB_ASID(cfg),
  463. base + ARM_SMMU_CB_S1_TLBIASID);
  464. } else {
  465. base = ARM_SMMU_GR0(smmu);
  466. writel_relaxed(ARM_SMMU_CB_VMID(cfg),
  467. base + ARM_SMMU_GR0_TLBIVMID);
  468. }
  469. arm_smmu_tlb_sync(smmu);
  470. }
  471. static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  472. {
  473. int flags, ret;
  474. u32 fsr, far, fsynr, resume;
  475. unsigned long iova;
  476. struct iommu_domain *domain = dev;
  477. struct arm_smmu_domain *smmu_domain = domain->priv;
  478. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  479. struct arm_smmu_device *smmu = root_cfg->smmu;
  480. void __iomem *cb_base;
  481. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
  482. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  483. if (!(fsr & FSR_FAULT))
  484. return IRQ_NONE;
  485. if (fsr & FSR_IGN)
  486. dev_err_ratelimited(smmu->dev,
  487. "Unexpected context fault (fsr 0x%u)\n",
  488. fsr);
  489. fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
  490. flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  491. far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
  492. iova = far;
  493. #ifdef CONFIG_64BIT
  494. far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
  495. iova |= ((unsigned long)far << 32);
  496. #endif
  497. if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
  498. ret = IRQ_HANDLED;
  499. resume = RESUME_RETRY;
  500. } else {
  501. dev_err_ratelimited(smmu->dev,
  502. "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
  503. iova, fsynr, root_cfg->cbndx);
  504. ret = IRQ_NONE;
  505. resume = RESUME_TERMINATE;
  506. }
  507. /* Clear the faulting FSR */
  508. writel(fsr, cb_base + ARM_SMMU_CB_FSR);
  509. /* Retry or terminate any stalled transactions */
  510. if (fsr & FSR_SS)
  511. writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
  512. return ret;
  513. }
  514. static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
  515. {
  516. u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
  517. struct arm_smmu_device *smmu = dev;
  518. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  519. gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
  520. if (!gfsr)
  521. return IRQ_NONE;
  522. gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
  523. gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
  524. gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
  525. dev_err_ratelimited(smmu->dev,
  526. "Unexpected global fault, this could be serious\n");
  527. dev_err_ratelimited(smmu->dev,
  528. "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
  529. gfsr, gfsynr0, gfsynr1, gfsynr2);
  530. writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
  531. return IRQ_HANDLED;
  532. }
  533. static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
  534. size_t size)
  535. {
  536. unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
  537. /* Ensure new page tables are visible to the hardware walker */
  538. if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
  539. dsb();
  540. } else {
  541. /*
  542. * If the SMMU can't walk tables in the CPU caches, treat them
  543. * like non-coherent DMA since we need to flush the new entries
  544. * all the way out to memory. There's no possibility of
  545. * recursion here as the SMMU table walker will not be wired
  546. * through another SMMU.
  547. */
  548. dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
  549. DMA_TO_DEVICE);
  550. }
  551. }
  552. static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
  553. {
  554. u32 reg;
  555. bool stage1;
  556. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  557. struct arm_smmu_device *smmu = root_cfg->smmu;
  558. void __iomem *cb_base, *gr0_base, *gr1_base;
  559. gr0_base = ARM_SMMU_GR0(smmu);
  560. gr1_base = ARM_SMMU_GR1(smmu);
  561. stage1 = root_cfg->cbar != CBAR_TYPE_S2_TRANS;
  562. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
  563. /* CBAR */
  564. reg = root_cfg->cbar;
  565. if (smmu->version == 1)
  566. reg |= root_cfg->irptndx << CBAR_IRPTNDX_SHIFT;
  567. /*
  568. * Use the weakest shareability/memory types, so they are
  569. * overridden by the ttbcr/pte.
  570. */
  571. if (stage1) {
  572. reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
  573. (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
  574. } else {
  575. reg |= ARM_SMMU_CB_VMID(root_cfg) << CBAR_VMID_SHIFT;
  576. }
  577. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(root_cfg->cbndx));
  578. if (smmu->version > 1) {
  579. /* CBA2R */
  580. #ifdef CONFIG_64BIT
  581. reg = CBA2R_RW64_64BIT;
  582. #else
  583. reg = CBA2R_RW64_32BIT;
  584. #endif
  585. writel_relaxed(reg,
  586. gr1_base + ARM_SMMU_GR1_CBA2R(root_cfg->cbndx));
  587. /* TTBCR2 */
  588. switch (smmu->input_size) {
  589. case 32:
  590. reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
  591. break;
  592. case 36:
  593. reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
  594. break;
  595. case 39:
  596. reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
  597. break;
  598. case 42:
  599. reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
  600. break;
  601. case 44:
  602. reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
  603. break;
  604. case 48:
  605. reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
  606. break;
  607. }
  608. switch (smmu->s1_output_size) {
  609. case 32:
  610. reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
  611. break;
  612. case 36:
  613. reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
  614. break;
  615. case 39:
  616. reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
  617. break;
  618. case 42:
  619. reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
  620. break;
  621. case 44:
  622. reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
  623. break;
  624. case 48:
  625. reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
  626. break;
  627. }
  628. if (stage1)
  629. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
  630. }
  631. /* TTBR0 */
  632. arm_smmu_flush_pgtable(smmu, root_cfg->pgd,
  633. PTRS_PER_PGD * sizeof(pgd_t));
  634. reg = __pa(root_cfg->pgd);
  635. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
  636. reg = (phys_addr_t)__pa(root_cfg->pgd) >> 32;
  637. if (stage1)
  638. reg |= ARM_SMMU_CB_ASID(root_cfg) << TTBRn_HI_ASID_SHIFT;
  639. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
  640. /*
  641. * TTBCR
  642. * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
  643. */
  644. if (smmu->version > 1) {
  645. if (PAGE_SIZE == SZ_4K)
  646. reg = TTBCR_TG0_4K;
  647. else
  648. reg = TTBCR_TG0_64K;
  649. if (!stage1) {
  650. switch (smmu->s2_output_size) {
  651. case 32:
  652. reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
  653. break;
  654. case 36:
  655. reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
  656. break;
  657. case 40:
  658. reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
  659. break;
  660. case 42:
  661. reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
  662. break;
  663. case 44:
  664. reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
  665. break;
  666. case 48:
  667. reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
  668. break;
  669. }
  670. } else {
  671. reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
  672. }
  673. } else {
  674. reg = 0;
  675. }
  676. reg |= TTBCR_EAE |
  677. (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
  678. (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
  679. (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT) |
  680. (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
  681. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
  682. /* MAIR0 (stage-1 only) */
  683. if (stage1) {
  684. reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
  685. (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
  686. (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
  687. writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
  688. }
  689. /* SCTLR */
  690. reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
  691. if (stage1)
  692. reg |= SCTLR_S1_ASIDPNE;
  693. #ifdef __BIG_ENDIAN
  694. reg |= SCTLR_E;
  695. #endif
  696. writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
  697. }
  698. static int arm_smmu_init_domain_context(struct iommu_domain *domain,
  699. struct device *dev)
  700. {
  701. int irq, ret, start;
  702. struct arm_smmu_domain *smmu_domain = domain->priv;
  703. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  704. struct arm_smmu_device *smmu, *parent;
  705. /*
  706. * Walk the SMMU chain to find the root device for this chain.
  707. * We assume that no masters have translations which terminate
  708. * early, and therefore check that the root SMMU does indeed have
  709. * a StreamID for the master in question.
  710. */
  711. parent = dev->archdata.iommu;
  712. smmu_domain->output_mask = -1;
  713. do {
  714. smmu = parent;
  715. smmu_domain->output_mask &= (1ULL << smmu->s2_output_size) - 1;
  716. } while ((parent = find_parent_smmu(smmu)));
  717. if (!find_smmu_master(smmu, dev->of_node)) {
  718. dev_err(dev, "unable to find root SMMU for device\n");
  719. return -ENODEV;
  720. }
  721. if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
  722. /*
  723. * We will likely want to change this if/when KVM gets
  724. * involved.
  725. */
  726. root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
  727. start = smmu->num_s2_context_banks;
  728. } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) {
  729. root_cfg->cbar = CBAR_TYPE_S2_TRANS;
  730. start = 0;
  731. } else {
  732. root_cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
  733. start = smmu->num_s2_context_banks;
  734. }
  735. ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
  736. smmu->num_context_banks);
  737. if (IS_ERR_VALUE(ret))
  738. return ret;
  739. root_cfg->cbndx = ret;
  740. if (smmu->version == 1) {
  741. root_cfg->irptndx = atomic_inc_return(&smmu->irptndx);
  742. root_cfg->irptndx %= smmu->num_context_irqs;
  743. } else {
  744. root_cfg->irptndx = root_cfg->cbndx;
  745. }
  746. irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
  747. ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
  748. "arm-smmu-context-fault", domain);
  749. if (IS_ERR_VALUE(ret)) {
  750. dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
  751. root_cfg->irptndx, irq);
  752. root_cfg->irptndx = INVALID_IRPTNDX;
  753. goto out_free_context;
  754. }
  755. root_cfg->smmu = smmu;
  756. arm_smmu_init_context_bank(smmu_domain);
  757. return ret;
  758. out_free_context:
  759. __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
  760. return ret;
  761. }
  762. static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
  763. {
  764. struct arm_smmu_domain *smmu_domain = domain->priv;
  765. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  766. struct arm_smmu_device *smmu = root_cfg->smmu;
  767. void __iomem *cb_base;
  768. int irq;
  769. if (!smmu)
  770. return;
  771. /* Disable the context bank and nuke the TLB before freeing it. */
  772. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, root_cfg->cbndx);
  773. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  774. arm_smmu_tlb_inv_context(root_cfg);
  775. if (root_cfg->irptndx != INVALID_IRPTNDX) {
  776. irq = smmu->irqs[smmu->num_global_irqs + root_cfg->irptndx];
  777. free_irq(irq, domain);
  778. }
  779. __arm_smmu_free_bitmap(smmu->context_map, root_cfg->cbndx);
  780. }
  781. static int arm_smmu_domain_init(struct iommu_domain *domain)
  782. {
  783. struct arm_smmu_domain *smmu_domain;
  784. pgd_t *pgd;
  785. /*
  786. * Allocate the domain and initialise some of its data structures.
  787. * We can't really do anything meaningful until we've added a
  788. * master.
  789. */
  790. smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
  791. if (!smmu_domain)
  792. return -ENOMEM;
  793. pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  794. if (!pgd)
  795. goto out_free_domain;
  796. smmu_domain->root_cfg.pgd = pgd;
  797. spin_lock_init(&smmu_domain->lock);
  798. domain->priv = smmu_domain;
  799. return 0;
  800. out_free_domain:
  801. kfree(smmu_domain);
  802. return -ENOMEM;
  803. }
  804. static void arm_smmu_free_ptes(pmd_t *pmd)
  805. {
  806. pgtable_t table = pmd_pgtable(*pmd);
  807. pgtable_page_dtor(table);
  808. __free_page(table);
  809. }
  810. static void arm_smmu_free_pmds(pud_t *pud)
  811. {
  812. int i;
  813. pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
  814. pmd = pmd_base;
  815. for (i = 0; i < PTRS_PER_PMD; ++i) {
  816. if (pmd_none(*pmd))
  817. continue;
  818. arm_smmu_free_ptes(pmd);
  819. pmd++;
  820. }
  821. pmd_free(NULL, pmd_base);
  822. }
  823. static void arm_smmu_free_puds(pgd_t *pgd)
  824. {
  825. int i;
  826. pud_t *pud, *pud_base = pud_offset(pgd, 0);
  827. pud = pud_base;
  828. for (i = 0; i < PTRS_PER_PUD; ++i) {
  829. if (pud_none(*pud))
  830. continue;
  831. arm_smmu_free_pmds(pud);
  832. pud++;
  833. }
  834. pud_free(NULL, pud_base);
  835. }
  836. static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
  837. {
  838. int i;
  839. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  840. pgd_t *pgd, *pgd_base = root_cfg->pgd;
  841. /*
  842. * Recursively free the page tables for this domain. We don't
  843. * care about speculative TLB filling, because the TLB will be
  844. * nuked next time this context bank is re-allocated and no devices
  845. * currently map to these tables.
  846. */
  847. pgd = pgd_base;
  848. for (i = 0; i < PTRS_PER_PGD; ++i) {
  849. if (pgd_none(*pgd))
  850. continue;
  851. arm_smmu_free_puds(pgd);
  852. pgd++;
  853. }
  854. kfree(pgd_base);
  855. }
  856. static void arm_smmu_domain_destroy(struct iommu_domain *domain)
  857. {
  858. struct arm_smmu_domain *smmu_domain = domain->priv;
  859. /*
  860. * Free the domain resources. We assume that all devices have
  861. * already been detached.
  862. */
  863. arm_smmu_destroy_domain_context(domain);
  864. arm_smmu_free_pgtables(smmu_domain);
  865. kfree(smmu_domain);
  866. }
  867. static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
  868. struct arm_smmu_master *master)
  869. {
  870. int i;
  871. struct arm_smmu_smr *smrs;
  872. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  873. if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
  874. return 0;
  875. if (master->smrs)
  876. return -EEXIST;
  877. smrs = kmalloc(sizeof(*smrs) * master->num_streamids, GFP_KERNEL);
  878. if (!smrs) {
  879. dev_err(smmu->dev, "failed to allocate %d SMRs for master %s\n",
  880. master->num_streamids, master->of_node->name);
  881. return -ENOMEM;
  882. }
  883. /* Allocate the SMRs on the root SMMU */
  884. for (i = 0; i < master->num_streamids; ++i) {
  885. int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
  886. smmu->num_mapping_groups);
  887. if (IS_ERR_VALUE(idx)) {
  888. dev_err(smmu->dev, "failed to allocate free SMR\n");
  889. goto err_free_smrs;
  890. }
  891. smrs[i] = (struct arm_smmu_smr) {
  892. .idx = idx,
  893. .mask = 0, /* We don't currently share SMRs */
  894. .id = master->streamids[i],
  895. };
  896. }
  897. /* It worked! Now, poke the actual hardware */
  898. for (i = 0; i < master->num_streamids; ++i) {
  899. u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
  900. smrs[i].mask << SMR_MASK_SHIFT;
  901. writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
  902. }
  903. master->smrs = smrs;
  904. return 0;
  905. err_free_smrs:
  906. while (--i >= 0)
  907. __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
  908. kfree(smrs);
  909. return -ENOSPC;
  910. }
  911. static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
  912. struct arm_smmu_master *master)
  913. {
  914. int i;
  915. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  916. struct arm_smmu_smr *smrs = master->smrs;
  917. /* Invalidate the SMRs before freeing back to the allocator */
  918. for (i = 0; i < master->num_streamids; ++i) {
  919. u8 idx = smrs[i].idx;
  920. writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
  921. __arm_smmu_free_bitmap(smmu->smr_map, idx);
  922. }
  923. master->smrs = NULL;
  924. kfree(smrs);
  925. }
  926. static void arm_smmu_bypass_stream_mapping(struct arm_smmu_device *smmu,
  927. struct arm_smmu_master *master)
  928. {
  929. int i;
  930. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  931. for (i = 0; i < master->num_streamids; ++i) {
  932. u16 sid = master->streamids[i];
  933. writel_relaxed(S2CR_TYPE_BYPASS,
  934. gr0_base + ARM_SMMU_GR0_S2CR(sid));
  935. }
  936. }
  937. static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
  938. struct arm_smmu_master *master)
  939. {
  940. int i, ret;
  941. struct arm_smmu_device *parent, *smmu = smmu_domain->root_cfg.smmu;
  942. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  943. ret = arm_smmu_master_configure_smrs(smmu, master);
  944. if (ret)
  945. return ret;
  946. /* Bypass the leaves */
  947. smmu = smmu_domain->leaf_smmu;
  948. while ((parent = find_parent_smmu(smmu))) {
  949. /*
  950. * We won't have a StreamID match for anything but the root
  951. * smmu, so we only need to worry about StreamID indexing,
  952. * where we must install bypass entries in the S2CRs.
  953. */
  954. if (smmu->features & ARM_SMMU_FEAT_STREAM_MATCH)
  955. continue;
  956. arm_smmu_bypass_stream_mapping(smmu, master);
  957. smmu = parent;
  958. }
  959. /* Now we're at the root, time to point at our context bank */
  960. for (i = 0; i < master->num_streamids; ++i) {
  961. u32 idx, s2cr;
  962. idx = master->smrs ? master->smrs[i].idx : master->streamids[i];
  963. s2cr = (S2CR_TYPE_TRANS << S2CR_TYPE_SHIFT) |
  964. (smmu_domain->root_cfg.cbndx << S2CR_CBNDX_SHIFT);
  965. writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
  966. }
  967. return 0;
  968. }
  969. static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
  970. struct arm_smmu_master *master)
  971. {
  972. struct arm_smmu_device *smmu = smmu_domain->root_cfg.smmu;
  973. /*
  974. * We *must* clear the S2CR first, because freeing the SMR means
  975. * that it can be re-allocated immediately.
  976. */
  977. arm_smmu_bypass_stream_mapping(smmu, master);
  978. arm_smmu_master_free_smrs(smmu, master);
  979. }
  980. static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  981. {
  982. int ret = -EINVAL;
  983. struct arm_smmu_domain *smmu_domain = domain->priv;
  984. struct arm_smmu_device *device_smmu = dev->archdata.iommu;
  985. struct arm_smmu_master *master;
  986. unsigned long flags;
  987. if (!device_smmu) {
  988. dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
  989. return -ENXIO;
  990. }
  991. /*
  992. * Sanity check the domain. We don't currently support domains
  993. * that cross between different SMMU chains.
  994. */
  995. spin_lock_irqsave(&smmu_domain->lock, flags);
  996. if (!smmu_domain->leaf_smmu) {
  997. /* Now that we have a master, we can finalise the domain */
  998. ret = arm_smmu_init_domain_context(domain, dev);
  999. if (IS_ERR_VALUE(ret))
  1000. goto err_unlock;
  1001. smmu_domain->leaf_smmu = device_smmu;
  1002. } else if (smmu_domain->leaf_smmu != device_smmu) {
  1003. dev_err(dev,
  1004. "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
  1005. dev_name(smmu_domain->leaf_smmu->dev),
  1006. dev_name(device_smmu->dev));
  1007. goto err_unlock;
  1008. }
  1009. spin_unlock_irqrestore(&smmu_domain->lock, flags);
  1010. /* Looks ok, so add the device to the domain */
  1011. master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node);
  1012. if (!master)
  1013. return -ENODEV;
  1014. return arm_smmu_domain_add_master(smmu_domain, master);
  1015. err_unlock:
  1016. spin_unlock_irqrestore(&smmu_domain->lock, flags);
  1017. return ret;
  1018. }
  1019. static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
  1020. {
  1021. struct arm_smmu_domain *smmu_domain = domain->priv;
  1022. struct arm_smmu_master *master;
  1023. master = find_smmu_master(smmu_domain->leaf_smmu, dev->of_node);
  1024. if (master)
  1025. arm_smmu_domain_remove_master(smmu_domain, master);
  1026. }
  1027. static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
  1028. unsigned long end)
  1029. {
  1030. return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
  1031. (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
  1032. }
  1033. static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
  1034. unsigned long addr, unsigned long end,
  1035. unsigned long pfn, int flags, int stage)
  1036. {
  1037. pte_t *pte, *start;
  1038. pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
  1039. if (pmd_none(*pmd)) {
  1040. /* Allocate a new set of tables */
  1041. pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
  1042. if (!table)
  1043. return -ENOMEM;
  1044. arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
  1045. if (!pgtable_page_ctor(table)) {
  1046. __free_page(table);
  1047. return -ENOMEM;
  1048. }
  1049. pmd_populate(NULL, pmd, table);
  1050. arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
  1051. }
  1052. if (stage == 1) {
  1053. pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
  1054. if (!(flags & IOMMU_WRITE) && (flags & IOMMU_READ))
  1055. pteval |= ARM_SMMU_PTE_AP_RDONLY;
  1056. if (flags & IOMMU_CACHE)
  1057. pteval |= (MAIR_ATTR_IDX_CACHE <<
  1058. ARM_SMMU_PTE_ATTRINDX_SHIFT);
  1059. } else {
  1060. pteval |= ARM_SMMU_PTE_HAP_FAULT;
  1061. if (flags & IOMMU_READ)
  1062. pteval |= ARM_SMMU_PTE_HAP_READ;
  1063. if (flags & IOMMU_WRITE)
  1064. pteval |= ARM_SMMU_PTE_HAP_WRITE;
  1065. if (flags & IOMMU_CACHE)
  1066. pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
  1067. else
  1068. pteval |= ARM_SMMU_PTE_MEMATTR_NC;
  1069. }
  1070. /* If no access, create a faulting entry to avoid TLB fills */
  1071. if (flags & IOMMU_EXEC)
  1072. pteval &= ~ARM_SMMU_PTE_XN;
  1073. else if (!(flags & (IOMMU_READ | IOMMU_WRITE)))
  1074. pteval &= ~ARM_SMMU_PTE_PAGE;
  1075. pteval |= ARM_SMMU_PTE_SH_IS;
  1076. start = pmd_page_vaddr(*pmd) + pte_index(addr);
  1077. pte = start;
  1078. /*
  1079. * Install the page table entries. This is fairly complicated
  1080. * since we attempt to make use of the contiguous hint in the
  1081. * ptes where possible. The contiguous hint indicates a series
  1082. * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
  1083. * contiguous region with the following constraints:
  1084. *
  1085. * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
  1086. * - Each pte in the region has the contiguous hint bit set
  1087. *
  1088. * This complicates unmapping (also handled by this code, when
  1089. * neither IOMMU_READ or IOMMU_WRITE are set) because it is
  1090. * possible, yet highly unlikely, that a client may unmap only
  1091. * part of a contiguous range. This requires clearing of the
  1092. * contiguous hint bits in the range before installing the new
  1093. * faulting entries.
  1094. *
  1095. * Note that re-mapping an address range without first unmapping
  1096. * it is not supported, so TLB invalidation is not required here
  1097. * and is instead performed at unmap and domain-init time.
  1098. */
  1099. do {
  1100. int i = 1;
  1101. pteval &= ~ARM_SMMU_PTE_CONT;
  1102. if (arm_smmu_pte_is_contiguous_range(addr, end)) {
  1103. i = ARM_SMMU_PTE_CONT_ENTRIES;
  1104. pteval |= ARM_SMMU_PTE_CONT;
  1105. } else if (pte_val(*pte) &
  1106. (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
  1107. int j;
  1108. pte_t *cont_start;
  1109. unsigned long idx = pte_index(addr);
  1110. idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
  1111. cont_start = pmd_page_vaddr(*pmd) + idx;
  1112. for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
  1113. pte_val(*(cont_start + j)) &= ~ARM_SMMU_PTE_CONT;
  1114. arm_smmu_flush_pgtable(smmu, cont_start,
  1115. sizeof(*pte) *
  1116. ARM_SMMU_PTE_CONT_ENTRIES);
  1117. }
  1118. do {
  1119. *pte = pfn_pte(pfn, __pgprot(pteval));
  1120. } while (pte++, pfn++, addr += PAGE_SIZE, --i);
  1121. } while (addr != end);
  1122. arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
  1123. return 0;
  1124. }
  1125. static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
  1126. unsigned long addr, unsigned long end,
  1127. phys_addr_t phys, int flags, int stage)
  1128. {
  1129. int ret;
  1130. pmd_t *pmd;
  1131. unsigned long next, pfn = __phys_to_pfn(phys);
  1132. #ifndef __PAGETABLE_PMD_FOLDED
  1133. if (pud_none(*pud)) {
  1134. pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
  1135. if (!pmd)
  1136. return -ENOMEM;
  1137. arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
  1138. pud_populate(NULL, pud, pmd);
  1139. arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
  1140. pmd += pmd_index(addr);
  1141. } else
  1142. #endif
  1143. pmd = pmd_offset(pud, addr);
  1144. do {
  1145. next = pmd_addr_end(addr, end);
  1146. ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, end, pfn,
  1147. flags, stage);
  1148. phys += next - addr;
  1149. } while (pmd++, addr = next, addr < end);
  1150. return ret;
  1151. }
  1152. static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
  1153. unsigned long addr, unsigned long end,
  1154. phys_addr_t phys, int flags, int stage)
  1155. {
  1156. int ret = 0;
  1157. pud_t *pud;
  1158. unsigned long next;
  1159. #ifndef __PAGETABLE_PUD_FOLDED
  1160. if (pgd_none(*pgd)) {
  1161. pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
  1162. if (!pud)
  1163. return -ENOMEM;
  1164. arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
  1165. pgd_populate(NULL, pgd, pud);
  1166. arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
  1167. pud += pud_index(addr);
  1168. } else
  1169. #endif
  1170. pud = pud_offset(pgd, addr);
  1171. do {
  1172. next = pud_addr_end(addr, end);
  1173. ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
  1174. flags, stage);
  1175. phys += next - addr;
  1176. } while (pud++, addr = next, addr < end);
  1177. return ret;
  1178. }
  1179. static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
  1180. unsigned long iova, phys_addr_t paddr,
  1181. size_t size, int flags)
  1182. {
  1183. int ret, stage;
  1184. unsigned long end;
  1185. phys_addr_t input_mask, output_mask;
  1186. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  1187. pgd_t *pgd = root_cfg->pgd;
  1188. struct arm_smmu_device *smmu = root_cfg->smmu;
  1189. unsigned long irqflags;
  1190. if (root_cfg->cbar == CBAR_TYPE_S2_TRANS) {
  1191. stage = 2;
  1192. output_mask = (1ULL << smmu->s2_output_size) - 1;
  1193. } else {
  1194. stage = 1;
  1195. output_mask = (1ULL << smmu->s1_output_size) - 1;
  1196. }
  1197. if (!pgd)
  1198. return -EINVAL;
  1199. if (size & ~PAGE_MASK)
  1200. return -EINVAL;
  1201. input_mask = (1ULL << smmu->input_size) - 1;
  1202. if ((phys_addr_t)iova & ~input_mask)
  1203. return -ERANGE;
  1204. if (paddr & ~output_mask)
  1205. return -ERANGE;
  1206. spin_lock_irqsave(&smmu_domain->lock, irqflags);
  1207. pgd += pgd_index(iova);
  1208. end = iova + size;
  1209. do {
  1210. unsigned long next = pgd_addr_end(iova, end);
  1211. ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
  1212. flags, stage);
  1213. if (ret)
  1214. goto out_unlock;
  1215. paddr += next - iova;
  1216. iova = next;
  1217. } while (pgd++, iova != end);
  1218. out_unlock:
  1219. spin_unlock_irqrestore(&smmu_domain->lock, irqflags);
  1220. return ret;
  1221. }
  1222. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  1223. phys_addr_t paddr, size_t size, int flags)
  1224. {
  1225. struct arm_smmu_domain *smmu_domain = domain->priv;
  1226. if (!smmu_domain)
  1227. return -ENODEV;
  1228. /* Check for silent address truncation up the SMMU chain. */
  1229. if ((phys_addr_t)iova & ~smmu_domain->output_mask)
  1230. return -ERANGE;
  1231. return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, flags);
  1232. }
  1233. static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  1234. size_t size)
  1235. {
  1236. int ret;
  1237. struct arm_smmu_domain *smmu_domain = domain->priv;
  1238. ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
  1239. arm_smmu_tlb_inv_context(&smmu_domain->root_cfg);
  1240. return ret ? ret : size;
  1241. }
  1242. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  1243. dma_addr_t iova)
  1244. {
  1245. pgd_t *pgdp, pgd;
  1246. pud_t pud;
  1247. pmd_t pmd;
  1248. pte_t pte;
  1249. struct arm_smmu_domain *smmu_domain = domain->priv;
  1250. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  1251. pgdp = root_cfg->pgd;
  1252. if (!pgdp)
  1253. return 0;
  1254. pgd = *(pgdp + pgd_index(iova));
  1255. if (pgd_none(pgd))
  1256. return 0;
  1257. pud = *pud_offset(&pgd, iova);
  1258. if (pud_none(pud))
  1259. return 0;
  1260. pmd = *pmd_offset(&pud, iova);
  1261. if (pmd_none(pmd))
  1262. return 0;
  1263. pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
  1264. if (pte_none(pte))
  1265. return 0;
  1266. return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
  1267. }
  1268. static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
  1269. unsigned long cap)
  1270. {
  1271. unsigned long caps = 0;
  1272. struct arm_smmu_domain *smmu_domain = domain->priv;
  1273. if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
  1274. caps |= IOMMU_CAP_CACHE_COHERENCY;
  1275. return !!(cap & caps);
  1276. }
  1277. static int arm_smmu_add_device(struct device *dev)
  1278. {
  1279. struct arm_smmu_device *child, *parent, *smmu;
  1280. struct arm_smmu_master *master = NULL;
  1281. struct iommu_group *group;
  1282. int ret;
  1283. if (dev->archdata.iommu) {
  1284. dev_warn(dev, "IOMMU driver already assigned to device\n");
  1285. return -EINVAL;
  1286. }
  1287. spin_lock(&arm_smmu_devices_lock);
  1288. list_for_each_entry(parent, &arm_smmu_devices, list) {
  1289. smmu = parent;
  1290. /* Try to find a child of the current SMMU. */
  1291. list_for_each_entry(child, &arm_smmu_devices, list) {
  1292. if (child->parent_of_node == parent->dev->of_node) {
  1293. /* Does the child sit above our master? */
  1294. master = find_smmu_master(child, dev->of_node);
  1295. if (master) {
  1296. smmu = NULL;
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. /* We found some children, so keep searching. */
  1302. if (!smmu) {
  1303. master = NULL;
  1304. continue;
  1305. }
  1306. master = find_smmu_master(smmu, dev->of_node);
  1307. if (master)
  1308. break;
  1309. }
  1310. spin_unlock(&arm_smmu_devices_lock);
  1311. if (!master)
  1312. return -ENODEV;
  1313. group = iommu_group_alloc();
  1314. if (IS_ERR(group)) {
  1315. dev_err(dev, "Failed to allocate IOMMU group\n");
  1316. return PTR_ERR(group);
  1317. }
  1318. ret = iommu_group_add_device(group, dev);
  1319. iommu_group_put(group);
  1320. dev->archdata.iommu = smmu;
  1321. return ret;
  1322. }
  1323. static void arm_smmu_remove_device(struct device *dev)
  1324. {
  1325. dev->archdata.iommu = NULL;
  1326. iommu_group_remove_device(dev);
  1327. }
  1328. static struct iommu_ops arm_smmu_ops = {
  1329. .domain_init = arm_smmu_domain_init,
  1330. .domain_destroy = arm_smmu_domain_destroy,
  1331. .attach_dev = arm_smmu_attach_dev,
  1332. .detach_dev = arm_smmu_detach_dev,
  1333. .map = arm_smmu_map,
  1334. .unmap = arm_smmu_unmap,
  1335. .iova_to_phys = arm_smmu_iova_to_phys,
  1336. .domain_has_cap = arm_smmu_domain_has_cap,
  1337. .add_device = arm_smmu_add_device,
  1338. .remove_device = arm_smmu_remove_device,
  1339. .pgsize_bitmap = (SECTION_SIZE |
  1340. ARM_SMMU_PTE_CONT_SIZE |
  1341. PAGE_SIZE),
  1342. };
  1343. static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
  1344. {
  1345. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1346. void __iomem *cb_base;
  1347. int i = 0;
  1348. u32 reg;
  1349. /* Clear Global FSR */
  1350. reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
  1351. writel(reg, gr0_base + ARM_SMMU_GR0_sGFSR);
  1352. /* Mark all SMRn as invalid and all S2CRn as bypass */
  1353. for (i = 0; i < smmu->num_mapping_groups; ++i) {
  1354. writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(i));
  1355. writel_relaxed(S2CR_TYPE_BYPASS, gr0_base + ARM_SMMU_GR0_S2CR(i));
  1356. }
  1357. /* Make sure all context banks are disabled and clear CB_FSR */
  1358. for (i = 0; i < smmu->num_context_banks; ++i) {
  1359. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
  1360. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  1361. writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
  1362. }
  1363. /* Invalidate the TLB, just in case */
  1364. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
  1365. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
  1366. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
  1367. reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sCR0);
  1368. /* Enable fault reporting */
  1369. reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
  1370. /* Disable TLB broadcasting. */
  1371. reg |= (sCR0_VMIDPNE | sCR0_PTM);
  1372. /* Enable client access, but bypass when no mapping is found */
  1373. reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
  1374. /* Disable forced broadcasting */
  1375. reg &= ~sCR0_FB;
  1376. /* Don't upgrade barriers */
  1377. reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
  1378. /* Push the button */
  1379. arm_smmu_tlb_sync(smmu);
  1380. writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sCR0);
  1381. }
  1382. static int arm_smmu_id_size_to_bits(int size)
  1383. {
  1384. switch (size) {
  1385. case 0:
  1386. return 32;
  1387. case 1:
  1388. return 36;
  1389. case 2:
  1390. return 40;
  1391. case 3:
  1392. return 42;
  1393. case 4:
  1394. return 44;
  1395. case 5:
  1396. default:
  1397. return 48;
  1398. }
  1399. }
  1400. static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
  1401. {
  1402. unsigned long size;
  1403. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1404. u32 id;
  1405. dev_notice(smmu->dev, "probing hardware configuration...\n");
  1406. /* Primecell ID */
  1407. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
  1408. smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
  1409. dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
  1410. /* ID0 */
  1411. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
  1412. #ifndef CONFIG_64BIT
  1413. if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
  1414. dev_err(smmu->dev, "\tno v7 descriptor support!\n");
  1415. return -ENODEV;
  1416. }
  1417. #endif
  1418. if (id & ID0_S1TS) {
  1419. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  1420. dev_notice(smmu->dev, "\tstage 1 translation\n");
  1421. }
  1422. if (id & ID0_S2TS) {
  1423. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  1424. dev_notice(smmu->dev, "\tstage 2 translation\n");
  1425. }
  1426. if (id & ID0_NTS) {
  1427. smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
  1428. dev_notice(smmu->dev, "\tnested translation\n");
  1429. }
  1430. if (!(smmu->features &
  1431. (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 |
  1432. ARM_SMMU_FEAT_TRANS_NESTED))) {
  1433. dev_err(smmu->dev, "\tno translation support!\n");
  1434. return -ENODEV;
  1435. }
  1436. if (id & ID0_CTTW) {
  1437. smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
  1438. dev_notice(smmu->dev, "\tcoherent table walk\n");
  1439. }
  1440. if (id & ID0_SMS) {
  1441. u32 smr, sid, mask;
  1442. smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
  1443. smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
  1444. ID0_NUMSMRG_MASK;
  1445. if (smmu->num_mapping_groups == 0) {
  1446. dev_err(smmu->dev,
  1447. "stream-matching supported, but no SMRs present!\n");
  1448. return -ENODEV;
  1449. }
  1450. smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
  1451. smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
  1452. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
  1453. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
  1454. mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
  1455. sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
  1456. if ((mask & sid) != sid) {
  1457. dev_err(smmu->dev,
  1458. "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
  1459. mask, sid);
  1460. return -ENODEV;
  1461. }
  1462. dev_notice(smmu->dev,
  1463. "\tstream matching with %u register groups, mask 0x%x",
  1464. smmu->num_mapping_groups, mask);
  1465. }
  1466. /* ID1 */
  1467. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
  1468. smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
  1469. /* Check for size mismatch of SMMU address space from mapped region */
  1470. size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
  1471. size *= (smmu->pagesize << 1);
  1472. if (smmu->size != size)
  1473. dev_warn(smmu->dev, "SMMU address space size (0x%lx) differs "
  1474. "from mapped region size (0x%lx)!\n", size, smmu->size);
  1475. smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
  1476. ID1_NUMS2CB_MASK;
  1477. smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
  1478. if (smmu->num_s2_context_banks > smmu->num_context_banks) {
  1479. dev_err(smmu->dev, "impossible number of S2 context banks!\n");
  1480. return -ENODEV;
  1481. }
  1482. dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
  1483. smmu->num_context_banks, smmu->num_s2_context_banks);
  1484. /* ID2 */
  1485. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
  1486. size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
  1487. /*
  1488. * Stage-1 output limited by stage-2 input size due to pgd
  1489. * allocation (PTRS_PER_PGD).
  1490. */
  1491. #ifdef CONFIG_64BIT
  1492. smmu->s1_output_size = min(39UL, size);
  1493. #else
  1494. smmu->s1_output_size = min(32UL, size);
  1495. #endif
  1496. /* The stage-2 output mask is also applied for bypass */
  1497. size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
  1498. smmu->s2_output_size = min((unsigned long)PHYS_MASK_SHIFT, size);
  1499. if (smmu->version == 1) {
  1500. smmu->input_size = 32;
  1501. } else {
  1502. #ifdef CONFIG_64BIT
  1503. size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
  1504. size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
  1505. #else
  1506. size = 32;
  1507. #endif
  1508. smmu->input_size = size;
  1509. if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
  1510. (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
  1511. (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
  1512. dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
  1513. PAGE_SIZE);
  1514. return -ENODEV;
  1515. }
  1516. }
  1517. dev_notice(smmu->dev,
  1518. "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
  1519. smmu->input_size, smmu->s1_output_size, smmu->s2_output_size);
  1520. return 0;
  1521. }
  1522. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  1523. {
  1524. struct resource *res;
  1525. struct arm_smmu_device *smmu;
  1526. struct device_node *dev_node;
  1527. struct device *dev = &pdev->dev;
  1528. struct rb_node *node;
  1529. struct of_phandle_args masterspec;
  1530. int num_irqs, i, err;
  1531. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  1532. if (!smmu) {
  1533. dev_err(dev, "failed to allocate arm_smmu_device\n");
  1534. return -ENOMEM;
  1535. }
  1536. smmu->dev = dev;
  1537. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1538. smmu->base = devm_ioremap_resource(dev, res);
  1539. if (IS_ERR(smmu->base))
  1540. return PTR_ERR(smmu->base);
  1541. smmu->size = resource_size(res);
  1542. if (of_property_read_u32(dev->of_node, "#global-interrupts",
  1543. &smmu->num_global_irqs)) {
  1544. dev_err(dev, "missing #global-interrupts property\n");
  1545. return -ENODEV;
  1546. }
  1547. num_irqs = 0;
  1548. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
  1549. num_irqs++;
  1550. if (num_irqs > smmu->num_global_irqs)
  1551. smmu->num_context_irqs++;
  1552. }
  1553. if (!smmu->num_context_irqs) {
  1554. dev_err(dev, "found %d interrupts but expected at least %d\n",
  1555. num_irqs, smmu->num_global_irqs + 1);
  1556. return -ENODEV;
  1557. }
  1558. smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
  1559. GFP_KERNEL);
  1560. if (!smmu->irqs) {
  1561. dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
  1562. return -ENOMEM;
  1563. }
  1564. for (i = 0; i < num_irqs; ++i) {
  1565. int irq = platform_get_irq(pdev, i);
  1566. if (irq < 0) {
  1567. dev_err(dev, "failed to get irq index %d\n", i);
  1568. return -ENODEV;
  1569. }
  1570. smmu->irqs[i] = irq;
  1571. }
  1572. i = 0;
  1573. smmu->masters = RB_ROOT;
  1574. while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
  1575. "#stream-id-cells", i,
  1576. &masterspec)) {
  1577. err = register_smmu_master(smmu, dev, &masterspec);
  1578. if (err) {
  1579. dev_err(dev, "failed to add master %s\n",
  1580. masterspec.np->name);
  1581. goto out_put_masters;
  1582. }
  1583. i++;
  1584. }
  1585. dev_notice(dev, "registered %d master devices\n", i);
  1586. if ((dev_node = of_parse_phandle(dev->of_node, "smmu-parent", 0)))
  1587. smmu->parent_of_node = dev_node;
  1588. err = arm_smmu_device_cfg_probe(smmu);
  1589. if (err)
  1590. goto out_put_parent;
  1591. if (smmu->version > 1 &&
  1592. smmu->num_context_banks != smmu->num_context_irqs) {
  1593. dev_err(dev,
  1594. "found only %d context interrupt(s) but %d required\n",
  1595. smmu->num_context_irqs, smmu->num_context_banks);
  1596. err = -ENODEV;
  1597. goto out_put_parent;
  1598. }
  1599. for (i = 0; i < smmu->num_global_irqs; ++i) {
  1600. err = request_irq(smmu->irqs[i],
  1601. arm_smmu_global_fault,
  1602. IRQF_SHARED,
  1603. "arm-smmu global fault",
  1604. smmu);
  1605. if (err) {
  1606. dev_err(dev, "failed to request global IRQ %d (%u)\n",
  1607. i, smmu->irqs[i]);
  1608. goto out_free_irqs;
  1609. }
  1610. }
  1611. INIT_LIST_HEAD(&smmu->list);
  1612. spin_lock(&arm_smmu_devices_lock);
  1613. list_add(&smmu->list, &arm_smmu_devices);
  1614. spin_unlock(&arm_smmu_devices_lock);
  1615. arm_smmu_device_reset(smmu);
  1616. return 0;
  1617. out_free_irqs:
  1618. while (i--)
  1619. free_irq(smmu->irqs[i], smmu);
  1620. out_put_parent:
  1621. if (smmu->parent_of_node)
  1622. of_node_put(smmu->parent_of_node);
  1623. out_put_masters:
  1624. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1625. struct arm_smmu_master *master;
  1626. master = container_of(node, struct arm_smmu_master, node);
  1627. of_node_put(master->of_node);
  1628. }
  1629. return err;
  1630. }
  1631. static int arm_smmu_device_remove(struct platform_device *pdev)
  1632. {
  1633. int i;
  1634. struct device *dev = &pdev->dev;
  1635. struct arm_smmu_device *curr, *smmu = NULL;
  1636. struct rb_node *node;
  1637. spin_lock(&arm_smmu_devices_lock);
  1638. list_for_each_entry(curr, &arm_smmu_devices, list) {
  1639. if (curr->dev == dev) {
  1640. smmu = curr;
  1641. list_del(&smmu->list);
  1642. break;
  1643. }
  1644. }
  1645. spin_unlock(&arm_smmu_devices_lock);
  1646. if (!smmu)
  1647. return -ENODEV;
  1648. if (smmu->parent_of_node)
  1649. of_node_put(smmu->parent_of_node);
  1650. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1651. struct arm_smmu_master *master;
  1652. master = container_of(node, struct arm_smmu_master, node);
  1653. of_node_put(master->of_node);
  1654. }
  1655. if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
  1656. dev_err(dev, "removing device with active domains!\n");
  1657. for (i = 0; i < smmu->num_global_irqs; ++i)
  1658. free_irq(smmu->irqs[i], smmu);
  1659. /* Turn the thing off */
  1660. writel_relaxed(sCR0_CLIENTPD, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_sCR0);
  1661. return 0;
  1662. }
  1663. #ifdef CONFIG_OF
  1664. static struct of_device_id arm_smmu_of_match[] = {
  1665. { .compatible = "arm,smmu-v1", },
  1666. { .compatible = "arm,smmu-v2", },
  1667. { .compatible = "arm,mmu-400", },
  1668. { .compatible = "arm,mmu-500", },
  1669. { },
  1670. };
  1671. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  1672. #endif
  1673. static struct platform_driver arm_smmu_driver = {
  1674. .driver = {
  1675. .owner = THIS_MODULE,
  1676. .name = "arm-smmu",
  1677. .of_match_table = of_match_ptr(arm_smmu_of_match),
  1678. },
  1679. .probe = arm_smmu_device_dt_probe,
  1680. .remove = arm_smmu_device_remove,
  1681. };
  1682. static int __init arm_smmu_init(void)
  1683. {
  1684. int ret;
  1685. ret = platform_driver_register(&arm_smmu_driver);
  1686. if (ret)
  1687. return ret;
  1688. /* Oh, for a proper bus abstraction */
  1689. if (!iommu_present(&platform_bus_type))
  1690. bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  1691. #ifdef CONFIG_ARM_AMBA
  1692. if (!iommu_present(&amba_bustype))
  1693. bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  1694. #endif
  1695. return 0;
  1696. }
  1697. static void __exit arm_smmu_exit(void)
  1698. {
  1699. return platform_driver_unregister(&arm_smmu_driver);
  1700. }
  1701. subsys_initcall(arm_smmu_init);
  1702. module_exit(arm_smmu_exit);
  1703. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
  1704. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  1705. MODULE_LICENSE("GPL v2");