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.
28 #include "exynos_drm.h"
29 #include "exynos_drm_drv.h"
30 #include "exynos_drm_gem.h"
32 #include <linux/dma-buf.h>
34 static struct sg_table *exynos_pages_to_sg(struct page **pages, int nr_pages,
35 unsigned int page_size)
37 struct sg_table *sgt = NULL;
38 struct scatterlist *sgl;
41 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
45 ret = sg_alloc_table(sgt, nr_pages, GFP_KERNEL);
49 if (page_size < PAGE_SIZE)
50 page_size = PAGE_SIZE;
52 for_each_sg(sgt->sgl, sgl, nr_pages, i)
53 sg_set_page(sgl, pages[i], page_size, 0);
64 static struct sg_table *
65 exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
66 enum dma_data_direction dir)
68 struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
69 struct drm_device *dev = gem_obj->base.dev;
70 struct exynos_drm_gem_buf *buf;
71 struct sg_table *sgt = NULL;
75 DRM_DEBUG_PRIME("%s\n", __FILE__);
77 mutex_lock(&dev->struct_mutex);
79 buf = gem_obj->buffer;
81 /* there should always be pages allocated. */
83 DRM_ERROR("pages is null.\n");
87 npages = buf->size / buf->page_size;
89 sgt = exynos_pages_to_sg(buf->pages, npages, buf->page_size);
91 DRM_DEBUG_PRIME("exynos_pages_to_sg returned NULL!\n");
94 nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
96 DRM_DEBUG_PRIME("npages = %d buffer size = 0x%lx page_size = 0x%lx\n",
97 npages, buf->size, buf->page_size);
100 mutex_unlock(&dev->struct_mutex);
104 static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
105 struct sg_table *sgt,
106 enum dma_data_direction dir)
108 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
114 static void exynos_dmabuf_release(struct dma_buf *dmabuf)
116 struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
118 DRM_DEBUG_PRIME("%s\n", __FILE__);
121 * exynos_dmabuf_release() call means that file object's
122 * f_count is 0 and it calls drm_gem_object_handle_unreference()
123 * to drop the references that these values had been increased
124 * at drm_prime_handle_to_fd()
126 if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
127 exynos_gem_obj->base.export_dma_buf = NULL;
130 * drop this gem object refcount to release allocated buffer
133 drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
137 static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
138 unsigned long page_num)
145 static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
146 unsigned long page_num,
152 static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
153 unsigned long page_num)
160 static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
161 unsigned long page_num, void *addr)
166 static 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,
173 .release = exynos_dmabuf_release,
176 struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
177 struct drm_gem_object *obj, int flags)
179 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
181 return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
182 exynos_gem_obj->base.size, 0600);
185 struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
186 struct dma_buf *dma_buf)
188 struct dma_buf_attachment *attach;
189 struct sg_table *sgt;
190 struct scatterlist *sgl;
191 struct exynos_drm_gem_obj *exynos_gem_obj;
192 struct exynos_drm_gem_buf *buffer;
196 DRM_DEBUG_PRIME("%s\n", __FILE__);
198 /* is this one of own objects? */
199 if (dma_buf->ops == &exynos_dmabuf_ops) {
200 struct drm_gem_object *obj;
202 exynos_gem_obj = dma_buf->priv;
203 obj = &exynos_gem_obj->base;
205 /* is it from our device? */
206 if (obj->dev == drm_dev) {
207 drm_gem_object_reference(obj);
212 attach = dma_buf_attach(dma_buf, drm_dev->dev);
214 return ERR_PTR(-EINVAL);
217 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
218 if (IS_ERR_OR_NULL(sgt)) {
223 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
225 DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
227 goto err_unmap_attach;
230 buffer->pages = kzalloc(sizeof(*page) * sgt->nents, GFP_KERNEL);
231 if (!buffer->pages) {
232 DRM_ERROR("failed to allocate pages.\n");
234 goto err_free_buffer;
237 exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
238 if (!exynos_gem_obj) {
245 if (sgt->nents == 1) {
246 buffer->dma_addr = sg_dma_address(sgt->sgl);
247 buffer->size = sg_dma_len(sgt->sgl);
249 /* always physically continuous memory if sgt->nents is 1. */
250 exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
254 buffer->dma_addr = sg_dma_address(sgl);
255 while (i < sgt->nents) {
256 buffer->pages[i] = sg_page(sgl);
257 buffer->size += sg_dma_len(sgl);
262 exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
265 exynos_gem_obj->buffer = buffer;
267 exynos_gem_obj->base.import_attach = attach;
269 DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
272 return &exynos_gem_obj->base;
275 kfree(buffer->pages);
276 buffer->pages = NULL;
281 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
283 dma_buf_detach(dma_buf, attach);
287 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
288 MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
289 MODULE_LICENSE("GPL");