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-mapping.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
17 #include <linux/io-64-nonatomic-hi-lo.h>
18 #include <linux/io-pgtable.h>
19 #include <linux/iommu.h>
20 #include <linux/iopoll.h>
21 #include <linux/kconfig.h>
22 #include <linux/init.h>
23 #include <linux/mutex.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/firmware/qcom/qcom_scm.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
36 #define SMMU_INTR_SEL_NS 0x2000
45 struct qcom_iommu_ctx;
47 struct qcom_iommu_dev {
48 /* IOMMU core code handle */
49 struct iommu_device iommu;
51 struct clk_bulk_data clks[CLK_NUM];
52 void __iomem *local_base;
55 struct qcom_iommu_ctx *ctxs[]; /* indexed by asid-1 */
58 struct qcom_iommu_ctx {
62 u8 asid; /* asid and ctx bank # are 1:1 */
63 struct iommu_domain *domain;
66 struct qcom_iommu_domain {
67 struct io_pgtable_ops *pgtbl_ops;
68 spinlock_t pgtbl_lock;
69 struct mutex init_mutex; /* Protects iommu pointer */
70 struct iommu_domain domain;
71 struct qcom_iommu_dev *iommu;
72 struct iommu_fwspec *fwspec;
75 static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom)
77 return container_of(dom, struct qcom_iommu_domain, domain);
80 static const struct iommu_ops qcom_iommu_ops;
82 static struct qcom_iommu_dev * to_iommu(struct device *dev)
84 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
86 if (!fwspec || fwspec->ops != &qcom_iommu_ops)
89 return dev_iommu_priv_get(dev);
92 static struct qcom_iommu_ctx * to_ctx(struct qcom_iommu_domain *d, unsigned asid)
94 struct qcom_iommu_dev *qcom_iommu = d->iommu;
97 return qcom_iommu->ctxs[asid - 1];
101 iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val)
103 writel_relaxed(val, ctx->base + reg);
107 iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val)
109 writeq_relaxed(val, ctx->base + reg);
113 iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg)
115 return readl_relaxed(ctx->base + reg);
119 iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg)
121 return readq_relaxed(ctx->base + reg);
124 static void qcom_iommu_tlb_sync(void *cookie)
126 struct qcom_iommu_domain *qcom_domain = cookie;
127 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
130 for (i = 0; i < fwspec->num_ids; i++) {
131 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
132 unsigned int val, ret;
134 iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0);
136 ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val,
137 (val & 0x1) == 0, 0, 5000000);
139 dev_err(ctx->dev, "timeout waiting for TLB SYNC\n");
143 static void qcom_iommu_tlb_inv_context(void *cookie)
145 struct qcom_iommu_domain *qcom_domain = cookie;
146 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
149 for (i = 0; i < fwspec->num_ids; i++) {
150 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, 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 qcom_iommu_domain *qcom_domain = cookie;
161 struct iommu_fwspec *fwspec = qcom_domain->fwspec;
164 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
166 for (i = 0; i < fwspec->num_ids; i++) {
167 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
170 iova = (iova >> 12) << 12;
173 iommu_writel(ctx, reg, iova);
175 } while (s -= granule);
179 static void qcom_iommu_tlb_flush_walk(unsigned long iova, size_t size,
180 size_t granule, void *cookie)
182 qcom_iommu_tlb_inv_range_nosync(iova, size, granule, false, cookie);
183 qcom_iommu_tlb_sync(cookie);
186 static void qcom_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
187 unsigned long iova, size_t granule,
190 qcom_iommu_tlb_inv_range_nosync(iova, granule, granule, true, cookie);
193 static const struct iommu_flush_ops qcom_flush_ops = {
194 .tlb_flush_all = qcom_iommu_tlb_inv_context,
195 .tlb_flush_walk = qcom_iommu_tlb_flush_walk,
196 .tlb_add_page = qcom_iommu_tlb_add_page,
199 static irqreturn_t qcom_iommu_fault(int irq, void *dev)
201 struct qcom_iommu_ctx *ctx = dev;
205 fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR);
207 if (!(fsr & ARM_SMMU_FSR_FAULT))
210 fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0);
211 iova = iommu_readq(ctx, ARM_SMMU_CB_FAR);
213 if (!report_iommu_fault(ctx->domain, ctx->dev, iova, 0)) {
214 dev_err_ratelimited(ctx->dev,
215 "Unhandled context fault: fsr=0x%x, "
216 "iova=0x%016llx, fsynr=0x%x, cb=%d\n",
217 fsr, iova, fsynr, ctx->asid);
220 iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr);
221 iommu_writel(ctx, ARM_SMMU_CB_RESUME, ARM_SMMU_RESUME_TERMINATE);
226 static int qcom_iommu_init_domain(struct iommu_domain *domain,
227 struct qcom_iommu_dev *qcom_iommu,
230 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
231 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
232 struct io_pgtable_ops *pgtbl_ops;
233 struct io_pgtable_cfg pgtbl_cfg;
237 mutex_lock(&qcom_domain->init_mutex);
238 if (qcom_domain->iommu)
241 pgtbl_cfg = (struct io_pgtable_cfg) {
242 .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap,
245 .tlb = &qcom_flush_ops,
246 .iommu_dev = qcom_iommu->dev,
249 qcom_domain->iommu = qcom_iommu;
250 qcom_domain->fwspec = fwspec;
252 pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, qcom_domain);
254 dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n");
256 goto out_clear_iommu;
259 /* Update the domain's page sizes to reflect the page table format */
260 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
261 domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1;
262 domain->geometry.force_aperture = true;
264 for (i = 0; i < fwspec->num_ids; i++) {
265 struct qcom_iommu_ctx *ctx = to_ctx(qcom_domain, fwspec->ids[i]);
267 if (!ctx->secure_init) {
268 ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid);
270 dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret);
271 goto out_clear_iommu;
273 ctx->secure_init = true;
277 iommu_writeq(ctx, ARM_SMMU_CB_TTBR0,
278 pgtbl_cfg.arm_lpae_s1_cfg.ttbr |
279 FIELD_PREP(ARM_SMMU_TTBRn_ASID, ctx->asid));
280 iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0);
283 iommu_writel(ctx, ARM_SMMU_CB_TCR2,
284 arm_smmu_lpae_tcr2(&pgtbl_cfg));
285 iommu_writel(ctx, ARM_SMMU_CB_TCR,
286 arm_smmu_lpae_tcr(&pgtbl_cfg) | ARM_SMMU_TCR_EAE);
288 /* MAIRs (stage-1 only) */
289 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0,
290 pgtbl_cfg.arm_lpae_s1_cfg.mair);
291 iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1,
292 pgtbl_cfg.arm_lpae_s1_cfg.mair >> 32);
295 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
296 ARM_SMMU_SCTLR_AFE | ARM_SMMU_SCTLR_TRE |
297 ARM_SMMU_SCTLR_M | ARM_SMMU_SCTLR_S1_ASIDPNE |
298 ARM_SMMU_SCTLR_CFCFG;
300 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
301 reg |= ARM_SMMU_SCTLR_E;
303 iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg);
305 ctx->domain = domain;
308 mutex_unlock(&qcom_domain->init_mutex);
310 /* Publish page table ops for map/unmap */
311 qcom_domain->pgtbl_ops = pgtbl_ops;
316 qcom_domain->iommu = NULL;
318 mutex_unlock(&qcom_domain->init_mutex);
322 static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type)
324 struct qcom_iommu_domain *qcom_domain;
326 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
329 * Allocate the domain and initialise some of its data structures.
330 * We can't really do anything meaningful until we've added a
333 qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL);
337 mutex_init(&qcom_domain->init_mutex);
338 spin_lock_init(&qcom_domain->pgtbl_lock);
340 return &qcom_domain->domain;
343 static void qcom_iommu_domain_free(struct iommu_domain *domain)
345 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
347 if (qcom_domain->iommu) {
349 * NOTE: unmap can be called after client device is powered
350 * off, for example, with GPUs or anything involving dma-buf.
351 * So we cannot rely on the device_link. Make sure the IOMMU
352 * is on to avoid unclocked accesses in the TLB inv path:
354 pm_runtime_get_sync(qcom_domain->iommu->dev);
355 free_io_pgtable_ops(qcom_domain->pgtbl_ops);
356 pm_runtime_put_sync(qcom_domain->iommu->dev);
362 static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
364 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
365 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
369 dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n");
373 /* Ensure that the domain is finalized */
374 pm_runtime_get_sync(qcom_iommu->dev);
375 ret = qcom_iommu_init_domain(domain, qcom_iommu, dev);
376 pm_runtime_put_sync(qcom_iommu->dev);
381 * Sanity check the domain. We don't support domains across
384 if (qcom_domain->iommu != qcom_iommu)
390 static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,
391 phys_addr_t paddr, size_t pgsize, size_t pgcount,
392 int prot, gfp_t gfp, size_t *mapped)
396 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
397 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
402 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
403 ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, GFP_ATOMIC, mapped);
404 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
408 static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
409 size_t pgsize, size_t pgcount,
410 struct iommu_iotlb_gather *gather)
414 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
415 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
420 /* NOTE: unmap can be called after client device is powered off,
421 * for example, with GPUs or anything involving dma-buf. So we
422 * cannot rely on the device_link. Make sure the IOMMU is on to
423 * avoid unclocked accesses in the TLB inv path:
425 pm_runtime_get_sync(qcom_domain->iommu->dev);
426 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
427 ret = ops->unmap_pages(ops, iova, pgsize, pgcount, gather);
428 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
429 pm_runtime_put_sync(qcom_domain->iommu->dev);
434 static void qcom_iommu_flush_iotlb_all(struct iommu_domain *domain)
436 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
437 struct io_pgtable *pgtable = container_of(qcom_domain->pgtbl_ops,
438 struct io_pgtable, ops);
439 if (!qcom_domain->pgtbl_ops)
442 pm_runtime_get_sync(qcom_domain->iommu->dev);
443 qcom_iommu_tlb_sync(pgtable->cookie);
444 pm_runtime_put_sync(qcom_domain->iommu->dev);
447 static void qcom_iommu_iotlb_sync(struct iommu_domain *domain,
448 struct iommu_iotlb_gather *gather)
450 qcom_iommu_flush_iotlb_all(domain);
453 static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain,
458 struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
459 struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops;
464 spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags);
465 ret = ops->iova_to_phys(ops, iova);
466 spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags);
471 static bool qcom_iommu_capable(struct device *dev, enum iommu_cap cap)
474 case IOMMU_CAP_CACHE_COHERENCY:
476 * Return true here as the SMMU can always send out coherent
480 case IOMMU_CAP_NOEXEC:
487 static struct iommu_device *qcom_iommu_probe_device(struct device *dev)
489 struct qcom_iommu_dev *qcom_iommu = to_iommu(dev);
490 struct device_link *link;
493 return ERR_PTR(-ENODEV);
496 * Establish the link between iommu and master, so that the
497 * iommu gets runtime enabled/disabled as per the master's
500 link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME);
502 dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n",
503 dev_name(qcom_iommu->dev), dev_name(dev));
504 return ERR_PTR(-ENODEV);
507 return &qcom_iommu->iommu;
510 static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
512 struct qcom_iommu_dev *qcom_iommu;
513 struct platform_device *iommu_pdev;
514 unsigned asid = args->args[0];
516 if (args->args_count != 1) {
517 dev_err(dev, "incorrect number of iommu params found for %s "
518 "(found %d, expected 1)\n",
519 args->np->full_name, args->args_count);
523 iommu_pdev = of_find_device_by_node(args->np);
524 if (WARN_ON(!iommu_pdev))
527 qcom_iommu = platform_get_drvdata(iommu_pdev);
529 /* make sure the asid specified in dt is valid, so we don't have
530 * to sanity check this elsewhere, since 'asid - 1' is used to
531 * index into qcom_iommu->ctxs:
533 if (WARN_ON(asid < 1) ||
534 WARN_ON(asid > qcom_iommu->num_ctxs)) {
535 put_device(&iommu_pdev->dev);
539 if (!dev_iommu_priv_get(dev)) {
540 dev_iommu_priv_set(dev, qcom_iommu);
542 /* make sure devices iommus dt node isn't referring to
543 * multiple different iommu devices. Multiple context
544 * banks are ok, but multiple devices are not:
546 if (WARN_ON(qcom_iommu != dev_iommu_priv_get(dev))) {
547 put_device(&iommu_pdev->dev);
552 return iommu_fwspec_add_ids(dev, &asid, 1);
555 static const struct iommu_ops qcom_iommu_ops = {
556 .capable = qcom_iommu_capable,
557 .domain_alloc = qcom_iommu_domain_alloc,
558 .probe_device = qcom_iommu_probe_device,
559 .device_group = generic_device_group,
560 .of_xlate = qcom_iommu_of_xlate,
561 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
562 .default_domain_ops = &(const struct iommu_domain_ops) {
563 .attach_dev = qcom_iommu_attach_dev,
564 .map_pages = qcom_iommu_map,
565 .unmap_pages = qcom_iommu_unmap,
566 .flush_iotlb_all = qcom_iommu_flush_iotlb_all,
567 .iotlb_sync = qcom_iommu_iotlb_sync,
568 .iova_to_phys = qcom_iommu_iova_to_phys,
569 .free = qcom_iommu_domain_free,
573 static int qcom_iommu_sec_ptbl_init(struct device *dev)
576 unsigned int spare = 0;
580 static bool allocated = false;
586 ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize);
588 dev_err(dev, "failed to get iommu secure pgtable size (%d)\n",
593 dev_info(dev, "iommu sec: pgtable size: %zu\n", psize);
595 attrs = DMA_ATTR_NO_KERNEL_MAPPING;
597 cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs);
599 dev_err(dev, "failed to allocate %zu bytes for pgtable\n",
604 ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare);
606 dev_err(dev, "failed to init iommu pgtable (%d)\n", ret);
614 dma_free_attrs(dev, psize, cpu_addr, paddr, attrs);
618 static int get_asid(const struct device_node *np)
622 /* read the "reg" property directly to get the relative address
623 * of the context bank, and calculate the asid from that:
625 if (of_property_read_u32_index(np, "reg", 0, ®))
628 return reg / 0x1000; /* context banks are 0x1000 apart */
631 static int qcom_iommu_ctx_probe(struct platform_device *pdev)
633 struct qcom_iommu_ctx *ctx;
634 struct device *dev = &pdev->dev;
635 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent);
636 struct resource *res;
639 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
644 platform_set_drvdata(pdev, ctx);
646 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
647 ctx->base = devm_ioremap_resource(dev, res);
648 if (IS_ERR(ctx->base))
649 return PTR_ERR(ctx->base);
651 irq = platform_get_irq(pdev, 0);
655 /* clear IRQs before registering fault handler, just in case the
656 * boot-loader left us a surprise:
658 iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR));
660 ret = devm_request_irq(dev, irq,
666 dev_err(dev, "failed to request IRQ %u\n", irq);
670 ret = get_asid(dev->of_node);
672 dev_err(dev, "missing reg property\n");
678 dev_dbg(dev, "found asid %u\n", ctx->asid);
680 qcom_iommu->ctxs[ctx->asid - 1] = ctx;
685 static int qcom_iommu_ctx_remove(struct platform_device *pdev)
687 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent);
688 struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev);
690 platform_set_drvdata(pdev, NULL);
692 qcom_iommu->ctxs[ctx->asid - 1] = NULL;
697 static const struct of_device_id ctx_of_match[] = {
698 { .compatible = "qcom,msm-iommu-v1-ns" },
699 { .compatible = "qcom,msm-iommu-v1-sec" },
703 static struct platform_driver qcom_iommu_ctx_driver = {
705 .name = "qcom-iommu-ctx",
706 .of_match_table = ctx_of_match,
708 .probe = qcom_iommu_ctx_probe,
709 .remove = qcom_iommu_ctx_remove,
712 static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu)
714 struct device_node *child;
716 for_each_child_of_node(qcom_iommu->dev->of_node, child) {
717 if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) {
726 static int qcom_iommu_device_probe(struct platform_device *pdev)
728 struct device_node *child;
729 struct qcom_iommu_dev *qcom_iommu;
730 struct device *dev = &pdev->dev;
731 struct resource *res;
733 int ret, max_asid = 0;
735 /* find the max asid (which is 1:1 to ctx bank idx), so we know how
736 * many child ctx devices we have:
738 for_each_child_of_node(dev->of_node, child)
739 max_asid = max(max_asid, get_asid(child));
741 qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid),
745 qcom_iommu->num_ctxs = max_asid;
746 qcom_iommu->dev = dev;
748 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
750 qcom_iommu->local_base = devm_ioremap_resource(dev, res);
751 if (IS_ERR(qcom_iommu->local_base))
752 return PTR_ERR(qcom_iommu->local_base);
755 clk = devm_clk_get(dev, "iface");
757 dev_err(dev, "failed to get iface clock\n");
760 qcom_iommu->clks[CLK_IFACE].clk = clk;
762 clk = devm_clk_get(dev, "bus");
764 dev_err(dev, "failed to get bus clock\n");
767 qcom_iommu->clks[CLK_BUS].clk = clk;
769 clk = devm_clk_get_optional(dev, "tbu");
771 dev_err(dev, "failed to get tbu clock\n");
774 qcom_iommu->clks[CLK_TBU].clk = clk;
776 if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id",
777 &qcom_iommu->sec_id)) {
778 dev_err(dev, "missing qcom,iommu-secure-id property\n");
782 if (qcom_iommu_has_secure_context(qcom_iommu)) {
783 ret = qcom_iommu_sec_ptbl_init(dev);
785 dev_err(dev, "cannot init secure pg table(%d)\n", ret);
790 platform_set_drvdata(pdev, qcom_iommu);
792 pm_runtime_enable(dev);
794 /* register context bank devices, which are child nodes: */
795 ret = devm_of_platform_populate(dev);
797 dev_err(dev, "Failed to populate iommu contexts\n");
801 ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL,
804 dev_err(dev, "Failed to register iommu in sysfs\n");
808 ret = iommu_device_register(&qcom_iommu->iommu, &qcom_iommu_ops, dev);
810 dev_err(dev, "Failed to register iommu\n");
814 if (qcom_iommu->local_base) {
815 pm_runtime_get_sync(dev);
816 writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS);
817 pm_runtime_put_sync(dev);
823 pm_runtime_disable(dev);
827 static int qcom_iommu_device_remove(struct platform_device *pdev)
829 struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev);
831 pm_runtime_force_suspend(&pdev->dev);
832 platform_set_drvdata(pdev, NULL);
833 iommu_device_sysfs_remove(&qcom_iommu->iommu);
834 iommu_device_unregister(&qcom_iommu->iommu);
839 static int __maybe_unused qcom_iommu_resume(struct device *dev)
841 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
843 return clk_bulk_prepare_enable(CLK_NUM, qcom_iommu->clks);
846 static int __maybe_unused qcom_iommu_suspend(struct device *dev)
848 struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev);
850 clk_bulk_disable_unprepare(CLK_NUM, qcom_iommu->clks);
855 static const struct dev_pm_ops qcom_iommu_pm_ops = {
856 SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL)
857 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
858 pm_runtime_force_resume)
861 static const struct of_device_id qcom_iommu_of_match[] = {
862 { .compatible = "qcom,msm-iommu-v1" },
866 static struct platform_driver qcom_iommu_driver = {
868 .name = "qcom-iommu",
869 .of_match_table = qcom_iommu_of_match,
870 .pm = &qcom_iommu_pm_ops,
872 .probe = qcom_iommu_device_probe,
873 .remove = qcom_iommu_device_remove,
876 static int __init qcom_iommu_init(void)
880 ret = platform_driver_register(&qcom_iommu_ctx_driver);
884 ret = platform_driver_register(&qcom_iommu_driver);
886 platform_driver_unregister(&qcom_iommu_ctx_driver);
890 device_initcall(qcom_iommu_init);