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