blob: 947a8c6b87dc731115798b7863f10b4d61b6241d [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
Yong Wuacb3c922019-08-24 11:01:58 +080047#define F_MMU_TF_PROT_TO_PROGRAM_ADDR (2 << 4)
Yong Wu0df4fab2016-02-23 01:20:50 +080048#define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4)
Yong Wuacb3c922019-08-24 11:01:58 +080049#define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173 (2 << 5)
Yong Wu0df4fab2016-02-23 01:20:50 +080050
51#define REG_MMU_IVRP_PADDR 0x114
Yong Wu70ca6082018-03-18 09:52:54 +080052
Yong Wu30e2fcc2017-08-21 19:00:20 +080053#define REG_MMU_VLD_PA_RNG 0x118
54#define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA))
Yong Wu0df4fab2016-02-23 01:20:50 +080055
56#define REG_MMU_INT_CONTROL0 0x120
57#define F_L2_MULIT_HIT_EN BIT(0)
58#define F_TABLE_WALK_FAULT_INT_EN BIT(1)
59#define F_PREETCH_FIFO_OVERFLOW_INT_EN BIT(2)
60#define F_MISS_FIFO_OVERFLOW_INT_EN BIT(3)
61#define F_PREFETCH_FIFO_ERR_INT_EN BIT(5)
62#define F_MISS_FIFO_ERR_INT_EN BIT(6)
63#define F_INT_CLR_BIT BIT(12)
64
65#define REG_MMU_INT_MAIN_CONTROL 0x124
66#define F_INT_TRANSLATION_FAULT BIT(0)
67#define F_INT_MAIN_MULTI_HIT_FAULT BIT(1)
68#define F_INT_INVALID_PA_FAULT BIT(2)
69#define F_INT_ENTRY_REPLACEMENT_FAULT BIT(3)
70#define F_INT_TLB_MISS_FAULT BIT(4)
71#define F_INT_MISS_TRANSACTION_FIFO_FAULT BIT(5)
72#define F_INT_PRETETCH_TRANSATION_FIFO_FAULT BIT(6)
73
74#define REG_MMU_CPE_DONE 0x12C
75
76#define REG_MMU_FAULT_ST1 0x134
77
78#define REG_MMU_FAULT_VA 0x13c
Yong Wu0df4fab2016-02-23 01:20:50 +080079#define F_MMU_FAULT_VA_WRITE_BIT BIT(1)
80#define F_MMU_FAULT_VA_LAYER_BIT BIT(0)
81
82#define REG_MMU_INVLD_PA 0x140
83#define REG_MMU_INT_ID 0x150
84#define F_MMU0_INT_ID_LARB_ID(a) (((a) >> 7) & 0x7)
85#define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f)
86
87#define MTK_PROTECT_PA_ALIGN 128
88
Yong Wua9467d92017-08-21 19:00:15 +080089/*
90 * Get the local arbiter ID and the portid within the larb arbiter
91 * from mtk_m4u_id which is defined by MTK_M4U_ID.
92 */
Yong Wue6dec922017-08-21 19:00:16 +080093#define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf)
Yong Wua9467d92017-08-21 19:00:15 +080094#define MTK_M4U_TO_PORT(id) ((id) & 0x1f)
95
Yong Wu0df4fab2016-02-23 01:20:50 +080096struct mtk_iommu_domain {
97 spinlock_t pgtlock; /* lock for page table */
98
99 struct io_pgtable_cfg cfg;
100 struct io_pgtable_ops *iop;
101
102 struct iommu_domain domain;
103};
104
Arvind Yadavb65f5012018-10-18 19:13:38 +0800105static const struct iommu_ops mtk_iommu_ops;
Yong Wu0df4fab2016-02-23 01:20:50 +0800106
Yong Wu76ce6542019-08-24 11:01:50 +0800107/*
108 * In M4U 4GB mode, the physical address is remapped as below:
109 *
110 * CPU Physical address:
111 * ====================
112 *
113 * 0 1G 2G 3G 4G 5G
114 * |---A---|---B---|---C---|---D---|---E---|
115 * +--I/O--+------------Memory-------------+
116 *
117 * IOMMU output physical address:
118 * =============================
119 *
120 * 4G 5G 6G 7G 8G
121 * |---E---|---B---|---C---|---D---|
122 * +------------Memory-------------+
123 *
124 * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
125 * bit32 of the CPU physical address always is needed to set, and for Region
126 * 'E', the CPU physical address keep as is.
127 * Additionally, The iommu consumers always use the CPU phyiscal address.
128 */
Yong Wub4dad402019-08-24 11:01:55 +0800129#define MTK_IOMMU_4GB_MODE_REMAP_BASE 0x140000000UL
Yong Wu76ce6542019-08-24 11:01:50 +0800130
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800131static LIST_HEAD(m4ulist); /* List all the M4U HWs */
132
133#define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list)
134
135/*
136 * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
137 * for the performance.
138 *
139 * Here always return the mtk_iommu_data of the first probed M4U where the
140 * iommu domain information is recorded.
141 */
142static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
143{
144 struct mtk_iommu_data *data;
145
146 for_each_m4u(data)
147 return data;
148
149 return NULL;
150}
151
Yong Wu0df4fab2016-02-23 01:20:50 +0800152static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
153{
154 return container_of(dom, struct mtk_iommu_domain, domain);
155}
156
157static void mtk_iommu_tlb_flush_all(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);
164 writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
165 wmb(); /* Make sure the tlb flush all done */
166 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800167}
168
169static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size,
170 size_t granule, bool leaf,
171 void *cookie)
172{
173 struct mtk_iommu_data *data = cookie;
174
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800175 for_each_m4u(data) {
176 writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
177 data->base + REG_MMU_INV_SEL);
Yong Wu0df4fab2016-02-23 01:20:50 +0800178
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800179 writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A);
180 writel_relaxed(iova + size - 1,
181 data->base + REG_MMU_INVLD_END_A);
182 writel_relaxed(F_MMU_INV_RANGE,
183 data->base + REG_MMU_INVALIDATE);
184 data->tlb_flush_active = true;
185 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800186}
187
188static void mtk_iommu_tlb_sync(void *cookie)
189{
190 struct mtk_iommu_data *data = cookie;
191 int ret;
192 u32 tmp;
193
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800194 for_each_m4u(data) {
195 /* Avoid timing out if there's nothing to wait for */
196 if (!data->tlb_flush_active)
197 return;
Robin Murphy98a8f632017-07-06 17:55:30 +0100198
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800199 ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
200 tmp, tmp != 0, 10, 100000);
201 if (ret) {
202 dev_warn(data->dev,
203 "Partial TLB flush timed out, falling back to full flush\n");
204 mtk_iommu_tlb_flush_all(cookie);
205 }
206 /* Clear the CPE status */
207 writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
208 data->tlb_flush_active = false;
Yong Wu0df4fab2016-02-23 01:20:50 +0800209 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800210}
211
212static const struct iommu_gather_ops mtk_iommu_gather_ops = {
213 .tlb_flush_all = mtk_iommu_tlb_flush_all,
214 .tlb_add_flush = mtk_iommu_tlb_add_flush_nosync,
215 .tlb_sync = mtk_iommu_tlb_sync,
216};
217
218static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
219{
220 struct mtk_iommu_data *data = dev_id;
221 struct mtk_iommu_domain *dom = data->m4u_dom;
222 u32 int_state, regval, fault_iova, fault_pa;
223 unsigned int fault_larb, fault_port;
224 bool layer, write;
225
226 /* Read error info from registers */
227 int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
228 fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA);
229 layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
230 write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
Yong Wu0df4fab2016-02-23 01:20:50 +0800231 fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA);
232 regval = readl_relaxed(data->base + REG_MMU_INT_ID);
233 fault_larb = F_MMU0_INT_ID_LARB_ID(regval);
234 fault_port = F_MMU0_INT_ID_PORT_ID(regval);
235
Yong Wub3e5eee72019-08-24 11:01:57 +0800236 fault_larb = data->plat_data->larbid_remap[fault_larb];
237
Yong Wu0df4fab2016-02-23 01:20:50 +0800238 if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
239 write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
240 dev_err_ratelimited(
241 data->dev,
242 "fault type=0x%x iova=0x%x pa=0x%x larb=%d port=%d layer=%d %s\n",
243 int_state, fault_iova, fault_pa, fault_larb, fault_port,
244 layer, write ? "write" : "read");
245 }
246
247 /* Interrupt clear */
248 regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
249 regval |= F_INT_CLR_BIT;
250 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
251
252 mtk_iommu_tlb_flush_all(data);
253
254 return IRQ_HANDLED;
255}
256
257static void mtk_iommu_config(struct mtk_iommu_data *data,
258 struct device *dev, bool enable)
259{
Yong Wu0df4fab2016-02-23 01:20:50 +0800260 struct mtk_smi_larb_iommu *larb_mmu;
261 unsigned int larbid, portid;
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100262 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100263 int i;
Yong Wu0df4fab2016-02-23 01:20:50 +0800264
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100265 for (i = 0; i < fwspec->num_ids; ++i) {
266 larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
267 portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
Yong Wu0df4fab2016-02-23 01:20:50 +0800268 larb_mmu = &data->smi_imu.larb_imu[larbid];
269
270 dev_dbg(dev, "%s iommu port: %d\n",
271 enable ? "enable" : "disable", portid);
272
273 if (enable)
274 larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
275 else
276 larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
277 }
278}
279
Yong Wu4b00f5a2017-08-21 19:00:18 +0800280static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom)
Yong Wu0df4fab2016-02-23 01:20:50 +0800281{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800282 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800283
284 spin_lock_init(&dom->pgtlock);
285
286 dom->cfg = (struct io_pgtable_cfg) {
287 .quirks = IO_PGTABLE_QUIRK_ARM_NS |
288 IO_PGTABLE_QUIRK_NO_PERMS |
Yong Wub4dad402019-08-24 11:01:55 +0800289 IO_PGTABLE_QUIRK_TLBI_ON_MAP |
290 IO_PGTABLE_QUIRK_ARM_MTK_EXT,
Yong Wu0df4fab2016-02-23 01:20:50 +0800291 .pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
292 .ias = 32,
Yong Wub4dad402019-08-24 11:01:55 +0800293 .oas = 34,
Yong Wu0df4fab2016-02-23 01:20:50 +0800294 .tlb = &mtk_iommu_gather_ops,
295 .iommu_dev = data->dev,
296 };
297
298 dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
299 if (!dom->iop) {
300 dev_err(data->dev, "Failed to alloc io pgtable\n");
301 return -EINVAL;
302 }
303
304 /* Update our support page sizes bitmap */
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100305 dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
Yong Wu0df4fab2016-02-23 01:20:50 +0800306 return 0;
307}
308
309static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
310{
311 struct mtk_iommu_domain *dom;
312
313 if (type != IOMMU_DOMAIN_DMA)
314 return NULL;
315
316 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
317 if (!dom)
318 return NULL;
319
Yong Wu4b00f5a2017-08-21 19:00:18 +0800320 if (iommu_get_dma_cookie(&dom->domain))
321 goto free_dom;
322
323 if (mtk_iommu_domain_finalise(dom))
324 goto put_dma_cookie;
Yong Wu0df4fab2016-02-23 01:20:50 +0800325
326 dom->domain.geometry.aperture_start = 0;
327 dom->domain.geometry.aperture_end = DMA_BIT_MASK(32);
328 dom->domain.geometry.force_aperture = true;
329
330 return &dom->domain;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800331
332put_dma_cookie:
333 iommu_put_dma_cookie(&dom->domain);
334free_dom:
335 kfree(dom);
336 return NULL;
Yong Wu0df4fab2016-02-23 01:20:50 +0800337}
338
339static void mtk_iommu_domain_free(struct iommu_domain *domain)
340{
Yong Wu4b00f5a2017-08-21 19:00:18 +0800341 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
342
343 free_io_pgtable_ops(dom->iop);
Yong Wu0df4fab2016-02-23 01:20:50 +0800344 iommu_put_dma_cookie(domain);
345 kfree(to_mtk_domain(domain));
346}
347
348static int mtk_iommu_attach_device(struct iommu_domain *domain,
349 struct device *dev)
350{
351 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100352 struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800353
Yong Wu4b00f5a2017-08-21 19:00:18 +0800354 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800355 return -ENODEV;
356
Yong Wu4b00f5a2017-08-21 19:00:18 +0800357 /* Update the pgtable base address register of the M4U HW */
Yong Wu0df4fab2016-02-23 01:20:50 +0800358 if (!data->m4u_dom) {
359 data->m4u_dom = dom;
Yong Wu4b00f5a2017-08-21 19:00:18 +0800360 writel(dom->cfg.arm_v7s_cfg.ttbr[0],
361 data->base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800362 }
363
Yong Wu4b00f5a2017-08-21 19:00:18 +0800364 mtk_iommu_config(data, dev, true);
Yong Wu0df4fab2016-02-23 01:20:50 +0800365 return 0;
366}
367
368static void mtk_iommu_detach_device(struct iommu_domain *domain,
369 struct device *dev)
370{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100371 struct mtk_iommu_data *data = dev_iommu_fwspec_get(dev)->iommu_priv;
Yong Wu0df4fab2016-02-23 01:20:50 +0800372
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100373 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800374 return;
375
Yong Wu0df4fab2016-02-23 01:20:50 +0800376 mtk_iommu_config(data, dev, false);
377}
378
379static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
380 phys_addr_t paddr, size_t size, int prot)
381{
382 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wub4dad402019-08-24 11:01:55 +0800383 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800384 unsigned long flags;
385 int ret;
386
Yong Wub4dad402019-08-24 11:01:55 +0800387 /* The "4GB mode" M4U physically can not use the lower remap of Dram. */
388 if (data->enable_4GB)
389 paddr |= BIT_ULL(32);
390
Yong Wu0df4fab2016-02-23 01:20:50 +0800391 spin_lock_irqsave(&dom->pgtlock, flags);
Yong Wub4dad402019-08-24 11:01:55 +0800392 ret = dom->iop->map(dom->iop, iova, paddr, size, prot);
Yong Wu0df4fab2016-02-23 01:20:50 +0800393 spin_unlock_irqrestore(&dom->pgtlock, flags);
394
395 return ret;
396}
397
398static size_t mtk_iommu_unmap(struct iommu_domain *domain,
399 unsigned long iova, size_t size)
400{
401 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
402 unsigned long flags;
403 size_t unmapsz;
404
405 spin_lock_irqsave(&dom->pgtlock, flags);
406 unmapsz = dom->iop->unmap(dom->iop, iova, size);
407 spin_unlock_irqrestore(&dom->pgtlock, flags);
408
409 return unmapsz;
410}
411
Robin Murphy4d689b62017-09-28 15:55:02 +0100412static void mtk_iommu_iotlb_sync(struct iommu_domain *domain)
413{
414 mtk_iommu_tlb_sync(mtk_iommu_get_m4u_data());
415}
416
Yong Wu0df4fab2016-02-23 01:20:50 +0800417static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
418 dma_addr_t iova)
419{
420 struct mtk_iommu_domain *dom = to_mtk_domain(domain);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800421 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800422 unsigned long flags;
423 phys_addr_t pa;
424
425 spin_lock_irqsave(&dom->pgtlock, flags);
426 pa = dom->iop->iova_to_phys(dom->iop, iova);
427 spin_unlock_irqrestore(&dom->pgtlock, flags);
428
Yong Wub4dad402019-08-24 11:01:55 +0800429 if (data->enable_4GB && pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
430 pa &= ~BIT_ULL(32);
Yong Wu30e2fcc2017-08-21 19:00:20 +0800431
Yong Wu0df4fab2016-02-23 01:20:50 +0800432 return pa;
433}
434
435static int mtk_iommu_add_device(struct device *dev)
436{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100437 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100438 struct mtk_iommu_data *data;
Yong Wu0df4fab2016-02-23 01:20:50 +0800439 struct iommu_group *group;
440
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100441 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100442 return -ENODEV; /* Not a iommu client device */
Yong Wu0df4fab2016-02-23 01:20:50 +0800443
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100444 data = fwspec->iommu_priv;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100445 iommu_device_link(&data->iommu, dev);
446
Yong Wu0df4fab2016-02-23 01:20:50 +0800447 group = iommu_group_get_for_dev(dev);
448 if (IS_ERR(group))
449 return PTR_ERR(group);
450
451 iommu_group_put(group);
452 return 0;
453}
454
455static void mtk_iommu_remove_device(struct device *dev)
456{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100457 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100458 struct mtk_iommu_data *data;
459
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100460 if (!fwspec || fwspec->ops != &mtk_iommu_ops)
Yong Wu0df4fab2016-02-23 01:20:50 +0800461 return;
462
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100463 data = fwspec->iommu_priv;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100464 iommu_device_unlink(&data->iommu, dev);
465
Yong Wu0df4fab2016-02-23 01:20:50 +0800466 iommu_group_remove_device(dev);
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100467 iommu_fwspec_free(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800468}
469
470static struct iommu_group *mtk_iommu_device_group(struct device *dev)
471{
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800472 struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
Yong Wu0df4fab2016-02-23 01:20:50 +0800473
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100474 if (!data)
Yong Wu0df4fab2016-02-23 01:20:50 +0800475 return ERR_PTR(-ENODEV);
476
477 /* All the client devices are in the same m4u iommu-group */
Yong Wu0df4fab2016-02-23 01:20:50 +0800478 if (!data->m4u_group) {
479 data->m4u_group = iommu_group_alloc();
480 if (IS_ERR(data->m4u_group))
481 dev_err(dev, "Failed to allocate M4U IOMMU group\n");
Robin Murphy3a8d40b2016-11-11 17:59:24 +0000482 } else {
483 iommu_group_ref_get(data->m4u_group);
Yong Wu0df4fab2016-02-23 01:20:50 +0800484 }
485 return data->m4u_group;
486}
487
488static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
489{
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100490 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800491 struct platform_device *m4updev;
492
493 if (args->args_count != 1) {
494 dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
495 args->args_count);
496 return -EINVAL;
497 }
498
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100499 if (!fwspec->iommu_priv) {
Yong Wu0df4fab2016-02-23 01:20:50 +0800500 /* Get the m4u device */
501 m4updev = of_find_device_by_node(args->np);
Yong Wu0df4fab2016-02-23 01:20:50 +0800502 if (WARN_ON(!m4updev))
503 return -EINVAL;
504
Joerg Roedela9bf2ee2018-11-29 14:01:00 +0100505 fwspec->iommu_priv = platform_get_drvdata(m4updev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800506 }
507
Robin Murphy58f0d1d2016-10-17 12:49:20 +0100508 return iommu_fwspec_add_ids(dev, args->args, 1);
Yong Wu0df4fab2016-02-23 01:20:50 +0800509}
510
Arvind Yadavb65f5012018-10-18 19:13:38 +0800511static const struct iommu_ops mtk_iommu_ops = {
Yong Wu0df4fab2016-02-23 01:20:50 +0800512 .domain_alloc = mtk_iommu_domain_alloc,
513 .domain_free = mtk_iommu_domain_free,
514 .attach_dev = mtk_iommu_attach_device,
515 .detach_dev = mtk_iommu_detach_device,
516 .map = mtk_iommu_map,
517 .unmap = mtk_iommu_unmap,
Robin Murphy4d689b62017-09-28 15:55:02 +0100518 .flush_iotlb_all = mtk_iommu_iotlb_sync,
519 .iotlb_sync = mtk_iommu_iotlb_sync,
Yong Wu0df4fab2016-02-23 01:20:50 +0800520 .iova_to_phys = mtk_iommu_iova_to_phys,
521 .add_device = mtk_iommu_add_device,
522 .remove_device = mtk_iommu_remove_device,
523 .device_group = mtk_iommu_device_group,
524 .of_xlate = mtk_iommu_of_xlate,
525 .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
526};
527
528static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
529{
530 u32 regval;
531 int ret;
532
533 ret = clk_prepare_enable(data->bclk);
534 if (ret) {
535 dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
536 return ret;
537 }
538
Yong Wucecdce92019-08-24 11:01:47 +0800539 if (data->plat_data->m4u_plat == M4U_MT8173)
Yong Wuacb3c922019-08-24 11:01:58 +0800540 regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
541 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
542 else
543 regval = F_MMU_TF_PROT_TO_PROGRAM_ADDR;
Yong Wu0df4fab2016-02-23 01:20:50 +0800544 writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);
545
546 regval = F_L2_MULIT_HIT_EN |
547 F_TABLE_WALK_FAULT_INT_EN |
548 F_PREETCH_FIFO_OVERFLOW_INT_EN |
549 F_MISS_FIFO_OVERFLOW_INT_EN |
550 F_PREFETCH_FIFO_ERR_INT_EN |
551 F_MISS_FIFO_ERR_INT_EN;
552 writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);
553
554 regval = F_INT_TRANSLATION_FAULT |
555 F_INT_MAIN_MULTI_HIT_FAULT |
556 F_INT_INVALID_PA_FAULT |
557 F_INT_ENTRY_REPLACEMENT_FAULT |
558 F_INT_TLB_MISS_FAULT |
559 F_INT_MISS_TRANSACTION_FIFO_FAULT |
560 F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
561 writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);
562
Yong Wucecdce92019-08-24 11:01:47 +0800563 if (data->plat_data->m4u_plat == M4U_MT8173)
Yong Wu70ca6082018-03-18 09:52:54 +0800564 regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
565 else
566 regval = lower_32_bits(data->protect_base) |
567 upper_32_bits(data->protect_base);
568 writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);
569
Yong Wucecdce92019-08-24 11:01:47 +0800570 if (data->enable_4GB && data->plat_data->m4u_plat != M4U_MT8173) {
Yong Wu30e2fcc2017-08-21 19:00:20 +0800571 /*
572 * If 4GB mode is enabled, the validate PA range is from
573 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
574 */
575 regval = F_MMU_VLD_PA_RNG(7, 4);
576 writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
577 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800578 writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
Yong Wue6dec922017-08-21 19:00:16 +0800579
580 /* It's MISC control register whose default value is ok except mt8173.*/
Yong Wucecdce92019-08-24 11:01:47 +0800581 if (data->plat_data->m4u_plat == M4U_MT8173)
Yong Wue6dec922017-08-21 19:00:16 +0800582 writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE);
Yong Wu0df4fab2016-02-23 01:20:50 +0800583
584 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
585 dev_name(data->dev), (void *)data)) {
586 writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
587 clk_disable_unprepare(data->bclk);
588 dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
589 return -ENODEV;
590 }
591
592 return 0;
593}
594
Yong Wu0df4fab2016-02-23 01:20:50 +0800595static const struct component_master_ops mtk_iommu_com_ops = {
596 .bind = mtk_iommu_bind,
597 .unbind = mtk_iommu_unbind,
598};
599
600static int mtk_iommu_probe(struct platform_device *pdev)
601{
602 struct mtk_iommu_data *data;
603 struct device *dev = &pdev->dev;
604 struct resource *res;
Joerg Roedelb16c0172017-02-03 12:57:32 +0100605 resource_size_t ioaddr;
Yong Wu0df4fab2016-02-23 01:20:50 +0800606 struct component_match *match = NULL;
607 void *protect;
Andrzej Hajda0b6c0ad2016-03-01 10:36:23 +0100608 int i, larb_nr, ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800609
610 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
611 if (!data)
612 return -ENOMEM;
613 data->dev = dev;
Yong Wucecdce92019-08-24 11:01:47 +0800614 data->plat_data = of_device_get_match_data(dev);
Yong Wu0df4fab2016-02-23 01:20:50 +0800615
616 /* Protect memory. HW will access here while translation fault.*/
617 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
618 if (!protect)
619 return -ENOMEM;
620 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);
621
Yong Wu01e23c92016-03-14 06:01:11 +0800622 /* Whether the current dram is over 4GB */
Yong Wu41939982017-08-24 15:42:12 +0800623 data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT));
Yong Wub4dad402019-08-24 11:01:55 +0800624 if (!data->plat_data->has_4gb_mode)
625 data->enable_4GB = false;
Yong Wu01e23c92016-03-14 06:01:11 +0800626
Yong Wu0df4fab2016-02-23 01:20:50 +0800627 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
628 data->base = devm_ioremap_resource(dev, res);
629 if (IS_ERR(data->base))
630 return PTR_ERR(data->base);
Joerg Roedelb16c0172017-02-03 12:57:32 +0100631 ioaddr = res->start;
Yong Wu0df4fab2016-02-23 01:20:50 +0800632
633 data->irq = platform_get_irq(pdev, 0);
634 if (data->irq < 0)
635 return data->irq;
636
Yong Wu2aa4c252019-08-24 11:01:56 +0800637 if (data->plat_data->has_bclk) {
638 data->bclk = devm_clk_get(dev, "bclk");
639 if (IS_ERR(data->bclk))
640 return PTR_ERR(data->bclk);
641 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800642
643 larb_nr = of_count_phandle_with_args(dev->of_node,
644 "mediatek,larbs", NULL);
645 if (larb_nr < 0)
646 return larb_nr;
647 data->smi_imu.larb_nr = larb_nr;
648
649 for (i = 0; i < larb_nr; i++) {
650 struct device_node *larbnode;
651 struct platform_device *plarbdev;
Yong Wue6dec922017-08-21 19:00:16 +0800652 u32 id;
Yong Wu0df4fab2016-02-23 01:20:50 +0800653
654 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
655 if (!larbnode)
656 return -EINVAL;
657
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800658 if (!of_device_is_available(larbnode)) {
659 of_node_put(larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800660 continue;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800661 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800662
Yong Wue6dec922017-08-21 19:00:16 +0800663 ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
664 if (ret)/* The id is consecutive if there is no this property */
665 id = i;
666
Yong Wu0df4fab2016-02-23 01:20:50 +0800667 plarbdev = of_find_device_by_node(larbnode);
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800668 if (!plarbdev) {
669 of_node_put(larbnode);
Yong Wue6dec922017-08-21 19:00:16 +0800670 return -EPROBE_DEFER;
Wen Yang1eb8e4e2019-04-17 10:41:19 +0800671 }
Yong Wue6dec922017-08-21 19:00:16 +0800672 data->smi_imu.larb_imu[id].dev = &plarbdev->dev;
Yong Wu0df4fab2016-02-23 01:20:50 +0800673
Russell King00c7c812016-10-19 11:30:34 +0100674 component_match_add_release(dev, &match, release_of,
675 compare_of, larbnode);
Yong Wu0df4fab2016-02-23 01:20:50 +0800676 }
677
678 platform_set_drvdata(pdev, data);
679
680 ret = mtk_iommu_hw_init(data);
681 if (ret)
682 return ret;
683
Joerg Roedelb16c0172017-02-03 12:57:32 +0100684 ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
685 "mtk-iommu.%pa", &ioaddr);
686 if (ret)
687 return ret;
688
689 iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
690 iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);
691
692 ret = iommu_device_register(&data->iommu);
693 if (ret)
694 return ret;
695
Yong Wu7c3a2ec2017-08-21 19:00:17 +0800696 list_add_tail(&data->list, &m4ulist);
697
Yong Wu0df4fab2016-02-23 01:20:50 +0800698 if (!iommu_present(&platform_bus_type))
699 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
700
701 return component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
702}
703
704static int mtk_iommu_remove(struct platform_device *pdev)
705{
706 struct mtk_iommu_data *data = platform_get_drvdata(pdev);
707
Joerg Roedelb16c0172017-02-03 12:57:32 +0100708 iommu_device_sysfs_remove(&data->iommu);
709 iommu_device_unregister(&data->iommu);
710
Yong Wu0df4fab2016-02-23 01:20:50 +0800711 if (iommu_present(&platform_bus_type))
712 bus_set_iommu(&platform_bus_type, NULL);
713
Yong Wu0df4fab2016-02-23 01:20:50 +0800714 clk_disable_unprepare(data->bclk);
715 devm_free_irq(&pdev->dev, data->irq, data);
716 component_master_del(&pdev->dev, &mtk_iommu_com_ops);
717 return 0;
718}
719
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100720static int __maybe_unused mtk_iommu_suspend(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800721{
722 struct mtk_iommu_data *data = dev_get_drvdata(dev);
723 struct mtk_iommu_suspend_reg *reg = &data->reg;
724 void __iomem *base = data->base;
725
726 reg->standard_axi_mode = readl_relaxed(base +
727 REG_MMU_STANDARD_AXI_MODE);
728 reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
729 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
730 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
731 reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800732 reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
Yong Wu6254b642017-08-21 19:00:19 +0800733 clk_disable_unprepare(data->bclk);
Yong Wu0df4fab2016-02-23 01:20:50 +0800734 return 0;
735}
736
Arnd Bergmannfd99f792016-02-29 10:19:07 +0100737static int __maybe_unused mtk_iommu_resume(struct device *dev)
Yong Wu0df4fab2016-02-23 01:20:50 +0800738{
739 struct mtk_iommu_data *data = dev_get_drvdata(dev);
740 struct mtk_iommu_suspend_reg *reg = &data->reg;
741 void __iomem *base = data->base;
Yong Wu6254b642017-08-21 19:00:19 +0800742 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800743
Yong Wu6254b642017-08-21 19:00:19 +0800744 ret = clk_prepare_enable(data->bclk);
745 if (ret) {
746 dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
747 return ret;
748 }
Yong Wu0df4fab2016-02-23 01:20:50 +0800749 writel_relaxed(reg->standard_axi_mode,
750 base + REG_MMU_STANDARD_AXI_MODE);
751 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
752 writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
753 writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
754 writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
Yong Wu70ca6082018-03-18 09:52:54 +0800755 writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
Yong Wue6dec922017-08-21 19:00:16 +0800756 if (data->m4u_dom)
757 writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0],
758 base + REG_MMU_PT_BASE_ADDR);
Yong Wu0df4fab2016-02-23 01:20:50 +0800759 return 0;
760}
761
Yong Wue6dec922017-08-21 19:00:16 +0800762static const struct dev_pm_ops mtk_iommu_pm_ops = {
Yong Wu6254b642017-08-21 19:00:19 +0800763 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume)
Yong Wu0df4fab2016-02-23 01:20:50 +0800764};
765
Yong Wucecdce92019-08-24 11:01:47 +0800766static const struct mtk_iommu_plat_data mt2712_data = {
767 .m4u_plat = M4U_MT2712,
Yong Wub4dad402019-08-24 11:01:55 +0800768 .has_4gb_mode = true,
Yong Wu2aa4c252019-08-24 11:01:56 +0800769 .has_bclk = true,
Yong Wub3e5eee72019-08-24 11:01:57 +0800770 .larbid_remap = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Yong Wucecdce92019-08-24 11:01:47 +0800771};
772
773static const struct mtk_iommu_plat_data mt8173_data = {
774 .m4u_plat = M4U_MT8173,
Yong Wub4dad402019-08-24 11:01:55 +0800775 .has_4gb_mode = true,
Yong Wu2aa4c252019-08-24 11:01:56 +0800776 .has_bclk = true,
Yong Wub3e5eee72019-08-24 11:01:57 +0800777 .larbid_remap = {0, 1, 2, 3, 4, 5}, /* Linear mapping. */
Yong Wucecdce92019-08-24 11:01:47 +0800778};
779
Yong Wu0df4fab2016-02-23 01:20:50 +0800780static const struct of_device_id mtk_iommu_of_ids[] = {
Yong Wucecdce92019-08-24 11:01:47 +0800781 { .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
782 { .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
Yong Wu0df4fab2016-02-23 01:20:50 +0800783 {}
784};
785
786static struct platform_driver mtk_iommu_driver = {
787 .probe = mtk_iommu_probe,
788 .remove = mtk_iommu_remove,
789 .driver = {
790 .name = "mtk-iommu",
Yong Wue6dec922017-08-21 19:00:16 +0800791 .of_match_table = of_match_ptr(mtk_iommu_of_ids),
Yong Wu0df4fab2016-02-23 01:20:50 +0800792 .pm = &mtk_iommu_pm_ops,
793 }
794};
795
Yong Wue6dec922017-08-21 19:00:16 +0800796static int __init mtk_iommu_init(void)
Yong Wu0df4fab2016-02-23 01:20:50 +0800797{
798 int ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800799
800 ret = platform_driver_register(&mtk_iommu_driver);
Yong Wue6dec922017-08-21 19:00:16 +0800801 if (ret != 0)
802 pr_err("Failed to register MTK IOMMU driver\n");
Yong Wu0df4fab2016-02-23 01:20:50 +0800803
Yong Wue6dec922017-08-21 19:00:16 +0800804 return ret;
Yong Wu0df4fab2016-02-23 01:20:50 +0800805}
806
Yong Wue6dec922017-08-21 19:00:16 +0800807subsys_initcall(mtk_iommu_init)