Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CPU-agnostic ARM page table allocator. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | * |
| 16 | * Copyright (C) 2014 ARM Limited |
| 17 | * |
| 18 | * Author: Will Deacon <will.deacon@arm.com> |
| 19 | */ |
| 20 | |
| 21 | #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt |
| 22 | |
| 23 | #include <linux/iommu.h> |
| 24 | #include <linux/kernel.h> |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 25 | #include <linux/scatterlist.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 26 | #include <linux/sizes.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/types.h> |
Lada Trimasova | 8f6aff9 | 2016-01-27 11:10:32 +0000 | [diff] [blame] | 29 | #include <linux/dma-mapping.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 30 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 31 | #include <asm/barrier.h> |
| 32 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 33 | #include "io-pgtable.h" |
| 34 | |
| 35 | #define ARM_LPAE_MAX_ADDR_BITS 48 |
| 36 | #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 |
| 37 | #define ARM_LPAE_MAX_LEVELS 4 |
| 38 | |
| 39 | /* Struct accessors */ |
| 40 | #define io_pgtable_to_data(x) \ |
| 41 | container_of((x), struct arm_lpae_io_pgtable, iop) |
| 42 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 43 | #define io_pgtable_ops_to_data(x) \ |
| 44 | io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) |
| 45 | |
| 46 | /* |
| 47 | * For consistency with the architecture, we always consider |
| 48 | * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 |
| 49 | */ |
| 50 | #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) |
| 51 | |
| 52 | /* |
| 53 | * Calculate the right shift amount to get to the portion describing level l |
| 54 | * in a virtual address mapped by the pagetable in d. |
| 55 | */ |
| 56 | #define ARM_LPAE_LVL_SHIFT(l,d) \ |
| 57 | ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ |
| 58 | * (d)->bits_per_level) + (d)->pg_shift) |
| 59 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 60 | #define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift) |
| 61 | |
Will Deacon | 367bd97 | 2015-02-16 18:38:20 +0000 | [diff] [blame] | 62 | #define ARM_LPAE_PAGES_PER_PGD(d) \ |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 63 | DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Calculate the index at level l used to map virtual address a using the |
| 67 | * pagetable in d. |
| 68 | */ |
| 69 | #define ARM_LPAE_PGD_IDX(l,d) \ |
| 70 | ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) |
| 71 | |
| 72 | #define ARM_LPAE_LVL_IDX(a,l,d) \ |
Will Deacon | 367bd97 | 2015-02-16 18:38:20 +0000 | [diff] [blame] | 73 | (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 74 | ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) |
| 75 | |
| 76 | /* Calculate the block/page mapping size at level l for pagetable in d. */ |
| 77 | #define ARM_LPAE_BLOCK_SIZE(l,d) \ |
| 78 | (1 << (ilog2(sizeof(arm_lpae_iopte)) + \ |
| 79 | ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) |
| 80 | |
| 81 | /* Page table bits */ |
| 82 | #define ARM_LPAE_PTE_TYPE_SHIFT 0 |
| 83 | #define ARM_LPAE_PTE_TYPE_MASK 0x3 |
| 84 | |
| 85 | #define ARM_LPAE_PTE_TYPE_BLOCK 1 |
| 86 | #define ARM_LPAE_PTE_TYPE_TABLE 3 |
| 87 | #define ARM_LPAE_PTE_TYPE_PAGE 3 |
| 88 | |
Laurent Pinchart | c896c13 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 89 | #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 90 | #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) |
| 91 | #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) |
| 92 | #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) |
| 93 | #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) |
| 94 | #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) |
Laurent Pinchart | c896c13 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 95 | #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 96 | #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) |
| 97 | |
| 98 | #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) |
| 99 | /* Ignore the contiguous bit for block splitting */ |
| 100 | #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) |
| 101 | #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ |
| 102 | ARM_LPAE_PTE_ATTR_HI_MASK) |
| 103 | |
| 104 | /* Stage-1 PTE */ |
Jeremy Gebben | f96739f | 2015-09-16 14:04:42 -0600 | [diff] [blame] | 105 | #define ARM_LPAE_PTE_AP_PRIV_RW (((arm_lpae_iopte)0) << 6) |
| 106 | #define ARM_LPAE_PTE_AP_RW (((arm_lpae_iopte)1) << 6) |
| 107 | #define ARM_LPAE_PTE_AP_PRIV_RO (((arm_lpae_iopte)2) << 6) |
| 108 | #define ARM_LPAE_PTE_AP_RO (((arm_lpae_iopte)3) << 6) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 109 | #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 |
| 110 | #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) |
| 111 | |
| 112 | /* Stage-2 PTE */ |
| 113 | #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) |
| 114 | #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) |
| 115 | #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) |
| 116 | #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) |
| 117 | #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) |
| 118 | #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) |
| 119 | |
| 120 | /* Register bits */ |
| 121 | #define ARM_32_LPAE_TCR_EAE (1 << 31) |
| 122 | #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) |
| 123 | |
Will Deacon | 63979b8 | 2015-03-18 10:22:18 +0000 | [diff] [blame] | 124 | #define ARM_LPAE_TCR_EPD1 (1 << 23) |
| 125 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 126 | #define ARM_LPAE_TCR_TG0_4K (0 << 14) |
| 127 | #define ARM_LPAE_TCR_TG0_64K (1 << 14) |
| 128 | #define ARM_LPAE_TCR_TG0_16K (2 << 14) |
| 129 | |
| 130 | #define ARM_LPAE_TCR_SH0_SHIFT 12 |
| 131 | #define ARM_LPAE_TCR_SH0_MASK 0x3 |
| 132 | #define ARM_LPAE_TCR_SH_NS 0 |
| 133 | #define ARM_LPAE_TCR_SH_OS 2 |
| 134 | #define ARM_LPAE_TCR_SH_IS 3 |
| 135 | |
| 136 | #define ARM_LPAE_TCR_ORGN0_SHIFT 10 |
| 137 | #define ARM_LPAE_TCR_IRGN0_SHIFT 8 |
| 138 | #define ARM_LPAE_TCR_RGN_MASK 0x3 |
| 139 | #define ARM_LPAE_TCR_RGN_NC 0 |
| 140 | #define ARM_LPAE_TCR_RGN_WBWA 1 |
| 141 | #define ARM_LPAE_TCR_RGN_WT 2 |
| 142 | #define ARM_LPAE_TCR_RGN_WB 3 |
| 143 | |
| 144 | #define ARM_LPAE_TCR_SL0_SHIFT 6 |
| 145 | #define ARM_LPAE_TCR_SL0_MASK 0x3 |
| 146 | |
| 147 | #define ARM_LPAE_TCR_T0SZ_SHIFT 0 |
| 148 | #define ARM_LPAE_TCR_SZ_MASK 0xf |
| 149 | |
| 150 | #define ARM_LPAE_TCR_PS_SHIFT 16 |
| 151 | #define ARM_LPAE_TCR_PS_MASK 0x7 |
| 152 | |
| 153 | #define ARM_LPAE_TCR_IPS_SHIFT 32 |
| 154 | #define ARM_LPAE_TCR_IPS_MASK 0x7 |
| 155 | |
| 156 | #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL |
| 157 | #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL |
| 158 | #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL |
| 159 | #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL |
| 160 | #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL |
| 161 | #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL |
| 162 | |
| 163 | #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) |
| 164 | #define ARM_LPAE_MAIR_ATTR_MASK 0xff |
| 165 | #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 |
| 166 | #define ARM_LPAE_MAIR_ATTR_NC 0x44 |
| 167 | #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff |
| 168 | #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 |
| 169 | #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 |
| 170 | #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 |
| 171 | |
| 172 | /* IOPTE accessors */ |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 173 | #define iopte_deref(pte, d) \ |
| 174 | (__va(iopte_val(pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \ |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 175 | & ~(ARM_LPAE_GRANULE(d) - 1ULL))) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 176 | |
| 177 | #define iopte_type(pte,l) \ |
| 178 | (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) |
| 179 | |
| 180 | #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) |
| 181 | |
| 182 | #define iopte_leaf(pte,l) \ |
| 183 | (l == (ARM_LPAE_MAX_LEVELS - 1) ? \ |
| 184 | (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \ |
| 185 | (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK)) |
| 186 | |
| 187 | #define iopte_to_pfn(pte,d) \ |
| 188 | (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift) |
| 189 | |
| 190 | #define pfn_to_iopte(pfn,d) \ |
| 191 | (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) |
| 192 | |
| 193 | struct arm_lpae_io_pgtable { |
| 194 | struct io_pgtable iop; |
| 195 | |
| 196 | int levels; |
| 197 | size_t pgd_size; |
| 198 | unsigned long pg_shift; |
| 199 | unsigned long bits_per_level; |
| 200 | |
| 201 | void *pgd; |
| 202 | }; |
| 203 | |
| 204 | typedef u64 arm_lpae_iopte; |
| 205 | |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 206 | /* |
| 207 | * We'll use some ignored bits in table entries to keep track of the number |
| 208 | * of page mappings beneath the table. The maximum number of entries |
| 209 | * beneath any table mapping in armv8 is 8192 (which is possible at the |
| 210 | * 2nd- and 3rd-level when using a 64K granule size). The bits at our |
| 211 | * disposal are: |
| 212 | * |
| 213 | * 4k granule: [58..52], [11..2] |
| 214 | * 64k granule: [58..52], [15..2] |
| 215 | * |
| 216 | * [58..52], [11..2] is enough bits for tracking table mappings at any |
| 217 | * level for any granule, so we'll use those. |
| 218 | */ |
| 219 | #define BOTTOM_IGNORED_MASK 0x3ff |
| 220 | #define BOTTOM_IGNORED_SHIFT 2 |
| 221 | #define BOTTOM_IGNORED_NUM_BITS 10 |
| 222 | #define TOP_IGNORED_MASK 0x7fULL |
| 223 | #define TOP_IGNORED_SHIFT 52 |
| 224 | #define IOPTE_RESERVED_MASK ((BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT) | \ |
| 225 | (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT)) |
| 226 | |
| 227 | static arm_lpae_iopte iopte_val(arm_lpae_iopte table_pte) |
| 228 | { |
| 229 | return table_pte & ~IOPTE_RESERVED_MASK; |
| 230 | } |
| 231 | |
| 232 | static arm_lpae_iopte _iopte_bottom_ignored_val(arm_lpae_iopte table_pte) |
| 233 | { |
| 234 | return (table_pte & (BOTTOM_IGNORED_MASK << BOTTOM_IGNORED_SHIFT)) |
| 235 | >> BOTTOM_IGNORED_SHIFT; |
| 236 | } |
| 237 | |
| 238 | static arm_lpae_iopte _iopte_top_ignored_val(arm_lpae_iopte table_pte) |
| 239 | { |
| 240 | return (table_pte & (TOP_IGNORED_MASK << TOP_IGNORED_SHIFT)) |
| 241 | >> TOP_IGNORED_SHIFT; |
| 242 | } |
| 243 | |
| 244 | static int iopte_tblcnt(arm_lpae_iopte table_pte) |
| 245 | { |
| 246 | return (_iopte_bottom_ignored_val(table_pte) | |
| 247 | (_iopte_top_ignored_val(table_pte) << BOTTOM_IGNORED_NUM_BITS)); |
| 248 | } |
| 249 | |
| 250 | static void iopte_tblcnt_set(arm_lpae_iopte *table_pte, int val) |
| 251 | { |
| 252 | arm_lpae_iopte pte = iopte_val(*table_pte); |
| 253 | |
| 254 | pte |= ((val & BOTTOM_IGNORED_MASK) << BOTTOM_IGNORED_SHIFT) | |
| 255 | (((val & (TOP_IGNORED_MASK << BOTTOM_IGNORED_NUM_BITS)) |
| 256 | >> BOTTOM_IGNORED_NUM_BITS) << TOP_IGNORED_SHIFT); |
| 257 | *table_pte = pte; |
| 258 | } |
| 259 | |
| 260 | static void iopte_tblcnt_sub(arm_lpae_iopte *table_ptep, int cnt) |
| 261 | { |
| 262 | arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep); |
| 263 | |
| 264 | current_cnt -= cnt; |
| 265 | iopte_tblcnt_set(table_ptep, current_cnt); |
| 266 | } |
| 267 | |
| 268 | static void iopte_tblcnt_add(arm_lpae_iopte *table_ptep, int cnt) |
| 269 | { |
| 270 | arm_lpae_iopte current_cnt = iopte_tblcnt(*table_ptep); |
| 271 | |
| 272 | current_cnt += cnt; |
| 273 | iopte_tblcnt_set(table_ptep, current_cnt); |
| 274 | } |
| 275 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 276 | static bool selftest_running = false; |
| 277 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 278 | static dma_addr_t __arm_lpae_dma_addr(void *pages) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 279 | { |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 280 | return (dma_addr_t)virt_to_phys(pages); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, |
| 284 | struct io_pgtable_cfg *cfg) |
| 285 | { |
| 286 | struct device *dev = cfg->iommu_dev; |
| 287 | dma_addr_t dma; |
| 288 | void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO); |
| 289 | |
| 290 | if (!pages) |
| 291 | return NULL; |
| 292 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 293 | if (!selftest_running) { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 294 | dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); |
| 295 | if (dma_mapping_error(dev, dma)) |
| 296 | goto out_free; |
| 297 | /* |
| 298 | * We depend on the IOMMU being able to work with any physical |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 299 | * address directly, so if the DMA layer suggests otherwise by |
| 300 | * translating or truncating them, that bodes very badly... |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 301 | */ |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 302 | if (dma != virt_to_phys(pages)) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 303 | goto out_unmap; |
| 304 | } |
| 305 | |
| 306 | return pages; |
| 307 | |
| 308 | out_unmap: |
| 309 | dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); |
| 310 | dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); |
| 311 | out_free: |
| 312 | free_pages_exact(pages, size); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | static void __arm_lpae_free_pages(void *pages, size_t size, |
| 317 | struct io_pgtable_cfg *cfg) |
| 318 | { |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 319 | if (!selftest_running) |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 320 | dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 321 | size, DMA_TO_DEVICE); |
| 322 | free_pages_exact(pages, size); |
| 323 | } |
| 324 | |
| 325 | 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] | 326 | struct io_pgtable_cfg *cfg) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 327 | { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 328 | *ptep = pte; |
| 329 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 330 | if (!selftest_running) |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 331 | dma_sync_single_for_device(cfg->iommu_dev, |
| 332 | __arm_lpae_dma_addr(ptep), |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 333 | sizeof(pte), DMA_TO_DEVICE); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 336 | static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 337 | unsigned long iova, phys_addr_t paddr, |
| 338 | arm_lpae_iopte prot, int lvl, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 339 | arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 340 | { |
| 341 | arm_lpae_iopte pte = prot; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 342 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 343 | |
Patrick Daly | 67ba8eb | 2016-06-27 18:44:42 -0700 | [diff] [blame] | 344 | /* We require an unmap first */ |
Mitchel Humpherys | 1b0313e | 2015-09-23 13:56:27 -0700 | [diff] [blame] | 345 | if (*ptep & ARM_LPAE_PTE_VALID) { |
| 346 | BUG_ON(!selftest_running); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 347 | return -EEXIST; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 348 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 349 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 350 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) |
Laurent Pinchart | c896c13 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 351 | pte |= ARM_LPAE_PTE_NS; |
| 352 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 353 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 354 | pte |= ARM_LPAE_PTE_TYPE_PAGE; |
| 355 | else |
| 356 | pte |= ARM_LPAE_PTE_TYPE_BLOCK; |
| 357 | |
| 358 | pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; |
| 359 | pte |= pfn_to_iopte(paddr >> data->pg_shift, data); |
| 360 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 361 | __arm_lpae_set_pte(ptep, pte, cfg); |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 362 | |
| 363 | if (prev_ptep) |
| 364 | iopte_tblcnt_add(prev_ptep, 1); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, |
| 369 | phys_addr_t paddr, size_t size, arm_lpae_iopte prot, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 370 | int lvl, arm_lpae_iopte *ptep, |
| 371 | arm_lpae_iopte *prev_ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 372 | { |
| 373 | arm_lpae_iopte *cptep, pte; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 374 | size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 375 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 376 | |
| 377 | /* Find our entry at the current level */ |
| 378 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 379 | |
| 380 | /* 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] | 381 | if (size == block_size && (size & cfg->pgsize_bitmap)) |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 382 | return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep, |
| 383 | prev_ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 384 | |
| 385 | /* We can't allocate tables at the final level */ |
| 386 | if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | /* Grab a pointer to the next level */ |
| 390 | pte = *ptep; |
| 391 | if (!pte) { |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 392 | cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data), |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 393 | GFP_ATOMIC, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 394 | if (!cptep) |
| 395 | return -ENOMEM; |
| 396 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 397 | pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 398 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) |
Laurent Pinchart | c896c13 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 399 | pte |= ARM_LPAE_PTE_NSTABLE; |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 400 | __arm_lpae_set_pte(ptep, pte, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 401 | } else { |
| 402 | cptep = iopte_deref(pte, data); |
| 403 | } |
| 404 | |
| 405 | /* Rinse, repeat */ |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 406 | return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep, |
| 407 | ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, |
| 411 | int prot) |
| 412 | { |
| 413 | arm_lpae_iopte pte; |
| 414 | |
| 415 | if (data->iop.fmt == ARM_64_LPAE_S1 || |
| 416 | data->iop.fmt == ARM_32_LPAE_S1) { |
Jeremy Gebben | f96739f | 2015-09-16 14:04:42 -0600 | [diff] [blame] | 417 | pte = ARM_LPAE_PTE_nG; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 418 | |
Jeremy Gebben | f96739f | 2015-09-16 14:04:42 -0600 | [diff] [blame] | 419 | if (prot & IOMMU_WRITE) |
| 420 | pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RW |
| 421 | : ARM_LPAE_PTE_AP_RW; |
| 422 | else |
| 423 | pte |= (prot & IOMMU_PRIV) ? ARM_LPAE_PTE_AP_PRIV_RO |
| 424 | : ARM_LPAE_PTE_AP_RO; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 425 | |
Robin Murphy | fb94825 | 2016-04-05 12:39:31 +0100 | [diff] [blame] | 426 | if (prot & IOMMU_MMIO) |
| 427 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV |
| 428 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
| 429 | else if (prot & IOMMU_CACHE) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 430 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE |
| 431 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
| 432 | } else { |
| 433 | pte = ARM_LPAE_PTE_HAP_FAULT; |
| 434 | if (prot & IOMMU_READ) |
| 435 | pte |= ARM_LPAE_PTE_HAP_READ; |
| 436 | if (prot & IOMMU_WRITE) |
| 437 | pte |= ARM_LPAE_PTE_HAP_WRITE; |
Robin Murphy | fb94825 | 2016-04-05 12:39:31 +0100 | [diff] [blame] | 438 | if (prot & IOMMU_MMIO) |
| 439 | pte |= ARM_LPAE_PTE_MEMATTR_DEV; |
| 440 | else if (prot & IOMMU_CACHE) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 441 | pte |= ARM_LPAE_PTE_MEMATTR_OIWB; |
| 442 | else |
| 443 | pte |= ARM_LPAE_PTE_MEMATTR_NC; |
| 444 | } |
| 445 | |
| 446 | if (prot & IOMMU_NOEXEC) |
| 447 | pte |= ARM_LPAE_PTE_XN; |
| 448 | |
| 449 | return pte; |
| 450 | } |
| 451 | |
| 452 | static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, |
| 453 | phys_addr_t paddr, size_t size, int iommu_prot) |
| 454 | { |
| 455 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 456 | arm_lpae_iopte *ptep = data->pgd; |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 457 | int ret, lvl = ARM_LPAE_START_LVL(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 458 | arm_lpae_iopte prot; |
| 459 | |
| 460 | /* If no access, then nothing to do */ |
| 461 | if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) |
| 462 | return 0; |
| 463 | |
| 464 | prot = arm_lpae_prot_to_pte(data, iommu_prot); |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 465 | ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, NULL); |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 466 | /* |
| 467 | * Synchronise all PTE updates for the new mapping before there's |
| 468 | * a chance for anything to kick off a table walk for the new iova. |
| 469 | */ |
| 470 | wmb(); |
| 471 | |
| 472 | return ret; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 475 | static int arm_lpae_map_sg(struct io_pgtable_ops *ops, unsigned long iova, |
| 476 | struct scatterlist *sg, unsigned int nents, |
Rohit Vaswani | 4d7cdd9 | 2015-08-18 17:57:44 -0700 | [diff] [blame] | 477 | int iommu_prot, size_t *size) |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 478 | { |
| 479 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 480 | arm_lpae_iopte *ptep = data->pgd; |
| 481 | int lvl = ARM_LPAE_START_LVL(data); |
| 482 | arm_lpae_iopte prot; |
| 483 | struct scatterlist *s; |
| 484 | size_t mapped = 0; |
| 485 | int i, ret; |
| 486 | unsigned int min_pagesz; |
| 487 | |
| 488 | /* If no access, then nothing to do */ |
| 489 | if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) |
Rohit Vaswani | 4d7cdd9 | 2015-08-18 17:57:44 -0700 | [diff] [blame] | 490 | goto out_err; |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 491 | |
| 492 | prot = arm_lpae_prot_to_pte(data, iommu_prot); |
| 493 | |
| 494 | min_pagesz = 1 << __ffs(data->iop.cfg.pgsize_bitmap); |
| 495 | |
| 496 | for_each_sg(sg, s, nents, i) { |
| 497 | phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset; |
| 498 | size_t size = s->length; |
| 499 | |
| 500 | /* |
| 501 | * We are mapping on IOMMU page boundaries, so offset within |
| 502 | * the page must be 0. However, the IOMMU may support pages |
| 503 | * smaller than PAGE_SIZE, so s->offset may still represent |
| 504 | * an offset of that boundary within the CPU page. |
| 505 | */ |
| 506 | if (!IS_ALIGNED(s->offset, min_pagesz)) |
| 507 | goto out_err; |
| 508 | |
| 509 | while (size) { |
| 510 | size_t pgsize = iommu_pgsize( |
| 511 | data->iop.cfg.pgsize_bitmap, iova | phys, size); |
| 512 | ret = __arm_lpae_map(data, iova, phys, pgsize, prot, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 513 | lvl, ptep, NULL); |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 514 | if (ret) |
| 515 | goto out_err; |
| 516 | |
| 517 | iova += pgsize; |
| 518 | mapped += pgsize; |
| 519 | phys += pgsize; |
| 520 | size -= pgsize; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return mapped; |
| 525 | |
| 526 | out_err: |
Rohit Vaswani | 4d7cdd9 | 2015-08-18 17:57:44 -0700 | [diff] [blame] | 527 | /* Return the size of the partial mapping so that they can be undone */ |
| 528 | *size = mapped; |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 532 | static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, |
| 533 | arm_lpae_iopte *ptep) |
| 534 | { |
| 535 | arm_lpae_iopte *start, *end; |
| 536 | unsigned long table_size; |
| 537 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 538 | if (lvl == ARM_LPAE_START_LVL(data)) |
| 539 | table_size = data->pgd_size; |
| 540 | else |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 541 | table_size = ARM_LPAE_GRANULE(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 542 | |
| 543 | start = ptep; |
Will Deacon | 12c2ab0 | 2015-12-15 16:08:12 +0000 | [diff] [blame] | 544 | |
| 545 | /* Only leaf entries at the last level */ |
| 546 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 547 | end = ptep; |
| 548 | else |
| 549 | end = (void *)ptep + table_size; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 550 | |
| 551 | while (ptep != end) { |
| 552 | arm_lpae_iopte pte = *ptep++; |
| 553 | |
| 554 | if (!pte || iopte_leaf(pte, lvl)) |
| 555 | continue; |
| 556 | |
| 557 | __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); |
| 558 | } |
| 559 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 560 | __arm_lpae_free_pages(start, table_size, &data->iop.cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static void arm_lpae_free_pgtable(struct io_pgtable *iop) |
| 564 | { |
| 565 | struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); |
| 566 | |
| 567 | __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); |
| 568 | kfree(data); |
| 569 | } |
| 570 | |
| 571 | static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, |
| 572 | unsigned long iova, size_t size, |
| 573 | arm_lpae_iopte prot, int lvl, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 574 | arm_lpae_iopte *ptep, |
| 575 | arm_lpae_iopte *prev_ptep, size_t blk_size) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 576 | { |
| 577 | unsigned long blk_start, blk_end; |
| 578 | phys_addr_t blk_paddr; |
| 579 | arm_lpae_iopte table = 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 580 | |
| 581 | blk_start = iova & ~(blk_size - 1); |
| 582 | blk_end = blk_start + blk_size; |
| 583 | blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift; |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 584 | size = ARM_LPAE_BLOCK_SIZE(lvl + 1, data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 585 | |
| 586 | for (; blk_start < blk_end; blk_start += size, blk_paddr += size) { |
| 587 | arm_lpae_iopte *tablep; |
| 588 | |
| 589 | /* Unmap! */ |
| 590 | if (blk_start == iova) |
| 591 | continue; |
| 592 | |
| 593 | /* __arm_lpae_map expects a pointer to the start of the table */ |
| 594 | tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data); |
| 595 | if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 596 | tablep, prev_ptep) < 0) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 597 | if (table) { |
| 598 | /* Free the table we allocated */ |
| 599 | tablep = iopte_deref(table, data); |
| 600 | __arm_lpae_free_pgtable(data, lvl + 1, tablep); |
| 601 | } |
| 602 | return 0; /* Bytes unmapped */ |
| 603 | } |
| 604 | } |
| 605 | |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 606 | __arm_lpae_set_pte(ptep, table, &data->iop.cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 607 | iova &= ~(blk_size - 1); |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 608 | io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 609 | return size; |
| 610 | } |
| 611 | |
| 612 | static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
| 613 | unsigned long iova, size_t size, int lvl, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 614 | arm_lpae_iopte *ptep, arm_lpae_iopte *prev_ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 615 | { |
| 616 | arm_lpae_iopte pte; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 617 | struct io_pgtable *iop = &data->iop; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 618 | size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 619 | |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 620 | /* Something went horribly wrong and we ran out of page table */ |
| 621 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 622 | return 0; |
| 623 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 624 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 625 | pte = *ptep; |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 626 | if (WARN_ON(!pte)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 627 | return 0; |
| 628 | |
| 629 | /* If the size matches this level, we're in the right place */ |
| 630 | if (size == blk_size) { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 631 | __arm_lpae_set_pte(ptep, 0, &iop->cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 632 | |
| 633 | if (!iopte_leaf(pte, lvl)) { |
| 634 | /* Also flush any partial walks */ |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 635 | io_pgtable_tlb_add_flush(iop, iova, size, |
| 636 | ARM_LPAE_GRANULE(data), false); |
| 637 | io_pgtable_tlb_sync(iop); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 638 | ptep = iopte_deref(pte, data); |
| 639 | __arm_lpae_free_pgtable(data, lvl + 1, ptep); |
| 640 | } else { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 641 | io_pgtable_tlb_add_flush(iop, iova, size, size, true); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | return size; |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 645 | } else if ((lvl == ARM_LPAE_MAX_LEVELS - 2) && !iopte_leaf(pte, lvl)) { |
| 646 | arm_lpae_iopte *table = iopte_deref(pte, data); |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 647 | arm_lpae_iopte *table_base = table; |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 648 | int tl_offset = ARM_LPAE_LVL_IDX(iova, lvl + 1, data); |
| 649 | int entry_size = ARM_LPAE_GRANULE(data); |
| 650 | int max_entries = ARM_LPAE_BLOCK_SIZE(lvl, data) / entry_size; |
| 651 | int entries = min_t(int, size / entry_size, |
| 652 | max_entries - tl_offset); |
| 653 | int table_len = entries * sizeof(*table); |
| 654 | |
| 655 | /* |
| 656 | * This isn't a block mapping so it must be a table mapping |
| 657 | * and since it's the 2nd-to-last level the next level has |
| 658 | * to be all page mappings. Zero them all out in one fell |
| 659 | * swoop. |
| 660 | */ |
| 661 | |
| 662 | table += tl_offset; |
| 663 | |
| 664 | memset(table, 0, table_len); |
| 665 | dma_sync_single_for_device(iop->cfg.iommu_dev, |
| 666 | __arm_lpae_dma_addr(table), |
| 667 | table_len, DMA_TO_DEVICE); |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 668 | |
| 669 | iopte_tblcnt_sub(ptep, entries); |
| 670 | if (!iopte_tblcnt(*ptep)) { |
| 671 | /* no valid mappings left under this table. free it. */ |
| 672 | __arm_lpae_set_pte(ptep, 0, &iop->cfg); |
| 673 | io_pgtable_tlb_add_flush(iop, iova, |
| 674 | entries * entry_size, |
| 675 | ARM_LPAE_GRANULE(data), |
| 676 | false); |
| 677 | __arm_lpae_free_pgtable(data, lvl + 1, table_base); |
| 678 | } else { |
| 679 | io_pgtable_tlb_add_flush(iop, iova, |
| 680 | entries * entry_size, |
| 681 | ARM_LPAE_GRANULE(data), |
| 682 | true); |
| 683 | } |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 684 | |
| 685 | return entries * entry_size; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 686 | } else if (iopte_leaf(pte, lvl)) { |
| 687 | /* |
| 688 | * Insert a table at the next level to map the old region, |
| 689 | * minus the part we want to unmap |
| 690 | */ |
| 691 | return arm_lpae_split_blk_unmap(data, iova, size, |
| 692 | iopte_prot(pte), lvl, ptep, |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 693 | prev_ptep, blk_size); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* Keep on walkin' */ |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 697 | prev_ptep = ptep; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 698 | ptep = iopte_deref(pte, data); |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 699 | return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep, prev_ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Mitchel Humpherys | 5e05059 | 2015-05-21 14:11:22 -0700 | [diff] [blame] | 702 | static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 703 | size_t size) |
| 704 | { |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 705 | size_t unmapped = 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 706 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 707 | arm_lpae_iopte *ptep = data->pgd; |
| 708 | int lvl = ARM_LPAE_START_LVL(data); |
| 709 | |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 710 | while (unmapped < size) { |
| 711 | size_t ret, size_to_unmap, remaining; |
| 712 | |
| 713 | remaining = (size - unmapped); |
Patrick Daly | f145f05 | 2016-06-27 18:38:09 -0700 | [diff] [blame] | 714 | size_to_unmap = iommu_pgsize(data->iop.cfg.pgsize_bitmap, iova, |
| 715 | remaining); |
| 716 | size_to_unmap = size_to_unmap >= SZ_2M ? |
| 717 | size_to_unmap : |
| 718 | min_t(unsigned long, remaining, |
| 719 | (ALIGN(iova + 1, SZ_2M) - iova)); |
Mitchel Humpherys | deb3e83 | 2015-07-14 16:41:29 -0700 | [diff] [blame] | 720 | ret = __arm_lpae_unmap(data, iova, size_to_unmap, lvl, ptep, |
| 721 | NULL); |
Mitchel Humpherys | 5f92f32 | 2015-04-30 09:49:29 -0700 | [diff] [blame] | 722 | if (ret == 0) |
| 723 | break; |
| 724 | unmapped += ret; |
| 725 | iova += ret; |
| 726 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 727 | if (unmapped) |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 728 | io_pgtable_tlb_sync(&data->iop); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 729 | |
| 730 | return unmapped; |
| 731 | } |
| 732 | |
| 733 | static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, |
| 734 | unsigned long iova) |
| 735 | { |
| 736 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 737 | arm_lpae_iopte pte, *ptep = data->pgd; |
| 738 | int lvl = ARM_LPAE_START_LVL(data); |
| 739 | |
| 740 | do { |
| 741 | /* Valid IOPTE pointer? */ |
| 742 | if (!ptep) |
| 743 | return 0; |
| 744 | |
| 745 | /* Grab the IOPTE we're interested in */ |
| 746 | pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data)); |
| 747 | |
| 748 | /* Valid entry? */ |
| 749 | if (!pte) |
| 750 | return 0; |
| 751 | |
| 752 | /* Leaf entry? */ |
| 753 | if (iopte_leaf(pte,lvl)) |
| 754 | goto found_translation; |
| 755 | |
| 756 | /* Take it to the next level */ |
| 757 | ptep = iopte_deref(pte, data); |
| 758 | } while (++lvl < ARM_LPAE_MAX_LEVELS); |
| 759 | |
| 760 | /* Ran out of page tables to walk */ |
| 761 | return 0; |
| 762 | |
| 763 | found_translation: |
Will Deacon | 7c6d90e | 2016-06-16 18:21:19 +0100 | [diff] [blame] | 764 | iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 765 | return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova; |
| 766 | } |
| 767 | |
| 768 | static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) |
| 769 | { |
| 770 | unsigned long granule; |
| 771 | |
| 772 | /* |
| 773 | * We need to restrict the supported page sizes to match the |
| 774 | * translation regime for a particular granule. Aim to match |
| 775 | * the CPU page size if possible, otherwise prefer smaller sizes. |
| 776 | * While we're at it, restrict the block sizes to match the |
| 777 | * chosen granule. |
| 778 | */ |
| 779 | if (cfg->pgsize_bitmap & PAGE_SIZE) |
| 780 | granule = PAGE_SIZE; |
| 781 | else if (cfg->pgsize_bitmap & ~PAGE_MASK) |
| 782 | granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); |
| 783 | else if (cfg->pgsize_bitmap & PAGE_MASK) |
| 784 | granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); |
| 785 | else |
| 786 | granule = 0; |
| 787 | |
| 788 | switch (granule) { |
| 789 | case SZ_4K: |
| 790 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 791 | break; |
| 792 | case SZ_16K: |
| 793 | cfg->pgsize_bitmap &= (SZ_16K | SZ_32M); |
| 794 | break; |
| 795 | case SZ_64K: |
| 796 | cfg->pgsize_bitmap &= (SZ_64K | SZ_512M); |
| 797 | break; |
| 798 | default: |
| 799 | cfg->pgsize_bitmap = 0; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | static struct arm_lpae_io_pgtable * |
| 804 | arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) |
| 805 | { |
| 806 | unsigned long va_bits, pgd_bits; |
| 807 | struct arm_lpae_io_pgtable *data; |
| 808 | |
| 809 | arm_lpae_restrict_pgsizes(cfg); |
| 810 | |
| 811 | if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) |
| 812 | return NULL; |
| 813 | |
| 814 | if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) |
| 815 | return NULL; |
| 816 | |
| 817 | if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) |
| 818 | return NULL; |
| 819 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 820 | if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { |
| 821 | dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); |
| 822 | return NULL; |
| 823 | } |
| 824 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 825 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 826 | if (!data) |
| 827 | return NULL; |
| 828 | |
| 829 | data->pg_shift = __ffs(cfg->pgsize_bitmap); |
| 830 | data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); |
| 831 | |
| 832 | va_bits = cfg->ias - data->pg_shift; |
| 833 | data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); |
| 834 | |
| 835 | /* Calculate the actual size of our pgd (without concatenation) */ |
| 836 | pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); |
| 837 | data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); |
| 838 | |
| 839 | data->iop.ops = (struct io_pgtable_ops) { |
| 840 | .map = arm_lpae_map, |
Mitchel Humpherys | daab041 | 2015-04-23 16:19:05 -0700 | [diff] [blame] | 841 | .map_sg = arm_lpae_map_sg, |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 842 | .unmap = arm_lpae_unmap, |
| 843 | .iova_to_phys = arm_lpae_iova_to_phys, |
| 844 | }; |
| 845 | |
| 846 | return data; |
| 847 | } |
| 848 | |
| 849 | static struct io_pgtable * |
| 850 | arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 851 | { |
| 852 | u64 reg; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 853 | struct arm_lpae_io_pgtable *data; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 854 | |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 855 | if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS) |
| 856 | return NULL; |
| 857 | |
| 858 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 859 | if (!data) |
| 860 | return NULL; |
| 861 | |
| 862 | /* TCR */ |
| 863 | reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 864 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 865 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 866 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 867 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 868 | case SZ_4K: |
| 869 | reg |= ARM_LPAE_TCR_TG0_4K; |
| 870 | break; |
| 871 | case SZ_16K: |
| 872 | reg |= ARM_LPAE_TCR_TG0_16K; |
| 873 | break; |
| 874 | case SZ_64K: |
| 875 | reg |= ARM_LPAE_TCR_TG0_64K; |
| 876 | break; |
| 877 | } |
| 878 | |
| 879 | switch (cfg->oas) { |
| 880 | case 32: |
| 881 | reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 882 | break; |
| 883 | case 36: |
| 884 | reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 885 | break; |
| 886 | case 40: |
| 887 | reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 888 | break; |
| 889 | case 42: |
| 890 | reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 891 | break; |
| 892 | case 44: |
| 893 | reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 894 | break; |
| 895 | case 48: |
| 896 | reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 897 | break; |
| 898 | default: |
| 899 | goto out_free_data; |
| 900 | } |
| 901 | |
| 902 | reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; |
Will Deacon | 63979b8 | 2015-03-18 10:22:18 +0000 | [diff] [blame] | 903 | |
| 904 | /* Disable speculative walks through TTBR1 */ |
| 905 | reg |= ARM_LPAE_TCR_EPD1; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 906 | cfg->arm_lpae_s1_cfg.tcr = reg; |
| 907 | |
| 908 | /* MAIRs */ |
| 909 | reg = (ARM_LPAE_MAIR_ATTR_NC |
| 910 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | |
| 911 | (ARM_LPAE_MAIR_ATTR_WBRWA |
| 912 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | |
| 913 | (ARM_LPAE_MAIR_ATTR_DEVICE |
| 914 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); |
| 915 | |
| 916 | cfg->arm_lpae_s1_cfg.mair[0] = reg; |
| 917 | cfg->arm_lpae_s1_cfg.mair[1] = 0; |
| 918 | |
| 919 | /* Looking good; allocate a pgd */ |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 920 | data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 921 | if (!data->pgd) |
| 922 | goto out_free_data; |
| 923 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 924 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 925 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 926 | |
| 927 | /* TTBRs */ |
| 928 | cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); |
| 929 | cfg->arm_lpae_s1_cfg.ttbr[1] = 0; |
| 930 | return &data->iop; |
| 931 | |
| 932 | out_free_data: |
| 933 | kfree(data); |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | static struct io_pgtable * |
| 938 | arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 939 | { |
| 940 | u64 reg, sl; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 941 | struct arm_lpae_io_pgtable *data; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 942 | |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 943 | /* The NS quirk doesn't apply at stage 2 */ |
| 944 | if (cfg->quirks) |
| 945 | return NULL; |
| 946 | |
| 947 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 948 | if (!data) |
| 949 | return NULL; |
| 950 | |
| 951 | /* |
| 952 | * Concatenate PGDs at level 1 if possible in order to reduce |
| 953 | * the depth of the stage-2 walk. |
| 954 | */ |
| 955 | if (data->levels == ARM_LPAE_MAX_LEVELS) { |
| 956 | unsigned long pgd_pages; |
| 957 | |
| 958 | pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); |
| 959 | if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { |
| 960 | data->pgd_size = pgd_pages << data->pg_shift; |
| 961 | data->levels--; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | /* VTCR */ |
| 966 | reg = ARM_64_LPAE_S2_TCR_RES1 | |
| 967 | (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 968 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 969 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 970 | |
| 971 | sl = ARM_LPAE_START_LVL(data); |
| 972 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 973 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 974 | case SZ_4K: |
| 975 | reg |= ARM_LPAE_TCR_TG0_4K; |
| 976 | sl++; /* SL0 format is different for 4K granule size */ |
| 977 | break; |
| 978 | case SZ_16K: |
| 979 | reg |= ARM_LPAE_TCR_TG0_16K; |
| 980 | break; |
| 981 | case SZ_64K: |
| 982 | reg |= ARM_LPAE_TCR_TG0_64K; |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | switch (cfg->oas) { |
| 987 | case 32: |
| 988 | reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 989 | break; |
| 990 | case 36: |
| 991 | reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 992 | break; |
| 993 | case 40: |
| 994 | reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 995 | break; |
| 996 | case 42: |
| 997 | reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 998 | break; |
| 999 | case 44: |
| 1000 | reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 1001 | break; |
| 1002 | case 48: |
| 1003 | reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 1004 | break; |
| 1005 | default: |
| 1006 | goto out_free_data; |
| 1007 | } |
| 1008 | |
| 1009 | reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; |
| 1010 | reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; |
| 1011 | cfg->arm_lpae_s2_cfg.vtcr = reg; |
| 1012 | |
| 1013 | /* Allocate pgd pages */ |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 1014 | data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1015 | if (!data->pgd) |
| 1016 | goto out_free_data; |
| 1017 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 1018 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 1019 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1020 | |
| 1021 | /* VTTBR */ |
| 1022 | cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); |
| 1023 | return &data->iop; |
| 1024 | |
| 1025 | out_free_data: |
| 1026 | kfree(data); |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | |
| 1030 | static struct io_pgtable * |
| 1031 | arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 1032 | { |
| 1033 | struct io_pgtable *iop; |
| 1034 | |
| 1035 | if (cfg->ias > 32 || cfg->oas > 40) |
| 1036 | return NULL; |
| 1037 | |
| 1038 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 1039 | iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); |
| 1040 | if (iop) { |
| 1041 | cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; |
| 1042 | cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; |
| 1043 | } |
| 1044 | |
| 1045 | return iop; |
| 1046 | } |
| 1047 | |
| 1048 | static struct io_pgtable * |
| 1049 | arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 1050 | { |
| 1051 | struct io_pgtable *iop; |
| 1052 | |
| 1053 | if (cfg->ias > 40 || cfg->oas > 40) |
| 1054 | return NULL; |
| 1055 | |
| 1056 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 1057 | iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); |
| 1058 | if (iop) |
| 1059 | cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; |
| 1060 | |
| 1061 | return iop; |
| 1062 | } |
| 1063 | |
| 1064 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { |
| 1065 | .alloc = arm_64_lpae_alloc_pgtable_s1, |
| 1066 | .free = arm_lpae_free_pgtable, |
| 1067 | }; |
| 1068 | |
| 1069 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { |
| 1070 | .alloc = arm_64_lpae_alloc_pgtable_s2, |
| 1071 | .free = arm_lpae_free_pgtable, |
| 1072 | }; |
| 1073 | |
| 1074 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { |
| 1075 | .alloc = arm_32_lpae_alloc_pgtable_s1, |
| 1076 | .free = arm_lpae_free_pgtable, |
| 1077 | }; |
| 1078 | |
| 1079 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { |
| 1080 | .alloc = arm_32_lpae_alloc_pgtable_s2, |
| 1081 | .free = arm_lpae_free_pgtable, |
| 1082 | }; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1083 | |
| 1084 | #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST |
| 1085 | |
| 1086 | static struct io_pgtable_cfg *cfg_cookie; |
| 1087 | |
| 1088 | static void dummy_tlb_flush_all(void *cookie) |
| 1089 | { |
| 1090 | WARN_ON(cookie != cfg_cookie); |
| 1091 | } |
| 1092 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 1093 | static void dummy_tlb_add_flush(unsigned long iova, size_t size, |
| 1094 | size_t granule, bool leaf, void *cookie) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1095 | { |
| 1096 | WARN_ON(cookie != cfg_cookie); |
| 1097 | WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); |
| 1098 | } |
| 1099 | |
| 1100 | static void dummy_tlb_sync(void *cookie) |
| 1101 | { |
| 1102 | WARN_ON(cookie != cfg_cookie); |
| 1103 | } |
| 1104 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1105 | static struct iommu_gather_ops dummy_tlb_ops __initdata = { |
| 1106 | .tlb_flush_all = dummy_tlb_flush_all, |
| 1107 | .tlb_add_flush = dummy_tlb_add_flush, |
| 1108 | .tlb_sync = dummy_tlb_sync, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1109 | }; |
| 1110 | |
| 1111 | static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) |
| 1112 | { |
| 1113 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 1114 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 1115 | |
| 1116 | pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", |
| 1117 | cfg->pgsize_bitmap, cfg->ias); |
| 1118 | pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", |
| 1119 | data->levels, data->pgd_size, data->pg_shift, |
| 1120 | data->bits_per_level, data->pgd); |
| 1121 | } |
| 1122 | |
| 1123 | #define __FAIL(ops, i) ({ \ |
| 1124 | WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ |
| 1125 | arm_lpae_dump_ops(ops); \ |
| 1126 | selftest_running = false; \ |
| 1127 | -EFAULT; \ |
| 1128 | }) |
| 1129 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1130 | /* |
| 1131 | * Returns true if the entire iova range has any mapping in ops. |
| 1132 | */ |
| 1133 | static bool arm_lpae_range_has_mapping(struct io_pgtable_ops *ops, |
| 1134 | unsigned long iova_start, size_t size) |
| 1135 | { |
| 1136 | unsigned long iova = iova_start; |
| 1137 | |
| 1138 | while (iova < (iova_start + size)) { |
| 1139 | if (!ops->iova_to_phys(ops, iova + 42)) |
| 1140 | return false; |
| 1141 | iova += SZ_4K; |
| 1142 | } |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Returns true if the iova range is successfully mapped to the contiguous |
| 1148 | * phys range in ops. |
| 1149 | */ |
| 1150 | static bool arm_lpae_range_has_specific_mapping(struct io_pgtable_ops *ops, |
| 1151 | const unsigned long iova_start, |
| 1152 | const phys_addr_t phys_start, |
| 1153 | const size_t size) |
| 1154 | { |
| 1155 | unsigned long iova = iova_start; |
| 1156 | phys_addr_t phys = phys_start; |
| 1157 | |
| 1158 | while (iova < (iova_start + size)) { |
| 1159 | if (ops->iova_to_phys(ops, iova + 42) != (phys + 42)) |
| 1160 | return false; |
| 1161 | iova += SZ_4K; |
| 1162 | phys += SZ_4K; |
| 1163 | } |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1167 | static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) |
| 1168 | { |
| 1169 | static const enum io_pgtable_fmt fmts[] = { |
| 1170 | ARM_64_LPAE_S1, |
| 1171 | ARM_64_LPAE_S2, |
| 1172 | }; |
| 1173 | |
Mitchel Humpherys | df18a9a | 2015-04-23 13:41:31 -0700 | [diff] [blame] | 1174 | int i, j, k; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1175 | unsigned long iova; |
| 1176 | size_t size; |
| 1177 | struct io_pgtable_ops *ops; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1178 | selftest_running = true; |
| 1179 | |
| 1180 | for (i = 0; i < ARRAY_SIZE(fmts); ++i) { |
Mitchel Humpherys | df18a9a | 2015-04-23 13:41:31 -0700 | [diff] [blame] | 1181 | unsigned long test_sg_sizes[] = { SZ_4K, SZ_64K, SZ_2M, |
| 1182 | SZ_1M * 12, SZ_1M * 20 }; |
| 1183 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1184 | cfg_cookie = cfg; |
| 1185 | ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); |
| 1186 | if (!ops) { |
| 1187 | pr_err("selftest: failed to allocate io pgtable ops\n"); |
| 1188 | return -ENOMEM; |
| 1189 | } |
| 1190 | |
| 1191 | /* |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1192 | * Initial sanity checks. Empty page tables shouldn't |
| 1193 | * provide any translations. TODO: check entire supported |
| 1194 | * range for these ops rather than first 2G |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1195 | */ |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1196 | if (arm_lpae_range_has_mapping(ops, 0, SZ_2G)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1197 | return __FAIL(ops, i); |
| 1198 | |
| 1199 | /* |
| 1200 | * Distinct mappings of different granule sizes. |
| 1201 | */ |
| 1202 | iova = 0; |
| 1203 | j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); |
| 1204 | while (j != BITS_PER_LONG) { |
| 1205 | size = 1UL << j; |
| 1206 | |
| 1207 | if (ops->map(ops, iova, iova, size, IOMMU_READ | |
| 1208 | IOMMU_WRITE | |
| 1209 | IOMMU_NOEXEC | |
| 1210 | IOMMU_CACHE)) |
| 1211 | return __FAIL(ops, i); |
| 1212 | |
| 1213 | /* Overlapping mappings */ |
| 1214 | if (!ops->map(ops, iova, iova + size, size, |
| 1215 | IOMMU_READ | IOMMU_NOEXEC)) |
| 1216 | return __FAIL(ops, i); |
| 1217 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1218 | if (!arm_lpae_range_has_specific_mapping(ops, iova, |
| 1219 | iova, size)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1220 | return __FAIL(ops, i); |
| 1221 | |
| 1222 | iova += SZ_1G; |
| 1223 | j++; |
| 1224 | j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); |
| 1225 | } |
| 1226 | |
| 1227 | /* Partial unmap */ |
| 1228 | size = 1UL << __ffs(cfg->pgsize_bitmap); |
| 1229 | if (ops->unmap(ops, SZ_1G + size, size) != size) |
| 1230 | return __FAIL(ops, i); |
| 1231 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1232 | if (arm_lpae_range_has_mapping(ops, SZ_1G + size, size)) |
| 1233 | return __FAIL(ops, i); |
| 1234 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1235 | /* Remap of partial unmap */ |
| 1236 | if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) |
| 1237 | return __FAIL(ops, i); |
| 1238 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1239 | if (!arm_lpae_range_has_specific_mapping(ops, SZ_1G + size, |
| 1240 | size, size)) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1241 | return __FAIL(ops, i); |
| 1242 | |
| 1243 | /* Full unmap */ |
| 1244 | iova = 0; |
| 1245 | j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); |
| 1246 | while (j != BITS_PER_LONG) { |
| 1247 | size = 1UL << j; |
| 1248 | |
| 1249 | if (ops->unmap(ops, iova, size) != size) |
| 1250 | return __FAIL(ops, i); |
| 1251 | |
| 1252 | if (ops->iova_to_phys(ops, iova + 42)) |
| 1253 | return __FAIL(ops, i); |
| 1254 | |
| 1255 | /* Remap full block */ |
| 1256 | if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) |
| 1257 | return __FAIL(ops, i); |
| 1258 | |
| 1259 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1260 | return __FAIL(ops, i); |
| 1261 | |
Mitchel Humpherys | df18a9a | 2015-04-23 13:41:31 -0700 | [diff] [blame] | 1262 | if (ops->unmap(ops, iova, size) != size) |
| 1263 | return __FAIL(ops, i); |
| 1264 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1265 | iova += SZ_1G; |
| 1266 | j++; |
| 1267 | j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); |
| 1268 | } |
| 1269 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1270 | if (arm_lpae_range_has_mapping(ops, 0, SZ_2G)) |
| 1271 | return __FAIL(ops, i); |
| 1272 | |
Mitchel Humpherys | e4012a6 | 2015-06-01 15:44:49 -0700 | [diff] [blame^] | 1273 | if ((cfg->pgsize_bitmap & SZ_2M) && |
| 1274 | (cfg->pgsize_bitmap & SZ_4K)) { |
| 1275 | /* mixed block + page mappings */ |
| 1276 | iova = 0; |
| 1277 | if (ops->map(ops, iova, iova, SZ_2M, IOMMU_READ)) |
| 1278 | return __FAIL(ops, i); |
| 1279 | |
| 1280 | if (ops->map(ops, iova + SZ_2M, iova + SZ_2M, SZ_4K, |
| 1281 | IOMMU_READ)) |
| 1282 | return __FAIL(ops, i); |
| 1283 | |
| 1284 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1285 | return __FAIL(ops, i); |
| 1286 | |
| 1287 | if (ops->iova_to_phys(ops, iova + SZ_2M + 42) != |
| 1288 | (iova + SZ_2M + 42)) |
| 1289 | return __FAIL(ops, i); |
| 1290 | |
| 1291 | /* unmap both mappings at once */ |
| 1292 | if (ops->unmap(ops, iova, SZ_2M + SZ_4K) != |
| 1293 | (SZ_2M + SZ_4K)) |
| 1294 | return __FAIL(ops, i); |
| 1295 | |
| 1296 | if (arm_lpae_range_has_mapping(ops, 0, SZ_2G)) |
| 1297 | return __FAIL(ops, i); |
| 1298 | } |
| 1299 | |
Mitchel Humpherys | df18a9a | 2015-04-23 13:41:31 -0700 | [diff] [blame] | 1300 | /* map_sg */ |
| 1301 | for (j = 0; j < ARRAY_SIZE(test_sg_sizes); ++j) { |
| 1302 | size_t mapped; |
| 1303 | size_t unused; |
| 1304 | struct page *page; |
| 1305 | phys_addr_t page_phys; |
| 1306 | struct sg_table table; |
| 1307 | struct scatterlist *sg; |
| 1308 | unsigned long total_size = test_sg_sizes[j]; |
| 1309 | int chunk_size = 1UL << find_first_bit( |
| 1310 | &cfg->pgsize_bitmap, BITS_PER_LONG); |
| 1311 | int nents = total_size / chunk_size; |
| 1312 | |
| 1313 | if (total_size < chunk_size) |
| 1314 | continue; |
| 1315 | |
| 1316 | page = alloc_pages(GFP_KERNEL, get_order(chunk_size)); |
| 1317 | page_phys = page_to_phys(page); |
| 1318 | |
| 1319 | iova = 0; |
| 1320 | BUG_ON(sg_alloc_table(&table, nents, GFP_KERNEL)); |
| 1321 | BUG_ON(!page); |
| 1322 | for_each_sg(table.sgl, sg, table.nents, k) |
| 1323 | sg_set_page(sg, page, chunk_size, 0); |
| 1324 | |
| 1325 | mapped = ops->map_sg(ops, iova, table.sgl, table.nents, |
| 1326 | IOMMU_READ | IOMMU_WRITE, &unused); |
| 1327 | |
| 1328 | if (mapped != total_size) |
| 1329 | return __FAIL(ops, i); |
| 1330 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1331 | if (!arm_lpae_range_has_mapping(ops, iova, total_size)) |
| 1332 | return __FAIL(ops, i); |
| 1333 | |
| 1334 | if (arm_lpae_range_has_mapping(ops, iova + total_size, |
| 1335 | SZ_2G - (iova + total_size))) |
| 1336 | return __FAIL(ops, i); |
| 1337 | |
Mitchel Humpherys | df18a9a | 2015-04-23 13:41:31 -0700 | [diff] [blame] | 1338 | for_each_sg(table.sgl, sg, table.nents, k) { |
| 1339 | dma_addr_t newphys = |
| 1340 | ops->iova_to_phys(ops, iova + 42); |
| 1341 | if (newphys != (page_phys + 42)) |
| 1342 | return __FAIL(ops, i); |
| 1343 | iova += chunk_size; |
| 1344 | } |
| 1345 | |
| 1346 | if (ops->unmap(ops, 0, total_size) != total_size) |
| 1347 | return __FAIL(ops, i); |
| 1348 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1349 | if (arm_lpae_range_has_mapping(ops, 0, SZ_2G)) |
| 1350 | return __FAIL(ops, i); |
| 1351 | |
Mitchel Humpherys | df18a9a | 2015-04-23 13:41:31 -0700 | [diff] [blame] | 1352 | sg_free_table(&table); |
| 1353 | __free_pages(page, get_order(chunk_size)); |
| 1354 | } |
| 1355 | |
Mitchel Humpherys | fdf212a | 2015-05-11 13:40:38 -0700 | [diff] [blame] | 1356 | if (arm_lpae_range_has_mapping(ops, 0, SZ_2G)) |
| 1357 | return __FAIL(ops, i); |
| 1358 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1359 | free_io_pgtable_ops(ops); |
| 1360 | } |
| 1361 | |
| 1362 | selftest_running = false; |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static int __init arm_lpae_do_selftests(void) |
| 1367 | { |
| 1368 | static const unsigned long pgsize[] = { |
| 1369 | SZ_4K | SZ_2M | SZ_1G, |
| 1370 | SZ_16K | SZ_32M, |
| 1371 | SZ_64K | SZ_512M, |
| 1372 | }; |
| 1373 | |
| 1374 | static const unsigned int ias[] = { |
| 1375 | 32, 36, 40, 42, 44, 48, |
| 1376 | }; |
| 1377 | |
| 1378 | int i, j, pass = 0, fail = 0; |
| 1379 | struct io_pgtable_cfg cfg = { |
| 1380 | .tlb = &dummy_tlb_ops, |
| 1381 | .oas = 48, |
| 1382 | }; |
| 1383 | |
| 1384 | for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { |
| 1385 | for (j = 0; j < ARRAY_SIZE(ias); ++j) { |
| 1386 | cfg.pgsize_bitmap = pgsize[i]; |
| 1387 | cfg.ias = ias[j]; |
| 1388 | pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", |
| 1389 | pgsize[i], ias[j]); |
| 1390 | if (arm_lpae_run_tests(&cfg)) |
| 1391 | fail++; |
| 1392 | else |
| 1393 | pass++; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); |
| 1398 | return fail ? -EFAULT : 0; |
| 1399 | } |
| 1400 | subsys_initcall(arm_lpae_do_selftests); |
| 1401 | #endif |