xref: /openbmc/linux/drivers/iommu/mtk_iommu_v1.c (revision da5d2748)
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 
367b65f5016SArvind Yadav static const 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);
445da5d2748SJoerg Roedel 
446da5d2748SJoerg Roedel 		/* dev->iommu_fwspec might have changed */
447da5d2748SJoerg Roedel 		fwspec = dev_iommu_fwspec_get(dev);
448da5d2748SJoerg Roedel 
449b17336c5SHonghui Zhang 		of_node_put(iommu_spec.np);
450b17336c5SHonghui Zhang 	}
451b17336c5SHonghui Zhang 
452a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
45384672f19SRobin Murphy 		return -ENODEV; /* Not a iommu client device */
454b17336c5SHonghui Zhang 
455f3e827d7SYong Wu 	/*
456f3e827d7SYong Wu 	 * This is a short-term bodge because the ARM DMA code doesn't
457f3e827d7SYong Wu 	 * understand multi-device groups, but we have to call into it
458f3e827d7SYong Wu 	 * successfully (and not just rely on a normal IOMMU API attach
459f3e827d7SYong Wu 	 * here) in order to set the correct DMA API ops on @dev.
460f3e827d7SYong Wu 	 */
461f3e827d7SYong Wu 	group = iommu_group_alloc();
462b17336c5SHonghui Zhang 	if (IS_ERR(group))
463b17336c5SHonghui Zhang 		return PTR_ERR(group);
464b17336c5SHonghui Zhang 
465f3e827d7SYong Wu 	err = iommu_group_add_device(group, dev);
466b17336c5SHonghui Zhang 	iommu_group_put(group);
467f3e827d7SYong Wu 	if (err)
468f3e827d7SYong Wu 		return err;
469f3e827d7SYong Wu 
470a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
471f3e827d7SYong Wu 	mtk_mapping = data->dev->archdata.iommu;
472f3e827d7SYong Wu 	err = arm_iommu_attach_device(dev, mtk_mapping);
473f3e827d7SYong Wu 	if (err) {
474f3e827d7SYong Wu 		iommu_group_remove_device(dev);
475f3e827d7SYong Wu 		return err;
476f3e827d7SYong Wu 	}
477f3e827d7SYong Wu 
478f3e827d7SYong Wu 	return iommu_device_link(&data->iommu, dev);;
479b17336c5SHonghui Zhang }
480b17336c5SHonghui Zhang 
481b17336c5SHonghui Zhang static void mtk_iommu_remove_device(struct device *dev)
482b17336c5SHonghui Zhang {
483a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
4846f66ea09SJoerg Roedel 	struct mtk_iommu_data *data;
4856f66ea09SJoerg Roedel 
486a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
487b17336c5SHonghui Zhang 		return;
488b17336c5SHonghui Zhang 
489a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
4906f66ea09SJoerg Roedel 	iommu_device_unlink(&data->iommu, dev);
4916f66ea09SJoerg Roedel 
492b17336c5SHonghui Zhang 	iommu_group_remove_device(dev);
49384672f19SRobin Murphy 	iommu_fwspec_free(dev);
494b17336c5SHonghui Zhang }
495b17336c5SHonghui Zhang 
496b17336c5SHonghui Zhang static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
497b17336c5SHonghui Zhang {
498b17336c5SHonghui Zhang 	u32 regval;
499b17336c5SHonghui Zhang 	int ret;
500b17336c5SHonghui Zhang 
501b17336c5SHonghui Zhang 	ret = clk_prepare_enable(data->bclk);
502b17336c5SHonghui Zhang 	if (ret) {
503b17336c5SHonghui Zhang 		dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
504b17336c5SHonghui Zhang 		return ret;
505b17336c5SHonghui Zhang 	}
506b17336c5SHonghui Zhang 
507b17336c5SHonghui Zhang 	regval = F_MMU_CTRL_COHERENT_EN | F_MMU_TF_PROTECT_SEL(2);
508b17336c5SHonghui Zhang 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
509b17336c5SHonghui Zhang 
510b17336c5SHonghui Zhang 	regval = F_INT_TRANSLATION_FAULT |
511b17336c5SHonghui Zhang 		F_INT_MAIN_MULTI_HIT_FAULT |
512b17336c5SHonghui Zhang 		F_INT_INVALID_PA_FAULT |
513b17336c5SHonghui Zhang 		F_INT_ENTRY_REPLACEMENT_FAULT |
514b17336c5SHonghui Zhang 		F_INT_TABLE_WALK_FAULT |
515b17336c5SHonghui Zhang 		F_INT_TLB_MISS_FAULT |
516b17336c5SHonghui Zhang 		F_INT_PFH_DMA_FIFO_OVERFLOW |
517b17336c5SHonghui Zhang 		F_INT_MISS_DMA_FIFO_OVERFLOW;
518b17336c5SHonghui Zhang 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
519b17336c5SHonghui Zhang 
520b17336c5SHonghui Zhang 	/* protect memory,hw will write here while translation fault */
521b17336c5SHonghui Zhang 	writel_relaxed(data->protect_base,
522b17336c5SHonghui Zhang 			data->base + REG_MMU_IVRP_PADDR);
523b17336c5SHonghui Zhang 
524b17336c5SHonghui Zhang 	writel_relaxed(F_MMU_DCM_ON, data->base + REG_MMU_DCM);
525b17336c5SHonghui Zhang 
526b17336c5SHonghui Zhang 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
527b17336c5SHonghui Zhang 			     dev_name(data->dev), (void *)data)) {
528b17336c5SHonghui Zhang 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
529b17336c5SHonghui Zhang 		clk_disable_unprepare(data->bclk);
530b17336c5SHonghui Zhang 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
531b17336c5SHonghui Zhang 		return -ENODEV;
532b17336c5SHonghui Zhang 	}
533b17336c5SHonghui Zhang 
534b17336c5SHonghui Zhang 	return 0;
535b17336c5SHonghui Zhang }
536b17336c5SHonghui Zhang 
537b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
538b17336c5SHonghui Zhang 	.domain_alloc	= mtk_iommu_domain_alloc,
539b17336c5SHonghui Zhang 	.domain_free	= mtk_iommu_domain_free,
540b17336c5SHonghui Zhang 	.attach_dev	= mtk_iommu_attach_device,
541b17336c5SHonghui Zhang 	.detach_dev	= mtk_iommu_detach_device,
542b17336c5SHonghui Zhang 	.map		= mtk_iommu_map,
543b17336c5SHonghui Zhang 	.unmap		= mtk_iommu_unmap,
544b17336c5SHonghui Zhang 	.iova_to_phys	= mtk_iommu_iova_to_phys,
545b17336c5SHonghui Zhang 	.add_device	= mtk_iommu_add_device,
546b17336c5SHonghui Zhang 	.remove_device	= mtk_iommu_remove_device,
547b17336c5SHonghui Zhang 	.pgsize_bitmap	= ~0UL << MT2701_IOMMU_PAGE_SHIFT,
548b17336c5SHonghui Zhang };
549b17336c5SHonghui Zhang 
550b17336c5SHonghui Zhang static const struct of_device_id mtk_iommu_of_ids[] = {
551b17336c5SHonghui Zhang 	{ .compatible = "mediatek,mt2701-m4u", },
552b17336c5SHonghui Zhang 	{}
553b17336c5SHonghui Zhang };
554b17336c5SHonghui Zhang 
555b17336c5SHonghui Zhang static const struct component_master_ops mtk_iommu_com_ops = {
556b17336c5SHonghui Zhang 	.bind		= mtk_iommu_bind,
557b17336c5SHonghui Zhang 	.unbind		= mtk_iommu_unbind,
558b17336c5SHonghui Zhang };
559b17336c5SHonghui Zhang 
560b17336c5SHonghui Zhang static int mtk_iommu_probe(struct platform_device *pdev)
561b17336c5SHonghui Zhang {
562b17336c5SHonghui Zhang 	struct mtk_iommu_data		*data;
563b17336c5SHonghui Zhang 	struct device			*dev = &pdev->dev;
564b17336c5SHonghui Zhang 	struct resource			*res;
565b17336c5SHonghui Zhang 	struct component_match		*match = NULL;
566b17336c5SHonghui Zhang 	struct of_phandle_args		larb_spec;
567b17336c5SHonghui Zhang 	struct of_phandle_iterator	it;
568b17336c5SHonghui Zhang 	void				*protect;
569b17336c5SHonghui Zhang 	int				larb_nr, ret, err;
570b17336c5SHonghui Zhang 
571b17336c5SHonghui Zhang 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
572b17336c5SHonghui Zhang 	if (!data)
573b17336c5SHonghui Zhang 		return -ENOMEM;
574b17336c5SHonghui Zhang 
575b17336c5SHonghui Zhang 	data->dev = dev;
576b17336c5SHonghui Zhang 
577b17336c5SHonghui Zhang 	/* Protect memory. HW will access here while translation fault.*/
578b17336c5SHonghui Zhang 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
579b17336c5SHonghui Zhang 			GFP_KERNEL | GFP_DMA);
580b17336c5SHonghui Zhang 	if (!protect)
581b17336c5SHonghui Zhang 		return -ENOMEM;
582b17336c5SHonghui Zhang 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
583b17336c5SHonghui Zhang 
584b17336c5SHonghui Zhang 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
585b17336c5SHonghui Zhang 	data->base = devm_ioremap_resource(dev, res);
586b17336c5SHonghui Zhang 	if (IS_ERR(data->base))
587b17336c5SHonghui Zhang 		return PTR_ERR(data->base);
588b17336c5SHonghui Zhang 
589b17336c5SHonghui Zhang 	data->irq = platform_get_irq(pdev, 0);
590b17336c5SHonghui Zhang 	if (data->irq < 0)
591b17336c5SHonghui Zhang 		return data->irq;
592b17336c5SHonghui Zhang 
593b17336c5SHonghui Zhang 	data->bclk = devm_clk_get(dev, "bclk");
594b17336c5SHonghui Zhang 	if (IS_ERR(data->bclk))
595b17336c5SHonghui Zhang 		return PTR_ERR(data->bclk);
596b17336c5SHonghui Zhang 
597b17336c5SHonghui Zhang 	larb_nr = 0;
598b17336c5SHonghui Zhang 	of_for_each_phandle(&it, err, dev->of_node,
599b17336c5SHonghui Zhang 			"mediatek,larbs", NULL, 0) {
600b17336c5SHonghui Zhang 		struct platform_device *plarbdev;
601b17336c5SHonghui Zhang 		int count = of_phandle_iterator_args(&it, larb_spec.args,
602b17336c5SHonghui Zhang 					MAX_PHANDLE_ARGS);
603b17336c5SHonghui Zhang 
604b17336c5SHonghui Zhang 		if (count)
605b17336c5SHonghui Zhang 			continue;
606b17336c5SHonghui Zhang 
607b17336c5SHonghui Zhang 		larb_spec.np = of_node_get(it.node);
608b17336c5SHonghui Zhang 		if (!of_device_is_available(larb_spec.np))
609b17336c5SHonghui Zhang 			continue;
610b17336c5SHonghui Zhang 
611b17336c5SHonghui Zhang 		plarbdev = of_find_device_by_node(larb_spec.np);
612b17336c5SHonghui Zhang 		if (!plarbdev) {
613b17336c5SHonghui Zhang 			plarbdev = of_platform_device_create(
614b17336c5SHonghui Zhang 						larb_spec.np, NULL,
615b17336c5SHonghui Zhang 						platform_bus_type.dev_root);
61600c7c81fSRussell King 			if (!plarbdev) {
61700c7c81fSRussell King 				of_node_put(larb_spec.np);
618b17336c5SHonghui Zhang 				return -EPROBE_DEFER;
619b17336c5SHonghui Zhang 			}
62000c7c81fSRussell King 		}
621b17336c5SHonghui Zhang 
622b17336c5SHonghui Zhang 		data->smi_imu.larb_imu[larb_nr].dev = &plarbdev->dev;
62300c7c81fSRussell King 		component_match_add_release(dev, &match, release_of,
62400c7c81fSRussell King 					    compare_of, larb_spec.np);
625b17336c5SHonghui Zhang 		larb_nr++;
626b17336c5SHonghui Zhang 	}
627b17336c5SHonghui Zhang 
628b17336c5SHonghui Zhang 	data->smi_imu.larb_nr = larb_nr;
629b17336c5SHonghui Zhang 
630b17336c5SHonghui Zhang 	platform_set_drvdata(pdev, data);
631b17336c5SHonghui Zhang 
632b17336c5SHonghui Zhang 	ret = mtk_iommu_hw_init(data);
633b17336c5SHonghui Zhang 	if (ret)
634b17336c5SHonghui Zhang 		return ret;
635b17336c5SHonghui Zhang 
6366f66ea09SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
6376f66ea09SJoerg Roedel 				     dev_name(&pdev->dev));
6386f66ea09SJoerg Roedel 	if (ret)
6396f66ea09SJoerg Roedel 		return ret;
6406f66ea09SJoerg Roedel 
6416f66ea09SJoerg Roedel 	iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
6426f66ea09SJoerg Roedel 
6436f66ea09SJoerg Roedel 	ret = iommu_device_register(&data->iommu);
6446f66ea09SJoerg Roedel 	if (ret)
6456f66ea09SJoerg Roedel 		return ret;
6466f66ea09SJoerg Roedel 
647b17336c5SHonghui Zhang 	if (!iommu_present(&platform_bus_type))
648b17336c5SHonghui Zhang 		bus_set_iommu(&platform_bus_type,  &mtk_iommu_ops);
649b17336c5SHonghui Zhang 
650b17336c5SHonghui Zhang 	return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
651b17336c5SHonghui Zhang }
652b17336c5SHonghui Zhang 
653b17336c5SHonghui Zhang static int mtk_iommu_remove(struct platform_device *pdev)
654b17336c5SHonghui Zhang {
655b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
656b17336c5SHonghui Zhang 
6576f66ea09SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
6586f66ea09SJoerg Roedel 	iommu_device_unregister(&data->iommu);
6596f66ea09SJoerg Roedel 
660b17336c5SHonghui Zhang 	if (iommu_present(&platform_bus_type))
661b17336c5SHonghui Zhang 		bus_set_iommu(&platform_bus_type, NULL);
662b17336c5SHonghui Zhang 
663b17336c5SHonghui Zhang 	clk_disable_unprepare(data->bclk);
664b17336c5SHonghui Zhang 	devm_free_irq(&pdev->dev, data->irq, data);
665b17336c5SHonghui Zhang 	component_master_del(&pdev->dev, &mtk_iommu_com_ops);
666b17336c5SHonghui Zhang 	return 0;
667b17336c5SHonghui Zhang }
668b17336c5SHonghui Zhang 
669b17336c5SHonghui Zhang static int __maybe_unused mtk_iommu_suspend(struct device *dev)
670b17336c5SHonghui Zhang {
671b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
672b17336c5SHonghui Zhang 	struct mtk_iommu_suspend_reg *reg = &data->reg;
673b17336c5SHonghui Zhang 	void __iomem *base = data->base;
674b17336c5SHonghui Zhang 
675b17336c5SHonghui Zhang 	reg->standard_axi_mode = readl_relaxed(base +
676b17336c5SHonghui Zhang 					       REG_MMU_STANDARD_AXI_MODE);
677b17336c5SHonghui Zhang 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM);
678b17336c5SHonghui Zhang 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
679b17336c5SHonghui Zhang 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL);
680b17336c5SHonghui Zhang 	return 0;
681b17336c5SHonghui Zhang }
682b17336c5SHonghui Zhang 
683b17336c5SHonghui Zhang static int __maybe_unused mtk_iommu_resume(struct device *dev)
684b17336c5SHonghui Zhang {
685b17336c5SHonghui Zhang 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
686b17336c5SHonghui Zhang 	struct mtk_iommu_suspend_reg *reg = &data->reg;
687b17336c5SHonghui Zhang 	void __iomem *base = data->base;
688b17336c5SHonghui Zhang 
689b17336c5SHonghui Zhang 	writel_relaxed(data->m4u_dom->pgt_pa, base + REG_MMU_PT_BASE_ADDR);
690b17336c5SHonghui Zhang 	writel_relaxed(reg->standard_axi_mode,
691b17336c5SHonghui Zhang 		       base + REG_MMU_STANDARD_AXI_MODE);
692b17336c5SHonghui Zhang 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM);
693b17336c5SHonghui Zhang 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
694b17336c5SHonghui Zhang 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL);
695b17336c5SHonghui Zhang 	writel_relaxed(data->protect_base, base + REG_MMU_IVRP_PADDR);
696b17336c5SHonghui Zhang 	return 0;
697b17336c5SHonghui Zhang }
698b17336c5SHonghui Zhang 
699131bc8ebSJoerg Roedel static const struct dev_pm_ops mtk_iommu_pm_ops = {
700b17336c5SHonghui Zhang 	SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
701b17336c5SHonghui Zhang };
702b17336c5SHonghui Zhang 
703b17336c5SHonghui Zhang static struct platform_driver mtk_iommu_driver = {
704b17336c5SHonghui Zhang 	.probe	= mtk_iommu_probe,
705b17336c5SHonghui Zhang 	.remove	= mtk_iommu_remove,
706b17336c5SHonghui Zhang 	.driver	= {
707395df08dSMatthias Brugger 		.name = "mtk-iommu-v1",
708b17336c5SHonghui Zhang 		.of_match_table = mtk_iommu_of_ids,
709b17336c5SHonghui Zhang 		.pm = &mtk_iommu_pm_ops,
710b17336c5SHonghui Zhang 	}
711b17336c5SHonghui Zhang };
712b17336c5SHonghui Zhang 
713b17336c5SHonghui Zhang static int __init m4u_init(void)
714b17336c5SHonghui Zhang {
715b17336c5SHonghui Zhang 	return platform_driver_register(&mtk_iommu_driver);
716b17336c5SHonghui Zhang }
717b17336c5SHonghui Zhang subsys_initcall(m4u_init);
718