omap-iommu.c 31.3 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 <linux/of.h>
#include <linux/of_iommu.h>
#include <linux/of_irq.h>
29
#include <linux/of_platform.h>
30 31 32

#include <asm/cacheflush.h>

33
#include <linux/platform_data/iommu-omap.h>
34

35
#include "omap-iopgtable.h"
36
#include "omap-iommu.h"
37

38 39 40
#define to_iommu(dev)							\
	((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))

41 42 43 44 45
#define for_each_iotlb_cr(obj, n, __i, cr)				\
	for (__i = 0;							\
	     (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true);	\
	     __i++)

46 47 48
/* bitmap of the page sizes currently supported */
#define OMAP_IOMMU_PGSIZES	(SZ_4K | SZ_64K | SZ_1M | SZ_16M)

49 50 51 52 53
/**
 * 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.
54
 * @dev:	Device using this domain.
55 56 57 58
 * @lock:	domain lock, should be taken when attaching/detaching
 */
struct omap_iommu_domain {
	u32 *pgtable;
59
	struct omap_iommu *iommu_dev;
60
	struct device *dev;
61
	spinlock_t lock;
62
	struct iommu_domain domain;
63 64
};

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#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;
};

80 81 82
static struct platform_driver omap_iommu_driver;
static struct kmem_cache *iopte_cachep;

83 84 85 86 87 88 89 90 91
/**
 * to_omap_domain - Get struct omap_iommu_domain from generic iommu_domain
 * @dom:	generic iommu domain handle
 **/
static struct omap_iommu_domain *to_omap_domain(struct iommu_domain *dom)
{
	return container_of(dom, struct omap_iommu_domain, domain);
}

92
/**
93
 * omap_iommu_save_ctx - Save registers for pm off-mode support
94
 * @dev:	client device
95
 **/
96
void omap_iommu_save_ctx(struct device *dev)
97
{
98
	struct omap_iommu *obj = dev_to_omap_iommu(dev);
99 100
	u32 *p = obj->ctx;
	int i;
101

102 103 104 105
	for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
		p[i] = iommu_read_reg(obj, i * sizeof(u32));
		dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
	}
106
}
107
EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
108 109

/**
110
 * omap_iommu_restore_ctx - Restore registers for pm off-mode support
111
 * @dev:	client device
112
 **/
113
void omap_iommu_restore_ctx(struct device *dev)
114
{
115
	struct omap_iommu *obj = dev_to_omap_iommu(dev);
116 117
	u32 *p = obj->ctx;
	int i;
118

119 120 121 122
	for (i = 0; i < (MMU_REG_SIZE / sizeof(u32)); i++) {
		iommu_write_reg(obj, p[i], i * sizeof(u32));
		dev_dbg(obj->dev, "%s\t[%02d] %08x\n", __func__, i, p[i]);
	}
123
}
124
EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static void __iommu_set_twl(struct omap_iommu *obj, bool on)
{
	u32 l = iommu_read_reg(obj, MMU_CNTL);

	if (on)
		iommu_write_reg(obj, MMU_IRQ_TWL_MASK, MMU_IRQENABLE);
	else
		iommu_write_reg(obj, MMU_IRQ_TLB_MISS_MASK, MMU_IRQENABLE);

	l &= ~MMU_CNTL_MASK;
	if (on)
		l |= (MMU_CNTL_MMU_EN | MMU_CNTL_TWL_EN);
	else
		l |= (MMU_CNTL_MMU_EN);

	iommu_write_reg(obj, l, MMU_CNTL);
}

static int omap2_iommu_enable(struct omap_iommu *obj)
{
	u32 l, pa;

	if (!obj->iopgd || !IS_ALIGNED((u32)obj->iopgd,  SZ_16K))
		return -EINVAL;

	pa = virt_to_phys(obj->iopgd);
	if (!IS_ALIGNED(pa, SZ_16K))
		return -EINVAL;

	l = iommu_read_reg(obj, MMU_REVISION);
	dev_info(obj->dev, "%s: version %d.%d\n", obj->name,
		 (l >> 4) & 0xf, l & 0xf);

	iommu_write_reg(obj, pa, MMU_TTB);

	if (obj->has_bus_err_back)
		iommu_write_reg(obj, MMU_GP_REG_BUS_ERR_BACK_EN, MMU_GP_REG);

	__iommu_set_twl(obj, true);

	return 0;
}

