Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion_system_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 | c80005a | 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 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 18 | #include <asm/page.h> |
| 19 | #include <linux/dma-mapping.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 21 | #include <linux/highmem.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 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> |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 28 | #include "ion_priv.h" |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 29 | #include <mach/memory.h> |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 30 | #include <asm/cacheflush.h> |
Mitchel Humpherys | af2e5c5 | 2012-09-06 12:16:36 -0700 | [diff] [blame] | 31 | #include <linux/msm_ion.h> |
Neeti Desai | 3f3c282 | 2013-03-08 17:29:53 -0800 | [diff] [blame] | 32 | #include <linux/dma-mapping.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 33 | |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 34 | static atomic_t system_heap_allocated; |
| 35 | static atomic_t system_contig_heap_allocated; |
| 36 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 37 | struct page_info { |
| 38 | struct page *page; |
| 39 | unsigned long order; |
| 40 | struct list_head list; |
| 41 | }; |
| 42 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 43 | static struct page_info *alloc_largest_available(unsigned long size, |
| 44 | bool split_pages) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 45 | { |
| 46 | static unsigned int orders[] = {8, 4, 0}; |
| 47 | struct page *page; |
| 48 | struct page_info *info; |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < ARRAY_SIZE(orders); i++) { |
| 52 | if (size < (1 << orders[i]) * PAGE_SIZE) |
| 53 | continue; |
Dima Zavin | dcd71bc | 2012-07-27 15:29:55 -0700 | [diff] [blame] | 54 | page = alloc_pages(GFP_HIGHUSER | __GFP_ZERO | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 55 | __GFP_NOWARN | __GFP_NORETRY, orders[i]); |
| 56 | if (!page) |
| 57 | continue; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 58 | if (split_pages) |
| 59 | split_page(page, orders[i]); |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame^] | 60 | info = kmalloc(sizeof(struct page_info *), GFP_KERNEL); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 61 | info->page = page; |
| 62 | info->order = orders[i]; |
| 63 | return info; |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 68 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 69 | struct ion_buffer *buffer, |
| 70 | unsigned long size, unsigned long align, |
| 71 | unsigned long flags) |
| 72 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 73 | struct sg_table *table; |
| 74 | struct scatterlist *sg; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 75 | int ret; |
| 76 | struct list_head pages; |
| 77 | struct page_info *info, *tmp_info; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 78 | int i = 0; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 79 | long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 80 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| 81 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 82 | |
| 83 | INIT_LIST_HEAD(&pages); |
| 84 | while (size_remaining > 0) { |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 85 | info = alloc_largest_available(size_remaining, split_pages); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 86 | if (!info) |
| 87 | goto err; |
| 88 | list_add_tail(&info->list, &pages); |
| 89 | size_remaining -= (1 << info->order) * PAGE_SIZE; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 90 | i++; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 91 | } |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 92 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 93 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 94 | if (!table) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 95 | goto err; |
| 96 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 97 | if (split_pages) |
| 98 | ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE, |
| 99 | GFP_KERNEL); |
| 100 | else |
| 101 | ret = sg_alloc_table(table, i, GFP_KERNEL); |
| 102 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 103 | if (ret) |
| 104 | goto err1; |
| 105 | |
| 106 | sg = table->sgl; |
| 107 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| 108 | struct page *page = info->page; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 109 | |
| 110 | if (split_pages) { |
| 111 | for (i = 0; i < (1 << info->order); i++) { |
| 112 | sg_set_page(sg, page + i, PAGE_SIZE, 0); |
| 113 | sg = sg_next(sg); |
| 114 | } |
| 115 | } else { |
| 116 | sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, |
| 117 | 0); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 118 | sg = sg_next(sg); |
| 119 | } |
| 120 | list_del(&info->list); |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame^] | 121 | kfree(info); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 122 | } |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 123 | |
| 124 | dma_sync_sg_for_device(NULL, table->sgl, table->nents, |
| 125 | DMA_BIDIRECTIONAL); |
| 126 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 127 | buffer->priv_virt = table; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 128 | atomic_add(size, &system_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 129 | return 0; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 130 | err1: |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 131 | kfree(table); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 132 | err: |
| 133 | list_for_each_entry(info, &pages, list) { |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 134 | if (split_pages) |
| 135 | for (i = 0; i < (1 << info->order); i++) |
| 136 | __free_page(info->page + i); |
| 137 | else |
| 138 | __free_pages(info->page, info->order); |
| 139 | |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame^] | 140 | kfree(info); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 141 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 142 | return -ENOMEM; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void ion_system_heap_free(struct ion_buffer *buffer) |
| 146 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 147 | int i; |
| 148 | struct scatterlist *sg; |
| 149 | struct sg_table *table = buffer->priv_virt; |
| 150 | |
| 151 | for_each_sg(table->sgl, sg, table->nents, i) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 152 | __free_pages(sg_page(sg), get_order(sg_dma_len(sg))); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 153 | if (buffer->sg_table) |
| 154 | sg_free_table(buffer->sg_table); |
| 155 | kfree(buffer->sg_table); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 156 | atomic_sub(buffer->size, &system_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 159 | struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 160 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 161 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 162 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 166 | struct ion_buffer *buffer) |
| 167 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 168 | return; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void *ion_system_heap_map_kernel(struct ion_heap *heap, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 172 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 173 | { |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 174 | struct scatterlist *sg; |
| 175 | int i, j; |
| 176 | void *vaddr; |
| 177 | pgprot_t pgprot; |
| 178 | struct sg_table *table = buffer->priv_virt; |
| 179 | int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; |
| 180 | struct page **pages = kzalloc(sizeof(struct page *) * npages, |
| 181 | GFP_KERNEL); |
| 182 | struct page **tmp = pages; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 183 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 184 | if (buffer->flags & ION_FLAG_CACHED) |
| 185 | pgprot = PAGE_KERNEL; |
| 186 | else |
| 187 | pgprot = pgprot_writecombine(PAGE_KERNEL); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 188 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 189 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 190 | int npages_this_entry = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE; |
| 191 | struct page *page = sg_page(sg); |
| 192 | BUG_ON(i >= npages); |
| 193 | for (j = 0; j < npages_this_entry; j++) { |
| 194 | *(tmp++) = page++; |
| 195 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 196 | } |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 197 | vaddr = vmap(pages, npages, VM_MAP, pgprot); |
| 198 | kfree(pages); |
| 199 | |
| 200 | return vaddr; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void ion_system_heap_unmap_kernel(struct ion_heap *heap, |
| 204 | struct ion_buffer *buffer) |
| 205 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 206 | vunmap(buffer->vaddr); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | int ion_system_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 210 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 211 | { |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 212 | struct sg_table *table = buffer->priv_virt; |
| 213 | unsigned long addr = vma->vm_start; |
| 214 | unsigned long offset = vma->vm_pgoff; |
| 215 | struct scatterlist *sg; |
| 216 | int i; |
| 217 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 218 | if (!ION_IS_CACHED(buffer->flags)) { |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 219 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 220 | return -EINVAL; |
| 221 | } |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 222 | |
| 223 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 224 | if (offset) { |
| 225 | offset--; |
| 226 | continue; |
| 227 | } |
| 228 | remap_pfn_range(vma, addr, page_to_pfn(sg_page(sg)), |
| 229 | sg_dma_len(sg), vma->vm_page_prot); |
| 230 | addr += sg_dma_len(sg); |
| 231 | } |
| 232 | return 0; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 235 | static int ion_system_print_debug(struct ion_heap *heap, struct seq_file *s, |
| 236 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 237 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 238 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 239 | (unsigned long) atomic_read(&system_heap_allocated)); |
| 240 | |
| 241 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 244 | static struct ion_heap_ops vmalloc_ops = { |
| 245 | .allocate = ion_system_heap_allocate, |
| 246 | .free = ion_system_heap_free, |
| 247 | .map_dma = ion_system_heap_map_dma, |
| 248 | .unmap_dma = ion_system_heap_unmap_dma, |
| 249 | .map_kernel = ion_system_heap_map_kernel, |
| 250 | .unmap_kernel = ion_system_heap_unmap_kernel, |
| 251 | .map_user = ion_system_heap_map_user, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 252 | .print_debug = ion_system_print_debug, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 255 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *pheap) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 256 | { |
| 257 | struct ion_heap *heap; |
| 258 | |
| 259 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 260 | if (!heap) |
| 261 | return ERR_PTR(-ENOMEM); |
| 262 | heap->ops = &vmalloc_ops; |
| 263 | heap->type = ION_HEAP_TYPE_SYSTEM; |
| 264 | return heap; |
| 265 | } |
| 266 | |
| 267 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 268 | { |
| 269 | kfree(heap); |
| 270 | } |
| 271 | |
| 272 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 273 | struct ion_buffer *buffer, |
| 274 | unsigned long len, |
| 275 | unsigned long align, |
| 276 | unsigned long flags) |
| 277 | { |
| 278 | buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| 279 | if (!buffer->priv_virt) |
| 280 | return -ENOMEM; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 281 | atomic_add(len, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| 286 | { |
| 287 | kfree(buffer->priv_virt); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 288 | atomic_sub(buffer->size, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 292 | struct ion_buffer *buffer, |
| 293 | ion_phys_addr_t *addr, size_t *len) |
| 294 | { |
| 295 | *addr = virt_to_phys(buffer->priv_virt); |
| 296 | *len = buffer->size; |
| 297 | return 0; |
| 298 | } |
| 299 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 300 | struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 301 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 302 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 303 | struct sg_table *table; |
| 304 | int ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 305 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 306 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 307 | if (!table) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 308 | return ERR_PTR(-ENOMEM); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 309 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 310 | if (ret) { |
| 311 | kfree(table); |
| 312 | return ERR_PTR(ret); |
| 313 | } |
| 314 | sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 315 | 0); |
| 316 | return table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 319 | void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 320 | struct ion_buffer *buffer) |
| 321 | { |
| 322 | sg_free_table(buffer->sg_table); |
| 323 | kfree(buffer->sg_table); |
| 324 | } |
| 325 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 326 | int ion_system_contig_heap_map_user(struct ion_heap *heap, |
| 327 | struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 328 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 329 | { |
| 330 | unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt)); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 331 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 332 | if (ION_IS_CACHED(buffer->flags)) |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 333 | return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 334 | vma->vm_end - vma->vm_start, |
| 335 | vma->vm_page_prot); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 336 | else { |
| 337 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 338 | return -EINVAL; |
| 339 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 342 | static int ion_system_contig_print_debug(struct ion_heap *heap, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 343 | struct seq_file *s, |
| 344 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 345 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 346 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 347 | (unsigned long) atomic_read(&system_contig_heap_allocated)); |
| 348 | |
| 349 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 352 | void *ion_system_contig_heap_map_kernel(struct ion_heap *heap, |
| 353 | struct ion_buffer *buffer) |
| 354 | { |
| 355 | return buffer->priv_virt; |
| 356 | } |
| 357 | |
| 358 | void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap, |
| 359 | struct ion_buffer *buffer) |
| 360 | { |
| 361 | return; |
| 362 | } |
| 363 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 364 | static struct ion_heap_ops kmalloc_ops = { |
| 365 | .allocate = ion_system_contig_heap_allocate, |
| 366 | .free = ion_system_contig_heap_free, |
| 367 | .phys = ion_system_contig_heap_phys, |
| 368 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 369 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 370 | .map_kernel = ion_system_contig_heap_map_kernel, |
| 371 | .unmap_kernel = ion_system_contig_heap_unmap_kernel, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 372 | .map_user = ion_system_contig_heap_map_user, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 373 | .print_debug = ion_system_contig_print_debug, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 374 | }; |
| 375 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 376 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *pheap) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 377 | { |
| 378 | struct ion_heap *heap; |
| 379 | |
| 380 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 381 | if (!heap) |
| 382 | return ERR_PTR(-ENOMEM); |
| 383 | heap->ops = &kmalloc_ops; |
| 384 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 385 | return heap; |
| 386 | } |
| 387 | |
| 388 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 389 | { |
| 390 | kfree(heap); |
| 391 | } |
| 392 | |