arm-smmu.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  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. if (!pgtable_page_ctor(table)) {
  1029. __free_page(table);
  1030. return -ENOMEM;
  1031. }
  1032. pmd_populate(NULL, pmd, table);
  1033. arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
  1034. }
  1035. if (stage == 1) {
  1036. pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
  1037. if (!(flags & IOMMU_WRITE) && (flags & IOMMU_READ))
  1038. pteval |= ARM_SMMU_PTE_AP_RDONLY;
  1039. if (flags & IOMMU_CACHE)
  1040. pteval |= (MAIR_ATTR_IDX_CACHE <<
  1041. ARM_SMMU_PTE_ATTRINDX_SHIFT);
  1042. } else {
  1043. pteval |= ARM_SMMU_PTE_HAP_FAULT;
  1044. if (flags & IOMMU_READ)
  1045. pteval |= ARM_SMMU_PTE_HAP_READ;
  1046. if (flags & IOMMU_WRITE)
  1047. pteval |= ARM_SMMU_PTE_HAP_WRITE;
  1048. if (flags & IOMMU_CACHE)
  1049. pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
  1050. else
  1051. pteval |= ARM_SMMU_PTE_MEMATTR_NC;
  1052. }
  1053. /* If no access, create a faulting entry to avoid TLB fills */
  1054. if (!(flags & (IOMMU_READ | IOMMU_WRITE)))
  1055. pteval &= ~ARM_SMMU_PTE_PAGE;
  1056. pteval |= ARM_SMMU_PTE_SH_IS;
  1057. start = pmd_page_vaddr(*pmd) + pte_index(addr);
  1058. pte = start;
  1059. /*
  1060. * Install the page table entries. This is fairly complicated
  1061. * since we attempt to make use of the contiguous hint in the
  1062. * ptes where possible. The contiguous hint indicates a series
  1063. * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
  1064. * contiguous region with the following constraints:
  1065. *
  1066. * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
  1067. * - Each pte in the region has the contiguous hint bit set
  1068. *
  1069. * This complicates unmapping (also handled by this code, when
  1070. * neither IOMMU_READ or IOMMU_WRITE are set) because it is
  1071. * possible, yet highly unlikely, that a client may unmap only
  1072. * part of a contiguous range. This requires clearing of the
  1073. * contiguous hint bits in the range before installing the new
  1074. * faulting entries.
  1075. *
  1076. * Note that re-mapping an address range without first unmapping
  1077. * it is not supported, so TLB invalidation is not required here
  1078. * and is instead performed at unmap and domain-init time.
  1079. */
  1080. do {
  1081. int i = 1;
  1082. pteval &= ~ARM_SMMU_PTE_CONT;
  1083. if (arm_smmu_pte_is_contiguous_range(addr, end)) {
  1084. i = ARM_SMMU_PTE_CONT_ENTRIES;
  1085. pteval |= ARM_SMMU_PTE_CONT;
  1086. } else if (pte_val(*pte) &
  1087. (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
  1088. int j;
  1089. pte_t *cont_start;
  1090. unsigned long idx = pte_index(addr);
  1091. idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
  1092. cont_start = pmd_page_vaddr(*pmd) + idx;
  1093. for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
  1094. pte_val(*(cont_start + j)) &= ~ARM_SMMU_PTE_CONT;
  1095. arm_smmu_flush_pgtable(smmu, cont_start,
  1096. sizeof(*pte) *
  1097. ARM_SMMU_PTE_CONT_ENTRIES);
  1098. }
  1099. do {
  1100. *pte = pfn_pte(pfn, __pgprot(pteval));
  1101. } while (pte++, pfn++, addr += PAGE_SIZE, --i);
  1102. } while (addr != end);
  1103. arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
  1104. return 0;
  1105. }
  1106. static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
  1107. unsigned long addr, unsigned long end,
  1108. phys_addr_t phys, int flags, int stage)
  1109. {
  1110. int ret;
  1111. pmd_t *pmd;
  1112. unsigned long next, pfn = __phys_to_pfn(phys);
  1113. #ifndef __PAGETABLE_PMD_FOLDED
  1114. if (pud_none(*pud)) {
  1115. pmd = pmd_alloc_one(NULL, addr);
  1116. if (!pmd)
  1117. return -ENOMEM;
  1118. } else
  1119. #endif
  1120. pmd = pmd_offset(pud, addr);
  1121. do {
  1122. next = pmd_addr_end(addr, end);
  1123. ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, end, pfn,
  1124. flags, stage);
  1125. pud_populate(NULL, pud, pmd);
  1126. arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
  1127. phys += next - addr;
  1128. } while (pmd++, addr = next, addr < end);
  1129. return ret;
  1130. }
  1131. static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
  1132. unsigned long addr, unsigned long end,
  1133. phys_addr_t phys, int flags, int stage)
  1134. {
  1135. int ret = 0;
  1136. pud_t *pud;
  1137. unsigned long next;
  1138. #ifndef __PAGETABLE_PUD_FOLDED
  1139. if (pgd_none(*pgd)) {
  1140. pud = pud_alloc_one(NULL, addr);
  1141. if (!pud)
  1142. return -ENOMEM;
  1143. } else
  1144. #endif
  1145. pud = pud_offset(pgd, addr);
  1146. do {
  1147. next = pud_addr_end(addr, end);
  1148. ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
  1149. flags, stage);
  1150. pgd_populate(NULL, pud, pgd);
  1151. arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
  1152. phys += next - addr;
  1153. } while (pud++, addr = next, addr < end);
  1154. return ret;
  1155. }
  1156. static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
  1157. unsigned long iova, phys_addr_t paddr,
  1158. size_t size, int flags)
  1159. {
  1160. int ret, stage;
  1161. unsigned long end;
  1162. phys_addr_t input_mask, output_mask;
  1163. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  1164. pgd_t *pgd = root_cfg->pgd;
  1165. struct arm_smmu_device *smmu = root_cfg->smmu;
  1166. if (root_cfg->cbar == CBAR_TYPE_S2_TRANS) {
  1167. stage = 2;
  1168. output_mask = (1ULL << smmu->s2_output_size) - 1;
  1169. } else {
  1170. stage = 1;
  1171. output_mask = (1ULL << smmu->s1_output_size) - 1;
  1172. }
  1173. if (!pgd)
  1174. return -EINVAL;
  1175. if (size & ~PAGE_MASK)
  1176. return -EINVAL;
  1177. input_mask = (1ULL << smmu->input_size) - 1;
  1178. if ((phys_addr_t)iova & ~input_mask)
  1179. return -ERANGE;
  1180. if (paddr & ~output_mask)
  1181. return -ERANGE;
  1182. spin_lock(&smmu_domain->lock);
  1183. pgd += pgd_index(iova);
  1184. end = iova + size;
  1185. do {
  1186. unsigned long next = pgd_addr_end(iova, end);
  1187. ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
  1188. flags, stage);
  1189. if (ret)
  1190. goto out_unlock;
  1191. paddr += next - iova;
  1192. iova = next;
  1193. } while (pgd++, iova != end);
  1194. out_unlock:
  1195. spin_unlock(&smmu_domain->lock);
  1196. /* Ensure new page tables are visible to the hardware walker */
  1197. if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
  1198. dsb();
  1199. return ret;
  1200. }
  1201. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  1202. phys_addr_t paddr, size_t size, int flags)
  1203. {
  1204. struct arm_smmu_domain *smmu_domain = domain->priv;
  1205. struct arm_smmu_device *smmu = smmu_domain->leaf_smmu;
  1206. if (!smmu_domain || !smmu)
  1207. return -ENODEV;
  1208. /* Check for silent address truncation up the SMMU chain. */
  1209. if ((phys_addr_t)iova & ~smmu_domain->output_mask)
  1210. return -ERANGE;
  1211. return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, flags);
  1212. }
  1213. static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  1214. size_t size)
  1215. {
  1216. int ret;
  1217. struct arm_smmu_domain *smmu_domain = domain->priv;
  1218. ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
  1219. arm_smmu_tlb_inv_context(&smmu_domain->root_cfg);
  1220. return ret ? ret : size;
  1221. }
  1222. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  1223. dma_addr_t iova)
  1224. {
  1225. pgd_t *pgd;
  1226. pud_t *pud;
  1227. pmd_t *pmd;
  1228. pte_t *pte;
  1229. struct arm_smmu_domain *smmu_domain = domain->priv;
  1230. struct arm_smmu_cfg *root_cfg = &smmu_domain->root_cfg;
  1231. struct arm_smmu_device *smmu = root_cfg->smmu;
  1232. spin_lock(&smmu_domain->lock);
  1233. pgd = root_cfg->pgd;
  1234. if (!pgd)
  1235. goto err_unlock;
  1236. pgd += pgd_index(iova);
  1237. if (pgd_none_or_clear_bad(pgd))
  1238. goto err_unlock;
  1239. pud = pud_offset(pgd, iova);
  1240. if (pud_none_or_clear_bad(pud))
  1241. goto err_unlock;
  1242. pmd = pmd_offset(pud, iova);
  1243. if (pmd_none_or_clear_bad(pmd))
  1244. goto err_unlock;
  1245. pte = pmd_page_vaddr(*pmd) + pte_index(iova);
  1246. if (pte_none(pte))
  1247. goto err_unlock;
  1248. spin_unlock(&smmu_domain->lock);
  1249. return __pfn_to_phys(pte_pfn(*pte)) | (iova & ~PAGE_MASK);
  1250. err_unlock:
  1251. spin_unlock(&smmu_domain->lock);
  1252. dev_warn(smmu->dev,
  1253. "invalid (corrupt?) page tables detected for iova 0x%llx\n",
  1254. (unsigned long long)iova);
  1255. return -EINVAL;
  1256. }
  1257. static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
  1258. unsigned long cap)
  1259. {
  1260. unsigned long caps = 0;
  1261. struct arm_smmu_domain *smmu_domain = domain->priv;
  1262. if (smmu_domain->root_cfg.smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
  1263. caps |= IOMMU_CAP_CACHE_COHERENCY;
  1264. return !!(cap & caps);
  1265. }
  1266. static int arm_smmu_add_device(struct device *dev)
  1267. {
  1268. struct arm_smmu_device *child, *parent, *smmu;
  1269. struct arm_smmu_master *master = NULL;
  1270. spin_lock(&arm_smmu_devices_lock);
  1271. list_for_each_entry(parent, &arm_smmu_devices, list) {
  1272. smmu = parent;
  1273. /* Try to find a child of the current SMMU. */
  1274. list_for_each_entry(child, &arm_smmu_devices, list) {
  1275. if (child->parent_of_node == parent->dev->of_node) {
  1276. /* Does the child sit above our master? */
  1277. master = find_smmu_master(child, dev->of_node);
  1278. if (master) {
  1279. smmu = NULL;
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. /* We found some children, so keep searching. */
  1285. if (!smmu) {
  1286. master = NULL;
  1287. continue;
  1288. }
  1289. master = find_smmu_master(smmu, dev->of_node);
  1290. if (master)
  1291. break;
  1292. }
  1293. spin_unlock(&arm_smmu_devices_lock);
  1294. if (!master)
  1295. return -ENODEV;
  1296. dev->archdata.iommu = smmu;
  1297. return 0;
  1298. }
  1299. static void arm_smmu_remove_device(struct device *dev)
  1300. {
  1301. dev->archdata.iommu = NULL;
  1302. }
  1303. static struct iommu_ops arm_smmu_ops = {
  1304. .domain_init = arm_smmu_domain_init,
  1305. .domain_destroy = arm_smmu_domain_destroy,
  1306. .attach_dev = arm_smmu_attach_dev,
  1307. .detach_dev = arm_smmu_detach_dev,
  1308. .map = arm_smmu_map,
  1309. .unmap = arm_smmu_unmap,
  1310. .iova_to_phys = arm_smmu_iova_to_phys,
  1311. .domain_has_cap = arm_smmu_domain_has_cap,
  1312. .add_device = arm_smmu_add_device,
  1313. .remove_device = arm_smmu_remove_device,
  1314. .pgsize_bitmap = (SECTION_SIZE |
  1315. ARM_SMMU_PTE_CONT_SIZE |
  1316. PAGE_SIZE),
  1317. };
  1318. static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
  1319. {
  1320. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1321. void __iomem *sctlr_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB_SCTLR;
  1322. int i = 0;
  1323. u32 scr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sCR0);
  1324. /* Mark all SMRn as invalid and all S2CRn as bypass */
  1325. for (i = 0; i < smmu->num_mapping_groups; ++i) {
  1326. writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(i));
  1327. writel_relaxed(S2CR_TYPE_BYPASS, gr0_base + ARM_SMMU_GR0_S2CR(i));
  1328. }
  1329. /* Make sure all context banks are disabled */
  1330. for (i = 0; i < smmu->num_context_banks; ++i)
  1331. writel_relaxed(0, sctlr_base + ARM_SMMU_CB(smmu, i));
  1332. /* Invalidate the TLB, just in case */
  1333. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
  1334. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
  1335. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
  1336. /* Enable fault reporting */
  1337. scr0 |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
  1338. /* Disable TLB broadcasting. */
  1339. scr0 |= (sCR0_VMIDPNE | sCR0_PTM);
  1340. /* Enable client access, but bypass when no mapping is found */
  1341. scr0 &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
  1342. /* Disable forced broadcasting */
  1343. scr0 &= ~sCR0_FB;
  1344. /* Don't upgrade barriers */
  1345. scr0 &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
  1346. /* Push the button */
  1347. arm_smmu_tlb_sync(smmu);
  1348. writel(scr0, gr0_base + ARM_SMMU_GR0_sCR0);
  1349. }
  1350. static int arm_smmu_id_size_to_bits(int size)
  1351. {
  1352. switch (size) {
  1353. case 0:
  1354. return 32;
  1355. case 1:
  1356. return 36;
  1357. case 2:
  1358. return 40;
  1359. case 3:
  1360. return 42;
  1361. case 4:
  1362. return 44;
  1363. case 5:
  1364. default:
  1365. return 48;
  1366. }
  1367. }
  1368. static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
  1369. {
  1370. unsigned long size;
  1371. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1372. u32 id;
  1373. dev_notice(smmu->dev, "probing hardware configuration...\n");
  1374. /* Primecell ID */
  1375. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
  1376. smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
  1377. dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
  1378. /* ID0 */
  1379. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
  1380. #ifndef CONFIG_64BIT
  1381. if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
  1382. dev_err(smmu->dev, "\tno v7 descriptor support!\n");
  1383. return -ENODEV;
  1384. }
  1385. #endif
  1386. if (id & ID0_S1TS) {
  1387. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  1388. dev_notice(smmu->dev, "\tstage 1 translation\n");
  1389. }
  1390. if (id & ID0_S2TS) {
  1391. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  1392. dev_notice(smmu->dev, "\tstage 2 translation\n");
  1393. }
  1394. if (id & ID0_NTS) {
  1395. smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
  1396. dev_notice(smmu->dev, "\tnested translation\n");
  1397. }
  1398. if (!(smmu->features &
  1399. (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2 |
  1400. ARM_SMMU_FEAT_TRANS_NESTED))) {
  1401. dev_err(smmu->dev, "\tno translation support!\n");
  1402. return -ENODEV;
  1403. }
  1404. if (id & ID0_CTTW) {
  1405. smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
  1406. dev_notice(smmu->dev, "\tcoherent table walk\n");
  1407. }
  1408. if (id & ID0_SMS) {
  1409. u32 smr, sid, mask;
  1410. smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
  1411. smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
  1412. ID0_NUMSMRG_MASK;
  1413. if (smmu->num_mapping_groups == 0) {
  1414. dev_err(smmu->dev,
  1415. "stream-matching supported, but no SMRs present!\n");
  1416. return -ENODEV;
  1417. }
  1418. smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
  1419. smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
  1420. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
  1421. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
  1422. mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
  1423. sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
  1424. if ((mask & sid) != sid) {
  1425. dev_err(smmu->dev,
  1426. "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
  1427. mask, sid);
  1428. return -ENODEV;
  1429. }
  1430. dev_notice(smmu->dev,
  1431. "\tstream matching with %u register groups, mask 0x%x",
  1432. smmu->num_mapping_groups, mask);
  1433. }
  1434. /* ID1 */
  1435. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
  1436. smmu->pagesize = (id & ID1_PAGESIZE) ? SZ_64K : SZ_4K;
  1437. /* Check that we ioremapped enough */
  1438. size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
  1439. size *= (smmu->pagesize << 1);
  1440. if (smmu->size < size)
  1441. dev_warn(smmu->dev,
  1442. "device is 0x%lx bytes but only mapped 0x%lx!\n",
  1443. size, smmu->size);
  1444. smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
  1445. ID1_NUMS2CB_MASK;
  1446. smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
  1447. if (smmu->num_s2_context_banks > smmu->num_context_banks) {
  1448. dev_err(smmu->dev, "impossible number of S2 context banks!\n");
  1449. return -ENODEV;
  1450. }
  1451. dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
  1452. smmu->num_context_banks, smmu->num_s2_context_banks);
  1453. /* ID2 */
  1454. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
  1455. size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
  1456. /*
  1457. * Stage-1 output limited by stage-2 input size due to pgd
  1458. * allocation (PTRS_PER_PGD).
  1459. */
  1460. #ifdef CONFIG_64BIT
  1461. /* Current maximum output size of 39 bits */
  1462. smmu->s1_output_size = min(39UL, size);
  1463. #else
  1464. smmu->s1_output_size = min(32UL, size);
  1465. #endif
  1466. /* The stage-2 output mask is also applied for bypass */
  1467. size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
  1468. smmu->s2_output_size = min((unsigned long)PHYS_MASK_SHIFT, size);
  1469. if (smmu->version == 1) {
  1470. smmu->input_size = 32;
  1471. } else {
  1472. #ifdef CONFIG_64BIT
  1473. size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
  1474. size = min(39, arm_smmu_id_size_to_bits(size));
  1475. #else
  1476. size = 32;
  1477. #endif
  1478. smmu->input_size = size;
  1479. if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
  1480. (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
  1481. (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
  1482. dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
  1483. PAGE_SIZE);
  1484. return -ENODEV;
  1485. }
  1486. }
  1487. dev_notice(smmu->dev,
  1488. "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
  1489. smmu->input_size, smmu->s1_output_size, smmu->s2_output_size);
  1490. return 0;
  1491. }
  1492. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  1493. {
  1494. struct resource *res;
  1495. struct arm_smmu_device *smmu;
  1496. struct device_node *dev_node;
  1497. struct device *dev = &pdev->dev;
  1498. struct rb_node *node;
  1499. struct of_phandle_args masterspec;
  1500. int num_irqs, i, err;
  1501. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  1502. if (!smmu) {
  1503. dev_err(dev, "failed to allocate arm_smmu_device\n");
  1504. return -ENOMEM;
  1505. }
  1506. smmu->dev = dev;
  1507. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1508. if (!res) {
  1509. dev_err(dev, "missing base address/size\n");
  1510. return -ENODEV;
  1511. }
  1512. smmu->size = resource_size(res);
  1513. smmu->base = devm_request_and_ioremap(dev, res);
  1514. if (!smmu->base)
  1515. return -EADDRNOTAVAIL;
  1516. if (of_property_read_u32(dev->of_node, "#global-interrupts",
  1517. &smmu->num_global_irqs)) {
  1518. dev_err(dev, "missing #global-interrupts property\n");
  1519. return -ENODEV;
  1520. }
  1521. num_irqs = 0;
  1522. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
  1523. num_irqs++;
  1524. if (num_irqs > smmu->num_global_irqs)
  1525. smmu->num_context_irqs++;
  1526. }
  1527. if (num_irqs < smmu->num_global_irqs) {
  1528. dev_warn(dev, "found %d interrupts but expected at least %d\n",
  1529. num_irqs, smmu->num_global_irqs);
  1530. smmu->num_global_irqs = num_irqs;
  1531. }
  1532. smmu->num_context_irqs = num_irqs - smmu->num_global_irqs;
  1533. smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
  1534. GFP_KERNEL);
  1535. if (!smmu->irqs) {
  1536. dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
  1537. return -ENOMEM;
  1538. }
  1539. for (i = 0; i < num_irqs; ++i) {
  1540. int irq = platform_get_irq(pdev, i);
  1541. if (irq < 0) {
  1542. dev_err(dev, "failed to get irq index %d\n", i);
  1543. return -ENODEV;
  1544. }
  1545. smmu->irqs[i] = irq;
  1546. }
  1547. i = 0;
  1548. smmu->masters = RB_ROOT;
  1549. while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
  1550. "#stream-id-cells", i,
  1551. &masterspec)) {
  1552. err = register_smmu_master(smmu, dev, &masterspec);
  1553. if (err) {
  1554. dev_err(dev, "failed to add master %s\n",
  1555. masterspec.np->name);
  1556. goto out_put_masters;
  1557. }
  1558. i++;
  1559. }
  1560. dev_notice(dev, "registered %d master devices\n", i);
  1561. if ((dev_node = of_parse_phandle(dev->of_node, "smmu-parent", 0)))
  1562. smmu->parent_of_node = dev_node;
  1563. err = arm_smmu_device_cfg_probe(smmu);
  1564. if (err)
  1565. goto out_put_parent;
  1566. if (smmu->version > 1 &&
  1567. smmu->num_context_banks != smmu->num_context_irqs) {
  1568. dev_err(dev,
  1569. "found only %d context interrupt(s) but %d required\n",
  1570. smmu->num_context_irqs, smmu->num_context_banks);
  1571. goto out_put_parent;
  1572. }
  1573. for (i = 0; i < smmu->num_global_irqs; ++i) {
  1574. err = request_irq(smmu->irqs[i],
  1575. arm_smmu_global_fault,
  1576. IRQF_SHARED,
  1577. "arm-smmu global fault",
  1578. smmu);
  1579. if (err) {
  1580. dev_err(dev, "failed to request global IRQ %d (%u)\n",
  1581. i, smmu->irqs[i]);
  1582. goto out_free_irqs;
  1583. }
  1584. }
  1585. INIT_LIST_HEAD(&smmu->list);
  1586. spin_lock(&arm_smmu_devices_lock);
  1587. list_add(&smmu->list, &arm_smmu_devices);
  1588. spin_unlock(&arm_smmu_devices_lock);
  1589. arm_smmu_device_reset(smmu);
  1590. return 0;
  1591. out_free_irqs:
  1592. while (i--)
  1593. free_irq(smmu->irqs[i], smmu);
  1594. out_put_parent:
  1595. if (smmu->parent_of_node)
  1596. of_node_put(smmu->parent_of_node);
  1597. out_put_masters:
  1598. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1599. struct arm_smmu_master *master;
  1600. master = container_of(node, struct arm_smmu_master, node);
  1601. of_node_put(master->of_node);
  1602. }
  1603. return err;
  1604. }
  1605. static int arm_smmu_device_remove(struct platform_device *pdev)
  1606. {
  1607. int i;
  1608. struct device *dev = &pdev->dev;
  1609. struct arm_smmu_device *curr, *smmu = NULL;
  1610. struct rb_node *node;
  1611. spin_lock(&arm_smmu_devices_lock);
  1612. list_for_each_entry(curr, &arm_smmu_devices, list) {
  1613. if (curr->dev == dev) {
  1614. smmu = curr;
  1615. list_del(&smmu->list);
  1616. break;
  1617. }
  1618. }
  1619. spin_unlock(&arm_smmu_devices_lock);
  1620. if (!smmu)
  1621. return -ENODEV;
  1622. if (smmu->parent_of_node)
  1623. of_node_put(smmu->parent_of_node);
  1624. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1625. struct arm_smmu_master *master;
  1626. master = container_of(node, struct arm_smmu_master, node);
  1627. of_node_put(master->of_node);
  1628. }
  1629. if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
  1630. dev_err(dev, "removing device with active domains!\n");
  1631. for (i = 0; i < smmu->num_global_irqs; ++i)
  1632. free_irq(smmu->irqs[i], smmu);
  1633. /* Turn the thing off */
  1634. writel(sCR0_CLIENTPD, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_sCR0);
  1635. return 0;
  1636. }
  1637. #ifdef CONFIG_OF
  1638. static struct of_device_id arm_smmu_of_match[] = {
  1639. { .compatible = "arm,smmu-v1", },
  1640. { .compatible = "arm,smmu-v2", },
  1641. { .compatible = "arm,mmu-400", },
  1642. { .compatible = "arm,mmu-500", },
  1643. { },
  1644. };
  1645. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  1646. #endif
  1647. static struct platform_driver arm_smmu_driver = {
  1648. .driver = {
  1649. .owner = THIS_MODULE,
  1650. .name = "arm-smmu",
  1651. .of_match_table = of_match_ptr(arm_smmu_of_match),
  1652. },
  1653. .probe = arm_smmu_device_dt_probe,
  1654. .remove = arm_smmu_device_remove,
  1655. };
  1656. static int __init arm_smmu_init(void)
  1657. {
  1658. int ret;
  1659. ret = platform_driver_register(&arm_smmu_driver);
  1660. if (ret)
  1661. return ret;
  1662. /* Oh, for a proper bus abstraction */
  1663. if (!iommu_present(&platform_bus_type))
  1664. bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  1665. if (!iommu_present(&amba_bustype))
  1666. bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  1667. return 0;
  1668. }
  1669. static void __exit arm_smmu_exit(void)
  1670. {
  1671. return platform_driver_unregister(&arm_smmu_driver);
  1672. }
  1673. module_init(arm_smmu_init);
  1674. module_exit(arm_smmu_exit);
  1675. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
  1676. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  1677. MODULE_LICENSE("GPL v2");