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