blob: e43983ba59c5eb77ac61caa3a2ebaca83b50416c [file] [log] [blame]
Hiroshi DOYU69d3a842009-01-28 21:32:08 +02001/*
2 * omap iommu: simple virtual address space management
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020015#include <linux/vmalloc.h>
16#include <linux/device.h>
17#include <linux/scatterlist.h>
18
19#include <asm/cacheflush.h>
20#include <asm/mach/map.h>
21
Tony Lindgrence491cf2009-10-20 09:40:47 -070022#include <plat/iommu.h>
23#include <plat/iovmm.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020024
25#include "iopgtable.h"
26
27/*
28 * A device driver needs to create address mappings between:
29 *
30 * - iommu/device address
31 * - physical address
32 * - mpu virtual address
33 *
34 * There are 4 possible patterns for them:
35 *
36 * |iova/ mapping iommu_ page
37 * | da pa va (d)-(p)-(v) function type
38 * ---------------------------------------------------------------------------
39 * 1 | c c c 1 - 1 - 1 _kmap() / _kunmap() s
40 * 2 | c c,a c 1 - 1 - 1 _kmalloc()/ _kfree() s
41 * 3 | c d c 1 - n - 1 _vmap() / _vunmap() s
42 * 4 | c d,a c 1 - n - 1 _vmalloc()/ _vfree() n*
43 *
44 *
45 * 'iova': device iommu virtual address
46 * 'da': alias of 'iova'
47 * 'pa': physical address
48 * 'va': mpu virtual address
49 *
50 * 'c': contiguous memory area
Hiroshi DOYUba6a1172009-10-05 13:31:45 -070051 * 'd': discontiguous memory area
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020052 * 'a': anonymous memory allocation
53 * '()': optional feature
54 *
55 * 'n': a normal page(4KB) size is used.
56 * 's': multiple iommu superpage(16MB, 1MB, 64KB, 4KB) size is used.
57 *
58 * '*': not yet, but feasible.
59 */
60
61static struct kmem_cache *iovm_area_cachep;
62
63/* return total bytes of sg buffers */
64static size_t sgtable_len(const struct sg_table *sgt)
65{
66 unsigned int i, total = 0;
67 struct scatterlist *sg;
68
69 if (!sgt)
70 return 0;
71
72 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
73 size_t bytes;
74
75 bytes = sg_dma_len(sg);
76
77 if (!iopgsz_ok(bytes)) {
78 pr_err("%s: sg[%d] not iommu pagesize(%x)\n",
79 __func__, i, bytes);
80 return 0;
81 }
82
83 total += bytes;
84 }
85
86 return total;
87}
88#define sgtable_ok(x) (!!sgtable_len(x))
89
90/*
91 * calculate the optimal number sg elements from total bytes based on
92 * iommu superpages
93 */
94static unsigned int sgtable_nents(size_t bytes)
95{
96 int i;
97 unsigned int nr_entries;
98 const unsigned long pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, };
99
100 if (!IS_ALIGNED(bytes, PAGE_SIZE)) {
101 pr_err("%s: wrong size %08x\n", __func__, bytes);
102 return 0;
103 }
104
105 nr_entries = 0;
106 for (i = 0; i < ARRAY_SIZE(pagesize); i++) {
107 if (bytes >= pagesize[i]) {
108 nr_entries += (bytes / pagesize[i]);
109 bytes %= pagesize[i];
110 }
111 }
112 BUG_ON(bytes);
113
114 return nr_entries;
115}
116
117/* allocate and initialize sg_table header(a kind of 'superblock') */
118static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags)
119{
120 unsigned int nr_entries;
121 int err;
122 struct sg_table *sgt;
123
124 if (!bytes)
125 return ERR_PTR(-EINVAL);
126
127 if (!IS_ALIGNED(bytes, PAGE_SIZE))
128 return ERR_PTR(-EINVAL);
129
130 /* FIXME: IOVMF_DA_FIXED should support 'superpages' */
131 if ((flags & IOVMF_LINEAR) && (flags & IOVMF_DA_ANON)) {
132 nr_entries = sgtable_nents(bytes);
133 if (!nr_entries)
134 return ERR_PTR(-EINVAL);
135 } else
136 nr_entries = bytes / PAGE_SIZE;
137
138 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
139 if (!sgt)
140 return ERR_PTR(-ENOMEM);
141
142 err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL);
143 if (err)
144 return ERR_PTR(err);
145
146 pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries);
147
148 return sgt;
149}
150
151/* free sg_table header(a kind of superblock) */
152static void sgtable_free(struct sg_table *sgt)
153{
154 if (!sgt)
155 return;
156
157 sg_free_table(sgt);
158 kfree(sgt);
159
160 pr_debug("%s: sgt:%p\n", __func__, sgt);
161}
162
163/* map 'sglist' to a contiguous mpu virtual area and return 'va' */
164static void *vmap_sg(const struct sg_table *sgt)
165{
166 u32 va;
167 size_t total;
168 unsigned int i;
169 struct scatterlist *sg;
170 struct vm_struct *new;
171 const struct mem_type *mtype;
172
173 mtype = get_mem_type(MT_DEVICE);
174 if (!mtype)
175 return ERR_PTR(-EINVAL);
176
177 total = sgtable_len(sgt);
178 if (!total)
179 return ERR_PTR(-EINVAL);
180
181 new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
182 if (!new)
183 return ERR_PTR(-ENOMEM);
184 va = (u32)new->addr;
185
186 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
187 size_t bytes;
188 u32 pa;
189 int err;
190
191 pa = sg_phys(sg);
192 bytes = sg_dma_len(sg);
193
194 BUG_ON(bytes != PAGE_SIZE);
195
196 err = ioremap_page(va, pa, mtype);
197 if (err)
198 goto err_out;
199
200 va += bytes;
201 }
202
Sanjeev Premi6716bd02009-09-24 16:23:12 -0700203 flush_cache_vmap((unsigned long)new->addr,
204 (unsigned long)(new->addr + total));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200205 return new->addr;
206
207err_out:
208 WARN_ON(1); /* FIXME: cleanup some mpu mappings */
209 vunmap(new->addr);
210 return ERR_PTR(-EAGAIN);
211}
212
213static inline void vunmap_sg(const void *va)
214{
215 vunmap(va);
216}
217
218static struct iovm_struct *__find_iovm_area(struct iommu *obj, const u32 da)
219{
220 struct iovm_struct *tmp;
221
222 list_for_each_entry(tmp, &obj->mmap, list) {
223 if ((da >= tmp->da_start) && (da < tmp->da_end)) {
224 size_t len;
225
226 len = tmp->da_end - tmp->da_start;
227
228 dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n",
229 __func__, tmp->da_start, da, tmp->da_end, len,
230 tmp->flags);
231
232 return tmp;
233 }
234 }
235
236 return NULL;
237}
238
239/**
240 * find_iovm_area - find iovma which includes @da
241 * @da: iommu device virtual address
242 *
243 * Find the existing iovma starting at @da
244 */
245struct iovm_struct *find_iovm_area(struct iommu *obj, u32 da)
246{
247 struct iovm_struct *area;
248
249 mutex_lock(&obj->mmap_lock);
250 area = __find_iovm_area(obj, da);
251 mutex_unlock(&obj->mmap_lock);
252
253 return area;
254}
255EXPORT_SYMBOL_GPL(find_iovm_area);
256
257/*
258 * This finds the hole(area) which fits the requested address and len
259 * in iovmas mmap, and returns the new allocated iovma.
260 */
261static struct iovm_struct *alloc_iovm_area(struct iommu *obj, u32 da,
262 size_t bytes, u32 flags)
263{
264 struct iovm_struct *new, *tmp;
265 u32 start, prev_end, alignement;
266
267 if (!obj || !bytes)
268 return ERR_PTR(-EINVAL);
269
270 start = da;
271 alignement = PAGE_SIZE;
272
273 if (flags & IOVMF_DA_ANON) {
274 /*
275 * Reserve the first page for NULL
276 */
277 start = PAGE_SIZE;
278 if (flags & IOVMF_LINEAR)
279 alignement = iopgsz_max(bytes);
280 start = roundup(start, alignement);
281 }
282
283 tmp = NULL;
284 if (list_empty(&obj->mmap))
285 goto found;
286
287 prev_end = 0;
288 list_for_each_entry(tmp, &obj->mmap, list) {
289
Hiroshi DOYUe0a42e42010-05-06 17:09:25 +0300290 if (prev_end >= start)
291 break;
292
293 if (start + bytes < tmp->da_start)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200294 goto found;
295
296 if (flags & IOVMF_DA_ANON)
Hiroshi DOYUfa460b82010-05-06 16:10:18 +0300297 start = roundup(tmp->da_end + 1, alignement);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200298
299 prev_end = tmp->da_end;
300 }
301
Hiroshi DOYUfa460b82010-05-06 16:10:18 +0300302 if ((start > prev_end) && (ULONG_MAX - start >= bytes))
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200303 goto found;
304
305 dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
306 __func__, da, bytes, flags);
307
308 return ERR_PTR(-EINVAL);
309
310found:
311 new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
312 if (!new)
313 return ERR_PTR(-ENOMEM);
314
315 new->iommu = obj;
316 new->da_start = start;
317 new->da_end = start + bytes;
318 new->flags = flags;
319
320 /*
321 * keep ascending order of iovmas
322 */
323 if (tmp)
324 list_add_tail(&new->list, &tmp->list);
325 else
326 list_add(&new->list, &obj->mmap);
327
328 dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
329 __func__, new->da_start, start, new->da_end, bytes, flags);
330
331 return new;
332}
333
334static void free_iovm_area(struct iommu *obj, struct iovm_struct *area)
335{
336 size_t bytes;
337
338 BUG_ON(!obj || !area);
339
340 bytes = area->da_end - area->da_start;
341
342 dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
343 __func__, area->da_start, area->da_end, bytes, area->flags);
344
345 list_del(&area->list);
346 kmem_cache_free(iovm_area_cachep, area);
347}
348
349/**
350 * da_to_va - convert (d) to (v)
351 * @obj: objective iommu
352 * @da: iommu device virtual address
353 * @va: mpu virtual address
354 *
355 * Returns mpu virtual addr which corresponds to a given device virtual addr
356 */
357void *da_to_va(struct iommu *obj, u32 da)
358{
359 void *va = NULL;
360 struct iovm_struct *area;
361
362 mutex_lock(&obj->mmap_lock);
363
364 area = __find_iovm_area(obj, da);
365 if (!area) {
366 dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
367 goto out;
368 }
369 va = area->va;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200370out:
Daniel Walker26548902009-10-05 13:31:45 -0700371 mutex_unlock(&obj->mmap_lock);
372
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200373 return va;
374}
375EXPORT_SYMBOL_GPL(da_to_va);
376
377static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
378{
379 unsigned int i;
380 struct scatterlist *sg;
381 void *va = _va;
382 void *va_end;
383
384 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
385 struct page *pg;
386 const size_t bytes = PAGE_SIZE;
387
388 /*
389 * iommu 'superpage' isn't supported with 'iommu_vmalloc()'
390 */
391 pg = vmalloc_to_page(va);
392 BUG_ON(!pg);
393 sg_set_page(sg, pg, bytes, 0);
394
395 va += bytes;
396 }
397
398 va_end = _va + PAGE_SIZE * i;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200399}
400
401static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
402{
403 /*
404 * Actually this is not necessary at all, just exists for
Hiroshi DOYUba6a1172009-10-05 13:31:45 -0700405 * consistency of the code readability.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200406 */
407 BUG_ON(!sgt);
408}
409
410static void sgtable_fill_kmalloc(struct sg_table *sgt, u32 pa, size_t len)
411{
412 unsigned int i;
413 struct scatterlist *sg;
414 void *va;
415
416 va = phys_to_virt(pa);
417
418 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
419 size_t bytes;
420
421 bytes = iopgsz_max(len);
422
423 BUG_ON(!iopgsz_ok(bytes));
424
425 sg_set_buf(sg, phys_to_virt(pa), bytes);
426 /*
427 * 'pa' is cotinuous(linear).
428 */
429 pa += bytes;
430 len -= bytes;
431 }
432 BUG_ON(len);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200433}
434
435static inline void sgtable_drain_kmalloc(struct sg_table *sgt)
436{
437 /*
438 * Actually this is not necessary at all, just exists for
Hiroshi DOYUba6a1172009-10-05 13:31:45 -0700439 * consistency of the code readability
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200440 */
441 BUG_ON(!sgt);
442}
443
444/* create 'da' <-> 'pa' mapping from 'sgt' */
445static int map_iovm_area(struct iommu *obj, struct iovm_struct *new,
446 const struct sg_table *sgt, u32 flags)
447{
448 int err;
449 unsigned int i, j;
450 struct scatterlist *sg;
451 u32 da = new->da_start;
452
Julia Lawall20e11c22009-11-22 10:11:16 -0800453 if (!obj || !sgt)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200454 return -EINVAL;
455
456 BUG_ON(!sgtable_ok(sgt));
457
458 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
459 u32 pa;
460 int pgsz;
461 size_t bytes;
462 struct iotlb_entry e;
463
464 pa = sg_phys(sg);
465 bytes = sg_dma_len(sg);
466
467 flags &= ~IOVMF_PGSZ_MASK;
468 pgsz = bytes_to_iopgsz(bytes);
469 if (pgsz < 0)
470 goto err_out;
471 flags |= pgsz;
472
473 pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
474 i, da, pa, bytes);
475
476 iotlb_init_entry(&e, da, pa, flags);
477 err = iopgtable_store_entry(obj, &e);
478 if (err)
479 goto err_out;
480
481 da += bytes;
482 }
483 return 0;
484
485err_out:
486 da = new->da_start;
487
488 for_each_sg(sgt->sgl, sg, i, j) {
489 size_t bytes;
490
491 bytes = iopgtable_clear_entry(obj, da);
492
493 BUG_ON(!iopgsz_ok(bytes));
494
495 da += bytes;
496 }
497 return err;
498}
499
500/* release 'da' <-> 'pa' mapping */
501static void unmap_iovm_area(struct iommu *obj, struct iovm_struct *area)
502{
503 u32 start;
504 size_t total = area->da_end - area->da_start;
505
506 BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));
507
508 start = area->da_start;
509 while (total > 0) {
510 size_t bytes;
511
512 bytes = iopgtable_clear_entry(obj, start);
513 if (bytes == 0)
514 bytes = PAGE_SIZE;
515 else
516 dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
517 __func__, start, bytes, area->flags);
518
519 BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));
520
521 total -= bytes;
522 start += bytes;
523 }
524 BUG_ON(total);
525}
526
527/* template function for all unmapping */
528static struct sg_table *unmap_vm_area(struct iommu *obj, const u32 da,
529 void (*fn)(const void *), u32 flags)
530{
531 struct sg_table *sgt = NULL;
532 struct iovm_struct *area;
533
534 if (!IS_ALIGNED(da, PAGE_SIZE)) {
535 dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
536 return NULL;
537 }
538
539 mutex_lock(&obj->mmap_lock);
540
541 area = __find_iovm_area(obj, da);
542 if (!area) {
543 dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
544 goto out;
545 }
546
547 if ((area->flags & flags) != flags) {
548 dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
549 area->flags);
550 goto out;
551 }
552 sgt = (struct sg_table *)area->sgt;
553
554 unmap_iovm_area(obj, area);
555
556 fn(area->va);
557
558 dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
559 area->da_start, da, area->da_end,
560 area->da_end - area->da_start, area->flags);
561
562 free_iovm_area(obj, area);
563out:
564 mutex_unlock(&obj->mmap_lock);
565
566 return sgt;
567}
568
569static u32 map_iommu_region(struct iommu *obj, u32 da,
570 const struct sg_table *sgt, void *va, size_t bytes, u32 flags)
571{
572 int err = -ENOMEM;
573 struct iovm_struct *new;
574
575 mutex_lock(&obj->mmap_lock);
576
577 new = alloc_iovm_area(obj, da, bytes, flags);
578 if (IS_ERR(new)) {
579 err = PTR_ERR(new);
580 goto err_alloc_iovma;
581 }
582 new->va = va;
583 new->sgt = sgt;
584
585 if (map_iovm_area(obj, new, sgt, new->flags))
586 goto err_map;
587
588 mutex_unlock(&obj->mmap_lock);
589
590 dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
591 __func__, new->da_start, bytes, new->flags, va);
592
593 return new->da_start;
594
595err_map:
596 free_iovm_area(obj, new);
597err_alloc_iovma:
598 mutex_unlock(&obj->mmap_lock);
599 return err;
600}
601
602static inline u32 __iommu_vmap(struct iommu *obj, u32 da,
603 const struct sg_table *sgt, void *va, size_t bytes, u32 flags)
604{
605 return map_iommu_region(obj, da, sgt, va, bytes, flags);
606}
607
608/**
609 * iommu_vmap - (d)-(p)-(v) address mapper
610 * @obj: objective iommu
611 * @sgt: address of scatter gather table
612 * @flags: iovma and page property
613 *
614 * Creates 1-n-1 mapping with given @sgt and returns @da.
615 * All @sgt element must be io page size aligned.
616 */
617u32 iommu_vmap(struct iommu *obj, u32 da, const struct sg_table *sgt,
618 u32 flags)
619{
620 size_t bytes;
Hiroshi DOYU935e4732009-11-22 10:11:02 -0800621 void *va = NULL;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200622
623 if (!obj || !obj->dev || !sgt)
624 return -EINVAL;
625
626 bytes = sgtable_len(sgt);
627 if (!bytes)
628 return -EINVAL;
629 bytes = PAGE_ALIGN(bytes);
630
Hiroshi DOYU935e4732009-11-22 10:11:02 -0800631 if (flags & IOVMF_MMIO) {
632 va = vmap_sg(sgt);
633 if (IS_ERR(va))
634 return PTR_ERR(va);
635 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200636
637 flags &= IOVMF_HW_MASK;
638 flags |= IOVMF_DISCONT;
639 flags |= IOVMF_MMIO;
640 flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
641
642 da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
643 if (IS_ERR_VALUE(da))
644 vunmap_sg(va);
645
646 return da;
647}
648EXPORT_SYMBOL_GPL(iommu_vmap);
649
650/**
651 * iommu_vunmap - release virtual mapping obtained by 'iommu_vmap()'
652 * @obj: objective iommu
653 * @da: iommu device virtual address
654 *
655 * Free the iommu virtually contiguous memory area starting at
656 * @da, which was returned by 'iommu_vmap()'.
657 */
658struct sg_table *iommu_vunmap(struct iommu *obj, u32 da)
659{
660 struct sg_table *sgt;
661 /*
662 * 'sgt' is allocated before 'iommu_vmalloc()' is called.
663 * Just returns 'sgt' to the caller to free
664 */
665 sgt = unmap_vm_area(obj, da, vunmap_sg, IOVMF_DISCONT | IOVMF_MMIO);
666 if (!sgt)
667 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
668 return sgt;
669}
670EXPORT_SYMBOL_GPL(iommu_vunmap);
671
672/**
673 * iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
674 * @obj: objective iommu
675 * @da: contiguous iommu virtual memory
676 * @bytes: allocation size
677 * @flags: iovma and page property
678 *
679 * Allocate @bytes linearly and creates 1-n-1 mapping and returns
680 * @da again, which might be adjusted if 'IOVMF_DA_ANON' is set.
681 */
682u32 iommu_vmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
683{
684 void *va;
685 struct sg_table *sgt;
686
687 if (!obj || !obj->dev || !bytes)
688 return -EINVAL;
689
690 bytes = PAGE_ALIGN(bytes);
691
692 va = vmalloc(bytes);
693 if (!va)
694 return -ENOMEM;
695
696 sgt = sgtable_alloc(bytes, flags);
697 if (IS_ERR(sgt)) {
698 da = PTR_ERR(sgt);
699 goto err_sgt_alloc;
700 }
701 sgtable_fill_vmalloc(sgt, va);
702
703 flags &= IOVMF_HW_MASK;
704 flags |= IOVMF_DISCONT;
705 flags |= IOVMF_ALLOC;
706 flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
707
708 da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
709 if (IS_ERR_VALUE(da))
710 goto err_iommu_vmap;
711
712 return da;
713
714err_iommu_vmap:
715 sgtable_drain_vmalloc(sgt);
716 sgtable_free(sgt);
717err_sgt_alloc:
718 vfree(va);
719 return da;
720}
721EXPORT_SYMBOL_GPL(iommu_vmalloc);
722
723/**
724 * iommu_vfree - release memory allocated by 'iommu_vmalloc()'
725 * @obj: objective iommu
726 * @da: iommu device virtual address
727 *
728 * Frees the iommu virtually continuous memory area starting at
729 * @da, as obtained from 'iommu_vmalloc()'.
730 */
731void iommu_vfree(struct iommu *obj, const u32 da)
732{
733 struct sg_table *sgt;
734
735 sgt = unmap_vm_area(obj, da, vfree, IOVMF_DISCONT | IOVMF_ALLOC);
736 if (!sgt)
737 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
738 sgtable_free(sgt);
739}
740EXPORT_SYMBOL_GPL(iommu_vfree);
741
742static u32 __iommu_kmap(struct iommu *obj, u32 da, u32 pa, void *va,
743 size_t bytes, u32 flags)
744{
745 struct sg_table *sgt;
746
747 sgt = sgtable_alloc(bytes, flags);
748 if (IS_ERR(sgt))
749 return PTR_ERR(sgt);
750
751 sgtable_fill_kmalloc(sgt, pa, bytes);
752
753 da = map_iommu_region(obj, da, sgt, va, bytes, flags);
754 if (IS_ERR_VALUE(da)) {
755 sgtable_drain_kmalloc(sgt);
756 sgtable_free(sgt);
757 }
758
759 return da;
760}
761
762/**
763 * iommu_kmap - (d)-(p)-(v) address mapper
764 * @obj: objective iommu
765 * @da: contiguous iommu virtual memory
766 * @pa: contiguous physical memory
767 * @flags: iovma and page property
768 *
769 * Creates 1-1-1 mapping and returns @da again, which can be
770 * adjusted if 'IOVMF_DA_ANON' is set.
771 */
772u32 iommu_kmap(struct iommu *obj, u32 da, u32 pa, size_t bytes,
773 u32 flags)
774{
775 void *va;
776
777 if (!obj || !obj->dev || !bytes)
778 return -EINVAL;
779
780 bytes = PAGE_ALIGN(bytes);
781
782 va = ioremap(pa, bytes);
783 if (!va)
784 return -ENOMEM;
785
786 flags &= IOVMF_HW_MASK;
787 flags |= IOVMF_LINEAR;
788 flags |= IOVMF_MMIO;
789 flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
790
791 da = __iommu_kmap(obj, da, pa, va, bytes, flags);
792 if (IS_ERR_VALUE(da))
793 iounmap(va);
794
795 return da;
796}
797EXPORT_SYMBOL_GPL(iommu_kmap);
798
799/**
800 * iommu_kunmap - release virtual mapping obtained by 'iommu_kmap()'
801 * @obj: objective iommu
802 * @da: iommu device virtual address
803 *
804 * Frees the iommu virtually contiguous memory area starting at
805 * @da, which was passed to and was returned by'iommu_kmap()'.
806 */
807void iommu_kunmap(struct iommu *obj, u32 da)
808{
809 struct sg_table *sgt;
810 typedef void (*func_t)(const void *);
811
812 sgt = unmap_vm_area(obj, da, (func_t)__iounmap,
813 IOVMF_LINEAR | IOVMF_MMIO);
814 if (!sgt)
815 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
816 sgtable_free(sgt);
817}
818EXPORT_SYMBOL_GPL(iommu_kunmap);
819
820/**
821 * iommu_kmalloc - (d)-(p)-(v) address allocator and mapper
822 * @obj: objective iommu
823 * @da: contiguous iommu virtual memory
824 * @bytes: bytes for allocation
825 * @flags: iovma and page property
826 *
827 * Allocate @bytes linearly and creates 1-1-1 mapping and returns
828 * @da again, which might be adjusted if 'IOVMF_DA_ANON' is set.
829 */
830u32 iommu_kmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
831{
832 void *va;
833 u32 pa;
834
835 if (!obj || !obj->dev || !bytes)
836 return -EINVAL;
837
838 bytes = PAGE_ALIGN(bytes);
839
840 va = kmalloc(bytes, GFP_KERNEL | GFP_DMA);
841 if (!va)
842 return -ENOMEM;
843 pa = virt_to_phys(va);
844
845 flags &= IOVMF_HW_MASK;
846 flags |= IOVMF_LINEAR;
847 flags |= IOVMF_ALLOC;
848 flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
849
850 da = __iommu_kmap(obj, da, pa, va, bytes, flags);
851 if (IS_ERR_VALUE(da))
852 kfree(va);
853
854 return da;
855}
856EXPORT_SYMBOL_GPL(iommu_kmalloc);
857
858/**
859 * iommu_kfree - release virtual mapping obtained by 'iommu_kmalloc()'
860 * @obj: objective iommu
861 * @da: iommu device virtual address
862 *
863 * Frees the iommu virtually contiguous memory area starting at
864 * @da, which was passed to and was returned by'iommu_kmalloc()'.
865 */
866void iommu_kfree(struct iommu *obj, u32 da)
867{
868 struct sg_table *sgt;
869
870 sgt = unmap_vm_area(obj, da, kfree, IOVMF_LINEAR | IOVMF_ALLOC);
871 if (!sgt)
872 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
873 sgtable_free(sgt);
874}
875EXPORT_SYMBOL_GPL(iommu_kfree);
876
877
878static int __init iovmm_init(void)
879{
880 const unsigned long flags = SLAB_HWCACHE_ALIGN;
881 struct kmem_cache *p;
882
883 p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
884 flags, NULL);
885 if (!p)
886 return -ENOMEM;
887 iovm_area_cachep = p;
888
889 return 0;
890}
891module_init(iovmm_init);
892
893static void __exit iovmm_exit(void)
894{
895 kmem_cache_destroy(iovm_area_cachep);
896}
897module_exit(iovmm_exit);
898
899MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
900MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
901MODULE_LICENSE("GPL v2");