omap-iovmm.c 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * omap iommu: simple virtual address space management
 *
 * Copyright (C) 2008-2009 Nokia Corporation
 *
 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.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.
 */

#include <linux/err.h>
14
#include <linux/slab.h>
15 16 17
#include <linux/vmalloc.h>
#include <linux/device.h>
#include <linux/scatterlist.h>
18
#include <linux/iommu.h>
19 20 21 22

#include <asm/cacheflush.h>
#include <asm/mach/map.h>

23 24
#include <plat/iommu.h>
#include <plat/iovmm.h>
25

26
#include <plat/iopgtable.h>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

static struct kmem_cache *iovm_area_cachep;

/* return total bytes of sg buffers */
static size_t sgtable_len(const struct sg_table *sgt)
{
	unsigned int i, total = 0;
	struct scatterlist *sg;

	if (!sgt)
		return 0;

	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
		size_t bytes;

42
		bytes = sg->length;
43 44 45 46 47 48 49 50 51 52 53 54 55 56

		if (!iopgsz_ok(bytes)) {
			pr_err("%s: sg[%d] not iommu pagesize(%x)\n",
			       __func__, i, bytes);
			return 0;
		}

		total += bytes;
	}

	return total;
}
#define sgtable_ok(x)	(!!sgtable_len(x))

57 58 59 60 61 62 63 64 65
static unsigned max_alignment(u32 addr)
{
	int i;
	unsigned pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, };
	for (i = 0; i < ARRAY_SIZE(pagesize) && addr & (pagesize[i] - 1); i++)
		;
	return (i < ARRAY_SIZE(pagesize)) ? pagesize[i] : 0;
}

66 67 68 69
/*
 * calculate the optimal number sg elements from total bytes based on
 * iommu superpages
 */
70
static unsigned sgtable_nents(size_t bytes, u32 da, u32 pa)
71
{
72
	unsigned nr_entries = 0, ent_sz;
73 74 75 76 77 78

	if (!IS_ALIGNED(bytes, PAGE_SIZE)) {
		pr_err("%s: wrong size %08x\n", __func__, bytes);
		return 0;
	}

79 80 81 82 83 84 85
	while (bytes) {
		ent_sz = max_alignment(da | pa);
		ent_sz = min_t(unsigned, ent_sz, iopgsz_max(bytes));
		nr_entries++;
		da += ent_sz;
		pa += ent_sz;
		bytes -= ent_sz;
86 87 88 89 90 91
	}

	return nr_entries;
}

/* allocate and initialize sg_table header(a kind of 'superblock') */
92 93
static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags,
							u32 da, u32 pa)
94 95 96 97 98 99 100 101 102 103 104
{
	unsigned int nr_entries;
	int err;
	struct sg_table *sgt;

	if (!bytes)
		return ERR_PTR(-EINVAL);

	if (!IS_ALIGNED(bytes, PAGE_SIZE))
		return ERR_PTR(-EINVAL);

105 106
	if (flags & IOVMF_LINEAR) {
		nr_entries = sgtable_nents(bytes, da, pa);
107 108 109 110 111 112 113 114 115 116
		if (!nr_entries)
			return ERR_PTR(-EINVAL);
	} else
		nr_entries =  bytes / PAGE_SIZE;

	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
	if (!sgt)
		return ERR_PTR(-ENOMEM);

	err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL);
S
Satish 已提交
117 118
	if (err) {
		kfree(sgt);
119
		return ERR_PTR(err);
S
Satish 已提交
120
	}
121 122 123 124 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

	pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries);

	return sgt;
}

/* free sg_table header(a kind of superblock) */
static void sgtable_free(struct sg_table *sgt)
{
	if (!sgt)
		return;

	sg_free_table(sgt);
	kfree(sgt);

	pr_debug("%s: sgt:%p\n", __func__, sgt);
}

