xref: /openbmc/linux/drivers/iommu/mtk_iommu_v1.c (revision a9bf2eec)
1b17336c5SHonghui Zhang /*
2d4cf5bbdSPaul Gortmaker  * IOMMU API for MTK architected m4u v1 implementations
3d4cf5bbdSPaul Gortmaker  *
4b17336c5SHonghui Zhang  * Copyright (c) 2015-2016 MediaTek Inc.
5b17336c5SHonghui Zhang  * Author: Honghui Zhang <honghui.zhang@mediatek.com>
6b17336c5SHonghui Zhang  *
7b17336c5SHonghui Zhang  * Based on driver/iommu/mtk_iommu.c
8b17336c5SHonghui Zhang  *
9b17336c5SHonghui Zhang  * This program is free software; you can redistribute it and/or modify
10b17336c5SHonghui Zhang  * it under the terms of the GNU General Public License version 2 as
11b17336c5SHonghui Zhang  * published by the Free Software Foundation.
12b17336c5SHonghui Zhang  *
13b17336c5SHonghui Zhang  * This program is distributed in the hope that it will be useful,
14b17336c5SHonghui Zhang  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15b17336c5SHonghui Zhang  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16b17336c5SHonghui Zhang  * GNU General Public License for more details.
17b17336c5SHonghui Zhang  */
1857c8a661SMike Rapoport #include <linux/memblock.h>
19b17336c5SHonghui Zhang #include <linux/bug.h>
20b17336c5SHonghui Zhang #include <linux/clk.h>
21b17336c5SHonghui Zhang #include <linux/component.h>
22b17336c5SHonghui Zhang #include <linux/device.h>
23745b6e74SArnd Bergmann #include <linux/dma-mapping.h>
24b17336c5SHonghui Zhang #include <linux/dma-iommu.h>
25b17336c5SHonghui Zhang #include <linux/err.h>
26b17336c5SHonghui Zhang #include <linux/interrupt.h>
27b17336c5SHonghui Zhang #include <linux/io.h>
28b17336c5SHonghui Zhang #include <linux/iommu.h>
29b17336c5SHonghui Zhang #include <linux/iopoll.h>
30b17336c5SHonghui Zhang #include <linux/list.h>
31b17336c5SHonghui Zhang #include <linux/of_address.h>
32b17336c5SHonghui Zhang #include <linux/of_iommu.h>
33b17336c5SHonghui Zhang #include <linux/of_irq.h>
34b17336c5SHonghui Zhang #include <linux/of_platform.h>
35b17336c5SHonghui Zhang #include <linux/platform_device.h>
36b17336c5SHonghui Zhang #include <linux/slab.h>
37b17336c5SHonghui Zhang #include <linux/spinlock.h>
38b17336c5SHonghui Zhang #include <asm/barrier.h>
39b17336c5SHonghui Zhang #include <asm/dma-iommu.h>
40d4cf5bbdSPaul Gortmaker #include <linux/init.h>
41b17336c5SHonghui Zhang #include <dt-bindings/memory/mt2701-larb-port.h>
42b17336c5SHonghui Zhang #include <soc/mediatek/smi.h>
43b17336c5SHonghui Zhang #include "mtk_iommu.h"
44b17336c5SHonghui Zhang 
45b17336c5SHonghui Zhang #define REG_MMU_PT_BASE_ADDR			0x000
46b17336c5SHonghui Zhang 
47b17336c5SHonghui Zhang #define F_ALL_INVLD				0x2
48b17336c5SHonghui Zhang #define F_MMU_INV_RANGE				0x1
49b17336c5SHonghui Zhang #define F_INVLD_EN0				BIT(0)
50b17336c5SHonghui Zhang #define F_INVLD_EN1				BIT(1)
51b17336c5SHonghui Zhang 
52b17336c5SHonghui Zhang #define F_MMU_FAULT_VA_MSK			0xfffff000
53b17336c5SHonghui Zhang #define MTK_PROTECT_PA_ALIGN			128
54b17336c5SHonghui Zhang 
55b17336c5SHonghui Zhang #define REG_MMU_CTRL_REG			0x210
56b17336c5SHonghui Zhang #define F_MMU_CTRL_COHERENT_EN			BIT(8)
57b17336c5SHonghui Zhang #define REG_MMU_IVRP_PADDR			0x214
58b17336c5SHonghui Zhang #define REG_MMU_INT_CONTROL			0x220
59b17336c5SHonghui Zhang #define F_INT_TRANSLATION_FAULT			BIT(0)
60b17336c5SHonghui Zhang #define F_INT_MAIN_MULTI_HIT_FAULT		BIT(1)
61b17336c5SHonghui Zhang #define F_INT_INVALID_PA_FAULT			BIT(2)
62b17336c5SHonghui Zhang #define F_INT_ENTRY_REPLACEMENT_FAULT		BIT(3)
63b17336c5SHonghui Zhang #define F_INT_TABLE_WALK_FAULT			BIT(4)
64b17336c5SHonghui Zhang #define F_INT_TLB_MISS_FAULT			BIT(5)
65b17336c5SHonghui Zhang #define F_INT_PFH_DMA_FIFO_OVERFLOW		BIT(6)
66b17336c5SHonghui Zhang #define F_INT_MISS_DMA_FIFO_OVERFLOW		BIT(7)
67b17336c5SHonghui Zhang 
68b17336c5SHonghui Zhang #define F_MMU_TF_PROTECT_SEL(prot)		(((prot) & 0x3) << 5)
69b17336c5SHonghui Zhang #define F_INT_CLR_BIT				BIT(12)
70b17336c5SHonghui Zhang 
71b17336c5SHonghui Zhang #define REG_MMU_FAULT_ST			0x224
72b17336c5SHonghui Zhang #define REG_MMU_FAULT_VA			0x228
73b17336c5SHonghui Zhang #define REG_MMU_INVLD_PA			0x22C
74b17336c5SHonghui Zhang #define REG_MMU_INT_ID				0x388
75b17336c5SHonghui Zhang #define REG_MMU_INVALIDATE			0x5c0
76b17336c5SHonghui Zhang #define REG_MMU_INVLD_START_A			0x5c4
77b17336c5SHonghui Zhang #define REG_MMU_INVLD_END_A			0x5c8
78b17336c5SHonghui Zhang 
79b17336c5SHonghui Zhang #define REG_MMU_INV_SEL				0x5d8
80b17336c5SHonghui Zhang #define REG_MMU_STANDARD_AXI_MODE		0x5e8
81b17336c5SHonghui Zhang 
82b17336c5SHonghui Zhang #define REG_MMU_DCM				0x5f0
83b17336c5SHonghui Zhang #define F_MMU_DCM_ON				BIT(1)
84b17336c5SHonghui Zhang #define REG_MMU_CPE_DONE			0x60c
85b17336c5SHonghui Zhang #define F_DESC_VALID				0x2
86b17336c5SHonghui Zhang #define F_DESC_NONSEC				BIT(3)
87b17336c5SHonghui Zhang #define MT2701_M4U_TF_LARB(TF)			(6 - (((TF) >> 13) & 0x7))
88b17336c5SHonghui Zhang #define MT2701_M4U_TF_PORT(TF)			(((TF) >> 8) & 0xF)
89b17336c5SHonghui Zhang /* MTK generation one iommu HW only support 4K size mapping */
90b17336c5SHonghui Zhang #define MT2701_IOMMU_PAGE_SHIFT			12
91b17336c5SHonghui Zhang #define MT2701_IOMMU_PAGE_SIZE			(1UL << MT2701_IOMMU_PAGE_SHIFT)
92b17336c5SHonghui Zhang 
93b17336c5SHonghui Zhang /*
94b17336c5SHonghui Zhang  * MTK m4u support 4GB iova address space, and only support 4K page
95b17336c5SHonghui Zhang  * mapping. So the pagetable size should be exactly as 4M.
96b17336c5SHonghui Zhang  */
97b17336c5SHonghui Zhang #define M2701_IOMMU_PGT_SIZE			SZ_4M
98b17336c5SHonghui Zhang 
99b17336c5SHonghui Zhang struct mtk_iommu_domain {
100b17336c5SHonghui Zhang 	spinlock_t			pgtlock; /* lock for page table */
101b17336c5SHonghui Zhang 	struct iommu_domain		domain;
102b17336c5SHonghui Zhang 	u32				*pgt_va;
103b17336c5SHonghui Zhang 	dma_addr_t			pgt_pa;
104b17336c5SHonghui Zhang 	struct mtk_iommu_data		*data;
105b17336c5SHonghui Zhang };
106b17336c5SHonghui Zhang 
107b17336c5SHonghui Zhang static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
108b17336c5SHonghui Zhang {
109b17336c5SHonghui Zhang 	return container_of(dom, struct mtk_iommu_domain, domain);
110b17336c5SHonghui Zhang }
111b17336c5SHonghui Zhang 
112b17336c5SHonghui Zhang static const int mt2701_m4u_in_larb[] = {
113b17336c5SHonghui Zhang 	LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
114b17336c5SHonghui Zhang 	LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
115b17336c5SHonghui Zhang };
116b17336c5SHonghui Zhang 
117b17336c5SHonghui Zhang static inline int mt2701_m4u_to_larb(int id)
118b17336c5SHonghui Zhang {
119b17336c5SHonghui Zhang 	int i;
120b17336c5SHonghui Zhang 
121b17336c5SHonghui Zhang 	for (i = ARRAY_SIZE(mt2701_m4u_in_larb) - 1; i >= 0; i--)
122b17336c5SHonghui Zhang 		if ((id) >= mt2701_m4u_in_larb[i])
123b17336c5SHonghui Zhang 			return i;
124b17336c5SHonghui Zhang 
125b17336c5SHonghui Zhang 	return 0;
126b17336c5SHonghui Zhang }
127b17336c5SHonghui Zhang 
128b17336c5SHonghui Zhang static inline int mt2701_m4u_to_port(int id)
129b17336c5SHonghui Zhang {
130b17336c5SHonghui Zhang 	int larb = mt2701_m4u_to_larb(id);
131b17336c5SHonghui Zhang 
132b17336c5SHonghui Zhang 	return id - mt2701_m4u_in_larb[larb];
133b17336c5SHonghui Zhang }
134b17336c5SHonghui Zhang 
135b17336c5SHonghui Zhang static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
136b17336c5SHonghui Zhang {
137b17336c5SHonghui Zhang 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
138b17336c5SHonghui Zhang 			data->base + REG_MMU_INV_SEL);
139b17336c5SHonghui Zhang 	writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
140b17336c5SHonghui Zhang 	wmb(); /* Make sure the tlb flush all done */
141b17336c5SHonghui Zhang }
142b17336c5SHonghui Zhang 
143b17336c5SHonghui Zhang static void mtk_iommu_tlb_flush_range(struct mtk_iommu_data *data,
144b17336c5SHonghui Zhang 				unsigned long iova, size_t size)
145b17336c5SHonghui Zhang {
146b17336c5SHonghui Zhang 	int ret;
147b17336c5SHonghui Zhang 	u32 tmp;
148b17336c5SHonghui Zhang 
149b17336c5SHonghui Zhang 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
150b17336c5SHonghui Zhang 		data->base + REG_MMU_INV_SEL);
151b17336c5SHonghui Zhang 	writel_relaxed(iova & F_MMU_FAULT_VA_MSK,
152b17336c5SHonghui Zhang 		data->base + REG_MMU_INVLD_START_A);
153b17336c5SHonghui Zhang 	writel_relaxed((iova + size - 1) & F_MMU_FAULT_VA_MSK,
154b17336c5SHonghui Zhang 		data->base + REG_MMU_INVLD_END_A);
155b17336c5SHonghui Zhang 	writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE);
156b17336c5SHonghui Zhang 
157b17336c5SHonghui Zhang 	ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
158b17336c5SHonghui Zhang 				tmp, tmp != 0, 10, 100000);
159b17336c5SHonghui Zhang 	if (ret) {
160b17336c5SHonghui Zhang 		dev_warn(data->dev,
161b17336c5SHonghui Zhang 			 "Partial TLB flush timed out, falling back to full flush\n");
162b17336c5SHonghui Zhang 		mtk_iommu_tlb_flush_all(data);
163b17336c5SHonghui Zhang 	}
164b17336c5SHonghui Zhang 	/* Clear the CPE status */
165b17336c5SHonghui Zhang 	writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
166b17336c5SHonghui Zhang }
167b17336c5SHonghui Zhang 
168b17336c5SHonghui Zhang static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
169b17336c5SHonghui Zhang {
170b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = dev_id;
171b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = data->m4u_dom;
172b17336c5SHonghui Zhang 	u32 int_state, regval, fault_iova, fault_pa;
173b17336c5SHonghui Zhang 	unsigned int fault_larb, fault_port;
174b17336c5SHonghui Zhang 
175b17336c5SHonghui Zhang 	/* Read error information from registers */
176b17336c5SHonghui Zhang 	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST);
177b17336c5SHonghui Zhang 	fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
178b17336c5SHonghui Zhang 
179b17336c5SHonghui Zhang 	fault_iova &= F_MMU_FAULT_VA_MSK;
180b17336c5SHonghui Zhang 	fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
181b17336c5SHonghui Zhang 	regval = readl_relaxed(data->base + REG_MMU_INT_ID);
182b17336c5SHonghui Zhang 	fault_larb = MT2701_M4U_TF_LARB(regval);
183b17336c5SHonghui Zhang 	fault_port = MT2701_M4U_TF_PORT(regval);
184b17336c5SHonghui Zhang 
185b17336c5SHonghui Zhang 	/*
186b17336c5SHonghui Zhang 	 * MTK v1 iommu HW could not determine whether the fault is read or
187b17336c5SHonghui Zhang 	 * write fault, report as read fault.
188b17336c5SHonghui Zhang 	 */
189b17336c5SHonghui Zhang 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
190b17336c5SHonghui Zhang 			IOMMU_FAULT_READ))
191b17336c5SHonghui Zhang 		dev_err_ratelimited(data->dev,
192b17336c5SHonghui Zhang 			"fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d\n",
193b17336c5SHonghui Zhang 			int_state, fault_iova, fault_pa,
194b17336c5SHonghui Zhang 			fault_larb, fault_port);
195b17336c5SHonghui Zhang 
196b17336c5SHonghui Zhang 	/* Interrupt clear */
197b17336c5SHonghui Zhang 	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL);
198b17336c5SHonghui Zhang 	regval |= F_INT_CLR_BIT;
199b17336c5SHonghui Zhang 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
200b17336c5SHonghui Zhang 
201b17336c5SHonghui Zhang 	mtk_iommu_tlb_flush_all(data);
202b17336c5SHonghui Zhang 
203b17336c5SHonghui Zhang 	return IRQ_HANDLED;
204b17336c5SHonghui Zhang }
205b17336c5SHonghui Zhang 
206b17336c5SHonghui Zhang static void mtk_iommu_config(struct mtk_iommu_data *data,
207b17336c5SHonghui Zhang 			     struct device *dev, bool enable)
208b17336c5SHonghui Zhang {
209b17336c5SHonghui Zhang 	struct mtk_smi_larb_iommu    *larb_mmu;
210b17336c5SHonghui Zhang 	unsigned int                 larbid, portid;
211a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
21284672f19SRobin Murphy 	int i;
213b17336c5SHonghui Zhang 
21484672f19SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
21584672f19SRobin Murphy 		larbid = mt2701_m4u_to_larb(fwspec->ids[i]);
21684672f19SRobin Murphy 		portid = mt2701_m4u_to_port(fwspec->ids[i]);
217b17336c5SHonghui Zhang 		larb_mmu = &data->smi_imu.larb_imu[larbid];
218b17336c5SHonghui Zhang 
219b17336c5SHonghui Zhang 		dev_dbg(dev, "%s iommu port: %d\n",
220b17336c5SHonghui Zhang 			enable ? "enable" : "disable", portid);
221b17336c5SHonghui Zhang 
222b17336c5SHonghui Zhang 		if (enable)
223b17336c5SHonghui Zhang 			larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
224b17336c5SHonghui Zhang 		else
225b17336c5SHonghui Zhang 			larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
226b17336c5SHonghui Zhang 	}
227b17336c5SHonghui Zhang }
228b17336c5SHonghui Zhang 
229b17336c5SHonghui Zhang static int mtk_iommu_domain_finalise(struct mtk_iommu_data *data)
230b17336c5SHonghui Zhang {
231b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = data->m4u_dom;
232b17336c5SHonghui Zhang 
233b17336c5SHonghui Zhang 	spin_lock_init(&dom->pgtlock);
234b17336c5SHonghui Zhang 
235b17336c5SHonghui Zhang 	dom->pgt_va = dma_zalloc_coherent(data->dev,
236b17336c5SHonghui Zhang 				M2701_IOMMU_PGT_SIZE,
237b17336c5SHonghui Zhang 				&dom->pgt_pa, GFP_KERNEL);
238b17336c5SHonghui Zhang 	if (!dom->pgt_va)
239b17336c5SHonghui Zhang 		return -ENOMEM;
240b17336c5SHonghui Zhang 
241b17336c5SHonghui Zhang 	writel(dom->pgt_pa, data->base + REG_MMU_PT_BASE_ADDR);
242b17336c5SHonghui Zhang 
243b17336c5SHonghui Zhang 	dom->data = data;
244b17336c5SHonghui Zhang 
245b17336c5SHonghui Zhang 	return 0;
246b17336c5SHonghui Zhang }
247b17336c5SHonghui Zhang 
248b17336c5SHonghui Zhang static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
249b17336c5SHonghui Zhang {
250b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom;
251b17336c5SHonghui Zhang 
252b17336c5SHonghui Zhang 	if (type != IOMMU_DOMAIN_UNMANAGED)
253b17336c5SHonghui Zhang 		return NULL;
254b17336c5SHonghui Zhang 
255b17336c5SHonghui Zhang 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
256b17336c5SHonghui Zhang 	if (!dom)
257b17336c5SHonghui Zhang 		return NULL;
258b17336c5SHonghui Zhang 
259b17336c5SHonghui Zhang 	return &dom->domain;
260b17336c5SHonghui Zhang }
261b17336c5SHonghui Zhang 
262b17336c5SHonghui Zhang static void mtk_iommu_domain_free(struct iommu_domain *domain)
263b17336c5SHonghui Zhang {
264b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
265b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = dom->data;
266b17336c5SHonghui Zhang 
267b17336c5SHonghui Zhang 	dma_free_coherent(data->dev, M2701_IOMMU_PGT_SIZE,
268b17336c5SHonghui Zhang 			dom->pgt_va, dom->pgt_pa);
269b17336c5SHonghui Zhang 	kfree(to_mtk_domain(domain));
270b17336c5SHonghui Zhang }
271b17336c5SHonghui Zhang 
272b17336c5SHonghui Zhang static int mtk_iommu_attach_device(struct iommu_domain *domain,
273b17336c5SHonghui Zhang 				   struct device *dev)
274b17336c5SHonghui Zhang {
275b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
276a9bf2eecSJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
277b17336c5SHonghui Zhang 	int ret;
278b17336c5SHonghui Zhang 
27984672f19SRobin Murphy 	if (!data)
280b17336c5SHonghui Zhang 		return -ENODEV;
281b17336c5SHonghui Zhang 
282b17336c5SHonghui Zhang 	if (!data->m4u_dom) {
283b17336c5SHonghui Zhang 		data->m4u_dom = dom;
284b17336c5SHonghui Zhang 		ret = mtk_iommu_domain_finalise(data);
285b17336c5SHonghui Zhang 		if (ret) {
286b17336c5SHonghui Zhang 			data->m4u_dom = NULL;
287b17336c5SHonghui Zhang 			return ret;
288b17336c5SHonghui Zhang 		}
289b17336c5SHonghui Zhang 	}
290b17336c5SHonghui Zhang 
291b17336c5SHonghui Zhang 	mtk_iommu_config(data, dev, true);
292b17336c5SHonghui Zhang 	return 0;
293b17336c5SHonghui Zhang }
294b17336c5SHonghui Zhang 
295b17336c5SHonghui Zhang static void mtk_iommu_detach_device(struct iommu_domain *domain,
296b17336c5SHonghui Zhang 				    struct device *dev)
297b17336c5SHonghui Zhang {
298a9bf2eecSJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
299b17336c5SHonghui Zhang 
30084672f19SRobin Murphy 	if (!data)
301b17336c5SHonghui Zhang 		return;
302b17336c5SHonghui Zhang 
303b17336c5SHonghui Zhang 	mtk_iommu_config(data, dev, false);
304b17336c5SHonghui Zhang }
305b17336c5SHonghui Zhang 
306b17336c5SHonghui Zhang static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
307b17336c5SHonghui Zhang 			 phys_addr_t paddr, size_t size, int prot)
308b17336c5SHonghui Zhang {
309b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
310b17336c5SHonghui Zhang 	unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
311b17336c5SHonghui Zhang 	unsigned long flags;
312b17336c5SHonghui Zhang 	unsigned int i;
313b17336c5SHonghui Zhang 	u32 *pgt_base_iova = dom->pgt_va + (iova  >> MT2701_IOMMU_PAGE_SHIFT);
314b17336c5SHonghui Zhang 	u32 pabase = (u32)paddr;
315b17336c5SHonghui Zhang 	int map_size = 0;
316b17336c5SHonghui Zhang 
317b17336c5SHonghui Zhang 	spin_lock_irqsave(&dom->pgtlock, flags);
318b17336c5SHonghui Zhang 	for (i = 0; i < page_num; i++) {
319b17336c5SHonghui Zhang 		if (pgt_base_iova[i]) {
320b17336c5SHonghui Zhang 			memset(pgt_base_iova, 0, i * sizeof(u32));
321b17336c5SHonghui Zhang 			break;
322b17336c5SHonghui Zhang 		}
323b17336c5SHonghui Zhang 		pgt_base_iova[i] = pabase | F_DESC_VALID | F_DESC_NONSEC;
324b17336c5SHonghui Zhang 		pabase += MT2701_IOMMU_PAGE_SIZE;
325b17336c5SHonghui Zhang 		map_size += MT2701_IOMMU_PAGE_SIZE;
326b17336c5SHonghui Zhang 	}
327b17336c5SHonghui Zhang 
328b17336c5SHonghui Zhang 	spin_unlock_irqrestore(&dom->pgtlock, flags);
329b17336c5SHonghui Zhang 
330b17336c5SHonghui Zhang 	mtk_iommu_tlb_flush_range(dom->data, iova, size);
331b17336c5SHonghui Zhang 
332b17336c5SHonghui Zhang 	return map_size == size ? 0 : -EEXIST;
333b17336c5SHonghui Zhang }
334b17336c5SHonghui Zhang 
335b17336c5SHonghui Zhang static size_t mtk_iommu_unmap(struct iommu_domain *domain,
336b17336c5SHonghui Zhang 			      unsigned long iova, size_t size)
337b17336c5SHonghui Zhang {
338b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
339b17336c5SHonghui Zhang 	unsigned long flags;
340b17336c5SHonghui Zhang 	u32 *pgt_base_iova = dom->pgt_va + (iova  >> MT2701_IOMMU_PAGE_SHIFT);
341b17336c5SHonghui Zhang 	unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
342b17336c5SHonghui Zhang 
343b17336c5SHonghui Zhang 	spin_lock_irqsave(&dom->pgtlock, flags);
344b17336c5SHonghui Zhang 	memset(pgt_base_iova, 0, page_num * sizeof(u32));
345b17336c5SHonghui Zhang 	spin_unlock_irqrestore(&dom->pgtlock, flags);
346b17336c5SHonghui Zhang 
347b17336c5SHonghui Zhang 	mtk_iommu_tlb_flush_range(dom->data, iova, size);
348b17336c5SHonghui Zhang 
349b17336c5SHonghui Zhang 	return size;
350b17336c5SHonghui Zhang }
351b17336c5SHonghui Zhang 
352b17336c5SHonghui Zhang static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
353b17336c5SHonghui Zhang 					  dma_addr_t iova)
354b17336c5SHonghui Zhang {
355b17336c5SHonghui Zhang 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
356b17336c5SHonghui Zhang 	unsigned long flags;
357b17336c5SHonghui Zhang 	phys_addr_t pa;
358b17336c5SHonghui Zhang 
359b17336c5SHonghui Zhang 	spin_lock_irqsave(&dom->pgtlock, flags);
360b17336c5SHonghui Zhang 	pa = *(dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT));
361b17336c5SHonghui Zhang 	pa = pa & (~(MT2701_IOMMU_PAGE_SIZE - 1));
362b17336c5SHonghui Zhang 	spin_unlock_irqrestore(&dom->pgtlock, flags);
363b17336c5SHonghui Zhang 
364b17336c5SHonghui Zhang 	return pa;
365b17336c5SHonghui Zhang }
366b17336c5SHonghui Zhang 
36784672f19SRobin Murphy static struct iommu_ops mtk_iommu_ops;
36884672f19SRobin Murphy 
369b17336c5SHonghui Zhang /*
370b17336c5SHonghui Zhang  * MTK generation one iommu HW only support one iommu domain, and all the client
371b17336c5SHonghui Zhang  * sharing the same iova address space.
372b17336c5SHonghui Zhang  */
373b17336c5SHonghui Zhang static int mtk_iommu_create_mapping(struct device *dev,
374b17336c5SHonghui Zhang 				    struct of_phandle_args *args)
375b17336c5SHonghui Zhang {
376a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
37784672f19SRobin Murphy 	struct mtk_iommu_data *data;
378b17336c5SHonghui Zhang 	struct platform_device *m4updev;
379b17336c5SHonghui Zhang 	struct dma_iommu_mapping *mtk_mapping;
380b17336c5SHonghui Zhang 	struct device *m4udev;
381b17336c5SHonghui Zhang 	int ret;
382b17336c5SHonghui Zhang 
383b17336c5SHonghui Zhang 	if (args->args_count != 1) {
384b17336c5SHonghui Zhang 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
385b17336c5SHonghui Zhang 			args->args_count);
386b17336c5SHonghui Zhang 		return -EINVAL;
387b17336c5SHonghui Zhang 	}
388b17336c5SHonghui Zhang 
389a9bf2eecSJoerg Roedel 	if (!fwspec) {
39084672f19SRobin Murphy 		ret = iommu_fwspec_init(dev, &args->np->fwnode, &mtk_iommu_ops);
39184672f19SRobin Murphy 		if (ret)
39284672f19SRobin Murphy 			return ret;
393a9bf2eecSJoerg Roedel 		fwspec = dev_iommu_fwspec_get(dev);
394a9bf2eecSJoerg Roedel 	} else if (dev_iommu_fwspec_get(dev)->ops != &mtk_iommu_ops) {
39584672f19SRobin Murphy 		return -EINVAL;
39684672f19SRobin Murphy 	}
39784672f19SRobin Murphy 
398a9bf2eecSJoerg Roedel 	if (!fwspec->iommu_priv) {
399b17336c5SHonghui Zhang 		/* Get the m4u device */
400b17336c5SHonghui Zhang 		m4updev = of_find_device_by_node(args->np);
401b17336c5SHonghui Zhang 		if (WARN_ON(!m4updev))
402b17336c5SHonghui Zhang 			return -EINVAL;
403b17336c5SHonghui Zhang 
404a9bf2eecSJoerg Roedel 		fwspec->iommu_priv = platform_get_drvdata(m4updev);
405b17336c5SHonghui Zhang 	}
406b17336c5SHonghui Zhang 
40784672f19SRobin Murphy 	ret = iommu_fwspec_add_ids(dev, args->args, 1);
40884672f19SRobin Murphy 	if (ret)
40984672f19SRobin Murphy 		return ret;
410b17336c5SHonghui Zhang 
411a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
41284672f19SRobin Murphy 	m4udev = data->dev;
413b17336c5SHonghui Zhang 	mtk_mapping = m4udev->archdata.iommu;
414b17336c5SHonghui Zhang 	if (!mtk_mapping) {
415b17336c5SHonghui Zhang 		/* MTK iommu support 4GB iova address space. */
416b17336c5SHonghui Zhang 		mtk_mapping = arm_iommu_create_mapping(&platform_bus_type,
417b17336c5SHonghui Zhang 						0, 1ULL << 32);
41884672f19SRobin Murphy 		if (IS_ERR(mtk_mapping))
41984672f19SRobin Murphy 			return PTR_ERR(mtk_mapping);
42084672f19SRobin Murphy 
421b17336c5SHonghui Zhang 		m4udev->archdata.iommu = mtk_mapping;
422b17336c5SHonghui Zhang 	}
423b17336c5SHonghui Zhang 
424b17336c5SHonghui Zhang 	return 0;
425b17336c5SHonghui Zhang }
426b17336c5SHonghui Zhang 
427b17336c5SHonghui Zhang static int mtk_iommu_add_device(struct device *dev)
428b17336c5SHonghui Zhang {
429a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
430f3e827d7SYong Wu 	struct dma_iommu_mapping *mtk_mapping;
431b17336c5SHonghui Zhang 	struct of_phandle_args iommu_spec;
432b17336c5SHonghui Zhang 	struct of_phandle_iterator it;
4336f66ea09SJoerg Roedel 	struct mtk_iommu_data *data;
4346f66ea09SJoerg Roedel 	struct iommu_group *group;
435b17336c5SHonghui Zhang 	int err;
436b17336c5SHonghui Zhang 
437b17336c5SHonghui Zhang 	of_for_each_phandle(&it, err, dev->of_node, "iommus",
438b17336c5SHonghui Zhang 			"#iommu-cells", 0) {
439b17336c5SHonghui Zhang 		int count = of_phandle_iterator_args(&it, iommu_spec.args,
440b17336c5SHonghui Zhang 					MAX_PHANDLE_ARGS);
441b17336c5SHonghui Zhang 		iommu_spec.np = of_node_get(it.node);
442b17336c5SHonghui Zhang 		iommu_spec.args_count = count;
443b17336c5SHonghui Zhang 
444b17336c5SHonghui Zhang 		mtk_iommu_create_mapping(dev, &iommu_spec);
445b17336c5SHonghui Zhang 		of_node_put(iommu_spec.np);
446b17336c5SHonghui Zhang 	}
447b17336c5SHonghui Zhang 
448a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
44984672f19SRobin Murphy 		return -ENODEV; /* Not a iommu client device */
450b17336c5SHonghui Zhang 
451f3e827d7SYong Wu 	/*
452f3e827d7SYong Wu 	 * This is a short-term bodge because the ARM DMA code doesn't
453f3e827d7SYong Wu 	 * understand multi-device groups, but we have to call into it
454f3e827d7SYong Wu 	 * successfully (and not just rely on a normal IOMMU API attach
455f3e827d7SYong Wu 	 * here) in order to set the correct DMA API ops on @dev.
456f3e827d7SYong Wu 	 */
457f3e827d7SYong Wu 	group = iommu_group_alloc();
458b17336c5SHonghui Zhang 	if (IS_ERR(group))
459b17336c5SHonghui Zhang 		return PTR_ERR(group);
460b17336c5SHonghui Zhang 
461f3e827d7SYong Wu 	err = iommu_group_add_device(group, dev);
462b17336c5SHonghui Zhang 	iommu_group_put(group);
463f3e827d7SYong Wu 	if (err)
464f3e827d7SYong Wu 		return err;
465f3e827d7SYong Wu 
466a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
467f3e827d7SYong Wu 	mtk_mapping = data->dev->archdata.iommu;
468f3e827d7SYong Wu 	err = arm_iommu_attach_device(dev, mtk_mapping);
469f3e827d7SYong Wu 	if (err) {
470f3e827d7SYong Wu 		iommu_group_remove_device(dev);
471f3e827d7SYong Wu 		return err;
472f3e827d7SYong Wu 	}
473f3e827d7SYong Wu 
474f3e827d7SYong Wu 	return iommu_device_link(&data->iommu, dev);;
475b17336c5SHonghui Zhang }
476b17336c5SHonghui Zhang 
477b17336c5SHonghui Zhang static void mtk_iommu_remove_device(struct device *dev)
478b17336c5SHonghui Zhang {
479a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
4806f66ea09SJoerg Roedel 	struct mtk_iommu_data *data;
4816f66ea09SJoerg Roedel 
482a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
483b17336c5SHonghui Zhang 		return;
484b17336c5SHonghui Zhang 
485a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
4866f66ea09SJoerg Roedel 	iommu_device_unlink(&data->iommu, dev);
4876f66ea09SJoerg Roedel 
488b17336c5SHonghui Zhang 	iommu_group_remove_device(dev);
48984672f19SRobin Murphy 	iommu_fwspec_free(dev);
490b17336c5SHonghui Zhang }
491b17336c5SHonghui Zhang 
492b17336c5SHonghui Zhang static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
493b17336c5SHonghui Zhang {
494b17336c5SHonghui Zhang 	u32 regval;
495b17336c5SHonghui Zhang 	int ret;
496b17336c5SHonghui Zhang 
497b17336c5SHonghui Zhang 	ret = clk_prepare_enable(data->bclk);
498b17336c5SHonghui Zhang 	if (ret) {
499b17336c5SHonghui Zhang 		dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
500b17336c5SHonghui Zhang 		return ret;
501b17336c5SHonghui Zhang 	}
502b17336c5SHonghui Zhang 
503b17336c5SHonghui Zhang 	regval = F_MMU_CTRL_COHERENT_EN | F_MMU_TF_PROTECT_SEL(2);
504b17336c5SHonghui Zhang 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
505b17336c5SHonghui Zhang 
506b17336c5SHonghui Zhang 	regval = F_INT_TRANSLATION_FAULT |
507b17336c5SHonghui Zhang 		F_INT_MAIN_MULTI_HIT_FAULT |
508b17336c5SHonghui Zhang 		F_INT_INVALID_PA_FAULT |
509b17336c5SHonghui Zhang 		F_INT_ENTRY_REPLACEMENT_FAULT |
510b17336c5SHonghui Zhang 		F_INT_TABLE_WALK_FAULT |
511b17336c5SHonghui Zhang 		F_INT_TLB_MISS_FAULT |
512b17336c5SHonghui Zhang 		F_INT_PFH_DMA_FIFO_OVERFLOW |
513b17336c5SHonghui Zhang 		F_INT_MISS_DMA_FIFO_OVERFLOW;
514b17336c5SHonghui Zhang 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
515b17336c5SHonghui Zhang 
516b17336c5SHonghui Zhang 	/* protect memory,hw will write here while translation fault */
517b17336c5SHonghui Zhang 	writel_relaxed(data->protect_base,
518b17336c5SHonghui Zhang 			data->base + REG_MMU_IVRP_PADDR);
519b17336c5SHonghui Zhang 
520b17336c5SHonghui Zhang 	writel_relaxed(F_MMU_DCM_ON, data->base + REG_MMU_DCM);
521b17336c5SHonghui Zhang 
522b17336c5SHonghui Zhang 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
523b17336c5SHonghui Zhang 			     dev_name(data->dev), (void *)data)) {
524b17336c5SHonghui Zhang 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
525b17336c5SHonghui Zhang 		clk_disable_unprepare(data->bclk);
526b17336c5SHonghui Zhang 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
527b17336c5SHonghui Zhang 		return -ENODEV;
528b17336c5SHonghui Zhang 	}
529b17336c5SHonghui Zhang 
530b17336c5SHonghui Zhang 	return 0;
531b17336c5SHonghui Zhang }
532b17336c5SHonghui Zhang 
533b17336c5SHonghui Zhang static struct iommu_ops mtk_iommu_ops = {
534b17336c5SHonghui Zhang 	.domain_alloc	= mtk_iommu_domain_alloc,
535b17336c5SHonghui Zhang 	.domain_free	= mtk_iommu_domain_free,
536b17336c5SHonghui Zhang 	.attach_dev	= mtk_iommu_attach_device,
537b17336c5SHonghui Zhang 	.detach_dev	= mtk_iommu_detach_device,
538b17336c5SHonghui Zhang 	.map		= mtk_iommu_map,
539b17336c5SHonghui Zhang 	.unmap		= mtk_iommu_unmap,
540b17336c5SHonghui Zhang 	.iova_to_phys	= mtk_iommu_iova_to_phys,
541b17336c5SHonghui Zhang 	.add_device	= mtk_iommu_add_device,
542b17336c5SHonghui Zhang 	.remove_device	= mtk_iommu_remove_device,
543b17336c5SHonghui Zhang 	.pgsize_bitmap	= ~0UL << MT2701_IOMMU_PAGE_SHIFT,
544b17336c5SHonghui Zhang };
545b17336c5SHonghui Zhang 
546b17336c5SHonghui Zhang static const struct of_device_id mtk_iommu_of_ids[] = {
547b17336c5SHonghui Zhang 	{ .compatible = "mediatek,mt2701-m4u", },
548b17336c5SHonghui Zhang 	{}
549b17336c5SHonghui Zhang };
550b17336c5SHonghui Zhang 
551b17336c5SHonghui Zhang static const struct component_master_ops mtk_iommu_com_ops = {
552b17336c5SHonghui Zhang 	.bind		= mtk_iommu_bind,
553b17336c5SHonghui Zhang 	.unbind		= mtk_iommu_unbind,
554b17336c5SHonghui Zhang };
555b17336c5SHonghui Zhang 
556b17336c5SHonghui Zhang static int mtk_iommu_probe(struct platform_device *pdev)
557b17336c5SHonghui Zhang {
558b17336c5SHonghui Zhang 	struct mtk_iommu_data		*data;
559b17336c5SHonghui Zhang 	struct device			*dev = &pdev->dev;
560b17336c5SHonghui Zhang 	struct resource			*res;
561b17336c5SHonghui Zhang 	struct component_match		*match = NULL;
562b17336c5SHonghui Zhang 	struct of_phandle_args		larb_spec;
563b17336c5SHonghui Zhang 	struct of_phandle_iterator	it;
564b17336c5SHonghui Zhang 	void				*protect;
565b17336c5SHonghui Zhang 	int				larb_nr, ret, err;
566b17336c5SHonghui Zhang 
567b17336c5SHonghui Zhang 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
568b17336c5SHonghui Zhang 	if (!data)
569b17336c5SHonghui Zhang 		return -ENOMEM;
570b17336c5SHonghui Zhang 
571b17336c5SHonghui Zhang 	data->dev = dev;
572b17336c5SHonghui Zhang 
573b17336c5SHonghui Zhang 	/* Protect memory. HW will access here while translation fault.*/
574b17336c5SHonghui Zhang 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
575b17336c5SHonghui Zhang 			GFP_KERNEL | GFP_DMA);
576b17336c5SHonghui Zhang 	if (!protect)
577b17336c5SHonghui Zhang 		return -ENOMEM;
578b17336c5SHonghui Zhang 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
579b17336c5SHonghui Zhang 
580b17336c5SHonghui Zhang 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
581b17336c5SHonghui Zhang 	data->base = devm_ioremap_resource(dev, res);
582b17336c5SHonghui Zhang 	if (IS_ERR(data->base))
583b17336c5SHonghui Zhang 		return PTR_ERR(data->base);
584b17336c5SHonghui Zhang 
585b17336c5SHonghui Zhang 	data->irq = platform_get_irq(pdev, 0);
586b17336c5SHonghui Zhang 	if (data->irq < 0)
587b17336c5SHonghui Zhang 		return data->irq;
588b17336c5SHonghui Zhang 
589b17336c5SHonghui Zhang 	data->bclk = devm_clk_get(dev, "bclk");
590b17336c5SHonghui Zhang 	if (IS_ERR(data->bclk))
591b17336c5SHonghui Zhang 		return PTR_ERR(data->bclk);
592b17336c5SHonghui Zhang 
593b17336c5SHonghui Zhang 	larb_nr = 0;
594b17336c5SHonghui Zhang 	of_for_each_phandle(&it, err, dev->of_node,
595b17336c5SHonghui Zhang 			"mediatek,larbs", NULL, 0) {
596b17336c5SHonghui Zhang 		struct platform_device *plarbdev;
597b17336c5SHonghui Zhang 		int count = of_phandle_iterator_args(&it, larb_spec.args,
598b17336c5SHonghui Zhang 					MAX_PHANDLE_ARGS);
599b17336c5SHonghui Zhang 
600b17336c5SHonghui Zhang 		if (count)
601b17336c5SHonghui Zhang 			continue;
602b17336c5SHonghui Zhang 
603b17336c5SHonghui Zhang 		larb_spec.np = of_node_get(it.node);
604b17336c5SHonghui Zhang 		if (!of_device_is_available(larb_spec.np))
605b17336c5SHonghui Zhang 			continue;
606b17336c5SHonghui Zhang 
607b17336c5SHonghui Zhang 		plarbdev = of_find_device_by_node(larb_spec.np);
608b17336c5SHonghui Zhang 		if (!plarbdev) {
609b17336c5SHonghui Zhang 			plarbdev = of_platform_device_create(
610b17336c5SHonghui Zhang 						larb_spec.np, NULL,
611b17336c5SHonghui Zhang 						platform_bus_type.dev_root);
61200c7c81fSRussell King 			if (!plarbdev) {
61300c7c81fSRussell King 				of_node_put(larb_spec.np);
614b17336c5SHonghui Zhang 				return -EPROBE_DEFER;
615b17336c5SHonghui Zhang 			}
61600c7c81fSRussell King 		}
617b17336c5SHonghui Zhang 
618b17336c5SHonghui Zhang 		data->smi_imu.larb_imu[larb_nr].dev = &plarbdev->dev;
61900c7c81fSRussell King 		component_match_add_release(dev, &match, release_of,
62000c7c81fSRussell King 					    compare_of, larb_spec.np);
621b17336c5SHonghui Zhang 		larb_nr++;
622b17336c5SHonghui Zhang 	}
623b17336c5SHonghui Zhang 
624b17336c5SHonghui Zhang 	data->smi_imu.larb_nr = larb_nr;
625b17336c5SHonghui Zhang 
626b17336c5SHonghui Zhang 	platform_set_drvdata(pdev, data);
627b17336c5SHonghui Zhang 
628b17336c5SHonghui Zhang 	ret = mtk_iommu_hw_init(data);
629b17336c5SHonghui Zhang 	if (ret)
630b17336c5SHonghui Zhang 		return ret;
631b17336c5SHonghui Zhang 
6326f66ea09SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
6336f66ea09SJoerg Roedel 				     dev_name(&pdev->dev));
6346f66ea09SJoerg Roedel 	if (ret)
6356f66ea09SJoerg Roedel 		return ret;
6366f66ea09SJoerg Roedel 
6376f66ea09SJoerg Roedel 	iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
6386f66ea09SJoerg Roedel 
6396f66ea09SJoerg Roedel 	ret = iommu_device_register(&data->iommu);
6406f66ea09SJoerg Roedel 	if (ret)
6416f66ea09SJoerg Roedel 		return ret;
6426f66ea09SJoerg Roedel 
643b17336c5SHonghui Zhang 	if (!iommu_present(&platform_bus_type))
644b17336c5SHonghui Zhang 		bus_set_iommu(&platform_bus_type,  &mtk_iommu_ops);
645b17336c5SHonghui Zhang 
646b17336c5SHonghui Zhang 	return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
647b17336c5SHonghui Zhang }
648b17336c5SHonghui Zhang 
649b17336c5SHonghui Zhang static int mtk_iommu_remove(struct platform_device *pdev)
650b17336c5SHonghui Zhang {
651b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
652b17336c5SHonghui Zhang 
6536f66ea09SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
6546f66ea09SJoerg Roedel 	iommu_device_unregister(&data->iommu);
6556f66ea09SJoerg Roedel 
656b17336c5SHonghui Zhang 	if (iommu_present(&platform_bus_type))
657b17336c5SHonghui Zhang 		bus_set_iommu(&platform_bus_type, NULL);
658b17336c5SHonghui Zhang 
659b17336c5SHonghui Zhang 	clk_disable_unprepare(data->bclk);
660b17336c5SHonghui Zhang 	devm_free_irq(&pdev->dev, data->irq, data);
661b17336c5SHonghui Zhang 	component_master_del(&pdev->dev, &mtk_iommu_com_ops);
662b17336c5SHonghui Zhang 	return 0;
663b17336c5SHonghui Zhang }
664b17336c5SHonghui Zhang 
665b17336c5SHonghui Zhang static int __maybe_unused mtk_iommu_suspend(struct device *dev)
666b17336c5SHonghui Zhang {
667b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
668b17336c5SHonghui Zhang 	struct mtk_iommu_suspend_reg *reg = &data->reg;
669b17336c5SHonghui Zhang 	void __iomem *base = data->base;
670b17336c5SHonghui Zhang 
671b17336c5SHonghui Zhang 	reg->standard_axi_mode = readl_relaxed(base +
672b17336c5SHonghui Zhang 					       REG_MMU_STANDARD_AXI_MODE);
673b17336c5SHonghui Zhang 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM);
674b17336c5SHonghui Zhang 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
675b17336c5SHonghui Zhang 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL);
676b17336c5SHonghui Zhang 	return 0;
677b17336c5SHonghui Zhang }
678b17336c5SHonghui Zhang 
679b17336c5SHonghui Zhang static int __maybe_unused mtk_iommu_resume(struct device *dev)
680b17336c5SHonghui Zhang {
681b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
682b17336c5SHonghui Zhang 	struct mtk_iommu_suspend_reg *reg = &data->reg;
683b17336c5SHonghui Zhang 	void __iomem *base = data->base;
684b17336c5SHonghui Zhang 
685b17336c5SHonghui Zhang 	writel_relaxed(data->m4u_dom->pgt_pa, base + REG_MMU_PT_BASE_ADDR);
686b17336c5SHonghui Zhang 	writel_relaxed(reg->standard_axi_mode,
687b17336c5SHonghui Zhang 		       base + REG_MMU_STANDARD_AXI_MODE);
688b17336c5SHonghui Zhang 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM);
689b17336c5SHonghui Zhang 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
690b17336c5SHonghui Zhang 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL);
691b17336c5SHonghui Zhang 	writel_relaxed(data->protect_base, base + REG_MMU_IVRP_PADDR);
692b17336c5SHonghui Zhang 	return 0;
693b17336c5SHonghui Zhang }
694b17336c5SHonghui Zhang 
695131bc8ebSJoerg Roedel static const struct dev_pm_ops mtk_iommu_pm_ops = {
696b17336c5SHonghui Zhang 	SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
697b17336c5SHonghui Zhang };
698b17336c5SHonghui Zhang 
699b17336c5SHonghui Zhang static struct platform_driver mtk_iommu_driver = {
700b17336c5SHonghui Zhang 	.probe	= mtk_iommu_probe,
701b17336c5SHonghui Zhang 	.remove	= mtk_iommu_remove,
702b17336c5SHonghui Zhang 	.driver	= {
703395df08dSMatthias Brugger 		.name = "mtk-iommu-v1",
704b17336c5SHonghui Zhang 		.of_match_table = mtk_iommu_of_ids,
705b17336c5SHonghui Zhang 		.pm = &mtk_iommu_pm_ops,
706b17336c5SHonghui Zhang 	}
707b17336c5SHonghui Zhang };
708b17336c5SHonghui Zhang 
709b17336c5SHonghui Zhang static int __init m4u_init(void)
710b17336c5SHonghui Zhang {
711b17336c5SHonghui Zhang 	return platform_driver_register(&mtk_iommu_driver);
712b17336c5SHonghui Zhang }
713b17336c5SHonghui Zhang subsys_initcall(m4u_init);
714