blob: a6a5796e9c41f2ca371c9b2a2b38093234fbe59e [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Will Deacon45ae7cf2013-06-24 18:31:25 +01002/*
3 * IOMMU API for ARM architected SMMU implementations.
4 *
Will Deacon45ae7cf2013-06-24 18:31:25 +01005 * 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
Will Deacon45ae7cf2013-06-24 18:31:25 +010014 * - Context fault reporting
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +030015 * - Extended Stream ID (16 bit)
Will Deacon45ae7cf2013-06-24 18:31:25 +010016 */
17
18#define pr_fmt(fmt) "arm-smmu: " fmt
19
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +000020#include <linux/acpi.h>
21#include <linux/acpi_iort.h>
Robin Murphy0caf5f42019-08-15 19:37:23 +010022#include <linux/bitfield.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010023#include <linux/delay.h>
Robin Murphy9adb9592016-01-26 18:06:36 +000024#include <linux/dma-iommu.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010025#include <linux/dma-mapping.h>
26#include <linux/err.h>
27#include <linux/interrupt.h>
28#include <linux/io.h>
Mitchel Humpherys859a7322014-10-29 21:13:40 +000029#include <linux/iopoll.h>
Will Deaconb06c0762019-12-19 12:03:45 +000030#include <linux/module.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010031#include <linux/of.h>
Robin Murphybae2c2d2015-07-29 19:46:05 +010032#include <linux/of_address.h>
Robin Murphyd6fc5d92016-09-12 17:13:52 +010033#include <linux/of_device.h>
Robin Murphyadfec2e2016-09-12 17:13:55 +010034#include <linux/of_iommu.h>
Will Deacona9a1b0b2014-05-01 18:05:08 +010035#include <linux/pci.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010036#include <linux/platform_device.h>
Sricharan R96a299d2018-12-04 11:52:09 +053037#include <linux/pm_runtime.h>
Robin Murphy931a0ba2019-09-17 15:45:34 +010038#include <linux/ratelimit.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010039#include <linux/slab.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010040
41#include <linux/amba/bus.h>
Nipun Guptaeab03e22018-09-10 19:19:18 +053042#include <linux/fsl/mc.h>
Will Deacon45ae7cf2013-06-24 18:31:25 +010043
Robin Murphyc5fc6482019-08-15 19:37:32 +010044#include "arm-smmu.h"
Rob Clark2b037742017-08-09 10:43:03 -040045
Robin Murphy4e4abae2019-06-03 14:15:37 +020046/*
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
Rob Clark2b037742017-08-09 10:43:03 -040055#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
56#define TLB_SPIN_COUNT 10
Will Deacon45ae7cf2013-06-24 18:31:25 +010057
Eric Augerf3ebee82017-01-19 20:57:55 +000058#define MSI_IOVA_BASE 0x8000000
59#define MSI_IOVA_LENGTH 0x100000
60
Will Deacon4cf740b2014-07-14 19:47:39 +010061static int force_stage;
Robin Murphy25a1c962016-02-10 14:25:33 +000062module_param(force_stage, int, S_IRUGO);
Will Deacon4cf740b2014-07-14 19:47:39 +010063MODULE_PARM_DESC(force_stage,
64 "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.");
Douglas Anderson954a03b2019-03-01 11:20:17 -080065static bool disable_bypass =
66 IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT);
Robin Murphy25a1c962016-02-10 14:25:33 +000067module_param(disable_bypass, bool, S_IRUGO);
68MODULE_PARM_DESC(disable_bypass,
69 "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.");
Will Deacon4cf740b2014-07-14 19:47:39 +010070
Robin Murphy8e8b2032016-09-12 17:13:50 +010071struct arm_smmu_s2cr {
Robin Murphy588888a2016-09-12 17:13:54 +010072 struct iommu_group *group;
73 int count;
Robin Murphy8e8b2032016-09-12 17:13:50 +010074 enum arm_smmu_s2cr_type type;
75 enum arm_smmu_s2cr_privcfg privcfg;
76 u8 cbndx;
77};
78
79#define s2cr_init_val (struct arm_smmu_s2cr){ \
80 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
81}
82
Will Deacon45ae7cf2013-06-24 18:31:25 +010083struct arm_smmu_smr {
Will Deacon45ae7cf2013-06-24 18:31:25 +010084 u16 mask;
85 u16 id;
Robin Murphy1f3d5ca2016-09-12 17:13:49 +010086 bool valid;
Will Deacon45ae7cf2013-06-24 18:31:25 +010087};
88
Robin Murphy90df3732017-08-08 14:56:14 +010089struct arm_smmu_cb {
90 u64 ttbr[2];
91 u32 tcr[2];
92 u32 mair[2];
93 struct arm_smmu_cfg *cfg;
94};
95
Will Deacona9a1b0b2014-05-01 18:05:08 +010096struct arm_smmu_master_cfg {
Robin Murphyf80cd882016-09-14 15:21:39 +010097 struct arm_smmu_device *smmu;
Robin Murphyadfec2e2016-09-12 17:13:55 +010098 s16 smendx[];
Will Deacon45ae7cf2013-06-24 18:31:25 +010099};
Robin Murphy1f3d5ca2016-09-12 17:13:49 +0100100#define INVALID_SMENDX -1
Robin Murphy24651702020-03-26 16:08:35 +0100101#define cfg_smendx(cfg, fw, i) \
102 (i >= fw->num_ids ? INVALID_SMENDX : cfg->smendx[i])
103#define for_each_cfg_sme(cfg, fw, i, idx) \
104 for (i = 0; idx = cfg_smendx(cfg, fw, i), i < fw->num_ids; ++i)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100105
Robin Murphy021bb842016-09-14 15:26:46 +0100106static bool using_legacy_binding, using_generic_binding;
107
Sricharan Rd4a44f02018-12-04 11:52:10 +0530108static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
109{
110 if (pm_runtime_enabled(smmu->dev))
111 return pm_runtime_get_sync(smmu->dev);
112
113 return 0;
114}
115
116static inline void arm_smmu_rpm_put(struct arm_smmu_device *smmu)
117{
118 if (pm_runtime_enabled(smmu->dev))
Rob Clarkee9bdfe2019-10-31 14:31:02 -0700119 pm_runtime_put_autosuspend(smmu->dev);
Sricharan Rd4a44f02018-12-04 11:52:10 +0530120}
121
Joerg Roedel1d672632015-03-26 13:43:10 +0100122static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
123{
124 return container_of(dom, struct arm_smmu_domain, domain);
125}
126
Will Deaconcd221bd2019-12-19 12:03:51 +0000127static struct platform_driver arm_smmu_driver;
128static struct iommu_ops arm_smmu_ops;
129
130#ifdef CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS
131static int arm_smmu_bus_init(struct iommu_ops *ops);
132
Will Deacon8f68f8e2014-07-15 11:27:08 +0100133static struct device_node *dev_get_dev_node(struct device *dev)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100134{
135 if (dev_is_pci(dev)) {
136 struct pci_bus *bus = to_pci_dev(dev)->bus;
Mitchel Humpherys29073202014-07-08 09:52:18 -0700137
Will Deacona9a1b0b2014-05-01 18:05:08 +0100138 while (!pci_is_root_bus(bus))
139 bus = bus->parent;
Robin Murphyf80cd882016-09-14 15:21:39 +0100140 return of_node_get(bus->bridge->parent->of_node);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100141 }
142
Robin Murphyf80cd882016-09-14 15:21:39 +0100143 return of_node_get(dev->of_node);
Will Deacona9a1b0b2014-05-01 18:05:08 +0100144}
145
Robin Murphyf80cd882016-09-14 15:21:39 +0100146static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100147{
Robin Murphyf80cd882016-09-14 15:21:39 +0100148 *((__be32 *)data) = cpu_to_be32(alias);
149 return 0; /* Continue walking */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100150}
151
Robin Murphyf80cd882016-09-14 15:21:39 +0100152static int __find_legacy_master_phandle(struct device *dev, void *data)
Will Deacona9a1b0b2014-05-01 18:05:08 +0100153{
Robin Murphyf80cd882016-09-14 15:21:39 +0100154 struct of_phandle_iterator *it = *(void **)data;
155 struct device_node *np = it->node;
156 int err;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100157
Robin Murphyf80cd882016-09-14 15:21:39 +0100158 of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
Uwe Kleine-Königc680e9a2019-08-24 15:28:45 +0200159 "#stream-id-cells", -1)
Robin Murphyf80cd882016-09-14 15:21:39 +0100160 if (it->node == np) {
161 *(void **)data = dev;
162 return 1;
Olav Haugan3c8766d2014-08-22 17:12:32 -0700163 }
Robin Murphyf80cd882016-09-14 15:21:39 +0100164 it->node = np;
165 return err == -ENOENT ? 0 : err;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100166}
167
Robin Murphyadfec2e2016-09-12 17:13:55 +0100168static int arm_smmu_register_legacy_master(struct device *dev,
169 struct arm_smmu_device **smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100170{
Robin Murphyadfec2e2016-09-12 17:13:55 +0100171 struct device *smmu_dev;
Robin Murphyf80cd882016-09-14 15:21:39 +0100172 struct device_node *np;
173 struct of_phandle_iterator it;
174 void *data = &it;
Robin Murphyadfec2e2016-09-12 17:13:55 +0100175 u32 *sids;
Robin Murphyf80cd882016-09-14 15:21:39 +0100176 __be32 pci_sid;
177 int err;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100178
Robin Murphyf80cd882016-09-14 15:21:39 +0100179 np = dev_get_dev_node(dev);
180 if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
181 of_node_put(np);
182 return -ENODEV;
183 }
184
185 it.node = np;
Robin Murphyd6fc5d92016-09-12 17:13:52 +0100186 err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
187 __find_legacy_master_phandle);
Robin Murphyadfec2e2016-09-12 17:13:55 +0100188 smmu_dev = data;
Robin Murphyf80cd882016-09-14 15:21:39 +0100189 of_node_put(np);
190 if (err == 0)
191 return -ENODEV;
192 if (err < 0)
193 return err;
Will Deacon44680ee2014-06-25 11:29:12 +0100194
Robin Murphyf80cd882016-09-14 15:21:39 +0100195 if (dev_is_pci(dev)) {
196 /* "mmu-masters" assumes Stream ID == Requester ID */
197 pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
198 &pci_sid);
199 it.cur = &pci_sid;
200 it.cur_count = 1;
201 }
202
Robin Murphyadfec2e2016-09-12 17:13:55 +0100203 err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
204 &arm_smmu_ops);
205 if (err)
206 return err;
207
208 sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
209 if (!sids)
Robin Murphyf80cd882016-09-14 15:21:39 +0100210 return -ENOMEM;
211
Robin Murphyadfec2e2016-09-12 17:13:55 +0100212 *smmu = dev_get_drvdata(smmu_dev);
213 of_phandle_iterator_args(&it, sids, it.cur_count);
214 err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
215 kfree(sids);
216 return err;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100217}
218
Will Deaconcd221bd2019-12-19 12:03:51 +0000219/*
220 * With the legacy DT binding in play, we have no guarantees about
221 * probe order, but then we're also not doing default domains, so we can
222 * delay setting bus ops until we're sure every possible SMMU is ready,
223 * and that way ensure that no add_device() calls get missed.
224 */
225static int arm_smmu_legacy_bus_init(void)
226{
227 if (using_legacy_binding)
228 return arm_smmu_bus_init(&arm_smmu_ops);
229 return 0;
230}
231device_initcall_sync(arm_smmu_legacy_bus_init);
232#else
233static int arm_smmu_register_legacy_master(struct device *dev,
234 struct arm_smmu_device **smmu)
235{
236 return -ENODEV;
237}
238#endif /* CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS */
239
Will Deacon45ae7cf2013-06-24 18:31:25 +0100240static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
241{
242 int idx;
243
244 do {
245 idx = find_next_zero_bit(map, end, start);
246 if (idx == end)
247 return -ENOSPC;
248 } while (test_and_set_bit(idx, map));
249
250 return idx;
251}
252
253static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
254{
255 clear_bit(idx, map);
256}
257
258/* Wait for any pending TLB invalidations to complete */
Robin Murphy19713fd2019-08-15 19:37:30 +0100259static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
260 int sync, int status)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100261{
Robin Murphy8513c892017-03-30 17:56:32 +0100262 unsigned int spin_cnt, delay;
Robin Murphy19713fd2019-08-15 19:37:30 +0100263 u32 reg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100264
Robin Murphyae2b60f2019-09-18 17:17:50 +0100265 if (smmu->impl && unlikely(smmu->impl->tlb_sync))
266 return smmu->impl->tlb_sync(smmu, page, sync, status);
267
Robin Murphy19713fd2019-08-15 19:37:30 +0100268 arm_smmu_writel(smmu, page, sync, QCOM_DUMMY_VAL);
Robin Murphy8513c892017-03-30 17:56:32 +0100269 for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
270 for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
Robin Murphy19713fd2019-08-15 19:37:30 +0100271 reg = arm_smmu_readl(smmu, page, status);
Will Deaconfba6e962020-01-10 13:20:03 +0000272 if (!(reg & ARM_SMMU_sTLBGSTATUS_GSACTIVE))
Robin Murphy8513c892017-03-30 17:56:32 +0100273 return;
274 cpu_relax();
Will Deacon45ae7cf2013-06-24 18:31:25 +0100275 }
Robin Murphy8513c892017-03-30 17:56:32 +0100276 udelay(delay);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100277 }
Robin Murphy8513c892017-03-30 17:56:32 +0100278 dev_err_ratelimited(smmu->dev,
279 "TLB sync timed out -- SMMU may be deadlocked\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +0100280}
281
Robin Murphy11febfc2017-03-30 17:56:31 +0100282static void arm_smmu_tlb_sync_global(struct arm_smmu_device *smmu)
Will Deacon1463fe42013-07-31 19:21:27 +0100283{
Will Deacon8e517e72017-07-06 15:55:48 +0100284 unsigned long flags;
Robin Murphy11febfc2017-03-30 17:56:31 +0100285
Will Deacon8e517e72017-07-06 15:55:48 +0100286 spin_lock_irqsave(&smmu->global_sync_lock, flags);
Robin Murphy00320ce2019-08-15 19:37:31 +0100287 __arm_smmu_tlb_sync(smmu, ARM_SMMU_GR0, ARM_SMMU_GR0_sTLBGSYNC,
Robin Murphy19713fd2019-08-15 19:37:30 +0100288 ARM_SMMU_GR0_sTLBGSTATUS);
Will Deacon8e517e72017-07-06 15:55:48 +0100289 spin_unlock_irqrestore(&smmu->global_sync_lock, flags);
Will Deacon518f7132014-11-14 17:17:54 +0000290}
291
Robin Murphyae2b60f2019-09-18 17:17:50 +0100292static void arm_smmu_tlb_sync_context(struct arm_smmu_domain *smmu_domain)
Will Deacon1463fe42013-07-31 19:21:27 +0100293{
Robin Murphy11febfc2017-03-30 17:56:31 +0100294 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon8e517e72017-07-06 15:55:48 +0100295 unsigned long flags;
Robin Murphy11febfc2017-03-30 17:56:31 +0100296
Will Deacon8e517e72017-07-06 15:55:48 +0100297 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
Robin Murphy19713fd2019-08-15 19:37:30 +0100298 __arm_smmu_tlb_sync(smmu, ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx),
299 ARM_SMMU_CB_TLBSYNC, ARM_SMMU_CB_TLBSTATUS);
Will Deacon8e517e72017-07-06 15:55:48 +0100300 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
Will Deacon518f7132014-11-14 17:17:54 +0000301}
302
Robin Murphy11febfc2017-03-30 17:56:31 +0100303static void arm_smmu_tlb_inv_context_s1(void *cookie)
Will Deacon518f7132014-11-14 17:17:54 +0000304{
305 struct arm_smmu_domain *smmu_domain = cookie;
Robin Murphy44f68762018-09-20 17:10:27 +0100306 /*
Robin Murphy19713fd2019-08-15 19:37:30 +0100307 * The TLBI write may be relaxed, so ensure that PTEs cleared by the
308 * current CPU are visible beforehand.
Robin Murphy44f68762018-09-20 17:10:27 +0100309 */
Robin Murphy19713fd2019-08-15 19:37:30 +0100310 wmb();
311 arm_smmu_cb_write(smmu_domain->smmu, smmu_domain->cfg.cbndx,
312 ARM_SMMU_CB_S1_TLBIASID, smmu_domain->cfg.asid);
Robin Murphyae2b60f2019-09-18 17:17:50 +0100313 arm_smmu_tlb_sync_context(smmu_domain);
Robin Murphy11febfc2017-03-30 17:56:31 +0100314}
315
316static void arm_smmu_tlb_inv_context_s2(void *cookie)
317{
318 struct arm_smmu_domain *smmu_domain = cookie;
Will Deacon44680ee2014-06-25 11:29:12 +0100319 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon1463fe42013-07-31 19:21:27 +0100320
Robin Murphy00320ce2019-08-15 19:37:31 +0100321 /* See above */
322 wmb();
323 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
Robin Murphy11febfc2017-03-30 17:56:31 +0100324 arm_smmu_tlb_sync_global(smmu);
Will Deacon1463fe42013-07-31 19:21:27 +0100325}
326
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100327static void arm_smmu_tlb_inv_range_s1(unsigned long iova, size_t size,
Robin Murphy3370cb62019-09-18 17:17:49 +0100328 size_t granule, void *cookie, int reg)
Will Deacon518f7132014-11-14 17:17:54 +0000329{
330 struct arm_smmu_domain *smmu_domain = cookie;
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100331 struct arm_smmu_device *smmu = smmu_domain->smmu;
Will Deacon518f7132014-11-14 17:17:54 +0000332 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Robin Murphy3370cb62019-09-18 17:17:49 +0100333 int idx = cfg->cbndx;
Will Deacon518f7132014-11-14 17:17:54 +0000334
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100335 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
Will Deacon7d321bd32018-10-01 12:42:49 +0100336 wmb();
337
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100338 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
339 iova = (iova >> 12) << 12;
340 iova |= cfg->asid;
Robin Murphy75df1382015-12-07 18:18:52 +0000341 do {
Robin Murphy19713fd2019-08-15 19:37:30 +0100342 arm_smmu_cb_write(smmu, idx, reg, iova);
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100343 iova += granule;
344 } while (size -= granule);
345 } else {
346 iova >>= 12;
347 iova |= (u64)cfg->asid << 48;
348 do {
Robin Murphy19713fd2019-08-15 19:37:30 +0100349 arm_smmu_cb_writeq(smmu, idx, reg, iova);
Robin Murphy75df1382015-12-07 18:18:52 +0000350 iova += granule >> 12;
351 } while (size -= granule);
Will Deacon518f7132014-11-14 17:17:54 +0000352 }
353}
354
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100355static void arm_smmu_tlb_inv_range_s2(unsigned long iova, size_t size,
Robin Murphy3370cb62019-09-18 17:17:49 +0100356 size_t granule, void *cookie, int reg)
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100357{
358 struct arm_smmu_domain *smmu_domain = cookie;
359 struct arm_smmu_device *smmu = smmu_domain->smmu;
Robin Murphy3370cb62019-09-18 17:17:49 +0100360 int idx = smmu_domain->cfg.cbndx;
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100361
362 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
363 wmb();
364
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100365 iova >>= 12;
366 do {
Robin Murphy61005762019-08-15 19:37:28 +0100367 if (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64)
Robin Murphy19713fd2019-08-15 19:37:30 +0100368 arm_smmu_cb_writeq(smmu, idx, reg, iova);
Robin Murphy61005762019-08-15 19:37:28 +0100369 else
Robin Murphy19713fd2019-08-15 19:37:30 +0100370 arm_smmu_cb_write(smmu, idx, reg, iova);
Robin Murphy71e8a8c2019-08-15 19:37:27 +0100371 iova += granule >> 12;
372 } while (size -= granule);
373}
374
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100375static void arm_smmu_tlb_inv_walk_s1(unsigned long iova, size_t size,
376 size_t granule, void *cookie)
377{
Robin Murphy3370cb62019-09-18 17:17:49 +0100378 arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
379 ARM_SMMU_CB_S1_TLBIVA);
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100380 arm_smmu_tlb_sync_context(cookie);
381}
382
383static void arm_smmu_tlb_inv_leaf_s1(unsigned long iova, size_t size,
384 size_t granule, void *cookie)
385{
Robin Murphy3370cb62019-09-18 17:17:49 +0100386 arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
387 ARM_SMMU_CB_S1_TLBIVAL);
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100388 arm_smmu_tlb_sync_context(cookie);
389}
390
391static void arm_smmu_tlb_add_page_s1(struct iommu_iotlb_gather *gather,
392 unsigned long iova, size_t granule,
393 void *cookie)
394{
Robin Murphy3370cb62019-09-18 17:17:49 +0100395 arm_smmu_tlb_inv_range_s1(iova, granule, granule, cookie,
396 ARM_SMMU_CB_S1_TLBIVAL);
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100397}
398
399static void arm_smmu_tlb_inv_walk_s2(unsigned long iova, size_t size,
400 size_t granule, void *cookie)
401{
Robin Murphy3370cb62019-09-18 17:17:49 +0100402 arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
403 ARM_SMMU_CB_S2_TLBIIPAS2);
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100404 arm_smmu_tlb_sync_context(cookie);
405}
406
407static void arm_smmu_tlb_inv_leaf_s2(unsigned long iova, size_t size,
408 size_t granule, void *cookie)
409{
Robin Murphy3370cb62019-09-18 17:17:49 +0100410 arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
411 ARM_SMMU_CB_S2_TLBIIPAS2L);
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100412 arm_smmu_tlb_sync_context(cookie);
413}
414
415static void arm_smmu_tlb_add_page_s2(struct iommu_iotlb_gather *gather,
416 unsigned long iova, size_t granule,
417 void *cookie)
418{
Robin Murphy3370cb62019-09-18 17:17:49 +0100419 arm_smmu_tlb_inv_range_s2(iova, granule, granule, cookie,
420 ARM_SMMU_CB_S2_TLBIIPAS2L);
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100421}
422
423static void arm_smmu_tlb_inv_any_s2_v1(unsigned long iova, size_t size,
424 size_t granule, void *cookie)
425{
426 arm_smmu_tlb_inv_context_s2(cookie);
427}
Robin Murphy11febfc2017-03-30 17:56:31 +0100428/*
429 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
430 * almost negligible, but the benefit of getting the first one in as far ahead
431 * of the sync as possible is significant, hence we don't just make this a
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100432 * no-op and call arm_smmu_tlb_inv_context_s2() from .iotlb_sync as you might
433 * think.
Robin Murphy11febfc2017-03-30 17:56:31 +0100434 */
Robin Murphy3f3b8d02019-09-18 17:17:48 +0100435static void arm_smmu_tlb_add_page_s2_v1(struct iommu_iotlb_gather *gather,
436 unsigned long iova, size_t granule,
437 void *cookie)
Robin Murphy11febfc2017-03-30 17:56:31 +0100438{
439 struct arm_smmu_domain *smmu_domain = cookie;
Robin Murphy00320ce2019-08-15 19:37:31 +0100440 struct arm_smmu_device *smmu = smmu_domain->smmu;
Robin Murphy11febfc2017-03-30 17:56:31 +0100441
Robin Murphy00320ce2019-08-15 19:37:31 +0100442 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
Will Deacon7d321bd32018-10-01 12:42:49 +0100443 wmb();
444
Robin Murphy00320ce2019-08-15 19:37:31 +0100445 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
Robin Murphy11febfc2017-03-30 17:56:31 +0100446}
447
Robin Murphy696bcfb2019-09-18 17:17:51 +0100448static const struct iommu_flush_ops arm_smmu_s1_tlb_ops = {
449 .tlb_flush_all = arm_smmu_tlb_inv_context_s1,
450 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s1,
451 .tlb_flush_leaf = arm_smmu_tlb_inv_leaf_s1,
452 .tlb_add_page = arm_smmu_tlb_add_page_s1,
Robin Murphy11febfc2017-03-30 17:56:31 +0100453};
454
Robin Murphy696bcfb2019-09-18 17:17:51 +0100455static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v2 = {
456 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
457 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s2,
458 .tlb_flush_leaf = arm_smmu_tlb_inv_leaf_s2,
459 .tlb_add_page = arm_smmu_tlb_add_page_s2,
Robin Murphy11febfc2017-03-30 17:56:31 +0100460};
461
Robin Murphy696bcfb2019-09-18 17:17:51 +0100462static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v1 = {
463 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
464 .tlb_flush_walk = arm_smmu_tlb_inv_any_s2_v1,
465 .tlb_flush_leaf = arm_smmu_tlb_inv_any_s2_v1,
466 .tlb_add_page = arm_smmu_tlb_add_page_s2_v1,
Will Deacon518f7132014-11-14 17:17:54 +0000467};
468
Will Deacon45ae7cf2013-06-24 18:31:25 +0100469static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
470{
Vivek Gautambc580b52019-04-22 12:40:36 +0530471 u32 fsr, fsynr, cbfrsynra;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100472 unsigned long iova;
473 struct iommu_domain *domain = dev;
Joerg Roedel1d672632015-03-26 13:43:10 +0100474 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100475 struct arm_smmu_device *smmu = smmu_domain->smmu;
Robin Murphy19713fd2019-08-15 19:37:30 +0100476 int idx = smmu_domain->cfg.cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100477
Robin Murphy19713fd2019-08-15 19:37:30 +0100478 fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
Will Deaconfba6e962020-01-10 13:20:03 +0000479 if (!(fsr & ARM_SMMU_FSR_FAULT))
Will Deacon45ae7cf2013-06-24 18:31:25 +0100480 return IRQ_NONE;
481
Robin Murphy19713fd2019-08-15 19:37:30 +0100482 fsynr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSYNR0);
483 iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
484 cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
Will Deacon45ae7cf2013-06-24 18:31:25 +0100485
Will Deacon3714ce1d2016-08-05 19:49:45 +0100486 dev_err_ratelimited(smmu->dev,
Vivek Gautambc580b52019-04-22 12:40:36 +0530487 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
Robin Murphy19713fd2019-08-15 19:37:30 +0100488 fsr, iova, fsynr, cbfrsynra, idx);
Will Deacon3714ce1d2016-08-05 19:49:45 +0100489
Robin Murphy19713fd2019-08-15 19:37:30 +0100490 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
Will Deacon3714ce1d2016-08-05 19:49:45 +0100491 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100492}
493
494static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
495{
496 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
497 struct arm_smmu_device *smmu = dev;
Robin Murphy931a0ba2019-09-17 15:45:34 +0100498 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
499 DEFAULT_RATELIMIT_BURST);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100500
Robin Murphy00320ce2019-08-15 19:37:31 +0100501 gfsr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
502 gfsynr0 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR0);
503 gfsynr1 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR1);
504 gfsynr2 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR2);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100505
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +0000506 if (!gfsr)
507 return IRQ_NONE;
508
Robin Murphy931a0ba2019-09-17 15:45:34 +0100509 if (__ratelimit(&rs)) {
510 if (IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT) &&
Will Deaconfba6e962020-01-10 13:20:03 +0000511 (gfsr & ARM_SMMU_sGFSR_USF))
Robin Murphy931a0ba2019-09-17 15:45:34 +0100512 dev_err(smmu->dev,
513 "Blocked unknown Stream ID 0x%hx; boot with \"arm-smmu.disable_bypass=0\" to allow, but this may have security implications\n",
514 (u16)gfsynr1);
515 else
516 dev_err(smmu->dev,
517 "Unexpected global fault, this could be serious\n");
518 dev_err(smmu->dev,
519 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
520 gfsr, gfsynr0, gfsynr1, gfsynr2);
521 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100522
Robin Murphy00320ce2019-08-15 19:37:31 +0100523 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, gfsr);
Will Deaconadaba322013-07-31 19:21:26 +0100524 return IRQ_HANDLED;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100525}
526
Will Deacon518f7132014-11-14 17:17:54 +0000527static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
528 struct io_pgtable_cfg *pgtbl_cfg)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100529{
Will Deacon44680ee2014-06-25 11:29:12 +0100530 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Robin Murphy90df3732017-08-08 14:56:14 +0100531 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
532 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
533
534 cb->cfg = cfg;
535
Robin Murphy620565a2019-08-15 19:37:25 +0100536 /* TCR */
Robin Murphy90df3732017-08-08 14:56:14 +0100537 if (stage1) {
538 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
539 cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
540 } else {
Robin Murphyfb485eb2019-10-25 19:08:38 +0100541 cb->tcr[0] = arm_smmu_lpae_tcr(pgtbl_cfg);
542 cb->tcr[1] = arm_smmu_lpae_tcr2(pgtbl_cfg);
Robin Murphy90df3732017-08-08 14:56:14 +0100543 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
Will Deaconfba6e962020-01-10 13:20:03 +0000544 cb->tcr[1] |= ARM_SMMU_TCR2_AS;
Robin Murphyfb485eb2019-10-25 19:08:38 +0100545 else
Will Deaconfba6e962020-01-10 13:20:03 +0000546 cb->tcr[0] |= ARM_SMMU_TCR_EAE;
Robin Murphy90df3732017-08-08 14:56:14 +0100547 }
548 } else {
Will Deaconac4b80e2020-01-10 14:51:59 +0000549 cb->tcr[0] = arm_smmu_lpae_vtcr(pgtbl_cfg);
Robin Murphy90df3732017-08-08 14:56:14 +0100550 }
551
552 /* TTBRs */
553 if (stage1) {
554 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
Robin Murphyd1e5f262019-10-25 19:08:37 +0100555 cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr;
556 cb->ttbr[1] = 0;
Robin Murphy90df3732017-08-08 14:56:14 +0100557 } else {
Robin Murphyd1e5f262019-10-25 19:08:37 +0100558 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
Will Deaconfba6e962020-01-10 13:20:03 +0000559 cb->ttbr[0] |= FIELD_PREP(ARM_SMMU_TTBRn_ASID,
560 cfg->asid);
561 cb->ttbr[1] = FIELD_PREP(ARM_SMMU_TTBRn_ASID,
562 cfg->asid);
Robin Murphy90df3732017-08-08 14:56:14 +0100563 }
564 } else {
565 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
566 }
567
568 /* MAIRs (stage-1 only) */
569 if (stage1) {
570 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
571 cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
572 cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
573 } else {
Robin Murphy205577a2019-10-25 19:08:36 +0100574 cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair;
575 cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair >> 32;
Robin Murphy90df3732017-08-08 14:56:14 +0100576 }
577 }
578}
579
580static void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
581{
582 u32 reg;
583 bool stage1;
584 struct arm_smmu_cb *cb = &smmu->cbs[idx];
585 struct arm_smmu_cfg *cfg = cb->cfg;
Robin Murphy90df3732017-08-08 14:56:14 +0100586
587 /* Unassigned context banks only need disabling */
588 if (!cfg) {
Robin Murphy19713fd2019-08-15 19:37:30 +0100589 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, 0);
Robin Murphy90df3732017-08-08 14:56:14 +0100590 return;
591 }
592
Will Deacon44680ee2014-06-25 11:29:12 +0100593 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100594
Robin Murphy90df3732017-08-08 14:56:14 +0100595 /* CBA2R */
Will Deacon4a1c93c2015-03-04 12:21:03 +0000596 if (smmu->version > ARM_SMMU_V1) {
Robin Murphy7602b872016-04-28 17:12:09 +0100597 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
Will Deaconfba6e962020-01-10 13:20:03 +0000598 reg = ARM_SMMU_CBA2R_VA64;
Robin Murphy7602b872016-04-28 17:12:09 +0100599 else
Robin Murphy5114e962019-08-15 19:37:24 +0100600 reg = 0;
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800601 /* 16-bit VMIDs live in CBA2R */
602 if (smmu->features & ARM_SMMU_FEAT_VMID16)
Will Deaconfba6e962020-01-10 13:20:03 +0000603 reg |= FIELD_PREP(ARM_SMMU_CBA2R_VMID16, cfg->vmid);
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800604
Robin Murphyaadbf212019-08-15 19:37:29 +0100605 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBA2R(idx), reg);
Will Deacon4a1c93c2015-03-04 12:21:03 +0000606 }
607
Will Deacon45ae7cf2013-06-24 18:31:25 +0100608 /* CBAR */
Will Deaconfba6e962020-01-10 13:20:03 +0000609 reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, cfg->cbar);
Robin Murphyb7862e32016-04-13 18:13:03 +0100610 if (smmu->version < ARM_SMMU_V2)
Will Deaconfba6e962020-01-10 13:20:03 +0000611 reg |= FIELD_PREP(ARM_SMMU_CBAR_IRPTNDX, cfg->irptndx);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100612
Will Deacon57ca90f2014-02-06 14:59:05 +0000613 /*
614 * Use the weakest shareability/memory types, so they are
615 * overridden by the ttbcr/pte.
616 */
617 if (stage1) {
Will Deaconfba6e962020-01-10 13:20:03 +0000618 reg |= FIELD_PREP(ARM_SMMU_CBAR_S1_BPSHCFG,
619 ARM_SMMU_CBAR_S1_BPSHCFG_NSH) |
620 FIELD_PREP(ARM_SMMU_CBAR_S1_MEMATTR,
621 ARM_SMMU_CBAR_S1_MEMATTR_WB);
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -0800622 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
623 /* 8-bit VMIDs live in CBAR */
Will Deaconfba6e962020-01-10 13:20:03 +0000624 reg |= FIELD_PREP(ARM_SMMU_CBAR_VMID, cfg->vmid);
Will Deacon57ca90f2014-02-06 14:59:05 +0000625 }
Robin Murphyaadbf212019-08-15 19:37:29 +0100626 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(idx), reg);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100627
Sunil Goutham125458a2017-03-28 16:11:12 +0530628 /*
Robin Murphy620565a2019-08-15 19:37:25 +0100629 * TCR
Sunil Goutham125458a2017-03-28 16:11:12 +0530630 * We must write this before the TTBRs, since it determines the
631 * access behaviour of some fields (in particular, ASID[15:8]).
632 */
Robin Murphy90df3732017-08-08 14:56:14 +0100633 if (stage1 && smmu->version > ARM_SMMU_V1)
Robin Murphy19713fd2019-08-15 19:37:30 +0100634 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR2, cb->tcr[1]);
635 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR, cb->tcr[0]);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100636
Will Deacon45ae7cf2013-06-24 18:31:25 +0100637 /* TTBRs */
Robin Murphy90df3732017-08-08 14:56:14 +0100638 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
Robin Murphy19713fd2019-08-15 19:37:30 +0100639 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_CONTEXTIDR, cfg->asid);
640 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
641 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR1, cb->ttbr[1]);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100642 } else {
Robin Murphy19713fd2019-08-15 19:37:30 +0100643 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
Robin Murphy90df3732017-08-08 14:56:14 +0100644 if (stage1)
Robin Murphy19713fd2019-08-15 19:37:30 +0100645 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR1,
646 cb->ttbr[1]);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100647 }
648
Will Deacon518f7132014-11-14 17:17:54 +0000649 /* MAIRs (stage-1 only) */
Will Deacon45ae7cf2013-06-24 18:31:25 +0100650 if (stage1) {
Robin Murphy19713fd2019-08-15 19:37:30 +0100651 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR0, cb->mair[0]);
652 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR1, cb->mair[1]);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100653 }
654
Will Deacon45ae7cf2013-06-24 18:31:25 +0100655 /* SCTLR */
Will Deaconfba6e962020-01-10 13:20:03 +0000656 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE | ARM_SMMU_SCTLR_AFE |
657 ARM_SMMU_SCTLR_TRE | ARM_SMMU_SCTLR_M;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100658 if (stage1)
Will Deaconfba6e962020-01-10 13:20:03 +0000659 reg |= ARM_SMMU_SCTLR_S1_ASIDPNE;
Robin Murphy90df3732017-08-08 14:56:14 +0100660 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
Will Deaconfba6e962020-01-10 13:20:03 +0000661 reg |= ARM_SMMU_SCTLR_E;
Robin Murphy90df3732017-08-08 14:56:14 +0100662
Robin Murphy19713fd2019-08-15 19:37:30 +0100663 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100664}
665
666static int arm_smmu_init_domain_context(struct iommu_domain *domain,
Will Deacon44680ee2014-06-25 11:29:12 +0100667 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100668{
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100669 int irq, start, ret = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000670 unsigned long ias, oas;
671 struct io_pgtable_ops *pgtbl_ops;
672 struct io_pgtable_cfg pgtbl_cfg;
673 enum io_pgtable_fmt fmt;
Joerg Roedel1d672632015-03-26 13:43:10 +0100674 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100675 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100676
Will Deacon518f7132014-11-14 17:17:54 +0000677 mutex_lock(&smmu_domain->init_mutex);
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100678 if (smmu_domain->smmu)
679 goto out_unlock;
680
Will Deacon61bc6712017-01-06 16:56:03 +0000681 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
682 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
683 smmu_domain->smmu = smmu;
684 goto out_unlock;
685 }
686
Will Deaconc752ce42014-06-25 22:46:31 +0100687 /*
688 * Mapping the requested stage onto what we support is surprisingly
689 * complicated, mainly because the spec allows S1+S2 SMMUs without
690 * support for nested translation. That means we end up with the
691 * following table:
692 *
693 * Requested Supported Actual
694 * S1 N S1
695 * S1 S1+S2 S1
696 * S1 S2 S2
697 * S1 S1 S1
698 * N N N
699 * N S1+S2 S2
700 * N S2 S2
701 * N S1 S1
702 *
703 * Note that you can't actually request stage-2 mappings.
704 */
705 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
706 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
707 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
708 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
709
Robin Murphy7602b872016-04-28 17:12:09 +0100710 /*
711 * Choosing a suitable context format is even more fiddly. Until we
712 * grow some way for the caller to express a preference, and/or move
713 * the decision into the io-pgtable code where it arguably belongs,
714 * just aim for the closest thing to the rest of the system, and hope
715 * that the hardware isn't esoteric enough that we can't assume AArch64
716 * support to be a superset of AArch32 support...
717 */
718 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
719 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
Robin Murphy60705292016-08-11 17:44:06 +0100720 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
721 !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
722 (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
723 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
724 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
Robin Murphy7602b872016-04-28 17:12:09 +0100725 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
726 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
727 ARM_SMMU_FEAT_FMT_AARCH64_16K |
728 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
729 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
730
731 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
732 ret = -EINVAL;
733 goto out_unlock;
734 }
735
Will Deaconc752ce42014-06-25 22:46:31 +0100736 switch (smmu_domain->stage) {
737 case ARM_SMMU_DOMAIN_S1:
738 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
739 start = smmu->num_s2_context_banks;
Will Deacon518f7132014-11-14 17:17:54 +0000740 ias = smmu->va_size;
741 oas = smmu->ipa_size;
Robin Murphy7602b872016-04-28 17:12:09 +0100742 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
Will Deacon518f7132014-11-14 17:17:54 +0000743 fmt = ARM_64_LPAE_S1;
Robin Murphy60705292016-08-11 17:44:06 +0100744 } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
Will Deacon518f7132014-11-14 17:17:54 +0000745 fmt = ARM_32_LPAE_S1;
Robin Murphy7602b872016-04-28 17:12:09 +0100746 ias = min(ias, 32UL);
747 oas = min(oas, 40UL);
Robin Murphy60705292016-08-11 17:44:06 +0100748 } else {
749 fmt = ARM_V7S;
750 ias = min(ias, 32UL);
751 oas = min(oas, 32UL);
Robin Murphy7602b872016-04-28 17:12:09 +0100752 }
Will Deaconabfd6fe2019-07-02 16:44:41 +0100753 smmu_domain->flush_ops = &arm_smmu_s1_tlb_ops;
Will Deaconc752ce42014-06-25 22:46:31 +0100754 break;
755 case ARM_SMMU_DOMAIN_NESTED:
Will Deacon45ae7cf2013-06-24 18:31:25 +0100756 /*
757 * We will likely want to change this if/when KVM gets
758 * involved.
759 */
Will Deaconc752ce42014-06-25 22:46:31 +0100760 case ARM_SMMU_DOMAIN_S2:
Will Deacon9c5c92e2014-06-25 12:12:41 +0100761 cfg->cbar = CBAR_TYPE_S2_TRANS;
762 start = 0;
Will Deacon518f7132014-11-14 17:17:54 +0000763 ias = smmu->ipa_size;
764 oas = smmu->pa_size;
Robin Murphy7602b872016-04-28 17:12:09 +0100765 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
Will Deacon518f7132014-11-14 17:17:54 +0000766 fmt = ARM_64_LPAE_S2;
Robin Murphy7602b872016-04-28 17:12:09 +0100767 } else {
Will Deacon518f7132014-11-14 17:17:54 +0000768 fmt = ARM_32_LPAE_S2;
Robin Murphy7602b872016-04-28 17:12:09 +0100769 ias = min(ias, 40UL);
770 oas = min(oas, 40UL);
771 }
Robin Murphy11febfc2017-03-30 17:56:31 +0100772 if (smmu->version == ARM_SMMU_V2)
Will Deaconabfd6fe2019-07-02 16:44:41 +0100773 smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v2;
Robin Murphy11febfc2017-03-30 17:56:31 +0100774 else
Will Deaconabfd6fe2019-07-02 16:44:41 +0100775 smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v1;
Will Deaconc752ce42014-06-25 22:46:31 +0100776 break;
777 default:
778 ret = -EINVAL;
779 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100780 }
Will Deacon45ae7cf2013-06-24 18:31:25 +0100781 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
782 smmu->num_context_banks);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200783 if (ret < 0)
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100784 goto out_unlock;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100785
Will Deacon44680ee2014-06-25 11:29:12 +0100786 cfg->cbndx = ret;
Robin Murphyb7862e32016-04-13 18:13:03 +0100787 if (smmu->version < ARM_SMMU_V2) {
Will Deacon44680ee2014-06-25 11:29:12 +0100788 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
789 cfg->irptndx %= smmu->num_context_irqs;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100790 } else {
Will Deacon44680ee2014-06-25 11:29:12 +0100791 cfg->irptndx = cfg->cbndx;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100792 }
793
Robin Murphy280b6832017-03-30 17:56:29 +0100794 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
Robin Murphyba7e4a02019-08-15 19:37:37 +0100795 cfg->vmid = cfg->cbndx + 1;
Robin Murphy280b6832017-03-30 17:56:29 +0100796 else
Robin Murphyba7e4a02019-08-15 19:37:37 +0100797 cfg->asid = cfg->cbndx;
798
799 smmu_domain->smmu = smmu;
800 if (smmu->impl && smmu->impl->init_context) {
801 ret = smmu->impl->init_context(smmu_domain);
802 if (ret)
803 goto out_unlock;
804 }
Robin Murphy280b6832017-03-30 17:56:29 +0100805
Will Deacon518f7132014-11-14 17:17:54 +0000806 pgtbl_cfg = (struct io_pgtable_cfg) {
Robin Murphyd5466352016-05-09 17:20:09 +0100807 .pgsize_bitmap = smmu->pgsize_bitmap,
Will Deacon518f7132014-11-14 17:17:54 +0000808 .ias = ias,
809 .oas = oas,
Will Deacon4f418452019-06-25 12:51:25 +0100810 .coherent_walk = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK,
Robin Murphy696bcfb2019-09-18 17:17:51 +0100811 .tlb = smmu_domain->flush_ops,
Robin Murphy2df7a252015-07-29 19:46:06 +0100812 .iommu_dev = smmu->dev,
Will Deacon518f7132014-11-14 17:17:54 +0000813 };
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100814
Robin Murphy44f68762018-09-20 17:10:27 +0100815 if (smmu_domain->non_strict)
816 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
817
Will Deacon518f7132014-11-14 17:17:54 +0000818 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
819 if (!pgtbl_ops) {
820 ret = -ENOMEM;
821 goto out_clear_smmu;
822 }
823
Robin Murphyd5466352016-05-09 17:20:09 +0100824 /* Update the domain's page sizes to reflect the page table format */
825 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
Robin Murphy455eb7d2016-09-12 17:13:58 +0100826 domain->geometry.aperture_end = (1UL << ias) - 1;
827 domain->geometry.force_aperture = true;
Will Deacon518f7132014-11-14 17:17:54 +0000828
829 /* Initialise the context bank with our page table cfg */
830 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
Robin Murphy90df3732017-08-08 14:56:14 +0100831 arm_smmu_write_context_bank(smmu, cfg->cbndx);
Will Deacon518f7132014-11-14 17:17:54 +0000832
833 /*
834 * Request context fault interrupt. Do this last to avoid the
835 * handler seeing a half-initialised domain state.
836 */
Will Deacon44680ee2014-06-25 11:29:12 +0100837 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Peng Fanbee14002016-07-04 17:38:22 +0800838 ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
839 IRQF_SHARED, "arm-smmu-context-fault", domain);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200840 if (ret < 0) {
Will Deacon45ae7cf2013-06-24 18:31:25 +0100841 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
Will Deacon44680ee2014-06-25 11:29:12 +0100842 cfg->irptndx, irq);
Will Deaconfba6e962020-01-10 13:20:03 +0000843 cfg->irptndx = ARM_SMMU_INVALID_IRPTNDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100844 }
845
Will Deacon518f7132014-11-14 17:17:54 +0000846 mutex_unlock(&smmu_domain->init_mutex);
847
848 /* Publish page table ops for map/unmap */
849 smmu_domain->pgtbl_ops = pgtbl_ops;
Will Deacona9a1b0b2014-05-01 18:05:08 +0100850 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100851
Will Deacon518f7132014-11-14 17:17:54 +0000852out_clear_smmu:
Liu Xiang6db7bfb2019-09-16 21:53:00 +0800853 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Will Deacon518f7132014-11-14 17:17:54 +0000854 smmu_domain->smmu = NULL;
Mitchel Humpherysa18037b2014-07-30 18:58:13 +0100855out_unlock:
Will Deacon518f7132014-11-14 17:17:54 +0000856 mutex_unlock(&smmu_domain->init_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100857 return ret;
858}
859
860static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
861{
Joerg Roedel1d672632015-03-26 13:43:10 +0100862 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon44680ee2014-06-25 11:29:12 +0100863 struct arm_smmu_device *smmu = smmu_domain->smmu;
864 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
Sricharan Rd4a44f02018-12-04 11:52:10 +0530865 int ret, irq;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100866
Will Deacon61bc6712017-01-06 16:56:03 +0000867 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100868 return;
869
Sricharan Rd4a44f02018-12-04 11:52:10 +0530870 ret = arm_smmu_rpm_get(smmu);
871 if (ret < 0)
872 return;
873
Will Deacon518f7132014-11-14 17:17:54 +0000874 /*
875 * Disable the context bank and free the page tables before freeing
876 * it.
877 */
Robin Murphy90df3732017-08-08 14:56:14 +0100878 smmu->cbs[cfg->cbndx].cfg = NULL;
879 arm_smmu_write_context_bank(smmu, cfg->cbndx);
Will Deacon1463fe42013-07-31 19:21:27 +0100880
Will Deaconfba6e962020-01-10 13:20:03 +0000881 if (cfg->irptndx != ARM_SMMU_INVALID_IRPTNDX) {
Will Deacon44680ee2014-06-25 11:29:12 +0100882 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
Peng Fanbee14002016-07-04 17:38:22 +0800883 devm_free_irq(smmu->dev, irq, domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100884 }
885
Markus Elfring44830b02015-11-06 18:32:41 +0100886 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon44680ee2014-06-25 11:29:12 +0100887 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
Sricharan Rd4a44f02018-12-04 11:52:10 +0530888
889 arm_smmu_rpm_put(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100890}
891
Joerg Roedel1d672632015-03-26 13:43:10 +0100892static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100893{
894 struct arm_smmu_domain *smmu_domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100895
Will Deacon61bc6712017-01-06 16:56:03 +0000896 if (type != IOMMU_DOMAIN_UNMANAGED &&
897 type != IOMMU_DOMAIN_DMA &&
898 type != IOMMU_DOMAIN_IDENTITY)
Joerg Roedel1d672632015-03-26 13:43:10 +0100899 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100900 /*
901 * Allocate the domain and initialise some of its data structures.
902 * We can't really do anything meaningful until we've added a
903 * master.
904 */
905 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
906 if (!smmu_domain)
Joerg Roedel1d672632015-03-26 13:43:10 +0100907 return NULL;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100908
Robin Murphy021bb842016-09-14 15:26:46 +0100909 if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
910 iommu_get_dma_cookie(&smmu_domain->domain))) {
Robin Murphy9adb9592016-01-26 18:06:36 +0000911 kfree(smmu_domain);
912 return NULL;
913 }
914
Will Deacon518f7132014-11-14 17:17:54 +0000915 mutex_init(&smmu_domain->init_mutex);
Robin Murphy523d7422017-06-22 16:53:56 +0100916 spin_lock_init(&smmu_domain->cb_lock);
Joerg Roedel1d672632015-03-26 13:43:10 +0100917
918 return &smmu_domain->domain;
Will Deacon45ae7cf2013-06-24 18:31:25 +0100919}
920
Joerg Roedel1d672632015-03-26 13:43:10 +0100921static void arm_smmu_domain_free(struct iommu_domain *domain)
Will Deacon45ae7cf2013-06-24 18:31:25 +0100922{
Joerg Roedel1d672632015-03-26 13:43:10 +0100923 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deacon1463fe42013-07-31 19:21:27 +0100924
925 /*
926 * Free the domain resources. We assume that all devices have
927 * already been detached.
928 */
Robin Murphy9adb9592016-01-26 18:06:36 +0000929 iommu_put_dma_cookie(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100930 arm_smmu_destroy_domain_context(domain);
Will Deacon45ae7cf2013-06-24 18:31:25 +0100931 kfree(smmu_domain);
932}
933
Robin Murphy1f3d5ca2016-09-12 17:13:49 +0100934static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
935{
936 struct arm_smmu_smr *smr = smmu->smrs + idx;
Will Deaconfba6e962020-01-10 13:20:03 +0000937 u32 reg = FIELD_PREP(ARM_SMMU_SMR_ID, smr->id) |
938 FIELD_PREP(ARM_SMMU_SMR_MASK, smr->mask);
Robin Murphy1f3d5ca2016-09-12 17:13:49 +0100939
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300940 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
Will Deaconfba6e962020-01-10 13:20:03 +0000941 reg |= ARM_SMMU_SMR_VALID;
Robin Murphy00320ce2019-08-15 19:37:31 +0100942 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(idx), reg);
Robin Murphy1f3d5ca2016-09-12 17:13:49 +0100943}
944
Robin Murphy8e8b2032016-09-12 17:13:50 +0100945static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
946{
947 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
Will Deaconfba6e962020-01-10 13:20:03 +0000948 u32 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, s2cr->type) |
949 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, s2cr->cbndx) |
950 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
Robin Murphy8e8b2032016-09-12 17:13:50 +0100951
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300952 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
953 smmu->smrs[idx].valid)
Will Deaconfba6e962020-01-10 13:20:03 +0000954 reg |= ARM_SMMU_S2CR_EXIDVALID;
Robin Murphy00320ce2019-08-15 19:37:31 +0100955 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
Robin Murphy8e8b2032016-09-12 17:13:50 +0100956}
957
958static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
959{
960 arm_smmu_write_s2cr(smmu, idx);
961 if (smmu->smrs)
962 arm_smmu_write_smr(smmu, idx);
963}
964
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300965/*
966 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
967 * should be called after sCR0 is written.
968 */
969static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
970{
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300971 u32 smr;
Robin Murphy79f7a5c2020-01-10 15:25:02 +0000972 int i;
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300973
974 if (!smmu->smrs)
975 return;
Robin Murphy79f7a5c2020-01-10 15:25:02 +0000976 /*
977 * If we've had to accommodate firmware memory regions, we may
978 * have live SMRs by now; tread carefully...
979 *
980 * Somewhat perversely, not having a free SMR for this test implies we
981 * can get away without it anyway, as we'll only be able to 'allocate'
982 * these SMRs for the ID/mask values we're already trusting to be OK.
983 */
984 for (i = 0; i < smmu->num_mapping_groups; i++)
985 if (!smmu->smrs[i].valid)
986 goto smr_ok;
987 return;
988smr_ok:
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300989 /*
990 * SMR.ID bits may not be preserved if the corresponding MASK
991 * bits are set, so check each one separately. We can reject
992 * masters later if they try to claim IDs outside these masks.
993 */
Will Deaconfba6e962020-01-10 13:20:03 +0000994 smr = FIELD_PREP(ARM_SMMU_SMR_ID, smmu->streamid_mask);
Robin Murphy79f7a5c2020-01-10 15:25:02 +0000995 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
996 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
Will Deaconfba6e962020-01-10 13:20:03 +0000997 smmu->streamid_mask = FIELD_GET(ARM_SMMU_SMR_ID, smr);
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +0300998
Will Deaconfba6e962020-01-10 13:20:03 +0000999 smr = FIELD_PREP(ARM_SMMU_SMR_MASK, smmu->streamid_mask);
Robin Murphy79f7a5c2020-01-10 15:25:02 +00001000 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
1001 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
Will Deaconfba6e962020-01-10 13:20:03 +00001002 smmu->smr_mask_mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +03001003}
1004
Robin Murphy588888a2016-09-12 17:13:54 +01001005static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001006{
1007 struct arm_smmu_smr *smrs = smmu->smrs;
Robin Murphy588888a2016-09-12 17:13:54 +01001008 int i, free_idx = -ENOSPC;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001009
Robin Murphy588888a2016-09-12 17:13:54 +01001010 /* Stream indexing is blissfully easy */
1011 if (!smrs)
1012 return id;
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001013
Robin Murphy588888a2016-09-12 17:13:54 +01001014 /* Validating SMRs is... less so */
1015 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1016 if (!smrs[i].valid) {
1017 /*
1018 * Note the first free entry we come across, which
1019 * we'll claim in the end if nothing else matches.
1020 */
1021 if (free_idx < 0)
1022 free_idx = i;
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001023 continue;
1024 }
Robin Murphy588888a2016-09-12 17:13:54 +01001025 /*
1026 * If the new entry is _entirely_ matched by an existing entry,
1027 * then reuse that, with the guarantee that there also cannot
1028 * be any subsequent conflicting entries. In normal use we'd
1029 * expect simply identical entries for this case, but there's
1030 * no harm in accommodating the generalisation.
1031 */
1032 if ((mask & smrs[i].mask) == mask &&
1033 !((id ^ smrs[i].id) & ~smrs[i].mask))
1034 return i;
1035 /*
1036 * If the new entry has any other overlap with an existing one,
1037 * though, then there always exists at least one stream ID
1038 * which would cause a conflict, and we can't allow that risk.
1039 */
1040 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1041 return -EINVAL;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001042 }
1043
Robin Murphy588888a2016-09-12 17:13:54 +01001044 return free_idx;
1045}
1046
1047static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1048{
1049 if (--smmu->s2crs[idx].count)
1050 return false;
1051
1052 smmu->s2crs[idx] = s2cr_init_val;
1053 if (smmu->smrs)
1054 smmu->smrs[idx].valid = false;
1055
1056 return true;
1057}
1058
1059static int arm_smmu_master_alloc_smes(struct device *dev)
1060{
Joerg Roedel9b468f72018-11-29 14:01:00 +01001061 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelc84500a2020-03-26 16:08:36 +01001062 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
Robin Murphy588888a2016-09-12 17:13:54 +01001063 struct arm_smmu_device *smmu = cfg->smmu;
1064 struct arm_smmu_smr *smrs = smmu->smrs;
1065 struct iommu_group *group;
1066 int i, idx, ret;
1067
1068 mutex_lock(&smmu->stream_map_mutex);
1069 /* Figure out a viable stream map entry allocation */
Robin Murphy24651702020-03-26 16:08:35 +01001070 for_each_cfg_sme(cfg, fwspec, i, idx) {
Will Deaconfba6e962020-01-10 13:20:03 +00001071 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1072 u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
Robin Murphy021bb842016-09-14 15:26:46 +01001073
Robin Murphy588888a2016-09-12 17:13:54 +01001074 if (idx != INVALID_SMENDX) {
1075 ret = -EEXIST;
1076 goto out_err;
1077 }
1078
Robin Murphy021bb842016-09-14 15:26:46 +01001079 ret = arm_smmu_find_sme(smmu, sid, mask);
Robin Murphy588888a2016-09-12 17:13:54 +01001080 if (ret < 0)
1081 goto out_err;
1082
1083 idx = ret;
1084 if (smrs && smmu->s2crs[idx].count == 0) {
Robin Murphy021bb842016-09-14 15:26:46 +01001085 smrs[idx].id = sid;
1086 smrs[idx].mask = mask;
Robin Murphy588888a2016-09-12 17:13:54 +01001087 smrs[idx].valid = true;
1088 }
1089 smmu->s2crs[idx].count++;
1090 cfg->smendx[i] = (s16)idx;
1091 }
1092
1093 group = iommu_group_get_for_dev(dev);
Robin Murphy588888a2016-09-12 17:13:54 +01001094 if (IS_ERR(group)) {
1095 ret = PTR_ERR(group);
1096 goto out_err;
1097 }
1098 iommu_group_put(group);
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001099
Will Deacon45ae7cf2013-06-24 18:31:25 +01001100 /* It worked! Now, poke the actual hardware */
Robin Murphy24651702020-03-26 16:08:35 +01001101 for_each_cfg_sme(cfg, fwspec, i, idx) {
Robin Murphy588888a2016-09-12 17:13:54 +01001102 arm_smmu_write_sme(smmu, idx);
1103 smmu->s2crs[idx].group = group;
1104 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001105
Robin Murphy588888a2016-09-12 17:13:54 +01001106 mutex_unlock(&smmu->stream_map_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001107 return 0;
1108
Robin Murphy588888a2016-09-12 17:13:54 +01001109out_err:
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001110 while (i--) {
Robin Murphy588888a2016-09-12 17:13:54 +01001111 arm_smmu_free_sme(smmu, cfg->smendx[i]);
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001112 cfg->smendx[i] = INVALID_SMENDX;
1113 }
Robin Murphy588888a2016-09-12 17:13:54 +01001114 mutex_unlock(&smmu->stream_map_mutex);
1115 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001116}
1117
Robin Murphy24651702020-03-26 16:08:35 +01001118static void arm_smmu_master_free_smes(struct arm_smmu_master_cfg *cfg,
1119 struct iommu_fwspec *fwspec)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001120{
Robin Murphy24651702020-03-26 16:08:35 +01001121 struct arm_smmu_device *smmu = cfg->smmu;
Robin Murphyd3097e32016-09-12 17:13:53 +01001122 int i, idx;
Will Deacon43b412b2014-07-15 11:22:24 +01001123
Robin Murphy588888a2016-09-12 17:13:54 +01001124 mutex_lock(&smmu->stream_map_mutex);
Robin Murphy24651702020-03-26 16:08:35 +01001125 for_each_cfg_sme(cfg, fwspec, i, idx) {
Robin Murphy588888a2016-09-12 17:13:54 +01001126 if (arm_smmu_free_sme(smmu, idx))
1127 arm_smmu_write_sme(smmu, idx);
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001128 cfg->smendx[i] = INVALID_SMENDX;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001129 }
Robin Murphy588888a2016-09-12 17:13:54 +01001130 mutex_unlock(&smmu->stream_map_mutex);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001131}
1132
Will Deacon45ae7cf2013-06-24 18:31:25 +01001133static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
Robin Murphy24651702020-03-26 16:08:35 +01001134 struct arm_smmu_master_cfg *cfg,
Robin Murphyadfec2e2016-09-12 17:13:55 +01001135 struct iommu_fwspec *fwspec)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001136{
Will Deacon44680ee2014-06-25 11:29:12 +01001137 struct arm_smmu_device *smmu = smmu_domain->smmu;
Robin Murphy8e8b2032016-09-12 17:13:50 +01001138 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
Robin Murphy8e8b2032016-09-12 17:13:50 +01001139 u8 cbndx = smmu_domain->cfg.cbndx;
Will Deacon61bc6712017-01-06 16:56:03 +00001140 enum arm_smmu_s2cr_type type;
Robin Murphy588888a2016-09-12 17:13:54 +01001141 int i, idx;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001142
Will Deacon61bc6712017-01-06 16:56:03 +00001143 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1144 type = S2CR_TYPE_BYPASS;
1145 else
1146 type = S2CR_TYPE_TRANS;
1147
Robin Murphy24651702020-03-26 16:08:35 +01001148 for_each_cfg_sme(cfg, fwspec, i, idx) {
Robin Murphy8e8b2032016-09-12 17:13:50 +01001149 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
Robin Murphy588888a2016-09-12 17:13:54 +01001150 continue;
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001151
Robin Murphy8e8b2032016-09-12 17:13:50 +01001152 s2cr[idx].type = type;
Sricharan Re1989802017-01-06 18:58:15 +05301153 s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
Robin Murphy8e8b2032016-09-12 17:13:50 +01001154 s2cr[idx].cbndx = cbndx;
1155 arm_smmu_write_s2cr(smmu, idx);
Will Deacon43b412b2014-07-15 11:22:24 +01001156 }
Robin Murphy8e8b2032016-09-12 17:13:50 +01001157 return 0;
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001158}
1159
Will Deacon45ae7cf2013-06-24 18:31:25 +01001160static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1161{
Joerg Roedel1d672632015-03-26 13:43:10 +01001162 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Joerg Roedelc84500a2020-03-26 16:08:36 +01001163 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy24651702020-03-26 16:08:35 +01001164 struct arm_smmu_master_cfg *cfg;
1165 struct arm_smmu_device *smmu;
Joerg Roedelc84500a2020-03-26 16:08:36 +01001166 int ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001167
Robin Murphyadfec2e2016-09-12 17:13:55 +01001168 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001169 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1170 return -ENXIO;
1171 }
1172
Robin Murphyfba4f8e2016-10-17 12:06:21 +01001173 /*
1174 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1175 * domains between of_xlate() and add_device() - we have no way to cope
1176 * with that, so until ARM gets converted to rely on groups and default
1177 * domains, just say no (but more politely than by dereferencing NULL).
1178 * This should be at least a WARN_ON once that's sorted.
1179 */
Joerg Roedelc84500a2020-03-26 16:08:36 +01001180 cfg = dev_iommu_priv_get(dev);
Robin Murphy24651702020-03-26 16:08:35 +01001181 if (!cfg)
Robin Murphyfba4f8e2016-10-17 12:06:21 +01001182 return -ENODEV;
1183
Robin Murphy24651702020-03-26 16:08:35 +01001184 smmu = cfg->smmu;
Sricharan Rd4a44f02018-12-04 11:52:10 +05301185
1186 ret = arm_smmu_rpm_get(smmu);
1187 if (ret < 0)
1188 return ret;
1189
Will Deacon518f7132014-11-14 17:17:54 +00001190 /* Ensure that the domain is finalised */
Robin Murphyadfec2e2016-09-12 17:13:55 +01001191 ret = arm_smmu_init_domain_context(domain, smmu);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001192 if (ret < 0)
Sricharan Rd4a44f02018-12-04 11:52:10 +05301193 goto rpm_put;
Will Deacon518f7132014-11-14 17:17:54 +00001194
Will Deacon45ae7cf2013-06-24 18:31:25 +01001195 /*
Will Deacon44680ee2014-06-25 11:29:12 +01001196 * Sanity check the domain. We don't support domains across
1197 * different SMMUs.
Will Deacon45ae7cf2013-06-24 18:31:25 +01001198 */
Robin Murphyadfec2e2016-09-12 17:13:55 +01001199 if (smmu_domain->smmu != smmu) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001200 dev_err(dev,
1201 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
Robin Murphyadfec2e2016-09-12 17:13:55 +01001202 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
Sricharan Rd4a44f02018-12-04 11:52:10 +05301203 ret = -EINVAL;
1204 goto rpm_put;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001205 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001206
1207 /* Looks ok, so add the device to the domain */
Robin Murphy24651702020-03-26 16:08:35 +01001208 ret = arm_smmu_domain_add_master(smmu_domain, cfg, fwspec);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301209
Rob Clarkee9bdfe2019-10-31 14:31:02 -07001210 /*
1211 * Setup an autosuspend delay to avoid bouncing runpm state.
1212 * Otherwise, if a driver for a suspended consumer device
1213 * unmaps buffers, it will runpm resume/suspend for each one.
1214 *
1215 * For example, when used by a GPU device, when an application
1216 * or game exits, it can trigger unmapping 100s or 1000s of
1217 * buffers. With a runpm cycle for each buffer, that adds up
1218 * to 5-10sec worth of reprogramming the context bank, while
1219 * the system appears to be locked up to the user.
1220 */
1221 pm_runtime_set_autosuspend_delay(smmu->dev, 20);
1222 pm_runtime_use_autosuspend(smmu->dev);
1223
Sricharan Rd4a44f02018-12-04 11:52:10 +05301224rpm_put:
1225 arm_smmu_rpm_put(smmu);
1226 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001227}
1228
Will Deacon45ae7cf2013-06-24 18:31:25 +01001229static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
Tom Murphy781ca2d2019-09-08 09:56:38 -07001230 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001231{
Robin Murphy523d7422017-06-22 16:53:56 +01001232 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
Sricharan Rd4a44f02018-12-04 11:52:10 +05301233 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1234 int ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001235
Will Deacon518f7132014-11-14 17:17:54 +00001236 if (!ops)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001237 return -ENODEV;
1238
Sricharan Rd4a44f02018-12-04 11:52:10 +05301239 arm_smmu_rpm_get(smmu);
1240 ret = ops->map(ops, iova, paddr, size, prot);
1241 arm_smmu_rpm_put(smmu);
1242
1243 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001244}
1245
1246static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
Will Deacon56f8af52019-07-02 16:44:06 +01001247 size_t size, struct iommu_iotlb_gather *gather)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001248{
Robin Murphy523d7422017-06-22 16:53:56 +01001249 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
Sricharan Rd4a44f02018-12-04 11:52:10 +05301250 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1251 size_t ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001252
Will Deacon518f7132014-11-14 17:17:54 +00001253 if (!ops)
1254 return 0;
1255
Sricharan Rd4a44f02018-12-04 11:52:10 +05301256 arm_smmu_rpm_get(smmu);
Will Deacona2d3a382019-07-02 16:44:58 +01001257 ret = ops->unmap(ops, iova, size, gather);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301258 arm_smmu_rpm_put(smmu);
1259
1260 return ret;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001261}
1262
Robin Murphy44f68762018-09-20 17:10:27 +01001263static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1264{
1265 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301266 struct arm_smmu_device *smmu = smmu_domain->smmu;
Robin Murphy44f68762018-09-20 17:10:27 +01001267
Will Deaconabfd6fe2019-07-02 16:44:41 +01001268 if (smmu_domain->flush_ops) {
Sricharan Rd4a44f02018-12-04 11:52:10 +05301269 arm_smmu_rpm_get(smmu);
Robin Murphy696bcfb2019-09-18 17:17:51 +01001270 smmu_domain->flush_ops->tlb_flush_all(smmu_domain);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301271 arm_smmu_rpm_put(smmu);
1272 }
Robin Murphy44f68762018-09-20 17:10:27 +01001273}
1274
Will Deacon56f8af52019-07-02 16:44:06 +01001275static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
1276 struct iommu_iotlb_gather *gather)
Robin Murphy32b12442017-09-28 15:55:01 +01001277{
1278 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301279 struct arm_smmu_device *smmu = smmu_domain->smmu;
Robin Murphy32b12442017-09-28 15:55:01 +01001280
Robin Murphyae2b60f2019-09-18 17:17:50 +01001281 if (!smmu)
1282 return;
1283
1284 arm_smmu_rpm_get(smmu);
1285 if (smmu->version == ARM_SMMU_V2 ||
1286 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1287 arm_smmu_tlb_sync_context(smmu_domain);
1288 else
1289 arm_smmu_tlb_sync_global(smmu);
1290 arm_smmu_rpm_put(smmu);
Robin Murphy32b12442017-09-28 15:55:01 +01001291}
1292
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001293static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1294 dma_addr_t iova)
1295{
Joerg Roedel1d672632015-03-26 13:43:10 +01001296 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001297 struct arm_smmu_device *smmu = smmu_domain->smmu;
1298 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1299 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1300 struct device *dev = smmu->dev;
Robin Murphy19713fd2019-08-15 19:37:30 +01001301 void __iomem *reg;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001302 u32 tmp;
1303 u64 phys;
Robin Murphy523d7422017-06-22 16:53:56 +01001304 unsigned long va, flags;
Robin Murphy19713fd2019-08-15 19:37:30 +01001305 int ret, idx = cfg->cbndx;
Sricharan Rd4a44f02018-12-04 11:52:10 +05301306
1307 ret = arm_smmu_rpm_get(smmu);
1308 if (ret < 0)
1309 return 0;
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001310
Robin Murphy523d7422017-06-22 16:53:56 +01001311 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
Robin Murphy661d9622015-05-27 17:09:34 +01001312 va = iova & ~0xfffUL;
Robin Murphy61005762019-08-15 19:37:28 +01001313 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
Robin Murphy19713fd2019-08-15 19:37:30 +01001314 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
Robin Murphy61005762019-08-15 19:37:28 +01001315 else
Robin Murphy19713fd2019-08-15 19:37:30 +01001316 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001317
Robin Murphy19713fd2019-08-15 19:37:30 +01001318 reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx)) + ARM_SMMU_CB_ATSR;
Will Deaconfba6e962020-01-10 13:20:03 +00001319 if (readl_poll_timeout_atomic(reg, tmp, !(tmp & ARM_SMMU_ATSR_ACTIVE),
1320 5, 50)) {
Robin Murphy523d7422017-06-22 16:53:56 +01001321 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001322 dev_err(dev,
Fabio Estevam077124c2015-08-18 17:12:24 +01001323 "iova to phys timed out on %pad. Falling back to software table walk.\n",
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001324 &iova);
1325 return ops->iova_to_phys(ops, iova);
1326 }
1327
Robin Murphy19713fd2019-08-15 19:37:30 +01001328 phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
Robin Murphy523d7422017-06-22 16:53:56 +01001329 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
Will Deaconfba6e962020-01-10 13:20:03 +00001330 if (phys & ARM_SMMU_CB_PAR_F) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001331 dev_err(dev, "translation fault!\n");
1332 dev_err(dev, "PAR = 0x%llx\n", phys);
1333 return 0;
1334 }
1335
Sricharan Rd4a44f02018-12-04 11:52:10 +05301336 arm_smmu_rpm_put(smmu);
1337
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001338 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1339}
1340
Will Deacon45ae7cf2013-06-24 18:31:25 +01001341static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001342 dma_addr_t iova)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001343{
Joerg Roedel1d672632015-03-26 13:43:10 +01001344 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Robin Murphy523d7422017-06-22 16:53:56 +01001345 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001346
Sunil Gouthambdf95922017-04-25 15:27:52 +05301347 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1348 return iova;
1349
Will Deacon518f7132014-11-14 17:17:54 +00001350 if (!ops)
Will Deacona44a97912013-11-07 18:47:50 +00001351 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001352
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001353 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
Robin Murphy523d7422017-06-22 16:53:56 +01001354 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1355 return arm_smmu_iova_to_phys_hard(domain, iova);
Baptiste Reynal83a60ed2015-03-04 16:51:06 +01001356
Robin Murphy523d7422017-06-22 16:53:56 +01001357 return ops->iova_to_phys(ops, iova);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001358}
1359
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001360static bool arm_smmu_capable(enum iommu_cap cap)
Will Deacon45ae7cf2013-06-24 18:31:25 +01001361{
Will Deacond0948942014-06-24 17:30:10 +01001362 switch (cap) {
1363 case IOMMU_CAP_CACHE_COHERENCY:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001364 /*
1365 * Return true here as the SMMU can always send out coherent
1366 * requests.
1367 */
1368 return true;
Antonios Motakis0029a8d2014-10-13 14:06:18 +01001369 case IOMMU_CAP_NOEXEC:
1370 return true;
Will Deacond0948942014-06-24 17:30:10 +01001371 default:
Joerg Roedel1fd0c772014-09-05 10:49:34 +02001372 return false;
Will Deacond0948942014-06-24 17:30:10 +01001373 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01001374}
Will Deacon45ae7cf2013-06-24 18:31:25 +01001375
Lorenzo Pieralisice9babe2016-11-21 10:01:37 +00001376static
1377struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
Robin Murphy021bb842016-09-14 15:26:46 +01001378{
Suzuki K Poulose67843bb2019-07-23 23:18:34 +01001379 struct device *dev = driver_find_device_by_fwnode(&arm_smmu_driver.driver,
1380 fwnode);
Robin Murphy021bb842016-09-14 15:26:46 +01001381 put_device(dev);
1382 return dev ? dev_get_drvdata(dev) : NULL;
1383}
1384
Will Deacon03edb222015-01-19 14:27:33 +00001385static int arm_smmu_add_device(struct device *dev)
1386{
Joerg Roedel0b242eb2020-03-26 16:08:32 +01001387 struct arm_smmu_device *smmu = NULL;
Robin Murphyf80cd882016-09-14 15:21:39 +01001388 struct arm_smmu_master_cfg *cfg;
Joerg Roedel9b468f72018-11-29 14:01:00 +01001389 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphyf80cd882016-09-14 15:21:39 +01001390 int i, ret;
1391
Robin Murphy021bb842016-09-14 15:26:46 +01001392 if (using_legacy_binding) {
1393 ret = arm_smmu_register_legacy_master(dev, &smmu);
Artem Savkova7990c62017-08-08 12:26:02 +02001394
1395 /*
1396 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1397 * will allocate/initialise a new one. Thus we need to update fwspec for
1398 * later use.
1399 */
Joerg Roedel9b468f72018-11-29 14:01:00 +01001400 fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy021bb842016-09-14 15:26:46 +01001401 if (ret)
1402 goto out_free;
Robin Murphy3c117b52016-11-02 17:31:32 +00001403 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
Lorenzo Pieralisice9babe2016-11-21 10:01:37 +00001404 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
Robin Murphy021bb842016-09-14 15:26:46 +01001405 } else {
1406 return -ENODEV;
1407 }
Robin Murphyf80cd882016-09-14 15:21:39 +01001408
1409 ret = -EINVAL;
Robin Murphyadfec2e2016-09-12 17:13:55 +01001410 for (i = 0; i < fwspec->num_ids; i++) {
Will Deaconfba6e962020-01-10 13:20:03 +00001411 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1412 u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
Robin Murphyf80cd882016-09-14 15:21:39 +01001413
Robin Murphyadfec2e2016-09-12 17:13:55 +01001414 if (sid & ~smmu->streamid_mask) {
Robin Murphyf80cd882016-09-14 15:21:39 +01001415 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
Robin Murphy021bb842016-09-14 15:26:46 +01001416 sid, smmu->streamid_mask);
1417 goto out_free;
1418 }
1419 if (mask & ~smmu->smr_mask_mask) {
1420 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
Peng Fan6323f472017-04-21 17:03:36 +08001421 mask, smmu->smr_mask_mask);
Robin Murphyf80cd882016-09-14 15:21:39 +01001422 goto out_free;
1423 }
Robin Murphyf80cd882016-09-14 15:21:39 +01001424 }
Will Deacon03edb222015-01-19 14:27:33 +00001425
Robin Murphyadfec2e2016-09-12 17:13:55 +01001426 ret = -ENOMEM;
1427 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1428 GFP_KERNEL);
1429 if (!cfg)
1430 goto out_free;
1431
1432 cfg->smmu = smmu;
Joerg Roedelc84500a2020-03-26 16:08:36 +01001433 dev_iommu_priv_set(dev, cfg);
Robin Murphyadfec2e2016-09-12 17:13:55 +01001434 while (i--)
1435 cfg->smendx[i] = INVALID_SMENDX;
1436
Sricharan Rd4a44f02018-12-04 11:52:10 +05301437 ret = arm_smmu_rpm_get(smmu);
1438 if (ret < 0)
1439 goto out_cfg_free;
1440
Robin Murphy588888a2016-09-12 17:13:54 +01001441 ret = arm_smmu_master_alloc_smes(dev);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301442 arm_smmu_rpm_put(smmu);
1443
Robin Murphyadfec2e2016-09-12 17:13:55 +01001444 if (ret)
Vivek Gautamc54451a2017-07-06 15:07:00 +05301445 goto out_cfg_free;
Robin Murphyadfec2e2016-09-12 17:13:55 +01001446
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001447 iommu_device_link(&smmu->iommu, dev);
1448
Sricharan R655e3642018-12-04 11:52:11 +05301449 device_link_add(dev, smmu->dev,
1450 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1451
Robin Murphyadfec2e2016-09-12 17:13:55 +01001452 return 0;
Robin Murphyf80cd882016-09-14 15:21:39 +01001453
Vivek Gautamc54451a2017-07-06 15:07:00 +05301454out_cfg_free:
1455 kfree(cfg);
Robin Murphyf80cd882016-09-14 15:21:39 +01001456out_free:
Robin Murphyadfec2e2016-09-12 17:13:55 +01001457 iommu_fwspec_free(dev);
Robin Murphyf80cd882016-09-14 15:21:39 +01001458 return ret;
Will Deacon03edb222015-01-19 14:27:33 +00001459}
1460
Will Deacon45ae7cf2013-06-24 18:31:25 +01001461static void arm_smmu_remove_device(struct device *dev)
1462{
Joerg Roedel9b468f72018-11-29 14:01:00 +01001463 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001464 struct arm_smmu_master_cfg *cfg;
1465 struct arm_smmu_device *smmu;
Sricharan Rd4a44f02018-12-04 11:52:10 +05301466 int ret;
Robin Murphy8e8b2032016-09-12 17:13:50 +01001467
Robin Murphyadfec2e2016-09-12 17:13:55 +01001468 if (!fwspec || fwspec->ops != &arm_smmu_ops)
Robin Murphyf80cd882016-09-14 15:21:39 +01001469 return;
Robin Murphy8e8b2032016-09-12 17:13:50 +01001470
Joerg Roedelc84500a2020-03-26 16:08:36 +01001471 cfg = dev_iommu_priv_get(dev);
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001472 smmu = cfg->smmu;
1473
Sricharan Rd4a44f02018-12-04 11:52:10 +05301474 ret = arm_smmu_rpm_get(smmu);
1475 if (ret < 0)
1476 return;
1477
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001478 iommu_device_unlink(&smmu->iommu, dev);
Robin Murphy24651702020-03-26 16:08:35 +01001479 arm_smmu_master_free_smes(cfg, fwspec);
Sricharan Rd4a44f02018-12-04 11:52:10 +05301480
1481 arm_smmu_rpm_put(smmu);
1482
Joerg Roedelc84500a2020-03-26 16:08:36 +01001483 dev_iommu_priv_set(dev, NULL);
Antonios Motakis5fc63a72013-10-18 16:08:29 +01001484 iommu_group_remove_device(dev);
Joerg Roedelc84500a2020-03-26 16:08:36 +01001485 kfree(cfg);
Robin Murphyadfec2e2016-09-12 17:13:55 +01001486 iommu_fwspec_free(dev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001487}
1488
Joerg Roedelaf659932015-10-21 23:51:41 +02001489static struct iommu_group *arm_smmu_device_group(struct device *dev)
1490{
Joerg Roedelc84500a2020-03-26 16:08:36 +01001491 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
Joerg Roedel9b468f72018-11-29 14:01:00 +01001492 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy24651702020-03-26 16:08:35 +01001493 struct arm_smmu_device *smmu = cfg->smmu;
Robin Murphy588888a2016-09-12 17:13:54 +01001494 struct iommu_group *group = NULL;
1495 int i, idx;
1496
Robin Murphy24651702020-03-26 16:08:35 +01001497 for_each_cfg_sme(cfg, fwspec, i, idx) {
Robin Murphy588888a2016-09-12 17:13:54 +01001498 if (group && smmu->s2crs[idx].group &&
1499 group != smmu->s2crs[idx].group)
1500 return ERR_PTR(-EINVAL);
1501
1502 group = smmu->s2crs[idx].group;
1503 }
1504
1505 if (group)
Robin Murphye1b44cb2016-11-11 17:59:22 +00001506 return iommu_group_ref_get(group);
Joerg Roedelaf659932015-10-21 23:51:41 +02001507
1508 if (dev_is_pci(dev))
1509 group = pci_device_group(dev);
Nipun Guptaeab03e22018-09-10 19:19:18 +05301510 else if (dev_is_fsl_mc(dev))
1511 group = fsl_mc_device_group(dev);
Joerg Roedelaf659932015-10-21 23:51:41 +02001512 else
1513 group = generic_device_group(dev);
1514
Joerg Roedelaf659932015-10-21 23:51:41 +02001515 return group;
1516}
1517
Will Deaconc752ce42014-06-25 22:46:31 +01001518static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1519 enum iommu_attr attr, void *data)
1520{
Joerg Roedel1d672632015-03-26 13:43:10 +01001521 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001522
Robin Murphy44f68762018-09-20 17:10:27 +01001523 switch(domain->type) {
1524 case IOMMU_DOMAIN_UNMANAGED:
1525 switch (attr) {
1526 case DOMAIN_ATTR_NESTING:
1527 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1528 return 0;
1529 default:
1530 return -ENODEV;
1531 }
1532 break;
1533 case IOMMU_DOMAIN_DMA:
1534 switch (attr) {
1535 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1536 *(int *)data = smmu_domain->non_strict;
1537 return 0;
1538 default:
1539 return -ENODEV;
1540 }
1541 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001542 default:
Robin Murphy44f68762018-09-20 17:10:27 +01001543 return -EINVAL;
Will Deaconc752ce42014-06-25 22:46:31 +01001544 }
1545}
1546
1547static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1548 enum iommu_attr attr, void *data)
1549{
Will Deacon518f7132014-11-14 17:17:54 +00001550 int ret = 0;
Joerg Roedel1d672632015-03-26 13:43:10 +01001551 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Will Deaconc752ce42014-06-25 22:46:31 +01001552
Will Deacon518f7132014-11-14 17:17:54 +00001553 mutex_lock(&smmu_domain->init_mutex);
1554
Robin Murphy44f68762018-09-20 17:10:27 +01001555 switch(domain->type) {
1556 case IOMMU_DOMAIN_UNMANAGED:
1557 switch (attr) {
1558 case DOMAIN_ATTR_NESTING:
1559 if (smmu_domain->smmu) {
1560 ret = -EPERM;
1561 goto out_unlock;
1562 }
1563
1564 if (*(int *)data)
1565 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1566 else
1567 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1568 break;
1569 default:
1570 ret = -ENODEV;
Will Deacon518f7132014-11-14 17:17:54 +00001571 }
Robin Murphy44f68762018-09-20 17:10:27 +01001572 break;
1573 case IOMMU_DOMAIN_DMA:
1574 switch (attr) {
1575 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1576 smmu_domain->non_strict = *(int *)data;
1577 break;
1578 default:
1579 ret = -ENODEV;
1580 }
Will Deacon518f7132014-11-14 17:17:54 +00001581 break;
Will Deaconc752ce42014-06-25 22:46:31 +01001582 default:
Robin Murphy44f68762018-09-20 17:10:27 +01001583 ret = -EINVAL;
Will Deaconc752ce42014-06-25 22:46:31 +01001584 }
Will Deacon518f7132014-11-14 17:17:54 +00001585out_unlock:
1586 mutex_unlock(&smmu_domain->init_mutex);
1587 return ret;
Will Deaconc752ce42014-06-25 22:46:31 +01001588}
1589
Robin Murphy021bb842016-09-14 15:26:46 +01001590static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1591{
Robin Murphy56fbf602017-03-31 12:03:33 +01001592 u32 mask, fwid = 0;
Robin Murphy021bb842016-09-14 15:26:46 +01001593
1594 if (args->args_count > 0)
Will Deaconfba6e962020-01-10 13:20:03 +00001595 fwid |= FIELD_PREP(ARM_SMMU_SMR_ID, args->args[0]);
Robin Murphy021bb842016-09-14 15:26:46 +01001596
1597 if (args->args_count > 1)
Will Deaconfba6e962020-01-10 13:20:03 +00001598 fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, args->args[1]);
Robin Murphy56fbf602017-03-31 12:03:33 +01001599 else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
Will Deaconfba6e962020-01-10 13:20:03 +00001600 fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, mask);
Robin Murphy021bb842016-09-14 15:26:46 +01001601
1602 return iommu_fwspec_add_ids(dev, &fwid, 1);
1603}
1604
Eric Augerf3ebee82017-01-19 20:57:55 +00001605static void arm_smmu_get_resv_regions(struct device *dev,
1606 struct list_head *head)
1607{
1608 struct iommu_resv_region *region;
1609 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1610
1611 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001612 prot, IOMMU_RESV_SW_MSI);
Eric Augerf3ebee82017-01-19 20:57:55 +00001613 if (!region)
1614 return;
1615
1616 list_add_tail(&region->list, head);
Robin Murphy273df962017-03-16 17:00:19 +00001617
1618 iommu_dma_get_resv_regions(dev, head);
Eric Augerf3ebee82017-01-19 20:57:55 +00001619}
1620
Will Deacon518f7132014-11-14 17:17:54 +00001621static struct iommu_ops arm_smmu_ops = {
Will Deaconc752ce42014-06-25 22:46:31 +01001622 .capable = arm_smmu_capable,
Joerg Roedel1d672632015-03-26 13:43:10 +01001623 .domain_alloc = arm_smmu_domain_alloc,
1624 .domain_free = arm_smmu_domain_free,
Will Deaconc752ce42014-06-25 22:46:31 +01001625 .attach_dev = arm_smmu_attach_dev,
Will Deaconc752ce42014-06-25 22:46:31 +01001626 .map = arm_smmu_map,
1627 .unmap = arm_smmu_unmap,
Robin Murphy44f68762018-09-20 17:10:27 +01001628 .flush_iotlb_all = arm_smmu_flush_iotlb_all,
Robin Murphy32b12442017-09-28 15:55:01 +01001629 .iotlb_sync = arm_smmu_iotlb_sync,
Will Deaconc752ce42014-06-25 22:46:31 +01001630 .iova_to_phys = arm_smmu_iova_to_phys,
1631 .add_device = arm_smmu_add_device,
1632 .remove_device = arm_smmu_remove_device,
Joerg Roedelaf659932015-10-21 23:51:41 +02001633 .device_group = arm_smmu_device_group,
Will Deaconc752ce42014-06-25 22:46:31 +01001634 .domain_get_attr = arm_smmu_domain_get_attr,
1635 .domain_set_attr = arm_smmu_domain_set_attr,
Robin Murphy021bb842016-09-14 15:26:46 +01001636 .of_xlate = arm_smmu_of_xlate,
Eric Augerf3ebee82017-01-19 20:57:55 +00001637 .get_resv_regions = arm_smmu_get_resv_regions,
Thierry Redinga66c5dc2019-12-18 14:42:02 +01001638 .put_resv_regions = generic_iommu_put_resv_regions,
Will Deacon518f7132014-11-14 17:17:54 +00001639 .pgsize_bitmap = -1UL, /* Restricted during device attach */
Will Deacon45ae7cf2013-06-24 18:31:25 +01001640};
1641
1642static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1643{
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001644 int i;
Robin Murphy62b993a2019-08-15 19:37:36 +01001645 u32 reg;
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001646
Andreas Herrmann3a5df8f2014-01-30 18:18:04 +00001647 /* clear global FSR */
Robin Murphy00320ce2019-08-15 19:37:31 +01001648 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
1649 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, reg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001650
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001651 /*
1652 * Reset stream mapping groups: Initial values mark all SMRn as
1653 * invalid and all S2CRn as bypass unless overridden.
1654 */
Robin Murphy8e8b2032016-09-12 17:13:50 +01001655 for (i = 0; i < smmu->num_mapping_groups; ++i)
1656 arm_smmu_write_sme(smmu, i);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001657
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001658 /* Make sure all context banks are disabled and clear CB_FSR */
1659 for (i = 0; i < smmu->num_context_banks; ++i) {
Robin Murphy90df3732017-08-08 14:56:14 +01001660 arm_smmu_write_context_bank(smmu, i);
Will Deaconfba6e962020-01-10 13:20:03 +00001661 arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_FSR, ARM_SMMU_FSR_FAULT);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001662 }
Will Deacon1463fe42013-07-31 19:21:27 +01001663
Will Deacon45ae7cf2013-06-24 18:31:25 +01001664 /* Invalidate the TLB, just in case */
Robin Murphy00320ce2019-08-15 19:37:31 +01001665 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLH, QCOM_DUMMY_VAL);
1666 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLNSNH, QCOM_DUMMY_VAL);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001667
Robin Murphy00320ce2019-08-15 19:37:31 +01001668 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sCR0);
Andreas Herrmann659db6f2013-10-01 13:39:09 +01001669
Will Deacon45ae7cf2013-06-24 18:31:25 +01001670 /* Enable fault reporting */
Will Deaconfba6e962020-01-10 13:20:03 +00001671 reg |= (ARM_SMMU_sCR0_GFRE | ARM_SMMU_sCR0_GFIE |
1672 ARM_SMMU_sCR0_GCFGFRE | ARM_SMMU_sCR0_GCFGFIE);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001673
1674 /* Disable TLB broadcasting. */
Will Deaconfba6e962020-01-10 13:20:03 +00001675 reg |= (ARM_SMMU_sCR0_VMIDPNE | ARM_SMMU_sCR0_PTM);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001676
Robin Murphy25a1c962016-02-10 14:25:33 +00001677 /* Enable client access, handling unmatched streams as appropriate */
Will Deaconfba6e962020-01-10 13:20:03 +00001678 reg &= ~ARM_SMMU_sCR0_CLIENTPD;
Robin Murphy25a1c962016-02-10 14:25:33 +00001679 if (disable_bypass)
Will Deaconfba6e962020-01-10 13:20:03 +00001680 reg |= ARM_SMMU_sCR0_USFCFG;
Robin Murphy25a1c962016-02-10 14:25:33 +00001681 else
Will Deaconfba6e962020-01-10 13:20:03 +00001682 reg &= ~ARM_SMMU_sCR0_USFCFG;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001683
1684 /* Disable forced broadcasting */
Will Deaconfba6e962020-01-10 13:20:03 +00001685 reg &= ~ARM_SMMU_sCR0_FB;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001686
1687 /* Don't upgrade barriers */
Will Deaconfba6e962020-01-10 13:20:03 +00001688 reg &= ~(ARM_SMMU_sCR0_BSU);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001689
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -08001690 if (smmu->features & ARM_SMMU_FEAT_VMID16)
Will Deaconfba6e962020-01-10 13:20:03 +00001691 reg |= ARM_SMMU_sCR0_VMID16EN;
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -08001692
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +03001693 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
Will Deaconfba6e962020-01-10 13:20:03 +00001694 reg |= ARM_SMMU_sCR0_EXIDENABLE;
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +03001695
Robin Murphy62b993a2019-08-15 19:37:36 +01001696 if (smmu->impl && smmu->impl->reset)
1697 smmu->impl->reset(smmu);
1698
Will Deacon45ae7cf2013-06-24 18:31:25 +01001699 /* Push the button */
Robin Murphy11febfc2017-03-30 17:56:31 +01001700 arm_smmu_tlb_sync_global(smmu);
Robin Murphy00320ce2019-08-15 19:37:31 +01001701 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, reg);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001702}
1703
1704static int arm_smmu_id_size_to_bits(int size)
1705{
1706 switch (size) {
1707 case 0:
1708 return 32;
1709 case 1:
1710 return 36;
1711 case 2:
1712 return 40;
1713 case 3:
1714 return 42;
1715 case 4:
1716 return 44;
1717 case 5:
1718 default:
1719 return 48;
1720 }
1721}
1722
1723static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1724{
Robin Murphy490325e2019-08-15 19:37:26 +01001725 unsigned int size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001726 u32 id;
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00001727 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
Robin Murphy8e8b2032016-09-12 17:13:50 +01001728 int i;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001729
1730 dev_notice(smmu->dev, "probing hardware configuration...\n");
Robin Murphyb7862e32016-04-13 18:13:03 +01001731 dev_notice(smmu->dev, "SMMUv%d with:\n",
1732 smmu->version == ARM_SMMU_V2 ? 2 : 1);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001733
1734 /* ID0 */
Robin Murphy00320ce2019-08-15 19:37:31 +01001735 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID0);
Will Deacon4cf740b2014-07-14 19:47:39 +01001736
1737 /* Restrict available stages based on module parameter */
1738 if (force_stage == 1)
Will Deaconfba6e962020-01-10 13:20:03 +00001739 id &= ~(ARM_SMMU_ID0_S2TS | ARM_SMMU_ID0_NTS);
Will Deacon4cf740b2014-07-14 19:47:39 +01001740 else if (force_stage == 2)
Will Deaconfba6e962020-01-10 13:20:03 +00001741 id &= ~(ARM_SMMU_ID0_S1TS | ARM_SMMU_ID0_NTS);
Will Deacon4cf740b2014-07-14 19:47:39 +01001742
Will Deaconfba6e962020-01-10 13:20:03 +00001743 if (id & ARM_SMMU_ID0_S1TS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001744 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1745 dev_notice(smmu->dev, "\tstage 1 translation\n");
1746 }
1747
Will Deaconfba6e962020-01-10 13:20:03 +00001748 if (id & ARM_SMMU_ID0_S2TS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001749 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1750 dev_notice(smmu->dev, "\tstage 2 translation\n");
1751 }
1752
Will Deaconfba6e962020-01-10 13:20:03 +00001753 if (id & ARM_SMMU_ID0_NTS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001754 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1755 dev_notice(smmu->dev, "\tnested translation\n");
1756 }
1757
1758 if (!(smmu->features &
Will Deacon4cf740b2014-07-14 19:47:39 +01001759 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001760 dev_err(smmu->dev, "\tno translation support!\n");
1761 return -ENODEV;
1762 }
1763
Will Deaconfba6e962020-01-10 13:20:03 +00001764 if ((id & ARM_SMMU_ID0_S1TS) &&
1765 ((smmu->version < ARM_SMMU_V2) || !(id & ARM_SMMU_ID0_ATOSNS))) {
Mitchel Humpherys859a7322014-10-29 21:13:40 +00001766 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1767 dev_notice(smmu->dev, "\taddress translation ops\n");
1768 }
1769
Robin Murphybae2c2d2015-07-29 19:46:05 +01001770 /*
1771 * In order for DMA API calls to work properly, we must defer to what
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00001772 * the FW says about coherency, regardless of what the hardware claims.
Robin Murphybae2c2d2015-07-29 19:46:05 +01001773 * Fortunately, this also opens up a workaround for systems where the
1774 * ID register value has ended up configured incorrectly.
1775 */
Will Deaconfba6e962020-01-10 13:20:03 +00001776 cttw_reg = !!(id & ARM_SMMU_ID0_CTTW);
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00001777 if (cttw_fw || cttw_reg)
Robin Murphybae2c2d2015-07-29 19:46:05 +01001778 dev_notice(smmu->dev, "\t%scoherent table walk\n",
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00001779 cttw_fw ? "" : "non-");
1780 if (cttw_fw != cttw_reg)
Robin Murphybae2c2d2015-07-29 19:46:05 +01001781 dev_notice(smmu->dev,
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00001782 "\t(IDR0.CTTW overridden by FW configuration)\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01001783
Robin Murphy21174242016-09-12 17:13:48 +01001784 /* Max. number of entries we have for stream matching/indexing */
Will Deaconfba6e962020-01-10 13:20:03 +00001785 if (smmu->version == ARM_SMMU_V2 && id & ARM_SMMU_ID0_EXIDS) {
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +03001786 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1787 size = 1 << 16;
1788 } else {
Will Deaconfba6e962020-01-10 13:20:03 +00001789 size = 1 << FIELD_GET(ARM_SMMU_ID0_NUMSIDB, id);
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +03001790 }
Robin Murphy21174242016-09-12 17:13:48 +01001791 smmu->streamid_mask = size - 1;
Will Deaconfba6e962020-01-10 13:20:03 +00001792 if (id & ARM_SMMU_ID0_SMS) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001793 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
Will Deaconfba6e962020-01-10 13:20:03 +00001794 size = FIELD_GET(ARM_SMMU_ID0_NUMSMRG, id);
Robin Murphy21174242016-09-12 17:13:48 +01001795 if (size == 0) {
Will Deacon45ae7cf2013-06-24 18:31:25 +01001796 dev_err(smmu->dev,
1797 "stream-matching supported, but no SMRs present!\n");
1798 return -ENODEV;
1799 }
1800
Robin Murphy1f3d5ca2016-09-12 17:13:49 +01001801 /* Zero-initialised to mark as invalid */
1802 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1803 GFP_KERNEL);
1804 if (!smmu->smrs)
1805 return -ENOMEM;
1806
Will Deacon45ae7cf2013-06-24 18:31:25 +01001807 dev_notice(smmu->dev,
Robin Murphy490325e2019-08-15 19:37:26 +01001808 "\tstream matching with %u register groups", size);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001809 }
Robin Murphy8e8b2032016-09-12 17:13:50 +01001810 /* s2cr->type == 0 means translation, so initialise explicitly */
1811 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1812 GFP_KERNEL);
1813 if (!smmu->s2crs)
1814 return -ENOMEM;
1815 for (i = 0; i < size; i++)
1816 smmu->s2crs[i] = s2cr_init_val;
1817
Robin Murphy21174242016-09-12 17:13:48 +01001818 smmu->num_mapping_groups = size;
Robin Murphy588888a2016-09-12 17:13:54 +01001819 mutex_init(&smmu->stream_map_mutex);
Will Deacon8e517e72017-07-06 15:55:48 +01001820 spin_lock_init(&smmu->global_sync_lock);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001821
Will Deaconfba6e962020-01-10 13:20:03 +00001822 if (smmu->version < ARM_SMMU_V2 ||
1823 !(id & ARM_SMMU_ID0_PTFS_NO_AARCH32)) {
Robin Murphy7602b872016-04-28 17:12:09 +01001824 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
Will Deaconfba6e962020-01-10 13:20:03 +00001825 if (!(id & ARM_SMMU_ID0_PTFS_NO_AARCH32S))
Robin Murphy7602b872016-04-28 17:12:09 +01001826 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1827 }
1828
Will Deacon45ae7cf2013-06-24 18:31:25 +01001829 /* ID1 */
Robin Murphy00320ce2019-08-15 19:37:31 +01001830 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID1);
Will Deaconfba6e962020-01-10 13:20:03 +00001831 smmu->pgshift = (id & ARM_SMMU_ID1_PAGESIZE) ? 16 : 12;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001832
Andreas Herrmannc55af7f2013-10-01 13:39:06 +01001833 /* Check for size mismatch of SMMU address space from mapped region */
Will Deaconfba6e962020-01-10 13:20:03 +00001834 size = 1 << (FIELD_GET(ARM_SMMU_ID1_NUMPAGENDXB, id) + 1);
Robin Murphy490325e2019-08-15 19:37:26 +01001835 if (smmu->numpage != 2 * size << smmu->pgshift)
Mitchel Humpherys29073202014-07-08 09:52:18 -07001836 dev_warn(smmu->dev,
Robin Murphy490325e2019-08-15 19:37:26 +01001837 "SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1838 2 * size << smmu->pgshift, smmu->numpage);
1839 /* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1840 smmu->numpage = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001841
Will Deaconfba6e962020-01-10 13:20:03 +00001842 smmu->num_s2_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMS2CB, id);
1843 smmu->num_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMCB, id);
Will Deacon45ae7cf2013-06-24 18:31:25 +01001844 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1845 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1846 return -ENODEV;
1847 }
1848 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1849 smmu->num_context_banks, smmu->num_s2_context_banks);
Robin Murphy90df3732017-08-08 14:56:14 +01001850 smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1851 sizeof(*smmu->cbs), GFP_KERNEL);
1852 if (!smmu->cbs)
1853 return -ENOMEM;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001854
1855 /* ID2 */
Robin Murphy00320ce2019-08-15 19:37:31 +01001856 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID2);
Will Deaconfba6e962020-01-10 13:20:03 +00001857 size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_IAS, id));
Will Deacon518f7132014-11-14 17:17:54 +00001858 smmu->ipa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001859
Will Deacon518f7132014-11-14 17:17:54 +00001860 /* The output mask is also applied for bypass */
Will Deaconfba6e962020-01-10 13:20:03 +00001861 size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_OAS, id));
Will Deacon518f7132014-11-14 17:17:54 +00001862 smmu->pa_size = size;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001863
Will Deaconfba6e962020-01-10 13:20:03 +00001864 if (id & ARM_SMMU_ID2_VMID16)
Tirumalesh Chalamarla4e3e9b62016-02-23 10:19:00 -08001865 smmu->features |= ARM_SMMU_FEAT_VMID16;
1866
Robin Murphyf1d84542015-03-04 16:41:05 +00001867 /*
1868 * What the page table walker can address actually depends on which
1869 * descriptor format is in use, but since a) we don't know that yet,
1870 * and b) it can vary per context bank, this will have to do...
1871 */
1872 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1873 dev_warn(smmu->dev,
1874 "failed to set DMA mask for table walker\n");
1875
Robin Murphyb7862e32016-04-13 18:13:03 +01001876 if (smmu->version < ARM_SMMU_V2) {
Will Deacon518f7132014-11-14 17:17:54 +00001877 smmu->va_size = smmu->ipa_size;
Robin Murphyb7862e32016-04-13 18:13:03 +01001878 if (smmu->version == ARM_SMMU_V1_64K)
1879 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001880 } else {
Will Deaconfba6e962020-01-10 13:20:03 +00001881 size = FIELD_GET(ARM_SMMU_ID2_UBS, id);
Will Deacon518f7132014-11-14 17:17:54 +00001882 smmu->va_size = arm_smmu_id_size_to_bits(size);
Will Deaconfba6e962020-01-10 13:20:03 +00001883 if (id & ARM_SMMU_ID2_PTFS_4K)
Robin Murphy7602b872016-04-28 17:12:09 +01001884 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
Will Deaconfba6e962020-01-10 13:20:03 +00001885 if (id & ARM_SMMU_ID2_PTFS_16K)
Robin Murphy7602b872016-04-28 17:12:09 +01001886 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
Will Deaconfba6e962020-01-10 13:20:03 +00001887 if (id & ARM_SMMU_ID2_PTFS_64K)
Robin Murphy7602b872016-04-28 17:12:09 +01001888 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
Will Deacon45ae7cf2013-06-24 18:31:25 +01001889 }
1890
Robin Murphy7602b872016-04-28 17:12:09 +01001891 /* Now we've corralled the various formats, what'll it do? */
Robin Murphy7602b872016-04-28 17:12:09 +01001892 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
Robin Murphyd5466352016-05-09 17:20:09 +01001893 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
Robin Murphy7602b872016-04-28 17:12:09 +01001894 if (smmu->features &
1895 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
Robin Murphyd5466352016-05-09 17:20:09 +01001896 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
Robin Murphy7602b872016-04-28 17:12:09 +01001897 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
Robin Murphyd5466352016-05-09 17:20:09 +01001898 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
Robin Murphy7602b872016-04-28 17:12:09 +01001899 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
Robin Murphyd5466352016-05-09 17:20:09 +01001900 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
Robin Murphy7602b872016-04-28 17:12:09 +01001901
Robin Murphyd5466352016-05-09 17:20:09 +01001902 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1903 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1904 else
1905 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1906 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1907 smmu->pgsize_bitmap);
1908
Will Deacon518f7132014-11-14 17:17:54 +00001909
Will Deacon28d60072014-09-01 16:24:48 +01001910 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1911 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001912 smmu->va_size, smmu->ipa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001913
1914 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1915 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
Will Deacon518f7132014-11-14 17:17:54 +00001916 smmu->ipa_size, smmu->pa_size);
Will Deacon28d60072014-09-01 16:24:48 +01001917
Robin Murphy3995e182019-08-15 19:37:35 +01001918 if (smmu->impl && smmu->impl->cfg_probe)
1919 return smmu->impl->cfg_probe(smmu);
1920
Will Deacon45ae7cf2013-06-24 18:31:25 +01001921 return 0;
1922}
1923
Robin Murphy67b65a32016-04-13 18:12:57 +01001924struct arm_smmu_match_data {
1925 enum arm_smmu_arch_version version;
1926 enum arm_smmu_implementation model;
1927};
1928
1929#define ARM_SMMU_MATCH_DATA(name, ver, imp) \
Sricharan R96a299d2018-12-04 11:52:09 +05301930static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
Robin Murphy67b65a32016-04-13 18:12:57 +01001931
1932ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1933ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
Robin Murphyb7862e32016-04-13 18:13:03 +01001934ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
Robin Murphyf0cfffc2016-04-13 18:12:59 +01001935ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
Robin Murphye086d912016-04-13 18:12:58 +01001936ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
Vivek Gautam89cddc52018-12-04 11:52:13 +05301937ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
Robin Murphy67b65a32016-04-13 18:12:57 +01001938
Joerg Roedel09b52692014-10-02 12:24:45 +02001939static const struct of_device_id arm_smmu_of_match[] = {
Robin Murphy67b65a32016-04-13 18:12:57 +01001940 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1941 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1942 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
Robin Murphyb7862e32016-04-13 18:13:03 +01001943 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
Robin Murphyf0cfffc2016-04-13 18:12:59 +01001944 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
Robin Murphye086d912016-04-13 18:12:58 +01001945 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
Vivek Gautam89cddc52018-12-04 11:52:13 +05301946 { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
Robin Murphy09360402014-08-28 17:51:59 +01001947 { },
1948};
Will Deaconb06c0762019-12-19 12:03:45 +00001949MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
Robin Murphy09360402014-08-28 17:51:59 +01001950
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001951#ifdef CONFIG_ACPI
1952static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
1953{
1954 int ret = 0;
1955
1956 switch (model) {
1957 case ACPI_IORT_SMMU_V1:
1958 case ACPI_IORT_SMMU_CORELINK_MMU400:
1959 smmu->version = ARM_SMMU_V1;
1960 smmu->model = GENERIC_SMMU;
1961 break;
Robin Murphy84c24372017-06-19 16:41:56 +01001962 case ACPI_IORT_SMMU_CORELINK_MMU401:
1963 smmu->version = ARM_SMMU_V1_64K;
1964 smmu->model = GENERIC_SMMU;
1965 break;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001966 case ACPI_IORT_SMMU_V2:
1967 smmu->version = ARM_SMMU_V2;
1968 smmu->model = GENERIC_SMMU;
1969 break;
1970 case ACPI_IORT_SMMU_CORELINK_MMU500:
1971 smmu->version = ARM_SMMU_V2;
1972 smmu->model = ARM_MMU500;
1973 break;
Robin Murphy84c24372017-06-19 16:41:56 +01001974 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
1975 smmu->version = ARM_SMMU_V2;
1976 smmu->model = CAVIUM_SMMUV2;
1977 break;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001978 default:
1979 ret = -ENODEV;
1980 }
1981
1982 return ret;
1983}
1984
1985static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1986 struct arm_smmu_device *smmu)
1987{
1988 struct device *dev = smmu->dev;
1989 struct acpi_iort_node *node =
1990 *(struct acpi_iort_node **)dev_get_platdata(dev);
1991 struct acpi_iort_smmu *iort_smmu;
1992 int ret;
1993
1994 /* Retrieve SMMU1/2 specific data */
1995 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
1996
1997 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
1998 if (ret < 0)
1999 return ret;
2000
2001 /* Ignore the configuration access interrupt */
2002 smmu->num_global_irqs = 1;
2003
2004 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
2005 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2006
2007 return 0;
2008}
2009#else
2010static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2011 struct arm_smmu_device *smmu)
2012{
2013 return -ENODEV;
2014}
2015#endif
2016
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002017static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2018 struct arm_smmu_device *smmu)
Will Deacon45ae7cf2013-06-24 18:31:25 +01002019{
Robin Murphy67b65a32016-04-13 18:12:57 +01002020 const struct arm_smmu_match_data *data;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002021 struct device *dev = &pdev->dev;
Robin Murphy021bb842016-09-14 15:26:46 +01002022 bool legacy_binding;
2023
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002024 if (of_property_read_u32(dev->of_node, "#global-interrupts",
2025 &smmu->num_global_irqs)) {
2026 dev_err(dev, "missing #global-interrupts property\n");
2027 return -ENODEV;
2028 }
2029
2030 data = of_device_get_match_data(dev);
2031 smmu->version = data->version;
2032 smmu->model = data->model;
2033
Robin Murphy021bb842016-09-14 15:26:46 +01002034 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
2035 if (legacy_binding && !using_generic_binding) {
Will Deaconcd221bd2019-12-19 12:03:51 +00002036 if (!using_legacy_binding) {
2037 pr_notice("deprecated \"mmu-masters\" DT property in use; %s support unavailable\n",
2038 IS_ENABLED(CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS) ? "DMA API" : "SMMU");
2039 }
Robin Murphy021bb842016-09-14 15:26:46 +01002040 using_legacy_binding = true;
2041 } else if (!legacy_binding && !using_legacy_binding) {
2042 using_generic_binding = true;
2043 } else {
2044 dev_err(dev, "not probing due to mismatched DT properties\n");
2045 return -ENODEV;
2046 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01002047
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002048 if (of_dma_is_coherent(dev->of_node))
2049 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2050
2051 return 0;
2052}
2053
Will Deacon73595722019-12-19 12:03:50 +00002054static int arm_smmu_bus_init(struct iommu_ops *ops)
Robin Murphyf6810c12017-04-10 16:51:05 +05302055{
Will Deacon73595722019-12-19 12:03:50 +00002056 int err;
2057
Robin Murphyf6810c12017-04-10 16:51:05 +05302058 /* Oh, for a proper bus abstraction */
Will Deacon73595722019-12-19 12:03:50 +00002059 if (!iommu_present(&platform_bus_type)) {
2060 err = bus_set_iommu(&platform_bus_type, ops);
2061 if (err)
2062 return err;
2063 }
Robin Murphyf6810c12017-04-10 16:51:05 +05302064#ifdef CONFIG_ARM_AMBA
Will Deacon73595722019-12-19 12:03:50 +00002065 if (!iommu_present(&amba_bustype)) {
2066 err = bus_set_iommu(&amba_bustype, ops);
2067 if (err)
2068 goto err_reset_platform_ops;
2069 }
Robin Murphyf6810c12017-04-10 16:51:05 +05302070#endif
2071#ifdef CONFIG_PCI
2072 if (!iommu_present(&pci_bus_type)) {
Will Deacon73595722019-12-19 12:03:50 +00002073 err = bus_set_iommu(&pci_bus_type, ops);
2074 if (err)
2075 goto err_reset_amba_ops;
Robin Murphyf6810c12017-04-10 16:51:05 +05302076 }
2077#endif
Nipun Guptaeab03e22018-09-10 19:19:18 +05302078#ifdef CONFIG_FSL_MC_BUS
Will Deacon73595722019-12-19 12:03:50 +00002079 if (!iommu_present(&fsl_mc_bus_type)) {
2080 err = bus_set_iommu(&fsl_mc_bus_type, ops);
2081 if (err)
2082 goto err_reset_pci_ops;
2083 }
Nipun Guptaeab03e22018-09-10 19:19:18 +05302084#endif
Will Deacon73595722019-12-19 12:03:50 +00002085 return 0;
2086
2087err_reset_pci_ops: __maybe_unused;
2088#ifdef CONFIG_PCI
2089 bus_set_iommu(&pci_bus_type, NULL);
2090#endif
2091err_reset_amba_ops: __maybe_unused;
2092#ifdef CONFIG_ARM_AMBA
2093 bus_set_iommu(&amba_bustype, NULL);
2094#endif
2095err_reset_platform_ops: __maybe_unused;
2096 bus_set_iommu(&platform_bus_type, NULL);
2097 return err;
Robin Murphyf6810c12017-04-10 16:51:05 +05302098}
2099
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002100static int arm_smmu_device_probe(struct platform_device *pdev)
2101{
2102 struct resource *res;
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002103 resource_size_t ioaddr;
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002104 struct arm_smmu_device *smmu;
2105 struct device *dev = &pdev->dev;
2106 int num_irqs, i, err;
2107
Will Deacon45ae7cf2013-06-24 18:31:25 +01002108 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2109 if (!smmu) {
2110 dev_err(dev, "failed to allocate arm_smmu_device\n");
2111 return -ENOMEM;
2112 }
2113 smmu->dev = dev;
2114
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00002115 if (dev->of_node)
2116 err = arm_smmu_device_dt_probe(pdev, smmu);
2117 else
2118 err = arm_smmu_device_acpi_probe(pdev, smmu);
2119
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002120 if (err)
2121 return err;
Robin Murphy09360402014-08-28 17:51:59 +01002122
Robin Murphyfc058d32019-08-15 19:37:33 +01002123 smmu = arm_smmu_impl_init(smmu);
2124 if (IS_ERR(smmu))
2125 return PTR_ERR(smmu);
2126
Will Deacon45ae7cf2013-06-24 18:31:25 +01002127 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002128 ioaddr = res->start;
Julia Lawall8a7f4312013-08-19 12:20:37 +01002129 smmu->base = devm_ioremap_resource(dev, res);
2130 if (IS_ERR(smmu->base))
2131 return PTR_ERR(smmu->base);
Robin Murphy490325e2019-08-15 19:37:26 +01002132 /*
2133 * The resource size should effectively match the value of SMMU_TOP;
2134 * stash that temporarily until we know PAGESIZE to validate it with.
2135 */
2136 smmu->numpage = resource_size(res);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002137
Will Deacon45ae7cf2013-06-24 18:31:25 +01002138 num_irqs = 0;
2139 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2140 num_irqs++;
2141 if (num_irqs > smmu->num_global_irqs)
2142 smmu->num_context_irqs++;
2143 }
2144
Andreas Herrmann44a08de2013-10-01 13:39:07 +01002145 if (!smmu->num_context_irqs) {
2146 dev_err(dev, "found %d interrupts but expected at least %d\n",
2147 num_irqs, smmu->num_global_irqs + 1);
2148 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002149 }
Will Deacon45ae7cf2013-06-24 18:31:25 +01002150
Kees Cooka86854d2018-06-12 14:07:58 -07002151 smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
Will Deacon45ae7cf2013-06-24 18:31:25 +01002152 GFP_KERNEL);
2153 if (!smmu->irqs) {
2154 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2155 return -ENOMEM;
2156 }
2157
2158 for (i = 0; i < num_irqs; ++i) {
2159 int irq = platform_get_irq(pdev, i);
Mitchel Humpherys29073202014-07-08 09:52:18 -07002160
Jean-Philippe Brucker34d1b082019-11-11 12:17:21 +01002161 if (irq < 0)
Will Deacon45ae7cf2013-06-24 18:31:25 +01002162 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002163 smmu->irqs[i] = irq;
2164 }
2165
Sricharan R96a299d2018-12-04 11:52:09 +05302166 err = devm_clk_bulk_get_all(dev, &smmu->clks);
2167 if (err < 0) {
2168 dev_err(dev, "failed to get clocks %d\n", err);
2169 return err;
2170 }
2171 smmu->num_clks = err;
2172
2173 err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2174 if (err)
2175 return err;
2176
Olav Haugan3c8766d2014-08-22 17:12:32 -07002177 err = arm_smmu_device_cfg_probe(smmu);
2178 if (err)
2179 return err;
2180
Vivek Gautamd1e20222018-07-19 23:23:56 +05302181 if (smmu->version == ARM_SMMU_V2) {
2182 if (smmu->num_context_banks > smmu->num_context_irqs) {
2183 dev_err(dev,
2184 "found only %d context irq(s) but %d required\n",
2185 smmu->num_context_irqs, smmu->num_context_banks);
2186 return -ENODEV;
2187 }
2188
2189 /* Ignore superfluous interrupts */
2190 smmu->num_context_irqs = smmu->num_context_banks;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002191 }
2192
Will Deacon45ae7cf2013-06-24 18:31:25 +01002193 for (i = 0; i < smmu->num_global_irqs; ++i) {
Peng Fanbee14002016-07-04 17:38:22 +08002194 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2195 arm_smmu_global_fault,
2196 IRQF_SHARED,
2197 "arm-smmu global fault",
2198 smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002199 if (err) {
2200 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2201 i, smmu->irqs[i]);
Robin Murphyf80cd882016-09-14 15:21:39 +01002202 return err;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002203 }
2204 }
2205
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002206 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2207 "smmu.%pa", &ioaddr);
2208 if (err) {
2209 dev_err(dev, "Failed to register iommu in sysfs\n");
2210 return err;
2211 }
2212
2213 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2214 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2215
2216 err = iommu_device_register(&smmu->iommu);
2217 if (err) {
2218 dev_err(dev, "Failed to register iommu\n");
2219 return err;
2220 }
2221
Robin Murphyd6fc5d92016-09-12 17:13:52 +01002222 platform_set_drvdata(pdev, smmu);
Will Deaconfd90cec2013-08-21 13:56:34 +01002223 arm_smmu_device_reset(smmu);
Aleksey Makarovdc0eaa42017-01-19 17:36:36 +03002224 arm_smmu_test_smr_masks(smmu);
Robin Murphy021bb842016-09-14 15:26:46 +01002225
Robin Murphyf6810c12017-04-10 16:51:05 +05302226 /*
Sricharan Rd4a44f02018-12-04 11:52:10 +05302227 * We want to avoid touching dev->power.lock in fastpaths unless
2228 * it's really going to do something useful - pm_runtime_enabled()
2229 * can serve as an ideal proxy for that decision. So, conditionally
2230 * enable pm_runtime.
2231 */
2232 if (dev->pm_domain) {
2233 pm_runtime_set_active(dev);
2234 pm_runtime_enable(dev);
2235 }
2236
2237 /*
Robin Murphyf6810c12017-04-10 16:51:05 +05302238 * For ACPI and generic DT bindings, an SMMU will be probed before
2239 * any device which might need it, so we want the bus ops in place
2240 * ready to handle default domain setup as soon as any SMMU exists.
2241 */
2242 if (!using_legacy_binding)
Will Deacon73595722019-12-19 12:03:50 +00002243 return arm_smmu_bus_init(&arm_smmu_ops);
Robin Murphyf6810c12017-04-10 16:51:05 +05302244
Will Deacon45ae7cf2013-06-24 18:31:25 +01002245 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002246}
2247
Will Deaconb06c0762019-12-19 12:03:45 +00002248static int arm_smmu_device_remove(struct platform_device *pdev)
Will Deacon45ae7cf2013-06-24 18:31:25 +01002249{
Robin Murphyd6fc5d92016-09-12 17:13:52 +01002250 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002251
2252 if (!smmu)
Will Deaconb06c0762019-12-19 12:03:45 +00002253 return -ENODEV;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002254
Will Deaconecfadb62013-07-31 19:21:28 +01002255 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
Robin Murphyd6fc5d92016-09-12 17:13:52 +01002256 dev_err(&pdev->dev, "removing device with active domains!\n");
Will Deacon45ae7cf2013-06-24 18:31:25 +01002257
Will Deacon73595722019-12-19 12:03:50 +00002258 arm_smmu_bus_init(NULL);
2259 iommu_device_unregister(&smmu->iommu);
2260 iommu_device_sysfs_remove(&smmu->iommu);
2261
Sricharan Rd4a44f02018-12-04 11:52:10 +05302262 arm_smmu_rpm_get(smmu);
Will Deacon45ae7cf2013-06-24 18:31:25 +01002263 /* Turn the thing off */
Will Deaconfba6e962020-01-10 13:20:03 +00002264 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, ARM_SMMU_sCR0_CLIENTPD);
Sricharan Rd4a44f02018-12-04 11:52:10 +05302265 arm_smmu_rpm_put(smmu);
Sricharan R96a299d2018-12-04 11:52:09 +05302266
Sricharan Rd4a44f02018-12-04 11:52:10 +05302267 if (pm_runtime_enabled(smmu->dev))
2268 pm_runtime_force_suspend(smmu->dev);
2269 else
2270 clk_bulk_disable(smmu->num_clks, smmu->clks);
2271
2272 clk_bulk_unprepare(smmu->num_clks, smmu->clks);
Will Deaconb06c0762019-12-19 12:03:45 +00002273 return 0;
2274}
2275
2276static void arm_smmu_device_shutdown(struct platform_device *pdev)
2277{
2278 arm_smmu_device_remove(pdev);
Nate Watterson7aa86192017-06-29 18:18:15 -04002279}
2280
Sricharan R96a299d2018-12-04 11:52:09 +05302281static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
Robin Murphya2d866f2017-08-08 14:56:15 +01002282{
2283 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
Sricharan R96a299d2018-12-04 11:52:09 +05302284 int ret;
2285
2286 ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2287 if (ret)
2288 return ret;
Robin Murphya2d866f2017-08-08 14:56:15 +01002289
2290 arm_smmu_device_reset(smmu);
Sricharan R96a299d2018-12-04 11:52:09 +05302291
Will Deacon45ae7cf2013-06-24 18:31:25 +01002292 return 0;
2293}
2294
Sricharan R96a299d2018-12-04 11:52:09 +05302295static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
Dan Carpenter6614ee72013-08-21 09:34:20 +01002296{
Sricharan R96a299d2018-12-04 11:52:09 +05302297 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2298
2299 clk_bulk_disable(smmu->num_clks, smmu->clks);
2300
2301 return 0;
Will Deacon45ae7cf2013-06-24 18:31:25 +01002302}
2303
Robin Murphya2d866f2017-08-08 14:56:15 +01002304static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2305{
Sricharan R96a299d2018-12-04 11:52:09 +05302306 if (pm_runtime_suspended(dev))
2307 return 0;
Robin Murphya2d866f2017-08-08 14:56:15 +01002308
Sricharan R96a299d2018-12-04 11:52:09 +05302309 return arm_smmu_runtime_resume(dev);
Robin Murphya2d866f2017-08-08 14:56:15 +01002310}
2311
Sricharan R96a299d2018-12-04 11:52:09 +05302312static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2313{
2314 if (pm_runtime_suspended(dev))
2315 return 0;
2316
2317 return arm_smmu_runtime_suspend(dev);
2318}
2319
2320static const struct dev_pm_ops arm_smmu_pm_ops = {
2321 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2322 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2323 arm_smmu_runtime_resume, NULL)
2324};
Robin Murphya2d866f2017-08-08 14:56:15 +01002325
Will Deacon45ae7cf2013-06-24 18:31:25 +01002326static struct platform_driver arm_smmu_driver = {
2327 .driver = {
Paul Gortmakeraddb672f2018-12-01 14:19:16 -05002328 .name = "arm-smmu",
Masahiro Yamadacd037ff2019-12-24 17:15:00 +09002329 .of_match_table = arm_smmu_of_match,
Paul Gortmakeraddb672f2018-12-01 14:19:16 -05002330 .pm = &arm_smmu_pm_ops,
Will Deacon34debdc2019-12-19 12:03:46 +00002331 .suppress_bind_attrs = true,
Will Deacon45ae7cf2013-06-24 18:31:25 +01002332 },
Lorenzo Pieralisibbb8a182016-11-21 10:01:44 +00002333 .probe = arm_smmu_device_probe,
Will Deaconb06c0762019-12-19 12:03:45 +00002334 .remove = arm_smmu_device_remove,
Nate Watterson7aa86192017-06-29 18:18:15 -04002335 .shutdown = arm_smmu_device_shutdown,
Will Deacon45ae7cf2013-06-24 18:31:25 +01002336};
Will Deaconb06c0762019-12-19 12:03:45 +00002337module_platform_driver(arm_smmu_driver);
2338
2339MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
Will Deacon1ea27ee2019-12-19 12:03:52 +00002340MODULE_AUTHOR("Will Deacon <will@kernel.org>");
Ard Biesheuveld3daf662019-12-19 12:03:48 +00002341MODULE_ALIAS("platform:arm-smmu");
Will Deaconb06c0762019-12-19 12:03:45 +00002342MODULE_LICENSE("GPL v2");