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. With some carefully-chosen coefficients we can
48 * hide the ugly inconsistencies behind these macros and at least let the
49 * rest of the code pretend to be somewhat sane.
51 #define ARM_V7S_ADDR_BITS 32
52 #define _ARM_V7S_LVL_BITS(lvl) (16 - (lvl) * 4)
53 #define ARM_V7S_LVL_SHIFT(lvl) (ARM_V7S_ADDR_BITS - (4 + 8 * (lvl)))
54 #define ARM_V7S_TABLE_SHIFT 10
56 #define ARM_V7S_PTES_PER_LVL(lvl) (1 << _ARM_V7S_LVL_BITS(lvl))
57 #define ARM_V7S_TABLE_SIZE(lvl) \
58 (ARM_V7S_PTES_PER_LVL(lvl) * sizeof(arm_v7s_iopte))
60 #define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl))
61 #define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl)))
62 #define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT))
63 #define _ARM_V7S_IDX_MASK(lvl) (ARM_V7S_PTES_PER_LVL(lvl) - 1)
64 #define ARM_V7S_LVL_IDX(addr, lvl) ({ \
66 ((u32)(addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l); \
70 * Large page/supersection entries are effectively a block of 16 page/section
71 * entries, along the lines of the LPAE contiguous hint, but all with the
72 * same output address. For want of a better common name we'll call them
73 * "contiguous" versions of their respective page/section entries here, but
74 * noting the distinction (WRT to TLB maintenance) that they represent *one*
75 * entry repeated 16 times, not 16 separate entries (as in the LPAE case).
77 #define ARM_V7S_CONT_PAGES 16
79 /* PTE type bits: these are all mixed up with XN/PXN bits in most cases */
80 #define ARM_V7S_PTE_TYPE_TABLE 0x1
81 #define ARM_V7S_PTE_TYPE_PAGE 0x2
82 #define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1
84 #define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0)
85 #define ARM_V7S_PTE_IS_TABLE(pte, lvl) \
86 ((lvl) == 1 && (((pte) & 0x3) == ARM_V7S_PTE_TYPE_TABLE))
89 #define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl)))
90 #define ARM_V7S_ATTR_B BIT(2)
91 #define ARM_V7S_ATTR_C BIT(3)
92 #define ARM_V7S_ATTR_NS_TABLE BIT(3)
93 #define ARM_V7S_ATTR_NS_SECTION BIT(19)
95 #define ARM_V7S_CONT_SECTION BIT(18)
96 #define ARM_V7S_CONT_PAGE_XN_SHIFT 15
99 * The attribute bits are consistently ordered*, but occupy bits [17:10] of
100 * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual
101 * fields relative to that 8-bit block, plus a total shift relative to the PTE.
103 #define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6)
105 #define ARM_V7S_ATTR_MASK 0xff
106 #define ARM_V7S_ATTR_AP0 BIT(0)
107 #define ARM_V7S_ATTR_AP1 BIT(1)
108 #define ARM_V7S_ATTR_AP2 BIT(5)
109 #define ARM_V7S_ATTR_S BIT(6)
110 #define ARM_V7S_ATTR_NG BIT(7)
111 #define ARM_V7S_TEX_SHIFT 2
112 #define ARM_V7S_TEX_MASK 0x7
113 #define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT)
115 /* MediaTek extend the two bits for PA 32bit/33bit */
116 #define ARM_V7S_ATTR_MTK_PA_BIT32 BIT(9)
117 #define ARM_V7S_ATTR_MTK_PA_BIT33 BIT(4)
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 #define ARM_V7S_TCR_PD1 BIT(5)
154 #ifdef CONFIG_ZONE_DMA32
155 #define ARM_V7S_TABLE_GFP_DMA GFP_DMA32
156 #define ARM_V7S_TABLE_SLAB_FLAGS SLAB_CACHE_DMA32
158 #define ARM_V7S_TABLE_GFP_DMA GFP_DMA
159 #define ARM_V7S_TABLE_SLAB_FLAGS SLAB_CACHE_DMA
162 typedef u32 arm_v7s_iopte;
164 static bool selftest_running;
166 struct arm_v7s_io_pgtable {
167 struct io_pgtable iop;
170 struct kmem_cache *l2_tables;
171 spinlock_t split_lock;
174 static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl);
176 static dma_addr_t __arm_v7s_dma_addr(void *pages)
178 return (dma_addr_t)virt_to_phys(pages);
181 static bool arm_v7s_is_mtk_enabled(struct io_pgtable_cfg *cfg)
183 return IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
184 (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_EXT);
187 static arm_v7s_iopte paddr_to_iopte(phys_addr_t paddr, int lvl,
188 struct io_pgtable_cfg *cfg)
190 arm_v7s_iopte pte = paddr & ARM_V7S_LVL_MASK(lvl);
192 if (!arm_v7s_is_mtk_enabled(cfg))
195 if (paddr & BIT_ULL(32))
196 pte |= ARM_V7S_ATTR_MTK_PA_BIT32;
197 if (paddr & BIT_ULL(33))
198 pte |= ARM_V7S_ATTR_MTK_PA_BIT33;
202 static phys_addr_t iopte_to_paddr(arm_v7s_iopte pte, int lvl,
203 struct io_pgtable_cfg *cfg)
208 if (ARM_V7S_PTE_IS_TABLE(pte, lvl))
209 mask = ARM_V7S_TABLE_MASK;
210 else if (arm_v7s_pte_is_cont(pte, lvl))
211 mask = ARM_V7S_LVL_MASK(lvl) * ARM_V7S_CONT_PAGES;
213 mask = ARM_V7S_LVL_MASK(lvl);
216 if (!arm_v7s_is_mtk_enabled(cfg))
219 if (pte & ARM_V7S_ATTR_MTK_PA_BIT32)
220 paddr |= BIT_ULL(32);
221 if (pte & ARM_V7S_ATTR_MTK_PA_BIT33)
222 paddr |= BIT_ULL(33);
226 static arm_v7s_iopte *iopte_deref(arm_v7s_iopte pte, int lvl,
227 struct arm_v7s_io_pgtable *data)
229 return phys_to_virt(iopte_to_paddr(pte, lvl, &data->iop.cfg));
232 static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp,
233 struct arm_v7s_io_pgtable *data)
235 struct io_pgtable_cfg *cfg = &data->iop.cfg;
236 struct device *dev = cfg->iommu_dev;
239 size_t size = ARM_V7S_TABLE_SIZE(lvl);
243 table = (void *)__get_free_pages(
244 __GFP_ZERO | ARM_V7S_TABLE_GFP_DMA, get_order(size));
246 table = kmem_cache_zalloc(data->l2_tables, gfp);
251 phys = virt_to_phys(table);
252 if (phys != (arm_v7s_iopte)phys) {
253 /* Doesn't fit in PTE */
254 dev_err(dev, "Page table does not fit in PTE: %pa", &phys);
257 if (!cfg->coherent_walk) {
258 dma = dma_map_single(dev, table, size, DMA_TO_DEVICE);
259 if (dma_mapping_error(dev, dma))
262 * We depend on the IOMMU being able to work with any physical
263 * address directly, so if the DMA layer suggests otherwise by
264 * translating or truncating them, that bodes very badly...
270 kmemleak_ignore(table);
274 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
275 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
278 free_pages((unsigned long)table, get_order(size));
280 kmem_cache_free(data->l2_tables, table);
284 static void __arm_v7s_free_table(void *table, int lvl,
285 struct arm_v7s_io_pgtable *data)
287 struct io_pgtable_cfg *cfg = &data->iop.cfg;
288 struct device *dev = cfg->iommu_dev;
289 size_t size = ARM_V7S_TABLE_SIZE(lvl);
291 if (!cfg->coherent_walk)
292 dma_unmap_single(dev, __arm_v7s_dma_addr(table), size,
295 free_pages((unsigned long)table, get_order(size));
297 kmem_cache_free(data->l2_tables, table);
300 static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries,
301 struct io_pgtable_cfg *cfg)
303 if (cfg->coherent_walk)
306 dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep),
307 num_entries * sizeof(*ptep), DMA_TO_DEVICE);
309 static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte,
310 int num_entries, struct io_pgtable_cfg *cfg)
314 for (i = 0; i < num_entries; i++)
317 __arm_v7s_pte_sync(ptep, num_entries, cfg);
320 static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
321 struct io_pgtable_cfg *cfg)
323 bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS);
324 arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S;
326 if (!(prot & IOMMU_MMIO))
327 pte |= ARM_V7S_ATTR_TEX(1);
329 pte |= ARM_V7S_PTE_AF;
330 if (!(prot & IOMMU_PRIV))
331 pte |= ARM_V7S_PTE_AP_UNPRIV;
332 if (!(prot & IOMMU_WRITE))
333 pte |= ARM_V7S_PTE_AP_RDONLY;
335 pte <<= ARM_V7S_ATTR_SHIFT(lvl);
337 if ((prot & IOMMU_NOEXEC) && ap)
338 pte |= ARM_V7S_ATTR_XN(lvl);
339 if (prot & IOMMU_MMIO)
340 pte |= ARM_V7S_ATTR_B;
341 else if (prot & IOMMU_CACHE)
342 pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C;
344 pte |= ARM_V7S_PTE_TYPE_PAGE;
345 if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS))
346 pte |= ARM_V7S_ATTR_NS_SECTION;
351 static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
353 int prot = IOMMU_READ;
354 arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl);
356 if (!(attr & ARM_V7S_PTE_AP_RDONLY))
358 if (!(attr & ARM_V7S_PTE_AP_UNPRIV))
360 if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
362 else if (pte & ARM_V7S_ATTR_C)
364 if (pte & ARM_V7S_ATTR_XN(lvl))
365 prot |= IOMMU_NOEXEC;
370 static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl)
373 pte |= ARM_V7S_CONT_SECTION;
374 } else if (lvl == 2) {
375 arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl);
376 arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK;
378 pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE;
379 pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) |
380 (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
381 ARM_V7S_PTE_TYPE_CONT_PAGE;
386 static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl)
389 pte &= ~ARM_V7S_CONT_SECTION;
390 } else if (lvl == 2) {
391 arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT);
392 arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK <<
393 ARM_V7S_CONT_PAGE_TEX_SHIFT);
395 pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE;
396 pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) |
397 (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) |
398 ARM_V7S_PTE_TYPE_PAGE;
403 static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl)
405 if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl))
406 return pte & ARM_V7S_CONT_SECTION;
408 return !(pte & ARM_V7S_PTE_TYPE_PAGE);
412 static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable *,
413 struct iommu_iotlb_gather *, unsigned long,
414 size_t, int, arm_v7s_iopte *);
416 static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data,
417 unsigned long iova, phys_addr_t paddr, int prot,
418 int lvl, int num_entries, arm_v7s_iopte *ptep)
420 struct io_pgtable_cfg *cfg = &data->iop.cfg;
424 for (i = 0; i < num_entries; i++)
425 if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) {
427 * We need to unmap and free the old table before
428 * overwriting it with a block entry.
431 size_t sz = ARM_V7S_BLOCK_SIZE(lvl);
433 tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl);
434 if (WARN_ON(__arm_v7s_unmap(data, NULL, iova + i * sz,
435 sz, lvl, tblp) != sz))
437 } else if (ptep[i]) {
438 /* We require an unmap first */
439 WARN_ON(!selftest_running);
443 pte = arm_v7s_prot_to_pte(prot, lvl, cfg);
445 pte = arm_v7s_pte_to_cont(pte, lvl);
447 pte |= paddr_to_iopte(paddr, lvl, cfg);
449 __arm_v7s_set_pte(ptep, pte, num_entries, cfg);
453 static arm_v7s_iopte arm_v7s_install_table(arm_v7s_iopte *table,
456 struct io_pgtable_cfg *cfg)
458 arm_v7s_iopte old, new;
460 new = virt_to_phys(table) | ARM_V7S_PTE_TYPE_TABLE;
461 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
462 new |= ARM_V7S_ATTR_NS_TABLE;
465 * Ensure the table itself is visible before its PTE can be.
466 * Whilst we could get away with cmpxchg64_release below, this
467 * doesn't have any ordering semantics when !CONFIG_SMP.
471 old = cmpxchg_relaxed(ptep, curr, new);
472 __arm_v7s_pte_sync(ptep, 1, cfg);
477 static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova,
478 phys_addr_t paddr, size_t size, int prot,
479 int lvl, arm_v7s_iopte *ptep)
481 struct io_pgtable_cfg *cfg = &data->iop.cfg;
482 arm_v7s_iopte pte, *cptep;
483 int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
485 /* Find our entry at the current level */
486 ptep += ARM_V7S_LVL_IDX(iova, lvl);
488 /* If we can install a leaf entry at this level, then do so */
490 return arm_v7s_init_pte(data, iova, paddr, prot,
491 lvl, num_entries, ptep);
493 /* We can't allocate tables at the final level */
494 if (WARN_ON(lvl == 2))
497 /* Grab a pointer to the next level */
498 pte = READ_ONCE(*ptep);
500 cptep = __arm_v7s_alloc_table(lvl + 1, GFP_ATOMIC, data);
504 pte = arm_v7s_install_table(cptep, ptep, 0, cfg);
506 __arm_v7s_free_table(cptep, lvl + 1, data);
508 /* We've no easy way of knowing if it's synced yet, so... */
509 __arm_v7s_pte_sync(ptep, 1, cfg);
512 if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) {
513 cptep = iopte_deref(pte, lvl, data);
515 /* We require an unmap first */
516 WARN_ON(!selftest_running);
521 return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep);
524 static int arm_v7s_map(struct io_pgtable_ops *ops, unsigned long iova,
525 phys_addr_t paddr, size_t size, int prot)
527 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
528 struct io_pgtable *iop = &data->iop;
531 /* If no access, then nothing to do */
532 if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
535 if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) ||
536 paddr >= (1ULL << data->iop.cfg.oas)))
539 ret = __arm_v7s_map(data, iova, paddr, size, prot, 1, data->pgd);
541 * Synchronise all PTE updates for the new mapping before there's
542 * a chance for anything to kick off a table walk for the new iova.
544 if (iop->cfg.quirks & IO_PGTABLE_QUIRK_TLBI_ON_MAP) {
545 io_pgtable_tlb_flush_walk(iop, iova, size,
546 ARM_V7S_BLOCK_SIZE(2));
554 static void arm_v7s_free_pgtable(struct io_pgtable *iop)
556 struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop);
559 for (i = 0; i < ARM_V7S_PTES_PER_LVL(1); i++) {
560 arm_v7s_iopte pte = data->pgd[i];
562 if (ARM_V7S_PTE_IS_TABLE(pte, 1))
563 __arm_v7s_free_table(iopte_deref(pte, 1, data),
566 __arm_v7s_free_table(data->pgd, 1, data);
567 kmem_cache_destroy(data->l2_tables);
571 static arm_v7s_iopte arm_v7s_split_cont(struct arm_v7s_io_pgtable *data,
572 unsigned long iova, int idx, int lvl,
575 struct io_pgtable *iop = &data->iop;
577 size_t size = ARM_V7S_BLOCK_SIZE(lvl);
580 /* Check that we didn't lose a race to get the lock */
582 if (!arm_v7s_pte_is_cont(pte, lvl))
585 ptep -= idx & (ARM_V7S_CONT_PAGES - 1);
586 pte = arm_v7s_cont_to_pte(pte, lvl);
587 for (i = 0; i < ARM_V7S_CONT_PAGES; i++)
588 ptep[i] = pte + i * size;
590 __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg);
592 size *= ARM_V7S_CONT_PAGES;
593 io_pgtable_tlb_flush_leaf(iop, iova, size, size);
597 static size_t arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data,
598 struct iommu_iotlb_gather *gather,
599 unsigned long iova, size_t size,
600 arm_v7s_iopte blk_pte,
603 struct io_pgtable_cfg *cfg = &data->iop.cfg;
604 arm_v7s_iopte pte, *tablep;
605 int i, unmap_idx, num_entries, num_ptes;
607 tablep = __arm_v7s_alloc_table(2, GFP_ATOMIC, data);
609 return 0; /* Bytes unmapped */
611 num_ptes = ARM_V7S_PTES_PER_LVL(2);
612 num_entries = size >> ARM_V7S_LVL_SHIFT(2);
613 unmap_idx = ARM_V7S_LVL_IDX(iova, 2);
615 pte = arm_v7s_prot_to_pte(arm_v7s_pte_to_prot(blk_pte, 1), 2, cfg);
617 pte = arm_v7s_pte_to_cont(pte, 2);
619 for (i = 0; i < num_ptes; i += num_entries, pte += size) {
624 __arm_v7s_set_pte(&tablep[i], pte, num_entries, cfg);
627 pte = arm_v7s_install_table(tablep, ptep, blk_pte, cfg);
628 if (pte != blk_pte) {
629 __arm_v7s_free_table(tablep, 2, data);
631 if (!ARM_V7S_PTE_IS_TABLE(pte, 1))
634 tablep = iopte_deref(pte, 1, data);
635 return __arm_v7s_unmap(data, gather, iova, size, 2, tablep);
638 io_pgtable_tlb_add_page(&data->iop, gather, iova, size);
642 static size_t __arm_v7s_unmap(struct arm_v7s_io_pgtable *data,
643 struct iommu_iotlb_gather *gather,
644 unsigned long iova, size_t size, int lvl,
647 arm_v7s_iopte pte[ARM_V7S_CONT_PAGES];
648 struct io_pgtable *iop = &data->iop;
649 int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
651 /* Something went horribly wrong and we ran out of page table */
652 if (WARN_ON(lvl > 2))
655 idx = ARM_V7S_LVL_IDX(iova, lvl);
658 pte[i] = READ_ONCE(ptep[i]);
659 if (WARN_ON(!ARM_V7S_PTE_IS_VALID(pte[i])))
661 } while (++i < num_entries);
664 * If we've hit a contiguous 'large page' entry at this level, it
665 * needs splitting first, unless we're unmapping the whole lot.
667 * For splitting, we can't rewrite 16 PTEs atomically, and since we
668 * can't necessarily assume TEX remap we don't have a software bit to
669 * mark live entries being split. In practice (i.e. DMA API code), we
670 * will never be splitting large pages anyway, so just wrap this edge
671 * case in a lock for the sake of correctness and be done with it.
673 if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl)) {
676 spin_lock_irqsave(&data->split_lock, flags);
677 pte[0] = arm_v7s_split_cont(data, iova, idx, lvl, ptep);
678 spin_unlock_irqrestore(&data->split_lock, flags);
681 /* If the size matches this level, we're in the right place */
683 size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl);
685 __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg);
687 for (i = 0; i < num_entries; i++) {
688 if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) {
689 /* Also flush any partial walks */
690 io_pgtable_tlb_flush_walk(iop, iova, blk_size,
691 ARM_V7S_BLOCK_SIZE(lvl + 1));
692 ptep = iopte_deref(pte[i], lvl, data);
693 __arm_v7s_free_table(ptep, lvl + 1, data);
694 } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
696 * Order the PTE update against queueing the IOVA, to
697 * guarantee that a flush callback from a different CPU
698 * has observed it before the TLBIALL can be issued.
702 io_pgtable_tlb_add_page(iop, gather, iova, blk_size);
707 } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) {
709 * Insert a table at the next level to map the old region,
710 * minus the part we want to unmap
712 return arm_v7s_split_blk_unmap(data, gather, iova, size, pte[0],
716 /* Keep on walkin' */
717 ptep = iopte_deref(pte[0], lvl, data);
718 return __arm_v7s_unmap(data, gather, iova, size, lvl + 1, ptep);
721 static size_t arm_v7s_unmap(struct io_pgtable_ops *ops, unsigned long iova,
722 size_t size, struct iommu_iotlb_gather *gather)
724 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
726 if (WARN_ON(upper_32_bits(iova)))
729 return __arm_v7s_unmap(data, gather, iova, size, 1, data->pgd);
732 static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
735 struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
736 arm_v7s_iopte *ptep = data->pgd, pte;
741 ptep += ARM_V7S_LVL_IDX(iova, ++lvl);
742 pte = READ_ONCE(*ptep);
743 ptep = iopte_deref(pte, lvl, data);
744 } while (ARM_V7S_PTE_IS_TABLE(pte, lvl));
746 if (!ARM_V7S_PTE_IS_VALID(pte))
749 mask = ARM_V7S_LVL_MASK(lvl);
750 if (arm_v7s_pte_is_cont(pte, lvl))
751 mask *= ARM_V7S_CONT_PAGES;
752 return iopte_to_paddr(pte, lvl, &data->iop.cfg) | (iova & ~mask);
755 static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg,
758 struct arm_v7s_io_pgtable *data;
760 if (cfg->ias > ARM_V7S_ADDR_BITS)
763 if (cfg->oas > (arm_v7s_is_mtk_enabled(cfg) ? 34 : ARM_V7S_ADDR_BITS))
766 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
767 IO_PGTABLE_QUIRK_NO_PERMS |
768 IO_PGTABLE_QUIRK_TLBI_ON_MAP |
769 IO_PGTABLE_QUIRK_ARM_MTK_EXT |
770 IO_PGTABLE_QUIRK_NON_STRICT))
773 /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
774 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_EXT &&
775 !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))
778 data = kmalloc(sizeof(*data), GFP_KERNEL);
782 spin_lock_init(&data->split_lock);
783 data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",
784 ARM_V7S_TABLE_SIZE(2),
785 ARM_V7S_TABLE_SIZE(2),
786 ARM_V7S_TABLE_SLAB_FLAGS, NULL);
787 if (!data->l2_tables)
790 data->iop.ops = (struct io_pgtable_ops) {
792 .unmap = arm_v7s_unmap,
793 .iova_to_phys = arm_v7s_iova_to_phys,
796 /* We have to do this early for __arm_v7s_alloc_table to work... */
797 data->iop.cfg = *cfg;
800 * Unless the IOMMU driver indicates supersection support by
801 * having SZ_16M set in the initial bitmap, they won't be used.
803 cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
805 /* TCR: T0SZ=0, disable TTBR1 */
806 cfg->arm_v7s_cfg.tcr = ARM_V7S_TCR_PD1;
809 * TEX remap: the indices used map to the closest equivalent types
810 * under the non-TEX-remap interpretation of those attribute bits,
811 * excepting various implementation-defined aspects of shareability.
813 cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) |
814 ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) |
815 ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) |
816 ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 |
817 ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7);
818 cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) |
819 ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA);
821 /* Looking good; allocate a pgd */
822 data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data);
826 /* Ensure the empty pgd is visible before any actual TTBR write */
830 cfg->arm_v7s_cfg.ttbr[0] = virt_to_phys(data->pgd) |
831 ARM_V7S_TTBR_S | ARM_V7S_TTBR_NOS |
832 (cfg->coherent_walk ?
833 (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |
834 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA)) :
835 (ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_NC) |
836 ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_NC)));
837 cfg->arm_v7s_cfg.ttbr[1] = 0;
841 kmem_cache_destroy(data->l2_tables);
846 struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
847 .alloc = arm_v7s_alloc_pgtable,
848 .free = arm_v7s_free_pgtable,
851 #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
853 static struct io_pgtable_cfg *cfg_cookie;
855 static void dummy_tlb_flush_all(void *cookie)
857 WARN_ON(cookie != cfg_cookie);
860 static void dummy_tlb_flush(unsigned long iova, size_t size, size_t granule,
863 WARN_ON(cookie != cfg_cookie);
864 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
867 static void dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
868 unsigned long iova, size_t granule, void *cookie)
870 dummy_tlb_flush(iova, granule, granule, cookie);
873 static const struct iommu_flush_ops dummy_tlb_ops = {
874 .tlb_flush_all = dummy_tlb_flush_all,
875 .tlb_flush_walk = dummy_tlb_flush,
876 .tlb_flush_leaf = dummy_tlb_flush,
877 .tlb_add_page = dummy_tlb_add_page,
880 #define __FAIL(ops) ({ \
881 WARN(1, "selftest: test failed\n"); \
882 selftest_running = false; \
886 static int __init arm_v7s_do_selftests(void)
888 struct io_pgtable_ops *ops;
889 struct io_pgtable_cfg cfg = {
890 .tlb = &dummy_tlb_ops,
893 .coherent_walk = true,
894 .quirks = IO_PGTABLE_QUIRK_ARM_NS,
895 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
897 unsigned int iova, size, iova_start;
898 unsigned int i, loopnr = 0;
900 selftest_running = true;
904 ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg);
906 pr_err("selftest: failed to allocate io pgtable ops\n");
911 * Initial sanity checks.
912 * Empty page tables shouldn't provide any translations.
914 if (ops->iova_to_phys(ops, 42))
917 if (ops->iova_to_phys(ops, SZ_1G + 42))
920 if (ops->iova_to_phys(ops, SZ_2G + 42))
924 * Distinct mappings of different granule sizes.
927 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
929 if (ops->map(ops, iova, iova, size, IOMMU_READ |
935 /* Overlapping mappings */
936 if (!ops->map(ops, iova, iova + size, size,
937 IOMMU_READ | IOMMU_NOEXEC))
940 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
949 size = 1UL << __ffs(cfg.pgsize_bitmap);
951 iova_start = i * SZ_16M;
952 if (ops->unmap(ops, iova_start + size, size, NULL) != size)
955 /* Remap of partial unmap */
956 if (ops->map(ops, iova_start + size, size, size, IOMMU_READ))
959 if (ops->iova_to_phys(ops, iova_start + size + 42)
967 for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) {
970 if (ops->unmap(ops, iova, size, NULL) != size)
973 if (ops->iova_to_phys(ops, iova + 42))
976 /* Remap full block */
977 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
980 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
986 free_io_pgtable_ops(ops);
988 selftest_running = false;
990 pr_info("self test ok\n");
993 subsys_initcall(arm_v7s_do_selftests);