blob: 539da9f4eb9762cae7137d2160ad4919eaff26e1 [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 = dma_get_sgtable(drm_dev->dev, sgt, buf->kvaddr,
44 buf->dma_addr, buf->size);
45 if (ret < 0) {
46 DRM_ERROR("failed to get sgtable.\n");
Prathyush K11197072012-11-07 15:58:58 +053047 goto err_free_sgt;
Inki Dae0519f9a2012-10-20 07:53:42 -070048 }
Inki Daeb2df26c2012-04-23 21:01:28 +090049
50 return sgt;
51
52err_free_sgt:
53 kfree(sgt);
54 sgt = NULL;
55out:
56 return NULL;
57}
58
59static struct sg_table *
60 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
61 enum dma_data_direction dir)
62{
63 struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
64 struct drm_device *dev = gem_obj->base.dev;
65 struct exynos_drm_gem_buf *buf;
66 struct sg_table *sgt = NULL;
Inki Daeb2df26c2012-04-23 21:01:28 +090067 int nents;
68
69 DRM_DEBUG_PRIME("%s\n", __FILE__);
70
Inki Dae0519f9a2012-10-20 07:53:42 -070071 buf = gem_obj->buffer;
72 if (!buf) {
73 DRM_ERROR("buffer is null.\n");
74 return sgt;
75 }
76
Inki Daeb2df26c2012-04-23 21:01:28 +090077 mutex_lock(&dev->struct_mutex);
78
Inki Dae0519f9a2012-10-20 07:53:42 -070079 sgt = exynos_get_sgt(dev, buf);
80 if (!sgt)
Inki Daeb2df26c2012-04-23 21:01:28 +090081 goto err_unlock;
Inki Daeb2df26c2012-04-23 21:01:28 +090082
Inki Daeb2df26c2012-04-23 21:01:28 +090083 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
Inki Dae0519f9a2012-10-20 07:53:42 -070084 if (!nents) {
85 DRM_ERROR("failed to map sgl with iommu.\n");
86 sgt = NULL;
87 goto err_unlock;
88 }
Inki Daeb2df26c2012-04-23 21:01:28 +090089
Prathyush K465ed662012-11-20 19:32:56 +090090 DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
Inki Daeb2df26c2012-04-23 21:01:28 +090091
92err_unlock:
93 mutex_unlock(&dev->struct_mutex);
94 return sgt;
95}
96
97static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
98 struct sg_table *sgt,
99 enum dma_data_direction dir)
100{
101 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
Inki Dae0519f9a2012-10-20 07:53:42 -0700102
Inki Daeb2df26c2012-04-23 21:01:28 +0900103 sg_free_table(sgt);
104 kfree(sgt);
105 sgt = NULL;
106}
107
108static void exynos_dmabuf_release(struct dma_buf *dmabuf)
109{
110 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
111
112 DRM_DEBUG_PRIME("%s\n", __FILE__);
113
114 /*
115 * exynos_dmabuf_release() call means that file object's
116 * f_count is 0 and it calls drm_gem_object_handle_unreference()
117 * to drop the references that these values had been increased
118 * at drm_prime_handle_to_fd()
119 */
120 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
121 exynos_gem_obj->base.export_dma_buf = NULL;
122
123 /*
124 * drop this gem object refcount to release allocated buffer
125 * and resources.
126 */
127 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
128 }
129}
130
131static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
132 unsigned long page_num)
133{
134 /* TODO */
135
136 return NULL;
137}
138
139static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
140 unsigned long page_num,
141 void *addr)
142{
143 /* TODO */
144}
145
146static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
147 unsigned long page_num)
148{
149 /* TODO */
150
151 return NULL;
152}
153
154static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
155 unsigned long page_num, void *addr)
156{
157 /* TODO */
158}
159
Tomasz Stanislawskib716d462012-09-05 19:31:56 +0900160static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
161 struct vm_area_struct *vma)
162{
163 return -ENOTTY;
164}
165
Inki Daeb2df26c2012-04-23 21:01:28 +0900166static struct dma_buf_ops exynos_dmabuf_ops = {
167 .map_dma_buf = exynos_gem_map_dma_buf,
168 .unmap_dma_buf = exynos_gem_unmap_dma_buf,
169 .kmap = exynos_gem_dmabuf_kmap,
170 .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
171 .kunmap = exynos_gem_dmabuf_kunmap,
172 .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
Tomasz Stanislawskib716d462012-09-05 19:31:56 +0900173 .mmap = exynos_gem_dmabuf_mmap,
Inki Daeb2df26c2012-04-23 21:01:28 +0900174 .release = exynos_dmabuf_release,
175};
176
177struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
178 struct drm_gem_object *obj, int flags)
179{
180 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
181
182 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
183 exynos_gem_obj->base.size, 0600);
184}
185
186struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
187 struct dma_buf *dma_buf)
188{
189 struct dma_buf_attachment *attach;
190 struct sg_table *sgt;
191 struct scatterlist *sgl;
192 struct exynos_drm_gem_obj *exynos_gem_obj;
193 struct exynos_drm_gem_buf *buffer;
Inki Dae47fcdce2012-06-07 16:15:07 +0900194 int ret;
Inki Daeb2df26c2012-04-23 21:01:28 +0900195
196 DRM_DEBUG_PRIME("%s\n", __FILE__);
197
198 /* is this one of own objects? */
199 if (dma_buf->ops == &exynos_dmabuf_ops) {
200 struct drm_gem_object *obj;
201
202 exynos_gem_obj = dma_buf->priv;
203 obj = &exynos_gem_obj->base;
204
205 /* is it from our device? */
206 if (obj->dev == drm_dev) {
207 drm_gem_object_reference(obj);
208 return obj;
209 }
210 }
211
212 attach = dma_buf_attach(dma_buf, drm_dev->dev);
213 if (IS_ERR(attach))
214 return ERR_PTR(-EINVAL);
215
216
217 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
Subash Patel0dd3b722012-06-25 11:22:57 -0700218 if (IS_ERR_OR_NULL(sgt)) {
Inki Daeb2df26c2012-04-23 21:01:28 +0900219 ret = PTR_ERR(sgt);
220 goto err_buf_detach;
221 }
222
223 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
224 if (!buffer) {
225 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
226 ret = -ENOMEM;
227 goto err_unmap_attach;
228 }
229
Inki Dae0519f9a2012-10-20 07:53:42 -0700230 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
231 if (!exynos_gem_obj) {
Inki Daeb2df26c2012-04-23 21:01:28 +0900232 ret = -ENOMEM;
233 goto err_free_buffer;
234 }
235
Inki Daeb2df26c2012-04-23 21:01:28 +0900236 sgl = sgt->sgl;
Inki Daeb2df26c2012-04-23 21:01:28 +0900237
Inki Dae0519f9a2012-10-20 07:53:42 -0700238 buffer->size = dma_buf->size;
239 buffer->dma_addr = sg_dma_address(sgl);
Inki Dae47fcdce2012-06-07 16:15:07 +0900240
Inki Dae0519f9a2012-10-20 07:53:42 -0700241 if (sgt->nents == 1) {
Inki Dae47fcdce2012-06-07 16:15:07 +0900242 /* always physically continuous memory if sgt->nents is 1. */
243 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
244 } else {
Inki Dae0519f9a2012-10-20 07:53:42 -0700245 /*
246 * this case could be CONTIG or NONCONTIG type but for now
247 * sets NONCONTIG.
248 * TODO. we have to find a way that exporter can notify
249 * the type of its own buffer to importer.
250 */
Inki Dae47fcdce2012-06-07 16:15:07 +0900251 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
Inki Daeb2df26c2012-04-23 21:01:28 +0900252 }
253
254 exynos_gem_obj->buffer = buffer;
255 buffer->sgt = sgt;
256 exynos_gem_obj->base.import_attach = attach;
257
258 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
259 buffer->size);
260
261 return &exynos_gem_obj->base;
262
Inki Daeb2df26c2012-04-23 21:01:28 +0900263err_free_buffer:
264 kfree(buffer);
265 buffer = NULL;
266err_unmap_attach:
267 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
268err_buf_detach:
269 dma_buf_detach(dma_buf, attach);
270 return ERR_PTR(ret);
271}
272
273MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
274MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
275MODULE_LICENSE("GPL");