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