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 237cef39ddSJean-Philippe Brucker #include "io-pgtable-arm.h" 247cef39ddSJean-Philippe Brucker 256c89928fSRobin Murphy #define ARM_LPAE_MAX_ADDR_BITS 52 26e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 27e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS 4 28e1d3c0fdSWill Deacon 29e1d3c0fdSWill Deacon /* Struct accessors */ 30e1d3c0fdSWill Deacon #define io_pgtable_to_data(x) \ 31e1d3c0fdSWill Deacon container_of((x), struct arm_lpae_io_pgtable, iop) 32e1d3c0fdSWill Deacon 33e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x) \ 34e1d3c0fdSWill Deacon io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 35e1d3c0fdSWill Deacon 36e1d3c0fdSWill Deacon /* 37e1d3c0fdSWill Deacon * Calculate the right shift amount to get to the portion describing level l 38e1d3c0fdSWill Deacon * in a virtual address mapped by the pagetable in d. 39e1d3c0fdSWill Deacon */ 40e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d) \ 415fb190b0SRobin Murphy (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \ 425fb190b0SRobin Murphy ilog2(sizeof(arm_lpae_iopte))) 43e1d3c0fdSWill Deacon 445fb190b0SRobin Murphy #define ARM_LPAE_GRANULE(d) \ 455fb190b0SRobin Murphy (sizeof(arm_lpae_iopte) << (d)->bits_per_level) 46c79278c1SRobin Murphy #define ARM_LPAE_PGD_SIZE(d) \ 47c79278c1SRobin Murphy (sizeof(arm_lpae_iopte) << (d)->pgd_bits) 48e1d3c0fdSWill Deacon 491fe27be5SIsaac J. Manjarres #define ARM_LPAE_PTES_PER_TABLE(d) \ 501fe27be5SIsaac J. Manjarres (ARM_LPAE_GRANULE(d) >> ilog2(sizeof(arm_lpae_iopte))) 511fe27be5SIsaac J. Manjarres 52e1d3c0fdSWill Deacon /* 53e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 54e1d3c0fdSWill Deacon * pagetable in d. 55e1d3c0fdSWill Deacon */ 56e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 57c79278c1SRobin Murphy ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0) 58e1d3c0fdSWill Deacon 59e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 60367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 61e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 62e1d3c0fdSWill Deacon 63e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 645fb190b0SRobin Murphy #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d)) 65e1d3c0fdSWill Deacon 66e1d3c0fdSWill Deacon /* Page table bits */ 67e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 68e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 69e1d3c0fdSWill Deacon 70e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 71e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 72e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 73e1d3c0fdSWill Deacon 746c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) 756c89928fSRobin Murphy 76c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 77e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 78e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 79e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 80e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 81e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 82c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 84e1d3c0fdSWill Deacon 85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 86e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 87e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 88e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 89e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 902c3d273eSRobin Murphy /* Software bit for solving coherency races */ 912c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) 92e1d3c0fdSWill Deacon 93e1d3c0fdSWill Deacon /* Stage-1 PTE */ 94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 98e1d3c0fdSWill Deacon 99e1d3c0fdSWill Deacon /* Stage-2 PTE */ 100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 102e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 103e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 104e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 105e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 106e1d3c0fdSWill Deacon 107e1d3c0fdSWill Deacon /* Register bits */ 108fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_SL0_MASK 0x3 109e1d3c0fdSWill Deacon 110e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 111e1d3c0fdSWill Deacon 112fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_SHIFT 16 113fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_MASK 0x7 114e1d3c0fdSWill Deacon 115e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 116e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 117e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 118e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 11990ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 120e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 121e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 122e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 123e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 12490ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 125e1d3c0fdSWill Deacon 126d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) 127d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) 128d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) 129d08d42deSRob Herring 13052f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL 13152f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL 13252f325f4SRobin Murphy 133892384cdSSven Peter #define APPLE_DART_PTE_PROT_NO_WRITE (1<<7) 134892384cdSSven Peter #define APPLE_DART_PTE_PROT_NO_READ (1<<8) 135892384cdSSven Peter 136e1d3c0fdSWill Deacon /* IOPTE accessors */ 1376c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 138e1d3c0fdSWill Deacon 139f37eb484SKunkun Jiang #define iopte_type(pte) \ 140e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 141e1d3c0fdSWill Deacon 142e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 143e1d3c0fdSWill Deacon 144e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 145e1d3c0fdSWill Deacon struct io_pgtable iop; 146e1d3c0fdSWill Deacon 147c79278c1SRobin Murphy int pgd_bits; 148594ab90fSRobin Murphy int start_level; 1495fb190b0SRobin Murphy int bits_per_level; 150e1d3c0fdSWill Deacon 151e1d3c0fdSWill Deacon void *pgd; 152e1d3c0fdSWill Deacon }; 153e1d3c0fdSWill Deacon 154e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 155e1d3c0fdSWill Deacon 156d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, 157d08d42deSRob Herring enum io_pgtable_fmt fmt) 158d08d42deSRob Herring { 159d08d42deSRob Herring if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) 160f37eb484SKunkun Jiang return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE; 161d08d42deSRob Herring 162f37eb484SKunkun Jiang return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK; 163d08d42deSRob Herring } 164d08d42deSRob Herring 1656c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, 1666c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1676c89928fSRobin Murphy { 1686c89928fSRobin Murphy arm_lpae_iopte pte = paddr; 1696c89928fSRobin Murphy 1706c89928fSRobin Murphy /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ 1716c89928fSRobin Murphy return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; 1726c89928fSRobin Murphy } 1736c89928fSRobin Murphy 1746c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, 1756c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1766c89928fSRobin Murphy { 17778688059SRobin Murphy u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; 1786c89928fSRobin Murphy 1795fb190b0SRobin Murphy if (ARM_LPAE_GRANULE(data) < SZ_64K) 1806c89928fSRobin Murphy return paddr; 1816c89928fSRobin Murphy 1826c89928fSRobin Murphy /* Rotate the packed high-order bits back to the top */ 1836c89928fSRobin Murphy return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); 1846c89928fSRobin Murphy } 1856c89928fSRobin Murphy 186fe4b991dSWill Deacon static bool selftest_running = false; 187fe4b991dSWill Deacon 188ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 189f8d54961SRobin Murphy { 190ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 191f8d54961SRobin Murphy } 192f8d54961SRobin Murphy 193f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 194f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 195f8d54961SRobin Murphy { 196f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 1974b123757SRobin Murphy int order = get_order(size); 1984b123757SRobin Murphy struct page *p; 199f8d54961SRobin Murphy dma_addr_t dma; 2004b123757SRobin Murphy void *pages; 201f8d54961SRobin Murphy 2024b123757SRobin Murphy VM_BUG_ON((gfp & __GFP_HIGHMEM)); 203fac83d29SJean-Philippe Brucker p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 204fac83d29SJean-Philippe Brucker gfp | __GFP_ZERO, order); 2054b123757SRobin Murphy if (!p) 206f8d54961SRobin Murphy return NULL; 207f8d54961SRobin Murphy 2084b123757SRobin Murphy pages = page_address(p); 2094f41845bSWill Deacon if (!cfg->coherent_walk) { 210f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 211f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 212f8d54961SRobin Murphy goto out_free; 213f8d54961SRobin Murphy /* 214f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 215ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 216ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 217f8d54961SRobin Murphy */ 218ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 219f8d54961SRobin Murphy goto out_unmap; 220f8d54961SRobin Murphy } 221f8d54961SRobin Murphy 222f8d54961SRobin Murphy return pages; 223f8d54961SRobin Murphy 224f8d54961SRobin Murphy out_unmap: 225f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 226f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 227f8d54961SRobin Murphy out_free: 2284b123757SRobin Murphy __free_pages(p, order); 229f8d54961SRobin Murphy return NULL; 230f8d54961SRobin Murphy } 231f8d54961SRobin Murphy 232f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 233f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 234f8d54961SRobin Murphy { 2354f41845bSWill Deacon if (!cfg->coherent_walk) 236ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 237f8d54961SRobin Murphy size, DMA_TO_DEVICE); 2384b123757SRobin Murphy free_pages((unsigned long)pages, get_order(size)); 239f8d54961SRobin Murphy } 240f8d54961SRobin Murphy 24141e1eb25SIsaac J. Manjarres static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries, 2422c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2432c3d273eSRobin Murphy { 2442c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 24541e1eb25SIsaac J. Manjarres sizeof(*ptep) * num_entries, DMA_TO_DEVICE); 2462c3d273eSRobin Murphy } 2472c3d273eSRobin Murphy 2481fe27be5SIsaac J. Manjarres static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg) 249f8d54961SRobin Murphy { 25041e1eb25SIsaac J. Manjarres 2511fe27be5SIsaac J. Manjarres *ptep = 0; 252f8d54961SRobin Murphy 2534f41845bSWill Deacon if (!cfg->coherent_walk) 2541fe27be5SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, 1, cfg); 255f8d54961SRobin Murphy } 256f8d54961SRobin Murphy 257193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 2583951c41aSWill Deacon struct iommu_iotlb_gather *gather, 2591fe27be5SIsaac J. Manjarres unsigned long iova, size_t size, size_t pgcount, 2601fe27be5SIsaac J. Manjarres int lvl, arm_lpae_iopte *ptep); 261cf27ec93SWill Deacon 262fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 263fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 26441e1eb25SIsaac J. Manjarres int lvl, int num_entries, arm_lpae_iopte *ptep) 265fb3a9579SRobin Murphy { 266fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 26741e1eb25SIsaac J. Manjarres struct io_pgtable_cfg *cfg = &data->iop.cfg; 26841e1eb25SIsaac J. Manjarres size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 26941e1eb25SIsaac J. Manjarres int i; 270fb3a9579SRobin Murphy 271d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) 272fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 273fb3a9579SRobin Murphy else 274fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 275fb3a9579SRobin Murphy 27641e1eb25SIsaac J. Manjarres for (i = 0; i < num_entries; i++) 27741e1eb25SIsaac J. Manjarres ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data); 278fb3a9579SRobin Murphy 27941e1eb25SIsaac J. Manjarres if (!cfg->coherent_walk) 28041e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, num_entries, cfg); 281fb3a9579SRobin Murphy } 282fb3a9579SRobin Murphy 283e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 284e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 28541e1eb25SIsaac J. Manjarres arm_lpae_iopte prot, int lvl, int num_entries, 286e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 287e1d3c0fdSWill Deacon { 28841e1eb25SIsaac J. Manjarres int i; 289e1d3c0fdSWill Deacon 29041e1eb25SIsaac J. Manjarres for (i = 0; i < num_entries; i++) 29141e1eb25SIsaac J. Manjarres if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) { 292cf27ec93SWill Deacon /* We require an unmap first */ 293fe4b991dSWill Deacon WARN_ON(!selftest_running); 294e1d3c0fdSWill Deacon return -EEXIST; 29541e1eb25SIsaac J. Manjarres } else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) { 296cf27ec93SWill Deacon /* 297cf27ec93SWill Deacon * We need to unmap and free the old table before 298cf27ec93SWill Deacon * overwriting it with a block entry. 299cf27ec93SWill Deacon */ 300cf27ec93SWill Deacon arm_lpae_iopte *tblp; 301cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 302cf27ec93SWill Deacon 303cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 3041fe27be5SIsaac J. Manjarres if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1, 30541e1eb25SIsaac J. Manjarres lvl, tblp) != sz) { 3063951c41aSWill Deacon WARN_ON(1); 307cf27ec93SWill Deacon return -EINVAL; 308fe4b991dSWill Deacon } 3093951c41aSWill Deacon } 310e1d3c0fdSWill Deacon 31141e1eb25SIsaac J. Manjarres __arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep); 312e1d3c0fdSWill Deacon return 0; 313e1d3c0fdSWill Deacon } 314e1d3c0fdSWill Deacon 315fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 316fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3172c3d273eSRobin Murphy arm_lpae_iopte curr, 318*9abe2ac8SHector Martin struct arm_lpae_io_pgtable *data) 319fb3a9579SRobin Murphy { 3202c3d273eSRobin Murphy arm_lpae_iopte old, new; 321*9abe2ac8SHector Martin struct io_pgtable_cfg *cfg = &data->iop.cfg; 322fb3a9579SRobin Murphy 323*9abe2ac8SHector Martin new = paddr_to_iopte(__pa(table), data) | ARM_LPAE_PTE_TYPE_TABLE; 324fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 325fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 326fb3a9579SRobin Murphy 32777f34458SWill Deacon /* 32877f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 32977f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 33077f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 33177f34458SWill Deacon */ 33277f34458SWill Deacon dma_wmb(); 3332c3d273eSRobin Murphy 3342c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3352c3d273eSRobin Murphy 3364f41845bSWill Deacon if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) 3372c3d273eSRobin Murphy return old; 3382c3d273eSRobin Murphy 3392c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 34041e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, 1, cfg); 3412c3d273eSRobin Murphy if (old == curr) 3422c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3432c3d273eSRobin Murphy 3442c3d273eSRobin Murphy return old; 345fb3a9579SRobin Murphy } 346fb3a9579SRobin Murphy 347e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 3484a77b12dSIsaac J. Manjarres phys_addr_t paddr, size_t size, size_t pgcount, 3494a77b12dSIsaac J. Manjarres arm_lpae_iopte prot, int lvl, arm_lpae_iopte *ptep, 3504a77b12dSIsaac J. Manjarres gfp_t gfp, size_t *mapped) 351e1d3c0fdSWill Deacon { 352e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 353e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3542c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 355f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 3564a77b12dSIsaac J. Manjarres int ret = 0, num_entries, max_entries, map_idx_start; 357e1d3c0fdSWill Deacon 358e1d3c0fdSWill Deacon /* Find our entry at the current level */ 3594a77b12dSIsaac J. Manjarres map_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data); 3604a77b12dSIsaac J. Manjarres ptep += map_idx_start; 361e1d3c0fdSWill Deacon 362e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 3634a77b12dSIsaac J. Manjarres if (size == block_size) { 3644a77b12dSIsaac J. Manjarres max_entries = ARM_LPAE_PTES_PER_TABLE(data) - map_idx_start; 3654a77b12dSIsaac J. Manjarres num_entries = min_t(int, pgcount, max_entries); 3664a77b12dSIsaac J. Manjarres ret = arm_lpae_init_pte(data, iova, paddr, prot, lvl, num_entries, ptep); 3674a77b12dSIsaac J. Manjarres if (!ret && mapped) 3684a77b12dSIsaac J. Manjarres *mapped += num_entries * size; 3694a77b12dSIsaac J. Manjarres 3704a77b12dSIsaac J. Manjarres return ret; 3714a77b12dSIsaac J. Manjarres } 372e1d3c0fdSWill Deacon 373e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 374e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 375e1d3c0fdSWill Deacon return -EINVAL; 376e1d3c0fdSWill Deacon 377e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3782c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 379e1d3c0fdSWill Deacon if (!pte) { 380f34ce7a7SBaolin Wang cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg); 381e1d3c0fdSWill Deacon if (!cptep) 382e1d3c0fdSWill Deacon return -ENOMEM; 383e1d3c0fdSWill Deacon 384*9abe2ac8SHector Martin pte = arm_lpae_install_table(cptep, ptep, 0, data); 3852c3d273eSRobin Murphy if (pte) 3862c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 3874f41845bSWill Deacon } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 38841e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, 1, cfg); 3892c3d273eSRobin Murphy } 3902c3d273eSRobin Murphy 391d08d42deSRob Herring if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { 392e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 3932c3d273eSRobin Murphy } else if (pte) { 394ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 395ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 396ed46e66cSOleksandr Tyshchenko return -EEXIST; 397e1d3c0fdSWill Deacon } 398e1d3c0fdSWill Deacon 399e1d3c0fdSWill Deacon /* Rinse, repeat */ 4004a77b12dSIsaac J. Manjarres return __arm_lpae_map(data, iova, paddr, size, pgcount, prot, lvl + 1, 4014a77b12dSIsaac J. Manjarres cptep, gfp, mapped); 402e1d3c0fdSWill Deacon } 403e1d3c0fdSWill Deacon 404e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 405e1d3c0fdSWill Deacon int prot) 406e1d3c0fdSWill Deacon { 407e1d3c0fdSWill Deacon arm_lpae_iopte pte; 408e1d3c0fdSWill Deacon 409892384cdSSven Peter if (data->iop.fmt == APPLE_DART) { 410892384cdSSven Peter pte = 0; 411892384cdSSven Peter if (!(prot & IOMMU_WRITE)) 412892384cdSSven Peter pte |= APPLE_DART_PTE_PROT_NO_WRITE; 413892384cdSSven Peter if (!(prot & IOMMU_READ)) 414892384cdSSven Peter pte |= APPLE_DART_PTE_PROT_NO_READ; 415892384cdSSven Peter return pte; 416892384cdSSven Peter } 417892384cdSSven Peter 418e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 419e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 420e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 421e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 422e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 423e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 424e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 425e1d3c0fdSWill Deacon } else { 426e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 427e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 428e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 429e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 430e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 431d08d42deSRob Herring } 432d08d42deSRob Herring 433d08d42deSRob Herring /* 434d08d42deSRob Herring * Note that this logic is structured to accommodate Mali LPAE 435d08d42deSRob Herring * having stage-1-like attributes but stage-2-like permissions. 436d08d42deSRob Herring */ 437d08d42deSRob Herring if (data->iop.fmt == ARM_64_LPAE_S2 || 438d08d42deSRob Herring data->iop.fmt == ARM_32_LPAE_S2) { 439fb948251SRobin Murphy if (prot & IOMMU_MMIO) 440fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 441fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 442e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 443e1d3c0fdSWill Deacon else 444e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 445d08d42deSRob Herring } else { 446d08d42deSRob Herring if (prot & IOMMU_MMIO) 447d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 448d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 449d08d42deSRob Herring else if (prot & IOMMU_CACHE) 450d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 451d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 452e1d3c0fdSWill Deacon } 453e1d3c0fdSWill Deacon 454728da60dSRobin Murphy /* 455728da60dSRobin Murphy * Also Mali has its own notions of shareability wherein its Inner 456728da60dSRobin Murphy * domain covers the cores within the GPU, and its Outer domain is 457728da60dSRobin Murphy * "outside the GPU" (i.e. either the Inner or System domain in CPU 458728da60dSRobin Murphy * terms, depending on coherency). 459728da60dSRobin Murphy */ 460728da60dSRobin Murphy if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE) 4617618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_IS; 4627618e479SRobin Murphy else 4637618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_OS; 4647618e479SRobin Murphy 465e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 466e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 467e1d3c0fdSWill Deacon 4687618e479SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 4697618e479SRobin Murphy pte |= ARM_LPAE_PTE_NS; 4707618e479SRobin Murphy 4717618e479SRobin Murphy if (data->iop.fmt != ARM_MALI_LPAE) 4727618e479SRobin Murphy pte |= ARM_LPAE_PTE_AF; 4737618e479SRobin Murphy 474e1d3c0fdSWill Deacon return pte; 475e1d3c0fdSWill Deacon } 476e1d3c0fdSWill Deacon 4774a77b12dSIsaac J. Manjarres static int arm_lpae_map_pages(struct io_pgtable_ops *ops, unsigned long iova, 4784a77b12dSIsaac J. Manjarres phys_addr_t paddr, size_t pgsize, size_t pgcount, 4794a77b12dSIsaac J. Manjarres int iommu_prot, gfp_t gfp, size_t *mapped) 480e1d3c0fdSWill Deacon { 481e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 482f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 483e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 484594ab90fSRobin Murphy int ret, lvl = data->start_level; 485e1d3c0fdSWill Deacon arm_lpae_iopte prot; 48608090744SRobin Murphy long iaext = (s64)iova >> cfg->ias; 487e1d3c0fdSWill Deacon 4884a77b12dSIsaac J. Manjarres if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize)) 489f7b90d2cSRobin Murphy return -EINVAL; 490f7b90d2cSRobin Murphy 491db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 492db690301SRobin Murphy iaext = ~iaext; 493db690301SRobin Murphy if (WARN_ON(iaext || paddr >> cfg->oas)) 49476557391SRobin Murphy return -ERANGE; 49576557391SRobin Murphy 496f12e0d22SKeqian Zhu /* If no access, then nothing to do */ 497f12e0d22SKeqian Zhu if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 498f12e0d22SKeqian Zhu return 0; 499f12e0d22SKeqian Zhu 500e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 5014a77b12dSIsaac J. Manjarres ret = __arm_lpae_map(data, iova, paddr, pgsize, pgcount, prot, lvl, 5024a77b12dSIsaac J. Manjarres ptep, gfp, mapped); 50387a91b15SRobin Murphy /* 50487a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 50587a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 50687a91b15SRobin Murphy */ 50787a91b15SRobin Murphy wmb(); 50887a91b15SRobin Murphy 50987a91b15SRobin Murphy return ret; 510e1d3c0fdSWill Deacon } 511e1d3c0fdSWill Deacon 5124a77b12dSIsaac J. Manjarres static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 5134a77b12dSIsaac J. Manjarres phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp) 5144a77b12dSIsaac J. Manjarres { 5154a77b12dSIsaac J. Manjarres return arm_lpae_map_pages(ops, iova, paddr, size, 1, iommu_prot, gfp, 5164a77b12dSIsaac J. Manjarres NULL); 5174a77b12dSIsaac J. Manjarres } 5184a77b12dSIsaac J. Manjarres 519e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 520e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 521e1d3c0fdSWill Deacon { 522e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 523e1d3c0fdSWill Deacon unsigned long table_size; 524e1d3c0fdSWill Deacon 525594ab90fSRobin Murphy if (lvl == data->start_level) 526c79278c1SRobin Murphy table_size = ARM_LPAE_PGD_SIZE(data); 527e1d3c0fdSWill Deacon else 52806c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 529e1d3c0fdSWill Deacon 530e1d3c0fdSWill Deacon start = ptep; 53112c2ab09SWill Deacon 53212c2ab09SWill Deacon /* Only leaf entries at the last level */ 53312c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 53412c2ab09SWill Deacon end = ptep; 53512c2ab09SWill Deacon else 536e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 537e1d3c0fdSWill Deacon 538e1d3c0fdSWill Deacon while (ptep != end) { 539e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 540e1d3c0fdSWill Deacon 541d08d42deSRob Herring if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) 542e1d3c0fdSWill Deacon continue; 543e1d3c0fdSWill Deacon 544e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 545e1d3c0fdSWill Deacon } 546e1d3c0fdSWill Deacon 547f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 548e1d3c0fdSWill Deacon } 549e1d3c0fdSWill Deacon 550e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 551e1d3c0fdSWill Deacon { 552e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 553e1d3c0fdSWill Deacon 554594ab90fSRobin Murphy __arm_lpae_free_pgtable(data, data->start_level, data->pgd); 555e1d3c0fdSWill Deacon kfree(data); 556e1d3c0fdSWill Deacon } 557e1d3c0fdSWill Deacon 558193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 5593951c41aSWill Deacon struct iommu_iotlb_gather *gather, 560e1d3c0fdSWill Deacon unsigned long iova, size_t size, 561fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 5621fe27be5SIsaac J. Manjarres arm_lpae_iopte *ptep, size_t pgcount) 563e1d3c0fdSWill Deacon { 564fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 565fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 566e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 567fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 568fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 5691fe27be5SIsaac J. Manjarres int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data); 5701fe27be5SIsaac J. Manjarres int i, unmap_idx_start = -1, num_entries = 0, max_entries; 571e1d3c0fdSWill Deacon 572fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 573fb3a9579SRobin Murphy return 0; 574e1d3c0fdSWill Deacon 575fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 576fb3a9579SRobin Murphy if (!tablep) 577fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 578e1d3c0fdSWill Deacon 5791fe27be5SIsaac J. Manjarres if (size == split_sz) { 5801fe27be5SIsaac J. Manjarres unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data); 5811fe27be5SIsaac J. Manjarres max_entries = ptes_per_table - unmap_idx_start; 5821fe27be5SIsaac J. Manjarres num_entries = min_t(int, pgcount, max_entries); 5831fe27be5SIsaac J. Manjarres } 584fb3a9579SRobin Murphy 5856c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 586fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 587fb3a9579SRobin Murphy 5881fe27be5SIsaac J. Manjarres for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) { 589e1d3c0fdSWill Deacon /* Unmap! */ 5901fe27be5SIsaac J. Manjarres if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries)) 591e1d3c0fdSWill Deacon continue; 592e1d3c0fdSWill Deacon 59341e1eb25SIsaac J. Manjarres __arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]); 594e1d3c0fdSWill Deacon } 595e1d3c0fdSWill Deacon 596*9abe2ac8SHector Martin pte = arm_lpae_install_table(tablep, ptep, blk_pte, data); 5972c3d273eSRobin Murphy if (pte != blk_pte) { 5982c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5992c3d273eSRobin Murphy /* 6002c3d273eSRobin Murphy * We may race against someone unmapping another part of this 6012c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 6022c3d273eSRobin Murphy * a page entry here since we're never at the last level. 6032c3d273eSRobin Murphy */ 604f37eb484SKunkun Jiang if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE) 6052c3d273eSRobin Murphy return 0; 6062c3d273eSRobin Murphy 6072c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 6081fe27be5SIsaac J. Manjarres } else if (unmap_idx_start >= 0) { 6091fe27be5SIsaac J. Manjarres for (i = 0; i < num_entries; i++) 6101fe27be5SIsaac J. Manjarres io_pgtable_tlb_add_page(&data->iop, gather, iova + i * size, size); 6111fe27be5SIsaac J. Manjarres 6121fe27be5SIsaac J. Manjarres return num_entries * size; 613e1d3c0fdSWill Deacon } 614e1d3c0fdSWill Deacon 6151fe27be5SIsaac J. Manjarres return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep); 61685c7a0f1SRobin Murphy } 61785c7a0f1SRobin Murphy 618193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 6193951c41aSWill Deacon struct iommu_iotlb_gather *gather, 6201fe27be5SIsaac J. Manjarres unsigned long iova, size_t size, size_t pgcount, 6211fe27be5SIsaac J. Manjarres int lvl, arm_lpae_iopte *ptep) 622e1d3c0fdSWill Deacon { 623e1d3c0fdSWill Deacon arm_lpae_iopte pte; 624507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 6251fe27be5SIsaac J. Manjarres int i = 0, num_entries, max_entries, unmap_idx_start; 626e1d3c0fdSWill Deacon 6272eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 6282eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 6292eb97c78SRobin Murphy return 0; 6302eb97c78SRobin Murphy 6311fe27be5SIsaac J. Manjarres unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data); 6321fe27be5SIsaac J. Manjarres ptep += unmap_idx_start; 6332c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 6342eb97c78SRobin Murphy if (WARN_ON(!pte)) 635e1d3c0fdSWill Deacon return 0; 636e1d3c0fdSWill Deacon 637e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 638fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 6391fe27be5SIsaac J. Manjarres max_entries = ARM_LPAE_PTES_PER_TABLE(data) - unmap_idx_start; 6401fe27be5SIsaac J. Manjarres num_entries = min_t(int, pgcount, max_entries); 6411fe27be5SIsaac J. Manjarres 6421fe27be5SIsaac J. Manjarres while (i < num_entries) { 6431fe27be5SIsaac J. Manjarres pte = READ_ONCE(*ptep); 6441fe27be5SIsaac J. Manjarres if (WARN_ON(!pte)) 6451fe27be5SIsaac J. Manjarres break; 6461fe27be5SIsaac J. Manjarres 6471fe27be5SIsaac J. Manjarres __arm_lpae_clear_pte(ptep, &iop->cfg); 648e1d3c0fdSWill Deacon 649d08d42deSRob Herring if (!iopte_leaf(pte, lvl, iop->fmt)) { 650e1d3c0fdSWill Deacon /* Also flush any partial walks */ 6511fe27be5SIsaac J. Manjarres io_pgtable_tlb_flush_walk(iop, iova + i * size, size, 65210b7a7d9SWill Deacon ARM_LPAE_GRANULE(data)); 6531fe27be5SIsaac J. Manjarres __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 654f7403abfSRobin Murphy } else if (!iommu_iotlb_gather_queued(gather)) { 6551fe27be5SIsaac J. Manjarres io_pgtable_tlb_add_page(iop, gather, iova + i * size, size); 656e1d3c0fdSWill Deacon } 657e1d3c0fdSWill Deacon 6581fe27be5SIsaac J. Manjarres ptep++; 6591fe27be5SIsaac J. Manjarres i++; 6601fe27be5SIsaac J. Manjarres } 6611fe27be5SIsaac J. Manjarres 6621fe27be5SIsaac J. Manjarres return i * size; 663d08d42deSRob Herring } else if (iopte_leaf(pte, lvl, iop->fmt)) { 664e1d3c0fdSWill Deacon /* 665e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 666e1d3c0fdSWill Deacon * minus the part we want to unmap 667e1d3c0fdSWill Deacon */ 6683951c41aSWill Deacon return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, 6691fe27be5SIsaac J. Manjarres lvl + 1, ptep, pgcount); 670e1d3c0fdSWill Deacon } 671e1d3c0fdSWill Deacon 672e1d3c0fdSWill Deacon /* Keep on walkin' */ 673e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 6741fe27be5SIsaac J. Manjarres return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl + 1, ptep); 675e1d3c0fdSWill Deacon } 676e1d3c0fdSWill Deacon 6771fe27be5SIsaac J. Manjarres static size_t arm_lpae_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova, 6781fe27be5SIsaac J. Manjarres size_t pgsize, size_t pgcount, 6791fe27be5SIsaac J. Manjarres struct iommu_iotlb_gather *gather) 680e1d3c0fdSWill Deacon { 681e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 682f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 683e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 68408090744SRobin Murphy long iaext = (s64)iova >> cfg->ias; 685e1d3c0fdSWill Deacon 6861fe27be5SIsaac J. Manjarres if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount)) 687f7b90d2cSRobin Murphy return 0; 688f7b90d2cSRobin Murphy 689db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 690db690301SRobin Murphy iaext = ~iaext; 691db690301SRobin Murphy if (WARN_ON(iaext)) 69276557391SRobin Murphy return 0; 69376557391SRobin Murphy 6941fe27be5SIsaac J. Manjarres return __arm_lpae_unmap(data, gather, iova, pgsize, pgcount, 6951fe27be5SIsaac J. Manjarres data->start_level, ptep); 6961fe27be5SIsaac J. Manjarres } 6971fe27be5SIsaac J. Manjarres 6981fe27be5SIsaac J. Manjarres static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 6991fe27be5SIsaac J. Manjarres size_t size, struct iommu_iotlb_gather *gather) 7001fe27be5SIsaac J. Manjarres { 7011fe27be5SIsaac J. Manjarres return arm_lpae_unmap_pages(ops, iova, size, 1, gather); 702e1d3c0fdSWill Deacon } 703e1d3c0fdSWill Deacon 704e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 705e1d3c0fdSWill Deacon unsigned long iova) 706e1d3c0fdSWill Deacon { 707e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 708e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 709594ab90fSRobin Murphy int lvl = data->start_level; 710e1d3c0fdSWill Deacon 711e1d3c0fdSWill Deacon do { 712e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 713e1d3c0fdSWill Deacon if (!ptep) 714e1d3c0fdSWill Deacon return 0; 715e1d3c0fdSWill Deacon 716e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 7172c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 7182c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 719e1d3c0fdSWill Deacon 720e1d3c0fdSWill Deacon /* Valid entry? */ 721e1d3c0fdSWill Deacon if (!pte) 722e1d3c0fdSWill Deacon return 0; 723e1d3c0fdSWill Deacon 724e1d3c0fdSWill Deacon /* Leaf entry? */ 725d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) 726e1d3c0fdSWill Deacon goto found_translation; 727e1d3c0fdSWill Deacon 728e1d3c0fdSWill Deacon /* Take it to the next level */ 729e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 730e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 731e1d3c0fdSWill Deacon 732e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 733e1d3c0fdSWill Deacon return 0; 734e1d3c0fdSWill Deacon 735e1d3c0fdSWill Deacon found_translation: 7367c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 7376c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 738e1d3c0fdSWill Deacon } 739e1d3c0fdSWill Deacon 740e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 741e1d3c0fdSWill Deacon { 7426c89928fSRobin Murphy unsigned long granule, page_sizes; 7436c89928fSRobin Murphy unsigned int max_addr_bits = 48; 744e1d3c0fdSWill Deacon 745e1d3c0fdSWill Deacon /* 746e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 747e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 748e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 749e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 750e1d3c0fdSWill Deacon * chosen granule. 751e1d3c0fdSWill Deacon */ 752e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 753e1d3c0fdSWill Deacon granule = PAGE_SIZE; 754e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 755e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 756e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 757e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 758e1d3c0fdSWill Deacon else 759e1d3c0fdSWill Deacon granule = 0; 760e1d3c0fdSWill Deacon 761e1d3c0fdSWill Deacon switch (granule) { 762e1d3c0fdSWill Deacon case SZ_4K: 7636c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 764e1d3c0fdSWill Deacon break; 765e1d3c0fdSWill Deacon case SZ_16K: 7666c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 767e1d3c0fdSWill Deacon break; 768e1d3c0fdSWill Deacon case SZ_64K: 7696c89928fSRobin Murphy max_addr_bits = 52; 7706c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7716c89928fSRobin Murphy if (cfg->oas > 48) 7726c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 773e1d3c0fdSWill Deacon break; 774e1d3c0fdSWill Deacon default: 7756c89928fSRobin Murphy page_sizes = 0; 776e1d3c0fdSWill Deacon } 7776c89928fSRobin Murphy 7786c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7796c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7806c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 781e1d3c0fdSWill Deacon } 782e1d3c0fdSWill Deacon 783e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 784e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 785e1d3c0fdSWill Deacon { 786e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 7875fb190b0SRobin Murphy int levels, va_bits, pg_shift; 788e1d3c0fdSWill Deacon 789e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 790e1d3c0fdSWill Deacon 791e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 792e1d3c0fdSWill Deacon return NULL; 793e1d3c0fdSWill Deacon 794e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 795e1d3c0fdSWill Deacon return NULL; 796e1d3c0fdSWill Deacon 797e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 798e1d3c0fdSWill Deacon return NULL; 799e1d3c0fdSWill Deacon 800e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 801e1d3c0fdSWill Deacon if (!data) 802e1d3c0fdSWill Deacon return NULL; 803e1d3c0fdSWill Deacon 8045fb190b0SRobin Murphy pg_shift = __ffs(cfg->pgsize_bitmap); 8055fb190b0SRobin Murphy data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); 806e1d3c0fdSWill Deacon 8075fb190b0SRobin Murphy va_bits = cfg->ias - pg_shift; 808594ab90fSRobin Murphy levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 809594ab90fSRobin Murphy data->start_level = ARM_LPAE_MAX_LEVELS - levels; 810e1d3c0fdSWill Deacon 811e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 812c79278c1SRobin Murphy data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); 813e1d3c0fdSWill Deacon 814e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 815e1d3c0fdSWill Deacon .map = arm_lpae_map, 8164a77b12dSIsaac J. Manjarres .map_pages = arm_lpae_map_pages, 817e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 8181fe27be5SIsaac J. Manjarres .unmap_pages = arm_lpae_unmap_pages, 819e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 820e1d3c0fdSWill Deacon }; 821e1d3c0fdSWill Deacon 822e1d3c0fdSWill Deacon return data; 823e1d3c0fdSWill Deacon } 824e1d3c0fdSWill Deacon 825e1d3c0fdSWill Deacon static struct io_pgtable * 826e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 827e1d3c0fdSWill Deacon { 828e1d3c0fdSWill Deacon u64 reg; 8293850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 830fb485eb1SRobin Murphy typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; 831db690301SRobin Murphy bool tg1; 832e1d3c0fdSWill Deacon 8334f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | 834e67890c9SSai Prakash Ranjan IO_PGTABLE_QUIRK_ARM_TTBR1 | 835e67890c9SSai Prakash Ranjan IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)) 8363850db49SRobin Murphy return NULL; 8373850db49SRobin Murphy 8383850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 839e1d3c0fdSWill Deacon if (!data) 840e1d3c0fdSWill Deacon return NULL; 841e1d3c0fdSWill Deacon 842e1d3c0fdSWill Deacon /* TCR */ 8439e6ea59fSBjorn Andersson if (cfg->coherent_walk) { 844fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_IS; 845fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 846fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 847e67890c9SSai Prakash Ranjan if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA) 848e67890c9SSai Prakash Ranjan goto out_free_data; 8499e6ea59fSBjorn Andersson } else { 850fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_OS; 851fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_NC; 852e67890c9SSai Prakash Ranjan if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)) 853fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_NC; 854e67890c9SSai Prakash Ranjan else 855e67890c9SSai Prakash Ranjan tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 8569e6ea59fSBjorn Andersson } 857e1d3c0fdSWill Deacon 858db690301SRobin Murphy tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1; 85906c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 860e1d3c0fdSWill Deacon case SZ_4K: 861db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K; 862e1d3c0fdSWill Deacon break; 863e1d3c0fdSWill Deacon case SZ_16K: 864db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K; 865e1d3c0fdSWill Deacon break; 866e1d3c0fdSWill Deacon case SZ_64K: 867db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K; 868e1d3c0fdSWill Deacon break; 869e1d3c0fdSWill Deacon } 870e1d3c0fdSWill Deacon 871e1d3c0fdSWill Deacon switch (cfg->oas) { 872e1d3c0fdSWill Deacon case 32: 873fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_32_BIT; 874e1d3c0fdSWill Deacon break; 875e1d3c0fdSWill Deacon case 36: 876fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_36_BIT; 877e1d3c0fdSWill Deacon break; 878e1d3c0fdSWill Deacon case 40: 879fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_40_BIT; 880e1d3c0fdSWill Deacon break; 881e1d3c0fdSWill Deacon case 42: 882fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_42_BIT; 883e1d3c0fdSWill Deacon break; 884e1d3c0fdSWill Deacon case 44: 885fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_44_BIT; 886e1d3c0fdSWill Deacon break; 887e1d3c0fdSWill Deacon case 48: 888fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_48_BIT; 889e1d3c0fdSWill Deacon break; 8906c89928fSRobin Murphy case 52: 891fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_52_BIT; 8926c89928fSRobin Murphy break; 893e1d3c0fdSWill Deacon default: 894e1d3c0fdSWill Deacon goto out_free_data; 895e1d3c0fdSWill Deacon } 896e1d3c0fdSWill Deacon 897fb485eb1SRobin Murphy tcr->tsz = 64ULL - cfg->ias; 898e1d3c0fdSWill Deacon 899e1d3c0fdSWill Deacon /* MAIRs */ 900e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 901e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 902e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 903e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 904e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 90590ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | 90690ec7a76SVivek Gautam (ARM_LPAE_MAIR_ATTR_INC_OWBRWA 90790ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); 908e1d3c0fdSWill Deacon 909205577abSRobin Murphy cfg->arm_lpae_s1_cfg.mair = reg; 910e1d3c0fdSWill Deacon 911e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 912c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 913c79278c1SRobin Murphy GFP_KERNEL, cfg); 914e1d3c0fdSWill Deacon if (!data->pgd) 915e1d3c0fdSWill Deacon goto out_free_data; 916e1d3c0fdSWill Deacon 91787a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 91887a91b15SRobin Murphy wmb(); 919e1d3c0fdSWill Deacon 920d1e5f26fSRobin Murphy /* TTBR */ 921d1e5f26fSRobin Murphy cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); 922e1d3c0fdSWill Deacon return &data->iop; 923e1d3c0fdSWill Deacon 924e1d3c0fdSWill Deacon out_free_data: 925e1d3c0fdSWill Deacon kfree(data); 926e1d3c0fdSWill Deacon return NULL; 927e1d3c0fdSWill Deacon } 928e1d3c0fdSWill Deacon 929e1d3c0fdSWill Deacon static struct io_pgtable * 930e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 931e1d3c0fdSWill Deacon { 932ac4b80e5SWill Deacon u64 sl; 9333850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 934ac4b80e5SWill Deacon typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; 935e1d3c0fdSWill Deacon 9363850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 937a8e5f044SRobin Murphy if (cfg->quirks) 9383850db49SRobin Murphy return NULL; 9393850db49SRobin Murphy 9403850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 941e1d3c0fdSWill Deacon if (!data) 942e1d3c0fdSWill Deacon return NULL; 943e1d3c0fdSWill Deacon 944e1d3c0fdSWill Deacon /* 945e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 946e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 947e1d3c0fdSWill Deacon */ 948594ab90fSRobin Murphy if (data->start_level == 0) { 949e1d3c0fdSWill Deacon unsigned long pgd_pages; 950e1d3c0fdSWill Deacon 951c79278c1SRobin Murphy pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); 952e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 953c79278c1SRobin Murphy data->pgd_bits += data->bits_per_level; 954594ab90fSRobin Murphy data->start_level++; 955e1d3c0fdSWill Deacon } 956e1d3c0fdSWill Deacon } 957e1d3c0fdSWill Deacon 958e1d3c0fdSWill Deacon /* VTCR */ 95930d2acb6SWill Deacon if (cfg->coherent_walk) { 960ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_IS; 961ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 962ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 96330d2acb6SWill Deacon } else { 964ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_OS; 965ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_NC; 966ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_NC; 96730d2acb6SWill Deacon } 968e1d3c0fdSWill Deacon 969594ab90fSRobin Murphy sl = data->start_level; 970e1d3c0fdSWill Deacon 97106c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 972e1d3c0fdSWill Deacon case SZ_4K: 973ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_4K; 974e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 975e1d3c0fdSWill Deacon break; 976e1d3c0fdSWill Deacon case SZ_16K: 977ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_16K; 978e1d3c0fdSWill Deacon break; 979e1d3c0fdSWill Deacon case SZ_64K: 980ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_64K; 981e1d3c0fdSWill Deacon break; 982e1d3c0fdSWill Deacon } 983e1d3c0fdSWill Deacon 984e1d3c0fdSWill Deacon switch (cfg->oas) { 985e1d3c0fdSWill Deacon case 32: 986ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; 987e1d3c0fdSWill Deacon break; 988e1d3c0fdSWill Deacon case 36: 989ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; 990e1d3c0fdSWill Deacon break; 991e1d3c0fdSWill Deacon case 40: 992ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; 993e1d3c0fdSWill Deacon break; 994e1d3c0fdSWill Deacon case 42: 995ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; 996e1d3c0fdSWill Deacon break; 997e1d3c0fdSWill Deacon case 44: 998ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; 999e1d3c0fdSWill Deacon break; 1000e1d3c0fdSWill Deacon case 48: 1001ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; 1002e1d3c0fdSWill Deacon break; 10036c89928fSRobin Murphy case 52: 1004ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; 10056c89928fSRobin Murphy break; 1006e1d3c0fdSWill Deacon default: 1007e1d3c0fdSWill Deacon goto out_free_data; 1008e1d3c0fdSWill Deacon } 1009e1d3c0fdSWill Deacon 1010ac4b80e5SWill Deacon vtcr->tsz = 64ULL - cfg->ias; 1011ac4b80e5SWill Deacon vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; 1012e1d3c0fdSWill Deacon 1013e1d3c0fdSWill Deacon /* Allocate pgd pages */ 1014c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 1015c79278c1SRobin Murphy GFP_KERNEL, cfg); 1016e1d3c0fdSWill Deacon if (!data->pgd) 1017e1d3c0fdSWill Deacon goto out_free_data; 1018e1d3c0fdSWill Deacon 101987a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 102087a91b15SRobin Murphy wmb(); 1021e1d3c0fdSWill Deacon 1022e1d3c0fdSWill Deacon /* VTTBR */ 1023e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 1024e1d3c0fdSWill Deacon return &data->iop; 1025e1d3c0fdSWill Deacon 1026e1d3c0fdSWill Deacon out_free_data: 1027e1d3c0fdSWill Deacon kfree(data); 1028e1d3c0fdSWill Deacon return NULL; 1029e1d3c0fdSWill Deacon } 1030e1d3c0fdSWill Deacon 1031e1d3c0fdSWill Deacon static struct io_pgtable * 1032e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 1033e1d3c0fdSWill Deacon { 1034e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 1035e1d3c0fdSWill Deacon return NULL; 1036e1d3c0fdSWill Deacon 1037e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1038fb485eb1SRobin Murphy return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 1039e1d3c0fdSWill Deacon } 1040e1d3c0fdSWill Deacon 1041e1d3c0fdSWill Deacon static struct io_pgtable * 1042e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 1043e1d3c0fdSWill Deacon { 1044e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 1045e1d3c0fdSWill Deacon return NULL; 1046e1d3c0fdSWill Deacon 1047e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1048ac4b80e5SWill Deacon return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 1049e1d3c0fdSWill Deacon } 1050e1d3c0fdSWill Deacon 1051d08d42deSRob Herring static struct io_pgtable * 1052d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 1053d08d42deSRob Herring { 105452f325f4SRobin Murphy struct arm_lpae_io_pgtable *data; 1055d08d42deSRob Herring 105652f325f4SRobin Murphy /* No quirks for Mali (hopefully) */ 105752f325f4SRobin Murphy if (cfg->quirks) 105852f325f4SRobin Murphy return NULL; 1059d08d42deSRob Herring 10601be08f45SRobin Murphy if (cfg->ias > 48 || cfg->oas > 40) 1061d08d42deSRob Herring return NULL; 1062d08d42deSRob Herring 1063d08d42deSRob Herring cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1064d08d42deSRob Herring 106552f325f4SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 106652f325f4SRobin Murphy if (!data) 106752f325f4SRobin Murphy return NULL; 1068d08d42deSRob Herring 10691be08f45SRobin Murphy /* Mali seems to need a full 4-level table regardless of IAS */ 1070594ab90fSRobin Murphy if (data->start_level > 0) { 1071594ab90fSRobin Murphy data->start_level = 0; 1072c79278c1SRobin Murphy data->pgd_bits = 0; 10731be08f45SRobin Murphy } 107452f325f4SRobin Murphy /* 107552f325f4SRobin Murphy * MEMATTR: Mali has no actual notion of a non-cacheable type, so the 107652f325f4SRobin Murphy * best we can do is mimic the out-of-tree driver and hope that the 107752f325f4SRobin Murphy * "implementation-defined caching policy" is good enough. Similarly, 107852f325f4SRobin Murphy * we'll use it for the sake of a valid attribute for our 'device' 107952f325f4SRobin Murphy * index, although callers should never request that in practice. 108052f325f4SRobin Murphy */ 108152f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.memattr = 108252f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 108352f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 108452f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 108552f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 108652f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 108752f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 108852f325f4SRobin Murphy 1089c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1090c79278c1SRobin Murphy cfg); 109152f325f4SRobin Murphy if (!data->pgd) 109252f325f4SRobin Murphy goto out_free_data; 109352f325f4SRobin Murphy 109452f325f4SRobin Murphy /* Ensure the empty pgd is visible before TRANSTAB can be written */ 109552f325f4SRobin Murphy wmb(); 109652f325f4SRobin Murphy 109752f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | 1098d08d42deSRob Herring ARM_MALI_LPAE_TTBR_READ_INNER | 1099d08d42deSRob Herring ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; 1100728da60dSRobin Murphy if (cfg->coherent_walk) 1101728da60dSRobin Murphy cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER; 1102728da60dSRobin Murphy 110352f325f4SRobin Murphy return &data->iop; 1104d08d42deSRob Herring 110552f325f4SRobin Murphy out_free_data: 110652f325f4SRobin Murphy kfree(data); 110752f325f4SRobin Murphy return NULL; 1108d08d42deSRob Herring } 1109d08d42deSRob Herring 1110892384cdSSven Peter static struct io_pgtable * 1111892384cdSSven Peter apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 1112892384cdSSven Peter { 1113892384cdSSven Peter struct arm_lpae_io_pgtable *data; 1114892384cdSSven Peter int i; 1115892384cdSSven Peter 1116892384cdSSven Peter if (cfg->oas > 36) 1117892384cdSSven Peter return NULL; 1118892384cdSSven Peter 1119892384cdSSven Peter data = arm_lpae_alloc_pgtable(cfg); 1120892384cdSSven Peter if (!data) 1121892384cdSSven Peter return NULL; 1122892384cdSSven Peter 1123892384cdSSven Peter /* 1124892384cdSSven Peter * The table format itself always uses two levels, but the total VA 1125892384cdSSven Peter * space is mapped by four separate tables, making the MMIO registers 1126892384cdSSven Peter * an effective "level 1". For simplicity, though, we treat this 1127892384cdSSven Peter * equivalently to LPAE stage 2 concatenation at level 2, with the 1128892384cdSSven Peter * additional TTBRs each just pointing at consecutive pages. 1129892384cdSSven Peter */ 1130892384cdSSven Peter if (data->start_level < 1) 1131892384cdSSven Peter goto out_free_data; 1132892384cdSSven Peter if (data->start_level == 1 && data->pgd_bits > 2) 1133892384cdSSven Peter goto out_free_data; 1134892384cdSSven Peter if (data->start_level > 1) 1135892384cdSSven Peter data->pgd_bits = 0; 1136892384cdSSven Peter data->start_level = 2; 1137892384cdSSven Peter cfg->apple_dart_cfg.n_ttbrs = 1 << data->pgd_bits; 1138892384cdSSven Peter data->pgd_bits += data->bits_per_level; 1139892384cdSSven Peter 1140892384cdSSven Peter data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1141892384cdSSven Peter cfg); 1142892384cdSSven Peter if (!data->pgd) 1143892384cdSSven Peter goto out_free_data; 1144892384cdSSven Peter 1145892384cdSSven Peter for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i) 1146892384cdSSven Peter cfg->apple_dart_cfg.ttbr[i] = 1147892384cdSSven Peter virt_to_phys(data->pgd + i * ARM_LPAE_GRANULE(data)); 1148892384cdSSven Peter 1149892384cdSSven Peter return &data->iop; 1150892384cdSSven Peter 1151892384cdSSven Peter out_free_data: 1152892384cdSSven Peter kfree(data); 1153892384cdSSven Peter return NULL; 1154892384cdSSven Peter } 1155892384cdSSven Peter 1156e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1157e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 1158e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1159e1d3c0fdSWill Deacon }; 1160e1d3c0fdSWill Deacon 1161e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1162e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 1163e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1164e1d3c0fdSWill Deacon }; 1165e1d3c0fdSWill Deacon 1166e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1167e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1168e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1169e1d3c0fdSWill Deacon }; 1170e1d3c0fdSWill Deacon 1171e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1172e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1173e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1174e1d3c0fdSWill Deacon }; 1175fe4b991dSWill Deacon 1176d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1177d08d42deSRob Herring .alloc = arm_mali_lpae_alloc_pgtable, 1178d08d42deSRob Herring .free = arm_lpae_free_pgtable, 1179d08d42deSRob Herring }; 1180d08d42deSRob Herring 1181892384cdSSven Peter struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = { 1182892384cdSSven Peter .alloc = apple_dart_alloc_pgtable, 1183892384cdSSven Peter .free = arm_lpae_free_pgtable, 1184892384cdSSven Peter }; 1185892384cdSSven Peter 1186fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1187fe4b991dSWill Deacon 1188b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata; 1189fe4b991dSWill Deacon 1190b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie) 1191fe4b991dSWill Deacon { 1192fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1193fe4b991dSWill Deacon } 1194fe4b991dSWill Deacon 1195b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size, 1196b5813c16SRobin Murphy size_t granule, void *cookie) 1197fe4b991dSWill Deacon { 1198fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1199fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1200fe4b991dSWill Deacon } 1201fe4b991dSWill Deacon 1202b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, 1203b5813c16SRobin Murphy unsigned long iova, size_t granule, 1204b5813c16SRobin Murphy void *cookie) 120510b7a7d9SWill Deacon { 1206abfd6fe0SWill Deacon dummy_tlb_flush(iova, granule, granule, cookie); 120710b7a7d9SWill Deacon } 120810b7a7d9SWill Deacon 1209298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = { 1210fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 121110b7a7d9SWill Deacon .tlb_flush_walk = dummy_tlb_flush, 1212abfd6fe0SWill Deacon .tlb_add_page = dummy_tlb_add_page, 1213fe4b991dSWill Deacon }; 1214fe4b991dSWill Deacon 1215fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1216fe4b991dSWill Deacon { 1217fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1218fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1219fe4b991dSWill Deacon 1220fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1221fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 12225fb190b0SRobin Murphy pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", 1223c79278c1SRobin Murphy ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), 12245fb190b0SRobin Murphy ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); 1225fe4b991dSWill Deacon } 1226fe4b991dSWill Deacon 1227fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1228fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1229fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1230fe4b991dSWill Deacon selftest_running = false; \ 1231fe4b991dSWill Deacon -EFAULT; \ 1232fe4b991dSWill Deacon }) 1233fe4b991dSWill Deacon 1234fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1235fe4b991dSWill Deacon { 12369062c1d0SChristophe JAILLET static const enum io_pgtable_fmt fmts[] __initconst = { 1237fe4b991dSWill Deacon ARM_64_LPAE_S1, 1238fe4b991dSWill Deacon ARM_64_LPAE_S2, 1239fe4b991dSWill Deacon }; 1240fe4b991dSWill Deacon 1241fe4b991dSWill Deacon int i, j; 1242fe4b991dSWill Deacon unsigned long iova; 1243fe4b991dSWill Deacon size_t size; 1244fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1245fe4b991dSWill Deacon 1246fe4b991dSWill Deacon selftest_running = true; 1247fe4b991dSWill Deacon 1248fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1249fe4b991dSWill Deacon cfg_cookie = cfg; 1250fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1251fe4b991dSWill Deacon if (!ops) { 1252fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1253fe4b991dSWill Deacon return -ENOMEM; 1254fe4b991dSWill Deacon } 1255fe4b991dSWill Deacon 1256fe4b991dSWill Deacon /* 1257fe4b991dSWill Deacon * Initial sanity checks. 1258fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1259fe4b991dSWill Deacon */ 1260fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1261fe4b991dSWill Deacon return __FAIL(ops, i); 1262fe4b991dSWill Deacon 1263fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1264fe4b991dSWill Deacon return __FAIL(ops, i); 1265fe4b991dSWill Deacon 1266fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1267fe4b991dSWill Deacon return __FAIL(ops, i); 1268fe4b991dSWill Deacon 1269fe4b991dSWill Deacon /* 1270fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1271fe4b991dSWill Deacon */ 1272fe4b991dSWill Deacon iova = 0; 12734ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1274fe4b991dSWill Deacon size = 1UL << j; 1275fe4b991dSWill Deacon 1276fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1277fe4b991dSWill Deacon IOMMU_WRITE | 1278fe4b991dSWill Deacon IOMMU_NOEXEC | 1279f34ce7a7SBaolin Wang IOMMU_CACHE, GFP_KERNEL)) 1280fe4b991dSWill Deacon return __FAIL(ops, i); 1281fe4b991dSWill Deacon 1282fe4b991dSWill Deacon /* Overlapping mappings */ 1283fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1284f34ce7a7SBaolin Wang IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL)) 1285fe4b991dSWill Deacon return __FAIL(ops, i); 1286fe4b991dSWill Deacon 1287fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1288fe4b991dSWill Deacon return __FAIL(ops, i); 1289fe4b991dSWill Deacon 1290fe4b991dSWill Deacon iova += SZ_1G; 1291fe4b991dSWill Deacon } 1292fe4b991dSWill Deacon 1293fe4b991dSWill Deacon /* Partial unmap */ 1294fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1295a2d3a382SWill Deacon if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) 1296fe4b991dSWill Deacon return __FAIL(ops, i); 1297fe4b991dSWill Deacon 1298fe4b991dSWill Deacon /* Remap of partial unmap */ 1299f34ce7a7SBaolin Wang if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL)) 1300fe4b991dSWill Deacon return __FAIL(ops, i); 1301fe4b991dSWill Deacon 1302fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1303fe4b991dSWill Deacon return __FAIL(ops, i); 1304fe4b991dSWill Deacon 1305fe4b991dSWill Deacon /* Full unmap */ 1306fe4b991dSWill Deacon iova = 0; 1307f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1308fe4b991dSWill Deacon size = 1UL << j; 1309fe4b991dSWill Deacon 1310a2d3a382SWill Deacon if (ops->unmap(ops, iova, size, NULL) != size) 1311fe4b991dSWill Deacon return __FAIL(ops, i); 1312fe4b991dSWill Deacon 1313fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1314fe4b991dSWill Deacon return __FAIL(ops, i); 1315fe4b991dSWill Deacon 1316fe4b991dSWill Deacon /* Remap full block */ 1317f34ce7a7SBaolin Wang if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL)) 1318fe4b991dSWill Deacon return __FAIL(ops, i); 1319fe4b991dSWill Deacon 1320fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1321fe4b991dSWill Deacon return __FAIL(ops, i); 1322fe4b991dSWill Deacon 1323fe4b991dSWill Deacon iova += SZ_1G; 1324fe4b991dSWill Deacon } 1325fe4b991dSWill Deacon 1326fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1327fe4b991dSWill Deacon } 1328fe4b991dSWill Deacon 1329fe4b991dSWill Deacon selftest_running = false; 1330fe4b991dSWill Deacon return 0; 1331fe4b991dSWill Deacon } 1332fe4b991dSWill Deacon 1333fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1334fe4b991dSWill Deacon { 13359062c1d0SChristophe JAILLET static const unsigned long pgsize[] __initconst = { 1336fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1337fe4b991dSWill Deacon SZ_16K | SZ_32M, 1338fe4b991dSWill Deacon SZ_64K | SZ_512M, 1339fe4b991dSWill Deacon }; 1340fe4b991dSWill Deacon 13419062c1d0SChristophe JAILLET static const unsigned int ias[] __initconst = { 1342fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1343fe4b991dSWill Deacon }; 1344fe4b991dSWill Deacon 1345fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1346fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1347fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1348fe4b991dSWill Deacon .oas = 48, 13494f41845bSWill Deacon .coherent_walk = true, 1350fe4b991dSWill Deacon }; 1351fe4b991dSWill Deacon 1352fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1353fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1354fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1355fe4b991dSWill Deacon cfg.ias = ias[j]; 1356fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1357fe4b991dSWill Deacon pgsize[i], ias[j]); 1358fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1359fe4b991dSWill Deacon fail++; 1360fe4b991dSWill Deacon else 1361fe4b991dSWill Deacon pass++; 1362fe4b991dSWill Deacon } 1363fe4b991dSWill Deacon } 1364fe4b991dSWill Deacon 1365fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1366fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1367fe4b991dSWill Deacon } 1368fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1369fe4b991dSWill Deacon #endif 1370