static void omap2_iommu_disable(struct omap_iommu *obj)
{
	u32 l = iommu_read_reg(obj, MMU_CNTL);

	l &= ~MMU_CNTL_MASK;
	iommu_write_reg(obj, l, MMU_CNTL);

	dev_dbg(obj->dev, "%s is shutting down\n", obj->name);
}

179
static int iommu_enable(struct omap_iommu *obj)
180 181
{
	int err;
182
	struct platform_device *pdev = to_platform_device(obj->dev);
K
Kiran Padwal 已提交
183
	struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
184

185
	if (pdata && pdata->deassert_reset) {
186 187 188 189 190 191 192
		err = pdata->deassert_reset(pdev, pdata->reset_name);
		if (err) {
			dev_err(obj->dev, "deassert_reset failed: %d\n", err);
			return err;
		}
	}

193
	pm_runtime_get_sync(obj->dev);
194

195
	err = omap2_iommu_enable(obj);
196 197 198 199

	return err;
}

200
static void iommu_disable(struct omap_iommu *obj)
201
{
202
	struct platform_device *pdev = to_platform_device(obj->dev);
K
Kiran Padwal 已提交
203
	struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
204

205
	omap2_iommu_disable(obj);
206

207
	pm_runtime_put_sync(obj->dev);
208

209
	if (pdata && pdata->assert_reset)
210
		pdata->assert_reset(pdev, pdata->reset_name);
211 212 213 214 215 216 217 218 219 220
}

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

221
	return cr->cam & MMU_CAM_V;
222 223
}

224
static u32 iotlb_cr_to_virt(struct cr_regs *cr)
225
{
226 227 228 229
	u32 page_size = cr->cam & MMU_CAM_PGSZ_MASK;
	u32 mask = get_cam_va_mask(cr->cam & page_size);

	return cr->cam & mask;
230 231 232 233
}

static u32 get_iopte_attr(struct iotlb_entry *e)
{
234 235 236 237 238 239 240 241
	u32 attr;

	attr = e->mixed << 5;
	attr |= e->endian;
	attr |= e->elsz >> 3;
	attr <<= (((e->pgsz == MMU_CAM_PGSZ_4K) ||
			(e->pgsz == MMU_CAM_PGSZ_64K)) ? 0 : 6);
	return attr;
242 243
}

244
static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
245
{
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	u32 status, fault_addr;

	status = iommu_read_reg(obj, MMU_IRQSTATUS);
	status &= MMU_IRQ_MASK;
	if (!status) {
		*da = 0;
		return 0;
	}

	fault_addr = iommu_read_reg(obj, MMU_FAULT_AD);
	*da = fault_addr;

	iommu_write_reg(obj, status, MMU_IRQSTATUS);

	return status;
261 262
}

263
static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
264 265 266 267 268 269 270 271 272 273
{
	u32 val;

	val = iommu_read_reg(obj, MMU_LOCK);

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

}

274
static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
275 276 277 278 279 280 281 282 283
{
	u32 val;

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

	iommu_write_reg(obj, val, MMU_LOCK);
}

284
static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
285
{
286 287
	cr->cam = iommu_read_reg(obj, MMU_READ_CAM);
	cr->ram = iommu_read_reg(obj, MMU_READ_RAM);
288 289
}

