1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e1d3c0fdSWill Deacon /* 3e1d3c0fdSWill Deacon * CPU-agnostic ARM page table allocator. 4e1d3c0fdSWill Deacon * 5e1d3c0fdSWill Deacon * Copyright (C) 2014 ARM Limited 6e1d3c0fdSWill Deacon * 7e1d3c0fdSWill Deacon * Author: Will Deacon <will.deacon@arm.com> 8e1d3c0fdSWill Deacon */ 9e1d3c0fdSWill Deacon 10e1d3c0fdSWill Deacon #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt 11e1d3c0fdSWill Deacon 122c3d273eSRobin Murphy #include <linux/atomic.h> 136c89928fSRobin Murphy #include <linux/bitops.h> 14b77cf11fSRob Herring #include <linux/io-pgtable.h> 15e1d3c0fdSWill Deacon #include <linux/kernel.h> 16e1d3c0fdSWill Deacon #include <linux/sizes.h> 17e1d3c0fdSWill Deacon #include <linux/slab.h> 18e1d3c0fdSWill Deacon #include <linux/types.h> 198f6aff98SLada Trimasova #include <linux/dma-mapping.h> 20e1d3c0fdSWill Deacon 2187a91b15SRobin Murphy #include <asm/barrier.h> 2287a91b15SRobin Murphy 236c89928fSRobin Murphy #define ARM_LPAE_MAX_ADDR_BITS 52 24e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 25e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS 4 26e1d3c0fdSWill Deacon 27e1d3c0fdSWill Deacon /* Struct accessors */ 28e1d3c0fdSWill Deacon #define io_pgtable_to_data(x) \ 29e1d3c0fdSWill Deacon container_of((x), struct arm_lpae_io_pgtable, iop) 30e1d3c0fdSWill Deacon 31e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x) \ 32e1d3c0fdSWill Deacon io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 33e1d3c0fdSWill Deacon 34e1d3c0fdSWill Deacon /* 35e1d3c0fdSWill Deacon * Calculate the right shift amount to get to the portion describing level l 36e1d3c0fdSWill Deacon * in a virtual address mapped by the pagetable in d. 37e1d3c0fdSWill Deacon */ 38e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d) \ 395fb190b0SRobin Murphy (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \ 405fb190b0SRobin Murphy ilog2(sizeof(arm_lpae_iopte))) 41e1d3c0fdSWill Deacon 425fb190b0SRobin Murphy #define ARM_LPAE_GRANULE(d) \ 435fb190b0SRobin Murphy (sizeof(arm_lpae_iopte) << (d)->bits_per_level) 44c79278c1SRobin Murphy #define ARM_LPAE_PGD_SIZE(d) \ 45c79278c1SRobin Murphy (sizeof(arm_lpae_iopte) << (d)->pgd_bits) 46e1d3c0fdSWill Deacon 47e1d3c0fdSWill Deacon /* 48e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 49e1d3c0fdSWill Deacon * pagetable in d. 50e1d3c0fdSWill Deacon */ 51e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 52c79278c1SRobin Murphy ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0) 53e1d3c0fdSWill Deacon 54e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 55367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 56e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 57e1d3c0fdSWill Deacon 58e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 595fb190b0SRobin Murphy #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d)) 60e1d3c0fdSWill Deacon 61e1d3c0fdSWill Deacon /* Page table bits */ 62e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 63e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 64e1d3c0fdSWill Deacon 65e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 66e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 67e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 68e1d3c0fdSWill Deacon 696c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) 706c89928fSRobin Murphy 71c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 72e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 73e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 74e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 75e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 76e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 77c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 78e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 79e1d3c0fdSWill Deacon 80e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 81e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 82e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 84e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 852c3d273eSRobin Murphy /* Software bit for solving coherency races */ 862c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) 87e1d3c0fdSWill Deacon 88e1d3c0fdSWill Deacon /* Stage-1 PTE */ 89e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 90e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 91e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 93e1d3c0fdSWill Deacon 94e1d3c0fdSWill Deacon /* Stage-2 PTE */ 95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 98e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 99e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 101e1d3c0fdSWill Deacon 102e1d3c0fdSWill Deacon /* Register bits */ 103fb485eb1SRobin Murphy #define ARM_LPAE_TCR_TG0_4K 0 104fb485eb1SRobin Murphy #define ARM_LPAE_TCR_TG0_64K 1 105fb485eb1SRobin Murphy #define ARM_LPAE_TCR_TG0_16K 2 106e1d3c0fdSWill Deacon 107e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS 0 108e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS 2 109e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS 3 110e1d3c0fdSWill Deacon 111e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC 0 112e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA 1 113e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT 2 114e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB 3 115e1d3c0fdSWill Deacon 116fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_SL0_MASK 0x3 117e1d3c0fdSWill Deacon 118e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 119e1d3c0fdSWill Deacon 120fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_SHIFT 16 121fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_MASK 0x7 122e1d3c0fdSWill Deacon 123e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 124e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 125e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 126e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 127e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 1296c89928fSRobin Murphy #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL 130e1d3c0fdSWill Deacon 131e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 132e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 133e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 134e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 13590ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 136e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 137e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 138e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 139e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 14090ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 141e1d3c0fdSWill Deacon 142d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) 143d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) 144d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) 145d08d42deSRob Herring 14652f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL 14752f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL 14852f325f4SRobin Murphy 149e1d3c0fdSWill Deacon /* IOPTE accessors */ 1506c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 151e1d3c0fdSWill Deacon 152e1d3c0fdSWill Deacon #define iopte_type(pte,l) \ 153e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 154e1d3c0fdSWill Deacon 155e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 156e1d3c0fdSWill Deacon 157e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 158e1d3c0fdSWill Deacon struct io_pgtable iop; 159e1d3c0fdSWill Deacon 160c79278c1SRobin Murphy int pgd_bits; 161594ab90fSRobin Murphy int start_level; 1625fb190b0SRobin Murphy int bits_per_level; 163e1d3c0fdSWill Deacon 164e1d3c0fdSWill Deacon void *pgd; 165e1d3c0fdSWill Deacon }; 166e1d3c0fdSWill Deacon 167e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 168e1d3c0fdSWill Deacon 169d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, 170d08d42deSRob Herring enum io_pgtable_fmt fmt) 171d08d42deSRob Herring { 172d08d42deSRob Herring if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) 173d08d42deSRob Herring return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE; 174d08d42deSRob Herring 175d08d42deSRob Herring return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK; 176d08d42deSRob Herring } 177d08d42deSRob Herring 1786c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, 1796c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1806c89928fSRobin Murphy { 1816c89928fSRobin Murphy arm_lpae_iopte pte = paddr; 1826c89928fSRobin Murphy 1836c89928fSRobin Murphy /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ 1846c89928fSRobin Murphy return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; 1856c89928fSRobin Murphy } 1866c89928fSRobin Murphy 1876c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, 1886c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1896c89928fSRobin Murphy { 19078688059SRobin Murphy u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; 1916c89928fSRobin Murphy 1925fb190b0SRobin Murphy if (ARM_LPAE_GRANULE(data) < SZ_64K) 1936c89928fSRobin Murphy return paddr; 1946c89928fSRobin Murphy 1956c89928fSRobin Murphy /* Rotate the packed high-order bits back to the top */ 1966c89928fSRobin Murphy return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); 1976c89928fSRobin Murphy } 1986c89928fSRobin Murphy 199fe4b991dSWill Deacon static bool selftest_running = false; 200fe4b991dSWill Deacon 201ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 202f8d54961SRobin Murphy { 203ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 204f8d54961SRobin Murphy } 205f8d54961SRobin Murphy 206f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 207f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 208f8d54961SRobin Murphy { 209f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 2104b123757SRobin Murphy int order = get_order(size); 2114b123757SRobin Murphy struct page *p; 212f8d54961SRobin Murphy dma_addr_t dma; 2134b123757SRobin Murphy void *pages; 214f8d54961SRobin Murphy 2154b123757SRobin Murphy VM_BUG_ON((gfp & __GFP_HIGHMEM)); 216fac83d29SJean-Philippe Brucker p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 217fac83d29SJean-Philippe Brucker gfp | __GFP_ZERO, order); 2184b123757SRobin Murphy if (!p) 219f8d54961SRobin Murphy return NULL; 220f8d54961SRobin Murphy 2214b123757SRobin Murphy pages = page_address(p); 2224f41845bSWill Deacon if (!cfg->coherent_walk) { 223f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 224f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 225f8d54961SRobin Murphy goto out_free; 226f8d54961SRobin Murphy /* 227f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 228ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 229ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 230f8d54961SRobin Murphy */ 231ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 232f8d54961SRobin Murphy goto out_unmap; 233f8d54961SRobin Murphy } 234f8d54961SRobin Murphy 235f8d54961SRobin Murphy return pages; 236f8d54961SRobin Murphy 237f8d54961SRobin Murphy out_unmap: 238f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 239f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 240f8d54961SRobin Murphy out_free: 2414b123757SRobin Murphy __free_pages(p, order); 242f8d54961SRobin Murphy return NULL; 243f8d54961SRobin Murphy } 244f8d54961SRobin Murphy 245f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 246f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 247f8d54961SRobin Murphy { 2484f41845bSWill Deacon if (!cfg->coherent_walk) 249ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 250f8d54961SRobin Murphy size, DMA_TO_DEVICE); 2514b123757SRobin Murphy free_pages((unsigned long)pages, get_order(size)); 252f8d54961SRobin Murphy } 253f8d54961SRobin Murphy 2542c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 2552c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2562c3d273eSRobin Murphy { 2572c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 2582c3d273eSRobin Murphy sizeof(*ptep), DMA_TO_DEVICE); 2592c3d273eSRobin Murphy } 2602c3d273eSRobin Murphy 261f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 26287a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 263f8d54961SRobin Murphy { 264f8d54961SRobin Murphy *ptep = pte; 265f8d54961SRobin Murphy 2664f41845bSWill Deacon if (!cfg->coherent_walk) 2672c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 268f8d54961SRobin Murphy } 269f8d54961SRobin Murphy 270193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 2713951c41aSWill Deacon struct iommu_iotlb_gather *gather, 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 281d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) 282fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 283fb3a9579SRobin Murphy else 284fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 285fb3a9579SRobin Murphy 2866c89928fSRobin Murphy pte |= paddr_to_iopte(paddr, data); 287fb3a9579SRobin Murphy 288fb3a9579SRobin Murphy __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); 289fb3a9579SRobin Murphy } 290fb3a9579SRobin Murphy 291e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 292e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 293e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 294e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 295e1d3c0fdSWill Deacon { 296fb3a9579SRobin Murphy arm_lpae_iopte pte = *ptep; 297e1d3c0fdSWill Deacon 298d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) { 299cf27ec93SWill Deacon /* We require an unmap first */ 300fe4b991dSWill Deacon WARN_ON(!selftest_running); 301e1d3c0fdSWill Deacon return -EEXIST; 302fb3a9579SRobin Murphy } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 303cf27ec93SWill Deacon /* 304cf27ec93SWill Deacon * We need to unmap and free the old table before 305cf27ec93SWill Deacon * overwriting it with a block entry. 306cf27ec93SWill Deacon */ 307cf27ec93SWill Deacon arm_lpae_iopte *tblp; 308cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 309cf27ec93SWill Deacon 310cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 3113951c41aSWill Deacon if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) { 3123951c41aSWill Deacon WARN_ON(1); 313cf27ec93SWill Deacon return -EINVAL; 314fe4b991dSWill Deacon } 3153951c41aSWill Deacon } 316e1d3c0fdSWill Deacon 317fb3a9579SRobin Murphy __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); 318e1d3c0fdSWill Deacon return 0; 319e1d3c0fdSWill Deacon } 320e1d3c0fdSWill Deacon 321fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 322fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3232c3d273eSRobin Murphy arm_lpae_iopte curr, 324fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 325fb3a9579SRobin Murphy { 3262c3d273eSRobin Murphy arm_lpae_iopte old, new; 327fb3a9579SRobin Murphy 328fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 329fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 330fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 331fb3a9579SRobin Murphy 33277f34458SWill Deacon /* 33377f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 33477f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 33577f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 33677f34458SWill Deacon */ 33777f34458SWill Deacon dma_wmb(); 3382c3d273eSRobin Murphy 3392c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3402c3d273eSRobin Murphy 3414f41845bSWill Deacon if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) 3422c3d273eSRobin Murphy return old; 3432c3d273eSRobin Murphy 3442c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 3452c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3462c3d273eSRobin Murphy if (old == curr) 3472c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3482c3d273eSRobin Murphy 3492c3d273eSRobin Murphy return old; 350fb3a9579SRobin Murphy } 351fb3a9579SRobin Murphy 352e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 353e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 354e1d3c0fdSWill Deacon int lvl, arm_lpae_iopte *ptep) 355e1d3c0fdSWill Deacon { 356e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 357e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3582c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 359f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 360e1d3c0fdSWill Deacon 361e1d3c0fdSWill Deacon /* Find our entry at the current level */ 362e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 363e1d3c0fdSWill Deacon 364e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 365f7b90d2cSRobin Murphy if (size == block_size) 366e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 367e1d3c0fdSWill Deacon 368e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 369e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 370e1d3c0fdSWill Deacon return -EINVAL; 371e1d3c0fdSWill Deacon 372e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3732c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 374e1d3c0fdSWill Deacon if (!pte) { 3752c3d273eSRobin Murphy cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); 376e1d3c0fdSWill Deacon if (!cptep) 377e1d3c0fdSWill Deacon return -ENOMEM; 378e1d3c0fdSWill Deacon 3792c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 3802c3d273eSRobin Murphy if (pte) 3812c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 3824f41845bSWill Deacon } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 3832c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3842c3d273eSRobin Murphy } 3852c3d273eSRobin Murphy 386d08d42deSRob Herring if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { 387e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 3882c3d273eSRobin Murphy } else if (pte) { 389ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 390ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 391ed46e66cSOleksandr Tyshchenko return -EEXIST; 392e1d3c0fdSWill Deacon } 393e1d3c0fdSWill Deacon 394e1d3c0fdSWill Deacon /* Rinse, repeat */ 395e1d3c0fdSWill Deacon return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 396e1d3c0fdSWill Deacon } 397e1d3c0fdSWill Deacon 398e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 399e1d3c0fdSWill Deacon int prot) 400e1d3c0fdSWill Deacon { 401e1d3c0fdSWill Deacon arm_lpae_iopte pte; 402e1d3c0fdSWill Deacon 403e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 404e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 405e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 406e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 407e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 408e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 409e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 410e1d3c0fdSWill Deacon } else { 411e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 412e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 413e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 414e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 415e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 416d08d42deSRob Herring } 417d08d42deSRob Herring 418d08d42deSRob Herring /* 419d08d42deSRob Herring * Note that this logic is structured to accommodate Mali LPAE 420d08d42deSRob Herring * having stage-1-like attributes but stage-2-like permissions. 421d08d42deSRob Herring */ 422d08d42deSRob Herring if (data->iop.fmt == ARM_64_LPAE_S2 || 423d08d42deSRob Herring data->iop.fmt == ARM_32_LPAE_S2) { 424fb948251SRobin Murphy if (prot & IOMMU_MMIO) 425fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 426fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 427e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 428e1d3c0fdSWill Deacon else 429e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 430d08d42deSRob Herring } else { 431d08d42deSRob Herring if (prot & IOMMU_MMIO) 432d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 433d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 434d08d42deSRob Herring else if (prot & IOMMU_CACHE) 435d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 436d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 437dd5ddd3cSWill Deacon else if (prot & IOMMU_SYS_CACHE_ONLY) 43890ec7a76SVivek Gautam pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 43990ec7a76SVivek Gautam << ARM_LPAE_PTE_ATTRINDX_SHIFT); 440e1d3c0fdSWill Deacon } 441e1d3c0fdSWill Deacon 4427618e479SRobin Murphy if (prot & IOMMU_CACHE) 4437618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_IS; 4447618e479SRobin Murphy else 4457618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_OS; 4467618e479SRobin Murphy 447e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 448e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 449e1d3c0fdSWill Deacon 4507618e479SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 4517618e479SRobin Murphy pte |= ARM_LPAE_PTE_NS; 4527618e479SRobin Murphy 4537618e479SRobin Murphy if (data->iop.fmt != ARM_MALI_LPAE) 4547618e479SRobin Murphy pte |= ARM_LPAE_PTE_AF; 4557618e479SRobin Murphy 456e1d3c0fdSWill Deacon return pte; 457e1d3c0fdSWill Deacon } 458e1d3c0fdSWill Deacon 459e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 460e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, int iommu_prot) 461e1d3c0fdSWill Deacon { 462e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 463f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 464e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 465594ab90fSRobin Murphy int ret, lvl = data->start_level; 466e1d3c0fdSWill Deacon arm_lpae_iopte prot; 467e1d3c0fdSWill Deacon 468e1d3c0fdSWill Deacon /* If no access, then nothing to do */ 469e1d3c0fdSWill Deacon if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 470e1d3c0fdSWill Deacon return 0; 471e1d3c0fdSWill Deacon 472f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 473f7b90d2cSRobin Murphy return -EINVAL; 474f7b90d2cSRobin Murphy 47567f3e53dSRobin Murphy if (WARN_ON(iova >> data->iop.cfg.ias || paddr >> data->iop.cfg.oas)) 47676557391SRobin Murphy return -ERANGE; 47776557391SRobin Murphy 478e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 47987a91b15SRobin Murphy ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 48087a91b15SRobin Murphy /* 48187a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 48287a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 48387a91b15SRobin Murphy */ 48487a91b15SRobin Murphy wmb(); 48587a91b15SRobin Murphy 48687a91b15SRobin Murphy return ret; 487e1d3c0fdSWill Deacon } 488e1d3c0fdSWill Deacon 489e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 490e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 491e1d3c0fdSWill Deacon { 492e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 493e1d3c0fdSWill Deacon unsigned long table_size; 494e1d3c0fdSWill Deacon 495594ab90fSRobin Murphy if (lvl == data->start_level) 496c79278c1SRobin Murphy table_size = ARM_LPAE_PGD_SIZE(data); 497e1d3c0fdSWill Deacon else 49806c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 499e1d3c0fdSWill Deacon 500e1d3c0fdSWill Deacon start = ptep; 50112c2ab09SWill Deacon 50212c2ab09SWill Deacon /* Only leaf entries at the last level */ 50312c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 50412c2ab09SWill Deacon end = ptep; 50512c2ab09SWill Deacon else 506e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 507e1d3c0fdSWill Deacon 508e1d3c0fdSWill Deacon while (ptep != end) { 509e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 510e1d3c0fdSWill Deacon 511d08d42deSRob Herring if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) 512e1d3c0fdSWill Deacon continue; 513e1d3c0fdSWill Deacon 514e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 515e1d3c0fdSWill Deacon } 516e1d3c0fdSWill Deacon 517f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 518e1d3c0fdSWill Deacon } 519e1d3c0fdSWill Deacon 520e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 521e1d3c0fdSWill Deacon { 522e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 523e1d3c0fdSWill Deacon 524594ab90fSRobin Murphy __arm_lpae_free_pgtable(data, data->start_level, data->pgd); 525e1d3c0fdSWill Deacon kfree(data); 526e1d3c0fdSWill Deacon } 527e1d3c0fdSWill Deacon 528193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 5293951c41aSWill Deacon struct iommu_iotlb_gather *gather, 530e1d3c0fdSWill Deacon unsigned long iova, size_t size, 531fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 532fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 533e1d3c0fdSWill Deacon { 534fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 535fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 536e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 537fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 538fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 539fb3a9579SRobin Murphy int i, unmap_idx = -1; 540e1d3c0fdSWill Deacon 541fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 542fb3a9579SRobin Murphy return 0; 543e1d3c0fdSWill Deacon 544fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 545fb3a9579SRobin Murphy if (!tablep) 546fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 547e1d3c0fdSWill Deacon 548fb3a9579SRobin Murphy if (size == split_sz) 549fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 550fb3a9579SRobin Murphy 5516c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 552fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 553fb3a9579SRobin Murphy 554fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 555e1d3c0fdSWill Deacon /* Unmap! */ 556fb3a9579SRobin Murphy if (i == unmap_idx) 557e1d3c0fdSWill Deacon continue; 558e1d3c0fdSWill Deacon 559fb3a9579SRobin Murphy __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); 560e1d3c0fdSWill Deacon } 561e1d3c0fdSWill Deacon 5622c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5632c3d273eSRobin Murphy if (pte != blk_pte) { 5642c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5652c3d273eSRobin Murphy /* 5662c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5672c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5682c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5692c3d273eSRobin Murphy */ 5702c3d273eSRobin Murphy if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) 5712c3d273eSRobin Murphy return 0; 5722c3d273eSRobin Murphy 5732c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 57485c7a0f1SRobin Murphy } else if (unmap_idx >= 0) { 5753951c41aSWill Deacon io_pgtable_tlb_add_page(&data->iop, gather, iova, size); 576e1d3c0fdSWill Deacon return size; 577e1d3c0fdSWill Deacon } 578e1d3c0fdSWill Deacon 5793951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep); 58085c7a0f1SRobin Murphy } 58185c7a0f1SRobin Murphy 582193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 5833951c41aSWill Deacon struct iommu_iotlb_gather *gather, 584e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 585e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 586e1d3c0fdSWill Deacon { 587e1d3c0fdSWill Deacon arm_lpae_iopte pte; 588507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 589e1d3c0fdSWill Deacon 5902eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5912eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5922eb97c78SRobin Murphy return 0; 5932eb97c78SRobin Murphy 594e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 5952c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 5962eb97c78SRobin Murphy if (WARN_ON(!pte)) 597e1d3c0fdSWill Deacon return 0; 598e1d3c0fdSWill Deacon 599e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 600fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 601507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 602e1d3c0fdSWill Deacon 603d08d42deSRob Herring if (!iopte_leaf(pte, lvl, iop->fmt)) { 604e1d3c0fdSWill Deacon /* Also flush any partial walks */ 60510b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(iop, iova, size, 60610b7a7d9SWill Deacon ARM_LPAE_GRANULE(data)); 607e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 608e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 609b6b65ca2SZhen Lei } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { 610b6b65ca2SZhen Lei /* 611b6b65ca2SZhen Lei * Order the PTE update against queueing the IOVA, to 612b6b65ca2SZhen Lei * guarantee that a flush callback from a different CPU 613b6b65ca2SZhen Lei * has observed it before the TLBIALL can be issued. 614b6b65ca2SZhen Lei */ 615b6b65ca2SZhen Lei smp_wmb(); 616e1d3c0fdSWill Deacon } else { 6173951c41aSWill Deacon io_pgtable_tlb_add_page(iop, gather, iova, size); 618e1d3c0fdSWill Deacon } 619e1d3c0fdSWill Deacon 620e1d3c0fdSWill Deacon return size; 621d08d42deSRob Herring } else if (iopte_leaf(pte, lvl, iop->fmt)) { 622e1d3c0fdSWill Deacon /* 623e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 624e1d3c0fdSWill Deacon * minus the part we want to unmap 625e1d3c0fdSWill Deacon */ 6263951c41aSWill Deacon return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, 627fb3a9579SRobin Murphy lvl + 1, ptep); 628e1d3c0fdSWill Deacon } 629e1d3c0fdSWill Deacon 630e1d3c0fdSWill Deacon /* Keep on walkin' */ 631e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 6323951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep); 633e1d3c0fdSWill Deacon } 634e1d3c0fdSWill Deacon 635193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 636a2d3a382SWill Deacon size_t size, struct iommu_iotlb_gather *gather) 637e1d3c0fdSWill Deacon { 638e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 639f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 640e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 641e1d3c0fdSWill Deacon 642f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 643f7b90d2cSRobin Murphy return 0; 644f7b90d2cSRobin Murphy 64567f3e53dSRobin Murphy if (WARN_ON(iova >> data->iop.cfg.ias)) 64676557391SRobin Murphy return 0; 64776557391SRobin Murphy 648594ab90fSRobin Murphy return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep); 649e1d3c0fdSWill Deacon } 650e1d3c0fdSWill Deacon 651e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 652e1d3c0fdSWill Deacon unsigned long iova) 653e1d3c0fdSWill Deacon { 654e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 655e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 656594ab90fSRobin Murphy int lvl = data->start_level; 657e1d3c0fdSWill Deacon 658e1d3c0fdSWill Deacon do { 659e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 660e1d3c0fdSWill Deacon if (!ptep) 661e1d3c0fdSWill Deacon return 0; 662e1d3c0fdSWill Deacon 663e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6642c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6652c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 666e1d3c0fdSWill Deacon 667e1d3c0fdSWill Deacon /* Valid entry? */ 668e1d3c0fdSWill Deacon if (!pte) 669e1d3c0fdSWill Deacon return 0; 670e1d3c0fdSWill Deacon 671e1d3c0fdSWill Deacon /* Leaf entry? */ 672d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) 673e1d3c0fdSWill Deacon goto found_translation; 674e1d3c0fdSWill Deacon 675e1d3c0fdSWill Deacon /* Take it to the next level */ 676e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 677e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 678e1d3c0fdSWill Deacon 679e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 680e1d3c0fdSWill Deacon return 0; 681e1d3c0fdSWill Deacon 682e1d3c0fdSWill Deacon found_translation: 6837c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 6846c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 685e1d3c0fdSWill Deacon } 686e1d3c0fdSWill Deacon 687e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 688e1d3c0fdSWill Deacon { 6896c89928fSRobin Murphy unsigned long granule, page_sizes; 6906c89928fSRobin Murphy unsigned int max_addr_bits = 48; 691e1d3c0fdSWill Deacon 692e1d3c0fdSWill Deacon /* 693e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 694e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 695e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 696e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 697e1d3c0fdSWill Deacon * chosen granule. 698e1d3c0fdSWill Deacon */ 699e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 700e1d3c0fdSWill Deacon granule = PAGE_SIZE; 701e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 702e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 703e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 704e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 705e1d3c0fdSWill Deacon else 706e1d3c0fdSWill Deacon granule = 0; 707e1d3c0fdSWill Deacon 708e1d3c0fdSWill Deacon switch (granule) { 709e1d3c0fdSWill Deacon case SZ_4K: 7106c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 711e1d3c0fdSWill Deacon break; 712e1d3c0fdSWill Deacon case SZ_16K: 7136c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 714e1d3c0fdSWill Deacon break; 715e1d3c0fdSWill Deacon case SZ_64K: 7166c89928fSRobin Murphy max_addr_bits = 52; 7176c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7186c89928fSRobin Murphy if (cfg->oas > 48) 7196c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 720e1d3c0fdSWill Deacon break; 721e1d3c0fdSWill Deacon default: 7226c89928fSRobin Murphy page_sizes = 0; 723e1d3c0fdSWill Deacon } 7246c89928fSRobin Murphy 7256c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7266c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7276c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 728e1d3c0fdSWill Deacon } 729e1d3c0fdSWill Deacon 730e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 731e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 732e1d3c0fdSWill Deacon { 733e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 7345fb190b0SRobin Murphy int levels, va_bits, pg_shift; 735e1d3c0fdSWill Deacon 736e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 737e1d3c0fdSWill Deacon 738e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 739e1d3c0fdSWill Deacon return NULL; 740e1d3c0fdSWill Deacon 741e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 742e1d3c0fdSWill Deacon return NULL; 743e1d3c0fdSWill Deacon 744e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 745e1d3c0fdSWill Deacon return NULL; 746e1d3c0fdSWill Deacon 747ffcb6d16SRobin Murphy if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { 748ffcb6d16SRobin Murphy dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); 749ffcb6d16SRobin Murphy return NULL; 750ffcb6d16SRobin Murphy } 751ffcb6d16SRobin Murphy 752e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 753e1d3c0fdSWill Deacon if (!data) 754e1d3c0fdSWill Deacon return NULL; 755e1d3c0fdSWill Deacon 7565fb190b0SRobin Murphy pg_shift = __ffs(cfg->pgsize_bitmap); 7575fb190b0SRobin Murphy data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); 758e1d3c0fdSWill Deacon 7595fb190b0SRobin Murphy va_bits = cfg->ias - pg_shift; 760594ab90fSRobin Murphy levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 761594ab90fSRobin Murphy data->start_level = ARM_LPAE_MAX_LEVELS - levels; 762e1d3c0fdSWill Deacon 763e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 764c79278c1SRobin Murphy data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); 765e1d3c0fdSWill Deacon 766e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 767e1d3c0fdSWill Deacon .map = arm_lpae_map, 768e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 769e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 770e1d3c0fdSWill Deacon }; 771e1d3c0fdSWill Deacon 772e1d3c0fdSWill Deacon return data; 773e1d3c0fdSWill Deacon } 774e1d3c0fdSWill Deacon 775e1d3c0fdSWill Deacon static struct io_pgtable * 776e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 777e1d3c0fdSWill Deacon { 778e1d3c0fdSWill Deacon u64 reg; 7793850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 780fb485eb1SRobin Murphy typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; 781e1d3c0fdSWill Deacon 7824f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | 783b6b65ca2SZhen Lei IO_PGTABLE_QUIRK_NON_STRICT)) 7843850db49SRobin Murphy return NULL; 7853850db49SRobin Murphy 7863850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 787e1d3c0fdSWill Deacon if (!data) 788e1d3c0fdSWill Deacon return NULL; 789e1d3c0fdSWill Deacon 790e1d3c0fdSWill Deacon /* TCR */ 7919e6ea59fSBjorn Andersson if (cfg->coherent_walk) { 792fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_IS; 793fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 794fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 7959e6ea59fSBjorn Andersson } else { 796fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_OS; 797fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_NC; 798fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_NC; 7999e6ea59fSBjorn Andersson } 800e1d3c0fdSWill Deacon 80106c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 802e1d3c0fdSWill Deacon case SZ_4K: 803fb485eb1SRobin Murphy tcr->tg = ARM_LPAE_TCR_TG0_4K; 804e1d3c0fdSWill Deacon break; 805e1d3c0fdSWill Deacon case SZ_16K: 806fb485eb1SRobin Murphy tcr->tg = ARM_LPAE_TCR_TG0_16K; 807e1d3c0fdSWill Deacon break; 808e1d3c0fdSWill Deacon case SZ_64K: 809fb485eb1SRobin Murphy tcr->tg = ARM_LPAE_TCR_TG0_64K; 810e1d3c0fdSWill Deacon break; 811e1d3c0fdSWill Deacon } 812e1d3c0fdSWill Deacon 813e1d3c0fdSWill Deacon switch (cfg->oas) { 814e1d3c0fdSWill Deacon case 32: 815fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_32_BIT; 816e1d3c0fdSWill Deacon break; 817e1d3c0fdSWill Deacon case 36: 818fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_36_BIT; 819e1d3c0fdSWill Deacon break; 820e1d3c0fdSWill Deacon case 40: 821fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_40_BIT; 822e1d3c0fdSWill Deacon break; 823e1d3c0fdSWill Deacon case 42: 824fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_42_BIT; 825e1d3c0fdSWill Deacon break; 826e1d3c0fdSWill Deacon case 44: 827fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_44_BIT; 828e1d3c0fdSWill Deacon break; 829e1d3c0fdSWill Deacon case 48: 830fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_48_BIT; 831e1d3c0fdSWill Deacon break; 8326c89928fSRobin Murphy case 52: 833fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_52_BIT; 8346c89928fSRobin Murphy break; 835e1d3c0fdSWill Deacon default: 836e1d3c0fdSWill Deacon goto out_free_data; 837e1d3c0fdSWill Deacon } 838e1d3c0fdSWill Deacon 839fb485eb1SRobin Murphy tcr->tsz = 64ULL - cfg->ias; 840e1d3c0fdSWill Deacon 841e1d3c0fdSWill Deacon /* MAIRs */ 842e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 843e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 844e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 845e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 846e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 84790ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | 84890ec7a76SVivek Gautam (ARM_LPAE_MAIR_ATTR_INC_OWBRWA 84990ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); 850e1d3c0fdSWill Deacon 851205577abSRobin Murphy cfg->arm_lpae_s1_cfg.mair = reg; 852e1d3c0fdSWill Deacon 853e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 854c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 855c79278c1SRobin Murphy GFP_KERNEL, cfg); 856e1d3c0fdSWill Deacon if (!data->pgd) 857e1d3c0fdSWill Deacon goto out_free_data; 858e1d3c0fdSWill Deacon 85987a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 86087a91b15SRobin Murphy wmb(); 861e1d3c0fdSWill Deacon 862d1e5f26fSRobin Murphy /* TTBR */ 863d1e5f26fSRobin Murphy cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); 864e1d3c0fdSWill Deacon return &data->iop; 865e1d3c0fdSWill Deacon 866e1d3c0fdSWill Deacon out_free_data: 867e1d3c0fdSWill Deacon kfree(data); 868e1d3c0fdSWill Deacon return NULL; 869e1d3c0fdSWill Deacon } 870e1d3c0fdSWill Deacon 871e1d3c0fdSWill Deacon static struct io_pgtable * 872e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 873e1d3c0fdSWill Deacon { 874*ac4b80e5SWill Deacon u64 sl; 8753850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 876*ac4b80e5SWill Deacon typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; 877e1d3c0fdSWill Deacon 8783850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 8794f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) 8803850db49SRobin Murphy return NULL; 8813850db49SRobin Murphy 8823850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 883e1d3c0fdSWill Deacon if (!data) 884e1d3c0fdSWill Deacon return NULL; 885e1d3c0fdSWill Deacon 886e1d3c0fdSWill Deacon /* 887e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 888e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 889e1d3c0fdSWill Deacon */ 890594ab90fSRobin Murphy if (data->start_level == 0) { 891e1d3c0fdSWill Deacon unsigned long pgd_pages; 892e1d3c0fdSWill Deacon 893c79278c1SRobin Murphy pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); 894e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 895c79278c1SRobin Murphy data->pgd_bits += data->bits_per_level; 896594ab90fSRobin Murphy data->start_level++; 897e1d3c0fdSWill Deacon } 898e1d3c0fdSWill Deacon } 899e1d3c0fdSWill Deacon 900e1d3c0fdSWill Deacon /* VTCR */ 90130d2acb6SWill Deacon if (cfg->coherent_walk) { 902*ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_IS; 903*ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 904*ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 90530d2acb6SWill Deacon } else { 906*ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_OS; 907*ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_NC; 908*ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_NC; 90930d2acb6SWill Deacon } 910e1d3c0fdSWill Deacon 911594ab90fSRobin Murphy sl = data->start_level; 912e1d3c0fdSWill Deacon 91306c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 914e1d3c0fdSWill Deacon case SZ_4K: 915*ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_4K; 916e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 917e1d3c0fdSWill Deacon break; 918e1d3c0fdSWill Deacon case SZ_16K: 919*ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_16K; 920e1d3c0fdSWill Deacon break; 921e1d3c0fdSWill Deacon case SZ_64K: 922*ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_64K; 923e1d3c0fdSWill Deacon break; 924e1d3c0fdSWill Deacon } 925e1d3c0fdSWill Deacon 926e1d3c0fdSWill Deacon switch (cfg->oas) { 927e1d3c0fdSWill Deacon case 32: 928*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; 929e1d3c0fdSWill Deacon break; 930e1d3c0fdSWill Deacon case 36: 931*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; 932e1d3c0fdSWill Deacon break; 933e1d3c0fdSWill Deacon case 40: 934*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; 935e1d3c0fdSWill Deacon break; 936e1d3c0fdSWill Deacon case 42: 937*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; 938e1d3c0fdSWill Deacon break; 939e1d3c0fdSWill Deacon case 44: 940*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; 941e1d3c0fdSWill Deacon break; 942e1d3c0fdSWill Deacon case 48: 943*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; 944e1d3c0fdSWill Deacon break; 9456c89928fSRobin Murphy case 52: 946*ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; 9476c89928fSRobin Murphy break; 948e1d3c0fdSWill Deacon default: 949e1d3c0fdSWill Deacon goto out_free_data; 950e1d3c0fdSWill Deacon } 951e1d3c0fdSWill Deacon 952*ac4b80e5SWill Deacon vtcr->tsz = 64ULL - cfg->ias; 953*ac4b80e5SWill Deacon vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; 954e1d3c0fdSWill Deacon 955e1d3c0fdSWill Deacon /* Allocate pgd pages */ 956c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 957c79278c1SRobin Murphy GFP_KERNEL, cfg); 958e1d3c0fdSWill Deacon if (!data->pgd) 959e1d3c0fdSWill Deacon goto out_free_data; 960e1d3c0fdSWill Deacon 96187a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 96287a91b15SRobin Murphy wmb(); 963e1d3c0fdSWill Deacon 964e1d3c0fdSWill Deacon /* VTTBR */ 965e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 966e1d3c0fdSWill Deacon return &data->iop; 967e1d3c0fdSWill Deacon 968e1d3c0fdSWill Deacon out_free_data: 969e1d3c0fdSWill Deacon kfree(data); 970e1d3c0fdSWill Deacon return NULL; 971e1d3c0fdSWill Deacon } 972e1d3c0fdSWill Deacon 973e1d3c0fdSWill Deacon static struct io_pgtable * 974e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 975e1d3c0fdSWill Deacon { 976e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 977e1d3c0fdSWill Deacon return NULL; 978e1d3c0fdSWill Deacon 979e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 980fb485eb1SRobin Murphy return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 981e1d3c0fdSWill Deacon } 982e1d3c0fdSWill Deacon 983e1d3c0fdSWill Deacon static struct io_pgtable * 984e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 985e1d3c0fdSWill Deacon { 986e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 987e1d3c0fdSWill Deacon return NULL; 988e1d3c0fdSWill Deacon 989e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 990*ac4b80e5SWill Deacon return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 991e1d3c0fdSWill Deacon } 992e1d3c0fdSWill Deacon 993d08d42deSRob Herring static struct io_pgtable * 994d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 995d08d42deSRob Herring { 99652f325f4SRobin Murphy struct arm_lpae_io_pgtable *data; 997d08d42deSRob Herring 99852f325f4SRobin Murphy /* No quirks for Mali (hopefully) */ 99952f325f4SRobin Murphy if (cfg->quirks) 100052f325f4SRobin Murphy return NULL; 1001d08d42deSRob Herring 10021be08f45SRobin Murphy if (cfg->ias > 48 || cfg->oas > 40) 1003d08d42deSRob Herring return NULL; 1004d08d42deSRob Herring 1005d08d42deSRob Herring cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1006d08d42deSRob Herring 100752f325f4SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 100852f325f4SRobin Murphy if (!data) 100952f325f4SRobin Murphy return NULL; 1010d08d42deSRob Herring 10111be08f45SRobin Murphy /* Mali seems to need a full 4-level table regardless of IAS */ 1012594ab90fSRobin Murphy if (data->start_level > 0) { 1013594ab90fSRobin Murphy data->start_level = 0; 1014c79278c1SRobin Murphy data->pgd_bits = 0; 10151be08f45SRobin Murphy } 101652f325f4SRobin Murphy /* 101752f325f4SRobin Murphy * MEMATTR: Mali has no actual notion of a non-cacheable type, so the 101852f325f4SRobin Murphy * best we can do is mimic the out-of-tree driver and hope that the 101952f325f4SRobin Murphy * "implementation-defined caching policy" is good enough. Similarly, 102052f325f4SRobin Murphy * we'll use it for the sake of a valid attribute for our 'device' 102152f325f4SRobin Murphy * index, although callers should never request that in practice. 102252f325f4SRobin Murphy */ 102352f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.memattr = 102452f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 102552f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 102652f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 102752f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 102852f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 102952f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 103052f325f4SRobin Murphy 1031c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1032c79278c1SRobin Murphy cfg); 103352f325f4SRobin Murphy if (!data->pgd) 103452f325f4SRobin Murphy goto out_free_data; 103552f325f4SRobin Murphy 103652f325f4SRobin Murphy /* Ensure the empty pgd is visible before TRANSTAB can be written */ 103752f325f4SRobin Murphy wmb(); 103852f325f4SRobin Murphy 103952f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | 1040d08d42deSRob Herring ARM_MALI_LPAE_TTBR_READ_INNER | 1041d08d42deSRob Herring ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; 104252f325f4SRobin Murphy return &data->iop; 1043d08d42deSRob Herring 104452f325f4SRobin Murphy out_free_data: 104552f325f4SRobin Murphy kfree(data); 104652f325f4SRobin Murphy return NULL; 1047d08d42deSRob Herring } 1048d08d42deSRob Herring 1049e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1050e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 1051e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1052e1d3c0fdSWill Deacon }; 1053e1d3c0fdSWill Deacon 1054e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1055e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 1056e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1057e1d3c0fdSWill Deacon }; 1058e1d3c0fdSWill Deacon 1059e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1060e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1061e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1062e1d3c0fdSWill Deacon }; 1063e1d3c0fdSWill Deacon 1064e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1065e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1066e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1067e1d3c0fdSWill Deacon }; 1068fe4b991dSWill Deacon 1069d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1070d08d42deSRob Herring .alloc = arm_mali_lpae_alloc_pgtable, 1071d08d42deSRob Herring .free = arm_lpae_free_pgtable, 1072d08d42deSRob Herring }; 1073d08d42deSRob Herring 1074fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1075fe4b991dSWill Deacon 1076b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata; 1077fe4b991dSWill Deacon 1078b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie) 1079fe4b991dSWill Deacon { 1080fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1081fe4b991dSWill Deacon } 1082fe4b991dSWill Deacon 1083b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size, 1084b5813c16SRobin Murphy size_t granule, void *cookie) 1085fe4b991dSWill Deacon { 1086fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1087fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1088fe4b991dSWill Deacon } 1089fe4b991dSWill Deacon 1090b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, 1091b5813c16SRobin Murphy unsigned long iova, size_t granule, 1092b5813c16SRobin Murphy void *cookie) 109310b7a7d9SWill Deacon { 1094abfd6fe0SWill Deacon dummy_tlb_flush(iova, granule, granule, cookie); 109510b7a7d9SWill Deacon } 109610b7a7d9SWill Deacon 1097298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = { 1098fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 109910b7a7d9SWill Deacon .tlb_flush_walk = dummy_tlb_flush, 110010b7a7d9SWill Deacon .tlb_flush_leaf = dummy_tlb_flush, 1101abfd6fe0SWill Deacon .tlb_add_page = dummy_tlb_add_page, 1102fe4b991dSWill Deacon }; 1103fe4b991dSWill Deacon 1104fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1105fe4b991dSWill Deacon { 1106fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1107fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1108fe4b991dSWill Deacon 1109fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1110fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 11115fb190b0SRobin Murphy pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", 1112c79278c1SRobin Murphy ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), 11135fb190b0SRobin Murphy ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); 1114fe4b991dSWill Deacon } 1115fe4b991dSWill Deacon 1116fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1117fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1118fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1119fe4b991dSWill Deacon selftest_running = false; \ 1120fe4b991dSWill Deacon -EFAULT; \ 1121fe4b991dSWill Deacon }) 1122fe4b991dSWill Deacon 1123fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1124fe4b991dSWill Deacon { 11259062c1d0SChristophe JAILLET static const enum io_pgtable_fmt fmts[] __initconst = { 1126fe4b991dSWill Deacon ARM_64_LPAE_S1, 1127fe4b991dSWill Deacon ARM_64_LPAE_S2, 1128fe4b991dSWill Deacon }; 1129fe4b991dSWill Deacon 1130fe4b991dSWill Deacon int i, j; 1131fe4b991dSWill Deacon unsigned long iova; 1132fe4b991dSWill Deacon size_t size; 1133fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1134fe4b991dSWill Deacon 1135fe4b991dSWill Deacon selftest_running = true; 1136fe4b991dSWill Deacon 1137fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1138fe4b991dSWill Deacon cfg_cookie = cfg; 1139fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1140fe4b991dSWill Deacon if (!ops) { 1141fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1142fe4b991dSWill Deacon return -ENOMEM; 1143fe4b991dSWill Deacon } 1144fe4b991dSWill Deacon 1145fe4b991dSWill Deacon /* 1146fe4b991dSWill Deacon * Initial sanity checks. 1147fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1148fe4b991dSWill Deacon */ 1149fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1150fe4b991dSWill Deacon return __FAIL(ops, i); 1151fe4b991dSWill Deacon 1152fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1153fe4b991dSWill Deacon return __FAIL(ops, i); 1154fe4b991dSWill Deacon 1155fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1156fe4b991dSWill Deacon return __FAIL(ops, i); 1157fe4b991dSWill Deacon 1158fe4b991dSWill Deacon /* 1159fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1160fe4b991dSWill Deacon */ 1161fe4b991dSWill Deacon iova = 0; 11624ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1163fe4b991dSWill Deacon size = 1UL << j; 1164fe4b991dSWill Deacon 1165fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1166fe4b991dSWill Deacon IOMMU_WRITE | 1167fe4b991dSWill Deacon IOMMU_NOEXEC | 1168fe4b991dSWill Deacon IOMMU_CACHE)) 1169fe4b991dSWill Deacon return __FAIL(ops, i); 1170fe4b991dSWill Deacon 1171fe4b991dSWill Deacon /* Overlapping mappings */ 1172fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1173fe4b991dSWill Deacon IOMMU_READ | IOMMU_NOEXEC)) 1174fe4b991dSWill Deacon return __FAIL(ops, i); 1175fe4b991dSWill Deacon 1176fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1177fe4b991dSWill Deacon return __FAIL(ops, i); 1178fe4b991dSWill Deacon 1179fe4b991dSWill Deacon iova += SZ_1G; 1180fe4b991dSWill Deacon } 1181fe4b991dSWill Deacon 1182fe4b991dSWill Deacon /* Partial unmap */ 1183fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1184a2d3a382SWill Deacon if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) 1185fe4b991dSWill Deacon return __FAIL(ops, i); 1186fe4b991dSWill Deacon 1187fe4b991dSWill Deacon /* Remap of partial unmap */ 1188fe4b991dSWill Deacon if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 1189fe4b991dSWill Deacon return __FAIL(ops, i); 1190fe4b991dSWill Deacon 1191fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1192fe4b991dSWill Deacon return __FAIL(ops, i); 1193fe4b991dSWill Deacon 1194fe4b991dSWill Deacon /* Full unmap */ 1195fe4b991dSWill Deacon iova = 0; 1196f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1197fe4b991dSWill Deacon size = 1UL << j; 1198fe4b991dSWill Deacon 1199a2d3a382SWill Deacon if (ops->unmap(ops, iova, size, NULL) != size) 1200fe4b991dSWill Deacon return __FAIL(ops, i); 1201fe4b991dSWill Deacon 1202fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1203fe4b991dSWill Deacon return __FAIL(ops, i); 1204fe4b991dSWill Deacon 1205fe4b991dSWill Deacon /* Remap full block */ 1206fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 1207fe4b991dSWill Deacon return __FAIL(ops, i); 1208fe4b991dSWill Deacon 1209fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1210fe4b991dSWill Deacon return __FAIL(ops, i); 1211fe4b991dSWill Deacon 1212fe4b991dSWill Deacon iova += SZ_1G; 1213fe4b991dSWill Deacon } 1214fe4b991dSWill Deacon 1215fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1216fe4b991dSWill Deacon } 1217fe4b991dSWill Deacon 1218fe4b991dSWill Deacon selftest_running = false; 1219fe4b991dSWill Deacon return 0; 1220fe4b991dSWill Deacon } 1221fe4b991dSWill Deacon 1222fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1223fe4b991dSWill Deacon { 12249062c1d0SChristophe JAILLET static const unsigned long pgsize[] __initconst = { 1225fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1226fe4b991dSWill Deacon SZ_16K | SZ_32M, 1227fe4b991dSWill Deacon SZ_64K | SZ_512M, 1228fe4b991dSWill Deacon }; 1229fe4b991dSWill Deacon 12309062c1d0SChristophe JAILLET static const unsigned int ias[] __initconst = { 1231fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1232fe4b991dSWill Deacon }; 1233fe4b991dSWill Deacon 1234fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1235fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1236fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1237fe4b991dSWill Deacon .oas = 48, 12384f41845bSWill Deacon .coherent_walk = true, 1239fe4b991dSWill Deacon }; 1240fe4b991dSWill Deacon 1241fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1242fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1243fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1244fe4b991dSWill Deacon cfg.ias = ias[j]; 1245fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1246fe4b991dSWill Deacon pgsize[i], ias[j]); 1247fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1248fe4b991dSWill Deacon fail++; 1249fe4b991dSWill Deacon else 1250fe4b991dSWill Deacon pass++; 1251fe4b991dSWill Deacon } 1252fe4b991dSWill Deacon } 1253fe4b991dSWill Deacon 1254fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1255fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1256fe4b991dSWill Deacon } 1257fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1258fe4b991dSWill Deacon #endif 1259