blob: 7c9d632f1d0907eaea71050095ca9ef5a904e793 [file] [log] [blame]
Will Deacone1d3c0f2014-11-14 17:18:23 +00001/*
2 * CPU-agnostic ARM page table allocator.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Copyright (C) 2014 ARM Limited
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 */
20
21#define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
22
23#include <linux/iommu.h>
24#include <linux/kernel.h>
25#include <linux/sizes.h>
26#include <linux/slab.h>
27#include <linux/types.h>
Lada Trimasova8f6aff92016-01-27 11:10:32 +000028#include <linux/dma-mapping.h>
Will Deacone1d3c0f2014-11-14 17:18:23 +000029
Robin Murphy87a91b12015-07-29 19:46:09 +010030#include <asm/barrier.h>
31
Will Deacone1d3c0f2014-11-14 17:18:23 +000032#include "io-pgtable.h"
33
34#define ARM_LPAE_MAX_ADDR_BITS 48
35#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
36#define ARM_LPAE_MAX_LEVELS 4
37
38/* Struct accessors */
39#define io_pgtable_to_data(x) \
40 container_of((x), struct arm_lpae_io_pgtable, iop)
41
Will Deacone1d3c0f2014-11-14 17:18:23 +000042#define io_pgtable_ops_to_data(x) \
43 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
44
45/*
46 * For consistency with the architecture, we always consider
47 * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
48 */
49#define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels)
50
51/*
52 * Calculate the right shift amount to get to the portion describing level l
53 * in a virtual address mapped by the pagetable in d.
54 */
55#define ARM_LPAE_LVL_SHIFT(l,d) \
56 ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \
57 * (d)->bits_per_level) + (d)->pg_shift)
58
Robin Murphy06c610e2015-12-07 18:18:53 +000059#define ARM_LPAE_GRANULE(d) (1UL << (d)->pg_shift)
60
Will Deacon367bd972015-02-16 18:38:20 +000061#define ARM_LPAE_PAGES_PER_PGD(d) \
Robin Murphy06c610e2015-12-07 18:18:53 +000062 DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d))
Will Deacone1d3c0f2014-11-14 17:18:23 +000063
64/*
65 * Calculate the index at level l used to map virtual address a using the
66 * pagetable in d.
67 */
68#define ARM_LPAE_PGD_IDX(l,d) \
69 ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
70
71#define ARM_LPAE_LVL_IDX(a,l,d) \
Will Deacon367bd972015-02-16 18:38:20 +000072 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
Will Deacone1d3c0f2014-11-14 17:18:23 +000073 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
74
75/* Calculate the block/page mapping size at level l for pagetable in d. */
76#define ARM_LPAE_BLOCK_SIZE(l,d) \
77 (1 << (ilog2(sizeof(arm_lpae_iopte)) + \
78 ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
79
80/* Page table bits */
81#define ARM_LPAE_PTE_TYPE_SHIFT 0
82#define ARM_LPAE_PTE_TYPE_MASK 0x3
83
84#define ARM_LPAE_PTE_TYPE_BLOCK 1
85#define ARM_LPAE_PTE_TYPE_TABLE 3
86#define ARM_LPAE_PTE_TYPE_PAGE 3
87
Laurent Pinchartc896c132014-12-14 23:34:50 +020088#define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
Will Deacone1d3c0f2014-11-14 17:18:23 +000089#define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
90#define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
91#define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
92#define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
93#define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
Laurent Pinchartc896c132014-12-14 23:34:50 +020094#define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
Will Deacone1d3c0f2014-11-14 17:18:23 +000095#define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
96
97#define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
98/* Ignore the contiguous bit for block splitting */
99#define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
100#define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
101 ARM_LPAE_PTE_ATTR_HI_MASK)
102
103/* Stage-1 PTE */
104#define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
105#define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
106#define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
107#define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
108
109/* Stage-2 PTE */
110#define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
111#define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
112#define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
113#define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
114#define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
115#define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
116
117/* Register bits */
118#define ARM_32_LPAE_TCR_EAE (1 << 31)
119#define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
120
Will Deacon63979b82015-03-18 10:22:18 +0000121#define ARM_LPAE_TCR_EPD1 (1 << 23)
122
Will Deacone1d3c0f2014-11-14 17:18:23 +0000123#define ARM_LPAE_TCR_TG0_4K (0 << 14)
124#define ARM_LPAE_TCR_TG0_64K (1 << 14)
125#define ARM_LPAE_TCR_TG0_16K (2 << 14)
126
127#define ARM_LPAE_TCR_SH0_SHIFT 12
128#define ARM_LPAE_TCR_SH0_MASK 0x3
129#define ARM_LPAE_TCR_SH_NS 0
130#define ARM_LPAE_TCR_SH_OS 2
131#define ARM_LPAE_TCR_SH_IS 3
132
133#define ARM_LPAE_TCR_ORGN0_SHIFT 10
134#define ARM_LPAE_TCR_IRGN0_SHIFT 8
135#define ARM_LPAE_TCR_RGN_MASK 0x3
136#define ARM_LPAE_TCR_RGN_NC 0
137#define ARM_LPAE_TCR_RGN_WBWA 1
138#define ARM_LPAE_TCR_RGN_WT 2
139#define ARM_LPAE_TCR_RGN_WB 3
140
141#define ARM_LPAE_TCR_SL0_SHIFT 6
142#define ARM_LPAE_TCR_SL0_MASK 0x3
143
144#define ARM_LPAE_TCR_T0SZ_SHIFT 0
145#define ARM_LPAE_TCR_SZ_MASK 0xf
146
147#define ARM_LPAE_TCR_PS_SHIFT 16
148#define ARM_LPAE_TCR_PS_MASK 0x7
149
150#define ARM_LPAE_TCR_IPS_SHIFT 32
151#define ARM_LPAE_TCR_IPS_MASK 0x7
152
153#define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
154#define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
155#define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
156#define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
157#define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
158#define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
159
160#define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
161#define ARM_LPAE_MAIR_ATTR_MASK 0xff
162#define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
163#define ARM_LPAE_MAIR_ATTR_NC 0x44
164#define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
165#define ARM_LPAE_MAIR_ATTR_IDX_NC 0
166#define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
167#define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
168
169/* IOPTE accessors */
170#define iopte_deref(pte,d) \
171 (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \
Robin Murphy06c610e2015-12-07 18:18:53 +0000172 & ~(ARM_LPAE_GRANULE(d) - 1ULL)))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000173
174#define iopte_type(pte,l) \
175 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
176
177#define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
178
179#define iopte_leaf(pte,l) \
180 (l == (ARM_LPAE_MAX_LEVELS - 1) ? \
181 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \
182 (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
183
184#define iopte_to_pfn(pte,d) \
185 (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
186
187#define pfn_to_iopte(pfn,d) \
188 (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
189
190struct arm_lpae_io_pgtable {
191 struct io_pgtable iop;
192
193 int levels;
194 size_t pgd_size;
195 unsigned long pg_shift;
196 unsigned long bits_per_level;
197
198 void *pgd;
199};
200
201typedef u64 arm_lpae_iopte;
202
Will Deaconfe4b9912014-11-17 23:31:12 +0000203static bool selftest_running = false;
204
Robin Murphyffcb6d12015-09-17 17:42:16 +0100205static dma_addr_t __arm_lpae_dma_addr(void *pages)
Robin Murphyf8d54962015-07-29 19:46:04 +0100206{
Robin Murphyffcb6d12015-09-17 17:42:16 +0100207 return (dma_addr_t)virt_to_phys(pages);
Robin Murphyf8d54962015-07-29 19:46:04 +0100208}
209
210static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
211 struct io_pgtable_cfg *cfg)
212{
213 struct device *dev = cfg->iommu_dev;
214 dma_addr_t dma;
215 void *pages = alloc_pages_exact(size, gfp | __GFP_ZERO);
216
217 if (!pages)
218 return NULL;
219
Robin Murphy87a91b12015-07-29 19:46:09 +0100220 if (!selftest_running) {
Robin Murphyf8d54962015-07-29 19:46:04 +0100221 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
222 if (dma_mapping_error(dev, dma))
223 goto out_free;
224 /*
225 * We depend on the IOMMU being able to work with any physical
Robin Murphyffcb6d12015-09-17 17:42:16 +0100226 * address directly, so if the DMA layer suggests otherwise by
227 * translating or truncating them, that bodes very badly...
Robin Murphyf8d54962015-07-29 19:46:04 +0100228 */
Robin Murphyffcb6d12015-09-17 17:42:16 +0100229 if (dma != virt_to_phys(pages))
Robin Murphyf8d54962015-07-29 19:46:04 +0100230 goto out_unmap;
231 }
232
233 return pages;
234
235out_unmap:
236 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
237 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
238out_free:
239 free_pages_exact(pages, size);
240 return NULL;
241}
242
243static void __arm_lpae_free_pages(void *pages, size_t size,
244 struct io_pgtable_cfg *cfg)
245{
Robin Murphy87a91b12015-07-29 19:46:09 +0100246 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100247 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
Robin Murphyf8d54962015-07-29 19:46:04 +0100248 size, DMA_TO_DEVICE);
249 free_pages_exact(pages, size);
250}
251
252static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
Robin Murphy87a91b12015-07-29 19:46:09 +0100253 struct io_pgtable_cfg *cfg)
Robin Murphyf8d54962015-07-29 19:46:04 +0100254{
Robin Murphyf8d54962015-07-29 19:46:04 +0100255 *ptep = pte;
256
Robin Murphy87a91b12015-07-29 19:46:09 +0100257 if (!selftest_running)
Robin Murphyffcb6d12015-09-17 17:42:16 +0100258 dma_sync_single_for_device(cfg->iommu_dev,
259 __arm_lpae_dma_addr(ptep),
Robin Murphyf8d54962015-07-29 19:46:04 +0100260 sizeof(pte), DMA_TO_DEVICE);
Robin Murphyf8d54962015-07-29 19:46:04 +0100261}
262
Will Deaconcf27ec92015-08-11 16:48:32 +0100263static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
264 unsigned long iova, size_t size, int lvl,
265 arm_lpae_iopte *ptep);
266
Will Deacone1d3c0f2014-11-14 17:18:23 +0000267static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
268 unsigned long iova, phys_addr_t paddr,
269 arm_lpae_iopte prot, int lvl,
270 arm_lpae_iopte *ptep)
271{
272 arm_lpae_iopte pte = prot;
Robin Murphyf8d54962015-07-29 19:46:04 +0100273 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000274
Will Deaconfe4b9912014-11-17 23:31:12 +0000275 if (iopte_leaf(*ptep, lvl)) {
Will Deaconcf27ec92015-08-11 16:48:32 +0100276 /* We require an unmap first */
Will Deaconfe4b9912014-11-17 23:31:12 +0000277 WARN_ON(!selftest_running);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000278 return -EEXIST;
Will Deaconcf27ec92015-08-11 16:48:32 +0100279 } else if (iopte_type(*ptep, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
280 /*
281 * We need to unmap and free the old table before
282 * overwriting it with a block entry.
283 */
284 arm_lpae_iopte *tblp;
285 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
286
287 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
288 if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz))
289 return -EINVAL;
Will Deaconfe4b9912014-11-17 23:31:12 +0000290 }
Will Deacone1d3c0f2014-11-14 17:18:23 +0000291
Robin Murphyf8d54962015-07-29 19:46:04 +0100292 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200293 pte |= ARM_LPAE_PTE_NS;
294
Will Deacone1d3c0f2014-11-14 17:18:23 +0000295 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
296 pte |= ARM_LPAE_PTE_TYPE_PAGE;
297 else
298 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
299
300 pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
301 pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
302
Robin Murphy87a91b12015-07-29 19:46:09 +0100303 __arm_lpae_set_pte(ptep, pte, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000304 return 0;
305}
306
307static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
308 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
309 int lvl, arm_lpae_iopte *ptep)
310{
311 arm_lpae_iopte *cptep, pte;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000312 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
Robin Murphyf8d54962015-07-29 19:46:04 +0100313 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000314
315 /* Find our entry at the current level */
316 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
317
318 /* If we can install a leaf entry at this level, then do so */
Robin Murphyf8d54962015-07-29 19:46:04 +0100319 if (size == block_size && (size & cfg->pgsize_bitmap))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000320 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
321
322 /* We can't allocate tables at the final level */
323 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
324 return -EINVAL;
325
326 /* Grab a pointer to the next level */
327 pte = *ptep;
328 if (!pte) {
Robin Murphy06c610e2015-12-07 18:18:53 +0000329 cptep = __arm_lpae_alloc_pages(ARM_LPAE_GRANULE(data),
Robin Murphyf8d54962015-07-29 19:46:04 +0100330 GFP_ATOMIC, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000331 if (!cptep)
332 return -ENOMEM;
333
Will Deacone1d3c0f2014-11-14 17:18:23 +0000334 pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
Robin Murphyf8d54962015-07-29 19:46:04 +0100335 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
Laurent Pinchartc896c132014-12-14 23:34:50 +0200336 pte |= ARM_LPAE_PTE_NSTABLE;
Robin Murphy87a91b12015-07-29 19:46:09 +0100337 __arm_lpae_set_pte(ptep, pte, cfg);
Oleksandr Tyshchenko2d595302017-02-27 14:30:25 +0200338 } else if (!iopte_leaf(pte, lvl)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000339 cptep = iopte_deref(pte, data);
Oleksandr Tyshchenko2d595302017-02-27 14:30:25 +0200340 } else {
341 /* We require an unmap first */
342 WARN_ON(!selftest_running);
343 return -EEXIST;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000344 }
345
346 /* Rinse, repeat */
347 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
348}
349
350static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
351 int prot)
352{
353 arm_lpae_iopte pte;
354
355 if (data->iop.fmt == ARM_64_LPAE_S1 ||
356 data->iop.fmt == ARM_32_LPAE_S1) {
357 pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
358
359 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
360 pte |= ARM_LPAE_PTE_AP_RDONLY;
361
Robin Murphyfb948252016-04-05 12:39:31 +0100362 if (prot & IOMMU_MMIO)
363 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
364 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
365 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000366 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
367 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
368 } else {
369 pte = ARM_LPAE_PTE_HAP_FAULT;
370 if (prot & IOMMU_READ)
371 pte |= ARM_LPAE_PTE_HAP_READ;
372 if (prot & IOMMU_WRITE)
373 pte |= ARM_LPAE_PTE_HAP_WRITE;
Robin Murphyfb948252016-04-05 12:39:31 +0100374 if (prot & IOMMU_MMIO)
375 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
376 else if (prot & IOMMU_CACHE)
Will Deacone1d3c0f2014-11-14 17:18:23 +0000377 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
378 else
379 pte |= ARM_LPAE_PTE_MEMATTR_NC;
380 }
381
382 if (prot & IOMMU_NOEXEC)
383 pte |= ARM_LPAE_PTE_XN;
384
385 return pte;
386}
387
388static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
389 phys_addr_t paddr, size_t size, int iommu_prot)
390{
391 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
392 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100393 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000394 arm_lpae_iopte prot;
395
396 /* If no access, then nothing to do */
397 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
398 return 0;
399
400 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100401 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
402 /*
403 * Synchronise all PTE updates for the new mapping before there's
404 * a chance for anything to kick off a table walk for the new iova.
405 */
406 wmb();
407
408 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000409}
410
411static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
412 arm_lpae_iopte *ptep)
413{
414 arm_lpae_iopte *start, *end;
415 unsigned long table_size;
416
Will Deacone1d3c0f2014-11-14 17:18:23 +0000417 if (lvl == ARM_LPAE_START_LVL(data))
418 table_size = data->pgd_size;
419 else
Robin Murphy06c610e2015-12-07 18:18:53 +0000420 table_size = ARM_LPAE_GRANULE(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000421
422 start = ptep;
Will Deacon12c2ab02015-12-15 16:08:12 +0000423
424 /* Only leaf entries at the last level */
425 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
426 end = ptep;
427 else
428 end = (void *)ptep + table_size;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000429
430 while (ptep != end) {
431 arm_lpae_iopte pte = *ptep++;
432
433 if (!pte || iopte_leaf(pte, lvl))
434 continue;
435
436 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
437 }
438
Robin Murphyf8d54962015-07-29 19:46:04 +0100439 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000440}
441
442static void arm_lpae_free_pgtable(struct io_pgtable *iop)
443{
444 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
445
446 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
447 kfree(data);
448}
449
450static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
451 unsigned long iova, size_t size,
452 arm_lpae_iopte prot, int lvl,
453 arm_lpae_iopte *ptep, size_t blk_size)
454{
455 unsigned long blk_start, blk_end;
456 phys_addr_t blk_paddr;
457 arm_lpae_iopte table = 0;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000458
459 blk_start = iova & ~(blk_size - 1);
460 blk_end = blk_start + blk_size;
461 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
462
463 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
464 arm_lpae_iopte *tablep;
465
466 /* Unmap! */
467 if (blk_start == iova)
468 continue;
469
470 /* __arm_lpae_map expects a pointer to the start of the table */
471 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
472 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
473 tablep) < 0) {
474 if (table) {
475 /* Free the table we allocated */
476 tablep = iopte_deref(table, data);
477 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
478 }
479 return 0; /* Bytes unmapped */
480 }
481 }
482
Robin Murphy507e4c92016-01-26 17:13:14 +0000483 __arm_lpae_set_pte(ptep, table, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000484 iova &= ~(blk_size - 1);
Robin Murphy507e4c92016-01-26 17:13:14 +0000485 io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000486 return size;
487}
488
489static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
490 unsigned long iova, size_t size, int lvl,
491 arm_lpae_iopte *ptep)
492{
493 arm_lpae_iopte pte;
Robin Murphy507e4c92016-01-26 17:13:14 +0000494 struct io_pgtable *iop = &data->iop;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000495 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
496
Robin Murphy2eb97c72015-12-04 17:52:58 +0000497 /* Something went horribly wrong and we ran out of page table */
498 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
499 return 0;
500
Will Deacone1d3c0f2014-11-14 17:18:23 +0000501 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
502 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000503 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000504 return 0;
505
506 /* If the size matches this level, we're in the right place */
507 if (size == blk_size) {
Robin Murphy507e4c92016-01-26 17:13:14 +0000508 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000509
510 if (!iopte_leaf(pte, lvl)) {
511 /* Also flush any partial walks */
Robin Murphy507e4c92016-01-26 17:13:14 +0000512 io_pgtable_tlb_add_flush(iop, iova, size,
513 ARM_LPAE_GRANULE(data), false);
514 io_pgtable_tlb_sync(iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000515 ptep = iopte_deref(pte, data);
516 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
517 } else {
Robin Murphy507e4c92016-01-26 17:13:14 +0000518 io_pgtable_tlb_add_flush(iop, iova, size, size, true);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000519 }
520
521 return size;
522 } else if (iopte_leaf(pte, lvl)) {
523 /*
524 * Insert a table at the next level to map the old region,
525 * minus the part we want to unmap
526 */
527 return arm_lpae_split_blk_unmap(data, iova, size,
528 iopte_prot(pte), lvl, ptep,
529 blk_size);
530 }
531
532 /* Keep on walkin' */
533 ptep = iopte_deref(pte, data);
534 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
535}
536
537static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
538 size_t size)
539{
540 size_t unmapped;
541 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000542 arm_lpae_iopte *ptep = data->pgd;
543 int lvl = ARM_LPAE_START_LVL(data);
544
545 unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
546 if (unmapped)
Robin Murphy507e4c92016-01-26 17:13:14 +0000547 io_pgtable_tlb_sync(&data->iop);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000548
549 return unmapped;
550}
551
552static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
553 unsigned long iova)
554{
555 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
556 arm_lpae_iopte pte, *ptep = data->pgd;
557 int lvl = ARM_LPAE_START_LVL(data);
558
559 do {
560 /* Valid IOPTE pointer? */
561 if (!ptep)
562 return 0;
563
564 /* Grab the IOPTE we're interested in */
565 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
566
567 /* Valid entry? */
568 if (!pte)
569 return 0;
570
571 /* Leaf entry? */
572 if (iopte_leaf(pte,lvl))
573 goto found_translation;
574
575 /* Take it to the next level */
576 ptep = iopte_deref(pte, data);
577 } while (++lvl < ARM_LPAE_MAX_LEVELS);
578
579 /* Ran out of page tables to walk */
580 return 0;
581
582found_translation:
Will Deacon7c6d90e2016-06-16 18:21:19 +0100583 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000584 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
585}
586
587static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
588{
589 unsigned long granule;
590
591 /*
592 * We need to restrict the supported page sizes to match the
593 * translation regime for a particular granule. Aim to match
594 * the CPU page size if possible, otherwise prefer smaller sizes.
595 * While we're at it, restrict the block sizes to match the
596 * chosen granule.
597 */
598 if (cfg->pgsize_bitmap & PAGE_SIZE)
599 granule = PAGE_SIZE;
600 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
601 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
602 else if (cfg->pgsize_bitmap & PAGE_MASK)
603 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
604 else
605 granule = 0;
606
607 switch (granule) {
608 case SZ_4K:
609 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
610 break;
611 case SZ_16K:
612 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
613 break;
614 case SZ_64K:
615 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
616 break;
617 default:
618 cfg->pgsize_bitmap = 0;
619 }
620}
621
622static struct arm_lpae_io_pgtable *
623arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
624{
625 unsigned long va_bits, pgd_bits;
626 struct arm_lpae_io_pgtable *data;
627
628 arm_lpae_restrict_pgsizes(cfg);
629
630 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
631 return NULL;
632
633 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
634 return NULL;
635
636 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
637 return NULL;
638
Robin Murphyffcb6d12015-09-17 17:42:16 +0100639 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
640 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
641 return NULL;
642 }
643
Will Deacone1d3c0f2014-11-14 17:18:23 +0000644 data = kmalloc(sizeof(*data), GFP_KERNEL);
645 if (!data)
646 return NULL;
647
648 data->pg_shift = __ffs(cfg->pgsize_bitmap);
649 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
650
651 va_bits = cfg->ias - data->pg_shift;
652 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
653
654 /* Calculate the actual size of our pgd (without concatenation) */
655 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
656 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
657
658 data->iop.ops = (struct io_pgtable_ops) {
659 .map = arm_lpae_map,
660 .unmap = arm_lpae_unmap,
661 .iova_to_phys = arm_lpae_iova_to_phys,
662 };
663
664 return data;
665}
666
667static struct io_pgtable *
668arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
669{
670 u64 reg;
Robin Murphy3850db42016-02-12 17:09:46 +0000671 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000672
Robin Murphy3850db42016-02-12 17:09:46 +0000673 if (cfg->quirks & ~IO_PGTABLE_QUIRK_ARM_NS)
674 return NULL;
675
676 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000677 if (!data)
678 return NULL;
679
680 /* TCR */
681 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
682 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
683 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
684
Robin Murphy06c610e2015-12-07 18:18:53 +0000685 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000686 case SZ_4K:
687 reg |= ARM_LPAE_TCR_TG0_4K;
688 break;
689 case SZ_16K:
690 reg |= ARM_LPAE_TCR_TG0_16K;
691 break;
692 case SZ_64K:
693 reg |= ARM_LPAE_TCR_TG0_64K;
694 break;
695 }
696
697 switch (cfg->oas) {
698 case 32:
699 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
700 break;
701 case 36:
702 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
703 break;
704 case 40:
705 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
706 break;
707 case 42:
708 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
709 break;
710 case 44:
711 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
712 break;
713 case 48:
714 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
715 break;
716 default:
717 goto out_free_data;
718 }
719
720 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000721
722 /* Disable speculative walks through TTBR1 */
723 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000724 cfg->arm_lpae_s1_cfg.tcr = reg;
725
726 /* MAIRs */
727 reg = (ARM_LPAE_MAIR_ATTR_NC
728 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
729 (ARM_LPAE_MAIR_ATTR_WBRWA
730 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
731 (ARM_LPAE_MAIR_ATTR_DEVICE
732 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
733
734 cfg->arm_lpae_s1_cfg.mair[0] = reg;
735 cfg->arm_lpae_s1_cfg.mair[1] = 0;
736
737 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100738 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000739 if (!data->pgd)
740 goto out_free_data;
741
Robin Murphy87a91b12015-07-29 19:46:09 +0100742 /* Ensure the empty pgd is visible before any actual TTBR write */
743 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000744
745 /* TTBRs */
746 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
747 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
748 return &data->iop;
749
750out_free_data:
751 kfree(data);
752 return NULL;
753}
754
755static struct io_pgtable *
756arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
757{
758 u64 reg, sl;
Robin Murphy3850db42016-02-12 17:09:46 +0000759 struct arm_lpae_io_pgtable *data;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000760
Robin Murphy3850db42016-02-12 17:09:46 +0000761 /* The NS quirk doesn't apply at stage 2 */
762 if (cfg->quirks)
763 return NULL;
764
765 data = arm_lpae_alloc_pgtable(cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000766 if (!data)
767 return NULL;
768
769 /*
770 * Concatenate PGDs at level 1 if possible in order to reduce
771 * the depth of the stage-2 walk.
772 */
773 if (data->levels == ARM_LPAE_MAX_LEVELS) {
774 unsigned long pgd_pages;
775
776 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
777 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
778 data->pgd_size = pgd_pages << data->pg_shift;
779 data->levels--;
780 }
781 }
782
783 /* VTCR */
784 reg = ARM_64_LPAE_S2_TCR_RES1 |
785 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
786 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
787 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
788
789 sl = ARM_LPAE_START_LVL(data);
790
Robin Murphy06c610e2015-12-07 18:18:53 +0000791 switch (ARM_LPAE_GRANULE(data)) {
Will Deacone1d3c0f2014-11-14 17:18:23 +0000792 case SZ_4K:
793 reg |= ARM_LPAE_TCR_TG0_4K;
794 sl++; /* SL0 format is different for 4K granule size */
795 break;
796 case SZ_16K:
797 reg |= ARM_LPAE_TCR_TG0_16K;
798 break;
799 case SZ_64K:
800 reg |= ARM_LPAE_TCR_TG0_64K;
801 break;
802 }
803
804 switch (cfg->oas) {
805 case 32:
806 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
807 break;
808 case 36:
809 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
810 break;
811 case 40:
812 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
813 break;
814 case 42:
815 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
816 break;
817 case 44:
818 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
819 break;
820 case 48:
821 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
822 break;
823 default:
824 goto out_free_data;
825 }
826
827 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
828 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
829 cfg->arm_lpae_s2_cfg.vtcr = reg;
830
831 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100832 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000833 if (!data->pgd)
834 goto out_free_data;
835
Robin Murphy87a91b12015-07-29 19:46:09 +0100836 /* Ensure the empty pgd is visible before any actual TTBR write */
837 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000838
839 /* VTTBR */
840 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
841 return &data->iop;
842
843out_free_data:
844 kfree(data);
845 return NULL;
846}
847
848static struct io_pgtable *
849arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
850{
851 struct io_pgtable *iop;
852
853 if (cfg->ias > 32 || cfg->oas > 40)
854 return NULL;
855
856 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
857 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
858 if (iop) {
859 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
860 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
861 }
862
863 return iop;
864}
865
866static struct io_pgtable *
867arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
868{
869 struct io_pgtable *iop;
870
871 if (cfg->ias > 40 || cfg->oas > 40)
872 return NULL;
873
874 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
875 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
876 if (iop)
877 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
878
879 return iop;
880}
881
882struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
883 .alloc = arm_64_lpae_alloc_pgtable_s1,
884 .free = arm_lpae_free_pgtable,
885};
886
887struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
888 .alloc = arm_64_lpae_alloc_pgtable_s2,
889 .free = arm_lpae_free_pgtable,
890};
891
892struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
893 .alloc = arm_32_lpae_alloc_pgtable_s1,
894 .free = arm_lpae_free_pgtable,
895};
896
897struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
898 .alloc = arm_32_lpae_alloc_pgtable_s2,
899 .free = arm_lpae_free_pgtable,
900};
Will Deaconfe4b9912014-11-17 23:31:12 +0000901
902#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
903
904static struct io_pgtable_cfg *cfg_cookie;
905
906static void dummy_tlb_flush_all(void *cookie)
907{
908 WARN_ON(cookie != cfg_cookie);
909}
910
Robin Murphy06c610e2015-12-07 18:18:53 +0000911static void dummy_tlb_add_flush(unsigned long iova, size_t size,
912 size_t granule, bool leaf, void *cookie)
Will Deaconfe4b9912014-11-17 23:31:12 +0000913{
914 WARN_ON(cookie != cfg_cookie);
915 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
916}
917
918static void dummy_tlb_sync(void *cookie)
919{
920 WARN_ON(cookie != cfg_cookie);
921}
922
Will Deaconfe4b9912014-11-17 23:31:12 +0000923static struct iommu_gather_ops dummy_tlb_ops __initdata = {
924 .tlb_flush_all = dummy_tlb_flush_all,
925 .tlb_add_flush = dummy_tlb_add_flush,
926 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +0000927};
928
929static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
930{
931 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
932 struct io_pgtable_cfg *cfg = &data->iop.cfg;
933
934 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
935 cfg->pgsize_bitmap, cfg->ias);
936 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
937 data->levels, data->pgd_size, data->pg_shift,
938 data->bits_per_level, data->pgd);
939}
940
941#define __FAIL(ops, i) ({ \
942 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
943 arm_lpae_dump_ops(ops); \
944 selftest_running = false; \
945 -EFAULT; \
946})
947
948static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
949{
950 static const enum io_pgtable_fmt fmts[] = {
951 ARM_64_LPAE_S1,
952 ARM_64_LPAE_S2,
953 };
954
955 int i, j;
956 unsigned long iova;
957 size_t size;
958 struct io_pgtable_ops *ops;
959
960 selftest_running = true;
961
962 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
963 cfg_cookie = cfg;
964 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
965 if (!ops) {
966 pr_err("selftest: failed to allocate io pgtable ops\n");
967 return -ENOMEM;
968 }
969
970 /*
971 * Initial sanity checks.
972 * Empty page tables shouldn't provide any translations.
973 */
974 if (ops->iova_to_phys(ops, 42))
975 return __FAIL(ops, i);
976
977 if (ops->iova_to_phys(ops, SZ_1G + 42))
978 return __FAIL(ops, i);
979
980 if (ops->iova_to_phys(ops, SZ_2G + 42))
981 return __FAIL(ops, i);
982
983 /*
984 * Distinct mappings of different granule sizes.
985 */
986 iova = 0;
987 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
988 while (j != BITS_PER_LONG) {
989 size = 1UL << j;
990
991 if (ops->map(ops, iova, iova, size, IOMMU_READ |
992 IOMMU_WRITE |
993 IOMMU_NOEXEC |
994 IOMMU_CACHE))
995 return __FAIL(ops, i);
996
997 /* Overlapping mappings */
998 if (!ops->map(ops, iova, iova + size, size,
999 IOMMU_READ | IOMMU_NOEXEC))
1000 return __FAIL(ops, i);
1001
1002 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1003 return __FAIL(ops, i);
1004
1005 iova += SZ_1G;
1006 j++;
1007 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1008 }
1009
1010 /* Partial unmap */
1011 size = 1UL << __ffs(cfg->pgsize_bitmap);
1012 if (ops->unmap(ops, SZ_1G + size, size) != size)
1013 return __FAIL(ops, i);
1014
1015 /* Remap of partial unmap */
1016 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1017 return __FAIL(ops, i);
1018
1019 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1020 return __FAIL(ops, i);
1021
1022 /* Full unmap */
1023 iova = 0;
1024 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1025 while (j != BITS_PER_LONG) {
1026 size = 1UL << j;
1027
1028 if (ops->unmap(ops, iova, size) != size)
1029 return __FAIL(ops, i);
1030
1031 if (ops->iova_to_phys(ops, iova + 42))
1032 return __FAIL(ops, i);
1033
1034 /* Remap full block */
1035 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1036 return __FAIL(ops, i);
1037
1038 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1039 return __FAIL(ops, i);
1040
1041 iova += SZ_1G;
1042 j++;
1043 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1044 }
1045
1046 free_io_pgtable_ops(ops);
1047 }
1048
1049 selftest_running = false;
1050 return 0;
1051}
1052
1053static int __init arm_lpae_do_selftests(void)
1054{
1055 static const unsigned long pgsize[] = {
1056 SZ_4K | SZ_2M | SZ_1G,
1057 SZ_16K | SZ_32M,
1058 SZ_64K | SZ_512M,
1059 };
1060
1061 static const unsigned int ias[] = {
1062 32, 36, 40, 42, 44, 48,
1063 };
1064
1065 int i, j, pass = 0, fail = 0;
1066 struct io_pgtable_cfg cfg = {
1067 .tlb = &dummy_tlb_ops,
1068 .oas = 48,
1069 };
1070
1071 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1072 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1073 cfg.pgsize_bitmap = pgsize[i];
1074 cfg.ias = ias[j];
1075 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1076 pgsize[i], ias[j]);
1077 if (arm_lpae_run_tests(&cfg))
1078 fail++;
1079 else
1080 pass++;
1081 }
1082 }
1083
1084 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1085 return fail ? -EFAULT : 0;
1086}
1087subsys_initcall(arm_lpae_do_selftests);
1088#endif