treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iommu / io-pgtable-arm.c
blobbdf47f74526879d1316f640e2308a0a3871931f1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * CPU-agnostic ARM page table allocator.
5 * Copyright (C) 2014 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
10 #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt
12 #include <linux/atomic.h>
13 #include <linux/bitops.h>
14 #include <linux/io-pgtable.h>
15 #include <linux/kernel.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/dma-mapping.h>
21 #include <asm/barrier.h>
23 #define ARM_LPAE_MAX_ADDR_BITS 52
24 #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16
25 #define ARM_LPAE_MAX_LEVELS 4
27 /* Struct accessors */
28 #define io_pgtable_to_data(x) \
29 container_of((x), struct arm_lpae_io_pgtable, iop)
31 #define io_pgtable_ops_to_data(x) \
32 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
35 * Calculate the right shift amount to get to the portion describing level l
36 * in a virtual address mapped by the pagetable in d.
38 #define ARM_LPAE_LVL_SHIFT(l,d) \
39 (((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) + \
40 ilog2(sizeof(arm_lpae_iopte)))
42 #define ARM_LPAE_GRANULE(d) \
43 (sizeof(arm_lpae_iopte) << (d)->bits_per_level)
44 #define ARM_LPAE_PGD_SIZE(d) \
45 (sizeof(arm_lpae_iopte) << (d)->pgd_bits)
48 * Calculate the index at level l used to map virtual address a using the
49 * pagetable in d.
51 #define ARM_LPAE_PGD_IDX(l,d) \
52 ((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
54 #define ARM_LPAE_LVL_IDX(a,l,d) \
55 (((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \
56 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
58 /* Calculate the block/page mapping size at level l for pagetable in d. */
59 #define ARM_LPAE_BLOCK_SIZE(l,d) (1ULL << ARM_LPAE_LVL_SHIFT(l,d))
61 /* Page table bits */
62 #define ARM_LPAE_PTE_TYPE_SHIFT 0
63 #define ARM_LPAE_PTE_TYPE_MASK 0x3
65 #define ARM_LPAE_PTE_TYPE_BLOCK 1
66 #define ARM_LPAE_PTE_TYPE_TABLE 3
67 #define ARM_LPAE_PTE_TYPE_PAGE 3
69 #define ARM_LPAE_PTE_ADDR_MASK GENMASK_ULL(47,12)
71 #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63)
72 #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53)
73 #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10)
74 #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8)
75 #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8)
76 #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8)
77 #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5)
78 #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0)
80 #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2)
81 /* Ignore the contiguous bit for block splitting */
82 #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52)
83 #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \
84 ARM_LPAE_PTE_ATTR_HI_MASK)
85 /* Software bit for solving coherency races */
86 #define ARM_LPAE_PTE_SW_SYNC (((arm_lpae_iopte)1) << 55)
88 /* Stage-1 PTE */
89 #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6)
90 #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6)
91 #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2
92 #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11)
94 /* Stage-2 PTE */
95 #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6)
96 #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6)
97 #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6)
98 #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2)
99 #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2)
100 #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2)
102 /* Register bits */
103 #define ARM_32_LPAE_TCR_EAE (1 << 31)
104 #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
106 #define ARM_LPAE_TCR_EPD1 (1 << 23)
108 #define ARM_LPAE_TCR_TG0_4K (0 << 14)
109 #define ARM_LPAE_TCR_TG0_64K (1 << 14)
110 #define ARM_LPAE_TCR_TG0_16K (2 << 14)
112 #define ARM_LPAE_TCR_SH0_SHIFT 12
113 #define ARM_LPAE_TCR_SH0_MASK 0x3
114 #define ARM_LPAE_TCR_SH_NS 0
115 #define ARM_LPAE_TCR_SH_OS 2
116 #define ARM_LPAE_TCR_SH_IS 3
118 #define ARM_LPAE_TCR_ORGN0_SHIFT 10
119 #define ARM_LPAE_TCR_IRGN0_SHIFT 8
120 #define ARM_LPAE_TCR_RGN_MASK 0x3
121 #define ARM_LPAE_TCR_RGN_NC 0
122 #define ARM_LPAE_TCR_RGN_WBWA 1
123 #define ARM_LPAE_TCR_RGN_WT 2
124 #define ARM_LPAE_TCR_RGN_WB 3
126 #define ARM_LPAE_TCR_SL0_SHIFT 6
127 #define ARM_LPAE_TCR_SL0_MASK 0x3
129 #define ARM_LPAE_TCR_T0SZ_SHIFT 0
130 #define ARM_LPAE_TCR_SZ_MASK 0xf
132 #define ARM_LPAE_TCR_PS_SHIFT 16
133 #define ARM_LPAE_TCR_PS_MASK 0x7
135 #define ARM_LPAE_TCR_IPS_SHIFT 32
136 #define ARM_LPAE_TCR_IPS_MASK 0x7
138 #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL
139 #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL
140 #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL
141 #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL
142 #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL
143 #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL
144 #define ARM_LPAE_TCR_PS_52_BIT 0x6ULL
146 #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3)
147 #define ARM_LPAE_MAIR_ATTR_MASK 0xff
148 #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04
149 #define ARM_LPAE_MAIR_ATTR_NC 0x44
150 #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA 0xf4
151 #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff
152 #define ARM_LPAE_MAIR_ATTR_IDX_NC 0
153 #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1
154 #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2
155 #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE 3
157 #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
158 #define ARM_MALI_LPAE_TTBR_READ_INNER BIT(2)
159 #define ARM_MALI_LPAE_TTBR_SHARE_OUTER BIT(4)
161 #define ARM_MALI_LPAE_MEMATTR_IMP_DEF 0x88ULL
162 #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
164 /* IOPTE accessors */
165 #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
167 #define iopte_type(pte,l) \
168 (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
170 #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK)
172 struct arm_lpae_io_pgtable {
173 struct io_pgtable iop;
175 int pgd_bits;
176 int start_level;
177 int bits_per_level;
179 void *pgd;
182 typedef u64 arm_lpae_iopte;
184 static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
185 enum io_pgtable_fmt fmt)
187 if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
188 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE;
190 return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK;
193 static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
194 struct arm_lpae_io_pgtable *data)
196 arm_lpae_iopte pte = paddr;
198 /* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
199 return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
202 static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
203 struct arm_lpae_io_pgtable *data)
205 u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
207 if (ARM_LPAE_GRANULE(data) < SZ_64K)
208 return paddr;
210 /* Rotate the packed high-order bits back to the top */
211 return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
214 static bool selftest_running = false;
216 static dma_addr_t __arm_lpae_dma_addr(void *pages)
218 return (dma_addr_t)virt_to_phys(pages);
221 static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
222 struct io_pgtable_cfg *cfg)
224 struct device *dev = cfg->iommu_dev;
225 int order = get_order(size);
226 struct page *p;
227 dma_addr_t dma;
228 void *pages;
230 VM_BUG_ON((gfp & __GFP_HIGHMEM));
231 p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
232 gfp | __GFP_ZERO, order);
233 if (!p)
234 return NULL;
236 pages = page_address(p);
237 if (!cfg->coherent_walk) {
238 dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
239 if (dma_mapping_error(dev, dma))
240 goto out_free;
242 * We depend on the IOMMU being able to work with any physical
243 * address directly, so if the DMA layer suggests otherwise by
244 * translating or truncating them, that bodes very badly...
246 if (dma != virt_to_phys(pages))
247 goto out_unmap;
250 return pages;
252 out_unmap:
253 dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
254 dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
255 out_free:
256 __free_pages(p, order);
257 return NULL;
260 static void __arm_lpae_free_pages(void *pages, size_t size,
261 struct io_pgtable_cfg *cfg)
263 if (!cfg->coherent_walk)
264 dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
265 size, DMA_TO_DEVICE);
266 free_pages((unsigned long)pages, get_order(size));
269 static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
270 struct io_pgtable_cfg *cfg)
272 dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
273 sizeof(*ptep), DMA_TO_DEVICE);
276 static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
277 struct io_pgtable_cfg *cfg)
279 *ptep = pte;
281 if (!cfg->coherent_walk)
282 __arm_lpae_sync_pte(ptep, cfg);
285 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
286 struct iommu_iotlb_gather *gather,
287 unsigned long iova, size_t size, int lvl,
288 arm_lpae_iopte *ptep);
290 static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
291 phys_addr_t paddr, arm_lpae_iopte prot,
292 int lvl, arm_lpae_iopte *ptep)
294 arm_lpae_iopte pte = prot;
296 if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
297 pte |= ARM_LPAE_PTE_NS;
299 if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
300 pte |= ARM_LPAE_PTE_TYPE_PAGE;
301 else
302 pte |= ARM_LPAE_PTE_TYPE_BLOCK;
304 if (data->iop.fmt != ARM_MALI_LPAE)
305 pte |= ARM_LPAE_PTE_AF;
306 pte |= ARM_LPAE_PTE_SH_IS;
307 pte |= paddr_to_iopte(paddr, data);
309 __arm_lpae_set_pte(ptep, pte, &data->iop.cfg);
312 static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
313 unsigned long iova, phys_addr_t paddr,
314 arm_lpae_iopte prot, int lvl,
315 arm_lpae_iopte *ptep)
317 arm_lpae_iopte pte = *ptep;
319 if (iopte_leaf(pte, lvl, data->iop.fmt)) {
320 /* We require an unmap first */
321 WARN_ON(!selftest_running);
322 return -EEXIST;
323 } else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
325 * We need to unmap and free the old table before
326 * overwriting it with a block entry.
328 arm_lpae_iopte *tblp;
329 size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
331 tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
332 if (__arm_lpae_unmap(data, NULL, iova, sz, lvl, tblp) != sz) {
333 WARN_ON(1);
334 return -EINVAL;
338 __arm_lpae_init_pte(data, paddr, prot, lvl, ptep);
339 return 0;
342 static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
343 arm_lpae_iopte *ptep,
344 arm_lpae_iopte curr,
345 struct io_pgtable_cfg *cfg)
347 arm_lpae_iopte old, new;
349 new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
350 if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
351 new |= ARM_LPAE_PTE_NSTABLE;
354 * Ensure the table itself is visible before its PTE can be.
355 * Whilst we could get away with cmpxchg64_release below, this
356 * doesn't have any ordering semantics when !CONFIG_SMP.
358 dma_wmb();
360 old = cmpxchg64_relaxed(ptep, curr, new);
362 if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
363 return old;
365 /* Even if it's not ours, there's no point waiting; just kick it */
366 __arm_lpae_sync_pte(ptep, cfg);
367 if (old == curr)
368 WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
370 return old;
373 static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
374 phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
375 int lvl, arm_lpae_iopte *ptep)
377 arm_lpae_iopte *cptep, pte;
378 size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
379 size_t tblsz = ARM_LPAE_GRANULE(data);
380 struct io_pgtable_cfg *cfg = &data->iop.cfg;
382 /* Find our entry at the current level */
383 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
385 /* If we can install a leaf entry at this level, then do so */
386 if (size == block_size)
387 return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
389 /* We can't allocate tables at the final level */
390 if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
391 return -EINVAL;
393 /* Grab a pointer to the next level */
394 pte = READ_ONCE(*ptep);
395 if (!pte) {
396 cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg);
397 if (!cptep)
398 return -ENOMEM;
400 pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
401 if (pte)
402 __arm_lpae_free_pages(cptep, tblsz, cfg);
403 } else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
404 __arm_lpae_sync_pte(ptep, cfg);
407 if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
408 cptep = iopte_deref(pte, data);
409 } else if (pte) {
410 /* We require an unmap first */
411 WARN_ON(!selftest_running);
412 return -EEXIST;
415 /* Rinse, repeat */
416 return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
419 static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
420 int prot)
422 arm_lpae_iopte pte;
424 if (data->iop.fmt == ARM_64_LPAE_S1 ||
425 data->iop.fmt == ARM_32_LPAE_S1) {
426 pte = ARM_LPAE_PTE_nG;
427 if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
428 pte |= ARM_LPAE_PTE_AP_RDONLY;
429 if (!(prot & IOMMU_PRIV))
430 pte |= ARM_LPAE_PTE_AP_UNPRIV;
431 } else {
432 pte = ARM_LPAE_PTE_HAP_FAULT;
433 if (prot & IOMMU_READ)
434 pte |= ARM_LPAE_PTE_HAP_READ;
435 if (prot & IOMMU_WRITE)
436 pte |= ARM_LPAE_PTE_HAP_WRITE;
440 * Note that this logic is structured to accommodate Mali LPAE
441 * having stage-1-like attributes but stage-2-like permissions.
443 if (data->iop.fmt == ARM_64_LPAE_S2 ||
444 data->iop.fmt == ARM_32_LPAE_S2) {
445 if (prot & IOMMU_MMIO)
446 pte |= ARM_LPAE_PTE_MEMATTR_DEV;
447 else if (prot & IOMMU_CACHE)
448 pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
449 else
450 pte |= ARM_LPAE_PTE_MEMATTR_NC;
451 } else {
452 if (prot & IOMMU_MMIO)
453 pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
454 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
455 else if (prot & IOMMU_CACHE)
456 pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
457 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
458 else if (prot & IOMMU_SYS_CACHE_ONLY)
459 pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE
460 << ARM_LPAE_PTE_ATTRINDX_SHIFT);
463 if (prot & IOMMU_NOEXEC)
464 pte |= ARM_LPAE_PTE_XN;
466 return pte;
469 static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
470 phys_addr_t paddr, size_t size, int iommu_prot)
472 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
473 struct io_pgtable_cfg *cfg = &data->iop.cfg;
474 arm_lpae_iopte *ptep = data->pgd;
475 int ret, lvl = data->start_level;
476 arm_lpae_iopte prot;
478 /* If no access, then nothing to do */
479 if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
480 return 0;
482 if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
483 return -EINVAL;
485 if (WARN_ON(iova >> data->iop.cfg.ias || paddr >> data->iop.cfg.oas))
486 return -ERANGE;
488 prot = arm_lpae_prot_to_pte(data, iommu_prot);
489 ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
491 * Synchronise all PTE updates for the new mapping before there's
492 * a chance for anything to kick off a table walk for the new iova.
494 wmb();
496 return ret;
499 static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
500 arm_lpae_iopte *ptep)
502 arm_lpae_iopte *start, *end;
503 unsigned long table_size;
505 if (lvl == data->start_level)
506 table_size = ARM_LPAE_PGD_SIZE(data);
507 else
508 table_size = ARM_LPAE_GRANULE(data);
510 start = ptep;
512 /* Only leaf entries at the last level */
513 if (lvl == ARM_LPAE_MAX_LEVELS - 1)
514 end = ptep;
515 else
516 end = (void *)ptep + table_size;
518 while (ptep != end) {
519 arm_lpae_iopte pte = *ptep++;
521 if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
522 continue;
524 __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
527 __arm_lpae_free_pages(start, table_size, &data->iop.cfg);
530 static void arm_lpae_free_pgtable(struct io_pgtable *iop)
532 struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
534 __arm_lpae_free_pgtable(data, data->start_level, data->pgd);
535 kfree(data);
538 static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
539 struct iommu_iotlb_gather *gather,
540 unsigned long iova, size_t size,
541 arm_lpae_iopte blk_pte, int lvl,
542 arm_lpae_iopte *ptep)
544 struct io_pgtable_cfg *cfg = &data->iop.cfg;
545 arm_lpae_iopte pte, *tablep;
546 phys_addr_t blk_paddr;
547 size_t tablesz = ARM_LPAE_GRANULE(data);
548 size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
549 int i, unmap_idx = -1;
551 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
552 return 0;
554 tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
555 if (!tablep)
556 return 0; /* Bytes unmapped */
558 if (size == split_sz)
559 unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data);
561 blk_paddr = iopte_to_paddr(blk_pte, data);
562 pte = iopte_prot(blk_pte);
564 for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) {
565 /* Unmap! */
566 if (i == unmap_idx)
567 continue;
569 __arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]);
572 pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg);
573 if (pte != blk_pte) {
574 __arm_lpae_free_pages(tablep, tablesz, cfg);
576 * We may race against someone unmapping another part of this
577 * block, but anything else is invalid. We can't misinterpret
578 * a page entry here since we're never at the last level.
580 if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE)
581 return 0;
583 tablep = iopte_deref(pte, data);
584 } else if (unmap_idx >= 0) {
585 io_pgtable_tlb_add_page(&data->iop, gather, iova, size);
586 return size;
589 return __arm_lpae_unmap(data, gather, iova, size, lvl, tablep);
592 static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
593 struct iommu_iotlb_gather *gather,
594 unsigned long iova, size_t size, int lvl,
595 arm_lpae_iopte *ptep)
597 arm_lpae_iopte pte;
598 struct io_pgtable *iop = &data->iop;
600 /* Something went horribly wrong and we ran out of page table */
601 if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
602 return 0;
604 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
605 pte = READ_ONCE(*ptep);
606 if (WARN_ON(!pte))
607 return 0;
609 /* If the size matches this level, we're in the right place */
610 if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
611 __arm_lpae_set_pte(ptep, 0, &iop->cfg);
613 if (!iopte_leaf(pte, lvl, iop->fmt)) {
614 /* Also flush any partial walks */
615 io_pgtable_tlb_flush_walk(iop, iova, size,
616 ARM_LPAE_GRANULE(data));
617 ptep = iopte_deref(pte, data);
618 __arm_lpae_free_pgtable(data, lvl + 1, ptep);
619 } else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
621 * Order the PTE update against queueing the IOVA, to
622 * guarantee that a flush callback from a different CPU
623 * has observed it before the TLBIALL can be issued.
625 smp_wmb();
626 } else {
627 io_pgtable_tlb_add_page(iop, gather, iova, size);
630 return size;
631 } else if (iopte_leaf(pte, lvl, iop->fmt)) {
633 * Insert a table at the next level to map the old region,
634 * minus the part we want to unmap
636 return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
637 lvl + 1, ptep);
640 /* Keep on walkin' */
641 ptep = iopte_deref(pte, data);
642 return __arm_lpae_unmap(data, gather, iova, size, lvl + 1, ptep);
645 static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
646 size_t size, struct iommu_iotlb_gather *gather)
648 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
649 struct io_pgtable_cfg *cfg = &data->iop.cfg;
650 arm_lpae_iopte *ptep = data->pgd;
652 if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
653 return 0;
655 if (WARN_ON(iova >> data->iop.cfg.ias))
656 return 0;
658 return __arm_lpae_unmap(data, gather, iova, size, data->start_level, ptep);
661 static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
662 unsigned long iova)
664 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
665 arm_lpae_iopte pte, *ptep = data->pgd;
666 int lvl = data->start_level;
668 do {
669 /* Valid IOPTE pointer? */
670 if (!ptep)
671 return 0;
673 /* Grab the IOPTE we're interested in */
674 ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
675 pte = READ_ONCE(*ptep);
677 /* Valid entry? */
678 if (!pte)
679 return 0;
681 /* Leaf entry? */
682 if (iopte_leaf(pte, lvl, data->iop.fmt))
683 goto found_translation;
685 /* Take it to the next level */
686 ptep = iopte_deref(pte, data);
687 } while (++lvl < ARM_LPAE_MAX_LEVELS);
689 /* Ran out of page tables to walk */
690 return 0;
692 found_translation:
693 iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
694 return iopte_to_paddr(pte, data) | iova;
697 static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
699 unsigned long granule, page_sizes;
700 unsigned int max_addr_bits = 48;
703 * We need to restrict the supported page sizes to match the
704 * translation regime for a particular granule. Aim to match
705 * the CPU page size if possible, otherwise prefer smaller sizes.
706 * While we're at it, restrict the block sizes to match the
707 * chosen granule.
709 if (cfg->pgsize_bitmap & PAGE_SIZE)
710 granule = PAGE_SIZE;
711 else if (cfg->pgsize_bitmap & ~PAGE_MASK)
712 granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
713 else if (cfg->pgsize_bitmap & PAGE_MASK)
714 granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
715 else
716 granule = 0;
718 switch (granule) {
719 case SZ_4K:
720 page_sizes = (SZ_4K | SZ_2M | SZ_1G);
721 break;
722 case SZ_16K:
723 page_sizes = (SZ_16K | SZ_32M);
724 break;
725 case SZ_64K:
726 max_addr_bits = 52;
727 page_sizes = (SZ_64K | SZ_512M);
728 if (cfg->oas > 48)
729 page_sizes |= 1ULL << 42; /* 4TB */
730 break;
731 default:
732 page_sizes = 0;
735 cfg->pgsize_bitmap &= page_sizes;
736 cfg->ias = min(cfg->ias, max_addr_bits);
737 cfg->oas = min(cfg->oas, max_addr_bits);
740 static struct arm_lpae_io_pgtable *
741 arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
743 struct arm_lpae_io_pgtable *data;
744 int levels, va_bits, pg_shift;
746 arm_lpae_restrict_pgsizes(cfg);
748 if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
749 return NULL;
751 if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
752 return NULL;
754 if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
755 return NULL;
757 if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
758 dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
759 return NULL;
762 data = kmalloc(sizeof(*data), GFP_KERNEL);
763 if (!data)
764 return NULL;
766 pg_shift = __ffs(cfg->pgsize_bitmap);
767 data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
769 va_bits = cfg->ias - pg_shift;
770 levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
771 data->start_level = ARM_LPAE_MAX_LEVELS - levels;
773 /* Calculate the actual size of our pgd (without concatenation) */
774 data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
776 data->iop.ops = (struct io_pgtable_ops) {
777 .map = arm_lpae_map,
778 .unmap = arm_lpae_unmap,
779 .iova_to_phys = arm_lpae_iova_to_phys,
782 return data;
785 static struct io_pgtable *
786 arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
788 u64 reg;
789 struct arm_lpae_io_pgtable *data;
791 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
792 IO_PGTABLE_QUIRK_NON_STRICT))
793 return NULL;
795 data = arm_lpae_alloc_pgtable(cfg);
796 if (!data)
797 return NULL;
799 /* TCR */
800 if (cfg->coherent_walk) {
801 reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
802 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
803 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
804 } else {
805 reg = (ARM_LPAE_TCR_SH_OS << ARM_LPAE_TCR_SH0_SHIFT) |
806 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_IRGN0_SHIFT) |
807 (ARM_LPAE_TCR_RGN_NC << ARM_LPAE_TCR_ORGN0_SHIFT);
810 switch (ARM_LPAE_GRANULE(data)) {
811 case SZ_4K:
812 reg |= ARM_LPAE_TCR_TG0_4K;
813 break;
814 case SZ_16K:
815 reg |= ARM_LPAE_TCR_TG0_16K;
816 break;
817 case SZ_64K:
818 reg |= ARM_LPAE_TCR_TG0_64K;
819 break;
822 switch (cfg->oas) {
823 case 32:
824 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
825 break;
826 case 36:
827 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
828 break;
829 case 40:
830 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
831 break;
832 case 42:
833 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
834 break;
835 case 44:
836 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
837 break;
838 case 48:
839 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
840 break;
841 case 52:
842 reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT);
843 break;
844 default:
845 goto out_free_data;
848 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
850 /* Disable speculative walks through TTBR1 */
851 reg |= ARM_LPAE_TCR_EPD1;
852 cfg->arm_lpae_s1_cfg.tcr = reg;
854 /* MAIRs */
855 reg = (ARM_LPAE_MAIR_ATTR_NC
856 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
857 (ARM_LPAE_MAIR_ATTR_WBRWA
858 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
859 (ARM_LPAE_MAIR_ATTR_DEVICE
860 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
861 (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
862 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
864 cfg->arm_lpae_s1_cfg.mair = reg;
866 /* Looking good; allocate a pgd */
867 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
868 GFP_KERNEL, cfg);
869 if (!data->pgd)
870 goto out_free_data;
872 /* Ensure the empty pgd is visible before any actual TTBR write */
873 wmb();
875 /* TTBRs */
876 cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
877 cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
878 return &data->iop;
880 out_free_data:
881 kfree(data);
882 return NULL;
885 static struct io_pgtable *
886 arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
888 u64 reg, sl;
889 struct arm_lpae_io_pgtable *data;
891 /* The NS quirk doesn't apply at stage 2 */
892 if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT))
893 return NULL;
895 data = arm_lpae_alloc_pgtable(cfg);
896 if (!data)
897 return NULL;
900 * Concatenate PGDs at level 1 if possible in order to reduce
901 * the depth of the stage-2 walk.
903 if (data->start_level == 0) {
904 unsigned long pgd_pages;
906 pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
907 if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
908 data->pgd_bits += data->bits_per_level;
909 data->start_level++;
913 /* VTCR */
914 reg = ARM_64_LPAE_S2_TCR_RES1 |
915 (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
916 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
917 (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
919 sl = data->start_level;
921 switch (ARM_LPAE_GRANULE(data)) {
922 case SZ_4K:
923 reg |= ARM_LPAE_TCR_TG0_4K;
924 sl++; /* SL0 format is different for 4K granule size */
925 break;
926 case SZ_16K:
927 reg |= ARM_LPAE_TCR_TG0_16K;
928 break;
929 case SZ_64K:
930 reg |= ARM_LPAE_TCR_TG0_64K;
931 break;
934 switch (cfg->oas) {
935 case 32:
936 reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
937 break;
938 case 36:
939 reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
940 break;
941 case 40:
942 reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
943 break;
944 case 42:
945 reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
946 break;
947 case 44:
948 reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
949 break;
950 case 48:
951 reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
952 break;
953 case 52:
954 reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT);
955 break;
956 default:
957 goto out_free_data;
960 reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
961 reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
962 cfg->arm_lpae_s2_cfg.vtcr = reg;
964 /* Allocate pgd pages */
965 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
966 GFP_KERNEL, cfg);
967 if (!data->pgd)
968 goto out_free_data;
970 /* Ensure the empty pgd is visible before any actual TTBR write */
971 wmb();
973 /* VTTBR */
974 cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
975 return &data->iop;
977 out_free_data:
978 kfree(data);
979 return NULL;
982 static struct io_pgtable *
983 arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
985 struct io_pgtable *iop;
987 if (cfg->ias > 32 || cfg->oas > 40)
988 return NULL;
990 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
991 iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
992 if (iop) {
993 cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
994 cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
997 return iop;
1000 static struct io_pgtable *
1001 arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1003 struct io_pgtable *iop;
1005 if (cfg->ias > 40 || cfg->oas > 40)
1006 return NULL;
1008 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1009 iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1010 if (iop)
1011 cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1013 return iop;
1016 static struct io_pgtable *
1017 arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1019 struct arm_lpae_io_pgtable *data;
1021 /* No quirks for Mali (hopefully) */
1022 if (cfg->quirks)
1023 return NULL;
1025 if (cfg->ias > 48 || cfg->oas > 40)
1026 return NULL;
1028 cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1030 data = arm_lpae_alloc_pgtable(cfg);
1031 if (!data)
1032 return NULL;
1034 /* Mali seems to need a full 4-level table regardless of IAS */
1035 if (data->start_level > 0) {
1036 data->start_level = 0;
1037 data->pgd_bits = 0;
1040 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
1041 * best we can do is mimic the out-of-tree driver and hope that the
1042 * "implementation-defined caching policy" is good enough. Similarly,
1043 * we'll use it for the sake of a valid attribute for our 'device'
1044 * index, although callers should never request that in practice.
1046 cfg->arm_mali_lpae_cfg.memattr =
1047 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1048 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
1049 (ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
1050 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
1051 (ARM_MALI_LPAE_MEMATTR_IMP_DEF
1052 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
1054 data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1055 cfg);
1056 if (!data->pgd)
1057 goto out_free_data;
1059 /* Ensure the empty pgd is visible before TRANSTAB can be written */
1060 wmb();
1062 cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
1063 ARM_MALI_LPAE_TTBR_READ_INNER |
1064 ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
1065 return &data->iop;
1067 out_free_data:
1068 kfree(data);
1069 return NULL;
1072 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1073 .alloc = arm_64_lpae_alloc_pgtable_s1,
1074 .free = arm_lpae_free_pgtable,
1077 struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1078 .alloc = arm_64_lpae_alloc_pgtable_s2,
1079 .free = arm_lpae_free_pgtable,
1082 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1083 .alloc = arm_32_lpae_alloc_pgtable_s1,
1084 .free = arm_lpae_free_pgtable,
1087 struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1088 .alloc = arm_32_lpae_alloc_pgtable_s2,
1089 .free = arm_lpae_free_pgtable,
1092 struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1093 .alloc = arm_mali_lpae_alloc_pgtable,
1094 .free = arm_lpae_free_pgtable,
1097 #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1099 static struct io_pgtable_cfg *cfg_cookie __initdata;
1101 static void __init dummy_tlb_flush_all(void *cookie)
1103 WARN_ON(cookie != cfg_cookie);
1106 static void __init dummy_tlb_flush(unsigned long iova, size_t size,
1107 size_t granule, void *cookie)
1109 WARN_ON(cookie != cfg_cookie);
1110 WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1113 static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
1114 unsigned long iova, size_t granule,
1115 void *cookie)
1117 dummy_tlb_flush(iova, granule, granule, cookie);
1120 static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
1121 .tlb_flush_all = dummy_tlb_flush_all,
1122 .tlb_flush_walk = dummy_tlb_flush,
1123 .tlb_flush_leaf = dummy_tlb_flush,
1124 .tlb_add_page = dummy_tlb_add_page,
1127 static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1129 struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1130 struct io_pgtable_cfg *cfg = &data->iop.cfg;
1132 pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1133 cfg->pgsize_bitmap, cfg->ias);
1134 pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
1135 ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
1136 ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
1139 #define __FAIL(ops, i) ({ \
1140 WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \
1141 arm_lpae_dump_ops(ops); \
1142 selftest_running = false; \
1143 -EFAULT; \
1146 static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1148 static const enum io_pgtable_fmt fmts[] __initconst = {
1149 ARM_64_LPAE_S1,
1150 ARM_64_LPAE_S2,
1153 int i, j;
1154 unsigned long iova;
1155 size_t size;
1156 struct io_pgtable_ops *ops;
1158 selftest_running = true;
1160 for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1161 cfg_cookie = cfg;
1162 ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1163 if (!ops) {
1164 pr_err("selftest: failed to allocate io pgtable ops\n");
1165 return -ENOMEM;
1169 * Initial sanity checks.
1170 * Empty page tables shouldn't provide any translations.
1172 if (ops->iova_to_phys(ops, 42))
1173 return __FAIL(ops, i);
1175 if (ops->iova_to_phys(ops, SZ_1G + 42))
1176 return __FAIL(ops, i);
1178 if (ops->iova_to_phys(ops, SZ_2G + 42))
1179 return __FAIL(ops, i);
1182 * Distinct mappings of different granule sizes.
1184 iova = 0;
1185 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1186 size = 1UL << j;
1188 if (ops->map(ops, iova, iova, size, IOMMU_READ |
1189 IOMMU_WRITE |
1190 IOMMU_NOEXEC |
1191 IOMMU_CACHE))
1192 return __FAIL(ops, i);
1194 /* Overlapping mappings */
1195 if (!ops->map(ops, iova, iova + size, size,
1196 IOMMU_READ | IOMMU_NOEXEC))
1197 return __FAIL(ops, i);
1199 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1200 return __FAIL(ops, i);
1202 iova += SZ_1G;
1205 /* Partial unmap */
1206 size = 1UL << __ffs(cfg->pgsize_bitmap);
1207 if (ops->unmap(ops, SZ_1G + size, size, NULL) != size)
1208 return __FAIL(ops, i);
1210 /* Remap of partial unmap */
1211 if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1212 return __FAIL(ops, i);
1214 if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1215 return __FAIL(ops, i);
1217 /* Full unmap */
1218 iova = 0;
1219 for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1220 size = 1UL << j;
1222 if (ops->unmap(ops, iova, size, NULL) != size)
1223 return __FAIL(ops, i);
1225 if (ops->iova_to_phys(ops, iova + 42))
1226 return __FAIL(ops, i);
1228 /* Remap full block */
1229 if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1230 return __FAIL(ops, i);
1232 if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1233 return __FAIL(ops, i);
1235 iova += SZ_1G;
1238 free_io_pgtable_ops(ops);
1241 selftest_running = false;
1242 return 0;
1245 static int __init arm_lpae_do_selftests(void)
1247 static const unsigned long pgsize[] __initconst = {
1248 SZ_4K | SZ_2M | SZ_1G,
1249 SZ_16K | SZ_32M,
1250 SZ_64K | SZ_512M,
1253 static const unsigned int ias[] __initconst = {
1254 32, 36, 40, 42, 44, 48,
1257 int i, j, pass = 0, fail = 0;
1258 struct io_pgtable_cfg cfg = {
1259 .tlb = &dummy_tlb_ops,
1260 .oas = 48,
1261 .coherent_walk = true,
1264 for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1265 for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1266 cfg.pgsize_bitmap = pgsize[i];
1267 cfg.ias = ias[j];
1268 pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1269 pgsize[i], ias[j]);
1270 if (arm_lpae_run_tests(&cfg))
1271 fail++;
1272 else
1273 pass++;
1277 pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1278 return fail ? -EFAULT : 0;
1280 subsys_initcall(arm_lpae_do_selftests);
1281 #endif