2009-01-28 19:32:08 +00:00
|
|
|
/*
|
|
|
|
* omap iommu: simple virtual address space management
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2009 Nokia Corporation
|
|
|
|
*
|
|
|
|
* Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2011-11-08 10:29:15 +00:00
|
|
|
#include <linux/module.h>
|
2009-01-28 19:32:08 +00:00
|
|
|
#include <linux/err.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2009-01-28 19:32:08 +00:00
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/scatterlist.h>
|
2011-06-01 22:46:12 +00:00
|
|
|
#include <linux/iommu.h>
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
#include <asm/mach/map.h>
|
|
|
|
|
2009-10-20 16:40:47 +00:00
|
|
|
#include <plat/iommu.h>
|
|
|
|
#include <plat/iovmm.h>
|
2009-01-28 19:32:08 +00:00
|
|
|
|
2011-08-15 20:21:41 +00:00
|
|
|
#include <plat/iopgtable.h>
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
static struct kmem_cache *iovm_area_cachep;
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
/* return the offset of the first scatterlist entry in a sg table */
|
|
|
|
static unsigned int sgtable_offset(const struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
if (!sgt || !sgt->nents)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return sgt->sgl->offset;
|
|
|
|
}
|
|
|
|
|
2009-01-28 19:32:08 +00:00
|
|
|
/* return total bytes of sg buffers */
|
|
|
|
static size_t sgtable_len(const struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
unsigned int i, total = 0;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
|
|
|
|
if (!sgt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for_each_sg(sgt->sgl, sg, sgt->nents, i) {
|
|
|
|
size_t bytes;
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
bytes = sg->length + sg->offset;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
if (!iopgsz_ok(bytes)) {
|
2011-09-02 17:32:30 +00:00
|
|
|
pr_err("%s: sg[%d] not iommu pagesize(%u %u)\n",
|
|
|
|
__func__, i, bytes, sg->offset);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i && sg->offset) {
|
|
|
|
pr_err("%s: sg[%d] offset not allowed in internal "
|
|
|
|
"entries\n", __func__, i);
|
2009-01-28 19:32:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
total += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
#define sgtable_ok(x) (!!sgtable_len(x))
|
|
|
|
|
2010-12-15 00:54:01 +00:00
|
|
|
static unsigned max_alignment(u32 addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, };
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pagesize) && addr & (pagesize[i] - 1); i++)
|
|
|
|
;
|
|
|
|
return (i < ARRAY_SIZE(pagesize)) ? pagesize[i] : 0;
|
|
|
|
}
|
|
|
|
|
2009-01-28 19:32:08 +00:00
|
|
|
/*
|
|
|
|
* calculate the optimal number sg elements from total bytes based on
|
|
|
|
* iommu superpages
|
|
|
|
*/
|
2010-12-15 00:54:01 +00:00
|
|
|
static unsigned sgtable_nents(size_t bytes, u32 da, u32 pa)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
2010-12-15 00:54:01 +00:00
|
|
|
unsigned nr_entries = 0, ent_sz;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
if (!IS_ALIGNED(bytes, PAGE_SIZE)) {
|
|
|
|
pr_err("%s: wrong size %08x\n", __func__, bytes);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-15 00:54:01 +00:00
|
|
|
while (bytes) {
|
|
|
|
ent_sz = max_alignment(da | pa);
|
|
|
|
ent_sz = min_t(unsigned, ent_sz, iopgsz_max(bytes));
|
|
|
|
nr_entries++;
|
|
|
|
da += ent_sz;
|
|
|
|
pa += ent_sz;
|
|
|
|
bytes -= ent_sz;
|
2009-01-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nr_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate and initialize sg_table header(a kind of 'superblock') */
|
2010-12-15 00:54:01 +00:00
|
|
|
static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags,
|
|
|
|
u32 da, u32 pa)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
unsigned int nr_entries;
|
|
|
|
int err;
|
|
|
|
struct sg_table *sgt;
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
if (!IS_ALIGNED(bytes, PAGE_SIZE))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2010-12-15 00:54:01 +00:00
|
|
|
if (flags & IOVMF_LINEAR) {
|
|
|
|
nr_entries = sgtable_nents(bytes, da, pa);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (!nr_entries)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
} else
|
|
|
|
nr_entries = bytes / PAGE_SIZE;
|
|
|
|
|
|
|
|
sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
|
|
|
|
if (!sgt)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL);
|
2010-06-09 10:21:27 +00:00
|
|
|
if (err) {
|
|
|
|
kfree(sgt);
|
2009-01-28 19:32:08 +00:00
|
|
|
return ERR_PTR(err);
|
2010-06-09 10:21:27 +00:00
|
|
|
}
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries);
|
|
|
|
|
|
|
|
return sgt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free sg_table header(a kind of superblock) */
|
|
|
|
static void sgtable_free(struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
if (!sgt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sg_free_table(sgt);
|
|
|
|
kfree(sgt);
|
|
|
|
|
|
|
|
pr_debug("%s: sgt:%p\n", __func__, sgt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* map 'sglist' to a contiguous mpu virtual area and return 'va' */
|
|
|
|
static void *vmap_sg(const struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
u32 va;
|
|
|
|
size_t total;
|
|
|
|
unsigned int i;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
struct vm_struct *new;
|
|
|
|
const struct mem_type *mtype;
|
|
|
|
|
|
|
|
mtype = get_mem_type(MT_DEVICE);
|
|
|
|
if (!mtype)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
total = sgtable_len(sgt);
|
|
|
|
if (!total)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
|
|
|
|
if (!new)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
va = (u32)new->addr;
|
|
|
|
|
|
|
|
for_each_sg(sgt->sgl, sg, sgt->nents, i) {
|
|
|
|
size_t bytes;
|
|
|
|
u32 pa;
|
|
|
|
int err;
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
pa = sg_phys(sg) - sg->offset;
|
|
|
|
bytes = sg->length + sg->offset;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
BUG_ON(bytes != PAGE_SIZE);
|
|
|
|
|
|
|
|
err = ioremap_page(va, pa, mtype);
|
|
|
|
if (err)
|
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
va += bytes;
|
|
|
|
}
|
|
|
|
|
2009-09-24 23:23:12 +00:00
|
|
|
flush_cache_vmap((unsigned long)new->addr,
|
|
|
|
(unsigned long)(new->addr + total));
|
2009-01-28 19:32:08 +00:00
|
|
|
return new->addr;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
WARN_ON(1); /* FIXME: cleanup some mpu mappings */
|
|
|
|
vunmap(new->addr);
|
|
|
|
return ERR_PTR(-EAGAIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vunmap_sg(const void *va)
|
|
|
|
{
|
|
|
|
vunmap(va);
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:57:56 +00:00
|
|
|
static struct iovm_struct *__find_iovm_area(struct omap_iommu *obj,
|
|
|
|
const u32 da)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
struct iovm_struct *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry(tmp, &obj->mmap, list) {
|
|
|
|
if ((da >= tmp->da_start) && (da < tmp->da_end)) {
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = tmp->da_end - tmp->da_start;
|
|
|
|
|
|
|
|
dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n",
|
|
|
|
__func__, tmp->da_start, da, tmp->da_end, len,
|
|
|
|
tmp->flags);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-17 19:57:56 +00:00
|
|
|
* omap_find_iovm_area - find iovma which includes @da
|
2009-01-28 19:32:08 +00:00
|
|
|
* @da: iommu device virtual address
|
|
|
|
*
|
|
|
|
* Find the existing iovma starting at @da
|
|
|
|
*/
|
2011-08-17 19:57:56 +00:00
|
|
|
struct iovm_struct *omap_find_iovm_area(struct omap_iommu *obj, u32 da)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
struct iovm_struct *area;
|
|
|
|
|
|
|
|
mutex_lock(&obj->mmap_lock);
|
|
|
|
area = __find_iovm_area(obj, da);
|
|
|
|
mutex_unlock(&obj->mmap_lock);
|
|
|
|
|
|
|
|
return area;
|
|
|
|
}
|
2011-08-17 19:57:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(omap_find_iovm_area);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This finds the hole(area) which fits the requested address and len
|
|
|
|
* in iovmas mmap, and returns the new allocated iovma.
|
|
|
|
*/
|
2011-08-17 19:57:56 +00:00
|
|
|
static struct iovm_struct *alloc_iovm_area(struct omap_iommu *obj, u32 da,
|
2009-01-28 19:32:08 +00:00
|
|
|
size_t bytes, u32 flags)
|
|
|
|
{
|
|
|
|
struct iovm_struct *new, *tmp;
|
2011-03-09 09:17:32 +00:00
|
|
|
u32 start, prev_end, alignment;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
if (!obj || !bytes)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
start = da;
|
2011-03-09 09:17:32 +00:00
|
|
|
alignment = PAGE_SIZE;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
2011-03-09 09:17:33 +00:00
|
|
|
if (~flags & IOVMF_DA_FIXED) {
|
2011-03-09 09:17:32 +00:00
|
|
|
/* Don't map address 0 */
|
|
|
|
start = obj->da_start ? obj->da_start : alignment;
|
2010-12-15 00:54:03 +00:00
|
|
|
|
2009-01-28 19:32:08 +00:00
|
|
|
if (flags & IOVMF_LINEAR)
|
2011-03-09 09:17:32 +00:00
|
|
|
alignment = iopgsz_max(bytes);
|
|
|
|
start = roundup(start, alignment);
|
2010-12-15 00:54:03 +00:00
|
|
|
} else if (start < obj->da_start || start > obj->da_end ||
|
|
|
|
obj->da_end - start < bytes) {
|
|
|
|
return ERR_PTR(-EINVAL);
|
2009-01-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp = NULL;
|
|
|
|
if (list_empty(&obj->mmap))
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
prev_end = 0;
|
|
|
|
list_for_each_entry(tmp, &obj->mmap, list) {
|
|
|
|
|
2010-12-15 00:54:00 +00:00
|
|
|
if (prev_end > start)
|
2010-05-06 14:09:25 +00:00
|
|
|
break;
|
|
|
|
|
2010-12-15 00:54:03 +00:00
|
|
|
if (tmp->da_start > start && (tmp->da_start - start) >= bytes)
|
2009-01-28 19:32:08 +00:00
|
|
|
goto found;
|
|
|
|
|
2011-03-09 09:17:33 +00:00
|
|
|
if (tmp->da_end >= start && ~flags & IOVMF_DA_FIXED)
|
2011-03-09 09:17:32 +00:00
|
|
|
start = roundup(tmp->da_end + 1, alignment);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
prev_end = tmp->da_end;
|
|
|
|
}
|
|
|
|
|
2010-12-15 00:54:03 +00:00
|
|
|
if ((start >= prev_end) && (obj->da_end - start >= bytes))
|
2009-01-28 19:32:08 +00:00
|
|
|
goto found;
|
|
|
|
|
|
|
|
dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
|
|
|
|
__func__, da, bytes, flags);
|
|
|
|
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
found:
|
|
|
|
new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
|
|
|
|
if (!new)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
new->iommu = obj;
|
|
|
|
new->da_start = start;
|
|
|
|
new->da_end = start + bytes;
|
|
|
|
new->flags = flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* keep ascending order of iovmas
|
|
|
|
*/
|
|
|
|
if (tmp)
|
|
|
|
list_add_tail(&new->list, &tmp->list);
|
|
|
|
else
|
|
|
|
list_add(&new->list, &obj->mmap);
|
|
|
|
|
|
|
|
dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
|
|
|
|
__func__, new->da_start, start, new->da_end, bytes, flags);
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:57:56 +00:00
|
|
|
static void free_iovm_area(struct omap_iommu *obj, struct iovm_struct *area)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
size_t bytes;
|
|
|
|
|
|
|
|
BUG_ON(!obj || !area);
|
|
|
|
|
|
|
|
bytes = area->da_end - area->da_start;
|
|
|
|
|
|
|
|
dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
|
|
|
|
__func__, area->da_start, area->da_end, bytes, area->flags);
|
|
|
|
|
|
|
|
list_del(&area->list);
|
|
|
|
kmem_cache_free(iovm_area_cachep, area);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-17 19:57:56 +00:00
|
|
|
* omap_da_to_va - convert (d) to (v)
|
2009-01-28 19:32:08 +00:00
|
|
|
* @obj: objective iommu
|
|
|
|
* @da: iommu device virtual address
|
|
|
|
* @va: mpu virtual address
|
|
|
|
*
|
|
|
|
* Returns mpu virtual addr which corresponds to a given device virtual addr
|
|
|
|
*/
|
2011-08-17 19:57:56 +00:00
|
|
|
void *omap_da_to_va(struct omap_iommu *obj, u32 da)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
void *va = NULL;
|
|
|
|
struct iovm_struct *area;
|
|
|
|
|
|
|
|
mutex_lock(&obj->mmap_lock);
|
|
|
|
|
|
|
|
area = __find_iovm_area(obj, da);
|
|
|
|
if (!area) {
|
|
|
|
dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
va = area->va;
|
|
|
|
out:
|
2009-10-05 20:31:45 +00:00
|
|
|
mutex_unlock(&obj->mmap_lock);
|
|
|
|
|
2009-01-28 19:32:08 +00:00
|
|
|
return va;
|
|
|
|
}
|
2011-08-17 19:57:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(omap_da_to_va);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
void *va = _va;
|
|
|
|
void *va_end;
|
|
|
|
|
|
|
|
for_each_sg(sgt->sgl, sg, sgt->nents, i) {
|
|
|
|
struct page *pg;
|
|
|
|
const size_t bytes = PAGE_SIZE;
|
|
|
|
|
|
|
|
/*
|
2011-08-17 19:57:56 +00:00
|
|
|
* iommu 'superpage' isn't supported with 'omap_iommu_vmalloc()'
|
2009-01-28 19:32:08 +00:00
|
|
|
*/
|
|
|
|
pg = vmalloc_to_page(va);
|
|
|
|
BUG_ON(!pg);
|
|
|
|
sg_set_page(sg, pg, bytes, 0);
|
|
|
|
|
|
|
|
va += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end = _va + PAGE_SIZE * i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Actually this is not necessary at all, just exists for
|
2009-10-05 20:31:45 +00:00
|
|
|
* consistency of the code readability.
|
2009-01-28 19:32:08 +00:00
|
|
|
*/
|
|
|
|
BUG_ON(!sgt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create 'da' <-> 'pa' mapping from 'sgt' */
|
2011-06-01 22:46:12 +00:00
|
|
|
static int map_iovm_area(struct iommu_domain *domain, struct iovm_struct *new,
|
|
|
|
const struct sg_table *sgt, u32 flags)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
unsigned int i, j;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
u32 da = new->da_start;
|
2011-06-01 22:46:12 +00:00
|
|
|
int order;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
if (!domain || !sgt)
|
2009-01-28 19:32:08 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
BUG_ON(!sgtable_ok(sgt));
|
|
|
|
|
|
|
|
for_each_sg(sgt->sgl, sg, sgt->nents, i) {
|
|
|
|
u32 pa;
|
|
|
|
size_t bytes;
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
pa = sg_phys(sg) - sg->offset;
|
|
|
|
bytes = sg->length + sg->offset;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
flags &= ~IOVMF_PGSZ_MASK;
|
2011-06-01 22:46:12 +00:00
|
|
|
|
|
|
|
if (bytes_to_iopgsz(bytes) < 0)
|
2009-01-28 19:32:08 +00:00
|
|
|
goto err_out;
|
2011-06-01 22:46:12 +00:00
|
|
|
|
|
|
|
order = get_order(bytes);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
|
|
|
|
i, da, pa, bytes);
|
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
err = iommu_map(domain, da, pa, order, flags);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (err)
|
|
|
|
goto err_out;
|
|
|
|
|
|
|
|
da += bytes;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
da = new->da_start;
|
|
|
|
|
|
|
|
for_each_sg(sgt->sgl, sg, i, j) {
|
|
|
|
size_t bytes;
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
bytes = sg->length + sg->offset;
|
2011-06-01 22:46:12 +00:00
|
|
|
order = get_order(bytes);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
/* ignore failures.. we're already handling one */
|
|
|
|
iommu_unmap(domain, da, order);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
da += bytes;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release 'da' <-> 'pa' mapping */
|
2011-08-17 19:57:56 +00:00
|
|
|
static void unmap_iovm_area(struct iommu_domain *domain, struct omap_iommu *obj,
|
2011-06-01 22:46:12 +00:00
|
|
|
struct iovm_struct *area)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
u32 start;
|
|
|
|
size_t total = area->da_end - area->da_start;
|
2011-06-01 22:46:12 +00:00
|
|
|
const struct sg_table *sgt = area->sgt;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i, err;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
BUG_ON(!sgtable_ok(sgt));
|
2009-01-28 19:32:08 +00:00
|
|
|
BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));
|
|
|
|
|
|
|
|
start = area->da_start;
|
2011-06-01 22:46:12 +00:00
|
|
|
for_each_sg(sgt->sgl, sg, sgt->nents, i) {
|
2009-01-28 19:32:08 +00:00
|
|
|
size_t bytes;
|
2011-06-01 22:46:12 +00:00
|
|
|
int order;
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
bytes = sg->length + sg->offset;
|
2011-06-01 22:46:12 +00:00
|
|
|
order = get_order(bytes);
|
|
|
|
|
|
|
|
err = iommu_unmap(domain, start, order);
|
2011-09-02 17:32:33 +00:00
|
|
|
if (err < 0)
|
2011-06-01 22:46:12 +00:00
|
|
|
break;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
|
2009-01-28 19:32:08 +00:00
|
|
|
__func__, start, bytes, area->flags);
|
|
|
|
|
|
|
|
BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));
|
|
|
|
|
|
|
|
total -= bytes;
|
|
|
|
start += bytes;
|
|
|
|
}
|
|
|
|
BUG_ON(total);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* template function for all unmapping */
|
2011-06-01 22:46:12 +00:00
|
|
|
static struct sg_table *unmap_vm_area(struct iommu_domain *domain,
|
2011-08-17 19:57:56 +00:00
|
|
|
struct omap_iommu *obj, const u32 da,
|
2009-01-28 19:32:08 +00:00
|
|
|
void (*fn)(const void *), u32 flags)
|
|
|
|
{
|
|
|
|
struct sg_table *sgt = NULL;
|
|
|
|
struct iovm_struct *area;
|
|
|
|
|
|
|
|
if (!IS_ALIGNED(da, PAGE_SIZE)) {
|
|
|
|
dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&obj->mmap_lock);
|
|
|
|
|
|
|
|
area = __find_iovm_area(obj, da);
|
|
|
|
if (!area) {
|
|
|
|
dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((area->flags & flags) != flags) {
|
|
|
|
dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
|
|
|
|
area->flags);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
sgt = (struct sg_table *)area->sgt;
|
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
unmap_iovm_area(domain, obj, area);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
fn(area->va);
|
|
|
|
|
|
|
|
dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
|
|
|
|
area->da_start, da, area->da_end,
|
|
|
|
area->da_end - area->da_start, area->flags);
|
|
|
|
|
|
|
|
free_iovm_area(obj, area);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&obj->mmap_lock);
|
|
|
|
|
|
|
|
return sgt;
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:57:56 +00:00
|
|
|
static u32 map_iommu_region(struct iommu_domain *domain, struct omap_iommu *obj,
|
2011-06-01 22:46:12 +00:00
|
|
|
u32 da, const struct sg_table *sgt, void *va,
|
|
|
|
size_t bytes, u32 flags)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
int err = -ENOMEM;
|
|
|
|
struct iovm_struct *new;
|
|
|
|
|
|
|
|
mutex_lock(&obj->mmap_lock);
|
|
|
|
|
|
|
|
new = alloc_iovm_area(obj, da, bytes, flags);
|
|
|
|
if (IS_ERR(new)) {
|
|
|
|
err = PTR_ERR(new);
|
|
|
|
goto err_alloc_iovma;
|
|
|
|
}
|
|
|
|
new->va = va;
|
|
|
|
new->sgt = sgt;
|
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
if (map_iovm_area(domain, new, sgt, new->flags))
|
2009-01-28 19:32:08 +00:00
|
|
|
goto err_map;
|
|
|
|
|
|
|
|
mutex_unlock(&obj->mmap_lock);
|
|
|
|
|
|
|
|
dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
|
|
|
|
__func__, new->da_start, bytes, new->flags, va);
|
|
|
|
|
|
|
|
return new->da_start;
|
|
|
|
|
|
|
|
err_map:
|
|
|
|
free_iovm_area(obj, new);
|
|
|
|
err_alloc_iovma:
|
|
|
|
mutex_unlock(&obj->mmap_lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:57:56 +00:00
|
|
|
static inline u32
|
|
|
|
__iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj,
|
2011-06-01 22:46:12 +00:00
|
|
|
u32 da, const struct sg_table *sgt,
|
|
|
|
void *va, size_t bytes, u32 flags)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
2011-06-01 22:46:12 +00:00
|
|
|
return map_iommu_region(domain, obj, da, sgt, va, bytes, flags);
|
2009-01-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-17 19:57:56 +00:00
|
|
|
* omap_iommu_vmap - (d)-(p)-(v) address mapper
|
2009-01-28 19:32:08 +00:00
|
|
|
* @obj: objective iommu
|
|
|
|
* @sgt: address of scatter gather table
|
|
|
|
* @flags: iovma and page property
|
|
|
|
*
|
|
|
|
* Creates 1-n-1 mapping with given @sgt and returns @da.
|
|
|
|
* All @sgt element must be io page size aligned.
|
|
|
|
*/
|
2011-08-17 19:57:56 +00:00
|
|
|
u32 omap_iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj, u32 da,
|
2011-06-01 22:46:12 +00:00
|
|
|
const struct sg_table *sgt, u32 flags)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
size_t bytes;
|
2009-11-22 18:11:02 +00:00
|
|
|
void *va = NULL;
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
if (!obj || !obj->dev || !sgt)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
bytes = sgtable_len(sgt);
|
|
|
|
if (!bytes)
|
|
|
|
return -EINVAL;
|
|
|
|
bytes = PAGE_ALIGN(bytes);
|
|
|
|
|
2009-11-22 18:11:02 +00:00
|
|
|
if (flags & IOVMF_MMIO) {
|
|
|
|
va = vmap_sg(sgt);
|
|
|
|
if (IS_ERR(va))
|
|
|
|
return PTR_ERR(va);
|
|
|
|
}
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
flags |= IOVMF_DISCONT;
|
|
|
|
flags |= IOVMF_MMIO;
|
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (IS_ERR_VALUE(da))
|
|
|
|
vunmap_sg(va);
|
|
|
|
|
2011-09-02 17:32:30 +00:00
|
|
|
return da + sgtable_offset(sgt);
|
2009-01-28 19:32:08 +00:00
|
|
|
}
|
2011-08-17 19:57:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(omap_iommu_vmap);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-17 19:57:56 +00:00
|
|
|
* omap_iommu_vunmap - release virtual mapping obtained by 'omap_iommu_vmap()'
|
2009-01-28 19:32:08 +00:00
|
|
|
* @obj: objective iommu
|
|
|
|
* @da: iommu device virtual address
|
|
|
|
*
|
|
|
|
* Free the iommu virtually contiguous memory area starting at
|
2011-08-17 19:57:56 +00:00
|
|
|
* @da, which was returned by 'omap_iommu_vmap()'.
|
2009-01-28 19:32:08 +00:00
|
|
|
*/
|
2011-06-01 22:46:12 +00:00
|
|
|
struct sg_table *
|
2011-08-17 19:57:56 +00:00
|
|
|
omap_iommu_vunmap(struct iommu_domain *domain, struct omap_iommu *obj, u32 da)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
struct sg_table *sgt;
|
|
|
|
/*
|
2011-08-17 19:57:56 +00:00
|
|
|
* 'sgt' is allocated before 'omap_iommu_vmalloc()' is called.
|
2009-01-28 19:32:08 +00:00
|
|
|
* Just returns 'sgt' to the caller to free
|
|
|
|
*/
|
2011-09-02 17:32:30 +00:00
|
|
|
da &= PAGE_MASK;
|
2011-06-01 22:46:12 +00:00
|
|
|
sgt = unmap_vm_area(domain, obj, da, vunmap_sg,
|
|
|
|
IOVMF_DISCONT | IOVMF_MMIO);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (!sgt)
|
|
|
|
dev_dbg(obj->dev, "%s: No sgt\n", __func__);
|
|
|
|
return sgt;
|
|
|
|
}
|
2011-08-17 19:57:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(omap_iommu_vunmap);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-17 19:57:56 +00:00
|
|
|
* omap_iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
|
2009-01-28 19:32:08 +00:00
|
|
|
* @obj: objective iommu
|
|
|
|
* @da: contiguous iommu virtual memory
|
|
|
|
* @bytes: allocation size
|
|
|
|
* @flags: iovma and page property
|
|
|
|
*
|
|
|
|
* Allocate @bytes linearly and creates 1-n-1 mapping and returns
|
2011-03-09 09:17:33 +00:00
|
|
|
* @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
|
2009-01-28 19:32:08 +00:00
|
|
|
*/
|
2011-08-17 19:57:56 +00:00
|
|
|
u32
|
|
|
|
omap_iommu_vmalloc(struct iommu_domain *domain, struct omap_iommu *obj, u32 da,
|
2011-06-01 22:46:12 +00:00
|
|
|
size_t bytes, u32 flags)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
void *va;
|
|
|
|
struct sg_table *sgt;
|
|
|
|
|
|
|
|
if (!obj || !obj->dev || !bytes)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
bytes = PAGE_ALIGN(bytes);
|
|
|
|
|
|
|
|
va = vmalloc(bytes);
|
|
|
|
if (!va)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-12-15 00:54:01 +00:00
|
|
|
flags |= IOVMF_DISCONT;
|
|
|
|
flags |= IOVMF_ALLOC;
|
|
|
|
|
|
|
|
sgt = sgtable_alloc(bytes, flags, da, 0);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (IS_ERR(sgt)) {
|
|
|
|
da = PTR_ERR(sgt);
|
|
|
|
goto err_sgt_alloc;
|
|
|
|
}
|
|
|
|
sgtable_fill_vmalloc(sgt, va);
|
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (IS_ERR_VALUE(da))
|
|
|
|
goto err_iommu_vmap;
|
|
|
|
|
|
|
|
return da;
|
|
|
|
|
|
|
|
err_iommu_vmap:
|
|
|
|
sgtable_drain_vmalloc(sgt);
|
|
|
|
sgtable_free(sgt);
|
|
|
|
err_sgt_alloc:
|
|
|
|
vfree(va);
|
|
|
|
return da;
|
|
|
|
}
|
2011-08-17 19:57:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(omap_iommu_vmalloc);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
/**
|
2011-08-17 19:57:56 +00:00
|
|
|
* omap_iommu_vfree - release memory allocated by 'omap_iommu_vmalloc()'
|
2009-01-28 19:32:08 +00:00
|
|
|
* @obj: objective iommu
|
|
|
|
* @da: iommu device virtual address
|
|
|
|
*
|
|
|
|
* Frees the iommu virtually continuous memory area starting at
|
2011-08-17 19:57:56 +00:00
|
|
|
* @da, as obtained from 'omap_iommu_vmalloc()'.
|
2009-01-28 19:32:08 +00:00
|
|
|
*/
|
2011-08-17 19:57:56 +00:00
|
|
|
void omap_iommu_vfree(struct iommu_domain *domain, struct omap_iommu *obj,
|
|
|
|
const u32 da)
|
2009-01-28 19:32:08 +00:00
|
|
|
{
|
|
|
|
struct sg_table *sgt;
|
|
|
|
|
2011-06-01 22:46:12 +00:00
|
|
|
sgt = unmap_vm_area(domain, obj, da, vfree,
|
|
|
|
IOVMF_DISCONT | IOVMF_ALLOC);
|
2009-01-28 19:32:08 +00:00
|
|
|
if (!sgt)
|
|
|
|
dev_dbg(obj->dev, "%s: No sgt\n", __func__);
|
|
|
|
sgtable_free(sgt);
|
|
|
|
}
|
2011-08-17 19:57:56 +00:00
|
|
|
EXPORT_SYMBOL_GPL(omap_iommu_vfree);
|
2009-01-28 19:32:08 +00:00
|
|
|
|
|
|
|
static int __init iovmm_init(void)
|
|
|
|
{
|
|
|
|
const unsigned long flags = SLAB_HWCACHE_ALIGN;
|
|
|
|
struct kmem_cache *p;
|
|
|
|
|
|
|
|
p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
|
|
|
|
flags, NULL);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
iovm_area_cachep = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(iovmm_init);
|
|
|
|
|
|
|
|
static void __exit iovmm_exit(void)
|
|
|
|
{
|
|
|
|
kmem_cache_destroy(iovm_area_cachep);
|
|
|
|
}
|
|
|
|
module_exit(iovmm_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
|
|
|
|
MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|