exynos-iommu.c 38.8 KB
Newer Older
1 2
/*
 * Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14
 *		http://www.samsung.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
#define DEBUG
#endif

#include <linux/clk.h>
15
#include <linux/dma-mapping.h>
16
#include <linux/err.h>
17
#include <linux/io.h>
18
#include <linux/iommu.h>
19
#include <linux/interrupt.h>
20
#include <linux/kmemleak.h>
21
#include <linux/list.h>
22 23 24
#include <linux/of.h>
#include <linux/of_iommu.h>
#include <linux/of_platform.h>
25 26 27
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
28
#include <linux/dma-iommu.h>
29

30 31 32
typedef u32 sysmmu_iova_t;
typedef u32 sysmmu_pte_t;

S
Sachin Kamat 已提交
33
/* We do not consider super section mapping (16MB) */
34 35 36 37 38 39 40 41 42 43 44 45
#define SECT_ORDER 20
#define LPAGE_ORDER 16
#define SPAGE_ORDER 12

#define SECT_SIZE (1 << SECT_ORDER)
#define LPAGE_SIZE (1 << LPAGE_ORDER)
#define SPAGE_SIZE (1 << SPAGE_ORDER)

#define SECT_MASK (~(SECT_SIZE - 1))
#define LPAGE_MASK (~(LPAGE_SIZE - 1))
#define SPAGE_MASK (~(SPAGE_SIZE - 1))