/* map 'sglist' to a contiguous mpu virtual area and return 'va' */
static void *vmap_sg(const struct sg_table *sgt)
{
	u32 va;
	size_t total;
	unsigned int i;
	struct scatterlist *sg;
	struct vm_struct *new;
	const struct mem_type *mtype;

	mtype = get_mem_type(MT_DEVICE);
	if (!mtype)
		return ERR_PTR(-EINVAL);

	total = sgtable_len(sgt);
	if (!total)
		return ERR_PTR(-EINVAL);

	new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
	if (!new)
		return ERR_PTR(-ENOMEM);
	va = (u32)new->addr;

	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
		size_t bytes;
		u32 pa;
		int err;

		pa = sg_phys(sg);
168
		bytes = sg->length;
169 170 171 172 173 174 175 176 177 178

		BUG_ON(bytes != PAGE_SIZE);

		err = ioremap_page(va,  pa, mtype);
		if (err)
			goto err_out;

		va += bytes;
	}

179 180
	flush_cache_vmap((unsigned long)new->addr,
				(unsigned long)(new->addr + total));
181 182 183 184 185 186 187 188 189 190 191 192 193
	return new->addr;

err_out:
	WARN_ON(1); /* FIXME: cleanup some mpu mappings */
	vunmap(new->addr);
	return ERR_PTR(-EAGAIN);
}

static inline void vunmap_sg(const void *va)
{
	vunmap(va);
}

194 195
static struct iovm_struct *__find_iovm_area(struct omap_iommu *obj,
							const u32 da)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
{
	struct iovm_struct *tmp;

	list_for_each_entry(tmp, &obj->mmap, list) {
		if ((da >= tmp->da_start) && (da < tmp->da_end)) {
			size_t len;

			len = tmp->da_end - tmp->da_start;

			dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n",
				__func__, tmp->da_start, da, tmp->da_end, len,
				tmp->flags);

			return tmp;
		}
	}

	return NULL;
}

/**
217
 * omap_find_iovm_area  -  find iovma which includes @da
218 219 220 221
 * @da:		iommu device virtual address
 *
 * Find the existing iovma starting at @da
 */
222
struct iovm_struct *omap_find_iovm_area(struct omap_iommu *obj, u32 da)
223 224 225 226 227 228 229 230 231
{
	struct iovm_struct *area;

	mutex_lock(&obj->mmap_lock);
	area = __find_iovm_area(obj, da);
	mutex_unlock(&obj->mmap_lock);

	return area;
}
232
EXPORT_SYMBOL_GPL(omap_find_iovm_area);
233 234 235 236 237

/*
 * This finds the hole(area) which fits the requested address and len
 * in iovmas mmap, and returns the new allocated iovma.
 */
238
static struct iovm_struct *alloc_iovm_area(struct omap_iommu *obj, u32 da,
239 240 241
					   size_t bytes, u32 flags)
{
	struct iovm_struct *new, *tmp;
242
	u32 start, prev_end, alignment;
243 244 245 246 247

	if (!obj || !bytes)
		return ERR_PTR(-EINVAL);

	start = da;
248
	alignment = PAGE_SIZE;
249

250
	if (~flags & IOVMF_DA_FIXED) {
251 252
		/* Don't map address 0 */
		start = obj->da_start ? obj->da_start : alignment;
253

254
		if (flags & IOVMF_LINEAR)
255 256
			alignment = iopgsz_max(bytes);
		start = roundup(start, alignment);
257 258 259
	} else if (start < obj->da_start || start > obj->da_end ||
					obj->da_end - start < bytes) {
		return ERR_PTR(-EINVAL);
260 261 262 263 264 265 266 267 268
	}

	tmp = NULL;
	if (list_empty(&obj->mmap))
		goto found;

	prev_end = 0;
	list_for_each_entry(tmp, &obj->mmap, list) {

269
		if (prev_end > start)
270 271
			break;

272
		if (tmp->da_start > start && (tmp->da_start - start) >= bytes)
273 274
			goto found;

275
		if (tmp->da_end >= start && ~flags & IOVMF_DA_FIXED)
276
			start = roundup(tmp->da_end + 1, alignment);
277 278 279 280

		prev_end = tmp->da_end;
	}

281
	if ((start >= prev_end) && (obj->da_end - start >= bytes))
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		goto found;

	dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
		__func__, da, bytes, flags);

	return ERR_PTR(-EINVAL);