290
static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
291
{
292 293
	iommu_write_reg(obj, cr->cam | MMU_CAM_V, MMU_CAM);
	iommu_write_reg(obj, cr->ram, MMU_RAM);
294 295 296 297 298

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

299
/* only used in iotlb iteration for-loop */
300
static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
301 302 303 304 305 306 307 308 309 310 311 312
{
	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;
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
#ifdef PREFETCH_IOTLB
static struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
				      struct iotlb_entry *e)
{
	struct cr_regs *cr;

	if (!e)
		return NULL;

	if (e->da & ~(get_cam_va_mask(e->pgsz))) {
		dev_err(obj->dev, "%s:\twrong alignment: %08x\n", __func__,
			e->da);
		return ERR_PTR(-EINVAL);
	}

	cr = kmalloc(sizeof(*cr), GFP_KERNEL);
	if (!cr)
		return ERR_PTR(-ENOMEM);

	cr->cam = (e->da & MMU_CAM_VATAG_MASK) | e->prsvd | e->pgsz | e->valid;
	cr->ram = e->pa | e->endian | e->elsz | e->mixed;

	return cr;
}

338 339 340 341 342
/**
 * load_iotlb_entry - Set an iommu tlb entry
 * @obj:	target iommu
 * @e:		an iommu tlb entry info
 **/
343
static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
344 345 346 347 348 349 350 351
{
	int err = 0;
	struct iotlb_lock l;
	struct cr_regs *cr;

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

352
	pm_runtime_get_sync(obj->dev);
353

354 355 356
	iotlb_lock_get(obj, &l);
	if (l.base == obj->nr_tlb_entries) {
		dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
357 358 359
		err = -EBUSY;
		goto out;
	}
360
	if (!e->prsvd) {
361 362
		int i;
		struct cr_regs tmp;
363

364
		for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
365 366
			if (!iotlb_cr_valid(&tmp))
				break;
367

368 369 370 371 372
		if (i == obj->nr_tlb_entries) {
			dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
			err = -EBUSY;
			goto out;
		}
373 374

		iotlb_lock_get(obj, &l);
375 376 377 378
	} else {
		l.vict = l.base;
		iotlb_lock_set(obj, &l);
	}
379 380 381

	cr = iotlb_alloc_cr(obj, e);
	if (IS_ERR(cr)) {
382
		pm_runtime_put_sync(obj->dev);
383 384 385 386 387 388
		return PTR_ERR(cr);
	}

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

389 390
	if (e->prsvd)
		l.base++;
391 392
	/* increment victim for next tlb load */
	if (++l.vict == obj->nr_tlb_entries)
393
		l.vict = l.base;
394 395
	iotlb_lock_set(obj, &l);
out:
396
	pm_runtime_put_sync(obj->dev);
397 398 399
	return err;
}

400 401
#else /* !PREFETCH_IOTLB */

402
static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
403 404 405 406 407 408
{
	return 0;
}

#endif /* !PREFETCH_IOTLB */

409
static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
410 411 412
{
	return load_iotlb_entry(obj, e);
}
413 414 415 416 417 418 419 420

/**
 * 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.
 **/
421
static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
422 423
{
	int i;
424
	struct cr_regs cr;
425

426
	pm_runtime_get_sync(obj->dev);
427

428
	for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
429 430 431 432 433 434 435 436 437 438 439 440
		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);
441
			iotlb_load_cr(obj, &cr);
442
			iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
443
			break;
444 445
		}
	}
446
	pm_runtime_put_sync(obj->dev);
447 448 449 450 451 452 453 454 455

	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
 **/
456
static void flush_iotlb_all(struct omap_iommu *obj)
457 458 459
{
	struct iotlb_lock l;

460
	pm_runtime_get_sync(obj->dev);
461 462 463 464 465 466 467

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

	iommu_write_reg(obj, 1, MMU_GFLUSH);

468
	pm_runtime_put_sync(obj->dev);
469
}
470

