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 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 29 | static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN | |
| 30 | __GFP_NORETRY) & ~__GFP_WAIT; |
| 31 | static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 32 | static const unsigned int orders[] = {8, 4, 0}; |
| 33 | static const int num_orders = ARRAY_SIZE(orders); |
| 34 | static int order_to_index(unsigned int order) |
| 35 | { |
| 36 | int i; |
| 37 | for (i = 0; i < num_orders; i++) |
| 38 | if (order == orders[i]) |
| 39 | return i; |
| 40 | BUG(); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | static unsigned int order_to_size(int order) |
| 45 | { |
| 46 | return PAGE_SIZE << order; |
| 47 | } |
| 48 | |
| 49 | struct ion_system_heap { |
| 50 | struct ion_heap heap; |
| 51 | struct ion_page_pool **pools; |
| 52 | }; |
| 53 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 54 | struct page_info { |
| 55 | struct page *page; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 56 | unsigned int order; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 57 | struct list_head list; |
| 58 | }; |
| 59 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 60 | static struct page *alloc_buffer_page(struct ion_system_heap *heap, |
| 61 | struct ion_buffer *buffer, |
| 62 | unsigned long order) |
| 63 | { |
| 64 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 65 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
| 66 | struct page *page; |
Rebecca Schultz Zavin | b0599c0 | 2013-12-13 14:24:08 -0800 | [diff] [blame] | 67 | |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 68 | if (!cached) { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 69 | page = ion_page_pool_alloc(pool); |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 70 | } else { |
| 71 | gfp_t gfp_flags = low_order_gfp_flags; |
| 72 | |
| 73 | if (order > 4) |
| 74 | gfp_flags = high_order_gfp_flags; |
Colin Cross | a305690 | 2013-12-13 19:26:25 -0800 | [diff] [blame] | 75 | page = alloc_pages(gfp_flags, order); |
Rebecca Schultz Zavin | 8fae831 | 2013-12-13 14:24:18 -0800 | [diff] [blame] | 76 | if (!page) |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 77 | return NULL; |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 78 | ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order, |
| 79 | DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 80 | } |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 81 | if (!page) |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 82 | return NULL; |
Rebecca Schultz Zavin | 8fae831 | 2013-12-13 14:24:18 -0800 | [diff] [blame] | 83 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 84 | return page; |
| 85 | } |
| 86 | |
| 87 | static void free_buffer_page(struct ion_system_heap *heap, |
| 88 | struct ion_buffer *buffer, struct page *page, |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 89 | unsigned int order) |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 90 | { |
| 91 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 92 | |
Mitchel Humpherys | 53a91c6 | 2014-02-17 13:58:39 -0800 | [diff] [blame^] | 93 | if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 94 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 95 | ion_page_pool_free(pool, page); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 96 | } else { |
| 97 | __free_pages(page, order); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static struct page_info *alloc_largest_available(struct ion_system_heap *heap, |
| 103 | struct ion_buffer *buffer, |
| 104 | unsigned long size, |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 105 | unsigned int max_order) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 106 | { |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 107 | struct page *page; |
| 108 | struct page_info *info; |
| 109 | int i; |
| 110 | |
John Stultz | f4ea823 | 2013-12-16 21:07:52 -0800 | [diff] [blame] | 111 | info = kmalloc(sizeof(struct page_info), GFP_KERNEL); |
| 112 | if (!info) |
| 113 | return NULL; |
| 114 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 115 | for (i = 0; i < num_orders; i++) { |
| 116 | if (size < order_to_size(orders[i])) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 117 | continue; |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 118 | if (max_order < orders[i]) |
| 119 | continue; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 120 | |
| 121 | page = alloc_buffer_page(heap, buffer, orders[i]); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 122 | if (!page) |
| 123 | continue; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 124 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 125 | info->page = page; |
| 126 | info->order = orders[i]; |
Colin Cross | c9e8440 | 2014-02-04 16:08:38 -0800 | [diff] [blame] | 127 | INIT_LIST_HEAD(&info->list); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 128 | return info; |
| 129 | } |
John Stultz | f4ea823 | 2013-12-16 21:07:52 -0800 | [diff] [blame] | 130 | kfree(info); |
| 131 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
| 134 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 135 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 136 | struct ion_buffer *buffer, |
| 137 | unsigned long size, unsigned long align, |
| 138 | unsigned long flags) |
| 139 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 140 | struct ion_system_heap *sys_heap = container_of(heap, |
| 141 | struct ion_system_heap, |
| 142 | heap); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 143 | struct sg_table *table; |
| 144 | struct scatterlist *sg; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 145 | int ret; |
| 146 | struct list_head pages; |
| 147 | struct page_info *info, *tmp_info; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 148 | int i = 0; |
Colin Cross | c9e8440 | 2014-02-04 16:08:38 -0800 | [diff] [blame] | 149 | unsigned long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 150 | unsigned int max_order = orders[0]; |
| 151 | |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 152 | if (align > PAGE_SIZE) |
| 153 | return -EINVAL; |
| 154 | |
Colin Cross | c9e8440 | 2014-02-04 16:08:38 -0800 | [diff] [blame] | 155 | if (size / PAGE_SIZE > totalram_pages / 2) |
| 156 | return -ENOMEM; |
| 157 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 158 | INIT_LIST_HEAD(&pages); |
| 159 | while (size_remaining > 0) { |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 160 | info = alloc_largest_available(sys_heap, buffer, size_remaining, |
| 161 | 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 | } |
John Stultz | ea725ec | 2013-12-13 19:26:18 -0800 | [diff] [blame] | 169 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 170 | if (!table) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 171 | goto err; |
| 172 | |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 173 | ret = sg_alloc_table(table, i, GFP_KERNEL); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 174 | if (ret) |
| 175 | goto err1; |
| 176 | |
| 177 | sg = table->sgl; |
| 178 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| 179 | struct page *page = info->page; |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 180 | sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0); |
| 181 | sg = sg_next(sg); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 182 | list_del(&info->list); |
Rebecca Schultz Zavin | 708f0ca | 2013-12-13 14:23:59 -0800 | [diff] [blame] | 183 | kfree(info); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 184 | } |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 185 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 186 | buffer->priv_virt = table; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 187 | return 0; |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 188 | err1: |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 189 | kfree(table); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 190 | err: |
John Stultz | ea725ec | 2013-12-13 19:26:18 -0800 | [diff] [blame] | 191 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 192 | free_buffer_page(sys_heap, buffer, info->page, info->order); |
Rebecca Schultz Zavin | 708f0ca | 2013-12-13 14:23:59 -0800 | [diff] [blame] | 193 | kfree(info); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 194 | } |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 195 | return -ENOMEM; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 198 | static void ion_system_heap_free(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 199 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 200 | struct ion_heap *heap = buffer->heap; |
| 201 | struct ion_system_heap *sys_heap = container_of(heap, |
| 202 | struct ion_system_heap, |
| 203 | heap); |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 204 | struct sg_table *table = buffer->sg_table; |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 205 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 206 | struct scatterlist *sg; |
| 207 | LIST_HEAD(pages); |
| 208 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 209 | |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 210 | /* uncached pages come from the page pools, zero them before returning |
| 211 | for security purposes (other allocations are zerod at alloc time */ |
Mitchel Humpherys | 53a91c6 | 2014-02-17 13:58:39 -0800 | [diff] [blame^] | 212 | if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 213 | ion_heap_buffer_zero(buffer); |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 214 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 215 | for_each_sg(table->sgl, sg, table->nents, i) |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 216 | free_buffer_page(sys_heap, buffer, sg_page(sg), |
Colin Cross | 06e0dca | 2013-12-13 14:25:02 -0800 | [diff] [blame] | 217 | get_order(sg->length)); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 218 | sg_free_table(table); |
| 219 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 222 | static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 223 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 224 | { |
| 225 | return buffer->priv_virt; |
| 226 | } |
| 227 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 228 | static void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 229 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 230 | { |
| 231 | return; |
| 232 | } |
| 233 | |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 234 | static int ion_system_heap_shrink(struct ion_heap *heap, gfp_t gfp_mask, |
| 235 | int nr_to_scan) |
| 236 | { |
| 237 | struct ion_system_heap *sys_heap; |
| 238 | int nr_total = 0; |
| 239 | int i; |
| 240 | |
| 241 | sys_heap = container_of(heap, struct ion_system_heap, heap); |
| 242 | |
| 243 | for (i = 0; i < num_orders; i++) { |
| 244 | struct ion_page_pool *pool = sys_heap->pools[i]; |
| 245 | nr_total += ion_page_pool_shrink(pool, gfp_mask, nr_to_scan); |
| 246 | } |
| 247 | |
| 248 | return nr_total; |
| 249 | } |
| 250 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 251 | static struct ion_heap_ops system_heap_ops = { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 252 | .allocate = ion_system_heap_allocate, |
| 253 | .free = ion_system_heap_free, |
| 254 | .map_dma = ion_system_heap_map_dma, |
| 255 | .unmap_dma = ion_system_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 256 | .map_kernel = ion_heap_map_kernel, |
| 257 | .unmap_kernel = ion_heap_unmap_kernel, |
| 258 | .map_user = ion_heap_map_user, |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 259 | .shrink = ion_system_heap_shrink, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 260 | }; |
| 261 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 262 | static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s, |
| 263 | void *unused) |
| 264 | { |
| 265 | |
| 266 | struct ion_system_heap *sys_heap = container_of(heap, |
| 267 | struct ion_system_heap, |
| 268 | heap); |
| 269 | int i; |
| 270 | for (i = 0; i < num_orders; i++) { |
| 271 | struct ion_page_pool *pool = sys_heap->pools[i]; |
Rebecca Schultz Zavin | 0fb9b81 | 2013-12-13 14:24:13 -0800 | [diff] [blame] | 272 | seq_printf(s, "%d order %u highmem pages in pool = %lu total\n", |
| 273 | pool->high_count, pool->order, |
| 274 | (1 << pool->order) * PAGE_SIZE * pool->high_count); |
| 275 | seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n", |
| 276 | pool->low_count, pool->order, |
| 277 | (1 << pool->order) * PAGE_SIZE * pool->low_count); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 282 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused) |
| 283 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 284 | struct ion_system_heap *heap; |
| 285 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 286 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 287 | heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 288 | if (!heap) |
| 289 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 290 | heap->heap.ops = &system_heap_ops; |
| 291 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 292 | heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 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 | ee4a498 | 2013-12-13 14:24:12 -0800 | [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 | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 304 | if (!pool) |
| 305 | goto err_create_pool; |
| 306 | heap->pools[i] = pool; |
| 307 | } |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 308 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 309 | heap->heap.debug_show = ion_system_heap_debug_show; |
| 310 | return &heap->heap; |
| 311 | err_create_pool: |
| 312 | for (i = 0; i < num_orders; i++) |
| 313 | if (heap->pools[i]) |
| 314 | ion_page_pool_destroy(heap->pools[i]); |
| 315 | kfree(heap->pools); |
| 316 | err_alloc_pools: |
| 317 | kfree(heap); |
| 318 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 322 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 323 | struct ion_system_heap *sys_heap = container_of(heap, |
| 324 | struct ion_system_heap, |
| 325 | heap); |
| 326 | int i; |
| 327 | |
| 328 | for (i = 0; i < num_orders; i++) |
| 329 | ion_page_pool_destroy(sys_heap->pools[i]); |
| 330 | kfree(sys_heap->pools); |
| 331 | kfree(sys_heap); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 335 | struct ion_buffer *buffer, |
| 336 | unsigned long len, |
| 337 | unsigned long align, |
| 338 | unsigned long flags) |
| 339 | { |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 340 | int order = get_order(len); |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 341 | struct page *page; |
| 342 | struct sg_table *table; |
| 343 | unsigned long i; |
| 344 | int ret; |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 345 | |
| 346 | if (align > (PAGE_SIZE << order)) |
| 347 | return -EINVAL; |
| 348 | |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 349 | page = alloc_pages(low_order_gfp_flags, order); |
| 350 | if (!page) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 351 | return -ENOMEM; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 352 | |
| 353 | split_page(page, order); |
| 354 | |
| 355 | len = PAGE_ALIGN(len); |
| 356 | for (i = len >> PAGE_SHIFT; i < (1 << order); i++) |
| 357 | __free_page(page + i); |
| 358 | |
| 359 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 360 | if (!table) { |
| 361 | ret = -ENOMEM; |
| 362 | goto out; |
| 363 | } |
| 364 | |
| 365 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 366 | if (ret) |
| 367 | goto out; |
| 368 | |
| 369 | sg_set_page(table->sgl, page, len, 0); |
| 370 | |
| 371 | buffer->priv_virt = table; |
| 372 | |
| 373 | ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL); |
| 374 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 375 | return 0; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 376 | |
| 377 | out: |
| 378 | for (i = 0; i < len >> PAGE_SHIFT; i++) |
| 379 | __free_page(page + i); |
| 380 | kfree(table); |
| 381 | return ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 384 | static void ion_system_contig_heap_free(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 385 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 386 | struct sg_table *table = buffer->priv_virt; |
| 387 | struct page *page = sg_page(table->sgl); |
| 388 | unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT; |
| 389 | unsigned long i; |
| 390 | |
| 391 | for (i = 0; i < pages; i++) |
| 392 | __free_page(page + i); |
| 393 | sg_free_table(table); |
| 394 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 398 | struct ion_buffer *buffer, |
| 399 | ion_phys_addr_t *addr, size_t *len) |
| 400 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 401 | struct sg_table *table = buffer->priv_virt; |
| 402 | struct page *page = sg_page(table->sgl); |
| 403 | *addr = page_to_phys(page); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 404 | *len = buffer->size; |
| 405 | return 0; |
| 406 | } |
| 407 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 408 | static 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] | 409 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 410 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 411 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 414 | static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 415 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 416 | { |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 419 | static struct ion_heap_ops kmalloc_ops = { |
| 420 | .allocate = ion_system_contig_heap_allocate, |
| 421 | .free = ion_system_contig_heap_free, |
| 422 | .phys = ion_system_contig_heap_phys, |
| 423 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 424 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 425 | .map_kernel = ion_heap_map_kernel, |
| 426 | .unmap_kernel = ion_heap_unmap_kernel, |
Colin Cross | a82130f | 2013-12-13 19:26:15 -0800 | [diff] [blame] | 427 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused) |
| 431 | { |
| 432 | struct ion_heap *heap; |
| 433 | |
| 434 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 435 | if (!heap) |
| 436 | return ERR_PTR(-ENOMEM); |
| 437 | heap->ops = &kmalloc_ops; |
| 438 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
| 439 | return heap; |
| 440 | } |
| 441 | |
| 442 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 443 | { |
| 444 | kfree(heap); |
| 445 | } |
| 446 | |