1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * IOMMU API for ARM architected SMMU implementations.
4  *
5  * Copyright (C) 2013 ARM Limited
6  *
7  * Author: Will Deacon <will.deacon@arm.com>
8  *
9  * This driver currently supports:
10  *	- SMMUv1 and v2 implementations
11  *	- Stream-matching and stream-indexing
12  *	- v7/v8 long-descriptor format
13  *	- Non-secure access to the SMMU
14  *	- Context fault reporting
15  *	- Extended Stream ID (16 bit)
16  */
17 
18 #define pr_fmt(fmt) "arm-smmu: " fmt
19 
20 #include <linux/acpi.h>
21 #include <linux/acpi_iort.h>
22 #include <linux/bitfield.h>
23 #include <linux/delay.h>
24 #include <linux/dma-iommu.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
28 #include <linux/io.h>
29 #include <linux/iopoll.h>
30 #include <linux/module.h>
31 #include <linux/of.h>
32 #include <linux/of_address.h>
33 #include <linux/of_device.h>
34 #include <linux/of_iommu.h>
35 #include <linux/pci.h>
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/ratelimit.h>
39 #include <linux/slab.h>
40 
41 #include <linux/amba/bus.h>
42 #include <linux/fsl/mc.h>
43 
44 #include "arm-smmu.h"
45 
46 /*
47  * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
48  * global register space are still, in fact, using a hypervisor to mediate it
49  * by trapping and emulating register accesses. Sadly, some deployed versions
50  * of said trapping code have bugs wherein they go horribly wrong for stores
51  * using r31 (i.e. XZR/WZR) as the source register.
52  */
53 #define QCOM_DUMMY_VAL -1
54 
55 #define MSI_IOVA_BASE			0x8000000
56 #define MSI_IOVA_LENGTH			0x100000
57 
58 static int force_stage;
59 module_param(force_stage, int, S_IRUGO);
60 MODULE_PARM_DESC(force_stage,
61 	"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.");
62 static bool disable_bypass =
63 	IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT);
64 module_param(disable_bypass, bool, S_IRUGO);
65 MODULE_PARM_DESC(disable_bypass,
66 	"Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
67 
68 #define s2cr_init_val (struct arm_smmu_s2cr){				\
69 	.type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS,	\
70 }
71 
72 static bool using_legacy_binding, using_generic_binding;
73 
74 static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
75 {
76 	if (pm_runtime_enabled(smmu->dev))
77 		return pm_runtime_get_sync(smmu->dev);
78 
79 	return 0;
80 }
81 
82 static inline void arm_smmu_rpm_put(struct arm_smmu_device *smmu)
83 {
84 	if (pm_runtime_enabled(smmu->dev))
85 		pm_runtime_put_autosuspend(smmu->dev);
86 }
87 
88 static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
89 {
90 	return container_of(dom, struct arm_smmu_domain, domain);
91 }
92 
93 static struct platform_driver arm_smmu_driver;
94 static struct iommu_ops arm_smmu_ops;
95 
96 #ifdef CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS
97 static int arm_smmu_bus_init(struct iommu_ops *ops);
98 
99 static struct device_node *dev_get_dev_node(struct device *dev)
100 {
101 	if (dev_is_pci(dev)) {
102 		struct pci_bus *bus = to_pci_dev(dev)->bus;
103 
104 		while (!pci_is_root_bus(bus))
105 			bus = bus->parent;
106 		return of_node_get(bus->bridge->parent->of_node);
107 	}
108 
109 	return of_node_get(dev->of_node);
110 }
111 
112 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
113 {
114 	*((__be32 *)data) = cpu_to_be32(alias);
115 	return 0; /* Continue walking */
116 }
117 
118 static int __find_legacy_master_phandle(struct device *dev, void *data)
119 {
120 	struct of_phandle_iterator *it = *(void **)data;
121 	struct device_node *np = it->node;
122 	int err;
123 
124 	of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
125 			    "#stream-id-cells", -1)
126 		if (it->node == np) {
127 			*(void **)data = dev;
128 			return 1;
129 		}
130 	it->node = np;
131 	return err == -ENOENT ? 0 : err;
132 }
133 
134 static int arm_smmu_register_legacy_master(struct device *dev,
135 					   struct arm_smmu_device **smmu)
136 {
137 	struct device *smmu_dev;
138 	struct device_node *np;
139 	struct of_phandle_iterator it;
140 	void *data = &it;
141 	u32 *sids;
142 	__be32 pci_sid;
143 	int err;
144 
145 	np = dev_get_dev_node(dev);
146 	if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
147 		of_node_put(np);
148 		return -ENODEV;
149 	}
150 
151 	it.node = np;
152 	err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
153 				     __find_legacy_master_phandle);
154 	smmu_dev = data;
155 	of_node_put(np);
156 	if (err == 0)
157 		return -ENODEV;
158 	if (err < 0)
159 		return err;
160 
161 	if (dev_is_pci(dev)) {
162 		/* "mmu-masters" assumes Stream ID == Requester ID */
163 		pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
164 				       &pci_sid);
165 		it.cur = &pci_sid;
166 		it.cur_count = 1;
167 	}
168 
169 	err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
170 				&arm_smmu_ops);
171 	if (err)
172 		return err;
173 
174 	sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
175 	if (!sids)
176 		return -ENOMEM;
177 
178 	*smmu = dev_get_drvdata(smmu_dev);
179 	of_phandle_iterator_args(&it, sids, it.cur_count);
180 	err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
181 	kfree(sids);
182 	return err;
183 }
184 
185 /*
186  * With the legacy DT binding in play, we have no guarantees about
187  * probe order, but then we're also not doing default domains, so we can
188  * delay setting bus ops until we're sure every possible SMMU is ready,
189  * and that way ensure that no probe_device() calls get missed.
190  */
191 static int arm_smmu_legacy_bus_init(void)
192 {
193 	if (using_legacy_binding)
194 		return arm_smmu_bus_init(&arm_smmu_ops);
195 	return 0;
196 }
197 device_initcall_sync(arm_smmu_legacy_bus_init);
198 #else
199 static int arm_smmu_register_legacy_master(struct device *dev,
200 					   struct arm_smmu_device **smmu)
201 {
202 	return -ENODEV;
203 }
204 #endif /* CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS */
205 
206 static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
207 {
208 	clear_bit(idx, map);
209 }
210 
211 /* Wait for any pending TLB invalidations to complete */
212 static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
213 				int sync, int status)
214 {
215 	unsigned int spin_cnt, delay;
216 	u32 reg;
217 
218 	if (smmu->impl && unlikely(smmu->impl->tlb_sync))
219 		return smmu->impl->tlb_sync(smmu, page, sync, status);
220 
221 	arm_smmu_writel(smmu, page, sync, QCOM_DUMMY_VAL);
222 	for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
223 		for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
224 			reg = arm_smmu_readl(smmu, page, status);
225 			if (!(reg & ARM_SMMU_sTLBGSTATUS_GSACTIVE))
226 				return;
227 			cpu_relax();
228 		}
229 		udelay(delay);
230 	}
231 	dev_err_ratelimited(smmu->dev,
232 			    "TLB sync timed out -- SMMU may be deadlocked\n");
233 }
234 
235 static void arm_smmu_tlb_sync_global(struct arm_smmu_device *smmu)
236 {
237 	unsigned long flags;
238 
239 	spin_lock_irqsave(&smmu->global_sync_lock, flags);
240 	__arm_smmu_tlb_sync(smmu, ARM_SMMU_GR0, ARM_SMMU_GR0_sTLBGSYNC,
241 			    ARM_SMMU_GR0_sTLBGSTATUS);
242 	spin_unlock_irqrestore(&smmu->global_sync_lock, flags);
243 }
244 
245 static void arm_smmu_tlb_sync_context(struct arm_smmu_domain *smmu_domain)
246 {
247 	struct arm_smmu_device *smmu = smmu_domain->smmu;
248 	unsigned long flags;
249 
250 	spin_lock_irqsave(&smmu_domain->cb_lock, flags);
251 	__arm_smmu_tlb_sync(smmu, ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx),
252 			    ARM_SMMU_CB_TLBSYNC, ARM_SMMU_CB_TLBSTATUS);
253 	spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
254 }
255 
256 static void arm_smmu_tlb_inv_context_s1(void *cookie)
257 {
258 	struct arm_smmu_domain *smmu_domain = cookie;
259 	/*
260 	 * The TLBI write may be relaxed, so ensure that PTEs cleared by the
261 	 * current CPU are visible beforehand.
262 	 */
263 	wmb();
264 	arm_smmu_cb_write(smmu_domain->smmu, smmu_domain->cfg.cbndx,
265 			  ARM_SMMU_CB_S1_TLBIASID, smmu_domain->cfg.asid);
266 	arm_smmu_tlb_sync_context(smmu_domain);
267 }
268 
269 static void arm_smmu_tlb_inv_context_s2(void *cookie)
270 {
271 	struct arm_smmu_domain *smmu_domain = cookie;
272 	struct arm_smmu_device *smmu = smmu_domain->smmu;
273 
274 	/* See above */
275 	wmb();
276 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
277 	arm_smmu_tlb_sync_global(smmu);
278 }
279 
280 static void arm_smmu_tlb_inv_range_s1(unsigned long iova, size_t size,
281 				      size_t granule, void *cookie, int reg)
282 {
283 	struct arm_smmu_domain *smmu_domain = cookie;
284 	struct arm_smmu_device *smmu = smmu_domain->smmu;
285 	struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
286 	int idx = cfg->cbndx;
287 
288 	if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
289 		wmb();
290 
291 	if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
292 		iova = (iova >> 12) << 12;
293 		iova |= cfg->asid;
294 		do {
295 			arm_smmu_cb_write(smmu, idx, reg, iova);
296 			iova += granule;
297 		} while (size -= granule);
298 	} else {
299 		iova >>= 12;
300 		iova |= (u64)cfg->asid << 48;
301 		do {
302 			arm_smmu_cb_writeq(smmu, idx, reg, iova);
303 			iova += granule >> 12;
304 		} while (size -= granule);
305 	}
306 }
307 
308 static void arm_smmu_tlb_inv_range_s2(unsigned long iova, size_t size,
309 				      size_t granule, void *cookie, int reg)
310 {
311 	struct arm_smmu_domain *smmu_domain = cookie;
312 	struct arm_smmu_device *smmu = smmu_domain->smmu;
313 	int idx = smmu_domain->cfg.cbndx;
314 
315 	if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
316 		wmb();
317 
318 	iova >>= 12;
319 	do {
320 		if (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64)
321 			arm_smmu_cb_writeq(smmu, idx, reg, iova);
322 		else
323 			arm_smmu_cb_write(smmu, idx, reg, iova);
324 		iova += granule >> 12;
325 	} while (size -= granule);
326 }
327 
328 static void arm_smmu_tlb_inv_walk_s1(unsigned long iova, size_t size,
329 				     size_t granule, void *cookie)
330 {
331 	arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
332 				  ARM_SMMU_CB_S1_TLBIVA);
333 	arm_smmu_tlb_sync_context(cookie);
334 }
335 
336 static void arm_smmu_tlb_inv_leaf_s1(unsigned long iova, size_t size,
337 				     size_t granule, void *cookie)
338 {
339 	arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
340 				  ARM_SMMU_CB_S1_TLBIVAL);
341 	arm_smmu_tlb_sync_context(cookie);
342 }
343 
344 static void arm_smmu_tlb_add_page_s1(struct iommu_iotlb_gather *gather,
345 				     unsigned long iova, size_t granule,
346 				     void *cookie)
347 {
348 	arm_smmu_tlb_inv_range_s1(iova, granule, granule, cookie,
349 				  ARM_SMMU_CB_S1_TLBIVAL);
350 }
351 
352 static void arm_smmu_tlb_inv_walk_s2(unsigned long iova, size_t size,
353 				     size_t granule, void *cookie)
354 {
355 	arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
356 				  ARM_SMMU_CB_S2_TLBIIPAS2);
357 	arm_smmu_tlb_sync_context(cookie);
358 }
359 
360 static void arm_smmu_tlb_inv_leaf_s2(unsigned long iova, size_t size,
361 				     size_t granule, void *cookie)
362 {
363 	arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
364 				  ARM_SMMU_CB_S2_TLBIIPAS2L);
365 	arm_smmu_tlb_sync_context(cookie);
366 }
367 
368 static void arm_smmu_tlb_add_page_s2(struct iommu_iotlb_gather *gather,
369 				     unsigned long iova, size_t granule,
370 				     void *cookie)
371 {
372 	arm_smmu_tlb_inv_range_s2(iova, granule, granule, cookie,
373 				  ARM_SMMU_CB_S2_TLBIIPAS2L);
374 }
375 
376 static void arm_smmu_tlb_inv_any_s2_v1(unsigned long iova, size_t size,
377 				       size_t granule, void *cookie)
378 {
379 	arm_smmu_tlb_inv_context_s2(cookie);
380 }
381 /*
382  * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
383  * almost negligible, but the benefit of getting the first one in as far ahead
384  * of the sync as possible is significant, hence we don't just make this a
385  * no-op and call arm_smmu_tlb_inv_context_s2() from .iotlb_sync as you might
386  * think.
387  */
388 static void arm_smmu_tlb_add_page_s2_v1(struct iommu_iotlb_gather *gather,
389 					unsigned long iova, size_t granule,
390 					void *cookie)
391 {
392 	struct arm_smmu_domain *smmu_domain = cookie;
393 	struct arm_smmu_device *smmu = smmu_domain->smmu;
394 
395 	if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
396 		wmb();
397 
398 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
399 }
400 
401 static const struct iommu_flush_ops arm_smmu_s1_tlb_ops = {
402 	.tlb_flush_all	= arm_smmu_tlb_inv_context_s1,
403 	.tlb_flush_walk	= arm_smmu_tlb_inv_walk_s1,
404 	.tlb_flush_leaf	= arm_smmu_tlb_inv_leaf_s1,
405 	.tlb_add_page	= arm_smmu_tlb_add_page_s1,
406 };
407 
408 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v2 = {
409 	.tlb_flush_all	= arm_smmu_tlb_inv_context_s2,
410 	.tlb_flush_walk	= arm_smmu_tlb_inv_walk_s2,
411 	.tlb_flush_leaf	= arm_smmu_tlb_inv_leaf_s2,
412 	.tlb_add_page	= arm_smmu_tlb_add_page_s2,
413 };
414 
415 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v1 = {
416 	.tlb_flush_all	= arm_smmu_tlb_inv_context_s2,
417 	.tlb_flush_walk	= arm_smmu_tlb_inv_any_s2_v1,
418 	.tlb_flush_leaf	= arm_smmu_tlb_inv_any_s2_v1,
419 	.tlb_add_page	= arm_smmu_tlb_add_page_s2_v1,
420 };
421 
422 static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
423 {
424 	u32 fsr, fsynr, cbfrsynra;
425 	unsigned long iova;
426 	struct iommu_domain *domain = dev;
427 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
428 	struct arm_smmu_device *smmu = smmu_domain->smmu;
429 	int idx = smmu_domain->cfg.cbndx;
430 
431 	fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
432 	if (!(fsr & ARM_SMMU_FSR_FAULT))
433 		return IRQ_NONE;
434 
435 	fsynr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSYNR0);
436 	iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
437 	cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
438 
439 	dev_err_ratelimited(smmu->dev,
440 	"Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
441 			    fsr, iova, fsynr, cbfrsynra, idx);
442 
443 	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
444 	return IRQ_HANDLED;
445 }
446 
447 static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
448 {
449 	u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
450 	struct arm_smmu_device *smmu = dev;
451 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
452 				      DEFAULT_RATELIMIT_BURST);
453 
454 	gfsr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
455 	gfsynr0 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR0);
456 	gfsynr1 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR1);
457 	gfsynr2 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR2);
458 
459 	if (!gfsr)
460 		return IRQ_NONE;
461 
462 	if (__ratelimit(&rs)) {
463 		if (IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT) &&
464 		    (gfsr & ARM_SMMU_sGFSR_USF))
465 			dev_err(smmu->dev,
466 				"Blocked unknown Stream ID 0x%hx; boot with \"arm-smmu.disable_bypass=0\" to allow, but this may have security implications\n",
467 				(u16)gfsynr1);
468 		else
469 			dev_err(smmu->dev,
470 				"Unexpected global fault, this could be serious\n");
471 		dev_err(smmu->dev,
472 			"\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
473 			gfsr, gfsynr0, gfsynr1, gfsynr2);
474 	}
475 
476 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, gfsr);
477 	return IRQ_HANDLED;
478 }
479 
480 static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
481 				       struct io_pgtable_cfg *pgtbl_cfg)
482 {
483 	struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
484 	struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
485 	bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
486 
487 	cb->cfg = cfg;
488 
489 	/* TCR */
490 	if (stage1) {
491 		if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
492 			cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
493 		} else {
494 			cb->tcr[0] = arm_smmu_lpae_tcr(pgtbl_cfg);
495 			cb->tcr[1] = arm_smmu_lpae_tcr2(pgtbl_cfg);
496 			if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
497 				cb->tcr[1] |= ARM_SMMU_TCR2_AS;
498 			else
499 				cb->tcr[0] |= ARM_SMMU_TCR_EAE;
500 		}
501 	} else {
502 		cb->tcr[0] = arm_smmu_lpae_vtcr(pgtbl_cfg);
503 	}
504 
505 	/* TTBRs */
506 	if (stage1) {
507 		if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
508 			cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr;
509 			cb->ttbr[1] = 0;
510 		} else {
511 			cb->ttbr[0] = FIELD_PREP(ARM_SMMU_TTBRn_ASID,
512 						 cfg->asid);
513 			cb->ttbr[1] = FIELD_PREP(ARM_SMMU_TTBRn_ASID,
514 						 cfg->asid);
515 
516 			if (pgtbl_cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
517 				cb->ttbr[1] |= pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
518 			else
519 				cb->ttbr[0] |= pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
520 		}
521 	} else {
522 		cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
523 	}
524 
525 	/* MAIRs (stage-1 only) */
526 	if (stage1) {
527 		if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
528 			cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
529 			cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
530 		} else {
531 			cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair;
532 			cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair >> 32;
533 		}
534 	}
535 }
536 
537 void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
538 {
539 	u32 reg;
540 	bool stage1;
541 	struct arm_smmu_cb *cb = &smmu->cbs[idx];
542 	struct arm_smmu_cfg *cfg = cb->cfg;
543 
544 	/* Unassigned context banks only need disabling */
545 	if (!cfg) {
546 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, 0);
547 		return;
548 	}
549 
550 	stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
551 
552 	/* CBA2R */
553 	if (smmu->version > ARM_SMMU_V1) {
554 		if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
555 			reg = ARM_SMMU_CBA2R_VA64;
556 		else
557 			reg = 0;
558 		/* 16-bit VMIDs live in CBA2R */
559 		if (smmu->features & ARM_SMMU_FEAT_VMID16)
560 			reg |= FIELD_PREP(ARM_SMMU_CBA2R_VMID16, cfg->vmid);
561 
562 		arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBA2R(idx), reg);
563 	}
564 
565 	/* CBAR */
566 	reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, cfg->cbar);
567 	if (smmu->version < ARM_SMMU_V2)
568 		reg |= FIELD_PREP(ARM_SMMU_CBAR_IRPTNDX, cfg->irptndx);
569 
570 	/*
571 	 * Use the weakest shareability/memory types, so they are
572 	 * overridden by the ttbcr/pte.
573 	 */
574 	if (stage1) {
575 		reg |= FIELD_PREP(ARM_SMMU_CBAR_S1_BPSHCFG,
576 				  ARM_SMMU_CBAR_S1_BPSHCFG_NSH) |
577 		       FIELD_PREP(ARM_SMMU_CBAR_S1_MEMATTR,
578 				  ARM_SMMU_CBAR_S1_MEMATTR_WB);
579 	} else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
580 		/* 8-bit VMIDs live in CBAR */
581 		reg |= FIELD_PREP(ARM_SMMU_CBAR_VMID, cfg->vmid);
582 	}
583 	arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(idx), reg);
584 
585 	/*
586 	 * TCR
587 	 * We must write this before the TTBRs, since it determines the
588 	 * access behaviour of some fields (in particular, ASID[15:8]).
589 	 */
590 	if (stage1 && smmu->version > ARM_SMMU_V1)
591 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR2, cb->tcr[1]);
592 	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR, cb->tcr[0]);
593 
594 	/* TTBRs */
595 	if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
596 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_CONTEXTIDR, cfg->asid);
597 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
598 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR1, cb->ttbr[1]);
599 	} else {
600 		arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
601 		if (stage1)
602 			arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR1,
603 					   cb->ttbr[1]);
604 	}
605 
606 	/* MAIRs (stage-1 only) */
607 	if (stage1) {
608 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR0, cb->mair[0]);
609 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR1, cb->mair[1]);
610 	}
611 
612 	/* SCTLR */
613 	reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE | ARM_SMMU_SCTLR_AFE |
614 	      ARM_SMMU_SCTLR_TRE | ARM_SMMU_SCTLR_M;
615 	if (stage1)
616 		reg |= ARM_SMMU_SCTLR_S1_ASIDPNE;
617 	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
618 		reg |= ARM_SMMU_SCTLR_E;
619 
620 	arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
621 }
622 
623 static int arm_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
624 				       struct arm_smmu_device *smmu,
625 				       struct device *dev, unsigned int start)
626 {
627 	if (smmu->impl && smmu->impl->alloc_context_bank)
628 		return smmu->impl->alloc_context_bank(smmu_domain, smmu, dev, start);
629 
630 	return __arm_smmu_alloc_bitmap(smmu->context_map, start, smmu->num_context_banks);
631 }
632 
633 static int arm_smmu_init_domain_context(struct iommu_domain *domain,
634 					struct arm_smmu_device *smmu,
635 					struct device *dev)
636 {
637 	int irq, start, ret = 0;
638 	unsigned long ias, oas;
639 	struct io_pgtable_ops *pgtbl_ops;
640 	struct io_pgtable_cfg pgtbl_cfg;
641 	enum io_pgtable_fmt fmt;
642 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
643 	struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
644 	irqreturn_t (*context_fault)(int irq, void *dev);
645 
646 	mutex_lock(&smmu_domain->init_mutex);
647 	if (smmu_domain->smmu)
648 		goto out_unlock;
649 
650 	if (domain->type == IOMMU_DOMAIN_IDENTITY) {
651 		smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
652 		smmu_domain->smmu = smmu;
653 		goto out_unlock;
654 	}
655 
656 	/*
657 	 * Mapping the requested stage onto what we support is surprisingly
658 	 * complicated, mainly because the spec allows S1+S2 SMMUs without
659 	 * support for nested translation. That means we end up with the
660 	 * following table:
661 	 *
662 	 * Requested        Supported        Actual
663 	 *     S1               N              S1
664 	 *     S1             S1+S2            S1
665 	 *     S1               S2             S2
666 	 *     S1               S1             S1
667 	 *     N                N              N
668 	 *     N              S1+S2            S2
669 	 *     N                S2             S2
670 	 *     N                S1             S1
671 	 *
672 	 * Note that you can't actually request stage-2 mappings.
673 	 */
674 	if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
675 		smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
676 	if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
677 		smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
678 
679 	/*
680 	 * Choosing a suitable context format is even more fiddly. Until we
681 	 * grow some way for the caller to express a preference, and/or move
682 	 * the decision into the io-pgtable code where it arguably belongs,
683 	 * just aim for the closest thing to the rest of the system, and hope
684 	 * that the hardware isn't esoteric enough that we can't assume AArch64
685 	 * support to be a superset of AArch32 support...
686 	 */
687 	if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
688 		cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
689 	if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
690 	    !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
691 	    (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
692 	    (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
693 		cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
694 	if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
695 	    (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
696 			       ARM_SMMU_FEAT_FMT_AARCH64_16K |
697 			       ARM_SMMU_FEAT_FMT_AARCH64_4K)))
698 		cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
699 
700 	if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
701 		ret = -EINVAL;
702 		goto out_unlock;
703 	}
704 
705 	switch (smmu_domain->stage) {
706 	case ARM_SMMU_DOMAIN_S1:
707 		cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
708 		start = smmu->num_s2_context_banks;
709 		ias = smmu->va_size;
710 		oas = smmu->ipa_size;
711 		if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
712 			fmt = ARM_64_LPAE_S1;
713 		} else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
714 			fmt = ARM_32_LPAE_S1;
715 			ias = min(ias, 32UL);
716 			oas = min(oas, 40UL);
717 		} else {
718 			fmt = ARM_V7S;
719 			ias = min(ias, 32UL);
720 			oas = min(oas, 32UL);
721 		}
722 		smmu_domain->flush_ops = &arm_smmu_s1_tlb_ops;
723 		break;
724 	case ARM_SMMU_DOMAIN_NESTED:
725 		/*
726 		 * We will likely want to change this if/when KVM gets
727 		 * involved.
728 		 */
729 	case ARM_SMMU_DOMAIN_S2:
730 		cfg->cbar = CBAR_TYPE_S2_TRANS;
731 		start = 0;
732 		ias = smmu->ipa_size;
733 		oas = smmu->pa_size;
734 		if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
735 			fmt = ARM_64_LPAE_S2;
736 		} else {
737 			fmt = ARM_32_LPAE_S2;
738 			ias = min(ias, 40UL);
739 			oas = min(oas, 40UL);
740 		}
741 		if (smmu->version == ARM_SMMU_V2)
742 			smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v2;
743 		else
744 			smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v1;
745 		break;
746 	default:
747 		ret = -EINVAL;
748 		goto out_unlock;
749 	}
750 
751 	ret = arm_smmu_alloc_context_bank(smmu_domain, smmu, dev, start);
752 	if (ret < 0) {
753 		goto out_unlock;
754 	}
755 
756 	smmu_domain->smmu = smmu;
757 
758 	cfg->cbndx = ret;
759 	if (smmu->version < ARM_SMMU_V2) {
760 		cfg->irptndx = atomic_inc_return(&smmu->irptndx);
761 		cfg->irptndx %= smmu->num_context_irqs;
762 	} else {
763 		cfg->irptndx = cfg->cbndx;
764 	}
765 
766 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
767 		cfg->vmid = cfg->cbndx + 1;
768 	else
769 		cfg->asid = cfg->cbndx;
770 
771 	pgtbl_cfg = (struct io_pgtable_cfg) {
772 		.pgsize_bitmap	= smmu->pgsize_bitmap,
773 		.ias		= ias,
774 		.oas		= oas,
775 		.coherent_walk	= smmu->features & ARM_SMMU_FEAT_COHERENT_WALK,
776 		.tlb		= smmu_domain->flush_ops,
777 		.iommu_dev	= smmu->dev,
778 	};
779 
780 	if (smmu->impl && smmu->impl->init_context) {
781 		ret = smmu->impl->init_context(smmu_domain, &pgtbl_cfg, dev);
782 		if (ret)
783 			goto out_clear_smmu;
784 	}
785 
786 	if (smmu_domain->non_strict)
787 		pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
788 
789 	pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
790 	if (!pgtbl_ops) {
791 		ret = -ENOMEM;
792 		goto out_clear_smmu;
793 	}
794 
795 	/* Update the domain's page sizes to reflect the page table format */
796 	domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
797 
798 	if (pgtbl_cfg.quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) {
799 		domain->geometry.aperture_start = ~0UL << ias;
800 		domain->geometry.aperture_end = ~0UL;
801 	} else {
802 		domain->geometry.aperture_end = (1UL << ias) - 1;
803 	}
804 
805 	domain->geometry.force_aperture = true;
806 
807 	/* Initialise the context bank with our page table cfg */
808 	arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
809 	arm_smmu_write_context_bank(smmu, cfg->cbndx);
810 
811 	/*
812 	 * Request context fault interrupt. Do this last to avoid the
813 	 * handler seeing a half-initialised domain state.
814 	 */
815 	irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
816 
817 	if (smmu->impl && smmu->impl->context_fault)
818 		context_fault = smmu->impl->context_fault;
819 	else
820 		context_fault = arm_smmu_context_fault;
821 
822 	ret = devm_request_irq(smmu->dev, irq, context_fault,
823 			       IRQF_SHARED, "arm-smmu-context-fault", domain);
824 	if (ret < 0) {
825 		dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
826 			cfg->irptndx, irq);
827 		cfg->irptndx = ARM_SMMU_INVALID_IRPTNDX;
828 	}
829 
830 	mutex_unlock(&smmu_domain->init_mutex);
831 
832 	/* Publish page table ops for map/unmap */
833 	smmu_domain->pgtbl_ops = pgtbl_ops;
834 	return 0;
835 
836 out_clear_smmu:
837 	__arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
838 	smmu_domain->smmu = NULL;
839 out_unlock:
840 	mutex_unlock(&smmu_domain->init_mutex);
841 	return ret;
842 }
843 
844 static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
845 {
846 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
847 	struct arm_smmu_device *smmu = smmu_domain->smmu;
848 	struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
849 	int ret, irq;
850 
851 	if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
852 		return;
853 
854 	ret = arm_smmu_rpm_get(smmu);
855 	if (ret < 0)
856 		return;
857 
858 	/*
859 	 * Disable the context bank and free the page tables before freeing
860 	 * it.
861 	 */
862 	smmu->cbs[cfg->cbndx].cfg = NULL;
863 	arm_smmu_write_context_bank(smmu, cfg->cbndx);
864 
865 	if (cfg->irptndx != ARM_SMMU_INVALID_IRPTNDX) {
866 		irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
867 		devm_free_irq(smmu->dev, irq, domain);
868 	}
869 
870 	free_io_pgtable_ops(smmu_domain->pgtbl_ops);
871 	__arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
872 
873 	arm_smmu_rpm_put(smmu);
874 }
875 
876 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
877 {
878 	struct arm_smmu_domain *smmu_domain;
879 
880 	if (type != IOMMU_DOMAIN_UNMANAGED &&
881 	    type != IOMMU_DOMAIN_DMA &&
882 	    type != IOMMU_DOMAIN_IDENTITY)
883 		return NULL;
884 	/*
885 	 * Allocate the domain and initialise some of its data structures.
886 	 * We can't really do anything meaningful until we've added a
887 	 * master.
888 	 */
889 	smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
890 	if (!smmu_domain)
891 		return NULL;
892 
893 	if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
894 	    iommu_get_dma_cookie(&smmu_domain->domain))) {
895 		kfree(smmu_domain);
896 		return NULL;
897 	}
898 
899 	mutex_init(&smmu_domain->init_mutex);
900 	spin_lock_init(&smmu_domain->cb_lock);
901 
902 	return &smmu_domain->domain;
903 }
904 
905 static void arm_smmu_domain_free(struct iommu_domain *domain)
906 {
907 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
908 
909 	/*
910 	 * Free the domain resources. We assume that all devices have
911 	 * already been detached.
912 	 */
913 	iommu_put_dma_cookie(domain);
914 	arm_smmu_destroy_domain_context(domain);
915 	kfree(smmu_domain);
916 }
917 
918 static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
919 {
920 	struct arm_smmu_smr *smr = smmu->smrs + idx;
921 	u32 reg = FIELD_PREP(ARM_SMMU_SMR_ID, smr->id) |
922 		  FIELD_PREP(ARM_SMMU_SMR_MASK, smr->mask);
923 
924 	if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
925 		reg |= ARM_SMMU_SMR_VALID;
926 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(idx), reg);
927 }
928 
929 static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
930 {
931 	struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
932 	u32 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, s2cr->type) |
933 		  FIELD_PREP(ARM_SMMU_S2CR_CBNDX, s2cr->cbndx) |
934 		  FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
935 
936 	if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
937 	    smmu->smrs[idx].valid)
938 		reg |= ARM_SMMU_S2CR_EXIDVALID;
939 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
940 }
941 
942 static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
943 {
944 	arm_smmu_write_s2cr(smmu, idx);
945 	if (smmu->smrs)
946 		arm_smmu_write_smr(smmu, idx);
947 }
948 
949 /*
950  * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
951  * should be called after sCR0 is written.
952  */
953 static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
954 {
955 	u32 smr;
956 	int i;
957 
958 	if (!smmu->smrs)
959 		return;
960 	/*
961 	 * If we've had to accommodate firmware memory regions, we may
962 	 * have live SMRs by now; tread carefully...
963 	 *
964 	 * Somewhat perversely, not having a free SMR for this test implies we
965 	 * can get away without it anyway, as we'll only be able to 'allocate'
966 	 * these SMRs for the ID/mask values we're already trusting to be OK.
967 	 */
968 	for (i = 0; i < smmu->num_mapping_groups; i++)
969 		if (!smmu->smrs[i].valid)
970 			goto smr_ok;
971 	return;
972 smr_ok:
973 	/*
974 	 * SMR.ID bits may not be preserved if the corresponding MASK
975 	 * bits are set, so check each one separately. We can reject
976 	 * masters later if they try to claim IDs outside these masks.
977 	 */
978 	smr = FIELD_PREP(ARM_SMMU_SMR_ID, smmu->streamid_mask);
979 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
980 	smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
981 	smmu->streamid_mask = FIELD_GET(ARM_SMMU_SMR_ID, smr);
982 
983 	smr = FIELD_PREP(ARM_SMMU_SMR_MASK, smmu->streamid_mask);
984 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
985 	smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
986 	smmu->smr_mask_mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
987 }
988 
989 static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
990 {
991 	struct arm_smmu_smr *smrs = smmu->smrs;
992 	int i, free_idx = -ENOSPC;
993 
994 	/* Stream indexing is blissfully easy */
995 	if (!smrs)
996 		return id;
997 
998 	/* Validating SMRs is... less so */
999 	for (i = 0; i < smmu->num_mapping_groups; ++i) {
1000 		if (!smrs[i].valid) {
1001 			/*
1002 			 * Note the first free entry we come across, which
1003 			 * we'll claim in the end if nothing else matches.
1004 			 */
1005 			if (free_idx < 0)
1006 				free_idx = i;
1007 			continue;
1008 		}
1009 		/*
1010 		 * If the new entry is _entirely_ matched by an existing entry,
1011 		 * then reuse that, with the guarantee that there also cannot
1012 		 * be any subsequent conflicting entries. In normal use we'd
1013 		 * expect simply identical entries for this case, but there's
1014 		 * no harm in accommodating the generalisation.
1015 		 */
1016 		if ((mask & smrs[i].mask) == mask &&
1017 		    !((id ^ smrs[i].id) & ~smrs[i].mask))
1018 			return i;
1019 		/*
1020 		 * If the new entry has any other overlap with an existing one,
1021 		 * though, then there always exists at least one stream ID
1022 		 * which would cause a conflict, and we can't allow that risk.
1023 		 */
1024 		if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1025 			return -EINVAL;
1026 	}
1027 
1028 	return free_idx;
1029 }
1030 
1031 static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1032 {
1033 	if (--smmu->s2crs[idx].count)
1034 		return false;
1035 
1036 	smmu->s2crs[idx] = s2cr_init_val;
1037 	if (smmu->smrs)
1038 		smmu->smrs[idx].valid = false;
1039 
1040 	return true;
1041 }
1042 
1043 static int arm_smmu_master_alloc_smes(struct device *dev)
1044 {
1045 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1046 	struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1047 	struct arm_smmu_device *smmu = cfg->smmu;
1048 	struct arm_smmu_smr *smrs = smmu->smrs;
1049 	int i, idx, ret;
1050 
1051 	mutex_lock(&smmu->stream_map_mutex);
1052 	/* Figure out a viable stream map entry allocation */
1053 	for_each_cfg_sme(cfg, fwspec, i, idx) {
1054 		u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1055 		u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
1056 
1057 		if (idx != INVALID_SMENDX) {
1058 			ret = -EEXIST;
1059 			goto out_err;
1060 		}
1061 
1062 		ret = arm_smmu_find_sme(smmu, sid, mask);
1063 		if (ret < 0)
1064 			goto out_err;
1065 
1066 		idx = ret;
1067 		if (smrs && smmu->s2crs[idx].count == 0) {
1068 			smrs[idx].id = sid;
1069 			smrs[idx].mask = mask;
1070 			smrs[idx].valid = true;
1071 		}
1072 		smmu->s2crs[idx].count++;
1073 		cfg->smendx[i] = (s16)idx;
1074 	}
1075 
1076 	/* It worked! Now, poke the actual hardware */
1077 	for_each_cfg_sme(cfg, fwspec, i, idx)
1078 		arm_smmu_write_sme(smmu, idx);
1079 
1080 	mutex_unlock(&smmu->stream_map_mutex);
1081 	return 0;
1082 
1083 out_err:
1084 	while (i--) {
1085 		arm_smmu_free_sme(smmu, cfg->smendx[i]);
1086 		cfg->smendx[i] = INVALID_SMENDX;
1087 	}
1088 	mutex_unlock(&smmu->stream_map_mutex);
1089 	return ret;
1090 }
1091 
1092 static void arm_smmu_master_free_smes(struct arm_smmu_master_cfg *cfg,
1093 				      struct iommu_fwspec *fwspec)
1094 {
1095 	struct arm_smmu_device *smmu = cfg->smmu;
1096 	int i, idx;
1097 
1098 	mutex_lock(&smmu->stream_map_mutex);
1099 	for_each_cfg_sme(cfg, fwspec, i, idx) {
1100 		if (arm_smmu_free_sme(smmu, idx))
1101 			arm_smmu_write_sme(smmu, idx);
1102 		cfg->smendx[i] = INVALID_SMENDX;
1103 	}
1104 	mutex_unlock(&smmu->stream_map_mutex);
1105 }
1106 
1107 static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1108 				      struct arm_smmu_master_cfg *cfg,
1109 				      struct iommu_fwspec *fwspec)
1110 {
1111 	struct arm_smmu_device *smmu = smmu_domain->smmu;
1112 	struct arm_smmu_s2cr *s2cr = smmu->s2crs;
1113 	u8 cbndx = smmu_domain->cfg.cbndx;
1114 	enum arm_smmu_s2cr_type type;
1115 	int i, idx;
1116 
1117 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1118 		type = S2CR_TYPE_BYPASS;
1119 	else
1120 		type = S2CR_TYPE_TRANS;
1121 
1122 	for_each_cfg_sme(cfg, fwspec, i, idx) {
1123 		if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
1124 			continue;
1125 
1126 		s2cr[idx].type = type;
1127 		s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
1128 		s2cr[idx].cbndx = cbndx;
1129 		arm_smmu_write_s2cr(smmu, idx);
1130 	}
1131 	return 0;
1132 }
1133 
1134 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1135 {
1136 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1137 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1138 	struct arm_smmu_master_cfg *cfg;
1139 	struct arm_smmu_device *smmu;
1140 	int ret;
1141 
1142 	if (!fwspec || fwspec->ops != &arm_smmu_ops) {
1143 		dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1144 		return -ENXIO;
1145 	}
1146 
1147 	/*
1148 	 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1149 	 * domains between of_xlate() and probe_device() - we have no way to cope
1150 	 * with that, so until ARM gets converted to rely on groups and default
1151 	 * domains, just say no (but more politely than by dereferencing NULL).
1152 	 * This should be at least a WARN_ON once that's sorted.
1153 	 */
1154 	cfg = dev_iommu_priv_get(dev);
1155 	if (!cfg)
1156 		return -ENODEV;
1157 
1158 	smmu = cfg->smmu;
1159 
1160 	ret = arm_smmu_rpm_get(smmu);
1161 	if (ret < 0)
1162 		return ret;
1163 
1164 	/* Ensure that the domain is finalised */
1165 	ret = arm_smmu_init_domain_context(domain, smmu, dev);
1166 	if (ret < 0)
1167 		goto rpm_put;
1168 
1169 	/*
1170 	 * Sanity check the domain. We don't support domains across
1171 	 * different SMMUs.
1172 	 */
1173 	if (smmu_domain->smmu != smmu) {
1174 		dev_err(dev,
1175 			"cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1176 			dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1177 		ret = -EINVAL;
1178 		goto rpm_put;
1179 	}
1180 
1181 	/* Looks ok, so add the device to the domain */
1182 	ret = arm_smmu_domain_add_master(smmu_domain, cfg, fwspec);
1183 
1184 	/*
1185 	 * Setup an autosuspend delay to avoid bouncing runpm state.
1186 	 * Otherwise, if a driver for a suspended consumer device
1187 	 * unmaps buffers, it will runpm resume/suspend for each one.
1188 	 *
1189 	 * For example, when used by a GPU device, when an application
1190 	 * or game exits, it can trigger unmapping 100s or 1000s of
1191 	 * buffers.  With a runpm cycle for each buffer, that adds up
1192 	 * to 5-10sec worth of reprogramming the context bank, while
1193 	 * the system appears to be locked up to the user.
1194 	 */
1195 	pm_runtime_set_autosuspend_delay(smmu->dev, 20);
1196 	pm_runtime_use_autosuspend(smmu->dev);
1197 
1198 rpm_put:
1199 	arm_smmu_rpm_put(smmu);
1200 	return ret;
1201 }
1202 
1203 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1204 			phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
1205 {
1206 	struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1207 	struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1208 	int ret;
1209 
1210 	if (!ops)
1211 		return -ENODEV;
1212 
1213 	arm_smmu_rpm_get(smmu);
1214 	ret = ops->map(ops, iova, paddr, size, prot, gfp);
1215 	arm_smmu_rpm_put(smmu);
1216 
1217 	return ret;
1218 }
1219 
1220 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1221 			     size_t size, struct iommu_iotlb_gather *gather)
1222 {
1223 	struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1224 	struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1225 	size_t ret;
1226 
1227 	if (!ops)
1228 		return 0;
1229 
1230 	arm_smmu_rpm_get(smmu);
1231 	ret = ops->unmap(ops, iova, size, gather);
1232 	arm_smmu_rpm_put(smmu);
1233 
1234 	return ret;
1235 }
1236 
1237 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1238 {
1239 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1240 	struct arm_smmu_device *smmu = smmu_domain->smmu;
1241 
1242 	if (smmu_domain->flush_ops) {
1243 		arm_smmu_rpm_get(smmu);
1244 		smmu_domain->flush_ops->tlb_flush_all(smmu_domain);
1245 		arm_smmu_rpm_put(smmu);
1246 	}
1247 }
1248 
1249 static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
1250 				struct iommu_iotlb_gather *gather)
1251 {
1252 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1253 	struct arm_smmu_device *smmu = smmu_domain->smmu;
1254 
1255 	if (!smmu)
1256 		return;
1257 
1258 	arm_smmu_rpm_get(smmu);
1259 	if (smmu->version == ARM_SMMU_V2 ||
1260 	    smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1261 		arm_smmu_tlb_sync_context(smmu_domain);
1262 	else
1263 		arm_smmu_tlb_sync_global(smmu);
1264 	arm_smmu_rpm_put(smmu);
1265 }
1266 
1267 static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1268 					      dma_addr_t iova)
1269 {
1270 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1271 	struct arm_smmu_device *smmu = smmu_domain->smmu;
1272 	struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1273 	struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1274 	struct device *dev = smmu->dev;
1275 	void __iomem *reg;
1276 	u32 tmp;
1277 	u64 phys;
1278 	unsigned long va, flags;
1279 	int ret, idx = cfg->cbndx;
1280 
1281 	ret = arm_smmu_rpm_get(smmu);
1282 	if (ret < 0)
1283 		return 0;
1284 
1285 	spin_lock_irqsave(&smmu_domain->cb_lock, flags);
1286 	va = iova & ~0xfffUL;
1287 	if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
1288 		arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1289 	else
1290 		arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1291 
1292 	reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx)) + ARM_SMMU_CB_ATSR;
1293 	if (readl_poll_timeout_atomic(reg, tmp, !(tmp & ARM_SMMU_ATSR_ACTIVE),
1294 				      5, 50)) {
1295 		spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1296 		dev_err(dev,
1297 			"iova to phys timed out on %pad. Falling back to software table walk.\n",
1298 			&iova);
1299 		return ops->iova_to_phys(ops, iova);
1300 	}
1301 
1302 	phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
1303 	spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1304 	if (phys & ARM_SMMU_CB_PAR_F) {
1305 		dev_err(dev, "translation fault!\n");
1306 		dev_err(dev, "PAR = 0x%llx\n", phys);
1307 		return 0;
1308 	}
1309 
1310 	arm_smmu_rpm_put(smmu);
1311 
1312 	return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1313 }
1314 
1315 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1316 					dma_addr_t iova)
1317 {
1318 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1319 	struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1320 
1321 	if (domain->type == IOMMU_DOMAIN_IDENTITY)
1322 		return iova;
1323 
1324 	if (!ops)
1325 		return 0;
1326 
1327 	if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1328 			smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1329 		return arm_smmu_iova_to_phys_hard(domain, iova);
1330 
1331 	return ops->iova_to_phys(ops, iova);
1332 }
1333 
1334 static bool arm_smmu_capable(enum iommu_cap cap)
1335 {
1336 	switch (cap) {
1337 	case IOMMU_CAP_CACHE_COHERENCY:
1338 		/*
1339 		 * Return true here as the SMMU can always send out coherent
1340 		 * requests.
1341 		 */
1342 		return true;
1343 	case IOMMU_CAP_NOEXEC:
1344 		return true;
1345 	default:
1346 		return false;
1347 	}
1348 }
1349 
1350 static
1351 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
1352 {
1353 	struct device *dev = driver_find_device_by_fwnode(&arm_smmu_driver.driver,
1354 							  fwnode);
1355 	put_device(dev);
1356 	return dev ? dev_get_drvdata(dev) : NULL;
1357 }
1358 
1359 static struct iommu_device *arm_smmu_probe_device(struct device *dev)
1360 {
1361 	struct arm_smmu_device *smmu = NULL;
1362 	struct arm_smmu_master_cfg *cfg;
1363 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1364 	int i, ret;
1365 
1366 	if (using_legacy_binding) {
1367 		ret = arm_smmu_register_legacy_master(dev, &smmu);
1368 
1369 		/*
1370 		 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1371 		 * will allocate/initialise a new one. Thus we need to update fwspec for
1372 		 * later use.
1373 		 */
1374 		fwspec = dev_iommu_fwspec_get(dev);
1375 		if (ret)
1376 			goto out_free;
1377 	} else if (fwspec && fwspec->ops == &arm_smmu_ops) {
1378 		smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
1379 	} else {
1380 		return ERR_PTR(-ENODEV);
1381 	}
1382 
1383 	ret = -EINVAL;
1384 	for (i = 0; i < fwspec->num_ids; i++) {
1385 		u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1386 		u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
1387 
1388 		if (sid & ~smmu->streamid_mask) {
1389 			dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1390 				sid, smmu->streamid_mask);
1391 			goto out_free;
1392 		}
1393 		if (mask & ~smmu->smr_mask_mask) {
1394 			dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1395 				mask, smmu->smr_mask_mask);
1396 			goto out_free;
1397 		}
1398 	}
1399 
1400 	ret = -ENOMEM;
1401 	cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1402 		      GFP_KERNEL);
1403 	if (!cfg)
1404 		goto out_free;
1405 
1406 	cfg->smmu = smmu;
1407 	dev_iommu_priv_set(dev, cfg);
1408 	while (i--)
1409 		cfg->smendx[i] = INVALID_SMENDX;
1410 
1411 	ret = arm_smmu_rpm_get(smmu);
1412 	if (ret < 0)
1413 		goto out_cfg_free;
1414 
1415 	ret = arm_smmu_master_alloc_smes(dev);
1416 	arm_smmu_rpm_put(smmu);
1417 
1418 	if (ret)
1419 		goto out_cfg_free;
1420 
1421 	device_link_add(dev, smmu->dev,
1422 			DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1423 
1424 	return &smmu->iommu;
1425 
1426 out_cfg_free:
1427 	kfree(cfg);
1428 out_free:
1429 	iommu_fwspec_free(dev);
1430 	return ERR_PTR(ret);
1431 }
1432 
1433 static void arm_smmu_release_device(struct device *dev)
1434 {
1435 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1436 	struct arm_smmu_master_cfg *cfg;
1437 	struct arm_smmu_device *smmu;
1438 	int ret;
1439 
1440 	if (!fwspec || fwspec->ops != &arm_smmu_ops)
1441 		return;
1442 
1443 	cfg  = dev_iommu_priv_get(dev);
1444 	smmu = cfg->smmu;
1445 
1446 	ret = arm_smmu_rpm_get(smmu);
1447 	if (ret < 0)
1448 		return;
1449 
1450 	arm_smmu_master_free_smes(cfg, fwspec);
1451 
1452 	arm_smmu_rpm_put(smmu);
1453 
1454 	dev_iommu_priv_set(dev, NULL);
1455 	kfree(cfg);
1456 	iommu_fwspec_free(dev);
1457 }
1458 
1459 static struct iommu_group *arm_smmu_device_group(struct device *dev)
1460 {
1461 	struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1462 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1463 	struct arm_smmu_device *smmu = cfg->smmu;
1464 	struct iommu_group *group = NULL;
1465 	int i, idx;
1466 
1467 	for_each_cfg_sme(cfg, fwspec, i, idx) {
1468 		if (group && smmu->s2crs[idx].group &&
1469 		    group != smmu->s2crs[idx].group)
1470 			return ERR_PTR(-EINVAL);
1471 
1472 		group = smmu->s2crs[idx].group;
1473 	}
1474 
1475 	if (group)
1476 		return iommu_group_ref_get(group);
1477 
1478 	if (dev_is_pci(dev))
1479 		group = pci_device_group(dev);
1480 	else if (dev_is_fsl_mc(dev))
1481 		group = fsl_mc_device_group(dev);
1482 	else
1483 		group = generic_device_group(dev);
1484 
1485 	/* Remember group for faster lookups */
1486 	if (!IS_ERR(group))
1487 		for_each_cfg_sme(cfg, fwspec, i, idx)
1488 			smmu->s2crs[idx].group = group;
1489 
1490 	return group;
1491 }
1492 
1493 static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1494 				    enum iommu_attr attr, void *data)
1495 {
1496 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1497 
1498 	switch(domain->type) {
1499 	case IOMMU_DOMAIN_UNMANAGED:
1500 		switch (attr) {
1501 		case DOMAIN_ATTR_NESTING:
1502 			*(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1503 			return 0;
1504 		default:
1505 			return -ENODEV;
1506 		}
1507 		break;
1508 	case IOMMU_DOMAIN_DMA:
1509 		switch (attr) {
1510 		case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1511 			*(int *)data = smmu_domain->non_strict;
1512 			return 0;
1513 		default:
1514 			return -ENODEV;
1515 		}
1516 		break;
1517 	default:
1518 		return -EINVAL;
1519 	}
1520 }
1521 
1522 static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1523 				    enum iommu_attr attr, void *data)
1524 {
1525 	int ret = 0;
1526 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1527 
1528 	mutex_lock(&smmu_domain->init_mutex);
1529 
1530 	switch(domain->type) {
1531 	case IOMMU_DOMAIN_UNMANAGED:
1532 		switch (attr) {
1533 		case DOMAIN_ATTR_NESTING:
1534 			if (smmu_domain->smmu) {
1535 				ret = -EPERM;
1536 				goto out_unlock;
1537 			}
1538 
1539 			if (*(int *)data)
1540 				smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1541 			else
1542 				smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1543 			break;
1544 		default:
1545 			ret = -ENODEV;
1546 		}
1547 		break;
1548 	case IOMMU_DOMAIN_DMA:
1549 		switch (attr) {
1550 		case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1551 			smmu_domain->non_strict = *(int *)data;
1552 			break;
1553 		default:
1554 			ret = -ENODEV;
1555 		}
1556 		break;
1557 	default:
1558 		ret = -EINVAL;
1559 	}
1560 out_unlock:
1561 	mutex_unlock(&smmu_domain->init_mutex);
1562 	return ret;
1563 }
1564 
1565 static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1566 {
1567 	u32 mask, fwid = 0;
1568 
1569 	if (args->args_count > 0)
1570 		fwid |= FIELD_PREP(ARM_SMMU_SMR_ID, args->args[0]);
1571 
1572 	if (args->args_count > 1)
1573 		fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, args->args[1]);
1574 	else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
1575 		fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, mask);
1576 
1577 	return iommu_fwspec_add_ids(dev, &fwid, 1);
1578 }
1579 
1580 static void arm_smmu_get_resv_regions(struct device *dev,
1581 				      struct list_head *head)
1582 {
1583 	struct iommu_resv_region *region;
1584 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1585 
1586 	region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1587 					 prot, IOMMU_RESV_SW_MSI);
1588 	if (!region)
1589 		return;
1590 
1591 	list_add_tail(&region->list, head);
1592 
1593 	iommu_dma_get_resv_regions(dev, head);
1594 }
1595 
1596 static int arm_smmu_def_domain_type(struct device *dev)
1597 {
1598 	struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1599 	const struct arm_smmu_impl *impl = cfg->smmu->impl;
1600 
1601 	if (impl && impl->def_domain_type)
1602 		return impl->def_domain_type(dev);
1603 
1604 	return 0;
1605 }
1606 
1607 static struct iommu_ops arm_smmu_ops = {
1608 	.capable		= arm_smmu_capable,
1609 	.domain_alloc		= arm_smmu_domain_alloc,
1610 	.domain_free		= arm_smmu_domain_free,
1611 	.attach_dev		= arm_smmu_attach_dev,
1612 	.map			= arm_smmu_map,
1613 	.unmap			= arm_smmu_unmap,
1614 	.flush_iotlb_all	= arm_smmu_flush_iotlb_all,
1615 	.iotlb_sync		= arm_smmu_iotlb_sync,
1616 	.iova_to_phys		= arm_smmu_iova_to_phys,
1617 	.probe_device		= arm_smmu_probe_device,
1618 	.release_device		= arm_smmu_release_device,
1619 	.device_group		= arm_smmu_device_group,
1620 	.domain_get_attr	= arm_smmu_domain_get_attr,
1621 	.domain_set_attr	= arm_smmu_domain_set_attr,
1622 	.of_xlate		= arm_smmu_of_xlate,
1623 	.get_resv_regions	= arm_smmu_get_resv_regions,
1624 	.put_resv_regions	= generic_iommu_put_resv_regions,
1625 	.def_domain_type	= arm_smmu_def_domain_type,
1626 	.pgsize_bitmap		= -1UL, /* Restricted during device attach */
1627 };
1628 
1629 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1630 {
1631 	int i;
1632 	u32 reg;
1633 
1634 	/* clear global FSR */
1635 	reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
1636 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, reg);
1637 
1638 	/*
1639 	 * Reset stream mapping groups: Initial values mark all SMRn as
1640 	 * invalid and all S2CRn as bypass unless overridden.
1641 	 */
1642 	for (i = 0; i < smmu->num_mapping_groups; ++i)
1643 		arm_smmu_write_sme(smmu, i);
1644 
1645 	/* Make sure all context banks are disabled and clear CB_FSR  */
1646 	for (i = 0; i < smmu->num_context_banks; ++i) {
1647 		arm_smmu_write_context_bank(smmu, i);
1648 		arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_FSR, ARM_SMMU_FSR_FAULT);
1649 	}
1650 
1651 	/* Invalidate the TLB, just in case */
1652 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLH, QCOM_DUMMY_VAL);
1653 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLNSNH, QCOM_DUMMY_VAL);
1654 
1655 	reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sCR0);
1656 
1657 	/* Enable fault reporting */
1658 	reg |= (ARM_SMMU_sCR0_GFRE | ARM_SMMU_sCR0_GFIE |
1659 		ARM_SMMU_sCR0_GCFGFRE | ARM_SMMU_sCR0_GCFGFIE);
1660 
1661 	/* Disable TLB broadcasting. */
1662 	reg |= (ARM_SMMU_sCR0_VMIDPNE | ARM_SMMU_sCR0_PTM);
1663 
1664 	/* Enable client access, handling unmatched streams as appropriate */
1665 	reg &= ~ARM_SMMU_sCR0_CLIENTPD;
1666 	if (disable_bypass)
1667 		reg |= ARM_SMMU_sCR0_USFCFG;
1668 	else
1669 		reg &= ~ARM_SMMU_sCR0_USFCFG;
1670 
1671 	/* Disable forced broadcasting */
1672 	reg &= ~ARM_SMMU_sCR0_FB;
1673 
1674 	/* Don't upgrade barriers */
1675 	reg &= ~(ARM_SMMU_sCR0_BSU);
1676 
1677 	if (smmu->features & ARM_SMMU_FEAT_VMID16)
1678 		reg |= ARM_SMMU_sCR0_VMID16EN;
1679 
1680 	if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1681 		reg |= ARM_SMMU_sCR0_EXIDENABLE;
1682 
1683 	if (smmu->impl && smmu->impl->reset)
1684 		smmu->impl->reset(smmu);
1685 
1686 	/* Push the button */
1687 	arm_smmu_tlb_sync_global(smmu);
1688 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, reg);
1689 }
1690 
1691 static int arm_smmu_id_size_to_bits(int size)
1692 {
1693 	switch (size) {
1694 	case 0:
1695 		return 32;
1696 	case 1:
1697 		return 36;
1698 	case 2:
1699 		return 40;
1700 	case 3:
1701 		return 42;
1702 	case 4:
1703 		return 44;
1704 	case 5:
1705 	default:
1706 		return 48;
1707 	}
1708 }
1709 
1710 static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1711 {
1712 	unsigned int size;
1713 	u32 id;
1714 	bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
1715 	int i, ret;
1716 
1717 	dev_notice(smmu->dev, "probing hardware configuration...\n");
1718 	dev_notice(smmu->dev, "SMMUv%d with:\n",
1719 			smmu->version == ARM_SMMU_V2 ? 2 : 1);
1720 
1721 	/* ID0 */
1722 	id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID0);
1723 
1724 	/* Restrict available stages based on module parameter */
1725 	if (force_stage == 1)
1726 		id &= ~(ARM_SMMU_ID0_S2TS | ARM_SMMU_ID0_NTS);
1727 	else if (force_stage == 2)
1728 		id &= ~(ARM_SMMU_ID0_S1TS | ARM_SMMU_ID0_NTS);
1729 
1730 	if (id & ARM_SMMU_ID0_S1TS) {
1731 		smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1732 		dev_notice(smmu->dev, "\tstage 1 translation\n");
1733 	}
1734 
1735 	if (id & ARM_SMMU_ID0_S2TS) {
1736 		smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1737 		dev_notice(smmu->dev, "\tstage 2 translation\n");
1738 	}
1739 
1740 	if (id & ARM_SMMU_ID0_NTS) {
1741 		smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1742 		dev_notice(smmu->dev, "\tnested translation\n");
1743 	}
1744 
1745 	if (!(smmu->features &
1746 		(ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1747 		dev_err(smmu->dev, "\tno translation support!\n");
1748 		return -ENODEV;
1749 	}
1750 
1751 	if ((id & ARM_SMMU_ID0_S1TS) &&
1752 	    ((smmu->version < ARM_SMMU_V2) || !(id & ARM_SMMU_ID0_ATOSNS))) {
1753 		smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1754 		dev_notice(smmu->dev, "\taddress translation ops\n");
1755 	}
1756 
1757 	/*
1758 	 * In order for DMA API calls to work properly, we must defer to what
1759 	 * the FW says about coherency, regardless of what the hardware claims.
1760 	 * Fortunately, this also opens up a workaround for systems where the
1761 	 * ID register value has ended up configured incorrectly.
1762 	 */
1763 	cttw_reg = !!(id & ARM_SMMU_ID0_CTTW);
1764 	if (cttw_fw || cttw_reg)
1765 		dev_notice(smmu->dev, "\t%scoherent table walk\n",
1766 			   cttw_fw ? "" : "non-");
1767 	if (cttw_fw != cttw_reg)
1768 		dev_notice(smmu->dev,
1769 			   "\t(IDR0.CTTW overridden by FW configuration)\n");
1770 
1771 	/* Max. number of entries we have for stream matching/indexing */
1772 	if (smmu->version == ARM_SMMU_V2 && id & ARM_SMMU_ID0_EXIDS) {
1773 		smmu->features |= ARM_SMMU_FEAT_EXIDS;
1774 		size = 1 << 16;
1775 	} else {
1776 		size = 1 << FIELD_GET(ARM_SMMU_ID0_NUMSIDB, id);
1777 	}
1778 	smmu->streamid_mask = size - 1;
1779 	if (id & ARM_SMMU_ID0_SMS) {
1780 		smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1781 		size = FIELD_GET(ARM_SMMU_ID0_NUMSMRG, id);
1782 		if (size == 0) {
1783 			dev_err(smmu->dev,
1784 				"stream-matching supported, but no SMRs present!\n");
1785 			return -ENODEV;
1786 		}
1787 
1788 		/* Zero-initialised to mark as invalid */
1789 		smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1790 					  GFP_KERNEL);
1791 		if (!smmu->smrs)
1792 			return -ENOMEM;
1793 
1794 		dev_notice(smmu->dev,
1795 			   "\tstream matching with %u register groups", size);
1796 	}
1797 	/* s2cr->type == 0 means translation, so initialise explicitly */
1798 	smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1799 					 GFP_KERNEL);
1800 	if (!smmu->s2crs)
1801 		return -ENOMEM;
1802 	for (i = 0; i < size; i++)
1803 		smmu->s2crs[i] = s2cr_init_val;
1804 
1805 	smmu->num_mapping_groups = size;
1806 	mutex_init(&smmu->stream_map_mutex);
1807 	spin_lock_init(&smmu->global_sync_lock);
1808 
1809 	if (smmu->version < ARM_SMMU_V2 ||
1810 	    !(id & ARM_SMMU_ID0_PTFS_NO_AARCH32)) {
1811 		smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1812 		if (!(id & ARM_SMMU_ID0_PTFS_NO_AARCH32S))
1813 			smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1814 	}
1815 
1816 	/* ID1 */
1817 	id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID1);
1818 	smmu->pgshift = (id & ARM_SMMU_ID1_PAGESIZE) ? 16 : 12;
1819 
1820 	/* Check for size mismatch of SMMU address space from mapped region */
1821 	size = 1 << (FIELD_GET(ARM_SMMU_ID1_NUMPAGENDXB, id) + 1);
1822 	if (smmu->numpage != 2 * size << smmu->pgshift)
1823 		dev_warn(smmu->dev,
1824 			"SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1825 			2 * size << smmu->pgshift, smmu->numpage);
1826 	/* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1827 	smmu->numpage = size;
1828 
1829 	smmu->num_s2_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMS2CB, id);
1830 	smmu->num_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMCB, id);
1831 	if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1832 		dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1833 		return -ENODEV;
1834 	}
1835 	dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1836 		   smmu->num_context_banks, smmu->num_s2_context_banks);
1837 	smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1838 				 sizeof(*smmu->cbs), GFP_KERNEL);
1839 	if (!smmu->cbs)
1840 		return -ENOMEM;
1841 
1842 	/* ID2 */
1843 	id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID2);
1844 	size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_IAS, id));
1845 	smmu->ipa_size = size;
1846 
1847 	/* The output mask is also applied for bypass */
1848 	size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_OAS, id));
1849 	smmu->pa_size = size;
1850 
1851 	if (id & ARM_SMMU_ID2_VMID16)
1852 		smmu->features |= ARM_SMMU_FEAT_VMID16;
1853 
1854 	/*
1855 	 * What the page table walker can address actually depends on which
1856 	 * descriptor format is in use, but since a) we don't know that yet,
1857 	 * and b) it can vary per context bank, this will have to do...
1858 	 */
1859 	if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1860 		dev_warn(smmu->dev,
1861 			 "failed to set DMA mask for table walker\n");
1862 
1863 	if (smmu->version < ARM_SMMU_V2) {
1864 		smmu->va_size = smmu->ipa_size;
1865 		if (smmu->version == ARM_SMMU_V1_64K)
1866 			smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1867 	} else {
1868 		size = FIELD_GET(ARM_SMMU_ID2_UBS, id);
1869 		smmu->va_size = arm_smmu_id_size_to_bits(size);
1870 		if (id & ARM_SMMU_ID2_PTFS_4K)
1871 			smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
1872 		if (id & ARM_SMMU_ID2_PTFS_16K)
1873 			smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
1874 		if (id & ARM_SMMU_ID2_PTFS_64K)
1875 			smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1876 	}
1877 
1878 	if (smmu->impl && smmu->impl->cfg_probe) {
1879 		ret = smmu->impl->cfg_probe(smmu);
1880 		if (ret)
1881 			return ret;
1882 	}
1883 
1884 	/* Now we've corralled the various formats, what'll it do? */
1885 	if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
1886 		smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
1887 	if (smmu->features &
1888 	    (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
1889 		smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
1890 	if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
1891 		smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
1892 	if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
1893 		smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
1894 
1895 	if (arm_smmu_ops.pgsize_bitmap == -1UL)
1896 		arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1897 	else
1898 		arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1899 	dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1900 		   smmu->pgsize_bitmap);
1901 
1902 
1903 	if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1904 		dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1905 			   smmu->va_size, smmu->ipa_size);
1906 
1907 	if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1908 		dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1909 			   smmu->ipa_size, smmu->pa_size);
1910 
1911 	return 0;
1912 }
1913 
1914 struct arm_smmu_match_data {
1915 	enum arm_smmu_arch_version version;
1916 	enum arm_smmu_implementation model;
1917 };
1918 
1919 #define ARM_SMMU_MATCH_DATA(name, ver, imp)	\
1920 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
1921 
1922 ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1923 ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
1924 ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
1925 ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
1926 ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
1927 ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
1928 
1929 static const struct of_device_id arm_smmu_of_match[] = {
1930 	{ .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1931 	{ .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1932 	{ .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
1933 	{ .compatible = "arm,mmu-401", .data = &arm_mmu401 },
1934 	{ .compatible = "arm,mmu-500", .data = &arm_mmu500 },
1935 	{ .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
1936 	{ .compatible = "nvidia,smmu-500", .data = &arm_mmu500 },
1937 	{ .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
1938 	{ },
1939 };
1940 MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1941 
1942 #ifdef CONFIG_ACPI
1943 static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
1944 {
1945 	int ret = 0;
1946 
1947 	switch (model) {
1948 	case ACPI_IORT_SMMU_V1:
1949 	case ACPI_IORT_SMMU_CORELINK_MMU400:
1950 		smmu->version = ARM_SMMU_V1;
1951 		smmu->model = GENERIC_SMMU;
1952 		break;
1953 	case ACPI_IORT_SMMU_CORELINK_MMU401:
1954 		smmu->version = ARM_SMMU_V1_64K;
1955 		smmu->model = GENERIC_SMMU;
1956 		break;
1957 	case ACPI_IORT_SMMU_V2:
1958 		smmu->version = ARM_SMMU_V2;
1959 		smmu->model = GENERIC_SMMU;
1960 		break;
1961 	case ACPI_IORT_SMMU_CORELINK_MMU500:
1962 		smmu->version = ARM_SMMU_V2;
1963 		smmu->model = ARM_MMU500;
1964 		break;
1965 	case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
1966 		smmu->version = ARM_SMMU_V2;
1967 		smmu->model = CAVIUM_SMMUV2;
1968 		break;
1969 	default:
1970 		ret = -ENODEV;
1971 	}
1972 
1973 	return ret;
1974 }
1975 
1976 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1977 				      struct arm_smmu_device *smmu)
1978 {
1979 	struct device *dev = smmu->dev;
1980 	struct acpi_iort_node *node =
1981 		*(struct acpi_iort_node **)dev_get_platdata(dev);
1982 	struct acpi_iort_smmu *iort_smmu;
1983 	int ret;
1984 
1985 	/* Retrieve SMMU1/2 specific data */
1986 	iort_smmu = (struct acpi_iort_smmu *)node->node_data;
1987 
1988 	ret = acpi_smmu_get_data(iort_smmu->model, smmu);
1989 	if (ret < 0)
1990 		return ret;
1991 
1992 	/* Ignore the configuration access interrupt */
1993 	smmu->num_global_irqs = 1;
1994 
1995 	if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
1996 		smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1997 
1998 	return 0;
1999 }
2000 #else
2001 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2002 					     struct arm_smmu_device *smmu)
2003 {
2004 	return -ENODEV;
2005 }
2006 #endif
2007 
2008 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2009 				    struct arm_smmu_device *smmu)
2010 {
2011 	const struct arm_smmu_match_data *data;
2012 	struct device *dev = &pdev->dev;
2013 	bool legacy_binding;
2014 
2015 	if (of_property_read_u32(dev->of_node, "#global-interrupts",
2016 				 &smmu->num_global_irqs)) {
2017 		dev_err(dev, "missing #global-interrupts property\n");
2018 		return -ENODEV;
2019 	}
2020 
2021 	data = of_device_get_match_data(dev);
2022 	smmu->version = data->version;
2023 	smmu->model = data->model;
2024 
2025 	legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
2026 	if (legacy_binding && !using_generic_binding) {
2027 		if (!using_legacy_binding) {
2028 			pr_notice("deprecated \"mmu-masters\" DT property in use; %s support unavailable\n",
2029 				  IS_ENABLED(CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS) ? "DMA API" : "SMMU");
2030 		}
2031 		using_legacy_binding = true;
2032 	} else if (!legacy_binding && !using_legacy_binding) {
2033 		using_generic_binding = true;
2034 	} else {
2035 		dev_err(dev, "not probing due to mismatched DT properties\n");
2036 		return -ENODEV;
2037 	}
2038 
2039 	if (of_dma_is_coherent(dev->of_node))
2040 		smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2041 
2042 	return 0;
2043 }
2044 
2045 static int arm_smmu_bus_init(struct iommu_ops *ops)
2046 {
2047 	int err;
2048 
2049 	/* Oh, for a proper bus abstraction */
2050 	if (!iommu_present(&platform_bus_type)) {
2051 		err = bus_set_iommu(&platform_bus_type, ops);
2052 		if (err)
2053 			return err;
2054 	}
2055 #ifdef CONFIG_ARM_AMBA
2056 	if (!iommu_present(&amba_bustype)) {
2057 		err = bus_set_iommu(&amba_bustype, ops);
2058 		if (err)
2059 			goto err_reset_platform_ops;
2060 	}
2061 #endif
2062 #ifdef CONFIG_PCI
2063 	if (!iommu_present(&pci_bus_type)) {
2064 		err = bus_set_iommu(&pci_bus_type, ops);
2065 		if (err)
2066 			goto err_reset_amba_ops;
2067 	}
2068 #endif
2069 #ifdef CONFIG_FSL_MC_BUS
2070 	if (!iommu_present(&fsl_mc_bus_type)) {
2071 		err = bus_set_iommu(&fsl_mc_bus_type, ops);
2072 		if (err)
2073 			goto err_reset_pci_ops;
2074 	}
2075 #endif
2076 	return 0;
2077 
2078 err_reset_pci_ops: __maybe_unused;
2079 #ifdef CONFIG_PCI
2080 	bus_set_iommu(&pci_bus_type, NULL);
2081 #endif
2082 err_reset_amba_ops: __maybe_unused;
2083 #ifdef CONFIG_ARM_AMBA
2084 	bus_set_iommu(&amba_bustype, NULL);
2085 #endif
2086 err_reset_platform_ops: __maybe_unused;
2087 	bus_set_iommu(&platform_bus_type, NULL);
2088 	return err;
2089 }
2090 
2091 static int arm_smmu_device_probe(struct platform_device *pdev)
2092 {
2093 	struct resource *res;
2094 	resource_size_t ioaddr;
2095 	struct arm_smmu_device *smmu;
2096 	struct device *dev = &pdev->dev;
2097 	int num_irqs, i, err;
2098 	irqreturn_t (*global_fault)(int irq, void *dev);
2099 
2100 	smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2101 	if (!smmu) {
2102 		dev_err(dev, "failed to allocate arm_smmu_device\n");
2103 		return -ENOMEM;
2104 	}
2105 	smmu->dev = dev;
2106 
2107 	if (dev->of_node)
2108 		err = arm_smmu_device_dt_probe(pdev, smmu);
2109 	else
2110 		err = arm_smmu_device_acpi_probe(pdev, smmu);
2111 
2112 	if (err)
2113 		return err;
2114 
2115 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2116 	ioaddr = res->start;
2117 	smmu->base = devm_ioremap_resource(dev, res);
2118 	if (IS_ERR(smmu->base))
2119 		return PTR_ERR(smmu->base);
2120 	/*
2121 	 * The resource size should effectively match the value of SMMU_TOP;
2122 	 * stash that temporarily until we know PAGESIZE to validate it with.
2123 	 */
2124 	smmu->numpage = resource_size(res);
2125 
2126 	smmu = arm_smmu_impl_init(smmu);
2127 	if (IS_ERR(smmu))
2128 		return PTR_ERR(smmu);
2129 
2130 	num_irqs = 0;
2131 	while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2132 		num_irqs++;
2133 		if (num_irqs > smmu->num_global_irqs)
2134 			smmu->num_context_irqs++;
2135 	}
2136 
2137 	if (!smmu->num_context_irqs) {
2138 		dev_err(dev, "found %d interrupts but expected at least %d\n",
2139 			num_irqs, smmu->num_global_irqs + 1);
2140 		return -ENODEV;
2141 	}
2142 
2143 	smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
2144 				  GFP_KERNEL);
2145 	if (!smmu->irqs) {
2146 		dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2147 		return -ENOMEM;
2148 	}
2149 
2150 	for (i = 0; i < num_irqs; ++i) {
2151 		int irq = platform_get_irq(pdev, i);
2152 
2153 		if (irq < 0)
2154 			return -ENODEV;
2155 		smmu->irqs[i] = irq;
2156 	}
2157 
2158 	err = devm_clk_bulk_get_all(dev, &smmu->clks);
2159 	if (err < 0) {
2160 		dev_err(dev, "failed to get clocks %d\n", err);
2161 		return err;
2162 	}
2163 	smmu->num_clks = err;
2164 
2165 	err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2166 	if (err)
2167 		return err;
2168 
2169 	err = arm_smmu_device_cfg_probe(smmu);
2170 	if (err)
2171 		return err;
2172 
2173 	if (smmu->version == ARM_SMMU_V2) {
2174 		if (smmu->num_context_banks > smmu->num_context_irqs) {
2175 			dev_err(dev,
2176 			      "found only %d context irq(s) but %d required\n",
2177 			      smmu->num_context_irqs, smmu->num_context_banks);
2178 			return -ENODEV;
2179 		}
2180 
2181 		/* Ignore superfluous interrupts */
2182 		smmu->num_context_irqs = smmu->num_context_banks;
2183 	}
2184 
2185 	if (smmu->impl && smmu->impl->global_fault)
2186 		global_fault = smmu->impl->global_fault;
2187 	else
2188 		global_fault = arm_smmu_global_fault;
2189 
2190 	for (i = 0; i < smmu->num_global_irqs; ++i) {
2191 		err = devm_request_irq(smmu->dev, smmu->irqs[i],
2192 				       global_fault,
2193 				       IRQF_SHARED,
2194 				       "arm-smmu global fault",
2195 				       smmu);
2196 		if (err) {
2197 			dev_err(dev, "failed to request global IRQ %d (%u)\n",
2198 				i, smmu->irqs[i]);
2199 			return err;
2200 		}
2201 	}
2202 
2203 	err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2204 				     "smmu.%pa", &ioaddr);
2205 	if (err) {
2206 		dev_err(dev, "Failed to register iommu in sysfs\n");
2207 		return err;
2208 	}
2209 
2210 	iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2211 	iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2212 
2213 	err = iommu_device_register(&smmu->iommu);
2214 	if (err) {
2215 		dev_err(dev, "Failed to register iommu\n");
2216 		return err;
2217 	}
2218 
2219 	platform_set_drvdata(pdev, smmu);
2220 	arm_smmu_device_reset(smmu);
2221 	arm_smmu_test_smr_masks(smmu);
2222 
2223 	/*
2224 	 * We want to avoid touching dev->power.lock in fastpaths unless
2225 	 * it's really going to do something useful - pm_runtime_enabled()
2226 	 * can serve as an ideal proxy for that decision. So, conditionally
2227 	 * enable pm_runtime.
2228 	 */
2229 	if (dev->pm_domain) {
2230 		pm_runtime_set_active(dev);
2231 		pm_runtime_enable(dev);
2232 	}
2233 
2234 	/*
2235 	 * For ACPI and generic DT bindings, an SMMU will be probed before
2236 	 * any device which might need it, so we want the bus ops in place
2237 	 * ready to handle default domain setup as soon as any SMMU exists.
2238 	 */
2239 	if (!using_legacy_binding)
2240 		return arm_smmu_bus_init(&arm_smmu_ops);
2241 
2242 	return 0;
2243 }
2244 
2245 static int arm_smmu_device_remove(struct platform_device *pdev)
2246 {
2247 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
2248 
2249 	if (!smmu)
2250 		return -ENODEV;
2251 
2252 	if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
2253 		dev_notice(&pdev->dev, "disabling translation\n");
2254 
2255 	arm_smmu_bus_init(NULL);
2256 	iommu_device_unregister(&smmu->iommu);
2257 	iommu_device_sysfs_remove(&smmu->iommu);
2258 
2259 	arm_smmu_rpm_get(smmu);
2260 	/* Turn the thing off */
2261 	arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, ARM_SMMU_sCR0_CLIENTPD);
2262 	arm_smmu_rpm_put(smmu);
2263 
2264 	if (pm_runtime_enabled(smmu->dev))
2265 		pm_runtime_force_suspend(smmu->dev);
2266 	else
2267 		clk_bulk_disable(smmu->num_clks, smmu->clks);
2268 
2269 	clk_bulk_unprepare(smmu->num_clks, smmu->clks);
2270 	return 0;
2271 }
2272 
2273 static void arm_smmu_device_shutdown(struct platform_device *pdev)
2274 {
2275 	arm_smmu_device_remove(pdev);
2276 }
2277 
2278 static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
2279 {
2280 	struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2281 	int ret;
2282 
2283 	ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2284 	if (ret)
2285 		return ret;
2286 
2287 	arm_smmu_device_reset(smmu);
2288 
2289 	return 0;
2290 }
2291 
2292 static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
2293 {
2294 	struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2295 
2296 	clk_bulk_disable(smmu->num_clks, smmu->clks);
2297 
2298 	return 0;
2299 }
2300 
2301 static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2302 {
2303 	if (pm_runtime_suspended(dev))
2304 		return 0;
2305 
2306 	return arm_smmu_runtime_resume(dev);
2307 }
2308 
2309 static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2310 {
2311 	if (pm_runtime_suspended(dev))
2312 		return 0;
2313 
2314 	return arm_smmu_runtime_suspend(dev);
2315 }
2316 
2317 static const struct dev_pm_ops arm_smmu_pm_ops = {
2318 	SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2319 	SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2320 			   arm_smmu_runtime_resume, NULL)
2321 };
2322 
2323 static struct platform_driver arm_smmu_driver = {
2324 	.driver	= {
2325 		.name			= "arm-smmu",
2326 		.of_match_table		= arm_smmu_of_match,
2327 		.pm			= &arm_smmu_pm_ops,
2328 		.suppress_bind_attrs    = true,
2329 	},
2330 	.probe	= arm_smmu_device_probe,
2331 	.remove	= arm_smmu_device_remove,
2332 	.shutdown = arm_smmu_device_shutdown,
2333 };
2334 module_platform_driver(arm_smmu_driver);
2335 
2336 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2337 MODULE_AUTHOR("Will Deacon <will@kernel.org>");
2338 MODULE_ALIAS("platform:arm-smmu");
2339 MODULE_LICENSE("GPL v2");
2340