1e1d3c0fdSWill Deacon /* 2e1d3c0fdSWill Deacon * CPU-agnostic ARM page table allocator. 3e1d3c0fdSWill Deacon * 4e1d3c0fdSWill Deacon * This program is free software; you can redistribute it and/or modify 5e1d3c0fdSWill Deacon * it under the terms of the GNU General Public License version 2 as 6e1d3c0fdSWill Deacon * published by the Free Software Foundation. 7e1d3c0fdSWill Deacon * 8e1d3c0fdSWill Deacon * This program is distributed in the hope that it will be useful, 9e1d3c0fdSWill Deacon * but WITHOUT ANY WARRANTY; without even the implied warranty of 10e1d3c0fdSWill Deacon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11e1d3c0fdSWill Deacon * GNU General Public License for more details. 12e1d3c0fdSWill Deacon * 13e1d3c0fdSWill Deacon * You should have received a copy of the GNU General Public License 14e1d3c0fdSWill Deacon * along with this program. If not, see <http://www.gnu.org/licenses/>. 15e1d3c0fdSWill Deacon * 16e1d3c0fdSWill Deacon * Copyright (C) 2014 ARM Limited 17e1d3c0fdSWill Deacon * 18e1d3c0fdSWill Deacon * Author: Will Deacon <will.deacon@arm.com> 19e1d3c0fdSWill Deacon */ 20e1d3c0fdSWill Deacon 21e1d3c0fdSWill Deacon #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt 22e1d3c0fdSWill Deacon 23e1d3c0fdSWill Deacon #include <linux/iommu.h> 24e1d3c0fdSWill Deacon #include <linux/kernel.h> 25e1d3c0fdSWill Deacon #include <linux/sizes.h> 26e1d3c0fdSWill Deacon #include <linux/slab.h> 27e1d3c0fdSWill Deacon #include <linux/types.h> 288f6aff98SLada Trimasova #include <linux/dma-mapping.h> 29e1d3c0fdSWill Deacon 3087a91b15SRobin Murphy #include <asm/barrier.h> 3187a91b15SRobin Murphy 32e1d3c0fdSWill Deacon #include "io-pgtable.h" 33e1d3c0fdSWill Deacon 34e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_ADDR_BITS 48 35e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 36e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS 4 37e1d3c0fdSWill Deacon 38e1d3c0fdSWill Deacon /* Struct accessors */ 39e1d3c0fdSWill Deacon #define io_pgtable_to_data(x) \ 40e1d3c0fdSWill Deacon container_of((x), struct arm_lpae_io_pgtable, iop) 41e1d3c0fdSWill Deacon 42e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x) \ 43e1d3c0fdSWill Deacon io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 44e1d3c0fdSWill Deacon 45e1d3c0fdSWill Deacon /* 46e1d3c0fdSWill Deacon * For consistency with the architecture, we always consider 47e1d3c0fdSWill Deacon * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 48e1d3c0fdSWill Deacon */ 49e1d3c0fdSWill Deacon #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) 50e1d3c0fdSWill Deacon 51e1d3c0fdSWill Deacon /* 52e1d3c0fdSWill Deacon * Calculate the right shift amount to get to the portion describing level l 53e1d3c0fdSWill Deacon * in a virtual address mapped by the pagetable in d. 54e1d3c0fdSWill Deacon */ 55e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d) \ 56e1d3c0fdSWill Deacon ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ 57e1d3c0fdSWill Deacon * (d)->bits_per_level) + (d)->pg_shift) 58e1d3c0fdSWill Deacon 5906c610e8SRobin Murphy #define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift) 6006c610e8SRobin Murphy 61367bd978SWill Deacon #define ARM_LPAE_PAGES_PER_PGD(d) \ 6206c610e8SRobin Murphy DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d)) 63e1d3c0fdSWill Deacon 64e1d3c0fdSWill Deacon /* 65e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 66e1d3c0fdSWill Deacon * pagetable in d. 67e1d3c0fdSWill Deacon */ 68e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 69e1d3c0fdSWill Deacon ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) 70e1d3c0fdSWill Deacon 71e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 72367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 73e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 74e1d3c0fdSWill Deacon 75e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 76e1d3c0fdSWill Deacon #define ARM_LPAE_BLOCK_SIZE(l,d) \ 77e1d3c0fdSWill Deacon (1 << (ilog2(sizeof(arm_lpae_iopte)) + \ 78e1d3c0fdSWill Deacon ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) 79e1d3c0fdSWill Deacon 80e1d3c0fdSWill Deacon /* Page table bits */ 81e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 82e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 83e1d3c0fdSWill Deacon 84e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 86e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 87e1d3c0fdSWill Deacon 88c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 89e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 90e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 91e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 93e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 94c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 96e1d3c0fdSWill Deacon 97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 98e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 99e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 101e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 102e1d3c0fdSWill Deacon 103e1d3c0fdSWill Deacon /* Stage-1 PTE */ 104e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 105e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 106e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 107e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 108e1d3c0fdSWill Deacon 109e1d3c0fdSWill Deacon /* Stage-2 PTE */ 110e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 111e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 112e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 113e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 114e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 115e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 116e1d3c0fdSWill Deacon 117e1d3c0fdSWill Deacon /* Register bits */ 118e1d3c0fdSWill Deacon #define ARM_32_LPAE_TCR_EAE (1 << 31) 119e1d3c0fdSWill Deacon #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) 120e1d3c0fdSWill Deacon 12163979b8dSWill Deacon #define ARM_LPAE_TCR_EPD1 (1 << 23) 12263979b8dSWill Deacon 123e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_4K (0 << 14) 124e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_64K (1 << 14) 125e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_16K (2 << 14) 126e1d3c0fdSWill Deacon 127e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_SHIFT 12 128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_MASK 0x3 129e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS 0 130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS 2 131e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS 3 132e1d3c0fdSWill Deacon 133e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_ORGN0_SHIFT 10 134e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IRGN0_SHIFT 8 135e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_MASK 0x3 136e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC 0 137e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA 1 138e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT 2 139e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB 3 140e1d3c0fdSWill Deacon 141e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_SHIFT 6 142e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_MASK 0x3 143e1d3c0fdSWill Deacon 144e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 145e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SZ_MASK 0xf 146e1d3c0fdSWill Deacon 147e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_SHIFT 16 148e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_MASK 0x7 149e1d3c0fdSWill Deacon 150e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_SHIFT 32 151e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_MASK 0x7 152e1d3c0fdSWill Deacon 153e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 154e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 155e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 156e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 157e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 158e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 159e1d3c0fdSWill Deacon 160e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 161e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 162e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 163e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 164e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 165e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 166e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 167e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 168e1d3c0fdSWill Deacon 169e1d3c0fdSWill Deacon /* IOPTE accessors */ 170e1d3c0fdSWill Deacon #define iopte_deref(pte,d) \ 171e1d3c0fdSWill Deacon (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \ 17206c610e8SRobin Murphy & ~(ARM_LPAE_GRANULE(d) - 1ULL))) 173e1d3c0fdSWill Deacon 174e1d3c0fdSWill Deacon #define iopte_type(pte,l) \ 175e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 176e1d3c0fdSWill Deacon 177e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 178e1d3c0fdSWill Deacon 179e1d3c0fdSWill Deacon #define iopte_leaf(pte,l) \ 180e1d3c0fdSWill Deacon (l == (ARM_LPAE_MAX_LEVELS - 1) ? \ 181e1d3c0fdSWill Deacon (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \ 182e1d3c0fdSWill Deacon (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK)) 183e1d3c0fdSWill Deacon 184e1d3c0fdSWill Deacon #define iopte_to_pfn(pte,d) \ 185e1d3c0fdSWill Deacon (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift) 186e1d3c0fdSWill Deacon 187e1d3c0fdSWill Deacon #define pfn_to_iopte(pfn,d) \ 188e1d3c0fdSWill Deacon (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) 189e1d3c0fdSWill Deacon 190e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 191e1d3c0fdSWill Deacon struct io_pgtable iop; 192e1d3c0fdSWill Deacon 193e1d3c0fdSWill Deacon int levels; 194e1d3c0fdSWill Deacon size_t pgd_size; 195e1d3c0fdSWill Deacon unsigned long pg_shift; 196e1d3c0fdSWill Deacon unsigned long bits_per_level; 197e1d3c0fdSWill Deacon 198e1d3c0fdSWill Deacon void *pgd; 199e1d3c0fdSWill Deacon }; 200e1d3c0fdSWill Deacon 201e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 202e1d3c0fdSWill Deacon 203fe4b991dSWill Deacon static bool selftest_running = false; 204fe4b991dSWill Deacon 205ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 206f8d54961SRobin Murphy { 207ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 208f8d54961SRobin Murphy } 209f8d54961SRobin Murphy 210f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 211f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 212f8d54961SRobin Murphy { 213f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 214f8d54961SRobin Murphy dma_addr_t dma; 215f8d54961SRobin Murphy void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO); 216f8d54961SRobin Murphy 217f8d54961SRobin Murphy if (!pages) 218f8d54961SRobin Murphy return NULL; 219f8d54961SRobin Murphy 22087a91b15SRobin Murphy if (!selftest_running) { 221f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 222f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 223f8d54961SRobin Murphy goto out_free; 224f8d54961SRobin Murphy /* 225f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 226ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 227ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 228f8d54961SRobin Murphy */ 229ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 230f8d54961SRobin Murphy goto out_unmap; 231f8d54961SRobin Murphy } 232f8d54961SRobin Murphy 233f8d54961SRobin Murphy return pages; 234f8d54961SRobin Murphy 235f8d54961SRobin Murphy out_unmap: 236f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 237f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 238f8d54961SRobin Murphy out_free: 239f8d54961SRobin Murphy free_pages_exact(pages, size); 240f8d54961SRobin Murphy return NULL; 241f8d54961SRobin Murphy } 242f8d54961SRobin Murphy 243f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 244f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 245f8d54961SRobin Murphy { 24687a91b15SRobin Murphy if (!selftest_running) 247ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 248f8d54961SRobin Murphy size, DMA_TO_DEVICE); 249f8d54961SRobin Murphy free_pages_exact(pages, size); 250f8d54961SRobin Murphy } 251f8d54961SRobin Murphy 252f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 25387a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 254f8d54961SRobin Murphy { 255f8d54961SRobin Murphy *ptep = pte; 256f8d54961SRobin Murphy 25787a91b15SRobin Murphy if (!selftest_running) 258ffcb6d16SRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, 259ffcb6d16SRobin Murphy __arm_lpae_dma_addr(ptep), 260f8d54961SRobin Murphy sizeof(pte), DMA_TO_DEVICE); 261f8d54961SRobin Murphy } 262f8d54961SRobin Murphy 263cf27ec93SWill Deacon static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 264cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 265cf27ec93SWill Deacon arm_lpae_iopte *ptep); 266cf27ec93SWill Deacon 267e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 268e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 269e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 270e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 271e1d3c0fdSWill Deacon { 272e1d3c0fdSWill Deacon arm_lpae_iopte pte = prot; 273f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 274e1d3c0fdSWill Deacon 275fe4b991dSWill Deacon if (iopte_leaf(*ptep, lvl)) { 276cf27ec93SWill Deacon /* We require an unmap first */ 277fe4b991dSWill Deacon WARN_ON(!selftest_running); 278e1d3c0fdSWill Deacon return -EEXIST; 279cf27ec93SWill Deacon } else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 280cf27ec93SWill Deacon /* 281cf27ec93SWill Deacon * We need to unmap and free the old table before 282cf27ec93SWill Deacon * overwriting it with a block entry. 283cf27ec93SWill Deacon */ 284cf27ec93SWill Deacon arm_lpae_iopte *tblp; 285cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 286cf27ec93SWill Deacon 287cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 288cf27ec93SWill Deacon if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz)) 289cf27ec93SWill Deacon return -EINVAL; 290fe4b991dSWill Deacon } 291e1d3c0fdSWill Deacon 292f8d54961SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 293c896c132SLaurent Pinchart pte |= ARM_LPAE_PTE_NS; 294c896c132SLaurent Pinchart 295e1d3c0fdSWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 296e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_TYPE_PAGE; 297e1d3c0fdSWill Deacon else 298e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_TYPE_BLOCK; 299e1d3c0fdSWill Deacon 300e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; 301e1d3c0fdSWill Deacon pte |= pfn_to_iopte(paddr >> data->pg_shift, data); 302e1d3c0fdSWill Deacon 30387a91b15SRobin Murphy __arm_lpae_set_pte(ptep, pte, cfg); 304e1d3c0fdSWill Deacon return 0; 305e1d3c0fdSWill Deacon } 306e1d3c0fdSWill Deacon 307e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 308e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 309e1d3c0fdSWill Deacon int lvl, arm_lpae_iopte *ptep) 310e1d3c0fdSWill Deacon { 311e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 312e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 313f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 314e1d3c0fdSWill Deacon 315e1d3c0fdSWill Deacon /* Find our entry at the current level */ 316e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 317e1d3c0fdSWill Deacon 318e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 319f8d54961SRobin Murphy if (size == block_size && (size & cfg->pgsize_bitmap)) 320e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 321e1d3c0fdSWill Deacon 322e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 323e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 324e1d3c0fdSWill Deacon return -EINVAL; 325e1d3c0fdSWill Deacon 326e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 327e1d3c0fdSWill Deacon pte = *ptep; 328e1d3c0fdSWill Deacon if (!pte) { 32906c610e8SRobin Murphy cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data), 330f8d54961SRobin Murphy GFP_ATOMIC, cfg); 331e1d3c0fdSWill Deacon if (!cptep) 332e1d3c0fdSWill Deacon return -ENOMEM; 333e1d3c0fdSWill Deacon 334e1d3c0fdSWill Deacon pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE; 335f8d54961SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 336c896c132SLaurent Pinchart pte |= ARM_LPAE_PTE_NSTABLE; 33787a91b15SRobin Murphy __arm_lpae_set_pte(ptep, pte, cfg); 338*ed46e66cSOleksandr Tyshchenko } else if (!iopte_leaf(pte, lvl)) { 339e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 340*ed46e66cSOleksandr Tyshchenko } else { 341*ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 342*ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 343*ed46e66cSOleksandr Tyshchenko return -EEXIST; 344e1d3c0fdSWill Deacon } 345e1d3c0fdSWill Deacon 346e1d3c0fdSWill Deacon /* Rinse, repeat */ 347e1d3c0fdSWill Deacon return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 348e1d3c0fdSWill Deacon } 349e1d3c0fdSWill Deacon 350e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 351e1d3c0fdSWill Deacon int prot) 352e1d3c0fdSWill Deacon { 353e1d3c0fdSWill Deacon arm_lpae_iopte pte; 354e1d3c0fdSWill Deacon 355e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 356e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 357e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 358e1d3c0fdSWill Deacon 359e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 360e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 361e1d3c0fdSWill Deacon 362e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 363e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 364e7468a23SJeremy Gebben 365fb948251SRobin Murphy if (prot & IOMMU_MMIO) 366fb948251SRobin Murphy pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 367fb948251SRobin Murphy << ARM_LPAE_PTE_ATTRINDX_SHIFT); 368fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 369e1d3c0fdSWill Deacon pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 370e1d3c0fdSWill Deacon << ARM_LPAE_PTE_ATTRINDX_SHIFT); 371e1d3c0fdSWill Deacon } else { 372e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 373e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 374e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 375e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 376e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 377fb948251SRobin Murphy if (prot & IOMMU_MMIO) 378fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 379fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 380e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 381e1d3c0fdSWill Deacon else 382e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 383e1d3c0fdSWill Deacon } 384e1d3c0fdSWill Deacon 385e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 386e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 387e1d3c0fdSWill Deacon 388e1d3c0fdSWill Deacon return pte; 389e1d3c0fdSWill Deacon } 390e1d3c0fdSWill Deacon 391e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 392e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, int iommu_prot) 393e1d3c0fdSWill Deacon { 394e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 395e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 39687a91b15SRobin Murphy int ret, lvl = ARM_LPAE_START_LVL(data); 397e1d3c0fdSWill Deacon arm_lpae_iopte prot; 398e1d3c0fdSWill Deacon 399e1d3c0fdSWill Deacon /* If no access, then nothing to do */ 400e1d3c0fdSWill Deacon if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 401e1d3c0fdSWill Deacon return 0; 402e1d3c0fdSWill Deacon 403e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 40487a91b15SRobin Murphy ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 40587a91b15SRobin Murphy /* 40687a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 40787a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 40887a91b15SRobin Murphy */ 40987a91b15SRobin Murphy wmb(); 41087a91b15SRobin Murphy 41187a91b15SRobin Murphy return ret; 412e1d3c0fdSWill Deacon } 413e1d3c0fdSWill Deacon 414e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 415e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 416e1d3c0fdSWill Deacon { 417e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 418e1d3c0fdSWill Deacon unsigned long table_size; 419e1d3c0fdSWill Deacon 420e1d3c0fdSWill Deacon if (lvl == ARM_LPAE_START_LVL(data)) 421e1d3c0fdSWill Deacon table_size = data->pgd_size; 422e1d3c0fdSWill Deacon else 42306c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 424e1d3c0fdSWill Deacon 425e1d3c0fdSWill Deacon start = ptep; 42612c2ab09SWill Deacon 42712c2ab09SWill Deacon /* Only leaf entries at the last level */ 42812c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 42912c2ab09SWill Deacon end = ptep; 43012c2ab09SWill Deacon else 431e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 432e1d3c0fdSWill Deacon 433e1d3c0fdSWill Deacon while (ptep != end) { 434e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 435e1d3c0fdSWill Deacon 436e1d3c0fdSWill Deacon if (!pte || iopte_leaf(pte, lvl)) 437e1d3c0fdSWill Deacon continue; 438e1d3c0fdSWill Deacon 439e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 440e1d3c0fdSWill Deacon } 441e1d3c0fdSWill Deacon 442f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 443e1d3c0fdSWill Deacon } 444e1d3c0fdSWill Deacon 445e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 446e1d3c0fdSWill Deacon { 447e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 448e1d3c0fdSWill Deacon 449e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); 450e1d3c0fdSWill Deacon kfree(data); 451e1d3c0fdSWill Deacon } 452e1d3c0fdSWill Deacon 453e1d3c0fdSWill Deacon static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 454e1d3c0fdSWill Deacon unsigned long iova, size_t size, 455e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 456e1d3c0fdSWill Deacon arm_lpae_iopte *ptep, size_t blk_size) 457e1d3c0fdSWill Deacon { 458e1d3c0fdSWill Deacon unsigned long blk_start, blk_end; 459e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 460e1d3c0fdSWill Deacon arm_lpae_iopte table = 0; 461e1d3c0fdSWill Deacon 462e1d3c0fdSWill Deacon blk_start = iova & ~(blk_size - 1); 463e1d3c0fdSWill Deacon blk_end = blk_start + blk_size; 464e1d3c0fdSWill Deacon blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift; 465e1d3c0fdSWill Deacon 466e1d3c0fdSWill Deacon for (; blk_start < blk_end; blk_start += size, blk_paddr += size) { 467e1d3c0fdSWill Deacon arm_lpae_iopte *tablep; 468e1d3c0fdSWill Deacon 469e1d3c0fdSWill Deacon /* Unmap! */ 470e1d3c0fdSWill Deacon if (blk_start == iova) 471e1d3c0fdSWill Deacon continue; 472e1d3c0fdSWill Deacon 473e1d3c0fdSWill Deacon /* __arm_lpae_map expects a pointer to the start of the table */ 474e1d3c0fdSWill Deacon tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data); 475e1d3c0fdSWill Deacon if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl, 476e1d3c0fdSWill Deacon tablep) < 0) { 477e1d3c0fdSWill Deacon if (table) { 478e1d3c0fdSWill Deacon /* Free the table we allocated */ 479e1d3c0fdSWill Deacon tablep = iopte_deref(table, data); 480e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, tablep); 481e1d3c0fdSWill Deacon } 482e1d3c0fdSWill Deacon return 0; /* Bytes unmapped */ 483e1d3c0fdSWill Deacon } 484e1d3c0fdSWill Deacon } 485e1d3c0fdSWill Deacon 486507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, table, &data->iop.cfg); 487e1d3c0fdSWill Deacon iova &= ~(blk_size - 1); 488507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true); 489e1d3c0fdSWill Deacon return size; 490e1d3c0fdSWill Deacon } 491e1d3c0fdSWill Deacon 492e1d3c0fdSWill Deacon static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 493e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 494e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 495e1d3c0fdSWill Deacon { 496e1d3c0fdSWill Deacon arm_lpae_iopte pte; 497507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 498e1d3c0fdSWill Deacon size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 499e1d3c0fdSWill Deacon 5002eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5012eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5022eb97c78SRobin Murphy return 0; 5032eb97c78SRobin Murphy 504e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 505e1d3c0fdSWill Deacon pte = *ptep; 5062eb97c78SRobin Murphy if (WARN_ON(!pte)) 507e1d3c0fdSWill Deacon return 0; 508e1d3c0fdSWill Deacon 509e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 510e1d3c0fdSWill Deacon if (size == blk_size) { 511507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 512e1d3c0fdSWill Deacon 513e1d3c0fdSWill Deacon if (!iopte_leaf(pte, lvl)) { 514e1d3c0fdSWill Deacon /* Also flush any partial walks */ 515507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(iop, iova, size, 516507e4c9dSRobin Murphy ARM_LPAE_GRANULE(data), false); 517507e4c9dSRobin Murphy io_pgtable_tlb_sync(iop); 518e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 519e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 520e1d3c0fdSWill Deacon } else { 521507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(iop, iova, size, size, true); 522e1d3c0fdSWill Deacon } 523e1d3c0fdSWill Deacon 524e1d3c0fdSWill Deacon return size; 525e1d3c0fdSWill Deacon } else if (iopte_leaf(pte, lvl)) { 526e1d3c0fdSWill Deacon /* 527e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 528e1d3c0fdSWill Deacon * minus the part we want to unmap 529e1d3c0fdSWill Deacon */ 530e1d3c0fdSWill Deacon return arm_lpae_split_blk_unmap(data, iova, size, 531e1d3c0fdSWill Deacon iopte_prot(pte), lvl, ptep, 532e1d3c0fdSWill Deacon blk_size); 533e1d3c0fdSWill Deacon } 534e1d3c0fdSWill Deacon 535e1d3c0fdSWill Deacon /* Keep on walkin' */ 536e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 537e1d3c0fdSWill Deacon return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep); 538e1d3c0fdSWill Deacon } 539e1d3c0fdSWill Deacon 540e1d3c0fdSWill Deacon static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 541e1d3c0fdSWill Deacon size_t size) 542e1d3c0fdSWill Deacon { 543e1d3c0fdSWill Deacon size_t unmapped; 544e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 545e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 546e1d3c0fdSWill Deacon int lvl = ARM_LPAE_START_LVL(data); 547e1d3c0fdSWill Deacon 548e1d3c0fdSWill Deacon unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep); 549e1d3c0fdSWill Deacon if (unmapped) 550507e4c9dSRobin Murphy io_pgtable_tlb_sync(&data->iop); 551e1d3c0fdSWill Deacon 552e1d3c0fdSWill Deacon return unmapped; 553e1d3c0fdSWill Deacon } 554e1d3c0fdSWill Deacon 555e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 556e1d3c0fdSWill Deacon unsigned long iova) 557e1d3c0fdSWill Deacon { 558e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 559e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 560e1d3c0fdSWill Deacon int lvl = ARM_LPAE_START_LVL(data); 561e1d3c0fdSWill Deacon 562e1d3c0fdSWill Deacon do { 563e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 564e1d3c0fdSWill Deacon if (!ptep) 565e1d3c0fdSWill Deacon return 0; 566e1d3c0fdSWill Deacon 567e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 568e1d3c0fdSWill Deacon pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data)); 569e1d3c0fdSWill Deacon 570e1d3c0fdSWill Deacon /* Valid entry? */ 571e1d3c0fdSWill Deacon if (!pte) 572e1d3c0fdSWill Deacon return 0; 573e1d3c0fdSWill Deacon 574e1d3c0fdSWill Deacon /* Leaf entry? */ 575e1d3c0fdSWill Deacon if (iopte_leaf(pte,lvl)) 576e1d3c0fdSWill Deacon goto found_translation; 577e1d3c0fdSWill Deacon 578e1d3c0fdSWill Deacon /* Take it to the next level */ 579e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 580e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 581e1d3c0fdSWill Deacon 582e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 583e1d3c0fdSWill Deacon return 0; 584e1d3c0fdSWill Deacon 585e1d3c0fdSWill Deacon found_translation: 5867c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 587e1d3c0fdSWill Deacon return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova; 588e1d3c0fdSWill Deacon } 589e1d3c0fdSWill Deacon 590e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 591e1d3c0fdSWill Deacon { 592e1d3c0fdSWill Deacon unsigned long granule; 593e1d3c0fdSWill Deacon 594e1d3c0fdSWill Deacon /* 595e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 596e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 597e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 598e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 599e1d3c0fdSWill Deacon * chosen granule. 600e1d3c0fdSWill Deacon */ 601e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 602e1d3c0fdSWill Deacon granule = PAGE_SIZE; 603e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 604e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 605e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 606e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 607e1d3c0fdSWill Deacon else 608e1d3c0fdSWill Deacon granule = 0; 609e1d3c0fdSWill Deacon 610e1d3c0fdSWill Deacon switch (granule) { 611e1d3c0fdSWill Deacon case SZ_4K: 612e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 613e1d3c0fdSWill Deacon break; 614e1d3c0fdSWill Deacon case SZ_16K: 615e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_16K | SZ_32M); 616e1d3c0fdSWill Deacon break; 617e1d3c0fdSWill Deacon case SZ_64K: 618e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_64K | SZ_512M); 619e1d3c0fdSWill Deacon break; 620e1d3c0fdSWill Deacon default: 621e1d3c0fdSWill Deacon cfg->pgsize_bitmap = 0; 622e1d3c0fdSWill Deacon } 623e1d3c0fdSWill Deacon } 624e1d3c0fdSWill Deacon 625e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 626e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 627e1d3c0fdSWill Deacon { 628e1d3c0fdSWill Deacon unsigned long va_bits, pgd_bits; 629e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 630e1d3c0fdSWill Deacon 631e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 632e1d3c0fdSWill Deacon 633e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 634e1d3c0fdSWill Deacon return NULL; 635e1d3c0fdSWill Deacon 636e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 637e1d3c0fdSWill Deacon return NULL; 638e1d3c0fdSWill Deacon 639e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 640e1d3c0fdSWill Deacon return NULL; 641e1d3c0fdSWill Deacon 642ffcb6d16SRobin Murphy if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { 643ffcb6d16SRobin Murphy dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); 644ffcb6d16SRobin Murphy return NULL; 645ffcb6d16SRobin Murphy } 646ffcb6d16SRobin Murphy 647e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 648e1d3c0fdSWill Deacon if (!data) 649e1d3c0fdSWill Deacon return NULL; 650e1d3c0fdSWill Deacon 651e1d3c0fdSWill Deacon data->pg_shift = __ffs(cfg->pgsize_bitmap); 652e1d3c0fdSWill Deacon data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); 653e1d3c0fdSWill Deacon 654e1d3c0fdSWill Deacon va_bits = cfg->ias - data->pg_shift; 655e1d3c0fdSWill Deacon data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 656e1d3c0fdSWill Deacon 657e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 658e1d3c0fdSWill Deacon pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); 659e1d3c0fdSWill Deacon data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); 660e1d3c0fdSWill Deacon 661e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 662e1d3c0fdSWill Deacon .map = arm_lpae_map, 663e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 664e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 665e1d3c0fdSWill Deacon }; 666e1d3c0fdSWill Deacon 667e1d3c0fdSWill Deacon return data; 668e1d3c0fdSWill Deacon } 669e1d3c0fdSWill Deacon 670e1d3c0fdSWill Deacon static struct io_pgtable * 671e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 672e1d3c0fdSWill Deacon { 673e1d3c0fdSWill Deacon u64 reg; 6743850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 675e1d3c0fdSWill Deacon 6763850db49SRobin Murphy if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS) 6773850db49SRobin Murphy return NULL; 6783850db49SRobin Murphy 6793850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 680e1d3c0fdSWill Deacon if (!data) 681e1d3c0fdSWill Deacon return NULL; 682e1d3c0fdSWill Deacon 683e1d3c0fdSWill Deacon /* TCR */ 684e1d3c0fdSWill Deacon reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 685e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 686e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 687e1d3c0fdSWill Deacon 68806c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 689e1d3c0fdSWill Deacon case SZ_4K: 690e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 691e1d3c0fdSWill Deacon break; 692e1d3c0fdSWill Deacon case SZ_16K: 693e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 694e1d3c0fdSWill Deacon break; 695e1d3c0fdSWill Deacon case SZ_64K: 696e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 697e1d3c0fdSWill Deacon break; 698e1d3c0fdSWill Deacon } 699e1d3c0fdSWill Deacon 700e1d3c0fdSWill Deacon switch (cfg->oas) { 701e1d3c0fdSWill Deacon case 32: 702e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); 703e1d3c0fdSWill Deacon break; 704e1d3c0fdSWill Deacon case 36: 705e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); 706e1d3c0fdSWill Deacon break; 707e1d3c0fdSWill Deacon case 40: 708e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); 709e1d3c0fdSWill Deacon break; 710e1d3c0fdSWill Deacon case 42: 711e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); 712e1d3c0fdSWill Deacon break; 713e1d3c0fdSWill Deacon case 44: 714e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); 715e1d3c0fdSWill Deacon break; 716e1d3c0fdSWill Deacon case 48: 717e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); 718e1d3c0fdSWill Deacon break; 719e1d3c0fdSWill Deacon default: 720e1d3c0fdSWill Deacon goto out_free_data; 721e1d3c0fdSWill Deacon } 722e1d3c0fdSWill Deacon 723e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 72463979b8dSWill Deacon 72563979b8dSWill Deacon /* Disable speculative walks through TTBR1 */ 72663979b8dSWill Deacon reg |= ARM_LPAE_TCR_EPD1; 727e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr = reg; 728e1d3c0fdSWill Deacon 729e1d3c0fdSWill Deacon /* MAIRs */ 730e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 731e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 732e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 733e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 734e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 735e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 736e1d3c0fdSWill Deacon 737e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[0] = reg; 738e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[1] = 0; 739e1d3c0fdSWill Deacon 740e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 741f8d54961SRobin Murphy data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 742e1d3c0fdSWill Deacon if (!data->pgd) 743e1d3c0fdSWill Deacon goto out_free_data; 744e1d3c0fdSWill Deacon 74587a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 74687a91b15SRobin Murphy wmb(); 747e1d3c0fdSWill Deacon 748e1d3c0fdSWill Deacon /* TTBRs */ 749e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); 750e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[1] = 0; 751e1d3c0fdSWill Deacon return &data->iop; 752e1d3c0fdSWill Deacon 753e1d3c0fdSWill Deacon out_free_data: 754e1d3c0fdSWill Deacon kfree(data); 755e1d3c0fdSWill Deacon return NULL; 756e1d3c0fdSWill Deacon } 757e1d3c0fdSWill Deacon 758e1d3c0fdSWill Deacon static struct io_pgtable * 759e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 760e1d3c0fdSWill Deacon { 761e1d3c0fdSWill Deacon u64 reg, sl; 7623850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 763e1d3c0fdSWill Deacon 7643850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 7653850db49SRobin Murphy if (cfg->quirks) 7663850db49SRobin Murphy return NULL; 7673850db49SRobin Murphy 7683850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 769e1d3c0fdSWill Deacon if (!data) 770e1d3c0fdSWill Deacon return NULL; 771e1d3c0fdSWill Deacon 772e1d3c0fdSWill Deacon /* 773e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 774e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 775e1d3c0fdSWill Deacon */ 776e1d3c0fdSWill Deacon if (data->levels == ARM_LPAE_MAX_LEVELS) { 777e1d3c0fdSWill Deacon unsigned long pgd_pages; 778e1d3c0fdSWill Deacon 779e1d3c0fdSWill Deacon pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); 780e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 781e1d3c0fdSWill Deacon data->pgd_size = pgd_pages << data->pg_shift; 782e1d3c0fdSWill Deacon data->levels--; 783e1d3c0fdSWill Deacon } 784e1d3c0fdSWill Deacon } 785e1d3c0fdSWill Deacon 786e1d3c0fdSWill Deacon /* VTCR */ 787e1d3c0fdSWill Deacon reg = ARM_64_LPAE_S2_TCR_RES1 | 788e1d3c0fdSWill Deacon (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 789e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 790e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 791e1d3c0fdSWill Deacon 792e1d3c0fdSWill Deacon sl = ARM_LPAE_START_LVL(data); 793e1d3c0fdSWill Deacon 79406c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 795e1d3c0fdSWill Deacon case SZ_4K: 796e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 797e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 798e1d3c0fdSWill Deacon break; 799e1d3c0fdSWill Deacon case SZ_16K: 800e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 801e1d3c0fdSWill Deacon break; 802e1d3c0fdSWill Deacon case SZ_64K: 803e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 804e1d3c0fdSWill Deacon break; 805e1d3c0fdSWill Deacon } 806e1d3c0fdSWill Deacon 807e1d3c0fdSWill Deacon switch (cfg->oas) { 808e1d3c0fdSWill Deacon case 32: 809e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); 810e1d3c0fdSWill Deacon break; 811e1d3c0fdSWill Deacon case 36: 812e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); 813e1d3c0fdSWill Deacon break; 814e1d3c0fdSWill Deacon case 40: 815e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); 816e1d3c0fdSWill Deacon break; 817e1d3c0fdSWill Deacon case 42: 818e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); 819e1d3c0fdSWill Deacon break; 820e1d3c0fdSWill Deacon case 44: 821e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); 822e1d3c0fdSWill Deacon break; 823e1d3c0fdSWill Deacon case 48: 824e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); 825e1d3c0fdSWill Deacon break; 826e1d3c0fdSWill Deacon default: 827e1d3c0fdSWill Deacon goto out_free_data; 828e1d3c0fdSWill Deacon } 829e1d3c0fdSWill Deacon 830e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 831e1d3c0fdSWill Deacon reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; 832e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr = reg; 833e1d3c0fdSWill Deacon 834e1d3c0fdSWill Deacon /* Allocate pgd pages */ 835f8d54961SRobin Murphy data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 836e1d3c0fdSWill Deacon if (!data->pgd) 837e1d3c0fdSWill Deacon goto out_free_data; 838e1d3c0fdSWill Deacon 83987a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 84087a91b15SRobin Murphy wmb(); 841e1d3c0fdSWill Deacon 842e1d3c0fdSWill Deacon /* VTTBR */ 843e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 844e1d3c0fdSWill Deacon return &data->iop; 845e1d3c0fdSWill Deacon 846e1d3c0fdSWill Deacon out_free_data: 847e1d3c0fdSWill Deacon kfree(data); 848e1d3c0fdSWill Deacon return NULL; 849e1d3c0fdSWill Deacon } 850e1d3c0fdSWill Deacon 851e1d3c0fdSWill Deacon static struct io_pgtable * 852e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 853e1d3c0fdSWill Deacon { 854e1d3c0fdSWill Deacon struct io_pgtable *iop; 855e1d3c0fdSWill Deacon 856e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 857e1d3c0fdSWill Deacon return NULL; 858e1d3c0fdSWill Deacon 859e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 860e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 861e1d3c0fdSWill Deacon if (iop) { 862e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; 863e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; 864e1d3c0fdSWill Deacon } 865e1d3c0fdSWill Deacon 866e1d3c0fdSWill Deacon return iop; 867e1d3c0fdSWill Deacon } 868e1d3c0fdSWill Deacon 869e1d3c0fdSWill Deacon static struct io_pgtable * 870e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 871e1d3c0fdSWill Deacon { 872e1d3c0fdSWill Deacon struct io_pgtable *iop; 873e1d3c0fdSWill Deacon 874e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 875e1d3c0fdSWill Deacon return NULL; 876e1d3c0fdSWill Deacon 877e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 878e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 879e1d3c0fdSWill Deacon if (iop) 880e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; 881e1d3c0fdSWill Deacon 882e1d3c0fdSWill Deacon return iop; 883e1d3c0fdSWill Deacon } 884e1d3c0fdSWill Deacon 885e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 886e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 887e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 888e1d3c0fdSWill Deacon }; 889e1d3c0fdSWill Deacon 890e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 891e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 892e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 893e1d3c0fdSWill Deacon }; 894e1d3c0fdSWill Deacon 895e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 896e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 897e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 898e1d3c0fdSWill Deacon }; 899e1d3c0fdSWill Deacon 900e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 901e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 902e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 903e1d3c0fdSWill Deacon }; 904fe4b991dSWill Deacon 905fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 906fe4b991dSWill Deacon 907fe4b991dSWill Deacon static struct io_pgtable_cfg *cfg_cookie; 908fe4b991dSWill Deacon 909fe4b991dSWill Deacon static void dummy_tlb_flush_all(void *cookie) 910fe4b991dSWill Deacon { 911fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 912fe4b991dSWill Deacon } 913fe4b991dSWill Deacon 91406c610e8SRobin Murphy static void dummy_tlb_add_flush(unsigned long iova, size_t size, 91506c610e8SRobin Murphy size_t granule, bool leaf, void *cookie) 916fe4b991dSWill Deacon { 917fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 918fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 919fe4b991dSWill Deacon } 920fe4b991dSWill Deacon 921fe4b991dSWill Deacon static void dummy_tlb_sync(void *cookie) 922fe4b991dSWill Deacon { 923fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 924fe4b991dSWill Deacon } 925fe4b991dSWill Deacon 926dfed5f01SBhumika Goyal static const struct iommu_gather_ops dummy_tlb_ops __initconst = { 927fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 928fe4b991dSWill Deacon .tlb_add_flush = dummy_tlb_add_flush, 929fe4b991dSWill Deacon .tlb_sync = dummy_tlb_sync, 930fe4b991dSWill Deacon }; 931fe4b991dSWill Deacon 932fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 933fe4b991dSWill Deacon { 934fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 935fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 936fe4b991dSWill Deacon 937fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 938fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 939fe4b991dSWill Deacon pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", 940fe4b991dSWill Deacon data->levels, data->pgd_size, data->pg_shift, 941fe4b991dSWill Deacon data->bits_per_level, data->pgd); 942fe4b991dSWill Deacon } 943fe4b991dSWill Deacon 944fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 945fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 946fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 947fe4b991dSWill Deacon selftest_running = false; \ 948fe4b991dSWill Deacon -EFAULT; \ 949fe4b991dSWill Deacon }) 950fe4b991dSWill Deacon 951fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 952fe4b991dSWill Deacon { 953fe4b991dSWill Deacon static const enum io_pgtable_fmt fmts[] = { 954fe4b991dSWill Deacon ARM_64_LPAE_S1, 955fe4b991dSWill Deacon ARM_64_LPAE_S2, 956fe4b991dSWill Deacon }; 957fe4b991dSWill Deacon 958fe4b991dSWill Deacon int i, j; 959fe4b991dSWill Deacon unsigned long iova; 960fe4b991dSWill Deacon size_t size; 961fe4b991dSWill Deacon struct io_pgtable_ops *ops; 962fe4b991dSWill Deacon 963fe4b991dSWill Deacon selftest_running = true; 964fe4b991dSWill Deacon 965fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 966fe4b991dSWill Deacon cfg_cookie = cfg; 967fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 968fe4b991dSWill Deacon if (!ops) { 969fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 970fe4b991dSWill Deacon return -ENOMEM; 971fe4b991dSWill Deacon } 972fe4b991dSWill Deacon 973fe4b991dSWill Deacon /* 974fe4b991dSWill Deacon * Initial sanity checks. 975fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 976fe4b991dSWill Deacon */ 977fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 978fe4b991dSWill Deacon return __FAIL(ops, i); 979fe4b991dSWill Deacon 980fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 981fe4b991dSWill Deacon return __FAIL(ops, i); 982fe4b991dSWill Deacon 983fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 984fe4b991dSWill Deacon return __FAIL(ops, i); 985fe4b991dSWill Deacon 986fe4b991dSWill Deacon /* 987fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 988fe4b991dSWill Deacon */ 989fe4b991dSWill Deacon iova = 0; 9904ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 991fe4b991dSWill Deacon size = 1UL << j; 992fe4b991dSWill Deacon 993fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 994fe4b991dSWill Deacon IOMMU_WRITE | 995fe4b991dSWill Deacon IOMMU_NOEXEC | 996fe4b991dSWill Deacon IOMMU_CACHE)) 997fe4b991dSWill Deacon return __FAIL(ops, i); 998fe4b991dSWill Deacon 999fe4b991dSWill Deacon /* Overlapping mappings */ 1000fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1001fe4b991dSWill Deacon IOMMU_READ | IOMMU_NOEXEC)) 1002fe4b991dSWill Deacon return __FAIL(ops, i); 1003fe4b991dSWill Deacon 1004fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1005fe4b991dSWill Deacon return __FAIL(ops, i); 1006fe4b991dSWill Deacon 1007fe4b991dSWill Deacon iova += SZ_1G; 1008fe4b991dSWill Deacon } 1009fe4b991dSWill Deacon 1010fe4b991dSWill Deacon /* Partial unmap */ 1011fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1012fe4b991dSWill Deacon if (ops->unmap(ops, SZ_1G + size, size) != size) 1013fe4b991dSWill Deacon return __FAIL(ops, i); 1014fe4b991dSWill Deacon 1015fe4b991dSWill Deacon /* Remap of partial unmap */ 1016fe4b991dSWill Deacon if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 1017fe4b991dSWill Deacon return __FAIL(ops, i); 1018fe4b991dSWill Deacon 1019fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1020fe4b991dSWill Deacon return __FAIL(ops, i); 1021fe4b991dSWill Deacon 1022fe4b991dSWill Deacon /* Full unmap */ 1023fe4b991dSWill Deacon iova = 0; 1024fe4b991dSWill Deacon j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); 1025fe4b991dSWill Deacon while (j != BITS_PER_LONG) { 1026fe4b991dSWill Deacon size = 1UL << j; 1027fe4b991dSWill Deacon 1028fe4b991dSWill Deacon if (ops->unmap(ops, iova, size) != size) 1029fe4b991dSWill Deacon return __FAIL(ops, i); 1030fe4b991dSWill Deacon 1031fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1032fe4b991dSWill Deacon return __FAIL(ops, i); 1033fe4b991dSWill Deacon 1034fe4b991dSWill Deacon /* Remap full block */ 1035fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 1036fe4b991dSWill Deacon return __FAIL(ops, i); 1037fe4b991dSWill Deacon 1038fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1039fe4b991dSWill Deacon return __FAIL(ops, i); 1040fe4b991dSWill Deacon 1041fe4b991dSWill Deacon iova += SZ_1G; 1042fe4b991dSWill Deacon j++; 1043fe4b991dSWill Deacon j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); 1044fe4b991dSWill Deacon } 1045fe4b991dSWill Deacon 1046fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1047fe4b991dSWill Deacon } 1048fe4b991dSWill Deacon 1049fe4b991dSWill Deacon selftest_running = false; 1050fe4b991dSWill Deacon return 0; 1051fe4b991dSWill Deacon } 1052fe4b991dSWill Deacon 1053fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1054fe4b991dSWill Deacon { 1055fe4b991dSWill Deacon static const unsigned long pgsize[] = { 1056fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1057fe4b991dSWill Deacon SZ_16K | SZ_32M, 1058fe4b991dSWill Deacon SZ_64K | SZ_512M, 1059fe4b991dSWill Deacon }; 1060fe4b991dSWill Deacon 1061fe4b991dSWill Deacon static const unsigned int ias[] = { 1062fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1063fe4b991dSWill Deacon }; 1064fe4b991dSWill Deacon 1065fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1066fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1067fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1068fe4b991dSWill Deacon .oas = 48, 1069fe4b991dSWill Deacon }; 1070fe4b991dSWill Deacon 1071fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1072fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1073fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1074fe4b991dSWill Deacon cfg.ias = ias[j]; 1075fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1076fe4b991dSWill Deacon pgsize[i], ias[j]); 1077fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1078fe4b991dSWill Deacon fail++; 1079fe4b991dSWill Deacon else 1080fe4b991dSWill Deacon pass++; 1081fe4b991dSWill Deacon } 1082fe4b991dSWill Deacon } 1083fe4b991dSWill Deacon 1084fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1085fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1086fe4b991dSWill Deacon } 1087fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1088fe4b991dSWill Deacon #endif 1089