omap-iommu.c 27.8 KB
Newer Older
1 2 3
/*
 * omap iommu: tlb and pagetable primitives
 *
4
 * Copyright (C) 2008-2010 Nokia Corporation
5 6 7 8 9 10 11 12 13 14 15
 *
 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
 *		Paul Mundt and Toshihiro Kobayashi
 *
 * 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.
 */

#include <linux/err.h>
#include <linux/module.h>
16
#include <linux/slab.h>
17 18 19
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
20
#include <linux/iommu.h>
21
#include <linux/omap-iommu.h>
22 23
#include <linux/mutex.h>
#include <linux/spinlock.h>
24
#include <linux/io.h>
25
#include <linux/pm_runtime.h>
26 27 28

#include <asm/cacheflush.h>

29
#include <linux/platform_data/iommu-omap.h>
30

31
#include "omap-iopgtable.h"
32
#include "omap-iommu.h"
33

34 35 36 37 38
#define for_each_iotlb_cr(obj, n, __i, cr)				\
	for (__i = 0;							\
	     (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true);	\
	     __i++)

39 40 41
/* bitmap of the page sizes currently supported */
#define OMAP_IOMMU_PGSIZES	(SZ_4K | SZ_64K | SZ_1M | SZ_16M)

42 43 44 45 46
/**
 * struct omap_iommu_domain - omap iommu domain
 * @pgtable:	the page table
 * @iommu_dev:	an omap iommu device attached to this domain. only a single
 *		iommu device can be attached for now.
47
 * @dev:	Device using this domain.
48 49 50 51
 * @lock:	domain lock, should be taken when attaching/detaching
 */
