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 232c3d273eSRobin Murphy #include <linux/atomic.h> 24e1d3c0fdSWill Deacon #include <linux/iommu.h> 25e1d3c0fdSWill Deacon #include <linux/kernel.h> 26e1d3c0fdSWill Deacon #include <linux/sizes.h> 27e1d3c0fdSWill Deacon #include <linux/slab.h> 28e1d3c0fdSWill Deacon #include <linux/types.h> 298f6aff98SLada Trimasova #include <linux/dma-mapping.h> 30e1d3c0fdSWill Deacon 3187a91b15SRobin Murphy #include <asm/barrier.h> 3287a91b15SRobin Murphy 33e1d3c0fdSWill Deacon #include "io-pgtable.h" 34e1d3c0fdSWill Deacon 35e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_ADDR_BITS 48 36e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 37e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS 4 38e1d3c0fdSWill Deacon 39e1d3c0fdSWill Deacon /* Struct accessors */ 40e1d3c0fdSWill Deacon #define io_pgtable_to_data(x) \ 41e1d3c0fdSWill Deacon container_of((x), struct arm_lpae_io_pgtable, iop) 42e1d3c0fdSWill Deacon 43e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x) \ 44e1d3c0fdSWill Deacon io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 45e1d3c0fdSWill Deacon 46e1d3c0fdSWill Deacon /* 47e1d3c0fdSWill Deacon * For consistency with the architecture, we always consider 48e1d3c0fdSWill Deacon * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 49e1d3c0fdSWill Deacon */ 50e1d3c0fdSWill Deacon #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) 51e1d3c0fdSWill Deacon 52e1d3c0fdSWill Deacon /* 53e1d3c0fdSWill Deacon * Calculate the right shift amount to get to the portion describing level l 54e1d3c0fdSWill Deacon * in a virtual address mapped by the pagetable in d. 55e1d3c0fdSWill Deacon */ 56e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d) \ 57e1d3c0fdSWill Deacon ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ 58e1d3c0fdSWill Deacon * (d)->bits_per_level) + (d)->pg_shift) 59e1d3c0fdSWill Deacon 6006c610e8SRobin Murphy #define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift) 6106c610e8SRobin Murphy 62367bd978SWill Deacon #define ARM_LPAE_PAGES_PER_PGD(d) \ 6306c610e8SRobin Murphy DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d)) 64e1d3c0fdSWill Deacon 65e1d3c0fdSWill Deacon /* 66e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 67e1d3c0fdSWill Deacon * pagetable in d. 68e1d3c0fdSWill Deacon */ 69e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 70e1d3c0fdSWill Deacon ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) 71e1d3c0fdSWill Deacon 72e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 73367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 74e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 75e1d3c0fdSWill Deacon 76e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 77e1d3c0fdSWill Deacon #define ARM_LPAE_BLOCK_SIZE(l,d) \ 78022f4e4fSRobin Murphy (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \ 79e1d3c0fdSWill Deacon ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) 80e1d3c0fdSWill Deacon 81e1d3c0fdSWill Deacon /* Page table bits */ 82e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 84e1d3c0fdSWill Deacon 85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 86e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 87e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 88e1d3c0fdSWill Deacon 89c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 90e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 91e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 93e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 95c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 97e1d3c0fdSWill Deacon 98e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 99e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 102e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 1032c3d273eSRobin Murphy /* Software bit for solving coherency races */ 1042c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) 105e1d3c0fdSWill Deacon 106e1d3c0fdSWill Deacon /* Stage-1 PTE */ 107e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 108e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 109e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 110e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 111e1d3c0fdSWill Deacon 112e1d3c0fdSWill Deacon /* Stage-2 PTE */ 113e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 114e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 115e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 116e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 117e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 118e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 119e1d3c0fdSWill Deacon 120e1d3c0fdSWill Deacon /* Register bits */ 121e1d3c0fdSWill Deacon #define ARM_32_LPAE_TCR_EAE (1 << 31) 122e1d3c0fdSWill Deacon #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) 123e1d3c0fdSWill Deacon 12463979b8dSWill Deacon #define ARM_LPAE_TCR_EPD1 (1 << 23) 12563979b8dSWill Deacon 126e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_4K (0 << 14) 127e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_64K (1 << 14) 128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_16K (2 << 14) 129e1d3c0fdSWill Deacon 130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_SHIFT 12 131e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_MASK 0x3 132e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS 0 133e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS 2 134e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS 3 135e1d3c0fdSWill Deacon 136e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_ORGN0_SHIFT 10 137e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IRGN0_SHIFT 8 138e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_MASK 0x3 139e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC 0 140e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA 1 141e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT 2 142e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB 3 143e1d3c0fdSWill Deacon 144e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_SHIFT 6 145e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_MASK 0x3 146e1d3c0fdSWill Deacon 147e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 148e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SZ_MASK 0xf 149e1d3c0fdSWill Deacon 150e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_SHIFT 16 151e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_MASK 0x7 152e1d3c0fdSWill Deacon 153e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_SHIFT 32 154e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_MASK 0x7 155e1d3c0fdSWill Deacon 156e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 157e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 158e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 159e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 160e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 161e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 162e1d3c0fdSWill Deacon 163e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 164e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 165e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 166e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 167e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 168e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 169e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 170e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 171e1d3c0fdSWill Deacon 172e1d3c0fdSWill Deacon /* IOPTE accessors */ 173e1d3c0fdSWill Deacon #define iopte_deref(pte,d) \ 174e1d3c0fdSWill Deacon (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \ 17506c610e8SRobin Murphy & ~(ARM_LPAE_GRANULE(d) - 1ULL))) 176e1d3c0fdSWill Deacon 177e1d3c0fdSWill Deacon #define iopte_type(pte,l) \ 178e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 179e1d3c0fdSWill Deacon 180e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 181e1d3c0fdSWill Deacon 182e1d3c0fdSWill Deacon #define iopte_leaf(pte,l) \ 183e1d3c0fdSWill Deacon (l == (ARM_LPAE_MAX_LEVELS - 1) ? \ 184e1d3c0fdSWill Deacon (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \ 185e1d3c0fdSWill Deacon (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK)) 186e1d3c0fdSWill Deacon 187e1d3c0fdSWill Deacon #define iopte_to_pfn(pte,d) \ 188e1d3c0fdSWill Deacon (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift) 189e1d3c0fdSWill Deacon 190e1d3c0fdSWill Deacon #define pfn_to_iopte(pfn,d) \ 191e1d3c0fdSWill Deacon (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) 192e1d3c0fdSWill Deacon 193e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 194e1d3c0fdSWill Deacon struct io_pgtable iop; 195e1d3c0fdSWill Deacon 196e1d3c0fdSWill Deacon int levels; 197e1d3c0fdSWill Deacon size_t pgd_size; 198e1d3c0fdSWill Deacon unsigned long pg_shift; 199e1d3c0fdSWill Deacon unsigned long bits_per_level; 200e1d3c0fdSWill Deacon 201e1d3c0fdSWill Deacon void *pgd; 202e1d3c0fdSWill Deacon }; 203e1d3c0fdSWill Deacon 204e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 205e1d3c0fdSWill Deacon 206fe4b991dSWill Deacon static bool selftest_running = false; 207fe4b991dSWill Deacon 208ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 209f8d54961SRobin Murphy { 210ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 211f8d54961SRobin Murphy } 212f8d54961SRobin Murphy 213f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 214f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 215f8d54961SRobin Murphy { 216f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 217f8d54961SRobin Murphy dma_addr_t dma; 218f8d54961SRobin Murphy void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO); 219f8d54961SRobin Murphy 220f8d54961SRobin Murphy if (!pages) 221f8d54961SRobin Murphy return NULL; 222f8d54961SRobin Murphy 22381b3c252SRobin Murphy if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) { 224f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 225f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 226f8d54961SRobin Murphy goto out_free; 227f8d54961SRobin Murphy /* 228f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 229ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 230ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 231f8d54961SRobin Murphy */ 232ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 233f8d54961SRobin Murphy goto out_unmap; 234f8d54961SRobin Murphy } 235f8d54961SRobin Murphy 236f8d54961SRobin Murphy return pages; 237f8d54961SRobin Murphy 238f8d54961SRobin Murphy out_unmap: 239f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 240f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 241f8d54961SRobin Murphy out_free: 242f8d54961SRobin Murphy free_pages_exact(pages, size); 243f8d54961SRobin Murphy return NULL; 244f8d54961SRobin Murphy } 245f8d54961SRobin Murphy 246f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 247f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 248f8d54961SRobin Murphy { 24981b3c252SRobin Murphy if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) 250ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 251f8d54961SRobin Murphy size, DMA_TO_DEVICE); 252f8d54961SRobin Murphy free_pages_exact(pages, size); 253f8d54961SRobin Murphy } 254f8d54961SRobin Murphy 2552c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 2562c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2572c3d273eSRobin Murphy { 2582c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 2592c3d273eSRobin Murphy sizeof(*ptep), DMA_TO_DEVICE); 2602c3d273eSRobin Murphy } 2612c3d273eSRobin Murphy 262f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 26387a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 264f8d54961SRobin Murphy { 265f8d54961SRobin Murphy *ptep = pte; 266f8d54961SRobin Murphy 26781b3c252SRobin Murphy if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) 2682c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 269f8d54961SRobin Murphy } 270f8d54961SRobin Murphy 271cf27ec93SWill Deacon static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 272cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 273cf27ec93SWill Deacon arm_lpae_iopte *ptep); 274cf27ec93SWill Deacon 275fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 276fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 277fb3a9579SRobin Murphy int lvl, arm_lpae_iopte *ptep) 278fb3a9579SRobin Murphy { 279fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 280fb3a9579SRobin Murphy 281fb3a9579SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 282fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_NS; 283fb3a9579SRobin Murphy 284fb3a9579SRobin Murphy if (lvl == ARM_LPAE_MAX_LEVELS - 1) 285fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 286fb3a9579SRobin Murphy else 287fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 288fb3a9579SRobin Murphy 289fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; 290fb3a9579SRobin Murphy pte |= pfn_to_iopte(paddr >> data->pg_shift, data); 291fb3a9579SRobin Murphy 292fb3a9579SRobin Murphy __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); 293fb3a9579SRobin Murphy } 294fb3a9579SRobin Murphy 295e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 296e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 297e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 298e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 299e1d3c0fdSWill Deacon { 300fb3a9579SRobin Murphy arm_lpae_iopte pte = *ptep; 301e1d3c0fdSWill Deacon 302fb3a9579SRobin Murphy if (iopte_leaf(pte, lvl)) { 303cf27ec93SWill Deacon /* We require an unmap first */ 304fe4b991dSWill Deacon WARN_ON(!selftest_running); 305e1d3c0fdSWill Deacon return -EEXIST; 306fb3a9579SRobin Murphy } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 307cf27ec93SWill Deacon /* 308cf27ec93SWill Deacon * We need to unmap and free the old table before 309cf27ec93SWill Deacon * overwriting it with a block entry. 310cf27ec93SWill Deacon */ 311cf27ec93SWill Deacon arm_lpae_iopte *tblp; 312cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 313cf27ec93SWill Deacon 314cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 315cf27ec93SWill Deacon if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz)) 316cf27ec93SWill Deacon return -EINVAL; 317fe4b991dSWill Deacon } 318e1d3c0fdSWill Deacon 319fb3a9579SRobin Murphy __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); 320e1d3c0fdSWill Deacon return 0; 321e1d3c0fdSWill Deacon } 322e1d3c0fdSWill Deacon 323fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 324fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3252c3d273eSRobin Murphy arm_lpae_iopte curr, 326fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 327fb3a9579SRobin Murphy { 3282c3d273eSRobin Murphy arm_lpae_iopte old, new; 329fb3a9579SRobin Murphy 330fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 331fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 332fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 333fb3a9579SRobin Murphy 33477f34458SWill Deacon /* 33577f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 33677f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 33777f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 33877f34458SWill Deacon */ 33977f34458SWill Deacon dma_wmb(); 3402c3d273eSRobin Murphy 3412c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3422c3d273eSRobin Murphy 3432c3d273eSRobin Murphy if ((cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) || 3442c3d273eSRobin Murphy (old & ARM_LPAE_PTE_SW_SYNC)) 3452c3d273eSRobin Murphy return old; 3462c3d273eSRobin Murphy 3472c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 3482c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3492c3d273eSRobin Murphy if (old == curr) 3502c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3512c3d273eSRobin Murphy 3522c3d273eSRobin Murphy return old; 353fb3a9579SRobin Murphy } 354fb3a9579SRobin Murphy 355e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 356e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 357e1d3c0fdSWill Deacon int lvl, arm_lpae_iopte *ptep) 358e1d3c0fdSWill Deacon { 359e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 360e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3612c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 362f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 363e1d3c0fdSWill Deacon 364e1d3c0fdSWill Deacon /* Find our entry at the current level */ 365e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 366e1d3c0fdSWill Deacon 367e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 368f8d54961SRobin Murphy if (size == block_size && (size & cfg->pgsize_bitmap)) 369e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 370e1d3c0fdSWill Deacon 371e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 372e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 373e1d3c0fdSWill Deacon return -EINVAL; 374e1d3c0fdSWill Deacon 375e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3762c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 377e1d3c0fdSWill Deacon if (!pte) { 3782c3d273eSRobin Murphy cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); 379e1d3c0fdSWill Deacon if (!cptep) 380e1d3c0fdSWill Deacon return -ENOMEM; 381e1d3c0fdSWill Deacon 3822c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 3832c3d273eSRobin Murphy if (pte) 3842c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 3852c3d273eSRobin Murphy } else if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) && 3862c3d273eSRobin Murphy !(pte & ARM_LPAE_PTE_SW_SYNC)) { 3872c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3882c3d273eSRobin Murphy } 3892c3d273eSRobin Murphy 3902c3d273eSRobin Murphy if (pte && !iopte_leaf(pte, lvl)) { 391e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 3922c3d273eSRobin Murphy } else if (pte) { 393ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 394ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 395ed46e66cSOleksandr Tyshchenko return -EEXIST; 396e1d3c0fdSWill Deacon } 397e1d3c0fdSWill Deacon 398e1d3c0fdSWill Deacon /* Rinse, repeat */ 399e1d3c0fdSWill Deacon return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 400e1d3c0fdSWill Deacon } 401e1d3c0fdSWill Deacon 402e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 403e1d3c0fdSWill Deacon int prot) 404e1d3c0fdSWill Deacon { 405e1d3c0fdSWill Deacon arm_lpae_iopte pte; 406e1d3c0fdSWill Deacon 407e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 408e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 409e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 410e1d3c0fdSWill Deacon 411e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 412e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 413e1d3c0fdSWill Deacon 414e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 415e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 416e7468a23SJeremy Gebben 417fb948251SRobin Murphy if (prot & IOMMU_MMIO) 418fb948251SRobin Murphy pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 419fb948251SRobin Murphy << ARM_LPAE_PTE_ATTRINDX_SHIFT); 420fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 421e1d3c0fdSWill Deacon pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 422e1d3c0fdSWill Deacon << ARM_LPAE_PTE_ATTRINDX_SHIFT); 423e1d3c0fdSWill Deacon } else { 424e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 425e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 426e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 427e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 428e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 429fb948251SRobin Murphy if (prot & IOMMU_MMIO) 430fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 431fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 432e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 433e1d3c0fdSWill Deacon else 434e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 435e1d3c0fdSWill Deacon } 436e1d3c0fdSWill Deacon 437e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 438e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 439e1d3c0fdSWill Deacon 440e1d3c0fdSWill Deacon return pte; 441e1d3c0fdSWill Deacon } 442e1d3c0fdSWill Deacon 443e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 444e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, int iommu_prot) 445e1d3c0fdSWill Deacon { 446e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 447e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 44887a91b15SRobin Murphy int ret, lvl = ARM_LPAE_START_LVL(data); 449e1d3c0fdSWill Deacon arm_lpae_iopte prot; 450e1d3c0fdSWill Deacon 451e1d3c0fdSWill Deacon /* If no access, then nothing to do */ 452e1d3c0fdSWill Deacon if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 453e1d3c0fdSWill Deacon return 0; 454e1d3c0fdSWill Deacon 45576557391SRobin Murphy if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) || 45676557391SRobin Murphy paddr >= (1ULL << data->iop.cfg.oas))) 45776557391SRobin Murphy return -ERANGE; 45876557391SRobin Murphy 459e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 46087a91b15SRobin Murphy ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 46187a91b15SRobin Murphy /* 46287a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 46387a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 46487a91b15SRobin Murphy */ 46587a91b15SRobin Murphy wmb(); 46687a91b15SRobin Murphy 46787a91b15SRobin Murphy return ret; 468e1d3c0fdSWill Deacon } 469e1d3c0fdSWill Deacon 470e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 471e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 472e1d3c0fdSWill Deacon { 473e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 474e1d3c0fdSWill Deacon unsigned long table_size; 475e1d3c0fdSWill Deacon 476e1d3c0fdSWill Deacon if (lvl == ARM_LPAE_START_LVL(data)) 477e1d3c0fdSWill Deacon table_size = data->pgd_size; 478e1d3c0fdSWill Deacon else 47906c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 480e1d3c0fdSWill Deacon 481e1d3c0fdSWill Deacon start = ptep; 48212c2ab09SWill Deacon 48312c2ab09SWill Deacon /* Only leaf entries at the last level */ 48412c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 48512c2ab09SWill Deacon end = ptep; 48612c2ab09SWill Deacon else 487e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 488e1d3c0fdSWill Deacon 489e1d3c0fdSWill Deacon while (ptep != end) { 490e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 491e1d3c0fdSWill Deacon 492e1d3c0fdSWill Deacon if (!pte || iopte_leaf(pte, lvl)) 493e1d3c0fdSWill Deacon continue; 494e1d3c0fdSWill Deacon 495e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 496e1d3c0fdSWill Deacon } 497e1d3c0fdSWill Deacon 498f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 499e1d3c0fdSWill Deacon } 500e1d3c0fdSWill Deacon 501e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 502e1d3c0fdSWill Deacon { 503e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 504e1d3c0fdSWill Deacon 505e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); 506e1d3c0fdSWill Deacon kfree(data); 507e1d3c0fdSWill Deacon } 508e1d3c0fdSWill Deacon 509e1d3c0fdSWill Deacon static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 510e1d3c0fdSWill Deacon unsigned long iova, size_t size, 511fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 512fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 513e1d3c0fdSWill Deacon { 514fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 515fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 516e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 517fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 518fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 519fb3a9579SRobin Murphy int i, unmap_idx = -1; 520e1d3c0fdSWill Deacon 521fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 522fb3a9579SRobin Murphy return 0; 523e1d3c0fdSWill Deacon 524fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 525fb3a9579SRobin Murphy if (!tablep) 526fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 527e1d3c0fdSWill Deacon 528fb3a9579SRobin Murphy if (size == split_sz) 529fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 530fb3a9579SRobin Murphy 531fb3a9579SRobin Murphy blk_paddr = iopte_to_pfn(blk_pte, data) << data->pg_shift; 532fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 533fb3a9579SRobin Murphy 534fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 535e1d3c0fdSWill Deacon /* Unmap! */ 536fb3a9579SRobin Murphy if (i == unmap_idx) 537e1d3c0fdSWill Deacon continue; 538e1d3c0fdSWill Deacon 539fb3a9579SRobin Murphy __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); 540e1d3c0fdSWill Deacon } 541e1d3c0fdSWill Deacon 5422c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5432c3d273eSRobin Murphy if (pte != blk_pte) { 5442c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5452c3d273eSRobin Murphy /* 5462c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5472c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5482c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5492c3d273eSRobin Murphy */ 5502c3d273eSRobin Murphy if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) 5512c3d273eSRobin Murphy return 0; 5522c3d273eSRobin Murphy 5532c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 5542c3d273eSRobin Murphy } 555fb3a9579SRobin Murphy 556fb3a9579SRobin Murphy if (unmap_idx < 0) 557fb3a9579SRobin Murphy return __arm_lpae_unmap(data, iova, size, lvl, tablep); 558fb3a9579SRobin Murphy 559fb3a9579SRobin Murphy io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true); 560e1d3c0fdSWill Deacon return size; 561e1d3c0fdSWill Deacon } 562e1d3c0fdSWill Deacon 563e1d3c0fdSWill Deacon static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 564e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 565e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 566e1d3c0fdSWill Deacon { 567e1d3c0fdSWill Deacon arm_lpae_iopte pte; 568507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 569e1d3c0fdSWill Deacon 5702eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5712eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5722eb97c78SRobin Murphy return 0; 5732eb97c78SRobin Murphy 574e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 5752c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 5762eb97c78SRobin Murphy if (WARN_ON(!pte)) 577e1d3c0fdSWill Deacon return 0; 578e1d3c0fdSWill Deacon 579e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 580fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 581507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 582e1d3c0fdSWill Deacon 583e1d3c0fdSWill Deacon if (!iopte_leaf(pte, lvl)) { 584e1d3c0fdSWill Deacon /* Also flush any partial walks */ 585507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(iop, iova, size, 586507e4c9dSRobin Murphy ARM_LPAE_GRANULE(data), false); 587507e4c9dSRobin Murphy io_pgtable_tlb_sync(iop); 588e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 589e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 590e1d3c0fdSWill Deacon } else { 591507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(iop, iova, size, size, true); 592e1d3c0fdSWill Deacon } 593e1d3c0fdSWill Deacon 594e1d3c0fdSWill Deacon return size; 595e1d3c0fdSWill Deacon } else if (iopte_leaf(pte, lvl)) { 596e1d3c0fdSWill Deacon /* 597e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 598e1d3c0fdSWill Deacon * minus the part we want to unmap 599e1d3c0fdSWill Deacon */ 600fb3a9579SRobin Murphy return arm_lpae_split_blk_unmap(data, iova, size, pte, 601fb3a9579SRobin Murphy lvl + 1, ptep); 602e1d3c0fdSWill Deacon } 603e1d3c0fdSWill Deacon 604e1d3c0fdSWill Deacon /* Keep on walkin' */ 605e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 606e1d3c0fdSWill Deacon return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep); 607e1d3c0fdSWill Deacon } 608e1d3c0fdSWill Deacon 609e1d3c0fdSWill Deacon static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 610e1d3c0fdSWill Deacon size_t size) 611e1d3c0fdSWill Deacon { 612e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 613e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 614e1d3c0fdSWill Deacon int lvl = ARM_LPAE_START_LVL(data); 615e1d3c0fdSWill Deacon 61676557391SRobin Murphy if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias))) 61776557391SRobin Murphy return 0; 61876557391SRobin Murphy 619*32b12449SRobin Murphy return __arm_lpae_unmap(data, iova, size, lvl, ptep); 620e1d3c0fdSWill Deacon } 621e1d3c0fdSWill Deacon 622e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 623e1d3c0fdSWill Deacon unsigned long iova) 624e1d3c0fdSWill Deacon { 625e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 626e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 627e1d3c0fdSWill Deacon int lvl = ARM_LPAE_START_LVL(data); 628e1d3c0fdSWill Deacon 629e1d3c0fdSWill Deacon do { 630e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 631e1d3c0fdSWill Deacon if (!ptep) 632e1d3c0fdSWill Deacon return 0; 633e1d3c0fdSWill Deacon 634e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6352c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6362c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 637e1d3c0fdSWill Deacon 638e1d3c0fdSWill Deacon /* Valid entry? */ 639e1d3c0fdSWill Deacon if (!pte) 640e1d3c0fdSWill Deacon return 0; 641e1d3c0fdSWill Deacon 642e1d3c0fdSWill Deacon /* Leaf entry? */ 643e1d3c0fdSWill Deacon if (iopte_leaf(pte,lvl)) 644e1d3c0fdSWill Deacon goto found_translation; 645e1d3c0fdSWill Deacon 646e1d3c0fdSWill Deacon /* Take it to the next level */ 647e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 648e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 649e1d3c0fdSWill Deacon 650e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 651e1d3c0fdSWill Deacon return 0; 652e1d3c0fdSWill Deacon 653e1d3c0fdSWill Deacon found_translation: 6547c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 655e1d3c0fdSWill Deacon return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova; 656e1d3c0fdSWill Deacon } 657e1d3c0fdSWill Deacon 658e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 659e1d3c0fdSWill Deacon { 660e1d3c0fdSWill Deacon unsigned long granule; 661e1d3c0fdSWill Deacon 662e1d3c0fdSWill Deacon /* 663e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 664e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 665e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 666e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 667e1d3c0fdSWill Deacon * chosen granule. 668e1d3c0fdSWill Deacon */ 669e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 670e1d3c0fdSWill Deacon granule = PAGE_SIZE; 671e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 672e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 673e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 674e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 675e1d3c0fdSWill Deacon else 676e1d3c0fdSWill Deacon granule = 0; 677e1d3c0fdSWill Deacon 678e1d3c0fdSWill Deacon switch (granule) { 679e1d3c0fdSWill Deacon case SZ_4K: 680e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 681e1d3c0fdSWill Deacon break; 682e1d3c0fdSWill Deacon case SZ_16K: 683e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_16K | SZ_32M); 684e1d3c0fdSWill Deacon break; 685e1d3c0fdSWill Deacon case SZ_64K: 686e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_64K | SZ_512M); 687e1d3c0fdSWill Deacon break; 688e1d3c0fdSWill Deacon default: 689e1d3c0fdSWill Deacon cfg->pgsize_bitmap = 0; 690e1d3c0fdSWill Deacon } 691e1d3c0fdSWill Deacon } 692e1d3c0fdSWill Deacon 693e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 694e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 695e1d3c0fdSWill Deacon { 696e1d3c0fdSWill Deacon unsigned long va_bits, pgd_bits; 697e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 698e1d3c0fdSWill Deacon 699e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 700e1d3c0fdSWill Deacon 701e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 702e1d3c0fdSWill Deacon return NULL; 703e1d3c0fdSWill Deacon 704e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 705e1d3c0fdSWill Deacon return NULL; 706e1d3c0fdSWill Deacon 707e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 708e1d3c0fdSWill Deacon return NULL; 709e1d3c0fdSWill Deacon 710ffcb6d16SRobin Murphy if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { 711ffcb6d16SRobin Murphy dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); 712ffcb6d16SRobin Murphy return NULL; 713ffcb6d16SRobin Murphy } 714ffcb6d16SRobin Murphy 715e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 716e1d3c0fdSWill Deacon if (!data) 717e1d3c0fdSWill Deacon return NULL; 718e1d3c0fdSWill Deacon 719e1d3c0fdSWill Deacon data->pg_shift = __ffs(cfg->pgsize_bitmap); 720e1d3c0fdSWill Deacon data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); 721e1d3c0fdSWill Deacon 722e1d3c0fdSWill Deacon va_bits = cfg->ias - data->pg_shift; 723e1d3c0fdSWill Deacon data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 724e1d3c0fdSWill Deacon 725e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 726e1d3c0fdSWill Deacon pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); 727e1d3c0fdSWill Deacon data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); 728e1d3c0fdSWill Deacon 729e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 730e1d3c0fdSWill Deacon .map = arm_lpae_map, 731e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 732e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 733e1d3c0fdSWill Deacon }; 734e1d3c0fdSWill Deacon 735e1d3c0fdSWill Deacon return data; 736e1d3c0fdSWill Deacon } 737e1d3c0fdSWill Deacon 738e1d3c0fdSWill Deacon static struct io_pgtable * 739e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 740e1d3c0fdSWill Deacon { 741e1d3c0fdSWill Deacon u64 reg; 7423850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 743e1d3c0fdSWill Deacon 74481b3c252SRobin Murphy if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | IO_PGTABLE_QUIRK_NO_DMA)) 7453850db49SRobin Murphy return NULL; 7463850db49SRobin Murphy 7473850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 748e1d3c0fdSWill Deacon if (!data) 749e1d3c0fdSWill Deacon return NULL; 750e1d3c0fdSWill Deacon 751e1d3c0fdSWill Deacon /* TCR */ 752e1d3c0fdSWill Deacon reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 753e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 754e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 755e1d3c0fdSWill Deacon 75606c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 757e1d3c0fdSWill Deacon case SZ_4K: 758e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 759e1d3c0fdSWill Deacon break; 760e1d3c0fdSWill Deacon case SZ_16K: 761e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 762e1d3c0fdSWill Deacon break; 763e1d3c0fdSWill Deacon case SZ_64K: 764e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 765e1d3c0fdSWill Deacon break; 766e1d3c0fdSWill Deacon } 767e1d3c0fdSWill Deacon 768e1d3c0fdSWill Deacon switch (cfg->oas) { 769e1d3c0fdSWill Deacon case 32: 770e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); 771e1d3c0fdSWill Deacon break; 772e1d3c0fdSWill Deacon case 36: 773e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); 774e1d3c0fdSWill Deacon break; 775e1d3c0fdSWill Deacon case 40: 776e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); 777e1d3c0fdSWill Deacon break; 778e1d3c0fdSWill Deacon case 42: 779e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); 780e1d3c0fdSWill Deacon break; 781e1d3c0fdSWill Deacon case 44: 782e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); 783e1d3c0fdSWill Deacon break; 784e1d3c0fdSWill Deacon case 48: 785e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); 786e1d3c0fdSWill Deacon break; 787e1d3c0fdSWill Deacon default: 788e1d3c0fdSWill Deacon goto out_free_data; 789e1d3c0fdSWill Deacon } 790e1d3c0fdSWill Deacon 791e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 79263979b8dSWill Deacon 79363979b8dSWill Deacon /* Disable speculative walks through TTBR1 */ 79463979b8dSWill Deacon reg |= ARM_LPAE_TCR_EPD1; 795e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr = reg; 796e1d3c0fdSWill Deacon 797e1d3c0fdSWill Deacon /* MAIRs */ 798e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 799e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 800e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 801e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 802e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 803e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 804e1d3c0fdSWill Deacon 805e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[0] = reg; 806e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[1] = 0; 807e1d3c0fdSWill Deacon 808e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 809f8d54961SRobin Murphy data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 810e1d3c0fdSWill Deacon if (!data->pgd) 811e1d3c0fdSWill Deacon goto out_free_data; 812e1d3c0fdSWill Deacon 81387a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 81487a91b15SRobin Murphy wmb(); 815e1d3c0fdSWill Deacon 816e1d3c0fdSWill Deacon /* TTBRs */ 817e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); 818e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[1] = 0; 819e1d3c0fdSWill Deacon return &data->iop; 820e1d3c0fdSWill Deacon 821e1d3c0fdSWill Deacon out_free_data: 822e1d3c0fdSWill Deacon kfree(data); 823e1d3c0fdSWill Deacon return NULL; 824e1d3c0fdSWill Deacon } 825e1d3c0fdSWill Deacon 826e1d3c0fdSWill Deacon static struct io_pgtable * 827e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 828e1d3c0fdSWill Deacon { 829e1d3c0fdSWill Deacon u64 reg, sl; 8303850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 831e1d3c0fdSWill Deacon 8323850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 83381b3c252SRobin Murphy if (cfg->quirks & ~IO_PGTABLE_QUIRK_NO_DMA) 8343850db49SRobin Murphy return NULL; 8353850db49SRobin Murphy 8363850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 837e1d3c0fdSWill Deacon if (!data) 838e1d3c0fdSWill Deacon return NULL; 839e1d3c0fdSWill Deacon 840e1d3c0fdSWill Deacon /* 841e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 842e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 843e1d3c0fdSWill Deacon */ 844e1d3c0fdSWill Deacon if (data->levels == ARM_LPAE_MAX_LEVELS) { 845e1d3c0fdSWill Deacon unsigned long pgd_pages; 846e1d3c0fdSWill Deacon 847e1d3c0fdSWill Deacon pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); 848e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 849e1d3c0fdSWill Deacon data->pgd_size = pgd_pages << data->pg_shift; 850e1d3c0fdSWill Deacon data->levels--; 851e1d3c0fdSWill Deacon } 852e1d3c0fdSWill Deacon } 853e1d3c0fdSWill Deacon 854e1d3c0fdSWill Deacon /* VTCR */ 855e1d3c0fdSWill Deacon reg = ARM_64_LPAE_S2_TCR_RES1 | 856e1d3c0fdSWill Deacon (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 857e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 858e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 859e1d3c0fdSWill Deacon 860e1d3c0fdSWill Deacon sl = ARM_LPAE_START_LVL(data); 861e1d3c0fdSWill Deacon 86206c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 863e1d3c0fdSWill Deacon case SZ_4K: 864e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 865e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 866e1d3c0fdSWill Deacon break; 867e1d3c0fdSWill Deacon case SZ_16K: 868e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 869e1d3c0fdSWill Deacon break; 870e1d3c0fdSWill Deacon case SZ_64K: 871e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 872e1d3c0fdSWill Deacon break; 873e1d3c0fdSWill Deacon } 874e1d3c0fdSWill Deacon 875e1d3c0fdSWill Deacon switch (cfg->oas) { 876e1d3c0fdSWill Deacon case 32: 877e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); 878e1d3c0fdSWill Deacon break; 879e1d3c0fdSWill Deacon case 36: 880e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); 881e1d3c0fdSWill Deacon break; 882e1d3c0fdSWill Deacon case 40: 883e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); 884e1d3c0fdSWill Deacon break; 885e1d3c0fdSWill Deacon case 42: 886e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); 887e1d3c0fdSWill Deacon break; 888e1d3c0fdSWill Deacon case 44: 889e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); 890e1d3c0fdSWill Deacon break; 891e1d3c0fdSWill Deacon case 48: 892e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); 893e1d3c0fdSWill Deacon break; 894e1d3c0fdSWill Deacon default: 895e1d3c0fdSWill Deacon goto out_free_data; 896e1d3c0fdSWill Deacon } 897e1d3c0fdSWill Deacon 898e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 899e1d3c0fdSWill Deacon reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; 900e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr = reg; 901e1d3c0fdSWill Deacon 902e1d3c0fdSWill Deacon /* Allocate pgd pages */ 903f8d54961SRobin Murphy data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 904e1d3c0fdSWill Deacon if (!data->pgd) 905e1d3c0fdSWill Deacon goto out_free_data; 906e1d3c0fdSWill Deacon 90787a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 90887a91b15SRobin Murphy wmb(); 909e1d3c0fdSWill Deacon 910e1d3c0fdSWill Deacon /* VTTBR */ 911e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 912e1d3c0fdSWill Deacon return &data->iop; 913e1d3c0fdSWill Deacon 914e1d3c0fdSWill Deacon out_free_data: 915e1d3c0fdSWill Deacon kfree(data); 916e1d3c0fdSWill Deacon return NULL; 917e1d3c0fdSWill Deacon } 918e1d3c0fdSWill Deacon 919e1d3c0fdSWill Deacon static struct io_pgtable * 920e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 921e1d3c0fdSWill Deacon { 922e1d3c0fdSWill Deacon struct io_pgtable *iop; 923e1d3c0fdSWill Deacon 924e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 925e1d3c0fdSWill Deacon return NULL; 926e1d3c0fdSWill Deacon 927e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 928e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 929e1d3c0fdSWill Deacon if (iop) { 930e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; 931e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; 932e1d3c0fdSWill Deacon } 933e1d3c0fdSWill Deacon 934e1d3c0fdSWill Deacon return iop; 935e1d3c0fdSWill Deacon } 936e1d3c0fdSWill Deacon 937e1d3c0fdSWill Deacon static struct io_pgtable * 938e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 939e1d3c0fdSWill Deacon { 940e1d3c0fdSWill Deacon struct io_pgtable *iop; 941e1d3c0fdSWill Deacon 942e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 943e1d3c0fdSWill Deacon return NULL; 944e1d3c0fdSWill Deacon 945e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 946e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 947e1d3c0fdSWill Deacon if (iop) 948e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; 949e1d3c0fdSWill Deacon 950e1d3c0fdSWill Deacon return iop; 951e1d3c0fdSWill Deacon } 952e1d3c0fdSWill Deacon 953e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 954e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 955e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 956e1d3c0fdSWill Deacon }; 957e1d3c0fdSWill Deacon 958e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 959e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 960e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 961e1d3c0fdSWill Deacon }; 962e1d3c0fdSWill Deacon 963e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 964e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 965e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 966e1d3c0fdSWill Deacon }; 967e1d3c0fdSWill Deacon 968e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 969e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 970e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 971e1d3c0fdSWill Deacon }; 972fe4b991dSWill Deacon 973fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 974fe4b991dSWill Deacon 975fe4b991dSWill Deacon static struct io_pgtable_cfg *cfg_cookie; 976fe4b991dSWill Deacon 977fe4b991dSWill Deacon static void dummy_tlb_flush_all(void *cookie) 978fe4b991dSWill Deacon { 979fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 980fe4b991dSWill Deacon } 981fe4b991dSWill Deacon 98206c610e8SRobin Murphy static void dummy_tlb_add_flush(unsigned long iova, size_t size, 98306c610e8SRobin Murphy size_t granule, bool leaf, void *cookie) 984fe4b991dSWill Deacon { 985fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 986fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 987fe4b991dSWill Deacon } 988fe4b991dSWill Deacon 989fe4b991dSWill Deacon static void dummy_tlb_sync(void *cookie) 990fe4b991dSWill Deacon { 991fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 992fe4b991dSWill Deacon } 993fe4b991dSWill Deacon 994dfed5f01SBhumika Goyal static const struct iommu_gather_ops dummy_tlb_ops __initconst = { 995fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 996fe4b991dSWill Deacon .tlb_add_flush = dummy_tlb_add_flush, 997fe4b991dSWill Deacon .tlb_sync = dummy_tlb_sync, 998fe4b991dSWill Deacon }; 999fe4b991dSWill Deacon 1000fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1001fe4b991dSWill Deacon { 1002fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1003fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1004fe4b991dSWill Deacon 1005fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1006fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 1007fe4b991dSWill Deacon pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", 1008fe4b991dSWill Deacon data->levels, data->pgd_size, data->pg_shift, 1009fe4b991dSWill Deacon data->bits_per_level, data->pgd); 1010fe4b991dSWill Deacon } 1011fe4b991dSWill Deacon 1012fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1013fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1014fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1015fe4b991dSWill Deacon selftest_running = false; \ 1016fe4b991dSWill Deacon -EFAULT; \ 1017fe4b991dSWill Deacon }) 1018fe4b991dSWill Deacon 1019fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1020fe4b991dSWill Deacon { 1021fe4b991dSWill Deacon static const enum io_pgtable_fmt fmts[] = { 1022fe4b991dSWill Deacon ARM_64_LPAE_S1, 1023fe4b991dSWill Deacon ARM_64_LPAE_S2, 1024fe4b991dSWill Deacon }; 1025fe4b991dSWill Deacon 1026fe4b991dSWill Deacon int i, j; 1027fe4b991dSWill Deacon unsigned long iova; 1028fe4b991dSWill Deacon size_t size; 1029fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1030fe4b991dSWill Deacon 1031fe4b991dSWill Deacon selftest_running = true; 1032fe4b991dSWill Deacon 1033fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1034fe4b991dSWill Deacon cfg_cookie = cfg; 1035fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1036fe4b991dSWill Deacon if (!ops) { 1037fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1038fe4b991dSWill Deacon return -ENOMEM; 1039fe4b991dSWill Deacon } 1040fe4b991dSWill Deacon 1041fe4b991dSWill Deacon /* 1042fe4b991dSWill Deacon * Initial sanity checks. 1043fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1044fe4b991dSWill Deacon */ 1045fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1046fe4b991dSWill Deacon return __FAIL(ops, i); 1047fe4b991dSWill Deacon 1048fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1049fe4b991dSWill Deacon return __FAIL(ops, i); 1050fe4b991dSWill Deacon 1051fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1052fe4b991dSWill Deacon return __FAIL(ops, i); 1053fe4b991dSWill Deacon 1054fe4b991dSWill Deacon /* 1055fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1056fe4b991dSWill Deacon */ 1057fe4b991dSWill Deacon iova = 0; 10584ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1059fe4b991dSWill Deacon size = 1UL << j; 1060fe4b991dSWill Deacon 1061fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1062fe4b991dSWill Deacon IOMMU_WRITE | 1063fe4b991dSWill Deacon IOMMU_NOEXEC | 1064fe4b991dSWill Deacon IOMMU_CACHE)) 1065fe4b991dSWill Deacon return __FAIL(ops, i); 1066fe4b991dSWill Deacon 1067fe4b991dSWill Deacon /* Overlapping mappings */ 1068fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1069fe4b991dSWill Deacon IOMMU_READ | IOMMU_NOEXEC)) 1070fe4b991dSWill Deacon return __FAIL(ops, i); 1071fe4b991dSWill Deacon 1072fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1073fe4b991dSWill Deacon return __FAIL(ops, i); 1074fe4b991dSWill Deacon 1075fe4b991dSWill Deacon iova += SZ_1G; 1076fe4b991dSWill Deacon } 1077fe4b991dSWill Deacon 1078fe4b991dSWill Deacon /* Partial unmap */ 1079fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1080fe4b991dSWill Deacon if (ops->unmap(ops, SZ_1G + size, size) != size) 1081fe4b991dSWill Deacon return __FAIL(ops, i); 1082fe4b991dSWill Deacon 1083fe4b991dSWill Deacon /* Remap of partial unmap */ 1084fe4b991dSWill Deacon if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 1085fe4b991dSWill Deacon return __FAIL(ops, i); 1086fe4b991dSWill Deacon 1087fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1088fe4b991dSWill Deacon return __FAIL(ops, i); 1089fe4b991dSWill Deacon 1090fe4b991dSWill Deacon /* Full unmap */ 1091fe4b991dSWill Deacon iova = 0; 1092fe4b991dSWill Deacon j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); 1093fe4b991dSWill Deacon while (j != BITS_PER_LONG) { 1094fe4b991dSWill Deacon size = 1UL << j; 1095fe4b991dSWill Deacon 1096fe4b991dSWill Deacon if (ops->unmap(ops, iova, size) != size) 1097fe4b991dSWill Deacon return __FAIL(ops, i); 1098fe4b991dSWill Deacon 1099fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1100fe4b991dSWill Deacon return __FAIL(ops, i); 1101fe4b991dSWill Deacon 1102fe4b991dSWill Deacon /* Remap full block */ 1103fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 1104fe4b991dSWill Deacon return __FAIL(ops, i); 1105fe4b991dSWill Deacon 1106fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1107fe4b991dSWill Deacon return __FAIL(ops, i); 1108fe4b991dSWill Deacon 1109fe4b991dSWill Deacon iova += SZ_1G; 1110fe4b991dSWill Deacon j++; 1111fe4b991dSWill Deacon j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); 1112fe4b991dSWill Deacon } 1113fe4b991dSWill Deacon 1114fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1115fe4b991dSWill Deacon } 1116fe4b991dSWill Deacon 1117fe4b991dSWill Deacon selftest_running = false; 1118fe4b991dSWill Deacon return 0; 1119fe4b991dSWill Deacon } 1120fe4b991dSWill Deacon 1121fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1122fe4b991dSWill Deacon { 1123fe4b991dSWill Deacon static const unsigned long pgsize[] = { 1124fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1125fe4b991dSWill Deacon SZ_16K | SZ_32M, 1126fe4b991dSWill Deacon SZ_64K | SZ_512M, 1127fe4b991dSWill Deacon }; 1128fe4b991dSWill Deacon 1129fe4b991dSWill Deacon static const unsigned int ias[] = { 1130fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1131fe4b991dSWill Deacon }; 1132fe4b991dSWill Deacon 1133fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1134fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1135fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1136fe4b991dSWill Deacon .oas = 48, 113781b3c252SRobin Murphy .quirks = IO_PGTABLE_QUIRK_NO_DMA, 1138fe4b991dSWill Deacon }; 1139fe4b991dSWill Deacon 1140fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1141fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1142fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1143fe4b991dSWill Deacon cfg.ias = ias[j]; 1144fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1145fe4b991dSWill Deacon pgsize[i], ias[j]); 1146fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1147fe4b991dSWill Deacon fail++; 1148fe4b991dSWill Deacon else 1149fe4b991dSWill Deacon pass++; 1150fe4b991dSWill Deacon } 1151fe4b991dSWill Deacon } 1152fe4b991dSWill Deacon 1153fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1154fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1155fe4b991dSWill Deacon } 1156fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1157fe4b991dSWill Deacon #endif 1158