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 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>
31 #include <asm/pgalloc.h>
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)
39 #define IPMMU_CTX_MAX 8U
40 #define IPMMU_CTX_INVALID -1
42 #define IPMMU_UTLB_MAX 48U
44 struct ipmmu_features
{
45 bool use_ns_alias_offset
;
46 bool has_cache_leaf_nodes
;
47 unsigned int number_of_contexts
;
48 unsigned int num_utlbs
;
50 bool twobit_imttbcr_sl0
;
51 bool reserved_context
;
53 unsigned int ctx_offset_base
;
54 unsigned int ctx_offset_stride
;
55 unsigned int utlb_offset_base
;
58 struct ipmmu_vmsa_device
{
61 struct iommu_device iommu
;
62 struct ipmmu_vmsa_device
*root
;
63 const struct ipmmu_features
*features
;
65 spinlock_t lock
; /* Protects ctx and domains[] */
66 DECLARE_BITMAP(ctx
, IPMMU_CTX_MAX
);
67 struct ipmmu_vmsa_domain
*domains
[IPMMU_CTX_MAX
];
68 s8 utlb_ctx
[IPMMU_UTLB_MAX
];
70 struct iommu_group
*group
;
71 struct dma_iommu_mapping
*mapping
;
74 struct ipmmu_vmsa_domain
{
75 struct ipmmu_vmsa_device
*mmu
;
76 struct iommu_domain io_domain
;
78 struct io_pgtable_cfg cfg
;
79 struct io_pgtable_ops
*iop
;
81 unsigned int context_id
;
82 struct mutex mutex
; /* Protects mappings */
85 static struct ipmmu_vmsa_domain
*to_vmsa_domain(struct iommu_domain
*dom
)
87 return container_of(dom
, struct ipmmu_vmsa_domain
, io_domain
);
90 static struct ipmmu_vmsa_device
*to_ipmmu(struct device
*dev
)
92 return dev_iommu_priv_get(dev
);
95 #define TLB_LOOP_TIMEOUT 100 /* 100us */
97 /* -----------------------------------------------------------------------------
98 * Registers Definition
101 #define IM_NS_ALIAS_OFFSET 0x800
103 /* MMU "context" registers */
104 #define IMCTR 0x0000 /* R-Car Gen2/3 */
105 #define IMCTR_INTEN (1 << 2) /* R-Car Gen2/3 */
106 #define IMCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
107 #define IMCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
109 #define IMTTBCR 0x0008 /* R-Car Gen2/3 */
110 #define IMTTBCR_EAE (1 << 31) /* R-Car Gen2/3 */
111 #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12) /* R-Car Gen2 only */
112 #define IMTTBCR_ORGN0_WB_WA (1 << 10) /* R-Car Gen2 only */
113 #define IMTTBCR_IRGN0_WB_WA (1 << 8) /* R-Car Gen2 only */
114 #define IMTTBCR_SL0_TWOBIT_LVL_1 (2 << 6) /* R-Car Gen3 only */
115 #define IMTTBCR_SL0_LVL_1 (1 << 4) /* R-Car Gen2 only */
117 #define IMBUSCR 0x000c /* R-Car Gen2 only */
118 #define IMBUSCR_DVM (1 << 2) /* R-Car Gen2 only */
119 #define IMBUSCR_BUSSEL_MASK (3 << 0) /* R-Car Gen2 only */
121 #define IMTTLBR0 0x0010 /* R-Car Gen2/3 */
122 #define IMTTUBR0 0x0014 /* R-Car Gen2/3 */
124 #define IMSTR 0x0020 /* R-Car Gen2/3 */
125 #define IMSTR_MHIT (1 << 4) /* R-Car Gen2/3 */
126 #define IMSTR_ABORT (1 << 2) /* R-Car Gen2/3 */
127 #define IMSTR_PF (1 << 1) /* R-Car Gen2/3 */
128 #define IMSTR_TF (1 << 0) /* R-Car Gen2/3 */
130 #define IMMAIR0 0x0028 /* R-Car Gen2/3 */
132 #define IMELAR 0x0030 /* R-Car Gen2/3, IMEAR on R-Car Gen2 */
133 #define IMEUAR 0x0034 /* R-Car Gen3 only */
136 #define IMUCTR(n) ((n) < 32 ? IMUCTR0(n) : IMUCTR32(n))
137 #define IMUCTR0(n) (0x0300 + ((n) * 16)) /* R-Car Gen2/3 */
138 #define IMUCTR32(n) (0x0600 + (((n) - 32) * 16)) /* R-Car Gen3 only */
139 #define IMUCTR_TTSEL_MMU(n) ((n) << 4) /* R-Car Gen2/3 */
140 #define IMUCTR_FLUSH (1 << 1) /* R-Car Gen2/3 */
141 #define IMUCTR_MMUEN (1 << 0) /* R-Car Gen2/3 */
143 #define IMUASID(n) ((n) < 32 ? IMUASID0(n) : IMUASID32(n))
144 #define IMUASID0(n) (0x0308 + ((n) * 16)) /* R-Car Gen2/3 */
145 #define IMUASID32(n) (0x0608 + (((n) - 32) * 16)) /* R-Car Gen3 only */
147 /* -----------------------------------------------------------------------------
148 * Root device handling
151 static struct platform_driver ipmmu_driver
;
153 static bool ipmmu_is_root(struct ipmmu_vmsa_device
*mmu
)
155 return mmu
->root
== mmu
;
158 static int __ipmmu_check_device(struct device
*dev
, void *data
)
160 struct ipmmu_vmsa_device
*mmu
= dev_get_drvdata(dev
);
161 struct ipmmu_vmsa_device
**rootp
= data
;
163 if (ipmmu_is_root(mmu
))
169 static struct ipmmu_vmsa_device
*ipmmu_find_root(void)
171 struct ipmmu_vmsa_device
*root
= NULL
;
173 return driver_for_each_device(&ipmmu_driver
.driver
, NULL
, &root
,
174 __ipmmu_check_device
) == 0 ? root
: NULL
;
177 /* -----------------------------------------------------------------------------
181 static u32
ipmmu_read(struct ipmmu_vmsa_device
*mmu
, unsigned int offset
)
183 return ioread32(mmu
->base
+ offset
);
186 static void ipmmu_write(struct ipmmu_vmsa_device
*mmu
, unsigned int offset
,
189 iowrite32(data
, mmu
->base
+ offset
);
192 static unsigned int ipmmu_ctx_reg(struct ipmmu_vmsa_device
*mmu
,
193 unsigned int context_id
, unsigned int reg
)
195 return mmu
->features
->ctx_offset_base
+
196 context_id
* mmu
->features
->ctx_offset_stride
+ reg
;
199 static u32
ipmmu_ctx_read(struct ipmmu_vmsa_device
*mmu
,
200 unsigned int context_id
, unsigned int reg
)
202 return ipmmu_read(mmu
, ipmmu_ctx_reg(mmu
, context_id
, reg
));
205 static void ipmmu_ctx_write(struct ipmmu_vmsa_device
*mmu
,
206 unsigned int context_id
, unsigned int reg
, u32 data
)
208 ipmmu_write(mmu
, ipmmu_ctx_reg(mmu
, context_id
, reg
), data
);
211 static u32
ipmmu_ctx_read_root(struct ipmmu_vmsa_domain
*domain
,
214 return ipmmu_ctx_read(domain
->mmu
->root
, domain
->context_id
, reg
);
217 static void ipmmu_ctx_write_root(struct ipmmu_vmsa_domain
*domain
,
218 unsigned int reg
, u32 data
)
220 ipmmu_ctx_write(domain
->mmu
->root
, domain
->context_id
, reg
, data
);
223 static void ipmmu_ctx_write_all(struct ipmmu_vmsa_domain
*domain
,
224 unsigned int reg
, u32 data
)
226 if (domain
->mmu
!= domain
->mmu
->root
)
227 ipmmu_ctx_write(domain
->mmu
, domain
->context_id
, reg
, data
);
229 ipmmu_ctx_write(domain
->mmu
->root
, domain
->context_id
, reg
, data
);
232 static u32
ipmmu_utlb_reg(struct ipmmu_vmsa_device
*mmu
, unsigned int reg
)
234 return mmu
->features
->utlb_offset_base
+ reg
;
237 static void ipmmu_imuasid_write(struct ipmmu_vmsa_device
*mmu
,
238 unsigned int utlb
, u32 data
)
240 ipmmu_write(mmu
, ipmmu_utlb_reg(mmu
, IMUASID(utlb
)), data
);
243 static void ipmmu_imuctr_write(struct ipmmu_vmsa_device
*mmu
,
244 unsigned int utlb
, u32 data
)
246 ipmmu_write(mmu
, ipmmu_utlb_reg(mmu
, IMUCTR(utlb
)), data
);
249 /* -----------------------------------------------------------------------------
250 * TLB and microTLB Management
253 /* Wait for any pending TLB invalidations to complete */
254 static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain
*domain
)
256 unsigned int count
= 0;
258 while (ipmmu_ctx_read_root(domain
, IMCTR
) & IMCTR_FLUSH
) {
260 if (++count
== TLB_LOOP_TIMEOUT
) {
261 dev_err_ratelimited(domain
->mmu
->dev
,
262 "TLB sync timed out -- MMU may be deadlocked\n");
269 static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain
*domain
)
273 reg
= ipmmu_ctx_read_root(domain
, IMCTR
);
275 ipmmu_ctx_write_all(domain
, IMCTR
, reg
);
277 ipmmu_tlb_sync(domain
);
281 * Enable MMU translation for the microTLB.
283 static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain
*domain
,
286 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
289 * TODO: Reference-count the microTLB as several bus masters can be
290 * connected to the same microTLB.
293 /* TODO: What should we set the ASID to ? */
294 ipmmu_imuasid_write(mmu
, utlb
, 0);
295 /* TODO: Do we need to flush the microTLB ? */
296 ipmmu_imuctr_write(mmu
, utlb
, IMUCTR_TTSEL_MMU(domain
->context_id
) |
297 IMUCTR_FLUSH
| IMUCTR_MMUEN
);
298 mmu
->utlb_ctx
[utlb
] = domain
->context_id
;
302 * Disable MMU translation for the microTLB.
304 static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain
*domain
,
307 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
309 ipmmu_imuctr_write(mmu
, utlb
, 0);
310 mmu
->utlb_ctx
[utlb
] = IPMMU_CTX_INVALID
;
313 static void ipmmu_tlb_flush_all(void *cookie
)
315 struct ipmmu_vmsa_domain
*domain
= cookie
;
317 ipmmu_tlb_invalidate(domain
);
320 static void ipmmu_tlb_flush(unsigned long iova
, size_t size
,
321 size_t granule
, void *cookie
)
323 ipmmu_tlb_flush_all(cookie
);
326 static const struct iommu_flush_ops ipmmu_flush_ops
= {
327 .tlb_flush_all
= ipmmu_tlb_flush_all
,
328 .tlb_flush_walk
= ipmmu_tlb_flush
,
329 .tlb_flush_leaf
= ipmmu_tlb_flush
,
332 /* -----------------------------------------------------------------------------
333 * Domain/Context Management
336 static int ipmmu_domain_allocate_context(struct ipmmu_vmsa_device
*mmu
,
337 struct ipmmu_vmsa_domain
*domain
)
342 spin_lock_irqsave(&mmu
->lock
, flags
);
344 ret
= find_first_zero_bit(mmu
->ctx
, mmu
->num_ctx
);
345 if (ret
!= mmu
->num_ctx
) {
346 mmu
->domains
[ret
] = domain
;
347 set_bit(ret
, mmu
->ctx
);
351 spin_unlock_irqrestore(&mmu
->lock
, flags
);
356 static void ipmmu_domain_free_context(struct ipmmu_vmsa_device
*mmu
,
357 unsigned int context_id
)
361 spin_lock_irqsave(&mmu
->lock
, flags
);
363 clear_bit(context_id
, mmu
->ctx
);
364 mmu
->domains
[context_id
] = NULL
;
366 spin_unlock_irqrestore(&mmu
->lock
, flags
);
369 static void ipmmu_domain_setup_context(struct ipmmu_vmsa_domain
*domain
)
375 ttbr
= domain
->cfg
.arm_lpae_s1_cfg
.ttbr
;
376 ipmmu_ctx_write_root(domain
, IMTTLBR0
, ttbr
);
377 ipmmu_ctx_write_root(domain
, IMTTUBR0
, ttbr
>> 32);
381 * We use long descriptors and allocate the whole 32-bit VA space to
384 if (domain
->mmu
->features
->twobit_imttbcr_sl0
)
385 tmp
= IMTTBCR_SL0_TWOBIT_LVL_1
;
387 tmp
= IMTTBCR_SL0_LVL_1
;
389 if (domain
->mmu
->features
->cache_snoop
)
390 tmp
|= IMTTBCR_SH0_INNER_SHAREABLE
| IMTTBCR_ORGN0_WB_WA
|
393 ipmmu_ctx_write_root(domain
, IMTTBCR
, IMTTBCR_EAE
| tmp
);
396 ipmmu_ctx_write_root(domain
, IMMAIR0
,
397 domain
->cfg
.arm_lpae_s1_cfg
.mair
);
400 if (domain
->mmu
->features
->setup_imbuscr
)
401 ipmmu_ctx_write_root(domain
, IMBUSCR
,
402 ipmmu_ctx_read_root(domain
, IMBUSCR
) &
403 ~(IMBUSCR_DVM
| IMBUSCR_BUSSEL_MASK
));
407 * Clear all interrupt flags.
409 ipmmu_ctx_write_root(domain
, IMSTR
, ipmmu_ctx_read_root(domain
, IMSTR
));
413 * Enable the MMU and interrupt generation. The long-descriptor
414 * translation table format doesn't use TEX remapping. Don't enable AF
415 * software management as we have no use for it. Flush the TLB as
416 * required when modifying the context registers.
418 ipmmu_ctx_write_all(domain
, IMCTR
,
419 IMCTR_INTEN
| IMCTR_FLUSH
| IMCTR_MMUEN
);
422 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain
*domain
)
427 * Allocate the page table operations.
429 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory
430 * access, Long-descriptor format" that the NStable bit being set in a
431 * table descriptor will result in the NStable and NS bits of all child
432 * entries being ignored and considered as being set. The IPMMU seems
433 * not to comply with this, as it generates a secure access page fault
434 * if any of the NStable and NS bits isn't set when running in
437 domain
->cfg
.quirks
= IO_PGTABLE_QUIRK_ARM_NS
;
438 domain
->cfg
.pgsize_bitmap
= SZ_1G
| SZ_2M
| SZ_4K
;
439 domain
->cfg
.ias
= 32;
440 domain
->cfg
.oas
= 40;
441 domain
->cfg
.tlb
= &ipmmu_flush_ops
;
442 domain
->io_domain
.geometry
.aperture_end
= DMA_BIT_MASK(32);
443 domain
->io_domain
.geometry
.force_aperture
= true;
445 * TODO: Add support for coherent walk through CCI with DVM and remove
446 * cache handling. For now, delegate it to the io-pgtable code.
448 domain
->cfg
.coherent_walk
= false;
449 domain
->cfg
.iommu_dev
= domain
->mmu
->root
->dev
;
452 * Find an unused context.
454 ret
= ipmmu_domain_allocate_context(domain
->mmu
->root
, domain
);
458 domain
->context_id
= ret
;
460 domain
->iop
= alloc_io_pgtable_ops(ARM_32_LPAE_S1
, &domain
->cfg
,
463 ipmmu_domain_free_context(domain
->mmu
->root
,
468 ipmmu_domain_setup_context(domain
);
472 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain
*domain
)
478 * Disable the context. Flush the TLB as required when modifying the
481 * TODO: Is TLB flush really needed ?
483 ipmmu_ctx_write_all(domain
, IMCTR
, IMCTR_FLUSH
);
484 ipmmu_tlb_sync(domain
);
485 ipmmu_domain_free_context(domain
->mmu
->root
, domain
->context_id
);
488 /* -----------------------------------------------------------------------------
492 static irqreturn_t
ipmmu_domain_irq(struct ipmmu_vmsa_domain
*domain
)
494 const u32 err_mask
= IMSTR_MHIT
| IMSTR_ABORT
| IMSTR_PF
| IMSTR_TF
;
495 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
499 status
= ipmmu_ctx_read_root(domain
, IMSTR
);
500 if (!(status
& err_mask
))
503 iova
= ipmmu_ctx_read_root(domain
, IMELAR
);
504 if (IS_ENABLED(CONFIG_64BIT
))
505 iova
|= (u64
)ipmmu_ctx_read_root(domain
, IMEUAR
) << 32;
508 * Clear the error status flags. Unlike traditional interrupt flag
509 * registers that must be cleared by writing 1, this status register
510 * seems to require 0. The error address register must be read before,
511 * otherwise its value will be 0.
513 ipmmu_ctx_write_root(domain
, IMSTR
, 0);
515 /* Log fatal errors. */
516 if (status
& IMSTR_MHIT
)
517 dev_err_ratelimited(mmu
->dev
, "Multiple TLB hits @0x%lx\n",
519 if (status
& IMSTR_ABORT
)
520 dev_err_ratelimited(mmu
->dev
, "Page Table Walk Abort @0x%lx\n",
523 if (!(status
& (IMSTR_PF
| IMSTR_TF
)))
527 * Try to handle page faults and translation faults.
529 * TODO: We need to look up the faulty device based on the I/O VA. Use
530 * the IOMMU device for now.
532 if (!report_iommu_fault(&domain
->io_domain
, mmu
->dev
, iova
, 0))
535 dev_err_ratelimited(mmu
->dev
,
536 "Unhandled fault: status 0x%08x iova 0x%lx\n",
542 static irqreturn_t
ipmmu_irq(int irq
, void *dev
)
544 struct ipmmu_vmsa_device
*mmu
= dev
;
545 irqreturn_t status
= IRQ_NONE
;
549 spin_lock_irqsave(&mmu
->lock
, flags
);
552 * Check interrupts for all active contexts.
554 for (i
= 0; i
< mmu
->num_ctx
; i
++) {
555 if (!mmu
->domains
[i
])
557 if (ipmmu_domain_irq(mmu
->domains
[i
]) == IRQ_HANDLED
)
558 status
= IRQ_HANDLED
;
561 spin_unlock_irqrestore(&mmu
->lock
, flags
);
566 /* -----------------------------------------------------------------------------
570 static struct iommu_domain
*__ipmmu_domain_alloc(unsigned type
)
572 struct ipmmu_vmsa_domain
*domain
;
574 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
578 mutex_init(&domain
->mutex
);
580 return &domain
->io_domain
;
583 static struct iommu_domain
*ipmmu_domain_alloc(unsigned type
)
585 struct iommu_domain
*io_domain
= NULL
;
588 case IOMMU_DOMAIN_UNMANAGED
:
589 io_domain
= __ipmmu_domain_alloc(type
);
592 case IOMMU_DOMAIN_DMA
:
593 io_domain
= __ipmmu_domain_alloc(type
);
594 if (io_domain
&& iommu_get_dma_cookie(io_domain
)) {
604 static void ipmmu_domain_free(struct iommu_domain
*io_domain
)
606 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
609 * Free the domain resources. We assume that all devices have already
612 iommu_put_dma_cookie(io_domain
);
613 ipmmu_domain_destroy_context(domain
);
614 free_io_pgtable_ops(domain
->iop
);
618 static int ipmmu_attach_device(struct iommu_domain
*io_domain
,
621 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
622 struct ipmmu_vmsa_device
*mmu
= to_ipmmu(dev
);
623 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
628 dev_err(dev
, "Cannot attach to IPMMU\n");
632 mutex_lock(&domain
->mutex
);
635 /* The domain hasn't been used yet, initialize it. */
637 ret
= ipmmu_domain_init_context(domain
);
639 dev_err(dev
, "Unable to initialize IPMMU context\n");
642 dev_info(dev
, "Using IPMMU context %u\n",
645 } else if (domain
->mmu
!= mmu
) {
647 * Something is wrong, we can't attach two devices using
648 * different IOMMUs to the same domain.
650 dev_err(dev
, "Can't attach IPMMU %s to domain on IPMMU %s\n",
651 dev_name(mmu
->dev
), dev_name(domain
->mmu
->dev
));
654 dev_info(dev
, "Reusing IPMMU context %u\n", domain
->context_id
);
656 mutex_unlock(&domain
->mutex
);
661 for (i
= 0; i
< fwspec
->num_ids
; ++i
)
662 ipmmu_utlb_enable(domain
, fwspec
->ids
[i
]);
667 static void ipmmu_detach_device(struct iommu_domain
*io_domain
,
670 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
671 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
674 for (i
= 0; i
< fwspec
->num_ids
; ++i
)
675 ipmmu_utlb_disable(domain
, fwspec
->ids
[i
]);
678 * TODO: Optimize by disabling the context when no device is attached.
682 static int ipmmu_map(struct iommu_domain
*io_domain
, unsigned long iova
,
683 phys_addr_t paddr
, size_t size
, int prot
, gfp_t gfp
)
685 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
690 return domain
->iop
->map(domain
->iop
, iova
, paddr
, size
, prot
);
693 static size_t ipmmu_unmap(struct iommu_domain
*io_domain
, unsigned long iova
,
694 size_t size
, struct iommu_iotlb_gather
*gather
)
696 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
698 return domain
->iop
->unmap(domain
->iop
, iova
, size
, gather
);
701 static void ipmmu_flush_iotlb_all(struct iommu_domain
*io_domain
)
703 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
706 ipmmu_tlb_flush_all(domain
);
709 static void ipmmu_iotlb_sync(struct iommu_domain
*io_domain
,
710 struct iommu_iotlb_gather
*gather
)
712 ipmmu_flush_iotlb_all(io_domain
);
715 static phys_addr_t
ipmmu_iova_to_phys(struct iommu_domain
*io_domain
,
718 struct ipmmu_vmsa_domain
*domain
= to_vmsa_domain(io_domain
);
720 /* TODO: Is locking needed ? */
722 return domain
->iop
->iova_to_phys(domain
->iop
, iova
);
725 static int ipmmu_init_platform_device(struct device
*dev
,
726 struct of_phandle_args
*args
)
728 struct platform_device
*ipmmu_pdev
;
730 ipmmu_pdev
= of_find_device_by_node(args
->np
);
734 dev_iommu_priv_set(dev
, platform_get_drvdata(ipmmu_pdev
));
739 static const struct soc_device_attribute soc_rcar_gen3
[] = {
740 { .soc_id
= "r8a774a1", },
741 { .soc_id
= "r8a774b1", },
742 { .soc_id
= "r8a774c0", },
743 { .soc_id
= "r8a7795", },
744 { .soc_id
= "r8a7796", },
745 { .soc_id
= "r8a77965", },
746 { .soc_id
= "r8a77970", },
747 { .soc_id
= "r8a77990", },
748 { .soc_id
= "r8a77995", },
752 static const struct soc_device_attribute soc_rcar_gen3_whitelist
[] = {
753 { .soc_id
= "r8a774b1", },
754 { .soc_id
= "r8a774c0", },
755 { .soc_id
= "r8a7795", .revision
= "ES3.*" },
756 { .soc_id
= "r8a77965", },
757 { .soc_id
= "r8a77990", },
758 { .soc_id
= "r8a77995", },
762 static const char * const rcar_gen3_slave_whitelist
[] = {
765 static bool ipmmu_slave_whitelist(struct device
*dev
)
770 * For R-Car Gen3 use a white list to opt-in slave devices.
771 * For Other SoCs, this returns true anyway.
773 if (!soc_device_match(soc_rcar_gen3
))
776 /* Check whether this R-Car Gen3 can use the IPMMU correctly or not */
777 if (!soc_device_match(soc_rcar_gen3_whitelist
))
780 /* Check whether this slave device can work with the IPMMU */
781 for (i
= 0; i
< ARRAY_SIZE(rcar_gen3_slave_whitelist
); i
++) {
782 if (!strcmp(dev_name(dev
), rcar_gen3_slave_whitelist
[i
]))
786 /* Otherwise, do not allow use of IPMMU */
790 static int ipmmu_of_xlate(struct device
*dev
,
791 struct of_phandle_args
*spec
)
793 if (!ipmmu_slave_whitelist(dev
))
796 iommu_fwspec_add_ids(dev
, spec
->args
, 1);
798 /* Initialize once - xlate() will call multiple times */
802 return ipmmu_init_platform_device(dev
, spec
);
805 static int ipmmu_init_arm_mapping(struct device
*dev
)
807 struct ipmmu_vmsa_device
*mmu
= to_ipmmu(dev
);
808 struct iommu_group
*group
;
811 /* Create a device group and add the device to it. */
812 group
= iommu_group_alloc();
814 dev_err(dev
, "Failed to allocate IOMMU group\n");
815 return PTR_ERR(group
);
818 ret
= iommu_group_add_device(group
, dev
);
819 iommu_group_put(group
);
822 dev_err(dev
, "Failed to add device to IPMMU group\n");
827 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
828 * VAs. This will allocate a corresponding IOMMU domain.
831 * - Create one mapping per context (TLB).
832 * - Make the mapping size configurable ? We currently use a 2GB mapping
833 * at a 1GB offset to ensure that NULL VAs will fault.
836 struct dma_iommu_mapping
*mapping
;
838 mapping
= arm_iommu_create_mapping(&platform_bus_type
,
840 if (IS_ERR(mapping
)) {
841 dev_err(mmu
->dev
, "failed to create ARM IOMMU mapping\n");
842 ret
= PTR_ERR(mapping
);
846 mmu
->mapping
= mapping
;
849 /* Attach the ARM VA mapping to the device. */
850 ret
= arm_iommu_attach_device(dev
, mmu
->mapping
);
852 dev_err(dev
, "Failed to attach device to VA mapping\n");
859 iommu_group_remove_device(dev
);
861 arm_iommu_release_mapping(mmu
->mapping
);
866 static int ipmmu_add_device(struct device
*dev
)
868 struct ipmmu_vmsa_device
*mmu
= to_ipmmu(dev
);
869 struct iommu_group
*group
;
873 * Only let through devices that have been verified in xlate()
878 if (IS_ENABLED(CONFIG_ARM
) && !IS_ENABLED(CONFIG_IOMMU_DMA
)) {
879 ret
= ipmmu_init_arm_mapping(dev
);
883 group
= iommu_group_get_for_dev(dev
);
885 return PTR_ERR(group
);
887 iommu_group_put(group
);
890 iommu_device_link(&mmu
->iommu
, dev
);
894 static void ipmmu_remove_device(struct device
*dev
)
896 struct ipmmu_vmsa_device
*mmu
= to_ipmmu(dev
);
898 iommu_device_unlink(&mmu
->iommu
, dev
);
899 arm_iommu_detach_device(dev
);
900 iommu_group_remove_device(dev
);
903 static struct iommu_group
*ipmmu_find_group(struct device
*dev
)
905 struct ipmmu_vmsa_device
*mmu
= to_ipmmu(dev
);
906 struct iommu_group
*group
;
909 return iommu_group_ref_get(mmu
->group
);
911 group
= iommu_group_alloc();
918 static const struct iommu_ops ipmmu_ops
= {
919 .domain_alloc
= ipmmu_domain_alloc
,
920 .domain_free
= ipmmu_domain_free
,
921 .attach_dev
= ipmmu_attach_device
,
922 .detach_dev
= ipmmu_detach_device
,
924 .unmap
= ipmmu_unmap
,
925 .flush_iotlb_all
= ipmmu_flush_iotlb_all
,
926 .iotlb_sync
= ipmmu_iotlb_sync
,
927 .iova_to_phys
= ipmmu_iova_to_phys
,
928 .add_device
= ipmmu_add_device
,
929 .remove_device
= ipmmu_remove_device
,
930 .device_group
= ipmmu_find_group
,
931 .pgsize_bitmap
= SZ_1G
| SZ_2M
| SZ_4K
,
932 .of_xlate
= ipmmu_of_xlate
,
935 /* -----------------------------------------------------------------------------
936 * Probe/remove and init
939 static void ipmmu_device_reset(struct ipmmu_vmsa_device
*mmu
)
943 /* Disable all contexts. */
944 for (i
= 0; i
< mmu
->num_ctx
; ++i
)
945 ipmmu_ctx_write(mmu
, i
, IMCTR
, 0);
948 static const struct ipmmu_features ipmmu_features_default
= {
949 .use_ns_alias_offset
= true,
950 .has_cache_leaf_nodes
= false,
951 .number_of_contexts
= 1, /* software only tested with one context */
953 .setup_imbuscr
= true,
954 .twobit_imttbcr_sl0
= false,
955 .reserved_context
= false,
957 .ctx_offset_base
= 0,
958 .ctx_offset_stride
= 0x40,
959 .utlb_offset_base
= 0,
962 static const struct ipmmu_features ipmmu_features_rcar_gen3
= {
963 .use_ns_alias_offset
= false,
964 .has_cache_leaf_nodes
= true,
965 .number_of_contexts
= 8,
967 .setup_imbuscr
= false,
968 .twobit_imttbcr_sl0
= true,
969 .reserved_context
= true,
970 .cache_snoop
= false,
971 .ctx_offset_base
= 0,
972 .ctx_offset_stride
= 0x40,
973 .utlb_offset_base
= 0,
976 static const struct of_device_id ipmmu_of_ids
[] = {
978 .compatible
= "renesas,ipmmu-vmsa",
979 .data
= &ipmmu_features_default
,
981 .compatible
= "renesas,ipmmu-r8a774a1",
982 .data
= &ipmmu_features_rcar_gen3
,
984 .compatible
= "renesas,ipmmu-r8a774b1",
985 .data
= &ipmmu_features_rcar_gen3
,
987 .compatible
= "renesas,ipmmu-r8a774c0",
988 .data
= &ipmmu_features_rcar_gen3
,
990 .compatible
= "renesas,ipmmu-r8a7795",
991 .data
= &ipmmu_features_rcar_gen3
,
993 .compatible
= "renesas,ipmmu-r8a7796",
994 .data
= &ipmmu_features_rcar_gen3
,
996 .compatible
= "renesas,ipmmu-r8a77965",
997 .data
= &ipmmu_features_rcar_gen3
,
999 .compatible
= "renesas,ipmmu-r8a77970",
1000 .data
= &ipmmu_features_rcar_gen3
,
1002 .compatible
= "renesas,ipmmu-r8a77990",
1003 .data
= &ipmmu_features_rcar_gen3
,
1005 .compatible
= "renesas,ipmmu-r8a77995",
1006 .data
= &ipmmu_features_rcar_gen3
,
1012 static int ipmmu_probe(struct platform_device
*pdev
)
1014 struct ipmmu_vmsa_device
*mmu
;
1015 struct resource
*res
;
1019 mmu
= devm_kzalloc(&pdev
->dev
, sizeof(*mmu
), GFP_KERNEL
);
1021 dev_err(&pdev
->dev
, "cannot allocate device data\n");
1025 mmu
->dev
= &pdev
->dev
;
1026 spin_lock_init(&mmu
->lock
);
1027 bitmap_zero(mmu
->ctx
, IPMMU_CTX_MAX
);
1028 mmu
->features
= of_device_get_match_data(&pdev
->dev
);
1029 memset(mmu
->utlb_ctx
, IPMMU_CTX_INVALID
, mmu
->features
->num_utlbs
);
1030 dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(40));
1032 /* Map I/O memory and request IRQ. */
1033 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1034 mmu
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1035 if (IS_ERR(mmu
->base
))
1036 return PTR_ERR(mmu
->base
);
1039 * The IPMMU has two register banks, for secure and non-secure modes.
1040 * The bank mapped at the beginning of the IPMMU address space
1041 * corresponds to the running mode of the CPU. When running in secure
1042 * mode the non-secure register bank is also available at an offset.
1044 * Secure mode operation isn't clearly documented and is thus currently
1045 * not implemented in the driver. Furthermore, preliminary tests of
1046 * non-secure operation with the main register bank were not successful.
1047 * Offset the registers base unconditionally to point to the non-secure
1048 * alias space for now.
1050 if (mmu
->features
->use_ns_alias_offset
)
1051 mmu
->base
+= IM_NS_ALIAS_OFFSET
;
1053 mmu
->num_ctx
= min(IPMMU_CTX_MAX
, mmu
->features
->number_of_contexts
);
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
))
1063 mmu
->root
= ipmmu_find_root();
1066 * Wait until the root device has been registered for sure.
1069 return -EPROBE_DEFER
;
1071 /* Root devices have mandatory IRQs */
1072 if (ipmmu_is_root(mmu
)) {
1073 irq
= platform_get_irq(pdev
, 0);
1077 ret
= devm_request_irq(&pdev
->dev
, irq
, ipmmu_irq
, 0,
1078 dev_name(&pdev
->dev
), mmu
);
1080 dev_err(&pdev
->dev
, "failed to request IRQ %d\n", irq
);
1084 ipmmu_device_reset(mmu
);
1086 if (mmu
->features
->reserved_context
) {
1087 dev_info(&pdev
->dev
, "IPMMU context 0 is reserved\n");
1088 set_bit(0, mmu
->ctx
);
1093 * Register the IPMMU to the IOMMU subsystem in the following cases:
1094 * - R-Car Gen2 IPMMU (all devices registered)
1095 * - R-Car Gen3 IPMMU (leaf devices only - skip root IPMMU-MM device)
1097 if (!mmu
->features
->has_cache_leaf_nodes
|| !ipmmu_is_root(mmu
)) {
1098 ret
= iommu_device_sysfs_add(&mmu
->iommu
, &pdev
->dev
, NULL
,
1099 dev_name(&pdev
->dev
));
1103 iommu_device_set_ops(&mmu
->iommu
, &ipmmu_ops
);
1104 iommu_device_set_fwnode(&mmu
->iommu
,
1105 &pdev
->dev
.of_node
->fwnode
);
1107 ret
= iommu_device_register(&mmu
->iommu
);
1111 #if defined(CONFIG_IOMMU_DMA)
1112 if (!iommu_present(&platform_bus_type
))
1113 bus_set_iommu(&platform_bus_type
, &ipmmu_ops
);
1118 * We can't create the ARM mapping here as it requires the bus to have
1119 * an IOMMU, which only happens when bus_set_iommu() is called in
1120 * ipmmu_init() after the probe function returns.
1123 platform_set_drvdata(pdev
, mmu
);
1128 static int ipmmu_remove(struct platform_device
*pdev
)
1130 struct ipmmu_vmsa_device
*mmu
= platform_get_drvdata(pdev
);
1132 iommu_device_sysfs_remove(&mmu
->iommu
);
1133 iommu_device_unregister(&mmu
->iommu
);
1135 arm_iommu_release_mapping(mmu
->mapping
);
1137 ipmmu_device_reset(mmu
);
1142 #ifdef CONFIG_PM_SLEEP
1143 static int ipmmu_resume_noirq(struct device
*dev
)
1145 struct ipmmu_vmsa_device
*mmu
= dev_get_drvdata(dev
);
1148 /* Reset root MMU and restore contexts */
1149 if (ipmmu_is_root(mmu
)) {
1150 ipmmu_device_reset(mmu
);
1152 for (i
= 0; i
< mmu
->num_ctx
; i
++) {
1153 if (!mmu
->domains
[i
])
1156 ipmmu_domain_setup_context(mmu
->domains
[i
]);
1160 /* Re-enable active micro-TLBs */
1161 for (i
= 0; i
< mmu
->features
->num_utlbs
; i
++) {
1162 if (mmu
->utlb_ctx
[i
] == IPMMU_CTX_INVALID
)
1165 ipmmu_utlb_enable(mmu
->root
->domains
[mmu
->utlb_ctx
[i
]], i
);
1171 static const struct dev_pm_ops ipmmu_pm
= {
1172 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL
, ipmmu_resume_noirq
)
1174 #define DEV_PM_OPS &ipmmu_pm
1176 #define DEV_PM_OPS NULL
1177 #endif /* CONFIG_PM_SLEEP */
1179 static struct platform_driver ipmmu_driver
= {
1181 .name
= "ipmmu-vmsa",
1182 .of_match_table
= of_match_ptr(ipmmu_of_ids
),
1185 .probe
= ipmmu_probe
,
1186 .remove
= ipmmu_remove
,
1189 static int __init
ipmmu_init(void)
1191 struct device_node
*np
;
1192 static bool setup_done
;
1198 np
= of_find_matching_node(NULL
, ipmmu_of_ids
);
1204 ret
= platform_driver_register(&ipmmu_driver
);
1208 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA)
1209 if (!iommu_present(&platform_bus_type
))
1210 bus_set_iommu(&platform_bus_type
, &ipmmu_ops
);
1216 subsys_initcall(ipmmu_init
);