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 133f37eb484SKunkun 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) 154f37eb484SKunkun Jiang return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE; 155d08d42deSRob Herring 156f37eb484SKunkun 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 235*41e1eb25SIsaac J. Manjarres static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries, 2362c3d273eSRobin Murphy struct io_pgtable_cfg *cfg) 2372c3d273eSRobin Murphy { 2382c3d273eSRobin Murphy dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep), 239*41e1eb25SIsaac J. Manjarres sizeof(*ptep) * num_entries, DMA_TO_DEVICE); 2402c3d273eSRobin Murphy } 2412c3d273eSRobin Murphy 242f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte, 243*41e1eb25SIsaac J. Manjarres int num_entries, struct io_pgtable_cfg *cfg) 244f8d54961SRobin Murphy { 245*41e1eb25SIsaac J. Manjarres int i; 246*41e1eb25SIsaac J. Manjarres 247*41e1eb25SIsaac J. Manjarres for (i = 0; i < num_entries; i++) 248*41e1eb25SIsaac J. Manjarres ptep[i] = pte; 249f8d54961SRobin Murphy 2504f41845bSWill Deacon if (!cfg->coherent_walk) 251*41e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, num_entries, cfg); 252f8d54961SRobin Murphy } 253f8d54961SRobin Murphy 254193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 2553951c41aSWill Deacon struct iommu_iotlb_gather *gather, 256cf27ec93SWill Deacon unsigned long iova, size_t size, int lvl, 257cf27ec93SWill Deacon arm_lpae_iopte *ptep); 258cf27ec93SWill Deacon 259fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 260fb3a9579SRobin Murphy phys_addr_t paddr, arm_lpae_iopte prot, 261*41e1eb25SIsaac J. Manjarres int lvl, int num_entries, arm_lpae_iopte *ptep) 262fb3a9579SRobin Murphy { 263fb3a9579SRobin Murphy arm_lpae_iopte pte = prot; 264*41e1eb25SIsaac J. Manjarres struct io_pgtable_cfg *cfg = &data->iop.cfg; 265*41e1eb25SIsaac J. Manjarres size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 266*41e1eb25SIsaac J. Manjarres int i; 267fb3a9579SRobin Murphy 268d08d42deSRob Herring if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1) 269fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_PAGE; 270fb3a9579SRobin Murphy else 271fb3a9579SRobin Murphy pte |= ARM_LPAE_PTE_TYPE_BLOCK; 272fb3a9579SRobin Murphy 273*41e1eb25SIsaac J. Manjarres for (i = 0; i < num_entries; i++) 274*41e1eb25SIsaac J. Manjarres ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data); 275fb3a9579SRobin Murphy 276*41e1eb25SIsaac J. Manjarres if (!cfg->coherent_walk) 277*41e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, num_entries, cfg); 278fb3a9579SRobin Murphy } 279fb3a9579SRobin Murphy 280e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 281e1d3c0fdSWill Deacon unsigned long iova, phys_addr_t paddr, 282*41e1eb25SIsaac J. Manjarres arm_lpae_iopte prot, int lvl, int num_entries, 283e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 284e1d3c0fdSWill Deacon { 285*41e1eb25SIsaac J. Manjarres int i; 286e1d3c0fdSWill Deacon 287*41e1eb25SIsaac J. Manjarres for (i = 0; i < num_entries; i++) 288*41e1eb25SIsaac J. Manjarres if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) { 289cf27ec93SWill Deacon /* We require an unmap first */ 290fe4b991dSWill Deacon WARN_ON(!selftest_running); 291e1d3c0fdSWill Deacon return -EEXIST; 292*41e1eb25SIsaac J. Manjarres } else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) { 293cf27ec93SWill Deacon /* 294cf27ec93SWill Deacon * We need to unmap and free the old table before 295cf27ec93SWill Deacon * overwriting it with a block entry. 296cf27ec93SWill Deacon */ 297cf27ec93SWill Deacon arm_lpae_iopte *tblp; 298cf27ec93SWill Deacon size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 299cf27ec93SWill Deacon 300cf27ec93SWill Deacon tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data); 301*41e1eb25SIsaac J. Manjarres if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 302*41e1eb25SIsaac J. Manjarres lvl, tblp) != sz) { 3033951c41aSWill Deacon WARN_ON(1); 304cf27ec93SWill Deacon return -EINVAL; 305fe4b991dSWill Deacon } 3063951c41aSWill Deacon } 307e1d3c0fdSWill Deacon 308*41e1eb25SIsaac J. Manjarres __arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep); 309e1d3c0fdSWill Deacon return 0; 310e1d3c0fdSWill Deacon } 311e1d3c0fdSWill Deacon 312fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table, 313fb3a9579SRobin Murphy arm_lpae_iopte *ptep, 3142c3d273eSRobin Murphy arm_lpae_iopte curr, 315fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg) 316fb3a9579SRobin Murphy { 3172c3d273eSRobin Murphy arm_lpae_iopte old, new; 318fb3a9579SRobin Murphy 319fb3a9579SRobin Murphy new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE; 320fb3a9579SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS) 321fb3a9579SRobin Murphy new |= ARM_LPAE_PTE_NSTABLE; 322fb3a9579SRobin Murphy 32377f34458SWill Deacon /* 32477f34458SWill Deacon * Ensure the table itself is visible before its PTE can be. 32577f34458SWill Deacon * Whilst we could get away with cmpxchg64_release below, this 32677f34458SWill Deacon * doesn't have any ordering semantics when !CONFIG_SMP. 32777f34458SWill Deacon */ 32877f34458SWill Deacon dma_wmb(); 3292c3d273eSRobin Murphy 3302c3d273eSRobin Murphy old = cmpxchg64_relaxed(ptep, curr, new); 3312c3d273eSRobin Murphy 3324f41845bSWill Deacon if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC)) 3332c3d273eSRobin Murphy return old; 3342c3d273eSRobin Murphy 3352c3d273eSRobin Murphy /* Even if it's not ours, there's no point waiting; just kick it */ 336*41e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, 1, cfg); 3372c3d273eSRobin Murphy if (old == curr) 3382c3d273eSRobin Murphy WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC); 3392c3d273eSRobin Murphy 3402c3d273eSRobin Murphy return old; 341fb3a9579SRobin Murphy } 342fb3a9579SRobin Murphy 343e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 344e1d3c0fdSWill Deacon phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 345f34ce7a7SBaolin Wang int lvl, arm_lpae_iopte *ptep, gfp_t gfp) 346e1d3c0fdSWill Deacon { 347e1d3c0fdSWill Deacon arm_lpae_iopte *cptep, pte; 348e1d3c0fdSWill Deacon size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 3492c3d273eSRobin Murphy size_t tblsz = ARM_LPAE_GRANULE(data); 350f8d54961SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 351e1d3c0fdSWill Deacon 352e1d3c0fdSWill Deacon /* Find our entry at the current level */ 353e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 354e1d3c0fdSWill Deacon 355e1d3c0fdSWill Deacon /* If we can install a leaf entry at this level, then do so */ 356f7b90d2cSRobin Murphy if (size == block_size) 357*41e1eb25SIsaac J. Manjarres return arm_lpae_init_pte(data, iova, paddr, prot, lvl, 1, ptep); 358e1d3c0fdSWill Deacon 359e1d3c0fdSWill Deacon /* We can't allocate tables at the final level */ 360e1d3c0fdSWill Deacon if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 361e1d3c0fdSWill Deacon return -EINVAL; 362e1d3c0fdSWill Deacon 363e1d3c0fdSWill Deacon /* Grab a pointer to the next level */ 3642c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 365e1d3c0fdSWill Deacon if (!pte) { 366f34ce7a7SBaolin Wang cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg); 367e1d3c0fdSWill Deacon if (!cptep) 368e1d3c0fdSWill Deacon return -ENOMEM; 369e1d3c0fdSWill Deacon 3702c3d273eSRobin Murphy pte = arm_lpae_install_table(cptep, ptep, 0, cfg); 3712c3d273eSRobin Murphy if (pte) 3722c3d273eSRobin Murphy __arm_lpae_free_pages(cptep, tblsz, cfg); 3734f41845bSWill Deacon } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) { 374*41e1eb25SIsaac J. Manjarres __arm_lpae_sync_pte(ptep, 1, cfg); 3752c3d273eSRobin Murphy } 3762c3d273eSRobin Murphy 377d08d42deSRob Herring if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) { 378e1d3c0fdSWill Deacon cptep = iopte_deref(pte, data); 3792c3d273eSRobin Murphy } else if (pte) { 380ed46e66cSOleksandr Tyshchenko /* We require an unmap first */ 381ed46e66cSOleksandr Tyshchenko WARN_ON(!selftest_running); 382ed46e66cSOleksandr Tyshchenko return -EEXIST; 383e1d3c0fdSWill Deacon } 384e1d3c0fdSWill Deacon 385e1d3c0fdSWill Deacon /* Rinse, repeat */ 386f34ce7a7SBaolin Wang return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep, gfp); 387e1d3c0fdSWill Deacon } 388e1d3c0fdSWill Deacon 389e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 390e1d3c0fdSWill Deacon int prot) 391e1d3c0fdSWill Deacon { 392e1d3c0fdSWill Deacon arm_lpae_iopte pte; 393e1d3c0fdSWill Deacon 394e1d3c0fdSWill Deacon if (data->iop.fmt == ARM_64_LPAE_S1 || 395e1d3c0fdSWill Deacon data->iop.fmt == ARM_32_LPAE_S1) { 396e7468a23SJeremy Gebben pte = ARM_LPAE_PTE_nG; 397e1d3c0fdSWill Deacon if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 398e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_AP_RDONLY; 399e7468a23SJeremy Gebben if (!(prot & IOMMU_PRIV)) 400e7468a23SJeremy Gebben pte |= ARM_LPAE_PTE_AP_UNPRIV; 401e1d3c0fdSWill Deacon } else { 402e1d3c0fdSWill Deacon pte = ARM_LPAE_PTE_HAP_FAULT; 403e1d3c0fdSWill Deacon if (prot & IOMMU_READ) 404e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_READ; 405e1d3c0fdSWill Deacon if (prot & IOMMU_WRITE) 406e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_HAP_WRITE; 407d08d42deSRob Herring } 408d08d42deSRob Herring 409d08d42deSRob Herring /* 410d08d42deSRob Herring * Note that this logic is structured to accommodate Mali LPAE 411d08d42deSRob Herring * having stage-1-like attributes but stage-2-like permissions. 412d08d42deSRob Herring */ 413d08d42deSRob Herring if (data->iop.fmt == ARM_64_LPAE_S2 || 414d08d42deSRob Herring data->iop.fmt == ARM_32_LPAE_S2) { 415fb948251SRobin Murphy if (prot & IOMMU_MMIO) 416fb948251SRobin Murphy pte |= ARM_LPAE_PTE_MEMATTR_DEV; 417fb948251SRobin Murphy else if (prot & IOMMU_CACHE) 418e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 419e1d3c0fdSWill Deacon else 420e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_MEMATTR_NC; 421d08d42deSRob Herring } else { 422d08d42deSRob Herring if (prot & IOMMU_MMIO) 423d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV 424d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 425d08d42deSRob Herring else if (prot & IOMMU_CACHE) 426d08d42deSRob Herring pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 427d08d42deSRob Herring << ARM_LPAE_PTE_ATTRINDX_SHIFT); 428e1d3c0fdSWill Deacon } 429e1d3c0fdSWill Deacon 430728da60dSRobin Murphy /* 431728da60dSRobin Murphy * Also Mali has its own notions of shareability wherein its Inner 432728da60dSRobin Murphy * domain covers the cores within the GPU, and its Outer domain is 433728da60dSRobin Murphy * "outside the GPU" (i.e. either the Inner or System domain in CPU 434728da60dSRobin Murphy * terms, depending on coherency). 435728da60dSRobin Murphy */ 436728da60dSRobin Murphy if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE) 4377618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_IS; 4387618e479SRobin Murphy else 4397618e479SRobin Murphy pte |= ARM_LPAE_PTE_SH_OS; 4407618e479SRobin Murphy 441e1d3c0fdSWill Deacon if (prot & IOMMU_NOEXEC) 442e1d3c0fdSWill Deacon pte |= ARM_LPAE_PTE_XN; 443e1d3c0fdSWill Deacon 4447618e479SRobin Murphy if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 4457618e479SRobin Murphy pte |= ARM_LPAE_PTE_NS; 4467618e479SRobin Murphy 4477618e479SRobin Murphy if (data->iop.fmt != ARM_MALI_LPAE) 4487618e479SRobin Murphy pte |= ARM_LPAE_PTE_AF; 4497618e479SRobin Murphy 450e1d3c0fdSWill Deacon return pte; 451e1d3c0fdSWill Deacon } 452e1d3c0fdSWill Deacon 453e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 454f34ce7a7SBaolin Wang phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp) 455e1d3c0fdSWill Deacon { 456e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 457f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 458e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 459594ab90fSRobin Murphy int ret, lvl = data->start_level; 460e1d3c0fdSWill Deacon arm_lpae_iopte prot; 46108090744SRobin Murphy long iaext = (s64)iova >> cfg->ias; 462e1d3c0fdSWill Deacon 463f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 464f7b90d2cSRobin Murphy return -EINVAL; 465f7b90d2cSRobin Murphy 466db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 467db690301SRobin Murphy iaext = ~iaext; 468db690301SRobin Murphy if (WARN_ON(iaext || paddr >> cfg->oas)) 46976557391SRobin Murphy return -ERANGE; 47076557391SRobin Murphy 471f12e0d22SKeqian Zhu /* If no access, then nothing to do */ 472f12e0d22SKeqian Zhu if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 473f12e0d22SKeqian Zhu return 0; 474f12e0d22SKeqian Zhu 475e1d3c0fdSWill Deacon prot = arm_lpae_prot_to_pte(data, iommu_prot); 476f34ce7a7SBaolin Wang ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, gfp); 47787a91b15SRobin Murphy /* 47887a91b15SRobin Murphy * Synchronise all PTE updates for the new mapping before there's 47987a91b15SRobin Murphy * a chance for anything to kick off a table walk for the new iova. 48087a91b15SRobin Murphy */ 48187a91b15SRobin Murphy wmb(); 48287a91b15SRobin Murphy 48387a91b15SRobin Murphy return ret; 484e1d3c0fdSWill Deacon } 485e1d3c0fdSWill Deacon 486e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 487e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 488e1d3c0fdSWill Deacon { 489e1d3c0fdSWill Deacon arm_lpae_iopte *start, *end; 490e1d3c0fdSWill Deacon unsigned long table_size; 491e1d3c0fdSWill Deacon 492594ab90fSRobin Murphy if (lvl == data->start_level) 493c79278c1SRobin Murphy table_size = ARM_LPAE_PGD_SIZE(data); 494e1d3c0fdSWill Deacon else 49506c610e8SRobin Murphy table_size = ARM_LPAE_GRANULE(data); 496e1d3c0fdSWill Deacon 497e1d3c0fdSWill Deacon start = ptep; 49812c2ab09SWill Deacon 49912c2ab09SWill Deacon /* Only leaf entries at the last level */ 50012c2ab09SWill Deacon if (lvl == ARM_LPAE_MAX_LEVELS - 1) 50112c2ab09SWill Deacon end = ptep; 50212c2ab09SWill Deacon else 503e1d3c0fdSWill Deacon end = (void *)ptep + table_size; 504e1d3c0fdSWill Deacon 505e1d3c0fdSWill Deacon while (ptep != end) { 506e1d3c0fdSWill Deacon arm_lpae_iopte pte = *ptep++; 507e1d3c0fdSWill Deacon 508d08d42deSRob Herring if (!pte || iopte_leaf(pte, lvl, data->iop.fmt)) 509e1d3c0fdSWill Deacon continue; 510e1d3c0fdSWill Deacon 511e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 512e1d3c0fdSWill Deacon } 513e1d3c0fdSWill Deacon 514f8d54961SRobin Murphy __arm_lpae_free_pages(start, table_size, &data->iop.cfg); 515e1d3c0fdSWill Deacon } 516e1d3c0fdSWill Deacon 517e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop) 518e1d3c0fdSWill Deacon { 519e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 520e1d3c0fdSWill Deacon 521594ab90fSRobin Murphy __arm_lpae_free_pgtable(data, data->start_level, data->pgd); 522e1d3c0fdSWill Deacon kfree(data); 523e1d3c0fdSWill Deacon } 524e1d3c0fdSWill Deacon 525193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 5263951c41aSWill Deacon struct iommu_iotlb_gather *gather, 527e1d3c0fdSWill Deacon unsigned long iova, size_t size, 528fb3a9579SRobin Murphy arm_lpae_iopte blk_pte, int lvl, 529fb3a9579SRobin Murphy arm_lpae_iopte *ptep) 530e1d3c0fdSWill Deacon { 531fb3a9579SRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 532fb3a9579SRobin Murphy arm_lpae_iopte pte, *tablep; 533e1d3c0fdSWill Deacon phys_addr_t blk_paddr; 534fb3a9579SRobin Murphy size_t tablesz = ARM_LPAE_GRANULE(data); 535fb3a9579SRobin Murphy size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data); 536fb3a9579SRobin Murphy int i, unmap_idx = -1; 537e1d3c0fdSWill Deacon 538fb3a9579SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 539fb3a9579SRobin Murphy return 0; 540e1d3c0fdSWill Deacon 541fb3a9579SRobin Murphy tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg); 542fb3a9579SRobin Murphy if (!tablep) 543fb3a9579SRobin Murphy return 0; /* Bytes unmapped */ 544e1d3c0fdSWill Deacon 545fb3a9579SRobin Murphy if (size == split_sz) 546fb3a9579SRobin Murphy unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data); 547fb3a9579SRobin Murphy 5486c89928fSRobin Murphy blk_paddr = iopte_to_paddr(blk_pte, data); 549fb3a9579SRobin Murphy pte = iopte_prot(blk_pte); 550fb3a9579SRobin Murphy 551fb3a9579SRobin Murphy for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) { 552e1d3c0fdSWill Deacon /* Unmap! */ 553fb3a9579SRobin Murphy if (i == unmap_idx) 554e1d3c0fdSWill Deacon continue; 555e1d3c0fdSWill Deacon 556*41e1eb25SIsaac J. Manjarres __arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]); 557e1d3c0fdSWill Deacon } 558e1d3c0fdSWill Deacon 5592c3d273eSRobin Murphy pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg); 5602c3d273eSRobin Murphy if (pte != blk_pte) { 5612c3d273eSRobin Murphy __arm_lpae_free_pages(tablep, tablesz, cfg); 5622c3d273eSRobin Murphy /* 5632c3d273eSRobin Murphy * We may race against someone unmapping another part of this 5642c3d273eSRobin Murphy * block, but anything else is invalid. We can't misinterpret 5652c3d273eSRobin Murphy * a page entry here since we're never at the last level. 5662c3d273eSRobin Murphy */ 567f37eb484SKunkun Jiang if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE) 5682c3d273eSRobin Murphy return 0; 5692c3d273eSRobin Murphy 5702c3d273eSRobin Murphy tablep = iopte_deref(pte, data); 57185c7a0f1SRobin Murphy } else if (unmap_idx >= 0) { 5723951c41aSWill Deacon io_pgtable_tlb_add_page(&data->iop, gather, iova, size); 573e1d3c0fdSWill Deacon return size; 574e1d3c0fdSWill Deacon } 575e1d3c0fdSWill Deacon 5763951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep); 57785c7a0f1SRobin Murphy } 57885c7a0f1SRobin Murphy 579193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 5803951c41aSWill Deacon struct iommu_iotlb_gather *gather, 581e1d3c0fdSWill Deacon unsigned long iova, size_t size, int lvl, 582e1d3c0fdSWill Deacon arm_lpae_iopte *ptep) 583e1d3c0fdSWill Deacon { 584e1d3c0fdSWill Deacon arm_lpae_iopte pte; 585507e4c9dSRobin Murphy struct io_pgtable *iop = &data->iop; 586e1d3c0fdSWill Deacon 5872eb97c78SRobin Murphy /* Something went horribly wrong and we ran out of page table */ 5882eb97c78SRobin Murphy if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS)) 5892eb97c78SRobin Murphy return 0; 5902eb97c78SRobin Murphy 591e1d3c0fdSWill Deacon ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 5922c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 5932eb97c78SRobin Murphy if (WARN_ON(!pte)) 594e1d3c0fdSWill Deacon return 0; 595e1d3c0fdSWill Deacon 596e1d3c0fdSWill Deacon /* If the size matches this level, we're in the right place */ 597fb3a9579SRobin Murphy if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) { 598*41e1eb25SIsaac J. Manjarres __arm_lpae_set_pte(ptep, 0, 1, &iop->cfg); 599e1d3c0fdSWill Deacon 600d08d42deSRob Herring if (!iopte_leaf(pte, lvl, iop->fmt)) { 601e1d3c0fdSWill Deacon /* Also flush any partial walks */ 60210b7a7d9SWill Deacon io_pgtable_tlb_flush_walk(iop, iova, size, 60310b7a7d9SWill Deacon ARM_LPAE_GRANULE(data)); 604e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 605e1d3c0fdSWill Deacon __arm_lpae_free_pgtable(data, lvl + 1, ptep); 606b6b65ca2SZhen Lei } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) { 607b6b65ca2SZhen Lei /* 608b6b65ca2SZhen Lei * Order the PTE update against queueing the IOVA, to 609b6b65ca2SZhen Lei * guarantee that a flush callback from a different CPU 610b6b65ca2SZhen Lei * has observed it before the TLBIALL can be issued. 611b6b65ca2SZhen Lei */ 612b6b65ca2SZhen Lei smp_wmb(); 613e1d3c0fdSWill Deacon } else { 6143951c41aSWill Deacon io_pgtable_tlb_add_page(iop, gather, iova, size); 615e1d3c0fdSWill Deacon } 616e1d3c0fdSWill Deacon 617e1d3c0fdSWill Deacon return size; 618d08d42deSRob Herring } else if (iopte_leaf(pte, lvl, iop->fmt)) { 619e1d3c0fdSWill Deacon /* 620e1d3c0fdSWill Deacon * Insert a table at the next level to map the old region, 621e1d3c0fdSWill Deacon * minus the part we want to unmap 622e1d3c0fdSWill Deacon */ 6233951c41aSWill Deacon return arm_lpae_split_blk_unmap(data, gather, iova, size, pte, 624fb3a9579SRobin Murphy lvl + 1, ptep); 625e1d3c0fdSWill Deacon } 626e1d3c0fdSWill Deacon 627e1d3c0fdSWill Deacon /* Keep on walkin' */ 628e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 6293951c41aSWill Deacon return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep); 630e1d3c0fdSWill Deacon } 631e1d3c0fdSWill Deacon 632193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 633a2d3a382SWill Deacon size_t size, struct iommu_iotlb_gather *gather) 634e1d3c0fdSWill Deacon { 635e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 636f7b90d2cSRobin Murphy struct io_pgtable_cfg *cfg = &data->iop.cfg; 637e1d3c0fdSWill Deacon arm_lpae_iopte *ptep = data->pgd; 63808090744SRobin Murphy long iaext = (s64)iova >> cfg->ias; 639e1d3c0fdSWill Deacon 640f7b90d2cSRobin Murphy if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size)) 641f7b90d2cSRobin Murphy return 0; 642f7b90d2cSRobin Murphy 643db690301SRobin Murphy if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) 644db690301SRobin Murphy iaext = ~iaext; 645db690301SRobin Murphy if (WARN_ON(iaext)) 64676557391SRobin Murphy return 0; 64776557391SRobin Murphy 648594ab90fSRobin Murphy return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep); 649e1d3c0fdSWill Deacon } 650e1d3c0fdSWill Deacon 651e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 652e1d3c0fdSWill Deacon unsigned long iova) 653e1d3c0fdSWill Deacon { 654e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 655e1d3c0fdSWill Deacon arm_lpae_iopte pte, *ptep = data->pgd; 656594ab90fSRobin Murphy int lvl = data->start_level; 657e1d3c0fdSWill Deacon 658e1d3c0fdSWill Deacon do { 659e1d3c0fdSWill Deacon /* Valid IOPTE pointer? */ 660e1d3c0fdSWill Deacon if (!ptep) 661e1d3c0fdSWill Deacon return 0; 662e1d3c0fdSWill Deacon 663e1d3c0fdSWill Deacon /* Grab the IOPTE we're interested in */ 6642c3d273eSRobin Murphy ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 6652c3d273eSRobin Murphy pte = READ_ONCE(*ptep); 666e1d3c0fdSWill Deacon 667e1d3c0fdSWill Deacon /* Valid entry? */ 668e1d3c0fdSWill Deacon if (!pte) 669e1d3c0fdSWill Deacon return 0; 670e1d3c0fdSWill Deacon 671e1d3c0fdSWill Deacon /* Leaf entry? */ 672d08d42deSRob Herring if (iopte_leaf(pte, lvl, data->iop.fmt)) 673e1d3c0fdSWill Deacon goto found_translation; 674e1d3c0fdSWill Deacon 675e1d3c0fdSWill Deacon /* Take it to the next level */ 676e1d3c0fdSWill Deacon ptep = iopte_deref(pte, data); 677e1d3c0fdSWill Deacon } while (++lvl < ARM_LPAE_MAX_LEVELS); 678e1d3c0fdSWill Deacon 679e1d3c0fdSWill Deacon /* Ran out of page tables to walk */ 680e1d3c0fdSWill Deacon return 0; 681e1d3c0fdSWill Deacon 682e1d3c0fdSWill Deacon found_translation: 6837c6d90e2SWill Deacon iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1); 6846c89928fSRobin Murphy return iopte_to_paddr(pte, data) | iova; 685e1d3c0fdSWill Deacon } 686e1d3c0fdSWill Deacon 687e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 688e1d3c0fdSWill Deacon { 6896c89928fSRobin Murphy unsigned long granule, page_sizes; 6906c89928fSRobin Murphy unsigned int max_addr_bits = 48; 691e1d3c0fdSWill Deacon 692e1d3c0fdSWill Deacon /* 693e1d3c0fdSWill Deacon * We need to restrict the supported page sizes to match the 694e1d3c0fdSWill Deacon * translation regime for a particular granule. Aim to match 695e1d3c0fdSWill Deacon * the CPU page size if possible, otherwise prefer smaller sizes. 696e1d3c0fdSWill Deacon * While we're at it, restrict the block sizes to match the 697e1d3c0fdSWill Deacon * chosen granule. 698e1d3c0fdSWill Deacon */ 699e1d3c0fdSWill Deacon if (cfg->pgsize_bitmap & PAGE_SIZE) 700e1d3c0fdSWill Deacon granule = PAGE_SIZE; 701e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & ~PAGE_MASK) 702e1d3c0fdSWill Deacon granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 703e1d3c0fdSWill Deacon else if (cfg->pgsize_bitmap & PAGE_MASK) 704e1d3c0fdSWill Deacon granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 705e1d3c0fdSWill Deacon else 706e1d3c0fdSWill Deacon granule = 0; 707e1d3c0fdSWill Deacon 708e1d3c0fdSWill Deacon switch (granule) { 709e1d3c0fdSWill Deacon case SZ_4K: 7106c89928fSRobin Murphy page_sizes = (SZ_4K | SZ_2M | SZ_1G); 711e1d3c0fdSWill Deacon break; 712e1d3c0fdSWill Deacon case SZ_16K: 7136c89928fSRobin Murphy page_sizes = (SZ_16K | SZ_32M); 714e1d3c0fdSWill Deacon break; 715e1d3c0fdSWill Deacon case SZ_64K: 7166c89928fSRobin Murphy max_addr_bits = 52; 7176c89928fSRobin Murphy page_sizes = (SZ_64K | SZ_512M); 7186c89928fSRobin Murphy if (cfg->oas > 48) 7196c89928fSRobin Murphy page_sizes |= 1ULL << 42; /* 4TB */ 720e1d3c0fdSWill Deacon break; 721e1d3c0fdSWill Deacon default: 7226c89928fSRobin Murphy page_sizes = 0; 723e1d3c0fdSWill Deacon } 7246c89928fSRobin Murphy 7256c89928fSRobin Murphy cfg->pgsize_bitmap &= page_sizes; 7266c89928fSRobin Murphy cfg->ias = min(cfg->ias, max_addr_bits); 7276c89928fSRobin Murphy cfg->oas = min(cfg->oas, max_addr_bits); 728e1d3c0fdSWill Deacon } 729e1d3c0fdSWill Deacon 730e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable * 731e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 732e1d3c0fdSWill Deacon { 733e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable *data; 7345fb190b0SRobin Murphy int levels, va_bits, pg_shift; 735e1d3c0fdSWill Deacon 736e1d3c0fdSWill Deacon arm_lpae_restrict_pgsizes(cfg); 737e1d3c0fdSWill Deacon 738e1d3c0fdSWill Deacon if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 739e1d3c0fdSWill Deacon return NULL; 740e1d3c0fdSWill Deacon 741e1d3c0fdSWill Deacon if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 742e1d3c0fdSWill Deacon return NULL; 743e1d3c0fdSWill Deacon 744e1d3c0fdSWill Deacon if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 745e1d3c0fdSWill Deacon return NULL; 746e1d3c0fdSWill Deacon 747e1d3c0fdSWill Deacon data = kmalloc(sizeof(*data), GFP_KERNEL); 748e1d3c0fdSWill Deacon if (!data) 749e1d3c0fdSWill Deacon return NULL; 750e1d3c0fdSWill Deacon 7515fb190b0SRobin Murphy pg_shift = __ffs(cfg->pgsize_bitmap); 7525fb190b0SRobin Murphy data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte)); 753e1d3c0fdSWill Deacon 7545fb190b0SRobin Murphy va_bits = cfg->ias - pg_shift; 755594ab90fSRobin Murphy levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 756594ab90fSRobin Murphy data->start_level = ARM_LPAE_MAX_LEVELS - levels; 757e1d3c0fdSWill Deacon 758e1d3c0fdSWill Deacon /* Calculate the actual size of our pgd (without concatenation) */ 759c79278c1SRobin Murphy data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1)); 760e1d3c0fdSWill Deacon 761e1d3c0fdSWill Deacon data->iop.ops = (struct io_pgtable_ops) { 762e1d3c0fdSWill Deacon .map = arm_lpae_map, 763e1d3c0fdSWill Deacon .unmap = arm_lpae_unmap, 764e1d3c0fdSWill Deacon .iova_to_phys = arm_lpae_iova_to_phys, 765e1d3c0fdSWill Deacon }; 766e1d3c0fdSWill Deacon 767e1d3c0fdSWill Deacon return data; 768e1d3c0fdSWill Deacon } 769e1d3c0fdSWill Deacon 770e1d3c0fdSWill Deacon static struct io_pgtable * 771e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 772e1d3c0fdSWill Deacon { 773e1d3c0fdSWill Deacon u64 reg; 7743850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 775fb485eb1SRobin Murphy typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr; 776db690301SRobin Murphy bool tg1; 777e1d3c0fdSWill Deacon 7784f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | 779db690301SRobin Murphy IO_PGTABLE_QUIRK_NON_STRICT | 780e67890c9SSai Prakash Ranjan IO_PGTABLE_QUIRK_ARM_TTBR1 | 781e67890c9SSai Prakash Ranjan IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)) 7823850db49SRobin Murphy return NULL; 7833850db49SRobin Murphy 7843850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 785e1d3c0fdSWill Deacon if (!data) 786e1d3c0fdSWill Deacon return NULL; 787e1d3c0fdSWill Deacon 788e1d3c0fdSWill Deacon /* TCR */ 7899e6ea59fSBjorn Andersson if (cfg->coherent_walk) { 790fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_IS; 791fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 792fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 793e67890c9SSai Prakash Ranjan if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA) 794e67890c9SSai Prakash Ranjan goto out_free_data; 7959e6ea59fSBjorn Andersson } else { 796fb485eb1SRobin Murphy tcr->sh = ARM_LPAE_TCR_SH_OS; 797fb485eb1SRobin Murphy tcr->irgn = ARM_LPAE_TCR_RGN_NC; 798e67890c9SSai Prakash Ranjan if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)) 799fb485eb1SRobin Murphy tcr->orgn = ARM_LPAE_TCR_RGN_NC; 800e67890c9SSai Prakash Ranjan else 801e67890c9SSai Prakash Ranjan tcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 8029e6ea59fSBjorn Andersson } 803e1d3c0fdSWill Deacon 804db690301SRobin Murphy tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1; 80506c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 806e1d3c0fdSWill Deacon case SZ_4K: 807db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K; 808e1d3c0fdSWill Deacon break; 809e1d3c0fdSWill Deacon case SZ_16K: 810db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K; 811e1d3c0fdSWill Deacon break; 812e1d3c0fdSWill Deacon case SZ_64K: 813db690301SRobin Murphy tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K; 814e1d3c0fdSWill Deacon break; 815e1d3c0fdSWill Deacon } 816e1d3c0fdSWill Deacon 817e1d3c0fdSWill Deacon switch (cfg->oas) { 818e1d3c0fdSWill Deacon case 32: 819fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_32_BIT; 820e1d3c0fdSWill Deacon break; 821e1d3c0fdSWill Deacon case 36: 822fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_36_BIT; 823e1d3c0fdSWill Deacon break; 824e1d3c0fdSWill Deacon case 40: 825fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_40_BIT; 826e1d3c0fdSWill Deacon break; 827e1d3c0fdSWill Deacon case 42: 828fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_42_BIT; 829e1d3c0fdSWill Deacon break; 830e1d3c0fdSWill Deacon case 44: 831fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_44_BIT; 832e1d3c0fdSWill Deacon break; 833e1d3c0fdSWill Deacon case 48: 834fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_48_BIT; 835e1d3c0fdSWill Deacon break; 8366c89928fSRobin Murphy case 52: 837fb485eb1SRobin Murphy tcr->ips = ARM_LPAE_TCR_PS_52_BIT; 8386c89928fSRobin Murphy break; 839e1d3c0fdSWill Deacon default: 840e1d3c0fdSWill Deacon goto out_free_data; 841e1d3c0fdSWill Deacon } 842e1d3c0fdSWill Deacon 843fb485eb1SRobin Murphy tcr->tsz = 64ULL - cfg->ias; 844e1d3c0fdSWill Deacon 845e1d3c0fdSWill Deacon /* MAIRs */ 846e1d3c0fdSWill Deacon reg = (ARM_LPAE_MAIR_ATTR_NC 847e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 848e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_WBRWA 849e1d3c0fdSWill Deacon << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 850e1d3c0fdSWill Deacon (ARM_LPAE_MAIR_ATTR_DEVICE 85190ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) | 85290ec7a76SVivek Gautam (ARM_LPAE_MAIR_ATTR_INC_OWBRWA 85390ec7a76SVivek Gautam << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE)); 854e1d3c0fdSWill Deacon 855205577abSRobin Murphy cfg->arm_lpae_s1_cfg.mair = reg; 856e1d3c0fdSWill Deacon 857e1d3c0fdSWill Deacon /* Looking good; allocate a pgd */ 858c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 859c79278c1SRobin Murphy GFP_KERNEL, cfg); 860e1d3c0fdSWill Deacon if (!data->pgd) 861e1d3c0fdSWill Deacon goto out_free_data; 862e1d3c0fdSWill Deacon 86387a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 86487a91b15SRobin Murphy wmb(); 865e1d3c0fdSWill Deacon 866d1e5f26fSRobin Murphy /* TTBR */ 867d1e5f26fSRobin Murphy cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd); 868e1d3c0fdSWill Deacon return &data->iop; 869e1d3c0fdSWill Deacon 870e1d3c0fdSWill Deacon out_free_data: 871e1d3c0fdSWill Deacon kfree(data); 872e1d3c0fdSWill Deacon return NULL; 873e1d3c0fdSWill Deacon } 874e1d3c0fdSWill Deacon 875e1d3c0fdSWill Deacon static struct io_pgtable * 876e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 877e1d3c0fdSWill Deacon { 878ac4b80e5SWill Deacon u64 sl; 8793850db49SRobin Murphy struct arm_lpae_io_pgtable *data; 880ac4b80e5SWill Deacon typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr; 881e1d3c0fdSWill Deacon 8823850db49SRobin Murphy /* The NS quirk doesn't apply at stage 2 */ 8834f41845bSWill Deacon if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT)) 8843850db49SRobin Murphy return NULL; 8853850db49SRobin Murphy 8863850db49SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 887e1d3c0fdSWill Deacon if (!data) 888e1d3c0fdSWill Deacon return NULL; 889e1d3c0fdSWill Deacon 890e1d3c0fdSWill Deacon /* 891e1d3c0fdSWill Deacon * Concatenate PGDs at level 1 if possible in order to reduce 892e1d3c0fdSWill Deacon * the depth of the stage-2 walk. 893e1d3c0fdSWill Deacon */ 894594ab90fSRobin Murphy if (data->start_level == 0) { 895e1d3c0fdSWill Deacon unsigned long pgd_pages; 896e1d3c0fdSWill Deacon 897c79278c1SRobin Murphy pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte); 898e1d3c0fdSWill Deacon if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 899c79278c1SRobin Murphy data->pgd_bits += data->bits_per_level; 900594ab90fSRobin Murphy data->start_level++; 901e1d3c0fdSWill Deacon } 902e1d3c0fdSWill Deacon } 903e1d3c0fdSWill Deacon 904e1d3c0fdSWill Deacon /* VTCR */ 90530d2acb6SWill Deacon if (cfg->coherent_walk) { 906ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_IS; 907ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA; 908ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA; 90930d2acb6SWill Deacon } else { 910ac4b80e5SWill Deacon vtcr->sh = ARM_LPAE_TCR_SH_OS; 911ac4b80e5SWill Deacon vtcr->irgn = ARM_LPAE_TCR_RGN_NC; 912ac4b80e5SWill Deacon vtcr->orgn = ARM_LPAE_TCR_RGN_NC; 91330d2acb6SWill Deacon } 914e1d3c0fdSWill Deacon 915594ab90fSRobin Murphy sl = data->start_level; 916e1d3c0fdSWill Deacon 91706c610e8SRobin Murphy switch (ARM_LPAE_GRANULE(data)) { 918e1d3c0fdSWill Deacon case SZ_4K: 919ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_4K; 920e1d3c0fdSWill Deacon sl++; /* SL0 format is different for 4K granule size */ 921e1d3c0fdSWill Deacon break; 922e1d3c0fdSWill Deacon case SZ_16K: 923ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_16K; 924e1d3c0fdSWill Deacon break; 925e1d3c0fdSWill Deacon case SZ_64K: 926ac4b80e5SWill Deacon vtcr->tg = ARM_LPAE_TCR_TG0_64K; 927e1d3c0fdSWill Deacon break; 928e1d3c0fdSWill Deacon } 929e1d3c0fdSWill Deacon 930e1d3c0fdSWill Deacon switch (cfg->oas) { 931e1d3c0fdSWill Deacon case 32: 932ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_32_BIT; 933e1d3c0fdSWill Deacon break; 934e1d3c0fdSWill Deacon case 36: 935ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_36_BIT; 936e1d3c0fdSWill Deacon break; 937e1d3c0fdSWill Deacon case 40: 938ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_40_BIT; 939e1d3c0fdSWill Deacon break; 940e1d3c0fdSWill Deacon case 42: 941ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_42_BIT; 942e1d3c0fdSWill Deacon break; 943e1d3c0fdSWill Deacon case 44: 944ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_44_BIT; 945e1d3c0fdSWill Deacon break; 946e1d3c0fdSWill Deacon case 48: 947ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_48_BIT; 948e1d3c0fdSWill Deacon break; 9496c89928fSRobin Murphy case 52: 950ac4b80e5SWill Deacon vtcr->ps = ARM_LPAE_TCR_PS_52_BIT; 9516c89928fSRobin Murphy break; 952e1d3c0fdSWill Deacon default: 953e1d3c0fdSWill Deacon goto out_free_data; 954e1d3c0fdSWill Deacon } 955e1d3c0fdSWill Deacon 956ac4b80e5SWill Deacon vtcr->tsz = 64ULL - cfg->ias; 957ac4b80e5SWill Deacon vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK; 958e1d3c0fdSWill Deacon 959e1d3c0fdSWill Deacon /* Allocate pgd pages */ 960c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), 961c79278c1SRobin Murphy GFP_KERNEL, cfg); 962e1d3c0fdSWill Deacon if (!data->pgd) 963e1d3c0fdSWill Deacon goto out_free_data; 964e1d3c0fdSWill Deacon 96587a91b15SRobin Murphy /* Ensure the empty pgd is visible before any actual TTBR write */ 96687a91b15SRobin Murphy wmb(); 967e1d3c0fdSWill Deacon 968e1d3c0fdSWill Deacon /* VTTBR */ 969e1d3c0fdSWill Deacon cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 970e1d3c0fdSWill Deacon return &data->iop; 971e1d3c0fdSWill Deacon 972e1d3c0fdSWill Deacon out_free_data: 973e1d3c0fdSWill Deacon kfree(data); 974e1d3c0fdSWill Deacon return NULL; 975e1d3c0fdSWill Deacon } 976e1d3c0fdSWill Deacon 977e1d3c0fdSWill Deacon static struct io_pgtable * 978e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 979e1d3c0fdSWill Deacon { 980e1d3c0fdSWill Deacon if (cfg->ias > 32 || cfg->oas > 40) 981e1d3c0fdSWill Deacon return NULL; 982e1d3c0fdSWill Deacon 983e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 984fb485eb1SRobin Murphy return arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 985e1d3c0fdSWill Deacon } 986e1d3c0fdSWill Deacon 987e1d3c0fdSWill Deacon static struct io_pgtable * 988e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 989e1d3c0fdSWill Deacon { 990e1d3c0fdSWill Deacon if (cfg->ias > 40 || cfg->oas > 40) 991e1d3c0fdSWill Deacon return NULL; 992e1d3c0fdSWill Deacon 993e1d3c0fdSWill Deacon cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 994ac4b80e5SWill Deacon return arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 995e1d3c0fdSWill Deacon } 996e1d3c0fdSWill Deacon 997d08d42deSRob Herring static struct io_pgtable * 998d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie) 999d08d42deSRob Herring { 100052f325f4SRobin Murphy struct arm_lpae_io_pgtable *data; 1001d08d42deSRob Herring 100252f325f4SRobin Murphy /* No quirks for Mali (hopefully) */ 100352f325f4SRobin Murphy if (cfg->quirks) 100452f325f4SRobin Murphy return NULL; 1005d08d42deSRob Herring 10061be08f45SRobin Murphy if (cfg->ias > 48 || cfg->oas > 40) 1007d08d42deSRob Herring return NULL; 1008d08d42deSRob Herring 1009d08d42deSRob Herring cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 1010d08d42deSRob Herring 101152f325f4SRobin Murphy data = arm_lpae_alloc_pgtable(cfg); 101252f325f4SRobin Murphy if (!data) 101352f325f4SRobin Murphy return NULL; 1014d08d42deSRob Herring 10151be08f45SRobin Murphy /* Mali seems to need a full 4-level table regardless of IAS */ 1016594ab90fSRobin Murphy if (data->start_level > 0) { 1017594ab90fSRobin Murphy data->start_level = 0; 1018c79278c1SRobin Murphy data->pgd_bits = 0; 10191be08f45SRobin Murphy } 102052f325f4SRobin Murphy /* 102152f325f4SRobin Murphy * MEMATTR: Mali has no actual notion of a non-cacheable type, so the 102252f325f4SRobin Murphy * best we can do is mimic the out-of-tree driver and hope that the 102352f325f4SRobin Murphy * "implementation-defined caching policy" is good enough. Similarly, 102452f325f4SRobin Murphy * we'll use it for the sake of a valid attribute for our 'device' 102552f325f4SRobin Murphy * index, although callers should never request that in practice. 102652f325f4SRobin Murphy */ 102752f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.memattr = 102852f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 102952f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 103052f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 103152f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 103252f325f4SRobin Murphy (ARM_MALI_LPAE_MEMATTR_IMP_DEF 103352f325f4SRobin Murphy << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 103452f325f4SRobin Murphy 1035c79278c1SRobin Murphy data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL, 1036c79278c1SRobin Murphy cfg); 103752f325f4SRobin Murphy if (!data->pgd) 103852f325f4SRobin Murphy goto out_free_data; 103952f325f4SRobin Murphy 104052f325f4SRobin Murphy /* Ensure the empty pgd is visible before TRANSTAB can be written */ 104152f325f4SRobin Murphy wmb(); 104252f325f4SRobin Murphy 104352f325f4SRobin Murphy cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) | 1044d08d42deSRob Herring ARM_MALI_LPAE_TTBR_READ_INNER | 1045d08d42deSRob Herring ARM_MALI_LPAE_TTBR_ADRMODE_TABLE; 1046728da60dSRobin Murphy if (cfg->coherent_walk) 1047728da60dSRobin Murphy cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER; 1048728da60dSRobin Murphy 104952f325f4SRobin Murphy return &data->iop; 1050d08d42deSRob Herring 105152f325f4SRobin Murphy out_free_data: 105252f325f4SRobin Murphy kfree(data); 105352f325f4SRobin Murphy return NULL; 1054d08d42deSRob Herring } 1055d08d42deSRob Herring 1056e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 1057e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s1, 1058e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1059e1d3c0fdSWill Deacon }; 1060e1d3c0fdSWill Deacon 1061e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 1062e1d3c0fdSWill Deacon .alloc = arm_64_lpae_alloc_pgtable_s2, 1063e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1064e1d3c0fdSWill Deacon }; 1065e1d3c0fdSWill Deacon 1066e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 1067e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s1, 1068e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1069e1d3c0fdSWill Deacon }; 1070e1d3c0fdSWill Deacon 1071e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 1072e1d3c0fdSWill Deacon .alloc = arm_32_lpae_alloc_pgtable_s2, 1073e1d3c0fdSWill Deacon .free = arm_lpae_free_pgtable, 1074e1d3c0fdSWill Deacon }; 1075fe4b991dSWill Deacon 1076d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = { 1077d08d42deSRob Herring .alloc = arm_mali_lpae_alloc_pgtable, 1078d08d42deSRob Herring .free = arm_lpae_free_pgtable, 1079d08d42deSRob Herring }; 1080d08d42deSRob Herring 1081fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 1082fe4b991dSWill Deacon 1083b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata; 1084fe4b991dSWill Deacon 1085b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie) 1086fe4b991dSWill Deacon { 1087fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1088fe4b991dSWill Deacon } 1089fe4b991dSWill Deacon 1090b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size, 1091b5813c16SRobin Murphy size_t granule, void *cookie) 1092fe4b991dSWill Deacon { 1093fe4b991dSWill Deacon WARN_ON(cookie != cfg_cookie); 1094fe4b991dSWill Deacon WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 1095fe4b991dSWill Deacon } 1096fe4b991dSWill Deacon 1097b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather, 1098b5813c16SRobin Murphy unsigned long iova, size_t granule, 1099b5813c16SRobin Murphy void *cookie) 110010b7a7d9SWill Deacon { 1101abfd6fe0SWill Deacon dummy_tlb_flush(iova, granule, granule, cookie); 110210b7a7d9SWill Deacon } 110310b7a7d9SWill Deacon 1104298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = { 1105fe4b991dSWill Deacon .tlb_flush_all = dummy_tlb_flush_all, 110610b7a7d9SWill Deacon .tlb_flush_walk = dummy_tlb_flush, 1107abfd6fe0SWill Deacon .tlb_add_page = dummy_tlb_add_page, 1108fe4b991dSWill Deacon }; 1109fe4b991dSWill Deacon 1110fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 1111fe4b991dSWill Deacon { 1112fe4b991dSWill Deacon struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 1113fe4b991dSWill Deacon struct io_pgtable_cfg *cfg = &data->iop.cfg; 1114fe4b991dSWill Deacon 1115fe4b991dSWill Deacon pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 1116fe4b991dSWill Deacon cfg->pgsize_bitmap, cfg->ias); 11175fb190b0SRobin Murphy pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n", 1118c79278c1SRobin Murphy ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data), 11195fb190b0SRobin Murphy ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd); 1120fe4b991dSWill Deacon } 1121fe4b991dSWill Deacon 1122fe4b991dSWill Deacon #define __FAIL(ops, i) ({ \ 1123fe4b991dSWill Deacon WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 1124fe4b991dSWill Deacon arm_lpae_dump_ops(ops); \ 1125fe4b991dSWill Deacon selftest_running = false; \ 1126fe4b991dSWill Deacon -EFAULT; \ 1127fe4b991dSWill Deacon }) 1128fe4b991dSWill Deacon 1129fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 1130fe4b991dSWill Deacon { 11319062c1d0SChristophe JAILLET static const enum io_pgtable_fmt fmts[] __initconst = { 1132fe4b991dSWill Deacon ARM_64_LPAE_S1, 1133fe4b991dSWill Deacon ARM_64_LPAE_S2, 1134fe4b991dSWill Deacon }; 1135fe4b991dSWill Deacon 1136fe4b991dSWill Deacon int i, j; 1137fe4b991dSWill Deacon unsigned long iova; 1138fe4b991dSWill Deacon size_t size; 1139fe4b991dSWill Deacon struct io_pgtable_ops *ops; 1140fe4b991dSWill Deacon 1141fe4b991dSWill Deacon selftest_running = true; 1142fe4b991dSWill Deacon 1143fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 1144fe4b991dSWill Deacon cfg_cookie = cfg; 1145fe4b991dSWill Deacon ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 1146fe4b991dSWill Deacon if (!ops) { 1147fe4b991dSWill Deacon pr_err("selftest: failed to allocate io pgtable ops\n"); 1148fe4b991dSWill Deacon return -ENOMEM; 1149fe4b991dSWill Deacon } 1150fe4b991dSWill Deacon 1151fe4b991dSWill Deacon /* 1152fe4b991dSWill Deacon * Initial sanity checks. 1153fe4b991dSWill Deacon * Empty page tables shouldn't provide any translations. 1154fe4b991dSWill Deacon */ 1155fe4b991dSWill Deacon if (ops->iova_to_phys(ops, 42)) 1156fe4b991dSWill Deacon return __FAIL(ops, i); 1157fe4b991dSWill Deacon 1158fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + 42)) 1159fe4b991dSWill Deacon return __FAIL(ops, i); 1160fe4b991dSWill Deacon 1161fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_2G + 42)) 1162fe4b991dSWill Deacon return __FAIL(ops, i); 1163fe4b991dSWill Deacon 1164fe4b991dSWill Deacon /* 1165fe4b991dSWill Deacon * Distinct mappings of different granule sizes. 1166fe4b991dSWill Deacon */ 1167fe4b991dSWill Deacon iova = 0; 11684ae8a5c5SKefeng Wang for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1169fe4b991dSWill Deacon size = 1UL << j; 1170fe4b991dSWill Deacon 1171fe4b991dSWill Deacon if (ops->map(ops, iova, iova, size, IOMMU_READ | 1172fe4b991dSWill Deacon IOMMU_WRITE | 1173fe4b991dSWill Deacon IOMMU_NOEXEC | 1174f34ce7a7SBaolin Wang IOMMU_CACHE, GFP_KERNEL)) 1175fe4b991dSWill Deacon return __FAIL(ops, i); 1176fe4b991dSWill Deacon 1177fe4b991dSWill Deacon /* Overlapping mappings */ 1178fe4b991dSWill Deacon if (!ops->map(ops, iova, iova + size, size, 1179f34ce7a7SBaolin Wang IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL)) 1180fe4b991dSWill Deacon return __FAIL(ops, i); 1181fe4b991dSWill Deacon 1182fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1183fe4b991dSWill Deacon return __FAIL(ops, i); 1184fe4b991dSWill Deacon 1185fe4b991dSWill Deacon iova += SZ_1G; 1186fe4b991dSWill Deacon } 1187fe4b991dSWill Deacon 1188fe4b991dSWill Deacon /* Partial unmap */ 1189fe4b991dSWill Deacon size = 1UL << __ffs(cfg->pgsize_bitmap); 1190a2d3a382SWill Deacon if (ops->unmap(ops, SZ_1G + size, size, NULL) != size) 1191fe4b991dSWill Deacon return __FAIL(ops, i); 1192fe4b991dSWill Deacon 1193fe4b991dSWill Deacon /* Remap of partial unmap */ 1194f34ce7a7SBaolin Wang if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL)) 1195fe4b991dSWill Deacon return __FAIL(ops, i); 1196fe4b991dSWill Deacon 1197fe4b991dSWill Deacon if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 1198fe4b991dSWill Deacon return __FAIL(ops, i); 1199fe4b991dSWill Deacon 1200fe4b991dSWill Deacon /* Full unmap */ 1201fe4b991dSWill Deacon iova = 0; 1202f793b13eSYueHaibing for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) { 1203fe4b991dSWill Deacon size = 1UL << j; 1204fe4b991dSWill Deacon 1205a2d3a382SWill Deacon if (ops->unmap(ops, iova, size, NULL) != size) 1206fe4b991dSWill Deacon return __FAIL(ops, i); 1207fe4b991dSWill Deacon 1208fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42)) 1209fe4b991dSWill Deacon return __FAIL(ops, i); 1210fe4b991dSWill Deacon 1211fe4b991dSWill Deacon /* Remap full block */ 1212f34ce7a7SBaolin Wang if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL)) 1213fe4b991dSWill Deacon return __FAIL(ops, i); 1214fe4b991dSWill Deacon 1215fe4b991dSWill Deacon if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 1216fe4b991dSWill Deacon return __FAIL(ops, i); 1217fe4b991dSWill Deacon 1218fe4b991dSWill Deacon iova += SZ_1G; 1219fe4b991dSWill Deacon } 1220fe4b991dSWill Deacon 1221fe4b991dSWill Deacon free_io_pgtable_ops(ops); 1222fe4b991dSWill Deacon } 1223fe4b991dSWill Deacon 1224fe4b991dSWill Deacon selftest_running = false; 1225fe4b991dSWill Deacon return 0; 1226fe4b991dSWill Deacon } 1227fe4b991dSWill Deacon 1228fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void) 1229fe4b991dSWill Deacon { 12309062c1d0SChristophe JAILLET static const unsigned long pgsize[] __initconst = { 1231fe4b991dSWill Deacon SZ_4K | SZ_2M | SZ_1G, 1232fe4b991dSWill Deacon SZ_16K | SZ_32M, 1233fe4b991dSWill Deacon SZ_64K | SZ_512M, 1234fe4b991dSWill Deacon }; 1235fe4b991dSWill Deacon 12369062c1d0SChristophe JAILLET static const unsigned int ias[] __initconst = { 1237fe4b991dSWill Deacon 32, 36, 40, 42, 44, 48, 1238fe4b991dSWill Deacon }; 1239fe4b991dSWill Deacon 1240fe4b991dSWill Deacon int i, j, pass = 0, fail = 0; 1241fe4b991dSWill Deacon struct io_pgtable_cfg cfg = { 1242fe4b991dSWill Deacon .tlb = &dummy_tlb_ops, 1243fe4b991dSWill Deacon .oas = 48, 12444f41845bSWill Deacon .coherent_walk = true, 1245fe4b991dSWill Deacon }; 1246fe4b991dSWill Deacon 1247fe4b991dSWill Deacon for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 1248fe4b991dSWill Deacon for (j = 0; j < ARRAY_SIZE(ias); ++j) { 1249fe4b991dSWill Deacon cfg.pgsize_bitmap = pgsize[i]; 1250fe4b991dSWill Deacon cfg.ias = ias[j]; 1251fe4b991dSWill Deacon pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 1252fe4b991dSWill Deacon pgsize[i], ias[j]); 1253fe4b991dSWill Deacon if (arm_lpae_run_tests(&cfg)) 1254fe4b991dSWill Deacon fail++; 1255fe4b991dSWill Deacon else 1256fe4b991dSWill Deacon pass++; 1257fe4b991dSWill Deacon } 1258fe4b991dSWill Deacon } 1259fe4b991dSWill Deacon 1260fe4b991dSWill Deacon pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 1261fe4b991dSWill Deacon return fail ? -EFAULT : 0; 1262fe4b991dSWill Deacon } 1263fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests); 1264fe4b991dSWill Deacon #endif 1265