xref: /linux/drivers/iommu/mtk_iommu.c (revision 9bdfe4c175c8a9ea4959f7ad087e3d66d3917bc8)
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  */
60df4fabeSYong Wu #include <linux/bug.h>
70df4fabeSYong Wu #include <linux/clk.h>
80df4fabeSYong Wu #include <linux/component.h>
90df4fabeSYong Wu #include <linux/device.h>
100df4fabeSYong Wu #include <linux/dma-iommu.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>
160df4fabeSYong Wu #include <linux/list.h>
17c2c59456SMiles Chen #include <linux/mfd/syscon.h>
180df4fabeSYong Wu #include <linux/of_address.h>
190df4fabeSYong Wu #include <linux/of_iommu.h>
200df4fabeSYong Wu #include <linux/of_irq.h>
210df4fabeSYong Wu #include <linux/of_platform.h>
220df4fabeSYong Wu #include <linux/platform_device.h>
23c2c59456SMiles Chen #include <linux/regmap.h>
240df4fabeSYong Wu #include <linux/slab.h>
250df4fabeSYong Wu #include <linux/spinlock.h>
26c2c59456SMiles Chen #include <linux/soc/mediatek/infracfg.h>
270df4fabeSYong Wu #include <asm/barrier.h>
280df4fabeSYong Wu #include <soc/mediatek/smi.h>
290df4fabeSYong Wu 
309ca340c9SHonghui Zhang #include "mtk_iommu.h"
310df4fabeSYong Wu 
320df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
33907ba6a1SYong Wu #define MMU_PT_ADDR_MASK			GENMASK(31, 7)
340df4fabeSYong Wu 
350df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
360df4fabeSYong Wu #define F_ALL_INVLD				0x2
370df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
380df4fabeSYong Wu 
390df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
400df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
410df4fabeSYong Wu 
42068c86e9SChao Hao #define REG_MMU_INV_SEL_GEN2			0x02c
43b053bc71SChao Hao #define REG_MMU_INV_SEL_GEN1			0x038
440df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
450df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
460df4fabeSYong Wu 
4775eed350SChao Hao #define REG_MMU_MISC_CTRL			0x048
484bb2bf4cSChao Hao #define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
494bb2bf4cSChao Hao #define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))
504bb2bf4cSChao Hao 
510df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
5235c1b48dSChao Hao #define REG_MMU_WR_LEN_CTRL			0x054
5335c1b48dSChao Hao #define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
540df4fabeSYong Wu 
550df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
56acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
570df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
58acb3c92aSYong Wu #define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
590df4fabeSYong Wu 
600df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6170ca608bSYong Wu 
6230e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6330e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
640df4fabeSYong Wu 
650df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
660df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
670df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
680df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
690df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
700df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
710df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
720df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
730df4fabeSYong Wu 
740df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
7515a01f4cSYong Wu 						/* mmu0 | mmu1 */
7615a01f4cSYong Wu #define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
7715a01f4cSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
7815a01f4cSYong Wu #define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
7915a01f4cSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
8015a01f4cSYong Wu #define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
8115a01f4cSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
8215a01f4cSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
830df4fabeSYong Wu 
840df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
850df4fabeSYong Wu 
860df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
8715a01f4cSYong Wu #define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
8815a01f4cSYong Wu #define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
890df4fabeSYong Wu 
9015a01f4cSYong Wu #define REG_MMU0_FAULT_VA			0x13c
910df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
920df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
930df4fabeSYong Wu 
9415a01f4cSYong Wu #define REG_MMU0_INVLD_PA			0x140
9515a01f4cSYong Wu #define REG_MMU1_FAULT_VA			0x144
9615a01f4cSYong Wu #define REG_MMU1_INVLD_PA			0x148
9715a01f4cSYong Wu #define REG_MMU0_INT_ID				0x150
9815a01f4cSYong Wu #define REG_MMU1_INT_ID				0x154
9937276e00SChao Hao #define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
10037276e00SChao Hao #define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
10115a01f4cSYong Wu #define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
10215a01f4cSYong Wu #define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
1030df4fabeSYong Wu 
104829316b3SChao Hao #define MTK_PROTECT_PA_ALIGN			256
1050df4fabeSYong Wu 
1066b717796SChao Hao #define HAS_4GB_MODE			BIT(0)
1076b717796SChao Hao /* HW will use the EMI clock if there isn't the "bclk". */
1086b717796SChao Hao #define HAS_BCLK			BIT(1)
1096b717796SChao Hao #define HAS_VLD_PA_RNG			BIT(2)
1106b717796SChao Hao #define RESET_AXI			BIT(3)
1114bb2bf4cSChao Hao #define OUT_ORDER_WR_EN			BIT(4)
11237276e00SChao Hao #define HAS_SUB_COMM			BIT(5)
11335c1b48dSChao Hao #define WR_THROT_EN			BIT(6)
114d1b5ef00SFabien Parent #define HAS_LEGACY_IVRP_PADDR		BIT(7)
1152f317da4SYong Wu #define IOVA_34_EN			BIT(8)
1166b717796SChao Hao 
1176b717796SChao Hao #define MTK_IOMMU_HAS_FLAG(pdata, _x) \
1186b717796SChao Hao 		((((pdata)->flags) & (_x)) == (_x))
1196b717796SChao Hao 
1200df4fabeSYong Wu struct mtk_iommu_domain {
1210df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
1220df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
1230df4fabeSYong Wu 
1240df4fabeSYong Wu 	struct iommu_domain		domain;
1250df4fabeSYong Wu };
1260df4fabeSYong Wu 
127b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops;
1280df4fabeSYong Wu 
12976ce6546SYong Wu /*
13076ce6546SYong Wu  * In M4U 4GB mode, the physical address is remapped as below:
13176ce6546SYong Wu  *
13276ce6546SYong Wu  * CPU Physical address:
13376ce6546SYong Wu  * ====================
13476ce6546SYong Wu  *
13576ce6546SYong Wu  * 0      1G       2G     3G       4G     5G
13676ce6546SYong Wu  * |---A---|---B---|---C---|---D---|---E---|
13776ce6546SYong Wu  * +--I/O--+------------Memory-------------+
13876ce6546SYong Wu  *
13976ce6546SYong Wu  * IOMMU output physical address:
14076ce6546SYong Wu  *  =============================
14176ce6546SYong Wu  *
14276ce6546SYong Wu  *                                 4G      5G     6G      7G      8G
14376ce6546SYong Wu  *                                 |---E---|---B---|---C---|---D---|
14476ce6546SYong Wu  *                                 +------------Memory-------------+
14576ce6546SYong Wu  *
14676ce6546SYong Wu  * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
14776ce6546SYong Wu  * bit32 of the CPU physical address always is needed to set, and for Region
14876ce6546SYong Wu  * 'E', the CPU physical address keep as is.
14976ce6546SYong Wu  * Additionally, The iommu consumers always use the CPU phyiscal address.
15076ce6546SYong Wu  */
151b4dad40eSYong Wu #define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
15276ce6546SYong Wu 
1537c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
1547c3a2ec0SYong Wu 
1557c3a2ec0SYong Wu #define for_each_m4u(data)	list_for_each_entry(data, &m4ulist, list)
1567c3a2ec0SYong Wu 
1577c3a2ec0SYong Wu /*
1587c3a2ec0SYong Wu  * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
1597c3a2ec0SYong Wu  * for the performance.
1607c3a2ec0SYong Wu  *
1617c3a2ec0SYong Wu  * Here always return the mtk_iommu_data of the first probed M4U where the
1627c3a2ec0SYong Wu  * iommu domain information is recorded.
1637c3a2ec0SYong Wu  */
1647c3a2ec0SYong Wu static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
1657c3a2ec0SYong Wu {
1667c3a2ec0SYong Wu 	struct mtk_iommu_data *data;
1677c3a2ec0SYong Wu 
1687c3a2ec0SYong Wu 	for_each_m4u(data)
1697c3a2ec0SYong Wu 		return data;
1707c3a2ec0SYong Wu 
1717c3a2ec0SYong Wu 	return NULL;
1727c3a2ec0SYong Wu }
1737c3a2ec0SYong Wu 
1740df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
1750df4fabeSYong Wu {
1760df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
1770df4fabeSYong Wu }
1780df4fabeSYong Wu 
1790954d61aSYong Wu static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
1800df4fabeSYong Wu {
1817c3a2ec0SYong Wu 	for_each_m4u(data) {
1827c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
183b053bc71SChao Hao 			       data->base + data->plat_data->inv_sel_reg);
1840df4fabeSYong Wu 		writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
1850df4fabeSYong Wu 		wmb(); /* Make sure the tlb flush all done */
1860df4fabeSYong Wu 	}
1877c3a2ec0SYong Wu }
1880df4fabeSYong Wu 
1891f4fd624SYong Wu static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
1900954d61aSYong Wu 					   size_t granule,
1910954d61aSYong Wu 					   struct mtk_iommu_data *data)
1920df4fabeSYong Wu {
1931f4fd624SYong Wu 	unsigned long flags;
1941f4fd624SYong Wu 	int ret;
1951f4fd624SYong Wu 	u32 tmp;
1960df4fabeSYong Wu 
1977c3a2ec0SYong Wu 	for_each_m4u(data) {
1981f4fd624SYong Wu 		spin_lock_irqsave(&data->tlb_lock, flags);
1997c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
200b053bc71SChao Hao 			       data->base + data->plat_data->inv_sel_reg);
2010df4fabeSYong Wu 
2020df4fabeSYong Wu 		writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
2037c3a2ec0SYong Wu 		writel_relaxed(iova + size - 1,
2047c3a2ec0SYong Wu 			       data->base + REG_MMU_INVLD_END_A);
2057c3a2ec0SYong Wu 		writel_relaxed(F_MMU_INV_RANGE,
2067c3a2ec0SYong Wu 			       data->base + REG_MMU_INVALIDATE);
2070df4fabeSYong Wu 
2081f4fd624SYong Wu 		/* tlb sync */
2097c3a2ec0SYong Wu 		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
210c90ae4a6SYong Wu 						tmp, tmp != 0, 10, 1000);
2110df4fabeSYong Wu 		if (ret) {
2120df4fabeSYong Wu 			dev_warn(data->dev,
2130df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
2140954d61aSYong Wu 			mtk_iommu_tlb_flush_all(data);
2150df4fabeSYong Wu 		}
2160df4fabeSYong Wu 		/* Clear the CPE status */
2170df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
218da3cc91bSYong Wu 		spin_unlock_irqrestore(&data->tlb_lock, flags);
2190df4fabeSYong Wu 	}
2207c3a2ec0SYong Wu }
2210df4fabeSYong Wu 
2220df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
2230df4fabeSYong Wu {
2240df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_id;
2250df4fabeSYong Wu 	struct mtk_iommu_domain *dom = data->m4u_dom;
2260df4fabeSYong Wu 	u32 int_state, regval, fault_iova, fault_pa;
22737276e00SChao Hao 	unsigned int fault_larb, fault_port, sub_comm = 0;
2280df4fabeSYong Wu 	bool layer, write;
2290df4fabeSYong Wu 
2300df4fabeSYong Wu 	/* Read error info from registers */
2310df4fabeSYong Wu 	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
23215a01f4cSYong Wu 	if (int_state & F_REG_MMU0_FAULT_MASK) {
23315a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
23415a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
23515a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
23615a01f4cSYong Wu 	} else {
23715a01f4cSYong Wu 		regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
23815a01f4cSYong Wu 		fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
23915a01f4cSYong Wu 		fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
24015a01f4cSYong Wu 	}
2410df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
2420df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
24315a01f4cSYong Wu 	fault_port = F_MMU_INT_ID_PORT_ID(regval);
24437276e00SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM)) {
24537276e00SChao Hao 		fault_larb = F_MMU_INT_ID_COMM_ID(regval);
24637276e00SChao Hao 		sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
24737276e00SChao Hao 	} else {
24837276e00SChao Hao 		fault_larb = F_MMU_INT_ID_LARB_ID(regval);
24937276e00SChao Hao 	}
25037276e00SChao Hao 	fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
251b3e5eee7SYong Wu 
2520df4fabeSYong Wu 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
2530df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
2540df4fabeSYong Wu 		dev_err_ratelimited(
2550df4fabeSYong Wu 			data->dev,
2560df4fabeSYong Wu 			"fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
2570df4fabeSYong Wu 			int_state, fault_iova, fault_pa, fault_larb, fault_port,
2580df4fabeSYong Wu 			layer, write ? "write" : "read");
2590df4fabeSYong Wu 	}
2600df4fabeSYong Wu 
2610df4fabeSYong Wu 	/* Interrupt clear */
2620df4fabeSYong Wu 	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
2630df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
2640df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
2650df4fabeSYong Wu 
2660df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
2670df4fabeSYong Wu 
2680df4fabeSYong Wu 	return IRQ_HANDLED;
2690df4fabeSYong Wu }
2700df4fabeSYong Wu 
2710df4fabeSYong Wu static void mtk_iommu_config(struct mtk_iommu_data *data,
2720df4fabeSYong Wu 			     struct device *dev, bool enable)
2730df4fabeSYong Wu {
2740df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
2750df4fabeSYong Wu 	unsigned int                 larbid, portid;
276a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
27758f0d1d5SRobin Murphy 	int i;
2780df4fabeSYong Wu 
27958f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
28058f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
28158f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
2821ee9feb2SYong Wu 		larb_mmu = &data->larb_imu[larbid];
2830df4fabeSYong Wu 
2840df4fabeSYong Wu 		dev_dbg(dev, "%s iommu port: %d\n",
2850df4fabeSYong Wu 			enable ? "enable" : "disable", portid);
2860df4fabeSYong Wu 
2870df4fabeSYong Wu 		if (enable)
2880df4fabeSYong Wu 			larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
2890df4fabeSYong Wu 		else
2900df4fabeSYong Wu 			larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
2910df4fabeSYong Wu 	}
2920df4fabeSYong Wu }
2930df4fabeSYong Wu 
2944b00f5acSYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
2950df4fabeSYong Wu {
2964b00f5acSYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
2970df4fabeSYong Wu 
2980df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
2990df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
3000df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
301b4dad40eSYong Wu 			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
3020df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
3032f317da4SYong Wu 		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
3040df4fabeSYong Wu 		.iommu_dev = data->dev,
3050df4fabeSYong Wu 	};
3060df4fabeSYong Wu 
307*9bdfe4c1SYong Wu 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
308*9bdfe4c1SYong Wu 		dom->cfg.oas = data->enable_4GB ? 33 : 32;
309*9bdfe4c1SYong Wu 	else
310*9bdfe4c1SYong Wu 		dom->cfg.oas = 35;
311*9bdfe4c1SYong Wu 
3120df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
3130df4fabeSYong Wu 	if (!dom->iop) {
3140df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
3150df4fabeSYong Wu 		return -EINVAL;
3160df4fabeSYong Wu 	}
3170df4fabeSYong Wu 
3180df4fabeSYong Wu 	/* Update our support page sizes bitmap */
319d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
3200df4fabeSYong Wu 	return 0;
3210df4fabeSYong Wu }
3220df4fabeSYong Wu 
3230df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
3240df4fabeSYong Wu {
3250df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
3260df4fabeSYong Wu 
3270df4fabeSYong Wu 	if (type != IOMMU_DOMAIN_DMA)
3280df4fabeSYong Wu 		return NULL;
3290df4fabeSYong Wu 
3300df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
3310df4fabeSYong Wu 	if (!dom)
3320df4fabeSYong Wu 		return NULL;
3330df4fabeSYong Wu 
3344b00f5acSYong Wu 	if (iommu_get_dma_cookie(&dom->domain))
3354b00f5acSYong Wu 		goto  free_dom;
3364b00f5acSYong Wu 
3374b00f5acSYong Wu 	if (mtk_iommu_domain_finalise(dom))
3384b00f5acSYong Wu 		goto  put_dma_cookie;
3390df4fabeSYong Wu 
3400df4fabeSYong Wu 	dom->domain.geometry.aperture_start = 0;
3410df4fabeSYong Wu 	dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
3420df4fabeSYong Wu 	dom->domain.geometry.force_aperture = true;
3430df4fabeSYong Wu 
3440df4fabeSYong Wu 	return &dom->domain;
3454b00f5acSYong Wu 
3464b00f5acSYong Wu put_dma_cookie:
3474b00f5acSYong Wu 	iommu_put_dma_cookie(&dom->domain);
3484b00f5acSYong Wu free_dom:
3494b00f5acSYong Wu 	kfree(dom);
3504b00f5acSYong Wu 	return NULL;
3510df4fabeSYong Wu }
3520df4fabeSYong Wu 
3530df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
3540df4fabeSYong Wu {
3554b00f5acSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
3564b00f5acSYong Wu 
3574b00f5acSYong Wu 	free_io_pgtable_ops(dom->iop);
3580df4fabeSYong Wu 	iommu_put_dma_cookie(domain);
3590df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
3600df4fabeSYong Wu }
3610df4fabeSYong Wu 
3620df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
3630df4fabeSYong Wu 				   struct device *dev)
3640df4fabeSYong Wu {
3653524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
3660df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
3670df4fabeSYong Wu 
3684b00f5acSYong Wu 	if (!data)
3690df4fabeSYong Wu 		return -ENODEV;
3700df4fabeSYong Wu 
3714b00f5acSYong Wu 	/* Update the pgtable base address register of the M4U HW */
3720df4fabeSYong Wu 	if (!data->m4u_dom) {
3730df4fabeSYong Wu 		data->m4u_dom = dom;
374d1e5f26fSRobin Murphy 		writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
3754b00f5acSYong Wu 		       data->base + REG_MMU_PT_BASE_ADDR);
3760df4fabeSYong Wu 	}
3770df4fabeSYong Wu 
3784b00f5acSYong Wu 	mtk_iommu_config(data, dev, true);
3790df4fabeSYong Wu 	return 0;
3800df4fabeSYong Wu }
3810df4fabeSYong Wu 
3820df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
3830df4fabeSYong Wu 				    struct device *dev)
3840df4fabeSYong Wu {
3853524b559SJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
3860df4fabeSYong Wu 
38758f0d1d5SRobin Murphy 	if (!data)
3880df4fabeSYong Wu 		return;
3890df4fabeSYong Wu 
3900df4fabeSYong Wu 	mtk_iommu_config(data, dev, false);
3910df4fabeSYong Wu }
3920df4fabeSYong Wu 
3930df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
394781ca2deSTom Murphy 			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
3950df4fabeSYong Wu {
3960df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
397b4dad40eSYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
3980df4fabeSYong Wu 
399b4dad40eSYong Wu 	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
400b4dad40eSYong Wu 	if (data->enable_4GB)
401b4dad40eSYong Wu 		paddr |= BIT_ULL(32);
402b4dad40eSYong Wu 
40360829b4dSYong Wu 	/* Synchronize with the tlb_lock */
404f34ce7a7SBaolin Wang 	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
4050df4fabeSYong Wu }
4060df4fabeSYong Wu 
4070df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
40856f8af5eSWill Deacon 			      unsigned long iova, size_t size,
40956f8af5eSWill Deacon 			      struct iommu_iotlb_gather *gather)
4100df4fabeSYong Wu {
4110df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
412f21ae3b1SYong Wu 	unsigned long end = iova + size - 1;
4130df4fabeSYong Wu 
414f21ae3b1SYong Wu 	if (gather->start > iova)
415f21ae3b1SYong Wu 		gather->start = iova;
416f21ae3b1SYong Wu 	if (gather->end < end)
417f21ae3b1SYong Wu 		gather->end = end;
41860829b4dSYong Wu 	return dom->iop->unmap(dom->iop, iova, size, gather);
4190df4fabeSYong Wu }
4200df4fabeSYong Wu 
42156f8af5eSWill Deacon static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
42256f8af5eSWill Deacon {
4232009122fSYong Wu 	mtk_iommu_tlb_flush_all(mtk_iommu_get_m4u_data());
42456f8af5eSWill Deacon }
42556f8af5eSWill Deacon 
42656f8af5eSWill Deacon static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
42756f8af5eSWill Deacon 				 struct iommu_iotlb_gather *gather)
4284d689b61SRobin Murphy {
429da3cc91bSYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
430862c3715SYong Wu 	size_t length = gather->end - gather->start + 1;
431da3cc91bSYong Wu 
4321f4fd624SYong Wu 	mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize,
43367caf7e2SYong Wu 				       data);
4344d689b61SRobin Murphy }
4354d689b61SRobin Murphy 
43620143451SYong Wu static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
43720143451SYong Wu 			       size_t size)
43820143451SYong Wu {
43920143451SYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
44020143451SYong Wu 
44120143451SYong Wu 	mtk_iommu_tlb_flush_range_sync(iova, size, size, data);
44220143451SYong Wu }
44320143451SYong Wu 
4440df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
4450df4fabeSYong Wu 					  dma_addr_t iova)
4460df4fabeSYong Wu {
4470df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
44830e2fccfSYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
4490df4fabeSYong Wu 	phys_addr_t pa;
4500df4fabeSYong Wu 
4510df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
452b4dad40eSYong Wu 	if (data->enable_4GB && pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
453b4dad40eSYong Wu 		pa &= ~BIT_ULL(32);
45430e2fccfSYong Wu 
4550df4fabeSYong Wu 	return pa;
4560df4fabeSYong Wu }
4570df4fabeSYong Wu 
45880e4592aSJoerg Roedel static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
4590df4fabeSYong Wu {
460a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
461b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
4620df4fabeSYong Wu 
463a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
46480e4592aSJoerg Roedel 		return ERR_PTR(-ENODEV); /* Not a iommu client device */
4650df4fabeSYong Wu 
4663524b559SJoerg Roedel 	data = dev_iommu_priv_get(dev);
467b16c0170SJoerg Roedel 
46880e4592aSJoerg Roedel 	return &data->iommu;
4690df4fabeSYong Wu }
4700df4fabeSYong Wu 
47180e4592aSJoerg Roedel static void mtk_iommu_release_device(struct device *dev)
4720df4fabeSYong Wu {
473a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
474b16c0170SJoerg Roedel 
475a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
4760df4fabeSYong Wu 		return;
4770df4fabeSYong Wu 
47858f0d1d5SRobin Murphy 	iommu_fwspec_free(dev);
4790df4fabeSYong Wu }
4800df4fabeSYong Wu 
4810df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
4820df4fabeSYong Wu {
4837c3a2ec0SYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
4840df4fabeSYong Wu 
48558f0d1d5SRobin Murphy 	if (!data)
4860df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
4870df4fabeSYong Wu 
4880df4fabeSYong Wu 	/* All the client devices are in the same m4u iommu-group */
4890df4fabeSYong Wu 	if (!data->m4u_group) {
4900df4fabeSYong Wu 		data->m4u_group = iommu_group_alloc();
4910df4fabeSYong Wu 		if (IS_ERR(data->m4u_group))
4920df4fabeSYong Wu 			dev_err(dev, "Failed to allocate M4U IOMMU group\n");
4933a8d40b6SRobin Murphy 	} else {
4943a8d40b6SRobin Murphy 		iommu_group_ref_get(data->m4u_group);
4950df4fabeSYong Wu 	}
4960df4fabeSYong Wu 	return data->m4u_group;
4970df4fabeSYong Wu }
4980df4fabeSYong Wu 
4990df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
5000df4fabeSYong Wu {
5010df4fabeSYong Wu 	struct platform_device *m4updev;
5020df4fabeSYong Wu 
5030df4fabeSYong Wu 	if (args->args_count != 1) {
5040df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
5050df4fabeSYong Wu 			args->args_count);
5060df4fabeSYong Wu 		return -EINVAL;
5070df4fabeSYong Wu 	}
5080df4fabeSYong Wu 
5093524b559SJoerg Roedel 	if (!dev_iommu_priv_get(dev)) {
5100df4fabeSYong Wu 		/* Get the m4u device */
5110df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
5120df4fabeSYong Wu 		if (WARN_ON(!m4updev))
5130df4fabeSYong Wu 			return -EINVAL;
5140df4fabeSYong Wu 
5153524b559SJoerg Roedel 		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
5160df4fabeSYong Wu 	}
5170df4fabeSYong Wu 
51858f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
5190df4fabeSYong Wu }
5200df4fabeSYong Wu 
521b65f5016SArvind Yadav static const struct iommu_ops mtk_iommu_ops = {
5220df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
5230df4fabeSYong Wu 	.domain_free	= mtk_iommu_domain_free,
5240df4fabeSYong Wu 	.attach_dev	= mtk_iommu_attach_device,
5250df4fabeSYong Wu 	.detach_dev	= mtk_iommu_detach_device,
5260df4fabeSYong Wu 	.map		= mtk_iommu_map,
5270df4fabeSYong Wu 	.unmap		= mtk_iommu_unmap,
52856f8af5eSWill Deacon 	.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
5294d689b61SRobin Murphy 	.iotlb_sync	= mtk_iommu_iotlb_sync,
53020143451SYong Wu 	.iotlb_sync_map	= mtk_iommu_sync_map,
5310df4fabeSYong Wu 	.iova_to_phys	= mtk_iommu_iova_to_phys,
53280e4592aSJoerg Roedel 	.probe_device	= mtk_iommu_probe_device,
53380e4592aSJoerg Roedel 	.release_device	= mtk_iommu_release_device,
5340df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
5350df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
5360df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
5370df4fabeSYong Wu };
5380df4fabeSYong Wu 
5390df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
5400df4fabeSYong Wu {
5410df4fabeSYong Wu 	u32 regval;
5420df4fabeSYong Wu 	int ret;
5430df4fabeSYong Wu 
5440df4fabeSYong Wu 	ret = clk_prepare_enable(data->bclk);
5450df4fabeSYong Wu 	if (ret) {
5460df4fabeSYong Wu 		dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
5470df4fabeSYong Wu 		return ret;
5480df4fabeSYong Wu 	}
5490df4fabeSYong Wu 
55086444413SChao Hao 	if (data->plat_data->m4u_plat == M4U_MT8173) {
551acb3c92aSYong Wu 		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
552acb3c92aSYong Wu 			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
55386444413SChao Hao 	} else {
55486444413SChao Hao 		regval = readl_relaxed(data->base + REG_MMU_CTRL_REG);
55586444413SChao Hao 		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
55686444413SChao Hao 	}
5570df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
5580df4fabeSYong Wu 
5590df4fabeSYong Wu 	regval = F_L2_MULIT_HIT_EN |
5600df4fabeSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
5610df4fabeSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
5620df4fabeSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
5630df4fabeSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
5640df4fabeSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
5650df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
5660df4fabeSYong Wu 
5670df4fabeSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
5680df4fabeSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
5690df4fabeSYong Wu 		F_INT_INVALID_PA_FAULT |
5700df4fabeSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
5710df4fabeSYong Wu 		F_INT_TLB_MISS_FAULT |
5720df4fabeSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
5730df4fabeSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
5740df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
5750df4fabeSYong Wu 
576d1b5ef00SFabien Parent 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
57770ca608bSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
57870ca608bSYong Wu 	else
57970ca608bSYong Wu 		regval = lower_32_bits(data->protect_base) |
58070ca608bSYong Wu 			 upper_32_bits(data->protect_base);
58170ca608bSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
58270ca608bSYong Wu 
5836b717796SChao Hao 	if (data->enable_4GB &&
5846b717796SChao Hao 	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
58530e2fccfSYong Wu 		/*
58630e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
58730e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
58830e2fccfSYong Wu 		 */
58930e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
59030e2fccfSYong Wu 		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
59130e2fccfSYong Wu 	}
5920df4fabeSYong Wu 	writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
59335c1b48dSChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
59435c1b48dSChao Hao 		/* write command throttling mode */
59535c1b48dSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL);
59635c1b48dSChao Hao 		regval &= ~F_MMU_WR_THROT_DIS_MASK;
59735c1b48dSChao Hao 		writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL);
59835c1b48dSChao Hao 	}
599e6dec923SYong Wu 
6006b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
60175eed350SChao Hao 		/* The register is called STANDARD_AXI_MODE in this case */
6024bb2bf4cSChao Hao 		regval = 0;
6034bb2bf4cSChao Hao 	} else {
6044bb2bf4cSChao Hao 		regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
6054bb2bf4cSChao Hao 		regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
6064bb2bf4cSChao Hao 		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
6074bb2bf4cSChao Hao 			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
60875eed350SChao Hao 	}
6094bb2bf4cSChao Hao 	writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
6100df4fabeSYong Wu 
6110df4fabeSYong Wu 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
6120df4fabeSYong Wu 			     dev_name(data->dev), (void *)data)) {
6130df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
6140df4fabeSYong Wu 		clk_disable_unprepare(data->bclk);
6150df4fabeSYong Wu 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
6160df4fabeSYong Wu 		return -ENODEV;
6170df4fabeSYong Wu 	}
6180df4fabeSYong Wu 
6190df4fabeSYong Wu 	return 0;
6200df4fabeSYong Wu }
6210df4fabeSYong Wu 
6220df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
6230df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
6240df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
6250df4fabeSYong Wu };
6260df4fabeSYong Wu 
6270df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
6280df4fabeSYong Wu {
6290df4fabeSYong Wu 	struct mtk_iommu_data   *data;
6300df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
6310df4fabeSYong Wu 	struct resource         *res;
632b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
6330df4fabeSYong Wu 	struct component_match  *match = NULL;
634c2c59456SMiles Chen 	struct regmap		*infracfg;
6350df4fabeSYong Wu 	void                    *protect;
6360b6c0ad3SAndrzej Hajda 	int                     i, larb_nr, ret;
637c2c59456SMiles Chen 	u32			val;
638c2c59456SMiles Chen 	char                    *p;
6390df4fabeSYong Wu 
6400df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
6410df4fabeSYong Wu 	if (!data)
6420df4fabeSYong Wu 		return -ENOMEM;
6430df4fabeSYong Wu 	data->dev = dev;
644cecdce9dSYong Wu 	data->plat_data = of_device_get_match_data(dev);
6450df4fabeSYong Wu 
6460df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
6470df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
6480df4fabeSYong Wu 	if (!protect)
6490df4fabeSYong Wu 		return -ENOMEM;
6500df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
6510df4fabeSYong Wu 
652c2c59456SMiles Chen 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
653c2c59456SMiles Chen 		switch (data->plat_data->m4u_plat) {
654c2c59456SMiles Chen 		case M4U_MT2712:
655c2c59456SMiles Chen 			p = "mediatek,mt2712-infracfg";
656c2c59456SMiles Chen 			break;
657c2c59456SMiles Chen 		case M4U_MT8173:
658c2c59456SMiles Chen 			p = "mediatek,mt8173-infracfg";
659c2c59456SMiles Chen 			break;
660c2c59456SMiles Chen 		default:
661c2c59456SMiles Chen 			p = NULL;
662c2c59456SMiles Chen 		}
663c2c59456SMiles Chen 
664c2c59456SMiles Chen 		infracfg = syscon_regmap_lookup_by_compatible(p);
665c2c59456SMiles Chen 
666c2c59456SMiles Chen 		if (IS_ERR(infracfg))
667c2c59456SMiles Chen 			return PTR_ERR(infracfg);
668c2c59456SMiles Chen 
669c2c59456SMiles Chen 		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
670c2c59456SMiles Chen 		if (ret)
671c2c59456SMiles Chen 			return ret;
672c2c59456SMiles Chen 		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
673c2c59456SMiles Chen 	}
67401e23c93SYong Wu 
6750df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6760df4fabeSYong Wu 	data->base = devm_ioremap_resource(dev, res);
6770df4fabeSYong Wu 	if (IS_ERR(data->base))
6780df4fabeSYong Wu 		return PTR_ERR(data->base);
679b16c0170SJoerg Roedel 	ioaddr = res->start;
6800df4fabeSYong Wu 
6810df4fabeSYong Wu 	data->irq = platform_get_irq(pdev, 0);
6820df4fabeSYong Wu 	if (data->irq < 0)
6830df4fabeSYong Wu 		return data->irq;
6840df4fabeSYong Wu 
6856b717796SChao Hao 	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
6860df4fabeSYong Wu 		data->bclk = devm_clk_get(dev, "bclk");
6870df4fabeSYong Wu 		if (IS_ERR(data->bclk))
6880df4fabeSYong Wu 			return PTR_ERR(data->bclk);
6892aa4c259SYong Wu 	}
6900df4fabeSYong Wu 
6910df4fabeSYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node,
6920df4fabeSYong Wu 					     "mediatek,larbs", NULL);
6930df4fabeSYong Wu 	if (larb_nr < 0)
6940df4fabeSYong Wu 		return larb_nr;
6950df4fabeSYong Wu 
6960df4fabeSYong Wu 	for (i = 0; i < larb_nr; i++) {
6970df4fabeSYong Wu 		struct device_node *larbnode;
6980df4fabeSYong Wu 		struct platform_device *plarbdev;
699e6dec923SYong Wu 		u32 id;
7000df4fabeSYong Wu 
7010df4fabeSYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
7020df4fabeSYong Wu 		if (!larbnode)
7030df4fabeSYong Wu 			return -EINVAL;
7040df4fabeSYong Wu 
7051eb8e4e2SWen Yang 		if (!of_device_is_available(larbnode)) {
7061eb8e4e2SWen Yang 			of_node_put(larbnode);
7070df4fabeSYong Wu 			continue;
7081eb8e4e2SWen Yang 		}
7090df4fabeSYong Wu 
710e6dec923SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
711e6dec923SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
712e6dec923SYong Wu 			id = i;
713e6dec923SYong Wu 
7140df4fabeSYong Wu 		plarbdev = of_find_device_by_node(larbnode);
7151eb8e4e2SWen Yang 		if (!plarbdev) {
7161eb8e4e2SWen Yang 			of_node_put(larbnode);
7170df4fabeSYong Wu 			return -EPROBE_DEFER;
7181eb8e4e2SWen Yang 		}
7191ee9feb2SYong Wu 		data->larb_imu[id].dev = &plarbdev->dev;
7200df4fabeSYong Wu 
72100c7c81fSRussell King 		component_match_add_release(dev, &match, release_of,
72200c7c81fSRussell King 					    compare_of, larbnode);
7230df4fabeSYong Wu 	}
7240df4fabeSYong Wu 
7250df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
7260df4fabeSYong Wu 
7270df4fabeSYong Wu 	ret = mtk_iommu_hw_init(data);
7280df4fabeSYong Wu 	if (ret)
7290df4fabeSYong Wu 		return ret;
7300df4fabeSYong Wu 
731b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
732b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
733b16c0170SJoerg Roedel 	if (ret)
734b16c0170SJoerg Roedel 		return ret;
735b16c0170SJoerg Roedel 
736b16c0170SJoerg Roedel 	iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
737b16c0170SJoerg Roedel 	iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
738b16c0170SJoerg Roedel 
739b16c0170SJoerg Roedel 	ret = iommu_device_register(&data->iommu);
740b16c0170SJoerg Roedel 	if (ret)
741b16c0170SJoerg Roedel 		return ret;
742b16c0170SJoerg Roedel 
743da3cc91bSYong Wu 	spin_lock_init(&data->tlb_lock);
7447c3a2ec0SYong Wu 	list_add_tail(&data->list, &m4ulist);
7457c3a2ec0SYong Wu 
7460df4fabeSYong Wu 	if (!iommu_present(&platform_bus_type))
7470df4fabeSYong Wu 		bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
7480df4fabeSYong Wu 
7490df4fabeSYong Wu 	return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
7500df4fabeSYong Wu }
7510df4fabeSYong Wu 
7520df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
7530df4fabeSYong Wu {
7540df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
7550df4fabeSYong Wu 
756b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
757b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
758b16c0170SJoerg Roedel 
7590df4fabeSYong Wu 	if (iommu_present(&platform_bus_type))
7600df4fabeSYong Wu 		bus_set_iommu(&platform_bus_type, NULL);
7610df4fabeSYong Wu 
7620df4fabeSYong Wu 	clk_disable_unprepare(data->bclk);
7630df4fabeSYong Wu 	devm_free_irq(&pdev->dev, data->irq, data);
7640df4fabeSYong Wu 	component_master_del(&pdev->dev, &mtk_iommu_com_ops);
7650df4fabeSYong Wu 	return 0;
7660df4fabeSYong Wu }
7670df4fabeSYong Wu 
768fd99f796SArnd Bergmann static int __maybe_unused mtk_iommu_suspend(struct device *dev)
7690df4fabeSYong Wu {
7700df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
7710df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
7720df4fabeSYong Wu 	void __iomem *base = data->base;
7730df4fabeSYong Wu 
77435c1b48dSChao Hao 	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
77575eed350SChao Hao 	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
7760df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
7770df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
7780df4fabeSYong Wu 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
7790df4fabeSYong Wu 	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
78070ca608bSYong Wu 	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
781b9475b34SYong Wu 	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
7826254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
7830df4fabeSYong Wu 	return 0;
7840df4fabeSYong Wu }
7850df4fabeSYong Wu 
786fd99f796SArnd Bergmann static int __maybe_unused mtk_iommu_resume(struct device *dev)
7870df4fabeSYong Wu {
7880df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
7890df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
790907ba6a1SYong Wu 	struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
7910df4fabeSYong Wu 	void __iomem *base = data->base;
7926254b64fSYong Wu 	int ret;
7930df4fabeSYong Wu 
7946254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
7956254b64fSYong Wu 	if (ret) {
7966254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
7976254b64fSYong Wu 		return ret;
7986254b64fSYong Wu 	}
79935c1b48dSChao Hao 	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
80075eed350SChao Hao 	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
8010df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
8020df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
8030df4fabeSYong Wu 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
8040df4fabeSYong Wu 	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
80570ca608bSYong Wu 	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
806b9475b34SYong Wu 	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
807907ba6a1SYong Wu 	if (m4u_dom)
808d1e5f26fSRobin Murphy 		writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
809e6dec923SYong Wu 		       base + REG_MMU_PT_BASE_ADDR);
8100df4fabeSYong Wu 	return 0;
8110df4fabeSYong Wu }
8120df4fabeSYong Wu 
813e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
8146254b64fSYong Wu 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
8150df4fabeSYong Wu };
8160df4fabeSYong Wu 
817cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt2712_data = {
818cecdce9dSYong Wu 	.m4u_plat     = M4U_MT2712,
8196b717796SChao Hao 	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG,
820b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
82137276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
822cecdce9dSYong Wu };
823cecdce9dSYong Wu 
824068c86e9SChao Hao static const struct mtk_iommu_plat_data mt6779_data = {
825068c86e9SChao Hao 	.m4u_plat      = M4U_MT6779,
826068c86e9SChao Hao 	.flags         = HAS_SUB_COMM | OUT_ORDER_WR_EN | WR_THROT_EN,
827068c86e9SChao Hao 	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
828068c86e9SChao Hao 	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
829cecdce9dSYong Wu };
830cecdce9dSYong Wu 
8313c213562SFabien Parent static const struct mtk_iommu_plat_data mt8167_data = {
8323c213562SFabien Parent 	.m4u_plat     = M4U_MT8167,
8333c213562SFabien Parent 	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR,
8343c213562SFabien Parent 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
8353c213562SFabien Parent 	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
8363c213562SFabien Parent };
8373c213562SFabien Parent 
838cecdce9dSYong Wu static const struct mtk_iommu_plat_data mt8173_data = {
839cecdce9dSYong Wu 	.m4u_plat     = M4U_MT8173,
840d1b5ef00SFabien Parent 	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
841d1b5ef00SFabien Parent 			HAS_LEGACY_IVRP_PADDR,
842b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
84337276e00SChao Hao 	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
844cecdce9dSYong Wu };
845cecdce9dSYong Wu 
846907ba6a1SYong Wu static const struct mtk_iommu_plat_data mt8183_data = {
847907ba6a1SYong Wu 	.m4u_plat     = M4U_MT8183,
8486b717796SChao Hao 	.flags        = RESET_AXI,
849b053bc71SChao Hao 	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
85037276e00SChao Hao 	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
851907ba6a1SYong Wu };
852907ba6a1SYong Wu 
8530df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
854cecdce9dSYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
855068c86e9SChao Hao 	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
8563c213562SFabien Parent 	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
857cecdce9dSYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
858907ba6a1SYong Wu 	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
8590df4fabeSYong Wu 	{}
8600df4fabeSYong Wu };
8610df4fabeSYong Wu 
8620df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
8630df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
8640df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
8650df4fabeSYong Wu 	.driver	= {
8660df4fabeSYong Wu 		.name = "mtk-iommu",
867f53dd978SKrzysztof Kozlowski 		.of_match_table = mtk_iommu_of_ids,
8680df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
8690df4fabeSYong Wu 	}
8700df4fabeSYong Wu };
8710df4fabeSYong Wu 
872e6dec923SYong Wu static int __init mtk_iommu_init(void)
8730df4fabeSYong Wu {
8740df4fabeSYong Wu 	int ret;
8750df4fabeSYong Wu 
8760df4fabeSYong Wu 	ret = platform_driver_register(&mtk_iommu_driver);
877e6dec923SYong Wu 	if (ret != 0)
878e6dec923SYong Wu 		pr_err("Failed to register MTK IOMMU driver\n");
879e6dec923SYong Wu 
8800df4fabeSYong Wu 	return ret;
8810df4fabeSYong Wu }
8820df4fabeSYong Wu 
883e6dec923SYong Wu subsys_initcall(mtk_iommu_init)
884