2 * IOMMU API for ARM architected SMMU implementations.
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.
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.
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.
17 * Copyright (C) 2013 ARM Limited
19 * Author: Will Deacon <will.deacon@arm.com>
21 * This driver currently supports:
22 * - SMMUv1 and v2 implementations
23 * - Stream-matching and stream-indexing
24 * - v7/v8 long-descriptor format
25 * - Non-secure access to the SMMU
26 * - 4k and 64k pages, with contiguous pte hints.
27 * - Up to 42-bit addressing (dependent on VA_BITS)
28 * - Context fault reporting
31 #define pr_fmt(fmt) "arm-smmu: " fmt
33 #include <linux/delay.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/err.h>
36 #include <linux/interrupt.h>
38 #include <linux/iommu.h>
40 #include <linux/module.h>
42 #include <linux/pci.h>
43 #include <linux/platform_device.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
47 #include <linux/amba/bus.h>
49 #include <asm/pgalloc.h>
51 /* Maximum number of stream IDs assigned to a single device */
52 #define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS
54 /* Maximum number of context banks per SMMU */
55 #define ARM_SMMU_MAX_CBS 128
57 /* Maximum number of mapping groups per SMMU */
58 #define ARM_SMMU_MAX_SMRS 128
60 /* SMMU global address space */
61 #define ARM_SMMU_GR0(smmu) ((smmu)->base)
62 #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
65 * SMMU global address space with conditional offset to access secure
66 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
69 #define ARM_SMMU_GR0_NS(smmu) \
71 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
75 #define ARM_SMMU_PTE_XN (((pteval_t)3) << 53)
76 #define ARM_SMMU_PTE_CONT (((pteval_t)1) << 52)
77 #define ARM_SMMU_PTE_AF (((pteval_t)1) << 10)
78 #define ARM_SMMU_PTE_SH_NS (((pteval_t)0) << 8)
79 #define ARM_SMMU_PTE_SH_OS (((pteval_t)2) << 8)
80 #define ARM_SMMU_PTE_SH_IS (((pteval_t)3) << 8)
81 #define ARM_SMMU_PTE_PAGE (((pteval_t)3) << 0)
83 #if PAGE_SIZE == SZ_4K
84 #define ARM_SMMU_PTE_CONT_ENTRIES 16
85 #elif PAGE_SIZE == SZ_64K
86 #define ARM_SMMU_PTE_CONT_ENTRIES 32
88 #define ARM_SMMU_PTE_CONT_ENTRIES 1
91 #define ARM_SMMU_PTE_CONT_SIZE (PAGE_SIZE * ARM_SMMU_PTE_CONT_ENTRIES)
92 #define ARM_SMMU_PTE_CONT_MASK (~(ARM_SMMU_PTE_CONT_SIZE - 1))
95 #define ARM_SMMU_PTE_AP_UNPRIV (((pteval_t)1) << 6)
96 #define ARM_SMMU_PTE_AP_RDONLY (((pteval_t)2) << 6)
97 #define ARM_SMMU_PTE_ATTRINDX_SHIFT 2
98 #define ARM_SMMU_PTE_nG (((pteval_t)1) << 11)
101 #define ARM_SMMU_PTE_HAP_FAULT (((pteval_t)0) << 6)
102 #define ARM_SMMU_PTE_HAP_READ (((pteval_t)1) << 6)
103 #define ARM_SMMU_PTE_HAP_WRITE (((pteval_t)2) << 6)
104 #define ARM_SMMU_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2)
105 #define ARM_SMMU_PTE_MEMATTR_NC (((pteval_t)0x5) << 2)
106 #define ARM_SMMU_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2)
108 /* Configuration registers */
109 #define ARM_SMMU_GR0_sCR0 0x0
110 #define sCR0_CLIENTPD (1 << 0)
111 #define sCR0_GFRE (1 << 1)
112 #define sCR0_GFIE (1 << 2)
113 #define sCR0_GCFGFRE (1 << 4)
114 #define sCR0_GCFGFIE (1 << 5)
115 #define sCR0_USFCFG (1 << 10)
116 #define sCR0_VMIDPNE (1 << 11)
117 #define sCR0_PTM (1 << 12)
118 #define sCR0_FB (1 << 13)
119 #define sCR0_BSU_SHIFT 14
120 #define sCR0_BSU_MASK 0x3
122 /* Identification registers */
123 #define ARM_SMMU_GR0_ID0 0x20
124 #define ARM_SMMU_GR0_ID1 0x24
125 #define ARM_SMMU_GR0_ID2 0x28
126 #define ARM_SMMU_GR0_ID3 0x2c
127 #define ARM_SMMU_GR0_ID4 0x30
128 #define ARM_SMMU_GR0_ID5 0x34
129 #define ARM_SMMU_GR0_ID6 0x38
130 #define ARM_SMMU_GR0_ID7 0x3c
131 #define ARM_SMMU_GR0_sGFSR 0x48
132 #define ARM_SMMU_GR0_sGFSYNR0 0x50
133 #define ARM_SMMU_GR0_sGFSYNR1 0x54
134 #define ARM_SMMU_GR0_sGFSYNR2 0x58
135 #define ARM_SMMU_GR0_PIDR0 0xfe0
136 #define ARM_SMMU_GR0_PIDR1 0xfe4
137 #define ARM_SMMU_GR0_PIDR2 0xfe8
139 #define ID0_S1TS (1 << 30)
140 #define ID0_S2TS (1 << 29)
141 #define ID0_NTS (1 << 28)
142 #define ID0_SMS (1 << 27)
143 #define ID0_PTFS_SHIFT 24
144 #define ID0_PTFS_MASK 0x2
145 #define ID0_PTFS_V8_ONLY 0x2
146 #define ID0_CTTW (1 << 14)
147 #define ID0_NUMIRPT_SHIFT 16
148 #define ID0_NUMIRPT_MASK 0xff
149 #define ID0_NUMSIDB_SHIFT 9
150 #define ID0_NUMSIDB_MASK 0xf
151 #define ID0_NUMSMRG_SHIFT 0
152 #define ID0_NUMSMRG_MASK 0xff
154 #define ID1_PAGESIZE (1 << 31)
155 #define ID1_NUMPAGENDXB_SHIFT 28
156 #define ID1_NUMPAGENDXB_MASK 7
157 #define ID1_NUMS2CB_SHIFT 16
158 #define ID1_NUMS2CB_MASK 0xff
159 #define ID1_NUMCB_SHIFT 0
160 #define ID1_NUMCB_MASK 0xff
162 #define ID2_OAS_SHIFT 4
163 #define ID2_OAS_MASK 0xf
164 #define ID2_IAS_SHIFT 0
165 #define ID2_IAS_MASK 0xf
166 #define ID2_UBS_SHIFT 8
167 #define ID2_UBS_MASK 0xf
168 #define ID2_PTFS_4K (1 << 12)
169 #define ID2_PTFS_16K (1 << 13)
170 #define ID2_PTFS_64K (1 << 14)
172 #define PIDR2_ARCH_SHIFT 4
173 #define PIDR2_ARCH_MASK 0xf
175 /* Global TLB invalidation */
176 #define ARM_SMMU_GR0_STLBIALL 0x60
177 #define ARM_SMMU_GR0_TLBIVMID 0x64
178 #define ARM_SMMU_GR0_TLBIALLNSNH 0x68
179 #define ARM_SMMU_GR0_TLBIALLH 0x6c
180 #define ARM_SMMU_GR0_sTLBGSYNC 0x70
181 #define ARM_SMMU_GR0_sTLBGSTATUS 0x74
182 #define sTLBGSTATUS_GSACTIVE (1 << 0)
183 #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
185 /* Stream mapping registers */
186 #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
187 #define SMR_VALID (1 << 31)
188 #define SMR_MASK_SHIFT 16
189 #define SMR_MASK_MASK 0x7fff
190 #define SMR_ID_SHIFT 0
191 #define SMR_ID_MASK 0x7fff
193 #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
194 #define S2CR_CBNDX_SHIFT 0
195 #define S2CR_CBNDX_MASK 0xff
196 #define S2CR_TYPE_SHIFT 16
197 #define S2CR_TYPE_MASK 0x3
198 #define S2CR_TYPE_TRANS (0 << S2CR_TYPE_SHIFT)
199 #define S2CR_TYPE_BYPASS (1 << S2CR_TYPE_SHIFT)
200 #define S2CR_TYPE_FAULT (2 << S2CR_TYPE_SHIFT)
202 /* Context bank attribute registers */
203 #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
204 #define CBAR_VMID_SHIFT 0
205 #define CBAR_VMID_MASK 0xff
206 #define CBAR_S1_BPSHCFG_SHIFT 8
207 #define CBAR_S1_BPSHCFG_MASK 3
208 #define CBAR_S1_BPSHCFG_NSH 3
209 #define CBAR_S1_MEMATTR_SHIFT 12
210 #define CBAR_S1_MEMATTR_MASK 0xf
211 #define CBAR_S1_MEMATTR_WB 0xf
212 #define CBAR_TYPE_SHIFT 16
213 #define CBAR_TYPE_MASK 0x3
214 #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT)
215 #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT)
216 #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT)
217 #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT)
218 #define CBAR_IRPTNDX_SHIFT 24
219 #define CBAR_IRPTNDX_MASK 0xff
221 #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
222 #define CBA2R_RW64_32BIT (0 << 0)
223 #define CBA2R_RW64_64BIT (1 << 0)
225 /* Translation context bank */
226 #define ARM_SMMU_CB_BASE(smmu) ((smmu)->base + ((smmu)->size >> 1))
227 #define ARM_SMMU_CB(smmu, n) ((n) * (1 << (smmu)->pgshift))
229 #define ARM_SMMU_CB_SCTLR 0x0
230 #define ARM_SMMU_CB_RESUME 0x8
231 #define ARM_SMMU_CB_TTBCR2 0x10
232 #define ARM_SMMU_CB_TTBR0_LO 0x20
233 #define ARM_SMMU_CB_TTBR0_HI 0x24
234 #define ARM_SMMU_CB_TTBCR 0x30
235 #define ARM_SMMU_CB_S1_MAIR0 0x38
236 #define ARM_SMMU_CB_FSR 0x58
237 #define ARM_SMMU_CB_FAR_LO 0x60
238 #define ARM_SMMU_CB_FAR_HI 0x64
239 #define ARM_SMMU_CB_FSYNR0 0x68
240 #define ARM_SMMU_CB_S1_TLBIASID 0x610
242 #define SCTLR_S1_ASIDPNE (1 << 12)
243 #define SCTLR_CFCFG (1 << 7)
244 #define SCTLR_CFIE (1 << 6)
245 #define SCTLR_CFRE (1 << 5)
246 #define SCTLR_E (1 << 4)
247 #define SCTLR_AFE (1 << 2)
248 #define SCTLR_TRE (1 << 1)
249 #define SCTLR_M (1 << 0)
250 #define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE)
252 #define RESUME_RETRY (0 << 0)
253 #define RESUME_TERMINATE (1 << 0)
255 #define TTBCR_EAE (1 << 31)
257 #define TTBCR_PASIZE_SHIFT 16
258 #define TTBCR_PASIZE_MASK 0x7
260 #define TTBCR_TG0_4K (0 << 14)
261 #define TTBCR_TG0_64K (1 << 14)
263 #define TTBCR_SH0_SHIFT 12
264 #define TTBCR_SH0_MASK 0x3
265 #define TTBCR_SH_NS 0
266 #define TTBCR_SH_OS 2
267 #define TTBCR_SH_IS 3
269 #define TTBCR_ORGN0_SHIFT 10
270 #define TTBCR_IRGN0_SHIFT 8
271 #define TTBCR_RGN_MASK 0x3
272 #define TTBCR_RGN_NC 0
273 #define TTBCR_RGN_WBWA 1
274 #define TTBCR_RGN_WT 2
275 #define TTBCR_RGN_WB 3
277 #define TTBCR_SL0_SHIFT 6
278 #define TTBCR_SL0_MASK 0x3
279 #define TTBCR_SL0_LVL_2 0
280 #define TTBCR_SL0_LVL_1 1
282 #define TTBCR_T1SZ_SHIFT 16
283 #define TTBCR_T0SZ_SHIFT 0
284 #define TTBCR_SZ_MASK 0xf
286 #define TTBCR2_SEP_SHIFT 15
287 #define TTBCR2_SEP_MASK 0x7
289 #define TTBCR2_PASIZE_SHIFT 0
290 #define TTBCR2_PASIZE_MASK 0x7
292 /* Common definitions for PASize and SEP fields */
293 #define TTBCR2_ADDR_32 0
294 #define TTBCR2_ADDR_36 1
295 #define TTBCR2_ADDR_40 2
296 #define TTBCR2_ADDR_42 3
297 #define TTBCR2_ADDR_44 4
298 #define TTBCR2_ADDR_48 5
300 #define TTBRn_HI_ASID_SHIFT 16
302 #define MAIR_ATTR_SHIFT(n) ((n) << 3)
303 #define MAIR_ATTR_MASK 0xff
304 #define MAIR_ATTR_DEVICE 0x04
305 #define MAIR_ATTR_NC 0x44
306 #define MAIR_ATTR_WBRWA 0xff
307 #define MAIR_ATTR_IDX_NC 0
308 #define MAIR_ATTR_IDX_CACHE 1
309 #define MAIR_ATTR_IDX_DEV 2
311 #define FSR_MULTI (1 << 31)
312 #define FSR_SS (1 << 30)
313 #define FSR_UUT (1 << 8)
314 #define FSR_ASF (1 << 7)
315 #define FSR_TLBLKF (1 << 6)
316 #define FSR_TLBMCF (1 << 5)
317 #define FSR_EF (1 << 4)
318 #define FSR_PF (1 << 3)
319 #define FSR_AFF (1 << 2)
320 #define FSR_TF (1 << 1)
322 #define FSR_IGN (FSR_AFF | FSR_ASF | \
323 FSR_TLBMCF | FSR_TLBLKF)
324 #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \
325 FSR_EF | FSR_PF | FSR_TF | FSR_IGN)
327 #define FSYNR0_WNR (1 << 4)
329 static int force_stage;
330 module_param_named(force_stage, force_stage, int, S_IRUGO | S_IWUSR);
331 MODULE_PARM_DESC(force_stage,
332 "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.");
334 struct arm_smmu_smr {
340 struct arm_smmu_master_cfg {
342 u16 streamids[MAX_MASTER_STREAMIDS];
343 struct arm_smmu_smr *smrs;
346 struct arm_smmu_master {
347 struct device_node *of_node;
349 struct arm_smmu_master_cfg cfg;
352 struct arm_smmu_device {
357 unsigned long pgshift;
359 #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
360 #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
361 #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
362 #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
363 #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
366 #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
370 u32 num_context_banks;
371 u32 num_s2_context_banks;
372 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
375 u32 num_mapping_groups;
376 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS);
378 unsigned long input_size;
379 unsigned long s1_output_size;
380 unsigned long s2_output_size;
383 u32 num_context_irqs;
386 struct list_head list;
387 struct rb_root masters;
390 struct arm_smmu_cfg {
396 #define INVALID_IRPTNDX 0xff
398 #define ARM_SMMU_CB_ASID(cfg) ((cfg)->cbndx)
399 #define ARM_SMMU_CB_VMID(cfg) ((cfg)->cbndx + 1)
401 struct arm_smmu_domain {
402 struct arm_smmu_device *smmu;
403 struct arm_smmu_cfg cfg;
407 static DEFINE_SPINLOCK(arm_smmu_devices_lock);
408 static LIST_HEAD(arm_smmu_devices);
410 struct arm_smmu_option_prop {
415 static struct arm_smmu_option_prop arm_smmu_options[] = {
416 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
420 static void parse_driver_options(struct arm_smmu_device *smmu)
425 if (of_property_read_bool(smmu->dev->of_node,
426 arm_smmu_options[i].prop)) {
427 smmu->options |= arm_smmu_options[i].opt;
428 dev_notice(smmu->dev, "option %s\n",
429 arm_smmu_options[i].prop);
431 } while (arm_smmu_options[++i].opt);
434 static struct device_node *dev_get_dev_node(struct device *dev)
436 if (dev_is_pci(dev)) {
437 struct pci_bus *bus = to_pci_dev(dev)->bus;
439 while (!pci_is_root_bus(bus))
441 return bus->bridge->parent->of_node;
447 static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
448 struct device_node *dev_node)
450 struct rb_node *node = smmu->masters.rb_node;
453 struct arm_smmu_master *master;
455 master = container_of(node, struct arm_smmu_master, node);
457 if (dev_node < master->of_node)
458 node = node->rb_left;
459 else if (dev_node > master->of_node)
460 node = node->rb_right;
468 static struct arm_smmu_master_cfg *
469 find_smmu_master_cfg(struct device *dev)
471 struct arm_smmu_master_cfg *cfg = NULL;
472 struct iommu_group *group = iommu_group_get(dev);
475 cfg = iommu_group_get_iommudata(group);
476 iommu_group_put(group);
482 static int insert_smmu_master(struct arm_smmu_device *smmu,
483 struct arm_smmu_master *master)
485 struct rb_node **new, *parent;
487 new = &smmu->masters.rb_node;
490 struct arm_smmu_master *this
491 = container_of(*new, struct arm_smmu_master, node);
494 if (master->of_node < this->of_node)
495 new = &((*new)->rb_left);
496 else if (master->of_node > this->of_node)
497 new = &((*new)->rb_right);
502 rb_link_node(&master->node, parent, new);
503 rb_insert_color(&master->node, &smmu->masters);
507 static int register_smmu_master(struct arm_smmu_device *smmu,
509 struct of_phandle_args *masterspec)
512 struct arm_smmu_master *master;
514 master = find_smmu_master(smmu, masterspec->np);
517 "rejecting multiple registrations for master device %s\n",
518 masterspec->np->name);
522 if (masterspec->args_count > MAX_MASTER_STREAMIDS) {
524 "reached maximum number (%d) of stream IDs for master device %s\n",
525 MAX_MASTER_STREAMIDS, masterspec->np->name);
529 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
533 master->of_node = masterspec->np;
534 master->cfg.num_streamids = masterspec->args_count;
536 for (i = 0; i < master->cfg.num_streamids; ++i) {
537 u16 streamid = masterspec->args[i];
539 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
540 (streamid >= smmu->num_mapping_groups)) {
542 "stream ID for master device %s greater than maximum allowed (%d)\n",
543 masterspec->np->name, smmu->num_mapping_groups);
546 master->cfg.streamids[i] = streamid;
548 return insert_smmu_master(smmu, master);
551 static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
553 struct arm_smmu_device *smmu;
554 struct arm_smmu_master *master = NULL;
555 struct device_node *dev_node = dev_get_dev_node(dev);
557 spin_lock(&arm_smmu_devices_lock);
558 list_for_each_entry(smmu, &arm_smmu_devices, list) {
559 master = find_smmu_master(smmu, dev_node);
563 spin_unlock(&arm_smmu_devices_lock);
565 return master ? smmu : NULL;
568 static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
573 idx = find_next_zero_bit(map, end, start);
576 } while (test_and_set_bit(idx, map));
581 static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
586 /* Wait for any pending TLB invalidations to complete */
587 static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
590 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
592 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_sTLBGSYNC);
593 while (readl_relaxed(gr0_base + ARM_SMMU_GR0_sTLBGSTATUS)
594 & sTLBGSTATUS_GSACTIVE) {
596 if (++count == TLB_LOOP_TIMEOUT) {
597 dev_err_ratelimited(smmu->dev,
598 "TLB sync timed out -- SMMU may be deadlocked\n");
605 static void arm_smmu_tlb_inv_context(struct arm_smmu_domain *smmu_domain)
607 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
608 struct arm_smmu_device *smmu = smmu_domain->smmu;
609 void __iomem *base = ARM_SMMU_GR0(smmu);
610 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
613 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
614 writel_relaxed(ARM_SMMU_CB_ASID(cfg),
615 base + ARM_SMMU_CB_S1_TLBIASID);
617 base = ARM_SMMU_GR0(smmu);
618 writel_relaxed(ARM_SMMU_CB_VMID(cfg),
619 base + ARM_SMMU_GR0_TLBIVMID);
622 arm_smmu_tlb_sync(smmu);
625 static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
628 u32 fsr, far, fsynr, resume;
630 struct iommu_domain *domain = dev;
631 struct arm_smmu_domain *smmu_domain = domain->priv;
632 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
633 struct arm_smmu_device *smmu = smmu_domain->smmu;
634 void __iomem *cb_base;
636 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
637 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
639 if (!(fsr & FSR_FAULT))
643 dev_err_ratelimited(smmu->dev,
644 "Unexpected context fault (fsr 0x%x)\n",
647 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
648 flags = fsynr & FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
650 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_LO);
653 far = readl_relaxed(cb_base + ARM_SMMU_CB_FAR_HI);
654 iova |= ((unsigned long)far << 32);
657 if (!report_iommu_fault(domain, smmu->dev, iova, flags)) {
659 resume = RESUME_RETRY;
661 dev_err_ratelimited(smmu->dev,
662 "Unhandled context fault: iova=0x%08lx, fsynr=0x%x, cb=%d\n",
663 iova, fsynr, cfg->cbndx);
665 resume = RESUME_TERMINATE;
668 /* Clear the faulting FSR */
669 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
671 /* Retry or terminate any stalled transactions */
673 writel_relaxed(resume, cb_base + ARM_SMMU_CB_RESUME);
678 static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
680 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
681 struct arm_smmu_device *smmu = dev;
682 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
684 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
685 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
686 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
687 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
692 dev_err_ratelimited(smmu->dev,
693 "Unexpected global fault, this could be serious\n");
694 dev_err_ratelimited(smmu->dev,
695 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
696 gfsr, gfsynr0, gfsynr1, gfsynr2);
698 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
702 static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr,
705 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
708 /* Ensure new page tables are visible to the hardware walker */
709 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) {
713 * If the SMMU can't walk tables in the CPU caches, treat them
714 * like non-coherent DMA since we need to flush the new entries
715 * all the way out to memory. There's no possibility of
716 * recursion here as the SMMU table walker will not be wired
717 * through another SMMU.
719 dma_map_page(smmu->dev, virt_to_page(addr), offset, size,
724 static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain)
728 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
729 struct arm_smmu_device *smmu = smmu_domain->smmu;
730 void __iomem *cb_base, *gr0_base, *gr1_base;
732 gr0_base = ARM_SMMU_GR0(smmu);
733 gr1_base = ARM_SMMU_GR1(smmu);
734 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
735 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
739 if (smmu->version == 1)
740 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
743 * Use the weakest shareability/memory types, so they are
744 * overridden by the ttbcr/pte.
747 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
748 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
750 reg |= ARM_SMMU_CB_VMID(cfg) << CBAR_VMID_SHIFT;
752 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx));
754 if (smmu->version > 1) {
757 reg = CBA2R_RW64_64BIT;
759 reg = CBA2R_RW64_32BIT;
762 gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx));
765 switch (smmu->input_size) {
767 reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT);
770 reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT);
774 reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT);
777 reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT);
780 reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT);
783 reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT);
787 switch (smmu->s1_output_size) {
789 reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT);
792 reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT);
796 reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT);
799 reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT);
802 reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT);
805 reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT);
810 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2);
814 arm_smmu_flush_pgtable(smmu, cfg->pgd,
815 PTRS_PER_PGD * sizeof(pgd_t));
816 reg = __pa(cfg->pgd);
817 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO);
818 reg = (phys_addr_t)__pa(cfg->pgd) >> 32;
820 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT;
821 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI);
825 * We use long descriptor, with inner-shareable WBWA tables in TTBR0.
827 if (smmu->version > 1) {
828 if (PAGE_SIZE == SZ_4K)
834 reg |= (64 - smmu->s1_output_size) << TTBCR_T0SZ_SHIFT;
836 switch (smmu->s2_output_size) {
838 reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT);
841 reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT);
844 reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT);
847 reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT);
850 reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT);
853 reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT);
857 reg |= (64 - smmu->input_size) << TTBCR_T0SZ_SHIFT;
864 (TTBCR_SH_IS << TTBCR_SH0_SHIFT) |
865 (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) |
866 (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT);
869 reg |= (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT);
871 writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR);
873 /* MAIR0 (stage-1 only) */
875 reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) |
876 (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) |
877 (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV));
878 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0);
882 reg = SCTLR_CFCFG | SCTLR_CFIE | SCTLR_CFRE | SCTLR_M | SCTLR_EAE_SBOP;
884 reg |= SCTLR_S1_ASIDPNE;
888 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
891 static int arm_smmu_init_domain_context(struct iommu_domain *domain,
892 struct arm_smmu_device *smmu)
894 int irq, start, ret = 0;
896 struct arm_smmu_domain *smmu_domain = domain->priv;
897 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
899 spin_lock_irqsave(&smmu_domain->lock, flags);
900 if (smmu_domain->smmu)
903 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
905 * We will likely want to change this if/when KVM gets
908 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
909 start = smmu->num_s2_context_banks;
910 } else if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) {
911 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
912 start = smmu->num_s2_context_banks;
914 cfg->cbar = CBAR_TYPE_S2_TRANS;
918 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
919 smmu->num_context_banks);
920 if (IS_ERR_VALUE(ret))
924 if (smmu->version == 1) {
925 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
926 cfg->irptndx %= smmu->num_context_irqs;
928 cfg->irptndx = cfg->cbndx;
931 ACCESS_ONCE(smmu_domain->smmu) = smmu;
932 arm_smmu_init_context_bank(smmu_domain);
933 spin_unlock_irqrestore(&smmu_domain->lock, flags);
935 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
936 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
937 "arm-smmu-context-fault", domain);
938 if (IS_ERR_VALUE(ret)) {
939 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
941 cfg->irptndx = INVALID_IRPTNDX;
947 spin_unlock_irqrestore(&smmu_domain->lock, flags);
951 static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
953 struct arm_smmu_domain *smmu_domain = domain->priv;
954 struct arm_smmu_device *smmu = smmu_domain->smmu;
955 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
956 void __iomem *cb_base;
962 /* Disable the context bank and nuke the TLB before freeing it. */
963 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx);
964 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
965 arm_smmu_tlb_inv_context(smmu_domain);
967 if (cfg->irptndx != INVALID_IRPTNDX) {
968 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
969 free_irq(irq, domain);
972 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
975 static int arm_smmu_domain_init(struct iommu_domain *domain)
977 struct arm_smmu_domain *smmu_domain;
981 * Allocate the domain and initialise some of its data structures.
982 * We can't really do anything meaningful until we've added a
985 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
989 pgd = kcalloc(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL);
991 goto out_free_domain;
992 smmu_domain->cfg.pgd = pgd;
994 spin_lock_init(&smmu_domain->lock);
995 domain->priv = smmu_domain;
1003 static void arm_smmu_free_ptes(pmd_t *pmd)
1005 pgtable_t table = pmd_pgtable(*pmd);
1010 static void arm_smmu_free_pmds(pud_t *pud)
1013 pmd_t *pmd, *pmd_base = pmd_offset(pud, 0);
1016 for (i = 0; i < PTRS_PER_PMD; ++i) {
1020 arm_smmu_free_ptes(pmd);
1024 pmd_free(NULL, pmd_base);
1027 static void arm_smmu_free_puds(pgd_t *pgd)
1030 pud_t *pud, *pud_base = pud_offset(pgd, 0);
1033 for (i = 0; i < PTRS_PER_PUD; ++i) {
1037 arm_smmu_free_pmds(pud);
1041 pud_free(NULL, pud_base);
1044 static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain)
1047 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1048 pgd_t *pgd, *pgd_base = cfg->pgd;
1051 * Recursively free the page tables for this domain. We don't
1052 * care about speculative TLB filling because the tables should
1053 * not be active in any context bank at this point (SCTLR.M is 0).
1056 for (i = 0; i < PTRS_PER_PGD; ++i) {
1059 arm_smmu_free_puds(pgd);
1066 static void arm_smmu_domain_destroy(struct iommu_domain *domain)
1068 struct arm_smmu_domain *smmu_domain = domain->priv;
1071 * Free the domain resources. We assume that all devices have
1072 * already been detached.
1074 arm_smmu_destroy_domain_context(domain);
1075 arm_smmu_free_pgtables(smmu_domain);
1079 static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
1080 struct arm_smmu_master_cfg *cfg)
1083 struct arm_smmu_smr *smrs;
1084 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1086 if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH))
1092 smrs = kmalloc_array(cfg->num_streamids, sizeof(*smrs), GFP_KERNEL);
1094 dev_err(smmu->dev, "failed to allocate %d SMRs\n",
1095 cfg->num_streamids);
1099 /* Allocate the SMRs on the SMMU */
1100 for (i = 0; i < cfg->num_streamids; ++i) {
1101 int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
1102 smmu->num_mapping_groups);
1103 if (IS_ERR_VALUE(idx)) {
1104 dev_err(smmu->dev, "failed to allocate free SMR\n");
1108 smrs[i] = (struct arm_smmu_smr) {
1110 .mask = 0, /* We don't currently share SMRs */
1111 .id = cfg->streamids[i],
1115 /* It worked! Now, poke the actual hardware */
1116 for (i = 0; i < cfg->num_streamids; ++i) {
1117 u32 reg = SMR_VALID | smrs[i].id << SMR_ID_SHIFT |
1118 smrs[i].mask << SMR_MASK_SHIFT;
1119 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_SMR(smrs[i].idx));
1127 __arm_smmu_free_bitmap(smmu->smr_map, smrs[i].idx);
1132 static void arm_smmu_master_free_smrs(struct arm_smmu_device *smmu,
1133 struct arm_smmu_master_cfg *cfg)
1136 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1137 struct arm_smmu_smr *smrs = cfg->smrs;
1142 /* Invalidate the SMRs before freeing back to the allocator */
1143 for (i = 0; i < cfg->num_streamids; ++i) {
1144 u8 idx = smrs[i].idx;
1146 writel_relaxed(~SMR_VALID, gr0_base + ARM_SMMU_GR0_SMR(idx));
1147 __arm_smmu_free_bitmap(smmu->smr_map, idx);
1154 static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1155 struct arm_smmu_master_cfg *cfg)
1158 struct arm_smmu_device *smmu = smmu_domain->smmu;
1159 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1161 /* Devices in an IOMMU group may already be configured */
1162 ret = arm_smmu_master_configure_smrs(smmu, cfg);
1164 return ret == -EEXIST ? 0 : ret;
1166 for (i = 0; i < cfg->num_streamids; ++i) {
1169 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1170 s2cr = S2CR_TYPE_TRANS |
1171 (smmu_domain->cfg.cbndx << S2CR_CBNDX_SHIFT);
1172 writel_relaxed(s2cr, gr0_base + ARM_SMMU_GR0_S2CR(idx));
1178 static void arm_smmu_domain_remove_master(struct arm_smmu_domain *smmu_domain,
1179 struct arm_smmu_master_cfg *cfg)
1182 struct arm_smmu_device *smmu = smmu_domain->smmu;
1183 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1185 /* An IOMMU group is torn down by the first device to be removed */
1186 if ((smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) && !cfg->smrs)
1190 * We *must* clear the S2CR first, because freeing the SMR means
1191 * that it can be re-allocated immediately.
1193 for (i = 0; i < cfg->num_streamids; ++i) {
1194 u32 idx = cfg->smrs ? cfg->smrs[i].idx : cfg->streamids[i];
1196 writel_relaxed(S2CR_TYPE_BYPASS,
1197 gr0_base + ARM_SMMU_GR0_S2CR(idx));
1200 arm_smmu_master_free_smrs(smmu, cfg);
1203 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1206 struct arm_smmu_domain *smmu_domain = domain->priv;
1207 struct arm_smmu_device *smmu, *dom_smmu;
1208 struct arm_smmu_master_cfg *cfg;
1210 smmu = find_smmu_for_device(dev);
1212 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1216 if (dev->archdata.iommu) {
1217 dev_err(dev, "already attached to IOMMU domain\n");
1222 * Sanity check the domain. We don't support domains across
1225 dom_smmu = ACCESS_ONCE(smmu_domain->smmu);
1227 /* Now that we have a master, we can finalise the domain */
1228 ret = arm_smmu_init_domain_context(domain, smmu);
1229 if (IS_ERR_VALUE(ret))
1232 dom_smmu = smmu_domain->smmu;
1235 if (dom_smmu != smmu) {
1237 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1238 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1242 /* Looks ok, so add the device to the domain */
1243 cfg = find_smmu_master_cfg(dev);
1247 ret = arm_smmu_domain_add_master(smmu_domain, cfg);
1249 dev->archdata.iommu = domain;
1253 static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1255 struct arm_smmu_domain *smmu_domain = domain->priv;
1256 struct arm_smmu_master_cfg *cfg;
1258 cfg = find_smmu_master_cfg(dev);
1262 dev->archdata.iommu = NULL;
1263 arm_smmu_domain_remove_master(smmu_domain, cfg);
1266 static bool arm_smmu_pte_is_contiguous_range(unsigned long addr,
1269 return !(addr & ~ARM_SMMU_PTE_CONT_MASK) &&
1270 (addr + ARM_SMMU_PTE_CONT_SIZE <= end);
1273 static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd,
1274 unsigned long addr, unsigned long end,
1275 unsigned long pfn, int prot, int stage)
1278 pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF | ARM_SMMU_PTE_XN;
1280 if (pmd_none(*pmd)) {
1281 /* Allocate a new set of tables */
1282 pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO);
1287 arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE);
1288 pmd_populate(NULL, pmd, table);
1289 arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd));
1293 pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG;
1294 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
1295 pteval |= ARM_SMMU_PTE_AP_RDONLY;
1297 if (prot & IOMMU_CACHE)
1298 pteval |= (MAIR_ATTR_IDX_CACHE <<
1299 ARM_SMMU_PTE_ATTRINDX_SHIFT);
1301 pteval |= ARM_SMMU_PTE_HAP_FAULT;
1302 if (prot & IOMMU_READ)
1303 pteval |= ARM_SMMU_PTE_HAP_READ;
1304 if (prot & IOMMU_WRITE)
1305 pteval |= ARM_SMMU_PTE_HAP_WRITE;
1306 if (prot & IOMMU_CACHE)
1307 pteval |= ARM_SMMU_PTE_MEMATTR_OIWB;
1309 pteval |= ARM_SMMU_PTE_MEMATTR_NC;
1312 /* If no access, create a faulting entry to avoid TLB fills */
1313 if (prot & IOMMU_EXEC)
1314 pteval &= ~ARM_SMMU_PTE_XN;
1315 else if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
1316 pteval &= ~ARM_SMMU_PTE_PAGE;
1318 pteval |= ARM_SMMU_PTE_SH_IS;
1319 start = pmd_page_vaddr(*pmd) + pte_index(addr);
1323 * Install the page table entries. This is fairly complicated
1324 * since we attempt to make use of the contiguous hint in the
1325 * ptes where possible. The contiguous hint indicates a series
1326 * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically
1327 * contiguous region with the following constraints:
1329 * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE
1330 * - Each pte in the region has the contiguous hint bit set
1332 * This complicates unmapping (also handled by this code, when
1333 * neither IOMMU_READ or IOMMU_WRITE are set) because it is
1334 * possible, yet highly unlikely, that a client may unmap only
1335 * part of a contiguous range. This requires clearing of the
1336 * contiguous hint bits in the range before installing the new
1339 * Note that re-mapping an address range without first unmapping
1340 * it is not supported, so TLB invalidation is not required here
1341 * and is instead performed at unmap and domain-init time.
1346 pteval &= ~ARM_SMMU_PTE_CONT;
1348 if (arm_smmu_pte_is_contiguous_range(addr, end)) {
1349 i = ARM_SMMU_PTE_CONT_ENTRIES;
1350 pteval |= ARM_SMMU_PTE_CONT;
1351 } else if (pte_val(*pte) &
1352 (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) {
1355 unsigned long idx = pte_index(addr);
1357 idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1);
1358 cont_start = pmd_page_vaddr(*pmd) + idx;
1359 for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j)
1360 pte_val(*(cont_start + j)) &=
1363 arm_smmu_flush_pgtable(smmu, cont_start,
1365 ARM_SMMU_PTE_CONT_ENTRIES);
1369 *pte = pfn_pte(pfn, __pgprot(pteval));
1370 } while (pte++, pfn++, addr += PAGE_SIZE, --i);
1371 } while (addr != end);
1373 arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start));
1377 static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud,
1378 unsigned long addr, unsigned long end,
1379 phys_addr_t phys, int prot, int stage)
1383 unsigned long next, pfn = __phys_to_pfn(phys);
1385 #ifndef __PAGETABLE_PMD_FOLDED
1386 if (pud_none(*pud)) {
1387 pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
1391 arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE);
1392 pud_populate(NULL, pud, pmd);
1393 arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud));
1395 pmd += pmd_index(addr);
1398 pmd = pmd_offset(pud, addr);
1401 next = pmd_addr_end(addr, end);
1402 ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn,
1404 phys += next - addr;
1405 } while (pmd++, addr = next, addr < end);
1410 static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd,
1411 unsigned long addr, unsigned long end,
1412 phys_addr_t phys, int prot, int stage)
1418 #ifndef __PAGETABLE_PUD_FOLDED
1419 if (pgd_none(*pgd)) {
1420 pud = (pud_t *)get_zeroed_page(GFP_ATOMIC);
1424 arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE);
1425 pgd_populate(NULL, pgd, pud);
1426 arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd));
1428 pud += pud_index(addr);
1431 pud = pud_offset(pgd, addr);
1434 next = pud_addr_end(addr, end);
1435 ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys,
1437 phys += next - addr;
1438 } while (pud++, addr = next, addr < end);
1443 static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain,
1444 unsigned long iova, phys_addr_t paddr,
1445 size_t size, int prot)
1449 phys_addr_t input_mask, output_mask;
1450 struct arm_smmu_device *smmu = smmu_domain->smmu;
1451 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1452 pgd_t *pgd = cfg->pgd;
1453 unsigned long flags;
1455 if (cfg->cbar == CBAR_TYPE_S2_TRANS) {
1457 output_mask = (1ULL << smmu->s2_output_size) - 1;
1460 output_mask = (1ULL << smmu->s1_output_size) - 1;
1466 if (size & ~PAGE_MASK)
1469 input_mask = (1ULL << smmu->input_size) - 1;
1470 if ((phys_addr_t)iova & ~input_mask)
1473 if (paddr & ~output_mask)
1476 spin_lock_irqsave(&smmu_domain->lock, flags);
1477 pgd += pgd_index(iova);
1480 unsigned long next = pgd_addr_end(iova, end);
1482 ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr,
1487 paddr += next - iova;
1489 } while (pgd++, iova != end);
1492 spin_unlock_irqrestore(&smmu_domain->lock, flags);
1497 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1498 phys_addr_t paddr, size_t size, int prot)
1500 struct arm_smmu_domain *smmu_domain = domain->priv;
1505 return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, prot);
1508 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1512 struct arm_smmu_domain *smmu_domain = domain->priv;
1514 ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0);
1515 arm_smmu_tlb_inv_context(smmu_domain);
1516 return ret ? 0 : size;
1519 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1526 struct arm_smmu_domain *smmu_domain = domain->priv;
1527 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1533 pgd = *(pgdp + pgd_index(iova));
1537 pud = *pud_offset(&pgd, iova);
1541 pmd = *pmd_offset(&pud, iova);
1545 pte = *(pmd_page_vaddr(pmd) + pte_index(iova));
1549 return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK);
1552 static int arm_smmu_domain_has_cap(struct iommu_domain *domain,
1555 struct arm_smmu_domain *smmu_domain = domain->priv;
1556 struct arm_smmu_device *smmu = smmu_domain->smmu;
1557 u32 features = smmu ? smmu->features : 0;
1560 case IOMMU_CAP_CACHE_COHERENCY:
1561 return features & ARM_SMMU_FEAT_COHERENT_WALK;
1562 case IOMMU_CAP_INTR_REMAP:
1563 return 1; /* MSIs are just memory writes */
1569 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
1571 *((u16 *)data) = alias;
1572 return 0; /* Continue walking */
1575 static void __arm_smmu_release_pci_iommudata(void *data)
1580 static int arm_smmu_add_device(struct device *dev)
1582 struct arm_smmu_device *smmu;
1583 struct arm_smmu_master_cfg *cfg;
1584 struct iommu_group *group;
1585 void (*releasefn)(void *) = NULL;
1588 smmu = find_smmu_for_device(dev);
1592 group = iommu_group_alloc();
1593 if (IS_ERR(group)) {
1594 dev_err(dev, "Failed to allocate IOMMU group\n");
1595 return PTR_ERR(group);
1598 if (dev_is_pci(dev)) {
1599 struct pci_dev *pdev = to_pci_dev(dev);
1601 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1607 cfg->num_streamids = 1;
1609 * Assume Stream ID == Requester ID for now.
1610 * We need a way to describe the ID mappings in FDT.
1612 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid,
1613 &cfg->streamids[0]);
1614 releasefn = __arm_smmu_release_pci_iommudata;
1616 struct arm_smmu_master *master;
1618 master = find_smmu_master(smmu, dev->of_node);
1627 iommu_group_set_iommudata(group, cfg, releasefn);
1628 ret = iommu_group_add_device(group, dev);
1631 iommu_group_put(group);
1635 static void arm_smmu_remove_device(struct device *dev)
1637 iommu_group_remove_device(dev);
1640 static const struct iommu_ops arm_smmu_ops = {
1641 .domain_init = arm_smmu_domain_init,
1642 .domain_destroy = arm_smmu_domain_destroy,
1643 .attach_dev = arm_smmu_attach_dev,
1644 .detach_dev = arm_smmu_detach_dev,
1645 .map = arm_smmu_map,
1646 .unmap = arm_smmu_unmap,
1647 .iova_to_phys = arm_smmu_iova_to_phys,
1648 .domain_has_cap = arm_smmu_domain_has_cap,
1649 .add_device = arm_smmu_add_device,
1650 .remove_device = arm_smmu_remove_device,
1651 .pgsize_bitmap = (SECTION_SIZE |
1652 ARM_SMMU_PTE_CONT_SIZE |
1656 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1658 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1659 void __iomem *cb_base;
1663 /* clear global FSR */
1664 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1665 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1667 /* Mark all SMRn as invalid and all S2CRn as bypass */
1668 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1669 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_SMR(i));
1670 writel_relaxed(S2CR_TYPE_BYPASS,
1671 gr0_base + ARM_SMMU_GR0_S2CR(i));
1674 /* Make sure all context banks are disabled and clear CB_FSR */
1675 for (i = 0; i < smmu->num_context_banks; ++i) {
1676 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, i);
1677 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
1678 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
1681 /* Invalidate the TLB, just in case */
1682 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL);
1683 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1684 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
1686 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
1688 /* Enable fault reporting */
1689 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
1691 /* Disable TLB broadcasting. */
1692 reg |= (sCR0_VMIDPNE | sCR0_PTM);
1694 /* Enable client access, but bypass when no mapping is found */
1695 reg &= ~(sCR0_CLIENTPD | sCR0_USFCFG);
1697 /* Disable forced broadcasting */
1700 /* Don't upgrade barriers */
1701 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
1703 /* Push the button */
1704 arm_smmu_tlb_sync(smmu);
1705 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
1708 static int arm_smmu_id_size_to_bits(int size)
1727 static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1730 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1733 dev_notice(smmu->dev, "probing hardware configuration...\n");
1736 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_PIDR2);
1737 smmu->version = ((id >> PIDR2_ARCH_SHIFT) & PIDR2_ARCH_MASK) + 1;
1738 dev_notice(smmu->dev, "SMMUv%d with:\n", smmu->version);
1741 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
1742 #ifndef CONFIG_64BIT
1743 if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) {
1744 dev_err(smmu->dev, "\tno v7 descriptor support!\n");
1749 /* Restrict available stages based on module parameter */
1750 if (force_stage == 1)
1751 id &= ~(ID0_S2TS | ID0_NTS);
1752 else if (force_stage == 2)
1753 id &= ~(ID0_S1TS | ID0_NTS);
1755 if (id & ID0_S1TS) {
1756 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1757 dev_notice(smmu->dev, "\tstage 1 translation\n");
1760 if (id & ID0_S2TS) {
1761 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1762 dev_notice(smmu->dev, "\tstage 2 translation\n");
1766 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1767 dev_notice(smmu->dev, "\tnested translation\n");
1770 if (!(smmu->features &
1771 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1772 dev_err(smmu->dev, "\tno translation support!\n");
1776 if (id & ID0_CTTW) {
1777 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1778 dev_notice(smmu->dev, "\tcoherent table walk\n");
1784 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1785 smmu->num_mapping_groups = (id >> ID0_NUMSMRG_SHIFT) &
1787 if (smmu->num_mapping_groups == 0) {
1789 "stream-matching supported, but no SMRs present!\n");
1793 smr = SMR_MASK_MASK << SMR_MASK_SHIFT;
1794 smr |= (SMR_ID_MASK << SMR_ID_SHIFT);
1795 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1796 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1798 mask = (smr >> SMR_MASK_SHIFT) & SMR_MASK_MASK;
1799 sid = (smr >> SMR_ID_SHIFT) & SMR_ID_MASK;
1800 if ((mask & sid) != sid) {
1802 "SMR mask bits (0x%x) insufficient for ID field (0x%x)\n",
1807 dev_notice(smmu->dev,
1808 "\tstream matching with %u register groups, mask 0x%x",
1809 smmu->num_mapping_groups, mask);
1811 smmu->num_mapping_groups = (id >> ID0_NUMSIDB_SHIFT) &
1816 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
1817 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
1819 /* Check for size mismatch of SMMU address space from mapped region */
1821 (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
1822 size *= 2 << smmu->pgshift;
1823 if (smmu->size != size)
1825 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n",
1828 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) &
1830 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1831 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1832 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1835 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1836 smmu->num_context_banks, smmu->num_s2_context_banks);
1839 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1840 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
1843 * Stage-1 output limited by stage-2 input size due to pgd
1844 * allocation (PTRS_PER_PGD).
1846 if (smmu->features & ARM_SMMU_FEAT_TRANS_NESTED) {
1848 smmu->s1_output_size = min_t(unsigned long, VA_BITS, size);
1850 smmu->s1_output_size = min(32UL, size);
1853 smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT,
1857 /* The stage-2 output mask is also applied for bypass */
1858 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
1859 smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size);
1861 if (smmu->version == 1) {
1862 smmu->input_size = 32;
1865 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
1866 size = min(VA_BITS, arm_smmu_id_size_to_bits(size));
1870 smmu->input_size = size;
1872 if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) ||
1873 (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) ||
1874 (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) {
1875 dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n",
1881 dev_notice(smmu->dev,
1882 "\t%lu-bit VA, %lu-bit IPA, %lu-bit PA\n",
1883 smmu->input_size, smmu->s1_output_size,
1884 smmu->s2_output_size);
1888 static int arm_smmu_device_dt_probe(struct platform_device *pdev)
1890 struct resource *res;
1891 struct arm_smmu_device *smmu;
1892 struct device *dev = &pdev->dev;
1893 struct rb_node *node;
1894 struct of_phandle_args masterspec;
1895 int num_irqs, i, err;
1897 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
1899 dev_err(dev, "failed to allocate arm_smmu_device\n");
1904 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1905 smmu->base = devm_ioremap_resource(dev, res);
1906 if (IS_ERR(smmu->base))
1907 return PTR_ERR(smmu->base);
1908 smmu->size = resource_size(res);
1910 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1911 &smmu->num_global_irqs)) {
1912 dev_err(dev, "missing #global-interrupts property\n");
1917 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
1919 if (num_irqs > smmu->num_global_irqs)
1920 smmu->num_context_irqs++;
1923 if (!smmu->num_context_irqs) {
1924 dev_err(dev, "found %d interrupts but expected at least %d\n",
1925 num_irqs, smmu->num_global_irqs + 1);
1929 smmu->irqs = devm_kzalloc(dev, sizeof(*smmu->irqs) * num_irqs,
1932 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
1936 for (i = 0; i < num_irqs; ++i) {
1937 int irq = platform_get_irq(pdev, i);
1940 dev_err(dev, "failed to get irq index %d\n", i);
1943 smmu->irqs[i] = irq;
1946 err = arm_smmu_device_cfg_probe(smmu);
1951 smmu->masters = RB_ROOT;
1952 while (!of_parse_phandle_with_args(dev->of_node, "mmu-masters",
1953 "#stream-id-cells", i,
1955 err = register_smmu_master(smmu, dev, &masterspec);
1957 dev_err(dev, "failed to add master %s\n",
1958 masterspec.np->name);
1959 goto out_put_masters;
1964 dev_notice(dev, "registered %d master devices\n", i);
1966 parse_driver_options(smmu);
1968 if (smmu->version > 1 &&
1969 smmu->num_context_banks != smmu->num_context_irqs) {
1971 "found only %d context interrupt(s) but %d required\n",
1972 smmu->num_context_irqs, smmu->num_context_banks);
1974 goto out_put_masters;
1977 for (i = 0; i < smmu->num_global_irqs; ++i) {
1978 err = request_irq(smmu->irqs[i],
1979 arm_smmu_global_fault,
1981 "arm-smmu global fault",
1984 dev_err(dev, "failed to request global IRQ %d (%u)\n",
1990 INIT_LIST_HEAD(&smmu->list);
1991 spin_lock(&arm_smmu_devices_lock);
1992 list_add(&smmu->list, &arm_smmu_devices);
1993 spin_unlock(&arm_smmu_devices_lock);
1995 arm_smmu_device_reset(smmu);
2000 free_irq(smmu->irqs[i], smmu);
2003 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
2004 struct arm_smmu_master *master
2005 = container_of(node, struct arm_smmu_master, node);
2006 of_node_put(master->of_node);
2012 static int arm_smmu_device_remove(struct platform_device *pdev)
2015 struct device *dev = &pdev->dev;
2016 struct arm_smmu_device *curr, *smmu = NULL;
2017 struct rb_node *node;
2019 spin_lock(&arm_smmu_devices_lock);
2020 list_for_each_entry(curr, &arm_smmu_devices, list) {
2021 if (curr->dev == dev) {
2023 list_del(&smmu->list);
2027 spin_unlock(&arm_smmu_devices_lock);
2032 for (node = rb_first(&smmu->masters); node; node = rb_next(node)) {
2033 struct arm_smmu_master *master
2034 = container_of(node, struct arm_smmu_master, node);
2035 of_node_put(master->of_node);
2038 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
2039 dev_err(dev, "removing device with active domains!\n");
2041 for (i = 0; i < smmu->num_global_irqs; ++i)
2042 free_irq(smmu->irqs[i], smmu);
2044 /* Turn the thing off */
2045 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
2050 static struct of_device_id arm_smmu_of_match[] = {
2051 { .compatible = "arm,smmu-v1", },
2052 { .compatible = "arm,smmu-v2", },
2053 { .compatible = "arm,mmu-400", },
2054 { .compatible = "arm,mmu-500", },
2057 MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2060 static struct platform_driver arm_smmu_driver = {
2062 .owner = THIS_MODULE,
2064 .of_match_table = of_match_ptr(arm_smmu_of_match),
2066 .probe = arm_smmu_device_dt_probe,
2067 .remove = arm_smmu_device_remove,
2070 static int __init arm_smmu_init(void)
2074 ret = platform_driver_register(&arm_smmu_driver);
2078 /* Oh, for a proper bus abstraction */
2079 if (!iommu_present(&platform_bus_type))
2080 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2082 #ifdef CONFIG_ARM_AMBA
2083 if (!iommu_present(&amba_bustype))
2084 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2088 if (!iommu_present(&pci_bus_type))
2089 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2095 static void __exit arm_smmu_exit(void)
2097 return platform_driver_unregister(&arm_smmu_driver);
2100 subsys_initcall(arm_smmu_init);
2101 module_exit(arm_smmu_exit);
2103 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2104 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2105 MODULE_LICENSE("GPL v2");