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