blob: aa2c47893b02f4b9545797e2151890213432b520 [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>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030018#include <linux/iommu.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020019
20#include <asm/cacheflush.h>
21#include <asm/mach/map.h>
22
Tony Lindgrence491cf2009-10-20 09:40:47 -070023#include <plat/iommu.h>
24#include <plat/iovmm.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020025
26#include "iopgtable.h"
27
28/*
29 * A device driver needs to create address mappings between:
30 *
31 * - iommu/device address
32 * - physical address
33 * - mpu virtual address
34 *
35 * There are 4 possible patterns for them:
36 *
37 * |iova/ mapping iommu_ page
38 * | da pa va (d)-(p)-(v) function type
39 * ---------------------------------------------------------------------------
40 * 1 | c c c 1 - 1 - 1 _kmap() / _kunmap() s
41 * 2 | c c,a c 1 - 1 - 1 _kmalloc()/ _kfree() s
42 * 3 | c d c 1 - n - 1 _vmap() / _vunmap() s
43 * 4 | c d,a c 1 - n - 1 _vmalloc()/ _vfree() n*
44 *
45 *
46 * 'iova': device iommu virtual address
47 * 'da': alias of 'iova'
48 * 'pa': physical address
49 * 'va': mpu virtual address
50 *
51 * 'c': contiguous memory area
Hiroshi DOYUba6a1172009-10-05 13:31:45 -070052 * 'd': discontiguous memory area
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020053 * 'a': anonymous memory allocation
54 * '()': optional feature
55 *
56 * 'n': a normal page(4KB) size is used.
57 * 's': multiple iommu superpage(16MB, 1MB, 64KB, 4KB) size is used.
58 *
59 * '*': not yet, but feasible.
60 */
61
62static struct kmem_cache *iovm_area_cachep;
63
64/* return total bytes of sg buffers */
65static size_t sgtable_len(const struct sg_table *sgt)
66{
67 unsigned int i, total = 0;
68 struct scatterlist *sg;
69
70 if (!sgt)
71 return 0;
72
73 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
74 size_t bytes;
75
Ohad Ben-Cohen66cf4022011-06-08 09:06:11 +030076 bytes = sg->length;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020077
78 if (!iopgsz_ok(bytes)) {
79 pr_err("%s: sg[%d] not iommu pagesize(%x)\n",
80 __func__, i, bytes);
81 return 0;
82 }
83
84 total += bytes;
85 }
86
87 return total;
88}
89#define sgtable_ok(x) (!!sgtable_len(x))
90
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +000091static unsigned max_alignment(u32 addr)
92{
93 int i;
94 unsigned pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, };
95 for (i = 0; i < ARRAY_SIZE(pagesize) && addr & (pagesize[i] - 1); i++)
96 ;
97 return (i < ARRAY_SIZE(pagesize)) ? pagesize[i] : 0;
98}
99
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200100/*
101 * calculate the optimal number sg elements from total bytes based on
102 * iommu superpages
103 */
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000104static unsigned sgtable_nents(size_t bytes, u32 da, u32 pa)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200105{
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000106 unsigned nr_entries = 0, ent_sz;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200107
108 if (!IS_ALIGNED(bytes, PAGE_SIZE)) {
109 pr_err("%s: wrong size %08x\n", __func__, bytes);
110 return 0;
111 }
112
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000113 while (bytes) {
114 ent_sz = max_alignment(da | pa);
115 ent_sz = min_t(unsigned, ent_sz, iopgsz_max(bytes));
116 nr_entries++;
117 da += ent_sz;
118 pa += ent_sz;
119 bytes -= ent_sz;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200120 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200121
122 return nr_entries;
123}
124
125/* allocate and initialize sg_table header(a kind of 'superblock') */
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000126static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags,
127 u32 da, u32 pa)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200128{
129 unsigned int nr_entries;
130 int err;
131 struct sg_table *sgt;
132
133 if (!bytes)
134 return ERR_PTR(-EINVAL);
135
136 if (!IS_ALIGNED(bytes, PAGE_SIZE))
137 return ERR_PTR(-EINVAL);
138
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000139 if (flags & IOVMF_LINEAR) {
140 nr_entries = sgtable_nents(bytes, da, pa);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200141 if (!nr_entries)
142 return ERR_PTR(-EINVAL);
143 } else
144 nr_entries = bytes / PAGE_SIZE;
145
146 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
147 if (!sgt)
148 return ERR_PTR(-ENOMEM);
149
150 err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL);
Satish7f1225b2010-06-09 13:21:27 +0300151 if (err) {
152 kfree(sgt);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200153 return ERR_PTR(err);
Satish7f1225b2010-06-09 13:21:27 +0300154 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200155
156 pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries);
157
158 return sgt;
159}
160
161/* free sg_table header(a kind of superblock) */
162static void sgtable_free(struct sg_table *sgt)
163{
164 if (!sgt)
165 return;
166
167 sg_free_table(sgt);
168 kfree(sgt);
169
170 pr_debug("%s: sgt:%p\n", __func__, sgt);
171}
172
173/* map 'sglist' to a contiguous mpu virtual area and return 'va' */
174static void *vmap_sg(const struct sg_table *sgt)
175{
176 u32 va;
177 size_t total;
178 unsigned int i;
179 struct scatterlist *sg;
180 struct vm_struct *new;
181 const struct mem_type *mtype;
182
183 mtype = get_mem_type(MT_DEVICE);
184 if (!mtype)
185 return ERR_PTR(-EINVAL);
186
187 total = sgtable_len(sgt);
188 if (!total)
189 return ERR_PTR(-EINVAL);
190
191 new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
192 if (!new)
193 return ERR_PTR(-ENOMEM);
194 va = (u32)new->addr;
195
196 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
197 size_t bytes;
198 u32 pa;
199 int err;
200
201 pa = sg_phys(sg);
Ohad Ben-Cohen66cf4022011-06-08 09:06:11 +0300202 bytes = sg->length;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200203
204 BUG_ON(bytes != PAGE_SIZE);
205
206 err = ioremap_page(va, pa, mtype);
207 if (err)
208 goto err_out;
209
210 va += bytes;
211 }
212
Sanjeev Premi6716bd02009-09-24 16:23:12 -0700213 flush_cache_vmap((unsigned long)new->addr,
214 (unsigned long)(new->addr + total));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200215 return new->addr;
216
217err_out:
218 WARN_ON(1); /* FIXME: cleanup some mpu mappings */
219 vunmap(new->addr);
220 return ERR_PTR(-EAGAIN);
221}
222
223static inline void vunmap_sg(const void *va)
224{
225 vunmap(va);
226}
227
228static struct iovm_struct *__find_iovm_area(struct iommu *obj, const u32 da)
229{
230 struct iovm_struct *tmp;
231
232 list_for_each_entry(tmp, &obj->mmap, list) {
233 if ((da >= tmp->da_start) && (da < tmp->da_end)) {
234 size_t len;
235
236 len = tmp->da_end - tmp->da_start;
237
238 dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n",
239 __func__, tmp->da_start, da, tmp->da_end, len,
240 tmp->flags);
241
242 return tmp;
243 }
244 }
245
246 return NULL;
247}
248
249/**
250 * find_iovm_area - find iovma which includes @da
251 * @da: iommu device virtual address
252 *
253 * Find the existing iovma starting at @da
254 */
255struct iovm_struct *find_iovm_area(struct iommu *obj, u32 da)
256{
257 struct iovm_struct *area;
258
259 mutex_lock(&obj->mmap_lock);
260 area = __find_iovm_area(obj, da);
261 mutex_unlock(&obj->mmap_lock);
262
263 return area;
264}
265EXPORT_SYMBOL_GPL(find_iovm_area);
266
267/*
268 * This finds the hole(area) which fits the requested address and len
269 * in iovmas mmap, and returns the new allocated iovma.
270 */
271static struct iovm_struct *alloc_iovm_area(struct iommu *obj, u32 da,
272 size_t bytes, u32 flags)
273{
274 struct iovm_struct *new, *tmp;
Michael Jones4359d382011-03-09 09:17:32 +0000275 u32 start, prev_end, alignment;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200276
277 if (!obj || !bytes)
278 return ERR_PTR(-EINVAL);
279
280 start = da;
Michael Jones4359d382011-03-09 09:17:32 +0000281 alignment = PAGE_SIZE;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200282
David Cohend038aee2011-03-09 09:17:33 +0000283 if (~flags & IOVMF_DA_FIXED) {
Michael Jones4359d382011-03-09 09:17:32 +0000284 /* Don't map address 0 */
285 start = obj->da_start ? obj->da_start : alignment;
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000286
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200287 if (flags & IOVMF_LINEAR)
Michael Jones4359d382011-03-09 09:17:32 +0000288 alignment = iopgsz_max(bytes);
289 start = roundup(start, alignment);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000290 } else if (start < obj->da_start || start > obj->da_end ||
291 obj->da_end - start < bytes) {
292 return ERR_PTR(-EINVAL);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200293 }
294
295 tmp = NULL;
296 if (list_empty(&obj->mmap))
297 goto found;
298
299 prev_end = 0;
300 list_for_each_entry(tmp, &obj->mmap, list) {
301
Guzman Lugo, Fernandoba6e1f42010-12-15 00:54:00 +0000302 if (prev_end > start)
Hiroshi DOYUe0a42e42010-05-06 17:09:25 +0300303 break;
304
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000305 if (tmp->da_start > start && (tmp->da_start - start) >= bytes)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200306 goto found;
307
David Cohend038aee2011-03-09 09:17:33 +0000308 if (tmp->da_end >= start && ~flags & IOVMF_DA_FIXED)
Michael Jones4359d382011-03-09 09:17:32 +0000309 start = roundup(tmp->da_end + 1, alignment);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200310
311 prev_end = tmp->da_end;
312 }
313
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000314 if ((start >= prev_end) && (obj->da_end - start >= bytes))
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200315 goto found;
316
317 dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
318 __func__, da, bytes, flags);
319
320 return ERR_PTR(-EINVAL);
321
322found:
323 new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
324 if (!new)
325 return ERR_PTR(-ENOMEM);
326
327 new->iommu = obj;
328 new->da_start = start;
329 new->da_end = start + bytes;
330 new->flags = flags;
331
332 /*
333 * keep ascending order of iovmas
334 */
335 if (tmp)
336 list_add_tail(&new->list, &tmp->list);
337 else
338 list_add(&new->list, &obj->mmap);
339
340 dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
341 __func__, new->da_start, start, new->da_end, bytes, flags);
342
343 return new;
344}
345
346static void free_iovm_area(struct iommu *obj, struct iovm_struct *area)
347{
348 size_t bytes;
349
350 BUG_ON(!obj || !area);
351
352 bytes = area->da_end - area->da_start;
353
354 dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
355 __func__, area->da_start, area->da_end, bytes, area->flags);
356
357 list_del(&area->list);
358 kmem_cache_free(iovm_area_cachep, area);
359}
360
361/**
362 * da_to_va - convert (d) to (v)
363 * @obj: objective iommu
364 * @da: iommu device virtual address
365 * @va: mpu virtual address
366 *
367 * Returns mpu virtual addr which corresponds to a given device virtual addr
368 */
369void *da_to_va(struct iommu *obj, u32 da)
370{
371 void *va = NULL;
372 struct iovm_struct *area;
373
374 mutex_lock(&obj->mmap_lock);
375
376 area = __find_iovm_area(obj, da);
377 if (!area) {
378 dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
379 goto out;
380 }
381 va = area->va;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200382out:
Daniel Walker26548902009-10-05 13:31:45 -0700383 mutex_unlock(&obj->mmap_lock);
384
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200385 return va;
386}
387EXPORT_SYMBOL_GPL(da_to_va);
388
389static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
390{
391 unsigned int i;
392 struct scatterlist *sg;
393 void *va = _va;
394 void *va_end;
395
396 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
397 struct page *pg;
398 const size_t bytes = PAGE_SIZE;
399
400 /*
401 * iommu 'superpage' isn't supported with 'iommu_vmalloc()'
402 */
403 pg = vmalloc_to_page(va);
404 BUG_ON(!pg);
405 sg_set_page(sg, pg, bytes, 0);
406
407 va += bytes;
408 }
409
410 va_end = _va + PAGE_SIZE * i;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200411}
412
413static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
414{
415 /*
416 * Actually this is not necessary at all, just exists for
Hiroshi DOYUba6a1172009-10-05 13:31:45 -0700417 * consistency of the code readability.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200418 */
419 BUG_ON(!sgt);
420}
421
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000422static void sgtable_fill_kmalloc(struct sg_table *sgt, u32 pa, u32 da,
423 size_t len)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200424{
425 unsigned int i;
426 struct scatterlist *sg;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200427
428 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000429 unsigned bytes;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200430
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000431 bytes = max_alignment(da | pa);
432 bytes = min_t(unsigned, bytes, iopgsz_max(len));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200433
434 BUG_ON(!iopgsz_ok(bytes));
435
436 sg_set_buf(sg, phys_to_virt(pa), bytes);
437 /*
438 * 'pa' is cotinuous(linear).
439 */
440 pa += bytes;
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000441 da += bytes;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200442 len -= bytes;
443 }
444 BUG_ON(len);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200445}
446
447static inline void sgtable_drain_kmalloc(struct sg_table *sgt)
448{
449 /*
450 * Actually this is not necessary at all, just exists for
Hiroshi DOYUba6a1172009-10-05 13:31:45 -0700451 * consistency of the code readability
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200452 */
453 BUG_ON(!sgt);
454}
455
456/* create 'da' <-> 'pa' mapping from 'sgt' */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300457static int map_iovm_area(struct iommu_domain *domain, struct iovm_struct *new,
458 const struct sg_table *sgt, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200459{
460 int err;
461 unsigned int i, j;
462 struct scatterlist *sg;
463 u32 da = new->da_start;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300464 int order;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200465
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300466 if (!domain || !sgt)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200467 return -EINVAL;
468
469 BUG_ON(!sgtable_ok(sgt));
470
471 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
472 u32 pa;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200473 size_t bytes;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200474
475 pa = sg_phys(sg);
Ohad Ben-Cohen66cf4022011-06-08 09:06:11 +0300476 bytes = sg->length;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200477
478 flags &= ~IOVMF_PGSZ_MASK;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300479
480 if (bytes_to_iopgsz(bytes) < 0)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200481 goto err_out;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300482
483 order = get_order(bytes);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200484
485 pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
486 i, da, pa, bytes);
487
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300488 err = iommu_map(domain, da, pa, order, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200489 if (err)
490 goto err_out;
491
492 da += bytes;
493 }
494 return 0;
495
496err_out:
497 da = new->da_start;
498
499 for_each_sg(sgt->sgl, sg, i, j) {
500 size_t bytes;
501
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300502 bytes = sg->length;
503 order = get_order(bytes);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200504
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300505 /* ignore failures.. we're already handling one */
506 iommu_unmap(domain, da, order);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200507
508 da += bytes;
509 }
510 return err;
511}
512
513/* release 'da' <-> 'pa' mapping */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300514static void unmap_iovm_area(struct iommu_domain *domain, struct iommu *obj,
515 struct iovm_struct *area)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200516{
517 u32 start;
518 size_t total = area->da_end - area->da_start;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300519 const struct sg_table *sgt = area->sgt;
520 struct scatterlist *sg;
521 int i, err;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200522
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300523 BUG_ON(!sgtable_ok(sgt));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200524 BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));
525
526 start = area->da_start;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300527 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200528 size_t bytes;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300529 int order;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200530
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300531 bytes = sg->length;
532 order = get_order(bytes);
533
534 err = iommu_unmap(domain, start, order);
535 if (err)
536 break;
537
538 dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200539 __func__, start, bytes, area->flags);
540
541 BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));
542
543 total -= bytes;
544 start += bytes;
545 }
546 BUG_ON(total);
547}
548
549/* template function for all unmapping */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300550static struct sg_table *unmap_vm_area(struct iommu_domain *domain,
551 struct iommu *obj, const u32 da,
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200552 void (*fn)(const void *), u32 flags)
553{
554 struct sg_table *sgt = NULL;
555 struct iovm_struct *area;
556
557 if (!IS_ALIGNED(da, PAGE_SIZE)) {
558 dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
559 return NULL;
560 }
561
562 mutex_lock(&obj->mmap_lock);
563
564 area = __find_iovm_area(obj, da);
565 if (!area) {
566 dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
567 goto out;
568 }
569
570 if ((area->flags & flags) != flags) {
571 dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
572 area->flags);
573 goto out;
574 }
575 sgt = (struct sg_table *)area->sgt;
576
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300577 unmap_iovm_area(domain, obj, area);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200578
579 fn(area->va);
580
581 dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
582 area->da_start, da, area->da_end,
583 area->da_end - area->da_start, area->flags);
584
585 free_iovm_area(obj, area);
586out:
587 mutex_unlock(&obj->mmap_lock);
588
589 return sgt;
590}
591
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300592static u32 map_iommu_region(struct iommu_domain *domain, struct iommu *obj,
593 u32 da, const struct sg_table *sgt, void *va,
594 size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200595{
596 int err = -ENOMEM;
597 struct iovm_struct *new;
598
599 mutex_lock(&obj->mmap_lock);
600
601 new = alloc_iovm_area(obj, da, bytes, flags);
602 if (IS_ERR(new)) {
603 err = PTR_ERR(new);
604 goto err_alloc_iovma;
605 }
606 new->va = va;
607 new->sgt = sgt;
608
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300609 if (map_iovm_area(domain, new, sgt, new->flags))
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200610 goto err_map;
611
612 mutex_unlock(&obj->mmap_lock);
613
614 dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
615 __func__, new->da_start, bytes, new->flags, va);
616
617 return new->da_start;
618
619err_map:
620 free_iovm_area(obj, new);
621err_alloc_iovma:
622 mutex_unlock(&obj->mmap_lock);
623 return err;
624}
625
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300626static inline u32 __iommu_vmap(struct iommu_domain *domain, struct iommu *obj,
627 u32 da, const struct sg_table *sgt,
628 void *va, size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200629{
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300630 return map_iommu_region(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200631}
632
633/**
634 * iommu_vmap - (d)-(p)-(v) address mapper
635 * @obj: objective iommu
636 * @sgt: address of scatter gather table
637 * @flags: iovma and page property
638 *
639 * Creates 1-n-1 mapping with given @sgt and returns @da.
640 * All @sgt element must be io page size aligned.
641 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300642u32 iommu_vmap(struct iommu_domain *domain, struct iommu *obj, u32 da,
643 const struct sg_table *sgt, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200644{
645 size_t bytes;
Hiroshi DOYU935e4732009-11-22 10:11:02 -0800646 void *va = NULL;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200647
648 if (!obj || !obj->dev || !sgt)
649 return -EINVAL;
650
651 bytes = sgtable_len(sgt);
652 if (!bytes)
653 return -EINVAL;
654 bytes = PAGE_ALIGN(bytes);
655
Hiroshi DOYU935e4732009-11-22 10:11:02 -0800656 if (flags & IOVMF_MMIO) {
657 va = vmap_sg(sgt);
658 if (IS_ERR(va))
659 return PTR_ERR(va);
660 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200661
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200662 flags |= IOVMF_DISCONT;
663 flags |= IOVMF_MMIO;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200664
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300665 da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200666 if (IS_ERR_VALUE(da))
667 vunmap_sg(va);
668
669 return da;
670}
671EXPORT_SYMBOL_GPL(iommu_vmap);
672
673/**
674 * iommu_vunmap - release virtual mapping obtained by 'iommu_vmap()'
675 * @obj: objective iommu
676 * @da: iommu device virtual address
677 *
678 * Free the iommu virtually contiguous memory area starting at
679 * @da, which was returned by 'iommu_vmap()'.
680 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300681struct sg_table *
682iommu_vunmap(struct iommu_domain *domain, struct iommu *obj, u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200683{
684 struct sg_table *sgt;
685 /*
686 * 'sgt' is allocated before 'iommu_vmalloc()' is called.
687 * Just returns 'sgt' to the caller to free
688 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300689 sgt = unmap_vm_area(domain, obj, da, vunmap_sg,
690 IOVMF_DISCONT | IOVMF_MMIO);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200691 if (!sgt)
692 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
693 return sgt;
694}
695EXPORT_SYMBOL_GPL(iommu_vunmap);
696
697/**
698 * iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
699 * @obj: objective iommu
700 * @da: contiguous iommu virtual memory
701 * @bytes: allocation size
702 * @flags: iovma and page property
703 *
704 * Allocate @bytes linearly and creates 1-n-1 mapping and returns
David Cohend038aee2011-03-09 09:17:33 +0000705 * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200706 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300707u32 iommu_vmalloc(struct iommu_domain *domain, struct iommu *obj, u32 da,
708 size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200709{
710 void *va;
711 struct sg_table *sgt;
712
713 if (!obj || !obj->dev || !bytes)
714 return -EINVAL;
715
716 bytes = PAGE_ALIGN(bytes);
717
718 va = vmalloc(bytes);
719 if (!va)
720 return -ENOMEM;
721
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000722 flags |= IOVMF_DISCONT;
723 flags |= IOVMF_ALLOC;
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000724
725 sgt = sgtable_alloc(bytes, flags, da, 0);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200726 if (IS_ERR(sgt)) {
727 da = PTR_ERR(sgt);
728 goto err_sgt_alloc;
729 }
730 sgtable_fill_vmalloc(sgt, va);
731
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300732 da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200733 if (IS_ERR_VALUE(da))
734 goto err_iommu_vmap;
735
736 return da;
737
738err_iommu_vmap:
739 sgtable_drain_vmalloc(sgt);
740 sgtable_free(sgt);
741err_sgt_alloc:
742 vfree(va);
743 return da;
744}
745EXPORT_SYMBOL_GPL(iommu_vmalloc);
746
747/**
748 * iommu_vfree - release memory allocated by 'iommu_vmalloc()'
749 * @obj: objective iommu
750 * @da: iommu device virtual address
751 *
752 * Frees the iommu virtually continuous memory area starting at
753 * @da, as obtained from 'iommu_vmalloc()'.
754 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300755void iommu_vfree(struct iommu_domain *domain, struct iommu *obj, const u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200756{
757 struct sg_table *sgt;
758
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300759 sgt = unmap_vm_area(domain, obj, da, vfree,
760 IOVMF_DISCONT | IOVMF_ALLOC);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200761 if (!sgt)
762 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
763 sgtable_free(sgt);
764}
765EXPORT_SYMBOL_GPL(iommu_vfree);
766
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300767static u32 __iommu_kmap(struct iommu_domain *domain, struct iommu *obj,
768 u32 da, u32 pa, void *va, size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200769{
770 struct sg_table *sgt;
771
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000772 sgt = sgtable_alloc(bytes, flags, da, pa);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200773 if (IS_ERR(sgt))
774 return PTR_ERR(sgt);
775
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000776 sgtable_fill_kmalloc(sgt, pa, da, bytes);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200777
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300778 da = map_iommu_region(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200779 if (IS_ERR_VALUE(da)) {
780 sgtable_drain_kmalloc(sgt);
781 sgtable_free(sgt);
782 }
783
784 return da;
785}
786
787/**
788 * iommu_kmap - (d)-(p)-(v) address mapper
789 * @obj: objective iommu
790 * @da: contiguous iommu virtual memory
791 * @pa: contiguous physical memory
792 * @flags: iovma and page property
793 *
794 * Creates 1-1-1 mapping and returns @da again, which can be
David Cohend038aee2011-03-09 09:17:33 +0000795 * adjusted if 'IOVMF_DA_FIXED' is not set.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200796 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300797u32 iommu_kmap(struct iommu_domain *domain, struct iommu *obj, u32 da, u32 pa,
798 size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200799{
800 void *va;
801
802 if (!obj || !obj->dev || !bytes)
803 return -EINVAL;
804
805 bytes = PAGE_ALIGN(bytes);
806
807 va = ioremap(pa, bytes);
808 if (!va)
809 return -ENOMEM;
810
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200811 flags |= IOVMF_LINEAR;
812 flags |= IOVMF_MMIO;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200813
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300814 da = __iommu_kmap(domain, obj, da, pa, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200815 if (IS_ERR_VALUE(da))
816 iounmap(va);
817
818 return da;
819}
820EXPORT_SYMBOL_GPL(iommu_kmap);
821
822/**
823 * iommu_kunmap - release virtual mapping obtained by 'iommu_kmap()'
824 * @obj: objective iommu
825 * @da: iommu device virtual address
826 *
827 * Frees the iommu virtually contiguous memory area starting at
828 * @da, which was passed to and was returned by'iommu_kmap()'.
829 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300830void iommu_kunmap(struct iommu_domain *domain, struct iommu *obj, u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200831{
832 struct sg_table *sgt;
833 typedef void (*func_t)(const void *);
834
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300835 sgt = unmap_vm_area(domain, obj, da, (func_t)iounmap,
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200836 IOVMF_LINEAR | IOVMF_MMIO);
837 if (!sgt)
838 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
839 sgtable_free(sgt);
840}
841EXPORT_SYMBOL_GPL(iommu_kunmap);
842
843/**
844 * iommu_kmalloc - (d)-(p)-(v) address allocator and mapper
845 * @obj: objective iommu
846 * @da: contiguous iommu virtual memory
847 * @bytes: bytes for allocation
848 * @flags: iovma and page property
849 *
850 * Allocate @bytes linearly and creates 1-1-1 mapping and returns
David Cohend038aee2011-03-09 09:17:33 +0000851 * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200852 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300853u32 iommu_kmalloc(struct iommu_domain *domain, struct iommu *obj, u32 da,
854 size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200855{
856 void *va;
857 u32 pa;
858
859 if (!obj || !obj->dev || !bytes)
860 return -EINVAL;
861
862 bytes = PAGE_ALIGN(bytes);
863
864 va = kmalloc(bytes, GFP_KERNEL | GFP_DMA);
865 if (!va)
866 return -ENOMEM;
867 pa = virt_to_phys(va);
868
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200869 flags |= IOVMF_LINEAR;
870 flags |= IOVMF_ALLOC;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200871
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300872 da = __iommu_kmap(domain, obj, da, pa, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200873 if (IS_ERR_VALUE(da))
874 kfree(va);
875
876 return da;
877}
878EXPORT_SYMBOL_GPL(iommu_kmalloc);
879
880/**
881 * iommu_kfree - release virtual mapping obtained by 'iommu_kmalloc()'
882 * @obj: objective iommu
883 * @da: iommu device virtual address
884 *
885 * Frees the iommu virtually contiguous memory area starting at
886 * @da, which was passed to and was returned by'iommu_kmalloc()'.
887 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300888void iommu_kfree(struct iommu_domain *domain, struct iommu *obj, u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200889{
890 struct sg_table *sgt;
891
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300892 sgt = unmap_vm_area(domain, obj, da, kfree, IOVMF_LINEAR | IOVMF_ALLOC);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200893 if (!sgt)
894 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
895 sgtable_free(sgt);
896}
897EXPORT_SYMBOL_GPL(iommu_kfree);
898
899
900static int __init iovmm_init(void)
901{
902 const unsigned long flags = SLAB_HWCACHE_ALIGN;
903 struct kmem_cache *p;
904
905 p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
906 flags, NULL);
907 if (!p)
908 return -ENOMEM;
909 iovm_area_cachep = p;
910
911 return 0;
912}
913module_init(iovmm_init);
914
915static void __exit iovmm_exit(void)
916{
917 kmem_cache_destroy(iovm_area_cachep);
918}
919module_exit(iovmm_exit);
920
921MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
922MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
923MODULE_LICENSE("GPL v2");