mtk_iommu.c 29.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5
/*
 * Copyright (c) 2015-2016 MediaTek Inc.
 * Author: Yong Wu <yong.wu@mediatek.com>
 */
6
#include <linux/bitfield.h>
7 8 9 10
#include <linux/bug.h>
#include <linux/clk.h>
#include <linux/component.h>
#include <linux/device.h>
11
#include <linux/dma-direct.h>
12 13 14 15 16 17 18
#include <linux/dma-iommu.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iommu.h>
#include <linux/iopoll.h>
#include <linux/list.h>
19
#include <linux/mfd/syscon.h>
20 21 22 23 24
#include <linux/of_address.h>
#include <linux/of_iommu.h>
#include <linux/of_irq.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
25
#include <linux/pm_runtime.h>
26
#include <linux/regmap.h>
27 28
#include <linux/slab.h>
#include <linux/spinlock.h>
29
#include <linux/soc/mediatek/infracfg.h>
30 31 32
#include <asm/barrier.h>
#include <soc/mediatek/smi.h>

33
#include "mtk_iommu.h"
34 35

#define REG_MMU_PT_BASE_ADDR			0x000
36
#define MMU_PT_ADDR_MASK			GENMASK(31, 7)
37 38 39 40 41 42 43 44

#define REG_MMU_INVALIDATE			0x020
#define F_ALL_INVLD				0x2
#define F_MMU_INV_RANGE				0x1

#define REG_MMU_INVLD_START_A			0x024
#define REG_MMU_INVLD_END_A			0x028

45
#define REG_MMU_INV_SEL_GEN2			0x02c
46
#define REG_MMU_INV_SEL_GEN1			0x038
47 48 49
#define F_INVLD_EN0				BIT(0)
#define F_INVLD_EN1				BIT(1)

50
#define REG_MMU_MISC_CTRL			0x048
51 52 53
#define F_MMU_IN_ORDER_WR_EN_MASK		(BIT(1) | BIT(17))
#define F_MMU_STANDARD_AXI_MODE_MASK		(BIT(3) | BIT(19))

54
#define REG_MMU_DCM_DIS				0x050
55 56
#define REG_MMU_WR_LEN_CTRL			0x054
#define F_MMU_WR_THROT_DIS_MASK			(BIT(5) | BIT(21))
57 58

#define REG_MMU_CTRL_REG			0x110
59
#define F_MMU_TF_PROT_TO_PROGRAM_ADDR		(2 << 4)
60
#define F_MMU_PREFETCH_RT_REPLACE_MOD		BIT(4)
61
#define F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173	(2 << 5)
62 63

#define REG_MMU_IVRP_PADDR			0x114
64

65 66
#define REG_MMU_VLD_PA_RNG			0x118
#define F_MMU_VLD_PA_RNG(EA, SA)		(((EA) << 8) | (SA))
67 68 69 70 71 72 73 74 75 76 77

#define REG_MMU_INT_CONTROL0			0x120
#define F_L2_MULIT_HIT_EN			BIT(0)
#define F_TABLE_WALK_FAULT_INT_EN		BIT(1)
#define F_PREETCH_FIFO_OVERFLOW_INT_EN		BIT(2)
#define F_MISS_FIFO_OVERFLOW_INT_EN		BIT(3)
#define F_PREFETCH_FIFO_ERR_INT_EN		BIT(5)
#define F_MISS_FIFO_ERR_INT_EN			BIT(6)
#define F_INT_CLR_BIT				BIT(12)

#define REG_MMU_INT_MAIN_CONTROL		0x124
Y
Yong Wu 已提交
78 79 80 81 82 83 84 85
						/* mmu0 | mmu1 */
#define F_INT_TRANSLATION_FAULT			(BIT(0) | BIT(7))
#define F_INT_MAIN_MULTI_HIT_FAULT		(BIT(1) | BIT(8))
#define F_INT_INVALID_PA_FAULT			(BIT(2) | BIT(9))
#define F_INT_ENTRY_REPLACEMENT_FAULT		(BIT(3) | BIT(10))
#define F_INT_TLB_MISS_FAULT			(BIT(4) | BIT(11))
#define F_INT_MISS_TRANSACTION_FIFO_FAULT	(BIT(5) | BIT(12))
#define F_INT_PRETETCH_TRANSATION_FIFO_FAULT	(BIT(6) | BIT(13))
86 87 88 89

#define REG_MMU_CPE_DONE			0x12C

#define REG_MMU_FAULT_ST1			0x134
Y
Yong Wu 已提交
90 91
#define F_REG_MMU0_FAULT_MASK			GENMASK(6, 0)
#define F_REG_MMU1_FAULT_MASK			GENMASK(13, 7)
92

Y
Yong Wu 已提交
93
#define REG_MMU0_FAULT_VA			0x13c
94 95 96
#define F_MMU_INVAL_VA_31_12_MASK		GENMASK(31, 12)
#define F_MMU_INVAL_VA_34_32_MASK		GENMASK(11, 9)
#define F_MMU_INVAL_PA_34_32_MASK		GENMASK(8, 6)
97 98 99
#define F_MMU_FAULT_VA_WRITE_BIT		BIT(1)
#define F_MMU_FAULT_VA_LAYER_BIT		BIT(0)

