Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 2 | /* |
| 3 | * CPU-agnostic ARM page table allocator. |
| 4 | * |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 5 | * Copyright (C) 2014 ARM Limited |
| 6 | * |
| 7 | * Author: Will Deacon <will.deacon@arm.com> |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt |
| 11 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 12 | #include <linux/atomic.h> |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Rob Herring | b77cf11 | 2019-02-05 10:37:31 -0600 | [diff] [blame] | 14 | #include <linux/io-pgtable.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 15 | #include <linux/kernel.h> |
| 16 | #include <linux/sizes.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/types.h> |
Lada Trimasova | 8f6aff9 | 2016-01-27 11:10:32 +0000 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 20 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 21 | #include <asm/barrier.h> |
| 22 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 23 | #define ARM_LPAE_MAX_ADDR_BITS 52 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 24 | #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 |
| 25 | #define ARM_LPAE_MAX_LEVELS 4 |
| 26 | |
| 27 | /* Struct accessors */ |
| 28 | #define io_pgtable_to_data(x) \ |
| 29 | container_of((x), struct arm_lpae_io_pgtable, iop) |
| 30 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 31 | #define io_pgtable_ops_to_data(x) \ |
| 32 | io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) |
| 33 | |
| 34 | /* |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 35 | * Calculate the right shift amount to get to the portion describing level l |
| 36 | * in a virtual address mapped by the pagetable in d. |
| 37 | */ |
| 38 | #define ARM_LPAE_LVL_SHIFT(l,d) \ |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 39 | (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \ |
| 40 | ilog2(sizeof(arm_lpae_iopte))) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 41 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 42 | #define ARM_LPAE_GRANULE(d) \ |
| 43 | (sizeof(arm_lpae_iopte) << (d)->bits_per_level) |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 44 | #define ARM_LPAE_PGD_SIZE(d) \ |
| 45 | (sizeof(arm_lpae_iopte) << (d)->pgd_bits) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Calculate the index at level l used to map virtual address a using the |
| 49 | * pagetable in d. |
| 50 | */ |
| 51 | #define ARM_LPAE_PGD_IDX(l,d) \ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 52 | ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 53 | |
| 54 | #define ARM_LPAE_LVL_IDX(a,l,d) \ |
Will Deacon | 367bd97 | 2015-02-16 18:38:20 +0000 | [diff] [blame] | 55 | (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 56 | ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) |
| 57 | |
| 58 | /* Calculate the block/page mapping size at level l for pagetable in d. */ |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 59 | #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 60 | |
| 61 | /* Page table bits */ |
| 62 | #define ARM_LPAE_PTE_TYPE_SHIFT 0 |
| 63 | #define ARM_LPAE_PTE_TYPE_MASK 0x3 |
| 64 | |
| 65 | #define ARM_LPAE_PTE_TYPE_BLOCK 1 |
| 66 | #define ARM_LPAE_PTE_TYPE_TABLE 3 |
| 67 | #define ARM_LPAE_PTE_TYPE_PAGE 3 |
| 68 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 69 | #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) |
| 70 | |
Laurent Pinchart | c896c132 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 71 | #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 72 | #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) |
| 73 | #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) |
| 74 | #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) |
| 75 | #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) |
| 76 | #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) |
Laurent Pinchart | c896c132 | 2014-12-14 23:34:50 +0200 | [diff] [blame] | 77 | #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 78 | #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) |
| 79 | |
| 80 | #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) |
| 81 | /* Ignore the contiguous bit for block splitting */ |
| 82 | #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) |
| 83 | #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ |
| 84 | ARM_LPAE_PTE_ATTR_HI_MASK) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 85 | /* Software bit for solving coherency races */ |
| 86 | #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 87 | |
| 88 | /* Stage-1 PTE */ |
| 89 | #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) |
| 90 | #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) |
| 91 | #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 |
| 92 | #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) |
| 93 | |
| 94 | /* Stage-2 PTE */ |
| 95 | #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) |
| 96 | #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) |
| 97 | #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) |
| 98 | #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) |
| 99 | #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) |
| 100 | #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) |
| 101 | |
| 102 | /* Register bits */ |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 103 | #define ARM_LPAE_TCR_TG0_4K 0 |
| 104 | #define ARM_LPAE_TCR_TG0_64K 1 |
| 105 | #define ARM_LPAE_TCR_TG0_16K 2 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 106 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 107 | #define ARM_LPAE_TCR_TG1_16K 1 |
| 108 | #define ARM_LPAE_TCR_TG1_4K 2 |
| 109 | #define ARM_LPAE_TCR_TG1_64K 3 |
| 110 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 111 | #define ARM_LPAE_TCR_SH_NS 0 |
| 112 | #define ARM_LPAE_TCR_SH_OS 2 |
| 113 | #define ARM_LPAE_TCR_SH_IS 3 |
| 114 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 115 | #define ARM_LPAE_TCR_RGN_NC 0 |
| 116 | #define ARM_LPAE_TCR_RGN_WBWA 1 |
| 117 | #define ARM_LPAE_TCR_RGN_WT 2 |
| 118 | #define ARM_LPAE_TCR_RGN_WB 3 |
| 119 | |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 120 | #define ARM_LPAE_VTCR_SL0_MASK 0x3 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 121 | |
| 122 | #define ARM_LPAE_TCR_T0SZ_SHIFT 0 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 123 | |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 124 | #define ARM_LPAE_VTCR_PS_SHIFT 16 |
| 125 | #define ARM_LPAE_VTCR_PS_MASK 0x7 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 126 | |
| 127 | #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL |
| 128 | #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL |
| 129 | #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL |
| 130 | #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL |
| 131 | #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL |
| 132 | #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 133 | #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 134 | |
| 135 | #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) |
| 136 | #define ARM_LPAE_MAIR_ATTR_MASK 0xff |
| 137 | #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 |
| 138 | #define ARM_LPAE_MAIR_ATTR_NC 0x44 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 139 | #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 140 | #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff |
| 141 | #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 |
| 142 | #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 |
| 143 | #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 144 | #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 145 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 146 | #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) |
| 147 | #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) |
| 148 | #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) |
| 149 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 150 | #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL |
| 151 | #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL |
| 152 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 153 | /* IOPTE accessors */ |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 154 | #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 155 | |
| 156 | #define iopte_type(pte,l) \ |
| 157 | (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) |
| 158 | |
| 159 | #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) |
| 160 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 161 | struct arm_lpae_io_pgtable { |
| 162 | struct io_pgtable iop; |
| 163 | |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 164 | int pgd_bits; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 165 | int start_level; |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 166 | int bits_per_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 167 | |
| 168 | void *pgd; |
| 169 | }; |
| 170 | |
| 171 | typedef u64 arm_lpae_iopte; |
| 172 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 173 | static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, |
| 174 | enum io_pgtable_fmt fmt) |
| 175 | { |
| 176 | if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) |
| 177 | return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE; |
| 178 | |
| 179 | return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK; |
| 180 | } |
| 181 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 182 | static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, |
| 183 | struct arm_lpae_io_pgtable *data) |
| 184 | { |
| 185 | arm_lpae_iopte pte = paddr; |
| 186 | |
| 187 | /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ |
| 188 | return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; |
| 189 | } |
| 190 | |
| 191 | static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, |
| 192 | struct arm_lpae_io_pgtable *data) |
| 193 | { |
Robin Murphy | 7868805 | 2018-03-29 12:24:52 +0100 | [diff] [blame] | 194 | u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 195 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 196 | if (ARM_LPAE_GRANULE(data) < SZ_64K) |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 197 | return paddr; |
| 198 | |
| 199 | /* Rotate the packed high-order bits back to the top */ |
| 200 | return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); |
| 201 | } |
| 202 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 203 | static bool selftest_running = false; |
| 204 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 205 | static dma_addr_t __arm_lpae_dma_addr(void *pages) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 206 | { |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 207 | return (dma_addr_t)virt_to_phys(pages); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, |
| 211 | struct io_pgtable_cfg *cfg) |
| 212 | { |
| 213 | struct device *dev = cfg->iommu_dev; |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 214 | int order = get_order(size); |
| 215 | struct page *p; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 216 | dma_addr_t dma; |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 217 | void *pages; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 218 | |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 219 | VM_BUG_ON((gfp & __GFP_HIGHMEM)); |
Jean-Philippe Brucker | fac83d2 | 2018-06-18 12:27:54 +0100 | [diff] [blame] | 220 | p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, |
| 221 | gfp | __GFP_ZERO, order); |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 222 | if (!p) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 223 | return NULL; |
| 224 | |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 225 | pages = page_address(p); |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 226 | if (!cfg->coherent_walk) { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 227 | dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); |
| 228 | if (dma_mapping_error(dev, dma)) |
| 229 | goto out_free; |
| 230 | /* |
| 231 | * We depend on the IOMMU being able to work with any physical |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 232 | * address directly, so if the DMA layer suggests otherwise by |
| 233 | * translating or truncating them, that bodes very badly... |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 234 | */ |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 235 | if (dma != virt_to_phys(pages)) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 236 | goto out_unmap; |
| 237 | } |
| 238 | |
| 239 | return pages; |
| 240 | |
| 241 | out_unmap: |
| 242 | dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); |
| 243 | dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); |
| 244 | out_free: |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 245 | __free_pages(p, order); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | static void __arm_lpae_free_pages(void *pages, size_t size, |
| 250 | struct io_pgtable_cfg *cfg) |
| 251 | { |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 252 | if (!cfg->coherent_walk) |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 253 | dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 254 | size, DMA_TO_DEVICE); |
Robin Murphy | 4b12375 | 2018-05-22 12:50:09 +0100 | [diff] [blame] | 255 | free_pages((unsigned long)pages, get_order(size)); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 258 | static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, |
| 259 | struct io_pgtable_cfg *cfg) |
| 260 | { |
| 261 | dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), |
| 262 | sizeof(*ptep), DMA_TO_DEVICE); |
| 263 | } |
| 264 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 265 | 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] | 266 | struct io_pgtable_cfg *cfg) |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 267 | { |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 268 | *ptep = pte; |
| 269 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 270 | if (!cfg->coherent_walk) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 271 | __arm_lpae_sync_pte(ptep, cfg); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 274 | static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 275 | struct iommu_iotlb_gather *gather, |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 276 | unsigned long iova, size_t size, int lvl, |
| 277 | arm_lpae_iopte *ptep); |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 278 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 279 | static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 280 | phys_addr_t paddr, arm_lpae_iopte prot, |
| 281 | int lvl, arm_lpae_iopte *ptep) |
| 282 | { |
| 283 | arm_lpae_iopte pte = prot; |
| 284 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 285 | if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 286 | pte |= ARM_LPAE_PTE_TYPE_PAGE; |
| 287 | else |
| 288 | pte |= ARM_LPAE_PTE_TYPE_BLOCK; |
| 289 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 290 | pte |= paddr_to_iopte(paddr, data); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 291 | |
| 292 | __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); |
| 293 | } |
| 294 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 295 | static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, |
| 296 | unsigned long iova, phys_addr_t paddr, |
| 297 | arm_lpae_iopte prot, int lvl, |
| 298 | arm_lpae_iopte *ptep) |
| 299 | { |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 300 | arm_lpae_iopte pte = *ptep; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 301 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 302 | if (iopte_leaf(pte, lvl, data->iop.fmt)) { |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 303 | /* We require an unmap first */ |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 304 | WARN_ON(!selftest_running); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 305 | return -EEXIST; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 306 | } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 307 | /* |
| 308 | * We need to unmap and free the old table before |
| 309 | * overwriting it with a block entry. |
| 310 | */ |
| 311 | arm_lpae_iopte *tblp; |
| 312 | size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 313 | |
| 314 | tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 315 | if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) { |
| 316 | WARN_ON(1); |
Will Deacon | cf27ec9 | 2015-08-11 16:48:32 +0100 | [diff] [blame] | 317 | return -EINVAL; |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 318 | } |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 319 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 320 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 321 | __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 325 | static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, |
| 326 | arm_lpae_iopte *ptep, |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 327 | arm_lpae_iopte curr, |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 328 | struct io_pgtable_cfg *cfg) |
| 329 | { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 330 | arm_lpae_iopte old, new; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 331 | |
| 332 | new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; |
| 333 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 334 | new |= ARM_LPAE_PTE_NSTABLE; |
| 335 | |
Will Deacon | 77f3445 | 2017-06-23 12:02:38 +0100 | [diff] [blame] | 336 | /* |
| 337 | * Ensure the table itself is visible before its PTE can be. |
| 338 | * Whilst we could get away with cmpxchg64_release below, this |
| 339 | * doesn't have any ordering semantics when !CONFIG_SMP. |
| 340 | */ |
| 341 | dma_wmb(); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 342 | |
| 343 | old = cmpxchg64_relaxed(ptep, curr, new); |
| 344 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 345 | if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 346 | return old; |
| 347 | |
| 348 | /* Even if it's not ours, there's no point waiting; just kick it */ |
| 349 | __arm_lpae_sync_pte(ptep, cfg); |
| 350 | if (old == curr) |
| 351 | WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); |
| 352 | |
| 353 | return old; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 356 | static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, |
| 357 | phys_addr_t paddr, size_t size, arm_lpae_iopte prot, |
| 358 | int lvl, arm_lpae_iopte *ptep) |
| 359 | { |
| 360 | arm_lpae_iopte *cptep, pte; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 361 | size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 362 | size_t tblsz = ARM_LPAE_GRANULE(data); |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 363 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 364 | |
| 365 | /* Find our entry at the current level */ |
| 366 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 367 | |
| 368 | /* If we can install a leaf entry at this level, then do so */ |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 369 | if (size == block_size) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 370 | return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); |
| 371 | |
| 372 | /* We can't allocate tables at the final level */ |
| 373 | if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | /* Grab a pointer to the next level */ |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 377 | pte = READ_ONCE(*ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 378 | if (!pte) { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 379 | cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 380 | if (!cptep) |
| 381 | return -ENOMEM; |
| 382 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 383 | pte = arm_lpae_install_table(cptep, ptep, 0, cfg); |
| 384 | if (pte) |
| 385 | __arm_lpae_free_pages(cptep, tblsz, cfg); |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 386 | } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 387 | __arm_lpae_sync_pte(ptep, cfg); |
| 388 | } |
| 389 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 390 | if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 391 | cptep = iopte_deref(pte, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 392 | } else if (pte) { |
Oleksandr Tyshchenko | ed46e66 | 2017-02-27 14:30:25 +0200 | [diff] [blame] | 393 | /* We require an unmap first */ |
| 394 | WARN_ON(!selftest_running); |
| 395 | return -EEXIST; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* Rinse, repeat */ |
| 399 | return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); |
| 400 | } |
| 401 | |
| 402 | static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, |
| 403 | int prot) |
| 404 | { |
| 405 | arm_lpae_iopte pte; |
| 406 | |
| 407 | if (data->iop.fmt == ARM_64_LPAE_S1 || |
| 408 | data->iop.fmt == ARM_32_LPAE_S1) { |
Jeremy Gebben | e7468a2 | 2017-01-06 18:58:09 +0530 | [diff] [blame] | 409 | pte = ARM_LPAE_PTE_nG; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 410 | if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) |
| 411 | pte |= ARM_LPAE_PTE_AP_RDONLY; |
Jeremy Gebben | e7468a2 | 2017-01-06 18:58:09 +0530 | [diff] [blame] | 412 | if (!(prot & IOMMU_PRIV)) |
| 413 | pte |= ARM_LPAE_PTE_AP_UNPRIV; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 414 | } else { |
| 415 | pte = ARM_LPAE_PTE_HAP_FAULT; |
| 416 | if (prot & IOMMU_READ) |
| 417 | pte |= ARM_LPAE_PTE_HAP_READ; |
| 418 | if (prot & IOMMU_WRITE) |
| 419 | pte |= ARM_LPAE_PTE_HAP_WRITE; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Note that this logic is structured to accommodate Mali LPAE |
| 424 | * having stage-1-like attributes but stage-2-like permissions. |
| 425 | */ |
| 426 | if (data->iop.fmt == ARM_64_LPAE_S2 || |
| 427 | data->iop.fmt == ARM_32_LPAE_S2) { |
Robin Murphy | fb94825 | 2016-04-05 12:39:31 +0100 | [diff] [blame] | 428 | if (prot & IOMMU_MMIO) |
| 429 | pte |= ARM_LPAE_PTE_MEMATTR_DEV; |
| 430 | else if (prot & IOMMU_CACHE) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 431 | pte |= ARM_LPAE_PTE_MEMATTR_OIWB; |
| 432 | else |
| 433 | pte |= ARM_LPAE_PTE_MEMATTR_NC; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 434 | } else { |
| 435 | if (prot & IOMMU_MMIO) |
| 436 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV |
| 437 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
| 438 | else if (prot & IOMMU_CACHE) |
| 439 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE |
| 440 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
Will Deacon | dd5ddd3 | 2019-10-24 16:57:39 +0100 | [diff] [blame] | 441 | else if (prot & IOMMU_SYS_CACHE_ONLY) |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 442 | pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE |
| 443 | << ARM_LPAE_PTE_ATTRINDX_SHIFT); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Robin Murphy | 7618e47 | 2020-01-10 15:21:51 +0000 | [diff] [blame] | 446 | if (prot & IOMMU_CACHE) |
| 447 | pte |= ARM_LPAE_PTE_SH_IS; |
| 448 | else |
| 449 | pte |= ARM_LPAE_PTE_SH_OS; |
| 450 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 451 | if (prot & IOMMU_NOEXEC) |
| 452 | pte |= ARM_LPAE_PTE_XN; |
| 453 | |
Robin Murphy | 7618e47 | 2020-01-10 15:21:51 +0000 | [diff] [blame] | 454 | if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) |
| 455 | pte |= ARM_LPAE_PTE_NS; |
| 456 | |
| 457 | if (data->iop.fmt != ARM_MALI_LPAE) |
| 458 | pte |= ARM_LPAE_PTE_AF; |
| 459 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 460 | return pte; |
| 461 | } |
| 462 | |
| 463 | static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, |
| 464 | phys_addr_t paddr, size_t size, int iommu_prot) |
| 465 | { |
| 466 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 467 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 468 | arm_lpae_iopte *ptep = data->pgd; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 469 | int ret, lvl = data->start_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 470 | arm_lpae_iopte prot; |
Robin Murphy | 0809074 | 2020-02-28 14:18:55 +0000 | [diff] [blame] | 471 | long iaext = (s64)iova >> cfg->ias; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 472 | |
| 473 | /* If no access, then nothing to do */ |
| 474 | if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) |
| 475 | return 0; |
| 476 | |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 477 | if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) |
| 478 | return -EINVAL; |
| 479 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 480 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) |
| 481 | iaext = ~iaext; |
| 482 | if (WARN_ON(iaext || paddr >> cfg->oas)) |
Robin Murphy | 7655739 | 2017-07-03 14:52:24 +0100 | [diff] [blame] | 483 | return -ERANGE; |
| 484 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 485 | prot = arm_lpae_prot_to_pte(data, iommu_prot); |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 486 | ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); |
| 487 | /* |
| 488 | * Synchronise all PTE updates for the new mapping before there's |
| 489 | * a chance for anything to kick off a table walk for the new iova. |
| 490 | */ |
| 491 | wmb(); |
| 492 | |
| 493 | return ret; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, |
| 497 | arm_lpae_iopte *ptep) |
| 498 | { |
| 499 | arm_lpae_iopte *start, *end; |
| 500 | unsigned long table_size; |
| 501 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 502 | if (lvl == data->start_level) |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 503 | table_size = ARM_LPAE_PGD_SIZE(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 504 | else |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 505 | table_size = ARM_LPAE_GRANULE(data); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 506 | |
| 507 | start = ptep; |
Will Deacon | 12c2ab0 | 2015-12-15 16:08:12 +0000 | [diff] [blame] | 508 | |
| 509 | /* Only leaf entries at the last level */ |
| 510 | if (lvl == ARM_LPAE_MAX_LEVELS - 1) |
| 511 | end = ptep; |
| 512 | else |
| 513 | end = (void *)ptep + table_size; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 514 | |
| 515 | while (ptep != end) { |
| 516 | arm_lpae_iopte pte = *ptep++; |
| 517 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 518 | if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 519 | continue; |
| 520 | |
| 521 | __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); |
| 522 | } |
| 523 | |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 524 | __arm_lpae_free_pages(start, table_size, &data->iop.cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static void arm_lpae_free_pgtable(struct io_pgtable *iop) |
| 528 | { |
| 529 | struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); |
| 530 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 531 | __arm_lpae_free_pgtable(data, data->start_level, data->pgd); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 532 | kfree(data); |
| 533 | } |
| 534 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 535 | static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 536 | struct iommu_iotlb_gather *gather, |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 537 | unsigned long iova, size_t size, |
| 538 | arm_lpae_iopte blk_pte, int lvl, |
| 539 | arm_lpae_iopte *ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 540 | { |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 541 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 542 | arm_lpae_iopte pte, *tablep; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 543 | phys_addr_t blk_paddr; |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 544 | size_t tablesz = ARM_LPAE_GRANULE(data); |
| 545 | size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); |
| 546 | int i, unmap_idx = -1; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 547 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 548 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 549 | return 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 550 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 551 | tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); |
| 552 | if (!tablep) |
| 553 | return 0; /* Bytes unmapped */ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 554 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 555 | if (size == split_sz) |
| 556 | unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 557 | |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 558 | blk_paddr = iopte_to_paddr(blk_pte, data); |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 559 | pte = iopte_prot(blk_pte); |
| 560 | |
| 561 | for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 562 | /* Unmap! */ |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 563 | if (i == unmap_idx) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 564 | continue; |
| 565 | |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 566 | __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 569 | pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); |
| 570 | if (pte != blk_pte) { |
| 571 | __arm_lpae_free_pages(tablep, tablesz, cfg); |
| 572 | /* |
| 573 | * We may race against someone unmapping another part of this |
| 574 | * block, but anything else is invalid. We can't misinterpret |
| 575 | * a page entry here since we're never at the last level. |
| 576 | */ |
| 577 | if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) |
| 578 | return 0; |
| 579 | |
| 580 | tablep = iopte_deref(pte, data); |
Robin Murphy | 85c7a0f | 2018-09-06 17:59:50 +0100 | [diff] [blame] | 581 | } else if (unmap_idx >= 0) { |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 582 | io_pgtable_tlb_add_page(&data->iop, gather, iova, size); |
Robin Murphy | 85c7a0f | 2018-09-06 17:59:50 +0100 | [diff] [blame] | 583 | return size; |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 584 | } |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 585 | |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 586 | return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 589 | static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 590 | struct iommu_iotlb_gather *gather, |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 591 | unsigned long iova, size_t size, int lvl, |
| 592 | arm_lpae_iopte *ptep) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 593 | { |
| 594 | arm_lpae_iopte pte; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 595 | struct io_pgtable *iop = &data->iop; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 596 | |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 597 | /* Something went horribly wrong and we ran out of page table */ |
| 598 | if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) |
| 599 | return 0; |
| 600 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 601 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 602 | pte = READ_ONCE(*ptep); |
Robin Murphy | 2eb97c7 | 2015-12-04 17:52:58 +0000 | [diff] [blame] | 603 | if (WARN_ON(!pte)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 604 | return 0; |
| 605 | |
| 606 | /* If the size matches this level, we're in the right place */ |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 607 | if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 608 | __arm_lpae_set_pte(ptep, 0, &iop->cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 609 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 610 | if (!iopte_leaf(pte, lvl, iop->fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 611 | /* Also flush any partial walks */ |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 612 | io_pgtable_tlb_flush_walk(iop, iova, size, |
| 613 | ARM_LPAE_GRANULE(data)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 614 | ptep = iopte_deref(pte, data); |
| 615 | __arm_lpae_free_pgtable(data, lvl + 1, ptep); |
Zhen Lei | b6b65ca | 2018-09-20 17:10:24 +0100 | [diff] [blame] | 616 | } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { |
| 617 | /* |
| 618 | * Order the PTE update against queueing the IOVA, to |
| 619 | * guarantee that a flush callback from a different CPU |
| 620 | * has observed it before the TLBIALL can be issued. |
| 621 | */ |
| 622 | smp_wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 623 | } else { |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 624 | io_pgtable_tlb_add_page(iop, gather, iova, size); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return size; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 628 | } else if (iopte_leaf(pte, lvl, iop->fmt)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 629 | /* |
| 630 | * Insert a table at the next level to map the old region, |
| 631 | * minus the part we want to unmap |
| 632 | */ |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 633 | return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, |
Robin Murphy | fb3a957 | 2017-06-22 16:53:51 +0100 | [diff] [blame] | 634 | lvl + 1, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* Keep on walkin' */ |
| 638 | ptep = iopte_deref(pte, data); |
Will Deacon | 3951c41 | 2019-07-02 16:45:15 +0100 | [diff] [blame] | 639 | return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Vivek Gautam | 193e67c | 2018-02-05 23:29:19 +0530 | [diff] [blame] | 642 | static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, |
Will Deacon | a2d3a38 | 2019-07-02 16:44:58 +0100 | [diff] [blame] | 643 | size_t size, struct iommu_iotlb_gather *gather) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 644 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 645 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 646 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 647 | arm_lpae_iopte *ptep = data->pgd; |
Robin Murphy | 0809074 | 2020-02-28 14:18:55 +0000 | [diff] [blame] | 648 | long iaext = (s64)iova >> cfg->ias; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 649 | |
Robin Murphy | f7b90d2 | 2019-10-25 19:08:31 +0100 | [diff] [blame] | 650 | if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) |
| 651 | return 0; |
| 652 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 653 | if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) |
| 654 | iaext = ~iaext; |
| 655 | if (WARN_ON(iaext)) |
Robin Murphy | 7655739 | 2017-07-03 14:52:24 +0100 | [diff] [blame] | 656 | return 0; |
| 657 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 658 | return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, |
| 662 | unsigned long iova) |
| 663 | { |
| 664 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 665 | arm_lpae_iopte pte, *ptep = data->pgd; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 666 | int lvl = data->start_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 667 | |
| 668 | do { |
| 669 | /* Valid IOPTE pointer? */ |
| 670 | if (!ptep) |
| 671 | return 0; |
| 672 | |
| 673 | /* Grab the IOPTE we're interested in */ |
Robin Murphy | 2c3d273 | 2017-06-22 16:53:54 +0100 | [diff] [blame] | 674 | ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); |
| 675 | pte = READ_ONCE(*ptep); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 676 | |
| 677 | /* Valid entry? */ |
| 678 | if (!pte) |
| 679 | return 0; |
| 680 | |
| 681 | /* Leaf entry? */ |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 682 | if (iopte_leaf(pte, lvl, data->iop.fmt)) |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 683 | goto found_translation; |
| 684 | |
| 685 | /* Take it to the next level */ |
| 686 | ptep = iopte_deref(pte, data); |
| 687 | } while (++lvl < ARM_LPAE_MAX_LEVELS); |
| 688 | |
| 689 | /* Ran out of page tables to walk */ |
| 690 | return 0; |
| 691 | |
| 692 | found_translation: |
Will Deacon | 7c6d90e | 2016-06-16 18:21:19 +0100 | [diff] [blame] | 693 | iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 694 | return iopte_to_paddr(pte, data) | iova; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) |
| 698 | { |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 699 | unsigned long granule, page_sizes; |
| 700 | unsigned int max_addr_bits = 48; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 701 | |
| 702 | /* |
| 703 | * We need to restrict the supported page sizes to match the |
| 704 | * translation regime for a particular granule. Aim to match |
| 705 | * the CPU page size if possible, otherwise prefer smaller sizes. |
| 706 | * While we're at it, restrict the block sizes to match the |
| 707 | * chosen granule. |
| 708 | */ |
| 709 | if (cfg->pgsize_bitmap & PAGE_SIZE) |
| 710 | granule = PAGE_SIZE; |
| 711 | else if (cfg->pgsize_bitmap & ~PAGE_MASK) |
| 712 | granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); |
| 713 | else if (cfg->pgsize_bitmap & PAGE_MASK) |
| 714 | granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); |
| 715 | else |
| 716 | granule = 0; |
| 717 | |
| 718 | switch (granule) { |
| 719 | case SZ_4K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 720 | page_sizes = (SZ_4K | SZ_2M | SZ_1G); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 721 | break; |
| 722 | case SZ_16K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 723 | page_sizes = (SZ_16K | SZ_32M); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 724 | break; |
| 725 | case SZ_64K: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 726 | max_addr_bits = 52; |
| 727 | page_sizes = (SZ_64K | SZ_512M); |
| 728 | if (cfg->oas > 48) |
| 729 | page_sizes |= 1ULL << 42; /* 4TB */ |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 730 | break; |
| 731 | default: |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 732 | page_sizes = 0; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 733 | } |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 734 | |
| 735 | cfg->pgsize_bitmap &= page_sizes; |
| 736 | cfg->ias = min(cfg->ias, max_addr_bits); |
| 737 | cfg->oas = min(cfg->oas, max_addr_bits); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static struct arm_lpae_io_pgtable * |
| 741 | arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) |
| 742 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 743 | struct arm_lpae_io_pgtable *data; |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 744 | int levels, va_bits, pg_shift; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 745 | |
| 746 | arm_lpae_restrict_pgsizes(cfg); |
| 747 | |
| 748 | if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) |
| 749 | return NULL; |
| 750 | |
| 751 | if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) |
| 752 | return NULL; |
| 753 | |
| 754 | if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) |
| 755 | return NULL; |
| 756 | |
Robin Murphy | ffcb6d1 | 2015-09-17 17:42:16 +0100 | [diff] [blame] | 757 | if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { |
| 758 | dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); |
| 759 | return NULL; |
| 760 | } |
| 761 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 762 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 763 | if (!data) |
| 764 | return NULL; |
| 765 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 766 | pg_shift = __ffs(cfg->pgsize_bitmap); |
| 767 | data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 768 | |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 769 | va_bits = cfg->ias - pg_shift; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 770 | levels = DIV_ROUND_UP(va_bits, data->bits_per_level); |
| 771 | data->start_level = ARM_LPAE_MAX_LEVELS - levels; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 772 | |
| 773 | /* Calculate the actual size of our pgd (without concatenation) */ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 774 | data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 775 | |
| 776 | data->iop.ops = (struct io_pgtable_ops) { |
| 777 | .map = arm_lpae_map, |
| 778 | .unmap = arm_lpae_unmap, |
| 779 | .iova_to_phys = arm_lpae_iova_to_phys, |
| 780 | }; |
| 781 | |
| 782 | return data; |
| 783 | } |
| 784 | |
| 785 | static struct io_pgtable * |
| 786 | arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 787 | { |
| 788 | u64 reg; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 789 | struct arm_lpae_io_pgtable *data; |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 790 | typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 791 | bool tg1; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 792 | |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 793 | if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 794 | IO_PGTABLE_QUIRK_NON_STRICT | |
| 795 | IO_PGTABLE_QUIRK_ARM_TTBR1)) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 796 | return NULL; |
| 797 | |
| 798 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 799 | if (!data) |
| 800 | return NULL; |
| 801 | |
| 802 | /* TCR */ |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 803 | if (cfg->coherent_walk) { |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 804 | tcr->sh = ARM_LPAE_TCR_SH_IS; |
| 805 | tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; |
| 806 | tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 807 | } else { |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 808 | tcr->sh = ARM_LPAE_TCR_SH_OS; |
| 809 | tcr->irgn = ARM_LPAE_TCR_RGN_NC; |
| 810 | tcr->orgn = ARM_LPAE_TCR_RGN_NC; |
Bjorn Andersson | 9e6ea59 | 2019-05-15 16:32:34 -0700 | [diff] [blame] | 811 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 812 | |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 813 | tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1; |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 814 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 815 | case SZ_4K: |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 816 | tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 817 | break; |
| 818 | case SZ_16K: |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 819 | tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 820 | break; |
| 821 | case SZ_64K: |
Robin Murphy | db69030 | 2019-10-25 19:08:39 +0100 | [diff] [blame] | 822 | tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 823 | break; |
| 824 | } |
| 825 | |
| 826 | switch (cfg->oas) { |
| 827 | case 32: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 828 | tcr->ips = ARM_LPAE_TCR_PS_32_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 829 | break; |
| 830 | case 36: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 831 | tcr->ips = ARM_LPAE_TCR_PS_36_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 832 | break; |
| 833 | case 40: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 834 | tcr->ips = ARM_LPAE_TCR_PS_40_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 835 | break; |
| 836 | case 42: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 837 | tcr->ips = ARM_LPAE_TCR_PS_42_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 838 | break; |
| 839 | case 44: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 840 | tcr->ips = ARM_LPAE_TCR_PS_44_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 841 | break; |
| 842 | case 48: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 843 | tcr->ips = ARM_LPAE_TCR_PS_48_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 844 | break; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 845 | case 52: |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 846 | tcr->ips = ARM_LPAE_TCR_PS_52_BIT; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 847 | break; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 848 | default: |
| 849 | goto out_free_data; |
| 850 | } |
| 851 | |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 852 | tcr->tsz = 64ULL - cfg->ias; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 853 | |
| 854 | /* MAIRs */ |
| 855 | reg = (ARM_LPAE_MAIR_ATTR_NC |
| 856 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | |
| 857 | (ARM_LPAE_MAIR_ATTR_WBRWA |
| 858 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | |
| 859 | (ARM_LPAE_MAIR_ATTR_DEVICE |
Vivek Gautam | 90ec7a7 | 2019-05-16 15:00:20 +0530 | [diff] [blame] | 860 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | |
| 861 | (ARM_LPAE_MAIR_ATTR_INC_OWBRWA |
| 862 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 863 | |
Robin Murphy | 205577a | 2019-10-25 19:08:36 +0100 | [diff] [blame] | 864 | cfg->arm_lpae_s1_cfg.mair = reg; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 865 | |
| 866 | /* Looking good; allocate a pgd */ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 867 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), |
| 868 | GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 869 | if (!data->pgd) |
| 870 | goto out_free_data; |
| 871 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 872 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 873 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 874 | |
Robin Murphy | d1e5f26 | 2019-10-25 19:08:37 +0100 | [diff] [blame] | 875 | /* TTBR */ |
| 876 | cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 877 | return &data->iop; |
| 878 | |
| 879 | out_free_data: |
| 880 | kfree(data); |
| 881 | return NULL; |
| 882 | } |
| 883 | |
| 884 | static struct io_pgtable * |
| 885 | arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 886 | { |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 887 | u64 sl; |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 888 | struct arm_lpae_io_pgtable *data; |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 889 | typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 890 | |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 891 | /* The NS quirk doesn't apply at stage 2 */ |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 892 | if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 893 | return NULL; |
| 894 | |
| 895 | data = arm_lpae_alloc_pgtable(cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 896 | if (!data) |
| 897 | return NULL; |
| 898 | |
| 899 | /* |
| 900 | * Concatenate PGDs at level 1 if possible in order to reduce |
| 901 | * the depth of the stage-2 walk. |
| 902 | */ |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 903 | if (data->start_level == 0) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 904 | unsigned long pgd_pages; |
| 905 | |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 906 | pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 907 | if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 908 | data->pgd_bits += data->bits_per_level; |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 909 | data->start_level++; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
| 913 | /* VTCR */ |
Will Deacon | 30d2acb | 2020-01-10 11:40:33 +0000 | [diff] [blame] | 914 | if (cfg->coherent_walk) { |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 915 | vtcr->sh = ARM_LPAE_TCR_SH_IS; |
| 916 | vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; |
| 917 | vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; |
Will Deacon | 30d2acb | 2020-01-10 11:40:33 +0000 | [diff] [blame] | 918 | } else { |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 919 | vtcr->sh = ARM_LPAE_TCR_SH_OS; |
| 920 | vtcr->irgn = ARM_LPAE_TCR_RGN_NC; |
| 921 | vtcr->orgn = ARM_LPAE_TCR_RGN_NC; |
Will Deacon | 30d2acb | 2020-01-10 11:40:33 +0000 | [diff] [blame] | 922 | } |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 923 | |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 924 | sl = data->start_level; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 925 | |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 926 | switch (ARM_LPAE_GRANULE(data)) { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 927 | case SZ_4K: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 928 | vtcr->tg = ARM_LPAE_TCR_TG0_4K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 929 | sl++; /* SL0 format is different for 4K granule size */ |
| 930 | break; |
| 931 | case SZ_16K: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 932 | vtcr->tg = ARM_LPAE_TCR_TG0_16K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 933 | break; |
| 934 | case SZ_64K: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 935 | vtcr->tg = ARM_LPAE_TCR_TG0_64K; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 936 | break; |
| 937 | } |
| 938 | |
| 939 | switch (cfg->oas) { |
| 940 | case 32: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 941 | vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 942 | break; |
| 943 | case 36: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 944 | vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 945 | break; |
| 946 | case 40: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 947 | vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 948 | break; |
| 949 | case 42: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 950 | vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 951 | break; |
| 952 | case 44: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 953 | vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 954 | break; |
| 955 | case 48: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 956 | vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 957 | break; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 958 | case 52: |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 959 | vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; |
Robin Murphy | 6c89928 | 2018-03-26 13:35:13 +0100 | [diff] [blame] | 960 | break; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 961 | default: |
| 962 | goto out_free_data; |
| 963 | } |
| 964 | |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 965 | vtcr->tsz = 64ULL - cfg->ias; |
| 966 | vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 967 | |
| 968 | /* Allocate pgd pages */ |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 969 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), |
| 970 | GFP_KERNEL, cfg); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 971 | if (!data->pgd) |
| 972 | goto out_free_data; |
| 973 | |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 974 | /* Ensure the empty pgd is visible before any actual TTBR write */ |
| 975 | wmb(); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 976 | |
| 977 | /* VTTBR */ |
| 978 | cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); |
| 979 | return &data->iop; |
| 980 | |
| 981 | out_free_data: |
| 982 | kfree(data); |
| 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | static struct io_pgtable * |
| 987 | arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) |
| 988 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 989 | if (cfg->ias > 32 || cfg->oas > 40) |
| 990 | return NULL; |
| 991 | |
| 992 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
Robin Murphy | fb485eb | 2019-10-25 19:08:38 +0100 | [diff] [blame] | 993 | return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | static struct io_pgtable * |
| 997 | arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) |
| 998 | { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 999 | if (cfg->ias > 40 || cfg->oas > 40) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
Will Deacon | ac4b80e | 2020-01-10 14:51:59 +0000 | [diff] [blame] | 1003 | return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1006 | static struct io_pgtable * |
| 1007 | arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) |
| 1008 | { |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1009 | struct arm_lpae_io_pgtable *data; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1010 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1011 | /* No quirks for Mali (hopefully) */ |
| 1012 | if (cfg->quirks) |
| 1013 | return NULL; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1014 | |
Robin Murphy | 1be08f4 | 2019-09-30 15:11:01 +0100 | [diff] [blame] | 1015 | if (cfg->ias > 48 || cfg->oas > 40) |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1016 | return NULL; |
| 1017 | |
| 1018 | cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1019 | |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1020 | data = arm_lpae_alloc_pgtable(cfg); |
| 1021 | if (!data) |
| 1022 | return NULL; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1023 | |
Robin Murphy | 1be08f4 | 2019-09-30 15:11:01 +0100 | [diff] [blame] | 1024 | /* Mali seems to need a full 4-level table regardless of IAS */ |
Robin Murphy | 594ab90 | 2019-10-25 19:08:33 +0100 | [diff] [blame] | 1025 | if (data->start_level > 0) { |
| 1026 | data->start_level = 0; |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1027 | data->pgd_bits = 0; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1028 | } |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * MEMATTR: Mali has no actual notion of a non-cacheable type, so the |
| 1031 | * best we can do is mimic the out-of-tree driver and hope that the |
| 1032 | * "implementation-defined caching policy" is good enough. Similarly, |
| 1033 | * we'll use it for the sake of a valid attribute for our 'device' |
| 1034 | * index, although callers should never request that in practice. |
| 1035 | */ |
| 1036 | cfg->arm_mali_lpae_cfg.memattr = |
| 1037 | (ARM_MALI_LPAE_MEMATTR_IMP_DEF |
| 1038 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | |
| 1039 | (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC |
| 1040 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | |
| 1041 | (ARM_MALI_LPAE_MEMATTR_IMP_DEF |
| 1042 | << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1043 | |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1044 | data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, |
| 1045 | cfg); |
Robin Murphy | 52f325f | 2019-09-30 15:11:00 +0100 | [diff] [blame] | 1046 | if (!data->pgd) |
| 1047 | goto out_free_data; |
| 1048 | |
| 1049 | /* Ensure the empty pgd is visible before TRANSTAB can be written */ |
| 1050 | wmb(); |
| 1051 | |
| 1052 | cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | |
| 1053 | ARM_MALI_LPAE_TTBR_READ_INNER | |
| 1054 | ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; |
| 1055 | return &data->iop; |
| 1056 | |
| 1057 | out_free_data: |
| 1058 | kfree(data); |
| 1059 | return NULL; |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1060 | } |
| 1061 | |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 1062 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { |
| 1063 | .alloc = arm_64_lpae_alloc_pgtable_s1, |
| 1064 | .free = arm_lpae_free_pgtable, |
| 1065 | }; |
| 1066 | |
| 1067 | struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { |
| 1068 | .alloc = arm_64_lpae_alloc_pgtable_s2, |
| 1069 | .free = arm_lpae_free_pgtable, |
| 1070 | }; |
| 1071 | |
| 1072 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { |
| 1073 | .alloc = arm_32_lpae_alloc_pgtable_s1, |
| 1074 | .free = arm_lpae_free_pgtable, |
| 1075 | }; |
| 1076 | |
| 1077 | struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { |
| 1078 | .alloc = arm_32_lpae_alloc_pgtable_s2, |
| 1079 | .free = arm_lpae_free_pgtable, |
| 1080 | }; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1081 | |
Rob Herring | d08d42d | 2019-02-21 14:23:25 -0600 | [diff] [blame] | 1082 | struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { |
| 1083 | .alloc = arm_mali_lpae_alloc_pgtable, |
| 1084 | .free = arm_lpae_free_pgtable, |
| 1085 | }; |
| 1086 | |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1087 | #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST |
| 1088 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1089 | static struct io_pgtable_cfg *cfg_cookie __initdata; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1090 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1091 | static void __init dummy_tlb_flush_all(void *cookie) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1092 | { |
| 1093 | WARN_ON(cookie != cfg_cookie); |
| 1094 | } |
| 1095 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1096 | static void __init dummy_tlb_flush(unsigned long iova, size_t size, |
| 1097 | size_t granule, void *cookie) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1098 | { |
| 1099 | WARN_ON(cookie != cfg_cookie); |
| 1100 | WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); |
| 1101 | } |
| 1102 | |
Robin Murphy | b5813c1 | 2019-10-25 19:08:30 +0100 | [diff] [blame] | 1103 | static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, |
| 1104 | unsigned long iova, size_t granule, |
| 1105 | void *cookie) |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 1106 | { |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 1107 | dummy_tlb_flush(iova, granule, granule, cookie); |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
Will Deacon | 298f7889 | 2019-07-02 16:43:34 +0100 | [diff] [blame] | 1110 | static const struct iommu_flush_ops dummy_tlb_ops __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1111 | .tlb_flush_all = dummy_tlb_flush_all, |
Will Deacon | 10b7a7d | 2019-07-02 16:44:32 +0100 | [diff] [blame] | 1112 | .tlb_flush_walk = dummy_tlb_flush, |
| 1113 | .tlb_flush_leaf = dummy_tlb_flush, |
Will Deacon | abfd6fe | 2019-07-02 16:44:41 +0100 | [diff] [blame] | 1114 | .tlb_add_page = dummy_tlb_add_page, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1115 | }; |
| 1116 | |
| 1117 | static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) |
| 1118 | { |
| 1119 | struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); |
| 1120 | struct io_pgtable_cfg *cfg = &data->iop.cfg; |
| 1121 | |
| 1122 | pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", |
| 1123 | cfg->pgsize_bitmap, cfg->ias); |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 1124 | pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", |
Robin Murphy | c79278c | 2019-10-25 19:08:34 +0100 | [diff] [blame] | 1125 | ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), |
Robin Murphy | 5fb190b | 2019-10-25 19:08:35 +0100 | [diff] [blame] | 1126 | ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | #define __FAIL(ops, i) ({ \ |
| 1130 | WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ |
| 1131 | arm_lpae_dump_ops(ops); \ |
| 1132 | selftest_running = false; \ |
| 1133 | -EFAULT; \ |
| 1134 | }) |
| 1135 | |
| 1136 | static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) |
| 1137 | { |
Christophe JAILLET | 9062c1d | 2019-09-09 22:19:19 +0200 | [diff] [blame] | 1138 | static const enum io_pgtable_fmt fmts[] __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1139 | ARM_64_LPAE_S1, |
| 1140 | ARM_64_LPAE_S2, |
| 1141 | }; |
| 1142 | |
| 1143 | int i, j; |
| 1144 | unsigned long iova; |
| 1145 | size_t size; |
| 1146 | struct io_pgtable_ops *ops; |
| 1147 | |
| 1148 | selftest_running = true; |
| 1149 | |
| 1150 | for (i = 0; i < ARRAY_SIZE(fmts); ++i) { |
| 1151 | cfg_cookie = cfg; |
| 1152 | ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); |
| 1153 | if (!ops) { |
| 1154 | pr_err("selftest: failed to allocate io pgtable ops\n"); |
| 1155 | return -ENOMEM; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Initial sanity checks. |
| 1160 | * Empty page tables shouldn't provide any translations. |
| 1161 | */ |
| 1162 | if (ops->iova_to_phys(ops, 42)) |
| 1163 | return __FAIL(ops, i); |
| 1164 | |
| 1165 | if (ops->iova_to_phys(ops, SZ_1G + 42)) |
| 1166 | return __FAIL(ops, i); |
| 1167 | |
| 1168 | if (ops->iova_to_phys(ops, SZ_2G + 42)) |
| 1169 | return __FAIL(ops, i); |
| 1170 | |
| 1171 | /* |
| 1172 | * Distinct mappings of different granule sizes. |
| 1173 | */ |
| 1174 | iova = 0; |
Kefeng Wang | 4ae8a5c | 2016-09-21 13:41:31 +0800 | [diff] [blame] | 1175 | for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1176 | size = 1UL << j; |
| 1177 | |
| 1178 | if (ops->map(ops, iova, iova, size, IOMMU_READ | |
| 1179 | IOMMU_WRITE | |
| 1180 | IOMMU_NOEXEC | |
| 1181 | IOMMU_CACHE)) |
| 1182 | return __FAIL(ops, i); |
| 1183 | |
| 1184 | /* Overlapping mappings */ |
| 1185 | if (!ops->map(ops, iova, iova + size, size, |
| 1186 | IOMMU_READ | IOMMU_NOEXEC)) |
| 1187 | return __FAIL(ops, i); |
| 1188 | |
| 1189 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1190 | return __FAIL(ops, i); |
| 1191 | |
| 1192 | iova += SZ_1G; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /* Partial unmap */ |
| 1196 | size = 1UL << __ffs(cfg->pgsize_bitmap); |
Will Deacon | a2d3a38 | 2019-07-02 16:44:58 +0100 | [diff] [blame] | 1197 | if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1198 | return __FAIL(ops, i); |
| 1199 | |
| 1200 | /* Remap of partial unmap */ |
| 1201 | if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) |
| 1202 | return __FAIL(ops, i); |
| 1203 | |
| 1204 | if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) |
| 1205 | return __FAIL(ops, i); |
| 1206 | |
| 1207 | /* Full unmap */ |
| 1208 | iova = 0; |
YueHaibing | f793b13 | 2018-04-26 12:49:29 +0800 | [diff] [blame] | 1209 | for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1210 | size = 1UL << j; |
| 1211 | |
Will Deacon | a2d3a38 | 2019-07-02 16:44:58 +0100 | [diff] [blame] | 1212 | if (ops->unmap(ops, iova, size, NULL) != size) |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1213 | return __FAIL(ops, i); |
| 1214 | |
| 1215 | if (ops->iova_to_phys(ops, iova + 42)) |
| 1216 | return __FAIL(ops, i); |
| 1217 | |
| 1218 | /* Remap full block */ |
| 1219 | if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) |
| 1220 | return __FAIL(ops, i); |
| 1221 | |
| 1222 | if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) |
| 1223 | return __FAIL(ops, i); |
| 1224 | |
| 1225 | iova += SZ_1G; |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | free_io_pgtable_ops(ops); |
| 1229 | } |
| 1230 | |
| 1231 | selftest_running = false; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int __init arm_lpae_do_selftests(void) |
| 1236 | { |
Christophe JAILLET | 9062c1d | 2019-09-09 22:19:19 +0200 | [diff] [blame] | 1237 | static const unsigned long pgsize[] __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1238 | SZ_4K | SZ_2M | SZ_1G, |
| 1239 | SZ_16K | SZ_32M, |
| 1240 | SZ_64K | SZ_512M, |
| 1241 | }; |
| 1242 | |
Christophe JAILLET | 9062c1d | 2019-09-09 22:19:19 +0200 | [diff] [blame] | 1243 | static const unsigned int ias[] __initconst = { |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1244 | 32, 36, 40, 42, 44, 48, |
| 1245 | }; |
| 1246 | |
| 1247 | int i, j, pass = 0, fail = 0; |
| 1248 | struct io_pgtable_cfg cfg = { |
| 1249 | .tlb = &dummy_tlb_ops, |
| 1250 | .oas = 48, |
Will Deacon | 4f41845 | 2019-06-25 12:51:25 +0100 | [diff] [blame] | 1251 | .coherent_walk = true, |
Will Deacon | fe4b991 | 2014-11-17 23:31:12 +0000 | [diff] [blame] | 1252 | }; |
| 1253 | |
| 1254 | for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { |
| 1255 | for (j = 0; j < ARRAY_SIZE(ias); ++j) { |
| 1256 | cfg.pgsize_bitmap = pgsize[i]; |
| 1257 | cfg.ias = ias[j]; |
| 1258 | pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", |
| 1259 | pgsize[i], ias[j]); |
| 1260 | if (arm_lpae_run_tests(&cfg)) |
| 1261 | fail++; |
| 1262 | else |
| 1263 | pass++; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); |
| 1268 | return fail ? -EFAULT : 0; |
| 1269 | } |
| 1270 | subsys_initcall(arm_lpae_do_selftests); |
| 1271 | #endif |