arm-smmu.c 51 KB

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