Y
Yong Wu 已提交
100 101 102 103 104
#define REG_MMU0_INVLD_PA			0x140
#define REG_MMU1_FAULT_VA			0x144
#define REG_MMU1_INVLD_PA			0x148
#define REG_MMU0_INT_ID				0x150
#define REG_MMU1_INT_ID				0x154
105 106
#define F_MMU_INT_ID_COMM_ID(a)			(((a) >> 9) & 0x7)
#define F_MMU_INT_ID_SUB_COMM_ID(a)		(((a) >> 7) & 0x3)
Y
Yong Wu 已提交
107 108
#define F_MMU_INT_ID_LARB_ID(a)			(((a) >> 7) & 0x7)
#define F_MMU_INT_ID_PORT_ID(a)			(((a) >> 2) & 0x1f)
109

110
#define MTK_PROTECT_PA_ALIGN			256
111

112 113 114 115 116
#define HAS_4GB_MODE			BIT(0)
/* HW will use the EMI clock if there isn't the "bclk". */
#define HAS_BCLK			BIT(1)
#define HAS_VLD_PA_RNG			BIT(2)
#define RESET_AXI			BIT(3)
117
#define OUT_ORDER_WR_EN			BIT(4)
118
#define HAS_SUB_COMM			BIT(5)
119
#define WR_THROT_EN			BIT(6)
120
#define HAS_LEGACY_IVRP_PADDR		BIT(7)
121
#define IOVA_34_EN			BIT(8)
122 123 124 125

#define MTK_IOMMU_HAS_FLAG(pdata, _x) \
		((((pdata)->flags) & (_x)) == (_x))

126 127 128 129
struct mtk_iommu_domain {
	struct io_pgtable_cfg		cfg;
	struct io_pgtable_ops		*iop;

Y
Yong Wu 已提交
130
	struct mtk_iommu_data		*data;
131 132 133
	struct iommu_domain		domain;
};

A
Arvind Yadav 已提交
134
static const struct iommu_ops mtk_iommu_ops;
135

136 137
static int mtk_iommu_hw_init(const struct mtk_iommu_data *data);

138 139 140 141 142
#define MTK_IOMMU_TLB_ADDR(iova) ({					\
	dma_addr_t _addr = iova;					\
	((lower_32_bits(_addr) & GENMASK(31, 12)) | upper_32_bits(_addr));\
})

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/*
 * In M4U 4GB mode, the physical address is remapped as below:
 *
 * CPU Physical address:
 * ====================
 *
 * 0      1G       2G     3G       4G     5G
 * |---A---|---B---|---C---|---D---|---E---|
 * +--I/O--+------------Memory-------------+
 *
 * IOMMU output physical address:
 *  =============================
 *
 *                                 4G      5G     6G      7G      8G
 *                                 |---E---|---B---|---C---|---D---|
 *                                 +------------Memory-------------+
 *
 * The Region 'A'(I/O) can NOT be mapped by M4U; For Region 'B'/'C'/'D', the
 * bit32 of the CPU physical address always is needed to set, and for Region
 * 'E', the CPU physical address keep as is.
 * Additionally, The iommu consumers always use the CPU phyiscal address.
 */
165
#define MTK_IOMMU_4GB_MODE_REMAP_BASE	 0x140000000UL
166

167 168 169 170
static LIST_HEAD(m4ulist);	/* List all the M4U HWs */

#define for_each_m4u(data)	list_for_each_entry(data, &m4ulist, list)

171 172 173 174 175 176 177 178 179
struct mtk_iommu_iova_region {
	dma_addr_t		iova_base;
	unsigned long long	size;
};

static const struct mtk_iommu_iova_region single_domain[] = {
	{.iova_base = 0,		.size = SZ_4G},
};

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/*
 * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain
 * for the performance.
 *
 * Here always return the mtk_iommu_data of the first probed M4U where the
 * iommu domain information is recorded.
 */
static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void)
{
	struct mtk_iommu_data *data;

	for_each_m4u(data)
		return data;

	return NULL;
}

197 198 199 200 201
static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom)
{
	return container_of(dom, struct mtk_iommu_domain, domain);
}

202
static void mtk_iommu_tlb_flush_all(struct mtk_iommu_data *data)
203
{
204
	for_each_m4u(data) {
205 206 207
		if (pm_runtime_get_if_in_use(data->dev) <= 0)
			continue;

208
		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
209
			       data->base + data->plat_data->inv_sel_reg);
210 211
		writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE);
		wmb(); /* Make sure the tlb flush all done */
212 213

		pm_runtime_put(data->dev);
214
	}
215 216
}

217
static void mtk_iommu_tlb_flush_range_sync(unsigned long iova, size_t size,
218 219
					   size_t granule,
					   struct mtk_iommu_data *data)
220
{
221
	bool has_pm = !!data->dev->pm_domain;
222 223 224
	unsigned long flags;
	int ret;
	u32 tmp;
225

226
	for_each_m4u(data) {
227 228 229 230 231
		if (has_pm) {
			if (pm_runtime_get_if_in_use(data->dev) <= 0)
				continue;
		}

232
		spin_lock_irqsave(&data->tlb_lock, flags);
233
		writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0,
234
			       data->base + data->plat_data->inv_sel_reg);
235

236 237 238
		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova),
			       data->base + REG_MMU_INVLD_START_A);
		writel_relaxed(MTK_IOMMU_TLB_ADDR(iova + size - 1),
239 240 241
			       data->base + REG_MMU_INVLD_END_A);
		writel_relaxed(F_MMU_INV_RANGE,
			       data->base + REG_MMU_INVALIDATE);
