arm-smmu-v3.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724
  1. /*
  2. * IOMMU API for ARM architected SMMUv3 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, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Copyright (C) 2015 ARM Limited
  17. *
  18. * Author: Will Deacon <will.deacon@arm.com>
  19. *
  20. * This driver is powered by bad coffee and bombay mix.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/dma-iommu.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/iommu.h>
  27. #include <linux/iopoll.h>
  28. #include <linux/module.h>
  29. #include <linux/msi.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_iommu.h>
  33. #include <linux/of_platform.h>
  34. #include <linux/pci.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/amba/bus.h>
  37. #include "io-pgtable.h"
  38. /* MMIO registers */
  39. #define ARM_SMMU_IDR0 0x0
  40. #define IDR0_ST_LVL_SHIFT 27
  41. #define IDR0_ST_LVL_MASK 0x3
  42. #define IDR0_ST_LVL_2LVL (1 << IDR0_ST_LVL_SHIFT)
  43. #define IDR0_STALL_MODEL_SHIFT 24
  44. #define IDR0_STALL_MODEL_MASK 0x3
  45. #define IDR0_STALL_MODEL_STALL (0 << IDR0_STALL_MODEL_SHIFT)
  46. #define IDR0_STALL_MODEL_FORCE (2 << IDR0_STALL_MODEL_SHIFT)
  47. #define IDR0_TTENDIAN_SHIFT 21
  48. #define IDR0_TTENDIAN_MASK 0x3
  49. #define IDR0_TTENDIAN_LE (2 << IDR0_TTENDIAN_SHIFT)
  50. #define IDR0_TTENDIAN_BE (3 << IDR0_TTENDIAN_SHIFT)
  51. #define IDR0_TTENDIAN_MIXED (0 << IDR0_TTENDIAN_SHIFT)
  52. #define IDR0_CD2L (1 << 19)
  53. #define IDR0_VMID16 (1 << 18)
  54. #define IDR0_PRI (1 << 16)
  55. #define IDR0_SEV (1 << 14)
  56. #define IDR0_MSI (1 << 13)
  57. #define IDR0_ASID16 (1 << 12)
  58. #define IDR0_ATS (1 << 10)
  59. #define IDR0_HYP (1 << 9)
  60. #define IDR0_COHACC (1 << 4)
  61. #define IDR0_TTF_SHIFT 2
  62. #define IDR0_TTF_MASK 0x3
  63. #define IDR0_TTF_AARCH64 (2 << IDR0_TTF_SHIFT)
  64. #define IDR0_TTF_AARCH32_64 (3 << IDR0_TTF_SHIFT)
  65. #define IDR0_S1P (1 << 1)
  66. #define IDR0_S2P (1 << 0)
  67. #define ARM_SMMU_IDR1 0x4
  68. #define IDR1_TABLES_PRESET (1 << 30)
  69. #define IDR1_QUEUES_PRESET (1 << 29)
  70. #define IDR1_REL (1 << 28)
  71. #define IDR1_CMDQ_SHIFT 21
  72. #define IDR1_CMDQ_MASK 0x1f
  73. #define IDR1_EVTQ_SHIFT 16
  74. #define IDR1_EVTQ_MASK 0x1f
  75. #define IDR1_PRIQ_SHIFT 11
  76. #define IDR1_PRIQ_MASK 0x1f
  77. #define IDR1_SSID_SHIFT 6
  78. #define IDR1_SSID_MASK 0x1f
  79. #define IDR1_SID_SHIFT 0
  80. #define IDR1_SID_MASK 0x3f
  81. #define ARM_SMMU_IDR5 0x14
  82. #define IDR5_STALL_MAX_SHIFT 16
  83. #define IDR5_STALL_MAX_MASK 0xffff
  84. #define IDR5_GRAN64K (1 << 6)
  85. #define IDR5_GRAN16K (1 << 5)
  86. #define IDR5_GRAN4K (1 << 4)
  87. #define IDR5_OAS_SHIFT 0
  88. #define IDR5_OAS_MASK 0x7
  89. #define IDR5_OAS_32_BIT (0 << IDR5_OAS_SHIFT)
  90. #define IDR5_OAS_36_BIT (1 << IDR5_OAS_SHIFT)
  91. #define IDR5_OAS_40_BIT (2 << IDR5_OAS_SHIFT)
  92. #define IDR5_OAS_42_BIT (3 << IDR5_OAS_SHIFT)
  93. #define IDR5_OAS_44_BIT (4 << IDR5_OAS_SHIFT)
  94. #define IDR5_OAS_48_BIT (5 << IDR5_OAS_SHIFT)
  95. #define ARM_SMMU_CR0 0x20
  96. #define CR0_CMDQEN (1 << 3)
  97. #define CR0_EVTQEN (1 << 2)
  98. #define CR0_PRIQEN (1 << 1)
  99. #define CR0_SMMUEN (1 << 0)
  100. #define ARM_SMMU_CR0ACK 0x24
  101. #define ARM_SMMU_CR1 0x28
  102. #define CR1_SH_NSH 0
  103. #define CR1_SH_OSH 2
  104. #define CR1_SH_ISH 3
  105. #define CR1_CACHE_NC 0
  106. #define CR1_CACHE_WB 1
  107. #define CR1_CACHE_WT 2
  108. #define CR1_TABLE_SH_SHIFT 10
  109. #define CR1_TABLE_OC_SHIFT 8
  110. #define CR1_TABLE_IC_SHIFT 6
  111. #define CR1_QUEUE_SH_SHIFT 4
  112. #define CR1_QUEUE_OC_SHIFT 2
  113. #define CR1_QUEUE_IC_SHIFT 0
  114. #define ARM_SMMU_CR2 0x2c
  115. #define CR2_PTM (1 << 2)
  116. #define CR2_RECINVSID (1 << 1)
  117. #define CR2_E2H (1 << 0)
  118. #define ARM_SMMU_GBPA 0x44
  119. #define GBPA_ABORT (1 << 20)
  120. #define GBPA_UPDATE (1 << 31)
  121. #define ARM_SMMU_IRQ_CTRL 0x50
  122. #define IRQ_CTRL_EVTQ_IRQEN (1 << 2)
  123. #define IRQ_CTRL_PRIQ_IRQEN (1 << 1)
  124. #define IRQ_CTRL_GERROR_IRQEN (1 << 0)
  125. #define ARM_SMMU_IRQ_CTRLACK 0x54
  126. #define ARM_SMMU_GERROR 0x60
  127. #define GERROR_SFM_ERR (1 << 8)
  128. #define GERROR_MSI_GERROR_ABT_ERR (1 << 7)
  129. #define GERROR_MSI_PRIQ_ABT_ERR (1 << 6)
  130. #define GERROR_MSI_EVTQ_ABT_ERR (1 << 5)
  131. #define GERROR_MSI_CMDQ_ABT_ERR (1 << 4)
  132. #define GERROR_PRIQ_ABT_ERR (1 << 3)
  133. #define GERROR_EVTQ_ABT_ERR (1 << 2)
  134. #define GERROR_CMDQ_ERR (1 << 0)
  135. #define GERROR_ERR_MASK 0xfd
  136. #define ARM_SMMU_GERRORN 0x64
  137. #define ARM_SMMU_GERROR_IRQ_CFG0 0x68
  138. #define ARM_SMMU_GERROR_IRQ_CFG1 0x70
  139. #define ARM_SMMU_GERROR_IRQ_CFG2 0x74
  140. #define ARM_SMMU_STRTAB_BASE 0x80
  141. #define STRTAB_BASE_RA (1UL << 62)
  142. #define STRTAB_BASE_ADDR_SHIFT 6
  143. #define STRTAB_BASE_ADDR_MASK 0x3ffffffffffUL
  144. #define ARM_SMMU_STRTAB_BASE_CFG 0x88
  145. #define STRTAB_BASE_CFG_LOG2SIZE_SHIFT 0
  146. #define STRTAB_BASE_CFG_LOG2SIZE_MASK 0x3f
  147. #define STRTAB_BASE_CFG_SPLIT_SHIFT 6
  148. #define STRTAB_BASE_CFG_SPLIT_MASK 0x1f
  149. #define STRTAB_BASE_CFG_FMT_SHIFT 16
  150. #define STRTAB_BASE_CFG_FMT_MASK 0x3
  151. #define STRTAB_BASE_CFG_FMT_LINEAR (0 << STRTAB_BASE_CFG_FMT_SHIFT)
  152. #define STRTAB_BASE_CFG_FMT_2LVL (1 << STRTAB_BASE_CFG_FMT_SHIFT)
  153. #define ARM_SMMU_CMDQ_BASE 0x90
  154. #define ARM_SMMU_CMDQ_PROD 0x98
  155. #define ARM_SMMU_CMDQ_CONS 0x9c
  156. #define ARM_SMMU_EVTQ_BASE 0xa0
  157. #define ARM_SMMU_EVTQ_PROD 0x100a8
  158. #define ARM_SMMU_EVTQ_CONS 0x100ac
  159. #define ARM_SMMU_EVTQ_IRQ_CFG0 0xb0
  160. #define ARM_SMMU_EVTQ_IRQ_CFG1 0xb8
  161. #define ARM_SMMU_EVTQ_IRQ_CFG2 0xbc
  162. #define ARM_SMMU_PRIQ_BASE 0xc0
  163. #define ARM_SMMU_PRIQ_PROD 0x100c8
  164. #define ARM_SMMU_PRIQ_CONS 0x100cc
  165. #define ARM_SMMU_PRIQ_IRQ_CFG0 0xd0
  166. #define ARM_SMMU_PRIQ_IRQ_CFG1 0xd8
  167. #define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc
  168. /* Common MSI config fields */
  169. #define MSI_CFG0_ADDR_SHIFT 2
  170. #define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL
  171. #define MSI_CFG2_SH_SHIFT 4
  172. #define MSI_CFG2_SH_NSH (0UL << MSI_CFG2_SH_SHIFT)
  173. #define MSI_CFG2_SH_OSH (2UL << MSI_CFG2_SH_SHIFT)
  174. #define MSI_CFG2_SH_ISH (3UL << MSI_CFG2_SH_SHIFT)
  175. #define MSI_CFG2_MEMATTR_SHIFT 0
  176. #define MSI_CFG2_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG2_MEMATTR_SHIFT)
  177. #define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1))
  178. #define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift))
  179. #define Q_OVERFLOW_FLAG (1 << 31)
  180. #define Q_OVF(q, p) ((p) & Q_OVERFLOW_FLAG)
  181. #define Q_ENT(q, p) ((q)->base + \
  182. Q_IDX(q, p) * (q)->ent_dwords)
  183. #define Q_BASE_RWA (1UL << 62)
  184. #define Q_BASE_ADDR_SHIFT 5
  185. #define Q_BASE_ADDR_MASK 0xfffffffffffUL
  186. #define Q_BASE_LOG2SIZE_SHIFT 0
  187. #define Q_BASE_LOG2SIZE_MASK 0x1fUL
  188. /*
  189. * Stream table.
  190. *
  191. * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
  192. * 2lvl: 128k L1 entries,
  193. * 256 lazy entries per table (each table covers a PCI bus)
  194. */
  195. #define STRTAB_L1_SZ_SHIFT 20
  196. #define STRTAB_SPLIT 8
  197. #define STRTAB_L1_DESC_DWORDS 1
  198. #define STRTAB_L1_DESC_SPAN_SHIFT 0
  199. #define STRTAB_L1_DESC_SPAN_MASK 0x1fUL
  200. #define STRTAB_L1_DESC_L2PTR_SHIFT 6
  201. #define STRTAB_L1_DESC_L2PTR_MASK 0x3ffffffffffUL
  202. #define STRTAB_STE_DWORDS 8
  203. #define STRTAB_STE_0_V (1UL << 0)
  204. #define STRTAB_STE_0_CFG_SHIFT 1
  205. #define STRTAB_STE_0_CFG_MASK 0x7UL
  206. #define STRTAB_STE_0_CFG_ABORT (0UL << STRTAB_STE_0_CFG_SHIFT)
  207. #define STRTAB_STE_0_CFG_BYPASS (4UL << STRTAB_STE_0_CFG_SHIFT)
  208. #define STRTAB_STE_0_CFG_S1_TRANS (5UL << STRTAB_STE_0_CFG_SHIFT)
  209. #define STRTAB_STE_0_CFG_S2_TRANS (6UL << STRTAB_STE_0_CFG_SHIFT)
  210. #define STRTAB_STE_0_S1FMT_SHIFT 4
  211. #define STRTAB_STE_0_S1FMT_LINEAR (0UL << STRTAB_STE_0_S1FMT_SHIFT)
  212. #define STRTAB_STE_0_S1CTXPTR_SHIFT 6
  213. #define STRTAB_STE_0_S1CTXPTR_MASK 0x3ffffffffffUL
  214. #define STRTAB_STE_0_S1CDMAX_SHIFT 59
  215. #define STRTAB_STE_0_S1CDMAX_MASK 0x1fUL
  216. #define STRTAB_STE_1_S1C_CACHE_NC 0UL
  217. #define STRTAB_STE_1_S1C_CACHE_WBRA 1UL
  218. #define STRTAB_STE_1_S1C_CACHE_WT 2UL
  219. #define STRTAB_STE_1_S1C_CACHE_WB 3UL
  220. #define STRTAB_STE_1_S1C_SH_NSH 0UL
  221. #define STRTAB_STE_1_S1C_SH_OSH 2UL
  222. #define STRTAB_STE_1_S1C_SH_ISH 3UL
  223. #define STRTAB_STE_1_S1CIR_SHIFT 2
  224. #define STRTAB_STE_1_S1COR_SHIFT 4
  225. #define STRTAB_STE_1_S1CSH_SHIFT 6
  226. #define STRTAB_STE_1_S1STALLD (1UL << 27)
  227. #define STRTAB_STE_1_EATS_ABT 0UL
  228. #define STRTAB_STE_1_EATS_TRANS 1UL
  229. #define STRTAB_STE_1_EATS_S1CHK 2UL
  230. #define STRTAB_STE_1_EATS_SHIFT 28
  231. #define STRTAB_STE_1_STRW_NSEL1 0UL
  232. #define STRTAB_STE_1_STRW_EL2 2UL
  233. #define STRTAB_STE_1_STRW_SHIFT 30
  234. #define STRTAB_STE_1_SHCFG_INCOMING 1UL
  235. #define STRTAB_STE_1_SHCFG_SHIFT 44
  236. #define STRTAB_STE_1_PRIVCFG_UNPRIV 2UL
  237. #define STRTAB_STE_1_PRIVCFG_SHIFT 48
  238. #define STRTAB_STE_2_S2VMID_SHIFT 0
  239. #define STRTAB_STE_2_S2VMID_MASK 0xffffUL
  240. #define STRTAB_STE_2_VTCR_SHIFT 32
  241. #define STRTAB_STE_2_VTCR_MASK 0x7ffffUL
  242. #define STRTAB_STE_2_S2AA64 (1UL << 51)
  243. #define STRTAB_STE_2_S2ENDI (1UL << 52)
  244. #define STRTAB_STE_2_S2PTW (1UL << 54)
  245. #define STRTAB_STE_2_S2R (1UL << 58)
  246. #define STRTAB_STE_3_S2TTB_SHIFT 4
  247. #define STRTAB_STE_3_S2TTB_MASK 0xfffffffffffUL
  248. /* Context descriptor (stage-1 only) */
  249. #define CTXDESC_CD_DWORDS 8
  250. #define CTXDESC_CD_0_TCR_T0SZ_SHIFT 0
  251. #define ARM64_TCR_T0SZ_SHIFT 0
  252. #define ARM64_TCR_T0SZ_MASK 0x1fUL
  253. #define CTXDESC_CD_0_TCR_TG0_SHIFT 6
  254. #define ARM64_TCR_TG0_SHIFT 14
  255. #define ARM64_TCR_TG0_MASK 0x3UL
  256. #define CTXDESC_CD_0_TCR_IRGN0_SHIFT 8
  257. #define ARM64_TCR_IRGN0_SHIFT 8
  258. #define ARM64_TCR_IRGN0_MASK 0x3UL
  259. #define CTXDESC_CD_0_TCR_ORGN0_SHIFT 10
  260. #define ARM64_TCR_ORGN0_SHIFT 10
  261. #define ARM64_TCR_ORGN0_MASK 0x3UL
  262. #define CTXDESC_CD_0_TCR_SH0_SHIFT 12
  263. #define ARM64_TCR_SH0_SHIFT 12
  264. #define ARM64_TCR_SH0_MASK 0x3UL
  265. #define CTXDESC_CD_0_TCR_EPD0_SHIFT 14
  266. #define ARM64_TCR_EPD0_SHIFT 7
  267. #define ARM64_TCR_EPD0_MASK 0x1UL
  268. #define CTXDESC_CD_0_TCR_EPD1_SHIFT 30
  269. #define ARM64_TCR_EPD1_SHIFT 23
  270. #define ARM64_TCR_EPD1_MASK 0x1UL
  271. #define CTXDESC_CD_0_ENDI (1UL << 15)
  272. #define CTXDESC_CD_0_V (1UL << 31)
  273. #define CTXDESC_CD_0_TCR_IPS_SHIFT 32
  274. #define ARM64_TCR_IPS_SHIFT 32
  275. #define ARM64_TCR_IPS_MASK 0x7UL
  276. #define CTXDESC_CD_0_TCR_TBI0_SHIFT 38
  277. #define ARM64_TCR_TBI0_SHIFT 37
  278. #define ARM64_TCR_TBI0_MASK 0x1UL
  279. #define CTXDESC_CD_0_AA64 (1UL << 41)
  280. #define CTXDESC_CD_0_R (1UL << 45)
  281. #define CTXDESC_CD_0_A (1UL << 46)
  282. #define CTXDESC_CD_0_ASET_SHIFT 47
  283. #define CTXDESC_CD_0_ASET_SHARED (0UL << CTXDESC_CD_0_ASET_SHIFT)
  284. #define CTXDESC_CD_0_ASET_PRIVATE (1UL << CTXDESC_CD_0_ASET_SHIFT)
  285. #define CTXDESC_CD_0_ASID_SHIFT 48
  286. #define CTXDESC_CD_0_ASID_MASK 0xffffUL
  287. #define CTXDESC_CD_1_TTB0_SHIFT 4
  288. #define CTXDESC_CD_1_TTB0_MASK 0xfffffffffffUL
  289. #define CTXDESC_CD_3_MAIR_SHIFT 0
  290. /* Convert between AArch64 (CPU) TCR format and SMMU CD format */
  291. #define ARM_SMMU_TCR2CD(tcr, fld) \
  292. (((tcr) >> ARM64_TCR_##fld##_SHIFT & ARM64_TCR_##fld##_MASK) \
  293. << CTXDESC_CD_0_TCR_##fld##_SHIFT)
  294. /* Command queue */
  295. #define CMDQ_ENT_DWORDS 2
  296. #define CMDQ_MAX_SZ_SHIFT 8
  297. #define CMDQ_ERR_SHIFT 24
  298. #define CMDQ_ERR_MASK 0x7f
  299. #define CMDQ_ERR_CERROR_NONE_IDX 0
  300. #define CMDQ_ERR_CERROR_ILL_IDX 1
  301. #define CMDQ_ERR_CERROR_ABT_IDX 2
  302. #define CMDQ_0_OP_SHIFT 0
  303. #define CMDQ_0_OP_MASK 0xffUL
  304. #define CMDQ_0_SSV (1UL << 11)
  305. #define CMDQ_PREFETCH_0_SID_SHIFT 32
  306. #define CMDQ_PREFETCH_1_SIZE_SHIFT 0
  307. #define CMDQ_PREFETCH_1_ADDR_MASK ~0xfffUL
  308. #define CMDQ_CFGI_0_SID_SHIFT 32
  309. #define CMDQ_CFGI_0_SID_MASK 0xffffffffUL
  310. #define CMDQ_CFGI_1_LEAF (1UL << 0)
  311. #define CMDQ_CFGI_1_RANGE_SHIFT 0
  312. #define CMDQ_CFGI_1_RANGE_MASK 0x1fUL
  313. #define CMDQ_TLBI_0_VMID_SHIFT 32
  314. #define CMDQ_TLBI_0_ASID_SHIFT 48
  315. #define CMDQ_TLBI_1_LEAF (1UL << 0)
  316. #define CMDQ_TLBI_1_VA_MASK ~0xfffUL
  317. #define CMDQ_TLBI_1_IPA_MASK 0xfffffffff000UL
  318. #define CMDQ_PRI_0_SSID_SHIFT 12
  319. #define CMDQ_PRI_0_SSID_MASK 0xfffffUL
  320. #define CMDQ_PRI_0_SID_SHIFT 32
  321. #define CMDQ_PRI_0_SID_MASK 0xffffffffUL
  322. #define CMDQ_PRI_1_GRPID_SHIFT 0
  323. #define CMDQ_PRI_1_GRPID_MASK 0x1ffUL
  324. #define CMDQ_PRI_1_RESP_SHIFT 12
  325. #define CMDQ_PRI_1_RESP_DENY (0UL << CMDQ_PRI_1_RESP_SHIFT)
  326. #define CMDQ_PRI_1_RESP_FAIL (1UL << CMDQ_PRI_1_RESP_SHIFT)
  327. #define CMDQ_PRI_1_RESP_SUCC (2UL << CMDQ_PRI_1_RESP_SHIFT)
  328. #define CMDQ_SYNC_0_CS_SHIFT 12
  329. #define CMDQ_SYNC_0_CS_NONE (0UL << CMDQ_SYNC_0_CS_SHIFT)
  330. #define CMDQ_SYNC_0_CS_SEV (2UL << CMDQ_SYNC_0_CS_SHIFT)
  331. /* Event queue */
  332. #define EVTQ_ENT_DWORDS 4
  333. #define EVTQ_MAX_SZ_SHIFT 7
  334. #define EVTQ_0_ID_SHIFT 0
  335. #define EVTQ_0_ID_MASK 0xffUL
  336. /* PRI queue */
  337. #define PRIQ_ENT_DWORDS 2
  338. #define PRIQ_MAX_SZ_SHIFT 8
  339. #define PRIQ_0_SID_SHIFT 0
  340. #define PRIQ_0_SID_MASK 0xffffffffUL
  341. #define PRIQ_0_SSID_SHIFT 32
  342. #define PRIQ_0_SSID_MASK 0xfffffUL
  343. #define PRIQ_0_PERM_PRIV (1UL << 58)
  344. #define PRIQ_0_PERM_EXEC (1UL << 59)
  345. #define PRIQ_0_PERM_READ (1UL << 60)
  346. #define PRIQ_0_PERM_WRITE (1UL << 61)
  347. #define PRIQ_0_PRG_LAST (1UL << 62)
  348. #define PRIQ_0_SSID_V (1UL << 63)
  349. #define PRIQ_1_PRG_IDX_SHIFT 0
  350. #define PRIQ_1_PRG_IDX_MASK 0x1ffUL
  351. #define PRIQ_1_ADDR_SHIFT 12
  352. #define PRIQ_1_ADDR_MASK 0xfffffffffffffUL
  353. /* High-level queue structures */
  354. #define ARM_SMMU_POLL_TIMEOUT_US 100
  355. static bool disable_bypass;
  356. module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO);
  357. MODULE_PARM_DESC(disable_bypass,
  358. "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
  359. enum pri_resp {
  360. PRI_RESP_DENY,
  361. PRI_RESP_FAIL,
  362. PRI_RESP_SUCC,
  363. };
  364. enum arm_smmu_msi_index {
  365. EVTQ_MSI_INDEX,
  366. GERROR_MSI_INDEX,
  367. PRIQ_MSI_INDEX,
  368. ARM_SMMU_MAX_MSIS,
  369. };
  370. static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {
  371. [EVTQ_MSI_INDEX] = {
  372. ARM_SMMU_EVTQ_IRQ_CFG0,
  373. ARM_SMMU_EVTQ_IRQ_CFG1,
  374. ARM_SMMU_EVTQ_IRQ_CFG2,
  375. },
  376. [GERROR_MSI_INDEX] = {
  377. ARM_SMMU_GERROR_IRQ_CFG0,
  378. ARM_SMMU_GERROR_IRQ_CFG1,
  379. ARM_SMMU_GERROR_IRQ_CFG2,
  380. },
  381. [PRIQ_MSI_INDEX] = {
  382. ARM_SMMU_PRIQ_IRQ_CFG0,
  383. ARM_SMMU_PRIQ_IRQ_CFG1,
  384. ARM_SMMU_PRIQ_IRQ_CFG2,
  385. },
  386. };
  387. struct arm_smmu_cmdq_ent {
  388. /* Common fields */
  389. u8 opcode;
  390. bool substream_valid;
  391. /* Command-specific fields */
  392. union {
  393. #define CMDQ_OP_PREFETCH_CFG 0x1
  394. struct {
  395. u32 sid;
  396. u8 size;
  397. u64 addr;
  398. } prefetch;
  399. #define CMDQ_OP_CFGI_STE 0x3
  400. #define CMDQ_OP_CFGI_ALL 0x4
  401. struct {
  402. u32 sid;
  403. union {
  404. bool leaf;
  405. u8 span;
  406. };
  407. } cfgi;
  408. #define CMDQ_OP_TLBI_NH_ASID 0x11
  409. #define CMDQ_OP_TLBI_NH_VA 0x12
  410. #define CMDQ_OP_TLBI_EL2_ALL 0x20
  411. #define CMDQ_OP_TLBI_S12_VMALL 0x28
  412. #define CMDQ_OP_TLBI_S2_IPA 0x2a
  413. #define CMDQ_OP_TLBI_NSNH_ALL 0x30
  414. struct {
  415. u16 asid;
  416. u16 vmid;
  417. bool leaf;
  418. u64 addr;
  419. } tlbi;
  420. #define CMDQ_OP_PRI_RESP 0x41
  421. struct {
  422. u32 sid;
  423. u32 ssid;
  424. u16 grpid;
  425. enum pri_resp resp;
  426. } pri;
  427. #define CMDQ_OP_CMD_SYNC 0x46
  428. };
  429. };
  430. struct arm_smmu_queue {
  431. int irq; /* Wired interrupt */
  432. __le64 *base;
  433. dma_addr_t base_dma;
  434. u64 q_base;
  435. size_t ent_dwords;
  436. u32 max_n_shift;
  437. u32 prod;
  438. u32 cons;
  439. u32 __iomem *prod_reg;
  440. u32 __iomem *cons_reg;
  441. };
  442. struct arm_smmu_cmdq {
  443. struct arm_smmu_queue q;
  444. spinlock_t lock;
  445. };
  446. struct arm_smmu_evtq {
  447. struct arm_smmu_queue q;
  448. u32 max_stalls;
  449. };
  450. struct arm_smmu_priq {
  451. struct arm_smmu_queue q;
  452. };
  453. /* High-level stream table and context descriptor structures */
  454. struct arm_smmu_strtab_l1_desc {
  455. u8 span;
  456. __le64 *l2ptr;
  457. dma_addr_t l2ptr_dma;
  458. };
  459. struct arm_smmu_s1_cfg {
  460. __le64 *cdptr;
  461. dma_addr_t cdptr_dma;
  462. struct arm_smmu_ctx_desc {
  463. u16 asid;
  464. u64 ttbr;
  465. u64 tcr;
  466. u64 mair;
  467. } cd;
  468. };
  469. struct arm_smmu_s2_cfg {
  470. u16 vmid;
  471. u64 vttbr;
  472. u64 vtcr;
  473. };
  474. struct arm_smmu_strtab_ent {
  475. bool valid;
  476. bool bypass; /* Overrides s1/s2 config */
  477. struct arm_smmu_s1_cfg *s1_cfg;
  478. struct arm_smmu_s2_cfg *s2_cfg;
  479. };
  480. struct arm_smmu_strtab_cfg {
  481. __le64 *strtab;
  482. dma_addr_t strtab_dma;
  483. struct arm_smmu_strtab_l1_desc *l1_desc;
  484. unsigned int num_l1_ents;
  485. u64 strtab_base;
  486. u32 strtab_base_cfg;
  487. };
  488. /* An SMMUv3 instance */
  489. struct arm_smmu_device {
  490. struct device *dev;
  491. void __iomem *base;
  492. #define ARM_SMMU_FEAT_2_LVL_STRTAB (1 << 0)
  493. #define ARM_SMMU_FEAT_2_LVL_CDTAB (1 << 1)
  494. #define ARM_SMMU_FEAT_TT_LE (1 << 2)
  495. #define ARM_SMMU_FEAT_TT_BE (1 << 3)
  496. #define ARM_SMMU_FEAT_PRI (1 << 4)
  497. #define ARM_SMMU_FEAT_ATS (1 << 5)
  498. #define ARM_SMMU_FEAT_SEV (1 << 6)
  499. #define ARM_SMMU_FEAT_MSI (1 << 7)
  500. #define ARM_SMMU_FEAT_COHERENCY (1 << 8)
  501. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 9)
  502. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 10)
  503. #define ARM_SMMU_FEAT_STALLS (1 << 11)
  504. #define ARM_SMMU_FEAT_HYP (1 << 12)
  505. u32 features;
  506. #define ARM_SMMU_OPT_SKIP_PREFETCH (1 << 0)
  507. u32 options;
  508. struct arm_smmu_cmdq cmdq;
  509. struct arm_smmu_evtq evtq;
  510. struct arm_smmu_priq priq;
  511. int gerr_irq;
  512. unsigned long ias; /* IPA */
  513. unsigned long oas; /* PA */
  514. unsigned long pgsize_bitmap;
  515. #define ARM_SMMU_MAX_ASIDS (1 << 16)
  516. unsigned int asid_bits;
  517. DECLARE_BITMAP(asid_map, ARM_SMMU_MAX_ASIDS);
  518. #define ARM_SMMU_MAX_VMIDS (1 << 16)
  519. unsigned int vmid_bits;
  520. DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);
  521. unsigned int ssid_bits;
  522. unsigned int sid_bits;
  523. struct arm_smmu_strtab_cfg strtab_cfg;
  524. };
  525. /* SMMU private data for each master */
  526. struct arm_smmu_master_data {
  527. struct arm_smmu_device *smmu;
  528. struct arm_smmu_strtab_ent ste;
  529. };
  530. /* SMMU private data for an IOMMU domain */
  531. enum arm_smmu_domain_stage {
  532. ARM_SMMU_DOMAIN_S1 = 0,
  533. ARM_SMMU_DOMAIN_S2,
  534. ARM_SMMU_DOMAIN_NESTED,
  535. };
  536. struct arm_smmu_domain {
  537. struct arm_smmu_device *smmu;
  538. struct mutex init_mutex; /* Protects smmu pointer */
  539. struct io_pgtable_ops *pgtbl_ops;
  540. spinlock_t pgtbl_lock;
  541. enum arm_smmu_domain_stage stage;
  542. union {
  543. struct arm_smmu_s1_cfg s1_cfg;
  544. struct arm_smmu_s2_cfg s2_cfg;
  545. };
  546. struct iommu_domain domain;
  547. };
  548. struct arm_smmu_option_prop {
  549. u32 opt;
  550. const char *prop;
  551. };
  552. static struct arm_smmu_option_prop arm_smmu_options[] = {
  553. { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
  554. { 0, NULL},
  555. };
  556. static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
  557. {
  558. return container_of(dom, struct arm_smmu_domain, domain);
  559. }
  560. static void parse_driver_options(struct arm_smmu_device *smmu)
  561. {
  562. int i = 0;
  563. do {
  564. if (of_property_read_bool(smmu->dev->of_node,
  565. arm_smmu_options[i].prop)) {
  566. smmu->options |= arm_smmu_options[i].opt;
  567. dev_notice(smmu->dev, "option %s\n",
  568. arm_smmu_options[i].prop);
  569. }
  570. } while (arm_smmu_options[++i].opt);
  571. }
  572. /* Low-level queue manipulation functions */
  573. static bool queue_full(struct arm_smmu_queue *q)
  574. {
  575. return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
  576. Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
  577. }
  578. static bool queue_empty(struct arm_smmu_queue *q)
  579. {
  580. return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
  581. Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
  582. }
  583. static void queue_sync_cons(struct arm_smmu_queue *q)
  584. {
  585. q->cons = readl_relaxed(q->cons_reg);
  586. }
  587. static void queue_inc_cons(struct arm_smmu_queue *q)
  588. {
  589. u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
  590. q->cons = Q_OVF(q, q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
  591. writel(q->cons, q->cons_reg);
  592. }
  593. static int queue_sync_prod(struct arm_smmu_queue *q)
  594. {
  595. int ret = 0;
  596. u32 prod = readl_relaxed(q->prod_reg);
  597. if (Q_OVF(q, prod) != Q_OVF(q, q->prod))
  598. ret = -EOVERFLOW;
  599. q->prod = prod;
  600. return ret;
  601. }
  602. static void queue_inc_prod(struct arm_smmu_queue *q)
  603. {
  604. u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + 1;
  605. q->prod = Q_OVF(q, q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
  606. writel(q->prod, q->prod_reg);
  607. }
  608. /*
  609. * Wait for the SMMU to consume items. If drain is true, wait until the queue
  610. * is empty. Otherwise, wait until there is at least one free slot.
  611. */
  612. static int queue_poll_cons(struct arm_smmu_queue *q, bool drain, bool wfe)
  613. {
  614. ktime_t timeout = ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US);
  615. while (queue_sync_cons(q), (drain ? !queue_empty(q) : queue_full(q))) {
  616. if (ktime_compare(ktime_get(), timeout) > 0)
  617. return -ETIMEDOUT;
  618. if (wfe) {
  619. wfe();
  620. } else {
  621. cpu_relax();
  622. udelay(1);
  623. }
  624. }
  625. return 0;
  626. }
  627. static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
  628. {
  629. int i;
  630. for (i = 0; i < n_dwords; ++i)
  631. *dst++ = cpu_to_le64(*src++);
  632. }
  633. static int queue_insert_raw(struct arm_smmu_queue *q, u64 *ent)
  634. {
  635. if (queue_full(q))
  636. return -ENOSPC;
  637. queue_write(Q_ENT(q, q->prod), ent, q->ent_dwords);
  638. queue_inc_prod(q);
  639. return 0;
  640. }
  641. static void queue_read(__le64 *dst, u64 *src, size_t n_dwords)
  642. {
  643. int i;
  644. for (i = 0; i < n_dwords; ++i)
  645. *dst++ = le64_to_cpu(*src++);
  646. }
  647. static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
  648. {
  649. if (queue_empty(q))
  650. return -EAGAIN;
  651. queue_read(ent, Q_ENT(q, q->cons), q->ent_dwords);
  652. queue_inc_cons(q);
  653. return 0;
  654. }
  655. /* High-level queue accessors */
  656. static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
  657. {
  658. memset(cmd, 0, CMDQ_ENT_DWORDS << 3);
  659. cmd[0] |= (ent->opcode & CMDQ_0_OP_MASK) << CMDQ_0_OP_SHIFT;
  660. switch (ent->opcode) {
  661. case CMDQ_OP_TLBI_EL2_ALL:
  662. case CMDQ_OP_TLBI_NSNH_ALL:
  663. break;
  664. case CMDQ_OP_PREFETCH_CFG:
  665. cmd[0] |= (u64)ent->prefetch.sid << CMDQ_PREFETCH_0_SID_SHIFT;
  666. cmd[1] |= ent->prefetch.size << CMDQ_PREFETCH_1_SIZE_SHIFT;
  667. cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK;
  668. break;
  669. case CMDQ_OP_CFGI_STE:
  670. cmd[0] |= (u64)ent->cfgi.sid << CMDQ_CFGI_0_SID_SHIFT;
  671. cmd[1] |= ent->cfgi.leaf ? CMDQ_CFGI_1_LEAF : 0;
  672. break;
  673. case CMDQ_OP_CFGI_ALL:
  674. /* Cover the entire SID range */
  675. cmd[1] |= CMDQ_CFGI_1_RANGE_MASK << CMDQ_CFGI_1_RANGE_SHIFT;
  676. break;
  677. case CMDQ_OP_TLBI_NH_VA:
  678. cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
  679. cmd[1] |= ent->tlbi.leaf ? CMDQ_TLBI_1_LEAF : 0;
  680. cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_VA_MASK;
  681. break;
  682. case CMDQ_OP_TLBI_S2_IPA:
  683. cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
  684. cmd[1] |= ent->tlbi.leaf ? CMDQ_TLBI_1_LEAF : 0;
  685. cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_IPA_MASK;
  686. break;
  687. case CMDQ_OP_TLBI_NH_ASID:
  688. cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
  689. /* Fallthrough */
  690. case CMDQ_OP_TLBI_S12_VMALL:
  691. cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
  692. break;
  693. case CMDQ_OP_PRI_RESP:
  694. cmd[0] |= ent->substream_valid ? CMDQ_0_SSV : 0;
  695. cmd[0] |= ent->pri.ssid << CMDQ_PRI_0_SSID_SHIFT;
  696. cmd[0] |= (u64)ent->pri.sid << CMDQ_PRI_0_SID_SHIFT;
  697. cmd[1] |= ent->pri.grpid << CMDQ_PRI_1_GRPID_SHIFT;
  698. switch (ent->pri.resp) {
  699. case PRI_RESP_DENY:
  700. cmd[1] |= CMDQ_PRI_1_RESP_DENY;
  701. break;
  702. case PRI_RESP_FAIL:
  703. cmd[1] |= CMDQ_PRI_1_RESP_FAIL;
  704. break;
  705. case PRI_RESP_SUCC:
  706. cmd[1] |= CMDQ_PRI_1_RESP_SUCC;
  707. break;
  708. default:
  709. return -EINVAL;
  710. }
  711. break;
  712. case CMDQ_OP_CMD_SYNC:
  713. cmd[0] |= CMDQ_SYNC_0_CS_SEV;
  714. break;
  715. default:
  716. return -ENOENT;
  717. }
  718. return 0;
  719. }
  720. static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
  721. {
  722. static const char *cerror_str[] = {
  723. [CMDQ_ERR_CERROR_NONE_IDX] = "No error",
  724. [CMDQ_ERR_CERROR_ILL_IDX] = "Illegal command",
  725. [CMDQ_ERR_CERROR_ABT_IDX] = "Abort on command fetch",
  726. };
  727. int i;
  728. u64 cmd[CMDQ_ENT_DWORDS];
  729. struct arm_smmu_queue *q = &smmu->cmdq.q;
  730. u32 cons = readl_relaxed(q->cons_reg);
  731. u32 idx = cons >> CMDQ_ERR_SHIFT & CMDQ_ERR_MASK;
  732. struct arm_smmu_cmdq_ent cmd_sync = {
  733. .opcode = CMDQ_OP_CMD_SYNC,
  734. };
  735. dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
  736. idx < ARRAY_SIZE(cerror_str) ? cerror_str[idx] : "Unknown");
  737. switch (idx) {
  738. case CMDQ_ERR_CERROR_ABT_IDX:
  739. dev_err(smmu->dev, "retrying command fetch\n");
  740. case CMDQ_ERR_CERROR_NONE_IDX:
  741. return;
  742. case CMDQ_ERR_CERROR_ILL_IDX:
  743. /* Fallthrough */
  744. default:
  745. break;
  746. }
  747. /*
  748. * We may have concurrent producers, so we need to be careful
  749. * not to touch any of the shadow cmdq state.
  750. */
  751. queue_read(cmd, Q_ENT(q, cons), q->ent_dwords);
  752. dev_err(smmu->dev, "skipping command in error state:\n");
  753. for (i = 0; i < ARRAY_SIZE(cmd); ++i)
  754. dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
  755. /* Convert the erroneous command into a CMD_SYNC */
  756. if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
  757. dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
  758. return;
  759. }
  760. queue_write(Q_ENT(q, cons), cmd, q->ent_dwords);
  761. }
  762. static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
  763. struct arm_smmu_cmdq_ent *ent)
  764. {
  765. u64 cmd[CMDQ_ENT_DWORDS];
  766. unsigned long flags;
  767. bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
  768. struct arm_smmu_queue *q = &smmu->cmdq.q;
  769. if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
  770. dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
  771. ent->opcode);
  772. return;
  773. }
  774. spin_lock_irqsave(&smmu->cmdq.lock, flags);
  775. while (queue_insert_raw(q, cmd) == -ENOSPC) {
  776. if (queue_poll_cons(q, false, wfe))
  777. dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
  778. }
  779. if (ent->opcode == CMDQ_OP_CMD_SYNC && queue_poll_cons(q, true, wfe))
  780. dev_err_ratelimited(smmu->dev, "CMD_SYNC timeout\n");
  781. spin_unlock_irqrestore(&smmu->cmdq.lock, flags);
  782. }
  783. /* Context descriptor manipulation functions */
  784. static u64 arm_smmu_cpu_tcr_to_cd(u64 tcr)
  785. {
  786. u64 val = 0;
  787. /* Repack the TCR. Just care about TTBR0 for now */
  788. val |= ARM_SMMU_TCR2CD(tcr, T0SZ);
  789. val |= ARM_SMMU_TCR2CD(tcr, TG0);
  790. val |= ARM_SMMU_TCR2CD(tcr, IRGN0);
  791. val |= ARM_SMMU_TCR2CD(tcr, ORGN0);
  792. val |= ARM_SMMU_TCR2CD(tcr, SH0);
  793. val |= ARM_SMMU_TCR2CD(tcr, EPD0);
  794. val |= ARM_SMMU_TCR2CD(tcr, EPD1);
  795. val |= ARM_SMMU_TCR2CD(tcr, IPS);
  796. val |= ARM_SMMU_TCR2CD(tcr, TBI0);
  797. return val;
  798. }
  799. static void arm_smmu_write_ctx_desc(struct arm_smmu_device *smmu,
  800. struct arm_smmu_s1_cfg *cfg)
  801. {
  802. u64 val;
  803. /*
  804. * We don't need to issue any invalidation here, as we'll invalidate
  805. * the STE when installing the new entry anyway.
  806. */
  807. val = arm_smmu_cpu_tcr_to_cd(cfg->cd.tcr) |
  808. #ifdef __BIG_ENDIAN
  809. CTXDESC_CD_0_ENDI |
  810. #endif
  811. CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET_PRIVATE |
  812. CTXDESC_CD_0_AA64 | (u64)cfg->cd.asid << CTXDESC_CD_0_ASID_SHIFT |
  813. CTXDESC_CD_0_V;
  814. cfg->cdptr[0] = cpu_to_le64(val);
  815. val = cfg->cd.ttbr & CTXDESC_CD_1_TTB0_MASK << CTXDESC_CD_1_TTB0_SHIFT;
  816. cfg->cdptr[1] = cpu_to_le64(val);
  817. cfg->cdptr[3] = cpu_to_le64(cfg->cd.mair << CTXDESC_CD_3_MAIR_SHIFT);
  818. }
  819. /* Stream table manipulation functions */
  820. static void
  821. arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
  822. {
  823. u64 val = 0;
  824. val |= (desc->span & STRTAB_L1_DESC_SPAN_MASK)
  825. << STRTAB_L1_DESC_SPAN_SHIFT;
  826. val |= desc->l2ptr_dma &
  827. STRTAB_L1_DESC_L2PTR_MASK << STRTAB_L1_DESC_L2PTR_SHIFT;
  828. *dst = cpu_to_le64(val);
  829. }
  830. static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
  831. {
  832. struct arm_smmu_cmdq_ent cmd = {
  833. .opcode = CMDQ_OP_CFGI_STE,
  834. .cfgi = {
  835. .sid = sid,
  836. .leaf = true,
  837. },
  838. };
  839. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  840. cmd.opcode = CMDQ_OP_CMD_SYNC;
  841. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  842. }
  843. static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
  844. __le64 *dst, struct arm_smmu_strtab_ent *ste)
  845. {
  846. /*
  847. * This is hideously complicated, but we only really care about
  848. * three cases at the moment:
  849. *
  850. * 1. Invalid (all zero) -> bypass (init)
  851. * 2. Bypass -> translation (attach)
  852. * 3. Translation -> bypass (detach)
  853. *
  854. * Given that we can't update the STE atomically and the SMMU
  855. * doesn't read the thing in a defined order, that leaves us
  856. * with the following maintenance requirements:
  857. *
  858. * 1. Update Config, return (init time STEs aren't live)
  859. * 2. Write everything apart from dword 0, sync, write dword 0, sync
  860. * 3. Update Config, sync
  861. */
  862. u64 val = le64_to_cpu(dst[0]);
  863. bool ste_live = false;
  864. struct arm_smmu_cmdq_ent prefetch_cmd = {
  865. .opcode = CMDQ_OP_PREFETCH_CFG,
  866. .prefetch = {
  867. .sid = sid,
  868. },
  869. };
  870. if (val & STRTAB_STE_0_V) {
  871. u64 cfg;
  872. cfg = val & STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT;
  873. switch (cfg) {
  874. case STRTAB_STE_0_CFG_BYPASS:
  875. break;
  876. case STRTAB_STE_0_CFG_S1_TRANS:
  877. case STRTAB_STE_0_CFG_S2_TRANS:
  878. ste_live = true;
  879. break;
  880. case STRTAB_STE_0_CFG_ABORT:
  881. if (disable_bypass)
  882. break;
  883. default:
  884. BUG(); /* STE corruption */
  885. }
  886. }
  887. /* Nuke the existing STE_0 value, as we're going to rewrite it */
  888. val = ste->valid ? STRTAB_STE_0_V : 0;
  889. if (ste->bypass) {
  890. val |= disable_bypass ? STRTAB_STE_0_CFG_ABORT
  891. : STRTAB_STE_0_CFG_BYPASS;
  892. dst[0] = cpu_to_le64(val);
  893. dst[1] = cpu_to_le64(STRTAB_STE_1_SHCFG_INCOMING
  894. << STRTAB_STE_1_SHCFG_SHIFT);
  895. dst[2] = 0; /* Nuke the VMID */
  896. if (ste_live)
  897. arm_smmu_sync_ste_for_sid(smmu, sid);
  898. return;
  899. }
  900. if (ste->s1_cfg) {
  901. BUG_ON(ste_live);
  902. dst[1] = cpu_to_le64(
  903. STRTAB_STE_1_S1C_CACHE_WBRA
  904. << STRTAB_STE_1_S1CIR_SHIFT |
  905. STRTAB_STE_1_S1C_CACHE_WBRA
  906. << STRTAB_STE_1_S1COR_SHIFT |
  907. STRTAB_STE_1_S1C_SH_ISH << STRTAB_STE_1_S1CSH_SHIFT |
  908. #ifdef CONFIG_PCI_ATS
  909. STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT |
  910. #endif
  911. STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT |
  912. STRTAB_STE_1_PRIVCFG_UNPRIV <<
  913. STRTAB_STE_1_PRIVCFG_SHIFT);
  914. if (smmu->features & ARM_SMMU_FEAT_STALLS)
  915. dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD);
  916. val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK
  917. << STRTAB_STE_0_S1CTXPTR_SHIFT) |
  918. STRTAB_STE_0_CFG_S1_TRANS;
  919. }
  920. if (ste->s2_cfg) {
  921. BUG_ON(ste_live);
  922. dst[2] = cpu_to_le64(
  923. ste->s2_cfg->vmid << STRTAB_STE_2_S2VMID_SHIFT |
  924. (ste->s2_cfg->vtcr & STRTAB_STE_2_VTCR_MASK)
  925. << STRTAB_STE_2_VTCR_SHIFT |
  926. #ifdef __BIG_ENDIAN
  927. STRTAB_STE_2_S2ENDI |
  928. #endif
  929. STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
  930. STRTAB_STE_2_S2R);
  931. dst[3] = cpu_to_le64(ste->s2_cfg->vttbr &
  932. STRTAB_STE_3_S2TTB_MASK << STRTAB_STE_3_S2TTB_SHIFT);
  933. val |= STRTAB_STE_0_CFG_S2_TRANS;
  934. }
  935. arm_smmu_sync_ste_for_sid(smmu, sid);
  936. dst[0] = cpu_to_le64(val);
  937. arm_smmu_sync_ste_for_sid(smmu, sid);
  938. /* It's likely that we'll want to use the new STE soon */
  939. if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
  940. arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
  941. }
  942. static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
  943. {
  944. unsigned int i;
  945. struct arm_smmu_strtab_ent ste = {
  946. .valid = true,
  947. .bypass = true,
  948. };
  949. for (i = 0; i < nent; ++i) {
  950. arm_smmu_write_strtab_ent(NULL, -1, strtab, &ste);
  951. strtab += STRTAB_STE_DWORDS;
  952. }
  953. }
  954. static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
  955. {
  956. size_t size;
  957. void *strtab;
  958. struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
  959. struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
  960. if (desc->l2ptr)
  961. return 0;
  962. size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
  963. strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
  964. desc->span = STRTAB_SPLIT + 1;
  965. desc->l2ptr = dmam_alloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
  966. GFP_KERNEL | __GFP_ZERO);
  967. if (!desc->l2ptr) {
  968. dev_err(smmu->dev,
  969. "failed to allocate l2 stream table for SID %u\n",
  970. sid);
  971. return -ENOMEM;
  972. }
  973. arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
  974. arm_smmu_write_strtab_l1_desc(strtab, desc);
  975. return 0;
  976. }
  977. /* IRQ and event handlers */
  978. static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
  979. {
  980. int i;
  981. struct arm_smmu_device *smmu = dev;
  982. struct arm_smmu_queue *q = &smmu->evtq.q;
  983. u64 evt[EVTQ_ENT_DWORDS];
  984. do {
  985. while (!queue_remove_raw(q, evt)) {
  986. u8 id = evt[0] >> EVTQ_0_ID_SHIFT & EVTQ_0_ID_MASK;
  987. dev_info(smmu->dev, "event 0x%02x received:\n", id);
  988. for (i = 0; i < ARRAY_SIZE(evt); ++i)
  989. dev_info(smmu->dev, "\t0x%016llx\n",
  990. (unsigned long long)evt[i]);
  991. }
  992. /*
  993. * Not much we can do on overflow, so scream and pretend we're
  994. * trying harder.
  995. */
  996. if (queue_sync_prod(q) == -EOVERFLOW)
  997. dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
  998. } while (!queue_empty(q));
  999. /* Sync our overflow flag, as we believe we're up to speed */
  1000. q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
  1001. return IRQ_HANDLED;
  1002. }
  1003. static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt)
  1004. {
  1005. u32 sid, ssid;
  1006. u16 grpid;
  1007. bool ssv, last;
  1008. sid = evt[0] >> PRIQ_0_SID_SHIFT & PRIQ_0_SID_MASK;
  1009. ssv = evt[0] & PRIQ_0_SSID_V;
  1010. ssid = ssv ? evt[0] >> PRIQ_0_SSID_SHIFT & PRIQ_0_SSID_MASK : 0;
  1011. last = evt[0] & PRIQ_0_PRG_LAST;
  1012. grpid = evt[1] >> PRIQ_1_PRG_IDX_SHIFT & PRIQ_1_PRG_IDX_MASK;
  1013. dev_info(smmu->dev, "unexpected PRI request received:\n");
  1014. dev_info(smmu->dev,
  1015. "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
  1016. sid, ssid, grpid, last ? "L" : "",
  1017. evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
  1018. evt[0] & PRIQ_0_PERM_READ ? "R" : "",
  1019. evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
  1020. evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
  1021. evt[1] & PRIQ_1_ADDR_MASK << PRIQ_1_ADDR_SHIFT);
  1022. if (last) {
  1023. struct arm_smmu_cmdq_ent cmd = {
  1024. .opcode = CMDQ_OP_PRI_RESP,
  1025. .substream_valid = ssv,
  1026. .pri = {
  1027. .sid = sid,
  1028. .ssid = ssid,
  1029. .grpid = grpid,
  1030. .resp = PRI_RESP_DENY,
  1031. },
  1032. };
  1033. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1034. }
  1035. }
  1036. static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
  1037. {
  1038. struct arm_smmu_device *smmu = dev;
  1039. struct arm_smmu_queue *q = &smmu->priq.q;
  1040. u64 evt[PRIQ_ENT_DWORDS];
  1041. do {
  1042. while (!queue_remove_raw(q, evt))
  1043. arm_smmu_handle_ppr(smmu, evt);
  1044. if (queue_sync_prod(q) == -EOVERFLOW)
  1045. dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
  1046. } while (!queue_empty(q));
  1047. /* Sync our overflow flag, as we believe we're up to speed */
  1048. q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
  1049. return IRQ_HANDLED;
  1050. }
  1051. static irqreturn_t arm_smmu_cmdq_sync_handler(int irq, void *dev)
  1052. {
  1053. /* We don't actually use CMD_SYNC interrupts for anything */
  1054. return IRQ_HANDLED;
  1055. }
  1056. static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
  1057. static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
  1058. {
  1059. u32 gerror, gerrorn, active;
  1060. struct arm_smmu_device *smmu = dev;
  1061. gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
  1062. gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
  1063. active = gerror ^ gerrorn;
  1064. if (!(active & GERROR_ERR_MASK))
  1065. return IRQ_NONE; /* No errors pending */
  1066. dev_warn(smmu->dev,
  1067. "unexpected global error reported (0x%08x), this could be serious\n",
  1068. active);
  1069. if (active & GERROR_SFM_ERR) {
  1070. dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
  1071. arm_smmu_device_disable(smmu);
  1072. }
  1073. if (active & GERROR_MSI_GERROR_ABT_ERR)
  1074. dev_warn(smmu->dev, "GERROR MSI write aborted\n");
  1075. if (active & GERROR_MSI_PRIQ_ABT_ERR)
  1076. dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
  1077. if (active & GERROR_MSI_EVTQ_ABT_ERR)
  1078. dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
  1079. if (active & GERROR_MSI_CMDQ_ABT_ERR) {
  1080. dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
  1081. arm_smmu_cmdq_sync_handler(irq, smmu->dev);
  1082. }
  1083. if (active & GERROR_PRIQ_ABT_ERR)
  1084. dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
  1085. if (active & GERROR_EVTQ_ABT_ERR)
  1086. dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
  1087. if (active & GERROR_CMDQ_ERR)
  1088. arm_smmu_cmdq_skip_err(smmu);
  1089. writel(gerror, smmu->base + ARM_SMMU_GERRORN);
  1090. return IRQ_HANDLED;
  1091. }
  1092. /* IO_PGTABLE API */
  1093. static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
  1094. {
  1095. struct arm_smmu_cmdq_ent cmd;
  1096. cmd.opcode = CMDQ_OP_CMD_SYNC;
  1097. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1098. }
  1099. static void arm_smmu_tlb_sync(void *cookie)
  1100. {
  1101. struct arm_smmu_domain *smmu_domain = cookie;
  1102. __arm_smmu_tlb_sync(smmu_domain->smmu);
  1103. }
  1104. static void arm_smmu_tlb_inv_context(void *cookie)
  1105. {
  1106. struct arm_smmu_domain *smmu_domain = cookie;
  1107. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1108. struct arm_smmu_cmdq_ent cmd;
  1109. if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1110. cmd.opcode = CMDQ_OP_TLBI_NH_ASID;
  1111. cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
  1112. cmd.tlbi.vmid = 0;
  1113. } else {
  1114. cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;
  1115. cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
  1116. }
  1117. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1118. __arm_smmu_tlb_sync(smmu);
  1119. }
  1120. static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  1121. size_t granule, bool leaf, void *cookie)
  1122. {
  1123. struct arm_smmu_domain *smmu_domain = cookie;
  1124. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1125. struct arm_smmu_cmdq_ent cmd = {
  1126. .tlbi = {
  1127. .leaf = leaf,
  1128. .addr = iova,
  1129. },
  1130. };
  1131. if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1132. cmd.opcode = CMDQ_OP_TLBI_NH_VA;
  1133. cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
  1134. } else {
  1135. cmd.opcode = CMDQ_OP_TLBI_S2_IPA;
  1136. cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
  1137. }
  1138. do {
  1139. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1140. cmd.tlbi.addr += granule;
  1141. } while (size -= granule);
  1142. }
  1143. static struct iommu_gather_ops arm_smmu_gather_ops = {
  1144. .tlb_flush_all = arm_smmu_tlb_inv_context,
  1145. .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
  1146. .tlb_sync = arm_smmu_tlb_sync,
  1147. };
  1148. /* IOMMU API */
  1149. static bool arm_smmu_capable(enum iommu_cap cap)
  1150. {
  1151. switch (cap) {
  1152. case IOMMU_CAP_CACHE_COHERENCY:
  1153. return true;
  1154. case IOMMU_CAP_INTR_REMAP:
  1155. return true; /* MSIs are just memory writes */
  1156. case IOMMU_CAP_NOEXEC:
  1157. return true;
  1158. default:
  1159. return false;
  1160. }
  1161. }
  1162. static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
  1163. {
  1164. struct arm_smmu_domain *smmu_domain;
  1165. if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
  1166. return NULL;
  1167. /*
  1168. * Allocate the domain and initialise some of its data structures.
  1169. * We can't really do anything meaningful until we've added a
  1170. * master.
  1171. */
  1172. smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
  1173. if (!smmu_domain)
  1174. return NULL;
  1175. if (type == IOMMU_DOMAIN_DMA &&
  1176. iommu_get_dma_cookie(&smmu_domain->domain)) {
  1177. kfree(smmu_domain);
  1178. return NULL;
  1179. }
  1180. mutex_init(&smmu_domain->init_mutex);
  1181. spin_lock_init(&smmu_domain->pgtbl_lock);
  1182. return &smmu_domain->domain;
  1183. }
  1184. static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
  1185. {
  1186. int idx, size = 1 << span;
  1187. do {
  1188. idx = find_first_zero_bit(map, size);
  1189. if (idx == size)
  1190. return -ENOSPC;
  1191. } while (test_and_set_bit(idx, map));
  1192. return idx;
  1193. }
  1194. static void arm_smmu_bitmap_free(unsigned long *map, int idx)
  1195. {
  1196. clear_bit(idx, map);
  1197. }
  1198. static void arm_smmu_domain_free(struct iommu_domain *domain)
  1199. {
  1200. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1201. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1202. iommu_put_dma_cookie(domain);
  1203. free_io_pgtable_ops(smmu_domain->pgtbl_ops);
  1204. /* Free the CD and ASID, if we allocated them */
  1205. if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1206. struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
  1207. if (cfg->cdptr) {
  1208. dmam_free_coherent(smmu_domain->smmu->dev,
  1209. CTXDESC_CD_DWORDS << 3,
  1210. cfg->cdptr,
  1211. cfg->cdptr_dma);
  1212. arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
  1213. }
  1214. } else {
  1215. struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
  1216. if (cfg->vmid)
  1217. arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
  1218. }
  1219. kfree(smmu_domain);
  1220. }
  1221. static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
  1222. struct io_pgtable_cfg *pgtbl_cfg)
  1223. {
  1224. int ret;
  1225. int asid;
  1226. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1227. struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
  1228. asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
  1229. if (asid < 0)
  1230. return asid;
  1231. cfg->cdptr = dmam_alloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
  1232. &cfg->cdptr_dma,
  1233. GFP_KERNEL | __GFP_ZERO);
  1234. if (!cfg->cdptr) {
  1235. dev_warn(smmu->dev, "failed to allocate context descriptor\n");
  1236. ret = -ENOMEM;
  1237. goto out_free_asid;
  1238. }
  1239. cfg->cd.asid = (u16)asid;
  1240. cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
  1241. cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
  1242. cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
  1243. return 0;
  1244. out_free_asid:
  1245. arm_smmu_bitmap_free(smmu->asid_map, asid);
  1246. return ret;
  1247. }
  1248. static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
  1249. struct io_pgtable_cfg *pgtbl_cfg)
  1250. {
  1251. int vmid;
  1252. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1253. struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
  1254. vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
  1255. if (vmid < 0)
  1256. return vmid;
  1257. cfg->vmid = (u16)vmid;
  1258. cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
  1259. cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
  1260. return 0;
  1261. }
  1262. static int arm_smmu_domain_finalise(struct iommu_domain *domain)
  1263. {
  1264. int ret;
  1265. unsigned long ias, oas;
  1266. enum io_pgtable_fmt fmt;
  1267. struct io_pgtable_cfg pgtbl_cfg;
  1268. struct io_pgtable_ops *pgtbl_ops;
  1269. int (*finalise_stage_fn)(struct arm_smmu_domain *,
  1270. struct io_pgtable_cfg *);
  1271. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1272. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1273. /* Restrict the stage to what we can actually support */
  1274. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
  1275. smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
  1276. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
  1277. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  1278. switch (smmu_domain->stage) {
  1279. case ARM_SMMU_DOMAIN_S1:
  1280. ias = VA_BITS;
  1281. oas = smmu->ias;
  1282. fmt = ARM_64_LPAE_S1;
  1283. finalise_stage_fn = arm_smmu_domain_finalise_s1;
  1284. break;
  1285. case ARM_SMMU_DOMAIN_NESTED:
  1286. case ARM_SMMU_DOMAIN_S2:
  1287. ias = smmu->ias;
  1288. oas = smmu->oas;
  1289. fmt = ARM_64_LPAE_S2;
  1290. finalise_stage_fn = arm_smmu_domain_finalise_s2;
  1291. break;
  1292. default:
  1293. return -EINVAL;
  1294. }
  1295. pgtbl_cfg = (struct io_pgtable_cfg) {
  1296. .pgsize_bitmap = smmu->pgsize_bitmap,
  1297. .ias = ias,
  1298. .oas = oas,
  1299. .tlb = &arm_smmu_gather_ops,
  1300. .iommu_dev = smmu->dev,
  1301. };
  1302. pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
  1303. if (!pgtbl_ops)
  1304. return -ENOMEM;
  1305. domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
  1306. domain->geometry.aperture_end = (1UL << ias) - 1;
  1307. domain->geometry.force_aperture = true;
  1308. ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
  1309. if (ret < 0) {
  1310. free_io_pgtable_ops(pgtbl_ops);
  1311. return ret;
  1312. }
  1313. smmu_domain->pgtbl_ops = pgtbl_ops;
  1314. return 0;
  1315. }
  1316. static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
  1317. {
  1318. __le64 *step;
  1319. struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
  1320. if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
  1321. struct arm_smmu_strtab_l1_desc *l1_desc;
  1322. int idx;
  1323. /* Two-level walk */
  1324. idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
  1325. l1_desc = &cfg->l1_desc[idx];
  1326. idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
  1327. step = &l1_desc->l2ptr[idx];
  1328. } else {
  1329. /* Simple linear lookup */
  1330. step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
  1331. }
  1332. return step;
  1333. }
  1334. static int arm_smmu_install_ste_for_dev(struct iommu_fwspec *fwspec)
  1335. {
  1336. int i, j;
  1337. struct arm_smmu_master_data *master = fwspec->iommu_priv;
  1338. struct arm_smmu_device *smmu = master->smmu;
  1339. for (i = 0; i < fwspec->num_ids; ++i) {
  1340. u32 sid = fwspec->ids[i];
  1341. __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
  1342. /* Bridged PCI devices may end up with duplicated IDs */
  1343. for (j = 0; j < i; j++)
  1344. if (fwspec->ids[j] == sid)
  1345. break;
  1346. if (j < i)
  1347. continue;
  1348. arm_smmu_write_strtab_ent(smmu, sid, step, &master->ste);
  1349. }
  1350. return 0;
  1351. }
  1352. static void arm_smmu_detach_dev(struct device *dev)
  1353. {
  1354. struct arm_smmu_master_data *master = dev->iommu_fwspec->iommu_priv;
  1355. master->ste.bypass = true;
  1356. if (arm_smmu_install_ste_for_dev(dev->iommu_fwspec) < 0)
  1357. dev_warn(dev, "failed to install bypass STE\n");
  1358. }
  1359. static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  1360. {
  1361. int ret = 0;
  1362. struct arm_smmu_device *smmu;
  1363. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1364. struct arm_smmu_master_data *master;
  1365. struct arm_smmu_strtab_ent *ste;
  1366. if (!dev->iommu_fwspec)
  1367. return -ENOENT;
  1368. master = dev->iommu_fwspec->iommu_priv;
  1369. smmu = master->smmu;
  1370. ste = &master->ste;
  1371. /* Already attached to a different domain? */
  1372. if (!ste->bypass)
  1373. arm_smmu_detach_dev(dev);
  1374. mutex_lock(&smmu_domain->init_mutex);
  1375. if (!smmu_domain->smmu) {
  1376. smmu_domain->smmu = smmu;
  1377. ret = arm_smmu_domain_finalise(domain);
  1378. if (ret) {
  1379. smmu_domain->smmu = NULL;
  1380. goto out_unlock;
  1381. }
  1382. } else if (smmu_domain->smmu != smmu) {
  1383. dev_err(dev,
  1384. "cannot attach to SMMU %s (upstream of %s)\n",
  1385. dev_name(smmu_domain->smmu->dev),
  1386. dev_name(smmu->dev));
  1387. ret = -ENXIO;
  1388. goto out_unlock;
  1389. }
  1390. ste->bypass = false;
  1391. ste->valid = true;
  1392. if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1393. ste->s1_cfg = &smmu_domain->s1_cfg;
  1394. ste->s2_cfg = NULL;
  1395. arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
  1396. } else {
  1397. ste->s1_cfg = NULL;
  1398. ste->s2_cfg = &smmu_domain->s2_cfg;
  1399. }
  1400. ret = arm_smmu_install_ste_for_dev(dev->iommu_fwspec);
  1401. if (ret < 0)
  1402. ste->valid = false;
  1403. out_unlock:
  1404. mutex_unlock(&smmu_domain->init_mutex);
  1405. return ret;
  1406. }
  1407. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  1408. phys_addr_t paddr, size_t size, int prot)
  1409. {
  1410. int ret;
  1411. unsigned long flags;
  1412. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1413. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  1414. if (!ops)
  1415. return -ENODEV;
  1416. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1417. ret = ops->map(ops, iova, paddr, size, prot);
  1418. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1419. return ret;
  1420. }
  1421. static size_t
  1422. arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
  1423. {
  1424. size_t ret;
  1425. unsigned long flags;
  1426. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1427. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  1428. if (!ops)
  1429. return 0;
  1430. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1431. ret = ops->unmap(ops, iova, size);
  1432. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1433. return ret;
  1434. }
  1435. static phys_addr_t
  1436. arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
  1437. {
  1438. phys_addr_t ret;
  1439. unsigned long flags;
  1440. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1441. struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
  1442. if (!ops)
  1443. return 0;
  1444. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1445. ret = ops->iova_to_phys(ops, iova);
  1446. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1447. return ret;
  1448. }
  1449. static struct platform_driver arm_smmu_driver;
  1450. static int arm_smmu_match_node(struct device *dev, void *data)
  1451. {
  1452. return dev->of_node == data;
  1453. }
  1454. static struct arm_smmu_device *arm_smmu_get_by_node(struct device_node *np)
  1455. {
  1456. struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
  1457. np, arm_smmu_match_node);
  1458. put_device(dev);
  1459. return dev ? dev_get_drvdata(dev) : NULL;
  1460. }
  1461. static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
  1462. {
  1463. unsigned long limit = smmu->strtab_cfg.num_l1_ents;
  1464. if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
  1465. limit *= 1UL << STRTAB_SPLIT;
  1466. return sid < limit;
  1467. }
  1468. static struct iommu_ops arm_smmu_ops;
  1469. static int arm_smmu_add_device(struct device *dev)
  1470. {
  1471. int i, ret;
  1472. struct arm_smmu_device *smmu;
  1473. struct arm_smmu_master_data *master;
  1474. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1475. struct iommu_group *group;
  1476. if (!fwspec || fwspec->ops != &arm_smmu_ops)
  1477. return -ENODEV;
  1478. /*
  1479. * We _can_ actually withstand dodgy bus code re-calling add_device()
  1480. * without an intervening remove_device()/of_xlate() sequence, but
  1481. * we're not going to do so quietly...
  1482. */
  1483. if (WARN_ON_ONCE(fwspec->iommu_priv)) {
  1484. master = fwspec->iommu_priv;
  1485. smmu = master->smmu;
  1486. } else {
  1487. smmu = arm_smmu_get_by_node(to_of_node(fwspec->iommu_fwnode));
  1488. if (!smmu)
  1489. return -ENODEV;
  1490. master = kzalloc(sizeof(*master), GFP_KERNEL);
  1491. if (!master)
  1492. return -ENOMEM;
  1493. master->smmu = smmu;
  1494. fwspec->iommu_priv = master;
  1495. }
  1496. /* Check the SIDs are in range of the SMMU and our stream table */
  1497. for (i = 0; i < fwspec->num_ids; i++) {
  1498. u32 sid = fwspec->ids[i];
  1499. if (!arm_smmu_sid_in_range(smmu, sid))
  1500. return -ERANGE;
  1501. /* Ensure l2 strtab is initialised */
  1502. if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
  1503. ret = arm_smmu_init_l2_strtab(smmu, sid);
  1504. if (ret)
  1505. return ret;
  1506. }
  1507. }
  1508. group = iommu_group_get_for_dev(dev);
  1509. if (!IS_ERR(group))
  1510. iommu_group_put(group);
  1511. return PTR_ERR_OR_ZERO(group);
  1512. }
  1513. static void arm_smmu_remove_device(struct device *dev)
  1514. {
  1515. struct iommu_fwspec *fwspec = dev->iommu_fwspec;
  1516. struct arm_smmu_master_data *master;
  1517. if (!fwspec || fwspec->ops != &arm_smmu_ops)
  1518. return;
  1519. master = fwspec->iommu_priv;
  1520. if (master && master->ste.valid)
  1521. arm_smmu_detach_dev(dev);
  1522. iommu_group_remove_device(dev);
  1523. kfree(master);
  1524. iommu_fwspec_free(dev);
  1525. }
  1526. static struct iommu_group *arm_smmu_device_group(struct device *dev)
  1527. {
  1528. struct iommu_group *group;
  1529. /*
  1530. * We don't support devices sharing stream IDs other than PCI RID
  1531. * aliases, since the necessary ID-to-device lookup becomes rather
  1532. * impractical given a potential sparse 32-bit stream ID space.
  1533. */
  1534. if (dev_is_pci(dev))
  1535. group = pci_device_group(dev);
  1536. else
  1537. group = generic_device_group(dev);
  1538. return group;
  1539. }
  1540. static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
  1541. enum iommu_attr attr, void *data)
  1542. {
  1543. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1544. switch (attr) {
  1545. case DOMAIN_ATTR_NESTING:
  1546. *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
  1547. return 0;
  1548. default:
  1549. return -ENODEV;
  1550. }
  1551. }
  1552. static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
  1553. enum iommu_attr attr, void *data)
  1554. {
  1555. int ret = 0;
  1556. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1557. mutex_lock(&smmu_domain->init_mutex);
  1558. switch (attr) {
  1559. case DOMAIN_ATTR_NESTING:
  1560. if (smmu_domain->smmu) {
  1561. ret = -EPERM;
  1562. goto out_unlock;
  1563. }
  1564. if (*(int *)data)
  1565. smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
  1566. else
  1567. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  1568. break;
  1569. default:
  1570. ret = -ENODEV;
  1571. }
  1572. out_unlock:
  1573. mutex_unlock(&smmu_domain->init_mutex);
  1574. return ret;
  1575. }
  1576. static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
  1577. {
  1578. return iommu_fwspec_add_ids(dev, args->args, 1);
  1579. }
  1580. static struct iommu_ops arm_smmu_ops = {
  1581. .capable = arm_smmu_capable,
  1582. .domain_alloc = arm_smmu_domain_alloc,
  1583. .domain_free = arm_smmu_domain_free,
  1584. .attach_dev = arm_smmu_attach_dev,
  1585. .map = arm_smmu_map,
  1586. .unmap = arm_smmu_unmap,
  1587. .map_sg = default_iommu_map_sg,
  1588. .iova_to_phys = arm_smmu_iova_to_phys,
  1589. .add_device = arm_smmu_add_device,
  1590. .remove_device = arm_smmu_remove_device,
  1591. .device_group = arm_smmu_device_group,
  1592. .domain_get_attr = arm_smmu_domain_get_attr,
  1593. .domain_set_attr = arm_smmu_domain_set_attr,
  1594. .of_xlate = arm_smmu_of_xlate,
  1595. .pgsize_bitmap = -1UL, /* Restricted during device attach */
  1596. };
  1597. /* Probing and initialisation functions */
  1598. static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
  1599. struct arm_smmu_queue *q,
  1600. unsigned long prod_off,
  1601. unsigned long cons_off,
  1602. size_t dwords)
  1603. {
  1604. size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
  1605. q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
  1606. if (!q->base) {
  1607. dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
  1608. qsz);
  1609. return -ENOMEM;
  1610. }
  1611. q->prod_reg = smmu->base + prod_off;
  1612. q->cons_reg = smmu->base + cons_off;
  1613. q->ent_dwords = dwords;
  1614. q->q_base = Q_BASE_RWA;
  1615. q->q_base |= q->base_dma & Q_BASE_ADDR_MASK << Q_BASE_ADDR_SHIFT;
  1616. q->q_base |= (q->max_n_shift & Q_BASE_LOG2SIZE_MASK)
  1617. << Q_BASE_LOG2SIZE_SHIFT;
  1618. q->prod = q->cons = 0;
  1619. return 0;
  1620. }
  1621. static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
  1622. {
  1623. int ret;
  1624. /* cmdq */
  1625. spin_lock_init(&smmu->cmdq.lock);
  1626. ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
  1627. ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
  1628. if (ret)
  1629. return ret;
  1630. /* evtq */
  1631. ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
  1632. ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
  1633. if (ret)
  1634. return ret;
  1635. /* priq */
  1636. if (!(smmu->features & ARM_SMMU_FEAT_PRI))
  1637. return 0;
  1638. return arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
  1639. ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
  1640. }
  1641. static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
  1642. {
  1643. unsigned int i;
  1644. struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
  1645. size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
  1646. void *strtab = smmu->strtab_cfg.strtab;
  1647. cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
  1648. if (!cfg->l1_desc) {
  1649. dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
  1650. return -ENOMEM;
  1651. }
  1652. for (i = 0; i < cfg->num_l1_ents; ++i) {
  1653. arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
  1654. strtab += STRTAB_L1_DESC_DWORDS << 3;
  1655. }
  1656. return 0;
  1657. }
  1658. static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
  1659. {
  1660. void *strtab;
  1661. u64 reg;
  1662. u32 size, l1size;
  1663. struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
  1664. /*
  1665. * If we can resolve everything with a single L2 table, then we
  1666. * just need a single L1 descriptor. Otherwise, calculate the L1
  1667. * size, capped to the SIDSIZE.
  1668. */
  1669. if (smmu->sid_bits < STRTAB_SPLIT) {
  1670. size = 0;
  1671. } else {
  1672. size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
  1673. size = min(size, smmu->sid_bits - STRTAB_SPLIT);
  1674. }
  1675. cfg->num_l1_ents = 1 << size;
  1676. size += STRTAB_SPLIT;
  1677. if (size < smmu->sid_bits)
  1678. dev_warn(smmu->dev,
  1679. "2-level strtab only covers %u/%u bits of SID\n",
  1680. size, smmu->sid_bits);
  1681. l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
  1682. strtab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
  1683. GFP_KERNEL | __GFP_ZERO);
  1684. if (!strtab) {
  1685. dev_err(smmu->dev,
  1686. "failed to allocate l1 stream table (%u bytes)\n",
  1687. size);
  1688. return -ENOMEM;
  1689. }
  1690. cfg->strtab = strtab;
  1691. /* Configure strtab_base_cfg for 2 levels */
  1692. reg = STRTAB_BASE_CFG_FMT_2LVL;
  1693. reg |= (size & STRTAB_BASE_CFG_LOG2SIZE_MASK)
  1694. << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
  1695. reg |= (STRTAB_SPLIT & STRTAB_BASE_CFG_SPLIT_MASK)
  1696. << STRTAB_BASE_CFG_SPLIT_SHIFT;
  1697. cfg->strtab_base_cfg = reg;
  1698. return arm_smmu_init_l1_strtab(smmu);
  1699. }
  1700. static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
  1701. {
  1702. void *strtab;
  1703. u64 reg;
  1704. u32 size;
  1705. struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
  1706. size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
  1707. strtab = dmam_alloc_coherent(smmu->dev, size, &cfg->strtab_dma,
  1708. GFP_KERNEL | __GFP_ZERO);
  1709. if (!strtab) {
  1710. dev_err(smmu->dev,
  1711. "failed to allocate linear stream table (%u bytes)\n",
  1712. size);
  1713. return -ENOMEM;
  1714. }
  1715. cfg->strtab = strtab;
  1716. cfg->num_l1_ents = 1 << smmu->sid_bits;
  1717. /* Configure strtab_base_cfg for a linear table covering all SIDs */
  1718. reg = STRTAB_BASE_CFG_FMT_LINEAR;
  1719. reg |= (smmu->sid_bits & STRTAB_BASE_CFG_LOG2SIZE_MASK)
  1720. << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
  1721. cfg->strtab_base_cfg = reg;
  1722. arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
  1723. return 0;
  1724. }
  1725. static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
  1726. {
  1727. u64 reg;
  1728. int ret;
  1729. if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
  1730. ret = arm_smmu_init_strtab_2lvl(smmu);
  1731. else
  1732. ret = arm_smmu_init_strtab_linear(smmu);
  1733. if (ret)
  1734. return ret;
  1735. /* Set the strtab base address */
  1736. reg = smmu->strtab_cfg.strtab_dma &
  1737. STRTAB_BASE_ADDR_MASK << STRTAB_BASE_ADDR_SHIFT;
  1738. reg |= STRTAB_BASE_RA;
  1739. smmu->strtab_cfg.strtab_base = reg;
  1740. /* Allocate the first VMID for stage-2 bypass STEs */
  1741. set_bit(0, smmu->vmid_map);
  1742. return 0;
  1743. }
  1744. static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
  1745. {
  1746. int ret;
  1747. ret = arm_smmu_init_queues(smmu);
  1748. if (ret)
  1749. return ret;
  1750. return arm_smmu_init_strtab(smmu);
  1751. }
  1752. static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
  1753. unsigned int reg_off, unsigned int ack_off)
  1754. {
  1755. u32 reg;
  1756. writel_relaxed(val, smmu->base + reg_off);
  1757. return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
  1758. 1, ARM_SMMU_POLL_TIMEOUT_US);
  1759. }
  1760. /* GBPA is "special" */
  1761. static int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr)
  1762. {
  1763. int ret;
  1764. u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA;
  1765. ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
  1766. 1, ARM_SMMU_POLL_TIMEOUT_US);
  1767. if (ret)
  1768. return ret;
  1769. reg &= ~clr;
  1770. reg |= set;
  1771. writel_relaxed(reg | GBPA_UPDATE, gbpa);
  1772. return readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
  1773. 1, ARM_SMMU_POLL_TIMEOUT_US);
  1774. }
  1775. static void arm_smmu_free_msis(void *data)
  1776. {
  1777. struct device *dev = data;
  1778. platform_msi_domain_free_irqs(dev);
  1779. }
  1780. static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  1781. {
  1782. phys_addr_t doorbell;
  1783. struct device *dev = msi_desc_to_dev(desc);
  1784. struct arm_smmu_device *smmu = dev_get_drvdata(dev);
  1785. phys_addr_t *cfg = arm_smmu_msi_cfg[desc->platform.msi_index];
  1786. doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
  1787. doorbell &= MSI_CFG0_ADDR_MASK << MSI_CFG0_ADDR_SHIFT;
  1788. writeq_relaxed(doorbell, smmu->base + cfg[0]);
  1789. writel_relaxed(msg->data, smmu->base + cfg[1]);
  1790. writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
  1791. }
  1792. static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
  1793. {
  1794. struct msi_desc *desc;
  1795. int ret, nvec = ARM_SMMU_MAX_MSIS;
  1796. struct device *dev = smmu->dev;
  1797. /* Clear the MSI address regs */
  1798. writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
  1799. writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
  1800. if (smmu->features & ARM_SMMU_FEAT_PRI)
  1801. writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
  1802. else
  1803. nvec--;
  1804. if (!(smmu->features & ARM_SMMU_FEAT_MSI))
  1805. return;
  1806. /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
  1807. ret = platform_msi_domain_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
  1808. if (ret) {
  1809. dev_warn(dev, "failed to allocate MSIs\n");
  1810. return;
  1811. }
  1812. for_each_msi_entry(desc, dev) {
  1813. switch (desc->platform.msi_index) {
  1814. case EVTQ_MSI_INDEX:
  1815. smmu->evtq.q.irq = desc->irq;
  1816. break;
  1817. case GERROR_MSI_INDEX:
  1818. smmu->gerr_irq = desc->irq;
  1819. break;
  1820. case PRIQ_MSI_INDEX:
  1821. smmu->priq.q.irq = desc->irq;
  1822. break;
  1823. default: /* Unknown */
  1824. continue;
  1825. }
  1826. }
  1827. /* Add callback to free MSIs on teardown */
  1828. devm_add_action(dev, arm_smmu_free_msis, dev);
  1829. }
  1830. static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
  1831. {
  1832. int ret, irq;
  1833. u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
  1834. /* Disable IRQs first */
  1835. ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
  1836. ARM_SMMU_IRQ_CTRLACK);
  1837. if (ret) {
  1838. dev_err(smmu->dev, "failed to disable irqs\n");
  1839. return ret;
  1840. }
  1841. arm_smmu_setup_msis(smmu);
  1842. /* Request interrupt lines */
  1843. irq = smmu->evtq.q.irq;
  1844. if (irq) {
  1845. ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
  1846. arm_smmu_evtq_thread,
  1847. IRQF_ONESHOT,
  1848. "arm-smmu-v3-evtq", smmu);
  1849. if (ret < 0)
  1850. dev_warn(smmu->dev, "failed to enable evtq irq\n");
  1851. }
  1852. irq = smmu->cmdq.q.irq;
  1853. if (irq) {
  1854. ret = devm_request_irq(smmu->dev, irq,
  1855. arm_smmu_cmdq_sync_handler, 0,
  1856. "arm-smmu-v3-cmdq-sync", smmu);
  1857. if (ret < 0)
  1858. dev_warn(smmu->dev, "failed to enable cmdq-sync irq\n");
  1859. }
  1860. irq = smmu->gerr_irq;
  1861. if (irq) {
  1862. ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
  1863. 0, "arm-smmu-v3-gerror", smmu);
  1864. if (ret < 0)
  1865. dev_warn(smmu->dev, "failed to enable gerror irq\n");
  1866. }
  1867. if (smmu->features & ARM_SMMU_FEAT_PRI) {
  1868. irq = smmu->priq.q.irq;
  1869. if (irq) {
  1870. ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
  1871. arm_smmu_priq_thread,
  1872. IRQF_ONESHOT,
  1873. "arm-smmu-v3-priq",
  1874. smmu);
  1875. if (ret < 0)
  1876. dev_warn(smmu->dev,
  1877. "failed to enable priq irq\n");
  1878. else
  1879. irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
  1880. }
  1881. }
  1882. /* Enable interrupt generation on the SMMU */
  1883. ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
  1884. ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
  1885. if (ret)
  1886. dev_warn(smmu->dev, "failed to enable irqs\n");
  1887. return 0;
  1888. }
  1889. static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
  1890. {
  1891. int ret;
  1892. ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
  1893. if (ret)
  1894. dev_err(smmu->dev, "failed to clear cr0\n");
  1895. return ret;
  1896. }
  1897. static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)
  1898. {
  1899. int ret;
  1900. u32 reg, enables;
  1901. struct arm_smmu_cmdq_ent cmd;
  1902. /* Clear CR0 and sync (disables SMMU and queue processing) */
  1903. reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
  1904. if (reg & CR0_SMMUEN)
  1905. dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
  1906. ret = arm_smmu_device_disable(smmu);
  1907. if (ret)
  1908. return ret;
  1909. /* CR1 (table and queue memory attributes) */
  1910. reg = (CR1_SH_ISH << CR1_TABLE_SH_SHIFT) |
  1911. (CR1_CACHE_WB << CR1_TABLE_OC_SHIFT) |
  1912. (CR1_CACHE_WB << CR1_TABLE_IC_SHIFT) |
  1913. (CR1_SH_ISH << CR1_QUEUE_SH_SHIFT) |
  1914. (CR1_CACHE_WB << CR1_QUEUE_OC_SHIFT) |
  1915. (CR1_CACHE_WB << CR1_QUEUE_IC_SHIFT);
  1916. writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
  1917. /* CR2 (random crap) */
  1918. reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
  1919. writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
  1920. /* Stream table */
  1921. writeq_relaxed(smmu->strtab_cfg.strtab_base,
  1922. smmu->base + ARM_SMMU_STRTAB_BASE);
  1923. writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
  1924. smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
  1925. /* Command queue */
  1926. writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
  1927. writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
  1928. writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
  1929. enables = CR0_CMDQEN;
  1930. ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
  1931. ARM_SMMU_CR0ACK);
  1932. if (ret) {
  1933. dev_err(smmu->dev, "failed to enable command queue\n");
  1934. return ret;
  1935. }
  1936. /* Invalidate any cached configuration */
  1937. cmd.opcode = CMDQ_OP_CFGI_ALL;
  1938. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1939. cmd.opcode = CMDQ_OP_CMD_SYNC;
  1940. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1941. /* Invalidate any stale TLB entries */
  1942. if (smmu->features & ARM_SMMU_FEAT_HYP) {
  1943. cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
  1944. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1945. }
  1946. cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
  1947. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1948. cmd.opcode = CMDQ_OP_CMD_SYNC;
  1949. arm_smmu_cmdq_issue_cmd(smmu, &cmd);
  1950. /* Event queue */
  1951. writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
  1952. writel_relaxed(smmu->evtq.q.prod, smmu->base + ARM_SMMU_EVTQ_PROD);
  1953. writel_relaxed(smmu->evtq.q.cons, smmu->base + ARM_SMMU_EVTQ_CONS);
  1954. enables |= CR0_EVTQEN;
  1955. ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
  1956. ARM_SMMU_CR0ACK);
  1957. if (ret) {
  1958. dev_err(smmu->dev, "failed to enable event queue\n");
  1959. return ret;
  1960. }
  1961. /* PRI queue */
  1962. if (smmu->features & ARM_SMMU_FEAT_PRI) {
  1963. writeq_relaxed(smmu->priq.q.q_base,
  1964. smmu->base + ARM_SMMU_PRIQ_BASE);
  1965. writel_relaxed(smmu->priq.q.prod,
  1966. smmu->base + ARM_SMMU_PRIQ_PROD);
  1967. writel_relaxed(smmu->priq.q.cons,
  1968. smmu->base + ARM_SMMU_PRIQ_CONS);
  1969. enables |= CR0_PRIQEN;
  1970. ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
  1971. ARM_SMMU_CR0ACK);
  1972. if (ret) {
  1973. dev_err(smmu->dev, "failed to enable PRI queue\n");
  1974. return ret;
  1975. }
  1976. }
  1977. ret = arm_smmu_setup_irqs(smmu);
  1978. if (ret) {
  1979. dev_err(smmu->dev, "failed to setup irqs\n");
  1980. return ret;
  1981. }
  1982. /* Enable the SMMU interface, or ensure bypass */
  1983. if (!bypass || disable_bypass) {
  1984. enables |= CR0_SMMUEN;
  1985. } else {
  1986. ret = arm_smmu_update_gbpa(smmu, 0, GBPA_ABORT);
  1987. if (ret) {
  1988. dev_err(smmu->dev, "GBPA not responding to update\n");
  1989. return ret;
  1990. }
  1991. }
  1992. ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
  1993. ARM_SMMU_CR0ACK);
  1994. if (ret) {
  1995. dev_err(smmu->dev, "failed to enable SMMU interface\n");
  1996. return ret;
  1997. }
  1998. return 0;
  1999. }
  2000. static int arm_smmu_device_probe(struct arm_smmu_device *smmu)
  2001. {
  2002. u32 reg;
  2003. bool coherent;
  2004. /* IDR0 */
  2005. reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
  2006. /* 2-level structures */
  2007. if ((reg & IDR0_ST_LVL_MASK << IDR0_ST_LVL_SHIFT) == IDR0_ST_LVL_2LVL)
  2008. smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
  2009. if (reg & IDR0_CD2L)
  2010. smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
  2011. /*
  2012. * Translation table endianness.
  2013. * We currently require the same endianness as the CPU, but this
  2014. * could be changed later by adding a new IO_PGTABLE_QUIRK.
  2015. */
  2016. switch (reg & IDR0_TTENDIAN_MASK << IDR0_TTENDIAN_SHIFT) {
  2017. case IDR0_TTENDIAN_MIXED:
  2018. smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
  2019. break;
  2020. #ifdef __BIG_ENDIAN
  2021. case IDR0_TTENDIAN_BE:
  2022. smmu->features |= ARM_SMMU_FEAT_TT_BE;
  2023. break;
  2024. #else
  2025. case IDR0_TTENDIAN_LE:
  2026. smmu->features |= ARM_SMMU_FEAT_TT_LE;
  2027. break;
  2028. #endif
  2029. default:
  2030. dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
  2031. return -ENXIO;
  2032. }
  2033. /* Boolean feature flags */
  2034. if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
  2035. smmu->features |= ARM_SMMU_FEAT_PRI;
  2036. if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
  2037. smmu->features |= ARM_SMMU_FEAT_ATS;
  2038. if (reg & IDR0_SEV)
  2039. smmu->features |= ARM_SMMU_FEAT_SEV;
  2040. if (reg & IDR0_MSI)
  2041. smmu->features |= ARM_SMMU_FEAT_MSI;
  2042. if (reg & IDR0_HYP)
  2043. smmu->features |= ARM_SMMU_FEAT_HYP;
  2044. /*
  2045. * The dma-coherent property is used in preference to the ID
  2046. * register, but warn on mismatch.
  2047. */
  2048. coherent = of_dma_is_coherent(smmu->dev->of_node);
  2049. if (coherent)
  2050. smmu->features |= ARM_SMMU_FEAT_COHERENCY;
  2051. if (!!(reg & IDR0_COHACC) != coherent)
  2052. dev_warn(smmu->dev, "IDR0.COHACC overridden by dma-coherent property (%s)\n",
  2053. coherent ? "true" : "false");
  2054. switch (reg & IDR0_STALL_MODEL_MASK << IDR0_STALL_MODEL_SHIFT) {
  2055. case IDR0_STALL_MODEL_STALL:
  2056. /* Fallthrough */
  2057. case IDR0_STALL_MODEL_FORCE:
  2058. smmu->features |= ARM_SMMU_FEAT_STALLS;
  2059. }
  2060. if (reg & IDR0_S1P)
  2061. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  2062. if (reg & IDR0_S2P)
  2063. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  2064. if (!(reg & (IDR0_S1P | IDR0_S2P))) {
  2065. dev_err(smmu->dev, "no translation support!\n");
  2066. return -ENXIO;
  2067. }
  2068. /* We only support the AArch64 table format at present */
  2069. switch (reg & IDR0_TTF_MASK << IDR0_TTF_SHIFT) {
  2070. case IDR0_TTF_AARCH32_64:
  2071. smmu->ias = 40;
  2072. /* Fallthrough */
  2073. case IDR0_TTF_AARCH64:
  2074. break;
  2075. default:
  2076. dev_err(smmu->dev, "AArch64 table format not supported!\n");
  2077. return -ENXIO;
  2078. }
  2079. /* ASID/VMID sizes */
  2080. smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
  2081. smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
  2082. /* IDR1 */
  2083. reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
  2084. if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
  2085. dev_err(smmu->dev, "embedded implementation not supported\n");
  2086. return -ENXIO;
  2087. }
  2088. /* Queue sizes, capped at 4k */
  2089. smmu->cmdq.q.max_n_shift = min((u32)CMDQ_MAX_SZ_SHIFT,
  2090. reg >> IDR1_CMDQ_SHIFT & IDR1_CMDQ_MASK);
  2091. if (!smmu->cmdq.q.max_n_shift) {
  2092. /* Odd alignment restrictions on the base, so ignore for now */
  2093. dev_err(smmu->dev, "unit-length command queue not supported\n");
  2094. return -ENXIO;
  2095. }
  2096. smmu->evtq.q.max_n_shift = min((u32)EVTQ_MAX_SZ_SHIFT,
  2097. reg >> IDR1_EVTQ_SHIFT & IDR1_EVTQ_MASK);
  2098. smmu->priq.q.max_n_shift = min((u32)PRIQ_MAX_SZ_SHIFT,
  2099. reg >> IDR1_PRIQ_SHIFT & IDR1_PRIQ_MASK);
  2100. /* SID/SSID sizes */
  2101. smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK;
  2102. smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK;
  2103. /* IDR5 */
  2104. reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
  2105. /* Maximum number of outstanding stalls */
  2106. smmu->evtq.max_stalls = reg >> IDR5_STALL_MAX_SHIFT
  2107. & IDR5_STALL_MAX_MASK;
  2108. /* Page sizes */
  2109. if (reg & IDR5_GRAN64K)
  2110. smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
  2111. if (reg & IDR5_GRAN16K)
  2112. smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
  2113. if (reg & IDR5_GRAN4K)
  2114. smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
  2115. if (arm_smmu_ops.pgsize_bitmap == -1UL)
  2116. arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
  2117. else
  2118. arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
  2119. /* Output address size */
  2120. switch (reg & IDR5_OAS_MASK << IDR5_OAS_SHIFT) {
  2121. case IDR5_OAS_32_BIT:
  2122. smmu->oas = 32;
  2123. break;
  2124. case IDR5_OAS_36_BIT:
  2125. smmu->oas = 36;
  2126. break;
  2127. case IDR5_OAS_40_BIT:
  2128. smmu->oas = 40;
  2129. break;
  2130. case IDR5_OAS_42_BIT:
  2131. smmu->oas = 42;
  2132. break;
  2133. case IDR5_OAS_44_BIT:
  2134. smmu->oas = 44;
  2135. break;
  2136. default:
  2137. dev_info(smmu->dev,
  2138. "unknown output address size. Truncating to 48-bit\n");
  2139. /* Fallthrough */
  2140. case IDR5_OAS_48_BIT:
  2141. smmu->oas = 48;
  2142. }
  2143. /* Set the DMA mask for our table walker */
  2144. if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
  2145. dev_warn(smmu->dev,
  2146. "failed to set DMA mask for table walker\n");
  2147. smmu->ias = max(smmu->ias, smmu->oas);
  2148. dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
  2149. smmu->ias, smmu->oas, smmu->features);
  2150. return 0;
  2151. }
  2152. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  2153. {
  2154. int irq, ret;
  2155. struct resource *res;
  2156. struct arm_smmu_device *smmu;
  2157. struct device *dev = &pdev->dev;
  2158. bool bypass = true;
  2159. u32 cells;
  2160. if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))
  2161. dev_err(dev, "missing #iommu-cells property\n");
  2162. else if (cells != 1)
  2163. dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);
  2164. else
  2165. bypass = false;
  2166. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  2167. if (!smmu) {
  2168. dev_err(dev, "failed to allocate arm_smmu_device\n");
  2169. return -ENOMEM;
  2170. }
  2171. smmu->dev = dev;
  2172. /* Base address */
  2173. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2174. if (resource_size(res) + 1 < SZ_128K) {
  2175. dev_err(dev, "MMIO region too small (%pr)\n", res);
  2176. return -EINVAL;
  2177. }
  2178. smmu->base = devm_ioremap_resource(dev, res);
  2179. if (IS_ERR(smmu->base))
  2180. return PTR_ERR(smmu->base);
  2181. /* Interrupt lines */
  2182. irq = platform_get_irq_byname(pdev, "eventq");
  2183. if (irq > 0)
  2184. smmu->evtq.q.irq = irq;
  2185. irq = platform_get_irq_byname(pdev, "priq");
  2186. if (irq > 0)
  2187. smmu->priq.q.irq = irq;
  2188. irq = platform_get_irq_byname(pdev, "cmdq-sync");
  2189. if (irq > 0)
  2190. smmu->cmdq.q.irq = irq;
  2191. irq = platform_get_irq_byname(pdev, "gerror");
  2192. if (irq > 0)
  2193. smmu->gerr_irq = irq;
  2194. parse_driver_options(smmu);
  2195. /* Probe the h/w */
  2196. ret = arm_smmu_device_probe(smmu);
  2197. if (ret)
  2198. return ret;
  2199. /* Initialise in-memory data structures */
  2200. ret = arm_smmu_init_structures(smmu);
  2201. if (ret)
  2202. return ret;
  2203. /* Record our private device structure */
  2204. platform_set_drvdata(pdev, smmu);
  2205. /* Reset the device */
  2206. ret = arm_smmu_device_reset(smmu, bypass);
  2207. if (ret)
  2208. return ret;
  2209. /* And we're up. Go go go! */
  2210. of_iommu_set_ops(dev->of_node, &arm_smmu_ops);
  2211. #ifdef CONFIG_PCI
  2212. if (pci_bus_type.iommu_ops != &arm_smmu_ops) {
  2213. pci_request_acs();
  2214. ret = bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
  2215. if (ret)
  2216. return ret;
  2217. }
  2218. #endif
  2219. #ifdef CONFIG_ARM_AMBA
  2220. if (amba_bustype.iommu_ops != &arm_smmu_ops) {
  2221. ret = bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  2222. if (ret)
  2223. return ret;
  2224. }
  2225. #endif
  2226. if (platform_bus_type.iommu_ops != &arm_smmu_ops) {
  2227. ret = bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  2228. if (ret)
  2229. return ret;
  2230. }
  2231. return 0;
  2232. }
  2233. static int arm_smmu_device_remove(struct platform_device *pdev)
  2234. {
  2235. struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
  2236. arm_smmu_device_disable(smmu);
  2237. return 0;
  2238. }
  2239. static struct of_device_id arm_smmu_of_match[] = {
  2240. { .compatible = "arm,smmu-v3", },
  2241. { },
  2242. };
  2243. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  2244. static struct platform_driver arm_smmu_driver = {
  2245. .driver = {
  2246. .name = "arm-smmu-v3",
  2247. .of_match_table = of_match_ptr(arm_smmu_of_match),
  2248. },
  2249. .probe = arm_smmu_device_dt_probe,
  2250. .remove = arm_smmu_device_remove,
  2251. };
  2252. static int __init arm_smmu_init(void)
  2253. {
  2254. static bool registered;
  2255. int ret = 0;
  2256. if (!registered) {
  2257. ret = platform_driver_register(&arm_smmu_driver);
  2258. registered = !ret;
  2259. }
  2260. return ret;
  2261. }
  2262. static void __exit arm_smmu_exit(void)
  2263. {
  2264. return platform_driver_unregister(&arm_smmu_driver);
  2265. }
  2266. subsys_initcall(arm_smmu_init);
  2267. module_exit(arm_smmu_exit);
  2268. static int __init arm_smmu_of_init(struct device_node *np)
  2269. {
  2270. int ret = arm_smmu_init();
  2271. if (ret)
  2272. return ret;
  2273. if (!of_platform_device_create(np, NULL, platform_bus_type.dev_root))
  2274. return -ENODEV;
  2275. return 0;
  2276. }
  2277. IOMMU_OF_DECLARE(arm_smmuv3, "arm,smmu-v3", arm_smmu_of_init);
  2278. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
  2279. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  2280. MODULE_LICENSE("GPL v2");