xref: /openbmc/linux/drivers/iommu/io-pgtable-dart.c (revision f3c3091b98d5d52df40aaf27f11530701d02ac56)
1745ef109SJanne Grunau // SPDX-License-Identifier: GPL-2.0-only
2745ef109SJanne Grunau /*
3745ef109SJanne Grunau  * Apple DART page table allocator.
4745ef109SJanne Grunau  *
5745ef109SJanne Grunau  * Copyright (C) 2022 The Asahi Linux Contributors
6745ef109SJanne Grunau  *
7745ef109SJanne Grunau  * Based on io-pgtable-arm.
8745ef109SJanne Grunau  *
9745ef109SJanne Grunau  * Copyright (C) 2014 ARM Limited
10745ef109SJanne Grunau  *
11745ef109SJanne Grunau  * Author: Will Deacon <will.deacon@arm.com>
12745ef109SJanne Grunau  */
13745ef109SJanne Grunau 
14745ef109SJanne Grunau #define pr_fmt(fmt)	"dart io-pgtable: " fmt
15745ef109SJanne Grunau 
16745ef109SJanne Grunau #include <linux/atomic.h>
17d8fe365aSSven Peter #include <linux/bitfield.h>
18745ef109SJanne Grunau #include <linux/bitops.h>
19745ef109SJanne Grunau #include <linux/io-pgtable.h>
20745ef109SJanne Grunau #include <linux/kernel.h>
21745ef109SJanne Grunau #include <linux/sizes.h>
22745ef109SJanne Grunau #include <linux/slab.h>
23745ef109SJanne Grunau #include <linux/types.h>
24745ef109SJanne Grunau 
25745ef109SJanne Grunau #include <asm/barrier.h>
26745ef109SJanne Grunau 
27745ef109SJanne Grunau #define DART1_MAX_ADDR_BITS	36
28745ef109SJanne Grunau 
29745ef109SJanne Grunau #define DART_MAX_TABLES		4
30745ef109SJanne Grunau #define DART_LEVELS		2
31745ef109SJanne Grunau 
32745ef109SJanne Grunau /* Struct accessors */
33745ef109SJanne Grunau #define io_pgtable_to_data(x)						\
34745ef109SJanne Grunau 	container_of((x), struct dart_io_pgtable, iop)
35745ef109SJanne Grunau 
36745ef109SJanne Grunau #define io_pgtable_ops_to_data(x)					\
37745ef109SJanne Grunau 	io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
38745ef109SJanne Grunau 
39745ef109SJanne Grunau #define DART_GRANULE(d)						\
40745ef109SJanne Grunau 	(sizeof(dart_iopte) << (d)->bits_per_level)
41745ef109SJanne Grunau #define DART_PTES_PER_TABLE(d)					\
42745ef109SJanne Grunau 	(DART_GRANULE(d) >> ilog2(sizeof(dart_iopte)))
43745ef109SJanne Grunau 
44d8fe365aSSven Peter #define APPLE_DART_PTE_SUBPAGE_START   GENMASK_ULL(63, 52)
45d8fe365aSSven Peter #define APPLE_DART_PTE_SUBPAGE_END     GENMASK_ULL(51, 40)
46d8fe365aSSven Peter 
47745ef109SJanne Grunau #define APPLE_DART1_PADDR_MASK	GENMASK_ULL(35, 12)
48dc09fe1cSSven Peter #define APPLE_DART2_PADDR_MASK	GENMASK_ULL(37, 10)
49dc09fe1cSSven Peter #define APPLE_DART2_PADDR_SHIFT	(4)
50745ef109SJanne Grunau 
51745ef109SJanne Grunau /* Apple DART1 protection bits */
52745ef109SJanne Grunau #define APPLE_DART1_PTE_PROT_NO_READ	BIT(8)
53745ef109SJanne Grunau #define APPLE_DART1_PTE_PROT_NO_WRITE	BIT(7)
54745ef109SJanne Grunau #define APPLE_DART1_PTE_PROT_SP_DIS	BIT(1)
55745ef109SJanne Grunau 
56dc09fe1cSSven Peter /* Apple DART2 protection bits */
57dc09fe1cSSven Peter #define APPLE_DART2_PTE_PROT_NO_READ	BIT(3)
58dc09fe1cSSven Peter #define APPLE_DART2_PTE_PROT_NO_WRITE	BIT(2)
59dc09fe1cSSven Peter #define APPLE_DART2_PTE_PROT_NO_CACHE	BIT(1)
60dc09fe1cSSven Peter 
61745ef109SJanne Grunau /* marks PTE as valid */
62745ef109SJanne Grunau #define APPLE_DART_PTE_VALID		BIT(0)
63745ef109SJanne Grunau 
64745ef109SJanne Grunau /* IOPTE accessors */
65745ef109SJanne Grunau #define iopte_deref(pte, d) __va(iopte_to_paddr(pte, d))
66745ef109SJanne Grunau 
67745ef109SJanne Grunau struct dart_io_pgtable {
68745ef109SJanne Grunau 	struct io_pgtable	iop;
69745ef109SJanne Grunau 
70745ef109SJanne Grunau 	int			tbl_bits;
71745ef109SJanne Grunau 	int			bits_per_level;
72745ef109SJanne Grunau 
73745ef109SJanne Grunau 	void			*pgd[DART_MAX_TABLES];
74745ef109SJanne Grunau };
75745ef109SJanne Grunau 
76745ef109SJanne Grunau typedef u64 dart_iopte;
77745ef109SJanne Grunau 
78745ef109SJanne Grunau 
paddr_to_iopte(phys_addr_t paddr,struct dart_io_pgtable * data)79745ef109SJanne Grunau static dart_iopte paddr_to_iopte(phys_addr_t paddr,
80745ef109SJanne Grunau 				     struct dart_io_pgtable *data)
81745ef109SJanne Grunau {
82dc09fe1cSSven Peter 	dart_iopte pte;
83dc09fe1cSSven Peter 
84dc09fe1cSSven Peter 	if (data->iop.fmt == APPLE_DART)
85745ef109SJanne Grunau 		return paddr & APPLE_DART1_PADDR_MASK;
86dc09fe1cSSven Peter 
87dc09fe1cSSven Peter 	/* format is APPLE_DART2 */
88dc09fe1cSSven Peter 	pte = paddr >> APPLE_DART2_PADDR_SHIFT;
89dc09fe1cSSven Peter 	pte &= APPLE_DART2_PADDR_MASK;
90dc09fe1cSSven Peter 
91dc09fe1cSSven Peter 	return pte;
92745ef109SJanne Grunau }
93745ef109SJanne Grunau 
iopte_to_paddr(dart_iopte pte,struct dart_io_pgtable * data)94745ef109SJanne Grunau static phys_addr_t iopte_to_paddr(dart_iopte pte,
95745ef109SJanne Grunau 				  struct dart_io_pgtable *data)
96745ef109SJanne Grunau {
97dc09fe1cSSven Peter 	u64 paddr;
98dc09fe1cSSven Peter 
99dc09fe1cSSven Peter 	if (data->iop.fmt == APPLE_DART)
100745ef109SJanne Grunau 		return pte & APPLE_DART1_PADDR_MASK;
101dc09fe1cSSven Peter 
102dc09fe1cSSven Peter 	/* format is APPLE_DART2 */
103dc09fe1cSSven Peter 	paddr = pte & APPLE_DART2_PADDR_MASK;
104dc09fe1cSSven Peter 	paddr <<= APPLE_DART2_PADDR_SHIFT;
105dc09fe1cSSven Peter 
106dc09fe1cSSven Peter 	return paddr;
107745ef109SJanne Grunau }
108745ef109SJanne Grunau 
__dart_alloc_pages(size_t size,gfp_t gfp,struct io_pgtable_cfg * cfg)109745ef109SJanne Grunau static void *__dart_alloc_pages(size_t size, gfp_t gfp,
110745ef109SJanne Grunau 				    struct io_pgtable_cfg *cfg)
111745ef109SJanne Grunau {
112745ef109SJanne Grunau 	int order = get_order(size);
113745ef109SJanne Grunau 	struct page *p;
114745ef109SJanne Grunau 
115745ef109SJanne Grunau 	VM_BUG_ON((gfp & __GFP_HIGHMEM));
116745ef109SJanne Grunau 	p = alloc_pages(gfp | __GFP_ZERO, order);
117745ef109SJanne Grunau 	if (!p)
118745ef109SJanne Grunau 		return NULL;
119745ef109SJanne Grunau 
120745ef109SJanne Grunau 	return page_address(p);
121745ef109SJanne Grunau }
122745ef109SJanne Grunau 
dart_init_pte(struct dart_io_pgtable * data,unsigned long iova,phys_addr_t paddr,dart_iopte prot,int num_entries,dart_iopte * ptep)123745ef109SJanne Grunau static int dart_init_pte(struct dart_io_pgtable *data,
124745ef109SJanne Grunau 			     unsigned long iova, phys_addr_t paddr,
125745ef109SJanne Grunau 			     dart_iopte prot, int num_entries,
126745ef109SJanne Grunau 			     dart_iopte *ptep)
127745ef109SJanne Grunau {
128745ef109SJanne Grunau 	int i;
129745ef109SJanne Grunau 	dart_iopte pte = prot;
130745ef109SJanne Grunau 	size_t sz = data->iop.cfg.pgsize_bitmap;
131745ef109SJanne Grunau 
132745ef109SJanne Grunau 	for (i = 0; i < num_entries; i++)
133745ef109SJanne Grunau 		if (ptep[i] & APPLE_DART_PTE_VALID) {
134745ef109SJanne Grunau 			/* We require an unmap first */
135745ef109SJanne Grunau 			WARN_ON(ptep[i] & APPLE_DART_PTE_VALID);
136745ef109SJanne Grunau 			return -EEXIST;
137745ef109SJanne Grunau 		}
138745ef109SJanne Grunau 
139d8fe365aSSven Peter 	/* subpage protection: always allow access to the entire page */
140d8fe365aSSven Peter 	pte |= FIELD_PREP(APPLE_DART_PTE_SUBPAGE_START, 0);
141d8fe365aSSven Peter 	pte |= FIELD_PREP(APPLE_DART_PTE_SUBPAGE_END, 0xfff);
142d8fe365aSSven Peter 
143745ef109SJanne Grunau 	pte |= APPLE_DART1_PTE_PROT_SP_DIS;
144745ef109SJanne Grunau 	pte |= APPLE_DART_PTE_VALID;
145745ef109SJanne Grunau 
146745ef109SJanne Grunau 	for (i = 0; i < num_entries; i++)
147745ef109SJanne Grunau 		ptep[i] = pte | paddr_to_iopte(paddr + i * sz, data);
148745ef109SJanne Grunau 
149745ef109SJanne Grunau 	return 0;
150745ef109SJanne Grunau }
151745ef109SJanne Grunau 
dart_install_table(dart_iopte * table,dart_iopte * ptep,dart_iopte curr,struct dart_io_pgtable * data)152745ef109SJanne Grunau static dart_iopte dart_install_table(dart_iopte *table,
153745ef109SJanne Grunau 					     dart_iopte *ptep,
154745ef109SJanne Grunau 					     dart_iopte curr,
155745ef109SJanne Grunau 					     struct dart_io_pgtable *data)
156745ef109SJanne Grunau {
157745ef109SJanne Grunau 	dart_iopte old, new;
158745ef109SJanne Grunau 
159745ef109SJanne Grunau 	new = paddr_to_iopte(__pa(table), data) | APPLE_DART_PTE_VALID;
160745ef109SJanne Grunau 
161745ef109SJanne Grunau 	/*
162745ef109SJanne Grunau 	 * Ensure the table itself is visible before its PTE can be.
163745ef109SJanne Grunau 	 * Whilst we could get away with cmpxchg64_release below, this
164745ef109SJanne Grunau 	 * doesn't have any ordering semantics when !CONFIG_SMP.
165745ef109SJanne Grunau 	 */
166745ef109SJanne Grunau 	dma_wmb();
167745ef109SJanne Grunau 
168745ef109SJanne Grunau 	old = cmpxchg64_relaxed(ptep, curr, new);
169745ef109SJanne Grunau 
170745ef109SJanne Grunau 	return old;
171745ef109SJanne Grunau }
172745ef109SJanne Grunau 
dart_get_table(struct dart_io_pgtable * data,unsigned long iova)173745ef109SJanne Grunau static int dart_get_table(struct dart_io_pgtable *data, unsigned long iova)
174745ef109SJanne Grunau {
175745ef109SJanne Grunau 	return (iova >> (3 * data->bits_per_level + ilog2(sizeof(dart_iopte)))) &
176745ef109SJanne Grunau 		((1 << data->tbl_bits) - 1);
177745ef109SJanne Grunau }
178745ef109SJanne Grunau 
dart_get_l1_index(struct dart_io_pgtable * data,unsigned long iova)179745ef109SJanne Grunau static int dart_get_l1_index(struct dart_io_pgtable *data, unsigned long iova)
180745ef109SJanne Grunau {
181745ef109SJanne Grunau 
182745ef109SJanne Grunau 	return (iova >> (2 * data->bits_per_level + ilog2(sizeof(dart_iopte)))) &
183745ef109SJanne Grunau 		 ((1 << data->bits_per_level) - 1);
184745ef109SJanne Grunau }
185745ef109SJanne Grunau 
dart_get_l2_index(struct dart_io_pgtable * data,unsigned long iova)186745ef109SJanne Grunau static int dart_get_l2_index(struct dart_io_pgtable *data, unsigned long iova)
187745ef109SJanne Grunau {
188745ef109SJanne Grunau 
189745ef109SJanne Grunau 	return (iova >> (data->bits_per_level + ilog2(sizeof(dart_iopte)))) &
190745ef109SJanne Grunau 		 ((1 << data->bits_per_level) - 1);
191745ef109SJanne Grunau }
192745ef109SJanne Grunau 
dart_get_l2(struct dart_io_pgtable * data,unsigned long iova)193745ef109SJanne Grunau static  dart_iopte *dart_get_l2(struct dart_io_pgtable *data, unsigned long iova)
194745ef109SJanne Grunau {
195745ef109SJanne Grunau 	dart_iopte pte, *ptep;
196745ef109SJanne Grunau 	int tbl = dart_get_table(data, iova);
197745ef109SJanne Grunau 
198745ef109SJanne Grunau 	ptep = data->pgd[tbl];
199745ef109SJanne Grunau 	if (!ptep)
200745ef109SJanne Grunau 		return NULL;
201745ef109SJanne Grunau 
202745ef109SJanne Grunau 	ptep += dart_get_l1_index(data, iova);
203745ef109SJanne Grunau 	pte = READ_ONCE(*ptep);
204745ef109SJanne Grunau 
205745ef109SJanne Grunau 	/* Valid entry? */
206745ef109SJanne Grunau 	if (!pte)
207745ef109SJanne Grunau 		return NULL;
208745ef109SJanne Grunau 
209745ef109SJanne Grunau 	/* Deref to get level 2 table */
210745ef109SJanne Grunau 	return iopte_deref(pte, data);
211745ef109SJanne Grunau }
212745ef109SJanne Grunau 
dart_prot_to_pte(struct dart_io_pgtable * data,int prot)213745ef109SJanne Grunau static dart_iopte dart_prot_to_pte(struct dart_io_pgtable *data,
214745ef109SJanne Grunau 					   int prot)
215745ef109SJanne Grunau {
216745ef109SJanne Grunau 	dart_iopte pte = 0;
217745ef109SJanne Grunau 
218dc09fe1cSSven Peter 	if (data->iop.fmt == APPLE_DART) {
219745ef109SJanne Grunau 		if (!(prot & IOMMU_WRITE))
220745ef109SJanne Grunau 			pte |= APPLE_DART1_PTE_PROT_NO_WRITE;
221745ef109SJanne Grunau 		if (!(prot & IOMMU_READ))
222745ef109SJanne Grunau 			pte |= APPLE_DART1_PTE_PROT_NO_READ;
223dc09fe1cSSven Peter 	}
224dc09fe1cSSven Peter 	if (data->iop.fmt == APPLE_DART2) {
225dc09fe1cSSven Peter 		if (!(prot & IOMMU_WRITE))
226dc09fe1cSSven Peter 			pte |= APPLE_DART2_PTE_PROT_NO_WRITE;
227dc09fe1cSSven Peter 		if (!(prot & IOMMU_READ))
228dc09fe1cSSven Peter 			pte |= APPLE_DART2_PTE_PROT_NO_READ;
229dc09fe1cSSven Peter 		if (!(prot & IOMMU_CACHE))
230dc09fe1cSSven Peter 			pte |= APPLE_DART2_PTE_PROT_NO_CACHE;
231dc09fe1cSSven Peter 	}
232745ef109SJanne Grunau 
233745ef109SJanne Grunau 	return pte;
234745ef109SJanne Grunau }
235745ef109SJanne Grunau 
dart_map_pages(struct io_pgtable_ops * ops,unsigned long iova,phys_addr_t paddr,size_t pgsize,size_t pgcount,int iommu_prot,gfp_t gfp,size_t * mapped)236745ef109SJanne Grunau static int dart_map_pages(struct io_pgtable_ops *ops, unsigned long iova,
237745ef109SJanne Grunau 			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
238745ef109SJanne Grunau 			      int iommu_prot, gfp_t gfp, size_t *mapped)
239745ef109SJanne Grunau {
240745ef109SJanne Grunau 	struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops);
241745ef109SJanne Grunau 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
242745ef109SJanne Grunau 	size_t tblsz = DART_GRANULE(data);
243745ef109SJanne Grunau 	int ret = 0, tbl, num_entries, max_entries, map_idx_start;
244745ef109SJanne Grunau 	dart_iopte pte, *cptep, *ptep;
245745ef109SJanne Grunau 	dart_iopte prot;
246745ef109SJanne Grunau 
247745ef109SJanne Grunau 	if (WARN_ON(pgsize != cfg->pgsize_bitmap))
248745ef109SJanne Grunau 		return -EINVAL;
249745ef109SJanne Grunau 
250745ef109SJanne Grunau 	if (WARN_ON(paddr >> cfg->oas))
251745ef109SJanne Grunau 		return -ERANGE;
252745ef109SJanne Grunau 
253745ef109SJanne Grunau 	if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE)))
254*99912d85SJason Gunthorpe 		return -EINVAL;
255745ef109SJanne Grunau 
256745ef109SJanne Grunau 	tbl = dart_get_table(data, iova);
257745ef109SJanne Grunau 
258745ef109SJanne Grunau 	ptep = data->pgd[tbl];
259745ef109SJanne Grunau 	ptep += dart_get_l1_index(data, iova);
260745ef109SJanne Grunau 	pte = READ_ONCE(*ptep);
261745ef109SJanne Grunau 
262745ef109SJanne Grunau 	/* no L2 table present */
263745ef109SJanne Grunau 	if (!pte) {
264745ef109SJanne Grunau 		cptep = __dart_alloc_pages(tblsz, gfp, cfg);
265745ef109SJanne Grunau 		if (!cptep)
266745ef109SJanne Grunau 			return -ENOMEM;
267745ef109SJanne Grunau 
268745ef109SJanne Grunau 		pte = dart_install_table(cptep, ptep, 0, data);
269745ef109SJanne Grunau 		if (pte)
270745ef109SJanne Grunau 			free_pages((unsigned long)cptep, get_order(tblsz));
271745ef109SJanne Grunau 
272745ef109SJanne Grunau 		/* L2 table is present (now) */
273745ef109SJanne Grunau 		pte = READ_ONCE(*ptep);
274745ef109SJanne Grunau 	}
275745ef109SJanne Grunau 
276745ef109SJanne Grunau 	ptep = iopte_deref(pte, data);
277745ef109SJanne Grunau 
278745ef109SJanne Grunau 	/* install a leaf entries into L2 table */
279745ef109SJanne Grunau 	prot = dart_prot_to_pte(data, iommu_prot);
280745ef109SJanne Grunau 	map_idx_start = dart_get_l2_index(data, iova);
281745ef109SJanne Grunau 	max_entries = DART_PTES_PER_TABLE(data) - map_idx_start;
282745ef109SJanne Grunau 	num_entries = min_t(int, pgcount, max_entries);
283745ef109SJanne Grunau 	ptep += map_idx_start;
284745ef109SJanne Grunau 	ret = dart_init_pte(data, iova, paddr, prot, num_entries, ptep);
285745ef109SJanne Grunau 	if (!ret && mapped)
286745ef109SJanne Grunau 		*mapped += num_entries * pgsize;
287745ef109SJanne Grunau 
288745ef109SJanne Grunau 	/*
289745ef109SJanne Grunau 	 * Synchronise all PTE updates for the new mapping before there's
290745ef109SJanne Grunau 	 * a chance for anything to kick off a table walk for the new iova.
291745ef109SJanne Grunau 	 */
292745ef109SJanne Grunau 	wmb();
293745ef109SJanne Grunau 
294745ef109SJanne Grunau 	return ret;
295745ef109SJanne Grunau }
296745ef109SJanne Grunau 
dart_unmap_pages(struct io_pgtable_ops * ops,unsigned long iova,size_t pgsize,size_t pgcount,struct iommu_iotlb_gather * gather)297745ef109SJanne Grunau static size_t dart_unmap_pages(struct io_pgtable_ops *ops, unsigned long iova,
298745ef109SJanne Grunau 				   size_t pgsize, size_t pgcount,
299745ef109SJanne Grunau 				   struct iommu_iotlb_gather *gather)
300745ef109SJanne Grunau {
301745ef109SJanne Grunau 	struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops);
302745ef109SJanne Grunau 	struct io_pgtable_cfg *cfg = &data->iop.cfg;
303745ef109SJanne Grunau 	int i = 0, num_entries, max_entries, unmap_idx_start;
304745ef109SJanne Grunau 	dart_iopte pte, *ptep;
305745ef109SJanne Grunau 
306745ef109SJanne Grunau 	if (WARN_ON(pgsize != cfg->pgsize_bitmap || !pgcount))
307745ef109SJanne Grunau 		return 0;
308745ef109SJanne Grunau 
309745ef109SJanne Grunau 	ptep = dart_get_l2(data, iova);
310745ef109SJanne Grunau 
311745ef109SJanne Grunau 	/* Valid L2 IOPTE pointer? */
312745ef109SJanne Grunau 	if (WARN_ON(!ptep))
313745ef109SJanne Grunau 		return 0;
314745ef109SJanne Grunau 
315745ef109SJanne Grunau 	unmap_idx_start = dart_get_l2_index(data, iova);
316745ef109SJanne Grunau 	ptep += unmap_idx_start;
317745ef109SJanne Grunau 
318745ef109SJanne Grunau 	max_entries = DART_PTES_PER_TABLE(data) - unmap_idx_start;
319745ef109SJanne Grunau 	num_entries = min_t(int, pgcount, max_entries);
320745ef109SJanne Grunau 
321745ef109SJanne Grunau 	while (i < num_entries) {
322745ef109SJanne Grunau 		pte = READ_ONCE(*ptep);
323745ef109SJanne Grunau 		if (WARN_ON(!pte))
324745ef109SJanne Grunau 			break;
325745ef109SJanne Grunau 
326745ef109SJanne Grunau 		/* clear pte */
327745ef109SJanne Grunau 		*ptep = 0;
328745ef109SJanne Grunau 
329745ef109SJanne Grunau 		if (!iommu_iotlb_gather_queued(gather))
330745ef109SJanne Grunau 			io_pgtable_tlb_add_page(&data->iop, gather,
331745ef109SJanne Grunau 						iova + i * pgsize, pgsize);
332745ef109SJanne Grunau 
333745ef109SJanne Grunau 		ptep++;
334745ef109SJanne Grunau 		i++;
335745ef109SJanne Grunau 	}
336745ef109SJanne Grunau 
337745ef109SJanne Grunau 	return i * pgsize;
338745ef109SJanne Grunau }
339745ef109SJanne Grunau 
dart_iova_to_phys(struct io_pgtable_ops * ops,unsigned long iova)340745ef109SJanne Grunau static phys_addr_t dart_iova_to_phys(struct io_pgtable_ops *ops,
341745ef109SJanne Grunau 					 unsigned long iova)
342745ef109SJanne Grunau {
343745ef109SJanne Grunau 	struct dart_io_pgtable *data = io_pgtable_ops_to_data(ops);
344745ef109SJanne Grunau 	dart_iopte pte, *ptep;
345745ef109SJanne Grunau 
346745ef109SJanne Grunau 	ptep = dart_get_l2(data, iova);
347745ef109SJanne Grunau 
348745ef109SJanne Grunau 	/* Valid L2 IOPTE pointer? */
349745ef109SJanne Grunau 	if (!ptep)
350745ef109SJanne Grunau 		return 0;
351745ef109SJanne Grunau 
352745ef109SJanne Grunau 	ptep += dart_get_l2_index(data, iova);
353745ef109SJanne Grunau 
354745ef109SJanne Grunau 	pte = READ_ONCE(*ptep);
355745ef109SJanne Grunau 	/* Found translation */
356745ef109SJanne Grunau 	if (pte) {
357745ef109SJanne Grunau 		iova &= (data->iop.cfg.pgsize_bitmap - 1);
358745ef109SJanne Grunau 		return iopte_to_paddr(pte, data) | iova;
359745ef109SJanne Grunau 	}
360745ef109SJanne Grunau 
361745ef109SJanne Grunau 	/* Ran out of page tables to walk */
362745ef109SJanne Grunau 	return 0;
363745ef109SJanne Grunau }
364745ef109SJanne Grunau 
365745ef109SJanne Grunau static struct dart_io_pgtable *
dart_alloc_pgtable(struct io_pgtable_cfg * cfg)366745ef109SJanne Grunau dart_alloc_pgtable(struct io_pgtable_cfg *cfg)
367745ef109SJanne Grunau {
368745ef109SJanne Grunau 	struct dart_io_pgtable *data;
369745ef109SJanne Grunau 	int tbl_bits, bits_per_level, va_bits, pg_shift;
370745ef109SJanne Grunau 
371745ef109SJanne Grunau 	pg_shift = __ffs(cfg->pgsize_bitmap);
372745ef109SJanne Grunau 	bits_per_level = pg_shift - ilog2(sizeof(dart_iopte));
373745ef109SJanne Grunau 
374745ef109SJanne Grunau 	va_bits = cfg->ias - pg_shift;
375745ef109SJanne Grunau 
376745ef109SJanne Grunau 	tbl_bits = max_t(int, 0, va_bits - (bits_per_level * DART_LEVELS));
377745ef109SJanne Grunau 	if ((1 << tbl_bits) > DART_MAX_TABLES)
378745ef109SJanne Grunau 		return NULL;
379745ef109SJanne Grunau 
380745ef109SJanne Grunau 	data = kzalloc(sizeof(*data), GFP_KERNEL);
381745ef109SJanne Grunau 	if (!data)
382745ef109SJanne Grunau 		return NULL;
383745ef109SJanne Grunau 
384745ef109SJanne Grunau 	data->tbl_bits = tbl_bits;
385745ef109SJanne Grunau 	data->bits_per_level = bits_per_level;
386745ef109SJanne Grunau 
387745ef109SJanne Grunau 	data->iop.ops = (struct io_pgtable_ops) {
388745ef109SJanne Grunau 		.map_pages	= dart_map_pages,
389745ef109SJanne Grunau 		.unmap_pages	= dart_unmap_pages,
390745ef109SJanne Grunau 		.iova_to_phys	= dart_iova_to_phys,
391745ef109SJanne Grunau 	};
392745ef109SJanne Grunau 
393745ef109SJanne Grunau 	return data;
394745ef109SJanne Grunau }
395745ef109SJanne Grunau 
396745ef109SJanne Grunau static struct io_pgtable *
apple_dart_alloc_pgtable(struct io_pgtable_cfg * cfg,void * cookie)397745ef109SJanne Grunau apple_dart_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
398745ef109SJanne Grunau {
399745ef109SJanne Grunau 	struct dart_io_pgtable *data;
400745ef109SJanne Grunau 	int i;
401745ef109SJanne Grunau 
402745ef109SJanne Grunau 	if (!cfg->coherent_walk)
403745ef109SJanne Grunau 		return NULL;
404745ef109SJanne Grunau 
405dc09fe1cSSven Peter 	if (cfg->oas != 36 && cfg->oas != 42)
406745ef109SJanne Grunau 		return NULL;
407745ef109SJanne Grunau 
408745ef109SJanne Grunau 	if (cfg->ias > cfg->oas)
409745ef109SJanne Grunau 		return NULL;
410745ef109SJanne Grunau 
411745ef109SJanne Grunau 	if (!(cfg->pgsize_bitmap == SZ_4K || cfg->pgsize_bitmap == SZ_16K))
412745ef109SJanne Grunau 		return NULL;
413745ef109SJanne Grunau 
414745ef109SJanne Grunau 	data = dart_alloc_pgtable(cfg);
415745ef109SJanne Grunau 	if (!data)
416745ef109SJanne Grunau 		return NULL;
417745ef109SJanne Grunau 
418745ef109SJanne Grunau 	cfg->apple_dart_cfg.n_ttbrs = 1 << data->tbl_bits;
419745ef109SJanne Grunau 
420745ef109SJanne Grunau 	for (i = 0; i < cfg->apple_dart_cfg.n_ttbrs; ++i) {
421745ef109SJanne Grunau 		data->pgd[i] = __dart_alloc_pages(DART_GRANULE(data), GFP_KERNEL,
422745ef109SJanne Grunau 					   cfg);
423745ef109SJanne Grunau 		if (!data->pgd[i])
424745ef109SJanne Grunau 			goto out_free_data;
425745ef109SJanne Grunau 		cfg->apple_dart_cfg.ttbr[i] = virt_to_phys(data->pgd[i]);
426745ef109SJanne Grunau 	}
427745ef109SJanne Grunau 
428745ef109SJanne Grunau 	return &data->iop;
429745ef109SJanne Grunau 
430745ef109SJanne Grunau out_free_data:
431745ef109SJanne Grunau 	while (--i >= 0)
432745ef109SJanne Grunau 		free_pages((unsigned long)data->pgd[i],
433745ef109SJanne Grunau 			   get_order(DART_GRANULE(data)));
434745ef109SJanne Grunau 	kfree(data);
435745ef109SJanne Grunau 	return NULL;
436745ef109SJanne Grunau }
437745ef109SJanne Grunau 
apple_dart_free_pgtable(struct io_pgtable * iop)438745ef109SJanne Grunau static void apple_dart_free_pgtable(struct io_pgtable *iop)
439745ef109SJanne Grunau {
440745ef109SJanne Grunau 	struct dart_io_pgtable *data = io_pgtable_to_data(iop);
441745ef109SJanne Grunau 	dart_iopte *ptep, *end;
442745ef109SJanne Grunau 	int i;
443745ef109SJanne Grunau 
444745ef109SJanne Grunau 	for (i = 0; i < (1 << data->tbl_bits) && data->pgd[i]; ++i) {
445745ef109SJanne Grunau 		ptep = data->pgd[i];
446745ef109SJanne Grunau 		end = (void *)ptep + DART_GRANULE(data);
447745ef109SJanne Grunau 
448745ef109SJanne Grunau 		while (ptep != end) {
449745ef109SJanne Grunau 			dart_iopte pte = *ptep++;
450745ef109SJanne Grunau 
451745ef109SJanne Grunau 			if (pte) {
452745ef109SJanne Grunau 				unsigned long page =
453745ef109SJanne Grunau 					(unsigned long)iopte_deref(pte, data);
454745ef109SJanne Grunau 
455745ef109SJanne Grunau 				free_pages(page, get_order(DART_GRANULE(data)));
456745ef109SJanne Grunau 			}
457745ef109SJanne Grunau 		}
458745ef109SJanne Grunau 		free_pages((unsigned long)data->pgd[i],
459745ef109SJanne Grunau 			   get_order(DART_GRANULE(data)));
460745ef109SJanne Grunau 	}
461745ef109SJanne Grunau 
462745ef109SJanne Grunau 	kfree(data);
463745ef109SJanne Grunau }
464745ef109SJanne Grunau 
465745ef109SJanne Grunau struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns = {
466745ef109SJanne Grunau 	.alloc	= apple_dart_alloc_pgtable,
467745ef109SJanne Grunau 	.free	= apple_dart_free_pgtable,
468745ef109SJanne Grunau };
469