blob: da5d22f97b417b5aa27a89a987959df30dbb71ff [file] [log] [blame]
Benjamin Gaignard07b590e2012-08-15 10:55:10 -07001/*
2 * drivers/gpu/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/ion.h>
20#include <linux/slab.h>
21#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/dma-mapping.h>
Laura Abbott4979d972012-08-15 11:09:10 -070024#include <linux/msm_ion.h>
25#include <mach/iommu_domains.h>
26
27#include <asm/cacheflush.h>
Benjamin Gaignard07b590e2012-08-15 10:55:10 -070028
29/* for ion_heap_ops structure */
30#include "ion_priv.h"
31
32#define ION_CMA_ALLOCATE_FAILED -1
33
34struct ion_cma_buffer_info {
35 void *cpu_addr;
36 dma_addr_t handle;
37 struct sg_table *table;
Laura Abbott4979d972012-08-15 11:09:10 -070038 bool is_cached;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -070039};
40
Laura Abbott4979d972012-08-15 11:09:10 -070041static int cma_heap_has_outer_cache;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -070042/*
43 * Create scatter-list for the already allocated DMA buffer.
44 * This function could be replace by dma_common_get_sgtable
45 * as soon as it will avalaible.
46 */
47int ion_cma_get_sgtable(struct device *dev, struct sg_table *sgt,
48 void *cpu_addr, dma_addr_t handle, size_t size)
49{
50 struct page *page = virt_to_page(cpu_addr);
51 int ret;
52
53 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
54 if (unlikely(ret))
55 return ret;
56
57 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
58 return 0;
59}
60
61/* ION CMA heap operations functions */
62static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
63 unsigned long len, unsigned long align,
64 unsigned long flags)
65{
66 struct device *dev = heap->priv;
67 struct ion_cma_buffer_info *info;
68
69 dev_dbg(dev, "Request buffer allocation len %ld\n", len);
70
71 info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL);
72 if (!info) {
73 dev_err(dev, "Can't allocate buffer info\n");
74 return ION_CMA_ALLOCATE_FAILED;
75 }
76
Laura Abbott4979d972012-08-15 11:09:10 -070077 if (!ION_IS_CACHED(flags))
78 info->cpu_addr = dma_alloc_writecombine(dev, len,
79 &(info->handle), 0);
80 else
81 info->cpu_addr = dma_alloc_nonconsistent(dev, len,
82 &(info->handle), 0);
Benjamin Gaignard07b590e2012-08-15 10:55:10 -070083
84 if (!info->cpu_addr) {
85 dev_err(dev, "Fail to allocate buffer\n");
86 goto err;
87 }
88
89 info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
90 if (!info->table) {
91 dev_err(dev, "Fail to allocate sg table\n");
92 goto err;
93 }
94
Laura Abbott4979d972012-08-15 11:09:10 -070095 info->is_cached = ION_IS_CACHED(flags);
96
Benjamin Gaignard07b590e2012-08-15 10:55:10 -070097 ion_cma_get_sgtable(dev,
98 info->table, info->cpu_addr, info->handle, len);
99
100 /* keep this for memory release */
101 buffer->priv_virt = info;
102 dev_dbg(dev, "Allocate buffer %p\n", buffer);
103 return 0;
104
105err:
106 kfree(info);
107 return ION_CMA_ALLOCATE_FAILED;
108}
109
110static void ion_cma_free(struct ion_buffer *buffer)
111{
112 struct device *dev = buffer->heap->priv;
113 struct ion_cma_buffer_info *info = buffer->priv_virt;
114
115 dev_dbg(dev, "Release buffer %p\n", buffer);
116 /* release memory */
117 dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle);
118 /* release sg table */
119 kfree(info->table);
120 kfree(info);
121}
122
123/* return physical address in addr */
124static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
125 ion_phys_addr_t *addr, size_t *len)
126{
127 struct device *dev = heap->priv;
128 struct ion_cma_buffer_info *info = buffer->priv_virt;
129
130 dev_dbg(dev, "Return buffer %p physical address 0x%x\n", buffer,
Laura Abbott4979d972012-08-15 11:09:10 -0700131 info->handle);
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700132
Laura Abbott4979d972012-08-15 11:09:10 -0700133 *addr = info->handle;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700134 *len = buffer->size;
135
136 return 0;
137}
138
139struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap,
140 struct ion_buffer *buffer)
141{
142 struct ion_cma_buffer_info *info = buffer->priv_virt;
143
144 return info->table;
145}
146
147void ion_cma_heap_unmap_dma(struct ion_heap *heap,
148 struct ion_buffer *buffer)
149{
150 return;
151}
152
153static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer,
154 struct vm_area_struct *vma)
155{
156 struct device *dev = buffer->heap->priv;
157 struct ion_cma_buffer_info *info = buffer->priv_virt;
158
Laura Abbott4979d972012-08-15 11:09:10 -0700159 if (info->is_cached)
160 return dma_mmap_nonconsistent(dev, vma, info->cpu_addr,
161 info->handle, buffer->size);
162 else
163 return dma_mmap_writecombine(dev, vma, info->cpu_addr,
164 info->handle, buffer->size);
165}
166
167static void *ion_cma_map_kernel(struct ion_heap *heap,
168 struct ion_buffer *buffer)
169{
170 struct ion_cma_buffer_info *info = buffer->priv_virt;
171
172 return info->cpu_addr;
173}
174
175static void ion_cma_unmap_kernel(struct ion_heap *heap,
176 struct ion_buffer *buffer)
177{
178 return;
179}
180
181int ion_cma_map_iommu(struct ion_buffer *buffer,
182 struct ion_iommu_map *data,
183 unsigned int domain_num,
184 unsigned int partition_num,
185 unsigned long align,
186 unsigned long iova_length,
187 unsigned long flags)
188{
189 int ret = 0;
190 struct iommu_domain *domain;
191 unsigned long extra;
192 unsigned long extra_iova_addr;
193 struct ion_cma_buffer_info *info = buffer->priv_virt;
194 struct sg_table *table = info->table;
195 int prot = IOMMU_WRITE | IOMMU_READ;
196
197 data->mapped_size = iova_length;
198
199 if (!msm_use_iommu()) {
200 data->iova_addr = info->handle;
201 return 0;
202 }
203
204 extra = iova_length - buffer->size;
205
206 ret = msm_allocate_iova_address(domain_num, partition_num,
207 data->mapped_size, align,
208 &data->iova_addr);
209
210 if (ret)
211 goto out;
212
213 domain = msm_get_iommu_domain(domain_num);
214
215 if (!domain) {
216 ret = -EINVAL;
217 goto out1;
218 }
219
220 ret = iommu_map_range(domain, data->iova_addr, table->sgl,
221 buffer->size, prot);
222
223 if (ret) {
224 pr_err("%s: could not map %lx in domain %p\n",
225 __func__, data->iova_addr, domain);
226 goto out1;
227 }
228
229 extra_iova_addr = data->iova_addr + buffer->size;
230 if (extra) {
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -0800231 unsigned long phys_addr = sg_phys(table->sgl);
232 ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr,
233 extra, SZ_4K, prot);
Laura Abbott4979d972012-08-15 11:09:10 -0700234 if (ret)
235 goto out2;
236 }
237 return ret;
238
239out2:
240 iommu_unmap_range(domain, data->iova_addr, buffer->size);
241out1:
242 msm_free_iova_address(data->iova_addr, domain_num, partition_num,
243 data->mapped_size);
244out:
245 return ret;
246}
247
248
249void ion_cma_unmap_iommu(struct ion_iommu_map *data)
250{
251 unsigned int domain_num;
252 unsigned int partition_num;
253 struct iommu_domain *domain;
254
255 if (!msm_use_iommu())
256 return;
257
258 domain_num = iommu_map_domain(data);
259 partition_num = iommu_map_partition(data);
260
261 domain = msm_get_iommu_domain(domain_num);
262
263 if (!domain) {
264 WARN(1, "Could not get domain %d. Corruption?\n", domain_num);
265 return;
266 }
267
268 iommu_unmap_range(domain, data->iova_addr, data->mapped_size);
269 msm_free_iova_address(data->iova_addr, domain_num, partition_num,
270 data->mapped_size);
271
272 return;
273}
274
275int ion_cma_cache_ops(struct ion_heap *heap,
276 struct ion_buffer *buffer, void *vaddr,
277 unsigned int offset, unsigned int length,
278 unsigned int cmd)
279{
280 void (*outer_cache_op)(phys_addr_t, phys_addr_t);
281
282 switch (cmd) {
283 case ION_IOC_CLEAN_CACHES:
284 dmac_clean_range(vaddr, vaddr + length);
285 outer_cache_op = outer_clean_range;
286 break;
287 case ION_IOC_INV_CACHES:
288 dmac_inv_range(vaddr, vaddr + length);
289 outer_cache_op = outer_inv_range;
290 break;
291 case ION_IOC_CLEAN_INV_CACHES:
292 dmac_flush_range(vaddr, vaddr + length);
293 outer_cache_op = outer_flush_range;
294 break;
295 default:
296 return -EINVAL;
297 }
298
299 if (cma_heap_has_outer_cache) {
300 struct ion_cma_buffer_info *info = buffer->priv_virt;
301
302 outer_cache_op(info->handle, info->handle + length);
303 }
304
305 return 0;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700306}
307
308static struct ion_heap_ops ion_cma_ops = {
309 .allocate = ion_cma_allocate,
310 .free = ion_cma_free,
311 .map_dma = ion_cma_heap_map_dma,
312 .unmap_dma = ion_cma_heap_unmap_dma,
313 .phys = ion_cma_phys,
314 .map_user = ion_cma_mmap,
Laura Abbott4979d972012-08-15 11:09:10 -0700315 .map_kernel = ion_cma_map_kernel,
316 .unmap_kernel = ion_cma_unmap_kernel,
317 .map_iommu = ion_cma_map_iommu,
318 .unmap_iommu = ion_cma_unmap_iommu,
319 .cache_op = ion_cma_cache_ops,
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700320};
321
322struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data)
323{
324 struct ion_heap *heap;
325
326 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
327
328 if (!heap)
329 return ERR_PTR(-ENOMEM);
330
331 heap->ops = &ion_cma_ops;
332 /* set device as private heaps data, later it will be
333 * used to make the link with reserved CMA memory */
334 heap->priv = data->priv;
335 heap->type = ION_HEAP_TYPE_DMA;
Laura Abbott4979d972012-08-15 11:09:10 -0700336 cma_heap_has_outer_cache = data->has_outer_cache;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700337 return heap;
338}
339
340void ion_cma_heap_destroy(struct ion_heap *heap)
341{
342 kfree(heap);
343}