1 // SPDX-License-Identifier: GPL-2.0
3 * IOMMU API for Renesas VMSA-compatible IPMMU
4 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
6 * Copyright (C) 2014-2020 Renesas Electronics Corporation
9 #include <linux/bitmap.h>
10 #include <linux/delay.h>
11 #include <linux/dma-iommu.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/io-pgtable.h>
19 #include <linux/iommu.h>
21 #include <linux/of_device.h>
22 #include <linux/of_iommu.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/sizes.h>
26 #include <linux/slab.h>
27 #include <linux/sys_soc.h>
29 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
30 #include <asm/dma-iommu.h>
32 #define arm_iommu_create_mapping(...) NULL
33 #define arm_iommu_attach_device(...) -ENODEV
34 #define arm_iommu_release_mapping(...) do {} while (0)
35 #define arm_iommu_detach_device(...) do {} while (0)
38 #define IPMMU_CTX_MAX 8U
39 #define IPMMU_CTX_INVALID -1
41 #define IPMMU_UTLB_MAX 48U
43 struct ipmmu_features {
44 bool use_ns_alias_offset;
45 bool has_cache_leaf_nodes;
46 unsigned int number_of_contexts;
47 unsigned int num_utlbs;
49 bool twobit_imttbcr_sl0;
50 bool reserved_context;
52 unsigned int ctx_offset_base;
53 unsigned int ctx_offset_stride;
54 unsigned int utlb_offset_base;
57 struct ipmmu_vmsa_device {
60 struct iommu_device iommu;
61 struct ipmmu_vmsa_device *root;
62 const struct ipmmu_features *features;
64 spinlock_t lock; /* Protects ctx and domains[] */
65 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
66 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
67 s8 utlb_ctx[IPMMU_UTLB_MAX];
69 struct iommu_group *group;
70 struct dma_iommu_mapping *mapping;
73 struct ipmmu_vmsa_domain {
74 struct ipmmu_vmsa_device *mmu;
75 struct iommu_domain io_domain;
77 struct io_pgtable_cfg cfg;
78 struct io_pgtable_ops *iop;
80 unsigned int context_id;
81 struct mutex mutex; /* Protects mappings */
84 static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
86 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
89 static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
91 return dev_iommu_priv_get(dev);
94 #define TLB_LOOP_TIMEOUT 100 /* 100us */
96 /* -----------------------------------------------------------------------------
97 * Registers Definition
100 #define IM_NS_ALIAS_OFFSET 0x800
102 /* MMU "context" registers */
103 #define IMCTR 0x0000 /* R-Car Gen2/3 */
104 #define IMCTR_INTEN (1 << 2) /* R-Car Gen2/3 */
105 #define IMCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
106 #define IMCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
108 #define IMTTBCR 0x0008 /* R-Car Gen2/3 */
109 #define IMTTBCR_EAE (1 << 31) /* R-Car Gen2/3 */
110 #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) /* R-Car Gen2 only */
111 #define IMTTBCR_ORGN0_WB_WA (1 << 10) /* R-Car Gen2 only */
112 #define IMTTBCR_IRGN0_WB_WA (1 << 8) /* R-Car Gen2 only */
113 #define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6) /* R-Car Gen3 only */
114 #define IMTTBCR_SL0_LVL_1 (1 << 4) /* R-Car Gen2 only */
116 #define IMBUSCR 0x000c /* R-Car Gen2 only */
117 #define IMBUSCR_DVM (1 << 2) /* R-Car Gen2 only */
118 #define IMBUSCR_BUSSEL_MASK (3 << 0) /* R-Car Gen2 only */
120 #define IMTTLBR0 0x0010 /* R-Car Gen2/3 */
121 #define IMTTUBR0 0x0014 /* R-Car Gen2/3 */
123 #define IMSTR 0x0020 /* R-Car Gen2/3 */
124 #define IMSTR_MHIT (1 << 4) /* R-Car Gen2/3 */
125 #define IMSTR_ABORT (1 << 2) /* R-Car Gen2/3 */
126 #define IMSTR_PF (1 << 1) /* R-Car Gen2/3 */
127 #define IMSTR_TF (1 << 0) /* R-Car Gen2/3 */
129 #define IMMAIR0 0x0028 /* R-Car Gen2/3 */
131 #define IMELAR 0x0030 /* R-Car Gen2/3, IMEAR on R-Car Gen2 */
132 #define IMEUAR 0x0034 /* R-Car Gen3 only */
135 #define IMUCTR(n) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
136 #define IMUCTR0(n) (0x0300 + ((n) * 16)) /* R-Car Gen2/3 */
137 #define IMUCTR32(n) (0x0600 + (((n) - 32) * 16)) /* R-Car Gen3 only */
138 #define IMUCTR_TTSEL_MMU(n) ((n) << 4) /* R-Car Gen2/3 */
139 #define IMUCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
140 #define IMUCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
142 #define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n))
143 #define IMUASID0(n) (0x0308 + ((n) * 16)) /* R-Car Gen2/3 */
144 #define IMUASID32(n) (0x0608 + (((n) - 32) * 16)) /* R-Car Gen3 only */
146 /* -----------------------------------------------------------------------------
147 * Root device handling
150 static struct platform_driver ipmmu_driver;
152 static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
154 return mmu->root == mmu;
157 static int __ipmmu_check_device(struct device *dev, void *data)
159 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
160 struct ipmmu_vmsa_device **rootp = data;
162 if (ipmmu_is_root(mmu))
168 static struct ipmmu_vmsa_device *ipmmu_find_root(void)
170 struct ipmmu_vmsa_device *root = NULL;
172 return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
173 __ipmmu_check_device) == 0 ? root : NULL;
176 /* -----------------------------------------------------------------------------
180 static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
182 return ioread32(mmu->base + offset);
185 static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
188 iowrite32(data, mmu->base + offset);
191 static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device *mmu,
192 unsigned int context_id, unsigned int reg)
194 return mmu->features->ctx_offset_base +
195 context_id * mmu->features->ctx_offset_stride + reg;
198 static u32 ipmmu_ctx_read(struct ipmmu_vmsa_device *mmu,
199 unsigned int context_id, unsigned int reg)
201 return ipmmu_read(mmu, ipmmu_ctx_reg(mmu, context_id, reg));
204 static void ipmmu_ctx_write(struct ipmmu_vmsa_device *mmu,
205 unsigned int context_id, unsigned int reg, u32 data)
207 ipmmu_write(mmu, ipmmu_ctx_reg(mmu, context_id, reg), data);
210 static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
213 return ipmmu_ctx_read(domain->mmu->root, domain->context_id, reg);
216 static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
217 unsigned int reg, u32 data)
219 ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
222 static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
223 unsigned int reg, u32 data)
225 if (domain->mmu != domain->mmu->root)
226 ipmmu_ctx_write(domain->mmu, domain->context_id, reg, data);
228 ipmmu_ctx_write(domain->mmu->root, domain->context_id, reg, data);
231 static u32 ipmmu_utlb_reg(struct ipmmu_vmsa_device *mmu, unsigned int reg)
233 return mmu->features->utlb_offset_base + reg;
236 static void ipmmu_imuasid_write(struct ipmmu_vmsa_device *mmu,
237 unsigned int utlb, u32 data)
239 ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUASID(utlb)), data);
242 static void ipmmu_imuctr_write(struct ipmmu_vmsa_device *mmu,
243 unsigned int utlb, u32 data)
245 ipmmu_write(mmu, ipmmu_utlb_reg(mmu, IMUCTR(utlb)), data);
248 /* -----------------------------------------------------------------------------
249 * TLB and microTLB Management
252 /* Wait for any pending TLB invalidations to complete */
253 static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
255 unsigned int count = 0;
257 while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) {
259 if (++count == TLB_LOOP_TIMEOUT) {
260 dev_err_ratelimited(domain->mmu->dev,
261 "TLB sync timed out -- MMU may be deadlocked\n");
268 static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
272 reg = ipmmu_ctx_read_root(domain, IMCTR);
274 ipmmu_ctx_write_all(domain, IMCTR, reg);
276 ipmmu_tlb_sync(domain);
280 * Enable MMU translation for the microTLB.
282 static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
285 struct ipmmu_vmsa_device *mmu = domain->mmu;
288 * TODO: Reference-count the microTLB as several bus masters can be
289 * connected to the same microTLB.
292 /* TODO: What should we set the ASID to ? */
293 ipmmu_imuasid_write(mmu, utlb, 0);
294 /* TODO: Do we need to flush the microTLB ? */
295 ipmmu_imuctr_write(mmu, utlb, IMUCTR_TTSEL_MMU(domain->context_id) |
296 IMUCTR_FLUSH | IMUCTR_MMUEN);
297 mmu->utlb_ctx[utlb] = domain->context_id;
301 * Disable MMU translation for the microTLB.
303 static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
306 struct ipmmu_vmsa_device *mmu = domain->mmu;
308 ipmmu_imuctr_write(mmu, utlb, 0);
309 mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID;
312 static void ipmmu_tlb_flush_all(void *cookie)
314 struct ipmmu_vmsa_domain *domain = cookie;
316 ipmmu_tlb_invalidate(domain);
319 static void ipmmu_tlb_flush(unsigned long iova, size_t size,
320 size_t granule, void *cookie)
322 ipmmu_tlb_flush_all(cookie);
325 static const struct iommu_flush_ops ipmmu_flush_ops = {
326 .tlb_flush_all = ipmmu_tlb_flush_all,
327 .tlb_flush_walk = ipmmu_tlb_flush,
328 .tlb_flush_leaf = ipmmu_tlb_flush,
331 /* -----------------------------------------------------------------------------
332 * Domain/Context Management
335 static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
336 struct ipmmu_vmsa_domain *domain)
341 spin_lock_irqsave(&mmu->lock, flags);
343 ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
344 if (ret != mmu->num_ctx) {
345 mmu->domains[ret] = domain;
346 set_bit(ret, mmu->ctx);
350 spin_unlock_irqrestore(&mmu->lock, flags);
355 static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
356 unsigned int context_id)
360 spin_lock_irqsave(&mmu->lock, flags);
362 clear_bit(context_id, mmu->ctx);
363 mmu->domains[context_id] = NULL;
365 spin_unlock_irqrestore(&mmu->lock, flags);
368 static void ipmmu_domain_setup_context(struct ipmmu_vmsa_domain *domain)
374 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr;
375 ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
376 ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
380 * We use long descriptors and allocate the whole 32-bit VA space to
383 if (domain->mmu->features->twobit_imttbcr_sl0)
384 tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
386 tmp = IMTTBCR_SL0_LVL_1;
388 if (domain->mmu->features->cache_snoop)
389 tmp |= IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
392 ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE | tmp);
395 ipmmu_ctx_write_root(domain, IMMAIR0,
396 domain->cfg.arm_lpae_s1_cfg.mair);
399 if (domain->mmu->features->setup_imbuscr)
400 ipmmu_ctx_write_root(domain, IMBUSCR,
401 ipmmu_ctx_read_root(domain, IMBUSCR) &
402 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
406 * Clear all interrupt flags.
408 ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
412 * Enable the MMU and interrupt generation. The long-descriptor
413 * translation table format doesn't use TEX remapping. Don't enable AF
414 * software management as we have no use for it. Flush the TLB as
415 * required when modifying the context registers.
417 ipmmu_ctx_write_all(domain, IMCTR,
418 IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
421 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
426 * Allocate the page table operations.
428 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
429 * access, Long-descriptor format" that the NStable bit being set in a
430 * table descriptor will result in the NStable and NS bits of all child
431 * entries being ignored and considered as being set. The IPMMU seems
432 * not to comply with this, as it generates a secure access page fault
433 * if any of the NStable and NS bits isn't set when running in
436 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
437 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
438 domain->cfg.ias = 32;
439 domain->cfg.oas = 40;
440 domain->cfg.tlb = &ipmmu_flush_ops;
441 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
442 domain->io_domain.geometry.force_aperture = true;
444 * TODO: Add support for coherent walk through CCI with DVM and remove
445 * cache handling. For now, delegate it to the io-pgtable code.
447 domain->cfg.coherent_walk = false;
448 domain->cfg.iommu_dev = domain->mmu->root->dev;
451 * Find an unused context.
453 ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
457 domain->context_id = ret;
459 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
462 ipmmu_domain_free_context(domain->mmu->root,
467 ipmmu_domain_setup_context(domain);
471 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
477 * Disable the context. Flush the TLB as required when modifying the
480 * TODO: Is TLB flush really needed ?
482 ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
483 ipmmu_tlb_sync(domain);
484 ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
487 /* -----------------------------------------------------------------------------
491 static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
493 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
494 struct ipmmu_vmsa_device *mmu = domain->mmu;
498 status = ipmmu_ctx_read_root(domain, IMSTR);
499 if (!(status & err_mask))
502 iova = ipmmu_ctx_read_root(domain, IMELAR);
503 if (IS_ENABLED(CONFIG_64BIT))
504 iova |= (u64)ipmmu_ctx_read_root(domain, IMEUAR) << 32;
507 * Clear the error status flags. Unlike traditional interrupt flag
508 * registers that must be cleared by writing 1, this status register
509 * seems to require 0. The error address register must be read before,
510 * otherwise its value will be 0.
512 ipmmu_ctx_write_root(domain, IMSTR, 0);
514 /* Log fatal errors. */
515 if (status & IMSTR_MHIT)
516 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%lx\n",
518 if (status & IMSTR_ABORT)
519 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%lx\n",
522 if (!(status & (IMSTR_PF | IMSTR_TF)))
526 * Try to handle page faults and translation faults.
528 * TODO: We need to look up the faulty device based on the I/O VA. Use
529 * the IOMMU device for now.
531 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
534 dev_err_ratelimited(mmu->dev,
535 "Unhandled fault: status 0x%08x iova 0x%lx\n",
541 static irqreturn_t ipmmu_irq(int irq, void *dev)
543 struct ipmmu_vmsa_device *mmu = dev;
544 irqreturn_t status = IRQ_NONE;
548 spin_lock_irqsave(&mmu->lock, flags);
551 * Check interrupts for all active contexts.
553 for (i = 0; i < mmu->num_ctx; i++) {
554 if (!mmu->domains[i])
556 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
557 status = IRQ_HANDLED;
560 spin_unlock_irqrestore(&mmu->lock, flags);
565 /* -----------------------------------------------------------------------------
569 static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
571 struct ipmmu_vmsa_domain *domain;
573 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
577 mutex_init(&domain->mutex);
579 return &domain->io_domain;
582 static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
584 struct iommu_domain *io_domain = NULL;
587 case IOMMU_DOMAIN_UNMANAGED:
588 io_domain = __ipmmu_domain_alloc(type);
591 case IOMMU_DOMAIN_DMA:
592 io_domain = __ipmmu_domain_alloc(type);
593 if (io_domain && iommu_get_dma_cookie(io_domain)) {
603 static void ipmmu_domain_free(struct iommu_domain *io_domain)
605 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
608 * Free the domain resources. We assume that all devices have already
611 iommu_put_dma_cookie(io_domain);
612 ipmmu_domain_destroy_context(domain);
613 free_io_pgtable_ops(domain->iop);
617 static int ipmmu_attach_device(struct iommu_domain *io_domain,
620 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
621 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
622 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
627 dev_err(dev, "Cannot attach to IPMMU\n");
631 mutex_lock(&domain->mutex);
634 /* The domain hasn't been used yet, initialize it. */
636 ret = ipmmu_domain_init_context(domain);
638 dev_err(dev, "Unable to initialize IPMMU context\n");
641 dev_info(dev, "Using IPMMU context %u\n",
644 } else if (domain->mmu != mmu) {
646 * Something is wrong, we can't attach two devices using
647 * different IOMMUs to the same domain.
649 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
650 dev_name(mmu->dev), dev_name(domain->mmu->dev));
653 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
655 mutex_unlock(&domain->mutex);
660 for (i = 0; i < fwspec->num_ids; ++i)
661 ipmmu_utlb_enable(domain, fwspec->ids[i]);
666 static void ipmmu_detach_device(struct iommu_domain *io_domain,
669 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
670 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
673 for (i = 0; i < fwspec->num_ids; ++i)
674 ipmmu_utlb_disable(domain, fwspec->ids[i]);
677 * TODO: Optimize by disabling the context when no device is attached.
681 static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
682 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
684 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
689 return domain->iop->map(domain->iop, iova, paddr, size, prot, gfp);
692 static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
693 size_t size, struct iommu_iotlb_gather *gather)
695 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
697 return domain->iop->unmap(domain->iop, iova, size, gather);
700 static void ipmmu_flush_iotlb_all(struct iommu_domain *io_domain)
702 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
705 ipmmu_tlb_flush_all(domain);
708 static void ipmmu_iotlb_sync(struct iommu_domain *io_domain,
709 struct iommu_iotlb_gather *gather)
711 ipmmu_flush_iotlb_all(io_domain);
714 static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
717 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
719 /* TODO: Is locking needed ? */
721 return domain->iop->iova_to_phys(domain->iop, iova);
724 static int ipmmu_init_platform_device(struct device *dev,
725 struct of_phandle_args *args)
727 struct platform_device *ipmmu_pdev;
729 ipmmu_pdev = of_find_device_by_node(args->np);
733 dev_iommu_priv_set(dev, platform_get_drvdata(ipmmu_pdev));
738 static const struct soc_device_attribute soc_rcar_gen3[] = {
739 { .soc_id = "r8a774a1", },
740 { .soc_id = "r8a774b1", },
741 { .soc_id = "r8a774c0", },
742 { .soc_id = "r8a774e1", },
743 { .soc_id = "r8a7795", },
744 { .soc_id = "r8a77961", },
745 { .soc_id = "r8a7796", },
746 { .soc_id = "r8a77965", },
747 { .soc_id = "r8a77970", },
748 { .soc_id = "r8a77990", },
749 { .soc_id = "r8a77995", },
753 static const struct soc_device_attribute soc_rcar_gen3_whitelist[] = {
754 { .soc_id = "r8a774b1", },
755 { .soc_id = "r8a774c0", },
756 { .soc_id = "r8a774e1", },
757 { .soc_id = "r8a7795", .revision = "ES3.*" },
758 { .soc_id = "r8a77961", },
759 { .soc_id = "r8a77965", },
760 { .soc_id = "r8a77990", },
761 { .soc_id = "r8a77995", },
765 static const char * const rcar_gen3_slave_whitelist[] = {
768 static bool ipmmu_slave_whitelist(struct device *dev)
773 * For R-Car Gen3 use a white list to opt-in slave devices.
774 * For Other SoCs, this returns true anyway.
776 if (!soc_device_match(soc_rcar_gen3))
779 /* Check whether this R-Car Gen3 can use the IPMMU correctly or not */
780 if (!soc_device_match(soc_rcar_gen3_whitelist))
783 /* Check whether this slave device can work with the IPMMU */
784 for (i = 0; i < ARRAY_SIZE(rcar_gen3_slave_whitelist); i++) {
785 if (!strcmp(dev_name(dev), rcar_gen3_slave_whitelist[i]))
789 /* Otherwise, do not allow use of IPMMU */
793 static int ipmmu_of_xlate(struct device *dev,
794 struct of_phandle_args *spec)
796 if (!ipmmu_slave_whitelist(dev))
799 iommu_fwspec_add_ids(dev, spec->args, 1);
801 /* Initialize once - xlate() will call multiple times */
805 return ipmmu_init_platform_device(dev, spec);
808 static int ipmmu_init_arm_mapping(struct device *dev)
810 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
814 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
815 * VAs. This will allocate a corresponding IOMMU domain.
818 * - Create one mapping per context (TLB).
819 * - Make the mapping size configurable ? We currently use a 2GB mapping
820 * at a 1GB offset to ensure that NULL VAs will fault.
823 struct dma_iommu_mapping *mapping;
825 mapping = arm_iommu_create_mapping(&platform_bus_type,
827 if (IS_ERR(mapping)) {
828 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
829 ret = PTR_ERR(mapping);
833 mmu->mapping = mapping;
836 /* Attach the ARM VA mapping to the device. */
837 ret = arm_iommu_attach_device(dev, mmu->mapping);
839 dev_err(dev, "Failed to attach device to VA mapping\n");
847 arm_iommu_release_mapping(mmu->mapping);
852 static struct iommu_device *ipmmu_probe_device(struct device *dev)
854 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
857 * Only let through devices that have been verified in xlate()
860 return ERR_PTR(-ENODEV);
865 static void ipmmu_probe_finalize(struct device *dev)
869 if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
870 ret = ipmmu_init_arm_mapping(dev);
873 dev_err(dev, "Can't create IOMMU mapping - DMA-OPS will not work\n");
876 static void ipmmu_release_device(struct device *dev)
878 arm_iommu_detach_device(dev);
881 static struct iommu_group *ipmmu_find_group(struct device *dev)
883 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
884 struct iommu_group *group;
887 return iommu_group_ref_get(mmu->group);
889 group = iommu_group_alloc();
896 static const struct iommu_ops ipmmu_ops = {
897 .domain_alloc = ipmmu_domain_alloc,
898 .domain_free = ipmmu_domain_free,
899 .attach_dev = ipmmu_attach_device,
900 .detach_dev = ipmmu_detach_device,
902 .unmap = ipmmu_unmap,
903 .flush_iotlb_all = ipmmu_flush_iotlb_all,
904 .iotlb_sync = ipmmu_iotlb_sync,
905 .iova_to_phys = ipmmu_iova_to_phys,
906 .probe_device = ipmmu_probe_device,
907 .release_device = ipmmu_release_device,
908 .probe_finalize = ipmmu_probe_finalize,
909 .device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)
910 ? generic_device_group : ipmmu_find_group,
911 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
912 .of_xlate = ipmmu_of_xlate,
915 /* -----------------------------------------------------------------------------
916 * Probe/remove and init
919 static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
923 /* Disable all contexts. */
924 for (i = 0; i < mmu->num_ctx; ++i)
925 ipmmu_ctx_write(mmu, i, IMCTR, 0);
928 static const struct ipmmu_features ipmmu_features_default = {
929 .use_ns_alias_offset = true,
930 .has_cache_leaf_nodes = false,
931 .number_of_contexts = 1, /* software only tested with one context */
933 .setup_imbuscr = true,
934 .twobit_imttbcr_sl0 = false,
935 .reserved_context = false,
937 .ctx_offset_base = 0,
938 .ctx_offset_stride = 0x40,
939 .utlb_offset_base = 0,
942 static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
943 .use_ns_alias_offset = false,
944 .has_cache_leaf_nodes = true,
945 .number_of_contexts = 8,
947 .setup_imbuscr = false,
948 .twobit_imttbcr_sl0 = true,
949 .reserved_context = true,
950 .cache_snoop = false,
951 .ctx_offset_base = 0,
952 .ctx_offset_stride = 0x40,
953 .utlb_offset_base = 0,
956 static const struct of_device_id ipmmu_of_ids[] = {
958 .compatible = "renesas,ipmmu-vmsa",
959 .data = &ipmmu_features_default,
961 .compatible = "renesas,ipmmu-r8a774a1",
962 .data = &ipmmu_features_rcar_gen3,
964 .compatible = "renesas,ipmmu-r8a774b1",
965 .data = &ipmmu_features_rcar_gen3,
967 .compatible = "renesas,ipmmu-r8a774c0",
968 .data = &ipmmu_features_rcar_gen3,
970 .compatible = "renesas,ipmmu-r8a774e1",
971 .data = &ipmmu_features_rcar_gen3,
973 .compatible = "renesas,ipmmu-r8a7795",
974 .data = &ipmmu_features_rcar_gen3,
976 .compatible = "renesas,ipmmu-r8a7796",
977 .data = &ipmmu_features_rcar_gen3,
979 .compatible = "renesas,ipmmu-r8a77961",
980 .data = &ipmmu_features_rcar_gen3,
982 .compatible = "renesas,ipmmu-r8a77965",
983 .data = &ipmmu_features_rcar_gen3,
985 .compatible = "renesas,ipmmu-r8a77970",
986 .data = &ipmmu_features_rcar_gen3,
988 .compatible = "renesas,ipmmu-r8a77990",
989 .data = &ipmmu_features_rcar_gen3,
991 .compatible = "renesas,ipmmu-r8a77995",
992 .data = &ipmmu_features_rcar_gen3,
998 static int ipmmu_probe(struct platform_device *pdev)
1000 struct ipmmu_vmsa_device *mmu;
1001 struct resource *res;
1005 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
1007 dev_err(&pdev->dev, "cannot allocate device data\n");
1011 mmu->dev = &pdev->dev;
1012 spin_lock_init(&mmu->lock);
1013 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
1014 mmu->features = of_device_get_match_data(&pdev->dev);
1015 memset(mmu->utlb_ctx, IPMMU_CTX_INVALID, mmu->features->num_utlbs);
1016 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
1020 /* Map I/O memory and request IRQ. */
1021 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1022 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1023 if (IS_ERR(mmu->base))
1024 return PTR_ERR(mmu->base);
1027 * The IPMMU has two register banks, for secure and non-secure modes.
1028 * The bank mapped at the beginning of the IPMMU address space
1029 * corresponds to the running mode of the CPU. When running in secure
1030 * mode the non-secure register bank is also available at an offset.
1032 * Secure mode operation isn't clearly documented and is thus currently
1033 * not implemented in the driver. Furthermore, preliminary tests of
1034 * non-secure operation with the main register bank were not successful.
1035 * Offset the registers base unconditionally to point to the non-secure
1036 * alias space for now.
1038 if (mmu->features->use_ns_alias_offset)
1039 mmu->base += IM_NS_ALIAS_OFFSET;
1041 mmu->num_ctx = min(IPMMU_CTX_MAX, mmu->features->number_of_contexts);
1044 * Determine if this IPMMU instance is a root device by checking for
1045 * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
1047 if (!mmu->features->has_cache_leaf_nodes ||
1048 !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL))
1051 mmu->root = ipmmu_find_root();
1054 * Wait until the root device has been registered for sure.
1057 return -EPROBE_DEFER;
1059 /* Root devices have mandatory IRQs */
1060 if (ipmmu_is_root(mmu)) {
1061 irq = platform_get_irq(pdev, 0);
1065 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1066 dev_name(&pdev->dev), mmu);
1068 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1072 ipmmu_device_reset(mmu);
1074 if (mmu->features->reserved_context) {
1075 dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
1076 set_bit(0, mmu->ctx);
1081 * Register the IPMMU to the IOMMU subsystem in the following cases:
1082 * - R-Car Gen2 IPMMU (all devices registered)
1083 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
1085 if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
1086 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
1087 dev_name(&pdev->dev));
1091 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
1092 iommu_device_set_fwnode(&mmu->iommu,
1093 &pdev->dev.of_node->fwnode);
1095 ret = iommu_device_register(&mmu->iommu);
1099 #if defined(CONFIG_IOMMU_DMA)
1100 if (!iommu_present(&platform_bus_type))
1101 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1106 * We can't create the ARM mapping here as it requires the bus to have
1107 * an IOMMU, which only happens when bus_set_iommu() is called in
1108 * ipmmu_init() after the probe function returns.
1111 platform_set_drvdata(pdev, mmu);
1116 static int ipmmu_remove(struct platform_device *pdev)
1118 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1120 iommu_device_sysfs_remove(&mmu->iommu);
1121 iommu_device_unregister(&mmu->iommu);
1123 arm_iommu_release_mapping(mmu->mapping);
1125 ipmmu_device_reset(mmu);
1130 #ifdef CONFIG_PM_SLEEP
1131 static int ipmmu_resume_noirq(struct device *dev)
1133 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
1136 /* Reset root MMU and restore contexts */
1137 if (ipmmu_is_root(mmu)) {
1138 ipmmu_device_reset(mmu);
1140 for (i = 0; i < mmu->num_ctx; i++) {
1141 if (!mmu->domains[i])
1144 ipmmu_domain_setup_context(mmu->domains[i]);
1148 /* Re-enable active micro-TLBs */
1149 for (i = 0; i < mmu->features->num_utlbs; i++) {
1150 if (mmu->utlb_ctx[i] == IPMMU_CTX_INVALID)
1153 ipmmu_utlb_enable(mmu->root->domains[mmu->utlb_ctx[i]], i);
1159 static const struct dev_pm_ops ipmmu_pm = {
1160 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, ipmmu_resume_noirq)
1162 #define DEV_PM_OPS &ipmmu_pm
1164 #define DEV_PM_OPS NULL
1165 #endif /* CONFIG_PM_SLEEP */
1167 static struct platform_driver ipmmu_driver = {
1169 .name = "ipmmu-vmsa",
1170 .of_match_table = of_match_ptr(ipmmu_of_ids),
1173 .probe = ipmmu_probe,
1174 .remove = ipmmu_remove,
1177 static int __init ipmmu_init(void)
1179 struct device_node *np;
1180 static bool setup_done;
1186 np = of_find_matching_node(NULL, ipmmu_of_ids);
1192 ret = platform_driver_register(&ipmmu_driver);
1196 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
1197 if (!iommu_present(&platform_bus_type))
1198 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1204 subsys_initcall(ipmmu_init);