drm/exynos: fix flags in dma buf exporting
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / exynos / exynos_drm_dmabuf.c
1 /* exynos_drm_dmabuf.c
2 *
3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <drm/drmP.h>
27 #include <drm/exynos_drm.h>
28 #include "exynos_drm_drv.h"
29 #include "exynos_drm_gem.h"
30
31 #include <linux/dma-buf.h>
32
33 struct exynos_drm_dmabuf_attachment {
34 struct sg_table sgt;
35 enum dma_data_direction dir;
36 };
37
38 static int exynos_gem_attach_dma_buf(struct dma_buf *dmabuf,
39 struct device *dev,
40 struct dma_buf_attachment *attach)
41 {
42 struct exynos_drm_dmabuf_attachment *exynos_attach;
43
44 exynos_attach = kzalloc(sizeof(*exynos_attach), GFP_KERNEL);
45 if (!exynos_attach)
46 return -ENOMEM;
47
48 exynos_attach->dir = DMA_NONE;
49 attach->priv = exynos_attach;
50
51 return 0;
52 }
53
54 static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
55 struct dma_buf_attachment *attach)
56 {
57 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
58 struct sg_table *sgt;
59
60 if (!exynos_attach)
61 return;
62
63 sgt = &exynos_attach->sgt;
64
65 if (exynos_attach->dir != DMA_NONE)
66 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
67 exynos_attach->dir);
68
69 sg_free_table(sgt);
70 kfree(exynos_attach);
71 attach->priv = NULL;
72 }
73
74 static struct sg_table *
75 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
76 enum dma_data_direction dir)
77 {
78 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
79 struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
80 struct drm_device *dev = gem_obj->base.dev;
81 struct exynos_drm_gem_buf *buf;
82 struct scatterlist *rd, *wr;
83 struct sg_table *sgt = NULL;
84 unsigned int i;
85 int nents, ret;
86
87 DRM_DEBUG_PRIME("%s\n", __FILE__);
88
89 if (WARN_ON(dir == DMA_NONE))
90 return ERR_PTR(-EINVAL);
91
92 /* just return current sgt if already requested. */
93 if (exynos_attach->dir == dir)
94 return &exynos_attach->sgt;
95
96 /* reattaching is not allowed. */
97 if (WARN_ON(exynos_attach->dir != DMA_NONE))
98 return ERR_PTR(-EBUSY);
99
100 buf = gem_obj->buffer;
101 if (!buf) {
102 DRM_ERROR("buffer is null.\n");
103 return ERR_PTR(-ENOMEM);
104 }
105
106 sgt = &exynos_attach->sgt;
107
108 ret = sg_alloc_table(sgt, buf->sgt->orig_nents, GFP_KERNEL);
109 if (ret) {
110 DRM_ERROR("failed to alloc sgt.\n");
111 return ERR_PTR(-ENOMEM);
112 }
113
114 mutex_lock(&dev->struct_mutex);
115
116 rd = buf->sgt->sgl;
117 wr = sgt->sgl;
118 for (i = 0; i < sgt->orig_nents; ++i) {
119 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
120 rd = sg_next(rd);
121 wr = sg_next(wr);
122 }
123
124 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
125 if (!nents) {
126 DRM_ERROR("failed to map sgl with iommu.\n");
127 sgt = ERR_PTR(-EIO);
128 goto err_unlock;
129 }
130
131 exynos_attach->dir = dir;
132 attach->priv = exynos_attach;
133
134 DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
135
136 err_unlock:
137 mutex_unlock(&dev->struct_mutex);
138 return sgt;
139 }
140
141 static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
142 struct sg_table *sgt,
143 enum dma_data_direction dir)
144 {
145 /* Nothing to do. */
146 }
147
148 static void exynos_dmabuf_release(struct dma_buf *dmabuf)
149 {
150 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
151
152 DRM_DEBUG_PRIME("%s\n", __FILE__);
153
154 /*
155 * exynos_dmabuf_release() call means that file object's
156 * f_count is 0 and it calls drm_gem_object_handle_unreference()
157 * to drop the references that these values had been increased
158 * at drm_prime_handle_to_fd()
159 */
160 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
161 exynos_gem_obj->base.export_dma_buf = NULL;
162
163 /*
164 * drop this gem object refcount to release allocated buffer
165 * and resources.
166 */
167 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
168 }
169 }
170
171 static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
172 unsigned long page_num)
173 {
174 /* TODO */
175
176 return NULL;
177 }
178
179 static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
180 unsigned long page_num,
181 void *addr)
182 {
183 /* TODO */
184 }
185
186 static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
187 unsigned long page_num)
188 {
189 /* TODO */
190
191 return NULL;
192 }
193
194 static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
195 unsigned long page_num, void *addr)
196 {
197 /* TODO */
198 }
199
200 static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
201 struct vm_area_struct *vma)
202 {
203 return -ENOTTY;
204 }
205
206 static struct dma_buf_ops exynos_dmabuf_ops = {
207 .attach = exynos_gem_attach_dma_buf,
208 .detach = exynos_gem_detach_dma_buf,
209 .map_dma_buf = exynos_gem_map_dma_buf,
210 .unmap_dma_buf = exynos_gem_unmap_dma_buf,
211 .kmap = exynos_gem_dmabuf_kmap,
212 .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
213 .kunmap = exynos_gem_dmabuf_kunmap,
214 .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
215 .mmap = exynos_gem_dmabuf_mmap,
216 .release = exynos_dmabuf_release,
217 };
218
219 struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
220 struct drm_gem_object *obj, int flags)
221 {
222 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
223
224 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
225 exynos_gem_obj->base.size, flags);
226 }
227
228 struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
229 struct dma_buf *dma_buf)
230 {
231 struct dma_buf_attachment *attach;
232 struct sg_table *sgt;
233 struct scatterlist *sgl;
234 struct exynos_drm_gem_obj *exynos_gem_obj;
235 struct exynos_drm_gem_buf *buffer;
236 int ret;
237
238 DRM_DEBUG_PRIME("%s\n", __FILE__);
239
240 /* is this one of own objects? */
241 if (dma_buf->ops == &exynos_dmabuf_ops) {
242 struct drm_gem_object *obj;
243
244 exynos_gem_obj = dma_buf->priv;
245 obj = &exynos_gem_obj->base;
246
247 /* is it from our device? */
248 if (obj->dev == drm_dev) {
249 drm_gem_object_reference(obj);
250 return obj;
251 }
252 }
253
254 attach = dma_buf_attach(dma_buf, drm_dev->dev);
255 if (IS_ERR(attach))
256 return ERR_PTR(-EINVAL);
257
258
259 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
260 if (IS_ERR_OR_NULL(sgt)) {
261 ret = PTR_ERR(sgt);
262 goto err_buf_detach;
263 }
264
265 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
266 if (!buffer) {
267 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
268 ret = -ENOMEM;
269 goto err_unmap_attach;
270 }
271
272 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
273 if (!exynos_gem_obj) {
274 ret = -ENOMEM;
275 goto err_free_buffer;
276 }
277
278 sgl = sgt->sgl;
279
280 buffer->size = dma_buf->size;
281 buffer->dma_addr = sg_dma_address(sgl);
282
283 if (sgt->nents == 1) {
284 /* always physically continuous memory if sgt->nents is 1. */
285 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
286 } else {
287 /*
288 * this case could be CONTIG or NONCONTIG type but for now
289 * sets NONCONTIG.
290 * TODO. we have to find a way that exporter can notify
291 * the type of its own buffer to importer.
292 */
293 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
294 }
295
296 exynos_gem_obj->buffer = buffer;
297 buffer->sgt = sgt;
298 exynos_gem_obj->base.import_attach = attach;
299
300 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
301 buffer->size);
302
303 return &exynos_gem_obj->base;
304
305 err_free_buffer:
306 kfree(buffer);
307 buffer = NULL;
308 err_unmap_attach:
309 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
310 err_buf_detach:
311 dma_buf_detach(dma_buf, attach);
312 return ERR_PTR(ret);
313 }
314
315 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
316 MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
317 MODULE_LICENSE("GPL");