xref: /linux/drivers/iommu/sun50i-iommu.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2016-2018, Allwinner Technology CO., LTD.
3 // Copyright (C) 2019-2020, Cerno
4 
5 #include <linux/bitfield.h>
6 #include <linux/bug.h>
7 #include <linux/clk.h>
8 #include <linux/device.h>
9 #include <linux/dma-direction.h>
10 #include <linux/dma-iommu.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
15 #include <linux/iommu.h>
16 #include <linux/iopoll.h>
17 #include <linux/ioport.h>
18 #include <linux/log2.h>
19 #include <linux/module.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/reset.h>
25 #include <linux/sizes.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 
30 #define IOMMU_RESET_REG			0x010
31 #define IOMMU_ENABLE_REG		0x020
32 #define IOMMU_ENABLE_ENABLE			BIT(0)
33 
34 #define IOMMU_BYPASS_REG		0x030
35 #define IOMMU_AUTO_GATING_REG		0x040
36 #define IOMMU_AUTO_GATING_ENABLE		BIT(0)
37 
38 #define IOMMU_WBUF_CTRL_REG		0x044
39 #define IOMMU_OOO_CTRL_REG		0x048
40 #define IOMMU_4KB_BDY_PRT_CTRL_REG	0x04c
41 #define IOMMU_TTB_REG			0x050
42 #define IOMMU_TLB_ENABLE_REG		0x060
43 #define IOMMU_TLB_PREFETCH_REG		0x070
44 #define IOMMU_TLB_PREFETCH_MASTER_ENABLE(m)	BIT(m)
45 
46 #define IOMMU_TLB_FLUSH_REG		0x080
47 #define IOMMU_TLB_FLUSH_PTW_CACHE		BIT(17)
48 #define IOMMU_TLB_FLUSH_MACRO_TLB		BIT(16)
49 #define IOMMU_TLB_FLUSH_MICRO_TLB(i)		(BIT(i) & GENMASK(5, 0))
50 
51 #define IOMMU_TLB_IVLD_ADDR_REG		0x090
52 #define IOMMU_TLB_IVLD_ADDR_MASK_REG	0x094
53 #define IOMMU_TLB_IVLD_ENABLE_REG	0x098
54 #define IOMMU_TLB_IVLD_ENABLE_ENABLE		BIT(0)
55 
56 #define IOMMU_PC_IVLD_ADDR_REG		0x0a0
57 #define IOMMU_PC_IVLD_ENABLE_REG	0x0a8
58 #define IOMMU_PC_IVLD_ENABLE_ENABLE		BIT(0)
59 
60 #define IOMMU_DM_AUT_CTRL_REG(d)	(0x0b0 + ((d) / 2) * 4)
61 #define IOMMU_DM_AUT_CTRL_RD_UNAVAIL(d, m)	(1 << (((d & 1) * 16) + ((m) * 2)))
62 #define IOMMU_DM_AUT_CTRL_WR_UNAVAIL(d, m)	(1 << (((d & 1) * 16) + ((m) * 2) + 1))
63 
64 #define IOMMU_DM_AUT_OVWT_REG		0x0d0
65 #define IOMMU_INT_ENABLE_REG		0x100
66 #define IOMMU_INT_CLR_REG		0x104
67 #define IOMMU_INT_STA_REG		0x108
68 #define IOMMU_INT_ERR_ADDR_REG(i)	(0x110 + (i) * 4)
69 #define IOMMU_INT_ERR_ADDR_L1_REG	0x130
70 #define IOMMU_INT_ERR_ADDR_L2_REG	0x134
71 #define IOMMU_INT_ERR_DATA_REG(i)	(0x150 + (i) * 4)
72 #define IOMMU_L1PG_INT_REG		0x0180
73 #define IOMMU_L2PG_INT_REG		0x0184
74 
75 #define IOMMU_INT_INVALID_L2PG			BIT(17)
76 #define IOMMU_INT_INVALID_L1PG			BIT(16)
77 #define IOMMU_INT_MASTER_PERMISSION(m)		BIT(m)
78 #define IOMMU_INT_MASTER_MASK			(IOMMU_INT_MASTER_PERMISSION(0) | \
79 						 IOMMU_INT_MASTER_PERMISSION(1) | \
80 						 IOMMU_INT_MASTER_PERMISSION(2) | \
81 						 IOMMU_INT_MASTER_PERMISSION(3) | \
82 						 IOMMU_INT_MASTER_PERMISSION(4) | \
83 						 IOMMU_INT_MASTER_PERMISSION(5))
84 #define IOMMU_INT_MASK				(IOMMU_INT_INVALID_L1PG | \
85 						 IOMMU_INT_INVALID_L2PG | \
86 						 IOMMU_INT_MASTER_MASK)
87 
88 #define PT_ENTRY_SIZE			sizeof(u32)
89 
90 #define NUM_DT_ENTRIES			4096
91 #define DT_SIZE				(NUM_DT_ENTRIES * PT_ENTRY_SIZE)
92 
93 #define NUM_PT_ENTRIES			256
94 #define PT_SIZE				(NUM_PT_ENTRIES * PT_ENTRY_SIZE)
95 
96 struct sun50i_iommu {
97 	struct iommu_device iommu;
98 
99 	/* Lock to modify the IOMMU registers */
100 	spinlock_t iommu_lock;
101 
102 	struct device *dev;
103 	void __iomem *base;
104 	struct reset_control *reset;
105 	struct clk *clk;
106 
107 	struct iommu_domain *domain;
108 	struct iommu_group *group;
109 	struct kmem_cache *pt_pool;
110 };
111 
112 struct sun50i_iommu_domain {
113 	struct iommu_domain domain;
114 
115 	/* Number of devices attached to the domain */
116 	refcount_t refcnt;
117 
118 	/* L1 Page Table */
119 	u32 *dt;
120 	dma_addr_t dt_dma;
121 
122 	struct sun50i_iommu *iommu;
123 };
124 
125 static struct sun50i_iommu_domain *to_sun50i_domain(struct iommu_domain *domain)
126 {
127 	return container_of(domain, struct sun50i_iommu_domain, domain);
128 }
129 
130 static struct sun50i_iommu *sun50i_iommu_from_dev(struct device *dev)
131 {
132 	return dev_iommu_priv_get(dev);
133 }
134 
135 static u32 iommu_read(struct sun50i_iommu *iommu, u32 offset)
136 {
137 	return readl(iommu->base + offset);
138 }
139 
140 static void iommu_write(struct sun50i_iommu *iommu, u32 offset, u32 value)
141 {
142 	writel(value, iommu->base + offset);
143 }
144 
145 /*
146  * The Allwinner H6 IOMMU uses a 2-level page table.
147  *
148  * The first level is the usual Directory Table (DT), that consists of
149  * 4096 4-bytes Directory Table Entries (DTE), each pointing to a Page
150  * Table (PT).
151  *
152  * Each PT consits of 256 4-bytes Page Table Entries (PTE), each
153  * pointing to a 4kB page of physical memory.
154  *
155  * The IOMMU supports a single DT, pointed by the IOMMU_TTB_REG
156  * register that contains its physical address.
157  */
158 
159 #define SUN50I_IOVA_DTE_MASK	GENMASK(31, 20)
160 #define SUN50I_IOVA_PTE_MASK	GENMASK(19, 12)
161 #define SUN50I_IOVA_PAGE_MASK	GENMASK(11, 0)
162 
163 static u32 sun50i_iova_get_dte_index(dma_addr_t iova)
164 {
165 	return FIELD_GET(SUN50I_IOVA_DTE_MASK, iova);
166 }
167 
168 static u32 sun50i_iova_get_pte_index(dma_addr_t iova)
169 {
170 	return FIELD_GET(SUN50I_IOVA_PTE_MASK, iova);
171 }
172 
173 static u32 sun50i_iova_get_page_offset(dma_addr_t iova)
174 {
175 	return FIELD_GET(SUN50I_IOVA_PAGE_MASK, iova);
176 }
177 
178 /*
179  * Each Directory Table Entry has a Page Table address and a valid
180  * bit:
181 
182  * +---------------------+-----------+-+
183  * | PT address          | Reserved  |V|
184  * +---------------------+-----------+-+
185  *  31:10 - Page Table address
186  *   9:2  - Reserved
187  *   1:0  - 1 if the entry is valid
188  */
189 
190 #define SUN50I_DTE_PT_ADDRESS_MASK	GENMASK(31, 10)
191 #define SUN50I_DTE_PT_ATTRS		GENMASK(1, 0)
192 #define SUN50I_DTE_PT_VALID		1
193 
194 static phys_addr_t sun50i_dte_get_pt_address(u32 dte)
195 {
196 	return (phys_addr_t)dte & SUN50I_DTE_PT_ADDRESS_MASK;
197 }
198 
199 static bool sun50i_dte_is_pt_valid(u32 dte)
200 {
201 	return (dte & SUN50I_DTE_PT_ATTRS) == SUN50I_DTE_PT_VALID;
202 }
203 
204 static u32 sun50i_mk_dte(dma_addr_t pt_dma)
205 {
206 	return (pt_dma & SUN50I_DTE_PT_ADDRESS_MASK) | SUN50I_DTE_PT_VALID;
207 }
208 
209 /*
210  * Each PTE has a Page address, an authority index and a valid bit:
211  *
212  * +----------------+-----+-----+-----+---+-----+
213  * | Page address   | Rsv | ACI | Rsv | V | Rsv |
214  * +----------------+-----+-----+-----+---+-----+
215  *  31:12 - Page address
216  *  11:8  - Reserved
217  *   7:4  - Authority Control Index
218  *   3:2  - Reserved
219  *     1  - 1 if the entry is valid
220  *     0  - Reserved
221  *
222  * The way permissions work is that the IOMMU has 16 "domains" that
223  * can be configured to give each masters either read or write
224  * permissions through the IOMMU_DM_AUT_CTRL_REG registers. The domain
225  * 0 seems like the default domain, and its permissions in the
226  * IOMMU_DM_AUT_CTRL_REG are only read-only, so it's not really
227  * useful to enforce any particular permission.
228  *
229  * Each page entry will then have a reference to the domain they are
230  * affected to, so that we can actually enforce them on a per-page
231  * basis.
232  *
233  * In order to make it work with the IOMMU framework, we will be using
234  * 4 different domains, starting at 1: RD_WR, RD, WR and NONE
235  * depending on the permission we want to enforce. Each domain will
236  * have each master setup in the same way, since the IOMMU framework
237  * doesn't seem to restrict page access on a per-device basis. And
238  * then we will use the relevant domain index when generating the page
239  * table entry depending on the permissions we want to be enforced.
240  */
241 
242 enum sun50i_iommu_aci {
243 	SUN50I_IOMMU_ACI_DO_NOT_USE = 0,
244 	SUN50I_IOMMU_ACI_NONE,
245 	SUN50I_IOMMU_ACI_RD,
246 	SUN50I_IOMMU_ACI_WR,
247 	SUN50I_IOMMU_ACI_RD_WR,
248 };
249 
250 #define SUN50I_PTE_PAGE_ADDRESS_MASK	GENMASK(31, 12)
251 #define SUN50I_PTE_ACI_MASK		GENMASK(7, 4)
252 #define SUN50I_PTE_PAGE_VALID		BIT(1)
253 
254 static phys_addr_t sun50i_pte_get_page_address(u32 pte)
255 {
256 	return (phys_addr_t)pte & SUN50I_PTE_PAGE_ADDRESS_MASK;
257 }
258 
259 static enum sun50i_iommu_aci sun50i_get_pte_aci(u32 pte)
260 {
261 	return FIELD_GET(SUN50I_PTE_ACI_MASK, pte);
262 }
263 
264 static bool sun50i_pte_is_page_valid(u32 pte)
265 {
266 	return pte & SUN50I_PTE_PAGE_VALID;
267 }
268 
269 static u32 sun50i_mk_pte(phys_addr_t page, int prot)
270 {
271 	enum sun50i_iommu_aci aci;
272 	u32 flags = 0;
273 
274 	if (prot & (IOMMU_READ | IOMMU_WRITE))
275 		aci = SUN50I_IOMMU_ACI_RD_WR;
276 	else if (prot & IOMMU_READ)
277 		aci = SUN50I_IOMMU_ACI_RD;
278 	else if (prot & IOMMU_WRITE)
279 		aci = SUN50I_IOMMU_ACI_WR;
280 	else
281 		aci = SUN50I_IOMMU_ACI_NONE;
282 
283 	flags |= FIELD_PREP(SUN50I_PTE_ACI_MASK, aci);
284 	page &= SUN50I_PTE_PAGE_ADDRESS_MASK;
285 	return page | flags | SUN50I_PTE_PAGE_VALID;
286 }
287 
288 static void sun50i_table_flush(struct sun50i_iommu_domain *sun50i_domain,
289 			       void *vaddr, unsigned int count)
290 {
291 	struct sun50i_iommu *iommu = sun50i_domain->iommu;
292 	dma_addr_t dma = virt_to_phys(vaddr);
293 	size_t size = count * PT_ENTRY_SIZE;
294 
295 	dma_sync_single_for_device(iommu->dev, dma, size, DMA_TO_DEVICE);
296 }
297 
298 static int sun50i_iommu_flush_all_tlb(struct sun50i_iommu *iommu)
299 {
300 	u32 reg;
301 	int ret;
302 
303 	assert_spin_locked(&iommu->iommu_lock);
304 
305 	iommu_write(iommu,
306 		    IOMMU_TLB_FLUSH_REG,
307 		    IOMMU_TLB_FLUSH_PTW_CACHE |
308 		    IOMMU_TLB_FLUSH_MACRO_TLB |
309 		    IOMMU_TLB_FLUSH_MICRO_TLB(5) |
310 		    IOMMU_TLB_FLUSH_MICRO_TLB(4) |
311 		    IOMMU_TLB_FLUSH_MICRO_TLB(3) |
312 		    IOMMU_TLB_FLUSH_MICRO_TLB(2) |
313 		    IOMMU_TLB_FLUSH_MICRO_TLB(1) |
314 		    IOMMU_TLB_FLUSH_MICRO_TLB(0));
315 
316 	ret = readl_poll_timeout(iommu->base + IOMMU_TLB_FLUSH_REG,
317 				 reg, !reg,
318 				 1, 2000);
319 	if (ret)
320 		dev_warn(iommu->dev, "TLB Flush timed out!\n");
321 
322 	return ret;
323 }
324 
325 static void sun50i_iommu_flush_iotlb_all(struct iommu_domain *domain)
326 {
327 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
328 	struct sun50i_iommu *iommu = sun50i_domain->iommu;
329 	unsigned long flags;
330 
331 	/*
332 	 * At boot, we'll have a first call into .flush_iotlb_all right after
333 	 * .probe_device, and since we link our (single) domain to our iommu in
334 	 * the .attach_device callback, we don't have that pointer set.
335 	 *
336 	 * It shouldn't really be any trouble to ignore it though since we flush
337 	 * all caches as part of the device powerup.
338 	 */
339 	if (!iommu)
340 		return;
341 
342 	spin_lock_irqsave(&iommu->iommu_lock, flags);
343 	sun50i_iommu_flush_all_tlb(iommu);
344 	spin_unlock_irqrestore(&iommu->iommu_lock, flags);
345 }
346 
347 static void sun50i_iommu_iotlb_sync(struct iommu_domain *domain,
348 				    struct iommu_iotlb_gather *gather)
349 {
350 	sun50i_iommu_flush_iotlb_all(domain);
351 }
352 
353 static int sun50i_iommu_enable(struct sun50i_iommu *iommu)
354 {
355 	struct sun50i_iommu_domain *sun50i_domain;
356 	unsigned long flags;
357 	int ret;
358 
359 	if (!iommu->domain)
360 		return 0;
361 
362 	sun50i_domain = to_sun50i_domain(iommu->domain);
363 
364 	ret = reset_control_deassert(iommu->reset);
365 	if (ret)
366 		return ret;
367 
368 	ret = clk_prepare_enable(iommu->clk);
369 	if (ret)
370 		goto err_reset_assert;
371 
372 	spin_lock_irqsave(&iommu->iommu_lock, flags);
373 
374 	iommu_write(iommu, IOMMU_TTB_REG, sun50i_domain->dt_dma);
375 	iommu_write(iommu, IOMMU_TLB_PREFETCH_REG,
376 		    IOMMU_TLB_PREFETCH_MASTER_ENABLE(0) |
377 		    IOMMU_TLB_PREFETCH_MASTER_ENABLE(1) |
378 		    IOMMU_TLB_PREFETCH_MASTER_ENABLE(2) |
379 		    IOMMU_TLB_PREFETCH_MASTER_ENABLE(3) |
380 		    IOMMU_TLB_PREFETCH_MASTER_ENABLE(4) |
381 		    IOMMU_TLB_PREFETCH_MASTER_ENABLE(5));
382 	iommu_write(iommu, IOMMU_INT_ENABLE_REG, IOMMU_INT_MASK);
383 	iommu_write(iommu, IOMMU_DM_AUT_CTRL_REG(SUN50I_IOMMU_ACI_NONE),
384 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 0) |
385 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 0) |
386 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 1) |
387 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 1) |
388 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 2) |
389 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 2) |
390 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 3) |
391 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 3) |
392 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 4) |
393 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 4) |
394 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 5) |
395 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_NONE, 5));
396 
397 	iommu_write(iommu, IOMMU_DM_AUT_CTRL_REG(SUN50I_IOMMU_ACI_RD),
398 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_RD, 0) |
399 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_RD, 1) |
400 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_RD, 2) |
401 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_RD, 3) |
402 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_RD, 4) |
403 		    IOMMU_DM_AUT_CTRL_WR_UNAVAIL(SUN50I_IOMMU_ACI_RD, 5));
404 
405 	iommu_write(iommu, IOMMU_DM_AUT_CTRL_REG(SUN50I_IOMMU_ACI_WR),
406 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_WR, 0) |
407 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_WR, 1) |
408 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_WR, 2) |
409 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_WR, 3) |
410 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_WR, 4) |
411 		    IOMMU_DM_AUT_CTRL_RD_UNAVAIL(SUN50I_IOMMU_ACI_WR, 5));
412 
413 	ret = sun50i_iommu_flush_all_tlb(iommu);
414 	if (ret) {
415 		spin_unlock_irqrestore(&iommu->iommu_lock, flags);
416 		goto err_clk_disable;
417 	}
418 
419 	iommu_write(iommu, IOMMU_AUTO_GATING_REG, IOMMU_AUTO_GATING_ENABLE);
420 	iommu_write(iommu, IOMMU_ENABLE_REG, IOMMU_ENABLE_ENABLE);
421 
422 	spin_unlock_irqrestore(&iommu->iommu_lock, flags);
423 
424 	return 0;
425 
426 err_clk_disable:
427 	clk_disable_unprepare(iommu->clk);
428 
429 err_reset_assert:
430 	reset_control_assert(iommu->reset);
431 
432 	return ret;
433 }
434 
435 static void sun50i_iommu_disable(struct sun50i_iommu *iommu)
436 {
437 	unsigned long flags;
438 
439 	spin_lock_irqsave(&iommu->iommu_lock, flags);
440 
441 	iommu_write(iommu, IOMMU_ENABLE_REG, 0);
442 	iommu_write(iommu, IOMMU_TTB_REG, 0);
443 
444 	spin_unlock_irqrestore(&iommu->iommu_lock, flags);
445 
446 	clk_disable_unprepare(iommu->clk);
447 	reset_control_assert(iommu->reset);
448 }
449 
450 static void *sun50i_iommu_alloc_page_table(struct sun50i_iommu *iommu,
451 					   gfp_t gfp)
452 {
453 	dma_addr_t pt_dma;
454 	u32 *page_table;
455 
456 	page_table = kmem_cache_zalloc(iommu->pt_pool, gfp);
457 	if (!page_table)
458 		return ERR_PTR(-ENOMEM);
459 
460 	pt_dma = dma_map_single(iommu->dev, page_table, PT_SIZE, DMA_TO_DEVICE);
461 	if (dma_mapping_error(iommu->dev, pt_dma)) {
462 		dev_err(iommu->dev, "Couldn't map L2 Page Table\n");
463 		kmem_cache_free(iommu->pt_pool, page_table);
464 		return ERR_PTR(-ENOMEM);
465 	}
466 
467 	/* We rely on the physical address and DMA address being the same */
468 	WARN_ON(pt_dma != virt_to_phys(page_table));
469 
470 	return page_table;
471 }
472 
473 static void sun50i_iommu_free_page_table(struct sun50i_iommu *iommu,
474 					 u32 *page_table)
475 {
476 	phys_addr_t pt_phys = virt_to_phys(page_table);
477 
478 	dma_unmap_single(iommu->dev, pt_phys, PT_SIZE, DMA_TO_DEVICE);
479 	kmem_cache_free(iommu->pt_pool, page_table);
480 }
481 
482 static u32 *sun50i_dte_get_page_table(struct sun50i_iommu_domain *sun50i_domain,
483 				      dma_addr_t iova, gfp_t gfp)
484 {
485 	struct sun50i_iommu *iommu = sun50i_domain->iommu;
486 	u32 *page_table;
487 	u32 *dte_addr;
488 	u32 old_dte;
489 	u32 dte;
490 
491 	dte_addr = &sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
492 	dte = *dte_addr;
493 	if (sun50i_dte_is_pt_valid(dte)) {
494 		phys_addr_t pt_phys = sun50i_dte_get_pt_address(dte);
495 		return (u32 *)phys_to_virt(pt_phys);
496 	}
497 
498 	page_table = sun50i_iommu_alloc_page_table(iommu, gfp);
499 	if (IS_ERR(page_table))
500 		return page_table;
501 
502 	dte = sun50i_mk_dte(virt_to_phys(page_table));
503 	old_dte = cmpxchg(dte_addr, 0, dte);
504 	if (old_dte) {
505 		phys_addr_t installed_pt_phys =
506 			sun50i_dte_get_pt_address(old_dte);
507 		u32 *installed_pt = phys_to_virt(installed_pt_phys);
508 		u32 *drop_pt = page_table;
509 
510 		page_table = installed_pt;
511 		dte = old_dte;
512 		sun50i_iommu_free_page_table(iommu, drop_pt);
513 	}
514 
515 	sun50i_table_flush(sun50i_domain, page_table, PT_SIZE);
516 	sun50i_table_flush(sun50i_domain, dte_addr, 1);
517 
518 	return page_table;
519 }
520 
521 static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova,
522 			    phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
523 {
524 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
525 	struct sun50i_iommu *iommu = sun50i_domain->iommu;
526 	u32 pte_index;
527 	u32 *page_table, *pte_addr;
528 	int ret = 0;
529 
530 	page_table = sun50i_dte_get_page_table(sun50i_domain, iova, gfp);
531 	if (IS_ERR(page_table)) {
532 		ret = PTR_ERR(page_table);
533 		goto out;
534 	}
535 
536 	pte_index = sun50i_iova_get_pte_index(iova);
537 	pte_addr = &page_table[pte_index];
538 	if (unlikely(sun50i_pte_is_page_valid(*pte_addr))) {
539 		phys_addr_t page_phys = sun50i_pte_get_page_address(*pte_addr);
540 		dev_err(iommu->dev,
541 			"iova %pad already mapped to %pa cannot remap to %pa prot: %#x\n",
542 			&iova, &page_phys, &paddr, prot);
543 		ret = -EBUSY;
544 		goto out;
545 	}
546 
547 	*pte_addr = sun50i_mk_pte(paddr, prot);
548 	sun50i_table_flush(sun50i_domain, pte_addr, 1);
549 
550 out:
551 	return ret;
552 }
553 
554 static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
555 				 size_t size, struct iommu_iotlb_gather *gather)
556 {
557 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
558 	phys_addr_t pt_phys;
559 	dma_addr_t pte_dma;
560 	u32 *pte_addr;
561 	u32 dte;
562 
563 	dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
564 	if (!sun50i_dte_is_pt_valid(dte))
565 		return 0;
566 
567 	pt_phys = sun50i_dte_get_pt_address(dte);
568 	pte_addr = (u32 *)phys_to_virt(pt_phys) + sun50i_iova_get_pte_index(iova);
569 	pte_dma = pt_phys + sun50i_iova_get_pte_index(iova) * PT_ENTRY_SIZE;
570 
571 	if (!sun50i_pte_is_page_valid(*pte_addr))
572 		return 0;
573 
574 	memset(pte_addr, 0, sizeof(*pte_addr));
575 	sun50i_table_flush(sun50i_domain, pte_addr, 1);
576 
577 	return SZ_4K;
578 }
579 
580 static phys_addr_t sun50i_iommu_iova_to_phys(struct iommu_domain *domain,
581 					     dma_addr_t iova)
582 {
583 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
584 	phys_addr_t pt_phys;
585 	u32 *page_table;
586 	u32 dte, pte;
587 
588 	dte = sun50i_domain->dt[sun50i_iova_get_dte_index(iova)];
589 	if (!sun50i_dte_is_pt_valid(dte))
590 		return 0;
591 
592 	pt_phys = sun50i_dte_get_pt_address(dte);
593 	page_table = (u32 *)phys_to_virt(pt_phys);
594 	pte = page_table[sun50i_iova_get_pte_index(iova)];
595 	if (!sun50i_pte_is_page_valid(pte))
596 		return 0;
597 
598 	return sun50i_pte_get_page_address(pte) +
599 		sun50i_iova_get_page_offset(iova);
600 }
601 
602 static struct iommu_domain *sun50i_iommu_domain_alloc(unsigned type)
603 {
604 	struct sun50i_iommu_domain *sun50i_domain;
605 
606 	if (type != IOMMU_DOMAIN_DMA &&
607 	    type != IOMMU_DOMAIN_IDENTITY &&
608 	    type != IOMMU_DOMAIN_UNMANAGED)
609 		return NULL;
610 
611 	sun50i_domain = kzalloc(sizeof(*sun50i_domain), GFP_KERNEL);
612 	if (!sun50i_domain)
613 		return NULL;
614 
615 	if (type == IOMMU_DOMAIN_DMA &&
616 	    iommu_get_dma_cookie(&sun50i_domain->domain))
617 		goto err_free_domain;
618 
619 	sun50i_domain->dt = (u32 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
620 						    get_order(DT_SIZE));
621 	if (!sun50i_domain->dt)
622 		goto err_put_cookie;
623 
624 	refcount_set(&sun50i_domain->refcnt, 1);
625 
626 	sun50i_domain->domain.geometry.aperture_start = 0;
627 	sun50i_domain->domain.geometry.aperture_end = DMA_BIT_MASK(32);
628 	sun50i_domain->domain.geometry.force_aperture = true;
629 
630 	return &sun50i_domain->domain;
631 
632 err_put_cookie:
633 	if (type == IOMMU_DOMAIN_DMA)
634 		iommu_put_dma_cookie(&sun50i_domain->domain);
635 
636 err_free_domain:
637 	kfree(sun50i_domain);
638 
639 	return NULL;
640 }
641 
642 static void sun50i_iommu_domain_free(struct iommu_domain *domain)
643 {
644 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
645 
646 	free_pages((unsigned long)sun50i_domain->dt, get_order(DT_SIZE));
647 	sun50i_domain->dt = NULL;
648 
649 	iommu_put_dma_cookie(domain);
650 
651 	kfree(sun50i_domain);
652 }
653 
654 static int sun50i_iommu_attach_domain(struct sun50i_iommu *iommu,
655 				      struct sun50i_iommu_domain *sun50i_domain)
656 {
657 	iommu->domain = &sun50i_domain->domain;
658 	sun50i_domain->iommu = iommu;
659 
660 	sun50i_domain->dt_dma = dma_map_single(iommu->dev, sun50i_domain->dt,
661 					       DT_SIZE, DMA_TO_DEVICE);
662 	if (dma_mapping_error(iommu->dev, sun50i_domain->dt_dma)) {
663 		dev_err(iommu->dev, "Couldn't map L1 Page Table\n");
664 		return -ENOMEM;
665 	}
666 
667 	return sun50i_iommu_enable(iommu);
668 }
669 
670 static void sun50i_iommu_detach_domain(struct sun50i_iommu *iommu,
671 				       struct sun50i_iommu_domain *sun50i_domain)
672 {
673 	unsigned int i;
674 
675 	for (i = 0; i < NUM_DT_ENTRIES; i++) {
676 		phys_addr_t pt_phys;
677 		u32 *page_table;
678 		u32 *dte_addr;
679 		u32 dte;
680 
681 		dte_addr = &sun50i_domain->dt[i];
682 		dte = *dte_addr;
683 		if (!sun50i_dte_is_pt_valid(dte))
684 			continue;
685 
686 		memset(dte_addr, 0, sizeof(*dte_addr));
687 		sun50i_table_flush(sun50i_domain, dte_addr, 1);
688 
689 		pt_phys = sun50i_dte_get_pt_address(dte);
690 		page_table = phys_to_virt(pt_phys);
691 		sun50i_iommu_free_page_table(iommu, page_table);
692 	}
693 
694 
695 	sun50i_iommu_disable(iommu);
696 
697 	dma_unmap_single(iommu->dev, virt_to_phys(sun50i_domain->dt),
698 			 DT_SIZE, DMA_TO_DEVICE);
699 
700 	iommu->domain = NULL;
701 }
702 
703 static void sun50i_iommu_detach_device(struct iommu_domain *domain,
704 				       struct device *dev)
705 {
706 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
707 	struct sun50i_iommu *iommu = dev_iommu_priv_get(dev);
708 
709 	dev_dbg(dev, "Detaching from IOMMU domain\n");
710 
711 	if (iommu->domain != domain)
712 		return;
713 
714 	if (refcount_dec_and_test(&sun50i_domain->refcnt))
715 		sun50i_iommu_detach_domain(iommu, sun50i_domain);
716 }
717 
718 static int sun50i_iommu_attach_device(struct iommu_domain *domain,
719 				      struct device *dev)
720 {
721 	struct sun50i_iommu_domain *sun50i_domain = to_sun50i_domain(domain);
722 	struct sun50i_iommu *iommu;
723 
724 	iommu = sun50i_iommu_from_dev(dev);
725 	if (!iommu)
726 		return -ENODEV;
727 
728 	dev_dbg(dev, "Attaching to IOMMU domain\n");
729 
730 	refcount_inc(&sun50i_domain->refcnt);
731 
732 	if (iommu->domain == domain)
733 		return 0;
734 
735 	if (iommu->domain)
736 		sun50i_iommu_detach_device(iommu->domain, dev);
737 
738 	sun50i_iommu_attach_domain(iommu, sun50i_domain);
739 
740 	return 0;
741 }
742 
743 static struct iommu_device *sun50i_iommu_probe_device(struct device *dev)
744 {
745 	struct sun50i_iommu *iommu;
746 
747 	iommu = sun50i_iommu_from_dev(dev);
748 	if (!iommu)
749 		return ERR_PTR(-ENODEV);
750 
751 	return &iommu->iommu;
752 }
753 
754 static void sun50i_iommu_release_device(struct device *dev) {}
755 
756 static struct iommu_group *sun50i_iommu_device_group(struct device *dev)
757 {
758 	struct sun50i_iommu *iommu = sun50i_iommu_from_dev(dev);
759 
760 	return iommu_group_ref_get(iommu->group);
761 }
762 
763 static int sun50i_iommu_of_xlate(struct device *dev,
764 				 struct of_phandle_args *args)
765 {
766 	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
767 	unsigned id = args->args[0];
768 
769 	dev_iommu_priv_set(dev, platform_get_drvdata(iommu_pdev));
770 
771 	return iommu_fwspec_add_ids(dev, &id, 1);
772 }
773 
774 static const struct iommu_ops sun50i_iommu_ops = {
775 	.pgsize_bitmap	= SZ_4K,
776 	.attach_dev	= sun50i_iommu_attach_device,
777 	.detach_dev	= sun50i_iommu_detach_device,
778 	.device_group	= sun50i_iommu_device_group,
779 	.domain_alloc	= sun50i_iommu_domain_alloc,
780 	.domain_free	= sun50i_iommu_domain_free,
781 	.flush_iotlb_all = sun50i_iommu_flush_iotlb_all,
782 	.iotlb_sync	= sun50i_iommu_iotlb_sync,
783 	.iova_to_phys	= sun50i_iommu_iova_to_phys,
784 	.map		= sun50i_iommu_map,
785 	.of_xlate	= sun50i_iommu_of_xlate,
786 	.probe_device	= sun50i_iommu_probe_device,
787 	.release_device	= sun50i_iommu_release_device,
788 	.unmap		= sun50i_iommu_unmap,
789 };
790 
791 static void sun50i_iommu_report_fault(struct sun50i_iommu *iommu,
792 				      unsigned master, phys_addr_t iova,
793 				      unsigned prot)
794 {
795 	dev_err(iommu->dev, "Page fault for %pad (master %d, dir %s)\n",
796 		&iova, master, (prot == IOMMU_FAULT_WRITE) ? "wr" : "rd");
797 
798 	if (iommu->domain)
799 		report_iommu_fault(iommu->domain, iommu->dev, iova, prot);
800 	else
801 		dev_err(iommu->dev, "Page fault while iommu not attached to any domain?\n");
802 }
803 
804 static phys_addr_t sun50i_iommu_handle_pt_irq(struct sun50i_iommu *iommu,
805 					      unsigned addr_reg,
806 					      unsigned blame_reg)
807 {
808 	phys_addr_t iova;
809 	unsigned master;
810 	u32 blame;
811 
812 	assert_spin_locked(&iommu->iommu_lock);
813 
814 	iova = iommu_read(iommu, addr_reg);
815 	blame = iommu_read(iommu, blame_reg);
816 	master = ilog2(blame & IOMMU_INT_MASTER_MASK);
817 
818 	/*
819 	 * If the address is not in the page table, we can't get what
820 	 * operation triggered the fault. Assume it's a read
821 	 * operation.
822 	 */
823 	sun50i_iommu_report_fault(iommu, master, iova, IOMMU_FAULT_READ);
824 
825 	return iova;
826 }
827 
828 static phys_addr_t sun50i_iommu_handle_perm_irq(struct sun50i_iommu *iommu)
829 {
830 	enum sun50i_iommu_aci aci;
831 	phys_addr_t iova;
832 	unsigned master;
833 	unsigned dir;
834 	u32 blame;
835 
836 	assert_spin_locked(&iommu->iommu_lock);
837 
838 	blame = iommu_read(iommu, IOMMU_INT_STA_REG);
839 	master = ilog2(blame & IOMMU_INT_MASTER_MASK);
840 	iova = iommu_read(iommu, IOMMU_INT_ERR_ADDR_REG(master));
841 	aci = sun50i_get_pte_aci(iommu_read(iommu,
842 					    IOMMU_INT_ERR_DATA_REG(master)));
843 
844 	switch (aci) {
845 		/*
846 		 * If we are in the read-only domain, then it means we
847 		 * tried to write.
848 		 */
849 	case SUN50I_IOMMU_ACI_RD:
850 		dir = IOMMU_FAULT_WRITE;
851 		break;
852 
853 		/*
854 		 * If we are in the write-only domain, then it means
855 		 * we tried to read.
856 		 */
857 	case SUN50I_IOMMU_ACI_WR:
858 
859 		/*
860 		 * If we are in the domain without any permission, we
861 		 * can't really tell. Let's default to a read
862 		 * operation.
863 		 */
864 	case SUN50I_IOMMU_ACI_NONE:
865 
866 		/* WTF? */
867 	case SUN50I_IOMMU_ACI_RD_WR:
868 	default:
869 		dir = IOMMU_FAULT_READ;
870 		break;
871 	}
872 
873 	/*
874 	 * If the address is not in the page table, we can't get what
875 	 * operation triggered the fault. Assume it's a read
876 	 * operation.
877 	 */
878 	sun50i_iommu_report_fault(iommu, master, iova, dir);
879 
880 	return iova;
881 }
882 
883 static irqreturn_t sun50i_iommu_irq(int irq, void *dev_id)
884 {
885 	struct sun50i_iommu *iommu = dev_id;
886 	phys_addr_t iova;
887 	u32 status;
888 
889 	spin_lock(&iommu->iommu_lock);
890 
891 	status = iommu_read(iommu, IOMMU_INT_STA_REG);
892 	if (!(status & IOMMU_INT_MASK)) {
893 		spin_unlock(&iommu->iommu_lock);
894 		return IRQ_NONE;
895 	}
896 
897 	if (status & IOMMU_INT_INVALID_L2PG)
898 		iova = sun50i_iommu_handle_pt_irq(iommu,
899 						  IOMMU_INT_ERR_ADDR_L2_REG,
900 						  IOMMU_L2PG_INT_REG);
901 	else if (status & IOMMU_INT_INVALID_L1PG)
902 		iova = sun50i_iommu_handle_pt_irq(iommu,
903 						  IOMMU_INT_ERR_ADDR_L1_REG,
904 						  IOMMU_L1PG_INT_REG);
905 	else
906 		iova = sun50i_iommu_handle_perm_irq(iommu);
907 
908 	iommu_write(iommu, IOMMU_INT_CLR_REG, status);
909 
910 	iommu_write(iommu, IOMMU_RESET_REG, ~status);
911 	iommu_write(iommu, IOMMU_RESET_REG, status);
912 
913 	spin_unlock(&iommu->iommu_lock);
914 
915 	return IRQ_HANDLED;
916 }
917 
918 static int sun50i_iommu_probe(struct platform_device *pdev)
919 {
920 	struct sun50i_iommu *iommu;
921 	int ret, irq;
922 
923 	iommu = devm_kzalloc(&pdev->dev, sizeof(*iommu), GFP_KERNEL);
924 	if (!iommu)
925 		return -ENOMEM;
926 	spin_lock_init(&iommu->iommu_lock);
927 	platform_set_drvdata(pdev, iommu);
928 	iommu->dev = &pdev->dev;
929 
930 	iommu->pt_pool = kmem_cache_create(dev_name(&pdev->dev),
931 					   PT_SIZE, PT_SIZE,
932 					   SLAB_HWCACHE_ALIGN,
933 					   NULL);
934 	if (!iommu->pt_pool)
935 		return -ENOMEM;
936 
937 	iommu->group = iommu_group_alloc();
938 	if (IS_ERR(iommu->group)) {
939 		ret = PTR_ERR(iommu->group);
940 		goto err_free_cache;
941 	}
942 
943 	iommu->base = devm_platform_ioremap_resource(pdev, 0);
944 	if (IS_ERR(iommu->base)) {
945 		ret = PTR_ERR(iommu->base);
946 		goto err_free_group;
947 	}
948 
949 	irq = platform_get_irq(pdev, 0);
950 	if (irq < 0) {
951 		ret = irq;
952 		goto err_free_group;
953 	}
954 
955 	iommu->clk = devm_clk_get(&pdev->dev, NULL);
956 	if (IS_ERR(iommu->clk)) {
957 		dev_err(&pdev->dev, "Couldn't get our clock.\n");
958 		ret = PTR_ERR(iommu->clk);
959 		goto err_free_group;
960 	}
961 
962 	iommu->reset = devm_reset_control_get(&pdev->dev, NULL);
963 	if (IS_ERR(iommu->reset)) {
964 		dev_err(&pdev->dev, "Couldn't get our reset line.\n");
965 		ret = PTR_ERR(iommu->reset);
966 		goto err_free_group;
967 	}
968 
969 	ret = iommu_device_sysfs_add(&iommu->iommu, &pdev->dev,
970 				     NULL, dev_name(&pdev->dev));
971 	if (ret)
972 		goto err_free_group;
973 
974 	iommu_device_set_ops(&iommu->iommu, &sun50i_iommu_ops);
975 	iommu_device_set_fwnode(&iommu->iommu, &pdev->dev.of_node->fwnode);
976 
977 	ret = iommu_device_register(&iommu->iommu);
978 	if (ret)
979 		goto err_remove_sysfs;
980 
981 	ret = devm_request_irq(&pdev->dev, irq, sun50i_iommu_irq, 0,
982 			       dev_name(&pdev->dev), iommu);
983 	if (ret < 0)
984 		goto err_unregister;
985 
986 	bus_set_iommu(&platform_bus_type, &sun50i_iommu_ops);
987 
988 	return 0;
989 
990 err_unregister:
991 	iommu_device_unregister(&iommu->iommu);
992 
993 err_remove_sysfs:
994 	iommu_device_sysfs_remove(&iommu->iommu);
995 
996 err_free_group:
997 	iommu_group_put(iommu->group);
998 
999 err_free_cache:
1000 	kmem_cache_destroy(iommu->pt_pool);
1001 
1002 	return ret;
1003 }
1004 
1005 static const struct of_device_id sun50i_iommu_dt[] = {
1006 	{ .compatible = "allwinner,sun50i-h6-iommu", },
1007 	{ /* sentinel */ },
1008 };
1009 MODULE_DEVICE_TABLE(of, sun50i_iommu_dt);
1010 
1011 static struct platform_driver sun50i_iommu_driver = {
1012 	.driver		= {
1013 		.name			= "sun50i-iommu",
1014 		.of_match_table 	= sun50i_iommu_dt,
1015 		.suppress_bind_attrs	= true,
1016 	}
1017 };
1018 builtin_platform_driver_probe(sun50i_iommu_driver, sun50i_iommu_probe);
1019 
1020 MODULE_DESCRIPTION("Allwinner H6 IOMMU driver");
1021 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
1022 MODULE_AUTHOR("zhuxianbin <zhuxianbin@allwinnertech.com>");
1023 MODULE_LICENSE("Dual BSD/GPL");
1024