arm-smmu-v3.c 68 KB

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