471
#ifdef CONFIG_OMAP_IOMMU_DEBUG
472

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
#define pr_reg(name)							\
	do {								\
		ssize_t bytes;						\
		const char *str = "%20s: %08x\n";			\
		const int maxcol = 32;					\
		bytes = snprintf(p, maxcol, str, __stringify(name),	\
				 iommu_read_reg(obj, MMU_##name));	\
		p += bytes;						\
		len -= bytes;						\
		if (len < maxcol)					\
			goto out;					\
	} while (0)

static ssize_t
omap2_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t len)
{
	char *p = buf;

	pr_reg(REVISION);
	pr_reg(IRQSTATUS);
	pr_reg(IRQENABLE);
	pr_reg(WALKING_ST);
	pr_reg(CNTL);
	pr_reg(FAULT_AD);
	pr_reg(TTB);
	pr_reg(LOCK);
	pr_reg(LD_TLB);
	pr_reg(CAM);
	pr_reg(RAM);
	pr_reg(GFLUSH);
	pr_reg(FLUSH_ENTRY);
	pr_reg(READ_CAM);
	pr_reg(READ_RAM);
	pr_reg(EMU_FAULT_AD);
out:
	return p - buf;
}

511
ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
512 513 514 515
{
	if (!obj || !buf)
		return -EINVAL;

516
	pm_runtime_get_sync(obj->dev);
517

518
	bytes = omap2_iommu_dump_ctx(obj, buf, bytes);
519

520
	pm_runtime_put_sync(obj->dev);
521 522 523 524

	return bytes;
}

525 526
static int
__dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
527 528
{
	int i;
529 530
	struct iotlb_lock saved;
	struct cr_regs tmp;
531 532
	struct cr_regs *p = crs;

533
	pm_runtime_get_sync(obj->dev);
534 535
	iotlb_lock_get(obj, &saved);

536
	for_each_iotlb_cr(obj, num, i, tmp) {
537 538 539 540
		if (!iotlb_cr_valid(&tmp))
			continue;
		*p++ = tmp;
	}
541

542
	iotlb_lock_set(obj, &saved);
543
	pm_runtime_put_sync(obj->dev);
544 545 546 547

	return  p - crs;
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/**
 * iotlb_dump_cr - Dump an iommu tlb entry into buf
 * @obj:	target iommu
 * @cr:		contents of cam and ram register
 * @buf:	output buffer
 **/
static ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
			     char *buf)
{
	char *p = buf;

	/* FIXME: Need more detail analysis of cam/ram */
	p += sprintf(p, "%08x %08x %01x\n", cr->cam, cr->ram,
					(cr->cam & MMU_CAM_P) ? 1 : 0);

	return p - buf;
}

566
/**
567
 * omap_dump_tlb_entries - dump cr arrays to given buffer
568 569 570
 * @obj:	target iommu
 * @buf:	output buffer
 **/
571
size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
572
{
573
	int i, num;
574 575 576
	struct cr_regs *cr;
	char *p = buf;

577 578 579 580
	num = bytes / sizeof(*cr);
	num = min(obj->nr_tlb_entries, num);

	cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
581 582 583
	if (!cr)
		return 0;

584 585
	num = __dump_tlb_entries(obj, cr, num);
	for (i = 0; i < num; i++)
586 587 588 589 590 591
		p += iotlb_dump_cr(obj, cr + i, p);
	kfree(cr);

	return p - buf;
}

592
#endif /* CONFIG_OMAP_IOMMU_DEBUG */
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

/*
 *	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 */
620 621
	if (iopte)
		kmem_cache_free(iopte_cachep, iopte);
622 623
}

624
static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
{
	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;
}

662
static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
663 664 665
{
	u32 *iopgd = iopgd_offset(obj, da);

666 667 668 669 670 671
	if ((da | pa) & ~IOSECTION_MASK) {
		dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
			__func__, da, pa, IOSECTION_SIZE);
		return -EINVAL;
	}

672 673 674 675 676
	*iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
	flush_iopgd_range(iopgd, iopgd);
	return 0;
}

677
static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
678 679 680 681
{
	u32 *iopgd = iopgd_offset(obj, da);
	int i;

682 683 684 685 686 687
	if ((da | pa) & ~IOSUPER_MASK) {
		dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
			__func__, da, pa, IOSUPER_SIZE);
		return -EINVAL;
	}

688 689 690 691 692 693
	for (i = 0; i < 16; i++)
		*(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
	flush_iopgd_range(iopgd, iopgd + 15);
	return 0;
}

694
static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
{
	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;
}

711
static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
712 713 714 715 716
{
	u32 *iopgd = iopgd_offset(obj, da);
	u32 *iopte = iopte_alloc(obj, iopgd, da);
	int i;

717 718 719 720 721 722
	if ((da | pa) & ~IOLARGE_MASK) {
		dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
			__func__, da, pa, IOLARGE_SIZE);
		return -EINVAL;
	}

