blob: 366a354c689d5ccfd06a981eb4b398966b2951da [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>
28
Robin Murphy87a91b12015-07-29 19:46:09 +010029#include <asm/barrier.h>
30
Will Deacone1d3c0f2014-11-14 17:18:23 +000031#include "io-pgtable.h"
32
33#define ARM_LPAE_MAX_ADDR_BITS 48
34#define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
35#define ARM_LPAE_MAX_LEVELS 4
36
37/* Struct accessors */
38#define io_pgtable_to_data(x) \
39 container_of((x), struct arm_lpae_io_pgtable, iop)
40
41#define io_pgtable_ops_to_pgtable(x) \
42 container_of((x), struct io_pgtable, ops)
43
44#define io_pgtable_ops_to_data(x) \
45 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
46
47/*
48 * For consistency with the architecture, we always consider
49 * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
50 */
51#define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels)
52
53/*
54 * Calculate the right shift amount to get to the portion describing level l
55 * in a virtual address mapped by the pagetable in d.
56 */
57#define ARM_LPAE_LVL_SHIFT(l,d) \
58 ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \
59 * (d)->bits_per_level) + (d)->pg_shift)
60
Will Deacon367bd972015-02-16 18:38:20 +000061#define ARM_LPAE_PAGES_PER_PGD(d) \
62 DIV_ROUND_UP((d)->pgd_size, 1UL << (d)->pg_shift)
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) \
172 & ~((1ULL << (d)->pg_shift) - 1)))
173
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 Murphyf8d54962015-07-29 19:46:04 +0100329 cptep = __arm_lpae_alloc_pages(1UL << data->pg_shift,
330 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);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000338 } else {
339 cptep = iopte_deref(pte, data);
340 }
341
342 /* Rinse, repeat */
343 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
344}
345
346static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
347 int prot)
348{
349 arm_lpae_iopte pte;
350
351 if (data->iop.fmt == ARM_64_LPAE_S1 ||
352 data->iop.fmt == ARM_32_LPAE_S1) {
353 pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
354
355 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
356 pte |= ARM_LPAE_PTE_AP_RDONLY;
357
358 if (prot & IOMMU_CACHE)
359 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
360 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
361 } else {
362 pte = ARM_LPAE_PTE_HAP_FAULT;
363 if (prot & IOMMU_READ)
364 pte |= ARM_LPAE_PTE_HAP_READ;
365 if (prot & IOMMU_WRITE)
366 pte |= ARM_LPAE_PTE_HAP_WRITE;
367 if (prot & IOMMU_CACHE)
368 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
369 else
370 pte |= ARM_LPAE_PTE_MEMATTR_NC;
371 }
372
373 if (prot & IOMMU_NOEXEC)
374 pte |= ARM_LPAE_PTE_XN;
375
376 return pte;
377}
378
379static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
380 phys_addr_t paddr, size_t size, int iommu_prot)
381{
382 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
383 arm_lpae_iopte *ptep = data->pgd;
Robin Murphy87a91b12015-07-29 19:46:09 +0100384 int ret, lvl = ARM_LPAE_START_LVL(data);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000385 arm_lpae_iopte prot;
386
387 /* If no access, then nothing to do */
388 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
389 return 0;
390
391 prot = arm_lpae_prot_to_pte(data, iommu_prot);
Robin Murphy87a91b12015-07-29 19:46:09 +0100392 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
393 /*
394 * Synchronise all PTE updates for the new mapping before there's
395 * a chance for anything to kick off a table walk for the new iova.
396 */
397 wmb();
398
399 return ret;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000400}
401
402static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
403 arm_lpae_iopte *ptep)
404{
405 arm_lpae_iopte *start, *end;
406 unsigned long table_size;
407
408 /* Only leaf entries at the last level */
409 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
410 return;
411
412 if (lvl == ARM_LPAE_START_LVL(data))
413 table_size = data->pgd_size;
414 else
415 table_size = 1UL << data->pg_shift;
416
417 start = ptep;
418 end = (void *)ptep + table_size;
419
420 while (ptep != end) {
421 arm_lpae_iopte pte = *ptep++;
422
423 if (!pte || iopte_leaf(pte, lvl))
424 continue;
425
426 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
427 }
428
Robin Murphyf8d54962015-07-29 19:46:04 +0100429 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000430}
431
432static void arm_lpae_free_pgtable(struct io_pgtable *iop)
433{
434 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
435
436 __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
437 kfree(data);
438}
439
440static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
441 unsigned long iova, size_t size,
442 arm_lpae_iopte prot, int lvl,
443 arm_lpae_iopte *ptep, size_t blk_size)
444{
445 unsigned long blk_start, blk_end;
446 phys_addr_t blk_paddr;
447 arm_lpae_iopte table = 0;
Robin Murphyf8d54962015-07-29 19:46:04 +0100448 struct io_pgtable_cfg *cfg = &data->iop.cfg;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000449
450 blk_start = iova & ~(blk_size - 1);
451 blk_end = blk_start + blk_size;
452 blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
453
454 for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
455 arm_lpae_iopte *tablep;
456
457 /* Unmap! */
458 if (blk_start == iova)
459 continue;
460
461 /* __arm_lpae_map expects a pointer to the start of the table */
462 tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
463 if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
464 tablep) < 0) {
465 if (table) {
466 /* Free the table we allocated */
467 tablep = iopte_deref(table, data);
468 __arm_lpae_free_pgtable(data, lvl + 1, tablep);
469 }
470 return 0; /* Bytes unmapped */
471 }
472 }
473
Robin Murphy87a91b12015-07-29 19:46:09 +0100474 __arm_lpae_set_pte(ptep, table, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000475 iova &= ~(blk_size - 1);
Robin Murphy87a91b12015-07-29 19:46:09 +0100476 cfg->tlb->tlb_add_flush(iova, blk_size, true, data->iop.cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000477 return size;
478}
479
480static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
481 unsigned long iova, size_t size, int lvl,
482 arm_lpae_iopte *ptep)
483{
484 arm_lpae_iopte pte;
485 const struct iommu_gather_ops *tlb = data->iop.cfg.tlb;
486 void *cookie = data->iop.cookie;
487 size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
488
Robin Murphy2eb97c72015-12-04 17:52:58 +0000489 /* Something went horribly wrong and we ran out of page table */
490 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
491 return 0;
492
Will Deacone1d3c0f2014-11-14 17:18:23 +0000493 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
494 pte = *ptep;
Robin Murphy2eb97c72015-12-04 17:52:58 +0000495 if (WARN_ON(!pte))
Will Deacone1d3c0f2014-11-14 17:18:23 +0000496 return 0;
497
498 /* If the size matches this level, we're in the right place */
499 if (size == blk_size) {
Robin Murphy87a91b12015-07-29 19:46:09 +0100500 __arm_lpae_set_pte(ptep, 0, &data->iop.cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000501
502 if (!iopte_leaf(pte, lvl)) {
503 /* Also flush any partial walks */
504 tlb->tlb_add_flush(iova, size, false, cookie);
Robin Murphyf8d54962015-07-29 19:46:04 +0100505 tlb->tlb_sync(cookie);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000506 ptep = iopte_deref(pte, data);
507 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
508 } else {
509 tlb->tlb_add_flush(iova, size, true, cookie);
510 }
511
512 return size;
513 } else if (iopte_leaf(pte, lvl)) {
514 /*
515 * Insert a table at the next level to map the old region,
516 * minus the part we want to unmap
517 */
518 return arm_lpae_split_blk_unmap(data, iova, size,
519 iopte_prot(pte), lvl, ptep,
520 blk_size);
521 }
522
523 /* Keep on walkin' */
524 ptep = iopte_deref(pte, data);
525 return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
526}
527
528static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
529 size_t size)
530{
531 size_t unmapped;
532 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
533 struct io_pgtable *iop = &data->iop;
534 arm_lpae_iopte *ptep = data->pgd;
535 int lvl = ARM_LPAE_START_LVL(data);
536
537 unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
538 if (unmapped)
539 iop->cfg.tlb->tlb_sync(iop->cookie);
540
541 return unmapped;
542}
543
544static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
545 unsigned long iova)
546{
547 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
548 arm_lpae_iopte pte, *ptep = data->pgd;
549 int lvl = ARM_LPAE_START_LVL(data);
550
551 do {
552 /* Valid IOPTE pointer? */
553 if (!ptep)
554 return 0;
555
556 /* Grab the IOPTE we're interested in */
557 pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
558
559 /* Valid entry? */
560 if (!pte)
561 return 0;
562
563 /* Leaf entry? */
564 if (iopte_leaf(pte,lvl))
565 goto found_translation;
566
567 /* Take it to the next level */
568 ptep = iopte_deref(pte, data);
569 } while (++lvl < ARM_LPAE_MAX_LEVELS);
570
571 /* Ran out of page tables to walk */
572 return 0;
573
574found_translation:
575 iova &= ((1 << data->pg_shift) - 1);
576 return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
577}
578
579static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
580{
581 unsigned long granule;
582
583 /*
584 * We need to restrict the supported page sizes to match the
585 * translation regime for a particular granule. Aim to match
586 * the CPU page size if possible, otherwise prefer smaller sizes.
587 * While we're at it, restrict the block sizes to match the
588 * chosen granule.
589 */
590 if (cfg->pgsize_bitmap & PAGE_SIZE)
591 granule = PAGE_SIZE;
592 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
593 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
594 else if (cfg->pgsize_bitmap & PAGE_MASK)
595 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
596 else
597 granule = 0;
598
599 switch (granule) {
600 case SZ_4K:
601 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
602 break;
603 case SZ_16K:
604 cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
605 break;
606 case SZ_64K:
607 cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
608 break;
609 default:
610 cfg->pgsize_bitmap = 0;
611 }
612}
613
614static struct arm_lpae_io_pgtable *
615arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
616{
617 unsigned long va_bits, pgd_bits;
618 struct arm_lpae_io_pgtable *data;
619
620 arm_lpae_restrict_pgsizes(cfg);
621
622 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
623 return NULL;
624
625 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
626 return NULL;
627
628 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
629 return NULL;
630
Robin Murphyffcb6d12015-09-17 17:42:16 +0100631 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
632 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
633 return NULL;
634 }
635
Will Deacone1d3c0f2014-11-14 17:18:23 +0000636 data = kmalloc(sizeof(*data), GFP_KERNEL);
637 if (!data)
638 return NULL;
639
640 data->pg_shift = __ffs(cfg->pgsize_bitmap);
641 data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
642
643 va_bits = cfg->ias - data->pg_shift;
644 data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
645
646 /* Calculate the actual size of our pgd (without concatenation) */
647 pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
648 data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
649
650 data->iop.ops = (struct io_pgtable_ops) {
651 .map = arm_lpae_map,
652 .unmap = arm_lpae_unmap,
653 .iova_to_phys = arm_lpae_iova_to_phys,
654 };
655
656 return data;
657}
658
659static struct io_pgtable *
660arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
661{
662 u64 reg;
663 struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
664
665 if (!data)
666 return NULL;
667
668 /* TCR */
669 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
670 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
671 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
672
673 switch (1 << data->pg_shift) {
674 case SZ_4K:
675 reg |= ARM_LPAE_TCR_TG0_4K;
676 break;
677 case SZ_16K:
678 reg |= ARM_LPAE_TCR_TG0_16K;
679 break;
680 case SZ_64K:
681 reg |= ARM_LPAE_TCR_TG0_64K;
682 break;
683 }
684
685 switch (cfg->oas) {
686 case 32:
687 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
688 break;
689 case 36:
690 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
691 break;
692 case 40:
693 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
694 break;
695 case 42:
696 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
697 break;
698 case 44:
699 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
700 break;
701 case 48:
702 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
703 break;
704 default:
705 goto out_free_data;
706 }
707
708 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
Will Deacon63979b82015-03-18 10:22:18 +0000709
710 /* Disable speculative walks through TTBR1 */
711 reg |= ARM_LPAE_TCR_EPD1;
Will Deacone1d3c0f2014-11-14 17:18:23 +0000712 cfg->arm_lpae_s1_cfg.tcr = reg;
713
714 /* MAIRs */
715 reg = (ARM_LPAE_MAIR_ATTR_NC
716 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
717 (ARM_LPAE_MAIR_ATTR_WBRWA
718 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
719 (ARM_LPAE_MAIR_ATTR_DEVICE
720 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
721
722 cfg->arm_lpae_s1_cfg.mair[0] = reg;
723 cfg->arm_lpae_s1_cfg.mair[1] = 0;
724
725 /* Looking good; allocate a pgd */
Robin Murphyf8d54962015-07-29 19:46:04 +0100726 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000727 if (!data->pgd)
728 goto out_free_data;
729
Robin Murphy87a91b12015-07-29 19:46:09 +0100730 /* Ensure the empty pgd is visible before any actual TTBR write */
731 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000732
733 /* TTBRs */
734 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
735 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
736 return &data->iop;
737
738out_free_data:
739 kfree(data);
740 return NULL;
741}
742
743static struct io_pgtable *
744arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
745{
746 u64 reg, sl;
747 struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
748
749 if (!data)
750 return NULL;
751
752 /*
753 * Concatenate PGDs at level 1 if possible in order to reduce
754 * the depth of the stage-2 walk.
755 */
756 if (data->levels == ARM_LPAE_MAX_LEVELS) {
757 unsigned long pgd_pages;
758
759 pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
760 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
761 data->pgd_size = pgd_pages << data->pg_shift;
762 data->levels--;
763 }
764 }
765
766 /* VTCR */
767 reg = ARM_64_LPAE_S2_TCR_RES1 |
768 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
769 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
770 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
771
772 sl = ARM_LPAE_START_LVL(data);
773
774 switch (1 << data->pg_shift) {
775 case SZ_4K:
776 reg |= ARM_LPAE_TCR_TG0_4K;
777 sl++; /* SL0 format is different for 4K granule size */
778 break;
779 case SZ_16K:
780 reg |= ARM_LPAE_TCR_TG0_16K;
781 break;
782 case SZ_64K:
783 reg |= ARM_LPAE_TCR_TG0_64K;
784 break;
785 }
786
787 switch (cfg->oas) {
788 case 32:
789 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
790 break;
791 case 36:
792 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
793 break;
794 case 40:
795 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
796 break;
797 case 42:
798 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
799 break;
800 case 44:
801 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
802 break;
803 case 48:
804 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
805 break;
806 default:
807 goto out_free_data;
808 }
809
810 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
811 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
812 cfg->arm_lpae_s2_cfg.vtcr = reg;
813
814 /* Allocate pgd pages */
Robin Murphyf8d54962015-07-29 19:46:04 +0100815 data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
Will Deacone1d3c0f2014-11-14 17:18:23 +0000816 if (!data->pgd)
817 goto out_free_data;
818
Robin Murphy87a91b12015-07-29 19:46:09 +0100819 /* Ensure the empty pgd is visible before any actual TTBR write */
820 wmb();
Will Deacone1d3c0f2014-11-14 17:18:23 +0000821
822 /* VTTBR */
823 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
824 return &data->iop;
825
826out_free_data:
827 kfree(data);
828 return NULL;
829}
830
831static struct io_pgtable *
832arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
833{
834 struct io_pgtable *iop;
835
836 if (cfg->ias > 32 || cfg->oas > 40)
837 return NULL;
838
839 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
840 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
841 if (iop) {
842 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
843 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
844 }
845
846 return iop;
847}
848
849static struct io_pgtable *
850arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
851{
852 struct io_pgtable *iop;
853
854 if (cfg->ias > 40 || cfg->oas > 40)
855 return NULL;
856
857 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
858 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
859 if (iop)
860 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
861
862 return iop;
863}
864
865struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
866 .alloc = arm_64_lpae_alloc_pgtable_s1,
867 .free = arm_lpae_free_pgtable,
868};
869
870struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
871 .alloc = arm_64_lpae_alloc_pgtable_s2,
872 .free = arm_lpae_free_pgtable,
873};
874
875struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
876 .alloc = arm_32_lpae_alloc_pgtable_s1,
877 .free = arm_lpae_free_pgtable,
878};
879
880struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
881 .alloc = arm_32_lpae_alloc_pgtable_s2,
882 .free = arm_lpae_free_pgtable,
883};
Will Deaconfe4b9912014-11-17 23:31:12 +0000884
885#ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
886
887static struct io_pgtable_cfg *cfg_cookie;
888
889static void dummy_tlb_flush_all(void *cookie)
890{
891 WARN_ON(cookie != cfg_cookie);
892}
893
894static void dummy_tlb_add_flush(unsigned long iova, size_t size, bool leaf,
895 void *cookie)
896{
897 WARN_ON(cookie != cfg_cookie);
898 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
899}
900
901static void dummy_tlb_sync(void *cookie)
902{
903 WARN_ON(cookie != cfg_cookie);
904}
905
Will Deaconfe4b9912014-11-17 23:31:12 +0000906static struct iommu_gather_ops dummy_tlb_ops __initdata = {
907 .tlb_flush_all = dummy_tlb_flush_all,
908 .tlb_add_flush = dummy_tlb_add_flush,
909 .tlb_sync = dummy_tlb_sync,
Will Deaconfe4b9912014-11-17 23:31:12 +0000910};
911
912static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
913{
914 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
915 struct io_pgtable_cfg *cfg = &data->iop.cfg;
916
917 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
918 cfg->pgsize_bitmap, cfg->ias);
919 pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
920 data->levels, data->pgd_size, data->pg_shift,
921 data->bits_per_level, data->pgd);
922}
923
924#define __FAIL(ops, i) ({ \
925 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
926 arm_lpae_dump_ops(ops); \
927 selftest_running = false; \
928 -EFAULT; \
929})
930
931static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
932{
933 static const enum io_pgtable_fmt fmts[] = {
934 ARM_64_LPAE_S1,
935 ARM_64_LPAE_S2,
936 };
937
938 int i, j;
939 unsigned long iova;
940 size_t size;
941 struct io_pgtable_ops *ops;
942
943 selftest_running = true;
944
945 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
946 cfg_cookie = cfg;
947 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
948 if (!ops) {
949 pr_err("selftest: failed to allocate io pgtable ops\n");
950 return -ENOMEM;
951 }
952
953 /*
954 * Initial sanity checks.
955 * Empty page tables shouldn't provide any translations.
956 */
957 if (ops->iova_to_phys(ops, 42))
958 return __FAIL(ops, i);
959
960 if (ops->iova_to_phys(ops, SZ_1G + 42))
961 return __FAIL(ops, i);
962
963 if (ops->iova_to_phys(ops, SZ_2G + 42))
964 return __FAIL(ops, i);
965
966 /*
967 * Distinct mappings of different granule sizes.
968 */
969 iova = 0;
970 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
971 while (j != BITS_PER_LONG) {
972 size = 1UL << j;
973
974 if (ops->map(ops, iova, iova, size, IOMMU_READ |
975 IOMMU_WRITE |
976 IOMMU_NOEXEC |
977 IOMMU_CACHE))
978 return __FAIL(ops, i);
979
980 /* Overlapping mappings */
981 if (!ops->map(ops, iova, iova + size, size,
982 IOMMU_READ | IOMMU_NOEXEC))
983 return __FAIL(ops, i);
984
985 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
986 return __FAIL(ops, i);
987
988 iova += SZ_1G;
989 j++;
990 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
991 }
992
993 /* Partial unmap */
994 size = 1UL << __ffs(cfg->pgsize_bitmap);
995 if (ops->unmap(ops, SZ_1G + size, size) != size)
996 return __FAIL(ops, i);
997
998 /* Remap of partial unmap */
999 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1000 return __FAIL(ops, i);
1001
1002 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1003 return __FAIL(ops, i);
1004
1005 /* Full unmap */
1006 iova = 0;
1007 j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
1008 while (j != BITS_PER_LONG) {
1009 size = 1UL << j;
1010
1011 if (ops->unmap(ops, iova, size) != size)
1012 return __FAIL(ops, i);
1013
1014 if (ops->iova_to_phys(ops, iova + 42))
1015 return __FAIL(ops, i);
1016
1017 /* Remap full block */
1018 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1019 return __FAIL(ops, i);
1020
1021 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1022 return __FAIL(ops, i);
1023
1024 iova += SZ_1G;
1025 j++;
1026 j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
1027 }
1028
1029 free_io_pgtable_ops(ops);
1030 }
1031
1032 selftest_running = false;
1033 return 0;
1034}
1035
1036static int __init arm_lpae_do_selftests(void)
1037{
1038 static const unsigned long pgsize[] = {
1039 SZ_4K | SZ_2M | SZ_1G,
1040 SZ_16K | SZ_32M,
1041 SZ_64K | SZ_512M,
1042 };
1043
1044 static const unsigned int ias[] = {
1045 32, 36, 40, 42, 44, 48,
1046 };
1047
1048 int i, j, pass = 0, fail = 0;
1049 struct io_pgtable_cfg cfg = {
1050 .tlb = &dummy_tlb_ops,
1051 .oas = 48,
1052 };
1053
1054 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1055 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1056 cfg.pgsize_bitmap = pgsize[i];
1057 cfg.ias = ias[j];
1058 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1059 pgsize[i], ias[j]);
1060 if (arm_lpae_run_tests(&cfg))
1061 fail++;
1062 else
1063 pass++;
1064 }
1065 }
1066
1067 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1068 return fail ? -EFAULT : 0;
1069}
1070subsys_initcall(arm_lpae_do_selftests);
1071#endif