2 * drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/dma-buf.h>
24 static struct sg_table *omap_gem_map_dma_buf(
25 struct dma_buf_attachment *attachment,
26 enum dma_data_direction dir)
28 struct drm_gem_object *obj = attachment->dmabuf->priv;
33 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
35 return ERR_PTR(-ENOMEM);
37 /* camera, etc, need physically contiguous.. but we need a
38 * better way to know this..
40 ret = omap_gem_get_paddr(obj, &paddr, true);
44 ret = sg_alloc_table(sg, 1, GFP_KERNEL);
48 sg_init_table(sg->sgl, 1);
49 sg_dma_len(sg->sgl) = obj->size;
50 sg_set_page(sg->sgl, pfn_to_page(PFN_DOWN(paddr)), obj->size, 0);
51 sg_dma_address(sg->sgl) = paddr;
53 /* this should be after _get_paddr() to ensure we have pages attached */
54 omap_gem_dma_sync(obj, dir);
62 static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
63 struct sg_table *sg, enum dma_data_direction dir)
65 struct drm_gem_object *obj = attachment->dmabuf->priv;
66 omap_gem_put_paddr(obj);
71 static void omap_gem_dmabuf_release(struct dma_buf *buffer)
73 struct drm_gem_object *obj = buffer->priv;
74 /* release reference that was taken when dmabuf was exported
75 * in omap_gem_prime_set()..
77 drm_gem_object_unreference_unlocked(obj);
81 static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer,
82 size_t start, size_t len, enum dma_data_direction dir)
84 struct drm_gem_object *obj = buffer->priv;
86 if (omap_gem_flags(obj) & OMAP_BO_TILED) {
87 /* TODO we would need to pin at least part of the buffer to
88 * get de-tiled view. For now just reject it.
92 /* make sure we have the pages: */
93 return omap_gem_get_pages(obj, &pages, true);
96 static void omap_gem_dmabuf_end_cpu_access(struct dma_buf *buffer,
97 size_t start, size_t len, enum dma_data_direction dir)
99 struct drm_gem_object *obj = buffer->priv;
100 omap_gem_put_pages(obj);
104 static void *omap_gem_dmabuf_kmap_atomic(struct dma_buf *buffer,
105 unsigned long page_num)
107 struct drm_gem_object *obj = buffer->priv;
109 omap_gem_get_pages(obj, &pages, false);
110 omap_gem_cpu_sync(obj, page_num);
111 return kmap_atomic(pages[page_num]);
114 static void omap_gem_dmabuf_kunmap_atomic(struct dma_buf *buffer,
115 unsigned long page_num, void *addr)
120 static void *omap_gem_dmabuf_kmap(struct dma_buf *buffer,
121 unsigned long page_num)
123 struct drm_gem_object *obj = buffer->priv;
125 omap_gem_get_pages(obj, &pages, false);
126 omap_gem_cpu_sync(obj, page_num);
127 return kmap(pages[page_num]);
130 static void omap_gem_dmabuf_kunmap(struct dma_buf *buffer,
131 unsigned long page_num, void *addr)
133 struct drm_gem_object *obj = buffer->priv;
135 omap_gem_get_pages(obj, &pages, false);
136 kunmap(pages[page_num]);
139 static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
140 struct vm_area_struct *vma)
142 struct drm_gem_object *obj = buffer->priv;
143 struct drm_device *dev = obj->dev;
146 if (WARN_ON(!obj->filp))
149 mutex_lock(&dev->struct_mutex);
150 ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
151 mutex_unlock(&dev->struct_mutex);
155 return omap_gem_mmap_obj(obj, vma);
158 static struct dma_buf_ops omap_dmabuf_ops = {
159 .map_dma_buf = omap_gem_map_dma_buf,
160 .unmap_dma_buf = omap_gem_unmap_dma_buf,
161 .release = omap_gem_dmabuf_release,
162 .begin_cpu_access = omap_gem_dmabuf_begin_cpu_access,
163 .end_cpu_access = omap_gem_dmabuf_end_cpu_access,
164 .kmap_atomic = omap_gem_dmabuf_kmap_atomic,
165 .kunmap_atomic = omap_gem_dmabuf_kunmap_atomic,
166 .kmap = omap_gem_dmabuf_kmap,
167 .kunmap = omap_gem_dmabuf_kunmap,
168 .mmap = omap_gem_dmabuf_mmap,
171 struct dma_buf *omap_gem_prime_export(struct drm_device *dev,
172 struct drm_gem_object *obj, int flags)
174 return dma_buf_export(obj, &omap_dmabuf_ops, obj->size, flags);
177 struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
178 struct dma_buf *buffer)
180 struct drm_gem_object *obj;
182 /* is this one of own objects? */
183 if (buffer->ops == &omap_dmabuf_ops) {
185 /* is it from our device? */
186 if (obj->dev == dev) {
188 * Importing dmabuf exported from out own gem increases
189 * refcount on gem itself instead of f_count of dmabuf.
191 drm_gem_object_reference(obj);
197 * TODO add support for importing buffers from other devices..
198 * for now we don't need this but would be nice to add eventually
200 return ERR_PTR(-EINVAL);