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;
25 static int exynos_gem_attach_dma_buf(struct dma_buf *dmabuf,
27 struct dma_buf_attachment *attach)
29 struct exynos_drm_dmabuf_attachment *exynos_attach;
31 exynos_attach = kzalloc(sizeof(*exynos_attach), GFP_KERNEL);
35 exynos_attach->dir = DMA_NONE;
36 attach->priv = exynos_attach;
41 static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
42 struct dma_buf_attachment *attach)
44 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
50 sgt = &exynos_attach->sgt;
52 if (exynos_attach->dir != DMA_NONE)
53 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
61 static struct sg_table *
62 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
63 enum dma_data_direction dir)
65 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
66 struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
67 struct drm_device *dev = gem_obj->base.dev;
68 struct exynos_drm_gem_buf *buf;
69 struct scatterlist *rd, *wr;
70 struct sg_table *sgt = NULL;
74 DRM_DEBUG_PRIME("%s\n", __FILE__);
76 /* just return current sgt if already requested. */
77 if (exynos_attach->dir == dir && exynos_attach->is_mapped)
78 return &exynos_attach->sgt;
80 buf = gem_obj->buffer;
82 DRM_ERROR("buffer is null.\n");
83 return ERR_PTR(-ENOMEM);
86 sgt = &exynos_attach->sgt;
88 ret = sg_alloc_table(sgt, buf->sgt->orig_nents, GFP_KERNEL);
90 DRM_ERROR("failed to alloc sgt.\n");
91 return ERR_PTR(-ENOMEM);
94 mutex_lock(&dev->struct_mutex);
98 for (i = 0; i < sgt->orig_nents; ++i) {
99 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
104 if (dir != DMA_NONE) {
105 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
107 DRM_ERROR("failed to map sgl with iommu.\n");
114 exynos_attach->is_mapped = true;
115 exynos_attach->dir = dir;
116 attach->priv = exynos_attach;
118 DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
121 mutex_unlock(&dev->struct_mutex);
125 static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
126 struct sg_table *sgt,
127 enum dma_data_direction dir)
132 static void exynos_dmabuf_release(struct dma_buf *dmabuf)
134 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
136 DRM_DEBUG_PRIME("%s\n", __FILE__);
139 * exynos_dmabuf_release() call means that file object's
140 * f_count is 0 and it calls drm_gem_object_handle_unreference()
141 * to drop the references that these values had been increased
142 * at drm_prime_handle_to_fd()
144 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
145 exynos_gem_obj->base.export_dma_buf = NULL;
148 * drop this gem object refcount to release allocated buffer
151 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
155 static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
156 unsigned long page_num)
163 static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
164 unsigned long page_num,
170 static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
171 unsigned long page_num)
178 static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
179 unsigned long page_num, void *addr)
184 static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
185 struct vm_area_struct *vma)
190 static struct dma_buf_ops exynos_dmabuf_ops = {
191 .attach = exynos_gem_attach_dma_buf,
192 .detach = exynos_gem_detach_dma_buf,
193 .map_dma_buf = exynos_gem_map_dma_buf,
194 .unmap_dma_buf = exynos_gem_unmap_dma_buf,
195 .kmap = exynos_gem_dmabuf_kmap,
196 .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
197 .kunmap = exynos_gem_dmabuf_kunmap,
198 .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
199 .mmap = exynos_gem_dmabuf_mmap,
200 .release = exynos_dmabuf_release,
203 struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
204 struct drm_gem_object *obj, int flags)
206 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
208 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
209 exynos_gem_obj->base.size, flags);
212 struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
213 struct dma_buf *dma_buf)
215 struct dma_buf_attachment *attach;
216 struct sg_table *sgt;
217 struct scatterlist *sgl;
218 struct exynos_drm_gem_obj *exynos_gem_obj;
219 struct exynos_drm_gem_buf *buffer;
222 DRM_DEBUG_PRIME("%s\n", __FILE__);
224 /* is this one of own objects? */
225 if (dma_buf->ops == &exynos_dmabuf_ops) {
226 struct drm_gem_object *obj;
228 exynos_gem_obj = dma_buf->priv;
229 obj = &exynos_gem_obj->base;
231 /* is it from our device? */
232 if (obj->dev == drm_dev) {
234 * Importing dmabuf exported from out own gem increases
235 * refcount on gem itself instead of f_count of dmabuf.
237 drm_gem_object_reference(obj);
238 dma_buf_put(dma_buf);
243 attach = dma_buf_attach(dma_buf, drm_dev->dev);
245 return ERR_PTR(-EINVAL);
248 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
249 if (IS_ERR_OR_NULL(sgt)) {
254 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
256 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
258 goto err_unmap_attach;
261 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
262 if (!exynos_gem_obj) {
264 goto err_free_buffer;
269 buffer->size = dma_buf->size;
270 buffer->dma_addr = sg_dma_address(sgl);
272 if (sgt->nents == 1) {
273 /* always physically continuous memory if sgt->nents is 1. */
274 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
277 * this case could be CONTIG or NONCONTIG type but for now
279 * TODO. we have to find a way that exporter can notify
280 * the type of its own buffer to importer.
282 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
285 exynos_gem_obj->buffer = buffer;
287 exynos_gem_obj->base.import_attach = attach;
289 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
292 return &exynos_gem_obj->base;
298 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
300 dma_buf_detach(dma_buf, attach);
304 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
305 MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
306 MODULE_LICENSE("GPL");