Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Red Hat Inc |
| 3 | * |
| 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: |
| 10 | * |
| 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 |
| 13 | * Software. |
| 14 | * |
| 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. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Dave Airlie <airlied@redhat.com> |
| 25 | */ |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 26 | #include <drm/drmP.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 27 | #include "i915_drv.h" |
| 28 | #include <linux/dma-buf.h> |
| 29 | |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 30 | static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf) |
| 31 | { |
| 32 | return to_intel_bo(buf->priv); |
| 33 | } |
| 34 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 35 | static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 36 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 37 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 38 | struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 39 | struct sg_table *st; |
| 40 | struct scatterlist *src, *dst; |
| 41 | int ret, i; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 42 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 43 | ret = i915_mutex_lock_interruptible(obj->base.dev); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 44 | if (ret) |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 45 | goto err; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 46 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 47 | ret = i915_gem_object_get_pages(obj); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 48 | if (ret) |
| 49 | goto err_unlock; |
| 50 | |
| 51 | i915_gem_object_pin_pages(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 52 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 53 | /* Copy sg so that we make an independent mapping */ |
| 54 | st = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 55 | if (st == NULL) { |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 56 | ret = -ENOMEM; |
| 57 | goto err_unpin; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 61 | if (ret) |
| 62 | goto err_free; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 63 | |
| 64 | src = obj->pages->sgl; |
| 65 | dst = st->sgl; |
| 66 | for (i = 0; i < obj->pages->nents; i++) { |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 67 | sg_set_page(dst, sg_page(src), src->length, 0); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 68 | dst = sg_next(dst); |
| 69 | src = sg_next(src); |
| 70 | } |
| 71 | |
| 72 | if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) { |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 73 | ret =-ENOMEM; |
| 74 | goto err_free_sg; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 77 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 78 | return st; |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 79 | |
| 80 | err_free_sg: |
| 81 | sg_free_table(st); |
| 82 | err_free: |
| 83 | kfree(st); |
| 84 | err_unpin: |
| 85 | i915_gem_object_unpin_pages(obj); |
| 86 | err_unlock: |
| 87 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 88 | err: |
| 89 | return ERR_PTR(ret); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 92 | static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 93 | struct sg_table *sg, |
| 94 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 95 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 96 | struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 97 | |
| 98 | mutex_lock(&obj->base.dev->struct_mutex); |
| 99 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 100 | dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir); |
| 101 | sg_free_table(sg); |
| 102 | kfree(sg); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 103 | |
| 104 | i915_gem_object_unpin_pages(obj); |
| 105 | |
| 106 | mutex_unlock(&obj->base.dev->struct_mutex); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 109 | static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf) |
| 110 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 111 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 112 | struct drm_device *dev = obj->base.dev; |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 113 | struct sg_page_iter sg_iter; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 114 | struct page **pages; |
| 115 | int ret, i; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 116 | |
| 117 | ret = i915_mutex_lock_interruptible(dev); |
| 118 | if (ret) |
| 119 | return ERR_PTR(ret); |
| 120 | |
| 121 | if (obj->dma_buf_vmapping) { |
| 122 | obj->vmapping_count++; |
| 123 | goto out_unlock; |
| 124 | } |
| 125 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 126 | ret = i915_gem_object_get_pages(obj); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 127 | if (ret) |
Chris Wilson | 993fc6e | 2013-11-29 11:44:59 +0000 | [diff] [blame] | 128 | goto err; |
| 129 | |
| 130 | i915_gem_object_pin_pages(obj); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 131 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 132 | ret = -ENOMEM; |
| 133 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 134 | pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages)); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 135 | if (pages == NULL) |
Chris Wilson | 993fc6e | 2013-11-29 11:44:59 +0000 | [diff] [blame] | 136 | goto err_unpin; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 137 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 138 | i = 0; |
Dave Airlie | b11b88e | 2013-05-01 14:23:41 +1000 | [diff] [blame] | 139 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 140 | pages[i++] = sg_page_iter_page(&sg_iter); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 141 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 142 | obj->dma_buf_vmapping = vmap(pages, i, 0, PAGE_KERNEL); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 143 | drm_free_large(pages); |
| 144 | |
| 145 | if (!obj->dma_buf_vmapping) |
Chris Wilson | 993fc6e | 2013-11-29 11:44:59 +0000 | [diff] [blame] | 146 | goto err_unpin; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 147 | |
| 148 | obj->vmapping_count = 1; |
| 149 | out_unlock: |
| 150 | mutex_unlock(&dev->struct_mutex); |
| 151 | return obj->dma_buf_vmapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 152 | |
Chris Wilson | 993fc6e | 2013-11-29 11:44:59 +0000 | [diff] [blame] | 153 | err_unpin: |
| 154 | i915_gem_object_unpin_pages(obj); |
| 155 | err: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 156 | mutex_unlock(&dev->struct_mutex); |
| 157 | return ERR_PTR(ret); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr) |
| 161 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 162 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 163 | struct drm_device *dev = obj->base.dev; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 164 | |
Chris Wilson | ce7ec76 | 2014-04-07 17:01:47 -0300 | [diff] [blame] | 165 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 166 | if (--obj->vmapping_count == 0) { |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 167 | vunmap(obj->dma_buf_vmapping); |
| 168 | obj->dma_buf_vmapping = NULL; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 169 | |
| 170 | i915_gem_object_unpin_pages(obj); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 171 | } |
| 172 | mutex_unlock(&dev->struct_mutex); |
| 173 | } |
| 174 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 175 | static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num) |
| 176 | { |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 181 | { |
| 182 | |
| 183 | } |
| 184 | static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num) |
| 185 | { |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 190 | { |
| 191 | |
| 192 | } |
| 193 | |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 194 | static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) |
| 195 | { |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
Tiago Vignatti | 831e9da | 2015-12-22 19:36:45 -0200 | [diff] [blame] | 199 | static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction) |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 200 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 201 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 202 | struct drm_device *dev = obj->base.dev; |
| 203 | int ret; |
| 204 | bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE); |
| 205 | |
| 206 | ret = i915_mutex_lock_interruptible(dev); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | |
| 210 | ret = i915_gem_object_set_to_cpu_domain(obj, write); |
| 211 | mutex_unlock(&dev->struct_mutex); |
| 212 | return ret; |
| 213 | } |
| 214 | |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame^] | 215 | static void i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction) |
| 216 | { |
| 217 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
| 218 | struct drm_device *dev = obj->base.dev; |
| 219 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 220 | bool was_interruptible; |
| 221 | int ret; |
| 222 | |
| 223 | mutex_lock(&dev->struct_mutex); |
| 224 | was_interruptible = dev_priv->mm.interruptible; |
| 225 | dev_priv->mm.interruptible = false; |
| 226 | |
| 227 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 228 | |
| 229 | dev_priv->mm.interruptible = was_interruptible; |
| 230 | mutex_unlock(&dev->struct_mutex); |
| 231 | |
| 232 | if (unlikely(ret)) |
| 233 | DRM_ERROR("unable to flush buffer following CPU access; rendering may be corrupt\n"); |
| 234 | } |
| 235 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 236 | static const struct dma_buf_ops i915_dmabuf_ops = { |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 237 | .map_dma_buf = i915_gem_map_dma_buf, |
| 238 | .unmap_dma_buf = i915_gem_unmap_dma_buf, |
Daniel Vetter | c1d6798 | 2013-08-15 00:02:30 +0200 | [diff] [blame] | 239 | .release = drm_gem_dmabuf_release, |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 240 | .kmap = i915_gem_dmabuf_kmap, |
| 241 | .kmap_atomic = i915_gem_dmabuf_kmap_atomic, |
| 242 | .kunmap = i915_gem_dmabuf_kunmap, |
| 243 | .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic, |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 244 | .mmap = i915_gem_dmabuf_mmap, |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 245 | .vmap = i915_gem_dmabuf_vmap, |
| 246 | .vunmap = i915_gem_dmabuf_vunmap, |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 247 | .begin_cpu_access = i915_gem_begin_cpu_access, |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame^] | 248 | .end_cpu_access = i915_gem_end_cpu_access, |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | struct dma_buf *i915_gem_prime_export(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 252 | struct drm_gem_object *gem_obj, int flags) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 253 | { |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 254 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 255 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 256 | |
| 257 | exp_info.ops = &i915_dmabuf_ops; |
| 258 | exp_info.size = gem_obj->size; |
| 259 | exp_info.flags = flags; |
| 260 | exp_info.priv = gem_obj; |
| 261 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 262 | |
| 263 | if (obj->ops->dmabuf_export) { |
| 264 | int ret = obj->ops->dmabuf_export(obj); |
| 265 | if (ret) |
| 266 | return ERR_PTR(ret); |
| 267 | } |
| 268 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 269 | return dma_buf_export(&exp_info); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 272 | static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj) |
| 273 | { |
| 274 | struct sg_table *sg; |
| 275 | |
| 276 | sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL); |
| 277 | if (IS_ERR(sg)) |
| 278 | return PTR_ERR(sg); |
| 279 | |
| 280 | obj->pages = sg; |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj) |
| 285 | { |
| 286 | dma_buf_unmap_attachment(obj->base.import_attach, |
| 287 | obj->pages, DMA_BIDIRECTIONAL); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = { |
| 291 | .get_pages = i915_gem_object_get_pages_dmabuf, |
| 292 | .put_pages = i915_gem_object_put_pages_dmabuf, |
| 293 | }; |
| 294 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 295 | struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 296 | struct dma_buf *dma_buf) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 297 | { |
| 298 | struct dma_buf_attachment *attach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 299 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 300 | int ret; |
| 301 | |
| 302 | /* is this one of own objects? */ |
| 303 | if (dma_buf->ops == &i915_dmabuf_ops) { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 304 | obj = dma_buf_to_obj(dma_buf); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 305 | /* is it from our device? */ |
| 306 | if (obj->base.dev == dev) { |
Seung-Woo Kim | be8a42a | 2012-09-27 15:30:06 +0900 | [diff] [blame] | 307 | /* |
| 308 | * Importing dmabuf exported from out own gem increases |
| 309 | * refcount on gem itself instead of f_count of dmabuf. |
| 310 | */ |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 311 | drm_gem_object_reference(&obj->base); |
| 312 | return &obj->base; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* need to attach */ |
| 317 | attach = dma_buf_attach(dma_buf, dev->dev); |
| 318 | if (IS_ERR(attach)) |
| 319 | return ERR_CAST(attach); |
| 320 | |
Imre Deak | 011c2282 | 2013-04-19 11:11:56 +1000 | [diff] [blame] | 321 | get_dma_buf(dma_buf); |
| 322 | |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 323 | obj = i915_gem_object_alloc(dev); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 324 | if (obj == NULL) { |
| 325 | ret = -ENOMEM; |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 326 | goto fail_detach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 327 | } |
| 328 | |
David Herrmann | 89c8233 | 2013-07-11 11:56:32 +0200 | [diff] [blame] | 329 | drm_gem_private_object_init(dev, &obj->base, dma_buf->size); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 330 | i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 331 | obj->base.import_attach = attach; |
| 332 | |
| 333 | return &obj->base; |
| 334 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 335 | fail_detach: |
| 336 | dma_buf_detach(dma_buf, attach); |
Imre Deak | 011c2282 | 2013-04-19 11:11:56 +1000 | [diff] [blame] | 337 | dma_buf_put(dma_buf); |
| 338 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 339 | return ERR_PTR(ret); |
| 340 | } |