blob: d4bbab724ad865bf6ba01be9acc29153880d7822 [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{
Laura Abbottf2f42852013-04-15 11:15:54 -070050 struct page *page = phys_to_page(handle);
Benjamin Gaignard07b590e2012-08-15 10:55:10 -070051 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,
Laura Abbott83062cf2013-06-19 10:07:21 -070079 &(info->handle), GFP_KERNEL);
Laura Abbott4979d972012-08-15 11:09:10 -070080 else
81 info->cpu_addr = dma_alloc_nonconsistent(dev, len,
Laura Abbott83062cf2013-06-19 10:07:21 -070082 &(info->handle), GFP_KERNEL);
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);
Laura Abbottab439ca2013-05-16 13:00:08 -0700118 sg_free_table(info->table);
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700119 /* release sg table */
120 kfree(info->table);
121 kfree(info);
122}
123
124/* return physical address in addr */
125static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
126 ion_phys_addr_t *addr, size_t *len)
127{
128 struct device *dev = heap->priv;
129 struct ion_cma_buffer_info *info = buffer->priv_virt;
130
Laura Abbottbd4af162013-03-18 11:14:47 -0700131 dev_dbg(dev, "Return buffer %p physical address 0x%pa\n", buffer,
132 &info->handle);
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700133
Laura Abbott4979d972012-08-15 11:09:10 -0700134 *addr = info->handle;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700135 *len = buffer->size;
136
137 return 0;
138}
139
140struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap,
141 struct ion_buffer *buffer)
142{
143 struct ion_cma_buffer_info *info = buffer->priv_virt;
144
145 return info->table;
146}
147
148void ion_cma_heap_unmap_dma(struct ion_heap *heap,
149 struct ion_buffer *buffer)
150{
151 return;
152}
153
154static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer,
155 struct vm_area_struct *vma)
156{
157 struct device *dev = buffer->heap->priv;
158 struct ion_cma_buffer_info *info = buffer->priv_virt;
159
Laura Abbott4979d972012-08-15 11:09:10 -0700160 if (info->is_cached)
161 return dma_mmap_nonconsistent(dev, vma, info->cpu_addr,
162 info->handle, buffer->size);
163 else
164 return dma_mmap_writecombine(dev, vma, info->cpu_addr,
165 info->handle, buffer->size);
166}
167
168static void *ion_cma_map_kernel(struct ion_heap *heap,
169 struct ion_buffer *buffer)
170{
171 struct ion_cma_buffer_info *info = buffer->priv_virt;
172
173 return info->cpu_addr;
174}
175
176static void ion_cma_unmap_kernel(struct ion_heap *heap,
177 struct ion_buffer *buffer)
178{
179 return;
180}
181
Chintan Pandyadaf75622013-01-29 19:40:01 +0530182static int ion_cma_print_debug(struct ion_heap *heap, struct seq_file *s,
Mitchel Humpherysee0aa9c2013-11-15 22:56:04 -0800183 const struct list_head *mem_map)
Chintan Pandyadaf75622013-01-29 19:40:01 +0530184{
185 if (mem_map) {
Mitchel Humpherysee0aa9c2013-11-15 22:56:04 -0800186 struct mem_map_data *data;
Chintan Pandyadaf75622013-01-29 19:40:01 +0530187
188 seq_printf(s, "\nMemory Map\n");
189 seq_printf(s, "%16.s %14.s %14.s %14.s\n",
190 "client", "start address", "end address",
191 "size (hex)");
192
Mitchel Humpherysee0aa9c2013-11-15 22:56:04 -0800193 list_for_each_entry(data, mem_map, node) {
Chintan Pandyadaf75622013-01-29 19:40:01 +0530194 const char *client_name = "(null)";
195
196
197 if (data->client_name)
198 client_name = data->client_name;
199
Laura Abbott1135c9e2013-03-13 15:33:40 -0700200 seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n",
201 client_name, &data->addr,
202 &data->addr_end,
Chintan Pandyadaf75622013-01-29 19:40:01 +0530203 data->size, data->size);
204 }
205 }
206 return 0;
207}
208
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700209static struct ion_heap_ops ion_cma_ops = {
210 .allocate = ion_cma_allocate,
211 .free = ion_cma_free,
212 .map_dma = ion_cma_heap_map_dma,
213 .unmap_dma = ion_cma_heap_unmap_dma,
214 .phys = ion_cma_phys,
215 .map_user = ion_cma_mmap,
Laura Abbott4979d972012-08-15 11:09:10 -0700216 .map_kernel = ion_cma_map_kernel,
217 .unmap_kernel = ion_cma_unmap_kernel,
Chintan Pandyadaf75622013-01-29 19:40:01 +0530218 .print_debug = ion_cma_print_debug,
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700219};
220
221struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data)
222{
223 struct ion_heap *heap;
224
225 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
226
227 if (!heap)
228 return ERR_PTR(-ENOMEM);
229
230 heap->ops = &ion_cma_ops;
231 /* set device as private heaps data, later it will be
232 * used to make the link with reserved CMA memory */
233 heap->priv = data->priv;
234 heap->type = ION_HEAP_TYPE_DMA;
Laura Abbott4979d972012-08-15 11:09:10 -0700235 cma_heap_has_outer_cache = data->has_outer_cache;
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700236 return heap;
237}
238
239void ion_cma_heap_destroy(struct ion_heap *heap)
240{
241 kfree(heap);
242}