blob: f6117726da992eb94e2ab964d8064780aad4d7ec [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clark0ae349a2017-08-09 10:43:04 -04002/*
3 * IOMMU API for QCOM secure IOMMUs. Somewhat based on arm-smmu.c
4 *
Rob Clark0ae349a2017-08-09 10:43:04 -04005 * Copyright (C) 2013 ARM Limited
6 * Copyright (C) 2017 Red Hat
7 */
8
9#include <linux/atomic.h>
Robin Murphy620565a2019-08-15 19:37:25 +010010#include <linux/bitfield.h>
Rob Clark0ae349a2017-08-09 10:43:04 -040011#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/dma-iommu.h>
14#include <linux/dma-mapping.h>
15#include <linux/err.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/io-64-nonatomic-hi-lo.h>
Rob Herringb77cf112019-02-05 10:37:31 -060019#include <linux/io-pgtable.h>
Rob Clark0ae349a2017-08-09 10:43:04 -040020#include <linux/iommu.h>
21#include <linux/iopoll.h>
22#include <linux/kconfig.h>
Paul Gortmakerf295cf22018-12-01 14:19:14 -050023#include <linux/init.h>
Rob Clark0ae349a2017-08-09 10:43:04 -040024#include <linux/mutex.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_device.h>
28#include <linux/of_iommu.h>
29#include <linux/platform_device.h>
30#include <linux/pm.h>
31#include <linux/pm_runtime.h>
32#include <linux/qcom_scm.h>
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35
Robin Murphyc5fc6482019-08-15 19:37:32 +010036#include "arm-smmu.h"
Rob Clark0ae349a2017-08-09 10:43:04 -040037
38#define SMMU_INTR_SEL_NS 0x2000
39
40struct qcom_iommu_ctx;
41
42struct qcom_iommu_dev {
43 /* IOMMU core code handle */
44 struct iommu_device iommu;
45 struct device *dev;
46 struct clk *iface_clk;
47 struct clk *bus_clk;
48 void __iomem *local_base;
49 u32 sec_id;
50 u8 num_ctxs;
Gustavo A. R. Silvae26060e2020-02-12 18:14:01 -060051 struct qcom_iommu_ctx *ctxs[]; /* indexed by asid-1 */
Rob Clark0ae349a2017-08-09 10:43:04 -040052};
53
54struct qcom_iommu_ctx {
55 struct device *dev;
56 void __iomem *base;
57 bool secure_init;
58 u8 asid; /* asid and ctx bank # are 1:1 */
Rob Clark049541e2017-11-03 10:50:33 -060059 struct iommu_domain *domain;
Rob Clark0ae349a2017-08-09 10:43:04 -040060};
61
62struct qcom_iommu_domain {
63 struct io_pgtable_ops *pgtbl_ops;
64 spinlock_t pgtbl_lock;
65 struct mutex init_mutex; /* Protects iommu pointer */
66 struct iommu_domain domain;
67 struct qcom_iommu_dev *iommu;
68};
69
70static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
71{
72 return container_of(dom, struct qcom_iommu_domain, domain);
73}
74
75static const struct iommu_ops qcom_iommu_ops;
76
77static struct qcom_iommu_dev * to_iommu(struct iommu_fwspec *fwspec)
78{
79 if (!fwspec || fwspec->ops != &qcom_iommu_ops)
80 return NULL;
81 return fwspec->iommu_priv;
82}
83
84static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid)
85{
86 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
87 if (!qcom_iommu)
88 return NULL;
89 return qcom_iommu->ctxs[asid - 1];
90}
91
92static inline void
93iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
94{
95 writel_relaxed(val, ctx->base + reg);
96}
97
98static inline void
99iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
100{
101 writeq_relaxed(val, ctx->base + reg);
102}
103
104static inline u32
105iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
106{
107 return readl_relaxed(ctx->base + reg);
108}
109
110static inline u64
111iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
112{
113 return readq_relaxed(ctx->base + reg);
114}
115
116static void qcom_iommu_tlb_sync(void *cookie)
117{
118 struct iommu_fwspec *fwspec = cookie;
119 unsigned i;
120
121 for (i = 0; i < fwspec->num_ids; i++) {
122 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
123 unsigned int val, ret;
124
125 iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
126
127 ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
128 (val & 0x1) == 0, 0, 5000000);
129 if (ret)
130 dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
131 }
132}
133
134static void qcom_iommu_tlb_inv_context(void *cookie)
135{
136 struct iommu_fwspec *fwspec = cookie;
137 unsigned i;
138
139 for (i = 0; i < fwspec->num_ids; i++) {
140 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
141 iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
142 }
143
144 qcom_iommu_tlb_sync(cookie);
145}
146
147static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
148 size_t granule, bool leaf, void *cookie)
149{
150 struct iommu_fwspec *fwspec = cookie;
151 unsigned i, reg;
152
153 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
154
155 for (i = 0; i < fwspec->num_ids; i++) {
156 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
157 size_t s = size;
158
Robin Murphya5b396c2019-08-15 19:37:22 +0100159 iova = (iova >> 12) << 12;
Rob Clark0ae349a2017-08-09 10:43:04 -0400160 iova |= ctx->asid;
161 do {
162 iommu_writel(ctx, reg, iova);
163 iova += granule;
164 } while (s -= granule);
165 }
166}
167
Will Deacon05aed942019-07-02 16:44:25 +0100168static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
169 size_t granule, void *cookie)
170{
171 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
172 qcom_iommu_tlb_sync(cookie);
173}
174
175static void qcom_iommu_tlb_flush_leaf(unsigned long iova, size_t size,
176 size_t granule, void *cookie)
177{
178 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, true, cookie);
179 qcom_iommu_tlb_sync(cookie);
180}
181
Will Deacon3951c412019-07-02 16:45:15 +0100182static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
183 unsigned long iova, size_t granule,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100184 void *cookie)
185{
186 qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
187}
188
Will Deacon298f78892019-07-02 16:43:34 +0100189static const struct iommu_flush_ops qcom_flush_ops = {
Rob Clark0ae349a2017-08-09 10:43:04 -0400190 .tlb_flush_all = qcom_iommu_tlb_inv_context,
Will Deacon05aed942019-07-02 16:44:25 +0100191 .tlb_flush_walk = qcom_iommu_tlb_flush_walk,
192 .tlb_flush_leaf = qcom_iommu_tlb_flush_leaf,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100193 .tlb_add_page = qcom_iommu_tlb_add_page,
Rob Clark0ae349a2017-08-09 10:43:04 -0400194};
195
196static irqreturn_t qcom_iommu_fault(int irq, void *dev)
197{
198 struct qcom_iommu_ctx *ctx = dev;
199 u32 fsr, fsynr;
200 u64 iova;
201
202 fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
203
Will Deaconfba6e962020-01-10 13:20:03 +0000204 if (!(fsr & ARM_SMMU_FSR_FAULT))
Rob Clark0ae349a2017-08-09 10:43:04 -0400205 return IRQ_NONE;
206
207 fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
208 iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
209
Rob Clark049541e2017-11-03 10:50:33 -0600210 if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
211 dev_err_ratelimited(ctx->dev,
212 "Unhandled context fault: fsr=0x%x, "
213 "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
214 fsr, iova, fsynr, ctx->asid);
215 }
Rob Clark0ae349a2017-08-09 10:43:04 -0400216
217 iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
Will Deaconfba6e962020-01-10 13:20:03 +0000218 iommu_writel(ctx, ARM_SMMU_CB_RESUME, ARM_SMMU_RESUME_TERMINATE);
Rob Clark0ae349a2017-08-09 10:43:04 -0400219
220 return IRQ_HANDLED;
221}
222
223static int qcom_iommu_init_domain(struct iommu_domain *domain,
224 struct qcom_iommu_dev *qcom_iommu,
225 struct iommu_fwspec *fwspec)
226{
227 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
228 struct io_pgtable_ops *pgtbl_ops;
229 struct io_pgtable_cfg pgtbl_cfg;
230 int i, ret = 0;
231 u32 reg;
232
233 mutex_lock(&qcom_domain->init_mutex);
234 if (qcom_domain->iommu)
235 goto out_unlock;
236
237 pgtbl_cfg = (struct io_pgtable_cfg) {
238 .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
239 .ias = 32,
240 .oas = 40,
Will Deacon298f78892019-07-02 16:43:34 +0100241 .tlb = &qcom_flush_ops,
Rob Clark0ae349a2017-08-09 10:43:04 -0400242 .iommu_dev = qcom_iommu->dev,
243 };
244
245 qcom_domain->iommu = qcom_iommu;
246 pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec);
247 if (!pgtbl_ops) {
248 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
249 ret = -ENOMEM;
250 goto out_clear_iommu;
251 }
252
253 /* Update the domain's page sizes to reflect the page table format */
254 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
255 domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
256 domain->geometry.force_aperture = true;
257
258 for (i = 0; i < fwspec->num_ids; i++) {
259 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
260
261 if (!ctx->secure_init) {
262 ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
263 if (ret) {
264 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
265 goto out_clear_iommu;
266 }
267 ctx->secure_init = true;
268 }
269
270 /* TTBRs */
271 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
Robin Murphyd1e5f262019-10-25 19:08:37 +0100272 pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
Will Deaconfba6e962020-01-10 13:20:03 +0000273 FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
Robin Murphyfb485eb2019-10-25 19:08:38 +0100274 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
Rob Clark0ae349a2017-08-09 10:43:04 -0400275
Robin Murphy620565a2019-08-15 19:37:25 +0100276 /* TCR */
277 iommu_writel(ctx, ARM_SMMU_CB_TCR2,
Robin Murphyfb485eb2019-10-25 19:08:38 +0100278 arm_smmu_lpae_tcr2(&pgtbl_cfg));
Robin Murphy620565a2019-08-15 19:37:25 +0100279 iommu_writel(ctx, ARM_SMMU_CB_TCR,
Will Deaconfba6e962020-01-10 13:20:03 +0000280 arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
Rob Clark0ae349a2017-08-09 10:43:04 -0400281
282 /* MAIRs (stage-1 only) */
283 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
Robin Murphy205577a2019-10-25 19:08:36 +0100284 pgtbl_cfg.arm_lpae_s1_cfg.mair);
Rob Clark0ae349a2017-08-09 10:43:04 -0400285 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
Robin Murphy205577a2019-10-25 19:08:36 +0100286 pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
Rob Clark0ae349a2017-08-09 10:43:04 -0400287
288 /* SCTLR */
Will Deaconfba6e962020-01-10 13:20:03 +0000289 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
290 ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
291 ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
292 ARM_SMMU_SCTLR_CFCFG;
Rob Clark0ae349a2017-08-09 10:43:04 -0400293
294 if (IS_ENABLED(CONFIG_BIG_ENDIAN))
Will Deaconfba6e962020-01-10 13:20:03 +0000295 reg |= ARM_SMMU_SCTLR_E;
Rob Clark0ae349a2017-08-09 10:43:04 -0400296
297 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
Rob Clark049541e2017-11-03 10:50:33 -0600298
299 ctx->domain = domain;
Rob Clark0ae349a2017-08-09 10:43:04 -0400300 }
301
302 mutex_unlock(&qcom_domain->init_mutex);
303
304 /* Publish page table ops for map/unmap */
305 qcom_domain->pgtbl_ops = pgtbl_ops;
306
307 return 0;
308
309out_clear_iommu:
310 qcom_domain->iommu = NULL;
311out_unlock:
312 mutex_unlock(&qcom_domain->init_mutex);
313 return ret;
314}
315
316static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
317{
318 struct qcom_iommu_domain *qcom_domain;
319
320 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
321 return NULL;
322 /*
323 * Allocate the domain and initialise some of its data structures.
324 * We can't really do anything meaningful until we've added a
325 * master.
326 */
327 qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
328 if (!qcom_domain)
329 return NULL;
330
331 if (type == IOMMU_DOMAIN_DMA &&
332 iommu_get_dma_cookie(&qcom_domain->domain)) {
333 kfree(qcom_domain);
334 return NULL;
335 }
336
337 mutex_init(&qcom_domain->init_mutex);
338 spin_lock_init(&qcom_domain->pgtbl_lock);
339
340 return &qcom_domain->domain;
341}
342
343static void qcom_iommu_domain_free(struct iommu_domain *domain)
344{
345 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
346
Rob Clark0ae349a2017-08-09 10:43:04 -0400347 iommu_put_dma_cookie(domain);
348
Robin Murphyfaf305c2020-02-18 18:12:41 +0000349 if (qcom_domain->iommu) {
350 /*
351 * NOTE: unmap can be called after client device is powered
352 * off, for example, with GPUs or anything involving dma-buf.
353 * So we cannot rely on the device_link. Make sure the IOMMU
354 * is on to avoid unclocked accesses in the TLB inv path:
355 */
356 pm_runtime_get_sync(qcom_domain->iommu->dev);
357 free_io_pgtable_ops(qcom_domain->pgtbl_ops);
358 pm_runtime_put_sync(qcom_domain->iommu->dev);
359 }
Rob Clark0ae349a2017-08-09 10:43:04 -0400360
361 kfree(qcom_domain);
362}
363
364static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
365{
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100366 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
367 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
Rob Clark0ae349a2017-08-09 10:43:04 -0400368 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
369 int ret;
370
371 if (!qcom_iommu) {
372 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
373 return -ENXIO;
374 }
375
376 /* Ensure that the domain is finalized */
377 pm_runtime_get_sync(qcom_iommu->dev);
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100378 ret = qcom_iommu_init_domain(domain, qcom_iommu, fwspec);
Rob Clark0ae349a2017-08-09 10:43:04 -0400379 pm_runtime_put_sync(qcom_iommu->dev);
380 if (ret < 0)
381 return ret;
382
383 /*
384 * Sanity check the domain. We don't support domains across
385 * different IOMMUs.
386 */
387 if (qcom_domain->iommu != qcom_iommu) {
388 dev_err(dev, "cannot attach to IOMMU %s while already "
389 "attached to domain on IOMMU %s\n",
390 dev_name(qcom_domain->iommu->dev),
391 dev_name(qcom_iommu->dev));
392 return -EINVAL;
393 }
394
395 return 0;
396}
397
398static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
399{
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100400 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Rob Clark0ae349a2017-08-09 10:43:04 -0400401 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
402 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
403 unsigned i;
404
Robin Murphyfaf305c2020-02-18 18:12:41 +0000405 if (WARN_ON(!qcom_domain->iommu))
Rob Clark0ae349a2017-08-09 10:43:04 -0400406 return;
407
408 pm_runtime_get_sync(qcom_iommu->dev);
409 for (i = 0; i < fwspec->num_ids; i++) {
410 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
411
412 /* Disable the context bank: */
413 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
Rob Clark049541e2017-11-03 10:50:33 -0600414
415 ctx->domain = NULL;
Rob Clark0ae349a2017-08-09 10:43:04 -0400416 }
417 pm_runtime_put_sync(qcom_iommu->dev);
Rob Clark0ae349a2017-08-09 10:43:04 -0400418}
419
420static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
Tom Murphy781ca2d2019-09-08 09:56:38 -0700421 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Rob Clark0ae349a2017-08-09 10:43:04 -0400422{
423 int ret;
424 unsigned long flags;
425 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
426 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
427
428 if (!ops)
429 return -ENODEV;
430
431 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
432 ret = ops->map(ops, iova, paddr, size, prot);
433 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
434 return ret;
435}
436
437static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
Will Deacon56f8af52019-07-02 16:44:06 +0100438 size_t size, struct iommu_iotlb_gather *gather)
Rob Clark0ae349a2017-08-09 10:43:04 -0400439{
440 size_t ret;
441 unsigned long flags;
442 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
443 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
444
445 if (!ops)
446 return 0;
447
448 /* NOTE: unmap can be called after client device is powered off,
449 * for example, with GPUs or anything involving dma-buf. So we
450 * cannot rely on the device_link. Make sure the IOMMU is on to
451 * avoid unclocked accesses in the TLB inv path:
452 */
453 pm_runtime_get_sync(qcom_domain->iommu->dev);
454 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
Will Deacona2d3a382019-07-02 16:44:58 +0100455 ret = ops->unmap(ops, iova, size, gather);
Rob Clark0ae349a2017-08-09 10:43:04 -0400456 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
457 pm_runtime_put_sync(qcom_domain->iommu->dev);
458
459 return ret;
460}
461
Will Deacon56f8af52019-07-02 16:44:06 +0100462static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
Robin Murphy4d689b62017-09-28 15:55:02 +0100463{
464 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
465 struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
466 struct io_pgtable, ops);
467 if (!qcom_domain->pgtbl_ops)
468 return;
469
470 pm_runtime_get_sync(qcom_domain->iommu->dev);
471 qcom_iommu_tlb_sync(pgtable->cookie);
472 pm_runtime_put_sync(qcom_domain->iommu->dev);
473}
474
Will Deacon56f8af52019-07-02 16:44:06 +0100475static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
476 struct iommu_iotlb_gather *gather)
477{
478 qcom_iommu_flush_iotlb_all(domain);
479}
480
Rob Clark0ae349a2017-08-09 10:43:04 -0400481static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
482 dma_addr_t iova)
483{
484 phys_addr_t ret;
485 unsigned long flags;
486 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
487 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
488
489 if (!ops)
490 return 0;
491
492 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
493 ret = ops->iova_to_phys(ops, iova);
494 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
495
496 return ret;
497}
498
499static bool qcom_iommu_capable(enum iommu_cap cap)
500{
501 switch (cap) {
502 case IOMMU_CAP_CACHE_COHERENCY:
503 /*
504 * Return true here as the SMMU can always send out coherent
505 * requests.
506 */
507 return true;
508 case IOMMU_CAP_NOEXEC:
509 return true;
510 default:
511 return false;
512 }
513}
514
515static int qcom_iommu_add_device(struct device *dev)
516{
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100517 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
Rob Clark0ae349a2017-08-09 10:43:04 -0400518 struct iommu_group *group;
519 struct device_link *link;
520
521 if (!qcom_iommu)
522 return -ENODEV;
523
524 /*
525 * Establish the link between iommu and master, so that the
526 * iommu gets runtime enabled/disabled as per the master's
527 * needs.
528 */
529 link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
530 if (!link) {
531 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
532 dev_name(qcom_iommu->dev), dev_name(dev));
533 return -ENODEV;
534 }
535
536 group = iommu_group_get_for_dev(dev);
Christophe JAILLETda6b05d2019-09-16 22:29:36 +0200537 if (IS_ERR(group))
538 return PTR_ERR(group);
Rob Clark0ae349a2017-08-09 10:43:04 -0400539
540 iommu_group_put(group);
541 iommu_device_link(&qcom_iommu->iommu, dev);
542
543 return 0;
544}
545
546static void qcom_iommu_remove_device(struct device *dev)
547{
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100548 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
Rob Clark0ae349a2017-08-09 10:43:04 -0400549
550 if (!qcom_iommu)
551 return;
552
553 iommu_device_unlink(&qcom_iommu->iommu, dev);
554 iommu_group_remove_device(dev);
555 iommu_fwspec_free(dev);
556}
557
558static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
559{
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100560 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Rob Clark0ae349a2017-08-09 10:43:04 -0400561 struct qcom_iommu_dev *qcom_iommu;
562 struct platform_device *iommu_pdev;
563 unsigned asid = args->args[0];
564
565 if (args->args_count != 1) {
566 dev_err(dev, "incorrect number of iommu params found for %s "
567 "(found %d, expected 1)\n",
568 args->np->full_name, args->args_count);
569 return -EINVAL;
570 }
571
572 iommu_pdev = of_find_device_by_node(args->np);
573 if (WARN_ON(!iommu_pdev))
574 return -EINVAL;
575
576 qcom_iommu = platform_get_drvdata(iommu_pdev);
577
578 /* make sure the asid specified in dt is valid, so we don't have
579 * to sanity check this elsewhere, since 'asid - 1' is used to
580 * index into qcom_iommu->ctxs:
581 */
582 if (WARN_ON(asid < 1) ||
583 WARN_ON(asid > qcom_iommu->num_ctxs))
584 return -EINVAL;
585
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100586 if (!fwspec->iommu_priv) {
587 fwspec->iommu_priv = qcom_iommu;
Rob Clark0ae349a2017-08-09 10:43:04 -0400588 } else {
589 /* make sure devices iommus dt node isn't referring to
590 * multiple different iommu devices. Multiple context
591 * banks are ok, but multiple devices are not:
592 */
Joerg Roedel2000e5f2018-11-29 14:01:00 +0100593 if (WARN_ON(qcom_iommu != fwspec->iommu_priv))
Rob Clark0ae349a2017-08-09 10:43:04 -0400594 return -EINVAL;
595 }
596
597 return iommu_fwspec_add_ids(dev, &asid, 1);
598}
599
600static const struct iommu_ops qcom_iommu_ops = {
601 .capable = qcom_iommu_capable,
602 .domain_alloc = qcom_iommu_domain_alloc,
603 .domain_free = qcom_iommu_domain_free,
604 .attach_dev = qcom_iommu_attach_dev,
605 .detach_dev = qcom_iommu_detach_dev,
606 .map = qcom_iommu_map,
607 .unmap = qcom_iommu_unmap,
Will Deacon56f8af52019-07-02 16:44:06 +0100608 .flush_iotlb_all = qcom_iommu_flush_iotlb_all,
Robin Murphy4d689b62017-09-28 15:55:02 +0100609 .iotlb_sync = qcom_iommu_iotlb_sync,
Rob Clark0ae349a2017-08-09 10:43:04 -0400610 .iova_to_phys = qcom_iommu_iova_to_phys,
611 .add_device = qcom_iommu_add_device,
612 .remove_device = qcom_iommu_remove_device,
613 .device_group = generic_device_group,
614 .of_xlate = qcom_iommu_of_xlate,
615 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
616};
617
618static int qcom_iommu_enable_clocks(struct qcom_iommu_dev *qcom_iommu)
619{
620 int ret;
621
622 ret = clk_prepare_enable(qcom_iommu->iface_clk);
623 if (ret) {
624 dev_err(qcom_iommu->dev, "Couldn't enable iface_clk\n");
625 return ret;
626 }
627
628 ret = clk_prepare_enable(qcom_iommu->bus_clk);
629 if (ret) {
630 dev_err(qcom_iommu->dev, "Couldn't enable bus_clk\n");
631 clk_disable_unprepare(qcom_iommu->iface_clk);
632 return ret;
633 }
634
635 return 0;
636}
637
638static void qcom_iommu_disable_clocks(struct qcom_iommu_dev *qcom_iommu)
639{
640 clk_disable_unprepare(qcom_iommu->bus_clk);
641 clk_disable_unprepare(qcom_iommu->iface_clk);
642}
643
Stanimir Varbanovd051f282017-08-09 10:43:05 -0400644static int qcom_iommu_sec_ptbl_init(struct device *dev)
645{
646 size_t psize = 0;
647 unsigned int spare = 0;
648 void *cpu_addr;
649 dma_addr_t paddr;
650 unsigned long attrs;
651 static bool allocated = false;
652 int ret;
653
654 if (allocated)
655 return 0;
656
657 ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
658 if (ret) {
659 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
660 ret);
661 return ret;
662 }
663
664 dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
665
666 attrs = DMA_ATTR_NO_KERNEL_MAPPING;
667
668 cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
669 if (!cpu_addr) {
670 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
671 psize);
672 return -ENOMEM;
673 }
674
675 ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
676 if (ret) {
677 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
678 goto free_mem;
679 }
680
681 allocated = true;
682 return 0;
683
684free_mem:
685 dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
686 return ret;
687}
688
Rob Clark0ae349a2017-08-09 10:43:04 -0400689static int get_asid(const struct device_node *np)
690{
691 u32 reg;
692
693 /* read the "reg" property directly to get the relative address
694 * of the context bank, and calculate the asid from that:
695 */
696 if (of_property_read_u32_index(np, "reg", 0, &reg))
697 return -ENODEV;
698
699 return reg / 0x1000; /* context banks are 0x1000 apart */
700}
701
702static int qcom_iommu_ctx_probe(struct platform_device *pdev)
703{
704 struct qcom_iommu_ctx *ctx;
705 struct device *dev = &pdev->dev;
706 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
707 struct resource *res;
708 int ret, irq;
709
710 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
711 if (!ctx)
712 return -ENOMEM;
713
714 ctx->dev = dev;
715 platform_set_drvdata(pdev, ctx);
716
717 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
718 ctx->base = devm_ioremap_resource(dev, res);
719 if (IS_ERR(ctx->base))
720 return PTR_ERR(ctx->base);
721
722 irq = platform_get_irq(pdev, 0);
Stephen Boyd086f9efa2019-07-30 11:15:22 -0700723 if (irq < 0)
Rob Clark0ae349a2017-08-09 10:43:04 -0400724 return -ENODEV;
Rob Clark0ae349a2017-08-09 10:43:04 -0400725
726 /* clear IRQs before registering fault handler, just in case the
727 * boot-loader left us a surprise:
728 */
729 iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
730
731 ret = devm_request_irq(dev, irq,
732 qcom_iommu_fault,
733 IRQF_SHARED,
734 "qcom-iommu-fault",
735 ctx);
736 if (ret) {
737 dev_err(dev, "failed to request IRQ %u\n", irq);
738 return ret;
739 }
740
741 ret = get_asid(dev->of_node);
742 if (ret < 0) {
743 dev_err(dev, "missing reg property\n");
744 return ret;
745 }
746
747 ctx->asid = ret;
748
749 dev_dbg(dev, "found asid %u\n", ctx->asid);
750
751 qcom_iommu->ctxs[ctx->asid - 1] = ctx;
752
753 return 0;
754}
755
756static int qcom_iommu_ctx_remove(struct platform_device *pdev)
757{
758 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
759 struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
760
761 platform_set_drvdata(pdev, NULL);
762
763 qcom_iommu->ctxs[ctx->asid - 1] = NULL;
764
765 return 0;
766}
767
768static const struct of_device_id ctx_of_match[] = {
769 { .compatible = "qcom,msm-iommu-v1-ns" },
770 { .compatible = "qcom,msm-iommu-v1-sec" },
771 { /* sentinel */ }
772};
773
774static struct platform_driver qcom_iommu_ctx_driver = {
775 .driver = {
776 .name = "qcom-iommu-ctx",
777 .of_match_table = of_match_ptr(ctx_of_match),
778 },
779 .probe = qcom_iommu_ctx_probe,
780 .remove = qcom_iommu_ctx_remove,
781};
782
Stanimir Varbanovd051f282017-08-09 10:43:05 -0400783static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
784{
785 struct device_node *child;
786
787 for_each_child_of_node(qcom_iommu->dev->of_node, child)
788 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec"))
789 return true;
790
791 return false;
792}
793
Rob Clark0ae349a2017-08-09 10:43:04 -0400794static int qcom_iommu_device_probe(struct platform_device *pdev)
795{
796 struct device_node *child;
797 struct qcom_iommu_dev *qcom_iommu;
798 struct device *dev = &pdev->dev;
799 struct resource *res;
Gustavo A. R. Silva87585532019-08-29 23:03:27 -0500800 int ret, max_asid = 0;
Rob Clark0ae349a2017-08-09 10:43:04 -0400801
802 /* find the max asid (which is 1:1 to ctx bank idx), so we know how
803 * many child ctx devices we have:
804 */
805 for_each_child_of_node(dev->of_node, child)
806 max_asid = max(max_asid, get_asid(child));
807
Gustavo A. R. Silva87585532019-08-29 23:03:27 -0500808 qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
809 GFP_KERNEL);
Rob Clark0ae349a2017-08-09 10:43:04 -0400810 if (!qcom_iommu)
811 return -ENOMEM;
812 qcom_iommu->num_ctxs = max_asid;
813 qcom_iommu->dev = dev;
814
815 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
816 if (res)
817 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
818
819 qcom_iommu->iface_clk = devm_clk_get(dev, "iface");
820 if (IS_ERR(qcom_iommu->iface_clk)) {
821 dev_err(dev, "failed to get iface clock\n");
822 return PTR_ERR(qcom_iommu->iface_clk);
823 }
824
825 qcom_iommu->bus_clk = devm_clk_get(dev, "bus");
826 if (IS_ERR(qcom_iommu->bus_clk)) {
827 dev_err(dev, "failed to get bus clock\n");
828 return PTR_ERR(qcom_iommu->bus_clk);
829 }
830
831 if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
832 &qcom_iommu->sec_id)) {
833 dev_err(dev, "missing qcom,iommu-secure-id property\n");
834 return -ENODEV;
835 }
836
Stanimir Varbanovd051f282017-08-09 10:43:05 -0400837 if (qcom_iommu_has_secure_context(qcom_iommu)) {
838 ret = qcom_iommu_sec_ptbl_init(dev);
839 if (ret) {
840 dev_err(dev, "cannot init secure pg table(%d)\n", ret);
841 return ret;
842 }
843 }
844
Rob Clark0ae349a2017-08-09 10:43:04 -0400845 platform_set_drvdata(pdev, qcom_iommu);
846
847 pm_runtime_enable(dev);
848
849 /* register context bank devices, which are child nodes: */
850 ret = devm_of_platform_populate(dev);
851 if (ret) {
852 dev_err(dev, "Failed to populate iommu contexts\n");
853 return ret;
854 }
855
856 ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
857 dev_name(dev));
858 if (ret) {
859 dev_err(dev, "Failed to register iommu in sysfs\n");
860 return ret;
861 }
862
863 iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops);
864 iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode);
865
866 ret = iommu_device_register(&qcom_iommu->iommu);
867 if (ret) {
868 dev_err(dev, "Failed to register iommu\n");
869 return ret;
870 }
871
872 bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
873
874 if (qcom_iommu->local_base) {
875 pm_runtime_get_sync(dev);
876 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
877 pm_runtime_put_sync(dev);
878 }
879
880 return 0;
881}
882
883static int qcom_iommu_device_remove(struct platform_device *pdev)
884{
885 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
886
887 bus_set_iommu(&platform_bus_type, NULL);
888
889 pm_runtime_force_suspend(&pdev->dev);
890 platform_set_drvdata(pdev, NULL);
891 iommu_device_sysfs_remove(&qcom_iommu->iommu);
892 iommu_device_unregister(&qcom_iommu->iommu);
893
894 return 0;
895}
896
Arnd Bergmann6ce5b0f2017-08-23 15:42:45 +0200897static int __maybe_unused qcom_iommu_resume(struct device *dev)
Rob Clark0ae349a2017-08-09 10:43:04 -0400898{
Wolfram Sang7d1bf142018-04-19 16:05:54 +0200899 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
Rob Clark0ae349a2017-08-09 10:43:04 -0400900
901 return qcom_iommu_enable_clocks(qcom_iommu);
902}
903
Arnd Bergmann6ce5b0f2017-08-23 15:42:45 +0200904static int __maybe_unused qcom_iommu_suspend(struct device *dev)
Rob Clark0ae349a2017-08-09 10:43:04 -0400905{
Wolfram Sang7d1bf142018-04-19 16:05:54 +0200906 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
Rob Clark0ae349a2017-08-09 10:43:04 -0400907
908 qcom_iommu_disable_clocks(qcom_iommu);
909
910 return 0;
911}
Rob Clark0ae349a2017-08-09 10:43:04 -0400912
913static const struct dev_pm_ops qcom_iommu_pm_ops = {
914 SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
915 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
916 pm_runtime_force_resume)
917};
918
919static const struct of_device_id qcom_iommu_of_match[] = {
920 { .compatible = "qcom,msm-iommu-v1" },
921 { /* sentinel */ }
922};
Rob Clark0ae349a2017-08-09 10:43:04 -0400923
924static struct platform_driver qcom_iommu_driver = {
925 .driver = {
926 .name = "qcom-iommu",
927 .of_match_table = of_match_ptr(qcom_iommu_of_match),
928 .pm = &qcom_iommu_pm_ops,
929 },
930 .probe = qcom_iommu_device_probe,
931 .remove = qcom_iommu_device_remove,
932};
933
934static int __init qcom_iommu_init(void)
935{
936 int ret;
937
938 ret = platform_driver_register(&qcom_iommu_ctx_driver);
939 if (ret)
940 return ret;
941
942 ret = platform_driver_register(&qcom_iommu_driver);
943 if (ret)
944 platform_driver_unregister(&qcom_iommu_ctx_driver);
945
946 return ret;
947}
Paul Gortmakerf295cf22018-12-01 14:19:14 -0500948device_initcall(qcom_iommu_init);