Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/staging/android/ion/ion_system_heap.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 17 | #include <asm/page.h> |
| 18 | #include <linux/dma-mapping.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 19 | #include <linux/err.h> |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 20 | #include <linux/highmem.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/scatterlist.h> |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 23 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include "ion.h" |
| 27 | #include "ion_priv.h" |
| 28 | |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 29 | static unsigned int high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | |
Arve Hjønnevåg | 1584f40 | 2013-12-13 14:24:41 -0800 | [diff] [blame] | 30 | __GFP_NOWARN | __GFP_NORETRY) & |
| 31 | ~__GFP_WAIT; |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 32 | static unsigned int low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | |
| 33 | __GFP_NOWARN); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 34 | static const unsigned int orders[] = {8, 4, 0}; |
| 35 | static const int num_orders = ARRAY_SIZE(orders); |
| 36 | static int order_to_index(unsigned int order) |
| 37 | { |
| 38 | int i; |
| 39 | for (i = 0; i < num_orders; i++) |
| 40 | if (order == orders[i]) |
| 41 | return i; |
| 42 | BUG(); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | static unsigned int order_to_size(int order) |
| 47 | { |
| 48 | return PAGE_SIZE << order; |
| 49 | } |
| 50 | |
| 51 | struct ion_system_heap { |
| 52 | struct ion_heap heap; |
| 53 | struct ion_page_pool **pools; |
| 54 | }; |
| 55 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 56 | struct page_info { |
| 57 | struct page *page; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 58 | unsigned int order; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 59 | struct list_head list; |
| 60 | }; |
| 61 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 62 | static struct page *alloc_buffer_page(struct ion_system_heap *heap, |
| 63 | struct ion_buffer *buffer, |
| 64 | unsigned long order) |
| 65 | { |
| 66 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 67 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
| 68 | struct page *page; |
Rebecca Schultz Zavin | b0599c0 | 2013-12-13 14:24:08 -0800 | [diff] [blame] | 69 | |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 70 | if (!cached) { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 71 | page = ion_page_pool_alloc(pool); |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 72 | } else { |
| 73 | gfp_t gfp_flags = low_order_gfp_flags; |
| 74 | |
| 75 | if (order > 4) |
| 76 | gfp_flags = high_order_gfp_flags; |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 77 | page = ion_heap_alloc_pages(buffer, gfp_flags, order); |
Rebecca Schultz Zavin | 8fae831 | 2013-12-13 14:24:18 -0800 | [diff] [blame] | 78 | if (!page) |
| 79 | return 0; |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 80 | ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order, |
| 81 | DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 82 | } |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 83 | if (!page) |
| 84 | return 0; |
Rebecca Schultz Zavin | 8fae831 | 2013-12-13 14:24:18 -0800 | [diff] [blame] | 85 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 86 | return page; |
| 87 | } |
| 88 | |
| 89 | static void free_buffer_page(struct ion_system_heap *heap, |
| 90 | struct ion_buffer *buffer, struct page *page, |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 91 | unsigned int order) |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 92 | { |
| 93 | bool cached = ion_buffer_cached(buffer); |
| 94 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| 95 | int i; |
| 96 | |
| 97 | if (!cached) { |
| 98 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 99 | ion_page_pool_free(pool, page); |
| 100 | } else if (split_pages) { |
| 101 | for (i = 0; i < (1 << order); i++) |
| 102 | __free_page(page + i); |
| 103 | } else { |
| 104 | __free_pages(page, order); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | static struct page_info *alloc_largest_available(struct ion_system_heap *heap, |
| 110 | struct ion_buffer *buffer, |
| 111 | unsigned long size, |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 112 | unsigned int max_order) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 113 | { |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 114 | struct page *page; |
| 115 | struct page_info *info; |
| 116 | int i; |
| 117 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 118 | for (i = 0; i < num_orders; i++) { |
| 119 | if (size < order_to_size(orders[i])) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 120 | continue; |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 121 | if (max_order < orders[i]) |
| 122 | continue; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 123 | |
| 124 | page = alloc_buffer_page(heap, buffer, orders[i]); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 125 | if (!page) |
| 126 | continue; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 127 | |
| 128 | info = kmalloc(sizeof(struct page_info), GFP_KERNEL); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 129 | info->page = page; |
| 130 | info->order = orders[i]; |
| 131 | return info; |
| 132 | } |
| 133 | return NULL; |
| 134 | } |
| 135 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 136 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 137 | struct ion_buffer *buffer, |
| 138 | unsigned long size, unsigned long align, |
| 139 | unsigned long flags) |
| 140 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 141 | struct ion_system_heap *sys_heap = container_of(heap, |
| 142 | struct ion_system_heap, |
| 143 | heap); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 144 | struct sg_table *table; |
| 145 | struct scatterlist *sg; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 146 | int ret; |
| 147 | struct list_head pages; |
| 148 | struct page_info *info, *tmp_info; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 149 | int i = 0; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 150 | long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 151 | unsigned int max_order = orders[0]; |
| 152 | |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame^] | 153 | if (align > PAGE_SIZE) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | if (ion_buffer_fault_user_mappings(buffer)) |
| 157 | return -EINVAL; |
| 158 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 159 | INIT_LIST_HEAD(&pages); |
| 160 | while (size_remaining > 0) { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 161 | info = alloc_largest_available(sys_heap, buffer, size_remaining, max_order); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 162 | if (!info) |
| 163 | goto err; |
| 164 | list_add_tail(&info->list, &pages); |
| 165 | size_remaining -= (1 << info->order) * PAGE_SIZE; |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 166 | max_order = info->order; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 167 | i++; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 168 | } |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 169 | |
| 170 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 171 | if (!table) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 172 | goto err; |
| 173 | |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 174 | ret = sg_alloc_table(table, i, GFP_KERNEL); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 175 | if (ret) |
| 176 | goto err1; |
| 177 | |
| 178 | sg = table->sgl; |
| 179 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| 180 | struct page *page = info->page; |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 181 | sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0); |
| 182 | sg = sg_next(sg); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 183 | list_del(&info->list); |
Rebecca Schultz Zavin | 708f0ca | 2013-12-13 14:23:59 -0800 | [diff] [blame] | 184 | kfree(info); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 185 | } |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 186 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 187 | buffer->priv_virt = table; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 188 | return 0; |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 189 | err1: |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 190 | kfree(table); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 191 | err: |
| 192 | list_for_each_entry(info, &pages, list) { |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 193 | free_buffer_page(sys_heap, buffer, info->page, info->order); |
Rebecca Schultz Zavin | 708f0ca | 2013-12-13 14:23:59 -0800 | [diff] [blame] | 194 | kfree(info); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 195 | } |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 196 | return -ENOMEM; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void ion_system_heap_free(struct ion_buffer *buffer) |
| 200 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 201 | struct ion_heap *heap = buffer->heap; |
| 202 | struct ion_system_heap *sys_heap = container_of(heap, |
| 203 | struct ion_system_heap, |
| 204 | heap); |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 205 | struct sg_table *table = buffer->sg_table; |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 206 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 207 | struct scatterlist *sg; |
| 208 | LIST_HEAD(pages); |
| 209 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 210 | |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 211 | /* uncached pages come from the page pools, zero them before returning |
| 212 | for security purposes (other allocations are zerod at alloc time */ |
| 213 | if (!cached) |
| 214 | ion_heap_buffer_zero(buffer); |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 215 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 216 | for_each_sg(table->sgl, sg, table->nents, i) |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 217 | free_buffer_page(sys_heap, buffer, sg_page(sg), |
Colin Cross | 06e0dca | 2013-12-13 14:25:02 -0800 | [diff] [blame] | 218 | get_order(sg->length)); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 219 | sg_free_table(table); |
| 220 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 223 | struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 224 | struct ion_buffer *buffer) |
| 225 | { |
| 226 | return buffer->priv_virt; |
| 227 | } |
| 228 | |
| 229 | void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 230 | struct ion_buffer *buffer) |
| 231 | { |
| 232 | return; |
| 233 | } |
| 234 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 235 | static struct ion_heap_ops system_heap_ops = { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 236 | .allocate = ion_system_heap_allocate, |
| 237 | .free = ion_system_heap_free, |
| 238 | .map_dma = ion_system_heap_map_dma, |
| 239 | .unmap_dma = ion_system_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 240 | .map_kernel = ion_heap_map_kernel, |
| 241 | .unmap_kernel = ion_heap_unmap_kernel, |
| 242 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 243 | }; |
| 244 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 245 | static int ion_system_heap_shrink(struct shrinker *shrinker, |
| 246 | struct shrink_control *sc) { |
| 247 | |
| 248 | struct ion_heap *heap = container_of(shrinker, struct ion_heap, |
| 249 | shrinker); |
| 250 | struct ion_system_heap *sys_heap = container_of(heap, |
| 251 | struct ion_system_heap, |
| 252 | heap); |
| 253 | int nr_total = 0; |
| 254 | int nr_freed = 0; |
| 255 | int i; |
| 256 | |
| 257 | if (sc->nr_to_scan == 0) |
| 258 | goto end; |
| 259 | |
| 260 | /* shrink the free list first, no point in zeroing the memory if |
| 261 | we're just going to reclaim it */ |
| 262 | nr_freed += ion_heap_freelist_drain(heap, sc->nr_to_scan * PAGE_SIZE) / |
| 263 | PAGE_SIZE; |
| 264 | |
| 265 | if (nr_freed >= sc->nr_to_scan) |
| 266 | goto end; |
| 267 | |
| 268 | for (i = 0; i < num_orders; i++) { |
| 269 | struct ion_page_pool *pool = sys_heap->pools[i]; |
| 270 | |
| 271 | nr_freed += ion_page_pool_shrink(pool, sc->gfp_mask, |
| 272 | sc->nr_to_scan); |
| 273 | if (nr_freed >= sc->nr_to_scan) |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | end: |
| 278 | /* total number of items is whatever the page pools are holding |
| 279 | plus whatever's in the freelist */ |
| 280 | for (i = 0; i < num_orders; i++) { |
| 281 | struct ion_page_pool *pool = sys_heap->pools[i]; |
| 282 | nr_total += ion_page_pool_shrink(pool, sc->gfp_mask, 0); |
| 283 | } |
| 284 | nr_total += ion_heap_freelist_size(heap) / PAGE_SIZE; |
| 285 | return nr_total; |
| 286 | |
| 287 | } |
| 288 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 289 | static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s, |
| 290 | void *unused) |
| 291 | { |
| 292 | |
| 293 | struct ion_system_heap *sys_heap = container_of(heap, |
| 294 | struct ion_system_heap, |
| 295 | heap); |
| 296 | int i; |
| 297 | for (i = 0; i < num_orders; i++) { |
| 298 | struct ion_page_pool *pool = sys_heap->pools[i]; |
Rebecca Schultz Zavin | 0fb9b81 | 2013-12-13 14:24:13 -0800 | [diff] [blame] | 299 | seq_printf(s, "%d order %u highmem pages in pool = %lu total\n", |
| 300 | pool->high_count, pool->order, |
| 301 | (1 << pool->order) * PAGE_SIZE * pool->high_count); |
| 302 | seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n", |
| 303 | pool->low_count, pool->order, |
| 304 | (1 << pool->order) * PAGE_SIZE * pool->low_count); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 309 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused) |
| 310 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 311 | struct ion_system_heap *heap; |
| 312 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 313 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 314 | heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 315 | if (!heap) |
| 316 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 317 | heap->heap.ops = &system_heap_ops; |
| 318 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 319 | heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 320 | heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders, |
| 321 | GFP_KERNEL); |
| 322 | if (!heap->pools) |
| 323 | goto err_alloc_pools; |
| 324 | for (i = 0; i < num_orders; i++) { |
| 325 | struct ion_page_pool *pool; |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 326 | gfp_t gfp_flags = low_order_gfp_flags; |
| 327 | |
| 328 | if (orders[i] > 4) |
| 329 | gfp_flags = high_order_gfp_flags; |
| 330 | pool = ion_page_pool_create(gfp_flags, orders[i]); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 331 | if (!pool) |
| 332 | goto err_create_pool; |
| 333 | heap->pools[i] = pool; |
| 334 | } |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 335 | |
| 336 | heap->heap.shrinker.shrink = ion_system_heap_shrink; |
| 337 | heap->heap.shrinker.seeks = DEFAULT_SEEKS; |
| 338 | heap->heap.shrinker.batch = 0; |
| 339 | register_shrinker(&heap->heap.shrinker); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 340 | heap->heap.debug_show = ion_system_heap_debug_show; |
| 341 | return &heap->heap; |
| 342 | err_create_pool: |
| 343 | for (i = 0; i < num_orders; i++) |
| 344 | if (heap->pools[i]) |
| 345 | ion_page_pool_destroy(heap->pools[i]); |
| 346 | kfree(heap->pools); |
| 347 | err_alloc_pools: |
| 348 | kfree(heap); |
| 349 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 353 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 354 | struct ion_system_heap *sys_heap = container_of(heap, |
| 355 | struct ion_system_heap, |
| 356 | heap); |
| 357 | int i; |
| 358 | |
| 359 | for (i = 0; i < num_orders; i++) |
| 360 | ion_page_pool_destroy(sys_heap->pools[i]); |
| 361 | kfree(sys_heap->pools); |
| 362 | kfree(sys_heap); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 366 | struct ion_buffer *buffer, |
| 367 | unsigned long len, |
| 368 | unsigned long align, |
| 369 | unsigned long flags) |
| 370 | { |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame^] | 371 | int order = get_order(len); |
| 372 | |
| 373 | if (align > (PAGE_SIZE << order)) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | if (ion_buffer_fault_user_mappings(buffer)) |
| 377 | return -EINVAL; |
| 378 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 379 | buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| 380 | if (!buffer->priv_virt) |
| 381 | return -ENOMEM; |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| 386 | { |
| 387 | kfree(buffer->priv_virt); |
| 388 | } |
| 389 | |
| 390 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 391 | struct ion_buffer *buffer, |
| 392 | ion_phys_addr_t *addr, size_t *len) |
| 393 | { |
| 394 | *addr = virt_to_phys(buffer->priv_virt); |
| 395 | *len = buffer->size; |
| 396 | return 0; |
| 397 | } |
| 398 | |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 399 | struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 400 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 401 | { |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 402 | struct sg_table *table; |
| 403 | int ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 404 | |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 405 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 406 | if (!table) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 407 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 4d5ca32 | 2013-12-13 14:23:37 -0800 | [diff] [blame] | 408 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 409 | if (ret) { |
| 410 | kfree(table); |
| 411 | return ERR_PTR(ret); |
| 412 | } |
| 413 | sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 414 | 0); |
| 415 | return table; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 418 | void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 419 | struct ion_buffer *buffer) |
| 420 | { |
| 421 | sg_free_table(buffer->sg_table); |
| 422 | kfree(buffer->sg_table); |
| 423 | } |
| 424 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 425 | int ion_system_contig_heap_map_user(struct ion_heap *heap, |
| 426 | struct ion_buffer *buffer, |
| 427 | struct vm_area_struct *vma) |
| 428 | { |
| 429 | unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt)); |
| 430 | return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, |
| 431 | vma->vm_end - vma->vm_start, |
| 432 | vma->vm_page_prot); |
| 433 | |
| 434 | } |
| 435 | |
| 436 | static struct ion_heap_ops kmalloc_ops = { |
| 437 | .allocate = ion_system_contig_heap_allocate, |
| 438 | .free = ion_system_contig_heap_free, |
| 439 | .phys = ion_system_contig_heap_phys, |
| 440 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 441 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 442 | .map_kernel = ion_heap_map_kernel, |
| 443 | .unmap_kernel = ion_heap_unmap_kernel, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 444 | .map_user = ion_system_contig_heap_map_user, |
| 445 | }; |
| 446 | |
| 447 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused) |
| 448 | { |
| 449 | struct ion_heap *heap; |
| 450 | |
| 451 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 452 | if (!heap) |
| 453 | return ERR_PTR(-ENOMEM); |
| 454 | heap->ops = &kmalloc_ops; |
| 455 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
| 456 | return heap; |
| 457 | } |
| 458 | |
| 459 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 460 | { |
| 461 | kfree(heap); |
| 462 | } |
| 463 | |