xref: /linux/drivers/iommu/mtk_iommu.c (revision b3fc95709c54ffbe80f16801e0a792a4d2b3d55e)
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>
110df4fabeSYong Wu #include <linux/err.h>
120df4fabeSYong Wu #include <linux/interrupt.h>
130df4fabeSYong Wu #include <linux/io.h>
140df4fabeSYong Wu #include <linux/iommu.h>
150df4fabeSYong Wu #include <linux/iopoll.h>
166a513de3SYong Wu #include <linux/io-pgtable.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>
23e7629070SYong 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 
336a513de3SYong Wu #include <dt-bindings/memory/mtk-memory-port.h>
340df4fabeSYong Wu 
350df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
360df4fabeSYong Wu 
370df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
380df4fabeSYong Wu #define F_ALL_INVLD				0x2
390df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
400df4fabeSYong Wu 
410df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
420df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
430df4fabeSYong Wu 
44068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2			0x02c
45b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1			0x038
460df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
470df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
480df4fabeSYong Wu 
4975eed350SChao Hao #define REG_MMU_MISC_CTRL			0x048
504bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
514bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))
524bb2bf4cSChao Hao 
530df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
549a87005eSYong Wu #define F_MMU_DCM				BIT(8)
559a87005eSYong Wu 
5635c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5735c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
580df4fabeSYong Wu 
590df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
60acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
610df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
62acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
630df4fabeSYong Wu 
640df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6570ca608bSYong Wu 
6630e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6730e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
680df4fabeSYong Wu 
690df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
700df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
710df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
720df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
730df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
740df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
750df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
760df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
770df4fabeSYong Wu 
780df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
7915a01f4cSYong Wu 						/* mmu0 | mmu1 */
8015a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
8115a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
8215a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
8315a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8415a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8515a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8615a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
870df4fabeSYong Wu 
880df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
890df4fabeSYong Wu 
900df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
9115a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
9215a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
930df4fabeSYong Wu 
9415a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
95ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
96ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
97ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
980df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
990df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
1000df4fabeSYong Wu 
10115a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
10215a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
10315a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
10415a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
10515a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
10637276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10737276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
1089ec30c09SYong Wu #define F_MMU_INT_ID_COMM_ID_EXT(a)		(((a) >> 10) & 0x7)
1099ec30c09SYong Wu #define F_MMU_INT_ID_SUB_COMM_ID_EXT(a)		(((a) >> 7) & 0x7)
11065df7d82SFabien Parent /* Macro for 5 bits length port ID field (default) */
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)
11365df7d82SFabien Parent /* Macro for 6 bits length port ID field */
11465df7d82SFabien Parent #define F_MMU_INT_ID_LARB_ID_WID_6(a)		(((a) >> 8) & 0x7)
11565df7d82SFabien Parent #define F_MMU_INT_ID_PORT_ID_WID_6(a)		(((a) >> 2) & 0x3f)
1160df4fabeSYong Wu 
117829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
11842d57fc5SYong Wu #define MTK_IOMMU_BANK_SZ			0x1000
1190df4fabeSYong Wu 
120f9b8c9b2SYong Wu #define PERICFG_IOMMU_1				0x714
121f9b8c9b2SYong Wu 
1226b717796SChao Hao #define HAS_4GB_MODE			BIT(0)
1236b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */
1246b717796SChao Hao #define HAS_BCLK			BIT(1)
1256b717796SChao Hao #define HAS_VLD_PA_RNG			BIT(2)
1266b717796SChao Hao #define RESET_AXI			BIT(3)
1274bb2bf4cSChao Hao #define OUT_ORDER_WR_EN			BIT(4)
1289ec30c09SYong Wu #define HAS_SUB_COMM_2BITS		BIT(5)
1299ec30c09SYong Wu #define HAS_SUB_COMM_3BITS		BIT(6)
1309ec30c09SYong Wu #define WR_THROT_EN			BIT(7)
1319ec30c09SYong Wu #define HAS_LEGACY_IVRP_PADDR		BIT(8)
1329ec30c09SYong Wu #define IOVA_34_EN			BIT(9)
1339ec30c09SYong Wu #define SHARE_PGTABLE			BIT(10) /* 2 HW share pgtable */
1349ec30c09SYong Wu #define DCM_DISABLE			BIT(11)
1359ec30c09SYong Wu #define STD_AXI_MODE			BIT(12) /* For non MM iommu */
1368cd1e619SYong Wu /* 2 bits: iommu type */
1378cd1e619SYong Wu #define MTK_IOMMU_TYPE_MM		(0x0 << 13)
1388cd1e619SYong Wu #define MTK_IOMMU_TYPE_INFRA		(0x1 << 13)
1398cd1e619SYong Wu #define MTK_IOMMU_TYPE_MASK		(0x3 << 13)
1406077c7e5SYong Wu /* PM and clock always on. e.g. infra iommu */
1416077c7e5SYong Wu #define PM_CLK_AO			BIT(15)
142e7629070SYong Wu #define IFA_IOMMU_PCIE_SUPPORT		BIT(16)
143301c3ca1SYunfei Wang #define PGTABLE_PA_35_EN		BIT(17)
14486580ec9SAngeloGioacchino Del Regno #define TF_PORT_TO_ADDR_MT8173		BIT(18)
14565df7d82SFabien Parent #define INT_ID_PORT_WIDTH_6		BIT(19)
1466b717796SChao Hao 
1478cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask)	\
1488cd1e619SYong Wu 				((((pdata)->flags) & (mask)) == (_x))
1498cd1e619SYong Wu 
1508cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, _x)
1518cd1e619SYong Wu #define MTK_IOMMU_IS_TYPE(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x,\
1528cd1e619SYong Wu 							MTK_IOMMU_TYPE_MASK)
1536b717796SChao Hao 
154d2e9a110SYong Wu #define MTK_INVALID_LARBID		MTK_LARB_NR_MAX
155d2e9a110SYong Wu 
1569485a04aSYong Wu #define MTK_LARB_COM_MAX	8
1579485a04aSYong Wu #define MTK_LARB_SUBCOM_MAX	8
1589485a04aSYong Wu 
1599485a04aSYong Wu #define MTK_IOMMU_GROUP_MAX	8
16099ca0228SYong Wu #define MTK_IOMMU_BANK_MAX	5
1619485a04aSYong Wu 
1629485a04aSYong Wu enum mtk_iommu_plat {
1639485a04aSYong Wu 	M4U_MT2712,
1649485a04aSYong Wu 	M4U_MT6779,
165717ec15eSAngeloGioacchino Del Regno 	M4U_MT6795,
1669485a04aSYong Wu 	M4U_MT8167,
1679485a04aSYong Wu 	M4U_MT8173,
1689485a04aSYong Wu 	M4U_MT8183,
169e8d7ccaaSYong Wu 	M4U_MT8186,
1709485a04aSYong Wu 	M4U_MT8192,
1719485a04aSYong Wu 	M4U_MT8195,
1723cd0e4a3SFabien Parent 	M4U_MT8365,
1739485a04aSYong Wu };
1749485a04aSYong Wu 
1759485a04aSYong Wu struct mtk_iommu_iova_region {
1769485a04aSYong Wu 	dma_addr_t		iova_base;
1779485a04aSYong Wu 	unsigned long long	size;
1789485a04aSYong Wu };
1799485a04aSYong Wu 
1806a513de3SYong Wu struct mtk_iommu_suspend_reg {
1816a513de3SYong Wu 	u32			misc_ctrl;
1826a513de3SYong Wu 	u32			dcm_dis;
1836a513de3SYong Wu 	u32			ctrl_reg;
1846a513de3SYong Wu 	u32			vld_pa_rng;
1856a513de3SYong Wu 	u32			wr_len_ctrl;
186d7127de1SYong Wu 
187d7127de1SYong Wu 	u32			int_control[MTK_IOMMU_BANK_MAX];
188d7127de1SYong Wu 	u32			int_main_control[MTK_IOMMU_BANK_MAX];
189d7127de1SYong Wu 	u32			ivrp_paddr[MTK_IOMMU_BANK_MAX];
1906a513de3SYong Wu };
1916a513de3SYong Wu 
1929485a04aSYong Wu struct mtk_iommu_plat_data {
1939485a04aSYong Wu 	enum mtk_iommu_plat	m4u_plat;
1949485a04aSYong Wu 	u32			flags;
1959485a04aSYong Wu 	u32			inv_sel_reg;
1969485a04aSYong Wu 
1979485a04aSYong Wu 	char			*pericfg_comp_str;
1989485a04aSYong Wu 	struct list_head	*hw_list;
199ae669345SYong Wu 
200ae669345SYong Wu 	/*
201ae669345SYong Wu 	 * The IOMMU HW may support 16GB iova. In order to balance the IOVA ranges,
202ae669345SYong Wu 	 * different masters will be put in different iova ranges, for example vcodec
203ae669345SYong Wu 	 * is in 4G-8G and cam is in 8G-12G. Meanwhile, some masters may have the
204ae669345SYong Wu 	 * special IOVA range requirement, like CCU can only support the address
205ae669345SYong Wu 	 * 0x40000000-0x44000000.
206ae669345SYong Wu 	 * Here list the iova ranges this SoC supports and which larbs/ports are in
207ae669345SYong Wu 	 * which region.
208ae669345SYong Wu 	 *
209ae669345SYong Wu 	 * 16GB iova all use one pgtable, but each a region is a iommu group.
210ae669345SYong Wu 	 */
211ae669345SYong Wu 	struct {
2129485a04aSYong Wu 		unsigned int	iova_region_nr;
2139485a04aSYong Wu 		const struct mtk_iommu_iova_region	*iova_region;
214b2a6876dSYong Wu 		/*
215b2a6876dSYong Wu 		 * Indicate the correspondance between larbs, ports and regions.
216b2a6876dSYong Wu 		 *
217b2a6876dSYong Wu 		 * The index is the same as iova_region and larb port numbers are
218b2a6876dSYong Wu 		 * described as bit positions.
219b2a6876dSYong Wu 		 * For example, storing BIT(0) at index 2,1 means "larb 1, port0 is in region 2".
220b2a6876dSYong Wu 		 *              [2] = { [1] = BIT(0) }
221b2a6876dSYong Wu 		 */
222b2a6876dSYong Wu 		const u32	(*iova_region_larb_msk)[MTK_LARB_NR_MAX];
223ae669345SYong Wu 	};
22499ca0228SYong Wu 
225ae669345SYong Wu 	/*
226ae669345SYong Wu 	 * The IOMMU HW may have 5 banks. Each bank has a independent pgtable.
227ae669345SYong Wu 	 * Here list how many banks this SoC supports/enables and which ports are in which bank.
228ae669345SYong Wu 	 */
229ae669345SYong Wu 	struct {
23099ca0228SYong Wu 		u8		banks_num;
23199ca0228SYong Wu 		bool		banks_enable[MTK_IOMMU_BANK_MAX];
23257fb481fSYong Wu 		unsigned int	banks_portmsk[MTK_IOMMU_BANK_MAX];
233ae669345SYong Wu 	};
234ae669345SYong Wu 
2359485a04aSYong Wu 	unsigned char       larbid_remap[MTK_LARB_COM_MAX][MTK_LARB_SUBCOM_MAX];
2369485a04aSYong Wu };
2379485a04aSYong Wu 
23899ca0228SYong Wu struct mtk_iommu_bank_data {
2399485a04aSYong Wu 	void __iomem			*base;
2409485a04aSYong Wu 	int				irq;
24199ca0228SYong Wu 	u8				id;
24299ca0228SYong Wu 	struct device			*parent_dev;
24399ca0228SYong Wu 	struct mtk_iommu_data		*parent_data;
24499ca0228SYong Wu 	spinlock_t			tlb_lock; /* lock for tlb range flush */
24599ca0228SYong Wu 	struct mtk_iommu_domain		*m4u_dom; /* Each bank has a domain */
24699ca0228SYong Wu };
24799ca0228SYong Wu 
24899ca0228SYong Wu struct mtk_iommu_data {
2499485a04aSYong Wu 	struct device			*dev;
2509485a04aSYong Wu 	struct clk			*bclk;
2519485a04aSYong Wu 	phys_addr_t			protect_base; /* protect memory base */
2529485a04aSYong Wu 	struct mtk_iommu_suspend_reg	reg;
2539485a04aSYong Wu 	struct iommu_group		*m4u_group[MTK_IOMMU_GROUP_MAX];
2549485a04aSYong Wu 	bool                            enable_4GB;
2559485a04aSYong Wu 
2569485a04aSYong Wu 	struct iommu_device		iommu;
2579485a04aSYong Wu 	const struct mtk_iommu_plat_data *plat_data;
2589485a04aSYong Wu 	struct device			*smicomm_dev;
2599485a04aSYong Wu 
26099ca0228SYong Wu 	struct mtk_iommu_bank_data	*bank;
2619485a04aSYong Wu 	struct regmap			*pericfg;
2629485a04aSYong Wu 	struct mutex			mutex; /* Protect m4u_group/m4u_dom above */
2639485a04aSYong Wu 
2649485a04aSYong Wu 	/*
2659485a04aSYong Wu 	 * In the sharing pgtable case, list data->list to the global list like m4ulist.
2669485a04aSYong Wu 	 * In the non-sharing pgtable case, list data->list to the itself hw_list_head.
2679485a04aSYong Wu 	 */
2689485a04aSYong Wu 	struct list_head		*hw_list;
2699485a04aSYong Wu 	struct list_head		hw_list_head;
2709485a04aSYong Wu 	struct list_head		list;
2719485a04aSYong Wu 	struct mtk_smi_larb_iommu	larb_imu[MTK_LARB_NR_MAX];
2729485a04aSYong Wu };
2739485a04aSYong Wu 
2740df4fabeSYong Wu struct mtk_iommu_domain {
2750df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
2760df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
2770df4fabeSYong Wu 
27899ca0228SYong Wu 	struct mtk_iommu_bank_data	*bank;
2790df4fabeSYong Wu 	struct iommu_domain		domain;
280ddf67a87SYong Wu 
281ddf67a87SYong Wu 	struct mutex			mutex; /* Protect "data" in this structure */
2820df4fabeSYong Wu };
2830df4fabeSYong Wu 
2849485a04aSYong Wu static int mtk_iommu_bind(struct device *dev)
2859485a04aSYong Wu {
2869485a04aSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
2879485a04aSYong Wu 
2889485a04aSYong Wu 	return component_bind_all(dev, &data->larb_imu);
2899485a04aSYong Wu }
2909485a04aSYong Wu 
2919485a04aSYong Wu static void mtk_iommu_unbind(struct device *dev)
2929485a04aSYong Wu {
2939485a04aSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
2949485a04aSYong Wu 
2959485a04aSYong Wu 	component_unbind_all(dev, &data->larb_imu);
2969485a04aSYong Wu }
2979485a04aSYong Wu 
298b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
2990df4fabeSYong Wu 
300e24453e1SYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data, unsigned int bankid);
3017f37a91dSYong Wu 
302bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({					\
303bfed8731SYong Wu 	dma_addr_t _addr = iova;					\
304bfed8731SYong Wu 	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
305bfed8731SYong Wu })
306bfed8731SYong Wu 
30776ce6546SYong Wu /*
30876ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
30976ce6546SYong Wu  *
31076ce6546SYong Wu  * CPU Physical address:
31176ce6546SYong Wu  * ====================
31276ce6546SYong Wu  *
31376ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
31476ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
31576ce6546SYong Wu  * +--I/O--+------------Memory-------------+
31676ce6546SYong Wu  *
31776ce6546SYong Wu  * IOMMU output physical address:
31876ce6546SYong Wu  *  =============================
31976ce6546SYong Wu  *
32076ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
32176ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
32276ce6546SYong Wu  *                                 +------------Memory-------------+
32376ce6546SYong Wu  *
32476ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
32576ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
32676ce6546SYong Wu  * 'E', the CPU physical address keep as is.
32776ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
32876ce6546SYong Wu  */
329b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
33076ce6546SYong Wu 
3317c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
3327c3a2ec0SYong Wu 
3339e3a2a64SYong Wu #define for_each_m4u(data, head)  list_for_each_entry(data, head, list)
3347c3a2ec0SYong Wu 
3353df9bdd4SYong Wu #define MTK_IOMMU_IOVA_SZ_4G		(SZ_4G - SZ_8M) /* 8M as gap */
3363df9bdd4SYong Wu 
337585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = {
3383df9bdd4SYong Wu 	{.iova_base = 0,		.size = MTK_IOMMU_IOVA_SZ_4G},
339585e58f4SYong Wu };
340585e58f4SYong Wu 
3416b1317f9SYong Wu #define MT8192_MULTI_REGION_NR_MAX	6
3426b1317f9SYong Wu 
3436b1317f9SYong Wu #define MT8192_MULTI_REGION_NR	(IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) ? \
3446b1317f9SYong Wu 				 MT8192_MULTI_REGION_NR_MAX : 1)
3456b1317f9SYong Wu 
3466b1317f9SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[MT8192_MULTI_REGION_NR] = {
3473df9bdd4SYong Wu 	{ .iova_base = 0x0,		.size = MTK_IOMMU_IOVA_SZ_4G},	/* 0 ~ 4G,  */
3489e3489e0SYong Wu 	#if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)
3493df9bdd4SYong Wu 	{ .iova_base = SZ_4G,		.size = MTK_IOMMU_IOVA_SZ_4G},	/* 4G ~ 8G */
3503df9bdd4SYong Wu 	{ .iova_base = SZ_4G * 2,	.size = MTK_IOMMU_IOVA_SZ_4G},	/* 8G ~ 12G */
3513df9bdd4SYong Wu 	{ .iova_base = SZ_4G * 3,	.size = MTK_IOMMU_IOVA_SZ_4G},	/* 12G ~ 16G */
352129a3b88SYong Wu 
3539e3489e0SYong Wu 	{ .iova_base = 0x240000000ULL,	.size = 0x4000000},	/* CCU0 */
3549e3489e0SYong Wu 	{ .iova_base = 0x244000000ULL,	.size = 0x4000000},	/* CCU1 */
3559e3489e0SYong Wu 	#endif
3569e3489e0SYong Wu };
3579e3489e0SYong Wu 
3589e3a2a64SYong Wu /* If 2 M4U share a domain(use the same hwlist), Put the corresponding info in first data.*/
3599e3a2a64SYong Wu static struct mtk_iommu_data *mtk_iommu_get_frst_data(struct list_head *hwlist)
3607c3a2ec0SYong Wu {
3619e3a2a64SYong Wu 	return list_first_entry(hwlist, struct mtk_iommu_data, list);
3627c3a2ec0SYong Wu }
3637c3a2ec0SYong Wu 
3640df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
3650df4fabeSYong Wu {
3660df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
3670df4fabeSYong Wu }
3680df4fabeSYong Wu 
3690954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
3700df4fabeSYong Wu {
37199ca0228SYong Wu 	/* Tlb flush all always is in bank0. */
37299ca0228SYong Wu 	struct mtk_iommu_bank_data *bank = &data->bank[0];
37399ca0228SYong Wu 	void __iomem *base = bank->base;
37415672b6dSYong Wu 	unsigned long flags;
375c0b57581SYong Wu 
37699ca0228SYong Wu 	spin_lock_irqsave(&bank->tlb_lock, flags);
377887cf6a7SYong Wu 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, base + data->plat_data->inv_sel_reg);
378887cf6a7SYong Wu 	writel_relaxed(F_ALL_INVLD, base + REG_MMU_INVALIDATE);
3790df4fabeSYong Wu 	wmb(); /* Make sure the tlb flush all done */
38099ca0228SYong Wu 	spin_unlock_irqrestore(&bank->tlb_lock, flags);
3817c3a2ec0SYong Wu }
3820df4fabeSYong Wu 
3831f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
38499ca0228SYong Wu 					   struct mtk_iommu_bank_data *bank)
3850df4fabeSYong Wu {
38699ca0228SYong Wu 	struct list_head *head = bank->parent_data->hw_list;
38799ca0228SYong Wu 	struct mtk_iommu_bank_data *curbank;
38899ca0228SYong Wu 	struct mtk_iommu_data *data;
3896077c7e5SYong Wu 	bool check_pm_status;
3901f4fd624SYong Wu 	unsigned long flags;
391887cf6a7SYong Wu 	void __iomem *base;
3921f4fd624SYong Wu 	int ret;
3931f4fd624SYong Wu 	u32 tmp;
3940df4fabeSYong Wu 
3959e3a2a64SYong Wu 	for_each_m4u(data, head) {
3966077c7e5SYong Wu 		/*
3976077c7e5SYong Wu 		 * To avoid resume the iommu device frequently when the iommu device
3986077c7e5SYong Wu 		 * is not active, it doesn't always call pm_runtime_get here, then tlb
3996077c7e5SYong Wu 		 * flush depends on the tlb flush all in the runtime resume.
4006077c7e5SYong Wu 		 *
4016077c7e5SYong Wu 		 * There are 2 special cases:
4026077c7e5SYong Wu 		 *
4036077c7e5SYong Wu 		 * Case1: The iommu dev doesn't have power domain but has bclk. This case
4046077c7e5SYong Wu 		 * should also avoid the tlb flush while the dev is not active to mute
4056077c7e5SYong Wu 		 * the tlb timeout log. like mt8173.
4066077c7e5SYong Wu 		 *
4076077c7e5SYong Wu 		 * Case2: The power/clock of infra iommu is always on, and it doesn't
4086077c7e5SYong Wu 		 * have the device link with the master devices. This case should avoid
4096077c7e5SYong Wu 		 * the PM status check.
4106077c7e5SYong Wu 		 */
4116077c7e5SYong Wu 		check_pm_status = !MTK_IOMMU_HAS_FLAG(data->plat_data, PM_CLK_AO);
4126077c7e5SYong Wu 
4136077c7e5SYong Wu 		if (check_pm_status) {
414c0b57581SYong Wu 			if (pm_runtime_get_if_in_use(data->dev) <= 0)
415c0b57581SYong Wu 				continue;
4166077c7e5SYong Wu 		}
417c0b57581SYong Wu 
41899ca0228SYong Wu 		curbank = &data->bank[bank->id];
41999ca0228SYong Wu 		base = curbank->base;
420887cf6a7SYong Wu 
42199ca0228SYong Wu 		spin_lock_irqsave(&curbank->tlb_lock, flags);
4227c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
423887cf6a7SYong Wu 			       base + data->plat_data->inv_sel_reg);
4240df4fabeSYong Wu 
425887cf6a7SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova), base + REG_MMU_INVLD_START_A);
426bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
427887cf6a7SYong Wu 			       base + REG_MMU_INVLD_END_A);
428887cf6a7SYong Wu 		writel_relaxed(F_MMU_INV_RANGE, base + REG_MMU_INVALIDATE);
4290df4fabeSYong Wu 
4301f4fd624SYong Wu 		/* tlb sync */
431887cf6a7SYong Wu 		ret = readl_poll_timeout_atomic(base + REG_MMU_CPE_DONE,
432c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
43315672b6dSYong Wu 
43415672b6dSYong Wu 		/* Clear the CPE status */
435887cf6a7SYong Wu 		writel_relaxed(0, base + REG_MMU_CPE_DONE);
43699ca0228SYong Wu 		spin_unlock_irqrestore(&curbank->tlb_lock, flags);
43715672b6dSYong Wu 
4380df4fabeSYong Wu 		if (ret) {
4390df4fabeSYong Wu 			dev_warn(data->dev,
4400df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
4410954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
4420df4fabeSYong Wu 		}
443c0b57581SYong Wu 
4446077c7e5SYong Wu 		if (check_pm_status)
445c0b57581SYong Wu 			pm_runtime_put(data->dev);
4460df4fabeSYong Wu 	}
4477c3a2ec0SYong Wu }
4480df4fabeSYong Wu 
4490df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
4500df4fabeSYong Wu {
45199ca0228SYong Wu 	struct mtk_iommu_bank_data *bank = dev_id;
45299ca0228SYong Wu 	struct mtk_iommu_data *data = bank->parent_data;
45399ca0228SYong Wu 	struct mtk_iommu_domain *dom = bank->m4u_dom;
454d2e9a110SYong Wu 	unsigned int fault_larb = MTK_INVALID_LARBID, fault_port = 0, sub_comm = 0;
455ef0f0986SYong Wu 	u32 int_state, regval, va34_32, pa34_32;
456887cf6a7SYong Wu 	const struct mtk_iommu_plat_data *plat_data = data->plat_data;
45799ca0228SYong Wu 	void __iomem *base = bank->base;
458ef0f0986SYong Wu 	u64 fault_iova, fault_pa;
4590df4fabeSYong Wu 	bool layer, write;
4600df4fabeSYong Wu 
4610df4fabeSYong Wu 	/* Read error info from registers */
462887cf6a7SYong Wu 	int_state = readl_relaxed(base + REG_MMU_FAULT_ST1);
46315a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
464887cf6a7SYong Wu 		regval = readl_relaxed(base + REG_MMU0_INT_ID);
465887cf6a7SYong Wu 		fault_iova = readl_relaxed(base + REG_MMU0_FAULT_VA);
466887cf6a7SYong Wu 		fault_pa = readl_relaxed(base + REG_MMU0_INVLD_PA);
46715a01f4cSYong Wu 	} else {
468887cf6a7SYong Wu 		regval = readl_relaxed(base + REG_MMU1_INT_ID);
469887cf6a7SYong Wu 		fault_iova = readl_relaxed(base + REG_MMU1_FAULT_VA);
470887cf6a7SYong Wu 		fault_pa = readl_relaxed(base + REG_MMU1_INVLD_PA);
47115a01f4cSYong Wu 	}
4720df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
4730df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
474887cf6a7SYong Wu 	if (MTK_IOMMU_HAS_FLAG(plat_data, IOVA_34_EN)) {
475ef0f0986SYong Wu 		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
476ef0f0986SYong Wu 		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
477ef0f0986SYong Wu 		fault_iova |= (u64)va34_32 << 32;
478ef0f0986SYong Wu 	}
47982e51771SYong Wu 	pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
48082e51771SYong Wu 	fault_pa |= (u64)pa34_32 << 32;
481ef0f0986SYong Wu 
482887cf6a7SYong Wu 	if (MTK_IOMMU_IS_TYPE(plat_data, MTK_IOMMU_TYPE_MM)) {
483887cf6a7SYong Wu 		if (MTK_IOMMU_HAS_FLAG(plat_data, HAS_SUB_COMM_2BITS)) {
48437276e00SChao Hao 			fault_larb = F_MMU_INT_ID_COMM_ID(regval);
48537276e00SChao Hao 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
48665df7d82SFabien Parent 			fault_port = F_MMU_INT_ID_PORT_ID(regval);
487887cf6a7SYong Wu 		} else if (MTK_IOMMU_HAS_FLAG(plat_data, HAS_SUB_COMM_3BITS)) {
4889ec30c09SYong Wu 			fault_larb = F_MMU_INT_ID_COMM_ID_EXT(regval);
4899ec30c09SYong Wu 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID_EXT(regval);
49065df7d82SFabien Parent 			fault_port = F_MMU_INT_ID_PORT_ID(regval);
49165df7d82SFabien Parent 		} else if (MTK_IOMMU_HAS_FLAG(plat_data, INT_ID_PORT_WIDTH_6)) {
49265df7d82SFabien Parent 			fault_port = F_MMU_INT_ID_PORT_ID_WID_6(regval);
49365df7d82SFabien Parent 			fault_larb = F_MMU_INT_ID_LARB_ID_WID_6(regval);
49437276e00SChao Hao 		} else {
49565df7d82SFabien Parent 			fault_port = F_MMU_INT_ID_PORT_ID(regval);
49637276e00SChao Hao 			fault_larb = F_MMU_INT_ID_LARB_ID(regval);
49737276e00SChao Hao 		}
49837276e00SChao Hao 		fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
499d2e9a110SYong Wu 	}
500b3e5eee7SYong Wu 
50100ef8885SRicardo Ribalda 	if (!dom || report_iommu_fault(&dom->domain, bank->parent_dev, fault_iova,
5020df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
5030df4fabeSYong Wu 		dev_err_ratelimited(
50499ca0228SYong Wu 			bank->parent_dev,
505f9b8c9b2SYong Wu 			"fault type=0x%x iova=0x%llx pa=0x%llx master=0x%x(larb=%d port=%d) layer=%d %s\n",
506f9b8c9b2SYong Wu 			int_state, fault_iova, fault_pa, regval, fault_larb, fault_port,
5070df4fabeSYong Wu 			layer, write ? "write" : "read");
5080df4fabeSYong Wu 	}
5090df4fabeSYong Wu 
5100df4fabeSYong Wu 	/* Interrupt clear */
511887cf6a7SYong Wu 	regval = readl_relaxed(base + REG_MMU_INT_CONTROL0);
5120df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
513887cf6a7SYong Wu 	writel_relaxed(regval, base + REG_MMU_INT_CONTROL0);
5140df4fabeSYong Wu 
5150df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
5160df4fabeSYong Wu 
5170df4fabeSYong Wu 	return IRQ_HANDLED;
5180df4fabeSYong Wu }
5190df4fabeSYong Wu 
52057fb481fSYong Wu static unsigned int mtk_iommu_get_bank_id(struct device *dev,
52157fb481fSYong Wu 					  const struct mtk_iommu_plat_data *plat_data)
52257fb481fSYong Wu {
52357fb481fSYong Wu 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
52457fb481fSYong Wu 	unsigned int i, portmsk = 0, bankid = 0;
52557fb481fSYong Wu 
52657fb481fSYong Wu 	if (plat_data->banks_num == 1)
52757fb481fSYong Wu 		return bankid;
52857fb481fSYong Wu 
52957fb481fSYong Wu 	for (i = 0; i < fwspec->num_ids; i++)
53057fb481fSYong Wu 		portmsk |= BIT(MTK_M4U_TO_PORT(fwspec->ids[i]));
53157fb481fSYong Wu 
53257fb481fSYong Wu 	for (i = 0; i < plat_data->banks_num && i < MTK_IOMMU_BANK_MAX; i++) {
53357fb481fSYong Wu 		if (!plat_data->banks_enable[i])
53457fb481fSYong Wu 			continue;
53557fb481fSYong Wu 
53657fb481fSYong Wu 		if (portmsk & plat_data->banks_portmsk[i]) {
53757fb481fSYong Wu 			bankid = i;
53857fb481fSYong Wu 			break;
53957fb481fSYong Wu 		}
54057fb481fSYong Wu 	}
54157fb481fSYong Wu 	return bankid; /* default is 0 */
54257fb481fSYong Wu }
54357fb481fSYong Wu 
544d72e0ff5SYong Wu static int mtk_iommu_get_iova_region_id(struct device *dev,
545803cf9e5SYong Wu 					const struct mtk_iommu_plat_data *plat_data)
546803cf9e5SYong Wu {
547b2a6876dSYong Wu 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
548b2a6876dSYong Wu 	unsigned int portidmsk = 0, larbid;
549b2a6876dSYong Wu 	const u32 *rgn_larb_msk;
550b2a6876dSYong Wu 	int i;
551803cf9e5SYong Wu 
552b2a6876dSYong Wu 	if (plat_data->iova_region_nr == 1)
553803cf9e5SYong Wu 		return 0;
554803cf9e5SYong Wu 
555b2a6876dSYong Wu 	larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
556b2a6876dSYong Wu 	for (i = 0; i < fwspec->num_ids; i++)
557b2a6876dSYong Wu 		portidmsk |= BIT(MTK_M4U_TO_PORT(fwspec->ids[i]));
558b2a6876dSYong Wu 
559b2a6876dSYong Wu 	for (i = 0; i < plat_data->iova_region_nr; i++) {
560b2a6876dSYong Wu 		rgn_larb_msk = plat_data->iova_region_larb_msk[i];
561b2a6876dSYong Wu 		if (!rgn_larb_msk)
562b2a6876dSYong Wu 			continue;
563b2a6876dSYong Wu 
564b2a6876dSYong Wu 		if ((rgn_larb_msk[larbid] & portidmsk) == portidmsk)
565803cf9e5SYong Wu 			return i;
566803cf9e5SYong Wu 	}
567803cf9e5SYong Wu 
568b2a6876dSYong Wu 	dev_err(dev, "Can NOT find the region for larb(%d-%x).\n",
569b2a6876dSYong Wu 		larbid, portidmsk);
570803cf9e5SYong Wu 	return -EINVAL;
571803cf9e5SYong Wu }
572803cf9e5SYong Wu 
573f9b8c9b2SYong Wu static int mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev,
574d72e0ff5SYong Wu 			    bool enable, unsigned int regionid)
5750df4fabeSYong Wu {
5760df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
5770df4fabeSYong Wu 	unsigned int                 larbid, portid;
578a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
5798d2c749eSYong Wu 	const struct mtk_iommu_iova_region *region;
580f9b8c9b2SYong Wu 	u32 peri_mmuen, peri_mmuen_msk;
581f9b8c9b2SYong Wu 	int i, ret = 0;
5820df4fabeSYong Wu 
58358f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
58458f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
58558f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
5868d2c749eSYong Wu 
587d2e9a110SYong Wu 		if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
5881ee9feb2SYong Wu 			larb_mmu = &data->larb_imu[larbid];
5890df4fabeSYong Wu 
590d72e0ff5SYong Wu 			region = data->plat_data->iova_region + regionid;
5918d2c749eSYong Wu 			larb_mmu->bank[portid] = upper_32_bits(region->iova_base);
5928d2c749eSYong Wu 
593d72e0ff5SYong Wu 			dev_dbg(dev, "%s iommu for larb(%s) port %d region %d rgn-bank %d.\n",
5948d2c749eSYong Wu 				enable ? "enable" : "disable", dev_name(larb_mmu->dev),
595d72e0ff5SYong Wu 				portid, regionid, larb_mmu->bank[portid]);
5960df4fabeSYong Wu 
5970df4fabeSYong Wu 			if (enable)
5980df4fabeSYong Wu 				larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
5990df4fabeSYong Wu 			else
6000df4fabeSYong Wu 				larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
601f9b8c9b2SYong Wu 		} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {
602f9b8c9b2SYong Wu 			peri_mmuen_msk = BIT(portid);
603e7629070SYong Wu 			/* PCI dev has only one output id, enable the next writing bit for PCIe */
604e7629070SYong Wu 			if (dev_is_pci(dev))
605e7629070SYong Wu 				peri_mmuen_msk |= BIT(portid + 1);
606f9b8c9b2SYong Wu 
607e7629070SYong Wu 			peri_mmuen = enable ? peri_mmuen_msk : 0;
608f9b8c9b2SYong Wu 			ret = regmap_update_bits(data->pericfg, PERICFG_IOMMU_1,
609f9b8c9b2SYong Wu 						 peri_mmuen_msk, peri_mmuen);
610f9b8c9b2SYong Wu 			if (ret)
611f9b8c9b2SYong Wu 				dev_err(dev, "%s iommu(%s) inframaster 0x%x fail(%d).\n",
612f9b8c9b2SYong Wu 					enable ? "enable" : "disable",
613f9b8c9b2SYong Wu 					dev_name(data->dev), peri_mmuen_msk, ret);
6140df4fabeSYong Wu 		}
6150df4fabeSYong Wu 	}
616f9b8c9b2SYong Wu 	return ret;
617d2e9a110SYong Wu }
6180df4fabeSYong Wu 
6194f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
620c3045f39SYong Wu 				     struct mtk_iommu_data *data,
621d72e0ff5SYong Wu 				     unsigned int region_id)
6220df4fabeSYong Wu {
623c3045f39SYong Wu 	const struct mtk_iommu_iova_region *region;
62499ca0228SYong Wu 	struct mtk_iommu_domain	*m4u_dom;
625c3045f39SYong Wu 
62699ca0228SYong Wu 	/* Always use bank0 in sharing pgtable case */
62799ca0228SYong Wu 	m4u_dom = data->bank[0].m4u_dom;
62899ca0228SYong Wu 	if (m4u_dom) {
62999ca0228SYong Wu 		dom->iop = m4u_dom->iop;
63099ca0228SYong Wu 		dom->cfg = m4u_dom->cfg;
63199ca0228SYong Wu 		dom->domain.pgsize_bitmap = m4u_dom->cfg.pgsize_bitmap;
632c3045f39SYong Wu 		goto update_iova_region;
633c3045f39SYong Wu 	}
634c3045f39SYong Wu 
6350df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
6360df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
6370df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
638b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
6390df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
6402f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
6410df4fabeSYong Wu 		.iommu_dev = data->dev,
6420df4fabeSYong Wu 	};
6430df4fabeSYong Wu 
644301c3ca1SYunfei Wang 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, PGTABLE_PA_35_EN))
645301c3ca1SYunfei Wang 		dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT;
646301c3ca1SYunfei Wang 
6479bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
6489bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
6499bdfe4c1SYong Wu 	else
6509bdfe4c1SYong Wu 		dom->cfg.oas = 35;
6519bdfe4c1SYong Wu 
6520df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
6530df4fabeSYong Wu 	if (!dom->iop) {
6540df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
655bd7ebb77SNicolin Chen 		return -ENOMEM;
6560df4fabeSYong Wu 	}
6570df4fabeSYong Wu 
6580df4fabeSYong Wu 	/* Update our support page sizes bitmap */
659d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
660b7875eb9SYong Wu 
661c3045f39SYong Wu update_iova_region:
662c3045f39SYong Wu 	/* Update the iova region for this domain */
663d72e0ff5SYong Wu 	region = data->plat_data->iova_region + region_id;
664c3045f39SYong Wu 	dom->domain.geometry.aperture_start = region->iova_base;
665c3045f39SYong Wu 	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
666b7875eb9SYong Wu 	dom->domain.geometry.force_aperture = true;
6670df4fabeSYong Wu 	return 0;
6680df4fabeSYong Wu }
6690df4fabeSYong Wu 
6700df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
6710df4fabeSYong Wu {
6720df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
6730df4fabeSYong Wu 
67432e1cccfSYong Wu 	if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED)
6750df4fabeSYong Wu 		return NULL;
6760df4fabeSYong Wu 
6770df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
6780df4fabeSYong Wu 	if (!dom)
6790df4fabeSYong Wu 		return NULL;
680ddf67a87SYong Wu 	mutex_init(&dom->mutex);
6810df4fabeSYong Wu 
6824f956c97SYong Wu 	return &dom->domain;
6834f956c97SYong Wu }
6844f956c97SYong Wu 
6850df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
6860df4fabeSYong Wu {
6870df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
6880df4fabeSYong Wu }
6890df4fabeSYong Wu 
6900df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
6910df4fabeSYong Wu 				   struct device *dev)
6920df4fabeSYong Wu {
693645b87c1SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev), *frstdata;
6940df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
6959e3a2a64SYong Wu 	struct list_head *hw_list = data->hw_list;
696c0b57581SYong Wu 	struct device *m4udev = data->dev;
69799ca0228SYong Wu 	struct mtk_iommu_bank_data *bank;
69857fb481fSYong Wu 	unsigned int bankid;
699d72e0ff5SYong Wu 	int ret, region_id;
7000df4fabeSYong Wu 
701d72e0ff5SYong Wu 	region_id = mtk_iommu_get_iova_region_id(dev, data->plat_data);
702d72e0ff5SYong Wu 	if (region_id < 0)
703d72e0ff5SYong Wu 		return region_id;
704803cf9e5SYong Wu 
70557fb481fSYong Wu 	bankid = mtk_iommu_get_bank_id(dev, data->plat_data);
706ddf67a87SYong Wu 	mutex_lock(&dom->mutex);
70799ca0228SYong Wu 	if (!dom->bank) {
708645b87c1SYong Wu 		/* Data is in the frstdata in sharing pgtable case. */
7099e3a2a64SYong Wu 		frstdata = mtk_iommu_get_frst_data(hw_list);
710645b87c1SYong Wu 
711d72e0ff5SYong Wu 		ret = mtk_iommu_domain_finalise(dom, frstdata, region_id);
712ddf67a87SYong Wu 		if (ret) {
713ddf67a87SYong Wu 			mutex_unlock(&dom->mutex);
71404cee82eSNicolin Chen 			return ret;
715ddf67a87SYong Wu 		}
71699ca0228SYong Wu 		dom->bank = &data->bank[bankid];
7174f956c97SYong Wu 	}
718ddf67a87SYong Wu 	mutex_unlock(&dom->mutex);
7194f956c97SYong Wu 
7200e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
72199ca0228SYong Wu 	bank = &data->bank[bankid];
722e24453e1SYong Wu 	if (!bank->m4u_dom) { /* Initialize the M4U HW for each a BANK */
723c0b57581SYong Wu 		ret = pm_runtime_resume_and_get(m4udev);
724e24453e1SYong Wu 		if (ret < 0) {
725e24453e1SYong Wu 			dev_err(m4udev, "pm get fail(%d) in attach.\n", ret);
7260e5a3f2eSYong Wu 			goto err_unlock;
727e24453e1SYong Wu 		}
728c0b57581SYong Wu 
729e24453e1SYong Wu 		ret = mtk_iommu_hw_init(data, bankid);
730c0b57581SYong Wu 		if (ret) {
731c0b57581SYong Wu 			pm_runtime_put(m4udev);
7320e5a3f2eSYong Wu 			goto err_unlock;
733c0b57581SYong Wu 		}
73499ca0228SYong Wu 		bank->m4u_dom = dom;
735301c3ca1SYunfei Wang 		writel(dom->cfg.arm_v7s_cfg.ttbr, bank->base + REG_MMU_PT_BASE_ADDR);
736c0b57581SYong Wu 
737c0b57581SYong Wu 		pm_runtime_put(m4udev);
7380df4fabeSYong Wu 	}
7390e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
7400df4fabeSYong Wu 
741f7da2da8SYong Wu 	if (region_id > 0) {
742f7da2da8SYong Wu 		ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(34));
743f7da2da8SYong Wu 		if (ret) {
744f7da2da8SYong Wu 			dev_err(m4udev, "Failed to set dma_mask for %s(%d).\n", dev_name(dev), ret);
745f7da2da8SYong Wu 			return ret;
746f7da2da8SYong Wu 		}
747f7da2da8SYong Wu 	}
748f7da2da8SYong Wu 
749d72e0ff5SYong Wu 	return mtk_iommu_config(data, dev, true, region_id);
7500e5a3f2eSYong Wu 
7510e5a3f2eSYong Wu err_unlock:
7520e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
7530e5a3f2eSYong Wu 	return ret;
7540df4fabeSYong Wu }
7550df4fabeSYong Wu 
7560df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
75785637380SRobin Murphy 			 phys_addr_t paddr, size_t pgsize, size_t pgcount,
75885637380SRobin Murphy 			 int prot, gfp_t gfp, size_t *mapped)
7590df4fabeSYong Wu {
7600df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
7610df4fabeSYong Wu 
762b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
76399ca0228SYong Wu 	if (dom->bank->parent_data->enable_4GB)
764b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
765b4dad40eSYong Wu 
76660829b4dSYong Wu 	/* Synchronize with the tlb_lock */
76785637380SRobin Murphy 	return dom->iop->map_pages(dom->iop, iova, paddr, pgsize, pgcount, prot, gfp, mapped);
7680df4fabeSYong Wu }
7690df4fabeSYong Wu 
7700df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
77185637380SRobin Murphy 			      unsigned long iova, size_t pgsize, size_t pgcount,
77256f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
7730df4fabeSYong Wu {
7740df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
7750df4fabeSYong Wu 
77685637380SRobin Murphy 	iommu_iotlb_gather_add_range(gather, iova, pgsize * pgcount);
77785637380SRobin Murphy 	return dom->iop->unmap_pages(dom->iop, iova, pgsize, pgcount, gather);
7780df4fabeSYong Wu }
7790df4fabeSYong Wu 
78056f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
78156f8af5eSWill Deacon {
78208500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
78308500c43SYong Wu 
784*b3fc9570SChen-Yu Tsai 	if (dom->bank)
78599ca0228SYong Wu 		mtk_iommu_tlb_flush_all(dom->bank->parent_data);
78656f8af5eSWill Deacon }
78756f8af5eSWill Deacon 
78856f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
78956f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
7904d689b61SRobin Murphy {
79108500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
792862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
793da3cc91bSYong Wu 
79499ca0228SYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, dom->bank);
7954d689b61SRobin Murphy }
7964d689b61SRobin Murphy 
79720143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
79820143451SYong Wu 			       size_t size)
79920143451SYong Wu {
80008500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
80120143451SYong Wu 
80299ca0228SYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, dom->bank);
80320143451SYong Wu }
80420143451SYong Wu 
8050df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
8060df4fabeSYong Wu 					  dma_addr_t iova)
8070df4fabeSYong Wu {
8080df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
8090df4fabeSYong Wu 	phys_addr_t pa;
8100df4fabeSYong Wu 
8110df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
812f13efafcSArnd Bergmann 	if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
81399ca0228SYong Wu 	    dom->bank->parent_data->enable_4GB &&
814f13efafcSArnd Bergmann 	    pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
815b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
81630e2fccfSYong Wu 
8170df4fabeSYong Wu 	return pa;
8180df4fabeSYong Wu }
8190df4fabeSYong Wu 
82080e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
8210df4fabeSYong Wu {
822a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
823b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
824635319a4SYong Wu 	struct device_link *link;
825635319a4SYong Wu 	struct device *larbdev;
826635319a4SYong Wu 	unsigned int larbid, larbidx, i;
8270df4fabeSYong Wu 
828a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
82980e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
8300df4fabeSYong Wu 
8313524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
832b16c0170SJoerg Roedel 
833d2e9a110SYong Wu 	if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
834d2e9a110SYong Wu 		return &data->iommu;
835d2e9a110SYong Wu 
836635319a4SYong Wu 	/*
837635319a4SYong Wu 	 * Link the consumer device with the smi-larb device(supplier).
838635319a4SYong Wu 	 * The device that connects with each a larb is a independent HW.
839635319a4SYong Wu 	 * All the ports in each a device should be in the same larbs.
840635319a4SYong Wu 	 */
841635319a4SYong Wu 	larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
842de78657eSMiles Chen 	if (larbid >= MTK_LARB_NR_MAX)
843de78657eSMiles Chen 		return ERR_PTR(-EINVAL);
844de78657eSMiles Chen 
845635319a4SYong Wu 	for (i = 1; i < fwspec->num_ids; i++) {
846635319a4SYong Wu 		larbidx = MTK_M4U_TO_LARB(fwspec->ids[i]);
847635319a4SYong Wu 		if (larbid != larbidx) {
848635319a4SYong Wu 			dev_err(dev, "Can only use one larb. Fail@larb%d-%d.\n",
849635319a4SYong Wu 				larbid, larbidx);
850635319a4SYong Wu 			return ERR_PTR(-EINVAL);
851635319a4SYong Wu 		}
852635319a4SYong Wu 	}
853635319a4SYong Wu 	larbdev = data->larb_imu[larbid].dev;
854de78657eSMiles Chen 	if (!larbdev)
855de78657eSMiles Chen 		return ERR_PTR(-EINVAL);
856de78657eSMiles Chen 
857635319a4SYong Wu 	link = device_link_add(dev, larbdev,
858635319a4SYong Wu 			       DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
859635319a4SYong Wu 	if (!link)
860635319a4SYong Wu 		dev_err(dev, "Unable to link %s\n", dev_name(larbdev));
86180e4592aSJoerg Roedel 	return &data->iommu;
8620df4fabeSYong Wu }
8630df4fabeSYong Wu 
86480e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
8650df4fabeSYong Wu {
866a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
867635319a4SYong Wu 	struct mtk_iommu_data *data;
868635319a4SYong Wu 	struct device *larbdev;
869635319a4SYong Wu 	unsigned int larbid;
870b16c0170SJoerg Roedel 
871635319a4SYong Wu 	data = dev_iommu_priv_get(dev);
872d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
873635319a4SYong Wu 		larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
874635319a4SYong Wu 		larbdev = data->larb_imu[larbid].dev;
875635319a4SYong Wu 		device_link_remove(dev, larbdev);
876d2e9a110SYong Wu 	}
8770df4fabeSYong Wu }
8780df4fabeSYong Wu 
87957fb481fSYong Wu static int mtk_iommu_get_group_id(struct device *dev, const struct mtk_iommu_plat_data *plat_data)
88057fb481fSYong Wu {
88157fb481fSYong Wu 	unsigned int bankid;
88257fb481fSYong Wu 
88357fb481fSYong Wu 	/*
88457fb481fSYong Wu 	 * If the bank function is enabled, each bank is a iommu group/domain.
88557fb481fSYong Wu 	 * Otherwise, each iova region is a iommu group/domain.
88657fb481fSYong Wu 	 */
88757fb481fSYong Wu 	bankid = mtk_iommu_get_bank_id(dev, plat_data);
88857fb481fSYong Wu 	if (bankid)
88957fb481fSYong Wu 		return bankid;
89057fb481fSYong Wu 
89157fb481fSYong Wu 	return mtk_iommu_get_iova_region_id(dev, plat_data);
89257fb481fSYong Wu }
89357fb481fSYong Wu 
8940df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
8950df4fabeSYong Wu {
8969e3a2a64SYong Wu 	struct mtk_iommu_data *c_data = dev_iommu_priv_get(dev), *data;
8979e3a2a64SYong Wu 	struct list_head *hw_list = c_data->hw_list;
898c3045f39SYong Wu 	struct iommu_group *group;
89957fb481fSYong Wu 	int groupid;
9000df4fabeSYong Wu 
9019e3a2a64SYong Wu 	data = mtk_iommu_get_frst_data(hw_list);
90258f0d1d5SRobin Murphy 	if (!data)
9030df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
9040df4fabeSYong Wu 
90557fb481fSYong Wu 	groupid = mtk_iommu_get_group_id(dev, data->plat_data);
90657fb481fSYong Wu 	if (groupid < 0)
90757fb481fSYong Wu 		return ERR_PTR(groupid);
908803cf9e5SYong Wu 
9090e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
91057fb481fSYong Wu 	group = data->m4u_group[groupid];
911c3045f39SYong Wu 	if (!group) {
912c3045f39SYong Wu 		group = iommu_group_alloc();
913c3045f39SYong Wu 		if (!IS_ERR(group))
91457fb481fSYong Wu 			data->m4u_group[groupid] = group;
9153a8d40b6SRobin Murphy 	} else {
916c3045f39SYong Wu 		iommu_group_ref_get(group);
9170df4fabeSYong Wu 	}
9180e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
919c3045f39SYong Wu 	return group;
9200df4fabeSYong Wu }
9210df4fabeSYong Wu 
9220df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
9230df4fabeSYong Wu {
9240df4fabeSYong Wu 	struct platform_device *m4updev;
9250df4fabeSYong Wu 
9260df4fabeSYong Wu 	if (args->args_count != 1) {
9270df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
9280df4fabeSYong Wu 			args->args_count);
9290df4fabeSYong Wu 		return -EINVAL;
9300df4fabeSYong Wu 	}
9310df4fabeSYong Wu 
9323524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
9330df4fabeSYong Wu 		/* Get the m4u device */
9340df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
9350df4fabeSYong Wu 		if (WARN_ON(!m4updev))
9360df4fabeSYong Wu 			return -EINVAL;
9370df4fabeSYong Wu 
9383524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
9390df4fabeSYong Wu 	}
9400df4fabeSYong Wu 
94158f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
9420df4fabeSYong Wu }
9430df4fabeSYong Wu 
944ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev,
945ab1d5281SYong Wu 				       struct list_head *head)
946ab1d5281SYong Wu {
947ab1d5281SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
948d72e0ff5SYong Wu 	unsigned int regionid = mtk_iommu_get_iova_region_id(dev, data->plat_data), i;
949ab1d5281SYong Wu 	const struct mtk_iommu_iova_region *resv, *curdom;
950ab1d5281SYong Wu 	struct iommu_resv_region *region;
951ab1d5281SYong Wu 	int prot = IOMMU_WRITE | IOMMU_READ;
952ab1d5281SYong Wu 
953d72e0ff5SYong Wu 	if ((int)regionid < 0)
954ab1d5281SYong Wu 		return;
955d72e0ff5SYong Wu 	curdom = data->plat_data->iova_region + regionid;
956ab1d5281SYong Wu 	for (i = 0; i < data->plat_data->iova_region_nr; i++) {
957ab1d5281SYong Wu 		resv = data->plat_data->iova_region + i;
958ab1d5281SYong Wu 
959ab1d5281SYong Wu 		/* Only reserve when the region is inside the current domain */
960ab1d5281SYong Wu 		if (resv->iova_base <= curdom->iova_base ||
961ab1d5281SYong Wu 		    resv->iova_base + resv->size >= curdom->iova_base + curdom->size)
962ab1d5281SYong Wu 			continue;
963ab1d5281SYong Wu 
964ab1d5281SYong Wu 		region = iommu_alloc_resv_region(resv->iova_base, resv->size,
9650251d010SLu Baolu 						 prot, IOMMU_RESV_RESERVED,
9660251d010SLu Baolu 						 GFP_KERNEL);
967ab1d5281SYong Wu 		if (!region)
968ab1d5281SYong Wu 			return;
969ab1d5281SYong Wu 
970ab1d5281SYong Wu 		list_add_tail(&region->list, head);
971ab1d5281SYong Wu 	}
972ab1d5281SYong Wu }
973ab1d5281SYong Wu 
974b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
9750df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
97680e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
97780e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
9780df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
9790df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
980ab1d5281SYong Wu 	.get_resv_regions = mtk_iommu_get_resv_regions,
9810df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
98218d8c74eSYong Wu 	.owner		= THIS_MODULE,
9839a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
9849a630a4bSLu Baolu 		.attach_dev	= mtk_iommu_attach_device,
98585637380SRobin Murphy 		.map_pages	= mtk_iommu_map,
98685637380SRobin Murphy 		.unmap_pages	= mtk_iommu_unmap,
9879a630a4bSLu Baolu 		.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
9889a630a4bSLu Baolu 		.iotlb_sync	= mtk_iommu_iotlb_sync,
9899a630a4bSLu Baolu 		.iotlb_sync_map	= mtk_iommu_sync_map,
9909a630a4bSLu Baolu 		.iova_to_phys	= mtk_iommu_iova_to_phys,
9919a630a4bSLu Baolu 		.free		= mtk_iommu_domain_free,
9929a630a4bSLu Baolu 	}
9930df4fabeSYong Wu };
9940df4fabeSYong Wu 
995e24453e1SYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data, unsigned int bankid)
9960df4fabeSYong Wu {
997e24453e1SYong Wu 	const struct mtk_iommu_bank_data *bankx = &data->bank[bankid];
99899ca0228SYong Wu 	const struct mtk_iommu_bank_data *bank0 = &data->bank[0];
9990df4fabeSYong Wu 	u32 regval;
10000df4fabeSYong Wu 
1001e24453e1SYong Wu 	/*
1002e24453e1SYong Wu 	 * Global control settings are in bank0. May re-init these global registers
1003e24453e1SYong Wu 	 * since no sure if there is bank0 consumers.
1004e24453e1SYong Wu 	 */
100586580ec9SAngeloGioacchino Del Regno 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, TF_PORT_TO_ADDR_MT8173)) {
1006acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
1007acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
100886444413SChao Hao 	} else {
100999ca0228SYong Wu 		regval = readl_relaxed(bank0->base + REG_MMU_CTRL_REG);
101086444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
101186444413SChao Hao 	}
101299ca0228SYong Wu 	writel_relaxed(regval, bank0->base + REG_MMU_CTRL_REG);
10130df4fabeSYong Wu 
10146b717796SChao Hao 	if (data->enable_4GB &&
10156b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
101630e2fccfSYong Wu 		/*
101730e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
101830e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
101930e2fccfSYong Wu 		 */
102030e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
102199ca0228SYong Wu 		writel_relaxed(regval, bank0->base + REG_MMU_VLD_PA_RNG);
102230e2fccfSYong Wu 	}
10239a87005eSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, DCM_DISABLE))
102499ca0228SYong Wu 		writel_relaxed(F_MMU_DCM, bank0->base + REG_MMU_DCM_DIS);
10259a87005eSYong Wu 	else
102699ca0228SYong Wu 		writel_relaxed(0, bank0->base + REG_MMU_DCM_DIS);
10279a87005eSYong Wu 
102835c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
102935c1b48dSChao Hao 		/* write command throttling mode */
103099ca0228SYong Wu 		regval = readl_relaxed(bank0->base + REG_MMU_WR_LEN_CTRL);
103135c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
103299ca0228SYong Wu 		writel_relaxed(regval, bank0->base + REG_MMU_WR_LEN_CTRL);
103335c1b48dSChao Hao 	}
1034e6dec923SYong Wu 
10356b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
103675eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
10374bb2bf4cSChao Hao 		regval = 0;
10384bb2bf4cSChao Hao 	} else {
103999ca0228SYong Wu 		regval = readl_relaxed(bank0->base + REG_MMU_MISC_CTRL);
1040d265a4adSYong Wu 		if (!MTK_IOMMU_HAS_FLAG(data->plat_data, STD_AXI_MODE))
10414bb2bf4cSChao Hao 			regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
10424bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
10434bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
104475eed350SChao Hao 	}
104599ca0228SYong Wu 	writel_relaxed(regval, bank0->base + REG_MMU_MISC_CTRL);
10460df4fabeSYong Wu 
1047e24453e1SYong Wu 	/* Independent settings for each bank */
1048634f57dfSYong Wu 	regval = F_L2_MULIT_HIT_EN |
1049634f57dfSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
1050634f57dfSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
1051634f57dfSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
1052634f57dfSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
1053634f57dfSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
1054e24453e1SYong Wu 	writel_relaxed(regval, bankx->base + REG_MMU_INT_CONTROL0);
1055634f57dfSYong Wu 
1056634f57dfSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
1057634f57dfSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
1058634f57dfSYong Wu 		F_INT_INVALID_PA_FAULT |
1059634f57dfSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
1060634f57dfSYong Wu 		F_INT_TLB_MISS_FAULT |
1061634f57dfSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
1062634f57dfSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
1063e24453e1SYong Wu 	writel_relaxed(regval, bankx->base + REG_MMU_INT_MAIN_CONTROL);
1064634f57dfSYong Wu 
1065634f57dfSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
1066634f57dfSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
1067634f57dfSYong Wu 	else
1068634f57dfSYong Wu 		regval = lower_32_bits(data->protect_base) |
1069634f57dfSYong Wu 			 upper_32_bits(data->protect_base);
1070e24453e1SYong Wu 	writel_relaxed(regval, bankx->base + REG_MMU_IVRP_PADDR);
1071634f57dfSYong Wu 
1072e24453e1SYong Wu 	if (devm_request_irq(bankx->parent_dev, bankx->irq, mtk_iommu_isr, 0,
1073e24453e1SYong Wu 			     dev_name(bankx->parent_dev), (void *)bankx)) {
1074e24453e1SYong Wu 		writel_relaxed(0, bankx->base + REG_MMU_PT_BASE_ADDR);
1075e24453e1SYong Wu 		dev_err(bankx->parent_dev, "Failed @ IRQ-%d Request\n", bankx->irq);
10760df4fabeSYong Wu 		return -ENODEV;
10770df4fabeSYong Wu 	}
10780df4fabeSYong Wu 
10790df4fabeSYong Wu 	return 0;
10800df4fabeSYong Wu }
10810df4fabeSYong Wu 
10820df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
10830df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
10840df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
10850df4fabeSYong Wu };
10860df4fabeSYong Wu 
1087d2e9a110SYong Wu static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **match,
1088d2e9a110SYong Wu 				  struct mtk_iommu_data *data)
1089d2e9a110SYong Wu {
10906cde583dSYong Wu 	struct device_node *larbnode, *frst_avail_smicomm_node = NULL;
1091dcb40e9fSYong Wu 	struct platform_device *plarbdev, *pcommdev;
1092d2e9a110SYong Wu 	struct device_link *link;
1093d2e9a110SYong Wu 	int i, larb_nr, ret;
1094d2e9a110SYong Wu 
1095d2e9a110SYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL);
1096d2e9a110SYong Wu 	if (larb_nr < 0)
1097d2e9a110SYong Wu 		return larb_nr;
1098ef693a84SGuenter Roeck 	if (larb_nr == 0 || larb_nr > MTK_LARB_NR_MAX)
1099ef693a84SGuenter Roeck 		return -EINVAL;
1100d2e9a110SYong Wu 
1101d2e9a110SYong Wu 	for (i = 0; i < larb_nr; i++) {
11026cde583dSYong Wu 		struct device_node *smicomm_node, *smi_subcomm_node;
1103d2e9a110SYong Wu 		u32 id;
1104d2e9a110SYong Wu 
1105d2e9a110SYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
110626593928SYong Wu 		if (!larbnode) {
110726593928SYong Wu 			ret = -EINVAL;
110826593928SYong Wu 			goto err_larbdev_put;
110926593928SYong Wu 		}
1110d2e9a110SYong Wu 
1111d2e9a110SYong Wu 		if (!of_device_is_available(larbnode)) {
1112d2e9a110SYong Wu 			of_node_put(larbnode);
1113d2e9a110SYong Wu 			continue;
1114d2e9a110SYong Wu 		}
1115d2e9a110SYong Wu 
1116d2e9a110SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
1117d2e9a110SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
1118d2e9a110SYong Wu 			id = i;
1119ef693a84SGuenter Roeck 		if (id >= MTK_LARB_NR_MAX) {
1120ef693a84SGuenter Roeck 			of_node_put(larbnode);
1121ef693a84SGuenter Roeck 			ret = -EINVAL;
1122ef693a84SGuenter Roeck 			goto err_larbdev_put;
1123ef693a84SGuenter Roeck 		}
1124d2e9a110SYong Wu 
1125d2e9a110SYong Wu 		plarbdev = of_find_device_by_node(larbnode);
1126d2e9a110SYong Wu 		of_node_put(larbnode);
1127d2e9a110SYong Wu 		if (!plarbdev) {
112826593928SYong Wu 			ret = -ENODEV;
112926593928SYong Wu 			goto err_larbdev_put;
1130d2e9a110SYong Wu 		}
1131ef693a84SGuenter Roeck 		if (data->larb_imu[id].dev) {
1132ef693a84SGuenter Roeck 			platform_device_put(plarbdev);
1133ef693a84SGuenter Roeck 			ret = -EEXIST;
1134ef693a84SGuenter Roeck 			goto err_larbdev_put;
1135d2e9a110SYong Wu 		}
1136d2e9a110SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
1137d2e9a110SYong Wu 
113826593928SYong Wu 		if (!plarbdev->dev.driver) {
113926593928SYong Wu 			ret = -EPROBE_DEFER;
114026593928SYong Wu 			goto err_larbdev_put;
1141d2e9a110SYong Wu 		}
1142d2e9a110SYong Wu 
1143f7b71d0dSYong Wu 		/* Get smi-(sub)-common dev from the last larb. */
1144f7b71d0dSYong Wu 		smi_subcomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
11456cde583dSYong Wu 		if (!smi_subcomm_node) {
11466cde583dSYong Wu 			ret = -EINVAL;
11476cde583dSYong Wu 			goto err_larbdev_put;
11486cde583dSYong Wu 		}
1149d2e9a110SYong Wu 
1150f7b71d0dSYong Wu 		/*
1151f7b71d0dSYong Wu 		 * It may have two level smi-common. the node is smi-sub-common if it
1152f7b71d0dSYong Wu 		 * has a new mediatek,smi property. otherwise it is smi-commmon.
1153f7b71d0dSYong Wu 		 */
1154f7b71d0dSYong Wu 		smicomm_node = of_parse_phandle(smi_subcomm_node, "mediatek,smi", 0);
1155f7b71d0dSYong Wu 		if (smicomm_node)
1156f7b71d0dSYong Wu 			of_node_put(smi_subcomm_node);
1157f7b71d0dSYong Wu 		else
1158f7b71d0dSYong Wu 			smicomm_node = smi_subcomm_node;
1159f7b71d0dSYong Wu 
11606cde583dSYong Wu 		/*
11616cde583dSYong Wu 		 * All the larbs that connect to one IOMMU must connect with the same
11626cde583dSYong Wu 		 * smi-common.
11636cde583dSYong Wu 		 */
11646cde583dSYong Wu 		if (!frst_avail_smicomm_node) {
11656cde583dSYong Wu 			frst_avail_smicomm_node = smicomm_node;
11666cde583dSYong Wu 		} else if (frst_avail_smicomm_node != smicomm_node) {
11676cde583dSYong Wu 			dev_err(dev, "mediatek,smi property is not right @larb%d.", id);
1168d2e9a110SYong Wu 			of_node_put(smicomm_node);
11696cde583dSYong Wu 			ret = -EINVAL;
11706cde583dSYong Wu 			goto err_larbdev_put;
11716cde583dSYong Wu 		} else {
11726cde583dSYong Wu 			of_node_put(smicomm_node);
11736cde583dSYong Wu 		}
11746cde583dSYong Wu 
11756cde583dSYong Wu 		component_match_add(dev, match, component_compare_dev, &plarbdev->dev);
11766cde583dSYong Wu 		platform_device_put(plarbdev);
11776cde583dSYong Wu 	}
11786cde583dSYong Wu 
11796cde583dSYong Wu 	if (!frst_avail_smicomm_node)
11806cde583dSYong Wu 		return -EINVAL;
11816cde583dSYong Wu 
11826cde583dSYong Wu 	pcommdev = of_find_device_by_node(frst_avail_smicomm_node);
11836cde583dSYong Wu 	of_node_put(frst_avail_smicomm_node);
1184dcb40e9fSYong Wu 	if (!pcommdev)
1185dcb40e9fSYong Wu 		return -ENODEV;
1186dcb40e9fSYong Wu 	data->smicomm_dev = &pcommdev->dev;
1187d2e9a110SYong Wu 
1188d2e9a110SYong Wu 	link = device_link_add(data->smicomm_dev, dev,
1189d2e9a110SYong Wu 			       DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
1190dcb40e9fSYong Wu 	platform_device_put(pcommdev);
1191d2e9a110SYong Wu 	if (!link) {
1192d2e9a110SYong Wu 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
1193d2e9a110SYong Wu 		return -EINVAL;
1194d2e9a110SYong Wu 	}
1195d2e9a110SYong Wu 	return 0;
119626593928SYong Wu 
119726593928SYong Wu err_larbdev_put:
1198462e768bSDan Carpenter 	for (i = MTK_LARB_NR_MAX - 1; i >= 0; i--) {
119926593928SYong Wu 		if (!data->larb_imu[i].dev)
120026593928SYong Wu 			continue;
120126593928SYong Wu 		put_device(data->larb_imu[i].dev);
120226593928SYong Wu 	}
120326593928SYong Wu 	return ret;
1204d2e9a110SYong Wu }
1205d2e9a110SYong Wu 
12060df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
12070df4fabeSYong Wu {
12080df4fabeSYong Wu 	struct mtk_iommu_data   *data;
12090df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
12100df4fabeSYong Wu 	struct resource         *res;
1211b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
12120df4fabeSYong Wu 	struct component_match  *match = NULL;
1213c2c59456SMiles Chen 	struct regmap		*infracfg;
12140df4fabeSYong Wu 	void                    *protect;
121542d57fc5SYong Wu 	int                     ret, banks_num, i = 0;
1216c2c59456SMiles Chen 	u32			val;
1217c2c59456SMiles Chen 	char                    *p;
121899ca0228SYong Wu 	struct mtk_iommu_bank_data *bank;
121999ca0228SYong Wu 	void __iomem		*base;
12200df4fabeSYong Wu 
12210df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
12220df4fabeSYong Wu 	if (!data)
12230df4fabeSYong Wu 		return -ENOMEM;
12240df4fabeSYong Wu 	data->dev = dev;
1225cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
12260df4fabeSYong Wu 
12270df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
12280df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
12290df4fabeSYong Wu 	if (!protect)
12300df4fabeSYong Wu 		return -ENOMEM;
12310df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
12320df4fabeSYong Wu 
1233c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
12347d748ffdSAngeloGioacchino Del Regno 		infracfg = syscon_regmap_lookup_by_phandle(dev->of_node, "mediatek,infracfg");
12357d748ffdSAngeloGioacchino Del Regno 		if (IS_ERR(infracfg)) {
12367d748ffdSAngeloGioacchino Del Regno 			/*
12377d748ffdSAngeloGioacchino Del Regno 			 * Legacy devicetrees will not specify a phandle to
12387d748ffdSAngeloGioacchino Del Regno 			 * mediatek,infracfg: in that case, we use the older
12397d748ffdSAngeloGioacchino Del Regno 			 * way to retrieve a syscon to infra.
12407d748ffdSAngeloGioacchino Del Regno 			 *
12417d748ffdSAngeloGioacchino Del Regno 			 * This is for retrocompatibility purposes only, hence
12427d748ffdSAngeloGioacchino Del Regno 			 * no more compatibles shall be added to this.
12437d748ffdSAngeloGioacchino Del Regno 			 */
1244c2c59456SMiles Chen 			switch (data->plat_data->m4u_plat) {
1245c2c59456SMiles Chen 			case M4U_MT2712:
1246c2c59456SMiles Chen 				p = "mediatek,mt2712-infracfg";
1247c2c59456SMiles Chen 				break;
1248c2c59456SMiles Chen 			case M4U_MT8173:
1249c2c59456SMiles Chen 				p = "mediatek,mt8173-infracfg";
1250c2c59456SMiles Chen 				break;
1251c2c59456SMiles Chen 			default:
1252c2c59456SMiles Chen 				p = NULL;
1253c2c59456SMiles Chen 			}
1254c2c59456SMiles Chen 
1255c2c59456SMiles Chen 			infracfg = syscon_regmap_lookup_by_compatible(p);
1256c2c59456SMiles Chen 			if (IS_ERR(infracfg))
1257c2c59456SMiles Chen 				return PTR_ERR(infracfg);
12587d748ffdSAngeloGioacchino Del Regno 		}
1259c2c59456SMiles Chen 
1260c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
1261c2c59456SMiles Chen 		if (ret)
1262c2c59456SMiles Chen 			return ret;
1263c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
1264c2c59456SMiles Chen 	}
126501e23c93SYong Wu 
126642d57fc5SYong Wu 	banks_num = data->plat_data->banks_num;
12670df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
126873b6924cSYang Yingliang 	if (!res)
126973b6924cSYang Yingliang 		return -EINVAL;
127042d57fc5SYong Wu 	if (resource_size(res) < banks_num * MTK_IOMMU_BANK_SZ) {
127142d57fc5SYong Wu 		dev_err(dev, "banknr %d. res %pR is not enough.\n", banks_num, res);
127242d57fc5SYong Wu 		return -EINVAL;
127342d57fc5SYong Wu 	}
127499ca0228SYong Wu 	base = devm_ioremap_resource(dev, res);
127599ca0228SYong Wu 	if (IS_ERR(base))
127699ca0228SYong Wu 		return PTR_ERR(base);
1277b16c0170SJoerg Roedel 	ioaddr = res->start;
12780df4fabeSYong Wu 
127999ca0228SYong Wu 	data->bank = devm_kmalloc(dev, banks_num * sizeof(*data->bank), GFP_KERNEL);
128099ca0228SYong Wu 	if (!data->bank)
128199ca0228SYong Wu 		return -ENOMEM;
128299ca0228SYong Wu 
128342d57fc5SYong Wu 	do {
128442d57fc5SYong Wu 		if (!data->plat_data->banks_enable[i])
128542d57fc5SYong Wu 			continue;
128642d57fc5SYong Wu 		bank = &data->bank[i];
128742d57fc5SYong Wu 		bank->id = i;
128842d57fc5SYong Wu 		bank->base = base + i * MTK_IOMMU_BANK_SZ;
128999ca0228SYong Wu 		bank->m4u_dom = NULL;
129042d57fc5SYong Wu 
129142d57fc5SYong Wu 		bank->irq = platform_get_irq(pdev, i);
129299ca0228SYong Wu 		if (bank->irq < 0)
129399ca0228SYong Wu 			return bank->irq;
129499ca0228SYong Wu 		bank->parent_dev = dev;
129599ca0228SYong Wu 		bank->parent_data = data;
129699ca0228SYong Wu 		spin_lock_init(&bank->tlb_lock);
129742d57fc5SYong Wu 	} while (++i < banks_num);
12980df4fabeSYong Wu 
12996b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
13000df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
13010df4fabeSYong Wu 		if (IS_ERR(data->bclk))
13020df4fabeSYong Wu 			return PTR_ERR(data->bclk);
13032aa4c259SYong Wu 	}
13040df4fabeSYong Wu 
1305f045e9dfSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, PGTABLE_PA_35_EN)) {
1306f045e9dfSYong Wu 		ret = dma_set_mask(dev, DMA_BIT_MASK(35));
1307f045e9dfSYong Wu 		if (ret) {
1308f045e9dfSYong Wu 			dev_err(dev, "Failed to set dma_mask 35.\n");
1309f045e9dfSYong Wu 			return ret;
1310f045e9dfSYong Wu 		}
1311f045e9dfSYong Wu 	}
1312f045e9dfSYong Wu 
1313c0b57581SYong Wu 	pm_runtime_enable(dev);
1314c0b57581SYong Wu 
1315d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1316d2e9a110SYong Wu 		ret = mtk_iommu_mm_dts_parse(dev, &match, data);
1317d2e9a110SYong Wu 		if (ret) {
13183168010dSNícolas F. R. A. Prado 			dev_err_probe(dev, ret, "mm dts parse fail\n");
1319c0b57581SYong Wu 			goto out_runtime_disable;
1320baf94e6eSYong Wu 		}
132121fd9be4SAngeloGioacchino Del Regno 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {
132221fd9be4SAngeloGioacchino Del Regno 		p = data->plat_data->pericfg_comp_str;
132321fd9be4SAngeloGioacchino Del Regno 		data->pericfg = syscon_regmap_lookup_by_compatible(p);
132421fd9be4SAngeloGioacchino Del Regno 		if (IS_ERR(data->pericfg)) {
132521fd9be4SAngeloGioacchino Del Regno 			ret = PTR_ERR(data->pericfg);
1326f9b8c9b2SYong Wu 			goto out_runtime_disable;
1327f9b8c9b2SYong Wu 		}
1328d2e9a110SYong Wu 	}
1329baf94e6eSYong Wu 
13300df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
13310e5a3f2eSYong Wu 	mutex_init(&data->mutex);
13320df4fabeSYong Wu 
1333b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
1334b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
1335b16c0170SJoerg Roedel 	if (ret)
1336baf94e6eSYong Wu 		goto out_link_remove;
1337b16c0170SJoerg Roedel 
13382d471b20SRobin Murphy 	ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
1339b16c0170SJoerg Roedel 	if (ret)
1340986d9ec5SYong Wu 		goto out_sysfs_remove;
1341b16c0170SJoerg Roedel 
13429e3a2a64SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, SHARE_PGTABLE)) {
13439e3a2a64SYong Wu 		list_add_tail(&data->list, data->plat_data->hw_list);
13449e3a2a64SYong Wu 		data->hw_list = data->plat_data->hw_list;
13459e3a2a64SYong Wu 	} else {
13469e3a2a64SYong Wu 		INIT_LIST_HEAD(&data->hw_list_head);
13479e3a2a64SYong Wu 		list_add_tail(&data->list, &data->hw_list_head);
13489e3a2a64SYong Wu 		data->hw_list = &data->hw_list_head;
13499e3a2a64SYong Wu 	}
13507c3a2ec0SYong Wu 
1351d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1352986d9ec5SYong Wu 		ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
1353986d9ec5SYong Wu 		if (ret)
1354e7629070SYong Wu 			goto out_list_del;
1355e7629070SYong Wu 	}
1356986d9ec5SYong Wu 	return ret;
1357986d9ec5SYong Wu 
1358986d9ec5SYong Wu out_list_del:
1359986d9ec5SYong Wu 	list_del(&data->list);
1360986d9ec5SYong Wu 	iommu_device_unregister(&data->iommu);
1361986d9ec5SYong Wu out_sysfs_remove:
1362986d9ec5SYong Wu 	iommu_device_sysfs_remove(&data->iommu);
1363baf94e6eSYong Wu out_link_remove:
1364d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
1365baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, dev);
1366c0b57581SYong Wu out_runtime_disable:
1367c0b57581SYong Wu 	pm_runtime_disable(dev);
1368986d9ec5SYong Wu 	return ret;
13690df4fabeSYong Wu }
13700df4fabeSYong Wu 
1371d8149d39SUwe Kleine-König static void mtk_iommu_remove(struct platform_device *pdev)
13720df4fabeSYong Wu {
13730df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
137442d57fc5SYong Wu 	struct mtk_iommu_bank_data *bank;
137542d57fc5SYong Wu 	int i;
13760df4fabeSYong Wu 
1377b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
1378b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
1379b16c0170SJoerg Roedel 
1380ee55f75eSYong Wu 	list_del(&data->list);
13810df4fabeSYong Wu 
1382d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1383baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, &pdev->dev);
1384d2e9a110SYong Wu 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
1385d2e9a110SYong Wu 	}
1386c0b57581SYong Wu 	pm_runtime_disable(&pdev->dev);
138742d57fc5SYong Wu 	for (i = 0; i < data->plat_data->banks_num; i++) {
138842d57fc5SYong Wu 		bank = &data->bank[i];
138942d57fc5SYong Wu 		if (!bank->m4u_dom)
139042d57fc5SYong Wu 			continue;
139199ca0228SYong Wu 		devm_free_irq(&pdev->dev, bank->irq, bank);
139242d57fc5SYong Wu 	}
13930df4fabeSYong Wu }
13940df4fabeSYong Wu 
139534665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
13960df4fabeSYong Wu {
13970df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
13980df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1399d7127de1SYong Wu 	void __iomem *base;
1400d7127de1SYong Wu 	int i = 0;
14010df4fabeSYong Wu 
1402d7127de1SYong Wu 	base = data->bank[i].base;
140335c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
140475eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
14050df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
14060df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
1407b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
1408d7127de1SYong Wu 	do {
1409d7127de1SYong Wu 		if (!data->plat_data->banks_enable[i])
1410d7127de1SYong Wu 			continue;
1411d7127de1SYong Wu 		base = data->bank[i].base;
1412d7127de1SYong Wu 		reg->int_control[i] = readl_relaxed(base + REG_MMU_INT_CONTROL0);
1413d7127de1SYong Wu 		reg->int_main_control[i] = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
1414d7127de1SYong Wu 		reg->ivrp_paddr[i] = readl_relaxed(base + REG_MMU_IVRP_PADDR);
1415d7127de1SYong Wu 	} while (++i < data->plat_data->banks_num);
14166254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
14170df4fabeSYong Wu 	return 0;
14180df4fabeSYong Wu }
14190df4fabeSYong Wu 
142034665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
14210df4fabeSYong Wu {
14220df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
14230df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1424d7127de1SYong Wu 	struct mtk_iommu_domain *m4u_dom;
1425d7127de1SYong Wu 	void __iomem *base;
1426d7127de1SYong Wu 	int ret, i = 0;
14270df4fabeSYong Wu 
14286254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
14296254b64fSYong Wu 	if (ret) {
14306254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
14316254b64fSYong Wu 		return ret;
14326254b64fSYong Wu 	}
1433b34ea31fSDafna Hirschfeld 
1434b34ea31fSDafna Hirschfeld 	/*
1435b34ea31fSDafna Hirschfeld 	 * Uppon first resume, only enable the clk and return, since the values of the
1436b34ea31fSDafna Hirschfeld 	 * registers are not yet set.
1437b34ea31fSDafna Hirschfeld 	 */
1438d7127de1SYong Wu 	if (!reg->wr_len_ctrl)
1439b34ea31fSDafna Hirschfeld 		return 0;
1440b34ea31fSDafna Hirschfeld 
1441d7127de1SYong Wu 	base = data->bank[i].base;
144235c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
144375eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
14440df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
14450df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
1446b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
1447d7127de1SYong Wu 	do {
1448d7127de1SYong Wu 		m4u_dom = data->bank[i].m4u_dom;
1449d7127de1SYong Wu 		if (!data->plat_data->banks_enable[i] || !m4u_dom)
1450d7127de1SYong Wu 			continue;
1451d7127de1SYong Wu 		base = data->bank[i].base;
1452d7127de1SYong Wu 		writel_relaxed(reg->int_control[i], base + REG_MMU_INT_CONTROL0);
1453d7127de1SYong Wu 		writel_relaxed(reg->int_main_control[i], base + REG_MMU_INT_MAIN_CONTROL);
1454d7127de1SYong Wu 		writel_relaxed(reg->ivrp_paddr[i], base + REG_MMU_IVRP_PADDR);
1455301c3ca1SYunfei Wang 		writel(m4u_dom->cfg.arm_v7s_cfg.ttbr, base + REG_MMU_PT_BASE_ADDR);
1456d7127de1SYong Wu 	} while (++i < data->plat_data->banks_num);
14574f23f6d4SYong Wu 
14584f23f6d4SYong Wu 	/*
14594f23f6d4SYong Wu 	 * Users may allocate dma buffer before they call pm_runtime_get,
14604f23f6d4SYong Wu 	 * in which case it will lack the necessary tlb flush.
14614f23f6d4SYong Wu 	 * Thus, make sure to update the tlb after each PM resume.
14624f23f6d4SYong Wu 	 */
14634f23f6d4SYong Wu 	mtk_iommu_tlb_flush_all(data);
14640df4fabeSYong Wu 	return 0;
14650df4fabeSYong Wu }
14660df4fabeSYong Wu 
1467e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
146834665c79SYong Wu 	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
146934665c79SYong Wu 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
147034665c79SYong Wu 				     pm_runtime_force_resume)
14710df4fabeSYong Wu };
14720df4fabeSYong Wu 
1473cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
1474cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
1475d2e9a110SYong Wu 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG | SHARE_PGTABLE |
1476d2e9a110SYong Wu 			MTK_IOMMU_TYPE_MM,
14779e3a2a64SYong Wu 	.hw_list      = &m4ulist,
1478b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1479585e58f4SYong Wu 	.iova_region  = single_domain,
148099ca0228SYong Wu 	.banks_num    = 1,
148199ca0228SYong Wu 	.banks_enable = {true},
1482585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
148337276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
1484cecdce9dSYong Wu };
1485cecdce9dSYong Wu 
1486068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
1487068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
1488d2e9a110SYong Wu 	.flags         = HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN | WR_THROT_EN |
1489301c3ca1SYunfei Wang 			 MTK_IOMMU_TYPE_MM | PGTABLE_PA_35_EN,
1490068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
149199ca0228SYong Wu 	.banks_num    = 1,
149299ca0228SYong Wu 	.banks_enable = {true},
1493585e58f4SYong Wu 	.iova_region   = single_domain,
1494585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
1495068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
1496cecdce9dSYong Wu };
1497cecdce9dSYong Wu 
1498717ec15eSAngeloGioacchino Del Regno static const struct mtk_iommu_plat_data mt6795_data = {
1499717ec15eSAngeloGioacchino Del Regno 	.m4u_plat     = M4U_MT6795,
1500717ec15eSAngeloGioacchino Del Regno 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
1501717ec15eSAngeloGioacchino Del Regno 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM |
1502717ec15eSAngeloGioacchino Del Regno 			TF_PORT_TO_ADDR_MT8173,
1503717ec15eSAngeloGioacchino Del Regno 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1504717ec15eSAngeloGioacchino Del Regno 	.banks_num    = 1,
1505717ec15eSAngeloGioacchino Del Regno 	.banks_enable = {true},
1506717ec15eSAngeloGioacchino Del Regno 	.iova_region  = single_domain,
1507717ec15eSAngeloGioacchino Del Regno 	.iova_region_nr = ARRAY_SIZE(single_domain),
1508717ec15eSAngeloGioacchino Del Regno 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}}, /* Linear mapping. */
1509717ec15eSAngeloGioacchino Del Regno };
1510717ec15eSAngeloGioacchino Del Regno 
15113c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
15123c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
1513d2e9a110SYong Wu 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
15143c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
151599ca0228SYong Wu 	.banks_num    = 1,
151699ca0228SYong Wu 	.banks_enable = {true},
1517585e58f4SYong Wu 	.iova_region  = single_domain,
1518585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
15193c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
15203c213562SFabien Parent };
15213c213562SFabien Parent 
1522cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
1523cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
1524d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
152586580ec9SAngeloGioacchino Del Regno 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM |
152686580ec9SAngeloGioacchino Del Regno 			TF_PORT_TO_ADDR_MT8173,
1527b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
152899ca0228SYong Wu 	.banks_num    = 1,
152999ca0228SYong Wu 	.banks_enable = {true},
1530585e58f4SYong Wu 	.iova_region  = single_domain,
1531585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
153237276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
1533cecdce9dSYong Wu };
1534cecdce9dSYong Wu 
1535907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
1536907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
1537d2e9a110SYong Wu 	.flags        = RESET_AXI | MTK_IOMMU_TYPE_MM,
1538b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
153999ca0228SYong Wu 	.banks_num    = 1,
154099ca0228SYong Wu 	.banks_enable = {true},
1541585e58f4SYong Wu 	.iova_region  = single_domain,
1542585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
154337276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1544907ba6a1SYong Wu };
1545907ba6a1SYong Wu 
1546f5d4233aSYong Wu static const unsigned int mt8186_larb_region_msk[MT8192_MULTI_REGION_NR_MAX][MTK_LARB_NR_MAX] = {
1547f5d4233aSYong Wu 	[0] = {~0, ~0, ~0},			/* Region0: all ports for larb0/1/2 */
1548f5d4233aSYong Wu 	[1] = {0, 0, 0, 0, ~0, 0, 0, ~0},	/* Region1: larb4/7 */
1549f5d4233aSYong Wu 	[2] = {0, 0, 0, 0, 0, 0, 0, 0,		/* Region2: larb8/9/11/13/16/17/19/20 */
1550f5d4233aSYong Wu 	       ~0, ~0, 0, ~0, 0, ~(u32)(BIT(9) | BIT(10)), 0, 0,
1551f5d4233aSYong Wu 						/* larb13: the other ports except port9/10 */
1552f5d4233aSYong Wu 	       ~0, ~0, 0, ~0, ~0},
1553f5d4233aSYong Wu 	[3] = {0},
1554f5d4233aSYong Wu 	[4] = {[13] = BIT(9) | BIT(10)},	/* larb13 port9/10 */
1555f5d4233aSYong Wu 	[5] = {[14] = ~0},			/* larb14 */
1556f5d4233aSYong Wu };
1557f5d4233aSYong Wu 
1558e8d7ccaaSYong Wu static const struct mtk_iommu_plat_data mt8186_data_mm = {
1559e8d7ccaaSYong Wu 	.m4u_plat       = M4U_MT8186,
1560e8d7ccaaSYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1561e8d7ccaaSYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
1562e8d7ccaaSYong Wu 	.larbid_remap   = {{0}, {1, MTK_INVALID_LARBID, 8}, {4}, {7}, {2}, {9, 11, 19, 20},
1563e8d7ccaaSYong Wu 			   {MTK_INVALID_LARBID, 14, 16},
1564e8d7ccaaSYong Wu 			   {MTK_INVALID_LARBID, 13, MTK_INVALID_LARBID, 17}},
1565e8d7ccaaSYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
1566e8d7ccaaSYong Wu 	.banks_num      = 1,
1567e8d7ccaaSYong Wu 	.banks_enable   = {true},
1568e8d7ccaaSYong Wu 	.iova_region    = mt8192_multi_dom,
1569e8d7ccaaSYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
1570f5d4233aSYong Wu 	.iova_region_larb_msk = mt8186_larb_region_msk,
1571e8d7ccaaSYong Wu };
1572e8d7ccaaSYong Wu 
15736b1317f9SYong Wu static const unsigned int mt8192_larb_region_msk[MT8192_MULTI_REGION_NR_MAX][MTK_LARB_NR_MAX] = {
15746b1317f9SYong Wu 	[0] = {~0, ~0},				/* Region0: larb0/1 */
15756b1317f9SYong Wu 	[1] = {0, 0, 0, 0, ~0, ~0, 0, ~0},	/* Region1: larb4/5/7 */
15766b1317f9SYong Wu 	[2] = {0, 0, ~0, 0, 0, 0, 0, 0,		/* Region2: larb2/9/11/13/14/16/17/18/19/20 */
15776b1317f9SYong Wu 	       0, ~0, 0, ~0, 0, ~(u32)(BIT(9) | BIT(10)), ~(u32)(BIT(4) | BIT(5)), 0,
15786b1317f9SYong Wu 	       ~0, ~0, ~0, ~0, ~0},
15796b1317f9SYong Wu 	[3] = {0},
15806b1317f9SYong Wu 	[4] = {[13] = BIT(9) | BIT(10)},	/* larb13 port9/10 */
15816b1317f9SYong Wu 	[5] = {[14] = BIT(4) | BIT(5)},		/* larb14 port4/5 */
15820df4fabeSYong Wu };
15830df4fabeSYong Wu 
15849e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = {
15859e3489e0SYong Wu 	.m4u_plat       = M4U_MT8192,
15869ec30c09SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1587d2e9a110SYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
15889e3489e0SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
158999ca0228SYong Wu 	.banks_num      = 1,
159099ca0228SYong Wu 	.banks_enable   = {true},
15919e3489e0SYong Wu 	.iova_region    = mt8192_multi_dom,
15929e3489e0SYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
15936b1317f9SYong Wu 	.iova_region_larb_msk = mt8192_larb_region_msk,
15949e3489e0SYong Wu 	.larbid_remap   = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20},
15959e3489e0SYong Wu 			   {0, 14, 16}, {0, 13, 18, 17}},
15969e3489e0SYong Wu };
15979e3489e0SYong Wu 
1598ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_infra = {
1599ef68a193SYong Wu 	.m4u_plat	  = M4U_MT8195,
1600ef68a193SYong Wu 	.flags            = WR_THROT_EN | DCM_DISABLE | STD_AXI_MODE | PM_CLK_AO |
1601ef68a193SYong Wu 			    MTK_IOMMU_TYPE_INFRA | IFA_IOMMU_PCIE_SUPPORT,
1602ef68a193SYong Wu 	.pericfg_comp_str = "mediatek,mt8195-pericfg_ao",
1603ef68a193SYong Wu 	.inv_sel_reg      = REG_MMU_INV_SEL_GEN2,
16047597e3c5SYong Wu 	.banks_num	  = 5,
16057597e3c5SYong Wu 	.banks_enable     = {true, false, false, false, true},
16067597e3c5SYong Wu 	.banks_portmsk    = {[0] = GENMASK(19, 16),     /* PCIe */
16077597e3c5SYong Wu 			     [4] = GENMASK(31, 20),     /* USB */
16087597e3c5SYong Wu 			    },
1609ef68a193SYong Wu 	.iova_region      = single_domain,
1610ef68a193SYong Wu 	.iova_region_nr   = ARRAY_SIZE(single_domain),
1611ef68a193SYong Wu };
1612ef68a193SYong Wu 
1613a43e767dSYong Wu static const unsigned int mt8195_larb_region_msk[MT8192_MULTI_REGION_NR_MAX][MTK_LARB_NR_MAX] = {
1614a43e767dSYong Wu 	[0] = {~0, ~0, ~0, ~0},               /* Region0: all ports for larb0/1/2/3 */
1615a43e767dSYong Wu 	[1] = {0, 0, 0, 0, 0, 0, 0, 0,
1616a43e767dSYong Wu 	       0, 0, 0, 0, 0, 0, 0, 0,
1617a43e767dSYong Wu 	       0, 0, 0, ~0, ~0, ~0, ~0, ~0,   /* Region1: larb19/20/21/22/23/24 */
1618a43e767dSYong Wu 	       ~0},
1619a43e767dSYong Wu 	[2] = {0, 0, 0, 0, ~0, ~0, ~0, ~0,    /* Region2: the other larbs. */
1620a43e767dSYong Wu 	       ~0, ~0, ~0, ~0, ~0, ~0, ~0, ~0,
1621a43e767dSYong Wu 	       ~0, ~0, 0, 0, 0, 0, 0, 0,
1622a43e767dSYong Wu 	       0, ~0, ~0, ~0, ~0},
1623a43e767dSYong Wu 	[3] = {0},
1624a43e767dSYong Wu 	[4] = {[18] = BIT(0) | BIT(1)},       /* Only larb18 port0/1 */
1625a43e767dSYong Wu 	[5] = {[18] = BIT(2) | BIT(3)},       /* Only larb18 port2/3 */
1626a43e767dSYong Wu };
1627a43e767dSYong Wu 
1628ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_vdo = {
1629ef68a193SYong Wu 	.m4u_plat	= M4U_MT8195,
1630ef68a193SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1631ef68a193SYong Wu 			  WR_THROT_EN | IOVA_34_EN | SHARE_PGTABLE | MTK_IOMMU_TYPE_MM,
1632ef68a193SYong Wu 	.hw_list        = &m4ulist,
1633ef68a193SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
163499ca0228SYong Wu 	.banks_num      = 1,
163599ca0228SYong Wu 	.banks_enable   = {true},
1636ef68a193SYong Wu 	.iova_region	= mt8192_multi_dom,
1637ef68a193SYong Wu 	.iova_region_nr	= ARRAY_SIZE(mt8192_multi_dom),
1638a43e767dSYong Wu 	.iova_region_larb_msk = mt8195_larb_region_msk,
1639ef68a193SYong Wu 	.larbid_remap   = {{2, 0}, {21}, {24}, {7}, {19}, {9, 10, 11},
1640ef68a193SYong Wu 			   {13, 17, 15/* 17b */, 25}, {5}},
1641ef68a193SYong Wu };
1642ef68a193SYong Wu 
1643ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_vpp = {
1644ef68a193SYong Wu 	.m4u_plat	= M4U_MT8195,
1645ef68a193SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_3BITS | OUT_ORDER_WR_EN |
1646ef68a193SYong Wu 			  WR_THROT_EN | IOVA_34_EN | SHARE_PGTABLE | MTK_IOMMU_TYPE_MM,
1647ef68a193SYong Wu 	.hw_list        = &m4ulist,
1648ef68a193SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
164999ca0228SYong Wu 	.banks_num      = 1,
165099ca0228SYong Wu 	.banks_enable   = {true},
1651ef68a193SYong Wu 	.iova_region	= mt8192_multi_dom,
1652ef68a193SYong Wu 	.iova_region_nr	= ARRAY_SIZE(mt8192_multi_dom),
1653a43e767dSYong Wu 	.iova_region_larb_msk = mt8195_larb_region_msk,
1654ef68a193SYong Wu 	.larbid_remap   = {{1}, {3},
1655ef68a193SYong Wu 			   {22, MTK_INVALID_LARBID, MTK_INVALID_LARBID, MTK_INVALID_LARBID, 23},
1656ef68a193SYong Wu 			   {8}, {20}, {12},
1657ef68a193SYong Wu 			   /* 16: 16a; 29: 16b; 30: CCUtop0; 31: CCUtop1 */
1658ef68a193SYong Wu 			   {14, 16, 29, 26, 30, 31, 18},
1659ef68a193SYong Wu 			   {4, MTK_INVALID_LARBID, MTK_INVALID_LARBID, MTK_INVALID_LARBID, 6}},
1660ef68a193SYong Wu };
1661ef68a193SYong Wu 
16623cd0e4a3SFabien Parent static const struct mtk_iommu_plat_data mt8365_data = {
16633cd0e4a3SFabien Parent 	.m4u_plat	= M4U_MT8365,
16643cd0e4a3SFabien Parent 	.flags		= RESET_AXI | INT_ID_PORT_WIDTH_6,
16653cd0e4a3SFabien Parent 	.inv_sel_reg	= REG_MMU_INV_SEL_GEN1,
16663cd0e4a3SFabien Parent 	.banks_num	= 1,
16673cd0e4a3SFabien Parent 	.banks_enable	= {true},
16683cd0e4a3SFabien Parent 	.iova_region	= single_domain,
16693cd0e4a3SFabien Parent 	.iova_region_nr	= ARRAY_SIZE(single_domain),
16703cd0e4a3SFabien Parent 	.larbid_remap	= {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
16713cd0e4a3SFabien Parent };
16723cd0e4a3SFabien Parent 
16730df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
1674cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1675068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
1676717ec15eSAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt6795-m4u", .data = &mt6795_data},
16773c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1678cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1679907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
1680e8d7ccaaSYong Wu 	{ .compatible = "mediatek,mt8186-iommu-mm",    .data = &mt8186_data_mm}, /* mm: m4u */
16819e3489e0SYong Wu 	{ .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data},
1682ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-infra", .data = &mt8195_data_infra},
1683ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-vdo",   .data = &mt8195_data_vdo},
1684ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-vpp",   .data = &mt8195_data_vpp},
16853cd0e4a3SFabien Parent 	{ .compatible = "mediatek,mt8365-m4u", .data = &mt8365_data},
16860df4fabeSYong Wu 	{}
16870df4fabeSYong Wu };
16880df4fabeSYong Wu 
16890df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
16900df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
1691d8149d39SUwe Kleine-König 	.remove_new = mtk_iommu_remove,
16920df4fabeSYong Wu 	.driver	= {
16930df4fabeSYong Wu 		.name = "mtk-iommu",
1694f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
16950df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
16960df4fabeSYong Wu 	}
16970df4fabeSYong Wu };
169818d8c74eSYong Wu module_platform_driver(mtk_iommu_driver);
16990df4fabeSYong Wu 
170018d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations");
170118d8c74eSYong Wu MODULE_LICENSE("GPL v2");
1702