Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 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 | |
Ming Lei | 08f2e63 | 2011-11-08 18:29:15 +0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 14 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/scatterlist.h> |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 19 | #include <linux/iommu.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 20 | |
| 21 | #include <asm/cacheflush.h> |
| 22 | #include <asm/mach/map.h> |
| 23 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 24 | #include <plat/iommu.h> |
| 25 | #include <plat/iovmm.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 26 | |
Ohad Ben-Cohen | fcf3a6e | 2011-08-15 23:21:41 +0300 | [diff] [blame] | 27 | #include <plat/iopgtable.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 28 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 29 | static struct kmem_cache *iovm_area_cachep; |
| 30 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 31 | /* return the offset of the first scatterlist entry in a sg table */ |
| 32 | static 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 DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 40 | /* return total bytes of sg buffers */ |
| 41 | static 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 Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 52 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 53 | |
| 54 | if (!iopgsz_ok(bytes)) { |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 55 | 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 DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | total += bytes; |
| 67 | } |
| 68 | |
| 69 | return total; |
| 70 | } |
| 71 | #define sgtable_ok(x) (!!sgtable_len(x)) |
| 72 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 73 | static 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 DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 82 | /* |
| 83 | * calculate the optimal number sg elements from total bytes based on |
| 84 | * iommu superpages |
| 85 | */ |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 86 | static unsigned sgtable_nents(size_t bytes, u32 da, u32 pa) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 87 | { |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 88 | unsigned nr_entries = 0, ent_sz; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 89 | |
| 90 | if (!IS_ALIGNED(bytes, PAGE_SIZE)) { |
| 91 | pr_err("%s: wrong size %08x\n", __func__, bytes); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 95 | 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 DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 102 | } |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 103 | |
| 104 | return nr_entries; |
| 105 | } |
| 106 | |
| 107 | /* allocate and initialize sg_table header(a kind of 'superblock') */ |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 108 | static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags, |
| 109 | u32 da, u32 pa) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 110 | { |
| 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, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 121 | if (flags & IOVMF_LINEAR) { |
| 122 | nr_entries = sgtable_nents(bytes, da, pa); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 123 | 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); |
Satish | 7f1225b | 2010-06-09 13:21:27 +0300 | [diff] [blame] | 133 | if (err) { |
| 134 | kfree(sgt); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 135 | return ERR_PTR(err); |
Satish | 7f1225b | 2010-06-09 13:21:27 +0300 | [diff] [blame] | 136 | } |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 137 | |
| 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) */ |
| 144 | static 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' */ |
| 156 | static 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 Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 183 | pa = sg_phys(sg) - sg->offset; |
| 184 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 185 | |
| 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 Premi | 6716bd0 | 2009-09-24 16:23:12 -0700 | [diff] [blame] | 195 | flush_cache_vmap((unsigned long)new->addr, |
| 196 | (unsigned long)(new->addr + total)); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 197 | return new->addr; |
| 198 | |
| 199 | err_out: |
| 200 | WARN_ON(1); /* FIXME: cleanup some mpu mappings */ |
| 201 | vunmap(new->addr); |
| 202 | return ERR_PTR(-EAGAIN); |
| 203 | } |
| 204 | |
| 205 | static inline void vunmap_sg(const void *va) |
| 206 | { |
| 207 | vunmap(va); |
| 208 | } |
| 209 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 210 | static struct iovm_struct *__find_iovm_area(struct omap_iommu *obj, |
| 211 | const u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 212 | { |
| 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-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 233 | * omap_find_iovm_area - find iovma which includes @da |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 234 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 235 | * @da: iommu device virtual address |
| 236 | * |
| 237 | * Find the existing iovma starting at @da |
| 238 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 239 | struct iovm_struct *omap_find_iovm_area(struct device *dev, u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 240 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 241 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 242 | struct iovm_struct *area; |
| 243 | |
| 244 | mutex_lock(&obj->mmap_lock); |
| 245 | area = __find_iovm_area(obj, da); |
| 246 | mutex_unlock(&obj->mmap_lock); |
| 247 | |
| 248 | return area; |
| 249 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 250 | EXPORT_SYMBOL_GPL(omap_find_iovm_area); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * This finds the hole(area) which fits the requested address and len |
| 254 | * in iovmas mmap, and returns the new allocated iovma. |
| 255 | */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 256 | static struct iovm_struct *alloc_iovm_area(struct omap_iommu *obj, u32 da, |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 257 | size_t bytes, u32 flags) |
| 258 | { |
| 259 | struct iovm_struct *new, *tmp; |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 260 | u32 start, prev_end, alignment; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 261 | |
| 262 | if (!obj || !bytes) |
| 263 | return ERR_PTR(-EINVAL); |
| 264 | |
| 265 | start = da; |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 266 | alignment = PAGE_SIZE; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 267 | |
David Cohen | d038aee | 2011-03-09 09:17:33 +0000 | [diff] [blame] | 268 | if (~flags & IOVMF_DA_FIXED) { |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 269 | /* Don't map address 0 */ |
| 270 | start = obj->da_start ? obj->da_start : alignment; |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 271 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 272 | if (flags & IOVMF_LINEAR) |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 273 | alignment = iopgsz_max(bytes); |
| 274 | start = roundup(start, alignment); |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 275 | } else if (start < obj->da_start || start > obj->da_end || |
| 276 | obj->da_end - start < bytes) { |
| 277 | return ERR_PTR(-EINVAL); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | tmp = NULL; |
| 281 | if (list_empty(&obj->mmap)) |
| 282 | goto found; |
| 283 | |
| 284 | prev_end = 0; |
| 285 | list_for_each_entry(tmp, &obj->mmap, list) { |
| 286 | |
Guzman Lugo, Fernando | ba6e1f4 | 2010-12-15 00:54:00 +0000 | [diff] [blame] | 287 | if (prev_end > start) |
Hiroshi DOYU | e0a42e4 | 2010-05-06 17:09:25 +0300 | [diff] [blame] | 288 | break; |
| 289 | |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 290 | if (tmp->da_start > start && (tmp->da_start - start) >= bytes) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 291 | goto found; |
| 292 | |
David Cohen | d038aee | 2011-03-09 09:17:33 +0000 | [diff] [blame] | 293 | if (tmp->da_end >= start && ~flags & IOVMF_DA_FIXED) |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 294 | start = roundup(tmp->da_end + 1, alignment); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 295 | |
| 296 | prev_end = tmp->da_end; |
| 297 | } |
| 298 | |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 299 | if ((start >= prev_end) && (obj->da_end - start >= bytes)) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 300 | goto found; |
| 301 | |
| 302 | dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n", |
| 303 | __func__, da, bytes, flags); |
| 304 | |
| 305 | return ERR_PTR(-EINVAL); |
| 306 | |
| 307 | found: |
| 308 | new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL); |
| 309 | if (!new) |
| 310 | return ERR_PTR(-ENOMEM); |
| 311 | |
| 312 | new->iommu = obj; |
| 313 | new->da_start = start; |
| 314 | new->da_end = start + bytes; |
| 315 | new->flags = flags; |
| 316 | |
| 317 | /* |
| 318 | * keep ascending order of iovmas |
| 319 | */ |
| 320 | if (tmp) |
| 321 | list_add_tail(&new->list, &tmp->list); |
| 322 | else |
| 323 | list_add(&new->list, &obj->mmap); |
| 324 | |
| 325 | dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n", |
| 326 | __func__, new->da_start, start, new->da_end, bytes, flags); |
| 327 | |
| 328 | return new; |
| 329 | } |
| 330 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 331 | static void free_iovm_area(struct omap_iommu *obj, struct iovm_struct *area) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 332 | { |
| 333 | size_t bytes; |
| 334 | |
| 335 | BUG_ON(!obj || !area); |
| 336 | |
| 337 | bytes = area->da_end - area->da_start; |
| 338 | |
| 339 | dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n", |
| 340 | __func__, area->da_start, area->da_end, bytes, area->flags); |
| 341 | |
| 342 | list_del(&area->list); |
| 343 | kmem_cache_free(iovm_area_cachep, area); |
| 344 | } |
| 345 | |
| 346 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 347 | * omap_da_to_va - convert (d) to (v) |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 348 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 349 | * @da: iommu device virtual address |
| 350 | * @va: mpu virtual address |
| 351 | * |
| 352 | * Returns mpu virtual addr which corresponds to a given device virtual addr |
| 353 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 354 | void *omap_da_to_va(struct device *dev, u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 355 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 356 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 357 | void *va = NULL; |
| 358 | struct iovm_struct *area; |
| 359 | |
| 360 | mutex_lock(&obj->mmap_lock); |
| 361 | |
| 362 | area = __find_iovm_area(obj, da); |
| 363 | if (!area) { |
| 364 | dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da); |
| 365 | goto out; |
| 366 | } |
| 367 | va = area->va; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 368 | out: |
Daniel Walker | 2654890 | 2009-10-05 13:31:45 -0700 | [diff] [blame] | 369 | mutex_unlock(&obj->mmap_lock); |
| 370 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 371 | return va; |
| 372 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 373 | EXPORT_SYMBOL_GPL(omap_da_to_va); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 374 | |
| 375 | static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va) |
| 376 | { |
| 377 | unsigned int i; |
| 378 | struct scatterlist *sg; |
| 379 | void *va = _va; |
| 380 | void *va_end; |
| 381 | |
| 382 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 383 | struct page *pg; |
| 384 | const size_t bytes = PAGE_SIZE; |
| 385 | |
| 386 | /* |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 387 | * iommu 'superpage' isn't supported with 'omap_iommu_vmalloc()' |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 388 | */ |
| 389 | pg = vmalloc_to_page(va); |
| 390 | BUG_ON(!pg); |
| 391 | sg_set_page(sg, pg, bytes, 0); |
| 392 | |
| 393 | va += bytes; |
| 394 | } |
| 395 | |
| 396 | va_end = _va + PAGE_SIZE * i; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static inline void sgtable_drain_vmalloc(struct sg_table *sgt) |
| 400 | { |
| 401 | /* |
| 402 | * Actually this is not necessary at all, just exists for |
Hiroshi DOYU | ba6a117 | 2009-10-05 13:31:45 -0700 | [diff] [blame] | 403 | * consistency of the code readability. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 404 | */ |
| 405 | BUG_ON(!sgt); |
| 406 | } |
| 407 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 408 | /* create 'da' <-> 'pa' mapping from 'sgt' */ |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 409 | static int map_iovm_area(struct iommu_domain *domain, struct iovm_struct *new, |
| 410 | const struct sg_table *sgt, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 411 | { |
| 412 | int err; |
| 413 | unsigned int i, j; |
| 414 | struct scatterlist *sg; |
| 415 | u32 da = new->da_start; |
| 416 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 417 | if (!domain || !sgt) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 418 | return -EINVAL; |
| 419 | |
| 420 | BUG_ON(!sgtable_ok(sgt)); |
| 421 | |
| 422 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 423 | u32 pa; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 424 | size_t bytes; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 425 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 426 | pa = sg_phys(sg) - sg->offset; |
| 427 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 428 | |
| 429 | flags &= ~IOVMF_PGSZ_MASK; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 430 | |
| 431 | if (bytes_to_iopgsz(bytes) < 0) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 432 | goto err_out; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 433 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 434 | pr_debug("%s: [%d] %08x %08x(%x)\n", __func__, |
| 435 | i, da, pa, bytes); |
| 436 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 437 | err = iommu_map(domain, da, pa, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 438 | if (err) |
| 439 | goto err_out; |
| 440 | |
| 441 | da += bytes; |
| 442 | } |
| 443 | return 0; |
| 444 | |
| 445 | err_out: |
| 446 | da = new->da_start; |
| 447 | |
| 448 | for_each_sg(sgt->sgl, sg, i, j) { |
| 449 | size_t bytes; |
| 450 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 451 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 452 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 453 | /* ignore failures.. we're already handling one */ |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 454 | iommu_unmap(domain, da, bytes); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 455 | |
| 456 | da += bytes; |
| 457 | } |
| 458 | return err; |
| 459 | } |
| 460 | |
| 461 | /* release 'da' <-> 'pa' mapping */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 462 | static void unmap_iovm_area(struct iommu_domain *domain, struct omap_iommu *obj, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 463 | struct iovm_struct *area) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 464 | { |
| 465 | u32 start; |
| 466 | size_t total = area->da_end - area->da_start; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 467 | const struct sg_table *sgt = area->sgt; |
| 468 | struct scatterlist *sg; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 469 | int i; |
| 470 | size_t unmapped; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 471 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 472 | BUG_ON(!sgtable_ok(sgt)); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 473 | BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE)); |
| 474 | |
| 475 | start = area->da_start; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 476 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 477 | size_t bytes; |
| 478 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 479 | bytes = sg->length + sg->offset; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 480 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 481 | unmapped = iommu_unmap(domain, start, bytes); |
| 482 | if (unmapped < bytes) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 483 | break; |
| 484 | |
| 485 | dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n", |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 486 | __func__, start, bytes, area->flags); |
| 487 | |
| 488 | BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE)); |
| 489 | |
| 490 | total -= bytes; |
| 491 | start += bytes; |
| 492 | } |
| 493 | BUG_ON(total); |
| 494 | } |
| 495 | |
| 496 | /* template function for all unmapping */ |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 497 | static struct sg_table *unmap_vm_area(struct iommu_domain *domain, |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 498 | struct omap_iommu *obj, const u32 da, |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 499 | void (*fn)(const void *), u32 flags) |
| 500 | { |
| 501 | struct sg_table *sgt = NULL; |
| 502 | struct iovm_struct *area; |
| 503 | |
| 504 | if (!IS_ALIGNED(da, PAGE_SIZE)) { |
| 505 | dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | mutex_lock(&obj->mmap_lock); |
| 510 | |
| 511 | area = __find_iovm_area(obj, da); |
| 512 | if (!area) { |
| 513 | dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da); |
| 514 | goto out; |
| 515 | } |
| 516 | |
| 517 | if ((area->flags & flags) != flags) { |
| 518 | dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__, |
| 519 | area->flags); |
| 520 | goto out; |
| 521 | } |
| 522 | sgt = (struct sg_table *)area->sgt; |
| 523 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 524 | unmap_iovm_area(domain, obj, area); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 525 | |
| 526 | fn(area->va); |
| 527 | |
| 528 | dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__, |
| 529 | area->da_start, da, area->da_end, |
| 530 | area->da_end - area->da_start, area->flags); |
| 531 | |
| 532 | free_iovm_area(obj, area); |
| 533 | out: |
| 534 | mutex_unlock(&obj->mmap_lock); |
| 535 | |
| 536 | return sgt; |
| 537 | } |
| 538 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 539 | static u32 map_iommu_region(struct iommu_domain *domain, struct omap_iommu *obj, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 540 | u32 da, const struct sg_table *sgt, void *va, |
| 541 | size_t bytes, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 542 | { |
| 543 | int err = -ENOMEM; |
| 544 | struct iovm_struct *new; |
| 545 | |
| 546 | mutex_lock(&obj->mmap_lock); |
| 547 | |
| 548 | new = alloc_iovm_area(obj, da, bytes, flags); |
| 549 | if (IS_ERR(new)) { |
| 550 | err = PTR_ERR(new); |
| 551 | goto err_alloc_iovma; |
| 552 | } |
| 553 | new->va = va; |
| 554 | new->sgt = sgt; |
| 555 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 556 | if (map_iovm_area(domain, new, sgt, new->flags)) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 557 | goto err_map; |
| 558 | |
| 559 | mutex_unlock(&obj->mmap_lock); |
| 560 | |
| 561 | dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n", |
| 562 | __func__, new->da_start, bytes, new->flags, va); |
| 563 | |
| 564 | return new->da_start; |
| 565 | |
| 566 | err_map: |
| 567 | free_iovm_area(obj, new); |
| 568 | err_alloc_iovma: |
| 569 | mutex_unlock(&obj->mmap_lock); |
| 570 | return err; |
| 571 | } |
| 572 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 573 | static inline u32 |
| 574 | __iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 575 | u32 da, const struct sg_table *sgt, |
| 576 | void *va, size_t bytes, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 577 | { |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 578 | return map_iommu_region(domain, obj, da, sgt, va, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 582 | * omap_iommu_vmap - (d)-(p)-(v) address mapper |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 583 | * @domain: iommu domain |
| 584 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 585 | * @sgt: address of scatter gather table |
| 586 | * @flags: iovma and page property |
| 587 | * |
| 588 | * Creates 1-n-1 mapping with given @sgt and returns @da. |
| 589 | * All @sgt element must be io page size aligned. |
| 590 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 591 | u32 omap_iommu_vmap(struct iommu_domain *domain, struct device *dev, u32 da, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 592 | const struct sg_table *sgt, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 593 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 594 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 595 | size_t bytes; |
Hiroshi DOYU | 935e473 | 2009-11-22 10:11:02 -0800 | [diff] [blame] | 596 | void *va = NULL; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 597 | |
| 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 DOYU | 935e473 | 2009-11-22 10:11:02 -0800 | [diff] [blame] | 606 | if (flags & IOVMF_MMIO) { |
| 607 | va = vmap_sg(sgt); |
| 608 | if (IS_ERR(va)) |
| 609 | return PTR_ERR(va); |
| 610 | } |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 611 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 612 | flags |= IOVMF_DISCONT; |
| 613 | flags |= IOVMF_MMIO; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 614 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 615 | da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 616 | if (IS_ERR_VALUE(da)) |
| 617 | vunmap_sg(va); |
| 618 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 619 | return da + sgtable_offset(sgt); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 620 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 621 | EXPORT_SYMBOL_GPL(omap_iommu_vmap); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 622 | |
| 623 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 624 | * omap_iommu_vunmap - release virtual mapping obtained by 'omap_iommu_vmap()' |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 625 | * @domain: iommu domain |
| 626 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 627 | * @da: iommu device virtual address |
| 628 | * |
| 629 | * Free the iommu virtually contiguous memory area starting at |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 630 | * @da, which was returned by 'omap_iommu_vmap()'. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 631 | */ |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 632 | struct sg_table * |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 633 | omap_iommu_vunmap(struct iommu_domain *domain, struct device *dev, u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 634 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 635 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 636 | struct sg_table *sgt; |
| 637 | /* |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 638 | * 'sgt' is allocated before 'omap_iommu_vmalloc()' is called. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 639 | * Just returns 'sgt' to the caller to free |
| 640 | */ |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 641 | da &= PAGE_MASK; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 642 | sgt = unmap_vm_area(domain, obj, da, vunmap_sg, |
| 643 | IOVMF_DISCONT | IOVMF_MMIO); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 644 | if (!sgt) |
| 645 | dev_dbg(obj->dev, "%s: No sgt\n", __func__); |
| 646 | return sgt; |
| 647 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 648 | EXPORT_SYMBOL_GPL(omap_iommu_vunmap); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 649 | |
| 650 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 651 | * omap_iommu_vmalloc - (d)-(p)-(v) address allocator and mapper |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 652 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 653 | * @da: contiguous iommu virtual memory |
| 654 | * @bytes: allocation size |
| 655 | * @flags: iovma and page property |
| 656 | * |
| 657 | * Allocate @bytes linearly and creates 1-n-1 mapping and returns |
David Cohen | d038aee | 2011-03-09 09:17:33 +0000 | [diff] [blame] | 658 | * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 659 | */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 660 | u32 |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 661 | omap_iommu_vmalloc(struct iommu_domain *domain, struct device *dev, u32 da, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 662 | size_t bytes, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 663 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 664 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 665 | void *va; |
| 666 | struct sg_table *sgt; |
| 667 | |
| 668 | if (!obj || !obj->dev || !bytes) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | bytes = PAGE_ALIGN(bytes); |
| 672 | |
| 673 | va = vmalloc(bytes); |
| 674 | if (!va) |
| 675 | return -ENOMEM; |
| 676 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 677 | flags |= IOVMF_DISCONT; |
| 678 | flags |= IOVMF_ALLOC; |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 679 | |
| 680 | sgt = sgtable_alloc(bytes, flags, da, 0); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 681 | if (IS_ERR(sgt)) { |
| 682 | da = PTR_ERR(sgt); |
| 683 | goto err_sgt_alloc; |
| 684 | } |
| 685 | sgtable_fill_vmalloc(sgt, va); |
| 686 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 687 | da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 688 | if (IS_ERR_VALUE(da)) |
| 689 | goto err_iommu_vmap; |
| 690 | |
| 691 | return da; |
| 692 | |
| 693 | err_iommu_vmap: |
| 694 | sgtable_drain_vmalloc(sgt); |
| 695 | sgtable_free(sgt); |
| 696 | err_sgt_alloc: |
| 697 | vfree(va); |
| 698 | return da; |
| 699 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 700 | EXPORT_SYMBOL_GPL(omap_iommu_vmalloc); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 701 | |
| 702 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 703 | * omap_iommu_vfree - release memory allocated by 'omap_iommu_vmalloc()' |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 704 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 705 | * @da: iommu device virtual address |
| 706 | * |
| 707 | * Frees the iommu virtually continuous memory area starting at |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 708 | * @da, as obtained from 'omap_iommu_vmalloc()'. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 709 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 710 | void omap_iommu_vfree(struct iommu_domain *domain, struct device *dev, |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 711 | const u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 712 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 713 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 714 | struct sg_table *sgt; |
| 715 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 716 | sgt = unmap_vm_area(domain, obj, da, vfree, |
| 717 | IOVMF_DISCONT | IOVMF_ALLOC); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 718 | if (!sgt) |
| 719 | dev_dbg(obj->dev, "%s: No sgt\n", __func__); |
| 720 | sgtable_free(sgt); |
| 721 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 722 | EXPORT_SYMBOL_GPL(omap_iommu_vfree); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 723 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 724 | static int __init iovmm_init(void) |
| 725 | { |
| 726 | const unsigned long flags = SLAB_HWCACHE_ALIGN; |
| 727 | struct kmem_cache *p; |
| 728 | |
| 729 | p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0, |
| 730 | flags, NULL); |
| 731 | if (!p) |
| 732 | return -ENOMEM; |
| 733 | iovm_area_cachep = p; |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | module_init(iovmm_init); |
| 738 | |
| 739 | static void __exit iovmm_exit(void) |
| 740 | { |
| 741 | kmem_cache_destroy(iovm_area_cachep); |
| 742 | } |
| 743 | module_exit(iovmm_exit); |
| 744 | |
| 745 | MODULE_DESCRIPTION("omap iommu: simple virtual address space management"); |
| 746 | MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>"); |
| 747 | MODULE_LICENSE("GPL v2"); |