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/dma-mapping.h>
23 
24 #include "ion.h"
25 #include "ion_priv.h"
26 
27 #define ION_CMA_ALLOCATE_FAILED -1
28 
29 struct ion_cma_heap {
30 	struct ion_heap heap;
31 	struct device *dev;
32 };
33 
34 #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
35 
36 struct ion_cma_buffer_info {
37 	void *cpu_addr;
38 	dma_addr_t handle;
39 	struct sg_table *table;
40 };
41 
42 
43 /* ION CMA heap operations functions */
ion_cma_allocate(struct ion_heap * heap,struct ion_buffer * buffer,unsigned long len,unsigned long align,unsigned long flags)44 static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
45 			    unsigned long len, unsigned long align,
46 			    unsigned long flags)
47 {
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;
51 
52 	dev_dbg(dev, "Request buffer allocation len %ld\n", len);
53 
54 	if (buffer->flags & ION_FLAG_CACHED)
55 		return -EINVAL;
56 
57 	if (align > PAGE_SIZE)
58 		return -EINVAL;
59 
60 	info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL);
61 	if (!info)
62 		return ION_CMA_ALLOCATE_FAILED;
63 
64 	info->cpu_addr = dma_alloc_coherent(dev, len, &(info->handle),
65 						GFP_HIGHUSER | __GFP_ZERO);
66 
67 	if (!info->cpu_addr) {
68 		dev_err(dev, "Fail to allocate buffer\n");
69 		goto err;
70 	}
71 
72 	info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
73 	if (!info->table)
74 		goto free_mem;
75 
76 	if (dma_common_get_sgtable
77 	    (dev, info->table, info->cpu_addr, info->handle, len))
78 		goto free_table;
79 	/* keep this for memory release */
80 	buffer->priv_virt = info;
81 	dev_dbg(dev, "Allocate buffer %p\n", buffer);
82 	return 0;
83 
84 free_table:
85 	kfree(info->table);
86 free_mem:
87 	dma_free_coherent(dev, len, info->cpu_addr, info->handle);
88 err:
89 	kfree(info);
90 	return ION_CMA_ALLOCATE_FAILED;
91 }
92 
ion_cma_free(struct ion_buffer * buffer)93 static void ion_cma_free(struct ion_buffer *buffer)
94 {
95 	struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
96 	struct device *dev = cma_heap->dev;
97 	struct ion_cma_buffer_info *info = buffer->priv_virt;
98 
99 	dev_dbg(dev, "Release buffer %p\n", buffer);
100 	/* release memory */
101 	dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle);
102 	/* release sg table */
103 	sg_free_table(info->table);
104 	kfree(info->table);
105 	kfree(info);
106 }
107 
108 /* return physical address in addr */
ion_cma_phys(struct ion_heap * heap,struct ion_buffer * buffer,ion_phys_addr_t * addr,size_t * len)109 static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
110 			ion_phys_addr_t *addr, size_t *len)
111 {
112 	struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
113 	struct device *dev = cma_heap->dev;
114 	struct ion_cma_buffer_info *info = buffer->priv_virt;
115 
116 	dev_dbg(dev, "Return buffer %p physical address %pa\n", buffer,
117 		&info->handle);
118 
119 	*addr = info->handle;
120 	*len = buffer->size;
121 
122 	return 0;
123 }
124 
ion_cma_heap_map_dma(struct ion_heap * heap,struct ion_buffer * buffer)125 static struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap,
126 					     struct ion_buffer *buffer)
127 {
128 	struct ion_cma_buffer_info *info = buffer->priv_virt;
129 
130 	return info->table;
131 }
132 
ion_cma_heap_unmap_dma(struct ion_heap * heap,struct ion_buffer * buffer)133 static void ion_cma_heap_unmap_dma(struct ion_heap *heap,
134 				   struct ion_buffer *buffer)
135 {
136 }
137 
ion_cma_mmap(struct ion_heap * mapper,struct ion_buffer * buffer,struct vm_area_struct * vma)138 static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer,
139 			struct vm_area_struct *vma)
140 {
141 	struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
142 	struct device *dev = cma_heap->dev;
143 	struct ion_cma_buffer_info *info = buffer->priv_virt;
144 
145 	return dma_mmap_coherent(dev, vma, info->cpu_addr, info->handle,
146 				 buffer->size);
147 }
148 
ion_cma_map_kernel(struct ion_heap * heap,struct ion_buffer * buffer)149 static void *ion_cma_map_kernel(struct ion_heap *heap,
150 				struct ion_buffer *buffer)
151 {
152 	struct ion_cma_buffer_info *info = buffer->priv_virt;
153 	/* kernel memory mapping has been done at allocation time */
154 	return info->cpu_addr;
155 }
156 
ion_cma_unmap_kernel(struct ion_heap * heap,struct ion_buffer * buffer)157 static void ion_cma_unmap_kernel(struct ion_heap *heap,
158 					struct ion_buffer *buffer)
159 {
160 }
161 
162 static struct ion_heap_ops ion_cma_ops = {
163 	.allocate = ion_cma_allocate,
164 	.free = ion_cma_free,
165 	.map_dma = ion_cma_heap_map_dma,
166 	.unmap_dma = ion_cma_heap_unmap_dma,
167 	.phys = ion_cma_phys,
168 	.map_user = ion_cma_mmap,
169 	.map_kernel = ion_cma_map_kernel,
170 	.unmap_kernel = ion_cma_unmap_kernel,
171 };
172 
ion_cma_heap_create(struct ion_platform_heap * data)173 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data)
174 {
175 	struct ion_cma_heap *cma_heap;
176 
177 	cma_heap = kzalloc(sizeof(struct ion_cma_heap), GFP_KERNEL);
178 
179 	if (!cma_heap)
180 		return ERR_PTR(-ENOMEM);
181 
182 	cma_heap->heap.ops = &ion_cma_ops;
183 	/* get device from private heaps data, later it will be
184 	 * used to make the link with reserved CMA memory */
185 	cma_heap->dev = data->priv;
186 	cma_heap->heap.type = ION_HEAP_TYPE_DMA;
187 	return &cma_heap->heap;
188 }
189 
ion_cma_heap_destroy(struct ion_heap * heap)190 void ion_cma_heap_destroy(struct ion_heap *heap)
191 {
192 	struct ion_cma_heap *cma_heap = to_cma_heap(heap);
193 
194 	kfree(cma_heap);
195 }
196