1 // SPDX-License-Identifier: GPL-2.0-only
3 * CPU-agnostic ARM page table allocator.
5 * Copyright (C) 2014 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
10 #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
12 #include <linux/atomic.h>
13 #include <linux/bitops.h>
14 #include <linux/io-pgtable.h>
15 #include <linux/kernel.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/dma-mapping.h>
21 #include <asm/barrier.h>
23 #define ARM_LPAE_MAX_ADDR_BITS 52
24 #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
25 #define ARM_LPAE_MAX_LEVELS 4
27 /* Struct accessors */
28 #define io_pgtable_to_data(x) \
29 container_of((x), struct arm_lpae_io_pgtable, iop)
31 #define io_pgtable_ops_to_data(x) \
32 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
35 * Calculate the right shift amount to get to the portion describing level l
36 * in a virtual address mapped by the pagetable in d.
38 #define ARM_LPAE_LVL_SHIFT(l,d) \
39 (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
40 ilog2(sizeof(arm_lpae_iopte)))
42 #define ARM_LPAE_GRANULE(d) \
43 (sizeof(arm_lpae_iopte) << (d)->bits_per_level)
44 #define ARM_LPAE_PGD_SIZE(d) \
45 (sizeof(arm_lpae_iopte) << (d)->pgd_bits)
48 * Calculate the index at level l used to map virtual address a using the
51 #define ARM_LPAE_PGD_IDX(l,d) \
52 ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
54 #define ARM_LPAE_LVL_IDX(a,l,d) \
55 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
56 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
58 /* Calculate the block/page mapping size at level l for pagetable in d. */
59 #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
62 #define ARM_LPAE_PTE_TYPE_SHIFT 0
63 #define ARM_LPAE_PTE_TYPE_MASK 0x3
65 #define ARM_LPAE_PTE_TYPE_BLOCK 1
66 #define ARM_LPAE_PTE_TYPE_TABLE 3
67 #define ARM_LPAE_PTE_TYPE_PAGE 3
69 #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
71 #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
72 #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
73 #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
74 #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
75 #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
76 #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
77 #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
78 #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
80 #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
81 /* Ignore the contiguous bit for block splitting */
82 #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
83 #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
84 ARM_LPAE_PTE_ATTR_HI_MASK)
85 /* Software bit for solving coherency races */
86 #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
89 #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
90 #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
91 #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
92 #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
95 #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
96 #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
97 #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
98 #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
99 #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
100 #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
103 #define ARM_LPAE_TCR_TG0_4K 0
104 #define ARM_LPAE_TCR_TG0_64K 1
105 #define ARM_LPAE_TCR_TG0_16K 2
107 #define ARM_LPAE_TCR_TG1_16K 1
108 #define ARM_LPAE_TCR_TG1_4K 2
109 #define ARM_LPAE_TCR_TG1_64K 3
111 #define ARM_LPAE_TCR_SH_NS 0
112 #define ARM_LPAE_TCR_SH_OS 2
113 #define ARM_LPAE_TCR_SH_IS 3
115 #define ARM_LPAE_TCR_RGN_NC 0
116 #define ARM_LPAE_TCR_RGN_WBWA 1
117 #define ARM_LPAE_TCR_RGN_WT 2
118 #define ARM_LPAE_TCR_RGN_WB 3
120 #define ARM_LPAE_VTCR_SL0_MASK 0x3
122 #define ARM_LPAE_TCR_T0SZ_SHIFT 0
124 #define ARM_LPAE_VTCR_PS_SHIFT 16
125 #define ARM_LPAE_VTCR_PS_MASK 0x7
127 #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
128 #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
129 #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
130 #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
131 #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
132 #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
133 #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL
135 #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
136 #define ARM_LPAE_MAIR_ATTR_MASK 0xff
137 #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
138 #define ARM_LPAE_MAIR_ATTR_NC 0x44
139 #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
140 #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
141 #define ARM_LPAE_MAIR_ATTR_IDX_NC 0
142 #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
143 #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
144 #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
146 #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
147 #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
148 #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
150 #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
151 #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
153 /* IOPTE accessors */
154 #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
156 #define iopte_type(pte,l) \
157 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
159 #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
161 struct arm_lpae_io_pgtable
{
162 struct io_pgtable iop
;
171 typedef u64 arm_lpae_iopte
;
173 static inline bool iopte_leaf(arm_lpae_iopte pte
, int lvl
,
174 enum io_pgtable_fmt fmt
)
176 if (lvl
== (ARM_LPAE_MAX_LEVELS
- 1) && fmt
!= ARM_MALI_LPAE
)
177 return iopte_type(pte
, lvl
) == ARM_LPAE_PTE_TYPE_PAGE
;
179 return iopte_type(pte
, lvl
) == ARM_LPAE_PTE_TYPE_BLOCK
;
182 static arm_lpae_iopte
paddr_to_iopte(phys_addr_t paddr
,
183 struct arm_lpae_io_pgtable
*data
)
185 arm_lpae_iopte pte
= paddr
;
187 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
188 return (pte
| (pte
>> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK
;
191 static phys_addr_t
iopte_to_paddr(arm_lpae_iopte pte
,
192 struct arm_lpae_io_pgtable
*data
)
194 u64 paddr
= pte
& ARM_LPAE_PTE_ADDR_MASK
;
196 if (ARM_LPAE_GRANULE(data
) < SZ_64K
)
199 /* Rotate the packed high-order bits back to the top */
200 return (paddr
| (paddr
<< (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK
<< 4);
203 static bool selftest_running
= false;
205 static dma_addr_t
__arm_lpae_dma_addr(void *pages
)
207 return (dma_addr_t
)virt_to_phys(pages
);
210 static void *__arm_lpae_alloc_pages(size_t size
, gfp_t gfp
,
211 struct io_pgtable_cfg
*cfg
)
213 struct device
*dev
= cfg
->iommu_dev
;
214 int order
= get_order(size
);
219 VM_BUG_ON((gfp
& __GFP_HIGHMEM
));
220 p
= alloc_pages_node(dev
? dev_to_node(dev
) : NUMA_NO_NODE
,
221 gfp
| __GFP_ZERO
, order
);
225 pages
= page_address(p
);
226 if (!cfg
->coherent_walk
) {
227 dma
= dma_map_single(dev
, pages
, size
, DMA_TO_DEVICE
);
228 if (dma_mapping_error(dev
, dma
))
231 * We depend on the IOMMU being able to work with any physical
232 * address directly, so if the DMA layer suggests otherwise by
233 * translating or truncating them, that bodes very badly...
235 if (dma
!= virt_to_phys(pages
))
242 dev_err(dev
, "Cannot accommodate DMA translation for IOMMU page tables\n");
243 dma_unmap_single(dev
, dma
, size
, DMA_TO_DEVICE
);
245 __free_pages(p
, order
);
249 static void __arm_lpae_free_pages(void *pages
, size_t size
,
250 struct io_pgtable_cfg
*cfg
)
252 if (!cfg
->coherent_walk
)
253 dma_unmap_single(cfg
->iommu_dev
, __arm_lpae_dma_addr(pages
),
254 size
, DMA_TO_DEVICE
);
255 free_pages((unsigned long)pages
, get_order(size
));
258 static void __arm_lpae_sync_pte(arm_lpae_iopte
*ptep
,
259 struct io_pgtable_cfg
*cfg
)
261 dma_sync_single_for_device(cfg
->iommu_dev
, __arm_lpae_dma_addr(ptep
),
262 sizeof(*ptep
), DMA_TO_DEVICE
);
265 static void __arm_lpae_set_pte(arm_lpae_iopte
*ptep
, arm_lpae_iopte pte
,
266 struct io_pgtable_cfg
*cfg
)
270 if (!cfg
->coherent_walk
)
271 __arm_lpae_sync_pte(ptep
, cfg
);
274 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable
*data
,
275 struct iommu_iotlb_gather
*gather
,
276 unsigned long iova
, size_t size
, int lvl
,
277 arm_lpae_iopte
*ptep
);
279 static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable
*data
,
280 phys_addr_t paddr
, arm_lpae_iopte prot
,
281 int lvl
, arm_lpae_iopte
*ptep
)
283 arm_lpae_iopte pte
= prot
;
285 if (data
->iop
.fmt
!= ARM_MALI_LPAE
&& lvl
== ARM_LPAE_MAX_LEVELS
- 1)
286 pte
|= ARM_LPAE_PTE_TYPE_PAGE
;
288 pte
|= ARM_LPAE_PTE_TYPE_BLOCK
;
290 pte
|= paddr_to_iopte(paddr
, data
);
292 __arm_lpae_set_pte(ptep
, pte
, &data
->iop
.cfg
);
295 static int arm_lpae_init_pte(struct arm_lpae_io_pgtable
*data
,
296 unsigned long iova
, phys_addr_t paddr
,
297 arm_lpae_iopte prot
, int lvl
,
298 arm_lpae_iopte
*ptep
)
300 arm_lpae_iopte pte
= *ptep
;
302 if (iopte_leaf(pte
, lvl
, data
->iop
.fmt
)) {
303 /* We require an unmap first */
304 WARN_ON(!selftest_running
);
306 } else if (iopte_type(pte
, lvl
) == ARM_LPAE_PTE_TYPE_TABLE
) {
308 * We need to unmap and free the old table before
309 * overwriting it with a block entry.
311 arm_lpae_iopte
*tblp
;
312 size_t sz
= ARM_LPAE_BLOCK_SIZE(lvl
, data
);
314 tblp
= ptep
- ARM_LPAE_LVL_IDX(iova
, lvl
, data
);
315 if (__arm_lpae_unmap(data
, NULL
, iova
, sz
, lvl
, tblp
) != sz
) {
321 __arm_lpae_init_pte(data
, paddr
, prot
, lvl
, ptep
);
325 static arm_lpae_iopte
arm_lpae_install_table(arm_lpae_iopte
*table
,
326 arm_lpae_iopte
*ptep
,
328 struct io_pgtable_cfg
*cfg
)
330 arm_lpae_iopte old
, new;
332 new = __pa(table
) | ARM_LPAE_PTE_TYPE_TABLE
;
333 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_NS
)
334 new |= ARM_LPAE_PTE_NSTABLE
;
337 * Ensure the table itself is visible before its PTE can be.
338 * Whilst we could get away with cmpxchg64_release below, this
339 * doesn't have any ordering semantics when !CONFIG_SMP.
343 old
= cmpxchg64_relaxed(ptep
, curr
, new);
345 if (cfg
->coherent_walk
|| (old
& ARM_LPAE_PTE_SW_SYNC
))
348 /* Even if it's not ours, there's no point waiting; just kick it */
349 __arm_lpae_sync_pte(ptep
, cfg
);
351 WRITE_ONCE(*ptep
, new | ARM_LPAE_PTE_SW_SYNC
);
356 static int __arm_lpae_map(struct arm_lpae_io_pgtable
*data
, unsigned long iova
,
357 phys_addr_t paddr
, size_t size
, arm_lpae_iopte prot
,
358 int lvl
, arm_lpae_iopte
*ptep
)
360 arm_lpae_iopte
*cptep
, pte
;
361 size_t block_size
= ARM_LPAE_BLOCK_SIZE(lvl
, data
);
362 size_t tblsz
= ARM_LPAE_GRANULE(data
);
363 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
365 /* Find our entry at the current level */
366 ptep
+= ARM_LPAE_LVL_IDX(iova
, lvl
, data
);
368 /* If we can install a leaf entry at this level, then do so */
369 if (size
== block_size
)
370 return arm_lpae_init_pte(data
, iova
, paddr
, prot
, lvl
, ptep
);
372 /* We can't allocate tables at the final level */
373 if (WARN_ON(lvl
>= ARM_LPAE_MAX_LEVELS
- 1))
376 /* Grab a pointer to the next level */
377 pte
= READ_ONCE(*ptep
);
379 cptep
= __arm_lpae_alloc_pages(tblsz
, GFP_ATOMIC
, cfg
);
383 pte
= arm_lpae_install_table(cptep
, ptep
, 0, cfg
);
385 __arm_lpae_free_pages(cptep
, tblsz
, cfg
);
386 } else if (!cfg
->coherent_walk
&& !(pte
& ARM_LPAE_PTE_SW_SYNC
)) {
387 __arm_lpae_sync_pte(ptep
, cfg
);
390 if (pte
&& !iopte_leaf(pte
, lvl
, data
->iop
.fmt
)) {
391 cptep
= iopte_deref(pte
, data
);
393 /* We require an unmap first */
394 WARN_ON(!selftest_running
);
399 return __arm_lpae_map(data
, iova
, paddr
, size
, prot
, lvl
+ 1, cptep
);
402 static arm_lpae_iopte
arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable
*data
,
407 if (data
->iop
.fmt
== ARM_64_LPAE_S1
||
408 data
->iop
.fmt
== ARM_32_LPAE_S1
) {
409 pte
= ARM_LPAE_PTE_nG
;
410 if (!(prot
& IOMMU_WRITE
) && (prot
& IOMMU_READ
))
411 pte
|= ARM_LPAE_PTE_AP_RDONLY
;
412 if (!(prot
& IOMMU_PRIV
))
413 pte
|= ARM_LPAE_PTE_AP_UNPRIV
;
415 pte
= ARM_LPAE_PTE_HAP_FAULT
;
416 if (prot
& IOMMU_READ
)
417 pte
|= ARM_LPAE_PTE_HAP_READ
;
418 if (prot
& IOMMU_WRITE
)
419 pte
|= ARM_LPAE_PTE_HAP_WRITE
;
423 * Note that this logic is structured to accommodate Mali LPAE
424 * having stage-1-like attributes but stage-2-like permissions.
426 if (data
->iop
.fmt
== ARM_64_LPAE_S2
||
427 data
->iop
.fmt
== ARM_32_LPAE_S2
) {
428 if (prot
& IOMMU_MMIO
)
429 pte
|= ARM_LPAE_PTE_MEMATTR_DEV
;
430 else if (prot
& IOMMU_CACHE
)
431 pte
|= ARM_LPAE_PTE_MEMATTR_OIWB
;
433 pte
|= ARM_LPAE_PTE_MEMATTR_NC
;
435 if (prot
& IOMMU_MMIO
)
436 pte
|= (ARM_LPAE_MAIR_ATTR_IDX_DEV
437 << ARM_LPAE_PTE_ATTRINDX_SHIFT
);
438 else if (prot
& IOMMU_CACHE
)
439 pte
|= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
440 << ARM_LPAE_PTE_ATTRINDX_SHIFT
);
441 else if (prot
& IOMMU_SYS_CACHE_ONLY
)
442 pte
|= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE
443 << ARM_LPAE_PTE_ATTRINDX_SHIFT
);
446 if (prot
& IOMMU_CACHE
)
447 pte
|= ARM_LPAE_PTE_SH_IS
;
449 pte
|= ARM_LPAE_PTE_SH_OS
;
451 if (prot
& IOMMU_NOEXEC
)
452 pte
|= ARM_LPAE_PTE_XN
;
454 if (data
->iop
.cfg
.quirks
& IO_PGTABLE_QUIRK_ARM_NS
)
455 pte
|= ARM_LPAE_PTE_NS
;
457 if (data
->iop
.fmt
!= ARM_MALI_LPAE
)
458 pte
|= ARM_LPAE_PTE_AF
;
463 static int arm_lpae_map(struct io_pgtable_ops
*ops
, unsigned long iova
,
464 phys_addr_t paddr
, size_t size
, int iommu_prot
)
466 struct arm_lpae_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
467 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
468 arm_lpae_iopte
*ptep
= data
->pgd
;
469 int ret
, lvl
= data
->start_level
;
471 long iaext
= (s64
)iova
>> cfg
->ias
;
473 /* If no access, then nothing to do */
474 if (!(iommu_prot
& (IOMMU_READ
| IOMMU_WRITE
)))
477 if (WARN_ON(!size
|| (size
& cfg
->pgsize_bitmap
) != size
))
480 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_TTBR1
)
482 if (WARN_ON(iaext
|| paddr
>> cfg
->oas
))
485 prot
= arm_lpae_prot_to_pte(data
, iommu_prot
);
486 ret
= __arm_lpae_map(data
, iova
, paddr
, size
, prot
, lvl
, ptep
);
488 * Synchronise all PTE updates for the new mapping before there's
489 * a chance for anything to kick off a table walk for the new iova.
496 static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable
*data
, int lvl
,
497 arm_lpae_iopte
*ptep
)
499 arm_lpae_iopte
*start
, *end
;
500 unsigned long table_size
;
502 if (lvl
== data
->start_level
)
503 table_size
= ARM_LPAE_PGD_SIZE(data
);
505 table_size
= ARM_LPAE_GRANULE(data
);
509 /* Only leaf entries at the last level */
510 if (lvl
== ARM_LPAE_MAX_LEVELS
- 1)
513 end
= (void *)ptep
+ table_size
;
515 while (ptep
!= end
) {
516 arm_lpae_iopte pte
= *ptep
++;
518 if (!pte
|| iopte_leaf(pte
, lvl
, data
->iop
.fmt
))
521 __arm_lpae_free_pgtable(data
, lvl
+ 1, iopte_deref(pte
, data
));
524 __arm_lpae_free_pages(start
, table_size
, &data
->iop
.cfg
);
527 static void arm_lpae_free_pgtable(struct io_pgtable
*iop
)
529 struct arm_lpae_io_pgtable
*data
= io_pgtable_to_data(iop
);
531 __arm_lpae_free_pgtable(data
, data
->start_level
, data
->pgd
);
535 static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable
*data
,
536 struct iommu_iotlb_gather
*gather
,
537 unsigned long iova
, size_t size
,
538 arm_lpae_iopte blk_pte
, int lvl
,
539 arm_lpae_iopte
*ptep
)
541 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
542 arm_lpae_iopte pte
, *tablep
;
543 phys_addr_t blk_paddr
;
544 size_t tablesz
= ARM_LPAE_GRANULE(data
);
545 size_t split_sz
= ARM_LPAE_BLOCK_SIZE(lvl
, data
);
546 int i
, unmap_idx
= -1;
548 if (WARN_ON(lvl
== ARM_LPAE_MAX_LEVELS
))
551 tablep
= __arm_lpae_alloc_pages(tablesz
, GFP_ATOMIC
, cfg
);
553 return 0; /* Bytes unmapped */
555 if (size
== split_sz
)
556 unmap_idx
= ARM_LPAE_LVL_IDX(iova
, lvl
, data
);
558 blk_paddr
= iopte_to_paddr(blk_pte
, data
);
559 pte
= iopte_prot(blk_pte
);
561 for (i
= 0; i
< tablesz
/ sizeof(pte
); i
++, blk_paddr
+= split_sz
) {
566 __arm_lpae_init_pte(data
, blk_paddr
, pte
, lvl
, &tablep
[i
]);
569 pte
= arm_lpae_install_table(tablep
, ptep
, blk_pte
, cfg
);
570 if (pte
!= blk_pte
) {
571 __arm_lpae_free_pages(tablep
, tablesz
, cfg
);
573 * We may race against someone unmapping another part of this
574 * block, but anything else is invalid. We can't misinterpret
575 * a page entry here since we're never at the last level.
577 if (iopte_type(pte
, lvl
- 1) != ARM_LPAE_PTE_TYPE_TABLE
)
580 tablep
= iopte_deref(pte
, data
);
581 } else if (unmap_idx
>= 0) {
582 io_pgtable_tlb_add_page(&data
->iop
, gather
, iova
, size
);
586 return __arm_lpae_unmap(data
, gather
, iova
, size
, lvl
, tablep
);
589 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable
*data
,
590 struct iommu_iotlb_gather
*gather
,
591 unsigned long iova
, size_t size
, int lvl
,
592 arm_lpae_iopte
*ptep
)
595 struct io_pgtable
*iop
= &data
->iop
;
597 /* Something went horribly wrong and we ran out of page table */
598 if (WARN_ON(lvl
== ARM_LPAE_MAX_LEVELS
))
601 ptep
+= ARM_LPAE_LVL_IDX(iova
, lvl
, data
);
602 pte
= READ_ONCE(*ptep
);
606 /* If the size matches this level, we're in the right place */
607 if (size
== ARM_LPAE_BLOCK_SIZE(lvl
, data
)) {
608 __arm_lpae_set_pte(ptep
, 0, &iop
->cfg
);
610 if (!iopte_leaf(pte
, lvl
, iop
->fmt
)) {
611 /* Also flush any partial walks */
612 io_pgtable_tlb_flush_walk(iop
, iova
, size
,
613 ARM_LPAE_GRANULE(data
));
614 ptep
= iopte_deref(pte
, data
);
615 __arm_lpae_free_pgtable(data
, lvl
+ 1, ptep
);
616 } else if (iop
->cfg
.quirks
& IO_PGTABLE_QUIRK_NON_STRICT
) {
618 * Order the PTE update against queueing the IOVA, to
619 * guarantee that a flush callback from a different CPU
620 * has observed it before the TLBIALL can be issued.
624 io_pgtable_tlb_add_page(iop
, gather
, iova
, size
);
628 } else if (iopte_leaf(pte
, lvl
, iop
->fmt
)) {
630 * Insert a table at the next level to map the old region,
631 * minus the part we want to unmap
633 return arm_lpae_split_blk_unmap(data
, gather
, iova
, size
, pte
,
637 /* Keep on walkin' */
638 ptep
= iopte_deref(pte
, data
);
639 return __arm_lpae_unmap(data
, gather
, iova
, size
, lvl
+ 1, ptep
);
642 static size_t arm_lpae_unmap(struct io_pgtable_ops
*ops
, unsigned long iova
,
643 size_t size
, struct iommu_iotlb_gather
*gather
)
645 struct arm_lpae_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
646 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
647 arm_lpae_iopte
*ptep
= data
->pgd
;
648 long iaext
= (s64
)iova
>> cfg
->ias
;
650 if (WARN_ON(!size
|| (size
& cfg
->pgsize_bitmap
) != size
))
653 if (cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_TTBR1
)
658 return __arm_lpae_unmap(data
, gather
, iova
, size
, data
->start_level
, ptep
);
661 static phys_addr_t
arm_lpae_iova_to_phys(struct io_pgtable_ops
*ops
,
664 struct arm_lpae_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
665 arm_lpae_iopte pte
, *ptep
= data
->pgd
;
666 int lvl
= data
->start_level
;
669 /* Valid IOPTE pointer? */
673 /* Grab the IOPTE we're interested in */
674 ptep
+= ARM_LPAE_LVL_IDX(iova
, lvl
, data
);
675 pte
= READ_ONCE(*ptep
);
682 if (iopte_leaf(pte
, lvl
, data
->iop
.fmt
))
683 goto found_translation
;
685 /* Take it to the next level */
686 ptep
= iopte_deref(pte
, data
);
687 } while (++lvl
< ARM_LPAE_MAX_LEVELS
);
689 /* Ran out of page tables to walk */
693 iova
&= (ARM_LPAE_BLOCK_SIZE(lvl
, data
) - 1);
694 return iopte_to_paddr(pte
, data
) | iova
;
697 static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg
*cfg
)
699 unsigned long granule
, page_sizes
;
700 unsigned int max_addr_bits
= 48;
703 * We need to restrict the supported page sizes to match the
704 * translation regime for a particular granule. Aim to match
705 * the CPU page size if possible, otherwise prefer smaller sizes.
706 * While we're at it, restrict the block sizes to match the
709 if (cfg
->pgsize_bitmap
& PAGE_SIZE
)
711 else if (cfg
->pgsize_bitmap
& ~PAGE_MASK
)
712 granule
= 1UL << __fls(cfg
->pgsize_bitmap
& ~PAGE_MASK
);
713 else if (cfg
->pgsize_bitmap
& PAGE_MASK
)
714 granule
= 1UL << __ffs(cfg
->pgsize_bitmap
& PAGE_MASK
);
720 page_sizes
= (SZ_4K
| SZ_2M
| SZ_1G
);
723 page_sizes
= (SZ_16K
| SZ_32M
);
727 page_sizes
= (SZ_64K
| SZ_512M
);
729 page_sizes
|= 1ULL << 42; /* 4TB */
735 cfg
->pgsize_bitmap
&= page_sizes
;
736 cfg
->ias
= min(cfg
->ias
, max_addr_bits
);
737 cfg
->oas
= min(cfg
->oas
, max_addr_bits
);
740 static struct arm_lpae_io_pgtable
*
741 arm_lpae_alloc_pgtable(struct io_pgtable_cfg
*cfg
)
743 struct arm_lpae_io_pgtable
*data
;
744 int levels
, va_bits
, pg_shift
;
746 arm_lpae_restrict_pgsizes(cfg
);
748 if (!(cfg
->pgsize_bitmap
& (SZ_4K
| SZ_16K
| SZ_64K
)))
751 if (cfg
->ias
> ARM_LPAE_MAX_ADDR_BITS
)
754 if (cfg
->oas
> ARM_LPAE_MAX_ADDR_BITS
)
757 if (!selftest_running
&& cfg
->iommu_dev
->dma_pfn_offset
) {
758 dev_err(cfg
->iommu_dev
, "Cannot accommodate DMA offset for IOMMU page tables\n");
762 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
766 pg_shift
= __ffs(cfg
->pgsize_bitmap
);
767 data
->bits_per_level
= pg_shift
- ilog2(sizeof(arm_lpae_iopte
));
769 va_bits
= cfg
->ias
- pg_shift
;
770 levels
= DIV_ROUND_UP(va_bits
, data
->bits_per_level
);
771 data
->start_level
= ARM_LPAE_MAX_LEVELS
- levels
;
773 /* Calculate the actual size of our pgd (without concatenation) */
774 data
->pgd_bits
= va_bits
- (data
->bits_per_level
* (levels
- 1));
776 data
->iop
.ops
= (struct io_pgtable_ops
) {
778 .unmap
= arm_lpae_unmap
,
779 .iova_to_phys
= arm_lpae_iova_to_phys
,
785 static struct io_pgtable
*
786 arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg
*cfg
, void *cookie
)
789 struct arm_lpae_io_pgtable
*data
;
790 typeof(&cfg
->arm_lpae_s1_cfg
.tcr
) tcr
= &cfg
->arm_lpae_s1_cfg
.tcr
;
793 if (cfg
->quirks
& ~(IO_PGTABLE_QUIRK_ARM_NS
|
794 IO_PGTABLE_QUIRK_NON_STRICT
|
795 IO_PGTABLE_QUIRK_ARM_TTBR1
))
798 data
= arm_lpae_alloc_pgtable(cfg
);
803 if (cfg
->coherent_walk
) {
804 tcr
->sh
= ARM_LPAE_TCR_SH_IS
;
805 tcr
->irgn
= ARM_LPAE_TCR_RGN_WBWA
;
806 tcr
->orgn
= ARM_LPAE_TCR_RGN_WBWA
;
808 tcr
->sh
= ARM_LPAE_TCR_SH_OS
;
809 tcr
->irgn
= ARM_LPAE_TCR_RGN_NC
;
810 tcr
->orgn
= ARM_LPAE_TCR_RGN_NC
;
813 tg1
= cfg
->quirks
& IO_PGTABLE_QUIRK_ARM_TTBR1
;
814 switch (ARM_LPAE_GRANULE(data
)) {
816 tcr
->tg
= tg1
? ARM_LPAE_TCR_TG1_4K
: ARM_LPAE_TCR_TG0_4K
;
819 tcr
->tg
= tg1
? ARM_LPAE_TCR_TG1_16K
: ARM_LPAE_TCR_TG0_16K
;
822 tcr
->tg
= tg1
? ARM_LPAE_TCR_TG1_64K
: ARM_LPAE_TCR_TG0_64K
;
828 tcr
->ips
= ARM_LPAE_TCR_PS_32_BIT
;
831 tcr
->ips
= ARM_LPAE_TCR_PS_36_BIT
;
834 tcr
->ips
= ARM_LPAE_TCR_PS_40_BIT
;
837 tcr
->ips
= ARM_LPAE_TCR_PS_42_BIT
;
840 tcr
->ips
= ARM_LPAE_TCR_PS_44_BIT
;
843 tcr
->ips
= ARM_LPAE_TCR_PS_48_BIT
;
846 tcr
->ips
= ARM_LPAE_TCR_PS_52_BIT
;
852 tcr
->tsz
= 64ULL - cfg
->ias
;
855 reg
= (ARM_LPAE_MAIR_ATTR_NC
856 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC
)) |
857 (ARM_LPAE_MAIR_ATTR_WBRWA
858 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE
)) |
859 (ARM_LPAE_MAIR_ATTR_DEVICE
860 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV
)) |
861 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
862 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE
));
864 cfg
->arm_lpae_s1_cfg
.mair
= reg
;
866 /* Looking good; allocate a pgd */
867 data
->pgd
= __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data
),
872 /* Ensure the empty pgd is visible before any actual TTBR write */
876 cfg
->arm_lpae_s1_cfg
.ttbr
= virt_to_phys(data
->pgd
);
884 static struct io_pgtable
*
885 arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg
*cfg
, void *cookie
)
888 struct arm_lpae_io_pgtable
*data
;
889 typeof(&cfg
->arm_lpae_s2_cfg
.vtcr
) vtcr
= &cfg
->arm_lpae_s2_cfg
.vtcr
;
891 /* The NS quirk doesn't apply at stage 2 */
892 if (cfg
->quirks
& ~(IO_PGTABLE_QUIRK_NON_STRICT
))
895 data
= arm_lpae_alloc_pgtable(cfg
);
900 * Concatenate PGDs at level 1 if possible in order to reduce
901 * the depth of the stage-2 walk.
903 if (data
->start_level
== 0) {
904 unsigned long pgd_pages
;
906 pgd_pages
= ARM_LPAE_PGD_SIZE(data
) / sizeof(arm_lpae_iopte
);
907 if (pgd_pages
<= ARM_LPAE_S2_MAX_CONCAT_PAGES
) {
908 data
->pgd_bits
+= data
->bits_per_level
;
914 if (cfg
->coherent_walk
) {
915 vtcr
->sh
= ARM_LPAE_TCR_SH_IS
;
916 vtcr
->irgn
= ARM_LPAE_TCR_RGN_WBWA
;
917 vtcr
->orgn
= ARM_LPAE_TCR_RGN_WBWA
;
919 vtcr
->sh
= ARM_LPAE_TCR_SH_OS
;
920 vtcr
->irgn
= ARM_LPAE_TCR_RGN_NC
;
921 vtcr
->orgn
= ARM_LPAE_TCR_RGN_NC
;
924 sl
= data
->start_level
;
926 switch (ARM_LPAE_GRANULE(data
)) {
928 vtcr
->tg
= ARM_LPAE_TCR_TG0_4K
;
929 sl
++; /* SL0 format is different for 4K granule size */
932 vtcr
->tg
= ARM_LPAE_TCR_TG0_16K
;
935 vtcr
->tg
= ARM_LPAE_TCR_TG0_64K
;
941 vtcr
->ps
= ARM_LPAE_TCR_PS_32_BIT
;
944 vtcr
->ps
= ARM_LPAE_TCR_PS_36_BIT
;
947 vtcr
->ps
= ARM_LPAE_TCR_PS_40_BIT
;
950 vtcr
->ps
= ARM_LPAE_TCR_PS_42_BIT
;
953 vtcr
->ps
= ARM_LPAE_TCR_PS_44_BIT
;
956 vtcr
->ps
= ARM_LPAE_TCR_PS_48_BIT
;
959 vtcr
->ps
= ARM_LPAE_TCR_PS_52_BIT
;
965 vtcr
->tsz
= 64ULL - cfg
->ias
;
966 vtcr
->sl
= ~sl
& ARM_LPAE_VTCR_SL0_MASK
;
968 /* Allocate pgd pages */
969 data
->pgd
= __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data
),
974 /* Ensure the empty pgd is visible before any actual TTBR write */
978 cfg
->arm_lpae_s2_cfg
.vttbr
= virt_to_phys(data
->pgd
);
986 static struct io_pgtable
*
987 arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg
*cfg
, void *cookie
)
989 if (cfg
->ias
> 32 || cfg
->oas
> 40)
992 cfg
->pgsize_bitmap
&= (SZ_4K
| SZ_2M
| SZ_1G
);
993 return arm_64_lpae_alloc_pgtable_s1(cfg
, cookie
);
996 static struct io_pgtable
*
997 arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg
*cfg
, void *cookie
)
999 if (cfg
->ias
> 40 || cfg
->oas
> 40)
1002 cfg
->pgsize_bitmap
&= (SZ_4K
| SZ_2M
| SZ_1G
);
1003 return arm_64_lpae_alloc_pgtable_s2(cfg
, cookie
);
1006 static struct io_pgtable
*
1007 arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg
*cfg
, void *cookie
)
1009 struct arm_lpae_io_pgtable
*data
;
1011 /* No quirks for Mali (hopefully) */
1015 if (cfg
->ias
> 48 || cfg
->oas
> 40)
1018 cfg
->pgsize_bitmap
&= (SZ_4K
| SZ_2M
| SZ_1G
);
1020 data
= arm_lpae_alloc_pgtable(cfg
);
1024 /* Mali seems to need a full 4-level table regardless of IAS */
1025 if (data
->start_level
> 0) {
1026 data
->start_level
= 0;
1030 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
1031 * best we can do is mimic the out-of-tree driver and hope that the
1032 * "implementation-defined caching policy" is good enough. Similarly,
1033 * we'll use it for the sake of a valid attribute for our 'device'
1034 * index, although callers should never request that in practice.
1036 cfg
->arm_mali_lpae_cfg
.memattr
=
1037 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1038 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC
)) |
1039 (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
1040 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE
)) |
1041 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1042 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV
));
1044 data
->pgd
= __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data
), GFP_KERNEL
,
1049 /* Ensure the empty pgd is visible before TRANSTAB can be written */
1052 cfg
->arm_mali_lpae_cfg
.transtab
= virt_to_phys(data
->pgd
) |
1053 ARM_MALI_LPAE_TTBR_READ_INNER
|
1054 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE
;
1062 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns
= {
1063 .alloc
= arm_64_lpae_alloc_pgtable_s1
,
1064 .free
= arm_lpae_free_pgtable
,
1067 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns
= {
1068 .alloc
= arm_64_lpae_alloc_pgtable_s2
,
1069 .free
= arm_lpae_free_pgtable
,
1072 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns
= {
1073 .alloc
= arm_32_lpae_alloc_pgtable_s1
,
1074 .free
= arm_lpae_free_pgtable
,
1077 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns
= {
1078 .alloc
= arm_32_lpae_alloc_pgtable_s2
,
1079 .free
= arm_lpae_free_pgtable
,
1082 struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns
= {
1083 .alloc
= arm_mali_lpae_alloc_pgtable
,
1084 .free
= arm_lpae_free_pgtable
,
1087 #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1089 static struct io_pgtable_cfg
*cfg_cookie __initdata
;
1091 static void __init
dummy_tlb_flush_all(void *cookie
)
1093 WARN_ON(cookie
!= cfg_cookie
);
1096 static void __init
dummy_tlb_flush(unsigned long iova
, size_t size
,
1097 size_t granule
, void *cookie
)
1099 WARN_ON(cookie
!= cfg_cookie
);
1100 WARN_ON(!(size
& cfg_cookie
->pgsize_bitmap
));
1103 static void __init
dummy_tlb_add_page(struct iommu_iotlb_gather
*gather
,
1104 unsigned long iova
, size_t granule
,
1107 dummy_tlb_flush(iova
, granule
, granule
, cookie
);
1110 static const struct iommu_flush_ops dummy_tlb_ops __initconst
= {
1111 .tlb_flush_all
= dummy_tlb_flush_all
,
1112 .tlb_flush_walk
= dummy_tlb_flush
,
1113 .tlb_flush_leaf
= dummy_tlb_flush
,
1114 .tlb_add_page
= dummy_tlb_add_page
,
1117 static void __init
arm_lpae_dump_ops(struct io_pgtable_ops
*ops
)
1119 struct arm_lpae_io_pgtable
*data
= io_pgtable_ops_to_data(ops
);
1120 struct io_pgtable_cfg
*cfg
= &data
->iop
.cfg
;
1122 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1123 cfg
->pgsize_bitmap
, cfg
->ias
);
1124 pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
1125 ARM_LPAE_MAX_LEVELS
- data
->start_level
, ARM_LPAE_PGD_SIZE(data
),
1126 ilog2(ARM_LPAE_GRANULE(data
)), data
->bits_per_level
, data
->pgd
);
1129 #define __FAIL(ops, i) ({ \
1130 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1131 arm_lpae_dump_ops(ops); \
1132 selftest_running = false; \
1136 static int __init
arm_lpae_run_tests(struct io_pgtable_cfg
*cfg
)
1138 static const enum io_pgtable_fmt fmts
[] __initconst
= {
1146 struct io_pgtable_ops
*ops
;
1148 selftest_running
= true;
1150 for (i
= 0; i
< ARRAY_SIZE(fmts
); ++i
) {
1152 ops
= alloc_io_pgtable_ops(fmts
[i
], cfg
, cfg
);
1154 pr_err("selftest: failed to allocate io pgtable ops\n");
1159 * Initial sanity checks.
1160 * Empty page tables shouldn't provide any translations.
1162 if (ops
->iova_to_phys(ops
, 42))
1163 return __FAIL(ops
, i
);
1165 if (ops
->iova_to_phys(ops
, SZ_1G
+ 42))
1166 return __FAIL(ops
, i
);
1168 if (ops
->iova_to_phys(ops
, SZ_2G
+ 42))
1169 return __FAIL(ops
, i
);
1172 * Distinct mappings of different granule sizes.
1175 for_each_set_bit(j
, &cfg
->pgsize_bitmap
, BITS_PER_LONG
) {
1178 if (ops
->map(ops
, iova
, iova
, size
, IOMMU_READ
|
1182 return __FAIL(ops
, i
);
1184 /* Overlapping mappings */
1185 if (!ops
->map(ops
, iova
, iova
+ size
, size
,
1186 IOMMU_READ
| IOMMU_NOEXEC
))
1187 return __FAIL(ops
, i
);
1189 if (ops
->iova_to_phys(ops
, iova
+ 42) != (iova
+ 42))
1190 return __FAIL(ops
, i
);
1196 size
= 1UL << __ffs(cfg
->pgsize_bitmap
);
1197 if (ops
->unmap(ops
, SZ_1G
+ size
, size
, NULL
) != size
)
1198 return __FAIL(ops
, i
);
1200 /* Remap of partial unmap */
1201 if (ops
->map(ops
, SZ_1G
+ size
, size
, size
, IOMMU_READ
))
1202 return __FAIL(ops
, i
);
1204 if (ops
->iova_to_phys(ops
, SZ_1G
+ size
+ 42) != (size
+ 42))
1205 return __FAIL(ops
, i
);
1209 for_each_set_bit(j
, &cfg
->pgsize_bitmap
, BITS_PER_LONG
) {
1212 if (ops
->unmap(ops
, iova
, size
, NULL
) != size
)
1213 return __FAIL(ops
, i
);
1215 if (ops
->iova_to_phys(ops
, iova
+ 42))
1216 return __FAIL(ops
, i
);
1218 /* Remap full block */
1219 if (ops
->map(ops
, iova
, iova
, size
, IOMMU_WRITE
))
1220 return __FAIL(ops
, i
);
1222 if (ops
->iova_to_phys(ops
, iova
+ 42) != (iova
+ 42))
1223 return __FAIL(ops
, i
);
1228 free_io_pgtable_ops(ops
);
1231 selftest_running
= false;
1235 static int __init
arm_lpae_do_selftests(void)
1237 static const unsigned long pgsize
[] __initconst
= {
1238 SZ_4K
| SZ_2M
| SZ_1G
,
1243 static const unsigned int ias
[] __initconst
= {
1244 32, 36, 40, 42, 44, 48,
1247 int i
, j
, pass
= 0, fail
= 0;
1248 struct io_pgtable_cfg cfg
= {
1249 .tlb
= &dummy_tlb_ops
,
1251 .coherent_walk
= true,
1254 for (i
= 0; i
< ARRAY_SIZE(pgsize
); ++i
) {
1255 for (j
= 0; j
< ARRAY_SIZE(ias
); ++j
) {
1256 cfg
.pgsize_bitmap
= pgsize
[i
];
1258 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1260 if (arm_lpae_run_tests(&cfg
))
1267 pr_info("selftest: completed with %d PASS %d FAIL\n", pass
, fail
);
1268 return fail
? -EFAULT
: 0;
1270 subsys_initcall(arm_lpae_do_selftests
);