Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 2 | /* |
| 3 | * CPU-agnostic ARM page table allocator. |
| 4 | * |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 5 | * Copyright (C) 2014 ARM Limited |
| 6 | * |
| 7 | * Author: Will Deacon <will.deacon@arm.com> |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt |
| 11 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 12 | #include <linux/atomic.h> |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Rob Herring | b77cf11 | 2019-02-05 10:37:31 -0600 | [diff] [blame] | 14 | #include <linux/io-pgtable.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/sizes.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/types.h> |
Lada Trimasova | 8f6aff9 | 2016-01-27 11:10:32 +0000 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 20 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 21 | #include <asm/barrier.h> |
| 22 | |
Jean-Philippe Brucker | 7cef39d | 2020-09-18 12:18:45 +0200 | [diff] [blame] | 23 | #include "io-pgtable-arm.h" |
| 24 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 25 | #define ARM_LPAE_MAX_ADDR_BITS 52 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 26 | #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 |
| 27 | #define ARM_LPAE_MAX_LEVELS 4 |
| 28 | |
| 29 | /* Struct accessors */ |
| 30 | #define io_pgtable_to_data(x) \ |
| 31 | container_of((x), struct arm_lpae_io_pgtable, iop) |
| 32 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 33 | #define io_pgtable_ops_to_data(x) \ |
| 34 | io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) |
| 35 | |
| 36 | /* |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 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. |
| 39 | */ |
| 40 | #define ARM_LPAE_LVL_SHIFT(l,d) \ |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 41 | (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \ |
| 42 | ilog2(sizeof(arm_lpae_iopte))) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 43 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 44 | #define ARM_LPAE_GRANULE(d) \ |
| 45 | (sizeof(arm_lpae_iopte) << (d)->bits_per_level) |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 46 | #define ARM_LPAE_PGD_SIZE(d) \ |
| 47 | (sizeof(arm_lpae_iopte) << (d)->pgd_bits) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 48 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 49 | #define ARM_LPAE_PTES_PER_TABLE(d) \ |
| 50 | (ARM_LPAE_GRANULE(d) >> ilog2(sizeof(arm_lpae_iopte))) |
| 51 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 52 | /* |
| 53 | * Calculate the index at level l used to map virtual address a using the |
| 54 | * pagetable in d. |
| 55 | */ |
| 56 | #define ARM_LPAE_PGD_IDX(l,d) \ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 57 | ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 58 | |
| 59 | #define ARM_LPAE_LVL_IDX(a,l,d) \ |
Will Deacon | 367bd97 | 2015-02-16 18:38:20 +0000 | [diff] [blame] | 60 | (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 61 | ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) |
| 62 | |
| 63 | /* Calculate the block/page mapping size at level l for pagetable in d. */ |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 64 | #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 65 | |
| 66 | /* Page table bits */ |
| 67 | #define ARM_LPAE_PTE_TYPE_SHIFT 0 |
| 68 | #define ARM_LPAE_PTE_TYPE_MASK 0x3 |
| 69 | |
| 70 | #define ARM_LPAE_PTE_TYPE_BLOCK 1 |
| 71 | #define ARM_LPAE_PTE_TYPE_TABLE 3 |
| 72 | #define ARM_LPAE_PTE_TYPE_PAGE 3 |
| 73 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 74 | #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) |
| 75 | |
Laurent Pinchart | c896c132 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 76 | #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 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) |
Laurent Pinchart | c896c132 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 82 | #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 83 | #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) |
| 84 | |
| 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) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 90 | /* Software bit for solving coherency races */ |
| 91 | #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 92 | |
| 93 | /* Stage-1 PTE */ |
| 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) |
| 98 | |
| 99 | /* Stage-2 PTE */ |
| 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) |
| 106 | |
| 107 | /* Register bits */ |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 108 | #define ARM_LPAE_VTCR_SL0_MASK 0x3 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 109 | |
| 110 | #define ARM_LPAE_TCR_T0SZ_SHIFT 0 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 111 | |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 112 | #define ARM_LPAE_VTCR_PS_SHIFT 16 |
| 113 | #define ARM_LPAE_VTCR_PS_MASK 0x7 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 114 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 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 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 119 | #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 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 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 124 | #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 125 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 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) |
| 129 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 130 | #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL |
| 131 | #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL |
| 132 | |
Sven Peter | 892384c | 2021-08-03 14:16:49 +0200 | [diff] [blame] | 133 | #define APPLE_DART_PTE_PROT_NO_WRITE (1<<7) |
| 134 | #define APPLE_DART_PTE_PROT_NO_READ (1<<8) |
| 135 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 136 | /* IOPTE accessors */ |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 137 | #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 138 | |
Kunkun Jiang | f37eb48 | 2020-12-07 20:01:50 +0800 | [diff] [blame] | 139 | #define iopte_type(pte) \ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 140 | (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) |
| 141 | |
| 142 | #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) |
| 143 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 144 | struct arm_lpae_io_pgtable { |
| 145 | struct io_pgtable iop; |
| 146 | |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 147 | int pgd_bits; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 148 | int start_level; |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 149 | int bits_per_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 150 | |
| 151 | void *pgd; |
| 152 | }; |
| 153 | |
| 154 | typedef u64 arm_lpae_iopte; |
| 155 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 156 | static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, |
| 157 | enum io_pgtable_fmt fmt) |
| 158 | { |
| 159 | if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) |
Kunkun Jiang | f37eb48 | 2020-12-07 20:01:50 +0800 | [diff] [blame] | 160 | return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 161 | |
Kunkun Jiang | f37eb48 | 2020-12-07 20:01:50 +0800 | [diff] [blame] | 162 | return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 165 | static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, |
| 166 | struct arm_lpae_io_pgtable *data) |
| 167 | { |
| 168 | arm_lpae_iopte pte = paddr; |
| 169 | |
| 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; |
| 172 | } |
| 173 | |
| 174 | static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, |
| 175 | struct arm_lpae_io_pgtable *data) |
| 176 | { |
Robin Murphy | 7868805 | 2018-03-29 12:24:52 +0100 | [diff] [blame] | 177 | u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 178 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 179 | if (ARM_LPAE_GRANULE(data) < SZ_64K) |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 180 | return paddr; |
| 181 | |
| 182 | /* Rotate the packed high-order bits back to the top */ |
| 183 | return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); |
| 184 | } |
| 185 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 186 | static bool selftest_running = false; |
| 187 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 188 | static dma_addr_t __arm_lpae_dma_addr(void *pages) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 189 | { |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 190 | return (dma_addr_t)virt_to_phys(pages); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, |
| 194 | struct io_pgtable_cfg *cfg) |
| 195 | { |
| 196 | struct device *dev = cfg->iommu_dev; |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 197 | int order = get_order(size); |
| 198 | struct page *p; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 199 | dma_addr_t dma; |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 200 | void *pages; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 201 | |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 202 | VM_BUG_ON((gfp & __GFP_HIGHMEM)); |
Jean-Philippe Brucker | fac83d2 | 2018-06-18 12:27:54 +0100 | [diff] [blame] | 203 | p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, |
| 204 | gfp | __GFP_ZERO, order); |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 205 | if (!p) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 206 | return NULL; |
| 207 | |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 208 | pages = page_address(p); |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 209 | if (!cfg->coherent_walk) { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 210 | dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); |
| 211 | if (dma_mapping_error(dev, dma)) |
| 212 | goto out_free; |
| 213 | /* |
| 214 | * We depend on the IOMMU being able to work with any physical |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 215 | * address directly, so if the DMA layer suggests otherwise by |
| 216 | * translating or truncating them, that bodes very badly... |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 217 | */ |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 218 | if (dma != virt_to_phys(pages)) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 219 | goto out_unmap; |
| 220 | } |
| 221 | |
| 222 | return pages; |
| 223 | |
| 224 | out_unmap: |
| 225 | dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); |
| 226 | dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); |
| 227 | out_free: |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 228 | __free_pages(p, order); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | static void __arm_lpae_free_pages(void *pages, size_t size, |
| 233 | struct io_pgtable_cfg *cfg) |
| 234 | { |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 235 | if (!cfg->coherent_walk) |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 236 | dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 237 | size, DMA_TO_DEVICE); |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 238 | free_pages((unsigned long)pages, get_order(size)); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 241 | static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries, |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 242 | struct io_pgtable_cfg *cfg) |
| 243 | { |
| 244 | dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 245 | sizeof(*ptep) * num_entries, DMA_TO_DEVICE); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 248 | static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 249 | { |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 250 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 251 | *ptep = 0; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 252 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 253 | if (!cfg->coherent_walk) |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 254 | __arm_lpae_sync_pte(ptep, 1, cfg); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 257 | static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 258 | struct iommu_iotlb_gather *gather, |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 259 | unsigned long iova, size_t size, size_t pgcount, |
| 260 | int lvl, arm_lpae_iopte *ptep); |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 261 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 262 | static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 263 | phys_addr_t paddr, arm_lpae_iopte prot, |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 264 | int lvl, int num_entries, arm_lpae_iopte *ptep) |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 265 | { |
| 266 | arm_lpae_iopte pte = prot; |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 267 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 268 | size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 269 | int i; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 270 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 271 | if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 272 | pte |= ARM_LPAE_PTE_TYPE_PAGE; |
| 273 | else |
| 274 | pte |= ARM_LPAE_PTE_TYPE_BLOCK; |
| 275 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 276 | for (i = 0; i < num_entries; i++) |
| 277 | ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 278 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 279 | if (!cfg->coherent_walk) |
| 280 | __arm_lpae_sync_pte(ptep, num_entries, cfg); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 283 | static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 284 | unsigned long iova, phys_addr_t paddr, |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 285 | arm_lpae_iopte prot, int lvl, int num_entries, |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 286 | arm_lpae_iopte *ptep) |
| 287 | { |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 288 | int i; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 289 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 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); |
| 294 | return -EEXIST; |
| 295 | } else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) { |
| 296 | /* |
| 297 | * We need to unmap and free the old table before |
| 298 | * overwriting it with a block entry. |
| 299 | */ |
| 300 | arm_lpae_iopte *tblp; |
| 301 | size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 302 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 303 | tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 304 | if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1, |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 305 | lvl, tblp) != sz) { |
| 306 | WARN_ON(1); |
| 307 | return -EINVAL; |
| 308 | } |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 309 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 310 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 311 | __arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 315 | static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, |
| 316 | arm_lpae_iopte *ptep, |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 317 | arm_lpae_iopte curr, |
Hector Martin | 9abe2ac | 2021-11-20 12:13:43 +0900 | [diff] [blame] | 318 | struct arm_lpae_io_pgtable *data) |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 319 | { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 320 | arm_lpae_iopte old, new; |
Hector Martin | 9abe2ac | 2021-11-20 12:13:43 +0900 | [diff] [blame] | 321 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 322 | |
Hector Martin | 9abe2ac | 2021-11-20 12:13:43 +0900 | [diff] [blame] | 323 | new = paddr_to_iopte(__pa(table), data) | ARM_LPAE_PTE_TYPE_TABLE; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 324 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 325 | new |= ARM_LPAE_PTE_NSTABLE; |
| 326 | |
Will Deacon | 77f3445 | 2017-06-23 12:02:38 +0100 | [diff] [blame] | 327 | /* |
| 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. |
| 331 | */ |
| 332 | dma_wmb(); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 333 | |
| 334 | old = cmpxchg64_relaxed(ptep, curr, new); |
| 335 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 336 | if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 337 | return old; |
| 338 | |
| 339 | /* Even if it's not ours, there's no point waiting; just kick it */ |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 340 | __arm_lpae_sync_pte(ptep, 1, cfg); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 341 | if (old == curr) |
| 342 | WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); |
| 343 | |
| 344 | return old; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 347 | static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 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) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 351 | { |
| 352 | arm_lpae_iopte *cptep, pte; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 353 | size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 354 | size_t tblsz = ARM_LPAE_GRANULE(data); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 355 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 356 | int ret = 0, num_entries, max_entries, map_idx_start; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 357 | |
| 358 | /* Find our entry at the current level */ |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 359 | map_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 360 | ptep += map_idx_start; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 361 | |
| 362 | /* If we can install a leaf entry at this level, then do so */ |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 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); |
| 367 | if (!ret && mapped) |
| 368 | *mapped += num_entries * size; |
| 369 | |
| 370 | return ret; |
| 371 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 372 | |
| 373 | /* We can't allocate tables at the final level */ |
| 374 | if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | /* Grab a pointer to the next level */ |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 378 | pte = READ_ONCE(*ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 379 | if (!pte) { |
Baolin Wang | f34ce7a | 2020-06-12 11:39:55 +0800 | [diff] [blame] | 380 | cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 381 | if (!cptep) |
| 382 | return -ENOMEM; |
| 383 | |
Hector Martin | 9abe2ac | 2021-11-20 12:13:43 +0900 | [diff] [blame] | 384 | pte = arm_lpae_install_table(cptep, ptep, 0, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 385 | if (pte) |
| 386 | __arm_lpae_free_pages(cptep, tblsz, cfg); |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 387 | } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 388 | __arm_lpae_sync_pte(ptep, 1, cfg); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 391 | if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 392 | cptep = iopte_deref(pte, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 393 | } else if (pte) { |
Oleksandr Tyshchenko | ed46e66 | 2017-02-27 14:30:25 +0200 | [diff] [blame] | 394 | /* We require an unmap first */ |
| 395 | WARN_ON(!selftest_running); |
| 396 | return -EEXIST; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Rinse, repeat */ |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 400 | return __arm_lpae_map(data, iova, paddr, size, pgcount, prot, lvl + 1, |
| 401 | cptep, gfp, mapped); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, |
| 405 | int prot) |
| 406 | { |
| 407 | arm_lpae_iopte pte; |
| 408 | |
Sven Peter | 892384c | 2021-08-03 14:16:49 +0200 | [diff] [blame] | 409 | if (data->iop.fmt == APPLE_DART) { |
| 410 | pte = 0; |
| 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; |
| 415 | return pte; |
| 416 | } |
| 417 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 418 | if (data->iop.fmt == ARM_64_LPAE_S1 || |
| 419 | data->iop.fmt == ARM_32_LPAE_S1) { |
Jeremy Gebben | e7468a2 | 2017-01-06 18:58:09 +0530 | [diff] [blame] | 420 | pte = ARM_LPAE_PTE_nG; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 421 | if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) |
| 422 | pte |= ARM_LPAE_PTE_AP_RDONLY; |
Jeremy Gebben | e7468a2 | 2017-01-06 18:58:09 +0530 | [diff] [blame] | 423 | if (!(prot & IOMMU_PRIV)) |
| 424 | pte |= ARM_LPAE_PTE_AP_UNPRIV; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 425 | } else { |
| 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; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Note that this logic is structured to accommodate Mali LPAE |
| 435 | * having stage-1-like attributes but stage-2-like permissions. |
| 436 | */ |
| 437 | if (data->iop.fmt == ARM_64_LPAE_S2 || |
| 438 | data->iop.fmt == ARM_32_LPAE_S2) { |
Robin Murphy | fb94825 | 2016-04-05 12:39:31 +0100 | [diff] [blame] | 439 | if (prot & IOMMU_MMIO) |
| 440 | pte |= ARM_LPAE_PTE_MEMATTR_DEV; |
| 441 | else if (prot & IOMMU_CACHE) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 442 | pte |= ARM_LPAE_PTE_MEMATTR_OIWB; |
| 443 | else |
| 444 | pte |= ARM_LPAE_PTE_MEMATTR_NC; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 445 | } else { |
| 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); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Robin Murphy | 728da60 | 2020-09-22 15:16:48 +0100 | [diff] [blame] | 454 | /* |
| 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). |
| 459 | */ |
| 460 | if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE) |
Robin Murphy | 7618e47 | 2020-01-10 15:21:51 +0000 | [diff] [blame] | 461 | pte |= ARM_LPAE_PTE_SH_IS; |
| 462 | else |
| 463 | pte |= ARM_LPAE_PTE_SH_OS; |
| 464 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 465 | if (prot & IOMMU_NOEXEC) |
| 466 | pte |= ARM_LPAE_PTE_XN; |
| 467 | |
Robin Murphy | 7618e47 | 2020-01-10 15:21:51 +0000 | [diff] [blame] | 468 | if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 469 | pte |= ARM_LPAE_PTE_NS; |
| 470 | |
| 471 | if (data->iop.fmt != ARM_MALI_LPAE) |
| 472 | pte |= ARM_LPAE_PTE_AF; |
| 473 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 474 | return pte; |
| 475 | } |
| 476 | |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 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) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 480 | { |
| 481 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 482 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 483 | arm_lpae_iopte *ptep = data->pgd; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 484 | int ret, lvl = data->start_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 485 | arm_lpae_iopte prot; |
Robin Murphy | 0809074 | 2020-02-28 14:18:55 +0000 | [diff] [blame] | 486 | long iaext = (s64)iova >> cfg->ias; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 487 | |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 488 | if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize)) |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 489 | return -EINVAL; |
| 490 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 491 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) |
| 492 | iaext = ~iaext; |
| 493 | if (WARN_ON(iaext || paddr >> cfg->oas)) |
Robin Murphy | 7655739 | 2017-07-03 14:52:24 +0100 | [diff] [blame] | 494 | return -ERANGE; |
| 495 | |
Keqian Zhu | f12e0d2 | 2020-12-07 19:57:58 +0800 | [diff] [blame] | 496 | /* If no access, then nothing to do */ |
| 497 | if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) |
| 498 | return 0; |
| 499 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 500 | prot = arm_lpae_prot_to_pte(data, iommu_prot); |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 501 | ret = __arm_lpae_map(data, iova, paddr, pgsize, pgcount, prot, lvl, |
| 502 | ptep, gfp, mapped); |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 503 | /* |
| 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. |
| 506 | */ |
| 507 | wmb(); |
| 508 | |
| 509 | return ret; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 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) |
| 514 | { |
| 515 | return arm_lpae_map_pages(ops, iova, paddr, size, 1, iommu_prot, gfp, |
| 516 | NULL); |
| 517 | } |
| 518 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 519 | static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, |
| 520 | arm_lpae_iopte *ptep) |
| 521 | { |
| 522 | arm_lpae_iopte *start, *end; |
| 523 | unsigned long table_size; |
| 524 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 525 | if (lvl == data->start_level) |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 526 | table_size = ARM_LPAE_PGD_SIZE(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 527 | else |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 528 | table_size = ARM_LPAE_GRANULE(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 529 | |
| 530 | start = ptep; |
Will Deacon | 12c2ab0 | 2015-12-15 16:08:12 +0000 | [diff] [blame] | 531 | |
| 532 | /* Only leaf entries at the last level */ |
| 533 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 534 | end = ptep; |
| 535 | else |
| 536 | end = (void *)ptep + table_size; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 537 | |
| 538 | while (ptep != end) { |
| 539 | arm_lpae_iopte pte = *ptep++; |
| 540 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 541 | if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 542 | continue; |
| 543 | |
| 544 | __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); |
| 545 | } |
| 546 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 547 | __arm_lpae_free_pages(start, table_size, &data->iop.cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static void arm_lpae_free_pgtable(struct io_pgtable *iop) |
| 551 | { |
| 552 | struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); |
| 553 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 554 | __arm_lpae_free_pgtable(data, data->start_level, data->pgd); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 555 | kfree(data); |
| 556 | } |
| 557 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 558 | static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 559 | struct iommu_iotlb_gather *gather, |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 560 | unsigned long iova, size_t size, |
| 561 | arm_lpae_iopte blk_pte, int lvl, |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 562 | arm_lpae_iopte *ptep, size_t pgcount) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 563 | { |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 564 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 565 | arm_lpae_iopte pte, *tablep; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 566 | phys_addr_t blk_paddr; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 567 | size_t tablesz = ARM_LPAE_GRANULE(data); |
| 568 | size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 569 | int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data); |
| 570 | int i, unmap_idx_start = -1, num_entries = 0, max_entries; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 571 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 572 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 573 | return 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 574 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 575 | tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); |
| 576 | if (!tablep) |
| 577 | return 0; /* Bytes unmapped */ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 578 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 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); |
| 583 | } |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 584 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 585 | blk_paddr = iopte_to_paddr(blk_pte, data); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 586 | pte = iopte_prot(blk_pte); |
| 587 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 588 | for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 589 | /* Unmap! */ |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 590 | if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 591 | continue; |
| 592 | |
Isaac J. Manjarres | 41e1eb2 | 2021-06-16 06:38:50 -0700 | [diff] [blame] | 593 | __arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Hector Martin | 9abe2ac | 2021-11-20 12:13:43 +0900 | [diff] [blame] | 596 | pte = arm_lpae_install_table(tablep, ptep, blk_pte, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 597 | if (pte != blk_pte) { |
| 598 | __arm_lpae_free_pages(tablep, tablesz, cfg); |
| 599 | /* |
| 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. |
| 603 | */ |
Kunkun Jiang | f37eb48 | 2020-12-07 20:01:50 +0800 | [diff] [blame] | 604 | if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 605 | return 0; |
| 606 | |
| 607 | tablep = iopte_deref(pte, data); |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 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); |
| 611 | |
| 612 | return num_entries * size; |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 613 | } |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 614 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 615 | return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 618 | static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 619 | struct iommu_iotlb_gather *gather, |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 620 | unsigned long iova, size_t size, size_t pgcount, |
| 621 | int lvl, arm_lpae_iopte *ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 622 | { |
| 623 | arm_lpae_iopte pte; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 624 | struct io_pgtable *iop = &data->iop; |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 625 | int i = 0, num_entries, max_entries, unmap_idx_start; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 626 | |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 627 | /* Something went horribly wrong and we ran out of page table */ |
| 628 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 629 | return 0; |
| 630 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 631 | unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 632 | ptep += unmap_idx_start; |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 633 | pte = READ_ONCE(*ptep); |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 634 | if (WARN_ON(!pte)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 635 | return 0; |
| 636 | |
| 637 | /* If the size matches this level, we're in the right place */ |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 638 | if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 639 | max_entries = ARM_LPAE_PTES_PER_TABLE(data) - unmap_idx_start; |
| 640 | num_entries = min_t(int, pgcount, max_entries); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 641 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 642 | while (i < num_entries) { |
| 643 | pte = READ_ONCE(*ptep); |
| 644 | if (WARN_ON(!pte)) |
| 645 | break; |
| 646 | |
| 647 | __arm_lpae_clear_pte(ptep, &iop->cfg); |
| 648 | |
| 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)); |
Robin Murphy | f7403ab | 2021-08-20 14:14:42 +0100 | [diff] [blame] | 654 | } else if (!iommu_iotlb_gather_queued(gather)) { |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 655 | io_pgtable_tlb_add_page(iop, gather, iova + i * size, size); |
| 656 | } |
| 657 | |
| 658 | ptep++; |
| 659 | i++; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 662 | return i * size; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 663 | } else if (iopte_leaf(pte, lvl, iop->fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 664 | /* |
| 665 | * Insert a table at the next level to map the old region, |
| 666 | * minus the part we want to unmap |
| 667 | */ |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 668 | return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 669 | lvl + 1, ptep, pgcount); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* Keep on walkin' */ |
| 673 | ptep = iopte_deref(pte, data); |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 674 | return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl + 1, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 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) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 680 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 681 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 682 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 683 | arm_lpae_iopte *ptep = data->pgd; |
Robin Murphy | 0809074 | 2020-02-28 14:18:55 +0000 | [diff] [blame] | 684 | long iaext = (s64)iova >> cfg->ias; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 685 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 686 | if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount)) |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 687 | return 0; |
| 688 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 689 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) |
| 690 | iaext = ~iaext; |
| 691 | if (WARN_ON(iaext)) |
Robin Murphy | 7655739 | 2017-07-03 14:52:24 +0100 | [diff] [blame] | 692 | return 0; |
| 693 | |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 694 | return __arm_lpae_unmap(data, gather, iova, pgsize, pgcount, |
| 695 | data->start_level, ptep); |
| 696 | } |
| 697 | |
| 698 | static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, |
| 699 | size_t size, struct iommu_iotlb_gather *gather) |
| 700 | { |
| 701 | return arm_lpae_unmap_pages(ops, iova, size, 1, gather); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, |
| 705 | unsigned long iova) |
| 706 | { |
| 707 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 708 | arm_lpae_iopte pte, *ptep = data->pgd; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 709 | int lvl = data->start_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 710 | |
| 711 | do { |
| 712 | /* Valid IOPTE pointer? */ |
| 713 | if (!ptep) |
| 714 | return 0; |
| 715 | |
| 716 | /* Grab the IOPTE we're interested in */ |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 717 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 718 | pte = READ_ONCE(*ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 719 | |
| 720 | /* Valid entry? */ |
| 721 | if (!pte) |
| 722 | return 0; |
| 723 | |
| 724 | /* Leaf entry? */ |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 725 | if (iopte_leaf(pte, lvl, data->iop.fmt)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 726 | goto found_translation; |
| 727 | |
| 728 | /* Take it to the next level */ |
| 729 | ptep = iopte_deref(pte, data); |
| 730 | } while (++lvl < ARM_LPAE_MAX_LEVELS); |
| 731 | |
| 732 | /* Ran out of page tables to walk */ |
| 733 | return 0; |
| 734 | |
| 735 | found_translation: |
Will Deacon | 7c6d90e | 2016-06-16 18:21:19 +0100 | [diff] [blame] | 736 | iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 737 | return iopte_to_paddr(pte, data) | iova; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) |
| 741 | { |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 742 | unsigned long granule, page_sizes; |
| 743 | unsigned int max_addr_bits = 48; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 744 | |
| 745 | /* |
| 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 |
| 750 | * chosen granule. |
| 751 | */ |
| 752 | if (cfg->pgsize_bitmap & PAGE_SIZE) |
| 753 | granule = 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); |
| 758 | else |
| 759 | granule = 0; |
| 760 | |
| 761 | switch (granule) { |
| 762 | case SZ_4K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 763 | page_sizes = (SZ_4K | SZ_2M | SZ_1G); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 764 | break; |
| 765 | case SZ_16K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 766 | page_sizes = (SZ_16K | SZ_32M); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 767 | break; |
| 768 | case SZ_64K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 769 | max_addr_bits = 52; |
| 770 | page_sizes = (SZ_64K | SZ_512M); |
| 771 | if (cfg->oas > 48) |
| 772 | page_sizes |= 1ULL << 42; /* 4TB */ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 773 | break; |
| 774 | default: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 775 | page_sizes = 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 776 | } |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 777 | |
| 778 | cfg->pgsize_bitmap &= page_sizes; |
| 779 | cfg->ias = min(cfg->ias, max_addr_bits); |
| 780 | cfg->oas = min(cfg->oas, max_addr_bits); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static struct arm_lpae_io_pgtable * |
| 784 | arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) |
| 785 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 786 | struct arm_lpae_io_pgtable *data; |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 787 | int levels, va_bits, pg_shift; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 788 | |
| 789 | arm_lpae_restrict_pgsizes(cfg); |
| 790 | |
| 791 | if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) |
| 792 | return NULL; |
| 793 | |
| 794 | if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) |
| 795 | return NULL; |
| 796 | |
| 797 | if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) |
| 798 | return NULL; |
| 799 | |
| 800 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 801 | if (!data) |
| 802 | return NULL; |
| 803 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 804 | pg_shift = __ffs(cfg->pgsize_bitmap); |
| 805 | data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 806 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 807 | va_bits = cfg->ias - pg_shift; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 808 | levels = DIV_ROUND_UP(va_bits, data->bits_per_level); |
| 809 | data->start_level = ARM_LPAE_MAX_LEVELS - levels; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 810 | |
| 811 | /* Calculate the actual size of our pgd (without concatenation) */ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 812 | data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 813 | |
| 814 | data->iop.ops = (struct io_pgtable_ops) { |
| 815 | .map = arm_lpae_map, |
Isaac J. Manjarres | 4a77b12 | 2021-06-16 06:38:52 -0700 | [diff] [blame] | 816 | .map_pages = arm_lpae_map_pages, |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 817 | .unmap = arm_lpae_unmap, |
Isaac J. Manjarres | 1fe27be | 2021-06-16 06:38:51 -0700 | [diff] [blame] | 818 | .unmap_pages = arm_lpae_unmap_pages, |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 819 | .iova_to_phys = arm_lpae_iova_to_phys, |
| 820 | }; |
| 821 | |
| 822 | return data; |
| 823 | } |
| 824 | |
| 825 | static struct io_pgtable * |
| 826 | arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 827 | { |
| 828 | u64 reg; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 829 | struct arm_lpae_io_pgtable *data; |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 830 | typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 831 | bool tg1; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 832 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 833 | if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | |
Sai Prakash Ranjan | e67890c | 2020-11-25 12:30:11 +0530 | [diff] [blame] | 834 | IO_PGTABLE_QUIRK_ARM_TTBR1 | |
| 835 | IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 836 | return NULL; |
| 837 | |
| 838 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 839 | if (!data) |
| 840 | return NULL; |
| 841 | |
| 842 | /* TCR */ |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 843 | if (cfg->coherent_walk) { |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 844 | tcr->sh = ARM_LPAE_TCR_SH_IS; |
| 845 | tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; |
| 846 | tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; |
Sai Prakash Ranjan | e67890c | 2020-11-25 12:30:11 +0530 | [diff] [blame] | 847 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA) |
| 848 | goto out_free_data; |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 849 | } else { |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 850 | tcr->sh = ARM_LPAE_TCR_SH_OS; |
| 851 | tcr->irgn = ARM_LPAE_TCR_RGN_NC; |
Sai Prakash Ranjan | e67890c | 2020-11-25 12:30:11 +0530 | [diff] [blame] | 852 | if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)) |
| 853 | tcr->orgn = ARM_LPAE_TCR_RGN_NC; |
| 854 | else |
| 855 | tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 856 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 857 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 858 | tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1; |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 859 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 860 | case SZ_4K: |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 861 | tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 862 | break; |
| 863 | case SZ_16K: |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 864 | tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 865 | break; |
| 866 | case SZ_64K: |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 867 | tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 868 | break; |
| 869 | } |
| 870 | |
| 871 | switch (cfg->oas) { |
| 872 | case 32: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 873 | tcr->ips = ARM_LPAE_TCR_PS_32_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 874 | break; |
| 875 | case 36: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 876 | tcr->ips = ARM_LPAE_TCR_PS_36_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 877 | break; |
| 878 | case 40: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 879 | tcr->ips = ARM_LPAE_TCR_PS_40_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 880 | break; |
| 881 | case 42: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 882 | tcr->ips = ARM_LPAE_TCR_PS_42_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 883 | break; |
| 884 | case 44: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 885 | tcr->ips = ARM_LPAE_TCR_PS_44_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 886 | break; |
| 887 | case 48: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 888 | tcr->ips = ARM_LPAE_TCR_PS_48_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 889 | break; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 890 | case 52: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 891 | tcr->ips = ARM_LPAE_TCR_PS_52_BIT; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 892 | break; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 893 | default: |
| 894 | goto out_free_data; |
| 895 | } |
| 896 | |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 897 | tcr->tsz = 64ULL - cfg->ias; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 898 | |
| 899 | /* MAIRs */ |
| 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 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 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)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 908 | |
Robin Murphy | 205577a | 2019-10-25 19:08:36 +0100 | [diff] [blame] | 909 | cfg->arm_lpae_s1_cfg.mair = reg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 910 | |
| 911 | /* Looking good; allocate a pgd */ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 912 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), |
| 913 | GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 914 | if (!data->pgd) |
| 915 | goto out_free_data; |
| 916 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 917 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 918 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 919 | |
Robin Murphy | d1e5f26 | 2019-10-25 19:08:37 +0100 | [diff] [blame] | 920 | /* TTBR */ |
| 921 | cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 922 | return &data->iop; |
| 923 | |
| 924 | out_free_data: |
| 925 | kfree(data); |
| 926 | return NULL; |
| 927 | } |
| 928 | |
| 929 | static struct io_pgtable * |
| 930 | arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 931 | { |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 932 | u64 sl; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 933 | struct arm_lpae_io_pgtable *data; |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 934 | typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 935 | |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 936 | /* The NS quirk doesn't apply at stage 2 */ |
Robin Murphy | a8e5f04 | 2021-08-11 13:21:29 +0100 | [diff] [blame] | 937 | if (cfg->quirks) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 938 | return NULL; |
| 939 | |
| 940 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 941 | if (!data) |
| 942 | return NULL; |
| 943 | |
| 944 | /* |
| 945 | * Concatenate PGDs at level 1 if possible in order to reduce |
| 946 | * the depth of the stage-2 walk. |
| 947 | */ |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 948 | if (data->start_level == 0) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 949 | unsigned long pgd_pages; |
| 950 | |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 951 | pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 952 | if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 953 | data->pgd_bits += data->bits_per_level; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 954 | data->start_level++; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
| 958 | /* VTCR */ |
Will Deacon | 30d2acb | 2020-01-10 11:40:33 +0000 | [diff] [blame] | 959 | if (cfg->coherent_walk) { |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 960 | vtcr->sh = ARM_LPAE_TCR_SH_IS; |
| 961 | vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; |
| 962 | vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; |
Will Deacon | 30d2acb | 2020-01-10 11:40:33 +0000 | [diff] [blame] | 963 | } else { |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 964 | vtcr->sh = ARM_LPAE_TCR_SH_OS; |
| 965 | vtcr->irgn = ARM_LPAE_TCR_RGN_NC; |
| 966 | vtcr->orgn = ARM_LPAE_TCR_RGN_NC; |
Will Deacon | 30d2acb | 2020-01-10 11:40:33 +0000 | [diff] [blame] | 967 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 968 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 969 | sl = data->start_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 970 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 971 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 972 | case SZ_4K: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 973 | vtcr->tg = ARM_LPAE_TCR_TG0_4K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 974 | sl++; /* SL0 format is different for 4K granule size */ |
| 975 | break; |
| 976 | case SZ_16K: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 977 | vtcr->tg = ARM_LPAE_TCR_TG0_16K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 978 | break; |
| 979 | case SZ_64K: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 980 | vtcr->tg = ARM_LPAE_TCR_TG0_64K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 981 | break; |
| 982 | } |
| 983 | |
| 984 | switch (cfg->oas) { |
| 985 | case 32: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 986 | vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 987 | break; |
| 988 | case 36: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 989 | vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 990 | break; |
| 991 | case 40: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 992 | vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 993 | break; |
| 994 | case 42: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 995 | vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 996 | break; |
| 997 | case 44: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 998 | vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 999 | break; |
| 1000 | case 48: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 1001 | vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1002 | break; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 1003 | case 52: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 1004 | vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 1005 | break; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1006 | default: |
| 1007 | goto out_free_data; |
| 1008 | } |
| 1009 | |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 1010 | vtcr->tsz = 64ULL - cfg->ias; |
| 1011 | vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* Allocate pgd pages */ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1014 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), |
| 1015 | GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1016 | if (!data->pgd) |
| 1017 | goto out_free_data; |
| 1018 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 1019 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 1020 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1021 | |
| 1022 | /* VTTBR */ |
| 1023 | cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); |
| 1024 | return &data->iop; |
| 1025 | |
| 1026 | out_free_data: |
| 1027 | kfree(data); |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | |
| 1031 | static struct io_pgtable * |
| 1032 | arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 1033 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1034 | if (cfg->ias > 32 || cfg->oas > 40) |
| 1035 | return NULL; |
| 1036 | |
| 1037 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 1038 | return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static struct io_pgtable * |
| 1042 | arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 1043 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1044 | if (cfg->ias > 40 || cfg->oas > 40) |
| 1045 | return NULL; |
| 1046 | |
| 1047 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 1048 | return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1051 | static struct io_pgtable * |
| 1052 | arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) |
| 1053 | { |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1054 | struct arm_lpae_io_pgtable *data; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1055 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1056 | /* No quirks for Mali (hopefully) */ |
| 1057 | if (cfg->quirks) |
| 1058 | return NULL; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1059 | |
Robin Murphy | 1be08f4 | 2019-09-30 15:11:01 +0100 | [diff] [blame] | 1060 | if (cfg->ias > 48 || cfg->oas > 40) |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1061 | return NULL; |
| 1062 | |
| 1063 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1064 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1065 | data = arm_lpae_alloc_pgtable(cfg); |
| 1066 | if (!data) |
| 1067 | return NULL; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1068 | |
Robin Murphy | 1be08f4 | 2019-09-30 15:11:01 +0100 | [diff] [blame] | 1069 | /* Mali seems to need a full 4-level table regardless of IAS */ |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 1070 | if (data->start_level > 0) { |
| 1071 | data->start_level = 0; |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1072 | data->pgd_bits = 0; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1073 | } |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1074 | /* |
| 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. |
| 1080 | */ |
| 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)); |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1088 | |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1089 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, |
| 1090 | cfg); |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1091 | if (!data->pgd) |
| 1092 | goto out_free_data; |
| 1093 | |
| 1094 | /* Ensure the empty pgd is visible before TRANSTAB can be written */ |
| 1095 | wmb(); |
| 1096 | |
| 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; |
Robin Murphy | 728da60 | 2020-09-22 15:16:48 +0100 | [diff] [blame] | 1100 | if (cfg->coherent_walk) |
| 1101 | cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER; |
| 1102 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1103 | return &data->iop; |
| 1104 | |
| 1105 | out_free_data: |
| 1106 | kfree(data); |
| 1107 | return NULL; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1108 | } |
| 1109 | |
Sven Peter | 892384c | 2021-08-03 14:16:49 +0200 | [diff] [blame] | 1110 | static struct io_pgtable * |
| 1111 | apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) |
| 1112 | { |
| 1113 | struct arm_lpae_io_pgtable *data; |
| 1114 | int i; |
| 1115 | |
| 1116 | if (cfg->oas > 36) |
| 1117 | return NULL; |
| 1118 | |
| 1119 | data = arm_lpae_alloc_pgtable(cfg); |
| 1120 | if (!data) |
| 1121 | return NULL; |
| 1122 | |
| 1123 | /* |
| 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. |
| 1129 | */ |
| 1130 | if (data->start_level < 1) |
| 1131 | goto out_free_data; |
| 1132 | if (data->start_level == 1 && data->pgd_bits > 2) |
| 1133 | goto out_free_data; |
| 1134 | if (data->start_level > 1) |
| 1135 | data->pgd_bits = 0; |
| 1136 | data->start_level = 2; |
| 1137 | cfg->apple_dart_cfg.n_ttbrs = 1 << data->pgd_bits; |
| 1138 | data->pgd_bits += data->bits_per_level; |
| 1139 | |
| 1140 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, |
| 1141 | cfg); |
| 1142 | if (!data->pgd) |
| 1143 | goto out_free_data; |
| 1144 | |
| 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)); |
| 1148 | |
| 1149 | return &data->iop; |
| 1150 | |
| 1151 | out_free_data: |
| 1152 | kfree(data); |
| 1153 | return NULL; |
| 1154 | } |
| 1155 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 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, |
| 1159 | }; |
| 1160 | |
| 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, |
| 1164 | }; |
| 1165 | |
| 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, |
| 1169 | }; |
| 1170 | |
| 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, |
| 1174 | }; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1175 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 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, |
| 1179 | }; |
| 1180 | |
Sven Peter | 892384c | 2021-08-03 14:16:49 +0200 | [diff] [blame] | 1181 | struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = { |
| 1182 | .alloc = apple_dart_alloc_pgtable, |
| 1183 | .free = arm_lpae_free_pgtable, |
| 1184 | }; |
| 1185 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1186 | #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST |
| 1187 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1188 | static struct io_pgtable_cfg *cfg_cookie __initdata; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1189 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1190 | static void __init dummy_tlb_flush_all(void *cookie) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1191 | { |
| 1192 | WARN_ON(cookie != cfg_cookie); |
| 1193 | } |
| 1194 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1195 | static void __init dummy_tlb_flush(unsigned long iova, size_t size, |
| 1196 | size_t granule, void *cookie) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1197 | { |
| 1198 | WARN_ON(cookie != cfg_cookie); |
| 1199 | WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); |
| 1200 | } |
| 1201 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1202 | static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, |
| 1203 | unsigned long iova, size_t granule, |
| 1204 | void *cookie) |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 1205 | { |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 1206 | dummy_tlb_flush(iova, granule, granule, cookie); |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame] | 1209 | static const struct iommu_flush_ops dummy_tlb_ops __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1210 | .tlb_flush_all = dummy_tlb_flush_all, |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 1211 | .tlb_flush_walk = dummy_tlb_flush, |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 1212 | .tlb_add_page = dummy_tlb_add_page, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1213 | }; |
| 1214 | |
| 1215 | static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) |
| 1216 | { |
| 1217 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 1218 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 1219 | |
| 1220 | pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", |
| 1221 | cfg->pgsize_bitmap, cfg->ias); |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 1222 | pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1223 | ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 1224 | ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 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; \ |
| 1231 | -EFAULT; \ |
| 1232 | }) |
| 1233 | |
| 1234 | static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) |
| 1235 | { |
Christophe JAILLET | 9062c1d | 2019-09-09 22:19:19 +0200 | [diff] [blame] | 1236 | static const enum io_pgtable_fmt fmts[] __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1237 | ARM_64_LPAE_S1, |
| 1238 | ARM_64_LPAE_S2, |
| 1239 | }; |
| 1240 | |
| 1241 | int i, j; |
| 1242 | unsigned long iova; |
| 1243 | size_t size; |
| 1244 | struct io_pgtable_ops *ops; |
| 1245 | |
| 1246 | selftest_running = true; |
| 1247 | |
| 1248 | for (i = 0; i < ARRAY_SIZE(fmts); ++i) { |
| 1249 | cfg_cookie = cfg; |
| 1250 | ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); |
| 1251 | if (!ops) { |
| 1252 | pr_err("selftest: failed to allocate io pgtable ops\n"); |
| 1253 | return -ENOMEM; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * Initial sanity checks. |
| 1258 | * Empty page tables shouldn't provide any translations. |
| 1259 | */ |
| 1260 | if (ops->iova_to_phys(ops, 42)) |
| 1261 | return __FAIL(ops, i); |
| 1262 | |
| 1263 | if (ops->iova_to_phys(ops, SZ_1G + 42)) |
| 1264 | return __FAIL(ops, i); |
| 1265 | |
| 1266 | if (ops->iova_to_phys(ops, SZ_2G + 42)) |
| 1267 | return __FAIL(ops, i); |
| 1268 | |
| 1269 | /* |
| 1270 | * Distinct mappings of different granule sizes. |
| 1271 | */ |
| 1272 | iova = 0; |
Kefeng Wang | 4ae8a5c | 2016-09-21 13:41:31 +0800 | [diff] [blame] | 1273 | for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1274 | size = 1UL << j; |
| 1275 | |
| 1276 | if (ops->map(ops, iova, iova, size, IOMMU_READ | |
| 1277 | IOMMU_WRITE | |
| 1278 | IOMMU_NOEXEC | |
Baolin Wang | f34ce7a | 2020-06-12 11:39:55 +0800 | [diff] [blame] | 1279 | IOMMU_CACHE, GFP_KERNEL)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1280 | return __FAIL(ops, i); |
| 1281 | |
| 1282 | /* Overlapping mappings */ |
| 1283 | if (!ops->map(ops, iova, iova + size, size, |
Baolin Wang | f34ce7a | 2020-06-12 11:39:55 +0800 | [diff] [blame] | 1284 | IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1285 | return __FAIL(ops, i); |
| 1286 | |
| 1287 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1288 | return __FAIL(ops, i); |
| 1289 | |
| 1290 | iova += SZ_1G; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /* Partial unmap */ |
| 1294 | size = 1UL << __ffs(cfg->pgsize_bitmap); |
Will Deacon | a2d3a38 | 2019-07-02 16:44:58 +0100 | [diff] [blame] | 1295 | if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1296 | return __FAIL(ops, i); |
| 1297 | |
| 1298 | /* Remap of partial unmap */ |
Baolin Wang | f34ce7a | 2020-06-12 11:39:55 +0800 | [diff] [blame] | 1299 | if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1300 | return __FAIL(ops, i); |
| 1301 | |
| 1302 | if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) |
| 1303 | return __FAIL(ops, i); |
| 1304 | |
| 1305 | /* Full unmap */ |
| 1306 | iova = 0; |
YueHaibing | f793b13 | 2018-04-26 12:49:29 +0800 | [diff] [blame] | 1307 | for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1308 | size = 1UL << j; |
| 1309 | |
Will Deacon | a2d3a38 | 2019-07-02 16:44:58 +0100 | [diff] [blame] | 1310 | if (ops->unmap(ops, iova, size, NULL) != size) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1311 | return __FAIL(ops, i); |
| 1312 | |
| 1313 | if (ops->iova_to_phys(ops, iova + 42)) |
| 1314 | return __FAIL(ops, i); |
| 1315 | |
| 1316 | /* Remap full block */ |
Baolin Wang | f34ce7a | 2020-06-12 11:39:55 +0800 | [diff] [blame] | 1317 | if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1318 | return __FAIL(ops, i); |
| 1319 | |
| 1320 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1321 | return __FAIL(ops, i); |
| 1322 | |
| 1323 | iova += SZ_1G; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | free_io_pgtable_ops(ops); |
| 1327 | } |
| 1328 | |
| 1329 | selftest_running = false; |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | static int __init arm_lpae_do_selftests(void) |
| 1334 | { |
Christophe JAILLET | 9062c1d | 2019-09-09 22:19:19 +0200 | [diff] [blame] | 1335 | static const unsigned long pgsize[] __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1336 | SZ_4K | SZ_2M | SZ_1G, |
| 1337 | SZ_16K | SZ_32M, |
| 1338 | SZ_64K | SZ_512M, |
| 1339 | }; |
| 1340 | |
Christophe JAILLET | 9062c1d | 2019-09-09 22:19:19 +0200 | [diff] [blame] | 1341 | static const unsigned int ias[] __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1342 | 32, 36, 40, 42, 44, 48, |
| 1343 | }; |
| 1344 | |
| 1345 | int i, j, pass = 0, fail = 0; |
| 1346 | struct io_pgtable_cfg cfg = { |
| 1347 | .tlb = &dummy_tlb_ops, |
| 1348 | .oas = 48, |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 1349 | .coherent_walk = true, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1350 | }; |
| 1351 | |
| 1352 | for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { |
| 1353 | for (j = 0; j < ARRAY_SIZE(ias); ++j) { |
| 1354 | cfg.pgsize_bitmap = pgsize[i]; |
| 1355 | cfg.ias = ias[j]; |
| 1356 | pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", |
| 1357 | pgsize[i], ias[j]); |
| 1358 | if (arm_lpae_run_tests(&cfg)) |
| 1359 | fail++; |
| 1360 | else |
| 1361 | pass++; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); |
| 1366 | return fail ? -EFAULT : 0; |
| 1367 | } |
| 1368 | subsys_initcall(arm_lpae_do_selftests); |
| 1369 | #endif |