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 | */ |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 26 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 27 | #include <linux/dma-buf.h> |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 28 | #include <linux/reservation.h> |
| 29 | |
| 30 | #include <drm/drmP.h> |
| 31 | |
| 32 | #include "i915_drv.h" |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 33 | |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 34 | static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf) |
| 35 | { |
| 36 | return to_intel_bo(buf->priv); |
| 37 | } |
| 38 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 39 | 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] | 40 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 41 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 42 | struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 43 | struct sg_table *st; |
| 44 | struct scatterlist *src, *dst; |
| 45 | int ret, i; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 46 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 47 | ret = i915_mutex_lock_interruptible(obj->base.dev); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 48 | if (ret) |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 49 | goto err; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 50 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 51 | ret = i915_gem_object_get_pages(obj); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 52 | if (ret) |
| 53 | goto err_unlock; |
| 54 | |
| 55 | i915_gem_object_pin_pages(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 56 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 57 | /* Copy sg so that we make an independent mapping */ |
| 58 | st = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 59 | if (st == NULL) { |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 60 | ret = -ENOMEM; |
| 61 | goto err_unpin; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 65 | if (ret) |
| 66 | goto err_free; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 67 | |
| 68 | src = obj->pages->sgl; |
| 69 | dst = st->sgl; |
| 70 | for (i = 0; i < obj->pages->nents; i++) { |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 71 | sg_set_page(dst, sg_page(src), src->length, 0); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 72 | dst = sg_next(dst); |
| 73 | src = sg_next(src); |
| 74 | } |
| 75 | |
| 76 | if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) { |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 77 | ret =-ENOMEM; |
| 78 | goto err_free_sg; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 81 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 82 | return st; |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 83 | |
| 84 | err_free_sg: |
| 85 | sg_free_table(st); |
| 86 | err_free: |
| 87 | kfree(st); |
| 88 | err_unpin: |
| 89 | i915_gem_object_unpin_pages(obj); |
| 90 | err_unlock: |
| 91 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 92 | err: |
| 93 | return ERR_PTR(ret); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 96 | static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 97 | struct sg_table *sg, |
| 98 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 99 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 100 | struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 101 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 102 | dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir); |
| 103 | sg_free_table(sg); |
| 104 | kfree(sg); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 105 | |
Chris Wilson | 6d19245 | 2016-04-08 12:11:09 +0100 | [diff] [blame] | 106 | mutex_lock(&obj->base.dev->struct_mutex); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 107 | i915_gem_object_unpin_pages(obj); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 108 | mutex_unlock(&obj->base.dev->struct_mutex); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 111 | static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf) |
| 112 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 113 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 114 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 115 | void *addr; |
| 116 | int ret; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 117 | |
| 118 | ret = i915_mutex_lock_interruptible(dev); |
| 119 | if (ret) |
| 120 | return ERR_PTR(ret); |
| 121 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 122 | addr = i915_gem_object_pin_map(obj, I915_MAP_WB); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 123 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 124 | |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 125 | return addr; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr) |
| 129 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 130 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 131 | struct drm_device *dev = obj->base.dev; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 132 | |
Chris Wilson | ce7ec76 | 2014-04-07 17:01:47 -0300 | [diff] [blame] | 133 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 134 | i915_gem_object_unpin_map(obj); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 135 | mutex_unlock(&dev->struct_mutex); |
| 136 | } |
| 137 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 138 | static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num) |
| 139 | { |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 144 | { |
| 145 | |
| 146 | } |
| 147 | static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num) |
| 148 | { |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 153 | { |
| 154 | |
| 155 | } |
| 156 | |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 157 | static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) |
| 158 | { |
Tiago Vignatti | 2dbf0d9 | 2015-12-22 19:36:48 -0200 | [diff] [blame] | 159 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
| 160 | int ret; |
| 161 | |
| 162 | if (obj->base.size < vma->vm_end - vma->vm_start) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | if (!obj->base.filp) |
| 166 | return -ENODEV; |
| 167 | |
| 168 | ret = obj->base.filp->f_op->mmap(obj->base.filp, vma); |
| 169 | if (ret) |
| 170 | return ret; |
| 171 | |
| 172 | fput(vma->vm_file); |
| 173 | vma->vm_file = get_file(obj->base.filp); |
| 174 | |
| 175 | return 0; |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Tiago Vignatti | 831e9da | 2015-12-22 19:36:45 -0200 | [diff] [blame] | 178 | 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] | 179 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 180 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 181 | struct drm_device *dev = obj->base.dev; |
| 182 | int ret; |
| 183 | bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE); |
| 184 | |
| 185 | ret = i915_mutex_lock_interruptible(dev); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | ret = i915_gem_object_set_to_cpu_domain(obj, write); |
| 190 | mutex_unlock(&dev->struct_mutex); |
| 191 | return ret; |
| 192 | } |
| 193 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 194 | static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction) |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 195 | { |
| 196 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
| 197 | struct drm_device *dev = obj->base.dev; |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 198 | int ret; |
| 199 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 200 | ret = i915_mutex_lock_interruptible(dev); |
| 201 | if (ret) |
| 202 | return ret; |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 203 | |
| 204 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 205 | mutex_unlock(&dev->struct_mutex); |
| 206 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 207 | return ret; |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 208 | } |
| 209 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 210 | static const struct dma_buf_ops i915_dmabuf_ops = { |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 211 | .map_dma_buf = i915_gem_map_dma_buf, |
| 212 | .unmap_dma_buf = i915_gem_unmap_dma_buf, |
Daniel Vetter | c1d6798 | 2013-08-15 00:02:30 +0200 | [diff] [blame] | 213 | .release = drm_gem_dmabuf_release, |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 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, |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 218 | .mmap = i915_gem_dmabuf_mmap, |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 219 | .vmap = i915_gem_dmabuf_vmap, |
| 220 | .vunmap = i915_gem_dmabuf_vunmap, |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 221 | .begin_cpu_access = i915_gem_begin_cpu_access, |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 222 | .end_cpu_access = i915_gem_end_cpu_access, |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 223 | }; |
| 224 | |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 225 | static void export_fences(struct drm_i915_gem_object *obj, |
| 226 | struct dma_buf *dma_buf) |
| 227 | { |
| 228 | struct reservation_object *resv = dma_buf->resv; |
| 229 | struct drm_i915_gem_request *req; |
| 230 | unsigned long active; |
| 231 | int idx; |
| 232 | |
| 233 | active = __I915_BO_ACTIVE(obj); |
| 234 | if (!active) |
| 235 | return; |
| 236 | |
| 237 | /* Serialise with execbuf to prevent concurrent fence-loops */ |
| 238 | mutex_lock(&obj->base.dev->struct_mutex); |
| 239 | |
| 240 | /* Mark the object for future fences before racily adding old fences */ |
| 241 | obj->base.dma_buf = dma_buf; |
| 242 | |
| 243 | ww_mutex_lock(&resv->lock, NULL); |
| 244 | |
| 245 | for_each_active(active, idx) { |
| 246 | req = i915_gem_active_get(&obj->last_read[idx], |
| 247 | &obj->base.dev->struct_mutex); |
| 248 | if (!req) |
| 249 | continue; |
| 250 | |
| 251 | if (reservation_object_reserve_shared(resv) == 0) |
| 252 | reservation_object_add_shared_fence(resv, &req->fence); |
| 253 | |
| 254 | i915_gem_request_put(req); |
| 255 | } |
| 256 | |
| 257 | req = i915_gem_active_get(&obj->last_write, |
| 258 | &obj->base.dev->struct_mutex); |
| 259 | if (req) { |
| 260 | reservation_object_add_excl_fence(resv, &req->fence); |
| 261 | i915_gem_request_put(req); |
| 262 | } |
| 263 | |
| 264 | ww_mutex_unlock(&resv->lock); |
| 265 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 266 | } |
| 267 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 268 | struct dma_buf *i915_gem_prime_export(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 269 | struct drm_gem_object *gem_obj, int flags) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 270 | { |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 271 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 272 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 273 | struct dma_buf *dma_buf; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 274 | |
| 275 | exp_info.ops = &i915_dmabuf_ops; |
| 276 | exp_info.size = gem_obj->size; |
| 277 | exp_info.flags = flags; |
| 278 | exp_info.priv = gem_obj; |
| 279 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 280 | if (obj->ops->dmabuf_export) { |
| 281 | int ret = obj->ops->dmabuf_export(obj); |
| 282 | if (ret) |
| 283 | return ERR_PTR(ret); |
| 284 | } |
| 285 | |
Chris Wilson | a4fce9c | 2016-10-05 13:21:44 +0100 | [diff] [blame] | 286 | dma_buf = drm_gem_dmabuf_export(dev, &exp_info); |
Chris Wilson | ad778f8 | 2016-08-04 16:32:42 +0100 | [diff] [blame] | 287 | if (IS_ERR(dma_buf)) |
| 288 | return dma_buf; |
| 289 | |
| 290 | export_fences(obj, dma_buf); |
| 291 | return dma_buf; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 294 | static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj) |
| 295 | { |
| 296 | struct sg_table *sg; |
| 297 | |
| 298 | sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL); |
| 299 | if (IS_ERR(sg)) |
| 300 | return PTR_ERR(sg); |
| 301 | |
| 302 | obj->pages = sg; |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj) |
| 307 | { |
| 308 | dma_buf_unmap_attachment(obj->base.import_attach, |
| 309 | obj->pages, DMA_BIDIRECTIONAL); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = { |
| 313 | .get_pages = i915_gem_object_get_pages_dmabuf, |
| 314 | .put_pages = i915_gem_object_put_pages_dmabuf, |
| 315 | }; |
| 316 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 317 | struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 318 | struct dma_buf *dma_buf) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 319 | { |
| 320 | struct dma_buf_attachment *attach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 321 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 322 | int ret; |
| 323 | |
| 324 | /* is this one of own objects? */ |
| 325 | if (dma_buf->ops == &i915_dmabuf_ops) { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 326 | obj = dma_buf_to_obj(dma_buf); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 327 | /* is it from our device? */ |
| 328 | if (obj->base.dev == dev) { |
Seung-Woo Kim | be8a42a | 2012-09-27 15:30:06 +0900 | [diff] [blame] | 329 | /* |
| 330 | * Importing dmabuf exported from out own gem increases |
| 331 | * refcount on gem itself instead of f_count of dmabuf. |
| 332 | */ |
Chris Wilson | 25dc556 | 2016-07-20 13:31:52 +0100 | [diff] [blame] | 333 | return &i915_gem_object_get(obj)->base; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | /* need to attach */ |
| 338 | attach = dma_buf_attach(dma_buf, dev->dev); |
| 339 | if (IS_ERR(attach)) |
| 340 | return ERR_CAST(attach); |
| 341 | |
Imre Deak | 011c2282 | 2013-04-19 11:11:56 +1000 | [diff] [blame] | 342 | get_dma_buf(dma_buf); |
| 343 | |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 344 | obj = i915_gem_object_alloc(dev); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 345 | if (obj == NULL) { |
| 346 | ret = -ENOMEM; |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 347 | goto fail_detach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 348 | } |
| 349 | |
David Herrmann | 89c8233 | 2013-07-11 11:56:32 +0200 | [diff] [blame] | 350 | drm_gem_private_object_init(dev, &obj->base, dma_buf->size); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 351 | i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 352 | obj->base.import_attach = attach; |
| 353 | |
Chris Wilson | 30bc06c | 2016-07-20 09:21:14 +0100 | [diff] [blame] | 354 | /* We use GTT as shorthand for a coherent domain, one that is |
| 355 | * neither in the GPU cache nor in the CPU cache, where all |
| 356 | * writes are immediately visible in memory. (That's not strictly |
| 357 | * true, but it's close! There are internal buffers such as the |
| 358 | * write-combined buffer or a delay through the chipset for GTT |
| 359 | * writes that do require us to treat GTT as a separate cache domain.) |
| 360 | */ |
| 361 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
| 362 | obj->base.write_domain = 0; |
| 363 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 364 | return &obj->base; |
| 365 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 366 | fail_detach: |
| 367 | dma_buf_detach(dma_buf, attach); |
Imre Deak | 011c2282 | 2013-04-19 11:11:56 +1000 | [diff] [blame] | 368 | dma_buf_put(dma_buf); |
| 369 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 370 | return ERR_PTR(ret); |
| 371 | } |