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 | |
| 93 | if (!cached) { |
| 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]; |
| 127 | return info; |
| 128 | } |
John Stultz | f4ea823 | 2013-12-16 21:07:52 -0800 | [diff] [blame] | 129 | kfree(info); |
| 130 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 134 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 135 | struct ion_buffer *buffer, |
| 136 | unsigned long size, unsigned long align, |
| 137 | unsigned long flags) |
| 138 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 139 | struct ion_system_heap *sys_heap = container_of(heap, |
| 140 | struct ion_system_heap, |
| 141 | heap); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 142 | struct sg_table *table; |
| 143 | struct scatterlist *sg; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 144 | int ret; |
| 145 | struct list_head pages; |
| 146 | struct page_info *info, *tmp_info; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 147 | int i = 0; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 148 | long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 149 | unsigned int max_order = orders[0]; |
| 150 | |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 151 | if (align > PAGE_SIZE) |
| 152 | return -EINVAL; |
| 153 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 154 | INIT_LIST_HEAD(&pages); |
| 155 | while (size_remaining > 0) { |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 156 | info = alloc_largest_available(sys_heap, buffer, size_remaining, |
| 157 | max_order); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 158 | if (!info) |
| 159 | goto err; |
| 160 | list_add_tail(&info->list, &pages); |
| 161 | size_remaining -= (1 << info->order) * PAGE_SIZE; |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 162 | max_order = info->order; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 163 | i++; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 164 | } |
John Stultz | ea725ec | 2013-12-13 19:26:18 -0800 | [diff] [blame] | 165 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 166 | if (!table) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 167 | goto err; |
| 168 | |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 169 | ret = sg_alloc_table(table, i, GFP_KERNEL); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 170 | if (ret) |
| 171 | goto err1; |
| 172 | |
| 173 | sg = table->sgl; |
| 174 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| 175 | struct page *page = info->page; |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 176 | sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0); |
| 177 | sg = sg_next(sg); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 178 | list_del(&info->list); |
Rebecca Schultz Zavin | 708f0ca | 2013-12-13 14:23:59 -0800 | [diff] [blame] | 179 | kfree(info); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 180 | } |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 181 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 182 | buffer->priv_virt = table; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 183 | return 0; |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 184 | err1: |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 185 | kfree(table); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 186 | err: |
John Stultz | ea725ec | 2013-12-13 19:26:18 -0800 | [diff] [blame] | 187 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 188 | free_buffer_page(sys_heap, buffer, info->page, info->order); |
Rebecca Schultz Zavin | 708f0ca | 2013-12-13 14:23:59 -0800 | [diff] [blame] | 189 | kfree(info); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 190 | } |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 191 | return -ENOMEM; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 194 | static void ion_system_heap_free(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 195 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 196 | struct ion_heap *heap = buffer->heap; |
| 197 | struct ion_system_heap *sys_heap = container_of(heap, |
| 198 | struct ion_system_heap, |
| 199 | heap); |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 200 | struct sg_table *table = buffer->sg_table; |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 201 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 202 | struct scatterlist *sg; |
| 203 | LIST_HEAD(pages); |
| 204 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 205 | |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 206 | /* uncached pages come from the page pools, zero them before returning |
| 207 | for security purposes (other allocations are zerod at alloc time */ |
| 208 | if (!cached) |
| 209 | ion_heap_buffer_zero(buffer); |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 210 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 211 | for_each_sg(table->sgl, sg, table->nents, i) |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 212 | free_buffer_page(sys_heap, buffer, sg_page(sg), |
Colin Cross | 06e0dca | 2013-12-13 14:25:02 -0800 | [diff] [blame] | 213 | get_order(sg->length)); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 214 | sg_free_table(table); |
| 215 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 218 | static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 219 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 220 | { |
| 221 | return buffer->priv_virt; |
| 222 | } |
| 223 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 224 | static void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 225 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 226 | { |
| 227 | return; |
| 228 | } |
| 229 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 230 | static struct ion_heap_ops system_heap_ops = { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 231 | .allocate = ion_system_heap_allocate, |
| 232 | .free = ion_system_heap_free, |
| 233 | .map_dma = ion_system_heap_map_dma, |
| 234 | .unmap_dma = ion_system_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 235 | .map_kernel = ion_heap_map_kernel, |
| 236 | .unmap_kernel = ion_heap_unmap_kernel, |
| 237 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
John Stultz | b1aced6 | 2013-12-13 19:26:35 -0800 | [diff] [blame] | 240 | static unsigned long ion_system_heap_shrink_count(struct shrinker *shrinker, |
| 241 | struct shrink_control *sc) |
| 242 | { |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 243 | struct ion_heap *heap = container_of(shrinker, struct ion_heap, |
| 244 | shrinker); |
| 245 | struct ion_system_heap *sys_heap = container_of(heap, |
| 246 | struct ion_system_heap, |
| 247 | heap); |
| 248 | int nr_total = 0; |
John Stultz | b1aced6 | 2013-12-13 19:26:35 -0800 | [diff] [blame] | 249 | int i; |
| 250 | |
| 251 | /* total number of items is whatever the page pools are holding |
| 252 | plus whatever's in the freelist */ |
| 253 | for (i = 0; i < num_orders; i++) { |
| 254 | struct ion_page_pool *pool = sys_heap->pools[i]; |
| 255 | nr_total += ion_page_pool_shrink(pool, sc->gfp_mask, 0); |
| 256 | } |
| 257 | nr_total += ion_heap_freelist_size(heap) / PAGE_SIZE; |
| 258 | return nr_total; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | static unsigned long ion_system_heap_shrink_scan(struct shrinker *shrinker, |
| 263 | struct shrink_control *sc) |
| 264 | { |
| 265 | |
| 266 | struct ion_heap *heap = container_of(shrinker, struct ion_heap, |
| 267 | shrinker); |
| 268 | struct ion_system_heap *sys_heap = container_of(heap, |
| 269 | struct ion_system_heap, |
| 270 | heap); |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 271 | int nr_freed = 0; |
| 272 | int i; |
| 273 | |
| 274 | if (sc->nr_to_scan == 0) |
| 275 | goto end; |
| 276 | |
| 277 | /* shrink the free list first, no point in zeroing the memory if |
| 278 | we're just going to reclaim it */ |
| 279 | nr_freed += ion_heap_freelist_drain(heap, sc->nr_to_scan * PAGE_SIZE) / |
| 280 | PAGE_SIZE; |
| 281 | |
| 282 | if (nr_freed >= sc->nr_to_scan) |
| 283 | goto end; |
| 284 | |
| 285 | for (i = 0; i < num_orders; i++) { |
| 286 | struct ion_page_pool *pool = sys_heap->pools[i]; |
| 287 | |
| 288 | nr_freed += ion_page_pool_shrink(pool, sc->gfp_mask, |
| 289 | sc->nr_to_scan); |
| 290 | if (nr_freed >= sc->nr_to_scan) |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | end: |
John Stultz | b1aced6 | 2013-12-13 19:26:35 -0800 | [diff] [blame] | 295 | return nr_freed; |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 296 | |
| 297 | } |
| 298 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 299 | static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s, |
| 300 | void *unused) |
| 301 | { |
| 302 | |
| 303 | struct ion_system_heap *sys_heap = container_of(heap, |
| 304 | struct ion_system_heap, |
| 305 | heap); |
| 306 | int i; |
| 307 | for (i = 0; i < num_orders; i++) { |
| 308 | struct ion_page_pool *pool = sys_heap->pools[i]; |
Rebecca Schultz Zavin | 0fb9b81 | 2013-12-13 14:24:13 -0800 | [diff] [blame] | 309 | seq_printf(s, "%d order %u highmem pages in pool = %lu total\n", |
| 310 | pool->high_count, pool->order, |
| 311 | (1 << pool->order) * PAGE_SIZE * pool->high_count); |
| 312 | seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n", |
| 313 | pool->low_count, pool->order, |
| 314 | (1 << pool->order) * PAGE_SIZE * pool->low_count); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 319 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused) |
| 320 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 321 | struct ion_system_heap *heap; |
| 322 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 323 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 324 | heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 325 | if (!heap) |
| 326 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 327 | heap->heap.ops = &system_heap_ops; |
| 328 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 329 | heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 330 | heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders, |
| 331 | GFP_KERNEL); |
| 332 | if (!heap->pools) |
| 333 | goto err_alloc_pools; |
| 334 | for (i = 0; i < num_orders; i++) { |
| 335 | struct ion_page_pool *pool; |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 336 | gfp_t gfp_flags = low_order_gfp_flags; |
| 337 | |
| 338 | if (orders[i] > 4) |
| 339 | gfp_flags = high_order_gfp_flags; |
| 340 | pool = ion_page_pool_create(gfp_flags, orders[i]); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 341 | if (!pool) |
| 342 | goto err_create_pool; |
| 343 | heap->pools[i] = pool; |
| 344 | } |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 345 | |
John Stultz | b1aced6 | 2013-12-13 19:26:35 -0800 | [diff] [blame] | 346 | heap->heap.shrinker.scan_objects = ion_system_heap_shrink_scan; |
| 347 | heap->heap.shrinker.count_objects = ion_system_heap_shrink_count; |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 348 | heap->heap.shrinker.seeks = DEFAULT_SEEKS; |
| 349 | heap->heap.shrinker.batch = 0; |
| 350 | register_shrinker(&heap->heap.shrinker); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 351 | heap->heap.debug_show = ion_system_heap_debug_show; |
| 352 | return &heap->heap; |
| 353 | err_create_pool: |
| 354 | for (i = 0; i < num_orders; i++) |
| 355 | if (heap->pools[i]) |
| 356 | ion_page_pool_destroy(heap->pools[i]); |
| 357 | kfree(heap->pools); |
| 358 | err_alloc_pools: |
| 359 | kfree(heap); |
| 360 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 364 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 365 | struct ion_system_heap *sys_heap = container_of(heap, |
| 366 | struct ion_system_heap, |
| 367 | heap); |
| 368 | int i; |
| 369 | |
| 370 | for (i = 0; i < num_orders; i++) |
| 371 | ion_page_pool_destroy(sys_heap->pools[i]); |
| 372 | kfree(sys_heap->pools); |
| 373 | kfree(sys_heap); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 377 | struct ion_buffer *buffer, |
| 378 | unsigned long len, |
| 379 | unsigned long align, |
| 380 | unsigned long flags) |
| 381 | { |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 382 | int order = get_order(len); |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 383 | struct page *page; |
| 384 | struct sg_table *table; |
| 385 | unsigned long i; |
| 386 | int ret; |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 387 | |
| 388 | if (align > (PAGE_SIZE << order)) |
| 389 | return -EINVAL; |
| 390 | |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 391 | page = alloc_pages(low_order_gfp_flags, order); |
| 392 | if (!page) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 393 | return -ENOMEM; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 394 | |
| 395 | split_page(page, order); |
| 396 | |
| 397 | len = PAGE_ALIGN(len); |
| 398 | for (i = len >> PAGE_SHIFT; i < (1 << order); i++) |
| 399 | __free_page(page + i); |
| 400 | |
| 401 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 402 | if (!table) { |
| 403 | ret = -ENOMEM; |
| 404 | goto out; |
| 405 | } |
| 406 | |
| 407 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 408 | if (ret) |
| 409 | goto out; |
| 410 | |
| 411 | sg_set_page(table->sgl, page, len, 0); |
| 412 | |
| 413 | buffer->priv_virt = table; |
| 414 | |
| 415 | ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL); |
| 416 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 417 | return 0; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 418 | |
| 419 | out: |
| 420 | for (i = 0; i < len >> PAGE_SHIFT; i++) |
| 421 | __free_page(page + i); |
| 422 | kfree(table); |
| 423 | return ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 426 | static void ion_system_contig_heap_free(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 427 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 428 | struct sg_table *table = buffer->priv_virt; |
| 429 | struct page *page = sg_page(table->sgl); |
| 430 | unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT; |
| 431 | unsigned long i; |
| 432 | |
| 433 | for (i = 0; i < pages; i++) |
| 434 | __free_page(page + i); |
| 435 | sg_free_table(table); |
| 436 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 440 | struct ion_buffer *buffer, |
| 441 | ion_phys_addr_t *addr, size_t *len) |
| 442 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 443 | struct sg_table *table = buffer->priv_virt; |
| 444 | struct page *page = sg_page(table->sgl); |
| 445 | *addr = page_to_phys(page); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 446 | *len = buffer->size; |
| 447 | return 0; |
| 448 | } |
| 449 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 450 | 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] | 451 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 452 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 453 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 456 | static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 457 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 458 | { |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 461 | static struct ion_heap_ops kmalloc_ops = { |
| 462 | .allocate = ion_system_contig_heap_allocate, |
| 463 | .free = ion_system_contig_heap_free, |
| 464 | .phys = ion_system_contig_heap_phys, |
| 465 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 466 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 467 | .map_kernel = ion_heap_map_kernel, |
| 468 | .unmap_kernel = ion_heap_unmap_kernel, |
Colin Cross | a82130f | 2013-12-13 19:26:15 -0800 | [diff] [blame] | 469 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused) |
| 473 | { |
| 474 | struct ion_heap *heap; |
| 475 | |
| 476 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 477 | if (!heap) |
| 478 | return ERR_PTR(-ENOMEM); |
| 479 | heap->ops = &kmalloc_ops; |
| 480 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
| 481 | return heap; |
| 482 | } |
| 483 | |
| 484 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 485 | { |
| 486 | kfree(heap); |
| 487 | } |
| 488 | |