xref: /linux/drivers/iommu/mtk_iommu.c (revision e76290702570bb0ef825050ce4a529b10dab2dba)
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>
23*e7629070SYong Wu #include <linux/pci.h>
240df4fabeSYong Wu #include <linux/platform_device.h>
25baf94e6eSYong Wu #include <linux/pm_runtime.h>
26c2c59456SMiles Chen #include <linux/regmap.h>
270df4fabeSYong Wu #include <linux/slab.h>
280df4fabeSYong Wu #include <linux/spinlock.h>
29c2c59456SMiles Chen #include <linux/soc/mediatek/infracfg.h>
300df4fabeSYong Wu #include <asm/barrier.h>
310df4fabeSYong Wu #include <soc/mediatek/smi.h>
320df4fabeSYong Wu 
339ca340c9SHonghui Zhang #include "mtk_iommu.h"
340df4fabeSYong Wu 
350df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
36907ba6a1SYong Wu #define MMU_PT_ADDR_MASK			GENMASK(31, 7)
370df4fabeSYong Wu 
380df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
390df4fabeSYong Wu #define F_ALL_INVLD				0x2
400df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
410df4fabeSYong Wu 
420df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
430df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
440df4fabeSYong Wu 
45068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2			0x02c
46b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1			0x038
470df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
480df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
490df4fabeSYong Wu 
5075eed350SChao Hao #define REG_MMU_MISC_CTRL			0x048
514bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
524bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))
534bb2bf4cSChao Hao 
540df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
559a87005eSYong Wu #define F_MMU_DCM				BIT(8)
569a87005eSYong Wu 
5735c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5835c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
590df4fabeSYong Wu 
600df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
61acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
620df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
63acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
640df4fabeSYong Wu 
650df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6670ca608bSYong Wu 
6730e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6830e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
690df4fabeSYong Wu 
700df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
710df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
720df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
730df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
740df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
750df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
760df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
770df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
780df4fabeSYong Wu 
790df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
8015a01f4cSYong Wu 						/* mmu0 | mmu1 */
8115a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
8215a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
8315a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
8415a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8515a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8615a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8715a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
880df4fabeSYong Wu 
890df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
900df4fabeSYong Wu 
910df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
9215a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
9315a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
940df4fabeSYong Wu 
9515a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
96ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
97ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
98ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
990df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
1000df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
1010df4fabeSYong Wu 
10215a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
10315a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
10415a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
10515a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
10615a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
10737276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10837276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
1099ec30c09SYong Wu #define F_MMU_INT_ID_COMM_ID_EXT(a)		(((a) >> 10) & 0x7)
1109ec30c09SYong Wu #define F_MMU_INT_ID_SUB_COMM_ID_EXT(a)		(((a) >> 7) & 0x7)
11115a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
11215a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
1130df4fabeSYong Wu 
114829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
1150df4fabeSYong Wu 
116f9b8c9b2SYong Wu #define PERICFG_IOMMU_1				0x714
117f9b8c9b2SYong Wu 
1186b717796SChao Hao #define HAS_4GB_MODE			BIT(0)
1196b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */
1206b717796SChao Hao #define HAS_BCLK			BIT(1)
1216b717796SChao Hao #define HAS_VLD_PA_RNG			BIT(2)
1226b717796SChao Hao #define RESET_AXI			BIT(3)
1234bb2bf4cSChao Hao #define OUT_ORDER_WR_EN			BIT(4)
1249ec30c09SYong Wu #define HAS_SUB_COMM_2BITS		BIT(5)
1259ec30c09SYong Wu #define HAS_SUB_COMM_3BITS		BIT(6)
1269ec30c09SYong Wu #define WR_THROT_EN			BIT(7)
1279ec30c09SYong Wu #define HAS_LEGACY_IVRP_PADDR		BIT(8)
1289ec30c09SYong Wu #define IOVA_34_EN			BIT(9)
1299ec30c09SYong Wu #define SHARE_PGTABLE			BIT(10) /* 2 HW share pgtable */
1309ec30c09SYong Wu #define DCM_DISABLE			BIT(11)
1319ec30c09SYong Wu #define STD_AXI_MODE			BIT(12) /* For non MM iommu */
1328cd1e619SYong Wu /* 2 bits: iommu type */
1338cd1e619SYong Wu #define MTK_IOMMU_TYPE_MM		(0x0 << 13)
1348cd1e619SYong Wu #define MTK_IOMMU_TYPE_INFRA		(0x1 << 13)
1358cd1e619SYong Wu #define MTK_IOMMU_TYPE_MASK		(0x3 << 13)
1366077c7e5SYong Wu /* PM and clock always on. e.g. infra iommu */
1376077c7e5SYong Wu #define PM_CLK_AO			BIT(15)
138*e7629070SYong Wu #define IFA_IOMMU_PCIE_SUPPORT		BIT(16)
1396b717796SChao Hao 
1408cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask)	\
1418cd1e619SYong Wu 				((((pdata)->flags) & (mask)) == (_x))
1428cd1e619SYong Wu 
1438cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, _x)
1448cd1e619SYong Wu #define MTK_IOMMU_IS_TYPE(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x,\
1458cd1e619SYong Wu 							MTK_IOMMU_TYPE_MASK)
1466b717796SChao Hao 
147d2e9a110SYong Wu #define MTK_INVALID_LARBID		MTK_LARB_NR_MAX
148d2e9a110SYong Wu 
1490df4fabeSYong Wu struct mtk_iommu_domain {
1500df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
1510df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
1520df4fabeSYong Wu 
15308500c43SYong Wu 	struct mtk_iommu_data		*data;
1540df4fabeSYong Wu 	struct iommu_domain		domain;
155ddf67a87SYong Wu 
156ddf67a87SYong Wu 	struct mutex			mutex; /* Protect "data" in this structure */
1570df4fabeSYong Wu };
1580df4fabeSYong Wu 
159b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
1600df4fabeSYong Wu 
1617f37a91dSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data);
1627f37a91dSYong Wu 
163bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({					\
164bfed8731SYong Wu 	dma_addr_t _addr = iova;					\
165bfed8731SYong Wu 	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
166bfed8731SYong Wu })
167bfed8731SYong Wu 
16876ce6546SYong Wu /*
16976ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
17076ce6546SYong Wu  *
17176ce6546SYong Wu  * CPU Physical address:
17276ce6546SYong Wu  * ====================
17376ce6546SYong Wu  *
17476ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
17576ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
17676ce6546SYong Wu  * +--I/O--+------------Memory-------------+
17776ce6546SYong Wu  *
17876ce6546SYong Wu  * IOMMU output physical address:
17976ce6546SYong Wu  *  =============================
18076ce6546SYong Wu  *
18176ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
18276ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
18376ce6546SYong Wu  *                                 +------------Memory-------------+
18476ce6546SYong Wu  *
18576ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
18676ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
18776ce6546SYong Wu  * 'E', the CPU physical address keep as is.
18876ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
18976ce6546SYong Wu  */
190b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
19176ce6546SYong Wu 
1927c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
1937c3a2ec0SYong Wu 
1949e3a2a64SYong Wu #define for_each_m4u(data, head)  list_for_each_entry(data, head, list)
1957c3a2ec0SYong Wu 
196585e58f4SYong Wu struct mtk_iommu_iova_region {
197585e58f4SYong Wu 	dma_addr_t		iova_base;
198585e58f4SYong Wu 	unsigned long long	size;
199585e58f4SYong Wu };
200585e58f4SYong Wu 
201585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = {
202585e58f4SYong Wu 	{.iova_base = 0,		.size = SZ_4G},
203585e58f4SYong Wu };
204585e58f4SYong Wu 
2059e3489e0SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[] = {
206129a3b88SYong Wu 	{ .iova_base = 0x0,		.size = SZ_4G},		/* 0 ~ 4G */
2079e3489e0SYong Wu 	#if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)
208129a3b88SYong Wu 	{ .iova_base = SZ_4G,		.size = SZ_4G},		/* 4G ~ 8G */
209129a3b88SYong Wu 	{ .iova_base = SZ_4G * 2,	.size = SZ_4G},		/* 8G ~ 12G */
210129a3b88SYong Wu 	{ .iova_base = SZ_4G * 3,	.size = SZ_4G},		/* 12G ~ 16G */
211129a3b88SYong Wu 
2129e3489e0SYong Wu 	{ .iova_base = 0x240000000ULL,	.size = 0x4000000},	/* CCU0 */
2139e3489e0SYong Wu 	{ .iova_base = 0x244000000ULL,	.size = 0x4000000},	/* CCU1 */
2149e3489e0SYong Wu 	#endif
2159e3489e0SYong Wu };
2169e3489e0SYong Wu 
2179e3a2a64SYong Wu /* If 2 M4U share a domain(use the same hwlist), Put the corresponding info in first data.*/
2189e3a2a64SYong Wu static struct mtk_iommu_data *mtk_iommu_get_frst_data(struct list_head *hwlist)
2197c3a2ec0SYong Wu {
2209e3a2a64SYong Wu 	return list_first_entry(hwlist, struct mtk_iommu_data, list);
2217c3a2ec0SYong Wu }
2227c3a2ec0SYong Wu 
2230df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
2240df4fabeSYong Wu {
2250df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
2260df4fabeSYong Wu }
2270df4fabeSYong Wu 
2280954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
2290df4fabeSYong Wu {
23015672b6dSYong Wu 	unsigned long flags;
231c0b57581SYong Wu 
23215672b6dSYong Wu 	spin_lock_irqsave(&data->tlb_lock, flags);
2337c3a2ec0SYong Wu 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
234b053bc71SChao Hao 		       data->base + data->plat_data->inv_sel_reg);
2350df4fabeSYong Wu 	writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
2360df4fabeSYong Wu 	wmb(); /* Make sure the tlb flush all done */
23715672b6dSYong Wu 	spin_unlock_irqrestore(&data->tlb_lock, flags);
2387c3a2ec0SYong Wu }
2390df4fabeSYong Wu 
2401f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
2410954d61aSYong Wu 					   struct mtk_iommu_data *data)
2420df4fabeSYong Wu {
2439e3a2a64SYong Wu 	struct list_head *head = data->hw_list;
2446077c7e5SYong Wu 	bool check_pm_status;
2451f4fd624SYong Wu 	unsigned long flags;
2461f4fd624SYong Wu 	int ret;
2471f4fd624SYong Wu 	u32 tmp;
2480df4fabeSYong Wu 
2499e3a2a64SYong Wu 	for_each_m4u(data, head) {
2506077c7e5SYong Wu 		/*
2516077c7e5SYong Wu 		 * To avoid resume the iommu device frequently when the iommu device
2526077c7e5SYong Wu 		 * is not active, it doesn't always call pm_runtime_get here, then tlb
2536077c7e5SYong Wu 		 * flush depends on the tlb flush all in the runtime resume.
2546077c7e5SYong Wu 		 *
2556077c7e5SYong Wu 		 * There are 2 special cases:
2566077c7e5SYong Wu 		 *
2576077c7e5SYong Wu 		 * Case1: The iommu dev doesn't have power domain but has bclk. This case
2586077c7e5SYong Wu 		 * should also avoid the tlb flush while the dev is not active to mute
2596077c7e5SYong Wu 		 * the tlb timeout log. like mt8173.
2606077c7e5SYong Wu 		 *
2616077c7e5SYong Wu 		 * Case2: The power/clock of infra iommu is always on, and it doesn't
2626077c7e5SYong Wu 		 * have the device link with the master devices. This case should avoid
2636077c7e5SYong Wu 		 * the PM status check.
2646077c7e5SYong Wu 		 */
2656077c7e5SYong Wu 		check_pm_status = !MTK_IOMMU_HAS_FLAG(data->plat_data, PM_CLK_AO);
2666077c7e5SYong Wu 
2676077c7e5SYong Wu 		if (check_pm_status) {
268c0b57581SYong Wu 			if (pm_runtime_get_if_in_use(data->dev) <= 0)
269c0b57581SYong Wu 				continue;
2706077c7e5SYong Wu 		}
271c0b57581SYong Wu 
2721f4fd624SYong Wu 		spin_lock_irqsave(&data->tlb_lock, flags);
2737c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
274b053bc71SChao Hao 			       data->base + data->plat_data->inv_sel_reg);
2750df4fabeSYong Wu 
276bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova),
277bfed8731SYong Wu 			       data->base + REG_MMU_INVLD_START_A);
278bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
2797c3a2ec0SYong Wu 			       data->base + REG_MMU_INVLD_END_A);
2807c3a2ec0SYong Wu 		writel_relaxed(F_MMU_INV_RANGE,
2817c3a2ec0SYong Wu 			       data->base + REG_MMU_INVALIDATE);
2820df4fabeSYong Wu 
2831f4fd624SYong Wu 		/* tlb sync */
2847c3a2ec0SYong Wu 		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
285c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
28615672b6dSYong Wu 
28715672b6dSYong Wu 		/* Clear the CPE status */
28815672b6dSYong Wu 		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
28915672b6dSYong Wu 		spin_unlock_irqrestore(&data->tlb_lock, flags);
29015672b6dSYong Wu 
2910df4fabeSYong Wu 		if (ret) {
2920df4fabeSYong Wu 			dev_warn(data->dev,
2930df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
2940954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
2950df4fabeSYong Wu 		}
296c0b57581SYong Wu 
2976077c7e5SYong Wu 		if (check_pm_status)
298c0b57581SYong Wu 			pm_runtime_put(data->dev);
2990df4fabeSYong Wu 	}
3007c3a2ec0SYong Wu }
3010df4fabeSYong Wu 
3020df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
3030df4fabeSYong Wu {
3040df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_id;
3050df4fabeSYong Wu 	struct mtk_iommu_domain *dom = data->m4u_dom;
306d2e9a110SYong Wu 	unsigned int fault_larb = MTK_INVALID_LARBID, fault_port = 0, sub_comm = 0;
307ef0f0986SYong Wu 	u32 int_state, regval, va34_32, pa34_32;
308ef0f0986SYong Wu 	u64 fault_iova, fault_pa;
3090df4fabeSYong Wu 	bool layer, write;
3100df4fabeSYong Wu 
3110df4fabeSYong Wu 	/* Read error info from registers */
3120df4fabeSYong Wu 	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
31315a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
31415a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
31515a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
31615a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
31715a01f4cSYong Wu 	} else {
31815a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
31915a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
32015a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
32115a01f4cSYong Wu 	}
3220df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
3230df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
324ef0f0986SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN)) {
325ef0f0986SYong Wu 		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
326ef0f0986SYong Wu 		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
327ef0f0986SYong Wu 		fault_iova |= (u64)va34_32 << 32;
328ef0f0986SYong Wu 	}
32982e51771SYong Wu 	pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
33082e51771SYong Wu 	fault_pa |= (u64)pa34_32 << 32;
331ef0f0986SYong Wu 
332d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
33315a01f4cSYong Wu 		fault_port = F_MMU_INT_ID_PORT_ID(regval);
3349ec30c09SYong Wu 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM_2BITS)) {
33537276e00SChao Hao 			fault_larb = F_MMU_INT_ID_COMM_ID(regval);
33637276e00SChao Hao 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
3379ec30c09SYong Wu 		} else if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM_3BITS)) {
3389ec30c09SYong Wu 			fault_larb = F_MMU_INT_ID_COMM_ID_EXT(regval);
3399ec30c09SYong Wu 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID_EXT(regval);
34037276e00SChao Hao 		} else {
34137276e00SChao Hao 			fault_larb = F_MMU_INT_ID_LARB_ID(regval);
34237276e00SChao Hao 		}
34337276e00SChao Hao 		fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
344d2e9a110SYong Wu 	}
345b3e5eee7SYong Wu 
3460df4fabeSYong Wu 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
3470df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
3480df4fabeSYong Wu 		dev_err_ratelimited(
3490df4fabeSYong Wu 			data->dev,
350f9b8c9b2SYong Wu 			"fault type=0x%x iova=0x%llx pa=0x%llx master=0x%x(larb=%d port=%d) layer=%d %s\n",
351f9b8c9b2SYong Wu 			int_state, fault_iova, fault_pa, regval, fault_larb, fault_port,
3520df4fabeSYong Wu 			layer, write ? "write" : "read");
3530df4fabeSYong Wu 	}
3540df4fabeSYong Wu 
3550df4fabeSYong Wu 	/* Interrupt clear */
3560df4fabeSYong Wu 	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
3570df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
3580df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
3590df4fabeSYong Wu 
3600df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
3610df4fabeSYong Wu 
3620df4fabeSYong Wu 	return IRQ_HANDLED;
3630df4fabeSYong Wu }
3640df4fabeSYong Wu 
365803cf9e5SYong Wu static int mtk_iommu_get_domain_id(struct device *dev,
366803cf9e5SYong Wu 				   const struct mtk_iommu_plat_data *plat_data)
367803cf9e5SYong Wu {
368803cf9e5SYong Wu 	const struct mtk_iommu_iova_region *rgn = plat_data->iova_region;
369803cf9e5SYong Wu 	const struct bus_dma_region *dma_rgn = dev->dma_range_map;
370803cf9e5SYong Wu 	int i, candidate = -1;
371803cf9e5SYong Wu 	dma_addr_t dma_end;
372803cf9e5SYong Wu 
373803cf9e5SYong Wu 	if (!dma_rgn || plat_data->iova_region_nr == 1)
374803cf9e5SYong Wu 		return 0;
375803cf9e5SYong Wu 
376803cf9e5SYong Wu 	dma_end = dma_rgn->dma_start + dma_rgn->size - 1;
377803cf9e5SYong Wu 	for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) {
378803cf9e5SYong Wu 		/* Best fit. */
379803cf9e5SYong Wu 		if (dma_rgn->dma_start == rgn->iova_base &&
380803cf9e5SYong Wu 		    dma_end == rgn->iova_base + rgn->size - 1)
381803cf9e5SYong Wu 			return i;
382803cf9e5SYong Wu 		/* ok if it is inside this region. */
383803cf9e5SYong Wu 		if (dma_rgn->dma_start >= rgn->iova_base &&
384803cf9e5SYong Wu 		    dma_end < rgn->iova_base + rgn->size)
385803cf9e5SYong Wu 			candidate = i;
386803cf9e5SYong Wu 	}
387803cf9e5SYong Wu 
388803cf9e5SYong Wu 	if (candidate >= 0)
389803cf9e5SYong Wu 		return candidate;
390803cf9e5SYong Wu 	dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n",
391803cf9e5SYong Wu 		&dma_rgn->dma_start, dma_rgn->size);
392803cf9e5SYong Wu 	return -EINVAL;
393803cf9e5SYong Wu }
394803cf9e5SYong Wu 
395f9b8c9b2SYong Wu static int mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev,
3968d2c749eSYong Wu 			    bool enable, unsigned int domid)
3970df4fabeSYong Wu {
3980df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
3990df4fabeSYong Wu 	unsigned int                 larbid, portid;
400a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
4018d2c749eSYong Wu 	const struct mtk_iommu_iova_region *region;
402f9b8c9b2SYong Wu 	u32 peri_mmuen, peri_mmuen_msk;
403f9b8c9b2SYong Wu 	int i, ret = 0;
4040df4fabeSYong Wu 
40558f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
40658f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
40758f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
4088d2c749eSYong Wu 
409d2e9a110SYong Wu 		if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
4101ee9feb2SYong Wu 			larb_mmu = &data->larb_imu[larbid];
4110df4fabeSYong Wu 
4128d2c749eSYong Wu 			region = data->plat_data->iova_region + domid;
4138d2c749eSYong Wu 			larb_mmu->bank[portid] = upper_32_bits(region->iova_base);
4148d2c749eSYong Wu 
4158d2c749eSYong Wu 			dev_dbg(dev, "%s iommu for larb(%s) port %d dom %d bank %d.\n",
4168d2c749eSYong Wu 				enable ? "enable" : "disable", dev_name(larb_mmu->dev),
4178d2c749eSYong Wu 				portid, domid, larb_mmu->bank[portid]);
4180df4fabeSYong Wu 
4190df4fabeSYong Wu 			if (enable)
4200df4fabeSYong Wu 				larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
4210df4fabeSYong Wu 			else
4220df4fabeSYong Wu 				larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
423f9b8c9b2SYong Wu 		} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {
424f9b8c9b2SYong Wu 			peri_mmuen_msk = BIT(portid);
425*e7629070SYong Wu 			/* PCI dev has only one output id, enable the next writing bit for PCIe */
426*e7629070SYong Wu 			if (dev_is_pci(dev))
427*e7629070SYong Wu 				peri_mmuen_msk |= BIT(portid + 1);
428f9b8c9b2SYong Wu 
429*e7629070SYong Wu 			peri_mmuen = enable ? peri_mmuen_msk : 0;
430f9b8c9b2SYong Wu 			ret = regmap_update_bits(data->pericfg, PERICFG_IOMMU_1,
431f9b8c9b2SYong Wu 						 peri_mmuen_msk, peri_mmuen);
432f9b8c9b2SYong Wu 			if (ret)
433f9b8c9b2SYong Wu 				dev_err(dev, "%s iommu(%s) inframaster 0x%x fail(%d).\n",
434f9b8c9b2SYong Wu 					enable ? "enable" : "disable",
435f9b8c9b2SYong Wu 					dev_name(data->dev), peri_mmuen_msk, ret);
4360df4fabeSYong Wu 		}
4370df4fabeSYong Wu 	}
438f9b8c9b2SYong Wu 	return ret;
439d2e9a110SYong Wu }
4400df4fabeSYong Wu 
4414f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
442c3045f39SYong Wu 				     struct mtk_iommu_data *data,
443c3045f39SYong Wu 				     unsigned int domid)
4440df4fabeSYong Wu {
445c3045f39SYong Wu 	const struct mtk_iommu_iova_region *region;
446c3045f39SYong Wu 
447c3045f39SYong Wu 	/* Use the exist domain as there is only one pgtable here. */
448c3045f39SYong Wu 	if (data->m4u_dom) {
449c3045f39SYong Wu 		dom->iop = data->m4u_dom->iop;
450c3045f39SYong Wu 		dom->cfg = data->m4u_dom->cfg;
451c3045f39SYong Wu 		dom->domain.pgsize_bitmap = data->m4u_dom->cfg.pgsize_bitmap;
452c3045f39SYong Wu 		goto update_iova_region;
453c3045f39SYong Wu 	}
454c3045f39SYong Wu 
4550df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
4560df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
4570df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
458b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
4590df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
4602f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
4610df4fabeSYong Wu 		.iommu_dev = data->dev,
4620df4fabeSYong Wu 	};
4630df4fabeSYong Wu 
4649bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
4659bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
4669bdfe4c1SYong Wu 	else
4679bdfe4c1SYong Wu 		dom->cfg.oas = 35;
4689bdfe4c1SYong Wu 
4690df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
4700df4fabeSYong Wu 	if (!dom->iop) {
4710df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
4720df4fabeSYong Wu 		return -EINVAL;
4730df4fabeSYong Wu 	}
4740df4fabeSYong Wu 
4750df4fabeSYong Wu 	/* Update our support page sizes bitmap */
476d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
477b7875eb9SYong Wu 
478c3045f39SYong Wu update_iova_region:
479c3045f39SYong Wu 	/* Update the iova region for this domain */
480c3045f39SYong Wu 	region = data->plat_data->iova_region + domid;
481c3045f39SYong Wu 	dom->domain.geometry.aperture_start = region->iova_base;
482c3045f39SYong Wu 	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
483b7875eb9SYong Wu 	dom->domain.geometry.force_aperture = true;
4840df4fabeSYong Wu 	return 0;
4850df4fabeSYong Wu }
4860df4fabeSYong Wu 
4870df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
4880df4fabeSYong Wu {
4890df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
4900df4fabeSYong Wu 
49132e1cccfSYong Wu 	if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED)
4920df4fabeSYong Wu 		return NULL;
4930df4fabeSYong Wu 
4940df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
4950df4fabeSYong Wu 	if (!dom)
4960df4fabeSYong Wu 		return NULL;
497ddf67a87SYong Wu 	mutex_init(&dom->mutex);
4980df4fabeSYong Wu 
4994f956c97SYong Wu 	return &dom->domain;
5004f956c97SYong Wu }
5014f956c97SYong Wu 
5020df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
5030df4fabeSYong Wu {
5040df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
5050df4fabeSYong Wu }
5060df4fabeSYong Wu 
5070df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
5080df4fabeSYong Wu 				   struct device *dev)
5090df4fabeSYong Wu {
510645b87c1SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev), *frstdata;
5110df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5129e3a2a64SYong Wu 	struct list_head *hw_list = data->hw_list;
513c0b57581SYong Wu 	struct device *m4udev = data->dev;
514803cf9e5SYong Wu 	int ret, domid;
5150df4fabeSYong Wu 
516803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
517803cf9e5SYong Wu 	if (domid < 0)
518803cf9e5SYong Wu 		return domid;
519803cf9e5SYong Wu 
520ddf67a87SYong Wu 	mutex_lock(&dom->mutex);
5214f956c97SYong Wu 	if (!dom->data) {
522645b87c1SYong Wu 		/* Data is in the frstdata in sharing pgtable case. */
5239e3a2a64SYong Wu 		frstdata = mtk_iommu_get_frst_data(hw_list);
524645b87c1SYong Wu 
525ddf67a87SYong Wu 		ret = mtk_iommu_domain_finalise(dom, frstdata, domid);
526ddf67a87SYong Wu 		if (ret) {
527ddf67a87SYong Wu 			mutex_unlock(&dom->mutex);
5284f956c97SYong Wu 			return -ENODEV;
529ddf67a87SYong Wu 		}
5304f956c97SYong Wu 		dom->data = data;
5314f956c97SYong Wu 	}
532ddf67a87SYong Wu 	mutex_unlock(&dom->mutex);
5334f956c97SYong Wu 
5340e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
5357f37a91dSYong Wu 	if (!data->m4u_dom) { /* Initialize the M4U HW */
536c0b57581SYong Wu 		ret = pm_runtime_resume_and_get(m4udev);
537c0b57581SYong Wu 		if (ret < 0)
5380e5a3f2eSYong Wu 			goto err_unlock;
539c0b57581SYong Wu 
540c0b57581SYong Wu 		ret = mtk_iommu_hw_init(data);
541c0b57581SYong Wu 		if (ret) {
542c0b57581SYong Wu 			pm_runtime_put(m4udev);
5430e5a3f2eSYong Wu 			goto err_unlock;
544c0b57581SYong Wu 		}
5450df4fabeSYong Wu 		data->m4u_dom = dom;
546d1e5f26fSRobin Murphy 		writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
5474b00f5acSYong Wu 		       data->base + REG_MMU_PT_BASE_ADDR);
548c0b57581SYong Wu 
549c0b57581SYong Wu 		pm_runtime_put(m4udev);
5500df4fabeSYong Wu 	}
5510e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
5520df4fabeSYong Wu 
553f9b8c9b2SYong Wu 	return mtk_iommu_config(data, dev, true, domid);
5540e5a3f2eSYong Wu 
5550e5a3f2eSYong Wu err_unlock:
5560e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
5570e5a3f2eSYong Wu 	return ret;
5580df4fabeSYong Wu }
5590df4fabeSYong Wu 
5600df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
5610df4fabeSYong Wu 				    struct device *dev)
5620df4fabeSYong Wu {
5633524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
5640df4fabeSYong Wu 
5658d2c749eSYong Wu 	mtk_iommu_config(data, dev, false, 0);
5660df4fabeSYong Wu }
5670df4fabeSYong Wu 
5680df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
569781ca2deSTom Murphy 			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
5700df4fabeSYong Wu {
5710df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5720df4fabeSYong Wu 
573b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
57408500c43SYong Wu 	if (dom->data->enable_4GB)
575b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
576b4dad40eSYong Wu 
57760829b4dSYong Wu 	/* Synchronize with the tlb_lock */
578f34ce7a7SBaolin Wang 	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
5790df4fabeSYong Wu }
5800df4fabeSYong Wu 
5810df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
58256f8af5eSWill Deacon 			      unsigned long iova, size_t size,
58356f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
5840df4fabeSYong Wu {
5850df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5860df4fabeSYong Wu 
5873136895cSRobin Murphy 	iommu_iotlb_gather_add_range(gather, iova, size);
58860829b4dSYong Wu 	return dom->iop->unmap(dom->iop, iova, size, gather);
5890df4fabeSYong Wu }
5900df4fabeSYong Wu 
59156f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
59256f8af5eSWill Deacon {
59308500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
59408500c43SYong Wu 
59508500c43SYong Wu 	mtk_iommu_tlb_flush_all(dom->data);
59656f8af5eSWill Deacon }
59756f8af5eSWill Deacon 
59856f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
59956f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
6004d689b61SRobin Murphy {
60108500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
602862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
603da3cc91bSYong Wu 
604e6d25e7dSYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, dom->data);
6054d689b61SRobin Murphy }
6064d689b61SRobin Murphy 
60720143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
60820143451SYong Wu 			       size_t size)
60920143451SYong Wu {
61008500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
61120143451SYong Wu 
612e6d25e7dSYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, dom->data);
61320143451SYong Wu }
61420143451SYong Wu 
6150df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
6160df4fabeSYong Wu 					  dma_addr_t iova)
6170df4fabeSYong Wu {
6180df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
6190df4fabeSYong Wu 	phys_addr_t pa;
6200df4fabeSYong Wu 
6210df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
622f13efafcSArnd Bergmann 	if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
623f13efafcSArnd Bergmann 	    dom->data->enable_4GB &&
624f13efafcSArnd Bergmann 	    pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
625b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
62630e2fccfSYong Wu 
6270df4fabeSYong Wu 	return pa;
6280df4fabeSYong Wu }
6290df4fabeSYong Wu 
63080e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
6310df4fabeSYong Wu {
632a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
633b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
634635319a4SYong Wu 	struct device_link *link;
635635319a4SYong Wu 	struct device *larbdev;
636635319a4SYong Wu 	unsigned int larbid, larbidx, i;
6370df4fabeSYong Wu 
638a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
63980e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
6400df4fabeSYong Wu 
6413524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
642b16c0170SJoerg Roedel 
643d2e9a110SYong Wu 	if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
644d2e9a110SYong Wu 		return &data->iommu;
645d2e9a110SYong Wu 
646635319a4SYong Wu 	/*
647635319a4SYong Wu 	 * Link the consumer device with the smi-larb device(supplier).
648635319a4SYong Wu 	 * The device that connects with each a larb is a independent HW.
649635319a4SYong Wu 	 * All the ports in each a device should be in the same larbs.
650635319a4SYong Wu 	 */
651635319a4SYong Wu 	larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
652635319a4SYong Wu 	for (i = 1; i < fwspec->num_ids; i++) {
653635319a4SYong Wu 		larbidx = MTK_M4U_TO_LARB(fwspec->ids[i]);
654635319a4SYong Wu 		if (larbid != larbidx) {
655635319a4SYong Wu 			dev_err(dev, "Can only use one larb. Fail@larb%d-%d.\n",
656635319a4SYong Wu 				larbid, larbidx);
657635319a4SYong Wu 			return ERR_PTR(-EINVAL);
658635319a4SYong Wu 		}
659635319a4SYong Wu 	}
660635319a4SYong Wu 	larbdev = data->larb_imu[larbid].dev;
661635319a4SYong Wu 	link = device_link_add(dev, larbdev,
662635319a4SYong Wu 			       DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
663635319a4SYong Wu 	if (!link)
664635319a4SYong Wu 		dev_err(dev, "Unable to link %s\n", dev_name(larbdev));
66580e4592aSJoerg Roedel 	return &data->iommu;
6660df4fabeSYong Wu }
6670df4fabeSYong Wu 
66880e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
6690df4fabeSYong Wu {
670a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
671635319a4SYong Wu 	struct mtk_iommu_data *data;
672635319a4SYong Wu 	struct device *larbdev;
673635319a4SYong Wu 	unsigned int larbid;
674b16c0170SJoerg Roedel 
675a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
6760df4fabeSYong Wu 		return;
6770df4fabeSYong Wu 
678635319a4SYong Wu 	data = dev_iommu_priv_get(dev);
679d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
680635319a4SYong Wu 		larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
681635319a4SYong Wu 		larbdev = data->larb_imu[larbid].dev;
682635319a4SYong Wu 		device_link_remove(dev, larbdev);
683d2e9a110SYong Wu 	}
684635319a4SYong Wu 
68558f0d1d5SRobin Murphy 	iommu_fwspec_free(dev);
6860df4fabeSYong Wu }
6870df4fabeSYong Wu 
6880df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
6890df4fabeSYong Wu {
6909e3a2a64SYong Wu 	struct mtk_iommu_data *c_data = dev_iommu_priv_get(dev), *data;
6919e3a2a64SYong Wu 	struct list_head *hw_list = c_data->hw_list;
692c3045f39SYong Wu 	struct iommu_group *group;
693803cf9e5SYong Wu 	int domid;
6940df4fabeSYong Wu 
6959e3a2a64SYong Wu 	data = mtk_iommu_get_frst_data(hw_list);
69658f0d1d5SRobin Murphy 	if (!data)
6970df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
6980df4fabeSYong Wu 
699803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
700803cf9e5SYong Wu 	if (domid < 0)
701803cf9e5SYong Wu 		return ERR_PTR(domid);
702803cf9e5SYong Wu 
7030e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
704c3045f39SYong Wu 	group = data->m4u_group[domid];
705c3045f39SYong Wu 	if (!group) {
706c3045f39SYong Wu 		group = iommu_group_alloc();
707c3045f39SYong Wu 		if (!IS_ERR(group))
708c3045f39SYong Wu 			data->m4u_group[domid] = group;
7093a8d40b6SRobin Murphy 	} else {
710c3045f39SYong Wu 		iommu_group_ref_get(group);
7110df4fabeSYong Wu 	}
7120e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
713c3045f39SYong Wu 	return group;
7140df4fabeSYong Wu }
7150df4fabeSYong Wu 
7160df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
7170df4fabeSYong Wu {
7180df4fabeSYong Wu 	struct platform_device *m4updev;
7190df4fabeSYong Wu 
7200df4fabeSYong Wu 	if (args->args_count != 1) {
7210df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
7220df4fabeSYong Wu 			args->args_count);
7230df4fabeSYong Wu 		return -EINVAL;
7240df4fabeSYong Wu 	}
7250df4fabeSYong Wu 
7263524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
7270df4fabeSYong Wu 		/* Get the m4u device */
7280df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
7290df4fabeSYong Wu 		if (WARN_ON(!m4updev))
7300df4fabeSYong Wu 			return -EINVAL;
7310df4fabeSYong Wu 
7323524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
7330df4fabeSYong Wu 	}
7340df4fabeSYong Wu 
73558f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
7360df4fabeSYong Wu }
7370df4fabeSYong Wu 
738ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev,
739ab1d5281SYong Wu 				       struct list_head *head)
740ab1d5281SYong Wu {
741ab1d5281SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
742ab1d5281SYong Wu 	unsigned int domid = mtk_iommu_get_domain_id(dev, data->plat_data), i;
743ab1d5281SYong Wu 	const struct mtk_iommu_iova_region *resv, *curdom;
744ab1d5281SYong Wu 	struct iommu_resv_region *region;
745ab1d5281SYong Wu 	int prot = IOMMU_WRITE | IOMMU_READ;
746ab1d5281SYong Wu 
7477a566173SColin Ian King 	if ((int)domid < 0)
748ab1d5281SYong Wu 		return;
749ab1d5281SYong Wu 	curdom = data->plat_data->iova_region + domid;
750ab1d5281SYong Wu 	for (i = 0; i < data->plat_data->iova_region_nr; i++) {
751ab1d5281SYong Wu 		resv = data->plat_data->iova_region + i;
752ab1d5281SYong Wu 
753ab1d5281SYong Wu 		/* Only reserve when the region is inside the current domain */
754ab1d5281SYong Wu 		if (resv->iova_base <= curdom->iova_base ||
755ab1d5281SYong Wu 		    resv->iova_base + resv->size >= curdom->iova_base + curdom->size)
756ab1d5281SYong Wu 			continue;
757ab1d5281SYong Wu 
758ab1d5281SYong Wu 		region = iommu_alloc_resv_region(resv->iova_base, resv->size,
759ab1d5281SYong Wu 						 prot, IOMMU_RESV_RESERVED);
760ab1d5281SYong Wu 		if (!region)
761ab1d5281SYong Wu 			return;
762ab1d5281SYong Wu 
763ab1d5281SYong Wu 		list_add_tail(&region->list, head);
764ab1d5281SYong Wu 	}
765ab1d5281SYong Wu }
766ab1d5281SYong Wu 
767b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
7680df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
76980e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
77080e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
7710df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
7720df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
773ab1d5281SYong Wu 	.get_resv_regions = mtk_iommu_get_resv_regions,
774ab1d5281SYong Wu 	.put_resv_regions = generic_iommu_put_resv_regions,
7750df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
77618d8c74eSYong Wu 	.owner		= THIS_MODULE,
7779a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
7789a630a4bSLu Baolu 		.attach_dev	= mtk_iommu_attach_device,
7799a630a4bSLu Baolu 		.detach_dev	= mtk_iommu_detach_device,
7809a630a4bSLu Baolu 		.map		= mtk_iommu_map,
7819a630a4bSLu Baolu 		.unmap		= mtk_iommu_unmap,
7829a630a4bSLu Baolu 		.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
7839a630a4bSLu Baolu 		.iotlb_sync	= mtk_iommu_iotlb_sync,
7849a630a4bSLu Baolu 		.iotlb_sync_map	= mtk_iommu_sync_map,
7859a630a4bSLu Baolu 		.iova_to_phys	= mtk_iommu_iova_to_phys,
7869a630a4bSLu Baolu 		.free		= mtk_iommu_domain_free,
7879a630a4bSLu Baolu 	}
7880df4fabeSYong Wu };
7890df4fabeSYong Wu 
7900df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
7910df4fabeSYong Wu {
7920df4fabeSYong Wu 	u32 regval;
7930df4fabeSYong Wu 
79486444413SChao Hao 	if (data->plat_data->m4u_plat == M4U_MT8173) {
795acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
796acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
79786444413SChao Hao 	} else {
79886444413SChao Hao 		regval = readl_relaxed(data->base + REG_MMU_CTRL_REG);
79986444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
80086444413SChao Hao 	}
8010df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
8020df4fabeSYong Wu 
8030df4fabeSYong Wu 	regval = F_L2_MULIT_HIT_EN |
8040df4fabeSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
8050df4fabeSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
8060df4fabeSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
8070df4fabeSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
8080df4fabeSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
8090df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
8100df4fabeSYong Wu 
8110df4fabeSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
8120df4fabeSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
8130df4fabeSYong Wu 		F_INT_INVALID_PA_FAULT |
8140df4fabeSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
8150df4fabeSYong Wu 		F_INT_TLB_MISS_FAULT |
8160df4fabeSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
8170df4fabeSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
8180df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
8190df4fabeSYong Wu 
820d1b5ef00SFabien Parent 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
82170ca608bSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
82270ca608bSYong Wu 	else
82370ca608bSYong Wu 		regval = lower_32_bits(data->protect_base) |
82470ca608bSYong Wu 			 upper_32_bits(data->protect_base);
82570ca608bSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
82670ca608bSYong Wu 
8276b717796SChao Hao 	if (data->enable_4GB &&
8286b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
82930e2fccfSYong Wu 		/*
83030e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
83130e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
83230e2fccfSYong Wu 		 */
83330e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
83430e2fccfSYong Wu 		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
83530e2fccfSYong Wu 	}
8369a87005eSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, DCM_DISABLE))
8379a87005eSYong Wu 		writel_relaxed(F_MMU_DCM, data->base + REG_MMU_DCM_DIS);
8389a87005eSYong Wu 	else
8390df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
8409a87005eSYong Wu 
84135c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
84235c1b48dSChao Hao 		/* write command throttling mode */
84335c1b48dSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL);
84435c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
84535c1b48dSChao Hao 		writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL);
84635c1b48dSChao Hao 	}
847e6dec923SYong Wu 
8486b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
84975eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
8504bb2bf4cSChao Hao 		regval = 0;
8514bb2bf4cSChao Hao 	} else {
8524bb2bf4cSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
853d265a4adSYong Wu 		if (!MTK_IOMMU_HAS_FLAG(data->plat_data, STD_AXI_MODE))
8544bb2bf4cSChao Hao 			regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
8554bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
8564bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
85775eed350SChao Hao 	}
8584bb2bf4cSChao Hao 	writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
8590df4fabeSYong Wu 
8600df4fabeSYong Wu 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
8610df4fabeSYong Wu 			     dev_name(data->dev), (void *)data)) {
8620df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
8630df4fabeSYong Wu 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
8640df4fabeSYong Wu 		return -ENODEV;
8650df4fabeSYong Wu 	}
8660df4fabeSYong Wu 
8670df4fabeSYong Wu 	return 0;
8680df4fabeSYong Wu }
8690df4fabeSYong Wu 
8700df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
8710df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
8720df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
8730df4fabeSYong Wu };
8740df4fabeSYong Wu 
875d2e9a110SYong Wu static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **match,
876d2e9a110SYong Wu 				  struct mtk_iommu_data *data)
877d2e9a110SYong Wu {
878f7b71d0dSYong Wu 	struct device_node *larbnode, *smicomm_node, *smi_subcomm_node;
879d2e9a110SYong Wu 	struct platform_device *plarbdev;
880d2e9a110SYong Wu 	struct device_link *link;
881d2e9a110SYong Wu 	int i, larb_nr, ret;
882d2e9a110SYong Wu 
883d2e9a110SYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL);
884d2e9a110SYong Wu 	if (larb_nr < 0)
885d2e9a110SYong Wu 		return larb_nr;
886d2e9a110SYong Wu 
887d2e9a110SYong Wu 	for (i = 0; i < larb_nr; i++) {
888d2e9a110SYong Wu 		u32 id;
889d2e9a110SYong Wu 
890d2e9a110SYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
891d2e9a110SYong Wu 		if (!larbnode)
892d2e9a110SYong Wu 			return -EINVAL;
893d2e9a110SYong Wu 
894d2e9a110SYong Wu 		if (!of_device_is_available(larbnode)) {
895d2e9a110SYong Wu 			of_node_put(larbnode);
896d2e9a110SYong Wu 			continue;
897d2e9a110SYong Wu 		}
898d2e9a110SYong Wu 
899d2e9a110SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
900d2e9a110SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
901d2e9a110SYong Wu 			id = i;
902d2e9a110SYong Wu 
903d2e9a110SYong Wu 		plarbdev = of_find_device_by_node(larbnode);
904d2e9a110SYong Wu 		if (!plarbdev) {
905d2e9a110SYong Wu 			of_node_put(larbnode);
906d2e9a110SYong Wu 			return -ENODEV;
907d2e9a110SYong Wu 		}
908d2e9a110SYong Wu 		if (!plarbdev->dev.driver) {
909d2e9a110SYong Wu 			of_node_put(larbnode);
910d2e9a110SYong Wu 			return -EPROBE_DEFER;
911d2e9a110SYong Wu 		}
912d2e9a110SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
913d2e9a110SYong Wu 
914d2e9a110SYong Wu 		component_match_add_release(dev, match, component_release_of,
915d2e9a110SYong Wu 					    component_compare_of, larbnode);
916d2e9a110SYong Wu 	}
917d2e9a110SYong Wu 
918f7b71d0dSYong Wu 	/* Get smi-(sub)-common dev from the last larb. */
919f7b71d0dSYong Wu 	smi_subcomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
920f7b71d0dSYong Wu 	if (!smi_subcomm_node)
921d2e9a110SYong Wu 		return -EINVAL;
922d2e9a110SYong Wu 
923f7b71d0dSYong Wu 	/*
924f7b71d0dSYong Wu 	 * It may have two level smi-common. the node is smi-sub-common if it
925f7b71d0dSYong Wu 	 * has a new mediatek,smi property. otherwise it is smi-commmon.
926f7b71d0dSYong Wu 	 */
927f7b71d0dSYong Wu 	smicomm_node = of_parse_phandle(smi_subcomm_node, "mediatek,smi", 0);
928f7b71d0dSYong Wu 	if (smicomm_node)
929f7b71d0dSYong Wu 		of_node_put(smi_subcomm_node);
930f7b71d0dSYong Wu 	else
931f7b71d0dSYong Wu 		smicomm_node = smi_subcomm_node;
932f7b71d0dSYong Wu 
933d2e9a110SYong Wu 	plarbdev = of_find_device_by_node(smicomm_node);
934d2e9a110SYong Wu 	of_node_put(smicomm_node);
935d2e9a110SYong Wu 	data->smicomm_dev = &plarbdev->dev;
936d2e9a110SYong Wu 
937d2e9a110SYong Wu 	link = device_link_add(data->smicomm_dev, dev,
938d2e9a110SYong Wu 			       DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
939d2e9a110SYong Wu 	if (!link) {
940d2e9a110SYong Wu 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
941d2e9a110SYong Wu 		return -EINVAL;
942d2e9a110SYong Wu 	}
943d2e9a110SYong Wu 	return 0;
944d2e9a110SYong Wu }
945d2e9a110SYong Wu 
9460df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
9470df4fabeSYong Wu {
9480df4fabeSYong Wu 	struct mtk_iommu_data   *data;
9490df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
9500df4fabeSYong Wu 	struct resource         *res;
951b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
9520df4fabeSYong Wu 	struct component_match  *match = NULL;
953c2c59456SMiles Chen 	struct regmap		*infracfg;
9540df4fabeSYong Wu 	void                    *protect;
955d2e9a110SYong Wu 	int                     ret;
956c2c59456SMiles Chen 	u32			val;
957c2c59456SMiles Chen 	char                    *p;
9580df4fabeSYong Wu 
9590df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
9600df4fabeSYong Wu 	if (!data)
9610df4fabeSYong Wu 		return -ENOMEM;
9620df4fabeSYong Wu 	data->dev = dev;
963cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
9640df4fabeSYong Wu 
9650df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
9660df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
9670df4fabeSYong Wu 	if (!protect)
9680df4fabeSYong Wu 		return -ENOMEM;
9690df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
9700df4fabeSYong Wu 
971c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
972c2c59456SMiles Chen 		switch (data->plat_data->m4u_plat) {
973c2c59456SMiles Chen 		case M4U_MT2712:
974c2c59456SMiles Chen 			p = "mediatek,mt2712-infracfg";
975c2c59456SMiles Chen 			break;
976c2c59456SMiles Chen 		case M4U_MT8173:
977c2c59456SMiles Chen 			p = "mediatek,mt8173-infracfg";
978c2c59456SMiles Chen 			break;
979c2c59456SMiles Chen 		default:
980c2c59456SMiles Chen 			p = NULL;
981c2c59456SMiles Chen 		}
982c2c59456SMiles Chen 
983c2c59456SMiles Chen 		infracfg = syscon_regmap_lookup_by_compatible(p);
984c2c59456SMiles Chen 
985c2c59456SMiles Chen 		if (IS_ERR(infracfg))
986c2c59456SMiles Chen 			return PTR_ERR(infracfg);
987c2c59456SMiles Chen 
988c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
989c2c59456SMiles Chen 		if (ret)
990c2c59456SMiles Chen 			return ret;
991c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
992c2c59456SMiles Chen 	}
99301e23c93SYong Wu 
9940df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9950df4fabeSYong Wu 	data->base = devm_ioremap_resource(dev, res);
9960df4fabeSYong Wu 	if (IS_ERR(data->base))
9970df4fabeSYong Wu 		return PTR_ERR(data->base);
998b16c0170SJoerg Roedel 	ioaddr = res->start;
9990df4fabeSYong Wu 
10000df4fabeSYong Wu 	data->irq = platform_get_irq(pdev, 0);
10010df4fabeSYong Wu 	if (data->irq < 0)
10020df4fabeSYong Wu 		return data->irq;
10030df4fabeSYong Wu 
10046b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
10050df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
10060df4fabeSYong Wu 		if (IS_ERR(data->bclk))
10070df4fabeSYong Wu 			return PTR_ERR(data->bclk);
10082aa4c259SYong Wu 	}
10090df4fabeSYong Wu 
1010c0b57581SYong Wu 	pm_runtime_enable(dev);
1011c0b57581SYong Wu 
1012d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1013d2e9a110SYong Wu 		ret = mtk_iommu_mm_dts_parse(dev, &match, data);
1014d2e9a110SYong Wu 		if (ret) {
1015d2e9a110SYong Wu 			dev_err(dev, "mm dts parse fail(%d).", ret);
1016c0b57581SYong Wu 			goto out_runtime_disable;
1017baf94e6eSYong Wu 		}
1018f9b8c9b2SYong Wu 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&
1019f9b8c9b2SYong Wu 		   data->plat_data->pericfg_comp_str) {
1020f9b8c9b2SYong Wu 		infracfg = syscon_regmap_lookup_by_compatible(data->plat_data->pericfg_comp_str);
1021f9b8c9b2SYong Wu 		if (IS_ERR(infracfg)) {
1022f9b8c9b2SYong Wu 			ret = PTR_ERR(infracfg);
1023f9b8c9b2SYong Wu 			goto out_runtime_disable;
1024f9b8c9b2SYong Wu 		}
1025f9b8c9b2SYong Wu 
1026f9b8c9b2SYong Wu 		data->pericfg = infracfg;
1027d2e9a110SYong Wu 	}
1028baf94e6eSYong Wu 
10290df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
10300e5a3f2eSYong Wu 	mutex_init(&data->mutex);
10310df4fabeSYong Wu 
1032b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
1033b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
1034b16c0170SJoerg Roedel 	if (ret)
1035baf94e6eSYong Wu 		goto out_link_remove;
1036b16c0170SJoerg Roedel 
10372d471b20SRobin Murphy 	ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
1038b16c0170SJoerg Roedel 	if (ret)
1039986d9ec5SYong Wu 		goto out_sysfs_remove;
1040b16c0170SJoerg Roedel 
1041da3cc91bSYong Wu 	spin_lock_init(&data->tlb_lock);
10429e3a2a64SYong Wu 
10439e3a2a64SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, SHARE_PGTABLE)) {
10449e3a2a64SYong Wu 		list_add_tail(&data->list, data->plat_data->hw_list);
10459e3a2a64SYong Wu 		data->hw_list = data->plat_data->hw_list;
10469e3a2a64SYong Wu 	} else {
10479e3a2a64SYong Wu 		INIT_LIST_HEAD(&data->hw_list_head);
10489e3a2a64SYong Wu 		list_add_tail(&data->list, &data->hw_list_head);
10499e3a2a64SYong Wu 		data->hw_list = &data->hw_list_head;
10509e3a2a64SYong Wu 	}
10517c3a2ec0SYong Wu 
1052986d9ec5SYong Wu 	if (!iommu_present(&platform_bus_type)) {
1053986d9ec5SYong Wu 		ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
1054986d9ec5SYong Wu 		if (ret)
1055986d9ec5SYong Wu 			goto out_list_del;
1056986d9ec5SYong Wu 	}
10570df4fabeSYong Wu 
1058d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1059986d9ec5SYong Wu 		ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
1060986d9ec5SYong Wu 		if (ret)
1061986d9ec5SYong Wu 			goto out_bus_set_null;
1062*e7629070SYong Wu 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&
1063*e7629070SYong Wu 		   MTK_IOMMU_HAS_FLAG(data->plat_data, IFA_IOMMU_PCIE_SUPPORT)) {
1064*e7629070SYong Wu #ifdef CONFIG_PCI
1065*e7629070SYong Wu 		if (!iommu_present(&pci_bus_type)) {
1066*e7629070SYong Wu 			ret = bus_set_iommu(&pci_bus_type, &mtk_iommu_ops);
1067*e7629070SYong Wu 			if (ret) /* PCIe fail don't affect platform_bus. */
1068*e7629070SYong Wu 				goto out_list_del;
1069*e7629070SYong Wu 		}
1070*e7629070SYong Wu #endif
1071d2e9a110SYong Wu 	}
1072986d9ec5SYong Wu 	return ret;
1073986d9ec5SYong Wu 
1074986d9ec5SYong Wu out_bus_set_null:
1075986d9ec5SYong Wu 	bus_set_iommu(&platform_bus_type, NULL);
1076986d9ec5SYong Wu out_list_del:
1077986d9ec5SYong Wu 	list_del(&data->list);
1078986d9ec5SYong Wu 	iommu_device_unregister(&data->iommu);
1079986d9ec5SYong Wu out_sysfs_remove:
1080986d9ec5SYong Wu 	iommu_device_sysfs_remove(&data->iommu);
1081baf94e6eSYong Wu out_link_remove:
1082d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
1083baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, dev);
1084c0b57581SYong Wu out_runtime_disable:
1085c0b57581SYong Wu 	pm_runtime_disable(dev);
1086986d9ec5SYong Wu 	return ret;
10870df4fabeSYong Wu }
10880df4fabeSYong Wu 
10890df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
10900df4fabeSYong Wu {
10910df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
10920df4fabeSYong Wu 
1093b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
1094b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
1095b16c0170SJoerg Roedel 
1096ee55f75eSYong Wu 	list_del(&data->list);
10970df4fabeSYong Wu 
1098d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1099baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, &pdev->dev);
1100d2e9a110SYong Wu 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
1101*e7629070SYong Wu 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&
1102*e7629070SYong Wu 		   MTK_IOMMU_HAS_FLAG(data->plat_data, IFA_IOMMU_PCIE_SUPPORT)) {
1103*e7629070SYong Wu #ifdef CONFIG_PCI
1104*e7629070SYong Wu 		bus_set_iommu(&pci_bus_type, NULL);
1105*e7629070SYong Wu #endif
1106d2e9a110SYong Wu 	}
1107c0b57581SYong Wu 	pm_runtime_disable(&pdev->dev);
11080df4fabeSYong Wu 	devm_free_irq(&pdev->dev, data->irq, data);
11090df4fabeSYong Wu 	return 0;
11100df4fabeSYong Wu }
11110df4fabeSYong Wu 
111234665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
11130df4fabeSYong Wu {
11140df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
11150df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
11160df4fabeSYong Wu 	void __iomem *base = data->base;
11170df4fabeSYong Wu 
111835c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
111975eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
11200df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
11210df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
11220df4fabeSYong Wu 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
11230df4fabeSYong Wu 	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
112470ca608bSYong Wu 	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
1125b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
11266254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
11270df4fabeSYong Wu 	return 0;
11280df4fabeSYong Wu }
11290df4fabeSYong Wu 
113034665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
11310df4fabeSYong Wu {
11320df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
11330df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1134907ba6a1SYong Wu 	struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
11350df4fabeSYong Wu 	void __iomem *base = data->base;
11366254b64fSYong Wu 	int ret;
11370df4fabeSYong Wu 
11386254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
11396254b64fSYong Wu 	if (ret) {
11406254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
11416254b64fSYong Wu 		return ret;
11426254b64fSYong Wu 	}
1143b34ea31fSDafna Hirschfeld 
1144b34ea31fSDafna Hirschfeld 	/*
1145b34ea31fSDafna Hirschfeld 	 * Uppon first resume, only enable the clk and return, since the values of the
1146b34ea31fSDafna Hirschfeld 	 * registers are not yet set.
1147b34ea31fSDafna Hirschfeld 	 */
1148b34ea31fSDafna Hirschfeld 	if (!m4u_dom)
1149b34ea31fSDafna Hirschfeld 		return 0;
1150b34ea31fSDafna Hirschfeld 
115135c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
115275eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
11530df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
11540df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
11550df4fabeSYong Wu 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
11560df4fabeSYong Wu 	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
115770ca608bSYong Wu 	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
1158b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
1159c0b57581SYong Wu 	writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, base + REG_MMU_PT_BASE_ADDR);
11604f23f6d4SYong Wu 
11614f23f6d4SYong Wu 	/*
11624f23f6d4SYong Wu 	 * Users may allocate dma buffer before they call pm_runtime_get,
11634f23f6d4SYong Wu 	 * in which case it will lack the necessary tlb flush.
11644f23f6d4SYong Wu 	 * Thus, make sure to update the tlb after each PM resume.
11654f23f6d4SYong Wu 	 */
11664f23f6d4SYong Wu 	mtk_iommu_tlb_flush_all(data);
11670df4fabeSYong Wu 	return 0;
11680df4fabeSYong Wu }
11690df4fabeSYong Wu 
1170e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
117134665c79SYong Wu 	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
117234665c79SYong Wu 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
117334665c79SYong Wu 				     pm_runtime_force_resume)
11740df4fabeSYong Wu };
11750df4fabeSYong Wu 
1176cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
1177cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
1178d2e9a110SYong Wu 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG | SHARE_PGTABLE |
1179d2e9a110SYong Wu 			MTK_IOMMU_TYPE_MM,
11809e3a2a64SYong Wu 	.hw_list      = &m4ulist,
1181b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1182585e58f4SYong Wu 	.iova_region  = single_domain,
1183585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
118437276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
1185cecdce9dSYong Wu };
1186cecdce9dSYong Wu 
1187068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
1188068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
1189d2e9a110SYong Wu 	.flags         = HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN | WR_THROT_EN |
1190d2e9a110SYong Wu 			 MTK_IOMMU_TYPE_MM,
1191068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
1192585e58f4SYong Wu 	.iova_region   = single_domain,
1193585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
1194068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
1195cecdce9dSYong Wu };
1196cecdce9dSYong Wu 
11973c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
11983c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
1199d2e9a110SYong Wu 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
12003c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1201585e58f4SYong Wu 	.iova_region  = single_domain,
1202585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
12033c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
12043c213562SFabien Parent };
12053c213562SFabien Parent 
1206cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
1207cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
1208d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
1209d2e9a110SYong Wu 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
1210b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1211585e58f4SYong Wu 	.iova_region  = single_domain,
1212585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
121337276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
1214cecdce9dSYong Wu };
1215cecdce9dSYong Wu 
1216907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
1217907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
1218d2e9a110SYong Wu 	.flags        = RESET_AXI | MTK_IOMMU_TYPE_MM,
1219b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1220585e58f4SYong Wu 	.iova_region  = single_domain,
1221585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
122237276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1223907ba6a1SYong Wu };
1224907ba6a1SYong Wu 
12259e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = {
12269e3489e0SYong Wu 	.m4u_plat       = M4U_MT8192,
12279ec30c09SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1228d2e9a110SYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
12299e3489e0SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
12309e3489e0SYong Wu 	.iova_region    = mt8192_multi_dom,
12319e3489e0SYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
12329e3489e0SYong Wu 	.larbid_remap   = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20},
12339e3489e0SYong Wu 			   {0, 14, 16}, {0, 13, 18, 17}},
12349e3489e0SYong Wu };
12359e3489e0SYong Wu 
12360df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
1237cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1238068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
12393c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1240cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1241907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
12429e3489e0SYong Wu 	{ .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data},
12430df4fabeSYong Wu 	{}
12440df4fabeSYong Wu };
12450df4fabeSYong Wu 
12460df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
12470df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
12480df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
12490df4fabeSYong Wu 	.driver	= {
12500df4fabeSYong Wu 		.name = "mtk-iommu",
1251f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
12520df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
12530df4fabeSYong Wu 	}
12540df4fabeSYong Wu };
125518d8c74eSYong Wu module_platform_driver(mtk_iommu_driver);
12560df4fabeSYong Wu 
125718d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations");
125818d8c74eSYong Wu MODULE_LICENSE("GPL v2");
1259