blob: 46be456fcc00e01c10b2ae8106a83310b41d29b9 [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
Ming Lei08f2e632011-11-08 18:29:15 +080013#include <linux/module.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020014#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020016#include <linux/vmalloc.h>
17#include <linux/device.h>
18#include <linux/scatterlist.h>
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +030019#include <linux/iommu.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020020
21#include <asm/cacheflush.h>
22#include <asm/mach/map.h>
23
Tony Lindgrence491cf2009-10-20 09:40:47 -070024#include <plat/iommu.h>
25#include <plat/iovmm.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020026
Ohad Ben-Cohenfcf3a6e2011-08-15 23:21:41 +030027#include <plat/iopgtable.h>
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020028
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020029static struct kmem_cache *iovm_area_cachep;
30
Laurent Pinchart329d8d32011-09-02 13:32:30 -040031/* return the offset of the first scatterlist entry in a sg table */
32static unsigned int sgtable_offset(const struct sg_table *sgt)
33{
34 if (!sgt || !sgt->nents)
35 return 0;
36
37 return sgt->sgl->offset;
38}
39
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020040/* return total bytes of sg buffers */
41static size_t sgtable_len(const struct sg_table *sgt)
42{
43 unsigned int i, total = 0;
44 struct scatterlist *sg;
45
46 if (!sgt)
47 return 0;
48
49 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
50 size_t bytes;
51
Laurent Pinchart329d8d32011-09-02 13:32:30 -040052 bytes = sg->length + sg->offset;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020053
54 if (!iopgsz_ok(bytes)) {
Laurent Pinchart329d8d32011-09-02 13:32:30 -040055 pr_err("%s: sg[%d] not iommu pagesize(%u %u)\n",
56 __func__, i, bytes, sg->offset);
57 return 0;
58 }
59
60 if (i && sg->offset) {
61 pr_err("%s: sg[%d] offset not allowed in internal "
62 "entries\n", __func__, i);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020063 return 0;
64 }
65
66 total += bytes;
67 }
68
69 return total;
70}
71#define sgtable_ok(x) (!!sgtable_len(x))
72
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +000073static unsigned max_alignment(u32 addr)
74{
75 int i;
76 unsigned pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, };
77 for (i = 0; i < ARRAY_SIZE(pagesize) && addr & (pagesize[i] - 1); i++)
78 ;
79 return (i < ARRAY_SIZE(pagesize)) ? pagesize[i] : 0;
80}
81
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020082/*
83 * calculate the optimal number sg elements from total bytes based on
84 * iommu superpages
85 */
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +000086static unsigned sgtable_nents(size_t bytes, u32 da, u32 pa)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020087{
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +000088 unsigned nr_entries = 0, ent_sz;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020089
90 if (!IS_ALIGNED(bytes, PAGE_SIZE)) {
91 pr_err("%s: wrong size %08x\n", __func__, bytes);
92 return 0;
93 }
94
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +000095 while (bytes) {
96 ent_sz = max_alignment(da | pa);
97 ent_sz = min_t(unsigned, ent_sz, iopgsz_max(bytes));
98 nr_entries++;
99 da += ent_sz;
100 pa += ent_sz;
101 bytes -= ent_sz;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200102 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200103
104 return nr_entries;
105}
106
107/* allocate and initialize sg_table header(a kind of 'superblock') */
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000108static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags,
109 u32 da, u32 pa)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200110{
111 unsigned int nr_entries;
112 int err;
113 struct sg_table *sgt;
114
115 if (!bytes)
116 return ERR_PTR(-EINVAL);
117
118 if (!IS_ALIGNED(bytes, PAGE_SIZE))
119 return ERR_PTR(-EINVAL);
120
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000121 if (flags & IOVMF_LINEAR) {
122 nr_entries = sgtable_nents(bytes, da, pa);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200123 if (!nr_entries)
124 return ERR_PTR(-EINVAL);
125 } else
126 nr_entries = bytes / PAGE_SIZE;
127
128 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
129 if (!sgt)
130 return ERR_PTR(-ENOMEM);
131
132 err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL);
Satish7f1225b2010-06-09 13:21:27 +0300133 if (err) {
134 kfree(sgt);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200135 return ERR_PTR(err);
Satish7f1225b2010-06-09 13:21:27 +0300136 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200137
138 pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries);
139
140 return sgt;
141}
142
143/* free sg_table header(a kind of superblock) */
144static void sgtable_free(struct sg_table *sgt)
145{
146 if (!sgt)
147 return;
148
149 sg_free_table(sgt);
150 kfree(sgt);
151
152 pr_debug("%s: sgt:%p\n", __func__, sgt);
153}
154
155/* map 'sglist' to a contiguous mpu virtual area and return 'va' */
156static void *vmap_sg(const struct sg_table *sgt)
157{
158 u32 va;
159 size_t total;
160 unsigned int i;
161 struct scatterlist *sg;
162 struct vm_struct *new;
163 const struct mem_type *mtype;
164
165 mtype = get_mem_type(MT_DEVICE);
166 if (!mtype)
167 return ERR_PTR(-EINVAL);
168
169 total = sgtable_len(sgt);
170 if (!total)
171 return ERR_PTR(-EINVAL);
172
173 new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
174 if (!new)
175 return ERR_PTR(-ENOMEM);
176 va = (u32)new->addr;
177
178 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
179 size_t bytes;
180 u32 pa;
181 int err;
182
Laurent Pinchart329d8d32011-09-02 13:32:30 -0400183 pa = sg_phys(sg) - sg->offset;
184 bytes = sg->length + sg->offset;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200185
186 BUG_ON(bytes != PAGE_SIZE);
187
188 err = ioremap_page(va, pa, mtype);
189 if (err)
190 goto err_out;
191
192 va += bytes;
193 }
194
Sanjeev Premi6716bd02009-09-24 16:23:12 -0700195 flush_cache_vmap((unsigned long)new->addr,
196 (unsigned long)(new->addr + total));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200197 return new->addr;
198
199err_out:
200 WARN_ON(1); /* FIXME: cleanup some mpu mappings */
201 vunmap(new->addr);
202 return ERR_PTR(-EAGAIN);
203}
204
205static inline void vunmap_sg(const void *va)
206{
207 vunmap(va);
208}
209
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300210static struct iovm_struct *__find_iovm_area(struct omap_iommu *obj,
211 const u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200212{
213 struct iovm_struct *tmp;
214
215 list_for_each_entry(tmp, &obj->mmap, list) {
216 if ((da >= tmp->da_start) && (da < tmp->da_end)) {
217 size_t len;
218
219 len = tmp->da_end - tmp->da_start;
220
221 dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n",
222 __func__, tmp->da_start, da, tmp->da_end, len,
223 tmp->flags);
224
225 return tmp;
226 }
227 }
228
229 return NULL;
230}
231
232/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300233 * omap_find_iovm_area - find iovma which includes @da
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200234 * @da: iommu device virtual address
235 *
236 * Find the existing iovma starting at @da
237 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300238struct iovm_struct *omap_find_iovm_area(struct omap_iommu *obj, u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200239{
240 struct iovm_struct *area;
241
242 mutex_lock(&obj->mmap_lock);
243 area = __find_iovm_area(obj, da);
244 mutex_unlock(&obj->mmap_lock);
245
246 return area;
247}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300248EXPORT_SYMBOL_GPL(omap_find_iovm_area);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200249
250/*
251 * This finds the hole(area) which fits the requested address and len
252 * in iovmas mmap, and returns the new allocated iovma.
253 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300254static struct iovm_struct *alloc_iovm_area(struct omap_iommu *obj, u32 da,
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200255 size_t bytes, u32 flags)
256{
257 struct iovm_struct *new, *tmp;
Michael Jones4359d382011-03-09 09:17:32 +0000258 u32 start, prev_end, alignment;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200259
260 if (!obj || !bytes)
261 return ERR_PTR(-EINVAL);
262
263 start = da;
Michael Jones4359d382011-03-09 09:17:32 +0000264 alignment = PAGE_SIZE;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200265
David Cohend038aee2011-03-09 09:17:33 +0000266 if (~flags & IOVMF_DA_FIXED) {
Michael Jones4359d382011-03-09 09:17:32 +0000267 /* Don't map address 0 */
268 start = obj->da_start ? obj->da_start : alignment;
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000269
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200270 if (flags & IOVMF_LINEAR)
Michael Jones4359d382011-03-09 09:17:32 +0000271 alignment = iopgsz_max(bytes);
272 start = roundup(start, alignment);
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000273 } else if (start < obj->da_start || start > obj->da_end ||
274 obj->da_end - start < bytes) {
275 return ERR_PTR(-EINVAL);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200276 }
277
278 tmp = NULL;
279 if (list_empty(&obj->mmap))
280 goto found;
281
282 prev_end = 0;
283 list_for_each_entry(tmp, &obj->mmap, list) {
284
Guzman Lugo, Fernandoba6e1f42010-12-15 00:54:00 +0000285 if (prev_end > start)
Hiroshi DOYUe0a42e42010-05-06 17:09:25 +0300286 break;
287
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000288 if (tmp->da_start > start && (tmp->da_start - start) >= bytes)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200289 goto found;
290
David Cohend038aee2011-03-09 09:17:33 +0000291 if (tmp->da_end >= start && ~flags & IOVMF_DA_FIXED)
Michael Jones4359d382011-03-09 09:17:32 +0000292 start = roundup(tmp->da_end + 1, alignment);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200293
294 prev_end = tmp->da_end;
295 }
296
Guzman Lugo, Fernandoc7f4ab22010-12-15 00:54:03 +0000297 if ((start >= prev_end) && (obj->da_end - start >= bytes))
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200298 goto found;
299
300 dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
301 __func__, da, bytes, flags);
302
303 return ERR_PTR(-EINVAL);
304
305found:
306 new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
307 if (!new)
308 return ERR_PTR(-ENOMEM);
309
310 new->iommu = obj;
311 new->da_start = start;
312 new->da_end = start + bytes;
313 new->flags = flags;
314
315 /*
316 * keep ascending order of iovmas
317 */
318 if (tmp)
319 list_add_tail(&new->list, &tmp->list);
320 else
321 list_add(&new->list, &obj->mmap);
322
323 dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
324 __func__, new->da_start, start, new->da_end, bytes, flags);
325
326 return new;
327}
328
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300329static void free_iovm_area(struct omap_iommu *obj, struct iovm_struct *area)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200330{
331 size_t bytes;
332
333 BUG_ON(!obj || !area);
334
335 bytes = area->da_end - area->da_start;
336
337 dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
338 __func__, area->da_start, area->da_end, bytes, area->flags);
339
340 list_del(&area->list);
341 kmem_cache_free(iovm_area_cachep, area);
342}
343
344/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300345 * omap_da_to_va - convert (d) to (v)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200346 * @obj: objective iommu
347 * @da: iommu device virtual address
348 * @va: mpu virtual address
349 *
350 * Returns mpu virtual addr which corresponds to a given device virtual addr
351 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300352void *omap_da_to_va(struct omap_iommu *obj, u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200353{
354 void *va = NULL;
355 struct iovm_struct *area;
356
357 mutex_lock(&obj->mmap_lock);
358
359 area = __find_iovm_area(obj, da);
360 if (!area) {
361 dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
362 goto out;
363 }
364 va = area->va;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200365out:
Daniel Walker26548902009-10-05 13:31:45 -0700366 mutex_unlock(&obj->mmap_lock);
367
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200368 return va;
369}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300370EXPORT_SYMBOL_GPL(omap_da_to_va);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200371
372static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
373{
374 unsigned int i;
375 struct scatterlist *sg;
376 void *va = _va;
377 void *va_end;
378
379 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
380 struct page *pg;
381 const size_t bytes = PAGE_SIZE;
382
383 /*
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300384 * iommu 'superpage' isn't supported with 'omap_iommu_vmalloc()'
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200385 */
386 pg = vmalloc_to_page(va);
387 BUG_ON(!pg);
388 sg_set_page(sg, pg, bytes, 0);
389
390 va += bytes;
391 }
392
393 va_end = _va + PAGE_SIZE * i;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200394}
395
396static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
397{
398 /*
399 * Actually this is not necessary at all, just exists for
Hiroshi DOYUba6a1172009-10-05 13:31:45 -0700400 * consistency of the code readability.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200401 */
402 BUG_ON(!sgt);
403}
404
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200405/* create 'da' <-> 'pa' mapping from 'sgt' */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300406static int map_iovm_area(struct iommu_domain *domain, struct iovm_struct *new,
407 const struct sg_table *sgt, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200408{
409 int err;
410 unsigned int i, j;
411 struct scatterlist *sg;
412 u32 da = new->da_start;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300413 int order;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200414
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300415 if (!domain || !sgt)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200416 return -EINVAL;
417
418 BUG_ON(!sgtable_ok(sgt));
419
420 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
421 u32 pa;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200422 size_t bytes;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200423
Laurent Pinchart329d8d32011-09-02 13:32:30 -0400424 pa = sg_phys(sg) - sg->offset;
425 bytes = sg->length + sg->offset;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200426
427 flags &= ~IOVMF_PGSZ_MASK;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300428
429 if (bytes_to_iopgsz(bytes) < 0)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200430 goto err_out;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300431
432 order = get_order(bytes);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200433
434 pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
435 i, da, pa, bytes);
436
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300437 err = iommu_map(domain, da, pa, order, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200438 if (err)
439 goto err_out;
440
441 da += bytes;
442 }
443 return 0;
444
445err_out:
446 da = new->da_start;
447
448 for_each_sg(sgt->sgl, sg, i, j) {
449 size_t bytes;
450
Laurent Pinchart329d8d32011-09-02 13:32:30 -0400451 bytes = sg->length + sg->offset;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300452 order = get_order(bytes);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200453
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300454 /* ignore failures.. we're already handling one */
455 iommu_unmap(domain, da, order);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200456
457 da += bytes;
458 }
459 return err;
460}
461
462/* release 'da' <-> 'pa' mapping */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300463static void unmap_iovm_area(struct iommu_domain *domain, struct omap_iommu *obj,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300464 struct iovm_struct *area)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200465{
466 u32 start;
467 size_t total = area->da_end - area->da_start;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300468 const struct sg_table *sgt = area->sgt;
469 struct scatterlist *sg;
470 int i, err;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200471
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300472 BUG_ON(!sgtable_ok(sgt));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200473 BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));
474
475 start = area->da_start;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300476 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200477 size_t bytes;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300478 int order;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200479
Laurent Pinchart329d8d32011-09-02 13:32:30 -0400480 bytes = sg->length + sg->offset;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300481 order = get_order(bytes);
482
483 err = iommu_unmap(domain, start, order);
Ohad Ben-Cohen5e1b6122011-09-02 13:32:33 -0400484 if (err < 0)
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300485 break;
486
487 dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200488 __func__, start, bytes, area->flags);
489
490 BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));
491
492 total -= bytes;
493 start += bytes;
494 }
495 BUG_ON(total);
496}
497
498/* template function for all unmapping */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300499static struct sg_table *unmap_vm_area(struct iommu_domain *domain,
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300500 struct omap_iommu *obj, const u32 da,
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200501 void (*fn)(const void *), u32 flags)
502{
503 struct sg_table *sgt = NULL;
504 struct iovm_struct *area;
505
506 if (!IS_ALIGNED(da, PAGE_SIZE)) {
507 dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
508 return NULL;
509 }
510
511 mutex_lock(&obj->mmap_lock);
512
513 area = __find_iovm_area(obj, da);
514 if (!area) {
515 dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
516 goto out;
517 }
518
519 if ((area->flags & flags) != flags) {
520 dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
521 area->flags);
522 goto out;
523 }
524 sgt = (struct sg_table *)area->sgt;
525
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300526 unmap_iovm_area(domain, obj, area);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200527
528 fn(area->va);
529
530 dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
531 area->da_start, da, area->da_end,
532 area->da_end - area->da_start, area->flags);
533
534 free_iovm_area(obj, area);
535out:
536 mutex_unlock(&obj->mmap_lock);
537
538 return sgt;
539}
540
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300541static u32 map_iommu_region(struct iommu_domain *domain, struct omap_iommu *obj,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300542 u32 da, const struct sg_table *sgt, void *va,
543 size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200544{
545 int err = -ENOMEM;
546 struct iovm_struct *new;
547
548 mutex_lock(&obj->mmap_lock);
549
550 new = alloc_iovm_area(obj, da, bytes, flags);
551 if (IS_ERR(new)) {
552 err = PTR_ERR(new);
553 goto err_alloc_iovma;
554 }
555 new->va = va;
556 new->sgt = sgt;
557
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300558 if (map_iovm_area(domain, new, sgt, new->flags))
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200559 goto err_map;
560
561 mutex_unlock(&obj->mmap_lock);
562
563 dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
564 __func__, new->da_start, bytes, new->flags, va);
565
566 return new->da_start;
567
568err_map:
569 free_iovm_area(obj, new);
570err_alloc_iovma:
571 mutex_unlock(&obj->mmap_lock);
572 return err;
573}
574
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300575static inline u32
576__iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300577 u32 da, const struct sg_table *sgt,
578 void *va, size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200579{
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300580 return map_iommu_region(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200581}
582
583/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300584 * omap_iommu_vmap - (d)-(p)-(v) address mapper
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200585 * @obj: objective iommu
586 * @sgt: address of scatter gather table
587 * @flags: iovma and page property
588 *
589 * Creates 1-n-1 mapping with given @sgt and returns @da.
590 * All @sgt element must be io page size aligned.
591 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300592u32 omap_iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj, u32 da,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300593 const struct sg_table *sgt, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200594{
595 size_t bytes;
Hiroshi DOYU935e4732009-11-22 10:11:02 -0800596 void *va = NULL;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200597
598 if (!obj || !obj->dev || !sgt)
599 return -EINVAL;
600
601 bytes = sgtable_len(sgt);
602 if (!bytes)
603 return -EINVAL;
604 bytes = PAGE_ALIGN(bytes);
605
Hiroshi DOYU935e4732009-11-22 10:11:02 -0800606 if (flags & IOVMF_MMIO) {
607 va = vmap_sg(sgt);
608 if (IS_ERR(va))
609 return PTR_ERR(va);
610 }
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200611
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200612 flags |= IOVMF_DISCONT;
613 flags |= IOVMF_MMIO;
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200614
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300615 da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200616 if (IS_ERR_VALUE(da))
617 vunmap_sg(va);
618
Laurent Pinchart329d8d32011-09-02 13:32:30 -0400619 return da + sgtable_offset(sgt);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200620}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300621EXPORT_SYMBOL_GPL(omap_iommu_vmap);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200622
623/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300624 * omap_iommu_vunmap - release virtual mapping obtained by 'omap_iommu_vmap()'
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200625 * @obj: objective iommu
626 * @da: iommu device virtual address
627 *
628 * Free the iommu virtually contiguous memory area starting at
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300629 * @da, which was returned by 'omap_iommu_vmap()'.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200630 */
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300631struct sg_table *
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300632omap_iommu_vunmap(struct iommu_domain *domain, struct omap_iommu *obj, u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200633{
634 struct sg_table *sgt;
635 /*
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300636 * 'sgt' is allocated before 'omap_iommu_vmalloc()' is called.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200637 * Just returns 'sgt' to the caller to free
638 */
Laurent Pinchart329d8d32011-09-02 13:32:30 -0400639 da &= PAGE_MASK;
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300640 sgt = unmap_vm_area(domain, obj, da, vunmap_sg,
641 IOVMF_DISCONT | IOVMF_MMIO);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200642 if (!sgt)
643 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
644 return sgt;
645}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300646EXPORT_SYMBOL_GPL(omap_iommu_vunmap);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200647
648/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300649 * omap_iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200650 * @obj: objective iommu
651 * @da: contiguous iommu virtual memory
652 * @bytes: allocation size
653 * @flags: iovma and page property
654 *
655 * Allocate @bytes linearly and creates 1-n-1 mapping and returns
David Cohend038aee2011-03-09 09:17:33 +0000656 * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200657 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300658u32
659omap_iommu_vmalloc(struct iommu_domain *domain, struct omap_iommu *obj, u32 da,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300660 size_t bytes, u32 flags)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200661{
662 void *va;
663 struct sg_table *sgt;
664
665 if (!obj || !obj->dev || !bytes)
666 return -EINVAL;
667
668 bytes = PAGE_ALIGN(bytes);
669
670 va = vmalloc(bytes);
671 if (!va)
672 return -ENOMEM;
673
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000674 flags |= IOVMF_DISCONT;
675 flags |= IOVMF_ALLOC;
Guzman Lugo, Fernandoad108122010-12-15 00:54:01 +0000676
677 sgt = sgtable_alloc(bytes, flags, da, 0);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200678 if (IS_ERR(sgt)) {
679 da = PTR_ERR(sgt);
680 goto err_sgt_alloc;
681 }
682 sgtable_fill_vmalloc(sgt, va);
683
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300684 da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200685 if (IS_ERR_VALUE(da))
686 goto err_iommu_vmap;
687
688 return da;
689
690err_iommu_vmap:
691 sgtable_drain_vmalloc(sgt);
692 sgtable_free(sgt);
693err_sgt_alloc:
694 vfree(va);
695 return da;
696}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300697EXPORT_SYMBOL_GPL(omap_iommu_vmalloc);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200698
699/**
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300700 * omap_iommu_vfree - release memory allocated by 'omap_iommu_vmalloc()'
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200701 * @obj: objective iommu
702 * @da: iommu device virtual address
703 *
704 * Frees the iommu virtually continuous memory area starting at
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300705 * @da, as obtained from 'omap_iommu_vmalloc()'.
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200706 */
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300707void omap_iommu_vfree(struct iommu_domain *domain, struct omap_iommu *obj,
708 const u32 da)
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200709{
710 struct sg_table *sgt;
711
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300712 sgt = unmap_vm_area(domain, obj, da, vfree,
713 IOVMF_DISCONT | IOVMF_ALLOC);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200714 if (!sgt)
715 dev_dbg(obj->dev, "%s: No sgt\n", __func__);
716 sgtable_free(sgt);
717}
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300718EXPORT_SYMBOL_GPL(omap_iommu_vfree);
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200719
Hiroshi DOYU69d3a842009-01-28 21:32:08 +0200720static int __init iovmm_init(void)
721{
722 const unsigned long flags = SLAB_HWCACHE_ALIGN;
723 struct kmem_cache *p;
724
725 p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
726 flags, NULL);
727 if (!p)
728 return -ENOMEM;
729 iovm_area_cachep = p;
730
731 return 0;
732}
733module_init(iovmm_init);
734
735static void __exit iovmm_exit(void)
736{
737 kmem_cache_destroy(iovm_area_cachep);
738}
739module_exit(iovmm_exit);
740
741MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
742MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
743MODULE_LICENSE("GPL v2");