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
40 struct qcom_iommu_ctx;
42 struct qcom_iommu_dev {
43 /* IOMMU core code handle */
44 struct iommu_device iommu;
46 struct clk *iface_clk;
48 void __iomem *local_base;
51 struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid-1 */
54 struct qcom_iommu_ctx {
58 u8 asid; /* asid and ctx bank # are 1:1 */
59 struct iommu_domain *domain;
62 struct 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;
70 static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
72 return container_of(dom, struct qcom_iommu_domain, domain);
75 static const struct iommu_ops qcom_iommu_ops;
77 static struct qcom_iommu_dev * to_iommu(struct iommu_fwspec *fwspec)
79 if (!fwspec || fwspec->ops != &qcom_iommu_ops)
81 return fwspec->iommu_priv;
84 static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid)
86 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
89 return qcom_iommu->ctxs[asid - 1];
93 iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
95 writel_relaxed(val, ctx->base + reg);
99 iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
101 writeq_relaxed(val, ctx->base + reg);
105 iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
107 return readl_relaxed(ctx->base + reg);
111 iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
113 return readq_relaxed(ctx->base + reg);
116 static void qcom_iommu_tlb_sync(void *cookie)
118 struct iommu_fwspec *fwspec = cookie;
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;
125 iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
127 ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
128 (val & 0x1) == 0, 0, 5000000);
130 dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
134 static void qcom_iommu_tlb_inv_context(void *cookie)
136 struct iommu_fwspec *fwspec = cookie;
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);
144 qcom_iommu_tlb_sync(cookie);
147 static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
148 size_t granule, bool leaf, void *cookie)
150 struct iommu_fwspec *fwspec = cookie;
153 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
155 for (i = 0; i < fwspec->num_ids; i++) {
156 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
159 iova = (iova >> 12) << 12;
162 iommu_writel(ctx, reg, iova);
164 } while (s -= granule);
168 static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
169 size_t granule, void *cookie)
171 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
172 qcom_iommu_tlb_sync(cookie);
175 static void qcom_iommu_tlb_flush_leaf(unsigned long iova, size_t size,
176 size_t granule, void *cookie)
178 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, true, cookie);
179 qcom_iommu_tlb_sync(cookie);
182 static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
183 unsigned long iova, size_t granule,
186 qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
189 static const struct iommu_flush_ops qcom_flush_ops = {
190 .tlb_flush_all = qcom_iommu_tlb_inv_context,
191 .tlb_flush_walk = qcom_iommu_tlb_flush_walk,
192 .tlb_flush_leaf = qcom_iommu_tlb_flush_leaf,
193 .tlb_add_page = qcom_iommu_tlb_add_page,
196 static irqreturn_t qcom_iommu_fault(int irq, void *dev)
198 struct qcom_iommu_ctx *ctx = dev;
202 fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
204 if (!(fsr & FSR_FAULT))
207 fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
208 iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
210 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);
217 iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
218 iommu_writel(ctx, ARM_SMMU_CB_RESUME, RESUME_TERMINATE);
223 static int qcom_iommu_init_domain(struct iommu_domain *domain,
224 struct qcom_iommu_dev *qcom_iommu,
225 struct iommu_fwspec *fwspec)
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;
233 mutex_lock(&qcom_domain->init_mutex);
234 if (qcom_domain->iommu)
237 pgtbl_cfg = (struct io_pgtable_cfg) {
238 .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
241 .tlb = &qcom_flush_ops,
242 .iommu_dev = qcom_iommu->dev,
245 qcom_domain->iommu = qcom_iommu;
246 pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec);
248 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
250 goto out_clear_iommu;
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;
258 for (i = 0; i < fwspec->num_ids; i++) {
259 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
261 if (!ctx->secure_init) {
262 ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
264 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
265 goto out_clear_iommu;
267 ctx->secure_init = true;
271 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
272 pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0] |
273 FIELD_PREP(TTBRn_ASID, ctx->asid));
274 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1,
275 pgtbl_cfg.arm_lpae_s1_cfg.ttbr[1] |
276 FIELD_PREP(TTBRn_ASID, ctx->asid));
279 iommu_writel(ctx, ARM_SMMU_CB_TCR2,
280 (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) |
281 FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM));
282 iommu_writel(ctx, ARM_SMMU_CB_TCR,
283 pgtbl_cfg.arm_lpae_s1_cfg.tcr);
285 /* MAIRs (stage-1 only) */
286 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
287 pgtbl_cfg.arm_lpae_s1_cfg.mair[0]);
288 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
289 pgtbl_cfg.arm_lpae_s1_cfg.mair[1]);
292 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE |
293 SCTLR_M | SCTLR_S1_ASIDPNE | SCTLR_CFCFG;
295 if (IS_ENABLED(CONFIG_BIG_ENDIAN))
298 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
300 ctx->domain = domain;
303 mutex_unlock(&qcom_domain->init_mutex);
305 /* Publish page table ops for map/unmap */
306 qcom_domain->pgtbl_ops = pgtbl_ops;
311 qcom_domain->iommu = NULL;
313 mutex_unlock(&qcom_domain->init_mutex);
317 static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
319 struct qcom_iommu_domain *qcom_domain;
321 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
324 * Allocate the domain and initialise some of its data structures.
325 * We can't really do anything meaningful until we've added a
328 qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
332 if (type == IOMMU_DOMAIN_DMA &&
333 iommu_get_dma_cookie(&qcom_domain->domain)) {
338 mutex_init(&qcom_domain->init_mutex);
339 spin_lock_init(&qcom_domain->pgtbl_lock);
341 return &qcom_domain->domain;
344 static void qcom_iommu_domain_free(struct iommu_domain *domain)
346 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
348 iommu_put_dma_cookie(domain);
350 if (qcom_domain->iommu) {
352 * NOTE: unmap can be called after client device is powered
353 * off, for example, with GPUs or anything involving dma-buf.
354 * So we cannot rely on the device_link. Make sure the IOMMU
355 * is on to avoid unclocked accesses in the TLB inv path:
357 pm_runtime_get_sync(qcom_domain->iommu->dev);
358 free_io_pgtable_ops(qcom_domain->pgtbl_ops);
359 pm_runtime_put_sync(qcom_domain->iommu->dev);
365 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
367 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
368 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
369 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
373 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
377 /* Ensure that the domain is finalized */
378 pm_runtime_get_sync(qcom_iommu->dev);
379 ret = qcom_iommu_init_domain(domain, qcom_iommu, fwspec);
380 pm_runtime_put_sync(qcom_iommu->dev);
385 * Sanity check the domain. We don't support domains across
388 if (qcom_domain->iommu != qcom_iommu) {
389 dev_err(dev, "cannot attach to IOMMU %s while already "
390 "attached to domain on IOMMU %s\n",
391 dev_name(qcom_domain->iommu->dev),
392 dev_name(qcom_iommu->dev));
399 static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
401 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
402 struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec);
403 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
406 if (WARN_ON(!qcom_domain->iommu))
409 pm_runtime_get_sync(qcom_iommu->dev);
410 for (i = 0; i < fwspec->num_ids; i++) {
411 struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]);
413 /* Disable the context bank: */
414 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
418 pm_runtime_put_sync(qcom_iommu->dev);
421 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
422 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
426 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
427 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
432 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
433 ret = ops->map(ops, iova, paddr, size, prot);
434 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
438 static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
439 size_t size, struct iommu_iotlb_gather *gather)
443 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
444 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
449 /* NOTE: unmap can be called after client device is powered off,
450 * for example, with GPUs or anything involving dma-buf. So we
451 * cannot rely on the device_link. Make sure the IOMMU is on to
452 * avoid unclocked accesses in the TLB inv path:
454 pm_runtime_get_sync(qcom_domain->iommu->dev);
455 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
456 ret = ops->unmap(ops, iova, size, gather);
457 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
458 pm_runtime_put_sync(qcom_domain->iommu->dev);
463 static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
465 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
466 struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
467 struct io_pgtable, ops);
468 if (!qcom_domain->pgtbl_ops)
471 pm_runtime_get_sync(qcom_domain->iommu->dev);
472 qcom_iommu_tlb_sync(pgtable->cookie);
473 pm_runtime_put_sync(qcom_domain->iommu->dev);
476 static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
477 struct iommu_iotlb_gather *gather)
479 qcom_iommu_flush_iotlb_all(domain);
482 static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
487 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
488 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
493 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
494 ret = ops->iova_to_phys(ops, iova);
495 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
500 static bool qcom_iommu_capable(enum iommu_cap cap)
503 case IOMMU_CAP_CACHE_COHERENCY:
505 * Return true here as the SMMU can always send out coherent
509 case IOMMU_CAP_NOEXEC:
516 static int qcom_iommu_add_device(struct device *dev)
518 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
519 struct iommu_group *group;
520 struct device_link *link;
526 * Establish the link between iommu and master, so that the
527 * iommu gets runtime enabled/disabled as per the master's
530 link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
532 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
533 dev_name(qcom_iommu->dev), dev_name(dev));
537 group = iommu_group_get_for_dev(dev);
538 if (IS_ERR_OR_NULL(group))
539 return PTR_ERR_OR_ZERO(group);
541 iommu_group_put(group);
542 iommu_device_link(&qcom_iommu->iommu, dev);
547 static void qcom_iommu_remove_device(struct device *dev)
549 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev_iommu_fwspec_get(dev));
554 iommu_device_unlink(&qcom_iommu->iommu, dev);
555 iommu_group_remove_device(dev);
556 iommu_fwspec_free(dev);
559 static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
561 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
562 struct qcom_iommu_dev *qcom_iommu;
563 struct platform_device *iommu_pdev;
564 unsigned asid = args->args[0];
566 if (args->args_count != 1) {
567 dev_err(dev, "incorrect number of iommu params found for %s "
568 "(found %d, expected 1)\n",
569 args->np->full_name, args->args_count);
573 iommu_pdev = of_find_device_by_node(args->np);
574 if (WARN_ON(!iommu_pdev))
577 qcom_iommu = platform_get_drvdata(iommu_pdev);
579 /* make sure the asid specified in dt is valid, so we don't have
580 * to sanity check this elsewhere, since 'asid - 1' is used to
581 * index into qcom_iommu->ctxs:
583 if (WARN_ON(asid < 1) ||
584 WARN_ON(asid > qcom_iommu->num_ctxs))
587 if (!fwspec->iommu_priv) {
588 fwspec->iommu_priv = qcom_iommu;
590 /* make sure devices iommus dt node isn't referring to
591 * multiple different iommu devices. Multiple context
592 * banks are ok, but multiple devices are not:
594 if (WARN_ON(qcom_iommu != fwspec->iommu_priv))
598 return iommu_fwspec_add_ids(dev, &asid, 1);
601 static const struct iommu_ops qcom_iommu_ops = {
602 .capable = qcom_iommu_capable,
603 .domain_alloc = qcom_iommu_domain_alloc,
604 .domain_free = qcom_iommu_domain_free,
605 .attach_dev = qcom_iommu_attach_dev,
606 .detach_dev = qcom_iommu_detach_dev,
607 .map = qcom_iommu_map,
608 .unmap = qcom_iommu_unmap,
609 .flush_iotlb_all = qcom_iommu_flush_iotlb_all,
610 .iotlb_sync = qcom_iommu_iotlb_sync,
611 .iova_to_phys = qcom_iommu_iova_to_phys,
612 .add_device = qcom_iommu_add_device,
613 .remove_device = qcom_iommu_remove_device,
614 .device_group = generic_device_group,
615 .of_xlate = qcom_iommu_of_xlate,
616 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
619 static int qcom_iommu_enable_clocks(struct qcom_iommu_dev *qcom_iommu)
623 ret = clk_prepare_enable(qcom_iommu->iface_clk);
625 dev_err(qcom_iommu->dev, "Couldn't enable iface_clk\n");
629 ret = clk_prepare_enable(qcom_iommu->bus_clk);
631 dev_err(qcom_iommu->dev, "Couldn't enable bus_clk\n");
632 clk_disable_unprepare(qcom_iommu->iface_clk);
639 static void qcom_iommu_disable_clocks(struct qcom_iommu_dev *qcom_iommu)
641 clk_disable_unprepare(qcom_iommu->bus_clk);
642 clk_disable_unprepare(qcom_iommu->iface_clk);
645 static int qcom_iommu_sec_ptbl_init(struct device *dev)
648 unsigned int spare = 0;
652 static bool allocated = false;
658 ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
660 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
665 dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
667 attrs = DMA_ATTR_NO_KERNEL_MAPPING;
669 cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
671 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
676 ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
678 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
686 dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
690 static int get_asid(const struct device_node *np)
694 /* read the "reg" property directly to get the relative address
695 * of the context bank, and calculate the asid from that:
697 if (of_property_read_u32_index(np, "reg", 0, ®))
700 return reg / 0x1000; /* context banks are 0x1000 apart */
703 static int qcom_iommu_ctx_probe(struct platform_device *pdev)
705 struct qcom_iommu_ctx *ctx;
706 struct device *dev = &pdev->dev;
707 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
708 struct resource *res;
711 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
716 platform_set_drvdata(pdev, ctx);
718 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
719 ctx->base = devm_ioremap_resource(dev, res);
720 if (IS_ERR(ctx->base))
721 return PTR_ERR(ctx->base);
723 irq = platform_get_irq(pdev, 0);
727 /* clear IRQs before registering fault handler, just in case the
728 * boot-loader left us a surprise:
730 iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
732 ret = devm_request_irq(dev, irq,
738 dev_err(dev, "failed to request IRQ %u\n", irq);
742 ret = get_asid(dev->of_node);
744 dev_err(dev, "missing reg property\n");
750 dev_dbg(dev, "found asid %u\n", ctx->asid);
752 qcom_iommu->ctxs[ctx->asid - 1] = ctx;
757 static int qcom_iommu_ctx_remove(struct platform_device *pdev)
759 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
760 struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
762 platform_set_drvdata(pdev, NULL);
764 qcom_iommu->ctxs[ctx->asid - 1] = NULL;
769 static const struct of_device_id ctx_of_match[] = {
770 { .compatible = "qcom,msm-iommu-v1-ns" },
771 { .compatible = "qcom,msm-iommu-v1-sec" },
775 static struct platform_driver qcom_iommu_ctx_driver = {
777 .name = "qcom-iommu-ctx",
778 .of_match_table = of_match_ptr(ctx_of_match),
780 .probe = qcom_iommu_ctx_probe,
781 .remove = qcom_iommu_ctx_remove,
784 static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
786 struct device_node *child;
788 for_each_child_of_node(qcom_iommu->dev->of_node, child) {
789 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) {
798 static int qcom_iommu_device_probe(struct platform_device *pdev)
800 struct device_node *child;
801 struct qcom_iommu_dev *qcom_iommu;
802 struct device *dev = &pdev->dev;
803 struct resource *res;
804 int ret, max_asid = 0;
806 /* find the max asid (which is 1:1 to ctx bank idx), so we know how
807 * many child ctx devices we have:
809 for_each_child_of_node(dev->of_node, child)
810 max_asid = max(max_asid, get_asid(child));
812 qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
816 qcom_iommu->num_ctxs = max_asid;
817 qcom_iommu->dev = dev;
819 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
821 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
822 if (IS_ERR(qcom_iommu->local_base))
823 return PTR_ERR(qcom_iommu->local_base);
826 qcom_iommu->iface_clk = devm_clk_get(dev, "iface");
827 if (IS_ERR(qcom_iommu->iface_clk)) {
828 dev_err(dev, "failed to get iface clock\n");
829 return PTR_ERR(qcom_iommu->iface_clk);
832 qcom_iommu->bus_clk = devm_clk_get(dev, "bus");
833 if (IS_ERR(qcom_iommu->bus_clk)) {
834 dev_err(dev, "failed to get bus clock\n");
835 return PTR_ERR(qcom_iommu->bus_clk);
838 if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
839 &qcom_iommu->sec_id)) {
840 dev_err(dev, "missing qcom,iommu-secure-id property\n");
844 if (qcom_iommu_has_secure_context(qcom_iommu)) {
845 ret = qcom_iommu_sec_ptbl_init(dev);
847 dev_err(dev, "cannot init secure pg table(%d)\n", ret);
852 platform_set_drvdata(pdev, qcom_iommu);
854 pm_runtime_enable(dev);
856 /* register context bank devices, which are child nodes: */
857 ret = devm_of_platform_populate(dev);
859 dev_err(dev, "Failed to populate iommu contexts\n");
863 ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
866 dev_err(dev, "Failed to register iommu in sysfs\n");
870 iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops);
871 iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode);
873 ret = iommu_device_register(&qcom_iommu->iommu);
875 dev_err(dev, "Failed to register iommu\n");
879 bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
881 if (qcom_iommu->local_base) {
882 pm_runtime_get_sync(dev);
883 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
884 pm_runtime_put_sync(dev);
890 static int qcom_iommu_device_remove(struct platform_device *pdev)
892 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
894 bus_set_iommu(&platform_bus_type, NULL);
896 pm_runtime_force_suspend(&pdev->dev);
897 platform_set_drvdata(pdev, NULL);
898 iommu_device_sysfs_remove(&qcom_iommu->iommu);
899 iommu_device_unregister(&qcom_iommu->iommu);
904 static int __maybe_unused qcom_iommu_resume(struct device *dev)
906 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
908 return qcom_iommu_enable_clocks(qcom_iommu);
911 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
913 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
915 qcom_iommu_disable_clocks(qcom_iommu);
920 static const struct dev_pm_ops qcom_iommu_pm_ops = {
921 SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
922 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
923 pm_runtime_force_resume)
926 static const struct of_device_id qcom_iommu_of_match[] = {
927 { .compatible = "qcom,msm-iommu-v1" },
931 static struct platform_driver qcom_iommu_driver = {
933 .name = "qcom-iommu",
934 .of_match_table = of_match_ptr(qcom_iommu_of_match),
935 .pm = &qcom_iommu_pm_ops,
937 .probe = qcom_iommu_device_probe,
938 .remove = qcom_iommu_device_remove,
941 static int __init qcom_iommu_init(void)
945 ret = platform_driver_register(&qcom_iommu_ctx_driver);
949 ret = platform_driver_register(&qcom_iommu_driver);
951 platform_driver_unregister(&qcom_iommu_ctx_driver);
955 device_initcall(qcom_iommu_init);