blob: e21de8634cbac28663feb0c83c5f5f6cf89a8cca [file] [log] [blame]
Yong Wu0df4fab2016-02-23 01:20:50 +08001/*
2 * Copyright (c) 2015-2016 MediaTek Inc.
3 * Author: Yong Wu <yong.wu@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
Yong Wu01e23c92016-03-14 06:01:11 +080014#include <linux/bootmem.h>
Yong Wu0df4fab2016-02-23 01:20:50 +080015#include <linux/bug.h>
16#include <linux/clk.h>
17#include <linux/component.h>
18#include <linux/device.h>
19#include <linux/dma-iommu.h>
20#include <linux/err.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/iommu.h>
24#include <linux/iopoll.h>
25#include <linux/list.h>
26#include <linux/of_address.h>
27#include <linux/of_iommu.h>
28#include <linux/of_irq.h>
29#include <linux/of_platform.h>
30#include <linux/platform_device.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33#include <asm/barrier.h>
Yong Wu0df4fab2016-02-23 01:20:50 +080034#include <soc/mediatek/smi.h>
35
Honghui Zhang9ca340c2016-06-08 17:50:58 +080036#include "mtk_iommu.h"
Yong Wu0df4fab2016-02-23 01:20:50 +080037
38#define REG_MMU_PT_BASE_ADDR 0x000
39
40#define REG_MMU_INVALIDATE 0x020
41#define F_ALL_INVLD 0x2
42#define F_MMU_INV_RANGE 0x1
43
44#define REG_MMU_INVLD_START_A 0x024
45#define REG_MMU_INVLD_END_A 0x028
46
47#define REG_MMU_INV_SEL 0x038
48#define F_INVLD_EN0 BIT(0)
49#define F_INVLD_EN1 BIT(1)
50
51#define REG_MMU_STANDARD_AXI_MODE 0x048
52#define REG_MMU_DCM_DIS 0x050
53
54#define REG_MMU_CTRL_REG 0x110
55#define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
Yong Wue6dec922017-08-21 19:00:16 +080056#define F_MMU_TF_PROTECT_SEL_SHIFT(data) \
57 ((data)->m4u_plat == M4U_MT2712 ? 4 : 5)
58/* It's named by F_MMU_TF_PROT_SEL in mt2712. */
59#define F_MMU_TF_PROTECT_SEL(prot, data) \
60 (((prot) & 0x3) << F_MMU_TF_PROTECT_SEL_SHIFT(data))
Yong Wu0df4fab2016-02-23 01:20:50 +080061
62#define REG_MMU_IVRP_PADDR 0x114
Yong Wu01e23c92016-03-14 06:01:11 +080063#define F_MMU_IVRP_PA_SET(pa, ext) (((pa) >> 1) | ((!!(ext)) << 31))
Yong Wu0df4fab2016-02-23 01:20:50 +080064
65#define REG_MMU_INT_CONTROL0 0x120
66#define F_L2_MULIT_HIT_EN BIT(0)
67#define F_TABLE_WALK_FAULT_INT_EN BIT(1)
68#define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
69#define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
70#define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
71#define F_MISS_FIFO_ERR_INT_EN BIT(6)
72#define F_INT_CLR_BIT BIT(12)
73
74#define REG_MMU_INT_MAIN_CONTROL 0x124
75#define F_INT_TRANSLATION_FAULT BIT(0)
76#define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
77#define F_INT_INVALID_PA_FAULT BIT(2)
78#define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
79#define F_INT_TLB_MISS_FAULT BIT(4)
80#define F_INT_MISS_TRANSACTION_FIFO_FAULT BIT(5)
81#define F_INT_PRETETCH_TRANSATION_FIFO_FAULT BIT(6)
82
83#define REG_MMU_CPE_DONE 0x12C
84
85#define REG_MMU_FAULT_ST1 0x134
86
87#define REG_MMU_FAULT_VA 0x13c
88#define F_MMU_FAULT_VA_MSK 0xfffff000
89#define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
90#define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
91
92#define REG_MMU_INVLD_PA 0x140
93#define REG_MMU_INT_ID 0x150
94#define F_MMU0_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
95#define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
96
97#define MTK_PROTECT_PA_ALIGN 128
98
Yong Wua9467d92017-08-21 19:00:15 +080099/*
100 * Get the local arbiter ID and the portid within the larb arbiter
101 * from mtk_m4u_id which is defined by MTK_M4U_ID.
102 */
Yong Wue6dec922017-08-21 19:00:16 +0800103#define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf)
Yong Wua9467d92017-08-21 19:00:15 +0800104#define MTK_M4U_TO_PORT(id) ((id) & 0x1f)
105
Yong Wu0df4fab2016-02-23 01:20:50 +0800106struct mtk_iommu_domain {
107 spinlock_t pgtlock; /* lock for page table */
108
109 struct io_pgtable_cfg cfg;
110 struct io_pgtable_ops *iop;
111
112 struct iommu_domain domain;
113};
114
Yong Wu0df4fab2016-02-23 01:20:50 +0800115static struct iommu_ops mtk_iommu_ops;
116
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800117static LIST_HEAD(m4ulist); /* List all the M4U HWs */
118
119#define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list)
120
121/*
122 * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
123 * for the performance.
124 *
125 * Here always return the mtk_iommu_data of the first probed M4U where the
126 * iommu domain information is recorded.
127 */
128static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
129{
130 struct mtk_iommu_data *data;
131
132 for_each_m4u(data)
133 return data;
134
135 return NULL;
136}
137
Yong Wu0df4fab2016-02-23 01:20:50 +0800138static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
139{
140 return container_of(dom, struct mtk_iommu_domain, domain);
141}
142
143static void mtk_iommu_tlb_flush_all(void *cookie)
144{
145 struct mtk_iommu_data *data = cookie;
146
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800147 for_each_m4u(data) {
148 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
149 data->base + REG_MMU_INV_SEL);
150 writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
151 wmb(); /* Make sure the tlb flush all done */
152 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800153}
154
155static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size,
156 size_t granule, bool leaf,
157 void *cookie)
158{
159 struct mtk_iommu_data *data = cookie;
160
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800161 for_each_m4u(data) {
162 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
163 data->base + REG_MMU_INV_SEL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800164
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800165 writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
166 writel_relaxed(iova + size - 1,
167 data->base + REG_MMU_INVLD_END_A);
168 writel_relaxed(F_MMU_INV_RANGE,
169 data->base + REG_MMU_INVALIDATE);
170 data->tlb_flush_active = true;
171 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800172}
173
174static void mtk_iommu_tlb_sync(void *cookie)
175{
176 struct mtk_iommu_data *data = cookie;
177 int ret;
178 u32 tmp;
179
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800180 for_each_m4u(data) {
181 /* Avoid timing out if there's nothing to wait for */
182 if (!data->tlb_flush_active)
183 return;
Robin Murphy98a8f632017-07-06 17:55:30 +0100184
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800185 ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
186 tmp, tmp != 0, 10, 100000);
187 if (ret) {
188 dev_warn(data->dev,
189 "Partial TLB flush timed out, falling back to full flush\n");
190 mtk_iommu_tlb_flush_all(cookie);
191 }
192 /* Clear the CPE status */
193 writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
194 data->tlb_flush_active = false;
Yong Wu0df4fab2016-02-23 01:20:50 +0800195 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800196}
197
198static const struct iommu_gather_ops mtk_iommu_gather_ops = {
199 .tlb_flush_all = mtk_iommu_tlb_flush_all,
200 .tlb_add_flush = mtk_iommu_tlb_add_flush_nosync,
201 .tlb_sync = mtk_iommu_tlb_sync,
202};
203
204static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
205{
206 struct mtk_iommu_data *data = dev_id;
207 struct mtk_iommu_domain *dom = data->m4u_dom;
208 u32 int_state, regval, fault_iova, fault_pa;
209 unsigned int fault_larb, fault_port;
210 bool layer, write;
211
212 /* Read error info from registers */
213 int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
214 fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
215 layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
216 write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
217 fault_iova &= F_MMU_FAULT_VA_MSK;
218 fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
219 regval = readl_relaxed(data->base + REG_MMU_INT_ID);
220 fault_larb = F_MMU0_INT_ID_LARB_ID(regval);
221 fault_port = F_MMU0_INT_ID_PORT_ID(regval);
222
223 if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
224 write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
225 dev_err_ratelimited(
226 data->dev,
227 "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
228 int_state, fault_iova, fault_pa, fault_larb, fault_port,
229 layer, write ? "write" : "read");
230 }
231
232 /* Interrupt clear */
233 regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
234 regval |= F_INT_CLR_BIT;
235 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
236
237 mtk_iommu_tlb_flush_all(data);
238
239 return IRQ_HANDLED;
240}
241
242static void mtk_iommu_config(struct mtk_iommu_data *data,
243 struct device *dev, bool enable)
244{
Yong Wu0df4fab2016-02-23 01:20:50 +0800245 struct mtk_smi_larb_iommu *larb_mmu;
246 unsigned int larbid, portid;
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100247 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
248 int i;
Yong Wu0df4fab2016-02-23 01:20:50 +0800249
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100250 for (i = 0; i < fwspec->num_ids; ++i) {
251 larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
252 portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
Yong Wu0df4fab2016-02-23 01:20:50 +0800253 larb_mmu = &data->smi_imu.larb_imu[larbid];
254
255 dev_dbg(dev, "%s iommu port: %d\n",
256 enable ? "enable" : "disable", portid);
257
258 if (enable)
259 larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
260 else
261 larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
262 }
263}
264
Yong Wu4b00f5a2017-08-21 19:00:18 +0800265static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
Yong Wu0df4fab2016-02-23 01:20:50 +0800266{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800267 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800268
269 spin_lock_init(&dom->pgtlock);
270
271 dom->cfg = (struct io_pgtable_cfg) {
272 .quirks = IO_PGTABLE_QUIRK_ARM_NS |
273 IO_PGTABLE_QUIRK_NO_PERMS |
274 IO_PGTABLE_QUIRK_TLBI_ON_MAP,
275 .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
276 .ias = 32,
277 .oas = 32,
278 .tlb = &mtk_iommu_gather_ops,
279 .iommu_dev = data->dev,
280 };
281
Yong Wu01e23c92016-03-14 06:01:11 +0800282 if (data->enable_4GB)
283 dom->cfg.quirks |= IO_PGTABLE_QUIRK_ARM_MTK_4GB;
284
Yong Wu0df4fab2016-02-23 01:20:50 +0800285 dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
286 if (!dom->iop) {
287 dev_err(data->dev, "Failed to alloc io pgtable\n");
288 return -EINVAL;
289 }
290
291 /* Update our support page sizes bitmap */
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100292 dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
Yong Wu0df4fab2016-02-23 01:20:50 +0800293 return 0;
294}
295
296static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
297{
298 struct mtk_iommu_domain *dom;
299
300 if (type != IOMMU_DOMAIN_DMA)
301 return NULL;
302
303 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
304 if (!dom)
305 return NULL;
306
Yong Wu4b00f5a2017-08-21 19:00:18 +0800307 if (iommu_get_dma_cookie(&dom->domain))
308 goto free_dom;
309
310 if (mtk_iommu_domain_finalise(dom))
311 goto put_dma_cookie;
Yong Wu0df4fab2016-02-23 01:20:50 +0800312
313 dom->domain.geometry.aperture_start = 0;
314 dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
315 dom->domain.geometry.force_aperture = true;
316
317 return &dom->domain;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800318
319put_dma_cookie:
320 iommu_put_dma_cookie(&dom->domain);
321free_dom:
322 kfree(dom);
323 return NULL;
Yong Wu0df4fab2016-02-23 01:20:50 +0800324}
325
326static void mtk_iommu_domain_free(struct iommu_domain *domain)
327{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800328 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
329
330 free_io_pgtable_ops(dom->iop);
Yong Wu0df4fab2016-02-23 01:20:50 +0800331 iommu_put_dma_cookie(domain);
332 kfree(to_mtk_domain(domain));
333}
334
335static int mtk_iommu_attach_device(struct iommu_domain *domain,
336 struct device *dev)
337{
338 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu4b00f5a2017-08-21 19:00:18 +0800339 struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800340
Yong Wu4b00f5a2017-08-21 19:00:18 +0800341 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800342 return -ENODEV;
343
Yong Wu4b00f5a2017-08-21 19:00:18 +0800344 /* Update the pgtable base address register of the M4U HW */
Yong Wu0df4fab2016-02-23 01:20:50 +0800345 if (!data->m4u_dom) {
346 data->m4u_dom = dom;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800347 writel(dom->cfg.arm_v7s_cfg.ttbr[0],
348 data->base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800349 }
350
Yong Wu4b00f5a2017-08-21 19:00:18 +0800351 mtk_iommu_config(data, dev, true);
Yong Wu0df4fab2016-02-23 01:20:50 +0800352 return 0;
353}
354
355static void mtk_iommu_detach_device(struct iommu_domain *domain,
356 struct device *dev)
357{
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100358 struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800359
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100360 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800361 return;
362
Yong Wu0df4fab2016-02-23 01:20:50 +0800363 mtk_iommu_config(data, dev, false);
364}
365
366static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
367 phys_addr_t paddr, size_t size, int prot)
368{
369 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
370 unsigned long flags;
371 int ret;
372
373 spin_lock_irqsave(&dom->pgtlock, flags);
374 ret = dom->iop->map(dom->iop, iova, paddr, size, prot);
375 spin_unlock_irqrestore(&dom->pgtlock, flags);
376
377 return ret;
378}
379
380static size_t mtk_iommu_unmap(struct iommu_domain *domain,
381 unsigned long iova, size_t size)
382{
383 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
384 unsigned long flags;
385 size_t unmapsz;
386
387 spin_lock_irqsave(&dom->pgtlock, flags);
388 unmapsz = dom->iop->unmap(dom->iop, iova, size);
389 spin_unlock_irqrestore(&dom->pgtlock, flags);
390
391 return unmapsz;
392}
393
394static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
395 dma_addr_t iova)
396{
397 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
398 unsigned long flags;
399 phys_addr_t pa;
400
401 spin_lock_irqsave(&dom->pgtlock, flags);
402 pa = dom->iop->iova_to_phys(dom->iop, iova);
403 spin_unlock_irqrestore(&dom->pgtlock, flags);
404
405 return pa;
406}
407
408static int mtk_iommu_add_device(struct device *dev)
409{
Joerg Roedelb16c0172017-02-03 12:57:32 +0100410 struct mtk_iommu_data *data;
Yong Wu0df4fab2016-02-23 01:20:50 +0800411 struct iommu_group *group;
412
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100413 if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
414 return -ENODEV; /* Not a iommu client device */
Yong Wu0df4fab2016-02-23 01:20:50 +0800415
Joerg Roedelb16c0172017-02-03 12:57:32 +0100416 data = dev->iommu_fwspec->iommu_priv;
417 iommu_device_link(&data->iommu, dev);
418
Yong Wu0df4fab2016-02-23 01:20:50 +0800419 group = iommu_group_get_for_dev(dev);
420 if (IS_ERR(group))
421 return PTR_ERR(group);
422
423 iommu_group_put(group);
424 return 0;
425}
426
427static void mtk_iommu_remove_device(struct device *dev)
428{
Joerg Roedelb16c0172017-02-03 12:57:32 +0100429 struct mtk_iommu_data *data;
430
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100431 if (!dev->iommu_fwspec || dev->iommu_fwspec->ops != &mtk_iommu_ops)
Yong Wu0df4fab2016-02-23 01:20:50 +0800432 return;
433
Joerg Roedelb16c0172017-02-03 12:57:32 +0100434 data = dev->iommu_fwspec->iommu_priv;
435 iommu_device_unlink(&data->iommu, dev);
436
Yong Wu0df4fab2016-02-23 01:20:50 +0800437 iommu_group_remove_device(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100438 iommu_fwspec_free(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800439}
440
441static struct iommu_group *mtk_iommu_device_group(struct device *dev)
442{
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800443 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800444
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100445 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800446 return ERR_PTR(-ENODEV);
447
448 /* All the client devices are in the same m4u iommu-group */
Yong Wu0df4fab2016-02-23 01:20:50 +0800449 if (!data->m4u_group) {
450 data->m4u_group = iommu_group_alloc();
451 if (IS_ERR(data->m4u_group))
452 dev_err(dev, "Failed to allocate M4U IOMMU group\n");
Robin Murphy3a8d40b2016-11-11 17:59:24 +0000453 } else {
454 iommu_group_ref_get(data->m4u_group);
Yong Wu0df4fab2016-02-23 01:20:50 +0800455 }
456 return data->m4u_group;
457}
458
459static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
460{
Yong Wu0df4fab2016-02-23 01:20:50 +0800461 struct platform_device *m4updev;
462
463 if (args->args_count != 1) {
464 dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
465 args->args_count);
466 return -EINVAL;
467 }
468
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100469 if (!dev->iommu_fwspec->iommu_priv) {
Yong Wu0df4fab2016-02-23 01:20:50 +0800470 /* Get the m4u device */
471 m4updev = of_find_device_by_node(args->np);
Yong Wu0df4fab2016-02-23 01:20:50 +0800472 if (WARN_ON(!m4updev))
473 return -EINVAL;
474
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100475 dev->iommu_fwspec->iommu_priv = platform_get_drvdata(m4updev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800476 }
477
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100478 return iommu_fwspec_add_ids(dev, args->args, 1);
Yong Wu0df4fab2016-02-23 01:20:50 +0800479}
480
481static struct iommu_ops mtk_iommu_ops = {
482 .domain_alloc = mtk_iommu_domain_alloc,
483 .domain_free = mtk_iommu_domain_free,
484 .attach_dev = mtk_iommu_attach_device,
485 .detach_dev = mtk_iommu_detach_device,
486 .map = mtk_iommu_map,
487 .unmap = mtk_iommu_unmap,
488 .map_sg = default_iommu_map_sg,
489 .iova_to_phys = mtk_iommu_iova_to_phys,
490 .add_device = mtk_iommu_add_device,
491 .remove_device = mtk_iommu_remove_device,
492 .device_group = mtk_iommu_device_group,
493 .of_xlate = mtk_iommu_of_xlate,
494 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
495};
496
497static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
498{
499 u32 regval;
500 int ret;
501
502 ret = clk_prepare_enable(data->bclk);
503 if (ret) {
504 dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
505 return ret;
506 }
507
Yong Wue6dec922017-08-21 19:00:16 +0800508 regval = F_MMU_TF_PROTECT_SEL(2, data);
509 if (data->m4u_plat == M4U_MT8173)
510 regval |= F_MMU_PREFETCH_RT_REPLACE_MOD;
Yong Wu0df4fab2016-02-23 01:20:50 +0800511 writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
512
513 regval = F_L2_MULIT_HIT_EN |
514 F_TABLE_WALK_FAULT_INT_EN |
515 F_PREETCH_FIFO_OVERFLOW_INT_EN |
516 F_MISS_FIFO_OVERFLOW_INT_EN |
517 F_PREFETCH_FIFO_ERR_INT_EN |
518 F_MISS_FIFO_ERR_INT_EN;
519 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
520
521 regval = F_INT_TRANSLATION_FAULT |
522 F_INT_MAIN_MULTI_HIT_FAULT |
523 F_INT_INVALID_PA_FAULT |
524 F_INT_ENTRY_REPLACEMENT_FAULT |
525 F_INT_TLB_MISS_FAULT |
526 F_INT_MISS_TRANSACTION_FIFO_FAULT |
527 F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
528 writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
529
Yong Wu01e23c92016-03-14 06:01:11 +0800530 writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB),
Yong Wu0df4fab2016-02-23 01:20:50 +0800531 data->base + REG_MMU_IVRP_PADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800532 writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
Yong Wue6dec922017-08-21 19:00:16 +0800533
534 /* It's MISC control register whose default value is ok except mt8173.*/
535 if (data->m4u_plat == M4U_MT8173)
536 writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
Yong Wu0df4fab2016-02-23 01:20:50 +0800537
538 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
539 dev_name(data->dev), (void *)data)) {
540 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
541 clk_disable_unprepare(data->bclk);
542 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
543 return -ENODEV;
544 }
545
546 return 0;
547}
548
Yong Wu0df4fab2016-02-23 01:20:50 +0800549static const struct component_master_ops mtk_iommu_com_ops = {
550 .bind = mtk_iommu_bind,
551 .unbind = mtk_iommu_unbind,
552};
553
554static int mtk_iommu_probe(struct platform_device *pdev)
555{
556 struct mtk_iommu_data *data;
557 struct device *dev = &pdev->dev;
558 struct resource *res;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100559 resource_size_t ioaddr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800560 struct component_match *match = NULL;
561 void *protect;
Andrzej Hajda0b6c0ad2016-03-01 10:36:23 +0100562 int i, larb_nr, ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800563
564 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
565 if (!data)
566 return -ENOMEM;
567 data->dev = dev;
Yong Wue6dec922017-08-21 19:00:16 +0800568 data->m4u_plat = (enum mtk_iommu_plat)of_device_get_match_data(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800569
570 /* Protect memory. HW will access here while translation fault.*/
571 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
572 if (!protect)
573 return -ENOMEM;
574 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
575
Yong Wu01e23c92016-03-14 06:01:11 +0800576 /* Whether the current dram is over 4GB */
577 data->enable_4GB = !!(max_pfn > (0xffffffffUL >> PAGE_SHIFT));
578
Yong Wu0df4fab2016-02-23 01:20:50 +0800579 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
580 data->base = devm_ioremap_resource(dev, res);
581 if (IS_ERR(data->base))
582 return PTR_ERR(data->base);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100583 ioaddr = res->start;
Yong Wu0df4fab2016-02-23 01:20:50 +0800584
585 data->irq = platform_get_irq(pdev, 0);
586 if (data->irq < 0)
587 return data->irq;
588
589 data->bclk = devm_clk_get(dev, "bclk");
590 if (IS_ERR(data->bclk))
591 return PTR_ERR(data->bclk);
592
593 larb_nr = of_count_phandle_with_args(dev->of_node,
594 "mediatek,larbs", NULL);
595 if (larb_nr < 0)
596 return larb_nr;
597 data->smi_imu.larb_nr = larb_nr;
598
599 for (i = 0; i < larb_nr; i++) {
600 struct device_node *larbnode;
601 struct platform_device *plarbdev;
Yong Wue6dec922017-08-21 19:00:16 +0800602 u32 id;
Yong Wu0df4fab2016-02-23 01:20:50 +0800603
604 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
605 if (!larbnode)
606 return -EINVAL;
607
608 if (!of_device_is_available(larbnode))
609 continue;
610
Yong Wue6dec922017-08-21 19:00:16 +0800611 ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
612 if (ret)/* The id is consecutive if there is no this property */
613 id = i;
614
Yong Wu0df4fab2016-02-23 01:20:50 +0800615 plarbdev = of_find_device_by_node(larbnode);
Yong Wue6dec922017-08-21 19:00:16 +0800616 if (!plarbdev)
617 return -EPROBE_DEFER;
618 data->smi_imu.larb_imu[id].dev = &plarbdev->dev;
Yong Wu0df4fab2016-02-23 01:20:50 +0800619
Russell King00c7c812016-10-19 11:30:34 +0100620 component_match_add_release(dev, &match, release_of,
621 compare_of, larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800622 }
623
624 platform_set_drvdata(pdev, data);
625
626 ret = mtk_iommu_hw_init(data);
627 if (ret)
628 return ret;
629
Joerg Roedelb16c0172017-02-03 12:57:32 +0100630 ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
631 "mtk-iommu.%pa", &ioaddr);
632 if (ret)
633 return ret;
634
635 iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
636 iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
637
638 ret = iommu_device_register(&data->iommu);
639 if (ret)
640 return ret;
641
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800642 list_add_tail(&data->list, &m4ulist);
643
Yong Wu0df4fab2016-02-23 01:20:50 +0800644 if (!iommu_present(&platform_bus_type))
645 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
646
647 return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
648}
649
650static int mtk_iommu_remove(struct platform_device *pdev)
651{
652 struct mtk_iommu_data *data = platform_get_drvdata(pdev);
653
Joerg Roedelb16c0172017-02-03 12:57:32 +0100654 iommu_device_sysfs_remove(&data->iommu);
655 iommu_device_unregister(&data->iommu);
656
Yong Wu0df4fab2016-02-23 01:20:50 +0800657 if (iommu_present(&platform_bus_type))
658 bus_set_iommu(&platform_bus_type, NULL);
659
Yong Wu0df4fab2016-02-23 01:20:50 +0800660 clk_disable_unprepare(data->bclk);
661 devm_free_irq(&pdev->dev, data->irq, data);
662 component_master_del(&pdev->dev, &mtk_iommu_com_ops);
663 return 0;
664}
665
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100666static int __maybe_unused mtk_iommu_suspend(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800667{
668 struct mtk_iommu_data *data = dev_get_drvdata(dev);
669 struct mtk_iommu_suspend_reg *reg = &data->reg;
670 void __iomem *base = data->base;
671
672 reg->standard_axi_mode = readl_relaxed(base +
673 REG_MMU_STANDARD_AXI_MODE);
674 reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
675 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
676 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
677 reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu6254b642017-08-21 19:00:19 +0800678 clk_disable_unprepare(data->bclk);
Yong Wu0df4fab2016-02-23 01:20:50 +0800679 return 0;
680}
681
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100682static int __maybe_unused mtk_iommu_resume(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800683{
684 struct mtk_iommu_data *data = dev_get_drvdata(dev);
685 struct mtk_iommu_suspend_reg *reg = &data->reg;
686 void __iomem *base = data->base;
Yong Wu6254b642017-08-21 19:00:19 +0800687 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800688
Yong Wu6254b642017-08-21 19:00:19 +0800689 ret = clk_prepare_enable(data->bclk);
690 if (ret) {
691 dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
692 return ret;
693 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800694 writel_relaxed(reg->standard_axi_mode,
695 base + REG_MMU_STANDARD_AXI_MODE);
696 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
697 writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
698 writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
699 writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu01e23c92016-03-14 06:01:11 +0800700 writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB),
Yong Wu0df4fab2016-02-23 01:20:50 +0800701 base + REG_MMU_IVRP_PADDR);
Yong Wue6dec922017-08-21 19:00:16 +0800702 if (data->m4u_dom)
703 writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
704 base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800705 return 0;
706}
707
Yong Wue6dec922017-08-21 19:00:16 +0800708static const struct dev_pm_ops mtk_iommu_pm_ops = {
Yong Wu6254b642017-08-21 19:00:19 +0800709 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
Yong Wu0df4fab2016-02-23 01:20:50 +0800710};
711
712static const struct of_device_id mtk_iommu_of_ids[] = {
Yong Wue6dec922017-08-21 19:00:16 +0800713 { .compatible = "mediatek,mt2712-m4u", .data = (void *)M4U_MT2712},
714 { .compatible = "mediatek,mt8173-m4u", .data = (void *)M4U_MT8173},
Yong Wu0df4fab2016-02-23 01:20:50 +0800715 {}
716};
717
718static struct platform_driver mtk_iommu_driver = {
719 .probe = mtk_iommu_probe,
720 .remove = mtk_iommu_remove,
721 .driver = {
722 .name = "mtk-iommu",
Yong Wue6dec922017-08-21 19:00:16 +0800723 .of_match_table = of_match_ptr(mtk_iommu_of_ids),
Yong Wu0df4fab2016-02-23 01:20:50 +0800724 .pm = &mtk_iommu_pm_ops,
725 }
726};
727
Yong Wue6dec922017-08-21 19:00:16 +0800728static int __init mtk_iommu_init(void)
Yong Wu0df4fab2016-02-23 01:20:50 +0800729{
730 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800731
732 ret = platform_driver_register(&mtk_iommu_driver);
Yong Wue6dec922017-08-21 19:00:16 +0800733 if (ret != 0)
734 pr_err("Failed to register MTK IOMMU driver\n");
Yong Wu0df4fab2016-02-23 01:20:50 +0800735
Yong Wue6dec922017-08-21 19:00:16 +0800736 return ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800737}
738
Yong Wue6dec922017-08-21 19:00:16 +0800739subsys_initcall(mtk_iommu_init)