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