46 47 48 49 50 51
#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
			   ((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
			  ((*(sent) & 3) == 1))
52 53 54 55 56 57
#define lv1ent_section(sent) ((*(sent) & 3) == 2)

#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
#define lv2ent_small(pent) ((*(pent) & 2) == 2)
#define lv2ent_large(pent) ((*(pent) & 3) == 1)

58 59 60 61 62 63 64 65 66 67 68 69
/*
 * v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces
 * v5.0 introduced support for 36bit physical address space by shifting
 * all page entry values by 4 bits.
 * All SYSMMU controllers in the system support the address spaces of the same
 * size, so PG_ENT_SHIFT can be initialized on first SYSMMU probe to proper
 * value (0 or 4).
 */
static short PG_ENT_SHIFT = -1;
#define SYSMMU_PG_ENT_SHIFT 0
#define SYSMMU_V5_PG_ENT_SHIFT 4

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static const sysmmu_pte_t *LV1_PROT;
static const sysmmu_pte_t SYSMMU_LV1_PROT[] = {
	((0 << 15) | (0 << 10)), /* no access */
	((1 << 15) | (1 << 10)), /* IOMMU_READ only */
	((0 << 15) | (1 << 10)), /* IOMMU_WRITE not supported, use read/write */
	((0 << 15) | (1 << 10)), /* IOMMU_READ | IOMMU_WRITE */
};
static const sysmmu_pte_t SYSMMU_V5_LV1_PROT[] = {
	(0 << 4), /* no access */
	(1 << 4), /* IOMMU_READ only */
	(2 << 4), /* IOMMU_WRITE only */
	(3 << 4), /* IOMMU_READ | IOMMU_WRITE */
};

static const sysmmu_pte_t *LV2_PROT;
static const sysmmu_pte_t SYSMMU_LV2_PROT[] = {
	((0 << 9) | (0 << 4)), /* no access */
	((1 << 9) | (1 << 4)), /* IOMMU_READ only */
	((0 << 9) | (1 << 4)), /* IOMMU_WRITE not supported, use read/write */
	((0 << 9) | (1 << 4)), /* IOMMU_READ | IOMMU_WRITE */
};
static const sysmmu_pte_t SYSMMU_V5_LV2_PROT[] = {
	(0 << 2), /* no access */
	(1 << 2), /* IOMMU_READ only */
	(2 << 2), /* IOMMU_WRITE only */
	(3 << 2), /* IOMMU_READ | IOMMU_WRITE */
};

#define SYSMMU_SUPPORTED_PROT_BITS (IOMMU_READ | IOMMU_WRITE)

100 101 102 103 104 105 106
#define sect_to_phys(ent) (((phys_addr_t) ent) << PG_ENT_SHIFT)
#define section_phys(sent) (sect_to_phys(*(sent)) & SECT_MASK)
#define section_offs(iova) (iova & (SECT_SIZE - 1))
#define lpage_phys(pent) (sect_to_phys(*(pent)) & LPAGE_MASK)
#define lpage_offs(iova) (iova & (LPAGE_SIZE - 1))
#define spage_phys(pent) (sect_to_phys(*(pent)) & SPAGE_MASK)
#define spage_offs(iova) (iova & (SPAGE_SIZE - 1))
107 108

#define NUM_LV1ENTRIES 4096
109
#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
110

111 112 113 114 115 116 117 118 119 120
static u32 lv1ent_offset(sysmmu_iova_t iova)
{
	return iova >> SECT_ORDER;
}

static u32 lv2ent_offset(sysmmu_iova_t iova)
{
	return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
}

121
#define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
122
#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
123 124

#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
125
#define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
126

127
#define mk_lv1ent_sect(pa, prot) ((pa >> PG_ENT_SHIFT) | LV1_PROT[prot] | 2)
128
#define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
129 130
#define mk_lv2ent_lpage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 1)
#define mk_lv2ent_spage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 2)
131 132 133 134 135

#define CTRL_ENABLE	0x5
#define CTRL_BLOCK	0x7
#define CTRL_DISABLE	0x0

136
#define CFG_LRU		0x1
137
#define CFG_EAP		(1 << 2)
138 139 140 141 142
#define CFG_QOS(n)	((n & 0xF) << 7)
#define CFG_ACGEN	(1 << 24) /* System MMU 3.3 only */
#define CFG_SYSSEL	(1 << 22) /* System MMU 3.2 only */
#define CFG_FLPDCACHE	(1 << 20) /* System MMU 3.2+ only */

143
/* common registers */
144 145 146
#define REG_MMU_CTRL		0x000
#define REG_MMU_CFG		0x004
#define REG_MMU_STATUS		0x008
147 148 149 150 151 152 153 154 155
#define REG_MMU_VERSION		0x034

#define MMU_MAJ_VER(val)	((val) >> 7)
#define MMU_MIN_VER(val)	((val) & 0x7F)
#define MMU_RAW_VER(reg)	(((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */

#define MAKE_MMU_VER(maj, min)	((((maj) & 0xF) << 7) | ((min) & 0x7F))

/* v1.x - v3.x registers */
156 157 158 159 160 161 162 163 164 165 166
#define REG_MMU_FLUSH		0x00C
#define REG_MMU_FLUSH_ENTRY	0x010
#define REG_PT_BASE_ADDR	0x014
#define REG_INT_STATUS		0x018
#define REG_INT_CLEAR		0x01C

#define REG_PAGE_FAULT_ADDR	0x024
#define REG_AW_FAULT_ADDR	0x028
#define REG_AR_FAULT_ADDR	0x02C
#define REG_DEFAULT_SLAVE_ADDR	0x030

167 168 169 170
/* v5.x registers */
#define REG_V5_PT_BASE_PFN	0x00C
#define REG_V5_MMU_FLUSH_ALL	0x010
#define REG_V5_MMU_FLUSH_ENTRY	0x014
171 172 173
#define REG_V5_MMU_FLUSH_RANGE	0x018
#define REG_V5_MMU_FLUSH_START	0x020
#define REG_V5_MMU_FLUSH_END	0x024
174 175 176 177
#define REG_V5_INT_STATUS	0x060
#define REG_V5_INT_CLEAR	0x064
#define REG_V5_FAULT_AR_VA	0x070
#define REG_V5_FAULT_AW_VA	0x080
178

179 180
#define has_sysmmu(dev)		(dev->archdata.iommu != NULL)

181
static struct device *dma_dev;
182
static struct kmem_cache *lv2table_kmem_cache;
183 184
static sysmmu_pte_t *zero_lv2_table;
#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
185

186
static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
187 188 189 190
{
	return pgtable + lv1ent_offset(iova);
}

191
static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
192
{
193
	return (sysmmu_pte_t *)phys_to_virt(
C
Cho KyongHo 已提交
194
				lv2table_base(sent)) + lv2ent_offset(iova);
195 196
}

197 198 199 200 201 202 203 204
/*
 * IOMMU fault information register
 */
struct sysmmu_fault_info {
	unsigned int bit;	/* bit number in STATUS register */
	unsigned short addr_reg; /* register to read VA fault address */
	const char *name;	/* human readable fault name */
	unsigned int type;	/* fault type for report_iommu_fault */
205 206
};

207 208 209 210 211 212 213 214 215
static const struct sysmmu_fault_info sysmmu_faults[] = {
	{ 0, REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
	{ 1, REG_AR_FAULT_ADDR, "AR MULTI-HIT", IOMMU_FAULT_READ },
	{ 2, REG_AW_FAULT_ADDR, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
	{ 3, REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
	{ 4, REG_AR_FAULT_ADDR, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
	{ 5, REG_AR_FAULT_ADDR, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
	{ 6, REG_AW_FAULT_ADDR, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
	{ 7, REG_AW_FAULT_ADDR, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
216 217
};

218 219 220 221 222 223 224 225 226 227 228 229 230
static const struct sysmmu_fault_info sysmmu_v5_faults[] = {
	{ 0, REG_V5_FAULT_AR_VA, "AR PTW", IOMMU_FAULT_READ },
	{ 1, REG_V5_FAULT_AR_VA, "AR PAGE", IOMMU_FAULT_READ },
	{ 2, REG_V5_FAULT_AR_VA, "AR MULTI-HIT", IOMMU_FAULT_READ },
	{ 3, REG_V5_FAULT_AR_VA, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
	{ 4, REG_V5_FAULT_AR_VA, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
	{ 16, REG_V5_FAULT_AW_VA, "AW PTW", IOMMU_FAULT_WRITE },
	{ 17, REG_V5_FAULT_AW_VA, "AW PAGE", IOMMU_FAULT_WRITE },
	{ 18, REG_V5_FAULT_AW_VA, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
	{ 19, REG_V5_FAULT_AW_VA, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
	{ 20, REG_V5_FAULT_AW_VA, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
};

231 232 233 234 235 236
/*
 * This structure is attached to dev.archdata.iommu of the master device
 * on device add, contains a list of SYSMMU controllers defined by device tree,
 * which are bound to given master device. It is usually referenced by 'owner'
 * pointer.
*/
237
struct exynos_iommu_owner {
238
	struct list_head controllers;	/* list of sysmmu_drvdata.owner_node */
239
	struct iommu_domain *domain;	/* domain this device is attached */
240
	struct mutex rpm_lock;		/* for runtime pm of all sysmmus */
241 242
};

243 244 245 246 247 248
/*
 * This structure exynos specific generalization of struct iommu_domain.
 * It contains list of SYSMMU controllers from all master devices, which has
 * been attached to this domain and page tables of IO address space defined by
 * it. It is usually referenced by 'domain' pointer.
 */
249
struct exynos_iommu_domain {
250 251 252 253 254
	struct list_head clients; /* list of sysmmu_drvdata.domain_node */
	sysmmu_pte_t *pgtable;	/* lv1 page table, 16KB */
	short *lv2entcnt;	/* free lv2 entry counter for each section */
	spinlock_t lock;	/* lock for modyfying list of clients */
	spinlock_t pgtablelock;	/* lock for modifying page table @ pgtable */
255
	struct iommu_domain domain; /* generic domain data structure */
256 257
};

258 259 260 261 262 263
/*
 * This structure hold all data of a single SYSMMU controller, this includes
 * hw resources like registers and clocks, pointers and list nodes to connect
 * it to all other structures, internal state and parameters read from device
 * tree. It is usually referenced by 'data' pointer.
 */
264
struct sysmmu_drvdata {
265 266
	struct device *sysmmu;		/* SYSMMU controller device */
	struct device *master;		/* master device (owner) */
267
	struct device_link *link;	/* runtime PM link to master */
268 269
	void __iomem *sfrbase;		/* our registers */
	struct clk *clk;		/* SYSMMU's clock */
270 271
	struct clk *aclk;		/* SYSMMU's aclk clock */
	struct clk *pclk;		/* SYSMMU's pclk clock */
272 273
	struct clk *clk_master;		/* master's device clock */
	spinlock_t lock;		/* lock for modyfying state */
274
	bool active;			/* current status */
275 276
	struct exynos_iommu_domain *domain; /* domain we belong to */
	struct list_head domain_node;	/* node for domain clients list */
277
	struct list_head owner_node;	/* node for owner controllers list */
278 279
	phys_addr_t pgtable;		/* assigned page table structure */
	unsigned int version;		/* our version */
280 281

	struct iommu_device iommu;	/* IOMMU core handle */
282 283
};

284 285 286 287 288
static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
{
	return container_of(dom, struct exynos_iommu_domain, domain);
}

289
static void sysmmu_unblock(struct sysmmu_drvdata *data)
290
{
291
	writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
292 293
}

294
static bool sysmmu_block(struct sysmmu_drvdata *data)
295 296 297
{
	int i = 120;

298 299
	writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
	while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
300 301
		--i;

302
	if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
303
		sysmmu_unblock(data);
304 305 306 307 308 309
		return false;
	}

	return true;
}

310
static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
311
{
312
	if (MMU_MAJ_VER(data->version) < 5)
313
		writel(0x1, data->sfrbase + REG_MMU_FLUSH);
314
	else
315
		writel(0x1, data->sfrbase + REG_V5_MMU_FLUSH_ALL);
316 317
}

318
static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
319
				sysmmu_iova_t iova, unsigned int num_inv)
320
{
321
	unsigned int i;
322

323 324
	if (MMU_MAJ_VER(data->version) < 5) {
		for (i = 0; i < num_inv; i++) {
325
			writel((iova & SPAGE_MASK) | 1,
326
				     data->sfrbase + REG_MMU_FLUSH_ENTRY);
327 328 329 330
			iova += SPAGE_SIZE;
		}
	} else {
		if (num_inv == 1) {
331
			writel((iova & SPAGE_MASK) | 1,
332
				     data->sfrbase + REG_V5_MMU_FLUSH_ENTRY);
333 334 335 336 337 338 339
		} else {
			writel((iova & SPAGE_MASK),
				     data->sfrbase + REG_V5_MMU_FLUSH_START);
			writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,
				     data->sfrbase + REG_V5_MMU_FLUSH_END);
			writel(1, data->sfrbase + REG_V5_MMU_FLUSH_RANGE);
		}
340
	}
341 342
}

343
static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
344
{
345
	if (MMU_MAJ_VER(data->version) < 5)
346
		writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
347
	else
348
		writel(pgd >> PAGE_SHIFT,
349
			     data->sfrbase + REG_V5_PT_BASE_PFN);
350

351
	__sysmmu_tlb_invalidate(data);
352 353
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
static void __sysmmu_enable_clocks(struct sysmmu_drvdata *data)
{
	BUG_ON(clk_prepare_enable(data->clk_master));
	BUG_ON(clk_prepare_enable(data->clk));
	BUG_ON(clk_prepare_enable(data->pclk));
	BUG_ON(clk_prepare_enable(data->aclk));
}

static void __sysmmu_disable_clocks(struct sysmmu_drvdata *data)
{
	clk_disable_unprepare(data->aclk);
	clk_disable_unprepare(data->pclk);
	clk_disable_unprepare(data->clk);
	clk_disable_unprepare(data->clk_master);
}

370 371 372 373
static void __sysmmu_get_version(struct sysmmu_drvdata *data)
{
	u32 ver;

374
	__sysmmu_enable_clocks(data);
375

376
	ver = readl(data->sfrbase + REG_MMU_VERSION);
377 378 379 380 381 382 383 384 385 386

	/* controllers on some SoCs don't report proper version */
	if (ver == 0x80000001u)
		data->version = MAKE_MMU_VER(1, 0);
	else
		data->version = MMU_RAW_VER(ver);

	dev_dbg(data->sysmmu, "hardware version: %d.%d\n",
		MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));

387
	__sysmmu_disable_clocks(data);
388 389
}

390 391 392
static void show_fault_information(struct sysmmu_drvdata *data,
				   const struct sysmmu_fault_info *finfo,
				   sysmmu_iova_t fault_addr)
393
{
394
	sysmmu_pte_t *ent;
395

396 397 398
	dev_err(data->sysmmu, "%s: %s FAULT occurred at %#x\n",
		dev_name(data->master), finfo->name, fault_addr);
	dev_dbg(data->sysmmu, "Page table base: %pa\n", &data->pgtable);
399
	ent = section_entry(phys_to_virt(data->pgtable), fault_addr);
400
	dev_dbg(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
401 402
	if (lv1ent_page(ent)) {
		ent = page_entry(ent, fault_addr);
403
		dev_dbg(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
404 405 406 407 408
	}
}

static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
{
S
Sachin Kamat 已提交
409
	/* SYSMMU is in blocked state when interrupt occurred. */
410
	struct sysmmu_drvdata *data = dev_id;
411 412
	const struct sysmmu_fault_info *finfo;
	unsigned int i, n, itype;
413
	sysmmu_iova_t fault_addr = -1;
414
	unsigned short reg_status, reg_clear;
C
Cho KyongHo 已提交
415
	int ret = -ENOSYS;
416

417
	WARN_ON(!data->active);
418

419 420 421 422 423 424 425 426 427 428 429 430
	if (MMU_MAJ_VER(data->version) < 5) {
		reg_status = REG_INT_STATUS;
		reg_clear = REG_INT_CLEAR;
		finfo = sysmmu_faults;
		n = ARRAY_SIZE(sysmmu_faults);
	} else {
		reg_status = REG_V5_INT_STATUS;
		reg_clear = REG_V5_INT_CLEAR;
		finfo = sysmmu_v5_faults;
		n = ARRAY_SIZE(sysmmu_v5_faults);
	}

431 432
	spin_lock(&data->lock);

433
	clk_enable(data->clk_master);
434

435
	itype = __ffs(readl(data->sfrbase + reg_status));
436 437 438 439 440 441 442
	for (i = 0; i < n; i++, finfo++)
		if (finfo->bit == itype)
			break;
	/* unknown/unsupported fault */
	BUG_ON(i == n);

	/* print debug message */
443
	fault_addr = readl(data->sfrbase + finfo->addr_reg);
444
	show_fault_information(data, finfo, fault_addr);
445

446 447 448
	if (data->domain)
		ret = report_iommu_fault(&data->domain->domain,
					data->master, fault_addr, finfo->type);
449 450
	/* fault is not recovered by fault handler */
	BUG_ON(ret != 0);
451

452
	writel(1 << itype, data->sfrbase + reg_clear);
453

454
	sysmmu_unblock(data);
455

456
	clk_disable(data->clk_master);
457

458
	spin_unlock(&data->lock);
459 460 461 462

	return IRQ_HANDLED;
}

463
static void __sysmmu_disable(struct sysmmu_drvdata *data)
464
{
465 466
	unsigned long flags;

467
	clk_enable(data->clk_master);
468

469
	spin_lock_irqsave(&data->lock, flags);
470 471
	writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
	writel(0, data->sfrbase + REG_MMU_CFG);
472
	data->active = false;
473 474
	spin_unlock_irqrestore(&data->lock, flags);

475
	__sysmmu_disable_clocks(data);
476
}
477

478 479
static void __sysmmu_init_config(struct sysmmu_drvdata *data)
{
480 481 482 483 484 485 486 487
	unsigned int cfg;

	if (data->version <= MAKE_MMU_VER(3, 1))
		cfg = CFG_LRU | CFG_QOS(15);
	else if (data->version <= MAKE_MMU_VER(3, 2))
		cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
	else
		cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
488

489 490
	cfg |= CFG_EAP; /* enable access protection bits check */

491
	writel(cfg, data->sfrbase + REG_MMU_CFG);
492 493
}

494
static void __sysmmu_enable(struct sysmmu_drvdata *data)
495
{
496 497
	unsigned long flags;

498
	__sysmmu_enable_clocks(data);
499

500
	spin_lock_irqsave(&data->lock, flags);
501
	writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
502
	__sysmmu_init_config(data);
503
	__sysmmu_set_ptbase(data, data->pgtable);
504
	writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
505 506
	data->active = true;
	spin_unlock_irqrestore(&data->lock, flags);
C
Cho KyongHo 已提交
507

508 509 510 511 512 513
	/*
	 * SYSMMU driver keeps master's clock enabled only for the short
	 * time, while accessing the registers. For performing address
	 * translation during DMA transaction it relies on the client
	 * driver to enable it.
	 */
514
	clk_disable(data->clk_master);
515
}
516

517
static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
518 519 520 521 522
					    sysmmu_iova_t iova)
{
	unsigned long flags;

	spin_lock_irqsave(&data->lock, flags);
523
	if (data->active && data->version >= MAKE_MMU_VER(3, 3)) {
524
		clk_enable(data->clk_master);
525
		if (sysmmu_block(data)) {
526 527 528 529
			if (data->version >= MAKE_MMU_VER(5, 0))
				__sysmmu_tlb_invalidate(data);
			else
				__sysmmu_tlb_invalidate_entry(data, iova, 1);
530 531
			sysmmu_unblock(data);
		}
532
		clk_disable(data->clk_master);
533
	}
534 535 536
	spin_unlock_irqrestore(&data->lock, flags);
}

537 538
static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
					sysmmu_iova_t iova, size_t size)
539 540 541
{
	unsigned long flags;

542
	spin_lock_irqsave(&data->lock, flags);
543
	if (data->active) {
544
		unsigned int num_inv = 1;
545

546
		clk_enable(data->clk_master);
547

548 549 550
		/*
		 * L2TLB invalidation required
		 * 4KB page: 1 invalidation
S
Sachin Kamat 已提交
551 552
		 * 64KB page: 16 invalidations
		 * 1MB page: 64 invalidations
553 554 555 556 557
		 * because it is set-associative TLB
		 * with 8-way and 64 sets.
		 * 1MB page can be cached in one of all sets.
		 * 64KB page can be one of 16 consecutive sets.
		 */
558
		if (MMU_MAJ_VER(data->version) == 2)
559 560
			num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);

561 562 563
		if (sysmmu_block(data)) {
			__sysmmu_tlb_invalidate_entry(data, iova, num_inv);
			sysmmu_unblock(data);
564
		}
565
		clk_disable(data->clk_master);
566
	}
567
	spin_unlock_irqrestore(&data->lock, flags);
568 569
}

A
Arvind Yadav 已提交
570
static const struct iommu_ops exynos_iommu_ops;
571

572
static int __init exynos_sysmmu_probe(struct platform_device *pdev)
573
{
574
	int irq, ret;
C
Cho KyongHo 已提交
575
	struct device *dev = &pdev->dev;
576
	struct sysmmu_drvdata *data;
C
Cho KyongHo 已提交
577
	struct resource *res;
578

579 580 581
	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;
582

C
Cho KyongHo 已提交
583
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
584 585 586
	data->sfrbase = devm_ioremap_resource(dev, res);
	if (IS_ERR(data->sfrbase))
		return PTR_ERR(data->sfrbase);
587

588 589
	irq = platform_get_irq(pdev, 0);
	if (irq <= 0) {
590
		dev_err(dev, "Unable to find IRQ resource\n");
591
		return irq;
592 593
	}

594
	ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
C
Cho KyongHo 已提交
595 596
				dev_name(dev), data);
	if (ret) {
597 598
		dev_err(dev, "Unabled to register handler of irq %d\n", irq);
		return ret;
599 600
	}

601
	data->clk = devm_clk_get(dev, "sysmmu");
602
	if (PTR_ERR(data->clk) == -ENOENT)
603
		data->clk = NULL;
604 605
	else if (IS_ERR(data->clk))
		return PTR_ERR(data->clk);
606 607

	data->aclk = devm_clk_get(dev, "aclk");
608
	if (PTR_ERR(data->aclk) == -ENOENT)
609
		data->aclk = NULL;
610 611
	else if (IS_ERR(data->aclk))
		return PTR_ERR(data->aclk);
612 613

	data->pclk = devm_clk_get(dev, "pclk");
614
	if (PTR_ERR(data->pclk) == -ENOENT)
615
		data->pclk = NULL;
616 617
	else if (IS_ERR(data->pclk))
		return PTR_ERR(data->pclk);
618 619 620 621

	if (!data->clk && (!data->aclk || !data->pclk)) {
		dev_err(dev, "Failed to get device clock(s)!\n");
		return -ENOSYS;
622 623
	}

624
	data->clk_master = devm_clk_get(dev, "master");
625
	if (PTR_ERR(data->clk_master) == -ENOENT)
626
		data->clk_master = NULL;
627 628
	else if (IS_ERR(data->clk_master))
		return PTR_ERR(data->clk_master);
629

630
	data->sysmmu = dev;
631
	spin_lock_init(&data->lock);
632

633 634 635 636 637 638 639 640 641 642 643 644
	ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
				     dev_name(data->sysmmu));
	if (ret)
		return ret;

	iommu_device_set_ops(&data->iommu, &exynos_iommu_ops);
	iommu_device_set_fwnode(&data->iommu, &dev->of_node->fwnode);

	ret = iommu_device_register(&data->iommu);
	if (ret)
		return ret;

C
Cho KyongHo 已提交
645 646
	platform_set_drvdata(pdev, data);

647
	__sysmmu_get_version(data);
648
	if (PG_ENT_SHIFT < 0) {
649
		if (MMU_MAJ_VER(data->version) < 5) {
650
			PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
651 652 653
			LV1_PROT = SYSMMU_LV1_PROT;
			LV2_PROT = SYSMMU_LV2_PROT;
		} else {
654
			PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
655 656 657
			LV1_PROT = SYSMMU_V5_LV1_PROT;
			LV2_PROT = SYSMMU_V5_LV2_PROT;
		}
658 659
	}

660 661 662 663 664 665 666
	/*
	 * use the first registered sysmmu device for performing
	 * dma mapping operations on iommu page tables (cpu cache flush)
	 */
	if (!dma_dev)
		dma_dev = &pdev->dev;

667
	pm_runtime_enable(dev);
668 669 670 671

	return 0;
}

672
static int __maybe_unused exynos_sysmmu_suspend(struct device *dev)
673 674
{
	struct sysmmu_drvdata *data = dev_get_drvdata(dev);
675
	struct device *master = data->master;
676

677
	if (master) {
678 679 680
		struct exynos_iommu_owner *owner = master->archdata.iommu;

		mutex_lock(&owner->rpm_lock);
681 682 683 684
		if (data->domain) {
			dev_dbg(data->sysmmu, "saving state\n");
			__sysmmu_disable(data);
		}
685
		mutex_unlock(&owner->rpm_lock);
686 687 688 689
	}
	return 0;
}

690
static int __maybe_unused exynos_sysmmu_resume(struct device *dev)
691 692
{
	struct sysmmu_drvdata *data = dev_get_drvdata(dev);
693
	struct device *master = data->master;
694

695
	if (master) {
696 697 698
		struct exynos_iommu_owner *owner = master->archdata.iommu;

		mutex_lock(&owner->rpm_lock);
699 700 701 702
		if (data->domain) {
			dev_dbg(data->sysmmu, "restoring state\n");
			__sysmmu_enable(data);
		}
703
		mutex_unlock(&owner->rpm_lock);
704 705 706 707 708
	}
	return 0;
}

static const struct dev_pm_ops sysmmu_pm_ops = {
709
	SET_RUNTIME_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume, NULL)
710 711
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
712 713
};

714
static const struct of_device_id sysmmu_of_match[] = {
715 716 717 718 719 720 721
	{ .compatible	= "samsung,exynos-sysmmu", },
	{ },
};

static struct platform_driver exynos_sysmmu_driver __refdata = {
	.probe	= exynos_sysmmu_probe,
	.driver	= {
722
		.name		= "exynos-sysmmu",
723
		.of_match_table	= sysmmu_of_match,
724
		.pm		= &sysmmu_pm_ops,
725
		.suppress_bind_attrs = true,
726 727 728
	}
};

729
static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
730
{
731 732
	dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
				DMA_TO_DEVICE);
733
	*ent = cpu_to_le32(val);
734 735
	dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
				   DMA_TO_DEVICE);
736 737
}

738
static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
739
{
740
	struct exynos_iommu_domain *domain;
741
	dma_addr_t handle;
742
	int i;
743

744 745
	/* Check if correct PTE offsets are initialized */
	BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
746

747 748
	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
	if (!domain)
749
		return NULL;
750

751 752 753 754 755 756 757
	if (type == IOMMU_DOMAIN_DMA) {
		if (iommu_get_dma_cookie(&domain->domain) != 0)
			goto err_pgtable;
	} else if (type != IOMMU_DOMAIN_UNMANAGED) {
		goto err_pgtable;
	}

758 759
	domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
	if (!domain->pgtable)
760
		goto err_dma_cookie;
761

762 763
	domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
	if (!domain->lv2entcnt)
764 765
		goto err_counter;

S
Sachin Kamat 已提交
766
	/* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
767 768
	for (i = 0; i < NUM_LV1ENTRIES; i++)
		domain->pgtable[i] = ZERO_LV2LINK;
769

770 771 772 773
	handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
				DMA_TO_DEVICE);
	/* For mapping page table entries we rely on dma == phys */
	BUG_ON(handle != virt_to_phys(domain->pgtable));
774 775
	if (dma_mapping_error(dma_dev, handle))
		goto err_lv2ent;
776

777 778 779
	spin_lock_init(&domain->lock);
	spin_lock_init(&domain->pgtablelock);
	INIT_LIST_HEAD(&domain->clients);
780

781 782 783
	domain->domain.geometry.aperture_start = 0;
	domain->domain.geometry.aperture_end   = ~0UL;
	domain->domain.geometry.force_aperture = true;
784

785
	return &domain->domain;
786

787 788
err_lv2ent:
	free_pages((unsigned long)domain->lv2entcnt, 1);
789
err_counter:
790
	free_pages((unsigned long)domain->pgtable, 2);
791 792 793
err_dma_cookie:
	if (type == IOMMU_DOMAIN_DMA)
		iommu_put_dma_cookie(&domain->domain);
794
err_pgtable:
795
	kfree(domain);
796
	return NULL;
797 798
}

799
static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
800
{
801
	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
802
	struct sysmmu_drvdata *data, *next;
803 804 805
	unsigned long flags;
	int i;

806
	WARN_ON(!list_empty(&domain->clients));
807

808
	spin_lock_irqsave(&domain->lock, flags);
809

810
	list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
811
		spin_lock(&data->lock);
M
Marek Szyprowski 已提交
812
		__sysmmu_disable(data);
813 814
		data->pgtable = 0;
		data->domain = NULL;
815
		list_del_init(&data->domain_node);
816
		spin_unlock(&data->lock);
817 818
	}

819
	spin_unlock_irqrestore(&domain->lock, flags);
820

821 822 823
	if (iommu_domain->type == IOMMU_DOMAIN_DMA)
		iommu_put_dma_cookie(iommu_domain);

824 825 826
	dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
			 DMA_TO_DEVICE);

827
	for (i = 0; i < NUM_LV1ENTRIES; i++)
828 829 830 831 832
		if (lv1ent_page(domain->pgtable + i)) {
			phys_addr_t base = lv2table_base(domain->pgtable + i);

			dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
					 DMA_TO_DEVICE);
833
			kmem_cache_free(lv2table_kmem_cache,
834 835
					phys_to_virt(base));
		}
836

837 838 839
	free_pages((unsigned long)domain->pgtable, 2);
	free_pages((unsigned long)domain->lv2entcnt, 1);
	kfree(domain);
840 841
}

842 843 844 845 846 847 848 849 850 851 852 853
static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
				    struct device *dev)
{
	struct exynos_iommu_owner *owner = dev->archdata.iommu;
	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
	phys_addr_t pagetable = virt_to_phys(domain->pgtable);
	struct sysmmu_drvdata *data, *next;
	unsigned long flags;

