2 * drivers/staging/android/ion/ion_cma_heap.c
4 * Copyright (C) Linaro 2012
5 * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 #include <linux/errno.h>
21 #include <linux/err.h>
22 #include <linux/cma.h>
23 #include <linux/scatterlist.h>
32 #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
34 /* ION CMA heap operations functions */
35 static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
39 struct ion_cma_heap *cma_heap = to_cma_heap(heap);
40 struct sg_table *table;
44 pages = cma_alloc(cma_heap->cma, len, 0, GFP_KERNEL);
48 table = kmalloc(sizeof(*table), GFP_KERNEL);
52 ret = sg_alloc_table(table, 1, GFP_KERNEL);
56 sg_set_page(table->sgl, pages, len, 0);
58 buffer->priv_virt = pages;
59 buffer->sg_table = table;
65 cma_release(cma_heap->cma, pages, buffer->size);
69 static void ion_cma_free(struct ion_buffer *buffer)
71 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
72 struct page *pages = buffer->priv_virt;
75 cma_release(cma_heap->cma, pages, buffer->size);
76 /* release sg table */
77 sg_free_table(buffer->sg_table);
78 kfree(buffer->sg_table);
81 static struct ion_heap_ops ion_cma_ops = {
82 .allocate = ion_cma_allocate,
84 .map_user = ion_heap_map_user,
85 .map_kernel = ion_heap_map_kernel,
86 .unmap_kernel = ion_heap_unmap_kernel,
89 static struct ion_heap *__ion_cma_heap_create(struct cma *cma)
91 struct ion_cma_heap *cma_heap;
93 cma_heap = kzalloc(sizeof(*cma_heap), GFP_KERNEL);
96 return ERR_PTR(-ENOMEM);
98 cma_heap->heap.ops = &ion_cma_ops;
100 * get device from private heaps data, later it will be
101 * used to make the link with reserved CMA memory
104 cma_heap->heap.type = ION_HEAP_TYPE_DMA;
105 return &cma_heap->heap;
108 static int __ion_add_cma_heaps(struct cma *cma, void *data)
110 struct ion_heap *heap;
112 heap = __ion_cma_heap_create(cma);
114 return PTR_ERR(heap);
116 heap->name = cma_get_name(cma);
118 ion_device_add_heap(heap);
122 static int ion_add_cma_heaps(void)
124 cma_for_each_area(__ion_add_cma_heaps, NULL);
127 device_initcall(ion_add_cma_heaps);