xref: /openbmc/linux/drivers/iommu/io-pgtable-arm.c (revision 1fe27be5ffec20637a8fe17ec7c1e88f3aaf62f0)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e1d3c0fdSWill Deacon /*
3e1d3c0fdSWill Deacon  * CPU-agnostic ARM page table allocator.
4e1d3c0fdSWill Deacon  *
5e1d3c0fdSWill Deacon  * Copyright (C) 2014 ARM Limited
6e1d3c0fdSWill Deacon  *
7e1d3c0fdSWill Deacon  * Author: Will Deacon <will.deacon@arm.com>
8e1d3c0fdSWill Deacon  */
9e1d3c0fdSWill Deacon 
10e1d3c0fdSWill Deacon #define pr_fmt(fmt)	"arm-lpae io-pgtable: " fmt
11e1d3c0fdSWill Deacon 
122c3d273eSRobin Murphy #include <linux/atomic.h>
136c89928fSRobin Murphy #include <linux/bitops.h>
14b77cf11fSRob Herring #include <linux/io-pgtable.h>
15e1d3c0fdSWill Deacon #include <linux/kernel.h>
16e1d3c0fdSWill Deacon #include <linux/sizes.h>
17e1d3c0fdSWill Deacon #include <linux/slab.h>
18e1d3c0fdSWill Deacon #include <linux/types.h>
198f6aff98SLada Trimasova #include <linux/dma-mapping.h>
20e1d3c0fdSWill Deacon 
2187a91b15SRobin Murphy #include <asm/barrier.h>
2287a91b15SRobin Murphy 
237cef39ddSJean-Philippe Brucker #include "io-pgtable-arm.h"
247cef39ddSJean-Philippe Brucker 
256c89928fSRobin Murphy #define ARM_LPAE_MAX_ADDR_BITS		52
26e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES	16
27e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS		4
28e1d3c0fdSWill Deacon 
29e1d3c0fdSWill Deacon /* Struct accessors */
30e1d3c0fdSWill Deacon #define io_pgtable_to_data(x)						\
31e1d3c0fdSWill Deacon 	container_of((x), struct arm_lpae_io_pgtable, iop)
32e1d3c0fdSWill Deacon 
33e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x)					\
34e1d3c0fdSWill Deacon 	io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
35e1d3c0fdSWill Deacon 
36e1d3c0fdSWill Deacon /*
37e1d3c0fdSWill Deacon  * Calculate the right shift amount to get to the portion describing level l
38e1d3c0fdSWill Deacon  * in a virtual address mapped by the pagetable in d.
39e1d3c0fdSWill Deacon  */
40e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d)						\
415fb190b0SRobin Murphy 	(((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level) +		\
425fb190b0SRobin Murphy 	ilog2(sizeof(arm_lpae_iopte)))
43e1d3c0fdSWill Deacon 
445fb190b0SRobin Murphy #define ARM_LPAE_GRANULE(d)						\
455fb190b0SRobin Murphy 	(sizeof(arm_lpae_iopte) << (d)->bits_per_level)
46c79278c1SRobin Murphy #define ARM_LPAE_PGD_SIZE(d)						\
47c79278c1SRobin Murphy 	(sizeof(arm_lpae_iopte) << (d)->pgd_bits)
48e1d3c0fdSWill Deacon 
49*1fe27be5SIsaac J. Manjarres #define ARM_LPAE_PTES_PER_TABLE(d)					\
50*1fe27be5SIsaac J. Manjarres 	(ARM_LPAE_GRANULE(d) >> ilog2(sizeof(arm_lpae_iopte)))
51*1fe27be5SIsaac J. Manjarres 
52e1d3c0fdSWill Deacon /*
53e1d3c0fdSWill Deacon  * Calculate the index at level l used to map virtual address a using the
54e1d3c0fdSWill Deacon  * pagetable in d.
55e1d3c0fdSWill Deacon  */
56e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d)						\
57c79278c1SRobin Murphy 	((l) == (d)->start_level ? (d)->pgd_bits - (d)->bits_per_level : 0)
58e1d3c0fdSWill Deacon 
59e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d)						\
60367bd978SWill Deacon 	(((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) &			\
61e1d3c0fdSWill Deacon 	 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
62e1d3c0fdSWill Deacon 
63e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */
645fb190b0SRobin Murphy #define ARM_LPAE_BLOCK_SIZE(l,d)	(1ULL << ARM_LPAE_LVL_SHIFT(l,d))
65e1d3c0fdSWill Deacon 
66e1d3c0fdSWill Deacon /* Page table bits */
67e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT		0
68e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK		0x3
69e1d3c0fdSWill Deacon 
70e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK		1
71e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE		3
72e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE		3
73e1d3c0fdSWill Deacon 
746c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK		GENMASK_ULL(47,12)
756c89928fSRobin Murphy 
76c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE		(((arm_lpae_iopte)1) << 63)
77e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN			(((arm_lpae_iopte)3) << 53)
78e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF			(((arm_lpae_iopte)1) << 10)
79e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS		(((arm_lpae_iopte)0) << 8)
80e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS		(((arm_lpae_iopte)2) << 8)
81e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS		(((arm_lpae_iopte)3) << 8)
82c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS			(((arm_lpae_iopte)1) << 5)
83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID		(((arm_lpae_iopte)1) << 0)
84e1d3c0fdSWill Deacon 
85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK	(((arm_lpae_iopte)0x3ff) << 2)
86e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */
87e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK	(((arm_lpae_iopte)6) << 52)
88e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK		(ARM_LPAE_PTE_ATTR_LO_MASK |	\
89e1d3c0fdSWill Deacon 					 ARM_LPAE_PTE_ATTR_HI_MASK)
902c3d273eSRobin Murphy /* Software bit for solving coherency races */
912c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC		(((arm_lpae_iopte)1) << 55)
92e1d3c0fdSWill Deacon 
93e1d3c0fdSWill Deacon /* Stage-1 PTE */
94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV		(((arm_lpae_iopte)1) << 6)
95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY		(((arm_lpae_iopte)2) << 6)
96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT	2
97e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG			(((arm_lpae_iopte)1) << 11)
98e1d3c0fdSWill Deacon 
99e1d3c0fdSWill Deacon /* Stage-2 PTE */
100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT		(((arm_lpae_iopte)0) << 6)
101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ		(((arm_lpae_iopte)1) << 6)
102e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE		(((arm_lpae_iopte)2) << 6)
103e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB	(((arm_lpae_iopte)0xf) << 2)
104e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC		(((arm_lpae_iopte)0x5) << 2)
105e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV	(((arm_lpae_iopte)0x1) << 2)
106e1d3c0fdSWill Deacon 
107e1d3c0fdSWill Deacon /* Register bits */
108fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_SL0_MASK		0x3
109e1d3c0fdSWill Deacon 
110e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT		0
111e1d3c0fdSWill Deacon 
112fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_SHIFT		16
113fb485eb1SRobin Murphy #define ARM_LPAE_VTCR_PS_MASK		0x7
114e1d3c0fdSWill Deacon 
115e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n)	((n) << 3)
116e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK		0xff
117e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE	0x04
118e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC		0x44
11990ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA	0xf4
120e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA	0xff
121e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC	0
122e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE	1
123e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV	2
12490ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE	3
125e1d3c0fdSWill Deacon 
126d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
127d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER	BIT(2)
128d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER	BIT(4)
129d08d42deSRob Herring 
13052f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_IMP_DEF	0x88ULL
13152f325f4SRobin Murphy #define ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC 0x8DULL
13252f325f4SRobin Murphy 
133e1d3c0fdSWill Deacon /* IOPTE accessors */
1346c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
135e1d3c0fdSWill Deacon 
136f37eb484SKunkun Jiang #define iopte_type(pte)					\
137e1d3c0fdSWill Deacon 	(((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
138e1d3c0fdSWill Deacon 
139e1d3c0fdSWill Deacon #define iopte_prot(pte)	((pte) & ARM_LPAE_PTE_ATTR_MASK)
140e1d3c0fdSWill Deacon 
141e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable {
142e1d3c0fdSWill Deacon 	struct io_pgtable	iop;
143e1d3c0fdSWill Deacon 
144c79278c1SRobin Murphy 	int			pgd_bits;
145594ab90fSRobin Murphy 	int			start_level;
1465fb190b0SRobin Murphy 	int			bits_per_level;
147e1d3c0fdSWill Deacon 
148e1d3c0fdSWill Deacon 	void			*pgd;
149e1d3c0fdSWill Deacon };
150e1d3c0fdSWill Deacon 
151e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte;
152e1d3c0fdSWill Deacon 
153d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
154d08d42deSRob Herring 			      enum io_pgtable_fmt fmt)
155d08d42deSRob Herring {
156d08d42deSRob Herring 	if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
157f37eb484SKunkun Jiang 		return iopte_type(pte) == ARM_LPAE_PTE_TYPE_PAGE;
158d08d42deSRob Herring 
159f37eb484SKunkun Jiang 	return iopte_type(pte) == ARM_LPAE_PTE_TYPE_BLOCK;
160d08d42deSRob Herring }
161d08d42deSRob Herring 
1626c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
1636c89928fSRobin Murphy 				     struct arm_lpae_io_pgtable *data)
1646c89928fSRobin Murphy {
1656c89928fSRobin Murphy 	arm_lpae_iopte pte = paddr;
1666c89928fSRobin Murphy 
1676c89928fSRobin Murphy 	/* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
1686c89928fSRobin Murphy 	return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
1696c89928fSRobin Murphy }
1706c89928fSRobin Murphy 
1716c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
1726c89928fSRobin Murphy 				  struct arm_lpae_io_pgtable *data)
1736c89928fSRobin Murphy {
17478688059SRobin Murphy 	u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
1756c89928fSRobin Murphy 
1765fb190b0SRobin Murphy 	if (ARM_LPAE_GRANULE(data) < SZ_64K)
1776c89928fSRobin Murphy 		return paddr;
1786c89928fSRobin Murphy 
1796c89928fSRobin Murphy 	/* Rotate the packed high-order bits back to the top */
1806c89928fSRobin Murphy 	return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
1816c89928fSRobin Murphy }
1826c89928fSRobin Murphy 
183fe4b991dSWill Deacon static bool selftest_running = false;
184fe4b991dSWill Deacon 
185ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages)
186f8d54961SRobin Murphy {
187ffcb6d16SRobin Murphy 	return (dma_addr_t)virt_to_phys(pages);
188f8d54961SRobin Murphy }
189f8d54961SRobin Murphy 
190f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
191f8d54961SRobin Murphy 				    struct io_pgtable_cfg *cfg)
192f8d54961SRobin Murphy {
193f8d54961SRobin Murphy 	struct device *dev = cfg->iommu_dev;
1944b123757SRobin Murphy 	int order = get_order(size);
1954b123757SRobin Murphy 	struct page *p;
196f8d54961SRobin Murphy 	dma_addr_t dma;
1974b123757SRobin Murphy 	void *pages;
198f8d54961SRobin Murphy 
1994b123757SRobin Murphy 	VM_BUG_ON((gfp & __GFP_HIGHMEM));
200fac83d29SJean-Philippe Brucker 	p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
201fac83d29SJean-Philippe Brucker 			     gfp | __GFP_ZERO, order);
2024b123757SRobin Murphy 	if (!p)
203f8d54961SRobin Murphy 		return NULL;
204f8d54961SRobin Murphy 
2054b123757SRobin Murphy 	pages = page_address(p);
2064f41845bSWill Deacon 	if (!cfg->coherent_walk) {
207f8d54961SRobin Murphy 		dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
208f8d54961SRobin Murphy 		if (dma_mapping_error(dev, dma))
209f8d54961SRobin Murphy 			goto out_free;
210f8d54961SRobin Murphy 		/*
211f8d54961SRobin Murphy 		 * We depend on the IOMMU being able to work with any physical
212ffcb6d16SRobin Murphy 		 * address directly, so if the DMA layer suggests otherwise by
213ffcb6d16SRobin Murphy 		 * translating or truncating them, that bodes very badly...
214f8d54961SRobin Murphy 		 */
215ffcb6d16SRobin Murphy 		if (dma != virt_to_phys(pages))
216f8d54961SRobin Murphy 			goto out_unmap;
217f8d54961SRobin Murphy 	}
218f8d54961SRobin Murphy 
219f8d54961SRobin Murphy 	return pages;
220f8d54961SRobin Murphy 
221f8d54961SRobin Murphy out_unmap:
222f8d54961SRobin Murphy 	dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
223f8d54961SRobin Murphy 	dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
224f8d54961SRobin Murphy out_free:
2254b123757SRobin Murphy 	__free_pages(p, order);
226f8d54961SRobin Murphy 	return NULL;
227f8d54961SRobin Murphy }
228f8d54961SRobin Murphy 
229f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size,
230f8d54961SRobin Murphy 				  struct io_pgtable_cfg *cfg)
231f8d54961SRobin Murphy {
2324f41845bSWill Deacon 	if (!cfg->coherent_walk)
233ffcb6d16SRobin Murphy 		dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
234f8d54961SRobin Murphy 				 size, DMA_TO_DEVICE);
2354b123757SRobin Murphy 	free_pages((unsigned long)pages, get_order(size));
236f8d54961SRobin Murphy }
237f8d54961SRobin Murphy 
23841e1eb25SIsaac J. Manjarres static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep, int num_entries,
2392c3d273eSRobin Murphy 				struct io_pgtable_cfg *cfg)
2402c3d273eSRobin Murphy {
2412c3d273eSRobin Murphy 	dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
24241e1eb25SIsaac J. Manjarres 				   sizeof(*ptep) * num_entries, DMA_TO_DEVICE);
2432c3d273eSRobin Murphy }
2442c3d273eSRobin Murphy 
245*1fe27be5SIsaac J. Manjarres static void __arm_lpae_clear_pte(arm_lpae_iopte *ptep, struct io_pgtable_cfg *cfg)
246f8d54961SRobin Murphy {
24741e1eb25SIsaac J. Manjarres 
248*1fe27be5SIsaac J. Manjarres 	*ptep = 0;
249f8d54961SRobin Murphy 
2504f41845bSWill Deacon 	if (!cfg->coherent_walk)
251*1fe27be5SIsaac J. Manjarres 		__arm_lpae_sync_pte(ptep, 1, cfg);
252f8d54961SRobin Murphy }
253f8d54961SRobin Murphy 
254193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
2553951c41aSWill Deacon 			       struct iommu_iotlb_gather *gather,
256*1fe27be5SIsaac J. Manjarres 			       unsigned long iova, size_t size, size_t pgcount,
257*1fe27be5SIsaac J. Manjarres 			       int lvl, arm_lpae_iopte *ptep);
258cf27ec93SWill Deacon 
259fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
260fb3a9579SRobin Murphy 				phys_addr_t paddr, arm_lpae_iopte prot,
26141e1eb25SIsaac J. Manjarres 				int lvl, int num_entries, arm_lpae_iopte *ptep)
262fb3a9579SRobin Murphy {
263fb3a9579SRobin Murphy 	arm_lpae_iopte pte = prot;
26441e1eb25SIsaac J. Manjarres 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
26541e1eb25SIsaac J. Manjarres 	size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
26641e1eb25SIsaac J. Manjarres 	int i;
267fb3a9579SRobin Murphy 
268d08d42deSRob Herring 	if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
269fb3a9579SRobin Murphy 		pte |= ARM_LPAE_PTE_TYPE_PAGE;
270fb3a9579SRobin Murphy 	else
271fb3a9579SRobin Murphy 		pte |= ARM_LPAE_PTE_TYPE_BLOCK;
272fb3a9579SRobin Murphy 
27341e1eb25SIsaac J. Manjarres 	for (i = 0; i < num_entries; i++)
27441e1eb25SIsaac J. Manjarres 		ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data);
275fb3a9579SRobin Murphy 
27641e1eb25SIsaac J. Manjarres 	if (!cfg->coherent_walk)
27741e1eb25SIsaac J. Manjarres 		__arm_lpae_sync_pte(ptep, num_entries, cfg);
278fb3a9579SRobin Murphy }
279fb3a9579SRobin Murphy 
280e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
281e1d3c0fdSWill Deacon 			     unsigned long iova, phys_addr_t paddr,
28241e1eb25SIsaac J. Manjarres 			     arm_lpae_iopte prot, int lvl, int num_entries,
283e1d3c0fdSWill Deacon 			     arm_lpae_iopte *ptep)
284e1d3c0fdSWill Deacon {
28541e1eb25SIsaac J. Manjarres 	int i;
286e1d3c0fdSWill Deacon 
28741e1eb25SIsaac J. Manjarres 	for (i = 0; i < num_entries; i++)
28841e1eb25SIsaac J. Manjarres 		if (iopte_leaf(ptep[i], lvl, data->iop.fmt)) {
289cf27ec93SWill Deacon 			/* We require an unmap first */
290fe4b991dSWill Deacon 			WARN_ON(!selftest_running);
291e1d3c0fdSWill Deacon 			return -EEXIST;
29241e1eb25SIsaac J. Manjarres 		} else if (iopte_type(ptep[i]) == ARM_LPAE_PTE_TYPE_TABLE) {
293cf27ec93SWill Deacon 			/*
294cf27ec93SWill Deacon 			 * We need to unmap and free the old table before
295cf27ec93SWill Deacon 			 * overwriting it with a block entry.
296cf27ec93SWill Deacon 			 */
297cf27ec93SWill Deacon 			arm_lpae_iopte *tblp;
298cf27ec93SWill Deacon 			size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
299cf27ec93SWill Deacon 
300cf27ec93SWill Deacon 			tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
301*1fe27be5SIsaac J. Manjarres 			if (__arm_lpae_unmap(data, NULL, iova + i * sz, sz, 1,
30241e1eb25SIsaac J. Manjarres 					     lvl, tblp) != sz) {
3033951c41aSWill Deacon 				WARN_ON(1);
304cf27ec93SWill Deacon 				return -EINVAL;
305fe4b991dSWill Deacon 			}
3063951c41aSWill Deacon 		}
307e1d3c0fdSWill Deacon 
30841e1eb25SIsaac J. Manjarres 	__arm_lpae_init_pte(data, paddr, prot, lvl, num_entries, ptep);
309e1d3c0fdSWill Deacon 	return 0;
310e1d3c0fdSWill Deacon }
311e1d3c0fdSWill Deacon 
312fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
313fb3a9579SRobin Murphy 					     arm_lpae_iopte *ptep,
3142c3d273eSRobin Murphy 					     arm_lpae_iopte curr,
315fb3a9579SRobin Murphy 					     struct io_pgtable_cfg *cfg)
316fb3a9579SRobin Murphy {
3172c3d273eSRobin Murphy 	arm_lpae_iopte old, new;
318fb3a9579SRobin Murphy 
319fb3a9579SRobin Murphy 	new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
320fb3a9579SRobin Murphy 	if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
321fb3a9579SRobin Murphy 		new |= ARM_LPAE_PTE_NSTABLE;
322fb3a9579SRobin Murphy 
32377f34458SWill Deacon 	/*
32477f34458SWill Deacon 	 * Ensure the table itself is visible before its PTE can be.
32577f34458SWill Deacon 	 * Whilst we could get away with cmpxchg64_release below, this
32677f34458SWill Deacon 	 * doesn't have any ordering semantics when !CONFIG_SMP.
32777f34458SWill Deacon 	 */
32877f34458SWill Deacon 	dma_wmb();
3292c3d273eSRobin Murphy 
3302c3d273eSRobin Murphy 	old = cmpxchg64_relaxed(ptep, curr, new);
3312c3d273eSRobin Murphy 
3324f41845bSWill Deacon 	if (cfg->coherent_walk || (old & ARM_LPAE_PTE_SW_SYNC))
3332c3d273eSRobin Murphy 		return old;
3342c3d273eSRobin Murphy 
3352c3d273eSRobin Murphy 	/* Even if it's not ours, there's no point waiting; just kick it */
33641e1eb25SIsaac J. Manjarres 	__arm_lpae_sync_pte(ptep, 1, cfg);
3372c3d273eSRobin Murphy 	if (old == curr)
3382c3d273eSRobin Murphy 		WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
3392c3d273eSRobin Murphy 
3402c3d273eSRobin Murphy 	return old;
341fb3a9579SRobin Murphy }
342fb3a9579SRobin Murphy 
343e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
344e1d3c0fdSWill Deacon 			  phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
345f34ce7a7SBaolin Wang 			  int lvl, arm_lpae_iopte *ptep, gfp_t gfp)
346e1d3c0fdSWill Deacon {
347e1d3c0fdSWill Deacon 	arm_lpae_iopte *cptep, pte;
348e1d3c0fdSWill Deacon 	size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
3492c3d273eSRobin Murphy 	size_t tblsz = ARM_LPAE_GRANULE(data);
350f8d54961SRobin Murphy 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
351e1d3c0fdSWill Deacon 
352e1d3c0fdSWill Deacon 	/* Find our entry at the current level */
353e1d3c0fdSWill Deacon 	ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
354e1d3c0fdSWill Deacon 
355e1d3c0fdSWill Deacon 	/* If we can install a leaf entry at this level, then do so */
356f7b90d2cSRobin Murphy 	if (size == block_size)
35741e1eb25SIsaac J. Manjarres 		return arm_lpae_init_pte(data, iova, paddr, prot, lvl, 1, ptep);
358e1d3c0fdSWill Deacon 
359e1d3c0fdSWill Deacon 	/* We can't allocate tables at the final level */
360e1d3c0fdSWill Deacon 	if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
361e1d3c0fdSWill Deacon 		return -EINVAL;
362e1d3c0fdSWill Deacon 
363e1d3c0fdSWill Deacon 	/* Grab a pointer to the next level */
3642c3d273eSRobin Murphy 	pte = READ_ONCE(*ptep);
365e1d3c0fdSWill Deacon 	if (!pte) {
366f34ce7a7SBaolin Wang 		cptep = __arm_lpae_alloc_pages(tblsz, gfp, cfg);
367e1d3c0fdSWill Deacon 		if (!cptep)
368e1d3c0fdSWill Deacon 			return -ENOMEM;
369e1d3c0fdSWill Deacon 
3702c3d273eSRobin Murphy 		pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
3712c3d273eSRobin Murphy 		if (pte)
3722c3d273eSRobin Murphy 			__arm_lpae_free_pages(cptep, tblsz, cfg);
3734f41845bSWill Deacon 	} else if (!cfg->coherent_walk && !(pte & ARM_LPAE_PTE_SW_SYNC)) {
37441e1eb25SIsaac J. Manjarres 		__arm_lpae_sync_pte(ptep, 1, cfg);
3752c3d273eSRobin Murphy 	}
3762c3d273eSRobin Murphy 
377d08d42deSRob Herring 	if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
378e1d3c0fdSWill Deacon 		cptep = iopte_deref(pte, data);
3792c3d273eSRobin Murphy 	} else if (pte) {
380ed46e66cSOleksandr Tyshchenko 		/* We require an unmap first */
381ed46e66cSOleksandr Tyshchenko 		WARN_ON(!selftest_running);
382ed46e66cSOleksandr Tyshchenko 		return -EEXIST;
383e1d3c0fdSWill Deacon 	}
384e1d3c0fdSWill Deacon 
385e1d3c0fdSWill Deacon 	/* Rinse, repeat */
386f34ce7a7SBaolin Wang 	return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep, gfp);
387e1d3c0fdSWill Deacon }
388e1d3c0fdSWill Deacon 
389e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
390e1d3c0fdSWill Deacon 					   int prot)
391e1d3c0fdSWill Deacon {
392e1d3c0fdSWill Deacon 	arm_lpae_iopte pte;
393e1d3c0fdSWill Deacon 
394e1d3c0fdSWill Deacon 	if (data->iop.fmt == ARM_64_LPAE_S1 ||
395e1d3c0fdSWill Deacon 	    data->iop.fmt == ARM_32_LPAE_S1) {
396e7468a23SJeremy Gebben 		pte = ARM_LPAE_PTE_nG;
397e1d3c0fdSWill Deacon 		if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
398e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_AP_RDONLY;
399e7468a23SJeremy Gebben 		if (!(prot & IOMMU_PRIV))
400e7468a23SJeremy Gebben 			pte |= ARM_LPAE_PTE_AP_UNPRIV;
401e1d3c0fdSWill Deacon 	} else {
402e1d3c0fdSWill Deacon 		pte = ARM_LPAE_PTE_HAP_FAULT;
403e1d3c0fdSWill Deacon 		if (prot & IOMMU_READ)
404e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_HAP_READ;
405e1d3c0fdSWill Deacon 		if (prot & IOMMU_WRITE)
406e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_HAP_WRITE;
407d08d42deSRob Herring 	}
408d08d42deSRob Herring 
409d08d42deSRob Herring 	/*
410d08d42deSRob Herring 	 * Note that this logic is structured to accommodate Mali LPAE
411d08d42deSRob Herring 	 * having stage-1-like attributes but stage-2-like permissions.
412d08d42deSRob Herring 	 */
413d08d42deSRob Herring 	if (data->iop.fmt == ARM_64_LPAE_S2 ||
414d08d42deSRob Herring 	    data->iop.fmt == ARM_32_LPAE_S2) {
415fb948251SRobin Murphy 		if (prot & IOMMU_MMIO)
416fb948251SRobin Murphy 			pte |= ARM_LPAE_PTE_MEMATTR_DEV;
417fb948251SRobin Murphy 		else if (prot & IOMMU_CACHE)
418e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
419e1d3c0fdSWill Deacon 		else
420e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_MEMATTR_NC;
421d08d42deSRob Herring 	} else {
422d08d42deSRob Herring 		if (prot & IOMMU_MMIO)
423d08d42deSRob Herring 			pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
424d08d42deSRob Herring 				<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
425d08d42deSRob Herring 		else if (prot & IOMMU_CACHE)
426d08d42deSRob Herring 			pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
427d08d42deSRob Herring 				<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
428e1d3c0fdSWill Deacon 	}
429e1d3c0fdSWill Deacon 
430728da60dSRobin Murphy 	/*
431728da60dSRobin Murphy 	 * Also Mali has its own notions of shareability wherein its Inner
432728da60dSRobin Murphy 	 * domain covers the cores within the GPU, and its Outer domain is
433728da60dSRobin Murphy 	 * "outside the GPU" (i.e. either the Inner or System domain in CPU
434728da60dSRobin Murphy 	 * terms, depending on coherency).
435728da60dSRobin Murphy 	 */
436728da60dSRobin Murphy 	if (prot & IOMMU_CACHE && data->iop.fmt != ARM_MALI_LPAE)
4377618e479SRobin Murphy 		pte |= ARM_LPAE_PTE_SH_IS;
4387618e479SRobin Murphy 	else
4397618e479SRobin Murphy 		pte |= ARM_LPAE_PTE_SH_OS;
4407618e479SRobin Murphy 
441e1d3c0fdSWill Deacon 	if (prot & IOMMU_NOEXEC)
442e1d3c0fdSWill Deacon 		pte |= ARM_LPAE_PTE_XN;
443e1d3c0fdSWill Deacon 
4447618e479SRobin Murphy 	if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
4457618e479SRobin Murphy 		pte |= ARM_LPAE_PTE_NS;
4467618e479SRobin Murphy 
4477618e479SRobin Murphy 	if (data->iop.fmt != ARM_MALI_LPAE)
4487618e479SRobin Murphy 		pte |= ARM_LPAE_PTE_AF;
4497618e479SRobin Murphy 
450e1d3c0fdSWill Deacon 	return pte;
451e1d3c0fdSWill Deacon }
452e1d3c0fdSWill Deacon 
453e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
454f34ce7a7SBaolin Wang 			phys_addr_t paddr, size_t size, int iommu_prot, gfp_t gfp)
455e1d3c0fdSWill Deacon {
456e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
457f7b90d2cSRobin Murphy 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
458e1d3c0fdSWill Deacon 	arm_lpae_iopte *ptep = data->pgd;
459594ab90fSRobin Murphy 	int ret, lvl = data->start_level;
460e1d3c0fdSWill Deacon 	arm_lpae_iopte prot;
46108090744SRobin Murphy 	long iaext = (s64)iova >> cfg->ias;
462e1d3c0fdSWill Deacon 
463f7b90d2cSRobin Murphy 	if (WARN_ON(!size || (size & cfg->pgsize_bitmap) != size))
464f7b90d2cSRobin Murphy 		return -EINVAL;
465f7b90d2cSRobin Murphy 
466db690301SRobin Murphy 	if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
467db690301SRobin Murphy 		iaext = ~iaext;
468db690301SRobin Murphy 	if (WARN_ON(iaext || paddr >> cfg->oas))
46976557391SRobin Murphy 		return -ERANGE;
47076557391SRobin Murphy 
471f12e0d22SKeqian Zhu 	/* If no access, then nothing to do */
472f12e0d22SKeqian Zhu 	if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
473f12e0d22SKeqian Zhu 		return 0;
474f12e0d22SKeqian Zhu 
475e1d3c0fdSWill Deacon 	prot = arm_lpae_prot_to_pte(data, iommu_prot);
476f34ce7a7SBaolin Wang 	ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep, gfp);
47787a91b15SRobin Murphy 	/*
47887a91b15SRobin Murphy 	 * Synchronise all PTE updates for the new mapping before there's
47987a91b15SRobin Murphy 	 * a chance for anything to kick off a table walk for the new iova.
48087a91b15SRobin Murphy 	 */
48187a91b15SRobin Murphy 	wmb();
48287a91b15SRobin Murphy 
48387a91b15SRobin Murphy 	return ret;
484e1d3c0fdSWill Deacon }
485e1d3c0fdSWill Deacon 
486e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
487e1d3c0fdSWill Deacon 				    arm_lpae_iopte *ptep)
488e1d3c0fdSWill Deacon {
489e1d3c0fdSWill Deacon 	arm_lpae_iopte *start, *end;
490e1d3c0fdSWill Deacon 	unsigned long table_size;
491e1d3c0fdSWill Deacon 
492594ab90fSRobin Murphy 	if (lvl == data->start_level)
493c79278c1SRobin Murphy 		table_size = ARM_LPAE_PGD_SIZE(data);
494e1d3c0fdSWill Deacon 	else
49506c610e8SRobin Murphy 		table_size = ARM_LPAE_GRANULE(data);
496e1d3c0fdSWill Deacon 
497e1d3c0fdSWill Deacon 	start = ptep;
49812c2ab09SWill Deacon 
49912c2ab09SWill Deacon 	/* Only leaf entries at the last level */
50012c2ab09SWill Deacon 	if (lvl == ARM_LPAE_MAX_LEVELS - 1)
50112c2ab09SWill Deacon 		end = ptep;
50212c2ab09SWill Deacon 	else
503e1d3c0fdSWill Deacon 		end = (void *)ptep + table_size;
504e1d3c0fdSWill Deacon 
505e1d3c0fdSWill Deacon 	while (ptep != end) {
506e1d3c0fdSWill Deacon 		arm_lpae_iopte pte = *ptep++;
507e1d3c0fdSWill Deacon 
508d08d42deSRob Herring 		if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
509e1d3c0fdSWill Deacon 			continue;
510e1d3c0fdSWill Deacon 
511e1d3c0fdSWill Deacon 		__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
512e1d3c0fdSWill Deacon 	}
513e1d3c0fdSWill Deacon 
514f8d54961SRobin Murphy 	__arm_lpae_free_pages(start, table_size, &data->iop.cfg);
515e1d3c0fdSWill Deacon }
516e1d3c0fdSWill Deacon 
517e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop)
518e1d3c0fdSWill Deacon {
519e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
520e1d3c0fdSWill Deacon 
521594ab90fSRobin Murphy 	__arm_lpae_free_pgtable(data, data->start_level, data->pgd);
522e1d3c0fdSWill Deacon 	kfree(data);
523e1d3c0fdSWill Deacon }
524e1d3c0fdSWill Deacon 
525193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
5263951c41aSWill Deacon 				       struct iommu_iotlb_gather *gather,
527e1d3c0fdSWill Deacon 				       unsigned long iova, size_t size,
528fb3a9579SRobin Murphy 				       arm_lpae_iopte blk_pte, int lvl,
529*1fe27be5SIsaac J. Manjarres 				       arm_lpae_iopte *ptep, size_t pgcount)
530e1d3c0fdSWill Deacon {
531fb3a9579SRobin Murphy 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
532fb3a9579SRobin Murphy 	arm_lpae_iopte pte, *tablep;
533e1d3c0fdSWill Deacon 	phys_addr_t blk_paddr;
534fb3a9579SRobin Murphy 	size_t tablesz = ARM_LPAE_GRANULE(data);
535fb3a9579SRobin Murphy 	size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
536*1fe27be5SIsaac J. Manjarres 	int ptes_per_table = ARM_LPAE_PTES_PER_TABLE(data);
537*1fe27be5SIsaac J. Manjarres 	int i, unmap_idx_start = -1, num_entries = 0, max_entries;
538e1d3c0fdSWill Deacon 
539fb3a9579SRobin Murphy 	if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
540fb3a9579SRobin Murphy 		return 0;
541e1d3c0fdSWill Deacon 
542fb3a9579SRobin Murphy 	tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
543fb3a9579SRobin Murphy 	if (!tablep)
544fb3a9579SRobin Murphy 		return 0; /* Bytes unmapped */
545e1d3c0fdSWill Deacon 
546*1fe27be5SIsaac J. Manjarres 	if (size == split_sz) {
547*1fe27be5SIsaac J. Manjarres 		unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
548*1fe27be5SIsaac J. Manjarres 		max_entries = ptes_per_table - unmap_idx_start;
549*1fe27be5SIsaac J. Manjarres 		num_entries = min_t(int, pgcount, max_entries);
550*1fe27be5SIsaac J. Manjarres 	}
551fb3a9579SRobin Murphy 
5526c89928fSRobin Murphy 	blk_paddr = iopte_to_paddr(blk_pte, data);
553fb3a9579SRobin Murphy 	pte = iopte_prot(blk_pte);
554fb3a9579SRobin Murphy 
555*1fe27be5SIsaac J. Manjarres 	for (i = 0; i < ptes_per_table; i++, blk_paddr += split_sz) {
556e1d3c0fdSWill Deacon 		/* Unmap! */
557*1fe27be5SIsaac J. Manjarres 		if (i >= unmap_idx_start && i < (unmap_idx_start + num_entries))
558e1d3c0fdSWill Deacon 			continue;
559e1d3c0fdSWill Deacon 
56041e1eb25SIsaac J. Manjarres 		__arm_lpae_init_pte(data, blk_paddr, pte, lvl, 1, &tablep[i]);
561e1d3c0fdSWill Deacon 	}
562e1d3c0fdSWill Deacon 
5632c3d273eSRobin Murphy 	pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg);
5642c3d273eSRobin Murphy 	if (pte != blk_pte) {
5652c3d273eSRobin Murphy 		__arm_lpae_free_pages(tablep, tablesz, cfg);
5662c3d273eSRobin Murphy 		/*
5672c3d273eSRobin Murphy 		 * We may race against someone unmapping another part of this
5682c3d273eSRobin Murphy 		 * block, but anything else is invalid. We can't misinterpret
5692c3d273eSRobin Murphy 		 * a page entry here since we're never at the last level.
5702c3d273eSRobin Murphy 		 */
571f37eb484SKunkun Jiang 		if (iopte_type(pte) != ARM_LPAE_PTE_TYPE_TABLE)
5722c3d273eSRobin Murphy 			return 0;
5732c3d273eSRobin Murphy 
5742c3d273eSRobin Murphy 		tablep = iopte_deref(pte, data);
575*1fe27be5SIsaac J. Manjarres 	} else if (unmap_idx_start >= 0) {
576*1fe27be5SIsaac J. Manjarres 		for (i = 0; i < num_entries; i++)
577*1fe27be5SIsaac J. Manjarres 			io_pgtable_tlb_add_page(&data->iop, gather, iova + i * size, size);
578*1fe27be5SIsaac J. Manjarres 
579*1fe27be5SIsaac J. Manjarres 		return num_entries * size;
580e1d3c0fdSWill Deacon 	}
581e1d3c0fdSWill Deacon 
582*1fe27be5SIsaac J. Manjarres 	return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl, tablep);
58385c7a0f1SRobin Murphy }
58485c7a0f1SRobin Murphy 
585193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
5863951c41aSWill Deacon 			       struct iommu_iotlb_gather *gather,
587*1fe27be5SIsaac J. Manjarres 			       unsigned long iova, size_t size, size_t pgcount,
588*1fe27be5SIsaac J. Manjarres 			       int lvl, arm_lpae_iopte *ptep)
589e1d3c0fdSWill Deacon {
590e1d3c0fdSWill Deacon 	arm_lpae_iopte pte;
591507e4c9dSRobin Murphy 	struct io_pgtable *iop = &data->iop;
592*1fe27be5SIsaac J. Manjarres 	int i = 0, num_entries, max_entries, unmap_idx_start;
593e1d3c0fdSWill Deacon 
5942eb97c78SRobin Murphy 	/* Something went horribly wrong and we ran out of page table */
5952eb97c78SRobin Murphy 	if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
5962eb97c78SRobin Murphy 		return 0;
5972eb97c78SRobin Murphy 
598*1fe27be5SIsaac J. Manjarres 	unmap_idx_start = ARM_LPAE_LVL_IDX(iova, lvl, data);
599*1fe27be5SIsaac J. Manjarres 	ptep += unmap_idx_start;
6002c3d273eSRobin Murphy 	pte = READ_ONCE(*ptep);
6012eb97c78SRobin Murphy 	if (WARN_ON(!pte))
602e1d3c0fdSWill Deacon 		return 0;
603e1d3c0fdSWill Deacon 
604e1d3c0fdSWill Deacon 	/* If the size matches this level, we're in the right place */
605fb3a9579SRobin Murphy 	if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
606*1fe27be5SIsaac J. Manjarres 		max_entries = ARM_LPAE_PTES_PER_TABLE(data) - unmap_idx_start;
607*1fe27be5SIsaac J. Manjarres 		num_entries = min_t(int, pgcount, max_entries);
608*1fe27be5SIsaac J. Manjarres 
609*1fe27be5SIsaac J. Manjarres 		while (i < num_entries) {
610*1fe27be5SIsaac J. Manjarres 			pte = READ_ONCE(*ptep);
611*1fe27be5SIsaac J. Manjarres 			if (WARN_ON(!pte))
612*1fe27be5SIsaac J. Manjarres 				break;
613*1fe27be5SIsaac J. Manjarres 
614*1fe27be5SIsaac J. Manjarres 			__arm_lpae_clear_pte(ptep, &iop->cfg);
615e1d3c0fdSWill Deacon 
616d08d42deSRob Herring 			if (!iopte_leaf(pte, lvl, iop->fmt)) {
617e1d3c0fdSWill Deacon 				/* Also flush any partial walks */
618*1fe27be5SIsaac J. Manjarres 				io_pgtable_tlb_flush_walk(iop, iova + i * size, size,
61910b7a7d9SWill Deacon 							  ARM_LPAE_GRANULE(data));
620*1fe27be5SIsaac J. Manjarres 				__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
621b6b65ca2SZhen Lei 			} else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
622b6b65ca2SZhen Lei 				/*
623b6b65ca2SZhen Lei 				 * Order the PTE update against queueing the IOVA, to
624b6b65ca2SZhen Lei 				 * guarantee that a flush callback from a different CPU
625b6b65ca2SZhen Lei 				 * has observed it before the TLBIALL can be issued.
626b6b65ca2SZhen Lei 				 */
627b6b65ca2SZhen Lei 				smp_wmb();
628e1d3c0fdSWill Deacon 			} else {
629*1fe27be5SIsaac J. Manjarres 				io_pgtable_tlb_add_page(iop, gather, iova + i * size, size);
630e1d3c0fdSWill Deacon 			}
631e1d3c0fdSWill Deacon 
632*1fe27be5SIsaac J. Manjarres 			ptep++;
633*1fe27be5SIsaac J. Manjarres 			i++;
634*1fe27be5SIsaac J. Manjarres 		}
635*1fe27be5SIsaac J. Manjarres 
636*1fe27be5SIsaac J. Manjarres 		return i * size;
637d08d42deSRob Herring 	} else if (iopte_leaf(pte, lvl, iop->fmt)) {
638e1d3c0fdSWill Deacon 		/*
639e1d3c0fdSWill Deacon 		 * Insert a table at the next level to map the old region,
640e1d3c0fdSWill Deacon 		 * minus the part we want to unmap
641e1d3c0fdSWill Deacon 		 */
6423951c41aSWill Deacon 		return arm_lpae_split_blk_unmap(data, gather, iova, size, pte,
643*1fe27be5SIsaac J. Manjarres 						lvl + 1, ptep, pgcount);
644e1d3c0fdSWill Deacon 	}
645e1d3c0fdSWill Deacon 
646e1d3c0fdSWill Deacon 	/* Keep on walkin' */
647e1d3c0fdSWill Deacon 	ptep = iopte_deref(pte, data);
648*1fe27be5SIsaac J. Manjarres 	return __arm_lpae_unmap(data, gather, iova, size, pgcount, lvl + 1, ptep);
649e1d3c0fdSWill Deacon }
650e1d3c0fdSWill Deacon 
651*1fe27be5SIsaac J. Manjarres static size_t arm_lpae_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova,
652*1fe27be5SIsaac J. Manjarres 				   size_t pgsize, size_t pgcount,
653*1fe27be5SIsaac J. Manjarres 				   struct iommu_iotlb_gather *gather)
654e1d3c0fdSWill Deacon {
655e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
656f7b90d2cSRobin Murphy 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
657e1d3c0fdSWill Deacon 	arm_lpae_iopte *ptep = data->pgd;
65808090744SRobin Murphy 	long iaext = (s64)iova >> cfg->ias;
659e1d3c0fdSWill Deacon 
660*1fe27be5SIsaac J. Manjarres 	if (WARN_ON(!pgsize || (pgsize & cfg->pgsize_bitmap) != pgsize || !pgcount))
661f7b90d2cSRobin Murphy 		return 0;
662f7b90d2cSRobin Murphy 
663db690301SRobin Murphy 	if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
664db690301SRobin Murphy 		iaext = ~iaext;
665db690301SRobin Murphy 	if (WARN_ON(iaext))
66676557391SRobin Murphy 		return 0;
66776557391SRobin Murphy 
668*1fe27be5SIsaac J. Manjarres 	return __arm_lpae_unmap(data, gather, iova, pgsize, pgcount,
669*1fe27be5SIsaac J. Manjarres 				data->start_level, ptep);
670*1fe27be5SIsaac J. Manjarres }
671*1fe27be5SIsaac J. Manjarres 
672*1fe27be5SIsaac J. Manjarres static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
673*1fe27be5SIsaac J. Manjarres 			     size_t size, struct iommu_iotlb_gather *gather)
674*1fe27be5SIsaac J. Manjarres {
675*1fe27be5SIsaac J. Manjarres 	return arm_lpae_unmap_pages(ops, iova, size, 1, gather);
676e1d3c0fdSWill Deacon }
677e1d3c0fdSWill Deacon 
678e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
679e1d3c0fdSWill Deacon 					 unsigned long iova)
680e1d3c0fdSWill Deacon {
681e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
682e1d3c0fdSWill Deacon 	arm_lpae_iopte pte, *ptep = data->pgd;
683594ab90fSRobin Murphy 	int lvl = data->start_level;
684e1d3c0fdSWill Deacon 
685e1d3c0fdSWill Deacon 	do {
686e1d3c0fdSWill Deacon 		/* Valid IOPTE pointer? */
687e1d3c0fdSWill Deacon 		if (!ptep)
688e1d3c0fdSWill Deacon 			return 0;
689e1d3c0fdSWill Deacon 
690e1d3c0fdSWill Deacon 		/* Grab the IOPTE we're interested in */
6912c3d273eSRobin Murphy 		ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
6922c3d273eSRobin Murphy 		pte = READ_ONCE(*ptep);
693e1d3c0fdSWill Deacon 
694e1d3c0fdSWill Deacon 		/* Valid entry? */
695e1d3c0fdSWill Deacon 		if (!pte)
696e1d3c0fdSWill Deacon 			return 0;
697e1d3c0fdSWill Deacon 
698e1d3c0fdSWill Deacon 		/* Leaf entry? */
699d08d42deSRob Herring 		if (iopte_leaf(pte, lvl, data->iop.fmt))
700e1d3c0fdSWill Deacon 			goto found_translation;
701e1d3c0fdSWill Deacon 
702e1d3c0fdSWill Deacon 		/* Take it to the next level */
703e1d3c0fdSWill Deacon 		ptep = iopte_deref(pte, data);
704e1d3c0fdSWill Deacon 	} while (++lvl < ARM_LPAE_MAX_LEVELS);
705e1d3c0fdSWill Deacon 
706e1d3c0fdSWill Deacon 	/* Ran out of page tables to walk */
707e1d3c0fdSWill Deacon 	return 0;
708e1d3c0fdSWill Deacon 
709e1d3c0fdSWill Deacon found_translation:
7107c6d90e2SWill Deacon 	iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
7116c89928fSRobin Murphy 	return iopte_to_paddr(pte, data) | iova;
712e1d3c0fdSWill Deacon }
713e1d3c0fdSWill Deacon 
714e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
715e1d3c0fdSWill Deacon {
7166c89928fSRobin Murphy 	unsigned long granule, page_sizes;
7176c89928fSRobin Murphy 	unsigned int max_addr_bits = 48;
718e1d3c0fdSWill Deacon 
719e1d3c0fdSWill Deacon 	/*
720e1d3c0fdSWill Deacon 	 * We need to restrict the supported page sizes to match the
721e1d3c0fdSWill Deacon 	 * translation regime for a particular granule. Aim to match
722e1d3c0fdSWill Deacon 	 * the CPU page size if possible, otherwise prefer smaller sizes.
723e1d3c0fdSWill Deacon 	 * While we're at it, restrict the block sizes to match the
724e1d3c0fdSWill Deacon 	 * chosen granule.
725e1d3c0fdSWill Deacon 	 */
726e1d3c0fdSWill Deacon 	if (cfg->pgsize_bitmap & PAGE_SIZE)
727e1d3c0fdSWill Deacon 		granule = PAGE_SIZE;
728e1d3c0fdSWill Deacon 	else if (cfg->pgsize_bitmap & ~PAGE_MASK)
729e1d3c0fdSWill Deacon 		granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
730e1d3c0fdSWill Deacon 	else if (cfg->pgsize_bitmap & PAGE_MASK)
731e1d3c0fdSWill Deacon 		granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
732e1d3c0fdSWill Deacon 	else
733e1d3c0fdSWill Deacon 		granule = 0;
734e1d3c0fdSWill Deacon 
735e1d3c0fdSWill Deacon 	switch (granule) {
736e1d3c0fdSWill Deacon 	case SZ_4K:
7376c89928fSRobin Murphy 		page_sizes = (SZ_4K | SZ_2M | SZ_1G);
738e1d3c0fdSWill Deacon 		break;
739e1d3c0fdSWill Deacon 	case SZ_16K:
7406c89928fSRobin Murphy 		page_sizes = (SZ_16K | SZ_32M);
741e1d3c0fdSWill Deacon 		break;
742e1d3c0fdSWill Deacon 	case SZ_64K:
7436c89928fSRobin Murphy 		max_addr_bits = 52;
7446c89928fSRobin Murphy 		page_sizes = (SZ_64K | SZ_512M);
7456c89928fSRobin Murphy 		if (cfg->oas > 48)
7466c89928fSRobin Murphy 			page_sizes |= 1ULL << 42; /* 4TB */
747e1d3c0fdSWill Deacon 		break;
748e1d3c0fdSWill Deacon 	default:
7496c89928fSRobin Murphy 		page_sizes = 0;
750e1d3c0fdSWill Deacon 	}
7516c89928fSRobin Murphy 
7526c89928fSRobin Murphy 	cfg->pgsize_bitmap &= page_sizes;
7536c89928fSRobin Murphy 	cfg->ias = min(cfg->ias, max_addr_bits);
7546c89928fSRobin Murphy 	cfg->oas = min(cfg->oas, max_addr_bits);
755e1d3c0fdSWill Deacon }
756e1d3c0fdSWill Deacon 
757e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable *
758e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
759e1d3c0fdSWill Deacon {
760e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data;
7615fb190b0SRobin Murphy 	int levels, va_bits, pg_shift;
762e1d3c0fdSWill Deacon 
763e1d3c0fdSWill Deacon 	arm_lpae_restrict_pgsizes(cfg);
764e1d3c0fdSWill Deacon 
765e1d3c0fdSWill Deacon 	if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
766e1d3c0fdSWill Deacon 		return NULL;
767e1d3c0fdSWill Deacon 
768e1d3c0fdSWill Deacon 	if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
769e1d3c0fdSWill Deacon 		return NULL;
770e1d3c0fdSWill Deacon 
771e1d3c0fdSWill Deacon 	if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
772e1d3c0fdSWill Deacon 		return NULL;
773e1d3c0fdSWill Deacon 
774e1d3c0fdSWill Deacon 	data = kmalloc(sizeof(*data), GFP_KERNEL);
775e1d3c0fdSWill Deacon 	if (!data)
776e1d3c0fdSWill Deacon 		return NULL;
777e1d3c0fdSWill Deacon 
7785fb190b0SRobin Murphy 	pg_shift = __ffs(cfg->pgsize_bitmap);
7795fb190b0SRobin Murphy 	data->bits_per_level = pg_shift - ilog2(sizeof(arm_lpae_iopte));
780e1d3c0fdSWill Deacon 
7815fb190b0SRobin Murphy 	va_bits = cfg->ias - pg_shift;
782594ab90fSRobin Murphy 	levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
783594ab90fSRobin Murphy 	data->start_level = ARM_LPAE_MAX_LEVELS - levels;
784e1d3c0fdSWill Deacon 
785e1d3c0fdSWill Deacon 	/* Calculate the actual size of our pgd (without concatenation) */
786c79278c1SRobin Murphy 	data->pgd_bits = va_bits - (data->bits_per_level * (levels - 1));
787e1d3c0fdSWill Deacon 
788e1d3c0fdSWill Deacon 	data->iop.ops = (struct io_pgtable_ops) {
789e1d3c0fdSWill Deacon 		.map		= arm_lpae_map,
790e1d3c0fdSWill Deacon 		.unmap		= arm_lpae_unmap,
791*1fe27be5SIsaac J. Manjarres 		.unmap_pages	= arm_lpae_unmap_pages,
792e1d3c0fdSWill Deacon 		.iova_to_phys	= arm_lpae_iova_to_phys,
793e1d3c0fdSWill Deacon 	};
794e1d3c0fdSWill Deacon 
795e1d3c0fdSWill Deacon 	return data;
796e1d3c0fdSWill Deacon }
797e1d3c0fdSWill Deacon 
798e1d3c0fdSWill Deacon static struct io_pgtable *
799e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
800e1d3c0fdSWill Deacon {
801e1d3c0fdSWill Deacon 	u64 reg;
8023850db49SRobin Murphy 	struct arm_lpae_io_pgtable *data;
803fb485eb1SRobin Murphy 	typeof(&cfg->arm_lpae_s1_cfg.tcr) tcr = &cfg->arm_lpae_s1_cfg.tcr;
804db690301SRobin Murphy 	bool tg1;
805e1d3c0fdSWill Deacon 
8064f41845bSWill Deacon 	if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
807db690301SRobin Murphy 			    IO_PGTABLE_QUIRK_NON_STRICT |
808e67890c9SSai Prakash Ranjan 			    IO_PGTABLE_QUIRK_ARM_TTBR1 |
809e67890c9SSai Prakash Ranjan 			    IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
8103850db49SRobin Murphy 		return NULL;
8113850db49SRobin Murphy 
8123850db49SRobin Murphy 	data = arm_lpae_alloc_pgtable(cfg);
813e1d3c0fdSWill Deacon 	if (!data)
814e1d3c0fdSWill Deacon 		return NULL;
815e1d3c0fdSWill Deacon 
816e1d3c0fdSWill Deacon 	/* TCR */
8179e6ea59fSBjorn Andersson 	if (cfg->coherent_walk) {
818fb485eb1SRobin Murphy 		tcr->sh = ARM_LPAE_TCR_SH_IS;
819fb485eb1SRobin Murphy 		tcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
820fb485eb1SRobin Murphy 		tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
821e67890c9SSai Prakash Ranjan 		if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA)
822e67890c9SSai Prakash Ranjan 			goto out_free_data;
8239e6ea59fSBjorn Andersson 	} else {
824fb485eb1SRobin Murphy 		tcr->sh = ARM_LPAE_TCR_SH_OS;
825fb485eb1SRobin Murphy 		tcr->irgn = ARM_LPAE_TCR_RGN_NC;
826e67890c9SSai Prakash Ranjan 		if (!(cfg->quirks & IO_PGTABLE_QUIRK_ARM_OUTER_WBWA))
827fb485eb1SRobin Murphy 			tcr->orgn = ARM_LPAE_TCR_RGN_NC;
828e67890c9SSai Prakash Ranjan 		else
829e67890c9SSai Prakash Ranjan 			tcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
8309e6ea59fSBjorn Andersson 	}
831e1d3c0fdSWill Deacon 
832db690301SRobin Murphy 	tg1 = cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1;
83306c610e8SRobin Murphy 	switch (ARM_LPAE_GRANULE(data)) {
834e1d3c0fdSWill Deacon 	case SZ_4K:
835db690301SRobin Murphy 		tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_4K : ARM_LPAE_TCR_TG0_4K;
836e1d3c0fdSWill Deacon 		break;
837e1d3c0fdSWill Deacon 	case SZ_16K:
838db690301SRobin Murphy 		tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_16K : ARM_LPAE_TCR_TG0_16K;
839e1d3c0fdSWill Deacon 		break;
840e1d3c0fdSWill Deacon 	case SZ_64K:
841db690301SRobin Murphy 		tcr->tg = tg1 ? ARM_LPAE_TCR_TG1_64K : ARM_LPAE_TCR_TG0_64K;
842e1d3c0fdSWill Deacon 		break;
843e1d3c0fdSWill Deacon 	}
844e1d3c0fdSWill Deacon 
845e1d3c0fdSWill Deacon 	switch (cfg->oas) {
846e1d3c0fdSWill Deacon 	case 32:
847fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_32_BIT;
848e1d3c0fdSWill Deacon 		break;
849e1d3c0fdSWill Deacon 	case 36:
850fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_36_BIT;
851e1d3c0fdSWill Deacon 		break;
852e1d3c0fdSWill Deacon 	case 40:
853fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_40_BIT;
854e1d3c0fdSWill Deacon 		break;
855e1d3c0fdSWill Deacon 	case 42:
856fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_42_BIT;
857e1d3c0fdSWill Deacon 		break;
858e1d3c0fdSWill Deacon 	case 44:
859fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_44_BIT;
860e1d3c0fdSWill Deacon 		break;
861e1d3c0fdSWill Deacon 	case 48:
862fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_48_BIT;
863e1d3c0fdSWill Deacon 		break;
8646c89928fSRobin Murphy 	case 52:
865fb485eb1SRobin Murphy 		tcr->ips = ARM_LPAE_TCR_PS_52_BIT;
8666c89928fSRobin Murphy 		break;
867e1d3c0fdSWill Deacon 	default:
868e1d3c0fdSWill Deacon 		goto out_free_data;
869e1d3c0fdSWill Deacon 	}
870e1d3c0fdSWill Deacon 
871fb485eb1SRobin Murphy 	tcr->tsz = 64ULL - cfg->ias;
872e1d3c0fdSWill Deacon 
873e1d3c0fdSWill Deacon 	/* MAIRs */
874e1d3c0fdSWill Deacon 	reg = (ARM_LPAE_MAIR_ATTR_NC
875e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
876e1d3c0fdSWill Deacon 	      (ARM_LPAE_MAIR_ATTR_WBRWA
877e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
878e1d3c0fdSWill Deacon 	      (ARM_LPAE_MAIR_ATTR_DEVICE
87990ec7a76SVivek Gautam 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
88090ec7a76SVivek Gautam 	      (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
88190ec7a76SVivek Gautam 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
882e1d3c0fdSWill Deacon 
883205577abSRobin Murphy 	cfg->arm_lpae_s1_cfg.mair = reg;
884e1d3c0fdSWill Deacon 
885e1d3c0fdSWill Deacon 	/* Looking good; allocate a pgd */
886c79278c1SRobin Murphy 	data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
887c79278c1SRobin Murphy 					   GFP_KERNEL, cfg);
888e1d3c0fdSWill Deacon 	if (!data->pgd)
889e1d3c0fdSWill Deacon 		goto out_free_data;
890e1d3c0fdSWill Deacon 
89187a91b15SRobin Murphy 	/* Ensure the empty pgd is visible before any actual TTBR write */
89287a91b15SRobin Murphy 	wmb();
893e1d3c0fdSWill Deacon 
894d1e5f26fSRobin Murphy 	/* TTBR */
895d1e5f26fSRobin Murphy 	cfg->arm_lpae_s1_cfg.ttbr = virt_to_phys(data->pgd);
896e1d3c0fdSWill Deacon 	return &data->iop;
897e1d3c0fdSWill Deacon 
898e1d3c0fdSWill Deacon out_free_data:
899e1d3c0fdSWill Deacon 	kfree(data);
900e1d3c0fdSWill Deacon 	return NULL;
901e1d3c0fdSWill Deacon }
902e1d3c0fdSWill Deacon 
903e1d3c0fdSWill Deacon static struct io_pgtable *
904e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
905e1d3c0fdSWill Deacon {
906ac4b80e5SWill Deacon 	u64 sl;
9073850db49SRobin Murphy 	struct arm_lpae_io_pgtable *data;
908ac4b80e5SWill Deacon 	typeof(&cfg->arm_lpae_s2_cfg.vtcr) vtcr = &cfg->arm_lpae_s2_cfg.vtcr;
909e1d3c0fdSWill Deacon 
9103850db49SRobin Murphy 	/* The NS quirk doesn't apply at stage 2 */
9114f41845bSWill Deacon 	if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NON_STRICT))
9123850db49SRobin Murphy 		return NULL;
9133850db49SRobin Murphy 
9143850db49SRobin Murphy 	data = arm_lpae_alloc_pgtable(cfg);
915e1d3c0fdSWill Deacon 	if (!data)
916e1d3c0fdSWill Deacon 		return NULL;
917e1d3c0fdSWill Deacon 
918e1d3c0fdSWill Deacon 	/*
919e1d3c0fdSWill Deacon 	 * Concatenate PGDs at level 1 if possible in order to reduce
920e1d3c0fdSWill Deacon 	 * the depth of the stage-2 walk.
921e1d3c0fdSWill Deacon 	 */
922594ab90fSRobin Murphy 	if (data->start_level == 0) {
923e1d3c0fdSWill Deacon 		unsigned long pgd_pages;
924e1d3c0fdSWill Deacon 
925c79278c1SRobin Murphy 		pgd_pages = ARM_LPAE_PGD_SIZE(data) / sizeof(arm_lpae_iopte);
926e1d3c0fdSWill Deacon 		if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
927c79278c1SRobin Murphy 			data->pgd_bits += data->bits_per_level;
928594ab90fSRobin Murphy 			data->start_level++;
929e1d3c0fdSWill Deacon 		}
930e1d3c0fdSWill Deacon 	}
931e1d3c0fdSWill Deacon 
932e1d3c0fdSWill Deacon 	/* VTCR */
93330d2acb6SWill Deacon 	if (cfg->coherent_walk) {
934ac4b80e5SWill Deacon 		vtcr->sh = ARM_LPAE_TCR_SH_IS;
935ac4b80e5SWill Deacon 		vtcr->irgn = ARM_LPAE_TCR_RGN_WBWA;
936ac4b80e5SWill Deacon 		vtcr->orgn = ARM_LPAE_TCR_RGN_WBWA;
93730d2acb6SWill Deacon 	} else {
938ac4b80e5SWill Deacon 		vtcr->sh = ARM_LPAE_TCR_SH_OS;
939ac4b80e5SWill Deacon 		vtcr->irgn = ARM_LPAE_TCR_RGN_NC;
940ac4b80e5SWill Deacon 		vtcr->orgn = ARM_LPAE_TCR_RGN_NC;
94130d2acb6SWill Deacon 	}
942e1d3c0fdSWill Deacon 
943594ab90fSRobin Murphy 	sl = data->start_level;
944e1d3c0fdSWill Deacon 
94506c610e8SRobin Murphy 	switch (ARM_LPAE_GRANULE(data)) {
946e1d3c0fdSWill Deacon 	case SZ_4K:
947ac4b80e5SWill Deacon 		vtcr->tg = ARM_LPAE_TCR_TG0_4K;
948e1d3c0fdSWill Deacon 		sl++; /* SL0 format is different for 4K granule size */
949e1d3c0fdSWill Deacon 		break;
950e1d3c0fdSWill Deacon 	case SZ_16K:
951ac4b80e5SWill Deacon 		vtcr->tg = ARM_LPAE_TCR_TG0_16K;
952e1d3c0fdSWill Deacon 		break;
953e1d3c0fdSWill Deacon 	case SZ_64K:
954ac4b80e5SWill Deacon 		vtcr->tg = ARM_LPAE_TCR_TG0_64K;
955e1d3c0fdSWill Deacon 		break;
956e1d3c0fdSWill Deacon 	}
957e1d3c0fdSWill Deacon 
958e1d3c0fdSWill Deacon 	switch (cfg->oas) {
959e1d3c0fdSWill Deacon 	case 32:
960ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_32_BIT;
961e1d3c0fdSWill Deacon 		break;
962e1d3c0fdSWill Deacon 	case 36:
963ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_36_BIT;
964e1d3c0fdSWill Deacon 		break;
965e1d3c0fdSWill Deacon 	case 40:
966ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_40_BIT;
967e1d3c0fdSWill Deacon 		break;
968e1d3c0fdSWill Deacon 	case 42:
969ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_42_BIT;
970e1d3c0fdSWill Deacon 		break;
971e1d3c0fdSWill Deacon 	case 44:
972ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_44_BIT;
973e1d3c0fdSWill Deacon 		break;
974e1d3c0fdSWill Deacon 	case 48:
975ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_48_BIT;
976e1d3c0fdSWill Deacon 		break;
9776c89928fSRobin Murphy 	case 52:
978ac4b80e5SWill Deacon 		vtcr->ps = ARM_LPAE_TCR_PS_52_BIT;
9796c89928fSRobin Murphy 		break;
980e1d3c0fdSWill Deacon 	default:
981e1d3c0fdSWill Deacon 		goto out_free_data;
982e1d3c0fdSWill Deacon 	}
983e1d3c0fdSWill Deacon 
984ac4b80e5SWill Deacon 	vtcr->tsz = 64ULL - cfg->ias;
985ac4b80e5SWill Deacon 	vtcr->sl = ~sl & ARM_LPAE_VTCR_SL0_MASK;
986e1d3c0fdSWill Deacon 
987e1d3c0fdSWill Deacon 	/* Allocate pgd pages */
988c79278c1SRobin Murphy 	data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data),
989c79278c1SRobin Murphy 					   GFP_KERNEL, cfg);
990e1d3c0fdSWill Deacon 	if (!data->pgd)
991e1d3c0fdSWill Deacon 		goto out_free_data;
992e1d3c0fdSWill Deacon 
99387a91b15SRobin Murphy 	/* Ensure the empty pgd is visible before any actual TTBR write */
99487a91b15SRobin Murphy 	wmb();
995e1d3c0fdSWill Deacon 
996e1d3c0fdSWill Deacon 	/* VTTBR */
997e1d3c0fdSWill Deacon 	cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
998e1d3c0fdSWill Deacon 	return &data->iop;
999e1d3c0fdSWill Deacon 
1000e1d3c0fdSWill Deacon out_free_data:
1001e1d3c0fdSWill Deacon 	kfree(data);
1002e1d3c0fdSWill Deacon 	return NULL;
1003e1d3c0fdSWill Deacon }
1004e1d3c0fdSWill Deacon 
1005e1d3c0fdSWill Deacon static struct io_pgtable *
1006e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
1007e1d3c0fdSWill Deacon {
1008e1d3c0fdSWill Deacon 	if (cfg->ias > 32 || cfg->oas > 40)
1009e1d3c0fdSWill Deacon 		return NULL;
1010e1d3c0fdSWill Deacon 
1011e1d3c0fdSWill Deacon 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1012fb485eb1SRobin Murphy 	return arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1013e1d3c0fdSWill Deacon }
1014e1d3c0fdSWill Deacon 
1015e1d3c0fdSWill Deacon static struct io_pgtable *
1016e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1017e1d3c0fdSWill Deacon {
1018e1d3c0fdSWill Deacon 	if (cfg->ias > 40 || cfg->oas > 40)
1019e1d3c0fdSWill Deacon 		return NULL;
1020e1d3c0fdSWill Deacon 
1021e1d3c0fdSWill Deacon 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1022ac4b80e5SWill Deacon 	return arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1023e1d3c0fdSWill Deacon }
1024e1d3c0fdSWill Deacon 
1025d08d42deSRob Herring static struct io_pgtable *
1026d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1027d08d42deSRob Herring {
102852f325f4SRobin Murphy 	struct arm_lpae_io_pgtable *data;
1029d08d42deSRob Herring 
103052f325f4SRobin Murphy 	/* No quirks for Mali (hopefully) */
103152f325f4SRobin Murphy 	if (cfg->quirks)
103252f325f4SRobin Murphy 		return NULL;
1033d08d42deSRob Herring 
10341be08f45SRobin Murphy 	if (cfg->ias > 48 || cfg->oas > 40)
1035d08d42deSRob Herring 		return NULL;
1036d08d42deSRob Herring 
1037d08d42deSRob Herring 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1038d08d42deSRob Herring 
103952f325f4SRobin Murphy 	data = arm_lpae_alloc_pgtable(cfg);
104052f325f4SRobin Murphy 	if (!data)
104152f325f4SRobin Murphy 		return NULL;
1042d08d42deSRob Herring 
10431be08f45SRobin Murphy 	/* Mali seems to need a full 4-level table regardless of IAS */
1044594ab90fSRobin Murphy 	if (data->start_level > 0) {
1045594ab90fSRobin Murphy 		data->start_level = 0;
1046c79278c1SRobin Murphy 		data->pgd_bits = 0;
10471be08f45SRobin Murphy 	}
104852f325f4SRobin Murphy 	/*
104952f325f4SRobin Murphy 	 * MEMATTR: Mali has no actual notion of a non-cacheable type, so the
105052f325f4SRobin Murphy 	 * best we can do is mimic the out-of-tree driver and hope that the
105152f325f4SRobin Murphy 	 * "implementation-defined caching policy" is good enough. Similarly,
105252f325f4SRobin Murphy 	 * we'll use it for the sake of a valid attribute for our 'device'
105352f325f4SRobin Murphy 	 * index, although callers should never request that in practice.
105452f325f4SRobin Murphy 	 */
105552f325f4SRobin Murphy 	cfg->arm_mali_lpae_cfg.memattr =
105652f325f4SRobin Murphy 		(ARM_MALI_LPAE_MEMATTR_IMP_DEF
105752f325f4SRobin Murphy 		 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
105852f325f4SRobin Murphy 		(ARM_MALI_LPAE_MEMATTR_WRITE_ALLOC
105952f325f4SRobin Murphy 		 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
106052f325f4SRobin Murphy 		(ARM_MALI_LPAE_MEMATTR_IMP_DEF
106152f325f4SRobin Murphy 		 << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
106252f325f4SRobin Murphy 
1063c79278c1SRobin Murphy 	data->pgd = __arm_lpae_alloc_pages(ARM_LPAE_PGD_SIZE(data), GFP_KERNEL,
1064c79278c1SRobin Murphy 					   cfg);
106552f325f4SRobin Murphy 	if (!data->pgd)
106652f325f4SRobin Murphy 		goto out_free_data;
106752f325f4SRobin Murphy 
106852f325f4SRobin Murphy 	/* Ensure the empty pgd is visible before TRANSTAB can be written */
106952f325f4SRobin Murphy 	wmb();
107052f325f4SRobin Murphy 
107152f325f4SRobin Murphy 	cfg->arm_mali_lpae_cfg.transtab = virt_to_phys(data->pgd) |
1072d08d42deSRob Herring 					  ARM_MALI_LPAE_TTBR_READ_INNER |
1073d08d42deSRob Herring 					  ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
1074728da60dSRobin Murphy 	if (cfg->coherent_walk)
1075728da60dSRobin Murphy 		cfg->arm_mali_lpae_cfg.transtab |= ARM_MALI_LPAE_TTBR_SHARE_OUTER;
1076728da60dSRobin Murphy 
107752f325f4SRobin Murphy 	return &data->iop;
1078d08d42deSRob Herring 
107952f325f4SRobin Murphy out_free_data:
108052f325f4SRobin Murphy 	kfree(data);
108152f325f4SRobin Murphy 	return NULL;
1082d08d42deSRob Herring }
1083d08d42deSRob Herring 
1084e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1085e1d3c0fdSWill Deacon 	.alloc	= arm_64_lpae_alloc_pgtable_s1,
1086e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1087e1d3c0fdSWill Deacon };
1088e1d3c0fdSWill Deacon 
1089e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1090e1d3c0fdSWill Deacon 	.alloc	= arm_64_lpae_alloc_pgtable_s2,
1091e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1092e1d3c0fdSWill Deacon };
1093e1d3c0fdSWill Deacon 
1094e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1095e1d3c0fdSWill Deacon 	.alloc	= arm_32_lpae_alloc_pgtable_s1,
1096e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1097e1d3c0fdSWill Deacon };
1098e1d3c0fdSWill Deacon 
1099e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1100e1d3c0fdSWill Deacon 	.alloc	= arm_32_lpae_alloc_pgtable_s2,
1101e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1102e1d3c0fdSWill Deacon };
1103fe4b991dSWill Deacon 
1104d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1105d08d42deSRob Herring 	.alloc	= arm_mali_lpae_alloc_pgtable,
1106d08d42deSRob Herring 	.free	= arm_lpae_free_pgtable,
1107d08d42deSRob Herring };
1108d08d42deSRob Herring 
1109fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1110fe4b991dSWill Deacon 
1111b5813c16SRobin Murphy static struct io_pgtable_cfg *cfg_cookie __initdata;
1112fe4b991dSWill Deacon 
1113b5813c16SRobin Murphy static void __init dummy_tlb_flush_all(void *cookie)
1114fe4b991dSWill Deacon {
1115fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
1116fe4b991dSWill Deacon }
1117fe4b991dSWill Deacon 
1118b5813c16SRobin Murphy static void __init dummy_tlb_flush(unsigned long iova, size_t size,
1119b5813c16SRobin Murphy 				   size_t granule, void *cookie)
1120fe4b991dSWill Deacon {
1121fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
1122fe4b991dSWill Deacon 	WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1123fe4b991dSWill Deacon }
1124fe4b991dSWill Deacon 
1125b5813c16SRobin Murphy static void __init dummy_tlb_add_page(struct iommu_iotlb_gather *gather,
1126b5813c16SRobin Murphy 				      unsigned long iova, size_t granule,
1127b5813c16SRobin Murphy 				      void *cookie)
112810b7a7d9SWill Deacon {
1129abfd6fe0SWill Deacon 	dummy_tlb_flush(iova, granule, granule, cookie);
113010b7a7d9SWill Deacon }
113110b7a7d9SWill Deacon 
1132298f7889SWill Deacon static const struct iommu_flush_ops dummy_tlb_ops __initconst = {
1133fe4b991dSWill Deacon 	.tlb_flush_all	= dummy_tlb_flush_all,
113410b7a7d9SWill Deacon 	.tlb_flush_walk	= dummy_tlb_flush,
1135abfd6fe0SWill Deacon 	.tlb_add_page	= dummy_tlb_add_page,
1136fe4b991dSWill Deacon };
1137fe4b991dSWill Deacon 
1138fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1139fe4b991dSWill Deacon {
1140fe4b991dSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1141fe4b991dSWill Deacon 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
1142fe4b991dSWill Deacon 
1143fe4b991dSWill Deacon 	pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1144fe4b991dSWill Deacon 		cfg->pgsize_bitmap, cfg->ias);
11455fb190b0SRobin Murphy 	pr_err("data: %d levels, 0x%zx pgd_size, %u pg_shift, %u bits_per_level, pgd @ %p\n",
1146c79278c1SRobin Murphy 		ARM_LPAE_MAX_LEVELS - data->start_level, ARM_LPAE_PGD_SIZE(data),
11475fb190b0SRobin Murphy 		ilog2(ARM_LPAE_GRANULE(data)), data->bits_per_level, data->pgd);
1148fe4b991dSWill Deacon }
1149fe4b991dSWill Deacon 
1150fe4b991dSWill Deacon #define __FAIL(ops, i)	({						\
1151fe4b991dSWill Deacon 		WARN(1, "selftest: test failed for fmt idx %d\n", (i));	\
1152fe4b991dSWill Deacon 		arm_lpae_dump_ops(ops);					\
1153fe4b991dSWill Deacon 		selftest_running = false;				\
1154fe4b991dSWill Deacon 		-EFAULT;						\
1155fe4b991dSWill Deacon })
1156fe4b991dSWill Deacon 
1157fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1158fe4b991dSWill Deacon {
11599062c1d0SChristophe JAILLET 	static const enum io_pgtable_fmt fmts[] __initconst = {
1160fe4b991dSWill Deacon 		ARM_64_LPAE_S1,
1161fe4b991dSWill Deacon 		ARM_64_LPAE_S2,
1162fe4b991dSWill Deacon 	};
1163fe4b991dSWill Deacon 
1164fe4b991dSWill Deacon 	int i, j;
1165fe4b991dSWill Deacon 	unsigned long iova;
1166fe4b991dSWill Deacon 	size_t size;
1167fe4b991dSWill Deacon 	struct io_pgtable_ops *ops;
1168fe4b991dSWill Deacon 
1169fe4b991dSWill Deacon 	selftest_running = true;
1170fe4b991dSWill Deacon 
1171fe4b991dSWill Deacon 	for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1172fe4b991dSWill Deacon 		cfg_cookie = cfg;
1173fe4b991dSWill Deacon 		ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1174fe4b991dSWill Deacon 		if (!ops) {
1175fe4b991dSWill Deacon 			pr_err("selftest: failed to allocate io pgtable ops\n");
1176fe4b991dSWill Deacon 			return -ENOMEM;
1177fe4b991dSWill Deacon 		}
1178fe4b991dSWill Deacon 
1179fe4b991dSWill Deacon 		/*
1180fe4b991dSWill Deacon 		 * Initial sanity checks.
1181fe4b991dSWill Deacon 		 * Empty page tables shouldn't provide any translations.
1182fe4b991dSWill Deacon 		 */
1183fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, 42))
1184fe4b991dSWill Deacon 			return __FAIL(ops, i);
1185fe4b991dSWill Deacon 
1186fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_1G + 42))
1187fe4b991dSWill Deacon 			return __FAIL(ops, i);
1188fe4b991dSWill Deacon 
1189fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_2G + 42))
1190fe4b991dSWill Deacon 			return __FAIL(ops, i);
1191fe4b991dSWill Deacon 
1192fe4b991dSWill Deacon 		/*
1193fe4b991dSWill Deacon 		 * Distinct mappings of different granule sizes.
1194fe4b991dSWill Deacon 		 */
1195fe4b991dSWill Deacon 		iova = 0;
11964ae8a5c5SKefeng Wang 		for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1197fe4b991dSWill Deacon 			size = 1UL << j;
1198fe4b991dSWill Deacon 
1199fe4b991dSWill Deacon 			if (ops->map(ops, iova, iova, size, IOMMU_READ |
1200fe4b991dSWill Deacon 							    IOMMU_WRITE |
1201fe4b991dSWill Deacon 							    IOMMU_NOEXEC |
1202f34ce7a7SBaolin Wang 							    IOMMU_CACHE, GFP_KERNEL))
1203fe4b991dSWill Deacon 				return __FAIL(ops, i);
1204fe4b991dSWill Deacon 
1205fe4b991dSWill Deacon 			/* Overlapping mappings */
1206fe4b991dSWill Deacon 			if (!ops->map(ops, iova, iova + size, size,
1207f34ce7a7SBaolin Wang 				      IOMMU_READ | IOMMU_NOEXEC, GFP_KERNEL))
1208fe4b991dSWill Deacon 				return __FAIL(ops, i);
1209fe4b991dSWill Deacon 
1210fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1211fe4b991dSWill Deacon 				return __FAIL(ops, i);
1212fe4b991dSWill Deacon 
1213fe4b991dSWill Deacon 			iova += SZ_1G;
1214fe4b991dSWill Deacon 		}
1215fe4b991dSWill Deacon 
1216fe4b991dSWill Deacon 		/* Partial unmap */
1217fe4b991dSWill Deacon 		size = 1UL << __ffs(cfg->pgsize_bitmap);
1218a2d3a382SWill Deacon 		if (ops->unmap(ops, SZ_1G + size, size, NULL) != size)
1219fe4b991dSWill Deacon 			return __FAIL(ops, i);
1220fe4b991dSWill Deacon 
1221fe4b991dSWill Deacon 		/* Remap of partial unmap */
1222f34ce7a7SBaolin Wang 		if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ, GFP_KERNEL))
1223fe4b991dSWill Deacon 			return __FAIL(ops, i);
1224fe4b991dSWill Deacon 
1225fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1226fe4b991dSWill Deacon 			return __FAIL(ops, i);
1227fe4b991dSWill Deacon 
1228fe4b991dSWill Deacon 		/* Full unmap */
1229fe4b991dSWill Deacon 		iova = 0;
1230f793b13eSYueHaibing 		for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1231fe4b991dSWill Deacon 			size = 1UL << j;
1232fe4b991dSWill Deacon 
1233a2d3a382SWill Deacon 			if (ops->unmap(ops, iova, size, NULL) != size)
1234fe4b991dSWill Deacon 				return __FAIL(ops, i);
1235fe4b991dSWill Deacon 
1236fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42))
1237fe4b991dSWill Deacon 				return __FAIL(ops, i);
1238fe4b991dSWill Deacon 
1239fe4b991dSWill Deacon 			/* Remap full block */
1240f34ce7a7SBaolin Wang 			if (ops->map(ops, iova, iova, size, IOMMU_WRITE, GFP_KERNEL))
1241fe4b991dSWill Deacon 				return __FAIL(ops, i);
1242fe4b991dSWill Deacon 
1243fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1244fe4b991dSWill Deacon 				return __FAIL(ops, i);
1245fe4b991dSWill Deacon 
1246fe4b991dSWill Deacon 			iova += SZ_1G;
1247fe4b991dSWill Deacon 		}
1248fe4b991dSWill Deacon 
1249fe4b991dSWill Deacon 		free_io_pgtable_ops(ops);
1250fe4b991dSWill Deacon 	}
1251fe4b991dSWill Deacon 
1252fe4b991dSWill Deacon 	selftest_running = false;
1253fe4b991dSWill Deacon 	return 0;
1254fe4b991dSWill Deacon }
1255fe4b991dSWill Deacon 
1256fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void)
1257fe4b991dSWill Deacon {
12589062c1d0SChristophe JAILLET 	static const unsigned long pgsize[] __initconst = {
1259fe4b991dSWill Deacon 		SZ_4K | SZ_2M | SZ_1G,
1260fe4b991dSWill Deacon 		SZ_16K | SZ_32M,
1261fe4b991dSWill Deacon 		SZ_64K | SZ_512M,
1262fe4b991dSWill Deacon 	};
1263fe4b991dSWill Deacon 
12649062c1d0SChristophe JAILLET 	static const unsigned int ias[] __initconst = {
1265fe4b991dSWill Deacon 		32, 36, 40, 42, 44, 48,
1266fe4b991dSWill Deacon 	};
1267fe4b991dSWill Deacon 
1268fe4b991dSWill Deacon 	int i, j, pass = 0, fail = 0;
1269fe4b991dSWill Deacon 	struct io_pgtable_cfg cfg = {
1270fe4b991dSWill Deacon 		.tlb = &dummy_tlb_ops,
1271fe4b991dSWill Deacon 		.oas = 48,
12724f41845bSWill Deacon 		.coherent_walk = true,
1273fe4b991dSWill Deacon 	};
1274fe4b991dSWill Deacon 
1275fe4b991dSWill Deacon 	for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1276fe4b991dSWill Deacon 		for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1277fe4b991dSWill Deacon 			cfg.pgsize_bitmap = pgsize[i];
1278fe4b991dSWill Deacon 			cfg.ias = ias[j];
1279fe4b991dSWill Deacon 			pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1280fe4b991dSWill Deacon 				pgsize[i], ias[j]);
1281fe4b991dSWill Deacon 			if (arm_lpae_run_tests(&cfg))
1282fe4b991dSWill Deacon 				fail++;
1283fe4b991dSWill Deacon 			else
1284fe4b991dSWill Deacon 				pass++;
1285fe4b991dSWill Deacon 		}
1286fe4b991dSWill Deacon 	}
1287fe4b991dSWill Deacon 
1288fe4b991dSWill Deacon 	pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1289fe4b991dSWill Deacon 	return fail ? -EFAULT : 0;
1290fe4b991dSWill Deacon }
1291fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests);
1292fe4b991dSWill Deacon #endif
1293