	if (!has_sysmmu(dev) || owner->domain != iommu_domain)
		return;

854 855 856 857 858 859
	mutex_lock(&owner->rpm_lock);

	list_for_each_entry(data, &owner->controllers, owner_node) {
		pm_runtime_get_noresume(data->sysmmu);
		if (pm_runtime_active(data->sysmmu))
			__sysmmu_disable(data);
860 861 862
		pm_runtime_put(data->sysmmu);
	}

863 864
	spin_lock_irqsave(&domain->lock, flags);
	list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
865
		spin_lock(&data->lock);
866 867
		data->pgtable = 0;
		data->domain = NULL;
M
Marek Szyprowski 已提交
868
		list_del_init(&data->domain_node);
869
		spin_unlock(&data->lock);
870
	}
871
	owner->domain = NULL;
872 873
	spin_unlock_irqrestore(&domain->lock, flags);

874
	mutex_unlock(&owner->rpm_lock);
875

M
Marek Szyprowski 已提交
876 877
	dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n", __func__,
		&pagetable);
878 879
}

880
static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
881 882
				   struct device *dev)
{
883
	struct exynos_iommu_owner *owner = dev->archdata.iommu;
884
	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
885
	struct sysmmu_drvdata *data;
886
	phys_addr_t pagetable = virt_to_phys(domain->pgtable);
887 888
	unsigned long flags;

889 890
	if (!has_sysmmu(dev))
		return -ENODEV;
891

892 893 894
	if (owner->domain)
		exynos_iommu_detach_device(owner->domain, dev);

895 896
	mutex_lock(&owner->rpm_lock);

897
	spin_lock_irqsave(&domain->lock, flags);
898
	list_for_each_entry(data, &owner->controllers, owner_node) {
899
		spin_lock(&data->lock);
900 901
		data->pgtable = pagetable;
		data->domain = domain;
902 903 904 905 906 907
		list_add_tail(&data->domain_node, &domain->clients);
		spin_unlock(&data->lock);
	}
	owner->domain = iommu_domain;
	spin_unlock_irqrestore(&domain->lock, flags);

908 909 910 911 912 913 914 915 916
	list_for_each_entry(data, &owner->controllers, owner_node) {
		pm_runtime_get_noresume(data->sysmmu);
		if (pm_runtime_active(data->sysmmu))
			__sysmmu_enable(data);
		pm_runtime_put(data->sysmmu);
	}

