]>
git.ipfire.org Git - thirdparty/kernel/linux.git/blob - drivers/staging/android/ion/ion_cma_heap.c
6f6b1e208ec5f475be5ddd90ad8c8bb83638a71d
1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/staging/android/ion/ion_cma_heap.c
5 * Copyright (C) Linaro 2012
6 * Author: <benjamin.gaignard@linaro.org> for ST-Ericsson.
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/cma.h>
24 #include <linux/scatterlist.h>
33 #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
35 /* ION CMA heap operations functions */
36 static int ion_cma_allocate(struct ion_heap
*heap
, struct ion_buffer
*buffer
,
40 struct ion_cma_heap
*cma_heap
= to_cma_heap(heap
);
41 struct sg_table
*table
;
45 pages
= cma_alloc(cma_heap
->cma
, len
, 0, GFP_KERNEL
);
49 table
= kmalloc(sizeof(*table
), GFP_KERNEL
);
53 ret
= sg_alloc_table(table
, 1, GFP_KERNEL
);
57 sg_set_page(table
->sgl
, pages
, len
, 0);
59 buffer
->priv_virt
= pages
;
60 buffer
->sg_table
= table
;
66 cma_release(cma_heap
->cma
, pages
, buffer
->size
);
70 static void ion_cma_free(struct ion_buffer
*buffer
)
72 struct ion_cma_heap
*cma_heap
= to_cma_heap(buffer
->heap
);
73 struct page
*pages
= buffer
->priv_virt
;
76 cma_release(cma_heap
->cma
, pages
, buffer
->size
);
77 /* release sg table */
78 sg_free_table(buffer
->sg_table
);
79 kfree(buffer
->sg_table
);
82 static struct ion_heap_ops ion_cma_ops
= {
83 .allocate
= ion_cma_allocate
,
85 .map_user
= ion_heap_map_user
,
86 .map_kernel
= ion_heap_map_kernel
,
87 .unmap_kernel
= ion_heap_unmap_kernel
,
90 static struct ion_heap
*__ion_cma_heap_create(struct cma
*cma
)
92 struct ion_cma_heap
*cma_heap
;
94 cma_heap
= kzalloc(sizeof(*cma_heap
), GFP_KERNEL
);
97 return ERR_PTR(-ENOMEM
);
99 cma_heap
->heap
.ops
= &ion_cma_ops
;
101 * get device from private heaps data, later it will be
102 * used to make the link with reserved CMA memory
105 cma_heap
->heap
.type
= ION_HEAP_TYPE_DMA
;
106 return &cma_heap
->heap
;
109 static int __ion_add_cma_heaps(struct cma
*cma
, void *data
)
111 struct ion_heap
*heap
;
113 heap
= __ion_cma_heap_create(cma
);
115 return PTR_ERR(heap
);
117 heap
->name
= cma_get_name(cma
);
119 ion_device_add_heap(heap
);
123 static int ion_add_cma_heaps(void)
125 cma_for_each_area(__ion_add_cma_heaps
, NULL
);
128 device_initcall(ion_add_cma_heaps
);