mpls: Return error for RTA_GATEWAY attribute
[linux-stable.git] / drivers / iommu / qcom_iommu.c
blobd8595f0a987d66bbec2a897646813cdc55a1cd07
1 /*
2 * IOMMU API for QCOM secure IOMMUs. Somewhat based on arm-smmu.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2013 ARM Limited
17 * Copyright (C) 2017 Red Hat
20 #include <linux/atomic.h>
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/dma-iommu.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/err.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/io-64-nonatomic-hi-lo.h>
29 #include <linux/iommu.h>
30 #include <linux/iopoll.h>
31 #include <linux/kconfig.h>
32 #include <linux/init.h>
33 #include <linux/mutex.h>
34 #include <linux/of.h>
35 #include <linux/of_address.h>
36 #include <linux/of_device.h>
37 #include <linux/of_iommu.h>
38 #include <linux/platform_device.h>
39 #include <linux/pm.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/qcom_scm.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
45 #include "io-pgtable.h"
46 #include "arm-smmu-regs.h"
48 #define SMMU_INTR_SEL_NS 0x2000
50 struct qcom_iommu_ctx;
52 struct qcom_iommu_dev {
53 /* IOMMU core code handle */
54 struct iommu_device iommu;
55 struct device *dev;
56 struct clk *iface_clk;
57 struct clk *bus_clk;
58 void __iomem *local_base;
59 u32 sec_id;
60 u8 num_ctxs;
61 struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid-1 */
64 struct qcom_iommu_ctx {
65 struct device *dev;
66 void __iomem *base;
67 bool secure_init;
68 u8 asid; /* asid and ctx bank # are 1:1 */
69 struct iommu_domain *domain;
72 struct qcom_iommu_domain {
73 struct io_pgtable_ops *pgtbl_ops;
74 spinlock_t pgtbl_lock;
75 struct mutex init_mutex; /* Protects iommu pointer */
76 struct iommu_domain domain;
77 struct qcom_iommu_dev *iommu;
80 static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
82 return container_of(dom, struct qcom_iommu_domain, domain);
85 static const struct iommu_ops qcom_iommu_ops;
87 static struct qcom_iommu_dev * to_iommu(struct iommu_fwspec *fwspec)
89 if (!fwspec || fwspec->ops != &qcom_iommu_ops)
90 return NULL;
91 return fwspec->iommu_priv;
94 static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid)
96 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
97 if (!qcom_iommu)
98 return NULL;
99 return qcom_iommu->ctxs[asid - 1];
102 static inline void
103 iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
105 writel_relaxed(val, ctx->base + reg);
108 static inline void
109 iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
111 writeq_relaxed(val, ctx->base + reg);
114 static inline u32
115 iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
117 return readl_relaxed(ctx->base + reg);
120 static inline u64
121 iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
123 return readq_relaxed(ctx->base + reg);
126 static void qcom_iommu_tlb_sync(void *cookie)
128 struct iommu_fwspec *fwspec = cookie;
129 unsigned i;
131 for (i = 0; i < fwspec->num_ids; i++) {
132 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
133 unsigned int val, ret;
135 iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
137 ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
138 (val & 0x1) == 0, 0, 5000000);
139 if (ret)
140 dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
144 static void qcom_iommu_tlb_inv_context(void *cookie)
146 struct iommu_fwspec *fwspec = cookie;
147 unsigned i;
149 for (i = 0; i < fwspec->num_ids; i++) {
150 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
151 iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
154 qcom_iommu_tlb_sync(cookie);
157 static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
158 size_t granule, bool leaf, void *cookie)
160 struct iommu_fwspec *fwspec = cookie;
161 unsigned i, reg;
163 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
165 for (i = 0; i < fwspec->num_ids; i++) {
166 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
167 size_t s = size;
169 iova &= ~12UL;
170 iova |= ctx->asid;
171 do {
172 iommu_writel(ctx, reg, iova);
173 iova += granule;
174 } while (s -= granule);
178 static const struct iommu_gather_ops qcom_gather_ops = {
179 .tlb_flush_all = qcom_iommu_tlb_inv_context,
180 .tlb_add_flush = qcom_iommu_tlb_inv_range_nosync,
181 .tlb_sync = qcom_iommu_tlb_sync,
184 static irqreturn_t qcom_iommu_fault(int irq, void *dev)
186 struct qcom_iommu_ctx *ctx = dev;
187 u32 fsr, fsynr;
188 u64 iova;
190 fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
192 if (!(fsr & FSR_FAULT))
193 return IRQ_NONE;
195 fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
196 iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
198 if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
199 dev_err_ratelimited(ctx->dev,
200 "Unhandled context fault: fsr=0x%x, "
201 "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
202 fsr, iova, fsynr, ctx->asid);
205 iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
206 iommu_writel(ctx, ARM_SMMU_CB_RESUME, RESUME_TERMINATE);
208 return IRQ_HANDLED;
211 static int qcom_iommu_init_domain(struct iommu_domain *domain,
212 struct qcom_iommu_dev *qcom_iommu,
213 struct iommu_fwspec *fwspec)
215 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
216 struct io_pgtable_ops *pgtbl_ops;
217 struct io_pgtable_cfg pgtbl_cfg;
218 int i, ret = 0;
219 u32 reg;
221 mutex_lock(&qcom_domain->init_mutex);
222 if (qcom_domain->iommu)
223 goto out_unlock;
225 pgtbl_cfg = (struct io_pgtable_cfg) {
226 .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
227 .ias = 32,
228 .oas = 40,
229 .tlb = &qcom_gather_ops,
230 .iommu_dev = qcom_iommu->dev,
233 qcom_domain->iommu = qcom_iommu;
234 pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec);
235 if (!pgtbl_ops) {
236 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
237 ret = -ENOMEM;
238 goto out_clear_iommu;
241 /* Update the domain's page sizes to reflect the page table format */
242 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
243 domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
244 domain->geometry.force_aperture = true;
246 for (i = 0; i < fwspec->num_ids; i++) {
247 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
249 if (!ctx->secure_init) {
250 ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
251 if (ret) {
252 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
253 goto out_clear_iommu;
255 ctx->secure_init = true;
258 /* TTBRs */
259 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
260 pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0] |
261 ((u64)ctx->asid << TTBRn_ASID_SHIFT));
262 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1,
263 pgtbl_cfg.arm_lpae_s1_cfg.ttbr[1] |
264 ((u64)ctx->asid << TTBRn_ASID_SHIFT));
266 /* TTBCR */
267 iommu_writel(ctx, ARM_SMMU_CB_TTBCR2,
268 (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) |
269 TTBCR2_SEP_UPSTREAM);
270 iommu_writel(ctx, ARM_SMMU_CB_TTBCR,
271 pgtbl_cfg.arm_lpae_s1_cfg.tcr);
273 /* MAIRs (stage-1 only) */
274 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
275 pgtbl_cfg.arm_lpae_s1_cfg.mair[0]);
276 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
277 pgtbl_cfg.arm_lpae_s1_cfg.mair[1]);
279 /* SCTLR */
280 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE |
281 SCTLR_M | SCTLR_S1_ASIDPNE | SCTLR_CFCFG;
283 if (IS_ENABLED(CONFIG_BIG_ENDIAN))
284 reg |= SCTLR_E;
286 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
288 ctx->domain = domain;
291 mutex_unlock(&qcom_domain->init_mutex);
293 /* Publish page table ops for map/unmap */
294 qcom_domain->pgtbl_ops = pgtbl_ops;
296 return 0;
298 out_clear_iommu:
299 qcom_domain->iommu = NULL;
300 out_unlock:
301 mutex_unlock(&qcom_domain->init_mutex);
302 return ret;
305 static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
307 struct qcom_iommu_domain *qcom_domain;
309 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
310 return NULL;
312 * Allocate the domain and initialise some of its data structures.
313 * We can't really do anything meaningful until we've added a
314 * master.
316 qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
317 if (!qcom_domain)
318 return NULL;
320 if (type == IOMMU_DOMAIN_DMA &&
321 iommu_get_dma_cookie(&qcom_domain->domain)) {
322 kfree(qcom_domain);
323 return NULL;
326 mutex_init(&qcom_domain->init_mutex);
327 spin_lock_init(&qcom_domain->pgtbl_lock);
329 return &qcom_domain->domain;
332 static void qcom_iommu_domain_free(struct iommu_domain *domain)
334 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
336 if (WARN_ON(qcom_domain->iommu)) /* forgot to detach? */
337 return;
339 iommu_put_dma_cookie(domain);
341 /* NOTE: unmap can be called after client device is powered off,
342 * for example, with GPUs or anything involving dma-buf. So we
343 * cannot rely on the device_link. Make sure the IOMMU is on to
344 * avoid unclocked accesses in the TLB inv path:
346 pm_runtime_get_sync(qcom_domain->iommu->dev);
348 free_io_pgtable_ops(qcom_domain->pgtbl_ops);
350 pm_runtime_put_sync(qcom_domain->iommu->dev);
352 kfree(qcom_domain);
355 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
357 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
358 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
359 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
360 int ret;
362 if (!qcom_iommu) {
363 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
364 return -ENXIO;
367 /* Ensure that the domain is finalized */
368 pm_runtime_get_sync(qcom_iommu->dev);
369 ret = qcom_iommu_init_domain(domain, qcom_iommu, fwspec);
370 pm_runtime_put_sync(qcom_iommu->dev);
371 if (ret < 0)
372 return ret;
375 * Sanity check the domain. We don't support domains across
376 * different IOMMUs.
378 if (qcom_domain->iommu != qcom_iommu) {
379 dev_err(dev, "cannot attach to IOMMU %s while already "
380 "attached to domain on IOMMU %s\n",
381 dev_name(qcom_domain->iommu->dev),
382 dev_name(qcom_iommu->dev));
383 return -EINVAL;
386 return 0;
389 static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
391 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
392 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
393 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
394 unsigned i;
396 if (!qcom_domain->iommu)
397 return;
399 pm_runtime_get_sync(qcom_iommu->dev);
400 for (i = 0; i < fwspec->num_ids; i++) {
401 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
403 /* Disable the context bank: */
404 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
406 ctx->domain = NULL;
408 pm_runtime_put_sync(qcom_iommu->dev);
410 qcom_domain->iommu = NULL;
413 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
414 phys_addr_t paddr, size_t size, int prot)
416 int ret;
417 unsigned long flags;
418 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
419 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
421 if (!ops)
422 return -ENODEV;
424 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
425 ret = ops->map(ops, iova, paddr, size, prot);
426 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
427 return ret;
430 static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
431 size_t size)
433 size_t ret;
434 unsigned long flags;
435 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
436 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
438 if (!ops)
439 return 0;
441 /* NOTE: unmap can be called after client device is powered off,
442 * for example, with GPUs or anything involving dma-buf. So we
443 * cannot rely on the device_link. Make sure the IOMMU is on to
444 * avoid unclocked accesses in the TLB inv path:
446 pm_runtime_get_sync(qcom_domain->iommu->dev);
447 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
448 ret = ops->unmap(ops, iova, size);
449 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
450 pm_runtime_put_sync(qcom_domain->iommu->dev);
452 return ret;
455 static void qcom_iommu_iotlb_sync(struct iommu_domain *domain)
457 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
458 struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
459 struct io_pgtable, ops);
460 if (!qcom_domain->pgtbl_ops)
461 return;
463 pm_runtime_get_sync(qcom_domain->iommu->dev);
464 qcom_iommu_tlb_sync(pgtable->cookie);
465 pm_runtime_put_sync(qcom_domain->iommu->dev);
468 static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
469 dma_addr_t iova)
471 phys_addr_t ret;
472 unsigned long flags;
473 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
474 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
476 if (!ops)
477 return 0;
479 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
480 ret = ops->iova_to_phys(ops, iova);
481 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
483 return ret;
486 static bool qcom_iommu_capable(enum iommu_cap cap)
488 switch (cap) {
489 case IOMMU_CAP_CACHE_COHERENCY:
491 * Return true here as the SMMU can always send out coherent
492 * requests.
494 return true;
495 case IOMMU_CAP_NOEXEC:
496 return true;
497 default:
498 return false;
502 static int qcom_iommu_add_device(struct device *dev)
504 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
505 struct iommu_group *group;
506 struct device_link *link;
508 if (!qcom_iommu)
509 return -ENODEV;
512 * Establish the link between iommu and master, so that the
513 * iommu gets runtime enabled/disabled as per the master's
514 * needs.
516 link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
517 if (!link) {
518 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
519 dev_name(qcom_iommu->dev), dev_name(dev));
520 return -ENODEV;
523 group = iommu_group_get_for_dev(dev);
524 if (IS_ERR_OR_NULL(group))
525 return PTR_ERR_OR_ZERO(group);
527 iommu_group_put(group);
528 iommu_device_link(&qcom_iommu->iommu, dev);
530 return 0;
533 static void qcom_iommu_remove_device(struct device *dev)
535 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
537 if (!qcom_iommu)
538 return;
540 iommu_device_unlink(&qcom_iommu->iommu, dev);
541 iommu_group_remove_device(dev);
542 iommu_fwspec_free(dev);
545 static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
547 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
548 struct qcom_iommu_dev *qcom_iommu;
549 struct platform_device *iommu_pdev;
550 unsigned asid = args->args[0];
552 if (args->args_count != 1) {
553 dev_err(dev, "incorrect number of iommu params found for %s "
554 "(found %d, expected 1)\n",
555 args->np->full_name, args->args_count);
556 return -EINVAL;
559 iommu_pdev = of_find_device_by_node(args->np);
560 if (WARN_ON(!iommu_pdev))
561 return -EINVAL;
563 qcom_iommu = platform_get_drvdata(iommu_pdev);
565 /* make sure the asid specified in dt is valid, so we don't have
566 * to sanity check this elsewhere, since 'asid - 1' is used to
567 * index into qcom_iommu->ctxs:
569 if (WARN_ON(asid < 1) ||
570 WARN_ON(asid > qcom_iommu->num_ctxs))
571 return -EINVAL;
573 if (!fwspec->iommu_priv) {
574 fwspec->iommu_priv = qcom_iommu;
575 } else {
576 /* make sure devices iommus dt node isn't referring to
577 * multiple different iommu devices. Multiple context
578 * banks are ok, but multiple devices are not:
580 if (WARN_ON(qcom_iommu != fwspec->iommu_priv))
581 return -EINVAL;
584 return iommu_fwspec_add_ids(dev, &asid, 1);
587 static const struct iommu_ops qcom_iommu_ops = {
588 .capable = qcom_iommu_capable,
589 .domain_alloc = qcom_iommu_domain_alloc,
590 .domain_free = qcom_iommu_domain_free,
591 .attach_dev = qcom_iommu_attach_dev,
592 .detach_dev = qcom_iommu_detach_dev,
593 .map = qcom_iommu_map,
594 .unmap = qcom_iommu_unmap,
595 .flush_iotlb_all = qcom_iommu_iotlb_sync,
596 .iotlb_sync = qcom_iommu_iotlb_sync,
597 .iova_to_phys = qcom_iommu_iova_to_phys,
598 .add_device = qcom_iommu_add_device,
599 .remove_device = qcom_iommu_remove_device,
600 .device_group = generic_device_group,
601 .of_xlate = qcom_iommu_of_xlate,
602 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
605 static int qcom_iommu_enable_clocks(struct qcom_iommu_dev *qcom_iommu)
607 int ret;
609 ret = clk_prepare_enable(qcom_iommu->iface_clk);
610 if (ret) {
611 dev_err(qcom_iommu->dev, "Couldn't enable iface_clk\n");
612 return ret;
615 ret = clk_prepare_enable(qcom_iommu->bus_clk);
616 if (ret) {
617 dev_err(qcom_iommu->dev, "Couldn't enable bus_clk\n");
618 clk_disable_unprepare(qcom_iommu->iface_clk);
619 return ret;
622 return 0;
625 static void qcom_iommu_disable_clocks(struct qcom_iommu_dev *qcom_iommu)
627 clk_disable_unprepare(qcom_iommu->bus_clk);
628 clk_disable_unprepare(qcom_iommu->iface_clk);
631 static int qcom_iommu_sec_ptbl_init(struct device *dev)
633 size_t psize = 0;
634 unsigned int spare = 0;
635 void *cpu_addr;
636 dma_addr_t paddr;
637 unsigned long attrs;
638 static bool allocated = false;
639 int ret;
641 if (allocated)
642 return 0;
644 ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
645 if (ret) {
646 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
647 ret);
648 return ret;
651 dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
653 attrs = DMA_ATTR_NO_KERNEL_MAPPING;
655 cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
656 if (!cpu_addr) {
657 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
658 psize);
659 return -ENOMEM;
662 ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
663 if (ret) {
664 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
665 goto free_mem;
668 allocated = true;
669 return 0;
671 free_mem:
672 dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
673 return ret;
676 static int get_asid(const struct device_node *np)
678 u32 reg;
680 /* read the "reg" property directly to get the relative address
681 * of the context bank, and calculate the asid from that:
683 if (of_property_read_u32_index(np, "reg", 0, &reg))
684 return -ENODEV;
686 return reg / 0x1000; /* context banks are 0x1000 apart */
689 static int qcom_iommu_ctx_probe(struct platform_device *pdev)
691 struct qcom_iommu_ctx *ctx;
692 struct device *dev = &pdev->dev;
693 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
694 struct resource *res;
695 int ret, irq;
697 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
698 if (!ctx)
699 return -ENOMEM;
701 ctx->dev = dev;
702 platform_set_drvdata(pdev, ctx);
704 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
705 ctx->base = devm_ioremap_resource(dev, res);
706 if (IS_ERR(ctx->base))
707 return PTR_ERR(ctx->base);
709 irq = platform_get_irq(pdev, 0);
710 if (irq < 0) {
711 dev_err(dev, "failed to get irq\n");
712 return -ENODEV;
715 /* clear IRQs before registering fault handler, just in case the
716 * boot-loader left us a surprise:
718 iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
720 ret = devm_request_irq(dev, irq,
721 qcom_iommu_fault,
722 IRQF_SHARED,
723 "qcom-iommu-fault",
724 ctx);
725 if (ret) {
726 dev_err(dev, "failed to request IRQ %u\n", irq);
727 return ret;
730 ret = get_asid(dev->of_node);
731 if (ret < 0) {
732 dev_err(dev, "missing reg property\n");
733 return ret;
736 ctx->asid = ret;
738 dev_dbg(dev, "found asid %u\n", ctx->asid);
740 qcom_iommu->ctxs[ctx->asid - 1] = ctx;
742 return 0;
745 static int qcom_iommu_ctx_remove(struct platform_device *pdev)
747 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
748 struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
750 platform_set_drvdata(pdev, NULL);
752 qcom_iommu->ctxs[ctx->asid - 1] = NULL;
754 return 0;
757 static const struct of_device_id ctx_of_match[] = {
758 { .compatible = "qcom,msm-iommu-v1-ns" },
759 { .compatible = "qcom,msm-iommu-v1-sec" },
760 { /* sentinel */ }
763 static struct platform_driver qcom_iommu_ctx_driver = {
764 .driver = {
765 .name = "qcom-iommu-ctx",
766 .of_match_table = of_match_ptr(ctx_of_match),
768 .probe = qcom_iommu_ctx_probe,
769 .remove = qcom_iommu_ctx_remove,
772 static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
774 struct device_node *child;
776 for_each_child_of_node(qcom_iommu->dev->of_node, child)
777 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec"))
778 return true;
780 return false;
783 static int qcom_iommu_device_probe(struct platform_device *pdev)
785 struct device_node *child;
786 struct qcom_iommu_dev *qcom_iommu;
787 struct device *dev = &pdev->dev;
788 struct resource *res;
789 int ret, sz, max_asid = 0;
791 /* find the max asid (which is 1:1 to ctx bank idx), so we know how
792 * many child ctx devices we have:
794 for_each_child_of_node(dev->of_node, child)
795 max_asid = max(max_asid, get_asid(child));
797 sz = sizeof(*qcom_iommu) + (max_asid * sizeof(qcom_iommu->ctxs[0]));
799 qcom_iommu = devm_kzalloc(dev, sz, GFP_KERNEL);
800 if (!qcom_iommu)
801 return -ENOMEM;
802 qcom_iommu->num_ctxs = max_asid;
803 qcom_iommu->dev = dev;
805 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
806 if (res)
807 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
809 qcom_iommu->iface_clk = devm_clk_get(dev, "iface");
810 if (IS_ERR(qcom_iommu->iface_clk)) {
811 dev_err(dev, "failed to get iface clock\n");
812 return PTR_ERR(qcom_iommu->iface_clk);
815 qcom_iommu->bus_clk = devm_clk_get(dev, "bus");
816 if (IS_ERR(qcom_iommu->bus_clk)) {
817 dev_err(dev, "failed to get bus clock\n");
818 return PTR_ERR(qcom_iommu->bus_clk);
821 if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
822 &qcom_iommu->sec_id)) {
823 dev_err(dev, "missing qcom,iommu-secure-id property\n");
824 return -ENODEV;
827 if (qcom_iommu_has_secure_context(qcom_iommu)) {
828 ret = qcom_iommu_sec_ptbl_init(dev);
829 if (ret) {
830 dev_err(dev, "cannot init secure pg table(%d)\n", ret);
831 return ret;
835 platform_set_drvdata(pdev, qcom_iommu);
837 pm_runtime_enable(dev);
839 /* register context bank devices, which are child nodes: */
840 ret = devm_of_platform_populate(dev);
841 if (ret) {
842 dev_err(dev, "Failed to populate iommu contexts\n");
843 return ret;
846 ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
847 dev_name(dev));
848 if (ret) {
849 dev_err(dev, "Failed to register iommu in sysfs\n");
850 return ret;
853 iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops);
854 iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode);
856 ret = iommu_device_register(&qcom_iommu->iommu);
857 if (ret) {
858 dev_err(dev, "Failed to register iommu\n");
859 return ret;
862 bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
864 if (qcom_iommu->local_base) {
865 pm_runtime_get_sync(dev);
866 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
867 pm_runtime_put_sync(dev);
870 return 0;
873 static int qcom_iommu_device_remove(struct platform_device *pdev)
875 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
877 bus_set_iommu(&platform_bus_type, NULL);
879 pm_runtime_force_suspend(&pdev->dev);
880 platform_set_drvdata(pdev, NULL);
881 iommu_device_sysfs_remove(&qcom_iommu->iommu);
882 iommu_device_unregister(&qcom_iommu->iommu);
884 return 0;
887 static int __maybe_unused qcom_iommu_resume(struct device *dev)
889 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
891 return qcom_iommu_enable_clocks(qcom_iommu);
894 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
896 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
898 qcom_iommu_disable_clocks(qcom_iommu);
900 return 0;
903 static const struct dev_pm_ops qcom_iommu_pm_ops = {
904 SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
905 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
906 pm_runtime_force_resume)
909 static const struct of_device_id qcom_iommu_of_match[] = {
910 { .compatible = "qcom,msm-iommu-v1" },
911 { /* sentinel */ }
914 static struct platform_driver qcom_iommu_driver = {
915 .driver = {
916 .name = "qcom-iommu",
917 .of_match_table = of_match_ptr(qcom_iommu_of_match),
918 .pm = &qcom_iommu_pm_ops,
920 .probe = qcom_iommu_device_probe,
921 .remove = qcom_iommu_device_remove,
924 static int __init qcom_iommu_init(void)
926 int ret;
928 ret = platform_driver_register(&qcom_iommu_ctx_driver);
929 if (ret)
930 return ret;
932 ret = platform_driver_register(&qcom_iommu_driver);
933 if (ret)
934 platform_driver_unregister(&qcom_iommu_ctx_driver);
936 return ret;
938 device_initcall(qcom_iommu_init);