242

243
		/* tlb sync */
244
		ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE,
245
						tmp, tmp != 0, 10, 1000);
246 247 248
		if (ret) {
			dev_warn(data->dev,
				 "Partial TLB flush timed out, falling back to full flush\n");
249
			mtk_iommu_tlb_flush_all(data);
250 251 252
		}
		/* Clear the CPE status */
		writel_relaxed(0, data->base + REG_MMU_CPE_DONE);
253
		spin_unlock_irqrestore(&data->tlb_lock, flags);
254 255 256

		if (has_pm)
			pm_runtime_put(data->dev);
257 258 259 260 261 262 263
	}
}

static irqreturn_t mtk_iommu_isr(int irq, void *dev_id)
{
	struct mtk_iommu_data *data = dev_id;
	struct mtk_iommu_domain *dom = data->m4u_dom;
264
	unsigned int fault_larb, fault_port, sub_comm = 0;
265 266
	u32 int_state, regval, va34_32, pa34_32;
	u64 fault_iova, fault_pa;
267 268 269 270
	bool layer, write;

	/* Read error info from registers */
	int_state = readl_relaxed(data->base + REG_MMU_FAULT_ST1);
Y
Yong Wu 已提交
271 272 273 274 275 276 277 278 279
	if (int_state & F_REG_MMU0_FAULT_MASK) {
		regval = readl_relaxed(data->base + REG_MMU0_INT_ID);
		fault_iova = readl_relaxed(data->base + REG_MMU0_FAULT_VA);
		fault_pa = readl_relaxed(data->base + REG_MMU0_INVLD_PA);
	} else {
		regval = readl_relaxed(data->base + REG_MMU1_INT_ID);
		fault_iova = readl_relaxed(data->base + REG_MMU1_FAULT_VA);
		fault_pa = readl_relaxed(data->base + REG_MMU1_INVLD_PA);
	}
280 281
	layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT;
	write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT;
282 283 284 285 286 287 288 289
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN)) {
		va34_32 = FIELD_GET(F_MMU_INVAL_VA_34_32_MASK, fault_iova);
		pa34_32 = FIELD_GET(F_MMU_INVAL_PA_34_32_MASK, fault_iova);
		fault_iova = fault_iova & F_MMU_INVAL_VA_31_12_MASK;
		fault_iova |= (u64)va34_32 << 32;
		fault_pa |= (u64)pa34_32 << 32;
	}

Y
Yong Wu 已提交
290
	fault_port = F_MMU_INT_ID_PORT_ID(regval);
291 292 293 294 295 296 297
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_SUB_COMM)) {
		fault_larb = F_MMU_INT_ID_COMM_ID(regval);
		sub_comm = F_MMU_INT_ID_SUB_COMM_ID(regval);
	} else {
		fault_larb = F_MMU_INT_ID_LARB_ID(regval);
	}
	fault_larb = data->plat_data->larbid_remap[fault_larb][sub_comm];
298

299 300 301 302
	if (report_iommu_fault(&dom->domain, data->dev, fault_iova,
			       write ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ)) {
		dev_err_ratelimited(
			data->dev,
303
			"fault type=0x%x iova=0x%llx pa=0x%llx larb=%d port=%d layer=%d %s\n",
304 305 306 307 308 309 310 311 312 313 314 315 316 317
			int_state, fault_iova, fault_pa, fault_larb, fault_port,
			layer, write ? "write" : "read");
	}

	/* Interrupt clear */
	regval = readl_relaxed(data->base + REG_MMU_INT_CONTROL0);
	regval |= F_INT_CLR_BIT;
	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);

	mtk_iommu_tlb_flush_all(data);

	return IRQ_HANDLED;
}

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
static int mtk_iommu_get_domain_id(struct device *dev,
				   const struct mtk_iommu_plat_data *plat_data)
{
	const struct mtk_iommu_iova_region *rgn = plat_data->iova_region;
	const struct bus_dma_region *dma_rgn = dev->dma_range_map;
	int i, candidate = -1;
	dma_addr_t dma_end;

	if (!dma_rgn || plat_data->iova_region_nr == 1)
		return 0;

	dma_end = dma_rgn->dma_start + dma_rgn->size - 1;
	for (i = 0; i < plat_data->iova_region_nr; i++, rgn++) {
		/* Best fit. */
		if (dma_rgn->dma_start == rgn->iova_base &&
		    dma_end == rgn->iova_base + rgn->size - 1)
			return i;
		/* ok if it is inside this region. */
		if (dma_rgn->dma_start >= rgn->iova_base &&
		    dma_end < rgn->iova_base + rgn->size)
			candidate = i;
	}

	if (candidate >= 0)
		return candidate;
	dev_err(dev, "Can NOT find the iommu domain id(%pad 0x%llx).\n",
		&dma_rgn->dma_start, dma_rgn->size);
	return -EINVAL;
}

348 349 350 351 352
static void mtk_iommu_config(struct mtk_iommu_data *data,
			     struct device *dev, bool enable)
{
	struct mtk_smi_larb_iommu    *larb_mmu;
	unsigned int                 larbid, portid;
353
	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
354
	int i;
355

356 357 358
	for (i = 0; i < fwspec->num_ids; ++i) {
		larbid = MTK_M4U_TO_LARB(fwspec->ids[i]);
		portid = MTK_M4U_TO_PORT(fwspec->ids[i]);
359
		larb_mmu = &data->larb_imu[larbid];
360 361 362 363 364 365 366 367 368 369 370

		dev_dbg(dev, "%s iommu port: %d\n",
			enable ? "enable" : "disable", portid);

		if (enable)
			larb_mmu->mmu |= MTK_SMI_MMU_EN(portid);
		else
			larb_mmu->mmu &= ~MTK_SMI_MMU_EN(portid);
	}
}

