Linux 5.1.15
[linux/fpc-iii.git] / drivers / iommu / ipmmu-vmsa.c
blob9a380c10655e182d35d9f6c170336aeaa05030f6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * IOMMU API for Renesas VMSA-compatible IPMMU
4 * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
6 * Copyright (C) 2014 Renesas Electronics Corporation
7 */
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>
17 #include <linux/io.h>
18 #include <linux/io-pgtable.h>
19 #include <linux/iommu.h>
20 #include <linux/of.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>
31 #include <asm/pgalloc.h>
32 #else
33 #define arm_iommu_create_mapping(...) NULL
34 #define arm_iommu_attach_device(...) -ENODEV
35 #define arm_iommu_release_mapping(...) do {} while (0)
36 #define arm_iommu_detach_device(...) do {} while (0)
37 #endif
39 #define IPMMU_CTX_MAX 8
41 struct ipmmu_features {
42 bool use_ns_alias_offset;
43 bool has_cache_leaf_nodes;
44 unsigned int number_of_contexts;
45 bool setup_imbuscr;
46 bool twobit_imttbcr_sl0;
47 bool reserved_context;
50 struct ipmmu_vmsa_device {
51 struct device *dev;
52 void __iomem *base;
53 struct iommu_device iommu;
54 struct ipmmu_vmsa_device *root;
55 const struct ipmmu_features *features;
56 unsigned int num_utlbs;
57 unsigned int num_ctx;
58 spinlock_t lock; /* Protects ctx and domains[] */
59 DECLARE_BITMAP(ctx, IPMMU_CTX_MAX);
60 struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
62 struct iommu_group *group;
63 struct dma_iommu_mapping *mapping;
66 struct ipmmu_vmsa_domain {
67 struct ipmmu_vmsa_device *mmu;
68 struct iommu_domain io_domain;
70 struct io_pgtable_cfg cfg;
71 struct io_pgtable_ops *iop;
73 unsigned int context_id;
74 struct mutex mutex; /* Protects mappings */
77 static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom)
79 return container_of(dom, struct ipmmu_vmsa_domain, io_domain);
82 static struct ipmmu_vmsa_device *to_ipmmu(struct device *dev)
84 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
86 return fwspec ? 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
99 #define IMCTR 0x0000
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
166 #define IMSTR 0x0020
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
189 #define IMEAR 0x0030
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) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
198 #define IMUCTR0(n) (0x0300 + ((n) * 16))
199 #define IMUCTR32(n) (0x0600 + (((n) - 32) * 16))
200 #define IMUCTR_FIXADDEN (1 << 31)
201 #define IMUCTR_FIXADD_MASK (0xff << 16)
202 #define IMUCTR_FIXADD_SHIFT 16
203 #define IMUCTR_TTSEL_MMU(n) ((n) << 4)
204 #define IMUCTR_TTSEL_PMB (8 << 4)
205 #define IMUCTR_TTSEL_MASK (15 << 4)
206 #define IMUCTR_FLUSH (1 << 1)
207 #define IMUCTR_MMUEN (1 << 0)
209 #define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n))
210 #define IMUASID0(n) (0x0308 + ((n) * 16))
211 #define IMUASID32(n) (0x0608 + (((n) - 32) * 16))
212 #define IMUASID_ASID8_MASK (0xff << 8)
213 #define IMUASID_ASID8_SHIFT 8
214 #define IMUASID_ASID0_MASK (0xff << 0)
215 #define IMUASID_ASID0_SHIFT 0
217 /* -----------------------------------------------------------------------------
218 * Root device handling
221 static struct platform_driver ipmmu_driver;
223 static bool ipmmu_is_root(struct ipmmu_vmsa_device *mmu)
225 return mmu->root == mmu;
228 static int __ipmmu_check_device(struct device *dev, void *data)
230 struct ipmmu_vmsa_device *mmu = dev_get_drvdata(dev);
231 struct ipmmu_vmsa_device **rootp = data;
233 if (ipmmu_is_root(mmu))
234 *rootp = mmu;
236 return 0;
239 static struct ipmmu_vmsa_device *ipmmu_find_root(void)
241 struct ipmmu_vmsa_device *root = NULL;
243 return driver_for_each_device(&ipmmu_driver.driver, NULL, &root,
244 __ipmmu_check_device) == 0 ? root : NULL;
247 /* -----------------------------------------------------------------------------
248 * Read/Write Access
251 static u32 ipmmu_read(struct ipmmu_vmsa_device *mmu, unsigned int offset)
253 return ioread32(mmu->base + offset);
256 static void ipmmu_write(struct ipmmu_vmsa_device *mmu, unsigned int offset,
257 u32 data)
259 iowrite32(data, mmu->base + offset);
262 static u32 ipmmu_ctx_read_root(struct ipmmu_vmsa_domain *domain,
263 unsigned int reg)
265 return ipmmu_read(domain->mmu->root,
266 domain->context_id * IM_CTX_SIZE + reg);
269 static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain *domain,
270 unsigned int reg, u32 data)
272 ipmmu_write(domain->mmu->root,
273 domain->context_id * IM_CTX_SIZE + reg, data);
276 static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain *domain,
277 unsigned int reg, u32 data)
279 if (domain->mmu != domain->mmu->root)
280 ipmmu_write(domain->mmu,
281 domain->context_id * IM_CTX_SIZE + reg, data);
283 ipmmu_write(domain->mmu->root,
284 domain->context_id * IM_CTX_SIZE + reg, data);
287 /* -----------------------------------------------------------------------------
288 * TLB and microTLB Management
291 /* Wait for any pending TLB invalidations to complete */
292 static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain *domain)
294 unsigned int count = 0;
296 while (ipmmu_ctx_read_root(domain, IMCTR) & IMCTR_FLUSH) {
297 cpu_relax();
298 if (++count == TLB_LOOP_TIMEOUT) {
299 dev_err_ratelimited(domain->mmu->dev,
300 "TLB sync timed out -- MMU may be deadlocked\n");
301 return;
303 udelay(1);
307 static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain *domain)
309 u32 reg;
311 reg = ipmmu_ctx_read_root(domain, IMCTR);
312 reg |= IMCTR_FLUSH;
313 ipmmu_ctx_write_all(domain, IMCTR, reg);
315 ipmmu_tlb_sync(domain);
319 * Enable MMU translation for the microTLB.
321 static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain *domain,
322 unsigned int utlb)
324 struct ipmmu_vmsa_device *mmu = domain->mmu;
327 * TODO: Reference-count the microTLB as several bus masters can be
328 * connected to the same microTLB.
331 /* TODO: What should we set the ASID to ? */
332 ipmmu_write(mmu, IMUASID(utlb), 0);
333 /* TODO: Do we need to flush the microTLB ? */
334 ipmmu_write(mmu, IMUCTR(utlb),
335 IMUCTR_TTSEL_MMU(domain->context_id) | IMUCTR_FLUSH |
336 IMUCTR_MMUEN);
340 * Disable MMU translation for the microTLB.
342 static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain *domain,
343 unsigned int utlb)
345 struct ipmmu_vmsa_device *mmu = domain->mmu;
347 ipmmu_write(mmu, IMUCTR(utlb), 0);
350 static void ipmmu_tlb_flush_all(void *cookie)
352 struct ipmmu_vmsa_domain *domain = cookie;
354 ipmmu_tlb_invalidate(domain);
357 static void ipmmu_tlb_add_flush(unsigned long iova, size_t size,
358 size_t granule, bool leaf, void *cookie)
360 /* The hardware doesn't support selective TLB flush. */
363 static const struct iommu_gather_ops ipmmu_gather_ops = {
364 .tlb_flush_all = ipmmu_tlb_flush_all,
365 .tlb_add_flush = ipmmu_tlb_add_flush,
366 .tlb_sync = ipmmu_tlb_flush_all,
369 /* -----------------------------------------------------------------------------
370 * Domain/Context Management
373 static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device *mmu,
374 struct ipmmu_vmsa_domain *domain)
376 unsigned long flags;
377 int ret;
379 spin_lock_irqsave(&mmu->lock, flags);
381 ret = find_first_zero_bit(mmu->ctx, mmu->num_ctx);
382 if (ret != mmu->num_ctx) {
383 mmu->domains[ret] = domain;
384 set_bit(ret, mmu->ctx);
385 } else
386 ret = -EBUSY;
388 spin_unlock_irqrestore(&mmu->lock, flags);
390 return ret;
393 static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu,
394 unsigned int context_id)
396 unsigned long flags;
398 spin_lock_irqsave(&mmu->lock, flags);
400 clear_bit(context_id, mmu->ctx);
401 mmu->domains[context_id] = NULL;
403 spin_unlock_irqrestore(&mmu->lock, flags);
406 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain)
408 u64 ttbr;
409 u32 tmp;
410 int ret;
413 * Allocate the page table operations.
415 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
416 * access, Long-descriptor format" that the NStable bit being set in a
417 * table descriptor will result in the NStable and NS bits of all child
418 * entries being ignored and considered as being set. The IPMMU seems
419 * not to comply with this, as it generates a secure access page fault
420 * if any of the NStable and NS bits isn't set when running in
421 * non-secure mode.
423 domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS;
424 domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K;
425 domain->cfg.ias = 32;
426 domain->cfg.oas = 40;
427 domain->cfg.tlb = &ipmmu_gather_ops;
428 domain->io_domain.geometry.aperture_end = DMA_BIT_MASK(32);
429 domain->io_domain.geometry.force_aperture = true;
431 * TODO: Add support for coherent walk through CCI with DVM and remove
432 * cache handling. For now, delegate it to the io-pgtable code.
434 domain->cfg.iommu_dev = domain->mmu->root->dev;
437 * Find an unused context.
439 ret = ipmmu_domain_allocate_context(domain->mmu->root, domain);
440 if (ret < 0)
441 return ret;
443 domain->context_id = ret;
445 domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg,
446 domain);
447 if (!domain->iop) {
448 ipmmu_domain_free_context(domain->mmu->root,
449 domain->context_id);
450 return -EINVAL;
453 /* TTBR0 */
454 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0];
455 ipmmu_ctx_write_root(domain, IMTTLBR0, ttbr);
456 ipmmu_ctx_write_root(domain, IMTTUBR0, ttbr >> 32);
459 * TTBCR
460 * We use long descriptors with inner-shareable WBWA tables and allocate
461 * the whole 32-bit VA space to TTBR0.
463 if (domain->mmu->features->twobit_imttbcr_sl0)
464 tmp = IMTTBCR_SL0_TWOBIT_LVL_1;
465 else
466 tmp = IMTTBCR_SL0_LVL_1;
468 ipmmu_ctx_write_root(domain, IMTTBCR, IMTTBCR_EAE |
469 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA |
470 IMTTBCR_IRGN0_WB_WA | tmp);
472 /* MAIR0 */
473 ipmmu_ctx_write_root(domain, IMMAIR0,
474 domain->cfg.arm_lpae_s1_cfg.mair[0]);
476 /* IMBUSCR */
477 if (domain->mmu->features->setup_imbuscr)
478 ipmmu_ctx_write_root(domain, IMBUSCR,
479 ipmmu_ctx_read_root(domain, IMBUSCR) &
480 ~(IMBUSCR_DVM | IMBUSCR_BUSSEL_MASK));
483 * IMSTR
484 * Clear all interrupt flags.
486 ipmmu_ctx_write_root(domain, IMSTR, ipmmu_ctx_read_root(domain, IMSTR));
489 * IMCTR
490 * Enable the MMU and interrupt generation. The long-descriptor
491 * translation table format doesn't use TEX remapping. Don't enable AF
492 * software management as we have no use for it. Flush the TLB as
493 * required when modifying the context registers.
495 ipmmu_ctx_write_all(domain, IMCTR,
496 IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN);
498 return 0;
501 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain)
503 if (!domain->mmu)
504 return;
507 * Disable the context. Flush the TLB as required when modifying the
508 * context registers.
510 * TODO: Is TLB flush really needed ?
512 ipmmu_ctx_write_all(domain, IMCTR, IMCTR_FLUSH);
513 ipmmu_tlb_sync(domain);
514 ipmmu_domain_free_context(domain->mmu->root, domain->context_id);
517 /* -----------------------------------------------------------------------------
518 * Fault Handling
521 static irqreturn_t ipmmu_domain_irq(struct ipmmu_vmsa_domain *domain)
523 const u32 err_mask = IMSTR_MHIT | IMSTR_ABORT | IMSTR_PF | IMSTR_TF;
524 struct ipmmu_vmsa_device *mmu = domain->mmu;
525 u32 status;
526 u32 iova;
528 status = ipmmu_ctx_read_root(domain, IMSTR);
529 if (!(status & err_mask))
530 return IRQ_NONE;
532 iova = ipmmu_ctx_read_root(domain, IMEAR);
535 * Clear the error status flags. Unlike traditional interrupt flag
536 * registers that must be cleared by writing 1, this status register
537 * seems to require 0. The error address register must be read before,
538 * otherwise its value will be 0.
540 ipmmu_ctx_write_root(domain, IMSTR, 0);
542 /* Log fatal errors. */
543 if (status & IMSTR_MHIT)
544 dev_err_ratelimited(mmu->dev, "Multiple TLB hits @0x%08x\n",
545 iova);
546 if (status & IMSTR_ABORT)
547 dev_err_ratelimited(mmu->dev, "Page Table Walk Abort @0x%08x\n",
548 iova);
550 if (!(status & (IMSTR_PF | IMSTR_TF)))
551 return IRQ_NONE;
554 * Try to handle page faults and translation faults.
556 * TODO: We need to look up the faulty device based on the I/O VA. Use
557 * the IOMMU device for now.
559 if (!report_iommu_fault(&domain->io_domain, mmu->dev, iova, 0))
560 return IRQ_HANDLED;
562 dev_err_ratelimited(mmu->dev,
563 "Unhandled fault: status 0x%08x iova 0x%08x\n",
564 status, iova);
566 return IRQ_HANDLED;
569 static irqreturn_t ipmmu_irq(int irq, void *dev)
571 struct ipmmu_vmsa_device *mmu = dev;
572 irqreturn_t status = IRQ_NONE;
573 unsigned int i;
574 unsigned long flags;
576 spin_lock_irqsave(&mmu->lock, flags);
579 * Check interrupts for all active contexts.
581 for (i = 0; i < mmu->num_ctx; i++) {
582 if (!mmu->domains[i])
583 continue;
584 if (ipmmu_domain_irq(mmu->domains[i]) == IRQ_HANDLED)
585 status = IRQ_HANDLED;
588 spin_unlock_irqrestore(&mmu->lock, flags);
590 return status;
593 /* -----------------------------------------------------------------------------
594 * IOMMU Operations
597 static struct iommu_domain *__ipmmu_domain_alloc(unsigned type)
599 struct ipmmu_vmsa_domain *domain;
601 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
602 if (!domain)
603 return NULL;
605 mutex_init(&domain->mutex);
607 return &domain->io_domain;
610 static struct iommu_domain *ipmmu_domain_alloc(unsigned type)
612 struct iommu_domain *io_domain = NULL;
614 switch (type) {
615 case IOMMU_DOMAIN_UNMANAGED:
616 io_domain = __ipmmu_domain_alloc(type);
617 break;
619 case IOMMU_DOMAIN_DMA:
620 io_domain = __ipmmu_domain_alloc(type);
621 if (io_domain && iommu_get_dma_cookie(io_domain)) {
622 kfree(io_domain);
623 io_domain = NULL;
625 break;
628 return io_domain;
631 static void ipmmu_domain_free(struct iommu_domain *io_domain)
633 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
636 * Free the domain resources. We assume that all devices have already
637 * been detached.
639 iommu_put_dma_cookie(io_domain);
640 ipmmu_domain_destroy_context(domain);
641 free_io_pgtable_ops(domain->iop);
642 kfree(domain);
645 static int ipmmu_attach_device(struct iommu_domain *io_domain,
646 struct device *dev)
648 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
649 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
650 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
651 unsigned int i;
652 int ret = 0;
654 if (!mmu) {
655 dev_err(dev, "Cannot attach to IPMMU\n");
656 return -ENXIO;
659 mutex_lock(&domain->mutex);
661 if (!domain->mmu) {
662 /* The domain hasn't been used yet, initialize it. */
663 domain->mmu = mmu;
664 ret = ipmmu_domain_init_context(domain);
665 if (ret < 0) {
666 dev_err(dev, "Unable to initialize IPMMU context\n");
667 domain->mmu = NULL;
668 } else {
669 dev_info(dev, "Using IPMMU context %u\n",
670 domain->context_id);
672 } else if (domain->mmu != mmu) {
674 * Something is wrong, we can't attach two devices using
675 * different IOMMUs to the same domain.
677 dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n",
678 dev_name(mmu->dev), dev_name(domain->mmu->dev));
679 ret = -EINVAL;
680 } else
681 dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id);
683 mutex_unlock(&domain->mutex);
685 if (ret < 0)
686 return ret;
688 for (i = 0; i < fwspec->num_ids; ++i)
689 ipmmu_utlb_enable(domain, fwspec->ids[i]);
691 return 0;
694 static void ipmmu_detach_device(struct iommu_domain *io_domain,
695 struct device *dev)
697 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
698 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
699 unsigned int i;
701 for (i = 0; i < fwspec->num_ids; ++i)
702 ipmmu_utlb_disable(domain, fwspec->ids[i]);
705 * TODO: Optimize by disabling the context when no device is attached.
709 static int ipmmu_map(struct iommu_domain *io_domain, unsigned long iova,
710 phys_addr_t paddr, size_t size, int prot)
712 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
714 if (!domain)
715 return -ENODEV;
717 return domain->iop->map(domain->iop, iova, paddr, size, prot);
720 static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova,
721 size_t size)
723 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
725 return domain->iop->unmap(domain->iop, iova, size);
728 static void ipmmu_iotlb_sync(struct iommu_domain *io_domain)
730 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
732 if (domain->mmu)
733 ipmmu_tlb_flush_all(domain);
736 static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain,
737 dma_addr_t iova)
739 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain);
741 /* TODO: Is locking needed ? */
743 return domain->iop->iova_to_phys(domain->iop, iova);
746 static int ipmmu_init_platform_device(struct device *dev,
747 struct of_phandle_args *args)
749 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
750 struct platform_device *ipmmu_pdev;
752 ipmmu_pdev = of_find_device_by_node(args->np);
753 if (!ipmmu_pdev)
754 return -ENODEV;
756 fwspec->iommu_priv = platform_get_drvdata(ipmmu_pdev);
758 return 0;
761 static const struct soc_device_attribute soc_rcar_gen3[] = {
762 { .soc_id = "r8a774a1", },
763 { .soc_id = "r8a774c0", },
764 { .soc_id = "r8a7795", },
765 { .soc_id = "r8a7796", },
766 { .soc_id = "r8a77965", },
767 { .soc_id = "r8a77970", },
768 { .soc_id = "r8a77990", },
769 { .soc_id = "r8a77995", },
770 { /* sentinel */ }
773 static const struct soc_device_attribute soc_rcar_gen3_whitelist[] = {
774 { .soc_id = "r8a774c0", },
775 { .soc_id = "r8a7795", .revision = "ES3.*" },
776 { .soc_id = "r8a77965", },
777 { .soc_id = "r8a77990", },
778 { .soc_id = "r8a77995", },
779 { /* sentinel */ }
782 static const char * const rcar_gen3_slave_whitelist[] = {
785 static bool ipmmu_slave_whitelist(struct device *dev)
787 unsigned int i;
790 * For R-Car Gen3 use a white list to opt-in slave devices.
791 * For Other SoCs, this returns true anyway.
793 if (!soc_device_match(soc_rcar_gen3))
794 return true;
796 /* Check whether this R-Car Gen3 can use the IPMMU correctly or not */
797 if (!soc_device_match(soc_rcar_gen3_whitelist))
798 return false;
800 /* Check whether this slave device can work with the IPMMU */
801 for (i = 0; i < ARRAY_SIZE(rcar_gen3_slave_whitelist); i++) {
802 if (!strcmp(dev_name(dev), rcar_gen3_slave_whitelist[i]))
803 return true;
806 /* Otherwise, do not allow use of IPMMU */
807 return false;
810 static int ipmmu_of_xlate(struct device *dev,
811 struct of_phandle_args *spec)
813 if (!ipmmu_slave_whitelist(dev))
814 return -ENODEV;
816 iommu_fwspec_add_ids(dev, spec->args, 1);
818 /* Initialize once - xlate() will call multiple times */
819 if (to_ipmmu(dev))
820 return 0;
822 return ipmmu_init_platform_device(dev, spec);
825 static int ipmmu_init_arm_mapping(struct device *dev)
827 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
828 struct iommu_group *group;
829 int ret;
831 /* Create a device group and add the device to it. */
832 group = iommu_group_alloc();
833 if (IS_ERR(group)) {
834 dev_err(dev, "Failed to allocate IOMMU group\n");
835 return PTR_ERR(group);
838 ret = iommu_group_add_device(group, dev);
839 iommu_group_put(group);
841 if (ret < 0) {
842 dev_err(dev, "Failed to add device to IPMMU group\n");
843 return ret;
847 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
848 * VAs. This will allocate a corresponding IOMMU domain.
850 * TODO:
851 * - Create one mapping per context (TLB).
852 * - Make the mapping size configurable ? We currently use a 2GB mapping
853 * at a 1GB offset to ensure that NULL VAs will fault.
855 if (!mmu->mapping) {
856 struct dma_iommu_mapping *mapping;
858 mapping = arm_iommu_create_mapping(&platform_bus_type,
859 SZ_1G, SZ_2G);
860 if (IS_ERR(mapping)) {
861 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n");
862 ret = PTR_ERR(mapping);
863 goto error;
866 mmu->mapping = mapping;
869 /* Attach the ARM VA mapping to the device. */
870 ret = arm_iommu_attach_device(dev, mmu->mapping);
871 if (ret < 0) {
872 dev_err(dev, "Failed to attach device to VA mapping\n");
873 goto error;
876 return 0;
878 error:
879 iommu_group_remove_device(dev);
880 if (mmu->mapping)
881 arm_iommu_release_mapping(mmu->mapping);
883 return ret;
886 static int ipmmu_add_device(struct device *dev)
888 struct iommu_group *group;
891 * Only let through devices that have been verified in xlate()
893 if (!to_ipmmu(dev))
894 return -ENODEV;
896 if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA))
897 return ipmmu_init_arm_mapping(dev);
899 group = iommu_group_get_for_dev(dev);
900 if (IS_ERR(group))
901 return PTR_ERR(group);
903 iommu_group_put(group);
904 return 0;
907 static void ipmmu_remove_device(struct device *dev)
909 arm_iommu_detach_device(dev);
910 iommu_group_remove_device(dev);
913 static struct iommu_group *ipmmu_find_group(struct device *dev)
915 struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
916 struct iommu_group *group;
918 if (mmu->group)
919 return iommu_group_ref_get(mmu->group);
921 group = iommu_group_alloc();
922 if (!IS_ERR(group))
923 mmu->group = group;
925 return group;
928 static const struct iommu_ops ipmmu_ops = {
929 .domain_alloc = ipmmu_domain_alloc,
930 .domain_free = ipmmu_domain_free,
931 .attach_dev = ipmmu_attach_device,
932 .detach_dev = ipmmu_detach_device,
933 .map = ipmmu_map,
934 .unmap = ipmmu_unmap,
935 .flush_iotlb_all = ipmmu_iotlb_sync,
936 .iotlb_sync = ipmmu_iotlb_sync,
937 .iova_to_phys = ipmmu_iova_to_phys,
938 .add_device = ipmmu_add_device,
939 .remove_device = ipmmu_remove_device,
940 .device_group = ipmmu_find_group,
941 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
942 .of_xlate = ipmmu_of_xlate,
945 /* -----------------------------------------------------------------------------
946 * Probe/remove and init
949 static void ipmmu_device_reset(struct ipmmu_vmsa_device *mmu)
951 unsigned int i;
953 /* Disable all contexts. */
954 for (i = 0; i < mmu->num_ctx; ++i)
955 ipmmu_write(mmu, i * IM_CTX_SIZE + IMCTR, 0);
958 static const struct ipmmu_features ipmmu_features_default = {
959 .use_ns_alias_offset = true,
960 .has_cache_leaf_nodes = false,
961 .number_of_contexts = 1, /* software only tested with one context */
962 .setup_imbuscr = true,
963 .twobit_imttbcr_sl0 = false,
964 .reserved_context = false,
967 static const struct ipmmu_features ipmmu_features_rcar_gen3 = {
968 .use_ns_alias_offset = false,
969 .has_cache_leaf_nodes = true,
970 .number_of_contexts = 8,
971 .setup_imbuscr = false,
972 .twobit_imttbcr_sl0 = true,
973 .reserved_context = true,
976 static const struct of_device_id ipmmu_of_ids[] = {
978 .compatible = "renesas,ipmmu-vmsa",
979 .data = &ipmmu_features_default,
980 }, {
981 .compatible = "renesas,ipmmu-r8a774a1",
982 .data = &ipmmu_features_rcar_gen3,
983 }, {
984 .compatible = "renesas,ipmmu-r8a774c0",
985 .data = &ipmmu_features_rcar_gen3,
986 }, {
987 .compatible = "renesas,ipmmu-r8a7795",
988 .data = &ipmmu_features_rcar_gen3,
989 }, {
990 .compatible = "renesas,ipmmu-r8a7796",
991 .data = &ipmmu_features_rcar_gen3,
992 }, {
993 .compatible = "renesas,ipmmu-r8a77965",
994 .data = &ipmmu_features_rcar_gen3,
995 }, {
996 .compatible = "renesas,ipmmu-r8a77970",
997 .data = &ipmmu_features_rcar_gen3,
998 }, {
999 .compatible = "renesas,ipmmu-r8a77990",
1000 .data = &ipmmu_features_rcar_gen3,
1001 }, {
1002 .compatible = "renesas,ipmmu-r8a77995",
1003 .data = &ipmmu_features_rcar_gen3,
1004 }, {
1005 /* Terminator */
1009 static int ipmmu_probe(struct platform_device *pdev)
1011 struct ipmmu_vmsa_device *mmu;
1012 struct resource *res;
1013 int irq;
1014 int ret;
1016 mmu = devm_kzalloc(&pdev->dev, sizeof(*mmu), GFP_KERNEL);
1017 if (!mmu) {
1018 dev_err(&pdev->dev, "cannot allocate device data\n");
1019 return -ENOMEM;
1022 mmu->dev = &pdev->dev;
1023 mmu->num_utlbs = 48;
1024 spin_lock_init(&mmu->lock);
1025 bitmap_zero(mmu->ctx, IPMMU_CTX_MAX);
1026 mmu->features = of_device_get_match_data(&pdev->dev);
1027 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
1029 /* Map I/O memory and request IRQ. */
1030 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1031 mmu->base = devm_ioremap_resource(&pdev->dev, res);
1032 if (IS_ERR(mmu->base))
1033 return PTR_ERR(mmu->base);
1036 * The IPMMU has two register banks, for secure and non-secure modes.
1037 * The bank mapped at the beginning of the IPMMU address space
1038 * corresponds to the running mode of the CPU. When running in secure
1039 * mode the non-secure register bank is also available at an offset.
1041 * Secure mode operation isn't clearly documented and is thus currently
1042 * not implemented in the driver. Furthermore, preliminary tests of
1043 * non-secure operation with the main register bank were not successful.
1044 * Offset the registers base unconditionally to point to the non-secure
1045 * alias space for now.
1047 if (mmu->features->use_ns_alias_offset)
1048 mmu->base += IM_NS_ALIAS_OFFSET;
1050 mmu->num_ctx = min_t(unsigned int, IPMMU_CTX_MAX,
1051 mmu->features->number_of_contexts);
1053 irq = platform_get_irq(pdev, 0);
1056 * Determine if this IPMMU instance is a root device by checking for
1057 * the lack of has_cache_leaf_nodes flag or renesas,ipmmu-main property.
1059 if (!mmu->features->has_cache_leaf_nodes ||
1060 !of_find_property(pdev->dev.of_node, "renesas,ipmmu-main", NULL))
1061 mmu->root = mmu;
1062 else
1063 mmu->root = ipmmu_find_root();
1066 * Wait until the root device has been registered for sure.
1068 if (!mmu->root)
1069 return -EPROBE_DEFER;
1071 /* Root devices have mandatory IRQs */
1072 if (ipmmu_is_root(mmu)) {
1073 if (irq < 0) {
1074 dev_err(&pdev->dev, "no IRQ found\n");
1075 return irq;
1078 ret = devm_request_irq(&pdev->dev, irq, ipmmu_irq, 0,
1079 dev_name(&pdev->dev), mmu);
1080 if (ret < 0) {
1081 dev_err(&pdev->dev, "failed to request IRQ %d\n", irq);
1082 return ret;
1085 ipmmu_device_reset(mmu);
1087 if (mmu->features->reserved_context) {
1088 dev_info(&pdev->dev, "IPMMU context 0 is reserved\n");
1089 set_bit(0, mmu->ctx);
1094 * Register the IPMMU to the IOMMU subsystem in the following cases:
1095 * - R-Car Gen2 IPMMU (all devices registered)
1096 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
1098 if (!mmu->features->has_cache_leaf_nodes || !ipmmu_is_root(mmu)) {
1099 ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL,
1100 dev_name(&pdev->dev));
1101 if (ret)
1102 return ret;
1104 iommu_device_set_ops(&mmu->iommu, &ipmmu_ops);
1105 iommu_device_set_fwnode(&mmu->iommu,
1106 &pdev->dev.of_node->fwnode);
1108 ret = iommu_device_register(&mmu->iommu);
1109 if (ret)
1110 return ret;
1112 #if defined(CONFIG_IOMMU_DMA)
1113 if (!iommu_present(&platform_bus_type))
1114 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1115 #endif
1119 * We can't create the ARM mapping here as it requires the bus to have
1120 * an IOMMU, which only happens when bus_set_iommu() is called in
1121 * ipmmu_init() after the probe function returns.
1124 platform_set_drvdata(pdev, mmu);
1126 return 0;
1129 static int ipmmu_remove(struct platform_device *pdev)
1131 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev);
1133 iommu_device_sysfs_remove(&mmu->iommu);
1134 iommu_device_unregister(&mmu->iommu);
1136 arm_iommu_release_mapping(mmu->mapping);
1138 ipmmu_device_reset(mmu);
1140 return 0;
1143 static struct platform_driver ipmmu_driver = {
1144 .driver = {
1145 .name = "ipmmu-vmsa",
1146 .of_match_table = of_match_ptr(ipmmu_of_ids),
1148 .probe = ipmmu_probe,
1149 .remove = ipmmu_remove,
1152 static int __init ipmmu_init(void)
1154 struct device_node *np;
1155 static bool setup_done;
1156 int ret;
1158 if (setup_done)
1159 return 0;
1161 np = of_find_matching_node(NULL, ipmmu_of_ids);
1162 if (!np)
1163 return 0;
1165 of_node_put(np);
1167 ret = platform_driver_register(&ipmmu_driver);
1168 if (ret < 0)
1169 return ret;
1171 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
1172 if (!iommu_present(&platform_bus_type))
1173 bus_set_iommu(&platform_bus_type, &ipmmu_ops);
1174 #endif
1176 setup_done = true;
1177 return 0;
1179 subsys_initcall(ipmmu_init);