Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * CPU-agnostic ARM page table allocator. |
| 3 | * |
| 4 | * ARMv7 Short-descriptor format, supporting |
| 5 | * - Basic memory attributes |
| 6 | * - Simplified access permissions (AP[2:1] model) |
| 7 | * - Backwards-compatible TEX remap |
| 8 | * - Large pages/supersections (if indicated by the caller) |
| 9 | * |
| 10 | * Not supporting: |
| 11 | * - Legacy access permissions (AP[2:0] model) |
| 12 | * |
| 13 | * Almost certainly never supporting: |
| 14 | * - PXN |
| 15 | * - Domains |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License version 2 as |
| 19 | * published by the Free Software Foundation. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | * |
| 29 | * Copyright (C) 2014-2015 ARM Limited |
| 30 | * Copyright (c) 2014-2015 MediaTek Inc. |
| 31 | */ |
| 32 | |
| 33 | #define pr_fmt(fmt) "arm-v7s io-pgtable: " fmt |
| 34 | |
| 35 | #include <linux/dma-mapping.h> |
| 36 | #include <linux/gfp.h> |
| 37 | #include <linux/iommu.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/kmemleak.h> |
| 40 | #include <linux/sizes.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/types.h> |
| 43 | |
| 44 | #include <asm/barrier.h> |
| 45 | |
| 46 | #include "io-pgtable.h" |
| 47 | |
| 48 | /* Struct accessors */ |
| 49 | #define io_pgtable_to_data(x) \ |
| 50 | container_of((x), struct arm_v7s_io_pgtable, iop) |
| 51 | |
| 52 | #define io_pgtable_ops_to_data(x) \ |
| 53 | io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) |
| 54 | |
| 55 | /* |
| 56 | * We have 32 bits total; 12 bits resolved at level 1, 8 bits at level 2, |
| 57 | * and 12 bits in a page. With some carefully-chosen coefficients we can |
| 58 | * hide the ugly inconsistencies behind these macros and at least let the |
| 59 | * rest of the code pretend to be somewhat sane. |
| 60 | */ |
| 61 | #define ARM_V7S_ADDR_BITS 32 |
| 62 | #define _ARM_V7S_LVL_BITS(lvl) (16 - (lvl) * 4) |
| 63 | #define ARM_V7S_LVL_SHIFT(lvl) (ARM_V7S_ADDR_BITS - (4 + 8 * (lvl))) |
| 64 | #define ARM_V7S_TABLE_SHIFT 10 |
| 65 | |
| 66 | #define ARM_V7S_PTES_PER_LVL(lvl) (1 << _ARM_V7S_LVL_BITS(lvl)) |
| 67 | #define ARM_V7S_TABLE_SIZE(lvl) \ |
| 68 | (ARM_V7S_PTES_PER_LVL(lvl) * sizeof(arm_v7s_iopte)) |
| 69 | |
| 70 | #define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl)) |
| 71 | #define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl))) |
| 72 | #define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT)) |
| 73 | #define _ARM_V7S_IDX_MASK(lvl) (ARM_V7S_PTES_PER_LVL(lvl) - 1) |
| 74 | #define ARM_V7S_LVL_IDX(addr, lvl) ({ \ |
| 75 | int _l = lvl; \ |
| 76 | ((u32)(addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l); \ |
| 77 | }) |
| 78 | |
| 79 | /* |
| 80 | * Large page/supersection entries are effectively a block of 16 page/section |
| 81 | * entries, along the lines of the LPAE contiguous hint, but all with the |
| 82 | * same output address. For want of a better common name we'll call them |
| 83 | * "contiguous" versions of their respective page/section entries here, but |
| 84 | * noting the distinction (WRT to TLB maintenance) that they represent *one* |
| 85 | * entry repeated 16 times, not 16 separate entries (as in the LPAE case). |
| 86 | */ |
| 87 | #define ARM_V7S_CONT_PAGES 16 |
| 88 | |
| 89 | /* PTE type bits: these are all mixed up with XN/PXN bits in most cases */ |
| 90 | #define ARM_V7S_PTE_TYPE_TABLE 0x1 |
| 91 | #define ARM_V7S_PTE_TYPE_PAGE 0x2 |
| 92 | #define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1 |
| 93 | |
| 94 | #define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0) |
Robin Murphy | 9db829d | 2017-06-22 16:53:50 +0100 | [diff] [blame] | 95 | #define ARM_V7S_PTE_IS_TABLE(pte, lvl) \ |
| 96 | ((lvl) == 1 && (((pte) & 0x3) == ARM_V7S_PTE_TYPE_TABLE)) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 97 | |
| 98 | /* Page table bits */ |
| 99 | #define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl))) |
| 100 | #define ARM_V7S_ATTR_B BIT(2) |
| 101 | #define ARM_V7S_ATTR_C BIT(3) |
| 102 | #define ARM_V7S_ATTR_NS_TABLE BIT(3) |
| 103 | #define ARM_V7S_ATTR_NS_SECTION BIT(19) |
| 104 | |
| 105 | #define ARM_V7S_CONT_SECTION BIT(18) |
| 106 | #define ARM_V7S_CONT_PAGE_XN_SHIFT 15 |
| 107 | |
| 108 | /* |
| 109 | * The attribute bits are consistently ordered*, but occupy bits [17:10] of |
| 110 | * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual |
| 111 | * fields relative to that 8-bit block, plus a total shift relative to the PTE. |
| 112 | */ |
| 113 | #define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6) |
| 114 | |
| 115 | #define ARM_V7S_ATTR_MASK 0xff |
| 116 | #define ARM_V7S_ATTR_AP0 BIT(0) |
| 117 | #define ARM_V7S_ATTR_AP1 BIT(1) |
| 118 | #define ARM_V7S_ATTR_AP2 BIT(5) |
| 119 | #define ARM_V7S_ATTR_S BIT(6) |
| 120 | #define ARM_V7S_ATTR_NG BIT(7) |
| 121 | #define ARM_V7S_TEX_SHIFT 2 |
| 122 | #define ARM_V7S_TEX_MASK 0x7 |
| 123 | #define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT) |
| 124 | |
Yong Wu | 1afe231 | 2016-03-14 06:01:10 +0800 | [diff] [blame] | 125 | #define ARM_V7S_ATTR_MTK_4GB BIT(9) /* MTK extend it for 4GB mode */ |
| 126 | |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 127 | /* *well, except for TEX on level 2 large pages, of course :( */ |
| 128 | #define ARM_V7S_CONT_PAGE_TEX_SHIFT 6 |
| 129 | #define ARM_V7S_CONT_PAGE_TEX_MASK (ARM_V7S_TEX_MASK << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
| 130 | |
| 131 | /* Simplified access permissions */ |
| 132 | #define ARM_V7S_PTE_AF ARM_V7S_ATTR_AP0 |
| 133 | #define ARM_V7S_PTE_AP_UNPRIV ARM_V7S_ATTR_AP1 |
| 134 | #define ARM_V7S_PTE_AP_RDONLY ARM_V7S_ATTR_AP2 |
| 135 | |
| 136 | /* Register bits */ |
| 137 | #define ARM_V7S_RGN_NC 0 |
| 138 | #define ARM_V7S_RGN_WBWA 1 |
| 139 | #define ARM_V7S_RGN_WT 2 |
| 140 | #define ARM_V7S_RGN_WB 3 |
| 141 | |
| 142 | #define ARM_V7S_PRRR_TYPE_DEVICE 1 |
| 143 | #define ARM_V7S_PRRR_TYPE_NORMAL 2 |
| 144 | #define ARM_V7S_PRRR_TR(n, type) (((type) & 0x3) << ((n) * 2)) |
| 145 | #define ARM_V7S_PRRR_DS0 BIT(16) |
| 146 | #define ARM_V7S_PRRR_DS1 BIT(17) |
| 147 | #define ARM_V7S_PRRR_NS0 BIT(18) |
| 148 | #define ARM_V7S_PRRR_NS1 BIT(19) |
| 149 | #define ARM_V7S_PRRR_NOS(n) BIT((n) + 24) |
| 150 | |
| 151 | #define ARM_V7S_NMRR_IR(n, attr) (((attr) & 0x3) << ((n) * 2)) |
| 152 | #define ARM_V7S_NMRR_OR(n, attr) (((attr) & 0x3) << ((n) * 2 + 16)) |
| 153 | |
| 154 | #define ARM_V7S_TTBR_S BIT(1) |
| 155 | #define ARM_V7S_TTBR_NOS BIT(5) |
| 156 | #define ARM_V7S_TTBR_ORGN_ATTR(attr) (((attr) & 0x3) << 3) |
| 157 | #define ARM_V7S_TTBR_IRGN_ATTR(attr) \ |
| 158 | ((((attr) & 0x1) << 6) | (((attr) & 0x2) >> 1)) |
| 159 | |
| 160 | #define ARM_V7S_TCR_PD1 BIT(5) |
| 161 | |
| 162 | typedef u32 arm_v7s_iopte; |
| 163 | |
| 164 | static bool selftest_running; |
| 165 | |
| 166 | struct arm_v7s_io_pgtable { |
| 167 | struct io_pgtable iop; |
| 168 | |
| 169 | arm_v7s_iopte *pgd; |
| 170 | struct kmem_cache *l2_tables; |
| 171 | }; |
| 172 | |
| 173 | static dma_addr_t __arm_v7s_dma_addr(void *pages) |
| 174 | { |
| 175 | return (dma_addr_t)virt_to_phys(pages); |
| 176 | } |
| 177 | |
| 178 | static arm_v7s_iopte *iopte_deref(arm_v7s_iopte pte, int lvl) |
| 179 | { |
| 180 | if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) |
| 181 | pte &= ARM_V7S_TABLE_MASK; |
| 182 | else |
| 183 | pte &= ARM_V7S_LVL_MASK(lvl); |
| 184 | return phys_to_virt(pte); |
| 185 | } |
| 186 | |
| 187 | static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp, |
| 188 | struct arm_v7s_io_pgtable *data) |
| 189 | { |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 190 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 191 | struct device *dev = cfg->iommu_dev; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 192 | dma_addr_t dma; |
| 193 | size_t size = ARM_V7S_TABLE_SIZE(lvl); |
| 194 | void *table = NULL; |
| 195 | |
| 196 | if (lvl == 1) |
| 197 | table = (void *)__get_dma_pages(__GFP_ZERO, get_order(size)); |
| 198 | else if (lvl == 2) |
Robin Murphy | 048b31c | 2016-03-01 19:07:03 +0000 | [diff] [blame] | 199 | table = kmem_cache_zalloc(data->l2_tables, gfp | GFP_DMA); |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 200 | if (table && !(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) { |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 201 | dma = dma_map_single(dev, table, size, DMA_TO_DEVICE); |
| 202 | if (dma_mapping_error(dev, dma)) |
| 203 | goto out_free; |
| 204 | /* |
| 205 | * We depend on the IOMMU being able to work with any physical |
| 206 | * address directly, so if the DMA layer suggests otherwise by |
| 207 | * translating or truncating them, that bodes very badly... |
| 208 | */ |
| 209 | if (dma != virt_to_phys(table)) |
| 210 | goto out_unmap; |
| 211 | } |
| 212 | kmemleak_ignore(table); |
| 213 | return table; |
| 214 | |
| 215 | out_unmap: |
| 216 | dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); |
| 217 | dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); |
| 218 | out_free: |
| 219 | if (lvl == 1) |
| 220 | free_pages((unsigned long)table, get_order(size)); |
| 221 | else |
| 222 | kmem_cache_free(data->l2_tables, table); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | static void __arm_v7s_free_table(void *table, int lvl, |
| 227 | struct arm_v7s_io_pgtable *data) |
| 228 | { |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 229 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 230 | struct device *dev = cfg->iommu_dev; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 231 | size_t size = ARM_V7S_TABLE_SIZE(lvl); |
| 232 | |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 233 | if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 234 | dma_unmap_single(dev, __arm_v7s_dma_addr(table), size, |
| 235 | DMA_TO_DEVICE); |
| 236 | if (lvl == 1) |
| 237 | free_pages((unsigned long)table, get_order(size)); |
| 238 | else |
| 239 | kmem_cache_free(data->l2_tables, table); |
| 240 | } |
| 241 | |
| 242 | static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries, |
| 243 | struct io_pgtable_cfg *cfg) |
| 244 | { |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 245 | if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 246 | return; |
| 247 | |
| 248 | dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep), |
| 249 | num_entries * sizeof(*ptep), DMA_TO_DEVICE); |
| 250 | } |
| 251 | static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte, |
| 252 | int num_entries, struct io_pgtable_cfg *cfg) |
| 253 | { |
| 254 | int i; |
| 255 | |
| 256 | for (i = 0; i < num_entries; i++) |
| 257 | ptep[i] = pte; |
| 258 | |
| 259 | __arm_v7s_pte_sync(ptep, num_entries, cfg); |
| 260 | } |
| 261 | |
| 262 | static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl, |
| 263 | struct io_pgtable_cfg *cfg) |
| 264 | { |
| 265 | bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS); |
Robin Murphy | e88ccab | 2016-04-05 12:39:32 +0100 | [diff] [blame] | 266 | arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 267 | |
Robin Murphy | e88ccab | 2016-04-05 12:39:32 +0100 | [diff] [blame] | 268 | if (!(prot & IOMMU_MMIO)) |
| 269 | pte |= ARM_V7S_ATTR_TEX(1); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 270 | if (ap) { |
Robin Murphy | 5baf1e9 | 2017-01-06 18:58:10 +0530 | [diff] [blame] | 271 | pte |= ARM_V7S_PTE_AF; |
| 272 | if (!(prot & IOMMU_PRIV)) |
| 273 | pte |= ARM_V7S_PTE_AP_UNPRIV; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 274 | if (!(prot & IOMMU_WRITE)) |
| 275 | pte |= ARM_V7S_PTE_AP_RDONLY; |
| 276 | } |
| 277 | pte <<= ARM_V7S_ATTR_SHIFT(lvl); |
| 278 | |
| 279 | if ((prot & IOMMU_NOEXEC) && ap) |
| 280 | pte |= ARM_V7S_ATTR_XN(lvl); |
Robin Murphy | e88ccab | 2016-04-05 12:39:32 +0100 | [diff] [blame] | 281 | if (prot & IOMMU_MMIO) |
| 282 | pte |= ARM_V7S_ATTR_B; |
| 283 | else if (prot & IOMMU_CACHE) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 284 | pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C; |
| 285 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 286 | pte |= ARM_V7S_PTE_TYPE_PAGE; |
| 287 | if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)) |
| 288 | pte |= ARM_V7S_ATTR_NS_SECTION; |
| 289 | |
| 290 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB) |
| 291 | pte |= ARM_V7S_ATTR_MTK_4GB; |
| 292 | |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 293 | return pte; |
| 294 | } |
| 295 | |
| 296 | static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl) |
| 297 | { |
| 298 | int prot = IOMMU_READ; |
Robin Murphy | e88ccab | 2016-04-05 12:39:32 +0100 | [diff] [blame] | 299 | arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 300 | |
Robin Murphy | e633fc7 | 2016-08-11 17:44:05 +0100 | [diff] [blame] | 301 | if (!(attr & ARM_V7S_PTE_AP_RDONLY)) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 302 | prot |= IOMMU_WRITE; |
Robin Murphy | 5baf1e9 | 2017-01-06 18:58:10 +0530 | [diff] [blame] | 303 | if (!(attr & ARM_V7S_PTE_AP_UNPRIV)) |
| 304 | prot |= IOMMU_PRIV; |
Robin Murphy | e88ccab | 2016-04-05 12:39:32 +0100 | [diff] [blame] | 305 | if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0) |
| 306 | prot |= IOMMU_MMIO; |
| 307 | else if (pte & ARM_V7S_ATTR_C) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 308 | prot |= IOMMU_CACHE; |
Robin Murphy | e633fc7 | 2016-08-11 17:44:05 +0100 | [diff] [blame] | 309 | if (pte & ARM_V7S_ATTR_XN(lvl)) |
| 310 | prot |= IOMMU_NOEXEC; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 311 | |
| 312 | return prot; |
| 313 | } |
| 314 | |
| 315 | static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl) |
| 316 | { |
| 317 | if (lvl == 1) { |
| 318 | pte |= ARM_V7S_CONT_SECTION; |
| 319 | } else if (lvl == 2) { |
| 320 | arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl); |
| 321 | arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK; |
| 322 | |
| 323 | pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE; |
| 324 | pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) | |
| 325 | (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) | |
| 326 | ARM_V7S_PTE_TYPE_CONT_PAGE; |
| 327 | } |
| 328 | return pte; |
| 329 | } |
| 330 | |
| 331 | static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl) |
| 332 | { |
| 333 | if (lvl == 1) { |
| 334 | pte &= ~ARM_V7S_CONT_SECTION; |
| 335 | } else if (lvl == 2) { |
| 336 | arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT); |
| 337 | arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK << |
| 338 | ARM_V7S_CONT_PAGE_TEX_SHIFT); |
| 339 | |
| 340 | pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE; |
| 341 | pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) | |
| 342 | (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) | |
| 343 | ARM_V7S_PTE_TYPE_PAGE; |
| 344 | } |
| 345 | return pte; |
| 346 | } |
| 347 | |
| 348 | static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl) |
| 349 | { |
| 350 | if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl)) |
| 351 | return pte & ARM_V7S_CONT_SECTION; |
| 352 | else if (lvl == 2) |
| 353 | return !(pte & ARM_V7S_PTE_TYPE_PAGE); |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *, unsigned long, |
| 358 | size_t, int, arm_v7s_iopte *); |
| 359 | |
| 360 | static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data, |
| 361 | unsigned long iova, phys_addr_t paddr, int prot, |
| 362 | int lvl, int num_entries, arm_v7s_iopte *ptep) |
| 363 | { |
| 364 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 365 | arm_v7s_iopte pte; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 366 | int i; |
| 367 | |
| 368 | for (i = 0; i < num_entries; i++) |
| 369 | if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) { |
| 370 | /* |
| 371 | * We need to unmap and free the old table before |
| 372 | * overwriting it with a block entry. |
| 373 | */ |
| 374 | arm_v7s_iopte *tblp; |
| 375 | size_t sz = ARM_V7S_BLOCK_SIZE(lvl); |
| 376 | |
| 377 | tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl); |
| 378 | if (WARN_ON(__arm_v7s_unmap(data, iova + i * sz, |
| 379 | sz, lvl, tblp) != sz)) |
| 380 | return -EINVAL; |
| 381 | } else if (ptep[i]) { |
| 382 | /* We require an unmap first */ |
| 383 | WARN_ON(!selftest_running); |
| 384 | return -EEXIST; |
| 385 | } |
| 386 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 387 | pte = arm_v7s_prot_to_pte(prot, lvl, cfg); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 388 | if (num_entries > 1) |
| 389 | pte = arm_v7s_pte_to_cont(pte, lvl); |
| 390 | |
| 391 | pte |= paddr & ARM_V7S_LVL_MASK(lvl); |
| 392 | |
| 393 | __arm_v7s_set_pte(ptep, pte, num_entries, cfg); |
| 394 | return 0; |
| 395 | } |
| 396 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 397 | static arm_v7s_iopte arm_v7s_install_table(arm_v7s_iopte *table, |
| 398 | arm_v7s_iopte *ptep, |
| 399 | struct io_pgtable_cfg *cfg) |
| 400 | { |
| 401 | arm_v7s_iopte new; |
| 402 | |
| 403 | new = virt_to_phys(table) | ARM_V7S_PTE_TYPE_TABLE; |
| 404 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 405 | new |= ARM_V7S_ATTR_NS_TABLE; |
| 406 | |
| 407 | __arm_v7s_set_pte(ptep, new, 1, cfg); |
| 408 | return new; |
| 409 | } |
| 410 | |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 411 | static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova, |
| 412 | phys_addr_t paddr, size_t size, int prot, |
| 413 | int lvl, arm_v7s_iopte *ptep) |
| 414 | { |
| 415 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 416 | arm_v7s_iopte pte, *cptep; |
| 417 | int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl); |
| 418 | |
| 419 | /* Find our entry at the current level */ |
| 420 | ptep += ARM_V7S_LVL_IDX(iova, lvl); |
| 421 | |
| 422 | /* If we can install a leaf entry at this level, then do so */ |
| 423 | if (num_entries) |
| 424 | return arm_v7s_init_pte(data, iova, paddr, prot, |
| 425 | lvl, num_entries, ptep); |
| 426 | |
| 427 | /* We can't allocate tables at the final level */ |
| 428 | if (WARN_ON(lvl == 2)) |
| 429 | return -EINVAL; |
| 430 | |
| 431 | /* Grab a pointer to the next level */ |
| 432 | pte = *ptep; |
| 433 | if (!pte) { |
| 434 | cptep = __arm_v7s_alloc_table(lvl + 1, GFP_ATOMIC, data); |
| 435 | if (!cptep) |
| 436 | return -ENOMEM; |
| 437 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 438 | arm_v7s_install_table(cptep, ptep, cfg); |
Oleksandr Tyshchenko | a03849e | 2017-02-27 14:30:26 +0200 | [diff] [blame] | 439 | } else if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) { |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 440 | cptep = iopte_deref(pte, lvl); |
Oleksandr Tyshchenko | a03849e | 2017-02-27 14:30:26 +0200 | [diff] [blame] | 441 | } else { |
| 442 | /* We require an unmap first */ |
| 443 | WARN_ON(!selftest_running); |
| 444 | return -EEXIST; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* Rinse, repeat */ |
| 448 | return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep); |
| 449 | } |
| 450 | |
| 451 | static int arm_v7s_map(struct io_pgtable_ops *ops, unsigned long iova, |
| 452 | phys_addr_t paddr, size_t size, int prot) |
| 453 | { |
| 454 | struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 455 | struct io_pgtable *iop = &data->iop; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 456 | int ret; |
| 457 | |
| 458 | /* If no access, then nothing to do */ |
| 459 | if (!(prot & (IOMMU_READ | IOMMU_WRITE))) |
| 460 | return 0; |
| 461 | |
| 462 | ret = __arm_v7s_map(data, iova, paddr, size, prot, 1, data->pgd); |
| 463 | /* |
| 464 | * Synchronise all PTE updates for the new mapping before there's |
| 465 | * a chance for anything to kick off a table walk for the new iova. |
| 466 | */ |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 467 | if (iop->cfg.quirks & IO_PGTABLE_QUIRK_TLBI_ON_MAP) { |
| 468 | io_pgtable_tlb_add_flush(iop, iova, size, |
| 469 | ARM_V7S_BLOCK_SIZE(2), false); |
| 470 | io_pgtable_tlb_sync(iop); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 471 | } else { |
| 472 | wmb(); |
| 473 | } |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | static void arm_v7s_free_pgtable(struct io_pgtable *iop) |
| 479 | { |
| 480 | struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop); |
| 481 | int i; |
| 482 | |
| 483 | for (i = 0; i < ARM_V7S_PTES_PER_LVL(1); i++) { |
| 484 | arm_v7s_iopte pte = data->pgd[i]; |
| 485 | |
| 486 | if (ARM_V7S_PTE_IS_TABLE(pte, 1)) |
| 487 | __arm_v7s_free_table(iopte_deref(pte, 1), 2, data); |
| 488 | } |
| 489 | __arm_v7s_free_table(data->pgd, 1, data); |
| 490 | kmem_cache_destroy(data->l2_tables); |
| 491 | kfree(data); |
| 492 | } |
| 493 | |
| 494 | static void arm_v7s_split_cont(struct arm_v7s_io_pgtable *data, |
| 495 | unsigned long iova, int idx, int lvl, |
| 496 | arm_v7s_iopte *ptep) |
| 497 | { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 498 | struct io_pgtable *iop = &data->iop; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 499 | arm_v7s_iopte pte; |
| 500 | size_t size = ARM_V7S_BLOCK_SIZE(lvl); |
| 501 | int i; |
| 502 | |
| 503 | ptep -= idx & (ARM_V7S_CONT_PAGES - 1); |
| 504 | pte = arm_v7s_cont_to_pte(*ptep, lvl); |
| 505 | for (i = 0; i < ARM_V7S_CONT_PAGES; i++) { |
| 506 | ptep[i] = pte; |
| 507 | pte += size; |
| 508 | } |
| 509 | |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 510 | __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 511 | |
| 512 | size *= ARM_V7S_CONT_PAGES; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 513 | io_pgtable_tlb_add_flush(iop, iova, size, size, true); |
| 514 | io_pgtable_tlb_sync(iop); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static int arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data, |
| 518 | unsigned long iova, size_t size, |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 519 | arm_v7s_iopte blk_pte, arm_v7s_iopte *ptep) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 520 | { |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 521 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 522 | arm_v7s_iopte pte, *tablep; |
| 523 | int i, unmap_idx, num_entries, num_ptes; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 524 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 525 | tablep = __arm_v7s_alloc_table(2, GFP_ATOMIC, data); |
| 526 | if (!tablep) |
| 527 | return 0; /* Bytes unmapped */ |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 528 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 529 | num_ptes = ARM_V7S_PTES_PER_LVL(2); |
| 530 | num_entries = size >> ARM_V7S_LVL_SHIFT(2); |
| 531 | unmap_idx = ARM_V7S_LVL_IDX(iova, 2); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 532 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 533 | pte = arm_v7s_prot_to_pte(arm_v7s_pte_to_prot(blk_pte, 1), 2, cfg); |
| 534 | if (num_entries > 1) |
| 535 | pte = arm_v7s_pte_to_cont(pte, 2); |
| 536 | |
| 537 | for (i = 0; i < num_ptes; i += num_entries, pte += size) { |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 538 | /* Unmap! */ |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 539 | if (i == unmap_idx) |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 540 | continue; |
| 541 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 542 | __arm_v7s_set_pte(&tablep[i], pte, num_entries, cfg); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 545 | arm_v7s_install_table(tablep, ptep, cfg); |
| 546 | |
| 547 | io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 548 | return size; |
| 549 | } |
| 550 | |
| 551 | static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *data, |
| 552 | unsigned long iova, size_t size, int lvl, |
| 553 | arm_v7s_iopte *ptep) |
| 554 | { |
| 555 | arm_v7s_iopte pte[ARM_V7S_CONT_PAGES]; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 556 | struct io_pgtable *iop = &data->iop; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 557 | int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl); |
| 558 | |
| 559 | /* Something went horribly wrong and we ran out of page table */ |
| 560 | if (WARN_ON(lvl > 2)) |
| 561 | return 0; |
| 562 | |
| 563 | idx = ARM_V7S_LVL_IDX(iova, lvl); |
| 564 | ptep += idx; |
| 565 | do { |
| 566 | if (WARN_ON(!ARM_V7S_PTE_IS_VALID(ptep[i]))) |
| 567 | return 0; |
| 568 | pte[i] = ptep[i]; |
| 569 | } while (++i < num_entries); |
| 570 | |
| 571 | /* |
| 572 | * If we've hit a contiguous 'large page' entry at this level, it |
| 573 | * needs splitting first, unless we're unmapping the whole lot. |
| 574 | */ |
| 575 | if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl)) |
| 576 | arm_v7s_split_cont(data, iova, idx, lvl, ptep); |
| 577 | |
| 578 | /* If the size matches this level, we're in the right place */ |
| 579 | if (num_entries) { |
| 580 | size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl); |
| 581 | |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 582 | __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 583 | |
| 584 | for (i = 0; i < num_entries; i++) { |
| 585 | if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) { |
| 586 | /* Also flush any partial walks */ |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 587 | io_pgtable_tlb_add_flush(iop, iova, blk_size, |
| 588 | ARM_V7S_BLOCK_SIZE(lvl + 1), false); |
| 589 | io_pgtable_tlb_sync(iop); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 590 | ptep = iopte_deref(pte[i], lvl); |
| 591 | __arm_v7s_free_table(ptep, lvl + 1, data); |
| 592 | } else { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 593 | io_pgtable_tlb_add_flush(iop, iova, blk_size, |
| 594 | blk_size, true); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 595 | } |
| 596 | iova += blk_size; |
| 597 | } |
| 598 | return size; |
| 599 | } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) { |
| 600 | /* |
| 601 | * Insert a table at the next level to map the old region, |
| 602 | * minus the part we want to unmap |
| 603 | */ |
Robin Murphy | b9f1ef3 | 2017-06-22 16:53:52 +0100 | [diff] [blame] | 604 | return arm_v7s_split_blk_unmap(data, iova, size, pte[0], ptep); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* Keep on walkin' */ |
| 608 | ptep = iopte_deref(pte[0], lvl); |
| 609 | return __arm_v7s_unmap(data, iova, size, lvl + 1, ptep); |
| 610 | } |
| 611 | |
| 612 | static int arm_v7s_unmap(struct io_pgtable_ops *ops, unsigned long iova, |
| 613 | size_t size) |
| 614 | { |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 615 | struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 616 | size_t unmapped; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 617 | |
| 618 | unmapped = __arm_v7s_unmap(data, iova, size, 1, data->pgd); |
| 619 | if (unmapped) |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 620 | io_pgtable_tlb_sync(&data->iop); |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 621 | |
| 622 | return unmapped; |
| 623 | } |
| 624 | |
| 625 | static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops, |
| 626 | unsigned long iova) |
| 627 | { |
| 628 | struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 629 | arm_v7s_iopte *ptep = data->pgd, pte; |
| 630 | int lvl = 0; |
| 631 | u32 mask; |
| 632 | |
| 633 | do { |
| 634 | pte = ptep[ARM_V7S_LVL_IDX(iova, ++lvl)]; |
| 635 | ptep = iopte_deref(pte, lvl); |
| 636 | } while (ARM_V7S_PTE_IS_TABLE(pte, lvl)); |
| 637 | |
| 638 | if (!ARM_V7S_PTE_IS_VALID(pte)) |
| 639 | return 0; |
| 640 | |
| 641 | mask = ARM_V7S_LVL_MASK(lvl); |
| 642 | if (arm_v7s_pte_is_cont(pte, lvl)) |
| 643 | mask *= ARM_V7S_CONT_PAGES; |
| 644 | return (pte & mask) | (iova & ~mask); |
| 645 | } |
| 646 | |
| 647 | static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg, |
| 648 | void *cookie) |
| 649 | { |
| 650 | struct arm_v7s_io_pgtable *data; |
| 651 | |
Robin Murphy | 82db33dc | 2016-09-13 18:02:02 +0100 | [diff] [blame] | 652 | #ifdef PHYS_OFFSET |
| 653 | if (upper_32_bits(PHYS_OFFSET)) |
| 654 | return NULL; |
| 655 | #endif |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 656 | if (cfg->ias > ARM_V7S_ADDR_BITS || cfg->oas > ARM_V7S_ADDR_BITS) |
| 657 | return NULL; |
| 658 | |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 659 | if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | |
| 660 | IO_PGTABLE_QUIRK_NO_PERMS | |
Yong Wu | 1afe231 | 2016-03-14 06:01:10 +0800 | [diff] [blame] | 661 | IO_PGTABLE_QUIRK_TLBI_ON_MAP | |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 662 | IO_PGTABLE_QUIRK_ARM_MTK_4GB | |
| 663 | IO_PGTABLE_QUIRK_NO_DMA)) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 664 | return NULL; |
| 665 | |
Yong Wu | 1afe231 | 2016-03-14 06:01:10 +0800 | [diff] [blame] | 666 | /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */ |
| 667 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB && |
| 668 | !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS)) |
| 669 | return NULL; |
| 670 | |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 671 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 672 | if (!data) |
| 673 | return NULL; |
| 674 | |
| 675 | data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2", |
| 676 | ARM_V7S_TABLE_SIZE(2), |
| 677 | ARM_V7S_TABLE_SIZE(2), |
| 678 | SLAB_CACHE_DMA, NULL); |
| 679 | if (!data->l2_tables) |
| 680 | goto out_free_data; |
| 681 | |
| 682 | data->iop.ops = (struct io_pgtable_ops) { |
| 683 | .map = arm_v7s_map, |
| 684 | .unmap = arm_v7s_unmap, |
| 685 | .iova_to_phys = arm_v7s_iova_to_phys, |
| 686 | }; |
| 687 | |
| 688 | /* We have to do this early for __arm_v7s_alloc_table to work... */ |
| 689 | data->iop.cfg = *cfg; |
| 690 | |
| 691 | /* |
| 692 | * Unless the IOMMU driver indicates supersection support by |
| 693 | * having SZ_16M set in the initial bitmap, they won't be used. |
| 694 | */ |
| 695 | cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M; |
| 696 | |
| 697 | /* TCR: T0SZ=0, disable TTBR1 */ |
| 698 | cfg->arm_v7s_cfg.tcr = ARM_V7S_TCR_PD1; |
| 699 | |
| 700 | /* |
| 701 | * TEX remap: the indices used map to the closest equivalent types |
| 702 | * under the non-TEX-remap interpretation of those attribute bits, |
| 703 | * excepting various implementation-defined aspects of shareability. |
| 704 | */ |
| 705 | cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) | |
| 706 | ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) | |
| 707 | ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) | |
| 708 | ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 | |
| 709 | ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7); |
| 710 | cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) | |
| 711 | ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA); |
| 712 | |
| 713 | /* Looking good; allocate a pgd */ |
| 714 | data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data); |
| 715 | if (!data->pgd) |
| 716 | goto out_free_data; |
| 717 | |
| 718 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 719 | wmb(); |
| 720 | |
| 721 | /* TTBRs */ |
| 722 | cfg->arm_v7s_cfg.ttbr[0] = virt_to_phys(data->pgd) | |
| 723 | ARM_V7S_TTBR_S | ARM_V7S_TTBR_NOS | |
| 724 | ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) | |
| 725 | ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA); |
| 726 | cfg->arm_v7s_cfg.ttbr[1] = 0; |
| 727 | return &data->iop; |
| 728 | |
| 729 | out_free_data: |
| 730 | kmem_cache_destroy(data->l2_tables); |
| 731 | kfree(data); |
| 732 | return NULL; |
| 733 | } |
| 734 | |
| 735 | struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = { |
| 736 | .alloc = arm_v7s_alloc_pgtable, |
| 737 | .free = arm_v7s_free_pgtable, |
| 738 | }; |
| 739 | |
| 740 | #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST |
| 741 | |
| 742 | static struct io_pgtable_cfg *cfg_cookie; |
| 743 | |
| 744 | static void dummy_tlb_flush_all(void *cookie) |
| 745 | { |
| 746 | WARN_ON(cookie != cfg_cookie); |
| 747 | } |
| 748 | |
| 749 | static void dummy_tlb_add_flush(unsigned long iova, size_t size, |
| 750 | size_t granule, bool leaf, void *cookie) |
| 751 | { |
| 752 | WARN_ON(cookie != cfg_cookie); |
| 753 | WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); |
| 754 | } |
| 755 | |
| 756 | static void dummy_tlb_sync(void *cookie) |
| 757 | { |
| 758 | WARN_ON(cookie != cfg_cookie); |
| 759 | } |
| 760 | |
Arvind Yadav | 60ab7a7 | 2017-06-13 15:58:30 +0530 | [diff] [blame] | 761 | static const struct iommu_gather_ops dummy_tlb_ops = { |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 762 | .tlb_flush_all = dummy_tlb_flush_all, |
| 763 | .tlb_add_flush = dummy_tlb_add_flush, |
| 764 | .tlb_sync = dummy_tlb_sync, |
| 765 | }; |
| 766 | |
| 767 | #define __FAIL(ops) ({ \ |
| 768 | WARN(1, "selftest: test failed\n"); \ |
| 769 | selftest_running = false; \ |
| 770 | -EFAULT; \ |
| 771 | }) |
| 772 | |
| 773 | static int __init arm_v7s_do_selftests(void) |
| 774 | { |
| 775 | struct io_pgtable_ops *ops; |
| 776 | struct io_pgtable_cfg cfg = { |
| 777 | .tlb = &dummy_tlb_ops, |
| 778 | .oas = 32, |
| 779 | .ias = 32, |
Robin Murphy | 81b3c25 | 2017-06-22 16:53:53 +0100 | [diff] [blame^] | 780 | .quirks = IO_PGTABLE_QUIRK_ARM_NS | IO_PGTABLE_QUIRK_NO_DMA, |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 781 | .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M, |
| 782 | }; |
| 783 | unsigned int iova, size, iova_start; |
| 784 | unsigned int i, loopnr = 0; |
| 785 | |
| 786 | selftest_running = true; |
| 787 | |
| 788 | cfg_cookie = &cfg; |
| 789 | |
| 790 | ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg); |
| 791 | if (!ops) { |
| 792 | pr_err("selftest: failed to allocate io pgtable ops\n"); |
| 793 | return -EINVAL; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Initial sanity checks. |
| 798 | * Empty page tables shouldn't provide any translations. |
| 799 | */ |
| 800 | if (ops->iova_to_phys(ops, 42)) |
| 801 | return __FAIL(ops); |
| 802 | |
| 803 | if (ops->iova_to_phys(ops, SZ_1G + 42)) |
| 804 | return __FAIL(ops); |
| 805 | |
| 806 | if (ops->iova_to_phys(ops, SZ_2G + 42)) |
| 807 | return __FAIL(ops); |
| 808 | |
| 809 | /* |
| 810 | * Distinct mappings of different granule sizes. |
| 811 | */ |
| 812 | iova = 0; |
Kefeng Wang | 4ae8a5c | 2016-09-21 13:41:31 +0800 | [diff] [blame] | 813 | for_each_set_bit(i, &cfg.pgsize_bitmap, BITS_PER_LONG) { |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 814 | size = 1UL << i; |
| 815 | if (ops->map(ops, iova, iova, size, IOMMU_READ | |
| 816 | IOMMU_WRITE | |
| 817 | IOMMU_NOEXEC | |
| 818 | IOMMU_CACHE)) |
| 819 | return __FAIL(ops); |
| 820 | |
| 821 | /* Overlapping mappings */ |
| 822 | if (!ops->map(ops, iova, iova + size, size, |
| 823 | IOMMU_READ | IOMMU_NOEXEC)) |
| 824 | return __FAIL(ops); |
| 825 | |
| 826 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 827 | return __FAIL(ops); |
| 828 | |
| 829 | iova += SZ_16M; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 830 | loopnr++; |
| 831 | } |
| 832 | |
| 833 | /* Partial unmap */ |
| 834 | i = 1; |
| 835 | size = 1UL << __ffs(cfg.pgsize_bitmap); |
| 836 | while (i < loopnr) { |
| 837 | iova_start = i * SZ_16M; |
| 838 | if (ops->unmap(ops, iova_start + size, size) != size) |
| 839 | return __FAIL(ops); |
| 840 | |
| 841 | /* Remap of partial unmap */ |
| 842 | if (ops->map(ops, iova_start + size, size, size, IOMMU_READ)) |
| 843 | return __FAIL(ops); |
| 844 | |
| 845 | if (ops->iova_to_phys(ops, iova_start + size + 42) |
| 846 | != (size + 42)) |
| 847 | return __FAIL(ops); |
| 848 | i++; |
| 849 | } |
| 850 | |
| 851 | /* Full unmap */ |
| 852 | iova = 0; |
| 853 | i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG); |
| 854 | while (i != BITS_PER_LONG) { |
| 855 | size = 1UL << i; |
| 856 | |
| 857 | if (ops->unmap(ops, iova, size) != size) |
| 858 | return __FAIL(ops); |
| 859 | |
| 860 | if (ops->iova_to_phys(ops, iova + 42)) |
| 861 | return __FAIL(ops); |
| 862 | |
| 863 | /* Remap full block */ |
| 864 | if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) |
| 865 | return __FAIL(ops); |
| 866 | |
| 867 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 868 | return __FAIL(ops); |
| 869 | |
| 870 | iova += SZ_16M; |
| 871 | i++; |
| 872 | i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i); |
| 873 | } |
| 874 | |
| 875 | free_io_pgtable_ops(ops); |
| 876 | |
| 877 | selftest_running = false; |
| 878 | |
| 879 | pr_info("self test ok\n"); |
| 880 | return 0; |
| 881 | } |
| 882 | subsys_initcall(arm_v7s_do_selftests); |
| 883 | #endif |