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 107*db690301SRobin Murphy #define ARM_LPAE_TCR_TG1_16K 1 108*db690301SRobin Murphy #define ARM_LPAE_TCR_TG1_4K 2 109*db690301SRobin Murphy #define ARM_LPAE_TCR_TG1_64K 3 110*db690301SRobin Murphy 111e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS 0 112e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS 2 113e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS 3 114e1d3c0fdSWill Deacon 115e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC 0 116e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA 1 117e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT 2 118e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB 3 119e1d3c0fdSWill Deacon 120fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_SL0_MASK 0x3 121e1d3c0fdSWill Deacon 122e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 123e1d3c0fdSWill Deacon 124fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_SHIFT 16 125fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_MASK 0x7 126e1d3c0fdSWill Deacon 127e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 129e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 131e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 132e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 1336c89928fSRobin Murphy #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL 134e1d3c0fdSWill Deacon 135e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 136e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 137e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 138e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 13990ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 140e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 141e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 142e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 143e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 14490ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 145e1d3c0fdSWill Deacon 146d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) 147d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) 148d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) 149d08d42deSRob Herring 15052f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL 15152f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL 15252f325f4SRobin Murphy 153e1d3c0fdSWill Deacon /* IOPTE accessors */ 1546c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 155e1d3c0fdSWill Deacon 156e1d3c0fdSWill Deacon #define iopte_type(pte,l) \ 157e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 158e1d3c0fdSWill Deacon 159e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 160e1d3c0fdSWill Deacon 161e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 162e1d3c0fdSWill Deacon struct io_pgtable iop; 163e1d3c0fdSWill Deacon 164c79278c1SRobin Murphy int pgd_bits; 165594ab90fSRobin Murphy int start_level; 1665fb190b0SRobin Murphy int bits_per_level; 167e1d3c0fdSWill Deacon 168e1d3c0fdSWill Deacon void *pgd; 169e1d3c0fdSWill Deacon }; 170e1d3c0fdSWill Deacon 171e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 172e1d3c0fdSWill Deacon 173d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, 174d08d42deSRob Herring enum io_pgtable_fmt fmt) 175d08d42deSRob Herring { 176d08d42deSRob Herring if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) 177d08d42deSRob Herring return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE; 178d08d42deSRob Herring 179d08d42deSRob Herring return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK; 180d08d42deSRob Herring } 181d08d42deSRob Herring 1826c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, 1836c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1846c89928fSRobin Murphy { 1856c89928fSRobin Murphy arm_lpae_iopte pte = paddr; 1866c89928fSRobin Murphy 1876c89928fSRobin Murphy /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ 1886c89928fSRobin Murphy return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; 1896c89928fSRobin Murphy } 1906c89928fSRobin Murphy 1916c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, 1926c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1936c89928fSRobin Murphy { 19478688059SRobin Murphy u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; 1956c89928fSRobin Murphy 1965fb190b0SRobin Murphy if (ARM_LPAE_GRANULE(data) < SZ_64K) 1976c89928fSRobin Murphy return paddr; 1986c89928fSRobin Murphy 1996c89928fSRobin Murphy /* Rotate the packed high-order bits back to the top */ 2006c89928fSRobin Murphy return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); 2016c89928fSRobin Murphy } 2026c89928fSRobin Murphy 203fe4b991dSWill Deacon static bool selftest_running = false; 204fe4b991dSWill Deacon 205ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 206f8d54961SRobin Murphy { 207ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 208f8d54961SRobin Murphy } 209f8d54961SRobin Murphy 210f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 211f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 212f8d54961SRobin Murphy { 213f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 2144b123757SRobin Murphy int order = get_order(size); 2154b123757SRobin Murphy struct page *p; 216f8d54961SRobin Murphy dma_addr_t dma; 2174b123757SRobin Murphy void *pages; 218f8d54961SRobin Murphy 2194b123757SRobin Murphy VM_BUG_ON((gfp & __GFP_HIGHMEM)); 220fac83d29SJean-Philippe Brucker p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 221fac83d29SJean-Philippe Brucker gfp | __GFP_ZERO, order); 2224b123757SRobin Murphy if (!p) 223f8d54961SRobin Murphy return NULL; 224f8d54961SRobin Murphy 2254b123757SRobin Murphy pages = page_address(p); 2264f41845bSWill Deacon if (!cfg->coherent_walk) { 227f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 228f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 229f8d54961SRobin Murphy goto out_free; 230f8d54961SRobin Murphy /* 231f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 232ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 233ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 234f8d54961SRobin Murphy */ 235ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 236f8d54961SRobin Murphy goto out_unmap; 237f8d54961SRobin Murphy } 238f8d54961SRobin Murphy 239f8d54961SRobin Murphy return pages; 240f8d54961SRobin Murphy 241f8d54961SRobin Murphy out_unmap: 242f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 243f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 244f8d54961SRobin Murphy out_free: 2454b123757SRobin Murphy __free_pages(p, order); 246f8d54961SRobin Murphy return NULL; 247f8d54961SRobin Murphy } 248f8d54961SRobin Murphy 249f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 250f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 251f8d54961SRobin Murphy { 2524f41845bSWill Deacon if (!cfg->coherent_walk) 253ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 254f8d54961SRobin Murphy size, DMA_TO_DEVICE); 2554b123757SRobin Murphy free_pages((unsigned long)pages, get_order(size)); 256f8d54961SRobin Murphy } 257f8d54961SRobin Murphy 2582c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 2592c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2602c3d273eSRobin Murphy { 2612c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 2622c3d273eSRobin Murphy sizeof(*ptep), DMA_TO_DEVICE); 2632c3d273eSRobin Murphy } 2642c3d273eSRobin Murphy 265f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 26687a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 267f8d54961SRobin Murphy { 268f8d54961SRobin Murphy *ptep = pte; 269f8d54961SRobin Murphy 2704f41845bSWill Deacon if (!cfg->coherent_walk) 2712c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 272f8d54961SRobin Murphy } 273f8d54961SRobin Murphy 274193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 2753951c41aSWill Deacon struct iommu_iotlb_gather *gather, 276cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 277cf27ec93SWill Deacon arm_lpae_iopte *ptep); 278cf27ec93SWill Deacon 279fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 280fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 281fb3a9579SRobin Murphy int lvl, arm_lpae_iopte *ptep) 282fb3a9579SRobin Murphy { 283fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 284fb3a9579SRobin Murphy 285d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) 286fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 287fb3a9579SRobin Murphy else 288fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 289fb3a9579SRobin Murphy 2906c89928fSRobin Murphy pte |= paddr_to_iopte(paddr, data); 291fb3a9579SRobin Murphy 292fb3a9579SRobin Murphy __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); 293fb3a9579SRobin Murphy } 294fb3a9579SRobin Murphy 295e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 296e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 297e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 298e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 299e1d3c0fdSWill Deacon { 300fb3a9579SRobin Murphy arm_lpae_iopte pte = *ptep; 301e1d3c0fdSWill Deacon 302d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) { 303cf27ec93SWill Deacon /* We require an unmap first */ 304fe4b991dSWill Deacon WARN_ON(!selftest_running); 305e1d3c0fdSWill Deacon return -EEXIST; 306fb3a9579SRobin Murphy } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 307cf27ec93SWill Deacon /* 308cf27ec93SWill Deacon * We need to unmap and free the old table before 309cf27ec93SWill Deacon * overwriting it with a block entry. 310cf27ec93SWill Deacon */ 311cf27ec93SWill Deacon arm_lpae_iopte *tblp; 312cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 313cf27ec93SWill Deacon 314cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 3153951c41aSWill Deacon if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) { 3163951c41aSWill Deacon WARN_ON(1); 317cf27ec93SWill Deacon return -EINVAL; 318fe4b991dSWill Deacon } 3193951c41aSWill Deacon } 320e1d3c0fdSWill Deacon 321fb3a9579SRobin Murphy __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); 322e1d3c0fdSWill Deacon return 0; 323e1d3c0fdSWill Deacon } 324e1d3c0fdSWill Deacon 325fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 326fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3272c3d273eSRobin Murphy arm_lpae_iopte curr, 328fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 329fb3a9579SRobin Murphy { 3302c3d273eSRobin Murphy arm_lpae_iopte old, new; 331fb3a9579SRobin Murphy 332fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 333fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 334fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 335fb3a9579SRobin Murphy 33677f34458SWill Deacon /* 33777f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 33877f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 33977f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 34077f34458SWill Deacon */ 34177f34458SWill Deacon dma_wmb(); 3422c3d273eSRobin Murphy 3432c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3442c3d273eSRobin Murphy 3454f41845bSWill Deacon if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) 3462c3d273eSRobin Murphy return old; 3472c3d273eSRobin Murphy 3482c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 3492c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3502c3d273eSRobin Murphy if (old == curr) 3512c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3522c3d273eSRobin Murphy 3532c3d273eSRobin Murphy return old; 354fb3a9579SRobin Murphy } 355fb3a9579SRobin Murphy 356e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 357e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 358e1d3c0fdSWill Deacon int lvl, arm_lpae_iopte *ptep) 359e1d3c0fdSWill Deacon { 360e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 361e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3622c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 363f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 364e1d3c0fdSWill Deacon 365e1d3c0fdSWill Deacon /* Find our entry at the current level */ 366e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 367e1d3c0fdSWill Deacon 368e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 369f7b90d2cSRobin Murphy if (size == block_size) 370e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 371e1d3c0fdSWill Deacon 372e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 373e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 374e1d3c0fdSWill Deacon return -EINVAL; 375e1d3c0fdSWill Deacon 376e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3772c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 378e1d3c0fdSWill Deacon if (!pte) { 3792c3d273eSRobin Murphy cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); 380e1d3c0fdSWill Deacon if (!cptep) 381e1d3c0fdSWill Deacon return -ENOMEM; 382e1d3c0fdSWill Deacon 3832c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 3842c3d273eSRobin Murphy if (pte) 3852c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 3864f41845bSWill Deacon } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 3872c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3882c3d273eSRobin Murphy } 3892c3d273eSRobin Murphy 390d08d42deSRob Herring if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { 391e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 3922c3d273eSRobin Murphy } else if (pte) { 393ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 394ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 395ed46e66cSOleksandr Tyshchenko return -EEXIST; 396e1d3c0fdSWill Deacon } 397e1d3c0fdSWill Deacon 398e1d3c0fdSWill Deacon /* Rinse, repeat */ 399e1d3c0fdSWill Deacon return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 400e1d3c0fdSWill Deacon } 401e1d3c0fdSWill Deacon 402e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 403e1d3c0fdSWill Deacon int prot) 404e1d3c0fdSWill Deacon { 405e1d3c0fdSWill Deacon arm_lpae_iopte pte; 406e1d3c0fdSWill Deacon 407e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 408e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 409e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 410e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 411e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 412e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 413e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 414e1d3c0fdSWill Deacon } else { 415e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 416e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 417e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 418e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 419e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 420d08d42deSRob Herring } 421d08d42deSRob Herring 422d08d42deSRob Herring /* 423d08d42deSRob Herring * Note that this logic is structured to accommodate Mali LPAE 424d08d42deSRob Herring * having stage-1-like attributes but stage-2-like permissions. 425d08d42deSRob Herring */ 426d08d42deSRob Herring if (data->iop.fmt == ARM_64_LPAE_S2 || 427d08d42deSRob Herring data->iop.fmt == ARM_32_LPAE_S2) { 428fb948251SRobin Murphy if (prot & IOMMU_MMIO) 429fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 430fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 431e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 432e1d3c0fdSWill Deacon else 433e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 434d08d42deSRob Herring } else { 435d08d42deSRob Herring if (prot & IOMMU_MMIO) 436d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 437d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 438d08d42deSRob Herring else if (prot & IOMMU_CACHE) 439d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 440d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 441dd5ddd3cSWill Deacon else if (prot & IOMMU_SYS_CACHE_ONLY) 44290ec7a76SVivek Gautam pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 44390ec7a76SVivek Gautam << ARM_LPAE_PTE_ATTRINDX_SHIFT); 444e1d3c0fdSWill Deacon } 445e1d3c0fdSWill Deacon 4467618e479SRobin Murphy if (prot & IOMMU_CACHE) 4477618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_IS; 4487618e479SRobin Murphy else 4497618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_OS; 4507618e479SRobin Murphy 451e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 452e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 453e1d3c0fdSWill Deacon 4547618e479SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 4557618e479SRobin Murphy pte |= ARM_LPAE_PTE_NS; 4567618e479SRobin Murphy 4577618e479SRobin Murphy if (data->iop.fmt != ARM_MALI_LPAE) 4587618e479SRobin Murphy pte |= ARM_LPAE_PTE_AF; 4597618e479SRobin Murphy 460e1d3c0fdSWill Deacon return pte; 461e1d3c0fdSWill Deacon } 462e1d3c0fdSWill Deacon 463e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 464e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, int iommu_prot) 465e1d3c0fdSWill Deacon { 466e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 467f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 468e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 469594ab90fSRobin Murphy int ret, lvl = data->start_level; 470e1d3c0fdSWill Deacon arm_lpae_iopte prot; 471*db690301SRobin Murphy long iaext = (long)iova >> cfg->ias; 472e1d3c0fdSWill Deacon 473e1d3c0fdSWill Deacon /* If no access, then nothing to do */ 474e1d3c0fdSWill Deacon if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 475e1d3c0fdSWill Deacon return 0; 476e1d3c0fdSWill Deacon 477f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 478f7b90d2cSRobin Murphy return -EINVAL; 479f7b90d2cSRobin Murphy 480*db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 481*db690301SRobin Murphy iaext = ~iaext; 482*db690301SRobin Murphy if (WARN_ON(iaext || paddr >> cfg->oas)) 48376557391SRobin Murphy return -ERANGE; 48476557391SRobin Murphy 485e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 48687a91b15SRobin Murphy ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 48787a91b15SRobin Murphy /* 48887a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 48987a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 49087a91b15SRobin Murphy */ 49187a91b15SRobin Murphy wmb(); 49287a91b15SRobin Murphy 49387a91b15SRobin Murphy return ret; 494e1d3c0fdSWill Deacon } 495e1d3c0fdSWill Deacon 496e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 497e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 498e1d3c0fdSWill Deacon { 499e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 500e1d3c0fdSWill Deacon unsigned long table_size; 501e1d3c0fdSWill Deacon 502594ab90fSRobin Murphy if (lvl == data->start_level) 503c79278c1SRobin Murphy table_size = ARM_LPAE_PGD_SIZE(data); 504e1d3c0fdSWill Deacon else 50506c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 506e1d3c0fdSWill Deacon 507e1d3c0fdSWill Deacon start = ptep; 50812c2ab09SWill Deacon 50912c2ab09SWill Deacon /* Only leaf entries at the last level */ 51012c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 51112c2ab09SWill Deacon end = ptep; 51212c2ab09SWill Deacon else 513e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 514e1d3c0fdSWill Deacon 515e1d3c0fdSWill Deacon while (ptep != end) { 516e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 517e1d3c0fdSWill Deacon 518d08d42deSRob Herring if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) 519e1d3c0fdSWill Deacon continue; 520e1d3c0fdSWill Deacon 521e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 522e1d3c0fdSWill Deacon } 523e1d3c0fdSWill Deacon 524f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 525e1d3c0fdSWill Deacon } 526e1d3c0fdSWill Deacon 527e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 528e1d3c0fdSWill Deacon { 529e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 530e1d3c0fdSWill Deacon 531594ab90fSRobin Murphy __arm_lpae_free_pgtable(data, data->start_level, data->pgd); 532e1d3c0fdSWill Deacon kfree(data); 533e1d3c0fdSWill Deacon } 534e1d3c0fdSWill Deacon 535193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 5363951c41aSWill Deacon struct iommu_iotlb_gather *gather, 537e1d3c0fdSWill Deacon unsigned long iova, size_t size, 538fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 539fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 540e1d3c0fdSWill Deacon { 541fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 542fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 543e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 544fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 545fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 546fb3a9579SRobin Murphy int i, unmap_idx = -1; 547e1d3c0fdSWill Deacon 548fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 549fb3a9579SRobin Murphy return 0; 550e1d3c0fdSWill Deacon 551fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 552fb3a9579SRobin Murphy if (!tablep) 553fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 554e1d3c0fdSWill Deacon 555fb3a9579SRobin Murphy if (size == split_sz) 556fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 557fb3a9579SRobin Murphy 5586c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 559fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 560fb3a9579SRobin Murphy 561fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 562e1d3c0fdSWill Deacon /* Unmap! */ 563fb3a9579SRobin Murphy if (i == unmap_idx) 564e1d3c0fdSWill Deacon continue; 565e1d3c0fdSWill Deacon 566fb3a9579SRobin Murphy __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); 567e1d3c0fdSWill Deacon } 568e1d3c0fdSWill Deacon 5692c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5702c3d273eSRobin Murphy if (pte != blk_pte) { 5712c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5722c3d273eSRobin Murphy /* 5732c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5742c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5752c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5762c3d273eSRobin Murphy */ 5772c3d273eSRobin Murphy if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) 5782c3d273eSRobin Murphy return 0; 5792c3d273eSRobin Murphy 5802c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 58185c7a0f1SRobin Murphy } else if (unmap_idx >= 0) { 5823951c41aSWill Deacon io_pgtable_tlb_add_page(&data->iop, gather, iova, size); 583e1d3c0fdSWill Deacon return size; 584e1d3c0fdSWill Deacon } 585e1d3c0fdSWill Deacon 5863951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep); 58785c7a0f1SRobin Murphy } 58885c7a0f1SRobin Murphy 589193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 5903951c41aSWill Deacon struct iommu_iotlb_gather *gather, 591e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 592e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 593e1d3c0fdSWill Deacon { 594e1d3c0fdSWill Deacon arm_lpae_iopte pte; 595507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 596e1d3c0fdSWill Deacon 5972eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5982eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5992eb97c78SRobin Murphy return 0; 6002eb97c78SRobin Murphy 601e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6022c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 6032eb97c78SRobin Murphy if (WARN_ON(!pte)) 604e1d3c0fdSWill Deacon return 0; 605e1d3c0fdSWill Deacon 606e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 607fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 608507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 609e1d3c0fdSWill Deacon 610d08d42deSRob Herring if (!iopte_leaf(pte, lvl, iop->fmt)) { 611e1d3c0fdSWill Deacon /* Also flush any partial walks */ 61210b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(iop, iova, size, 61310b7a7d9SWill Deacon ARM_LPAE_GRANULE(data)); 614e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 615e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 616b6b65ca2SZhen Lei } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { 617b6b65ca2SZhen Lei /* 618b6b65ca2SZhen Lei * Order the PTE update against queueing the IOVA, to 619b6b65ca2SZhen Lei * guarantee that a flush callback from a different CPU 620b6b65ca2SZhen Lei * has observed it before the TLBIALL can be issued. 621b6b65ca2SZhen Lei */ 622b6b65ca2SZhen Lei smp_wmb(); 623e1d3c0fdSWill Deacon } else { 6243951c41aSWill Deacon io_pgtable_tlb_add_page(iop, gather, iova, size); 625e1d3c0fdSWill Deacon } 626e1d3c0fdSWill Deacon 627e1d3c0fdSWill Deacon return size; 628d08d42deSRob Herring } else if (iopte_leaf(pte, lvl, iop->fmt)) { 629e1d3c0fdSWill Deacon /* 630e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 631e1d3c0fdSWill Deacon * minus the part we want to unmap 632e1d3c0fdSWill Deacon */ 6333951c41aSWill Deacon return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, 634fb3a9579SRobin Murphy lvl + 1, ptep); 635e1d3c0fdSWill Deacon } 636e1d3c0fdSWill Deacon 637e1d3c0fdSWill Deacon /* Keep on walkin' */ 638e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 6393951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep); 640e1d3c0fdSWill Deacon } 641e1d3c0fdSWill Deacon 642193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 643a2d3a382SWill Deacon size_t size, struct iommu_iotlb_gather *gather) 644e1d3c0fdSWill Deacon { 645e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 646f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 647e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 648*db690301SRobin Murphy long iaext = (long)iova >> cfg->ias; 649e1d3c0fdSWill Deacon 650f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 651f7b90d2cSRobin Murphy return 0; 652f7b90d2cSRobin Murphy 653*db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 654*db690301SRobin Murphy iaext = ~iaext; 655*db690301SRobin Murphy if (WARN_ON(iaext)) 65676557391SRobin Murphy return 0; 65776557391SRobin Murphy 658594ab90fSRobin Murphy return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep); 659e1d3c0fdSWill Deacon } 660e1d3c0fdSWill Deacon 661e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 662e1d3c0fdSWill Deacon unsigned long iova) 663e1d3c0fdSWill Deacon { 664e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 665e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 666594ab90fSRobin Murphy int lvl = data->start_level; 667e1d3c0fdSWill Deacon 668e1d3c0fdSWill Deacon do { 669e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 670e1d3c0fdSWill Deacon if (!ptep) 671e1d3c0fdSWill Deacon return 0; 672e1d3c0fdSWill Deacon 673e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6742c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6752c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 676e1d3c0fdSWill Deacon 677e1d3c0fdSWill Deacon /* Valid entry? */ 678e1d3c0fdSWill Deacon if (!pte) 679e1d3c0fdSWill Deacon return 0; 680e1d3c0fdSWill Deacon 681e1d3c0fdSWill Deacon /* Leaf entry? */ 682d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) 683e1d3c0fdSWill Deacon goto found_translation; 684e1d3c0fdSWill Deacon 685e1d3c0fdSWill Deacon /* Take it to the next level */ 686e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 687e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 688e1d3c0fdSWill Deacon 689e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 690e1d3c0fdSWill Deacon return 0; 691e1d3c0fdSWill Deacon 692e1d3c0fdSWill Deacon found_translation: 6937c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 6946c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 695e1d3c0fdSWill Deacon } 696e1d3c0fdSWill Deacon 697e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 698e1d3c0fdSWill Deacon { 6996c89928fSRobin Murphy unsigned long granule, page_sizes; 7006c89928fSRobin Murphy unsigned int max_addr_bits = 48; 701e1d3c0fdSWill Deacon 702e1d3c0fdSWill Deacon /* 703e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 704e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 705e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 706e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 707e1d3c0fdSWill Deacon * chosen granule. 708e1d3c0fdSWill Deacon */ 709e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 710e1d3c0fdSWill Deacon granule = PAGE_SIZE; 711e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 712e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 713e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 714e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 715e1d3c0fdSWill Deacon else 716e1d3c0fdSWill Deacon granule = 0; 717e1d3c0fdSWill Deacon 718e1d3c0fdSWill Deacon switch (granule) { 719e1d3c0fdSWill Deacon case SZ_4K: 7206c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 721e1d3c0fdSWill Deacon break; 722e1d3c0fdSWill Deacon case SZ_16K: 7236c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 724e1d3c0fdSWill Deacon break; 725e1d3c0fdSWill Deacon case SZ_64K: 7266c89928fSRobin Murphy max_addr_bits = 52; 7276c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7286c89928fSRobin Murphy if (cfg->oas > 48) 7296c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 730e1d3c0fdSWill Deacon break; 731e1d3c0fdSWill Deacon default: 7326c89928fSRobin Murphy page_sizes = 0; 733e1d3c0fdSWill Deacon } 7346c89928fSRobin Murphy 7356c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7366c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7376c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 738e1d3c0fdSWill Deacon } 739e1d3c0fdSWill Deacon 740e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 741e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 742e1d3c0fdSWill Deacon { 743e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 7445fb190b0SRobin Murphy int levels, va_bits, pg_shift; 745e1d3c0fdSWill Deacon 746e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 747e1d3c0fdSWill Deacon 748e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 749e1d3c0fdSWill Deacon return NULL; 750e1d3c0fdSWill Deacon 751e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 752e1d3c0fdSWill Deacon return NULL; 753e1d3c0fdSWill Deacon 754e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 755e1d3c0fdSWill Deacon return NULL; 756e1d3c0fdSWill Deacon 757ffcb6d16SRobin Murphy if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { 758ffcb6d16SRobin Murphy dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); 759ffcb6d16SRobin Murphy return NULL; 760ffcb6d16SRobin Murphy } 761ffcb6d16SRobin Murphy 762e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 763e1d3c0fdSWill Deacon if (!data) 764e1d3c0fdSWill Deacon return NULL; 765e1d3c0fdSWill Deacon 7665fb190b0SRobin Murphy pg_shift = __ffs(cfg->pgsize_bitmap); 7675fb190b0SRobin Murphy data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); 768e1d3c0fdSWill Deacon 7695fb190b0SRobin Murphy va_bits = cfg->ias - pg_shift; 770594ab90fSRobin Murphy levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 771594ab90fSRobin Murphy data->start_level = ARM_LPAE_MAX_LEVELS - levels; 772e1d3c0fdSWill Deacon 773e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 774c79278c1SRobin Murphy data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); 775e1d3c0fdSWill Deacon 776e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 777e1d3c0fdSWill Deacon .map = arm_lpae_map, 778e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 779e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 780e1d3c0fdSWill Deacon }; 781e1d3c0fdSWill Deacon 782e1d3c0fdSWill Deacon return data; 783e1d3c0fdSWill Deacon } 784e1d3c0fdSWill Deacon 785e1d3c0fdSWill Deacon static struct io_pgtable * 786e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 787e1d3c0fdSWill Deacon { 788e1d3c0fdSWill Deacon u64 reg; 7893850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 790fb485eb1SRobin Murphy typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; 791*db690301SRobin Murphy bool tg1; 792e1d3c0fdSWill Deacon 7934f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | 794*db690301SRobin Murphy IO_PGTABLE_QUIRK_NON_STRICT | 795*db690301SRobin Murphy IO_PGTABLE_QUIRK_ARM_TTBR1)) 7963850db49SRobin Murphy return NULL; 7973850db49SRobin Murphy 7983850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 799e1d3c0fdSWill Deacon if (!data) 800e1d3c0fdSWill Deacon return NULL; 801e1d3c0fdSWill Deacon 802e1d3c0fdSWill Deacon /* TCR */ 8039e6ea59fSBjorn Andersson if (cfg->coherent_walk) { 804fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_IS; 805fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 806fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 8079e6ea59fSBjorn Andersson } else { 808fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_OS; 809fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_NC; 810fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_NC; 8119e6ea59fSBjorn Andersson } 812e1d3c0fdSWill Deacon 813*db690301SRobin Murphy tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1; 81406c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 815e1d3c0fdSWill Deacon case SZ_4K: 816*db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K; 817e1d3c0fdSWill Deacon break; 818e1d3c0fdSWill Deacon case SZ_16K: 819*db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K; 820e1d3c0fdSWill Deacon break; 821e1d3c0fdSWill Deacon case SZ_64K: 822*db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K; 823e1d3c0fdSWill Deacon break; 824e1d3c0fdSWill Deacon } 825e1d3c0fdSWill Deacon 826e1d3c0fdSWill Deacon switch (cfg->oas) { 827e1d3c0fdSWill Deacon case 32: 828fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_32_BIT; 829e1d3c0fdSWill Deacon break; 830e1d3c0fdSWill Deacon case 36: 831fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_36_BIT; 832e1d3c0fdSWill Deacon break; 833e1d3c0fdSWill Deacon case 40: 834fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_40_BIT; 835e1d3c0fdSWill Deacon break; 836e1d3c0fdSWill Deacon case 42: 837fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_42_BIT; 838e1d3c0fdSWill Deacon break; 839e1d3c0fdSWill Deacon case 44: 840fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_44_BIT; 841e1d3c0fdSWill Deacon break; 842e1d3c0fdSWill Deacon case 48: 843fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_48_BIT; 844e1d3c0fdSWill Deacon break; 8456c89928fSRobin Murphy case 52: 846fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_52_BIT; 8476c89928fSRobin Murphy break; 848e1d3c0fdSWill Deacon default: 849e1d3c0fdSWill Deacon goto out_free_data; 850e1d3c0fdSWill Deacon } 851e1d3c0fdSWill Deacon 852fb485eb1SRobin Murphy tcr->tsz = 64ULL - cfg->ias; 853e1d3c0fdSWill Deacon 854e1d3c0fdSWill Deacon /* MAIRs */ 855e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 856e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 857e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 858e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 859e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 86090ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | 86190ec7a76SVivek Gautam (ARM_LPAE_MAIR_ATTR_INC_OWBRWA 86290ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); 863e1d3c0fdSWill Deacon 864205577abSRobin Murphy cfg->arm_lpae_s1_cfg.mair = reg; 865e1d3c0fdSWill Deacon 866e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 867c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 868c79278c1SRobin Murphy GFP_KERNEL, cfg); 869e1d3c0fdSWill Deacon if (!data->pgd) 870e1d3c0fdSWill Deacon goto out_free_data; 871e1d3c0fdSWill Deacon 87287a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 87387a91b15SRobin Murphy wmb(); 874e1d3c0fdSWill Deacon 875d1e5f26fSRobin Murphy /* TTBR */ 876d1e5f26fSRobin Murphy cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); 877e1d3c0fdSWill Deacon return &data->iop; 878e1d3c0fdSWill Deacon 879e1d3c0fdSWill Deacon out_free_data: 880e1d3c0fdSWill Deacon kfree(data); 881e1d3c0fdSWill Deacon return NULL; 882e1d3c0fdSWill Deacon } 883e1d3c0fdSWill Deacon 884e1d3c0fdSWill Deacon static struct io_pgtable * 885e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 886e1d3c0fdSWill Deacon { 887ac4b80e5SWill Deacon u64 sl; 8883850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 889ac4b80e5SWill Deacon typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; 890e1d3c0fdSWill Deacon 8913850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 8924f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) 8933850db49SRobin Murphy return NULL; 8943850db49SRobin Murphy 8953850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 896e1d3c0fdSWill Deacon if (!data) 897e1d3c0fdSWill Deacon return NULL; 898e1d3c0fdSWill Deacon 899e1d3c0fdSWill Deacon /* 900e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 901e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 902e1d3c0fdSWill Deacon */ 903594ab90fSRobin Murphy if (data->start_level == 0) { 904e1d3c0fdSWill Deacon unsigned long pgd_pages; 905e1d3c0fdSWill Deacon 906c79278c1SRobin Murphy pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); 907e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 908c79278c1SRobin Murphy data->pgd_bits += data->bits_per_level; 909594ab90fSRobin Murphy data->start_level++; 910e1d3c0fdSWill Deacon } 911e1d3c0fdSWill Deacon } 912e1d3c0fdSWill Deacon 913e1d3c0fdSWill Deacon /* VTCR */ 91430d2acb6SWill Deacon if (cfg->coherent_walk) { 915ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_IS; 916ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 917ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 91830d2acb6SWill Deacon } else { 919ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_OS; 920ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_NC; 921ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_NC; 92230d2acb6SWill Deacon } 923e1d3c0fdSWill Deacon 924594ab90fSRobin Murphy sl = data->start_level; 925e1d3c0fdSWill Deacon 92606c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 927e1d3c0fdSWill Deacon case SZ_4K: 928ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_4K; 929e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 930e1d3c0fdSWill Deacon break; 931e1d3c0fdSWill Deacon case SZ_16K: 932ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_16K; 933e1d3c0fdSWill Deacon break; 934e1d3c0fdSWill Deacon case SZ_64K: 935ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_64K; 936e1d3c0fdSWill Deacon break; 937e1d3c0fdSWill Deacon } 938e1d3c0fdSWill Deacon 939e1d3c0fdSWill Deacon switch (cfg->oas) { 940e1d3c0fdSWill Deacon case 32: 941ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; 942e1d3c0fdSWill Deacon break; 943e1d3c0fdSWill Deacon case 36: 944ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; 945e1d3c0fdSWill Deacon break; 946e1d3c0fdSWill Deacon case 40: 947ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; 948e1d3c0fdSWill Deacon break; 949e1d3c0fdSWill Deacon case 42: 950ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; 951e1d3c0fdSWill Deacon break; 952e1d3c0fdSWill Deacon case 44: 953ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; 954e1d3c0fdSWill Deacon break; 955e1d3c0fdSWill Deacon case 48: 956ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; 957e1d3c0fdSWill Deacon break; 9586c89928fSRobin Murphy case 52: 959ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; 9606c89928fSRobin Murphy break; 961e1d3c0fdSWill Deacon default: 962e1d3c0fdSWill Deacon goto out_free_data; 963e1d3c0fdSWill Deacon } 964e1d3c0fdSWill Deacon 965ac4b80e5SWill Deacon vtcr->tsz = 64ULL - cfg->ias; 966ac4b80e5SWill Deacon vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; 967e1d3c0fdSWill Deacon 968e1d3c0fdSWill Deacon /* Allocate pgd pages */ 969c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 970c79278c1SRobin Murphy GFP_KERNEL, cfg); 971e1d3c0fdSWill Deacon if (!data->pgd) 972e1d3c0fdSWill Deacon goto out_free_data; 973e1d3c0fdSWill Deacon 97487a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 97587a91b15SRobin Murphy wmb(); 976e1d3c0fdSWill Deacon 977e1d3c0fdSWill Deacon /* VTTBR */ 978e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 979e1d3c0fdSWill Deacon return &data->iop; 980e1d3c0fdSWill Deacon 981e1d3c0fdSWill Deacon out_free_data: 982e1d3c0fdSWill Deacon kfree(data); 983e1d3c0fdSWill Deacon return NULL; 984e1d3c0fdSWill Deacon } 985e1d3c0fdSWill Deacon 986e1d3c0fdSWill Deacon static struct io_pgtable * 987e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 988e1d3c0fdSWill Deacon { 989e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 990e1d3c0fdSWill Deacon return NULL; 991e1d3c0fdSWill Deacon 992e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 993fb485eb1SRobin Murphy return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 994e1d3c0fdSWill Deacon } 995e1d3c0fdSWill Deacon 996e1d3c0fdSWill Deacon static struct io_pgtable * 997e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 998e1d3c0fdSWill Deacon { 999e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 1000e1d3c0fdSWill Deacon return NULL; 1001e1d3c0fdSWill Deacon 1002e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1003ac4b80e5SWill Deacon return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 1004e1d3c0fdSWill Deacon } 1005e1d3c0fdSWill Deacon 1006d08d42deSRob Herring static struct io_pgtable * 1007d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 1008d08d42deSRob Herring { 100952f325f4SRobin Murphy struct arm_lpae_io_pgtable *data; 1010d08d42deSRob Herring 101152f325f4SRobin Murphy /* No quirks for Mali (hopefully) */ 101252f325f4SRobin Murphy if (cfg->quirks) 101352f325f4SRobin Murphy return NULL; 1014d08d42deSRob Herring 10151be08f45SRobin Murphy if (cfg->ias > 48 || cfg->oas > 40) 1016d08d42deSRob Herring return NULL; 1017d08d42deSRob Herring 1018d08d42deSRob Herring cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1019d08d42deSRob Herring 102052f325f4SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 102152f325f4SRobin Murphy if (!data) 102252f325f4SRobin Murphy return NULL; 1023d08d42deSRob Herring 10241be08f45SRobin Murphy /* Mali seems to need a full 4-level table regardless of IAS */ 1025594ab90fSRobin Murphy if (data->start_level > 0) { 1026594ab90fSRobin Murphy data->start_level = 0; 1027c79278c1SRobin Murphy data->pgd_bits = 0; 10281be08f45SRobin Murphy } 102952f325f4SRobin Murphy /* 103052f325f4SRobin Murphy * MEMATTR: Mali has no actual notion of a non-cacheable type, so the 103152f325f4SRobin Murphy * best we can do is mimic the out-of-tree driver and hope that the 103252f325f4SRobin Murphy * "implementation-defined caching policy" is good enough. Similarly, 103352f325f4SRobin Murphy * we'll use it for the sake of a valid attribute for our 'device' 103452f325f4SRobin Murphy * index, although callers should never request that in practice. 103552f325f4SRobin Murphy */ 103652f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.memattr = 103752f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 103852f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 103952f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 104052f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 104152f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 104252f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 104352f325f4SRobin Murphy 1044c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1045c79278c1SRobin Murphy cfg); 104652f325f4SRobin Murphy if (!data->pgd) 104752f325f4SRobin Murphy goto out_free_data; 104852f325f4SRobin Murphy 104952f325f4SRobin Murphy /* Ensure the empty pgd is visible before TRANSTAB can be written */ 105052f325f4SRobin Murphy wmb(); 105152f325f4SRobin Murphy 105252f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | 1053d08d42deSRob Herring ARM_MALI_LPAE_TTBR_READ_INNER | 1054d08d42deSRob Herring ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; 105552f325f4SRobin Murphy return &data->iop; 1056d08d42deSRob Herring 105752f325f4SRobin Murphy out_free_data: 105852f325f4SRobin Murphy kfree(data); 105952f325f4SRobin Murphy return NULL; 1060d08d42deSRob Herring } 1061d08d42deSRob Herring 1062e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1063e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 1064e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1065e1d3c0fdSWill Deacon }; 1066e1d3c0fdSWill Deacon 1067e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1068e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 1069e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1070e1d3c0fdSWill Deacon }; 1071e1d3c0fdSWill Deacon 1072e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1073e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1074e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1075e1d3c0fdSWill Deacon }; 1076e1d3c0fdSWill Deacon 1077e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1078e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1079e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1080e1d3c0fdSWill Deacon }; 1081fe4b991dSWill Deacon 1082d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1083d08d42deSRob Herring .alloc = arm_mali_lpae_alloc_pgtable, 1084d08d42deSRob Herring .free = arm_lpae_free_pgtable, 1085d08d42deSRob Herring }; 1086d08d42deSRob Herring 1087fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1088fe4b991dSWill Deacon 1089b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata; 1090fe4b991dSWill Deacon 1091b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie) 1092fe4b991dSWill Deacon { 1093fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1094fe4b991dSWill Deacon } 1095fe4b991dSWill Deacon 1096b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size, 1097b5813c16SRobin Murphy size_t granule, void *cookie) 1098fe4b991dSWill Deacon { 1099fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1100fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1101fe4b991dSWill Deacon } 1102fe4b991dSWill Deacon 1103b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, 1104b5813c16SRobin Murphy unsigned long iova, size_t granule, 1105b5813c16SRobin Murphy void *cookie) 110610b7a7d9SWill Deacon { 1107abfd6fe0SWill Deacon dummy_tlb_flush(iova, granule, granule, cookie); 110810b7a7d9SWill Deacon } 110910b7a7d9SWill Deacon 1110298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = { 1111fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 111210b7a7d9SWill Deacon .tlb_flush_walk = dummy_tlb_flush, 111310b7a7d9SWill Deacon .tlb_flush_leaf = dummy_tlb_flush, 1114abfd6fe0SWill Deacon .tlb_add_page = dummy_tlb_add_page, 1115fe4b991dSWill Deacon }; 1116fe4b991dSWill Deacon 1117fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1118fe4b991dSWill Deacon { 1119fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1120fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1121fe4b991dSWill Deacon 1122fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1123fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 11245fb190b0SRobin Murphy pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", 1125c79278c1SRobin Murphy ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), 11265fb190b0SRobin Murphy ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); 1127fe4b991dSWill Deacon } 1128fe4b991dSWill Deacon 1129fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1130fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1131fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1132fe4b991dSWill Deacon selftest_running = false; \ 1133fe4b991dSWill Deacon -EFAULT; \ 1134fe4b991dSWill Deacon }) 1135fe4b991dSWill Deacon 1136fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1137fe4b991dSWill Deacon { 11389062c1d0SChristophe JAILLET static const enum io_pgtable_fmt fmts[] __initconst = { 1139fe4b991dSWill Deacon ARM_64_LPAE_S1, 1140fe4b991dSWill Deacon ARM_64_LPAE_S2, 1141fe4b991dSWill Deacon }; 1142fe4b991dSWill Deacon 1143fe4b991dSWill Deacon int i, j; 1144fe4b991dSWill Deacon unsigned long iova; 1145fe4b991dSWill Deacon size_t size; 1146fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1147fe4b991dSWill Deacon 1148fe4b991dSWill Deacon selftest_running = true; 1149fe4b991dSWill Deacon 1150fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1151fe4b991dSWill Deacon cfg_cookie = cfg; 1152fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1153fe4b991dSWill Deacon if (!ops) { 1154fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1155fe4b991dSWill Deacon return -ENOMEM; 1156fe4b991dSWill Deacon } 1157fe4b991dSWill Deacon 1158fe4b991dSWill Deacon /* 1159fe4b991dSWill Deacon * Initial sanity checks. 1160fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1161fe4b991dSWill Deacon */ 1162fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1163fe4b991dSWill Deacon return __FAIL(ops, i); 1164fe4b991dSWill Deacon 1165fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1166fe4b991dSWill Deacon return __FAIL(ops, i); 1167fe4b991dSWill Deacon 1168fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1169fe4b991dSWill Deacon return __FAIL(ops, i); 1170fe4b991dSWill Deacon 1171fe4b991dSWill Deacon /* 1172fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1173fe4b991dSWill Deacon */ 1174fe4b991dSWill Deacon iova = 0; 11754ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1176fe4b991dSWill Deacon size = 1UL << j; 1177fe4b991dSWill Deacon 1178fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1179fe4b991dSWill Deacon IOMMU_WRITE | 1180fe4b991dSWill Deacon IOMMU_NOEXEC | 1181fe4b991dSWill Deacon IOMMU_CACHE)) 1182fe4b991dSWill Deacon return __FAIL(ops, i); 1183fe4b991dSWill Deacon 1184fe4b991dSWill Deacon /* Overlapping mappings */ 1185fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1186fe4b991dSWill Deacon IOMMU_READ | IOMMU_NOEXEC)) 1187fe4b991dSWill Deacon return __FAIL(ops, i); 1188fe4b991dSWill Deacon 1189fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1190fe4b991dSWill Deacon return __FAIL(ops, i); 1191fe4b991dSWill Deacon 1192fe4b991dSWill Deacon iova += SZ_1G; 1193fe4b991dSWill Deacon } 1194fe4b991dSWill Deacon 1195fe4b991dSWill Deacon /* Partial unmap */ 1196fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1197a2d3a382SWill Deacon if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) 1198fe4b991dSWill Deacon return __FAIL(ops, i); 1199fe4b991dSWill Deacon 1200fe4b991dSWill Deacon /* Remap of partial unmap */ 1201fe4b991dSWill Deacon if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 1202fe4b991dSWill Deacon return __FAIL(ops, i); 1203fe4b991dSWill Deacon 1204fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1205fe4b991dSWill Deacon return __FAIL(ops, i); 1206fe4b991dSWill Deacon 1207fe4b991dSWill Deacon /* Full unmap */ 1208fe4b991dSWill Deacon iova = 0; 1209f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1210fe4b991dSWill Deacon size = 1UL << j; 1211fe4b991dSWill Deacon 1212a2d3a382SWill Deacon if (ops->unmap(ops, iova, size, NULL) != size) 1213fe4b991dSWill Deacon return __FAIL(ops, i); 1214fe4b991dSWill Deacon 1215fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1216fe4b991dSWill Deacon return __FAIL(ops, i); 1217fe4b991dSWill Deacon 1218fe4b991dSWill Deacon /* Remap full block */ 1219fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 1220fe4b991dSWill Deacon return __FAIL(ops, i); 1221fe4b991dSWill Deacon 1222fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1223fe4b991dSWill Deacon return __FAIL(ops, i); 1224fe4b991dSWill Deacon 1225fe4b991dSWill Deacon iova += SZ_1G; 1226fe4b991dSWill Deacon } 1227fe4b991dSWill Deacon 1228fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1229fe4b991dSWill Deacon } 1230fe4b991dSWill Deacon 1231fe4b991dSWill Deacon selftest_running = false; 1232fe4b991dSWill Deacon return 0; 1233fe4b991dSWill Deacon } 1234fe4b991dSWill Deacon 1235fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1236fe4b991dSWill Deacon { 12379062c1d0SChristophe JAILLET static const unsigned long pgsize[] __initconst = { 1238fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1239fe4b991dSWill Deacon SZ_16K | SZ_32M, 1240fe4b991dSWill Deacon SZ_64K | SZ_512M, 1241fe4b991dSWill Deacon }; 1242fe4b991dSWill Deacon 12439062c1d0SChristophe JAILLET static const unsigned int ias[] __initconst = { 1244fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1245fe4b991dSWill Deacon }; 1246fe4b991dSWill Deacon 1247fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1248fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1249fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1250fe4b991dSWill Deacon .oas = 48, 12514f41845bSWill Deacon .coherent_walk = true, 1252fe4b991dSWill Deacon }; 1253fe4b991dSWill Deacon 1254fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1255fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1256fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1257fe4b991dSWill Deacon cfg.ias = ias[j]; 1258fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1259fe4b991dSWill Deacon pgsize[i], ias[j]); 1260fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1261fe4b991dSWill Deacon fail++; 1262fe4b991dSWill Deacon else 1263fe4b991dSWill Deacon pass++; 1264fe4b991dSWill Deacon } 1265fe4b991dSWill Deacon } 1266fe4b991dSWill Deacon 1267fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1268fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1269fe4b991dSWill Deacon } 1270fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1271fe4b991dSWill Deacon #endif 1272