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/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/interrupt.h>
17 #include <linux/iommu.h>
18 #include <linux/module.h>
19 #include <linux/platform_data/ipmmu-vmsa.h>
20 #include <linux/platform_device.h>
21 #include <linux/sizes.h>
22 #include <linux/slab.h>
24 #include <asm/dma-iommu.h>
25 #include <asm/pgalloc.h>
27 struct ipmmu_vmsa_device
{
30 struct list_head list
;
32 const struct ipmmu_vmsa_platform_data
*pdata
;
33 unsigned int num_utlbs
;
35 struct dma_iommu_mapping
*mapping
;
38 struct ipmmu_vmsa_domain
{
39 struct ipmmu_vmsa_device
*mmu
;
40 struct iommu_domain
*io_domain
;
42 unsigned int context_id
;
43 spinlock_t lock
; /* Protects mappings */
47 struct ipmmu_vmsa_archdata
{
48 struct ipmmu_vmsa_device
*mmu
;
52 static DEFINE_SPINLOCK(ipmmu_devices_lock
);
53 static LIST_HEAD(ipmmu_devices
);
55 #define TLB_LOOP_TIMEOUT 100 /* 100us */
57 /* -----------------------------------------------------------------------------
58 * Registers Definition
61 #define IM_CTX_SIZE 0x40
64 #define IMCTR_TRE (1 << 17)
65 #define IMCTR_AFE (1 << 16)
66 #define IMCTR_RTSEL_MASK (3 << 4)
67 #define IMCTR_RTSEL_SHIFT 4
68 #define IMCTR_TREN (1 << 3)
69 #define IMCTR_INTEN (1 << 2)
70 #define IMCTR_FLUSH (1 << 1)
71 #define IMCTR_MMUEN (1 << 0)
75 #define IMTTBCR 0x0008
76 #define IMTTBCR_EAE (1 << 31)
77 #define IMTTBCR_PMB (1 << 30)
78 #define IMTTBCR_SH1_NON_SHAREABLE (0 << 28)
79 #define IMTTBCR_SH1_OUTER_SHAREABLE (2 << 28)
80 #define IMTTBCR_SH1_INNER_SHAREABLE (3 << 28)
81 #define IMTTBCR_SH1_MASK (3 << 28)
82 #define IMTTBCR_ORGN1_NC (0 << 26)
83 #define IMTTBCR_ORGN1_WB_WA (1 << 26)
84 #define IMTTBCR_ORGN1_WT (2 << 26)
85 #define IMTTBCR_ORGN1_WB (3 << 26)
86 #define IMTTBCR_ORGN1_MASK (3 << 26)
87 #define IMTTBCR_IRGN1_NC (0 << 24)
88 #define IMTTBCR_IRGN1_WB_WA (1 << 24)
89 #define IMTTBCR_IRGN1_WT (2 << 24)
90 #define IMTTBCR_IRGN1_WB (3 << 24)
91 #define IMTTBCR_IRGN1_MASK (3 << 24)
92 #define IMTTBCR_TSZ1_MASK (7 << 16)
93 #define IMTTBCR_TSZ1_SHIFT 16
94 #define IMTTBCR_SH0_NON_SHAREABLE (0 << 12)
95 #define IMTTBCR_SH0_OUTER_SHAREABLE (2 << 12)
96 #define IMTTBCR_SH0_INNER_SHAREABLE (3 << 12)
97 #define IMTTBCR_SH0_MASK (3 << 12)
98 #define IMTTBCR_ORGN0_NC (0 << 10)
99 #define IMTTBCR_ORGN0_WB_WA (1 << 10)
100 #define IMTTBCR_ORGN0_WT (2 << 10)
101 #define IMTTBCR_ORGN0_WB (3 << 10)
102 #define IMTTBCR_ORGN0_MASK (3 << 10)
103 #define IMTTBCR_IRGN0_NC (0 << 8)
104 #define IMTTBCR_IRGN0_WB_WA (1 << 8)
105 #define IMTTBCR_IRGN0_WT (2 << 8)
106 #define IMTTBCR_IRGN0_WB (3 << 8)
107 #define IMTTBCR_IRGN0_MASK (3 << 8)
108 #define IMTTBCR_SL0_LVL_2 (0 << 4)
109 #define IMTTBCR_SL0_LVL_1 (1 << 4)
110 #define IMTTBCR_TSZ0_MASK (7 << 0)
111 #define IMTTBCR_TSZ0_SHIFT O
113 #define IMBUSCR 0x000c
114 #define IMBUSCR_DVM (1 << 2)
115 #define IMBUSCR_BUSSEL_SYS (0 << 0)
116 #define IMBUSCR_BUSSEL_CCI (1 << 0)
117 #define IMBUSCR_BUSSEL_IMCAAR (2 << 0)
118 #define IMBUSCR_BUSSEL_CCI_IMCAAR (3 << 0)
119 #define IMBUSCR_BUSSEL_MASK (3 << 0)
121 #define IMTTLBR0 0x0010
122 #define IMTTUBR0 0x0014
123 #define IMTTLBR1 0x0018
124 #define IMTTUBR1 0x001c
127 #define IMSTR_ERRLVL_MASK (3 << 12)
128 #define IMSTR_ERRLVL_SHIFT 12
129 #define IMSTR_ERRCODE_TLB_FORMAT (1 << 8)
130 #define IMSTR_ERRCODE_ACCESS_PERM (4 << 8)
131 #define IMSTR_ERRCODE_SECURE_ACCESS (5 << 8)
132 #define IMSTR_ERRCODE_MASK (7 << 8)
133 #define IMSTR_MHIT (1 << 4)
134 #define IMSTR_ABORT (1 << 2)
135 #define IMSTR_PF (1 << 1)
136 #define IMSTR_TF (1 << 0)
138 #define IMMAIR0 0x0028
139 #define IMMAIR1 0x002c
140 #define IMMAIR_ATTR_MASK 0xff
141 #define IMMAIR_ATTR_DEVICE 0x04
142 #define IMMAIR_ATTR_NC 0x44
143 #define IMMAIR_ATTR_WBRWA 0xff
144 #define IMMAIR_ATTR_SHIFT(n) ((n) << 3)
145 #define IMMAIR_ATTR_IDX_NC 0
146 #define IMMAIR_ATTR_IDX_WBRWA 1
147 #define IMMAIR_ATTR_IDX_DEV 2
151 #define IMPCTR 0x0200
152 #define IMPSTR 0x0208
153 #define IMPEAR 0x020c
154 #define IMPMBA(n) (0x0280 + ((n) * 4))
155 #define IMPMBD(n) (0x02c0 + ((n) * 4))
157 #define IMUCTR(n) (0x0300 + ((n) * 16))
158 #define IMUCTR_FIXADDEN (1 << 31)
159 #define IMUCTR_FIXADD_MASK (0xff << 16)
160 #define IMUCTR_FIXADD_SHIFT 16
161 #define IMUCTR_TTSEL_MMU(n) ((n) << 4)
162 #define IMUCTR_TTSEL_PMB (8 << 4)
163 #define IMUCTR_TTSEL_MASK (15 << 4)
164 #define IMUCTR_FLUSH (1 << 1)
165 #define IMUCTR_MMUEN (1 << 0)
167 #define IMUASID(n) (0x0308 + ((n) * 16))
168 #define IMUASID_ASID8_MASK (0xff << 8)
169 #define IMUASID_ASID8_SHIFT 8
170 #define IMUASID_ASID0_MASK (0xff << 0)
171 #define IMUASID_ASID0_SHIFT 0
173 /* -----------------------------------------------------------------------------
178 * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory access,
179 * Long-descriptor format" that the NStable bit being set in a table descriptor
180 * will result in the NStable and NS bits of all child entries being ignored and
181 * considered as being set. The IPMMU seems not to comply with this, as it
182 * generates a secure access page fault if any of the NStable and NS bits isn't
183 * set when running in non-secure mode.
186 #define PMD_NSTABLE (_AT(pmdval_t, 1) << 63)
189 #define ARM_VMSA_PTE_XN (((pteval_t)3) << 53)
190 #define ARM_VMSA_PTE_CONT (((pteval_t)1) << 52)
191 #define ARM_VMSA_PTE_AF (((pteval_t)1) << 10)
192 #define ARM_VMSA_PTE_SH_NS (((pteval_t)0) << 8)
193 #define ARM_VMSA_PTE_SH_OS (((pteval_t)2) << 8)
194 #define ARM_VMSA_PTE_SH_IS (((pteval_t)3) << 8)
195 #define ARM_VMSA_PTE_SH_MASK (((pteval_t)3) << 8)
196 #define ARM_VMSA_PTE_NS (((pteval_t)1) << 5)
197 #define ARM_VMSA_PTE_PAGE (((pteval_t)3) << 0)
200 #define ARM_VMSA_PTE_nG (((pteval_t)1) << 11)
201 #define ARM_VMSA_PTE_AP_UNPRIV (((pteval_t)1) << 6)
202 #define ARM_VMSA_PTE_AP_RDONLY (((pteval_t)2) << 6)
203 #define ARM_VMSA_PTE_AP_MASK (((pteval_t)3) << 6)
204 #define ARM_VMSA_PTE_ATTRINDX_MASK (((pteval_t)3) << 2)
205 #define ARM_VMSA_PTE_ATTRINDX_SHIFT 2
207 #define ARM_VMSA_PTE_ATTRS_MASK \
208 (ARM_VMSA_PTE_XN | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_nG | \
209 ARM_VMSA_PTE_AF | ARM_VMSA_PTE_SH_MASK | ARM_VMSA_PTE_AP_MASK | \
210 ARM_VMSA_PTE_NS | ARM_VMSA_PTE_ATTRINDX_MASK)
212 #define ARM_VMSA_PTE_CONT_ENTRIES 16
213 #define ARM_VMSA_PTE_CONT_SIZE (PAGE_SIZE * ARM_VMSA_PTE_CONT_ENTRIES)
215 #define IPMMU_PTRS_PER_PTE 512
216 #define IPMMU_PTRS_PER_PMD 512
217 #define IPMMU_PTRS_PER_PGD 4
219 /* -----------------------------------------------------------------------------
223 static u32
ipmmu_read(struct ipmmu_vmsa_device
*mmu
, unsigned int offset
)
225 return ioread32(mmu
->base
+ offset
);
228 static void ipmmu_write(struct ipmmu_vmsa_device
*mmu
, unsigned int offset
,
231 iowrite32(data
, mmu
->base
+ offset
);
234 static u32
ipmmu_ctx_read(struct ipmmu_vmsa_domain
*domain
, unsigned int reg
)
236 return ipmmu_read(domain
->mmu
, domain
->context_id
* IM_CTX_SIZE
+ reg
);
239 static void ipmmu_ctx_write(struct ipmmu_vmsa_domain
*domain
, unsigned int reg
,
242 ipmmu_write(domain
->mmu
, domain
->context_id
* IM_CTX_SIZE
+ reg
, data
);
245 /* -----------------------------------------------------------------------------
246 * TLB and microTLB Management
249 /* Wait for any pending TLB invalidations to complete */
250 static void ipmmu_tlb_sync(struct ipmmu_vmsa_domain
*domain
)
252 unsigned int count
= 0;
254 while (ipmmu_ctx_read(domain
, IMCTR
) & IMCTR_FLUSH
) {
256 if (++count
== TLB_LOOP_TIMEOUT
) {
257 dev_err_ratelimited(domain
->mmu
->dev
,
258 "TLB sync timed out -- MMU may be deadlocked\n");
265 static void ipmmu_tlb_invalidate(struct ipmmu_vmsa_domain
*domain
)
269 reg
= ipmmu_ctx_read(domain
, IMCTR
);
271 ipmmu_ctx_write(domain
, IMCTR
, reg
);
273 ipmmu_tlb_sync(domain
);
277 * Enable MMU translation for the microTLB.
279 static void ipmmu_utlb_enable(struct ipmmu_vmsa_domain
*domain
,
282 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
285 * TODO: Reference-count the microTLB as several bus masters can be
286 * connected to the same microTLB.
289 /* TODO: What should we set the ASID to ? */
290 ipmmu_write(mmu
, IMUASID(utlb
), 0);
291 /* TODO: Do we need to flush the microTLB ? */
292 ipmmu_write(mmu
, IMUCTR(utlb
),
293 IMUCTR_TTSEL_MMU(domain
->context_id
) | IMUCTR_FLUSH
|
298 * Disable MMU translation for the microTLB.
300 static void ipmmu_utlb_disable(struct ipmmu_vmsa_domain
*domain
,
303 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
305 ipmmu_write(mmu
, IMUCTR(utlb
), 0);
308 static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device
*mmu
, void *addr
,
311 unsigned long offset
= (unsigned long)addr
& ~PAGE_MASK
;
314 * TODO: Add support for coherent walk through CCI with DVM and remove
317 dma_map_page(mmu
->dev
, virt_to_page(addr
), offset
, size
, DMA_TO_DEVICE
);
320 /* -----------------------------------------------------------------------------
321 * Domain/Context Management
324 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain
*domain
)
330 * TODO: When adding support for multiple contexts, find an unused
333 domain
->context_id
= 0;
336 ipmmu_flush_pgtable(domain
->mmu
, domain
->pgd
,
337 IPMMU_PTRS_PER_PGD
* sizeof(*domain
->pgd
));
338 ttbr
= __pa(domain
->pgd
);
339 ipmmu_ctx_write(domain
, IMTTLBR0
, ttbr
);
340 ipmmu_ctx_write(domain
, IMTTUBR0
, ttbr
>> 32);
344 * We use long descriptors with inner-shareable WBWA tables and allocate
345 * the whole 32-bit VA space to TTBR0.
347 ipmmu_ctx_write(domain
, IMTTBCR
, IMTTBCR_EAE
|
348 IMTTBCR_SH0_INNER_SHAREABLE
| IMTTBCR_ORGN0_WB_WA
|
349 IMTTBCR_IRGN0_WB_WA
| IMTTBCR_SL0_LVL_1
);
353 * We need three attributes only, non-cacheable, write-back read/write
354 * allocate and device memory.
356 reg
= (IMMAIR_ATTR_NC
<< IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC
))
357 | (IMMAIR_ATTR_WBRWA
<< IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA
))
358 | (IMMAIR_ATTR_DEVICE
<< IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV
));
359 ipmmu_ctx_write(domain
, IMMAIR0
, reg
);
362 ipmmu_ctx_write(domain
, IMBUSCR
,
363 ipmmu_ctx_read(domain
, IMBUSCR
) &
364 ~(IMBUSCR_DVM
| IMBUSCR_BUSSEL_MASK
));
368 * Clear all interrupt flags.
370 ipmmu_ctx_write(domain
, IMSTR
, ipmmu_ctx_read(domain
, IMSTR
));
374 * Enable the MMU and interrupt generation. The long-descriptor
375 * translation table format doesn't use TEX remapping. Don't enable AF
376 * software management as we have no use for it. Flush the TLB as
377 * required when modifying the context registers.
379 ipmmu_ctx_write(domain
, IMCTR
, IMCTR_INTEN
| IMCTR_FLUSH
| IMCTR_MMUEN
);
384 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain
*domain
)
387 * Disable the context. Flush the TLB as required when modifying the
390 * TODO: Is TLB flush really needed ?
392 ipmmu_ctx_write(domain
, IMCTR
, IMCTR_FLUSH
);
393 ipmmu_tlb_sync(domain
);
396 /* -----------------------------------------------------------------------------
400 static irqreturn_t
ipmmu_domain_irq(struct ipmmu_vmsa_domain
*domain
)
402 const u32 err_mask
= IMSTR_MHIT
| IMSTR_ABORT
| IMSTR_PF
| IMSTR_TF
;
403 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
407 status
= ipmmu_ctx_read(domain
, IMSTR
);
408 if (!(status
& err_mask
))
411 iova
= ipmmu_ctx_read(domain
, IMEAR
);
414 * Clear the error status flags. Unlike traditional interrupt flag
415 * registers that must be cleared by writing 1, this status register
416 * seems to require 0. The error address register must be read before,
417 * otherwise its value will be 0.
419 ipmmu_ctx_write(domain
, IMSTR
, 0);
421 /* Log fatal errors. */
422 if (status
& IMSTR_MHIT
)
423 dev_err_ratelimited(mmu
->dev
, "Multiple TLB hits @0x%08x\n",
425 if (status
& IMSTR_ABORT
)
426 dev_err_ratelimited(mmu
->dev
, "Page Table Walk Abort @0x%08x\n",
429 if (!(status
& (IMSTR_PF
| IMSTR_TF
)))
433 * Try to handle page faults and translation faults.
435 * TODO: We need to look up the faulty device based on the I/O VA. Use
436 * the IOMMU device for now.
438 if (!report_iommu_fault(domain
->io_domain
, mmu
->dev
, iova
, 0))
441 dev_err_ratelimited(mmu
->dev
,
442 "Unhandled fault: status 0x%08x iova 0x%08x\n",
448 static irqreturn_t
ipmmu_irq(int irq
, void *dev
)
450 struct ipmmu_vmsa_device
*mmu
= dev
;
451 struct iommu_domain
*io_domain
;
452 struct ipmmu_vmsa_domain
*domain
;
457 io_domain
= mmu
->mapping
->domain
;
458 domain
= io_domain
->priv
;
460 return ipmmu_domain_irq(domain
);
463 /* -----------------------------------------------------------------------------
464 * Page Table Management
467 #define pud_pgtable(pud) pfn_to_page(__phys_to_pfn(pud_val(pud) & PHYS_MASK))
469 static void ipmmu_free_ptes(pmd_t
*pmd
)
471 pgtable_t table
= pmd_pgtable(*pmd
);
475 static void ipmmu_free_pmds(pud_t
*pud
)
477 pmd_t
*pmd
= pmd_offset(pud
, 0);
481 for (i
= 0; i
< IPMMU_PTRS_PER_PMD
; ++i
) {
482 if (!pmd_table(*pmd
))
485 ipmmu_free_ptes(pmd
);
489 table
= pud_pgtable(*pud
);
493 static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain
*domain
)
495 pgd_t
*pgd
, *pgd_base
= domain
->pgd
;
499 * Recursively free the page tables for this domain. We don't care about
500 * speculative TLB filling, because the TLB will be nuked next time this
501 * context bank is re-allocated and no devices currently map to these
505 for (i
= 0; i
< IPMMU_PTRS_PER_PGD
; ++i
) {
508 ipmmu_free_pmds((pud_t
*)pgd
);
516 * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte)
517 * functions as they would flush the CPU TLB.
520 static pte_t
*ipmmu_alloc_pte(struct ipmmu_vmsa_device
*mmu
, pmd_t
*pmd
,
526 return pte_offset_kernel(pmd
, iova
);
528 pte
= (pte_t
*)get_zeroed_page(GFP_ATOMIC
);
532 ipmmu_flush_pgtable(mmu
, pte
, PAGE_SIZE
);
533 *pmd
= __pmd(__pa(pte
) | PMD_NSTABLE
| PMD_TYPE_TABLE
);
534 ipmmu_flush_pgtable(mmu
, pmd
, sizeof(*pmd
));
536 return pte
+ pte_index(iova
);
539 static pmd_t
*ipmmu_alloc_pmd(struct ipmmu_vmsa_device
*mmu
, pgd_t
*pgd
,
542 pud_t
*pud
= (pud_t
*)pgd
;
546 return pmd_offset(pud
, iova
);
548 pmd
= (pmd_t
*)get_zeroed_page(GFP_ATOMIC
);
552 ipmmu_flush_pgtable(mmu
, pmd
, PAGE_SIZE
);
553 *pud
= __pud(__pa(pmd
) | PMD_NSTABLE
| PMD_TYPE_TABLE
);
554 ipmmu_flush_pgtable(mmu
, pud
, sizeof(*pud
));
556 return pmd
+ pmd_index(iova
);
559 static u64
ipmmu_page_prot(unsigned int prot
, u64 type
)
561 u64 pgprot
= ARM_VMSA_PTE_XN
| ARM_VMSA_PTE_nG
| ARM_VMSA_PTE_AF
562 | ARM_VMSA_PTE_SH_IS
| ARM_VMSA_PTE_AP_UNPRIV
563 | ARM_VMSA_PTE_NS
| type
;
565 if (!(prot
& IOMMU_WRITE
) && (prot
& IOMMU_READ
))
566 pgprot
|= ARM_VMSA_PTE_AP_RDONLY
;
568 if (prot
& IOMMU_CACHE
)
569 pgprot
|= IMMAIR_ATTR_IDX_WBRWA
<< ARM_VMSA_PTE_ATTRINDX_SHIFT
;
571 if (prot
& IOMMU_EXEC
)
572 pgprot
&= ~ARM_VMSA_PTE_XN
;
573 else if (!(prot
& (IOMMU_READ
| IOMMU_WRITE
)))
574 /* If no access create a faulting entry to avoid TLB fills. */
575 pgprot
&= ~ARM_VMSA_PTE_PAGE
;
580 static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device
*mmu
, pmd_t
*pmd
,
581 unsigned long iova
, unsigned long pfn
,
582 size_t size
, int prot
)
584 pteval_t pteval
= ipmmu_page_prot(prot
, ARM_VMSA_PTE_PAGE
);
585 unsigned int num_ptes
= 1;
589 pte
= ipmmu_alloc_pte(mmu
, pmd
, iova
);
596 * Install the page table entries. We can be called both for a single
597 * page or for a block of 16 physically contiguous pages. In the latter
598 * case set the PTE contiguous hint.
600 if (size
== SZ_64K
) {
601 pteval
|= ARM_VMSA_PTE_CONT
;
602 num_ptes
= ARM_VMSA_PTE_CONT_ENTRIES
;
605 for (i
= num_ptes
; i
; --i
)
606 *pte
++ = pfn_pte(pfn
++, __pgprot(pteval
));
608 ipmmu_flush_pgtable(mmu
, start
, sizeof(*pte
) * num_ptes
);
613 static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device
*mmu
, pmd_t
*pmd
,
614 unsigned long iova
, unsigned long pfn
,
617 pmdval_t pmdval
= ipmmu_page_prot(prot
, PMD_TYPE_SECT
);
619 *pmd
= pfn_pmd(pfn
, __pgprot(pmdval
));
620 ipmmu_flush_pgtable(mmu
, pmd
, sizeof(*pmd
));
625 static int ipmmu_create_mapping(struct ipmmu_vmsa_domain
*domain
,
626 unsigned long iova
, phys_addr_t paddr
,
627 size_t size
, int prot
)
629 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
630 pgd_t
*pgd
= domain
->pgd
;
639 if (size
& ~PAGE_MASK
)
642 if (paddr
& ~((1ULL << 40) - 1))
645 pfn
= __phys_to_pfn(paddr
);
646 pgd
+= pgd_index(iova
);
648 /* Update the page tables. */
649 spin_lock_irqsave(&domain
->lock
, flags
);
651 pmd
= ipmmu_alloc_pmd(mmu
, pgd
, iova
);
659 ret
= ipmmu_alloc_init_pmd(mmu
, pmd
, iova
, pfn
, prot
);
663 ret
= ipmmu_alloc_init_pte(mmu
, pmd
, iova
, pfn
, size
, prot
);
671 spin_unlock_irqrestore(&domain
->lock
, flags
);
674 ipmmu_tlb_invalidate(domain
);
679 static void ipmmu_clear_pud(struct ipmmu_vmsa_device
*mmu
, pud_t
*pud
)
681 /* Free the page table. */
682 pgtable_t table
= pud_pgtable(*pud
);
687 ipmmu_flush_pgtable(mmu
, pud
, sizeof(*pud
));
690 static void ipmmu_clear_pmd(struct ipmmu_vmsa_device
*mmu
, pud_t
*pud
,
695 /* Free the page table. */
696 if (pmd_table(*pmd
)) {
697 pgtable_t table
= pmd_pgtable(*pmd
);
703 ipmmu_flush_pgtable(mmu
, pmd
, sizeof(*pmd
));
705 /* Check whether the PUD is still needed. */
706 pmd
= pmd_offset(pud
, 0);
707 for (i
= 0; i
< IPMMU_PTRS_PER_PMD
; ++i
) {
708 if (!pmd_none(pmd
[i
]))
712 /* Clear the parent PUD. */
713 ipmmu_clear_pud(mmu
, pud
);
716 static void ipmmu_clear_pte(struct ipmmu_vmsa_device
*mmu
, pud_t
*pud
,
717 pmd_t
*pmd
, pte_t
*pte
, unsigned int num_ptes
)
722 for (i
= num_ptes
; i
; --i
)
725 ipmmu_flush_pgtable(mmu
, pte
, sizeof(*pte
) * num_ptes
);
727 /* Check whether the PMD is still needed. */
728 pte
= pte_offset_kernel(pmd
, 0);
729 for (i
= 0; i
< IPMMU_PTRS_PER_PTE
; ++i
) {
730 if (!pte_none(pte
[i
]))
734 /* Clear the parent PMD. */
735 ipmmu_clear_pmd(mmu
, pud
, pmd
);
738 static int ipmmu_split_pmd(struct ipmmu_vmsa_device
*mmu
, pmd_t
*pmd
)
745 pte
= (pte_t
*)get_zeroed_page(GFP_ATOMIC
);
749 /* Copy the PMD attributes. */
750 pteval
= (pmd_val(*pmd
) & ARM_VMSA_PTE_ATTRS_MASK
)
751 | ARM_VMSA_PTE_CONT
| ARM_VMSA_PTE_PAGE
;
756 for (i
= IPMMU_PTRS_PER_PTE
; i
; --i
)
757 *pte
++ = pfn_pte(pfn
++, __pgprot(pteval
));
759 ipmmu_flush_pgtable(mmu
, start
, PAGE_SIZE
);
760 *pmd
= __pmd(__pa(start
) | PMD_NSTABLE
| PMD_TYPE_TABLE
);
761 ipmmu_flush_pgtable(mmu
, pmd
, sizeof(*pmd
));
766 static void ipmmu_split_pte(struct ipmmu_vmsa_device
*mmu
, pte_t
*pte
)
770 for (i
= ARM_VMSA_PTE_CONT_ENTRIES
; i
; --i
)
771 pte
[i
-1] = __pte(pte_val(*pte
) & ~ARM_VMSA_PTE_CONT
);
773 ipmmu_flush_pgtable(mmu
, pte
, sizeof(*pte
) * ARM_VMSA_PTE_CONT_ENTRIES
);
776 static int ipmmu_clear_mapping(struct ipmmu_vmsa_domain
*domain
,
777 unsigned long iova
, size_t size
)
779 struct ipmmu_vmsa_device
*mmu
= domain
->mmu
;
781 pgd_t
*pgd
= domain
->pgd
;
790 if (size
& ~PAGE_MASK
)
793 pgd
+= pgd_index(iova
);
796 spin_lock_irqsave(&domain
->lock
, flags
);
798 /* If there's no PUD or PMD we're done. */
802 pmd
= pmd_offset(pud
, iova
);
807 * When freeing a 2MB block just clear the PMD. In the unlikely case the
808 * block is mapped as individual pages this will free the corresponding
812 ipmmu_clear_pmd(mmu
, pud
, pmd
);
817 * If the PMD has been mapped as a section remap it as pages to allow
818 * freeing individual pages.
821 ipmmu_split_pmd(mmu
, pmd
);
823 pte
= pte_offset_kernel(pmd
, iova
);
826 * When freeing a 64kB block just clear the PTE entries. We don't have
827 * to care about the contiguous hint of the surrounding entries.
829 if (size
== SZ_64K
) {
830 ipmmu_clear_pte(mmu
, pud
, pmd
, pte
, ARM_VMSA_PTE_CONT_ENTRIES
);
835 * If the PTE has been mapped with the contiguous hint set remap it and
836 * its surrounding PTEs to allow unmapping a single page.
838 if (pte_val(*pte
) & ARM_VMSA_PTE_CONT
)
839 ipmmu_split_pte(mmu
, pte
);
842 ipmmu_clear_pte(mmu
, pud
, pmd
, pte
, 1);
845 spin_unlock_irqrestore(&domain
->lock
, flags
);
848 ipmmu_tlb_invalidate(domain
);
853 /* -----------------------------------------------------------------------------
857 static int ipmmu_domain_init(struct iommu_domain
*io_domain
)
859 struct ipmmu_vmsa_domain
*domain
;
861 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
865 spin_lock_init(&domain
->lock
);
867 domain
->pgd
= kzalloc(IPMMU_PTRS_PER_PGD
* sizeof(pgd_t
), GFP_KERNEL
);
873 io_domain
->priv
= domain
;
874 domain
->io_domain
= io_domain
;
879 static void ipmmu_domain_destroy(struct iommu_domain
*io_domain
)
881 struct ipmmu_vmsa_domain
*domain
= io_domain
->priv
;
884 * Free the domain resources. We assume that all devices have already
887 ipmmu_domain_destroy_context(domain
);
888 ipmmu_free_pgtables(domain
);
892 static int ipmmu_attach_device(struct iommu_domain
*io_domain
,
895 struct ipmmu_vmsa_archdata
*archdata
= dev
->archdata
.iommu
;
896 struct ipmmu_vmsa_device
*mmu
= archdata
->mmu
;
897 struct ipmmu_vmsa_domain
*domain
= io_domain
->priv
;
902 dev_err(dev
, "Cannot attach to IPMMU\n");
906 spin_lock_irqsave(&domain
->lock
, flags
);
909 /* The domain hasn't been used yet, initialize it. */
911 ret
= ipmmu_domain_init_context(domain
);
912 } else if (domain
->mmu
!= mmu
) {
914 * Something is wrong, we can't attach two devices using
915 * different IOMMUs to the same domain.
917 dev_err(dev
, "Can't attach IPMMU %s to domain on IPMMU %s\n",
918 dev_name(mmu
->dev
), dev_name(domain
->mmu
->dev
));
922 spin_unlock_irqrestore(&domain
->lock
, flags
);
927 ipmmu_utlb_enable(domain
, archdata
->utlb
);
932 static void ipmmu_detach_device(struct iommu_domain
*io_domain
,
935 struct ipmmu_vmsa_archdata
*archdata
= dev
->archdata
.iommu
;
936 struct ipmmu_vmsa_domain
*domain
= io_domain
->priv
;
938 ipmmu_utlb_disable(domain
, archdata
->utlb
);
941 * TODO: Optimize by disabling the context when no device is attached.
945 static int ipmmu_map(struct iommu_domain
*io_domain
, unsigned long iova
,
946 phys_addr_t paddr
, size_t size
, int prot
)
948 struct ipmmu_vmsa_domain
*domain
= io_domain
->priv
;
953 return ipmmu_create_mapping(domain
, iova
, paddr
, size
, prot
);
956 static size_t ipmmu_unmap(struct iommu_domain
*io_domain
, unsigned long iova
,
959 struct ipmmu_vmsa_domain
*domain
= io_domain
->priv
;
962 ret
= ipmmu_clear_mapping(domain
, iova
, size
);
963 return ret
? 0 : size
;
966 static phys_addr_t
ipmmu_iova_to_phys(struct iommu_domain
*io_domain
,
969 struct ipmmu_vmsa_domain
*domain
= io_domain
->priv
;
975 /* TODO: Is locking needed ? */
980 pgd
= *(domain
->pgd
+ pgd_index(iova
));
984 pud
= *pud_offset(&pgd
, iova
);
988 pmd
= *pmd_offset(&pud
, iova
);
993 return __pfn_to_phys(pmd_pfn(pmd
)) | (iova
& ~PMD_MASK
);
995 pte
= *(pmd_page_vaddr(pmd
) + pte_index(iova
));
999 return __pfn_to_phys(pte_pfn(pte
)) | (iova
& ~PAGE_MASK
);
1002 static int ipmmu_find_utlb(struct ipmmu_vmsa_device
*mmu
, struct device
*dev
)
1004 const struct ipmmu_vmsa_master
*master
= mmu
->pdata
->masters
;
1005 const char *devname
= dev_name(dev
);
1008 for (i
= 0; i
< mmu
->pdata
->num_masters
; ++i
, ++master
) {
1009 if (strcmp(master
->name
, devname
) == 0)
1010 return master
->utlb
;
1016 static int ipmmu_add_device(struct device
*dev
)
1018 struct ipmmu_vmsa_archdata
*archdata
;
1019 struct ipmmu_vmsa_device
*mmu
;
1020 struct iommu_group
*group
;
1024 if (dev
->archdata
.iommu
) {
1025 dev_warn(dev
, "IOMMU driver already assigned to device %s\n",
1030 /* Find the master corresponding to the device. */
1031 spin_lock(&ipmmu_devices_lock
);
1033 list_for_each_entry(mmu
, &ipmmu_devices
, list
) {
1034 utlb
= ipmmu_find_utlb(mmu
, dev
);
1037 * TODO Take a reference to the MMU to protect
1038 * against device removal.
1044 spin_unlock(&ipmmu_devices_lock
);
1049 if (utlb
>= mmu
->num_utlbs
)
1052 /* Create a device group and add the device to it. */
1053 group
= iommu_group_alloc();
1054 if (IS_ERR(group
)) {
1055 dev_err(dev
, "Failed to allocate IOMMU group\n");
1056 return PTR_ERR(group
);
1059 ret
= iommu_group_add_device(group
, dev
);
1060 iommu_group_put(group
);
1063 dev_err(dev
, "Failed to add device to IPMMU group\n");
1067 archdata
= kzalloc(sizeof(*archdata
), GFP_KERNEL
);
1073 archdata
->mmu
= mmu
;
1074 archdata
->utlb
= utlb
;
1075 dev
->archdata
.iommu
= archdata
;
1078 * Create the ARM mapping, used by the ARM DMA mapping core to allocate
1079 * VAs. This will allocate a corresponding IOMMU domain.
1082 * - Create one mapping per context (TLB).
1083 * - Make the mapping size configurable ? We currently use a 2GB mapping
1084 * at a 1GB offset to ensure that NULL VAs will fault.
1086 if (!mmu
->mapping
) {
1087 struct dma_iommu_mapping
*mapping
;
1089 mapping
= arm_iommu_create_mapping(&platform_bus_type
,
1091 if (IS_ERR(mapping
)) {
1092 dev_err(mmu
->dev
, "failed to create ARM IOMMU mapping\n");
1093 return PTR_ERR(mapping
);
1096 mmu
->mapping
= mapping
;
1099 /* Attach the ARM VA mapping to the device. */
1100 ret
= arm_iommu_attach_device(dev
, mmu
->mapping
);
1102 dev_err(dev
, "Failed to attach device to VA mapping\n");
1109 kfree(dev
->archdata
.iommu
);
1110 dev
->archdata
.iommu
= NULL
;
1111 iommu_group_remove_device(dev
);
1115 static void ipmmu_remove_device(struct device
*dev
)
1117 arm_iommu_detach_device(dev
);
1118 iommu_group_remove_device(dev
);
1119 kfree(dev
->archdata
.iommu
);
1120 dev
->archdata
.iommu
= NULL
;
1123 static const struct iommu_ops ipmmu_ops
= {
1124 .domain_init
= ipmmu_domain_init
,
1125 .domain_destroy
= ipmmu_domain_destroy
,
1126 .attach_dev
= ipmmu_attach_device
,
1127 .detach_dev
= ipmmu_detach_device
,
1129 .unmap
= ipmmu_unmap
,
1130 .iova_to_phys
= ipmmu_iova_to_phys
,
1131 .add_device
= ipmmu_add_device
,
1132 .remove_device
= ipmmu_remove_device
,
1133 .pgsize_bitmap
= SZ_2M
| SZ_64K
| SZ_4K
,
1136 /* -----------------------------------------------------------------------------
1137 * Probe/remove and init
1140 static void ipmmu_device_reset(struct ipmmu_vmsa_device
*mmu
)
1144 /* Disable all contexts. */
1145 for (i
= 0; i
< 4; ++i
)
1146 ipmmu_write(mmu
, i
* IM_CTX_SIZE
+ IMCTR
, 0);
1149 static int ipmmu_probe(struct platform_device
*pdev
)
1151 struct ipmmu_vmsa_device
*mmu
;
1152 struct resource
*res
;
1156 if (!pdev
->dev
.platform_data
) {
1157 dev_err(&pdev
->dev
, "missing platform data\n");
1161 mmu
= devm_kzalloc(&pdev
->dev
, sizeof(*mmu
), GFP_KERNEL
);
1163 dev_err(&pdev
->dev
, "cannot allocate device data\n");
1167 mmu
->dev
= &pdev
->dev
;
1168 mmu
->pdata
= pdev
->dev
.platform_data
;
1169 mmu
->num_utlbs
= 32;
1171 /* Map I/O memory and request IRQ. */
1172 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1173 mmu
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1174 if (IS_ERR(mmu
->base
))
1175 return PTR_ERR(mmu
->base
);
1177 irq
= platform_get_irq(pdev
, 0);
1179 dev_err(&pdev
->dev
, "no IRQ found\n");
1183 ret
= devm_request_irq(&pdev
->dev
, irq
, ipmmu_irq
, 0,
1184 dev_name(&pdev
->dev
), mmu
);
1186 dev_err(&pdev
->dev
, "failed to request IRQ %d\n", irq
);
1190 ipmmu_device_reset(mmu
);
1193 * We can't create the ARM mapping here as it requires the bus to have
1194 * an IOMMU, which only happens when bus_set_iommu() is called in
1195 * ipmmu_init() after the probe function returns.
1198 spin_lock(&ipmmu_devices_lock
);
1199 list_add(&mmu
->list
, &ipmmu_devices
);
1200 spin_unlock(&ipmmu_devices_lock
);
1202 platform_set_drvdata(pdev
, mmu
);
1207 static int ipmmu_remove(struct platform_device
*pdev
)
1209 struct ipmmu_vmsa_device
*mmu
= platform_get_drvdata(pdev
);
1211 spin_lock(&ipmmu_devices_lock
);
1212 list_del(&mmu
->list
);
1213 spin_unlock(&ipmmu_devices_lock
);
1215 arm_iommu_release_mapping(mmu
->mapping
);
1217 ipmmu_device_reset(mmu
);
1222 static struct platform_driver ipmmu_driver
= {
1224 .owner
= THIS_MODULE
,
1225 .name
= "ipmmu-vmsa",
1227 .probe
= ipmmu_probe
,
1228 .remove
= ipmmu_remove
,
1231 static int __init
ipmmu_init(void)
1235 ret
= platform_driver_register(&ipmmu_driver
);
1239 if (!iommu_present(&platform_bus_type
))
1240 bus_set_iommu(&platform_bus_type
, &ipmmu_ops
);
1245 static void __exit
ipmmu_exit(void)
1247 return platform_driver_unregister(&ipmmu_driver
);
1250 subsys_initcall(ipmmu_init
);
1251 module_exit(ipmmu_exit
);
1253 MODULE_DESCRIPTION("IOMMU API for Renesas VMSA-compatible IPMMU");
1254 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1255 MODULE_LICENSE("GPL v2");