	mutex_unlock(&owner->rpm_lock);

M
Marek Szyprowski 已提交
917 918
	dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa\n", __func__,
		&pagetable);
C
Cho KyongHo 已提交
919

M
Marek Szyprowski 已提交
920
	return 0;
921 922
}

923
static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
924
		sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
925
{
926
	if (lv1ent_section(sent)) {
927
		WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
928 929 930
		return ERR_PTR(-EADDRINUSE);
	}

931
	if (lv1ent_fault(sent)) {
932
		dma_addr_t handle;
933
		sysmmu_pte_t *pent;
934
		bool need_flush_flpd_cache = lv1ent_zero(sent);
935

936
		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
937
		BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
938
		if (!pent)
939
			return ERR_PTR(-ENOMEM);
940

941
		update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
942
		kmemleak_ignore(pent);
943
		*pgcounter = NUM_LV2ENTRIES;
944 945 946 947 948 949
		handle = dma_map_single(dma_dev, pent, LV2TABLE_SIZE,
					DMA_TO_DEVICE);
		if (dma_mapping_error(dma_dev, handle)) {
			kmem_cache_free(lv2table_kmem_cache, pent);
			return ERR_PTR(-EADDRINUSE);
		}
950 951

		/*
S
Sachin Kamat 已提交
952 953 954 955
		 * If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
		 * FLPD cache may cache the address of zero_l2_table. This
		 * function replaces the zero_l2_table with new L2 page table
		 * to write valid mappings.
956
		 * Accessing the valid area may cause page fault since FLPD
S
Sachin Kamat 已提交
957 958 959
		 * cache may still cache zero_l2_table for the valid area
		 * instead of new L2 page table that has the mapping
		 * information of the valid area.
960 961 962 963 964 965 966 967 968
		 * Thus any replacement of zero_l2_table with other valid L2
		 * page table must involve FLPD cache invalidation for System
		 * MMU v3.3.
		 * FLPD cache invalidation is performed with TLB invalidation
		 * by VPN without blocking. It is safe to invalidate TLB without
		 * blocking because the target address of TLB invalidation is
		 * not currently mapped.
		 */
		if (need_flush_flpd_cache) {
969
			struct sysmmu_drvdata *data;
970

971 972
			spin_lock(&domain->lock);
			list_for_each_entry(data, &domain->clients, domain_node)
973
				sysmmu_tlb_invalidate_flpdcache(data, iova);
974
			spin_unlock(&domain->lock);
975
		}
976 977 978 979 980
	}

	return page_entry(sent, iova);
}

