arm-smmu.c 50 KB

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