found:
	new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
	if (!new)
		return ERR_PTR(-ENOMEM);

	new->iommu = obj;
	new->da_start = start;
	new->da_end = start + bytes;
	new->flags = flags;

	/*
	 * keep ascending order of iovmas
	 */
	if (tmp)
		list_add_tail(&new->list, &tmp->list);
	else
		list_add(&new->list, &obj->mmap);

	dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
		__func__, new->da_start, start, new->da_end, bytes, flags);

	return new;
}

313
static void free_iovm_area(struct omap_iommu *obj, struct iovm_struct *area)
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
{
	size_t bytes;

	BUG_ON(!obj || !area);

	bytes = area->da_end - area->da_start;

	dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
		__func__, area->da_start, area->da_end, bytes, area->flags);

	list_del(&area->list);
	kmem_cache_free(iovm_area_cachep, area);
}

/**
329
 * omap_da_to_va - convert (d) to (v)
330 331 332 333 334 335
 * @obj:	objective iommu
 * @da:		iommu device virtual address
 * @va:		mpu virtual address
 *
 * Returns mpu virtual addr which corresponds to a given device virtual addr
 */
336
void *omap_da_to_va(struct omap_iommu *obj, u32 da)
337 338 339 340 341 342 343 344 345 346 347 348 349
{
	void *va = NULL;
	struct iovm_struct *area;

	mutex_lock(&obj->mmap_lock);

	area = __find_iovm_area(obj, da);
	if (!area) {
		dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
		goto out;
	}
	va = area->va;
out:
350 351
	mutex_unlock(&obj->mmap_lock);

352 353
	return va;
}
354
EXPORT_SYMBOL_GPL(omap_da_to_va);
355 356 357 358 359 360 361 362 363 364 365 366 367

static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
{
	unsigned int i;
	struct scatterlist *sg;
	void *va = _va;
	void *va_end;

	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
		struct page *pg;
		const size_t bytes = PAGE_SIZE;

		/*
368
		 * iommu 'superpage' isn't supported with 'omap_iommu_vmalloc()'
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
		 */
		pg = vmalloc_to_page(va);
		BUG_ON(!pg);
		sg_set_page(sg, pg, bytes, 0);

		va += bytes;
	}

	va_end = _va + PAGE_SIZE * i;
}

static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
{
	/*
	 * Actually this is not necessary at all, just exists for
H
Hiroshi DOYU 已提交
384
	 * consistency of the code readability.
385 386 387 388 389
	 */
	BUG_ON(!sgt);
}

/* create 'da' <-> 'pa' mapping from 'sgt' */
390 391
static int map_iovm_area(struct iommu_domain *domain, struct iovm_struct *new,
			const struct sg_table *sgt, u32 flags)
392 393 394 395 396
{
	int err;
	unsigned int i, j;
	struct scatterlist *sg;
	u32 da = new->da_start;
397
	int order;
398

399
	if (!domain || !sgt)
400 401 402 403 404 405 406 407 408
		return -EINVAL;

	BUG_ON(!sgtable_ok(sgt));

	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
		u32 pa;
		size_t bytes;

		pa = sg_phys(sg);
409
		bytes = sg->length;
410 411

		flags &= ~IOVMF_PGSZ_MASK;
412 413

		if (bytes_to_iopgsz(bytes) < 0)
414
			goto err_out;
415 416

		order = get_order(bytes);
417 418 419 420

		pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
			 i, da, pa, bytes);

421
		err = iommu_map(domain, da, pa, order, flags);
422 423 424 425 426 427 428 429 430 431 432 433 434
		if (err)
			goto err_out;

		da += bytes;
	}
	return 0;

err_out:
	da = new->da_start;

	for_each_sg(sgt->sgl, sg, i, j) {
		size_t bytes;

435 436
		bytes = sg->length;
		order = get_order(bytes);
437

438 439
		/* ignore failures.. we're already handling one */
		iommu_unmap(domain, da, order);
440 441 442 443 444 445 446

		da += bytes;
	}
	return err;
}

/* release 'da' <-> 'pa' mapping */
447
static void unmap_iovm_area(struct iommu_domain *domain, struct omap_iommu *obj,
448
						struct iovm_struct *area)
449 450 451
{
	u32 start;
	size_t total = area->da_end - area->da_start;
452 453 454
	const struct sg_table *sgt = area->sgt;
	struct scatterlist *sg;
	int i, err;
455

456
	BUG_ON(!sgtable_ok(sgt));
457 458 459
	BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));

	start = area->da_start;