723 724 725 726 727 728 729 730 731
	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;
}

732 733
static int
iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
734
{
735
	int (*fn)(struct omap_iommu *, u32, u32, u32);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	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;
}

/**
771
 * omap_iopgtable_store_entry - Make an iommu pte entry
772 773 774
 * @obj:	target iommu
 * @e:		an iommu tlb entry info
 **/
775 776
static int
omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
777 778 779 780 781 782
{
	int err;

	flush_iotlb_page(obj, e->da);
	err = iopgtable_store_entry_core(obj, e);
	if (!err)
783
		prefetch_iotlb_entry(obj, e);
784 785 786 787 788 789 790 791 792 793
	return err;
}

/**
 * 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
 **/
794 795
static void
iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
796 797 798 799 800 801 802
{
	u32 *iopgd, *iopte = NULL;

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

803
	if (iopgd_is_table(*iopgd))
804 805 806 807 808 809
		iopte = iopte_offset(iopgd, da);
out:
	*ppgd = iopgd;
	*ppte = iopte;
}

810
static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
811 812 813 814 815 816 817 818
{
	size_t bytes;
	u32 *iopgd = iopgd_offset(obj, da);
	int nent = 1;

	if (!*iopgd)
		return 0;

819
	if (iopgd_is_table(*iopgd)) {
820 821 822 823 824 825 826
		int i;
		u32 *iopte = iopte_offset(iopgd, da);

		bytes = IOPTE_SIZE;
		if (*iopte & IOPTE_LARGE) {
			nent *= 16;
			/* rewind to the 1st entry */
827
			iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
		}
		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;
845
		if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
846 847
			nent *= 16;
			/* rewind to the 1st entry */
848
			iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
849 850 851 852 853 854 855 856 857 858 859 860 861 862
		}
		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
 **/
863
static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
864 865 866 867 868 869 870 871 872 873 874 875 876
{
	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;
}

877
static void iopgtable_clear_entry_all(struct omap_iommu *obj)
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
{
	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;

893
		if (iopgd_is_table(*iopgd))
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
			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)
{
910
	u32 da, errs;
911
	u32 *iopgd, *iopte;
912
	struct omap_iommu *obj = data;
913
	struct iommu_domain *domain = obj->domain;
914
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
915

916
	if (!omap_domain->iommu_dev)
917 918
		return IRQ_NONE;

919
	errs = iommu_report_fault(obj, &da);
920 921
	if (errs == 0)
		return IRQ_HANDLED;
922 923

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

927 928
	iommu_disable(obj);

929 930
	iopgd = iopgd_offset(obj, da);

931
	if (!iopgd_is_table(*iopgd)) {
932 933
		dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:px%08x\n",
				obj->name, errs, da, iopgd, *iopgd);
934 935 936 937 938
		return IRQ_NONE;
	}

	iopte = iopte_offset(iopgd, da);

939 940
	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);
941 942 943 944 945 946

	return IRQ_NONE;
}

