2 * drivers/gpu/ion/ion_system_heap.c
4 * Copyright (C) 2011 Google, Inc.
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.
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.
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/highmem.h>
22 #include <linux/scatterlist.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
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);
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)
37 for (i = 0; i < num_orders; i++)
38 if (order == orders[i])
44 static unsigned int order_to_size(int order)
46 return PAGE_SIZE << order;
49 struct ion_system_heap {
51 struct ion_page_pool **pools;
57 struct list_head list;
60 static struct page *alloc_buffer_page(struct ion_system_heap *heap,
61 struct ion_buffer *buffer,
64 bool cached = ion_buffer_cached(buffer);
65 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
69 page = ion_page_pool_alloc(pool);
71 gfp_t gfp_flags = low_order_gfp_flags;
74 gfp_flags = high_order_gfp_flags;
75 page = alloc_pages(gfp_flags, order);
78 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order,
87 static void free_buffer_page(struct ion_system_heap *heap,
88 struct ion_buffer *buffer, struct page *page,
91 bool cached = ion_buffer_cached(buffer);
93 if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) {
94 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
95 ion_page_pool_free(pool, page);
97 __free_pages(page, order);
102 static struct page_info *alloc_largest_available(struct ion_system_heap *heap,
103 struct ion_buffer *buffer,
105 unsigned int max_order)
108 struct page_info *info;
111 info = kmalloc(sizeof(struct page_info), GFP_KERNEL);
115 for (i = 0; i < num_orders; i++) {
116 if (size < order_to_size(orders[i]))
118 if (max_order < orders[i])
121 page = alloc_buffer_page(heap, buffer, orders[i]);
126 info->order = orders[i];
127 INIT_LIST_HEAD(&info->list);
135 static int ion_system_heap_allocate(struct ion_heap *heap,
136 struct ion_buffer *buffer,
137 unsigned long size, unsigned long align,
140 struct ion_system_heap *sys_heap = container_of(heap,
141 struct ion_system_heap,
143 struct sg_table *table;
144 struct scatterlist *sg;
146 struct list_head pages;
147 struct page_info *info, *tmp_info;
149 unsigned long size_remaining = PAGE_ALIGN(size);
150 unsigned int max_order = orders[0];
152 if (align > PAGE_SIZE)
155 if (size / PAGE_SIZE > totalram_pages / 2)
158 INIT_LIST_HEAD(&pages);
159 while (size_remaining > 0) {
160 info = alloc_largest_available(sys_heap, buffer, size_remaining,
164 list_add_tail(&info->list, &pages);
165 size_remaining -= (1 << info->order) * PAGE_SIZE;
166 max_order = info->order;
169 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
173 ret = sg_alloc_table(table, i, GFP_KERNEL);
178 list_for_each_entry_safe(info, tmp_info, &pages, list) {
179 struct page *page = info->page;
180 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0);
182 list_del(&info->list);
186 buffer->priv_virt = table;
191 list_for_each_entry_safe(info, tmp_info, &pages, list) {
192 free_buffer_page(sys_heap, buffer, info->page, info->order);
198 static void ion_system_heap_free(struct ion_buffer *buffer)
200 struct ion_heap *heap = buffer->heap;
201 struct ion_system_heap *sys_heap = container_of(heap,
202 struct ion_system_heap,
204 struct sg_table *table = buffer->sg_table;
205 bool cached = ion_buffer_cached(buffer);
206 struct scatterlist *sg;
210 /* uncached pages come from the page pools, zero them before returning
211 for security purposes (other allocations are zerod at alloc time */
212 if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE))
213 ion_heap_buffer_zero(buffer);
215 for_each_sg(table->sgl, sg, table->nents, i)
216 free_buffer_page(sys_heap, buffer, sg_page(sg),
217 get_order(sg->length));
218 sg_free_table(table);
222 static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
223 struct ion_buffer *buffer)
225 return buffer->priv_virt;
228 static void ion_system_heap_unmap_dma(struct ion_heap *heap,
229 struct ion_buffer *buffer)
234 static int ion_system_heap_shrink(struct ion_heap *heap, gfp_t gfp_mask,
237 struct ion_system_heap *sys_heap;
241 sys_heap = container_of(heap, struct ion_system_heap, heap);
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);
251 static struct ion_heap_ops system_heap_ops = {
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,
256 .map_kernel = ion_heap_map_kernel,
257 .unmap_kernel = ion_heap_unmap_kernel,
258 .map_user = ion_heap_map_user,
259 .shrink = ion_system_heap_shrink,
262 static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
266 struct ion_system_heap *sys_heap = container_of(heap,
267 struct ion_system_heap,
270 for (i = 0; i < num_orders; i++) {
271 struct ion_page_pool *pool = sys_heap->pools[i];
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);
282 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
284 struct ion_system_heap *heap;
287 heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL);
289 return ERR_PTR(-ENOMEM);
290 heap->heap.ops = &system_heap_ops;
291 heap->heap.type = ION_HEAP_TYPE_SYSTEM;
292 heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
293 heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders,
296 goto err_alloc_pools;
297 for (i = 0; i < num_orders; i++) {
298 struct ion_page_pool *pool;
299 gfp_t gfp_flags = low_order_gfp_flags;
302 gfp_flags = high_order_gfp_flags;
303 pool = ion_page_pool_create(gfp_flags, orders[i]);
305 goto err_create_pool;
306 heap->pools[i] = pool;
309 heap->heap.debug_show = ion_system_heap_debug_show;
312 for (i = 0; i < num_orders; i++)
314 ion_page_pool_destroy(heap->pools[i]);
318 return ERR_PTR(-ENOMEM);
321 void ion_system_heap_destroy(struct ion_heap *heap)
323 struct ion_system_heap *sys_heap = container_of(heap,
324 struct ion_system_heap,
328 for (i = 0; i < num_orders; i++)
329 ion_page_pool_destroy(sys_heap->pools[i]);
330 kfree(sys_heap->pools);
334 static int ion_system_contig_heap_allocate(struct ion_heap *heap,
335 struct ion_buffer *buffer,
340 int order = get_order(len);
342 struct sg_table *table;
346 if (align > (PAGE_SIZE << order))
349 page = alloc_pages(low_order_gfp_flags, order);
353 split_page(page, order);
355 len = PAGE_ALIGN(len);
356 for (i = len >> PAGE_SHIFT; i < (1 << order); i++)
357 __free_page(page + i);
359 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
365 ret = sg_alloc_table(table, 1, GFP_KERNEL);
369 sg_set_page(table->sgl, page, len, 0);
371 buffer->priv_virt = table;
373 ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL);
378 for (i = 0; i < len >> PAGE_SHIFT; i++)
379 __free_page(page + i);
384 static void ion_system_contig_heap_free(struct ion_buffer *buffer)
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;
391 for (i = 0; i < pages; i++)
392 __free_page(page + i);
393 sg_free_table(table);
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)
401 struct sg_table *table = buffer->priv_virt;
402 struct page *page = sg_page(table->sgl);
403 *addr = page_to_phys(page);
408 static struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
409 struct ion_buffer *buffer)
411 return buffer->priv_virt;
414 static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
415 struct ion_buffer *buffer)
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,
424 .unmap_dma = ion_system_contig_heap_unmap_dma,
425 .map_kernel = ion_heap_map_kernel,
426 .unmap_kernel = ion_heap_unmap_kernel,
427 .map_user = ion_heap_map_user,
430 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
432 struct ion_heap *heap;
434 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
436 return ERR_PTR(-ENOMEM);
437 heap->ops = &kmalloc_ops;
438 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
442 void ion_system_contig_heap_destroy(struct ion_heap *heap)