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 * Create scatter-list for the already allocated DMA buffer.
44 * This function could be replaced by dma_common_get_sgtable
45 * as soon as it will avalaible.
47 int ion_cma_get_sgtable(struct device *dev, struct sg_table *sgt,
48 void *cpu_addr, dma_addr_t handle, size_t size)
50 struct page *page = virt_to_page(cpu_addr);
53 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
57 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
62 * Create scatter-list for each page of the already allocated DMA buffer.
64 int ion_cma_get_sgtable_per_page(struct device *dev, struct sg_table *sgt,
65 void *cpu_addr, dma_addr_t handle, size_t size)
67 struct page *page = virt_to_page(cpu_addr);
69 struct scatterlist *sg;
71 ret = sg_alloc_table(sgt, PAGE_ALIGN(size) / PAGE_SIZE, GFP_KERNEL);
76 for (i = 0; i < (PAGE_ALIGN(size) / PAGE_SIZE); i++) {
77 page = virt_to_page(cpu_addr + (i * PAGE_SIZE));
78 sg_set_page(sg, page, PAGE_SIZE, 0);
84 /* ION CMA heap operations functions */
85 static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
86 unsigned long len, unsigned long align,
89 struct ion_cma_heap *cma_heap = to_cma_heap(heap);
90 struct device *dev = cma_heap->dev;
91 struct ion_cma_buffer_info *info;
93 dev_dbg(dev, "Request buffer allocation len %ld\n", len);
95 info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL);
97 dev_err(dev, "Can't allocate buffer info\n");
98 return ION_CMA_ALLOCATE_FAILED;
101 info->cpu_addr = dma_alloc_coherent(dev, len, &(info->handle), 0);
103 if (!info->cpu_addr) {
104 dev_err(dev, "Fail to allocate buffer\n");
108 info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
110 dev_err(dev, "Fail to allocate sg table\n");
114 if (ion_buffer_fault_user_mappings(buffer)) {
115 if (ion_cma_get_sgtable_per_page
116 (dev, info->table, info->cpu_addr, info->handle, len))
119 if (ion_cma_get_sgtable
120 (dev, info->table, info->cpu_addr, info->handle, len))
123 /* keep this for memory release */
124 buffer->priv_virt = info;
125 dev_dbg(dev, "Allocate buffer %p\n", buffer);
131 dma_free_coherent(dev, len, info->cpu_addr, info->handle);
134 return ION_CMA_ALLOCATE_FAILED;
137 static void ion_cma_free(struct ion_buffer *buffer)
139 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
140 struct device *dev = cma_heap->dev;
141 struct ion_cma_buffer_info *info = buffer->priv_virt;
143 dev_dbg(dev, "Release buffer %p\n", buffer);
145 dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle);
146 /* release sg table */
147 sg_free_table(info->table);
152 /* return physical address in addr */
153 static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
154 ion_phys_addr_t *addr, size_t *len)
156 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
157 struct device *dev = cma_heap->dev;
158 struct ion_cma_buffer_info *info = buffer->priv_virt;
160 dev_dbg(dev, "Return buffer %p physical address 0x%x\n", buffer,
163 *addr = info->handle;
169 struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap,
170 struct ion_buffer *buffer)
172 struct ion_cma_buffer_info *info = buffer->priv_virt;
177 void ion_cma_heap_unmap_dma(struct ion_heap *heap,
178 struct ion_buffer *buffer)
183 static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer,
184 struct vm_area_struct *vma)
186 struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
187 struct device *dev = cma_heap->dev;
188 struct ion_cma_buffer_info *info = buffer->priv_virt;
190 return dma_mmap_coherent(dev, vma, info->cpu_addr, info->handle,
194 void *ion_cma_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer)
196 struct ion_cma_buffer_info *info = buffer->priv_virt;
197 /* kernel memory mapping has been done at allocation time */
198 return info->cpu_addr;
201 static struct ion_heap_ops ion_cma_ops = {
202 .allocate = ion_cma_allocate,
203 .free = ion_cma_free,
204 .map_dma = ion_cma_heap_map_dma,
205 .unmap_dma = ion_cma_heap_unmap_dma,
206 .phys = ion_cma_phys,
207 .map_user = ion_cma_mmap,
208 .map_kernel = ion_cma_map_kernel,
211 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data)
213 struct ion_cma_heap *cma_heap;
215 cma_heap = kzalloc(sizeof(struct ion_cma_heap), GFP_KERNEL);
218 return ERR_PTR(-ENOMEM);
220 cma_heap->heap.ops = &ion_cma_ops;
221 /* get device from private heaps data, later it will be
222 * used to make the link with reserved CMA memory */
223 cma_heap->dev = data->priv;
224 cma_heap->heap.type = ION_HEAP_TYPE_DMA;
225 return &cma_heap->heap;
228 void ion_cma_heap_destroy(struct ion_heap *heap)
230 struct ion_cma_heap *cma_heap = to_cma_heap(heap);