static int device_match_by_alias(struct device *dev, void *data)
{
947
	struct omap_iommu *obj = to_iommu(dev);
948 949 950 951 952 953 954 955
	const char *name = data;

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

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

/**
956
 * omap_iommu_attach() - attach iommu device to an iommu domain
957
 * @name:	name of target omap iommu device
958
 * @iopgd:	page table
959
 **/
960
static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
961
{
962
	int err;
963 964 965 966 967 968 969
	struct device *dev;
	struct omap_iommu *obj;

	dev = driver_find_device(&omap_iommu_driver.driver, NULL,
				(void *)name,
				device_match_by_alias);
	if (!dev)
970
		return ERR_PTR(-ENODEV);
971 972

	obj = to_iommu(dev);
973

974
	spin_lock(&obj->iommu_lock);
975

976 977 978 979 980 981 982
	obj->iopgd = iopgd;
	err = iommu_enable(obj);
	if (err)
		goto err_enable;
	flush_iotlb_all(obj);

	spin_unlock(&obj->iommu_lock);
983 984 985 986 987

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

err_enable:
988
	spin_unlock(&obj->iommu_lock);
989 990 991 992
	return ERR_PTR(err);
}

/**
993
 * omap_iommu_detach - release iommu device
994 995
 * @obj:	target iommu
 **/
996
static void omap_iommu_detach(struct omap_iommu *obj)
997
{
998
	if (!obj || IS_ERR(obj))
999 1000
		return;

1001
	spin_lock(&obj->iommu_lock);
1002

1003
	iommu_disable(obj);
1004
	obj->iopgd = NULL;
1005

1006
	spin_unlock(&obj->iommu_lock);
1007

1008
	dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
1009 1010
}

1011 1012 1013
/*
 *	OMAP Device MMU(IOMMU) detection
 */
1014
static int omap_iommu_probe(struct platform_device *pdev)
1015 1016 1017
{
	int err = -ENODEV;
	int irq;
1018
	struct omap_iommu *obj;
1019
	struct resource *res;
K
Kiran Padwal 已提交
1020
	struct iommu_platform_data *pdata = dev_get_platdata(&pdev->dev);
1021
	struct device_node *of = pdev->dev.of_node;
1022

1023
	obj = devm_kzalloc(&pdev->dev, sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
1024 1025 1026
	if (!obj)
		return -ENOMEM;

1027 1028 1029 1030 1031 1032 1033 1034 1035
	if (of) {
		obj->name = dev_name(&pdev->dev);
		obj->nr_tlb_entries = 32;
		err = of_property_read_u32(of, "ti,#tlb-entries",
					   &obj->nr_tlb_entries);
		if (err && err != -EINVAL)
			return err;
		if (obj->nr_tlb_entries != 32 && obj->nr_tlb_entries != 8)
			return -EINVAL;
1036 1037
		if (of_find_property(of, "ti,iommu-bus-err-back", NULL))
			obj->has_bus_err_back = MMU_GP_REG_BUS_ERR_BACK_EN;
1038 1039 1040 1041 1042
	} else {
		obj->nr_tlb_entries = pdata->nr_tlb_entries;
		obj->name = pdata->name;
	}

1043 1044 1045
	obj->dev = &pdev->dev;
	obj->ctx = (void *)obj + sizeof(*obj);

1046
	spin_lock_init(&obj->iommu_lock);
1047 1048 1049
	spin_lock_init(&obj->page_table_lock);

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1050 1051 1052
	obj->regbase = devm_ioremap_resource(obj->dev, res);
	if (IS_ERR(obj->regbase))
		return PTR_ERR(obj->regbase);
1053

1054
	irq = platform_get_irq(pdev, 0);
1055 1056 1057 1058 1059
	if (irq < 0)
		return -ENODEV;

	err = devm_request_irq(obj->dev, irq, iommu_fault_handler, IRQF_SHARED,
			       dev_name(obj->dev), obj);
1060
	if (err < 0)
1061
		return err;
1062 1063
	platform_set_drvdata(pdev, obj);

1064 1065 1066
	pm_runtime_irq_safe(obj->dev);
	pm_runtime_enable(obj->dev);

1067 1068
	omap_iommu_debugfs_add(obj);

1069 1070 1071 1072
	dev_info(&pdev->dev, "%s registered\n", obj->name);
	return 0;
}

1073
static int omap_iommu_remove(struct platform_device *pdev)
1074
{
1075
	struct omap_iommu *obj = platform_get_drvdata(pdev);
1076 1077

	iopgtable_clear_entry_all(obj);
1078
	omap_iommu_debugfs_remove(obj);
1079

1080 1081
	pm_runtime_disable(obj->dev);

1082 1083 1084 1085
	dev_info(&pdev->dev, "%s removed\n", obj->name);
	return 0;
}

1086
static const struct of_device_id omap_iommu_of_match[] = {
1087 1088 1089 1090 1091 1092 1093
	{ .compatible = "ti,omap2-iommu" },
	{ .compatible = "ti,omap4-iommu" },
	{ .compatible = "ti,dra7-iommu"	},
	{},
};
MODULE_DEVICE_TABLE(of, omap_iommu_of_match);

1094 1095
static struct platform_driver omap_iommu_driver = {
	.probe	= omap_iommu_probe,
1096
	.remove	= omap_iommu_remove,
1097 1098
	.driver	= {
		.name	= "omap-iommu",
1099
		.of_match_table = of_match_ptr(omap_iommu_of_match),
1100 1101 1102 1103 1104 1105 1106 1107
	},
};

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

1108
static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz)
1109 1110 1111 1112 1113
{
	memset(e, 0, sizeof(*e));

	e->da		= da;
	e->pa		= pa;
1114
	e->valid	= MMU_CAM_V;
1115 1116 1117 1118
	e->pgsz		= pgsz;
	e->endian	= MMU_RAM_ENDIAN_LITTLE;
	e->elsz		= MMU_RAM_ELSZ_8;
	e->mixed	= 0;
1119 1120 1121 1122

	return iopgsz_to_bytes(e->pgsz);
}

1123
static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
1124
			 phys_addr_t pa, size_t bytes, int prot)