371
static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom,
372 373
				     struct mtk_iommu_data *data,
				     unsigned int domid)
374
{
375 376 377 378 379 380 381 382 383 384
	const struct mtk_iommu_iova_region *region;

	/* Use the exist domain as there is only one pgtable here. */
	if (data->m4u_dom) {
		dom->iop = data->m4u_dom->iop;
		dom->cfg = data->m4u_dom->cfg;
		dom->domain.pgsize_bitmap = data->m4u_dom->cfg.pgsize_bitmap;
		goto update_iova_region;
	}

385 386 387
	dom->cfg = (struct io_pgtable_cfg) {
		.quirks = IO_PGTABLE_QUIRK_ARM_NS |
			IO_PGTABLE_QUIRK_NO_PERMS |
388
			IO_PGTABLE_QUIRK_ARM_MTK_EXT,
389
		.pgsize_bitmap = mtk_iommu_ops.pgsize_bitmap,
390
		.ias = MTK_IOMMU_HAS_FLAG(data->plat_data, IOVA_34_EN) ? 34 : 32,
391 392 393
		.iommu_dev = data->dev,
	};

Y
Yong Wu 已提交
394 395 396 397 398
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE))
		dom->cfg.oas = data->enable_4GB ? 33 : 32;
	else
		dom->cfg.oas = 35;

399 400 401 402 403 404 405
	dom->iop = alloc_io_pgtable_ops(ARM_V7S, &dom->cfg, data);
	if (!dom->iop) {
		dev_err(data->dev, "Failed to alloc io pgtable\n");
		return -EINVAL;
	}

	/* Update our support page sizes bitmap */
406
	dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap;
407

408 409 410 411 412
update_iova_region:
	/* Update the iova region for this domain */
	region = data->plat_data->iova_region + domid;
	dom->domain.geometry.aperture_start = region->iova_base;
	dom->domain.geometry.aperture_end = region->iova_base + region->size - 1;
413
	dom->domain.geometry.force_aperture = true;
414 415 416 417 418 419 420 421 422 423 424 425 426 427
	return 0;
}

static struct iommu_domain *mtk_iommu_domain_alloc(unsigned type)
{
	struct mtk_iommu_domain *dom;

	if (type != IOMMU_DOMAIN_DMA)
		return NULL;

	dom = kzalloc(sizeof(*dom), GFP_KERNEL);
	if (!dom)
		return NULL;

428 429 430 431
	if (iommu_get_dma_cookie(&dom->domain)) {
		kfree(dom);
		return NULL;
	}
432 433 434 435 436 437 438 439 440 441 442 443 444

	return &dom->domain;
}

static void mtk_iommu_domain_free(struct iommu_domain *domain)
{
	iommu_put_dma_cookie(domain);
	kfree(to_mtk_domain(domain));
}

static int mtk_iommu_attach_device(struct iommu_domain *domain,
				   struct device *dev)
{
445
	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
446
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
447
	struct device *m4udev = data->dev;
448
	int ret, domid;
449

450
	if (!data)
451 452
		return -ENODEV;

453 454 455 456
	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
	if (domid < 0)
		return domid;

457
	if (!dom->data) {
458
		if (mtk_iommu_domain_finalise(dom, data, domid))
459 460 461 462
			return -ENODEV;
		dom->data = data;
	}

463
	if (!data->m4u_dom) { /* Initialize the M4U HW */
464 465 466 467
		ret = pm_runtime_resume_and_get(m4udev);
		if (ret < 0)
			return ret;

468
		ret = mtk_iommu_hw_init(data);
469 470
		if (ret) {
			pm_runtime_put(m4udev);
471
			return ret;
472
		}
473
		data->m4u_dom = dom;
474
		writel(dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK,
475
		       data->base + REG_MMU_PT_BASE_ADDR);
476 477

		pm_runtime_put(m4udev);
478 479
	}

480
	mtk_iommu_config(data, dev, true);
481 482 483 484 485 486
	return 0;
}

static void mtk_iommu_detach_device(struct iommu_domain *domain,
				    struct device *dev)
{
487
	struct mtk_iommu_data *data = dev_iommu_priv_get(dev);
488

489
	if (!data)
490 491 492 493 494 495
		return;

	mtk_iommu_config(data, dev, false);
}

static int mtk_iommu_map(struct iommu_domain *domain, unsigned long iova,
496
			 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
497 498 499
{
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);

500
	/* The "4GB mode" M4U physically can not use the lower remap of Dram. */
Y
Yong Wu 已提交
501
	if (dom->data->enable_4GB)
502 503
		paddr |= BIT_ULL(32);

Y
Yong Wu 已提交
504
	/* Synchronize with the tlb_lock */
505
	return dom->iop->map(dom->iop, iova, paddr, size, prot, gfp);
506 507 508
}

static size_t mtk_iommu_unmap(struct iommu_domain *domain,
509 510
			      unsigned long iova, size_t size,
			      struct iommu_iotlb_gather *gather)