struct omap_iommu_domain {
	u32 *pgtable;
52
	struct omap_iommu *iommu_dev;
53
	struct device *dev;
54 55 56
	spinlock_t lock;
};

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#define MMU_LOCK_BASE_SHIFT	10
#define MMU_LOCK_BASE_MASK	(0x1f << MMU_LOCK_BASE_SHIFT)
#define MMU_LOCK_BASE(x)	\
	((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)

#define MMU_LOCK_VICT_SHIFT	4
#define MMU_LOCK_VICT_MASK	(0x1f << MMU_LOCK_VICT_SHIFT)
#define MMU_LOCK_VICT(x)	\
	((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)

struct iotlb_lock {
	short base;
	short vict;
};

72 73 74 75 76 77 78
/* accommodate the difference between omap1 and omap2/3 */
static const struct iommu_functions *arch_iommu;

static struct platform_driver omap_iommu_driver;
static struct kmem_cache *iopte_cachep;

/**
79
 * omap_install_iommu_arch - Install archtecure specific iommu functions
80 81 82 83 84
 * @ops:	a pointer to architecture specific iommu functions
 *
 * There are several kind of iommu algorithm(tlb, pagetable) among
 * omap series. This interface installs such an iommu algorighm.
 **/
85
int omap_install_iommu_arch(const struct iommu_functions *ops)
86 87 88 89 90 91 92
{
	if (arch_iommu)
		return -EBUSY;

	arch_iommu = ops;
	return 0;
}
93
EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
94 95

/**
96
 * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
97 98 99 100
 * @ops:	a pointer to architecture specific iommu functions
 *
 * This interface uninstalls the iommu algorighm installed previously.
 **/
101
void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
102 103 104 105 106 107
{
	if (arch_iommu != ops)
		pr_err("%s: not your arch\n", __func__);

	arch_iommu = NULL;
}
108
EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
109 110

/**
111
 * omap_iommu_save_ctx - Save registers for pm off-mode support
112
 * @dev:	client device
113
 **/
114
void omap_iommu_save_ctx(struct device *dev)
115
{
116 117
	struct omap_iommu *obj = dev_to_omap_iommu(dev);

118 119
	arch_iommu->save_ctx(obj);
}
120
EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
121 122

/**
123
 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
124
 * @dev:	client device
125
 **/
126
void omap_iommu_restore_ctx(struct device *dev)
127
{
128 129
	struct omap_iommu *obj = dev_to_omap_iommu(dev);

130 131
	arch_iommu->restore_ctx(obj);
}
132
EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
133 134

/**
135
 * omap_iommu_arch_version - Return running iommu arch version
136
 **/
137
u32 omap_iommu_arch_version(void)
138 139 140
{
	return arch_iommu->version;
}
141
EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
142

143
static int iommu_enable(struct omap_iommu *obj)
144 145
{
	int err;
146 147
	struct platform_device *pdev = to_platform_device(obj->dev);
	struct iommu_platform_data *pdata = pdev->dev.platform_data;
148

149
	if (!pdata)
150 151
		return -EINVAL;

152 153 154
	if (!arch_iommu)
		return -ENODEV;

155 156 157 158 159 160 161 162
	if (pdata->deassert_reset) {
		err = pdata->deassert_reset(pdev, pdata->reset_name);
		if (err) {
			dev_err(obj->dev, "deassert_reset failed: %d\n", err);
			return err;
		}
	}

163
	pm_runtime_get_sync(obj->dev);
164 165 166 167 168 169

	err = arch_iommu->enable(obj);

	return err;
}

170
static void iommu_disable(struct omap_iommu *obj)
171
{
172 173 174
	struct platform_device *pdev = to_platform_device(obj->dev);
	struct iommu_platform_data *pdata = pdev->dev.platform_data;

175
	if (!pdata)
176 177 178 179
		return;

	arch_iommu->disable(obj);

180
	pm_runtime_put_sync(obj->dev);
181 182 183

	if (pdata->assert_reset)
		pdata->assert_reset(pdev, pdata->reset_name);
184 185 186 187 188
}

/*
 *	TLB operations
 */
189
void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
190 191 192 193 194
{
	BUG_ON(!cr || !e);

	arch_iommu->cr_to_e(cr, e);
}
195
EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
196 197 198 199 200 201 202 203 204

static inline int iotlb_cr_valid(struct cr_regs *cr)
{
	if (!cr)
		return -EINVAL;

	return arch_iommu->cr_valid(cr);
}

205
static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
206 207 208 209 210 211 212 213
					     struct iotlb_entry *e)
{
	if (!e)
		return NULL;

	return arch_iommu->alloc_cr(obj, e);
}

214
static u32 iotlb_cr_to_virt(struct cr_regs *cr)
215 216 217 218 219 220 221 222 223
{
	return arch_iommu->cr_to_virt(cr);
}

static u32 get_iopte_attr(struct iotlb_entry *e)
{
	return arch_iommu->get_pte_attr(e);
}

224
static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
225 226 227 228
{
	return arch_iommu->fault_isr(obj, da);
}

229
static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
230 231 232 233 234 235 236 237 238 239
{
	u32 val;

	val = iommu_read_reg(obj, MMU_LOCK);

	l->base = MMU_LOCK_BASE(val);
	l->vict = MMU_LOCK_VICT(val);

}

240
static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
241 242 243 244 245 246 247 248 249
{
	u32 val;

	val = (l->base << MMU_LOCK_BASE_SHIFT);
	val |= (l->vict << MMU_LOCK_VICT_SHIFT);

	iommu_write_reg(obj, val, MMU_LOCK);
}

250
static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
251 252 253 254
{
	arch_iommu->tlb_read_cr(obj, cr);
}

255
static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
256 257 258 259 260 261 262 263 264 265 266 267 268
{
	arch_iommu->tlb_load_cr(obj, cr);

	iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
	iommu_write_reg(obj, 1, MMU_LD_TLB);
}

/**
 * iotlb_dump_cr - Dump an iommu tlb entry into buf
 * @obj:	target iommu
 * @cr:		contents of cam and ram register
 * @buf:	output buffer
 **/
269
static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
270 271 272 273 274 275 276
				    char *buf)
{
	BUG_ON(!cr || !buf);

	return arch_iommu->dump_cr(obj, cr, buf);
}

277
/* only used in iotlb iteration for-loop */
278
static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
279 280 281 282 283 284 285 286 287 288 289 290
{
	struct cr_regs cr;
	struct iotlb_lock l;

	iotlb_lock_get(obj, &l);
	l.vict = n;
	iotlb_lock_set(obj, &l);
	iotlb_read_cr(obj, &cr);

	return cr;
}

291 292 293 294 295
/**
 * load_iotlb_entry - Set an iommu tlb entry
 * @obj:	target iommu
 * @e:		an iommu tlb entry info
 **/
