1 // SPDX-License-Identifier: GPL-2.0-only
3 * IOMMU API for QCOM secure IOMMUs. Somewhat based on arm-smmu.c
5 * Copyright (C) 2013 ARM Limited
6 * Copyright (C) 2017 Red Hat
9 #include <linux/atomic.h>
10 #include <linux/bitfield.h>
11 #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>
18 #include <linux/io-64-nonatomic-hi-lo.h>
19 #include <linux/io-pgtable.h>
20 #include <linux/iommu.h>
21 #include <linux/iopoll.h>
22 #include <linux/kconfig.h>
23 #include <linux/init.h>
24 #include <linux/mutex.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>
31 #include <linux/pm_runtime.h>
32 #include <linux/qcom_scm.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
38 #define SMMU_INTR_SEL_NS 0x2000
47 struct qcom_iommu_ctx;
49 struct qcom_iommu_dev {
50 /* IOMMU core code handle */
51 struct iommu_device iommu;
53 struct clk_bulk_data clks[CLK_NUM];
54 void __iomem *local_base;
57 struct qcom_iommu_ctx *ctxs[]; /* indexed by asid-1 */
60 struct qcom_iommu_ctx {
64 u8 asid; /* asid and ctx bank # are 1:1 */
65 struct iommu_domain *domain;
68 struct qcom_iommu_domain {
69 struct io_pgtable_ops *pgtbl_ops;
70 spinlock_t pgtbl_lock;
71 struct mutex init_mutex; /* Protects iommu pointer */
72 struct iommu_domain domain;
73 struct qcom_iommu_dev *iommu;
74 struct iommu_fwspec *fwspec;
77 static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
79 return container_of(dom, struct qcom_iommu_domain, domain);
82 static const struct iommu_ops qcom_iommu_ops;
84 static struct qcom_iommu_dev * to_iommu(struct device *dev)
86 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
88 if (!fwspec || fwspec->ops != &qcom_iommu_ops)
91 return dev_iommu_priv_get(dev);
94 static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid)
96 struct qcom_iommu_dev *qcom_iommu = d->iommu;
99 return qcom_iommu->ctxs[asid - 1];
103 iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
105 writel_relaxed(val, ctx->base + reg);
109 iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
111 writeq_relaxed(val, ctx->base + reg);
115 iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
117 return readl_relaxed(ctx->base + reg);
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 qcom_iommu_domain *qcom_domain = cookie;
129 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
132 for (i = 0; i < fwspec->num_ids; i++) {
133 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
134 unsigned int val, ret;
136 iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
138 ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
139 (val & 0x1) == 0, 0, 5000000);
141 dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
145 static void qcom_iommu_tlb_inv_context(void *cookie)
147 struct qcom_iommu_domain *qcom_domain = cookie;
148 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
151 for (i = 0; i < fwspec->num_ids; i++) {
152 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
153 iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
156 qcom_iommu_tlb_sync(cookie);
159 static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
160 size_t granule, bool leaf, void *cookie)
162 struct qcom_iommu_domain *qcom_domain = cookie;
163 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
166 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
168 for (i = 0; i < fwspec->num_ids; i++) {
169 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
172 iova = (iova >> 12) << 12;
175 iommu_writel(ctx, reg, iova);
177 } while (s -= granule);
181 static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
182 size_t granule, void *cookie)
184 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
185 qcom_iommu_tlb_sync(cookie);
188 static void qcom_iommu_tlb_flush_leaf(unsigned long iova, size_t size,
189 size_t granule, void *cookie)
191 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, true, cookie);
192 qcom_iommu_tlb_sync(cookie);
195 static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
196 unsigned long iova, size_t granule,
199 qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
202 static const struct iommu_flush_ops qcom_flush_ops = {
203 .tlb_flush_all = qcom_iommu_tlb_inv_context,
204 .tlb_flush_walk = qcom_iommu_tlb_flush_walk,
205 .tlb_flush_leaf = qcom_iommu_tlb_flush_leaf,
206 .tlb_add_page = qcom_iommu_tlb_add_page,
209 static irqreturn_t qcom_iommu_fault(int irq, void *dev)
211 struct qcom_iommu_ctx *ctx = dev;
215 fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
217 if (!(fsr & ARM_SMMU_FSR_FAULT))
220 fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
221 iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
223 if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
224 dev_err_ratelimited(ctx->dev,
225 "Unhandled context fault: fsr=0x%x, "
226 "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
227 fsr, iova, fsynr, ctx->asid);
230 iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
231 iommu_writel(ctx, ARM_SMMU_CB_RESUME, ARM_SMMU_RESUME_TERMINATE);
236 static int qcom_iommu_init_domain(struct iommu_domain *domain,
237 struct qcom_iommu_dev *qcom_iommu,
240 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
241 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
242 struct io_pgtable_ops *pgtbl_ops;
243 struct io_pgtable_cfg pgtbl_cfg;
247 mutex_lock(&qcom_domain->init_mutex);
248 if (qcom_domain->iommu)
251 pgtbl_cfg = (struct io_pgtable_cfg) {
252 .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
255 .tlb = &qcom_flush_ops,
256 .iommu_dev = qcom_iommu->dev,
259 qcom_domain->iommu = qcom_iommu;
260 qcom_domain->fwspec = fwspec;
262 pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, qcom_domain);
264 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
266 goto out_clear_iommu;
269 /* Update the domain's page sizes to reflect the page table format */
270 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
271 domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
272 domain->geometry.force_aperture = true;
274 for (i = 0; i < fwspec->num_ids; i++) {
275 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
277 if (!ctx->secure_init) {
278 ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
280 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
281 goto out_clear_iommu;
283 ctx->secure_init = true;
287 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
288 pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
289 FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
290 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
293 iommu_writel(ctx, ARM_SMMU_CB_TCR2,
294 arm_smmu_lpae_tcr2(&pgtbl_cfg));
295 iommu_writel(ctx, ARM_SMMU_CB_TCR,
296 arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
298 /* MAIRs (stage-1 only) */
299 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
300 pgtbl_cfg.arm_lpae_s1_cfg.mair);
301 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
302 pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
305 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
306 ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
307 ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
308 ARM_SMMU_SCTLR_CFCFG;
310 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
311 reg |= ARM_SMMU_SCTLR_E;
313 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
315 ctx->domain = domain;
318 mutex_unlock(&qcom_domain->init_mutex);
320 /* Publish page table ops for map/unmap */
321 qcom_domain->pgtbl_ops = pgtbl_ops;
326 qcom_domain->iommu = NULL;
328 mutex_unlock(&qcom_domain->init_mutex);
332 static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
334 struct qcom_iommu_domain *qcom_domain;
336 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
339 * Allocate the domain and initialise some of its data structures.
340 * We can't really do anything meaningful until we've added a
343 qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
347 if (type == IOMMU_DOMAIN_DMA &&
348 iommu_get_dma_cookie(&qcom_domain->domain)) {
353 mutex_init(&qcom_domain->init_mutex);
354 spin_lock_init(&qcom_domain->pgtbl_lock);
356 return &qcom_domain->domain;
359 static void qcom_iommu_domain_free(struct iommu_domain *domain)
361 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
363 iommu_put_dma_cookie(domain);
365 if (qcom_domain->iommu) {
367 * NOTE: unmap can be called after client device is powered
368 * off, for example, with GPUs or anything involving dma-buf.
369 * So we cannot rely on the device_link. Make sure the IOMMU
370 * is on to avoid unclocked accesses in the TLB inv path:
372 pm_runtime_get_sync(qcom_domain->iommu->dev);
373 free_io_pgtable_ops(qcom_domain->pgtbl_ops);
374 pm_runtime_put_sync(qcom_domain->iommu->dev);
380 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
382 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
383 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
387 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
391 /* Ensure that the domain is finalized */
392 pm_runtime_get_sync(qcom_iommu->dev);
393 ret = qcom_iommu_init_domain(domain, qcom_iommu, dev);
394 pm_runtime_put_sync(qcom_iommu->dev);
399 * Sanity check the domain. We don't support domains across
402 if (qcom_domain->iommu != qcom_iommu) {
403 dev_err(dev, "cannot attach to IOMMU %s while already "
404 "attached to domain on IOMMU %s\n",
405 dev_name(qcom_domain->iommu->dev),
406 dev_name(qcom_iommu->dev));
413 static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
415 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
416 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
417 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
420 if (WARN_ON(!qcom_domain->iommu))
423 pm_runtime_get_sync(qcom_iommu->dev);
424 for (i = 0; i < fwspec->num_ids; i++) {
425 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
427 /* Disable the context bank: */
428 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
432 pm_runtime_put_sync(qcom_iommu->dev);
435 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
436 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
440 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
441 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
446 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
447 ret = ops->map(ops, iova, paddr, size, prot, GFP_ATOMIC);
448 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
452 static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
453 size_t size, struct iommu_iotlb_gather *gather)
457 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
458 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
463 /* NOTE: unmap can be called after client device is powered off,
464 * for example, with GPUs or anything involving dma-buf. So we
465 * cannot rely on the device_link. Make sure the IOMMU is on to
466 * avoid unclocked accesses in the TLB inv path:
468 pm_runtime_get_sync(qcom_domain->iommu->dev);
469 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
470 ret = ops->unmap(ops, iova, size, gather);
471 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
472 pm_runtime_put_sync(qcom_domain->iommu->dev);
477 static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
479 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
480 struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
481 struct io_pgtable, ops);
482 if (!qcom_domain->pgtbl_ops)
485 pm_runtime_get_sync(qcom_domain->iommu->dev);
486 qcom_iommu_tlb_sync(pgtable->cookie);
487 pm_runtime_put_sync(qcom_domain->iommu->dev);
490 static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
491 struct iommu_iotlb_gather *gather)
493 qcom_iommu_flush_iotlb_all(domain);
496 static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
501 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
502 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
507 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
508 ret = ops->iova_to_phys(ops, iova);
509 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
514 static bool qcom_iommu_capable(enum iommu_cap cap)
517 case IOMMU_CAP_CACHE_COHERENCY:
519 * Return true here as the SMMU can always send out coherent
523 case IOMMU_CAP_NOEXEC:
530 static struct iommu_device *qcom_iommu_probe_device(struct device *dev)
532 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
533 struct device_link *link;
536 return ERR_PTR(-ENODEV);
539 * Establish the link between iommu and master, so that the
540 * iommu gets runtime enabled/disabled as per the master's
543 link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
545 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
546 dev_name(qcom_iommu->dev), dev_name(dev));
547 return ERR_PTR(-ENODEV);
550 return &qcom_iommu->iommu;
553 static void qcom_iommu_release_device(struct device *dev)
555 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
560 iommu_fwspec_free(dev);
563 static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
565 struct qcom_iommu_dev *qcom_iommu;
566 struct platform_device *iommu_pdev;
567 unsigned asid = args->args[0];
569 if (args->args_count != 1) {
570 dev_err(dev, "incorrect number of iommu params found for %s "
571 "(found %d, expected 1)\n",
572 args->np->full_name, args->args_count);
576 iommu_pdev = of_find_device_by_node(args->np);
577 if (WARN_ON(!iommu_pdev))
580 qcom_iommu = platform_get_drvdata(iommu_pdev);
582 /* make sure the asid specified in dt is valid, so we don't have
583 * to sanity check this elsewhere, since 'asid - 1' is used to
584 * index into qcom_iommu->ctxs:
586 if (WARN_ON(asid < 1) ||
587 WARN_ON(asid > qcom_iommu->num_ctxs)) {
588 put_device(&iommu_pdev->dev);
592 if (!dev_iommu_priv_get(dev)) {
593 dev_iommu_priv_set(dev, qcom_iommu);
595 /* make sure devices iommus dt node isn't referring to
596 * multiple different iommu devices. Multiple context
597 * banks are ok, but multiple devices are not:
599 if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev))) {
600 put_device(&iommu_pdev->dev);
605 return iommu_fwspec_add_ids(dev, &asid, 1);
608 static const struct iommu_ops qcom_iommu_ops = {
609 .capable = qcom_iommu_capable,
610 .domain_alloc = qcom_iommu_domain_alloc,
611 .domain_free = qcom_iommu_domain_free,
612 .attach_dev = qcom_iommu_attach_dev,
613 .detach_dev = qcom_iommu_detach_dev,
614 .map = qcom_iommu_map,
615 .unmap = qcom_iommu_unmap,
616 .flush_iotlb_all = qcom_iommu_flush_iotlb_all,
617 .iotlb_sync = qcom_iommu_iotlb_sync,
618 .iova_to_phys = qcom_iommu_iova_to_phys,
619 .probe_device = qcom_iommu_probe_device,
620 .release_device = qcom_iommu_release_device,
621 .device_group = generic_device_group,
622 .of_xlate = qcom_iommu_of_xlate,
623 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
626 static int qcom_iommu_sec_ptbl_init(struct device *dev)
629 unsigned int spare = 0;
633 static bool allocated = false;
639 ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
641 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
646 dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
648 attrs = DMA_ATTR_NO_KERNEL_MAPPING;
650 cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
652 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
657 ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
659 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
667 dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
671 static int get_asid(const struct device_node *np)
675 /* read the "reg" property directly to get the relative address
676 * of the context bank, and calculate the asid from that:
678 if (of_property_read_u32_index(np, "reg", 0, ®))
681 return reg / 0x1000; /* context banks are 0x1000 apart */
684 static int qcom_iommu_ctx_probe(struct platform_device *pdev)
686 struct qcom_iommu_ctx *ctx;
687 struct device *dev = &pdev->dev;
688 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
689 struct resource *res;
692 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
697 platform_set_drvdata(pdev, ctx);
699 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
700 ctx->base = devm_ioremap_resource(dev, res);
701 if (IS_ERR(ctx->base))
702 return PTR_ERR(ctx->base);
704 irq = platform_get_irq(pdev, 0);
708 /* clear IRQs before registering fault handler, just in case the
709 * boot-loader left us a surprise:
711 iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
713 ret = devm_request_irq(dev, irq,
719 dev_err(dev, "failed to request IRQ %u\n", irq);
723 ret = get_asid(dev->of_node);
725 dev_err(dev, "missing reg property\n");
731 dev_dbg(dev, "found asid %u\n", ctx->asid);
733 qcom_iommu->ctxs[ctx->asid - 1] = ctx;
738 static int qcom_iommu_ctx_remove(struct platform_device *pdev)
740 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
741 struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
743 platform_set_drvdata(pdev, NULL);
745 qcom_iommu->ctxs[ctx->asid - 1] = NULL;
750 static const struct of_device_id ctx_of_match[] = {
751 { .compatible = "qcom,msm-iommu-v1-ns" },
752 { .compatible = "qcom,msm-iommu-v1-sec" },
756 static struct platform_driver qcom_iommu_ctx_driver = {
758 .name = "qcom-iommu-ctx",
759 .of_match_table = ctx_of_match,
761 .probe = qcom_iommu_ctx_probe,
762 .remove = qcom_iommu_ctx_remove,
765 static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
767 struct device_node *child;
769 for_each_child_of_node(qcom_iommu->dev->of_node, child) {
770 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) {
779 static int qcom_iommu_device_probe(struct platform_device *pdev)
781 struct device_node *child;
782 struct qcom_iommu_dev *qcom_iommu;
783 struct device *dev = &pdev->dev;
784 struct resource *res;
786 int ret, max_asid = 0;
788 /* find the max asid (which is 1:1 to ctx bank idx), so we know how
789 * many child ctx devices we have:
791 for_each_child_of_node(dev->of_node, child)
792 max_asid = max(max_asid, get_asid(child));
794 qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
798 qcom_iommu->num_ctxs = max_asid;
799 qcom_iommu->dev = dev;
801 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
803 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
804 if (IS_ERR(qcom_iommu->local_base))
805 return PTR_ERR(qcom_iommu->local_base);
808 clk = devm_clk_get(dev, "iface");
810 dev_err(dev, "failed to get iface clock\n");
813 qcom_iommu->clks[CLK_IFACE].clk = clk;
815 clk = devm_clk_get(dev, "bus");
817 dev_err(dev, "failed to get bus clock\n");
820 qcom_iommu->clks[CLK_BUS].clk = clk;
822 clk = devm_clk_get_optional(dev, "tbu");
824 dev_err(dev, "failed to get tbu clock\n");
827 qcom_iommu->clks[CLK_TBU].clk = clk;
829 if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
830 &qcom_iommu->sec_id)) {
831 dev_err(dev, "missing qcom,iommu-secure-id property\n");
835 if (qcom_iommu_has_secure_context(qcom_iommu)) {
836 ret = qcom_iommu_sec_ptbl_init(dev);
838 dev_err(dev, "cannot init secure pg table(%d)\n", ret);
843 platform_set_drvdata(pdev, qcom_iommu);
845 pm_runtime_enable(dev);
847 /* register context bank devices, which are child nodes: */
848 ret = devm_of_platform_populate(dev);
850 dev_err(dev, "Failed to populate iommu contexts\n");
854 ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
857 dev_err(dev, "Failed to register iommu in sysfs\n");
861 iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops);
862 iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode);
864 ret = iommu_device_register(&qcom_iommu->iommu);
866 dev_err(dev, "Failed to register iommu\n");
870 bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
872 if (qcom_iommu->local_base) {
873 pm_runtime_get_sync(dev);
874 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
875 pm_runtime_put_sync(dev);
881 static int qcom_iommu_device_remove(struct platform_device *pdev)
883 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
885 bus_set_iommu(&platform_bus_type, NULL);
887 pm_runtime_force_suspend(&pdev->dev);
888 platform_set_drvdata(pdev, NULL);
889 iommu_device_sysfs_remove(&qcom_iommu->iommu);
890 iommu_device_unregister(&qcom_iommu->iommu);
895 static int __maybe_unused qcom_iommu_resume(struct device *dev)
897 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
899 return clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks);
902 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
904 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
906 clk_bulk_disable_unprepare(CLK_NUM, qcom_iommu->clks);
911 static const struct dev_pm_ops qcom_iommu_pm_ops = {
912 SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
913 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
914 pm_runtime_force_resume)
917 static const struct of_device_id qcom_iommu_of_match[] = {
918 { .compatible = "qcom,msm-iommu-v1" },
922 static struct platform_driver qcom_iommu_driver = {
924 .name = "qcom-iommu",
925 .of_match_table = qcom_iommu_of_match,
926 .pm = &qcom_iommu_pm_ops,
928 .probe = qcom_iommu_device_probe,
929 .remove = qcom_iommu_device_remove,
932 static int __init qcom_iommu_init(void)
936 ret = platform_driver_register(&qcom_iommu_ctx_driver);
940 ret = platform_driver_register(&qcom_iommu_driver);
942 platform_driver_unregister(&qcom_iommu_ctx_driver);
946 device_initcall(qcom_iommu_init);