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 49e1d3c0fdSWill Deacon /* 50e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 51e1d3c0fdSWill Deacon * pagetable in d. 52e1d3c0fdSWill Deacon */ 53e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 54c79278c1SRobin Murphy ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0) 55e1d3c0fdSWill Deacon 56e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 57367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 58e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 59e1d3c0fdSWill Deacon 60e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 615fb190b0SRobin Murphy #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d)) 62e1d3c0fdSWill Deacon 63e1d3c0fdSWill Deacon /* Page table bits */ 64e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 65e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 66e1d3c0fdSWill Deacon 67e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 68e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 69e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 70e1d3c0fdSWill Deacon 716c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) 726c89928fSRobin Murphy 73c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 74e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 75e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 76e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 77e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 78e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 79c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 80e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 81e1d3c0fdSWill Deacon 82e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 83e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 84e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 86e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 872c3d273eSRobin Murphy /* Software bit for solving coherency races */ 882c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) 89e1d3c0fdSWill Deacon 90e1d3c0fdSWill Deacon /* Stage-1 PTE */ 91e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 93e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 95e1d3c0fdSWill Deacon 96e1d3c0fdSWill Deacon /* Stage-2 PTE */ 97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 98e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 99e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 102e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 103e1d3c0fdSWill Deacon 104e1d3c0fdSWill Deacon /* Register bits */ 105fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_SL0_MASK 0x3 106e1d3c0fdSWill Deacon 107e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 108e1d3c0fdSWill Deacon 109fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_SHIFT 16 110fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_MASK 0x7 111e1d3c0fdSWill Deacon 112e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 113e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 114e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 115e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 11690ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 117e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 118e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 119e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 120e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 12190ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 122e1d3c0fdSWill Deacon 123d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) 124d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) 125d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) 126d08d42deSRob Herring 12752f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL 12852f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL 12952f325f4SRobin Murphy 130e1d3c0fdSWill Deacon /* IOPTE accessors */ 1316c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 132e1d3c0fdSWill Deacon 133*f37eb484SKunkun Jiang #define iopte_type(pte) \ 134e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 135e1d3c0fdSWill Deacon 136e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 137e1d3c0fdSWill Deacon 138e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 139e1d3c0fdSWill Deacon struct io_pgtable iop; 140e1d3c0fdSWill Deacon 141c79278c1SRobin Murphy int pgd_bits; 142594ab90fSRobin Murphy int start_level; 1435fb190b0SRobin Murphy int bits_per_level; 144e1d3c0fdSWill Deacon 145e1d3c0fdSWill Deacon void *pgd; 146e1d3c0fdSWill Deacon }; 147e1d3c0fdSWill Deacon 148e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 149e1d3c0fdSWill Deacon 150d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, 151d08d42deSRob Herring enum io_pgtable_fmt fmt) 152d08d42deSRob Herring { 153d08d42deSRob Herring if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) 154*f37eb484SKunkun Jiang return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE; 155d08d42deSRob Herring 156*f37eb484SKunkun Jiang return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK; 157d08d42deSRob Herring } 158d08d42deSRob Herring 1596c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, 1606c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1616c89928fSRobin Murphy { 1626c89928fSRobin Murphy arm_lpae_iopte pte = paddr; 1636c89928fSRobin Murphy 1646c89928fSRobin Murphy /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ 1656c89928fSRobin Murphy return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; 1666c89928fSRobin Murphy } 1676c89928fSRobin Murphy 1686c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, 1696c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1706c89928fSRobin Murphy { 17178688059SRobin Murphy u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; 1726c89928fSRobin Murphy 1735fb190b0SRobin Murphy if (ARM_LPAE_GRANULE(data) < SZ_64K) 1746c89928fSRobin Murphy return paddr; 1756c89928fSRobin Murphy 1766c89928fSRobin Murphy /* Rotate the packed high-order bits back to the top */ 1776c89928fSRobin Murphy return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); 1786c89928fSRobin Murphy } 1796c89928fSRobin Murphy 180fe4b991dSWill Deacon static bool selftest_running = false; 181fe4b991dSWill Deacon 182ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 183f8d54961SRobin Murphy { 184ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 185f8d54961SRobin Murphy } 186f8d54961SRobin Murphy 187f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 188f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 189f8d54961SRobin Murphy { 190f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 1914b123757SRobin Murphy int order = get_order(size); 1924b123757SRobin Murphy struct page *p; 193f8d54961SRobin Murphy dma_addr_t dma; 1944b123757SRobin Murphy void *pages; 195f8d54961SRobin Murphy 1964b123757SRobin Murphy VM_BUG_ON((gfp & __GFP_HIGHMEM)); 197fac83d29SJean-Philippe Brucker p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 198fac83d29SJean-Philippe Brucker gfp | __GFP_ZERO, order); 1994b123757SRobin Murphy if (!p) 200f8d54961SRobin Murphy return NULL; 201f8d54961SRobin Murphy 2024b123757SRobin Murphy pages = page_address(p); 2034f41845bSWill Deacon if (!cfg->coherent_walk) { 204f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 205f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 206f8d54961SRobin Murphy goto out_free; 207f8d54961SRobin Murphy /* 208f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 209ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 210ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 211f8d54961SRobin Murphy */ 212ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 213f8d54961SRobin Murphy goto out_unmap; 214f8d54961SRobin Murphy } 215f8d54961SRobin Murphy 216f8d54961SRobin Murphy return pages; 217f8d54961SRobin Murphy 218f8d54961SRobin Murphy out_unmap: 219f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 220f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 221f8d54961SRobin Murphy out_free: 2224b123757SRobin Murphy __free_pages(p, order); 223f8d54961SRobin Murphy return NULL; 224f8d54961SRobin Murphy } 225f8d54961SRobin Murphy 226f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 227f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 228f8d54961SRobin Murphy { 2294f41845bSWill Deacon if (!cfg->coherent_walk) 230ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 231f8d54961SRobin Murphy size, DMA_TO_DEVICE); 2324b123757SRobin Murphy free_pages((unsigned long)pages, get_order(size)); 233f8d54961SRobin Murphy } 234f8d54961SRobin Murphy 2352c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 2362c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2372c3d273eSRobin Murphy { 2382c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 2392c3d273eSRobin Murphy sizeof(*ptep), DMA_TO_DEVICE); 2402c3d273eSRobin Murphy } 2412c3d273eSRobin Murphy 242f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 24387a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 244f8d54961SRobin Murphy { 245f8d54961SRobin Murphy *ptep = pte; 246f8d54961SRobin Murphy 2474f41845bSWill Deacon if (!cfg->coherent_walk) 2482c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 249f8d54961SRobin Murphy } 250f8d54961SRobin Murphy 251193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 2523951c41aSWill Deacon struct iommu_iotlb_gather *gather, 253cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 254cf27ec93SWill Deacon arm_lpae_iopte *ptep); 255cf27ec93SWill Deacon 256fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 257fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 258fb3a9579SRobin Murphy int lvl, arm_lpae_iopte *ptep) 259fb3a9579SRobin Murphy { 260fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 261fb3a9579SRobin Murphy 262d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) 263fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 264fb3a9579SRobin Murphy else 265fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 266fb3a9579SRobin Murphy 2676c89928fSRobin Murphy pte |= paddr_to_iopte(paddr, data); 268fb3a9579SRobin Murphy 269fb3a9579SRobin Murphy __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); 270fb3a9579SRobin Murphy } 271fb3a9579SRobin Murphy 272e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 273e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 274e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 275e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 276e1d3c0fdSWill Deacon { 277fb3a9579SRobin Murphy arm_lpae_iopte pte = *ptep; 278e1d3c0fdSWill Deacon 279d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) { 280cf27ec93SWill Deacon /* We require an unmap first */ 281fe4b991dSWill Deacon WARN_ON(!selftest_running); 282e1d3c0fdSWill Deacon return -EEXIST; 283*f37eb484SKunkun Jiang } else if (iopte_type(pte) == ARM_LPAE_PTE_TYPE_TABLE) { 284cf27ec93SWill Deacon /* 285cf27ec93SWill Deacon * We need to unmap and free the old table before 286cf27ec93SWill Deacon * overwriting it with a block entry. 287cf27ec93SWill Deacon */ 288cf27ec93SWill Deacon arm_lpae_iopte *tblp; 289cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 290cf27ec93SWill Deacon 291cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 2923951c41aSWill Deacon if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) { 2933951c41aSWill Deacon WARN_ON(1); 294cf27ec93SWill Deacon return -EINVAL; 295fe4b991dSWill Deacon } 2963951c41aSWill Deacon } 297e1d3c0fdSWill Deacon 298fb3a9579SRobin Murphy __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); 299e1d3c0fdSWill Deacon return 0; 300e1d3c0fdSWill Deacon } 301e1d3c0fdSWill Deacon 302fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 303fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3042c3d273eSRobin Murphy arm_lpae_iopte curr, 305fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 306fb3a9579SRobin Murphy { 3072c3d273eSRobin Murphy arm_lpae_iopte old, new; 308fb3a9579SRobin Murphy 309fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 310fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 311fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 312fb3a9579SRobin Murphy 31377f34458SWill Deacon /* 31477f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 31577f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 31677f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 31777f34458SWill Deacon */ 31877f34458SWill Deacon dma_wmb(); 3192c3d273eSRobin Murphy 3202c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3212c3d273eSRobin Murphy 3224f41845bSWill Deacon if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) 3232c3d273eSRobin Murphy return old; 3242c3d273eSRobin Murphy 3252c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 3262c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3272c3d273eSRobin Murphy if (old == curr) 3282c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3292c3d273eSRobin Murphy 3302c3d273eSRobin Murphy return old; 331fb3a9579SRobin Murphy } 332fb3a9579SRobin Murphy 333e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 334e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 335f34ce7a7SBaolin Wang int lvl, arm_lpae_iopte *ptep, gfp_t gfp) 336e1d3c0fdSWill Deacon { 337e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 338e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3392c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 340f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 341e1d3c0fdSWill Deacon 342e1d3c0fdSWill Deacon /* Find our entry at the current level */ 343e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 344e1d3c0fdSWill Deacon 345e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 346f7b90d2cSRobin Murphy if (size == block_size) 347e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 348e1d3c0fdSWill Deacon 349e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 350e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 351e1d3c0fdSWill Deacon return -EINVAL; 352e1d3c0fdSWill Deacon 353e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3542c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 355e1d3c0fdSWill Deacon if (!pte) { 356f34ce7a7SBaolin Wang cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg); 357e1d3c0fdSWill Deacon if (!cptep) 358e1d3c0fdSWill Deacon return -ENOMEM; 359e1d3c0fdSWill Deacon 3602c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 3612c3d273eSRobin Murphy if (pte) 3622c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 3634f41845bSWill Deacon } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 3642c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3652c3d273eSRobin Murphy } 3662c3d273eSRobin Murphy 367d08d42deSRob Herring if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { 368e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 3692c3d273eSRobin Murphy } else if (pte) { 370ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 371ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 372ed46e66cSOleksandr Tyshchenko return -EEXIST; 373e1d3c0fdSWill Deacon } 374e1d3c0fdSWill Deacon 375e1d3c0fdSWill Deacon /* Rinse, repeat */ 376f34ce7a7SBaolin Wang return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep, gfp); 377e1d3c0fdSWill Deacon } 378e1d3c0fdSWill Deacon 379e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 380e1d3c0fdSWill Deacon int prot) 381e1d3c0fdSWill Deacon { 382e1d3c0fdSWill Deacon arm_lpae_iopte pte; 383e1d3c0fdSWill Deacon 384e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 385e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 386e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 387e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 388e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 389e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 390e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 391e1d3c0fdSWill Deacon } else { 392e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 393e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 394e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 395e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 396e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 397d08d42deSRob Herring } 398d08d42deSRob Herring 399d08d42deSRob Herring /* 400d08d42deSRob Herring * Note that this logic is structured to accommodate Mali LPAE 401d08d42deSRob Herring * having stage-1-like attributes but stage-2-like permissions. 402d08d42deSRob Herring */ 403d08d42deSRob Herring if (data->iop.fmt == ARM_64_LPAE_S2 || 404d08d42deSRob Herring data->iop.fmt == ARM_32_LPAE_S2) { 405fb948251SRobin Murphy if (prot & IOMMU_MMIO) 406fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 407fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 408e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 409e1d3c0fdSWill Deacon else 410e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 411d08d42deSRob Herring } else { 412d08d42deSRob Herring if (prot & IOMMU_MMIO) 413d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 414d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 415d08d42deSRob Herring else if (prot & IOMMU_CACHE) 416d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 417d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 418e1d3c0fdSWill Deacon } 419e1d3c0fdSWill Deacon 4207618e479SRobin Murphy if (prot & IOMMU_CACHE) 4217618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_IS; 4227618e479SRobin Murphy else 4237618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_OS; 4247618e479SRobin Murphy 425e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 426e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 427e1d3c0fdSWill Deacon 4287618e479SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 4297618e479SRobin Murphy pte |= ARM_LPAE_PTE_NS; 4307618e479SRobin Murphy 4317618e479SRobin Murphy if (data->iop.fmt != ARM_MALI_LPAE) 4327618e479SRobin Murphy pte |= ARM_LPAE_PTE_AF; 4337618e479SRobin Murphy 434e1d3c0fdSWill Deacon return pte; 435e1d3c0fdSWill Deacon } 436e1d3c0fdSWill Deacon 437e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 438f34ce7a7SBaolin Wang phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp) 439e1d3c0fdSWill Deacon { 440e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 441f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 442e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 443594ab90fSRobin Murphy int ret, lvl = data->start_level; 444e1d3c0fdSWill Deacon arm_lpae_iopte prot; 44508090744SRobin Murphy long iaext = (s64)iova >> cfg->ias; 446e1d3c0fdSWill Deacon 447f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 448f7b90d2cSRobin Murphy return -EINVAL; 449f7b90d2cSRobin Murphy 450db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 451db690301SRobin Murphy iaext = ~iaext; 452db690301SRobin Murphy if (WARN_ON(iaext || paddr >> cfg->oas)) 45376557391SRobin Murphy return -ERANGE; 45476557391SRobin Murphy 455f12e0d22SKeqian Zhu /* If no access, then nothing to do */ 456f12e0d22SKeqian Zhu if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 457f12e0d22SKeqian Zhu return 0; 458f12e0d22SKeqian Zhu 459e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 460f34ce7a7SBaolin Wang ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, gfp); 46187a91b15SRobin Murphy /* 46287a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 46387a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 46487a91b15SRobin Murphy */ 46587a91b15SRobin Murphy wmb(); 46687a91b15SRobin Murphy 46787a91b15SRobin Murphy return ret; 468e1d3c0fdSWill Deacon } 469e1d3c0fdSWill Deacon 470e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 471e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 472e1d3c0fdSWill Deacon { 473e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 474e1d3c0fdSWill Deacon unsigned long table_size; 475e1d3c0fdSWill Deacon 476594ab90fSRobin Murphy if (lvl == data->start_level) 477c79278c1SRobin Murphy table_size = ARM_LPAE_PGD_SIZE(data); 478e1d3c0fdSWill Deacon else 47906c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 480e1d3c0fdSWill Deacon 481e1d3c0fdSWill Deacon start = ptep; 48212c2ab09SWill Deacon 48312c2ab09SWill Deacon /* Only leaf entries at the last level */ 48412c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 48512c2ab09SWill Deacon end = ptep; 48612c2ab09SWill Deacon else 487e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 488e1d3c0fdSWill Deacon 489e1d3c0fdSWill Deacon while (ptep != end) { 490e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 491e1d3c0fdSWill Deacon 492d08d42deSRob Herring if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) 493e1d3c0fdSWill Deacon continue; 494e1d3c0fdSWill Deacon 495e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 496e1d3c0fdSWill Deacon } 497e1d3c0fdSWill Deacon 498f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 499e1d3c0fdSWill Deacon } 500e1d3c0fdSWill Deacon 501e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 502e1d3c0fdSWill Deacon { 503e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 504e1d3c0fdSWill Deacon 505594ab90fSRobin Murphy __arm_lpae_free_pgtable(data, data->start_level, data->pgd); 506e1d3c0fdSWill Deacon kfree(data); 507e1d3c0fdSWill Deacon } 508e1d3c0fdSWill Deacon 509193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 5103951c41aSWill Deacon struct iommu_iotlb_gather *gather, 511e1d3c0fdSWill Deacon unsigned long iova, size_t size, 512fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 513fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 514e1d3c0fdSWill Deacon { 515fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 516fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 517e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 518fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 519fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 520fb3a9579SRobin Murphy int i, unmap_idx = -1; 521e1d3c0fdSWill Deacon 522fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 523fb3a9579SRobin Murphy return 0; 524e1d3c0fdSWill Deacon 525fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 526fb3a9579SRobin Murphy if (!tablep) 527fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 528e1d3c0fdSWill Deacon 529fb3a9579SRobin Murphy if (size == split_sz) 530fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 531fb3a9579SRobin Murphy 5326c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 533fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 534fb3a9579SRobin Murphy 535fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 536e1d3c0fdSWill Deacon /* Unmap! */ 537fb3a9579SRobin Murphy if (i == unmap_idx) 538e1d3c0fdSWill Deacon continue; 539e1d3c0fdSWill Deacon 540fb3a9579SRobin Murphy __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); 541e1d3c0fdSWill Deacon } 542e1d3c0fdSWill Deacon 5432c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5442c3d273eSRobin Murphy if (pte != blk_pte) { 5452c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5462c3d273eSRobin Murphy /* 5472c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5482c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5492c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5502c3d273eSRobin Murphy */ 551*f37eb484SKunkun Jiang if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE) 5522c3d273eSRobin Murphy return 0; 5532c3d273eSRobin Murphy 5542c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 55585c7a0f1SRobin Murphy } else if (unmap_idx >= 0) { 5563951c41aSWill Deacon io_pgtable_tlb_add_page(&data->iop, gather, iova, size); 557e1d3c0fdSWill Deacon return size; 558e1d3c0fdSWill Deacon } 559e1d3c0fdSWill Deacon 5603951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep); 56185c7a0f1SRobin Murphy } 56285c7a0f1SRobin Murphy 563193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 5643951c41aSWill Deacon struct iommu_iotlb_gather *gather, 565e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 566e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 567e1d3c0fdSWill Deacon { 568e1d3c0fdSWill Deacon arm_lpae_iopte pte; 569507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 570e1d3c0fdSWill Deacon 5712eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5722eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5732eb97c78SRobin Murphy return 0; 5742eb97c78SRobin Murphy 575e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 5762c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 5772eb97c78SRobin Murphy if (WARN_ON(!pte)) 578e1d3c0fdSWill Deacon return 0; 579e1d3c0fdSWill Deacon 580e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 581fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 582507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 583e1d3c0fdSWill Deacon 584d08d42deSRob Herring if (!iopte_leaf(pte, lvl, iop->fmt)) { 585e1d3c0fdSWill Deacon /* Also flush any partial walks */ 58610b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(iop, iova, size, 58710b7a7d9SWill Deacon ARM_LPAE_GRANULE(data)); 588e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 589e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 590b6b65ca2SZhen Lei } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { 591b6b65ca2SZhen Lei /* 592b6b65ca2SZhen Lei * Order the PTE update against queueing the IOVA, to 593b6b65ca2SZhen Lei * guarantee that a flush callback from a different CPU 594b6b65ca2SZhen Lei * has observed it before the TLBIALL can be issued. 595b6b65ca2SZhen Lei */ 596b6b65ca2SZhen Lei smp_wmb(); 597e1d3c0fdSWill Deacon } else { 5983951c41aSWill Deacon io_pgtable_tlb_add_page(iop, gather, iova, size); 599e1d3c0fdSWill Deacon } 600e1d3c0fdSWill Deacon 601e1d3c0fdSWill Deacon return size; 602d08d42deSRob Herring } else if (iopte_leaf(pte, lvl, iop->fmt)) { 603e1d3c0fdSWill Deacon /* 604e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 605e1d3c0fdSWill Deacon * minus the part we want to unmap 606e1d3c0fdSWill Deacon */ 6073951c41aSWill Deacon return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, 608fb3a9579SRobin Murphy lvl + 1, ptep); 609e1d3c0fdSWill Deacon } 610e1d3c0fdSWill Deacon 611e1d3c0fdSWill Deacon /* Keep on walkin' */ 612e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 6133951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep); 614e1d3c0fdSWill Deacon } 615e1d3c0fdSWill Deacon 616193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 617a2d3a382SWill Deacon size_t size, struct iommu_iotlb_gather *gather) 618e1d3c0fdSWill Deacon { 619e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 620f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 621e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 62208090744SRobin Murphy long iaext = (s64)iova >> cfg->ias; 623e1d3c0fdSWill Deacon 624f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 625f7b90d2cSRobin Murphy return 0; 626f7b90d2cSRobin Murphy 627db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 628db690301SRobin Murphy iaext = ~iaext; 629db690301SRobin Murphy if (WARN_ON(iaext)) 63076557391SRobin Murphy return 0; 63176557391SRobin Murphy 632594ab90fSRobin Murphy return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep); 633e1d3c0fdSWill Deacon } 634e1d3c0fdSWill Deacon 635e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 636e1d3c0fdSWill Deacon unsigned long iova) 637e1d3c0fdSWill Deacon { 638e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 639e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 640594ab90fSRobin Murphy int lvl = data->start_level; 641e1d3c0fdSWill Deacon 642e1d3c0fdSWill Deacon do { 643e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 644e1d3c0fdSWill Deacon if (!ptep) 645e1d3c0fdSWill Deacon return 0; 646e1d3c0fdSWill Deacon 647e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6482c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6492c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 650e1d3c0fdSWill Deacon 651e1d3c0fdSWill Deacon /* Valid entry? */ 652e1d3c0fdSWill Deacon if (!pte) 653e1d3c0fdSWill Deacon return 0; 654e1d3c0fdSWill Deacon 655e1d3c0fdSWill Deacon /* Leaf entry? */ 656d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) 657e1d3c0fdSWill Deacon goto found_translation; 658e1d3c0fdSWill Deacon 659e1d3c0fdSWill Deacon /* Take it to the next level */ 660e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 661e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 662e1d3c0fdSWill Deacon 663e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 664e1d3c0fdSWill Deacon return 0; 665e1d3c0fdSWill Deacon 666e1d3c0fdSWill Deacon found_translation: 6677c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 6686c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 669e1d3c0fdSWill Deacon } 670e1d3c0fdSWill Deacon 671e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 672e1d3c0fdSWill Deacon { 6736c89928fSRobin Murphy unsigned long granule, page_sizes; 6746c89928fSRobin Murphy unsigned int max_addr_bits = 48; 675e1d3c0fdSWill Deacon 676e1d3c0fdSWill Deacon /* 677e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 678e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 679e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 680e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 681e1d3c0fdSWill Deacon * chosen granule. 682e1d3c0fdSWill Deacon */ 683e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 684e1d3c0fdSWill Deacon granule = PAGE_SIZE; 685e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 686e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 687e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 688e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 689e1d3c0fdSWill Deacon else 690e1d3c0fdSWill Deacon granule = 0; 691e1d3c0fdSWill Deacon 692e1d3c0fdSWill Deacon switch (granule) { 693e1d3c0fdSWill Deacon case SZ_4K: 6946c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 695e1d3c0fdSWill Deacon break; 696e1d3c0fdSWill Deacon case SZ_16K: 6976c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 698e1d3c0fdSWill Deacon break; 699e1d3c0fdSWill Deacon case SZ_64K: 7006c89928fSRobin Murphy max_addr_bits = 52; 7016c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7026c89928fSRobin Murphy if (cfg->oas > 48) 7036c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 704e1d3c0fdSWill Deacon break; 705e1d3c0fdSWill Deacon default: 7066c89928fSRobin Murphy page_sizes = 0; 707e1d3c0fdSWill Deacon } 7086c89928fSRobin Murphy 7096c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7106c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7116c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 712e1d3c0fdSWill Deacon } 713e1d3c0fdSWill Deacon 714e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 715e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 716e1d3c0fdSWill Deacon { 717e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 7185fb190b0SRobin Murphy int levels, va_bits, pg_shift; 719e1d3c0fdSWill Deacon 720e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 721e1d3c0fdSWill Deacon 722e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 723e1d3c0fdSWill Deacon return NULL; 724e1d3c0fdSWill Deacon 725e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 726e1d3c0fdSWill Deacon return NULL; 727e1d3c0fdSWill Deacon 728e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 729e1d3c0fdSWill Deacon return NULL; 730e1d3c0fdSWill Deacon 731e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 732e1d3c0fdSWill Deacon if (!data) 733e1d3c0fdSWill Deacon return NULL; 734e1d3c0fdSWill Deacon 7355fb190b0SRobin Murphy pg_shift = __ffs(cfg->pgsize_bitmap); 7365fb190b0SRobin Murphy data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); 737e1d3c0fdSWill Deacon 7385fb190b0SRobin Murphy va_bits = cfg->ias - pg_shift; 739594ab90fSRobin Murphy levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 740594ab90fSRobin Murphy data->start_level = ARM_LPAE_MAX_LEVELS - levels; 741e1d3c0fdSWill Deacon 742e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 743c79278c1SRobin Murphy data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); 744e1d3c0fdSWill Deacon 745e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 746e1d3c0fdSWill Deacon .map = arm_lpae_map, 747e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 748e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 749e1d3c0fdSWill Deacon }; 750e1d3c0fdSWill Deacon 751e1d3c0fdSWill Deacon return data; 752e1d3c0fdSWill Deacon } 753e1d3c0fdSWill Deacon 754e1d3c0fdSWill Deacon static struct io_pgtable * 755e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 756e1d3c0fdSWill Deacon { 757e1d3c0fdSWill Deacon u64 reg; 7583850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 759fb485eb1SRobin Murphy typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; 760db690301SRobin Murphy bool tg1; 761e1d3c0fdSWill Deacon 7624f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | 763db690301SRobin Murphy IO_PGTABLE_QUIRK_NON_STRICT | 764db690301SRobin Murphy IO_PGTABLE_QUIRK_ARM_TTBR1)) 7653850db49SRobin Murphy return NULL; 7663850db49SRobin Murphy 7673850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 768e1d3c0fdSWill Deacon if (!data) 769e1d3c0fdSWill Deacon return NULL; 770e1d3c0fdSWill Deacon 771e1d3c0fdSWill Deacon /* TCR */ 7729e6ea59fSBjorn Andersson if (cfg->coherent_walk) { 773fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_IS; 774fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 775fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 7769e6ea59fSBjorn Andersson } else { 777fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_OS; 778fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_NC; 779fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_NC; 7809e6ea59fSBjorn Andersson } 781e1d3c0fdSWill Deacon 782db690301SRobin Murphy tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1; 78306c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 784e1d3c0fdSWill Deacon case SZ_4K: 785db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K; 786e1d3c0fdSWill Deacon break; 787e1d3c0fdSWill Deacon case SZ_16K: 788db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K; 789e1d3c0fdSWill Deacon break; 790e1d3c0fdSWill Deacon case SZ_64K: 791db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K; 792e1d3c0fdSWill Deacon break; 793e1d3c0fdSWill Deacon } 794e1d3c0fdSWill Deacon 795e1d3c0fdSWill Deacon switch (cfg->oas) { 796e1d3c0fdSWill Deacon case 32: 797fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_32_BIT; 798e1d3c0fdSWill Deacon break; 799e1d3c0fdSWill Deacon case 36: 800fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_36_BIT; 801e1d3c0fdSWill Deacon break; 802e1d3c0fdSWill Deacon case 40: 803fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_40_BIT; 804e1d3c0fdSWill Deacon break; 805e1d3c0fdSWill Deacon case 42: 806fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_42_BIT; 807e1d3c0fdSWill Deacon break; 808e1d3c0fdSWill Deacon case 44: 809fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_44_BIT; 810e1d3c0fdSWill Deacon break; 811e1d3c0fdSWill Deacon case 48: 812fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_48_BIT; 813e1d3c0fdSWill Deacon break; 8146c89928fSRobin Murphy case 52: 815fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_52_BIT; 8166c89928fSRobin Murphy break; 817e1d3c0fdSWill Deacon default: 818e1d3c0fdSWill Deacon goto out_free_data; 819e1d3c0fdSWill Deacon } 820e1d3c0fdSWill Deacon 821fb485eb1SRobin Murphy tcr->tsz = 64ULL - cfg->ias; 822e1d3c0fdSWill Deacon 823e1d3c0fdSWill Deacon /* MAIRs */ 824e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 825e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 826e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 827e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 828e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 82990ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | 83090ec7a76SVivek Gautam (ARM_LPAE_MAIR_ATTR_INC_OWBRWA 83190ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); 832e1d3c0fdSWill Deacon 833205577abSRobin Murphy cfg->arm_lpae_s1_cfg.mair = reg; 834e1d3c0fdSWill Deacon 835e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 836c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 837c79278c1SRobin Murphy GFP_KERNEL, cfg); 838e1d3c0fdSWill Deacon if (!data->pgd) 839e1d3c0fdSWill Deacon goto out_free_data; 840e1d3c0fdSWill Deacon 84187a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 84287a91b15SRobin Murphy wmb(); 843e1d3c0fdSWill Deacon 844d1e5f26fSRobin Murphy /* TTBR */ 845d1e5f26fSRobin Murphy cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); 846e1d3c0fdSWill Deacon return &data->iop; 847e1d3c0fdSWill Deacon 848e1d3c0fdSWill Deacon out_free_data: 849e1d3c0fdSWill Deacon kfree(data); 850e1d3c0fdSWill Deacon return NULL; 851e1d3c0fdSWill Deacon } 852e1d3c0fdSWill Deacon 853e1d3c0fdSWill Deacon static struct io_pgtable * 854e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 855e1d3c0fdSWill Deacon { 856ac4b80e5SWill Deacon u64 sl; 8573850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 858ac4b80e5SWill Deacon typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; 859e1d3c0fdSWill Deacon 8603850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 8614f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) 8623850db49SRobin Murphy return NULL; 8633850db49SRobin Murphy 8643850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 865e1d3c0fdSWill Deacon if (!data) 866e1d3c0fdSWill Deacon return NULL; 867e1d3c0fdSWill Deacon 868e1d3c0fdSWill Deacon /* 869e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 870e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 871e1d3c0fdSWill Deacon */ 872594ab90fSRobin Murphy if (data->start_level == 0) { 873e1d3c0fdSWill Deacon unsigned long pgd_pages; 874e1d3c0fdSWill Deacon 875c79278c1SRobin Murphy pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); 876e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 877c79278c1SRobin Murphy data->pgd_bits += data->bits_per_level; 878594ab90fSRobin Murphy data->start_level++; 879e1d3c0fdSWill Deacon } 880e1d3c0fdSWill Deacon } 881e1d3c0fdSWill Deacon 882e1d3c0fdSWill Deacon /* VTCR */ 88330d2acb6SWill Deacon if (cfg->coherent_walk) { 884ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_IS; 885ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 886ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 88730d2acb6SWill Deacon } else { 888ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_OS; 889ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_NC; 890ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_NC; 89130d2acb6SWill Deacon } 892e1d3c0fdSWill Deacon 893594ab90fSRobin Murphy sl = data->start_level; 894e1d3c0fdSWill Deacon 89506c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 896e1d3c0fdSWill Deacon case SZ_4K: 897ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_4K; 898e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 899e1d3c0fdSWill Deacon break; 900e1d3c0fdSWill Deacon case SZ_16K: 901ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_16K; 902e1d3c0fdSWill Deacon break; 903e1d3c0fdSWill Deacon case SZ_64K: 904ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_64K; 905e1d3c0fdSWill Deacon break; 906e1d3c0fdSWill Deacon } 907e1d3c0fdSWill Deacon 908e1d3c0fdSWill Deacon switch (cfg->oas) { 909e1d3c0fdSWill Deacon case 32: 910ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; 911e1d3c0fdSWill Deacon break; 912e1d3c0fdSWill Deacon case 36: 913ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; 914e1d3c0fdSWill Deacon break; 915e1d3c0fdSWill Deacon case 40: 916ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; 917e1d3c0fdSWill Deacon break; 918e1d3c0fdSWill Deacon case 42: 919ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; 920e1d3c0fdSWill Deacon break; 921e1d3c0fdSWill Deacon case 44: 922ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; 923e1d3c0fdSWill Deacon break; 924e1d3c0fdSWill Deacon case 48: 925ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; 926e1d3c0fdSWill Deacon break; 9276c89928fSRobin Murphy case 52: 928ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; 9296c89928fSRobin Murphy break; 930e1d3c0fdSWill Deacon default: 931e1d3c0fdSWill Deacon goto out_free_data; 932e1d3c0fdSWill Deacon } 933e1d3c0fdSWill Deacon 934ac4b80e5SWill Deacon vtcr->tsz = 64ULL - cfg->ias; 935ac4b80e5SWill Deacon vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; 936e1d3c0fdSWill Deacon 937e1d3c0fdSWill Deacon /* Allocate pgd pages */ 938c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 939c79278c1SRobin Murphy GFP_KERNEL, cfg); 940e1d3c0fdSWill Deacon if (!data->pgd) 941e1d3c0fdSWill Deacon goto out_free_data; 942e1d3c0fdSWill Deacon 94387a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 94487a91b15SRobin Murphy wmb(); 945e1d3c0fdSWill Deacon 946e1d3c0fdSWill Deacon /* VTTBR */ 947e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 948e1d3c0fdSWill Deacon return &data->iop; 949e1d3c0fdSWill Deacon 950e1d3c0fdSWill Deacon out_free_data: 951e1d3c0fdSWill Deacon kfree(data); 952e1d3c0fdSWill Deacon return NULL; 953e1d3c0fdSWill Deacon } 954e1d3c0fdSWill Deacon 955e1d3c0fdSWill Deacon static struct io_pgtable * 956e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 957e1d3c0fdSWill Deacon { 958e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 959e1d3c0fdSWill Deacon return NULL; 960e1d3c0fdSWill Deacon 961e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 962fb485eb1SRobin Murphy return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 963e1d3c0fdSWill Deacon } 964e1d3c0fdSWill Deacon 965e1d3c0fdSWill Deacon static struct io_pgtable * 966e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 967e1d3c0fdSWill Deacon { 968e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 969e1d3c0fdSWill Deacon return NULL; 970e1d3c0fdSWill Deacon 971e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 972ac4b80e5SWill Deacon return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 973e1d3c0fdSWill Deacon } 974e1d3c0fdSWill Deacon 975d08d42deSRob Herring static struct io_pgtable * 976d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 977d08d42deSRob Herring { 97852f325f4SRobin Murphy struct arm_lpae_io_pgtable *data; 979d08d42deSRob Herring 98052f325f4SRobin Murphy /* No quirks for Mali (hopefully) */ 98152f325f4SRobin Murphy if (cfg->quirks) 98252f325f4SRobin Murphy return NULL; 983d08d42deSRob Herring 9841be08f45SRobin Murphy if (cfg->ias > 48 || cfg->oas > 40) 985d08d42deSRob Herring return NULL; 986d08d42deSRob Herring 987d08d42deSRob Herring cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 988d08d42deSRob Herring 98952f325f4SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 99052f325f4SRobin Murphy if (!data) 99152f325f4SRobin Murphy return NULL; 992d08d42deSRob Herring 9931be08f45SRobin Murphy /* Mali seems to need a full 4-level table regardless of IAS */ 994594ab90fSRobin Murphy if (data->start_level > 0) { 995594ab90fSRobin Murphy data->start_level = 0; 996c79278c1SRobin Murphy data->pgd_bits = 0; 9971be08f45SRobin Murphy } 99852f325f4SRobin Murphy /* 99952f325f4SRobin Murphy * MEMATTR: Mali has no actual notion of a non-cacheable type, so the 100052f325f4SRobin Murphy * best we can do is mimic the out-of-tree driver and hope that the 100152f325f4SRobin Murphy * "implementation-defined caching policy" is good enough. Similarly, 100252f325f4SRobin Murphy * we'll use it for the sake of a valid attribute for our 'device' 100352f325f4SRobin Murphy * index, although callers should never request that in practice. 100452f325f4SRobin Murphy */ 100552f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.memattr = 100652f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 100752f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 100852f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 100952f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 101052f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 101152f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 101252f325f4SRobin Murphy 1013c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1014c79278c1SRobin Murphy cfg); 101552f325f4SRobin Murphy if (!data->pgd) 101652f325f4SRobin Murphy goto out_free_data; 101752f325f4SRobin Murphy 101852f325f4SRobin Murphy /* Ensure the empty pgd is visible before TRANSTAB can be written */ 101952f325f4SRobin Murphy wmb(); 102052f325f4SRobin Murphy 102152f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | 1022d08d42deSRob Herring ARM_MALI_LPAE_TTBR_READ_INNER | 1023d08d42deSRob Herring ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; 102452f325f4SRobin Murphy return &data->iop; 1025d08d42deSRob Herring 102652f325f4SRobin Murphy out_free_data: 102752f325f4SRobin Murphy kfree(data); 102852f325f4SRobin Murphy return NULL; 1029d08d42deSRob Herring } 1030d08d42deSRob Herring 1031e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1032e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 1033e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1034e1d3c0fdSWill Deacon }; 1035e1d3c0fdSWill Deacon 1036e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1037e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 1038e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1039e1d3c0fdSWill Deacon }; 1040e1d3c0fdSWill Deacon 1041e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1042e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1043e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1044e1d3c0fdSWill Deacon }; 1045e1d3c0fdSWill Deacon 1046e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1047e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1048e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1049e1d3c0fdSWill Deacon }; 1050fe4b991dSWill Deacon 1051d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1052d08d42deSRob Herring .alloc = arm_mali_lpae_alloc_pgtable, 1053d08d42deSRob Herring .free = arm_lpae_free_pgtable, 1054d08d42deSRob Herring }; 1055d08d42deSRob Herring 1056fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1057fe4b991dSWill Deacon 1058b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata; 1059fe4b991dSWill Deacon 1060b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie) 1061fe4b991dSWill Deacon { 1062fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1063fe4b991dSWill Deacon } 1064fe4b991dSWill Deacon 1065b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size, 1066b5813c16SRobin Murphy size_t granule, void *cookie) 1067fe4b991dSWill Deacon { 1068fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1069fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1070fe4b991dSWill Deacon } 1071fe4b991dSWill Deacon 1072b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, 1073b5813c16SRobin Murphy unsigned long iova, size_t granule, 1074b5813c16SRobin Murphy void *cookie) 107510b7a7d9SWill Deacon { 1076abfd6fe0SWill Deacon dummy_tlb_flush(iova, granule, granule, cookie); 107710b7a7d9SWill Deacon } 107810b7a7d9SWill Deacon 1079298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = { 1080fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 108110b7a7d9SWill Deacon .tlb_flush_walk = dummy_tlb_flush, 108210b7a7d9SWill Deacon .tlb_flush_leaf = dummy_tlb_flush, 1083abfd6fe0SWill Deacon .tlb_add_page = dummy_tlb_add_page, 1084fe4b991dSWill Deacon }; 1085fe4b991dSWill Deacon 1086fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1087fe4b991dSWill Deacon { 1088fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1089fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1090fe4b991dSWill Deacon 1091fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1092fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 10935fb190b0SRobin Murphy pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", 1094c79278c1SRobin Murphy ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), 10955fb190b0SRobin Murphy ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); 1096fe4b991dSWill Deacon } 1097fe4b991dSWill Deacon 1098fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1099fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1100fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1101fe4b991dSWill Deacon selftest_running = false; \ 1102fe4b991dSWill Deacon -EFAULT; \ 1103fe4b991dSWill Deacon }) 1104fe4b991dSWill Deacon 1105fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1106fe4b991dSWill Deacon { 11079062c1d0SChristophe JAILLET static const enum io_pgtable_fmt fmts[] __initconst = { 1108fe4b991dSWill Deacon ARM_64_LPAE_S1, 1109fe4b991dSWill Deacon ARM_64_LPAE_S2, 1110fe4b991dSWill Deacon }; 1111fe4b991dSWill Deacon 1112fe4b991dSWill Deacon int i, j; 1113fe4b991dSWill Deacon unsigned long iova; 1114fe4b991dSWill Deacon size_t size; 1115fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1116fe4b991dSWill Deacon 1117fe4b991dSWill Deacon selftest_running = true; 1118fe4b991dSWill Deacon 1119fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1120fe4b991dSWill Deacon cfg_cookie = cfg; 1121fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1122fe4b991dSWill Deacon if (!ops) { 1123fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1124fe4b991dSWill Deacon return -ENOMEM; 1125fe4b991dSWill Deacon } 1126fe4b991dSWill Deacon 1127fe4b991dSWill Deacon /* 1128fe4b991dSWill Deacon * Initial sanity checks. 1129fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1130fe4b991dSWill Deacon */ 1131fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1132fe4b991dSWill Deacon return __FAIL(ops, i); 1133fe4b991dSWill Deacon 1134fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1135fe4b991dSWill Deacon return __FAIL(ops, i); 1136fe4b991dSWill Deacon 1137fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1138fe4b991dSWill Deacon return __FAIL(ops, i); 1139fe4b991dSWill Deacon 1140fe4b991dSWill Deacon /* 1141fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1142fe4b991dSWill Deacon */ 1143fe4b991dSWill Deacon iova = 0; 11444ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1145fe4b991dSWill Deacon size = 1UL << j; 1146fe4b991dSWill Deacon 1147fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1148fe4b991dSWill Deacon IOMMU_WRITE | 1149fe4b991dSWill Deacon IOMMU_NOEXEC | 1150f34ce7a7SBaolin Wang IOMMU_CACHE, GFP_KERNEL)) 1151fe4b991dSWill Deacon return __FAIL(ops, i); 1152fe4b991dSWill Deacon 1153fe4b991dSWill Deacon /* Overlapping mappings */ 1154fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1155f34ce7a7SBaolin Wang IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL)) 1156fe4b991dSWill Deacon return __FAIL(ops, i); 1157fe4b991dSWill Deacon 1158fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1159fe4b991dSWill Deacon return __FAIL(ops, i); 1160fe4b991dSWill Deacon 1161fe4b991dSWill Deacon iova += SZ_1G; 1162fe4b991dSWill Deacon } 1163fe4b991dSWill Deacon 1164fe4b991dSWill Deacon /* Partial unmap */ 1165fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1166a2d3a382SWill Deacon if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) 1167fe4b991dSWill Deacon return __FAIL(ops, i); 1168fe4b991dSWill Deacon 1169fe4b991dSWill Deacon /* Remap of partial unmap */ 1170f34ce7a7SBaolin Wang if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL)) 1171fe4b991dSWill Deacon return __FAIL(ops, i); 1172fe4b991dSWill Deacon 1173fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1174fe4b991dSWill Deacon return __FAIL(ops, i); 1175fe4b991dSWill Deacon 1176fe4b991dSWill Deacon /* Full unmap */ 1177fe4b991dSWill Deacon iova = 0; 1178f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1179fe4b991dSWill Deacon size = 1UL << j; 1180fe4b991dSWill Deacon 1181a2d3a382SWill Deacon if (ops->unmap(ops, iova, size, NULL) != size) 1182fe4b991dSWill Deacon return __FAIL(ops, i); 1183fe4b991dSWill Deacon 1184fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1185fe4b991dSWill Deacon return __FAIL(ops, i); 1186fe4b991dSWill Deacon 1187fe4b991dSWill Deacon /* Remap full block */ 1188f34ce7a7SBaolin Wang if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL)) 1189fe4b991dSWill Deacon return __FAIL(ops, i); 1190fe4b991dSWill Deacon 1191fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1192fe4b991dSWill Deacon return __FAIL(ops, i); 1193fe4b991dSWill Deacon 1194fe4b991dSWill Deacon iova += SZ_1G; 1195fe4b991dSWill Deacon } 1196fe4b991dSWill Deacon 1197fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1198fe4b991dSWill Deacon } 1199fe4b991dSWill Deacon 1200fe4b991dSWill Deacon selftest_running = false; 1201fe4b991dSWill Deacon return 0; 1202fe4b991dSWill Deacon } 1203fe4b991dSWill Deacon 1204fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1205fe4b991dSWill Deacon { 12069062c1d0SChristophe JAILLET static const unsigned long pgsize[] __initconst = { 1207fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1208fe4b991dSWill Deacon SZ_16K | SZ_32M, 1209fe4b991dSWill Deacon SZ_64K | SZ_512M, 1210fe4b991dSWill Deacon }; 1211fe4b991dSWill Deacon 12129062c1d0SChristophe JAILLET static const unsigned int ias[] __initconst = { 1213fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1214fe4b991dSWill Deacon }; 1215fe4b991dSWill Deacon 1216fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1217fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1218fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1219fe4b991dSWill Deacon .oas = 48, 12204f41845bSWill Deacon .coherent_walk = true, 1221fe4b991dSWill Deacon }; 1222fe4b991dSWill Deacon 1223fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1224fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1225fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1226fe4b991dSWill Deacon cfg.ias = ias[j]; 1227fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1228fe4b991dSWill Deacon pgsize[i], ias[j]); 1229fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1230fe4b991dSWill Deacon fail++; 1231fe4b991dSWill Deacon else 1232fe4b991dSWill Deacon pass++; 1233fe4b991dSWill Deacon } 1234fe4b991dSWill Deacon } 1235fe4b991dSWill Deacon 1236fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1237fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1238fe4b991dSWill Deacon } 1239fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1240fe4b991dSWill Deacon #endif 1241