296
#ifdef PREFETCH_IOTLB
297
static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
298 299 300 301 302 303 304 305
{
	int err = 0;
	struct iotlb_lock l;
	struct cr_regs *cr;

	if (!obj || !obj->nr_tlb_entries || !e)
		return -EINVAL;

306
	pm_runtime_get_sync(obj->dev);
307

308 309 310
	iotlb_lock_get(obj, &l);
	if (l.base == obj->nr_tlb_entries) {
		dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
311 312 313
		err = -EBUSY;
		goto out;
	}
314
	if (!e->prsvd) {
315 316
		int i;
		struct cr_regs tmp;
317

318
		for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
319 320
			if (!iotlb_cr_valid(&tmp))
				break;
321

322 323 324 325 326
		if (i == obj->nr_tlb_entries) {
			dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
			err = -EBUSY;
			goto out;
		}
327 328

		iotlb_lock_get(obj, &l);
329 330 331 332
	} else {
		l.vict = l.base;
		iotlb_lock_set(obj, &l);
	}
333 334 335

	cr = iotlb_alloc_cr(obj, e);
	if (IS_ERR(cr)) {
336
		pm_runtime_put_sync(obj->dev);
337 338 339 340 341 342
		return PTR_ERR(cr);
	}

	iotlb_load_cr(obj, cr);
	kfree(cr);

343 344
	if (e->prsvd)
		l.base++;
345 346
	/* increment victim for next tlb load */
	if (++l.vict == obj->nr_tlb_entries)
347
		l.vict = l.base;
348 349
	iotlb_lock_set(obj, &l);
out:
350
	pm_runtime_put_sync(obj->dev);
351 352 353
	return err;
}

354 355
#else /* !PREFETCH_IOTLB */

356
static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
357 358 359 360 361 362
{
	return 0;
}

#endif /* !PREFETCH_IOTLB */

363
static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
364 365 366
{
	return load_iotlb_entry(obj, e);
}
367 368 369 370 371 372 373 374

/**
 * flush_iotlb_page - Clear an iommu tlb entry
 * @obj:	target iommu
 * @da:		iommu device virtual address
 *
 * Clear an iommu tlb entry which includes 'da' address.
 **/
375
static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
376 377
{
	int i;
378
	struct cr_regs cr;
379

380
	pm_runtime_get_sync(obj->dev);
381

382
	for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
383 384 385 386 387 388 389 390 391 392 393 394
		u32 start;
		size_t bytes;

		if (!iotlb_cr_valid(&cr))
			continue;

		start = iotlb_cr_to_virt(&cr);
		bytes = iopgsz_to_bytes(cr.cam & 3);

		if ((start <= da) && (da < start + bytes)) {
			dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
				__func__, start, da, bytes);
395
			iotlb_load_cr(obj, &cr);
396 397 398
			iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
		}
	}
399
	pm_runtime_put_sync(obj->dev);
400 401 402 403 404 405 406 407 408

	if (i == obj->nr_tlb_entries)
		dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
}

/**
 * flush_iotlb_all - Clear all iommu tlb entries
 * @obj:	target iommu
 **/
409
static void flush_iotlb_all(struct omap_iommu *obj)
410 411 412
{
	struct iotlb_lock l;

413
	pm_runtime_get_sync(obj->dev);
414 415 416 417 418 419 420

	l.base = 0;
	l.vict = 0;
	iotlb_lock_set(obj, &l);

	iommu_write_reg(obj, 1, MMU_GFLUSH);

421
	pm_runtime_put_sync(obj->dev);
422
}
423

424
#if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
425

426
ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
427 428 429 430
{
	if (!obj || !buf)
		return -EINVAL;

431
	pm_runtime_get_sync(obj->dev);
432

433
	bytes = arch_iommu->dump_ctx(obj, buf, bytes);
434

435
	pm_runtime_put_sync(obj->dev);
436 437 438

	return bytes;
}
439
EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
440

441 442
static int
__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
443 444
{
	int i;
445 446
	struct iotlb_lock saved;
	struct cr_regs tmp;
447 448
	struct cr_regs *p = crs;

449
	pm_runtime_get_sync(obj->dev);
450 451
	iotlb_lock_get(obj, &saved);

452
	for_each_iotlb_cr(obj, num, i, tmp) {
453 454 455 456
		if (!iotlb_cr_valid(&tmp))
			continue;
		*p++ = tmp;
	}
457

458
	iotlb_lock_set(obj, &saved);
459
	pm_runtime_put_sync(obj->dev);
460 461 462 463 464

	return  p - crs;
}

/**
465
 * omap_dump_tlb_entries - dump cr arrays to given buffer
466 467 468
 * @obj:	target iommu
 * @buf:	output buffer
 **/
