xref: /linux/drivers/iommu/mtk_iommu.c (revision 9a630a4b41a2639b65d024a5d2d88ed3ecca130a)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20df4fabeSYong Wu /*
30df4fabeSYong Wu  * Copyright (c) 2015-2016 MediaTek Inc.
40df4fabeSYong Wu  * Author: Yong Wu <yong.wu@mediatek.com>
50df4fabeSYong Wu  */
6ef0f0986SYong Wu #include <linux/bitfield.h>
70df4fabeSYong Wu #include <linux/bug.h>
80df4fabeSYong Wu #include <linux/clk.h>
90df4fabeSYong Wu #include <linux/component.h>
100df4fabeSYong Wu #include <linux/device.h>
11803cf9e5SYong Wu #include <linux/dma-direct.h>
120df4fabeSYong Wu #include <linux/err.h>
130df4fabeSYong Wu #include <linux/interrupt.h>
140df4fabeSYong Wu #include <linux/io.h>
150df4fabeSYong Wu #include <linux/iommu.h>
160df4fabeSYong Wu #include <linux/iopoll.h>
170df4fabeSYong Wu #include <linux/list.h>
18c2c59456SMiles Chen #include <linux/mfd/syscon.h>
1918d8c74eSYong Wu #include <linux/module.h>
200df4fabeSYong Wu #include <linux/of_address.h>
210df4fabeSYong Wu #include <linux/of_irq.h>
220df4fabeSYong Wu #include <linux/of_platform.h>
230df4fabeSYong Wu #include <linux/platform_device.h>
24baf94e6eSYong Wu #include <linux/pm_runtime.h>
25c2c59456SMiles Chen #include <linux/regmap.h>
260df4fabeSYong Wu #include <linux/slab.h>
270df4fabeSYong Wu #include <linux/spinlock.h>
28c2c59456SMiles Chen #include <linux/soc/mediatek/infracfg.h>
290df4fabeSYong Wu #include <asm/barrier.h>
300df4fabeSYong Wu #include <soc/mediatek/smi.h>
310df4fabeSYong Wu 
329ca340c9SHonghui Zhang #include "mtk_iommu.h"
330df4fabeSYong Wu 
340df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
35907ba6a1SYong Wu #define MMU_PT_ADDR_MASK			GENMASK(31, 7)
360df4fabeSYong Wu 
370df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
380df4fabeSYong Wu #define F_ALL_INVLD				0x2
390df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
400df4fabeSYong Wu 
410df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
420df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
430df4fabeSYong Wu 
44068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2			0x02c
45b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1			0x038
460df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
470df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
480df4fabeSYong Wu 
4975eed350SChao Hao #define REG_MMU_MISC_CTRL			0x048
504bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
514bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))
524bb2bf4cSChao Hao 
530df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
5435c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5535c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
560df4fabeSYong Wu 
570df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
58acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
590df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
60acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
610df4fabeSYong Wu 
620df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6370ca608bSYong Wu 
6430e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6530e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
660df4fabeSYong Wu 
670df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
680df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
690df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
700df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
710df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
720df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
730df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
740df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
750df4fabeSYong Wu 
760df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
7715a01f4cSYong Wu 						/* mmu0 | mmu1 */
7815a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
7915a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
8015a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
8115a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8215a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8315a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8415a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
850df4fabeSYong Wu 
860df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
870df4fabeSYong Wu 
880df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
8915a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
9015a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
910df4fabeSYong Wu 
9215a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
93ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
94ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
95ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
960df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
970df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
980df4fabeSYong Wu 
9915a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
10015a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
10115a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
10215a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
10315a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
10437276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10537276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
10615a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
10715a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
1080df4fabeSYong Wu 
109829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
1100df4fabeSYong Wu 
1116b717796SChao Hao #define HAS_4GB_MODE			BIT(0)
1126b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */
1136b717796SChao Hao #define HAS_BCLK			BIT(1)
1146b717796SChao Hao #define HAS_VLD_PA_RNG			BIT(2)
1156b717796SChao Hao #define RESET_AXI			BIT(3)
1164bb2bf4cSChao Hao #define OUT_ORDER_WR_EN			BIT(4)
11737276e00SChao Hao #define HAS_SUB_COMM			BIT(5)
11835c1b48dSChao Hao #define WR_THROT_EN			BIT(6)
119d1b5ef00SFabien Parent #define HAS_LEGACY_IVRP_PADDR		BIT(7)
1202f317da4SYong Wu #define IOVA_34_EN			BIT(8)
1216b717796SChao Hao 
1226b717796SChao Hao #define MTK_IOMMU_HAS_FLAG(pdata, _x) \
1236b717796SChao Hao 		((((pdata)->flags) & (_x)) == (_x))
1246b717796SChao Hao 
1250df4fabeSYong Wu struct mtk_iommu_domain {
1260df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
1270df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
1280df4fabeSYong Wu 
12908500c43SYong Wu 	struct mtk_iommu_data		*data;
1300df4fabeSYong Wu 	struct iommu_domain		domain;
1310df4fabeSYong Wu };
1320df4fabeSYong Wu 
133b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
1340df4fabeSYong Wu 
1357f37a91dSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data);
1367f37a91dSYong Wu 
137bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({					\
138bfed8731SYong Wu 	dma_addr_t _addr = iova;					\
139bfed8731SYong Wu 	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
140bfed8731SYong Wu })
141bfed8731SYong Wu 
14276ce6546SYong Wu /*
14376ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
14476ce6546SYong Wu  *
14576ce6546SYong Wu  * CPU Physical address:
14676ce6546SYong Wu  * ====================
14776ce6546SYong Wu  *
14876ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
14976ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
15076ce6546SYong Wu  * +--I/O--+------------Memory-------------+
15176ce6546SYong Wu  *
15276ce6546SYong Wu  * IOMMU output physical address:
15376ce6546SYong Wu  *  =============================
15476ce6546SYong Wu  *
15576ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
15676ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
15776ce6546SYong Wu  *                                 +------------Memory-------------+
15876ce6546SYong Wu  *
15976ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
16076ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
16176ce6546SYong Wu  * 'E', the CPU physical address keep as is.
16276ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
16376ce6546SYong Wu  */
164b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
16576ce6546SYong Wu 
1667c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
1677c3a2ec0SYong Wu 
1687c3a2ec0SYong Wu #define for_each_m4u(data)	list_for_each_entry(data, &m4ulist, list)
1697c3a2ec0SYong Wu 
170585e58f4SYong Wu struct mtk_iommu_iova_region {
171585e58f4SYong Wu 	dma_addr_t		iova_base;
172585e58f4SYong Wu 	unsigned long long	size;
173585e58f4SYong Wu };
174585e58f4SYong Wu 
175585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = {
176585e58f4SYong Wu 	{.iova_base = 0,		.size = SZ_4G},
177585e58f4SYong Wu };
178585e58f4SYong Wu 
1799e3489e0SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[] = {
1809e3489e0SYong Wu 	{ .iova_base = 0x0,		.size = SZ_4G},		/* disp: 0 ~ 4G */
1819e3489e0SYong Wu 	#if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)
1829e3489e0SYong Wu 	{ .iova_base = SZ_4G,		.size = SZ_4G},		/* vdec: 4G ~ 8G */
1839e3489e0SYong Wu 	{ .iova_base = SZ_4G * 2,	.size = SZ_4G},		/* CAM/MDP: 8G ~ 12G */
1849e3489e0SYong Wu 	{ .iova_base = 0x240000000ULL,	.size = 0x4000000},	/* CCU0 */
1859e3489e0SYong Wu 	{ .iova_base = 0x244000000ULL,	.size = 0x4000000},	/* CCU1 */
1869e3489e0SYong Wu 	#endif
1879e3489e0SYong Wu };
1889e3489e0SYong Wu 
1897c3a2ec0SYong Wu /*
1907c3a2ec0SYong Wu  * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
1917c3a2ec0SYong Wu  * for the performance.
1927c3a2ec0SYong Wu  *
1937c3a2ec0SYong Wu  * Here always return the mtk_iommu_data of the first probed M4U where the
1947c3a2ec0SYong Wu  * iommu domain information is recorded.
1957c3a2ec0SYong Wu  */
1967c3a2ec0SYong Wu static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
1977c3a2ec0SYong Wu {
1987c3a2ec0SYong Wu 	struct mtk_iommu_data *data;
1997c3a2ec0SYong Wu 
2007c3a2ec0SYong Wu 	for_each_m4u(data)
2017c3a2ec0SYong Wu 		return data;
2027c3a2ec0SYong Wu 
2037c3a2ec0SYong Wu 	return NULL;
2047c3a2ec0SYong Wu }
2057c3a2ec0SYong Wu 
2060df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
2070df4fabeSYong Wu {
2080df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
2090df4fabeSYong Wu }
2100df4fabeSYong Wu 
2110954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
2120df4fabeSYong Wu {
2137c3a2ec0SYong Wu 	for_each_m4u(data) {
214c0b57581SYong Wu 		if (pm_runtime_get_if_in_use(data->dev) <= 0)
215c0b57581SYong Wu 			continue;
216c0b57581SYong Wu 
2177c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
218b053bc71SChao Hao 			       data->base + data->plat_data->inv_sel_reg);
2190df4fabeSYong Wu 		writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
2200df4fabeSYong Wu 		wmb(); /* Make sure the tlb flush all done */
221c0b57581SYong Wu 
222c0b57581SYong Wu 		pm_runtime_put(data->dev);
2230df4fabeSYong Wu 	}
2247c3a2ec0SYong Wu }
2250df4fabeSYong Wu 
2261f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
2270954d61aSYong Wu 					   size_t granule,
2280954d61aSYong Wu 					   struct mtk_iommu_data *data)
2290df4fabeSYong Wu {
230c0b57581SYong Wu 	bool has_pm = !!data->dev->pm_domain;
2311f4fd624SYong Wu 	unsigned long flags;
2321f4fd624SYong Wu 	int ret;
2331f4fd624SYong Wu 	u32 tmp;
2340df4fabeSYong Wu 
2357c3a2ec0SYong Wu 	for_each_m4u(data) {
236c0b57581SYong Wu 		if (has_pm) {
237c0b57581SYong Wu 			if (pm_runtime_get_if_in_use(data->dev) <= 0)
238c0b57581SYong Wu 				continue;
239c0b57581SYong Wu 		}
240c0b57581SYong Wu 
2411f4fd624SYong Wu 		spin_lock_irqsave(&data->tlb_lock, flags);
2427c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
243b053bc71SChao Hao 			       data->base + data->plat_data->inv_sel_reg);
2440df4fabeSYong Wu 
245bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova),
246bfed8731SYong Wu 			       data->base + REG_MMU_INVLD_START_A);
247bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
2487c3a2ec0SYong Wu 			       data->base + REG_MMU_INVLD_END_A);
2497c3a2ec0SYong Wu 		writel_relaxed(F_MMU_INV_RANGE,
2507c3a2ec0SYong Wu 			       data->base + REG_MMU_INVALIDATE);
2510df4fabeSYong Wu 
2521f4fd624SYong Wu 		/* tlb sync */
2537c3a2ec0SYong Wu 		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
254c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
2550df4fabeSYong Wu 		if (ret) {
2560df4fabeSYong Wu 			dev_warn(data->dev,
2570df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
2580954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
2590df4fabeSYong Wu 		}
2600df4fabeSYong Wu 		/* Clear the CPE status */
2610df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
262da3cc91bSYong Wu 		spin_unlock_irqrestore(&data->tlb_lock, flags);
263c0b57581SYong Wu 
264c0b57581SYong Wu 		if (has_pm)
265c0b57581SYong Wu 			pm_runtime_put(data->dev);
2660df4fabeSYong Wu 	}
2677c3a2ec0SYong Wu }
2680df4fabeSYong Wu 
2690df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
2700df4fabeSYong Wu {
2710df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_id;
2720df4fabeSYong Wu 	struct mtk_iommu_domain *dom = data->m4u_dom;
27337276e00SChao Hao 	unsigned int fault_larb, fault_port, sub_comm = 0;
274ef0f0986SYong Wu 	u32 int_state, regval, va34_32, pa34_32;
275ef0f0986SYong Wu 	u64 fault_iova, fault_pa;
2760df4fabeSYong Wu 	bool layer, write;
2770df4fabeSYong Wu 
2780df4fabeSYong Wu 	/* Read error info from registers */
2790df4fabeSYong Wu 	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
28015a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
28115a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
28215a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
28315a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
28415a01f4cSYong Wu 	} else {
28515a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
28615a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
28715a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
28815a01f4cSYong Wu 	}
2890df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
2900df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
291ef0f0986SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN)) {
292ef0f0986SYong Wu 		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
293ef0f0986SYong Wu 		pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
294ef0f0986SYong Wu 		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
295ef0f0986SYong Wu 		fault_iova |= (u64)va34_32 << 32;
296ef0f0986SYong Wu 		fault_pa |= (u64)pa34_32 << 32;
297ef0f0986SYong Wu 	}
298ef0f0986SYong Wu 
29915a01f4cSYong Wu 	fault_port = F_MMU_INT_ID_PORT_ID(regval);
30037276e00SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM)) {
30137276e00SChao Hao 		fault_larb = F_MMU_INT_ID_COMM_ID(regval);
30237276e00SChao Hao 		sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
30337276e00SChao Hao 	} else {
30437276e00SChao Hao 		fault_larb = F_MMU_INT_ID_LARB_ID(regval);
30537276e00SChao Hao 	}
30637276e00SChao Hao 	fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
307b3e5eee7SYong Wu 
3080df4fabeSYong Wu 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
3090df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
3100df4fabeSYong Wu 		dev_err_ratelimited(
3110df4fabeSYong Wu 			data->dev,
312ef0f0986SYong Wu 			"fault type=0x%x iova=0x%llx pa=0x%llx larb=%d port=%d layer=%d %s\n",
3130df4fabeSYong Wu 			int_state, fault_iova, fault_pa, fault_larb, fault_port,
3140df4fabeSYong Wu 			layer, write ? "write" : "read");
3150df4fabeSYong Wu 	}
3160df4fabeSYong Wu 
3170df4fabeSYong Wu 	/* Interrupt clear */
3180df4fabeSYong Wu 	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
3190df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
3200df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
3210df4fabeSYong Wu 
3220df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
3230df4fabeSYong Wu 
3240df4fabeSYong Wu 	return IRQ_HANDLED;
3250df4fabeSYong Wu }
3260df4fabeSYong Wu 
327803cf9e5SYong Wu static int mtk_iommu_get_domain_id(struct device *dev,
328803cf9e5SYong Wu 				   const struct mtk_iommu_plat_data *plat_data)
329803cf9e5SYong Wu {
330803cf9e5SYong Wu 	const struct mtk_iommu_iova_region *rgn = plat_data->iova_region;
331803cf9e5SYong Wu 	const struct bus_dma_region *dma_rgn = dev->dma_range_map;
332803cf9e5SYong Wu 	int i, candidate = -1;
333803cf9e5SYong Wu 	dma_addr_t dma_end;
334803cf9e5SYong Wu 
335803cf9e5SYong Wu 	if (!dma_rgn || plat_data->iova_region_nr == 1)
336803cf9e5SYong Wu 		return 0;
337803cf9e5SYong Wu 
338803cf9e5SYong Wu 	dma_end = dma_rgn->dma_start + dma_rgn->size - 1;
339803cf9e5SYong Wu 	for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) {
340803cf9e5SYong Wu 		/* Best fit. */
341803cf9e5SYong Wu 		if (dma_rgn->dma_start == rgn->iova_base &&
342803cf9e5SYong Wu 		    dma_end == rgn->iova_base + rgn->size - 1)
343803cf9e5SYong Wu 			return i;
344803cf9e5SYong Wu 		/* ok if it is inside this region. */
345803cf9e5SYong Wu 		if (dma_rgn->dma_start >= rgn->iova_base &&
346803cf9e5SYong Wu 		    dma_end < rgn->iova_base + rgn->size)
347803cf9e5SYong Wu 			candidate = i;
348803cf9e5SYong Wu 	}
349803cf9e5SYong Wu 
350803cf9e5SYong Wu 	if (candidate >= 0)
351803cf9e5SYong Wu 		return candidate;
352803cf9e5SYong Wu 	dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n",
353803cf9e5SYong Wu 		&dma_rgn->dma_start, dma_rgn->size);
354803cf9e5SYong Wu 	return -EINVAL;
355803cf9e5SYong Wu }
356803cf9e5SYong Wu 
3578d2c749eSYong Wu static void mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev,
3588d2c749eSYong Wu 			     bool enable, unsigned int domid)
3590df4fabeSYong Wu {
3600df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
3610df4fabeSYong Wu 	unsigned int                 larbid, portid;
362a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3638d2c749eSYong Wu 	const struct mtk_iommu_iova_region *region;
36458f0d1d5SRobin Murphy 	int i;
3650df4fabeSYong Wu 
36658f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
36758f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
36858f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
3698d2c749eSYong Wu 
3701ee9feb2SYong Wu 		larb_mmu = &data->larb_imu[larbid];
3710df4fabeSYong Wu 
3728d2c749eSYong Wu 		region = data->plat_data->iova_region + domid;
3738d2c749eSYong Wu 		larb_mmu->bank[portid] = upper_32_bits(region->iova_base);
3748d2c749eSYong Wu 
3758d2c749eSYong Wu 		dev_dbg(dev, "%s iommu for larb(%s) port %d dom %d bank %d.\n",
3768d2c749eSYong Wu 			enable ? "enable" : "disable", dev_name(larb_mmu->dev),
3778d2c749eSYong Wu 			portid, domid, larb_mmu->bank[portid]);
3780df4fabeSYong Wu 
3790df4fabeSYong Wu 		if (enable)
3800df4fabeSYong Wu 			larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
3810df4fabeSYong Wu 		else
3820df4fabeSYong Wu 			larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
3830df4fabeSYong Wu 	}
3840df4fabeSYong Wu }
3850df4fabeSYong Wu 
3864f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
387c3045f39SYong Wu 				     struct mtk_iommu_data *data,
388c3045f39SYong Wu 				     unsigned int domid)
3890df4fabeSYong Wu {
390c3045f39SYong Wu 	const struct mtk_iommu_iova_region *region;
391c3045f39SYong Wu 
392c3045f39SYong Wu 	/* Use the exist domain as there is only one pgtable here. */
393c3045f39SYong Wu 	if (data->m4u_dom) {
394c3045f39SYong Wu 		dom->iop = data->m4u_dom->iop;
395c3045f39SYong Wu 		dom->cfg = data->m4u_dom->cfg;
396c3045f39SYong Wu 		dom->domain.pgsize_bitmap = data->m4u_dom->cfg.pgsize_bitmap;
397c3045f39SYong Wu 		goto update_iova_region;
398c3045f39SYong Wu 	}
399c3045f39SYong Wu 
4000df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
4010df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
4020df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
403b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
4040df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
4052f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
4060df4fabeSYong Wu 		.iommu_dev = data->dev,
4070df4fabeSYong Wu 	};
4080df4fabeSYong Wu 
4099bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
4109bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
4119bdfe4c1SYong Wu 	else
4129bdfe4c1SYong Wu 		dom->cfg.oas = 35;
4139bdfe4c1SYong Wu 
4140df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
4150df4fabeSYong Wu 	if (!dom->iop) {
4160df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
4170df4fabeSYong Wu 		return -EINVAL;
4180df4fabeSYong Wu 	}
4190df4fabeSYong Wu 
4200df4fabeSYong Wu 	/* Update our support page sizes bitmap */
421d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
422b7875eb9SYong Wu 
423c3045f39SYong Wu update_iova_region:
424c3045f39SYong Wu 	/* Update the iova region for this domain */
425c3045f39SYong Wu 	region = data->plat_data->iova_region + domid;
426c3045f39SYong Wu 	dom->domain.geometry.aperture_start = region->iova_base;
427c3045f39SYong Wu 	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
428b7875eb9SYong Wu 	dom->domain.geometry.force_aperture = true;
4290df4fabeSYong Wu 	return 0;
4300df4fabeSYong Wu }
4310df4fabeSYong Wu 
4320df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
4330df4fabeSYong Wu {
4340df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
4350df4fabeSYong Wu 
4360df4fabeSYong Wu 	if (type != IOMMU_DOMAIN_DMA)
4370df4fabeSYong Wu 		return NULL;
4380df4fabeSYong Wu 
4390df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
4400df4fabeSYong Wu 	if (!dom)
4410df4fabeSYong Wu 		return NULL;
4420df4fabeSYong Wu 
4434f956c97SYong Wu 	return &dom->domain;
4444f956c97SYong Wu }
4454f956c97SYong Wu 
4460df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
4470df4fabeSYong Wu {
4480df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
4490df4fabeSYong Wu }
4500df4fabeSYong Wu 
4510df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
4520df4fabeSYong Wu 				   struct device *dev)
4530df4fabeSYong Wu {
4543524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
4550df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
456c0b57581SYong Wu 	struct device *m4udev = data->dev;
457803cf9e5SYong Wu 	int ret, domid;
4580df4fabeSYong Wu 
459803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
460803cf9e5SYong Wu 	if (domid < 0)
461803cf9e5SYong Wu 		return domid;
462803cf9e5SYong Wu 
4634f956c97SYong Wu 	if (!dom->data) {
464c3045f39SYong Wu 		if (mtk_iommu_domain_finalise(dom, data, domid))
4654f956c97SYong Wu 			return -ENODEV;
4664f956c97SYong Wu 		dom->data = data;
4674f956c97SYong Wu 	}
4684f956c97SYong Wu 
4697f37a91dSYong Wu 	if (!data->m4u_dom) { /* Initialize the M4U HW */
470c0b57581SYong Wu 		ret = pm_runtime_resume_and_get(m4udev);
471c0b57581SYong Wu 		if (ret < 0)
4727f37a91dSYong Wu 			return ret;
473c0b57581SYong Wu 
474c0b57581SYong Wu 		ret = mtk_iommu_hw_init(data);
475c0b57581SYong Wu 		if (ret) {
476c0b57581SYong Wu 			pm_runtime_put(m4udev);
477c0b57581SYong Wu 			return ret;
478c0b57581SYong Wu 		}
4790df4fabeSYong Wu 		data->m4u_dom = dom;
480d1e5f26fSRobin Murphy 		writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
4814b00f5acSYong Wu 		       data->base + REG_MMU_PT_BASE_ADDR);
482c0b57581SYong Wu 
483c0b57581SYong Wu 		pm_runtime_put(m4udev);
4840df4fabeSYong Wu 	}
4850df4fabeSYong Wu 
4868d2c749eSYong Wu 	mtk_iommu_config(data, dev, true, domid);
4870df4fabeSYong Wu 	return 0;
4880df4fabeSYong Wu }
4890df4fabeSYong Wu 
4900df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
4910df4fabeSYong Wu 				    struct device *dev)
4920df4fabeSYong Wu {
4933524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
4940df4fabeSYong Wu 
4958d2c749eSYong Wu 	mtk_iommu_config(data, dev, false, 0);
4960df4fabeSYong Wu }
4970df4fabeSYong Wu 
4980df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
499781ca2deSTom Murphy 			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
5000df4fabeSYong Wu {
5010df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5020df4fabeSYong Wu 
503b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
50408500c43SYong Wu 	if (dom->data->enable_4GB)
505b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
506b4dad40eSYong Wu 
50760829b4dSYong Wu 	/* Synchronize with the tlb_lock */
508f34ce7a7SBaolin Wang 	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
5090df4fabeSYong Wu }
5100df4fabeSYong Wu 
5110df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
51256f8af5eSWill Deacon 			      unsigned long iova, size_t size,
51356f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
5140df4fabeSYong Wu {
5150df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5160df4fabeSYong Wu 
5173136895cSRobin Murphy 	iommu_iotlb_gather_add_range(gather, iova, size);
51860829b4dSYong Wu 	return dom->iop->unmap(dom->iop, iova, size, gather);
5190df4fabeSYong Wu }
5200df4fabeSYong Wu 
52156f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
52256f8af5eSWill Deacon {
52308500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
52408500c43SYong Wu 
52508500c43SYong Wu 	mtk_iommu_tlb_flush_all(dom->data);
52656f8af5eSWill Deacon }
52756f8af5eSWill Deacon 
52856f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
52956f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
5304d689b61SRobin Murphy {
53108500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
532862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
533da3cc91bSYong Wu 
5341f4fd624SYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize,
53508500c43SYong Wu 				       dom->data);
5364d689b61SRobin Murphy }
5374d689b61SRobin Murphy 
53820143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
53920143451SYong Wu 			       size_t size)
54020143451SYong Wu {
54108500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
54220143451SYong Wu 
54308500c43SYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, size, dom->data);
54420143451SYong Wu }
54520143451SYong Wu 
5460df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
5470df4fabeSYong Wu 					  dma_addr_t iova)
5480df4fabeSYong Wu {
5490df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5500df4fabeSYong Wu 	phys_addr_t pa;
5510df4fabeSYong Wu 
5520df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
553f13efafcSArnd Bergmann 	if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
554f13efafcSArnd Bergmann 	    dom->data->enable_4GB &&
555f13efafcSArnd Bergmann 	    pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
556b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
55730e2fccfSYong Wu 
5580df4fabeSYong Wu 	return pa;
5590df4fabeSYong Wu }
5600df4fabeSYong Wu 
56180e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
5620df4fabeSYong Wu {
563a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
564b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
5650df4fabeSYong Wu 
566a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
56780e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
5680df4fabeSYong Wu 
5693524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
570b16c0170SJoerg Roedel 
57180e4592aSJoerg Roedel 	return &data->iommu;
5720df4fabeSYong Wu }
5730df4fabeSYong Wu 
57480e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
5750df4fabeSYong Wu {
576a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
577b16c0170SJoerg Roedel 
578a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
5790df4fabeSYong Wu 		return;
5800df4fabeSYong Wu 
58158f0d1d5SRobin Murphy 	iommu_fwspec_free(dev);
5820df4fabeSYong Wu }
5830df4fabeSYong Wu 
5840df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
5850df4fabeSYong Wu {
5867c3a2ec0SYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
587c3045f39SYong Wu 	struct iommu_group *group;
588803cf9e5SYong Wu 	int domid;
5890df4fabeSYong Wu 
59058f0d1d5SRobin Murphy 	if (!data)
5910df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
5920df4fabeSYong Wu 
593803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
594803cf9e5SYong Wu 	if (domid < 0)
595803cf9e5SYong Wu 		return ERR_PTR(domid);
596803cf9e5SYong Wu 
597c3045f39SYong Wu 	group = data->m4u_group[domid];
598c3045f39SYong Wu 	if (!group) {
599c3045f39SYong Wu 		group = iommu_group_alloc();
600c3045f39SYong Wu 		if (!IS_ERR(group))
601c3045f39SYong Wu 			data->m4u_group[domid] = group;
6023a8d40b6SRobin Murphy 	} else {
603c3045f39SYong Wu 		iommu_group_ref_get(group);
6040df4fabeSYong Wu 	}
605c3045f39SYong Wu 	return group;
6060df4fabeSYong Wu }
6070df4fabeSYong Wu 
6080df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
6090df4fabeSYong Wu {
6100df4fabeSYong Wu 	struct platform_device *m4updev;
6110df4fabeSYong Wu 
6120df4fabeSYong Wu 	if (args->args_count != 1) {
6130df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
6140df4fabeSYong Wu 			args->args_count);
6150df4fabeSYong Wu 		return -EINVAL;
6160df4fabeSYong Wu 	}
6170df4fabeSYong Wu 
6183524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
6190df4fabeSYong Wu 		/* Get the m4u device */
6200df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
6210df4fabeSYong Wu 		if (WARN_ON(!m4updev))
6220df4fabeSYong Wu 			return -EINVAL;
6230df4fabeSYong Wu 
6243524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
6250df4fabeSYong Wu 	}
6260df4fabeSYong Wu 
62758f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
6280df4fabeSYong Wu }
6290df4fabeSYong Wu 
630ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev,
631ab1d5281SYong Wu 				       struct list_head *head)
632ab1d5281SYong Wu {
633ab1d5281SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
634ab1d5281SYong Wu 	unsigned int domid = mtk_iommu_get_domain_id(dev, data->plat_data), i;
635ab1d5281SYong Wu 	const struct mtk_iommu_iova_region *resv, *curdom;
636ab1d5281SYong Wu 	struct iommu_resv_region *region;
637ab1d5281SYong Wu 	int prot = IOMMU_WRITE | IOMMU_READ;
638ab1d5281SYong Wu 
6397a566173SColin Ian King 	if ((int)domid < 0)
640ab1d5281SYong Wu 		return;
641ab1d5281SYong Wu 	curdom = data->plat_data->iova_region + domid;
642ab1d5281SYong Wu 	for (i = 0; i < data->plat_data->iova_region_nr; i++) {
643ab1d5281SYong Wu 		resv = data->plat_data->iova_region + i;
644ab1d5281SYong Wu 
645ab1d5281SYong Wu 		/* Only reserve when the region is inside the current domain */
646ab1d5281SYong Wu 		if (resv->iova_base <= curdom->iova_base ||
647ab1d5281SYong Wu 		    resv->iova_base + resv->size >= curdom->iova_base + curdom->size)
648ab1d5281SYong Wu 			continue;
649ab1d5281SYong Wu 
650ab1d5281SYong Wu 		region = iommu_alloc_resv_region(resv->iova_base, resv->size,
651ab1d5281SYong Wu 						 prot, IOMMU_RESV_RESERVED);
652ab1d5281SYong Wu 		if (!region)
653ab1d5281SYong Wu 			return;
654ab1d5281SYong Wu 
655ab1d5281SYong Wu 		list_add_tail(&region->list, head);
656ab1d5281SYong Wu 	}
657ab1d5281SYong Wu }
658ab1d5281SYong Wu 
659b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
6600df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
66180e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
66280e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
6630df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
6640df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
665ab1d5281SYong Wu 	.get_resv_regions = mtk_iommu_get_resv_regions,
666ab1d5281SYong Wu 	.put_resv_regions = generic_iommu_put_resv_regions,
6670df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
66818d8c74eSYong Wu 	.owner		= THIS_MODULE,
669*9a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
670*9a630a4bSLu Baolu 		.attach_dev	= mtk_iommu_attach_device,
671*9a630a4bSLu Baolu 		.detach_dev	= mtk_iommu_detach_device,
672*9a630a4bSLu Baolu 		.map		= mtk_iommu_map,
673*9a630a4bSLu Baolu 		.unmap		= mtk_iommu_unmap,
674*9a630a4bSLu Baolu 		.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
675*9a630a4bSLu Baolu 		.iotlb_sync	= mtk_iommu_iotlb_sync,
676*9a630a4bSLu Baolu 		.iotlb_sync_map	= mtk_iommu_sync_map,
677*9a630a4bSLu Baolu 		.iova_to_phys	= mtk_iommu_iova_to_phys,
678*9a630a4bSLu Baolu 		.free		= mtk_iommu_domain_free,
679*9a630a4bSLu Baolu 	}
6800df4fabeSYong Wu };
6810df4fabeSYong Wu 
6820df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
6830df4fabeSYong Wu {
6840df4fabeSYong Wu 	u32 regval;
6850df4fabeSYong Wu 
68686444413SChao Hao 	if (data->plat_data->m4u_plat == M4U_MT8173) {
687acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
688acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
68986444413SChao Hao 	} else {
69086444413SChao Hao 		regval = readl_relaxed(data->base + REG_MMU_CTRL_REG);
69186444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
69286444413SChao Hao 	}
6930df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
6940df4fabeSYong Wu 
6950df4fabeSYong Wu 	regval = F_L2_MULIT_HIT_EN |
6960df4fabeSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
6970df4fabeSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
6980df4fabeSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
6990df4fabeSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
7000df4fabeSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
7010df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
7020df4fabeSYong Wu 
7030df4fabeSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
7040df4fabeSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
7050df4fabeSYong Wu 		F_INT_INVALID_PA_FAULT |
7060df4fabeSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
7070df4fabeSYong Wu 		F_INT_TLB_MISS_FAULT |
7080df4fabeSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
7090df4fabeSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
7100df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
7110df4fabeSYong Wu 
712d1b5ef00SFabien Parent 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
71370ca608bSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
71470ca608bSYong Wu 	else
71570ca608bSYong Wu 		regval = lower_32_bits(data->protect_base) |
71670ca608bSYong Wu 			 upper_32_bits(data->protect_base);
71770ca608bSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
71870ca608bSYong Wu 
7196b717796SChao Hao 	if (data->enable_4GB &&
7206b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
72130e2fccfSYong Wu 		/*
72230e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
72330e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
72430e2fccfSYong Wu 		 */
72530e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
72630e2fccfSYong Wu 		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
72730e2fccfSYong Wu 	}
7280df4fabeSYong Wu 	writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
72935c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
73035c1b48dSChao Hao 		/* write command throttling mode */
73135c1b48dSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL);
73235c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
73335c1b48dSChao Hao 		writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL);
73435c1b48dSChao Hao 	}
735e6dec923SYong Wu 
7366b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
73775eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
7384bb2bf4cSChao Hao 		regval = 0;
7394bb2bf4cSChao Hao 	} else {
7404bb2bf4cSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
7414bb2bf4cSChao Hao 		regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
7424bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
7434bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
74475eed350SChao Hao 	}
7454bb2bf4cSChao Hao 	writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
7460df4fabeSYong Wu 
7470df4fabeSYong Wu 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
7480df4fabeSYong Wu 			     dev_name(data->dev), (void *)data)) {
7490df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
7500df4fabeSYong Wu 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
7510df4fabeSYong Wu 		return -ENODEV;
7520df4fabeSYong Wu 	}
7530df4fabeSYong Wu 
7540df4fabeSYong Wu 	return 0;
7550df4fabeSYong Wu }
7560df4fabeSYong Wu 
7570df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
7580df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
7590df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
7600df4fabeSYong Wu };
7610df4fabeSYong Wu 
7620df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
7630df4fabeSYong Wu {
7640df4fabeSYong Wu 	struct mtk_iommu_data   *data;
7650df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
766baf94e6eSYong Wu 	struct device_node	*larbnode, *smicomm_node;
767baf94e6eSYong Wu 	struct platform_device	*plarbdev;
768baf94e6eSYong Wu 	struct device_link	*link;
7690df4fabeSYong Wu 	struct resource         *res;
770b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
7710df4fabeSYong Wu 	struct component_match  *match = NULL;
772c2c59456SMiles Chen 	struct regmap		*infracfg;
7730df4fabeSYong Wu 	void                    *protect;
7740b6c0ad3SAndrzej Hajda 	int                     i, larb_nr, ret;
775c2c59456SMiles Chen 	u32			val;
776c2c59456SMiles Chen 	char                    *p;
7770df4fabeSYong Wu 
7780df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
7790df4fabeSYong Wu 	if (!data)
7800df4fabeSYong Wu 		return -ENOMEM;
7810df4fabeSYong Wu 	data->dev = dev;
782cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
7830df4fabeSYong Wu 
7840df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
7850df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
7860df4fabeSYong Wu 	if (!protect)
7870df4fabeSYong Wu 		return -ENOMEM;
7880df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
7890df4fabeSYong Wu 
790c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
791c2c59456SMiles Chen 		switch (data->plat_data->m4u_plat) {
792c2c59456SMiles Chen 		case M4U_MT2712:
793c2c59456SMiles Chen 			p = "mediatek,mt2712-infracfg";
794c2c59456SMiles Chen 			break;
795c2c59456SMiles Chen 		case M4U_MT8173:
796c2c59456SMiles Chen 			p = "mediatek,mt8173-infracfg";
797c2c59456SMiles Chen 			break;
798c2c59456SMiles Chen 		default:
799c2c59456SMiles Chen 			p = NULL;
800c2c59456SMiles Chen 		}
801c2c59456SMiles Chen 
802c2c59456SMiles Chen 		infracfg = syscon_regmap_lookup_by_compatible(p);
803c2c59456SMiles Chen 
804c2c59456SMiles Chen 		if (IS_ERR(infracfg))
805c2c59456SMiles Chen 			return PTR_ERR(infracfg);
806c2c59456SMiles Chen 
807c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
808c2c59456SMiles Chen 		if (ret)
809c2c59456SMiles Chen 			return ret;
810c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
811c2c59456SMiles Chen 	}
81201e23c93SYong Wu 
8130df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
8140df4fabeSYong Wu 	data->base = devm_ioremap_resource(dev, res);
8150df4fabeSYong Wu 	if (IS_ERR(data->base))
8160df4fabeSYong Wu 		return PTR_ERR(data->base);
817b16c0170SJoerg Roedel 	ioaddr = res->start;
8180df4fabeSYong Wu 
8190df4fabeSYong Wu 	data->irq = platform_get_irq(pdev, 0);
8200df4fabeSYong Wu 	if (data->irq < 0)
8210df4fabeSYong Wu 		return data->irq;
8220df4fabeSYong Wu 
8236b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
8240df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
8250df4fabeSYong Wu 		if (IS_ERR(data->bclk))
8260df4fabeSYong Wu 			return PTR_ERR(data->bclk);
8272aa4c259SYong Wu 	}
8280df4fabeSYong Wu 
8290df4fabeSYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node,
8300df4fabeSYong Wu 					     "mediatek,larbs", NULL);
8310df4fabeSYong Wu 	if (larb_nr < 0)
8320df4fabeSYong Wu 		return larb_nr;
8330df4fabeSYong Wu 
8340df4fabeSYong Wu 	for (i = 0; i < larb_nr; i++) {
835e6dec923SYong Wu 		u32 id;
8360df4fabeSYong Wu 
8370df4fabeSYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
8380df4fabeSYong Wu 		if (!larbnode)
8390df4fabeSYong Wu 			return -EINVAL;
8400df4fabeSYong Wu 
8411eb8e4e2SWen Yang 		if (!of_device_is_available(larbnode)) {
8421eb8e4e2SWen Yang 			of_node_put(larbnode);
8430df4fabeSYong Wu 			continue;
8441eb8e4e2SWen Yang 		}
8450df4fabeSYong Wu 
846e6dec923SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
847e6dec923SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
848e6dec923SYong Wu 			id = i;
849e6dec923SYong Wu 
8500df4fabeSYong Wu 		plarbdev = of_find_device_by_node(larbnode);
8511eb8e4e2SWen Yang 		if (!plarbdev) {
8521eb8e4e2SWen Yang 			of_node_put(larbnode);
8530df4fabeSYong Wu 			return -EPROBE_DEFER;
8541eb8e4e2SWen Yang 		}
8551ee9feb2SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
8560df4fabeSYong Wu 
85700c7c81fSRussell King 		component_match_add_release(dev, &match, release_of,
85800c7c81fSRussell King 					    compare_of, larbnode);
8590df4fabeSYong Wu 	}
8600df4fabeSYong Wu 
861baf94e6eSYong Wu 	/* Get smi-common dev from the last larb. */
862baf94e6eSYong Wu 	smicomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
863baf94e6eSYong Wu 	if (!smicomm_node)
864baf94e6eSYong Wu 		return -EINVAL;
865baf94e6eSYong Wu 
866baf94e6eSYong Wu 	plarbdev = of_find_device_by_node(smicomm_node);
867baf94e6eSYong Wu 	of_node_put(smicomm_node);
868baf94e6eSYong Wu 	data->smicomm_dev = &plarbdev->dev;
869baf94e6eSYong Wu 
870c0b57581SYong Wu 	pm_runtime_enable(dev);
871c0b57581SYong Wu 
872baf94e6eSYong Wu 	link = device_link_add(data->smicomm_dev, dev,
873baf94e6eSYong Wu 			DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
874baf94e6eSYong Wu 	if (!link) {
875a92a90acSDan Carpenter 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
876a92a90acSDan Carpenter 		ret = -EINVAL;
877c0b57581SYong Wu 		goto out_runtime_disable;
878baf94e6eSYong Wu 	}
879baf94e6eSYong Wu 
8800df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
8810df4fabeSYong Wu 
882b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
883b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
884b16c0170SJoerg Roedel 	if (ret)
885baf94e6eSYong Wu 		goto out_link_remove;
886b16c0170SJoerg Roedel 
8872d471b20SRobin Murphy 	ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
888b16c0170SJoerg Roedel 	if (ret)
889986d9ec5SYong Wu 		goto out_sysfs_remove;
890b16c0170SJoerg Roedel 
891da3cc91bSYong Wu 	spin_lock_init(&data->tlb_lock);
8927c3a2ec0SYong Wu 	list_add_tail(&data->list, &m4ulist);
8937c3a2ec0SYong Wu 
894986d9ec5SYong Wu 	if (!iommu_present(&platform_bus_type)) {
895986d9ec5SYong Wu 		ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
896986d9ec5SYong Wu 		if (ret)
897986d9ec5SYong Wu 			goto out_list_del;
898986d9ec5SYong Wu 	}
8990df4fabeSYong Wu 
900986d9ec5SYong Wu 	ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
901986d9ec5SYong Wu 	if (ret)
902986d9ec5SYong Wu 		goto out_bus_set_null;
903986d9ec5SYong Wu 	return ret;
904986d9ec5SYong Wu 
905986d9ec5SYong Wu out_bus_set_null:
906986d9ec5SYong Wu 	bus_set_iommu(&platform_bus_type, NULL);
907986d9ec5SYong Wu out_list_del:
908986d9ec5SYong Wu 	list_del(&data->list);
909986d9ec5SYong Wu 	iommu_device_unregister(&data->iommu);
910986d9ec5SYong Wu out_sysfs_remove:
911986d9ec5SYong Wu 	iommu_device_sysfs_remove(&data->iommu);
912baf94e6eSYong Wu out_link_remove:
913baf94e6eSYong Wu 	device_link_remove(data->smicomm_dev, dev);
914c0b57581SYong Wu out_runtime_disable:
915c0b57581SYong Wu 	pm_runtime_disable(dev);
916986d9ec5SYong Wu 	return ret;
9170df4fabeSYong Wu }
9180df4fabeSYong Wu 
9190df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
9200df4fabeSYong Wu {
9210df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
9220df4fabeSYong Wu 
923b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
924b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
925b16c0170SJoerg Roedel 
9260df4fabeSYong Wu 	if (iommu_present(&platform_bus_type))
9270df4fabeSYong Wu 		bus_set_iommu(&platform_bus_type, NULL);
9280df4fabeSYong Wu 
9290df4fabeSYong Wu 	clk_disable_unprepare(data->bclk);
930baf94e6eSYong Wu 	device_link_remove(data->smicomm_dev, &pdev->dev);
931c0b57581SYong Wu 	pm_runtime_disable(&pdev->dev);
9320df4fabeSYong Wu 	devm_free_irq(&pdev->dev, data->irq, data);
9330df4fabeSYong Wu 	component_master_del(&pdev->dev, &mtk_iommu_com_ops);
9340df4fabeSYong Wu 	return 0;
9350df4fabeSYong Wu }
9360df4fabeSYong Wu 
93734665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
9380df4fabeSYong Wu {
9390df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
9400df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
9410df4fabeSYong Wu 	void __iomem *base = data->base;
9420df4fabeSYong Wu 
94335c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
94475eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
9450df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
9460df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
9470df4fabeSYong Wu 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
9480df4fabeSYong Wu 	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
94970ca608bSYong Wu 	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
950b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
9516254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
9520df4fabeSYong Wu 	return 0;
9530df4fabeSYong Wu }
9540df4fabeSYong Wu 
95534665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
9560df4fabeSYong Wu {
9570df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
9580df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
959907ba6a1SYong Wu 	struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
9600df4fabeSYong Wu 	void __iomem *base = data->base;
9616254b64fSYong Wu 	int ret;
9620df4fabeSYong Wu 
9636254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
9646254b64fSYong Wu 	if (ret) {
9656254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
9666254b64fSYong Wu 		return ret;
9676254b64fSYong Wu 	}
968b34ea31fSDafna Hirschfeld 
969b34ea31fSDafna Hirschfeld 	/*
970b34ea31fSDafna Hirschfeld 	 * Uppon first resume, only enable the clk and return, since the values of the
971b34ea31fSDafna Hirschfeld 	 * registers are not yet set.
972b34ea31fSDafna Hirschfeld 	 */
973b34ea31fSDafna Hirschfeld 	if (!m4u_dom)
974b34ea31fSDafna Hirschfeld 		return 0;
975b34ea31fSDafna Hirschfeld 
97635c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
97775eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
9780df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
9790df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
9800df4fabeSYong Wu 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
9810df4fabeSYong Wu 	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
98270ca608bSYong Wu 	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
983b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
984c0b57581SYong Wu 	writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, base + REG_MMU_PT_BASE_ADDR);
9850df4fabeSYong Wu 	return 0;
9860df4fabeSYong Wu }
9870df4fabeSYong Wu 
988e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
98934665c79SYong Wu 	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
99034665c79SYong Wu 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
99134665c79SYong Wu 				     pm_runtime_force_resume)
9920df4fabeSYong Wu };
9930df4fabeSYong Wu 
994cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
995cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
9966b717796SChao Hao 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG,
997b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
998585e58f4SYong Wu 	.iova_region  = single_domain,
999585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
100037276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
1001cecdce9dSYong Wu };
1002cecdce9dSYong Wu 
1003068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
1004068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
1005068c86e9SChao Hao 	.flags         = HAS_SUB_COMM | OUT_ORDER_WR_EN | WR_THROT_EN,
1006068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
1007585e58f4SYong Wu 	.iova_region   = single_domain,
1008585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
1009068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
1010cecdce9dSYong Wu };
1011cecdce9dSYong Wu 
10123c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
10133c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
10143c213562SFabien Parent 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR,
10153c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1016585e58f4SYong Wu 	.iova_region  = single_domain,
1017585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
10183c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
10193c213562SFabien Parent };
10203c213562SFabien Parent 
1021cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
1022cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
1023d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
1024d1b5ef00SFabien Parent 			HAS_LEGACY_IVRP_PADDR,
1025b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1026585e58f4SYong Wu 	.iova_region  = single_domain,
1027585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
102837276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
1029cecdce9dSYong Wu };
1030cecdce9dSYong Wu 
1031907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
1032907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
10336b717796SChao Hao 	.flags        = RESET_AXI,
1034b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1035585e58f4SYong Wu 	.iova_region  = single_domain,
1036585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
103737276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1038907ba6a1SYong Wu };
1039907ba6a1SYong Wu 
10409e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = {
10419e3489e0SYong Wu 	.m4u_plat       = M4U_MT8192,
10429e3489e0SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM | OUT_ORDER_WR_EN |
10439e3489e0SYong Wu 			  WR_THROT_EN | IOVA_34_EN,
10449e3489e0SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
10459e3489e0SYong Wu 	.iova_region    = mt8192_multi_dom,
10469e3489e0SYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
10479e3489e0SYong Wu 	.larbid_remap   = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20},
10489e3489e0SYong Wu 			   {0, 14, 16}, {0, 13, 18, 17}},
10499e3489e0SYong Wu };
10509e3489e0SYong Wu 
10510df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
1052cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1053068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
10543c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1055cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1056907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
10579e3489e0SYong Wu 	{ .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data},
10580df4fabeSYong Wu 	{}
10590df4fabeSYong Wu };
10600df4fabeSYong Wu 
10610df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
10620df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
10630df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
10640df4fabeSYong Wu 	.driver	= {
10650df4fabeSYong Wu 		.name = "mtk-iommu",
1066f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
10670df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
10680df4fabeSYong Wu 	}
10690df4fabeSYong Wu };
107018d8c74eSYong Wu module_platform_driver(mtk_iommu_driver);
10710df4fabeSYong Wu 
107218d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations");
107318d8c74eSYong Wu MODULE_LICENSE("GPL v2");
1074