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/iommu.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/sizes.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/types.h> |
Lada Trimasova | 8f6aff9 | 2016-01-27 11:10:32 +0000 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 21 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 22 | #include <asm/barrier.h> |
| 23 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 24 | #define ARM_LPAE_MAX_ADDR_BITS 52 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 25 | #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 |
| 26 | #define ARM_LPAE_MAX_LEVELS 4 |
| 27 | |
| 28 | /* Struct accessors */ |
| 29 | #define io_pgtable_to_data(x) \ |
| 30 | container_of((x), struct arm_lpae_io_pgtable, iop) |
| 31 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 32 | #define io_pgtable_ops_to_data(x) \ |
| 33 | io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) |
| 34 | |
| 35 | /* |
| 36 | * For consistency with the architecture, we always consider |
| 37 | * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 |
| 38 | */ |
| 39 | #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) |
| 40 | |
| 41 | /* |
| 42 | * Calculate the right shift amount to get to the portion describing level l |
| 43 | * in a virtual address mapped by the pagetable in d. |
| 44 | */ |
| 45 | #define ARM_LPAE_LVL_SHIFT(l,d) \ |
| 46 | ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ |
| 47 | * (d)->bits_per_level) + (d)->pg_shift) |
| 48 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 49 | #define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift) |
| 50 | |
Will Deacon | 367bd97 | 2015-02-16 18:38:20 +0000 | [diff] [blame] | 51 | #define ARM_LPAE_PAGES_PER_PGD(d) \ |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 52 | DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Calculate the index at level l used to map virtual address a using the |
| 56 | * pagetable in d. |
| 57 | */ |
| 58 | #define ARM_LPAE_PGD_IDX(l,d) \ |
| 59 | ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) |
| 60 | |
| 61 | #define ARM_LPAE_LVL_IDX(a,l,d) \ |
Will Deacon | 367bd97 | 2015-02-16 18:38:20 +0000 | [diff] [blame] | 62 | (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 63 | ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) |
| 64 | |
| 65 | /* Calculate the block/page mapping size at level l for pagetable in d. */ |
| 66 | #define ARM_LPAE_BLOCK_SIZE(l,d) \ |
Robin Murphy | 022f4e4 | 2017-04-03 13:12:10 +0100 | [diff] [blame] | 67 | (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 68 | ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) |
| 69 | |
| 70 | /* Page table bits */ |
| 71 | #define ARM_LPAE_PTE_TYPE_SHIFT 0 |
| 72 | #define ARM_LPAE_PTE_TYPE_MASK 0x3 |
| 73 | |
| 74 | #define ARM_LPAE_PTE_TYPE_BLOCK 1 |
| 75 | #define ARM_LPAE_PTE_TYPE_TABLE 3 |
| 76 | #define ARM_LPAE_PTE_TYPE_PAGE 3 |
| 77 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 78 | #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) |
| 79 | |
Laurent Pinchart | c896c132 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 80 | #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 81 | #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) |
| 82 | #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) |
| 83 | #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) |
| 84 | #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) |
| 85 | #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) |
Laurent Pinchart | c896c132 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 86 | #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 87 | #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) |
| 88 | |
| 89 | #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) |
| 90 | /* Ignore the contiguous bit for block splitting */ |
| 91 | #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) |
| 92 | #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ |
| 93 | ARM_LPAE_PTE_ATTR_HI_MASK) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 94 | /* Software bit for solving coherency races */ |
| 95 | #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 96 | |
| 97 | /* Stage-1 PTE */ |
| 98 | #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) |
| 99 | #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) |
| 100 | #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 |
| 101 | #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) |
| 102 | |
| 103 | /* Stage-2 PTE */ |
| 104 | #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) |
| 105 | #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) |
| 106 | #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) |
| 107 | #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) |
| 108 | #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) |
| 109 | #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) |
| 110 | |
| 111 | /* Register bits */ |
| 112 | #define ARM_32_LPAE_TCR_EAE (1 << 31) |
| 113 | #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) |
| 114 | |
Will Deacon | 63979b8 | 2015-03-18 10:22:18 +0000 | [diff] [blame] | 115 | #define ARM_LPAE_TCR_EPD1 (1 << 23) |
| 116 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 117 | #define ARM_LPAE_TCR_TG0_4K (0 << 14) |
| 118 | #define ARM_LPAE_TCR_TG0_64K (1 << 14) |
| 119 | #define ARM_LPAE_TCR_TG0_16K (2 << 14) |
| 120 | |
| 121 | #define ARM_LPAE_TCR_SH0_SHIFT 12 |
| 122 | #define ARM_LPAE_TCR_SH0_MASK 0x3 |
| 123 | #define ARM_LPAE_TCR_SH_NS 0 |
| 124 | #define ARM_LPAE_TCR_SH_OS 2 |
| 125 | #define ARM_LPAE_TCR_SH_IS 3 |
| 126 | |
| 127 | #define ARM_LPAE_TCR_ORGN0_SHIFT 10 |
| 128 | #define ARM_LPAE_TCR_IRGN0_SHIFT 8 |
| 129 | #define ARM_LPAE_TCR_RGN_MASK 0x3 |
| 130 | #define ARM_LPAE_TCR_RGN_NC 0 |
| 131 | #define ARM_LPAE_TCR_RGN_WBWA 1 |
| 132 | #define ARM_LPAE_TCR_RGN_WT 2 |
| 133 | #define ARM_LPAE_TCR_RGN_WB 3 |
| 134 | |
| 135 | #define ARM_LPAE_TCR_SL0_SHIFT 6 |
| 136 | #define ARM_LPAE_TCR_SL0_MASK 0x3 |
| 137 | |
| 138 | #define ARM_LPAE_TCR_T0SZ_SHIFT 0 |
| 139 | #define ARM_LPAE_TCR_SZ_MASK 0xf |
| 140 | |
| 141 | #define ARM_LPAE_TCR_PS_SHIFT 16 |
| 142 | #define ARM_LPAE_TCR_PS_MASK 0x7 |
| 143 | |
| 144 | #define ARM_LPAE_TCR_IPS_SHIFT 32 |
| 145 | #define ARM_LPAE_TCR_IPS_MASK 0x7 |
| 146 | |
| 147 | #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL |
| 148 | #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL |
| 149 | #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL |
| 150 | #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL |
| 151 | #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL |
| 152 | #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 153 | #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 154 | |
| 155 | #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) |
| 156 | #define ARM_LPAE_MAIR_ATTR_MASK 0xff |
| 157 | #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 |
| 158 | #define ARM_LPAE_MAIR_ATTR_NC 0x44 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 159 | #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 160 | #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff |
| 161 | #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 |
| 162 | #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 |
| 163 | #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 164 | #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 165 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 166 | #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) |
| 167 | #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) |
| 168 | #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) |
| 169 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 170 | /* IOPTE accessors */ |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 171 | #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 172 | |
| 173 | #define iopte_type(pte,l) \ |
| 174 | (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) |
| 175 | |
| 176 | #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) |
| 177 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 178 | struct arm_lpae_io_pgtable { |
| 179 | struct io_pgtable iop; |
| 180 | |
| 181 | int levels; |
| 182 | size_t pgd_size; |
| 183 | unsigned long pg_shift; |
| 184 | unsigned long bits_per_level; |
| 185 | |
| 186 | void *pgd; |
| 187 | }; |
| 188 | |
| 189 | typedef u64 arm_lpae_iopte; |
| 190 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 191 | static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, |
| 192 | enum io_pgtable_fmt fmt) |
| 193 | { |
| 194 | if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) |
| 195 | return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE; |
| 196 | |
| 197 | return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK; |
| 198 | } |
| 199 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 200 | static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, |
| 201 | struct arm_lpae_io_pgtable *data) |
| 202 | { |
| 203 | arm_lpae_iopte pte = paddr; |
| 204 | |
| 205 | /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ |
| 206 | return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; |
| 207 | } |
| 208 | |
| 209 | static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, |
| 210 | struct arm_lpae_io_pgtable *data) |
| 211 | { |
Robin Murphy | 7868805 | 2018-03-29 12:24:52 +0100 | [diff] [blame] | 212 | u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 213 | |
| 214 | if (data->pg_shift < 16) |
| 215 | return paddr; |
| 216 | |
| 217 | /* Rotate the packed high-order bits back to the top */ |
| 218 | return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); |
| 219 | } |
| 220 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 221 | static bool selftest_running = false; |
| 222 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 223 | static dma_addr_t __arm_lpae_dma_addr(void *pages) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 224 | { |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 225 | return (dma_addr_t)virt_to_phys(pages); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, |
| 229 | struct io_pgtable_cfg *cfg) |
| 230 | { |
| 231 | struct device *dev = cfg->iommu_dev; |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 232 | int order = get_order(size); |
| 233 | struct page *p; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 234 | dma_addr_t dma; |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 235 | void *pages; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 236 | |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 237 | VM_BUG_ON((gfp & __GFP_HIGHMEM)); |
Jean-Philippe Brucker | fac83d2 | 2018-06-18 12:27:54 +0100 | [diff] [blame] | 238 | p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, |
| 239 | gfp | __GFP_ZERO, order); |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 240 | if (!p) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 241 | return NULL; |
| 242 | |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 243 | pages = page_address(p); |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 244 | if (!cfg->coherent_walk) { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 245 | dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); |
| 246 | if (dma_mapping_error(dev, dma)) |
| 247 | goto out_free; |
| 248 | /* |
| 249 | * We depend on the IOMMU being able to work with any physical |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 250 | * address directly, so if the DMA layer suggests otherwise by |
| 251 | * translating or truncating them, that bodes very badly... |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 252 | */ |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 253 | if (dma != virt_to_phys(pages)) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 254 | goto out_unmap; |
| 255 | } |
| 256 | |
| 257 | return pages; |
| 258 | |
| 259 | out_unmap: |
| 260 | dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); |
| 261 | dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); |
| 262 | out_free: |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 263 | __free_pages(p, order); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | static void __arm_lpae_free_pages(void *pages, size_t size, |
| 268 | struct io_pgtable_cfg *cfg) |
| 269 | { |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 270 | if (!cfg->coherent_walk) |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 271 | dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 272 | size, DMA_TO_DEVICE); |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 273 | free_pages((unsigned long)pages, get_order(size)); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 276 | static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, |
| 277 | struct io_pgtable_cfg *cfg) |
| 278 | { |
| 279 | dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), |
| 280 | sizeof(*ptep), DMA_TO_DEVICE); |
| 281 | } |
| 282 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 283 | static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 284 | struct io_pgtable_cfg *cfg) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 285 | { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 286 | *ptep = pte; |
| 287 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 288 | if (!cfg->coherent_walk) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 289 | __arm_lpae_sync_pte(ptep, cfg); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 292 | static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
| 293 | unsigned long iova, size_t size, int lvl, |
| 294 | arm_lpae_iopte *ptep); |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 295 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 296 | static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 297 | phys_addr_t paddr, arm_lpae_iopte prot, |
| 298 | int lvl, arm_lpae_iopte *ptep) |
| 299 | { |
| 300 | arm_lpae_iopte pte = prot; |
| 301 | |
| 302 | if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 303 | pte |= ARM_LPAE_PTE_NS; |
| 304 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 305 | 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] | 306 | pte |= ARM_LPAE_PTE_TYPE_PAGE; |
| 307 | else |
| 308 | pte |= ARM_LPAE_PTE_TYPE_BLOCK; |
| 309 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 310 | if (data->iop.fmt != ARM_MALI_LPAE) |
| 311 | pte |= ARM_LPAE_PTE_AF; |
| 312 | pte |= ARM_LPAE_PTE_SH_IS; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 313 | pte |= paddr_to_iopte(paddr, data); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 314 | |
| 315 | __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); |
| 316 | } |
| 317 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 318 | static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 319 | unsigned long iova, phys_addr_t paddr, |
| 320 | arm_lpae_iopte prot, int lvl, |
| 321 | arm_lpae_iopte *ptep) |
| 322 | { |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 323 | arm_lpae_iopte pte = *ptep; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 324 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 325 | if (iopte_leaf(pte, lvl, data->iop.fmt)) { |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 326 | /* We require an unmap first */ |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 327 | WARN_ON(!selftest_running); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 328 | return -EEXIST; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 329 | } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 330 | /* |
| 331 | * We need to unmap and free the old table before |
| 332 | * overwriting it with a block entry. |
| 333 | */ |
| 334 | arm_lpae_iopte *tblp; |
| 335 | size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 336 | |
| 337 | tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 338 | if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz)) |
| 339 | return -EINVAL; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 340 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 341 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 342 | __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 346 | static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, |
| 347 | arm_lpae_iopte *ptep, |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 348 | arm_lpae_iopte curr, |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 349 | struct io_pgtable_cfg *cfg) |
| 350 | { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 351 | arm_lpae_iopte old, new; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 352 | |
| 353 | new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; |
| 354 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 355 | new |= ARM_LPAE_PTE_NSTABLE; |
| 356 | |
Will Deacon | 77f3445 | 2017-06-23 12:02:38 +0100 | [diff] [blame] | 357 | /* |
| 358 | * Ensure the table itself is visible before its PTE can be. |
| 359 | * Whilst we could get away with cmpxchg64_release below, this |
| 360 | * doesn't have any ordering semantics when !CONFIG_SMP. |
| 361 | */ |
| 362 | dma_wmb(); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 363 | |
| 364 | old = cmpxchg64_relaxed(ptep, curr, new); |
| 365 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 366 | if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 367 | return old; |
| 368 | |
| 369 | /* Even if it's not ours, there's no point waiting; just kick it */ |
| 370 | __arm_lpae_sync_pte(ptep, cfg); |
| 371 | if (old == curr) |
| 372 | WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); |
| 373 | |
| 374 | return old; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 377 | static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, |
| 378 | phys_addr_t paddr, size_t size, arm_lpae_iopte prot, |
| 379 | int lvl, arm_lpae_iopte *ptep) |
| 380 | { |
| 381 | arm_lpae_iopte *cptep, pte; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 382 | size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 383 | size_t tblsz = ARM_LPAE_GRANULE(data); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 384 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 385 | |
| 386 | /* Find our entry at the current level */ |
| 387 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 388 | |
| 389 | /* If we can install a leaf entry at this level, then do so */ |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 390 | if (size == block_size && (size & cfg->pgsize_bitmap)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 391 | return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); |
| 392 | |
| 393 | /* We can't allocate tables at the final level */ |
| 394 | if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) |
| 395 | return -EINVAL; |
| 396 | |
| 397 | /* Grab a pointer to the next level */ |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 398 | pte = READ_ONCE(*ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 399 | if (!pte) { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 400 | cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 401 | if (!cptep) |
| 402 | return -ENOMEM; |
| 403 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 404 | pte = arm_lpae_install_table(cptep, ptep, 0, cfg); |
| 405 | if (pte) |
| 406 | __arm_lpae_free_pages(cptep, tblsz, cfg); |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 407 | } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 408 | __arm_lpae_sync_pte(ptep, cfg); |
| 409 | } |
| 410 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 411 | if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 412 | cptep = iopte_deref(pte, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 413 | } else if (pte) { |
Oleksandr Tyshchenko | ed46e66 | 2017-02-27 14:30:25 +0200 | [diff] [blame] | 414 | /* We require an unmap first */ |
| 415 | WARN_ON(!selftest_running); |
| 416 | return -EEXIST; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Rinse, repeat */ |
| 420 | return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); |
| 421 | } |
| 422 | |
| 423 | static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, |
| 424 | int prot) |
| 425 | { |
| 426 | arm_lpae_iopte pte; |
| 427 | |
| 428 | if (data->iop.fmt == ARM_64_LPAE_S1 || |
| 429 | data->iop.fmt == ARM_32_LPAE_S1) { |
Jeremy Gebben | e7468a2 | 2017-01-06 18:58:09 +0530 | [diff] [blame] | 430 | pte = ARM_LPAE_PTE_nG; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 431 | if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) |
| 432 | pte |= ARM_LPAE_PTE_AP_RDONLY; |
Jeremy Gebben | e7468a2 | 2017-01-06 18:58:09 +0530 | [diff] [blame] | 433 | if (!(prot & IOMMU_PRIV)) |
| 434 | pte |= ARM_LPAE_PTE_AP_UNPRIV; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 435 | } else { |
| 436 | pte = ARM_LPAE_PTE_HAP_FAULT; |
| 437 | if (prot & IOMMU_READ) |
| 438 | pte |= ARM_LPAE_PTE_HAP_READ; |
| 439 | if (prot & IOMMU_WRITE) |
| 440 | pte |= ARM_LPAE_PTE_HAP_WRITE; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Note that this logic is structured to accommodate Mali LPAE |
| 445 | * having stage-1-like attributes but stage-2-like permissions. |
| 446 | */ |
| 447 | if (data->iop.fmt == ARM_64_LPAE_S2 || |
| 448 | data->iop.fmt == ARM_32_LPAE_S2) { |
Robin Murphy | fb94825 | 2016-04-05 12:39:31 +0100 | [diff] [blame] | 449 | if (prot & IOMMU_MMIO) |
| 450 | pte |= ARM_LPAE_PTE_MEMATTR_DEV; |
| 451 | else if (prot & IOMMU_CACHE) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 452 | pte |= ARM_LPAE_PTE_MEMATTR_OIWB; |
| 453 | else |
| 454 | pte |= ARM_LPAE_PTE_MEMATTR_NC; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 455 | } else { |
| 456 | if (prot & IOMMU_MMIO) |
| 457 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV |
| 458 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
| 459 | else if (prot & IOMMU_CACHE) |
| 460 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE |
| 461 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 462 | else if (prot & IOMMU_QCOM_SYS_CACHE) |
| 463 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE |
| 464 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (prot & IOMMU_NOEXEC) |
| 468 | pte |= ARM_LPAE_PTE_XN; |
| 469 | |
| 470 | return pte; |
| 471 | } |
| 472 | |
| 473 | static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, |
| 474 | phys_addr_t paddr, size_t size, int iommu_prot) |
| 475 | { |
| 476 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 477 | arm_lpae_iopte *ptep = data->pgd; |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 478 | int ret, lvl = ARM_LPAE_START_LVL(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 479 | arm_lpae_iopte prot; |
| 480 | |
| 481 | /* If no access, then nothing to do */ |
| 482 | if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) |
| 483 | return 0; |
| 484 | |
Robin Murphy | 7655739 | 2017-07-03 14:52:24 +0100 | [diff] [blame] | 485 | if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) || |
| 486 | paddr >= (1ULL << data->iop.cfg.oas))) |
| 487 | return -ERANGE; |
| 488 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 489 | prot = arm_lpae_prot_to_pte(data, iommu_prot); |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 490 | ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); |
| 491 | /* |
| 492 | * Synchronise all PTE updates for the new mapping before there's |
| 493 | * a chance for anything to kick off a table walk for the new iova. |
| 494 | */ |
| 495 | wmb(); |
| 496 | |
| 497 | return ret; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, |
| 501 | arm_lpae_iopte *ptep) |
| 502 | { |
| 503 | arm_lpae_iopte *start, *end; |
| 504 | unsigned long table_size; |
| 505 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 506 | if (lvl == ARM_LPAE_START_LVL(data)) |
| 507 | table_size = data->pgd_size; |
| 508 | else |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 509 | table_size = ARM_LPAE_GRANULE(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 510 | |
| 511 | start = ptep; |
Will Deacon | 12c2ab0 | 2015-12-15 16:08:12 +0000 | [diff] [blame] | 512 | |
| 513 | /* Only leaf entries at the last level */ |
| 514 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 515 | end = ptep; |
| 516 | else |
| 517 | end = (void *)ptep + table_size; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 518 | |
| 519 | while (ptep != end) { |
| 520 | arm_lpae_iopte pte = *ptep++; |
| 521 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 522 | if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 523 | continue; |
| 524 | |
| 525 | __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); |
| 526 | } |
| 527 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 528 | __arm_lpae_free_pages(start, table_size, &data->iop.cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static void arm_lpae_free_pgtable(struct io_pgtable *iop) |
| 532 | { |
| 533 | struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); |
| 534 | |
| 535 | __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); |
| 536 | kfree(data); |
| 537 | } |
| 538 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 539 | static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, |
| 540 | unsigned long iova, size_t size, |
| 541 | arm_lpae_iopte blk_pte, int lvl, |
| 542 | arm_lpae_iopte *ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 543 | { |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 544 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 545 | arm_lpae_iopte pte, *tablep; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 546 | phys_addr_t blk_paddr; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 547 | size_t tablesz = ARM_LPAE_GRANULE(data); |
| 548 | size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 549 | int i, unmap_idx = -1; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 550 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 551 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 552 | return 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 553 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 554 | tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); |
| 555 | if (!tablep) |
| 556 | return 0; /* Bytes unmapped */ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 557 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 558 | if (size == split_sz) |
| 559 | unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 560 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 561 | blk_paddr = iopte_to_paddr(blk_pte, data); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 562 | pte = iopte_prot(blk_pte); |
| 563 | |
| 564 | for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 565 | /* Unmap! */ |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 566 | if (i == unmap_idx) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 567 | continue; |
| 568 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 569 | __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 572 | pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); |
| 573 | if (pte != blk_pte) { |
| 574 | __arm_lpae_free_pages(tablep, tablesz, cfg); |
| 575 | /* |
| 576 | * We may race against someone unmapping another part of this |
| 577 | * block, but anything else is invalid. We can't misinterpret |
| 578 | * a page entry here since we're never at the last level. |
| 579 | */ |
| 580 | if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) |
| 581 | return 0; |
| 582 | |
| 583 | tablep = iopte_deref(pte, data); |
Robin Murphy | 85c7a0f | 2018-09-06 17:59:50 +0100 | [diff] [blame] | 584 | } else if (unmap_idx >= 0) { |
| 585 | io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true); |
| 586 | return size; |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 587 | } |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 588 | |
Robin Murphy | 85c7a0f | 2018-09-06 17:59:50 +0100 | [diff] [blame] | 589 | return __arm_lpae_unmap(data, iova, size, lvl, tablep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 592 | static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
| 593 | unsigned long iova, size_t size, int lvl, |
| 594 | arm_lpae_iopte *ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 595 | { |
| 596 | arm_lpae_iopte pte; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 597 | struct io_pgtable *iop = &data->iop; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 598 | |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 599 | /* Something went horribly wrong and we ran out of page table */ |
| 600 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 601 | return 0; |
| 602 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 603 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 604 | pte = READ_ONCE(*ptep); |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 605 | if (WARN_ON(!pte)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 606 | return 0; |
| 607 | |
| 608 | /* If the size matches this level, we're in the right place */ |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 609 | if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 610 | __arm_lpae_set_pte(ptep, 0, &iop->cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 611 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 612 | if (!iopte_leaf(pte, lvl, iop->fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 613 | /* Also flush any partial walks */ |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 614 | io_pgtable_tlb_add_flush(iop, iova, size, |
| 615 | ARM_LPAE_GRANULE(data), false); |
| 616 | io_pgtable_tlb_sync(iop); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 617 | ptep = iopte_deref(pte, data); |
| 618 | __arm_lpae_free_pgtable(data, lvl + 1, ptep); |
Zhen Lei | b6b65ca | 2018-09-20 17:10:24 +0100 | [diff] [blame] | 619 | } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { |
| 620 | /* |
| 621 | * Order the PTE update against queueing the IOVA, to |
| 622 | * guarantee that a flush callback from a different CPU |
| 623 | * has observed it before the TLBIALL can be issued. |
| 624 | */ |
| 625 | smp_wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 626 | } else { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 627 | io_pgtable_tlb_add_flush(iop, iova, size, size, true); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | return size; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 631 | } else if (iopte_leaf(pte, lvl, iop->fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 632 | /* |
| 633 | * Insert a table at the next level to map the old region, |
| 634 | * minus the part we want to unmap |
| 635 | */ |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 636 | return arm_lpae_split_blk_unmap(data, iova, size, pte, |
| 637 | lvl + 1, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /* Keep on walkin' */ |
| 641 | ptep = iopte_deref(pte, data); |
| 642 | return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep); |
| 643 | } |
| 644 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 645 | static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, |
| 646 | size_t size) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 647 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 648 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 649 | arm_lpae_iopte *ptep = data->pgd; |
| 650 | int lvl = ARM_LPAE_START_LVL(data); |
| 651 | |
Robin Murphy | 7655739 | 2017-07-03 14:52:24 +0100 | [diff] [blame] | 652 | if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias))) |
| 653 | return 0; |
| 654 | |
Robin Murphy | 32b1244 | 2017-09-28 15:55:01 +0100 | [diff] [blame] | 655 | return __arm_lpae_unmap(data, iova, size, lvl, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, |
| 659 | unsigned long iova) |
| 660 | { |
| 661 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 662 | arm_lpae_iopte pte, *ptep = data->pgd; |
| 663 | int lvl = ARM_LPAE_START_LVL(data); |
| 664 | |
| 665 | do { |
| 666 | /* Valid IOPTE pointer? */ |
| 667 | if (!ptep) |
| 668 | return 0; |
| 669 | |
| 670 | /* Grab the IOPTE we're interested in */ |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 671 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 672 | pte = READ_ONCE(*ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 673 | |
| 674 | /* Valid entry? */ |
| 675 | if (!pte) |
| 676 | return 0; |
| 677 | |
| 678 | /* Leaf entry? */ |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 679 | if (iopte_leaf(pte, lvl, data->iop.fmt)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 680 | goto found_translation; |
| 681 | |
| 682 | /* Take it to the next level */ |
| 683 | ptep = iopte_deref(pte, data); |
| 684 | } while (++lvl < ARM_LPAE_MAX_LEVELS); |
| 685 | |
| 686 | /* Ran out of page tables to walk */ |
| 687 | return 0; |
| 688 | |
| 689 | found_translation: |
Will Deacon | 7c6d90e | 2016-06-16 18:21:19 +0100 | [diff] [blame] | 690 | iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 691 | return iopte_to_paddr(pte, data) | iova; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) |
| 695 | { |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 696 | unsigned long granule, page_sizes; |
| 697 | unsigned int max_addr_bits = 48; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 698 | |
| 699 | /* |
| 700 | * We need to restrict the supported page sizes to match the |
| 701 | * translation regime for a particular granule. Aim to match |
| 702 | * the CPU page size if possible, otherwise prefer smaller sizes. |
| 703 | * While we're at it, restrict the block sizes to match the |
| 704 | * chosen granule. |
| 705 | */ |
| 706 | if (cfg->pgsize_bitmap & PAGE_SIZE) |
| 707 | granule = PAGE_SIZE; |
| 708 | else if (cfg->pgsize_bitmap & ~PAGE_MASK) |
| 709 | granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); |
| 710 | else if (cfg->pgsize_bitmap & PAGE_MASK) |
| 711 | granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); |
| 712 | else |
| 713 | granule = 0; |
| 714 | |
| 715 | switch (granule) { |
| 716 | case SZ_4K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 717 | page_sizes = (SZ_4K | SZ_2M | SZ_1G); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 718 | break; |
| 719 | case SZ_16K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 720 | page_sizes = (SZ_16K | SZ_32M); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 721 | break; |
| 722 | case SZ_64K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 723 | max_addr_bits = 52; |
| 724 | page_sizes = (SZ_64K | SZ_512M); |
| 725 | if (cfg->oas > 48) |
| 726 | page_sizes |= 1ULL << 42; /* 4TB */ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 727 | break; |
| 728 | default: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 729 | page_sizes = 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 730 | } |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 731 | |
| 732 | cfg->pgsize_bitmap &= page_sizes; |
| 733 | cfg->ias = min(cfg->ias, max_addr_bits); |
| 734 | cfg->oas = min(cfg->oas, max_addr_bits); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static struct arm_lpae_io_pgtable * |
| 738 | arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) |
| 739 | { |
| 740 | unsigned long va_bits, pgd_bits; |
| 741 | struct arm_lpae_io_pgtable *data; |
| 742 | |
| 743 | arm_lpae_restrict_pgsizes(cfg); |
| 744 | |
| 745 | if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) |
| 746 | return NULL; |
| 747 | |
| 748 | if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) |
| 749 | return NULL; |
| 750 | |
| 751 | if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) |
| 752 | return NULL; |
| 753 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 754 | if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { |
| 755 | dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); |
| 756 | return NULL; |
| 757 | } |
| 758 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 759 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 760 | if (!data) |
| 761 | return NULL; |
| 762 | |
| 763 | data->pg_shift = __ffs(cfg->pgsize_bitmap); |
| 764 | data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); |
| 765 | |
| 766 | va_bits = cfg->ias - data->pg_shift; |
| 767 | data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); |
| 768 | |
| 769 | /* Calculate the actual size of our pgd (without concatenation) */ |
| 770 | pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); |
| 771 | data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); |
| 772 | |
| 773 | data->iop.ops = (struct io_pgtable_ops) { |
| 774 | .map = arm_lpae_map, |
| 775 | .unmap = arm_lpae_unmap, |
| 776 | .iova_to_phys = arm_lpae_iova_to_phys, |
| 777 | }; |
| 778 | |
| 779 | return data; |
| 780 | } |
| 781 | |
| 782 | static struct io_pgtable * |
| 783 | arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 784 | { |
| 785 | u64 reg; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 786 | struct arm_lpae_io_pgtable *data; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 787 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 788 | if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | |
Zhen Lei | b6b65ca | 2018-09-20 17:10:24 +0100 | [diff] [blame] | 789 | IO_PGTABLE_QUIRK_NON_STRICT)) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 790 | return NULL; |
| 791 | |
| 792 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 793 | if (!data) |
| 794 | return NULL; |
| 795 | |
| 796 | /* TCR */ |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 797 | if (cfg->coherent_walk) { |
| 798 | reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 799 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 800 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 801 | } else { |
| 802 | reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 803 | (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 804 | (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 805 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 806 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 807 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 808 | case SZ_4K: |
| 809 | reg |= ARM_LPAE_TCR_TG0_4K; |
| 810 | break; |
| 811 | case SZ_16K: |
| 812 | reg |= ARM_LPAE_TCR_TG0_16K; |
| 813 | break; |
| 814 | case SZ_64K: |
| 815 | reg |= ARM_LPAE_TCR_TG0_64K; |
| 816 | break; |
| 817 | } |
| 818 | |
| 819 | switch (cfg->oas) { |
| 820 | case 32: |
| 821 | reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 822 | break; |
| 823 | case 36: |
| 824 | reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 825 | break; |
| 826 | case 40: |
| 827 | reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 828 | break; |
| 829 | case 42: |
| 830 | reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 831 | break; |
| 832 | case 44: |
| 833 | reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 834 | break; |
| 835 | case 48: |
| 836 | reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 837 | break; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 838 | case 52: |
| 839 | reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 840 | break; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 841 | default: |
| 842 | goto out_free_data; |
| 843 | } |
| 844 | |
| 845 | reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; |
Will Deacon | 63979b8 | 2015-03-18 10:22:18 +0000 | [diff] [blame] | 846 | |
| 847 | /* Disable speculative walks through TTBR1 */ |
| 848 | reg |= ARM_LPAE_TCR_EPD1; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 849 | cfg->arm_lpae_s1_cfg.tcr = reg; |
| 850 | |
| 851 | /* MAIRs */ |
| 852 | reg = (ARM_LPAE_MAIR_ATTR_NC |
| 853 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | |
| 854 | (ARM_LPAE_MAIR_ATTR_WBRWA |
| 855 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | |
| 856 | (ARM_LPAE_MAIR_ATTR_DEVICE |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 857 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | |
| 858 | (ARM_LPAE_MAIR_ATTR_INC_OWBRWA |
| 859 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 860 | |
| 861 | cfg->arm_lpae_s1_cfg.mair[0] = reg; |
| 862 | cfg->arm_lpae_s1_cfg.mair[1] = 0; |
| 863 | |
| 864 | /* Looking good; allocate a pgd */ |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 865 | data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 866 | if (!data->pgd) |
| 867 | goto out_free_data; |
| 868 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 869 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 870 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 871 | |
| 872 | /* TTBRs */ |
| 873 | cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); |
| 874 | cfg->arm_lpae_s1_cfg.ttbr[1] = 0; |
| 875 | return &data->iop; |
| 876 | |
| 877 | out_free_data: |
| 878 | kfree(data); |
| 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | static struct io_pgtable * |
| 883 | arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 884 | { |
| 885 | u64 reg, sl; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 886 | struct arm_lpae_io_pgtable *data; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 887 | |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 888 | /* The NS quirk doesn't apply at stage 2 */ |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 889 | if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 890 | return NULL; |
| 891 | |
| 892 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 893 | if (!data) |
| 894 | return NULL; |
| 895 | |
| 896 | /* |
| 897 | * Concatenate PGDs at level 1 if possible in order to reduce |
| 898 | * the depth of the stage-2 walk. |
| 899 | */ |
| 900 | if (data->levels == ARM_LPAE_MAX_LEVELS) { |
| 901 | unsigned long pgd_pages; |
| 902 | |
| 903 | pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); |
| 904 | if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { |
| 905 | data->pgd_size = pgd_pages << data->pg_shift; |
| 906 | data->levels--; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /* VTCR */ |
| 911 | reg = ARM_64_LPAE_S2_TCR_RES1 | |
| 912 | (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 913 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 914 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 915 | |
| 916 | sl = ARM_LPAE_START_LVL(data); |
| 917 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 918 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 919 | case SZ_4K: |
| 920 | reg |= ARM_LPAE_TCR_TG0_4K; |
| 921 | sl++; /* SL0 format is different for 4K granule size */ |
| 922 | break; |
| 923 | case SZ_16K: |
| 924 | reg |= ARM_LPAE_TCR_TG0_16K; |
| 925 | break; |
| 926 | case SZ_64K: |
| 927 | reg |= ARM_LPAE_TCR_TG0_64K; |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | switch (cfg->oas) { |
| 932 | case 32: |
| 933 | reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 934 | break; |
| 935 | case 36: |
| 936 | reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 937 | break; |
| 938 | case 40: |
| 939 | reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 940 | break; |
| 941 | case 42: |
| 942 | reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 943 | break; |
| 944 | case 44: |
| 945 | reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 946 | break; |
| 947 | case 48: |
| 948 | reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 949 | break; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 950 | case 52: |
| 951 | reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 952 | break; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 953 | default: |
| 954 | goto out_free_data; |
| 955 | } |
| 956 | |
| 957 | reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; |
| 958 | reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; |
| 959 | cfg->arm_lpae_s2_cfg.vtcr = reg; |
| 960 | |
| 961 | /* Allocate pgd pages */ |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 962 | data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 963 | if (!data->pgd) |
| 964 | goto out_free_data; |
| 965 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 966 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 967 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 968 | |
| 969 | /* VTTBR */ |
| 970 | cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); |
| 971 | return &data->iop; |
| 972 | |
| 973 | out_free_data: |
| 974 | kfree(data); |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | static struct io_pgtable * |
| 979 | arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 980 | { |
| 981 | struct io_pgtable *iop; |
| 982 | |
| 983 | if (cfg->ias > 32 || cfg->oas > 40) |
| 984 | return NULL; |
| 985 | |
| 986 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 987 | iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); |
| 988 | if (iop) { |
| 989 | cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; |
| 990 | cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; |
| 991 | } |
| 992 | |
| 993 | return iop; |
| 994 | } |
| 995 | |
| 996 | static struct io_pgtable * |
| 997 | arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 998 | { |
| 999 | struct io_pgtable *iop; |
| 1000 | |
| 1001 | if (cfg->ias > 40 || cfg->oas > 40) |
| 1002 | return NULL; |
| 1003 | |
| 1004 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 1005 | iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); |
| 1006 | if (iop) |
| 1007 | cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; |
| 1008 | |
| 1009 | return iop; |
| 1010 | } |
| 1011 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1012 | static struct io_pgtable * |
| 1013 | arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) |
| 1014 | { |
| 1015 | struct io_pgtable *iop; |
| 1016 | |
| 1017 | if (cfg->ias != 48 || cfg->oas > 40) |
| 1018 | return NULL; |
| 1019 | |
| 1020 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 1021 | iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); |
| 1022 | if (iop) { |
| 1023 | u64 mair, ttbr; |
| 1024 | |
| 1025 | /* Copy values as union fields overlap */ |
| 1026 | mair = cfg->arm_lpae_s1_cfg.mair[0]; |
| 1027 | ttbr = cfg->arm_lpae_s1_cfg.ttbr[0]; |
| 1028 | |
| 1029 | cfg->arm_mali_lpae_cfg.memattr = mair; |
| 1030 | cfg->arm_mali_lpae_cfg.transtab = ttbr | |
| 1031 | ARM_MALI_LPAE_TTBR_READ_INNER | |
| 1032 | ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; |
| 1033 | } |
| 1034 | |
| 1035 | return iop; |
| 1036 | } |
| 1037 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1038 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { |
| 1039 | .alloc = arm_64_lpae_alloc_pgtable_s1, |
| 1040 | .free = arm_lpae_free_pgtable, |
| 1041 | }; |
| 1042 | |
| 1043 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { |
| 1044 | .alloc = arm_64_lpae_alloc_pgtable_s2, |
| 1045 | .free = arm_lpae_free_pgtable, |
| 1046 | }; |
| 1047 | |
| 1048 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { |
| 1049 | .alloc = arm_32_lpae_alloc_pgtable_s1, |
| 1050 | .free = arm_lpae_free_pgtable, |
| 1051 | }; |
| 1052 | |
| 1053 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { |
| 1054 | .alloc = arm_32_lpae_alloc_pgtable_s2, |
| 1055 | .free = arm_lpae_free_pgtable, |
| 1056 | }; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1057 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1058 | struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { |
| 1059 | .alloc = arm_mali_lpae_alloc_pgtable, |
| 1060 | .free = arm_lpae_free_pgtable, |
| 1061 | }; |
| 1062 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1063 | #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST |
| 1064 | |
| 1065 | static struct io_pgtable_cfg *cfg_cookie; |
| 1066 | |
| 1067 | static void dummy_tlb_flush_all(void *cookie) |
| 1068 | { |
| 1069 | WARN_ON(cookie != cfg_cookie); |
| 1070 | } |
| 1071 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 1072 | static void dummy_tlb_add_flush(unsigned long iova, size_t size, |
| 1073 | size_t granule, bool leaf, void *cookie) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1074 | { |
| 1075 | WARN_ON(cookie != cfg_cookie); |
| 1076 | WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); |
| 1077 | } |
| 1078 | |
| 1079 | static void dummy_tlb_sync(void *cookie) |
| 1080 | { |
| 1081 | WARN_ON(cookie != cfg_cookie); |
| 1082 | } |
| 1083 | |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame^] | 1084 | static const struct iommu_flush_ops dummy_tlb_ops __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1085 | .tlb_flush_all = dummy_tlb_flush_all, |
| 1086 | .tlb_add_flush = dummy_tlb_add_flush, |
| 1087 | .tlb_sync = dummy_tlb_sync, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1088 | }; |
| 1089 | |
| 1090 | static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) |
| 1091 | { |
| 1092 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 1093 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 1094 | |
| 1095 | pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", |
| 1096 | cfg->pgsize_bitmap, cfg->ias); |
| 1097 | pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", |
| 1098 | data->levels, data->pgd_size, data->pg_shift, |
| 1099 | data->bits_per_level, data->pgd); |
| 1100 | } |
| 1101 | |
| 1102 | #define __FAIL(ops, i) ({ \ |
| 1103 | WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ |
| 1104 | arm_lpae_dump_ops(ops); \ |
| 1105 | selftest_running = false; \ |
| 1106 | -EFAULT; \ |
| 1107 | }) |
| 1108 | |
| 1109 | static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) |
| 1110 | { |
| 1111 | static const enum io_pgtable_fmt fmts[] = { |
| 1112 | ARM_64_LPAE_S1, |
| 1113 | ARM_64_LPAE_S2, |
| 1114 | }; |
| 1115 | |
| 1116 | int i, j; |
| 1117 | unsigned long iova; |
| 1118 | size_t size; |
| 1119 | struct io_pgtable_ops *ops; |
| 1120 | |
| 1121 | selftest_running = true; |
| 1122 | |
| 1123 | for (i = 0; i < ARRAY_SIZE(fmts); ++i) { |
| 1124 | cfg_cookie = cfg; |
| 1125 | ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); |
| 1126 | if (!ops) { |
| 1127 | pr_err("selftest: failed to allocate io pgtable ops\n"); |
| 1128 | return -ENOMEM; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Initial sanity checks. |
| 1133 | * Empty page tables shouldn't provide any translations. |
| 1134 | */ |
| 1135 | if (ops->iova_to_phys(ops, 42)) |
| 1136 | return __FAIL(ops, i); |
| 1137 | |
| 1138 | if (ops->iova_to_phys(ops, SZ_1G + 42)) |
| 1139 | return __FAIL(ops, i); |
| 1140 | |
| 1141 | if (ops->iova_to_phys(ops, SZ_2G + 42)) |
| 1142 | return __FAIL(ops, i); |
| 1143 | |
| 1144 | /* |
| 1145 | * Distinct mappings of different granule sizes. |
| 1146 | */ |
| 1147 | iova = 0; |
Kefeng Wang | 4ae8a5c | 2016-09-21 13:41:31 +0800 | [diff] [blame] | 1148 | for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1149 | size = 1UL << j; |
| 1150 | |
| 1151 | if (ops->map(ops, iova, iova, size, IOMMU_READ | |
| 1152 | IOMMU_WRITE | |
| 1153 | IOMMU_NOEXEC | |
| 1154 | IOMMU_CACHE)) |
| 1155 | return __FAIL(ops, i); |
| 1156 | |
| 1157 | /* Overlapping mappings */ |
| 1158 | if (!ops->map(ops, iova, iova + size, size, |
| 1159 | IOMMU_READ | IOMMU_NOEXEC)) |
| 1160 | return __FAIL(ops, i); |
| 1161 | |
| 1162 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1163 | return __FAIL(ops, i); |
| 1164 | |
| 1165 | iova += SZ_1G; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | /* Partial unmap */ |
| 1169 | size = 1UL << __ffs(cfg->pgsize_bitmap); |
| 1170 | if (ops->unmap(ops, SZ_1G + size, size) != size) |
| 1171 | return __FAIL(ops, i); |
| 1172 | |
| 1173 | /* Remap of partial unmap */ |
| 1174 | if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) |
| 1175 | return __FAIL(ops, i); |
| 1176 | |
| 1177 | if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) |
| 1178 | return __FAIL(ops, i); |
| 1179 | |
| 1180 | /* Full unmap */ |
| 1181 | iova = 0; |
YueHaibing | f793b13 | 2018-04-26 12:49:29 +0800 | [diff] [blame] | 1182 | for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1183 | size = 1UL << j; |
| 1184 | |
| 1185 | if (ops->unmap(ops, iova, size) != size) |
| 1186 | return __FAIL(ops, i); |
| 1187 | |
| 1188 | if (ops->iova_to_phys(ops, iova + 42)) |
| 1189 | return __FAIL(ops, i); |
| 1190 | |
| 1191 | /* Remap full block */ |
| 1192 | if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) |
| 1193 | return __FAIL(ops, i); |
| 1194 | |
| 1195 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1196 | return __FAIL(ops, i); |
| 1197 | |
| 1198 | iova += SZ_1G; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | free_io_pgtable_ops(ops); |
| 1202 | } |
| 1203 | |
| 1204 | selftest_running = false; |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | static int __init arm_lpae_do_selftests(void) |
| 1209 | { |
| 1210 | static const unsigned long pgsize[] = { |
| 1211 | SZ_4K | SZ_2M | SZ_1G, |
| 1212 | SZ_16K | SZ_32M, |
| 1213 | SZ_64K | SZ_512M, |
| 1214 | }; |
| 1215 | |
| 1216 | static const unsigned int ias[] = { |
| 1217 | 32, 36, 40, 42, 44, 48, |
| 1218 | }; |
| 1219 | |
| 1220 | int i, j, pass = 0, fail = 0; |
| 1221 | struct io_pgtable_cfg cfg = { |
| 1222 | .tlb = &dummy_tlb_ops, |
| 1223 | .oas = 48, |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 1224 | .coherent_walk = true, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1225 | }; |
| 1226 | |
| 1227 | for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { |
| 1228 | for (j = 0; j < ARRAY_SIZE(ias); ++j) { |
| 1229 | cfg.pgsize_bitmap = pgsize[i]; |
| 1230 | cfg.ias = ias[j]; |
| 1231 | pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", |
| 1232 | pgsize[i], ias[j]); |
| 1233 | if (arm_lpae_run_tests(&cfg)) |
| 1234 | fail++; |
| 1235 | else |
| 1236 | pass++; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); |
| 1241 | return fail ? -EFAULT : 0; |
| 1242 | } |
| 1243 | subsys_initcall(arm_lpae_do_selftests); |
| 1244 | #endif |