xref: /linux/drivers/iommu/mtk_iommu.c (revision a9bf2eec5a6fc01a0a5250eaf0bf61dfd382a78a)
10df4fabeSYong Wu /*
20df4fabeSYong Wu  * Copyright (c) 2015-2016 MediaTek Inc.
30df4fabeSYong Wu  * Author: Yong Wu <yong.wu@mediatek.com>
40df4fabeSYong Wu  *
50df4fabeSYong Wu  * This program is free software; you can redistribute it and/or modify
60df4fabeSYong Wu  * it under the terms of the GNU General Public License version 2 as
70df4fabeSYong Wu  * published by the Free Software Foundation.
80df4fabeSYong Wu  *
90df4fabeSYong Wu  * This program is distributed in the hope that it will be useful,
100df4fabeSYong Wu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
110df4fabeSYong Wu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
120df4fabeSYong Wu  * GNU General Public License for more details.
130df4fabeSYong Wu  */
1457c8a661SMike Rapoport #include <linux/memblock.h>
150df4fabeSYong Wu #include <linux/bug.h>
160df4fabeSYong Wu #include <linux/clk.h>
170df4fabeSYong Wu #include <linux/component.h>
180df4fabeSYong Wu #include <linux/device.h>
190df4fabeSYong Wu #include <linux/dma-iommu.h>
200df4fabeSYong Wu #include <linux/err.h>
210df4fabeSYong Wu #include <linux/interrupt.h>
220df4fabeSYong Wu #include <linux/io.h>
230df4fabeSYong Wu #include <linux/iommu.h>
240df4fabeSYong Wu #include <linux/iopoll.h>
250df4fabeSYong Wu #include <linux/list.h>
260df4fabeSYong Wu #include <linux/of_address.h>
270df4fabeSYong Wu #include <linux/of_iommu.h>
280df4fabeSYong Wu #include <linux/of_irq.h>
290df4fabeSYong Wu #include <linux/of_platform.h>
300df4fabeSYong Wu #include <linux/platform_device.h>
310df4fabeSYong Wu #include <linux/slab.h>
320df4fabeSYong Wu #include <linux/spinlock.h>
330df4fabeSYong Wu #include <asm/barrier.h>
340df4fabeSYong Wu #include <soc/mediatek/smi.h>
350df4fabeSYong Wu 
369ca340c9SHonghui Zhang #include "mtk_iommu.h"
370df4fabeSYong Wu 
380df4fabeSYong Wu #define REG_MMU_PT_BASE_ADDR			0x000
390df4fabeSYong Wu 
400df4fabeSYong Wu #define REG_MMU_INVALIDATE			0x020
410df4fabeSYong Wu #define F_ALL_INVLD				0x2
420df4fabeSYong Wu #define F_MMU_INV_RANGE				0x1
430df4fabeSYong Wu 
440df4fabeSYong Wu #define REG_MMU_INVLD_START_A			0x024
450df4fabeSYong Wu #define REG_MMU_INVLD_END_A			0x028
460df4fabeSYong Wu 
470df4fabeSYong Wu #define REG_MMU_INV_SEL				0x038
480df4fabeSYong Wu #define F_INVLD_EN0				BIT(0)
490df4fabeSYong Wu #define F_INVLD_EN1				BIT(1)
500df4fabeSYong Wu 
510df4fabeSYong Wu #define REG_MMU_STANDARD_AXI_MODE		0x048
520df4fabeSYong Wu #define REG_MMU_DCM_DIS				0x050
530df4fabeSYong Wu 
540df4fabeSYong Wu #define REG_MMU_CTRL_REG			0x110
550df4fabeSYong Wu #define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
56e6dec923SYong Wu #define F_MMU_TF_PROTECT_SEL_SHIFT(data) \
57e6dec923SYong Wu 	((data)->m4u_plat == M4U_MT2712 ? 4 : 5)
58e6dec923SYong Wu /* It's named by F_MMU_TF_PROT_SEL in mt2712. */
59e6dec923SYong Wu #define F_MMU_TF_PROTECT_SEL(prot, data) \
60e6dec923SYong Wu 	(((prot) & 0x3) << F_MMU_TF_PROTECT_SEL_SHIFT(data))
610df4fabeSYong Wu 
620df4fabeSYong Wu #define REG_MMU_IVRP_PADDR			0x114
6370ca608bSYong Wu 
6430e2fccfSYong Wu #define REG_MMU_VLD_PA_RNG			0x118
6530e2fccfSYong Wu #define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
660df4fabeSYong Wu 
670df4fabeSYong Wu #define REG_MMU_INT_CONTROL0			0x120
680df4fabeSYong Wu #define F_L2_MULIT_HIT_EN			BIT(0)
690df4fabeSYong Wu #define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
700df4fabeSYong Wu #define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
710df4fabeSYong Wu #define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
720df4fabeSYong Wu #define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
730df4fabeSYong Wu #define F_MISS_FIFO_ERR_INT_EN			BIT(6)
740df4fabeSYong Wu #define F_INT_CLR_BIT				BIT(12)
750df4fabeSYong Wu 
760df4fabeSYong Wu #define REG_MMU_INT_MAIN_CONTROL		0x124
770df4fabeSYong Wu #define F_INT_TRANSLATION_FAULT			BIT(0)
780df4fabeSYong Wu #define F_INT_MAIN_MULTI_HIT_FAULT		BIT(1)
790df4fabeSYong Wu #define F_INT_INVALID_PA_FAULT			BIT(2)
800df4fabeSYong Wu #define F_INT_ENTRY_REPLACEMENT_FAULT		BIT(3)
810df4fabeSYong Wu #define F_INT_TLB_MISS_FAULT			BIT(4)
820df4fabeSYong Wu #define F_INT_MISS_TRANSACTION_FIFO_FAULT	BIT(5)
830df4fabeSYong Wu #define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	BIT(6)
840df4fabeSYong Wu 
850df4fabeSYong Wu #define REG_MMU_CPE_DONE			0x12C
860df4fabeSYong Wu 
870df4fabeSYong Wu #define REG_MMU_FAULT_ST1			0x134
880df4fabeSYong Wu 
890df4fabeSYong Wu #define REG_MMU_FAULT_VA			0x13c
900df4fabeSYong Wu #define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
910df4fabeSYong Wu #define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)
920df4fabeSYong Wu 
930df4fabeSYong Wu #define REG_MMU_INVLD_PA			0x140
940df4fabeSYong Wu #define REG_MMU_INT_ID				0x150
950df4fabeSYong Wu #define F_MMU0_INT_ID_LARB_ID(a)		(((a) >> 7) & 0x7)
960df4fabeSYong Wu #define F_MMU0_INT_ID_PORT_ID(a)		(((a) >> 2) & 0x1f)
970df4fabeSYong Wu 
980df4fabeSYong Wu #define MTK_PROTECT_PA_ALIGN			128
990df4fabeSYong Wu 
100a9467d95SYong Wu /*
101a9467d95SYong Wu  * Get the local arbiter ID and the portid within the larb arbiter
102a9467d95SYong Wu  * from mtk_m4u_id which is defined by MTK_M4U_ID.
103a9467d95SYong Wu  */
104e6dec923SYong Wu #define MTK_M4U_TO_LARB(id)		(((id) >> 5) & 0xf)
105a9467d95SYong Wu #define MTK_M4U_TO_PORT(id)		((id) & 0x1f)
106a9467d95SYong Wu 
1070df4fabeSYong Wu struct mtk_iommu_domain {
1080df4fabeSYong Wu 	spinlock_t			pgtlock; /* lock for page table */
1090df4fabeSYong Wu 
1100df4fabeSYong Wu 	struct io_pgtable_cfg		cfg;
1110df4fabeSYong Wu 	struct io_pgtable_ops		*iop;
1120df4fabeSYong Wu 
1130df4fabeSYong Wu 	struct iommu_domain		domain;
1140df4fabeSYong Wu };
1150df4fabeSYong Wu 
1160df4fabeSYong Wu static struct iommu_ops mtk_iommu_ops;
1170df4fabeSYong Wu 
1187c3a2ec0SYong Wu static LIST_HEAD(m4ulist);	/* List all the M4U HWs */
1197c3a2ec0SYong Wu 
1207c3a2ec0SYong Wu #define for_each_m4u(data)	list_for_each_entry(data, &m4ulist, list)
1217c3a2ec0SYong Wu 
1227c3a2ec0SYong Wu /*
1237c3a2ec0SYong Wu  * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
1247c3a2ec0SYong Wu  * for the performance.
1257c3a2ec0SYong Wu  *
1267c3a2ec0SYong Wu  * Here always return the mtk_iommu_data of the first probed M4U where the
1277c3a2ec0SYong Wu  * iommu domain information is recorded.
1287c3a2ec0SYong Wu  */
1297c3a2ec0SYong Wu static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
1307c3a2ec0SYong Wu {
1317c3a2ec0SYong Wu 	struct mtk_iommu_data *data;
1327c3a2ec0SYong Wu 
1337c3a2ec0SYong Wu 	for_each_m4u(data)
1347c3a2ec0SYong Wu 		return data;
1357c3a2ec0SYong Wu 
1367c3a2ec0SYong Wu 	return NULL;
1377c3a2ec0SYong Wu }
1387c3a2ec0SYong Wu 
1390df4fabeSYong Wu static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
1400df4fabeSYong Wu {
1410df4fabeSYong Wu 	return container_of(dom, struct mtk_iommu_domain, domain);
1420df4fabeSYong Wu }
1430df4fabeSYong Wu 
1440df4fabeSYong Wu static void mtk_iommu_tlb_flush_all(void *cookie)
1450df4fabeSYong Wu {
1460df4fabeSYong Wu 	struct mtk_iommu_data *data = cookie;
1470df4fabeSYong Wu 
1487c3a2ec0SYong Wu 	for_each_m4u(data) {
1497c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
1507c3a2ec0SYong Wu 			       data->base + REG_MMU_INV_SEL);
1510df4fabeSYong Wu 		writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
1520df4fabeSYong Wu 		wmb(); /* Make sure the tlb flush all done */
1530df4fabeSYong Wu 	}
1547c3a2ec0SYong Wu }
1550df4fabeSYong Wu 
1560df4fabeSYong Wu static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size,
1570df4fabeSYong Wu 					   size_t granule, bool leaf,
1580df4fabeSYong Wu 					   void *cookie)
1590df4fabeSYong Wu {
1600df4fabeSYong Wu 	struct mtk_iommu_data *data = cookie;
1610df4fabeSYong Wu 
1627c3a2ec0SYong Wu 	for_each_m4u(data) {
1637c3a2ec0SYong Wu 		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
1647c3a2ec0SYong Wu 			       data->base + REG_MMU_INV_SEL);
1650df4fabeSYong Wu 
1660df4fabeSYong Wu 		writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
1677c3a2ec0SYong Wu 		writel_relaxed(iova + size - 1,
1687c3a2ec0SYong Wu 			       data->base + REG_MMU_INVLD_END_A);
1697c3a2ec0SYong Wu 		writel_relaxed(F_MMU_INV_RANGE,
1707c3a2ec0SYong Wu 			       data->base + REG_MMU_INVALIDATE);
17198a8f63eSRobin Murphy 		data->tlb_flush_active = true;
1720df4fabeSYong Wu 	}
1737c3a2ec0SYong Wu }
1740df4fabeSYong Wu 
1750df4fabeSYong Wu static void mtk_iommu_tlb_sync(void *cookie)
1760df4fabeSYong Wu {
1770df4fabeSYong Wu 	struct mtk_iommu_data *data = cookie;
1780df4fabeSYong Wu 	int ret;
1790df4fabeSYong Wu 	u32 tmp;
1800df4fabeSYong Wu 
1817c3a2ec0SYong Wu 	for_each_m4u(data) {
18298a8f63eSRobin Murphy 		/* Avoid timing out if there's nothing to wait for */
18398a8f63eSRobin Murphy 		if (!data->tlb_flush_active)
18498a8f63eSRobin Murphy 			return;
18598a8f63eSRobin Murphy 
1867c3a2ec0SYong Wu 		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
1877c3a2ec0SYong Wu 						tmp, tmp != 0, 10, 100000);
1880df4fabeSYong Wu 		if (ret) {
1890df4fabeSYong Wu 			dev_warn(data->dev,
1900df4fabeSYong Wu 				 "Partial TLB flush timed out, falling back to full flush\n");
1910df4fabeSYong Wu 			mtk_iommu_tlb_flush_all(cookie);
1920df4fabeSYong Wu 		}
1930df4fabeSYong Wu 		/* Clear the CPE status */
1940df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
19598a8f63eSRobin Murphy 		data->tlb_flush_active = false;
1960df4fabeSYong Wu 	}
1977c3a2ec0SYong Wu }
1980df4fabeSYong Wu 
1990df4fabeSYong Wu static const struct iommu_gather_ops mtk_iommu_gather_ops = {
2000df4fabeSYong Wu 	.tlb_flush_all = mtk_iommu_tlb_flush_all,
2010df4fabeSYong Wu 	.tlb_add_flush = mtk_iommu_tlb_add_flush_nosync,
2020df4fabeSYong Wu 	.tlb_sync = mtk_iommu_tlb_sync,
2030df4fabeSYong Wu };
2040df4fabeSYong Wu 
2050df4fabeSYong Wu static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
2060df4fabeSYong Wu {
2070df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_id;
2080df4fabeSYong Wu 	struct mtk_iommu_domain *dom = data->m4u_dom;
2090df4fabeSYong Wu 	u32 int_state, regval, fault_iova, fault_pa;
2100df4fabeSYong Wu 	unsigned int fault_larb, fault_port;
2110df4fabeSYong Wu 	bool layer, write;
2120df4fabeSYong Wu 
2130df4fabeSYong Wu 	/* Read error info from registers */
2140df4fabeSYong Wu 	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
2150df4fabeSYong Wu 	fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
2160df4fabeSYong Wu 	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
2170df4fabeSYong Wu 	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
2180df4fabeSYong Wu 	fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
2190df4fabeSYong Wu 	regval = readl_relaxed(data->base + REG_MMU_INT_ID);
2200df4fabeSYong Wu 	fault_larb = F_MMU0_INT_ID_LARB_ID(regval);
2210df4fabeSYong Wu 	fault_port = F_MMU0_INT_ID_PORT_ID(regval);
2220df4fabeSYong Wu 
2230df4fabeSYong Wu 	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
2240df4fabeSYong Wu 			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
2250df4fabeSYong Wu 		dev_err_ratelimited(
2260df4fabeSYong Wu 			data->dev,
2270df4fabeSYong Wu 			"fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
2280df4fabeSYong Wu 			int_state, fault_iova, fault_pa, fault_larb, fault_port,
2290df4fabeSYong Wu 			layer, write ? "write" : "read");
2300df4fabeSYong Wu 	}
2310df4fabeSYong Wu 
2320df4fabeSYong Wu 	/* Interrupt clear */
2330df4fabeSYong Wu 	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
2340df4fabeSYong Wu 	regval |= F_INT_CLR_BIT;
2350df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
2360df4fabeSYong Wu 
2370df4fabeSYong Wu 	mtk_iommu_tlb_flush_all(data);
2380df4fabeSYong Wu 
2390df4fabeSYong Wu 	return IRQ_HANDLED;
2400df4fabeSYong Wu }
2410df4fabeSYong Wu 
2420df4fabeSYong Wu static void mtk_iommu_config(struct mtk_iommu_data *data,
2430df4fabeSYong Wu 			     struct device *dev, bool enable)
2440df4fabeSYong Wu {
2450df4fabeSYong Wu 	struct mtk_smi_larb_iommu    *larb_mmu;
2460df4fabeSYong Wu 	unsigned int                 larbid, portid;
247*a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
24858f0d1d5SRobin Murphy 	int i;
2490df4fabeSYong Wu 
25058f0d1d5SRobin Murphy 	for (i = 0; i < fwspec->num_ids; ++i) {
25158f0d1d5SRobin Murphy 		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
25258f0d1d5SRobin Murphy 		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
2530df4fabeSYong Wu 		larb_mmu = &data->smi_imu.larb_imu[larbid];
2540df4fabeSYong Wu 
2550df4fabeSYong Wu 		dev_dbg(dev, "%s iommu port: %d\n",
2560df4fabeSYong Wu 			enable ? "enable" : "disable", portid);
2570df4fabeSYong Wu 
2580df4fabeSYong Wu 		if (enable)
2590df4fabeSYong Wu 			larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
2600df4fabeSYong Wu 		else
2610df4fabeSYong Wu 			larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
2620df4fabeSYong Wu 	}
2630df4fabeSYong Wu }
2640df4fabeSYong Wu 
2654b00f5acSYong Wu static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
2660df4fabeSYong Wu {
2674b00f5acSYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
2680df4fabeSYong Wu 
2690df4fabeSYong Wu 	spin_lock_init(&dom->pgtlock);
2700df4fabeSYong Wu 
2710df4fabeSYong Wu 	dom->cfg = (struct io_pgtable_cfg) {
2720df4fabeSYong Wu 		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
2730df4fabeSYong Wu 			IO_PGTABLE_QUIRK_NO_PERMS |
2740df4fabeSYong Wu 			IO_PGTABLE_QUIRK_TLBI_ON_MAP,
2750df4fabeSYong Wu 		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
2760df4fabeSYong Wu 		.ias = 32,
2770df4fabeSYong Wu 		.oas = 32,
2780df4fabeSYong Wu 		.tlb = &mtk_iommu_gather_ops,
2790df4fabeSYong Wu 		.iommu_dev = data->dev,
2800df4fabeSYong Wu 	};
2810df4fabeSYong Wu 
28201e23c93SYong Wu 	if (data->enable_4GB)
28301e23c93SYong Wu 		dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_4GB;
28401e23c93SYong Wu 
2850df4fabeSYong Wu 	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
2860df4fabeSYong Wu 	if (!dom->iop) {
2870df4fabeSYong Wu 		dev_err(data->dev, "Failed to alloc io pgtable\n");
2880df4fabeSYong Wu 		return -EINVAL;
2890df4fabeSYong Wu 	}
2900df4fabeSYong Wu 
2910df4fabeSYong Wu 	/* Update our support page sizes bitmap */
292d16e0faaSRobin Murphy 	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
2930df4fabeSYong Wu 	return 0;
2940df4fabeSYong Wu }
2950df4fabeSYong Wu 
2960df4fabeSYong Wu static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
2970df4fabeSYong Wu {
2980df4fabeSYong Wu 	struct mtk_iommu_domain *dom;
2990df4fabeSYong Wu 
3000df4fabeSYong Wu 	if (type != IOMMU_DOMAIN_DMA)
3010df4fabeSYong Wu 		return NULL;
3020df4fabeSYong Wu 
3030df4fabeSYong Wu 	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
3040df4fabeSYong Wu 	if (!dom)
3050df4fabeSYong Wu 		return NULL;
3060df4fabeSYong Wu 
3074b00f5acSYong Wu 	if (iommu_get_dma_cookie(&dom->domain))
3084b00f5acSYong Wu 		goto  free_dom;
3094b00f5acSYong Wu 
3104b00f5acSYong Wu 	if (mtk_iommu_domain_finalise(dom))
3114b00f5acSYong Wu 		goto  put_dma_cookie;
3120df4fabeSYong Wu 
3130df4fabeSYong Wu 	dom->domain.geometry.aperture_start = 0;
3140df4fabeSYong Wu 	dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
3150df4fabeSYong Wu 	dom->domain.geometry.force_aperture = true;
3160df4fabeSYong Wu 
3170df4fabeSYong Wu 	return &dom->domain;
3184b00f5acSYong Wu 
3194b00f5acSYong Wu put_dma_cookie:
3204b00f5acSYong Wu 	iommu_put_dma_cookie(&dom->domain);
3214b00f5acSYong Wu free_dom:
3224b00f5acSYong Wu 	kfree(dom);
3234b00f5acSYong Wu 	return NULL;
3240df4fabeSYong Wu }
3250df4fabeSYong Wu 
3260df4fabeSYong Wu static void mtk_iommu_domain_free(struct iommu_domain *domain)
3270df4fabeSYong Wu {
3284b00f5acSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
3294b00f5acSYong Wu 
3304b00f5acSYong Wu 	free_io_pgtable_ops(dom->iop);
3310df4fabeSYong Wu 	iommu_put_dma_cookie(domain);
3320df4fabeSYong Wu 	kfree(to_mtk_domain(domain));
3330df4fabeSYong Wu }
3340df4fabeSYong Wu 
3350df4fabeSYong Wu static int mtk_iommu_attach_device(struct iommu_domain *domain,
3360df4fabeSYong Wu 				   struct device *dev)
3370df4fabeSYong Wu {
3380df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
339*a9bf2eecSJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
3400df4fabeSYong Wu 
3414b00f5acSYong Wu 	if (!data)
3420df4fabeSYong Wu 		return -ENODEV;
3430df4fabeSYong Wu 
3444b00f5acSYong Wu 	/* Update the pgtable base address register of the M4U HW */
3450df4fabeSYong Wu 	if (!data->m4u_dom) {
3460df4fabeSYong Wu 		data->m4u_dom = dom;
3474b00f5acSYong Wu 		writel(dom->cfg.arm_v7s_cfg.ttbr[0],
3484b00f5acSYong Wu 		       data->base + REG_MMU_PT_BASE_ADDR);
3490df4fabeSYong Wu 	}
3500df4fabeSYong Wu 
3514b00f5acSYong Wu 	mtk_iommu_config(data, dev, true);
3520df4fabeSYong Wu 	return 0;
3530df4fabeSYong Wu }
3540df4fabeSYong Wu 
3550df4fabeSYong Wu static void mtk_iommu_detach_device(struct iommu_domain *domain,
3560df4fabeSYong Wu 				    struct device *dev)
3570df4fabeSYong Wu {
358*a9bf2eecSJoerg Roedel 	struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
3590df4fabeSYong Wu 
36058f0d1d5SRobin Murphy 	if (!data)
3610df4fabeSYong Wu 		return;
3620df4fabeSYong Wu 
3630df4fabeSYong Wu 	mtk_iommu_config(data, dev, false);
3640df4fabeSYong Wu }
3650df4fabeSYong Wu 
3660df4fabeSYong Wu static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
3670df4fabeSYong Wu 			 phys_addr_t paddr, size_t size, int prot)
3680df4fabeSYong Wu {
3690df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
3700df4fabeSYong Wu 	unsigned long flags;
3710df4fabeSYong Wu 	int ret;
3720df4fabeSYong Wu 
3730df4fabeSYong Wu 	spin_lock_irqsave(&dom->pgtlock, flags);
3741ff9b17cSYong Wu 	ret = dom->iop->map(dom->iop, iova, paddr & DMA_BIT_MASK(32),
3751ff9b17cSYong Wu 			    size, prot);
3760df4fabeSYong Wu 	spin_unlock_irqrestore(&dom->pgtlock, flags);
3770df4fabeSYong Wu 
3780df4fabeSYong Wu 	return ret;
3790df4fabeSYong Wu }
3800df4fabeSYong Wu 
3810df4fabeSYong Wu static size_t mtk_iommu_unmap(struct iommu_domain *domain,
3820df4fabeSYong Wu 			      unsigned long iova, size_t size)
3830df4fabeSYong Wu {
3840df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
3850df4fabeSYong Wu 	unsigned long flags;
3860df4fabeSYong Wu 	size_t unmapsz;
3870df4fabeSYong Wu 
3880df4fabeSYong Wu 	spin_lock_irqsave(&dom->pgtlock, flags);
3890df4fabeSYong Wu 	unmapsz = dom->iop->unmap(dom->iop, iova, size);
3900df4fabeSYong Wu 	spin_unlock_irqrestore(&dom->pgtlock, flags);
3910df4fabeSYong Wu 
3920df4fabeSYong Wu 	return unmapsz;
3930df4fabeSYong Wu }
3940df4fabeSYong Wu 
3954d689b61SRobin Murphy static void mtk_iommu_iotlb_sync(struct iommu_domain *domain)
3964d689b61SRobin Murphy {
3974d689b61SRobin Murphy 	mtk_iommu_tlb_sync(mtk_iommu_get_m4u_data());
3984d689b61SRobin Murphy }
3994d689b61SRobin Murphy 
4000df4fabeSYong Wu static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
4010df4fabeSYong Wu 					  dma_addr_t iova)
4020df4fabeSYong Wu {
4030df4fabeSYong Wu 	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
40430e2fccfSYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
4050df4fabeSYong Wu 	unsigned long flags;
4060df4fabeSYong Wu 	phys_addr_t pa;
4070df4fabeSYong Wu 
4080df4fabeSYong Wu 	spin_lock_irqsave(&dom->pgtlock, flags);
4090df4fabeSYong Wu 	pa = dom->iop->iova_to_phys(dom->iop, iova);
4100df4fabeSYong Wu 	spin_unlock_irqrestore(&dom->pgtlock, flags);
4110df4fabeSYong Wu 
41230e2fccfSYong Wu 	if (data->enable_4GB)
41341939980SYong Wu 		pa |= BIT_ULL(32);
41430e2fccfSYong Wu 
4150df4fabeSYong Wu 	return pa;
4160df4fabeSYong Wu }
4170df4fabeSYong Wu 
4180df4fabeSYong Wu static int mtk_iommu_add_device(struct device *dev)
4190df4fabeSYong Wu {
420*a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
421b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
4220df4fabeSYong Wu 	struct iommu_group *group;
4230df4fabeSYong Wu 
424*a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
42558f0d1d5SRobin Murphy 		return -ENODEV; /* Not a iommu client device */
4260df4fabeSYong Wu 
427*a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
428b16c0170SJoerg Roedel 	iommu_device_link(&data->iommu, dev);
429b16c0170SJoerg Roedel 
4300df4fabeSYong Wu 	group = iommu_group_get_for_dev(dev);
4310df4fabeSYong Wu 	if (IS_ERR(group))
4320df4fabeSYong Wu 		return PTR_ERR(group);
4330df4fabeSYong Wu 
4340df4fabeSYong Wu 	iommu_group_put(group);
4350df4fabeSYong Wu 	return 0;
4360df4fabeSYong Wu }
4370df4fabeSYong Wu 
4380df4fabeSYong Wu static void mtk_iommu_remove_device(struct device *dev)
4390df4fabeSYong Wu {
440*a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
441b16c0170SJoerg Roedel 	struct mtk_iommu_data *data;
442b16c0170SJoerg Roedel 
443*a9bf2eecSJoerg Roedel 	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
4440df4fabeSYong Wu 		return;
4450df4fabeSYong Wu 
446*a9bf2eecSJoerg Roedel 	data = fwspec->iommu_priv;
447b16c0170SJoerg Roedel 	iommu_device_unlink(&data->iommu, dev);
448b16c0170SJoerg Roedel 
4490df4fabeSYong Wu 	iommu_group_remove_device(dev);
45058f0d1d5SRobin Murphy 	iommu_fwspec_free(dev);
4510df4fabeSYong Wu }
4520df4fabeSYong Wu 
4530df4fabeSYong Wu static struct iommu_group *mtk_iommu_device_group(struct device *dev)
4540df4fabeSYong Wu {
4557c3a2ec0SYong Wu 	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
4560df4fabeSYong Wu 
45758f0d1d5SRobin Murphy 	if (!data)
4580df4fabeSYong Wu 		return ERR_PTR(-ENODEV);
4590df4fabeSYong Wu 
4600df4fabeSYong Wu 	/* All the client devices are in the same m4u iommu-group */
4610df4fabeSYong Wu 	if (!data->m4u_group) {
4620df4fabeSYong Wu 		data->m4u_group = iommu_group_alloc();
4630df4fabeSYong Wu 		if (IS_ERR(data->m4u_group))
4640df4fabeSYong Wu 			dev_err(dev, "Failed to allocate M4U IOMMU group\n");
4653a8d40b6SRobin Murphy 	} else {
4663a8d40b6SRobin Murphy 		iommu_group_ref_get(data->m4u_group);
4670df4fabeSYong Wu 	}
4680df4fabeSYong Wu 	return data->m4u_group;
4690df4fabeSYong Wu }
4700df4fabeSYong Wu 
4710df4fabeSYong Wu static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
4720df4fabeSYong Wu {
473*a9bf2eecSJoerg Roedel 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
4740df4fabeSYong Wu 	struct platform_device *m4updev;
4750df4fabeSYong Wu 
4760df4fabeSYong Wu 	if (args->args_count != 1) {
4770df4fabeSYong Wu 		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
4780df4fabeSYong Wu 			args->args_count);
4790df4fabeSYong Wu 		return -EINVAL;
4800df4fabeSYong Wu 	}
4810df4fabeSYong Wu 
482*a9bf2eecSJoerg Roedel 	if (!fwspec->iommu_priv) {
4830df4fabeSYong Wu 		/* Get the m4u device */
4840df4fabeSYong Wu 		m4updev = of_find_device_by_node(args->np);
4850df4fabeSYong Wu 		if (WARN_ON(!m4updev))
4860df4fabeSYong Wu 			return -EINVAL;
4870df4fabeSYong Wu 
488*a9bf2eecSJoerg Roedel 		fwspec->iommu_priv = platform_get_drvdata(m4updev);
4890df4fabeSYong Wu 	}
4900df4fabeSYong Wu 
49158f0d1d5SRobin Murphy 	return iommu_fwspec_add_ids(dev, args->args, 1);
4920df4fabeSYong Wu }
4930df4fabeSYong Wu 
4940df4fabeSYong Wu static struct iommu_ops mtk_iommu_ops = {
4950df4fabeSYong Wu 	.domain_alloc	= mtk_iommu_domain_alloc,
4960df4fabeSYong Wu 	.domain_free	= mtk_iommu_domain_free,
4970df4fabeSYong Wu 	.attach_dev	= mtk_iommu_attach_device,
4980df4fabeSYong Wu 	.detach_dev	= mtk_iommu_detach_device,
4990df4fabeSYong Wu 	.map		= mtk_iommu_map,
5000df4fabeSYong Wu 	.unmap		= mtk_iommu_unmap,
5014d689b61SRobin Murphy 	.flush_iotlb_all = mtk_iommu_iotlb_sync,
5024d689b61SRobin Murphy 	.iotlb_sync	= mtk_iommu_iotlb_sync,
5030df4fabeSYong Wu 	.iova_to_phys	= mtk_iommu_iova_to_phys,
5040df4fabeSYong Wu 	.add_device	= mtk_iommu_add_device,
5050df4fabeSYong Wu 	.remove_device	= mtk_iommu_remove_device,
5060df4fabeSYong Wu 	.device_group	= mtk_iommu_device_group,
5070df4fabeSYong Wu 	.of_xlate	= mtk_iommu_of_xlate,
5080df4fabeSYong Wu 	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
5090df4fabeSYong Wu };
5100df4fabeSYong Wu 
5110df4fabeSYong Wu static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
5120df4fabeSYong Wu {
5130df4fabeSYong Wu 	u32 regval;
5140df4fabeSYong Wu 	int ret;
5150df4fabeSYong Wu 
5160df4fabeSYong Wu 	ret = clk_prepare_enable(data->bclk);
5170df4fabeSYong Wu 	if (ret) {
5180df4fabeSYong Wu 		dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
5190df4fabeSYong Wu 		return ret;
5200df4fabeSYong Wu 	}
5210df4fabeSYong Wu 
522e6dec923SYong Wu 	regval = F_MMU_TF_PROTECT_SEL(2, data);
523e6dec923SYong Wu 	if (data->m4u_plat == M4U_MT8173)
524e6dec923SYong Wu 		regval |= F_MMU_PREFETCH_RT_REPLACE_MOD;
5250df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
5260df4fabeSYong Wu 
5270df4fabeSYong Wu 	regval = F_L2_MULIT_HIT_EN |
5280df4fabeSYong Wu 		F_TABLE_WALK_FAULT_INT_EN |
5290df4fabeSYong Wu 		F_PREETCH_FIFO_OVERFLOW_INT_EN |
5300df4fabeSYong Wu 		F_MISS_FIFO_OVERFLOW_INT_EN |
5310df4fabeSYong Wu 		F_PREFETCH_FIFO_ERR_INT_EN |
5320df4fabeSYong Wu 		F_MISS_FIFO_ERR_INT_EN;
5330df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
5340df4fabeSYong Wu 
5350df4fabeSYong Wu 	regval = F_INT_TRANSLATION_FAULT |
5360df4fabeSYong Wu 		F_INT_MAIN_MULTI_HIT_FAULT |
5370df4fabeSYong Wu 		F_INT_INVALID_PA_FAULT |
5380df4fabeSYong Wu 		F_INT_ENTRY_REPLACEMENT_FAULT |
5390df4fabeSYong Wu 		F_INT_TLB_MISS_FAULT |
5400df4fabeSYong Wu 		F_INT_MISS_TRANSACTION_FIFO_FAULT |
5410df4fabeSYong Wu 		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
5420df4fabeSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
5430df4fabeSYong Wu 
54470ca608bSYong Wu 	if (data->m4u_plat == M4U_MT8173)
54570ca608bSYong Wu 		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
54670ca608bSYong Wu 	else
54770ca608bSYong Wu 		regval = lower_32_bits(data->protect_base) |
54870ca608bSYong Wu 			 upper_32_bits(data->protect_base);
54970ca608bSYong Wu 	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
55070ca608bSYong Wu 
5514f1c8ea1SYong Wu 	if (data->enable_4GB && data->m4u_plat != M4U_MT8173) {
55230e2fccfSYong Wu 		/*
55330e2fccfSYong Wu 		 * If 4GB mode is enabled, the validate PA range is from
55430e2fccfSYong Wu 		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
55530e2fccfSYong Wu 		 */
55630e2fccfSYong Wu 		regval = F_MMU_VLD_PA_RNG(7, 4);
55730e2fccfSYong Wu 		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
55830e2fccfSYong Wu 	}
5590df4fabeSYong Wu 	writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
560e6dec923SYong Wu 
561e6dec923SYong Wu 	/* It's MISC control register whose default value is ok except mt8173.*/
562e6dec923SYong Wu 	if (data->m4u_plat == M4U_MT8173)
5630df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
5640df4fabeSYong Wu 
5650df4fabeSYong Wu 	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
5660df4fabeSYong Wu 			     dev_name(data->dev), (void *)data)) {
5670df4fabeSYong Wu 		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
5680df4fabeSYong Wu 		clk_disable_unprepare(data->bclk);
5690df4fabeSYong Wu 		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
5700df4fabeSYong Wu 		return -ENODEV;
5710df4fabeSYong Wu 	}
5720df4fabeSYong Wu 
5730df4fabeSYong Wu 	return 0;
5740df4fabeSYong Wu }
5750df4fabeSYong Wu 
5760df4fabeSYong Wu static const struct component_master_ops mtk_iommu_com_ops = {
5770df4fabeSYong Wu 	.bind		= mtk_iommu_bind,
5780df4fabeSYong Wu 	.unbind		= mtk_iommu_unbind,
5790df4fabeSYong Wu };
5800df4fabeSYong Wu 
5810df4fabeSYong Wu static int mtk_iommu_probe(struct platform_device *pdev)
5820df4fabeSYong Wu {
5830df4fabeSYong Wu 	struct mtk_iommu_data   *data;
5840df4fabeSYong Wu 	struct device           *dev = &pdev->dev;
5850df4fabeSYong Wu 	struct resource         *res;
586b16c0170SJoerg Roedel 	resource_size_t		ioaddr;
5870df4fabeSYong Wu 	struct component_match  *match = NULL;
5880df4fabeSYong Wu 	void                    *protect;
5890b6c0ad3SAndrzej Hajda 	int                     i, larb_nr, ret;
5900df4fabeSYong Wu 
5910df4fabeSYong Wu 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
5920df4fabeSYong Wu 	if (!data)
5930df4fabeSYong Wu 		return -ENOMEM;
5940df4fabeSYong Wu 	data->dev = dev;
595e6dec923SYong Wu 	data->m4u_plat = (enum mtk_iommu_plat)of_device_get_match_data(dev);
5960df4fabeSYong Wu 
5970df4fabeSYong Wu 	/* Protect memory. HW will access here while translation fault.*/
5980df4fabeSYong Wu 	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
5990df4fabeSYong Wu 	if (!protect)
6000df4fabeSYong Wu 		return -ENOMEM;
6010df4fabeSYong Wu 	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
6020df4fabeSYong Wu 
60301e23c93SYong Wu 	/* Whether the current dram is over 4GB */
60441939980SYong Wu 	data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT));
60501e23c93SYong Wu 
6060df4fabeSYong Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6070df4fabeSYong Wu 	data->base = devm_ioremap_resource(dev, res);
6080df4fabeSYong Wu 	if (IS_ERR(data->base))
6090df4fabeSYong Wu 		return PTR_ERR(data->base);
610b16c0170SJoerg Roedel 	ioaddr = res->start;
6110df4fabeSYong Wu 
6120df4fabeSYong Wu 	data->irq = platform_get_irq(pdev, 0);
6130df4fabeSYong Wu 	if (data->irq < 0)
6140df4fabeSYong Wu 		return data->irq;
6150df4fabeSYong Wu 
6160df4fabeSYong Wu 	data->bclk = devm_clk_get(dev, "bclk");
6170df4fabeSYong Wu 	if (IS_ERR(data->bclk))
6180df4fabeSYong Wu 		return PTR_ERR(data->bclk);
6190df4fabeSYong Wu 
6200df4fabeSYong Wu 	larb_nr = of_count_phandle_with_args(dev->of_node,
6210df4fabeSYong Wu 					     "mediatek,larbs", NULL);
6220df4fabeSYong Wu 	if (larb_nr < 0)
6230df4fabeSYong Wu 		return larb_nr;
6240df4fabeSYong Wu 	data->smi_imu.larb_nr = larb_nr;
6250df4fabeSYong Wu 
6260df4fabeSYong Wu 	for (i = 0; i < larb_nr; i++) {
6270df4fabeSYong Wu 		struct device_node *larbnode;
6280df4fabeSYong Wu 		struct platform_device *plarbdev;
629e6dec923SYong Wu 		u32 id;
6300df4fabeSYong Wu 
6310df4fabeSYong Wu 		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
6320df4fabeSYong Wu 		if (!larbnode)
6330df4fabeSYong Wu 			return -EINVAL;
6340df4fabeSYong Wu 
6350df4fabeSYong Wu 		if (!of_device_is_available(larbnode))
6360df4fabeSYong Wu 			continue;
6370df4fabeSYong Wu 
638e6dec923SYong Wu 		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
639e6dec923SYong Wu 		if (ret)/* The id is consecutive if there is no this property */
640e6dec923SYong Wu 			id = i;
641e6dec923SYong Wu 
6420df4fabeSYong Wu 		plarbdev = of_find_device_by_node(larbnode);
643e6dec923SYong Wu 		if (!plarbdev)
6440df4fabeSYong Wu 			return -EPROBE_DEFER;
645e6dec923SYong Wu 		data->smi_imu.larb_imu[id].dev = &plarbdev->dev;
6460df4fabeSYong Wu 
64700c7c81fSRussell King 		component_match_add_release(dev, &match, release_of,
64800c7c81fSRussell King 					    compare_of, larbnode);
6490df4fabeSYong Wu 	}
6500df4fabeSYong Wu 
6510df4fabeSYong Wu 	platform_set_drvdata(pdev, data);
6520df4fabeSYong Wu 
6530df4fabeSYong Wu 	ret = mtk_iommu_hw_init(data);
6540df4fabeSYong Wu 	if (ret)
6550df4fabeSYong Wu 		return ret;
6560df4fabeSYong Wu 
657b16c0170SJoerg Roedel 	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
658b16c0170SJoerg Roedel 				     "mtk-iommu.%pa", &ioaddr);
659b16c0170SJoerg Roedel 	if (ret)
660b16c0170SJoerg Roedel 		return ret;
661b16c0170SJoerg Roedel 
662b16c0170SJoerg Roedel 	iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
663b16c0170SJoerg Roedel 	iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
664b16c0170SJoerg Roedel 
665b16c0170SJoerg Roedel 	ret = iommu_device_register(&data->iommu);
666b16c0170SJoerg Roedel 	if (ret)
667b16c0170SJoerg Roedel 		return ret;
668b16c0170SJoerg Roedel 
6697c3a2ec0SYong Wu 	list_add_tail(&data->list, &m4ulist);
6707c3a2ec0SYong Wu 
6710df4fabeSYong Wu 	if (!iommu_present(&platform_bus_type))
6720df4fabeSYong Wu 		bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
6730df4fabeSYong Wu 
6740df4fabeSYong Wu 	return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
6750df4fabeSYong Wu }
6760df4fabeSYong Wu 
6770df4fabeSYong Wu static int mtk_iommu_remove(struct platform_device *pdev)
6780df4fabeSYong Wu {
6790df4fabeSYong Wu 	struct mtk_iommu_data *data = platform_get_drvdata(pdev);
6800df4fabeSYong Wu 
681b16c0170SJoerg Roedel 	iommu_device_sysfs_remove(&data->iommu);
682b16c0170SJoerg Roedel 	iommu_device_unregister(&data->iommu);
683b16c0170SJoerg Roedel 
6840df4fabeSYong Wu 	if (iommu_present(&platform_bus_type))
6850df4fabeSYong Wu 		bus_set_iommu(&platform_bus_type, NULL);
6860df4fabeSYong Wu 
6870df4fabeSYong Wu 	clk_disable_unprepare(data->bclk);
6880df4fabeSYong Wu 	devm_free_irq(&pdev->dev, data->irq, data);
6890df4fabeSYong Wu 	component_master_del(&pdev->dev, &mtk_iommu_com_ops);
6900df4fabeSYong Wu 	return 0;
6910df4fabeSYong Wu }
6920df4fabeSYong Wu 
693fd99f796SArnd Bergmann static int __maybe_unused mtk_iommu_suspend(struct device *dev)
6940df4fabeSYong Wu {
6950df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
6960df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
6970df4fabeSYong Wu 	void __iomem *base = data->base;
6980df4fabeSYong Wu 
6990df4fabeSYong Wu 	reg->standard_axi_mode = readl_relaxed(base +
7000df4fabeSYong Wu 					       REG_MMU_STANDARD_AXI_MODE);
7010df4fabeSYong Wu 	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
7020df4fabeSYong Wu 	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
7030df4fabeSYong Wu 	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
7040df4fabeSYong Wu 	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
70570ca608bSYong Wu 	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
7066254b64fSYong Wu 	clk_disable_unprepare(data->bclk);
7070df4fabeSYong Wu 	return 0;
7080df4fabeSYong Wu }
7090df4fabeSYong Wu 
710fd99f796SArnd Bergmann static int __maybe_unused mtk_iommu_resume(struct device *dev)
7110df4fabeSYong Wu {
7120df4fabeSYong Wu 	struct mtk_iommu_data *data = dev_get_drvdata(dev);
7130df4fabeSYong Wu 	struct mtk_iommu_suspend_reg *reg = &data->reg;
7140df4fabeSYong Wu 	void __iomem *base = data->base;
7156254b64fSYong Wu 	int ret;
7160df4fabeSYong Wu 
7176254b64fSYong Wu 	ret = clk_prepare_enable(data->bclk);
7186254b64fSYong Wu 	if (ret) {
7196254b64fSYong Wu 		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
7206254b64fSYong Wu 		return ret;
7216254b64fSYong Wu 	}
7220df4fabeSYong Wu 	writel_relaxed(reg->standard_axi_mode,
7230df4fabeSYong Wu 		       base + REG_MMU_STANDARD_AXI_MODE);
7240df4fabeSYong Wu 	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
7250df4fabeSYong Wu 	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
7260df4fabeSYong Wu 	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
7270df4fabeSYong Wu 	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
72870ca608bSYong Wu 	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
729e6dec923SYong Wu 	if (data->m4u_dom)
730e6dec923SYong Wu 		writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
731e6dec923SYong Wu 		       base + REG_MMU_PT_BASE_ADDR);
7320df4fabeSYong Wu 	return 0;
7330df4fabeSYong Wu }
7340df4fabeSYong Wu 
735e6dec923SYong Wu static const struct dev_pm_ops mtk_iommu_pm_ops = {
7366254b64fSYong Wu 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
7370df4fabeSYong Wu };
7380df4fabeSYong Wu 
7390df4fabeSYong Wu static const struct of_device_id mtk_iommu_of_ids[] = {
740e6dec923SYong Wu 	{ .compatible = "mediatek,mt2712-m4u", .data = (void *)M4U_MT2712},
741e6dec923SYong Wu 	{ .compatible = "mediatek,mt8173-m4u", .data = (void *)M4U_MT8173},
7420df4fabeSYong Wu 	{}
7430df4fabeSYong Wu };
7440df4fabeSYong Wu 
7450df4fabeSYong Wu static struct platform_driver mtk_iommu_driver = {
7460df4fabeSYong Wu 	.probe	= mtk_iommu_probe,
7470df4fabeSYong Wu 	.remove	= mtk_iommu_remove,
7480df4fabeSYong Wu 	.driver	= {
7490df4fabeSYong Wu 		.name = "mtk-iommu",
750e6dec923SYong Wu 		.of_match_table = of_match_ptr(mtk_iommu_of_ids),
7510df4fabeSYong Wu 		.pm = &mtk_iommu_pm_ops,
7520df4fabeSYong Wu 	}
7530df4fabeSYong Wu };
7540df4fabeSYong Wu 
755e6dec923SYong Wu static int __init mtk_iommu_init(void)
7560df4fabeSYong Wu {
7570df4fabeSYong Wu 	int ret;
7580df4fabeSYong Wu 
7590df4fabeSYong Wu 	ret = platform_driver_register(&mtk_iommu_driver);
760e6dec923SYong Wu 	if (ret != 0)
761e6dec923SYong Wu 		pr_err("Failed to register MTK IOMMU driver\n");
762e6dec923SYong Wu 
7630df4fabeSYong Wu 	return ret;
7640df4fabeSYong Wu }
7650df4fabeSYong Wu 
766e6dec923SYong Wu subsys_initcall(mtk_iommu_init)
767