blob: b98da307faec96a1cd1607c329545bca848d1623 [file] [log] [blame]
Inki Daeb2df26c2012-04-23 21:01:28 +09001/* 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
David Howells760285e2012-10-02 18:01:07 +010026#include <drm/drmP.h>
27#include <drm/exynos_drm.h>
Inki Daeb2df26c2012-04-23 21:01:28 +090028#include "exynos_drm_drv.h"
29#include "exynos_drm_gem.h"
30
31#include <linux/dma-buf.h>
32
Inki Dae0519f9a2012-10-20 07:53:42 -070033static struct sg_table *exynos_get_sgt(struct drm_device *drm_dev,
34 struct exynos_drm_gem_buf *buf)
Inki Daeb2df26c2012-04-23 21:01:28 +090035{
36 struct sg_table *sgt = NULL;
Inki Dae0519f9a2012-10-20 07:53:42 -070037 int ret;
Inki Daeb2df26c2012-04-23 21:01:28 +090038
39 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
40 if (!sgt)
41 goto out;
42
Inki Dae0519f9a2012-10-20 07:53:42 -070043 ret = sg_alloc_table(sgt, buf->sgt->nents, GFP_KERNEL);
Inki Daeb2df26c2012-04-23 21:01:28 +090044 if (ret)
45 goto err_free_sgt;
46
Inki Dae0519f9a2012-10-20 07:53:42 -070047 ret = dma_get_sgtable(drm_dev->dev, sgt, buf->kvaddr,
48 buf->dma_addr, buf->size);
49 if (ret < 0) {
50 DRM_ERROR("failed to get sgtable.\n");
51 goto err_free_table;
52 }
Inki Daeb2df26c2012-04-23 21:01:28 +090053
54 return sgt;
55
Inki Dae0519f9a2012-10-20 07:53:42 -070056err_free_table:
57 sg_free_table(sgt);
Inki Daeb2df26c2012-04-23 21:01:28 +090058err_free_sgt:
59 kfree(sgt);
60 sgt = NULL;
61out:
62 return NULL;
63}
64
65static struct sg_table *
66 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
67 enum dma_data_direction dir)
68{
69 struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
70 struct drm_device *dev = gem_obj->base.dev;
71 struct exynos_drm_gem_buf *buf;
72 struct sg_table *sgt = NULL;
Inki Daeb2df26c2012-04-23 21:01:28 +090073 int nents;
74
75 DRM_DEBUG_PRIME("%s\n", __FILE__);
76
Inki Dae0519f9a2012-10-20 07:53:42 -070077 buf = gem_obj->buffer;
78 if (!buf) {
79 DRM_ERROR("buffer is null.\n");
80 return sgt;
81 }
82
Inki Daeb2df26c2012-04-23 21:01:28 +090083 mutex_lock(&dev->struct_mutex);
84
Inki Dae0519f9a2012-10-20 07:53:42 -070085 sgt = exynos_get_sgt(dev, buf);
86 if (!sgt)
Inki Daeb2df26c2012-04-23 21:01:28 +090087 goto err_unlock;
Inki Daeb2df26c2012-04-23 21:01:28 +090088
Inki Daeb2df26c2012-04-23 21:01:28 +090089 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
Inki Dae0519f9a2012-10-20 07:53:42 -070090 if (!nents) {
91 DRM_ERROR("failed to map sgl with iommu.\n");
92 sgt = NULL;
93 goto err_unlock;
94 }
Inki Daeb2df26c2012-04-23 21:01:28 +090095
Inki Dae0519f9a2012-10-20 07:53:42 -070096 DRM_DEBUG_PRIME("buffer size = 0x%lx page_size = 0x%lx\n",
97 buf->size, buf->page_size);
Inki Daeb2df26c2012-04-23 21:01:28 +090098
99err_unlock:
100 mutex_unlock(&dev->struct_mutex);
101 return sgt;
102}
103
104static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
105 struct sg_table *sgt,
106 enum dma_data_direction dir)
107{
108 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
Inki Dae0519f9a2012-10-20 07:53:42 -0700109
Inki Daeb2df26c2012-04-23 21:01:28 +0900110 sg_free_table(sgt);
111 kfree(sgt);
112 sgt = NULL;
113}
114
115static void exynos_dmabuf_release(struct dma_buf *dmabuf)
116{
117 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
118
119 DRM_DEBUG_PRIME("%s\n", __FILE__);
120
121 /*
122 * exynos_dmabuf_release() call means that file object's
123 * f_count is 0 and it calls drm_gem_object_handle_unreference()
124 * to drop the references that these values had been increased
125 * at drm_prime_handle_to_fd()
126 */
127 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
128 exynos_gem_obj->base.export_dma_buf = NULL;
129
130 /*
131 * drop this gem object refcount to release allocated buffer
132 * and resources.
133 */
134 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
135 }
136}
137
138static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
139 unsigned long page_num)
140{
141 /* TODO */
142
143 return NULL;
144}
145
146static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
147 unsigned long page_num,
148 void *addr)
149{
150 /* TODO */
151}
152
153static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
154 unsigned long page_num)
155{
156 /* TODO */
157
158 return NULL;
159}
160
161static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
162 unsigned long page_num, void *addr)
163{
164 /* TODO */
165}
166
Tomasz Stanislawskib716d462012-09-05 19:31:56 +0900167static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
168 struct vm_area_struct *vma)
169{
170 return -ENOTTY;
171}
172
Inki Daeb2df26c2012-04-23 21:01:28 +0900173static struct dma_buf_ops exynos_dmabuf_ops = {
174 .map_dma_buf = exynos_gem_map_dma_buf,
175 .unmap_dma_buf = exynos_gem_unmap_dma_buf,
176 .kmap = exynos_gem_dmabuf_kmap,
177 .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
178 .kunmap = exynos_gem_dmabuf_kunmap,
179 .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
Tomasz Stanislawskib716d462012-09-05 19:31:56 +0900180 .mmap = exynos_gem_dmabuf_mmap,
Inki Daeb2df26c2012-04-23 21:01:28 +0900181 .release = exynos_dmabuf_release,
182};
183
184struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
185 struct drm_gem_object *obj, int flags)
186{
187 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
188
189 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
190 exynos_gem_obj->base.size, 0600);
191}
192
193struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
194 struct dma_buf *dma_buf)
195{
196 struct dma_buf_attachment *attach;
197 struct sg_table *sgt;
198 struct scatterlist *sgl;
199 struct exynos_drm_gem_obj *exynos_gem_obj;
200 struct exynos_drm_gem_buf *buffer;
Inki Dae47fcdce2012-06-07 16:15:07 +0900201 int ret;
Inki Daeb2df26c2012-04-23 21:01:28 +0900202
203 DRM_DEBUG_PRIME("%s\n", __FILE__);
204
205 /* is this one of own objects? */
206 if (dma_buf->ops == &exynos_dmabuf_ops) {
207 struct drm_gem_object *obj;
208
209 exynos_gem_obj = dma_buf->priv;
210 obj = &exynos_gem_obj->base;
211
212 /* is it from our device? */
213 if (obj->dev == drm_dev) {
214 drm_gem_object_reference(obj);
215 return obj;
216 }
217 }
218
219 attach = dma_buf_attach(dma_buf, drm_dev->dev);
220 if (IS_ERR(attach))
221 return ERR_PTR(-EINVAL);
222
223
224 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
Subash Patel0dd3b722012-06-25 11:22:57 -0700225 if (IS_ERR_OR_NULL(sgt)) {
Inki Daeb2df26c2012-04-23 21:01:28 +0900226 ret = PTR_ERR(sgt);
227 goto err_buf_detach;
228 }
229
230 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
231 if (!buffer) {
232 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
233 ret = -ENOMEM;
234 goto err_unmap_attach;
235 }
236
Inki Dae0519f9a2012-10-20 07:53:42 -0700237 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
238 if (!exynos_gem_obj) {
Inki Daeb2df26c2012-04-23 21:01:28 +0900239 ret = -ENOMEM;
240 goto err_free_buffer;
241 }
242
Inki Daeb2df26c2012-04-23 21:01:28 +0900243 sgl = sgt->sgl;
Inki Daeb2df26c2012-04-23 21:01:28 +0900244
Inki Dae0519f9a2012-10-20 07:53:42 -0700245 buffer->size = dma_buf->size;
246 buffer->dma_addr = sg_dma_address(sgl);
Inki Dae47fcdce2012-06-07 16:15:07 +0900247
Inki Dae0519f9a2012-10-20 07:53:42 -0700248 if (sgt->nents == 1) {
Inki Dae47fcdce2012-06-07 16:15:07 +0900249 /* always physically continuous memory if sgt->nents is 1. */
250 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
251 } else {
Inki Dae0519f9a2012-10-20 07:53:42 -0700252 /*
253 * this case could be CONTIG or NONCONTIG type but for now
254 * sets NONCONTIG.
255 * TODO. we have to find a way that exporter can notify
256 * the type of its own buffer to importer.
257 */
Inki Dae47fcdce2012-06-07 16:15:07 +0900258 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
Inki Daeb2df26c2012-04-23 21:01:28 +0900259 }
260
261 exynos_gem_obj->buffer = buffer;
262 buffer->sgt = sgt;
263 exynos_gem_obj->base.import_attach = attach;
264
265 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
266 buffer->size);
267
268 return &exynos_gem_obj->base;
269
Inki Daeb2df26c2012-04-23 21:01:28 +0900270err_free_buffer:
271 kfree(buffer);
272 buffer = NULL;
273err_unmap_attach:
274 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
275err_buf_detach:
276 dma_buf_detach(dma_buf, attach);
277 return ERR_PTR(ret);
278}
279
280MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
281MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
282MODULE_LICENSE("GPL");