arm-smmu.c 51 KB

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