staging: ion: cma heap support map/unmap iommu
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / ion / ion_cma_heap.c
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 #ifdef CONFIG_RK_IOMMU
24 #include <linux/rockchip-iovmm.h>
25 #endif
26
27 #include "ion.h"
28 #include "ion_priv.h"
29
30 #define ION_CMA_ALLOCATE_FAILED -1
31
32 struct ion_cma_heap {
33         struct ion_heap heap;
34         struct device *dev;
35 };
36
37 #define to_cma_heap(x) container_of(x, struct ion_cma_heap, heap)
38
39 struct ion_cma_buffer_info {
40         void *cpu_addr;
41         dma_addr_t handle;
42         struct sg_table *table;
43 };
44
45
46 /* ION CMA heap operations functions */
47 static int ion_cma_allocate(struct ion_heap *heap, struct ion_buffer *buffer,
48                             unsigned long len, unsigned long align,
49                             unsigned long flags)
50 {
51         struct ion_cma_heap *cma_heap = to_cma_heap(heap);
52         struct device *dev = cma_heap->dev;
53         struct ion_cma_buffer_info *info;
54
55         dev_dbg(dev, "Request buffer allocation len %ld\n", len);
56
57         if (buffer->flags & ION_FLAG_CACHED)
58                 return -EINVAL;
59
60         if (align > PAGE_SIZE)
61                 return -EINVAL;
62
63         info = kzalloc(sizeof(struct ion_cma_buffer_info), GFP_KERNEL);
64         if (!info)
65                 return ION_CMA_ALLOCATE_FAILED;
66
67         info->cpu_addr = dma_alloc_coherent(dev, len, &(info->handle),
68                                                 GFP_HIGHUSER | __GFP_ZERO);
69
70         if (!info->cpu_addr) {
71                 dev_err(dev, "Fail to allocate buffer\n");
72                 goto err;
73         }
74
75         info->table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
76         if (!info->table)
77                 goto free_mem;
78
79         if (dma_get_sgtable(dev, info->table, info->cpu_addr, info->handle,
80                             len))
81                 goto free_table;
82         /* keep this for memory release */
83         buffer->priv_virt = info;
84         dev_dbg(dev, "Allocate buffer %p\n", buffer);
85         return 0;
86
87 free_table:
88         kfree(info->table);
89 free_mem:
90         dma_free_coherent(dev, len, info->cpu_addr, info->handle);
91 err:
92         kfree(info);
93         return ION_CMA_ALLOCATE_FAILED;
94 }
95
96 static void ion_cma_free(struct ion_buffer *buffer)
97 {
98         struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
99         struct device *dev = cma_heap->dev;
100         struct ion_cma_buffer_info *info = buffer->priv_virt;
101
102         dev_dbg(dev, "Release buffer %p\n", buffer);
103         /* release memory */
104         dma_free_coherent(dev, buffer->size, info->cpu_addr, info->handle);
105         /* release sg table */
106         sg_free_table(info->table);
107         kfree(info->table);
108         kfree(info);
109 }
110
111 /* return physical address in addr */
112 static int ion_cma_phys(struct ion_heap *heap, struct ion_buffer *buffer,
113                         ion_phys_addr_t *addr, size_t *len)
114 {
115         struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
116         struct device *dev = cma_heap->dev;
117         struct ion_cma_buffer_info *info = buffer->priv_virt;
118
119         dev_dbg(dev, "Return buffer %p physical address %pa\n", buffer,
120                 &info->handle);
121
122         *addr = info->handle;
123         *len = buffer->size;
124
125         return 0;
126 }
127
128 static struct sg_table *ion_cma_heap_map_dma(struct ion_heap *heap,
129                                              struct ion_buffer *buffer)
130 {
131         struct ion_cma_buffer_info *info = buffer->priv_virt;
132
133         return info->table;
134 }
135
136 static void ion_cma_heap_unmap_dma(struct ion_heap *heap,
137                                    struct ion_buffer *buffer)
138 {
139 }
140
141 static int ion_cma_mmap(struct ion_heap *mapper, struct ion_buffer *buffer,
142                         struct vm_area_struct *vma)
143 {
144         struct ion_cma_heap *cma_heap = to_cma_heap(buffer->heap);
145         struct device *dev = cma_heap->dev;
146         struct ion_cma_buffer_info *info = buffer->priv_virt;
147
148         return dma_mmap_coherent(dev, vma, info->cpu_addr, info->handle,
149                                  buffer->size);
150 }
151
152 static void *ion_cma_map_kernel(struct ion_heap *heap,
153                                 struct ion_buffer *buffer)
154 {
155         struct ion_cma_buffer_info *info = buffer->priv_virt;
156         /* kernel memory mapping has been done at allocation time */
157         return info->cpu_addr;
158 }
159
160 static void ion_cma_unmap_kernel(struct ion_heap *heap,
161                                         struct ion_buffer *buffer)
162 {
163 }
164
165 #ifdef CONFIG_RK_IOMMU
166 static int ion_cma_map_iommu(struct ion_buffer *buffer,
167                              struct device *iommu_dev,
168                              struct ion_iommu_map *data,
169                              unsigned long iova_length,
170                              unsigned long flags)
171 {
172         int ret = 0;
173         struct ion_cma_buffer_info *info = buffer->priv_virt;
174
175         data->iova_addr = rockchip_iovmm_map(iommu_dev,
176                                           info->table->sgl,
177                                           0,
178                                           iova_length);
179         pr_debug("%s: map %pad -> %lx\n", __func__,
180                 &info->table->sgl->dma_address,
181                 data->iova_addr);
182         if (IS_ERR_VALUE(data->iova_addr)) {
183                 pr_err("%s: failed: %lx\n", __func__, data->iova_addr);
184                 ret = data->iova_addr;
185                 goto out;
186         }
187
188         data->mapped_size = iova_length;
189
190 out:
191         return ret;
192 }
193
194 void ion_cma_unmap_iommu(struct device *iommu_dev, struct ion_iommu_map *data)
195 {
196         pr_debug("%s: unmap %x@%lx\n",
197                  __func__,
198                  data->mapped_size,
199                  data->iova_addr);
200         rockchip_iovmm_unmap(iommu_dev, data->iova_addr);
201 }
202 #endif
203
204 static struct ion_heap_ops ion_cma_ops = {
205         .allocate = ion_cma_allocate,
206         .free = ion_cma_free,
207         .map_dma = ion_cma_heap_map_dma,
208         .unmap_dma = ion_cma_heap_unmap_dma,
209         .phys = ion_cma_phys,
210         .map_user = ion_cma_mmap,
211         .map_kernel = ion_cma_map_kernel,
212         .unmap_kernel = ion_cma_unmap_kernel,
213 #ifdef CONFIG_RK_IOMMU
214         .map_iommu = ion_cma_map_iommu,
215         .unmap_iommu = ion_cma_unmap_iommu,
216 #endif
217 };
218
219 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data)
220 {
221         struct ion_cma_heap *cma_heap;
222
223         cma_heap = kzalloc(sizeof(struct ion_cma_heap), GFP_KERNEL);
224
225         if (!cma_heap)
226                 return ERR_PTR(-ENOMEM);
227
228         cma_heap->heap.ops = &ion_cma_ops;
229         /*
230          * get device from private heaps data, later it will be
231          * used to make the link with reserved CMA memory
232          */
233         cma_heap->dev = data->priv;
234         cma_heap->heap.type = ION_HEAP_TYPE_DMA;
235         return &cma_heap->heap;
236 }
237
238 void ion_cma_heap_destroy(struct ion_heap *heap)
239 {
240         struct ion_cma_heap *cma_heap = to_cma_heap(heap);
241
242         kfree(cma_heap);
243 }