1125
{
1126
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1127
	struct omap_iommu *oiommu = omap_domain->iommu_dev;
1128 1129 1130
	struct device *dev = oiommu->dev;
	struct iotlb_entry e;
	int omap_pgsz;
1131
	u32 ret;
1132 1133 1134 1135 1136 1137 1138

	omap_pgsz = bytes_to_iopgsz(bytes);
	if (omap_pgsz < 0) {
		dev_err(dev, "invalid size to map: %d\n", bytes);
		return -EINVAL;
	}

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

1141
	iotlb_init_entry(&e, da, pa, omap_pgsz);
1142

1143
	ret = omap_iopgtable_store_entry(oiommu, &e);
1144
	if (ret)
1145
		dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
1146

1147
	return ret;
1148 1149
}

1150 1151
static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
			    size_t size)
1152
{
1153
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1154
	struct omap_iommu *oiommu = omap_domain->iommu_dev;
1155 1156
	struct device *dev = oiommu->dev;

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

1159
	return iopgtable_clear_entry(oiommu, da);
1160 1161 1162 1163 1164
}

static int
omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
{
1165
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1166
	struct omap_iommu *oiommu;
1167
	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1168 1169
	int ret = 0;

1170 1171 1172 1173 1174
	if (!arch_data || !arch_data->name) {
		dev_err(dev, "device doesn't have an associated iommu\n");
		return -EINVAL;
	}

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	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 */
1185
	oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
1186 1187 1188 1189 1190 1191
	if (IS_ERR(oiommu)) {
		ret = PTR_ERR(oiommu);
		dev_err(dev, "can't get omap iommu: %d\n", ret);
		goto out;
	}

1192
	omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
1193
	omap_domain->dev = dev;
1194
	oiommu->domain = domain;
1195 1196 1197 1198 1199 1200

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

1201 1202
static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
			struct device *dev)
1203
{
1204
	struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
1205
	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
1206 1207 1208 1209

	/* only a single device is supported per domain for now */
	if (omap_domain->iommu_dev != oiommu) {
		dev_err(dev, "invalid iommu device\n");
1210
		return;
1211 1212 1213 1214 1215 1216
	}

	iopgtable_clear_entry_all(oiommu);

	omap_iommu_detach(oiommu);

1217
	omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
1218
	omap_domain->dev = NULL;
1219
	oiommu->domain = NULL;
1220
}
1221

1222 1223 1224
static void omap_iommu_detach_dev(struct iommu_domain *domain,
				 struct device *dev)
{
1225
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1226 1227 1228

	spin_lock(&omap_domain->lock);
	_omap_iommu_detach_dev(omap_domain, dev);
1229 1230 1231
	spin_unlock(&omap_domain->lock);
}

1232
static struct iommu_domain *omap_iommu_domain_alloc(unsigned type)
1233 1234 1235
{
	struct omap_iommu_domain *omap_domain;

1236 1237 1238
	if (type != IOMMU_DOMAIN_UNMANAGED)
		return NULL;

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	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);