511 512
{
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
513
	unsigned long end = iova + size - 1;
514

515 516 517 518
	if (gather->start > iova)
		gather->start = iova;
	if (gather->end < end)
		gather->end = end;
Y
Yong Wu 已提交
519
	return dom->iop->unmap(dom->iop, iova, size, gather);
520 521
}

522 523
static void mtk_iommu_flush_iotlb_all(struct iommu_domain *domain)
{
Y
Yong Wu 已提交
524 525 526
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);

	mtk_iommu_tlb_flush_all(dom->data);
527 528 529 530
}

static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,
				 struct iommu_iotlb_gather *gather)
531
{
Y
Yong Wu 已提交
532
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
533
	size_t length = gather->end - gather->start + 1;
534

535
	mtk_iommu_tlb_flush_range_sync(gather->start, length, gather->pgsize,
Y
Yong Wu 已提交
536
				       dom->data);
537 538
}

539 540 541
static void mtk_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
			       size_t size)
{
Y
Yong Wu 已提交
542
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
543

Y
Yong Wu 已提交
544
	mtk_iommu_tlb_flush_range_sync(iova, size, size, dom->data);
545 546
}

547 548 549 550 551 552 553
static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
					  dma_addr_t iova)
{
	struct mtk_iommu_domain *dom = to_mtk_domain(domain);
	phys_addr_t pa;

	pa = dom->iop->iova_to_phys(dom->iop, iova);
Y
Yong Wu 已提交
554
	if (dom->data->enable_4GB && pa >= MTK_IOMMU_4GB_MODE_REMAP_BASE)
555
		pa &= ~BIT_ULL(32);
556

557 558 559
	return pa;
}

560
static struct iommu_device *mtk_iommu_probe_device(struct device *dev)
561
{
562
	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
563
	struct mtk_iommu_data *data;
564

565
	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
566
		return ERR_PTR(-ENODEV); /* Not a iommu client device */
567

568
	data = dev_iommu_priv_get(dev);
569

570
	return &data->iommu;
571 572
}

573
static void mtk_iommu_release_device(struct device *dev)
574
{
575
	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
576

577
	if (!fwspec || fwspec->ops != &mtk_iommu_ops)
578 579
		return;

580
	iommu_fwspec_free(dev);
581 582 583 584
}

static struct iommu_group *mtk_iommu_device_group(struct device *dev)
{
585
	struct mtk_iommu_data *data = mtk_iommu_get_m4u_data();
586
	struct iommu_group *group;
587
	int domid;
588

589
	if (!data)
590 591
		return ERR_PTR(-ENODEV);

592 593 594 595
	domid = mtk_iommu_get_domain_id(dev, data->plat_data);
	if (domid < 0)
		return ERR_PTR(domid);

596 597 598 599 600
	group = data->m4u_group[domid];
	if (!group) {
		group = iommu_group_alloc();
		if (!IS_ERR(group))
			data->m4u_group[domid] = group;
601
	} else {
602
		iommu_group_ref_get(group);
603
	}
604
	return group;
605 606 607 608 609 610 611 612 613 614 615 616
}

static int mtk_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
{
	struct platform_device *m4updev;

	if (args->args_count != 1) {
		dev_err(dev, "invalid #iommu-cells(%d) property for IOMMU\n",
			args->args_count);
		return -EINVAL;
	}

617
	if (!dev_iommu_priv_get(dev)) {
618 619 620 621 622
		/* Get the m4u device */
		m4updev = of_find_device_by_node(args->np);
		if (WARN_ON(!m4updev))
			return -EINVAL;

623
		dev_iommu_priv_set(dev, platform_get_drvdata(m4updev));
624 625
	}

626
	return iommu_fwspec_add_ids(dev, args->args, 1);
627 628
}

A
Arvind Yadav 已提交
629
static const struct iommu_ops mtk_iommu_ops = {
630 631 632 633 634 635
	.domain_alloc	= mtk_iommu_domain_alloc,
	.domain_free	= mtk_iommu_domain_free,
	.attach_dev	= mtk_iommu_attach_device,
	.detach_dev	= mtk_iommu_detach_device,
	.map		= mtk_iommu_map,
	.unmap		= mtk_iommu_unmap,
636
	.flush_iotlb_all = mtk_iommu_flush_iotlb_all,
637
	.iotlb_sync	= mtk_iommu_iotlb_sync,
638
	.iotlb_sync_map	= mtk_iommu_sync_map,
639
	.iova_to_phys	= mtk_iommu_iova_to_phys,
640 641
	.probe_device	= mtk_iommu_probe_device,
	.release_device	= mtk_iommu_release_device,
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	.device_group	= mtk_iommu_device_group,
	.of_xlate	= mtk_iommu_of_xlate,
	.pgsize_bitmap	= SZ_4K | SZ_64K | SZ_1M | SZ_16M,
};

