2 * Copyright 2012 Red Hat Inc
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Dave Airlie <airlied@redhat.com>
28 #include <linux/dma-buf.h>
30 static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
31 enum dma_data_direction dir)
33 struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
35 struct scatterlist *src, *dst;
38 ret = i915_mutex_lock_interruptible(obj->base.dev);
42 ret = i915_gem_object_get_pages(obj);
48 /* Copy sg so that we make an independent mapping */
49 st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
51 st = ERR_PTR(-ENOMEM);
55 ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
62 src = obj->pages->sgl;
64 for (i = 0; i < obj->pages->nents; i++) {
65 sg_set_page(dst, sg_page(src), PAGE_SIZE, 0);
70 if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
73 st = ERR_PTR(-ENOMEM);
77 i915_gem_object_pin_pages(obj);
80 mutex_unlock(&obj->base.dev->struct_mutex);
84 static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
86 enum dma_data_direction dir)
88 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
93 static void i915_gem_dmabuf_release(struct dma_buf *dma_buf)
95 struct drm_i915_gem_object *obj = dma_buf->priv;
97 if (obj->base.export_dma_buf == dma_buf) {
98 /* drop the reference on the export fd holds */
99 obj->base.export_dma_buf = NULL;
100 drm_gem_object_unreference_unlocked(&obj->base);
104 static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
106 struct drm_i915_gem_object *obj = dma_buf->priv;
107 struct drm_device *dev = obj->base.dev;
108 struct scatterlist *sg;
112 ret = i915_mutex_lock_interruptible(dev);
116 if (obj->dma_buf_vmapping) {
117 obj->vmapping_count++;
121 ret = i915_gem_object_get_pages(obj);
127 pages = drm_malloc_ab(obj->pages->nents, sizeof(struct page *));
131 for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i)
132 pages[i] = sg_page(sg);
134 obj->dma_buf_vmapping = vmap(pages, obj->pages->nents, 0, PAGE_KERNEL);
135 drm_free_large(pages);
137 if (!obj->dma_buf_vmapping)
140 obj->vmapping_count = 1;
141 i915_gem_object_pin_pages(obj);
143 mutex_unlock(&dev->struct_mutex);
144 return obj->dma_buf_vmapping;
147 mutex_unlock(&dev->struct_mutex);
151 static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
153 struct drm_i915_gem_object *obj = dma_buf->priv;
154 struct drm_device *dev = obj->base.dev;
157 ret = i915_mutex_lock_interruptible(dev);
161 if (--obj->vmapping_count == 0) {
162 vunmap(obj->dma_buf_vmapping);
163 obj->dma_buf_vmapping = NULL;
165 i915_gem_object_unpin_pages(obj);
167 mutex_unlock(&dev->struct_mutex);
170 static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
175 static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
179 static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
184 static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
189 static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
194 static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
196 struct drm_i915_gem_object *obj = dma_buf->priv;
197 struct drm_device *dev = obj->base.dev;
199 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
201 ret = i915_mutex_lock_interruptible(dev);
205 ret = i915_gem_object_set_to_cpu_domain(obj, write);
206 mutex_unlock(&dev->struct_mutex);
210 static const struct dma_buf_ops i915_dmabuf_ops = {
211 .map_dma_buf = i915_gem_map_dma_buf,
212 .unmap_dma_buf = i915_gem_unmap_dma_buf,
213 .release = i915_gem_dmabuf_release,
214 .kmap = i915_gem_dmabuf_kmap,
215 .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
216 .kunmap = i915_gem_dmabuf_kunmap,
217 .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
218 .mmap = i915_gem_dmabuf_mmap,
219 .vmap = i915_gem_dmabuf_vmap,
220 .vunmap = i915_gem_dmabuf_vunmap,
221 .begin_cpu_access = i915_gem_begin_cpu_access,
224 struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
225 struct drm_gem_object *gem_obj, int flags)
227 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
229 return dma_buf_export(obj, &i915_dmabuf_ops, obj->base.size, 0600);
232 static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
236 sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL);
241 obj->has_dma_mapping = true;
245 static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj)
247 dma_buf_unmap_attachment(obj->base.import_attach,
248 obj->pages, DMA_BIDIRECTIONAL);
249 obj->has_dma_mapping = false;
252 static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
253 .get_pages = i915_gem_object_get_pages_dmabuf,
254 .put_pages = i915_gem_object_put_pages_dmabuf,
257 struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
258 struct dma_buf *dma_buf)
260 struct dma_buf_attachment *attach;
261 struct drm_i915_gem_object *obj;
264 /* is this one of own objects? */
265 if (dma_buf->ops == &i915_dmabuf_ops) {
267 /* is it from our device? */
268 if (obj->base.dev == dev) {
269 drm_gem_object_reference(&obj->base);
275 attach = dma_buf_attach(dma_buf, dev->dev);
277 return ERR_CAST(attach);
280 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
286 ret = drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
292 i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
293 obj->base.import_attach = attach;
298 dma_buf_detach(dma_buf, attach);