Merge tag 'for-linus-4.14b-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / staging / android / ion / ion_cma_heap.c
1 /*
2  * drivers/staging/android/ion/ion_cma_heap.c
3  *
4  * Copyright (C) Linaro 2012
5  * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
6  *
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.
10  *
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.
15  *
16  */
17
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>
24
25 #include "ion.h"
26
27 struct ion_cma_heap {
28         struct ion_heap heap;
29         struct cma *cma;
30 };
31
32 #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
33
34 /* ION CMA heap operations functions */
35 static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
36                             unsigned long len,
37                             unsigned long flags)
38 {
39         struct ion_cma_heap *cma_heap = to_cma_heap(heap);
40         struct sg_table *table;
41         struct page *pages;
42         int ret;
43
44         pages = cma_alloc(cma_heap->cma, len, 0, GFP_KERNEL);
45         if (!pages)
46                 return -ENOMEM;
47
48         table = kmalloc(sizeof(*table), GFP_KERNEL);
49         if (!table)
50                 goto err;
51
52         ret = sg_alloc_table(table, 1, GFP_KERNEL);
53         if (ret)
54                 goto free_mem;
55
56         sg_set_page(table->sgl, pages, len, 0);
57
58         buffer->priv_virt = pages;
59         buffer->sg_table = table;
60         return 0;
61
62 free_mem:
63         kfree(table);
64 err:
65         cma_release(cma_heap->cma, pages, buffer->size);
66         return -ENOMEM;
67 }
68
69 static void ion_cma_free(struct ion_buffer *buffer)
70 {
71         struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
72         struct page *pages = buffer->priv_virt;
73
74         /* release memory */
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);
79 }
80
81 static struct ion_heap_ops ion_cma_ops = {
82         .allocate = ion_cma_allocate,
83         .free = ion_cma_free,
84         .map_user = ion_heap_map_user,
85         .map_kernel = ion_heap_map_kernel,
86         .unmap_kernel = ion_heap_unmap_kernel,
87 };
88
89 static struct ion_heap *__ion_cma_heap_create(struct cma *cma)
90 {
91         struct ion_cma_heap *cma_heap;
92
93         cma_heap = kzalloc(sizeof(*cma_heap), GFP_KERNEL);
94
95         if (!cma_heap)
96                 return ERR_PTR(-ENOMEM);
97
98         cma_heap->heap.ops = &ion_cma_ops;
99         /*
100          * get device from private heaps data, later it will be
101          * used to make the link with reserved CMA memory
102          */
103         cma_heap->cma = cma;
104         cma_heap->heap.type = ION_HEAP_TYPE_DMA;
105         return &cma_heap->heap;
106 }
107
108 static int __ion_add_cma_heaps(struct cma *cma, void *data)
109 {
110         struct ion_heap *heap;
111
112         heap = __ion_cma_heap_create(cma);
113         if (IS_ERR(heap))
114                 return PTR_ERR(heap);
115
116         heap->name = cma_get_name(cma);
117
118         ion_device_add_heap(heap);
119         return 0;
120 }
121
122 static int ion_add_cma_heaps(void)
123 {
124         cma_for_each_area(__ion_add_cma_heaps, NULL);
125         return 0;
126 }
127 device_initcall(ion_add_cma_heaps);