arm-smmu.c 52 KB

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