4 * Copyright (C) 2014 Renesas Electronics Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
11 #include <linux/bitmap.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/export.h>
17 #include <linux/interrupt.h>
19 #include <linux/iommu.h>
20 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/of_iommu.h>
24 #include <linux/of_platform.h>
25 #include <linux/platform_device.h>
26 #include <linux/sizes.h>
27 #include <linux/slab.h>
28 #include <linux/sys_soc.h>
30 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
31 #include <asm/dma-iommu.h>
32 #include <asm/pgalloc.h>
34 #define arm_iommu_create_mapping(...) NULL
35 #define arm_iommu_attach_device(...) -ENODEV
36 #define arm_iommu_release_mapping(...) do {} while (0)
37 #define arm_iommu_detach_device(...) do {} while (0)
40 #include "io-pgtable.h"
42 #define IPMMU_CTX_MAX 8
44 struct ipmmu_features {
45 bool use_ns_alias_offset;
46 bool has_cache_leaf_nodes;
47 unsigned int number_of_contexts;
49 bool twobit_imttbcr_sl0;
52 struct ipmmu_vmsa_device {
55 struct iommu_device iommu;
56 struct ipmmu_vmsa_device *root;
57 const struct ipmmu_features *features;
58 unsigned int num_utlbs;
60 spinlock_t lock; /* Protects ctx and domains[] */
61 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
62 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
64 struct iommu_group *group;
65 struct dma_iommu_mapping *mapping;
68 struct ipmmu_vmsa_domain {
69 struct ipmmu_vmsa_device *mmu;
70 struct iommu_domain io_domain;
72 struct io_pgtable_cfg cfg;
73 struct io_pgtable_ops *iop;
75 unsigned int context_id;
76 spinlock_t lock; /* Protects mappings */
79 static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
81 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
84 static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
86 return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL;
89 #define TLB_LOOP_TIMEOUT 100 /* 100us */
91 /* -----------------------------------------------------------------------------
92 * Registers Definition
95 #define IM_NS_ALIAS_OFFSET 0x800
97 #define IM_CTX_SIZE 0x40
100 #define IMCTR_TRE (1 << 17)
101 #define IMCTR_AFE (1 << 16)
102 #define IMCTR_RTSEL_MASK (3 << 4)
103 #define IMCTR_RTSEL_SHIFT 4
104 #define IMCTR_TREN (1 << 3)
105 #define IMCTR_INTEN (1 << 2)
106 #define IMCTR_FLUSH (1 << 1)
107 #define IMCTR_MMUEN (1 << 0)
109 #define IMCAAR 0x0004
111 #define IMTTBCR 0x0008
112 #define IMTTBCR_EAE (1 << 31)
113 #define IMTTBCR_PMB (1 << 30)
114 #define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
115 #define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
116 #define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
117 #define IMTTBCR_SH1_MASK (3 << 28)
118 #define IMTTBCR_ORGN1_NC (0 << 26)
119 #define IMTTBCR_ORGN1_WB_WA (1 << 26)
120 #define IMTTBCR_ORGN1_WT (2 << 26)
121 #define IMTTBCR_ORGN1_WB (3 << 26)
122 #define IMTTBCR_ORGN1_MASK (3 << 26)
123 #define IMTTBCR_IRGN1_NC (0 << 24)
124 #define IMTTBCR_IRGN1_WB_WA (1 << 24)
125 #define IMTTBCR_IRGN1_WT (2 << 24)
126 #define IMTTBCR_IRGN1_WB (3 << 24)
127 #define IMTTBCR_IRGN1_MASK (3 << 24)
128 #define IMTTBCR_TSZ1_MASK (7 << 16)
129 #define IMTTBCR_TSZ1_SHIFT 16
130 #define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
131 #define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
132 #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
133 #define IMTTBCR_SH0_MASK (3 << 12)
134 #define IMTTBCR_ORGN0_NC (0 << 10)
135 #define IMTTBCR_ORGN0_WB_WA (1 << 10)
136 #define IMTTBCR_ORGN0_WT (2 << 10)
137 #define IMTTBCR_ORGN0_WB (3 << 10)
138 #define IMTTBCR_ORGN0_MASK (3 << 10)
139 #define IMTTBCR_IRGN0_NC (0 << 8)
140 #define IMTTBCR_IRGN0_WB_WA (1 << 8)
141 #define IMTTBCR_IRGN0_WT (2 << 8)
142 #define IMTTBCR_IRGN0_WB (3 << 8)
143 #define IMTTBCR_IRGN0_MASK (3 << 8)
144 #define IMTTBCR_SL0_LVL_2 (0 << 4)
145 #define IMTTBCR_SL0_LVL_1 (1 << 4)
146 #define IMTTBCR_TSZ0_MASK (7 << 0)
147 #define IMTTBCR_TSZ0_SHIFT O
149 #define IMTTBCR_SL0_TWOBIT_LVL_3 (0 << 6)
150 #define IMTTBCR_SL0_TWOBIT_LVL_2 (1 << 6)
151 #define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6)
153 #define IMBUSCR 0x000c
154 #define IMBUSCR_DVM (1 << 2)
155 #define IMBUSCR_BUSSEL_SYS (0 << 0)
156 #define IMBUSCR_BUSSEL_CCI (1 << 0)
157 #define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
158 #define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
159 #define IMBUSCR_BUSSEL_MASK (3 << 0)
161 #define IMTTLBR0 0x0010
162 #define IMTTUBR0 0x0014
163 #define IMTTLBR1 0x0018
164 #define IMTTUBR1 0x001c
167 #define IMSTR_ERRLVL_MASK (3 << 12)
168 #define IMSTR_ERRLVL_SHIFT 12
169 #define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
170 #define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
171 #define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
172 #define IMSTR_ERRCODE_MASK (7 << 8)
173 #define IMSTR_MHIT (1 << 4)
174 #define IMSTR_ABORT (1 << 2)
175 #define IMSTR_PF (1 << 1)
176 #define IMSTR_TF (1 << 0)
178 #define IMMAIR0 0x0028
179 #define IMMAIR1 0x002c
180 #define IMMAIR_ATTR_MASK 0xff
181 #define IMMAIR_ATTR_DEVICE 0x04
182 #define IMMAIR_ATTR_NC 0x44
183 #define IMMAIR_ATTR_WBRWA 0xff
184 #define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
185 #define IMMAIR_ATTR_IDX_NC 0
186 #define IMMAIR_ATTR_IDX_WBRWA 1
187 #define IMMAIR_ATTR_IDX_DEV 2
191 #define IMPCTR 0x0200
192 #define IMPSTR 0x0208
193 #define IMPEAR 0x020c
194 #define IMPMBA(n) (0x0280 + ((n) * 4))
195 #define IMPMBD(n) (0x02c0 + ((n) * 4))
197 #define IMUCTR(n) (0x0300 + ((n) * 16))
198 #define IMUCTR_FIXADDEN (1 << 31)
199 #define IMUCTR_FIXADD_MASK (0xff << 16)
200 #define IMUCTR_FIXADD_SHIFT 16
201 #define IMUCTR_TTSEL_MMU(n) ((n) << 4)
202 #define IMUCTR_TTSEL_PMB (8 << 4)
203 #define IMUCTR_TTSEL_MASK (15 << 4)
204 #define IMUCTR_FLUSH (1 << 1)
205 #define IMUCTR_MMUEN (1 << 0)
207 #define IMUASID(n) (0x0308 + ((n) * 16))
208 #define IMUASID_ASID8_MASK (0xff << 8)
209 #define IMUASID_ASID8_SHIFT 8
210 #define IMUASID_ASID0_MASK (0xff << 0)
211 #define IMUASID_ASID0_SHIFT 0
213 /* -----------------------------------------------------------------------------
214 * Root device handling
217 static struct platform_driver ipmmu_driver;
219 static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
221 return mmu->root == mmu;
224 static int __ipmmu_check_device(struct device *dev, void *data)
226 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
227 struct ipmmu_vmsa_device **rootp = data;
229 if (ipmmu_is_root(mmu))
235 static struct ipmmu_vmsa_device *ipmmu_find_root(void)
237 struct ipmmu_vmsa_device *root = NULL;
239 return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
240 __ipmmu_check_device) == 0 ? root : NULL;
243 /* -----------------------------------------------------------------------------
247 static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
249 return ioread32(mmu->base + offset);
252 static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
255 iowrite32(data, mmu->base + offset);
258 static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
261 return ipmmu_read(domain->mmu->root,
262 domain->context_id * IM_CTX_SIZE + reg);
265 static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
266 unsigned int reg, u32 data)
268 ipmmu_write(domain->mmu->root,
269 domain->context_id * IM_CTX_SIZE + reg, data);
272 static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
273 unsigned int reg, u32 data)
275 if (domain->mmu != domain->mmu->root)
276 ipmmu_write(domain->mmu,
277 domain->context_id * IM_CTX_SIZE + reg, data);
279 ipmmu_write(domain->mmu->root,
280 domain->context_id * IM_CTX_SIZE + reg, data);
283 /* -----------------------------------------------------------------------------
284 * TLB and microTLB Management
287 /* Wait for any pending TLB invalidations to complete */
288 static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
290 unsigned int count = 0;
292 while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) {
294 if (++count == TLB_LOOP_TIMEOUT) {
295 dev_err_ratelimited(domain->mmu->dev,
296 "TLB sync timed out -- MMU may be deadlocked\n");
303 static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
307 reg = ipmmu_ctx_read_root(domain, IMCTR);
309 ipmmu_ctx_write_all(domain, IMCTR, reg);
311 ipmmu_tlb_sync(domain);
315 * Enable MMU translation for the microTLB.
317 static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
320 struct ipmmu_vmsa_device *mmu = domain->mmu;
323 * TODO: Reference-count the microTLB as several bus masters can be
324 * connected to the same microTLB.
327 /* TODO: What should we set the ASID to ? */
328 ipmmu_write(mmu, IMUASID(utlb), 0);
329 /* TODO: Do we need to flush the microTLB ? */
330 ipmmu_write(mmu, IMUCTR(utlb),
331 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
336 * Disable MMU translation for the microTLB.
338 static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
341 struct ipmmu_vmsa_device *mmu = domain->mmu;
343 ipmmu_write(mmu, IMUCTR(utlb), 0);
346 static void ipmmu_tlb_flush_all(void *cookie)
348 struct ipmmu_vmsa_domain *domain = cookie;
350 ipmmu_tlb_invalidate(domain);
353 static void ipmmu_tlb_add_flush(unsigned long iova, size_t size,
354 size_t granule, bool leaf, void *cookie)
356 /* The hardware doesn't support selective TLB flush. */
359 static const struct iommu_gather_ops ipmmu_gather_ops = {
360 .tlb_flush_all = ipmmu_tlb_flush_all,
361 .tlb_add_flush = ipmmu_tlb_add_flush,
362 .tlb_sync = ipmmu_tlb_flush_all,
365 /* -----------------------------------------------------------------------------
366 * Domain/Context Management
369 static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
370 struct ipmmu_vmsa_domain *domain)
375 spin_lock_irqsave(&mmu->lock, flags);
377 ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
378 if (ret != mmu->num_ctx) {
379 mmu->domains[ret] = domain;
380 set_bit(ret, mmu->ctx);
384 spin_unlock_irqrestore(&mmu->lock, flags);
389 static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
390 unsigned int context_id)
394 spin_lock_irqsave(&mmu->lock, flags);
396 clear_bit(context_id, mmu->ctx);
397 mmu->domains[context_id] = NULL;
399 spin_unlock_irqrestore(&mmu->lock, flags);
402 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
409 * Allocate the page table operations.
411 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
412 * access, Long-descriptor format" that the NStable bit being set in a
413 * table descriptor will result in the NStable and NS bits of all child
414 * entries being ignored and considered as being set. The IPMMU seems
415 * not to comply with this, as it generates a secure access page fault
416 * if any of the NStable and NS bits isn't set when running in
419 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
420 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
421 domain->cfg.ias = 32;
422 domain->cfg.oas = 40;
423 domain->cfg.tlb = &ipmmu_gather_ops;
424 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
425 domain->io_domain.geometry.force_aperture = true;
427 * TODO: Add support for coherent walk through CCI with DVM and remove
428 * cache handling. For now, delegate it to the io-pgtable code.
430 domain->cfg.iommu_dev = domain->mmu->root->dev;
433 * Find an unused context.
435 ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
439 domain->context_id = ret;
441 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
444 ipmmu_domain_free_context(domain->mmu->root,
450 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
451 ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
452 ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
456 * We use long descriptors with inner-shareable WBWA tables and allocate
457 * the whole 32-bit VA space to TTBR0.
459 if (domain->mmu->features->twobit_imttbcr_sl0)
460 tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
462 tmp = IMTTBCR_SL0_LVL_1;
464 ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE |
465 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
466 IMTTBCR_IRGN0_WB_WA | tmp);
469 ipmmu_ctx_write_root(domain, IMMAIR0,
470 domain->cfg.arm_lpae_s1_cfg.mair[0]);
473 if (domain->mmu->features->setup_imbuscr)
474 ipmmu_ctx_write_root(domain, IMBUSCR,
475 ipmmu_ctx_read_root(domain, IMBUSCR) &
476 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
480 * Clear all interrupt flags.
482 ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
486 * Enable the MMU and interrupt generation. The long-descriptor
487 * translation table format doesn't use TEX remapping. Don't enable AF
488 * software management as we have no use for it. Flush the TLB as
489 * required when modifying the context registers.
491 ipmmu_ctx_write_all(domain, IMCTR,
492 IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
497 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
500 * Disable the context. Flush the TLB as required when modifying the
503 * TODO: Is TLB flush really needed ?
505 ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
506 ipmmu_tlb_sync(domain);
507 ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
510 /* -----------------------------------------------------------------------------
514 static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
516 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
517 struct ipmmu_vmsa_device *mmu = domain->mmu;
521 status = ipmmu_ctx_read_root(domain, IMSTR);
522 if (!(status & err_mask))
525 iova = ipmmu_ctx_read_root(domain, IMEAR);
528 * Clear the error status flags. Unlike traditional interrupt flag
529 * registers that must be cleared by writing 1, this status register
530 * seems to require 0. The error address register must be read before,
531 * otherwise its value will be 0.
533 ipmmu_ctx_write_root(domain, IMSTR, 0);
535 /* Log fatal errors. */
536 if (status & IMSTR_MHIT)
537 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
539 if (status & IMSTR_ABORT)
540 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
543 if (!(status & (IMSTR_PF | IMSTR_TF)))
547 * Try to handle page faults and translation faults.
549 * TODO: We need to look up the faulty device based on the I/O VA. Use
550 * the IOMMU device for now.
552 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
555 dev_err_ratelimited(mmu->dev,
556 "Unhandled fault: status 0x%08x iova 0x%08x\n",
562 static irqreturn_t ipmmu_irq(int irq, void *dev)
564 struct ipmmu_vmsa_device *mmu = dev;
565 irqreturn_t status = IRQ_NONE;
569 spin_lock_irqsave(&mmu->lock, flags);
572 * Check interrupts for all active contexts.
574 for (i = 0; i < mmu->num_ctx; i++) {
575 if (!mmu->domains[i])
577 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
578 status = IRQ_HANDLED;
581 spin_unlock_irqrestore(&mmu->lock, flags);
586 /* -----------------------------------------------------------------------------
590 static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
592 struct ipmmu_vmsa_domain *domain;
594 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
598 spin_lock_init(&domain->lock);
600 return &domain->io_domain;
603 static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
605 struct iommu_domain *io_domain = NULL;
608 case IOMMU_DOMAIN_UNMANAGED:
609 io_domain = __ipmmu_domain_alloc(type);
612 case IOMMU_DOMAIN_DMA:
613 io_domain = __ipmmu_domain_alloc(type);
614 if (io_domain && iommu_get_dma_cookie(io_domain)) {
624 static void ipmmu_domain_free(struct iommu_domain *io_domain)
626 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
629 * Free the domain resources. We assume that all devices have already
632 iommu_put_dma_cookie(io_domain);
633 ipmmu_domain_destroy_context(domain);
634 free_io_pgtable_ops(domain->iop);
638 static int ipmmu_attach_device(struct iommu_domain *io_domain,
641 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
642 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
643 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
649 dev_err(dev, "Cannot attach to IPMMU\n");
653 spin_lock_irqsave(&domain->lock, flags);
656 /* The domain hasn't been used yet, initialize it. */
658 ret = ipmmu_domain_init_context(domain);
660 dev_err(dev, "Unable to initialize IPMMU context\n");
663 dev_info(dev, "Using IPMMU context %u\n",
666 } else if (domain->mmu != mmu) {
668 * Something is wrong, we can't attach two devices using
669 * different IOMMUs to the same domain.
671 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
672 dev_name(mmu->dev), dev_name(domain->mmu->dev));
675 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
677 spin_unlock_irqrestore(&domain->lock, flags);
682 for (i = 0; i < fwspec->num_ids; ++i)
683 ipmmu_utlb_enable(domain, fwspec->ids[i]);
688 static void ipmmu_detach_device(struct iommu_domain *io_domain,
691 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
692 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
695 for (i = 0; i < fwspec->num_ids; ++i)
696 ipmmu_utlb_disable(domain, fwspec->ids[i]);
699 * TODO: Optimize by disabling the context when no device is attached.
703 static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
704 phys_addr_t paddr, size_t size, int prot)
706 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
711 return domain->iop->map(domain->iop, iova, paddr, size, prot);
714 static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
717 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
719 return domain->iop->unmap(domain->iop, iova, size);
722 static void ipmmu_iotlb_sync(struct iommu_domain *io_domain)
724 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
727 ipmmu_tlb_flush_all(domain);
730 static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
733 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
735 /* TODO: Is locking needed ? */
737 return domain->iop->iova_to_phys(domain->iop, iova);
740 static int ipmmu_init_platform_device(struct device *dev,
741 struct of_phandle_args *args)
743 struct platform_device *ipmmu_pdev;
745 ipmmu_pdev = of_find_device_by_node(args->np);
749 dev->iommu_fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
753 static bool ipmmu_slave_whitelist(struct device *dev)
755 /* By default, do not allow use of IPMMU */
759 static const struct soc_device_attribute soc_r8a7795[] = {
760 { .soc_id = "r8a7795", },
764 static int ipmmu_of_xlate(struct device *dev,
765 struct of_phandle_args *spec)
767 /* For R-Car Gen3 use a white list to opt-in slave devices */
768 if (soc_device_match(soc_r8a7795) && !ipmmu_slave_whitelist(dev))
771 iommu_fwspec_add_ids(dev, spec->args, 1);
773 /* Initialize once - xlate() will call multiple times */
777 return ipmmu_init_platform_device(dev, spec);
780 static int ipmmu_init_arm_mapping(struct device *dev)
782 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
783 struct iommu_group *group;
786 /* Create a device group and add the device to it. */
787 group = iommu_group_alloc();
789 dev_err(dev, "Failed to allocate IOMMU group\n");
790 return PTR_ERR(group);
793 ret = iommu_group_add_device(group, dev);
794 iommu_group_put(group);
797 dev_err(dev, "Failed to add device to IPMMU group\n");
802 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
803 * VAs. This will allocate a corresponding IOMMU domain.
806 * - Create one mapping per context (TLB).
807 * - Make the mapping size configurable ? We currently use a 2GB mapping
808 * at a 1GB offset to ensure that NULL VAs will fault.
811 struct dma_iommu_mapping *mapping;
813 mapping = arm_iommu_create_mapping(&platform_bus_type,
815 if (IS_ERR(mapping)) {
816 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
817 ret = PTR_ERR(mapping);
821 mmu->mapping = mapping;
824 /* Attach the ARM VA mapping to the device. */
825 ret = arm_iommu_attach_device(dev, mmu->mapping);
827 dev_err(dev, "Failed to attach device to VA mapping\n");
834 iommu_group_remove_device(dev);
836 arm_iommu_release_mapping(mmu->mapping);
841 static int ipmmu_add_device(struct device *dev)
843 struct iommu_group *group;
846 * Only let through devices that have been verified in xlate()
851 if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
852 return ipmmu_init_arm_mapping(dev);
854 group = iommu_group_get_for_dev(dev);
856 return PTR_ERR(group);
858 iommu_group_put(group);
862 static void ipmmu_remove_device(struct device *dev)
864 arm_iommu_detach_device(dev);
865 iommu_group_remove_device(dev);
868 static struct iommu_group *ipmmu_find_group(struct device *dev)
870 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
871 struct iommu_group *group;
874 return iommu_group_ref_get(mmu->group);
876 group = iommu_group_alloc();
883 static const struct iommu_ops ipmmu_ops = {
884 .domain_alloc = ipmmu_domain_alloc,
885 .domain_free = ipmmu_domain_free,
886 .attach_dev = ipmmu_attach_device,
887 .detach_dev = ipmmu_detach_device,
889 .unmap = ipmmu_unmap,
890 .flush_iotlb_all = ipmmu_iotlb_sync,
891 .iotlb_sync = ipmmu_iotlb_sync,
892 .map_sg = default_iommu_map_sg,
893 .iova_to_phys = ipmmu_iova_to_phys,
894 .add_device = ipmmu_add_device,
895 .remove_device = ipmmu_remove_device,
896 .device_group = ipmmu_find_group,
897 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
898 .of_xlate = ipmmu_of_xlate,
901 /* -----------------------------------------------------------------------------
902 * Probe/remove and init
905 static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
909 /* Disable all contexts. */
910 for (i = 0; i < mmu->num_ctx; ++i)
911 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
914 static const struct ipmmu_features ipmmu_features_default = {
915 .use_ns_alias_offset = true,
916 .has_cache_leaf_nodes = false,
917 .number_of_contexts = 1, /* software only tested with one context */
918 .setup_imbuscr = true,
919 .twobit_imttbcr_sl0 = false,
922 static const struct ipmmu_features ipmmu_features_r8a7795 = {
923 .use_ns_alias_offset = false,
924 .has_cache_leaf_nodes = true,
925 .number_of_contexts = 8,
926 .setup_imbuscr = false,
927 .twobit_imttbcr_sl0 = true,
930 static const struct of_device_id ipmmu_of_ids[] = {
932 .compatible = "renesas,ipmmu-vmsa",
933 .data = &ipmmu_features_default,
935 .compatible = "renesas,ipmmu-r8a7795",
936 .data = &ipmmu_features_r8a7795,
942 MODULE_DEVICE_TABLE(of, ipmmu_of_ids);
944 static int ipmmu_probe(struct platform_device *pdev)
946 struct ipmmu_vmsa_device *mmu;
947 struct resource *res;
951 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
953 dev_err(&pdev->dev, "cannot allocate device data\n");
957 mmu->dev = &pdev->dev;
959 spin_lock_init(&mmu->lock);
960 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
961 mmu->features = of_device_get_match_data(&pdev->dev);
962 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
964 /* Map I/O memory and request IRQ. */
965 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
966 mmu->base = devm_ioremap_resource(&pdev->dev, res);
967 if (IS_ERR(mmu->base))
968 return PTR_ERR(mmu->base);
971 * The IPMMU has two register banks, for secure and non-secure modes.
972 * The bank mapped at the beginning of the IPMMU address space
973 * corresponds to the running mode of the CPU. When running in secure
974 * mode the non-secure register bank is also available at an offset.
976 * Secure mode operation isn't clearly documented and is thus currently
977 * not implemented in the driver. Furthermore, preliminary tests of
978 * non-secure operation with the main register bank were not successful.
979 * Offset the registers base unconditionally to point to the non-secure
980 * alias space for now.
982 if (mmu->features->use_ns_alias_offset)
983 mmu->base += IM_NS_ALIAS_OFFSET;
985 mmu->num_ctx = min_t(unsigned int, IPMMU_CTX_MAX,
986 mmu->features->number_of_contexts);
988 irq = platform_get_irq(pdev, 0);
991 * Determine if this IPMMU instance is a root device by checking for
992 * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
994 if (!mmu->features->has_cache_leaf_nodes ||
995 !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL))
998 mmu->root = ipmmu_find_root();
1001 * Wait until the root device has been registered for sure.
1004 return -EPROBE_DEFER;
1006 /* Root devices have mandatory IRQs */
1007 if (ipmmu_is_root(mmu)) {
1009 dev_err(&pdev->dev, "no IRQ found\n");
1013 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1014 dev_name(&pdev->dev), mmu);
1016 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1020 ipmmu_device_reset(mmu);
1024 * Register the IPMMU to the IOMMU subsystem in the following cases:
1025 * - R-Car Gen2 IPMMU (all devices registered)
1026 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
1028 if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
1029 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
1030 dev_name(&pdev->dev));
1034 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
1035 iommu_device_set_fwnode(&mmu->iommu,
1036 &pdev->dev.of_node->fwnode);
1038 ret = iommu_device_register(&mmu->iommu);
1042 #if defined(CONFIG_IOMMU_DMA)
1043 if (!iommu_present(&platform_bus_type))
1044 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1049 * We can't create the ARM mapping here as it requires the bus to have
1050 * an IOMMU, which only happens when bus_set_iommu() is called in
1051 * ipmmu_init() after the probe function returns.
1054 platform_set_drvdata(pdev, mmu);
1059 static int ipmmu_remove(struct platform_device *pdev)
1061 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1063 iommu_device_sysfs_remove(&mmu->iommu);
1064 iommu_device_unregister(&mmu->iommu);
1066 arm_iommu_release_mapping(mmu->mapping);
1068 ipmmu_device_reset(mmu);
1073 static struct platform_driver ipmmu_driver = {
1075 .name = "ipmmu-vmsa",
1076 .of_match_table = of_match_ptr(ipmmu_of_ids),
1078 .probe = ipmmu_probe,
1079 .remove = ipmmu_remove,
1082 static int __init ipmmu_init(void)
1084 static bool setup_done;
1090 ret = platform_driver_register(&ipmmu_driver);
1094 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
1095 if (!iommu_present(&platform_bus_type))
1096 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1103 static void __exit ipmmu_exit(void)
1105 return platform_driver_unregister(&ipmmu_driver);
1108 subsys_initcall(ipmmu_init);
1109 module_exit(ipmmu_exit);
1111 #ifdef CONFIG_IOMMU_DMA
1112 static int __init ipmmu_vmsa_iommu_of_setup(struct device_node *np)
1118 IOMMU_OF_DECLARE(ipmmu_vmsa_iommu_of, "renesas,ipmmu-vmsa",
1119 ipmmu_vmsa_iommu_of_setup);
1120 IOMMU_OF_DECLARE(ipmmu_r8a7795_iommu_of, "renesas,ipmmu-r8a7795",
1121 ipmmu_vmsa_iommu_of_setup);
1124 MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1125 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1126 MODULE_LICENSE("GPL v2");