blob: be22fcf988cee31a388b7d355f43cfe64473134f [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Honghui Zhangb17336c2016-06-08 17:51:00 +08002/*
Paul Gortmakerd4cf5bb2018-12-01 14:19:12 -05003 * IOMMU API for MTK architected m4u v1 implementations
4 *
Honghui Zhangb17336c2016-06-08 17:51:00 +08005 * Copyright (c) 2015-2016 MediaTek Inc.
6 * Author: Honghui Zhang <honghui.zhang@mediatek.com>
7 *
8 * Based on driver/iommu/mtk_iommu.c
Honghui Zhangb17336c2016-06-08 17:51:00 +08009 */
Mike Rapoport57c8a662018-10-30 15:09:49 -070010#include <linux/memblock.h>
Honghui Zhangb17336c2016-06-08 17:51:00 +080011#include <linux/bug.h>
12#include <linux/clk.h>
13#include <linux/component.h>
14#include <linux/device.h>
Arnd Bergmann745b6e72017-05-11 13:35:51 +020015#include <linux/dma-mapping.h>
Honghui Zhangb17336c2016-06-08 17:51:00 +080016#include <linux/err.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/iommu.h>
20#include <linux/iopoll.h>
Honghui Zhangb17336c2016-06-08 17:51:00 +080021#include <linux/list.h>
Yong Wu8de000c2021-03-26 11:23:36 +080022#include <linux/module.h>
Honghui Zhangb17336c2016-06-08 17:51:00 +080023#include <linux/of_address.h>
Honghui Zhangb17336c2016-06-08 17:51:00 +080024#include <linux/of_irq.h>
25#include <linux/of_platform.h>
26#include <linux/platform_device.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <asm/barrier.h>
30#include <asm/dma-iommu.h>
Paul Gortmakerd4cf5bb2018-12-01 14:19:12 -050031#include <linux/init.h>
Honghui Zhangb17336c2016-06-08 17:51:00 +080032#include <dt-bindings/memory/mt2701-larb-port.h>
33#include <soc/mediatek/smi.h>
34#include "mtk_iommu.h"
35
36#define REG_MMU_PT_BASE_ADDR 0x000
37
38#define F_ALL_INVLD 0x2
39#define F_MMU_INV_RANGE 0x1
40#define F_INVLD_EN0 BIT(0)
41#define F_INVLD_EN1 BIT(1)
42
43#define F_MMU_FAULT_VA_MSK 0xfffff000
44#define MTK_PROTECT_PA_ALIGN 128
45
46#define REG_MMU_CTRL_REG 0x210
47#define F_MMU_CTRL_COHERENT_EN BIT(8)
48#define REG_MMU_IVRP_PADDR 0x214
49#define REG_MMU_INT_CONTROL 0x220
50#define F_INT_TRANSLATION_FAULT BIT(0)
51#define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
52#define F_INT_INVALID_PA_FAULT BIT(2)
53#define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
54#define F_INT_TABLE_WALK_FAULT BIT(4)
55#define F_INT_TLB_MISS_FAULT BIT(5)
56#define F_INT_PFH_DMA_FIFO_OVERFLOW BIT(6)
57#define F_INT_MISS_DMA_FIFO_OVERFLOW BIT(7)
58
59#define F_MMU_TF_PROTECT_SEL(prot) (((prot) & 0x3) << 5)
60#define F_INT_CLR_BIT BIT(12)
61
62#define REG_MMU_FAULT_ST 0x224
63#define REG_MMU_FAULT_VA 0x228
64#define REG_MMU_INVLD_PA 0x22C
65#define REG_MMU_INT_ID 0x388
66#define REG_MMU_INVALIDATE 0x5c0
67#define REG_MMU_INVLD_START_A 0x5c4
68#define REG_MMU_INVLD_END_A 0x5c8
69
70#define REG_MMU_INV_SEL 0x5d8
71#define REG_MMU_STANDARD_AXI_MODE 0x5e8
72
73#define REG_MMU_DCM 0x5f0
74#define F_MMU_DCM_ON BIT(1)
75#define REG_MMU_CPE_DONE 0x60c
76#define F_DESC_VALID 0x2
77#define F_DESC_NONSEC BIT(3)
78#define MT2701_M4U_TF_LARB(TF) (6 - (((TF) >> 13) & 0x7))
79#define MT2701_M4U_TF_PORT(TF) (((TF) >> 8) & 0xF)
80/* MTK generation one iommu HW only support 4K size mapping */
81#define MT2701_IOMMU_PAGE_SHIFT 12
82#define MT2701_IOMMU_PAGE_SIZE (1UL << MT2701_IOMMU_PAGE_SHIFT)
83
84/*
85 * MTK m4u support 4GB iova address space, and only support 4K page
86 * mapping. So the pagetable size should be exactly as 4M.
87 */
88#define M2701_IOMMU_PGT_SIZE SZ_4M
89
90struct mtk_iommu_domain {
91 spinlock_t pgtlock; /* lock for page table */
92 struct iommu_domain domain;
93 u32 *pgt_va;
94 dma_addr_t pgt_pa;
95 struct mtk_iommu_data *data;
96};
97
98static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
99{
100 return container_of(dom, struct mtk_iommu_domain, domain);
101}
102
103static const int mt2701_m4u_in_larb[] = {
104 LARB0_PORT_OFFSET, LARB1_PORT_OFFSET,
105 LARB2_PORT_OFFSET, LARB3_PORT_OFFSET
106};
107
108static inline int mt2701_m4u_to_larb(int id)
109{
110 int i;
111
112 for (i = ARRAY_SIZE(mt2701_m4u_in_larb) - 1; i >= 0; i--)
113 if ((id) >= mt2701_m4u_in_larb[i])
114 return i;
115
116 return 0;
117}
118
119static inline int mt2701_m4u_to_port(int id)
120{
121 int larb = mt2701_m4u_to_larb(id);
122
123 return id - mt2701_m4u_in_larb[larb];
124}
125
126static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
127{
128 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
129 data->base + REG_MMU_INV_SEL);
130 writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
131 wmb(); /* Make sure the tlb flush all done */
132}
133
134static void mtk_iommu_tlb_flush_range(struct mtk_iommu_data *data,
135 unsigned long iova, size_t size)
136{
137 int ret;
138 u32 tmp;
139
140 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
141 data->base + REG_MMU_INV_SEL);
142 writel_relaxed(iova & F_MMU_FAULT_VA_MSK,
143 data->base + REG_MMU_INVLD_START_A);
144 writel_relaxed((iova + size - 1) & F_MMU_FAULT_VA_MSK,
145 data->base + REG_MMU_INVLD_END_A);
146 writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE);
147
148 ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
149 tmp, tmp != 0, 10, 100000);
150 if (ret) {
151 dev_warn(data->dev,
152 "Partial TLB flush timed out, falling back to full flush\n");
153 mtk_iommu_tlb_flush_all(data);
154 }
155 /* Clear the CPE status */
156 writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
157}
158
159static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
160{
161 struct mtk_iommu_data *data = dev_id;
162 struct mtk_iommu_domain *dom = data->m4u_dom;
163 u32 int_state, regval, fault_iova, fault_pa;
164 unsigned int fault_larb, fault_port;
165
166 /* Read error information from registers */
167 int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST);
168 fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
169
170 fault_iova &= F_MMU_FAULT_VA_MSK;
171 fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
172 regval = readl_relaxed(data->base + REG_MMU_INT_ID);
173 fault_larb = MT2701_M4U_TF_LARB(regval);
174 fault_port = MT2701_M4U_TF_PORT(regval);
175
176 /*
177 * MTK v1 iommu HW could not determine whether the fault is read or
178 * write fault, report as read fault.
179 */
180 if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
181 IOMMU_FAULT_READ))
182 dev_err_ratelimited(data->dev,
183 "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d\n",
184 int_state, fault_iova, fault_pa,
185 fault_larb, fault_port);
186
187 /* Interrupt clear */
188 regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL);
189 regval |= F_INT_CLR_BIT;
190 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
191
192 mtk_iommu_tlb_flush_all(data);
193
194 return IRQ_HANDLED;
195}
196
197static void mtk_iommu_config(struct mtk_iommu_data *data,
198 struct device *dev, bool enable)
199{
Honghui Zhangb17336c2016-06-08 17:51:00 +0800200 struct mtk_smi_larb_iommu *larb_mmu;
201 unsigned int larbid, portid;
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100202 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy84672f12016-10-17 12:49:21 +0100203 int i;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800204
Robin Murphy84672f12016-10-17 12:49:21 +0100205 for (i = 0; i < fwspec->num_ids; ++i) {
206 larbid = mt2701_m4u_to_larb(fwspec->ids[i]);
207 portid = mt2701_m4u_to_port(fwspec->ids[i]);
Yong Wu1ee9feb2019-08-24 11:02:08 +0800208 larb_mmu = &data->larb_imu[larbid];
Honghui Zhangb17336c2016-06-08 17:51:00 +0800209
210 dev_dbg(dev, "%s iommu port: %d\n",
211 enable ? "enable" : "disable", portid);
212
213 if (enable)
214 larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
215 else
216 larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
217 }
218}
219
220static int mtk_iommu_domain_finalise(struct mtk_iommu_data *data)
221{
222 struct mtk_iommu_domain *dom = data->m4u_dom;
223
224 spin_lock_init(&dom->pgtlock);
225
Luis Chamberlain750afb02019-01-04 09:23:09 +0100226 dom->pgt_va = dma_alloc_coherent(data->dev, M2701_IOMMU_PGT_SIZE,
227 &dom->pgt_pa, GFP_KERNEL);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800228 if (!dom->pgt_va)
229 return -ENOMEM;
230
231 writel(dom->pgt_pa, data->base + REG_MMU_PT_BASE_ADDR);
232
233 dom->data = data;
234
235 return 0;
236}
237
238static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
239{
240 struct mtk_iommu_domain *dom;
241
242 if (type != IOMMU_DOMAIN_UNMANAGED)
243 return NULL;
244
245 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
246 if (!dom)
247 return NULL;
248
249 return &dom->domain;
250}
251
252static void mtk_iommu_domain_free(struct iommu_domain *domain)
253{
254 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
255 struct mtk_iommu_data *data = dom->data;
256
257 dma_free_coherent(data->dev, M2701_IOMMU_PGT_SIZE,
258 dom->pgt_va, dom->pgt_pa);
259 kfree(to_mtk_domain(domain));
260}
261
262static int mtk_iommu_attach_device(struct iommu_domain *domain,
263 struct device *dev)
264{
Joerg Roedel3524b552020-03-26 16:08:38 +0100265 struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800266 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu8bbe13f2020-05-15 16:08:43 +0800267 struct dma_iommu_mapping *mtk_mapping;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800268 int ret;
269
Yong Wu8bbe13f2020-05-15 16:08:43 +0800270 /* Only allow the domain created internally. */
Joerg Roedel58960172020-06-25 15:08:31 +0200271 mtk_mapping = data->mapping;
Yong Wu8bbe13f2020-05-15 16:08:43 +0800272 if (mtk_mapping->domain != domain)
273 return 0;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800274
Honghui Zhangb17336c2016-06-08 17:51:00 +0800275 if (!data->m4u_dom) {
276 data->m4u_dom = dom;
277 ret = mtk_iommu_domain_finalise(data);
278 if (ret) {
279 data->m4u_dom = NULL;
280 return ret;
281 }
282 }
283
284 mtk_iommu_config(data, dev, true);
285 return 0;
286}
287
288static void mtk_iommu_detach_device(struct iommu_domain *domain,
289 struct device *dev)
290{
Joerg Roedel3524b552020-03-26 16:08:38 +0100291 struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800292
Honghui Zhangb17336c2016-06-08 17:51:00 +0800293 mtk_iommu_config(data, dev, false);
294}
295
296static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
Tom Murphy781ca2d2019-09-08 09:56:38 -0700297 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
Honghui Zhangb17336c2016-06-08 17:51:00 +0800298{
299 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
300 unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
301 unsigned long flags;
302 unsigned int i;
303 u32 *pgt_base_iova = dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT);
304 u32 pabase = (u32)paddr;
305 int map_size = 0;
306
307 spin_lock_irqsave(&dom->pgtlock, flags);
308 for (i = 0; i < page_num; i++) {
309 if (pgt_base_iova[i]) {
310 memset(pgt_base_iova, 0, i * sizeof(u32));
311 break;
312 }
313 pgt_base_iova[i] = pabase | F_DESC_VALID | F_DESC_NONSEC;
314 pabase += MT2701_IOMMU_PAGE_SIZE;
315 map_size += MT2701_IOMMU_PAGE_SIZE;
316 }
317
318 spin_unlock_irqrestore(&dom->pgtlock, flags);
319
320 mtk_iommu_tlb_flush_range(dom->data, iova, size);
321
322 return map_size == size ? 0 : -EEXIST;
323}
324
325static size_t mtk_iommu_unmap(struct iommu_domain *domain,
Will Deacon56f8af52019-07-02 16:44:06 +0100326 unsigned long iova, size_t size,
327 struct iommu_iotlb_gather *gather)
Honghui Zhangb17336c2016-06-08 17:51:00 +0800328{
329 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
330 unsigned long flags;
331 u32 *pgt_base_iova = dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT);
332 unsigned int page_num = size >> MT2701_IOMMU_PAGE_SHIFT;
333
334 spin_lock_irqsave(&dom->pgtlock, flags);
335 memset(pgt_base_iova, 0, page_num * sizeof(u32));
336 spin_unlock_irqrestore(&dom->pgtlock, flags);
337
338 mtk_iommu_tlb_flush_range(dom->data, iova, size);
339
340 return size;
341}
342
343static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
344 dma_addr_t iova)
345{
346 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
347 unsigned long flags;
348 phys_addr_t pa;
349
350 spin_lock_irqsave(&dom->pgtlock, flags);
351 pa = *(dom->pgt_va + (iova >> MT2701_IOMMU_PAGE_SHIFT));
352 pa = pa & (~(MT2701_IOMMU_PAGE_SIZE - 1));
353 spin_unlock_irqrestore(&dom->pgtlock, flags);
354
355 return pa;
356}
357
Arvind Yadavb65f5012018-10-18 19:13:38 +0800358static const struct iommu_ops mtk_iommu_ops;
Robin Murphy84672f12016-10-17 12:49:21 +0100359
Honghui Zhangb17336c2016-06-08 17:51:00 +0800360/*
361 * MTK generation one iommu HW only support one iommu domain, and all the client
362 * sharing the same iova address space.
363 */
364static int mtk_iommu_create_mapping(struct device *dev,
365 struct of_phandle_args *args)
366{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100367 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy84672f12016-10-17 12:49:21 +0100368 struct mtk_iommu_data *data;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800369 struct platform_device *m4updev;
370 struct dma_iommu_mapping *mtk_mapping;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800371 int ret;
372
373 if (args->args_count != 1) {
374 dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
375 args->args_count);
376 return -EINVAL;
377 }
378
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100379 if (!fwspec) {
Robin Murphy84672f12016-10-17 12:49:21 +0100380 ret = iommu_fwspec_init(dev, &args->np->fwnode, &mtk_iommu_ops);
381 if (ret)
382 return ret;
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100383 fwspec = dev_iommu_fwspec_get(dev);
384 } else if (dev_iommu_fwspec_get(dev)->ops != &mtk_iommu_ops) {
Robin Murphy84672f12016-10-17 12:49:21 +0100385 return -EINVAL;
386 }
387
Joerg Roedel3524b552020-03-26 16:08:38 +0100388 if (!dev_iommu_priv_get(dev)) {
Honghui Zhangb17336c2016-06-08 17:51:00 +0800389 /* Get the m4u device */
390 m4updev = of_find_device_by_node(args->np);
391 if (WARN_ON(!m4updev))
392 return -EINVAL;
393
Joerg Roedel3524b552020-03-26 16:08:38 +0100394 dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
Honghui Zhangb17336c2016-06-08 17:51:00 +0800395 }
396
Robin Murphy84672f12016-10-17 12:49:21 +0100397 ret = iommu_fwspec_add_ids(dev, args->args, 1);
398 if (ret)
399 return ret;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800400
Joerg Roedel3524b552020-03-26 16:08:38 +0100401 data = dev_iommu_priv_get(dev);
Joerg Roedel58960172020-06-25 15:08:31 +0200402 mtk_mapping = data->mapping;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800403 if (!mtk_mapping) {
404 /* MTK iommu support 4GB iova address space. */
405 mtk_mapping = arm_iommu_create_mapping(&platform_bus_type,
406 0, 1ULL << 32);
Robin Murphy84672f12016-10-17 12:49:21 +0100407 if (IS_ERR(mtk_mapping))
408 return PTR_ERR(mtk_mapping);
409
Joerg Roedel58960172020-06-25 15:08:31 +0200410 data->mapping = mtk_mapping;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800411 }
412
Honghui Zhangb17336c2016-06-08 17:51:00 +0800413 return 0;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800414}
415
Yong Wu8bbe13f2020-05-15 16:08:43 +0800416static int mtk_iommu_def_domain_type(struct device *dev)
417{
418 return IOMMU_DOMAIN_UNMANAGED;
419}
420
Joerg Roedel57dbf812020-04-29 15:37:01 +0200421static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
Honghui Zhangb17336c2016-06-08 17:51:00 +0800422{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100423 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800424 struct of_phandle_args iommu_spec;
Joerg Roedel6f66ea02017-03-31 15:12:31 +0200425 struct mtk_iommu_data *data;
Yong Wuf90a9a82021-04-12 14:48:42 +0800426 int err, idx = 0;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800427
Yong Wuf90a9a82021-04-12 14:48:42 +0800428 while (!of_parse_phandle_with_args(dev->of_node, "iommus",
429 "#iommu-cells",
430 idx, &iommu_spec)) {
Honghui Zhangb17336c2016-06-08 17:51:00 +0800431
Yong Wuf90a9a82021-04-12 14:48:42 +0800432 err = mtk_iommu_create_mapping(dev, &iommu_spec);
433 of_node_put(iommu_spec.np);
434 if (err)
435 return ERR_PTR(err);
Joerg Roedelda5d2742019-01-23 08:46:29 +0100436
437 /* dev->iommu_fwspec might have changed */
438 fwspec = dev_iommu_fwspec_get(dev);
Yong Wuf90a9a82021-04-12 14:48:42 +0800439 idx++;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800440 }
441
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100442 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Joerg Roedel57dbf812020-04-29 15:37:01 +0200443 return ERR_PTR(-ENODEV); /* Not a iommu client device */
Yong Wuf3e827d2018-01-26 15:11:28 +0800444
Joerg Roedel3524b552020-03-26 16:08:38 +0100445 data = dev_iommu_priv_get(dev);
Yong Wuf3e827d2018-01-26 15:11:28 +0800446
Joerg Roedel57dbf812020-04-29 15:37:01 +0200447 return &data->iommu;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800448}
449
Joerg Roedel57dbf812020-04-29 15:37:01 +0200450static void mtk_iommu_probe_finalize(struct device *dev)
451{
452 struct dma_iommu_mapping *mtk_mapping;
453 struct mtk_iommu_data *data;
454 int err;
455
456 data = dev_iommu_priv_get(dev);
Joerg Roedel58960172020-06-25 15:08:31 +0200457 mtk_mapping = data->mapping;
Joerg Roedel57dbf812020-04-29 15:37:01 +0200458
459 err = arm_iommu_attach_device(dev, mtk_mapping);
460 if (err)
461 dev_err(dev, "Can't create IOMMU mapping - DMA-OPS will not work\n");
462}
463
464static void mtk_iommu_release_device(struct device *dev)
Honghui Zhangb17336c2016-06-08 17:51:00 +0800465{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100466 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedel6f66ea02017-03-31 15:12:31 +0200467
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100468 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Honghui Zhangb17336c2016-06-08 17:51:00 +0800469 return;
470
Robin Murphy84672f12016-10-17 12:49:21 +0100471 iommu_fwspec_free(dev);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800472}
473
Honghui Zhangb17336c2016-06-08 17:51:00 +0800474static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
475{
476 u32 regval;
477 int ret;
478
479 ret = clk_prepare_enable(data->bclk);
480 if (ret) {
481 dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
482 return ret;
483 }
484
485 regval = F_MMU_CTRL_COHERENT_EN | F_MMU_TF_PROTECT_SEL(2);
486 writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
487
488 regval = F_INT_TRANSLATION_FAULT |
489 F_INT_MAIN_MULTI_HIT_FAULT |
490 F_INT_INVALID_PA_FAULT |
491 F_INT_ENTRY_REPLACEMENT_FAULT |
492 F_INT_TABLE_WALK_FAULT |
493 F_INT_TLB_MISS_FAULT |
494 F_INT_PFH_DMA_FIFO_OVERFLOW |
495 F_INT_MISS_DMA_FIFO_OVERFLOW;
496 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL);
497
498 /* protect memory,hw will write here while translation fault */
499 writel_relaxed(data->protect_base,
500 data->base + REG_MMU_IVRP_PADDR);
501
502 writel_relaxed(F_MMU_DCM_ON, data->base + REG_MMU_DCM);
503
504 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
505 dev_name(data->dev), (void *)data)) {
506 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
507 clk_disable_unprepare(data->bclk);
508 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
509 return -ENODEV;
510 }
511
512 return 0;
513}
514
Arvind Yadavb65f5012018-10-18 19:13:38 +0800515static const struct iommu_ops mtk_iommu_ops = {
Honghui Zhangb17336c2016-06-08 17:51:00 +0800516 .domain_alloc = mtk_iommu_domain_alloc,
517 .domain_free = mtk_iommu_domain_free,
518 .attach_dev = mtk_iommu_attach_device,
519 .detach_dev = mtk_iommu_detach_device,
520 .map = mtk_iommu_map,
521 .unmap = mtk_iommu_unmap,
Honghui Zhangb17336c2016-06-08 17:51:00 +0800522 .iova_to_phys = mtk_iommu_iova_to_phys,
Joerg Roedel57dbf812020-04-29 15:37:01 +0200523 .probe_device = mtk_iommu_probe_device,
524 .probe_finalize = mtk_iommu_probe_finalize,
525 .release_device = mtk_iommu_release_device,
Yong Wu8bbe13f2020-05-15 16:08:43 +0800526 .def_domain_type = mtk_iommu_def_domain_type,
Joerg Roedel57dbf812020-04-29 15:37:01 +0200527 .device_group = generic_device_group,
Honghui Zhangb17336c2016-06-08 17:51:00 +0800528 .pgsize_bitmap = ~0UL << MT2701_IOMMU_PAGE_SHIFT,
Yong Wu8de000c2021-03-26 11:23:36 +0800529 .owner = THIS_MODULE,
Honghui Zhangb17336c2016-06-08 17:51:00 +0800530};
531
532static const struct of_device_id mtk_iommu_of_ids[] = {
533 { .compatible = "mediatek,mt2701-m4u", },
534 {}
535};
536
537static const struct component_master_ops mtk_iommu_com_ops = {
538 .bind = mtk_iommu_bind,
539 .unbind = mtk_iommu_unbind,
540};
541
542static int mtk_iommu_probe(struct platform_device *pdev)
543{
544 struct mtk_iommu_data *data;
545 struct device *dev = &pdev->dev;
546 struct resource *res;
547 struct component_match *match = NULL;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800548 void *protect;
Yong Wuf90a9a82021-04-12 14:48:42 +0800549 int larb_nr, ret, i;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800550
551 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
552 if (!data)
553 return -ENOMEM;
554
555 data->dev = dev;
556
557 /* Protect memory. HW will access here while translation fault.*/
558 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2,
559 GFP_KERNEL | GFP_DMA);
560 if (!protect)
561 return -ENOMEM;
562 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
563
564 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
565 data->base = devm_ioremap_resource(dev, res);
566 if (IS_ERR(data->base))
567 return PTR_ERR(data->base);
568
569 data->irq = platform_get_irq(pdev, 0);
570 if (data->irq < 0)
571 return data->irq;
572
573 data->bclk = devm_clk_get(dev, "bclk");
574 if (IS_ERR(data->bclk))
575 return PTR_ERR(data->bclk);
576
Yong Wuf90a9a82021-04-12 14:48:42 +0800577 larb_nr = of_count_phandle_with_args(dev->of_node,
578 "mediatek,larbs", NULL);
579 if (larb_nr < 0)
580 return larb_nr;
581
582 for (i = 0; i < larb_nr; i++) {
583 struct device_node *larbnode;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800584 struct platform_device *plarbdev;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800585
Yong Wuf90a9a82021-04-12 14:48:42 +0800586 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
587 if (!larbnode)
588 return -EINVAL;
589
590 if (!of_device_is_available(larbnode)) {
591 of_node_put(larbnode);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800592 continue;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800593 }
594
Yong Wuf90a9a82021-04-12 14:48:42 +0800595 plarbdev = of_find_device_by_node(larbnode);
596 if (!plarbdev) {
597 of_node_put(larbnode);
598 return -EPROBE_DEFER;
599 }
600 data->larb_imu[i].dev = &plarbdev->dev;
601
Russell King00c7c812016-10-19 11:30:34 +0100602 component_match_add_release(dev, &match, release_of,
Yong Wuf90a9a82021-04-12 14:48:42 +0800603 compare_of, larbnode);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800604 }
605
Honghui Zhangb17336c2016-06-08 17:51:00 +0800606 platform_set_drvdata(pdev, data);
607
608 ret = mtk_iommu_hw_init(data);
609 if (ret)
610 return ret;
611
Joerg Roedel6f66ea02017-03-31 15:12:31 +0200612 ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
613 dev_name(&pdev->dev));
614 if (ret)
615 return ret;
616
Robin Murphy2d471b22021-04-01 14:56:26 +0100617 ret = iommu_device_register(&data->iommu, &mtk_iommu_ops, dev);
Joerg Roedel6f66ea02017-03-31 15:12:31 +0200618 if (ret)
Yong Wuac304c02021-04-12 14:48:43 +0800619 goto out_sysfs_remove;
Joerg Roedel6f66ea02017-03-31 15:12:31 +0200620
Yong Wuac304c02021-04-12 14:48:43 +0800621 if (!iommu_present(&platform_bus_type)) {
622 ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
623 if (ret)
624 goto out_dev_unreg;
625 }
Honghui Zhangb17336c2016-06-08 17:51:00 +0800626
Yong Wuac304c02021-04-12 14:48:43 +0800627 ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
628 if (ret)
629 goto out_bus_set_null;
630 return ret;
631
632out_bus_set_null:
633 bus_set_iommu(&platform_bus_type, NULL);
634out_dev_unreg:
635 iommu_device_unregister(&data->iommu);
636out_sysfs_remove:
637 iommu_device_sysfs_remove(&data->iommu);
638 return ret;
Honghui Zhangb17336c2016-06-08 17:51:00 +0800639}
640
641static int mtk_iommu_remove(struct platform_device *pdev)
642{
643 struct mtk_iommu_data *data = platform_get_drvdata(pdev);
644
Joerg Roedel6f66ea02017-03-31 15:12:31 +0200645 iommu_device_sysfs_remove(&data->iommu);
646 iommu_device_unregister(&data->iommu);
647
Honghui Zhangb17336c2016-06-08 17:51:00 +0800648 if (iommu_present(&platform_bus_type))
649 bus_set_iommu(&platform_bus_type, NULL);
650
651 clk_disable_unprepare(data->bclk);
652 devm_free_irq(&pdev->dev, data->irq, data);
653 component_master_del(&pdev->dev, &mtk_iommu_com_ops);
654 return 0;
655}
656
657static int __maybe_unused mtk_iommu_suspend(struct device *dev)
658{
659 struct mtk_iommu_data *data = dev_get_drvdata(dev);
660 struct mtk_iommu_suspend_reg *reg = &data->reg;
661 void __iomem *base = data->base;
662
663 reg->standard_axi_mode = readl_relaxed(base +
664 REG_MMU_STANDARD_AXI_MODE);
665 reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM);
666 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
667 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL);
668 return 0;
669}
670
671static int __maybe_unused mtk_iommu_resume(struct device *dev)
672{
673 struct mtk_iommu_data *data = dev_get_drvdata(dev);
674 struct mtk_iommu_suspend_reg *reg = &data->reg;
675 void __iomem *base = data->base;
676
677 writel_relaxed(data->m4u_dom->pgt_pa, base + REG_MMU_PT_BASE_ADDR);
678 writel_relaxed(reg->standard_axi_mode,
679 base + REG_MMU_STANDARD_AXI_MODE);
680 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM);
681 writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
682 writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL);
683 writel_relaxed(data->protect_base, base + REG_MMU_IVRP_PADDR);
684 return 0;
685}
686
Joerg Roedel131bc8e2016-06-21 11:52:13 +0200687static const struct dev_pm_ops mtk_iommu_pm_ops = {
Honghui Zhangb17336c2016-06-08 17:51:00 +0800688 SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
689};
690
691static struct platform_driver mtk_iommu_driver = {
692 .probe = mtk_iommu_probe,
693 .remove = mtk_iommu_remove,
694 .driver = {
Matthias Brugger395df082017-10-30 12:37:55 +0100695 .name = "mtk-iommu-v1",
Honghui Zhangb17336c2016-06-08 17:51:00 +0800696 .of_match_table = mtk_iommu_of_ids,
697 .pm = &mtk_iommu_pm_ops,
698 }
699};
Yong Wu8de000c2021-03-26 11:23:36 +0800700module_platform_driver(mtk_iommu_driver);
Honghui Zhangb17336c2016-06-08 17:51:00 +0800701
Yong Wu8de000c2021-03-26 11:23:36 +0800702MODULE_DESCRIPTION("IOMMU API for MediaTek M4U v1 implementations");
703MODULE_LICENSE("GPL v2");