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 | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 47 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 48 | if (ret) |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 49 | goto err; |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 50 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 51 | /* Copy sg so that we make an independent mapping */ |
| 52 | st = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 53 | if (st == NULL) { |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 54 | ret = -ENOMEM; |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 55 | goto err_unpin_pages; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 58 | ret = sg_alloc_table(st, obj->mm.pages->nents, GFP_KERNEL); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 59 | if (ret) |
| 60 | goto err_free; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 61 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 62 | src = obj->mm.pages->sgl; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 63 | dst = st->sgl; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 64 | for (i = 0; i < obj->mm.pages->nents; i++) { |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 65 | sg_set_page(dst, sg_page(src), src->length, 0); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 66 | dst = sg_next(dst); |
| 67 | src = sg_next(src); |
| 68 | } |
| 69 | |
| 70 | if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) { |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 71 | ret = -ENOMEM; |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 72 | goto err_free_sg; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 75 | return st; |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 76 | |
| 77 | err_free_sg: |
| 78 | sg_free_table(st); |
| 79 | err_free: |
| 80 | kfree(st); |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 81 | err_unpin_pages: |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 82 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 5cfacde | 2013-08-26 19:50:55 -0300 | [diff] [blame] | 83 | err: |
| 84 | return ERR_PTR(ret); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 87 | static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 88 | struct sg_table *sg, |
| 89 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 90 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 91 | struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 92 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 93 | dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir); |
| 94 | sg_free_table(sg); |
| 95 | kfree(sg); |
Daniel Vetter | f214266 | 2013-08-08 09:10:37 +0200 | [diff] [blame] | 96 | |
| 97 | i915_gem_object_unpin_pages(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 100 | static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf) |
| 101 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 102 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 103 | |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 104 | return i915_gem_object_pin_map(obj, I915_MAP_WB); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr) |
| 108 | { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 109 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 110 | |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 111 | i915_gem_object_unpin_map(obj); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 114 | static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num) |
| 115 | { |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 120 | { |
| 121 | |
| 122 | } |
| 123 | static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num) |
| 124 | { |
Chris Wilson | c944a30 | 2017-05-03 21:25:17 +0100 | [diff] [blame] | 125 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
| 126 | struct page *page; |
| 127 | |
| 128 | if (page_num >= obj->base.size >> PAGE_SHIFT) |
| 129 | return NULL; |
| 130 | |
| 131 | if (!i915_gem_object_has_struct_page(obj)) |
| 132 | return NULL; |
| 133 | |
| 134 | if (i915_gem_object_pin_pages(obj)) |
| 135 | return NULL; |
| 136 | |
| 137 | /* Synchronisation is left to the caller (via .begin_cpu_access()) */ |
| 138 | page = i915_gem_object_get_page(obj, page_num); |
| 139 | if (IS_ERR(page)) |
| 140 | goto err_unpin; |
| 141 | |
| 142 | return kmap(page); |
| 143 | |
| 144 | err_unpin: |
| 145 | i915_gem_object_unpin_pages(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 150 | { |
Chris Wilson | c944a30 | 2017-05-03 21:25:17 +0100 | [diff] [blame] | 151 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 152 | |
Chris Wilson | c944a30 | 2017-05-03 21:25:17 +0100 | [diff] [blame] | 153 | kunmap(virt_to_page(addr)); |
| 154 | i915_gem_object_unpin_pages(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 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 | |
Miklos Szeredi | f74ac01 | 2017-02-20 16:51:23 +0100 | [diff] [blame] | 168 | ret = call_mmap(obj->base.filp, vma); |
Tiago Vignatti | 2dbf0d9 | 2015-12-22 19:36:48 -0200 | [diff] [blame] | 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; |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 182 | bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE); |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 183 | int err; |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 184 | |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 185 | err = i915_gem_object_pin_pages(obj); |
| 186 | if (err) |
| 187 | return err; |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 188 | |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 189 | err = i915_mutex_lock_interruptible(dev); |
| 190 | if (err) |
| 191 | goto out; |
| 192 | |
| 193 | err = i915_gem_object_set_to_cpu_domain(obj, write); |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 194 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 195 | |
| 196 | out: |
| 197 | i915_gem_object_unpin_pages(obj); |
| 198 | return err; |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 201 | 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] | 202 | { |
| 203 | struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf); |
| 204 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 205 | int err; |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 206 | |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 207 | err = i915_gem_object_pin_pages(obj); |
| 208 | if (err) |
| 209 | return err; |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 210 | |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 211 | err = i915_mutex_lock_interruptible(dev); |
| 212 | if (err) |
| 213 | goto out; |
| 214 | |
| 215 | err = i915_gem_object_set_to_gtt_domain(obj, false); |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 216 | mutex_unlock(&dev->struct_mutex); |
| 217 | |
Chris Wilson | 7dd737f | 2016-10-28 13:58:38 +0100 | [diff] [blame] | 218 | out: |
| 219 | i915_gem_object_unpin_pages(obj); |
| 220 | return err; |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 221 | } |
| 222 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 223 | static const struct dma_buf_ops i915_dmabuf_ops = { |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 224 | .map_dma_buf = i915_gem_map_dma_buf, |
| 225 | .unmap_dma_buf = i915_gem_unmap_dma_buf, |
Daniel Vetter | c1d6798 | 2013-08-15 00:02:30 +0200 | [diff] [blame] | 226 | .release = drm_gem_dmabuf_release, |
Logan Gunthorpe | f9b67f0 | 2017-04-19 13:36:10 -0600 | [diff] [blame] | 227 | .map = i915_gem_dmabuf_kmap, |
| 228 | .map_atomic = i915_gem_dmabuf_kmap_atomic, |
| 229 | .unmap = i915_gem_dmabuf_kunmap, |
| 230 | .unmap_atomic = i915_gem_dmabuf_kunmap_atomic, |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 231 | .mmap = i915_gem_dmabuf_mmap, |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 232 | .vmap = i915_gem_dmabuf_vmap, |
| 233 | .vunmap = i915_gem_dmabuf_vunmap, |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 234 | .begin_cpu_access = i915_gem_begin_cpu_access, |
Tiago Vignatti | 346400c | 2015-12-22 19:36:47 -0200 | [diff] [blame] | 235 | .end_cpu_access = i915_gem_end_cpu_access, |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | struct dma_buf *i915_gem_prime_export(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 239 | struct drm_gem_object *gem_obj, int flags) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 240 | { |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 241 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 242 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 243 | |
| 244 | exp_info.ops = &i915_dmabuf_ops; |
| 245 | exp_info.size = gem_obj->size; |
| 246 | exp_info.flags = flags; |
| 247 | exp_info.priv = gem_obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 248 | exp_info.resv = obj->resv; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 249 | |
Chris Wilson | 5cc9ed4 | 2014-05-16 14:22:37 +0100 | [diff] [blame] | 250 | if (obj->ops->dmabuf_export) { |
| 251 | int ret = obj->ops->dmabuf_export(obj); |
| 252 | if (ret) |
| 253 | return ERR_PTR(ret); |
| 254 | } |
| 255 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 256 | return drm_gem_dmabuf_export(dev, &exp_info); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 259 | static struct sg_table * |
| 260 | i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj) |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 261 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 262 | return dma_buf_map_attachment(obj->base.import_attach, |
| 263 | DMA_BIDIRECTIONAL); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 266 | static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj, |
| 267 | struct sg_table *pages) |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 268 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 269 | dma_buf_unmap_attachment(obj->base.import_attach, pages, |
| 270 | DMA_BIDIRECTIONAL); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = { |
| 274 | .get_pages = i915_gem_object_get_pages_dmabuf, |
| 275 | .put_pages = i915_gem_object_put_pages_dmabuf, |
| 276 | }; |
| 277 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 278 | struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 279 | struct dma_buf *dma_buf) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 280 | { |
| 281 | struct dma_buf_attachment *attach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 282 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 283 | int ret; |
| 284 | |
| 285 | /* is this one of own objects? */ |
| 286 | if (dma_buf->ops == &i915_dmabuf_ops) { |
Daniel Vetter | 608806a | 2013-08-08 09:10:38 +0200 | [diff] [blame] | 287 | obj = dma_buf_to_obj(dma_buf); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 288 | /* is it from our device? */ |
| 289 | if (obj->base.dev == dev) { |
Seung-Woo Kim | be8a42a | 2012-09-27 15:30:06 +0900 | [diff] [blame] | 290 | /* |
| 291 | * Importing dmabuf exported from out own gem increases |
| 292 | * refcount on gem itself instead of f_count of dmabuf. |
| 293 | */ |
Chris Wilson | 25dc556 | 2016-07-20 13:31:52 +0100 | [diff] [blame] | 294 | return &i915_gem_object_get(obj)->base; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | /* need to attach */ |
| 299 | attach = dma_buf_attach(dma_buf, dev->dev); |
| 300 | if (IS_ERR(attach)) |
| 301 | return ERR_CAST(attach); |
| 302 | |
Imre Deak | 011c2282 | 2013-04-19 11:11:56 +1000 | [diff] [blame] | 303 | get_dma_buf(dma_buf); |
| 304 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 305 | obj = i915_gem_object_alloc(to_i915(dev)); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 306 | if (obj == NULL) { |
| 307 | ret = -ENOMEM; |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 308 | goto fail_detach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 309 | } |
| 310 | |
David Herrmann | 89c8233 | 2013-07-11 11:56:32 +0200 | [diff] [blame] | 311 | drm_gem_private_object_init(dev, &obj->base, dma_buf->size); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 312 | i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 313 | obj->base.import_attach = attach; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 314 | obj->resv = dma_buf->resv; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 315 | |
Chris Wilson | 30bc06c | 2016-07-20 09:21:14 +0100 | [diff] [blame] | 316 | /* We use GTT as shorthand for a coherent domain, one that is |
| 317 | * neither in the GPU cache nor in the CPU cache, where all |
| 318 | * writes are immediately visible in memory. (That's not strictly |
| 319 | * true, but it's close! There are internal buffers such as the |
| 320 | * write-combined buffer or a delay through the chipset for GTT |
| 321 | * writes that do require us to treat GTT as a separate cache domain.) |
| 322 | */ |
| 323 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
| 324 | obj->base.write_domain = 0; |
| 325 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 326 | return &obj->base; |
| 327 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 328 | fail_detach: |
| 329 | dma_buf_detach(dma_buf, attach); |
Imre Deak | 011c2282 | 2013-04-19 11:11:56 +1000 | [diff] [blame] | 330 | dma_buf_put(dma_buf); |
| 331 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 332 | return ERR_PTR(ret); |
| 333 | } |
Chris Wilson | 6cca22e | 2017-02-13 17:15:36 +0000 | [diff] [blame] | 334 | |
| 335 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 336 | #include "selftests/mock_dmabuf.c" |
| 337 | #include "selftests/i915_gem_dmabuf.c" |
| 338 | #endif |