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/dma-mapping.h>
27 #define ION_CMA_ALLOCATE_FAILED -1
34 #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
36 struct ion_cma_buffer_info {
39 struct sg_table *table;
43 /* ION CMA heap operations functions */
44 static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
45 unsigned long len, unsigned long align,
48 struct ion_cma_heap *cma_heap = to_cma_heap(heap);
49 struct device *dev = cma_heap->dev;
50 struct ion_cma_buffer_info *info;
52 dev_dbg(dev, "Request buffer allocation len %ld\n", len);
54 if (buffer->flags & ION_FLAG_CACHED)
57 if (align > PAGE_SIZE)
60 info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL);
62 return ION_CMA_ALLOCATE_FAILED;
64 info->cpu_addr = dma_alloc_coherent(dev, len, &(info->handle),
65 GFP_HIGHUSER | __GFP_ZERO);
67 if (!info->cpu_addr) {
68 dev_err(dev, "Fail to allocate buffer\n");
72 info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
76 if (dma_get_sgtable(dev, info->table, info->cpu_addr, info->handle, len))
78 /* keep this for memory release */
79 buffer->priv_virt = info;
80 dev_dbg(dev, "Allocate buffer %p\n", buffer);
86 dma_free_coherent(dev, len, info->cpu_addr, info->handle);
89 return ION_CMA_ALLOCATE_FAILED;
92 static void ion_cma_free(struct ion_buffer *buffer)
94 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
95 struct device *dev = cma_heap->dev;
96 struct ion_cma_buffer_info *info = buffer->priv_virt;
98 dev_dbg(dev, "Release buffer %p\n", buffer);
100 dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle);
101 /* release sg table */
102 sg_free_table(info->table);
107 /* return physical address in addr */
108 static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
109 ion_phys_addr_t *addr, size_t *len)
111 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
112 struct device *dev = cma_heap->dev;
113 struct ion_cma_buffer_info *info = buffer->priv_virt;
115 dev_dbg(dev, "Return buffer %p physical address %pa\n", buffer,
118 *addr = info->handle;
124 static struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap,
125 struct ion_buffer *buffer)
127 struct ion_cma_buffer_info *info = buffer->priv_virt;
132 static void ion_cma_heap_unmap_dma(struct ion_heap *heap,
133 struct ion_buffer *buffer)
137 static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer,
138 struct vm_area_struct *vma)
140 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
141 struct device *dev = cma_heap->dev;
142 struct ion_cma_buffer_info *info = buffer->priv_virt;
144 return dma_mmap_coherent(dev, vma, info->cpu_addr, info->handle,
148 static void *ion_cma_map_kernel(struct ion_heap *heap,
149 struct ion_buffer *buffer)
151 struct ion_cma_buffer_info *info = buffer->priv_virt;
152 /* kernel memory mapping has been done at allocation time */
153 return info->cpu_addr;
156 static void ion_cma_unmap_kernel(struct ion_heap *heap,
157 struct ion_buffer *buffer)
161 static struct ion_heap_ops ion_cma_ops = {
162 .allocate = ion_cma_allocate,
163 .free = ion_cma_free,
164 .map_dma = ion_cma_heap_map_dma,
165 .unmap_dma = ion_cma_heap_unmap_dma,
166 .phys = ion_cma_phys,
167 .map_user = ion_cma_mmap,
168 .map_kernel = ion_cma_map_kernel,
169 .unmap_kernel = ion_cma_unmap_kernel,
172 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data)
174 struct ion_cma_heap *cma_heap;
176 cma_heap = kzalloc(sizeof(struct ion_cma_heap), GFP_KERNEL);
179 return ERR_PTR(-ENOMEM);
181 cma_heap->heap.ops = &ion_cma_ops;
182 /* get device from private heaps data, later it will be
183 * used to make the link with reserved CMA memory */
184 cma_heap->dev = data->priv;
185 cma_heap->heap.type = ION_HEAP_TYPE_DMA;
186 return &cma_heap->heap;
189 void ion_cma_heap_destroy(struct ion_heap *heap)
191 struct ion_cma_heap *cma_heap = to_cma_heap(heap);