arm-smmu.c 52 KB

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