981
static int lv1set_section(struct exynos_iommu_domain *domain,
982
			  sysmmu_pte_t *sent, sysmmu_iova_t iova,
983
			  phys_addr_t paddr, int prot, short *pgcnt)
984
{
985
	if (lv1ent_section(sent)) {
986
		WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
987
			iova);
988
		return -EADDRINUSE;
989
	}
990 991

	if (lv1ent_page(sent)) {
992
		if (*pgcnt != NUM_LV2ENTRIES) {
993
			WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
994
				iova);
995
			return -EADDRINUSE;
996
		}
997

998
		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
999 1000 1001
		*pgcnt = 0;
	}

1002
	update_pte(sent, mk_lv1ent_sect(paddr, prot));
1003

1004
	spin_lock(&domain->lock);
1005
	if (lv1ent_page_zero(sent)) {
1006
		struct sysmmu_drvdata *data;
1007 1008 1009 1010
		/*
		 * Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
		 * entry by speculative prefetch of SLPD which has no mapping.
		 */
1011
		list_for_each_entry(data, &domain->clients, domain_node)
1012
			sysmmu_tlb_invalidate_flpdcache(data, iova);
1013
	}
1014
	spin_unlock(&domain->lock);
1015

1016 1017 1018
	return 0;
}

1019
static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
1020
		       int prot, short *pgcnt)
