arm-smmu.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /*
  2. * IOMMU API for ARM architected SMMU implementations.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Copyright (C) 2013 ARM Limited
  18. *
  19. * Author: Will Deacon <will.deacon@arm.com>
  20. *
  21. * This driver currently supports:
  22. * - SMMUv1 and v2 implementations
  23. * - Stream-matching and stream-indexing
  24. * - v7/v8 long-descriptor format
  25. * - Non-secure access to the SMMU
  26. * - Context fault reporting
  27. */
  28. #define pr_fmt(fmt) "arm-smmu: " fmt
  29. #include <linux/delay.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/err.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/iommu.h>
  35. #include <linux/iopoll.h>
  36. #include <linux/module.h>
  37. #include <linux/of.h>
  38. #include <linux/pci.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/amba/bus.h>
  43. #include "io-pgtable.h"
  44. /* Maximum number of stream IDs assigned to a single device */
  45. #define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
  46. /* Maximum number of context banks per SMMU */
  47. #define ARM_SMMU_MAX_CBS 128
  48. /* Maximum number of mapping groups per SMMU */
  49. #define ARM_SMMU_MAX_SMRS 128
  50. /* SMMU global address space */
  51. #define ARM_SMMU_GR0(smmu) ((smmu)->base)
  52. #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
  53. /*
  54. * SMMU global address space with conditional offset to access secure
  55. * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
  56. * nsGFSYNR0: 0x450)
  57. */
  58. #define ARM_SMMU_GR0_NS(smmu) \
  59. ((smmu)->base + \
  60. ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
  61. ? 0x400 : 0))
  62. /* Configuration registers */
  63. #define ARM_SMMU_GR0_sCR0 0x0
  64. #define sCR0_CLIENTPD (1 << 0)
  65. #define sCR0_GFRE (1 << 1)
  66. #define sCR0_GFIE (1 << 2)
  67. #define sCR0_GCFGFRE (1 << 4)
  68. #define sCR0_GCFGFIE (1 << 5)
  69. #define sCR0_USFCFG (1 << 10)
  70. #define sCR0_VMIDPNE (1 << 11)
  71. #define sCR0_PTM (1 << 12)
  72. #define sCR0_FB (1 << 13)
  73. #define sCR0_BSU_SHIFT 14
  74. #define sCR0_BSU_MASK 0x3
  75. /* Identification registers */
  76. #define ARM_SMMU_GR0_ID0 0x20
  77. #define ARM_SMMU_GR0_ID1 0x24
  78. #define ARM_SMMU_GR0_ID2 0x28
  79. #define ARM_SMMU_GR0_ID3 0x2c
  80. #define ARM_SMMU_GR0_ID4 0x30
  81. #define ARM_SMMU_GR0_ID5 0x34
  82. #define ARM_SMMU_GR0_ID6 0x38
  83. #define ARM_SMMU_GR0_ID7 0x3c
  84. #define ARM_SMMU_GR0_sGFSR 0x48
  85. #define ARM_SMMU_GR0_sGFSYNR0 0x50
  86. #define ARM_SMMU_GR0_sGFSYNR1 0x54
  87. #define ARM_SMMU_GR0_sGFSYNR2 0x58
  88. #define ID0_S1TS (1 << 30)
  89. #define ID0_S2TS (1 << 29)
  90. #define ID0_NTS (1 << 28)
  91. #define ID0_SMS (1 << 27)
  92. #define ID0_ATOSNS (1 << 26)
  93. #define ID0_CTTW (1 << 14)
  94. #define ID0_NUMIRPT_SHIFT 16
  95. #define ID0_NUMIRPT_MASK 0xff
  96. #define ID0_NUMSIDB_SHIFT 9
  97. #define ID0_NUMSIDB_MASK 0xf
  98. #define ID0_NUMSMRG_SHIFT 0
  99. #define ID0_NUMSMRG_MASK 0xff
  100. #define ID1_PAGESIZE (1 << 31)
  101. #define ID1_NUMPAGENDXB_SHIFT 28
  102. #define ID1_NUMPAGENDXB_MASK 7
  103. #define ID1_NUMS2CB_SHIFT 16
  104. #define ID1_NUMS2CB_MASK 0xff
  105. #define ID1_NUMCB_SHIFT 0
  106. #define ID1_NUMCB_MASK 0xff
  107. #define ID2_OAS_SHIFT 4
  108. #define ID2_OAS_MASK 0xf
  109. #define ID2_IAS_SHIFT 0
  110. #define ID2_IAS_MASK 0xf
  111. #define ID2_UBS_SHIFT 8
  112. #define ID2_UBS_MASK 0xf
  113. #define ID2_PTFS_4K (1 << 12)
  114. #define ID2_PTFS_16K (1 << 13)
  115. #define ID2_PTFS_64K (1 << 14)
  116. /* Global TLB invalidation */
  117. #define ARM_SMMU_GR0_TLBIVMID 0x64
  118. #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
  119. #define ARM_SMMU_GR0_TLBIALLH 0x6c
  120. #define ARM_SMMU_GR0_sTLBGSYNC 0x70
  121. #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
  122. #define sTLBGSTATUS_GSACTIVE (1 << 0)
  123. #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
  124. /* Stream mapping registers */
  125. #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
  126. #define SMR_VALID (1 << 31)
  127. #define SMR_MASK_SHIFT 16
  128. #define SMR_MASK_MASK 0x7fff
  129. #define SMR_ID_SHIFT 0
  130. #define SMR_ID_MASK 0x7fff
  131. #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
  132. #define S2CR_CBNDX_SHIFT 0
  133. #define S2CR_CBNDX_MASK 0xff
  134. #define S2CR_TYPE_SHIFT 16
  135. #define S2CR_TYPE_MASK 0x3
  136. #define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
  137. #define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
  138. #define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
  139. /* Context bank attribute registers */
  140. #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
  141. #define CBAR_VMID_SHIFT 0
  142. #define CBAR_VMID_MASK 0xff
  143. #define CBAR_S1_BPSHCFG_SHIFT 8
  144. #define CBAR_S1_BPSHCFG_MASK 3
  145. #define CBAR_S1_BPSHCFG_NSH 3
  146. #define CBAR_S1_MEMATTR_SHIFT 12
  147. #define CBAR_S1_MEMATTR_MASK 0xf
  148. #define CBAR_S1_MEMATTR_WB 0xf
  149. #define CBAR_TYPE_SHIFT 16
  150. #define CBAR_TYPE_MASK 0x3
  151. #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
  152. #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
  153. #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
  154. #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
  155. #define CBAR_IRPTNDX_SHIFT 24
  156. #define CBAR_IRPTNDX_MASK 0xff
  157. #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
  158. #define CBA2R_RW64_32BIT (0 << 0)
  159. #define CBA2R_RW64_64BIT (1 << 0)
  160. /* Translation context bank */
  161. #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
  162. #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
  163. #define ARM_SMMU_CB_SCTLR 0x0
  164. #define ARM_SMMU_CB_RESUME 0x8
  165. #define ARM_SMMU_CB_TTBCR2 0x10
  166. #define ARM_SMMU_CB_TTBR0_LO 0x20
  167. #define ARM_SMMU_CB_TTBR0_HI 0x24
  168. #define ARM_SMMU_CB_TTBR1_LO 0x28
  169. #define ARM_SMMU_CB_TTBR1_HI 0x2c
  170. #define ARM_SMMU_CB_TTBCR 0x30
  171. #define ARM_SMMU_CB_S1_MAIR0 0x38
  172. #define ARM_SMMU_CB_S1_MAIR1 0x3c
  173. #define ARM_SMMU_CB_PAR_LO 0x50
  174. #define ARM_SMMU_CB_PAR_HI 0x54
  175. #define ARM_SMMU_CB_FSR 0x58
  176. #define ARM_SMMU_CB_FAR_LO 0x60
  177. #define ARM_SMMU_CB_FAR_HI 0x64
  178. #define ARM_SMMU_CB_FSYNR0 0x68
  179. #define ARM_SMMU_CB_S1_TLBIVA 0x600
  180. #define ARM_SMMU_CB_S1_TLBIASID 0x610
  181. #define ARM_SMMU_CB_S1_TLBIVAL 0x620
  182. #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630
  183. #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638
  184. #define ARM_SMMU_CB_ATS1PR_LO 0x800
  185. #define ARM_SMMU_CB_ATS1PR_HI 0x804
  186. #define ARM_SMMU_CB_ATSR 0x8f0
  187. #define SCTLR_S1_ASIDPNE (1 << 12)
  188. #define SCTLR_CFCFG (1 << 7)
  189. #define SCTLR_CFIE (1 << 6)
  190. #define SCTLR_CFRE (1 << 5)
  191. #define SCTLR_E (1 << 4)
  192. #define SCTLR_AFE (1 << 2)
  193. #define SCTLR_TRE (1 << 1)
  194. #define SCTLR_M (1 << 0)
  195. #define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
  196. #define CB_PAR_F (1 << 0)
  197. #define ATSR_ACTIVE (1 << 0)
  198. #define RESUME_RETRY (0 << 0)
  199. #define RESUME_TERMINATE (1 << 0)
  200. #define TTBCR2_SEP_SHIFT 15
  201. #define TTBCR2_SEP_MASK 0x7
  202. #define TTBCR2_ADDR_32 0
  203. #define TTBCR2_ADDR_36 1
  204. #define TTBCR2_ADDR_40 2
  205. #define TTBCR2_ADDR_42 3
  206. #define TTBCR2_ADDR_44 4
  207. #define TTBCR2_ADDR_48 5
  208. #define TTBRn_HI_ASID_SHIFT 16
  209. #define FSR_MULTI (1 << 31)
  210. #define FSR_SS (1 << 30)
  211. #define FSR_UUT (1 << 8)
  212. #define FSR_ASF (1 << 7)
  213. #define FSR_TLBLKF (1 << 6)
  214. #define FSR_TLBMCF (1 << 5)
  215. #define FSR_EF (1 << 4)
  216. #define FSR_PF (1 << 3)
  217. #define FSR_AFF (1 << 2)
  218. #define FSR_TF (1 << 1)
  219. #define FSR_IGN (FSR_AFF | FSR_ASF | \
  220. FSR_TLBMCF | FSR_TLBLKF)
  221. #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
  222. FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
  223. #define FSYNR0_WNR (1 << 4)
  224. static int force_stage;
  225. module_param_named(force_stage, force_stage, int, S_IRUGO | S_IWUSR);
  226. MODULE_PARM_DESC(force_stage,
  227. "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
  228. enum arm_smmu_arch_version {
  229. ARM_SMMU_V1 = 1,
  230. ARM_SMMU_V2,
  231. };
  232. struct arm_smmu_smr {
  233. u8 idx;
  234. u16 mask;
  235. u16 id;
  236. };
  237. struct arm_smmu_master_cfg {
  238. int num_streamids;
  239. u16 streamids[MAX_MASTER_STREAMIDS];
  240. struct arm_smmu_smr *smrs;
  241. };
  242. struct arm_smmu_master {
  243. struct device_node *of_node;
  244. struct rb_node node;
  245. struct arm_smmu_master_cfg cfg;
  246. };
  247. struct arm_smmu_device {
  248. struct device *dev;
  249. void __iomem *base;
  250. unsigned long size;
  251. unsigned long pgshift;
  252. #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
  253. #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
  254. #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
  255. #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
  256. #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
  257. #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
  258. u32 features;
  259. #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
  260. u32 options;
  261. enum arm_smmu_arch_version version;
  262. u32 num_context_banks;
  263. u32 num_s2_context_banks;
  264. DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
  265. atomic_t irptndx;
  266. u32 num_mapping_groups;
  267. DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
  268. unsigned long va_size;
  269. unsigned long ipa_size;
  270. unsigned long pa_size;
  271. u32 num_global_irqs;
  272. u32 num_context_irqs;
  273. unsigned int *irqs;
  274. struct list_head list;
  275. struct rb_root masters;
  276. };
  277. struct arm_smmu_cfg {
  278. u8 cbndx;
  279. u8 irptndx;
  280. u32 cbar;
  281. };
  282. #define INVALID_IRPTNDX 0xff
  283. #define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
  284. #define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
  285. enum arm_smmu_domain_stage {
  286. ARM_SMMU_DOMAIN_S1 = 0,
  287. ARM_SMMU_DOMAIN_S2,
  288. ARM_SMMU_DOMAIN_NESTED,
  289. };
  290. struct arm_smmu_domain {
  291. struct arm_smmu_device *smmu;
  292. struct io_pgtable_ops *pgtbl_ops;
  293. spinlock_t pgtbl_lock;
  294. struct arm_smmu_cfg cfg;
  295. enum arm_smmu_domain_stage stage;
  296. struct mutex init_mutex; /* Protects smmu pointer */
  297. struct iommu_domain domain;
  298. };
  299. static struct iommu_ops arm_smmu_ops;
  300. static DEFINE_SPINLOCK(arm_smmu_devices_lock);
  301. static LIST_HEAD(arm_smmu_devices);
  302. struct arm_smmu_option_prop {
  303. u32 opt;
  304. const char *prop;
  305. };
  306. static struct arm_smmu_option_prop arm_smmu_options[] = {
  307. { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
  308. { 0, NULL},
  309. };
  310. static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
  311. {
  312. return container_of(dom, struct arm_smmu_domain, domain);
  313. }
  314. static void parse_driver_options(struct arm_smmu_device *smmu)
  315. {
  316. int i = 0;
  317. do {
  318. if (of_property_read_bool(smmu->dev->of_node,
  319. arm_smmu_options[i].prop)) {
  320. smmu->options |= arm_smmu_options[i].opt;
  321. dev_notice(smmu->dev, "option %s\n",
  322. arm_smmu_options[i].prop);
  323. }
  324. } while (arm_smmu_options[++i].opt);
  325. }
  326. static struct device_node *dev_get_dev_node(struct device *dev)
  327. {
  328. if (dev_is_pci(dev)) {
  329. struct pci_bus *bus = to_pci_dev(dev)->bus;
  330. while (!pci_is_root_bus(bus))
  331. bus = bus->parent;
  332. return bus->bridge->parent->of_node;
  333. }
  334. return dev->of_node;
  335. }
  336. static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
  337. struct device_node *dev_node)
  338. {
  339. struct rb_node *node = smmu->masters.rb_node;
  340. while (node) {
  341. struct arm_smmu_master *master;
  342. master = container_of(node, struct arm_smmu_master, node);
  343. if (dev_node < master->of_node)
  344. node = node->rb_left;
  345. else if (dev_node > master->of_node)
  346. node = node->rb_right;
  347. else
  348. return master;
  349. }
  350. return NULL;
  351. }
  352. static struct arm_smmu_master_cfg *
  353. find_smmu_master_cfg(struct device *dev)
  354. {
  355. struct arm_smmu_master_cfg *cfg = NULL;
  356. struct iommu_group *group = iommu_group_get(dev);
  357. if (group) {
  358. cfg = iommu_group_get_iommudata(group);
  359. iommu_group_put(group);
  360. }
  361. return cfg;
  362. }
  363. static int insert_smmu_master(struct arm_smmu_device *smmu,
  364. struct arm_smmu_master *master)
  365. {
  366. struct rb_node **new, *parent;
  367. new = &smmu->masters.rb_node;
  368. parent = NULL;
  369. while (*new) {
  370. struct arm_smmu_master *this
  371. = container_of(*new, struct arm_smmu_master, node);
  372. parent = *new;
  373. if (master->of_node < this->of_node)
  374. new = &((*new)->rb_left);
  375. else if (master->of_node > this->of_node)
  376. new = &((*new)->rb_right);
  377. else
  378. return -EEXIST;
  379. }
  380. rb_link_node(&master->node, parent, new);
  381. rb_insert_color(&master->node, &smmu->masters);
  382. return 0;
  383. }
  384. static int register_smmu_master(struct arm_smmu_device *smmu,
  385. struct device *dev,
  386. struct of_phandle_args *masterspec)
  387. {
  388. int i;
  389. struct arm_smmu_master *master;
  390. master = find_smmu_master(smmu, masterspec->np);
  391. if (master) {
  392. dev_err(dev,
  393. "rejecting multiple registrations for master device %s\n",
  394. masterspec->np->name);
  395. return -EBUSY;
  396. }
  397. if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
  398. dev_err(dev,
  399. "reached maximum number (%d) of stream IDs for master device %s\n",
  400. MAX_MASTER_STREAMIDS, masterspec->np->name);
  401. return -ENOSPC;
  402. }
  403. master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
  404. if (!master)
  405. return -ENOMEM;
  406. master->of_node = masterspec->np;
  407. master->cfg.num_streamids = masterspec->args_count;
  408. for (i = 0; i < master->cfg.num_streamids; ++i) {
  409. u16 streamid = masterspec->args[i];
  410. if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
  411. (streamid >= smmu->num_mapping_groups)) {
  412. dev_err(dev,
  413. "stream ID for master device %s greater than maximum allowed (%d)\n",
  414. masterspec->np->name, smmu->num_mapping_groups);
  415. return -ERANGE;
  416. }
  417. master->cfg.streamids[i] = streamid;
  418. }
  419. return insert_smmu_master(smmu, master);
  420. }
  421. static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
  422. {
  423. struct arm_smmu_device *smmu;
  424. struct arm_smmu_master *master = NULL;
  425. struct device_node *dev_node = dev_get_dev_node(dev);
  426. spin_lock(&arm_smmu_devices_lock);
  427. list_for_each_entry(smmu, &arm_smmu_devices, list) {
  428. master = find_smmu_master(smmu, dev_node);
  429. if (master)
  430. break;
  431. }
  432. spin_unlock(&arm_smmu_devices_lock);
  433. return master ? smmu : NULL;
  434. }
  435. static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
  436. {
  437. int idx;
  438. do {
  439. idx = find_next_zero_bit(map, end, start);
  440. if (idx == end)
  441. return -ENOSPC;
  442. } while (test_and_set_bit(idx, map));
  443. return idx;
  444. }
  445. static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
  446. {
  447. clear_bit(idx, map);
  448. }
  449. /* Wait for any pending TLB invalidations to complete */
  450. static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
  451. {
  452. int count = 0;
  453. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  454. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
  455. while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
  456. & sTLBGSTATUS_GSACTIVE) {
  457. cpu_relax();
  458. if (++count == TLB_LOOP_TIMEOUT) {
  459. dev_err_ratelimited(smmu->dev,
  460. "TLB sync timed out -- SMMU may be deadlocked\n");
  461. return;
  462. }
  463. udelay(1);
  464. }
  465. }
  466. static void arm_smmu_tlb_sync(void *cookie)
  467. {
  468. struct arm_smmu_domain *smmu_domain = cookie;
  469. __arm_smmu_tlb_sync(smmu_domain->smmu);
  470. }
  471. static void arm_smmu_tlb_inv_context(void *cookie)
  472. {
  473. struct arm_smmu_domain *smmu_domain = cookie;
  474. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  475. struct arm_smmu_device *smmu = smmu_domain->smmu;
  476. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  477. void __iomem *base;
  478. if (stage1) {
  479. base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  480. writel_relaxed(ARM_SMMU_CB_ASID(cfg),
  481. base + ARM_SMMU_CB_S1_TLBIASID);
  482. } else {
  483. base = ARM_SMMU_GR0(smmu);
  484. writel_relaxed(ARM_SMMU_CB_VMID(cfg),
  485. base + ARM_SMMU_GR0_TLBIVMID);
  486. }
  487. __arm_smmu_tlb_sync(smmu);
  488. }
  489. static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
  490. bool leaf, void *cookie)
  491. {
  492. struct arm_smmu_domain *smmu_domain = cookie;
  493. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  494. struct arm_smmu_device *smmu = smmu_domain->smmu;
  495. bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  496. void __iomem *reg;
  497. if (stage1) {
  498. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  499. reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
  500. if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) {
  501. iova &= ~12UL;
  502. iova |= ARM_SMMU_CB_ASID(cfg);
  503. writel_relaxed(iova, reg);
  504. #ifdef CONFIG_64BIT
  505. } else {
  506. iova >>= 12;
  507. iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48;
  508. writeq_relaxed(iova, reg);
  509. #endif
  510. }
  511. #ifdef CONFIG_64BIT
  512. } else if (smmu->version == ARM_SMMU_V2) {
  513. reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  514. reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
  515. ARM_SMMU_CB_S2_TLBIIPAS2;
  516. writeq_relaxed(iova >> 12, reg);
  517. #endif
  518. } else {
  519. reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID;
  520. writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg);
  521. }
  522. }
  523. static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)
  524. {
  525. struct arm_smmu_domain *smmu_domain = cookie;
  526. struct arm_smmu_device *smmu = smmu_domain->smmu;
  527. unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
  528. /* Ensure new page tables are visible to the hardware walker */
  529. if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
  530. dsb(ishst);
  531. } else {
  532. /*
  533. * If the SMMU can't walk tables in the CPU caches, treat them
  534. * like non-coherent DMA since we need to flush the new entries
  535. * all the way out to memory. There's no possibility of
  536. * recursion here as the SMMU table walker will not be wired
  537. * through another SMMU.
  538. */
  539. dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
  540. DMA_TO_DEVICE);
  541. }
  542. }
  543. static struct iommu_gather_ops arm_smmu_gather_ops = {
  544. .tlb_flush_all = arm_smmu_tlb_inv_context,
  545. .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
  546. .tlb_sync = arm_smmu_tlb_sync,
  547. .flush_pgtable = arm_smmu_flush_pgtable,
  548. };
  549. static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
  550. {
  551. int flags, ret;
  552. u32 fsr, far, fsynr, resume;
  553. unsigned long iova;
  554. struct iommu_domain *domain = dev;
  555. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  556. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  557. struct arm_smmu_device *smmu = smmu_domain->smmu;
  558. void __iomem *cb_base;
  559. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  560. fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
  561. if (!(fsr & FSR_FAULT))
  562. return IRQ_NONE;
  563. if (fsr & FSR_IGN)
  564. dev_err_ratelimited(smmu->dev,
  565. "Unexpected context fault (fsr 0x%x)\n",
  566. fsr);
  567. fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
  568. flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
  569. far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
  570. iova = far;
  571. #ifdef CONFIG_64BIT
  572. far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
  573. iova |= ((unsigned long)far << 32);
  574. #endif
  575. if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
  576. ret = IRQ_HANDLED;
  577. resume = RESUME_RETRY;
  578. } else {
  579. dev_err_ratelimited(smmu->dev,
  580. "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
  581. iova, fsynr, cfg->cbndx);
  582. ret = IRQ_NONE;
  583. resume = RESUME_TERMINATE;
  584. }
  585. /* Clear the faulting FSR */
  586. writel(fsr, cb_base + ARM_SMMU_CB_FSR);
  587. /* Retry or terminate any stalled transactions */
  588. if (fsr & FSR_SS)
  589. writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
  590. return ret;
  591. }
  592. static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
  593. {
  594. u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
  595. struct arm_smmu_device *smmu = dev;
  596. void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
  597. gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
  598. gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
  599. gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
  600. gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
  601. if (!gfsr)
  602. return IRQ_NONE;
  603. dev_err_ratelimited(smmu->dev,
  604. "Unexpected global fault, this could be serious\n");
  605. dev_err_ratelimited(smmu->dev,
  606. "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
  607. gfsr, gfsynr0, gfsynr1, gfsynr2);
  608. writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
  609. return IRQ_HANDLED;
  610. }
  611. static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
  612. struct io_pgtable_cfg *pgtbl_cfg)
  613. {
  614. u32 reg;
  615. bool stage1;
  616. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  617. struct arm_smmu_device *smmu = smmu_domain->smmu;
  618. void __iomem *cb_base, *gr0_base, *gr1_base;
  619. gr0_base = ARM_SMMU_GR0(smmu);
  620. gr1_base = ARM_SMMU_GR1(smmu);
  621. stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
  622. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  623. if (smmu->version > ARM_SMMU_V1) {
  624. /*
  625. * CBA2R.
  626. * *Must* be initialised before CBAR thanks to VMID16
  627. * architectural oversight affected some implementations.
  628. */
  629. #ifdef CONFIG_64BIT
  630. reg = CBA2R_RW64_64BIT;
  631. #else
  632. reg = CBA2R_RW64_32BIT;
  633. #endif
  634. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
  635. }
  636. /* CBAR */
  637. reg = cfg->cbar;
  638. if (smmu->version == ARM_SMMU_V1)
  639. reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
  640. /*
  641. * Use the weakest shareability/memory types, so they are
  642. * overridden by the ttbcr/pte.
  643. */
  644. if (stage1) {
  645. reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
  646. (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
  647. } else {
  648. reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
  649. }
  650. writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
  651. /* TTBRs */
  652. if (stage1) {
  653. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
  654. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
  655. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32;
  656. reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
  657. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
  658. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
  659. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO);
  660. reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32;
  661. reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
  662. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI);
  663. } else {
  664. reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
  665. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
  666. reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32;
  667. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
  668. }
  669. /* TTBCR */
  670. if (stage1) {
  671. reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
  672. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
  673. if (smmu->version > ARM_SMMU_V1) {
  674. reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
  675. switch (smmu->va_size) {
  676. case 32:
  677. reg |= (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
  678. break;
  679. case 36:
  680. reg |= (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
  681. break;
  682. case 40:
  683. reg |= (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
  684. break;
  685. case 42:
  686. reg |= (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
  687. break;
  688. case 44:
  689. reg |= (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
  690. break;
  691. case 48:
  692. reg |= (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
  693. break;
  694. }
  695. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
  696. }
  697. } else {
  698. reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
  699. writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
  700. }
  701. /* MAIRs (stage-1 only) */
  702. if (stage1) {
  703. reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
  704. writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
  705. reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
  706. writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1);
  707. }
  708. /* SCTLR */
  709. reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
  710. if (stage1)
  711. reg |= SCTLR_S1_ASIDPNE;
  712. #ifdef __BIG_ENDIAN
  713. reg |= SCTLR_E;
  714. #endif
  715. writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
  716. }
  717. static int arm_smmu_init_domain_context(struct iommu_domain *domain,
  718. struct arm_smmu_device *smmu)
  719. {
  720. int irq, start, ret = 0;
  721. unsigned long ias, oas;
  722. struct io_pgtable_ops *pgtbl_ops;
  723. struct io_pgtable_cfg pgtbl_cfg;
  724. enum io_pgtable_fmt fmt;
  725. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  726. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  727. mutex_lock(&smmu_domain->init_mutex);
  728. if (smmu_domain->smmu)
  729. goto out_unlock;
  730. /*
  731. * Mapping the requested stage onto what we support is surprisingly
  732. * complicated, mainly because the spec allows S1+S2 SMMUs without
  733. * support for nested translation. That means we end up with the
  734. * following table:
  735. *
  736. * Requested Supported Actual
  737. * S1 N S1
  738. * S1 S1+S2 S1
  739. * S1 S2 S2
  740. * S1 S1 S1
  741. * N N N
  742. * N S1+S2 S2
  743. * N S2 S2
  744. * N S1 S1
  745. *
  746. * Note that you can't actually request stage-2 mappings.
  747. */
  748. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
  749. smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
  750. if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
  751. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  752. switch (smmu_domain->stage) {
  753. case ARM_SMMU_DOMAIN_S1:
  754. cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
  755. start = smmu->num_s2_context_banks;
  756. ias = smmu->va_size;
  757. oas = smmu->ipa_size;
  758. if (IS_ENABLED(CONFIG_64BIT))
  759. fmt = ARM_64_LPAE_S1;
  760. else
  761. fmt = ARM_32_LPAE_S1;
  762. break;
  763. case ARM_SMMU_DOMAIN_NESTED:
  764. /*
  765. * We will likely want to change this if/when KVM gets
  766. * involved.
  767. */
  768. case ARM_SMMU_DOMAIN_S2:
  769. cfg->cbar = CBAR_TYPE_S2_TRANS;
  770. start = 0;
  771. ias = smmu->ipa_size;
  772. oas = smmu->pa_size;
  773. if (IS_ENABLED(CONFIG_64BIT))
  774. fmt = ARM_64_LPAE_S2;
  775. else
  776. fmt = ARM_32_LPAE_S2;
  777. break;
  778. default:
  779. ret = -EINVAL;
  780. goto out_unlock;
  781. }
  782. ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
  783. smmu->num_context_banks);
  784. if (IS_ERR_VALUE(ret))
  785. goto out_unlock;
  786. cfg->cbndx = ret;
  787. if (smmu->version == ARM_SMMU_V1) {
  788. cfg->irptndx = atomic_inc_return(&smmu->irptndx);
  789. cfg->irptndx %= smmu->num_context_irqs;
  790. } else {
  791. cfg->irptndx = cfg->cbndx;
  792. }
  793. pgtbl_cfg = (struct io_pgtable_cfg) {
  794. .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
  795. .ias = ias,
  796. .oas = oas,
  797. .tlb = &arm_smmu_gather_ops,
  798. };
  799. smmu_domain->smmu = smmu;
  800. pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
  801. if (!pgtbl_ops) {
  802. ret = -ENOMEM;
  803. goto out_clear_smmu;
  804. }
  805. /* Update our support page sizes to reflect the page table format */
  806. arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
  807. /* Initialise the context bank with our page table cfg */
  808. arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
  809. /*
  810. * Request context fault interrupt. Do this last to avoid the
  811. * handler seeing a half-initialised domain state.
  812. */
  813. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  814. ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
  815. "arm-smmu-context-fault", domain);
  816. if (IS_ERR_VALUE(ret)) {
  817. dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
  818. cfg->irptndx, irq);
  819. cfg->irptndx = INVALID_IRPTNDX;
  820. }
  821. mutex_unlock(&smmu_domain->init_mutex);
  822. /* Publish page table ops for map/unmap */
  823. smmu_domain->pgtbl_ops = pgtbl_ops;
  824. return 0;
  825. out_clear_smmu:
  826. smmu_domain->smmu = NULL;
  827. out_unlock:
  828. mutex_unlock(&smmu_domain->init_mutex);
  829. return ret;
  830. }
  831. static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
  832. {
  833. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  834. struct arm_smmu_device *smmu = smmu_domain->smmu;
  835. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  836. void __iomem *cb_base;
  837. int irq;
  838. if (!smmu)
  839. return;
  840. /*
  841. * Disable the context bank and free the page tables before freeing
  842. * it.
  843. */
  844. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  845. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  846. if (cfg->irptndx != INVALID_IRPTNDX) {
  847. irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
  848. free_irq(irq, domain);
  849. }
  850. if (smmu_domain->pgtbl_ops)
  851. free_io_pgtable_ops(smmu_domain->pgtbl_ops);
  852. __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
  853. }
  854. static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
  855. {
  856. struct arm_smmu_domain *smmu_domain;
  857. if (type != IOMMU_DOMAIN_UNMANAGED)
  858. return NULL;
  859. /*
  860. * Allocate the domain and initialise some of its data structures.
  861. * We can't really do anything meaningful until we've added a
  862. * master.
  863. */
  864. smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
  865. if (!smmu_domain)
  866. return NULL;
  867. mutex_init(&smmu_domain->init_mutex);
  868. spin_lock_init(&smmu_domain->pgtbl_lock);
  869. return &smmu_domain->domain;
  870. }
  871. static void arm_smmu_domain_free(struct iommu_domain *domain)
  872. {
  873. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  874. /*
  875. * Free the domain resources. We assume that all devices have
  876. * already been detached.
  877. */
  878. arm_smmu_destroy_domain_context(domain);
  879. kfree(smmu_domain);
  880. }
  881. static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
  882. struct arm_smmu_master_cfg *cfg)
  883. {
  884. int i;
  885. struct arm_smmu_smr *smrs;
  886. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  887. if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
  888. return 0;
  889. if (cfg->smrs)
  890. return -EEXIST;
  891. smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
  892. if (!smrs) {
  893. dev_err(smmu->dev, "failed to allocate %d SMRs\n",
  894. cfg->num_streamids);
  895. return -ENOMEM;
  896. }
  897. /* Allocate the SMRs on the SMMU */
  898. for (i = 0; i < cfg->num_streamids; ++i) {
  899. int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
  900. smmu->num_mapping_groups);
  901. if (IS_ERR_VALUE(idx)) {
  902. dev_err(smmu->dev, "failed to allocate free SMR\n");
  903. goto err_free_smrs;
  904. }
  905. smrs[i] = (struct arm_smmu_smr) {
  906. .idx = idx,
  907. .mask = 0, /* We don't currently share SMRs */
  908. .id = cfg->streamids[i],
  909. };
  910. }
  911. /* It worked! Now, poke the actual hardware */
  912. for (i = 0; i < cfg->num_streamids; ++i) {
  913. u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
  914. smrs[i].mask << SMR_MASK_SHIFT;
  915. writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
  916. }
  917. cfg->smrs = smrs;
  918. return 0;
  919. err_free_smrs:
  920. while (--i >= 0)
  921. __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
  922. kfree(smrs);
  923. return -ENOSPC;
  924. }
  925. static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
  926. struct arm_smmu_master_cfg *cfg)
  927. {
  928. int i;
  929. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  930. struct arm_smmu_smr *smrs = cfg->smrs;
  931. if (!smrs)
  932. return;
  933. /* Invalidate the SMRs before freeing back to the allocator */
  934. for (i = 0; i < cfg->num_streamids; ++i) {
  935. u8 idx = smrs[i].idx;
  936. writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
  937. __arm_smmu_free_bitmap(smmu->smr_map, idx);
  938. }
  939. cfg->smrs = NULL;
  940. kfree(smrs);
  941. }
  942. static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
  943. struct arm_smmu_master_cfg *cfg)
  944. {
  945. int i, ret;
  946. struct arm_smmu_device *smmu = smmu_domain->smmu;
  947. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  948. /* Devices in an IOMMU group may already be configured */
  949. ret = arm_smmu_master_configure_smrs(smmu, cfg);
  950. if (ret)
  951. return ret == -EEXIST ? 0 : ret;
  952. for (i = 0; i < cfg->num_streamids; ++i) {
  953. u32 idx, s2cr;
  954. idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
  955. s2cr = S2CR_TYPE_TRANS |
  956. (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
  957. writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
  958. }
  959. return 0;
  960. }
  961. static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
  962. struct arm_smmu_master_cfg *cfg)
  963. {
  964. int i;
  965. struct arm_smmu_device *smmu = smmu_domain->smmu;
  966. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  967. /* An IOMMU group is torn down by the first device to be removed */
  968. if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
  969. return;
  970. /*
  971. * We *must* clear the S2CR first, because freeing the SMR means
  972. * that it can be re-allocated immediately.
  973. */
  974. for (i = 0; i < cfg->num_streamids; ++i) {
  975. u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
  976. writel_relaxed(S2CR_TYPE_BYPASS,
  977. gr0_base + ARM_SMMU_GR0_S2CR(idx));
  978. }
  979. arm_smmu_master_free_smrs(smmu, cfg);
  980. }
  981. static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
  982. {
  983. int ret;
  984. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  985. struct arm_smmu_device *smmu;
  986. struct arm_smmu_master_cfg *cfg;
  987. smmu = find_smmu_for_device(dev);
  988. if (!smmu) {
  989. dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
  990. return -ENXIO;
  991. }
  992. if (dev->archdata.iommu) {
  993. dev_err(dev, "already attached to IOMMU domain\n");
  994. return -EEXIST;
  995. }
  996. /* Ensure that the domain is finalised */
  997. ret = arm_smmu_init_domain_context(domain, smmu);
  998. if (IS_ERR_VALUE(ret))
  999. return ret;
  1000. /*
  1001. * Sanity check the domain. We don't support domains across
  1002. * different SMMUs.
  1003. */
  1004. if (smmu_domain->smmu != smmu) {
  1005. dev_err(dev,
  1006. "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
  1007. dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
  1008. return -EINVAL;
  1009. }
  1010. /* Looks ok, so add the device to the domain */
  1011. cfg = find_smmu_master_cfg(dev);
  1012. if (!cfg)
  1013. return -ENODEV;
  1014. ret = arm_smmu_domain_add_master(smmu_domain, cfg);
  1015. if (!ret)
  1016. dev->archdata.iommu = domain;
  1017. return ret;
  1018. }
  1019. static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
  1020. {
  1021. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1022. struct arm_smmu_master_cfg *cfg;
  1023. cfg = find_smmu_master_cfg(dev);
  1024. if (!cfg)
  1025. return;
  1026. dev->archdata.iommu = NULL;
  1027. arm_smmu_domain_remove_master(smmu_domain, cfg);
  1028. }
  1029. static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
  1030. phys_addr_t paddr, size_t size, int prot)
  1031. {
  1032. int ret;
  1033. unsigned long flags;
  1034. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1035. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1036. if (!ops)
  1037. return -ENODEV;
  1038. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1039. ret = ops->map(ops, iova, paddr, size, prot);
  1040. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1041. return ret;
  1042. }
  1043. static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  1044. size_t size)
  1045. {
  1046. size_t ret;
  1047. unsigned long flags;
  1048. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1049. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1050. if (!ops)
  1051. return 0;
  1052. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1053. ret = ops->unmap(ops, iova, size);
  1054. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1055. return ret;
  1056. }
  1057. static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
  1058. dma_addr_t iova)
  1059. {
  1060. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1061. struct arm_smmu_device *smmu = smmu_domain->smmu;
  1062. struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
  1063. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1064. struct device *dev = smmu->dev;
  1065. void __iomem *cb_base;
  1066. u32 tmp;
  1067. u64 phys;
  1068. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
  1069. if (smmu->version == 1) {
  1070. u32 reg = iova & ~0xfff;
  1071. writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
  1072. } else {
  1073. u32 reg = iova & ~0xfff;
  1074. writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO);
  1075. reg = ((u64)iova & ~0xfff) >> 32;
  1076. writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_HI);
  1077. }
  1078. if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
  1079. !(tmp & ATSR_ACTIVE), 5, 50)) {
  1080. dev_err(dev,
  1081. "iova to phys timed out on 0x%pad. Falling back to software table walk.\n",
  1082. &iova);
  1083. return ops->iova_to_phys(ops, iova);
  1084. }
  1085. phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO);
  1086. phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32;
  1087. if (phys & CB_PAR_F) {
  1088. dev_err(dev, "translation fault!\n");
  1089. dev_err(dev, "PAR = 0x%llx\n", phys);
  1090. return 0;
  1091. }
  1092. return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
  1093. }
  1094. static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
  1095. dma_addr_t iova)
  1096. {
  1097. phys_addr_t ret;
  1098. unsigned long flags;
  1099. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1100. struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
  1101. if (!ops)
  1102. return 0;
  1103. spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
  1104. if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
  1105. smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
  1106. ret = arm_smmu_iova_to_phys_hard(domain, iova);
  1107. } else {
  1108. ret = ops->iova_to_phys(ops, iova);
  1109. }
  1110. spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
  1111. return ret;
  1112. }
  1113. static bool arm_smmu_capable(enum iommu_cap cap)
  1114. {
  1115. switch (cap) {
  1116. case IOMMU_CAP_CACHE_COHERENCY:
  1117. /*
  1118. * Return true here as the SMMU can always send out coherent
  1119. * requests.
  1120. */
  1121. return true;
  1122. case IOMMU_CAP_INTR_REMAP:
  1123. return true; /* MSIs are just memory writes */
  1124. case IOMMU_CAP_NOEXEC:
  1125. return true;
  1126. default:
  1127. return false;
  1128. }
  1129. }
  1130. static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
  1131. {
  1132. *((u16 *)data) = alias;
  1133. return 0; /* Continue walking */
  1134. }
  1135. static void __arm_smmu_release_pci_iommudata(void *data)
  1136. {
  1137. kfree(data);
  1138. }
  1139. static int arm_smmu_add_pci_device(struct pci_dev *pdev)
  1140. {
  1141. int i, ret;
  1142. u16 sid;
  1143. struct iommu_group *group;
  1144. struct arm_smmu_master_cfg *cfg;
  1145. group = iommu_group_get_for_dev(&pdev->dev);
  1146. if (IS_ERR(group))
  1147. return PTR_ERR(group);
  1148. cfg = iommu_group_get_iommudata(group);
  1149. if (!cfg) {
  1150. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1151. if (!cfg) {
  1152. ret = -ENOMEM;
  1153. goto out_put_group;
  1154. }
  1155. iommu_group_set_iommudata(group, cfg,
  1156. __arm_smmu_release_pci_iommudata);
  1157. }
  1158. if (cfg->num_streamids >= MAX_MASTER_STREAMIDS) {
  1159. ret = -ENOSPC;
  1160. goto out_put_group;
  1161. }
  1162. /*
  1163. * Assume Stream ID == Requester ID for now.
  1164. * We need a way to describe the ID mappings in FDT.
  1165. */
  1166. pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
  1167. for (i = 0; i < cfg->num_streamids; ++i)
  1168. if (cfg->streamids[i] == sid)
  1169. break;
  1170. /* Avoid duplicate SIDs, as this can lead to SMR conflicts */
  1171. if (i == cfg->num_streamids)
  1172. cfg->streamids[cfg->num_streamids++] = sid;
  1173. return 0;
  1174. out_put_group:
  1175. iommu_group_put(group);
  1176. return ret;
  1177. }
  1178. static int arm_smmu_add_platform_device(struct device *dev)
  1179. {
  1180. struct iommu_group *group;
  1181. struct arm_smmu_master *master;
  1182. struct arm_smmu_device *smmu = find_smmu_for_device(dev);
  1183. if (!smmu)
  1184. return -ENODEV;
  1185. master = find_smmu_master(smmu, dev->of_node);
  1186. if (!master)
  1187. return -ENODEV;
  1188. /* No automatic group creation for platform devices */
  1189. group = iommu_group_alloc();
  1190. if (IS_ERR(group))
  1191. return PTR_ERR(group);
  1192. iommu_group_set_iommudata(group, &master->cfg, NULL);
  1193. return iommu_group_add_device(group, dev);
  1194. }
  1195. static int arm_smmu_add_device(struct device *dev)
  1196. {
  1197. if (dev_is_pci(dev))
  1198. return arm_smmu_add_pci_device(to_pci_dev(dev));
  1199. return arm_smmu_add_platform_device(dev);
  1200. }
  1201. static void arm_smmu_remove_device(struct device *dev)
  1202. {
  1203. iommu_group_remove_device(dev);
  1204. }
  1205. static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
  1206. enum iommu_attr attr, void *data)
  1207. {
  1208. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1209. switch (attr) {
  1210. case DOMAIN_ATTR_NESTING:
  1211. *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
  1212. return 0;
  1213. default:
  1214. return -ENODEV;
  1215. }
  1216. }
  1217. static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
  1218. enum iommu_attr attr, void *data)
  1219. {
  1220. int ret = 0;
  1221. struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
  1222. mutex_lock(&smmu_domain->init_mutex);
  1223. switch (attr) {
  1224. case DOMAIN_ATTR_NESTING:
  1225. if (smmu_domain->smmu) {
  1226. ret = -EPERM;
  1227. goto out_unlock;
  1228. }
  1229. if (*(int *)data)
  1230. smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
  1231. else
  1232. smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
  1233. break;
  1234. default:
  1235. ret = -ENODEV;
  1236. }
  1237. out_unlock:
  1238. mutex_unlock(&smmu_domain->init_mutex);
  1239. return ret;
  1240. }
  1241. static struct iommu_ops arm_smmu_ops = {
  1242. .capable = arm_smmu_capable,
  1243. .domain_alloc = arm_smmu_domain_alloc,
  1244. .domain_free = arm_smmu_domain_free,
  1245. .attach_dev = arm_smmu_attach_dev,
  1246. .detach_dev = arm_smmu_detach_dev,
  1247. .map = arm_smmu_map,
  1248. .unmap = arm_smmu_unmap,
  1249. .map_sg = default_iommu_map_sg,
  1250. .iova_to_phys = arm_smmu_iova_to_phys,
  1251. .add_device = arm_smmu_add_device,
  1252. .remove_device = arm_smmu_remove_device,
  1253. .domain_get_attr = arm_smmu_domain_get_attr,
  1254. .domain_set_attr = arm_smmu_domain_set_attr,
  1255. .pgsize_bitmap = -1UL, /* Restricted during device attach */
  1256. };
  1257. static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
  1258. {
  1259. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1260. void __iomem *cb_base;
  1261. int i = 0;
  1262. u32 reg;
  1263. /* clear global FSR */
  1264. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  1265. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
  1266. /* Mark all SMRn as invalid and all S2CRn as bypass */
  1267. for (i = 0; i < smmu->num_mapping_groups; ++i) {
  1268. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
  1269. writel_relaxed(S2CR_TYPE_BYPASS,
  1270. gr0_base + ARM_SMMU_GR0_S2CR(i));
  1271. }
  1272. /* Make sure all context banks are disabled and clear CB_FSR */
  1273. for (i = 0; i < smmu->num_context_banks; ++i) {
  1274. cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
  1275. writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
  1276. writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
  1277. }
  1278. /* Invalidate the TLB, just in case */
  1279. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
  1280. writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
  1281. reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1282. /* Enable fault reporting */
  1283. reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
  1284. /* Disable TLB broadcasting. */
  1285. reg |= (sCR0_VMIDPNE | sCR0_PTM);
  1286. /* Enable client access, but bypass when no mapping is found */
  1287. reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
  1288. /* Disable forced broadcasting */
  1289. reg &= ~sCR0_FB;
  1290. /* Don't upgrade barriers */
  1291. reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
  1292. /* Push the button */
  1293. __arm_smmu_tlb_sync(smmu);
  1294. writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1295. }
  1296. static int arm_smmu_id_size_to_bits(int size)
  1297. {
  1298. switch (size) {
  1299. case 0:
  1300. return 32;
  1301. case 1:
  1302. return 36;
  1303. case 2:
  1304. return 40;
  1305. case 3:
  1306. return 42;
  1307. case 4:
  1308. return 44;
  1309. case 5:
  1310. default:
  1311. return 48;
  1312. }
  1313. }
  1314. static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
  1315. {
  1316. unsigned long size;
  1317. void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
  1318. u32 id;
  1319. dev_notice(smmu->dev, "probing hardware configuration...\n");
  1320. dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
  1321. /* ID0 */
  1322. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
  1323. /* Restrict available stages based on module parameter */
  1324. if (force_stage == 1)
  1325. id &= ~(ID0_S2TS | ID0_NTS);
  1326. else if (force_stage == 2)
  1327. id &= ~(ID0_S1TS | ID0_NTS);
  1328. if (id & ID0_S1TS) {
  1329. smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
  1330. dev_notice(smmu->dev, "\tstage 1 translation\n");
  1331. }
  1332. if (id & ID0_S2TS) {
  1333. smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
  1334. dev_notice(smmu->dev, "\tstage 2 translation\n");
  1335. }
  1336. if (id & ID0_NTS) {
  1337. smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
  1338. dev_notice(smmu->dev, "\tnested translation\n");
  1339. }
  1340. if (!(smmu->features &
  1341. (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
  1342. dev_err(smmu->dev, "\tno translation support!\n");
  1343. return -ENODEV;
  1344. }
  1345. if ((id & ID0_S1TS) && ((smmu->version == 1) || (id & ID0_ATOSNS))) {
  1346. smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
  1347. dev_notice(smmu->dev, "\taddress translation ops\n");
  1348. }
  1349. if (id & ID0_CTTW) {
  1350. smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
  1351. dev_notice(smmu->dev, "\tcoherent table walk\n");
  1352. }
  1353. if (id & ID0_SMS) {
  1354. u32 smr, sid, mask;
  1355. smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
  1356. smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
  1357. ID0_NUMSMRG_MASK;
  1358. if (smmu->num_mapping_groups == 0) {
  1359. dev_err(smmu->dev,
  1360. "stream-matching supported, but no SMRs present!\n");
  1361. return -ENODEV;
  1362. }
  1363. smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
  1364. smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
  1365. writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
  1366. smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
  1367. mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
  1368. sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
  1369. if ((mask & sid) != sid) {
  1370. dev_err(smmu->dev,
  1371. "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
  1372. mask, sid);
  1373. return -ENODEV;
  1374. }
  1375. dev_notice(smmu->dev,
  1376. "\tstream matching with %u register groups, mask 0x%x",
  1377. smmu->num_mapping_groups, mask);
  1378. } else {
  1379. smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
  1380. ID0_NUMSIDB_MASK;
  1381. }
  1382. /* ID1 */
  1383. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
  1384. smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
  1385. /* Check for size mismatch of SMMU address space from mapped region */
  1386. size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
  1387. size *= 2 << smmu->pgshift;
  1388. if (smmu->size != size)
  1389. dev_warn(smmu->dev,
  1390. "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
  1391. size, smmu->size);
  1392. smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
  1393. smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
  1394. if (smmu->num_s2_context_banks > smmu->num_context_banks) {
  1395. dev_err(smmu->dev, "impossible number of S2 context banks!\n");
  1396. return -ENODEV;
  1397. }
  1398. dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
  1399. smmu->num_context_banks, smmu->num_s2_context_banks);
  1400. /* ID2 */
  1401. id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
  1402. size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
  1403. smmu->ipa_size = size;
  1404. /* The output mask is also applied for bypass */
  1405. size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
  1406. smmu->pa_size = size;
  1407. /*
  1408. * What the page table walker can address actually depends on which
  1409. * descriptor format is in use, but since a) we don't know that yet,
  1410. * and b) it can vary per context bank, this will have to do...
  1411. */
  1412. if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
  1413. dev_warn(smmu->dev,
  1414. "failed to set DMA mask for table walker\n");
  1415. if (smmu->version == ARM_SMMU_V1) {
  1416. smmu->va_size = smmu->ipa_size;
  1417. size = SZ_4K | SZ_2M | SZ_1G;
  1418. } else {
  1419. size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
  1420. smmu->va_size = arm_smmu_id_size_to_bits(size);
  1421. #ifndef CONFIG_64BIT
  1422. smmu->va_size = min(32UL, smmu->va_size);
  1423. #endif
  1424. size = 0;
  1425. if (id & ID2_PTFS_4K)
  1426. size |= SZ_4K | SZ_2M | SZ_1G;
  1427. if (id & ID2_PTFS_16K)
  1428. size |= SZ_16K | SZ_32M;
  1429. if (id & ID2_PTFS_64K)
  1430. size |= SZ_64K | SZ_512M;
  1431. }
  1432. arm_smmu_ops.pgsize_bitmap &= size;
  1433. dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size);
  1434. if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
  1435. dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
  1436. smmu->va_size, smmu->ipa_size);
  1437. if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
  1438. dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
  1439. smmu->ipa_size, smmu->pa_size);
  1440. return 0;
  1441. }
  1442. static const struct of_device_id arm_smmu_of_match[] = {
  1443. { .compatible = "arm,smmu-v1", .data = (void *)ARM_SMMU_V1 },
  1444. { .compatible = "arm,smmu-v2", .data = (void *)ARM_SMMU_V2 },
  1445. { .compatible = "arm,mmu-400", .data = (void *)ARM_SMMU_V1 },
  1446. { .compatible = "arm,mmu-401", .data = (void *)ARM_SMMU_V1 },
  1447. { .compatible = "arm,mmu-500", .data = (void *)ARM_SMMU_V2 },
  1448. { },
  1449. };
  1450. MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
  1451. static int arm_smmu_device_dt_probe(struct platform_device *pdev)
  1452. {
  1453. const struct of_device_id *of_id;
  1454. struct resource *res;
  1455. struct arm_smmu_device *smmu;
  1456. struct device *dev = &pdev->dev;
  1457. struct rb_node *node;
  1458. struct of_phandle_args masterspec;
  1459. int num_irqs, i, err;
  1460. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  1461. if (!smmu) {
  1462. dev_err(dev, "failed to allocate arm_smmu_device\n");
  1463. return -ENOMEM;
  1464. }
  1465. smmu->dev = dev;
  1466. of_id = of_match_node(arm_smmu_of_match, dev->of_node);
  1467. smmu->version = (enum arm_smmu_arch_version)of_id->data;
  1468. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1469. smmu->base = devm_ioremap_resource(dev, res);
  1470. if (IS_ERR(smmu->base))
  1471. return PTR_ERR(smmu->base);
  1472. smmu->size = resource_size(res);
  1473. if (of_property_read_u32(dev->of_node, "#global-interrupts",
  1474. &smmu->num_global_irqs)) {
  1475. dev_err(dev, "missing #global-interrupts property\n");
  1476. return -ENODEV;
  1477. }
  1478. num_irqs = 0;
  1479. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
  1480. num_irqs++;
  1481. if (num_irqs > smmu->num_global_irqs)
  1482. smmu->num_context_irqs++;
  1483. }
  1484. if (!smmu->num_context_irqs) {
  1485. dev_err(dev, "found %d interrupts but expected at least %d\n",
  1486. num_irqs, smmu->num_global_irqs + 1);
  1487. return -ENODEV;
  1488. }
  1489. smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
  1490. GFP_KERNEL);
  1491. if (!smmu->irqs) {
  1492. dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
  1493. return -ENOMEM;
  1494. }
  1495. for (i = 0; i < num_irqs; ++i) {
  1496. int irq = platform_get_irq(pdev, i);
  1497. if (irq < 0) {
  1498. dev_err(dev, "failed to get irq index %d\n", i);
  1499. return -ENODEV;
  1500. }
  1501. smmu->irqs[i] = irq;
  1502. }
  1503. err = arm_smmu_device_cfg_probe(smmu);
  1504. if (err)
  1505. return err;
  1506. i = 0;
  1507. smmu->masters = RB_ROOT;
  1508. while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
  1509. "#stream-id-cells", i,
  1510. &masterspec)) {
  1511. err = register_smmu_master(smmu, dev, &masterspec);
  1512. if (err) {
  1513. dev_err(dev, "failed to add master %s\n",
  1514. masterspec.np->name);
  1515. goto out_put_masters;
  1516. }
  1517. i++;
  1518. }
  1519. dev_notice(dev, "registered %d master devices\n", i);
  1520. parse_driver_options(smmu);
  1521. if (smmu->version > ARM_SMMU_V1 &&
  1522. smmu->num_context_banks != smmu->num_context_irqs) {
  1523. dev_err(dev,
  1524. "found only %d context interrupt(s) but %d required\n",
  1525. smmu->num_context_irqs, smmu->num_context_banks);
  1526. err = -ENODEV;
  1527. goto out_put_masters;
  1528. }
  1529. for (i = 0; i < smmu->num_global_irqs; ++i) {
  1530. err = request_irq(smmu->irqs[i],
  1531. arm_smmu_global_fault,
  1532. IRQF_SHARED,
  1533. "arm-smmu global fault",
  1534. smmu);
  1535. if (err) {
  1536. dev_err(dev, "failed to request global IRQ %d (%u)\n",
  1537. i, smmu->irqs[i]);
  1538. goto out_free_irqs;
  1539. }
  1540. }
  1541. INIT_LIST_HEAD(&smmu->list);
  1542. spin_lock(&arm_smmu_devices_lock);
  1543. list_add(&smmu->list, &arm_smmu_devices);
  1544. spin_unlock(&arm_smmu_devices_lock);
  1545. arm_smmu_device_reset(smmu);
  1546. return 0;
  1547. out_free_irqs:
  1548. while (i--)
  1549. free_irq(smmu->irqs[i], smmu);
  1550. out_put_masters:
  1551. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1552. struct arm_smmu_master *master
  1553. = container_of(node, struct arm_smmu_master, node);
  1554. of_node_put(master->of_node);
  1555. }
  1556. return err;
  1557. }
  1558. static int arm_smmu_device_remove(struct platform_device *pdev)
  1559. {
  1560. int i;
  1561. struct device *dev = &pdev->dev;
  1562. struct arm_smmu_device *curr, *smmu = NULL;
  1563. struct rb_node *node;
  1564. spin_lock(&arm_smmu_devices_lock);
  1565. list_for_each_entry(curr, &arm_smmu_devices, list) {
  1566. if (curr->dev == dev) {
  1567. smmu = curr;
  1568. list_del(&smmu->list);
  1569. break;
  1570. }
  1571. }
  1572. spin_unlock(&arm_smmu_devices_lock);
  1573. if (!smmu)
  1574. return -ENODEV;
  1575. for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
  1576. struct arm_smmu_master *master
  1577. = container_of(node, struct arm_smmu_master, node);
  1578. of_node_put(master->of_node);
  1579. }
  1580. if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
  1581. dev_err(dev, "removing device with active domains!\n");
  1582. for (i = 0; i < smmu->num_global_irqs; ++i)
  1583. free_irq(smmu->irqs[i], smmu);
  1584. /* Turn the thing off */
  1585. writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
  1586. return 0;
  1587. }
  1588. static struct platform_driver arm_smmu_driver = {
  1589. .driver = {
  1590. .name = "arm-smmu",
  1591. .of_match_table = of_match_ptr(arm_smmu_of_match),
  1592. },
  1593. .probe = arm_smmu_device_dt_probe,
  1594. .remove = arm_smmu_device_remove,
  1595. };
  1596. static int __init arm_smmu_init(void)
  1597. {
  1598. struct device_node *np;
  1599. int ret;
  1600. /*
  1601. * Play nice with systems that don't have an ARM SMMU by checking that
  1602. * an ARM SMMU exists in the system before proceeding with the driver
  1603. * and IOMMU bus operation registration.
  1604. */
  1605. np = of_find_matching_node(NULL, arm_smmu_of_match);
  1606. if (!np)
  1607. return 0;
  1608. of_node_put(np);
  1609. ret = platform_driver_register(&arm_smmu_driver);
  1610. if (ret)
  1611. return ret;
  1612. /* Oh, for a proper bus abstraction */
  1613. if (!iommu_present(&platform_bus_type))
  1614. bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
  1615. #ifdef CONFIG_ARM_AMBA
  1616. if (!iommu_present(&amba_bustype))
  1617. bus_set_iommu(&amba_bustype, &arm_smmu_ops);
  1618. #endif
  1619. #ifdef CONFIG_PCI
  1620. if (!iommu_present(&pci_bus_type))
  1621. bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
  1622. #endif
  1623. return 0;
  1624. }
  1625. static void __exit arm_smmu_exit(void)
  1626. {
  1627. return platform_driver_unregister(&arm_smmu_driver);
  1628. }
  1629. subsys_initcall(arm_smmu_init);
  1630. module_exit(arm_smmu_exit);
  1631. MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
  1632. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  1633. MODULE_LICENSE("GPL v2");