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> |
| 25 | #include <linux/sizes.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/types.h> |
| 28 | |
| 29 | #include "io-pgtable.h" |
| 30 | |
| 31 | #define ARM_LPAE_MAX_ADDR_BITS 48 |
| 32 | #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 |
| 33 | #define ARM_LPAE_MAX_LEVELS 4 |
| 34 | |
| 35 | /* Struct accessors */ |
| 36 | #define io_pgtable_to_data(x) \ |
| 37 | container_of((x), struct arm_lpae_io_pgtable, iop) |
| 38 | |
| 39 | #define io_pgtable_ops_to_pgtable(x) \ |
| 40 | container_of((x), struct io_pgtable, ops) |
| 41 | |
| 42 | #define io_pgtable_ops_to_data(x) \ |
| 43 | io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) |
| 44 | |
| 45 | /* |
| 46 | * For consistency with the architecture, we always consider |
| 47 | * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 |
| 48 | */ |
| 49 | #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) |
| 50 | |
| 51 | /* |
| 52 | * Calculate the right shift amount to get to the portion describing level l |
| 53 | * in a virtual address mapped by the pagetable in d. |
| 54 | */ |
| 55 | #define ARM_LPAE_LVL_SHIFT(l,d) \ |
| 56 | ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ |
| 57 | * (d)->bits_per_level) + (d)->pg_shift) |
| 58 | |
| 59 | #define ARM_LPAE_PAGES_PER_PGD(d) ((d)->pgd_size >> (d)->pg_shift) |
| 60 | |
| 61 | /* |
| 62 | * Calculate the index at level l used to map virtual address a using the |
| 63 | * pagetable in d. |
| 64 | */ |
| 65 | #define ARM_LPAE_PGD_IDX(l,d) \ |
| 66 | ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) |
| 67 | |
| 68 | #define ARM_LPAE_LVL_IDX(a,l,d) \ |
| 69 | (((a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ |
| 70 | ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) |
| 71 | |
| 72 | /* Calculate the block/page mapping size at level l for pagetable in d. */ |
| 73 | #define ARM_LPAE_BLOCK_SIZE(l,d) \ |
| 74 | (1 << (ilog2(sizeof(arm_lpae_iopte)) + \ |
| 75 | ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) |
| 76 | |
| 77 | /* Page table bits */ |
| 78 | #define ARM_LPAE_PTE_TYPE_SHIFT 0 |
| 79 | #define ARM_LPAE_PTE_TYPE_MASK 0x3 |
| 80 | |
| 81 | #define ARM_LPAE_PTE_TYPE_BLOCK 1 |
| 82 | #define ARM_LPAE_PTE_TYPE_TABLE 3 |
| 83 | #define ARM_LPAE_PTE_TYPE_PAGE 3 |
| 84 | |
| 85 | #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) |
| 86 | #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) |
| 87 | #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) |
| 88 | #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) |
| 89 | #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) |
| 90 | #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) |
| 91 | |
| 92 | #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) |
| 93 | /* Ignore the contiguous bit for block splitting */ |
| 94 | #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) |
| 95 | #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ |
| 96 | ARM_LPAE_PTE_ATTR_HI_MASK) |
| 97 | |
| 98 | /* Stage-1 PTE */ |
| 99 | #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) |
| 100 | #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) |
| 101 | #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 |
| 102 | #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) |
| 103 | |
| 104 | /* Stage-2 PTE */ |
| 105 | #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) |
| 106 | #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) |
| 107 | #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) |
| 108 | #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) |
| 109 | #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) |
| 110 | #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) |
| 111 | |
| 112 | /* Register bits */ |
| 113 | #define ARM_32_LPAE_TCR_EAE (1 << 31) |
| 114 | #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) |
| 115 | |
| 116 | #define ARM_LPAE_TCR_TG0_4K (0 << 14) |
| 117 | #define ARM_LPAE_TCR_TG0_64K (1 << 14) |
| 118 | #define ARM_LPAE_TCR_TG0_16K (2 << 14) |
| 119 | |
| 120 | #define ARM_LPAE_TCR_SH0_SHIFT 12 |
| 121 | #define ARM_LPAE_TCR_SH0_MASK 0x3 |
| 122 | #define ARM_LPAE_TCR_SH_NS 0 |
| 123 | #define ARM_LPAE_TCR_SH_OS 2 |
| 124 | #define ARM_LPAE_TCR_SH_IS 3 |
| 125 | |
| 126 | #define ARM_LPAE_TCR_ORGN0_SHIFT 10 |
| 127 | #define ARM_LPAE_TCR_IRGN0_SHIFT 8 |
| 128 | #define ARM_LPAE_TCR_RGN_MASK 0x3 |
| 129 | #define ARM_LPAE_TCR_RGN_NC 0 |
| 130 | #define ARM_LPAE_TCR_RGN_WBWA 1 |
| 131 | #define ARM_LPAE_TCR_RGN_WT 2 |
| 132 | #define ARM_LPAE_TCR_RGN_WB 3 |
| 133 | |
| 134 | #define ARM_LPAE_TCR_SL0_SHIFT 6 |
| 135 | #define ARM_LPAE_TCR_SL0_MASK 0x3 |
| 136 | |
| 137 | #define ARM_LPAE_TCR_T0SZ_SHIFT 0 |
| 138 | #define ARM_LPAE_TCR_SZ_MASK 0xf |
| 139 | |
| 140 | #define ARM_LPAE_TCR_PS_SHIFT 16 |
| 141 | #define ARM_LPAE_TCR_PS_MASK 0x7 |
| 142 | |
| 143 | #define ARM_LPAE_TCR_IPS_SHIFT 32 |
| 144 | #define ARM_LPAE_TCR_IPS_MASK 0x7 |
| 145 | |
| 146 | #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL |
| 147 | #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL |
| 148 | #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL |
| 149 | #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL |
| 150 | #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL |
| 151 | #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL |
| 152 | |
| 153 | #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) |
| 154 | #define ARM_LPAE_MAIR_ATTR_MASK 0xff |
| 155 | #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 |
| 156 | #define ARM_LPAE_MAIR_ATTR_NC 0x44 |
| 157 | #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff |
| 158 | #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 |
| 159 | #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 |
| 160 | #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 |
| 161 | |
| 162 | /* IOPTE accessors */ |
| 163 | #define iopte_deref(pte,d) \ |
| 164 | (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \ |
| 165 | & ~((1ULL << (d)->pg_shift) - 1))) |
| 166 | |
| 167 | #define iopte_type(pte,l) \ |
| 168 | (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) |
| 169 | |
| 170 | #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) |
| 171 | |
| 172 | #define iopte_leaf(pte,l) \ |
| 173 | (l == (ARM_LPAE_MAX_LEVELS - 1) ? \ |
| 174 | (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \ |
| 175 | (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK)) |
| 176 | |
| 177 | #define iopte_to_pfn(pte,d) \ |
| 178 | (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift) |
| 179 | |
| 180 | #define pfn_to_iopte(pfn,d) \ |
| 181 | (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) |
| 182 | |
| 183 | struct arm_lpae_io_pgtable { |
| 184 | struct io_pgtable iop; |
| 185 | |
| 186 | int levels; |
| 187 | size_t pgd_size; |
| 188 | unsigned long pg_shift; |
| 189 | unsigned long bits_per_level; |
| 190 | |
| 191 | void *pgd; |
| 192 | }; |
| 193 | |
| 194 | typedef u64 arm_lpae_iopte; |
| 195 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame^] | 196 | static bool selftest_running = false; |
| 197 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 198 | static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 199 | unsigned long iova, phys_addr_t paddr, |
| 200 | arm_lpae_iopte prot, int lvl, |
| 201 | arm_lpae_iopte *ptep) |
| 202 | { |
| 203 | arm_lpae_iopte pte = prot; |
| 204 | |
| 205 | /* We require an unmap first */ |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame^] | 206 | if (iopte_leaf(*ptep, lvl)) { |
| 207 | WARN_ON(!selftest_running); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 208 | return -EEXIST; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame^] | 209 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 210 | |
| 211 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 212 | pte |= ARM_LPAE_PTE_TYPE_PAGE; |
| 213 | else |
| 214 | pte |= ARM_LPAE_PTE_TYPE_BLOCK; |
| 215 | |
| 216 | pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; |
| 217 | pte |= pfn_to_iopte(paddr >> data->pg_shift, data); |
| 218 | |
| 219 | *ptep = pte; |
| 220 | data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), data->iop.cookie); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, |
| 225 | phys_addr_t paddr, size_t size, arm_lpae_iopte prot, |
| 226 | int lvl, arm_lpae_iopte *ptep) |
| 227 | { |
| 228 | arm_lpae_iopte *cptep, pte; |
| 229 | void *cookie = data->iop.cookie; |
| 230 | size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 231 | |
| 232 | /* Find our entry at the current level */ |
| 233 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 234 | |
| 235 | /* If we can install a leaf entry at this level, then do so */ |
| 236 | if (size == block_size && (size & data->iop.cfg.pgsize_bitmap)) |
| 237 | return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); |
| 238 | |
| 239 | /* We can't allocate tables at the final level */ |
| 240 | if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | /* Grab a pointer to the next level */ |
| 244 | pte = *ptep; |
| 245 | if (!pte) { |
| 246 | cptep = alloc_pages_exact(1UL << data->pg_shift, |
| 247 | GFP_ATOMIC | __GFP_ZERO); |
| 248 | if (!cptep) |
| 249 | return -ENOMEM; |
| 250 | |
| 251 | data->iop.cfg.tlb->flush_pgtable(cptep, 1UL << data->pg_shift, |
| 252 | cookie); |
| 253 | pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE; |
| 254 | *ptep = pte; |
| 255 | data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); |
| 256 | } else { |
| 257 | cptep = iopte_deref(pte, data); |
| 258 | } |
| 259 | |
| 260 | /* Rinse, repeat */ |
| 261 | return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); |
| 262 | } |
| 263 | |
| 264 | static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, |
| 265 | int prot) |
| 266 | { |
| 267 | arm_lpae_iopte pte; |
| 268 | |
| 269 | if (data->iop.fmt == ARM_64_LPAE_S1 || |
| 270 | data->iop.fmt == ARM_32_LPAE_S1) { |
| 271 | pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG; |
| 272 | |
| 273 | if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) |
| 274 | pte |= ARM_LPAE_PTE_AP_RDONLY; |
| 275 | |
| 276 | if (prot & IOMMU_CACHE) |
| 277 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE |
| 278 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
| 279 | } else { |
| 280 | pte = ARM_LPAE_PTE_HAP_FAULT; |
| 281 | if (prot & IOMMU_READ) |
| 282 | pte |= ARM_LPAE_PTE_HAP_READ; |
| 283 | if (prot & IOMMU_WRITE) |
| 284 | pte |= ARM_LPAE_PTE_HAP_WRITE; |
| 285 | if (prot & IOMMU_CACHE) |
| 286 | pte |= ARM_LPAE_PTE_MEMATTR_OIWB; |
| 287 | else |
| 288 | pte |= ARM_LPAE_PTE_MEMATTR_NC; |
| 289 | } |
| 290 | |
| 291 | if (prot & IOMMU_NOEXEC) |
| 292 | pte |= ARM_LPAE_PTE_XN; |
| 293 | |
| 294 | return pte; |
| 295 | } |
| 296 | |
| 297 | static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, |
| 298 | phys_addr_t paddr, size_t size, int iommu_prot) |
| 299 | { |
| 300 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 301 | arm_lpae_iopte *ptep = data->pgd; |
| 302 | int lvl = ARM_LPAE_START_LVL(data); |
| 303 | arm_lpae_iopte prot; |
| 304 | |
| 305 | /* If no access, then nothing to do */ |
| 306 | if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) |
| 307 | return 0; |
| 308 | |
| 309 | prot = arm_lpae_prot_to_pte(data, iommu_prot); |
| 310 | return __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); |
| 311 | } |
| 312 | |
| 313 | static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, |
| 314 | arm_lpae_iopte *ptep) |
| 315 | { |
| 316 | arm_lpae_iopte *start, *end; |
| 317 | unsigned long table_size; |
| 318 | |
| 319 | /* Only leaf entries at the last level */ |
| 320 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 321 | return; |
| 322 | |
| 323 | if (lvl == ARM_LPAE_START_LVL(data)) |
| 324 | table_size = data->pgd_size; |
| 325 | else |
| 326 | table_size = 1UL << data->pg_shift; |
| 327 | |
| 328 | start = ptep; |
| 329 | end = (void *)ptep + table_size; |
| 330 | |
| 331 | while (ptep != end) { |
| 332 | arm_lpae_iopte pte = *ptep++; |
| 333 | |
| 334 | if (!pte || iopte_leaf(pte, lvl)) |
| 335 | continue; |
| 336 | |
| 337 | __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); |
| 338 | } |
| 339 | |
| 340 | free_pages_exact(start, table_size); |
| 341 | } |
| 342 | |
| 343 | static void arm_lpae_free_pgtable(struct io_pgtable *iop) |
| 344 | { |
| 345 | struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); |
| 346 | |
| 347 | __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); |
| 348 | kfree(data); |
| 349 | } |
| 350 | |
| 351 | static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, |
| 352 | unsigned long iova, size_t size, |
| 353 | arm_lpae_iopte prot, int lvl, |
| 354 | arm_lpae_iopte *ptep, size_t blk_size) |
| 355 | { |
| 356 | unsigned long blk_start, blk_end; |
| 357 | phys_addr_t blk_paddr; |
| 358 | arm_lpae_iopte table = 0; |
| 359 | void *cookie = data->iop.cookie; |
| 360 | const struct iommu_gather_ops *tlb = data->iop.cfg.tlb; |
| 361 | |
| 362 | blk_start = iova & ~(blk_size - 1); |
| 363 | blk_end = blk_start + blk_size; |
| 364 | blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift; |
| 365 | |
| 366 | for (; blk_start < blk_end; blk_start += size, blk_paddr += size) { |
| 367 | arm_lpae_iopte *tablep; |
| 368 | |
| 369 | /* Unmap! */ |
| 370 | if (blk_start == iova) |
| 371 | continue; |
| 372 | |
| 373 | /* __arm_lpae_map expects a pointer to the start of the table */ |
| 374 | tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data); |
| 375 | if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl, |
| 376 | tablep) < 0) { |
| 377 | if (table) { |
| 378 | /* Free the table we allocated */ |
| 379 | tablep = iopte_deref(table, data); |
| 380 | __arm_lpae_free_pgtable(data, lvl + 1, tablep); |
| 381 | } |
| 382 | return 0; /* Bytes unmapped */ |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | *ptep = table; |
| 387 | tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); |
| 388 | iova &= ~(blk_size - 1); |
| 389 | tlb->tlb_add_flush(iova, blk_size, true, cookie); |
| 390 | return size; |
| 391 | } |
| 392 | |
| 393 | static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
| 394 | unsigned long iova, size_t size, int lvl, |
| 395 | arm_lpae_iopte *ptep) |
| 396 | { |
| 397 | arm_lpae_iopte pte; |
| 398 | const struct iommu_gather_ops *tlb = data->iop.cfg.tlb; |
| 399 | void *cookie = data->iop.cookie; |
| 400 | size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 401 | |
| 402 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 403 | pte = *ptep; |
| 404 | |
| 405 | /* Something went horribly wrong and we ran out of page table */ |
| 406 | if (WARN_ON(!pte || (lvl == ARM_LPAE_MAX_LEVELS))) |
| 407 | return 0; |
| 408 | |
| 409 | /* If the size matches this level, we're in the right place */ |
| 410 | if (size == blk_size) { |
| 411 | *ptep = 0; |
| 412 | tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); |
| 413 | |
| 414 | if (!iopte_leaf(pte, lvl)) { |
| 415 | /* Also flush any partial walks */ |
| 416 | tlb->tlb_add_flush(iova, size, false, cookie); |
| 417 | tlb->tlb_sync(data->iop.cookie); |
| 418 | ptep = iopte_deref(pte, data); |
| 419 | __arm_lpae_free_pgtable(data, lvl + 1, ptep); |
| 420 | } else { |
| 421 | tlb->tlb_add_flush(iova, size, true, cookie); |
| 422 | } |
| 423 | |
| 424 | return size; |
| 425 | } else if (iopte_leaf(pte, lvl)) { |
| 426 | /* |
| 427 | * Insert a table at the next level to map the old region, |
| 428 | * minus the part we want to unmap |
| 429 | */ |
| 430 | return arm_lpae_split_blk_unmap(data, iova, size, |
| 431 | iopte_prot(pte), lvl, ptep, |
| 432 | blk_size); |
| 433 | } |
| 434 | |
| 435 | /* Keep on walkin' */ |
| 436 | ptep = iopte_deref(pte, data); |
| 437 | return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep); |
| 438 | } |
| 439 | |
| 440 | static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, |
| 441 | size_t size) |
| 442 | { |
| 443 | size_t unmapped; |
| 444 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 445 | struct io_pgtable *iop = &data->iop; |
| 446 | arm_lpae_iopte *ptep = data->pgd; |
| 447 | int lvl = ARM_LPAE_START_LVL(data); |
| 448 | |
| 449 | unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep); |
| 450 | if (unmapped) |
| 451 | iop->cfg.tlb->tlb_sync(iop->cookie); |
| 452 | |
| 453 | return unmapped; |
| 454 | } |
| 455 | |
| 456 | static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, |
| 457 | unsigned long iova) |
| 458 | { |
| 459 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 460 | arm_lpae_iopte pte, *ptep = data->pgd; |
| 461 | int lvl = ARM_LPAE_START_LVL(data); |
| 462 | |
| 463 | do { |
| 464 | /* Valid IOPTE pointer? */ |
| 465 | if (!ptep) |
| 466 | return 0; |
| 467 | |
| 468 | /* Grab the IOPTE we're interested in */ |
| 469 | pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data)); |
| 470 | |
| 471 | /* Valid entry? */ |
| 472 | if (!pte) |
| 473 | return 0; |
| 474 | |
| 475 | /* Leaf entry? */ |
| 476 | if (iopte_leaf(pte,lvl)) |
| 477 | goto found_translation; |
| 478 | |
| 479 | /* Take it to the next level */ |
| 480 | ptep = iopte_deref(pte, data); |
| 481 | } while (++lvl < ARM_LPAE_MAX_LEVELS); |
| 482 | |
| 483 | /* Ran out of page tables to walk */ |
| 484 | return 0; |
| 485 | |
| 486 | found_translation: |
| 487 | iova &= ((1 << data->pg_shift) - 1); |
| 488 | return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova; |
| 489 | } |
| 490 | |
| 491 | static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) |
| 492 | { |
| 493 | unsigned long granule; |
| 494 | |
| 495 | /* |
| 496 | * We need to restrict the supported page sizes to match the |
| 497 | * translation regime for a particular granule. Aim to match |
| 498 | * the CPU page size if possible, otherwise prefer smaller sizes. |
| 499 | * While we're at it, restrict the block sizes to match the |
| 500 | * chosen granule. |
| 501 | */ |
| 502 | if (cfg->pgsize_bitmap & PAGE_SIZE) |
| 503 | granule = PAGE_SIZE; |
| 504 | else if (cfg->pgsize_bitmap & ~PAGE_MASK) |
| 505 | granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); |
| 506 | else if (cfg->pgsize_bitmap & PAGE_MASK) |
| 507 | granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); |
| 508 | else |
| 509 | granule = 0; |
| 510 | |
| 511 | switch (granule) { |
| 512 | case SZ_4K: |
| 513 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 514 | break; |
| 515 | case SZ_16K: |
| 516 | cfg->pgsize_bitmap &= (SZ_16K | SZ_32M); |
| 517 | break; |
| 518 | case SZ_64K: |
| 519 | cfg->pgsize_bitmap &= (SZ_64K | SZ_512M); |
| 520 | break; |
| 521 | default: |
| 522 | cfg->pgsize_bitmap = 0; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static struct arm_lpae_io_pgtable * |
| 527 | arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) |
| 528 | { |
| 529 | unsigned long va_bits, pgd_bits; |
| 530 | struct arm_lpae_io_pgtable *data; |
| 531 | |
| 532 | arm_lpae_restrict_pgsizes(cfg); |
| 533 | |
| 534 | if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) |
| 535 | return NULL; |
| 536 | |
| 537 | if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) |
| 538 | return NULL; |
| 539 | |
| 540 | if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) |
| 541 | return NULL; |
| 542 | |
| 543 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 544 | if (!data) |
| 545 | return NULL; |
| 546 | |
| 547 | data->pg_shift = __ffs(cfg->pgsize_bitmap); |
| 548 | data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); |
| 549 | |
| 550 | va_bits = cfg->ias - data->pg_shift; |
| 551 | data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); |
| 552 | |
| 553 | /* Calculate the actual size of our pgd (without concatenation) */ |
| 554 | pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); |
| 555 | data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); |
| 556 | |
| 557 | data->iop.ops = (struct io_pgtable_ops) { |
| 558 | .map = arm_lpae_map, |
| 559 | .unmap = arm_lpae_unmap, |
| 560 | .iova_to_phys = arm_lpae_iova_to_phys, |
| 561 | }; |
| 562 | |
| 563 | return data; |
| 564 | } |
| 565 | |
| 566 | static struct io_pgtable * |
| 567 | arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 568 | { |
| 569 | u64 reg; |
| 570 | struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg); |
| 571 | |
| 572 | if (!data) |
| 573 | return NULL; |
| 574 | |
| 575 | /* TCR */ |
| 576 | reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 577 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 578 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 579 | |
| 580 | switch (1 << data->pg_shift) { |
| 581 | case SZ_4K: |
| 582 | reg |= ARM_LPAE_TCR_TG0_4K; |
| 583 | break; |
| 584 | case SZ_16K: |
| 585 | reg |= ARM_LPAE_TCR_TG0_16K; |
| 586 | break; |
| 587 | case SZ_64K: |
| 588 | reg |= ARM_LPAE_TCR_TG0_64K; |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | switch (cfg->oas) { |
| 593 | case 32: |
| 594 | reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 595 | break; |
| 596 | case 36: |
| 597 | reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 598 | break; |
| 599 | case 40: |
| 600 | reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 601 | break; |
| 602 | case 42: |
| 603 | reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 604 | break; |
| 605 | case 44: |
| 606 | reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 607 | break; |
| 608 | case 48: |
| 609 | reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); |
| 610 | break; |
| 611 | default: |
| 612 | goto out_free_data; |
| 613 | } |
| 614 | |
| 615 | reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; |
| 616 | cfg->arm_lpae_s1_cfg.tcr = reg; |
| 617 | |
| 618 | /* MAIRs */ |
| 619 | reg = (ARM_LPAE_MAIR_ATTR_NC |
| 620 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | |
| 621 | (ARM_LPAE_MAIR_ATTR_WBRWA |
| 622 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | |
| 623 | (ARM_LPAE_MAIR_ATTR_DEVICE |
| 624 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); |
| 625 | |
| 626 | cfg->arm_lpae_s1_cfg.mair[0] = reg; |
| 627 | cfg->arm_lpae_s1_cfg.mair[1] = 0; |
| 628 | |
| 629 | /* Looking good; allocate a pgd */ |
| 630 | data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO); |
| 631 | if (!data->pgd) |
| 632 | goto out_free_data; |
| 633 | |
| 634 | cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie); |
| 635 | |
| 636 | /* TTBRs */ |
| 637 | cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); |
| 638 | cfg->arm_lpae_s1_cfg.ttbr[1] = 0; |
| 639 | return &data->iop; |
| 640 | |
| 641 | out_free_data: |
| 642 | kfree(data); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | static struct io_pgtable * |
| 647 | arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 648 | { |
| 649 | u64 reg, sl; |
| 650 | struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg); |
| 651 | |
| 652 | if (!data) |
| 653 | return NULL; |
| 654 | |
| 655 | /* |
| 656 | * Concatenate PGDs at level 1 if possible in order to reduce |
| 657 | * the depth of the stage-2 walk. |
| 658 | */ |
| 659 | if (data->levels == ARM_LPAE_MAX_LEVELS) { |
| 660 | unsigned long pgd_pages; |
| 661 | |
| 662 | pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); |
| 663 | if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { |
| 664 | data->pgd_size = pgd_pages << data->pg_shift; |
| 665 | data->levels--; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /* VTCR */ |
| 670 | reg = ARM_64_LPAE_S2_TCR_RES1 | |
| 671 | (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | |
| 672 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | |
| 673 | (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); |
| 674 | |
| 675 | sl = ARM_LPAE_START_LVL(data); |
| 676 | |
| 677 | switch (1 << data->pg_shift) { |
| 678 | case SZ_4K: |
| 679 | reg |= ARM_LPAE_TCR_TG0_4K; |
| 680 | sl++; /* SL0 format is different for 4K granule size */ |
| 681 | break; |
| 682 | case SZ_16K: |
| 683 | reg |= ARM_LPAE_TCR_TG0_16K; |
| 684 | break; |
| 685 | case SZ_64K: |
| 686 | reg |= ARM_LPAE_TCR_TG0_64K; |
| 687 | break; |
| 688 | } |
| 689 | |
| 690 | switch (cfg->oas) { |
| 691 | case 32: |
| 692 | reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 693 | break; |
| 694 | case 36: |
| 695 | reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 696 | break; |
| 697 | case 40: |
| 698 | reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 699 | break; |
| 700 | case 42: |
| 701 | reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 702 | break; |
| 703 | case 44: |
| 704 | reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 705 | break; |
| 706 | case 48: |
| 707 | reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); |
| 708 | break; |
| 709 | default: |
| 710 | goto out_free_data; |
| 711 | } |
| 712 | |
| 713 | reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; |
| 714 | reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; |
| 715 | cfg->arm_lpae_s2_cfg.vtcr = reg; |
| 716 | |
| 717 | /* Allocate pgd pages */ |
| 718 | data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO); |
| 719 | if (!data->pgd) |
| 720 | goto out_free_data; |
| 721 | |
| 722 | cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie); |
| 723 | |
| 724 | /* VTTBR */ |
| 725 | cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); |
| 726 | return &data->iop; |
| 727 | |
| 728 | out_free_data: |
| 729 | kfree(data); |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | static struct io_pgtable * |
| 734 | arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 735 | { |
| 736 | struct io_pgtable *iop; |
| 737 | |
| 738 | if (cfg->ias > 32 || cfg->oas > 40) |
| 739 | return NULL; |
| 740 | |
| 741 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 742 | iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); |
| 743 | if (iop) { |
| 744 | cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; |
| 745 | cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; |
| 746 | } |
| 747 | |
| 748 | return iop; |
| 749 | } |
| 750 | |
| 751 | static struct io_pgtable * |
| 752 | arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 753 | { |
| 754 | struct io_pgtable *iop; |
| 755 | |
| 756 | if (cfg->ias > 40 || cfg->oas > 40) |
| 757 | return NULL; |
| 758 | |
| 759 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
| 760 | iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); |
| 761 | if (iop) |
| 762 | cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; |
| 763 | |
| 764 | return iop; |
| 765 | } |
| 766 | |
| 767 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { |
| 768 | .alloc = arm_64_lpae_alloc_pgtable_s1, |
| 769 | .free = arm_lpae_free_pgtable, |
| 770 | }; |
| 771 | |
| 772 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { |
| 773 | .alloc = arm_64_lpae_alloc_pgtable_s2, |
| 774 | .free = arm_lpae_free_pgtable, |
| 775 | }; |
| 776 | |
| 777 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { |
| 778 | .alloc = arm_32_lpae_alloc_pgtable_s1, |
| 779 | .free = arm_lpae_free_pgtable, |
| 780 | }; |
| 781 | |
| 782 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { |
| 783 | .alloc = arm_32_lpae_alloc_pgtable_s2, |
| 784 | .free = arm_lpae_free_pgtable, |
| 785 | }; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame^] | 786 | |
| 787 | #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST |
| 788 | |
| 789 | static struct io_pgtable_cfg *cfg_cookie; |
| 790 | |
| 791 | static void dummy_tlb_flush_all(void *cookie) |
| 792 | { |
| 793 | WARN_ON(cookie != cfg_cookie); |
| 794 | } |
| 795 | |
| 796 | static void dummy_tlb_add_flush(unsigned long iova, size_t size, bool leaf, |
| 797 | void *cookie) |
| 798 | { |
| 799 | WARN_ON(cookie != cfg_cookie); |
| 800 | WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); |
| 801 | } |
| 802 | |
| 803 | static void dummy_tlb_sync(void *cookie) |
| 804 | { |
| 805 | WARN_ON(cookie != cfg_cookie); |
| 806 | } |
| 807 | |
| 808 | static void dummy_flush_pgtable(void *ptr, size_t size, void *cookie) |
| 809 | { |
| 810 | WARN_ON(cookie != cfg_cookie); |
| 811 | } |
| 812 | |
| 813 | static struct iommu_gather_ops dummy_tlb_ops __initdata = { |
| 814 | .tlb_flush_all = dummy_tlb_flush_all, |
| 815 | .tlb_add_flush = dummy_tlb_add_flush, |
| 816 | .tlb_sync = dummy_tlb_sync, |
| 817 | .flush_pgtable = dummy_flush_pgtable, |
| 818 | }; |
| 819 | |
| 820 | static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) |
| 821 | { |
| 822 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 823 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 824 | |
| 825 | pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", |
| 826 | cfg->pgsize_bitmap, cfg->ias); |
| 827 | pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", |
| 828 | data->levels, data->pgd_size, data->pg_shift, |
| 829 | data->bits_per_level, data->pgd); |
| 830 | } |
| 831 | |
| 832 | #define __FAIL(ops, i) ({ \ |
| 833 | WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ |
| 834 | arm_lpae_dump_ops(ops); \ |
| 835 | selftest_running = false; \ |
| 836 | -EFAULT; \ |
| 837 | }) |
| 838 | |
| 839 | static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) |
| 840 | { |
| 841 | static const enum io_pgtable_fmt fmts[] = { |
| 842 | ARM_64_LPAE_S1, |
| 843 | ARM_64_LPAE_S2, |
| 844 | }; |
| 845 | |
| 846 | int i, j; |
| 847 | unsigned long iova; |
| 848 | size_t size; |
| 849 | struct io_pgtable_ops *ops; |
| 850 | |
| 851 | selftest_running = true; |
| 852 | |
| 853 | for (i = 0; i < ARRAY_SIZE(fmts); ++i) { |
| 854 | cfg_cookie = cfg; |
| 855 | ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); |
| 856 | if (!ops) { |
| 857 | pr_err("selftest: failed to allocate io pgtable ops\n"); |
| 858 | return -ENOMEM; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Initial sanity checks. |
| 863 | * Empty page tables shouldn't provide any translations. |
| 864 | */ |
| 865 | if (ops->iova_to_phys(ops, 42)) |
| 866 | return __FAIL(ops, i); |
| 867 | |
| 868 | if (ops->iova_to_phys(ops, SZ_1G + 42)) |
| 869 | return __FAIL(ops, i); |
| 870 | |
| 871 | if (ops->iova_to_phys(ops, SZ_2G + 42)) |
| 872 | return __FAIL(ops, i); |
| 873 | |
| 874 | /* |
| 875 | * Distinct mappings of different granule sizes. |
| 876 | */ |
| 877 | iova = 0; |
| 878 | j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); |
| 879 | while (j != BITS_PER_LONG) { |
| 880 | size = 1UL << j; |
| 881 | |
| 882 | if (ops->map(ops, iova, iova, size, IOMMU_READ | |
| 883 | IOMMU_WRITE | |
| 884 | IOMMU_NOEXEC | |
| 885 | IOMMU_CACHE)) |
| 886 | return __FAIL(ops, i); |
| 887 | |
| 888 | /* Overlapping mappings */ |
| 889 | if (!ops->map(ops, iova, iova + size, size, |
| 890 | IOMMU_READ | IOMMU_NOEXEC)) |
| 891 | return __FAIL(ops, i); |
| 892 | |
| 893 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 894 | return __FAIL(ops, i); |
| 895 | |
| 896 | iova += SZ_1G; |
| 897 | j++; |
| 898 | j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); |
| 899 | } |
| 900 | |
| 901 | /* Partial unmap */ |
| 902 | size = 1UL << __ffs(cfg->pgsize_bitmap); |
| 903 | if (ops->unmap(ops, SZ_1G + size, size) != size) |
| 904 | return __FAIL(ops, i); |
| 905 | |
| 906 | /* Remap of partial unmap */ |
| 907 | if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) |
| 908 | return __FAIL(ops, i); |
| 909 | |
| 910 | if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) |
| 911 | return __FAIL(ops, i); |
| 912 | |
| 913 | /* Full unmap */ |
| 914 | iova = 0; |
| 915 | j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); |
| 916 | while (j != BITS_PER_LONG) { |
| 917 | size = 1UL << j; |
| 918 | |
| 919 | if (ops->unmap(ops, iova, size) != size) |
| 920 | return __FAIL(ops, i); |
| 921 | |
| 922 | if (ops->iova_to_phys(ops, iova + 42)) |
| 923 | return __FAIL(ops, i); |
| 924 | |
| 925 | /* Remap full block */ |
| 926 | if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) |
| 927 | return __FAIL(ops, i); |
| 928 | |
| 929 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 930 | return __FAIL(ops, i); |
| 931 | |
| 932 | iova += SZ_1G; |
| 933 | j++; |
| 934 | j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); |
| 935 | } |
| 936 | |
| 937 | free_io_pgtable_ops(ops); |
| 938 | } |
| 939 | |
| 940 | selftest_running = false; |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static int __init arm_lpae_do_selftests(void) |
| 945 | { |
| 946 | static const unsigned long pgsize[] = { |
| 947 | SZ_4K | SZ_2M | SZ_1G, |
| 948 | SZ_16K | SZ_32M, |
| 949 | SZ_64K | SZ_512M, |
| 950 | }; |
| 951 | |
| 952 | static const unsigned int ias[] = { |
| 953 | 32, 36, 40, 42, 44, 48, |
| 954 | }; |
| 955 | |
| 956 | int i, j, pass = 0, fail = 0; |
| 957 | struct io_pgtable_cfg cfg = { |
| 958 | .tlb = &dummy_tlb_ops, |
| 959 | .oas = 48, |
| 960 | }; |
| 961 | |
| 962 | for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { |
| 963 | for (j = 0; j < ARRAY_SIZE(ias); ++j) { |
| 964 | cfg.pgsize_bitmap = pgsize[i]; |
| 965 | cfg.ias = ias[j]; |
| 966 | pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", |
| 967 | pgsize[i], ias[j]); |
| 968 | if (arm_lpae_run_tests(&cfg)) |
| 969 | fail++; |
| 970 | else |
| 971 | pass++; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); |
| 976 | return fail ? -EFAULT : 0; |
| 977 | } |
| 978 | subsys_initcall(arm_lpae_do_selftests); |
| 979 | #endif |