1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e1d3c0fdSWill Deacon /* 3e1d3c0fdSWill Deacon * CPU-agnostic ARM page table allocator. 4e1d3c0fdSWill Deacon * 5e1d3c0fdSWill Deacon * Copyright (C) 2014 ARM Limited 6e1d3c0fdSWill Deacon * 7e1d3c0fdSWill Deacon * Author: Will Deacon <will.deacon@arm.com> 8e1d3c0fdSWill Deacon */ 9e1d3c0fdSWill Deacon 10e1d3c0fdSWill Deacon #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt 11e1d3c0fdSWill Deacon 122c3d273eSRobin Murphy #include <linux/atomic.h> 136c89928fSRobin Murphy #include <linux/bitops.h> 14b77cf11fSRob Herring #include <linux/io-pgtable.h> 15e1d3c0fdSWill Deacon #include <linux/kernel.h> 16e1d3c0fdSWill Deacon #include <linux/sizes.h> 17e1d3c0fdSWill Deacon #include <linux/slab.h> 18e1d3c0fdSWill Deacon #include <linux/types.h> 198f6aff98SLada Trimasova #include <linux/dma-mapping.h> 20e1d3c0fdSWill Deacon 2187a91b15SRobin Murphy #include <asm/barrier.h> 2287a91b15SRobin Murphy 236c89928fSRobin Murphy #define ARM_LPAE_MAX_ADDR_BITS 52 24e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 25e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS 4 26e1d3c0fdSWill Deacon 27e1d3c0fdSWill Deacon /* Struct accessors */ 28e1d3c0fdSWill Deacon #define io_pgtable_to_data(x) \ 29e1d3c0fdSWill Deacon container_of((x), struct arm_lpae_io_pgtable, iop) 30e1d3c0fdSWill Deacon 31e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x) \ 32e1d3c0fdSWill Deacon io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 33e1d3c0fdSWill Deacon 34e1d3c0fdSWill Deacon /* 35e1d3c0fdSWill Deacon * Calculate the right shift amount to get to the portion describing level l 36e1d3c0fdSWill Deacon * in a virtual address mapped by the pagetable in d. 37e1d3c0fdSWill Deacon */ 38e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d) \ 39594ab90fSRobin Murphy (((ARM_LPAE_MAX_LEVELS - 1 - (l)) * (d)->bits_per_level) + \ 40594ab90fSRobin Murphy (d)->pg_shift) 41e1d3c0fdSWill Deacon 4206c610e8SRobin Murphy #define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift) 43*c79278c1SRobin Murphy #define ARM_LPAE_PGD_SIZE(d) \ 44*c79278c1SRobin Murphy (sizeof(arm_lpae_iopte) << (d)->pgd_bits) 45e1d3c0fdSWill Deacon 46e1d3c0fdSWill Deacon /* 47e1d3c0fdSWill Deacon * Calculate the index at level l used to map virtual address a using the 48e1d3c0fdSWill Deacon * pagetable in d. 49e1d3c0fdSWill Deacon */ 50e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d) \ 51*c79278c1SRobin Murphy ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0) 52e1d3c0fdSWill Deacon 53e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d) \ 54367bd978SWill Deacon (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 55e1d3c0fdSWill Deacon ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 56e1d3c0fdSWill Deacon 57e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */ 58e1d3c0fdSWill Deacon #define ARM_LPAE_BLOCK_SIZE(l,d) \ 59022f4e4fSRobin Murphy (1ULL << (ilog2(sizeof(arm_lpae_iopte)) + \ 60e1d3c0fdSWill Deacon ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) 61e1d3c0fdSWill Deacon 62e1d3c0fdSWill Deacon /* Page table bits */ 63e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT 0 64e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK 0x3 65e1d3c0fdSWill Deacon 66e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK 1 67e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE 3 68e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE 3 69e1d3c0fdSWill Deacon 706c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12) 716c89928fSRobin Murphy 72c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 73e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 74e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 75e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 76e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 77e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 78c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 79e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 80e1d3c0fdSWill Deacon 81e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 82e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */ 83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 84e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 85e1d3c0fdSWill Deacon ARM_LPAE_PTE_ATTR_HI_MASK) 862c3d273eSRobin Murphy /* Software bit for solving coherency races */ 872c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55) 88e1d3c0fdSWill Deacon 89e1d3c0fdSWill Deacon /* Stage-1 PTE */ 90e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 91e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 93e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 94e1d3c0fdSWill Deacon 95e1d3c0fdSWill Deacon /* Stage-2 PTE */ 96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 98e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 99e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 102e1d3c0fdSWill Deacon 103e1d3c0fdSWill Deacon /* Register bits */ 104e1d3c0fdSWill Deacon #define ARM_32_LPAE_TCR_EAE (1 << 31) 105e1d3c0fdSWill Deacon #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) 106e1d3c0fdSWill Deacon 10763979b8dSWill Deacon #define ARM_LPAE_TCR_EPD1 (1 << 23) 10863979b8dSWill Deacon 109e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_4K (0 << 14) 110e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_64K (1 << 14) 111e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_16K (2 << 14) 112e1d3c0fdSWill Deacon 113e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_SHIFT 12 114e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_MASK 0x3 115e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS 0 116e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS 2 117e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS 3 118e1d3c0fdSWill Deacon 119e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_ORGN0_SHIFT 10 120e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IRGN0_SHIFT 8 121e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_MASK 0x3 122e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC 0 123e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA 1 124e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT 2 125e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB 3 126e1d3c0fdSWill Deacon 127e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_SHIFT 6 128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_MASK 0x3 129e1d3c0fdSWill Deacon 130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT 0 131e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SZ_MASK 0xf 132e1d3c0fdSWill Deacon 133e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_SHIFT 16 134e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_MASK 0x7 135e1d3c0fdSWill Deacon 136e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_SHIFT 32 137e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_MASK 0x7 138e1d3c0fdSWill Deacon 139e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 140e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 141e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 142e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 143e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 144e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 1456c89928fSRobin Murphy #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL 146e1d3c0fdSWill Deacon 147e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 148e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK 0xff 149e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 150e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC 0x44 15190ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4 152e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 153e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 154e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 155e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 15690ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3 157e1d3c0fdSWill Deacon 158d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0) 159d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2) 160d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4) 161d08d42deSRob Herring 16252f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL 16352f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL 16452f325f4SRobin Murphy 165e1d3c0fdSWill Deacon /* IOPTE accessors */ 1666c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d)) 167e1d3c0fdSWill Deacon 168e1d3c0fdSWill Deacon #define iopte_type(pte,l) \ 169e1d3c0fdSWill Deacon (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 170e1d3c0fdSWill Deacon 171e1d3c0fdSWill Deacon #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 172e1d3c0fdSWill Deacon 173e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable { 174e1d3c0fdSWill Deacon struct io_pgtable iop; 175e1d3c0fdSWill Deacon 176*c79278c1SRobin Murphy int pgd_bits; 177594ab90fSRobin Murphy int start_level; 178e1d3c0fdSWill Deacon unsigned long pg_shift; 179e1d3c0fdSWill Deacon unsigned long bits_per_level; 180e1d3c0fdSWill Deacon 181e1d3c0fdSWill Deacon void *pgd; 182e1d3c0fdSWill Deacon }; 183e1d3c0fdSWill Deacon 184e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte; 185e1d3c0fdSWill Deacon 186d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl, 187d08d42deSRob Herring enum io_pgtable_fmt fmt) 188d08d42deSRob Herring { 189d08d42deSRob Herring if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE) 190d08d42deSRob Herring return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE; 191d08d42deSRob Herring 192d08d42deSRob Herring return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK; 193d08d42deSRob Herring } 194d08d42deSRob Herring 1956c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr, 1966c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 1976c89928fSRobin Murphy { 1986c89928fSRobin Murphy arm_lpae_iopte pte = paddr; 1996c89928fSRobin Murphy 2006c89928fSRobin Murphy /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */ 2016c89928fSRobin Murphy return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK; 2026c89928fSRobin Murphy } 2036c89928fSRobin Murphy 2046c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte, 2056c89928fSRobin Murphy struct arm_lpae_io_pgtable *data) 2066c89928fSRobin Murphy { 20778688059SRobin Murphy u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK; 2086c89928fSRobin Murphy 2096c89928fSRobin Murphy if (data->pg_shift < 16) 2106c89928fSRobin Murphy return paddr; 2116c89928fSRobin Murphy 2126c89928fSRobin Murphy /* Rotate the packed high-order bits back to the top */ 2136c89928fSRobin Murphy return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4); 2146c89928fSRobin Murphy } 2156c89928fSRobin Murphy 216fe4b991dSWill Deacon static bool selftest_running = false; 217fe4b991dSWill Deacon 218ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages) 219f8d54961SRobin Murphy { 220ffcb6d16SRobin Murphy return (dma_addr_t)virt_to_phys(pages); 221f8d54961SRobin Murphy } 222f8d54961SRobin Murphy 223f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp, 224f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 225f8d54961SRobin Murphy { 226f8d54961SRobin Murphy struct device *dev = cfg->iommu_dev; 2274b123757SRobin Murphy int order = get_order(size); 2284b123757SRobin Murphy struct page *p; 229f8d54961SRobin Murphy dma_addr_t dma; 2304b123757SRobin Murphy void *pages; 231f8d54961SRobin Murphy 2324b123757SRobin Murphy VM_BUG_ON((gfp & __GFP_HIGHMEM)); 233fac83d29SJean-Philippe Brucker p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE, 234fac83d29SJean-Philippe Brucker gfp | __GFP_ZERO, order); 2354b123757SRobin Murphy if (!p) 236f8d54961SRobin Murphy return NULL; 237f8d54961SRobin Murphy 2384b123757SRobin Murphy pages = page_address(p); 2394f41845bSWill Deacon if (!cfg->coherent_walk) { 240f8d54961SRobin Murphy dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE); 241f8d54961SRobin Murphy if (dma_mapping_error(dev, dma)) 242f8d54961SRobin Murphy goto out_free; 243f8d54961SRobin Murphy /* 244f8d54961SRobin Murphy * We depend on the IOMMU being able to work with any physical 245ffcb6d16SRobin Murphy * address directly, so if the DMA layer suggests otherwise by 246ffcb6d16SRobin Murphy * translating or truncating them, that bodes very badly... 247f8d54961SRobin Murphy */ 248ffcb6d16SRobin Murphy if (dma != virt_to_phys(pages)) 249f8d54961SRobin Murphy goto out_unmap; 250f8d54961SRobin Murphy } 251f8d54961SRobin Murphy 252f8d54961SRobin Murphy return pages; 253f8d54961SRobin Murphy 254f8d54961SRobin Murphy out_unmap: 255f8d54961SRobin Murphy dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n"); 256f8d54961SRobin Murphy dma_unmap_single(dev, dma, size, DMA_TO_DEVICE); 257f8d54961SRobin Murphy out_free: 2584b123757SRobin Murphy __free_pages(p, order); 259f8d54961SRobin Murphy return NULL; 260f8d54961SRobin Murphy } 261f8d54961SRobin Murphy 262f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size, 263f8d54961SRobin Murphy struct io_pgtable_cfg *cfg) 264f8d54961SRobin Murphy { 2654f41845bSWill Deacon if (!cfg->coherent_walk) 266ffcb6d16SRobin Murphy dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages), 267f8d54961SRobin Murphy size, DMA_TO_DEVICE); 2684b123757SRobin Murphy free_pages((unsigned long)pages, get_order(size)); 269f8d54961SRobin Murphy } 270f8d54961SRobin Murphy 2712c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, 2722c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2732c3d273eSRobin Murphy { 2742c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 2752c3d273eSRobin Murphy sizeof(*ptep), DMA_TO_DEVICE); 2762c3d273eSRobin Murphy } 2772c3d273eSRobin Murphy 278f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 27987a91b15SRobin Murphy struct io_pgtable_cfg *cfg) 280f8d54961SRobin Murphy { 281f8d54961SRobin Murphy *ptep = pte; 282f8d54961SRobin Murphy 2834f41845bSWill Deacon if (!cfg->coherent_walk) 2842c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 285f8d54961SRobin Murphy } 286f8d54961SRobin Murphy 287193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 2883951c41aSWill Deacon struct iommu_iotlb_gather *gather, 289cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 290cf27ec93SWill Deacon arm_lpae_iopte *ptep); 291cf27ec93SWill Deacon 292fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 293fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 294fb3a9579SRobin Murphy int lvl, arm_lpae_iopte *ptep) 295fb3a9579SRobin Murphy { 296fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 297fb3a9579SRobin Murphy 298fb3a9579SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 299fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_NS; 300fb3a9579SRobin Murphy 301d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) 302fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 303fb3a9579SRobin Murphy else 304fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 305fb3a9579SRobin Murphy 306d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE) 307d08d42deSRob Herring pte |= ARM_LPAE_PTE_AF; 308d08d42deSRob Herring pte |= ARM_LPAE_PTE_SH_IS; 3096c89928fSRobin Murphy pte |= paddr_to_iopte(paddr, data); 310fb3a9579SRobin Murphy 311fb3a9579SRobin Murphy __arm_lpae_set_pte(ptep, pte, &data->iop.cfg); 312fb3a9579SRobin Murphy } 313fb3a9579SRobin Murphy 314e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 315e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 316e1d3c0fdSWill Deacon arm_lpae_iopte prot, int lvl, 317e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 318e1d3c0fdSWill Deacon { 319fb3a9579SRobin Murphy arm_lpae_iopte pte = *ptep; 320e1d3c0fdSWill Deacon 321d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) { 322cf27ec93SWill Deacon /* We require an unmap first */ 323fe4b991dSWill Deacon WARN_ON(!selftest_running); 324e1d3c0fdSWill Deacon return -EEXIST; 325fb3a9579SRobin Murphy } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) { 326cf27ec93SWill Deacon /* 327cf27ec93SWill Deacon * We need to unmap and free the old table before 328cf27ec93SWill Deacon * overwriting it with a block entry. 329cf27ec93SWill Deacon */ 330cf27ec93SWill Deacon arm_lpae_iopte *tblp; 331cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 332cf27ec93SWill Deacon 333cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 3343951c41aSWill Deacon if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) { 3353951c41aSWill Deacon WARN_ON(1); 336cf27ec93SWill Deacon return -EINVAL; 337fe4b991dSWill Deacon } 3383951c41aSWill Deacon } 339e1d3c0fdSWill Deacon 340fb3a9579SRobin Murphy __arm_lpae_init_pte(data, paddr, prot, lvl, ptep); 341e1d3c0fdSWill Deacon return 0; 342e1d3c0fdSWill Deacon } 343e1d3c0fdSWill Deacon 344fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 345fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3462c3d273eSRobin Murphy arm_lpae_iopte curr, 347fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 348fb3a9579SRobin Murphy { 3492c3d273eSRobin Murphy arm_lpae_iopte old, new; 350fb3a9579SRobin Murphy 351fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 352fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 353fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 354fb3a9579SRobin Murphy 35577f34458SWill Deacon /* 35677f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 35777f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 35877f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 35977f34458SWill Deacon */ 36077f34458SWill Deacon dma_wmb(); 3612c3d273eSRobin Murphy 3622c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3632c3d273eSRobin Murphy 3644f41845bSWill Deacon if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) 3652c3d273eSRobin Murphy return old; 3662c3d273eSRobin Murphy 3672c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 3682c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 3692c3d273eSRobin Murphy if (old == curr) 3702c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3712c3d273eSRobin Murphy 3722c3d273eSRobin Murphy return old; 373fb3a9579SRobin Murphy } 374fb3a9579SRobin Murphy 375e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 376e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 377e1d3c0fdSWill Deacon int lvl, arm_lpae_iopte *ptep) 378e1d3c0fdSWill Deacon { 379e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 380e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3812c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 382f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 383e1d3c0fdSWill Deacon 384e1d3c0fdSWill Deacon /* Find our entry at the current level */ 385e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 386e1d3c0fdSWill Deacon 387e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 388f7b90d2cSRobin Murphy if (size == block_size) 389e1d3c0fdSWill Deacon return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 390e1d3c0fdSWill Deacon 391e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 392e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 393e1d3c0fdSWill Deacon return -EINVAL; 394e1d3c0fdSWill Deacon 395e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3962c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 397e1d3c0fdSWill Deacon if (!pte) { 3982c3d273eSRobin Murphy cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg); 399e1d3c0fdSWill Deacon if (!cptep) 400e1d3c0fdSWill Deacon return -ENOMEM; 401e1d3c0fdSWill Deacon 4022c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 4032c3d273eSRobin Murphy if (pte) 4042c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 4054f41845bSWill Deacon } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 4062c3d273eSRobin Murphy __arm_lpae_sync_pte(ptep, cfg); 4072c3d273eSRobin Murphy } 4082c3d273eSRobin Murphy 409d08d42deSRob Herring if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { 410e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 4112c3d273eSRobin Murphy } else if (pte) { 412ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 413ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 414ed46e66cSOleksandr Tyshchenko return -EEXIST; 415e1d3c0fdSWill Deacon } 416e1d3c0fdSWill Deacon 417e1d3c0fdSWill Deacon /* Rinse, repeat */ 418e1d3c0fdSWill Deacon return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 419e1d3c0fdSWill Deacon } 420e1d3c0fdSWill Deacon 421e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 422e1d3c0fdSWill Deacon int prot) 423e1d3c0fdSWill Deacon { 424e1d3c0fdSWill Deacon arm_lpae_iopte pte; 425e1d3c0fdSWill Deacon 426e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 427e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 428e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 429e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 430e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 431e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 432e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 433e1d3c0fdSWill Deacon } else { 434e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 435e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 436e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 437e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 438e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 439d08d42deSRob Herring } 440d08d42deSRob Herring 441d08d42deSRob Herring /* 442d08d42deSRob Herring * Note that this logic is structured to accommodate Mali LPAE 443d08d42deSRob Herring * having stage-1-like attributes but stage-2-like permissions. 444d08d42deSRob Herring */ 445d08d42deSRob Herring if (data->iop.fmt == ARM_64_LPAE_S2 || 446d08d42deSRob Herring data->iop.fmt == ARM_32_LPAE_S2) { 447fb948251SRobin Murphy if (prot & IOMMU_MMIO) 448fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 449fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 450e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 451e1d3c0fdSWill Deacon else 452e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 453d08d42deSRob Herring } else { 454d08d42deSRob Herring if (prot & IOMMU_MMIO) 455d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 456d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 457d08d42deSRob Herring else if (prot & IOMMU_CACHE) 458d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 459d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 46090ec7a76SVivek Gautam else if (prot & IOMMU_QCOM_SYS_CACHE) 46190ec7a76SVivek Gautam pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 46290ec7a76SVivek Gautam << ARM_LPAE_PTE_ATTRINDX_SHIFT); 463e1d3c0fdSWill Deacon } 464e1d3c0fdSWill Deacon 465e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 466e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 467e1d3c0fdSWill Deacon 468e1d3c0fdSWill Deacon return pte; 469e1d3c0fdSWill Deacon } 470e1d3c0fdSWill Deacon 471e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 472e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, int iommu_prot) 473e1d3c0fdSWill Deacon { 474e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 475f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 476e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 477594ab90fSRobin Murphy int ret, lvl = data->start_level; 478e1d3c0fdSWill Deacon arm_lpae_iopte prot; 479e1d3c0fdSWill Deacon 480e1d3c0fdSWill Deacon /* If no access, then nothing to do */ 481e1d3c0fdSWill Deacon if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 482e1d3c0fdSWill Deacon return 0; 483e1d3c0fdSWill Deacon 484f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 485f7b90d2cSRobin Murphy return -EINVAL; 486f7b90d2cSRobin Murphy 48767f3e53dSRobin Murphy if (WARN_ON(iova >> data->iop.cfg.ias || paddr >> data->iop.cfg.oas)) 48876557391SRobin Murphy return -ERANGE; 48976557391SRobin Murphy 490e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 49187a91b15SRobin Murphy ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 49287a91b15SRobin Murphy /* 49387a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 49487a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 49587a91b15SRobin Murphy */ 49687a91b15SRobin Murphy wmb(); 49787a91b15SRobin Murphy 49887a91b15SRobin Murphy return ret; 499e1d3c0fdSWill Deacon } 500e1d3c0fdSWill Deacon 501e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 502e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 503e1d3c0fdSWill Deacon { 504e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 505e1d3c0fdSWill Deacon unsigned long table_size; 506e1d3c0fdSWill Deacon 507594ab90fSRobin Murphy if (lvl == data->start_level) 508*c79278c1SRobin Murphy table_size = ARM_LPAE_PGD_SIZE(data); 509e1d3c0fdSWill Deacon else 51006c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 511e1d3c0fdSWill Deacon 512e1d3c0fdSWill Deacon start = ptep; 51312c2ab09SWill Deacon 51412c2ab09SWill Deacon /* Only leaf entries at the last level */ 51512c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 51612c2ab09SWill Deacon end = ptep; 51712c2ab09SWill Deacon else 518e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 519e1d3c0fdSWill Deacon 520e1d3c0fdSWill Deacon while (ptep != end) { 521e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 522e1d3c0fdSWill Deacon 523d08d42deSRob Herring if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) 524e1d3c0fdSWill Deacon continue; 525e1d3c0fdSWill Deacon 526e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 527e1d3c0fdSWill Deacon } 528e1d3c0fdSWill Deacon 529f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 530e1d3c0fdSWill Deacon } 531e1d3c0fdSWill Deacon 532e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 533e1d3c0fdSWill Deacon { 534e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 535e1d3c0fdSWill Deacon 536594ab90fSRobin Murphy __arm_lpae_free_pgtable(data, data->start_level, data->pgd); 537e1d3c0fdSWill Deacon kfree(data); 538e1d3c0fdSWill Deacon } 539e1d3c0fdSWill Deacon 540193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 5413951c41aSWill Deacon struct iommu_iotlb_gather *gather, 542e1d3c0fdSWill Deacon unsigned long iova, size_t size, 543fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 544fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 545e1d3c0fdSWill Deacon { 546fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 547fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 548e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 549fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 550fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 551fb3a9579SRobin Murphy int i, unmap_idx = -1; 552e1d3c0fdSWill Deacon 553fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 554fb3a9579SRobin Murphy return 0; 555e1d3c0fdSWill Deacon 556fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 557fb3a9579SRobin Murphy if (!tablep) 558fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 559e1d3c0fdSWill Deacon 560fb3a9579SRobin Murphy if (size == split_sz) 561fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 562fb3a9579SRobin Murphy 5636c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 564fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 565fb3a9579SRobin Murphy 566fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 567e1d3c0fdSWill Deacon /* Unmap! */ 568fb3a9579SRobin Murphy if (i == unmap_idx) 569e1d3c0fdSWill Deacon continue; 570e1d3c0fdSWill Deacon 571fb3a9579SRobin Murphy __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]); 572e1d3c0fdSWill Deacon } 573e1d3c0fdSWill Deacon 5742c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5752c3d273eSRobin Murphy if (pte != blk_pte) { 5762c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5772c3d273eSRobin Murphy /* 5782c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5792c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5802c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5812c3d273eSRobin Murphy */ 5822c3d273eSRobin Murphy if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE) 5832c3d273eSRobin Murphy return 0; 5842c3d273eSRobin Murphy 5852c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 58685c7a0f1SRobin Murphy } else if (unmap_idx >= 0) { 5873951c41aSWill Deacon io_pgtable_tlb_add_page(&data->iop, gather, iova, size); 588e1d3c0fdSWill Deacon return size; 589e1d3c0fdSWill Deacon } 590e1d3c0fdSWill Deacon 5913951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep); 59285c7a0f1SRobin Murphy } 59385c7a0f1SRobin Murphy 594193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 5953951c41aSWill Deacon struct iommu_iotlb_gather *gather, 596e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 597e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 598e1d3c0fdSWill Deacon { 599e1d3c0fdSWill Deacon arm_lpae_iopte pte; 600507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 601e1d3c0fdSWill Deacon 6022eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 6032eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 6042eb97c78SRobin Murphy return 0; 6052eb97c78SRobin Murphy 606e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6072c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 6082eb97c78SRobin Murphy if (WARN_ON(!pte)) 609e1d3c0fdSWill Deacon return 0; 610e1d3c0fdSWill Deacon 611e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 612fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 613507e4c9dSRobin Murphy __arm_lpae_set_pte(ptep, 0, &iop->cfg); 614e1d3c0fdSWill Deacon 615d08d42deSRob Herring if (!iopte_leaf(pte, lvl, iop->fmt)) { 616e1d3c0fdSWill Deacon /* Also flush any partial walks */ 61710b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(iop, iova, size, 61810b7a7d9SWill Deacon ARM_LPAE_GRANULE(data)); 619e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 620e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 621b6b65ca2SZhen Lei } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { 622b6b65ca2SZhen Lei /* 623b6b65ca2SZhen Lei * Order the PTE update against queueing the IOVA, to 624b6b65ca2SZhen Lei * guarantee that a flush callback from a different CPU 625b6b65ca2SZhen Lei * has observed it before the TLBIALL can be issued. 626b6b65ca2SZhen Lei */ 627b6b65ca2SZhen Lei smp_wmb(); 628e1d3c0fdSWill Deacon } else { 6293951c41aSWill Deacon io_pgtable_tlb_add_page(iop, gather, iova, size); 630e1d3c0fdSWill Deacon } 631e1d3c0fdSWill Deacon 632e1d3c0fdSWill Deacon return size; 633d08d42deSRob Herring } else if (iopte_leaf(pte, lvl, iop->fmt)) { 634e1d3c0fdSWill Deacon /* 635e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 636e1d3c0fdSWill Deacon * minus the part we want to unmap 637e1d3c0fdSWill Deacon */ 6383951c41aSWill Deacon return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, 639fb3a9579SRobin Murphy lvl + 1, ptep); 640e1d3c0fdSWill Deacon } 641e1d3c0fdSWill Deacon 642e1d3c0fdSWill Deacon /* Keep on walkin' */ 643e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 6443951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep); 645e1d3c0fdSWill Deacon } 646e1d3c0fdSWill Deacon 647193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 648a2d3a382SWill Deacon size_t size, struct iommu_iotlb_gather *gather) 649e1d3c0fdSWill Deacon { 650e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 651f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 652e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 653e1d3c0fdSWill Deacon 654f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 655f7b90d2cSRobin Murphy return 0; 656f7b90d2cSRobin Murphy 65767f3e53dSRobin Murphy if (WARN_ON(iova >> data->iop.cfg.ias)) 65876557391SRobin Murphy return 0; 65976557391SRobin Murphy 660594ab90fSRobin Murphy return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep); 661e1d3c0fdSWill Deacon } 662e1d3c0fdSWill Deacon 663e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 664e1d3c0fdSWill Deacon unsigned long iova) 665e1d3c0fdSWill Deacon { 666e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 667e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 668594ab90fSRobin Murphy int lvl = data->start_level; 669e1d3c0fdSWill Deacon 670e1d3c0fdSWill Deacon do { 671e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 672e1d3c0fdSWill Deacon if (!ptep) 673e1d3c0fdSWill Deacon return 0; 674e1d3c0fdSWill Deacon 675e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6762c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6772c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 678e1d3c0fdSWill Deacon 679e1d3c0fdSWill Deacon /* Valid entry? */ 680e1d3c0fdSWill Deacon if (!pte) 681e1d3c0fdSWill Deacon return 0; 682e1d3c0fdSWill Deacon 683e1d3c0fdSWill Deacon /* Leaf entry? */ 684d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) 685e1d3c0fdSWill Deacon goto found_translation; 686e1d3c0fdSWill Deacon 687e1d3c0fdSWill Deacon /* Take it to the next level */ 688e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 689e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 690e1d3c0fdSWill Deacon 691e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 692e1d3c0fdSWill Deacon return 0; 693e1d3c0fdSWill Deacon 694e1d3c0fdSWill Deacon found_translation: 6957c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 6966c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 697e1d3c0fdSWill Deacon } 698e1d3c0fdSWill Deacon 699e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 700e1d3c0fdSWill Deacon { 7016c89928fSRobin Murphy unsigned long granule, page_sizes; 7026c89928fSRobin Murphy unsigned int max_addr_bits = 48; 703e1d3c0fdSWill Deacon 704e1d3c0fdSWill Deacon /* 705e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 706e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 707e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 708e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 709e1d3c0fdSWill Deacon * chosen granule. 710e1d3c0fdSWill Deacon */ 711e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 712e1d3c0fdSWill Deacon granule = PAGE_SIZE; 713e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 714e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 715e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 716e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 717e1d3c0fdSWill Deacon else 718e1d3c0fdSWill Deacon granule = 0; 719e1d3c0fdSWill Deacon 720e1d3c0fdSWill Deacon switch (granule) { 721e1d3c0fdSWill Deacon case SZ_4K: 7226c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 723e1d3c0fdSWill Deacon break; 724e1d3c0fdSWill Deacon case SZ_16K: 7256c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 726e1d3c0fdSWill Deacon break; 727e1d3c0fdSWill Deacon case SZ_64K: 7286c89928fSRobin Murphy max_addr_bits = 52; 7296c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7306c89928fSRobin Murphy if (cfg->oas > 48) 7316c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 732e1d3c0fdSWill Deacon break; 733e1d3c0fdSWill Deacon default: 7346c89928fSRobin Murphy page_sizes = 0; 735e1d3c0fdSWill Deacon } 7366c89928fSRobin Murphy 7376c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7386c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7396c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 740e1d3c0fdSWill Deacon } 741e1d3c0fdSWill Deacon 742e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 743e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 744e1d3c0fdSWill Deacon { 745*c79278c1SRobin Murphy unsigned long va_bits; 746e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 747594ab90fSRobin Murphy int levels; 748e1d3c0fdSWill Deacon 749e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 750e1d3c0fdSWill Deacon 751e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 752e1d3c0fdSWill Deacon return NULL; 753e1d3c0fdSWill Deacon 754e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 755e1d3c0fdSWill Deacon return NULL; 756e1d3c0fdSWill Deacon 757e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 758e1d3c0fdSWill Deacon return NULL; 759e1d3c0fdSWill Deacon 760ffcb6d16SRobin Murphy if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) { 761ffcb6d16SRobin Murphy dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n"); 762ffcb6d16SRobin Murphy return NULL; 763ffcb6d16SRobin Murphy } 764ffcb6d16SRobin Murphy 765e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 766e1d3c0fdSWill Deacon if (!data) 767e1d3c0fdSWill Deacon return NULL; 768e1d3c0fdSWill Deacon 769e1d3c0fdSWill Deacon data->pg_shift = __ffs(cfg->pgsize_bitmap); 770e1d3c0fdSWill Deacon data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); 771e1d3c0fdSWill Deacon 772e1d3c0fdSWill Deacon va_bits = cfg->ias - data->pg_shift; 773594ab90fSRobin Murphy levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 774594ab90fSRobin Murphy data->start_level = ARM_LPAE_MAX_LEVELS - levels; 775e1d3c0fdSWill Deacon 776e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 777*c79278c1SRobin Murphy data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); 778e1d3c0fdSWill Deacon 779e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 780e1d3c0fdSWill Deacon .map = arm_lpae_map, 781e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 782e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 783e1d3c0fdSWill Deacon }; 784e1d3c0fdSWill Deacon 785e1d3c0fdSWill Deacon return data; 786e1d3c0fdSWill Deacon } 787e1d3c0fdSWill Deacon 788e1d3c0fdSWill Deacon static struct io_pgtable * 789e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 790e1d3c0fdSWill Deacon { 791e1d3c0fdSWill Deacon u64 reg; 7923850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 793e1d3c0fdSWill Deacon 7944f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | 795b6b65ca2SZhen Lei IO_PGTABLE_QUIRK_NON_STRICT)) 7963850db49SRobin Murphy return NULL; 7973850db49SRobin Murphy 7983850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 799e1d3c0fdSWill Deacon if (!data) 800e1d3c0fdSWill Deacon return NULL; 801e1d3c0fdSWill Deacon 802e1d3c0fdSWill Deacon /* TCR */ 8039e6ea59fSBjorn Andersson if (cfg->coherent_walk) { 804e1d3c0fdSWill Deacon reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 805e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 806e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 8079e6ea59fSBjorn Andersson } else { 8089e6ea59fSBjorn Andersson reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) | 8099e6ea59fSBjorn Andersson (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) | 8109e6ea59fSBjorn Andersson (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT); 8119e6ea59fSBjorn Andersson } 812e1d3c0fdSWill Deacon 81306c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 814e1d3c0fdSWill Deacon case SZ_4K: 815e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 816e1d3c0fdSWill Deacon break; 817e1d3c0fdSWill Deacon case SZ_16K: 818e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 819e1d3c0fdSWill Deacon break; 820e1d3c0fdSWill Deacon case SZ_64K: 821e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 822e1d3c0fdSWill Deacon break; 823e1d3c0fdSWill Deacon } 824e1d3c0fdSWill Deacon 825e1d3c0fdSWill Deacon switch (cfg->oas) { 826e1d3c0fdSWill Deacon case 32: 827e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); 828e1d3c0fdSWill Deacon break; 829e1d3c0fdSWill Deacon case 36: 830e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); 831e1d3c0fdSWill Deacon break; 832e1d3c0fdSWill Deacon case 40: 833e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); 834e1d3c0fdSWill Deacon break; 835e1d3c0fdSWill Deacon case 42: 836e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); 837e1d3c0fdSWill Deacon break; 838e1d3c0fdSWill Deacon case 44: 839e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); 840e1d3c0fdSWill Deacon break; 841e1d3c0fdSWill Deacon case 48: 842e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); 843e1d3c0fdSWill Deacon break; 8446c89928fSRobin Murphy case 52: 8456c89928fSRobin Murphy reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT); 8466c89928fSRobin Murphy break; 847e1d3c0fdSWill Deacon default: 848e1d3c0fdSWill Deacon goto out_free_data; 849e1d3c0fdSWill Deacon } 850e1d3c0fdSWill Deacon 851e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 85263979b8dSWill Deacon 85363979b8dSWill Deacon /* Disable speculative walks through TTBR1 */ 85463979b8dSWill Deacon reg |= ARM_LPAE_TCR_EPD1; 855e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr = reg; 856e1d3c0fdSWill Deacon 857e1d3c0fdSWill Deacon /* MAIRs */ 858e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 859e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 860e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 861e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 862e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 86390ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | 86490ec7a76SVivek Gautam (ARM_LPAE_MAIR_ATTR_INC_OWBRWA 86590ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); 866e1d3c0fdSWill Deacon 867e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[0] = reg; 868e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.mair[1] = 0; 869e1d3c0fdSWill Deacon 870e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 871*c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 872*c79278c1SRobin Murphy GFP_KERNEL, cfg); 873e1d3c0fdSWill Deacon if (!data->pgd) 874e1d3c0fdSWill Deacon goto out_free_data; 875e1d3c0fdSWill Deacon 87687a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 87787a91b15SRobin Murphy wmb(); 878e1d3c0fdSWill Deacon 879e1d3c0fdSWill Deacon /* TTBRs */ 880e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); 881e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.ttbr[1] = 0; 882e1d3c0fdSWill Deacon return &data->iop; 883e1d3c0fdSWill Deacon 884e1d3c0fdSWill Deacon out_free_data: 885e1d3c0fdSWill Deacon kfree(data); 886e1d3c0fdSWill Deacon return NULL; 887e1d3c0fdSWill Deacon } 888e1d3c0fdSWill Deacon 889e1d3c0fdSWill Deacon static struct io_pgtable * 890e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 891e1d3c0fdSWill Deacon { 892e1d3c0fdSWill Deacon u64 reg, sl; 8933850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 894e1d3c0fdSWill Deacon 8953850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 8964f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) 8973850db49SRobin Murphy return NULL; 8983850db49SRobin Murphy 8993850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 900e1d3c0fdSWill Deacon if (!data) 901e1d3c0fdSWill Deacon return NULL; 902e1d3c0fdSWill Deacon 903e1d3c0fdSWill Deacon /* 904e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 905e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 906e1d3c0fdSWill Deacon */ 907594ab90fSRobin Murphy if (data->start_level == 0) { 908e1d3c0fdSWill Deacon unsigned long pgd_pages; 909e1d3c0fdSWill Deacon 910*c79278c1SRobin Murphy pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); 911e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 912*c79278c1SRobin Murphy data->pgd_bits += data->bits_per_level; 913594ab90fSRobin Murphy data->start_level++; 914e1d3c0fdSWill Deacon } 915e1d3c0fdSWill Deacon } 916e1d3c0fdSWill Deacon 917e1d3c0fdSWill Deacon /* VTCR */ 918e1d3c0fdSWill Deacon reg = ARM_64_LPAE_S2_TCR_RES1 | 919e1d3c0fdSWill Deacon (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 920e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 921e1d3c0fdSWill Deacon (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 922e1d3c0fdSWill Deacon 923594ab90fSRobin Murphy sl = data->start_level; 924e1d3c0fdSWill Deacon 92506c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 926e1d3c0fdSWill Deacon case SZ_4K: 927e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_4K; 928e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 929e1d3c0fdSWill Deacon break; 930e1d3c0fdSWill Deacon case SZ_16K: 931e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_16K; 932e1d3c0fdSWill Deacon break; 933e1d3c0fdSWill Deacon case SZ_64K: 934e1d3c0fdSWill Deacon reg |= ARM_LPAE_TCR_TG0_64K; 935e1d3c0fdSWill Deacon break; 936e1d3c0fdSWill Deacon } 937e1d3c0fdSWill Deacon 938e1d3c0fdSWill Deacon switch (cfg->oas) { 939e1d3c0fdSWill Deacon case 32: 940e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); 941e1d3c0fdSWill Deacon break; 942e1d3c0fdSWill Deacon case 36: 943e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); 944e1d3c0fdSWill Deacon break; 945e1d3c0fdSWill Deacon case 40: 946e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); 947e1d3c0fdSWill Deacon break; 948e1d3c0fdSWill Deacon case 42: 949e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); 950e1d3c0fdSWill Deacon break; 951e1d3c0fdSWill Deacon case 44: 952e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); 953e1d3c0fdSWill Deacon break; 954e1d3c0fdSWill Deacon case 48: 955e1d3c0fdSWill Deacon reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); 956e1d3c0fdSWill Deacon break; 9576c89928fSRobin Murphy case 52: 9586c89928fSRobin Murphy reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT); 9596c89928fSRobin Murphy break; 960e1d3c0fdSWill Deacon default: 961e1d3c0fdSWill Deacon goto out_free_data; 962e1d3c0fdSWill Deacon } 963e1d3c0fdSWill Deacon 964e1d3c0fdSWill Deacon reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 965e1d3c0fdSWill Deacon reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; 966e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr = reg; 967e1d3c0fdSWill Deacon 968e1d3c0fdSWill Deacon /* Allocate pgd pages */ 969*c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 970*c79278c1SRobin Murphy GFP_KERNEL, cfg); 971e1d3c0fdSWill Deacon if (!data->pgd) 972e1d3c0fdSWill Deacon goto out_free_data; 973e1d3c0fdSWill Deacon 97487a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 97587a91b15SRobin Murphy wmb(); 976e1d3c0fdSWill Deacon 977e1d3c0fdSWill Deacon /* VTTBR */ 978e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 979e1d3c0fdSWill Deacon return &data->iop; 980e1d3c0fdSWill Deacon 981e1d3c0fdSWill Deacon out_free_data: 982e1d3c0fdSWill Deacon kfree(data); 983e1d3c0fdSWill Deacon return NULL; 984e1d3c0fdSWill Deacon } 985e1d3c0fdSWill Deacon 986e1d3c0fdSWill Deacon static struct io_pgtable * 987e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 988e1d3c0fdSWill Deacon { 989e1d3c0fdSWill Deacon struct io_pgtable *iop; 990e1d3c0fdSWill Deacon 991e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 992e1d3c0fdSWill Deacon return NULL; 993e1d3c0fdSWill Deacon 994e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 995e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 996e1d3c0fdSWill Deacon if (iop) { 997e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; 998e1d3c0fdSWill Deacon cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; 999e1d3c0fdSWill Deacon } 1000e1d3c0fdSWill Deacon 1001e1d3c0fdSWill Deacon return iop; 1002e1d3c0fdSWill Deacon } 1003e1d3c0fdSWill Deacon 1004e1d3c0fdSWill Deacon static struct io_pgtable * 1005e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 1006e1d3c0fdSWill Deacon { 1007e1d3c0fdSWill Deacon struct io_pgtable *iop; 1008e1d3c0fdSWill Deacon 1009e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 1010e1d3c0fdSWill Deacon return NULL; 1011e1d3c0fdSWill Deacon 1012e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1013e1d3c0fdSWill Deacon iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 1014e1d3c0fdSWill Deacon if (iop) 1015e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; 1016e1d3c0fdSWill Deacon 1017e1d3c0fdSWill Deacon return iop; 1018e1d3c0fdSWill Deacon } 1019e1d3c0fdSWill Deacon 1020d08d42deSRob Herring static struct io_pgtable * 1021d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 1022d08d42deSRob Herring { 102352f325f4SRobin Murphy struct arm_lpae_io_pgtable *data; 1024d08d42deSRob Herring 102552f325f4SRobin Murphy /* No quirks for Mali (hopefully) */ 102652f325f4SRobin Murphy if (cfg->quirks) 102752f325f4SRobin Murphy return NULL; 1028d08d42deSRob Herring 10291be08f45SRobin Murphy if (cfg->ias > 48 || cfg->oas > 40) 1030d08d42deSRob Herring return NULL; 1031d08d42deSRob Herring 1032d08d42deSRob Herring cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1033d08d42deSRob Herring 103452f325f4SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 103552f325f4SRobin Murphy if (!data) 103652f325f4SRobin Murphy return NULL; 1037d08d42deSRob Herring 10381be08f45SRobin Murphy /* Mali seems to need a full 4-level table regardless of IAS */ 1039594ab90fSRobin Murphy if (data->start_level > 0) { 1040594ab90fSRobin Murphy data->start_level = 0; 1041*c79278c1SRobin Murphy data->pgd_bits = 0; 10421be08f45SRobin Murphy } 104352f325f4SRobin Murphy /* 104452f325f4SRobin Murphy * MEMATTR: Mali has no actual notion of a non-cacheable type, so the 104552f325f4SRobin Murphy * best we can do is mimic the out-of-tree driver and hope that the 104652f325f4SRobin Murphy * "implementation-defined caching policy" is good enough. Similarly, 104752f325f4SRobin Murphy * we'll use it for the sake of a valid attribute for our 'device' 104852f325f4SRobin Murphy * index, although callers should never request that in practice. 104952f325f4SRobin Murphy */ 105052f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.memattr = 105152f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 105252f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 105352f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 105452f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 105552f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 105652f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 105752f325f4SRobin Murphy 1058*c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1059*c79278c1SRobin Murphy cfg); 106052f325f4SRobin Murphy if (!data->pgd) 106152f325f4SRobin Murphy goto out_free_data; 106252f325f4SRobin Murphy 106352f325f4SRobin Murphy /* Ensure the empty pgd is visible before TRANSTAB can be written */ 106452f325f4SRobin Murphy wmb(); 106552f325f4SRobin Murphy 106652f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | 1067d08d42deSRob Herring ARM_MALI_LPAE_TTBR_READ_INNER | 1068d08d42deSRob Herring ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; 106952f325f4SRobin Murphy return &data->iop; 1070d08d42deSRob Herring 107152f325f4SRobin Murphy out_free_data: 107252f325f4SRobin Murphy kfree(data); 107352f325f4SRobin Murphy return NULL; 1074d08d42deSRob Herring } 1075d08d42deSRob Herring 1076e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1077e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 1078e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1079e1d3c0fdSWill Deacon }; 1080e1d3c0fdSWill Deacon 1081e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1082e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 1083e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1084e1d3c0fdSWill Deacon }; 1085e1d3c0fdSWill Deacon 1086e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1087e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1088e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1089e1d3c0fdSWill Deacon }; 1090e1d3c0fdSWill Deacon 1091e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1092e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1093e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1094e1d3c0fdSWill Deacon }; 1095fe4b991dSWill Deacon 1096d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1097d08d42deSRob Herring .alloc = arm_mali_lpae_alloc_pgtable, 1098d08d42deSRob Herring .free = arm_lpae_free_pgtable, 1099d08d42deSRob Herring }; 1100d08d42deSRob Herring 1101fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1102fe4b991dSWill Deacon 1103b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata; 1104fe4b991dSWill Deacon 1105b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie) 1106fe4b991dSWill Deacon { 1107fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1108fe4b991dSWill Deacon } 1109fe4b991dSWill Deacon 1110b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size, 1111b5813c16SRobin Murphy size_t granule, void *cookie) 1112fe4b991dSWill Deacon { 1113fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1114fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1115fe4b991dSWill Deacon } 1116fe4b991dSWill Deacon 1117b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, 1118b5813c16SRobin Murphy unsigned long iova, size_t granule, 1119b5813c16SRobin Murphy void *cookie) 112010b7a7d9SWill Deacon { 1121abfd6fe0SWill Deacon dummy_tlb_flush(iova, granule, granule, cookie); 112210b7a7d9SWill Deacon } 112310b7a7d9SWill Deacon 1124298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = { 1125fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 112610b7a7d9SWill Deacon .tlb_flush_walk = dummy_tlb_flush, 112710b7a7d9SWill Deacon .tlb_flush_leaf = dummy_tlb_flush, 1128abfd6fe0SWill Deacon .tlb_add_page = dummy_tlb_add_page, 1129fe4b991dSWill Deacon }; 1130fe4b991dSWill Deacon 1131fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1132fe4b991dSWill Deacon { 1133fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1134fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1135fe4b991dSWill Deacon 1136fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1137fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 1138fe4b991dSWill Deacon pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", 1139*c79278c1SRobin Murphy ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), 1140594ab90fSRobin Murphy data->pg_shift, data->bits_per_level, data->pgd); 1141fe4b991dSWill Deacon } 1142fe4b991dSWill Deacon 1143fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1144fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1145fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1146fe4b991dSWill Deacon selftest_running = false; \ 1147fe4b991dSWill Deacon -EFAULT; \ 1148fe4b991dSWill Deacon }) 1149fe4b991dSWill Deacon 1150fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1151fe4b991dSWill Deacon { 11529062c1d0SChristophe JAILLET static const enum io_pgtable_fmt fmts[] __initconst = { 1153fe4b991dSWill Deacon ARM_64_LPAE_S1, 1154fe4b991dSWill Deacon ARM_64_LPAE_S2, 1155fe4b991dSWill Deacon }; 1156fe4b991dSWill Deacon 1157fe4b991dSWill Deacon int i, j; 1158fe4b991dSWill Deacon unsigned long iova; 1159fe4b991dSWill Deacon size_t size; 1160fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1161fe4b991dSWill Deacon 1162fe4b991dSWill Deacon selftest_running = true; 1163fe4b991dSWill Deacon 1164fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1165fe4b991dSWill Deacon cfg_cookie = cfg; 1166fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1167fe4b991dSWill Deacon if (!ops) { 1168fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1169fe4b991dSWill Deacon return -ENOMEM; 1170fe4b991dSWill Deacon } 1171fe4b991dSWill Deacon 1172fe4b991dSWill Deacon /* 1173fe4b991dSWill Deacon * Initial sanity checks. 1174fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1175fe4b991dSWill Deacon */ 1176fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1177fe4b991dSWill Deacon return __FAIL(ops, i); 1178fe4b991dSWill Deacon 1179fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1180fe4b991dSWill Deacon return __FAIL(ops, i); 1181fe4b991dSWill Deacon 1182fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1183fe4b991dSWill Deacon return __FAIL(ops, i); 1184fe4b991dSWill Deacon 1185fe4b991dSWill Deacon /* 1186fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1187fe4b991dSWill Deacon */ 1188fe4b991dSWill Deacon iova = 0; 11894ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1190fe4b991dSWill Deacon size = 1UL << j; 1191fe4b991dSWill Deacon 1192fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1193fe4b991dSWill Deacon IOMMU_WRITE | 1194fe4b991dSWill Deacon IOMMU_NOEXEC | 1195fe4b991dSWill Deacon IOMMU_CACHE)) 1196fe4b991dSWill Deacon return __FAIL(ops, i); 1197fe4b991dSWill Deacon 1198fe4b991dSWill Deacon /* Overlapping mappings */ 1199fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1200fe4b991dSWill Deacon IOMMU_READ | IOMMU_NOEXEC)) 1201fe4b991dSWill Deacon return __FAIL(ops, i); 1202fe4b991dSWill Deacon 1203fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1204fe4b991dSWill Deacon return __FAIL(ops, i); 1205fe4b991dSWill Deacon 1206fe4b991dSWill Deacon iova += SZ_1G; 1207fe4b991dSWill Deacon } 1208fe4b991dSWill Deacon 1209fe4b991dSWill Deacon /* Partial unmap */ 1210fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1211a2d3a382SWill Deacon if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) 1212fe4b991dSWill Deacon return __FAIL(ops, i); 1213fe4b991dSWill Deacon 1214fe4b991dSWill Deacon /* Remap of partial unmap */ 1215fe4b991dSWill Deacon if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 1216fe4b991dSWill Deacon return __FAIL(ops, i); 1217fe4b991dSWill Deacon 1218fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1219fe4b991dSWill Deacon return __FAIL(ops, i); 1220fe4b991dSWill Deacon 1221fe4b991dSWill Deacon /* Full unmap */ 1222fe4b991dSWill Deacon iova = 0; 1223f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1224fe4b991dSWill Deacon size = 1UL << j; 1225fe4b991dSWill Deacon 1226a2d3a382SWill Deacon if (ops->unmap(ops, iova, size, NULL) != size) 1227fe4b991dSWill Deacon return __FAIL(ops, i); 1228fe4b991dSWill Deacon 1229fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1230fe4b991dSWill Deacon return __FAIL(ops, i); 1231fe4b991dSWill Deacon 1232fe4b991dSWill Deacon /* Remap full block */ 1233fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 1234fe4b991dSWill Deacon return __FAIL(ops, i); 1235fe4b991dSWill Deacon 1236fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1237fe4b991dSWill Deacon return __FAIL(ops, i); 1238fe4b991dSWill Deacon 1239fe4b991dSWill Deacon iova += SZ_1G; 1240fe4b991dSWill Deacon } 1241fe4b991dSWill Deacon 1242fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1243fe4b991dSWill Deacon } 1244fe4b991dSWill Deacon 1245fe4b991dSWill Deacon selftest_running = false; 1246fe4b991dSWill Deacon return 0; 1247fe4b991dSWill Deacon } 1248fe4b991dSWill Deacon 1249fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1250fe4b991dSWill Deacon { 12519062c1d0SChristophe JAILLET static const unsigned long pgsize[] __initconst = { 1252fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1253fe4b991dSWill Deacon SZ_16K | SZ_32M, 1254fe4b991dSWill Deacon SZ_64K | SZ_512M, 1255fe4b991dSWill Deacon }; 1256fe4b991dSWill Deacon 12579062c1d0SChristophe JAILLET static const unsigned int ias[] __initconst = { 1258fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1259fe4b991dSWill Deacon }; 1260fe4b991dSWill Deacon 1261fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1262fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1263fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1264fe4b991dSWill Deacon .oas = 48, 12654f41845bSWill Deacon .coherent_walk = true, 1266fe4b991dSWill Deacon }; 1267fe4b991dSWill Deacon 1268fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1269fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1270fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1271fe4b991dSWill Deacon cfg.ias = ias[j]; 1272fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1273fe4b991dSWill Deacon pgsize[i], ias[j]); 1274fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1275fe4b991dSWill Deacon fail++; 1276fe4b991dSWill Deacon else 1277fe4b991dSWill Deacon pass++; 1278fe4b991dSWill Deacon } 1279fe4b991dSWill Deacon } 1280fe4b991dSWill Deacon 1281fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1282fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1283fe4b991dSWill Deacon } 1284fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1285fe4b991dSWill Deacon #endif 1286