469
size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
470
{
471
	int i, num;
472 473 474
	struct cr_regs *cr;
	char *p = buf;

475 476 477 478
	num = bytes / sizeof(*cr);
	num = min(obj->nr_tlb_entries, num);

	cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
479 480 481
	if (!cr)
		return 0;

482 483
	num = __dump_tlb_entries(obj, cr, num);
	for (i = 0; i < num; i++)
484 485 486 487 488
		p += iotlb_dump_cr(obj, cr + i, p);
	kfree(cr);

	return p - buf;
}
489
EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
490

491
int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
492 493 494 495
{
	return driver_for_each_device(&omap_iommu_driver.driver,
				      NULL, data, fn);
}
496
EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

#endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */

/*
 *	H/W pagetable operations
 */
static void flush_iopgd_range(u32 *first, u32 *last)
{
	/* FIXME: L2 cache should be taken care of if it exists */
	do {
		asm("mcr	p15, 0, %0, c7, c10, 1 @ flush_pgd"
		    : : "r" (first));
		first += L1_CACHE_BYTES / sizeof(*first);
	} while (first <= last);
}

static void flush_iopte_range(u32 *first, u32 *last)
{
	/* FIXME: L2 cache should be taken care of if it exists */
	do {
		asm("mcr	p15, 0, %0, c7, c10, 1 @ flush_pte"
		    : : "r" (first));
		first += L1_CACHE_BYTES / sizeof(*first);
	} while (first <= last);
}

static void iopte_free(u32 *iopte)
{
	/* Note: freed iopte's must be clean ready for re-use */
	kmem_cache_free(iopte_cachep, iopte);
}

529
static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
{
	u32 *iopte;

	/* a table has already existed */
	if (*iopgd)
		goto pte_ready;

	/*
	 * do the allocation outside the page table lock
	 */
	spin_unlock(&obj->page_table_lock);
	iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
	spin_lock(&obj->page_table_lock);

	if (!*iopgd) {
		if (!iopte)
			return ERR_PTR(-ENOMEM);

		*iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
		flush_iopgd_range(iopgd, iopgd);

		dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
	} else {
		/* We raced, free the reduniovant table */
		iopte_free(iopte);
	}

pte_ready:
	iopte = iopte_offset(iopgd, da);

	dev_vdbg(obj->dev,
		 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
		 __func__, da, iopgd, *iopgd, iopte, *iopte);

	return iopte;
}

567
static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
568 569 570
{
	u32 *iopgd = iopgd_offset(obj, da);

571 572 573 574 575 576
	if ((da | pa) & ~IOSECTION_MASK) {
		dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
			__func__, da, pa, IOSECTION_SIZE);
		return -EINVAL;
	}

577 578 579 580 581
	*iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
	flush_iopgd_range(iopgd, iopgd);
	return 0;
}

582
static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
583 584 585 586
{
	u32 *iopgd = iopgd_offset(obj, da);
	int i;

587 588 589 590 591 592
	if ((da | pa) & ~IOSUPER_MASK) {
		dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
			__func__, da, pa, IOSUPER_SIZE);
		return -EINVAL;
	}

593 594 595 596 597 598
	for (i = 0; i < 16; i++)
		*(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
	flush_iopgd_range(iopgd, iopgd + 15);
	return 0;
}

599
static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
{
	u32 *iopgd = iopgd_offset(obj, da);
	u32 *iopte = iopte_alloc(obj, iopgd, da);

	if (IS_ERR(iopte))
		return PTR_ERR(iopte);

	*iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
	flush_iopte_range(iopte, iopte);

	dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
		 __func__, da, pa, iopte, *iopte);

	return 0;
}

616
static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
617 618 619 620 621
{
	u32 *iopgd = iopgd_offset(obj, da);
	u32 *iopte = iopte_alloc(obj, iopgd, da);
	int i;

622 623 624 625 626 627
	if ((da | pa) & ~IOLARGE_MASK) {
		dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
			__func__, da, pa, IOLARGE_SIZE);
		return -EINVAL;
	}

628 629 630 631 632 633 634 635 636
	if (IS_ERR(iopte))
		return PTR_ERR(iopte);

	for (i = 0; i < 16; i++)
		*(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
	flush_iopte_range(iopte, iopte + 15);
	return 0;
}

