xref: /linux/drivers/iommu/mtk_iommu.c (revision 0251d0107cfb0bb5ab2d3f97710487b9522db020)
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>
176a513de3SYong 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 
346a513de3SYong Wu #include <dt-bindings/memory/mtk-memory-port.h>
350df4fabeSYong Wu 
360df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
370df4fabeSYong Wu 
380df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
390df4fabeSYong Wu #define F_ALL_INVLD				0x2
400df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
410df4fabeSYong Wu 
420df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
430df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
440df4fabeSYong Wu 
45068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2			0x02c
46b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1			0x038
470df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
480df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
490df4fabeSYong Wu 
5075eed350SChao Hao #define REG_MMU_MISC_CTRL			0x048
514bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
524bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))
534bb2bf4cSChao Hao 
540df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
559a87005eSYong Wu #define F_MMU_DCM				BIT(8)
569a87005eSYong Wu 
5735c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5835c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
590df4fabeSYong Wu 
600df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
61acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
620df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
63acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
640df4fabeSYong Wu 
650df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6670ca608bSYong Wu 
6730e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6830e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
690df4fabeSYong Wu 
700df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
710df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
720df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
730df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
740df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
750df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
760df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
770df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
780df4fabeSYong Wu 
790df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
8015a01f4cSYong Wu 						/* mmu0 | mmu1 */
8115a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
8215a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
8315a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
8415a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8515a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8615a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8715a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
880df4fabeSYong Wu 
890df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
900df4fabeSYong Wu 
910df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
9215a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
9315a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
940df4fabeSYong Wu 
9515a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
96ef0f0986SYong Wu #define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
97ef0f0986SYong Wu #define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
98ef0f0986SYong Wu #define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
990df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
1000df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
1010df4fabeSYong Wu 
10215a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
10315a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
10415a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
10515a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
10615a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
10737276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10837276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
1099ec30c09SYong Wu #define F_MMU_INT_ID_COMM_ID_EXT(a)		(((a) >> 10) & 0x7)
1109ec30c09SYong Wu #define F_MMU_INT_ID_SUB_COMM_ID_EXT(a)		(((a) >> 7) & 0x7)
11115a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
11215a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
1130df4fabeSYong Wu 
114829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
11542d57fc5SYong Wu #define MTK_IOMMU_BANK_SZ			0x1000
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)
140301c3ca1SYunfei Wang #define PGTABLE_PA_35_EN		BIT(17)
14186580ec9SAngeloGioacchino Del Regno #define TF_PORT_TO_ADDR_MT8173		BIT(18)
1426b717796SChao Hao 
1438cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, mask)	\
1448cd1e619SYong Wu 				((((pdata)->flags) & (mask)) == (_x))
1458cd1e619SYong Wu 
1468cd1e619SYong Wu #define MTK_IOMMU_HAS_FLAG(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x, _x)
1478cd1e619SYong Wu #define MTK_IOMMU_IS_TYPE(pdata, _x)	MTK_IOMMU_HAS_FLAG_MASK(pdata, _x,\
1488cd1e619SYong Wu 							MTK_IOMMU_TYPE_MASK)
1496b717796SChao Hao 
150d2e9a110SYong Wu #define MTK_INVALID_LARBID		MTK_LARB_NR_MAX
151d2e9a110SYong Wu 
1529485a04aSYong Wu #define MTK_LARB_COM_MAX	8
1539485a04aSYong Wu #define MTK_LARB_SUBCOM_MAX	8
1549485a04aSYong Wu 
1559485a04aSYong Wu #define MTK_IOMMU_GROUP_MAX	8
15699ca0228SYong Wu #define MTK_IOMMU_BANK_MAX	5
1579485a04aSYong Wu 
1589485a04aSYong Wu enum mtk_iommu_plat {
1599485a04aSYong Wu 	M4U_MT2712,
1609485a04aSYong Wu 	M4U_MT6779,
161717ec15eSAngeloGioacchino Del Regno 	M4U_MT6795,
1629485a04aSYong Wu 	M4U_MT8167,
1639485a04aSYong Wu 	M4U_MT8173,
1649485a04aSYong Wu 	M4U_MT8183,
165e8d7ccaaSYong Wu 	M4U_MT8186,
1669485a04aSYong Wu 	M4U_MT8192,
1679485a04aSYong Wu 	M4U_MT8195,
1689485a04aSYong Wu };
1699485a04aSYong Wu 
1709485a04aSYong Wu struct mtk_iommu_iova_region {
1719485a04aSYong Wu 	dma_addr_t		iova_base;
1729485a04aSYong Wu 	unsigned long long	size;
1739485a04aSYong Wu };
1749485a04aSYong Wu 
1756a513de3SYong Wu struct mtk_iommu_suspend_reg {
1766a513de3SYong Wu 	u32			misc_ctrl;
1776a513de3SYong Wu 	u32			dcm_dis;
1786a513de3SYong Wu 	u32			ctrl_reg;
1796a513de3SYong Wu 	u32			vld_pa_rng;
1806a513de3SYong Wu 	u32			wr_len_ctrl;
181d7127de1SYong Wu 
182d7127de1SYong Wu 	u32			int_control[MTK_IOMMU_BANK_MAX];
183d7127de1SYong Wu 	u32			int_main_control[MTK_IOMMU_BANK_MAX];
184d7127de1SYong Wu 	u32			ivrp_paddr[MTK_IOMMU_BANK_MAX];
1856a513de3SYong Wu };
1866a513de3SYong Wu 
1879485a04aSYong Wu struct mtk_iommu_plat_data {
1889485a04aSYong Wu 	enum mtk_iommu_plat	m4u_plat;
1899485a04aSYong Wu 	u32			flags;
1909485a04aSYong Wu 	u32			inv_sel_reg;
1919485a04aSYong Wu 
1929485a04aSYong Wu 	char			*pericfg_comp_str;
1939485a04aSYong Wu 	struct list_head	*hw_list;
1949485a04aSYong Wu 	unsigned int		iova_region_nr;
1959485a04aSYong Wu 	const struct mtk_iommu_iova_region	*iova_region;
19699ca0228SYong Wu 
19799ca0228SYong Wu 	u8                  banks_num;
19899ca0228SYong Wu 	bool                banks_enable[MTK_IOMMU_BANK_MAX];
19957fb481fSYong Wu 	unsigned int        banks_portmsk[MTK_IOMMU_BANK_MAX];
2009485a04aSYong Wu 	unsigned char       larbid_remap[MTK_LARB_COM_MAX][MTK_LARB_SUBCOM_MAX];
2019485a04aSYong Wu };
2029485a04aSYong Wu 
20399ca0228SYong Wu struct mtk_iommu_bank_data {
2049485a04aSYong Wu 	void __iomem			*base;
2059485a04aSYong Wu 	int				irq;
20699ca0228SYong Wu 	u8				id;
20799ca0228SYong Wu 	struct device			*parent_dev;
20899ca0228SYong Wu 	struct mtk_iommu_data		*parent_data;
20999ca0228SYong Wu 	spinlock_t			tlb_lock; /* lock for tlb range flush */
21099ca0228SYong Wu 	struct mtk_iommu_domain		*m4u_dom; /* Each bank has a domain */
21199ca0228SYong Wu };
21299ca0228SYong Wu 
21399ca0228SYong Wu struct mtk_iommu_data {
2149485a04aSYong Wu 	struct device			*dev;
2159485a04aSYong Wu 	struct clk			*bclk;
2169485a04aSYong Wu 	phys_addr_t			protect_base; /* protect memory base */
2179485a04aSYong Wu 	struct mtk_iommu_suspend_reg	reg;
2189485a04aSYong Wu 	struct iommu_group		*m4u_group[MTK_IOMMU_GROUP_MAX];
2199485a04aSYong Wu 	bool                            enable_4GB;
2209485a04aSYong Wu 
2219485a04aSYong Wu 	struct iommu_device		iommu;
2229485a04aSYong Wu 	const struct mtk_iommu_plat_data *plat_data;
2239485a04aSYong Wu 	struct device			*smicomm_dev;
2249485a04aSYong Wu 
22599ca0228SYong Wu 	struct mtk_iommu_bank_data	*bank;
22699ca0228SYong Wu 
2279485a04aSYong Wu 	struct dma_iommu_mapping	*mapping; /* For mtk_iommu_v1.c */
2289485a04aSYong Wu 	struct regmap			*pericfg;
2299485a04aSYong Wu 
2309485a04aSYong Wu 	struct mutex			mutex; /* Protect m4u_group/m4u_dom above */
2319485a04aSYong Wu 
2329485a04aSYong Wu 	/*
2339485a04aSYong Wu 	 * In the sharing pgtable case, list data->list to the global list like m4ulist.
2349485a04aSYong Wu 	 * In the non-sharing pgtable case, list data->list to the itself hw_list_head.
2359485a04aSYong Wu 	 */
2369485a04aSYong Wu 	struct list_head		*hw_list;
2379485a04aSYong Wu 	struct list_head		hw_list_head;
2389485a04aSYong Wu 	struct list_head		list;
2399485a04aSYong Wu 	struct mtk_smi_larb_iommu	larb_imu[MTK_LARB_NR_MAX];
2409485a04aSYong Wu };
2419485a04aSYong Wu 
2420df4fabeSYong Wu struct mtk_iommu_domain {
2430df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
2440df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
2450df4fabeSYong Wu 
24699ca0228SYong Wu 	struct mtk_iommu_bank_data	*bank;
2470df4fabeSYong Wu 	struct iommu_domain		domain;
248ddf67a87SYong Wu 
249ddf67a87SYong Wu 	struct mutex			mutex; /* Protect "data" in this structure */
2500df4fabeSYong Wu };
2510df4fabeSYong Wu 
2529485a04aSYong Wu static int mtk_iommu_bind(struct device *dev)
2539485a04aSYong Wu {
2549485a04aSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
2559485a04aSYong Wu 
2569485a04aSYong Wu 	return component_bind_all(dev, &data->larb_imu);
2579485a04aSYong Wu }
2589485a04aSYong Wu 
2599485a04aSYong Wu static void mtk_iommu_unbind(struct device *dev)
2609485a04aSYong Wu {
2619485a04aSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
2629485a04aSYong Wu 
2639485a04aSYong Wu 	component_unbind_all(dev, &data->larb_imu);
2649485a04aSYong Wu }
2659485a04aSYong Wu 
266b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
2670df4fabeSYong Wu 
268e24453e1SYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data, unsigned int bankid);
2697f37a91dSYong Wu 
270bfed8731SYong Wu #define MTK_IOMMU_TLB_ADDR(iova) ({					\
271bfed8731SYong Wu 	dma_addr_t _addr = iova;					\
272bfed8731SYong Wu 	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
273bfed8731SYong Wu })
274bfed8731SYong Wu 
27576ce6546SYong Wu /*
27676ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
27776ce6546SYong Wu  *
27876ce6546SYong Wu  * CPU Physical address:
27976ce6546SYong Wu  * ====================
28076ce6546SYong Wu  *
28176ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
28276ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
28376ce6546SYong Wu  * +--I/O--+------------Memory-------------+
28476ce6546SYong Wu  *
28576ce6546SYong Wu  * IOMMU output physical address:
28676ce6546SYong Wu  *  =============================
28776ce6546SYong Wu  *
28876ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
28976ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
29076ce6546SYong Wu  *                                 +------------Memory-------------+
29176ce6546SYong Wu  *
29276ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
29376ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
29476ce6546SYong Wu  * 'E', the CPU physical address keep as is.
29576ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
29676ce6546SYong Wu  */
297b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
29876ce6546SYong Wu 
2997c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
3007c3a2ec0SYong Wu 
3019e3a2a64SYong Wu #define for_each_m4u(data, head)  list_for_each_entry(data, head, list)
3027c3a2ec0SYong Wu 
303585e58f4SYong Wu static const struct mtk_iommu_iova_region single_domain[] = {
304585e58f4SYong Wu 	{.iova_base = 0,		.size = SZ_4G},
305585e58f4SYong Wu };
306585e58f4SYong Wu 
3079e3489e0SYong Wu static const struct mtk_iommu_iova_region mt8192_multi_dom[] = {
308129a3b88SYong Wu 	{ .iova_base = 0x0,		.size = SZ_4G},		/* 0 ~ 4G */
3099e3489e0SYong Wu 	#if IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)
310129a3b88SYong Wu 	{ .iova_base = SZ_4G,		.size = SZ_4G},		/* 4G ~ 8G */
311129a3b88SYong Wu 	{ .iova_base = SZ_4G * 2,	.size = SZ_4G},		/* 8G ~ 12G */
312129a3b88SYong Wu 	{ .iova_base = SZ_4G * 3,	.size = SZ_4G},		/* 12G ~ 16G */
313129a3b88SYong Wu 
3149e3489e0SYong Wu 	{ .iova_base = 0x240000000ULL,	.size = 0x4000000},	/* CCU0 */
3159e3489e0SYong Wu 	{ .iova_base = 0x244000000ULL,	.size = 0x4000000},	/* CCU1 */
3169e3489e0SYong Wu 	#endif
3179e3489e0SYong Wu };
3189e3489e0SYong Wu 
3199e3a2a64SYong Wu /* If 2 M4U share a domain(use the same hwlist), Put the corresponding info in first data.*/
3209e3a2a64SYong Wu static struct mtk_iommu_data *mtk_iommu_get_frst_data(struct list_head *hwlist)
3217c3a2ec0SYong Wu {
3229e3a2a64SYong Wu 	return list_first_entry(hwlist, struct mtk_iommu_data, list);
3237c3a2ec0SYong Wu }
3247c3a2ec0SYong Wu 
3250df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
3260df4fabeSYong Wu {
3270df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
3280df4fabeSYong Wu }
3290df4fabeSYong Wu 
3300954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
3310df4fabeSYong Wu {
33299ca0228SYong Wu 	/* Tlb flush all always is in bank0. */
33399ca0228SYong Wu 	struct mtk_iommu_bank_data *bank = &data->bank[0];
33499ca0228SYong Wu 	void __iomem *base = bank->base;
33515672b6dSYong Wu 	unsigned long flags;
336c0b57581SYong Wu 
33799ca0228SYong Wu 	spin_lock_irqsave(&bank->tlb_lock, flags);
338887cf6a7SYong Wu 	writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, base + data->plat_data->inv_sel_reg);
339887cf6a7SYong Wu 	writel_relaxed(F_ALL_INVLD, base + REG_MMU_INVALIDATE);
3400df4fabeSYong Wu 	wmb(); /* Make sure the tlb flush all done */
34199ca0228SYong Wu 	spin_unlock_irqrestore(&bank->tlb_lock, flags);
3427c3a2ec0SYong Wu }
3430df4fabeSYong Wu 
3441f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
34599ca0228SYong Wu 					   struct mtk_iommu_bank_data *bank)
3460df4fabeSYong Wu {
34799ca0228SYong Wu 	struct list_head *head = bank->parent_data->hw_list;
34899ca0228SYong Wu 	struct mtk_iommu_bank_data *curbank;
34999ca0228SYong Wu 	struct mtk_iommu_data *data;
3506077c7e5SYong Wu 	bool check_pm_status;
3511f4fd624SYong Wu 	unsigned long flags;
352887cf6a7SYong Wu 	void __iomem *base;
3531f4fd624SYong Wu 	int ret;
3541f4fd624SYong Wu 	u32 tmp;
3550df4fabeSYong Wu 
3569e3a2a64SYong Wu 	for_each_m4u(data, head) {
3576077c7e5SYong Wu 		/*
3586077c7e5SYong Wu 		 * To avoid resume the iommu device frequently when the iommu device
3596077c7e5SYong Wu 		 * is not active, it doesn't always call pm_runtime_get here, then tlb
3606077c7e5SYong Wu 		 * flush depends on the tlb flush all in the runtime resume.
3616077c7e5SYong Wu 		 *
3626077c7e5SYong Wu 		 * There are 2 special cases:
3636077c7e5SYong Wu 		 *
3646077c7e5SYong Wu 		 * Case1: The iommu dev doesn't have power domain but has bclk. This case
3656077c7e5SYong Wu 		 * should also avoid the tlb flush while the dev is not active to mute
3666077c7e5SYong Wu 		 * the tlb timeout log. like mt8173.
3676077c7e5SYong Wu 		 *
3686077c7e5SYong Wu 		 * Case2: The power/clock of infra iommu is always on, and it doesn't
3696077c7e5SYong Wu 		 * have the device link with the master devices. This case should avoid
3706077c7e5SYong Wu 		 * the PM status check.
3716077c7e5SYong Wu 		 */
3726077c7e5SYong Wu 		check_pm_status = !MTK_IOMMU_HAS_FLAG(data->plat_data, PM_CLK_AO);
3736077c7e5SYong Wu 
3746077c7e5SYong Wu 		if (check_pm_status) {
375c0b57581SYong Wu 			if (pm_runtime_get_if_in_use(data->dev) <= 0)
376c0b57581SYong Wu 				continue;
3776077c7e5SYong Wu 		}
378c0b57581SYong Wu 
37999ca0228SYong Wu 		curbank = &data->bank[bank->id];
38099ca0228SYong Wu 		base = curbank->base;
381887cf6a7SYong Wu 
38299ca0228SYong Wu 		spin_lock_irqsave(&curbank->tlb_lock, flags);
3837c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
384887cf6a7SYong Wu 			       base + data->plat_data->inv_sel_reg);
3850df4fabeSYong Wu 
386887cf6a7SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova), base + REG_MMU_INVLD_START_A);
387bfed8731SYong Wu 		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
388887cf6a7SYong Wu 			       base + REG_MMU_INVLD_END_A);
389887cf6a7SYong Wu 		writel_relaxed(F_MMU_INV_RANGE, base + REG_MMU_INVALIDATE);
3900df4fabeSYong Wu 
3911f4fd624SYong Wu 		/* tlb sync */
392887cf6a7SYong Wu 		ret = readl_poll_timeout_atomic(base + REG_MMU_CPE_DONE,
393c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
39415672b6dSYong Wu 
39515672b6dSYong Wu 		/* Clear the CPE status */
396887cf6a7SYong Wu 		writel_relaxed(0, base + REG_MMU_CPE_DONE);
39799ca0228SYong Wu 		spin_unlock_irqrestore(&curbank->tlb_lock, flags);
39815672b6dSYong Wu 
3990df4fabeSYong Wu 		if (ret) {
4000df4fabeSYong Wu 			dev_warn(data->dev,
4010df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
4020954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
4030df4fabeSYong Wu 		}
404c0b57581SYong Wu 
4056077c7e5SYong Wu 		if (check_pm_status)
406c0b57581SYong Wu 			pm_runtime_put(data->dev);
4070df4fabeSYong Wu 	}
4087c3a2ec0SYong Wu }
4090df4fabeSYong Wu 
4100df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
4110df4fabeSYong Wu {
41299ca0228SYong Wu 	struct mtk_iommu_bank_data *bank = dev_id;
41399ca0228SYong Wu 	struct mtk_iommu_data *data = bank->parent_data;
41499ca0228SYong Wu 	struct mtk_iommu_domain *dom = bank->m4u_dom;
415d2e9a110SYong Wu 	unsigned int fault_larb = MTK_INVALID_LARBID, fault_port = 0, sub_comm = 0;
416ef0f0986SYong Wu 	u32 int_state, regval, va34_32, pa34_32;
417887cf6a7SYong Wu 	const struct mtk_iommu_plat_data *plat_data = data->plat_data;
41899ca0228SYong Wu 	void __iomem *base = bank->base;
419ef0f0986SYong Wu 	u64 fault_iova, fault_pa;
4200df4fabeSYong Wu 	bool layer, write;
4210df4fabeSYong Wu 
4220df4fabeSYong Wu 	/* Read error info from registers */
423887cf6a7SYong Wu 	int_state = readl_relaxed(base + REG_MMU_FAULT_ST1);
42415a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
425887cf6a7SYong Wu 		regval = readl_relaxed(base + REG_MMU0_INT_ID);
426887cf6a7SYong Wu 		fault_iova = readl_relaxed(base + REG_MMU0_FAULT_VA);
427887cf6a7SYong Wu 		fault_pa = readl_relaxed(base + REG_MMU0_INVLD_PA);
42815a01f4cSYong Wu 	} else {
429887cf6a7SYong Wu 		regval = readl_relaxed(base + REG_MMU1_INT_ID);
430887cf6a7SYong Wu 		fault_iova = readl_relaxed(base + REG_MMU1_FAULT_VA);
431887cf6a7SYong Wu 		fault_pa = readl_relaxed(base + REG_MMU1_INVLD_PA);
43215a01f4cSYong Wu 	}
4330df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
4340df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
435887cf6a7SYong Wu 	if (MTK_IOMMU_HAS_FLAG(plat_data, IOVA_34_EN)) {
436ef0f0986SYong Wu 		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
437ef0f0986SYong Wu 		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
438ef0f0986SYong Wu 		fault_iova |= (u64)va34_32 << 32;
439ef0f0986SYong Wu 	}
44082e51771SYong Wu 	pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
44182e51771SYong Wu 	fault_pa |= (u64)pa34_32 << 32;
442ef0f0986SYong Wu 
443887cf6a7SYong Wu 	if (MTK_IOMMU_IS_TYPE(plat_data, MTK_IOMMU_TYPE_MM)) {
44415a01f4cSYong Wu 		fault_port = F_MMU_INT_ID_PORT_ID(regval);
445887cf6a7SYong Wu 		if (MTK_IOMMU_HAS_FLAG(plat_data, HAS_SUB_COMM_2BITS)) {
44637276e00SChao Hao 			fault_larb = F_MMU_INT_ID_COMM_ID(regval);
44737276e00SChao Hao 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
448887cf6a7SYong Wu 		} else if (MTK_IOMMU_HAS_FLAG(plat_data, HAS_SUB_COMM_3BITS)) {
4499ec30c09SYong Wu 			fault_larb = F_MMU_INT_ID_COMM_ID_EXT(regval);
4509ec30c09SYong Wu 			sub_comm = F_MMU_INT_ID_SUB_COMM_ID_EXT(regval);
45137276e00SChao Hao 		} else {
45237276e00SChao Hao 			fault_larb = F_MMU_INT_ID_LARB_ID(regval);
45337276e00SChao Hao 		}
45437276e00SChao Hao 		fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
455d2e9a110SYong Wu 	}
456b3e5eee7SYong Wu 
45799ca0228SYong Wu 	if (report_iommu_fault(&dom->domain, bank->parent_dev, fault_iova,
4580df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
4590df4fabeSYong Wu 		dev_err_ratelimited(
46099ca0228SYong Wu 			bank->parent_dev,
461f9b8c9b2SYong Wu 			"fault type=0x%x iova=0x%llx pa=0x%llx master=0x%x(larb=%d port=%d) layer=%d %s\n",
462f9b8c9b2SYong Wu 			int_state, fault_iova, fault_pa, regval, fault_larb, fault_port,
4630df4fabeSYong Wu 			layer, write ? "write" : "read");
4640df4fabeSYong Wu 	}
4650df4fabeSYong Wu 
4660df4fabeSYong Wu 	/* Interrupt clear */
467887cf6a7SYong Wu 	regval = readl_relaxed(base + REG_MMU_INT_CONTROL0);
4680df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
469887cf6a7SYong Wu 	writel_relaxed(regval, base + REG_MMU_INT_CONTROL0);
4700df4fabeSYong Wu 
4710df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
4720df4fabeSYong Wu 
4730df4fabeSYong Wu 	return IRQ_HANDLED;
4740df4fabeSYong Wu }
4750df4fabeSYong Wu 
47657fb481fSYong Wu static unsigned int mtk_iommu_get_bank_id(struct device *dev,
47757fb481fSYong Wu 					  const struct mtk_iommu_plat_data *plat_data)
47857fb481fSYong Wu {
47957fb481fSYong Wu 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
48057fb481fSYong Wu 	unsigned int i, portmsk = 0, bankid = 0;
48157fb481fSYong Wu 
48257fb481fSYong Wu 	if (plat_data->banks_num == 1)
48357fb481fSYong Wu 		return bankid;
48457fb481fSYong Wu 
48557fb481fSYong Wu 	for (i = 0; i < fwspec->num_ids; i++)
48657fb481fSYong Wu 		portmsk |= BIT(MTK_M4U_TO_PORT(fwspec->ids[i]));
48757fb481fSYong Wu 
48857fb481fSYong Wu 	for (i = 0; i < plat_data->banks_num && i < MTK_IOMMU_BANK_MAX; i++) {
48957fb481fSYong Wu 		if (!plat_data->banks_enable[i])
49057fb481fSYong Wu 			continue;
49157fb481fSYong Wu 
49257fb481fSYong Wu 		if (portmsk & plat_data->banks_portmsk[i]) {
49357fb481fSYong Wu 			bankid = i;
49457fb481fSYong Wu 			break;
49557fb481fSYong Wu 		}
49657fb481fSYong Wu 	}
49757fb481fSYong Wu 	return bankid; /* default is 0 */
49857fb481fSYong Wu }
49957fb481fSYong Wu 
500d72e0ff5SYong Wu static int mtk_iommu_get_iova_region_id(struct device *dev,
501803cf9e5SYong Wu 					const struct mtk_iommu_plat_data *plat_data)
502803cf9e5SYong Wu {
503803cf9e5SYong Wu 	const struct mtk_iommu_iova_region *rgn = plat_data->iova_region;
504803cf9e5SYong Wu 	const struct bus_dma_region *dma_rgn = dev->dma_range_map;
505803cf9e5SYong Wu 	int i, candidate = -1;
506803cf9e5SYong Wu 	dma_addr_t dma_end;
507803cf9e5SYong Wu 
508803cf9e5SYong Wu 	if (!dma_rgn || plat_data->iova_region_nr == 1)
509803cf9e5SYong Wu 		return 0;
510803cf9e5SYong Wu 
511803cf9e5SYong Wu 	dma_end = dma_rgn->dma_start + dma_rgn->size - 1;
512803cf9e5SYong Wu 	for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) {
513803cf9e5SYong Wu 		/* Best fit. */
514803cf9e5SYong Wu 		if (dma_rgn->dma_start == rgn->iova_base &&
515803cf9e5SYong Wu 		    dma_end == rgn->iova_base + rgn->size - 1)
516803cf9e5SYong Wu 			return i;
517803cf9e5SYong Wu 		/* ok if it is inside this region. */
518803cf9e5SYong Wu 		if (dma_rgn->dma_start >= rgn->iova_base &&
519803cf9e5SYong Wu 		    dma_end < rgn->iova_base + rgn->size)
520803cf9e5SYong Wu 			candidate = i;
521803cf9e5SYong Wu 	}
522803cf9e5SYong Wu 
523803cf9e5SYong Wu 	if (candidate >= 0)
524803cf9e5SYong Wu 		return candidate;
525803cf9e5SYong Wu 	dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n",
526803cf9e5SYong Wu 		&dma_rgn->dma_start, dma_rgn->size);
527803cf9e5SYong Wu 	return -EINVAL;
528803cf9e5SYong Wu }
529803cf9e5SYong Wu 
530f9b8c9b2SYong Wu static int mtk_iommu_config(struct mtk_iommu_data *data, struct device *dev,
531d72e0ff5SYong Wu 			    bool enable, unsigned int regionid)
5320df4fabeSYong Wu {
5330df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
5340df4fabeSYong Wu 	unsigned int                 larbid, portid;
535a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
5368d2c749eSYong Wu 	const struct mtk_iommu_iova_region *region;
537f9b8c9b2SYong Wu 	u32 peri_mmuen, peri_mmuen_msk;
538f9b8c9b2SYong Wu 	int i, ret = 0;
5390df4fabeSYong Wu 
54058f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
54158f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
54258f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
5438d2c749eSYong Wu 
544d2e9a110SYong Wu 		if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
5451ee9feb2SYong Wu 			larb_mmu = &data->larb_imu[larbid];
5460df4fabeSYong Wu 
547d72e0ff5SYong Wu 			region = data->plat_data->iova_region + regionid;
5488d2c749eSYong Wu 			larb_mmu->bank[portid] = upper_32_bits(region->iova_base);
5498d2c749eSYong Wu 
550d72e0ff5SYong Wu 			dev_dbg(dev, "%s iommu for larb(%s) port %d region %d rgn-bank %d.\n",
5518d2c749eSYong Wu 				enable ? "enable" : "disable", dev_name(larb_mmu->dev),
552d72e0ff5SYong Wu 				portid, regionid, larb_mmu->bank[portid]);
5530df4fabeSYong Wu 
5540df4fabeSYong Wu 			if (enable)
5550df4fabeSYong Wu 				larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
5560df4fabeSYong Wu 			else
5570df4fabeSYong Wu 				larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
558f9b8c9b2SYong Wu 		} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {
559f9b8c9b2SYong Wu 			peri_mmuen_msk = BIT(portid);
560e7629070SYong Wu 			/* PCI dev has only one output id, enable the next writing bit for PCIe */
561e7629070SYong Wu 			if (dev_is_pci(dev))
562e7629070SYong Wu 				peri_mmuen_msk |= BIT(portid + 1);
563f9b8c9b2SYong Wu 
564e7629070SYong Wu 			peri_mmuen = enable ? peri_mmuen_msk : 0;
565f9b8c9b2SYong Wu 			ret = regmap_update_bits(data->pericfg, PERICFG_IOMMU_1,
566f9b8c9b2SYong Wu 						 peri_mmuen_msk, peri_mmuen);
567f9b8c9b2SYong Wu 			if (ret)
568f9b8c9b2SYong Wu 				dev_err(dev, "%s iommu(%s) inframaster 0x%x fail(%d).\n",
569f9b8c9b2SYong Wu 					enable ? "enable" : "disable",
570f9b8c9b2SYong Wu 					dev_name(data->dev), peri_mmuen_msk, ret);
5710df4fabeSYong Wu 		}
5720df4fabeSYong Wu 	}
573f9b8c9b2SYong Wu 	return ret;
574d2e9a110SYong Wu }
5750df4fabeSYong Wu 
5764f956c97SYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
577c3045f39SYong Wu 				     struct mtk_iommu_data *data,
578d72e0ff5SYong Wu 				     unsigned int region_id)
5790df4fabeSYong Wu {
580c3045f39SYong Wu 	const struct mtk_iommu_iova_region *region;
58199ca0228SYong Wu 	struct mtk_iommu_domain	*m4u_dom;
582c3045f39SYong Wu 
58399ca0228SYong Wu 	/* Always use bank0 in sharing pgtable case */
58499ca0228SYong Wu 	m4u_dom = data->bank[0].m4u_dom;
58599ca0228SYong Wu 	if (m4u_dom) {
58699ca0228SYong Wu 		dom->iop = m4u_dom->iop;
58799ca0228SYong Wu 		dom->cfg = m4u_dom->cfg;
58899ca0228SYong Wu 		dom->domain.pgsize_bitmap = m4u_dom->cfg.pgsize_bitmap;
589c3045f39SYong Wu 		goto update_iova_region;
590c3045f39SYong Wu 	}
591c3045f39SYong Wu 
5920df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
5930df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
5940df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
595b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
5960df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
5972f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
5980df4fabeSYong Wu 		.iommu_dev = data->dev,
5990df4fabeSYong Wu 	};
6000df4fabeSYong Wu 
601301c3ca1SYunfei Wang 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, PGTABLE_PA_35_EN))
602301c3ca1SYunfei Wang 		dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_TTBR_EXT;
603301c3ca1SYunfei Wang 
6049bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
6059bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
6069bdfe4c1SYong Wu 	else
6079bdfe4c1SYong Wu 		dom->cfg.oas = 35;
6089bdfe4c1SYong Wu 
6090df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
6100df4fabeSYong Wu 	if (!dom->iop) {
6110df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
6120df4fabeSYong Wu 		return -EINVAL;
6130df4fabeSYong Wu 	}
6140df4fabeSYong Wu 
6150df4fabeSYong Wu 	/* Update our support page sizes bitmap */
616d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
617b7875eb9SYong Wu 
618c3045f39SYong Wu update_iova_region:
619c3045f39SYong Wu 	/* Update the iova region for this domain */
620d72e0ff5SYong Wu 	region = data->plat_data->iova_region + region_id;
621c3045f39SYong Wu 	dom->domain.geometry.aperture_start = region->iova_base;
622c3045f39SYong Wu 	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
623b7875eb9SYong Wu 	dom->domain.geometry.force_aperture = true;
6240df4fabeSYong Wu 	return 0;
6250df4fabeSYong Wu }
6260df4fabeSYong Wu 
6270df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
6280df4fabeSYong Wu {
6290df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
6300df4fabeSYong Wu 
63132e1cccfSYong Wu 	if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED)
6320df4fabeSYong Wu 		return NULL;
6330df4fabeSYong Wu 
6340df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
6350df4fabeSYong Wu 	if (!dom)
6360df4fabeSYong Wu 		return NULL;
637ddf67a87SYong Wu 	mutex_init(&dom->mutex);
6380df4fabeSYong Wu 
6394f956c97SYong Wu 	return &dom->domain;
6404f956c97SYong Wu }
6414f956c97SYong Wu 
6420df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
6430df4fabeSYong Wu {
6440df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
6450df4fabeSYong Wu }
6460df4fabeSYong Wu 
6470df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
6480df4fabeSYong Wu 				   struct device *dev)
6490df4fabeSYong Wu {
650645b87c1SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev), *frstdata;
6510df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
6529e3a2a64SYong Wu 	struct list_head *hw_list = data->hw_list;
653c0b57581SYong Wu 	struct device *m4udev = data->dev;
65499ca0228SYong Wu 	struct mtk_iommu_bank_data *bank;
65557fb481fSYong Wu 	unsigned int bankid;
656d72e0ff5SYong Wu 	int ret, region_id;
6570df4fabeSYong Wu 
658d72e0ff5SYong Wu 	region_id = mtk_iommu_get_iova_region_id(dev, data->plat_data);
659d72e0ff5SYong Wu 	if (region_id < 0)
660d72e0ff5SYong Wu 		return region_id;
661803cf9e5SYong Wu 
66257fb481fSYong Wu 	bankid = mtk_iommu_get_bank_id(dev, data->plat_data);
663ddf67a87SYong Wu 	mutex_lock(&dom->mutex);
66499ca0228SYong Wu 	if (!dom->bank) {
665645b87c1SYong Wu 		/* Data is in the frstdata in sharing pgtable case. */
6669e3a2a64SYong Wu 		frstdata = mtk_iommu_get_frst_data(hw_list);
667645b87c1SYong Wu 
668d72e0ff5SYong Wu 		ret = mtk_iommu_domain_finalise(dom, frstdata, region_id);
669ddf67a87SYong Wu 		if (ret) {
670ddf67a87SYong Wu 			mutex_unlock(&dom->mutex);
6714f956c97SYong Wu 			return -ENODEV;
672ddf67a87SYong Wu 		}
67399ca0228SYong Wu 		dom->bank = &data->bank[bankid];
6744f956c97SYong Wu 	}
675ddf67a87SYong Wu 	mutex_unlock(&dom->mutex);
6764f956c97SYong Wu 
6770e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
67899ca0228SYong Wu 	bank = &data->bank[bankid];
679e24453e1SYong Wu 	if (!bank->m4u_dom) { /* Initialize the M4U HW for each a BANK */
680c0b57581SYong Wu 		ret = pm_runtime_resume_and_get(m4udev);
681e24453e1SYong Wu 		if (ret < 0) {
682e24453e1SYong Wu 			dev_err(m4udev, "pm get fail(%d) in attach.\n", ret);
6830e5a3f2eSYong Wu 			goto err_unlock;
684e24453e1SYong Wu 		}
685c0b57581SYong Wu 
686e24453e1SYong Wu 		ret = mtk_iommu_hw_init(data, bankid);
687c0b57581SYong Wu 		if (ret) {
688c0b57581SYong Wu 			pm_runtime_put(m4udev);
6890e5a3f2eSYong Wu 			goto err_unlock;
690c0b57581SYong Wu 		}
69199ca0228SYong Wu 		bank->m4u_dom = dom;
692301c3ca1SYunfei Wang 		writel(dom->cfg.arm_v7s_cfg.ttbr, bank->base + REG_MMU_PT_BASE_ADDR);
693c0b57581SYong Wu 
694c0b57581SYong Wu 		pm_runtime_put(m4udev);
6950df4fabeSYong Wu 	}
6960e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
6970df4fabeSYong Wu 
698d72e0ff5SYong Wu 	return mtk_iommu_config(data, dev, true, region_id);
6990e5a3f2eSYong Wu 
7000e5a3f2eSYong Wu err_unlock:
7010e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
7020e5a3f2eSYong Wu 	return ret;
7030df4fabeSYong Wu }
7040df4fabeSYong Wu 
7050df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
7060df4fabeSYong Wu 				    struct device *dev)
7070df4fabeSYong Wu {
7083524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
7090df4fabeSYong Wu 
7108d2c749eSYong Wu 	mtk_iommu_config(data, dev, false, 0);
7110df4fabeSYong Wu }
7120df4fabeSYong Wu 
7130df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
714781ca2deSTom Murphy 			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
7150df4fabeSYong Wu {
7160df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
7170df4fabeSYong Wu 
718b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
71999ca0228SYong Wu 	if (dom->bank->parent_data->enable_4GB)
720b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
721b4dad40eSYong Wu 
72260829b4dSYong Wu 	/* Synchronize with the tlb_lock */
723f34ce7a7SBaolin Wang 	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
7240df4fabeSYong Wu }
7250df4fabeSYong Wu 
7260df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
72756f8af5eSWill Deacon 			      unsigned long iova, size_t size,
72856f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
7290df4fabeSYong Wu {
7300df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
7310df4fabeSYong Wu 
7323136895cSRobin Murphy 	iommu_iotlb_gather_add_range(gather, iova, size);
73360829b4dSYong Wu 	return dom->iop->unmap(dom->iop, iova, size, gather);
7340df4fabeSYong Wu }
7350df4fabeSYong Wu 
73656f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
73756f8af5eSWill Deacon {
73808500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
73908500c43SYong Wu 
74099ca0228SYong Wu 	mtk_iommu_tlb_flush_all(dom->bank->parent_data);
74156f8af5eSWill Deacon }
74256f8af5eSWill Deacon 
74356f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
74456f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
7454d689b61SRobin Murphy {
74608500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
747862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
748da3cc91bSYong Wu 
74999ca0228SYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, dom->bank);
7504d689b61SRobin Murphy }
7514d689b61SRobin Murphy 
75220143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
75320143451SYong Wu 			       size_t size)
75420143451SYong Wu {
75508500c43SYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
75620143451SYong Wu 
75799ca0228SYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, dom->bank);
75820143451SYong Wu }
75920143451SYong Wu 
7600df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
7610df4fabeSYong Wu 					  dma_addr_t iova)
7620df4fabeSYong Wu {
7630df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
7640df4fabeSYong Wu 	phys_addr_t pa;
7650df4fabeSYong Wu 
7660df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
767f13efafcSArnd Bergmann 	if (IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) &&
76899ca0228SYong Wu 	    dom->bank->parent_data->enable_4GB &&
769f13efafcSArnd Bergmann 	    pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
770b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
77130e2fccfSYong Wu 
7720df4fabeSYong Wu 	return pa;
7730df4fabeSYong Wu }
7740df4fabeSYong Wu 
77580e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
7760df4fabeSYong Wu {
777a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
778b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
779635319a4SYong Wu 	struct device_link *link;
780635319a4SYong Wu 	struct device *larbdev;
781635319a4SYong Wu 	unsigned int larbid, larbidx, i;
7820df4fabeSYong Wu 
783a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
78480e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
7850df4fabeSYong Wu 
7863524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
787b16c0170SJoerg Roedel 
788d2e9a110SYong Wu 	if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
789d2e9a110SYong Wu 		return &data->iommu;
790d2e9a110SYong Wu 
791635319a4SYong Wu 	/*
792635319a4SYong Wu 	 * Link the consumer device with the smi-larb device(supplier).
793635319a4SYong Wu 	 * The device that connects with each a larb is a independent HW.
794635319a4SYong Wu 	 * All the ports in each a device should be in the same larbs.
795635319a4SYong Wu 	 */
796635319a4SYong Wu 	larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
797de78657eSMiles Chen 	if (larbid >= MTK_LARB_NR_MAX)
798de78657eSMiles Chen 		return ERR_PTR(-EINVAL);
799de78657eSMiles Chen 
800635319a4SYong Wu 	for (i = 1; i < fwspec->num_ids; i++) {
801635319a4SYong Wu 		larbidx = MTK_M4U_TO_LARB(fwspec->ids[i]);
802635319a4SYong Wu 		if (larbid != larbidx) {
803635319a4SYong Wu 			dev_err(dev, "Can only use one larb. Fail@larb%d-%d.\n",
804635319a4SYong Wu 				larbid, larbidx);
805635319a4SYong Wu 			return ERR_PTR(-EINVAL);
806635319a4SYong Wu 		}
807635319a4SYong Wu 	}
808635319a4SYong Wu 	larbdev = data->larb_imu[larbid].dev;
809de78657eSMiles Chen 	if (!larbdev)
810de78657eSMiles Chen 		return ERR_PTR(-EINVAL);
811de78657eSMiles Chen 
812635319a4SYong Wu 	link = device_link_add(dev, larbdev,
813635319a4SYong Wu 			       DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
814635319a4SYong Wu 	if (!link)
815635319a4SYong Wu 		dev_err(dev, "Unable to link %s\n", dev_name(larbdev));
81680e4592aSJoerg Roedel 	return &data->iommu;
8170df4fabeSYong Wu }
8180df4fabeSYong Wu 
81980e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
8200df4fabeSYong Wu {
821a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
822635319a4SYong Wu 	struct mtk_iommu_data *data;
823635319a4SYong Wu 	struct device *larbdev;
824635319a4SYong Wu 	unsigned int larbid;
825b16c0170SJoerg Roedel 
826635319a4SYong Wu 	data = dev_iommu_priv_get(dev);
827d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
828635319a4SYong Wu 		larbid = MTK_M4U_TO_LARB(fwspec->ids[0]);
829635319a4SYong Wu 		larbdev = data->larb_imu[larbid].dev;
830635319a4SYong Wu 		device_link_remove(dev, larbdev);
831d2e9a110SYong Wu 	}
8320df4fabeSYong Wu }
8330df4fabeSYong Wu 
83457fb481fSYong Wu static int mtk_iommu_get_group_id(struct device *dev, const struct mtk_iommu_plat_data *plat_data)
83557fb481fSYong Wu {
83657fb481fSYong Wu 	unsigned int bankid;
83757fb481fSYong Wu 
83857fb481fSYong Wu 	/*
83957fb481fSYong Wu 	 * If the bank function is enabled, each bank is a iommu group/domain.
84057fb481fSYong Wu 	 * Otherwise, each iova region is a iommu group/domain.
84157fb481fSYong Wu 	 */
84257fb481fSYong Wu 	bankid = mtk_iommu_get_bank_id(dev, plat_data);
84357fb481fSYong Wu 	if (bankid)
84457fb481fSYong Wu 		return bankid;
84557fb481fSYong Wu 
84657fb481fSYong Wu 	return mtk_iommu_get_iova_region_id(dev, plat_data);
84757fb481fSYong Wu }
84857fb481fSYong Wu 
8490df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
8500df4fabeSYong Wu {
8519e3a2a64SYong Wu 	struct mtk_iommu_data *c_data = dev_iommu_priv_get(dev), *data;
8529e3a2a64SYong Wu 	struct list_head *hw_list = c_data->hw_list;
853c3045f39SYong Wu 	struct iommu_group *group;
85457fb481fSYong Wu 	int groupid;
8550df4fabeSYong Wu 
8569e3a2a64SYong Wu 	data = mtk_iommu_get_frst_data(hw_list);
85758f0d1d5SRobin Murphy 	if (!data)
8580df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
8590df4fabeSYong Wu 
86057fb481fSYong Wu 	groupid = mtk_iommu_get_group_id(dev, data->plat_data);
86157fb481fSYong Wu 	if (groupid < 0)
86257fb481fSYong Wu 		return ERR_PTR(groupid);
863803cf9e5SYong Wu 
8640e5a3f2eSYong Wu 	mutex_lock(&data->mutex);
86557fb481fSYong Wu 	group = data->m4u_group[groupid];
866c3045f39SYong Wu 	if (!group) {
867c3045f39SYong Wu 		group = iommu_group_alloc();
868c3045f39SYong Wu 		if (!IS_ERR(group))
86957fb481fSYong Wu 			data->m4u_group[groupid] = group;
8703a8d40b6SRobin Murphy 	} else {
871c3045f39SYong Wu 		iommu_group_ref_get(group);
8720df4fabeSYong Wu 	}
8730e5a3f2eSYong Wu 	mutex_unlock(&data->mutex);
874c3045f39SYong Wu 	return group;
8750df4fabeSYong Wu }
8760df4fabeSYong Wu 
8770df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
8780df4fabeSYong Wu {
8790df4fabeSYong Wu 	struct platform_device *m4updev;
8800df4fabeSYong Wu 
8810df4fabeSYong Wu 	if (args->args_count != 1) {
8820df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
8830df4fabeSYong Wu 			args->args_count);
8840df4fabeSYong Wu 		return -EINVAL;
8850df4fabeSYong Wu 	}
8860df4fabeSYong Wu 
8873524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
8880df4fabeSYong Wu 		/* Get the m4u device */
8890df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
8900df4fabeSYong Wu 		if (WARN_ON(!m4updev))
8910df4fabeSYong Wu 			return -EINVAL;
8920df4fabeSYong Wu 
8933524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
8940df4fabeSYong Wu 	}
8950df4fabeSYong Wu 
89658f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
8970df4fabeSYong Wu }
8980df4fabeSYong Wu 
899ab1d5281SYong Wu static void mtk_iommu_get_resv_regions(struct device *dev,
900ab1d5281SYong Wu 				       struct list_head *head)
901ab1d5281SYong Wu {
902ab1d5281SYong Wu 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
903d72e0ff5SYong Wu 	unsigned int regionid = mtk_iommu_get_iova_region_id(dev, data->plat_data), i;
904ab1d5281SYong Wu 	const struct mtk_iommu_iova_region *resv, *curdom;
905ab1d5281SYong Wu 	struct iommu_resv_region *region;
906ab1d5281SYong Wu 	int prot = IOMMU_WRITE | IOMMU_READ;
907ab1d5281SYong Wu 
908d72e0ff5SYong Wu 	if ((int)regionid < 0)
909ab1d5281SYong Wu 		return;
910d72e0ff5SYong Wu 	curdom = data->plat_data->iova_region + regionid;
911ab1d5281SYong Wu 	for (i = 0; i < data->plat_data->iova_region_nr; i++) {
912ab1d5281SYong Wu 		resv = data->plat_data->iova_region + i;
913ab1d5281SYong Wu 
914ab1d5281SYong Wu 		/* Only reserve when the region is inside the current domain */
915ab1d5281SYong Wu 		if (resv->iova_base <= curdom->iova_base ||
916ab1d5281SYong Wu 		    resv->iova_base + resv->size >= curdom->iova_base + curdom->size)
917ab1d5281SYong Wu 			continue;
918ab1d5281SYong Wu 
919ab1d5281SYong Wu 		region = iommu_alloc_resv_region(resv->iova_base, resv->size,
920*0251d010SLu Baolu 						 prot, IOMMU_RESV_RESERVED,
921*0251d010SLu Baolu 						 GFP_KERNEL);
922ab1d5281SYong Wu 		if (!region)
923ab1d5281SYong Wu 			return;
924ab1d5281SYong Wu 
925ab1d5281SYong Wu 		list_add_tail(&region->list, head);
926ab1d5281SYong Wu 	}
927ab1d5281SYong Wu }
928ab1d5281SYong Wu 
929b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
9300df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
93180e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
93280e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
9330df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
9340df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
935ab1d5281SYong Wu 	.get_resv_regions = mtk_iommu_get_resv_regions,
9360df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
93718d8c74eSYong Wu 	.owner		= THIS_MODULE,
9389a630a4bSLu Baolu 	.default_domain_ops = &(const struct iommu_domain_ops) {
9399a630a4bSLu Baolu 		.attach_dev	= mtk_iommu_attach_device,
9409a630a4bSLu Baolu 		.detach_dev	= mtk_iommu_detach_device,
9419a630a4bSLu Baolu 		.map		= mtk_iommu_map,
9429a630a4bSLu Baolu 		.unmap		= mtk_iommu_unmap,
9439a630a4bSLu Baolu 		.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
9449a630a4bSLu Baolu 		.iotlb_sync	= mtk_iommu_iotlb_sync,
9459a630a4bSLu Baolu 		.iotlb_sync_map	= mtk_iommu_sync_map,
9469a630a4bSLu Baolu 		.iova_to_phys	= mtk_iommu_iova_to_phys,
9479a630a4bSLu Baolu 		.free		= mtk_iommu_domain_free,
9489a630a4bSLu Baolu 	}
9490df4fabeSYong Wu };
9500df4fabeSYong Wu 
951e24453e1SYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data, unsigned int bankid)
9520df4fabeSYong Wu {
953e24453e1SYong Wu 	const struct mtk_iommu_bank_data *bankx = &data->bank[bankid];
95499ca0228SYong Wu 	const struct mtk_iommu_bank_data *bank0 = &data->bank[0];
9550df4fabeSYong Wu 	u32 regval;
9560df4fabeSYong Wu 
957e24453e1SYong Wu 	/*
958e24453e1SYong Wu 	 * Global control settings are in bank0. May re-init these global registers
959e24453e1SYong Wu 	 * since no sure if there is bank0 consumers.
960e24453e1SYong Wu 	 */
96186580ec9SAngeloGioacchino Del Regno 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, TF_PORT_TO_ADDR_MT8173)) {
962acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
963acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
96486444413SChao Hao 	} else {
96599ca0228SYong Wu 		regval = readl_relaxed(bank0->base + REG_MMU_CTRL_REG);
96686444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
96786444413SChao Hao 	}
96899ca0228SYong Wu 	writel_relaxed(regval, bank0->base + REG_MMU_CTRL_REG);
9690df4fabeSYong Wu 
9706b717796SChao Hao 	if (data->enable_4GB &&
9716b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
97230e2fccfSYong Wu 		/*
97330e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
97430e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
97530e2fccfSYong Wu 		 */
97630e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
97799ca0228SYong Wu 		writel_relaxed(regval, bank0->base + REG_MMU_VLD_PA_RNG);
97830e2fccfSYong Wu 	}
9799a87005eSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, DCM_DISABLE))
98099ca0228SYong Wu 		writel_relaxed(F_MMU_DCM, bank0->base + REG_MMU_DCM_DIS);
9819a87005eSYong Wu 	else
98299ca0228SYong Wu 		writel_relaxed(0, bank0->base + REG_MMU_DCM_DIS);
9839a87005eSYong Wu 
98435c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
98535c1b48dSChao Hao 		/* write command throttling mode */
98699ca0228SYong Wu 		regval = readl_relaxed(bank0->base + REG_MMU_WR_LEN_CTRL);
98735c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
98899ca0228SYong Wu 		writel_relaxed(regval, bank0->base + REG_MMU_WR_LEN_CTRL);
98935c1b48dSChao Hao 	}
990e6dec923SYong Wu 
9916b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
99275eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
9934bb2bf4cSChao Hao 		regval = 0;
9944bb2bf4cSChao Hao 	} else {
99599ca0228SYong Wu 		regval = readl_relaxed(bank0->base + REG_MMU_MISC_CTRL);
996d265a4adSYong Wu 		if (!MTK_IOMMU_HAS_FLAG(data->plat_data, STD_AXI_MODE))
9974bb2bf4cSChao Hao 			regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
9984bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
9994bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
100075eed350SChao Hao 	}
100199ca0228SYong Wu 	writel_relaxed(regval, bank0->base + REG_MMU_MISC_CTRL);
10020df4fabeSYong Wu 
1003e24453e1SYong Wu 	/* Independent settings for each bank */
1004634f57dfSYong Wu 	regval = F_L2_MULIT_HIT_EN |
1005634f57dfSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
1006634f57dfSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
1007634f57dfSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
1008634f57dfSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
1009634f57dfSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
1010e24453e1SYong Wu 	writel_relaxed(regval, bankx->base + REG_MMU_INT_CONTROL0);
1011634f57dfSYong Wu 
1012634f57dfSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
1013634f57dfSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
1014634f57dfSYong Wu 		F_INT_INVALID_PA_FAULT |
1015634f57dfSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
1016634f57dfSYong Wu 		F_INT_TLB_MISS_FAULT |
1017634f57dfSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
1018634f57dfSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
1019e24453e1SYong Wu 	writel_relaxed(regval, bankx->base + REG_MMU_INT_MAIN_CONTROL);
1020634f57dfSYong Wu 
1021634f57dfSYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
1022634f57dfSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
1023634f57dfSYong Wu 	else
1024634f57dfSYong Wu 		regval = lower_32_bits(data->protect_base) |
1025634f57dfSYong Wu 			 upper_32_bits(data->protect_base);
1026e24453e1SYong Wu 	writel_relaxed(regval, bankx->base + REG_MMU_IVRP_PADDR);
1027634f57dfSYong Wu 
1028e24453e1SYong Wu 	if (devm_request_irq(bankx->parent_dev, bankx->irq, mtk_iommu_isr, 0,
1029e24453e1SYong Wu 			     dev_name(bankx->parent_dev), (void *)bankx)) {
1030e24453e1SYong Wu 		writel_relaxed(0, bankx->base + REG_MMU_PT_BASE_ADDR);
1031e24453e1SYong Wu 		dev_err(bankx->parent_dev, "Failed @ IRQ-%d Request\n", bankx->irq);
10320df4fabeSYong Wu 		return -ENODEV;
10330df4fabeSYong Wu 	}
10340df4fabeSYong Wu 
10350df4fabeSYong Wu 	return 0;
10360df4fabeSYong Wu }
10370df4fabeSYong Wu 
10380df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
10390df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
10400df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
10410df4fabeSYong Wu };
10420df4fabeSYong Wu 
1043d2e9a110SYong Wu static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **match,
1044d2e9a110SYong Wu 				  struct mtk_iommu_data *data)
1045d2e9a110SYong Wu {
1046f7b71d0dSYong Wu 	struct device_node *larbnode, *smicomm_node, *smi_subcomm_node;
1047d2e9a110SYong Wu 	struct platform_device *plarbdev;
1048d2e9a110SYong Wu 	struct device_link *link;
1049d2e9a110SYong Wu 	int i, larb_nr, ret;
1050d2e9a110SYong Wu 
1051d2e9a110SYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL);
1052d2e9a110SYong Wu 	if (larb_nr < 0)
1053d2e9a110SYong Wu 		return larb_nr;
1054d2e9a110SYong Wu 
1055d2e9a110SYong Wu 	for (i = 0; i < larb_nr; i++) {
1056d2e9a110SYong Wu 		u32 id;
1057d2e9a110SYong Wu 
1058d2e9a110SYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
1059d2e9a110SYong Wu 		if (!larbnode)
1060d2e9a110SYong Wu 			return -EINVAL;
1061d2e9a110SYong Wu 
1062d2e9a110SYong Wu 		if (!of_device_is_available(larbnode)) {
1063d2e9a110SYong Wu 			of_node_put(larbnode);
1064d2e9a110SYong Wu 			continue;
1065d2e9a110SYong Wu 		}
1066d2e9a110SYong Wu 
1067d2e9a110SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
1068d2e9a110SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
1069d2e9a110SYong Wu 			id = i;
1070d2e9a110SYong Wu 
1071d2e9a110SYong Wu 		plarbdev = of_find_device_by_node(larbnode);
1072d2e9a110SYong Wu 		if (!plarbdev) {
1073d2e9a110SYong Wu 			of_node_put(larbnode);
1074d2e9a110SYong Wu 			return -ENODEV;
1075d2e9a110SYong Wu 		}
1076d2e9a110SYong Wu 		if (!plarbdev->dev.driver) {
1077d2e9a110SYong Wu 			of_node_put(larbnode);
1078d2e9a110SYong Wu 			return -EPROBE_DEFER;
1079d2e9a110SYong Wu 		}
1080d2e9a110SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
1081d2e9a110SYong Wu 
1082d2e9a110SYong Wu 		component_match_add_release(dev, match, component_release_of,
1083d2e9a110SYong Wu 					    component_compare_of, larbnode);
1084d2e9a110SYong Wu 	}
1085d2e9a110SYong Wu 
1086f7b71d0dSYong Wu 	/* Get smi-(sub)-common dev from the last larb. */
1087f7b71d0dSYong Wu 	smi_subcomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
1088f7b71d0dSYong Wu 	if (!smi_subcomm_node)
1089d2e9a110SYong Wu 		return -EINVAL;
1090d2e9a110SYong Wu 
1091f7b71d0dSYong Wu 	/*
1092f7b71d0dSYong Wu 	 * It may have two level smi-common. the node is smi-sub-common if it
1093f7b71d0dSYong Wu 	 * has a new mediatek,smi property. otherwise it is smi-commmon.
1094f7b71d0dSYong Wu 	 */
1095f7b71d0dSYong Wu 	smicomm_node = of_parse_phandle(smi_subcomm_node, "mediatek,smi", 0);
1096f7b71d0dSYong Wu 	if (smicomm_node)
1097f7b71d0dSYong Wu 		of_node_put(smi_subcomm_node);
1098f7b71d0dSYong Wu 	else
1099f7b71d0dSYong Wu 		smicomm_node = smi_subcomm_node;
1100f7b71d0dSYong Wu 
1101d2e9a110SYong Wu 	plarbdev = of_find_device_by_node(smicomm_node);
1102d2e9a110SYong Wu 	of_node_put(smicomm_node);
1103d2e9a110SYong Wu 	data->smicomm_dev = &plarbdev->dev;
1104d2e9a110SYong Wu 
1105d2e9a110SYong Wu 	link = device_link_add(data->smicomm_dev, dev,
1106d2e9a110SYong Wu 			       DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
1107d2e9a110SYong Wu 	if (!link) {
1108d2e9a110SYong Wu 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
1109d2e9a110SYong Wu 		return -EINVAL;
1110d2e9a110SYong Wu 	}
1111d2e9a110SYong Wu 	return 0;
1112d2e9a110SYong Wu }
1113d2e9a110SYong Wu 
11140df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
11150df4fabeSYong Wu {
11160df4fabeSYong Wu 	struct mtk_iommu_data   *data;
11170df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
11180df4fabeSYong Wu 	struct resource         *res;
1119b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
11200df4fabeSYong Wu 	struct component_match  *match = NULL;
1121c2c59456SMiles Chen 	struct regmap		*infracfg;
11220df4fabeSYong Wu 	void                    *protect;
112342d57fc5SYong Wu 	int                     ret, banks_num, i = 0;
1124c2c59456SMiles Chen 	u32			val;
1125c2c59456SMiles Chen 	char                    *p;
112699ca0228SYong Wu 	struct mtk_iommu_bank_data *bank;
112799ca0228SYong Wu 	void __iomem		*base;
11280df4fabeSYong Wu 
11290df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
11300df4fabeSYong Wu 	if (!data)
11310df4fabeSYong Wu 		return -ENOMEM;
11320df4fabeSYong Wu 	data->dev = dev;
1133cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
11340df4fabeSYong Wu 
11350df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
11360df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
11370df4fabeSYong Wu 	if (!protect)
11380df4fabeSYong Wu 		return -ENOMEM;
11390df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
11400df4fabeSYong Wu 
1141c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
11427d748ffdSAngeloGioacchino Del Regno 		infracfg = syscon_regmap_lookup_by_phandle(dev->of_node, "mediatek,infracfg");
11437d748ffdSAngeloGioacchino Del Regno 		if (IS_ERR(infracfg)) {
11447d748ffdSAngeloGioacchino Del Regno 			/*
11457d748ffdSAngeloGioacchino Del Regno 			 * Legacy devicetrees will not specify a phandle to
11467d748ffdSAngeloGioacchino Del Regno 			 * mediatek,infracfg: in that case, we use the older
11477d748ffdSAngeloGioacchino Del Regno 			 * way to retrieve a syscon to infra.
11487d748ffdSAngeloGioacchino Del Regno 			 *
11497d748ffdSAngeloGioacchino Del Regno 			 * This is for retrocompatibility purposes only, hence
11507d748ffdSAngeloGioacchino Del Regno 			 * no more compatibles shall be added to this.
11517d748ffdSAngeloGioacchino Del Regno 			 */
1152c2c59456SMiles Chen 			switch (data->plat_data->m4u_plat) {
1153c2c59456SMiles Chen 			case M4U_MT2712:
1154c2c59456SMiles Chen 				p = "mediatek,mt2712-infracfg";
1155c2c59456SMiles Chen 				break;
1156c2c59456SMiles Chen 			case M4U_MT8173:
1157c2c59456SMiles Chen 				p = "mediatek,mt8173-infracfg";
1158c2c59456SMiles Chen 				break;
1159c2c59456SMiles Chen 			default:
1160c2c59456SMiles Chen 				p = NULL;
1161c2c59456SMiles Chen 			}
1162c2c59456SMiles Chen 
1163c2c59456SMiles Chen 			infracfg = syscon_regmap_lookup_by_compatible(p);
1164c2c59456SMiles Chen 			if (IS_ERR(infracfg))
1165c2c59456SMiles Chen 				return PTR_ERR(infracfg);
11667d748ffdSAngeloGioacchino Del Regno 		}
1167c2c59456SMiles Chen 
1168c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
1169c2c59456SMiles Chen 		if (ret)
1170c2c59456SMiles Chen 			return ret;
1171c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
1172c2c59456SMiles Chen 	}
117301e23c93SYong Wu 
117442d57fc5SYong Wu 	banks_num = data->plat_data->banks_num;
11750df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
117642d57fc5SYong Wu 	if (resource_size(res) < banks_num * MTK_IOMMU_BANK_SZ) {
117742d57fc5SYong Wu 		dev_err(dev, "banknr %d. res %pR is not enough.\n", banks_num, res);
117842d57fc5SYong Wu 		return -EINVAL;
117942d57fc5SYong Wu 	}
118099ca0228SYong Wu 	base = devm_ioremap_resource(dev, res);
118199ca0228SYong Wu 	if (IS_ERR(base))
118299ca0228SYong Wu 		return PTR_ERR(base);
1183b16c0170SJoerg Roedel 	ioaddr = res->start;
11840df4fabeSYong Wu 
118599ca0228SYong Wu 	data->bank = devm_kmalloc(dev, banks_num * sizeof(*data->bank), GFP_KERNEL);
118699ca0228SYong Wu 	if (!data->bank)
118799ca0228SYong Wu 		return -ENOMEM;
118899ca0228SYong Wu 
118942d57fc5SYong Wu 	do {
119042d57fc5SYong Wu 		if (!data->plat_data->banks_enable[i])
119142d57fc5SYong Wu 			continue;
119242d57fc5SYong Wu 		bank = &data->bank[i];
119342d57fc5SYong Wu 		bank->id = i;
119442d57fc5SYong Wu 		bank->base = base + i * MTK_IOMMU_BANK_SZ;
119599ca0228SYong Wu 		bank->m4u_dom = NULL;
119642d57fc5SYong Wu 
119742d57fc5SYong Wu 		bank->irq = platform_get_irq(pdev, i);
119899ca0228SYong Wu 		if (bank->irq < 0)
119999ca0228SYong Wu 			return bank->irq;
120099ca0228SYong Wu 		bank->parent_dev = dev;
120199ca0228SYong Wu 		bank->parent_data = data;
120299ca0228SYong Wu 		spin_lock_init(&bank->tlb_lock);
120342d57fc5SYong Wu 	} while (++i < banks_num);
12040df4fabeSYong Wu 
12056b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
12060df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
12070df4fabeSYong Wu 		if (IS_ERR(data->bclk))
12080df4fabeSYong Wu 			return PTR_ERR(data->bclk);
12092aa4c259SYong Wu 	}
12100df4fabeSYong Wu 
1211c0b57581SYong Wu 	pm_runtime_enable(dev);
1212c0b57581SYong Wu 
1213d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1214d2e9a110SYong Wu 		ret = mtk_iommu_mm_dts_parse(dev, &match, data);
1215d2e9a110SYong Wu 		if (ret) {
12163168010dSNícolas F. R. A. Prado 			dev_err_probe(dev, ret, "mm dts parse fail\n");
1217c0b57581SYong Wu 			goto out_runtime_disable;
1218baf94e6eSYong Wu 		}
121921fd9be4SAngeloGioacchino Del Regno 	} else if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_INFRA)) {
122021fd9be4SAngeloGioacchino Del Regno 		p = data->plat_data->pericfg_comp_str;
122121fd9be4SAngeloGioacchino Del Regno 		data->pericfg = syscon_regmap_lookup_by_compatible(p);
122221fd9be4SAngeloGioacchino Del Regno 		if (IS_ERR(data->pericfg)) {
122321fd9be4SAngeloGioacchino Del Regno 			ret = PTR_ERR(data->pericfg);
1224f9b8c9b2SYong Wu 			goto out_runtime_disable;
1225f9b8c9b2SYong Wu 		}
1226d2e9a110SYong Wu 	}
1227baf94e6eSYong Wu 
12280df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
12290e5a3f2eSYong Wu 	mutex_init(&data->mutex);
12300df4fabeSYong Wu 
1231b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
1232b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
1233b16c0170SJoerg Roedel 	if (ret)
1234baf94e6eSYong Wu 		goto out_link_remove;
1235b16c0170SJoerg Roedel 
12362d471b20SRobin Murphy 	ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
1237b16c0170SJoerg Roedel 	if (ret)
1238986d9ec5SYong Wu 		goto out_sysfs_remove;
1239b16c0170SJoerg Roedel 
12409e3a2a64SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, SHARE_PGTABLE)) {
12419e3a2a64SYong Wu 		list_add_tail(&data->list, data->plat_data->hw_list);
12429e3a2a64SYong Wu 		data->hw_list = data->plat_data->hw_list;
12439e3a2a64SYong Wu 	} else {
12449e3a2a64SYong Wu 		INIT_LIST_HEAD(&data->hw_list_head);
12459e3a2a64SYong Wu 		list_add_tail(&data->list, &data->hw_list_head);
12469e3a2a64SYong Wu 		data->hw_list = &data->hw_list_head;
12479e3a2a64SYong Wu 	}
12487c3a2ec0SYong Wu 
1249d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1250986d9ec5SYong Wu 		ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
1251986d9ec5SYong Wu 		if (ret)
1252e7629070SYong Wu 			goto out_list_del;
1253e7629070SYong Wu 	}
1254986d9ec5SYong Wu 	return ret;
1255986d9ec5SYong Wu 
1256986d9ec5SYong Wu out_list_del:
1257986d9ec5SYong Wu 	list_del(&data->list);
1258986d9ec5SYong Wu 	iommu_device_unregister(&data->iommu);
1259986d9ec5SYong Wu out_sysfs_remove:
1260986d9ec5SYong Wu 	iommu_device_sysfs_remove(&data->iommu);
1261baf94e6eSYong Wu out_link_remove:
1262d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
1263baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, dev);
1264c0b57581SYong Wu out_runtime_disable:
1265c0b57581SYong Wu 	pm_runtime_disable(dev);
1266986d9ec5SYong Wu 	return ret;
12670df4fabeSYong Wu }
12680df4fabeSYong Wu 
12690df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
12700df4fabeSYong Wu {
12710df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
127242d57fc5SYong Wu 	struct mtk_iommu_bank_data *bank;
127342d57fc5SYong Wu 	int i;
12740df4fabeSYong Wu 
1275b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
1276b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
1277b16c0170SJoerg Roedel 
1278ee55f75eSYong Wu 	list_del(&data->list);
12790df4fabeSYong Wu 
1280d2e9a110SYong Wu 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
1281baf94e6eSYong Wu 		device_link_remove(data->smicomm_dev, &pdev->dev);
1282d2e9a110SYong Wu 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
1283d2e9a110SYong Wu 	}
1284c0b57581SYong Wu 	pm_runtime_disable(&pdev->dev);
128542d57fc5SYong Wu 	for (i = 0; i < data->plat_data->banks_num; i++) {
128642d57fc5SYong Wu 		bank = &data->bank[i];
128742d57fc5SYong Wu 		if (!bank->m4u_dom)
128842d57fc5SYong Wu 			continue;
128999ca0228SYong Wu 		devm_free_irq(&pdev->dev, bank->irq, bank);
129042d57fc5SYong Wu 	}
12910df4fabeSYong Wu 	return 0;
12920df4fabeSYong Wu }
12930df4fabeSYong Wu 
129434665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
12950df4fabeSYong Wu {
12960df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
12970df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1298d7127de1SYong Wu 	void __iomem *base;
1299d7127de1SYong Wu 	int i = 0;
13000df4fabeSYong Wu 
1301d7127de1SYong Wu 	base = data->bank[i].base;
130235c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
130375eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
13040df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
13050df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
1306b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
1307d7127de1SYong Wu 	do {
1308d7127de1SYong Wu 		if (!data->plat_data->banks_enable[i])
1309d7127de1SYong Wu 			continue;
1310d7127de1SYong Wu 		base = data->bank[i].base;
1311d7127de1SYong Wu 		reg->int_control[i] = readl_relaxed(base + REG_MMU_INT_CONTROL0);
1312d7127de1SYong Wu 		reg->int_main_control[i] = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
1313d7127de1SYong Wu 		reg->ivrp_paddr[i] = readl_relaxed(base + REG_MMU_IVRP_PADDR);
1314d7127de1SYong Wu 	} while (++i < data->plat_data->banks_num);
13156254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
13160df4fabeSYong Wu 	return 0;
13170df4fabeSYong Wu }
13180df4fabeSYong Wu 
131934665c79SYong Wu static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
13200df4fabeSYong Wu {
13210df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
13220df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
1323d7127de1SYong Wu 	struct mtk_iommu_domain *m4u_dom;
1324d7127de1SYong Wu 	void __iomem *base;
1325d7127de1SYong Wu 	int ret, i = 0;
13260df4fabeSYong Wu 
13276254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
13286254b64fSYong Wu 	if (ret) {
13296254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
13306254b64fSYong Wu 		return ret;
13316254b64fSYong Wu 	}
1332b34ea31fSDafna Hirschfeld 
1333b34ea31fSDafna Hirschfeld 	/*
1334b34ea31fSDafna Hirschfeld 	 * Uppon first resume, only enable the clk and return, since the values of the
1335b34ea31fSDafna Hirschfeld 	 * registers are not yet set.
1336b34ea31fSDafna Hirschfeld 	 */
1337d7127de1SYong Wu 	if (!reg->wr_len_ctrl)
1338b34ea31fSDafna Hirschfeld 		return 0;
1339b34ea31fSDafna Hirschfeld 
1340d7127de1SYong Wu 	base = data->bank[i].base;
134135c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
134275eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
13430df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
13440df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
1345b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
1346d7127de1SYong Wu 	do {
1347d7127de1SYong Wu 		m4u_dom = data->bank[i].m4u_dom;
1348d7127de1SYong Wu 		if (!data->plat_data->banks_enable[i] || !m4u_dom)
1349d7127de1SYong Wu 			continue;
1350d7127de1SYong Wu 		base = data->bank[i].base;
1351d7127de1SYong Wu 		writel_relaxed(reg->int_control[i], base + REG_MMU_INT_CONTROL0);
1352d7127de1SYong Wu 		writel_relaxed(reg->int_main_control[i], base + REG_MMU_INT_MAIN_CONTROL);
1353d7127de1SYong Wu 		writel_relaxed(reg->ivrp_paddr[i], base + REG_MMU_IVRP_PADDR);
1354301c3ca1SYunfei Wang 		writel(m4u_dom->cfg.arm_v7s_cfg.ttbr, base + REG_MMU_PT_BASE_ADDR);
1355d7127de1SYong Wu 	} while (++i < data->plat_data->banks_num);
13564f23f6d4SYong Wu 
13574f23f6d4SYong Wu 	/*
13584f23f6d4SYong Wu 	 * Users may allocate dma buffer before they call pm_runtime_get,
13594f23f6d4SYong Wu 	 * in which case it will lack the necessary tlb flush.
13604f23f6d4SYong Wu 	 * Thus, make sure to update the tlb after each PM resume.
13614f23f6d4SYong Wu 	 */
13624f23f6d4SYong Wu 	mtk_iommu_tlb_flush_all(data);
13630df4fabeSYong Wu 	return 0;
13640df4fabeSYong Wu }
13650df4fabeSYong Wu 
1366e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
136734665c79SYong Wu 	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
136834665c79SYong Wu 	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
136934665c79SYong Wu 				     pm_runtime_force_resume)
13700df4fabeSYong Wu };
13710df4fabeSYong Wu 
1372cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
1373cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
1374d2e9a110SYong Wu 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG | SHARE_PGTABLE |
1375d2e9a110SYong Wu 			MTK_IOMMU_TYPE_MM,
13769e3a2a64SYong Wu 	.hw_list      = &m4ulist,
1377b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1378585e58f4SYong Wu 	.iova_region  = single_domain,
137999ca0228SYong Wu 	.banks_num    = 1,
138099ca0228SYong Wu 	.banks_enable = {true},
1381585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
138237276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
1383cecdce9dSYong Wu };
1384cecdce9dSYong Wu 
1385068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
1386068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
1387d2e9a110SYong Wu 	.flags         = HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN | WR_THROT_EN |
1388301c3ca1SYunfei Wang 			 MTK_IOMMU_TYPE_MM | PGTABLE_PA_35_EN,
1389068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
139099ca0228SYong Wu 	.banks_num    = 1,
139199ca0228SYong Wu 	.banks_enable = {true},
1392585e58f4SYong Wu 	.iova_region   = single_domain,
1393585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
1394068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
1395cecdce9dSYong Wu };
1396cecdce9dSYong Wu 
1397717ec15eSAngeloGioacchino Del Regno static const struct mtk_iommu_plat_data mt6795_data = {
1398717ec15eSAngeloGioacchino Del Regno 	.m4u_plat     = M4U_MT6795,
1399717ec15eSAngeloGioacchino Del Regno 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
1400717ec15eSAngeloGioacchino Del Regno 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM |
1401717ec15eSAngeloGioacchino Del Regno 			TF_PORT_TO_ADDR_MT8173,
1402717ec15eSAngeloGioacchino Del Regno 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1403717ec15eSAngeloGioacchino Del Regno 	.banks_num    = 1,
1404717ec15eSAngeloGioacchino Del Regno 	.banks_enable = {true},
1405717ec15eSAngeloGioacchino Del Regno 	.iova_region  = single_domain,
1406717ec15eSAngeloGioacchino Del Regno 	.iova_region_nr = ARRAY_SIZE(single_domain),
1407717ec15eSAngeloGioacchino Del Regno 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}}, /* Linear mapping. */
1408717ec15eSAngeloGioacchino Del Regno };
1409717ec15eSAngeloGioacchino Del Regno 
14103c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
14113c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
1412d2e9a110SYong Wu 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM,
14133c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
141499ca0228SYong Wu 	.banks_num    = 1,
141599ca0228SYong Wu 	.banks_enable = {true},
1416585e58f4SYong Wu 	.iova_region  = single_domain,
1417585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
14183c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
14193c213562SFabien Parent };
14203c213562SFabien Parent 
1421cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
1422cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
1423d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
142486580ec9SAngeloGioacchino Del Regno 			HAS_LEGACY_IVRP_PADDR | MTK_IOMMU_TYPE_MM |
142586580ec9SAngeloGioacchino Del Regno 			TF_PORT_TO_ADDR_MT8173,
1426b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
142799ca0228SYong Wu 	.banks_num    = 1,
142899ca0228SYong Wu 	.banks_enable = {true},
1429585e58f4SYong Wu 	.iova_region  = single_domain,
1430585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
143137276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
1432cecdce9dSYong Wu };
1433cecdce9dSYong Wu 
1434907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
1435907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
1436d2e9a110SYong Wu 	.flags        = RESET_AXI | MTK_IOMMU_TYPE_MM,
1437b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
143899ca0228SYong Wu 	.banks_num    = 1,
143999ca0228SYong Wu 	.banks_enable = {true},
1440585e58f4SYong Wu 	.iova_region  = single_domain,
1441585e58f4SYong Wu 	.iova_region_nr = ARRAY_SIZE(single_domain),
144237276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1443907ba6a1SYong Wu };
1444907ba6a1SYong Wu 
1445e8d7ccaaSYong Wu static const struct mtk_iommu_plat_data mt8186_data_mm = {
1446e8d7ccaaSYong Wu 	.m4u_plat       = M4U_MT8186,
1447e8d7ccaaSYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1448e8d7ccaaSYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
1449e8d7ccaaSYong Wu 	.larbid_remap   = {{0}, {1, MTK_INVALID_LARBID, 8}, {4}, {7}, {2}, {9, 11, 19, 20},
1450e8d7ccaaSYong Wu 			   {MTK_INVALID_LARBID, 14, 16},
1451e8d7ccaaSYong Wu 			   {MTK_INVALID_LARBID, 13, MTK_INVALID_LARBID, 17}},
1452e8d7ccaaSYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
1453e8d7ccaaSYong Wu 	.banks_num      = 1,
1454e8d7ccaaSYong Wu 	.banks_enable   = {true},
1455e8d7ccaaSYong Wu 	.iova_region    = mt8192_multi_dom,
1456e8d7ccaaSYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
1457e8d7ccaaSYong Wu };
1458e8d7ccaaSYong Wu 
14599e3489e0SYong Wu static const struct mtk_iommu_plat_data mt8192_data = {
14609e3489e0SYong Wu 	.m4u_plat       = M4U_MT8192,
14619ec30c09SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1462d2e9a110SYong Wu 			  WR_THROT_EN | IOVA_34_EN | MTK_IOMMU_TYPE_MM,
14639e3489e0SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
146499ca0228SYong Wu 	.banks_num      = 1,
146599ca0228SYong Wu 	.banks_enable   = {true},
14669e3489e0SYong Wu 	.iova_region    = mt8192_multi_dom,
14679e3489e0SYong Wu 	.iova_region_nr = ARRAY_SIZE(mt8192_multi_dom),
14689e3489e0SYong Wu 	.larbid_remap   = {{0}, {1}, {4, 5}, {7}, {2}, {9, 11, 19, 20},
14699e3489e0SYong Wu 			   {0, 14, 16}, {0, 13, 18, 17}},
14709e3489e0SYong Wu };
14719e3489e0SYong Wu 
1472ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_infra = {
1473ef68a193SYong Wu 	.m4u_plat	  = M4U_MT8195,
1474ef68a193SYong Wu 	.flags            = WR_THROT_EN | DCM_DISABLE | STD_AXI_MODE | PM_CLK_AO |
1475ef68a193SYong Wu 			    MTK_IOMMU_TYPE_INFRA | IFA_IOMMU_PCIE_SUPPORT,
1476ef68a193SYong Wu 	.pericfg_comp_str = "mediatek,mt8195-pericfg_ao",
1477ef68a193SYong Wu 	.inv_sel_reg      = REG_MMU_INV_SEL_GEN2,
14787597e3c5SYong Wu 	.banks_num	  = 5,
14797597e3c5SYong Wu 	.banks_enable     = {true, false, false, false, true},
14807597e3c5SYong Wu 	.banks_portmsk    = {[0] = GENMASK(19, 16),     /* PCIe */
14817597e3c5SYong Wu 			     [4] = GENMASK(31, 20),     /* USB */
14827597e3c5SYong Wu 			    },
1483ef68a193SYong Wu 	.iova_region      = single_domain,
1484ef68a193SYong Wu 	.iova_region_nr   = ARRAY_SIZE(single_domain),
1485ef68a193SYong Wu };
1486ef68a193SYong Wu 
1487ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_vdo = {
1488ef68a193SYong Wu 	.m4u_plat	= M4U_MT8195,
1489ef68a193SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_2BITS | OUT_ORDER_WR_EN |
1490ef68a193SYong Wu 			  WR_THROT_EN | IOVA_34_EN | SHARE_PGTABLE | MTK_IOMMU_TYPE_MM,
1491ef68a193SYong Wu 	.hw_list        = &m4ulist,
1492ef68a193SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
149399ca0228SYong Wu 	.banks_num      = 1,
149499ca0228SYong Wu 	.banks_enable   = {true},
1495ef68a193SYong Wu 	.iova_region	= mt8192_multi_dom,
1496ef68a193SYong Wu 	.iova_region_nr	= ARRAY_SIZE(mt8192_multi_dom),
1497ef68a193SYong Wu 	.larbid_remap   = {{2, 0}, {21}, {24}, {7}, {19}, {9, 10, 11},
1498ef68a193SYong Wu 			   {13, 17, 15/* 17b */, 25}, {5}},
1499ef68a193SYong Wu };
1500ef68a193SYong Wu 
1501ef68a193SYong Wu static const struct mtk_iommu_plat_data mt8195_data_vpp = {
1502ef68a193SYong Wu 	.m4u_plat	= M4U_MT8195,
1503ef68a193SYong Wu 	.flags          = HAS_BCLK | HAS_SUB_COMM_3BITS | OUT_ORDER_WR_EN |
1504ef68a193SYong Wu 			  WR_THROT_EN | IOVA_34_EN | SHARE_PGTABLE | MTK_IOMMU_TYPE_MM,
1505ef68a193SYong Wu 	.hw_list        = &m4ulist,
1506ef68a193SYong Wu 	.inv_sel_reg    = REG_MMU_INV_SEL_GEN2,
150799ca0228SYong Wu 	.banks_num      = 1,
150899ca0228SYong Wu 	.banks_enable   = {true},
1509ef68a193SYong Wu 	.iova_region	= mt8192_multi_dom,
1510ef68a193SYong Wu 	.iova_region_nr	= ARRAY_SIZE(mt8192_multi_dom),
1511ef68a193SYong Wu 	.larbid_remap   = {{1}, {3},
1512ef68a193SYong Wu 			   {22, MTK_INVALID_LARBID, MTK_INVALID_LARBID, MTK_INVALID_LARBID, 23},
1513ef68a193SYong Wu 			   {8}, {20}, {12},
1514ef68a193SYong Wu 			   /* 16: 16a; 29: 16b; 30: CCUtop0; 31: CCUtop1 */
1515ef68a193SYong Wu 			   {14, 16, 29, 26, 30, 31, 18},
1516ef68a193SYong Wu 			   {4, MTK_INVALID_LARBID, MTK_INVALID_LARBID, MTK_INVALID_LARBID, 6}},
1517ef68a193SYong Wu };
1518ef68a193SYong Wu 
15190df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
1520cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1521068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
1522717ec15eSAngeloGioacchino Del Regno 	{ .compatible = "mediatek,mt6795-m4u", .data = &mt6795_data},
15233c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1524cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1525907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
1526e8d7ccaaSYong Wu 	{ .compatible = "mediatek,mt8186-iommu-mm",    .data = &mt8186_data_mm}, /* mm: m4u */
15279e3489e0SYong Wu 	{ .compatible = "mediatek,mt8192-m4u", .data = &mt8192_data},
1528ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-infra", .data = &mt8195_data_infra},
1529ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-vdo",   .data = &mt8195_data_vdo},
1530ef68a193SYong Wu 	{ .compatible = "mediatek,mt8195-iommu-vpp",   .data = &mt8195_data_vpp},
15310df4fabeSYong Wu 	{}
15320df4fabeSYong Wu };
15330df4fabeSYong Wu 
15340df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
15350df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
15360df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
15370df4fabeSYong Wu 	.driver	= {
15380df4fabeSYong Wu 		.name = "mtk-iommu",
1539f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
15400df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
15410df4fabeSYong Wu 	}
15420df4fabeSYong Wu };
154318d8c74eSYong Wu module_platform_driver(mtk_iommu_driver);
15440df4fabeSYong Wu 
154518d8c74eSYong Wu MODULE_DESCRIPTION("IOMMU API for MediaTek M4U implementations");
154618d8c74eSYong Wu MODULE_LICENSE("GPL v2");
1547