xref: /linux/drivers/iommu/mtk_iommu.c (revision 6a513de3efe099976fc5924082e48b79c467664f)
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>
17*6a513de3SYong Wu #include <linux/io-pgtable.h>
180df4fabeSYong Wu #include <linux/list.h>
19c2c59456SMiles Chen #include <linux/mfd/syscon.h>
2018d8c74eSYong Wu #include <linux/module.h>
210df4fabeSYong Wu #include <linux/of_address.h>
220df4fabeSYong Wu #include <linux/of_irq.h>
230df4fabeSYong Wu #include <linux/of_platform.h>
24e7629070SYong Wu #include <linux/pci.h>
250df4fabeSYong Wu #include <linux/platform_device.h>
26baf94e6eSYong Wu #include <linux/pm_runtime.h>
27c2c59456SMiles Chen #include <linux/regmap.h>
280df4fabeSYong Wu #include <linux/slab.h>
290df4fabeSYong Wu #include <linux/spinlock.h>
30c2c59456SMiles Chen #include <linux/soc/mediatek/infracfg.h>
310df4fabeSYong Wu #include <asm/barrier.h>
320df4fabeSYong Wu #include <soc/mediatek/smi.h>
330df4fabeSYong Wu 
34*6a513de3SYong Wu #include <dt-bindings/memory/mtk-memory-port.h>
350df4fabeSYong Wu 
360df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
37907ba6a1SYong Wu #define MMU_PT_ADDR_MASK			GENMASK(31, 7)
380df4fabeSYong Wu 
390df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
400df4fabeSYong Wu #define F_ALL_INVLD				0x2
410df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
420df4fabeSYong Wu 
430df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
440df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
450df4fabeSYong Wu 
46068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2			0x02c
47b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1			0x038
480df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
490df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
500df4fabeSYong Wu 
5175eed350SChao Hao #define REG_MMU_MISC_CTRL			0x048
524bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
534bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))
544bb2bf4cSChao Hao 
550df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
569a87005eSYong Wu #define F_MMU_DCM				BIT(8)
579a87005eSYong Wu 
5835c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5935c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
600df4fabeSYong Wu 
610df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
62acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
630df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
64acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
650df4fabeSYong Wu 
660df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6770ca608bSYong Wu 
6830e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6930e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
700df4fabeSYong Wu 
710df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
720df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
730df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
740df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
750df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
760df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
770df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
780df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
790df4fabeSYong Wu 
800df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
8115a01f4cSYong Wu 						/* mmu0 | mmu1 */
8215a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
8315a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
8415a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
8515a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8615a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8715a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8815a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
890df4fabeSYong Wu 
900df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
910df4fabeSYong Wu 
920df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
9315a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
9415a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
950df4fabeSYong Wu 
9615a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
97ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
98ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
99ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
1000df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
1010df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
1020df4fabeSYong Wu 
10315a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
10415a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
10515a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
10615a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
10715a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
10837276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10937276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
1109ec30c09SYong Wu #define F_MMU_INT_ID_COMM_ID_EXT(a)		(((a) >> 10) & 0x7)
1119ec30c09SYong Wu #define F_MMU_INT_ID_SUB_COMM_ID_EXT(a)		(((a) >> 7) & 0x7)
11215a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
11315a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
1140df4fabeSYong Wu 
115829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
1160df4fabeSYong Wu 
117f9b8c9b2SYong Wu #define PERICFG_IOMMU_1				0x714
118f9b8c9b2SYong Wu 
1196b717796SChao Hao #define HAS_4GB_MODE			BIT(0)
1206b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */
1216b717796SChao Hao #define HAS_BCLK			BIT(1)
1226b717796SChao Hao #define HAS_VLD_PA_RNG			BIT(2)
1236b717796SChao Hao #define RESET_AXI			BIT(3)
1244bb2bf4cSChao Hao #define OUT_ORDER_WR_EN			BIT(4)
1259ec30c09SYong Wu #define HAS_SUB_COMM_2BITS		BIT(5)
1269ec30c09SYong Wu #define HAS_SUB_COMM_3BITS		BIT(6)
1279ec30c09SYong Wu #define WR_THROT_EN			BIT(7)
1289ec30c09SYong Wu #define HAS_LEGACY_IVRP_PADDR		BIT(8)
1299ec30c09SYong Wu #define IOVA_34_EN			BIT(9)
1309ec30c09SYong Wu #define SHARE_PGTABLE			BIT(10) /* 2 HW share pgtable */
1319ec30c09SYong Wu #define DCM_DISABLE			BIT(11)
1329ec30c09SYong Wu #define STD_AXI_MODE			BIT(12) /* For non MM iommu */
1338cd1e619SYong Wu /* 2 bits: iommu type */
1348cd1e619SYong Wu #define MTK_IOMMU_TYPE_MM		(0x0 << 13)
1358cd1e619SYong Wu #define MTK_IOMMU_TYPE_INFRA		(0x1 << 13)
1368cd1e619SYong Wu #define MTK_IOMMU_TYPE_MASK		(0x3 << 13)
1376077c7e5SYong Wu /* PM and clock always on. e.g. infra iommu */
1386077c7e5SYong Wu #define PM_CLK_AO			BIT(15)
139e7629070SYong Wu #define IFA_IOMMU_PCIE_SUPPORT		BIT(16)
1406b717796SChao Hao 
1418cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask)	\
1428cd1e619SYong Wu 				((((pdata)->flags) & (mask)) == (_x))
1438cd1e619SYong Wu 
1448cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, _x)
1458cd1e619SYong Wu #define MTK_IOMMU_IS_TYPE(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x,\
1468cd1e619SYong Wu 							MTK_IOMMU_TYPE_MASK)
1476b717796SChao Hao 
148d2e9a110SYong Wu #define MTK_INVALID_LARBID		MTK_LARB_NR_MAX
149d2e9a110SYong Wu 
1509485a04aSYong Wu #define MTK_LARB_COM_MAX	8
1519485a04aSYong Wu #define MTK_LARB_SUBCOM_MAX	8
1529485a04aSYong Wu 
1539485a04aSYong Wu #define MTK_IOMMU_GROUP_MAX	8
1549485a04aSYong Wu 
1559485a04aSYong Wu enum mtk_iommu_plat {
1569485a04aSYong Wu 	M4U_MT2712,
1579485a04aSYong Wu 	M4U_MT6779,
1589485a04aSYong Wu 	M4U_MT8167,
1599485a04aSYong Wu 	M4U_MT8173,
1609485a04aSYong Wu 	M4U_MT8183,
1619485a04aSYong Wu 	M4U_MT8192,
1629485a04aSYong Wu 	M4U_MT8195,
1639485a04aSYong Wu };
1649485a04aSYong Wu 
1659485a04aSYong Wu struct mtk_iommu_iova_region {
1669485a04aSYong Wu 	dma_addr_t		iova_base;
1679485a04aSYong Wu 	unsigned long long	size;
1689485a04aSYong Wu };
1699485a04aSYong Wu 
170*6a513de3SYong Wu struct mtk_iommu_suspend_reg {
171*6a513de3SYong Wu 	u32			misc_ctrl;
172*6a513de3SYong Wu 	u32			dcm_dis;
173*6a513de3SYong Wu 	u32			ctrl_reg;
174*6a513de3SYong Wu 	u32			int_control0;
175*6a513de3SYong Wu 	u32			int_main_control;
176*6a513de3SYong Wu 	u32			ivrp_paddr;
177*6a513de3SYong Wu 	u32			vld_pa_rng;
178*6a513de3SYong Wu 	u32			wr_len_ctrl;
179*6a513de3SYong Wu };
180*6a513de3SYong Wu 
1819485a04aSYong Wu struct mtk_iommu_plat_data {
1829485a04aSYong Wu 	enum mtk_iommu_plat	m4u_plat;
1839485a04aSYong Wu 	u32			flags;
1849485a04aSYong Wu 	u32			inv_sel_reg;
1859485a04aSYong Wu 
1869485a04aSYong Wu 	char			*pericfg_comp_str;
1879485a04aSYong Wu 	struct list_head	*hw_list;
1889485a04aSYong Wu 	unsigned int		iova_region_nr;
1899485a04aSYong Wu 	const struct mtk_iommu_iova_region	*iova_region;
1909485a04aSYong Wu 	unsigned char       larbid_remap[MTK_LARB_COM_MAX][MTK_LARB_SUBCOM_MAX];
1919485a04aSYong Wu };
1929485a04aSYong Wu 
1939485a04aSYong Wu struct mtk_iommu_data {
1949485a04aSYong Wu 	void __iomem			*base;
1959485a04aSYong Wu 	int				irq;
1969485a04aSYong Wu 	struct device			*dev;
1979485a04aSYong Wu 	struct clk			*bclk;
1989485a04aSYong Wu 	phys_addr_t			protect_base; /* protect memory base */
1999485a04aSYong Wu 	struct mtk_iommu_suspend_reg	reg;
2009485a04aSYong Wu 	struct mtk_iommu_domain		*m4u_dom;
2019485a04aSYong Wu 	struct iommu_group		*m4u_group[MTK_IOMMU_GROUP_MAX];
2029485a04aSYong Wu 	bool                            enable_4GB;
2039485a04aSYong Wu 	spinlock_t			tlb_lock; /* lock for tlb range flush */
2049485a04aSYong Wu 
2059485a04aSYong Wu 	struct iommu_device		iommu;
2069485a04aSYong Wu 	const struct mtk_iommu_plat_data *plat_data;
2079485a04aSYong Wu 	struct device			*smicomm_dev;
2089485a04aSYong Wu 
2099485a04aSYong Wu 	struct dma_iommu_mapping	*mapping; /* For mtk_iommu_v1.c */
2109485a04aSYong Wu 	struct regmap			*pericfg;
2119485a04aSYong Wu 
2129485a04aSYong Wu 	struct mutex			mutex; /* Protect m4u_group/m4u_dom above */
2139485a04aSYong Wu 
2149485a04aSYong Wu 	/*
2159485a04aSYong Wu 	 * In the sharing pgtable case, list data->list to the global list like m4ulist.
2169485a04aSYong Wu 	 * In the non-sharing pgtable case, list data->list to the itself hw_list_head.
2179485a04aSYong Wu 	 */
2189485a04aSYong Wu 	struct list_head		*hw_list;
2199485a04aSYong Wu 	struct list_head		hw_list_head;
2209485a04aSYong Wu 	struct list_head		list;
2219485a04aSYong Wu 	struct mtk_smi_larb_iommu	larb_imu[MTK_LARB_NR_MAX];
2229485a04aSYong Wu };
2239485a04aSYong Wu 
2240df4fabeSYong Wu struct mtk_iommu_domain {
2250df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
2260df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
2270df4fabeSYong Wu 
22808500c43SYong Wu 	struct mtk_iommu_data		*data;
2290df4fabeSYong Wu 	struct iommu_domain		domain;
230ddf67a87SYong Wu 
231ddf67a87SYong Wu 	struct mutex			mutex; /* Protect "data" in this structure */
2320df4fabeSYong Wu };
2330df4fabeSYong Wu 
2349485a04aSYong Wu static int mtk_iommu_bind(struct device *dev)
2359485a04aSYong Wu {
2369485a04aSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
2379485a04aSYong Wu 
2389485a04aSYong Wu 	return component_bind_all(dev, &data->larb_imu);
2399485a04aSYong Wu }
2409485a04aSYong Wu 
2419485a04aSYong Wu static void mtk_iommu_unbind(struct device *dev)
2429485a04aSYong Wu {
2439485a04aSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
2449485a04aSYong Wu 
2459485a04aSYong Wu 	component_unbind_all(dev, &data->larb_imu);
2469485a04aSYong Wu }
2479485a04aSYong Wu 
248b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
2490df4fabeSYong Wu 
2507f37a91dSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data);
2517f37a91dSYong Wu 
252bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({					\
253bfed8731SYong Wu 	dma_addr_t _addr = iova;					\
254bfed8731SYong Wu 	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
255bfed8731SYong Wu })
256bfed8731SYong Wu 
25776ce6546SYong Wu /*
25876ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
25976ce6546SYong Wu  *
26076ce6546SYong Wu  * CPU Physical address:
26176ce6546SYong Wu  * ====================
26276ce6546SYong Wu  *
26376ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
26476ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
26576ce6546SYong Wu  * +--I/O--+------------Memory-------------+
26676ce6546SYong Wu  *
26776ce6546SYong Wu  * IOMMU output physical address:
26876ce6546SYong Wu  *  =============================
26976ce6546SYong Wu  *
27076ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
27176ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
27276ce6546SYong Wu  *                                 +------------Memory-------------+
27376ce6546SYong Wu  *
27476ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
27576ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
27676ce6546SYong Wu  * 'E', the CPU physical address keep as is.
27776ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
27876ce6546SYong Wu  */
279b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
28076ce6546SYong Wu 
2817c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
2827c3a2ec0SYong Wu 
2839e3a2a64SYong Wu #define for_each_m4u(data, head)  list_for_each_entry(data, head, list)
2847c3a2ec0SYong Wu 
285585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = {
286585e58f4SYong Wu 	{.iova_base = 0,		.size = SZ_4G},
287585e58f4SYong Wu };
288585e58f4SYong Wu 
2899e3489e0SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[] = {
290129a3b88SYong Wu 	{ .iova_base = 0x0,		.size = SZ_4G},		/* 0 ~ 4G */
2919e3489e0SYong Wu 	#if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)
292129a3b88SYong Wu 	{ .iova_base = SZ_4G,		.size = SZ_4G},		/* 4G ~ 8G */
293129a3b88SYong Wu 	{ .iova_base = SZ_4G * 2,	.size = SZ_4G},		/* 8G ~ 12G */
294129a3b88SYong Wu 	{ .iova_base = SZ_4G * 3,	.size = SZ_4G},		/* 12G ~ 16G */
295129a3b88SYong Wu 
2969e3489e0SYong Wu 	{ .iova_base = 0x240000000ULL,	.size = 0x4000000},	/* CCU0 */
2979e3489e0SYong Wu 	{ .iova_base = 0x244000000ULL,	.size = 0x4000000},	/* CCU1 */
2989e3489e0SYong Wu 	#endif
2999e3489e0SYong Wu };
3009e3489e0SYong Wu 
3019e3a2a64SYong Wu /* If 2 M4U share a domain(use the same hwlist), Put the corresponding info in first data.*/
3029e3a2a64SYong Wu static struct mtk_iommu_data *mtk_iommu_get_frst_data(struct list_head *hwlist)
3037c3a2ec0SYong Wu {
3049e3a2a64SYong Wu 	return list_first_entry(hwlist, struct mtk_iommu_data, list);
3057c3a2ec0SYong Wu }
3067c3a2ec0SYong Wu 
3070df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
3080df4fabeSYong Wu {
3090df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
3100df4fabeSYong Wu }
3110df4fabeSYong Wu 
3120954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
3130df4fabeSYong Wu {
314887cf6a7SYong Wu 	void __iomem *base = data->base;
31515672b6dSYong Wu 	unsigned long flags;
316c0b57581SYong Wu 
31715672b6dSYong Wu 	spin_lock_irqsave(&data->tlb_lock, flags);
318887cf6a7SYong Wu 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, base + data->plat_data->inv_sel_reg);
319887cf6a7SYong Wu 	writel_relaxed(F_ALL_INVLD, base + REG_MMU_INVALIDATE);
3200df4fabeSYong Wu 	wmb(); /* Make sure the tlb flush all done */
32115672b6dSYong Wu 	spin_unlock_irqrestore(&data->tlb_lock, flags);
3227c3a2ec0SYong Wu }
3230df4fabeSYong Wu 
3241f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
3250954d61aSYong Wu 					   struct mtk_iommu_data *data)
3260df4fabeSYong Wu {
3279e3a2a64SYong Wu 	struct list_head *head = data->hw_list;
3286077c7e5SYong Wu 	bool check_pm_status;
3291f4fd624SYong Wu 	unsigned long flags;
330887cf6a7SYong Wu 	void __iomem *base;
3311f4fd624SYong Wu 	int ret;
3321f4fd624SYong Wu 	u32 tmp;
3330df4fabeSYong Wu 
3349e3a2a64SYong Wu 	for_each_m4u(data, head) {
3356077c7e5SYong Wu 		/*
3366077c7e5SYong Wu 		 * To avoid resume the iommu device frequently when the iommu device
3376077c7e5SYong Wu 		 * is not active, it doesn't always call pm_runtime_get here, then tlb
3386077c7e5SYong Wu 		 * flush depends on the tlb flush all in the runtime resume.
3396077c7e5SYong Wu 		 *
3406077c7e5SYong Wu 		 * There are 2 special cases:
3416077c7e5SYong Wu 		 *
3426077c7e5SYong Wu 		 * Case1: The iommu dev doesn't have power domain but has bclk. This case
3436077c7e5SYong Wu 		 * should also avoid the tlb flush while the dev is not active to mute
3446077c7e5SYong Wu 		 * the tlb timeout log. like mt8173.
3456077c7e5SYong Wu 		 *
3466077c7e5SYong Wu 		 * Case2: The power/clock of infra iommu is always on, and it doesn't
3476077c7e5SYong Wu 		 * have the device link with the master devices. This case should avoid
3486077c7e5SYong Wu 		 * the PM status check.
3496077c7e5SYong Wu 		 */
3506077c7e5SYong Wu 		check_pm_status = !MTK_IOMMU_HAS_FLAG(data->plat_data, PM_CLK_AO);
3516077c7e5SYong Wu 
3526077c7e5SYong Wu 		if (check_pm_status) {
353c0b57581SYong Wu 			if (pm_runtime_get_if_in_use(data->dev) <= 0)
354c0b57581SYong Wu 				continue;
3556077c7e5SYong Wu 		}
356c0b57581SYong Wu 
357887cf6a7SYong Wu 		base = data->base;
358887cf6a7SYong Wu 
3591f4fd624SYong Wu 		spin_lock_irqsave(&data->tlb_lock, flags);
3607c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
361887cf6a7SYong Wu 			       base + data->plat_data->inv_sel_reg);
3620df4fabeSYong Wu 
363887cf6a7SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova), base + REG_MMU_INVLD_START_A);
364bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
365887cf6a7SYong Wu 			       base + REG_MMU_INVLD_END_A);
366887cf6a7SYong Wu 		writel_relaxed(F_MMU_INV_RANGE, base + REG_MMU_INVALIDATE);
3670df4fabeSYong Wu 
3681f4fd624SYong Wu 		/* tlb sync */
369887cf6a7SYong Wu 		ret = readl_poll_timeout_atomic(base + REG_MMU_CPE_DONE,
370c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
37115672b6dSYong Wu 
37215672b6dSYong Wu 		/* Clear the CPE status */
373887cf6a7SYong Wu 		writel_relaxed(0, base + REG_MMU_CPE_DONE);
37415672b6dSYong Wu 		spin_unlock_irqrestore(&data->tlb_lock, flags);
37515672b6dSYong Wu 
3760df4fabeSYong Wu 		if (ret) {
3770df4fabeSYong Wu 			dev_warn(data->dev,
3780df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
3790954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
3800df4fabeSYong Wu 		}
381c0b57581SYong Wu 
3826077c7e5SYong Wu 		if (check_pm_status)
383c0b57581SYong Wu 			pm_runtime_put(data->dev);
3840df4fabeSYong Wu 	}
3857c3a2ec0SYong Wu }
3860df4fabeSYong Wu 
3870df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
3880df4fabeSYong Wu {
3890df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_id;
3900df4fabeSYong Wu 	struct mtk_iommu_domain *dom = data->m4u_dom;
391d2e9a110SYong Wu 	unsigned int fault_larb = MTK_INVALID_LARBID, fault_port = 0, sub_comm = 0;
392ef0f0986SYong Wu 	u32 int_state, regval, va34_32, pa34_32;
393887cf6a7SYong Wu 	const struct mtk_iommu_plat_data *plat_data = data->plat_data;
394887cf6a7SYong Wu 	void __iomem *base = data->base;
395ef0f0986SYong Wu 	u64 fault_iova, fault_pa;
3960df4fabeSYong Wu 	bool layer, write;
3970df4fabeSYong Wu 
3980df4fabeSYong Wu 	/* Read error info from registers */
399887cf6a7SYong Wu 	int_state = readl_relaxed(base + REG_MMU_FAULT_ST1);
40015a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
401887cf6a7SYong Wu 		regval = readl_relaxed(base + REG_MMU0_INT_ID);
402887cf6a7SYong Wu 		fault_iova = readl_relaxed(base + REG_MMU0_FAULT_VA);
403887cf6a7SYong Wu 		fault_pa = readl_relaxed(base + REG_MMU0_INVLD_PA);
40415a01f4cSYong Wu 	} else {
405887cf6a7SYong Wu 		regval = readl_relaxed(base + REG_MMU1_INT_ID);
406887cf6a7SYong Wu 		fault_iova = readl_relaxed(base + REG_MMU1_FAULT_VA);
407887cf6a7SYong Wu 		fault_pa = readl_relaxed(base + REG_MMU1_INVLD_PA);
40815a01f4cSYong Wu 	}
4090df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
4100df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
411887cf6a7SYong Wu 	if (MTK_IOMMU_HAS_FLAG(plat_data, IOVA_34_EN)) {
412ef0f0986SYong Wu 		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
413ef0f0986SYong Wu 		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
414ef0f0986SYong Wu 		fault_iova |= (u64)va34_32 << 32;
415ef0f0986SYong Wu 	}
41682e51771SYong Wu 	pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
41782e51771SYong Wu 	fault_pa |= (u64)pa34_32 << 32;
418ef0f0986SYong Wu 
419887cf6a7SYong Wu 	if (MTK_IOMMU_IS_TYPE(plat_data, MTK_IOMMU_TYPE_MM)) {
42015a01f4cSYong Wu 		fault_port = F_MMU_INT_ID_PORT_ID(regval);
421887cf6a7SYong Wu 		if (MTK_IOMMU_HAS_FLAG(plat_data, HAS_SUB_COMM_2BITS)) {
42237276e00SChao Hao 			fault_larb = F_MMU_INT_ID_COMM_ID(regval);
42337276e00SChao Hao 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
424887cf6a7SYong Wu 		} else if (MTK_IOMMU_HAS_FLAG(plat_data, HAS_SUB_COMM_3BITS)) {
4259ec30c09SYong Wu 			fault_larb = F_MMU_INT_ID_COMM_ID_EXT(regval);
4269ec30c09SYong Wu 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID_EXT(regval);
42737276e00SChao Hao 		} else {
42837276e00SChao Hao 			fault_larb = F_MMU_INT_ID_LARB_ID(regval);
42937276e00SChao Hao 		}
43037276e00SChao Hao 		fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
431d2e9a110SYong Wu 	}
432b3e5eee7SYong Wu 
4330df4fabeSYong Wu 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
4340df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
4350df4fabeSYong Wu 		dev_err_ratelimited(
4360df4fabeSYong Wu 			data->dev,
437f9b8c9b2SYong Wu 			"fault type=0x%x iova=0x%llx pa=0x%llx master=0x%x(larb=%d port=%d) layer=%d %s\n",
438f9b8c9b2SYong Wu 			int_state, fault_iova, fault_pa, regval, fault_larb, fault_port,
4390df4fabeSYong Wu 			layer, write ? "write" : "read");
4400df4fabeSYong Wu 	}
4410df4fabeSYong Wu 
4420df4fabeSYong Wu 	/* Interrupt clear */
443887cf6a7SYong Wu 	regval = readl_relaxed(base + REG_MMU_INT_CONTROL0);
4440df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
445887cf6a7SYong Wu 	writel_relaxed(regval, base + REG_MMU_INT_CONTROL0);
4460df4fabeSYong Wu 
4470df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
4480df4fabeSYong Wu 
4490df4fabeSYong Wu 	return IRQ_HANDLED;
4500df4fabeSYong Wu }
4510df4fabeSYong Wu 
452803cf9e5SYong Wu static int mtk_iommu_get_domain_id(struct device *dev,
453803cf9e5SYong Wu 				   const struct mtk_iommu_plat_data *plat_data)
454803cf9e5SYong Wu {
455803cf9e5SYong Wu 	const struct mtk_iommu_iova_region *rgn = plat_data->iova_region;
456803cf9e5SYong Wu 	const struct bus_dma_region *dma_rgn = dev->dma_range_map;
457803cf9e5SYong Wu 	int i, candidate = -1;
458803cf9e5SYong Wu 	dma_addr_t dma_end;
459803cf9e5SYong Wu 
460803cf9e5SYong Wu 	if (!dma_rgn || plat_data->iova_region_nr == 1)
461803cf9e5SYong Wu 		return 0;
462803cf9e5SYong Wu 
463803cf9e5SYong Wu 	dma_end = dma_rgn->dma_start + dma_rgn->size - 1;
464803cf9e5SYong Wu 	for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) {
465803cf9e5SYong Wu 		/* Best fit. */
466803cf9e5SYong Wu 		if (dma_rgn->dma_start == rgn->iova_base &&
467803cf9e5SYong Wu 		    dma_end == rgn->iova_base + rgn->size - 1)
468803cf9e5SYong Wu 			return i;
469803cf9e5SYong Wu 		/* ok if it is inside this region. */
470803cf9e5SYong Wu 		if (dma_rgn->dma_start >= rgn->iova_base &&
471803cf9e5SYong Wu 		    dma_end < rgn->iova_base + rgn->size)
472803cf9e5SYong Wu 			candidate = i;
473803cf9e5SYong Wu 	}
474803cf9e5SYong Wu 
475803cf9e5SYong Wu 	if (candidate >= 0)
476803cf9e5SYong Wu 		return candidate;
477803cf9e5SYong Wu 	dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n",
478803cf9e5SYong Wu 		&dma_rgn->dma_start, dma_rgn->size);
479803cf9e5SYong Wu 	return -EINVAL;
480803cf9e5SYong Wu }
481803cf9e5SYong Wu 
482f9b8c9b2SYong Wu static int mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev,
4838d2c749eSYong Wu 			    bool enable, unsigned int domid)
4840df4fabeSYong Wu {
4850df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
4860df4fabeSYong Wu 	unsigned int                 larbid, portid;
487a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
4888d2c749eSYong Wu 	const struct mtk_iommu_iova_region *region;
489f9b8c9b2SYong Wu 	u32 peri_mmuen, peri_mmuen_msk;
490f9b8c9b2SYong Wu 	int i, ret = 0;
4910df4fabeSYong Wu 
49258f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
49358f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
49458f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
4958d2c749eSYong Wu 
496d2e9a110SYong Wu 		if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
4971ee9feb2SYong Wu 			larb_mmu = &data->larb_imu[larbid];
4980df4fabeSYong Wu 
4998d2c749eSYong Wu 			region = data->plat_data->iova_region + domid;
5008d2c749eSYong Wu 			larb_mmu->bank[portid] = upper_32_bits(region->iova_base);
5018d2c749eSYong Wu 
5028d2c749eSYong Wu 			dev_dbg(dev, "%s iommu for larb(%s) port %d dom %d bank %d.\n",
5038d2c749eSYong Wu 				enable ? "enable" : "disable", dev_name(larb_mmu->dev),
5048d2c749eSYong Wu 				portid, domid, larb_mmu->bank[portid]);
5050df4fabeSYong Wu 
5060df4fabeSYong Wu 			if (enable)
5070df4fabeSYong Wu 				larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
5080df4fabeSYong Wu 			else
5090df4fabeSYong Wu 				larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
510f9b8c9b2SYong Wu 		} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {
511f9b8c9b2SYong Wu 			peri_mmuen_msk = BIT(portid);
512e7629070SYong Wu 			/* PCI dev has only one output id, enable the next writing bit for PCIe */
513e7629070SYong Wu 			if (dev_is_pci(dev))
514e7629070SYong Wu 				peri_mmuen_msk |= BIT(portid + 1);
515f9b8c9b2SYong Wu 
516e7629070SYong Wu 			peri_mmuen = enable ? peri_mmuen_msk : 0;
517f9b8c9b2SYong Wu 			ret = regmap_update_bits(data->pericfg, PERICFG_IOMMU_1,
518f9b8c9b2SYong Wu 						 peri_mmuen_msk, peri_mmuen);
519f9b8c9b2SYong Wu 			if (ret)
520f9b8c9b2SYong Wu 				dev_err(dev, "%s iommu(%s) inframaster 0x%x fail(%d).\n",
521f9b8c9b2SYong Wu 					enable ? "enable" : "disable",
522f9b8c9b2SYong Wu 					dev_name(data->dev), peri_mmuen_msk, ret);
5230df4fabeSYong Wu 		}
5240df4fabeSYong Wu 	}
525f9b8c9b2SYong Wu 	return ret;
526d2e9a110SYong Wu }
5270df4fabeSYong Wu 
5284f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
529c3045f39SYong Wu 				     struct mtk_iommu_data *data,
530c3045f39SYong Wu 				     unsigned int domid)
5310df4fabeSYong Wu {
532c3045f39SYong Wu 	const struct mtk_iommu_iova_region *region;
533c3045f39SYong Wu 
534c3045f39SYong Wu 	/* Use the exist domain as there is only one pgtable here. */
535c3045f39SYong Wu 	if (data->m4u_dom) {
536c3045f39SYong Wu 		dom->iop = data->m4u_dom->iop;
537c3045f39SYong Wu 		dom->cfg = data->m4u_dom->cfg;
538c3045f39SYong Wu 		dom->domain.pgsize_bitmap = data->m4u_dom->cfg.pgsize_bitmap;
539c3045f39SYong Wu 		goto update_iova_region;
540c3045f39SYong Wu 	}
541c3045f39SYong Wu 
5420df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
5430df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
5440df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
545b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
5460df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
5472f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
5480df4fabeSYong Wu 		.iommu_dev = data->dev,
5490df4fabeSYong Wu 	};
5500df4fabeSYong Wu 
5519bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
5529bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
5539bdfe4c1SYong Wu 	else
5549bdfe4c1SYong Wu 		dom->cfg.oas = 35;
5559bdfe4c1SYong Wu 
5560df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
5570df4fabeSYong Wu 	if (!dom->iop) {
5580df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
5590df4fabeSYong Wu 		return -EINVAL;
5600df4fabeSYong Wu 	}
5610df4fabeSYong Wu 
5620df4fabeSYong Wu 	/* Update our support page sizes bitmap */
563d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
564b7875eb9SYong Wu 
565c3045f39SYong Wu update_iova_region:
566c3045f39SYong Wu 	/* Update the iova region for this domain */
567c3045f39SYong Wu 	region = data->plat_data->iova_region + domid;
568c3045f39SYong Wu 	dom->domain.geometry.aperture_start = region->iova_base;
569c3045f39SYong Wu 	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
570b7875eb9SYong Wu 	dom->domain.geometry.force_aperture = true;
5710df4fabeSYong Wu 	return 0;
5720df4fabeSYong Wu }
5730df4fabeSYong Wu 
5740df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
5750df4fabeSYong Wu {
5760df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
5770df4fabeSYong Wu 
57832e1cccfSYong Wu 	if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED)
5790df4fabeSYong Wu 		return NULL;
5800df4fabeSYong Wu 
5810df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
5820df4fabeSYong Wu 	if (!dom)
5830df4fabeSYong Wu 		return NULL;
584ddf67a87SYong Wu 	mutex_init(&dom->mutex);
5850df4fabeSYong Wu 
5864f956c97SYong Wu 	return &dom->domain;
5874f956c97SYong Wu }
5884f956c97SYong Wu 
5890df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
5900df4fabeSYong Wu {
5910df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
5920df4fabeSYong Wu }
5930df4fabeSYong Wu 
5940df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
5950df4fabeSYong Wu 				   struct device *dev)
5960df4fabeSYong Wu {
597645b87c1SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev), *frstdata;
5980df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
5999e3a2a64SYong Wu 	struct list_head *hw_list = data->hw_list;
600c0b57581SYong Wu 	struct device *m4udev = data->dev;
601803cf9e5SYong Wu 	int ret, domid;
6020df4fabeSYong Wu 
603803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
604803cf9e5SYong Wu 	if (domid < 0)
605803cf9e5SYong Wu 		return domid;
606803cf9e5SYong Wu 
607ddf67a87SYong Wu 	mutex_lock(&dom->mutex);
6084f956c97SYong Wu 	if (!dom->data) {
609645b87c1SYong Wu 		/* Data is in the frstdata in sharing pgtable case. */
6109e3a2a64SYong Wu 		frstdata = mtk_iommu_get_frst_data(hw_list);
611645b87c1SYong Wu 
612ddf67a87SYong Wu 		ret = mtk_iommu_domain_finalise(dom, frstdata, domid);
613ddf67a87SYong Wu 		if (ret) {
614ddf67a87SYong Wu 			mutex_unlock(&dom->mutex);
6154f956c97SYong Wu 			return -ENODEV;
616ddf67a87SYong Wu 		}
6174f956c97SYong Wu 		dom->data = data;
6184f956c97SYong Wu 	}
619ddf67a87SYong Wu 	mutex_unlock(&dom->mutex);
6204f956c97SYong Wu 
6210e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
6227f37a91dSYong Wu 	if (!data->m4u_dom) { /* Initialize the M4U HW */
623c0b57581SYong Wu 		ret = pm_runtime_resume_and_get(m4udev);
624c0b57581SYong Wu 		if (ret < 0)
6250e5a3f2eSYong Wu 			goto err_unlock;
626c0b57581SYong Wu 
627c0b57581SYong Wu 		ret = mtk_iommu_hw_init(data);
628c0b57581SYong Wu 		if (ret) {
629c0b57581SYong Wu 			pm_runtime_put(m4udev);
6300e5a3f2eSYong Wu 			goto err_unlock;
631c0b57581SYong Wu 		}
6320df4fabeSYong Wu 		data->m4u_dom = dom;
633d1e5f26fSRobin Murphy 		writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
6344b00f5acSYong Wu 		       data->base + REG_MMU_PT_BASE_ADDR);
635c0b57581SYong Wu 
636c0b57581SYong Wu 		pm_runtime_put(m4udev);
6370df4fabeSYong Wu 	}
6380e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
6390df4fabeSYong Wu 
640f9b8c9b2SYong Wu 	return mtk_iommu_config(data, dev, true, domid);
6410e5a3f2eSYong Wu 
6420e5a3f2eSYong Wu err_unlock:
6430e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
6440e5a3f2eSYong Wu 	return ret;
6450df4fabeSYong Wu }
6460df4fabeSYong Wu 
6470df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
6480df4fabeSYong Wu 				    struct device *dev)
6490df4fabeSYong Wu {
6503524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
6510df4fabeSYong Wu 
6528d2c749eSYong Wu 	mtk_iommu_config(data, dev, false, 0);
6530df4fabeSYong Wu }
6540df4fabeSYong Wu 
6550df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
656781ca2deSTom Murphy 			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
6570df4fabeSYong Wu {
6580df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
6590df4fabeSYong Wu 
660b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
66108500c43SYong Wu 	if (dom->data->enable_4GB)
662b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
663b4dad40eSYong Wu 
66460829b4dSYong Wu 	/* Synchronize with the tlb_lock */
665f34ce7a7SBaolin Wang 	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
6660df4fabeSYong Wu }
6670df4fabeSYong Wu 
6680df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
66956f8af5eSWill Deacon 			      unsigned long iova, size_t size,
67056f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
6710df4fabeSYong Wu {
6720df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
6730df4fabeSYong Wu 
6743136895cSRobin Murphy 	iommu_iotlb_gather_add_range(gather, iova, size);
67560829b4dSYong Wu 	return dom->iop->unmap(dom->iop, iova, size, gather);
6760df4fabeSYong Wu }
6770df4fabeSYong Wu 
67856f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
67956f8af5eSWill Deacon {
68008500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
68108500c43SYong Wu 
68208500c43SYong Wu 	mtk_iommu_tlb_flush_all(dom->data);
68356f8af5eSWill Deacon }
68456f8af5eSWill Deacon 
68556f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
68656f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
6874d689b61SRobin Murphy {
68808500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
689862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
690da3cc91bSYong Wu 
691e6d25e7dSYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, dom->data);
6924d689b61SRobin Murphy }
6934d689b61SRobin Murphy 
69420143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
69520143451SYong Wu 			       size_t size)
69620143451SYong Wu {
69708500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
69820143451SYong Wu 
699e6d25e7dSYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, dom->data);
70020143451SYong Wu }
70120143451SYong Wu 
7020df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
7030df4fabeSYong Wu 					  dma_addr_t iova)
7040df4fabeSYong Wu {
7050df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
7060df4fabeSYong Wu 	phys_addr_t pa;
7070df4fabeSYong Wu 
7080df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
709f13efafcSArnd Bergmann 	if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
710f13efafcSArnd Bergmann 	    dom->data->enable_4GB &&
711f13efafcSArnd Bergmann 	    pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
712b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
71330e2fccfSYong Wu 
7140df4fabeSYong Wu 	return pa;
7150df4fabeSYong Wu }
7160df4fabeSYong Wu 
71780e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
7180df4fabeSYong Wu {
719a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
720b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
721635319a4SYong Wu 	struct device_link *link;
722635319a4SYong Wu 	struct device *larbdev;
723635319a4SYong Wu 	unsigned int larbid, larbidx, i;
7240df4fabeSYong Wu 
725a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
72680e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
7270df4fabeSYong Wu 
7283524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
729b16c0170SJoerg Roedel 
730d2e9a110SYong Wu 	if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
731d2e9a110SYong Wu 		return &data->iommu;
732d2e9a110SYong Wu 
733635319a4SYong Wu 	/*
734635319a4SYong Wu 	 * Link the consumer device with the smi-larb device(supplier).
735635319a4SYong Wu 	 * The device that connects with each a larb is a independent HW.
736635319a4SYong Wu 	 * All the ports in each a device should be in the same larbs.
737635319a4SYong Wu 	 */
738635319a4SYong Wu 	larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
739635319a4SYong Wu 	for (i = 1; i < fwspec->num_ids; i++) {
740635319a4SYong Wu 		larbidx = MTK_M4U_TO_LARB(fwspec->ids[i]);
741635319a4SYong Wu 		if (larbid != larbidx) {
742635319a4SYong Wu 			dev_err(dev, "Can only use one larb. Fail@larb%d-%d.\n",
743635319a4SYong Wu 				larbid, larbidx);
744635319a4SYong Wu 			return ERR_PTR(-EINVAL);
745635319a4SYong Wu 		}
746635319a4SYong Wu 	}
747635319a4SYong Wu 	larbdev = data->larb_imu[larbid].dev;
748635319a4SYong Wu 	link = device_link_add(dev, larbdev,
749635319a4SYong Wu 			       DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
750635319a4SYong Wu 	if (!link)
751635319a4SYong Wu 		dev_err(dev, "Unable to link %s\n", dev_name(larbdev));
75280e4592aSJoerg Roedel 	return &data->iommu;
7530df4fabeSYong Wu }
7540df4fabeSYong Wu 
75580e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
7560df4fabeSYong Wu {
757a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
758635319a4SYong Wu 	struct mtk_iommu_data *data;
759635319a4SYong Wu 	struct device *larbdev;
760635319a4SYong Wu 	unsigned int larbid;
761b16c0170SJoerg Roedel 
762a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
7630df4fabeSYong Wu 		return;
7640df4fabeSYong Wu 
765635319a4SYong Wu 	data = dev_iommu_priv_get(dev);
766d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
767635319a4SYong Wu 		larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
768635319a4SYong Wu 		larbdev = data->larb_imu[larbid].dev;
769635319a4SYong Wu 		device_link_remove(dev, larbdev);
770d2e9a110SYong Wu 	}
771635319a4SYong Wu 
77258f0d1d5SRobin Murphy 	iommu_fwspec_free(dev);
7730df4fabeSYong Wu }
7740df4fabeSYong Wu 
7750df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
7760df4fabeSYong Wu {
7779e3a2a64SYong Wu 	struct mtk_iommu_data *c_data = dev_iommu_priv_get(dev), *data;
7789e3a2a64SYong Wu 	struct list_head *hw_list = c_data->hw_list;
779c3045f39SYong Wu 	struct iommu_group *group;
780803cf9e5SYong Wu 	int domid;
7810df4fabeSYong Wu 
7829e3a2a64SYong Wu 	data = mtk_iommu_get_frst_data(hw_list);
78358f0d1d5SRobin Murphy 	if (!data)
7840df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
7850df4fabeSYong Wu 
786803cf9e5SYong Wu 	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
787803cf9e5SYong Wu 	if (domid < 0)
788803cf9e5SYong Wu 		return ERR_PTR(domid);
789803cf9e5SYong Wu 
7900e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
791c3045f39SYong Wu 	group = data->m4u_group[domid];
792c3045f39SYong Wu 	if (!group) {
793c3045f39SYong Wu 		group = iommu_group_alloc();
794c3045f39SYong Wu 		if (!IS_ERR(group))
795c3045f39SYong Wu 			data->m4u_group[domid] = group;
7963a8d40b6SRobin Murphy 	} else {
797c3045f39SYong Wu 		iommu_group_ref_get(group);
7980df4fabeSYong Wu 	}
7990e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
800c3045f39SYong Wu 	return group;
8010df4fabeSYong Wu }
8020df4fabeSYong Wu 
8030df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
8040df4fabeSYong Wu {
8050df4fabeSYong Wu 	struct platform_device *m4updev;
8060df4fabeSYong Wu 
8070df4fabeSYong Wu 	if (args->args_count != 1) {
8080df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
8090df4fabeSYong Wu 			args->args_count);
8100df4fabeSYong Wu 		return -EINVAL;
8110df4fabeSYong Wu 	}
8120df4fabeSYong Wu 
8133524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
8140df4fabeSYong Wu 		/* Get the m4u device */
8150df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
8160df4fabeSYong Wu 		if (WARN_ON(!m4updev))
8170df4fabeSYong Wu 			return -EINVAL;
8180df4fabeSYong Wu 
8193524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
8200df4fabeSYong Wu 	}
8210df4fabeSYong Wu 
82258f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
8230df4fabeSYong Wu }
8240df4fabeSYong Wu 
825ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev,
826ab1d5281SYong Wu 				       struct list_head *head)
827ab1d5281SYong Wu {
828ab1d5281SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
829ab1d5281SYong Wu 	unsigned int domid = mtk_iommu_get_domain_id(dev, data->plat_data), i;
830ab1d5281SYong Wu 	const struct mtk_iommu_iova_region *resv, *curdom;
831ab1d5281SYong Wu 	struct iommu_resv_region *region;
832ab1d5281SYong Wu 	int prot = IOMMU_WRITE | IOMMU_READ;
833ab1d5281SYong Wu 
8347a566173SColin Ian King 	if ((int)domid < 0)
835ab1d5281SYong Wu 		return;
836ab1d5281SYong Wu 	curdom = data->plat_data->iova_region + domid;
837ab1d5281SYong Wu 	for (i = 0; i < data->plat_data->iova_region_nr; i++) {
838ab1d5281SYong Wu 		resv = data->plat_data->iova_region + i;
839ab1d5281SYong Wu 
840ab1d5281SYong Wu 		/* Only reserve when the region is inside the current domain */
841ab1d5281SYong Wu 		if (resv->iova_base <= curdom->iova_base ||
842ab1d5281SYong Wu 		    resv->iova_base + resv->size >= curdom->iova_base + curdom->size)
843ab1d5281SYong Wu 			continue;
844ab1d5281SYong Wu 
845ab1d5281SYong Wu 		region = iommu_alloc_resv_region(resv->iova_base, resv->size,
846ab1d5281SYong Wu 						 prot, IOMMU_RESV_RESERVED);
847ab1d5281SYong Wu 		if (!region)
848ab1d5281SYong Wu 			return;
849ab1d5281SYong Wu 
850ab1d5281SYong Wu 		list_add_tail(&region->list, head);
851ab1d5281SYong Wu 	}
852ab1d5281SYong Wu }
853ab1d5281SYong Wu 
854b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
8550df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
85680e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
85780e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
8580df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
8590df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
860ab1d5281SYong Wu 	.get_resv_regions = mtk_iommu_get_resv_regions,
861ab1d5281SYong Wu 	.put_resv_regions = generic_iommu_put_resv_regions,
8620df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
86318d8c74eSYong Wu 	.owner		= THIS_MODULE,
8649a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
8659a630a4bSLu Baolu 		.attach_dev	= mtk_iommu_attach_device,
8669a630a4bSLu Baolu 		.detach_dev	= mtk_iommu_detach_device,
8679a630a4bSLu Baolu 		.map		= mtk_iommu_map,
8689a630a4bSLu Baolu 		.unmap		= mtk_iommu_unmap,
8699a630a4bSLu Baolu 		.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
8709a630a4bSLu Baolu 		.iotlb_sync	= mtk_iommu_iotlb_sync,
8719a630a4bSLu Baolu 		.iotlb_sync_map	= mtk_iommu_sync_map,
8729a630a4bSLu Baolu 		.iova_to_phys	= mtk_iommu_iova_to_phys,
8739a630a4bSLu Baolu 		.free		= mtk_iommu_domain_free,
8749a630a4bSLu Baolu 	}
8750df4fabeSYong Wu };
8760df4fabeSYong Wu 
8770df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
8780df4fabeSYong Wu {
8790df4fabeSYong Wu 	u32 regval;
8800df4fabeSYong Wu 
88186444413SChao Hao 	if (data->plat_data->m4u_plat == M4U_MT8173) {
882acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
883acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
88486444413SChao Hao 	} else {
88586444413SChao Hao 		regval = readl_relaxed(data->base + REG_MMU_CTRL_REG);
88686444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
88786444413SChao Hao 	}
8880df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
8890df4fabeSYong Wu 
8906b717796SChao Hao 	if (data->enable_4GB &&
8916b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
89230e2fccfSYong Wu 		/*
89330e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
89430e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
89530e2fccfSYong Wu 		 */
89630e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
89730e2fccfSYong Wu 		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
89830e2fccfSYong Wu 	}
8999a87005eSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, DCM_DISABLE))
9009a87005eSYong Wu 		writel_relaxed(F_MMU_DCM, data->base + REG_MMU_DCM_DIS);
9019a87005eSYong Wu 	else
9020df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
9039a87005eSYong Wu 
90435c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
90535c1b48dSChao Hao 		/* write command throttling mode */
90635c1b48dSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL);
90735c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
90835c1b48dSChao Hao 		writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL);
90935c1b48dSChao Hao 	}
910e6dec923SYong Wu 
9116b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
91275eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
9134bb2bf4cSChao Hao 		regval = 0;
9144bb2bf4cSChao Hao 	} else {
9154bb2bf4cSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
916d265a4adSYong Wu 		if (!MTK_IOMMU_HAS_FLAG(data->plat_data, STD_AXI_MODE))
9174bb2bf4cSChao Hao 			regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
9184bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
9194bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
92075eed350SChao Hao 	}
9214bb2bf4cSChao Hao 	writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
9220df4fabeSYong Wu 
923634f57dfSYong Wu 	regval = F_L2_MULIT_HIT_EN |
924634f57dfSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
925634f57dfSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
926634f57dfSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
927634f57dfSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
928634f57dfSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
929634f57dfSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
930634f57dfSYong Wu 
931634f57dfSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
932634f57dfSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
933634f57dfSYong Wu 		F_INT_INVALID_PA_FAULT |
934634f57dfSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
935634f57dfSYong Wu 		F_INT_TLB_MISS_FAULT |
936634f57dfSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
937634f57dfSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
938634f57dfSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
939634f57dfSYong Wu 
940634f57dfSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
941634f57dfSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
942634f57dfSYong Wu 	else
943634f57dfSYong Wu 		regval = lower_32_bits(data->protect_base) |
944634f57dfSYong Wu 			 upper_32_bits(data->protect_base);
945634f57dfSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
946634f57dfSYong Wu 
9470df4fabeSYong Wu 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
9480df4fabeSYong Wu 			     dev_name(data->dev), (void *)data)) {
9490df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
9500df4fabeSYong Wu 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
9510df4fabeSYong Wu 		return -ENODEV;
9520df4fabeSYong Wu 	}
9530df4fabeSYong Wu 
9540df4fabeSYong Wu 	return 0;
9550df4fabeSYong Wu }
9560df4fabeSYong Wu 
9570df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
9580df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
9590df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
9600df4fabeSYong Wu };
9610df4fabeSYong Wu 
962d2e9a110SYong Wu static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **match,
963d2e9a110SYong Wu 				  struct mtk_iommu_data *data)
964d2e9a110SYong Wu {
965f7b71d0dSYong Wu 	struct device_node *larbnode, *smicomm_node, *smi_subcomm_node;
966d2e9a110SYong Wu 	struct platform_device *plarbdev;
967d2e9a110SYong Wu 	struct device_link *link;
968d2e9a110SYong Wu 	int i, larb_nr, ret;
969d2e9a110SYong Wu 
970d2e9a110SYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL);
971d2e9a110SYong Wu 	if (larb_nr < 0)
972d2e9a110SYong Wu 		return larb_nr;
973d2e9a110SYong Wu 
974d2e9a110SYong Wu 	for (i = 0; i < larb_nr; i++) {
975d2e9a110SYong Wu 		u32 id;
976d2e9a110SYong Wu 
977d2e9a110SYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
978d2e9a110SYong Wu 		if (!larbnode)
979d2e9a110SYong Wu 			return -EINVAL;
980d2e9a110SYong Wu 
981d2e9a110SYong Wu 		if (!of_device_is_available(larbnode)) {
982d2e9a110SYong Wu 			of_node_put(larbnode);
983d2e9a110SYong Wu 			continue;
984d2e9a110SYong Wu 		}
985d2e9a110SYong Wu 
986d2e9a110SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
987d2e9a110SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
988d2e9a110SYong Wu 			id = i;
989d2e9a110SYong Wu 
990d2e9a110SYong Wu 		plarbdev = of_find_device_by_node(larbnode);
991d2e9a110SYong Wu 		if (!plarbdev) {
992d2e9a110SYong Wu 			of_node_put(larbnode);
993d2e9a110SYong Wu 			return -ENODEV;
994d2e9a110SYong Wu 		}
995d2e9a110SYong Wu 		if (!plarbdev->dev.driver) {
996d2e9a110SYong Wu 			of_node_put(larbnode);
997d2e9a110SYong Wu 			return -EPROBE_DEFER;
998d2e9a110SYong Wu 		}
999d2e9a110SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
1000d2e9a110SYong Wu 
1001d2e9a110SYong Wu 		component_match_add_release(dev, match, component_release_of,
1002d2e9a110SYong Wu 					    component_compare_of, larbnode);
1003d2e9a110SYong Wu 	}
1004d2e9a110SYong Wu 
1005f7b71d0dSYong Wu 	/* Get smi-(sub)-common dev from the last larb. */
1006f7b71d0dSYong Wu 	smi_subcomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
1007f7b71d0dSYong Wu 	if (!smi_subcomm_node)
1008d2e9a110SYong Wu 		return -EINVAL;
1009d2e9a110SYong Wu 
1010f7b71d0dSYong Wu 	/*
1011f7b71d0dSYong Wu 	 * It may have two level smi-common. the node is smi-sub-common if it
1012f7b71d0dSYong Wu 	 * has a new mediatek,smi property. otherwise it is smi-commmon.
1013f7b71d0dSYong Wu 	 */
1014f7b71d0dSYong Wu 	smicomm_node = of_parse_phandle(smi_subcomm_node, "mediatek,smi", 0);
1015f7b71d0dSYong Wu 	if (smicomm_node)
1016f7b71d0dSYong Wu 		of_node_put(smi_subcomm_node);
1017f7b71d0dSYong Wu 	else
1018f7b71d0dSYong Wu 		smicomm_node = smi_subcomm_node;
1019f7b71d0dSYong Wu 
1020d2e9a110SYong Wu 	plarbdev = of_find_device_by_node(smicomm_node);
1021d2e9a110SYong Wu 	of_node_put(smicomm_node);
1022d2e9a110SYong Wu 	data->smicomm_dev = &plarbdev->dev;
1023d2e9a110SYong Wu 
1024d2e9a110SYong Wu 	link = device_link_add(data->smicomm_dev, dev,
1025d2e9a110SYong Wu 			       DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
1026d2e9a110SYong Wu 	if (!link) {
1027d2e9a110SYong Wu 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
1028d2e9a110SYong Wu 		return -EINVAL;
1029d2e9a110SYong Wu 	}
1030d2e9a110SYong Wu 	return 0;
1031d2e9a110SYong Wu }
1032d2e9a110SYong Wu 
10330df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
10340df4fabeSYong Wu {
10350df4fabeSYong Wu 	struct mtk_iommu_data   *data;
10360df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
10370df4fabeSYong Wu 	struct resource         *res;
1038b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
10390df4fabeSYong Wu 	struct component_match  *match = NULL;
1040c2c59456SMiles Chen 	struct regmap		*infracfg;
10410df4fabeSYong Wu 	void                    *protect;
1042d2e9a110SYong Wu 	int                     ret;
1043c2c59456SMiles Chen 	u32			val;
1044c2c59456SMiles Chen 	char                    *p;
10450df4fabeSYong Wu 
10460df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
10470df4fabeSYong Wu 	if (!data)
10480df4fabeSYong Wu 		return -ENOMEM;
10490df4fabeSYong Wu 	data->dev = dev;
1050cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
10510df4fabeSYong Wu 
10520df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
10530df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
10540df4fabeSYong Wu 	if (!protect)
10550df4fabeSYong Wu 		return -ENOMEM;
10560df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
10570df4fabeSYong Wu 
1058c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
1059c2c59456SMiles Chen 		switch (data->plat_data->m4u_plat) {
1060c2c59456SMiles Chen 		case M4U_MT2712:
1061c2c59456SMiles Chen 			p = "mediatek,mt2712-infracfg";
1062c2c59456SMiles Chen 			break;
1063c2c59456SMiles Chen 		case M4U_MT8173:
1064c2c59456SMiles Chen 			p = "mediatek,mt8173-infracfg";
1065c2c59456SMiles Chen 			break;
1066c2c59456SMiles Chen 		default:
1067c2c59456SMiles Chen 			p = NULL;
1068c2c59456SMiles Chen 		}
1069c2c59456SMiles Chen 
1070c2c59456SMiles Chen 		infracfg = syscon_regmap_lookup_by_compatible(p);
1071c2c59456SMiles Chen 
1072c2c59456SMiles Chen 		if (IS_ERR(infracfg))
1073c2c59456SMiles Chen 			return PTR_ERR(infracfg);
1074c2c59456SMiles Chen 
1075c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
1076c2c59456SMiles Chen 		if (ret)
1077c2c59456SMiles Chen 			return ret;
1078c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
1079c2c59456SMiles Chen 	}
108001e23c93SYong Wu 
10810df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
10820df4fabeSYong Wu 	data->base = devm_ioremap_resource(dev, res);
10830df4fabeSYong Wu 	if (IS_ERR(data->base))
10840df4fabeSYong Wu 		return PTR_ERR(data->base);
1085b16c0170SJoerg Roedel 	ioaddr = res->start;
10860df4fabeSYong Wu 
10870df4fabeSYong Wu 	data->irq = platform_get_irq(pdev, 0);
10880df4fabeSYong Wu 	if (data->irq < 0)
10890df4fabeSYong Wu 		return data->irq;
10900df4fabeSYong Wu 
10916b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
10920df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
10930df4fabeSYong Wu 		if (IS_ERR(data->bclk))
10940df4fabeSYong Wu 			return PTR_ERR(data->bclk);
10952aa4c259SYong Wu 	}
10960df4fabeSYong Wu 
1097c0b57581SYong Wu 	pm_runtime_enable(dev);
1098c0b57581SYong Wu 
1099d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1100d2e9a110SYong Wu 		ret = mtk_iommu_mm_dts_parse(dev, &match, data);
1101d2e9a110SYong Wu 		if (ret) {
1102d2e9a110SYong Wu 			dev_err(dev, "mm dts parse fail(%d).", ret);
1103c0b57581SYong Wu 			goto out_runtime_disable;
1104baf94e6eSYong Wu 		}
1105f9b8c9b2SYong Wu 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&
1106f9b8c9b2SYong Wu 		   data->plat_data->pericfg_comp_str) {
1107f9b8c9b2SYong Wu 		infracfg = syscon_regmap_lookup_by_compatible(data->plat_data->pericfg_comp_str);
1108f9b8c9b2SYong Wu 		if (IS_ERR(infracfg)) {
1109f9b8c9b2SYong Wu 			ret = PTR_ERR(infracfg);
1110f9b8c9b2SYong Wu 			goto out_runtime_disable;
1111f9b8c9b2SYong Wu 		}
1112f9b8c9b2SYong Wu 
1113f9b8c9b2SYong Wu 		data->pericfg = infracfg;
1114d2e9a110SYong Wu 	}
1115baf94e6eSYong Wu 
11160df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
11170e5a3f2eSYong Wu 	mutex_init(&data->mutex);
11180df4fabeSYong Wu 
1119b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
1120b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
1121b16c0170SJoerg Roedel 	if (ret)
1122baf94e6eSYong Wu 		goto out_link_remove;
1123b16c0170SJoerg Roedel 
11242d471b20SRobin Murphy 	ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
1125b16c0170SJoerg Roedel 	if (ret)
1126986d9ec5SYong Wu 		goto out_sysfs_remove;
1127b16c0170SJoerg Roedel 
1128da3cc91bSYong Wu 	spin_lock_init(&data->tlb_lock);
11299e3a2a64SYong Wu 
11309e3a2a64SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, SHARE_PGTABLE)) {
11319e3a2a64SYong Wu 		list_add_tail(&data->list, data->plat_data->hw_list);
11329e3a2a64SYong Wu 		data->hw_list = data->plat_data->hw_list;
11339e3a2a64SYong Wu 	} else {
11349e3a2a64SYong Wu 		INIT_LIST_HEAD(&data->hw_list_head);
11359e3a2a64SYong Wu 		list_add_tail(&data->list, &data->hw_list_head);
11369e3a2a64SYong Wu 		data->hw_list = &data->hw_list_head;
11379e3a2a64SYong Wu 	}
11387c3a2ec0SYong Wu 
1139986d9ec5SYong Wu 	if (!iommu_present(&platform_bus_type)) {
1140986d9ec5SYong Wu 		ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
1141986d9ec5SYong Wu 		if (ret)
1142986d9ec5SYong Wu 			goto out_list_del;
1143986d9ec5SYong Wu 	}
11440df4fabeSYong Wu 
1145d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1146986d9ec5SYong Wu 		ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
1147986d9ec5SYong Wu 		if (ret)
1148986d9ec5SYong Wu 			goto out_bus_set_null;
1149e7629070SYong Wu 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&
1150e7629070SYong Wu 		   MTK_IOMMU_HAS_FLAG(data->plat_data, IFA_IOMMU_PCIE_SUPPORT)) {
1151e7629070SYong Wu #ifdef CONFIG_PCI
1152e7629070SYong Wu 		if (!iommu_present(&pci_bus_type)) {
1153e7629070SYong Wu 			ret = bus_set_iommu(&pci_bus_type, &mtk_iommu_ops);
1154e7629070SYong Wu 			if (ret) /* PCIe fail don't affect platform_bus. */
1155e7629070SYong Wu 				goto out_list_del;
1156e7629070SYong Wu 		}
1157e7629070SYong Wu #endif
1158d2e9a110SYong Wu 	}
1159986d9ec5SYong Wu 	return ret;
1160986d9ec5SYong Wu 
1161986d9ec5SYong Wu out_bus_set_null:
1162986d9ec5SYong Wu 	bus_set_iommu(&platform_bus_type, NULL);
1163986d9ec5SYong Wu out_list_del:
1164986d9ec5SYong Wu 	list_del(&data->list);
1165986d9ec5SYong Wu 	iommu_device_unregister(&data->iommu);
1166986d9ec5SYong Wu out_sysfs_remove:
1167986d9ec5SYong Wu 	iommu_device_sysfs_remove(&data->iommu);
1168baf94e6eSYong Wu out_link_remove:
1169d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
1170baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, dev);
1171c0b57581SYong Wu out_runtime_disable:
1172c0b57581SYong Wu 	pm_runtime_disable(dev);
1173986d9ec5SYong Wu 	return ret;
11740df4fabeSYong Wu }
11750df4fabeSYong Wu 
11760df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
11770df4fabeSYong Wu {
11780df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
11790df4fabeSYong Wu 
1180b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
1181b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
1182b16c0170SJoerg Roedel 
1183ee55f75eSYong Wu 	list_del(&data->list);
11840df4fabeSYong Wu 
1185d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1186baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, &pdev->dev);
1187d2e9a110SYong Wu 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
1188e7629070SYong Wu 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA) &&
1189e7629070SYong Wu 		   MTK_IOMMU_HAS_FLAG(data->plat_data, IFA_IOMMU_PCIE_SUPPORT)) {
1190e7629070SYong Wu #ifdef CONFIG_PCI
1191e7629070SYong Wu 		bus_set_iommu(&pci_bus_type, NULL);
1192e7629070SYong Wu #endif
1193d2e9a110SYong Wu 	}
1194c0b57581SYong Wu 	pm_runtime_disable(&pdev->dev);
11950df4fabeSYong Wu 	devm_free_irq(&pdev->dev, data->irq, data);
11960df4fabeSYong Wu 	return 0;
11970df4fabeSYong Wu }
11980df4fabeSYong Wu 
119934665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
12000df4fabeSYong Wu {
12010df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
12020df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
12030df4fabeSYong Wu 	void __iomem *base = data->base;
12040df4fabeSYong Wu 
120535c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
120675eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
12070df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
12080df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
12090df4fabeSYong Wu 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
12100df4fabeSYong Wu 	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
121170ca608bSYong Wu 	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
1212b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
12136254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
12140df4fabeSYong Wu 	return 0;
12150df4fabeSYong Wu }
12160df4fabeSYong Wu 
121734665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
12180df4fabeSYong Wu {
12190df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
12200df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1221907ba6a1SYong Wu 	struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
12220df4fabeSYong Wu 	void __iomem *base = data->base;
12236254b64fSYong Wu 	int ret;
12240df4fabeSYong Wu 
12256254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
12266254b64fSYong Wu 	if (ret) {
12276254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
12286254b64fSYong Wu 		return ret;
12296254b64fSYong Wu 	}
1230b34ea31fSDafna Hirschfeld 
1231b34ea31fSDafna Hirschfeld 	/*
1232b34ea31fSDafna Hirschfeld 	 * Uppon first resume, only enable the clk and return, since the values of the
1233b34ea31fSDafna Hirschfeld 	 * registers are not yet set.
1234b34ea31fSDafna Hirschfeld 	 */
1235b34ea31fSDafna Hirschfeld 	if (!m4u_dom)
1236b34ea31fSDafna Hirschfeld 		return 0;
1237b34ea31fSDafna Hirschfeld 
123835c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
123975eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
12400df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
12410df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
12420df4fabeSYong Wu 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
12430df4fabeSYong Wu 	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
124470ca608bSYong Wu 	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
1245b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
1246c0b57581SYong Wu 	writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, base + REG_MMU_PT_BASE_ADDR);
12474f23f6d4SYong Wu 
12484f23f6d4SYong Wu 	/*
12494f23f6d4SYong Wu 	 * Users may allocate dma buffer before they call pm_runtime_get,
12504f23f6d4SYong Wu 	 * in which case it will lack the necessary tlb flush.
12514f23f6d4SYong Wu 	 * Thus, make sure to update the tlb after each PM resume.
12524f23f6d4SYong Wu 	 */
12534f23f6d4SYong Wu 	mtk_iommu_tlb_flush_all(data);
12540df4fabeSYong Wu 	return 0;
12550df4fabeSYong Wu }
12560df4fabeSYong Wu 
1257e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
125834665c79SYong Wu 	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
125934665c79SYong Wu 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
126034665c79SYong Wu 				     pm_runtime_force_resume)
12610df4fabeSYong Wu };
12620df4fabeSYong Wu 
1263cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
1264cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
1265d2e9a110SYong Wu 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG | SHARE_PGTABLE |
1266d2e9a110SYong Wu 			MTK_IOMMU_TYPE_MM,
12679e3a2a64SYong Wu 	.hw_list      = &m4ulist,
1268b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1269585e58f4SYong Wu 	.iova_region  = single_domain,
1270585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
127137276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
1272cecdce9dSYong Wu };
1273cecdce9dSYong Wu 
1274068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
1275068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
1276d2e9a110SYong Wu 	.flags         = HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN | WR_THROT_EN |
1277d2e9a110SYong Wu 			 MTK_IOMMU_TYPE_MM,
1278068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
1279585e58f4SYong Wu 	.iova_region   = single_domain,
1280585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
1281068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
1282cecdce9dSYong Wu };
1283cecdce9dSYong Wu 
12843c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
12853c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
1286d2e9a110SYong Wu 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
12873c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1288585e58f4SYong Wu 	.iova_region  = single_domain,
1289585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
12903c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
12913c213562SFabien Parent };
12923c213562SFabien Parent 
1293cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
1294cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
1295d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
1296d2e9a110SYong Wu 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
1297b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1298585e58f4SYong Wu 	.iova_region  = single_domain,
1299585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
130037276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
1301cecdce9dSYong Wu };
1302cecdce9dSYong Wu 
1303907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
1304907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
1305d2e9a110SYong Wu 	.flags        = RESET_AXI | MTK_IOMMU_TYPE_MM,
1306b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1307585e58f4SYong Wu 	.iova_region  = single_domain,
1308585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
130937276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1310907ba6a1SYong Wu };
1311907ba6a1SYong Wu 
13129e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = {
13139e3489e0SYong Wu 	.m4u_plat       = M4U_MT8192,
13149ec30c09SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1315d2e9a110SYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
13169e3489e0SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
13179e3489e0SYong Wu 	.iova_region    = mt8192_multi_dom,
13189e3489e0SYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
13199e3489e0SYong Wu 	.larbid_remap   = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20},
13209e3489e0SYong Wu 			   {0, 14, 16}, {0, 13, 18, 17}},
13219e3489e0SYong Wu };
13229e3489e0SYong Wu 
1323ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_infra = {
1324ef68a193SYong Wu 	.m4u_plat	  = M4U_MT8195,
1325ef68a193SYong Wu 	.flags            = WR_THROT_EN | DCM_DISABLE | STD_AXI_MODE | PM_CLK_AO |
1326ef68a193SYong Wu 			    MTK_IOMMU_TYPE_INFRA | IFA_IOMMU_PCIE_SUPPORT,
1327ef68a193SYong Wu 	.pericfg_comp_str = "mediatek,mt8195-pericfg_ao",
1328ef68a193SYong Wu 	.inv_sel_reg      = REG_MMU_INV_SEL_GEN2,
1329ef68a193SYong Wu 	.iova_region      = single_domain,
1330ef68a193SYong Wu 	.iova_region_nr   = ARRAY_SIZE(single_domain),
1331ef68a193SYong Wu };
1332ef68a193SYong Wu 
1333ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_vdo = {
1334ef68a193SYong Wu 	.m4u_plat	= M4U_MT8195,
1335ef68a193SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1336ef68a193SYong Wu 			  WR_THROT_EN | IOVA_34_EN | SHARE_PGTABLE | MTK_IOMMU_TYPE_MM,
1337ef68a193SYong Wu 	.hw_list        = &m4ulist,
1338ef68a193SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
1339ef68a193SYong Wu 	.iova_region	= mt8192_multi_dom,
1340ef68a193SYong Wu 	.iova_region_nr	= ARRAY_SIZE(mt8192_multi_dom),
1341ef68a193SYong Wu 	.larbid_remap   = {{2, 0}, {21}, {24}, {7}, {19}, {9, 10, 11},
1342ef68a193SYong Wu 			   {13, 17, 15/* 17b */, 25}, {5}},
1343ef68a193SYong Wu };
1344ef68a193SYong Wu 
1345ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_vpp = {
1346ef68a193SYong Wu 	.m4u_plat	= M4U_MT8195,
1347ef68a193SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_3BITS | OUT_ORDER_WR_EN |
1348ef68a193SYong Wu 			  WR_THROT_EN | IOVA_34_EN | SHARE_PGTABLE | MTK_IOMMU_TYPE_MM,
1349ef68a193SYong Wu 	.hw_list        = &m4ulist,
1350ef68a193SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
1351ef68a193SYong Wu 	.iova_region	= mt8192_multi_dom,
1352ef68a193SYong Wu 	.iova_region_nr	= ARRAY_SIZE(mt8192_multi_dom),
1353ef68a193SYong Wu 	.larbid_remap   = {{1}, {3},
1354ef68a193SYong Wu 			   {22, MTK_INVALID_LARBID, MTK_INVALID_LARBID, MTK_INVALID_LARBID, 23},
1355ef68a193SYong Wu 			   {8}, {20}, {12},
1356ef68a193SYong Wu 			   /* 16: 16a; 29: 16b; 30: CCUtop0; 31: CCUtop1 */
1357ef68a193SYong Wu 			   {14, 16, 29, 26, 30, 31, 18},
1358ef68a193SYong Wu 			   {4, MTK_INVALID_LARBID, MTK_INVALID_LARBID, MTK_INVALID_LARBID, 6}},
1359ef68a193SYong Wu };
1360ef68a193SYong Wu 
13610df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
1362cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1363068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
13643c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1365cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1366907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
13679e3489e0SYong Wu 	{ .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data},
1368ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-infra", .data = &mt8195_data_infra},
1369ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-vdo",   .data = &mt8195_data_vdo},
1370ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-vpp",   .data = &mt8195_data_vpp},
13710df4fabeSYong Wu 	{}
13720df4fabeSYong Wu };
13730df4fabeSYong Wu 
13740df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
13750df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
13760df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
13770df4fabeSYong Wu 	.driver	= {
13780df4fabeSYong Wu 		.name = "mtk-iommu",
1379f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
13800df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
13810df4fabeSYong Wu 	}
13820df4fabeSYong Wu };
138318d8c74eSYong Wu module_platform_driver(mtk_iommu_driver);
13840df4fabeSYong Wu 
138518d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations");
138618d8c74eSYong Wu MODULE_LICENSE("GPL v2");
1387