xref: /openbmc/linux/drivers/iommu/arm/arm-smmu/qcom_iommu.c (revision 9a630a4b41a2639b65d024a5d2d88ed3ecca130a)
1e86d1aa8SWill Deacon // SPDX-License-Identifier: GPL-2.0-only
2e86d1aa8SWill Deacon /*
3e86d1aa8SWill Deacon  * IOMMU API for QCOM secure IOMMUs.  Somewhat based on arm-smmu.c
4e86d1aa8SWill Deacon  *
5e86d1aa8SWill Deacon  * Copyright (C) 2013 ARM Limited
6e86d1aa8SWill Deacon  * Copyright (C) 2017 Red Hat
7e86d1aa8SWill Deacon  */
8e86d1aa8SWill Deacon 
9e86d1aa8SWill Deacon #include <linux/atomic.h>
10e86d1aa8SWill Deacon #include <linux/bitfield.h>
11e86d1aa8SWill Deacon #include <linux/clk.h>
12e86d1aa8SWill Deacon #include <linux/delay.h>
13e86d1aa8SWill Deacon #include <linux/dma-mapping.h>
14e86d1aa8SWill Deacon #include <linux/err.h>
15e86d1aa8SWill Deacon #include <linux/interrupt.h>
16e86d1aa8SWill Deacon #include <linux/io.h>
17e86d1aa8SWill Deacon #include <linux/io-64-nonatomic-hi-lo.h>
18e86d1aa8SWill Deacon #include <linux/io-pgtable.h>
19e86d1aa8SWill Deacon #include <linux/iommu.h>
20e86d1aa8SWill Deacon #include <linux/iopoll.h>
21e86d1aa8SWill Deacon #include <linux/kconfig.h>
22e86d1aa8SWill Deacon #include <linux/init.h>
23e86d1aa8SWill Deacon #include <linux/mutex.h>
24e86d1aa8SWill Deacon #include <linux/of.h>
25e86d1aa8SWill Deacon #include <linux/of_address.h>
26e86d1aa8SWill Deacon #include <linux/of_device.h>
27e86d1aa8SWill Deacon #include <linux/platform_device.h>
28e86d1aa8SWill Deacon #include <linux/pm.h>
29e86d1aa8SWill Deacon #include <linux/pm_runtime.h>
30e86d1aa8SWill Deacon #include <linux/qcom_scm.h>
31e86d1aa8SWill Deacon #include <linux/slab.h>
32e86d1aa8SWill Deacon #include <linux/spinlock.h>
33e86d1aa8SWill Deacon 
34e86d1aa8SWill Deacon #include "arm-smmu.h"
35e86d1aa8SWill Deacon 
36e86d1aa8SWill Deacon #define SMMU_INTR_SEL_NS     0x2000
37e86d1aa8SWill Deacon 
38e46b3c0dSJoerg Roedel enum qcom_iommu_clk {
39e46b3c0dSJoerg Roedel 	CLK_IFACE,
40e46b3c0dSJoerg Roedel 	CLK_BUS,
41e46b3c0dSJoerg Roedel 	CLK_TBU,
42e46b3c0dSJoerg Roedel 	CLK_NUM,
43e46b3c0dSJoerg Roedel };
44e46b3c0dSJoerg Roedel 
45e86d1aa8SWill Deacon struct qcom_iommu_ctx;
46e86d1aa8SWill Deacon 
47e86d1aa8SWill Deacon struct qcom_iommu_dev {
48e86d1aa8SWill Deacon 	/* IOMMU core code handle */
49e86d1aa8SWill Deacon 	struct iommu_device	 iommu;
50e86d1aa8SWill Deacon 	struct device		*dev;
51e46b3c0dSJoerg Roedel 	struct clk_bulk_data clks[CLK_NUM];
52e86d1aa8SWill Deacon 	void __iomem		*local_base;
53e86d1aa8SWill Deacon 	u32			 sec_id;
54e86d1aa8SWill Deacon 	u8			 num_ctxs;
55e86d1aa8SWill Deacon 	struct qcom_iommu_ctx	*ctxs[];   /* indexed by asid-1 */
56e86d1aa8SWill Deacon };
57e86d1aa8SWill Deacon 
58e86d1aa8SWill Deacon struct qcom_iommu_ctx {
59e86d1aa8SWill Deacon 	struct device		*dev;
60e86d1aa8SWill Deacon 	void __iomem		*base;
61e86d1aa8SWill Deacon 	bool			 secure_init;
62e86d1aa8SWill Deacon 	u8			 asid;      /* asid and ctx bank # are 1:1 */
63e86d1aa8SWill Deacon 	struct iommu_domain	*domain;
64e86d1aa8SWill Deacon };
65e86d1aa8SWill Deacon 
66e86d1aa8SWill Deacon struct qcom_iommu_domain {
67e86d1aa8SWill Deacon 	struct io_pgtable_ops	*pgtbl_ops;
68e86d1aa8SWill Deacon 	spinlock_t		 pgtbl_lock;
69e86d1aa8SWill Deacon 	struct mutex		 init_mutex; /* Protects iommu pointer */
70e86d1aa8SWill Deacon 	struct iommu_domain	 domain;
71e86d1aa8SWill Deacon 	struct qcom_iommu_dev	*iommu;
72e46b3c0dSJoerg Roedel 	struct iommu_fwspec	*fwspec;
73e86d1aa8SWill Deacon };
74e86d1aa8SWill Deacon 
75e86d1aa8SWill Deacon static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
76e86d1aa8SWill Deacon {
77e86d1aa8SWill Deacon 	return container_of(dom, struct qcom_iommu_domain, domain);
78e86d1aa8SWill Deacon }
79e86d1aa8SWill Deacon 
80e86d1aa8SWill Deacon static const struct iommu_ops qcom_iommu_ops;
81e86d1aa8SWill Deacon 
82e86d1aa8SWill Deacon static struct qcom_iommu_dev * to_iommu(struct device *dev)
83e86d1aa8SWill Deacon {
84e86d1aa8SWill Deacon 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
85e86d1aa8SWill Deacon 
86e86d1aa8SWill Deacon 	if (!fwspec || fwspec->ops != &qcom_iommu_ops)
87e86d1aa8SWill Deacon 		return NULL;
88e86d1aa8SWill Deacon 
89e86d1aa8SWill Deacon 	return dev_iommu_priv_get(dev);
90e86d1aa8SWill Deacon }
91e86d1aa8SWill Deacon 
92e46b3c0dSJoerg Roedel static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid)
93e86d1aa8SWill Deacon {
94e46b3c0dSJoerg Roedel 	struct qcom_iommu_dev *qcom_iommu = d->iommu;
95e86d1aa8SWill Deacon 	if (!qcom_iommu)
96e86d1aa8SWill Deacon 		return NULL;
97e86d1aa8SWill Deacon 	return qcom_iommu->ctxs[asid - 1];
98e86d1aa8SWill Deacon }
99e86d1aa8SWill Deacon 
100e86d1aa8SWill Deacon static inline void
101e86d1aa8SWill Deacon iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
102e86d1aa8SWill Deacon {
103e86d1aa8SWill Deacon 	writel_relaxed(val, ctx->base + reg);
104e86d1aa8SWill Deacon }
105e86d1aa8SWill Deacon 
106e86d1aa8SWill Deacon static inline void
107e86d1aa8SWill Deacon iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
108e86d1aa8SWill Deacon {
109e86d1aa8SWill Deacon 	writeq_relaxed(val, ctx->base + reg);
110e86d1aa8SWill Deacon }
111e86d1aa8SWill Deacon 
112e86d1aa8SWill Deacon static inline u32
113e86d1aa8SWill Deacon iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
114e86d1aa8SWill Deacon {
115e86d1aa8SWill Deacon 	return readl_relaxed(ctx->base + reg);
116e86d1aa8SWill Deacon }
117e86d1aa8SWill Deacon 
118e86d1aa8SWill Deacon static inline u64
119e86d1aa8SWill Deacon iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
120e86d1aa8SWill Deacon {
121e86d1aa8SWill Deacon 	return readq_relaxed(ctx->base + reg);
122e86d1aa8SWill Deacon }
123e86d1aa8SWill Deacon 
124e86d1aa8SWill Deacon static void qcom_iommu_tlb_sync(void *cookie)
125e86d1aa8SWill Deacon {
126e46b3c0dSJoerg Roedel 	struct qcom_iommu_domain *qcom_domain = cookie;
127e46b3c0dSJoerg Roedel 	struct iommu_fwspec *fwspec = qcom_domain->fwspec;
128e86d1aa8SWill Deacon 	unsigned i;
129e86d1aa8SWill Deacon 
130e86d1aa8SWill Deacon 	for (i = 0; i < fwspec->num_ids; i++) {
131e46b3c0dSJoerg Roedel 		struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
132e86d1aa8SWill Deacon 		unsigned int val, ret;
133e86d1aa8SWill Deacon 
134e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
135e86d1aa8SWill Deacon 
136e86d1aa8SWill Deacon 		ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
137e86d1aa8SWill Deacon 					 (val & 0x1) == 0, 0, 5000000);
138e86d1aa8SWill Deacon 		if (ret)
139e86d1aa8SWill Deacon 			dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
140e86d1aa8SWill Deacon 	}
141e86d1aa8SWill Deacon }
142e86d1aa8SWill Deacon 
143e86d1aa8SWill Deacon static void qcom_iommu_tlb_inv_context(void *cookie)
144e86d1aa8SWill Deacon {
145e46b3c0dSJoerg Roedel 	struct qcom_iommu_domain *qcom_domain = cookie;
146e46b3c0dSJoerg Roedel 	struct iommu_fwspec *fwspec = qcom_domain->fwspec;
147e86d1aa8SWill Deacon 	unsigned i;
148e86d1aa8SWill Deacon 
149e86d1aa8SWill Deacon 	for (i = 0; i < fwspec->num_ids; i++) {
150e46b3c0dSJoerg Roedel 		struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
151e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
152e86d1aa8SWill Deacon 	}
153e86d1aa8SWill Deacon 
154e86d1aa8SWill Deacon 	qcom_iommu_tlb_sync(cookie);
155e86d1aa8SWill Deacon }
156e86d1aa8SWill Deacon 
157e86d1aa8SWill Deacon static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
158e86d1aa8SWill Deacon 					    size_t granule, bool leaf, void *cookie)
159e86d1aa8SWill Deacon {
160e46b3c0dSJoerg Roedel 	struct qcom_iommu_domain *qcom_domain = cookie;
161e46b3c0dSJoerg Roedel 	struct iommu_fwspec *fwspec = qcom_domain->fwspec;
162e86d1aa8SWill Deacon 	unsigned i, reg;
163e86d1aa8SWill Deacon 
164e86d1aa8SWill Deacon 	reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
165e86d1aa8SWill Deacon 
166e86d1aa8SWill Deacon 	for (i = 0; i < fwspec->num_ids; i++) {
167e46b3c0dSJoerg Roedel 		struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
168e86d1aa8SWill Deacon 		size_t s = size;
169e86d1aa8SWill Deacon 
170e86d1aa8SWill Deacon 		iova = (iova >> 12) << 12;
171e86d1aa8SWill Deacon 		iova |= ctx->asid;
172e86d1aa8SWill Deacon 		do {
173e86d1aa8SWill Deacon 			iommu_writel(ctx, reg, iova);
174e86d1aa8SWill Deacon 			iova += granule;
175e86d1aa8SWill Deacon 		} while (s -= granule);
176e86d1aa8SWill Deacon 	}
177e86d1aa8SWill Deacon }
178e86d1aa8SWill Deacon 
179e86d1aa8SWill Deacon static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
180e86d1aa8SWill Deacon 				      size_t granule, void *cookie)
181e86d1aa8SWill Deacon {
182e86d1aa8SWill Deacon 	qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
183e86d1aa8SWill Deacon 	qcom_iommu_tlb_sync(cookie);
184e86d1aa8SWill Deacon }
185e86d1aa8SWill Deacon 
186e86d1aa8SWill Deacon static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
187e86d1aa8SWill Deacon 				    unsigned long iova, size_t granule,
188e86d1aa8SWill Deacon 				    void *cookie)
189e86d1aa8SWill Deacon {
190e86d1aa8SWill Deacon 	qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
191e86d1aa8SWill Deacon }
192e86d1aa8SWill Deacon 
193e86d1aa8SWill Deacon static const struct iommu_flush_ops qcom_flush_ops = {
194e86d1aa8SWill Deacon 	.tlb_flush_all	= qcom_iommu_tlb_inv_context,
195e86d1aa8SWill Deacon 	.tlb_flush_walk = qcom_iommu_tlb_flush_walk,
196e86d1aa8SWill Deacon 	.tlb_add_page	= qcom_iommu_tlb_add_page,
197e86d1aa8SWill Deacon };
198e86d1aa8SWill Deacon 
199e86d1aa8SWill Deacon static irqreturn_t qcom_iommu_fault(int irq, void *dev)
200e86d1aa8SWill Deacon {
201e86d1aa8SWill Deacon 	struct qcom_iommu_ctx *ctx = dev;
202e86d1aa8SWill Deacon 	u32 fsr, fsynr;
203e86d1aa8SWill Deacon 	u64 iova;
204e86d1aa8SWill Deacon 
205e86d1aa8SWill Deacon 	fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
206e86d1aa8SWill Deacon 
207e86d1aa8SWill Deacon 	if (!(fsr & ARM_SMMU_FSR_FAULT))
208e86d1aa8SWill Deacon 		return IRQ_NONE;
209e86d1aa8SWill Deacon 
210e86d1aa8SWill Deacon 	fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
211e86d1aa8SWill Deacon 	iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
212e86d1aa8SWill Deacon 
213e86d1aa8SWill Deacon 	if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
214e86d1aa8SWill Deacon 		dev_err_ratelimited(ctx->dev,
215e86d1aa8SWill Deacon 				    "Unhandled context fault: fsr=0x%x, "
216e86d1aa8SWill Deacon 				    "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
217e86d1aa8SWill Deacon 				    fsr, iova, fsynr, ctx->asid);
218e86d1aa8SWill Deacon 	}
219e86d1aa8SWill Deacon 
220e86d1aa8SWill Deacon 	iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
221e86d1aa8SWill Deacon 	iommu_writel(ctx, ARM_SMMU_CB_RESUME, ARM_SMMU_RESUME_TERMINATE);
222e86d1aa8SWill Deacon 
223e86d1aa8SWill Deacon 	return IRQ_HANDLED;
224e86d1aa8SWill Deacon }
225e86d1aa8SWill Deacon 
226e86d1aa8SWill Deacon static int qcom_iommu_init_domain(struct iommu_domain *domain,
227e86d1aa8SWill Deacon 				  struct qcom_iommu_dev *qcom_iommu,
228e86d1aa8SWill Deacon 				  struct device *dev)
229e86d1aa8SWill Deacon {
230e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
231e86d1aa8SWill Deacon 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
232e86d1aa8SWill Deacon 	struct io_pgtable_ops *pgtbl_ops;
233e86d1aa8SWill Deacon 	struct io_pgtable_cfg pgtbl_cfg;
234e86d1aa8SWill Deacon 	int i, ret = 0;
235e86d1aa8SWill Deacon 	u32 reg;
236e86d1aa8SWill Deacon 
237e86d1aa8SWill Deacon 	mutex_lock(&qcom_domain->init_mutex);
238e86d1aa8SWill Deacon 	if (qcom_domain->iommu)
239e86d1aa8SWill Deacon 		goto out_unlock;
240e86d1aa8SWill Deacon 
241e86d1aa8SWill Deacon 	pgtbl_cfg = (struct io_pgtable_cfg) {
242e86d1aa8SWill Deacon 		.pgsize_bitmap	= qcom_iommu_ops.pgsize_bitmap,
243e86d1aa8SWill Deacon 		.ias		= 32,
244e86d1aa8SWill Deacon 		.oas		= 40,
245e86d1aa8SWill Deacon 		.tlb		= &qcom_flush_ops,
246e86d1aa8SWill Deacon 		.iommu_dev	= qcom_iommu->dev,
247e86d1aa8SWill Deacon 	};
248e86d1aa8SWill Deacon 
249e86d1aa8SWill Deacon 	qcom_domain->iommu = qcom_iommu;
250e46b3c0dSJoerg Roedel 	qcom_domain->fwspec = fwspec;
251e46b3c0dSJoerg Roedel 
252e46b3c0dSJoerg Roedel 	pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, qcom_domain);
253e86d1aa8SWill Deacon 	if (!pgtbl_ops) {
254e86d1aa8SWill Deacon 		dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
255e86d1aa8SWill Deacon 		ret = -ENOMEM;
256e86d1aa8SWill Deacon 		goto out_clear_iommu;
257e86d1aa8SWill Deacon 	}
258e86d1aa8SWill Deacon 
259e86d1aa8SWill Deacon 	/* Update the domain's page sizes to reflect the page table format */
260e86d1aa8SWill Deacon 	domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
261e86d1aa8SWill Deacon 	domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
262e86d1aa8SWill Deacon 	domain->geometry.force_aperture = true;
263e86d1aa8SWill Deacon 
264e86d1aa8SWill Deacon 	for (i = 0; i < fwspec->num_ids; i++) {
265e46b3c0dSJoerg Roedel 		struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
266e86d1aa8SWill Deacon 
267e86d1aa8SWill Deacon 		if (!ctx->secure_init) {
268e86d1aa8SWill Deacon 			ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
269e86d1aa8SWill Deacon 			if (ret) {
270e86d1aa8SWill Deacon 				dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
271e86d1aa8SWill Deacon 				goto out_clear_iommu;
272e86d1aa8SWill Deacon 			}
273e86d1aa8SWill Deacon 			ctx->secure_init = true;
274e86d1aa8SWill Deacon 		}
275e86d1aa8SWill Deacon 
276e86d1aa8SWill Deacon 		/* TTBRs */
277e86d1aa8SWill Deacon 		iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
278e86d1aa8SWill Deacon 				pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
279e86d1aa8SWill Deacon 				FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
280e86d1aa8SWill Deacon 		iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
281e86d1aa8SWill Deacon 
282e86d1aa8SWill Deacon 		/* TCR */
283e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_TCR2,
284e86d1aa8SWill Deacon 				arm_smmu_lpae_tcr2(&pgtbl_cfg));
285e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_TCR,
286e86d1aa8SWill Deacon 			     arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
287e86d1aa8SWill Deacon 
288e86d1aa8SWill Deacon 		/* MAIRs (stage-1 only) */
289e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
290e86d1aa8SWill Deacon 				pgtbl_cfg.arm_lpae_s1_cfg.mair);
291e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
292e86d1aa8SWill Deacon 				pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
293e86d1aa8SWill Deacon 
294e86d1aa8SWill Deacon 		/* SCTLR */
295e86d1aa8SWill Deacon 		reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
296e86d1aa8SWill Deacon 		      ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
297e86d1aa8SWill Deacon 		      ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
298e86d1aa8SWill Deacon 		      ARM_SMMU_SCTLR_CFCFG;
299e86d1aa8SWill Deacon 
300e46b3c0dSJoerg Roedel 		if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
301e86d1aa8SWill Deacon 			reg |= ARM_SMMU_SCTLR_E;
302e86d1aa8SWill Deacon 
303e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
304e86d1aa8SWill Deacon 
305e86d1aa8SWill Deacon 		ctx->domain = domain;
306e86d1aa8SWill Deacon 	}
307e86d1aa8SWill Deacon 
308e86d1aa8SWill Deacon 	mutex_unlock(&qcom_domain->init_mutex);
309e86d1aa8SWill Deacon 
310e86d1aa8SWill Deacon 	/* Publish page table ops for map/unmap */
311e86d1aa8SWill Deacon 	qcom_domain->pgtbl_ops = pgtbl_ops;
312e86d1aa8SWill Deacon 
313e86d1aa8SWill Deacon 	return 0;
314e86d1aa8SWill Deacon 
315e86d1aa8SWill Deacon out_clear_iommu:
316e86d1aa8SWill Deacon 	qcom_domain->iommu = NULL;
317e86d1aa8SWill Deacon out_unlock:
318e86d1aa8SWill Deacon 	mutex_unlock(&qcom_domain->init_mutex);
319e86d1aa8SWill Deacon 	return ret;
320e86d1aa8SWill Deacon }
321e86d1aa8SWill Deacon 
322e86d1aa8SWill Deacon static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
323e86d1aa8SWill Deacon {
324e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain;
325e86d1aa8SWill Deacon 
326e86d1aa8SWill Deacon 	if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
327e86d1aa8SWill Deacon 		return NULL;
328e86d1aa8SWill Deacon 	/*
329e86d1aa8SWill Deacon 	 * Allocate the domain and initialise some of its data structures.
330e86d1aa8SWill Deacon 	 * We can't really do anything meaningful until we've added a
331e86d1aa8SWill Deacon 	 * master.
332e86d1aa8SWill Deacon 	 */
333e86d1aa8SWill Deacon 	qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
334e86d1aa8SWill Deacon 	if (!qcom_domain)
335e86d1aa8SWill Deacon 		return NULL;
336e86d1aa8SWill Deacon 
337e86d1aa8SWill Deacon 	mutex_init(&qcom_domain->init_mutex);
338e86d1aa8SWill Deacon 	spin_lock_init(&qcom_domain->pgtbl_lock);
339e86d1aa8SWill Deacon 
340e86d1aa8SWill Deacon 	return &qcom_domain->domain;
341e86d1aa8SWill Deacon }
342e86d1aa8SWill Deacon 
343e86d1aa8SWill Deacon static void qcom_iommu_domain_free(struct iommu_domain *domain)
344e86d1aa8SWill Deacon {
345e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
346e86d1aa8SWill Deacon 
347e86d1aa8SWill Deacon 	if (qcom_domain->iommu) {
348e86d1aa8SWill Deacon 		/*
349e86d1aa8SWill Deacon 		 * NOTE: unmap can be called after client device is powered
350e86d1aa8SWill Deacon 		 * off, for example, with GPUs or anything involving dma-buf.
351e86d1aa8SWill Deacon 		 * So we cannot rely on the device_link.  Make sure the IOMMU
352e86d1aa8SWill Deacon 		 * is on to avoid unclocked accesses in the TLB inv path:
353e86d1aa8SWill Deacon 		 */
354e86d1aa8SWill Deacon 		pm_runtime_get_sync(qcom_domain->iommu->dev);
355e86d1aa8SWill Deacon 		free_io_pgtable_ops(qcom_domain->pgtbl_ops);
356e86d1aa8SWill Deacon 		pm_runtime_put_sync(qcom_domain->iommu->dev);
357e86d1aa8SWill Deacon 	}
358e86d1aa8SWill Deacon 
359e86d1aa8SWill Deacon 	kfree(qcom_domain);
360e86d1aa8SWill Deacon }
361e86d1aa8SWill Deacon 
362e86d1aa8SWill Deacon static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
363e86d1aa8SWill Deacon {
364e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
365e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
366e86d1aa8SWill Deacon 	int ret;
367e86d1aa8SWill Deacon 
368e86d1aa8SWill Deacon 	if (!qcom_iommu) {
369e86d1aa8SWill Deacon 		dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
370e86d1aa8SWill Deacon 		return -ENXIO;
371e86d1aa8SWill Deacon 	}
372e86d1aa8SWill Deacon 
373e86d1aa8SWill Deacon 	/* Ensure that the domain is finalized */
374e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_iommu->dev);
375e86d1aa8SWill Deacon 	ret = qcom_iommu_init_domain(domain, qcom_iommu, dev);
376e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_iommu->dev);
377e86d1aa8SWill Deacon 	if (ret < 0)
378e86d1aa8SWill Deacon 		return ret;
379e86d1aa8SWill Deacon 
380e86d1aa8SWill Deacon 	/*
381e86d1aa8SWill Deacon 	 * Sanity check the domain. We don't support domains across
382e86d1aa8SWill Deacon 	 * different IOMMUs.
383e86d1aa8SWill Deacon 	 */
384e86d1aa8SWill Deacon 	if (qcom_domain->iommu != qcom_iommu) {
385e86d1aa8SWill Deacon 		dev_err(dev, "cannot attach to IOMMU %s while already "
386e86d1aa8SWill Deacon 			"attached to domain on IOMMU %s\n",
387e86d1aa8SWill Deacon 			dev_name(qcom_domain->iommu->dev),
388e86d1aa8SWill Deacon 			dev_name(qcom_iommu->dev));
389e86d1aa8SWill Deacon 		return -EINVAL;
390e86d1aa8SWill Deacon 	}
391e86d1aa8SWill Deacon 
392e86d1aa8SWill Deacon 	return 0;
393e86d1aa8SWill Deacon }
394e86d1aa8SWill Deacon 
395e86d1aa8SWill Deacon static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
396e86d1aa8SWill Deacon {
397e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
398e86d1aa8SWill Deacon 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
399e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
400e86d1aa8SWill Deacon 	unsigned i;
401e86d1aa8SWill Deacon 
402e86d1aa8SWill Deacon 	if (WARN_ON(!qcom_domain->iommu))
403e86d1aa8SWill Deacon 		return;
404e86d1aa8SWill Deacon 
405e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_iommu->dev);
406e86d1aa8SWill Deacon 	for (i = 0; i < fwspec->num_ids; i++) {
407e46b3c0dSJoerg Roedel 		struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
408e86d1aa8SWill Deacon 
409e86d1aa8SWill Deacon 		/* Disable the context bank: */
410e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
411e86d1aa8SWill Deacon 
412e86d1aa8SWill Deacon 		ctx->domain = NULL;
413e86d1aa8SWill Deacon 	}
414e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_iommu->dev);
415e86d1aa8SWill Deacon }
416e86d1aa8SWill Deacon 
417e86d1aa8SWill Deacon static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
418e86d1aa8SWill Deacon 			  phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
419e86d1aa8SWill Deacon {
420e86d1aa8SWill Deacon 	int ret;
421e86d1aa8SWill Deacon 	unsigned long flags;
422e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
423e86d1aa8SWill Deacon 	struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
424e86d1aa8SWill Deacon 
425e86d1aa8SWill Deacon 	if (!ops)
426e86d1aa8SWill Deacon 		return -ENODEV;
427e86d1aa8SWill Deacon 
428e86d1aa8SWill Deacon 	spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
429e46b3c0dSJoerg Roedel 	ret = ops->map(ops, iova, paddr, size, prot, GFP_ATOMIC);
430e86d1aa8SWill Deacon 	spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
431e86d1aa8SWill Deacon 	return ret;
432e86d1aa8SWill Deacon }
433e86d1aa8SWill Deacon 
434e86d1aa8SWill Deacon static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
435e86d1aa8SWill Deacon 			       size_t size, struct iommu_iotlb_gather *gather)
436e86d1aa8SWill Deacon {
437e86d1aa8SWill Deacon 	size_t ret;
438e86d1aa8SWill Deacon 	unsigned long flags;
439e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
440e86d1aa8SWill Deacon 	struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
441e86d1aa8SWill Deacon 
442e86d1aa8SWill Deacon 	if (!ops)
443e86d1aa8SWill Deacon 		return 0;
444e86d1aa8SWill Deacon 
445e86d1aa8SWill Deacon 	/* NOTE: unmap can be called after client device is powered off,
446e86d1aa8SWill Deacon 	 * for example, with GPUs or anything involving dma-buf.  So we
447e86d1aa8SWill Deacon 	 * cannot rely on the device_link.  Make sure the IOMMU is on to
448e86d1aa8SWill Deacon 	 * avoid unclocked accesses in the TLB inv path:
449e86d1aa8SWill Deacon 	 */
450e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_domain->iommu->dev);
451e86d1aa8SWill Deacon 	spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
452e86d1aa8SWill Deacon 	ret = ops->unmap(ops, iova, size, gather);
453e86d1aa8SWill Deacon 	spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
454e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_domain->iommu->dev);
455e86d1aa8SWill Deacon 
456e86d1aa8SWill Deacon 	return ret;
457e86d1aa8SWill Deacon }
458e86d1aa8SWill Deacon 
459e86d1aa8SWill Deacon static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
460e86d1aa8SWill Deacon {
461e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
462e86d1aa8SWill Deacon 	struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
463e86d1aa8SWill Deacon 						  struct io_pgtable, ops);
464e86d1aa8SWill Deacon 	if (!qcom_domain->pgtbl_ops)
465e86d1aa8SWill Deacon 		return;
466e86d1aa8SWill Deacon 
467e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_domain->iommu->dev);
468e86d1aa8SWill Deacon 	qcom_iommu_tlb_sync(pgtable->cookie);
469e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_domain->iommu->dev);
470e86d1aa8SWill Deacon }
471e86d1aa8SWill Deacon 
472e86d1aa8SWill Deacon static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
473e86d1aa8SWill Deacon 				  struct iommu_iotlb_gather *gather)
474e86d1aa8SWill Deacon {
475e86d1aa8SWill Deacon 	qcom_iommu_flush_iotlb_all(domain);
476e86d1aa8SWill Deacon }
477e86d1aa8SWill Deacon 
478e86d1aa8SWill Deacon static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
479e86d1aa8SWill Deacon 					   dma_addr_t iova)
480e86d1aa8SWill Deacon {
481e86d1aa8SWill Deacon 	phys_addr_t ret;
482e86d1aa8SWill Deacon 	unsigned long flags;
483e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
484e86d1aa8SWill Deacon 	struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
485e86d1aa8SWill Deacon 
486e86d1aa8SWill Deacon 	if (!ops)
487e86d1aa8SWill Deacon 		return 0;
488e86d1aa8SWill Deacon 
489e86d1aa8SWill Deacon 	spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
490e86d1aa8SWill Deacon 	ret = ops->iova_to_phys(ops, iova);
491e86d1aa8SWill Deacon 	spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
492e86d1aa8SWill Deacon 
493e86d1aa8SWill Deacon 	return ret;
494e86d1aa8SWill Deacon }
495e86d1aa8SWill Deacon 
496e86d1aa8SWill Deacon static bool qcom_iommu_capable(enum iommu_cap cap)
497e86d1aa8SWill Deacon {
498e86d1aa8SWill Deacon 	switch (cap) {
499e86d1aa8SWill Deacon 	case IOMMU_CAP_CACHE_COHERENCY:
500e86d1aa8SWill Deacon 		/*
501e86d1aa8SWill Deacon 		 * Return true here as the SMMU can always send out coherent
502e86d1aa8SWill Deacon 		 * requests.
503e86d1aa8SWill Deacon 		 */
504e86d1aa8SWill Deacon 		return true;
505e86d1aa8SWill Deacon 	case IOMMU_CAP_NOEXEC:
506e86d1aa8SWill Deacon 		return true;
507e86d1aa8SWill Deacon 	default:
508e86d1aa8SWill Deacon 		return false;
509e86d1aa8SWill Deacon 	}
510e86d1aa8SWill Deacon }
511e86d1aa8SWill Deacon 
512e86d1aa8SWill Deacon static struct iommu_device *qcom_iommu_probe_device(struct device *dev)
513e86d1aa8SWill Deacon {
514e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
515e86d1aa8SWill Deacon 	struct device_link *link;
516e86d1aa8SWill Deacon 
517e86d1aa8SWill Deacon 	if (!qcom_iommu)
518e86d1aa8SWill Deacon 		return ERR_PTR(-ENODEV);
519e86d1aa8SWill Deacon 
520e86d1aa8SWill Deacon 	/*
521e86d1aa8SWill Deacon 	 * Establish the link between iommu and master, so that the
522e86d1aa8SWill Deacon 	 * iommu gets runtime enabled/disabled as per the master's
523e86d1aa8SWill Deacon 	 * needs.
524e86d1aa8SWill Deacon 	 */
525e86d1aa8SWill Deacon 	link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
526e86d1aa8SWill Deacon 	if (!link) {
527e86d1aa8SWill Deacon 		dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
528e86d1aa8SWill Deacon 			dev_name(qcom_iommu->dev), dev_name(dev));
529e86d1aa8SWill Deacon 		return ERR_PTR(-ENODEV);
530e86d1aa8SWill Deacon 	}
531e86d1aa8SWill Deacon 
532e86d1aa8SWill Deacon 	return &qcom_iommu->iommu;
533e86d1aa8SWill Deacon }
534e86d1aa8SWill Deacon 
535e86d1aa8SWill Deacon static void qcom_iommu_release_device(struct device *dev)
536e86d1aa8SWill Deacon {
537e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
538e86d1aa8SWill Deacon 
539e86d1aa8SWill Deacon 	if (!qcom_iommu)
540e86d1aa8SWill Deacon 		return;
541e86d1aa8SWill Deacon 
542e86d1aa8SWill Deacon 	iommu_fwspec_free(dev);
543e86d1aa8SWill Deacon }
544e86d1aa8SWill Deacon 
545e86d1aa8SWill Deacon static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
546e86d1aa8SWill Deacon {
547e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu;
548e86d1aa8SWill Deacon 	struct platform_device *iommu_pdev;
549e86d1aa8SWill Deacon 	unsigned asid = args->args[0];
550e86d1aa8SWill Deacon 
551e86d1aa8SWill Deacon 	if (args->args_count != 1) {
552e86d1aa8SWill Deacon 		dev_err(dev, "incorrect number of iommu params found for %s "
553e86d1aa8SWill Deacon 			"(found %d, expected 1)\n",
554e86d1aa8SWill Deacon 			args->np->full_name, args->args_count);
555e86d1aa8SWill Deacon 		return -EINVAL;
556e86d1aa8SWill Deacon 	}
557e86d1aa8SWill Deacon 
558e86d1aa8SWill Deacon 	iommu_pdev = of_find_device_by_node(args->np);
559e86d1aa8SWill Deacon 	if (WARN_ON(!iommu_pdev))
560e86d1aa8SWill Deacon 		return -EINVAL;
561e86d1aa8SWill Deacon 
562e86d1aa8SWill Deacon 	qcom_iommu = platform_get_drvdata(iommu_pdev);
563e86d1aa8SWill Deacon 
564e86d1aa8SWill Deacon 	/* make sure the asid specified in dt is valid, so we don't have
565e86d1aa8SWill Deacon 	 * to sanity check this elsewhere, since 'asid - 1' is used to
566e86d1aa8SWill Deacon 	 * index into qcom_iommu->ctxs:
567e86d1aa8SWill Deacon 	 */
568e86d1aa8SWill Deacon 	if (WARN_ON(asid < 1) ||
569e2eae099SYu Kuai 	    WARN_ON(asid > qcom_iommu->num_ctxs)) {
570e2eae099SYu Kuai 		put_device(&iommu_pdev->dev);
571e86d1aa8SWill Deacon 		return -EINVAL;
572e2eae099SYu Kuai 	}
573e86d1aa8SWill Deacon 
574e86d1aa8SWill Deacon 	if (!dev_iommu_priv_get(dev)) {
575e86d1aa8SWill Deacon 		dev_iommu_priv_set(dev, qcom_iommu);
576e86d1aa8SWill Deacon 	} else {
577e86d1aa8SWill Deacon 		/* make sure devices iommus dt node isn't referring to
578e86d1aa8SWill Deacon 		 * multiple different iommu devices.  Multiple context
579e86d1aa8SWill Deacon 		 * banks are ok, but multiple devices are not:
580e86d1aa8SWill Deacon 		 */
581e2eae099SYu Kuai 		if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev))) {
582e2eae099SYu Kuai 			put_device(&iommu_pdev->dev);
583e86d1aa8SWill Deacon 			return -EINVAL;
584e86d1aa8SWill Deacon 		}
585e2eae099SYu Kuai 	}
586e86d1aa8SWill Deacon 
587e86d1aa8SWill Deacon 	return iommu_fwspec_add_ids(dev, &asid, 1);
588e86d1aa8SWill Deacon }
589e86d1aa8SWill Deacon 
590e86d1aa8SWill Deacon static const struct iommu_ops qcom_iommu_ops = {
591e86d1aa8SWill Deacon 	.capable	= qcom_iommu_capable,
592e86d1aa8SWill Deacon 	.domain_alloc	= qcom_iommu_domain_alloc,
593*9a630a4bSLu Baolu 	.probe_device	= qcom_iommu_probe_device,
594*9a630a4bSLu Baolu 	.release_device	= qcom_iommu_release_device,
595*9a630a4bSLu Baolu 	.device_group	= generic_device_group,
596*9a630a4bSLu Baolu 	.of_xlate	= qcom_iommu_of_xlate,
597*9a630a4bSLu Baolu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
598*9a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
599e86d1aa8SWill Deacon 		.attach_dev	= qcom_iommu_attach_dev,
600e86d1aa8SWill Deacon 		.detach_dev	= qcom_iommu_detach_dev,
601e86d1aa8SWill Deacon 		.map		= qcom_iommu_map,
602e86d1aa8SWill Deacon 		.unmap		= qcom_iommu_unmap,
603e86d1aa8SWill Deacon 		.flush_iotlb_all = qcom_iommu_flush_iotlb_all,
604e86d1aa8SWill Deacon 		.iotlb_sync	= qcom_iommu_iotlb_sync,
605e86d1aa8SWill Deacon 		.iova_to_phys	= qcom_iommu_iova_to_phys,
606*9a630a4bSLu Baolu 		.free		= qcom_iommu_domain_free,
607*9a630a4bSLu Baolu 	}
608e86d1aa8SWill Deacon };
609e86d1aa8SWill Deacon 
610e86d1aa8SWill Deacon static int qcom_iommu_sec_ptbl_init(struct device *dev)
611e86d1aa8SWill Deacon {
612e86d1aa8SWill Deacon 	size_t psize = 0;
613e86d1aa8SWill Deacon 	unsigned int spare = 0;
614e86d1aa8SWill Deacon 	void *cpu_addr;
615e86d1aa8SWill Deacon 	dma_addr_t paddr;
616e86d1aa8SWill Deacon 	unsigned long attrs;
617e86d1aa8SWill Deacon 	static bool allocated = false;
618e86d1aa8SWill Deacon 	int ret;
619e86d1aa8SWill Deacon 
620e86d1aa8SWill Deacon 	if (allocated)
621e86d1aa8SWill Deacon 		return 0;
622e86d1aa8SWill Deacon 
623e86d1aa8SWill Deacon 	ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
624e86d1aa8SWill Deacon 	if (ret) {
625e86d1aa8SWill Deacon 		dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
626e86d1aa8SWill Deacon 			ret);
627e86d1aa8SWill Deacon 		return ret;
628e86d1aa8SWill Deacon 	}
629e86d1aa8SWill Deacon 
630e86d1aa8SWill Deacon 	dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
631e86d1aa8SWill Deacon 
632e86d1aa8SWill Deacon 	attrs = DMA_ATTR_NO_KERNEL_MAPPING;
633e86d1aa8SWill Deacon 
634e86d1aa8SWill Deacon 	cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
635e86d1aa8SWill Deacon 	if (!cpu_addr) {
636e86d1aa8SWill Deacon 		dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
637e86d1aa8SWill Deacon 			psize);
638e86d1aa8SWill Deacon 		return -ENOMEM;
639e86d1aa8SWill Deacon 	}
640e86d1aa8SWill Deacon 
641e86d1aa8SWill Deacon 	ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
642e86d1aa8SWill Deacon 	if (ret) {
643e86d1aa8SWill Deacon 		dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
644e86d1aa8SWill Deacon 		goto free_mem;
645e86d1aa8SWill Deacon 	}
646e86d1aa8SWill Deacon 
647e86d1aa8SWill Deacon 	allocated = true;
648e86d1aa8SWill Deacon 	return 0;
649e86d1aa8SWill Deacon 
650e86d1aa8SWill Deacon free_mem:
651e86d1aa8SWill Deacon 	dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
652e86d1aa8SWill Deacon 	return ret;
653e86d1aa8SWill Deacon }
654e86d1aa8SWill Deacon 
655e86d1aa8SWill Deacon static int get_asid(const struct device_node *np)
656e86d1aa8SWill Deacon {
657e86d1aa8SWill Deacon 	u32 reg;
658e86d1aa8SWill Deacon 
659e86d1aa8SWill Deacon 	/* read the "reg" property directly to get the relative address
660e86d1aa8SWill Deacon 	 * of the context bank, and calculate the asid from that:
661e86d1aa8SWill Deacon 	 */
662e86d1aa8SWill Deacon 	if (of_property_read_u32_index(np, "reg", 0, &reg))
663e86d1aa8SWill Deacon 		return -ENODEV;
664e86d1aa8SWill Deacon 
665e86d1aa8SWill Deacon 	return reg / 0x1000;      /* context banks are 0x1000 apart */
666e86d1aa8SWill Deacon }
667e86d1aa8SWill Deacon 
668e86d1aa8SWill Deacon static int qcom_iommu_ctx_probe(struct platform_device *pdev)
669e86d1aa8SWill Deacon {
670e86d1aa8SWill Deacon 	struct qcom_iommu_ctx *ctx;
671e86d1aa8SWill Deacon 	struct device *dev = &pdev->dev;
672e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
673e86d1aa8SWill Deacon 	struct resource *res;
674e86d1aa8SWill Deacon 	int ret, irq;
675e86d1aa8SWill Deacon 
676e86d1aa8SWill Deacon 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
677e86d1aa8SWill Deacon 	if (!ctx)
678e86d1aa8SWill Deacon 		return -ENOMEM;
679e86d1aa8SWill Deacon 
680e86d1aa8SWill Deacon 	ctx->dev = dev;
681e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, ctx);
682e86d1aa8SWill Deacon 
683e86d1aa8SWill Deacon 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
684e86d1aa8SWill Deacon 	ctx->base = devm_ioremap_resource(dev, res);
685e86d1aa8SWill Deacon 	if (IS_ERR(ctx->base))
686e86d1aa8SWill Deacon 		return PTR_ERR(ctx->base);
687e86d1aa8SWill Deacon 
688e86d1aa8SWill Deacon 	irq = platform_get_irq(pdev, 0);
689e86d1aa8SWill Deacon 	if (irq < 0)
690e86d1aa8SWill Deacon 		return -ENODEV;
691e86d1aa8SWill Deacon 
692e86d1aa8SWill Deacon 	/* clear IRQs before registering fault handler, just in case the
693e86d1aa8SWill Deacon 	 * boot-loader left us a surprise:
694e86d1aa8SWill Deacon 	 */
695e86d1aa8SWill Deacon 	iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
696e86d1aa8SWill Deacon 
697e86d1aa8SWill Deacon 	ret = devm_request_irq(dev, irq,
698e86d1aa8SWill Deacon 			       qcom_iommu_fault,
699e86d1aa8SWill Deacon 			       IRQF_SHARED,
700e86d1aa8SWill Deacon 			       "qcom-iommu-fault",
701e86d1aa8SWill Deacon 			       ctx);
702e86d1aa8SWill Deacon 	if (ret) {
703e86d1aa8SWill Deacon 		dev_err(dev, "failed to request IRQ %u\n", irq);
704e86d1aa8SWill Deacon 		return ret;
705e86d1aa8SWill Deacon 	}
706e86d1aa8SWill Deacon 
707e86d1aa8SWill Deacon 	ret = get_asid(dev->of_node);
708e86d1aa8SWill Deacon 	if (ret < 0) {
709e86d1aa8SWill Deacon 		dev_err(dev, "missing reg property\n");
710e86d1aa8SWill Deacon 		return ret;
711e86d1aa8SWill Deacon 	}
712e86d1aa8SWill Deacon 
713e86d1aa8SWill Deacon 	ctx->asid = ret;
714e86d1aa8SWill Deacon 
715e86d1aa8SWill Deacon 	dev_dbg(dev, "found asid %u\n", ctx->asid);
716e86d1aa8SWill Deacon 
717e86d1aa8SWill Deacon 	qcom_iommu->ctxs[ctx->asid - 1] = ctx;
718e86d1aa8SWill Deacon 
719e86d1aa8SWill Deacon 	return 0;
720e86d1aa8SWill Deacon }
721e86d1aa8SWill Deacon 
722e86d1aa8SWill Deacon static int qcom_iommu_ctx_remove(struct platform_device *pdev)
723e86d1aa8SWill Deacon {
724e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
725e86d1aa8SWill Deacon 	struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
726e86d1aa8SWill Deacon 
727e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, NULL);
728e86d1aa8SWill Deacon 
729e86d1aa8SWill Deacon 	qcom_iommu->ctxs[ctx->asid - 1] = NULL;
730e86d1aa8SWill Deacon 
731e86d1aa8SWill Deacon 	return 0;
732e86d1aa8SWill Deacon }
733e86d1aa8SWill Deacon 
734e86d1aa8SWill Deacon static const struct of_device_id ctx_of_match[] = {
735e86d1aa8SWill Deacon 	{ .compatible = "qcom,msm-iommu-v1-ns" },
736e86d1aa8SWill Deacon 	{ .compatible = "qcom,msm-iommu-v1-sec" },
737e86d1aa8SWill Deacon 	{ /* sentinel */ }
738e86d1aa8SWill Deacon };
739e86d1aa8SWill Deacon 
740e86d1aa8SWill Deacon static struct platform_driver qcom_iommu_ctx_driver = {
741e86d1aa8SWill Deacon 	.driver	= {
742e86d1aa8SWill Deacon 		.name		= "qcom-iommu-ctx",
7437aaf0b0eSKrzysztof Kozlowski 		.of_match_table	= ctx_of_match,
744e86d1aa8SWill Deacon 	},
745e86d1aa8SWill Deacon 	.probe	= qcom_iommu_ctx_probe,
746e86d1aa8SWill Deacon 	.remove = qcom_iommu_ctx_remove,
747e86d1aa8SWill Deacon };
748e86d1aa8SWill Deacon 
749e86d1aa8SWill Deacon static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
750e86d1aa8SWill Deacon {
751e86d1aa8SWill Deacon 	struct device_node *child;
752e86d1aa8SWill Deacon 
753e86d1aa8SWill Deacon 	for_each_child_of_node(qcom_iommu->dev->of_node, child)
754e86d1aa8SWill Deacon 		if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec"))
755e86d1aa8SWill Deacon 			return true;
756e86d1aa8SWill Deacon 
757e86d1aa8SWill Deacon 	return false;
758e86d1aa8SWill Deacon }
759e86d1aa8SWill Deacon 
760e86d1aa8SWill Deacon static int qcom_iommu_device_probe(struct platform_device *pdev)
761e86d1aa8SWill Deacon {
762e86d1aa8SWill Deacon 	struct device_node *child;
763e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu;
764e86d1aa8SWill Deacon 	struct device *dev = &pdev->dev;
765e86d1aa8SWill Deacon 	struct resource *res;
766e46b3c0dSJoerg Roedel 	struct clk *clk;
767e86d1aa8SWill Deacon 	int ret, max_asid = 0;
768e86d1aa8SWill Deacon 
769e86d1aa8SWill Deacon 	/* find the max asid (which is 1:1 to ctx bank idx), so we know how
770e86d1aa8SWill Deacon 	 * many child ctx devices we have:
771e86d1aa8SWill Deacon 	 */
772e86d1aa8SWill Deacon 	for_each_child_of_node(dev->of_node, child)
773e86d1aa8SWill Deacon 		max_asid = max(max_asid, get_asid(child));
774e86d1aa8SWill Deacon 
775e86d1aa8SWill Deacon 	qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
776e86d1aa8SWill Deacon 				  GFP_KERNEL);
777e86d1aa8SWill Deacon 	if (!qcom_iommu)
778e86d1aa8SWill Deacon 		return -ENOMEM;
779e86d1aa8SWill Deacon 	qcom_iommu->num_ctxs = max_asid;
780e86d1aa8SWill Deacon 	qcom_iommu->dev = dev;
781e86d1aa8SWill Deacon 
782e86d1aa8SWill Deacon 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
783e86d1aa8SWill Deacon 	if (res) {
784e86d1aa8SWill Deacon 		qcom_iommu->local_base = devm_ioremap_resource(dev, res);
785e86d1aa8SWill Deacon 		if (IS_ERR(qcom_iommu->local_base))
786e86d1aa8SWill Deacon 			return PTR_ERR(qcom_iommu->local_base);
787e86d1aa8SWill Deacon 	}
788e86d1aa8SWill Deacon 
789e46b3c0dSJoerg Roedel 	clk = devm_clk_get(dev, "iface");
790e46b3c0dSJoerg Roedel 	if (IS_ERR(clk)) {
791e86d1aa8SWill Deacon 		dev_err(dev, "failed to get iface clock\n");
792e46b3c0dSJoerg Roedel 		return PTR_ERR(clk);
793e86d1aa8SWill Deacon 	}
794e46b3c0dSJoerg Roedel 	qcom_iommu->clks[CLK_IFACE].clk = clk;
795e86d1aa8SWill Deacon 
796e46b3c0dSJoerg Roedel 	clk = devm_clk_get(dev, "bus");
797e46b3c0dSJoerg Roedel 	if (IS_ERR(clk)) {
798e86d1aa8SWill Deacon 		dev_err(dev, "failed to get bus clock\n");
799e46b3c0dSJoerg Roedel 		return PTR_ERR(clk);
800e86d1aa8SWill Deacon 	}
801e46b3c0dSJoerg Roedel 	qcom_iommu->clks[CLK_BUS].clk = clk;
802e46b3c0dSJoerg Roedel 
803e46b3c0dSJoerg Roedel 	clk = devm_clk_get_optional(dev, "tbu");
804e46b3c0dSJoerg Roedel 	if (IS_ERR(clk)) {
805e46b3c0dSJoerg Roedel 		dev_err(dev, "failed to get tbu clock\n");
806e46b3c0dSJoerg Roedel 		return PTR_ERR(clk);
807e46b3c0dSJoerg Roedel 	}
808e46b3c0dSJoerg Roedel 	qcom_iommu->clks[CLK_TBU].clk = clk;
809e86d1aa8SWill Deacon 
810e86d1aa8SWill Deacon 	if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
811e86d1aa8SWill Deacon 				 &qcom_iommu->sec_id)) {
812e86d1aa8SWill Deacon 		dev_err(dev, "missing qcom,iommu-secure-id property\n");
813e86d1aa8SWill Deacon 		return -ENODEV;
814e86d1aa8SWill Deacon 	}
815e86d1aa8SWill Deacon 
816e86d1aa8SWill Deacon 	if (qcom_iommu_has_secure_context(qcom_iommu)) {
817e86d1aa8SWill Deacon 		ret = qcom_iommu_sec_ptbl_init(dev);
818e86d1aa8SWill Deacon 		if (ret) {
819e86d1aa8SWill Deacon 			dev_err(dev, "cannot init secure pg table(%d)\n", ret);
820e86d1aa8SWill Deacon 			return ret;
821e86d1aa8SWill Deacon 		}
822e86d1aa8SWill Deacon 	}
823e86d1aa8SWill Deacon 
824e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, qcom_iommu);
825e86d1aa8SWill Deacon 
826e86d1aa8SWill Deacon 	pm_runtime_enable(dev);
827e86d1aa8SWill Deacon 
828e86d1aa8SWill Deacon 	/* register context bank devices, which are child nodes: */
829e86d1aa8SWill Deacon 	ret = devm_of_platform_populate(dev);
830e86d1aa8SWill Deacon 	if (ret) {
831e86d1aa8SWill Deacon 		dev_err(dev, "Failed to populate iommu contexts\n");
832e86d1aa8SWill Deacon 		return ret;
833e86d1aa8SWill Deacon 	}
834e86d1aa8SWill Deacon 
835e86d1aa8SWill Deacon 	ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
836e86d1aa8SWill Deacon 				     dev_name(dev));
837e86d1aa8SWill Deacon 	if (ret) {
838e86d1aa8SWill Deacon 		dev_err(dev, "Failed to register iommu in sysfs\n");
839e86d1aa8SWill Deacon 		return ret;
840e86d1aa8SWill Deacon 	}
841e86d1aa8SWill Deacon 
8422d471b20SRobin Murphy 	ret = iommu_device_register(&qcom_iommu->iommu, &qcom_iommu_ops, dev);
843e86d1aa8SWill Deacon 	if (ret) {
844e86d1aa8SWill Deacon 		dev_err(dev, "Failed to register iommu\n");
845ce36c942SMarek Szyprowski 		return ret;
846e86d1aa8SWill Deacon 	}
847e86d1aa8SWill Deacon 
848ce36c942SMarek Szyprowski 	bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
849e86d1aa8SWill Deacon 
850e86d1aa8SWill Deacon 	if (qcom_iommu->local_base) {
851e86d1aa8SWill Deacon 		pm_runtime_get_sync(dev);
852e86d1aa8SWill Deacon 		writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
853e86d1aa8SWill Deacon 		pm_runtime_put_sync(dev);
854e86d1aa8SWill Deacon 	}
855e86d1aa8SWill Deacon 
856e86d1aa8SWill Deacon 	return 0;
857e86d1aa8SWill Deacon }
858e86d1aa8SWill Deacon 
859e86d1aa8SWill Deacon static int qcom_iommu_device_remove(struct platform_device *pdev)
860e86d1aa8SWill Deacon {
861e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
862e86d1aa8SWill Deacon 
863e86d1aa8SWill Deacon 	bus_set_iommu(&platform_bus_type, NULL);
864e86d1aa8SWill Deacon 
865e86d1aa8SWill Deacon 	pm_runtime_force_suspend(&pdev->dev);
866e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, NULL);
867e86d1aa8SWill Deacon 	iommu_device_sysfs_remove(&qcom_iommu->iommu);
868e86d1aa8SWill Deacon 	iommu_device_unregister(&qcom_iommu->iommu);
869e86d1aa8SWill Deacon 
870e86d1aa8SWill Deacon 	return 0;
871e86d1aa8SWill Deacon }
872e86d1aa8SWill Deacon 
873e86d1aa8SWill Deacon static int __maybe_unused qcom_iommu_resume(struct device *dev)
874e86d1aa8SWill Deacon {
875e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
876e86d1aa8SWill Deacon 
877e46b3c0dSJoerg Roedel 	return clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks);
878e86d1aa8SWill Deacon }
879e86d1aa8SWill Deacon 
880e86d1aa8SWill Deacon static int __maybe_unused qcom_iommu_suspend(struct device *dev)
881e86d1aa8SWill Deacon {
882e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
883e86d1aa8SWill Deacon 
884e46b3c0dSJoerg Roedel 	clk_bulk_disable_unprepare(CLK_NUM, qcom_iommu->clks);
885e86d1aa8SWill Deacon 
886e86d1aa8SWill Deacon 	return 0;
887e86d1aa8SWill Deacon }
888e86d1aa8SWill Deacon 
889e86d1aa8SWill Deacon static const struct dev_pm_ops qcom_iommu_pm_ops = {
890e86d1aa8SWill Deacon 	SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
891e86d1aa8SWill Deacon 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
892e86d1aa8SWill Deacon 				pm_runtime_force_resume)
893e86d1aa8SWill Deacon };
894e86d1aa8SWill Deacon 
895e86d1aa8SWill Deacon static const struct of_device_id qcom_iommu_of_match[] = {
896e86d1aa8SWill Deacon 	{ .compatible = "qcom,msm-iommu-v1" },
897e86d1aa8SWill Deacon 	{ /* sentinel */ }
898e86d1aa8SWill Deacon };
899e86d1aa8SWill Deacon 
900e86d1aa8SWill Deacon static struct platform_driver qcom_iommu_driver = {
901e86d1aa8SWill Deacon 	.driver	= {
902e86d1aa8SWill Deacon 		.name		= "qcom-iommu",
9037aaf0b0eSKrzysztof Kozlowski 		.of_match_table	= qcom_iommu_of_match,
904e86d1aa8SWill Deacon 		.pm		= &qcom_iommu_pm_ops,
905e86d1aa8SWill Deacon 	},
906e86d1aa8SWill Deacon 	.probe	= qcom_iommu_device_probe,
907e86d1aa8SWill Deacon 	.remove	= qcom_iommu_device_remove,
908e86d1aa8SWill Deacon };
909e86d1aa8SWill Deacon 
910e86d1aa8SWill Deacon static int __init qcom_iommu_init(void)
911e86d1aa8SWill Deacon {
912e86d1aa8SWill Deacon 	int ret;
913e86d1aa8SWill Deacon 
914e86d1aa8SWill Deacon 	ret = platform_driver_register(&qcom_iommu_ctx_driver);
915e86d1aa8SWill Deacon 	if (ret)
916e86d1aa8SWill Deacon 		return ret;
917e86d1aa8SWill Deacon 
918e86d1aa8SWill Deacon 	ret = platform_driver_register(&qcom_iommu_driver);
919e86d1aa8SWill Deacon 	if (ret)
920e86d1aa8SWill Deacon 		platform_driver_unregister(&qcom_iommu_ctx_driver);
921e86d1aa8SWill Deacon 
922e86d1aa8SWill Deacon 	return ret;
923e86d1aa8SWill Deacon }
924e86d1aa8SWill Deacon device_initcall(qcom_iommu_init);
925