arm-smmu.c 51 KB

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