637 638
static int
iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
639
{
640
	int (*fn)(struct omap_iommu *, u32, u32, u32);
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
	u32 prot;
	int err;

	if (!obj || !e)
		return -EINVAL;

	switch (e->pgsz) {
	case MMU_CAM_PGSZ_16M:
		fn = iopgd_alloc_super;
		break;
	case MMU_CAM_PGSZ_1M:
		fn = iopgd_alloc_section;
		break;
	case MMU_CAM_PGSZ_64K:
		fn = iopte_alloc_large;
		break;
	case MMU_CAM_PGSZ_4K:
		fn = iopte_alloc_page;
		break;
	default:
		fn = NULL;
		BUG();
		break;
	}

	prot = get_iopte_attr(e);

	spin_lock(&obj->page_table_lock);
	err = fn(obj, e->da, e->pa, prot);
	spin_unlock(&obj->page_table_lock);

	return err;
}

/**
676
 * omap_iopgtable_store_entry - Make an iommu pte entry
677 678 679
 * @obj:	target iommu
 * @e:		an iommu tlb entry info
 **/
680
int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
681 682 683 684 685 686
{
	int err;

	flush_iotlb_page(obj, e->da);
	err = iopgtable_store_entry_core(obj, e);
	if (!err)
687
		prefetch_iotlb_entry(obj, e);
688 689
	return err;
}
690
EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
691 692 693 694 695 696 697 698

/**
 * iopgtable_lookup_entry - Lookup an iommu pte entry
 * @obj:	target iommu
 * @da:		iommu device virtual address
 * @ppgd:	iommu pgd entry pointer to be returned
 * @ppte:	iommu pte entry pointer to be returned
 **/
699 700
static void
iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
701 702 703 704 705 706 707
{
	u32 *iopgd, *iopte = NULL;

	iopgd = iopgd_offset(obj, da);
	if (!*iopgd)
		goto out;

708
	if (iopgd_is_table(*iopgd))
709 710 711 712 713 714
		iopte = iopte_offset(iopgd, da);
out:
	*ppgd = iopgd;
	*ppte = iopte;
}

715
static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
716 717 718 719 720 721 722 723
{
	size_t bytes;
	u32 *iopgd = iopgd_offset(obj, da);
	int nent = 1;

	if (!*iopgd)
		return 0;

724
	if (iopgd_is_table(*iopgd)) {
725 726 727 728 729 730 731
		int i;
		u32 *iopte = iopte_offset(iopgd, da);

		bytes = IOPTE_SIZE;
		if (*iopte & IOPTE_LARGE) {
			nent *= 16;
			/* rewind to the 1st entry */
732
			iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
		}
		bytes *= nent;
		memset(iopte, 0, nent * sizeof(*iopte));
		flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));

		/*
		 * do table walk to check if this table is necessary or not
		 */
		iopte = iopte_offset(iopgd, 0);
		for (i = 0; i < PTRS_PER_IOPTE; i++)
			if (iopte[i])
				goto out;

		iopte_free(iopte);
		nent = 1; /* for the next L1 entry */
	} else {
		bytes = IOPGD_SIZE;
750
		if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
751 752
			nent *= 16;
			/* rewind to the 1st entry */
753
			iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
754 755 756 757 758 759 760 761 762 763 764 765 766 767
		}
		bytes *= nent;
	}
	memset(iopgd, 0, nent * sizeof(*iopgd));
	flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
out:
	return bytes;
}

/**
 * iopgtable_clear_entry - Remove an iommu pte entry
 * @obj:	target iommu
 * @da:		iommu device virtual address
 **/
768
static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
769 770 771 772 773 774 775 776 777 778 779 780 781
{
	size_t bytes;

	spin_lock(&obj->page_table_lock);

	bytes = iopgtable_clear_entry_core(obj, da);
	flush_iotlb_page(obj, da);

	spin_unlock(&obj->page_table_lock);

	return bytes;
}

782
static void iopgtable_clear_entry_all(struct omap_iommu *obj)
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
{
	int i;

	spin_lock(&obj->page_table_lock);

	for (i = 0; i < PTRS_PER_IOPGD; i++) {
		u32 da;
		u32 *iopgd;

		da = i << IOPGD_SHIFT;
		iopgd = iopgd_offset(obj, da);

		if (!*iopgd)
			continue;

798
		if (iopgd_is_table(*iopgd))
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
			iopte_free(iopte_offset(iopgd, 0));

		*iopgd = 0;
		flush_iopgd_range(iopgd, iopgd);
	}

	flush_iotlb_all(obj);

	spin_unlock(&obj->page_table_lock);
}

/*
 *	Device IOMMU generic operations
 */