460
	for_each_sg(sgt->sgl, sg, sgt->nents, i) {
461
		size_t bytes;
462 463 464 465 466 467 468 469
		int order;

		bytes = sg->length;
		order = get_order(bytes);

		err = iommu_unmap(domain, start, order);
		if (err)
			break;
470

471
		dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
472 473 474 475 476 477 478 479 480 481 482
				__func__, start, bytes, area->flags);

		BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));

		total -= bytes;
		start += bytes;
	}
	BUG_ON(total);
}

/* template function for all unmapping */
483
static struct sg_table *unmap_vm_area(struct iommu_domain *domain,
484
				      struct omap_iommu *obj, const u32 da,
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
				      void (*fn)(const void *), u32 flags)
{
	struct sg_table *sgt = NULL;
	struct iovm_struct *area;

	if (!IS_ALIGNED(da, PAGE_SIZE)) {
		dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
		return NULL;
	}

	mutex_lock(&obj->mmap_lock);

	area = __find_iovm_area(obj, da);
	if (!area) {
		dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
		goto out;
	}

	if ((area->flags & flags) != flags) {
		dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
			area->flags);
		goto out;
	}
	sgt = (struct sg_table *)area->sgt;

510
	unmap_iovm_area(domain, obj, area);
511 512 513 514 515 516 517 518 519 520 521 522 523 524

	fn(area->va);

	dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
		area->da_start, da, area->da_end,
		area->da_end - area->da_start, area->flags);

	free_iovm_area(obj, area);
out:
	mutex_unlock(&obj->mmap_lock);

	return sgt;
}

525
static u32 map_iommu_region(struct iommu_domain *domain, struct omap_iommu *obj,
526 527
				u32 da, const struct sg_table *sgt, void *va,
				size_t bytes, u32 flags)
528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	int err = -ENOMEM;
	struct iovm_struct *new;

	mutex_lock(&obj->mmap_lock);

	new = alloc_iovm_area(obj, da, bytes, flags);
	if (IS_ERR(new)) {
		err = PTR_ERR(new);
		goto err_alloc_iovma;
	}
	new->va = va;
	new->sgt = sgt;

542
	if (map_iovm_area(domain, new, sgt, new->flags))
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		goto err_map;

	mutex_unlock(&obj->mmap_lock);

	dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
		__func__, new->da_start, bytes, new->flags, va);

	return new->da_start;

err_map:
	free_iovm_area(obj, new);
err_alloc_iovma:
	mutex_unlock(&obj->mmap_lock);
	return err;
}

559 560
static inline u32
__iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj,
561 562
				u32 da, const struct sg_table *sgt,
				void *va, size_t bytes, u32 flags)
563
{
564
	return map_iommu_region(domain, obj, da, sgt, va, bytes, flags);
565 566 567
}

/**
568
 * omap_iommu_vmap  -  (d)-(p)-(v) address mapper
569 570 571 572 573 574 575
 * @obj:	objective iommu
 * @sgt:	address of scatter gather table
 * @flags:	iovma and page property
 *
 * Creates 1-n-1 mapping with given @sgt and returns @da.
 * All @sgt element must be io page size aligned.
 */
576
u32 omap_iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj, u32 da,
577
		const struct sg_table *sgt, u32 flags)
578 579
{
	size_t bytes;
580
	void *va = NULL;
581 582 583 584 585 586 587 588 589

	if (!obj || !obj->dev || !sgt)
		return -EINVAL;

	bytes = sgtable_len(sgt);
	if (!bytes)
		return -EINVAL;
	bytes = PAGE_ALIGN(bytes);

590 591 592 593 594
	if (flags & IOVMF_MMIO) {
		va = vmap_sg(sgt);
		if (IS_ERR(va))
			return PTR_ERR(va);
	}
595 596 597 598

	flags |= IOVMF_DISCONT;
	flags |= IOVMF_MMIO;

599
	da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
600 601 602 603 604
	if (IS_ERR_VALUE(da))
		vunmap_sg(va);

	return da;
}
605
EXPORT_SYMBOL_GPL(omap_iommu_vmap);
606 607

