2019-06-03 05:44:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2014-11-14 17:18:23 +00:00
|
|
|
/*
|
|
|
|
* CPU-agnostic ARM page table allocator.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 ARM Limited
|
|
|
|
*
|
|
|
|
* Author: Will Deacon <will.deacon@arm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
|
|
|
|
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
#include <linux/atomic.h>
|
2018-03-26 12:35:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2019-02-05 16:37:31 +00:00
|
|
|
#include <linux/io-pgtable.h>
|
2014-11-14 17:18:23 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sizes.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/types.h>
|
2016-01-27 11:10:32 +00:00
|
|
|
#include <linux/dma-mapping.h>
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2015-07-29 18:46:09 +00:00
|
|
|
#include <asm/barrier.h>
|
|
|
|
|
2020-09-18 10:18:45 +00:00
|
|
|
#include "io-pgtable-arm.h"
|
2024-04-13 00:25:15 +00:00
|
|
|
#include "iommu-pages.h"
|
2020-09-18 10:18:45 +00:00
|
|
|
|
2018-03-26 12:35:13 +00:00
|
|
|
#define ARM_LPAE_MAX_ADDR_BITS 52
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
|
|
|
|
#define ARM_LPAE_MAX_LEVELS 4
|
|
|
|
|
|
|
|
/* Struct accessors */
|
|
|
|
#define io_pgtable_to_data(x) \
|
|
|
|
container_of((x), struct arm_lpae_io_pgtable, iop)
|
|
|
|
|
|
|
|
#define io_pgtable_ops_to_data(x) \
|
|
|
|
io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the right shift amount to get to the portion describing level l
|
|
|
|
* in a virtual address mapped by the pagetable in d.
|
|
|
|
*/
|
|
|
|
#define ARM_LPAE_LVL_SHIFT(l,d) \
|
2019-10-25 18:08:35 +00:00
|
|
|
(((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
|
|
|
|
ilog2(sizeof(arm_lpae_iopte)))
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-10-25 18:08:35 +00:00
|
|
|
#define ARM_LPAE_GRANULE(d) \
|
|
|
|
(sizeof(arm_lpae_iopte) << (d)->bits_per_level)
|
2019-10-25 18:08:34 +00:00
|
|
|
#define ARM_LPAE_PGD_SIZE(d) \
|
|
|
|
(sizeof(arm_lpae_iopte) << (d)->pgd_bits)
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
#define ARM_LPAE_PTES_PER_TABLE(d) \
|
|
|
|
(ARM_LPAE_GRANULE(d) >> ilog2(sizeof(arm_lpae_iopte)))
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
/*
|
|
|
|
* Calculate the index at level l used to map virtual address a using the
|
|
|
|
* pagetable in d.
|
|
|
|
*/
|
|
|
|
#define ARM_LPAE_PGD_IDX(l,d) \
|
2019-10-25 18:08:34 +00:00
|
|
|
((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
#define ARM_LPAE_LVL_IDX(a,l,d) \
|
2015-02-16 18:38:20 +00:00
|
|
|
(((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
|
2014-11-14 17:18:23 +00:00
|
|
|
((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
|
|
|
|
|
|
|
|
/* Calculate the block/page mapping size at level l for pagetable in d. */
|
2019-10-25 18:08:35 +00:00
|
|
|
#define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Page table bits */
|
|
|
|
#define ARM_LPAE_PTE_TYPE_SHIFT 0
|
|
|
|
#define ARM_LPAE_PTE_TYPE_MASK 0x3
|
|
|
|
|
|
|
|
#define ARM_LPAE_PTE_TYPE_BLOCK 1
|
|
|
|
#define ARM_LPAE_PTE_TYPE_TABLE 3
|
|
|
|
#define ARM_LPAE_PTE_TYPE_PAGE 3
|
|
|
|
|
2018-03-26 12:35:13 +00:00
|
|
|
#define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
|
|
|
|
|
2014-12-14 21:34:50 +00:00
|
|
|
#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
|
2024-07-03 10:16:02 +00:00
|
|
|
#define ARM_LPAE_PTE_DBM (((arm_lpae_iopte)1) << 51)
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
|
|
|
|
#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
|
|
|
|
#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
|
|
|
|
#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
|
2014-12-14 21:34:50 +00:00
|
|
|
#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
|
|
|
|
|
|
|
|
#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
|
|
|
|
/* Ignore the contiguous bit for block splitting */
|
2024-07-03 10:16:02 +00:00
|
|
|
#define ARM_LPAE_PTE_ATTR_HI_MASK (ARM_LPAE_PTE_XN | ARM_LPAE_PTE_DBM)
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
|
|
|
|
ARM_LPAE_PTE_ATTR_HI_MASK)
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
/* Software bit for solving coherency races */
|
|
|
|
#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Stage-1 PTE */
|
|
|
|
#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
|
2024-07-03 10:16:02 +00:00
|
|
|
#define ARM_LPAE_PTE_AP_RDONLY_BIT 7
|
|
|
|
#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)1) << \
|
|
|
|
ARM_LPAE_PTE_AP_RDONLY_BIT)
|
|
|
|
#define ARM_LPAE_PTE_AP_WR_CLEAN_MASK (ARM_LPAE_PTE_AP_RDONLY | \
|
|
|
|
ARM_LPAE_PTE_DBM)
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
|
|
|
|
#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
|
|
|
|
|
|
|
|
/* Stage-2 PTE */
|
|
|
|
#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
|
|
|
|
#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
|
|
|
|
#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
|
|
|
|
#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
|
|
|
|
#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
|
|
|
|
#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
|
|
|
|
|
|
|
|
/* Register bits */
|
2019-10-25 18:08:38 +00:00
|
|
|
#define ARM_LPAE_VTCR_SL0_MASK 0x3
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
#define ARM_LPAE_TCR_T0SZ_SHIFT 0
|
|
|
|
|
2019-10-25 18:08:38 +00:00
|
|
|
#define ARM_LPAE_VTCR_PS_SHIFT 16
|
|
|
|
#define ARM_LPAE_VTCR_PS_MASK 0x7
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_MASK 0xff
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_NC 0x44
|
iommu/io-pgtable-arm: Add support to use system cache
Few Qualcomm platforms such as, sdm845 have an additional outer
cache called as System cache, aka. Last level cache (LLC) that
allows non-coherent devices to upgrade to using caching.
This cache sits right before the DDR, and is tightly coupled
with the memory controller. The clients using this cache request
their slices from this system cache, make it active, and can then
start using it.
There is a fundamental assumption that non-coherent devices can't
access caches. This change adds an exception where they *can* use
some level of cache despite still being non-coherent overall.
The coherent devices that use cacheable memory, and CPU make use of
this system cache by default.
Looking at memory types, we have following -
a) Normal uncached :- MAIR 0x44, inner non-cacheable,
outer non-cacheable;
b) Normal cached :- MAIR 0xff, inner read write-back non-transient,
outer read write-back non-transient;
attribute setting for coherenet I/O devices.
and, for non-coherent i/o devices that can allocate in system cache
another type gets added -
c) Normal sys-cached :- MAIR 0xf4, inner non-cacheable,
outer read write-back non-transient
Coherent I/O devices use system cache by marking the memory as
normal cached.
Non-coherent I/O devices should mark the memory as normal
sys-cached in page tables to use system cache.
Acked-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Vivek Gautam <vivek.gautam@codeaurora.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2019-05-16 09:30:20 +00:00
|
|
|
#define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
|
2014-11-14 17:18:23 +00:00
|
|
|
#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
|
|
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
|
iommu/io-pgtable-arm: Add support to use system cache
Few Qualcomm platforms such as, sdm845 have an additional outer
cache called as System cache, aka. Last level cache (LLC) that
allows non-coherent devices to upgrade to using caching.
This cache sits right before the DDR, and is tightly coupled
with the memory controller. The clients using this cache request
their slices from this system cache, make it active, and can then
start using it.
There is a fundamental assumption that non-coherent devices can't
access caches. This change adds an exception where they *can* use
some level of cache despite still being non-coherent overall.
The coherent devices that use cacheable memory, and CPU make use of
this system cache by default.
Looking at memory types, we have following -
a) Normal uncached :- MAIR 0x44, inner non-cacheable,
outer non-cacheable;
b) Normal cached :- MAIR 0xff, inner read write-back non-transient,
outer read write-back non-transient;
attribute setting for coherenet I/O devices.
and, for non-coherent i/o devices that can allocate in system cache
another type gets added -
c) Normal sys-cached :- MAIR 0xf4, inner non-cacheable,
outer read write-back non-transient
Coherent I/O devices use system cache by marking the memory as
normal cached.
Non-coherent I/O devices should mark the memory as normal
sys-cached in page tables to use system cache.
Acked-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Vivek Gautam <vivek.gautam@codeaurora.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2019-05-16 09:30:20 +00:00
|
|
|
#define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
#define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
|
|
|
|
#define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
|
|
|
|
#define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
|
|
|
|
|
2019-09-30 14:11:00 +00:00
|
|
|
#define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
|
|
|
|
#define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
/* IOPTE accessors */
|
2018-03-26 12:35:13 +00:00
|
|
|
#define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2020-12-07 12:01:50 +00:00
|
|
|
#define iopte_type(pte) \
|
2014-11-14 17:18:23 +00:00
|
|
|
(((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
|
|
|
|
|
|
|
|
#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
|
|
|
|
|
2024-07-03 10:16:02 +00:00
|
|
|
#define iopte_writeable_dirty(pte) \
|
|
|
|
(((pte) & ARM_LPAE_PTE_AP_WR_CLEAN_MASK) == ARM_LPAE_PTE_DBM)
|
|
|
|
|
|
|
|
#define iopte_set_writeable_clean(ptep) \
|
|
|
|
set_bit(ARM_LPAE_PTE_AP_RDONLY_BIT, (unsigned long *)(ptep))
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
struct arm_lpae_io_pgtable {
|
|
|
|
struct io_pgtable iop;
|
|
|
|
|
2019-10-25 18:08:34 +00:00
|
|
|
int pgd_bits;
|
2019-10-25 18:08:33 +00:00
|
|
|
int start_level;
|
2019-10-25 18:08:35 +00:00
|
|
|
int bits_per_level;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
void *pgd;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef u64 arm_lpae_iopte;
|
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
|
|
|
|
enum io_pgtable_fmt fmt)
|
|
|
|
{
|
|
|
|
if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
|
2020-12-07 12:01:50 +00:00
|
|
|
return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE;
|
2019-02-21 20:23:25 +00:00
|
|
|
|
2020-12-07 12:01:50 +00:00
|
|
|
return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK;
|
2019-02-21 20:23:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 10:16:02 +00:00
|
|
|
static inline bool iopte_table(arm_lpae_iopte pte, int lvl)
|
|
|
|
{
|
|
|
|
if (lvl == (ARM_LPAE_MAX_LEVELS - 1))
|
|
|
|
return false;
|
|
|
|
return iopte_type(pte) == ARM_LPAE_PTE_TYPE_TABLE;
|
|
|
|
}
|
|
|
|
|
2018-03-26 12:35:13 +00:00
|
|
|
static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
|
|
|
|
struct arm_lpae_io_pgtable *data)
|
|
|
|
{
|
|
|
|
arm_lpae_iopte pte = paddr;
|
|
|
|
|
|
|
|
/* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
|
|
|
|
return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
|
|
|
|
struct arm_lpae_io_pgtable *data)
|
|
|
|
{
|
2018-03-29 11:24:52 +00:00
|
|
|
u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
|
2018-03-26 12:35:13 +00:00
|
|
|
|
2019-10-25 18:08:35 +00:00
|
|
|
if (ARM_LPAE_GRANULE(data) < SZ_64K)
|
2018-03-26 12:35:13 +00:00
|
|
|
return paddr;
|
|
|
|
|
|
|
|
/* Rotate the packed high-order bits back to the top */
|
|
|
|
return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:31:12 +00:00
|
|
|
static bool selftest_running = false;
|
|
|
|
|
2015-09-17 16:42:16 +00:00
|
|
|
static dma_addr_t __arm_lpae_dma_addr(void *pages)
|
2015-07-29 18:46:04 +00:00
|
|
|
{
|
2015-09-17 16:42:16 +00:00
|
|
|
return (dma_addr_t)virt_to_phys(pages);
|
2015-07-29 18:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
|
2023-11-24 14:24:34 +00:00
|
|
|
struct io_pgtable_cfg *cfg,
|
|
|
|
void *cookie)
|
2015-07-29 18:46:04 +00:00
|
|
|
{
|
|
|
|
struct device *dev = cfg->iommu_dev;
|
2018-05-22 11:50:09 +00:00
|
|
|
int order = get_order(size);
|
2015-07-29 18:46:04 +00:00
|
|
|
dma_addr_t dma;
|
2018-05-22 11:50:09 +00:00
|
|
|
void *pages;
|
2015-07-29 18:46:04 +00:00
|
|
|
|
2018-05-22 11:50:09 +00:00
|
|
|
VM_BUG_ON((gfp & __GFP_HIGHMEM));
|
2023-11-24 14:24:34 +00:00
|
|
|
|
2024-04-13 00:25:15 +00:00
|
|
|
if (cfg->alloc)
|
2023-11-24 14:24:34 +00:00
|
|
|
pages = cfg->alloc(cookie, size, gfp);
|
2024-04-13 00:25:15 +00:00
|
|
|
else
|
|
|
|
pages = iommu_alloc_pages_node(dev_to_node(dev), gfp, order);
|
2023-11-24 14:24:34 +00:00
|
|
|
|
|
|
|
if (!pages)
|
2015-07-29 18:46:04 +00:00
|
|
|
return NULL;
|
|
|
|
|
2019-06-25 11:51:25 +00:00
|
|
|
if (!cfg->coherent_walk) {
|
2015-07-29 18:46:04 +00:00
|
|
|
dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
|
|
|
|
if (dma_mapping_error(dev, dma))
|
|
|
|
goto out_free;
|
|
|
|
/*
|
|
|
|
* We depend on the IOMMU being able to work with any physical
|
2015-09-17 16:42:16 +00:00
|
|
|
* address directly, so if the DMA layer suggests otherwise by
|
|
|
|
* translating or truncating them, that bodes very badly...
|
2015-07-29 18:46:04 +00:00
|
|
|
*/
|
2015-09-17 16:42:16 +00:00
|
|
|
if (dma != virt_to_phys(pages))
|
2015-07-29 18:46:04 +00:00
|
|
|
goto out_unmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
|
|
|
|
out_unmap:
|
|
|
|
dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
|
|
|
|
dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
|
2023-11-24 14:24:34 +00:00
|
|
|
|
2015-07-29 18:46:04 +00:00
|
|
|
out_free:
|
2023-11-24 14:24:34 +00:00
|
|
|
if (cfg->free)
|
|
|
|
cfg->free(cookie, pages, size);
|
|
|
|
else
|
2024-04-13 00:25:15 +00:00
|
|
|
iommu_free_pages(pages, order);
|
2023-11-24 14:24:34 +00:00
|
|
|
|
2015-07-29 18:46:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __arm_lpae_free_pages(void *pages, size_t size,
|
2023-11-24 14:24:34 +00:00
|
|
|
struct io_pgtable_cfg *cfg,
|
|
|
|
void *cookie)
|
2015-07-29 18:46:04 +00:00
|
|
|
{
|
2019-06-25 11:51:25 +00:00
|
|
|
if (!cfg->coherent_walk)
|
2015-09-17 16:42:16 +00:00
|
|
|
dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
|
2015-07-29 18:46:04 +00:00
|
|
|
size, DMA_TO_DEVICE);
|
2023-11-24 14:24:34 +00:00
|
|
|
|
|
|
|
if (cfg->free)
|
|
|
|
cfg->free(cookie, pages, size);
|
|
|
|
else
|
2024-04-13 00:25:15 +00:00
|
|
|
iommu_free_pages(pages, get_order(size));
|
2015-07-29 18:46:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 13:38:50 +00:00
|
|
|
static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries,
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
struct io_pgtable_cfg *cfg)
|
|
|
|
{
|
|
|
|
dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
|
2021-06-16 13:38:50 +00:00
|
|
|
sizeof(*ptep) * num_entries, DMA_TO_DEVICE);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 10:51:35 +00:00
|
|
|
static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg, int num_entries)
|
2015-07-29 18:46:04 +00:00
|
|
|
{
|
2024-08-06 10:51:35 +00:00
|
|
|
for (int i = 0; i < num_entries; i++)
|
|
|
|
ptep[i] = 0;
|
2021-06-16 13:38:50 +00:00
|
|
|
|
2024-08-06 10:51:35 +00:00
|
|
|
if (!cfg->coherent_walk && num_entries)
|
|
|
|
__arm_lpae_sync_pte(ptep, num_entries, cfg);
|
2015-07-29 18:46:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 17:59:19 +00:00
|
|
|
static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
|
2019-07-02 15:45:15 +00:00
|
|
|
struct iommu_iotlb_gather *gather,
|
2021-06-16 13:38:51 +00:00
|
|
|
unsigned long iova, size_t size, size_t pgcount,
|
|
|
|
int lvl, arm_lpae_iopte *ptep);
|
iommu/io-pgtable-arm: Unmap and free table when overwriting with block
When installing a block mapping, we unconditionally overwrite a non-leaf
PTE if we find one. However, this can cause a problem if the following
sequence of events occur:
(1) iommu_map called for a 4k (i.e. PAGE_SIZE) mapping at some address
- We initialise the page table all the way down to a leaf entry
- No TLB maintenance is required, because we're going from invalid
to valid.
(2) iommu_unmap is called on the mapping installed in (1)
- We walk the page table to the final (leaf) entry and zero it
- We only changed a valid leaf entry, so we invalidate leaf-only
(3) iommu_map is called on the same address as (1), but this time for
a 2MB (i.e. BLOCK_SIZE) mapping)
- We walk the page table down to the penultimate level, where we
find a table entry
- We overwrite the table entry with a block mapping and return
without any TLB maintenance and without freeing the memory used
by the now-orphaned table.
This last step can lead to a walk-cache caching the overwritten table
entry, causing unexpected faults when the new mapping is accessed by a
device. One way to fix this would be to collapse the page table when
freeing the last page at a given level, but this would require expensive
iteration on every map call. Instead, this patch detects the case when
we are overwriting a table entry and explicitly unmaps the table first,
which takes care of both freeing and TLB invalidation.
Cc: <stable@vger.kernel.org>
Reported-by: Brian Starkey <brian.starkey@arm.com>
Tested-by: Brian Starkey <brian.starkey@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
2015-08-11 15:48:32 +00:00
|
|
|
|
2017-06-22 15:53:51 +00:00
|
|
|
static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
|
|
|
|
phys_addr_t paddr, arm_lpae_iopte prot,
|
2021-06-16 13:38:50 +00:00
|
|
|
int lvl, int num_entries, arm_lpae_iopte *ptep)
|
2017-06-22 15:53:51 +00:00
|
|
|
{
|
|
|
|
arm_lpae_iopte pte = prot;
|
2021-06-16 13:38:50 +00:00
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
|
|
|
int i;
|
2017-06-22 15:53:51 +00:00
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
|
2017-06-22 15:53:51 +00:00
|
|
|
pte |= ARM_LPAE_PTE_TYPE_PAGE;
|
|
|
|
else
|
|
|
|
pte |= ARM_LPAE_PTE_TYPE_BLOCK;
|
|
|
|
|
2021-06-16 13:38:50 +00:00
|
|
|
for (i = 0; i < num_entries; i++)
|
|
|
|
ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data);
|
2017-06-22 15:53:51 +00:00
|
|
|
|
2021-06-16 13:38:50 +00:00
|
|
|
if (!cfg->coherent_walk)
|
|
|
|
__arm_lpae_sync_pte(ptep, num_entries, cfg);
|
2017-06-22 15:53:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
|
|
|
|
unsigned long iova, phys_addr_t paddr,
|
2021-06-16 13:38:50 +00:00
|
|
|
arm_lpae_iopte prot, int lvl, int num_entries,
|
2014-11-14 17:18:23 +00:00
|
|
|
arm_lpae_iopte *ptep)
|
|
|
|
{
|
2021-06-16 13:38:50 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_entries; i++)
|
|
|
|
if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
|
|
|
|
/* We require an unmap first */
|
|
|
|
WARN_ON(!selftest_running);
|
|
|
|
return -EEXIST;
|
|
|
|
} else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) {
|
|
|
|
/*
|
|
|
|
* We need to unmap and free the old table before
|
|
|
|
* overwriting it with a block entry.
|
|
|
|
*/
|
|
|
|
arm_lpae_iopte *tblp;
|
|
|
|
size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
|
|
|
|
|
|
|
tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
|
2021-06-16 13:38:51 +00:00
|
|
|
if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1,
|
2021-06-16 13:38:50 +00:00
|
|
|
lvl, tblp) != sz) {
|
|
|
|
WARN_ON(1);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-07-02 15:45:15 +00:00
|
|
|
}
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2021-06-16 13:38:50 +00:00
|
|
|
__arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep);
|
2017-06-22 15:53:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-14 21:34:50 +00:00
|
|
|
|
2017-06-22 15:53:51 +00:00
|
|
|
static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
|
|
|
|
arm_lpae_iopte *ptep,
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
arm_lpae_iopte curr,
|
2021-11-20 03:13:43 +00:00
|
|
|
struct arm_lpae_io_pgtable *data)
|
2017-06-22 15:53:51 +00:00
|
|
|
{
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
arm_lpae_iopte old, new;
|
2021-11-20 03:13:43 +00:00
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2021-11-20 03:13:43 +00:00
|
|
|
new = paddr_to_iopte(__pa(table), data) | ARM_LPAE_PTE_TYPE_TABLE;
|
2017-06-22 15:53:51 +00:00
|
|
|
if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
|
|
|
|
new |= ARM_LPAE_PTE_NSTABLE;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2017-06-23 11:02:38 +00:00
|
|
|
/*
|
|
|
|
* Ensure the table itself is visible before its PTE can be.
|
|
|
|
* Whilst we could get away with cmpxchg64_release below, this
|
|
|
|
* doesn't have any ordering semantics when !CONFIG_SMP.
|
|
|
|
*/
|
|
|
|
dma_wmb();
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
|
|
|
|
old = cmpxchg64_relaxed(ptep, curr, new);
|
|
|
|
|
2019-06-25 11:51:25 +00:00
|
|
|
if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
return old;
|
|
|
|
|
|
|
|
/* Even if it's not ours, there's no point waiting; just kick it */
|
2021-06-16 13:38:50 +00:00
|
|
|
__arm_lpae_sync_pte(ptep, 1, cfg);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
if (old == curr)
|
|
|
|
WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
|
|
|
|
|
|
|
|
return old;
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
|
2021-06-16 13:38:52 +00:00
|
|
|
phys_addr_t paddr, size_t size, size_t pgcount,
|
|
|
|
arm_lpae_iopte prot, int lvl, arm_lpae_iopte *ptep,
|
|
|
|
gfp_t gfp, size_t *mapped)
|
2014-11-14 17:18:23 +00:00
|
|
|
{
|
|
|
|
arm_lpae_iopte *cptep, pte;
|
|
|
|
size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
size_t tblsz = ARM_LPAE_GRANULE(data);
|
2015-07-29 18:46:04 +00:00
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
2021-06-16 13:38:52 +00:00
|
|
|
int ret = 0, num_entries, max_entries, map_idx_start;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Find our entry at the current level */
|
2021-06-16 13:38:52 +00:00
|
|
|
map_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
|
|
|
|
ptep += map_idx_start;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* If we can install a leaf entry at this level, then do so */
|
2021-06-16 13:38:52 +00:00
|
|
|
if (size == block_size) {
|
|
|
|
max_entries = ARM_LPAE_PTES_PER_TABLE(data) - map_idx_start;
|
|
|
|
num_entries = min_t(int, pgcount, max_entries);
|
|
|
|
ret = arm_lpae_init_pte(data, iova, paddr, prot, lvl, num_entries, ptep);
|
2022-11-15 15:26:41 +00:00
|
|
|
if (!ret)
|
2021-06-16 13:38:52 +00:00
|
|
|
*mapped += num_entries * size;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* We can't allocate tables at the final level */
|
|
|
|
if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Grab a pointer to the next level */
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
pte = READ_ONCE(*ptep);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!pte) {
|
2023-11-24 14:24:34 +00:00
|
|
|
cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg, data->iop.cookie);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!cptep)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-11-20 03:13:43 +00:00
|
|
|
pte = arm_lpae_install_table(cptep, ptep, 0, data);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
if (pte)
|
2023-11-24 14:24:34 +00:00
|
|
|
__arm_lpae_free_pages(cptep, tblsz, cfg, data->iop.cookie);
|
2019-06-25 11:51:25 +00:00
|
|
|
} else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
|
2021-06-16 13:38:50 +00:00
|
|
|
__arm_lpae_sync_pte(ptep, 1, cfg);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
|
2014-11-14 17:18:23 +00:00
|
|
|
cptep = iopte_deref(pte, data);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
} else if (pte) {
|
2017-02-27 12:30:25 +00:00
|
|
|
/* We require an unmap first */
|
|
|
|
WARN_ON(!selftest_running);
|
|
|
|
return -EEXIST;
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Rinse, repeat */
|
2021-06-16 13:38:52 +00:00
|
|
|
return __arm_lpae_map(data, iova, paddr, size, pgcount, prot, lvl + 1,
|
|
|
|
cptep, gfp, mapped);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
|
|
|
|
int prot)
|
|
|
|
{
|
|
|
|
arm_lpae_iopte pte;
|
|
|
|
|
|
|
|
if (data->iop.fmt == ARM_64_LPAE_S1 ||
|
|
|
|
data->iop.fmt == ARM_32_LPAE_S1) {
|
2017-01-06 13:28:09 +00:00
|
|
|
pte = ARM_LPAE_PTE_nG;
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
|
|
|
|
pte |= ARM_LPAE_PTE_AP_RDONLY;
|
2024-07-03 10:16:04 +00:00
|
|
|
else if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_HD)
|
|
|
|
pte |= ARM_LPAE_PTE_DBM;
|
2017-01-06 13:28:09 +00:00
|
|
|
if (!(prot & IOMMU_PRIV))
|
|
|
|
pte |= ARM_LPAE_PTE_AP_UNPRIV;
|
2014-11-14 17:18:23 +00:00
|
|
|
} else {
|
|
|
|
pte = ARM_LPAE_PTE_HAP_FAULT;
|
|
|
|
if (prot & IOMMU_READ)
|
|
|
|
pte |= ARM_LPAE_PTE_HAP_READ;
|
|
|
|
if (prot & IOMMU_WRITE)
|
|
|
|
pte |= ARM_LPAE_PTE_HAP_WRITE;
|
2019-02-21 20:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that this logic is structured to accommodate Mali LPAE
|
|
|
|
* having stage-1-like attributes but stage-2-like permissions.
|
|
|
|
*/
|
|
|
|
if (data->iop.fmt == ARM_64_LPAE_S2 ||
|
|
|
|
data->iop.fmt == ARM_32_LPAE_S2) {
|
2016-04-05 11:39:31 +00:00
|
|
|
if (prot & IOMMU_MMIO)
|
|
|
|
pte |= ARM_LPAE_PTE_MEMATTR_DEV;
|
|
|
|
else if (prot & IOMMU_CACHE)
|
2014-11-14 17:18:23 +00:00
|
|
|
pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
|
|
|
|
else
|
|
|
|
pte |= ARM_LPAE_PTE_MEMATTR_NC;
|
2019-02-21 20:23:25 +00:00
|
|
|
} else {
|
|
|
|
if (prot & IOMMU_MMIO)
|
|
|
|
pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
|
|
|
|
<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
|
|
|
|
else if (prot & IOMMU_CACHE)
|
|
|
|
pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
|
|
|
|
<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 14:16:48 +00:00
|
|
|
/*
|
|
|
|
* Also Mali has its own notions of shareability wherein its Inner
|
|
|
|
* domain covers the cores within the GPU, and its Outer domain is
|
|
|
|
* "outside the GPU" (i.e. either the Inner or System domain in CPU
|
|
|
|
* terms, depending on coherency).
|
|
|
|
*/
|
|
|
|
if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE)
|
2020-01-10 15:21:51 +00:00
|
|
|
pte |= ARM_LPAE_PTE_SH_IS;
|
|
|
|
else
|
|
|
|
pte |= ARM_LPAE_PTE_SH_OS;
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
if (prot & IOMMU_NOEXEC)
|
|
|
|
pte |= ARM_LPAE_PTE_XN;
|
|
|
|
|
2020-01-10 15:21:51 +00:00
|
|
|
if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
|
|
|
|
pte |= ARM_LPAE_PTE_NS;
|
|
|
|
|
|
|
|
if (data->iop.fmt != ARM_MALI_LPAE)
|
|
|
|
pte |= ARM_LPAE_PTE_AF;
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
return pte;
|
|
|
|
}
|
|
|
|
|
2021-06-16 13:38:52 +00:00
|
|
|
static int arm_lpae_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
|
|
|
|
phys_addr_t paddr, size_t pgsize, size_t pgcount,
|
|
|
|
int iommu_prot, gfp_t gfp, size_t *mapped)
|
2014-11-14 17:18:23 +00:00
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
2019-10-25 18:08:31 +00:00
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
2014-11-14 17:18:23 +00:00
|
|
|
arm_lpae_iopte *ptep = data->pgd;
|
2019-10-25 18:08:33 +00:00
|
|
|
int ret, lvl = data->start_level;
|
2014-11-14 17:18:23 +00:00
|
|
|
arm_lpae_iopte prot;
|
2020-02-28 14:18:55 +00:00
|
|
|
long iaext = (s64)iova >> cfg->ias;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2021-06-16 13:38:52 +00:00
|
|
|
if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize))
|
2019-10-25 18:08:31 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-10-25 18:08:39 +00:00
|
|
|
if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
|
|
|
|
iaext = ~iaext;
|
|
|
|
if (WARN_ON(iaext || paddr >> cfg->oas))
|
2017-07-03 13:52:24 +00:00
|
|
|
return -ERANGE;
|
|
|
|
|
2020-12-07 11:57:58 +00:00
|
|
|
if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
|
2024-08-22 14:45:55 +00:00
|
|
|
return -EINVAL;
|
2020-12-07 11:57:58 +00:00
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
prot = arm_lpae_prot_to_pte(data, iommu_prot);
|
2021-06-16 13:38:52 +00:00
|
|
|
ret = __arm_lpae_map(data, iova, paddr, pgsize, pgcount, prot, lvl,
|
|
|
|
ptep, gfp, mapped);
|
2015-07-29 18:46:09 +00:00
|
|
|
/*
|
|
|
|
* Synchronise all PTE updates for the new mapping before there's
|
|
|
|
* a chance for anything to kick off a table walk for the new iova.
|
|
|
|
*/
|
|
|
|
wmb();
|
|
|
|
|
|
|
|
return ret;
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
|
|
|
|
arm_lpae_iopte *ptep)
|
|
|
|
{
|
|
|
|
arm_lpae_iopte *start, *end;
|
|
|
|
unsigned long table_size;
|
|
|
|
|
2019-10-25 18:08:33 +00:00
|
|
|
if (lvl == data->start_level)
|
2019-10-25 18:08:34 +00:00
|
|
|
table_size = ARM_LPAE_PGD_SIZE(data);
|
2014-11-14 17:18:23 +00:00
|
|
|
else
|
2015-12-07 18:18:53 +00:00
|
|
|
table_size = ARM_LPAE_GRANULE(data);
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
start = ptep;
|
2015-12-15 16:08:12 +00:00
|
|
|
|
|
|
|
/* Only leaf entries at the last level */
|
|
|
|
if (lvl == ARM_LPAE_MAX_LEVELS - 1)
|
|
|
|
end = ptep;
|
|
|
|
else
|
|
|
|
end = (void *)ptep + table_size;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
while (ptep != end) {
|
|
|
|
arm_lpae_iopte pte = *ptep++;
|
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
|
2014-11-14 17:18:23 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:24:34 +00:00
|
|
|
__arm_lpae_free_pages(start, table_size, &data->iop.cfg, data->iop.cookie);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arm_lpae_free_pgtable(struct io_pgtable *iop)
|
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
|
|
|
|
|
2019-10-25 18:08:33 +00:00
|
|
|
__arm_lpae_free_pgtable(data, data->start_level, data->pgd);
|
2014-11-14 17:18:23 +00:00
|
|
|
kfree(data);
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:59:19 +00:00
|
|
|
static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
|
2019-07-02 15:45:15 +00:00
|
|
|
struct iommu_iotlb_gather *gather,
|
2018-02-05 17:59:19 +00:00
|
|
|
unsigned long iova, size_t size,
|
|
|
|
arm_lpae_iopte blk_pte, int lvl,
|
2021-06-16 13:38:51 +00:00
|
|
|
arm_lpae_iopte *ptep, size_t pgcount)
|
2014-11-14 17:18:23 +00:00
|
|
|
{
|
2017-06-22 15:53:51 +00:00
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
arm_lpae_iopte pte, *tablep;
|
2014-11-14 17:18:23 +00:00
|
|
|
phys_addr_t blk_paddr;
|
2017-06-22 15:53:51 +00:00
|
|
|
size_t tablesz = ARM_LPAE_GRANULE(data);
|
|
|
|
size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
2021-06-16 13:38:51 +00:00
|
|
|
int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data);
|
|
|
|
int i, unmap_idx_start = -1, num_entries = 0, max_entries;
|
2017-06-22 15:53:51 +00:00
|
|
|
|
|
|
|
if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
|
|
|
|
return 0;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2023-11-24 14:24:34 +00:00
|
|
|
tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg, data->iop.cookie);
|
2017-06-22 15:53:51 +00:00
|
|
|
if (!tablep)
|
|
|
|
return 0; /* Bytes unmapped */
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
if (size == split_sz) {
|
|
|
|
unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
|
|
|
|
max_entries = ptes_per_table - unmap_idx_start;
|
|
|
|
num_entries = min_t(int, pgcount, max_entries);
|
|
|
|
}
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2018-03-26 12:35:13 +00:00
|
|
|
blk_paddr = iopte_to_paddr(blk_pte, data);
|
2017-06-22 15:53:51 +00:00
|
|
|
pte = iopte_prot(blk_pte);
|
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) {
|
2014-11-14 17:18:23 +00:00
|
|
|
/* Unmap! */
|
2021-06-16 13:38:51 +00:00
|
|
|
if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries))
|
2014-11-14 17:18:23 +00:00
|
|
|
continue;
|
|
|
|
|
2021-06-16 13:38:50 +00:00
|
|
|
__arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 03:13:43 +00:00
|
|
|
pte = arm_lpae_install_table(tablep, ptep, blk_pte, data);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
if (pte != blk_pte) {
|
2023-11-24 14:24:34 +00:00
|
|
|
__arm_lpae_free_pages(tablep, tablesz, cfg, data->iop.cookie);
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
/*
|
|
|
|
* We may race against someone unmapping another part of this
|
|
|
|
* block, but anything else is invalid. We can't misinterpret
|
|
|
|
* a page entry here since we're never at the last level.
|
|
|
|
*/
|
2020-12-07 12:01:50 +00:00
|
|
|
if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE)
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
tablep = iopte_deref(pte, data);
|
2021-06-16 13:38:51 +00:00
|
|
|
} else if (unmap_idx_start >= 0) {
|
|
|
|
for (i = 0; i < num_entries; i++)
|
|
|
|
io_pgtable_tlb_add_page(&data->iop, gather, iova + i * size, size);
|
|
|
|
|
|
|
|
return num_entries * size;
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
}
|
2017-06-22 15:53:51 +00:00
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 17:59:19 +00:00
|
|
|
static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
|
2019-07-02 15:45:15 +00:00
|
|
|
struct iommu_iotlb_gather *gather,
|
2021-06-16 13:38:51 +00:00
|
|
|
unsigned long iova, size_t size, size_t pgcount,
|
|
|
|
int lvl, arm_lpae_iopte *ptep)
|
2014-11-14 17:18:23 +00:00
|
|
|
{
|
|
|
|
arm_lpae_iopte pte;
|
2016-01-26 17:13:14 +00:00
|
|
|
struct io_pgtable *iop = &data->iop;
|
2021-06-16 13:38:51 +00:00
|
|
|
int i = 0, num_entries, max_entries, unmap_idx_start;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2015-12-04 17:52:58 +00:00
|
|
|
/* Something went horribly wrong and we ran out of page table */
|
|
|
|
if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
|
|
|
|
return 0;
|
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
|
|
|
|
ptep += unmap_idx_start;
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
pte = READ_ONCE(*ptep);
|
2015-12-04 17:52:58 +00:00
|
|
|
if (WARN_ON(!pte))
|
2014-11-14 17:18:23 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If the size matches this level, we're in the right place */
|
2017-06-22 15:53:51 +00:00
|
|
|
if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
|
2021-06-16 13:38:51 +00:00
|
|
|
max_entries = ARM_LPAE_PTES_PER_TABLE(data) - unmap_idx_start;
|
|
|
|
num_entries = min_t(int, pgcount, max_entries);
|
|
|
|
|
2024-08-06 10:51:35 +00:00
|
|
|
/* Find and handle non-leaf entries */
|
|
|
|
for (i = 0; i < num_entries; i++) {
|
|
|
|
pte = READ_ONCE(ptep[i]);
|
2021-06-16 13:38:51 +00:00
|
|
|
if (WARN_ON(!pte))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!iopte_leaf(pte, lvl, iop->fmt)) {
|
2024-08-06 10:51:35 +00:00
|
|
|
__arm_lpae_clear_pte(&ptep[i], &iop->cfg, 1);
|
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
/* Also flush any partial walks */
|
|
|
|
io_pgtable_tlb_flush_walk(iop, iova + i * size, size,
|
|
|
|
ARM_LPAE_GRANULE(data));
|
|
|
|
__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
|
|
|
|
}
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 10:51:35 +00:00
|
|
|
/* Clear the remaining entries */
|
|
|
|
__arm_lpae_clear_pte(ptep, &iop->cfg, i);
|
|
|
|
|
|
|
|
if (gather && !iommu_iotlb_gather_queued(gather))
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
io_pgtable_tlb_add_page(iop, gather, iova + j * size, size);
|
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
return i * size;
|
2019-02-21 20:23:25 +00:00
|
|
|
} else if (iopte_leaf(pte, lvl, iop->fmt)) {
|
2014-11-14 17:18:23 +00:00
|
|
|
/*
|
|
|
|
* Insert a table at the next level to map the old region,
|
|
|
|
* minus the part we want to unmap
|
|
|
|
*/
|
2019-07-02 15:45:15 +00:00
|
|
|
return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
|
2021-06-16 13:38:51 +00:00
|
|
|
lvl + 1, ptep, pgcount);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep on walkin' */
|
|
|
|
ptep = iopte_deref(pte, data);
|
2021-06-16 13:38:51 +00:00
|
|
|
return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl + 1, ptep);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
static size_t arm_lpae_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova,
|
|
|
|
size_t pgsize, size_t pgcount,
|
|
|
|
struct iommu_iotlb_gather *gather)
|
2014-11-14 17:18:23 +00:00
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
2019-10-25 18:08:31 +00:00
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
2014-11-14 17:18:23 +00:00
|
|
|
arm_lpae_iopte *ptep = data->pgd;
|
2020-02-28 14:18:55 +00:00
|
|
|
long iaext = (s64)iova >> cfg->ias;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount))
|
2019-10-25 18:08:31 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-10-25 18:08:39 +00:00
|
|
|
if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
|
|
|
|
iaext = ~iaext;
|
|
|
|
if (WARN_ON(iaext))
|
2017-07-03 13:52:24 +00:00
|
|
|
return 0;
|
|
|
|
|
2021-06-16 13:38:51 +00:00
|
|
|
return __arm_lpae_unmap(data, gather, iova, pgsize, pgcount,
|
|
|
|
data->start_level, ptep);
|
|
|
|
}
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
|
|
|
|
unsigned long iova)
|
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
|
|
arm_lpae_iopte pte, *ptep = data->pgd;
|
2019-10-25 18:08:33 +00:00
|
|
|
int lvl = data->start_level;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* Valid IOPTE pointer? */
|
|
|
|
if (!ptep)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Grab the IOPTE we're interested in */
|
iommu/io-pgtable-arm: Support lockless operation
For parallel I/O with multiple concurrent threads servicing the same
device (or devices, if several share a domain), serialising page table
updates becomes a massive bottleneck. On reflection, though, we don't
strictly need to do that - for valid IOMMU API usage, there are in fact
only two races that we need to guard against: multiple map requests for
different blocks within the same region, when the intermediate-level
table for that region does not yet exist; and multiple unmaps of
different parts of the same block entry. Both of those are fairly easily
solved by using a cmpxchg to install the new table, such that if we then
find that someone else's table got there first, we can simply free ours
and continue.
Make the requisite changes such that we can withstand being called
without the caller maintaining a lock. In theory, this opens up a few
corners in which wildly misbehaving callers making nonsensical
overlapping requests might lead to crashes instead of just unpredictable
results, but correct code really does not deserve to pay a significant
performance cost for the sake of masking bugs in theoretical broken code.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2017-06-22 15:53:54 +00:00
|
|
|
ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
|
|
|
|
pte = READ_ONCE(*ptep);
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Valid entry? */
|
|
|
|
if (!pte)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Leaf entry? */
|
2019-02-21 20:23:25 +00:00
|
|
|
if (iopte_leaf(pte, lvl, data->iop.fmt))
|
2014-11-14 17:18:23 +00:00
|
|
|
goto found_translation;
|
|
|
|
|
|
|
|
/* Take it to the next level */
|
|
|
|
ptep = iopte_deref(pte, data);
|
|
|
|
} while (++lvl < ARM_LPAE_MAX_LEVELS);
|
|
|
|
|
|
|
|
/* Ran out of page tables to walk */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
found_translation:
|
2016-06-16 17:21:19 +00:00
|
|
|
iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
|
2018-03-26 12:35:13 +00:00
|
|
|
return iopte_to_paddr(pte, data) | iova;
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 10:16:02 +00:00
|
|
|
struct io_pgtable_walk_data {
|
|
|
|
struct iommu_dirty_bitmap *dirty;
|
|
|
|
unsigned long flags;
|
|
|
|
u64 addr;
|
|
|
|
const u64 end;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __arm_lpae_iopte_walk_dirty(struct arm_lpae_io_pgtable *data,
|
|
|
|
struct io_pgtable_walk_data *walk_data,
|
|
|
|
arm_lpae_iopte *ptep,
|
|
|
|
int lvl);
|
|
|
|
|
|
|
|
static int io_pgtable_visit_dirty(struct arm_lpae_io_pgtable *data,
|
|
|
|
struct io_pgtable_walk_data *walk_data,
|
|
|
|
arm_lpae_iopte *ptep, int lvl)
|
|
|
|
{
|
|
|
|
struct io_pgtable *iop = &data->iop;
|
|
|
|
arm_lpae_iopte pte = READ_ONCE(*ptep);
|
|
|
|
|
|
|
|
if (iopte_leaf(pte, lvl, iop->fmt)) {
|
|
|
|
size_t size = ARM_LPAE_BLOCK_SIZE(lvl, data);
|
|
|
|
|
|
|
|
if (iopte_writeable_dirty(pte)) {
|
|
|
|
iommu_dirty_bitmap_record(walk_data->dirty,
|
|
|
|
walk_data->addr, size);
|
|
|
|
if (!(walk_data->flags & IOMMU_DIRTY_NO_CLEAR))
|
|
|
|
iopte_set_writeable_clean(ptep);
|
|
|
|
}
|
|
|
|
walk_data->addr += size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WARN_ON(!iopte_table(pte, lvl)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ptep = iopte_deref(pte, data);
|
|
|
|
return __arm_lpae_iopte_walk_dirty(data, walk_data, ptep, lvl + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __arm_lpae_iopte_walk_dirty(struct arm_lpae_io_pgtable *data,
|
|
|
|
struct io_pgtable_walk_data *walk_data,
|
|
|
|
arm_lpae_iopte *ptep,
|
|
|
|
int lvl)
|
|
|
|
{
|
|
|
|
u32 idx;
|
|
|
|
int max_entries, ret;
|
|
|
|
|
|
|
|
if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (lvl == data->start_level)
|
|
|
|
max_entries = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
|
|
|
|
else
|
|
|
|
max_entries = ARM_LPAE_PTES_PER_TABLE(data);
|
|
|
|
|
|
|
|
for (idx = ARM_LPAE_LVL_IDX(walk_data->addr, lvl, data);
|
|
|
|
(idx < max_entries) && (walk_data->addr < walk_data->end); ++idx) {
|
|
|
|
ret = io_pgtable_visit_dirty(data, walk_data, ptep + idx, lvl);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int arm_lpae_read_and_clear_dirty(struct io_pgtable_ops *ops,
|
|
|
|
unsigned long iova, size_t size,
|
|
|
|
unsigned long flags,
|
|
|
|
struct iommu_dirty_bitmap *dirty)
|
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
struct io_pgtable_walk_data walk_data = {
|
|
|
|
.dirty = dirty,
|
|
|
|
.flags = flags,
|
|
|
|
.addr = iova,
|
|
|
|
.end = iova + size,
|
|
|
|
};
|
|
|
|
arm_lpae_iopte *ptep = data->pgd;
|
|
|
|
int lvl = data->start_level;
|
|
|
|
|
|
|
|
if (WARN_ON(!size))
|
|
|
|
return -EINVAL;
|
|
|
|
if (WARN_ON((iova + size - 1) & ~(BIT(cfg->ias) - 1)))
|
|
|
|
return -EINVAL;
|
|
|
|
if (data->iop.fmt != ARM_64_LPAE_S1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return __arm_lpae_iopte_walk_dirty(data, &walk_data, ptep, lvl);
|
|
|
|
}
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
|
|
|
|
{
|
2018-03-26 12:35:13 +00:00
|
|
|
unsigned long granule, page_sizes;
|
|
|
|
unsigned int max_addr_bits = 48;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to restrict the supported page sizes to match the
|
|
|
|
* translation regime for a particular granule. Aim to match
|
|
|
|
* the CPU page size if possible, otherwise prefer smaller sizes.
|
|
|
|
* While we're at it, restrict the block sizes to match the
|
|
|
|
* chosen granule.
|
|
|
|
*/
|
|
|
|
if (cfg->pgsize_bitmap & PAGE_SIZE)
|
|
|
|
granule = PAGE_SIZE;
|
|
|
|
else if (cfg->pgsize_bitmap & ~PAGE_MASK)
|
|
|
|
granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
|
|
|
|
else if (cfg->pgsize_bitmap & PAGE_MASK)
|
|
|
|
granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
|
|
|
|
else
|
|
|
|
granule = 0;
|
|
|
|
|
|
|
|
switch (granule) {
|
|
|
|
case SZ_4K:
|
2018-03-26 12:35:13 +00:00
|
|
|
page_sizes = (SZ_4K | SZ_2M | SZ_1G);
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case SZ_16K:
|
2018-03-26 12:35:13 +00:00
|
|
|
page_sizes = (SZ_16K | SZ_32M);
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case SZ_64K:
|
2018-03-26 12:35:13 +00:00
|
|
|
max_addr_bits = 52;
|
|
|
|
page_sizes = (SZ_64K | SZ_512M);
|
|
|
|
if (cfg->oas > 48)
|
|
|
|
page_sizes |= 1ULL << 42; /* 4TB */
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-03-26 12:35:13 +00:00
|
|
|
page_sizes = 0;
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
2018-03-26 12:35:13 +00:00
|
|
|
|
|
|
|
cfg->pgsize_bitmap &= page_sizes;
|
|
|
|
cfg->ias = min(cfg->ias, max_addr_bits);
|
|
|
|
cfg->oas = min(cfg->oas, max_addr_bits);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct arm_lpae_io_pgtable *
|
|
|
|
arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
|
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data;
|
2019-10-25 18:08:35 +00:00
|
|
|
int levels, va_bits, pg_shift;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
arm_lpae_restrict_pgsizes(cfg);
|
|
|
|
|
|
|
|
if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
data = kmalloc(sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
2019-10-25 18:08:35 +00:00
|
|
|
pg_shift = __ffs(cfg->pgsize_bitmap);
|
|
|
|
data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-10-25 18:08:35 +00:00
|
|
|
va_bits = cfg->ias - pg_shift;
|
2019-10-25 18:08:33 +00:00
|
|
|
levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
|
|
|
|
data->start_level = ARM_LPAE_MAX_LEVELS - levels;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Calculate the actual size of our pgd (without concatenation) */
|
2019-10-25 18:08:34 +00:00
|
|
|
data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
data->iop.ops = (struct io_pgtable_ops) {
|
2021-06-16 13:38:52 +00:00
|
|
|
.map_pages = arm_lpae_map_pages,
|
2021-06-16 13:38:51 +00:00
|
|
|
.unmap_pages = arm_lpae_unmap_pages,
|
2014-11-14 17:18:23 +00:00
|
|
|
.iova_to_phys = arm_lpae_iova_to_phys,
|
2024-07-03 10:16:02 +00:00
|
|
|
.read_and_clear_dirty = arm_lpae_read_and_clear_dirty,
|
2014-11-14 17:18:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_pgtable *
|
|
|
|
arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
|
|
|
|
{
|
|
|
|
u64 reg;
|
2016-02-12 17:09:46 +00:00
|
|
|
struct arm_lpae_io_pgtable *data;
|
2019-10-25 18:08:38 +00:00
|
|
|
typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr;
|
2019-10-25 18:08:39 +00:00
|
|
|
bool tg1;
|
2016-02-12 17:09:46 +00:00
|
|
|
|
2019-06-25 11:51:25 +00:00
|
|
|
if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
|
2020-11-25 07:00:11 +00:00
|
|
|
IO_PGTABLE_QUIRK_ARM_TTBR1 |
|
2024-07-03 10:16:04 +00:00
|
|
|
IO_PGTABLE_QUIRK_ARM_OUTER_WBWA |
|
|
|
|
IO_PGTABLE_QUIRK_ARM_HD))
|
2016-02-12 17:09:46 +00:00
|
|
|
return NULL;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2016-02-12 17:09:46 +00:00
|
|
|
data = arm_lpae_alloc_pgtable(cfg);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* TCR */
|
2019-05-15 23:32:34 +00:00
|
|
|
if (cfg->coherent_walk) {
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->sh = ARM_LPAE_TCR_SH_IS;
|
|
|
|
tcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
|
|
|
|
tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
|
2020-11-25 07:00:11 +00:00
|
|
|
if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)
|
|
|
|
goto out_free_data;
|
2019-05-15 23:32:34 +00:00
|
|
|
} else {
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->sh = ARM_LPAE_TCR_SH_OS;
|
|
|
|
tcr->irgn = ARM_LPAE_TCR_RGN_NC;
|
2020-11-25 07:00:11 +00:00
|
|
|
if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
|
|
|
|
tcr->orgn = ARM_LPAE_TCR_RGN_NC;
|
|
|
|
else
|
|
|
|
tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
|
2019-05-15 23:32:34 +00:00
|
|
|
}
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-10-25 18:08:39 +00:00
|
|
|
tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1;
|
2015-12-07 18:18:53 +00:00
|
|
|
switch (ARM_LPAE_GRANULE(data)) {
|
2014-11-14 17:18:23 +00:00
|
|
|
case SZ_4K:
|
2019-10-25 18:08:39 +00:00
|
|
|
tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case SZ_16K:
|
2019-10-25 18:08:39 +00:00
|
|
|
tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case SZ_64K:
|
2019-10-25 18:08:39 +00:00
|
|
|
tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cfg->oas) {
|
|
|
|
case 32:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_32_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 36:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_36_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 40:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_40_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 42:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_42_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 44:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_44_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 48:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_48_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
2018-03-26 12:35:13 +00:00
|
|
|
case 52:
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->ips = ARM_LPAE_TCR_PS_52_BIT;
|
2018-03-26 12:35:13 +00:00
|
|
|
break;
|
2014-11-14 17:18:23 +00:00
|
|
|
default:
|
|
|
|
goto out_free_data;
|
|
|
|
}
|
|
|
|
|
2019-10-25 18:08:38 +00:00
|
|
|
tcr->tsz = 64ULL - cfg->ias;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* MAIRs */
|
|
|
|
reg = (ARM_LPAE_MAIR_ATTR_NC
|
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
|
|
|
|
(ARM_LPAE_MAIR_ATTR_WBRWA
|
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
|
|
|
|
(ARM_LPAE_MAIR_ATTR_DEVICE
|
iommu/io-pgtable-arm: Add support to use system cache
Few Qualcomm platforms such as, sdm845 have an additional outer
cache called as System cache, aka. Last level cache (LLC) that
allows non-coherent devices to upgrade to using caching.
This cache sits right before the DDR, and is tightly coupled
with the memory controller. The clients using this cache request
their slices from this system cache, make it active, and can then
start using it.
There is a fundamental assumption that non-coherent devices can't
access caches. This change adds an exception where they *can* use
some level of cache despite still being non-coherent overall.
The coherent devices that use cacheable memory, and CPU make use of
this system cache by default.
Looking at memory types, we have following -
a) Normal uncached :- MAIR 0x44, inner non-cacheable,
outer non-cacheable;
b) Normal cached :- MAIR 0xff, inner read write-back non-transient,
outer read write-back non-transient;
attribute setting for coherenet I/O devices.
and, for non-coherent i/o devices that can allocate in system cache
another type gets added -
c) Normal sys-cached :- MAIR 0xf4, inner non-cacheable,
outer read write-back non-transient
Coherent I/O devices use system cache by marking the memory as
normal cached.
Non-coherent I/O devices should mark the memory as normal
sys-cached in page tables to use system cache.
Acked-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Vivek Gautam <vivek.gautam@codeaurora.org>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2019-05-16 09:30:20 +00:00
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
|
|
|
|
(ARM_LPAE_MAIR_ATTR_INC_OWBRWA
|
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-10-25 18:08:36 +00:00
|
|
|
cfg->arm_lpae_s1_cfg.mair = reg;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Looking good; allocate a pgd */
|
2019-10-25 18:08:34 +00:00
|
|
|
data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
|
2023-11-24 14:24:34 +00:00
|
|
|
GFP_KERNEL, cfg, cookie);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!data->pgd)
|
|
|
|
goto out_free_data;
|
|
|
|
|
2015-07-29 18:46:09 +00:00
|
|
|
/* Ensure the empty pgd is visible before any actual TTBR write */
|
|
|
|
wmb();
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-10-25 18:08:37 +00:00
|
|
|
/* TTBR */
|
|
|
|
cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
|
2014-11-14 17:18:23 +00:00
|
|
|
return &data->iop;
|
|
|
|
|
|
|
|
out_free_data:
|
|
|
|
kfree(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_pgtable *
|
|
|
|
arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
|
|
|
|
{
|
2020-01-10 14:51:59 +00:00
|
|
|
u64 sl;
|
2016-02-12 17:09:46 +00:00
|
|
|
struct arm_lpae_io_pgtable *data;
|
2020-01-10 14:51:59 +00:00
|
|
|
typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;
|
2016-02-12 17:09:46 +00:00
|
|
|
|
|
|
|
/* The NS quirk doesn't apply at stage 2 */
|
2021-08-11 12:21:29 +00:00
|
|
|
if (cfg->quirks)
|
2016-02-12 17:09:46 +00:00
|
|
|
return NULL;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2016-02-12 17:09:46 +00:00
|
|
|
data = arm_lpae_alloc_pgtable(cfg);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Concatenate PGDs at level 1 if possible in order to reduce
|
|
|
|
* the depth of the stage-2 walk.
|
|
|
|
*/
|
2019-10-25 18:08:33 +00:00
|
|
|
if (data->start_level == 0) {
|
2014-11-14 17:18:23 +00:00
|
|
|
unsigned long pgd_pages;
|
|
|
|
|
2019-10-25 18:08:34 +00:00
|
|
|
pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
|
2019-10-25 18:08:34 +00:00
|
|
|
data->pgd_bits += data->bits_per_level;
|
2019-10-25 18:08:33 +00:00
|
|
|
data->start_level++;
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VTCR */
|
2020-01-10 11:40:33 +00:00
|
|
|
if (cfg->coherent_walk) {
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->sh = ARM_LPAE_TCR_SH_IS;
|
|
|
|
vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
|
|
|
|
vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
|
2020-01-10 11:40:33 +00:00
|
|
|
} else {
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->sh = ARM_LPAE_TCR_SH_OS;
|
|
|
|
vtcr->irgn = ARM_LPAE_TCR_RGN_NC;
|
|
|
|
vtcr->orgn = ARM_LPAE_TCR_RGN_NC;
|
2020-01-10 11:40:33 +00:00
|
|
|
}
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2019-10-25 18:08:33 +00:00
|
|
|
sl = data->start_level;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
2015-12-07 18:18:53 +00:00
|
|
|
switch (ARM_LPAE_GRANULE(data)) {
|
2014-11-14 17:18:23 +00:00
|
|
|
case SZ_4K:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->tg = ARM_LPAE_TCR_TG0_4K;
|
2014-11-14 17:18:23 +00:00
|
|
|
sl++; /* SL0 format is different for 4K granule size */
|
|
|
|
break;
|
|
|
|
case SZ_16K:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->tg = ARM_LPAE_TCR_TG0_16K;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case SZ_64K:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->tg = ARM_LPAE_TCR_TG0_64K;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cfg->oas) {
|
|
|
|
case 32:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_32_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 36:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_36_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 40:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_40_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 42:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_42_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 44:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_44_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
|
|
|
case 48:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_48_BIT;
|
2014-11-14 17:18:23 +00:00
|
|
|
break;
|
2018-03-26 12:35:13 +00:00
|
|
|
case 52:
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->ps = ARM_LPAE_TCR_PS_52_BIT;
|
2018-03-26 12:35:13 +00:00
|
|
|
break;
|
2014-11-14 17:18:23 +00:00
|
|
|
default:
|
|
|
|
goto out_free_data;
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:51:59 +00:00
|
|
|
vtcr->tsz = 64ULL - cfg->ias;
|
|
|
|
vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK;
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* Allocate pgd pages */
|
2019-10-25 18:08:34 +00:00
|
|
|
data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
|
2023-11-24 14:24:34 +00:00
|
|
|
GFP_KERNEL, cfg, cookie);
|
2014-11-14 17:18:23 +00:00
|
|
|
if (!data->pgd)
|
|
|
|
goto out_free_data;
|
|
|
|
|
2015-07-29 18:46:09 +00:00
|
|
|
/* Ensure the empty pgd is visible before any actual TTBR write */
|
|
|
|
wmb();
|
2014-11-14 17:18:23 +00:00
|
|
|
|
|
|
|
/* VTTBR */
|
|
|
|
cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
|
|
|
|
return &data->iop;
|
|
|
|
|
|
|
|
out_free_data:
|
|
|
|
kfree(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_pgtable *
|
|
|
|
arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
|
|
|
|
{
|
|
|
|
if (cfg->ias > 32 || cfg->oas > 40)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
|
2019-10-25 18:08:38 +00:00
|
|
|
return arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_pgtable *
|
|
|
|
arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
|
|
|
|
{
|
|
|
|
if (cfg->ias > 40 || cfg->oas > 40)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
|
2020-01-10 14:51:59 +00:00
|
|
|
return arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
|
2014-11-14 17:18:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
static struct io_pgtable *
|
|
|
|
arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
|
|
|
|
{
|
2019-09-30 14:11:00 +00:00
|
|
|
struct arm_lpae_io_pgtable *data;
|
2019-02-21 20:23:25 +00:00
|
|
|
|
2019-09-30 14:11:00 +00:00
|
|
|
/* No quirks for Mali (hopefully) */
|
|
|
|
if (cfg->quirks)
|
|
|
|
return NULL;
|
2019-02-21 20:23:25 +00:00
|
|
|
|
2019-09-30 14:11:01 +00:00
|
|
|
if (cfg->ias > 48 || cfg->oas > 40)
|
2019-02-21 20:23:25 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
|
|
|
|
|
2019-09-30 14:11:00 +00:00
|
|
|
data = arm_lpae_alloc_pgtable(cfg);
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
2019-02-21 20:23:25 +00:00
|
|
|
|
2019-09-30 14:11:01 +00:00
|
|
|
/* Mali seems to need a full 4-level table regardless of IAS */
|
2019-10-25 18:08:33 +00:00
|
|
|
if (data->start_level > 0) {
|
|
|
|
data->start_level = 0;
|
2019-10-25 18:08:34 +00:00
|
|
|
data->pgd_bits = 0;
|
2019-02-21 20:23:25 +00:00
|
|
|
}
|
2019-09-30 14:11:00 +00:00
|
|
|
/*
|
|
|
|
* MEMATTR: Mali has no actual notion of a non-cacheable type, so the
|
|
|
|
* best we can do is mimic the out-of-tree driver and hope that the
|
|
|
|
* "implementation-defined caching policy" is good enough. Similarly,
|
|
|
|
* we'll use it for the sake of a valid attribute for our 'device'
|
|
|
|
* index, although callers should never request that in practice.
|
|
|
|
*/
|
|
|
|
cfg->arm_mali_lpae_cfg.memattr =
|
|
|
|
(ARM_MALI_LPAE_MEMATTR_IMP_DEF
|
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
|
|
|
|
(ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
|
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
|
|
|
|
(ARM_MALI_LPAE_MEMATTR_IMP_DEF
|
|
|
|
<< ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
|
2019-02-21 20:23:25 +00:00
|
|
|
|
2019-10-25 18:08:34 +00:00
|
|
|
data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
|
2023-11-24 14:24:34 +00:00
|
|
|
cfg, cookie);
|
2019-09-30 14:11:00 +00:00
|
|
|
if (!data->pgd)
|
|
|
|
goto out_free_data;
|
|
|
|
|
|
|
|
/* Ensure the empty pgd is visible before TRANSTAB can be written */
|
|
|
|
wmb();
|
|
|
|
|
|
|
|
cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
|
|
|
|
ARM_MALI_LPAE_TTBR_READ_INNER |
|
|
|
|
ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
|
2020-09-22 14:16:48 +00:00
|
|
|
if (cfg->coherent_walk)
|
|
|
|
cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER;
|
|
|
|
|
2019-09-30 14:11:00 +00:00
|
|
|
return &data->iop;
|
|
|
|
|
|
|
|
out_free_data:
|
|
|
|
kfree(data);
|
|
|
|
return NULL;
|
2019-02-21 20:23:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:18:23 +00:00
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
|
2023-11-24 14:24:34 +00:00
|
|
|
.caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR,
|
2014-11-14 17:18:23 +00:00
|
|
|
.alloc = arm_64_lpae_alloc_pgtable_s1,
|
|
|
|
.free = arm_lpae_free_pgtable,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
|
2023-11-24 14:24:34 +00:00
|
|
|
.caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR,
|
2014-11-14 17:18:23 +00:00
|
|
|
.alloc = arm_64_lpae_alloc_pgtable_s2,
|
|
|
|
.free = arm_lpae_free_pgtable,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
|
2023-11-24 14:24:34 +00:00
|
|
|
.caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR,
|
2014-11-14 17:18:23 +00:00
|
|
|
.alloc = arm_32_lpae_alloc_pgtable_s1,
|
|
|
|
.free = arm_lpae_free_pgtable,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
|
2023-11-24 14:24:34 +00:00
|
|
|
.caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR,
|
2014-11-14 17:18:23 +00:00
|
|
|
.alloc = arm_32_lpae_alloc_pgtable_s2,
|
|
|
|
.free = arm_lpae_free_pgtable,
|
|
|
|
};
|
2014-11-17 23:31:12 +00:00
|
|
|
|
2019-02-21 20:23:25 +00:00
|
|
|
struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
|
2023-11-24 14:24:34 +00:00
|
|
|
.caps = IO_PGTABLE_CAP_CUSTOM_ALLOCATOR,
|
2019-02-21 20:23:25 +00:00
|
|
|
.alloc = arm_mali_lpae_alloc_pgtable,
|
|
|
|
.free = arm_lpae_free_pgtable,
|
|
|
|
};
|
|
|
|
|
2014-11-17 23:31:12 +00:00
|
|
|
#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
|
|
|
|
|
2019-10-25 18:08:30 +00:00
|
|
|
static struct io_pgtable_cfg *cfg_cookie __initdata;
|
2014-11-17 23:31:12 +00:00
|
|
|
|
2019-10-25 18:08:30 +00:00
|
|
|
static void __init dummy_tlb_flush_all(void *cookie)
|
2014-11-17 23:31:12 +00:00
|
|
|
{
|
|
|
|
WARN_ON(cookie != cfg_cookie);
|
|
|
|
}
|
|
|
|
|
2019-10-25 18:08:30 +00:00
|
|
|
static void __init dummy_tlb_flush(unsigned long iova, size_t size,
|
|
|
|
size_t granule, void *cookie)
|
2014-11-17 23:31:12 +00:00
|
|
|
{
|
|
|
|
WARN_ON(cookie != cfg_cookie);
|
|
|
|
WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
|
|
|
|
}
|
|
|
|
|
2019-10-25 18:08:30 +00:00
|
|
|
static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
|
|
|
|
unsigned long iova, size_t granule,
|
|
|
|
void *cookie)
|
2019-07-02 15:44:32 +00:00
|
|
|
{
|
2019-07-02 15:44:41 +00:00
|
|
|
dummy_tlb_flush(iova, granule, granule, cookie);
|
2019-07-02 15:44:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 15:43:34 +00:00
|
|
|
static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
|
2014-11-17 23:31:12 +00:00
|
|
|
.tlb_flush_all = dummy_tlb_flush_all,
|
2019-07-02 15:44:32 +00:00
|
|
|
.tlb_flush_walk = dummy_tlb_flush,
|
2019-07-02 15:44:41 +00:00
|
|
|
.tlb_add_page = dummy_tlb_add_page,
|
2014-11-17 23:31:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
|
|
|
|
{
|
|
|
|
struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
|
|
|
|
struct io_pgtable_cfg *cfg = &data->iop.cfg;
|
|
|
|
|
|
|
|
pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
|
|
|
|
cfg->pgsize_bitmap, cfg->ias);
|
2019-10-25 18:08:35 +00:00
|
|
|
pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
|
2019-10-25 18:08:34 +00:00
|
|
|
ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
|
2019-10-25 18:08:35 +00:00
|
|
|
ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
|
2014-11-17 23:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define __FAIL(ops, i) ({ \
|
|
|
|
WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
|
|
|
|
arm_lpae_dump_ops(ops); \
|
|
|
|
selftest_running = false; \
|
|
|
|
-EFAULT; \
|
|
|
|
})
|
|
|
|
|
|
|
|
static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
|
|
|
|
{
|
2019-09-09 20:19:19 +00:00
|
|
|
static const enum io_pgtable_fmt fmts[] __initconst = {
|
2014-11-17 23:31:12 +00:00
|
|
|
ARM_64_LPAE_S1,
|
|
|
|
ARM_64_LPAE_S2,
|
|
|
|
};
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
unsigned long iova;
|
2022-11-15 15:26:41 +00:00
|
|
|
size_t size, mapped;
|
2014-11-17 23:31:12 +00:00
|
|
|
struct io_pgtable_ops *ops;
|
|
|
|
|
|
|
|
selftest_running = true;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
|
|
|
|
cfg_cookie = cfg;
|
|
|
|
ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
|
|
|
|
if (!ops) {
|
|
|
|
pr_err("selftest: failed to allocate io pgtable ops\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initial sanity checks.
|
|
|
|
* Empty page tables shouldn't provide any translations.
|
|
|
|
*/
|
|
|
|
if (ops->iova_to_phys(ops, 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
if (ops->iova_to_phys(ops, SZ_1G + 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
if (ops->iova_to_phys(ops, SZ_2G + 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Distinct mappings of different granule sizes.
|
|
|
|
*/
|
|
|
|
iova = 0;
|
2016-09-21 05:41:31 +00:00
|
|
|
for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
|
2014-11-17 23:31:12 +00:00
|
|
|
size = 1UL << j;
|
|
|
|
|
2022-11-15 15:26:41 +00:00
|
|
|
if (ops->map_pages(ops, iova, iova, size, 1,
|
|
|
|
IOMMU_READ | IOMMU_WRITE |
|
|
|
|
IOMMU_NOEXEC | IOMMU_CACHE,
|
|
|
|
GFP_KERNEL, &mapped))
|
2014-11-17 23:31:12 +00:00
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
/* Overlapping mappings */
|
2022-11-15 15:26:41 +00:00
|
|
|
if (!ops->map_pages(ops, iova, iova + size, size, 1,
|
|
|
|
IOMMU_READ | IOMMU_NOEXEC,
|
|
|
|
GFP_KERNEL, &mapped))
|
2014-11-17 23:31:12 +00:00
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
iova += SZ_1G;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Partial unmap */
|
|
|
|
size = 1UL << __ffs(cfg->pgsize_bitmap);
|
2022-11-15 15:26:41 +00:00
|
|
|
if (ops->unmap_pages(ops, SZ_1G + size, size, 1, NULL) != size)
|
2014-11-17 23:31:12 +00:00
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
/* Remap of partial unmap */
|
2022-11-15 15:26:41 +00:00
|
|
|
if (ops->map_pages(ops, SZ_1G + size, size, size, 1,
|
|
|
|
IOMMU_READ, GFP_KERNEL, &mapped))
|
2014-11-17 23:31:12 +00:00
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
/* Full unmap */
|
|
|
|
iova = 0;
|
2018-04-26 04:49:29 +00:00
|
|
|
for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
|
2014-11-17 23:31:12 +00:00
|
|
|
size = 1UL << j;
|
|
|
|
|
2022-11-15 15:26:41 +00:00
|
|
|
if (ops->unmap_pages(ops, iova, size, 1, NULL) != size)
|
2014-11-17 23:31:12 +00:00
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
if (ops->iova_to_phys(ops, iova + 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
/* Remap full block */
|
2022-11-15 15:26:41 +00:00
|
|
|
if (ops->map_pages(ops, iova, iova, size, 1,
|
|
|
|
IOMMU_WRITE, GFP_KERNEL, &mapped))
|
2014-11-17 23:31:12 +00:00
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
|
|
|
|
return __FAIL(ops, i);
|
|
|
|
|
|
|
|
iova += SZ_1G;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_io_pgtable_ops(ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
selftest_running = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init arm_lpae_do_selftests(void)
|
|
|
|
{
|
2019-09-09 20:19:19 +00:00
|
|
|
static const unsigned long pgsize[] __initconst = {
|
2014-11-17 23:31:12 +00:00
|
|
|
SZ_4K | SZ_2M | SZ_1G,
|
|
|
|
SZ_16K | SZ_32M,
|
|
|
|
SZ_64K | SZ_512M,
|
|
|
|
};
|
|
|
|
|
2019-09-09 20:19:19 +00:00
|
|
|
static const unsigned int ias[] __initconst = {
|
2014-11-17 23:31:12 +00:00
|
|
|
32, 36, 40, 42, 44, 48,
|
|
|
|
};
|
|
|
|
|
|
|
|
int i, j, pass = 0, fail = 0;
|
2022-08-15 16:15:55 +00:00
|
|
|
struct device dev;
|
2014-11-17 23:31:12 +00:00
|
|
|
struct io_pgtable_cfg cfg = {
|
|
|
|
.tlb = &dummy_tlb_ops,
|
|
|
|
.oas = 48,
|
2019-06-25 11:51:25 +00:00
|
|
|
.coherent_walk = true,
|
2022-08-15 16:15:55 +00:00
|
|
|
.iommu_dev = &dev,
|
2014-11-17 23:31:12 +00:00
|
|
|
};
|
|
|
|
|
2022-08-15 16:15:55 +00:00
|
|
|
/* __arm_lpae_alloc_pages() merely needs dev_to_node() to work */
|
|
|
|
set_dev_node(&dev, NUMA_NO_NODE);
|
|
|
|
|
2014-11-17 23:31:12 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
|
|
|
|
for (j = 0; j < ARRAY_SIZE(ias); ++j) {
|
|
|
|
cfg.pgsize_bitmap = pgsize[i];
|
|
|
|
cfg.ias = ias[j];
|
|
|
|
pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
|
|
|
|
pgsize[i], ias[j]);
|
|
|
|
if (arm_lpae_run_tests(&cfg))
|
|
|
|
fail++;
|
|
|
|
else
|
|
|
|
pass++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
|
|
|
|
return fail ? -EFAULT : 0;
|
|
|
|
}
|
|
|
|
subsys_initcall(arm_lpae_do_selftests);
|
|
|
|
#endif
|