arm-smmu.c 53 KB

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