blob: 95945f467c03f949bc07abc801152e181f2ecfce [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
40#define REG_MMU_INV_SEL 0x038
41#define F_INVLD_EN0 BIT(0)
42#define F_INVLD_EN1 BIT(1)
43
44#define REG_MMU_STANDARD_AXI_MODE 0x048
45#define REG_MMU_DCM_DIS 0x050
46
47#define REG_MMU_CTRL_REG 0x110
Yong Wuacb3c922019-08-24 11:01:58 +080048#define F_MMU_TF_PROT_TO_PROGRAM_ADDR (2 << 4)
Yong Wu0df4fab2016-02-23 01:20:50 +080049#define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
Yong Wuacb3c922019-08-24 11:01:58 +080050#define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173 (2 << 5)
Yong Wu0df4fab2016-02-23 01:20:50 +080051
52#define REG_MMU_IVRP_PADDR 0x114
Yong Wu70ca6082018-03-18 09:52:54 +080053
Yong Wu30e2fcc2017-08-21 19:00:20 +080054#define REG_MMU_VLD_PA_RNG 0x118
55#define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA))
Yong Wu0df4fab2016-02-23 01:20:50 +080056
57#define REG_MMU_INT_CONTROL0 0x120
58#define F_L2_MULIT_HIT_EN BIT(0)
59#define F_TABLE_WALK_FAULT_INT_EN BIT(1)
60#define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
61#define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
62#define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
63#define F_MISS_FIFO_ERR_INT_EN BIT(6)
64#define F_INT_CLR_BIT BIT(12)
65
66#define REG_MMU_INT_MAIN_CONTROL 0x124
Yong Wu15a01f42019-08-24 11:02:03 +080067 /* mmu0 | mmu1 */
68#define F_INT_TRANSLATION_FAULT (BIT(0) | BIT(7))
69#define F_INT_MAIN_MULTI_HIT_FAULT (BIT(1) | BIT(8))
70#define F_INT_INVALID_PA_FAULT (BIT(2) | BIT(9))
71#define F_INT_ENTRY_REPLACEMENT_FAULT (BIT(3) | BIT(10))
72#define F_INT_TLB_MISS_FAULT (BIT(4) | BIT(11))
73#define F_INT_MISS_TRANSACTION_FIFO_FAULT (BIT(5) | BIT(12))
74#define F_INT_PRETETCH_TRANSATION_FIFO_FAULT (BIT(6) | BIT(13))
Yong Wu0df4fab2016-02-23 01:20:50 +080075
76#define REG_MMU_CPE_DONE 0x12C
77
78#define REG_MMU_FAULT_ST1 0x134
Yong Wu15a01f42019-08-24 11:02:03 +080079#define F_REG_MMU0_FAULT_MASK GENMASK(6, 0)
80#define F_REG_MMU1_FAULT_MASK GENMASK(13, 7)
Yong Wu0df4fab2016-02-23 01:20:50 +080081
Yong Wu15a01f42019-08-24 11:02:03 +080082#define REG_MMU0_FAULT_VA 0x13c
Yong Wu0df4fab2016-02-23 01:20:50 +080083#define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
84#define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
85
Yong Wu15a01f42019-08-24 11:02:03 +080086#define REG_MMU0_INVLD_PA 0x140
87#define REG_MMU1_FAULT_VA 0x144
88#define REG_MMU1_INVLD_PA 0x148
89#define REG_MMU0_INT_ID 0x150
90#define REG_MMU1_INT_ID 0x154
91#define F_MMU_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
92#define F_MMU_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
Yong Wu0df4fab2016-02-23 01:20:50 +080093
94#define MTK_PROTECT_PA_ALIGN 128
95
Yong Wua9467d92017-08-21 19:00:15 +080096/*
97 * Get the local arbiter ID and the portid within the larb arbiter
98 * from mtk_m4u_id which is defined by MTK_M4U_ID.
99 */
Yong Wue6dec922017-08-21 19:00:16 +0800100#define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf)
Yong Wua9467d92017-08-21 19:00:15 +0800101#define MTK_M4U_TO_PORT(id) ((id) & 0x1f)
102
Yong Wu0df4fab2016-02-23 01:20:50 +0800103struct mtk_iommu_domain {
Yong Wu0df4fab2016-02-23 01:20:50 +0800104 struct io_pgtable_cfg cfg;
105 struct io_pgtable_ops *iop;
106
107 struct iommu_domain domain;
108};
109
Arvind Yadavb65f5012018-10-18 19:13:38 +0800110static const struct iommu_ops mtk_iommu_ops;
Yong Wu0df4fab2016-02-23 01:20:50 +0800111
Yong Wu76ce6542019-08-24 11:01:50 +0800112/*
113 * In M4U 4GB mode, the physical address is remapped as below:
114 *
115 * CPU Physical address:
116 * ====================
117 *
118 * 0 1G 2G 3G 4G 5G
119 * |---A---|---B---|---C---|---D---|---E---|
120 * +--I/O--+------------Memory-------------+
121 *
122 * IOMMU output physical address:
123 * =============================
124 *
125 * 4G 5G 6G 7G 8G
126 * |---E---|---B---|---C---|---D---|
127 * +------------Memory-------------+
128 *
129 * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
130 * bit32 of the CPU physical address always is needed to set, and for Region
131 * 'E', the CPU physical address keep as is.
132 * Additionally, The iommu consumers always use the CPU phyiscal address.
133 */
Yong Wub4dad402019-08-24 11:01:55 +0800134#define MTK_IOMMU_4GB_MODE_REMAP_BASE 0x140000000UL
Yong Wu76ce6542019-08-24 11:01:50 +0800135
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800136static LIST_HEAD(m4ulist); /* List all the M4U HWs */
137
138#define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list)
139
140/*
141 * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
142 * for the performance.
143 *
144 * Here always return the mtk_iommu_data of the first probed M4U where the
145 * iommu domain information is recorded.
146 */
147static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
148{
149 struct mtk_iommu_data *data;
150
151 for_each_m4u(data)
152 return data;
153
154 return NULL;
155}
156
Yong Wu0df4fab2016-02-23 01:20:50 +0800157static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
158{
159 return container_of(dom, struct mtk_iommu_domain, domain);
160}
161
162static void mtk_iommu_tlb_flush_all(void *cookie)
163{
164 struct mtk_iommu_data *data = cookie;
165
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800166 for_each_m4u(data) {
167 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
168 data->base + REG_MMU_INV_SEL);
169 writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
170 wmb(); /* Make sure the tlb flush all done */
171 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800172}
173
Yong Wu1f4fd622019-11-04 15:01:06 +0800174static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
Yong Wu67caf7e2019-11-04 15:01:05 +0800175 size_t granule, void *cookie)
Yong Wu0df4fab2016-02-23 01:20:50 +0800176{
177 struct mtk_iommu_data *data = cookie;
Yong Wu1f4fd622019-11-04 15:01:06 +0800178 unsigned long flags;
179 int ret;
180 u32 tmp;
Yong Wu0df4fab2016-02-23 01:20:50 +0800181
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800182 for_each_m4u(data) {
Yong Wu1f4fd622019-11-04 15:01:06 +0800183 spin_lock_irqsave(&data->tlb_lock, flags);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800184 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
185 data->base + REG_MMU_INV_SEL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800186
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800187 writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
188 writel_relaxed(iova + size - 1,
189 data->base + REG_MMU_INVLD_END_A);
190 writel_relaxed(F_MMU_INV_RANGE,
191 data->base + REG_MMU_INVALIDATE);
Yong Wu0df4fab2016-02-23 01:20:50 +0800192
Yong Wu1f4fd622019-11-04 15:01:06 +0800193 /* tlb sync */
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800194 ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
Yong Wuc90ae4a2019-11-04 15:01:08 +0800195 tmp, tmp != 0, 10, 1000);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800196 if (ret) {
197 dev_warn(data->dev,
198 "Partial TLB flush timed out, falling back to full flush\n");
199 mtk_iommu_tlb_flush_all(cookie);
200 }
201 /* Clear the CPE status */
202 writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
Yong Wu1f4fd622019-11-04 15:01:06 +0800203 spin_unlock_irqrestore(&data->tlb_lock, flags);
Yong Wu0df4fab2016-02-23 01:20:50 +0800204 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800205}
206
Will Deacon3951c412019-07-02 16:45:15 +0100207static void mtk_iommu_tlb_flush_page_nosync(struct iommu_iotlb_gather *gather,
208 unsigned long iova, size_t granule,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100209 void *cookie)
210{
Yong Wuda3cc912019-11-04 15:01:03 +0800211 struct mtk_iommu_data *data = cookie;
Yong Wua7a04ea2019-11-04 15:01:04 +0800212 struct iommu_domain *domain = &data->m4u_dom->domain;
Yong Wuda3cc912019-11-04 15:01:03 +0800213
Yong Wua7a04ea2019-11-04 15:01:04 +0800214 iommu_iotlb_gather_add_page(domain, gather, iova, granule);
Will Deaconabfd6fe2019-07-02 16:44:41 +0100215}
216
Will Deacon298f78892019-07-02 16:43:34 +0100217static const struct iommu_flush_ops mtk_iommu_flush_ops = {
Yong Wu0df4fab2016-02-23 01:20:50 +0800218 .tlb_flush_all = mtk_iommu_tlb_flush_all,
Yong Wu1f4fd622019-11-04 15:01:06 +0800219 .tlb_flush_walk = mtk_iommu_tlb_flush_range_sync,
220 .tlb_flush_leaf = mtk_iommu_tlb_flush_range_sync,
Will Deaconabfd6fe2019-07-02 16:44:41 +0100221 .tlb_add_page = mtk_iommu_tlb_flush_page_nosync,
Yong Wu0df4fab2016-02-23 01:20:50 +0800222};
223
224static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
225{
226 struct mtk_iommu_data *data = dev_id;
227 struct mtk_iommu_domain *dom = data->m4u_dom;
228 u32 int_state, regval, fault_iova, fault_pa;
229 unsigned int fault_larb, fault_port;
230 bool layer, write;
231
232 /* Read error info from registers */
233 int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
Yong Wu15a01f42019-08-24 11:02:03 +0800234 if (int_state & F_REG_MMU0_FAULT_MASK) {
235 regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
236 fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
237 fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
238 } else {
239 regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
240 fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
241 fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
242 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800243 layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
244 write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
Yong Wu15a01f42019-08-24 11:02:03 +0800245 fault_larb = F_MMU_INT_ID_LARB_ID(regval);
246 fault_port = F_MMU_INT_ID_PORT_ID(regval);
Yong Wu0df4fab2016-02-23 01:20:50 +0800247
Yong Wub3e5eee72019-08-24 11:01:57 +0800248 fault_larb = data->plat_data->larbid_remap[fault_larb];
249
Yong Wu0df4fab2016-02-23 01:20:50 +0800250 if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
251 write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
252 dev_err_ratelimited(
253 data->dev,
254 "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
255 int_state, fault_iova, fault_pa, fault_larb, fault_port,
256 layer, write ? "write" : "read");
257 }
258
259 /* Interrupt clear */
260 regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
261 regval |= F_INT_CLR_BIT;
262 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
263
264 mtk_iommu_tlb_flush_all(data);
265
266 return IRQ_HANDLED;
267}
268
269static void mtk_iommu_config(struct mtk_iommu_data *data,
270 struct device *dev, bool enable)
271{
Yong Wu0df4fab2016-02-23 01:20:50 +0800272 struct mtk_smi_larb_iommu *larb_mmu;
273 unsigned int larbid, portid;
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100274 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100275 int i;
Yong Wu0df4fab2016-02-23 01:20:50 +0800276
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100277 for (i = 0; i < fwspec->num_ids; ++i) {
278 larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
279 portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
Yong Wu1ee9feb2019-08-24 11:02:08 +0800280 larb_mmu = &data->larb_imu[larbid];
Yong Wu0df4fab2016-02-23 01:20:50 +0800281
282 dev_dbg(dev, "%s iommu port: %d\n",
283 enable ? "enable" : "disable", portid);
284
285 if (enable)
286 larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
287 else
288 larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
289 }
290}
291
Yong Wu4b00f5a2017-08-21 19:00:18 +0800292static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
Yong Wu0df4fab2016-02-23 01:20:50 +0800293{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800294 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800295
Yong Wu0df4fab2016-02-23 01:20:50 +0800296 dom->cfg = (struct io_pgtable_cfg) {
297 .quirks = IO_PGTABLE_QUIRK_ARM_NS |
298 IO_PGTABLE_QUIRK_NO_PERMS |
Yong Wub4dad402019-08-24 11:01:55 +0800299 IO_PGTABLE_QUIRK_TLBI_ON_MAP |
300 IO_PGTABLE_QUIRK_ARM_MTK_EXT,
Yong Wu0df4fab2016-02-23 01:20:50 +0800301 .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
302 .ias = 32,
Yong Wub4dad402019-08-24 11:01:55 +0800303 .oas = 34,
Will Deacon298f78892019-07-02 16:43:34 +0100304 .tlb = &mtk_iommu_flush_ops,
Yong Wu0df4fab2016-02-23 01:20:50 +0800305 .iommu_dev = data->dev,
306 };
307
308 dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
309 if (!dom->iop) {
310 dev_err(data->dev, "Failed to alloc io pgtable\n");
311 return -EINVAL;
312 }
313
314 /* Update our support page sizes bitmap */
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100315 dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
Yong Wu0df4fab2016-02-23 01:20:50 +0800316 return 0;
317}
318
319static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
320{
321 struct mtk_iommu_domain *dom;
322
323 if (type != IOMMU_DOMAIN_DMA)
324 return NULL;
325
326 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
327 if (!dom)
328 return NULL;
329
Yong Wu4b00f5a2017-08-21 19:00:18 +0800330 if (iommu_get_dma_cookie(&dom->domain))
331 goto free_dom;
332
333 if (mtk_iommu_domain_finalise(dom))
334 goto put_dma_cookie;
Yong Wu0df4fab2016-02-23 01:20:50 +0800335
336 dom->domain.geometry.aperture_start = 0;
337 dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
338 dom->domain.geometry.force_aperture = true;
339
340 return &dom->domain;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800341
342put_dma_cookie:
343 iommu_put_dma_cookie(&dom->domain);
344free_dom:
345 kfree(dom);
346 return NULL;
Yong Wu0df4fab2016-02-23 01:20:50 +0800347}
348
349static void mtk_iommu_domain_free(struct iommu_domain *domain)
350{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800351 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
352
353 free_io_pgtable_ops(dom->iop);
Yong Wu0df4fab2016-02-23 01:20:50 +0800354 iommu_put_dma_cookie(domain);
355 kfree(to_mtk_domain(domain));
356}
357
358static int mtk_iommu_attach_device(struct iommu_domain *domain,
359 struct device *dev)
360{
361 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100362 struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800363
Yong Wu4b00f5a2017-08-21 19:00:18 +0800364 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800365 return -ENODEV;
366
Yong Wu4b00f5a2017-08-21 19:00:18 +0800367 /* Update the pgtable base address register of the M4U HW */
Yong Wu0df4fab2016-02-23 01:20:50 +0800368 if (!data->m4u_dom) {
369 data->m4u_dom = dom;
Robin Murphyd1e5f262019-10-25 19:08:37 +0100370 writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
Yong Wu4b00f5a2017-08-21 19:00:18 +0800371 data->base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800372 }
373
Yong Wu4b00f5a2017-08-21 19:00:18 +0800374 mtk_iommu_config(data, dev, true);
Yong Wu0df4fab2016-02-23 01:20:50 +0800375 return 0;
376}
377
378static void mtk_iommu_detach_device(struct iommu_domain *domain,
379 struct device *dev)
380{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100381 struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800382
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100383 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800384 return;
385
Yong Wu0df4fab2016-02-23 01:20:50 +0800386 mtk_iommu_config(data, dev, false);
387}
388
389static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
Tom Murphy781ca2d2019-09-08 09:56:38 -0700390 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Yong Wu0df4fab2016-02-23 01:20:50 +0800391{
392 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wub4dad402019-08-24 11:01:55 +0800393 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800394
Yong Wub4dad402019-08-24 11:01:55 +0800395 /* The "4GB mode" M4U physically can not use the lower remap of Dram. */
396 if (data->enable_4GB)
397 paddr |= BIT_ULL(32);
398
Yong Wu60829b42019-11-04 15:01:07 +0800399 /* Synchronize with the tlb_lock */
400 return dom->iop->map(dom->iop, iova, paddr, size, prot);
Yong Wu0df4fab2016-02-23 01:20:50 +0800401}
402
403static size_t mtk_iommu_unmap(struct iommu_domain *domain,
Will Deacon56f8af52019-07-02 16:44:06 +0100404 unsigned long iova, size_t size,
405 struct iommu_iotlb_gather *gather)
Yong Wu0df4fab2016-02-23 01:20:50 +0800406{
407 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu0df4fab2016-02-23 01:20:50 +0800408
Yong Wu60829b42019-11-04 15:01:07 +0800409 return dom->iop->unmap(dom->iop, iova, size, gather);
Yong Wu0df4fab2016-02-23 01:20:50 +0800410}
411
Will Deacon56f8af52019-07-02 16:44:06 +0100412static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
413{
Yong Wu20091222019-11-04 15:01:02 +0800414 mtk_iommu_tlb_flush_all(mtk_iommu_get_m4u_data());
Will Deacon56f8af52019-07-02 16:44:06 +0100415}
416
417static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
418 struct iommu_iotlb_gather *gather)
Robin Murphy4d689b62017-09-28 15:55:02 +0100419{
Yong Wuda3cc912019-11-04 15:01:03 +0800420 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wua7a04ea2019-11-04 15:01:04 +0800421 size_t length = gather->end - gather->start;
Yong Wuda3cc912019-11-04 15:01:03 +0800422
Yong Wua7a04ea2019-11-04 15:01:04 +0800423 if (gather->start == ULONG_MAX)
424 return;
425
Yong Wu1f4fd622019-11-04 15:01:06 +0800426 mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize,
Yong Wu67caf7e2019-11-04 15:01:05 +0800427 data);
Robin Murphy4d689b62017-09-28 15:55:02 +0100428}
429
Yong Wu0df4fab2016-02-23 01:20:50 +0800430static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
431 dma_addr_t iova)
432{
433 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800434 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800435 phys_addr_t pa;
436
Yong Wu0df4fab2016-02-23 01:20:50 +0800437 pa = dom->iop->iova_to_phys(dom->iop, iova);
Yong Wub4dad402019-08-24 11:01:55 +0800438 if (data->enable_4GB && pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
439 pa &= ~BIT_ULL(32);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800440
Yong Wu0df4fab2016-02-23 01:20:50 +0800441 return pa;
442}
443
444static int mtk_iommu_add_device(struct device *dev)
445{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100446 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100447 struct mtk_iommu_data *data;
Yong Wu0df4fab2016-02-23 01:20:50 +0800448 struct iommu_group *group;
449
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100450 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100451 return -ENODEV; /* Not a iommu client device */
Yong Wu0df4fab2016-02-23 01:20:50 +0800452
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100453 data = fwspec->iommu_priv;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100454 iommu_device_link(&data->iommu, dev);
455
Yong Wu0df4fab2016-02-23 01:20:50 +0800456 group = iommu_group_get_for_dev(dev);
457 if (IS_ERR(group))
458 return PTR_ERR(group);
459
460 iommu_group_put(group);
461 return 0;
462}
463
464static void mtk_iommu_remove_device(struct device *dev)
465{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100466 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100467 struct mtk_iommu_data *data;
468
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100469 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Yong Wu0df4fab2016-02-23 01:20:50 +0800470 return;
471
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100472 data = fwspec->iommu_priv;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100473 iommu_device_unlink(&data->iommu, dev);
474
Yong Wu0df4fab2016-02-23 01:20:50 +0800475 iommu_group_remove_device(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100476 iommu_fwspec_free(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800477}
478
479static struct iommu_group *mtk_iommu_device_group(struct device *dev)
480{
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800481 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800482
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100483 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800484 return ERR_PTR(-ENODEV);
485
486 /* All the client devices are in the same m4u iommu-group */
Yong Wu0df4fab2016-02-23 01:20:50 +0800487 if (!data->m4u_group) {
488 data->m4u_group = iommu_group_alloc();
489 if (IS_ERR(data->m4u_group))
490 dev_err(dev, "Failed to allocate M4U IOMMU group\n");
Robin Murphy3a8d40b2016-11-11 17:59:24 +0000491 } else {
492 iommu_group_ref_get(data->m4u_group);
Yong Wu0df4fab2016-02-23 01:20:50 +0800493 }
494 return data->m4u_group;
495}
496
497static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
498{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100499 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
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 Roedela9bf2ee2018-11-29 14:01:00 +0100508 if (!fwspec->iommu_priv) {
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 Roedela9bf2ee2018-11-29 14:01:00 +0100514 fwspec->iommu_priv = 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,
530 .add_device = mtk_iommu_add_device,
531 .remove_device = mtk_iommu_remove_device,
532 .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
Yong Wu2b326d82019-08-24 11:02:00 +0800579 if (data->enable_4GB && data->plat_data->has_vld_pa_rng) {
Yong Wu30e2fcc2017-08-21 19:00:20 +0800580 /*
581 * If 4GB mode is enabled, the validate PA range is from
582 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
583 */
584 regval = F_MMU_VLD_PA_RNG(7, 4);
585 writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
586 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800587 writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
Yong Wue6dec922017-08-21 19:00:16 +0800588
Yong Wu50822b02019-08-24 11:01:59 +0800589 if (data->plat_data->reset_axi)
Yong Wue6dec922017-08-21 19:00:16 +0800590 writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
Yong Wu0df4fab2016-02-23 01:20:50 +0800591
592 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
593 dev_name(data->dev), (void *)data)) {
594 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
595 clk_disable_unprepare(data->bclk);
596 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
597 return -ENODEV;
598 }
599
600 return 0;
601}
602
Yong Wu0df4fab2016-02-23 01:20:50 +0800603static const struct component_master_ops mtk_iommu_com_ops = {
604 .bind = mtk_iommu_bind,
605 .unbind = mtk_iommu_unbind,
606};
607
608static int mtk_iommu_probe(struct platform_device *pdev)
609{
610 struct mtk_iommu_data *data;
611 struct device *dev = &pdev->dev;
612 struct resource *res;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100613 resource_size_t ioaddr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800614 struct component_match *match = NULL;
615 void *protect;
Andrzej Hajda0b6c0ad2016-03-01 10:36:23 +0100616 int i, larb_nr, ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800617
618 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
619 if (!data)
620 return -ENOMEM;
621 data->dev = dev;
Yong Wucecdce92019-08-24 11:01:47 +0800622 data->plat_data = of_device_get_match_data(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800623
624 /* Protect memory. HW will access here while translation fault.*/
625 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
626 if (!protect)
627 return -ENOMEM;
628 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
629
Yong Wu01e23c92016-03-14 06:01:11 +0800630 /* Whether the current dram is over 4GB */
Yong Wu41939982017-08-24 15:42:12 +0800631 data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT));
Yong Wub4dad402019-08-24 11:01:55 +0800632 if (!data->plat_data->has_4gb_mode)
633 data->enable_4GB = false;
Yong Wu01e23c92016-03-14 06:01:11 +0800634
Yong Wu0df4fab2016-02-23 01:20:50 +0800635 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
636 data->base = devm_ioremap_resource(dev, res);
637 if (IS_ERR(data->base))
638 return PTR_ERR(data->base);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100639 ioaddr = res->start;
Yong Wu0df4fab2016-02-23 01:20:50 +0800640
641 data->irq = platform_get_irq(pdev, 0);
642 if (data->irq < 0)
643 return data->irq;
644
Yong Wu2aa4c252019-08-24 11:01:56 +0800645 if (data->plat_data->has_bclk) {
646 data->bclk = devm_clk_get(dev, "bclk");
647 if (IS_ERR(data->bclk))
648 return PTR_ERR(data->bclk);
649 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800650
651 larb_nr = of_count_phandle_with_args(dev->of_node,
652 "mediatek,larbs", NULL);
653 if (larb_nr < 0)
654 return larb_nr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800655
656 for (i = 0; i < larb_nr; i++) {
657 struct device_node *larbnode;
658 struct platform_device *plarbdev;
Yong Wue6dec922017-08-21 19:00:16 +0800659 u32 id;
Yong Wu0df4fab2016-02-23 01:20:50 +0800660
661 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
662 if (!larbnode)
663 return -EINVAL;
664
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800665 if (!of_device_is_available(larbnode)) {
666 of_node_put(larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800667 continue;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800668 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800669
Yong Wue6dec922017-08-21 19:00:16 +0800670 ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
671 if (ret)/* The id is consecutive if there is no this property */
672 id = i;
673
Yong Wu0df4fab2016-02-23 01:20:50 +0800674 plarbdev = of_find_device_by_node(larbnode);
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800675 if (!plarbdev) {
676 of_node_put(larbnode);
Yong Wue6dec922017-08-21 19:00:16 +0800677 return -EPROBE_DEFER;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800678 }
Yong Wu1ee9feb2019-08-24 11:02:08 +0800679 data->larb_imu[id].dev = &plarbdev->dev;
Yong Wu0df4fab2016-02-23 01:20:50 +0800680
Russell King00c7c812016-10-19 11:30:34 +0100681 component_match_add_release(dev, &match, release_of,
682 compare_of, larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800683 }
684
685 platform_set_drvdata(pdev, data);
686
687 ret = mtk_iommu_hw_init(data);
688 if (ret)
689 return ret;
690
Joerg Roedelb16c0172017-02-03 12:57:32 +0100691 ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
692 "mtk-iommu.%pa", &ioaddr);
693 if (ret)
694 return ret;
695
696 iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
697 iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
698
699 ret = iommu_device_register(&data->iommu);
700 if (ret)
701 return ret;
702
Yong Wuda3cc912019-11-04 15:01:03 +0800703 spin_lock_init(&data->tlb_lock);
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800704 list_add_tail(&data->list, &m4ulist);
705
Yong Wu0df4fab2016-02-23 01:20:50 +0800706 if (!iommu_present(&platform_bus_type))
707 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
708
709 return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
710}
711
712static int mtk_iommu_remove(struct platform_device *pdev)
713{
714 struct mtk_iommu_data *data = platform_get_drvdata(pdev);
715
Joerg Roedelb16c0172017-02-03 12:57:32 +0100716 iommu_device_sysfs_remove(&data->iommu);
717 iommu_device_unregister(&data->iommu);
718
Yong Wu0df4fab2016-02-23 01:20:50 +0800719 if (iommu_present(&platform_bus_type))
720 bus_set_iommu(&platform_bus_type, NULL);
721
Yong Wu0df4fab2016-02-23 01:20:50 +0800722 clk_disable_unprepare(data->bclk);
723 devm_free_irq(&pdev->dev, data->irq, data);
724 component_master_del(&pdev->dev, &mtk_iommu_com_ops);
725 return 0;
726}
727
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100728static int __maybe_unused mtk_iommu_suspend(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800729{
730 struct mtk_iommu_data *data = dev_get_drvdata(dev);
731 struct mtk_iommu_suspend_reg *reg = &data->reg;
732 void __iomem *base = data->base;
733
734 reg->standard_axi_mode = readl_relaxed(base +
735 REG_MMU_STANDARD_AXI_MODE);
736 reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
737 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
738 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
739 reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800740 reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
Yong Wub9475b32019-08-24 11:02:06 +0800741 reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
Yong Wu6254b642017-08-21 19:00:19 +0800742 clk_disable_unprepare(data->bclk);
Yong Wu0df4fab2016-02-23 01:20:50 +0800743 return 0;
744}
745
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100746static int __maybe_unused mtk_iommu_resume(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800747{
748 struct mtk_iommu_data *data = dev_get_drvdata(dev);
749 struct mtk_iommu_suspend_reg *reg = &data->reg;
Yong Wu907ba6a2019-08-24 11:02:02 +0800750 struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
Yong Wu0df4fab2016-02-23 01:20:50 +0800751 void __iomem *base = data->base;
Yong Wu6254b642017-08-21 19:00:19 +0800752 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800753
Yong Wu6254b642017-08-21 19:00:19 +0800754 ret = clk_prepare_enable(data->bclk);
755 if (ret) {
756 dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
757 return ret;
758 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800759 writel_relaxed(reg->standard_axi_mode,
760 base + REG_MMU_STANDARD_AXI_MODE);
761 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
762 writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
763 writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
764 writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800765 writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
Yong Wub9475b32019-08-24 11:02:06 +0800766 writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
Yong Wu907ba6a2019-08-24 11:02:02 +0800767 if (m4u_dom)
Robin Murphyd1e5f262019-10-25 19:08:37 +0100768 writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
Yong Wue6dec922017-08-21 19:00:16 +0800769 base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800770 return 0;
771}
772
Yong Wue6dec922017-08-21 19:00:16 +0800773static const struct dev_pm_ops mtk_iommu_pm_ops = {
Yong Wu6254b642017-08-21 19:00:19 +0800774 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
Yong Wu0df4fab2016-02-23 01:20:50 +0800775};
776
Yong Wucecdce92019-08-24 11:01:47 +0800777static const struct mtk_iommu_plat_data mt2712_data = {
778 .m4u_plat = M4U_MT2712,
Yong Wub4dad402019-08-24 11:01:55 +0800779 .has_4gb_mode = true,
Yong Wu2aa4c252019-08-24 11:01:56 +0800780 .has_bclk = true,
Yong Wu2b326d82019-08-24 11:02:00 +0800781 .has_vld_pa_rng = true,
Yong Wub3e5eee72019-08-24 11:01:57 +0800782 .larbid_remap = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Yong Wucecdce92019-08-24 11:01:47 +0800783};
784
785static const struct mtk_iommu_plat_data mt8173_data = {
786 .m4u_plat = M4U_MT8173,
Yong Wub4dad402019-08-24 11:01:55 +0800787 .has_4gb_mode = true,
Yong Wu2aa4c252019-08-24 11:01:56 +0800788 .has_bclk = true,
Yong Wu50822b02019-08-24 11:01:59 +0800789 .reset_axi = true,
Yong Wub3e5eee72019-08-24 11:01:57 +0800790 .larbid_remap = {0, 1, 2, 3, 4, 5}, /* Linear mapping. */
Yong Wucecdce92019-08-24 11:01:47 +0800791};
792
Yong Wu907ba6a2019-08-24 11:02:02 +0800793static const struct mtk_iommu_plat_data mt8183_data = {
794 .m4u_plat = M4U_MT8183,
795 .reset_axi = true,
796 .larbid_remap = {0, 4, 5, 6, 7, 2, 3, 1},
797};
798
Yong Wu0df4fab2016-02-23 01:20:50 +0800799static const struct of_device_id mtk_iommu_of_ids[] = {
Yong Wucecdce92019-08-24 11:01:47 +0800800 { .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
801 { .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
Yong Wu907ba6a2019-08-24 11:02:02 +0800802 { .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
Yong Wu0df4fab2016-02-23 01:20:50 +0800803 {}
804};
805
806static struct platform_driver mtk_iommu_driver = {
807 .probe = mtk_iommu_probe,
808 .remove = mtk_iommu_remove,
809 .driver = {
810 .name = "mtk-iommu",
Yong Wue6dec922017-08-21 19:00:16 +0800811 .of_match_table = of_match_ptr(mtk_iommu_of_ids),
Yong Wu0df4fab2016-02-23 01:20:50 +0800812 .pm = &mtk_iommu_pm_ops,
813 }
814};
815
Yong Wue6dec922017-08-21 19:00:16 +0800816static int __init mtk_iommu_init(void)
Yong Wu0df4fab2016-02-23 01:20:50 +0800817{
818 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800819
820 ret = platform_driver_register(&mtk_iommu_driver);
Yong Wue6dec922017-08-21 19:00:16 +0800821 if (ret != 0)
822 pr_err("Failed to register MTK IOMMU driver\n");
Yong Wu0df4fab2016-02-23 01:20:50 +0800823
Yong Wue6dec922017-08-21 19:00:16 +0800824 return ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800825}
826
Yong Wue6dec922017-08-21 19:00:16 +0800827subsys_initcall(mtk_iommu_init)