3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
13 #include <drm/exynos_drm.h>
14 #include "exynos_drm_drv.h"
15 #include "exynos_drm_gem.h"
17 #include <linux/dma-buf.h>
19 struct exynos_drm_dmabuf_attachment {
21 enum dma_data_direction dir;
24 static int exynos_gem_attach_dma_buf(struct dma_buf *dmabuf,
26 struct dma_buf_attachment *attach)
28 struct exynos_drm_dmabuf_attachment *exynos_attach;
30 exynos_attach = kzalloc(sizeof(*exynos_attach), GFP_KERNEL);
34 exynos_attach->dir = DMA_NONE;
35 attach->priv = exynos_attach;
40 static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
41 struct dma_buf_attachment *attach)
43 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
49 sgt = &exynos_attach->sgt;
51 if (exynos_attach->dir != DMA_NONE)
52 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
60 static struct sg_table *
61 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
62 enum dma_data_direction dir)
64 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
65 struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
66 struct drm_device *dev = gem_obj->base.dev;
67 struct exynos_drm_gem_buf *buf;
68 struct scatterlist *rd, *wr;
69 struct sg_table *sgt = NULL;
73 DRM_DEBUG_PRIME("%s\n", __FILE__);
75 if (WARN_ON(dir == DMA_NONE))
76 return ERR_PTR(-EINVAL);
78 /* just return current sgt if already requested. */
79 if (exynos_attach->dir == dir)
80 return &exynos_attach->sgt;
82 /* reattaching is not allowed. */
83 if (WARN_ON(exynos_attach->dir != DMA_NONE))
84 return ERR_PTR(-EBUSY);
86 buf = gem_obj->buffer;
88 DRM_ERROR("buffer is null.\n");
89 return ERR_PTR(-ENOMEM);
92 sgt = &exynos_attach->sgt;
94 ret = sg_alloc_table(sgt, buf->sgt->orig_nents, GFP_KERNEL);
96 DRM_ERROR("failed to alloc sgt.\n");
97 return ERR_PTR(-ENOMEM);
100 mutex_lock(&dev->struct_mutex);
104 for (i = 0; i < sgt->orig_nents; ++i) {
105 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
110 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
112 DRM_ERROR("failed to map sgl with iommu.\n");
117 exynos_attach->dir = dir;
118 attach->priv = exynos_attach;
120 DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
123 mutex_unlock(&dev->struct_mutex);
127 static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
128 struct sg_table *sgt,
129 enum dma_data_direction dir)
134 static void exynos_dmabuf_release(struct dma_buf *dmabuf)
136 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
138 DRM_DEBUG_PRIME("%s\n", __FILE__);
141 * exynos_dmabuf_release() call means that file object's
142 * f_count is 0 and it calls drm_gem_object_handle_unreference()
143 * to drop the references that these values had been increased
144 * at drm_prime_handle_to_fd()
146 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
147 exynos_gem_obj->base.export_dma_buf = NULL;
150 * drop this gem object refcount to release allocated buffer
153 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
157 static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
158 unsigned long page_num)
165 static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
166 unsigned long page_num,
172 static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
173 unsigned long page_num)
180 static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
181 unsigned long page_num, void *addr)
186 static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
187 struct vm_area_struct *vma)
192 static struct dma_buf_ops exynos_dmabuf_ops = {
193 .attach = exynos_gem_attach_dma_buf,
194 .detach = exynos_gem_detach_dma_buf,
195 .map_dma_buf = exynos_gem_map_dma_buf,
196 .unmap_dma_buf = exynos_gem_unmap_dma_buf,
197 .kmap = exynos_gem_dmabuf_kmap,
198 .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
199 .kunmap = exynos_gem_dmabuf_kunmap,
200 .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
201 .mmap = exynos_gem_dmabuf_mmap,
202 .release = exynos_dmabuf_release,
205 struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
206 struct drm_gem_object *obj, int flags)
208 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
210 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
211 exynos_gem_obj->base.size, flags);
214 struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
215 struct dma_buf *dma_buf)
217 struct dma_buf_attachment *attach;
218 struct sg_table *sgt;
219 struct scatterlist *sgl;
220 struct exynos_drm_gem_obj *exynos_gem_obj;
221 struct exynos_drm_gem_buf *buffer;
224 DRM_DEBUG_PRIME("%s\n", __FILE__);
226 /* is this one of own objects? */
227 if (dma_buf->ops == &exynos_dmabuf_ops) {
228 struct drm_gem_object *obj;
230 exynos_gem_obj = dma_buf->priv;
231 obj = &exynos_gem_obj->base;
233 /* is it from our device? */
234 if (obj->dev == drm_dev) {
236 * Importing dmabuf exported from out own gem increases
237 * refcount on gem itself instead of f_count of dmabuf.
239 drm_gem_object_reference(obj);
240 dma_buf_put(dma_buf);
245 attach = dma_buf_attach(dma_buf, drm_dev->dev);
247 return ERR_PTR(-EINVAL);
250 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
251 if (IS_ERR_OR_NULL(sgt)) {
256 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
258 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
260 goto err_unmap_attach;
263 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
264 if (!exynos_gem_obj) {
266 goto err_free_buffer;
271 buffer->size = dma_buf->size;
272 buffer->dma_addr = sg_dma_address(sgl);
274 if (sgt->nents == 1) {
275 /* always physically continuous memory if sgt->nents is 1. */
276 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
279 * this case could be CONTIG or NONCONTIG type but for now
281 * TODO. we have to find a way that exporter can notify
282 * the type of its own buffer to importer.
284 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
287 exynos_gem_obj->buffer = buffer;
289 exynos_gem_obj->base.import_attach = attach;
291 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
294 return &exynos_gem_obj->base;
300 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
302 dma_buf_detach(dma_buf, attach);
306 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
307 MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
308 MODULE_LICENSE("GPL");