static irqreturn_t iommu_fault_handler(int irq, void *data)
{
815
	u32 da, errs;
816
	u32 *iopgd, *iopte;
817
	struct omap_iommu *obj = data;
818
	struct iommu_domain *domain = obj->domain;
819 820 821 822

	if (!obj->refcount)
		return IRQ_NONE;

823
	errs = iommu_report_fault(obj, &da);
824 825
	if (errs == 0)
		return IRQ_HANDLED;
826 827

	/* Fault callback or TLB/PTE Dynamic loading */
828
	if (!report_iommu_fault(domain, obj->dev, da, 0))
829 830
		return IRQ_HANDLED;

831 832
	iommu_disable(obj);

833 834
	iopgd = iopgd_offset(obj, da);

835
	if (!iopgd_is_table(*iopgd)) {
836 837
		dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
				obj->name, errs, da, iopgd, *iopgd);
838 839 840 841 842
		return IRQ_NONE;
	}

	iopte = iopte_offset(iopgd, da);

843 844
	dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x pte:0x%p *pte:0x%08x\n",
			obj->name, errs, da, iopgd, *iopgd, iopte, *iopte);
845 846 847 848 849 850

	return IRQ_NONE;
}

static int device_match_by_alias(struct device *dev, void *data)
{
851
	struct omap_iommu *obj = to_iommu(dev);
852 853 854 855 856 857 858 859
	const char *name = data;

	pr_debug("%s: %s %s\n", __func__, obj->name, name);

	return strcmp(obj->name, name) == 0;
}

/**
860
 * omap_iommu_attach() - attach iommu device to an iommu domain
861
 * @name:	name of target omap iommu device
862
 * @iopgd:	page table
863
 **/
864
static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
865
{
866
	int err;
867 868 869 870 871 872 873
	struct device *dev;
	struct omap_iommu *obj;

	dev = driver_find_device(&omap_iommu_driver.driver, NULL,
				(void *)name,
				device_match_by_alias);
	if (!dev)
874
		return ERR_PTR(-ENODEV);
875 876

	obj = to_iommu(dev);
877

878
	spin_lock(&obj->iommu_lock);
879

880 881 882 883 884
	/* an iommu device can only be attached once */
	if (++obj->refcount > 1) {
		dev_err(dev, "%s: already attached!\n", obj->name);
		err = -EBUSY;
		goto err_enable;
885 886
	}

887 888 889 890 891 892
	obj->iopgd = iopgd;
	err = iommu_enable(obj);
	if (err)
		goto err_enable;
	flush_iotlb_all(obj);

893 894
	if (!try_module_get(obj->owner)) {
		err = -ENODEV;
895
		goto err_module;
896
	}
897

898
	spin_unlock(&obj->iommu_lock);
899 900 901 902 903 904 905 906 907

	dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
	return obj;

err_module:
	if (obj->refcount == 1)
		iommu_disable(obj);
err_enable:
	obj->refcount--;
908
	spin_unlock(&obj->iommu_lock);
909 910 911 912
	return ERR_PTR(err);
}

/**
913
 * omap_iommu_detach - release iommu device
914 915
 * @obj:	target iommu
 **/
916
static void omap_iommu_detach(struct omap_iommu *obj)
917
{
918
	if (!obj || IS_ERR(obj))
919 920
		return;

921
	spin_lock(&obj->iommu_lock);
922 923 924 925 926 927

	if (--obj->refcount == 0)
		iommu_disable(obj);

	module_put(obj->owner);

928
	obj->iopgd = NULL;
929

930
	spin_unlock(&obj->iommu_lock);
931

932
	dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
933 934
}

935 936 937
/*
 *	OMAP Device MMU(IOMMU) detection
 */
938
static int omap_iommu_probe(struct platform_device *pdev)
939 940 941
{
	int err = -ENODEV;
	int irq;
942
	struct omap_iommu *obj;
943 944 945
	struct resource *res;
	struct iommu_platform_data *pdata = pdev->dev.platform_data;

946
	obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
947 948 949 950 951 952 953
	if (!obj)
		return -ENOMEM;

	obj->nr_tlb_entries = pdata->nr_tlb_entries;
	obj->name = pdata->name;
	obj->dev = &pdev->dev;
	obj->ctx = (void *)obj + sizeof(*obj);
954 955
	obj->da_start = pdata->da_start;
	obj->da_end = pdata->da_end;
956

957
	spin_lock_init(&obj->iommu_lock);
958 959 960 961 962
	mutex_init(&obj->mmap_lock);
	spin_lock_init(&obj->page_table_lock);
	INIT_LIST_HEAD(&obj->mmap);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
963 964 965
	obj->regbase = devm_ioremap_resource(obj->dev, res);
	if (IS_ERR(obj->regbase))
		return PTR_ERR(obj->regbase);
966

967
	irq = platform_get_irq(pdev, 0);
968 969 970 971 972
	if (irq < 0)
		return -ENODEV;

	err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
			       dev_name(obj->dev), obj);
