blob: 533b8f76f592e578f3727eeb37f43a148e8c87fb [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Yong Wu0df4fab2016-02-23 01:20:50 +08002/*
3 * Copyright (c) 2015-2016 MediaTek Inc.
4 * Author: Yong Wu <yong.wu@mediatek.com>
Yong Wu0df4fab2016-02-23 01:20:50 +08005 */
Mike Rapoport57c8a662018-10-30 15:09:49 -07006#include <linux/memblock.h>
Yong Wu0df4fab2016-02-23 01:20:50 +08007#include <linux/bug.h>
8#include <linux/clk.h>
9#include <linux/component.h>
10#include <linux/device.h>
11#include <linux/dma-iommu.h>
12#include <linux/err.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/iommu.h>
16#include <linux/iopoll.h>
17#include <linux/list.h>
18#include <linux/of_address.h>
19#include <linux/of_iommu.h>
20#include <linux/of_irq.h>
21#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25#include <asm/barrier.h>
Yong Wu0df4fab2016-02-23 01:20:50 +080026#include <soc/mediatek/smi.h>
27
Honghui Zhang9ca340c2016-06-08 17:50:58 +080028#include "mtk_iommu.h"
Yong Wu0df4fab2016-02-23 01:20:50 +080029
30#define REG_MMU_PT_BASE_ADDR 0x000
Yong Wu907ba6a2019-08-24 11:02:02 +080031#define MMU_PT_ADDR_MASK GENMASK(31, 7)
Yong Wu0df4fab2016-02-23 01:20:50 +080032
33#define REG_MMU_INVALIDATE 0x020
34#define F_ALL_INVLD 0x2
35#define F_MMU_INV_RANGE 0x1
36
37#define REG_MMU_INVLD_START_A 0x024
38#define REG_MMU_INVLD_END_A 0x028
39
Chao Haob053bc72020-07-03 12:41:22 +080040#define REG_MMU_INV_SEL_GEN1 0x038
Yong Wu0df4fab2016-02-23 01:20:50 +080041#define F_INVLD_EN0 BIT(0)
42#define F_INVLD_EN1 BIT(1)
43
Chao Hao75eed352020-07-03 12:41:19 +080044#define REG_MMU_MISC_CTRL 0x048
Chao Hao4bb2bf42020-07-03 12:41:21 +080045#define F_MMU_IN_ORDER_WR_EN_MASK (BIT(1) | BIT(17))
46#define F_MMU_STANDARD_AXI_MODE_MASK (BIT(3) | BIT(19))
47
Yong Wu0df4fab2016-02-23 01:20:50 +080048#define REG_MMU_DCM_DIS 0x050
49
50#define REG_MMU_CTRL_REG 0x110
Yong Wuacb3c922019-08-24 11:01:58 +080051#define F_MMU_TF_PROT_TO_PROGRAM_ADDR (2 << 4)
Yong Wu0df4fab2016-02-23 01:20:50 +080052#define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
Yong Wuacb3c922019-08-24 11:01:58 +080053#define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173 (2 << 5)
Yong Wu0df4fab2016-02-23 01:20:50 +080054
55#define REG_MMU_IVRP_PADDR 0x114
Yong Wu70ca6082018-03-18 09:52:54 +080056
Yong Wu30e2fcc2017-08-21 19:00:20 +080057#define REG_MMU_VLD_PA_RNG 0x118
58#define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA))
Yong Wu0df4fab2016-02-23 01:20:50 +080059
60#define REG_MMU_INT_CONTROL0 0x120
61#define F_L2_MULIT_HIT_EN BIT(0)
62#define F_TABLE_WALK_FAULT_INT_EN BIT(1)
63#define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
64#define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
65#define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
66#define F_MISS_FIFO_ERR_INT_EN BIT(6)
67#define F_INT_CLR_BIT BIT(12)
68
69#define REG_MMU_INT_MAIN_CONTROL 0x124
Yong Wu15a01f42019-08-24 11:02:03 +080070 /* mmu0 | mmu1 */
71#define F_INT_TRANSLATION_FAULT (BIT(0) | BIT(7))
72#define F_INT_MAIN_MULTI_HIT_FAULT (BIT(1) | BIT(8))
73#define F_INT_INVALID_PA_FAULT (BIT(2) | BIT(9))
74#define F_INT_ENTRY_REPLACEMENT_FAULT (BIT(3) | BIT(10))
75#define F_INT_TLB_MISS_FAULT (BIT(4) | BIT(11))
76#define F_INT_MISS_TRANSACTION_FIFO_FAULT (BIT(5) | BIT(12))
77#define F_INT_PRETETCH_TRANSATION_FIFO_FAULT (BIT(6) | BIT(13))
Yong Wu0df4fab2016-02-23 01:20:50 +080078
79#define REG_MMU_CPE_DONE 0x12C
80
81#define REG_MMU_FAULT_ST1 0x134
Yong Wu15a01f42019-08-24 11:02:03 +080082#define F_REG_MMU0_FAULT_MASK GENMASK(6, 0)
83#define F_REG_MMU1_FAULT_MASK GENMASK(13, 7)
Yong Wu0df4fab2016-02-23 01:20:50 +080084
Yong Wu15a01f42019-08-24 11:02:03 +080085#define REG_MMU0_FAULT_VA 0x13c
Yong Wu0df4fab2016-02-23 01:20:50 +080086#define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
87#define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
88
Yong Wu15a01f42019-08-24 11:02:03 +080089#define REG_MMU0_INVLD_PA 0x140
90#define REG_MMU1_FAULT_VA 0x144
91#define REG_MMU1_INVLD_PA 0x148
92#define REG_MMU0_INT_ID 0x150
93#define REG_MMU1_INT_ID 0x154
94#define F_MMU_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
95#define F_MMU_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
Yong Wu0df4fab2016-02-23 01:20:50 +080096
97#define MTK_PROTECT_PA_ALIGN 128
98
Yong Wua9467d92017-08-21 19:00:15 +080099/*
100 * Get the local arbiter ID and the portid within the larb arbiter
101 * from mtk_m4u_id which is defined by MTK_M4U_ID.
102 */
Yong Wue6dec922017-08-21 19:00:16 +0800103#define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf)
Yong Wua9467d92017-08-21 19:00:15 +0800104#define MTK_M4U_TO_PORT(id) ((id) & 0x1f)
105
Chao Hao6b717792020-07-03 12:41:20 +0800106#define HAS_4GB_MODE BIT(0)
107/* HW will use the EMI clock if there isn't the "bclk". */
108#define HAS_BCLK BIT(1)
109#define HAS_VLD_PA_RNG BIT(2)
110#define RESET_AXI BIT(3)
Chao Hao4bb2bf42020-07-03 12:41:21 +0800111#define OUT_ORDER_WR_EN BIT(4)
Chao Hao6b717792020-07-03 12:41:20 +0800112
113#define MTK_IOMMU_HAS_FLAG(pdata, _x) \
114 ((((pdata)->flags) & (_x)) == (_x))
115
Yong Wu0df4fab2016-02-23 01:20:50 +0800116struct mtk_iommu_domain {
Yong Wu0df4fab2016-02-23 01:20:50 +0800117 struct io_pgtable_cfg cfg;
118 struct io_pgtable_ops *iop;
119
120 struct iommu_domain domain;
121};
122
Arvind Yadavb65f5012018-10-18 19:13:38 +0800123static const struct iommu_ops mtk_iommu_ops;
Yong Wu0df4fab2016-02-23 01:20:50 +0800124
Yong Wu76ce6542019-08-24 11:01:50 +0800125/*
126 * In M4U 4GB mode, the physical address is remapped as below:
127 *
128 * CPU Physical address:
129 * ====================
130 *
131 * 0 1G 2G 3G 4G 5G
132 * |---A---|---B---|---C---|---D---|---E---|
133 * +--I/O--+------------Memory-------------+
134 *
135 * IOMMU output physical address:
136 * =============================
137 *
138 * 4G 5G 6G 7G 8G
139 * |---E---|---B---|---C---|---D---|
140 * +------------Memory-------------+
141 *
142 * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
143 * bit32 of the CPU physical address always is needed to set, and for Region
144 * 'E', the CPU physical address keep as is.
145 * Additionally, The iommu consumers always use the CPU phyiscal address.
146 */
Yong Wub4dad402019-08-24 11:01:55 +0800147#define MTK_IOMMU_4GB_MODE_REMAP_BASE 0x140000000UL
Yong Wu76ce6542019-08-24 11:01:50 +0800148
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800149static LIST_HEAD(m4ulist); /* List all the M4U HWs */
150
151#define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list)
152
153/*
154 * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
155 * for the performance.
156 *
157 * Here always return the mtk_iommu_data of the first probed M4U where the
158 * iommu domain information is recorded.
159 */
160static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
161{
162 struct mtk_iommu_data *data;
163
164 for_each_m4u(data)
165 return data;
166
167 return NULL;
168}
169
Yong Wu0df4fab2016-02-23 01:20:50 +0800170static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
171{
172 return container_of(dom, struct mtk_iommu_domain, domain);
173}
174
175static void mtk_iommu_tlb_flush_all(void *cookie)
176{
177 struct mtk_iommu_data *data = cookie;
178
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800179 for_each_m4u(data) {
180 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
Chao Haob053bc72020-07-03 12:41:22 +0800181 data->base + data->plat_data->inv_sel_reg);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800182 writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
183 wmb(); /* Make sure the tlb flush all done */
184 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800185}
186
Yong Wu1f4fd622019-11-04 15:01:06 +0800187static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
Yong Wu67caf7e2019-11-04 15:01:05 +0800188 size_t granule, void *cookie)
Yong Wu0df4fab2016-02-23 01:20:50 +0800189{
190 struct mtk_iommu_data *data = cookie;
Yong Wu1f4fd622019-11-04 15:01:06 +0800191 unsigned long flags;
192 int ret;
193 u32 tmp;
Yong Wu0df4fab2016-02-23 01:20:50 +0800194
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800195 for_each_m4u(data) {
Yong Wu1f4fd622019-11-04 15:01:06 +0800196 spin_lock_irqsave(&data->tlb_lock, flags);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800197 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
Chao Haob053bc72020-07-03 12:41:22 +0800198 data->base + data->plat_data->inv_sel_reg);
Yong Wu0df4fab2016-02-23 01:20:50 +0800199
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800200 writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
201 writel_relaxed(iova + size - 1,
202 data->base + REG_MMU_INVLD_END_A);
203 writel_relaxed(F_MMU_INV_RANGE,
204 data->base + REG_MMU_INVALIDATE);
Yong Wu0df4fab2016-02-23 01:20:50 +0800205
Yong Wu1f4fd622019-11-04 15:01:06 +0800206 /* tlb sync */
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800207 ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
Yong Wuc90ae4a2019-11-04 15:01:08 +0800208 tmp, tmp != 0, 10, 1000);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800209 if (ret) {
210 dev_warn(data->dev,
211 "Partial TLB flush timed out, falling back to full flush\n");
212 mtk_iommu_tlb_flush_all(cookie);
213 }
214 /* Clear the CPE status */
215 writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
Yong Wu1f4fd622019-11-04 15:01:06 +0800216 spin_unlock_irqrestore(&data->tlb_lock, flags);
Yong Wu0df4fab2016-02-23 01:20:50 +0800217 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800218}
219
Will Deacon3951c412019-07-02 16:45:15 +0100220static void mtk_iommu_tlb_flush_page_nosync(struct iommu_iotlb_gather *gather,
221 unsigned long iova, size_t granule,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100222 void *cookie)
223{
Yong Wuda3cc912019-11-04 15:01:03 +0800224 struct mtk_iommu_data *data = cookie;
Yong Wua7a04ea2019-11-04 15:01:04 +0800225 struct iommu_domain *domain = &data->m4u_dom->domain;
Yong Wuda3cc912019-11-04 15:01:03 +0800226
Yong Wua7a04ea2019-11-04 15:01:04 +0800227 iommu_iotlb_gather_add_page(domain, gather, iova, granule);
Will Deaconabfd6fe2019-07-02 16:44:41 +0100228}
229
Will Deacon298f78892019-07-02 16:43:34 +0100230static const struct iommu_flush_ops mtk_iommu_flush_ops = {
Yong Wu0df4fab2016-02-23 01:20:50 +0800231 .tlb_flush_all = mtk_iommu_tlb_flush_all,
Yong Wu1f4fd622019-11-04 15:01:06 +0800232 .tlb_flush_walk = mtk_iommu_tlb_flush_range_sync,
233 .tlb_flush_leaf = mtk_iommu_tlb_flush_range_sync,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100234 .tlb_add_page = mtk_iommu_tlb_flush_page_nosync,
Yong Wu0df4fab2016-02-23 01:20:50 +0800235};
236
237static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
238{
239 struct mtk_iommu_data *data = dev_id;
240 struct mtk_iommu_domain *dom = data->m4u_dom;
241 u32 int_state, regval, fault_iova, fault_pa;
242 unsigned int fault_larb, fault_port;
243 bool layer, write;
244
245 /* Read error info from registers */
246 int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
Yong Wu15a01f42019-08-24 11:02:03 +0800247 if (int_state & F_REG_MMU0_FAULT_MASK) {
248 regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
249 fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
250 fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
251 } else {
252 regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
253 fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
254 fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
255 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800256 layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
257 write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
Yong Wu15a01f42019-08-24 11:02:03 +0800258 fault_larb = F_MMU_INT_ID_LARB_ID(regval);
259 fault_port = F_MMU_INT_ID_PORT_ID(regval);
Yong Wu0df4fab2016-02-23 01:20:50 +0800260
Yong Wub3e5eee72019-08-24 11:01:57 +0800261 fault_larb = data->plat_data->larbid_remap[fault_larb];
262
Yong Wu0df4fab2016-02-23 01:20:50 +0800263 if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
264 write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
265 dev_err_ratelimited(
266 data->dev,
267 "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
268 int_state, fault_iova, fault_pa, fault_larb, fault_port,
269 layer, write ? "write" : "read");
270 }
271
272 /* Interrupt clear */
273 regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
274 regval |= F_INT_CLR_BIT;
275 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
276
277 mtk_iommu_tlb_flush_all(data);
278
279 return IRQ_HANDLED;
280}
281
282static void mtk_iommu_config(struct mtk_iommu_data *data,
283 struct device *dev, bool enable)
284{
Yong Wu0df4fab2016-02-23 01:20:50 +0800285 struct mtk_smi_larb_iommu *larb_mmu;
286 unsigned int larbid, portid;
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100287 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100288 int i;
Yong Wu0df4fab2016-02-23 01:20:50 +0800289
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100290 for (i = 0; i < fwspec->num_ids; ++i) {
291 larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
292 portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
Yong Wu1ee9feb2019-08-24 11:02:08 +0800293 larb_mmu = &data->larb_imu[larbid];
Yong Wu0df4fab2016-02-23 01:20:50 +0800294
295 dev_dbg(dev, "%s iommu port: %d\n",
296 enable ? "enable" : "disable", portid);
297
298 if (enable)
299 larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
300 else
301 larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
302 }
303}
304
Yong Wu4b00f5a2017-08-21 19:00:18 +0800305static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
Yong Wu0df4fab2016-02-23 01:20:50 +0800306{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800307 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800308
Yong Wu0df4fab2016-02-23 01:20:50 +0800309 dom->cfg = (struct io_pgtable_cfg) {
310 .quirks = IO_PGTABLE_QUIRK_ARM_NS |
311 IO_PGTABLE_QUIRK_NO_PERMS |
Yong Wub4dad402019-08-24 11:01:55 +0800312 IO_PGTABLE_QUIRK_TLBI_ON_MAP |
313 IO_PGTABLE_QUIRK_ARM_MTK_EXT,
Yong Wu0df4fab2016-02-23 01:20:50 +0800314 .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
315 .ias = 32,
Yong Wub4dad402019-08-24 11:01:55 +0800316 .oas = 34,
Will Deacon298f78892019-07-02 16:43:34 +0100317 .tlb = &mtk_iommu_flush_ops,
Yong Wu0df4fab2016-02-23 01:20:50 +0800318 .iommu_dev = data->dev,
319 };
320
321 dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
322 if (!dom->iop) {
323 dev_err(data->dev, "Failed to alloc io pgtable\n");
324 return -EINVAL;
325 }
326
327 /* Update our support page sizes bitmap */
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100328 dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
Yong Wu0df4fab2016-02-23 01:20:50 +0800329 return 0;
330}
331
332static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
333{
334 struct mtk_iommu_domain *dom;
335
336 if (type != IOMMU_DOMAIN_DMA)
337 return NULL;
338
339 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
340 if (!dom)
341 return NULL;
342
Yong Wu4b00f5a2017-08-21 19:00:18 +0800343 if (iommu_get_dma_cookie(&dom->domain))
344 goto free_dom;
345
346 if (mtk_iommu_domain_finalise(dom))
347 goto put_dma_cookie;
Yong Wu0df4fab2016-02-23 01:20:50 +0800348
349 dom->domain.geometry.aperture_start = 0;
350 dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
351 dom->domain.geometry.force_aperture = true;
352
353 return &dom->domain;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800354
355put_dma_cookie:
356 iommu_put_dma_cookie(&dom->domain);
357free_dom:
358 kfree(dom);
359 return NULL;
Yong Wu0df4fab2016-02-23 01:20:50 +0800360}
361
362static void mtk_iommu_domain_free(struct iommu_domain *domain)
363{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800364 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
365
366 free_io_pgtable_ops(dom->iop);
Yong Wu0df4fab2016-02-23 01:20:50 +0800367 iommu_put_dma_cookie(domain);
368 kfree(to_mtk_domain(domain));
369}
370
371static int mtk_iommu_attach_device(struct iommu_domain *domain,
372 struct device *dev)
373{
Joerg Roedel3524b552020-03-26 16:08:38 +0100374 struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800375 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu0df4fab2016-02-23 01:20:50 +0800376
Yong Wu4b00f5a2017-08-21 19:00:18 +0800377 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800378 return -ENODEV;
379
Yong Wu4b00f5a2017-08-21 19:00:18 +0800380 /* Update the pgtable base address register of the M4U HW */
Yong Wu0df4fab2016-02-23 01:20:50 +0800381 if (!data->m4u_dom) {
382 data->m4u_dom = dom;
Robin Murphyd1e5f262019-10-25 19:08:37 +0100383 writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
Yong Wu4b00f5a2017-08-21 19:00:18 +0800384 data->base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800385 }
386
Yong Wu4b00f5a2017-08-21 19:00:18 +0800387 mtk_iommu_config(data, dev, true);
Yong Wu0df4fab2016-02-23 01:20:50 +0800388 return 0;
389}
390
391static void mtk_iommu_detach_device(struct iommu_domain *domain,
392 struct device *dev)
393{
Joerg Roedel3524b552020-03-26 16:08:38 +0100394 struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800395
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100396 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800397 return;
398
Yong Wu0df4fab2016-02-23 01:20:50 +0800399 mtk_iommu_config(data, dev, false);
400}
401
402static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
Tom Murphy781ca2d2019-09-08 09:56:38 -0700403 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Yong Wu0df4fab2016-02-23 01:20:50 +0800404{
405 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wub4dad402019-08-24 11:01:55 +0800406 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800407
Yong Wub4dad402019-08-24 11:01:55 +0800408 /* The "4GB mode" M4U physically can not use the lower remap of Dram. */
409 if (data->enable_4GB)
410 paddr |= BIT_ULL(32);
411
Yong Wu60829b42019-11-04 15:01:07 +0800412 /* Synchronize with the tlb_lock */
413 return dom->iop->map(dom->iop, iova, paddr, size, prot);
Yong Wu0df4fab2016-02-23 01:20:50 +0800414}
415
416static size_t mtk_iommu_unmap(struct iommu_domain *domain,
Will Deacon56f8af52019-07-02 16:44:06 +0100417 unsigned long iova, size_t size,
418 struct iommu_iotlb_gather *gather)
Yong Wu0df4fab2016-02-23 01:20:50 +0800419{
420 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu0df4fab2016-02-23 01:20:50 +0800421
Yong Wu60829b42019-11-04 15:01:07 +0800422 return dom->iop->unmap(dom->iop, iova, size, gather);
Yong Wu0df4fab2016-02-23 01:20:50 +0800423}
424
Will Deacon56f8af52019-07-02 16:44:06 +0100425static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
426{
Yong Wu20091222019-11-04 15:01:02 +0800427 mtk_iommu_tlb_flush_all(mtk_iommu_get_m4u_data());
Will Deacon56f8af52019-07-02 16:44:06 +0100428}
429
430static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
431 struct iommu_iotlb_gather *gather)
Robin Murphy4d689b62017-09-28 15:55:02 +0100432{
Yong Wuda3cc912019-11-04 15:01:03 +0800433 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wua7a04ea2019-11-04 15:01:04 +0800434 size_t length = gather->end - gather->start;
Yong Wuda3cc912019-11-04 15:01:03 +0800435
Yong Wua7a04ea2019-11-04 15:01:04 +0800436 if (gather->start == ULONG_MAX)
437 return;
438
Yong Wu1f4fd622019-11-04 15:01:06 +0800439 mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize,
Yong Wu67caf7e2019-11-04 15:01:05 +0800440 data);
Robin Murphy4d689b62017-09-28 15:55:02 +0100441}
442
Yong Wu0df4fab2016-02-23 01:20:50 +0800443static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
444 dma_addr_t iova)
445{
446 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800447 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800448 phys_addr_t pa;
449
Yong Wu0df4fab2016-02-23 01:20:50 +0800450 pa = dom->iop->iova_to_phys(dom->iop, iova);
Yong Wub4dad402019-08-24 11:01:55 +0800451 if (data->enable_4GB && pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
452 pa &= ~BIT_ULL(32);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800453
Yong Wu0df4fab2016-02-23 01:20:50 +0800454 return pa;
455}
456
Joerg Roedel80e45922020-04-29 15:37:00 +0200457static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800458{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100459 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100460 struct mtk_iommu_data *data;
Yong Wu0df4fab2016-02-23 01:20:50 +0800461
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100462 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Joerg Roedel80e45922020-04-29 15:37:00 +0200463 return ERR_PTR(-ENODEV); /* Not a iommu client device */
Yong Wu0df4fab2016-02-23 01:20:50 +0800464
Joerg Roedel3524b552020-03-26 16:08:38 +0100465 data = dev_iommu_priv_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100466
Joerg Roedel80e45922020-04-29 15:37:00 +0200467 return &data->iommu;
Yong Wu0df4fab2016-02-23 01:20:50 +0800468}
469
Joerg Roedel80e45922020-04-29 15:37:00 +0200470static void mtk_iommu_release_device(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800471{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100472 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100473
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100474 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Yong Wu0df4fab2016-02-23 01:20:50 +0800475 return;
476
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100477 iommu_fwspec_free(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800478}
479
480static struct iommu_group *mtk_iommu_device_group(struct device *dev)
481{
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800482 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800483
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100484 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800485 return ERR_PTR(-ENODEV);
486
487 /* All the client devices are in the same m4u iommu-group */
Yong Wu0df4fab2016-02-23 01:20:50 +0800488 if (!data->m4u_group) {
489 data->m4u_group = iommu_group_alloc();
490 if (IS_ERR(data->m4u_group))
491 dev_err(dev, "Failed to allocate M4U IOMMU group\n");
Robin Murphy3a8d40b2016-11-11 17:59:24 +0000492 } else {
493 iommu_group_ref_get(data->m4u_group);
Yong Wu0df4fab2016-02-23 01:20:50 +0800494 }
495 return data->m4u_group;
496}
497
498static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
499{
Yong Wu0df4fab2016-02-23 01:20:50 +0800500 struct platform_device *m4updev;
501
502 if (args->args_count != 1) {
503 dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
504 args->args_count);
505 return -EINVAL;
506 }
507
Joerg Roedel3524b552020-03-26 16:08:38 +0100508 if (!dev_iommu_priv_get(dev)) {
Yong Wu0df4fab2016-02-23 01:20:50 +0800509 /* Get the m4u device */
510 m4updev = of_find_device_by_node(args->np);
Yong Wu0df4fab2016-02-23 01:20:50 +0800511 if (WARN_ON(!m4updev))
512 return -EINVAL;
513
Joerg Roedel3524b552020-03-26 16:08:38 +0100514 dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
Yong Wu0df4fab2016-02-23 01:20:50 +0800515 }
516
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100517 return iommu_fwspec_add_ids(dev, args->args, 1);
Yong Wu0df4fab2016-02-23 01:20:50 +0800518}
519
Arvind Yadavb65f5012018-10-18 19:13:38 +0800520static const struct iommu_ops mtk_iommu_ops = {
Yong Wu0df4fab2016-02-23 01:20:50 +0800521 .domain_alloc = mtk_iommu_domain_alloc,
522 .domain_free = mtk_iommu_domain_free,
523 .attach_dev = mtk_iommu_attach_device,
524 .detach_dev = mtk_iommu_detach_device,
525 .map = mtk_iommu_map,
526 .unmap = mtk_iommu_unmap,
Will Deacon56f8af52019-07-02 16:44:06 +0100527 .flush_iotlb_all = mtk_iommu_flush_iotlb_all,
Robin Murphy4d689b62017-09-28 15:55:02 +0100528 .iotlb_sync = mtk_iommu_iotlb_sync,
Yong Wu0df4fab2016-02-23 01:20:50 +0800529 .iova_to_phys = mtk_iommu_iova_to_phys,
Joerg Roedel80e45922020-04-29 15:37:00 +0200530 .probe_device = mtk_iommu_probe_device,
531 .release_device = mtk_iommu_release_device,
Yong Wu0df4fab2016-02-23 01:20:50 +0800532 .device_group = mtk_iommu_device_group,
533 .of_xlate = mtk_iommu_of_xlate,
534 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
535};
536
537static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
538{
539 u32 regval;
540 int ret;
541
542 ret = clk_prepare_enable(data->bclk);
543 if (ret) {
544 dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
545 return ret;
546 }
547
Yong Wucecdce92019-08-24 11:01:47 +0800548 if (data->plat_data->m4u_plat == M4U_MT8173)
Yong Wuacb3c922019-08-24 11:01:58 +0800549 regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
550 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
551 else
552 regval = F_MMU_TF_PROT_TO_PROGRAM_ADDR;
Yong Wu0df4fab2016-02-23 01:20:50 +0800553 writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
554
555 regval = F_L2_MULIT_HIT_EN |
556 F_TABLE_WALK_FAULT_INT_EN |
557 F_PREETCH_FIFO_OVERFLOW_INT_EN |
558 F_MISS_FIFO_OVERFLOW_INT_EN |
559 F_PREFETCH_FIFO_ERR_INT_EN |
560 F_MISS_FIFO_ERR_INT_EN;
561 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
562
563 regval = F_INT_TRANSLATION_FAULT |
564 F_INT_MAIN_MULTI_HIT_FAULT |
565 F_INT_INVALID_PA_FAULT |
566 F_INT_ENTRY_REPLACEMENT_FAULT |
567 F_INT_TLB_MISS_FAULT |
568 F_INT_MISS_TRANSACTION_FIFO_FAULT |
569 F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
570 writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
571
Yong Wucecdce92019-08-24 11:01:47 +0800572 if (data->plat_data->m4u_plat == M4U_MT8173)
Yong Wu70ca6082018-03-18 09:52:54 +0800573 regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
574 else
575 regval = lower_32_bits(data->protect_base) |
576 upper_32_bits(data->protect_base);
577 writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
578
Chao Hao6b717792020-07-03 12:41:20 +0800579 if (data->enable_4GB &&
580 MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
Yong Wu30e2fcc2017-08-21 19:00:20 +0800581 /*
582 * If 4GB mode is enabled, the validate PA range is from
583 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
584 */
585 regval = F_MMU_VLD_PA_RNG(7, 4);
586 writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
587 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800588 writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
Yong Wue6dec922017-08-21 19:00:16 +0800589
Chao Hao6b717792020-07-03 12:41:20 +0800590 if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
Chao Hao75eed352020-07-03 12:41:19 +0800591 /* The register is called STANDARD_AXI_MODE in this case */
Chao Hao4bb2bf42020-07-03 12:41:21 +0800592 regval = 0;
593 } else {
594 regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
595 regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
596 if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
597 regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
Chao Hao75eed352020-07-03 12:41:19 +0800598 }
Chao Hao4bb2bf42020-07-03 12:41:21 +0800599 writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800600
601 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
602 dev_name(data->dev), (void *)data)) {
603 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
604 clk_disable_unprepare(data->bclk);
605 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
606 return -ENODEV;
607 }
608
609 return 0;
610}
611
Yong Wu0df4fab2016-02-23 01:20:50 +0800612static const struct component_master_ops mtk_iommu_com_ops = {
613 .bind = mtk_iommu_bind,
614 .unbind = mtk_iommu_unbind,
615};
616
617static int mtk_iommu_probe(struct platform_device *pdev)
618{
619 struct mtk_iommu_data *data;
620 struct device *dev = &pdev->dev;
621 struct resource *res;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100622 resource_size_t ioaddr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800623 struct component_match *match = NULL;
624 void *protect;
Andrzej Hajda0b6c0ad2016-03-01 10:36:23 +0100625 int i, larb_nr, ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800626
627 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
628 if (!data)
629 return -ENOMEM;
630 data->dev = dev;
Yong Wucecdce92019-08-24 11:01:47 +0800631 data->plat_data = of_device_get_match_data(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800632
633 /* Protect memory. HW will access here while translation fault.*/
634 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
635 if (!protect)
636 return -ENOMEM;
637 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
638
Yong Wu01e23c92016-03-14 06:01:11 +0800639 /* Whether the current dram is over 4GB */
Yong Wu41939982017-08-24 15:42:12 +0800640 data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT));
Chao Hao6b717792020-07-03 12:41:20 +0800641 if (!MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
Yong Wub4dad402019-08-24 11:01:55 +0800642 data->enable_4GB = false;
Yong Wu01e23c92016-03-14 06:01:11 +0800643
Yong Wu0df4fab2016-02-23 01:20:50 +0800644 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
645 data->base = devm_ioremap_resource(dev, res);
646 if (IS_ERR(data->base))
647 return PTR_ERR(data->base);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100648 ioaddr = res->start;
Yong Wu0df4fab2016-02-23 01:20:50 +0800649
650 data->irq = platform_get_irq(pdev, 0);
651 if (data->irq < 0)
652 return data->irq;
653
Chao Hao6b717792020-07-03 12:41:20 +0800654 if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
Yong Wu2aa4c252019-08-24 11:01:56 +0800655 data->bclk = devm_clk_get(dev, "bclk");
656 if (IS_ERR(data->bclk))
657 return PTR_ERR(data->bclk);
658 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800659
660 larb_nr = of_count_phandle_with_args(dev->of_node,
661 "mediatek,larbs", NULL);
662 if (larb_nr < 0)
663 return larb_nr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800664
665 for (i = 0; i < larb_nr; i++) {
666 struct device_node *larbnode;
667 struct platform_device *plarbdev;
Yong Wue6dec922017-08-21 19:00:16 +0800668 u32 id;
Yong Wu0df4fab2016-02-23 01:20:50 +0800669
670 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
671 if (!larbnode)
672 return -EINVAL;
673
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800674 if (!of_device_is_available(larbnode)) {
675 of_node_put(larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800676 continue;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800677 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800678
Yong Wue6dec922017-08-21 19:00:16 +0800679 ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
680 if (ret)/* The id is consecutive if there is no this property */
681 id = i;
682
Yong Wu0df4fab2016-02-23 01:20:50 +0800683 plarbdev = of_find_device_by_node(larbnode);
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800684 if (!plarbdev) {
685 of_node_put(larbnode);
Yong Wue6dec922017-08-21 19:00:16 +0800686 return -EPROBE_DEFER;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800687 }
Yong Wu1ee9feb2019-08-24 11:02:08 +0800688 data->larb_imu[id].dev = &plarbdev->dev;
Yong Wu0df4fab2016-02-23 01:20:50 +0800689
Russell King00c7c812016-10-19 11:30:34 +0100690 component_match_add_release(dev, &match, release_of,
691 compare_of, larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800692 }
693
694 platform_set_drvdata(pdev, data);
695
696 ret = mtk_iommu_hw_init(data);
697 if (ret)
698 return ret;
699
Joerg Roedelb16c0172017-02-03 12:57:32 +0100700 ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
701 "mtk-iommu.%pa", &ioaddr);
702 if (ret)
703 return ret;
704
705 iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
706 iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
707
708 ret = iommu_device_register(&data->iommu);
709 if (ret)
710 return ret;
711
Yong Wuda3cc912019-11-04 15:01:03 +0800712 spin_lock_init(&data->tlb_lock);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800713 list_add_tail(&data->list, &m4ulist);
714
Yong Wu0df4fab2016-02-23 01:20:50 +0800715 if (!iommu_present(&platform_bus_type))
716 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
717
718 return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
719}
720
721static int mtk_iommu_remove(struct platform_device *pdev)
722{
723 struct mtk_iommu_data *data = platform_get_drvdata(pdev);
724
Joerg Roedelb16c0172017-02-03 12:57:32 +0100725 iommu_device_sysfs_remove(&data->iommu);
726 iommu_device_unregister(&data->iommu);
727
Yong Wu0df4fab2016-02-23 01:20:50 +0800728 if (iommu_present(&platform_bus_type))
729 bus_set_iommu(&platform_bus_type, NULL);
730
Yong Wu0df4fab2016-02-23 01:20:50 +0800731 clk_disable_unprepare(data->bclk);
732 devm_free_irq(&pdev->dev, data->irq, data);
733 component_master_del(&pdev->dev, &mtk_iommu_com_ops);
734 return 0;
735}
736
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100737static int __maybe_unused mtk_iommu_suspend(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800738{
739 struct mtk_iommu_data *data = dev_get_drvdata(dev);
740 struct mtk_iommu_suspend_reg *reg = &data->reg;
741 void __iomem *base = data->base;
742
Chao Hao75eed352020-07-03 12:41:19 +0800743 reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800744 reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
745 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
746 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
747 reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800748 reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
Yong Wub9475b32019-08-24 11:02:06 +0800749 reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
Yong Wu6254b642017-08-21 19:00:19 +0800750 clk_disable_unprepare(data->bclk);
Yong Wu0df4fab2016-02-23 01:20:50 +0800751 return 0;
752}
753
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100754static int __maybe_unused mtk_iommu_resume(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800755{
756 struct mtk_iommu_data *data = dev_get_drvdata(dev);
757 struct mtk_iommu_suspend_reg *reg = &data->reg;
Yong Wu907ba6a2019-08-24 11:02:02 +0800758 struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
Yong Wu0df4fab2016-02-23 01:20:50 +0800759 void __iomem *base = data->base;
Yong Wu6254b642017-08-21 19:00:19 +0800760 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800761
Yong Wu6254b642017-08-21 19:00:19 +0800762 ret = clk_prepare_enable(data->bclk);
763 if (ret) {
764 dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
765 return ret;
766 }
Chao Hao75eed352020-07-03 12:41:19 +0800767 writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800768 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
769 writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
770 writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
771 writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800772 writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
Yong Wub9475b32019-08-24 11:02:06 +0800773 writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
Yong Wu907ba6a2019-08-24 11:02:02 +0800774 if (m4u_dom)
Robin Murphyd1e5f262019-10-25 19:08:37 +0100775 writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
Yong Wue6dec922017-08-21 19:00:16 +0800776 base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800777 return 0;
778}
779
Yong Wue6dec922017-08-21 19:00:16 +0800780static const struct dev_pm_ops mtk_iommu_pm_ops = {
Yong Wu6254b642017-08-21 19:00:19 +0800781 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
Yong Wu0df4fab2016-02-23 01:20:50 +0800782};
783
Yong Wucecdce92019-08-24 11:01:47 +0800784static const struct mtk_iommu_plat_data mt2712_data = {
785 .m4u_plat = M4U_MT2712,
Chao Hao6b717792020-07-03 12:41:20 +0800786 .flags = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG,
Chao Haob053bc72020-07-03 12:41:22 +0800787 .inv_sel_reg = REG_MMU_INV_SEL_GEN1,
Yong Wub3e5eee72019-08-24 11:01:57 +0800788 .larbid_remap = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Yong Wucecdce92019-08-24 11:01:47 +0800789};
790
791static const struct mtk_iommu_plat_data mt8173_data = {
792 .m4u_plat = M4U_MT8173,
Chao Hao6b717792020-07-03 12:41:20 +0800793 .flags = HAS_4GB_MODE | HAS_BCLK | RESET_AXI,
Chao Haob053bc72020-07-03 12:41:22 +0800794 .inv_sel_reg = REG_MMU_INV_SEL_GEN1,
Yong Wub3e5eee72019-08-24 11:01:57 +0800795 .larbid_remap = {0, 1, 2, 3, 4, 5}, /* Linear mapping. */
Yong Wucecdce92019-08-24 11:01:47 +0800796};
797
Yong Wu907ba6a2019-08-24 11:02:02 +0800798static const struct mtk_iommu_plat_data mt8183_data = {
799 .m4u_plat = M4U_MT8183,
Chao Hao6b717792020-07-03 12:41:20 +0800800 .flags = RESET_AXI,
Chao Haob053bc72020-07-03 12:41:22 +0800801 .inv_sel_reg = REG_MMU_INV_SEL_GEN1,
Yong Wu907ba6a2019-08-24 11:02:02 +0800802 .larbid_remap = {0, 4, 5, 6, 7, 2, 3, 1},
803};
804
Yong Wu0df4fab2016-02-23 01:20:50 +0800805static const struct of_device_id mtk_iommu_of_ids[] = {
Yong Wucecdce92019-08-24 11:01:47 +0800806 { .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
807 { .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
Yong Wu907ba6a2019-08-24 11:02:02 +0800808 { .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
Yong Wu0df4fab2016-02-23 01:20:50 +0800809 {}
810};
811
812static struct platform_driver mtk_iommu_driver = {
813 .probe = mtk_iommu_probe,
814 .remove = mtk_iommu_remove,
815 .driver = {
816 .name = "mtk-iommu",
Yong Wue6dec922017-08-21 19:00:16 +0800817 .of_match_table = of_match_ptr(mtk_iommu_of_ids),
Yong Wu0df4fab2016-02-23 01:20:50 +0800818 .pm = &mtk_iommu_pm_ops,
819 }
820};
821
Yong Wue6dec922017-08-21 19:00:16 +0800822static int __init mtk_iommu_init(void)
Yong Wu0df4fab2016-02-23 01:20:50 +0800823{
824 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800825
826 ret = platform_driver_register(&mtk_iommu_driver);
Yong Wue6dec922017-08-21 19:00:16 +0800827 if (ret != 0)
828 pr_err("Failed to register MTK IOMMU driver\n");
Yong Wu0df4fab2016-02-23 01:20:50 +0800829
Yong Wue6dec922017-08-21 19:00:16 +0800830 return ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800831}
832
Yong Wue6dec922017-08-21 19:00:16 +0800833subsys_initcall(mtk_iommu_init)