xref: /openbmc/linux/drivers/iommu/io-pgtable-arm.c (revision fe4b991dcd84e0104cf2e29223a819335ed048a7)
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 
23e1d3c0fdSWill Deacon #include <linux/iommu.h>
24e1d3c0fdSWill Deacon #include <linux/kernel.h>
25e1d3c0fdSWill Deacon #include <linux/sizes.h>
26e1d3c0fdSWill Deacon #include <linux/slab.h>
27e1d3c0fdSWill Deacon #include <linux/types.h>
28e1d3c0fdSWill Deacon 
29e1d3c0fdSWill Deacon #include "io-pgtable.h"
30e1d3c0fdSWill Deacon 
31e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_ADDR_BITS		48
32e1d3c0fdSWill Deacon #define ARM_LPAE_S2_MAX_CONCAT_PAGES	16
33e1d3c0fdSWill Deacon #define ARM_LPAE_MAX_LEVELS		4
34e1d3c0fdSWill Deacon 
35e1d3c0fdSWill Deacon /* Struct accessors */
36e1d3c0fdSWill Deacon #define io_pgtable_to_data(x)						\
37e1d3c0fdSWill Deacon 	container_of((x), struct arm_lpae_io_pgtable, iop)
38e1d3c0fdSWill Deacon 
39e1d3c0fdSWill Deacon #define io_pgtable_ops_to_pgtable(x)					\
40e1d3c0fdSWill Deacon 	container_of((x), struct io_pgtable, ops)
41e1d3c0fdSWill Deacon 
42e1d3c0fdSWill Deacon #define io_pgtable_ops_to_data(x)					\
43e1d3c0fdSWill Deacon 	io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
44e1d3c0fdSWill Deacon 
45e1d3c0fdSWill Deacon /*
46e1d3c0fdSWill Deacon  * For consistency with the architecture, we always consider
47e1d3c0fdSWill Deacon  * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0
48e1d3c0fdSWill Deacon  */
49e1d3c0fdSWill Deacon #define ARM_LPAE_START_LVL(d)		(ARM_LPAE_MAX_LEVELS - (d)->levels)
50e1d3c0fdSWill Deacon 
51e1d3c0fdSWill Deacon /*
52e1d3c0fdSWill Deacon  * Calculate the right shift amount to get to the portion describing level l
53e1d3c0fdSWill Deacon  * in a virtual address mapped by the pagetable in d.
54e1d3c0fdSWill Deacon  */
55e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_SHIFT(l,d)						\
56e1d3c0fdSWill Deacon 	((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1))		\
57e1d3c0fdSWill Deacon 	  * (d)->bits_per_level) + (d)->pg_shift)
58e1d3c0fdSWill Deacon 
59e1d3c0fdSWill Deacon #define ARM_LPAE_PAGES_PER_PGD(d)	((d)->pgd_size >> (d)->pg_shift)
60e1d3c0fdSWill Deacon 
61e1d3c0fdSWill Deacon /*
62e1d3c0fdSWill Deacon  * Calculate the index at level l used to map virtual address a using the
63e1d3c0fdSWill Deacon  * pagetable in d.
64e1d3c0fdSWill Deacon  */
65e1d3c0fdSWill Deacon #define ARM_LPAE_PGD_IDX(l,d)						\
66e1d3c0fdSWill Deacon 	((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0)
67e1d3c0fdSWill Deacon 
68e1d3c0fdSWill Deacon #define ARM_LPAE_LVL_IDX(a,l,d)						\
69e1d3c0fdSWill Deacon 	(((a) >> ARM_LPAE_LVL_SHIFT(l,d)) &				\
70e1d3c0fdSWill Deacon 	 ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1))
71e1d3c0fdSWill Deacon 
72e1d3c0fdSWill Deacon /* Calculate the block/page mapping size at level l for pagetable in d. */
73e1d3c0fdSWill Deacon #define ARM_LPAE_BLOCK_SIZE(l,d)					\
74e1d3c0fdSWill Deacon 	(1 << (ilog2(sizeof(arm_lpae_iopte)) +				\
75e1d3c0fdSWill Deacon 		((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level)))
76e1d3c0fdSWill Deacon 
77e1d3c0fdSWill Deacon /* Page table bits */
78e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_SHIFT		0
79e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_MASK		0x3
80e1d3c0fdSWill Deacon 
81e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_BLOCK		1
82e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_TABLE		3
83e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_TYPE_PAGE		3
84e1d3c0fdSWill Deacon 
85e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_XN			(((arm_lpae_iopte)3) << 53)
86e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AF			(((arm_lpae_iopte)1) << 10)
87e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_NS		(((arm_lpae_iopte)0) << 8)
88e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_OS		(((arm_lpae_iopte)2) << 8)
89e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_SH_IS		(((arm_lpae_iopte)3) << 8)
90e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_VALID		(((arm_lpae_iopte)1) << 0)
91e1d3c0fdSWill Deacon 
92e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_LO_MASK	(((arm_lpae_iopte)0x3ff) << 2)
93e1d3c0fdSWill Deacon /* Ignore the contiguous bit for block splitting */
94e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_HI_MASK	(((arm_lpae_iopte)6) << 52)
95e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTR_MASK		(ARM_LPAE_PTE_ATTR_LO_MASK |	\
96e1d3c0fdSWill Deacon 					 ARM_LPAE_PTE_ATTR_HI_MASK)
97e1d3c0fdSWill Deacon 
98e1d3c0fdSWill Deacon /* Stage-1 PTE */
99e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_UNPRIV		(((arm_lpae_iopte)1) << 6)
100e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_AP_RDONLY		(((arm_lpae_iopte)2) << 6)
101e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_ATTRINDX_SHIFT	2
102e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_nG			(((arm_lpae_iopte)1) << 11)
103e1d3c0fdSWill Deacon 
104e1d3c0fdSWill Deacon /* Stage-2 PTE */
105e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_FAULT		(((arm_lpae_iopte)0) << 6)
106e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_READ		(((arm_lpae_iopte)1) << 6)
107e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_HAP_WRITE		(((arm_lpae_iopte)2) << 6)
108e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_OIWB	(((arm_lpae_iopte)0xf) << 2)
109e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_NC		(((arm_lpae_iopte)0x5) << 2)
110e1d3c0fdSWill Deacon #define ARM_LPAE_PTE_MEMATTR_DEV	(((arm_lpae_iopte)0x1) << 2)
111e1d3c0fdSWill Deacon 
112e1d3c0fdSWill Deacon /* Register bits */
113e1d3c0fdSWill Deacon #define ARM_32_LPAE_TCR_EAE		(1 << 31)
114e1d3c0fdSWill Deacon #define ARM_64_LPAE_S2_TCR_RES1		(1 << 31)
115e1d3c0fdSWill Deacon 
116e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_4K		(0 << 14)
117e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_64K		(1 << 14)
118e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_TG0_16K		(2 << 14)
119e1d3c0fdSWill Deacon 
120e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_SHIFT		12
121e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH0_MASK		0x3
122e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_NS		0
123e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_OS		2
124e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SH_IS		3
125e1d3c0fdSWill Deacon 
126e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_ORGN0_SHIFT	10
127e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IRGN0_SHIFT	8
128e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_MASK		0x3
129e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_NC		0
130e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WBWA		1
131e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WT		2
132e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_RGN_WB		3
133e1d3c0fdSWill Deacon 
134e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_SHIFT		6
135e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SL0_MASK		0x3
136e1d3c0fdSWill Deacon 
137e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_T0SZ_SHIFT		0
138e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_SZ_MASK		0xf
139e1d3c0fdSWill Deacon 
140e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_SHIFT		16
141e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_MASK		0x7
142e1d3c0fdSWill Deacon 
143e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_SHIFT		32
144e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_IPS_MASK		0x7
145e1d3c0fdSWill Deacon 
146e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_32_BIT		0x0ULL
147e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_36_BIT		0x1ULL
148e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_40_BIT		0x2ULL
149e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_42_BIT		0x3ULL
150e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_44_BIT		0x4ULL
151e1d3c0fdSWill Deacon #define ARM_LPAE_TCR_PS_48_BIT		0x5ULL
152e1d3c0fdSWill Deacon 
153e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_SHIFT(n)	((n) << 3)
154e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_MASK		0xff
155e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_DEVICE	0x04
156e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_NC		0x44
157e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_WBRWA	0xff
158e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_NC	0
159e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_CACHE	1
160e1d3c0fdSWill Deacon #define ARM_LPAE_MAIR_ATTR_IDX_DEV	2
161e1d3c0fdSWill Deacon 
162e1d3c0fdSWill Deacon /* IOPTE accessors */
163e1d3c0fdSWill Deacon #define iopte_deref(pte,d)					\
164e1d3c0fdSWill Deacon 	(__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)	\
165e1d3c0fdSWill Deacon 	& ~((1ULL << (d)->pg_shift) - 1)))
166e1d3c0fdSWill Deacon 
167e1d3c0fdSWill Deacon #define iopte_type(pte,l)					\
168e1d3c0fdSWill Deacon 	(((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK)
169e1d3c0fdSWill Deacon 
170e1d3c0fdSWill Deacon #define iopte_prot(pte)	((pte) & ARM_LPAE_PTE_ATTR_MASK)
171e1d3c0fdSWill Deacon 
172e1d3c0fdSWill Deacon #define iopte_leaf(pte,l)					\
173e1d3c0fdSWill Deacon 	(l == (ARM_LPAE_MAX_LEVELS - 1) ?			\
174e1d3c0fdSWill Deacon 		(iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) :	\
175e1d3c0fdSWill Deacon 		(iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK))
176e1d3c0fdSWill Deacon 
177e1d3c0fdSWill Deacon #define iopte_to_pfn(pte,d)					\
178e1d3c0fdSWill Deacon 	(((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift)
179e1d3c0fdSWill Deacon 
180e1d3c0fdSWill Deacon #define pfn_to_iopte(pfn,d)					\
181e1d3c0fdSWill Deacon 	(((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1))
182e1d3c0fdSWill Deacon 
183e1d3c0fdSWill Deacon struct arm_lpae_io_pgtable {
184e1d3c0fdSWill Deacon 	struct io_pgtable	iop;
185e1d3c0fdSWill Deacon 
186e1d3c0fdSWill Deacon 	int			levels;
187e1d3c0fdSWill Deacon 	size_t			pgd_size;
188e1d3c0fdSWill Deacon 	unsigned long		pg_shift;
189e1d3c0fdSWill Deacon 	unsigned long		bits_per_level;
190e1d3c0fdSWill Deacon 
191e1d3c0fdSWill Deacon 	void			*pgd;
192e1d3c0fdSWill Deacon };
193e1d3c0fdSWill Deacon 
194e1d3c0fdSWill Deacon typedef u64 arm_lpae_iopte;
195e1d3c0fdSWill Deacon 
196*fe4b991dSWill Deacon static bool selftest_running = false;
197*fe4b991dSWill Deacon 
198e1d3c0fdSWill Deacon static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data,
199e1d3c0fdSWill Deacon 			     unsigned long iova, phys_addr_t paddr,
200e1d3c0fdSWill Deacon 			     arm_lpae_iopte prot, int lvl,
201e1d3c0fdSWill Deacon 			     arm_lpae_iopte *ptep)
202e1d3c0fdSWill Deacon {
203e1d3c0fdSWill Deacon 	arm_lpae_iopte pte = prot;
204e1d3c0fdSWill Deacon 
205e1d3c0fdSWill Deacon 	/* We require an unmap first */
206*fe4b991dSWill Deacon 	if (iopte_leaf(*ptep, lvl)) {
207*fe4b991dSWill Deacon 		WARN_ON(!selftest_running);
208e1d3c0fdSWill Deacon 		return -EEXIST;
209*fe4b991dSWill Deacon 	}
210e1d3c0fdSWill Deacon 
211e1d3c0fdSWill Deacon 	if (lvl == ARM_LPAE_MAX_LEVELS - 1)
212e1d3c0fdSWill Deacon 		pte |= ARM_LPAE_PTE_TYPE_PAGE;
213e1d3c0fdSWill Deacon 	else
214e1d3c0fdSWill Deacon 		pte |= ARM_LPAE_PTE_TYPE_BLOCK;
215e1d3c0fdSWill Deacon 
216e1d3c0fdSWill Deacon 	pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS;
217e1d3c0fdSWill Deacon 	pte |= pfn_to_iopte(paddr >> data->pg_shift, data);
218e1d3c0fdSWill Deacon 
219e1d3c0fdSWill Deacon 	*ptep = pte;
220e1d3c0fdSWill Deacon 	data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), data->iop.cookie);
221e1d3c0fdSWill Deacon 	return 0;
222e1d3c0fdSWill Deacon }
223e1d3c0fdSWill Deacon 
224e1d3c0fdSWill Deacon static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova,
225e1d3c0fdSWill Deacon 			  phys_addr_t paddr, size_t size, arm_lpae_iopte prot,
226e1d3c0fdSWill Deacon 			  int lvl, arm_lpae_iopte *ptep)
227e1d3c0fdSWill Deacon {
228e1d3c0fdSWill Deacon 	arm_lpae_iopte *cptep, pte;
229e1d3c0fdSWill Deacon 	void *cookie = data->iop.cookie;
230e1d3c0fdSWill Deacon 	size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
231e1d3c0fdSWill Deacon 
232e1d3c0fdSWill Deacon 	/* Find our entry at the current level */
233e1d3c0fdSWill Deacon 	ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
234e1d3c0fdSWill Deacon 
235e1d3c0fdSWill Deacon 	/* If we can install a leaf entry at this level, then do so */
236e1d3c0fdSWill Deacon 	if (size == block_size && (size & data->iop.cfg.pgsize_bitmap))
237e1d3c0fdSWill Deacon 		return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep);
238e1d3c0fdSWill Deacon 
239e1d3c0fdSWill Deacon 	/* We can't allocate tables at the final level */
240e1d3c0fdSWill Deacon 	if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1))
241e1d3c0fdSWill Deacon 		return -EINVAL;
242e1d3c0fdSWill Deacon 
243e1d3c0fdSWill Deacon 	/* Grab a pointer to the next level */
244e1d3c0fdSWill Deacon 	pte = *ptep;
245e1d3c0fdSWill Deacon 	if (!pte) {
246e1d3c0fdSWill Deacon 		cptep = alloc_pages_exact(1UL << data->pg_shift,
247e1d3c0fdSWill Deacon 					 GFP_ATOMIC | __GFP_ZERO);
248e1d3c0fdSWill Deacon 		if (!cptep)
249e1d3c0fdSWill Deacon 			return -ENOMEM;
250e1d3c0fdSWill Deacon 
251e1d3c0fdSWill Deacon 		data->iop.cfg.tlb->flush_pgtable(cptep, 1UL << data->pg_shift,
252e1d3c0fdSWill Deacon 						 cookie);
253e1d3c0fdSWill Deacon 		pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE;
254e1d3c0fdSWill Deacon 		*ptep = pte;
255e1d3c0fdSWill Deacon 		data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), cookie);
256e1d3c0fdSWill Deacon 	} else {
257e1d3c0fdSWill Deacon 		cptep = iopte_deref(pte, data);
258e1d3c0fdSWill Deacon 	}
259e1d3c0fdSWill Deacon 
260e1d3c0fdSWill Deacon 	/* Rinse, repeat */
261e1d3c0fdSWill Deacon 	return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep);
262e1d3c0fdSWill Deacon }
263e1d3c0fdSWill Deacon 
264e1d3c0fdSWill Deacon static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data,
265e1d3c0fdSWill Deacon 					   int prot)
266e1d3c0fdSWill Deacon {
267e1d3c0fdSWill Deacon 	arm_lpae_iopte pte;
268e1d3c0fdSWill Deacon 
269e1d3c0fdSWill Deacon 	if (data->iop.fmt == ARM_64_LPAE_S1 ||
270e1d3c0fdSWill Deacon 	    data->iop.fmt == ARM_32_LPAE_S1) {
271e1d3c0fdSWill Deacon 		pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG;
272e1d3c0fdSWill Deacon 
273e1d3c0fdSWill Deacon 		if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ))
274e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_AP_RDONLY;
275e1d3c0fdSWill Deacon 
276e1d3c0fdSWill Deacon 		if (prot & IOMMU_CACHE)
277e1d3c0fdSWill Deacon 			pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE
278e1d3c0fdSWill Deacon 				<< ARM_LPAE_PTE_ATTRINDX_SHIFT);
279e1d3c0fdSWill Deacon 	} else {
280e1d3c0fdSWill Deacon 		pte = ARM_LPAE_PTE_HAP_FAULT;
281e1d3c0fdSWill Deacon 		if (prot & IOMMU_READ)
282e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_HAP_READ;
283e1d3c0fdSWill Deacon 		if (prot & IOMMU_WRITE)
284e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_HAP_WRITE;
285e1d3c0fdSWill Deacon 		if (prot & IOMMU_CACHE)
286e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_MEMATTR_OIWB;
287e1d3c0fdSWill Deacon 		else
288e1d3c0fdSWill Deacon 			pte |= ARM_LPAE_PTE_MEMATTR_NC;
289e1d3c0fdSWill Deacon 	}
290e1d3c0fdSWill Deacon 
291e1d3c0fdSWill Deacon 	if (prot & IOMMU_NOEXEC)
292e1d3c0fdSWill Deacon 		pte |= ARM_LPAE_PTE_XN;
293e1d3c0fdSWill Deacon 
294e1d3c0fdSWill Deacon 	return pte;
295e1d3c0fdSWill Deacon }
296e1d3c0fdSWill Deacon 
297e1d3c0fdSWill Deacon static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova,
298e1d3c0fdSWill Deacon 			phys_addr_t paddr, size_t size, int iommu_prot)
299e1d3c0fdSWill Deacon {
300e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
301e1d3c0fdSWill Deacon 	arm_lpae_iopte *ptep = data->pgd;
302e1d3c0fdSWill Deacon 	int lvl = ARM_LPAE_START_LVL(data);
303e1d3c0fdSWill Deacon 	arm_lpae_iopte prot;
304e1d3c0fdSWill Deacon 
305e1d3c0fdSWill Deacon 	/* If no access, then nothing to do */
306e1d3c0fdSWill Deacon 	if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
307e1d3c0fdSWill Deacon 		return 0;
308e1d3c0fdSWill Deacon 
309e1d3c0fdSWill Deacon 	prot = arm_lpae_prot_to_pte(data, iommu_prot);
310e1d3c0fdSWill Deacon 	return __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep);
311e1d3c0fdSWill Deacon }
312e1d3c0fdSWill Deacon 
313e1d3c0fdSWill Deacon static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl,
314e1d3c0fdSWill Deacon 				    arm_lpae_iopte *ptep)
315e1d3c0fdSWill Deacon {
316e1d3c0fdSWill Deacon 	arm_lpae_iopte *start, *end;
317e1d3c0fdSWill Deacon 	unsigned long table_size;
318e1d3c0fdSWill Deacon 
319e1d3c0fdSWill Deacon 	/* Only leaf entries at the last level */
320e1d3c0fdSWill Deacon 	if (lvl == ARM_LPAE_MAX_LEVELS - 1)
321e1d3c0fdSWill Deacon 		return;
322e1d3c0fdSWill Deacon 
323e1d3c0fdSWill Deacon 	if (lvl == ARM_LPAE_START_LVL(data))
324e1d3c0fdSWill Deacon 		table_size = data->pgd_size;
325e1d3c0fdSWill Deacon 	else
326e1d3c0fdSWill Deacon 		table_size = 1UL << data->pg_shift;
327e1d3c0fdSWill Deacon 
328e1d3c0fdSWill Deacon 	start = ptep;
329e1d3c0fdSWill Deacon 	end = (void *)ptep + table_size;
330e1d3c0fdSWill Deacon 
331e1d3c0fdSWill Deacon 	while (ptep != end) {
332e1d3c0fdSWill Deacon 		arm_lpae_iopte pte = *ptep++;
333e1d3c0fdSWill Deacon 
334e1d3c0fdSWill Deacon 		if (!pte || iopte_leaf(pte, lvl))
335e1d3c0fdSWill Deacon 			continue;
336e1d3c0fdSWill Deacon 
337e1d3c0fdSWill Deacon 		__arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data));
338e1d3c0fdSWill Deacon 	}
339e1d3c0fdSWill Deacon 
340e1d3c0fdSWill Deacon 	free_pages_exact(start, table_size);
341e1d3c0fdSWill Deacon }
342e1d3c0fdSWill Deacon 
343e1d3c0fdSWill Deacon static void arm_lpae_free_pgtable(struct io_pgtable *iop)
344e1d3c0fdSWill Deacon {
345e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop);
346e1d3c0fdSWill Deacon 
347e1d3c0fdSWill Deacon 	__arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd);
348e1d3c0fdSWill Deacon 	kfree(data);
349e1d3c0fdSWill Deacon }
350e1d3c0fdSWill Deacon 
351e1d3c0fdSWill Deacon static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data,
352e1d3c0fdSWill Deacon 				    unsigned long iova, size_t size,
353e1d3c0fdSWill Deacon 				    arm_lpae_iopte prot, int lvl,
354e1d3c0fdSWill Deacon 				    arm_lpae_iopte *ptep, size_t blk_size)
355e1d3c0fdSWill Deacon {
356e1d3c0fdSWill Deacon 	unsigned long blk_start, blk_end;
357e1d3c0fdSWill Deacon 	phys_addr_t blk_paddr;
358e1d3c0fdSWill Deacon 	arm_lpae_iopte table = 0;
359e1d3c0fdSWill Deacon 	void *cookie = data->iop.cookie;
360e1d3c0fdSWill Deacon 	const struct iommu_gather_ops *tlb = data->iop.cfg.tlb;
361e1d3c0fdSWill Deacon 
362e1d3c0fdSWill Deacon 	blk_start = iova & ~(blk_size - 1);
363e1d3c0fdSWill Deacon 	blk_end = blk_start + blk_size;
364e1d3c0fdSWill Deacon 	blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift;
365e1d3c0fdSWill Deacon 
366e1d3c0fdSWill Deacon 	for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
367e1d3c0fdSWill Deacon 		arm_lpae_iopte *tablep;
368e1d3c0fdSWill Deacon 
369e1d3c0fdSWill Deacon 		/* Unmap! */
370e1d3c0fdSWill Deacon 		if (blk_start == iova)
371e1d3c0fdSWill Deacon 			continue;
372e1d3c0fdSWill Deacon 
373e1d3c0fdSWill Deacon 		/* __arm_lpae_map expects a pointer to the start of the table */
374e1d3c0fdSWill Deacon 		tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data);
375e1d3c0fdSWill Deacon 		if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl,
376e1d3c0fdSWill Deacon 				   tablep) < 0) {
377e1d3c0fdSWill Deacon 			if (table) {
378e1d3c0fdSWill Deacon 				/* Free the table we allocated */
379e1d3c0fdSWill Deacon 				tablep = iopte_deref(table, data);
380e1d3c0fdSWill Deacon 				__arm_lpae_free_pgtable(data, lvl + 1, tablep);
381e1d3c0fdSWill Deacon 			}
382e1d3c0fdSWill Deacon 			return 0; /* Bytes unmapped */
383e1d3c0fdSWill Deacon 		}
384e1d3c0fdSWill Deacon 	}
385e1d3c0fdSWill Deacon 
386e1d3c0fdSWill Deacon 	*ptep = table;
387e1d3c0fdSWill Deacon 	tlb->flush_pgtable(ptep, sizeof(*ptep), cookie);
388e1d3c0fdSWill Deacon 	iova &= ~(blk_size - 1);
389e1d3c0fdSWill Deacon 	tlb->tlb_add_flush(iova, blk_size, true, cookie);
390e1d3c0fdSWill Deacon 	return size;
391e1d3c0fdSWill Deacon }
392e1d3c0fdSWill Deacon 
393e1d3c0fdSWill Deacon static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data,
394e1d3c0fdSWill Deacon 			    unsigned long iova, size_t size, int lvl,
395e1d3c0fdSWill Deacon 			    arm_lpae_iopte *ptep)
396e1d3c0fdSWill Deacon {
397e1d3c0fdSWill Deacon 	arm_lpae_iopte pte;
398e1d3c0fdSWill Deacon 	const struct iommu_gather_ops *tlb = data->iop.cfg.tlb;
399e1d3c0fdSWill Deacon 	void *cookie = data->iop.cookie;
400e1d3c0fdSWill Deacon 	size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data);
401e1d3c0fdSWill Deacon 
402e1d3c0fdSWill Deacon 	ptep += ARM_LPAE_LVL_IDX(iova, lvl, data);
403e1d3c0fdSWill Deacon 	pte = *ptep;
404e1d3c0fdSWill Deacon 
405e1d3c0fdSWill Deacon 	/* Something went horribly wrong and we ran out of page table */
406e1d3c0fdSWill Deacon 	if (WARN_ON(!pte || (lvl == ARM_LPAE_MAX_LEVELS)))
407e1d3c0fdSWill Deacon 		return 0;
408e1d3c0fdSWill Deacon 
409e1d3c0fdSWill Deacon 	/* If the size matches this level, we're in the right place */
410e1d3c0fdSWill Deacon 	if (size == blk_size) {
411e1d3c0fdSWill Deacon 		*ptep = 0;
412e1d3c0fdSWill Deacon 		tlb->flush_pgtable(ptep, sizeof(*ptep), cookie);
413e1d3c0fdSWill Deacon 
414e1d3c0fdSWill Deacon 		if (!iopte_leaf(pte, lvl)) {
415e1d3c0fdSWill Deacon 			/* Also flush any partial walks */
416e1d3c0fdSWill Deacon 			tlb->tlb_add_flush(iova, size, false, cookie);
417e1d3c0fdSWill Deacon 			tlb->tlb_sync(data->iop.cookie);
418e1d3c0fdSWill Deacon 			ptep = iopte_deref(pte, data);
419e1d3c0fdSWill Deacon 			__arm_lpae_free_pgtable(data, lvl + 1, ptep);
420e1d3c0fdSWill Deacon 		} else {
421e1d3c0fdSWill Deacon 			tlb->tlb_add_flush(iova, size, true, cookie);
422e1d3c0fdSWill Deacon 		}
423e1d3c0fdSWill Deacon 
424e1d3c0fdSWill Deacon 		return size;
425e1d3c0fdSWill Deacon 	} else if (iopte_leaf(pte, lvl)) {
426e1d3c0fdSWill Deacon 		/*
427e1d3c0fdSWill Deacon 		 * Insert a table at the next level to map the old region,
428e1d3c0fdSWill Deacon 		 * minus the part we want to unmap
429e1d3c0fdSWill Deacon 		 */
430e1d3c0fdSWill Deacon 		return arm_lpae_split_blk_unmap(data, iova, size,
431e1d3c0fdSWill Deacon 						iopte_prot(pte), lvl, ptep,
432e1d3c0fdSWill Deacon 						blk_size);
433e1d3c0fdSWill Deacon 	}
434e1d3c0fdSWill Deacon 
435e1d3c0fdSWill Deacon 	/* Keep on walkin' */
436e1d3c0fdSWill Deacon 	ptep = iopte_deref(pte, data);
437e1d3c0fdSWill Deacon 	return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep);
438e1d3c0fdSWill Deacon }
439e1d3c0fdSWill Deacon 
440e1d3c0fdSWill Deacon static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova,
441e1d3c0fdSWill Deacon 			  size_t size)
442e1d3c0fdSWill Deacon {
443e1d3c0fdSWill Deacon 	size_t unmapped;
444e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
445e1d3c0fdSWill Deacon 	struct io_pgtable *iop = &data->iop;
446e1d3c0fdSWill Deacon 	arm_lpae_iopte *ptep = data->pgd;
447e1d3c0fdSWill Deacon 	int lvl = ARM_LPAE_START_LVL(data);
448e1d3c0fdSWill Deacon 
449e1d3c0fdSWill Deacon 	unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep);
450e1d3c0fdSWill Deacon 	if (unmapped)
451e1d3c0fdSWill Deacon 		iop->cfg.tlb->tlb_sync(iop->cookie);
452e1d3c0fdSWill Deacon 
453e1d3c0fdSWill Deacon 	return unmapped;
454e1d3c0fdSWill Deacon }
455e1d3c0fdSWill Deacon 
456e1d3c0fdSWill Deacon static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops,
457e1d3c0fdSWill Deacon 					 unsigned long iova)
458e1d3c0fdSWill Deacon {
459e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
460e1d3c0fdSWill Deacon 	arm_lpae_iopte pte, *ptep = data->pgd;
461e1d3c0fdSWill Deacon 	int lvl = ARM_LPAE_START_LVL(data);
462e1d3c0fdSWill Deacon 
463e1d3c0fdSWill Deacon 	do {
464e1d3c0fdSWill Deacon 		/* Valid IOPTE pointer? */
465e1d3c0fdSWill Deacon 		if (!ptep)
466e1d3c0fdSWill Deacon 			return 0;
467e1d3c0fdSWill Deacon 
468e1d3c0fdSWill Deacon 		/* Grab the IOPTE we're interested in */
469e1d3c0fdSWill Deacon 		pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data));
470e1d3c0fdSWill Deacon 
471e1d3c0fdSWill Deacon 		/* Valid entry? */
472e1d3c0fdSWill Deacon 		if (!pte)
473e1d3c0fdSWill Deacon 			return 0;
474e1d3c0fdSWill Deacon 
475e1d3c0fdSWill Deacon 		/* Leaf entry? */
476e1d3c0fdSWill Deacon 		if (iopte_leaf(pte,lvl))
477e1d3c0fdSWill Deacon 			goto found_translation;
478e1d3c0fdSWill Deacon 
479e1d3c0fdSWill Deacon 		/* Take it to the next level */
480e1d3c0fdSWill Deacon 		ptep = iopte_deref(pte, data);
481e1d3c0fdSWill Deacon 	} while (++lvl < ARM_LPAE_MAX_LEVELS);
482e1d3c0fdSWill Deacon 
483e1d3c0fdSWill Deacon 	/* Ran out of page tables to walk */
484e1d3c0fdSWill Deacon 	return 0;
485e1d3c0fdSWill Deacon 
486e1d3c0fdSWill Deacon found_translation:
487e1d3c0fdSWill Deacon 	iova &= ((1 << data->pg_shift) - 1);
488e1d3c0fdSWill Deacon 	return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova;
489e1d3c0fdSWill Deacon }
490e1d3c0fdSWill Deacon 
491e1d3c0fdSWill Deacon static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg)
492e1d3c0fdSWill Deacon {
493e1d3c0fdSWill Deacon 	unsigned long granule;
494e1d3c0fdSWill Deacon 
495e1d3c0fdSWill Deacon 	/*
496e1d3c0fdSWill Deacon 	 * We need to restrict the supported page sizes to match the
497e1d3c0fdSWill Deacon 	 * translation regime for a particular granule. Aim to match
498e1d3c0fdSWill Deacon 	 * the CPU page size if possible, otherwise prefer smaller sizes.
499e1d3c0fdSWill Deacon 	 * While we're at it, restrict the block sizes to match the
500e1d3c0fdSWill Deacon 	 * chosen granule.
501e1d3c0fdSWill Deacon 	 */
502e1d3c0fdSWill Deacon 	if (cfg->pgsize_bitmap & PAGE_SIZE)
503e1d3c0fdSWill Deacon 		granule = PAGE_SIZE;
504e1d3c0fdSWill Deacon 	else if (cfg->pgsize_bitmap & ~PAGE_MASK)
505e1d3c0fdSWill Deacon 		granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK);
506e1d3c0fdSWill Deacon 	else if (cfg->pgsize_bitmap & PAGE_MASK)
507e1d3c0fdSWill Deacon 		granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK);
508e1d3c0fdSWill Deacon 	else
509e1d3c0fdSWill Deacon 		granule = 0;
510e1d3c0fdSWill Deacon 
511e1d3c0fdSWill Deacon 	switch (granule) {
512e1d3c0fdSWill Deacon 	case SZ_4K:
513e1d3c0fdSWill Deacon 		cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
514e1d3c0fdSWill Deacon 		break;
515e1d3c0fdSWill Deacon 	case SZ_16K:
516e1d3c0fdSWill Deacon 		cfg->pgsize_bitmap &= (SZ_16K | SZ_32M);
517e1d3c0fdSWill Deacon 		break;
518e1d3c0fdSWill Deacon 	case SZ_64K:
519e1d3c0fdSWill Deacon 		cfg->pgsize_bitmap &= (SZ_64K | SZ_512M);
520e1d3c0fdSWill Deacon 		break;
521e1d3c0fdSWill Deacon 	default:
522e1d3c0fdSWill Deacon 		cfg->pgsize_bitmap = 0;
523e1d3c0fdSWill Deacon 	}
524e1d3c0fdSWill Deacon }
525e1d3c0fdSWill Deacon 
526e1d3c0fdSWill Deacon static struct arm_lpae_io_pgtable *
527e1d3c0fdSWill Deacon arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
528e1d3c0fdSWill Deacon {
529e1d3c0fdSWill Deacon 	unsigned long va_bits, pgd_bits;
530e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data;
531e1d3c0fdSWill Deacon 
532e1d3c0fdSWill Deacon 	arm_lpae_restrict_pgsizes(cfg);
533e1d3c0fdSWill Deacon 
534e1d3c0fdSWill Deacon 	if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K)))
535e1d3c0fdSWill Deacon 		return NULL;
536e1d3c0fdSWill Deacon 
537e1d3c0fdSWill Deacon 	if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS)
538e1d3c0fdSWill Deacon 		return NULL;
539e1d3c0fdSWill Deacon 
540e1d3c0fdSWill Deacon 	if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
541e1d3c0fdSWill Deacon 		return NULL;
542e1d3c0fdSWill Deacon 
543e1d3c0fdSWill Deacon 	data = kmalloc(sizeof(*data), GFP_KERNEL);
544e1d3c0fdSWill Deacon 	if (!data)
545e1d3c0fdSWill Deacon 		return NULL;
546e1d3c0fdSWill Deacon 
547e1d3c0fdSWill Deacon 	data->pg_shift = __ffs(cfg->pgsize_bitmap);
548e1d3c0fdSWill Deacon 	data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte));
549e1d3c0fdSWill Deacon 
550e1d3c0fdSWill Deacon 	va_bits = cfg->ias - data->pg_shift;
551e1d3c0fdSWill Deacon 	data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level);
552e1d3c0fdSWill Deacon 
553e1d3c0fdSWill Deacon 	/* Calculate the actual size of our pgd (without concatenation) */
554e1d3c0fdSWill Deacon 	pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1));
555e1d3c0fdSWill Deacon 	data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte)));
556e1d3c0fdSWill Deacon 
557e1d3c0fdSWill Deacon 	data->iop.ops = (struct io_pgtable_ops) {
558e1d3c0fdSWill Deacon 		.map		= arm_lpae_map,
559e1d3c0fdSWill Deacon 		.unmap		= arm_lpae_unmap,
560e1d3c0fdSWill Deacon 		.iova_to_phys	= arm_lpae_iova_to_phys,
561e1d3c0fdSWill Deacon 	};
562e1d3c0fdSWill Deacon 
563e1d3c0fdSWill Deacon 	return data;
564e1d3c0fdSWill Deacon }
565e1d3c0fdSWill Deacon 
566e1d3c0fdSWill Deacon static struct io_pgtable *
567e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
568e1d3c0fdSWill Deacon {
569e1d3c0fdSWill Deacon 	u64 reg;
570e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
571e1d3c0fdSWill Deacon 
572e1d3c0fdSWill Deacon 	if (!data)
573e1d3c0fdSWill Deacon 		return NULL;
574e1d3c0fdSWill Deacon 
575e1d3c0fdSWill Deacon 	/* TCR */
576e1d3c0fdSWill Deacon 	reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
577e1d3c0fdSWill Deacon 	      (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
578e1d3c0fdSWill Deacon 	      (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
579e1d3c0fdSWill Deacon 
580e1d3c0fdSWill Deacon 	switch (1 << data->pg_shift) {
581e1d3c0fdSWill Deacon 	case SZ_4K:
582e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_4K;
583e1d3c0fdSWill Deacon 		break;
584e1d3c0fdSWill Deacon 	case SZ_16K:
585e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_16K;
586e1d3c0fdSWill Deacon 		break;
587e1d3c0fdSWill Deacon 	case SZ_64K:
588e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_64K;
589e1d3c0fdSWill Deacon 		break;
590e1d3c0fdSWill Deacon 	}
591e1d3c0fdSWill Deacon 
592e1d3c0fdSWill Deacon 	switch (cfg->oas) {
593e1d3c0fdSWill Deacon 	case 32:
594e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT);
595e1d3c0fdSWill Deacon 		break;
596e1d3c0fdSWill Deacon 	case 36:
597e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT);
598e1d3c0fdSWill Deacon 		break;
599e1d3c0fdSWill Deacon 	case 40:
600e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT);
601e1d3c0fdSWill Deacon 		break;
602e1d3c0fdSWill Deacon 	case 42:
603e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT);
604e1d3c0fdSWill Deacon 		break;
605e1d3c0fdSWill Deacon 	case 44:
606e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT);
607e1d3c0fdSWill Deacon 		break;
608e1d3c0fdSWill Deacon 	case 48:
609e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT);
610e1d3c0fdSWill Deacon 		break;
611e1d3c0fdSWill Deacon 	default:
612e1d3c0fdSWill Deacon 		goto out_free_data;
613e1d3c0fdSWill Deacon 	}
614e1d3c0fdSWill Deacon 
615e1d3c0fdSWill Deacon 	reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
616e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.tcr = reg;
617e1d3c0fdSWill Deacon 
618e1d3c0fdSWill Deacon 	/* MAIRs */
619e1d3c0fdSWill Deacon 	reg = (ARM_LPAE_MAIR_ATTR_NC
620e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) |
621e1d3c0fdSWill Deacon 	      (ARM_LPAE_MAIR_ATTR_WBRWA
622e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) |
623e1d3c0fdSWill Deacon 	      (ARM_LPAE_MAIR_ATTR_DEVICE
624e1d3c0fdSWill Deacon 	       << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV));
625e1d3c0fdSWill Deacon 
626e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.mair[0] = reg;
627e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.mair[1] = 0;
628e1d3c0fdSWill Deacon 
629e1d3c0fdSWill Deacon 	/* Looking good; allocate a pgd */
630e1d3c0fdSWill Deacon 	data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO);
631e1d3c0fdSWill Deacon 	if (!data->pgd)
632e1d3c0fdSWill Deacon 		goto out_free_data;
633e1d3c0fdSWill Deacon 
634e1d3c0fdSWill Deacon 	cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie);
635e1d3c0fdSWill Deacon 
636e1d3c0fdSWill Deacon 	/* TTBRs */
637e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd);
638e1d3c0fdSWill Deacon 	cfg->arm_lpae_s1_cfg.ttbr[1] = 0;
639e1d3c0fdSWill Deacon 	return &data->iop;
640e1d3c0fdSWill Deacon 
641e1d3c0fdSWill Deacon out_free_data:
642e1d3c0fdSWill Deacon 	kfree(data);
643e1d3c0fdSWill Deacon 	return NULL;
644e1d3c0fdSWill Deacon }
645e1d3c0fdSWill Deacon 
646e1d3c0fdSWill Deacon static struct io_pgtable *
647e1d3c0fdSWill Deacon arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
648e1d3c0fdSWill Deacon {
649e1d3c0fdSWill Deacon 	u64 reg, sl;
650e1d3c0fdSWill Deacon 	struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg);
651e1d3c0fdSWill Deacon 
652e1d3c0fdSWill Deacon 	if (!data)
653e1d3c0fdSWill Deacon 		return NULL;
654e1d3c0fdSWill Deacon 
655e1d3c0fdSWill Deacon 	/*
656e1d3c0fdSWill Deacon 	 * Concatenate PGDs at level 1 if possible in order to reduce
657e1d3c0fdSWill Deacon 	 * the depth of the stage-2 walk.
658e1d3c0fdSWill Deacon 	 */
659e1d3c0fdSWill Deacon 	if (data->levels == ARM_LPAE_MAX_LEVELS) {
660e1d3c0fdSWill Deacon 		unsigned long pgd_pages;
661e1d3c0fdSWill Deacon 
662e1d3c0fdSWill Deacon 		pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte));
663e1d3c0fdSWill Deacon 		if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) {
664e1d3c0fdSWill Deacon 			data->pgd_size = pgd_pages << data->pg_shift;
665e1d3c0fdSWill Deacon 			data->levels--;
666e1d3c0fdSWill Deacon 		}
667e1d3c0fdSWill Deacon 	}
668e1d3c0fdSWill Deacon 
669e1d3c0fdSWill Deacon 	/* VTCR */
670e1d3c0fdSWill Deacon 	reg = ARM_64_LPAE_S2_TCR_RES1 |
671e1d3c0fdSWill Deacon 	     (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) |
672e1d3c0fdSWill Deacon 	     (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) |
673e1d3c0fdSWill Deacon 	     (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT);
674e1d3c0fdSWill Deacon 
675e1d3c0fdSWill Deacon 	sl = ARM_LPAE_START_LVL(data);
676e1d3c0fdSWill Deacon 
677e1d3c0fdSWill Deacon 	switch (1 << data->pg_shift) {
678e1d3c0fdSWill Deacon 	case SZ_4K:
679e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_4K;
680e1d3c0fdSWill Deacon 		sl++; /* SL0 format is different for 4K granule size */
681e1d3c0fdSWill Deacon 		break;
682e1d3c0fdSWill Deacon 	case SZ_16K:
683e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_16K;
684e1d3c0fdSWill Deacon 		break;
685e1d3c0fdSWill Deacon 	case SZ_64K:
686e1d3c0fdSWill Deacon 		reg |= ARM_LPAE_TCR_TG0_64K;
687e1d3c0fdSWill Deacon 		break;
688e1d3c0fdSWill Deacon 	}
689e1d3c0fdSWill Deacon 
690e1d3c0fdSWill Deacon 	switch (cfg->oas) {
691e1d3c0fdSWill Deacon 	case 32:
692e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT);
693e1d3c0fdSWill Deacon 		break;
694e1d3c0fdSWill Deacon 	case 36:
695e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT);
696e1d3c0fdSWill Deacon 		break;
697e1d3c0fdSWill Deacon 	case 40:
698e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT);
699e1d3c0fdSWill Deacon 		break;
700e1d3c0fdSWill Deacon 	case 42:
701e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT);
702e1d3c0fdSWill Deacon 		break;
703e1d3c0fdSWill Deacon 	case 44:
704e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT);
705e1d3c0fdSWill Deacon 		break;
706e1d3c0fdSWill Deacon 	case 48:
707e1d3c0fdSWill Deacon 		reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT);
708e1d3c0fdSWill Deacon 		break;
709e1d3c0fdSWill Deacon 	default:
710e1d3c0fdSWill Deacon 		goto out_free_data;
711e1d3c0fdSWill Deacon 	}
712e1d3c0fdSWill Deacon 
713e1d3c0fdSWill Deacon 	reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT;
714e1d3c0fdSWill Deacon 	reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT;
715e1d3c0fdSWill Deacon 	cfg->arm_lpae_s2_cfg.vtcr = reg;
716e1d3c0fdSWill Deacon 
717e1d3c0fdSWill Deacon 	/* Allocate pgd pages */
718e1d3c0fdSWill Deacon 	data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO);
719e1d3c0fdSWill Deacon 	if (!data->pgd)
720e1d3c0fdSWill Deacon 		goto out_free_data;
721e1d3c0fdSWill Deacon 
722e1d3c0fdSWill Deacon 	cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie);
723e1d3c0fdSWill Deacon 
724e1d3c0fdSWill Deacon 	/* VTTBR */
725e1d3c0fdSWill Deacon 	cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd);
726e1d3c0fdSWill Deacon 	return &data->iop;
727e1d3c0fdSWill Deacon 
728e1d3c0fdSWill Deacon out_free_data:
729e1d3c0fdSWill Deacon 	kfree(data);
730e1d3c0fdSWill Deacon 	return NULL;
731e1d3c0fdSWill Deacon }
732e1d3c0fdSWill Deacon 
733e1d3c0fdSWill Deacon static struct io_pgtable *
734e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie)
735e1d3c0fdSWill Deacon {
736e1d3c0fdSWill Deacon 	struct io_pgtable *iop;
737e1d3c0fdSWill Deacon 
738e1d3c0fdSWill Deacon 	if (cfg->ias > 32 || cfg->oas > 40)
739e1d3c0fdSWill Deacon 		return NULL;
740e1d3c0fdSWill Deacon 
741e1d3c0fdSWill Deacon 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
742e1d3c0fdSWill Deacon 	iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie);
743e1d3c0fdSWill Deacon 	if (iop) {
744e1d3c0fdSWill Deacon 		cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE;
745e1d3c0fdSWill Deacon 		cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff;
746e1d3c0fdSWill Deacon 	}
747e1d3c0fdSWill Deacon 
748e1d3c0fdSWill Deacon 	return iop;
749e1d3c0fdSWill Deacon }
750e1d3c0fdSWill Deacon 
751e1d3c0fdSWill Deacon static struct io_pgtable *
752e1d3c0fdSWill Deacon arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie)
753e1d3c0fdSWill Deacon {
754e1d3c0fdSWill Deacon 	struct io_pgtable *iop;
755e1d3c0fdSWill Deacon 
756e1d3c0fdSWill Deacon 	if (cfg->ias > 40 || cfg->oas > 40)
757e1d3c0fdSWill Deacon 		return NULL;
758e1d3c0fdSWill Deacon 
759e1d3c0fdSWill Deacon 	cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G);
760e1d3c0fdSWill Deacon 	iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie);
761e1d3c0fdSWill Deacon 	if (iop)
762e1d3c0fdSWill Deacon 		cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff;
763e1d3c0fdSWill Deacon 
764e1d3c0fdSWill Deacon 	return iop;
765e1d3c0fdSWill Deacon }
766e1d3c0fdSWill Deacon 
767e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = {
768e1d3c0fdSWill Deacon 	.alloc	= arm_64_lpae_alloc_pgtable_s1,
769e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
770e1d3c0fdSWill Deacon };
771e1d3c0fdSWill Deacon 
772e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = {
773e1d3c0fdSWill Deacon 	.alloc	= arm_64_lpae_alloc_pgtable_s2,
774e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
775e1d3c0fdSWill Deacon };
776e1d3c0fdSWill Deacon 
777e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = {
778e1d3c0fdSWill Deacon 	.alloc	= arm_32_lpae_alloc_pgtable_s1,
779e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
780e1d3c0fdSWill Deacon };
781e1d3c0fdSWill Deacon 
782e1d3c0fdSWill Deacon struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = {
783e1d3c0fdSWill Deacon 	.alloc	= arm_32_lpae_alloc_pgtable_s2,
784e1d3c0fdSWill Deacon 	.free	= arm_lpae_free_pgtable,
785e1d3c0fdSWill Deacon };
786*fe4b991dSWill Deacon 
787*fe4b991dSWill Deacon #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST
788*fe4b991dSWill Deacon 
789*fe4b991dSWill Deacon static struct io_pgtable_cfg *cfg_cookie;
790*fe4b991dSWill Deacon 
791*fe4b991dSWill Deacon static void dummy_tlb_flush_all(void *cookie)
792*fe4b991dSWill Deacon {
793*fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
794*fe4b991dSWill Deacon }
795*fe4b991dSWill Deacon 
796*fe4b991dSWill Deacon static void dummy_tlb_add_flush(unsigned long iova, size_t size, bool leaf,
797*fe4b991dSWill Deacon 				void *cookie)
798*fe4b991dSWill Deacon {
799*fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
800*fe4b991dSWill Deacon 	WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
801*fe4b991dSWill Deacon }
802*fe4b991dSWill Deacon 
803*fe4b991dSWill Deacon static void dummy_tlb_sync(void *cookie)
804*fe4b991dSWill Deacon {
805*fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
806*fe4b991dSWill Deacon }
807*fe4b991dSWill Deacon 
808*fe4b991dSWill Deacon static void dummy_flush_pgtable(void *ptr, size_t size, void *cookie)
809*fe4b991dSWill Deacon {
810*fe4b991dSWill Deacon 	WARN_ON(cookie != cfg_cookie);
811*fe4b991dSWill Deacon }
812*fe4b991dSWill Deacon 
813*fe4b991dSWill Deacon static struct iommu_gather_ops dummy_tlb_ops __initdata = {
814*fe4b991dSWill Deacon 	.tlb_flush_all	= dummy_tlb_flush_all,
815*fe4b991dSWill Deacon 	.tlb_add_flush	= dummy_tlb_add_flush,
816*fe4b991dSWill Deacon 	.tlb_sync	= dummy_tlb_sync,
817*fe4b991dSWill Deacon 	.flush_pgtable	= dummy_flush_pgtable,
818*fe4b991dSWill Deacon };
819*fe4b991dSWill Deacon 
820*fe4b991dSWill Deacon static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops)
821*fe4b991dSWill Deacon {
822*fe4b991dSWill Deacon 	struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops);
823*fe4b991dSWill Deacon 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
824*fe4b991dSWill Deacon 
825*fe4b991dSWill Deacon 	pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n",
826*fe4b991dSWill Deacon 		cfg->pgsize_bitmap, cfg->ias);
827*fe4b991dSWill Deacon 	pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n",
828*fe4b991dSWill Deacon 		data->levels, data->pgd_size, data->pg_shift,
829*fe4b991dSWill Deacon 		data->bits_per_level, data->pgd);
830*fe4b991dSWill Deacon }
831*fe4b991dSWill Deacon 
832*fe4b991dSWill Deacon #define __FAIL(ops, i)	({						\
833*fe4b991dSWill Deacon 		WARN(1, "selftest: test failed for fmt idx %d\n", (i));	\
834*fe4b991dSWill Deacon 		arm_lpae_dump_ops(ops);					\
835*fe4b991dSWill Deacon 		selftest_running = false;				\
836*fe4b991dSWill Deacon 		-EFAULT;						\
837*fe4b991dSWill Deacon })
838*fe4b991dSWill Deacon 
839*fe4b991dSWill Deacon static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg)
840*fe4b991dSWill Deacon {
841*fe4b991dSWill Deacon 	static const enum io_pgtable_fmt fmts[] = {
842*fe4b991dSWill Deacon 		ARM_64_LPAE_S1,
843*fe4b991dSWill Deacon 		ARM_64_LPAE_S2,
844*fe4b991dSWill Deacon 	};
845*fe4b991dSWill Deacon 
846*fe4b991dSWill Deacon 	int i, j;
847*fe4b991dSWill Deacon 	unsigned long iova;
848*fe4b991dSWill Deacon 	size_t size;
849*fe4b991dSWill Deacon 	struct io_pgtable_ops *ops;
850*fe4b991dSWill Deacon 
851*fe4b991dSWill Deacon 	selftest_running = true;
852*fe4b991dSWill Deacon 
853*fe4b991dSWill Deacon 	for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
854*fe4b991dSWill Deacon 		cfg_cookie = cfg;
855*fe4b991dSWill Deacon 		ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg);
856*fe4b991dSWill Deacon 		if (!ops) {
857*fe4b991dSWill Deacon 			pr_err("selftest: failed to allocate io pgtable ops\n");
858*fe4b991dSWill Deacon 			return -ENOMEM;
859*fe4b991dSWill Deacon 		}
860*fe4b991dSWill Deacon 
861*fe4b991dSWill Deacon 		/*
862*fe4b991dSWill Deacon 		 * Initial sanity checks.
863*fe4b991dSWill Deacon 		 * Empty page tables shouldn't provide any translations.
864*fe4b991dSWill Deacon 		 */
865*fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, 42))
866*fe4b991dSWill Deacon 			return __FAIL(ops, i);
867*fe4b991dSWill Deacon 
868*fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_1G + 42))
869*fe4b991dSWill Deacon 			return __FAIL(ops, i);
870*fe4b991dSWill Deacon 
871*fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_2G + 42))
872*fe4b991dSWill Deacon 			return __FAIL(ops, i);
873*fe4b991dSWill Deacon 
874*fe4b991dSWill Deacon 		/*
875*fe4b991dSWill Deacon 		 * Distinct mappings of different granule sizes.
876*fe4b991dSWill Deacon 		 */
877*fe4b991dSWill Deacon 		iova = 0;
878*fe4b991dSWill Deacon 		j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
879*fe4b991dSWill Deacon 		while (j != BITS_PER_LONG) {
880*fe4b991dSWill Deacon 			size = 1UL << j;
881*fe4b991dSWill Deacon 
882*fe4b991dSWill Deacon 			if (ops->map(ops, iova, iova, size, IOMMU_READ |
883*fe4b991dSWill Deacon 							    IOMMU_WRITE |
884*fe4b991dSWill Deacon 							    IOMMU_NOEXEC |
885*fe4b991dSWill Deacon 							    IOMMU_CACHE))
886*fe4b991dSWill Deacon 				return __FAIL(ops, i);
887*fe4b991dSWill Deacon 
888*fe4b991dSWill Deacon 			/* Overlapping mappings */
889*fe4b991dSWill Deacon 			if (!ops->map(ops, iova, iova + size, size,
890*fe4b991dSWill Deacon 				      IOMMU_READ | IOMMU_NOEXEC))
891*fe4b991dSWill Deacon 				return __FAIL(ops, i);
892*fe4b991dSWill Deacon 
893*fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
894*fe4b991dSWill Deacon 				return __FAIL(ops, i);
895*fe4b991dSWill Deacon 
896*fe4b991dSWill Deacon 			iova += SZ_1G;
897*fe4b991dSWill Deacon 			j++;
898*fe4b991dSWill Deacon 			j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
899*fe4b991dSWill Deacon 		}
900*fe4b991dSWill Deacon 
901*fe4b991dSWill Deacon 		/* Partial unmap */
902*fe4b991dSWill Deacon 		size = 1UL << __ffs(cfg->pgsize_bitmap);
903*fe4b991dSWill Deacon 		if (ops->unmap(ops, SZ_1G + size, size) != size)
904*fe4b991dSWill Deacon 			return __FAIL(ops, i);
905*fe4b991dSWill Deacon 
906*fe4b991dSWill Deacon 		/* Remap of partial unmap */
907*fe4b991dSWill Deacon 		if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ))
908*fe4b991dSWill Deacon 			return __FAIL(ops, i);
909*fe4b991dSWill Deacon 
910*fe4b991dSWill Deacon 		if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42))
911*fe4b991dSWill Deacon 			return __FAIL(ops, i);
912*fe4b991dSWill Deacon 
913*fe4b991dSWill Deacon 		/* Full unmap */
914*fe4b991dSWill Deacon 		iova = 0;
915*fe4b991dSWill Deacon 		j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG);
916*fe4b991dSWill Deacon 		while (j != BITS_PER_LONG) {
917*fe4b991dSWill Deacon 			size = 1UL << j;
918*fe4b991dSWill Deacon 
919*fe4b991dSWill Deacon 			if (ops->unmap(ops, iova, size) != size)
920*fe4b991dSWill Deacon 				return __FAIL(ops, i);
921*fe4b991dSWill Deacon 
922*fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42))
923*fe4b991dSWill Deacon 				return __FAIL(ops, i);
924*fe4b991dSWill Deacon 
925*fe4b991dSWill Deacon 			/* Remap full block */
926*fe4b991dSWill Deacon 			if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
927*fe4b991dSWill Deacon 				return __FAIL(ops, i);
928*fe4b991dSWill Deacon 
929*fe4b991dSWill Deacon 			if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
930*fe4b991dSWill Deacon 				return __FAIL(ops, i);
931*fe4b991dSWill Deacon 
932*fe4b991dSWill Deacon 			iova += SZ_1G;
933*fe4b991dSWill Deacon 			j++;
934*fe4b991dSWill Deacon 			j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j);
935*fe4b991dSWill Deacon 		}
936*fe4b991dSWill Deacon 
937*fe4b991dSWill Deacon 		free_io_pgtable_ops(ops);
938*fe4b991dSWill Deacon 	}
939*fe4b991dSWill Deacon 
940*fe4b991dSWill Deacon 	selftest_running = false;
941*fe4b991dSWill Deacon 	return 0;
942*fe4b991dSWill Deacon }
943*fe4b991dSWill Deacon 
944*fe4b991dSWill Deacon static int __init arm_lpae_do_selftests(void)
945*fe4b991dSWill Deacon {
946*fe4b991dSWill Deacon 	static const unsigned long pgsize[] = {
947*fe4b991dSWill Deacon 		SZ_4K | SZ_2M | SZ_1G,
948*fe4b991dSWill Deacon 		SZ_16K | SZ_32M,
949*fe4b991dSWill Deacon 		SZ_64K | SZ_512M,
950*fe4b991dSWill Deacon 	};
951*fe4b991dSWill Deacon 
952*fe4b991dSWill Deacon 	static const unsigned int ias[] = {
953*fe4b991dSWill Deacon 		32, 36, 40, 42, 44, 48,
954*fe4b991dSWill Deacon 	};
955*fe4b991dSWill Deacon 
956*fe4b991dSWill Deacon 	int i, j, pass = 0, fail = 0;
957*fe4b991dSWill Deacon 	struct io_pgtable_cfg cfg = {
958*fe4b991dSWill Deacon 		.tlb = &dummy_tlb_ops,
959*fe4b991dSWill Deacon 		.oas = 48,
960*fe4b991dSWill Deacon 	};
961*fe4b991dSWill Deacon 
962*fe4b991dSWill Deacon 	for (i = 0; i < ARRAY_SIZE(pgsize); ++i) {
963*fe4b991dSWill Deacon 		for (j = 0; j < ARRAY_SIZE(ias); ++j) {
964*fe4b991dSWill Deacon 			cfg.pgsize_bitmap = pgsize[i];
965*fe4b991dSWill Deacon 			cfg.ias = ias[j];
966*fe4b991dSWill Deacon 			pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n",
967*fe4b991dSWill Deacon 				pgsize[i], ias[j]);
968*fe4b991dSWill Deacon 			if (arm_lpae_run_tests(&cfg))
969*fe4b991dSWill Deacon 				fail++;
970*fe4b991dSWill Deacon 			else
971*fe4b991dSWill Deacon 				pass++;
972*fe4b991dSWill Deacon 		}
973*fe4b991dSWill Deacon 	}
974*fe4b991dSWill Deacon 
975*fe4b991dSWill Deacon 	pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail);
976*fe4b991dSWill Deacon 	return fail ? -EFAULT : 0;
977*fe4b991dSWill Deacon }
978*fe4b991dSWill Deacon subsys_initcall(arm_lpae_do_selftests);
979*fe4b991dSWill Deacon #endif
980