blob: bcfbd0e44a4a07fb40b27aeb422b09a08adcfd63 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Will Deacone1d3c0f2014-11-14 17:18:23 +00002/*
3 * CPU-agnostic ARM page table allocator.
4 *
Will Deacone1d3c0f2014-11-14 17:18:23 +00005 * Copyright (C) 2014 ARM Limited
6 *
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
10#define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
11
Robin Murphy2c3d2732017-06-22 16:53:54 +010012#include <linux/atomic.h>
Robin Murphy6c899282018-03-26 13:35:13 +010013#include <linux/bitops.h>
Rob Herringb77cf112019-02-05 10:37:31 -060014#include <linux/io-pgtable.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000015#include <linux/kernel.h>
16#include <linux/sizes.h>
17#include <linux/slab.h>
18#include <linux/types.h>
Lada Trimasova8f6aff92016-01-27 11:10:32 +000019#include <linux/dma-mapping.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000020
Robin Murphy87a91b12015-07-29 19:46:09 +010021#include <asm/barrier.h>
22
Jean-Philippe Brucker7cef39d2020-09-18 12:18:45 +020023#include "io-pgtable-arm.h"
24
Robin Murphy6c899282018-03-26 13:35:13 +010025#define ARM_LPAE_MAX_ADDR_BITS 52
Will Deacone1d3c0f2014-11-14 17:18:23 +000026#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
27#define ARM_LPAE_MAX_LEVELS 4
28
29/* Struct accessors */
30#define io_pgtable_to_data(x) \
31 container_of((x), struct arm_lpae_io_pgtable, iop)
32
Will Deacone1d3c0f2014-11-14 17:18:23 +000033#define io_pgtable_ops_to_data(x) \
34 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
35
36/*
Will Deacone1d3c0f2014-11-14 17:18:23 +000037 * Calculate the right shift amount to get to the portion describing level l
38 * in a virtual address mapped by the pagetable in d.
39 */
40#define ARM_LPAE_LVL_SHIFT(l,d) \
Robin Murphy5fb190b2019-10-25 19:08:35 +010041 (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
42 ilog2(sizeof(arm_lpae_iopte)))
Will Deacone1d3c0f2014-11-14 17:18:23 +000043
Robin Murphy5fb190b2019-10-25 19:08:35 +010044#define ARM_LPAE_GRANULE(d) \
45 (sizeof(arm_lpae_iopte) << (d)->bits_per_level)
Robin Murphyc79278c2019-10-25 19:08:34 +010046#define ARM_LPAE_PGD_SIZE(d) \
47 (sizeof(arm_lpae_iopte) << (d)->pgd_bits)
Will Deacone1d3c0f2014-11-14 17:18:23 +000048
49/*
50 * Calculate the index at level l used to map virtual address a using the
51 * pagetable in d.
52 */
53#define ARM_LPAE_PGD_IDX(l,d) \
Robin Murphyc79278c2019-10-25 19:08:34 +010054 ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
Will Deacone1d3c0f2014-11-14 17:18:23 +000055
56#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000057 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000058 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
59
60/* Calculate the block/page mapping size at level l for pagetable in d. */
Robin Murphy5fb190b2019-10-25 19:08:35 +010061#define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000062
63/* Page table bits */
64#define ARM_LPAE_PTE_TYPE_SHIFT 0
65#define ARM_LPAE_PTE_TYPE_MASK 0x3
66
67#define ARM_LPAE_PTE_TYPE_BLOCK 1
68#define ARM_LPAE_PTE_TYPE_TABLE 3
69#define ARM_LPAE_PTE_TYPE_PAGE 3
70
Robin Murphy6c899282018-03-26 13:35:13 +010071#define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
72
Laurent Pinchartc896c1322014-12-14 23:34:50 +020073#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000074#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
75#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
76#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
77#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
78#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c1322014-12-14 23:34:50 +020079#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000080#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
81
82#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
83/* Ignore the contiguous bit for block splitting */
84#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
85#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
86 ARM_LPAE_PTE_ATTR_HI_MASK)
Robin Murphy2c3d2732017-06-22 16:53:54 +010087/* Software bit for solving coherency races */
88#define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
Will Deacone1d3c0f2014-11-14 17:18:23 +000089
90/* Stage-1 PTE */
91#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
92#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
93#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
94#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
95
96/* Stage-2 PTE */
97#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
98#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
99#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
100#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
101#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
102#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
103
104/* Register bits */
Robin Murphyfb485eb2019-10-25 19:08:38 +0100105#define ARM_LPAE_VTCR_SL0_MASK 0x3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000106
107#define ARM_LPAE_TCR_T0SZ_SHIFT 0
Will Deacone1d3c0f2014-11-14 17:18:23 +0000108
Robin Murphyfb485eb2019-10-25 19:08:38 +0100109#define ARM_LPAE_VTCR_PS_SHIFT 16
110#define ARM_LPAE_VTCR_PS_MASK 0x7
Will Deacone1d3c0f2014-11-14 17:18:23 +0000111
Will Deacone1d3c0f2014-11-14 17:18:23 +0000112#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
113#define ARM_LPAE_MAIR_ATTR_MASK 0xff
114#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
115#define ARM_LPAE_MAIR_ATTR_NC 0x44
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530116#define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
Will Deacone1d3c0f2014-11-14 17:18:23 +0000117#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
118#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
119#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
120#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530121#define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
Will Deacone1d3c0f2014-11-14 17:18:23 +0000122
Rob Herringd08d42d2019-02-21 14:23:25 -0600123#define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
124#define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
125#define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
126
Robin Murphy52f325f2019-09-30 15:11:00 +0100127#define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
128#define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
129
Will Deacone1d3c0f2014-11-14 17:18:23 +0000130/* IOPTE accessors */
Robin Murphy6c899282018-03-26 13:35:13 +0100131#define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000132
133#define iopte_type(pte,l) \
134 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
135
136#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
137
Will Deacone1d3c0f2014-11-14 17:18:23 +0000138struct arm_lpae_io_pgtable {
139 struct io_pgtable iop;
140
Robin Murphyc79278c2019-10-25 19:08:34 +0100141 int pgd_bits;
Robin Murphy594ab902019-10-25 19:08:33 +0100142 int start_level;
Robin Murphy5fb190b2019-10-25 19:08:35 +0100143 int bits_per_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000144
145 void *pgd;
146};
147
148typedef u64 arm_lpae_iopte;
149
Rob Herringd08d42d2019-02-21 14:23:25 -0600150static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
151 enum io_pgtable_fmt fmt)
152{
153 if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
154 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE;
155
156 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK;
157}
158
Robin Murphy6c899282018-03-26 13:35:13 +0100159static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
160 struct arm_lpae_io_pgtable *data)
161{
162 arm_lpae_iopte pte = paddr;
163
164 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
165 return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
166}
167
168static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
169 struct arm_lpae_io_pgtable *data)
170{
Robin Murphy78688052018-03-29 12:24:52 +0100171 u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
Robin Murphy6c899282018-03-26 13:35:13 +0100172
Robin Murphy5fb190b2019-10-25 19:08:35 +0100173 if (ARM_LPAE_GRANULE(data) < SZ_64K)
Robin Murphy6c899282018-03-26 13:35:13 +0100174 return paddr;
175
176 /* Rotate the packed high-order bits back to the top */
177 return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
178}
179
Will Deaconfe4b9912014-11-17 23:31:12 +0000180static bool selftest_running = false;
181
Robin Murphyffcb6d12015-09-17 17:42:16 +0100182static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100183{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100184 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100185}
186
187static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
188 struct io_pgtable_cfg *cfg)
189{
190 struct device *dev = cfg->iommu_dev;
Robin Murphy4b123752018-05-22 12:50:09 +0100191 int order = get_order(size);
192 struct page *p;
Robin Murphyf8d54962015-07-29 19:46:04 +0100193 dma_addr_t dma;
Robin Murphy4b123752018-05-22 12:50:09 +0100194 void *pages;
Robin Murphyf8d54962015-07-29 19:46:04 +0100195
Robin Murphy4b123752018-05-22 12:50:09 +0100196 VM_BUG_ON((gfp & __GFP_HIGHMEM));
Jean-Philippe Bruckerfac83d22018-06-18 12:27:54 +0100197 p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
198 gfp | __GFP_ZERO, order);
Robin Murphy4b123752018-05-22 12:50:09 +0100199 if (!p)
Robin Murphyf8d54962015-07-29 19:46:04 +0100200 return NULL;
201
Robin Murphy4b123752018-05-22 12:50:09 +0100202 pages = page_address(p);
Will Deacon4f418452019-06-25 12:51:25 +0100203 if (!cfg->coherent_walk) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100204 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
205 if (dma_mapping_error(dev, dma))
206 goto out_free;
207 /*
208 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100209 * address directly, so if the DMA layer suggests otherwise by
210 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100211 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100212 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100213 goto out_unmap;
214 }
215
216 return pages;
217
218out_unmap:
219 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
220 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
221out_free:
Robin Murphy4b123752018-05-22 12:50:09 +0100222 __free_pages(p, order);
Robin Murphyf8d54962015-07-29 19:46:04 +0100223 return NULL;
224}
225
226static void __arm_lpae_free_pages(void *pages, size_t size,
227 struct io_pgtable_cfg *cfg)
228{
Will Deacon4f418452019-06-25 12:51:25 +0100229 if (!cfg->coherent_walk)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100230 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100231 size, DMA_TO_DEVICE);
Robin Murphy4b123752018-05-22 12:50:09 +0100232 free_pages((unsigned long)pages, get_order(size));
Robin Murphyf8d54962015-07-29 19:46:04 +0100233}
234
Robin Murphy2c3d2732017-06-22 16:53:54 +0100235static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
236 struct io_pgtable_cfg *cfg)
237{
238 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
239 sizeof(*ptep), DMA_TO_DEVICE);
240}
241
Robin Murphyf8d54962015-07-29 19:46:04 +0100242static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100243 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100244{
Robin Murphyf8d54962015-07-29 19:46:04 +0100245 *ptep = pte;
246
Will Deacon4f418452019-06-25 12:51:25 +0100247 if (!cfg->coherent_walk)
Robin Murphy2c3d2732017-06-22 16:53:54 +0100248 __arm_lpae_sync_pte(ptep, cfg);
Robin Murphyf8d54962015-07-29 19:46:04 +0100249}
250
Vivek Gautam193e67c2018-02-05 23:29:19 +0530251static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100252 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530253 unsigned long iova, size_t size, int lvl,
254 arm_lpae_iopte *ptep);
Will Deaconcf27ec92015-08-11 16:48:32 +0100255
Robin Murphyfb3a9572017-06-22 16:53:51 +0100256static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
257 phys_addr_t paddr, arm_lpae_iopte prot,
258 int lvl, arm_lpae_iopte *ptep)
259{
260 arm_lpae_iopte pte = prot;
261
Rob Herringd08d42d2019-02-21 14:23:25 -0600262 if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
Robin Murphyfb3a9572017-06-22 16:53:51 +0100263 pte |= ARM_LPAE_PTE_TYPE_PAGE;
264 else
265 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
266
Robin Murphy6c899282018-03-26 13:35:13 +0100267 pte |= paddr_to_iopte(paddr, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100268
269 __arm_lpae_set_pte(ptep, pte, &data->iop.cfg);
270}
271
Will Deacone1d3c0f2014-11-14 17:18:23 +0000272static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
273 unsigned long iova, phys_addr_t paddr,
274 arm_lpae_iopte prot, int lvl,
275 arm_lpae_iopte *ptep)
276{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100277 arm_lpae_iopte pte = *ptep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000278
Rob Herringd08d42d2019-02-21 14:23:25 -0600279 if (iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100280 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000281 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000282 return -EEXIST;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100283 } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100284 /*
285 * We need to unmap and free the old table before
286 * overwriting it with a block entry.
287 */
288 arm_lpae_iopte *tblp;
289 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
290
291 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
Will Deacon3951c412019-07-02 16:45:15 +0100292 if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) {
293 WARN_ON(1);
Will Deaconcf27ec92015-08-11 16:48:32 +0100294 return -EINVAL;
Will Deacon3951c412019-07-02 16:45:15 +0100295 }
Will Deaconfe4b9912014-11-17 23:31:12 +0000296 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000297
Robin Murphyfb3a9572017-06-22 16:53:51 +0100298 __arm_lpae_init_pte(data, paddr, prot, lvl, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000299 return 0;
300}
301
Robin Murphyfb3a9572017-06-22 16:53:51 +0100302static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
303 arm_lpae_iopte *ptep,
Robin Murphy2c3d2732017-06-22 16:53:54 +0100304 arm_lpae_iopte curr,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100305 struct io_pgtable_cfg *cfg)
306{
Robin Murphy2c3d2732017-06-22 16:53:54 +0100307 arm_lpae_iopte old, new;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100308
309 new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
310 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
311 new |= ARM_LPAE_PTE_NSTABLE;
312
Will Deacon77f34452017-06-23 12:02:38 +0100313 /*
314 * Ensure the table itself is visible before its PTE can be.
315 * Whilst we could get away with cmpxchg64_release below, this
316 * doesn't have any ordering semantics when !CONFIG_SMP.
317 */
318 dma_wmb();
Robin Murphy2c3d2732017-06-22 16:53:54 +0100319
320 old = cmpxchg64_relaxed(ptep, curr, new);
321
Will Deacon4f418452019-06-25 12:51:25 +0100322 if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
Robin Murphy2c3d2732017-06-22 16:53:54 +0100323 return old;
324
325 /* Even if it's not ours, there's no point waiting; just kick it */
326 __arm_lpae_sync_pte(ptep, cfg);
327 if (old == curr)
328 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
329
330 return old;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100331}
332
Will Deacone1d3c0f2014-11-14 17:18:23 +0000333static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
334 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800335 int lvl, arm_lpae_iopte *ptep, gfp_t gfp)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000336{
337 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000338 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100339 size_t tblsz = ARM_LPAE_GRANULE(data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100340 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000341
342 /* Find our entry at the current level */
343 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
344
345 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf7b90d22019-10-25 19:08:31 +0100346 if (size == block_size)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000347 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
348
349 /* We can't allocate tables at the final level */
350 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
351 return -EINVAL;
352
353 /* Grab a pointer to the next level */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100354 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000355 if (!pte) {
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800356 cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000357 if (!cptep)
358 return -ENOMEM;
359
Robin Murphy2c3d2732017-06-22 16:53:54 +0100360 pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
361 if (pte)
362 __arm_lpae_free_pages(cptep, tblsz, cfg);
Will Deacon4f418452019-06-25 12:51:25 +0100363 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
Robin Murphy2c3d2732017-06-22 16:53:54 +0100364 __arm_lpae_sync_pte(ptep, cfg);
365 }
366
Rob Herringd08d42d2019-02-21 14:23:25 -0600367 if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000368 cptep = iopte_deref(pte, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100369 } else if (pte) {
Oleksandr Tyshchenkoed46e662017-02-27 14:30:25 +0200370 /* We require an unmap first */
371 WARN_ON(!selftest_running);
372 return -EEXIST;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000373 }
374
375 /* Rinse, repeat */
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800376 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep, gfp);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000377}
378
379static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
380 int prot)
381{
382 arm_lpae_iopte pte;
383
384 if (data->iop.fmt == ARM_64_LPAE_S1 ||
385 data->iop.fmt == ARM_32_LPAE_S1) {
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530386 pte = ARM_LPAE_PTE_nG;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000387 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
388 pte |= ARM_LPAE_PTE_AP_RDONLY;
Jeremy Gebbene7468a22017-01-06 18:58:09 +0530389 if (!(prot & IOMMU_PRIV))
390 pte |= ARM_LPAE_PTE_AP_UNPRIV;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000391 } else {
392 pte = ARM_LPAE_PTE_HAP_FAULT;
393 if (prot & IOMMU_READ)
394 pte |= ARM_LPAE_PTE_HAP_READ;
395 if (prot & IOMMU_WRITE)
396 pte |= ARM_LPAE_PTE_HAP_WRITE;
Rob Herringd08d42d2019-02-21 14:23:25 -0600397 }
398
399 /*
400 * Note that this logic is structured to accommodate Mali LPAE
401 * having stage-1-like attributes but stage-2-like permissions.
402 */
403 if (data->iop.fmt == ARM_64_LPAE_S2 ||
404 data->iop.fmt == ARM_32_LPAE_S2) {
Robin Murphyfb948252016-04-05 12:39:31 +0100405 if (prot & IOMMU_MMIO)
406 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
407 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000408 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
409 else
410 pte |= ARM_LPAE_PTE_MEMATTR_NC;
Rob Herringd08d42d2019-02-21 14:23:25 -0600411 } else {
412 if (prot & IOMMU_MMIO)
413 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
414 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
415 else if (prot & IOMMU_CACHE)
416 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
417 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000418 }
419
Robin Murphyb5848622020-09-22 15:16:48 +0100420 /*
421 * Also Mali has its own notions of shareability wherein its Inner
422 * domain covers the cores within the GPU, and its Outer domain is
423 * "outside the GPU" (i.e. either the Inner or System domain in CPU
424 * terms, depending on coherency).
425 */
426 if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE)
Robin Murphy7618e472020-01-10 15:21:51 +0000427 pte |= ARM_LPAE_PTE_SH_IS;
428 else
429 pte |= ARM_LPAE_PTE_SH_OS;
430
Will Deacone1d3c0f2014-11-14 17:18:23 +0000431 if (prot & IOMMU_NOEXEC)
432 pte |= ARM_LPAE_PTE_XN;
433
Robin Murphy7618e472020-01-10 15:21:51 +0000434 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
435 pte |= ARM_LPAE_PTE_NS;
436
437 if (data->iop.fmt != ARM_MALI_LPAE)
438 pte |= ARM_LPAE_PTE_AF;
439
Will Deacone1d3c0f2014-11-14 17:18:23 +0000440 return pte;
441}
442
443static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800444 phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000445{
446 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphyf7b90d22019-10-25 19:08:31 +0100447 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000448 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy594ab902019-10-25 19:08:33 +0100449 int ret, lvl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000450 arm_lpae_iopte prot;
Robin Murphy08090742020-02-28 14:18:55 +0000451 long iaext = (s64)iova >> cfg->ias;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000452
453 /* If no access, then nothing to do */
454 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
455 return 0;
456
Robin Murphyf7b90d22019-10-25 19:08:31 +0100457 if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
458 return -EINVAL;
459
Robin Murphydb690302019-10-25 19:08:39 +0100460 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
461 iaext = ~iaext;
462 if (WARN_ON(iaext || paddr >> cfg->oas))
Robin Murphy76557392017-07-03 14:52:24 +0100463 return -ERANGE;
464
Will Deacone1d3c0f2014-11-14 17:18:23 +0000465 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Baolin Wangf34ce7a2020-06-12 11:39:55 +0800466 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, gfp);
Robin Murphy87a91b12015-07-29 19:46:09 +0100467 /*
468 * Synchronise all PTE updates for the new mapping before there's
469 * a chance for anything to kick off a table walk for the new iova.
470 */
471 wmb();
472
473 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000474}
475
476static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
477 arm_lpae_iopte *ptep)
478{
479 arm_lpae_iopte *start, *end;
480 unsigned long table_size;
481
Robin Murphy594ab902019-10-25 19:08:33 +0100482 if (lvl == data->start_level)
Robin Murphyc79278c2019-10-25 19:08:34 +0100483 table_size = ARM_LPAE_PGD_SIZE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000484 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000485 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000486
487 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000488
489 /* Only leaf entries at the last level */
490 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
491 end = ptep;
492 else
493 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000494
495 while (ptep != end) {
496 arm_lpae_iopte pte = *ptep++;
497
Rob Herringd08d42d2019-02-21 14:23:25 -0600498 if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000499 continue;
500
501 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
502 }
503
Robin Murphyf8d54962015-07-29 19:46:04 +0100504 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000505}
506
507static void arm_lpae_free_pgtable(struct io_pgtable *iop)
508{
509 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
510
Robin Murphy594ab902019-10-25 19:08:33 +0100511 __arm_lpae_free_pgtable(data, data->start_level, data->pgd);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000512 kfree(data);
513}
514
Vivek Gautam193e67c2018-02-05 23:29:19 +0530515static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100516 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530517 unsigned long iova, size_t size,
518 arm_lpae_iopte blk_pte, int lvl,
519 arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000520{
Robin Murphyfb3a9572017-06-22 16:53:51 +0100521 struct io_pgtable_cfg *cfg = &data->iop.cfg;
522 arm_lpae_iopte pte, *tablep;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000523 phys_addr_t blk_paddr;
Robin Murphyfb3a9572017-06-22 16:53:51 +0100524 size_t tablesz = ARM_LPAE_GRANULE(data);
525 size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
526 int i, unmap_idx = -1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000527
Robin Murphyfb3a9572017-06-22 16:53:51 +0100528 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
529 return 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000530
Robin Murphyfb3a9572017-06-22 16:53:51 +0100531 tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
532 if (!tablep)
533 return 0; /* Bytes unmapped */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000534
Robin Murphyfb3a9572017-06-22 16:53:51 +0100535 if (size == split_sz)
536 unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data);
537
Robin Murphy6c899282018-03-26 13:35:13 +0100538 blk_paddr = iopte_to_paddr(blk_pte, data);
Robin Murphyfb3a9572017-06-22 16:53:51 +0100539 pte = iopte_prot(blk_pte);
540
541 for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000542 /* Unmap! */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100543 if (i == unmap_idx)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000544 continue;
545
Robin Murphyfb3a9572017-06-22 16:53:51 +0100546 __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000547 }
548
Robin Murphy2c3d2732017-06-22 16:53:54 +0100549 pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg);
550 if (pte != blk_pte) {
551 __arm_lpae_free_pages(tablep, tablesz, cfg);
552 /*
553 * We may race against someone unmapping another part of this
554 * block, but anything else is invalid. We can't misinterpret
555 * a page entry here since we're never at the last level.
556 */
557 if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE)
558 return 0;
559
560 tablep = iopte_deref(pte, data);
Robin Murphy85c7a0f2018-09-06 17:59:50 +0100561 } else if (unmap_idx >= 0) {
Will Deacon3951c412019-07-02 16:45:15 +0100562 io_pgtable_tlb_add_page(&data->iop, gather, iova, size);
Robin Murphy85c7a0f2018-09-06 17:59:50 +0100563 return size;
Robin Murphy2c3d2732017-06-22 16:53:54 +0100564 }
Robin Murphyfb3a9572017-06-22 16:53:51 +0100565
Will Deacon3951c412019-07-02 16:45:15 +0100566 return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000567}
568
Vivek Gautam193e67c2018-02-05 23:29:19 +0530569static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
Will Deacon3951c412019-07-02 16:45:15 +0100570 struct iommu_iotlb_gather *gather,
Vivek Gautam193e67c2018-02-05 23:29:19 +0530571 unsigned long iova, size_t size, int lvl,
572 arm_lpae_iopte *ptep)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000573{
574 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000575 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000576
Robin Murphy2eb97c72015-12-04 17:52:58 +0000577 /* Something went horribly wrong and we ran out of page table */
578 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
579 return 0;
580
Will Deacone1d3c0f2014-11-14 17:18:23 +0000581 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
Robin Murphy2c3d2732017-06-22 16:53:54 +0100582 pte = READ_ONCE(*ptep);
Robin Murphy2eb97c72015-12-04 17:52:58 +0000583 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000584 return 0;
585
586 /* If the size matches this level, we're in the right place */
Robin Murphyfb3a9572017-06-22 16:53:51 +0100587 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000588 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000589
Rob Herringd08d42d2019-02-21 14:23:25 -0600590 if (!iopte_leaf(pte, lvl, iop->fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000591 /* Also flush any partial walks */
Will Deacon10b7a7d2019-07-02 16:44:32 +0100592 io_pgtable_tlb_flush_walk(iop, iova, size,
593 ARM_LPAE_GRANULE(data));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000594 ptep = iopte_deref(pte, data);
595 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
Zhen Leib6b65ca2018-09-20 17:10:24 +0100596 } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
597 /*
598 * Order the PTE update against queueing the IOVA, to
599 * guarantee that a flush callback from a different CPU
600 * has observed it before the TLBIALL can be issued.
601 */
602 smp_wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000603 } else {
Will Deacon3951c412019-07-02 16:45:15 +0100604 io_pgtable_tlb_add_page(iop, gather, iova, size);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000605 }
606
607 return size;
Rob Herringd08d42d2019-02-21 14:23:25 -0600608 } else if (iopte_leaf(pte, lvl, iop->fmt)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000609 /*
610 * Insert a table at the next level to map the old region,
611 * minus the part we want to unmap
612 */
Will Deacon3951c412019-07-02 16:45:15 +0100613 return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
Robin Murphyfb3a9572017-06-22 16:53:51 +0100614 lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000615 }
616
617 /* Keep on walkin' */
618 ptep = iopte_deref(pte, data);
Will Deacon3951c412019-07-02 16:45:15 +0100619 return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000620}
621
Vivek Gautam193e67c2018-02-05 23:29:19 +0530622static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
Will Deacona2d3a382019-07-02 16:44:58 +0100623 size_t size, struct iommu_iotlb_gather *gather)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000624{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000625 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Robin Murphyf7b90d22019-10-25 19:08:31 +0100626 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000627 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy08090742020-02-28 14:18:55 +0000628 long iaext = (s64)iova >> cfg->ias;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000629
Robin Murphyf7b90d22019-10-25 19:08:31 +0100630 if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
631 return 0;
632
Robin Murphydb690302019-10-25 19:08:39 +0100633 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
634 iaext = ~iaext;
635 if (WARN_ON(iaext))
Robin Murphy76557392017-07-03 14:52:24 +0100636 return 0;
637
Robin Murphy594ab902019-10-25 19:08:33 +0100638 return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000639}
640
641static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
642 unsigned long iova)
643{
644 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
645 arm_lpae_iopte pte, *ptep = data->pgd;
Robin Murphy594ab902019-10-25 19:08:33 +0100646 int lvl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000647
648 do {
649 /* Valid IOPTE pointer? */
650 if (!ptep)
651 return 0;
652
653 /* Grab the IOPTE we're interested in */
Robin Murphy2c3d2732017-06-22 16:53:54 +0100654 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
655 pte = READ_ONCE(*ptep);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000656
657 /* Valid entry? */
658 if (!pte)
659 return 0;
660
661 /* Leaf entry? */
Rob Herringd08d42d2019-02-21 14:23:25 -0600662 if (iopte_leaf(pte, lvl, data->iop.fmt))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000663 goto found_translation;
664
665 /* Take it to the next level */
666 ptep = iopte_deref(pte, data);
667 } while (++lvl < ARM_LPAE_MAX_LEVELS);
668
669 /* Ran out of page tables to walk */
670 return 0;
671
672found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100673 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Robin Murphy6c899282018-03-26 13:35:13 +0100674 return iopte_to_paddr(pte, data) | iova;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000675}
676
677static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
678{
Robin Murphy6c899282018-03-26 13:35:13 +0100679 unsigned long granule, page_sizes;
680 unsigned int max_addr_bits = 48;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000681
682 /*
683 * We need to restrict the supported page sizes to match the
684 * translation regime for a particular granule. Aim to match
685 * the CPU page size if possible, otherwise prefer smaller sizes.
686 * While we're at it, restrict the block sizes to match the
687 * chosen granule.
688 */
689 if (cfg->pgsize_bitmap & PAGE_SIZE)
690 granule = PAGE_SIZE;
691 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
692 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
693 else if (cfg->pgsize_bitmap & PAGE_MASK)
694 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
695 else
696 granule = 0;
697
698 switch (granule) {
699 case SZ_4K:
Robin Murphy6c899282018-03-26 13:35:13 +0100700 page_sizes = (SZ_4K | SZ_2M | SZ_1G);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000701 break;
702 case SZ_16K:
Robin Murphy6c899282018-03-26 13:35:13 +0100703 page_sizes = (SZ_16K | SZ_32M);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000704 break;
705 case SZ_64K:
Robin Murphy6c899282018-03-26 13:35:13 +0100706 max_addr_bits = 52;
707 page_sizes = (SZ_64K | SZ_512M);
708 if (cfg->oas > 48)
709 page_sizes |= 1ULL << 42; /* 4TB */
Will Deacone1d3c0f2014-11-14 17:18:23 +0000710 break;
711 default:
Robin Murphy6c899282018-03-26 13:35:13 +0100712 page_sizes = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000713 }
Robin Murphy6c899282018-03-26 13:35:13 +0100714
715 cfg->pgsize_bitmap &= page_sizes;
716 cfg->ias = min(cfg->ias, max_addr_bits);
717 cfg->oas = min(cfg->oas, max_addr_bits);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000718}
719
720static struct arm_lpae_io_pgtable *
721arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
722{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000723 struct arm_lpae_io_pgtable *data;
Robin Murphy5fb190b2019-10-25 19:08:35 +0100724 int levels, va_bits, pg_shift;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000725
726 arm_lpae_restrict_pgsizes(cfg);
727
728 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
729 return NULL;
730
731 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
732 return NULL;
733
734 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
735 return NULL;
736
737 data = kmalloc(sizeof(*data), GFP_KERNEL);
738 if (!data)
739 return NULL;
740
Robin Murphy5fb190b2019-10-25 19:08:35 +0100741 pg_shift = __ffs(cfg->pgsize_bitmap);
742 data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000743
Robin Murphy5fb190b2019-10-25 19:08:35 +0100744 va_bits = cfg->ias - pg_shift;
Robin Murphy594ab902019-10-25 19:08:33 +0100745 levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
746 data->start_level = ARM_LPAE_MAX_LEVELS - levels;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000747
748 /* Calculate the actual size of our pgd (without concatenation) */
Robin Murphyc79278c2019-10-25 19:08:34 +0100749 data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000750
751 data->iop.ops = (struct io_pgtable_ops) {
752 .map = arm_lpae_map,
753 .unmap = arm_lpae_unmap,
754 .iova_to_phys = arm_lpae_iova_to_phys,
755 };
756
757 return data;
758}
759
760static struct io_pgtable *
761arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
762{
763 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000764 struct arm_lpae_io_pgtable *data;
Robin Murphyfb485eb2019-10-25 19:08:38 +0100765 typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr;
Robin Murphydb690302019-10-25 19:08:39 +0100766 bool tg1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000767
Will Deacon4f418452019-06-25 12:51:25 +0100768 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
Robin Murphydb690302019-10-25 19:08:39 +0100769 IO_PGTABLE_QUIRK_NON_STRICT |
770 IO_PGTABLE_QUIRK_ARM_TTBR1))
Robin Murphy3850db42016-02-12 17:09:46 +0000771 return NULL;
772
773 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000774 if (!data)
775 return NULL;
776
777 /* TCR */
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700778 if (cfg->coherent_walk) {
Robin Murphyfb485eb2019-10-25 19:08:38 +0100779 tcr->sh = ARM_LPAE_TCR_SH_IS;
780 tcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
781 tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700782 } else {
Robin Murphyfb485eb2019-10-25 19:08:38 +0100783 tcr->sh = ARM_LPAE_TCR_SH_OS;
784 tcr->irgn = ARM_LPAE_TCR_RGN_NC;
785 tcr->orgn = ARM_LPAE_TCR_RGN_NC;
Bjorn Andersson9e6ea592019-05-15 16:32:34 -0700786 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000787
Robin Murphydb690302019-10-25 19:08:39 +0100788 tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1;
Robin Murphy06c610e2015-12-07 18:18:53 +0000789 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000790 case SZ_4K:
Robin Murphydb690302019-10-25 19:08:39 +0100791 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000792 break;
793 case SZ_16K:
Robin Murphydb690302019-10-25 19:08:39 +0100794 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000795 break;
796 case SZ_64K:
Robin Murphydb690302019-10-25 19:08:39 +0100797 tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000798 break;
799 }
800
801 switch (cfg->oas) {
802 case 32:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100803 tcr->ips = ARM_LPAE_TCR_PS_32_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000804 break;
805 case 36:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100806 tcr->ips = ARM_LPAE_TCR_PS_36_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000807 break;
808 case 40:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100809 tcr->ips = ARM_LPAE_TCR_PS_40_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000810 break;
811 case 42:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100812 tcr->ips = ARM_LPAE_TCR_PS_42_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000813 break;
814 case 44:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100815 tcr->ips = ARM_LPAE_TCR_PS_44_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000816 break;
817 case 48:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100818 tcr->ips = ARM_LPAE_TCR_PS_48_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000819 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100820 case 52:
Robin Murphyfb485eb2019-10-25 19:08:38 +0100821 tcr->ips = ARM_LPAE_TCR_PS_52_BIT;
Robin Murphy6c899282018-03-26 13:35:13 +0100822 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000823 default:
824 goto out_free_data;
825 }
826
Robin Murphyfb485eb2019-10-25 19:08:38 +0100827 tcr->tsz = 64ULL - cfg->ias;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000828
829 /* MAIRs */
830 reg = (ARM_LPAE_MAIR_ATTR_NC
831 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
832 (ARM_LPAE_MAIR_ATTR_WBRWA
833 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
834 (ARM_LPAE_MAIR_ATTR_DEVICE
Vivek Gautam90ec7a72019-05-16 15:00:20 +0530835 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
836 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
837 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
Will Deacone1d3c0f2014-11-14 17:18:23 +0000838
Robin Murphy205577a2019-10-25 19:08:36 +0100839 cfg->arm_lpae_s1_cfg.mair = reg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000840
841 /* Looking good; allocate a pgd */
Robin Murphyc79278c2019-10-25 19:08:34 +0100842 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
843 GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000844 if (!data->pgd)
845 goto out_free_data;
846
Robin Murphy87a91b12015-07-29 19:46:09 +0100847 /* Ensure the empty pgd is visible before any actual TTBR write */
848 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000849
Robin Murphyd1e5f262019-10-25 19:08:37 +0100850 /* TTBR */
851 cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000852 return &data->iop;
853
854out_free_data:
855 kfree(data);
856 return NULL;
857}
858
859static struct io_pgtable *
860arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
861{
Will Deaconac4b80e2020-01-10 14:51:59 +0000862 u64 sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000863 struct arm_lpae_io_pgtable *data;
Will Deaconac4b80e2020-01-10 14:51:59 +0000864 typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000865
Robin Murphy3850db42016-02-12 17:09:46 +0000866 /* The NS quirk doesn't apply at stage 2 */
Will Deacon4f418452019-06-25 12:51:25 +0100867 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT))
Robin Murphy3850db42016-02-12 17:09:46 +0000868 return NULL;
869
870 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000871 if (!data)
872 return NULL;
873
874 /*
875 * Concatenate PGDs at level 1 if possible in order to reduce
876 * the depth of the stage-2 walk.
877 */
Robin Murphy594ab902019-10-25 19:08:33 +0100878 if (data->start_level == 0) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000879 unsigned long pgd_pages;
880
Robin Murphyc79278c2019-10-25 19:08:34 +0100881 pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000882 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
Robin Murphyc79278c2019-10-25 19:08:34 +0100883 data->pgd_bits += data->bits_per_level;
Robin Murphy594ab902019-10-25 19:08:33 +0100884 data->start_level++;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000885 }
886 }
887
888 /* VTCR */
Will Deacon30d2acb2020-01-10 11:40:33 +0000889 if (cfg->coherent_walk) {
Will Deaconac4b80e2020-01-10 14:51:59 +0000890 vtcr->sh = ARM_LPAE_TCR_SH_IS;
891 vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
892 vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
Will Deacon30d2acb2020-01-10 11:40:33 +0000893 } else {
Will Deaconac4b80e2020-01-10 14:51:59 +0000894 vtcr->sh = ARM_LPAE_TCR_SH_OS;
895 vtcr->irgn = ARM_LPAE_TCR_RGN_NC;
896 vtcr->orgn = ARM_LPAE_TCR_RGN_NC;
Will Deacon30d2acb2020-01-10 11:40:33 +0000897 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000898
Robin Murphy594ab902019-10-25 19:08:33 +0100899 sl = data->start_level;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000900
Robin Murphy06c610e2015-12-07 18:18:53 +0000901 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000902 case SZ_4K:
Will Deaconac4b80e2020-01-10 14:51:59 +0000903 vtcr->tg = ARM_LPAE_TCR_TG0_4K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000904 sl++; /* SL0 format is different for 4K granule size */
905 break;
906 case SZ_16K:
Will Deaconac4b80e2020-01-10 14:51:59 +0000907 vtcr->tg = ARM_LPAE_TCR_TG0_16K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000908 break;
909 case SZ_64K:
Will Deaconac4b80e2020-01-10 14:51:59 +0000910 vtcr->tg = ARM_LPAE_TCR_TG0_64K;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000911 break;
912 }
913
914 switch (cfg->oas) {
915 case 32:
Will Deaconac4b80e2020-01-10 14:51:59 +0000916 vtcr->ps = ARM_LPAE_TCR_PS_32_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000917 break;
918 case 36:
Will Deaconac4b80e2020-01-10 14:51:59 +0000919 vtcr->ps = ARM_LPAE_TCR_PS_36_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000920 break;
921 case 40:
Will Deaconac4b80e2020-01-10 14:51:59 +0000922 vtcr->ps = ARM_LPAE_TCR_PS_40_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000923 break;
924 case 42:
Will Deaconac4b80e2020-01-10 14:51:59 +0000925 vtcr->ps = ARM_LPAE_TCR_PS_42_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000926 break;
927 case 44:
Will Deaconac4b80e2020-01-10 14:51:59 +0000928 vtcr->ps = ARM_LPAE_TCR_PS_44_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000929 break;
930 case 48:
Will Deaconac4b80e2020-01-10 14:51:59 +0000931 vtcr->ps = ARM_LPAE_TCR_PS_48_BIT;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000932 break;
Robin Murphy6c899282018-03-26 13:35:13 +0100933 case 52:
Will Deaconac4b80e2020-01-10 14:51:59 +0000934 vtcr->ps = ARM_LPAE_TCR_PS_52_BIT;
Robin Murphy6c899282018-03-26 13:35:13 +0100935 break;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000936 default:
937 goto out_free_data;
938 }
939
Will Deaconac4b80e2020-01-10 14:51:59 +0000940 vtcr->tsz = 64ULL - cfg->ias;
941 vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000942
943 /* Allocate pgd pages */
Robin Murphyc79278c2019-10-25 19:08:34 +0100944 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
945 GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000946 if (!data->pgd)
947 goto out_free_data;
948
Robin Murphy87a91b12015-07-29 19:46:09 +0100949 /* Ensure the empty pgd is visible before any actual TTBR write */
950 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000951
952 /* VTTBR */
953 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
954 return &data->iop;
955
956out_free_data:
957 kfree(data);
958 return NULL;
959}
960
961static struct io_pgtable *
962arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
963{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000964 if (cfg->ias > 32 || cfg->oas > 40)
965 return NULL;
966
967 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Robin Murphyfb485eb2019-10-25 19:08:38 +0100968 return arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000969}
970
971static struct io_pgtable *
972arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
973{
Will Deacone1d3c0f2014-11-14 17:18:23 +0000974 if (cfg->ias > 40 || cfg->oas > 40)
975 return NULL;
976
977 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Will Deaconac4b80e2020-01-10 14:51:59 +0000978 return arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000979}
980
Rob Herringd08d42d2019-02-21 14:23:25 -0600981static struct io_pgtable *
982arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
983{
Robin Murphy52f325f2019-09-30 15:11:00 +0100984 struct arm_lpae_io_pgtable *data;
Rob Herringd08d42d2019-02-21 14:23:25 -0600985
Robin Murphy52f325f2019-09-30 15:11:00 +0100986 /* No quirks for Mali (hopefully) */
987 if (cfg->quirks)
988 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -0600989
Robin Murphy1be08f42019-09-30 15:11:01 +0100990 if (cfg->ias > 48 || cfg->oas > 40)
Rob Herringd08d42d2019-02-21 14:23:25 -0600991 return NULL;
992
993 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
Rob Herringd08d42d2019-02-21 14:23:25 -0600994
Robin Murphy52f325f2019-09-30 15:11:00 +0100995 data = arm_lpae_alloc_pgtable(cfg);
996 if (!data)
997 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -0600998
Robin Murphy1be08f42019-09-30 15:11:01 +0100999 /* Mali seems to need a full 4-level table regardless of IAS */
Robin Murphy594ab902019-10-25 19:08:33 +01001000 if (data->start_level > 0) {
1001 data->start_level = 0;
Robin Murphyc79278c2019-10-25 19:08:34 +01001002 data->pgd_bits = 0;
Rob Herringd08d42d2019-02-21 14:23:25 -06001003 }
Robin Murphy52f325f2019-09-30 15:11:00 +01001004 /*
1005 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
1006 * best we can do is mimic the out-of-tree driver and hope that the
1007 * "implementation-defined caching policy" is good enough. Similarly,
1008 * we'll use it for the sake of a valid attribute for our 'device'
1009 * index, although callers should never request that in practice.
1010 */
1011 cfg->arm_mali_lpae_cfg.memattr =
1012 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1013 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
1014 (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
1015 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
1016 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1017 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
Rob Herringd08d42d2019-02-21 14:23:25 -06001018
Robin Murphyc79278c2019-10-25 19:08:34 +01001019 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1020 cfg);
Robin Murphy52f325f2019-09-30 15:11:00 +01001021 if (!data->pgd)
1022 goto out_free_data;
1023
1024 /* Ensure the empty pgd is visible before TRANSTAB can be written */
1025 wmb();
1026
1027 cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
1028 ARM_MALI_LPAE_TTBR_READ_INNER |
1029 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
Robin Murphyb5848622020-09-22 15:16:48 +01001030 if (cfg->coherent_walk)
1031 cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER;
1032
Robin Murphy52f325f2019-09-30 15:11:00 +01001033 return &data->iop;
1034
1035out_free_data:
1036 kfree(data);
1037 return NULL;
Rob Herringd08d42d2019-02-21 14:23:25 -06001038}
1039
Will Deacone1d3c0f2014-11-14 17:18:23 +00001040struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1041 .alloc = arm_64_lpae_alloc_pgtable_s1,
1042 .free = arm_lpae_free_pgtable,
1043};
1044
1045struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1046 .alloc = arm_64_lpae_alloc_pgtable_s2,
1047 .free = arm_lpae_free_pgtable,
1048};
1049
1050struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1051 .alloc = arm_32_lpae_alloc_pgtable_s1,
1052 .free = arm_lpae_free_pgtable,
1053};
1054
1055struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1056 .alloc = arm_32_lpae_alloc_pgtable_s2,
1057 .free = arm_lpae_free_pgtable,
1058};
Will Deaconfe4b9912014-11-17 23:31:12 +00001059
Rob Herringd08d42d2019-02-21 14:23:25 -06001060struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1061 .alloc = arm_mali_lpae_alloc_pgtable,
1062 .free = arm_lpae_free_pgtable,
1063};
1064
Will Deaconfe4b9912014-11-17 23:31:12 +00001065#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1066
Robin Murphyb5813c12019-10-25 19:08:30 +01001067static struct io_pgtable_cfg *cfg_cookie __initdata;
Will Deaconfe4b9912014-11-17 23:31:12 +00001068
Robin Murphyb5813c12019-10-25 19:08:30 +01001069static void __init dummy_tlb_flush_all(void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001070{
1071 WARN_ON(cookie != cfg_cookie);
1072}
1073
Robin Murphyb5813c12019-10-25 19:08:30 +01001074static void __init dummy_tlb_flush(unsigned long iova, size_t size,
1075 size_t granule, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +00001076{
1077 WARN_ON(cookie != cfg_cookie);
1078 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1079}
1080
Robin Murphyb5813c12019-10-25 19:08:30 +01001081static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
1082 unsigned long iova, size_t granule,
1083 void *cookie)
Will Deacon10b7a7d2019-07-02 16:44:32 +01001084{
Will Deaconabfd6fe2019-07-02 16:44:41 +01001085 dummy_tlb_flush(iova, granule, granule, cookie);
Will Deacon10b7a7d2019-07-02 16:44:32 +01001086}
1087
Will Deacon298f78892019-07-02 16:43:34 +01001088static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001089 .tlb_flush_all = dummy_tlb_flush_all,
Will Deacon10b7a7d2019-07-02 16:44:32 +01001090 .tlb_flush_walk = dummy_tlb_flush,
1091 .tlb_flush_leaf = dummy_tlb_flush,
Will Deaconabfd6fe2019-07-02 16:44:41 +01001092 .tlb_add_page = dummy_tlb_add_page,
Will Deaconfe4b9912014-11-17 23:31:12 +00001093};
1094
1095static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1096{
1097 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1098 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1099
1100 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1101 cfg->pgsize_bitmap, cfg->ias);
Robin Murphy5fb190b2019-10-25 19:08:35 +01001102 pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
Robin Murphyc79278c2019-10-25 19:08:34 +01001103 ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
Robin Murphy5fb190b2019-10-25 19:08:35 +01001104 ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
Will Deaconfe4b9912014-11-17 23:31:12 +00001105}
1106
1107#define __FAIL(ops, i) ({ \
1108 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1109 arm_lpae_dump_ops(ops); \
1110 selftest_running = false; \
1111 -EFAULT; \
1112})
1113
1114static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1115{
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001116 static const enum io_pgtable_fmt fmts[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001117 ARM_64_LPAE_S1,
1118 ARM_64_LPAE_S2,
1119 };
1120
1121 int i, j;
1122 unsigned long iova;
1123 size_t size;
1124 struct io_pgtable_ops *ops;
1125
1126 selftest_running = true;
1127
1128 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1129 cfg_cookie = cfg;
1130 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1131 if (!ops) {
1132 pr_err("selftest: failed to allocate io pgtable ops\n");
1133 return -ENOMEM;
1134 }
1135
1136 /*
1137 * Initial sanity checks.
1138 * Empty page tables shouldn't provide any translations.
1139 */
1140 if (ops->iova_to_phys(ops, 42))
1141 return __FAIL(ops, i);
1142
1143 if (ops->iova_to_phys(ops, SZ_1G + 42))
1144 return __FAIL(ops, i);
1145
1146 if (ops->iova_to_phys(ops, SZ_2G + 42))
1147 return __FAIL(ops, i);
1148
1149 /*
1150 * Distinct mappings of different granule sizes.
1151 */
1152 iova = 0;
Kefeng Wang4ae8a5c2016-09-21 13:41:31 +08001153 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001154 size = 1UL << j;
1155
1156 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1157 IOMMU_WRITE |
1158 IOMMU_NOEXEC |
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001159 IOMMU_CACHE, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001160 return __FAIL(ops, i);
1161
1162 /* Overlapping mappings */
1163 if (!ops->map(ops, iova, iova + size, size,
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001164 IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001165 return __FAIL(ops, i);
1166
1167 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1168 return __FAIL(ops, i);
1169
1170 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001171 }
1172
1173 /* Partial unmap */
1174 size = 1UL << __ffs(cfg->pgsize_bitmap);
Will Deacona2d3a382019-07-02 16:44:58 +01001175 if (ops->unmap(ops, SZ_1G + size, size, NULL) != size)
Will Deaconfe4b9912014-11-17 23:31:12 +00001176 return __FAIL(ops, i);
1177
1178 /* Remap of partial unmap */
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001179 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001180 return __FAIL(ops, i);
1181
1182 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1183 return __FAIL(ops, i);
1184
1185 /* Full unmap */
1186 iova = 0;
YueHaibingf793b132018-04-26 12:49:29 +08001187 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
Will Deaconfe4b9912014-11-17 23:31:12 +00001188 size = 1UL << j;
1189
Will Deacona2d3a382019-07-02 16:44:58 +01001190 if (ops->unmap(ops, iova, size, NULL) != size)
Will Deaconfe4b9912014-11-17 23:31:12 +00001191 return __FAIL(ops, i);
1192
1193 if (ops->iova_to_phys(ops, iova + 42))
1194 return __FAIL(ops, i);
1195
1196 /* Remap full block */
Baolin Wangf34ce7a2020-06-12 11:39:55 +08001197 if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL))
Will Deaconfe4b9912014-11-17 23:31:12 +00001198 return __FAIL(ops, i);
1199
1200 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1201 return __FAIL(ops, i);
1202
1203 iova += SZ_1G;
Will Deaconfe4b9912014-11-17 23:31:12 +00001204 }
1205
1206 free_io_pgtable_ops(ops);
1207 }
1208
1209 selftest_running = false;
1210 return 0;
1211}
1212
1213static int __init arm_lpae_do_selftests(void)
1214{
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001215 static const unsigned long pgsize[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001216 SZ_4K | SZ_2M | SZ_1G,
1217 SZ_16K | SZ_32M,
1218 SZ_64K | SZ_512M,
1219 };
1220
Christophe JAILLET9062c1d2019-09-09 22:19:19 +02001221 static const unsigned int ias[] __initconst = {
Will Deaconfe4b9912014-11-17 23:31:12 +00001222 32, 36, 40, 42, 44, 48,
1223 };
1224
1225 int i, j, pass = 0, fail = 0;
1226 struct io_pgtable_cfg cfg = {
1227 .tlb = &dummy_tlb_ops,
1228 .oas = 48,
Will Deacon4f418452019-06-25 12:51:25 +01001229 .coherent_walk = true,
Will Deaconfe4b9912014-11-17 23:31:12 +00001230 };
1231
1232 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1233 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1234 cfg.pgsize_bitmap = pgsize[i];
1235 cfg.ias = ias[j];
1236 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1237 pgsize[i], ias[j]);
1238 if (arm_lpae_run_tests(&cfg))
1239 fail++;
1240 else
1241 pass++;
1242 }
1243 }
1244
1245 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1246 return fail ? -EFAULT : 0;
1247}
1248subsys_initcall(arm_lpae_do_selftests);
1249#endif