static int mtk_iommu_hw_init(const struct mtk_iommu_data *data)
{
	u32 regval;
	int ret;

	ret = clk_prepare_enable(data->bclk);
	if (ret) {
		dev_err(data->dev, "Failed to enable iommu bclk(%d)\n", ret);
		return ret;
	}

658
	if (data->plat_data->m4u_plat == M4U_MT8173) {
659 660
		regval = F_MMU_PREFETCH_RT_REPLACE_MOD |
			 F_MMU_TF_PROT_TO_PROGRAM_ADDR_MT8173;
661 662 663 664
	} else {
		regval = readl_relaxed(data->base + REG_MMU_CTRL_REG);
		regval |= F_MMU_TF_PROT_TO_PROGRAM_ADDR;
	}
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	writel_relaxed(regval, data->base + REG_MMU_CTRL_REG);

	regval = F_L2_MULIT_HIT_EN |
		F_TABLE_WALK_FAULT_INT_EN |
		F_PREETCH_FIFO_OVERFLOW_INT_EN |
		F_MISS_FIFO_OVERFLOW_INT_EN |
		F_PREFETCH_FIFO_ERR_INT_EN |
		F_MISS_FIFO_ERR_INT_EN;
	writel_relaxed(regval, data->base + REG_MMU_INT_CONTROL0);

	regval = F_INT_TRANSLATION_FAULT |
		F_INT_MAIN_MULTI_HIT_FAULT |
		F_INT_INVALID_PA_FAULT |
		F_INT_ENTRY_REPLACEMENT_FAULT |
		F_INT_TLB_MISS_FAULT |
		F_INT_MISS_TRANSACTION_FIFO_FAULT |
		F_INT_PRETETCH_TRANSATION_FIFO_FAULT;
	writel_relaxed(regval, data->base + REG_MMU_INT_MAIN_CONTROL);

684
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_LEGACY_IVRP_PADDR))
685 686 687 688 689 690
		regval = (data->protect_base >> 1) | (data->enable_4GB << 31);
	else
		regval = lower_32_bits(data->protect_base) |
			 upper_32_bits(data->protect_base);
	writel_relaxed(regval, data->base + REG_MMU_IVRP_PADDR);

691 692
	if (data->enable_4GB &&
	    MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_VLD_PA_RNG)) {
693 694 695 696 697 698 699
		/*
		 * If 4GB mode is enabled, the validate PA range is from
		 * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30].
		 */
		regval = F_MMU_VLD_PA_RNG(7, 4);
		writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG);
	}
700
	writel_relaxed(0, data->base + REG_MMU_DCM_DIS);
701 702 703 704 705 706
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, WR_THROT_EN)) {
		/* write command throttling mode */
		regval = readl_relaxed(data->base + REG_MMU_WR_LEN_CTRL);
		regval &= ~F_MMU_WR_THROT_DIS_MASK;
		writel_relaxed(regval, data->base + REG_MMU_WR_LEN_CTRL);
	}
707

708
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, RESET_AXI)) {
709
		/* The register is called STANDARD_AXI_MODE in this case */
710 711 712 713 714 715
		regval = 0;
	} else {
		regval = readl_relaxed(data->base + REG_MMU_MISC_CTRL);
		regval &= ~F_MMU_STANDARD_AXI_MODE_MASK;
		if (MTK_IOMMU_HAS_FLAG(data->plat_data, OUT_ORDER_WR_EN))
			regval &= ~F_MMU_IN_ORDER_WR_EN_MASK;
716
	}
717
	writel_relaxed(regval, data->base + REG_MMU_MISC_CTRL);
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

	if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0,
			     dev_name(data->dev), (void *)data)) {
		writel_relaxed(0, data->base + REG_MMU_PT_BASE_ADDR);
		clk_disable_unprepare(data->bclk);
		dev_err(data->dev, "Failed @ IRQ-%d Request\n", data->irq);
		return -ENODEV;
	}

	return 0;
}

static const struct component_master_ops mtk_iommu_com_ops = {
	.bind		= mtk_iommu_bind,
	.unbind		= mtk_iommu_unbind,
};

static int mtk_iommu_probe(struct platform_device *pdev)
{
	struct mtk_iommu_data   *data;
	struct device           *dev = &pdev->dev;
739 740 741
	struct device_node	*larbnode, *smicomm_node;
	struct platform_device	*plarbdev;
	struct device_link	*link;
742
	struct resource         *res;
743
	resource_size_t		ioaddr;
744
	struct component_match  *match = NULL;
745
	struct regmap		*infracfg;
746
	void                    *protect;
747
	int                     i, larb_nr, ret;
748 749
	u32			val;
	char                    *p;
750 751 752 753 754

	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;
	data->dev = dev;
755
	data->plat_data = of_device_get_match_data(dev);
756 757 758 759 760 761 762

	/* Protect memory. HW will access here while translation fault.*/
	protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL);
	if (!protect)
		return -ENOMEM;
	data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN);

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_4GB_MODE)) {
		switch (data->plat_data->m4u_plat) {
		case M4U_MT2712:
			p = "mediatek,mt2712-infracfg";
			break;
		case M4U_MT8173:
			p = "mediatek,mt8173-infracfg";
			break;
		default:
			p = NULL;
		}

		infracfg = syscon_regmap_lookup_by_compatible(p);

		if (IS_ERR(infracfg))
			return PTR_ERR(infracfg);

		ret = regmap_read(infracfg, REG_INFRA_MISC, &val);
		if (ret)
			return ret;
		data->enable_4GB = !!(val & F_DDR_4GB_SUPPORT_EN);
	}
Y
Yong Wu 已提交
785

786 787 788 789
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	data->base = devm_ioremap_resource(dev, res);
	if (IS_ERR(data->base))
		return PTR_ERR(data->base);
790
	ioaddr = res->start;
791 792 793 794 795

	data->irq = platform_get_irq(pdev, 0);
	if (data->irq < 0)
		return data->irq;

