2 * drivers/staging/android/ion/ion_chunk_heap.c
4 * Copyright (C) 2012 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.
16 #include <linux/dma-mapping.h>
17 #include <linux/err.h>
18 #include <linux/genalloc.h>
21 #include <linux/scatterlist.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
26 struct ion_chunk_heap {
28 struct gen_pool *pool;
30 unsigned long chunk_size;
32 unsigned long allocated;
35 static int ion_chunk_heap_allocate(struct ion_heap *heap,
36 struct ion_buffer *buffer,
40 struct ion_chunk_heap *chunk_heap =
41 container_of(heap, struct ion_chunk_heap, heap);
42 struct sg_table *table;
43 struct scatterlist *sg;
45 unsigned long num_chunks;
46 unsigned long allocated_size;
48 allocated_size = ALIGN(size, chunk_heap->chunk_size);
49 num_chunks = allocated_size / chunk_heap->chunk_size;
51 if (allocated_size > chunk_heap->size - chunk_heap->allocated)
54 table = kmalloc(sizeof(*table), GFP_KERNEL);
57 ret = sg_alloc_table(table, num_chunks, GFP_KERNEL);
64 for (i = 0; i < num_chunks; i++) {
65 unsigned long paddr = gen_pool_alloc(chunk_heap->pool,
66 chunk_heap->chunk_size);
69 sg_set_page(sg, pfn_to_page(PFN_DOWN(paddr)),
70 chunk_heap->chunk_size, 0);
74 buffer->sg_table = table;
75 chunk_heap->allocated += allocated_size;
79 for (i -= 1; i >= 0; i--) {
80 gen_pool_free(chunk_heap->pool, page_to_phys(sg_page(sg)),
89 static void ion_chunk_heap_free(struct ion_buffer *buffer)
91 struct ion_heap *heap = buffer->heap;
92 struct ion_chunk_heap *chunk_heap =
93 container_of(heap, struct ion_chunk_heap, heap);
94 struct sg_table *table = buffer->sg_table;
95 struct scatterlist *sg;
97 unsigned long allocated_size;
99 allocated_size = ALIGN(buffer->size, chunk_heap->chunk_size);
101 ion_heap_buffer_zero(buffer);
103 for_each_sg(table->sgl, sg, table->nents, i) {
104 gen_pool_free(chunk_heap->pool, page_to_phys(sg_page(sg)),
107 chunk_heap->allocated -= allocated_size;
108 sg_free_table(table);
112 static struct ion_heap_ops chunk_heap_ops = {
113 .allocate = ion_chunk_heap_allocate,
114 .free = ion_chunk_heap_free,
115 .map_user = ion_heap_map_user,
116 .map_kernel = ion_heap_map_kernel,
117 .unmap_kernel = ion_heap_unmap_kernel,
120 struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *heap_data)
122 struct ion_chunk_heap *chunk_heap;
127 page = pfn_to_page(PFN_DOWN(heap_data->base));
128 size = heap_data->size;
130 ret = ion_heap_pages_zero(page, size, pgprot_writecombine(PAGE_KERNEL));
134 chunk_heap = kzalloc(sizeof(*chunk_heap), GFP_KERNEL);
136 return ERR_PTR(-ENOMEM);
138 chunk_heap->chunk_size = (unsigned long)heap_data->priv;
139 chunk_heap->pool = gen_pool_create(get_order(chunk_heap->chunk_size) +
141 if (!chunk_heap->pool) {
143 goto error_gen_pool_create;
145 chunk_heap->base = heap_data->base;
146 chunk_heap->size = heap_data->size;
147 chunk_heap->allocated = 0;
149 gen_pool_add(chunk_heap->pool, chunk_heap->base, heap_data->size, -1);
150 chunk_heap->heap.ops = &chunk_heap_ops;
151 chunk_heap->heap.type = ION_HEAP_TYPE_CHUNK;
152 chunk_heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
153 pr_debug("%s: base %pa size %zu\n", __func__,
154 &chunk_heap->base, heap_data->size);
156 return &chunk_heap->heap;
158 error_gen_pool_create: