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