blob: 85a7176bf9ae222d1b57319f8b28d09994f8c6af [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
31
32#define REG_MMU_INVALIDATE 0x020
33#define F_ALL_INVLD 0x2
34#define F_MMU_INV_RANGE 0x1
35
36#define REG_MMU_INVLD_START_A 0x024
37#define REG_MMU_INVLD_END_A 0x028
38
39#define REG_MMU_INV_SEL 0x038
40#define F_INVLD_EN0 BIT(0)
41#define F_INVLD_EN1 BIT(1)
42
43#define REG_MMU_STANDARD_AXI_MODE 0x048
44#define REG_MMU_DCM_DIS 0x050
45
46#define REG_MMU_CTRL_REG 0x110
47#define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
Yong Wue6dec922017-08-21 19:00:16 +080048#define F_MMU_TF_PROTECT_SEL_SHIFT(data) \
49 ((data)->m4u_plat == M4U_MT2712 ? 4 : 5)
50/* It's named by F_MMU_TF_PROT_SEL in mt2712. */
51#define F_MMU_TF_PROTECT_SEL(prot, data) \
52 (((prot) & 0x3) << F_MMU_TF_PROTECT_SEL_SHIFT(data))
Yong Wu0df4fab2016-02-23 01:20:50 +080053
54#define REG_MMU_IVRP_PADDR 0x114
Yong Wu70ca6082018-03-18 09:52:54 +080055
Yong Wu30e2fcc2017-08-21 19:00:20 +080056#define REG_MMU_VLD_PA_RNG 0x118
57#define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA))
Yong Wu0df4fab2016-02-23 01:20:50 +080058
59#define REG_MMU_INT_CONTROL0 0x120
60#define F_L2_MULIT_HIT_EN BIT(0)
61#define F_TABLE_WALK_FAULT_INT_EN BIT(1)
62#define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
63#define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
64#define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
65#define F_MISS_FIFO_ERR_INT_EN BIT(6)
66#define F_INT_CLR_BIT BIT(12)
67
68#define REG_MMU_INT_MAIN_CONTROL 0x124
69#define F_INT_TRANSLATION_FAULT BIT(0)
70#define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
71#define F_INT_INVALID_PA_FAULT BIT(2)
72#define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
73#define F_INT_TLB_MISS_FAULT BIT(4)
74#define F_INT_MISS_TRANSACTION_FIFO_FAULT BIT(5)
75#define F_INT_PRETETCH_TRANSATION_FIFO_FAULT BIT(6)
76
77#define REG_MMU_CPE_DONE 0x12C
78
79#define REG_MMU_FAULT_ST1 0x134
80
81#define REG_MMU_FAULT_VA 0x13c
Yong Wu0df4fab2016-02-23 01:20:50 +080082#define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
83#define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
84
85#define REG_MMU_INVLD_PA 0x140
86#define REG_MMU_INT_ID 0x150
87#define F_MMU0_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
88#define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
89
90#define MTK_PROTECT_PA_ALIGN 128
91
Yong Wua9467d92017-08-21 19:00:15 +080092/*
93 * Get the local arbiter ID and the portid within the larb arbiter
94 * from mtk_m4u_id which is defined by MTK_M4U_ID.
95 */
Yong Wue6dec922017-08-21 19:00:16 +080096#define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf)
Yong Wua9467d92017-08-21 19:00:15 +080097#define MTK_M4U_TO_PORT(id) ((id) & 0x1f)
98
Yong Wu0df4fab2016-02-23 01:20:50 +080099struct mtk_iommu_domain {
100 spinlock_t pgtlock; /* lock for page table */
101
102 struct io_pgtable_cfg cfg;
103 struct io_pgtable_ops *iop;
104
105 struct iommu_domain domain;
106};
107
Arvind Yadavb65f5012018-10-18 19:13:38 +0800108static const struct iommu_ops mtk_iommu_ops;
Yong Wu0df4fab2016-02-23 01:20:50 +0800109
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800110static LIST_HEAD(m4ulist); /* List all the M4U HWs */
111
112#define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list)
113
114/*
115 * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
116 * for the performance.
117 *
118 * Here always return the mtk_iommu_data of the first probed M4U where the
119 * iommu domain information is recorded.
120 */
121static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
122{
123 struct mtk_iommu_data *data;
124
125 for_each_m4u(data)
126 return data;
127
128 return NULL;
129}
130
Yong Wu0df4fab2016-02-23 01:20:50 +0800131static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
132{
133 return container_of(dom, struct mtk_iommu_domain, domain);
134}
135
136static void mtk_iommu_tlb_flush_all(void *cookie)
137{
138 struct mtk_iommu_data *data = cookie;
139
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800140 for_each_m4u(data) {
141 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
142 data->base + REG_MMU_INV_SEL);
143 writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
144 wmb(); /* Make sure the tlb flush all done */
145 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800146}
147
148static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size,
149 size_t granule, bool leaf,
150 void *cookie)
151{
152 struct mtk_iommu_data *data = cookie;
153
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800154 for_each_m4u(data) {
155 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
156 data->base + REG_MMU_INV_SEL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800157
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800158 writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
159 writel_relaxed(iova + size - 1,
160 data->base + REG_MMU_INVLD_END_A);
161 writel_relaxed(F_MMU_INV_RANGE,
162 data->base + REG_MMU_INVALIDATE);
163 data->tlb_flush_active = true;
164 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800165}
166
167static void mtk_iommu_tlb_sync(void *cookie)
168{
169 struct mtk_iommu_data *data = cookie;
170 int ret;
171 u32 tmp;
172
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800173 for_each_m4u(data) {
174 /* Avoid timing out if there's nothing to wait for */
175 if (!data->tlb_flush_active)
176 return;
Robin Murphy98a8f632017-07-06 17:55:30 +0100177
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800178 ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
179 tmp, tmp != 0, 10, 100000);
180 if (ret) {
181 dev_warn(data->dev,
182 "Partial TLB flush timed out, falling back to full flush\n");
183 mtk_iommu_tlb_flush_all(cookie);
184 }
185 /* Clear the CPE status */
186 writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
187 data->tlb_flush_active = false;
Yong Wu0df4fab2016-02-23 01:20:50 +0800188 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800189}
190
Will Deacon05aed942019-07-02 16:44:25 +0100191static void mtk_iommu_tlb_flush_walk(unsigned long iova, size_t size,
192 size_t granule, void *cookie)
193{
194 mtk_iommu_tlb_add_flush_nosync(iova, size, granule, false, cookie);
195 mtk_iommu_tlb_sync(cookie);
196}
197
198static void mtk_iommu_tlb_flush_leaf(unsigned long iova, size_t size,
199 size_t granule, void *cookie)
200{
201 mtk_iommu_tlb_add_flush_nosync(iova, size, granule, true, cookie);
202 mtk_iommu_tlb_sync(cookie);
203}
204
Will Deacon298f78892019-07-02 16:43:34 +0100205static const struct iommu_flush_ops mtk_iommu_flush_ops = {
Yong Wu0df4fab2016-02-23 01:20:50 +0800206 .tlb_flush_all = mtk_iommu_tlb_flush_all,
Will Deacon05aed942019-07-02 16:44:25 +0100207 .tlb_flush_walk = mtk_iommu_tlb_flush_walk,
208 .tlb_flush_leaf = mtk_iommu_tlb_flush_leaf,
Yong Wu0df4fab2016-02-23 01:20:50 +0800209 .tlb_add_flush = mtk_iommu_tlb_add_flush_nosync,
210 .tlb_sync = mtk_iommu_tlb_sync,
211};
212
213static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
214{
215 struct mtk_iommu_data *data = dev_id;
216 struct mtk_iommu_domain *dom = data->m4u_dom;
217 u32 int_state, regval, fault_iova, fault_pa;
218 unsigned int fault_larb, fault_port;
219 bool layer, write;
220
221 /* Read error info from registers */
222 int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
223 fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
224 layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
225 write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
Yong Wu0df4fab2016-02-23 01:20:50 +0800226 fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
227 regval = readl_relaxed(data->base + REG_MMU_INT_ID);
228 fault_larb = F_MMU0_INT_ID_LARB_ID(regval);
229 fault_port = F_MMU0_INT_ID_PORT_ID(regval);
230
231 if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
232 write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
233 dev_err_ratelimited(
234 data->dev,
235 "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
236 int_state, fault_iova, fault_pa, fault_larb, fault_port,
237 layer, write ? "write" : "read");
238 }
239
240 /* Interrupt clear */
241 regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
242 regval |= F_INT_CLR_BIT;
243 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
244
245 mtk_iommu_tlb_flush_all(data);
246
247 return IRQ_HANDLED;
248}
249
250static void mtk_iommu_config(struct mtk_iommu_data *data,
251 struct device *dev, bool enable)
252{
Yong Wu0df4fab2016-02-23 01:20:50 +0800253 struct mtk_smi_larb_iommu *larb_mmu;
254 unsigned int larbid, portid;
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100255 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100256 int i;
Yong Wu0df4fab2016-02-23 01:20:50 +0800257
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100258 for (i = 0; i < fwspec->num_ids; ++i) {
259 larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
260 portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
Yong Wu0df4fab2016-02-23 01:20:50 +0800261 larb_mmu = &data->smi_imu.larb_imu[larbid];
262
263 dev_dbg(dev, "%s iommu port: %d\n",
264 enable ? "enable" : "disable", portid);
265
266 if (enable)
267 larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
268 else
269 larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
270 }
271}
272
Yong Wu4b00f5a2017-08-21 19:00:18 +0800273static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
Yong Wu0df4fab2016-02-23 01:20:50 +0800274{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800275 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800276
277 spin_lock_init(&dom->pgtlock);
278
279 dom->cfg = (struct io_pgtable_cfg) {
280 .quirks = IO_PGTABLE_QUIRK_ARM_NS |
281 IO_PGTABLE_QUIRK_NO_PERMS |
282 IO_PGTABLE_QUIRK_TLBI_ON_MAP,
283 .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
284 .ias = 32,
285 .oas = 32,
Will Deacon298f78892019-07-02 16:43:34 +0100286 .tlb = &mtk_iommu_flush_ops,
Yong Wu0df4fab2016-02-23 01:20:50 +0800287 .iommu_dev = data->dev,
288 };
289
Yong Wu01e23c92016-03-14 06:01:11 +0800290 if (data->enable_4GB)
291 dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_4GB;
292
Yong Wu0df4fab2016-02-23 01:20:50 +0800293 dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
294 if (!dom->iop) {
295 dev_err(data->dev, "Failed to alloc io pgtable\n");
296 return -EINVAL;
297 }
298
299 /* Update our support page sizes bitmap */
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100300 dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
Yong Wu0df4fab2016-02-23 01:20:50 +0800301 return 0;
302}
303
304static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
305{
306 struct mtk_iommu_domain *dom;
307
308 if (type != IOMMU_DOMAIN_DMA)
309 return NULL;
310
311 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
312 if (!dom)
313 return NULL;
314
Yong Wu4b00f5a2017-08-21 19:00:18 +0800315 if (iommu_get_dma_cookie(&dom->domain))
316 goto free_dom;
317
318 if (mtk_iommu_domain_finalise(dom))
319 goto put_dma_cookie;
Yong Wu0df4fab2016-02-23 01:20:50 +0800320
321 dom->domain.geometry.aperture_start = 0;
322 dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
323 dom->domain.geometry.force_aperture = true;
324
325 return &dom->domain;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800326
327put_dma_cookie:
328 iommu_put_dma_cookie(&dom->domain);
329free_dom:
330 kfree(dom);
331 return NULL;
Yong Wu0df4fab2016-02-23 01:20:50 +0800332}
333
334static void mtk_iommu_domain_free(struct iommu_domain *domain)
335{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800336 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
337
338 free_io_pgtable_ops(dom->iop);
Yong Wu0df4fab2016-02-23 01:20:50 +0800339 iommu_put_dma_cookie(domain);
340 kfree(to_mtk_domain(domain));
341}
342
343static int mtk_iommu_attach_device(struct iommu_domain *domain,
344 struct device *dev)
345{
346 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100347 struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800348
Yong Wu4b00f5a2017-08-21 19:00:18 +0800349 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800350 return -ENODEV;
351
Yong Wu4b00f5a2017-08-21 19:00:18 +0800352 /* Update the pgtable base address register of the M4U HW */
Yong Wu0df4fab2016-02-23 01:20:50 +0800353 if (!data->m4u_dom) {
354 data->m4u_dom = dom;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800355 writel(dom->cfg.arm_v7s_cfg.ttbr[0],
356 data->base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800357 }
358
Yong Wu4b00f5a2017-08-21 19:00:18 +0800359 mtk_iommu_config(data, dev, true);
Yong Wu0df4fab2016-02-23 01:20:50 +0800360 return 0;
361}
362
363static void mtk_iommu_detach_device(struct iommu_domain *domain,
364 struct device *dev)
365{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100366 struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800367
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100368 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800369 return;
370
Yong Wu0df4fab2016-02-23 01:20:50 +0800371 mtk_iommu_config(data, dev, false);
372}
373
374static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
375 phys_addr_t paddr, size_t size, int prot)
376{
377 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
378 unsigned long flags;
379 int ret;
380
381 spin_lock_irqsave(&dom->pgtlock, flags);
Yong Wu1ff9b172017-09-25 18:15:26 +0800382 ret = dom->iop->map(dom->iop, iova, paddr & DMA_BIT_MASK(32),
383 size, prot);
Yong Wu0df4fab2016-02-23 01:20:50 +0800384 spin_unlock_irqrestore(&dom->pgtlock, flags);
385
386 return ret;
387}
388
389static size_t mtk_iommu_unmap(struct iommu_domain *domain,
Will Deacon56f8af52019-07-02 16:44:06 +0100390 unsigned long iova, size_t size,
391 struct iommu_iotlb_gather *gather)
Yong Wu0df4fab2016-02-23 01:20:50 +0800392{
393 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
394 unsigned long flags;
395 size_t unmapsz;
396
397 spin_lock_irqsave(&dom->pgtlock, flags);
398 unmapsz = dom->iop->unmap(dom->iop, iova, size);
399 spin_unlock_irqrestore(&dom->pgtlock, flags);
400
401 return unmapsz;
402}
403
Will Deacon56f8af52019-07-02 16:44:06 +0100404static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
405{
406 mtk_iommu_tlb_sync(mtk_iommu_get_m4u_data());
407}
408
409static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
410 struct iommu_iotlb_gather *gather)
Robin Murphy4d689b62017-09-28 15:55:02 +0100411{
412 mtk_iommu_tlb_sync(mtk_iommu_get_m4u_data());
413}
414
Yong Wu0df4fab2016-02-23 01:20:50 +0800415static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
416 dma_addr_t iova)
417{
418 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800419 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800420 unsigned long flags;
421 phys_addr_t pa;
422
423 spin_lock_irqsave(&dom->pgtlock, flags);
424 pa = dom->iop->iova_to_phys(dom->iop, iova);
425 spin_unlock_irqrestore(&dom->pgtlock, flags);
426
Yong Wu30e2fcc2017-08-21 19:00:20 +0800427 if (data->enable_4GB)
Yong Wu41939982017-08-24 15:42:12 +0800428 pa |= BIT_ULL(32);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800429
Yong Wu0df4fab2016-02-23 01:20:50 +0800430 return pa;
431}
432
433static int mtk_iommu_add_device(struct device *dev)
434{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100435 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100436 struct mtk_iommu_data *data;
Yong Wu0df4fab2016-02-23 01:20:50 +0800437 struct iommu_group *group;
438
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100439 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100440 return -ENODEV; /* Not a iommu client device */
Yong Wu0df4fab2016-02-23 01:20:50 +0800441
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100442 data = fwspec->iommu_priv;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100443 iommu_device_link(&data->iommu, dev);
444
Yong Wu0df4fab2016-02-23 01:20:50 +0800445 group = iommu_group_get_for_dev(dev);
446 if (IS_ERR(group))
447 return PTR_ERR(group);
448
449 iommu_group_put(group);
450 return 0;
451}
452
453static void mtk_iommu_remove_device(struct device *dev)
454{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100455 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100456 struct mtk_iommu_data *data;
457
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100458 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Yong Wu0df4fab2016-02-23 01:20:50 +0800459 return;
460
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100461 data = fwspec->iommu_priv;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100462 iommu_device_unlink(&data->iommu, dev);
463
Yong Wu0df4fab2016-02-23 01:20:50 +0800464 iommu_group_remove_device(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100465 iommu_fwspec_free(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800466}
467
468static struct iommu_group *mtk_iommu_device_group(struct device *dev)
469{
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800470 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800471
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100472 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800473 return ERR_PTR(-ENODEV);
474
475 /* All the client devices are in the same m4u iommu-group */
Yong Wu0df4fab2016-02-23 01:20:50 +0800476 if (!data->m4u_group) {
477 data->m4u_group = iommu_group_alloc();
478 if (IS_ERR(data->m4u_group))
479 dev_err(dev, "Failed to allocate M4U IOMMU group\n");
Robin Murphy3a8d40b2016-11-11 17:59:24 +0000480 } else {
481 iommu_group_ref_get(data->m4u_group);
Yong Wu0df4fab2016-02-23 01:20:50 +0800482 }
483 return data->m4u_group;
484}
485
486static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
487{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100488 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800489 struct platform_device *m4updev;
490
491 if (args->args_count != 1) {
492 dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
493 args->args_count);
494 return -EINVAL;
495 }
496
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100497 if (!fwspec->iommu_priv) {
Yong Wu0df4fab2016-02-23 01:20:50 +0800498 /* Get the m4u device */
499 m4updev = of_find_device_by_node(args->np);
Yong Wu0df4fab2016-02-23 01:20:50 +0800500 if (WARN_ON(!m4updev))
501 return -EINVAL;
502
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100503 fwspec->iommu_priv = platform_get_drvdata(m4updev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800504 }
505
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100506 return iommu_fwspec_add_ids(dev, args->args, 1);
Yong Wu0df4fab2016-02-23 01:20:50 +0800507}
508
Arvind Yadavb65f5012018-10-18 19:13:38 +0800509static const struct iommu_ops mtk_iommu_ops = {
Yong Wu0df4fab2016-02-23 01:20:50 +0800510 .domain_alloc = mtk_iommu_domain_alloc,
511 .domain_free = mtk_iommu_domain_free,
512 .attach_dev = mtk_iommu_attach_device,
513 .detach_dev = mtk_iommu_detach_device,
514 .map = mtk_iommu_map,
515 .unmap = mtk_iommu_unmap,
Will Deacon56f8af52019-07-02 16:44:06 +0100516 .flush_iotlb_all = mtk_iommu_flush_iotlb_all,
Robin Murphy4d689b62017-09-28 15:55:02 +0100517 .iotlb_sync = mtk_iommu_iotlb_sync,
Yong Wu0df4fab2016-02-23 01:20:50 +0800518 .iova_to_phys = mtk_iommu_iova_to_phys,
519 .add_device = mtk_iommu_add_device,
520 .remove_device = mtk_iommu_remove_device,
521 .device_group = mtk_iommu_device_group,
522 .of_xlate = mtk_iommu_of_xlate,
523 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
524};
525
526static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
527{
528 u32 regval;
529 int ret;
530
531 ret = clk_prepare_enable(data->bclk);
532 if (ret) {
533 dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
534 return ret;
535 }
536
Yong Wue6dec922017-08-21 19:00:16 +0800537 regval = F_MMU_TF_PROTECT_SEL(2, data);
538 if (data->m4u_plat == M4U_MT8173)
539 regval |= F_MMU_PREFETCH_RT_REPLACE_MOD;
Yong Wu0df4fab2016-02-23 01:20:50 +0800540 writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
541
542 regval = F_L2_MULIT_HIT_EN |
543 F_TABLE_WALK_FAULT_INT_EN |
544 F_PREETCH_FIFO_OVERFLOW_INT_EN |
545 F_MISS_FIFO_OVERFLOW_INT_EN |
546 F_PREFETCH_FIFO_ERR_INT_EN |
547 F_MISS_FIFO_ERR_INT_EN;
548 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
549
550 regval = F_INT_TRANSLATION_FAULT |
551 F_INT_MAIN_MULTI_HIT_FAULT |
552 F_INT_INVALID_PA_FAULT |
553 F_INT_ENTRY_REPLACEMENT_FAULT |
554 F_INT_TLB_MISS_FAULT |
555 F_INT_MISS_TRANSACTION_FIFO_FAULT |
556 F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
557 writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
558
Yong Wu70ca6082018-03-18 09:52:54 +0800559 if (data->m4u_plat == M4U_MT8173)
560 regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
561 else
562 regval = lower_32_bits(data->protect_base) |
563 upper_32_bits(data->protect_base);
564 writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
565
Yong Wu4f1c8ea12017-08-24 15:42:11 +0800566 if (data->enable_4GB && data->m4u_plat != M4U_MT8173) {
Yong Wu30e2fcc2017-08-21 19:00:20 +0800567 /*
568 * If 4GB mode is enabled, the validate PA range is from
569 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
570 */
571 regval = F_MMU_VLD_PA_RNG(7, 4);
572 writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
573 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800574 writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
Yong Wue6dec922017-08-21 19:00:16 +0800575
576 /* It's MISC control register whose default value is ok except mt8173.*/
577 if (data->m4u_plat == M4U_MT8173)
578 writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
Yong Wu0df4fab2016-02-23 01:20:50 +0800579
580 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
581 dev_name(data->dev), (void *)data)) {
582 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
583 clk_disable_unprepare(data->bclk);
584 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
585 return -ENODEV;
586 }
587
588 return 0;
589}
590
Yong Wu0df4fab2016-02-23 01:20:50 +0800591static const struct component_master_ops mtk_iommu_com_ops = {
592 .bind = mtk_iommu_bind,
593 .unbind = mtk_iommu_unbind,
594};
595
596static int mtk_iommu_probe(struct platform_device *pdev)
597{
598 struct mtk_iommu_data *data;
599 struct device *dev = &pdev->dev;
600 struct resource *res;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100601 resource_size_t ioaddr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800602 struct component_match *match = NULL;
603 void *protect;
Andrzej Hajda0b6c0ad2016-03-01 10:36:23 +0100604 int i, larb_nr, ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800605
606 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
607 if (!data)
608 return -ENOMEM;
609 data->dev = dev;
Yong Wue6dec922017-08-21 19:00:16 +0800610 data->m4u_plat = (enum mtk_iommu_plat)of_device_get_match_data(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800611
612 /* Protect memory. HW will access here while translation fault.*/
613 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
614 if (!protect)
615 return -ENOMEM;
616 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
617
Yong Wu01e23c92016-03-14 06:01:11 +0800618 /* Whether the current dram is over 4GB */
Yong Wu41939982017-08-24 15:42:12 +0800619 data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT));
Yong Wu01e23c92016-03-14 06:01:11 +0800620
Yong Wu0df4fab2016-02-23 01:20:50 +0800621 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
622 data->base = devm_ioremap_resource(dev, res);
623 if (IS_ERR(data->base))
624 return PTR_ERR(data->base);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100625 ioaddr = res->start;
Yong Wu0df4fab2016-02-23 01:20:50 +0800626
627 data->irq = platform_get_irq(pdev, 0);
628 if (data->irq < 0)
629 return data->irq;
630
631 data->bclk = devm_clk_get(dev, "bclk");
632 if (IS_ERR(data->bclk))
633 return PTR_ERR(data->bclk);
634
635 larb_nr = of_count_phandle_with_args(dev->of_node,
636 "mediatek,larbs", NULL);
637 if (larb_nr < 0)
638 return larb_nr;
639 data->smi_imu.larb_nr = larb_nr;
640
641 for (i = 0; i < larb_nr; i++) {
642 struct device_node *larbnode;
643 struct platform_device *plarbdev;
Yong Wue6dec922017-08-21 19:00:16 +0800644 u32 id;
Yong Wu0df4fab2016-02-23 01:20:50 +0800645
646 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
647 if (!larbnode)
648 return -EINVAL;
649
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800650 if (!of_device_is_available(larbnode)) {
651 of_node_put(larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800652 continue;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800653 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800654
Yong Wue6dec922017-08-21 19:00:16 +0800655 ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
656 if (ret)/* The id is consecutive if there is no this property */
657 id = i;
658
Yong Wu0df4fab2016-02-23 01:20:50 +0800659 plarbdev = of_find_device_by_node(larbnode);
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800660 if (!plarbdev) {
661 of_node_put(larbnode);
Yong Wue6dec922017-08-21 19:00:16 +0800662 return -EPROBE_DEFER;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800663 }
Yong Wue6dec922017-08-21 19:00:16 +0800664 data->smi_imu.larb_imu[id].dev = &plarbdev->dev;
Yong Wu0df4fab2016-02-23 01:20:50 +0800665
Russell King00c7c812016-10-19 11:30:34 +0100666 component_match_add_release(dev, &match, release_of,
667 compare_of, larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800668 }
669
670 platform_set_drvdata(pdev, data);
671
672 ret = mtk_iommu_hw_init(data);
673 if (ret)
674 return ret;
675
Joerg Roedelb16c0172017-02-03 12:57:32 +0100676 ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
677 "mtk-iommu.%pa", &ioaddr);
678 if (ret)
679 return ret;
680
681 iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
682 iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
683
684 ret = iommu_device_register(&data->iommu);
685 if (ret)
686 return ret;
687
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800688 list_add_tail(&data->list, &m4ulist);
689
Yong Wu0df4fab2016-02-23 01:20:50 +0800690 if (!iommu_present(&platform_bus_type))
691 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
692
693 return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
694}
695
696static int mtk_iommu_remove(struct platform_device *pdev)
697{
698 struct mtk_iommu_data *data = platform_get_drvdata(pdev);
699
Joerg Roedelb16c0172017-02-03 12:57:32 +0100700 iommu_device_sysfs_remove(&data->iommu);
701 iommu_device_unregister(&data->iommu);
702
Yong Wu0df4fab2016-02-23 01:20:50 +0800703 if (iommu_present(&platform_bus_type))
704 bus_set_iommu(&platform_bus_type, NULL);
705
Yong Wu0df4fab2016-02-23 01:20:50 +0800706 clk_disable_unprepare(data->bclk);
707 devm_free_irq(&pdev->dev, data->irq, data);
708 component_master_del(&pdev->dev, &mtk_iommu_com_ops);
709 return 0;
710}
711
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100712static int __maybe_unused mtk_iommu_suspend(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800713{
714 struct mtk_iommu_data *data = dev_get_drvdata(dev);
715 struct mtk_iommu_suspend_reg *reg = &data->reg;
716 void __iomem *base = data->base;
717
718 reg->standard_axi_mode = readl_relaxed(base +
719 REG_MMU_STANDARD_AXI_MODE);
720 reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
721 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
722 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
723 reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800724 reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
Yong Wu6254b642017-08-21 19:00:19 +0800725 clk_disable_unprepare(data->bclk);
Yong Wu0df4fab2016-02-23 01:20:50 +0800726 return 0;
727}
728
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100729static int __maybe_unused mtk_iommu_resume(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800730{
731 struct mtk_iommu_data *data = dev_get_drvdata(dev);
732 struct mtk_iommu_suspend_reg *reg = &data->reg;
733 void __iomem *base = data->base;
Yong Wu6254b642017-08-21 19:00:19 +0800734 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800735
Yong Wu6254b642017-08-21 19:00:19 +0800736 ret = clk_prepare_enable(data->bclk);
737 if (ret) {
738 dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
739 return ret;
740 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800741 writel_relaxed(reg->standard_axi_mode,
742 base + REG_MMU_STANDARD_AXI_MODE);
743 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
744 writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
745 writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
746 writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800747 writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
Yong Wue6dec922017-08-21 19:00:16 +0800748 if (data->m4u_dom)
749 writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
750 base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800751 return 0;
752}
753
Yong Wue6dec922017-08-21 19:00:16 +0800754static const struct dev_pm_ops mtk_iommu_pm_ops = {
Yong Wu6254b642017-08-21 19:00:19 +0800755 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
Yong Wu0df4fab2016-02-23 01:20:50 +0800756};
757
758static const struct of_device_id mtk_iommu_of_ids[] = {
Yong Wue6dec922017-08-21 19:00:16 +0800759 { .compatible = "mediatek,mt2712-m4u", .data = (void *)M4U_MT2712},
760 { .compatible = "mediatek,mt8173-m4u", .data = (void *)M4U_MT8173},
Yong Wu0df4fab2016-02-23 01:20:50 +0800761 {}
762};
763
764static struct platform_driver mtk_iommu_driver = {
765 .probe = mtk_iommu_probe,
766 .remove = mtk_iommu_remove,
767 .driver = {
768 .name = "mtk-iommu",
Yong Wue6dec922017-08-21 19:00:16 +0800769 .of_match_table = of_match_ptr(mtk_iommu_of_ids),
Yong Wu0df4fab2016-02-23 01:20:50 +0800770 .pm = &mtk_iommu_pm_ops,
771 }
772};
773
Yong Wue6dec922017-08-21 19:00:16 +0800774static int __init mtk_iommu_init(void)
Yong Wu0df4fab2016-02-23 01:20:50 +0800775{
776 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800777
778 ret = platform_driver_register(&mtk_iommu_driver);
Yong Wue6dec922017-08-21 19:00:16 +0800779 if (ret != 0)
780 pr_err("Failed to register MTK IOMMU driver\n");
Yong Wu0df4fab2016-02-23 01:20:50 +0800781
Yong Wue6dec922017-08-21 19:00:16 +0800782 return ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800783}
784
Yong Wue6dec922017-08-21 19:00:16 +0800785subsys_initcall(mtk_iommu_init)