xref: /linux/drivers/iommu/mtk_iommu.c (revision f7b71d0d3ee7f83439f82af2d7e595ecd9b9b42c)
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
549a87005eSYong Wu #define F_MMU_DCM				BIT(8)
559a87005eSYong Wu 
5635c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5735c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
580df4fabeSYong Wu 
590df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
60acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
610df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
62acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
630df4fabeSYong Wu 
640df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6570ca608bSYong Wu 
6630e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6730e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
680df4fabeSYong Wu 
690df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
700df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
710df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
720df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
730df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
740df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
750df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
760df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
770df4fabeSYong Wu 
780df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
7915a01f4cSYong Wu 						/* mmu0 | mmu1 */
8015a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
8115a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
8215a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
8315a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8415a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8515a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8615a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
870df4fabeSYong Wu 
880df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
890df4fabeSYong Wu 
900df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
9115a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
9215a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
930df4fabeSYong Wu 
9415a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
95ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
96ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
97ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
980df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
990df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
1000df4fabeSYong Wu 
10115a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
10215a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
10315a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
10415a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
10515a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
10637276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10737276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
1089ec30c09SYong Wu #define F_MMU_INT_ID_COMM_ID_EXT(a)		(((a) >> 10) & 0x7)
1099ec30c09SYong Wu #define F_MMU_INT_ID_SUB_COMM_ID_EXT(a)		(((a) >> 7) & 0x7)
11015a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
11115a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
1120df4fabeSYong Wu 
113829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
1140df4fabeSYong Wu 
1156b717796SChao Hao #define HAS_4GB_MODE			BIT(0)
1166b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */
1176b717796SChao Hao #define HAS_BCLK			BIT(1)
1186b717796SChao Hao #define HAS_VLD_PA_RNG			BIT(2)
1196b717796SChao Hao #define RESET_AXI			BIT(3)
1204bb2bf4cSChao Hao #define OUT_ORDER_WR_EN			BIT(4)
1219ec30c09SYong Wu #define HAS_SUB_COMM_2BITS		BIT(5)
1229ec30c09SYong Wu #define HAS_SUB_COMM_3BITS		BIT(6)
1239ec30c09SYong Wu #define WR_THROT_EN			BIT(7)
1249ec30c09SYong Wu #define HAS_LEGACY_IVRP_PADDR		BIT(8)
1259ec30c09SYong Wu #define IOVA_34_EN			BIT(9)
1269ec30c09SYong Wu #define SHARE_PGTABLE			BIT(10) /* 2 HW share pgtable */
1279ec30c09SYong Wu #define DCM_DISABLE			BIT(11)
1289ec30c09SYong Wu #define STD_AXI_MODE			BIT(12) /* For non MM iommu */
1298cd1e619SYong Wu /* 2 bits: iommu type */
1308cd1e619SYong Wu #define MTK_IOMMU_TYPE_MM		(0x0 << 13)
1318cd1e619SYong Wu #define MTK_IOMMU_TYPE_INFRA		(0x1 << 13)
1328cd1e619SYong Wu #define MTK_IOMMU_TYPE_MASK		(0x3 << 13)
1336b717796SChao Hao 
1348cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask)	\
1358cd1e619SYong Wu 				((((pdata)->flags) & (mask)) == (_x))
1368cd1e619SYong Wu 
1378cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, _x)
1388cd1e619SYong Wu #define MTK_IOMMU_IS_TYPE(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x,\
1398cd1e619SYong Wu 							MTK_IOMMU_TYPE_MASK)
1406b717796SChao Hao 
141d2e9a110SYong Wu #define MTK_INVALID_LARBID		MTK_LARB_NR_MAX
142d2e9a110SYong Wu 
1430df4fabeSYong Wu struct mtk_iommu_domain {
1440df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
1450df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
1460df4fabeSYong Wu 
14708500c43SYong Wu 	struct mtk_iommu_data		*data;
1480df4fabeSYong Wu 	struct iommu_domain		domain;
149ddf67a87SYong Wu 
150ddf67a87SYong Wu 	struct mutex			mutex; /* Protect "data" in this structure */
1510df4fabeSYong Wu };
1520df4fabeSYong Wu 
153b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
1540df4fabeSYong Wu 
1557f37a91dSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data);
1567f37a91dSYong Wu 
157bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({					\
158bfed8731SYong Wu 	dma_addr_t _addr = iova;					\
159bfed8731SYong Wu 	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
160bfed8731SYong Wu })
161bfed8731SYong Wu 
16276ce6546SYong Wu /*
16376ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
16476ce6546SYong Wu  *
16576ce6546SYong Wu  * CPU Physical address:
16676ce6546SYong Wu  * ====================
16776ce6546SYong Wu  *
16876ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
16976ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
17076ce6546SYong Wu  * +--I/O--+------------Memory-------------+
17176ce6546SYong Wu  *
17276ce6546SYong Wu  * IOMMU output physical address:
17376ce6546SYong Wu  *  =============================
17476ce6546SYong Wu  *
17576ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
17676ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
17776ce6546SYong Wu  *                                 +------------Memory-------------+
17876ce6546SYong Wu  *
17976ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
18076ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
18176ce6546SYong Wu  * 'E', the CPU physical address keep as is.
18276ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
18376ce6546SYong Wu  */
184b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
18576ce6546SYong Wu 
1867c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
1877c3a2ec0SYong Wu 
1889e3a2a64SYong Wu #define for_each_m4u(data, head)  list_for_each_entry(data, head, list)
1897c3a2ec0SYong Wu 
190585e58f4SYong Wu struct mtk_iommu_iova_region {
191585e58f4SYong Wu 	dma_addr_t		iova_base;
192585e58f4SYong Wu 	unsigned long long	size;
193585e58f4SYong Wu };
194585e58f4SYong Wu 
195585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = {
196585e58f4SYong Wu 	{.iova_base = 0,		.size = SZ_4G},
197585e58f4SYong Wu };
198585e58f4SYong Wu 
1999e3489e0SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[] = {
200129a3b88SYong Wu 	{ .iova_base = 0x0,		.size = SZ_4G},		/* 0 ~ 4G */
2019e3489e0SYong Wu 	#if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)
202129a3b88SYong Wu 	{ .iova_base = SZ_4G,		.size = SZ_4G},		/* 4G ~ 8G */
203129a3b88SYong Wu 	{ .iova_base = SZ_4G * 2,	.size = SZ_4G},		/* 8G ~ 12G */
204129a3b88SYong Wu 	{ .iova_base = SZ_4G * 3,	.size = SZ_4G},		/* 12G ~ 16G */
205129a3b88SYong Wu 
2069e3489e0SYong Wu 	{ .iova_base = 0x240000000ULL,	.size = 0x4000000},	/* CCU0 */
2079e3489e0SYong Wu 	{ .iova_base = 0x244000000ULL,	.size = 0x4000000},	/* CCU1 */
2089e3489e0SYong Wu 	#endif
2099e3489e0SYong Wu };
2109e3489e0SYong Wu 
2119e3a2a64SYong Wu /* If 2 M4U share a domain(use the same hwlist), Put the corresponding info in first data.*/
2129e3a2a64SYong Wu static struct mtk_iommu_data *mtk_iommu_get_frst_data(struct list_head *hwlist)
2137c3a2ec0SYong Wu {
2149e3a2a64SYong Wu 	return list_first_entry(hwlist, struct mtk_iommu_data, list);
2157c3a2ec0SYong Wu }
2167c3a2ec0SYong Wu 
2170df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
2180df4fabeSYong Wu {
2190df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
2200df4fabeSYong Wu }
2210df4fabeSYong Wu 
2220954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
2230df4fabeSYong Wu {
22415672b6dSYong Wu 	unsigned long flags;
225c0b57581SYong Wu 
22615672b6dSYong Wu 	spin_lock_irqsave(&data->tlb_lock, flags);
2277c3a2ec0SYong Wu 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
228b053bc71SChao Hao 		       data->base + data->plat_data->inv_sel_reg);
2290df4fabeSYong Wu 	writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
2300df4fabeSYong Wu 	wmb(); /* Make sure the tlb flush all done */
23115672b6dSYong Wu 	spin_unlock_irqrestore(&data->tlb_lock, flags);
2327c3a2ec0SYong Wu }
2330df4fabeSYong Wu 
2341f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
2350954d61aSYong Wu 					   struct mtk_iommu_data *data)
2360df4fabeSYong Wu {
2379e3a2a64SYong Wu 	struct list_head *head = data->hw_list;
2381f4fd624SYong Wu 	unsigned long flags;
2391f4fd624SYong Wu 	int ret;
2401f4fd624SYong Wu 	u32 tmp;
2410df4fabeSYong Wu 
2429e3a2a64SYong Wu 	for_each_m4u(data, head) {
243c0b57581SYong Wu 		if (pm_runtime_get_if_in_use(data->dev) <= 0)
244c0b57581SYong Wu 			continue;
245c0b57581SYong Wu 
2461f4fd624SYong Wu 		spin_lock_irqsave(&data->tlb_lock, flags);
2477c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
248b053bc71SChao Hao 			       data->base + data->plat_data->inv_sel_reg);
2490df4fabeSYong Wu 
250bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova),
251bfed8731SYong Wu 			       data->base + REG_MMU_INVLD_START_A);
252bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
2537c3a2ec0SYong Wu 			       data->base + REG_MMU_INVLD_END_A);
2547c3a2ec0SYong Wu 		writel_relaxed(F_MMU_INV_RANGE,
2557c3a2ec0SYong Wu 			       data->base + REG_MMU_INVALIDATE);
2560df4fabeSYong Wu 
2571f4fd624SYong Wu 		/* tlb sync */
2587c3a2ec0SYong Wu 		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
259c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
26015672b6dSYong Wu 
26115672b6dSYong Wu 		/* Clear the CPE status */
26215672b6dSYong Wu 		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
26315672b6dSYong Wu 		spin_unlock_irqrestore(&data->tlb_lock, flags);
26415672b6dSYong Wu 
2650df4fabeSYong Wu 		if (ret) {
2660df4fabeSYong Wu 			dev_warn(data->dev,
2670df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
2680954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
2690df4fabeSYong Wu 		}
270c0b57581SYong Wu 
271c0b57581SYong Wu 		pm_runtime_put(data->dev);
2720df4fabeSYong Wu 	}
2737c3a2ec0SYong Wu }
2740df4fabeSYong Wu 
2750df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
2760df4fabeSYong Wu {
2770df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_id;
2780df4fabeSYong Wu 	struct mtk_iommu_domain *dom = data->m4u_dom;
279d2e9a110SYong Wu 	unsigned int fault_larb = MTK_INVALID_LARBID, fault_port = 0, sub_comm = 0;
280ef0f0986SYong Wu 	u32 int_state, regval, va34_32, pa34_32;
281ef0f0986SYong Wu 	u64 fault_iova, fault_pa;
2820df4fabeSYong Wu 	bool layer, write;
2830df4fabeSYong Wu 
2840df4fabeSYong Wu 	/* Read error info from registers */
2850df4fabeSYong Wu 	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
28615a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
28715a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
28815a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
28915a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
29015a01f4cSYong Wu 	} else {
29115a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
29215a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
29315a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
29415a01f4cSYong Wu 	}
2950df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
2960df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
297ef0f0986SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN)) {
298ef0f0986SYong Wu 		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
299ef0f0986SYong Wu 		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
300ef0f0986SYong Wu 		fault_iova |= (u64)va34_32 << 32;
301ef0f0986SYong Wu 	}
30282e51771SYong Wu 	pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
30382e51771SYong Wu 	fault_pa |= (u64)pa34_32 << 32;
304ef0f0986SYong Wu 
305d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
30615a01f4cSYong Wu 		fault_port = F_MMU_INT_ID_PORT_ID(regval);
3079ec30c09SYong Wu 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM_2BITS)) {
30837276e00SChao Hao 			fault_larb = F_MMU_INT_ID_COMM_ID(regval);
30937276e00SChao Hao 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
3109ec30c09SYong Wu 		} else if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM_3BITS)) {
3119ec30c09SYong Wu 			fault_larb = F_MMU_INT_ID_COMM_ID_EXT(regval);
3129ec30c09SYong Wu 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID_EXT(regval);
31337276e00SChao Hao 		} else {
31437276e00SChao Hao 			fault_larb = F_MMU_INT_ID_LARB_ID(regval);
31537276e00SChao Hao 		}
31637276e00SChao Hao 		fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
317d2e9a110SYong Wu 	}
318b3e5eee7SYong Wu 
3190df4fabeSYong Wu 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
3200df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
3210df4fabeSYong Wu 		dev_err_ratelimited(
3220df4fabeSYong Wu 			data->dev,
323ef0f0986SYong Wu 			"fault type=0x%x iova=0x%llx pa=0x%llx larb=%d port=%d layer=%d %s\n",
3240df4fabeSYong Wu 			int_state, fault_iova, fault_pa, fault_larb, fault_port,
3250df4fabeSYong Wu 			layer, write ? "write" : "read");
3260df4fabeSYong Wu 	}
3270df4fabeSYong Wu 
3280df4fabeSYong Wu 	/* Interrupt clear */
3290df4fabeSYong Wu 	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
3300df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
3310df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
3320df4fabeSYong Wu 
3330df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
3340df4fabeSYong Wu 
3350df4fabeSYong Wu 	return IRQ_HANDLED;
3360df4fabeSYong Wu }
3370df4fabeSYong Wu 
338803cf9e5SYong Wu static int mtk_iommu_get_domain_id(struct device *dev,
339803cf9e5SYong Wu 				   const struct mtk_iommu_plat_data *plat_data)
340803cf9e5SYong Wu {
341803cf9e5SYong Wu 	const struct mtk_iommu_iova_region *rgn = plat_data->iova_region;
342803cf9e5SYong Wu 	const struct bus_dma_region *dma_rgn = dev->dma_range_map;
343803cf9e5SYong Wu 	int i, candidate = -1;
344803cf9e5SYong Wu 	dma_addr_t dma_end;
345803cf9e5SYong Wu 
346803cf9e5SYong Wu 	if (!dma_rgn || plat_data->iova_region_nr == 1)
347803cf9e5SYong Wu 		return 0;
348803cf9e5SYong Wu 
349803cf9e5SYong Wu 	dma_end = dma_rgn->dma_start + dma_rgn->size - 1;
350803cf9e5SYong Wu 	for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) {
351803cf9e5SYong Wu 		/* Best fit. */
352803cf9e5SYong Wu 		if (dma_rgn->dma_start == rgn->iova_base &&
353803cf9e5SYong Wu 		    dma_end == rgn->iova_base + rgn->size - 1)
354803cf9e5SYong Wu 			return i;
355803cf9e5SYong Wu 		/* ok if it is inside this region. */
356803cf9e5SYong Wu 		if (dma_rgn->dma_start >= rgn->iova_base &&
357803cf9e5SYong Wu 		    dma_end < rgn->iova_base + rgn->size)
358803cf9e5SYong Wu 			candidate = i;
359803cf9e5SYong Wu 	}
360803cf9e5SYong Wu 
361803cf9e5SYong Wu 	if (candidate >= 0)
362803cf9e5SYong Wu 		return candidate;
363803cf9e5SYong Wu 	dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n",
364803cf9e5SYong Wu 		&dma_rgn->dma_start, dma_rgn->size);
365803cf9e5SYong Wu 	return -EINVAL;
366803cf9e5SYong Wu }
367803cf9e5SYong Wu 
3688d2c749eSYong Wu static void mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev,
3698d2c749eSYong Wu 			     bool enable, unsigned int domid)
3700df4fabeSYong Wu {
3710df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
3720df4fabeSYong Wu 	unsigned int                 larbid, portid;
373a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3748d2c749eSYong Wu 	const struct mtk_iommu_iova_region *region;
37558f0d1d5SRobin Murphy 	int i;
3760df4fabeSYong Wu 
37758f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
37858f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
37958f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
3808d2c749eSYong Wu 
381d2e9a110SYong Wu 		if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
3821ee9feb2SYong Wu 			larb_mmu = &data->larb_imu[larbid];
3830df4fabeSYong Wu 
3848d2c749eSYong Wu 			region = data->plat_data->iova_region + domid;
3858d2c749eSYong Wu 			larb_mmu->bank[portid] = upper_32_bits(region->iova_base);
3868d2c749eSYong Wu 
3878d2c749eSYong Wu 			dev_dbg(dev, "%s iommu for larb(%s) port %d dom %d bank %d.\n",
3888d2c749eSYong Wu 				enable ? "enable" : "disable", dev_name(larb_mmu->dev),
3898d2c749eSYong Wu 				portid, domid, larb_mmu->bank[portid]);
3900df4fabeSYong Wu 
3910df4fabeSYong Wu 			if (enable)
3920df4fabeSYong Wu 				larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
3930df4fabeSYong Wu 			else
3940df4fabeSYong Wu 				larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
3950df4fabeSYong Wu 		}
3960df4fabeSYong Wu 	}
397d2e9a110SYong Wu }
3980df4fabeSYong Wu 
3994f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
400c3045f39SYong Wu 				     struct mtk_iommu_data *data,
401c3045f39SYong Wu 				     unsigned int domid)
4020df4fabeSYong Wu {
403c3045f39SYong Wu 	const struct mtk_iommu_iova_region *region;
404c3045f39SYong Wu 
405c3045f39SYong Wu 	/* Use the exist domain as there is only one pgtable here. */
406c3045f39SYong Wu 	if (data->m4u_dom) {
407c3045f39SYong Wu 		dom->iop = data->m4u_dom->iop;
408c3045f39SYong Wu 		dom->cfg = data->m4u_dom->cfg;
409c3045f39SYong Wu 		dom->domain.pgsize_bitmap = data->m4u_dom->cfg.pgsize_bitmap;
410c3045f39SYong Wu 		goto update_iova_region;
411c3045f39SYong Wu 	}
412c3045f39SYong Wu 
4130df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
4140df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
4150df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
416b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
4170df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
4182f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
4190df4fabeSYong Wu 		.iommu_dev = data->dev,
4200df4fabeSYong Wu 	};
4210df4fabeSYong Wu 
4229bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
4239bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
4249bdfe4c1SYong Wu 	else
4259bdfe4c1SYong Wu 		dom->cfg.oas = 35;
4269bdfe4c1SYong Wu 
4270df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
4280df4fabeSYong Wu 	if (!dom->iop) {
4290df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
4300df4fabeSYong Wu 		return -EINVAL;
4310df4fabeSYong Wu 	}
4320df4fabeSYong Wu 
4330df4fabeSYong Wu 	/* Update our support page sizes bitmap */
434d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
435b7875eb9SYong Wu 
436c3045f39SYong Wu update_iova_region:
437c3045f39SYong Wu 	/* Update the iova region for this domain */
438c3045f39SYong Wu 	region = data->plat_data->iova_region + domid;
439c3045f39SYong Wu 	dom->domain.geometry.aperture_start = region->iova_base;
440c3045f39SYong Wu 	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
441b7875eb9SYong Wu 	dom->domain.geometry.force_aperture = true;
4420df4fabeSYong Wu 	return 0;
4430df4fabeSYong Wu }
4440df4fabeSYong Wu 
4450df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
4460df4fabeSYong Wu {
4470df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
4480df4fabeSYong Wu 
4490df4fabeSYong Wu 	if (type != IOMMU_DOMAIN_DMA)
4500df4fabeSYong Wu 		return NULL;
4510df4fabeSYong Wu 
4520df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
4530df4fabeSYong Wu 	if (!dom)
4540df4fabeSYong Wu 		return NULL;
455ddf67a87SYong Wu 	mutex_init(&dom->mutex);
4560df4fabeSYong Wu 
4574f956c97SYong Wu 	return &dom->domain;
4584f956c97SYong Wu }
4594f956c97SYong Wu 
4600df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
4610df4fabeSYong Wu {
4620df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
4630df4fabeSYong Wu }
4640df4fabeSYong Wu 
4650df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
4660df4fabeSYong Wu 				   struct device *dev)
4670df4fabeSYong Wu {
468645b87c1SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev), *frstdata;
4690df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
4709e3a2a64SYong Wu 	struct list_head *hw_list = data->hw_list;
471c0b57581SYong Wu 	struct device *m4udev = data->dev;
472803cf9e5SYong Wu 	int ret, domid;
4730df4fabeSYong Wu 
474803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
475803cf9e5SYong Wu 	if (domid < 0)
476803cf9e5SYong Wu 		return domid;
477803cf9e5SYong Wu 
478ddf67a87SYong Wu 	mutex_lock(&dom->mutex);
4794f956c97SYong Wu 	if (!dom->data) {
480645b87c1SYong Wu 		/* Data is in the frstdata in sharing pgtable case. */
4819e3a2a64SYong Wu 		frstdata = mtk_iommu_get_frst_data(hw_list);
482645b87c1SYong Wu 
483ddf67a87SYong Wu 		ret = mtk_iommu_domain_finalise(dom, frstdata, domid);
484ddf67a87SYong Wu 		if (ret) {
485ddf67a87SYong Wu 			mutex_unlock(&dom->mutex);
4864f956c97SYong Wu 			return -ENODEV;
487ddf67a87SYong Wu 		}
4884f956c97SYong Wu 		dom->data = data;
4894f956c97SYong Wu 	}
490ddf67a87SYong Wu 	mutex_unlock(&dom->mutex);
4914f956c97SYong Wu 
4920e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
4937f37a91dSYong Wu 	if (!data->m4u_dom) { /* Initialize the M4U HW */
494c0b57581SYong Wu 		ret = pm_runtime_resume_and_get(m4udev);
495c0b57581SYong Wu 		if (ret < 0)
4960e5a3f2eSYong Wu 			goto err_unlock;
497c0b57581SYong Wu 
498c0b57581SYong Wu 		ret = mtk_iommu_hw_init(data);
499c0b57581SYong Wu 		if (ret) {
500c0b57581SYong Wu 			pm_runtime_put(m4udev);
5010e5a3f2eSYong Wu 			goto err_unlock;
502c0b57581SYong Wu 		}
5030df4fabeSYong Wu 		data->m4u_dom = dom;
504d1e5f26fSRobin Murphy 		writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
5054b00f5acSYong Wu 		       data->base + REG_MMU_PT_BASE_ADDR);
506c0b57581SYong Wu 
507c0b57581SYong Wu 		pm_runtime_put(m4udev);
5080df4fabeSYong Wu 	}
5090e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
5100df4fabeSYong Wu 
5118d2c749eSYong Wu 	mtk_iommu_config(data, dev, true, domid);
5120df4fabeSYong Wu 	return 0;
5130e5a3f2eSYong Wu 
5140e5a3f2eSYong Wu err_unlock:
5150e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
5160e5a3f2eSYong Wu 	return ret;
5170df4fabeSYong Wu }
5180df4fabeSYong Wu 
5190df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
5200df4fabeSYong Wu 				    struct device *dev)
5210df4fabeSYong Wu {
5223524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
5230df4fabeSYong Wu 
5248d2c749eSYong Wu 	mtk_iommu_config(data, dev, false, 0);
5250df4fabeSYong Wu }
5260df4fabeSYong Wu 
5270df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
528781ca2deSTom Murphy 			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
5290df4fabeSYong Wu {
5300df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5310df4fabeSYong Wu 
532b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
53308500c43SYong Wu 	if (dom->data->enable_4GB)
534b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
535b4dad40eSYong Wu 
53660829b4dSYong Wu 	/* Synchronize with the tlb_lock */
537f34ce7a7SBaolin Wang 	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
5380df4fabeSYong Wu }
5390df4fabeSYong Wu 
5400df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
54156f8af5eSWill Deacon 			      unsigned long iova, size_t size,
54256f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
5430df4fabeSYong Wu {
5440df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5450df4fabeSYong Wu 
5463136895cSRobin Murphy 	iommu_iotlb_gather_add_range(gather, iova, size);
54760829b4dSYong Wu 	return dom->iop->unmap(dom->iop, iova, size, gather);
5480df4fabeSYong Wu }
5490df4fabeSYong Wu 
55056f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
55156f8af5eSWill Deacon {
55208500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
55308500c43SYong Wu 
55408500c43SYong Wu 	mtk_iommu_tlb_flush_all(dom->data);
55556f8af5eSWill Deacon }
55656f8af5eSWill Deacon 
55756f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
55856f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
5594d689b61SRobin Murphy {
56008500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
561862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
562da3cc91bSYong Wu 
563e6d25e7dSYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, dom->data);
5644d689b61SRobin Murphy }
5654d689b61SRobin Murphy 
56620143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
56720143451SYong Wu 			       size_t size)
56820143451SYong Wu {
56908500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
57020143451SYong Wu 
571e6d25e7dSYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, dom->data);
57220143451SYong Wu }
57320143451SYong Wu 
5740df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
5750df4fabeSYong Wu 					  dma_addr_t iova)
5760df4fabeSYong Wu {
5770df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5780df4fabeSYong Wu 	phys_addr_t pa;
5790df4fabeSYong Wu 
5800df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
581f13efafcSArnd Bergmann 	if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
582f13efafcSArnd Bergmann 	    dom->data->enable_4GB &&
583f13efafcSArnd Bergmann 	    pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
584b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
58530e2fccfSYong Wu 
5860df4fabeSYong Wu 	return pa;
5870df4fabeSYong Wu }
5880df4fabeSYong Wu 
58980e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
5900df4fabeSYong Wu {
591a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
592b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
593635319a4SYong Wu 	struct device_link *link;
594635319a4SYong Wu 	struct device *larbdev;
595635319a4SYong Wu 	unsigned int larbid, larbidx, i;
5960df4fabeSYong Wu 
597a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
59880e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
5990df4fabeSYong Wu 
6003524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
601b16c0170SJoerg Roedel 
602d2e9a110SYong Wu 	if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
603d2e9a110SYong Wu 		return &data->iommu;
604d2e9a110SYong Wu 
605635319a4SYong Wu 	/*
606635319a4SYong Wu 	 * Link the consumer device with the smi-larb device(supplier).
607635319a4SYong Wu 	 * The device that connects with each a larb is a independent HW.
608635319a4SYong Wu 	 * All the ports in each a device should be in the same larbs.
609635319a4SYong Wu 	 */
610635319a4SYong Wu 	larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
611635319a4SYong Wu 	for (i = 1; i < fwspec->num_ids; i++) {
612635319a4SYong Wu 		larbidx = MTK_M4U_TO_LARB(fwspec->ids[i]);
613635319a4SYong Wu 		if (larbid != larbidx) {
614635319a4SYong Wu 			dev_err(dev, "Can only use one larb. Fail@larb%d-%d.\n",
615635319a4SYong Wu 				larbid, larbidx);
616635319a4SYong Wu 			return ERR_PTR(-EINVAL);
617635319a4SYong Wu 		}
618635319a4SYong Wu 	}
619635319a4SYong Wu 	larbdev = data->larb_imu[larbid].dev;
620635319a4SYong Wu 	link = device_link_add(dev, larbdev,
621635319a4SYong Wu 			       DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
622635319a4SYong Wu 	if (!link)
623635319a4SYong Wu 		dev_err(dev, "Unable to link %s\n", dev_name(larbdev));
62480e4592aSJoerg Roedel 	return &data->iommu;
6250df4fabeSYong Wu }
6260df4fabeSYong Wu 
62780e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
6280df4fabeSYong Wu {
629a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
630635319a4SYong Wu 	struct mtk_iommu_data *data;
631635319a4SYong Wu 	struct device *larbdev;
632635319a4SYong Wu 	unsigned int larbid;
633b16c0170SJoerg Roedel 
634a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
6350df4fabeSYong Wu 		return;
6360df4fabeSYong Wu 
637635319a4SYong Wu 	data = dev_iommu_priv_get(dev);
638d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
639635319a4SYong Wu 		larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
640635319a4SYong Wu 		larbdev = data->larb_imu[larbid].dev;
641635319a4SYong Wu 		device_link_remove(dev, larbdev);
642d2e9a110SYong Wu 	}
643635319a4SYong Wu 
64458f0d1d5SRobin Murphy 	iommu_fwspec_free(dev);
6450df4fabeSYong Wu }
6460df4fabeSYong Wu 
6470df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
6480df4fabeSYong Wu {
6499e3a2a64SYong Wu 	struct mtk_iommu_data *c_data = dev_iommu_priv_get(dev), *data;
6509e3a2a64SYong Wu 	struct list_head *hw_list = c_data->hw_list;
651c3045f39SYong Wu 	struct iommu_group *group;
652803cf9e5SYong Wu 	int domid;
6530df4fabeSYong Wu 
6549e3a2a64SYong Wu 	data = mtk_iommu_get_frst_data(hw_list);
65558f0d1d5SRobin Murphy 	if (!data)
6560df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
6570df4fabeSYong Wu 
658803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
659803cf9e5SYong Wu 	if (domid < 0)
660803cf9e5SYong Wu 		return ERR_PTR(domid);
661803cf9e5SYong Wu 
6620e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
663c3045f39SYong Wu 	group = data->m4u_group[domid];
664c3045f39SYong Wu 	if (!group) {
665c3045f39SYong Wu 		group = iommu_group_alloc();
666c3045f39SYong Wu 		if (!IS_ERR(group))
667c3045f39SYong Wu 			data->m4u_group[domid] = group;
6683a8d40b6SRobin Murphy 	} else {
669c3045f39SYong Wu 		iommu_group_ref_get(group);
6700df4fabeSYong Wu 	}
6710e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
672c3045f39SYong Wu 	return group;
6730df4fabeSYong Wu }
6740df4fabeSYong Wu 
6750df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
6760df4fabeSYong Wu {
6770df4fabeSYong Wu 	struct platform_device *m4updev;
6780df4fabeSYong Wu 
6790df4fabeSYong Wu 	if (args->args_count != 1) {
6800df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
6810df4fabeSYong Wu 			args->args_count);
6820df4fabeSYong Wu 		return -EINVAL;
6830df4fabeSYong Wu 	}
6840df4fabeSYong Wu 
6853524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
6860df4fabeSYong Wu 		/* Get the m4u device */
6870df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
6880df4fabeSYong Wu 		if (WARN_ON(!m4updev))
6890df4fabeSYong Wu 			return -EINVAL;
6900df4fabeSYong Wu 
6913524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
6920df4fabeSYong Wu 	}
6930df4fabeSYong Wu 
69458f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
6950df4fabeSYong Wu }
6960df4fabeSYong Wu 
697ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev,
698ab1d5281SYong Wu 				       struct list_head *head)
699ab1d5281SYong Wu {
700ab1d5281SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
701ab1d5281SYong Wu 	unsigned int domid = mtk_iommu_get_domain_id(dev, data->plat_data), i;
702ab1d5281SYong Wu 	const struct mtk_iommu_iova_region *resv, *curdom;
703ab1d5281SYong Wu 	struct iommu_resv_region *region;
704ab1d5281SYong Wu 	int prot = IOMMU_WRITE | IOMMU_READ;
705ab1d5281SYong Wu 
7067a566173SColin Ian King 	if ((int)domid < 0)
707ab1d5281SYong Wu 		return;
708ab1d5281SYong Wu 	curdom = data->plat_data->iova_region + domid;
709ab1d5281SYong Wu 	for (i = 0; i < data->plat_data->iova_region_nr; i++) {
710ab1d5281SYong Wu 		resv = data->plat_data->iova_region + i;
711ab1d5281SYong Wu 
712ab1d5281SYong Wu 		/* Only reserve when the region is inside the current domain */
713ab1d5281SYong Wu 		if (resv->iova_base <= curdom->iova_base ||
714ab1d5281SYong Wu 		    resv->iova_base + resv->size >= curdom->iova_base + curdom->size)
715ab1d5281SYong Wu 			continue;
716ab1d5281SYong Wu 
717ab1d5281SYong Wu 		region = iommu_alloc_resv_region(resv->iova_base, resv->size,
718ab1d5281SYong Wu 						 prot, IOMMU_RESV_RESERVED);
719ab1d5281SYong Wu 		if (!region)
720ab1d5281SYong Wu 			return;
721ab1d5281SYong Wu 
722ab1d5281SYong Wu 		list_add_tail(&region->list, head);
723ab1d5281SYong Wu 	}
724ab1d5281SYong Wu }
725ab1d5281SYong Wu 
726b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
7270df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
72880e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
72980e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
7300df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
7310df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
732ab1d5281SYong Wu 	.get_resv_regions = mtk_iommu_get_resv_regions,
733ab1d5281SYong Wu 	.put_resv_regions = generic_iommu_put_resv_regions,
7340df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
73518d8c74eSYong Wu 	.owner		= THIS_MODULE,
7369a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
7379a630a4bSLu Baolu 		.attach_dev	= mtk_iommu_attach_device,
7389a630a4bSLu Baolu 		.detach_dev	= mtk_iommu_detach_device,
7399a630a4bSLu Baolu 		.map		= mtk_iommu_map,
7409a630a4bSLu Baolu 		.unmap		= mtk_iommu_unmap,
7419a630a4bSLu Baolu 		.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
7429a630a4bSLu Baolu 		.iotlb_sync	= mtk_iommu_iotlb_sync,
7439a630a4bSLu Baolu 		.iotlb_sync_map	= mtk_iommu_sync_map,
7449a630a4bSLu Baolu 		.iova_to_phys	= mtk_iommu_iova_to_phys,
7459a630a4bSLu Baolu 		.free		= mtk_iommu_domain_free,
7469a630a4bSLu Baolu 	}
7470df4fabeSYong Wu };
7480df4fabeSYong Wu 
7490df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
7500df4fabeSYong Wu {
7510df4fabeSYong Wu 	u32 regval;
7520df4fabeSYong Wu 
75386444413SChao Hao 	if (data->plat_data->m4u_plat == M4U_MT8173) {
754acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
755acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
75686444413SChao Hao 	} else {
75786444413SChao Hao 		regval = readl_relaxed(data->base + REG_MMU_CTRL_REG);
75886444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
75986444413SChao Hao 	}
7600df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
7610df4fabeSYong Wu 
7620df4fabeSYong Wu 	regval = F_L2_MULIT_HIT_EN |
7630df4fabeSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
7640df4fabeSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
7650df4fabeSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
7660df4fabeSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
7670df4fabeSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
7680df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
7690df4fabeSYong Wu 
7700df4fabeSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
7710df4fabeSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
7720df4fabeSYong Wu 		F_INT_INVALID_PA_FAULT |
7730df4fabeSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
7740df4fabeSYong Wu 		F_INT_TLB_MISS_FAULT |
7750df4fabeSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
7760df4fabeSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
7770df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
7780df4fabeSYong Wu 
779d1b5ef00SFabien Parent 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
78070ca608bSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
78170ca608bSYong Wu 	else
78270ca608bSYong Wu 		regval = lower_32_bits(data->protect_base) |
78370ca608bSYong Wu 			 upper_32_bits(data->protect_base);
78470ca608bSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
78570ca608bSYong Wu 
7866b717796SChao Hao 	if (data->enable_4GB &&
7876b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
78830e2fccfSYong Wu 		/*
78930e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
79030e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
79130e2fccfSYong Wu 		 */
79230e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
79330e2fccfSYong Wu 		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
79430e2fccfSYong Wu 	}
7959a87005eSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, DCM_DISABLE))
7969a87005eSYong Wu 		writel_relaxed(F_MMU_DCM, data->base + REG_MMU_DCM_DIS);
7979a87005eSYong Wu 	else
7980df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
7999a87005eSYong Wu 
80035c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
80135c1b48dSChao Hao 		/* write command throttling mode */
80235c1b48dSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL);
80335c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
80435c1b48dSChao Hao 		writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL);
80535c1b48dSChao Hao 	}
806e6dec923SYong Wu 
8076b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
80875eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
8094bb2bf4cSChao Hao 		regval = 0;
8104bb2bf4cSChao Hao 	} else {
8114bb2bf4cSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
812d265a4adSYong Wu 		if (!MTK_IOMMU_HAS_FLAG(data->plat_data, STD_AXI_MODE))
8134bb2bf4cSChao Hao 			regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
8144bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
8154bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
81675eed350SChao Hao 	}
8174bb2bf4cSChao Hao 	writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
8180df4fabeSYong Wu 
8190df4fabeSYong Wu 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
8200df4fabeSYong Wu 			     dev_name(data->dev), (void *)data)) {
8210df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
8220df4fabeSYong Wu 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
8230df4fabeSYong Wu 		return -ENODEV;
8240df4fabeSYong Wu 	}
8250df4fabeSYong Wu 
8260df4fabeSYong Wu 	return 0;
8270df4fabeSYong Wu }
8280df4fabeSYong Wu 
8290df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
8300df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
8310df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
8320df4fabeSYong Wu };
8330df4fabeSYong Wu 
834d2e9a110SYong Wu static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **match,
835d2e9a110SYong Wu 				  struct mtk_iommu_data *data)
836d2e9a110SYong Wu {
837*f7b71d0dSYong Wu 	struct device_node *larbnode, *smicomm_node, *smi_subcomm_node;
838d2e9a110SYong Wu 	struct platform_device *plarbdev;
839d2e9a110SYong Wu 	struct device_link *link;
840d2e9a110SYong Wu 	int i, larb_nr, ret;
841d2e9a110SYong Wu 
842d2e9a110SYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL);
843d2e9a110SYong Wu 	if (larb_nr < 0)
844d2e9a110SYong Wu 		return larb_nr;
845d2e9a110SYong Wu 
846d2e9a110SYong Wu 	for (i = 0; i < larb_nr; i++) {
847d2e9a110SYong Wu 		u32 id;
848d2e9a110SYong Wu 
849d2e9a110SYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
850d2e9a110SYong Wu 		if (!larbnode)
851d2e9a110SYong Wu 			return -EINVAL;
852d2e9a110SYong Wu 
853d2e9a110SYong Wu 		if (!of_device_is_available(larbnode)) {
854d2e9a110SYong Wu 			of_node_put(larbnode);
855d2e9a110SYong Wu 			continue;
856d2e9a110SYong Wu 		}
857d2e9a110SYong Wu 
858d2e9a110SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
859d2e9a110SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
860d2e9a110SYong Wu 			id = i;
861d2e9a110SYong Wu 
862d2e9a110SYong Wu 		plarbdev = of_find_device_by_node(larbnode);
863d2e9a110SYong Wu 		if (!plarbdev) {
864d2e9a110SYong Wu 			of_node_put(larbnode);
865d2e9a110SYong Wu 			return -ENODEV;
866d2e9a110SYong Wu 		}
867d2e9a110SYong Wu 		if (!plarbdev->dev.driver) {
868d2e9a110SYong Wu 			of_node_put(larbnode);
869d2e9a110SYong Wu 			return -EPROBE_DEFER;
870d2e9a110SYong Wu 		}
871d2e9a110SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
872d2e9a110SYong Wu 
873d2e9a110SYong Wu 		component_match_add_release(dev, match, component_release_of,
874d2e9a110SYong Wu 					    component_compare_of, larbnode);
875d2e9a110SYong Wu 	}
876d2e9a110SYong Wu 
877*f7b71d0dSYong Wu 	/* Get smi-(sub)-common dev from the last larb. */
878*f7b71d0dSYong Wu 	smi_subcomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
879*f7b71d0dSYong Wu 	if (!smi_subcomm_node)
880d2e9a110SYong Wu 		return -EINVAL;
881d2e9a110SYong Wu 
882*f7b71d0dSYong Wu 	/*
883*f7b71d0dSYong Wu 	 * It may have two level smi-common. the node is smi-sub-common if it
884*f7b71d0dSYong Wu 	 * has a new mediatek,smi property. otherwise it is smi-commmon.
885*f7b71d0dSYong Wu 	 */
886*f7b71d0dSYong Wu 	smicomm_node = of_parse_phandle(smi_subcomm_node, "mediatek,smi", 0);
887*f7b71d0dSYong Wu 	if (smicomm_node)
888*f7b71d0dSYong Wu 		of_node_put(smi_subcomm_node);
889*f7b71d0dSYong Wu 	else
890*f7b71d0dSYong Wu 		smicomm_node = smi_subcomm_node;
891*f7b71d0dSYong Wu 
892d2e9a110SYong Wu 	plarbdev = of_find_device_by_node(smicomm_node);
893d2e9a110SYong Wu 	of_node_put(smicomm_node);
894d2e9a110SYong Wu 	data->smicomm_dev = &plarbdev->dev;
895d2e9a110SYong Wu 
896d2e9a110SYong Wu 	link = device_link_add(data->smicomm_dev, dev,
897d2e9a110SYong Wu 			       DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
898d2e9a110SYong Wu 	if (!link) {
899d2e9a110SYong Wu 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
900d2e9a110SYong Wu 		return -EINVAL;
901d2e9a110SYong Wu 	}
902d2e9a110SYong Wu 	return 0;
903d2e9a110SYong Wu }
904d2e9a110SYong Wu 
9050df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
9060df4fabeSYong Wu {
9070df4fabeSYong Wu 	struct mtk_iommu_data   *data;
9080df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
9090df4fabeSYong Wu 	struct resource         *res;
910b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
9110df4fabeSYong Wu 	struct component_match  *match = NULL;
912c2c59456SMiles Chen 	struct regmap		*infracfg;
9130df4fabeSYong Wu 	void                    *protect;
914d2e9a110SYong Wu 	int                     ret;
915c2c59456SMiles Chen 	u32			val;
916c2c59456SMiles Chen 	char                    *p;
9170df4fabeSYong Wu 
9180df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
9190df4fabeSYong Wu 	if (!data)
9200df4fabeSYong Wu 		return -ENOMEM;
9210df4fabeSYong Wu 	data->dev = dev;
922cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
9230df4fabeSYong Wu 
9240df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
9250df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
9260df4fabeSYong Wu 	if (!protect)
9270df4fabeSYong Wu 		return -ENOMEM;
9280df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
9290df4fabeSYong Wu 
930c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
931c2c59456SMiles Chen 		switch (data->plat_data->m4u_plat) {
932c2c59456SMiles Chen 		case M4U_MT2712:
933c2c59456SMiles Chen 			p = "mediatek,mt2712-infracfg";
934c2c59456SMiles Chen 			break;
935c2c59456SMiles Chen 		case M4U_MT8173:
936c2c59456SMiles Chen 			p = "mediatek,mt8173-infracfg";
937c2c59456SMiles Chen 			break;
938c2c59456SMiles Chen 		default:
939c2c59456SMiles Chen 			p = NULL;
940c2c59456SMiles Chen 		}
941c2c59456SMiles Chen 
942c2c59456SMiles Chen 		infracfg = syscon_regmap_lookup_by_compatible(p);
943c2c59456SMiles Chen 
944c2c59456SMiles Chen 		if (IS_ERR(infracfg))
945c2c59456SMiles Chen 			return PTR_ERR(infracfg);
946c2c59456SMiles Chen 
947c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
948c2c59456SMiles Chen 		if (ret)
949c2c59456SMiles Chen 			return ret;
950c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
951c2c59456SMiles Chen 	}
95201e23c93SYong Wu 
9530df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9540df4fabeSYong Wu 	data->base = devm_ioremap_resource(dev, res);
9550df4fabeSYong Wu 	if (IS_ERR(data->base))
9560df4fabeSYong Wu 		return PTR_ERR(data->base);
957b16c0170SJoerg Roedel 	ioaddr = res->start;
9580df4fabeSYong Wu 
9590df4fabeSYong Wu 	data->irq = platform_get_irq(pdev, 0);
9600df4fabeSYong Wu 	if (data->irq < 0)
9610df4fabeSYong Wu 		return data->irq;
9620df4fabeSYong Wu 
9636b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
9640df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
9650df4fabeSYong Wu 		if (IS_ERR(data->bclk))
9660df4fabeSYong Wu 			return PTR_ERR(data->bclk);
9672aa4c259SYong Wu 	}
9680df4fabeSYong Wu 
969c0b57581SYong Wu 	pm_runtime_enable(dev);
970c0b57581SYong Wu 
971d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
972d2e9a110SYong Wu 		ret = mtk_iommu_mm_dts_parse(dev, &match, data);
973d2e9a110SYong Wu 		if (ret) {
974d2e9a110SYong Wu 			dev_err(dev, "mm dts parse fail(%d).", ret);
975c0b57581SYong Wu 			goto out_runtime_disable;
976baf94e6eSYong Wu 		}
977d2e9a110SYong Wu 	}
978baf94e6eSYong Wu 
9790df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
9800e5a3f2eSYong Wu 	mutex_init(&data->mutex);
9810df4fabeSYong Wu 
982b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
983b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
984b16c0170SJoerg Roedel 	if (ret)
985baf94e6eSYong Wu 		goto out_link_remove;
986b16c0170SJoerg Roedel 
9872d471b20SRobin Murphy 	ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
988b16c0170SJoerg Roedel 	if (ret)
989986d9ec5SYong Wu 		goto out_sysfs_remove;
990b16c0170SJoerg Roedel 
991da3cc91bSYong Wu 	spin_lock_init(&data->tlb_lock);
9929e3a2a64SYong Wu 
9939e3a2a64SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, SHARE_PGTABLE)) {
9949e3a2a64SYong Wu 		list_add_tail(&data->list, data->plat_data->hw_list);
9959e3a2a64SYong Wu 		data->hw_list = data->plat_data->hw_list;
9969e3a2a64SYong Wu 	} else {
9979e3a2a64SYong Wu 		INIT_LIST_HEAD(&data->hw_list_head);
9989e3a2a64SYong Wu 		list_add_tail(&data->list, &data->hw_list_head);
9999e3a2a64SYong Wu 		data->hw_list = &data->hw_list_head;
10009e3a2a64SYong Wu 	}
10017c3a2ec0SYong Wu 
1002986d9ec5SYong Wu 	if (!iommu_present(&platform_bus_type)) {
1003986d9ec5SYong Wu 		ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
1004986d9ec5SYong Wu 		if (ret)
1005986d9ec5SYong Wu 			goto out_list_del;
1006986d9ec5SYong Wu 	}
10070df4fabeSYong Wu 
1008d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1009986d9ec5SYong Wu 		ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
1010986d9ec5SYong Wu 		if (ret)
1011986d9ec5SYong Wu 			goto out_bus_set_null;
1012d2e9a110SYong Wu 	}
1013986d9ec5SYong Wu 	return ret;
1014986d9ec5SYong Wu 
1015986d9ec5SYong Wu out_bus_set_null:
1016986d9ec5SYong Wu 	bus_set_iommu(&platform_bus_type, NULL);
1017986d9ec5SYong Wu out_list_del:
1018986d9ec5SYong Wu 	list_del(&data->list);
1019986d9ec5SYong Wu 	iommu_device_unregister(&data->iommu);
1020986d9ec5SYong Wu out_sysfs_remove:
1021986d9ec5SYong Wu 	iommu_device_sysfs_remove(&data->iommu);
1022baf94e6eSYong Wu out_link_remove:
1023d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
1024baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, dev);
1025c0b57581SYong Wu out_runtime_disable:
1026c0b57581SYong Wu 	pm_runtime_disable(dev);
1027986d9ec5SYong Wu 	return ret;
10280df4fabeSYong Wu }
10290df4fabeSYong Wu 
10300df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
10310df4fabeSYong Wu {
10320df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
10330df4fabeSYong Wu 
1034b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
1035b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
1036b16c0170SJoerg Roedel 
1037ee55f75eSYong Wu 	list_del(&data->list);
10380df4fabeSYong Wu 
1039d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1040baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, &pdev->dev);
1041d2e9a110SYong Wu 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
1042d2e9a110SYong Wu 	}
1043c0b57581SYong Wu 	pm_runtime_disable(&pdev->dev);
10440df4fabeSYong Wu 	devm_free_irq(&pdev->dev, data->irq, data);
10450df4fabeSYong Wu 	return 0;
10460df4fabeSYong Wu }
10470df4fabeSYong Wu 
104834665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
10490df4fabeSYong Wu {
10500df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
10510df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
10520df4fabeSYong Wu 	void __iomem *base = data->base;
10530df4fabeSYong Wu 
105435c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
105575eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
10560df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
10570df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
10580df4fabeSYong Wu 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
10590df4fabeSYong Wu 	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
106070ca608bSYong Wu 	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
1061b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
10626254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
10630df4fabeSYong Wu 	return 0;
10640df4fabeSYong Wu }
10650df4fabeSYong Wu 
106634665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
10670df4fabeSYong Wu {
10680df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
10690df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1070907ba6a1SYong Wu 	struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
10710df4fabeSYong Wu 	void __iomem *base = data->base;
10726254b64fSYong Wu 	int ret;
10730df4fabeSYong Wu 
10746254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
10756254b64fSYong Wu 	if (ret) {
10766254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
10776254b64fSYong Wu 		return ret;
10786254b64fSYong Wu 	}
1079b34ea31fSDafna Hirschfeld 
1080b34ea31fSDafna Hirschfeld 	/*
1081b34ea31fSDafna Hirschfeld 	 * Uppon first resume, only enable the clk and return, since the values of the
1082b34ea31fSDafna Hirschfeld 	 * registers are not yet set.
1083b34ea31fSDafna Hirschfeld 	 */
1084b34ea31fSDafna Hirschfeld 	if (!m4u_dom)
1085b34ea31fSDafna Hirschfeld 		return 0;
1086b34ea31fSDafna Hirschfeld 
108735c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
108875eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
10890df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
10900df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
10910df4fabeSYong Wu 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
10920df4fabeSYong Wu 	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
109370ca608bSYong Wu 	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
1094b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
1095c0b57581SYong Wu 	writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, base + REG_MMU_PT_BASE_ADDR);
10964f23f6d4SYong Wu 
10974f23f6d4SYong Wu 	/*
10984f23f6d4SYong Wu 	 * Users may allocate dma buffer before they call pm_runtime_get,
10994f23f6d4SYong Wu 	 * in which case it will lack the necessary tlb flush.
11004f23f6d4SYong Wu 	 * Thus, make sure to update the tlb after each PM resume.
11014f23f6d4SYong Wu 	 */
11024f23f6d4SYong Wu 	mtk_iommu_tlb_flush_all(data);
11030df4fabeSYong Wu 	return 0;
11040df4fabeSYong Wu }
11050df4fabeSYong Wu 
1106e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
110734665c79SYong Wu 	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
110834665c79SYong Wu 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
110934665c79SYong Wu 				     pm_runtime_force_resume)
11100df4fabeSYong Wu };
11110df4fabeSYong Wu 
1112cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
1113cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
1114d2e9a110SYong Wu 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG | SHARE_PGTABLE |
1115d2e9a110SYong Wu 			MTK_IOMMU_TYPE_MM,
11169e3a2a64SYong Wu 	.hw_list      = &m4ulist,
1117b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1118585e58f4SYong Wu 	.iova_region  = single_domain,
1119585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
112037276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
1121cecdce9dSYong Wu };
1122cecdce9dSYong Wu 
1123068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
1124068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
1125d2e9a110SYong Wu 	.flags         = HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN | WR_THROT_EN |
1126d2e9a110SYong Wu 			 MTK_IOMMU_TYPE_MM,
1127068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
1128585e58f4SYong Wu 	.iova_region   = single_domain,
1129585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
1130068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
1131cecdce9dSYong Wu };
1132cecdce9dSYong Wu 
11333c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
11343c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
1135d2e9a110SYong Wu 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
11363c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1137585e58f4SYong Wu 	.iova_region  = single_domain,
1138585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
11393c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
11403c213562SFabien Parent };
11413c213562SFabien Parent 
1142cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
1143cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
1144d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
1145d2e9a110SYong Wu 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
1146b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1147585e58f4SYong Wu 	.iova_region  = single_domain,
1148585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
114937276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
1150cecdce9dSYong Wu };
1151cecdce9dSYong Wu 
1152907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
1153907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
1154d2e9a110SYong Wu 	.flags        = RESET_AXI | MTK_IOMMU_TYPE_MM,
1155b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1156585e58f4SYong Wu 	.iova_region  = single_domain,
1157585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
115837276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1159907ba6a1SYong Wu };
1160907ba6a1SYong Wu 
11619e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = {
11629e3489e0SYong Wu 	.m4u_plat       = M4U_MT8192,
11639ec30c09SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1164d2e9a110SYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
11659e3489e0SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
11669e3489e0SYong Wu 	.iova_region    = mt8192_multi_dom,
11679e3489e0SYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
11689e3489e0SYong Wu 	.larbid_remap   = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20},
11699e3489e0SYong Wu 			   {0, 14, 16}, {0, 13, 18, 17}},
11709e3489e0SYong Wu };
11719e3489e0SYong Wu 
11720df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
1173cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1174068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
11753c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1176cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1177907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
11789e3489e0SYong Wu 	{ .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data},
11790df4fabeSYong Wu 	{}
11800df4fabeSYong Wu };
11810df4fabeSYong Wu 
11820df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
11830df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
11840df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
11850df4fabeSYong Wu 	.driver	= {
11860df4fabeSYong Wu 		.name = "mtk-iommu",
1187f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
11880df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
11890df4fabeSYong Wu 	}
11900df4fabeSYong Wu };
119118d8c74eSYong Wu module_platform_driver(mtk_iommu_driver);
11920df4fabeSYong Wu 
119318d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations");
119418d8c74eSYong Wu MODULE_LICENSE("GPL v2");
1195