796
	if (MTK_IOMMU_HAS_FLAG(data->plat_data, HAS_BCLK)) {
797 798 799 800
		data->bclk = devm_clk_get(dev, "bclk");
		if (IS_ERR(data->bclk))
			return PTR_ERR(data->bclk);
	}
801 802 803 804 805 806 807

	larb_nr = of_count_phandle_with_args(dev->of_node,
					     "mediatek,larbs", NULL);
	if (larb_nr < 0)
		return larb_nr;

	for (i = 0; i < larb_nr; i++) {
808
		u32 id;
809 810 811 812 813

		larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i);
		if (!larbnode)
			return -EINVAL;

814 815
		if (!of_device_is_available(larbnode)) {
			of_node_put(larbnode);
816
			continue;
817
		}
818

819 820 821 822
		ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id);
		if (ret)/* The id is consecutive if there is no this property */
			id = i;

823
		plarbdev = of_find_device_by_node(larbnode);
824 825
		if (!plarbdev) {
			of_node_put(larbnode);
826
			return -EPROBE_DEFER;
827
		}
828
		data->larb_imu[id].dev = &plarbdev->dev;
829

830 831
		component_match_add_release(dev, &match, release_of,
					    compare_of, larbnode);
832 833
	}

834 835 836 837 838 839 840 841 842
	/* Get smi-common dev from the last larb. */
	smicomm_node = of_parse_phandle(larbnode, "mediatek,smi", 0);
	if (!smicomm_node)
		return -EINVAL;

	plarbdev = of_find_device_by_node(smicomm_node);
	of_node_put(smicomm_node);
	data->smicomm_dev = &plarbdev->dev;

843 844
	pm_runtime_enable(dev);

845 846 847 848
	link = device_link_add(data->smicomm_dev, dev,
			DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME);
	if (!link) {
		dev_err(dev, "Unable link %s.\n", dev_name(data->smicomm_dev));
849
		goto out_runtime_disable;
850 851
	}

852 853
	platform_set_drvdata(pdev, data);

854 855 856
	ret = iommu_device_sysfs_add(&data->iommu, dev, NULL,
				     "mtk-iommu.%pa", &ioaddr);
	if (ret)
857
		goto out_link_remove;
858 859 860 861 862 863

	iommu_device_set_ops(&data->iommu, &mtk_iommu_ops);
	iommu_device_set_fwnode(&data->iommu, &pdev->dev.of_node->fwnode);

	ret = iommu_device_register(&data->iommu);
	if (ret)
864
		goto out_sysfs_remove;
865

866
	spin_lock_init(&data->tlb_lock);
867 868
	list_add_tail(&data->list, &m4ulist);

869 870 871 872 873
	if (!iommu_present(&platform_bus_type)) {
		ret = bus_set_iommu(&platform_bus_type, &mtk_iommu_ops);
		if (ret)
			goto out_list_del;
	}
874

875 876 877 878 879 880 881 882 883 884 885 886
	ret = component_master_add_with_match(dev, &mtk_iommu_com_ops, match);
	if (ret)
		goto out_bus_set_null;
	return ret;

out_bus_set_null:
	bus_set_iommu(&platform_bus_type, NULL);
out_list_del:
	list_del(&data->list);
	iommu_device_unregister(&data->iommu);
out_sysfs_remove:
	iommu_device_sysfs_remove(&data->iommu);
887 888
out_link_remove:
	device_link_remove(data->smicomm_dev, dev);
889 890
out_runtime_disable:
	pm_runtime_disable(dev);
891
	return ret;
892 893 894 895 896 897
}

static int mtk_iommu_remove(struct platform_device *pdev)
{
	struct mtk_iommu_data *data = platform_get_drvdata(pdev);

898 899 900
	iommu_device_sysfs_remove(&data->iommu);
	iommu_device_unregister(&data->iommu);

901 902 903 904
	if (iommu_present(&platform_bus_type))
		bus_set_iommu(&platform_bus_type, NULL);

	clk_disable_unprepare(data->bclk);
905
	device_link_remove(data->smicomm_dev, &pdev->dev);
906
	pm_runtime_disable(&pdev->dev);
907 908 909 910 911
	devm_free_irq(&pdev->dev, data->irq, data);
	component_master_del(&pdev->dev, &mtk_iommu_com_ops);
	return 0;
}

912
static int __maybe_unused mtk_iommu_runtime_suspend(struct device *dev)
913 914 915 916 917
{
	struct mtk_iommu_data *data = dev_get_drvdata(dev);
	struct mtk_iommu_suspend_reg *reg = &data->reg;
	void __iomem *base = data->base;

918
	reg->wr_len_ctrl = readl_relaxed(base + REG_MMU_WR_LEN_CTRL);
919
	reg->misc_ctrl = readl_relaxed(base + REG_MMU_MISC_CTRL);
920 921 922 923
	reg->dcm_dis = readl_relaxed(base + REG_MMU_DCM_DIS);
	reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG);
	reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0);
	reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL);
924
	reg->ivrp_paddr = readl_relaxed(base + REG_MMU_IVRP_PADDR);
925
	reg->vld_pa_rng = readl_relaxed(base + REG_MMU_VLD_PA_RNG);
926
	clk_disable_unprepare(data->bclk);
927 928 929
	return 0;
}