1260 1261 1262
	omap_domain->domain.geometry.aperture_start = 0;
	omap_domain->domain.geometry.aperture_end   = (1ULL << 32) - 1;
	omap_domain->domain.geometry.force_aperture = true;
1263

1264
	return &omap_domain->domain;
1265 1266 1267 1268

fail_nomem:
	kfree(omap_domain);
out:
1269
	return NULL;
1270 1271
}

1272
static void omap_iommu_domain_free(struct iommu_domain *domain)
1273
{
1274
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1275

1276 1277 1278 1279 1280 1281 1282
	/*
	 * 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);

1283 1284 1285 1286 1287
	kfree(omap_domain->pgtable);
	kfree(omap_domain);
}

static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
1288
					  dma_addr_t da)
1289
{
1290
	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
1291
	struct omap_iommu *oiommu = omap_domain->iommu_dev;
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	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
1304 1305
			dev_err(dev, "bogus pte 0x%x, da 0x%llx", *pte,
							(unsigned long long)da);
1306 1307 1308 1309 1310 1311
	} 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
1312 1313
			dev_err(dev, "bogus pgd 0x%x, da 0x%llx", *pgd,
							(unsigned long long)da);
1314 1315 1316 1317 1318
	}

	return ret;
}

1319 1320 1321 1322
static int omap_iommu_add_device(struct device *dev)
{
	struct omap_iommu_arch_data *arch_data;
	struct device_node *np;
1323
	struct platform_device *pdev;
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

	/*
	 * Allocate the archdata iommu structure for DT-based devices.
	 *
	 * TODO: Simplify this when removing non-DT support completely from the
	 * IOMMU users.
	 */
	if (!dev->of_node)
		return 0;

	np = of_parse_phandle(dev->of_node, "iommus", 0);
	if (!np)
		return 0;

1338 1339 1340 1341 1342 1343
	pdev = of_find_device_by_node(np);
	if (WARN_ON(!pdev)) {
		of_node_put(np);
		return -EINVAL;
	}

1344 1345 1346 1347 1348 1349
	arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
	if (!arch_data) {
		of_node_put(np);
		return -ENOMEM;
	}

1350
	arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	dev->archdata.iommu = arch_data;

	of_node_put(np);

	return 0;
}

static void omap_iommu_remove_device(struct device *dev)
{
	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;

	if (!dev->of_node || !arch_data)
		return;

	kfree(arch_data->name);
	kfree(arch_data);
}

1369
static const struct iommu_ops omap_iommu_ops = {
1370 1371
	.domain_alloc	= omap_iommu_domain_alloc,
	.domain_free	= omap_iommu_domain_free,
1372 1373 1374 1375
	.attach_dev	= omap_iommu_attach_dev,
	.detach_dev	= omap_iommu_detach_dev,
	.map		= omap_iommu_map,
	.unmap		= omap_iommu_unmap,
O
Olav Haugan 已提交
1376
	.map_sg		= default_iommu_map_sg,
1377
	.iova_to_phys	= omap_iommu_iova_to_phys,
1378 1379
	.add_device	= omap_iommu_add_device,
	.remove_device	= omap_iommu_remove_device,
1380
	.pgsize_bitmap	= OMAP_IOMMU_PGSIZES,
1381 1382
};

1383 1384 1385 1386 1387
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 */
1388 1389 1390 1391 1392 1393 1394
	struct device_node *np;

	np = of_find_matching_node(NULL, omap_iommu_of_match);
	if (!np)
		return 0;

	of_node_put(np);
1395 1396 1397 1398 1399 1400 1401

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

1402
	bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
1403

1404 1405
	omap_iommu_debugfs_init();

1406 1407
	return platform_driver_register(&omap_iommu_driver);
}
1408 1409
/* must be ready before omap3isp is probed */
subsys_initcall(omap_iommu_init);
1410 1411 1412 1413 1414 1415

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

	platform_driver_unregister(&omap_iommu_driver);
1416 1417

	omap_iommu_debugfs_exit();
1418 1419 1420 1421 1422 1423 1424
}
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");