973
	if (err < 0)
974
		return err;
975 976
	platform_set_drvdata(pdev, obj);

977 978 979
	pm_runtime_irq_safe(obj->dev);
	pm_runtime_enable(obj->dev);

980 981 982 983
	dev_info(&pdev->dev, "%s registered\n", obj->name);
	return 0;
}

984
static int omap_iommu_remove(struct platform_device *pdev)
985
{
986
	struct omap_iommu *obj = platform_get_drvdata(pdev);
987 988 989

	iopgtable_clear_entry_all(obj);

990 991
	pm_runtime_disable(obj->dev);

992 993 994 995 996 997
	dev_info(&pdev->dev, "%s removed\n", obj->name);
	return 0;
}

static struct platform_driver omap_iommu_driver = {
	.probe	= omap_iommu_probe,
998
	.remove	= omap_iommu_remove,
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	.driver	= {
		.name	= "omap-iommu",
	},
};

static void iopte_cachep_ctor(void *iopte)
{
	clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
}

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa,
				   u32 flags)
{
	memset(e, 0, sizeof(*e));

	e->da		= da;
	e->pa		= pa;
	e->valid	= 1;
	/* FIXME: add OMAP1 support */
	e->pgsz		= flags & MMU_CAM_PGSZ_MASK;
	e->endian	= flags & MMU_RAM_ENDIAN_MASK;
	e->elsz		= flags & MMU_RAM_ELSZ_MASK;
	e->mixed	= flags & MMU_RAM_MIXED_MASK;

	return iopgsz_to_bytes(e->pgsz);
}

1026
static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1027
			 phys_addr_t pa, size_t bytes, int prot)
1028 1029
{
	struct omap_iommu_domain *omap_domain = domain->priv;
1030
	struct omap_iommu *oiommu = omap_domain->iommu_dev;
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	struct device *dev = oiommu->dev;
	struct iotlb_entry e;
	int omap_pgsz;
	u32 ret, flags;

	/* we only support mapping a single iommu page for now */
	omap_pgsz = bytes_to_iopgsz(bytes);
	if (omap_pgsz < 0) {
		dev_err(dev, "invalid size to map: %d\n", bytes);
		return -EINVAL;
	}

	dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);

	flags = omap_pgsz | prot;

	iotlb_init_entry(&e, da, pa, flags);

1049
	ret = omap_iopgtable_store_entry(oiommu, &e);
1050
	if (ret)
1051
		dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
1052

1053
	return ret;
1054 1055
}

1056 1057
static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
			    size_t size)
1058 1059
{
	struct omap_iommu_domain *omap_domain = domain->priv;
1060
	struct omap_iommu *oiommu = omap_domain->iommu_dev;
1061 1062
	struct device *dev = oiommu->dev;

1063
	dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
1064

1065
	return iopgtable_clear_entry(oiommu, da);
1066 1067 1068 1069 1070 1071
}

static int
omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
{
	struct omap_iommu_domain *omap_domain = domain->priv;
1072
	struct omap_iommu *oiommu;
1073
	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	int ret = 0;

	spin_lock(&omap_domain->lock);

	/* only a single device is supported per domain for now */
	if (omap_domain->iommu_dev) {
		dev_err(dev, "iommu domain is already attached\n");
		ret = -EBUSY;
		goto out;
	}

	/* get a handle to and enable the omap iommu */
1086
	oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
1087 1088 1089 1090 1091 1092
	if (IS_ERR(oiommu)) {
		ret = PTR_ERR(oiommu);
		dev_err(dev, "can't get omap iommu: %d\n", ret);
		goto out;
	}

1093
	omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
1094
	omap_domain->dev = dev;
1095
	oiommu->domain = domain;
1096 1097 1098 1099 1100 1101

out:
	spin_unlock(&omap_domain->lock);
	return ret;
}

1102 1103
static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
			struct device *dev)
