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> |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/vmalloc.h> |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 29 | #include "ion_priv.h" |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 30 | #include <mach/memory.h> |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 31 | #include <asm/cacheflush.h> |
Mitchel Humpherys | af2e5c5 | 2012-09-06 12:16:36 -0700 | [diff] [blame] | 32 | #include <linux/msm_ion.h> |
Neeti Desai | 3f3c282 | 2013-03-08 17:29:53 -0800 | [diff] [blame] | 33 | #include <linux/dma-mapping.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 34 | |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 35 | static atomic_t system_heap_allocated; |
| 36 | static atomic_t system_contig_heap_allocated; |
| 37 | |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 38 | static unsigned int high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | |
| 39 | __GFP_NOWARN | __GFP_NORETRY | |
Rebecca Schultz Zavin | 1797e59a | 2012-10-18 21:51:53 -0700 | [diff] [blame] | 40 | __GFP_NO_KSWAPD) & ~__GFP_WAIT; |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 41 | static unsigned int low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | |
| 42 | __GFP_NOWARN); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 43 | static const unsigned int orders[] = {8, 4, 0}; |
| 44 | static const int num_orders = ARRAY_SIZE(orders); |
| 45 | static int order_to_index(unsigned int order) |
| 46 | { |
| 47 | int i; |
| 48 | for (i = 0; i < num_orders; i++) |
| 49 | if (order == orders[i]) |
| 50 | return i; |
| 51 | BUG(); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | static unsigned int order_to_size(int order) |
| 56 | { |
| 57 | return PAGE_SIZE << order; |
| 58 | } |
| 59 | |
| 60 | struct ion_system_heap { |
| 61 | struct ion_heap heap; |
| 62 | struct ion_page_pool **pools; |
| 63 | }; |
| 64 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 65 | struct page_info { |
| 66 | struct page *page; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 67 | unsigned int order; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 68 | struct list_head list; |
| 69 | }; |
| 70 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 71 | static struct page *alloc_buffer_page(struct ion_system_heap *heap, |
| 72 | struct ion_buffer *buffer, |
| 73 | unsigned long order) |
| 74 | { |
| 75 | bool cached = ion_buffer_cached(buffer); |
| 76 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| 77 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
| 78 | struct page *page; |
Rebecca Schultz Zavin | 96dd58d | 2012-09-26 10:58:30 -0700 | [diff] [blame] | 79 | |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 80 | if (!cached) { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 81 | page = ion_page_pool_alloc(pool); |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 82 | } else { |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 83 | struct scatterlist sg; |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 84 | gfp_t gfp_flags = low_order_gfp_flags; |
| 85 | |
| 86 | if (order > 4) |
| 87 | gfp_flags = high_order_gfp_flags; |
| 88 | page = alloc_pages(gfp_flags, order); |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 89 | if (!page) |
| 90 | return 0; |
| 91 | sg_init_table(&sg, 1); |
| 92 | sg_set_page(&sg, page, PAGE_SIZE << order, 0); |
| 93 | dma_sync_sg_for_device(NULL, &sg, 1, DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 94 | } |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 95 | if (!page) |
| 96 | return 0; |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 97 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 98 | if (split_pages) |
| 99 | split_page(page, order); |
| 100 | return page; |
| 101 | } |
| 102 | |
| 103 | static void free_buffer_page(struct ion_system_heap *heap, |
| 104 | struct ion_buffer *buffer, struct page *page, |
| 105 | unsigned int order) |
| 106 | { |
| 107 | bool cached = ion_buffer_cached(buffer); |
| 108 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| 109 | int i; |
| 110 | |
| 111 | if (!cached) { |
| 112 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
| 113 | /* zero the pages before returning them to the pool for |
| 114 | security. This uses vmap as we want to set the pgprot so |
| 115 | the writes to occur to noncached mappings, as the pool's |
| 116 | purpose is to keep the pages out of the cache */ |
Rebecca Schultz Zavin | 430fca9 | 2012-12-03 11:43:49 -0800 | [diff] [blame] | 117 | for (i = 0; i < (1 << order); i++) { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 118 | struct page *sub_page = page + i; |
| 119 | void *addr = vmap(&sub_page, 1, VM_MAP, |
| 120 | pgprot_writecombine(PAGE_KERNEL)); |
| 121 | memset(addr, 0, PAGE_SIZE); |
| 122 | vunmap(addr); |
| 123 | } |
| 124 | ion_page_pool_free(pool, page); |
| 125 | } else if (split_pages) { |
| 126 | for (i = 0; i < (1 << order); i++) |
| 127 | __free_page(page + i); |
| 128 | } else { |
| 129 | __free_pages(page, order); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static struct page_info *alloc_largest_available(struct ion_system_heap *heap, |
| 135 | struct ion_buffer *buffer, |
| 136 | unsigned long size, |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 137 | unsigned int max_order) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 138 | { |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 139 | struct page *page; |
| 140 | struct page_info *info; |
| 141 | int i; |
| 142 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 143 | for (i = 0; i < num_orders; i++) { |
| 144 | if (size < order_to_size(orders[i])) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 145 | continue; |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 146 | if (max_order < orders[i]) |
| 147 | continue; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 148 | |
| 149 | page = alloc_buffer_page(heap, buffer, orders[i]); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 150 | if (!page) |
| 151 | continue; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 152 | |
| 153 | info = kmalloc(sizeof(struct page_info), GFP_KERNEL); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 154 | info->page = page; |
| 155 | info->order = orders[i]; |
| 156 | return info; |
| 157 | } |
| 158 | return NULL; |
| 159 | } |
| 160 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 161 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 162 | struct ion_buffer *buffer, |
| 163 | unsigned long size, unsigned long align, |
| 164 | unsigned long flags) |
| 165 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 166 | struct ion_system_heap *sys_heap = container_of(heap, |
| 167 | struct ion_system_heap, |
| 168 | heap); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 169 | struct sg_table *table; |
| 170 | struct scatterlist *sg; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 171 | int ret; |
| 172 | struct list_head pages; |
| 173 | struct page_info *info, *tmp_info; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 174 | int i = 0; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 175 | long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 176 | unsigned int max_order = orders[0]; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 177 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 178 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 179 | INIT_LIST_HEAD(&pages); |
| 180 | while (size_remaining > 0) { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 181 | info = alloc_largest_available(sys_heap, buffer, size_remaining, max_order); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 182 | if (!info) |
| 183 | goto err; |
| 184 | list_add_tail(&info->list, &pages); |
| 185 | size_remaining -= (1 << info->order) * PAGE_SIZE; |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 186 | max_order = info->order; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 187 | i++; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 188 | } |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 189 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 190 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 191 | if (!table) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 192 | goto err; |
| 193 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 194 | if (split_pages) |
| 195 | ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE, |
| 196 | GFP_KERNEL); |
| 197 | else |
| 198 | ret = sg_alloc_table(table, i, GFP_KERNEL); |
| 199 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 200 | if (ret) |
| 201 | goto err1; |
| 202 | |
| 203 | sg = table->sgl; |
| 204 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| 205 | struct page *page = info->page; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 206 | if (split_pages) { |
| 207 | for (i = 0; i < (1 << info->order); i++) { |
| 208 | sg_set_page(sg, page + i, PAGE_SIZE, 0); |
| 209 | sg = sg_next(sg); |
| 210 | } |
| 211 | } else { |
| 212 | sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, |
| 213 | 0); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 214 | sg = sg_next(sg); |
| 215 | } |
| 216 | list_del(&info->list); |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame] | 217 | kfree(info); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 218 | } |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 219 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 220 | buffer->priv_virt = table; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 221 | atomic_add(size, &system_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 222 | return 0; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 223 | err1: |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 224 | kfree(table); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 225 | err: |
| 226 | list_for_each_entry(info, &pages, list) { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 227 | free_buffer_page(sys_heap, buffer, info->page, info->order); |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame] | 228 | kfree(info); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 229 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 230 | return -ENOMEM; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void ion_system_heap_free(struct ion_buffer *buffer) |
| 234 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 235 | struct ion_heap *heap = buffer->heap; |
| 236 | struct ion_system_heap *sys_heap = container_of(heap, |
| 237 | struct ion_system_heap, |
| 238 | heap); |
Rebecca Schultz Zavin | 3df181c | 2012-11-15 10:43:46 -0800 | [diff] [blame^] | 239 | struct sg_table *table = buffer->sg_table; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 240 | struct scatterlist *sg; |
| 241 | LIST_HEAD(pages); |
| 242 | int i; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 243 | |
| 244 | for_each_sg(table->sgl, sg, table->nents, i) |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 245 | free_buffer_page(sys_heap, buffer, sg_page(sg), get_order(sg_dma_len(sg))); |
| 246 | sg_free_table(table); |
| 247 | kfree(table); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 248 | atomic_sub(buffer->size, &system_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 251 | struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 252 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 253 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 254 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 258 | struct ion_buffer *buffer) |
| 259 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 260 | return; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 263 | static int ion_system_print_debug(struct ion_heap *heap, struct seq_file *s, |
| 264 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 265 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 266 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 267 | (unsigned long) atomic_read(&system_heap_allocated)); |
| 268 | |
| 269 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 272 | static struct ion_heap_ops system_heap_ops = { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 273 | .allocate = ion_system_heap_allocate, |
| 274 | .free = ion_system_heap_free, |
| 275 | .map_dma = ion_system_heap_map_dma, |
| 276 | .unmap_dma = ion_system_heap_unmap_dma, |
Rebecca Schultz Zavin | 3df181c | 2012-11-15 10:43:46 -0800 | [diff] [blame^] | 277 | .map_kernel = ion_heap_map_kernel, |
| 278 | .unmap_kernel = ion_heap_unmap_kernel, |
| 279 | .map_user = ion_heap_map_user, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 280 | .print_debug = ion_system_print_debug, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 283 | 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] | 284 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 285 | struct ion_system_heap *heap; |
| 286 | int i; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 287 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 288 | heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 289 | if (!heap) |
| 290 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 291 | heap->heap.ops = &system_heap_ops; |
| 292 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; |
| 293 | heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders, |
| 294 | GFP_KERNEL); |
| 295 | if (!heap->pools) |
| 296 | goto err_alloc_pools; |
| 297 | for (i = 0; i < num_orders; i++) { |
| 298 | struct ion_page_pool *pool; |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 299 | gfp_t gfp_flags = low_order_gfp_flags; |
| 300 | |
| 301 | if (orders[i] > 4) |
| 302 | gfp_flags = high_order_gfp_flags; |
| 303 | pool = ion_page_pool_create(gfp_flags, orders[i]); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 304 | if (!pool) |
| 305 | goto err_create_pool; |
| 306 | heap->pools[i] = pool; |
| 307 | } |
| 308 | return &heap->heap; |
| 309 | err_create_pool: |
| 310 | for (i = 0; i < num_orders; i++) |
| 311 | if (heap->pools[i]) |
| 312 | ion_page_pool_destroy(heap->pools[i]); |
| 313 | kfree(heap->pools); |
| 314 | err_alloc_pools: |
| 315 | kfree(heap); |
| 316 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 320 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 321 | struct ion_system_heap *sys_heap = container_of(heap, |
| 322 | struct ion_system_heap, |
| 323 | heap); |
| 324 | int i; |
| 325 | |
| 326 | for (i = 0; i < num_orders; i++) |
| 327 | ion_page_pool_destroy(sys_heap->pools[i]); |
| 328 | kfree(sys_heap->pools); |
| 329 | kfree(sys_heap); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 333 | struct ion_buffer *buffer, |
| 334 | unsigned long len, |
| 335 | unsigned long align, |
| 336 | unsigned long flags) |
| 337 | { |
| 338 | buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| 339 | if (!buffer->priv_virt) |
| 340 | return -ENOMEM; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 341 | atomic_add(len, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| 346 | { |
| 347 | kfree(buffer->priv_virt); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 348 | atomic_sub(buffer->size, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 352 | struct ion_buffer *buffer, |
| 353 | ion_phys_addr_t *addr, size_t *len) |
| 354 | { |
| 355 | *addr = virt_to_phys(buffer->priv_virt); |
| 356 | *len = buffer->size; |
| 357 | return 0; |
| 358 | } |
| 359 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 360 | 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] | 361 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 362 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 363 | struct sg_table *table; |
| 364 | int ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 365 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 366 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 367 | if (!table) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 368 | return ERR_PTR(-ENOMEM); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 369 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 370 | if (ret) { |
| 371 | kfree(table); |
| 372 | return ERR_PTR(ret); |
| 373 | } |
| 374 | sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 375 | 0); |
| 376 | return table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 379 | void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 380 | struct ion_buffer *buffer) |
| 381 | { |
| 382 | sg_free_table(buffer->sg_table); |
| 383 | kfree(buffer->sg_table); |
| 384 | } |
| 385 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 386 | int ion_system_contig_heap_map_user(struct ion_heap *heap, |
| 387 | struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 388 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 389 | { |
| 390 | unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt)); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 391 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 392 | if (ION_IS_CACHED(buffer->flags)) |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 393 | 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] | 394 | vma->vm_end - vma->vm_start, |
| 395 | vma->vm_page_prot); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 396 | else { |
| 397 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 398 | return -EINVAL; |
| 399 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 402 | static int ion_system_contig_print_debug(struct ion_heap *heap, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 403 | struct seq_file *s, |
| 404 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 405 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 406 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 407 | (unsigned long) atomic_read(&system_contig_heap_allocated)); |
| 408 | |
| 409 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 412 | void *ion_system_contig_heap_map_kernel(struct ion_heap *heap, |
| 413 | struct ion_buffer *buffer) |
| 414 | { |
| 415 | return buffer->priv_virt; |
| 416 | } |
| 417 | |
| 418 | void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap, |
| 419 | struct ion_buffer *buffer) |
| 420 | { |
| 421 | return; |
| 422 | } |
| 423 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 424 | static struct ion_heap_ops kmalloc_ops = { |
| 425 | .allocate = ion_system_contig_heap_allocate, |
| 426 | .free = ion_system_contig_heap_free, |
| 427 | .phys = ion_system_contig_heap_phys, |
| 428 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 429 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rebecca Schultz Zavin | 3df181c | 2012-11-15 10:43:46 -0800 | [diff] [blame^] | 430 | .map_kernel = ion_heap_map_kernel, |
| 431 | .unmap_kernel = ion_heap_unmap_kernel, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 432 | .map_user = ion_system_contig_heap_map_user, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 433 | .print_debug = ion_system_contig_print_debug, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 434 | }; |
| 435 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 436 | 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] | 437 | { |
| 438 | struct ion_heap *heap; |
| 439 | |
| 440 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 441 | if (!heap) |
| 442 | return ERR_PTR(-ENOMEM); |
| 443 | heap->ops = &kmalloc_ops; |
| 444 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 445 | return heap; |
| 446 | } |
| 447 | |
| 448 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 449 | { |
| 450 | kfree(heap); |
| 451 | } |
| 452 | |