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> |
Tony Lindgren | c8d35c8 | 2012-11-02 12:24:03 -0700 | [diff] [blame] | 20 | #include <linux/omap-iommu.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 21 | |
| 22 | #include <asm/cacheflush.h> |
| 23 | #include <asm/mach/map.h> |
| 24 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 25 | #include <plat/iommu.h> |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 26 | |
Ido Yariv | 2f7702a | 2012-11-02 12:24:00 -0700 | [diff] [blame] | 27 | #include "omap-iopgtable.h" |
Tony Lindgren | ed1c7de | 2012-11-02 12:24:06 -0700 | [diff] [blame] | 28 | #include "omap-iommu.h" |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 29 | |
Tony Lindgren | c8d35c8 | 2012-11-02 12:24:03 -0700 | [diff] [blame] | 30 | /* |
| 31 | * IOVMF_FLAGS: attribute for iommu virtual memory area(iovma) |
| 32 | * |
| 33 | * lower 16 bit is used for h/w and upper 16 bit is for s/w. |
| 34 | */ |
| 35 | #define IOVMF_SW_SHIFT 16 |
| 36 | |
| 37 | /* |
| 38 | * iovma: h/w flags derived from cam and ram attribute |
| 39 | */ |
| 40 | #define IOVMF_CAM_MASK (~((1 << 10) - 1)) |
| 41 | #define IOVMF_RAM_MASK (~IOVMF_CAM_MASK) |
| 42 | |
| 43 | #define IOVMF_PGSZ_MASK (3 << 0) |
| 44 | #define IOVMF_PGSZ_1M MMU_CAM_PGSZ_1M |
| 45 | #define IOVMF_PGSZ_64K MMU_CAM_PGSZ_64K |
| 46 | #define IOVMF_PGSZ_4K MMU_CAM_PGSZ_4K |
| 47 | #define IOVMF_PGSZ_16M MMU_CAM_PGSZ_16M |
| 48 | |
| 49 | #define IOVMF_ENDIAN_MASK (1 << 9) |
| 50 | #define IOVMF_ENDIAN_BIG MMU_RAM_ENDIAN_BIG |
| 51 | |
| 52 | #define IOVMF_ELSZ_MASK (3 << 7) |
| 53 | #define IOVMF_ELSZ_16 MMU_RAM_ELSZ_16 |
| 54 | #define IOVMF_ELSZ_32 MMU_RAM_ELSZ_32 |
| 55 | #define IOVMF_ELSZ_NONE MMU_RAM_ELSZ_NONE |
| 56 | |
| 57 | #define IOVMF_MIXED_MASK (1 << 6) |
| 58 | #define IOVMF_MIXED MMU_RAM_MIXED |
| 59 | |
| 60 | /* |
| 61 | * iovma: s/w flags, used for mapping and umapping internally. |
| 62 | */ |
| 63 | #define IOVMF_MMIO (1 << IOVMF_SW_SHIFT) |
| 64 | #define IOVMF_ALLOC (2 << IOVMF_SW_SHIFT) |
| 65 | #define IOVMF_ALLOC_MASK (3 << IOVMF_SW_SHIFT) |
| 66 | |
| 67 | /* "superpages" is supported just with physically linear pages */ |
| 68 | #define IOVMF_DISCONT (1 << (2 + IOVMF_SW_SHIFT)) |
| 69 | #define IOVMF_LINEAR (2 << (2 + IOVMF_SW_SHIFT)) |
| 70 | #define IOVMF_LINEAR_MASK (3 << (2 + IOVMF_SW_SHIFT)) |
| 71 | |
| 72 | #define IOVMF_DA_FIXED (1 << (4 + IOVMF_SW_SHIFT)) |
| 73 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 74 | static struct kmem_cache *iovm_area_cachep; |
| 75 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 76 | /* return the offset of the first scatterlist entry in a sg table */ |
| 77 | static unsigned int sgtable_offset(const struct sg_table *sgt) |
| 78 | { |
| 79 | if (!sgt || !sgt->nents) |
| 80 | return 0; |
| 81 | |
| 82 | return sgt->sgl->offset; |
| 83 | } |
| 84 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 85 | /* return total bytes of sg buffers */ |
| 86 | static size_t sgtable_len(const struct sg_table *sgt) |
| 87 | { |
| 88 | unsigned int i, total = 0; |
| 89 | struct scatterlist *sg; |
| 90 | |
| 91 | if (!sgt) |
| 92 | return 0; |
| 93 | |
| 94 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 95 | size_t bytes; |
| 96 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 97 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 98 | |
| 99 | if (!iopgsz_ok(bytes)) { |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 100 | pr_err("%s: sg[%d] not iommu pagesize(%u %u)\n", |
| 101 | __func__, i, bytes, sg->offset); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | if (i && sg->offset) { |
| 106 | pr_err("%s: sg[%d] offset not allowed in internal " |
| 107 | "entries\n", __func__, i); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | total += bytes; |
| 112 | } |
| 113 | |
| 114 | return total; |
| 115 | } |
| 116 | #define sgtable_ok(x) (!!sgtable_len(x)) |
| 117 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 118 | static unsigned max_alignment(u32 addr) |
| 119 | { |
| 120 | int i; |
| 121 | unsigned pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, }; |
| 122 | for (i = 0; i < ARRAY_SIZE(pagesize) && addr & (pagesize[i] - 1); i++) |
| 123 | ; |
| 124 | return (i < ARRAY_SIZE(pagesize)) ? pagesize[i] : 0; |
| 125 | } |
| 126 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 127 | /* |
| 128 | * calculate the optimal number sg elements from total bytes based on |
| 129 | * iommu superpages |
| 130 | */ |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 131 | static unsigned sgtable_nents(size_t bytes, u32 da, u32 pa) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 132 | { |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 133 | unsigned nr_entries = 0, ent_sz; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 134 | |
| 135 | if (!IS_ALIGNED(bytes, PAGE_SIZE)) { |
| 136 | pr_err("%s: wrong size %08x\n", __func__, bytes); |
| 137 | return 0; |
| 138 | } |
| 139 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 140 | while (bytes) { |
| 141 | ent_sz = max_alignment(da | pa); |
| 142 | ent_sz = min_t(unsigned, ent_sz, iopgsz_max(bytes)); |
| 143 | nr_entries++; |
| 144 | da += ent_sz; |
| 145 | pa += ent_sz; |
| 146 | bytes -= ent_sz; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 147 | } |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 148 | |
| 149 | return nr_entries; |
| 150 | } |
| 151 | |
| 152 | /* allocate and initialize sg_table header(a kind of 'superblock') */ |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 153 | static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags, |
| 154 | u32 da, u32 pa) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 155 | { |
| 156 | unsigned int nr_entries; |
| 157 | int err; |
| 158 | struct sg_table *sgt; |
| 159 | |
| 160 | if (!bytes) |
| 161 | return ERR_PTR(-EINVAL); |
| 162 | |
| 163 | if (!IS_ALIGNED(bytes, PAGE_SIZE)) |
| 164 | return ERR_PTR(-EINVAL); |
| 165 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 166 | if (flags & IOVMF_LINEAR) { |
| 167 | nr_entries = sgtable_nents(bytes, da, pa); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 168 | if (!nr_entries) |
| 169 | return ERR_PTR(-EINVAL); |
| 170 | } else |
| 171 | nr_entries = bytes / PAGE_SIZE; |
| 172 | |
| 173 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 174 | if (!sgt) |
| 175 | return ERR_PTR(-ENOMEM); |
| 176 | |
| 177 | err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL); |
Satish | 7f1225b | 2010-06-09 13:21:27 +0300 | [diff] [blame] | 178 | if (err) { |
| 179 | kfree(sgt); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 180 | return ERR_PTR(err); |
Satish | 7f1225b | 2010-06-09 13:21:27 +0300 | [diff] [blame] | 181 | } |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 182 | |
| 183 | pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries); |
| 184 | |
| 185 | return sgt; |
| 186 | } |
| 187 | |
| 188 | /* free sg_table header(a kind of superblock) */ |
| 189 | static void sgtable_free(struct sg_table *sgt) |
| 190 | { |
| 191 | if (!sgt) |
| 192 | return; |
| 193 | |
| 194 | sg_free_table(sgt); |
| 195 | kfree(sgt); |
| 196 | |
| 197 | pr_debug("%s: sgt:%p\n", __func__, sgt); |
| 198 | } |
| 199 | |
| 200 | /* map 'sglist' to a contiguous mpu virtual area and return 'va' */ |
| 201 | static void *vmap_sg(const struct sg_table *sgt) |
| 202 | { |
| 203 | u32 va; |
| 204 | size_t total; |
| 205 | unsigned int i; |
| 206 | struct scatterlist *sg; |
| 207 | struct vm_struct *new; |
| 208 | const struct mem_type *mtype; |
| 209 | |
| 210 | mtype = get_mem_type(MT_DEVICE); |
| 211 | if (!mtype) |
| 212 | return ERR_PTR(-EINVAL); |
| 213 | |
| 214 | total = sgtable_len(sgt); |
| 215 | if (!total) |
| 216 | return ERR_PTR(-EINVAL); |
| 217 | |
| 218 | new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END); |
| 219 | if (!new) |
| 220 | return ERR_PTR(-ENOMEM); |
| 221 | va = (u32)new->addr; |
| 222 | |
| 223 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 224 | size_t bytes; |
| 225 | u32 pa; |
| 226 | int err; |
| 227 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 228 | pa = sg_phys(sg) - sg->offset; |
| 229 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 230 | |
| 231 | BUG_ON(bytes != PAGE_SIZE); |
| 232 | |
| 233 | err = ioremap_page(va, pa, mtype); |
| 234 | if (err) |
| 235 | goto err_out; |
| 236 | |
| 237 | va += bytes; |
| 238 | } |
| 239 | |
Sanjeev Premi | 6716bd0 | 2009-09-24 16:23:12 -0700 | [diff] [blame] | 240 | flush_cache_vmap((unsigned long)new->addr, |
| 241 | (unsigned long)(new->addr + total)); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 242 | return new->addr; |
| 243 | |
| 244 | err_out: |
| 245 | WARN_ON(1); /* FIXME: cleanup some mpu mappings */ |
| 246 | vunmap(new->addr); |
| 247 | return ERR_PTR(-EAGAIN); |
| 248 | } |
| 249 | |
| 250 | static inline void vunmap_sg(const void *va) |
| 251 | { |
| 252 | vunmap(va); |
| 253 | } |
| 254 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 255 | static struct iovm_struct *__find_iovm_area(struct omap_iommu *obj, |
| 256 | const u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 257 | { |
| 258 | struct iovm_struct *tmp; |
| 259 | |
| 260 | list_for_each_entry(tmp, &obj->mmap, list) { |
| 261 | if ((da >= tmp->da_start) && (da < tmp->da_end)) { |
| 262 | size_t len; |
| 263 | |
| 264 | len = tmp->da_end - tmp->da_start; |
| 265 | |
| 266 | dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", |
| 267 | __func__, tmp->da_start, da, tmp->da_end, len, |
| 268 | tmp->flags); |
| 269 | |
| 270 | return tmp; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 278 | * omap_find_iovm_area - find iovma which includes @da |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 279 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 280 | * @da: iommu device virtual address |
| 281 | * |
| 282 | * Find the existing iovma starting at @da |
| 283 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 284 | struct iovm_struct *omap_find_iovm_area(struct device *dev, u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 285 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 286 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 287 | struct iovm_struct *area; |
| 288 | |
| 289 | mutex_lock(&obj->mmap_lock); |
| 290 | area = __find_iovm_area(obj, da); |
| 291 | mutex_unlock(&obj->mmap_lock); |
| 292 | |
| 293 | return area; |
| 294 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 295 | EXPORT_SYMBOL_GPL(omap_find_iovm_area); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * This finds the hole(area) which fits the requested address and len |
| 299 | * in iovmas mmap, and returns the new allocated iovma. |
| 300 | */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 301 | 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] | 302 | size_t bytes, u32 flags) |
| 303 | { |
| 304 | struct iovm_struct *new, *tmp; |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 305 | u32 start, prev_end, alignment; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 306 | |
| 307 | if (!obj || !bytes) |
| 308 | return ERR_PTR(-EINVAL); |
| 309 | |
| 310 | start = da; |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 311 | alignment = PAGE_SIZE; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 312 | |
David Cohen | d038aee | 2011-03-09 09:17:33 +0000 | [diff] [blame] | 313 | if (~flags & IOVMF_DA_FIXED) { |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 314 | /* Don't map address 0 */ |
| 315 | start = obj->da_start ? obj->da_start : alignment; |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 316 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 317 | if (flags & IOVMF_LINEAR) |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 318 | alignment = iopgsz_max(bytes); |
| 319 | start = roundup(start, alignment); |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 320 | } else if (start < obj->da_start || start > obj->da_end || |
| 321 | obj->da_end - start < bytes) { |
| 322 | return ERR_PTR(-EINVAL); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | tmp = NULL; |
| 326 | if (list_empty(&obj->mmap)) |
| 327 | goto found; |
| 328 | |
| 329 | prev_end = 0; |
| 330 | list_for_each_entry(tmp, &obj->mmap, list) { |
| 331 | |
Guzman Lugo, Fernando | ba6e1f4 | 2010-12-15 00:54:00 +0000 | [diff] [blame] | 332 | if (prev_end > start) |
Hiroshi DOYU | e0a42e4 | 2010-05-06 17:09:25 +0300 | [diff] [blame] | 333 | break; |
| 334 | |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 335 | if (tmp->da_start > start && (tmp->da_start - start) >= bytes) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 336 | goto found; |
| 337 | |
David Cohen | d038aee | 2011-03-09 09:17:33 +0000 | [diff] [blame] | 338 | if (tmp->da_end >= start && ~flags & IOVMF_DA_FIXED) |
Michael Jones | 4359d38 | 2011-03-09 09:17:32 +0000 | [diff] [blame] | 339 | start = roundup(tmp->da_end + 1, alignment); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 340 | |
| 341 | prev_end = tmp->da_end; |
| 342 | } |
| 343 | |
Guzman Lugo, Fernando | c7f4ab2 | 2010-12-15 00:54:03 +0000 | [diff] [blame] | 344 | if ((start >= prev_end) && (obj->da_end - start >= bytes)) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 345 | goto found; |
| 346 | |
| 347 | dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n", |
| 348 | __func__, da, bytes, flags); |
| 349 | |
| 350 | return ERR_PTR(-EINVAL); |
| 351 | |
| 352 | found: |
| 353 | new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL); |
| 354 | if (!new) |
| 355 | return ERR_PTR(-ENOMEM); |
| 356 | |
| 357 | new->iommu = obj; |
| 358 | new->da_start = start; |
| 359 | new->da_end = start + bytes; |
| 360 | new->flags = flags; |
| 361 | |
| 362 | /* |
| 363 | * keep ascending order of iovmas |
| 364 | */ |
| 365 | if (tmp) |
| 366 | list_add_tail(&new->list, &tmp->list); |
| 367 | else |
| 368 | list_add(&new->list, &obj->mmap); |
| 369 | |
| 370 | dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n", |
| 371 | __func__, new->da_start, start, new->da_end, bytes, flags); |
| 372 | |
| 373 | return new; |
| 374 | } |
| 375 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 376 | 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] | 377 | { |
| 378 | size_t bytes; |
| 379 | |
| 380 | BUG_ON(!obj || !area); |
| 381 | |
| 382 | bytes = area->da_end - area->da_start; |
| 383 | |
| 384 | dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n", |
| 385 | __func__, area->da_start, area->da_end, bytes, area->flags); |
| 386 | |
| 387 | list_del(&area->list); |
| 388 | kmem_cache_free(iovm_area_cachep, area); |
| 389 | } |
| 390 | |
| 391 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 392 | * omap_da_to_va - convert (d) to (v) |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 393 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 394 | * @da: iommu device virtual address |
| 395 | * @va: mpu virtual address |
| 396 | * |
| 397 | * Returns mpu virtual addr which corresponds to a given device virtual addr |
| 398 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 399 | void *omap_da_to_va(struct device *dev, u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 400 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 401 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 402 | void *va = NULL; |
| 403 | struct iovm_struct *area; |
| 404 | |
| 405 | mutex_lock(&obj->mmap_lock); |
| 406 | |
| 407 | area = __find_iovm_area(obj, da); |
| 408 | if (!area) { |
| 409 | dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da); |
| 410 | goto out; |
| 411 | } |
| 412 | va = area->va; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 413 | out: |
Daniel Walker | 2654890 | 2009-10-05 13:31:45 -0700 | [diff] [blame] | 414 | mutex_unlock(&obj->mmap_lock); |
| 415 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 416 | return va; |
| 417 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(omap_da_to_va); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 419 | |
| 420 | static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va) |
| 421 | { |
| 422 | unsigned int i; |
| 423 | struct scatterlist *sg; |
| 424 | void *va = _va; |
| 425 | void *va_end; |
| 426 | |
| 427 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 428 | struct page *pg; |
| 429 | const size_t bytes = PAGE_SIZE; |
| 430 | |
| 431 | /* |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 432 | * iommu 'superpage' isn't supported with 'omap_iommu_vmalloc()' |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 433 | */ |
| 434 | pg = vmalloc_to_page(va); |
| 435 | BUG_ON(!pg); |
| 436 | sg_set_page(sg, pg, bytes, 0); |
| 437 | |
| 438 | va += bytes; |
| 439 | } |
| 440 | |
| 441 | va_end = _va + PAGE_SIZE * i; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static inline void sgtable_drain_vmalloc(struct sg_table *sgt) |
| 445 | { |
| 446 | /* |
| 447 | * Actually this is not necessary at all, just exists for |
Hiroshi DOYU | ba6a117 | 2009-10-05 13:31:45 -0700 | [diff] [blame] | 448 | * consistency of the code readability. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 449 | */ |
| 450 | BUG_ON(!sgt); |
| 451 | } |
| 452 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 453 | /* create 'da' <-> 'pa' mapping from 'sgt' */ |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 454 | static int map_iovm_area(struct iommu_domain *domain, struct iovm_struct *new, |
| 455 | const struct sg_table *sgt, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 456 | { |
| 457 | int err; |
| 458 | unsigned int i, j; |
| 459 | struct scatterlist *sg; |
| 460 | u32 da = new->da_start; |
| 461 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 462 | if (!domain || !sgt) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 463 | return -EINVAL; |
| 464 | |
| 465 | BUG_ON(!sgtable_ok(sgt)); |
| 466 | |
| 467 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 468 | u32 pa; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 469 | size_t bytes; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 470 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 471 | pa = sg_phys(sg) - sg->offset; |
| 472 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 473 | |
| 474 | flags &= ~IOVMF_PGSZ_MASK; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 475 | |
| 476 | if (bytes_to_iopgsz(bytes) < 0) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 477 | goto err_out; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 478 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 479 | pr_debug("%s: [%d] %08x %08x(%x)\n", __func__, |
| 480 | i, da, pa, bytes); |
| 481 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 482 | err = iommu_map(domain, da, pa, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 483 | if (err) |
| 484 | goto err_out; |
| 485 | |
| 486 | da += bytes; |
| 487 | } |
| 488 | return 0; |
| 489 | |
| 490 | err_out: |
| 491 | da = new->da_start; |
| 492 | |
| 493 | for_each_sg(sgt->sgl, sg, i, j) { |
| 494 | size_t bytes; |
| 495 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 496 | bytes = sg->length + sg->offset; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 497 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 498 | /* ignore failures.. we're already handling one */ |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 499 | iommu_unmap(domain, da, bytes); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 500 | |
| 501 | da += bytes; |
| 502 | } |
| 503 | return err; |
| 504 | } |
| 505 | |
| 506 | /* release 'da' <-> 'pa' mapping */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 507 | 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] | 508 | struct iovm_struct *area) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 509 | { |
| 510 | u32 start; |
| 511 | size_t total = area->da_end - area->da_start; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 512 | const struct sg_table *sgt = area->sgt; |
| 513 | struct scatterlist *sg; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 514 | int i; |
| 515 | size_t unmapped; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 516 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 517 | BUG_ON(!sgtable_ok(sgt)); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 518 | BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE)); |
| 519 | |
| 520 | start = area->da_start; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 521 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 522 | size_t bytes; |
| 523 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 524 | bytes = sg->length + sg->offset; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 525 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 526 | unmapped = iommu_unmap(domain, start, bytes); |
| 527 | if (unmapped < bytes) |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 528 | break; |
| 529 | |
| 530 | dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n", |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 531 | __func__, start, bytes, area->flags); |
| 532 | |
| 533 | BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE)); |
| 534 | |
| 535 | total -= bytes; |
| 536 | start += bytes; |
| 537 | } |
| 538 | BUG_ON(total); |
| 539 | } |
| 540 | |
| 541 | /* template function for all unmapping */ |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 542 | static struct sg_table *unmap_vm_area(struct iommu_domain *domain, |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 543 | struct omap_iommu *obj, const u32 da, |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 544 | void (*fn)(const void *), u32 flags) |
| 545 | { |
| 546 | struct sg_table *sgt = NULL; |
| 547 | struct iovm_struct *area; |
| 548 | |
| 549 | if (!IS_ALIGNED(da, PAGE_SIZE)) { |
| 550 | dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | mutex_lock(&obj->mmap_lock); |
| 555 | |
| 556 | area = __find_iovm_area(obj, da); |
| 557 | if (!area) { |
| 558 | dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da); |
| 559 | goto out; |
| 560 | } |
| 561 | |
| 562 | if ((area->flags & flags) != flags) { |
| 563 | dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__, |
| 564 | area->flags); |
| 565 | goto out; |
| 566 | } |
| 567 | sgt = (struct sg_table *)area->sgt; |
| 568 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 569 | unmap_iovm_area(domain, obj, area); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 570 | |
| 571 | fn(area->va); |
| 572 | |
| 573 | dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__, |
| 574 | area->da_start, da, area->da_end, |
| 575 | area->da_end - area->da_start, area->flags); |
| 576 | |
| 577 | free_iovm_area(obj, area); |
| 578 | out: |
| 579 | mutex_unlock(&obj->mmap_lock); |
| 580 | |
| 581 | return sgt; |
| 582 | } |
| 583 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 584 | 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] | 585 | u32 da, const struct sg_table *sgt, void *va, |
| 586 | size_t bytes, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 587 | { |
| 588 | int err = -ENOMEM; |
| 589 | struct iovm_struct *new; |
| 590 | |
| 591 | mutex_lock(&obj->mmap_lock); |
| 592 | |
| 593 | new = alloc_iovm_area(obj, da, bytes, flags); |
| 594 | if (IS_ERR(new)) { |
| 595 | err = PTR_ERR(new); |
| 596 | goto err_alloc_iovma; |
| 597 | } |
| 598 | new->va = va; |
| 599 | new->sgt = sgt; |
| 600 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 601 | if (map_iovm_area(domain, new, sgt, new->flags)) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 602 | goto err_map; |
| 603 | |
| 604 | mutex_unlock(&obj->mmap_lock); |
| 605 | |
| 606 | dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n", |
| 607 | __func__, new->da_start, bytes, new->flags, va); |
| 608 | |
| 609 | return new->da_start; |
| 610 | |
| 611 | err_map: |
| 612 | free_iovm_area(obj, new); |
| 613 | err_alloc_iovma: |
| 614 | mutex_unlock(&obj->mmap_lock); |
| 615 | return err; |
| 616 | } |
| 617 | |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 618 | static inline u32 |
| 619 | __iommu_vmap(struct iommu_domain *domain, struct omap_iommu *obj, |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 620 | u32 da, const struct sg_table *sgt, |
| 621 | void *va, size_t bytes, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 622 | { |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 623 | return map_iommu_region(domain, obj, da, sgt, va, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 627 | * omap_iommu_vmap - (d)-(p)-(v) address mapper |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 628 | * @domain: iommu domain |
| 629 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 630 | * @sgt: address of scatter gather table |
| 631 | * @flags: iovma and page property |
| 632 | * |
| 633 | * Creates 1-n-1 mapping with given @sgt and returns @da. |
| 634 | * All @sgt element must be io page size aligned. |
| 635 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 636 | 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] | 637 | const struct sg_table *sgt, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 638 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 639 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 640 | size_t bytes; |
Hiroshi DOYU | 935e473 | 2009-11-22 10:11:02 -0800 | [diff] [blame] | 641 | void *va = NULL; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 642 | |
| 643 | if (!obj || !obj->dev || !sgt) |
| 644 | return -EINVAL; |
| 645 | |
| 646 | bytes = sgtable_len(sgt); |
| 647 | if (!bytes) |
| 648 | return -EINVAL; |
| 649 | bytes = PAGE_ALIGN(bytes); |
| 650 | |
Hiroshi DOYU | 935e473 | 2009-11-22 10:11:02 -0800 | [diff] [blame] | 651 | if (flags & IOVMF_MMIO) { |
| 652 | va = vmap_sg(sgt); |
| 653 | if (IS_ERR(va)) |
| 654 | return PTR_ERR(va); |
| 655 | } |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 656 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 657 | flags |= IOVMF_DISCONT; |
| 658 | flags |= IOVMF_MMIO; |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 659 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 660 | da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 661 | if (IS_ERR_VALUE(da)) |
| 662 | vunmap_sg(va); |
| 663 | |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 664 | return da + sgtable_offset(sgt); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 665 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 666 | EXPORT_SYMBOL_GPL(omap_iommu_vmap); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 667 | |
| 668 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 669 | * omap_iommu_vunmap - release virtual mapping obtained by 'omap_iommu_vmap()' |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 670 | * @domain: iommu domain |
| 671 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 672 | * @da: iommu device virtual address |
| 673 | * |
| 674 | * Free the iommu virtually contiguous memory area starting at |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 675 | * @da, which was returned by 'omap_iommu_vmap()'. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 676 | */ |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 677 | struct sg_table * |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 678 | omap_iommu_vunmap(struct iommu_domain *domain, struct device *dev, u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 679 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 680 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 681 | struct sg_table *sgt; |
| 682 | /* |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 683 | * 'sgt' is allocated before 'omap_iommu_vmalloc()' is called. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 684 | * Just returns 'sgt' to the caller to free |
| 685 | */ |
Laurent Pinchart | 329d8d3 | 2011-09-02 13:32:30 -0400 | [diff] [blame] | 686 | da &= PAGE_MASK; |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 687 | sgt = unmap_vm_area(domain, obj, da, vunmap_sg, |
| 688 | IOVMF_DISCONT | IOVMF_MMIO); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 689 | if (!sgt) |
| 690 | dev_dbg(obj->dev, "%s: No sgt\n", __func__); |
| 691 | return sgt; |
| 692 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 693 | EXPORT_SYMBOL_GPL(omap_iommu_vunmap); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 694 | |
| 695 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 696 | * omap_iommu_vmalloc - (d)-(p)-(v) address allocator and mapper |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 697 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 698 | * @da: contiguous iommu virtual memory |
| 699 | * @bytes: allocation size |
| 700 | * @flags: iovma and page property |
| 701 | * |
| 702 | * Allocate @bytes linearly and creates 1-n-1 mapping and returns |
David Cohen | d038aee | 2011-03-09 09:17:33 +0000 | [diff] [blame] | 703 | * @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] | 704 | */ |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 705 | u32 |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 706 | 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] | 707 | size_t bytes, u32 flags) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 708 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 709 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 710 | void *va; |
| 711 | struct sg_table *sgt; |
| 712 | |
| 713 | if (!obj || !obj->dev || !bytes) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | bytes = PAGE_ALIGN(bytes); |
| 717 | |
| 718 | va = vmalloc(bytes); |
| 719 | if (!va) |
| 720 | return -ENOMEM; |
| 721 | |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 722 | flags |= IOVMF_DISCONT; |
| 723 | flags |= IOVMF_ALLOC; |
Guzman Lugo, Fernando | ad10812 | 2010-12-15 00:54:01 +0000 | [diff] [blame] | 724 | |
| 725 | sgt = sgtable_alloc(bytes, flags, da, 0); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 726 | if (IS_ERR(sgt)) { |
| 727 | da = PTR_ERR(sgt); |
| 728 | goto err_sgt_alloc; |
| 729 | } |
| 730 | sgtable_fill_vmalloc(sgt, va); |
| 731 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 732 | da = __iommu_vmap(domain, obj, da, sgt, va, bytes, flags); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 733 | if (IS_ERR_VALUE(da)) |
| 734 | goto err_iommu_vmap; |
| 735 | |
| 736 | return da; |
| 737 | |
| 738 | err_iommu_vmap: |
| 739 | sgtable_drain_vmalloc(sgt); |
| 740 | sgtable_free(sgt); |
| 741 | err_sgt_alloc: |
| 742 | vfree(va); |
| 743 | return da; |
| 744 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 745 | EXPORT_SYMBOL_GPL(omap_iommu_vmalloc); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 746 | |
| 747 | /** |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 748 | * omap_iommu_vfree - release memory allocated by 'omap_iommu_vmalloc()' |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 749 | * @dev: client device |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 750 | * @da: iommu device virtual address |
| 751 | * |
| 752 | * Frees the iommu virtually continuous memory area starting at |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 753 | * @da, as obtained from 'omap_iommu_vmalloc()'. |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 754 | */ |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 755 | void omap_iommu_vfree(struct iommu_domain *domain, struct device *dev, |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 756 | const u32 da) |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 757 | { |
Ohad Ben-Cohen | fabdbca | 2011-10-11 00:18:33 +0200 | [diff] [blame] | 758 | struct omap_iommu *obj = dev_to_omap_iommu(dev); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 759 | struct sg_table *sgt; |
| 760 | |
Ohad Ben-Cohen | f626b52 | 2011-06-02 01:46:12 +0300 | [diff] [blame] | 761 | sgt = unmap_vm_area(domain, obj, da, vfree, |
| 762 | IOVMF_DISCONT | IOVMF_ALLOC); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 763 | if (!sgt) |
| 764 | dev_dbg(obj->dev, "%s: No sgt\n", __func__); |
| 765 | sgtable_free(sgt); |
| 766 | } |
Ohad Ben-Cohen | 6c32df4 | 2011-08-17 22:57:56 +0300 | [diff] [blame] | 767 | EXPORT_SYMBOL_GPL(omap_iommu_vfree); |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 768 | |
Hiroshi DOYU | 69d3a84 | 2009-01-28 21:32:08 +0200 | [diff] [blame] | 769 | static int __init iovmm_init(void) |
| 770 | { |
| 771 | const unsigned long flags = SLAB_HWCACHE_ALIGN; |
| 772 | struct kmem_cache *p; |
| 773 | |
| 774 | p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0, |
| 775 | flags, NULL); |
| 776 | if (!p) |
| 777 | return -ENOMEM; |
| 778 | iovm_area_cachep = p; |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | module_init(iovmm_init); |
| 783 | |
| 784 | static void __exit iovmm_exit(void) |
| 785 | { |
| 786 | kmem_cache_destroy(iovm_area_cachep); |
| 787 | } |
| 788 | module_exit(iovmm_exit); |
| 789 | |
| 790 | MODULE_DESCRIPTION("omap iommu: simple virtual address space management"); |
| 791 | MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>"); |
| 792 | MODULE_LICENSE("GPL v2"); |