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/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/qcom_scm.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
37 #define SMMU_INTR_SEL_NS 0x2000
46 struct qcom_iommu_ctx;
48 struct qcom_iommu_dev {
49 /* IOMMU core code handle */
50 struct iommu_device iommu;
52 struct clk_bulk_data clks[CLK_NUM];
53 void __iomem *local_base;
56 struct qcom_iommu_ctx *ctxs[]; /* indexed by asid-1 */
59 struct qcom_iommu_ctx {
63 u8 asid; /* asid and ctx bank # are 1:1 */
64 struct iommu_domain *domain;
67 struct qcom_iommu_domain {
68 struct io_pgtable_ops *pgtbl_ops;
69 spinlock_t pgtbl_lock;
70 struct mutex init_mutex; /* Protects iommu pointer */
71 struct iommu_domain domain;
72 struct qcom_iommu_dev *iommu;
73 struct iommu_fwspec *fwspec;
76 static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
78 return container_of(dom, struct qcom_iommu_domain, domain);
81 static const struct iommu_ops qcom_iommu_ops;
83 static struct qcom_iommu_dev * to_iommu(struct device *dev)
85 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
87 if (!fwspec || fwspec->ops != &qcom_iommu_ops)
90 return dev_iommu_priv_get(dev);
93 static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid)
95 struct qcom_iommu_dev *qcom_iommu = d->iommu;
98 return qcom_iommu->ctxs[asid - 1];
102 iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
104 writel_relaxed(val, ctx->base + reg);
108 iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
110 writeq_relaxed(val, ctx->base + reg);
114 iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
116 return readl_relaxed(ctx->base + reg);
120 iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
122 return readq_relaxed(ctx->base + reg);
125 static void qcom_iommu_tlb_sync(void *cookie)
127 struct qcom_iommu_domain *qcom_domain = cookie;
128 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
131 for (i = 0; i < fwspec->num_ids; i++) {
132 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, 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);
140 dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
144 static void qcom_iommu_tlb_inv_context(void *cookie)
146 struct qcom_iommu_domain *qcom_domain = cookie;
147 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
150 for (i = 0; i < fwspec->num_ids; i++) {
151 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
152 iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid);
155 qcom_iommu_tlb_sync(cookie);
158 static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size,
159 size_t granule, bool leaf, void *cookie)
161 struct qcom_iommu_domain *qcom_domain = cookie;
162 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
165 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
167 for (i = 0; i < fwspec->num_ids; i++) {
168 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
171 iova = (iova >> 12) << 12;
174 iommu_writel(ctx, reg, iova);
176 } while (s -= granule);
180 static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
181 size_t granule, void *cookie)
183 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
184 qcom_iommu_tlb_sync(cookie);
187 static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
188 unsigned long iova, size_t granule,
191 qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
194 static const struct iommu_flush_ops qcom_flush_ops = {
195 .tlb_flush_all = qcom_iommu_tlb_inv_context,
196 .tlb_flush_walk = qcom_iommu_tlb_flush_walk,
197 .tlb_add_page = qcom_iommu_tlb_add_page,
200 static irqreturn_t qcom_iommu_fault(int irq, void *dev)
202 struct qcom_iommu_ctx *ctx = dev;
206 fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
208 if (!(fsr & ARM_SMMU_FSR_FAULT))
211 fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
212 iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
214 if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
215 dev_err_ratelimited(ctx->dev,
216 "Unhandled context fault: fsr=0x%x, "
217 "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
218 fsr, iova, fsynr, ctx->asid);
221 iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
222 iommu_writel(ctx, ARM_SMMU_CB_RESUME, ARM_SMMU_RESUME_TERMINATE);
227 static int qcom_iommu_init_domain(struct iommu_domain *domain,
228 struct qcom_iommu_dev *qcom_iommu,
231 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
232 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
233 struct io_pgtable_ops *pgtbl_ops;
234 struct io_pgtable_cfg pgtbl_cfg;
238 mutex_lock(&qcom_domain->init_mutex);
239 if (qcom_domain->iommu)
242 pgtbl_cfg = (struct io_pgtable_cfg) {
243 .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
246 .tlb = &qcom_flush_ops,
247 .iommu_dev = qcom_iommu->dev,
250 qcom_domain->iommu = qcom_iommu;
251 qcom_domain->fwspec = fwspec;
253 pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, qcom_domain);
255 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
257 goto out_clear_iommu;
260 /* Update the domain's page sizes to reflect the page table format */
261 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
262 domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
263 domain->geometry.force_aperture = true;
265 for (i = 0; i < fwspec->num_ids; i++) {
266 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
268 if (!ctx->secure_init) {
269 ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
271 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
272 goto out_clear_iommu;
274 ctx->secure_init = true;
278 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
279 pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
280 FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
281 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
284 iommu_writel(ctx, ARM_SMMU_CB_TCR2,
285 arm_smmu_lpae_tcr2(&pgtbl_cfg));
286 iommu_writel(ctx, ARM_SMMU_CB_TCR,
287 arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
289 /* MAIRs (stage-1 only) */
290 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
291 pgtbl_cfg.arm_lpae_s1_cfg.mair);
292 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
293 pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
296 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
297 ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
298 ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
299 ARM_SMMU_SCTLR_CFCFG;
301 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
302 reg |= ARM_SMMU_SCTLR_E;
304 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
306 ctx->domain = domain;
309 mutex_unlock(&qcom_domain->init_mutex);
311 /* Publish page table ops for map/unmap */
312 qcom_domain->pgtbl_ops = pgtbl_ops;
317 qcom_domain->iommu = NULL;
319 mutex_unlock(&qcom_domain->init_mutex);
323 static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
325 struct qcom_iommu_domain *qcom_domain;
327 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
330 * Allocate the domain and initialise some of its data structures.
331 * We can't really do anything meaningful until we've added a
334 qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
338 if (type == IOMMU_DOMAIN_DMA &&
339 iommu_get_dma_cookie(&qcom_domain->domain)) {
344 mutex_init(&qcom_domain->init_mutex);
345 spin_lock_init(&qcom_domain->pgtbl_lock);
347 return &qcom_domain->domain;
350 static void qcom_iommu_domain_free(struct iommu_domain *domain)
352 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
354 iommu_put_dma_cookie(domain);
356 if (qcom_domain->iommu) {
358 * NOTE: unmap can be called after client device is powered
359 * off, for example, with GPUs or anything involving dma-buf.
360 * So we cannot rely on the device_link. Make sure the IOMMU
361 * is on to avoid unclocked accesses in the TLB inv path:
363 pm_runtime_get_sync(qcom_domain->iommu->dev);
364 free_io_pgtable_ops(qcom_domain->pgtbl_ops);
365 pm_runtime_put_sync(qcom_domain->iommu->dev);
371 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
373 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
374 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
378 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
382 /* Ensure that the domain is finalized */
383 pm_runtime_get_sync(qcom_iommu->dev);
384 ret = qcom_iommu_init_domain(domain, qcom_iommu, dev);
385 pm_runtime_put_sync(qcom_iommu->dev);
390 * Sanity check the domain. We don't support domains across
393 if (qcom_domain->iommu != qcom_iommu) {
394 dev_err(dev, "cannot attach to IOMMU %s while already "
395 "attached to domain on IOMMU %s\n",
396 dev_name(qcom_domain->iommu->dev),
397 dev_name(qcom_iommu->dev));
404 static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev)
406 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
407 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
408 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
411 if (WARN_ON(!qcom_domain->iommu))
414 pm_runtime_get_sync(qcom_iommu->dev);
415 for (i = 0; i < fwspec->num_ids; i++) {
416 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
418 /* Disable the context bank: */
419 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0);
423 pm_runtime_put_sync(qcom_iommu->dev);
426 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
427 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
431 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
432 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
437 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
438 ret = ops->map(ops, iova, paddr, size, prot, GFP_ATOMIC);
439 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
443 static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
444 size_t size, struct iommu_iotlb_gather *gather)
448 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
449 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
454 /* NOTE: unmap can be called after client device is powered off,
455 * for example, with GPUs or anything involving dma-buf. So we
456 * cannot rely on the device_link. Make sure the IOMMU is on to
457 * avoid unclocked accesses in the TLB inv path:
459 pm_runtime_get_sync(qcom_domain->iommu->dev);
460 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
461 ret = ops->unmap(ops, iova, size, gather);
462 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
463 pm_runtime_put_sync(qcom_domain->iommu->dev);
468 static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
470 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
471 struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
472 struct io_pgtable, ops);
473 if (!qcom_domain->pgtbl_ops)
476 pm_runtime_get_sync(qcom_domain->iommu->dev);
477 qcom_iommu_tlb_sync(pgtable->cookie);
478 pm_runtime_put_sync(qcom_domain->iommu->dev);
481 static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
482 struct iommu_iotlb_gather *gather)
484 qcom_iommu_flush_iotlb_all(domain);
487 static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
492 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
493 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
498 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
499 ret = ops->iova_to_phys(ops, iova);
500 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
505 static bool qcom_iommu_capable(enum iommu_cap cap)
508 case IOMMU_CAP_CACHE_COHERENCY:
510 * Return true here as the SMMU can always send out coherent
514 case IOMMU_CAP_NOEXEC:
521 static struct iommu_device *qcom_iommu_probe_device(struct device *dev)
523 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
524 struct device_link *link;
527 return ERR_PTR(-ENODEV);
530 * Establish the link between iommu and master, so that the
531 * iommu gets runtime enabled/disabled as per the master's
534 link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
536 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
537 dev_name(qcom_iommu->dev), dev_name(dev));
538 return ERR_PTR(-ENODEV);
541 return &qcom_iommu->iommu;
544 static void qcom_iommu_release_device(struct device *dev)
546 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
551 iommu_fwspec_free(dev);
554 static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
556 struct qcom_iommu_dev *qcom_iommu;
557 struct platform_device *iommu_pdev;
558 unsigned asid = args->args[0];
560 if (args->args_count != 1) {
561 dev_err(dev, "incorrect number of iommu params found for %s "
562 "(found %d, expected 1)\n",
563 args->np->full_name, args->args_count);
567 iommu_pdev = of_find_device_by_node(args->np);
568 if (WARN_ON(!iommu_pdev))
571 qcom_iommu = platform_get_drvdata(iommu_pdev);
573 /* make sure the asid specified in dt is valid, so we don't have
574 * to sanity check this elsewhere, since 'asid - 1' is used to
575 * index into qcom_iommu->ctxs:
577 if (WARN_ON(asid < 1) ||
578 WARN_ON(asid > qcom_iommu->num_ctxs)) {
579 put_device(&iommu_pdev->dev);
583 if (!dev_iommu_priv_get(dev)) {
584 dev_iommu_priv_set(dev, qcom_iommu);
586 /* make sure devices iommus dt node isn't referring to
587 * multiple different iommu devices. Multiple context
588 * banks are ok, but multiple devices are not:
590 if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev))) {
591 put_device(&iommu_pdev->dev);
596 return iommu_fwspec_add_ids(dev, &asid, 1);
599 static const struct iommu_ops qcom_iommu_ops = {
600 .capable = qcom_iommu_capable,
601 .domain_alloc = qcom_iommu_domain_alloc,
602 .domain_free = qcom_iommu_domain_free,
603 .attach_dev = qcom_iommu_attach_dev,
604 .detach_dev = qcom_iommu_detach_dev,
605 .map = qcom_iommu_map,
606 .unmap = qcom_iommu_unmap,
607 .flush_iotlb_all = qcom_iommu_flush_iotlb_all,
608 .iotlb_sync = qcom_iommu_iotlb_sync,
609 .iova_to_phys = qcom_iommu_iova_to_phys,
610 .probe_device = qcom_iommu_probe_device,
611 .release_device = qcom_iommu_release_device,
612 .device_group = generic_device_group,
613 .of_xlate = qcom_iommu_of_xlate,
614 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
617 static int qcom_iommu_sec_ptbl_init(struct device *dev)
620 unsigned int spare = 0;
624 static bool allocated = false;
630 ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
632 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
637 dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
639 attrs = DMA_ATTR_NO_KERNEL_MAPPING;
641 cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
643 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
648 ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
650 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
658 dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
662 static int get_asid(const struct device_node *np)
666 /* read the "reg" property directly to get the relative address
667 * of the context bank, and calculate the asid from that:
669 if (of_property_read_u32_index(np, "reg", 0, ®))
672 return reg / 0x1000; /* context banks are 0x1000 apart */
675 static int qcom_iommu_ctx_probe(struct platform_device *pdev)
677 struct qcom_iommu_ctx *ctx;
678 struct device *dev = &pdev->dev;
679 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
680 struct resource *res;
683 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
688 platform_set_drvdata(pdev, ctx);
690 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
691 ctx->base = devm_ioremap_resource(dev, res);
692 if (IS_ERR(ctx->base))
693 return PTR_ERR(ctx->base);
695 irq = platform_get_irq(pdev, 0);
699 /* clear IRQs before registering fault handler, just in case the
700 * boot-loader left us a surprise:
702 iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
704 ret = devm_request_irq(dev, irq,
710 dev_err(dev, "failed to request IRQ %u\n", irq);
714 ret = get_asid(dev->of_node);
716 dev_err(dev, "missing reg property\n");
722 dev_dbg(dev, "found asid %u\n", ctx->asid);
724 qcom_iommu->ctxs[ctx->asid - 1] = ctx;
729 static int qcom_iommu_ctx_remove(struct platform_device *pdev)
731 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
732 struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
734 platform_set_drvdata(pdev, NULL);
736 qcom_iommu->ctxs[ctx->asid - 1] = NULL;
741 static const struct of_device_id ctx_of_match[] = {
742 { .compatible = "qcom,msm-iommu-v1-ns" },
743 { .compatible = "qcom,msm-iommu-v1-sec" },
747 static struct platform_driver qcom_iommu_ctx_driver = {
749 .name = "qcom-iommu-ctx",
750 .of_match_table = ctx_of_match,
752 .probe = qcom_iommu_ctx_probe,
753 .remove = qcom_iommu_ctx_remove,
756 static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
758 struct device_node *child;
760 for_each_child_of_node(qcom_iommu->dev->of_node, child)
761 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec"))
767 static int qcom_iommu_device_probe(struct platform_device *pdev)
769 struct device_node *child;
770 struct qcom_iommu_dev *qcom_iommu;
771 struct device *dev = &pdev->dev;
772 struct resource *res;
774 int ret, max_asid = 0;
776 /* find the max asid (which is 1:1 to ctx bank idx), so we know how
777 * many child ctx devices we have:
779 for_each_child_of_node(dev->of_node, child)
780 max_asid = max(max_asid, get_asid(child));
782 qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
786 qcom_iommu->num_ctxs = max_asid;
787 qcom_iommu->dev = dev;
789 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
791 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
792 if (IS_ERR(qcom_iommu->local_base))
793 return PTR_ERR(qcom_iommu->local_base);
796 clk = devm_clk_get(dev, "iface");
798 dev_err(dev, "failed to get iface clock\n");
801 qcom_iommu->clks[CLK_IFACE].clk = clk;
803 clk = devm_clk_get(dev, "bus");
805 dev_err(dev, "failed to get bus clock\n");
808 qcom_iommu->clks[CLK_BUS].clk = clk;
810 clk = devm_clk_get_optional(dev, "tbu");
812 dev_err(dev, "failed to get tbu clock\n");
815 qcom_iommu->clks[CLK_TBU].clk = clk;
817 if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
818 &qcom_iommu->sec_id)) {
819 dev_err(dev, "missing qcom,iommu-secure-id property\n");
823 if (qcom_iommu_has_secure_context(qcom_iommu)) {
824 ret = qcom_iommu_sec_ptbl_init(dev);
826 dev_err(dev, "cannot init secure pg table(%d)\n", ret);
831 platform_set_drvdata(pdev, qcom_iommu);
833 pm_runtime_enable(dev);
835 /* register context bank devices, which are child nodes: */
836 ret = devm_of_platform_populate(dev);
838 dev_err(dev, "Failed to populate iommu contexts\n");
842 ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
845 dev_err(dev, "Failed to register iommu in sysfs\n");
849 ret = iommu_device_register(&qcom_iommu->iommu, &qcom_iommu_ops, dev);
851 dev_err(dev, "Failed to register iommu\n");
855 bus_set_iommu(&platform_bus_type, &qcom_iommu_ops);
857 if (qcom_iommu->local_base) {
858 pm_runtime_get_sync(dev);
859 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
860 pm_runtime_put_sync(dev);
866 static int qcom_iommu_device_remove(struct platform_device *pdev)
868 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
870 bus_set_iommu(&platform_bus_type, NULL);
872 pm_runtime_force_suspend(&pdev->dev);
873 platform_set_drvdata(pdev, NULL);
874 iommu_device_sysfs_remove(&qcom_iommu->iommu);
875 iommu_device_unregister(&qcom_iommu->iommu);
880 static int __maybe_unused qcom_iommu_resume(struct device *dev)
882 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
884 return clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks);
887 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
889 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
891 clk_bulk_disable_unprepare(CLK_NUM, qcom_iommu->clks);
896 static const struct dev_pm_ops qcom_iommu_pm_ops = {
897 SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
898 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
899 pm_runtime_force_resume)
902 static const struct of_device_id qcom_iommu_of_match[] = {
903 { .compatible = "qcom,msm-iommu-v1" },
907 static struct platform_driver qcom_iommu_driver = {
909 .name = "qcom-iommu",
910 .of_match_table = qcom_iommu_of_match,
911 .pm = &qcom_iommu_pm_ops,
913 .probe = qcom_iommu_device_probe,
914 .remove = qcom_iommu_device_remove,
917 static int __init qcom_iommu_init(void)
921 ret = platform_driver_register(&qcom_iommu_ctx_driver);
925 ret = platform_driver_register(&qcom_iommu_driver);
927 platform_driver_unregister(&qcom_iommu_ctx_driver);
931 device_initcall(qcom_iommu_init);