arm-smmu.c 51 KB

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