1021 1022
{
	if (size == SPAGE_SIZE) {
1023
		if (WARN_ON(!lv2ent_fault(pent)))
1024 1025
			return -EADDRINUSE;

1026
		update_pte(pent, mk_lv2ent_spage(paddr, prot));
1027 1028 1029
		*pgcnt -= 1;
	} else { /* size == LPAGE_SIZE */
		int i;
1030
		dma_addr_t pent_base = virt_to_phys(pent);
1031

1032 1033 1034
		dma_sync_single_for_cpu(dma_dev, pent_base,
					sizeof(*pent) * SPAGES_PER_LPAGE,
					DMA_TO_DEVICE);
1035
		for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
1036
			if (WARN_ON(!lv2ent_fault(pent))) {
1037 1038
				if (i > 0)
					memset(pent - i, 0, sizeof(*pent) * i);
1039 1040 1041
				return -EADDRINUSE;
			}

1042
			*pent = mk_lv2ent_lpage(paddr, prot);
1043
		}
1044 1045 1046
		dma_sync_single_for_device(dma_dev, pent_base,
					   sizeof(*pent) * SPAGES_PER_LPAGE,
					   DMA_TO_DEVICE);
1047 1048 1049 1050 1051 1052
		*pgcnt -= SPAGES_PER_LPAGE;
	}

	return 0;
}

