1 // SPDX-License-Identifier: GPL-2.0-only
3 * CPU-agnostic ARM page table allocator.
5 * ARMv7 Short-descriptor format, supporting
6 * - Basic memory attributes
7 * - Simplified access permissions (AP[2:1] model)
8 * - Backwards-compatible TEX remap
9 * - Large pages/supersections (if indicated by the caller)
12 * - Legacy access permissions (AP[2:0] model)
14 * Almost certainly never supporting:
18 * Copyright (C) 2014-2015 ARM Limited
19 * Copyright (c) 2014-2015 MediaTek Inc.
22 #define pr_fmt(fmt) "arm-v7s io-pgtable: " fmt
24 #include <linux/atomic.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/gfp.h>
27 #include <linux/io-pgtable.h>
28 #include <linux/iommu.h>
29 #include <linux/kernel.h>
30 #include <linux/kmemleak.h>
31 #include <linux/sizes.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/types.h>
36 #include <asm/barrier.h>
38 /* Struct accessors */
39 #define io_pgtable_to_data(x) \
40 container_of((x), struct arm_v7s_io_pgtable, iop)
42 #define io_pgtable_ops_to_data(x) \
43 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
46 * We have 32 bits total; 12 bits resolved at level 1, 8 bits at level 2,
47 * and 12 bits in a page.
48 * MediaTek extend 2 bits to reach 34bits, 14 bits at lvl1 and 8 bits at lvl2.
50 #define ARM_V7S_ADDR_BITS 32
51 #define _ARM_V7S_LVL_BITS(lvl, cfg) ((lvl) == 1 ? ((cfg)->ias - 20) : 8)
52 #define ARM_V7S_LVL_SHIFT(lvl) ((lvl) == 1 ? 20 : 12)
53 #define ARM_V7S_TABLE_SHIFT 10
55 #define ARM_V7S_PTES_PER_LVL(lvl, cfg) (1 << _ARM_V7S_LVL_BITS(lvl, cfg))
56 #define ARM_V7S_TABLE_SIZE(lvl, cfg) \
57 (ARM_V7S_PTES_PER_LVL(lvl, cfg) * sizeof(arm_v7s_iopte))
59 #define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl))
60 #define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl)))
61 #define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT))
62 #define _ARM_V7S_IDX_MASK(lvl, cfg) (ARM_V7S_PTES_PER_LVL(lvl, cfg) - 1)
63 #define ARM_V7S_LVL_IDX(addr, lvl, cfg) ({ \
65 ((addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l, cfg); \
69 * Large page/supersection entries are effectively a block of 16 page/section
70 * entries, along the lines of the LPAE contiguous hint, but all with the
71 * same output address. For want of a better common name we'll call them
72 * "contiguous" versions of their respective page/section entries here, but
73 * noting the distinction (WRT to TLB maintenance) that they represent *one*
74 * entry repeated 16 times, not 16 separate entries (as in the LPAE case).
76 #define ARM_V7S_CONT_PAGES 16
78 /* PTE type bits: these are all mixed up with XN/PXN bits in most cases */
79 #define ARM_V7S_PTE_TYPE_TABLE 0x1
80 #define ARM_V7S_PTE_TYPE_PAGE 0x2
81 #define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1
83 #define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0)
84 #define ARM_V7S_PTE_IS_TABLE(pte, lvl) \
85 ((lvl) == 1 && (((pte) & 0x3) == ARM_V7S_PTE_TYPE_TABLE))
88 #define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl)))
89 #define ARM_V7S_ATTR_B BIT(2)
90 #define ARM_V7S_ATTR_C BIT(3)
91 #define ARM_V7S_ATTR_NS_TABLE BIT(3)
92 #define ARM_V7S_ATTR_NS_SECTION BIT(19)
94 #define ARM_V7S_CONT_SECTION BIT(18)
95 #define ARM_V7S_CONT_PAGE_XN_SHIFT 15
98 * The attribute bits are consistently ordered*, but occupy bits [17:10] of
99 * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual
100 * fields relative to that 8-bit block, plus a total shift relative to the PTE.
102 #define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6)
104 #define ARM_V7S_ATTR_MASK 0xff
105 #define ARM_V7S_ATTR_AP0 BIT(0)
106 #define ARM_V7S_ATTR_AP1 BIT(1)
107 #define ARM_V7S_ATTR_AP2 BIT(5)
108 #define ARM_V7S_ATTR_S BIT(6)
109 #define ARM_V7S_ATTR_NG BIT(7)
110 #define ARM_V7S_TEX_SHIFT 2
111 #define ARM_V7S_TEX_MASK 0x7
112 #define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT)
114 /* MediaTek extend the bits below for PA 32bit/33bit/34bit */
115 #define ARM_V7S_ATTR_MTK_PA_BIT32 BIT(9)
116 #define ARM_V7S_ATTR_MTK_PA_BIT33 BIT(4)
117 #define ARM_V7S_ATTR_MTK_PA_BIT34 BIT(5)
119 /* *well, except for TEX on level 2 large pages, of course :( */
120 #define ARM_V7S_CONT_PAGE_TEX_SHIFT 6
121 #define ARM_V7S_CONT_PAGE_TEX_MASK (ARM_V7S_TEX_MASK << ARM_V7S_CONT_PAGE_TEX_SHIFT)
123 /* Simplified access permissions */
124 #define ARM_V7S_PTE_AF ARM_V7S_ATTR_AP0
125 #define ARM_V7S_PTE_AP_UNPRIV ARM_V7S_ATTR_AP1
126 #define ARM_V7S_PTE_AP_RDONLY ARM_V7S_ATTR_AP2
129 #define ARM_V7S_RGN_NC 0
130 #define ARM_V7S_RGN_WBWA 1
131 #define ARM_V7S_RGN_WT 2
132 #define ARM_V7S_RGN_WB 3
134 #define ARM_V7S_PRRR_TYPE_DEVICE 1
135 #define ARM_V7S_PRRR_TYPE_NORMAL 2
136 #define ARM_V7S_PRRR_TR(n, type) (((type) & 0x3) << ((n) * 2))
137 #define ARM_V7S_PRRR_DS0 BIT(16)
138 #define ARM_V7S_PRRR_DS1 BIT(17)
139 #define ARM_V7S_PRRR_NS0 BIT(18)
140 #define ARM_V7S_PRRR_NS1 BIT(19)
141 #define ARM_V7S_PRRR_NOS(n) BIT((n) + 24)
143 #define ARM_V7S_NMRR_IR(n, attr) (((attr) & 0x3) << ((n) * 2))
144 #define ARM_V7S_NMRR_OR(n, attr) (((attr) & 0x3) << ((n) * 2 + 16))
146 #define ARM_V7S_TTBR_S BIT(1)
147 #define ARM_V7S_TTBR_NOS BIT(5)
148 #define ARM_V7S_TTBR_ORGN_ATTR(attr) (((attr) & 0x3) << 3)
149 #define ARM_V7S_TTBR_IRGN_ATTR(attr) \
150 ((((attr) & 0x1) << 6) | (((attr) & 0x2) >> 1))
152 #ifdef CONFIG_ZONE_DMA32
153 #define ARM_V7S_TABLE_GFP_DMA GFP_DMA32
154 #define ARM_V7S_TABLE_SLAB_FLAGS SLAB_CACHE_DMA32
156 #define ARM_V7S_TABLE_GFP_DMA GFP_DMA
157 #define ARM_V7S_TABLE_SLAB_FLAGS SLAB_CACHE_DMA
160 typedef u32 arm_v7s_iopte
;
162 static bool selftest_running
;
164 struct arm_v7s_io_pgtable
{
165 struct io_pgtable iop
;
168 struct kmem_cache
*l2_tables
;
171 static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte
, int lvl
);
173 static dma_addr_t
__arm_v7s_dma_addr(void *pages
)
175 return (dma_addr_t
)virt_to_phys(pages
);
178 static bool arm_v7s_is_mtk_enabled(struct io_pgtable_cfg
*cfg
)
180 return IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT
) &&
181 (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_EXT
);
184 static arm_v7s_iopte
to_mtk_iopte(phys_addr_t paddr
, arm_v7s_iopte pte
)
186 if (paddr
& BIT_ULL(32))
187 pte
|= ARM_V7S_ATTR_MTK_PA_BIT32
;
188 if (paddr
& BIT_ULL(33))
189 pte
|= ARM_V7S_ATTR_MTK_PA_BIT33
;
190 if (paddr
& BIT_ULL(34))
191 pte
|= ARM_V7S_ATTR_MTK_PA_BIT34
;
195 static arm_v7s_iopte
paddr_to_iopte(phys_addr_t paddr
, int lvl
,
196 struct io_pgtable_cfg
*cfg
)
198 arm_v7s_iopte pte
= paddr
& ARM_V7S_LVL_MASK(lvl
);
200 if (arm_v7s_is_mtk_enabled(cfg
))
201 return to_mtk_iopte(paddr
, pte
);
206 static phys_addr_t
iopte_to_paddr(arm_v7s_iopte pte
, int lvl
,
207 struct io_pgtable_cfg
*cfg
)
212 if (ARM_V7S_PTE_IS_TABLE(pte
, lvl
))
213 mask
= ARM_V7S_TABLE_MASK
;
214 else if (arm_v7s_pte_is_cont(pte
, lvl
))
215 mask
= ARM_V7S_LVL_MASK(lvl
) * ARM_V7S_CONT_PAGES
;
217 mask
= ARM_V7S_LVL_MASK(lvl
);
220 if (!arm_v7s_is_mtk_enabled(cfg
))
223 if (pte
& ARM_V7S_ATTR_MTK_PA_BIT32
)
224 paddr
|= BIT_ULL(32);
225 if (pte
& ARM_V7S_ATTR_MTK_PA_BIT33
)
226 paddr
|= BIT_ULL(33);
227 if (pte
& ARM_V7S_ATTR_MTK_PA_BIT34
)
228 paddr
|= BIT_ULL(34);
232 static arm_v7s_iopte
*iopte_deref(arm_v7s_iopte pte
, int lvl
,
233 struct arm_v7s_io_pgtable
*data
)
235 return phys_to_virt(iopte_to_paddr(pte
, lvl
, &data
->iop
.cfg
));
238 static void *__arm_v7s_alloc_table(int lvl
, gfp_t gfp
,
239 struct arm_v7s_io_pgtable
*data
)
241 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
242 struct device
*dev
= cfg
->iommu_dev
;
245 size_t size
= ARM_V7S_TABLE_SIZE(lvl
, cfg
);
250 * ARM_MTK_TTBR_EXT extend the translation table base support larger
253 gfp_l1
= cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT
?
254 GFP_KERNEL
: ARM_V7S_TABLE_GFP_DMA
;
257 table
= (void *)__get_free_pages(gfp_l1
| __GFP_ZERO
, get_order(size
));
259 table
= kmem_cache_zalloc(data
->l2_tables
, gfp
);
264 phys
= virt_to_phys(table
);
265 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT
?
266 phys
>= (1ULL << cfg
->oas
) : phys
!= (arm_v7s_iopte
)phys
) {
267 /* Doesn't fit in PTE */
268 dev_err(dev
, "Page table does not fit in PTE: %pa", &phys
);
271 if (!cfg
->coherent_walk
) {
272 dma
= dma_map_single(dev
, table
, size
, DMA_TO_DEVICE
);
273 if (dma_mapping_error(dev
, dma
))
276 * We depend on the IOMMU being able to work with any physical
277 * address directly, so if the DMA layer suggests otherwise by
278 * translating or truncating them, that bodes very badly...
284 kmemleak_ignore(table
);
288 dev_err(dev
, "Cannot accommodate DMA translation for IOMMU page tables\n");
289 dma_unmap_single(dev
, dma
, size
, DMA_TO_DEVICE
);
292 free_pages((unsigned long)table
, get_order(size
));
294 kmem_cache_free(data
->l2_tables
, table
);
298 static void __arm_v7s_free_table(void *table
, int lvl
,
299 struct arm_v7s_io_pgtable
*data
)
301 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
302 struct device
*dev
= cfg
->iommu_dev
;
303 size_t size
= ARM_V7S_TABLE_SIZE(lvl
, cfg
);
305 if (!cfg
->coherent_walk
)
306 dma_unmap_single(dev
, __arm_v7s_dma_addr(table
), size
,
309 free_pages((unsigned long)table
, get_order(size
));
311 kmem_cache_free(data
->l2_tables
, table
);
314 static void __arm_v7s_pte_sync(arm_v7s_iopte
*ptep
, int num_entries
,
315 struct io_pgtable_cfg
*cfg
)
317 if (cfg
->coherent_walk
)
320 dma_sync_single_for_device(cfg
->iommu_dev
, __arm_v7s_dma_addr(ptep
),
321 num_entries
* sizeof(*ptep
), DMA_TO_DEVICE
);
323 static void __arm_v7s_set_pte(arm_v7s_iopte
*ptep
, arm_v7s_iopte pte
,
324 int num_entries
, struct io_pgtable_cfg
*cfg
)
328 for (i
= 0; i
< num_entries
; i
++)
331 __arm_v7s_pte_sync(ptep
, num_entries
, cfg
);
334 static arm_v7s_iopte
arm_v7s_prot_to_pte(int prot
, int lvl
,
335 struct io_pgtable_cfg
*cfg
)
337 bool ap
= !(cfg
->quirks
& IO_PGTABLE_QUIRK_NO_PERMS
);
338 arm_v7s_iopte pte
= ARM_V7S_ATTR_NG
| ARM_V7S_ATTR_S
;
340 if (!(prot
& IOMMU_MMIO
))
341 pte
|= ARM_V7S_ATTR_TEX(1);
343 pte
|= ARM_V7S_PTE_AF
;
344 if (!(prot
& IOMMU_PRIV
))
345 pte
|= ARM_V7S_PTE_AP_UNPRIV
;
346 if (!(prot
& IOMMU_WRITE
))
347 pte
|= ARM_V7S_PTE_AP_RDONLY
;
349 pte
<<= ARM_V7S_ATTR_SHIFT(lvl
);
351 if ((prot
& IOMMU_NOEXEC
) && ap
)
352 pte
|= ARM_V7S_ATTR_XN(lvl
);
353 if (prot
& IOMMU_MMIO
)
354 pte
|= ARM_V7S_ATTR_B
;
355 else if (prot
& IOMMU_CACHE
)
356 pte
|= ARM_V7S_ATTR_B
| ARM_V7S_ATTR_C
;
358 pte
|= ARM_V7S_PTE_TYPE_PAGE
;
359 if (lvl
== 1 && (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_NS
))
360 pte
|= ARM_V7S_ATTR_NS_SECTION
;
365 static arm_v7s_iopte
arm_v7s_pte_to_cont(arm_v7s_iopte pte
, int lvl
)
368 pte
|= ARM_V7S_CONT_SECTION
;
369 } else if (lvl
== 2) {
370 arm_v7s_iopte xn
= pte
& ARM_V7S_ATTR_XN(lvl
);
371 arm_v7s_iopte tex
= pte
& ARM_V7S_CONT_PAGE_TEX_MASK
;
373 pte
^= xn
| tex
| ARM_V7S_PTE_TYPE_PAGE
;
374 pte
|= (xn
<< ARM_V7S_CONT_PAGE_XN_SHIFT
) |
375 (tex
<< ARM_V7S_CONT_PAGE_TEX_SHIFT
) |
376 ARM_V7S_PTE_TYPE_CONT_PAGE
;
381 static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte
, int lvl
)
383 if (lvl
== 1 && !ARM_V7S_PTE_IS_TABLE(pte
, lvl
))
384 return pte
& ARM_V7S_CONT_SECTION
;
386 return !(pte
& ARM_V7S_PTE_TYPE_PAGE
);
390 static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable
*,
391 struct iommu_iotlb_gather
*, unsigned long,
392 size_t, int, arm_v7s_iopte
*);
394 static int arm_v7s_init_pte(struct arm_v7s_io_pgtable
*data
,
395 unsigned long iova
, phys_addr_t paddr
, int prot
,
396 int lvl
, int num_entries
, arm_v7s_iopte
*ptep
)
398 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
402 for (i
= 0; i
< num_entries
; i
++)
403 if (ARM_V7S_PTE_IS_TABLE(ptep
[i
], lvl
)) {
405 * We need to unmap and free the old table before
406 * overwriting it with a block entry.
409 size_t sz
= ARM_V7S_BLOCK_SIZE(lvl
);
411 tblp
= ptep
- ARM_V7S_LVL_IDX(iova
, lvl
, cfg
);
412 if (WARN_ON(__arm_v7s_unmap(data
, NULL
, iova
+ i
* sz
,
413 sz
, lvl
, tblp
) != sz
))
415 } else if (ptep
[i
]) {
416 /* We require an unmap first */
417 WARN_ON(!selftest_running
);
421 pte
= arm_v7s_prot_to_pte(prot
, lvl
, cfg
);
423 pte
= arm_v7s_pte_to_cont(pte
, lvl
);
425 pte
|= paddr_to_iopte(paddr
, lvl
, cfg
);
427 __arm_v7s_set_pte(ptep
, pte
, num_entries
, cfg
);
431 static arm_v7s_iopte
arm_v7s_install_table(arm_v7s_iopte
*table
,
434 struct io_pgtable_cfg
*cfg
)
436 phys_addr_t phys
= virt_to_phys(table
);
437 arm_v7s_iopte old
, new;
439 new = phys
| ARM_V7S_PTE_TYPE_TABLE
;
441 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT
)
442 new = to_mtk_iopte(phys
, new);
444 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_NS
)
445 new |= ARM_V7S_ATTR_NS_TABLE
;
448 * Ensure the table itself is visible before its PTE can be.
449 * Whilst we could get away with cmpxchg64_release below, this
450 * doesn't have any ordering semantics when !CONFIG_SMP.
454 old
= cmpxchg_relaxed(ptep
, curr
, new);
455 __arm_v7s_pte_sync(ptep
, 1, cfg
);
460 static int __arm_v7s_map(struct arm_v7s_io_pgtable
*data
, unsigned long iova
,
461 phys_addr_t paddr
, size_t size
, int prot
,
462 int lvl
, arm_v7s_iopte
*ptep
, gfp_t gfp
)
464 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
465 arm_v7s_iopte pte
, *cptep
;
466 int num_entries
= size
>> ARM_V7S_LVL_SHIFT(lvl
);
468 /* Find our entry at the current level */
469 ptep
+= ARM_V7S_LVL_IDX(iova
, lvl
, cfg
);
471 /* If we can install a leaf entry at this level, then do so */
473 return arm_v7s_init_pte(data
, iova
, paddr
, prot
,
474 lvl
, num_entries
, ptep
);
476 /* We can't allocate tables at the final level */
477 if (WARN_ON(lvl
== 2))
480 /* Grab a pointer to the next level */
481 pte
= READ_ONCE(*ptep
);
483 cptep
= __arm_v7s_alloc_table(lvl
+ 1, gfp
, data
);
487 pte
= arm_v7s_install_table(cptep
, ptep
, 0, cfg
);
489 __arm_v7s_free_table(cptep
, lvl
+ 1, data
);
491 /* We've no easy way of knowing if it's synced yet, so... */
492 __arm_v7s_pte_sync(ptep
, 1, cfg
);
495 if (ARM_V7S_PTE_IS_TABLE(pte
, lvl
)) {
496 cptep
= iopte_deref(pte
, lvl
, data
);
498 /* We require an unmap first */
499 WARN_ON(!selftest_running
);
504 return __arm_v7s_map(data
, iova
, paddr
, size
, prot
, lvl
+ 1, cptep
, gfp
);
507 static int arm_v7s_map_pages(struct io_pgtable_ops
*ops
, unsigned long iova
,
508 phys_addr_t paddr
, size_t pgsize
, size_t pgcount
,
509 int prot
, gfp_t gfp
, size_t *mapped
)
511 struct arm_v7s_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
514 if (WARN_ON(iova
>= (1ULL << data
->iop
.cfg
.ias
) ||
515 paddr
>= (1ULL << data
->iop
.cfg
.oas
)))
518 if (!(prot
& (IOMMU_READ
| IOMMU_WRITE
)))
522 ret
= __arm_v7s_map(data
, iova
, paddr
, pgsize
, prot
, 1, data
->pgd
,
532 * Synchronise all PTE updates for the new mapping before there's
533 * a chance for anything to kick off a table walk for the new iova.
540 static void arm_v7s_free_pgtable(struct io_pgtable
*iop
)
542 struct arm_v7s_io_pgtable
*data
= io_pgtable_to_data(iop
);
545 for (i
= 0; i
< ARM_V7S_PTES_PER_LVL(1, &data
->iop
.cfg
); i
++) {
546 arm_v7s_iopte pte
= data
->pgd
[i
];
548 if (ARM_V7S_PTE_IS_TABLE(pte
, 1))
549 __arm_v7s_free_table(iopte_deref(pte
, 1, data
),
552 __arm_v7s_free_table(data
->pgd
, 1, data
);
553 kmem_cache_destroy(data
->l2_tables
);
557 static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable
*data
,
558 struct iommu_iotlb_gather
*gather
,
559 unsigned long iova
, size_t size
, int lvl
,
562 arm_v7s_iopte pte
[ARM_V7S_CONT_PAGES
];
563 struct io_pgtable
*iop
= &data
->iop
;
564 int idx
, i
= 0, num_entries
= size
>> ARM_V7S_LVL_SHIFT(lvl
);
566 /* Something went horribly wrong and we ran out of page table */
567 if (WARN_ON(lvl
> 2))
570 idx
= ARM_V7S_LVL_IDX(iova
, lvl
, &iop
->cfg
);
573 pte
[i
] = READ_ONCE(ptep
[i
]);
574 if (WARN_ON(!ARM_V7S_PTE_IS_VALID(pte
[i
])))
576 } while (++i
< num_entries
);
579 * If we've hit a contiguous 'large page' entry at this level, it
580 * needs splitting first, unless we're unmapping the whole lot.
582 * For splitting, we can't rewrite 16 PTEs atomically, and since we
583 * can't necessarily assume TEX remap we don't have a software bit to
584 * mark live entries being split. In practice (i.e. DMA API code), we
585 * will never be splitting large pages anyway, so just wrap this edge
586 * case in a lock for the sake of correctness and be done with it.
588 if (num_entries
<= 1 && arm_v7s_pte_is_cont(pte
[0], lvl
)) {
589 WARN_ONCE(true, "Unmap of a partial large IOPTE is not allowed");
593 /* If the size matches this level, we're in the right place */
595 size_t blk_size
= ARM_V7S_BLOCK_SIZE(lvl
);
597 __arm_v7s_set_pte(ptep
, 0, num_entries
, &iop
->cfg
);
599 for (i
= 0; i
< num_entries
; i
++) {
600 if (ARM_V7S_PTE_IS_TABLE(pte
[i
], lvl
)) {
601 /* Also flush any partial walks */
602 io_pgtable_tlb_flush_walk(iop
, iova
, blk_size
,
603 ARM_V7S_BLOCK_SIZE(lvl
+ 1));
604 ptep
= iopte_deref(pte
[i
], lvl
, data
);
605 __arm_v7s_free_table(ptep
, lvl
+ 1, data
);
606 } else if (!iommu_iotlb_gather_queued(gather
)) {
607 io_pgtable_tlb_add_page(iop
, gather
, iova
, blk_size
);
612 } else if (lvl
== 1 && !ARM_V7S_PTE_IS_TABLE(pte
[0], lvl
)) {
613 WARN_ONCE(true, "Unmap of a partial large IOPTE is not allowed");
617 /* Keep on walkin' */
618 ptep
= iopte_deref(pte
[0], lvl
, data
);
619 return __arm_v7s_unmap(data
, gather
, iova
, size
, lvl
+ 1, ptep
);
622 static size_t arm_v7s_unmap_pages(struct io_pgtable_ops
*ops
, unsigned long iova
,
623 size_t pgsize
, size_t pgcount
,
624 struct iommu_iotlb_gather
*gather
)
626 struct arm_v7s_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
627 size_t unmapped
= 0, ret
;
629 if (WARN_ON(iova
>= (1ULL << data
->iop
.cfg
.ias
)))
633 ret
= __arm_v7s_unmap(data
, gather
, iova
, pgsize
, 1, data
->pgd
);
644 static phys_addr_t
arm_v7s_iova_to_phys(struct io_pgtable_ops
*ops
,
647 struct arm_v7s_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
648 arm_v7s_iopte
*ptep
= data
->pgd
, pte
;
653 ptep
+= ARM_V7S_LVL_IDX(iova
, ++lvl
, &data
->iop
.cfg
);
654 pte
= READ_ONCE(*ptep
);
655 ptep
= iopte_deref(pte
, lvl
, data
);
656 } while (ARM_V7S_PTE_IS_TABLE(pte
, lvl
));
658 if (!ARM_V7S_PTE_IS_VALID(pte
))
661 mask
= ARM_V7S_LVL_MASK(lvl
);
662 if (arm_v7s_pte_is_cont(pte
, lvl
))
663 mask
*= ARM_V7S_CONT_PAGES
;
664 return iopte_to_paddr(pte
, lvl
, &data
->iop
.cfg
) | (iova
& ~mask
);
667 static struct io_pgtable
*arm_v7s_alloc_pgtable(struct io_pgtable_cfg
*cfg
,
670 struct arm_v7s_io_pgtable
*data
;
671 slab_flags_t slab_flag
;
674 if (cfg
->ias
> (arm_v7s_is_mtk_enabled(cfg
) ? 34 : ARM_V7S_ADDR_BITS
))
677 if (cfg
->oas
> (arm_v7s_is_mtk_enabled(cfg
) ? 35 : ARM_V7S_ADDR_BITS
))
680 if (cfg
->quirks
& ~(IO_PGTABLE_QUIRK_ARM_NS
|
681 IO_PGTABLE_QUIRK_NO_PERMS
|
682 IO_PGTABLE_QUIRK_ARM_MTK_EXT
|
683 IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT
))
686 /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
687 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_EXT
&&
688 !(cfg
->quirks
& IO_PGTABLE_QUIRK_NO_PERMS
))
691 if ((cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT
) &&
692 !arm_v7s_is_mtk_enabled(cfg
))
695 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
700 * ARM_MTK_TTBR_EXT extend the translation table base support larger
703 slab_flag
= cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT
?
704 0 : ARM_V7S_TABLE_SLAB_FLAGS
;
706 data
->l2_tables
= kmem_cache_create("io-pgtable_armv7s_l2",
707 ARM_V7S_TABLE_SIZE(2, cfg
),
708 ARM_V7S_TABLE_SIZE(2, cfg
),
710 if (!data
->l2_tables
)
713 data
->iop
.ops
= (struct io_pgtable_ops
) {
714 .map_pages
= arm_v7s_map_pages
,
715 .unmap_pages
= arm_v7s_unmap_pages
,
716 .iova_to_phys
= arm_v7s_iova_to_phys
,
719 /* We have to do this early for __arm_v7s_alloc_table to work... */
720 data
->iop
.cfg
= *cfg
;
723 * Unless the IOMMU driver indicates supersection support by
724 * having SZ_16M set in the initial bitmap, they won't be used.
726 cfg
->pgsize_bitmap
&= SZ_4K
| SZ_64K
| SZ_1M
| SZ_16M
;
728 /* TCR: T0SZ=0, EAE=0 (if applicable) */
729 cfg
->arm_v7s_cfg
.tcr
= 0;
732 * TEX remap: the indices used map to the closest equivalent types
733 * under the non-TEX-remap interpretation of those attribute bits,
734 * excepting various implementation-defined aspects of shareability.
736 cfg
->arm_v7s_cfg
.prrr
= ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE
) |
737 ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL
) |
738 ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL
) |
739 ARM_V7S_PRRR_DS0
| ARM_V7S_PRRR_DS1
|
740 ARM_V7S_PRRR_NS1
| ARM_V7S_PRRR_NOS(7);
741 cfg
->arm_v7s_cfg
.nmrr
= ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA
) |
742 ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA
);
744 /* Looking good; allocate a pgd */
745 data
->pgd
= __arm_v7s_alloc_table(1, GFP_KERNEL
, data
);
749 /* Ensure the empty pgd is visible before any actual TTBR write */
753 paddr
= virt_to_phys(data
->pgd
);
754 if (arm_v7s_is_mtk_enabled(cfg
))
755 cfg
->arm_v7s_cfg
.ttbr
= paddr
| upper_32_bits(paddr
);
757 cfg
->arm_v7s_cfg
.ttbr
= paddr
| ARM_V7S_TTBR_S
|
758 (cfg
->coherent_walk
? (ARM_V7S_TTBR_NOS
|
759 ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA
) |
760 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA
)) :
761 (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_NC
) |
762 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_NC
)));
766 kmem_cache_destroy(data
->l2_tables
);
771 struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns
= {
772 .alloc
= arm_v7s_alloc_pgtable
,
773 .free
= arm_v7s_free_pgtable
,
776 #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
778 static struct io_pgtable_cfg
*cfg_cookie __initdata
;
780 static void __init
dummy_tlb_flush_all(void *cookie
)
782 WARN_ON(cookie
!= cfg_cookie
);
785 static void __init
dummy_tlb_flush(unsigned long iova
, size_t size
,
786 size_t granule
, void *cookie
)
788 WARN_ON(cookie
!= cfg_cookie
);
789 WARN_ON(!(size
& cfg_cookie
->pgsize_bitmap
));
792 static void __init
dummy_tlb_add_page(struct iommu_iotlb_gather
*gather
,
793 unsigned long iova
, size_t granule
,
796 dummy_tlb_flush(iova
, granule
, granule
, cookie
);
799 static const struct iommu_flush_ops dummy_tlb_ops __initconst
= {
800 .tlb_flush_all
= dummy_tlb_flush_all
,
801 .tlb_flush_walk
= dummy_tlb_flush
,
802 .tlb_add_page
= dummy_tlb_add_page
,
805 #define __FAIL(ops) ({ \
806 WARN(1, "selftest: test failed\n"); \
807 selftest_running = false; \
811 static int __init
arm_v7s_do_selftests(void)
813 struct io_pgtable_ops
*ops
;
814 struct io_pgtable_cfg cfg
= {
815 .tlb
= &dummy_tlb_ops
,
818 .coherent_walk
= true,
819 .quirks
= IO_PGTABLE_QUIRK_ARM_NS
,
820 .pgsize_bitmap
= SZ_4K
| SZ_64K
| SZ_1M
| SZ_16M
,
822 unsigned int iova
, size
;
826 selftest_running
= true;
830 ops
= alloc_io_pgtable_ops(ARM_V7S
, &cfg
, &cfg
);
832 pr_err("selftest: failed to allocate io pgtable ops\n");
837 * Initial sanity checks.
838 * Empty page tables shouldn't provide any translations.
840 if (ops
->iova_to_phys(ops
, 42))
843 if (ops
->iova_to_phys(ops
, SZ_1G
+ 42))
846 if (ops
->iova_to_phys(ops
, SZ_2G
+ 42))
850 * Distinct mappings of different granule sizes.
853 for_each_set_bit(i
, &cfg
.pgsize_bitmap
, BITS_PER_LONG
) {
855 if (ops
->map_pages(ops
, iova
, iova
, size
, 1,
856 IOMMU_READ
| IOMMU_WRITE
|
857 IOMMU_NOEXEC
| IOMMU_CACHE
,
858 GFP_KERNEL
, &mapped
))
861 /* Overlapping mappings */
862 if (!ops
->map_pages(ops
, iova
, iova
+ size
, size
, 1,
863 IOMMU_READ
| IOMMU_NOEXEC
, GFP_KERNEL
,
867 if (ops
->iova_to_phys(ops
, iova
+ 42) != (iova
+ 42))
875 for_each_set_bit(i
, &cfg
.pgsize_bitmap
, BITS_PER_LONG
) {
878 if (ops
->unmap_pages(ops
, iova
, size
, 1, NULL
) != size
)
881 if (ops
->iova_to_phys(ops
, iova
+ 42))
884 /* Remap full block */
885 if (ops
->map_pages(ops
, iova
, iova
, size
, 1, IOMMU_WRITE
,
886 GFP_KERNEL
, &mapped
))
889 if (ops
->iova_to_phys(ops
, iova
+ 42) != (iova
+ 42))
895 free_io_pgtable_ops(ops
);
897 selftest_running
= false;
899 pr_info("self test ok\n");
902 subsys_initcall(arm_v7s_do_selftests
);