2 * drivers/staging/android/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 unsigned int high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO |
30 __GFP_NOWARN | __GFP_NORETRY) &
32 static unsigned int low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO |
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)
39 for (i = 0; i < num_orders; i++)
40 if (order == orders[i])
46 static unsigned int order_to_size(int order)
48 return PAGE_SIZE << order;
51 struct ion_system_heap {
53 struct ion_page_pool **pools;
59 struct list_head list;
62 static struct page *alloc_buffer_page(struct ion_system_heap *heap,
63 struct ion_buffer *buffer,
66 bool cached = ion_buffer_cached(buffer);
67 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
71 page = ion_page_pool_alloc(pool);
73 gfp_t gfp_flags = low_order_gfp_flags;
76 gfp_flags = high_order_gfp_flags;
77 page = ion_heap_alloc_pages(buffer, gfp_flags, order);
80 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order,
89 static void free_buffer_page(struct ion_system_heap *heap,
90 struct ion_buffer *buffer, struct page *page,
93 bool cached = ion_buffer_cached(buffer);
94 bool split_pages = ion_buffer_fault_user_mappings(buffer);
98 struct ion_page_pool *pool = heap->pools[order_to_index(order)];
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);
104 __free_pages(page, order);
109 static struct page_info *alloc_largest_available(struct ion_system_heap *heap,
110 struct ion_buffer *buffer,
112 unsigned int max_order)
115 struct page_info *info;
118 for (i = 0; i < num_orders; i++) {
119 if (size < order_to_size(orders[i]))
121 if (max_order < orders[i])
124 page = alloc_buffer_page(heap, buffer, orders[i]);
128 info = kmalloc(sizeof(struct page_info), GFP_KERNEL);
130 info->order = orders[i];
136 static int ion_system_heap_allocate(struct ion_heap *heap,
137 struct ion_buffer *buffer,
138 unsigned long size, unsigned long align,
141 struct ion_system_heap *sys_heap = container_of(heap,
142 struct ion_system_heap,
144 struct sg_table *table;
145 struct scatterlist *sg;
147 struct list_head pages;
148 struct page_info *info, *tmp_info;
150 long size_remaining = PAGE_ALIGN(size);
151 unsigned int max_order = orders[0];
153 INIT_LIST_HEAD(&pages);
154 while (size_remaining > 0) {
155 info = alloc_largest_available(sys_heap, buffer, size_remaining, max_order);
158 list_add_tail(&info->list, &pages);
159 size_remaining -= (1 << info->order) * PAGE_SIZE;
160 max_order = info->order;
164 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
168 ret = sg_alloc_table(table, i, GFP_KERNEL);
173 list_for_each_entry_safe(info, tmp_info, &pages, list) {
174 struct page *page = info->page;
175 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, 0);
177 list_del(&info->list);
181 buffer->priv_virt = table;
186 list_for_each_entry(info, &pages, list) {
187 free_buffer_page(sys_heap, buffer, info->page, info->order);
193 void ion_system_heap_free(struct ion_buffer *buffer)
195 struct ion_heap *heap = buffer->heap;
196 struct ion_system_heap *sys_heap = container_of(heap,
197 struct ion_system_heap,
199 struct sg_table *table = buffer->sg_table;
200 bool cached = ion_buffer_cached(buffer);
201 struct scatterlist *sg;
205 /* uncached pages come from the page pools, zero them before returning
206 for security purposes (other allocations are zerod at alloc time */
208 ion_heap_buffer_zero(buffer);
210 for_each_sg(table->sgl, sg, table->nents, i)
211 free_buffer_page(sys_heap, buffer, sg_page(sg),
212 get_order(sg_dma_len(sg)));
213 sg_free_table(table);
217 struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
218 struct ion_buffer *buffer)
220 return buffer->priv_virt;
223 void ion_system_heap_unmap_dma(struct ion_heap *heap,
224 struct ion_buffer *buffer)
229 static struct ion_heap_ops system_heap_ops = {
230 .allocate = ion_system_heap_allocate,
231 .free = ion_system_heap_free,
232 .map_dma = ion_system_heap_map_dma,
233 .unmap_dma = ion_system_heap_unmap_dma,
234 .map_kernel = ion_heap_map_kernel,
235 .unmap_kernel = ion_heap_unmap_kernel,
236 .map_user = ion_heap_map_user,
239 static int ion_system_heap_shrink(struct shrinker *shrinker,
240 struct shrink_control *sc) {
242 struct ion_heap *heap = container_of(shrinker, struct ion_heap,
244 struct ion_system_heap *sys_heap = container_of(heap,
245 struct ion_system_heap,
251 if (sc->nr_to_scan == 0)
254 /* shrink the free list first, no point in zeroing the memory if
255 we're just going to reclaim it */
256 nr_freed += ion_heap_freelist_drain(heap, sc->nr_to_scan * PAGE_SIZE) /
259 if (nr_freed >= sc->nr_to_scan)
262 for (i = 0; i < num_orders; i++) {
263 struct ion_page_pool *pool = sys_heap->pools[i];
265 nr_freed += ion_page_pool_shrink(pool, sc->gfp_mask,
267 if (nr_freed >= sc->nr_to_scan)
272 /* total number of items is whatever the page pools are holding
273 plus whatever's in the freelist */
274 for (i = 0; i < num_orders; i++) {
275 struct ion_page_pool *pool = sys_heap->pools[i];
276 nr_total += ion_page_pool_shrink(pool, sc->gfp_mask, 0);
278 nr_total += ion_heap_freelist_size(heap) / PAGE_SIZE;
283 static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
287 struct ion_system_heap *sys_heap = container_of(heap,
288 struct ion_system_heap,
291 for (i = 0; i < num_orders; i++) {
292 struct ion_page_pool *pool = sys_heap->pools[i];
293 seq_printf(s, "%d order %u highmem pages in pool = %lu total\n",
294 pool->high_count, pool->order,
295 (1 << pool->order) * PAGE_SIZE * pool->high_count);
296 seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n",
297 pool->low_count, pool->order,
298 (1 << pool->order) * PAGE_SIZE * pool->low_count);
303 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
305 struct ion_system_heap *heap;
308 heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL);
310 return ERR_PTR(-ENOMEM);
311 heap->heap.ops = &system_heap_ops;
312 heap->heap.type = ION_HEAP_TYPE_SYSTEM;
313 heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
314 heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders,
317 goto err_alloc_pools;
318 for (i = 0; i < num_orders; i++) {
319 struct ion_page_pool *pool;
320 gfp_t gfp_flags = low_order_gfp_flags;
323 gfp_flags = high_order_gfp_flags;
324 pool = ion_page_pool_create(gfp_flags, orders[i]);
326 goto err_create_pool;
327 heap->pools[i] = pool;
330 heap->heap.shrinker.shrink = ion_system_heap_shrink;
331 heap->heap.shrinker.seeks = DEFAULT_SEEKS;
332 heap->heap.shrinker.batch = 0;
333 register_shrinker(&heap->heap.shrinker);
334 heap->heap.debug_show = ion_system_heap_debug_show;
337 for (i = 0; i < num_orders; i++)
339 ion_page_pool_destroy(heap->pools[i]);
343 return ERR_PTR(-ENOMEM);
346 void ion_system_heap_destroy(struct ion_heap *heap)
348 struct ion_system_heap *sys_heap = container_of(heap,
349 struct ion_system_heap,
353 for (i = 0; i < num_orders; i++)
354 ion_page_pool_destroy(sys_heap->pools[i]);
355 kfree(sys_heap->pools);
359 static int ion_system_contig_heap_allocate(struct ion_heap *heap,
360 struct ion_buffer *buffer,
365 buffer->priv_virt = kzalloc(len, GFP_KERNEL);
366 if (!buffer->priv_virt)
371 void ion_system_contig_heap_free(struct ion_buffer *buffer)
373 kfree(buffer->priv_virt);
376 static int ion_system_contig_heap_phys(struct ion_heap *heap,
377 struct ion_buffer *buffer,
378 ion_phys_addr_t *addr, size_t *len)
380 *addr = virt_to_phys(buffer->priv_virt);
385 struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
386 struct ion_buffer *buffer)
388 struct sg_table *table;
391 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
393 return ERR_PTR(-ENOMEM);
394 ret = sg_alloc_table(table, 1, GFP_KERNEL);
399 sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size,
404 void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
405 struct ion_buffer *buffer)
407 sg_free_table(buffer->sg_table);
408 kfree(buffer->sg_table);
411 int ion_system_contig_heap_map_user(struct ion_heap *heap,
412 struct ion_buffer *buffer,
413 struct vm_area_struct *vma)
415 unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt));
416 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
417 vma->vm_end - vma->vm_start,
422 static struct ion_heap_ops kmalloc_ops = {
423 .allocate = ion_system_contig_heap_allocate,
424 .free = ion_system_contig_heap_free,
425 .phys = ion_system_contig_heap_phys,
426 .map_dma = ion_system_contig_heap_map_dma,
427 .unmap_dma = ion_system_contig_heap_unmap_dma,
428 .map_kernel = ion_heap_map_kernel,
429 .unmap_kernel = ion_heap_unmap_kernel,
430 .map_user = ion_system_contig_heap_map_user,
433 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
435 struct ion_heap *heap;
437 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
439 return ERR_PTR(-ENOMEM);
440 heap->ops = &kmalloc_ops;
441 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
445 void ion_system_contig_heap_destroy(struct ion_heap *heap)