3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
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:
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
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.
27 #include <drm/exynos_drm.h>
28 #include "exynos_drm_drv.h"
29 #include "exynos_drm_gem.h"
31 #include <linux/dma-buf.h>
33 struct exynos_drm_dmabuf_attachment {
35 enum dma_data_direction dir;
38 static int exynos_gem_attach_dma_buf(struct dma_buf *dmabuf,
40 struct dma_buf_attachment *attach)
42 struct exynos_drm_dmabuf_attachment *exynos_attach;
44 exynos_attach = kzalloc(sizeof(*exynos_attach), GFP_KERNEL);
48 exynos_attach->dir = DMA_NONE;
49 attach->priv = exynos_attach;
54 static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
55 struct dma_buf_attachment *attach)
57 struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
63 sgt = &exynos_attach->sgt;
65 if (exynos_attach->dir != DMA_NONE)
66 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
74 static struct sg_table *
75 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
76 enum dma_data_direction dir)
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;
87 DRM_DEBUG_PRIME("%s\n", __FILE__);
89 if (WARN_ON(dir == DMA_NONE))
90 return ERR_PTR(-EINVAL);
92 /* just return current sgt if already requested. */
93 if (exynos_attach->dir == dir)
94 return &exynos_attach->sgt;
96 /* reattaching is not allowed. */
97 if (WARN_ON(exynos_attach->dir != DMA_NONE))
98 return ERR_PTR(-EBUSY);
100 buf = gem_obj->buffer;
102 DRM_ERROR("buffer is null.\n");
103 return ERR_PTR(-ENOMEM);
106 sgt = &exynos_attach->sgt;
108 ret = sg_alloc_table(sgt, buf->sgt->orig_nents, GFP_KERNEL);
110 DRM_ERROR("failed to alloc sgt.\n");
111 return ERR_PTR(-ENOMEM);
114 mutex_lock(&dev->struct_mutex);
118 for (i = 0; i < sgt->orig_nents; ++i) {
119 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
124 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
126 DRM_ERROR("failed to map sgl with iommu.\n");
131 exynos_attach->dir = dir;
132 attach->priv = exynos_attach;
134 DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
137 mutex_unlock(&dev->struct_mutex);
141 static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
142 struct sg_table *sgt,
143 enum dma_data_direction dir)
148 static void exynos_dmabuf_release(struct dma_buf *dmabuf)
150 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
152 DRM_DEBUG_PRIME("%s\n", __FILE__);
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()
160 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
161 exynos_gem_obj->base.export_dma_buf = NULL;
164 * drop this gem object refcount to release allocated buffer
167 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
171 static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
172 unsigned long page_num)
179 static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
180 unsigned long page_num,
186 static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
187 unsigned long page_num)
194 static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
195 unsigned long page_num, void *addr)
200 static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
201 struct vm_area_struct *vma)
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,
219 struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
220 struct drm_gem_object *obj, int flags)
222 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
224 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
225 exynos_gem_obj->base.size, 0600);
228 struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
229 struct dma_buf *dma_buf)
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;
238 DRM_DEBUG_PRIME("%s\n", __FILE__);
240 /* is this one of own objects? */
241 if (dma_buf->ops == &exynos_dmabuf_ops) {
242 struct drm_gem_object *obj;
244 exynos_gem_obj = dma_buf->priv;
245 obj = &exynos_gem_obj->base;
247 /* is it from our device? */
248 if (obj->dev == drm_dev) {
249 drm_gem_object_reference(obj);
254 attach = dma_buf_attach(dma_buf, drm_dev->dev);
256 return ERR_PTR(-EINVAL);
259 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
260 if (IS_ERR_OR_NULL(sgt)) {
265 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
267 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
269 goto err_unmap_attach;
272 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
273 if (!exynos_gem_obj) {
275 goto err_free_buffer;
280 buffer->size = dma_buf->size;
281 buffer->dma_addr = sg_dma_address(sgl);
283 if (sgt->nents == 1) {
284 /* always physically continuous memory if sgt->nents is 1. */
285 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
288 * this case could be CONTIG or NONCONTIG type but for now
290 * TODO. we have to find a way that exporter can notify
291 * the type of its own buffer to importer.
293 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
296 exynos_gem_obj->buffer = buffer;
298 exynos_gem_obj->base.import_attach = attach;
300 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
303 return &exynos_gem_obj->base;
309 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
311 dma_buf_detach(dma_buf, attach);
315 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
316 MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
317 MODULE_LICENSE("GPL");