arm-smmu.c 56 KB

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