930
static int __maybe_unused mtk_iommu_runtime_resume(struct device *dev)
931 932 933
{
	struct mtk_iommu_data *data = dev_get_drvdata(dev);
	struct mtk_iommu_suspend_reg *reg = &data->reg;
934
	struct mtk_iommu_domain *m4u_dom = data->m4u_dom;
935
	void __iomem *base = data->base;
936
	int ret;
937

938 939 940
	/* Avoid first resume to affect the default value of registers below. */
	if (!m4u_dom)
		return 0;
941 942 943 944 945
	ret = clk_prepare_enable(data->bclk);
	if (ret) {
		dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret);
		return ret;
	}
946
	writel_relaxed(reg->wr_len_ctrl, base + REG_MMU_WR_LEN_CTRL);
947
	writel_relaxed(reg->misc_ctrl, base + REG_MMU_MISC_CTRL);
948 949 950 951
	writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS);
	writel_relaxed(reg->ctrl_reg, base + REG_MMU_CTRL_REG);
	writel_relaxed(reg->int_control0, base + REG_MMU_INT_CONTROL0);
	writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL);
952
	writel_relaxed(reg->ivrp_paddr, base + REG_MMU_IVRP_PADDR);
953
	writel_relaxed(reg->vld_pa_rng, base + REG_MMU_VLD_PA_RNG);
954
	writel(m4u_dom->cfg.arm_v7s_cfg.ttbr & MMU_PT_ADDR_MASK, base + REG_MMU_PT_BASE_ADDR);
955 956 957
	return 0;
}

958
static const struct dev_pm_ops mtk_iommu_pm_ops = {
959 960 961
	SET_RUNTIME_PM_OPS(mtk_iommu_runtime_suspend, mtk_iommu_runtime_resume, NULL)
	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				     pm_runtime_force_resume)
962 963
};

964 965
static const struct mtk_iommu_plat_data mt2712_data = {
	.m4u_plat     = M4U_MT2712,
966
	.flags        = HAS_4GB_MODE | HAS_BCLK | HAS_VLD_PA_RNG,
967
	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
968 969
	.iova_region  = single_domain,
	.iova_region_nr = ARRAY_SIZE(single_domain),
970
	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}},
971 972
};

973 974 975 976
static const struct mtk_iommu_plat_data mt6779_data = {
	.m4u_plat      = M4U_MT6779,
	.flags         = HAS_SUB_COMM | OUT_ORDER_WR_EN | WR_THROT_EN,
	.inv_sel_reg   = REG_MMU_INV_SEL_GEN2,
977 978
	.iova_region   = single_domain,
	.iova_region_nr = ARRAY_SIZE(single_domain),
979
	.larbid_remap  = {{0}, {1}, {2}, {3}, {5}, {7, 8}, {10}, {9}},
980 981
};

982 983 984 985
static const struct mtk_iommu_plat_data mt8167_data = {
	.m4u_plat     = M4U_MT8167,
	.flags        = RESET_AXI | HAS_LEGACY_IVRP_PADDR,
	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
986 987
	.iova_region  = single_domain,
	.iova_region_nr = ARRAY_SIZE(single_domain),
988 989 990
	.larbid_remap = {{0}, {1}, {2}}, /* Linear mapping. */
};

991 992
static const struct mtk_iommu_plat_data mt8173_data = {
	.m4u_plat     = M4U_MT8173,
993 994
	.flags	      = HAS_4GB_MODE | HAS_BCLK | RESET_AXI |
			HAS_LEGACY_IVRP_PADDR,
995
	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
996 997
	.iova_region  = single_domain,
	.iova_region_nr = ARRAY_SIZE(single_domain),
998
	.larbid_remap = {{0}, {1}, {2}, {3}, {4}, {5}}, /* Linear mapping. */
999 1000
};

1001 1002
static const struct mtk_iommu_plat_data mt8183_data = {
	.m4u_plat     = M4U_MT8183,
1003
	.flags        = RESET_AXI,
1004
	.inv_sel_reg  = REG_MMU_INV_SEL_GEN1,
1005 1006
	.iova_region  = single_domain,
	.iova_region_nr = ARRAY_SIZE(single_domain),
1007
	.larbid_remap = {{0}, {4}, {5}, {6}, {7}, {2}, {3}, {1}},
1008 1009
};

1010
static const struct of_device_id mtk_iommu_of_ids[] = {
1011
	{ .compatible = "mediatek,mt2712-m4u", .data = &mt2712_data},
1012
	{ .compatible = "mediatek,mt6779-m4u", .data = &mt6779_data},
1013
	{ .compatible = "mediatek,mt8167-m4u", .data = &mt8167_data},
1014
	{ .compatible = "mediatek,mt8173-m4u", .data = &mt8173_data},
1015
	{ .compatible = "mediatek,mt8183-m4u", .data = &mt8183_data},
1016 1017 1018 1019 1020 1021 1022 1023
	{}
};

static struct platform_driver mtk_iommu_driver = {
	.probe	= mtk_iommu_probe,
	.remove	= mtk_iommu_remove,
	.driver	= {
		.name = "mtk-iommu",
1024
		.of_match_table = mtk_iommu_of_ids,
1025 1026 1027 1028
		.pm = &mtk_iommu_pm_ops,
	}
};

1029
static int __init mtk_iommu_init(void)
1030 1031 1032 1033
{
	int ret;

	ret = platform_driver_register(&mtk_iommu_driver);
1034 1035
	if (ret != 0)
		pr_err("Failed to register MTK IOMMU driver\n");
1036

1037
	return ret;
1038 1039
}

1040
subsys_initcall(mtk_iommu_init)