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