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;
42 unsigned long size = PAGE_ALIGN(len);
43 unsigned long nr_pages = size >> PAGE_SHIFT;
44 unsigned long align = get_order(size);
47 if (align > CONFIG_CMA_ALIGNMENT)
48 align = CONFIG_CMA_ALIGNMENT;
50 pages = cma_alloc(cma_heap->cma, nr_pages, align, GFP_KERNEL);
54 table = kmalloc(sizeof(*table), GFP_KERNEL);
58 ret = sg_alloc_table(table, 1, GFP_KERNEL);
62 sg_set_page(table->sgl, pages, size, 0);
64 buffer->priv_virt = pages;
65 buffer->sg_table = table;
71 cma_release(cma_heap->cma, pages, nr_pages);
75 static void ion_cma_free(struct ion_buffer *buffer)
77 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
78 struct page *pages = buffer->priv_virt;
79 unsigned long nr_pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT;
82 cma_release(cma_heap->cma, pages, nr_pages);
83 /* release sg table */
84 sg_free_table(buffer->sg_table);
85 kfree(buffer->sg_table);
88 static struct ion_heap_ops ion_cma_ops = {
89 .allocate = ion_cma_allocate,
91 .map_user = ion_heap_map_user,
92 .map_kernel = ion_heap_map_kernel,
93 .unmap_kernel = ion_heap_unmap_kernel,
96 static struct ion_heap *__ion_cma_heap_create(struct cma *cma)
98 struct ion_cma_heap *cma_heap;
100 cma_heap = kzalloc(sizeof(*cma_heap), GFP_KERNEL);
103 return ERR_PTR(-ENOMEM);
105 cma_heap->heap.ops = &ion_cma_ops;
107 * get device from private heaps data, later it will be
108 * used to make the link with reserved CMA memory
111 cma_heap->heap.type = ION_HEAP_TYPE_DMA;
112 return &cma_heap->heap;
115 static int __ion_add_cma_heaps(struct cma *cma, void *data)
117 struct ion_heap *heap;
119 heap = __ion_cma_heap_create(cma);
121 return PTR_ERR(heap);
123 heap->name = cma_get_name(cma);
125 ion_device_add_heap(heap);
129 static int ion_add_cma_heaps(void)
131 cma_for_each_area(__ion_add_cma_heaps, NULL);
134 device_initcall(ion_add_cma_heaps);