1053 1054 1055
/*
 * *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
 *
S
Sachin Kamat 已提交
1056
 * System MMU v3.x has advanced logic to improve address translation
1057
 * performance with caching more page table entries by a page table walk.
S
Sachin Kamat 已提交
1058 1059 1060 1061 1062 1063
 * However, the logic has a bug that while caching faulty page table entries,
 * System MMU reports page fault if the cached fault entry is hit even though
 * the fault entry is updated to a valid entry after the entry is cached.
 * To prevent caching faulty page table entries which may be updated to valid
 * entries later, the virtual memory manager should care about the workaround
 * for the problem. The following describes the workaround.
1064 1065
 *
 * Any two consecutive I/O virtual address regions must have a hole of 128KiB
S
Sachin Kamat 已提交
1066
 * at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
1067
 *
S
Sachin Kamat 已提交
1068
 * Precisely, any start address of I/O virtual region must be aligned with
1069 1070 1071 1072 1073
 * the following sizes for System MMU v3.1 and v3.2.
 * System MMU v3.1: 128KiB
 * System MMU v3.2: 256KiB
 *
 * Because System MMU v3.3 caches page table entries more aggressively, it needs
S
Sachin Kamat 已提交
1074 1075 1076
 * more workarounds.
 * - Any two consecutive I/O virtual regions must have a hole of size larger
 *   than or equal to 128KiB.
1077 1078
 * - Start address of an I/O virtual region must be aligned by 128KiB.
 */
1079 1080 1081
static int exynos_iommu_map(struct iommu_domain *iommu_domain,
			    unsigned long l_iova, phys_addr_t paddr, size_t size,
			    int prot)
1082
{
1083
	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1084 1085
	sysmmu_pte_t *entry;
	sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
1086 1087 1088
	unsigned long flags;
	int ret = -ENOMEM;

1089
	BUG_ON(domain->pgtable == NULL);
1090
	prot &= SYSMMU_SUPPORTED_PROT_BITS;
1091

1092
	spin_lock_irqsave(&domain->pgtablelock, flags);
1093

1094
	entry = section_entry(domain->pgtable, iova);
1095 1096

	if (size == SECT_SIZE) {
1097
		ret = lv1set_section(domain, entry, iova, paddr, prot,
1098
				     &domain->lv2entcnt[lv1ent_offset(iova)]);
1099
	} else {
1100
		sysmmu_pte_t *pent;
1101

1102 1103
		pent = alloc_lv2entry(domain, entry, iova,
				      &domain->lv2entcnt[lv1ent_offset(iova)]);
1104

1105 1106
		if (IS_ERR(pent))
			ret = PTR_ERR(pent);
1107
		else
1108
			ret = lv2set_page(pent, paddr, size, prot,
1109
				       &domain->lv2entcnt[lv1ent_offset(iova)]);
1110 1111
	}

1112
	if (ret)
1113 1114
		pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
			__func__, ret, size, iova);
1115

1116
	spin_unlock_irqrestore(&domain->pgtablelock, flags);
1117 1118 1119 1120

	return ret;
}

1121 1122
static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
					      sysmmu_iova_t iova, size_t size)
1123
{
1124
	struct sysmmu_drvdata *data;
1125 1126
	unsigned long flags;

1127
	spin_lock_irqsave(&domain->lock, flags);
1128

1129
	list_for_each_entry(data, &domain->clients, domain_node)
1130
		sysmmu_tlb_invalidate_entry(data, iova, size);
1131

1132
	spin_unlock_irqrestore(&domain->lock, flags);
1133 1134
}

1135 1136
static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
				 unsigned long l_iova, size_t size)
1137
{
1138
	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1139 1140
	sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
	sysmmu_pte_t *ent;
1141
	size_t err_pgsize;
1142
	unsigned long flags;
1143

1144
	BUG_ON(domain->pgtable == NULL);
1145

1146
	spin_lock_irqsave(&domain->pgtablelock, flags);
1147

1148
	ent = section_entry(domain->pgtable, iova);
1149 1150

	if (lv1ent_section(ent)) {
1151
		if (WARN_ON(size < SECT_SIZE)) {
1152 1153 1154
			err_pgsize = SECT_SIZE;
			goto err;
		}
1155

S
Sachin Kamat 已提交
1156
		/* workaround for h/w bug in System MMU v3.3 */
1157
		update_pte(ent, ZERO_LV2LINK);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
		size = SECT_SIZE;
		goto done;
	}

	if (unlikely(lv1ent_fault(ent))) {
		if (size > SECT_SIZE)
			size = SECT_SIZE;
		goto done;
	}

	/* lv1ent_page(sent) == true here */

	ent = page_entry(ent, iova);

	if (unlikely(lv2ent_fault(ent))) {
		size = SPAGE_SIZE;
		goto done;
	}

	if (lv2ent_small(ent)) {
1178
		update_pte(ent, 0);
1179
		size = SPAGE_SIZE;
1180
		domain->lv2entcnt[lv1ent_offset(iova)] += 1;
1181 1182 1183 1184
		goto done;
	}

	/* lv1ent_large(ent) == true here */
1185
	if (WARN_ON(size < LPAGE_SIZE)) {
1186 1187 1188
		err_pgsize = LPAGE_SIZE;
		goto err;
	}
1189

1190 1191 1192
	dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
				sizeof(*ent) * SPAGES_PER_LPAGE,
				DMA_TO_DEVICE);
1193
	memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
1194 1195 1196
	dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
				   sizeof(*ent) * SPAGES_PER_LPAGE,
				   DMA_TO_DEVICE);
1197
	size = LPAGE_SIZE;
1198
	domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
1199
done:
1200
	spin_unlock_irqrestore(&domain->pgtablelock, flags);