1104
{
1105
	struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
1106
	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1107 1108 1109 1110

	/* only a single device is supported per domain for now */
	if (omap_domain->iommu_dev != oiommu) {
		dev_err(dev, "invalid iommu device\n");
1111
		return;
1112 1113 1114 1115 1116 1117
	}

	iopgtable_clear_entry_all(oiommu);

	omap_iommu_detach(oiommu);

1118
	omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
1119 1120
	omap_domain->dev = NULL;
}
1121

1122 1123 1124 1125 1126 1127 1128
static void omap_iommu_detach_dev(struct iommu_domain *domain,
				 struct device *dev)
{
	struct omap_iommu_domain *omap_domain = domain->priv;

	spin_lock(&omap_domain->lock);
	_omap_iommu_detach_dev(omap_domain, dev);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
	spin_unlock(&omap_domain->lock);
}

static int omap_iommu_domain_init(struct iommu_domain *domain)
{
	struct omap_iommu_domain *omap_domain;

	omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
	if (!omap_domain) {
		pr_err("kzalloc failed\n");
		goto out;
	}

	omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
	if (!omap_domain->pgtable) {
		pr_err("kzalloc failed\n");
		goto fail_nomem;
	}

	/*
	 * should never fail, but please keep this around to ensure
	 * we keep the hardware happy
	 */
	BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));

	clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
	spin_lock_init(&omap_domain->lock);

	domain->priv = omap_domain;

1159 1160 1161 1162
	domain->geometry.aperture_start = 0;
	domain->geometry.aperture_end   = (1ULL << 32) - 1;
	domain->geometry.force_aperture = true;

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	return 0;

fail_nomem:
	kfree(omap_domain);
out:
	return -ENOMEM;
}

static void omap_iommu_domain_destroy(struct iommu_domain *domain)
{
	struct omap_iommu_domain *omap_domain = domain->priv;

	domain->priv = NULL;

1177 1178 1179 1180 1181 1182 1183
	/*
	 * An iommu device is still attached
	 * (currently, only one device can be attached) ?
	 */
	if (omap_domain->iommu_dev)
		_omap_iommu_detach_dev(omap_domain, omap_domain->dev);

1184 1185 1186 1187 1188
	kfree(omap_domain->pgtable);
	kfree(omap_domain);
}

static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1189
					  dma_addr_t da)
1190 1191
{
	struct omap_iommu_domain *omap_domain = domain->priv;
1192
	struct omap_iommu *oiommu = omap_domain->iommu_dev;
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	struct device *dev = oiommu->dev;
	u32 *pgd, *pte;
	phys_addr_t ret = 0;

	iopgtable_lookup_entry(oiommu, da, &pgd, &pte);

	if (pte) {
		if (iopte_is_small(*pte))
			ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
		else if (iopte_is_large(*pte))
			ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
		else
1205 1206
			dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
							(unsigned long long)da);
1207 1208 1209 1210 1211 1212
	} else {
		if (iopgd_is_section(*pgd))
			ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
		else if (iopgd_is_super(*pgd))
			ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
		else
1213 1214
			dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
							(unsigned long long)da);
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	}

	return ret;
}

static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
				    unsigned long cap)
{
	return 0;
}

static struct iommu_ops omap_iommu_ops = {
	.domain_init	= omap_iommu_domain_init,
	.domain_destroy	= omap_iommu_domain_destroy,
	.attach_dev	= omap_iommu_attach_dev,
	.detach_dev	= omap_iommu_detach_dev,
	.map		= omap_iommu_map,
	.unmap		= omap_iommu_unmap,
	.iova_to_phys	= omap_iommu_iova_to_phys,
	.domain_has_cap	= omap_iommu_domain_has_cap,
1235
	.pgsize_bitmap	= OMAP_IOMMU_PGSIZES,
1236 1237
};

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
static int __init omap_iommu_init(void)
{
	struct kmem_cache *p;
	const unsigned long flags = SLAB_HWCACHE_ALIGN;
	size_t align = 1 << 10; /* L2 pagetable alignement */

	p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
			      iopte_cachep_ctor);
	if (!p)
		return -ENOMEM;
	iopte_cachep = p;

1250
	bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1251

1252 1253
	return platform_driver_register(&omap_iommu_driver);
}
1254 1255
/* must be ready before omap3isp is probed */
subsys_initcall(omap_iommu_init);
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

static void __exit omap_iommu_exit(void)
{
	kmem_cache_destroy(iopte_cachep);

	platform_driver_unregister(&omap_iommu_driver);
}
module_exit(omap_iommu_exit);

MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
MODULE_ALIAS("platform:omap-iommu");
MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
MODULE_LICENSE("GPL v2");