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> 246c89928fSRobin Murphy #include <linux/bitops.h> 25e1d3c0fdSWill Deacon #include <linux/iommu.h> 26e1d3c0fdSWill Deacon #include <linux/kernel.h> 27e1d3c0fdSWill Deacon #include <linux/sizes.h> 28e1d3c0fdSWill Deacon #include <linux/slab.h> 29e1d3c0fdSWill Deacon #include <linux/types.h> 308f6aff98SLada Trimasova #include <linux/dma-mapping.h> 31e1d3c0fdSWill Deacon 3287a91b15SRobin Murphy #include <asm/barrier.h> 3387a91b15SRobin Murphy 34e1d3c0fdSWill Deacon #include "io-pgtable.h" 35e1d3c0fdSWill Deacon 366c89928fSRobin Murphy #define ARM_LPAE_MAX_ADDR_BITS 52 37e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 38e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS 4 39e1d3c0fdSWill Deacon 40e1d3c0fdSWill Deacon /* Struct accessors */ 41e1d3c0fdSWill Deacon #define io_pgtable_to_data(x) \ 42e1d3c0fdSWill Deacon container_of((x), struct arm_lpae_io_pgtable, iop) 43e1d3c0fdSWill Deacon 44e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x) \ 45e1d3c0fdSWill Deacon io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 46e1d3c0fdSWill Deacon 47e1d3c0fdSWill Deacon /* 48e1d3c0fdSWill Deacon * For consistency with the architecture, we always consider 49e1d3c0fdSWill Deacon * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 50e1d3c0fdSWill Deacon */ 51e1d3c0fdSWill Deacon #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) 52e1d3c0fdSWill Deacon 53e1d3c0fdSWill Deacon /* 54e1d3c0fdSWill Deacon * Calculate the right shift amount to get to the portion describing level l 55e1d3c0fdSWill Deacon * in a virtual address mapped by the pagetable in d. 56e1d3c0fdSWill Deacon */ 57e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d) \ 58e1d3c0fdSWill Deacon ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ 59e1d3c0fdSWill Deacon * (d)->bits_per_level) + (d)->pg_shift) 60e1d3c0fdSWill Deacon 6106c610e8SRobin Murphy #define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift) 6206c610e8SRobin Murphy 63367bd978SWill Deacon #define ARM_LPAE_PAGES_PER_PGD(d) \ 6406c610e8SRobin Murphy DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d)) 65e1d3c0fdSWill Deacon 66e1d3c0fdSWill Deacon /* 67e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 68e1d3c0fdSWill Deacon * pagetable in d. 69e1d3c0fdSWill Deacon */ 70e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 71e1d3c0fdSWill Deacon ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) 72e1d3c0fdSWill Deacon 73e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 74367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 75e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 76e1d3c0fdSWill Deacon 77e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 78e1d3c0fdSWill Deacon #define ARM_LPAE_BLOCK_SIZE(l,d) \ 79022f4e4fSRobin Murphy (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \ 80e1d3c0fdSWill Deacon ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) 81e1d3c0fdSWill Deacon 82e1d3c0fdSWill Deacon /* Page table bits */ 83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 84e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 85e1d3c0fdSWill Deacon 86e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 87e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 88e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 89e1d3c0fdSWill Deacon 906c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) 916c89928fSRobin Murphy 92c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 93e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 98c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 99e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 100e1d3c0fdSWill Deacon 101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 102e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 103e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 104e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 105e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 1062c3d273eSRobin Murphy /* Software bit for solving coherency races */ 1072c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) 108e1d3c0fdSWill Deacon 109e1d3c0fdSWill Deacon /* Stage-1 PTE */ 110e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 111e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 112e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 113e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 114e1d3c0fdSWill Deacon 115e1d3c0fdSWill Deacon /* Stage-2 PTE */ 116e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 117e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 118e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 119e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 120e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 121e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 122e1d3c0fdSWill Deacon 123e1d3c0fdSWill Deacon /* Register bits */ 124e1d3c0fdSWill Deacon #define ARM_32_LPAE_TCR_EAE (1 << 31) 125e1d3c0fdSWill Deacon #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) 126e1d3c0fdSWill Deacon 12763979b8dSWill Deacon #define ARM_LPAE_TCR_EPD1 (1 << 23) 12863979b8dSWill Deacon 129e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_4K (0 << 14) 130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_64K (1 << 14) 131e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_16K (2 << 14) 132e1d3c0fdSWill Deacon 133e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_SHIFT 12 134e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_MASK 0x3 135e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS 0 136e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS 2 137e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS 3 138e1d3c0fdSWill Deacon 139e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_ORGN0_SHIFT 10 140e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IRGN0_SHIFT 8 141e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_MASK 0x3 142e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC 0 143e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA 1 144e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT 2 145e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB 3 146e1d3c0fdSWill Deacon 147e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_SHIFT 6 148e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_MASK 0x3 149e1d3c0fdSWill Deacon 150e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 151e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SZ_MASK 0xf 152e1d3c0fdSWill Deacon 153e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_SHIFT 16 154e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_MASK 0x7 155e1d3c0fdSWill Deacon 156e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_SHIFT 32 157e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_MASK 0x7 158e1d3c0fdSWill Deacon 159e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 160e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 161e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 162e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 163e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 164e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 1656c89928fSRobin Murphy #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL 166e1d3c0fdSWill Deacon 167e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 168e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 169e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 170e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 171e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 172e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 173e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 174e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 175e1d3c0fdSWill Deacon 176e1d3c0fdSWill Deacon /* IOPTE accessors */ 1776c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 178e1d3c0fdSWill Deacon 179e1d3c0fdSWill Deacon #define iopte_type(pte,l) \ 180e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 181e1d3c0fdSWill Deacon 182e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 183e1d3c0fdSWill Deacon 184e1d3c0fdSWill Deacon #define iopte_leaf(pte,l) \ 185e1d3c0fdSWill Deacon (l == (ARM_LPAE_MAX_LEVELS - 1) ? \ 186e1d3c0fdSWill Deacon (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \ 187e1d3c0fdSWill Deacon (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK)) 188e1d3c0fdSWill Deacon 189e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 190e1d3c0fdSWill Deacon struct io_pgtable iop; 191e1d3c0fdSWill Deacon 192e1d3c0fdSWill Deacon int levels; 193e1d3c0fdSWill Deacon size_t pgd_size; 194e1d3c0fdSWill Deacon unsigned long pg_shift; 195e1d3c0fdSWill Deacon unsigned long bits_per_level; 196e1d3c0fdSWill Deacon 197e1d3c0fdSWill Deacon void *pgd; 198e1d3c0fdSWill Deacon }; 199e1d3c0fdSWill Deacon 200e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 201e1d3c0fdSWill Deacon 2026c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, 2036c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 2046c89928fSRobin Murphy { 2056c89928fSRobin Murphy arm_lpae_iopte pte = paddr; 2066c89928fSRobin Murphy 2076c89928fSRobin Murphy /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ 2086c89928fSRobin Murphy return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; 2096c89928fSRobin Murphy } 2106c89928fSRobin Murphy 2116c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, 2126c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 2136c89928fSRobin Murphy { 21478688059SRobin Murphy u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; 2156c89928fSRobin Murphy 2166c89928fSRobin Murphy if (data->pg_shift < 16) 2176c89928fSRobin Murphy return paddr; 2186c89928fSRobin Murphy 2196c89928fSRobin Murphy /* Rotate the packed high-order bits back to the top */ 2206c89928fSRobin Murphy return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); 2216c89928fSRobin Murphy } 2226c89928fSRobin Murphy 223fe4b991dSWill Deacon static bool selftest_running = false; 224fe4b991dSWill Deacon 225ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 226f8d54961SRobin Murphy { 227ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 228f8d54961SRobin Murphy } 229f8d54961SRobin Murphy 230f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 231f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 232f8d54961SRobin Murphy { 233f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 2344b123757SRobin Murphy int order = get_order(size); 2354b123757SRobin Murphy struct page *p; 236f8d54961SRobin Murphy dma_addr_t dma; 2374b123757SRobin Murphy void *pages; 238f8d54961SRobin Murphy 2394b123757SRobin Murphy VM_BUG_ON((gfp & __GFP_HIGHMEM)); 240*fac83d29SJean-Philippe Brucker p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 241*fac83d29SJean-Philippe Brucker gfp | __GFP_ZERO, order); 2424b123757SRobin Murphy if (!p) 243f8d54961SRobin Murphy return NULL; 244f8d54961SRobin Murphy 2454b123757SRobin Murphy pages = page_address(p); 24681b3c252SRobin Murphy if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) { 247f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 248f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 249f8d54961SRobin Murphy goto out_free; 250f8d54961SRobin Murphy /* 251f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 252ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 253ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 254f8d54961SRobin Murphy */ 255ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 256f8d54961SRobin Murphy goto out_unmap; 257f8d54961SRobin Murphy } 258f8d54961SRobin Murphy 259f8d54961SRobin Murphy return pages; 260f8d54961SRobin Murphy 261f8d54961SRobin Murphy out_unmap: 262f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 263f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 264f8d54961SRobin Murphy out_free: 2654b123757SRobin Murphy __free_pages(p, order); 266f8d54961SRobin Murphy return NULL; 267f8d54961SRobin Murphy } 268f8d54961SRobin Murphy 269f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 270f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 271f8d54961SRobin Murphy { 27281b3c252SRobin Murphy if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) 273ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 274f8d54961SRobin Murphy size, DMA_TO_DEVICE); 2754b123757SRobin Murphy free_pages((unsigned long)pages, get_order(size)); 276f8d54961SRobin Murphy } 277f8d54961SRobin Murphy 2782c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 2792c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2802c3d273eSRobin Murphy { 2812c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 2822c3d273eSRobin Murphy sizeof(*ptep), DMA_TO_DEVICE); 2832c3d273eSRobin Murphy } 2842c3d273eSRobin Murphy 285f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 28687a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 287f8d54961SRobin Murphy { 288f8d54961SRobin Murphy *ptep = pte; 289f8d54961SRobin Murphy 29081b3c252SRobin Murphy if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) 2912c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 292f8d54961SRobin Murphy } 293f8d54961SRobin Murphy 294193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 295cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 296cf27ec93SWill Deacon arm_lpae_iopte *ptep); 297cf27ec93SWill Deacon 298fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 299fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 300fb3a9579SRobin Murphy int lvl, arm_lpae_iopte *ptep) 301fb3a9579SRobin Murphy { 302fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 303fb3a9579SRobin Murphy 304fb3a9579SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 305fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_NS; 306fb3a9579SRobin Murphy 307fb3a9579SRobin Murphy if (lvl == ARM_LPAE_MAX_LEVELS - 1) 308fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 309fb3a9579SRobin Murphy else 310fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 311fb3a9579SRobin Murphy 312fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; 3136c89928fSRobin Murphy pte |= paddr_to_iopte(paddr, data); 314fb3a9579SRobin Murphy 315fb3a9579SRobin Murphy __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); 316fb3a9579SRobin Murphy } 317fb3a9579SRobin Murphy 318e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 319e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 320e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 321e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 322e1d3c0fdSWill Deacon { 323fb3a9579SRobin Murphy arm_lpae_iopte pte = *ptep; 324e1d3c0fdSWill Deacon 325fb3a9579SRobin Murphy if (iopte_leaf(pte, lvl)) { 326cf27ec93SWill Deacon /* We require an unmap first */ 327fe4b991dSWill Deacon WARN_ON(!selftest_running); 328e1d3c0fdSWill Deacon return -EEXIST; 329fb3a9579SRobin Murphy } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 330cf27ec93SWill Deacon /* 331cf27ec93SWill Deacon * We need to unmap and free the old table before 332cf27ec93SWill Deacon * overwriting it with a block entry. 333cf27ec93SWill Deacon */ 334cf27ec93SWill Deacon arm_lpae_iopte *tblp; 335cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 336cf27ec93SWill Deacon 337cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 338cf27ec93SWill Deacon if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz)) 339cf27ec93SWill Deacon return -EINVAL; 340fe4b991dSWill Deacon } 341e1d3c0fdSWill Deacon 342fb3a9579SRobin Murphy __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); 343e1d3c0fdSWill Deacon return 0; 344e1d3c0fdSWill Deacon } 345e1d3c0fdSWill Deacon 346fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 347fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3482c3d273eSRobin Murphy arm_lpae_iopte curr, 349fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 350fb3a9579SRobin Murphy { 3512c3d273eSRobin Murphy arm_lpae_iopte old, new; 352fb3a9579SRobin Murphy 353fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 354fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 355fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 356fb3a9579SRobin Murphy 35777f34458SWill Deacon /* 35877f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 35977f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 36077f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 36177f34458SWill Deacon */ 36277f34458SWill Deacon dma_wmb(); 3632c3d273eSRobin Murphy 3642c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3652c3d273eSRobin Murphy 3662c3d273eSRobin Murphy if ((cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) || 3672c3d273eSRobin Murphy (old & ARM_LPAE_PTE_SW_SYNC)) 3682c3d273eSRobin Murphy return old; 3692c3d273eSRobin Murphy 3702c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 3712c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3722c3d273eSRobin Murphy if (old == curr) 3732c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3742c3d273eSRobin Murphy 3752c3d273eSRobin Murphy return old; 376fb3a9579SRobin Murphy } 377fb3a9579SRobin Murphy 378e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 379e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 380e1d3c0fdSWill Deacon int lvl, arm_lpae_iopte *ptep) 381e1d3c0fdSWill Deacon { 382e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 383e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3842c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 385f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 386e1d3c0fdSWill Deacon 387e1d3c0fdSWill Deacon /* Find our entry at the current level */ 388e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 389e1d3c0fdSWill Deacon 390e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 391f8d54961SRobin Murphy if (size == block_size && (size & cfg->pgsize_bitmap)) 392e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 393e1d3c0fdSWill Deacon 394e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 395e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 396e1d3c0fdSWill Deacon return -EINVAL; 397e1d3c0fdSWill Deacon 398e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3992c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 400e1d3c0fdSWill Deacon if (!pte) { 4012c3d273eSRobin Murphy cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); 402e1d3c0fdSWill Deacon if (!cptep) 403e1d3c0fdSWill Deacon return -ENOMEM; 404e1d3c0fdSWill Deacon 4052c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 4062c3d273eSRobin Murphy if (pte) 4072c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 4082c3d273eSRobin Murphy } else if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) && 4092c3d273eSRobin Murphy !(pte & ARM_LPAE_PTE_SW_SYNC)) { 4102c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 4112c3d273eSRobin Murphy } 4122c3d273eSRobin Murphy 4132c3d273eSRobin Murphy if (pte && !iopte_leaf(pte, lvl)) { 414e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 4152c3d273eSRobin Murphy } else if (pte) { 416ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 417ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 418ed46e66cSOleksandr Tyshchenko return -EEXIST; 419e1d3c0fdSWill Deacon } 420e1d3c0fdSWill Deacon 421e1d3c0fdSWill Deacon /* Rinse, repeat */ 422e1d3c0fdSWill Deacon return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 423e1d3c0fdSWill Deacon } 424e1d3c0fdSWill Deacon 425e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 426e1d3c0fdSWill Deacon int prot) 427e1d3c0fdSWill Deacon { 428e1d3c0fdSWill Deacon arm_lpae_iopte pte; 429e1d3c0fdSWill Deacon 430e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 431e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 432e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 433e1d3c0fdSWill Deacon 434e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 435e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 436e1d3c0fdSWill Deacon 437e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 438e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 439e7468a23SJeremy Gebben 440fb948251SRobin Murphy if (prot & IOMMU_MMIO) 441fb948251SRobin Murphy pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 442fb948251SRobin Murphy << ARM_LPAE_PTE_ATTRINDX_SHIFT); 443fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 444e1d3c0fdSWill Deacon pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 445e1d3c0fdSWill Deacon << ARM_LPAE_PTE_ATTRINDX_SHIFT); 446e1d3c0fdSWill Deacon } else { 447e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 448e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 449e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 450e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 451e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 452fb948251SRobin Murphy if (prot & IOMMU_MMIO) 453fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 454fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 455e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 456e1d3c0fdSWill Deacon else 457e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 458e1d3c0fdSWill Deacon } 459e1d3c0fdSWill Deacon 460e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 461e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 462e1d3c0fdSWill Deacon 463e1d3c0fdSWill Deacon return pte; 464e1d3c0fdSWill Deacon } 465e1d3c0fdSWill Deacon 466e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 467e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, int iommu_prot) 468e1d3c0fdSWill Deacon { 469e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 470e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 47187a91b15SRobin Murphy int ret, lvl = ARM_LPAE_START_LVL(data); 472e1d3c0fdSWill Deacon arm_lpae_iopte prot; 473e1d3c0fdSWill Deacon 474e1d3c0fdSWill Deacon /* If no access, then nothing to do */ 475e1d3c0fdSWill Deacon if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 476e1d3c0fdSWill Deacon return 0; 477e1d3c0fdSWill Deacon 47876557391SRobin Murphy if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) || 47976557391SRobin Murphy paddr >= (1ULL << data->iop.cfg.oas))) 48076557391SRobin Murphy return -ERANGE; 48176557391SRobin Murphy 482e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 48387a91b15SRobin Murphy ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 48487a91b15SRobin Murphy /* 48587a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 48687a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 48787a91b15SRobin Murphy */ 48887a91b15SRobin Murphy wmb(); 48987a91b15SRobin Murphy 49087a91b15SRobin Murphy return ret; 491e1d3c0fdSWill Deacon } 492e1d3c0fdSWill Deacon 493e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 494e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 495e1d3c0fdSWill Deacon { 496e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 497e1d3c0fdSWill Deacon unsigned long table_size; 498e1d3c0fdSWill Deacon 499e1d3c0fdSWill Deacon if (lvl == ARM_LPAE_START_LVL(data)) 500e1d3c0fdSWill Deacon table_size = data->pgd_size; 501e1d3c0fdSWill Deacon else 50206c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 503e1d3c0fdSWill Deacon 504e1d3c0fdSWill Deacon start = ptep; 50512c2ab09SWill Deacon 50612c2ab09SWill Deacon /* Only leaf entries at the last level */ 50712c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 50812c2ab09SWill Deacon end = ptep; 50912c2ab09SWill Deacon else 510e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 511e1d3c0fdSWill Deacon 512e1d3c0fdSWill Deacon while (ptep != end) { 513e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 514e1d3c0fdSWill Deacon 515e1d3c0fdSWill Deacon if (!pte || iopte_leaf(pte, lvl)) 516e1d3c0fdSWill Deacon continue; 517e1d3c0fdSWill Deacon 518e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 519e1d3c0fdSWill Deacon } 520e1d3c0fdSWill Deacon 521f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 522e1d3c0fdSWill Deacon } 523e1d3c0fdSWill Deacon 524e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 525e1d3c0fdSWill Deacon { 526e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 527e1d3c0fdSWill Deacon 528e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); 529e1d3c0fdSWill Deacon kfree(data); 530e1d3c0fdSWill Deacon } 531e1d3c0fdSWill Deacon 532193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 533e1d3c0fdSWill Deacon unsigned long iova, size_t size, 534fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 535fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 536e1d3c0fdSWill Deacon { 537fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 538fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 539e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 540fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 541fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 542fb3a9579SRobin Murphy int i, unmap_idx = -1; 543e1d3c0fdSWill Deacon 544fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 545fb3a9579SRobin Murphy return 0; 546e1d3c0fdSWill Deacon 547fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 548fb3a9579SRobin Murphy if (!tablep) 549fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 550e1d3c0fdSWill Deacon 551fb3a9579SRobin Murphy if (size == split_sz) 552fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 553fb3a9579SRobin Murphy 5546c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 555fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 556fb3a9579SRobin Murphy 557fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 558e1d3c0fdSWill Deacon /* Unmap! */ 559fb3a9579SRobin Murphy if (i == unmap_idx) 560e1d3c0fdSWill Deacon continue; 561e1d3c0fdSWill Deacon 562fb3a9579SRobin Murphy __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); 563e1d3c0fdSWill Deacon } 564e1d3c0fdSWill Deacon 5652c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5662c3d273eSRobin Murphy if (pte != blk_pte) { 5672c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5682c3d273eSRobin Murphy /* 5692c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5702c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5712c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5722c3d273eSRobin Murphy */ 5732c3d273eSRobin Murphy if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) 5742c3d273eSRobin Murphy return 0; 5752c3d273eSRobin Murphy 5762c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 5772c3d273eSRobin Murphy } 578fb3a9579SRobin Murphy 579fb3a9579SRobin Murphy if (unmap_idx < 0) 580fb3a9579SRobin Murphy return __arm_lpae_unmap(data, iova, size, lvl, tablep); 581fb3a9579SRobin Murphy 582fb3a9579SRobin Murphy io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true); 583e1d3c0fdSWill Deacon return size; 584e1d3c0fdSWill Deacon } 585e1d3c0fdSWill Deacon 586193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 587e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 588e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 589e1d3c0fdSWill Deacon { 590e1d3c0fdSWill Deacon arm_lpae_iopte pte; 591507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 592e1d3c0fdSWill Deacon 5932eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5942eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5952eb97c78SRobin Murphy return 0; 5962eb97c78SRobin Murphy 597e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 5982c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 5992eb97c78SRobin Murphy if (WARN_ON(!pte)) 600e1d3c0fdSWill Deacon return 0; 601e1d3c0fdSWill Deacon 602e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 603fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 604507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 605e1d3c0fdSWill Deacon 606e1d3c0fdSWill Deacon if (!iopte_leaf(pte, lvl)) { 607e1d3c0fdSWill Deacon /* Also flush any partial walks */ 608507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(iop, iova, size, 609507e4c9dSRobin Murphy ARM_LPAE_GRANULE(data), false); 610507e4c9dSRobin Murphy io_pgtable_tlb_sync(iop); 611e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 612e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 613e1d3c0fdSWill Deacon } else { 614507e4c9dSRobin Murphy io_pgtable_tlb_add_flush(iop, iova, size, size, true); 615e1d3c0fdSWill Deacon } 616e1d3c0fdSWill Deacon 617e1d3c0fdSWill Deacon return size; 618e1d3c0fdSWill Deacon } else if (iopte_leaf(pte, lvl)) { 619e1d3c0fdSWill Deacon /* 620e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 621e1d3c0fdSWill Deacon * minus the part we want to unmap 622e1d3c0fdSWill Deacon */ 623fb3a9579SRobin Murphy return arm_lpae_split_blk_unmap(data, iova, size, pte, 624fb3a9579SRobin Murphy lvl + 1, ptep); 625e1d3c0fdSWill Deacon } 626e1d3c0fdSWill Deacon 627e1d3c0fdSWill Deacon /* Keep on walkin' */ 628e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 629e1d3c0fdSWill Deacon return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep); 630e1d3c0fdSWill Deacon } 631e1d3c0fdSWill Deacon 632193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 633e1d3c0fdSWill Deacon size_t size) 634e1d3c0fdSWill Deacon { 635e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 636e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 637e1d3c0fdSWill Deacon int lvl = ARM_LPAE_START_LVL(data); 638e1d3c0fdSWill Deacon 63976557391SRobin Murphy if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias))) 64076557391SRobin Murphy return 0; 64176557391SRobin Murphy 64232b12449SRobin Murphy return __arm_lpae_unmap(data, iova, size, lvl, ptep); 643e1d3c0fdSWill Deacon } 644e1d3c0fdSWill Deacon 645e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 646e1d3c0fdSWill Deacon unsigned long iova) 647e1d3c0fdSWill Deacon { 648e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 649e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 650e1d3c0fdSWill Deacon int lvl = ARM_LPAE_START_LVL(data); 651e1d3c0fdSWill Deacon 652e1d3c0fdSWill Deacon do { 653e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 654e1d3c0fdSWill Deacon if (!ptep) 655e1d3c0fdSWill Deacon return 0; 656e1d3c0fdSWill Deacon 657e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6582c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6592c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 660e1d3c0fdSWill Deacon 661e1d3c0fdSWill Deacon /* Valid entry? */ 662e1d3c0fdSWill Deacon if (!pte) 663e1d3c0fdSWill Deacon return 0; 664e1d3c0fdSWill Deacon 665e1d3c0fdSWill Deacon /* Leaf entry? */ 666e1d3c0fdSWill Deacon if (iopte_leaf(pte,lvl)) 667e1d3c0fdSWill Deacon goto found_translation; 668e1d3c0fdSWill Deacon 669e1d3c0fdSWill Deacon /* Take it to the next level */ 670e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 671e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 672e1d3c0fdSWill Deacon 673e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 674e1d3c0fdSWill Deacon return 0; 675e1d3c0fdSWill Deacon 676e1d3c0fdSWill Deacon found_translation: 6777c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 6786c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 679e1d3c0fdSWill Deacon } 680e1d3c0fdSWill Deacon 681e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 682e1d3c0fdSWill Deacon { 6836c89928fSRobin Murphy unsigned long granule, page_sizes; 6846c89928fSRobin Murphy unsigned int max_addr_bits = 48; 685e1d3c0fdSWill Deacon 686e1d3c0fdSWill Deacon /* 687e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 688e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 689e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 690e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 691e1d3c0fdSWill Deacon * chosen granule. 692e1d3c0fdSWill Deacon */ 693e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 694e1d3c0fdSWill Deacon granule = PAGE_SIZE; 695e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 696e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 697e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 698e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 699e1d3c0fdSWill Deacon else 700e1d3c0fdSWill Deacon granule = 0; 701e1d3c0fdSWill Deacon 702e1d3c0fdSWill Deacon switch (granule) { 703e1d3c0fdSWill Deacon case SZ_4K: 7046c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 705e1d3c0fdSWill Deacon break; 706e1d3c0fdSWill Deacon case SZ_16K: 7076c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 708e1d3c0fdSWill Deacon break; 709e1d3c0fdSWill Deacon case SZ_64K: 7106c89928fSRobin Murphy max_addr_bits = 52; 7116c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7126c89928fSRobin Murphy if (cfg->oas > 48) 7136c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 714e1d3c0fdSWill Deacon break; 715e1d3c0fdSWill Deacon default: 7166c89928fSRobin Murphy page_sizes = 0; 717e1d3c0fdSWill Deacon } 7186c89928fSRobin Murphy 7196c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7206c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7216c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 722e1d3c0fdSWill Deacon } 723e1d3c0fdSWill Deacon 724e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 725e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 726e1d3c0fdSWill Deacon { 727e1d3c0fdSWill Deacon unsigned long va_bits, pgd_bits; 728e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 729e1d3c0fdSWill Deacon 730e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 731e1d3c0fdSWill Deacon 732e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 733e1d3c0fdSWill Deacon return NULL; 734e1d3c0fdSWill Deacon 735e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 736e1d3c0fdSWill Deacon return NULL; 737e1d3c0fdSWill Deacon 738e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 739e1d3c0fdSWill Deacon return NULL; 740e1d3c0fdSWill Deacon 741ffcb6d16SRobin Murphy if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { 742ffcb6d16SRobin Murphy dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); 743ffcb6d16SRobin Murphy return NULL; 744ffcb6d16SRobin Murphy } 745ffcb6d16SRobin Murphy 746e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 747e1d3c0fdSWill Deacon if (!data) 748e1d3c0fdSWill Deacon return NULL; 749e1d3c0fdSWill Deacon 750e1d3c0fdSWill Deacon data->pg_shift = __ffs(cfg->pgsize_bitmap); 751e1d3c0fdSWill Deacon data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); 752e1d3c0fdSWill Deacon 753e1d3c0fdSWill Deacon va_bits = cfg->ias - data->pg_shift; 754e1d3c0fdSWill Deacon data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 755e1d3c0fdSWill Deacon 756e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 757e1d3c0fdSWill Deacon pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); 758e1d3c0fdSWill Deacon data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); 759e1d3c0fdSWill Deacon 760e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 761e1d3c0fdSWill Deacon .map = arm_lpae_map, 762e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 763e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 764e1d3c0fdSWill Deacon }; 765e1d3c0fdSWill Deacon 766e1d3c0fdSWill Deacon return data; 767e1d3c0fdSWill Deacon } 768e1d3c0fdSWill Deacon 769e1d3c0fdSWill Deacon static struct io_pgtable * 770e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 771e1d3c0fdSWill Deacon { 772e1d3c0fdSWill Deacon u64 reg; 7733850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 774e1d3c0fdSWill Deacon 77581b3c252SRobin Murphy if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | IO_PGTABLE_QUIRK_NO_DMA)) 7763850db49SRobin Murphy return NULL; 7773850db49SRobin Murphy 7783850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 779e1d3c0fdSWill Deacon if (!data) 780e1d3c0fdSWill Deacon return NULL; 781e1d3c0fdSWill Deacon 782e1d3c0fdSWill Deacon /* TCR */ 783e1d3c0fdSWill Deacon reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 784e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 785e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 786e1d3c0fdSWill Deacon 78706c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 788e1d3c0fdSWill Deacon case SZ_4K: 789e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 790e1d3c0fdSWill Deacon break; 791e1d3c0fdSWill Deacon case SZ_16K: 792e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 793e1d3c0fdSWill Deacon break; 794e1d3c0fdSWill Deacon case SZ_64K: 795e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 796e1d3c0fdSWill Deacon break; 797e1d3c0fdSWill Deacon } 798e1d3c0fdSWill Deacon 799e1d3c0fdSWill Deacon switch (cfg->oas) { 800e1d3c0fdSWill Deacon case 32: 801e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); 802e1d3c0fdSWill Deacon break; 803e1d3c0fdSWill Deacon case 36: 804e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); 805e1d3c0fdSWill Deacon break; 806e1d3c0fdSWill Deacon case 40: 807e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); 808e1d3c0fdSWill Deacon break; 809e1d3c0fdSWill Deacon case 42: 810e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); 811e1d3c0fdSWill Deacon break; 812e1d3c0fdSWill Deacon case 44: 813e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); 814e1d3c0fdSWill Deacon break; 815e1d3c0fdSWill Deacon case 48: 816e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); 817e1d3c0fdSWill Deacon break; 8186c89928fSRobin Murphy case 52: 8196c89928fSRobin Murphy reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT); 8206c89928fSRobin Murphy break; 821e1d3c0fdSWill Deacon default: 822e1d3c0fdSWill Deacon goto out_free_data; 823e1d3c0fdSWill Deacon } 824e1d3c0fdSWill Deacon 825e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 82663979b8dSWill Deacon 82763979b8dSWill Deacon /* Disable speculative walks through TTBR1 */ 82863979b8dSWill Deacon reg |= ARM_LPAE_TCR_EPD1; 829e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr = reg; 830e1d3c0fdSWill Deacon 831e1d3c0fdSWill Deacon /* MAIRs */ 832e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 833e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 834e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 835e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 836e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 837e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 838e1d3c0fdSWill Deacon 839e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[0] = reg; 840e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[1] = 0; 841e1d3c0fdSWill Deacon 842e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 843f8d54961SRobin Murphy data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 844e1d3c0fdSWill Deacon if (!data->pgd) 845e1d3c0fdSWill Deacon goto out_free_data; 846e1d3c0fdSWill Deacon 84787a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 84887a91b15SRobin Murphy wmb(); 849e1d3c0fdSWill Deacon 850e1d3c0fdSWill Deacon /* TTBRs */ 851e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); 852e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[1] = 0; 853e1d3c0fdSWill Deacon return &data->iop; 854e1d3c0fdSWill Deacon 855e1d3c0fdSWill Deacon out_free_data: 856e1d3c0fdSWill Deacon kfree(data); 857e1d3c0fdSWill Deacon return NULL; 858e1d3c0fdSWill Deacon } 859e1d3c0fdSWill Deacon 860e1d3c0fdSWill Deacon static struct io_pgtable * 861e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 862e1d3c0fdSWill Deacon { 863e1d3c0fdSWill Deacon u64 reg, sl; 8643850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 865e1d3c0fdSWill Deacon 8663850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 86781b3c252SRobin Murphy if (cfg->quirks & ~IO_PGTABLE_QUIRK_NO_DMA) 8683850db49SRobin Murphy return NULL; 8693850db49SRobin Murphy 8703850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 871e1d3c0fdSWill Deacon if (!data) 872e1d3c0fdSWill Deacon return NULL; 873e1d3c0fdSWill Deacon 874e1d3c0fdSWill Deacon /* 875e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 876e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 877e1d3c0fdSWill Deacon */ 878e1d3c0fdSWill Deacon if (data->levels == ARM_LPAE_MAX_LEVELS) { 879e1d3c0fdSWill Deacon unsigned long pgd_pages; 880e1d3c0fdSWill Deacon 881e1d3c0fdSWill Deacon pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); 882e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 883e1d3c0fdSWill Deacon data->pgd_size = pgd_pages << data->pg_shift; 884e1d3c0fdSWill Deacon data->levels--; 885e1d3c0fdSWill Deacon } 886e1d3c0fdSWill Deacon } 887e1d3c0fdSWill Deacon 888e1d3c0fdSWill Deacon /* VTCR */ 889e1d3c0fdSWill Deacon reg = ARM_64_LPAE_S2_TCR_RES1 | 890e1d3c0fdSWill Deacon (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 891e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 892e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 893e1d3c0fdSWill Deacon 894e1d3c0fdSWill Deacon sl = ARM_LPAE_START_LVL(data); 895e1d3c0fdSWill Deacon 89606c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 897e1d3c0fdSWill Deacon case SZ_4K: 898e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 899e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 900e1d3c0fdSWill Deacon break; 901e1d3c0fdSWill Deacon case SZ_16K: 902e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 903e1d3c0fdSWill Deacon break; 904e1d3c0fdSWill Deacon case SZ_64K: 905e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 906e1d3c0fdSWill Deacon break; 907e1d3c0fdSWill Deacon } 908e1d3c0fdSWill Deacon 909e1d3c0fdSWill Deacon switch (cfg->oas) { 910e1d3c0fdSWill Deacon case 32: 911e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); 912e1d3c0fdSWill Deacon break; 913e1d3c0fdSWill Deacon case 36: 914e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); 915e1d3c0fdSWill Deacon break; 916e1d3c0fdSWill Deacon case 40: 917e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); 918e1d3c0fdSWill Deacon break; 919e1d3c0fdSWill Deacon case 42: 920e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); 921e1d3c0fdSWill Deacon break; 922e1d3c0fdSWill Deacon case 44: 923e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); 924e1d3c0fdSWill Deacon break; 925e1d3c0fdSWill Deacon case 48: 926e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); 927e1d3c0fdSWill Deacon break; 9286c89928fSRobin Murphy case 52: 9296c89928fSRobin Murphy reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT); 9306c89928fSRobin Murphy break; 931e1d3c0fdSWill Deacon default: 932e1d3c0fdSWill Deacon goto out_free_data; 933e1d3c0fdSWill Deacon } 934e1d3c0fdSWill Deacon 935e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 936e1d3c0fdSWill Deacon reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; 937e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr = reg; 938e1d3c0fdSWill Deacon 939e1d3c0fdSWill Deacon /* Allocate pgd pages */ 940f8d54961SRobin Murphy data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg); 941e1d3c0fdSWill Deacon if (!data->pgd) 942e1d3c0fdSWill Deacon goto out_free_data; 943e1d3c0fdSWill Deacon 94487a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 94587a91b15SRobin Murphy wmb(); 946e1d3c0fdSWill Deacon 947e1d3c0fdSWill Deacon /* VTTBR */ 948e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 949e1d3c0fdSWill Deacon return &data->iop; 950e1d3c0fdSWill Deacon 951e1d3c0fdSWill Deacon out_free_data: 952e1d3c0fdSWill Deacon kfree(data); 953e1d3c0fdSWill Deacon return NULL; 954e1d3c0fdSWill Deacon } 955e1d3c0fdSWill Deacon 956e1d3c0fdSWill Deacon static struct io_pgtable * 957e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 958e1d3c0fdSWill Deacon { 959e1d3c0fdSWill Deacon struct io_pgtable *iop; 960e1d3c0fdSWill Deacon 961e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 962e1d3c0fdSWill Deacon return NULL; 963e1d3c0fdSWill Deacon 964e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 965e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 966e1d3c0fdSWill Deacon if (iop) { 967e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; 968e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; 969e1d3c0fdSWill Deacon } 970e1d3c0fdSWill Deacon 971e1d3c0fdSWill Deacon return iop; 972e1d3c0fdSWill Deacon } 973e1d3c0fdSWill Deacon 974e1d3c0fdSWill Deacon static struct io_pgtable * 975e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 976e1d3c0fdSWill Deacon { 977e1d3c0fdSWill Deacon struct io_pgtable *iop; 978e1d3c0fdSWill Deacon 979e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 980e1d3c0fdSWill Deacon return NULL; 981e1d3c0fdSWill Deacon 982e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 983e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 984e1d3c0fdSWill Deacon if (iop) 985e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; 986e1d3c0fdSWill Deacon 987e1d3c0fdSWill Deacon return iop; 988e1d3c0fdSWill Deacon } 989e1d3c0fdSWill Deacon 990e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 991e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 992e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 993e1d3c0fdSWill Deacon }; 994e1d3c0fdSWill Deacon 995e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 996e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 997e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 998e1d3c0fdSWill Deacon }; 999e1d3c0fdSWill Deacon 1000e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1001e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1002e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1003e1d3c0fdSWill Deacon }; 1004e1d3c0fdSWill Deacon 1005e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1006e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1007e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1008e1d3c0fdSWill Deacon }; 1009fe4b991dSWill Deacon 1010fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1011fe4b991dSWill Deacon 1012fe4b991dSWill Deacon static struct io_pgtable_cfg *cfg_cookie; 1013fe4b991dSWill Deacon 1014fe4b991dSWill Deacon static void dummy_tlb_flush_all(void *cookie) 1015fe4b991dSWill Deacon { 1016fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1017fe4b991dSWill Deacon } 1018fe4b991dSWill Deacon 101906c610e8SRobin Murphy static void dummy_tlb_add_flush(unsigned long iova, size_t size, 102006c610e8SRobin Murphy size_t granule, bool leaf, void *cookie) 1021fe4b991dSWill Deacon { 1022fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1023fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1024fe4b991dSWill Deacon } 1025fe4b991dSWill Deacon 1026fe4b991dSWill Deacon static void dummy_tlb_sync(void *cookie) 1027fe4b991dSWill Deacon { 1028fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1029fe4b991dSWill Deacon } 1030fe4b991dSWill Deacon 1031dfed5f01SBhumika Goyal static const struct iommu_gather_ops dummy_tlb_ops __initconst = { 1032fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 1033fe4b991dSWill Deacon .tlb_add_flush = dummy_tlb_add_flush, 1034fe4b991dSWill Deacon .tlb_sync = dummy_tlb_sync, 1035fe4b991dSWill Deacon }; 1036fe4b991dSWill Deacon 1037fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1038fe4b991dSWill Deacon { 1039fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1040fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1041fe4b991dSWill Deacon 1042fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1043fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 1044fe4b991dSWill Deacon pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", 1045fe4b991dSWill Deacon data->levels, data->pgd_size, data->pg_shift, 1046fe4b991dSWill Deacon data->bits_per_level, data->pgd); 1047fe4b991dSWill Deacon } 1048fe4b991dSWill Deacon 1049fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1050fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1051fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1052fe4b991dSWill Deacon selftest_running = false; \ 1053fe4b991dSWill Deacon -EFAULT; \ 1054fe4b991dSWill Deacon }) 1055fe4b991dSWill Deacon 1056fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1057fe4b991dSWill Deacon { 1058fe4b991dSWill Deacon static const enum io_pgtable_fmt fmts[] = { 1059fe4b991dSWill Deacon ARM_64_LPAE_S1, 1060fe4b991dSWill Deacon ARM_64_LPAE_S2, 1061fe4b991dSWill Deacon }; 1062fe4b991dSWill Deacon 1063fe4b991dSWill Deacon int i, j; 1064fe4b991dSWill Deacon unsigned long iova; 1065fe4b991dSWill Deacon size_t size; 1066fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1067fe4b991dSWill Deacon 1068fe4b991dSWill Deacon selftest_running = true; 1069fe4b991dSWill Deacon 1070fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1071fe4b991dSWill Deacon cfg_cookie = cfg; 1072fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1073fe4b991dSWill Deacon if (!ops) { 1074fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1075fe4b991dSWill Deacon return -ENOMEM; 1076fe4b991dSWill Deacon } 1077fe4b991dSWill Deacon 1078fe4b991dSWill Deacon /* 1079fe4b991dSWill Deacon * Initial sanity checks. 1080fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1081fe4b991dSWill Deacon */ 1082fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1083fe4b991dSWill Deacon return __FAIL(ops, i); 1084fe4b991dSWill Deacon 1085fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1086fe4b991dSWill Deacon return __FAIL(ops, i); 1087fe4b991dSWill Deacon 1088fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1089fe4b991dSWill Deacon return __FAIL(ops, i); 1090fe4b991dSWill Deacon 1091fe4b991dSWill Deacon /* 1092fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1093fe4b991dSWill Deacon */ 1094fe4b991dSWill Deacon iova = 0; 10954ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1096fe4b991dSWill Deacon size = 1UL << j; 1097fe4b991dSWill Deacon 1098fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1099fe4b991dSWill Deacon IOMMU_WRITE | 1100fe4b991dSWill Deacon IOMMU_NOEXEC | 1101fe4b991dSWill Deacon IOMMU_CACHE)) 1102fe4b991dSWill Deacon return __FAIL(ops, i); 1103fe4b991dSWill Deacon 1104fe4b991dSWill Deacon /* Overlapping mappings */ 1105fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1106fe4b991dSWill Deacon IOMMU_READ | IOMMU_NOEXEC)) 1107fe4b991dSWill Deacon return __FAIL(ops, i); 1108fe4b991dSWill Deacon 1109fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1110fe4b991dSWill Deacon return __FAIL(ops, i); 1111fe4b991dSWill Deacon 1112fe4b991dSWill Deacon iova += SZ_1G; 1113fe4b991dSWill Deacon } 1114fe4b991dSWill Deacon 1115fe4b991dSWill Deacon /* Partial unmap */ 1116fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1117fe4b991dSWill Deacon if (ops->unmap(ops, SZ_1G + size, size) != size) 1118fe4b991dSWill Deacon return __FAIL(ops, i); 1119fe4b991dSWill Deacon 1120fe4b991dSWill Deacon /* Remap of partial unmap */ 1121fe4b991dSWill Deacon if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 1122fe4b991dSWill Deacon return __FAIL(ops, i); 1123fe4b991dSWill Deacon 1124fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1125fe4b991dSWill Deacon return __FAIL(ops, i); 1126fe4b991dSWill Deacon 1127fe4b991dSWill Deacon /* Full unmap */ 1128fe4b991dSWill Deacon iova = 0; 1129f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1130fe4b991dSWill Deacon size = 1UL << j; 1131fe4b991dSWill Deacon 1132fe4b991dSWill Deacon if (ops->unmap(ops, iova, size) != size) 1133fe4b991dSWill Deacon return __FAIL(ops, i); 1134fe4b991dSWill Deacon 1135fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1136fe4b991dSWill Deacon return __FAIL(ops, i); 1137fe4b991dSWill Deacon 1138fe4b991dSWill Deacon /* Remap full block */ 1139fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 1140fe4b991dSWill Deacon return __FAIL(ops, i); 1141fe4b991dSWill Deacon 1142fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1143fe4b991dSWill Deacon return __FAIL(ops, i); 1144fe4b991dSWill Deacon 1145fe4b991dSWill Deacon iova += SZ_1G; 1146fe4b991dSWill Deacon } 1147fe4b991dSWill Deacon 1148fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1149fe4b991dSWill Deacon } 1150fe4b991dSWill Deacon 1151fe4b991dSWill Deacon selftest_running = false; 1152fe4b991dSWill Deacon return 0; 1153fe4b991dSWill Deacon } 1154fe4b991dSWill Deacon 1155fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1156fe4b991dSWill Deacon { 1157fe4b991dSWill Deacon static const unsigned long pgsize[] = { 1158fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1159fe4b991dSWill Deacon SZ_16K | SZ_32M, 1160fe4b991dSWill Deacon SZ_64K | SZ_512M, 1161fe4b991dSWill Deacon }; 1162fe4b991dSWill Deacon 1163fe4b991dSWill Deacon static const unsigned int ias[] = { 1164fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1165fe4b991dSWill Deacon }; 1166fe4b991dSWill Deacon 1167fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1168fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1169fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1170fe4b991dSWill Deacon .oas = 48, 117181b3c252SRobin Murphy .quirks = IO_PGTABLE_QUIRK_NO_DMA, 1172fe4b991dSWill Deacon }; 1173fe4b991dSWill Deacon 1174fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1175fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1176fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1177fe4b991dSWill Deacon cfg.ias = ias[j]; 1178fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1179fe4b991dSWill Deacon pgsize[i], ias[j]); 1180fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1181fe4b991dSWill Deacon fail++; 1182fe4b991dSWill Deacon else 1183fe4b991dSWill Deacon pass++; 1184fe4b991dSWill Deacon } 1185fe4b991dSWill Deacon } 1186fe4b991dSWill Deacon 1187fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1188fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1189fe4b991dSWill Deacon } 1190fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1191fe4b991dSWill Deacon #endif 1192