/**
608
 * omap_iommu_vunmap  -  release virtual mapping obtained by 'omap_iommu_vmap()'
609 610 611 612
 * @obj:	objective iommu
 * @da:		iommu device virtual address
 *
 * Free the iommu virtually contiguous memory area starting at
613
 * @da, which was returned by 'omap_iommu_vmap()'.
614
 */
615
struct sg_table *
616
omap_iommu_vunmap(struct iommu_domain *domain, struct omap_iommu *obj, u32 da)
617 618 619
{
	struct sg_table *sgt;
	/*
620
	 * 'sgt' is allocated before 'omap_iommu_vmalloc()' is called.
621 622
	 * Just returns 'sgt' to the caller to free
	 */
623 624
	sgt = unmap_vm_area(domain, obj, da, vunmap_sg,
					IOVMF_DISCONT | IOVMF_MMIO);
625 626 627 628
	if (!sgt)
		dev_dbg(obj->dev, "%s: No sgt\n", __func__);
	return sgt;
}
629
EXPORT_SYMBOL_GPL(omap_iommu_vunmap);
630 631

/**
632
 * omap_iommu_vmalloc  -  (d)-(p)-(v) address allocator and mapper
633 634 635 636 637 638
 * @obj:	objective iommu
 * @da:		contiguous iommu virtual memory
 * @bytes:	allocation size
 * @flags:	iovma and page property
 *
 * Allocate @bytes linearly and creates 1-n-1 mapping and returns
639
 * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
640
 */
641 642
u32
omap_iommu_vmalloc(struct iommu_domain *domain, struct omap_iommu *obj, u32 da,
643
						size_t bytes, u32 flags)
644 645 646 647 648 649 650 651 652 653 654 655 656
{
	void *va;
	struct sg_table *sgt;

	if (!obj || !obj->dev || !bytes)
		return -EINVAL;

	bytes = PAGE_ALIGN(bytes);

	va = vmalloc(bytes);
	if (!va)
		return -ENOMEM;

657 658 659 660
	flags |= IOVMF_DISCONT;
	flags |= IOVMF_ALLOC;

	sgt = sgtable_alloc(bytes, flags, da, 0);
661 662 663 664 665 666
	if (IS_ERR(sgt)) {
		da = PTR_ERR(sgt);
		goto err_sgt_alloc;
	}
	sgtable_fill_vmalloc(sgt, va);

667
	da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
668 669 670 671 672 673 674 675 676 677 678 679
	if (IS_ERR_VALUE(da))
		goto err_iommu_vmap;

	return da;

err_iommu_vmap:
	sgtable_drain_vmalloc(sgt);
	sgtable_free(sgt);
err_sgt_alloc:
	vfree(va);
	return da;
}
680
EXPORT_SYMBOL_GPL(omap_iommu_vmalloc);
681 682

/**
683
 * omap_iommu_vfree  -  release memory allocated by 'omap_iommu_vmalloc()'
684 685 686 687
 * @obj:	objective iommu
 * @da:		iommu device virtual address
 *
 * Frees the iommu virtually continuous memory area starting at
688
 * @da, as obtained from 'omap_iommu_vmalloc()'.
689
 */
690 691
void omap_iommu_vfree(struct iommu_domain *domain, struct omap_iommu *obj,
								const u32 da)
692 693 694
{
	struct sg_table *sgt;

695 696
	sgt = unmap_vm_area(domain, obj, da, vfree,
						IOVMF_DISCONT | IOVMF_ALLOC);
697 698 699 700
	if (!sgt)
		dev_dbg(obj->dev, "%s: No sgt\n", __func__);
	sgtable_free(sgt);
}
701
EXPORT_SYMBOL_GPL(omap_iommu_vfree);
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

static int __init iovmm_init(void)
{
	const unsigned long flags = SLAB_HWCACHE_ALIGN;
	struct kmem_cache *p;

	p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
			      flags, NULL);
	if (!p)
		return -ENOMEM;
	iovm_area_cachep = p;

	return 0;
}
module_init(iovmm_init);

static void __exit iovmm_exit(void)
{
	kmem_cache_destroy(iovm_area_cachep);
}
module_exit(iovmm_exit);

MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
MODULE_LICENSE("GPL v2");