1201

1202
	exynos_iommu_tlb_invalidate_entry(domain, iova, size);
1203 1204

	return size;
1205
err:
1206
	spin_unlock_irqrestore(&domain->pgtablelock, flags);
1207

1208 1209
	pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
		__func__, size, iova, err_pgsize);
1210 1211

	return 0;
1212 1213
}

1214
static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
1215
					  dma_addr_t iova)
1216
{
1217
	struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
1218
	sysmmu_pte_t *entry;
1219 1220 1221
	unsigned long flags;
	phys_addr_t phys = 0;

1222
	spin_lock_irqsave(&domain->pgtablelock, flags);
1223

1224
	entry = section_entry(domain->pgtable, iova);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

	if (lv1ent_section(entry)) {
		phys = section_phys(entry) + section_offs(iova);
	} else if (lv1ent_page(entry)) {
		entry = page_entry(entry, iova);

		if (lv2ent_large(entry))
			phys = lpage_phys(entry) + lpage_offs(iova);
		else if (lv2ent_small(entry))
			phys = spage_phys(entry) + spage_offs(iova);
	}

1237
	spin_unlock_irqrestore(&domain->pgtablelock, flags);
1238 1239 1240 1241

	return phys;
}

1242 1243
static int exynos_iommu_add_device(struct device *dev)
{
1244 1245
	struct exynos_iommu_owner *owner = dev->archdata.iommu;
	struct sysmmu_drvdata *data;
1246 1247
	struct iommu_group *group;

1248 1249 1250
	if (!has_sysmmu(dev))
		return -ENODEV;

1251
	group = iommu_group_get_for_dev(dev);
1252

1253 1254
	if (IS_ERR(group))
		return PTR_ERR(group);
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264
	list_for_each_entry(data, &owner->controllers, owner_node) {
		/*
		 * SYSMMU will be runtime activated via device link
		 * (dependency) to its master device, so there are no
		 * direct calls to pm_runtime_get/put in this driver.
		 */
		data->link = device_link_add(dev, data->sysmmu,
					     DL_FLAG_PM_RUNTIME);
	}
1265 1266
	iommu_group_put(group);

1267
	return 0;
1268 1269 1270 1271
}

static void exynos_iommu_remove_device(struct device *dev)
{
1272
	struct exynos_iommu_owner *owner = dev->archdata.iommu;
1273
	struct sysmmu_drvdata *data;
1274

1275 1276 1277
	if (!has_sysmmu(dev))
		return;

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	if (owner->domain) {
		struct iommu_group *group = iommu_group_get(dev);

		if (group) {
			WARN_ON(owner->domain !=
				iommu_group_default_domain(group));
			exynos_iommu_detach_device(owner->domain, dev);
			iommu_group_put(group);
		}
	}
1288
	iommu_group_remove_device(dev);
1289 1290 1291

	list_for_each_entry(data, &owner->controllers, owner_node)
		device_link_del(data->link);
1292 1293
}

1294 1295 1296 1297 1298
static int exynos_iommu_of_xlate(struct device *dev,
				 struct of_phandle_args *spec)
{
	struct exynos_iommu_owner *owner = dev->archdata.iommu;
	struct platform_device *sysmmu = of_find_device_by_node(spec->np);
1299
	struct sysmmu_drvdata *data, *entry;
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

	if (!sysmmu)
		return -ENODEV;

	data = platform_get_drvdata(sysmmu);
	if (!data)
		return -ENODEV;

	if (!owner) {
		owner = kzalloc(sizeof(*owner), GFP_KERNEL);
		if (!owner)
			return -ENOMEM;

		INIT_LIST_HEAD(&owner->controllers);
1314
		mutex_init(&owner->rpm_lock);
1315 1316 1317
		dev->archdata.iommu = owner;
	}

1318 1319 1320 1321
	list_for_each_entry(entry, &owner->controllers, owner_node)
		if (entry == data)
			return 0;

1322
	list_add_tail(&data->owner_node, &owner->controllers);
1323
	data->master = dev;
1324

1325 1326 1327
	return 0;
}

A
Arvind Yadav 已提交
1328
static const struct iommu_ops exynos_iommu_ops = {
1329 1330
	.domain_alloc = exynos_iommu_domain_alloc,
	.domain_free = exynos_iommu_domain_free,
1331 1332 1333 1334
	.attach_dev = exynos_iommu_attach_device,
	.detach_dev = exynos_iommu_detach_device,
	.map = exynos_iommu_map,
	.unmap = exynos_iommu_unmap,
O
Olav Haugan 已提交
1335
	.map_sg = default_iommu_map_sg,
1336
	.iova_to_phys = exynos_iommu_iova_to_phys,
1337
	.device_group = generic_device_group,
1338 1339
	.add_device = exynos_iommu_add_device,
	.remove_device = exynos_iommu_remove_device,
1340
	.pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
1341
	.of_xlate = exynos_iommu_of_xlate,
1342 1343 1344 1345
};

static int __init exynos_iommu_init(void)
{
1346
	struct device_node *np;
1347 1348
	int ret;

1349 1350 1351 1352 1353 1354
	np = of_find_matching_node(NULL, sysmmu_of_match);
	if (!np)
		return 0;

	of_node_put(np);

1355 1356 1357 1358 1359 1360 1361
	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	if (!lv2table_kmem_cache) {
		pr_err("%s: Failed to create kmem cache\n", __func__);
		return -ENOMEM;
	}

1362
	ret = platform_driver_register(&exynos_sysmmu_driver);
1363 1364 1365 1366
	if (ret) {
		pr_err("%s: Failed to register driver\n", __func__);
		goto err_reg_driver;
	}
1367

1368 1369 1370 1371 1372 1373 1374 1375
	zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
	if (zero_lv2_table == NULL) {
		pr_err("%s: Failed to allocate zero level2 page table\n",
			__func__);
		ret = -ENOMEM;
		goto err_zero_lv2;
	}

1376 1377 1378 1379 1380 1381
	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
		pr_err("%s: Failed to register exynos-iommu driver.\n",
								__func__);
		goto err_set_iommu;
	}
1382

1383 1384
	return 0;
err_set_iommu:
1385 1386
	kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
err_zero_lv2:
1387 1388 1389
	platform_driver_unregister(&exynos_sysmmu_driver);
err_reg_driver:
	kmem_cache_destroy(lv2table_kmem_cache);
1390 1391
	return ret;
}
1392
core_initcall(exynos_iommu_init);
1393

R
Robin Murphy 已提交
1394
IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu");