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