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