xref: /openbmc/linux/drivers/iommu/arm/arm-smmu/qcom_iommu.c (revision 9f3fef23d9b5a858a6e6d5f478bb1b6b76265e76)
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>
303bf90ecaSElliot Berman #include <linux/firmware/qcom/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 
276*9f3fef23SAngeloGioacchino Del Regno 		/* Disable context bank before programming */
277*9f3fef23SAngeloGioacchino Del Regno 		iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
278*9f3fef23SAngeloGioacchino Del Regno 
279*9f3fef23SAngeloGioacchino Del Regno 		/* Clear context bank fault address fault status registers */
280*9f3fef23SAngeloGioacchino Del Regno 		iommu_writel(ctx, ARM_SMMU_CB_FAR, 0);
281*9f3fef23SAngeloGioacchino Del Regno 		iommu_writel(ctx, ARM_SMMU_CB_FSR, ARM_SMMU_FSR_FAULT);
282*9f3fef23SAngeloGioacchino Del Regno 
283e86d1aa8SWill Deacon 		/* TTBRs */
284e86d1aa8SWill Deacon 		iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
285e86d1aa8SWill Deacon 				pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
286e86d1aa8SWill Deacon 				FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
287e86d1aa8SWill Deacon 		iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
288e86d1aa8SWill Deacon 
289e86d1aa8SWill Deacon 		/* TCR */
290e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_TCR2,
291e86d1aa8SWill Deacon 				arm_smmu_lpae_tcr2(&pgtbl_cfg));
292e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_TCR,
293e86d1aa8SWill Deacon 			     arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
294e86d1aa8SWill Deacon 
295e86d1aa8SWill Deacon 		/* MAIRs (stage-1 only) */
296e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
297e86d1aa8SWill Deacon 				pgtbl_cfg.arm_lpae_s1_cfg.mair);
298e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
299e86d1aa8SWill Deacon 				pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
300e86d1aa8SWill Deacon 
301e86d1aa8SWill Deacon 		/* SCTLR */
302e86d1aa8SWill Deacon 		reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
303e86d1aa8SWill Deacon 		      ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
304e86d1aa8SWill Deacon 		      ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
305e86d1aa8SWill Deacon 		      ARM_SMMU_SCTLR_CFCFG;
306e86d1aa8SWill Deacon 
307e46b3c0dSJoerg Roedel 		if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
308e86d1aa8SWill Deacon 			reg |= ARM_SMMU_SCTLR_E;
309e86d1aa8SWill Deacon 
310e86d1aa8SWill Deacon 		iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
311e86d1aa8SWill Deacon 
312e86d1aa8SWill Deacon 		ctx->domain = domain;
313e86d1aa8SWill Deacon 	}
314e86d1aa8SWill Deacon 
315e86d1aa8SWill Deacon 	mutex_unlock(&qcom_domain->init_mutex);
316e86d1aa8SWill Deacon 
317e86d1aa8SWill Deacon 	/* Publish page table ops for map/unmap */
318e86d1aa8SWill Deacon 	qcom_domain->pgtbl_ops = pgtbl_ops;
319e86d1aa8SWill Deacon 
320e86d1aa8SWill Deacon 	return 0;
321e86d1aa8SWill Deacon 
322e86d1aa8SWill Deacon out_clear_iommu:
323e86d1aa8SWill Deacon 	qcom_domain->iommu = NULL;
324e86d1aa8SWill Deacon out_unlock:
325e86d1aa8SWill Deacon 	mutex_unlock(&qcom_domain->init_mutex);
326e86d1aa8SWill Deacon 	return ret;
327e86d1aa8SWill Deacon }
328e86d1aa8SWill Deacon 
329e86d1aa8SWill Deacon static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
330e86d1aa8SWill Deacon {
331e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain;
332e86d1aa8SWill Deacon 
333e86d1aa8SWill Deacon 	if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
334e86d1aa8SWill Deacon 		return NULL;
335e86d1aa8SWill Deacon 	/*
336e86d1aa8SWill Deacon 	 * Allocate the domain and initialise some of its data structures.
337e86d1aa8SWill Deacon 	 * We can't really do anything meaningful until we've added a
338e86d1aa8SWill Deacon 	 * master.
339e86d1aa8SWill Deacon 	 */
340e86d1aa8SWill Deacon 	qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
341e86d1aa8SWill Deacon 	if (!qcom_domain)
342e86d1aa8SWill Deacon 		return NULL;
343e86d1aa8SWill Deacon 
344e86d1aa8SWill Deacon 	mutex_init(&qcom_domain->init_mutex);
345e86d1aa8SWill Deacon 	spin_lock_init(&qcom_domain->pgtbl_lock);
346e86d1aa8SWill Deacon 
347e86d1aa8SWill Deacon 	return &qcom_domain->domain;
348e86d1aa8SWill Deacon }
349e86d1aa8SWill Deacon 
350e86d1aa8SWill Deacon static void qcom_iommu_domain_free(struct iommu_domain *domain)
351e86d1aa8SWill Deacon {
352e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
353e86d1aa8SWill Deacon 
354e86d1aa8SWill Deacon 	if (qcom_domain->iommu) {
355e86d1aa8SWill Deacon 		/*
356e86d1aa8SWill Deacon 		 * NOTE: unmap can be called after client device is powered
357e86d1aa8SWill Deacon 		 * off, for example, with GPUs or anything involving dma-buf.
358e86d1aa8SWill Deacon 		 * So we cannot rely on the device_link.  Make sure the IOMMU
359e86d1aa8SWill Deacon 		 * is on to avoid unclocked accesses in the TLB inv path:
360e86d1aa8SWill Deacon 		 */
361e86d1aa8SWill Deacon 		pm_runtime_get_sync(qcom_domain->iommu->dev);
362e86d1aa8SWill Deacon 		free_io_pgtable_ops(qcom_domain->pgtbl_ops);
363e86d1aa8SWill Deacon 		pm_runtime_put_sync(qcom_domain->iommu->dev);
364e86d1aa8SWill Deacon 	}
365e86d1aa8SWill Deacon 
366e86d1aa8SWill Deacon 	kfree(qcom_domain);
367e86d1aa8SWill Deacon }
368e86d1aa8SWill Deacon 
369e86d1aa8SWill Deacon static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
370e86d1aa8SWill Deacon {
371e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
372e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
373e86d1aa8SWill Deacon 	int ret;
374e86d1aa8SWill Deacon 
375e86d1aa8SWill Deacon 	if (!qcom_iommu) {
376e86d1aa8SWill Deacon 		dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
377e86d1aa8SWill Deacon 		return -ENXIO;
378e86d1aa8SWill Deacon 	}
379e86d1aa8SWill Deacon 
380e86d1aa8SWill Deacon 	/* Ensure that the domain is finalized */
381e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_iommu->dev);
382e86d1aa8SWill Deacon 	ret = qcom_iommu_init_domain(domain, qcom_iommu, dev);
383e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_iommu->dev);
384e86d1aa8SWill Deacon 	if (ret < 0)
385e86d1aa8SWill Deacon 		return ret;
386e86d1aa8SWill Deacon 
387e86d1aa8SWill Deacon 	/*
388e86d1aa8SWill Deacon 	 * Sanity check the domain. We don't support domains across
389e86d1aa8SWill Deacon 	 * different IOMMUs.
390e86d1aa8SWill Deacon 	 */
391f4a14773SNicolin Chen 	if (qcom_domain->iommu != qcom_iommu)
392e86d1aa8SWill Deacon 		return -EINVAL;
393e86d1aa8SWill Deacon 
394e86d1aa8SWill Deacon 	return 0;
395e86d1aa8SWill Deacon }
396e86d1aa8SWill Deacon 
397e86d1aa8SWill Deacon static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
398fa8ce574SRobin Murphy 			  phys_addr_t paddr, size_t pgsize, size_t pgcount,
399fa8ce574SRobin Murphy 			  int prot, gfp_t gfp, size_t *mapped)
400e86d1aa8SWill Deacon {
401e86d1aa8SWill Deacon 	int ret;
402e86d1aa8SWill Deacon 	unsigned long flags;
403e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
404e86d1aa8SWill Deacon 	struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
405e86d1aa8SWill Deacon 
406e86d1aa8SWill Deacon 	if (!ops)
407e86d1aa8SWill Deacon 		return -ENODEV;
408e86d1aa8SWill Deacon 
409e86d1aa8SWill Deacon 	spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
410fa8ce574SRobin Murphy 	ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, GFP_ATOMIC, mapped);
411e86d1aa8SWill Deacon 	spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
412e86d1aa8SWill Deacon 	return ret;
413e86d1aa8SWill Deacon }
414e86d1aa8SWill Deacon 
415e86d1aa8SWill Deacon static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
416fa8ce574SRobin Murphy 			       size_t pgsize, size_t pgcount,
417fa8ce574SRobin Murphy 			       struct iommu_iotlb_gather *gather)
418e86d1aa8SWill Deacon {
419e86d1aa8SWill Deacon 	size_t ret;
420e86d1aa8SWill Deacon 	unsigned long flags;
421e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
422e86d1aa8SWill Deacon 	struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
423e86d1aa8SWill Deacon 
424e86d1aa8SWill Deacon 	if (!ops)
425e86d1aa8SWill Deacon 		return 0;
426e86d1aa8SWill Deacon 
427e86d1aa8SWill Deacon 	/* NOTE: unmap can be called after client device is powered off,
428e86d1aa8SWill Deacon 	 * for example, with GPUs or anything involving dma-buf.  So we
429e86d1aa8SWill Deacon 	 * cannot rely on the device_link.  Make sure the IOMMU is on to
430e86d1aa8SWill Deacon 	 * avoid unclocked accesses in the TLB inv path:
431e86d1aa8SWill Deacon 	 */
432e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_domain->iommu->dev);
433e86d1aa8SWill Deacon 	spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
434fa8ce574SRobin Murphy 	ret = ops->unmap_pages(ops, iova, pgsize, pgcount, gather);
435e86d1aa8SWill Deacon 	spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
436e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_domain->iommu->dev);
437e86d1aa8SWill Deacon 
438e86d1aa8SWill Deacon 	return ret;
439e86d1aa8SWill Deacon }
440e86d1aa8SWill Deacon 
441e86d1aa8SWill Deacon static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
442e86d1aa8SWill Deacon {
443e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
444e86d1aa8SWill Deacon 	struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
445e86d1aa8SWill Deacon 						  struct io_pgtable, ops);
446e86d1aa8SWill Deacon 	if (!qcom_domain->pgtbl_ops)
447e86d1aa8SWill Deacon 		return;
448e86d1aa8SWill Deacon 
449e86d1aa8SWill Deacon 	pm_runtime_get_sync(qcom_domain->iommu->dev);
450e86d1aa8SWill Deacon 	qcom_iommu_tlb_sync(pgtable->cookie);
451e86d1aa8SWill Deacon 	pm_runtime_put_sync(qcom_domain->iommu->dev);
452e86d1aa8SWill Deacon }
453e86d1aa8SWill Deacon 
454e86d1aa8SWill Deacon static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
455e86d1aa8SWill Deacon 				  struct iommu_iotlb_gather *gather)
456e86d1aa8SWill Deacon {
457e86d1aa8SWill Deacon 	qcom_iommu_flush_iotlb_all(domain);
458e86d1aa8SWill Deacon }
459e86d1aa8SWill Deacon 
460e86d1aa8SWill Deacon static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
461e86d1aa8SWill Deacon 					   dma_addr_t iova)
462e86d1aa8SWill Deacon {
463e86d1aa8SWill Deacon 	phys_addr_t ret;
464e86d1aa8SWill Deacon 	unsigned long flags;
465e86d1aa8SWill Deacon 	struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
466e86d1aa8SWill Deacon 	struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
467e86d1aa8SWill Deacon 
468e86d1aa8SWill Deacon 	if (!ops)
469e86d1aa8SWill Deacon 		return 0;
470e86d1aa8SWill Deacon 
471e86d1aa8SWill Deacon 	spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
472e86d1aa8SWill Deacon 	ret = ops->iova_to_phys(ops, iova);
473e86d1aa8SWill Deacon 	spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
474e86d1aa8SWill Deacon 
475e86d1aa8SWill Deacon 	return ret;
476e86d1aa8SWill Deacon }
477e86d1aa8SWill Deacon 
478359ad157SRobin Murphy static bool qcom_iommu_capable(struct device *dev, enum iommu_cap cap)
479e86d1aa8SWill Deacon {
480e86d1aa8SWill Deacon 	switch (cap) {
481e86d1aa8SWill Deacon 	case IOMMU_CAP_CACHE_COHERENCY:
482e86d1aa8SWill Deacon 		/*
483e86d1aa8SWill Deacon 		 * Return true here as the SMMU can always send out coherent
484e86d1aa8SWill Deacon 		 * requests.
485e86d1aa8SWill Deacon 		 */
486e86d1aa8SWill Deacon 		return true;
487e86d1aa8SWill Deacon 	case IOMMU_CAP_NOEXEC:
488e86d1aa8SWill Deacon 		return true;
489e86d1aa8SWill Deacon 	default:
490e86d1aa8SWill Deacon 		return false;
491e86d1aa8SWill Deacon 	}
492e86d1aa8SWill Deacon }
493e86d1aa8SWill Deacon 
494e86d1aa8SWill Deacon static struct iommu_device *qcom_iommu_probe_device(struct device *dev)
495e86d1aa8SWill Deacon {
496e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
497e86d1aa8SWill Deacon 	struct device_link *link;
498e86d1aa8SWill Deacon 
499e86d1aa8SWill Deacon 	if (!qcom_iommu)
500e86d1aa8SWill Deacon 		return ERR_PTR(-ENODEV);
501e86d1aa8SWill Deacon 
502e86d1aa8SWill Deacon 	/*
503e86d1aa8SWill Deacon 	 * Establish the link between iommu and master, so that the
504e86d1aa8SWill Deacon 	 * iommu gets runtime enabled/disabled as per the master's
505e86d1aa8SWill Deacon 	 * needs.
506e86d1aa8SWill Deacon 	 */
507e86d1aa8SWill Deacon 	link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
508e86d1aa8SWill Deacon 	if (!link) {
509e86d1aa8SWill Deacon 		dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
510e86d1aa8SWill Deacon 			dev_name(qcom_iommu->dev), dev_name(dev));
511e86d1aa8SWill Deacon 		return ERR_PTR(-ENODEV);
512e86d1aa8SWill Deacon 	}
513e86d1aa8SWill Deacon 
514e86d1aa8SWill Deacon 	return &qcom_iommu->iommu;
515e86d1aa8SWill Deacon }
516e86d1aa8SWill Deacon 
517e86d1aa8SWill Deacon static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
518e86d1aa8SWill Deacon {
519e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu;
520e86d1aa8SWill Deacon 	struct platform_device *iommu_pdev;
521e86d1aa8SWill Deacon 	unsigned asid = args->args[0];
522e86d1aa8SWill Deacon 
523e86d1aa8SWill Deacon 	if (args->args_count != 1) {
524e86d1aa8SWill Deacon 		dev_err(dev, "incorrect number of iommu params found for %s "
525e86d1aa8SWill Deacon 			"(found %d, expected 1)\n",
526e86d1aa8SWill Deacon 			args->np->full_name, args->args_count);
527e86d1aa8SWill Deacon 		return -EINVAL;
528e86d1aa8SWill Deacon 	}
529e86d1aa8SWill Deacon 
530e86d1aa8SWill Deacon 	iommu_pdev = of_find_device_by_node(args->np);
531e86d1aa8SWill Deacon 	if (WARN_ON(!iommu_pdev))
532e86d1aa8SWill Deacon 		return -EINVAL;
533e86d1aa8SWill Deacon 
534e86d1aa8SWill Deacon 	qcom_iommu = platform_get_drvdata(iommu_pdev);
535e86d1aa8SWill Deacon 
536e86d1aa8SWill Deacon 	/* make sure the asid specified in dt is valid, so we don't have
537e86d1aa8SWill Deacon 	 * to sanity check this elsewhere, since 'asid - 1' is used to
538e86d1aa8SWill Deacon 	 * index into qcom_iommu->ctxs:
539e86d1aa8SWill Deacon 	 */
540e86d1aa8SWill Deacon 	if (WARN_ON(asid < 1) ||
541fcf226f1SAngeloGioacchino Del Regno 	    WARN_ON(asid > qcom_iommu->num_ctxs) ||
542fcf226f1SAngeloGioacchino Del Regno 	    WARN_ON(qcom_iommu->ctxs[asid - 1] == NULL)) {
543e2eae099SYu Kuai 		put_device(&iommu_pdev->dev);
544e86d1aa8SWill Deacon 		return -EINVAL;
545e2eae099SYu Kuai 	}
546e86d1aa8SWill Deacon 
547e86d1aa8SWill Deacon 	if (!dev_iommu_priv_get(dev)) {
548e86d1aa8SWill Deacon 		dev_iommu_priv_set(dev, qcom_iommu);
549e86d1aa8SWill Deacon 	} else {
550e86d1aa8SWill Deacon 		/* make sure devices iommus dt node isn't referring to
551e86d1aa8SWill Deacon 		 * multiple different iommu devices.  Multiple context
552e86d1aa8SWill Deacon 		 * banks are ok, but multiple devices are not:
553e86d1aa8SWill Deacon 		 */
554e2eae099SYu Kuai 		if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev))) {
555e2eae099SYu Kuai 			put_device(&iommu_pdev->dev);
556e86d1aa8SWill Deacon 			return -EINVAL;
557e86d1aa8SWill Deacon 		}
558e2eae099SYu Kuai 	}
559e86d1aa8SWill Deacon 
560e86d1aa8SWill Deacon 	return iommu_fwspec_add_ids(dev, &asid, 1);
561e86d1aa8SWill Deacon }
562e86d1aa8SWill Deacon 
563e86d1aa8SWill Deacon static const struct iommu_ops qcom_iommu_ops = {
564e86d1aa8SWill Deacon 	.capable	= qcom_iommu_capable,
565e86d1aa8SWill Deacon 	.domain_alloc	= qcom_iommu_domain_alloc,
5669a630a4bSLu Baolu 	.probe_device	= qcom_iommu_probe_device,
5679a630a4bSLu Baolu 	.device_group	= generic_device_group,
5689a630a4bSLu Baolu 	.of_xlate	= qcom_iommu_of_xlate,
5699a630a4bSLu Baolu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
5709a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
571e86d1aa8SWill Deacon 		.attach_dev	= qcom_iommu_attach_dev,
572fa8ce574SRobin Murphy 		.map_pages	= qcom_iommu_map,
573fa8ce574SRobin Murphy 		.unmap_pages	= qcom_iommu_unmap,
574e86d1aa8SWill Deacon 		.flush_iotlb_all = qcom_iommu_flush_iotlb_all,
575e86d1aa8SWill Deacon 		.iotlb_sync	= qcom_iommu_iotlb_sync,
576e86d1aa8SWill Deacon 		.iova_to_phys	= qcom_iommu_iova_to_phys,
5779a630a4bSLu Baolu 		.free		= qcom_iommu_domain_free,
5789a630a4bSLu Baolu 	}
579e86d1aa8SWill Deacon };
580e86d1aa8SWill Deacon 
581e86d1aa8SWill Deacon static int qcom_iommu_sec_ptbl_init(struct device *dev)
582e86d1aa8SWill Deacon {
583e86d1aa8SWill Deacon 	size_t psize = 0;
584e86d1aa8SWill Deacon 	unsigned int spare = 0;
585e86d1aa8SWill Deacon 	void *cpu_addr;
586e86d1aa8SWill Deacon 	dma_addr_t paddr;
587e86d1aa8SWill Deacon 	unsigned long attrs;
588e86d1aa8SWill Deacon 	static bool allocated = false;
589e86d1aa8SWill Deacon 	int ret;
590e86d1aa8SWill Deacon 
591e86d1aa8SWill Deacon 	if (allocated)
592e86d1aa8SWill Deacon 		return 0;
593e86d1aa8SWill Deacon 
594e86d1aa8SWill Deacon 	ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
595e86d1aa8SWill Deacon 	if (ret) {
596e86d1aa8SWill Deacon 		dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
597e86d1aa8SWill Deacon 			ret);
598e86d1aa8SWill Deacon 		return ret;
599e86d1aa8SWill Deacon 	}
600e86d1aa8SWill Deacon 
601e86d1aa8SWill Deacon 	dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
602e86d1aa8SWill Deacon 
603e86d1aa8SWill Deacon 	attrs = DMA_ATTR_NO_KERNEL_MAPPING;
604e86d1aa8SWill Deacon 
605e86d1aa8SWill Deacon 	cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
606e86d1aa8SWill Deacon 	if (!cpu_addr) {
607e86d1aa8SWill Deacon 		dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
608e86d1aa8SWill Deacon 			psize);
609e86d1aa8SWill Deacon 		return -ENOMEM;
610e86d1aa8SWill Deacon 	}
611e86d1aa8SWill Deacon 
612e86d1aa8SWill Deacon 	ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
613e86d1aa8SWill Deacon 	if (ret) {
614e86d1aa8SWill Deacon 		dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
615e86d1aa8SWill Deacon 		goto free_mem;
616e86d1aa8SWill Deacon 	}
617e86d1aa8SWill Deacon 
618e86d1aa8SWill Deacon 	allocated = true;
619e86d1aa8SWill Deacon 	return 0;
620e86d1aa8SWill Deacon 
621e86d1aa8SWill Deacon free_mem:
622e86d1aa8SWill Deacon 	dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
623e86d1aa8SWill Deacon 	return ret;
624e86d1aa8SWill Deacon }
625e86d1aa8SWill Deacon 
626e86d1aa8SWill Deacon static int get_asid(const struct device_node *np)
627e86d1aa8SWill Deacon {
628fcf226f1SAngeloGioacchino Del Regno 	u32 reg, val;
629fcf226f1SAngeloGioacchino Del Regno 	int asid;
630e86d1aa8SWill Deacon 
631e86d1aa8SWill Deacon 	/* read the "reg" property directly to get the relative address
632e86d1aa8SWill Deacon 	 * of the context bank, and calculate the asid from that:
633e86d1aa8SWill Deacon 	 */
634e86d1aa8SWill Deacon 	if (of_property_read_u32_index(np, "reg", 0, &reg))
635e86d1aa8SWill Deacon 		return -ENODEV;
636e86d1aa8SWill Deacon 
637fcf226f1SAngeloGioacchino Del Regno 	/*
638fcf226f1SAngeloGioacchino Del Regno 	 * Context banks are 0x1000 apart but, in some cases, the ASID
639fcf226f1SAngeloGioacchino Del Regno 	 * number doesn't match to this logic and needs to be passed
640fcf226f1SAngeloGioacchino Del Regno 	 * from the DT configuration explicitly.
641fcf226f1SAngeloGioacchino Del Regno 	 */
642fcf226f1SAngeloGioacchino Del Regno 	if (!of_property_read_u32(np, "qcom,ctx-asid", &val))
643fcf226f1SAngeloGioacchino Del Regno 		asid = val;
644fcf226f1SAngeloGioacchino Del Regno 	else
645fcf226f1SAngeloGioacchino Del Regno 		asid = reg / 0x1000;
646fcf226f1SAngeloGioacchino Del Regno 
647fcf226f1SAngeloGioacchino Del Regno 	return asid;
648e86d1aa8SWill Deacon }
649e86d1aa8SWill Deacon 
650e86d1aa8SWill Deacon static int qcom_iommu_ctx_probe(struct platform_device *pdev)
651e86d1aa8SWill Deacon {
652e86d1aa8SWill Deacon 	struct qcom_iommu_ctx *ctx;
653e86d1aa8SWill Deacon 	struct device *dev = &pdev->dev;
654e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
655e86d1aa8SWill Deacon 	int ret, irq;
656e86d1aa8SWill Deacon 
657e86d1aa8SWill Deacon 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
658e86d1aa8SWill Deacon 	if (!ctx)
659e86d1aa8SWill Deacon 		return -ENOMEM;
660e86d1aa8SWill Deacon 
661e86d1aa8SWill Deacon 	ctx->dev = dev;
662e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, ctx);
663e86d1aa8SWill Deacon 
6640a8c264dSYangtao Li 	ctx->base = devm_platform_ioremap_resource(pdev, 0);
665e86d1aa8SWill Deacon 	if (IS_ERR(ctx->base))
666e86d1aa8SWill Deacon 		return PTR_ERR(ctx->base);
667e86d1aa8SWill Deacon 
668e86d1aa8SWill Deacon 	irq = platform_get_irq(pdev, 0);
669e86d1aa8SWill Deacon 	if (irq < 0)
6700a8c264dSYangtao Li 		return irq;
671e86d1aa8SWill Deacon 
672e86d1aa8SWill Deacon 	/* clear IRQs before registering fault handler, just in case the
673e86d1aa8SWill Deacon 	 * boot-loader left us a surprise:
674e86d1aa8SWill Deacon 	 */
675e86d1aa8SWill Deacon 	iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
676e86d1aa8SWill Deacon 
677e86d1aa8SWill Deacon 	ret = devm_request_irq(dev, irq,
678e86d1aa8SWill Deacon 			       qcom_iommu_fault,
679e86d1aa8SWill Deacon 			       IRQF_SHARED,
680e86d1aa8SWill Deacon 			       "qcom-iommu-fault",
681e86d1aa8SWill Deacon 			       ctx);
682e86d1aa8SWill Deacon 	if (ret) {
683e86d1aa8SWill Deacon 		dev_err(dev, "failed to request IRQ %u\n", irq);
684e86d1aa8SWill Deacon 		return ret;
685e86d1aa8SWill Deacon 	}
686e86d1aa8SWill Deacon 
687e86d1aa8SWill Deacon 	ret = get_asid(dev->of_node);
688e86d1aa8SWill Deacon 	if (ret < 0) {
689e86d1aa8SWill Deacon 		dev_err(dev, "missing reg property\n");
690e86d1aa8SWill Deacon 		return ret;
691e86d1aa8SWill Deacon 	}
692e86d1aa8SWill Deacon 
693e86d1aa8SWill Deacon 	ctx->asid = ret;
694e86d1aa8SWill Deacon 
695e86d1aa8SWill Deacon 	dev_dbg(dev, "found asid %u\n", ctx->asid);
696e86d1aa8SWill Deacon 
697e86d1aa8SWill Deacon 	qcom_iommu->ctxs[ctx->asid - 1] = ctx;
698e86d1aa8SWill Deacon 
699e86d1aa8SWill Deacon 	return 0;
700e86d1aa8SWill Deacon }
701e86d1aa8SWill Deacon 
70262565a77SUwe Kleine-König static void qcom_iommu_ctx_remove(struct platform_device *pdev)
703e86d1aa8SWill Deacon {
704e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
705e86d1aa8SWill Deacon 	struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
706e86d1aa8SWill Deacon 
707e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, NULL);
708e86d1aa8SWill Deacon 
709e86d1aa8SWill Deacon 	qcom_iommu->ctxs[ctx->asid - 1] = NULL;
710e86d1aa8SWill Deacon }
711e86d1aa8SWill Deacon 
712e86d1aa8SWill Deacon static const struct of_device_id ctx_of_match[] = {
713e86d1aa8SWill Deacon 	{ .compatible = "qcom,msm-iommu-v1-ns" },
714e86d1aa8SWill Deacon 	{ .compatible = "qcom,msm-iommu-v1-sec" },
715e86d1aa8SWill Deacon 	{ /* sentinel */ }
716e86d1aa8SWill Deacon };
717e86d1aa8SWill Deacon 
718e86d1aa8SWill Deacon static struct platform_driver qcom_iommu_ctx_driver = {
719e86d1aa8SWill Deacon 	.driver	= {
720e86d1aa8SWill Deacon 		.name		= "qcom-iommu-ctx",
7217aaf0b0eSKrzysztof Kozlowski 		.of_match_table	= ctx_of_match,
722e86d1aa8SWill Deacon 	},
723e86d1aa8SWill Deacon 	.probe	= qcom_iommu_ctx_probe,
72462565a77SUwe Kleine-König 	.remove_new = qcom_iommu_ctx_remove,
725e86d1aa8SWill Deacon };
726e86d1aa8SWill Deacon 
727e86d1aa8SWill Deacon static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
728e86d1aa8SWill Deacon {
729e86d1aa8SWill Deacon 	struct device_node *child;
730e86d1aa8SWill Deacon 
731a91eb680SLiang He 	for_each_child_of_node(qcom_iommu->dev->of_node, child) {
732a91eb680SLiang He 		if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) {
733a91eb680SLiang He 			of_node_put(child);
734e86d1aa8SWill Deacon 			return true;
735a91eb680SLiang He 		}
736a91eb680SLiang He 	}
737e86d1aa8SWill Deacon 
738e86d1aa8SWill Deacon 	return false;
739e86d1aa8SWill Deacon }
740e86d1aa8SWill Deacon 
741e86d1aa8SWill Deacon static int qcom_iommu_device_probe(struct platform_device *pdev)
742e86d1aa8SWill Deacon {
743e86d1aa8SWill Deacon 	struct device_node *child;
744e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu;
745e86d1aa8SWill Deacon 	struct device *dev = &pdev->dev;
746e86d1aa8SWill Deacon 	struct resource *res;
747e46b3c0dSJoerg Roedel 	struct clk *clk;
748e86d1aa8SWill Deacon 	int ret, max_asid = 0;
749e86d1aa8SWill Deacon 
750e86d1aa8SWill Deacon 	/* find the max asid (which is 1:1 to ctx bank idx), so we know how
751e86d1aa8SWill Deacon 	 * many child ctx devices we have:
752e86d1aa8SWill Deacon 	 */
753e86d1aa8SWill Deacon 	for_each_child_of_node(dev->of_node, child)
754e86d1aa8SWill Deacon 		max_asid = max(max_asid, get_asid(child));
755e86d1aa8SWill Deacon 
756e86d1aa8SWill Deacon 	qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
757e86d1aa8SWill Deacon 				  GFP_KERNEL);
758e86d1aa8SWill Deacon 	if (!qcom_iommu)
759e86d1aa8SWill Deacon 		return -ENOMEM;
760e86d1aa8SWill Deacon 	qcom_iommu->num_ctxs = max_asid;
761e86d1aa8SWill Deacon 	qcom_iommu->dev = dev;
762e86d1aa8SWill Deacon 
763e86d1aa8SWill Deacon 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
764e86d1aa8SWill Deacon 	if (res) {
765e86d1aa8SWill Deacon 		qcom_iommu->local_base = devm_ioremap_resource(dev, res);
766e86d1aa8SWill Deacon 		if (IS_ERR(qcom_iommu->local_base))
767e86d1aa8SWill Deacon 			return PTR_ERR(qcom_iommu->local_base);
768e86d1aa8SWill Deacon 	}
769e86d1aa8SWill Deacon 
770e46b3c0dSJoerg Roedel 	clk = devm_clk_get(dev, "iface");
771e46b3c0dSJoerg Roedel 	if (IS_ERR(clk)) {
772e86d1aa8SWill Deacon 		dev_err(dev, "failed to get iface clock\n");
773e46b3c0dSJoerg Roedel 		return PTR_ERR(clk);
774e86d1aa8SWill Deacon 	}
775e46b3c0dSJoerg Roedel 	qcom_iommu->clks[CLK_IFACE].clk = clk;
776e86d1aa8SWill Deacon 
777e46b3c0dSJoerg Roedel 	clk = devm_clk_get(dev, "bus");
778e46b3c0dSJoerg Roedel 	if (IS_ERR(clk)) {
779e86d1aa8SWill Deacon 		dev_err(dev, "failed to get bus clock\n");
780e46b3c0dSJoerg Roedel 		return PTR_ERR(clk);
781e86d1aa8SWill Deacon 	}
782e46b3c0dSJoerg Roedel 	qcom_iommu->clks[CLK_BUS].clk = clk;
783e46b3c0dSJoerg Roedel 
784e46b3c0dSJoerg Roedel 	clk = devm_clk_get_optional(dev, "tbu");
785e46b3c0dSJoerg Roedel 	if (IS_ERR(clk)) {
786e46b3c0dSJoerg Roedel 		dev_err(dev, "failed to get tbu clock\n");
787e46b3c0dSJoerg Roedel 		return PTR_ERR(clk);
788e46b3c0dSJoerg Roedel 	}
789e46b3c0dSJoerg Roedel 	qcom_iommu->clks[CLK_TBU].clk = clk;
790e86d1aa8SWill Deacon 
791e86d1aa8SWill Deacon 	if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
792e86d1aa8SWill Deacon 				 &qcom_iommu->sec_id)) {
793e86d1aa8SWill Deacon 		dev_err(dev, "missing qcom,iommu-secure-id property\n");
794e86d1aa8SWill Deacon 		return -ENODEV;
795e86d1aa8SWill Deacon 	}
796e86d1aa8SWill Deacon 
797e86d1aa8SWill Deacon 	if (qcom_iommu_has_secure_context(qcom_iommu)) {
798e86d1aa8SWill Deacon 		ret = qcom_iommu_sec_ptbl_init(dev);
799e86d1aa8SWill Deacon 		if (ret) {
800e86d1aa8SWill Deacon 			dev_err(dev, "cannot init secure pg table(%d)\n", ret);
801e86d1aa8SWill Deacon 			return ret;
802e86d1aa8SWill Deacon 		}
803e86d1aa8SWill Deacon 	}
804e86d1aa8SWill Deacon 
805e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, qcom_iommu);
806e86d1aa8SWill Deacon 
807e86d1aa8SWill Deacon 	pm_runtime_enable(dev);
808e86d1aa8SWill Deacon 
809e86d1aa8SWill Deacon 	/* register context bank devices, which are child nodes: */
810e86d1aa8SWill Deacon 	ret = devm_of_platform_populate(dev);
811e86d1aa8SWill Deacon 	if (ret) {
812e86d1aa8SWill Deacon 		dev_err(dev, "Failed to populate iommu contexts\n");
81393665e02SMiaoqian Lin 		goto err_pm_disable;
814e86d1aa8SWill Deacon 	}
815e86d1aa8SWill Deacon 
816e86d1aa8SWill Deacon 	ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
817e86d1aa8SWill Deacon 				     dev_name(dev));
818e86d1aa8SWill Deacon 	if (ret) {
819e86d1aa8SWill Deacon 		dev_err(dev, "Failed to register iommu in sysfs\n");
82093665e02SMiaoqian Lin 		goto err_pm_disable;
821e86d1aa8SWill Deacon 	}
822e86d1aa8SWill Deacon 
8232d471b20SRobin Murphy 	ret = iommu_device_register(&qcom_iommu->iommu, &qcom_iommu_ops, dev);
824e86d1aa8SWill Deacon 	if (ret) {
825e86d1aa8SWill Deacon 		dev_err(dev, "Failed to register iommu\n");
82693665e02SMiaoqian Lin 		goto err_pm_disable;
827e86d1aa8SWill Deacon 	}
828e86d1aa8SWill Deacon 
829e86d1aa8SWill Deacon 	if (qcom_iommu->local_base) {
830e86d1aa8SWill Deacon 		pm_runtime_get_sync(dev);
831e86d1aa8SWill Deacon 		writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
832e86d1aa8SWill Deacon 		pm_runtime_put_sync(dev);
833e86d1aa8SWill Deacon 	}
834e86d1aa8SWill Deacon 
835e86d1aa8SWill Deacon 	return 0;
83693665e02SMiaoqian Lin 
83793665e02SMiaoqian Lin err_pm_disable:
83893665e02SMiaoqian Lin 	pm_runtime_disable(dev);
83993665e02SMiaoqian Lin 	return ret;
840e86d1aa8SWill Deacon }
841e86d1aa8SWill Deacon 
84262565a77SUwe Kleine-König static void qcom_iommu_device_remove(struct platform_device *pdev)
843e86d1aa8SWill Deacon {
844e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
845e86d1aa8SWill Deacon 
846e86d1aa8SWill Deacon 	pm_runtime_force_suspend(&pdev->dev);
847e86d1aa8SWill Deacon 	platform_set_drvdata(pdev, NULL);
848e86d1aa8SWill Deacon 	iommu_device_sysfs_remove(&qcom_iommu->iommu);
849e86d1aa8SWill Deacon 	iommu_device_unregister(&qcom_iommu->iommu);
850e86d1aa8SWill Deacon }
851e86d1aa8SWill Deacon 
852e86d1aa8SWill Deacon static int __maybe_unused qcom_iommu_resume(struct device *dev)
853e86d1aa8SWill Deacon {
854e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
855e86d1aa8SWill Deacon 
856e46b3c0dSJoerg Roedel 	return clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks);
857e86d1aa8SWill Deacon }
858e86d1aa8SWill Deacon 
859e86d1aa8SWill Deacon static int __maybe_unused qcom_iommu_suspend(struct device *dev)
860e86d1aa8SWill Deacon {
861e86d1aa8SWill Deacon 	struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
862e86d1aa8SWill Deacon 
863e46b3c0dSJoerg Roedel 	clk_bulk_disable_unprepare(CLK_NUM, qcom_iommu->clks);
864e86d1aa8SWill Deacon 
865e86d1aa8SWill Deacon 	return 0;
866e86d1aa8SWill Deacon }
867e86d1aa8SWill Deacon 
868e86d1aa8SWill Deacon static const struct dev_pm_ops qcom_iommu_pm_ops = {
869e86d1aa8SWill Deacon 	SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
870e86d1aa8SWill Deacon 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
871e86d1aa8SWill Deacon 				pm_runtime_force_resume)
872e86d1aa8SWill Deacon };
873e86d1aa8SWill Deacon 
874e86d1aa8SWill Deacon static const struct of_device_id qcom_iommu_of_match[] = {
875e86d1aa8SWill Deacon 	{ .compatible = "qcom,msm-iommu-v1" },
876e86d1aa8SWill Deacon 	{ /* sentinel */ }
877e86d1aa8SWill Deacon };
878e86d1aa8SWill Deacon 
879e86d1aa8SWill Deacon static struct platform_driver qcom_iommu_driver = {
880e86d1aa8SWill Deacon 	.driver	= {
881e86d1aa8SWill Deacon 		.name		= "qcom-iommu",
8827aaf0b0eSKrzysztof Kozlowski 		.of_match_table	= qcom_iommu_of_match,
883e86d1aa8SWill Deacon 		.pm		= &qcom_iommu_pm_ops,
884e86d1aa8SWill Deacon 	},
885e86d1aa8SWill Deacon 	.probe	= qcom_iommu_device_probe,
88662565a77SUwe Kleine-König 	.remove_new = qcom_iommu_device_remove,
887e86d1aa8SWill Deacon };
888e86d1aa8SWill Deacon 
889e86d1aa8SWill Deacon static int __init qcom_iommu_init(void)
890e86d1aa8SWill Deacon {
891e86d1aa8SWill Deacon 	int ret;
892e86d1aa8SWill Deacon 
893e86d1aa8SWill Deacon 	ret = platform_driver_register(&qcom_iommu_ctx_driver);
894e86d1aa8SWill Deacon 	if (ret)
895e86d1aa8SWill Deacon 		return ret;
896e86d1aa8SWill Deacon 
897e86d1aa8SWill Deacon 	ret = platform_driver_register(&qcom_iommu_driver);
898e86d1aa8SWill Deacon 	if (ret)
899e86d1aa8SWill Deacon 		platform_driver_unregister(&qcom_iommu_ctx_driver);
900e86d1aa8SWill Deacon 
901e86d1aa8SWill Deacon 	return ret;
902e86d1aa8SWill Deacon }
903e86d1aa8SWill Deacon device_initcall(qcom_iommu_init);
904