Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion_carveout_heap.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 5 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/spinlock.h> |
| 18 | |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/genalloc.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/ion.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/vmalloc.h> |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 27 | #include <linux/iommu.h> |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 29 | #include "ion_priv.h" |
| 30 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 31 | #include <mach/iommu_domains.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 32 | #include <asm/mach/map.h> |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 33 | #include <asm/cacheflush.h> |
Mitchel Humpherys | af2e5c5 | 2012-09-06 12:16:36 -0700 | [diff] [blame] | 34 | #include <linux/msm_ion.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 35 | |
| 36 | struct ion_carveout_heap { |
| 37 | struct ion_heap heap; |
| 38 | struct gen_pool *pool; |
| 39 | ion_phys_addr_t base; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 40 | unsigned long allocated_bytes; |
| 41 | unsigned long total_size; |
Olav Haugan | ee0f780 | 2011-12-19 13:28:57 -0800 | [diff] [blame] | 42 | int (*request_region)(void *); |
| 43 | int (*release_region)(void *); |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 44 | atomic_t map_count; |
| 45 | void *bus_id; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 46 | unsigned int has_outer_cache; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, |
| 50 | unsigned long size, |
| 51 | unsigned long align) |
| 52 | { |
| 53 | struct ion_carveout_heap *carveout_heap = |
| 54 | container_of(heap, struct ion_carveout_heap, heap); |
Laura Abbott | 5ee3921 | 2011-10-11 10:12:40 -0700 | [diff] [blame] | 55 | unsigned long offset = gen_pool_alloc_aligned(carveout_heap->pool, |
| 56 | size, ilog2(align)); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 57 | |
Laura Abbott | d16dd60 | 2011-10-31 13:51:10 -0700 | [diff] [blame] | 58 | if (!offset) { |
| 59 | if ((carveout_heap->total_size - |
Olav Haugan | 739e518 | 2012-04-19 14:14:43 -0700 | [diff] [blame] | 60 | carveout_heap->allocated_bytes) >= size) |
Laura Abbott | d16dd60 | 2011-10-31 13:51:10 -0700 | [diff] [blame] | 61 | pr_debug("%s: heap %s has enough memory (%lx) but" |
| 62 | " the allocation of size %lx still failed." |
| 63 | " Memory is probably fragmented.", |
| 64 | __func__, heap->name, |
| 65 | carveout_heap->total_size - |
| 66 | carveout_heap->allocated_bytes, size); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 67 | return ION_CARVEOUT_ALLOCATE_FAIL; |
Laura Abbott | d16dd60 | 2011-10-31 13:51:10 -0700 | [diff] [blame] | 68 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 69 | |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 70 | carveout_heap->allocated_bytes += size; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 71 | return offset; |
| 72 | } |
| 73 | |
| 74 | void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, |
| 75 | unsigned long size) |
| 76 | { |
| 77 | struct ion_carveout_heap *carveout_heap = |
| 78 | container_of(heap, struct ion_carveout_heap, heap); |
| 79 | |
| 80 | if (addr == ION_CARVEOUT_ALLOCATE_FAIL) |
| 81 | return; |
| 82 | gen_pool_free(carveout_heap->pool, addr, size); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 83 | carveout_heap->allocated_bytes -= size; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int ion_carveout_heap_phys(struct ion_heap *heap, |
| 87 | struct ion_buffer *buffer, |
| 88 | ion_phys_addr_t *addr, size_t *len) |
| 89 | { |
| 90 | *addr = buffer->priv_phys; |
| 91 | *len = buffer->size; |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int ion_carveout_heap_allocate(struct ion_heap *heap, |
| 96 | struct ion_buffer *buffer, |
| 97 | unsigned long size, unsigned long align, |
| 98 | unsigned long flags) |
| 99 | { |
| 100 | buffer->priv_phys = ion_carveout_allocate(heap, size, align); |
| 101 | return buffer->priv_phys == ION_CARVEOUT_ALLOCATE_FAIL ? -ENOMEM : 0; |
| 102 | } |
| 103 | |
| 104 | static void ion_carveout_heap_free(struct ion_buffer *buffer) |
| 105 | { |
| 106 | struct ion_heap *heap = buffer->heap; |
| 107 | |
| 108 | ion_carveout_free(heap, buffer->priv_phys, buffer->size); |
| 109 | buffer->priv_phys = ION_CARVEOUT_ALLOCATE_FAIL; |
| 110 | } |
| 111 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 112 | struct sg_table *ion_carveout_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 113 | struct ion_buffer *buffer) |
| 114 | { |
Mitchel Humpherys | 0432d69 | 2013-01-08 17:03:10 -0800 | [diff] [blame] | 115 | size_t chunk_size = buffer->size; |
Jordan Crouse | 0a1195e | 2011-10-20 14:03:31 -0600 | [diff] [blame] | 116 | |
Mitchel Humpherys | 0432d69 | 2013-01-08 17:03:10 -0800 | [diff] [blame] | 117 | if (ION_IS_CACHED(buffer->flags)) |
| 118 | chunk_size = PAGE_SIZE; |
Jordan Crouse | 0a1195e | 2011-10-20 14:03:31 -0600 | [diff] [blame] | 119 | |
Mitchel Humpherys | 0432d69 | 2013-01-08 17:03:10 -0800 | [diff] [blame] | 120 | return ion_create_chunked_sg_table(buffer->priv_phys, chunk_size, |
| 121 | buffer->size); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void ion_carveout_heap_unmap_dma(struct ion_heap *heap, |
| 125 | struct ion_buffer *buffer) |
| 126 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 127 | if (buffer->sg_table) |
| 128 | sg_free_table(buffer->sg_table); |
| 129 | kfree(buffer->sg_table); |
| 130 | buffer->sg_table = 0; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 133 | static int ion_carveout_request_region(struct ion_carveout_heap *carveout_heap) |
| 134 | { |
| 135 | int ret_value = 0; |
| 136 | if (atomic_inc_return(&carveout_heap->map_count) == 1) { |
| 137 | if (carveout_heap->request_region) { |
| 138 | ret_value = carveout_heap->request_region( |
| 139 | carveout_heap->bus_id); |
| 140 | if (ret_value) { |
| 141 | pr_err("Unable to request SMI region"); |
| 142 | atomic_dec(&carveout_heap->map_count); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | return ret_value; |
| 147 | } |
| 148 | |
| 149 | static int ion_carveout_release_region(struct ion_carveout_heap *carveout_heap) |
| 150 | { |
| 151 | int ret_value = 0; |
| 152 | if (atomic_dec_and_test(&carveout_heap->map_count)) { |
| 153 | if (carveout_heap->release_region) { |
| 154 | ret_value = carveout_heap->release_region( |
| 155 | carveout_heap->bus_id); |
| 156 | if (ret_value) |
| 157 | pr_err("Unable to release SMI region"); |
| 158 | } |
| 159 | } |
| 160 | return ret_value; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void *ion_carveout_heap_map_kernel(struct ion_heap *heap, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 164 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 165 | { |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 166 | struct ion_carveout_heap *carveout_heap = |
| 167 | container_of(heap, struct ion_carveout_heap, heap); |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 168 | void *ret_value; |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 169 | |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 170 | if (ion_carveout_request_region(carveout_heap)) |
| 171 | return NULL; |
| 172 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 173 | if (ION_IS_CACHED(buffer->flags)) |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 174 | ret_value = ioremap_cached(buffer->priv_phys, buffer->size); |
Laura Abbott | e8f2802 | 2011-09-28 14:40:40 -0700 | [diff] [blame] | 175 | else |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 176 | ret_value = ioremap(buffer->priv_phys, buffer->size); |
| 177 | |
| 178 | if (!ret_value) |
| 179 | ion_carveout_release_region(carveout_heap); |
| 180 | return ret_value; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void ion_carveout_heap_unmap_kernel(struct ion_heap *heap, |
| 184 | struct ion_buffer *buffer) |
| 185 | { |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 186 | struct ion_carveout_heap *carveout_heap = |
| 187 | container_of(heap, struct ion_carveout_heap, heap); |
| 188 | |
Colin Cross | 9a82640 | 2012-04-04 13:50:43 -0700 | [diff] [blame] | 189 | __arm_iounmap(buffer->vaddr); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 190 | buffer->vaddr = NULL; |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 191 | |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 192 | ion_carveout_release_region(carveout_heap); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | int ion_carveout_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 197 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 198 | { |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 199 | struct ion_carveout_heap *carveout_heap = |
| 200 | container_of(heap, struct ion_carveout_heap, heap); |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 201 | int ret_value = 0; |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 202 | |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 203 | if (ion_carveout_request_region(carveout_heap)) |
| 204 | return -EINVAL; |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 205 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 206 | if (!ION_IS_CACHED(buffer->flags)) |
Olav Haugan | de074a7 | 2012-02-22 15:39:54 -0800 | [diff] [blame] | 207 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 208 | |
| 209 | ret_value = remap_pfn_range(vma, vma->vm_start, |
| 210 | __phys_to_pfn(buffer->priv_phys) + vma->vm_pgoff, |
| 211 | vma->vm_end - vma->vm_start, |
| 212 | vma->vm_page_prot); |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 213 | |
| 214 | if (ret_value) |
| 215 | ion_carveout_release_region(carveout_heap); |
| 216 | return ret_value; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 219 | void ion_carveout_heap_unmap_user(struct ion_heap *heap, |
| 220 | struct ion_buffer *buffer) |
| 221 | { |
| 222 | struct ion_carveout_heap *carveout_heap = |
| 223 | container_of(heap, struct ion_carveout_heap, heap); |
Olav Haugan | cb9ad9e | 2012-01-09 11:15:14 -0800 | [diff] [blame] | 224 | ion_carveout_release_region(carveout_heap); |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 227 | int ion_carveout_cache_ops(struct ion_heap *heap, struct ion_buffer *buffer, |
| 228 | void *vaddr, unsigned int offset, unsigned int length, |
| 229 | unsigned int cmd) |
| 230 | { |
Neeti Desai | 3f3c282 | 2013-03-08 17:29:53 -0800 | [diff] [blame] | 231 | void (*outer_cache_op)(phys_addr_t, phys_addr_t) = NULL; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 232 | struct ion_carveout_heap *carveout_heap = |
| 233 | container_of(heap, struct ion_carveout_heap, heap); |
Neeti Desai | 3f3c282 | 2013-03-08 17:29:53 -0800 | [diff] [blame] | 234 | unsigned int size_to_vmap, total_size; |
| 235 | int i, j; |
| 236 | void *ptr = NULL; |
| 237 | ion_phys_addr_t buff_phys = buffer->priv_phys; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 238 | |
Neeti Desai | 3f3c282 | 2013-03-08 17:29:53 -0800 | [diff] [blame] | 239 | if (!vaddr) { |
| 240 | /* |
| 241 | * Split the vmalloc space into smaller regions in |
| 242 | * order to clean and/or invalidate the cache. |
| 243 | */ |
| 244 | size_to_vmap = ((VMALLOC_END - VMALLOC_START)/8); |
| 245 | total_size = buffer->size; |
| 246 | |
| 247 | for (i = 0; i < total_size; i += size_to_vmap) { |
| 248 | size_to_vmap = min(size_to_vmap, total_size - i); |
| 249 | for (j = 0; j < 10 && size_to_vmap; ++j) { |
| 250 | ptr = ioremap(buff_phys, size_to_vmap); |
| 251 | if (ptr) { |
| 252 | switch (cmd) { |
| 253 | case ION_IOC_CLEAN_CACHES: |
| 254 | dmac_clean_range(ptr, |
| 255 | ptr + size_to_vmap); |
| 256 | outer_cache_op = |
| 257 | outer_clean_range; |
| 258 | break; |
| 259 | case ION_IOC_INV_CACHES: |
| 260 | dmac_inv_range(ptr, |
| 261 | ptr + size_to_vmap); |
| 262 | outer_cache_op = |
| 263 | outer_inv_range; |
| 264 | break; |
| 265 | case ION_IOC_CLEAN_INV_CACHES: |
| 266 | dmac_flush_range(ptr, |
| 267 | ptr + size_to_vmap); |
| 268 | outer_cache_op = |
| 269 | outer_flush_range; |
| 270 | break; |
| 271 | default: |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | buff_phys += size_to_vmap; |
| 275 | break; |
| 276 | } else { |
| 277 | size_to_vmap >>= 1; |
| 278 | } |
| 279 | } |
| 280 | if (!ptr) { |
| 281 | pr_err("Couldn't io-remap the memory\n"); |
| 282 | return -EINVAL; |
| 283 | } |
| 284 | iounmap(ptr); |
| 285 | } |
| 286 | } else { |
| 287 | switch (cmd) { |
| 288 | case ION_IOC_CLEAN_CACHES: |
| 289 | dmac_clean_range(vaddr, vaddr + length); |
| 290 | outer_cache_op = outer_clean_range; |
| 291 | break; |
| 292 | case ION_IOC_INV_CACHES: |
| 293 | dmac_inv_range(vaddr, vaddr + length); |
| 294 | outer_cache_op = outer_inv_range; |
| 295 | break; |
| 296 | case ION_IOC_CLEAN_INV_CACHES: |
| 297 | dmac_flush_range(vaddr, vaddr + length); |
| 298 | outer_cache_op = outer_flush_range; |
| 299 | break; |
| 300 | default: |
| 301 | return -EINVAL; |
| 302 | } |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 305 | if (carveout_heap->has_outer_cache) { |
| 306 | unsigned long pstart = buffer->priv_phys + offset; |
| 307 | outer_cache_op(pstart, pstart + length); |
| 308 | } |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 312 | static int ion_carveout_print_debug(struct ion_heap *heap, struct seq_file *s, |
| 313 | const struct rb_root *mem_map) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 314 | { |
| 315 | struct ion_carveout_heap *carveout_heap = |
| 316 | container_of(heap, struct ion_carveout_heap, heap); |
| 317 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 318 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 319 | carveout_heap->allocated_bytes); |
| 320 | seq_printf(s, "total heap size: %lx\n", carveout_heap->total_size); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 321 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 322 | if (mem_map) { |
| 323 | unsigned long base = carveout_heap->base; |
| 324 | unsigned long size = carveout_heap->total_size; |
| 325 | unsigned long end = base+size; |
| 326 | unsigned long last_end = base; |
| 327 | struct rb_node *n; |
| 328 | |
| 329 | seq_printf(s, "\nMemory Map\n"); |
| 330 | seq_printf(s, "%16.s %14.s %14.s %14.s\n", |
| 331 | "client", "start address", "end address", |
| 332 | "size (hex)"); |
| 333 | |
| 334 | for (n = rb_first(mem_map); n; n = rb_next(n)) { |
| 335 | struct mem_map_data *data = |
| 336 | rb_entry(n, struct mem_map_data, node); |
| 337 | const char *client_name = "(null)"; |
| 338 | |
| 339 | if (last_end < data->addr) { |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 340 | phys_addr_t da; |
| 341 | |
| 342 | da = data->addr-1; |
| 343 | seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n", |
| 344 | "FREE", &last_end, &da, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 345 | data->addr-last_end, |
| 346 | data->addr-last_end); |
| 347 | } |
| 348 | |
| 349 | if (data->client_name) |
| 350 | client_name = data->client_name; |
| 351 | |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 352 | seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n", |
| 353 | client_name, &data->addr, |
| 354 | &data->addr_end, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 355 | data->size, data->size); |
| 356 | last_end = data->addr_end+1; |
| 357 | } |
| 358 | if (last_end < end) { |
| 359 | seq_printf(s, "%16.s %14lx %14lx %14lu (%lx)\n", "FREE", |
| 360 | last_end, end-1, end-last_end, end-last_end); |
| 361 | } |
| 362 | } |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 363 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 366 | int ion_carveout_heap_map_iommu(struct ion_buffer *buffer, |
| 367 | struct ion_iommu_map *data, |
| 368 | unsigned int domain_num, |
| 369 | unsigned int partition_num, |
| 370 | unsigned long align, |
| 371 | unsigned long iova_length, |
| 372 | unsigned long flags) |
| 373 | { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 374 | struct iommu_domain *domain; |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 375 | int ret = 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 376 | unsigned long extra; |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 377 | struct scatterlist *sglist = 0; |
Olav Haugan | f310cf2 | 2012-05-08 08:42:49 -0700 | [diff] [blame] | 378 | int prot = IOMMU_WRITE | IOMMU_READ; |
| 379 | prot |= ION_IS_CACHED(flags) ? IOMMU_CACHE : 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 380 | |
| 381 | data->mapped_size = iova_length; |
| 382 | |
| 383 | if (!msm_use_iommu()) { |
| 384 | data->iova_addr = buffer->priv_phys; |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | extra = iova_length - buffer->size; |
| 389 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 390 | ret = msm_allocate_iova_address(domain_num, partition_num, |
| 391 | data->mapped_size, align, |
| 392 | &data->iova_addr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 393 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 394 | if (ret) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 395 | goto out; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 396 | |
| 397 | domain = msm_get_iommu_domain(domain_num); |
| 398 | |
| 399 | if (!domain) { |
| 400 | ret = -ENOMEM; |
| 401 | goto out1; |
| 402 | } |
| 403 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 404 | sglist = vmalloc(sizeof(*sglist)); |
| 405 | if (!sglist) |
| 406 | goto out1; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 407 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 408 | sg_init_table(sglist, 1); |
| 409 | sglist->length = buffer->size; |
| 410 | sglist->offset = 0; |
| 411 | sglist->dma_address = buffer->priv_phys; |
| 412 | |
| 413 | ret = iommu_map_range(domain, data->iova_addr, sglist, |
| 414 | buffer->size, prot); |
| 415 | if (ret) { |
| 416 | pr_err("%s: could not map %lx in domain %p\n", |
| 417 | __func__, data->iova_addr, domain); |
| 418 | goto out1; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 421 | if (extra) { |
| 422 | unsigned long extra_iova_addr = data->iova_addr + buffer->size; |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 423 | unsigned long phys_addr = sg_phys(sglist); |
| 424 | ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr, |
| 425 | extra, SZ_4K, prot); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 426 | if (ret) |
| 427 | goto out2; |
| 428 | } |
| 429 | vfree(sglist); |
| 430 | return ret; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 431 | |
| 432 | out2: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 433 | iommu_unmap_range(domain, data->iova_addr, buffer->size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 434 | out1: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 435 | vfree(sglist); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 436 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
| 437 | data->mapped_size); |
| 438 | |
| 439 | out: |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | void ion_carveout_heap_unmap_iommu(struct ion_iommu_map *data) |
| 445 | { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 446 | unsigned int domain_num; |
| 447 | unsigned int partition_num; |
| 448 | struct iommu_domain *domain; |
| 449 | |
| 450 | if (!msm_use_iommu()) |
| 451 | return; |
| 452 | |
| 453 | domain_num = iommu_map_domain(data); |
| 454 | partition_num = iommu_map_partition(data); |
| 455 | |
| 456 | domain = msm_get_iommu_domain(domain_num); |
| 457 | |
| 458 | if (!domain) { |
| 459 | WARN(1, "Could not get domain %d. Corruption?\n", domain_num); |
| 460 | return; |
| 461 | } |
| 462 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 463 | iommu_unmap_range(domain, data->iova_addr, data->mapped_size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 464 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
| 465 | data->mapped_size); |
| 466 | |
| 467 | return; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static struct ion_heap_ops carveout_heap_ops = { |
| 471 | .allocate = ion_carveout_heap_allocate, |
| 472 | .free = ion_carveout_heap_free, |
| 473 | .phys = ion_carveout_heap_phys, |
| 474 | .map_user = ion_carveout_heap_map_user, |
| 475 | .map_kernel = ion_carveout_heap_map_kernel, |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 476 | .unmap_user = ion_carveout_heap_unmap_user, |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 477 | .unmap_kernel = ion_carveout_heap_unmap_kernel, |
Jordan Crouse | 0a1195e | 2011-10-20 14:03:31 -0600 | [diff] [blame] | 478 | .map_dma = ion_carveout_heap_map_dma, |
| 479 | .unmap_dma = ion_carveout_heap_unmap_dma, |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 480 | .cache_op = ion_carveout_cache_ops, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 481 | .print_debug = ion_carveout_print_debug, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 482 | .map_iommu = ion_carveout_heap_map_iommu, |
| 483 | .unmap_iommu = ion_carveout_heap_unmap_iommu, |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data) |
| 487 | { |
| 488 | struct ion_carveout_heap *carveout_heap; |
Laura Abbott | b1b7b43 | 2011-08-03 13:25:08 -0700 | [diff] [blame] | 489 | int ret; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 490 | |
| 491 | carveout_heap = kzalloc(sizeof(struct ion_carveout_heap), GFP_KERNEL); |
| 492 | if (!carveout_heap) |
| 493 | return ERR_PTR(-ENOMEM); |
| 494 | |
| 495 | carveout_heap->pool = gen_pool_create(12, -1); |
| 496 | if (!carveout_heap->pool) { |
| 497 | kfree(carveout_heap); |
| 498 | return ERR_PTR(-ENOMEM); |
| 499 | } |
| 500 | carveout_heap->base = heap_data->base; |
Laura Abbott | b1b7b43 | 2011-08-03 13:25:08 -0700 | [diff] [blame] | 501 | ret = gen_pool_add(carveout_heap->pool, carveout_heap->base, |
| 502 | heap_data->size, -1); |
| 503 | if (ret < 0) { |
Olav Haugan | 7fba5cf | 2012-01-06 10:05:31 -0800 | [diff] [blame] | 504 | gen_pool_destroy(carveout_heap->pool); |
Laura Abbott | b1b7b43 | 2011-08-03 13:25:08 -0700 | [diff] [blame] | 505 | kfree(carveout_heap); |
| 506 | return ERR_PTR(-EINVAL); |
| 507 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 508 | carveout_heap->heap.ops = &carveout_heap_ops; |
| 509 | carveout_heap->heap.type = ION_HEAP_TYPE_CARVEOUT; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 510 | carveout_heap->allocated_bytes = 0; |
| 511 | carveout_heap->total_size = heap_data->size; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 512 | carveout_heap->has_outer_cache = heap_data->has_outer_cache; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 513 | |
Olav Haugan | 0703dbf | 2011-12-19 17:53:38 -0800 | [diff] [blame] | 514 | if (heap_data->extra_data) { |
| 515 | struct ion_co_heap_pdata *extra_data = |
| 516 | heap_data->extra_data; |
| 517 | |
| 518 | if (extra_data->setup_region) |
| 519 | carveout_heap->bus_id = extra_data->setup_region(); |
| 520 | if (extra_data->request_region) |
| 521 | carveout_heap->request_region = |
| 522 | extra_data->request_region; |
| 523 | if (extra_data->release_region) |
| 524 | carveout_heap->release_region = |
| 525 | extra_data->release_region; |
| 526 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 527 | return &carveout_heap->heap; |
| 528 | } |
| 529 | |
| 530 | void ion_carveout_heap_destroy(struct ion_heap *heap) |
| 531 | { |
| 532 | struct ion_carveout_heap *carveout_heap = |
| 533 | container_of(heap, struct ion_carveout_heap, heap); |
| 534 | |
| 535 | gen_pool_destroy(carveout_heap->pool); |
| 536 | kfree(carveout_heap); |
| 537 | carveout_heap = NULL; |
| 538 | } |