xref: /openbmc/linux/drivers/iommu/io-pgtable-arm.c (revision 90ec7a76cc4ba65bfedeb8621cba09cd5a317d8f)
1e1d3c0fdSWill Deacon /*
2e1d3c0fdSWill Deacon  * CPU-agnostic ARM page table allocator.
3e1d3c0fdSWill Deacon  *
4e1d3c0fdSWill Deacon  * This program is free software; you can redistribute it and/or modify
5e1d3c0fdSWill Deacon  * it under the terms of the GNU General Public License version 2 as
6e1d3c0fdSWill Deacon  * published by the Free Software Foundation.
7e1d3c0fdSWill Deacon  *
8e1d3c0fdSWill Deacon  * This program is distributed in the hope that it will be useful,
9e1d3c0fdSWill Deacon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10e1d3c0fdSWill Deacon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11e1d3c0fdSWill Deacon  * GNU General Public License for more details.
12e1d3c0fdSWill Deacon  *
13e1d3c0fdSWill Deacon  * You should have received a copy of the GNU General Public License
14e1d3c0fdSWill Deacon  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15e1d3c0fdSWill Deacon  *
16e1d3c0fdSWill Deacon  * Copyright (C) 2014 ARM Limited
17e1d3c0fdSWill Deacon  *
18e1d3c0fdSWill Deacon  * Author: Will Deacon <will.deacon@arm.com>
19e1d3c0fdSWill Deacon  */
20e1d3c0fdSWill Deacon 
21e1d3c0fdSWill Deacon #define pr_fmt(fmt)	"arm-lpae io-pgtable: " fmt
22e1d3c0fdSWill Deacon 
232c3d273eSRobin Murphy #include <linux/atomic.h>
246c89928fSRobin Murphy #include <linux/bitops.h>
25b77cf11fSRob Herring #include <linux/io-pgtable.h>
26e1d3c0fdSWill Deacon #include <linux/iommu.h>
27e1d3c0fdSWill Deacon #include <linux/kernel.h>
28e1d3c0fdSWill Deacon #include <linux/sizes.h>
29e1d3c0fdSWill Deacon #include <linux/slab.h>
30e1d3c0fdSWill Deacon #include <linux/types.h>
318f6aff98SLada Trimasova #include <linux/dma-mapping.h>
32e1d3c0fdSWill Deacon 
3387a91b15SRobin Murphy #include <asm/barrier.h>
3487a91b15SRobin Murphy 
356c89928fSRobin Murphy #define ARM_LPAE_MAX_ADDR_BITS		52
36e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES	16
37e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS		4
38e1d3c0fdSWill Deacon 
39e1d3c0fdSWill Deacon /* Struct accessors */
40e1d3c0fdSWill Deacon #define io_pgtable_to_data(x)						\
41e1d3c0fdSWill Deacon 	container_of((x), struct arm_lpae_io_pgtable, iop)
42e1d3c0fdSWill Deacon 
43e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x)					\
44e1d3c0fdSWill Deacon 	io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
45e1d3c0fdSWill Deacon 
46e1d3c0fdSWill Deacon /*
47e1d3c0fdSWill Deacon  * For consistency with the architecture, we always consider
48e1d3c0fdSWill Deacon  * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
49e1d3c0fdSWill Deacon  */
50e1d3c0fdSWill Deacon #define ARM_LPAE_START_LVL(d)		(ARM_LPAE_MAX_LEVELS - (d)->levels)
51e1d3c0fdSWill Deacon 
52e1d3c0fdSWill Deacon /*
53e1d3c0fdSWill Deacon  * Calculate the right shift amount to get to the portion describing level l
54e1d3c0fdSWill Deacon  * in a virtual address mapped by the pagetable in d.
55e1d3c0fdSWill Deacon  */
56e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d)						\
57e1d3c0fdSWill Deacon 	((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1))		\
58e1d3c0fdSWill Deacon 	  * (d)->bits_per_level) + (d)->pg_shift)
59e1d3c0fdSWill Deacon 
6006c610e8SRobin Murphy #define ARM_LPAE_GRANULE(d)		(1UL << (d)->pg_shift)
6106c610e8SRobin Murphy 
62367bd978SWill Deacon #define ARM_LPAE_PAGES_PER_PGD(d)					\
6306c610e8SRobin Murphy 	DIV_ROUND_UP((d)->pgd_size, ARM_LPAE_GRANULE(d))
64e1d3c0fdSWill Deacon 
65e1d3c0fdSWill Deacon /*
66e1d3c0fdSWill Deacon  * Calculate the index at level l used to map virtual address a using the
67e1d3c0fdSWill Deacon  * pagetable in d.
68e1d3c0fdSWill Deacon  */
69e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d)						\
70e1d3c0fdSWill Deacon 	((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
71e1d3c0fdSWill Deacon 
72e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d)						\
73367bd978SWill Deacon 	(((u64)(a) >> ARM_LPAE_LVL_SHIFT(l,d)) &			\
74e1d3c0fdSWill Deacon 	 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
75e1d3c0fdSWill Deacon 
76e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */
77e1d3c0fdSWill Deacon #define ARM_LPAE_BLOCK_SIZE(l,d)					\
78022f4e4fSRobin Murphy 	(1ULL << (ilog2(sizeof(arm_lpae_iopte)) +			\
79e1d3c0fdSWill Deacon 		((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
80e1d3c0fdSWill Deacon 
81e1d3c0fdSWill Deacon /* Page table bits */
82e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT		0
83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK		0x3
84e1d3c0fdSWill Deacon 
85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK		1
86e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE		3
87e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE		3
88e1d3c0fdSWill Deacon 
896c89928fSRobin Murphy #define ARM_LPAE_PTE_ADDR_MASK		GENMASK_ULL(47,12)
906c89928fSRobin Murphy 
91c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NSTABLE		(((arm_lpae_iopte)1) << 63)
92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN			(((arm_lpae_iopte)3) << 53)
93e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF			(((arm_lpae_iopte)1) << 10)
94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS		(((arm_lpae_iopte)0) << 8)
95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS		(((arm_lpae_iopte)2) << 8)
96e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS		(((arm_lpae_iopte)3) << 8)
97c896c132SLaurent Pinchart #define ARM_LPAE_PTE_NS			(((arm_lpae_iopte)1) << 5)
98e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID		(((arm_lpae_iopte)1) << 0)
99e1d3c0fdSWill Deacon 
100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK	(((arm_lpae_iopte)0x3ff) << 2)
101e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */
102e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK	(((arm_lpae_iopte)6) << 52)
103e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK		(ARM_LPAE_PTE_ATTR_LO_MASK |	\
104e1d3c0fdSWill Deacon 					 ARM_LPAE_PTE_ATTR_HI_MASK)
1052c3d273eSRobin Murphy /* Software bit for solving coherency races */
1062c3d273eSRobin Murphy #define ARM_LPAE_PTE_SW_SYNC		(((arm_lpae_iopte)1) << 55)
107e1d3c0fdSWill Deacon 
108e1d3c0fdSWill Deacon /* Stage-1 PTE */
109e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV		(((arm_lpae_iopte)1) << 6)
110e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY		(((arm_lpae_iopte)2) << 6)
111e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT	2
112e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG			(((arm_lpae_iopte)1) << 11)
113e1d3c0fdSWill Deacon 
114e1d3c0fdSWill Deacon /* Stage-2 PTE */
115e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT		(((arm_lpae_iopte)0) << 6)
116e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ		(((arm_lpae_iopte)1) << 6)
117e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE		(((arm_lpae_iopte)2) << 6)
118e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB	(((arm_lpae_iopte)0xf) << 2)
119e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC		(((arm_lpae_iopte)0x5) << 2)
120e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV	(((arm_lpae_iopte)0x1) << 2)
121e1d3c0fdSWill Deacon 
122e1d3c0fdSWill Deacon /* Register bits */
123e1d3c0fdSWill Deacon #define ARM_32_LPAE_TCR_EAE		(1 << 31)
124e1d3c0fdSWill Deacon #define ARM_64_LPAE_S2_TCR_RES1		(1 << 31)
125e1d3c0fdSWill Deacon 
12663979b8dSWill Deacon #define ARM_LPAE_TCR_EPD1		(1 << 23)
12763979b8dSWill Deacon 
128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_4K		(0 << 14)
129e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_64K		(1 << 14)
130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_16K		(2 << 14)
131e1d3c0fdSWill Deacon 
132e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_SHIFT		12
133e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_MASK		0x3
134e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS		0
135e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS		2
136e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS		3
137e1d3c0fdSWill Deacon 
138e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_ORGN0_SHIFT	10
139e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IRGN0_SHIFT	8
140e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_MASK		0x3
141e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC		0
142e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA		1
143e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT		2
144e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB		3
145e1d3c0fdSWill Deacon 
146e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_SHIFT		6
147e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_MASK		0x3
148e1d3c0fdSWill Deacon 
149e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT		0
150e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SZ_MASK		0xf
151e1d3c0fdSWill Deacon 
152e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_SHIFT		16
153e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_MASK		0x7
154e1d3c0fdSWill Deacon 
155e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_SHIFT		32
156e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_MASK		0x7
157e1d3c0fdSWill Deacon 
158e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT		0x0ULL
159e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT		0x1ULL
160e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT		0x2ULL
161e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT		0x3ULL
162e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT		0x4ULL
163e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT		0x5ULL
1646c89928fSRobin Murphy #define ARM_LPAE_TCR_PS_52_BIT		0x6ULL
165e1d3c0fdSWill Deacon 
166e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n)	((n) << 3)
167e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK		0xff
168e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE	0x04
169e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC		0x44
170*90ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_INC_OWBRWA	0xf4
171e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA	0xff
172e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC	0
173e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE	1
174e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV	2
175*90ec7a76SVivek Gautam #define ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE	3
176e1d3c0fdSWill Deacon 
177d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_ADRMODE_TABLE (3u << 0)
178d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_READ_INNER	BIT(2)
179d08d42deSRob Herring #define ARM_MALI_LPAE_TTBR_SHARE_OUTER	BIT(4)
180d08d42deSRob Herring 
181e1d3c0fdSWill Deacon /* IOPTE accessors */
1826c89928fSRobin Murphy #define iopte_deref(pte,d) __va(iopte_to_paddr(pte, d))
183e1d3c0fdSWill Deacon 
184e1d3c0fdSWill Deacon #define iopte_type(pte,l)					\
185e1d3c0fdSWill Deacon 	(((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
186e1d3c0fdSWill Deacon 
187e1d3c0fdSWill Deacon #define iopte_prot(pte)	((pte) & ARM_LPAE_PTE_ATTR_MASK)
188e1d3c0fdSWill Deacon 
189e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable {
190e1d3c0fdSWill Deacon 	struct io_pgtable	iop;
191e1d3c0fdSWill Deacon 
192e1d3c0fdSWill Deacon 	int			levels;
193e1d3c0fdSWill Deacon 	size_t			pgd_size;
194e1d3c0fdSWill Deacon 	unsigned long		pg_shift;
195e1d3c0fdSWill Deacon 	unsigned long		bits_per_level;
196e1d3c0fdSWill Deacon 
197e1d3c0fdSWill Deacon 	void			*pgd;
198e1d3c0fdSWill Deacon };
199e1d3c0fdSWill Deacon 
200e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte;
201e1d3c0fdSWill Deacon 
202d08d42deSRob Herring static inline bool iopte_leaf(arm_lpae_iopte pte, int lvl,
203d08d42deSRob Herring 			      enum io_pgtable_fmt fmt)
204d08d42deSRob Herring {
205d08d42deSRob Herring 	if (lvl == (ARM_LPAE_MAX_LEVELS - 1) && fmt != ARM_MALI_LPAE)
206d08d42deSRob Herring 		return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_PAGE;
207d08d42deSRob Herring 
208d08d42deSRob Herring 	return iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_BLOCK;
209d08d42deSRob Herring }
210d08d42deSRob Herring 
2116c89928fSRobin Murphy static arm_lpae_iopte paddr_to_iopte(phys_addr_t paddr,
2126c89928fSRobin Murphy 				     struct arm_lpae_io_pgtable *data)
2136c89928fSRobin Murphy {
2146c89928fSRobin Murphy 	arm_lpae_iopte pte = paddr;
2156c89928fSRobin Murphy 
2166c89928fSRobin Murphy 	/* Of the bits which overlap, either 51:48 or 15:12 are always RES0 */
2176c89928fSRobin Murphy 	return (pte | (pte >> (48 - 12))) & ARM_LPAE_PTE_ADDR_MASK;
2186c89928fSRobin Murphy }
2196c89928fSRobin Murphy 
2206c89928fSRobin Murphy static phys_addr_t iopte_to_paddr(arm_lpae_iopte pte,
2216c89928fSRobin Murphy 				  struct arm_lpae_io_pgtable *data)
2226c89928fSRobin Murphy {
22378688059SRobin Murphy 	u64 paddr = pte & ARM_LPAE_PTE_ADDR_MASK;
2246c89928fSRobin Murphy 
2256c89928fSRobin Murphy 	if (data->pg_shift < 16)
2266c89928fSRobin Murphy 		return paddr;
2276c89928fSRobin Murphy 
2286c89928fSRobin Murphy 	/* Rotate the packed high-order bits back to the top */
2296c89928fSRobin Murphy 	return (paddr | (paddr << (48 - 12))) & (ARM_LPAE_PTE_ADDR_MASK << 4);
2306c89928fSRobin Murphy }
2316c89928fSRobin Murphy 
232fe4b991dSWill Deacon static bool selftest_running = false;
233fe4b991dSWill Deacon 
234ffcb6d16SRobin Murphy static dma_addr_t __arm_lpae_dma_addr(void *pages)
235f8d54961SRobin Murphy {
236ffcb6d16SRobin Murphy 	return (dma_addr_t)virt_to_phys(pages);
237f8d54961SRobin Murphy }
238f8d54961SRobin Murphy 
239f8d54961SRobin Murphy static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
240f8d54961SRobin Murphy 				    struct io_pgtable_cfg *cfg)
241f8d54961SRobin Murphy {
242f8d54961SRobin Murphy 	struct device *dev = cfg->iommu_dev;
2434b123757SRobin Murphy 	int order = get_order(size);
2444b123757SRobin Murphy 	struct page *p;
245f8d54961SRobin Murphy 	dma_addr_t dma;
2464b123757SRobin Murphy 	void *pages;
247f8d54961SRobin Murphy 
2484b123757SRobin Murphy 	VM_BUG_ON((gfp & __GFP_HIGHMEM));
249fac83d29SJean-Philippe Brucker 	p = alloc_pages_node(dev ? dev_to_node(dev) : NUMA_NO_NODE,
250fac83d29SJean-Philippe Brucker 			     gfp | __GFP_ZERO, order);
2514b123757SRobin Murphy 	if (!p)
252f8d54961SRobin Murphy 		return NULL;
253f8d54961SRobin Murphy 
2544b123757SRobin Murphy 	pages = page_address(p);
25581b3c252SRobin Murphy 	if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA)) {
256f8d54961SRobin Murphy 		dma = dma_map_single(dev, pages, size, DMA_TO_DEVICE);
257f8d54961SRobin Murphy 		if (dma_mapping_error(dev, dma))
258f8d54961SRobin Murphy 			goto out_free;
259f8d54961SRobin Murphy 		/*
260f8d54961SRobin Murphy 		 * We depend on the IOMMU being able to work with any physical
261ffcb6d16SRobin Murphy 		 * address directly, so if the DMA layer suggests otherwise by
262ffcb6d16SRobin Murphy 		 * translating or truncating them, that bodes very badly...
263f8d54961SRobin Murphy 		 */
264ffcb6d16SRobin Murphy 		if (dma != virt_to_phys(pages))
265f8d54961SRobin Murphy 			goto out_unmap;
266f8d54961SRobin Murphy 	}
267f8d54961SRobin Murphy 
268f8d54961SRobin Murphy 	return pages;
269f8d54961SRobin Murphy 
270f8d54961SRobin Murphy out_unmap:
271f8d54961SRobin Murphy 	dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
272f8d54961SRobin Murphy 	dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
273f8d54961SRobin Murphy out_free:
2744b123757SRobin Murphy 	__free_pages(p, order);
275f8d54961SRobin Murphy 	return NULL;
276f8d54961SRobin Murphy }
277f8d54961SRobin Murphy 
278f8d54961SRobin Murphy static void __arm_lpae_free_pages(void *pages, size_t size,
279f8d54961SRobin Murphy 				  struct io_pgtable_cfg *cfg)
280f8d54961SRobin Murphy {
28181b3c252SRobin Murphy 	if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA))
282ffcb6d16SRobin Murphy 		dma_unmap_single(cfg->iommu_dev, __arm_lpae_dma_addr(pages),
283f8d54961SRobin Murphy 				 size, DMA_TO_DEVICE);
2844b123757SRobin Murphy 	free_pages((unsigned long)pages, get_order(size));
285f8d54961SRobin Murphy }
286f8d54961SRobin Murphy 
2872c3d273eSRobin Murphy static void __arm_lpae_sync_pte(arm_lpae_iopte *ptep,
2882c3d273eSRobin Murphy 				struct io_pgtable_cfg *cfg)
2892c3d273eSRobin Murphy {
2902c3d273eSRobin Murphy 	dma_sync_single_for_device(cfg->iommu_dev, __arm_lpae_dma_addr(ptep),
2912c3d273eSRobin Murphy 				   sizeof(*ptep), DMA_TO_DEVICE);
2922c3d273eSRobin Murphy }
2932c3d273eSRobin Murphy 
294f8d54961SRobin Murphy static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
29587a91b15SRobin Murphy 			       struct io_pgtable_cfg *cfg)
296f8d54961SRobin Murphy {
297f8d54961SRobin Murphy 	*ptep = pte;
298f8d54961SRobin Murphy 
29981b3c252SRobin Murphy 	if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA))
3002c3d273eSRobin Murphy 		__arm_lpae_sync_pte(ptep, cfg);
301f8d54961SRobin Murphy }
302f8d54961SRobin Murphy 
303193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
304cf27ec93SWill Deacon 			       unsigned long iova, size_t size, int lvl,
305cf27ec93SWill Deacon 			       arm_lpae_iopte *ptep);
306cf27ec93SWill Deacon 
307fb3a9579SRobin Murphy static void __arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
308fb3a9579SRobin Murphy 				phys_addr_t paddr, arm_lpae_iopte prot,
309fb3a9579SRobin Murphy 				int lvl, arm_lpae_iopte *ptep)
310fb3a9579SRobin Murphy {
311fb3a9579SRobin Murphy 	arm_lpae_iopte pte = prot;
312fb3a9579SRobin Murphy 
313fb3a9579SRobin Murphy 	if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS)
314fb3a9579SRobin Murphy 		pte |= ARM_LPAE_PTE_NS;
315fb3a9579SRobin Murphy 
316d08d42deSRob Herring 	if (data->iop.fmt != ARM_MALI_LPAE && lvl == ARM_LPAE_MAX_LEVELS - 1)
317fb3a9579SRobin Murphy 		pte |= ARM_LPAE_PTE_TYPE_PAGE;
318fb3a9579SRobin Murphy 	else
319fb3a9579SRobin Murphy 		pte |= ARM_LPAE_PTE_TYPE_BLOCK;
320fb3a9579SRobin Murphy 
321d08d42deSRob Herring 	if (data->iop.fmt != ARM_MALI_LPAE)
322d08d42deSRob Herring 		pte |= ARM_LPAE_PTE_AF;
323d08d42deSRob Herring 	pte |= ARM_LPAE_PTE_SH_IS;
3246c89928fSRobin Murphy 	pte |= paddr_to_iopte(paddr, data);
325fb3a9579SRobin Murphy 
326fb3a9579SRobin Murphy 	__arm_lpae_set_pte(ptep, pte, &data->iop.cfg);
327fb3a9579SRobin Murphy }
328fb3a9579SRobin Murphy 
329e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
330e1d3c0fdSWill Deacon 			     unsigned long iova, phys_addr_t paddr,
331e1d3c0fdSWill Deacon 			     arm_lpae_iopte prot, int lvl,
332e1d3c0fdSWill Deacon 			     arm_lpae_iopte *ptep)
333e1d3c0fdSWill Deacon {
334fb3a9579SRobin Murphy 	arm_lpae_iopte pte = *ptep;
335e1d3c0fdSWill Deacon 
336d08d42deSRob Herring 	if (iopte_leaf(pte, lvl, data->iop.fmt)) {
337cf27ec93SWill Deacon 		/* We require an unmap first */
338fe4b991dSWill Deacon 		WARN_ON(!selftest_running);
339e1d3c0fdSWill Deacon 		return -EEXIST;
340fb3a9579SRobin Murphy 	} else if (iopte_type(pte, lvl) == ARM_LPAE_PTE_TYPE_TABLE) {
341cf27ec93SWill Deacon 		/*
342cf27ec93SWill Deacon 		 * We need to unmap and free the old table before
343cf27ec93SWill Deacon 		 * overwriting it with a block entry.
344cf27ec93SWill Deacon 		 */
345cf27ec93SWill Deacon 		arm_lpae_iopte *tblp;
346cf27ec93SWill Deacon 		size_t sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
347cf27ec93SWill Deacon 
348cf27ec93SWill Deacon 		tblp = ptep - ARM_LPAE_LVL_IDX(iova, lvl, data);
349cf27ec93SWill Deacon 		if (WARN_ON(__arm_lpae_unmap(data, iova, sz, lvl, tblp) != sz))
350cf27ec93SWill Deacon 			return -EINVAL;
351fe4b991dSWill Deacon 	}
352e1d3c0fdSWill Deacon 
353fb3a9579SRobin Murphy 	__arm_lpae_init_pte(data, paddr, prot, lvl, ptep);
354e1d3c0fdSWill Deacon 	return 0;
355e1d3c0fdSWill Deacon }
356e1d3c0fdSWill Deacon 
357fb3a9579SRobin Murphy static arm_lpae_iopte arm_lpae_install_table(arm_lpae_iopte *table,
358fb3a9579SRobin Murphy 					     arm_lpae_iopte *ptep,
3592c3d273eSRobin Murphy 					     arm_lpae_iopte curr,
360fb3a9579SRobin Murphy 					     struct io_pgtable_cfg *cfg)
361fb3a9579SRobin Murphy {
3622c3d273eSRobin Murphy 	arm_lpae_iopte old, new;
363fb3a9579SRobin Murphy 
364fb3a9579SRobin Murphy 	new = __pa(table) | ARM_LPAE_PTE_TYPE_TABLE;
365fb3a9579SRobin Murphy 	if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
366fb3a9579SRobin Murphy 		new |= ARM_LPAE_PTE_NSTABLE;
367fb3a9579SRobin Murphy 
36877f34458SWill Deacon 	/*
36977f34458SWill Deacon 	 * Ensure the table itself is visible before its PTE can be.
37077f34458SWill Deacon 	 * Whilst we could get away with cmpxchg64_release below, this
37177f34458SWill Deacon 	 * doesn't have any ordering semantics when !CONFIG_SMP.
37277f34458SWill Deacon 	 */
37377f34458SWill Deacon 	dma_wmb();
3742c3d273eSRobin Murphy 
3752c3d273eSRobin Murphy 	old = cmpxchg64_relaxed(ptep, curr, new);
3762c3d273eSRobin Murphy 
3772c3d273eSRobin Murphy 	if ((cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) ||
3782c3d273eSRobin Murphy 	    (old & ARM_LPAE_PTE_SW_SYNC))
3792c3d273eSRobin Murphy 		return old;
3802c3d273eSRobin Murphy 
3812c3d273eSRobin Murphy 	/* Even if it's not ours, there's no point waiting; just kick it */
3822c3d273eSRobin Murphy 	__arm_lpae_sync_pte(ptep, cfg);
3832c3d273eSRobin Murphy 	if (old == curr)
3842c3d273eSRobin Murphy 		WRITE_ONCE(*ptep, new | ARM_LPAE_PTE_SW_SYNC);
3852c3d273eSRobin Murphy 
3862c3d273eSRobin Murphy 	return old;
387fb3a9579SRobin Murphy }
388fb3a9579SRobin Murphy 
389e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
390e1d3c0fdSWill Deacon 			  phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
391e1d3c0fdSWill Deacon 			  int lvl, arm_lpae_iopte *ptep)
392e1d3c0fdSWill Deacon {
393e1d3c0fdSWill Deacon 	arm_lpae_iopte *cptep, pte;
394e1d3c0fdSWill Deacon 	size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
3952c3d273eSRobin Murphy 	size_t tblsz = ARM_LPAE_GRANULE(data);
396f8d54961SRobin Murphy 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
397e1d3c0fdSWill Deacon 
398e1d3c0fdSWill Deacon 	/* Find our entry at the current level */
399e1d3c0fdSWill Deacon 	ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
400e1d3c0fdSWill Deacon 
401e1d3c0fdSWill Deacon 	/* If we can install a leaf entry at this level, then do so */
402f8d54961SRobin Murphy 	if (size == block_size && (size & cfg->pgsize_bitmap))
403e1d3c0fdSWill Deacon 		return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
404e1d3c0fdSWill Deacon 
405e1d3c0fdSWill Deacon 	/* We can't allocate tables at the final level */
406e1d3c0fdSWill Deacon 	if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
407e1d3c0fdSWill Deacon 		return -EINVAL;
408e1d3c0fdSWill Deacon 
409e1d3c0fdSWill Deacon 	/* Grab a pointer to the next level */
4102c3d273eSRobin Murphy 	pte = READ_ONCE(*ptep);
411e1d3c0fdSWill Deacon 	if (!pte) {
4122c3d273eSRobin Murphy 		cptep = __arm_lpae_alloc_pages(tblsz, GFP_ATOMIC, cfg);
413e1d3c0fdSWill Deacon 		if (!cptep)
414e1d3c0fdSWill Deacon 			return -ENOMEM;
415e1d3c0fdSWill Deacon 
4162c3d273eSRobin Murphy 		pte = arm_lpae_install_table(cptep, ptep, 0, cfg);
4172c3d273eSRobin Murphy 		if (pte)
4182c3d273eSRobin Murphy 			__arm_lpae_free_pages(cptep, tblsz, cfg);
4192c3d273eSRobin Murphy 	} else if (!(cfg->quirks & IO_PGTABLE_QUIRK_NO_DMA) &&
4202c3d273eSRobin Murphy 		   !(pte & ARM_LPAE_PTE_SW_SYNC)) {
4212c3d273eSRobin Murphy 		__arm_lpae_sync_pte(ptep, cfg);
4222c3d273eSRobin Murphy 	}
4232c3d273eSRobin Murphy 
424d08d42deSRob Herring 	if (pte && !iopte_leaf(pte, lvl, data->iop.fmt)) {
425e1d3c0fdSWill Deacon 		cptep = iopte_deref(pte, data);
4262c3d273eSRobin Murphy 	} else if (pte) {
427ed46e66cSOleksandr Tyshchenko 		/* We require an unmap first */
428ed46e66cSOleksandr Tyshchenko 		WARN_ON(!selftest_running);
429ed46e66cSOleksandr Tyshchenko 		return -EEXIST;
430e1d3c0fdSWill Deacon 	}
431e1d3c0fdSWill Deacon 
432e1d3c0fdSWill Deacon 	/* Rinse, repeat */
433e1d3c0fdSWill Deacon 	return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
434e1d3c0fdSWill Deacon }
435e1d3c0fdSWill Deacon 
436e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
437e1d3c0fdSWill Deacon 					   int prot)
438e1d3c0fdSWill Deacon {
439e1d3c0fdSWill Deacon 	arm_lpae_iopte pte;
440e1d3c0fdSWill Deacon 
441e1d3c0fdSWill Deacon 	if (data->iop.fmt == ARM_64_LPAE_S1 ||
442e1d3c0fdSWill Deacon 	    data->iop.fmt == ARM_32_LPAE_S1) {
443e7468a23SJeremy Gebben 		pte = ARM_LPAE_PTE_nG;
444e1d3c0fdSWill Deacon 		if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
445e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_AP_RDONLY;
446e7468a23SJeremy Gebben 		if (!(prot & IOMMU_PRIV))
447e7468a23SJeremy Gebben 			pte |= ARM_LPAE_PTE_AP_UNPRIV;
448e1d3c0fdSWill Deacon 	} else {
449e1d3c0fdSWill Deacon 		pte = ARM_LPAE_PTE_HAP_FAULT;
450e1d3c0fdSWill Deacon 		if (prot & IOMMU_READ)
451e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_HAP_READ;
452e1d3c0fdSWill Deacon 		if (prot & IOMMU_WRITE)
453e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_HAP_WRITE;
454d08d42deSRob Herring 	}
455d08d42deSRob Herring 
456d08d42deSRob Herring 	/*
457d08d42deSRob Herring 	 * Note that this logic is structured to accommodate Mali LPAE
458d08d42deSRob Herring 	 * having stage-1-like attributes but stage-2-like permissions.
459d08d42deSRob Herring 	 */
460d08d42deSRob Herring 	if (data->iop.fmt == ARM_64_LPAE_S2 ||
461d08d42deSRob Herring 	    data->iop.fmt == ARM_32_LPAE_S2) {
462fb948251SRobin Murphy 		if (prot & IOMMU_MMIO)
463fb948251SRobin Murphy 			pte |= ARM_LPAE_PTE_MEMATTR_DEV;
464fb948251SRobin Murphy 		else if (prot & IOMMU_CACHE)
465e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
466e1d3c0fdSWill Deacon 		else
467e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_MEMATTR_NC;
468d08d42deSRob Herring 	} else {
469d08d42deSRob Herring 		if (prot & IOMMU_MMIO)
470d08d42deSRob Herring 			pte |= (ARM_LPAE_MAIR_ATTR_IDX_DEV
471d08d42deSRob Herring 				<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
472d08d42deSRob Herring 		else if (prot & IOMMU_CACHE)
473d08d42deSRob Herring 			pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
474d08d42deSRob Herring 				<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
475*90ec7a76SVivek Gautam 		else if (prot & IOMMU_QCOM_SYS_CACHE)
476*90ec7a76SVivek Gautam 			pte |= (ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE
477*90ec7a76SVivek Gautam 				<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
478e1d3c0fdSWill Deacon 	}
479e1d3c0fdSWill Deacon 
480e1d3c0fdSWill Deacon 	if (prot & IOMMU_NOEXEC)
481e1d3c0fdSWill Deacon 		pte |= ARM_LPAE_PTE_XN;
482e1d3c0fdSWill Deacon 
483e1d3c0fdSWill Deacon 	return pte;
484e1d3c0fdSWill Deacon }
485e1d3c0fdSWill Deacon 
486e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
487e1d3c0fdSWill Deacon 			phys_addr_t paddr, size_t size, int iommu_prot)
488e1d3c0fdSWill Deacon {
489e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
490e1d3c0fdSWill Deacon 	arm_lpae_iopte *ptep = data->pgd;
49187a91b15SRobin Murphy 	int ret, lvl = ARM_LPAE_START_LVL(data);
492e1d3c0fdSWill Deacon 	arm_lpae_iopte prot;
493e1d3c0fdSWill Deacon 
494e1d3c0fdSWill Deacon 	/* If no access, then nothing to do */
495e1d3c0fdSWill Deacon 	if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
496e1d3c0fdSWill Deacon 		return 0;
497e1d3c0fdSWill Deacon 
49876557391SRobin Murphy 	if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias) ||
49976557391SRobin Murphy 		    paddr >= (1ULL << data->iop.cfg.oas)))
50076557391SRobin Murphy 		return -ERANGE;
50176557391SRobin Murphy 
502e1d3c0fdSWill Deacon 	prot = arm_lpae_prot_to_pte(data, iommu_prot);
50387a91b15SRobin Murphy 	ret = __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
50487a91b15SRobin Murphy 	/*
50587a91b15SRobin Murphy 	 * Synchronise all PTE updates for the new mapping before there's
50687a91b15SRobin Murphy 	 * a chance for anything to kick off a table walk for the new iova.
50787a91b15SRobin Murphy 	 */
50887a91b15SRobin Murphy 	wmb();
50987a91b15SRobin Murphy 
51087a91b15SRobin Murphy 	return ret;
511e1d3c0fdSWill Deacon }
512e1d3c0fdSWill Deacon 
513e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
514e1d3c0fdSWill Deacon 				    arm_lpae_iopte *ptep)
515e1d3c0fdSWill Deacon {
516e1d3c0fdSWill Deacon 	arm_lpae_iopte *start, *end;
517e1d3c0fdSWill Deacon 	unsigned long table_size;
518e1d3c0fdSWill Deacon 
519e1d3c0fdSWill Deacon 	if (lvl == ARM_LPAE_START_LVL(data))
520e1d3c0fdSWill Deacon 		table_size = data->pgd_size;
521e1d3c0fdSWill Deacon 	else
52206c610e8SRobin Murphy 		table_size = ARM_LPAE_GRANULE(data);
523e1d3c0fdSWill Deacon 
524e1d3c0fdSWill Deacon 	start = ptep;
52512c2ab09SWill Deacon 
52612c2ab09SWill Deacon 	/* Only leaf entries at the last level */
52712c2ab09SWill Deacon 	if (lvl == ARM_LPAE_MAX_LEVELS - 1)
52812c2ab09SWill Deacon 		end = ptep;
52912c2ab09SWill Deacon 	else
530e1d3c0fdSWill Deacon 		end = (void *)ptep + table_size;
531e1d3c0fdSWill Deacon 
532e1d3c0fdSWill Deacon 	while (ptep != end) {
533e1d3c0fdSWill Deacon 		arm_lpae_iopte pte = *ptep++;
534e1d3c0fdSWill Deacon 
535d08d42deSRob Herring 		if (!pte || iopte_leaf(pte, lvl, data->iop.fmt))
536e1d3c0fdSWill Deacon 			continue;
537e1d3c0fdSWill Deacon 
538e1d3c0fdSWill Deacon 		__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
539e1d3c0fdSWill Deacon 	}
540e1d3c0fdSWill Deacon 
541f8d54961SRobin Murphy 	__arm_lpae_free_pages(start, table_size, &data->iop.cfg);
542e1d3c0fdSWill Deacon }
543e1d3c0fdSWill Deacon 
544e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop)
545e1d3c0fdSWill Deacon {
546e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
547e1d3c0fdSWill Deacon 
548e1d3c0fdSWill Deacon 	__arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
549e1d3c0fdSWill Deacon 	kfree(data);
550e1d3c0fdSWill Deacon }
551e1d3c0fdSWill Deacon 
552193e67c0SVivek Gautam static size_t arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
553e1d3c0fdSWill Deacon 				       unsigned long iova, size_t size,
554fb3a9579SRobin Murphy 				       arm_lpae_iopte blk_pte, int lvl,
555fb3a9579SRobin Murphy 				       arm_lpae_iopte *ptep)
556e1d3c0fdSWill Deacon {
557fb3a9579SRobin Murphy 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
558fb3a9579SRobin Murphy 	arm_lpae_iopte pte, *tablep;
559e1d3c0fdSWill Deacon 	phys_addr_t blk_paddr;
560fb3a9579SRobin Murphy 	size_t tablesz = ARM_LPAE_GRANULE(data);
561fb3a9579SRobin Murphy 	size_t split_sz = ARM_LPAE_BLOCK_SIZE(lvl, data);
562fb3a9579SRobin Murphy 	int i, unmap_idx = -1;
563e1d3c0fdSWill Deacon 
564fb3a9579SRobin Murphy 	if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
565fb3a9579SRobin Murphy 		return 0;
566e1d3c0fdSWill Deacon 
567fb3a9579SRobin Murphy 	tablep = __arm_lpae_alloc_pages(tablesz, GFP_ATOMIC, cfg);
568fb3a9579SRobin Murphy 	if (!tablep)
569fb3a9579SRobin Murphy 		return 0; /* Bytes unmapped */
570e1d3c0fdSWill Deacon 
571fb3a9579SRobin Murphy 	if (size == split_sz)
572fb3a9579SRobin Murphy 		unmap_idx = ARM_LPAE_LVL_IDX(iova, lvl, data);
573fb3a9579SRobin Murphy 
5746c89928fSRobin Murphy 	blk_paddr = iopte_to_paddr(blk_pte, data);
575fb3a9579SRobin Murphy 	pte = iopte_prot(blk_pte);
576fb3a9579SRobin Murphy 
577fb3a9579SRobin Murphy 	for (i = 0; i < tablesz / sizeof(pte); i++, blk_paddr += split_sz) {
578e1d3c0fdSWill Deacon 		/* Unmap! */
579fb3a9579SRobin Murphy 		if (i == unmap_idx)
580e1d3c0fdSWill Deacon 			continue;
581e1d3c0fdSWill Deacon 
582fb3a9579SRobin Murphy 		__arm_lpae_init_pte(data, blk_paddr, pte, lvl, &tablep[i]);
583e1d3c0fdSWill Deacon 	}
584e1d3c0fdSWill Deacon 
5852c3d273eSRobin Murphy 	pte = arm_lpae_install_table(tablep, ptep, blk_pte, cfg);
5862c3d273eSRobin Murphy 	if (pte != blk_pte) {
5872c3d273eSRobin Murphy 		__arm_lpae_free_pages(tablep, tablesz, cfg);
5882c3d273eSRobin Murphy 		/*
5892c3d273eSRobin Murphy 		 * We may race against someone unmapping another part of this
5902c3d273eSRobin Murphy 		 * block, but anything else is invalid. We can't misinterpret
5912c3d273eSRobin Murphy 		 * a page entry here since we're never at the last level.
5922c3d273eSRobin Murphy 		 */
5932c3d273eSRobin Murphy 		if (iopte_type(pte, lvl - 1) != ARM_LPAE_PTE_TYPE_TABLE)
5942c3d273eSRobin Murphy 			return 0;
5952c3d273eSRobin Murphy 
5962c3d273eSRobin Murphy 		tablep = iopte_deref(pte, data);
59785c7a0f1SRobin Murphy 	} else if (unmap_idx >= 0) {
598fb3a9579SRobin Murphy 		io_pgtable_tlb_add_flush(&data->iop, iova, size, size, true);
599b6b65ca2SZhen Lei 		io_pgtable_tlb_sync(&data->iop);
600e1d3c0fdSWill Deacon 		return size;
601e1d3c0fdSWill Deacon 	}
602e1d3c0fdSWill Deacon 
60385c7a0f1SRobin Murphy 	return __arm_lpae_unmap(data, iova, size, lvl, tablep);
60485c7a0f1SRobin Murphy }
60585c7a0f1SRobin Murphy 
606193e67c0SVivek Gautam static size_t __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
607e1d3c0fdSWill Deacon 			       unsigned long iova, size_t size, int lvl,
608e1d3c0fdSWill Deacon 			       arm_lpae_iopte *ptep)
609e1d3c0fdSWill Deacon {
610e1d3c0fdSWill Deacon 	arm_lpae_iopte pte;
611507e4c9dSRobin Murphy 	struct io_pgtable *iop = &data->iop;
612e1d3c0fdSWill Deacon 
6132eb97c78SRobin Murphy 	/* Something went horribly wrong and we ran out of page table */
6142eb97c78SRobin Murphy 	if (WARN_ON(lvl == ARM_LPAE_MAX_LEVELS))
6152eb97c78SRobin Murphy 		return 0;
6162eb97c78SRobin Murphy 
617e1d3c0fdSWill Deacon 	ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
6182c3d273eSRobin Murphy 	pte = READ_ONCE(*ptep);
6192eb97c78SRobin Murphy 	if (WARN_ON(!pte))
620e1d3c0fdSWill Deacon 		return 0;
621e1d3c0fdSWill Deacon 
622e1d3c0fdSWill Deacon 	/* If the size matches this level, we're in the right place */
623fb3a9579SRobin Murphy 	if (size == ARM_LPAE_BLOCK_SIZE(lvl, data)) {
624507e4c9dSRobin Murphy 		__arm_lpae_set_pte(ptep, 0, &iop->cfg);
625e1d3c0fdSWill Deacon 
626d08d42deSRob Herring 		if (!iopte_leaf(pte, lvl, iop->fmt)) {
627e1d3c0fdSWill Deacon 			/* Also flush any partial walks */
628507e4c9dSRobin Murphy 			io_pgtable_tlb_add_flush(iop, iova, size,
629507e4c9dSRobin Murphy 						ARM_LPAE_GRANULE(data), false);
630507e4c9dSRobin Murphy 			io_pgtable_tlb_sync(iop);
631e1d3c0fdSWill Deacon 			ptep = iopte_deref(pte, data);
632e1d3c0fdSWill Deacon 			__arm_lpae_free_pgtable(data, lvl + 1, ptep);
633b6b65ca2SZhen Lei 		} else if (iop->cfg.quirks & IO_PGTABLE_QUIRK_NON_STRICT) {
634b6b65ca2SZhen Lei 			/*
635b6b65ca2SZhen Lei 			 * Order the PTE update against queueing the IOVA, to
636b6b65ca2SZhen Lei 			 * guarantee that a flush callback from a different CPU
637b6b65ca2SZhen Lei 			 * has observed it before the TLBIALL can be issued.
638b6b65ca2SZhen Lei 			 */
639b6b65ca2SZhen Lei 			smp_wmb();
640e1d3c0fdSWill Deacon 		} else {
641507e4c9dSRobin Murphy 			io_pgtable_tlb_add_flush(iop, iova, size, size, true);
642e1d3c0fdSWill Deacon 		}
643e1d3c0fdSWill Deacon 
644e1d3c0fdSWill Deacon 		return size;
645d08d42deSRob Herring 	} else if (iopte_leaf(pte, lvl, iop->fmt)) {
646e1d3c0fdSWill Deacon 		/*
647e1d3c0fdSWill Deacon 		 * Insert a table at the next level to map the old region,
648e1d3c0fdSWill Deacon 		 * minus the part we want to unmap
649e1d3c0fdSWill Deacon 		 */
650fb3a9579SRobin Murphy 		return arm_lpae_split_blk_unmap(data, iova, size, pte,
651fb3a9579SRobin Murphy 						lvl + 1, ptep);
652e1d3c0fdSWill Deacon 	}
653e1d3c0fdSWill Deacon 
654e1d3c0fdSWill Deacon 	/* Keep on walkin' */
655e1d3c0fdSWill Deacon 	ptep = iopte_deref(pte, data);
656e1d3c0fdSWill Deacon 	return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
657e1d3c0fdSWill Deacon }
658e1d3c0fdSWill Deacon 
659193e67c0SVivek Gautam static size_t arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
660e1d3c0fdSWill Deacon 			     size_t size)
661e1d3c0fdSWill Deacon {
662e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
663e1d3c0fdSWill Deacon 	arm_lpae_iopte *ptep = data->pgd;
664e1d3c0fdSWill Deacon 	int lvl = ARM_LPAE_START_LVL(data);
665e1d3c0fdSWill Deacon 
66676557391SRobin Murphy 	if (WARN_ON(iova >= (1ULL << data->iop.cfg.ias)))
66776557391SRobin Murphy 		return 0;
66876557391SRobin Murphy 
66932b12449SRobin Murphy 	return __arm_lpae_unmap(data, iova, size, lvl, ptep);
670e1d3c0fdSWill Deacon }
671e1d3c0fdSWill Deacon 
672e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
673e1d3c0fdSWill Deacon 					 unsigned long iova)
674e1d3c0fdSWill Deacon {
675e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
676e1d3c0fdSWill Deacon 	arm_lpae_iopte pte, *ptep = data->pgd;
677e1d3c0fdSWill Deacon 	int lvl = ARM_LPAE_START_LVL(data);
678e1d3c0fdSWill Deacon 
679e1d3c0fdSWill Deacon 	do {
680e1d3c0fdSWill Deacon 		/* Valid IOPTE pointer? */
681e1d3c0fdSWill Deacon 		if (!ptep)
682e1d3c0fdSWill Deacon 			return 0;
683e1d3c0fdSWill Deacon 
684e1d3c0fdSWill Deacon 		/* Grab the IOPTE we're interested in */
6852c3d273eSRobin Murphy 		ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
6862c3d273eSRobin Murphy 		pte = READ_ONCE(*ptep);
687e1d3c0fdSWill Deacon 
688e1d3c0fdSWill Deacon 		/* Valid entry? */
689e1d3c0fdSWill Deacon 		if (!pte)
690e1d3c0fdSWill Deacon 			return 0;
691e1d3c0fdSWill Deacon 
692e1d3c0fdSWill Deacon 		/* Leaf entry? */
693d08d42deSRob Herring 		if (iopte_leaf(pte, lvl, data->iop.fmt))
694e1d3c0fdSWill Deacon 			goto found_translation;
695e1d3c0fdSWill Deacon 
696e1d3c0fdSWill Deacon 		/* Take it to the next level */
697e1d3c0fdSWill Deacon 		ptep = iopte_deref(pte, data);
698e1d3c0fdSWill Deacon 	} while (++lvl < ARM_LPAE_MAX_LEVELS);
699e1d3c0fdSWill Deacon 
700e1d3c0fdSWill Deacon 	/* Ran out of page tables to walk */
701e1d3c0fdSWill Deacon 	return 0;
702e1d3c0fdSWill Deacon 
703e1d3c0fdSWill Deacon found_translation:
7047c6d90e2SWill Deacon 	iova &= (ARM_LPAE_BLOCK_SIZE(lvl, data) - 1);
7056c89928fSRobin Murphy 	return iopte_to_paddr(pte, data) | iova;
706e1d3c0fdSWill Deacon }
707e1d3c0fdSWill Deacon 
708e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
709e1d3c0fdSWill Deacon {
7106c89928fSRobin Murphy 	unsigned long granule, page_sizes;
7116c89928fSRobin Murphy 	unsigned int max_addr_bits = 48;
712e1d3c0fdSWill Deacon 
713e1d3c0fdSWill Deacon 	/*
714e1d3c0fdSWill Deacon 	 * We need to restrict the supported page sizes to match the
715e1d3c0fdSWill Deacon 	 * translation regime for a particular granule. Aim to match
716e1d3c0fdSWill Deacon 	 * the CPU page size if possible, otherwise prefer smaller sizes.
717e1d3c0fdSWill Deacon 	 * While we're at it, restrict the block sizes to match the
718e1d3c0fdSWill Deacon 	 * chosen granule.
719e1d3c0fdSWill Deacon 	 */
720e1d3c0fdSWill Deacon 	if (cfg->pgsize_bitmap & PAGE_SIZE)
721e1d3c0fdSWill Deacon 		granule = PAGE_SIZE;
722e1d3c0fdSWill Deacon 	else if (cfg->pgsize_bitmap & ~PAGE_MASK)
723e1d3c0fdSWill Deacon 		granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
724e1d3c0fdSWill Deacon 	else if (cfg->pgsize_bitmap & PAGE_MASK)
725e1d3c0fdSWill Deacon 		granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
726e1d3c0fdSWill Deacon 	else
727e1d3c0fdSWill Deacon 		granule = 0;
728e1d3c0fdSWill Deacon 
729e1d3c0fdSWill Deacon 	switch (granule) {
730e1d3c0fdSWill Deacon 	case SZ_4K:
7316c89928fSRobin Murphy 		page_sizes = (SZ_4K | SZ_2M | SZ_1G);
732e1d3c0fdSWill Deacon 		break;
733e1d3c0fdSWill Deacon 	case SZ_16K:
7346c89928fSRobin Murphy 		page_sizes = (SZ_16K | SZ_32M);
735e1d3c0fdSWill Deacon 		break;
736e1d3c0fdSWill Deacon 	case SZ_64K:
7376c89928fSRobin Murphy 		max_addr_bits = 52;
7386c89928fSRobin Murphy 		page_sizes = (SZ_64K | SZ_512M);
7396c89928fSRobin Murphy 		if (cfg->oas > 48)
7406c89928fSRobin Murphy 			page_sizes |= 1ULL << 42; /* 4TB */
741e1d3c0fdSWill Deacon 		break;
742e1d3c0fdSWill Deacon 	default:
7436c89928fSRobin Murphy 		page_sizes = 0;
744e1d3c0fdSWill Deacon 	}
7456c89928fSRobin Murphy 
7466c89928fSRobin Murphy 	cfg->pgsize_bitmap &= page_sizes;
7476c89928fSRobin Murphy 	cfg->ias = min(cfg->ias, max_addr_bits);
7486c89928fSRobin Murphy 	cfg->oas = min(cfg->oas, max_addr_bits);
749e1d3c0fdSWill Deacon }
750e1d3c0fdSWill Deacon 
751e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable *
752e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
753e1d3c0fdSWill Deacon {
754e1d3c0fdSWill Deacon 	unsigned long va_bits, pgd_bits;
755e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data;
756e1d3c0fdSWill Deacon 
757e1d3c0fdSWill Deacon 	arm_lpae_restrict_pgsizes(cfg);
758e1d3c0fdSWill Deacon 
759e1d3c0fdSWill Deacon 	if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
760e1d3c0fdSWill Deacon 		return NULL;
761e1d3c0fdSWill Deacon 
762e1d3c0fdSWill Deacon 	if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
763e1d3c0fdSWill Deacon 		return NULL;
764e1d3c0fdSWill Deacon 
765e1d3c0fdSWill Deacon 	if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
766e1d3c0fdSWill Deacon 		return NULL;
767e1d3c0fdSWill Deacon 
768ffcb6d16SRobin Murphy 	if (!selftest_running && cfg->iommu_dev->dma_pfn_offset) {
769ffcb6d16SRobin Murphy 		dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
770ffcb6d16SRobin Murphy 		return NULL;
771ffcb6d16SRobin Murphy 	}
772ffcb6d16SRobin Murphy 
773e1d3c0fdSWill Deacon 	data = kmalloc(sizeof(*data), GFP_KERNEL);
774e1d3c0fdSWill Deacon 	if (!data)
775e1d3c0fdSWill Deacon 		return NULL;
776e1d3c0fdSWill Deacon 
777e1d3c0fdSWill Deacon 	data->pg_shift = __ffs(cfg->pgsize_bitmap);
778e1d3c0fdSWill Deacon 	data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
779e1d3c0fdSWill Deacon 
780e1d3c0fdSWill Deacon 	va_bits = cfg->ias - data->pg_shift;
781e1d3c0fdSWill Deacon 	data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
782e1d3c0fdSWill Deacon 
783e1d3c0fdSWill Deacon 	/* Calculate the actual size of our pgd (without concatenation) */
784e1d3c0fdSWill Deacon 	pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
785e1d3c0fdSWill Deacon 	data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
786e1d3c0fdSWill Deacon 
787e1d3c0fdSWill Deacon 	data->iop.ops = (struct io_pgtable_ops) {
788e1d3c0fdSWill Deacon 		.map		= arm_lpae_map,
789e1d3c0fdSWill Deacon 		.unmap		= arm_lpae_unmap,
790e1d3c0fdSWill Deacon 		.iova_to_phys	= arm_lpae_iova_to_phys,
791e1d3c0fdSWill Deacon 	};
792e1d3c0fdSWill Deacon 
793e1d3c0fdSWill Deacon 	return data;
794e1d3c0fdSWill Deacon }
795e1d3c0fdSWill Deacon 
796e1d3c0fdSWill Deacon static struct io_pgtable *
797e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
798e1d3c0fdSWill Deacon {
799e1d3c0fdSWill Deacon 	u64 reg;
8003850db49SRobin Murphy 	struct arm_lpae_io_pgtable *data;
801e1d3c0fdSWill Deacon 
802b6b65ca2SZhen Lei 	if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS | IO_PGTABLE_QUIRK_NO_DMA |
803b6b65ca2SZhen Lei 			    IO_PGTABLE_QUIRK_NON_STRICT))
8043850db49SRobin Murphy 		return NULL;
8053850db49SRobin Murphy 
8063850db49SRobin Murphy 	data = arm_lpae_alloc_pgtable(cfg);
807e1d3c0fdSWill Deacon 	if (!data)
808e1d3c0fdSWill Deacon 		return NULL;
809e1d3c0fdSWill Deacon 
810e1d3c0fdSWill Deacon 	/* TCR */
811e1d3c0fdSWill Deacon 	reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
812e1d3c0fdSWill Deacon 	      (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
813e1d3c0fdSWill Deacon 	      (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
814e1d3c0fdSWill Deacon 
81506c610e8SRobin Murphy 	switch (ARM_LPAE_GRANULE(data)) {
816e1d3c0fdSWill Deacon 	case SZ_4K:
817e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_4K;
818e1d3c0fdSWill Deacon 		break;
819e1d3c0fdSWill Deacon 	case SZ_16K:
820e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_16K;
821e1d3c0fdSWill Deacon 		break;
822e1d3c0fdSWill Deacon 	case SZ_64K:
823e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_64K;
824e1d3c0fdSWill Deacon 		break;
825e1d3c0fdSWill Deacon 	}
826e1d3c0fdSWill Deacon 
827e1d3c0fdSWill Deacon 	switch (cfg->oas) {
828e1d3c0fdSWill Deacon 	case 32:
829e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
830e1d3c0fdSWill Deacon 		break;
831e1d3c0fdSWill Deacon 	case 36:
832e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
833e1d3c0fdSWill Deacon 		break;
834e1d3c0fdSWill Deacon 	case 40:
835e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
836e1d3c0fdSWill Deacon 		break;
837e1d3c0fdSWill Deacon 	case 42:
838e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
839e1d3c0fdSWill Deacon 		break;
840e1d3c0fdSWill Deacon 	case 44:
841e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
842e1d3c0fdSWill Deacon 		break;
843e1d3c0fdSWill Deacon 	case 48:
844e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
845e1d3c0fdSWill Deacon 		break;
8466c89928fSRobin Murphy 	case 52:
8476c89928fSRobin Murphy 		reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_IPS_SHIFT);
8486c89928fSRobin Murphy 		break;
849e1d3c0fdSWill Deacon 	default:
850e1d3c0fdSWill Deacon 		goto out_free_data;
851e1d3c0fdSWill Deacon 	}
852e1d3c0fdSWill Deacon 
853e1d3c0fdSWill Deacon 	reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
85463979b8dSWill Deacon 
85563979b8dSWill Deacon 	/* Disable speculative walks through TTBR1 */
85663979b8dSWill Deacon 	reg |= ARM_LPAE_TCR_EPD1;
857e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.tcr = reg;
858e1d3c0fdSWill Deacon 
859e1d3c0fdSWill Deacon 	/* MAIRs */
860e1d3c0fdSWill Deacon 	reg = (ARM_LPAE_MAIR_ATTR_NC
861e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
862e1d3c0fdSWill Deacon 	      (ARM_LPAE_MAIR_ATTR_WBRWA
863e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
864e1d3c0fdSWill Deacon 	      (ARM_LPAE_MAIR_ATTR_DEVICE
865*90ec7a76SVivek Gautam 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)) |
866*90ec7a76SVivek Gautam 	      (ARM_LPAE_MAIR_ATTR_INC_OWBRWA
867*90ec7a76SVivek Gautam 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_INC_OCACHE));
868e1d3c0fdSWill Deacon 
869e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.mair[0] = reg;
870e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.mair[1] = 0;
871e1d3c0fdSWill Deacon 
872e1d3c0fdSWill Deacon 	/* Looking good; allocate a pgd */
873f8d54961SRobin Murphy 	data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
874e1d3c0fdSWill Deacon 	if (!data->pgd)
875e1d3c0fdSWill Deacon 		goto out_free_data;
876e1d3c0fdSWill Deacon 
87787a91b15SRobin Murphy 	/* Ensure the empty pgd is visible before any actual TTBR write */
87887a91b15SRobin Murphy 	wmb();
879e1d3c0fdSWill Deacon 
880e1d3c0fdSWill Deacon 	/* TTBRs */
881e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
882e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
883e1d3c0fdSWill Deacon 	return &data->iop;
884e1d3c0fdSWill Deacon 
885e1d3c0fdSWill Deacon out_free_data:
886e1d3c0fdSWill Deacon 	kfree(data);
887e1d3c0fdSWill Deacon 	return NULL;
888e1d3c0fdSWill Deacon }
889e1d3c0fdSWill Deacon 
890e1d3c0fdSWill Deacon static struct io_pgtable *
891e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
892e1d3c0fdSWill Deacon {
893e1d3c0fdSWill Deacon 	u64 reg, sl;
8943850db49SRobin Murphy 	struct arm_lpae_io_pgtable *data;
895e1d3c0fdSWill Deacon 
8963850db49SRobin Murphy 	/* The NS quirk doesn't apply at stage 2 */
897b6b65ca2SZhen Lei 	if (cfg->quirks & ~(IO_PGTABLE_QUIRK_NO_DMA |
898b6b65ca2SZhen Lei 			    IO_PGTABLE_QUIRK_NON_STRICT))
8993850db49SRobin Murphy 		return NULL;
9003850db49SRobin Murphy 
9013850db49SRobin Murphy 	data = arm_lpae_alloc_pgtable(cfg);
902e1d3c0fdSWill Deacon 	if (!data)
903e1d3c0fdSWill Deacon 		return NULL;
904e1d3c0fdSWill Deacon 
905e1d3c0fdSWill Deacon 	/*
906e1d3c0fdSWill Deacon 	 * Concatenate PGDs at level 1 if possible in order to reduce
907e1d3c0fdSWill Deacon 	 * the depth of the stage-2 walk.
908e1d3c0fdSWill Deacon 	 */
909e1d3c0fdSWill Deacon 	if (data->levels == ARM_LPAE_MAX_LEVELS) {
910e1d3c0fdSWill Deacon 		unsigned long pgd_pages;
911e1d3c0fdSWill Deacon 
912e1d3c0fdSWill Deacon 		pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
913e1d3c0fdSWill Deacon 		if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
914e1d3c0fdSWill Deacon 			data->pgd_size = pgd_pages << data->pg_shift;
915e1d3c0fdSWill Deacon 			data->levels--;
916e1d3c0fdSWill Deacon 		}
917e1d3c0fdSWill Deacon 	}
918e1d3c0fdSWill Deacon 
919e1d3c0fdSWill Deacon 	/* VTCR */
920e1d3c0fdSWill Deacon 	reg = ARM_64_LPAE_S2_TCR_RES1 |
921e1d3c0fdSWill Deacon 	     (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
922e1d3c0fdSWill Deacon 	     (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
923e1d3c0fdSWill Deacon 	     (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
924e1d3c0fdSWill Deacon 
925e1d3c0fdSWill Deacon 	sl = ARM_LPAE_START_LVL(data);
926e1d3c0fdSWill Deacon 
92706c610e8SRobin Murphy 	switch (ARM_LPAE_GRANULE(data)) {
928e1d3c0fdSWill Deacon 	case SZ_4K:
929e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_4K;
930e1d3c0fdSWill Deacon 		sl++; /* SL0 format is different for 4K granule size */
931e1d3c0fdSWill Deacon 		break;
932e1d3c0fdSWill Deacon 	case SZ_16K:
933e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_16K;
934e1d3c0fdSWill Deacon 		break;
935e1d3c0fdSWill Deacon 	case SZ_64K:
936e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_64K;
937e1d3c0fdSWill Deacon 		break;
938e1d3c0fdSWill Deacon 	}
939e1d3c0fdSWill Deacon 
940e1d3c0fdSWill Deacon 	switch (cfg->oas) {
941e1d3c0fdSWill Deacon 	case 32:
942e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
943e1d3c0fdSWill Deacon 		break;
944e1d3c0fdSWill Deacon 	case 36:
945e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
946e1d3c0fdSWill Deacon 		break;
947e1d3c0fdSWill Deacon 	case 40:
948e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
949e1d3c0fdSWill Deacon 		break;
950e1d3c0fdSWill Deacon 	case 42:
951e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
952e1d3c0fdSWill Deacon 		break;
953e1d3c0fdSWill Deacon 	case 44:
954e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
955e1d3c0fdSWill Deacon 		break;
956e1d3c0fdSWill Deacon 	case 48:
957e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
958e1d3c0fdSWill Deacon 		break;
9596c89928fSRobin Murphy 	case 52:
9606c89928fSRobin Murphy 		reg |= (ARM_LPAE_TCR_PS_52_BIT << ARM_LPAE_TCR_PS_SHIFT);
9616c89928fSRobin Murphy 		break;
962e1d3c0fdSWill Deacon 	default:
963e1d3c0fdSWill Deacon 		goto out_free_data;
964e1d3c0fdSWill Deacon 	}
965e1d3c0fdSWill Deacon 
966e1d3c0fdSWill Deacon 	reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
967e1d3c0fdSWill Deacon 	reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
968e1d3c0fdSWill Deacon 	cfg->arm_lpae_s2_cfg.vtcr = reg;
969e1d3c0fdSWill Deacon 
970e1d3c0fdSWill Deacon 	/* Allocate pgd pages */
971f8d54961SRobin Murphy 	data->pgd = __arm_lpae_alloc_pages(data->pgd_size, GFP_KERNEL, cfg);
972e1d3c0fdSWill Deacon 	if (!data->pgd)
973e1d3c0fdSWill Deacon 		goto out_free_data;
974e1d3c0fdSWill Deacon 
97587a91b15SRobin Murphy 	/* Ensure the empty pgd is visible before any actual TTBR write */
97687a91b15SRobin Murphy 	wmb();
977e1d3c0fdSWill Deacon 
978e1d3c0fdSWill Deacon 	/* VTTBR */
979e1d3c0fdSWill Deacon 	cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
980e1d3c0fdSWill Deacon 	return &data->iop;
981e1d3c0fdSWill Deacon 
982e1d3c0fdSWill Deacon out_free_data:
983e1d3c0fdSWill Deacon 	kfree(data);
984e1d3c0fdSWill Deacon 	return NULL;
985e1d3c0fdSWill Deacon }
986e1d3c0fdSWill Deacon 
987e1d3c0fdSWill Deacon static struct io_pgtable *
988e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
989e1d3c0fdSWill Deacon {
990e1d3c0fdSWill Deacon 	struct io_pgtable *iop;
991e1d3c0fdSWill Deacon 
992e1d3c0fdSWill Deacon 	if (cfg->ias > 32 || cfg->oas > 40)
993e1d3c0fdSWill Deacon 		return NULL;
994e1d3c0fdSWill Deacon 
995e1d3c0fdSWill Deacon 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
996e1d3c0fdSWill Deacon 	iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
997e1d3c0fdSWill Deacon 	if (iop) {
998e1d3c0fdSWill Deacon 		cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
999e1d3c0fdSWill Deacon 		cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
1000e1d3c0fdSWill Deacon 	}
1001e1d3c0fdSWill Deacon 
1002e1d3c0fdSWill Deacon 	return iop;
1003e1d3c0fdSWill Deacon }
1004e1d3c0fdSWill Deacon 
1005e1d3c0fdSWill Deacon static struct io_pgtable *
1006e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
1007e1d3c0fdSWill Deacon {
1008e1d3c0fdSWill Deacon 	struct io_pgtable *iop;
1009e1d3c0fdSWill Deacon 
1010e1d3c0fdSWill Deacon 	if (cfg->ias > 40 || cfg->oas > 40)
1011e1d3c0fdSWill Deacon 		return NULL;
1012e1d3c0fdSWill Deacon 
1013e1d3c0fdSWill Deacon 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1014e1d3c0fdSWill Deacon 	iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
1015e1d3c0fdSWill Deacon 	if (iop)
1016e1d3c0fdSWill Deacon 		cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
1017e1d3c0fdSWill Deacon 
1018e1d3c0fdSWill Deacon 	return iop;
1019e1d3c0fdSWill Deacon }
1020e1d3c0fdSWill Deacon 
1021d08d42deSRob Herring static struct io_pgtable *
1022d08d42deSRob Herring arm_mali_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
1023d08d42deSRob Herring {
1024d08d42deSRob Herring 	struct io_pgtable *iop;
1025d08d42deSRob Herring 
1026d08d42deSRob Herring 	if (cfg->ias != 48 || cfg->oas > 40)
1027d08d42deSRob Herring 		return NULL;
1028d08d42deSRob Herring 
1029d08d42deSRob Herring 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
1030d08d42deSRob Herring 	iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
1031d08d42deSRob Herring 	if (iop) {
1032d08d42deSRob Herring 		u64 mair, ttbr;
1033d08d42deSRob Herring 
1034d08d42deSRob Herring 		/* Copy values as union fields overlap */
1035d08d42deSRob Herring 		mair = cfg->arm_lpae_s1_cfg.mair[0];
1036d08d42deSRob Herring 		ttbr = cfg->arm_lpae_s1_cfg.ttbr[0];
1037d08d42deSRob Herring 
1038d08d42deSRob Herring 		cfg->arm_mali_lpae_cfg.memattr = mair;
1039d08d42deSRob Herring 		cfg->arm_mali_lpae_cfg.transtab = ttbr |
1040d08d42deSRob Herring 			ARM_MALI_LPAE_TTBR_READ_INNER |
1041d08d42deSRob Herring 			ARM_MALI_LPAE_TTBR_ADRMODE_TABLE;
1042d08d42deSRob Herring 	}
1043d08d42deSRob Herring 
1044d08d42deSRob Herring 	return iop;
1045d08d42deSRob Herring }
1046d08d42deSRob Herring 
1047e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
1048e1d3c0fdSWill Deacon 	.alloc	= arm_64_lpae_alloc_pgtable_s1,
1049e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1050e1d3c0fdSWill Deacon };
1051e1d3c0fdSWill Deacon 
1052e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
1053e1d3c0fdSWill Deacon 	.alloc	= arm_64_lpae_alloc_pgtable_s2,
1054e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1055e1d3c0fdSWill Deacon };
1056e1d3c0fdSWill Deacon 
1057e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
1058e1d3c0fdSWill Deacon 	.alloc	= arm_32_lpae_alloc_pgtable_s1,
1059e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1060e1d3c0fdSWill Deacon };
1061e1d3c0fdSWill Deacon 
1062e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
1063e1d3c0fdSWill Deacon 	.alloc	= arm_32_lpae_alloc_pgtable_s2,
1064e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
1065e1d3c0fdSWill Deacon };
1066fe4b991dSWill Deacon 
1067d08d42deSRob Herring struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns = {
1068d08d42deSRob Herring 	.alloc	= arm_mali_lpae_alloc_pgtable,
1069d08d42deSRob Herring 	.free	= arm_lpae_free_pgtable,
1070d08d42deSRob Herring };
1071d08d42deSRob Herring 
1072fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
1073fe4b991dSWill Deacon 
1074fe4b991dSWill Deacon static struct io_pgtable_cfg *cfg_cookie;
1075fe4b991dSWill Deacon 
1076fe4b991dSWill Deacon static void dummy_tlb_flush_all(void *cookie)
1077fe4b991dSWill Deacon {
1078fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
1079fe4b991dSWill Deacon }
1080fe4b991dSWill Deacon 
108106c610e8SRobin Murphy static void dummy_tlb_add_flush(unsigned long iova, size_t size,
108206c610e8SRobin Murphy 				size_t granule, bool leaf, void *cookie)
1083fe4b991dSWill Deacon {
1084fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
1085fe4b991dSWill Deacon 	WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
1086fe4b991dSWill Deacon }
1087fe4b991dSWill Deacon 
1088fe4b991dSWill Deacon static void dummy_tlb_sync(void *cookie)
1089fe4b991dSWill Deacon {
1090fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
1091fe4b991dSWill Deacon }
1092fe4b991dSWill Deacon 
1093dfed5f01SBhumika Goyal static const struct iommu_gather_ops dummy_tlb_ops __initconst = {
1094fe4b991dSWill Deacon 	.tlb_flush_all	= dummy_tlb_flush_all,
1095fe4b991dSWill Deacon 	.tlb_add_flush	= dummy_tlb_add_flush,
1096fe4b991dSWill Deacon 	.tlb_sync	= dummy_tlb_sync,
1097fe4b991dSWill Deacon };
1098fe4b991dSWill Deacon 
1099fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
1100fe4b991dSWill Deacon {
1101fe4b991dSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
1102fe4b991dSWill Deacon 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
1103fe4b991dSWill Deacon 
1104fe4b991dSWill Deacon 	pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
1105fe4b991dSWill Deacon 		cfg->pgsize_bitmap, cfg->ias);
1106fe4b991dSWill Deacon 	pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
1107fe4b991dSWill Deacon 		data->levels, data->pgd_size, data->pg_shift,
1108fe4b991dSWill Deacon 		data->bits_per_level, data->pgd);
1109fe4b991dSWill Deacon }
1110fe4b991dSWill Deacon 
1111fe4b991dSWill Deacon #define __FAIL(ops, i)	({						\
1112fe4b991dSWill Deacon 		WARN(1, "selftest: test failed for fmt idx %d\n", (i));	\
1113fe4b991dSWill Deacon 		arm_lpae_dump_ops(ops);					\
1114fe4b991dSWill Deacon 		selftest_running = false;				\
1115fe4b991dSWill Deacon 		-EFAULT;						\
1116fe4b991dSWill Deacon })
1117fe4b991dSWill Deacon 
1118fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
1119fe4b991dSWill Deacon {
1120fe4b991dSWill Deacon 	static const enum io_pgtable_fmt fmts[] = {
1121fe4b991dSWill Deacon 		ARM_64_LPAE_S1,
1122fe4b991dSWill Deacon 		ARM_64_LPAE_S2,
1123fe4b991dSWill Deacon 	};
1124fe4b991dSWill Deacon 
1125fe4b991dSWill Deacon 	int i, j;
1126fe4b991dSWill Deacon 	unsigned long iova;
1127fe4b991dSWill Deacon 	size_t size;
1128fe4b991dSWill Deacon 	struct io_pgtable_ops *ops;
1129fe4b991dSWill Deacon 
1130fe4b991dSWill Deacon 	selftest_running = true;
1131fe4b991dSWill Deacon 
1132fe4b991dSWill Deacon 	for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
1133fe4b991dSWill Deacon 		cfg_cookie = cfg;
1134fe4b991dSWill Deacon 		ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
1135fe4b991dSWill Deacon 		if (!ops) {
1136fe4b991dSWill Deacon 			pr_err("selftest: failed to allocate io pgtable ops\n");
1137fe4b991dSWill Deacon 			return -ENOMEM;
1138fe4b991dSWill Deacon 		}
1139fe4b991dSWill Deacon 
1140fe4b991dSWill Deacon 		/*
1141fe4b991dSWill Deacon 		 * Initial sanity checks.
1142fe4b991dSWill Deacon 		 * Empty page tables shouldn't provide any translations.
1143fe4b991dSWill Deacon 		 */
1144fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, 42))
1145fe4b991dSWill Deacon 			return __FAIL(ops, i);
1146fe4b991dSWill Deacon 
1147fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_1G + 42))
1148fe4b991dSWill Deacon 			return __FAIL(ops, i);
1149fe4b991dSWill Deacon 
1150fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_2G + 42))
1151fe4b991dSWill Deacon 			return __FAIL(ops, i);
1152fe4b991dSWill Deacon 
1153fe4b991dSWill Deacon 		/*
1154fe4b991dSWill Deacon 		 * Distinct mappings of different granule sizes.
1155fe4b991dSWill Deacon 		 */
1156fe4b991dSWill Deacon 		iova = 0;
11574ae8a5c5SKefeng Wang 		for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1158fe4b991dSWill Deacon 			size = 1UL << j;
1159fe4b991dSWill Deacon 
1160fe4b991dSWill Deacon 			if (ops->map(ops, iova, iova, size, IOMMU_READ |
1161fe4b991dSWill Deacon 							    IOMMU_WRITE |
1162fe4b991dSWill Deacon 							    IOMMU_NOEXEC |
1163fe4b991dSWill Deacon 							    IOMMU_CACHE))
1164fe4b991dSWill Deacon 				return __FAIL(ops, i);
1165fe4b991dSWill Deacon 
1166fe4b991dSWill Deacon 			/* Overlapping mappings */
1167fe4b991dSWill Deacon 			if (!ops->map(ops, iova, iova + size, size,
1168fe4b991dSWill Deacon 				      IOMMU_READ | IOMMU_NOEXEC))
1169fe4b991dSWill Deacon 				return __FAIL(ops, i);
1170fe4b991dSWill Deacon 
1171fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1172fe4b991dSWill Deacon 				return __FAIL(ops, i);
1173fe4b991dSWill Deacon 
1174fe4b991dSWill Deacon 			iova += SZ_1G;
1175fe4b991dSWill Deacon 		}
1176fe4b991dSWill Deacon 
1177fe4b991dSWill Deacon 		/* Partial unmap */
1178fe4b991dSWill Deacon 		size = 1UL << __ffs(cfg->pgsize_bitmap);
1179fe4b991dSWill Deacon 		if (ops->unmap(ops, SZ_1G + size, size) != size)
1180fe4b991dSWill Deacon 			return __FAIL(ops, i);
1181fe4b991dSWill Deacon 
1182fe4b991dSWill Deacon 		/* Remap of partial unmap */
1183fe4b991dSWill Deacon 		if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
1184fe4b991dSWill Deacon 			return __FAIL(ops, i);
1185fe4b991dSWill Deacon 
1186fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
1187fe4b991dSWill Deacon 			return __FAIL(ops, i);
1188fe4b991dSWill Deacon 
1189fe4b991dSWill Deacon 		/* Full unmap */
1190fe4b991dSWill Deacon 		iova = 0;
1191f793b13eSYueHaibing 		for_each_set_bit(j, &cfg->pgsize_bitmap, BITS_PER_LONG) {
1192fe4b991dSWill Deacon 			size = 1UL << j;
1193fe4b991dSWill Deacon 
1194fe4b991dSWill Deacon 			if (ops->unmap(ops, iova, size) != size)
1195fe4b991dSWill Deacon 				return __FAIL(ops, i);
1196fe4b991dSWill Deacon 
1197fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42))
1198fe4b991dSWill Deacon 				return __FAIL(ops, i);
1199fe4b991dSWill Deacon 
1200fe4b991dSWill Deacon 			/* Remap full block */
1201fe4b991dSWill Deacon 			if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
1202fe4b991dSWill Deacon 				return __FAIL(ops, i);
1203fe4b991dSWill Deacon 
1204fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
1205fe4b991dSWill Deacon 				return __FAIL(ops, i);
1206fe4b991dSWill Deacon 
1207fe4b991dSWill Deacon 			iova += SZ_1G;
1208fe4b991dSWill Deacon 		}
1209fe4b991dSWill Deacon 
1210fe4b991dSWill Deacon 		free_io_pgtable_ops(ops);
1211fe4b991dSWill Deacon 	}
1212fe4b991dSWill Deacon 
1213fe4b991dSWill Deacon 	selftest_running = false;
1214fe4b991dSWill Deacon 	return 0;
1215fe4b991dSWill Deacon }
1216fe4b991dSWill Deacon 
1217fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void)
1218fe4b991dSWill Deacon {
1219fe4b991dSWill Deacon 	static const unsigned long pgsize[] = {
1220fe4b991dSWill Deacon 		SZ_4K | SZ_2M | SZ_1G,
1221fe4b991dSWill Deacon 		SZ_16K | SZ_32M,
1222fe4b991dSWill Deacon 		SZ_64K | SZ_512M,
1223fe4b991dSWill Deacon 	};
1224fe4b991dSWill Deacon 
1225fe4b991dSWill Deacon 	static const unsigned int ias[] = {
1226fe4b991dSWill Deacon 		32, 36, 40, 42, 44, 48,
1227fe4b991dSWill Deacon 	};
1228fe4b991dSWill Deacon 
1229fe4b991dSWill Deacon 	int i, j, pass = 0, fail = 0;
1230fe4b991dSWill Deacon 	struct io_pgtable_cfg cfg = {
1231fe4b991dSWill Deacon 		.tlb = &dummy_tlb_ops,
1232fe4b991dSWill Deacon 		.oas = 48,
123381b3c252SRobin Murphy 		.quirks = IO_PGTABLE_QUIRK_NO_DMA,
1234fe4b991dSWill Deacon 	};
1235fe4b991dSWill Deacon 
1236fe4b991dSWill Deacon 	for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
1237fe4b991dSWill Deacon 		for (j = 0; j < ARRAY_SIZE(ias); ++j) {
1238fe4b991dSWill Deacon 			cfg.pgsize_bitmap = pgsize[i];
1239fe4b991dSWill Deacon 			cfg.ias = ias[j];
1240fe4b991dSWill Deacon 			pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
1241fe4b991dSWill Deacon 				pgsize[i], ias[j]);
1242fe4b991dSWill Deacon 			if (arm_lpae_run_tests(&cfg))
1243fe4b991dSWill Deacon 				fail++;
1244fe4b991dSWill Deacon 			else
1245fe4b991dSWill Deacon 				pass++;
1246fe4b991dSWill Deacon 		}
1247fe4b991dSWill Deacon 	}
1248fe4b991dSWill Deacon 
1249fe4b991dSWill Deacon 	pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
1250fe4b991dSWill Deacon 	return fail ? -EFAULT : 0;
1251fe4b991dSWill Deacon }
1252fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests);
1253fe4b991dSWill Deacon #endif
1254