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 #include "io-pgtable-arm.h"
25 #define ARM_LPAE_MAX_ADDR_BITS 52
26 #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
27 #define ARM_LPAE_MAX_LEVELS 4
29 /* Struct accessors */
30 #define io_pgtable_to_data(x) \
31 container_of((x), struct arm_lpae_io_pgtable, iop)
33 #define io_pgtable_ops_to_data(x) \
34 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
37 * Calculate the right shift amount to get to the portion describing level l
38 * in a virtual address mapped by the pagetable in d.
40 #define ARM_LPAE_LVL_SHIFT(l,d) \
41 (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
42 ilog2(sizeof(arm_lpae_iopte)))
44 #define ARM_LPAE_GRANULE(d) \
45 (sizeof(arm_lpae_iopte) << (d)->bits_per_level)
46 #define ARM_LPAE_PGD_SIZE(d) \
47 (sizeof(arm_lpae_iopte) << (d)->pgd_bits)
49 #define ARM_LPAE_PTES_PER_TABLE(d) \
50 (ARM_LPAE_GRANULE(d) >> ilog2(sizeof(arm_lpae_iopte)))
53 * Calculate the index at level l used to map virtual address a using the
56 #define ARM_LPAE_PGD_IDX(l,d) \
57 ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
59 #define ARM_LPAE_LVL_IDX(a,l,d) \
60 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
61 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
63 /* Calculate the block/page mapping size at level l for pagetable in d. */
64 #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
67 #define ARM_LPAE_PTE_TYPE_SHIFT 0
68 #define ARM_LPAE_PTE_TYPE_MASK 0x3
70 #define ARM_LPAE_PTE_TYPE_BLOCK 1
71 #define ARM_LPAE_PTE_TYPE_TABLE 3
72 #define ARM_LPAE_PTE_TYPE_PAGE 3
74 #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
76 #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
77 #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
78 #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
79 #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
80 #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
81 #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
82 #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
83 #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
85 #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
86 /* Ignore the contiguous bit for block splitting */
87 #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
88 #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
89 ARM_LPAE_PTE_ATTR_HI_MASK)
90 /* Software bit for solving coherency races */
91 #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
94 #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
95 #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
96 #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
97 #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
100 #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
101 #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
102 #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
103 #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
104 #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
105 #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
108 #define ARM_LPAE_VTCR_SL0_MASK 0x3
110 #define ARM_LPAE_TCR_T0SZ_SHIFT 0
112 #define ARM_LPAE_VTCR_PS_SHIFT 16
113 #define ARM_LPAE_VTCR_PS_MASK 0x7
115 #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
116 #define ARM_LPAE_MAIR_ATTR_MASK 0xff
117 #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
118 #define ARM_LPAE_MAIR_ATTR_NC 0x44
119 #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
120 #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
121 #define ARM_LPAE_MAIR_ATTR_IDX_NC 0
122 #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
123 #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
124 #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
126 #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
127 #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
128 #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
130 #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
131 #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
133 #define APPLE_DART_PTE_PROT_NO_WRITE (1<<7)
134 #define APPLE_DART_PTE_PROT_NO_READ (1<<8)
136 /* IOPTE accessors */
137 #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
139 #define iopte_type(pte) \
140 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
142 #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
144 struct arm_lpae_io_pgtable {
145 struct io_pgtable iop;
154 typedef u64 arm_lpae_iopte;
156 static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
157 enum io_pgtable_fmt fmt)
159 if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
160 return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE;
162 return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK;
165 static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
166 struct arm_lpae_io_pgtable *data)
168 arm_lpae_iopte pte = paddr;
170 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
171 return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
174 static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
175 struct arm_lpae_io_pgtable *data)
177 u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
179 if (ARM_LPAE_GRANULE(data) < SZ_64K)
182 /* Rotate the packed high-order bits back to the top */
183 return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
186 static bool selftest_running = false;
188 static dma_addr_t __arm_lpae_dma_addr(void *pages)
190 return (dma_addr_t)virt_to_phys(pages);
193 static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
194 struct io_pgtable_cfg *cfg)
196 struct device *dev = cfg->iommu_dev;
197 int order = get_order(size);
202 VM_BUG_ON((gfp & __GFP_HIGHMEM));
203 p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
204 gfp | __GFP_ZERO, order);
208 pages = page_address(p);
209 if (!cfg->coherent_walk) {
210 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
211 if (dma_mapping_error(dev, dma))
214 * We depend on the IOMMU being able to work with any physical
215 * address directly, so if the DMA layer suggests otherwise by
216 * translating or truncating them, that bodes very badly...
218 if (dma != virt_to_phys(pages))
225 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
226 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
228 __free_pages(p, order);
232 static void __arm_lpae_free_pages(void *pages, size_t size,
233 struct io_pgtable_cfg *cfg)
235 if (!cfg->coherent_walk)
236 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
237 size, DMA_TO_DEVICE);
238 free_pages((unsigned long)pages, get_order(size));
241 static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries,
242 struct io_pgtable_cfg *cfg)
244 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
245 sizeof(*ptep) * num_entries, DMA_TO_DEVICE);
248 static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg)
253 if (!cfg->coherent_walk)
254 __arm_lpae_sync_pte(ptep, 1, cfg);
257 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
258 struct iommu_iotlb_gather *gather,
259 unsigned long iova, size_t size, size_t pgcount,
260 int lvl, arm_lpae_iopte *ptep);
262 static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
263 phys_addr_t paddr, arm_lpae_iopte prot,
264 int lvl, int num_entries, arm_lpae_iopte *ptep)
266 arm_lpae_iopte pte = prot;
267 struct io_pgtable_cfg *cfg = &data->iop.cfg;
268 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
271 if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
272 pte |= ARM_LPAE_PTE_TYPE_PAGE;
274 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
276 for (i = 0; i < num_entries; i++)
277 ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data);
279 if (!cfg->coherent_walk)
280 __arm_lpae_sync_pte(ptep, num_entries, cfg);
283 static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
284 unsigned long iova, phys_addr_t paddr,
285 arm_lpae_iopte prot, int lvl, int num_entries,
286 arm_lpae_iopte *ptep)
290 for (i = 0; i < num_entries; i++)
291 if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
292 /* We require an unmap first */
293 WARN_ON(!selftest_running);
295 } else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) {
297 * We need to unmap and free the old table before
298 * overwriting it with a block entry.
300 arm_lpae_iopte *tblp;
301 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
303 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
304 if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1,
311 __arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep);
315 static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
316 arm_lpae_iopte *ptep,
318 struct arm_lpae_io_pgtable *data)
320 arm_lpae_iopte old, new;
321 struct io_pgtable_cfg *cfg = &data->iop.cfg;
323 new = paddr_to_iopte(__pa(table), data) | ARM_LPAE_PTE_TYPE_TABLE;
324 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
325 new |= ARM_LPAE_PTE_NSTABLE;
328 * Ensure the table itself is visible before its PTE can be.
329 * Whilst we could get away with cmpxchg64_release below, this
330 * doesn't have any ordering semantics when !CONFIG_SMP.
334 old = cmpxchg64_relaxed(ptep, curr, new);
336 if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
339 /* Even if it's not ours, there's no point waiting; just kick it */
340 __arm_lpae_sync_pte(ptep, 1, cfg);
342 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
347 static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
348 phys_addr_t paddr, size_t size, size_t pgcount,
349 arm_lpae_iopte prot, int lvl, arm_lpae_iopte *ptep,
350 gfp_t gfp, size_t *mapped)
352 arm_lpae_iopte *cptep, pte;
353 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
354 size_t tblsz = ARM_LPAE_GRANULE(data);
355 struct io_pgtable_cfg *cfg = &data->iop.cfg;
356 int ret = 0, num_entries, max_entries, map_idx_start;
358 /* Find our entry at the current level */
359 map_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
360 ptep += map_idx_start;
362 /* If we can install a leaf entry at this level, then do so */
363 if (size == block_size) {
364 max_entries = ARM_LPAE_PTES_PER_TABLE(data) - map_idx_start;
365 num_entries = min_t(int, pgcount, max_entries);
366 ret = arm_lpae_init_pte(data, iova, paddr, prot, lvl, num_entries, ptep);
368 *mapped += num_entries * size;
373 /* We can't allocate tables at the final level */
374 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
377 /* Grab a pointer to the next level */
378 pte = READ_ONCE(*ptep);
380 cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg);
384 pte = arm_lpae_install_table(cptep, ptep, 0, data);
386 __arm_lpae_free_pages(cptep, tblsz, cfg);
387 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
388 __arm_lpae_sync_pte(ptep, 1, cfg);
391 if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
392 cptep = iopte_deref(pte, data);
394 /* We require an unmap first */
395 WARN_ON(!selftest_running);
400 return __arm_lpae_map(data, iova, paddr, size, pgcount, prot, lvl + 1,
404 static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
409 if (data->iop.fmt == APPLE_DART) {
411 if (!(prot & IOMMU_WRITE))
412 pte |= APPLE_DART_PTE_PROT_NO_WRITE;
413 if (!(prot & IOMMU_READ))
414 pte |= APPLE_DART_PTE_PROT_NO_READ;
418 if (data->iop.fmt == ARM_64_LPAE_S1 ||
419 data->iop.fmt == ARM_32_LPAE_S1) {
420 pte = ARM_LPAE_PTE_nG;
421 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
422 pte |= ARM_LPAE_PTE_AP_RDONLY;
423 if (!(prot & IOMMU_PRIV))
424 pte |= ARM_LPAE_PTE_AP_UNPRIV;
426 pte = ARM_LPAE_PTE_HAP_FAULT;
427 if (prot & IOMMU_READ)
428 pte |= ARM_LPAE_PTE_HAP_READ;
429 if (prot & IOMMU_WRITE)
430 pte |= ARM_LPAE_PTE_HAP_WRITE;
434 * Note that this logic is structured to accommodate Mali LPAE
435 * having stage-1-like attributes but stage-2-like permissions.
437 if (data->iop.fmt == ARM_64_LPAE_S2 ||
438 data->iop.fmt == ARM_32_LPAE_S2) {
439 if (prot & IOMMU_MMIO)
440 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
441 else if (prot & IOMMU_CACHE)
442 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
444 pte |= ARM_LPAE_PTE_MEMATTR_NC;
446 if (prot & IOMMU_MMIO)
447 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
448 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
449 else if (prot & IOMMU_CACHE)
450 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
451 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
455 * Also Mali has its own notions of shareability wherein its Inner
456 * domain covers the cores within the GPU, and its Outer domain is
457 * "outside the GPU" (i.e. either the Inner or System domain in CPU
458 * terms, depending on coherency).
460 if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE)
461 pte |= ARM_LPAE_PTE_SH_IS;
463 pte |= ARM_LPAE_PTE_SH_OS;
465 if (prot & IOMMU_NOEXEC)
466 pte |= ARM_LPAE_PTE_XN;
468 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
469 pte |= ARM_LPAE_PTE_NS;
471 if (data->iop.fmt != ARM_MALI_LPAE)
472 pte |= ARM_LPAE_PTE_AF;
477 static int arm_lpae_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
478 phys_addr_t paddr, size_t pgsize, size_t pgcount,
479 int iommu_prot, gfp_t gfp, size_t *mapped)
481 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
482 struct io_pgtable_cfg *cfg = &data->iop.cfg;
483 arm_lpae_iopte *ptep = data->pgd;
484 int ret, lvl = data->start_level;
486 long iaext = (s64)iova >> cfg->ias;
488 if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize))
491 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
493 if (WARN_ON(iaext || paddr >> cfg->oas))
496 /* If no access, then nothing to do */
497 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
500 prot = arm_lpae_prot_to_pte(data, iommu_prot);
501 ret = __arm_lpae_map(data, iova, paddr, pgsize, pgcount, prot, lvl,
504 * Synchronise all PTE updates for the new mapping before there's
505 * a chance for anything to kick off a table walk for the new iova.
512 static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
513 phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp)
515 return arm_lpae_map_pages(ops, iova, paddr, size, 1, iommu_prot, gfp,
519 static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
520 arm_lpae_iopte *ptep)
522 arm_lpae_iopte *start, *end;
523 unsigned long table_size;
525 if (lvl == data->start_level)
526 table_size = ARM_LPAE_PGD_SIZE(data);
528 table_size = ARM_LPAE_GRANULE(data);
532 /* Only leaf entries at the last level */
533 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
536 end = (void *)ptep + table_size;
538 while (ptep != end) {
539 arm_lpae_iopte pte = *ptep++;
541 if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
544 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
547 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
550 static void arm_lpae_free_pgtable(struct io_pgtable *iop)
552 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
554 __arm_lpae_free_pgtable(data, data->start_level, data->pgd);
558 static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
559 struct iommu_iotlb_gather *gather,
560 unsigned long iova, size_t size,
561 arm_lpae_iopte blk_pte, int lvl,
562 arm_lpae_iopte *ptep, size_t pgcount)
564 struct io_pgtable_cfg *cfg = &data->iop.cfg;
565 arm_lpae_iopte pte, *tablep;
566 phys_addr_t blk_paddr;
567 size_t tablesz = ARM_LPAE_GRANULE(data);
568 size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
569 int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data);
570 int i, unmap_idx_start = -1, num_entries = 0, max_entries;
572 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
575 tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
577 return 0; /* Bytes unmapped */
579 if (size == split_sz) {
580 unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
581 max_entries = ptes_per_table - unmap_idx_start;
582 num_entries = min_t(int, pgcount, max_entries);
585 blk_paddr = iopte_to_paddr(blk_pte, data);
586 pte = iopte_prot(blk_pte);
588 for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) {
590 if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries))
593 __arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]);
596 pte = arm_lpae_install_table(tablep, ptep, blk_pte, data);
597 if (pte != blk_pte) {
598 __arm_lpae_free_pages(tablep, tablesz, cfg);
600 * We may race against someone unmapping another part of this
601 * block, but anything else is invalid. We can't misinterpret
602 * a page entry here since we're never at the last level.
604 if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE)
607 tablep = iopte_deref(pte, data);
608 } else if (unmap_idx_start >= 0) {
609 for (i = 0; i < num_entries; i++)
610 io_pgtable_tlb_add_page(&data->iop, gather, iova + i * size, size);
612 return num_entries * size;
615 return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep);
618 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
619 struct iommu_iotlb_gather *gather,
620 unsigned long iova, size_t size, size_t pgcount,
621 int lvl, arm_lpae_iopte *ptep)
624 struct io_pgtable *iop = &data->iop;
625 int i = 0, num_entries, max_entries, unmap_idx_start;
627 /* Something went horribly wrong and we ran out of page table */
628 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
631 unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
632 ptep += unmap_idx_start;
633 pte = READ_ONCE(*ptep);
637 /* If the size matches this level, we're in the right place */
638 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
639 max_entries = ARM_LPAE_PTES_PER_TABLE(data) - unmap_idx_start;
640 num_entries = min_t(int, pgcount, max_entries);
642 while (i < num_entries) {
643 pte = READ_ONCE(*ptep);
647 __arm_lpae_clear_pte(ptep, &iop->cfg);
649 if (!iopte_leaf(pte, lvl, iop->fmt)) {
650 /* Also flush any partial walks */
651 io_pgtable_tlb_flush_walk(iop, iova + i * size, size,
652 ARM_LPAE_GRANULE(data));
653 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
654 } else if (!iommu_iotlb_gather_queued(gather)) {
655 io_pgtable_tlb_add_page(iop, gather, iova + i * size, size);
663 } else if (iopte_leaf(pte, lvl, iop->fmt)) {
665 * Insert a table at the next level to map the old region,
666 * minus the part we want to unmap
668 return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
669 lvl + 1, ptep, pgcount);
672 /* Keep on walkin' */
673 ptep = iopte_deref(pte, data);
674 return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl + 1, ptep);
677 static size_t arm_lpae_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova,
678 size_t pgsize, size_t pgcount,
679 struct iommu_iotlb_gather *gather)
681 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
682 struct io_pgtable_cfg *cfg = &data->iop.cfg;
683 arm_lpae_iopte *ptep = data->pgd;
684 long iaext = (s64)iova >> cfg->ias;
686 if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount))
689 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
694 return __arm_lpae_unmap(data, gather, iova, pgsize, pgcount,
695 data->start_level, ptep);
698 static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
699 size_t size, struct iommu_iotlb_gather *gather)
701 return arm_lpae_unmap_pages(ops, iova, size, 1, gather);
704 static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
707 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
708 arm_lpae_iopte pte, *ptep = data->pgd;
709 int lvl = data->start_level;
712 /* Valid IOPTE pointer? */
716 /* Grab the IOPTE we're interested in */
717 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
718 pte = READ_ONCE(*ptep);
725 if (iopte_leaf(pte, lvl, data->iop.fmt))
726 goto found_translation;
728 /* Take it to the next level */
729 ptep = iopte_deref(pte, data);
730 } while (++lvl < ARM_LPAE_MAX_LEVELS);
732 /* Ran out of page tables to walk */
736 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
737 return iopte_to_paddr(pte, data) | iova;
740 static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
742 unsigned long granule, page_sizes;
743 unsigned int max_addr_bits = 48;
746 * We need to restrict the supported page sizes to match the
747 * translation regime for a particular granule. Aim to match
748 * the CPU page size if possible, otherwise prefer smaller sizes.
749 * While we're at it, restrict the block sizes to match the
752 if (cfg->pgsize_bitmap & PAGE_SIZE)
754 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
755 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
756 else if (cfg->pgsize_bitmap & PAGE_MASK)
757 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
763 page_sizes = (SZ_4K | SZ_2M | SZ_1G);
766 page_sizes = (SZ_16K | SZ_32M);
770 page_sizes = (SZ_64K | SZ_512M);
772 page_sizes |= 1ULL << 42; /* 4TB */
778 cfg->pgsize_bitmap &= page_sizes;
779 cfg->ias = min(cfg->ias, max_addr_bits);
780 cfg->oas = min(cfg->oas, max_addr_bits);
783 static struct arm_lpae_io_pgtable *
784 arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
786 struct arm_lpae_io_pgtable *data;
787 int levels, va_bits, pg_shift;
789 arm_lpae_restrict_pgsizes(cfg);
791 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
794 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
797 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
800 data = kmalloc(sizeof(*data), GFP_KERNEL);
804 pg_shift = __ffs(cfg->pgsize_bitmap);
805 data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
807 va_bits = cfg->ias - pg_shift;
808 levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
809 data->start_level = ARM_LPAE_MAX_LEVELS - levels;
811 /* Calculate the actual size of our pgd (without concatenation) */
812 data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
814 data->iop.ops = (struct io_pgtable_ops) {
816 .map_pages = arm_lpae_map_pages,
817 .unmap = arm_lpae_unmap,
818 .unmap_pages = arm_lpae_unmap_pages,
819 .iova_to_phys = arm_lpae_iova_to_phys,
825 static struct io_pgtable *
826 arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
829 struct arm_lpae_io_pgtable *data;
830 typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr;
833 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
834 IO_PGTABLE_QUIRK_ARM_TTBR1 |
835 IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
838 data = arm_lpae_alloc_pgtable(cfg);
843 if (cfg->coherent_walk) {
844 tcr->sh = ARM_LPAE_TCR_SH_IS;
845 tcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
846 tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
847 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)
850 tcr->sh = ARM_LPAE_TCR_SH_OS;
851 tcr->irgn = ARM_LPAE_TCR_RGN_NC;
852 if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
853 tcr->orgn = ARM_LPAE_TCR_RGN_NC;
855 tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
858 tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1;
859 switch (ARM_LPAE_GRANULE(data)) {
861 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K;
864 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K;
867 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K;
873 tcr->ips = ARM_LPAE_TCR_PS_32_BIT;
876 tcr->ips = ARM_LPAE_TCR_PS_36_BIT;
879 tcr->ips = ARM_LPAE_TCR_PS_40_BIT;
882 tcr->ips = ARM_LPAE_TCR_PS_42_BIT;
885 tcr->ips = ARM_LPAE_TCR_PS_44_BIT;
888 tcr->ips = ARM_LPAE_TCR_PS_48_BIT;
891 tcr->ips = ARM_LPAE_TCR_PS_52_BIT;
897 tcr->tsz = 64ULL - cfg->ias;
900 reg = (ARM_LPAE_MAIR_ATTR_NC
901 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
902 (ARM_LPAE_MAIR_ATTR_WBRWA
903 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
904 (ARM_LPAE_MAIR_ATTR_DEVICE
905 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
906 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
907 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
909 cfg->arm_lpae_s1_cfg.mair = reg;
911 /* Looking good; allocate a pgd */
912 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
917 /* Ensure the empty pgd is visible before any actual TTBR write */
921 cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
929 static struct io_pgtable *
930 arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
933 struct arm_lpae_io_pgtable *data;
934 typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;
936 /* The NS quirk doesn't apply at stage 2 */
940 data = arm_lpae_alloc_pgtable(cfg);
945 * Concatenate PGDs at level 1 if possible in order to reduce
946 * the depth of the stage-2 walk.
948 if (data->start_level == 0) {
949 unsigned long pgd_pages;
951 pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
952 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
953 data->pgd_bits += data->bits_per_level;
959 if (cfg->coherent_walk) {
960 vtcr->sh = ARM_LPAE_TCR_SH_IS;
961 vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
962 vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
964 vtcr->sh = ARM_LPAE_TCR_SH_OS;
965 vtcr->irgn = ARM_LPAE_TCR_RGN_NC;
966 vtcr->orgn = ARM_LPAE_TCR_RGN_NC;
969 sl = data->start_level;
971 switch (ARM_LPAE_GRANULE(data)) {
973 vtcr->tg = ARM_LPAE_TCR_TG0_4K;
974 sl++; /* SL0 format is different for 4K granule size */
977 vtcr->tg = ARM_LPAE_TCR_TG0_16K;
980 vtcr->tg = ARM_LPAE_TCR_TG0_64K;
986 vtcr->ps = ARM_LPAE_TCR_PS_32_BIT;
989 vtcr->ps = ARM_LPAE_TCR_PS_36_BIT;
992 vtcr->ps = ARM_LPAE_TCR_PS_40_BIT;
995 vtcr->ps = ARM_LPAE_TCR_PS_42_BIT;
998 vtcr->ps = ARM_LPAE_TCR_PS_44_BIT;
1001 vtcr->ps = ARM_LPAE_TCR_PS_48_BIT;
1004 vtcr->ps = ARM_LPAE_TCR_PS_52_BIT;
1010 vtcr->tsz = 64ULL - cfg->ias;
1011 vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK;
1013 /* Allocate pgd pages */
1014 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
1019 /* Ensure the empty pgd is visible before any actual TTBR write */
1023 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
1031 static struct io_pgtable *
1032 arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1034 if (cfg->ias > 32 || cfg->oas > 40)
1037 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1038 return arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1041 static struct io_pgtable *
1042 arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1044 if (cfg->ias > 40 || cfg->oas > 40)
1047 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1048 return arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1051 static struct io_pgtable *
1052 arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1054 struct arm_lpae_io_pgtable *data;
1056 /* No quirks for Mali (hopefully) */
1060 if (cfg->ias > 48 || cfg->oas > 40)
1063 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1065 data = arm_lpae_alloc_pgtable(cfg);
1069 /* Mali seems to need a full 4-level table regardless of IAS */
1070 if (data->start_level > 0) {
1071 data->start_level = 0;
1075 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
1076 * best we can do is mimic the out-of-tree driver and hope that the
1077 * "implementation-defined caching policy" is good enough. Similarly,
1078 * we'll use it for the sake of a valid attribute for our 'device'
1079 * index, although callers should never request that in practice.
1081 cfg->arm_mali_lpae_cfg.memattr =
1082 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1083 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
1084 (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
1085 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
1086 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1087 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
1089 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1094 /* Ensure the empty pgd is visible before TRANSTAB can be written */
1097 cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
1098 ARM_MALI_LPAE_TTBR_READ_INNER |
1099 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
1100 if (cfg->coherent_walk)
1101 cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER;
1110 static struct io_pgtable *
1111 apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1113 struct arm_lpae_io_pgtable *data;
1119 data = arm_lpae_alloc_pgtable(cfg);
1124 * The table format itself always uses two levels, but the total VA
1125 * space is mapped by four separate tables, making the MMIO registers
1126 * an effective "level 1". For simplicity, though, we treat this
1127 * equivalently to LPAE stage 2 concatenation at level 2, with the
1128 * additional TTBRs each just pointing at consecutive pages.
1130 if (data->start_level < 1)
1132 if (data->start_level == 1 && data->pgd_bits > 2)
1134 if (data->start_level > 1)
1136 data->start_level = 2;
1137 cfg->apple_dart_cfg.n_ttbrs = 1 << data->pgd_bits;
1138 data->pgd_bits += data->bits_per_level;
1140 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1145 for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i)
1146 cfg->apple_dart_cfg.ttbr[i] =
1147 virt_to_phys(data->pgd + i * ARM_LPAE_GRANULE(data));
1156 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1157 .alloc = arm_64_lpae_alloc_pgtable_s1,
1158 .free = arm_lpae_free_pgtable,
1161 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1162 .alloc = arm_64_lpae_alloc_pgtable_s2,
1163 .free = arm_lpae_free_pgtable,
1166 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1167 .alloc = arm_32_lpae_alloc_pgtable_s1,
1168 .free = arm_lpae_free_pgtable,
1171 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1172 .alloc = arm_32_lpae_alloc_pgtable_s2,
1173 .free = arm_lpae_free_pgtable,
1176 struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1177 .alloc = arm_mali_lpae_alloc_pgtable,
1178 .free = arm_lpae_free_pgtable,
1181 struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = {
1182 .alloc = apple_dart_alloc_pgtable,
1183 .free = arm_lpae_free_pgtable,
1186 #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1188 static struct io_pgtable_cfg *cfg_cookie __initdata;
1190 static void __init dummy_tlb_flush_all(void *cookie)
1192 WARN_ON(cookie != cfg_cookie);
1195 static void __init dummy_tlb_flush(unsigned long iova, size_t size,
1196 size_t granule, void *cookie)
1198 WARN_ON(cookie != cfg_cookie);
1199 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1202 static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
1203 unsigned long iova, size_t granule,
1206 dummy_tlb_flush(iova, granule, granule, cookie);
1209 static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
1210 .tlb_flush_all = dummy_tlb_flush_all,
1211 .tlb_flush_walk = dummy_tlb_flush,
1212 .tlb_add_page = dummy_tlb_add_page,
1215 static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1217 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1218 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1220 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1221 cfg->pgsize_bitmap, cfg->ias);
1222 pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
1223 ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
1224 ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
1227 #define __FAIL(ops, i) ({ \
1228 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1229 arm_lpae_dump_ops(ops); \
1230 selftest_running = false; \
1234 static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1236 static const enum io_pgtable_fmt fmts[] __initconst = {
1244 struct io_pgtable_ops *ops;
1246 selftest_running = true;
1248 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1250 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1252 pr_err("selftest: failed to allocate io pgtable ops\n");
1257 * Initial sanity checks.
1258 * Empty page tables shouldn't provide any translations.
1260 if (ops->iova_to_phys(ops, 42))
1261 return __FAIL(ops, i);
1263 if (ops->iova_to_phys(ops, SZ_1G + 42))
1264 return __FAIL(ops, i);
1266 if (ops->iova_to_phys(ops, SZ_2G + 42))
1267 return __FAIL(ops, i);
1270 * Distinct mappings of different granule sizes.
1273 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1276 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1279 IOMMU_CACHE, GFP_KERNEL))
1280 return __FAIL(ops, i);
1282 /* Overlapping mappings */
1283 if (!ops->map(ops, iova, iova + size, size,
1284 IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL))
1285 return __FAIL(ops, i);
1287 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1288 return __FAIL(ops, i);
1294 size = 1UL << __ffs(cfg->pgsize_bitmap);
1295 if (ops->unmap(ops, SZ_1G + size, size, NULL) != size)
1296 return __FAIL(ops, i);
1298 /* Remap of partial unmap */
1299 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL))
1300 return __FAIL(ops, i);
1302 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1303 return __FAIL(ops, i);
1307 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1310 if (ops->unmap(ops, iova, size, NULL) != size)
1311 return __FAIL(ops, i);
1313 if (ops->iova_to_phys(ops, iova + 42))
1314 return __FAIL(ops, i);
1316 /* Remap full block */
1317 if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL))
1318 return __FAIL(ops, i);
1320 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1321 return __FAIL(ops, i);
1326 free_io_pgtable_ops(ops);
1329 selftest_running = false;
1333 static int __init arm_lpae_do_selftests(void)
1335 static const unsigned long pgsize[] __initconst = {
1336 SZ_4K | SZ_2M | SZ_1G,
1341 static const unsigned int ias[] __initconst = {
1342 32, 36, 40, 42, 44, 48,
1345 int i, j, pass = 0, fail = 0;
1346 struct io_pgtable_cfg cfg = {
1347 .tlb = &dummy_tlb_ops,
1349 .coherent_walk = true,
1352 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1353 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1354 cfg.pgsize_bitmap = pgsize[i];
1356 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1358 if (arm_lpae_run_tests(&cfg))
1365 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1366 return fail ? -EFAULT : 0;
1368 subsys_initcall(arm_lpae_do_selftests);