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 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 30 | 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] | 31 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 32 | { |
| 33 | struct drm_i915_gem_object *obj = attachment->dmabuf->priv; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 34 | struct sg_table *st; |
| 35 | struct scatterlist *src, *dst; |
| 36 | int ret, i; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 37 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 38 | ret = i915_mutex_lock_interruptible(obj->base.dev); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 39 | if (ret) |
| 40 | return ERR_PTR(ret); |
| 41 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 42 | ret = i915_gem_object_get_pages(obj); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 43 | if (ret) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 44 | st = ERR_PTR(ret); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 45 | goto out; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 48 | /* Copy sg so that we make an independent mapping */ |
| 49 | st = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 50 | if (st == NULL) { |
| 51 | st = ERR_PTR(-ENOMEM); |
| 52 | goto out; |
| 53 | } |
| 54 | |
| 55 | ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL); |
| 56 | if (ret) { |
| 57 | kfree(st); |
| 58 | st = ERR_PTR(ret); |
| 59 | goto out; |
| 60 | } |
| 61 | |
| 62 | src = obj->pages->sgl; |
| 63 | dst = st->sgl; |
| 64 | for (i = 0; i < obj->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)) { |
| 71 | sg_free_table(st); |
| 72 | kfree(st); |
| 73 | st = ERR_PTR(-ENOMEM); |
| 74 | goto out; |
| 75 | } |
| 76 | |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 77 | i915_gem_object_pin_pages(obj); |
| 78 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 79 | out: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 80 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 81 | return st; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 84 | static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 85 | struct sg_table *sg, |
| 86 | enum dma_data_direction dir) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 87 | { |
| 88 | dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir); |
| 89 | sg_free_table(sg); |
| 90 | kfree(sg); |
| 91 | } |
| 92 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 93 | static void i915_gem_dmabuf_release(struct dma_buf *dma_buf) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 94 | { |
| 95 | struct drm_i915_gem_object *obj = dma_buf->priv; |
| 96 | |
| 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); |
| 101 | } |
| 102 | } |
| 103 | |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 104 | static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf) |
| 105 | { |
| 106 | struct drm_i915_gem_object *obj = dma_buf->priv; |
| 107 | struct drm_device *dev = obj->base.dev; |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 108 | struct sg_page_iter sg_iter; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 109 | struct page **pages; |
| 110 | int ret, i; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 111 | |
| 112 | ret = i915_mutex_lock_interruptible(dev); |
| 113 | if (ret) |
| 114 | return ERR_PTR(ret); |
| 115 | |
| 116 | if (obj->dma_buf_vmapping) { |
| 117 | obj->vmapping_count++; |
| 118 | goto out_unlock; |
| 119 | } |
| 120 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 121 | ret = i915_gem_object_get_pages(obj); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 122 | if (ret) |
| 123 | goto error; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 124 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 125 | ret = -ENOMEM; |
| 126 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 127 | pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages)); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 128 | if (pages == NULL) |
| 129 | goto error; |
| 130 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 131 | i = 0; |
| 132 | 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] | 133 | pages[i++] = sg_page_iter_page(&sg_iter); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 134 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 135 | obj->dma_buf_vmapping = vmap(pages, i, 0, PAGE_KERNEL); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 136 | drm_free_large(pages); |
| 137 | |
| 138 | if (!obj->dma_buf_vmapping) |
| 139 | goto error; |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 140 | |
| 141 | obj->vmapping_count = 1; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 142 | i915_gem_object_pin_pages(obj); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 143 | out_unlock: |
| 144 | mutex_unlock(&dev->struct_mutex); |
| 145 | return obj->dma_buf_vmapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 146 | |
| 147 | error: |
| 148 | mutex_unlock(&dev->struct_mutex); |
| 149 | return ERR_PTR(ret); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr) |
| 153 | { |
| 154 | struct drm_i915_gem_object *obj = dma_buf->priv; |
| 155 | struct drm_device *dev = obj->base.dev; |
| 156 | int ret; |
| 157 | |
| 158 | ret = i915_mutex_lock_interruptible(dev); |
| 159 | if (ret) |
| 160 | return; |
| 161 | |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 162 | if (--obj->vmapping_count == 0) { |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 163 | vunmap(obj->dma_buf_vmapping); |
| 164 | obj->dma_buf_vmapping = NULL; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 165 | |
| 166 | i915_gem_object_unpin_pages(obj); |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 167 | } |
| 168 | mutex_unlock(&dev->struct_mutex); |
| 169 | } |
| 170 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 171 | static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num) |
| 172 | { |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 177 | { |
| 178 | |
| 179 | } |
| 180 | static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num) |
| 181 | { |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 186 | { |
| 187 | |
| 188 | } |
| 189 | |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 190 | static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) |
| 191 | { |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 195 | static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction) |
| 196 | { |
| 197 | struct drm_i915_gem_object *obj = dma_buf->priv; |
| 198 | struct drm_device *dev = obj->base.dev; |
| 199 | int ret; |
| 200 | bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE); |
| 201 | |
| 202 | ret = i915_mutex_lock_interruptible(dev); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | |
| 206 | ret = i915_gem_object_set_to_cpu_domain(obj, write); |
| 207 | mutex_unlock(&dev->struct_mutex); |
| 208 | return ret; |
| 209 | } |
| 210 | |
Dave Airlie | 6a101cb | 2012-05-23 14:09:32 +0100 | [diff] [blame] | 211 | static const struct dma_buf_ops i915_dmabuf_ops = { |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 212 | .map_dma_buf = i915_gem_map_dma_buf, |
| 213 | .unmap_dma_buf = i915_gem_unmap_dma_buf, |
| 214 | .release = i915_gem_dmabuf_release, |
| 215 | .kmap = i915_gem_dmabuf_kmap, |
| 216 | .kmap_atomic = i915_gem_dmabuf_kmap_atomic, |
| 217 | .kunmap = i915_gem_dmabuf_kunmap, |
| 218 | .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic, |
Dave Airlie | 2dad9d4 | 2012-05-29 15:11:22 +0100 | [diff] [blame] | 219 | .mmap = i915_gem_dmabuf_mmap, |
Dave Airlie | 9a70cc2 | 2012-05-22 13:09:21 +0100 | [diff] [blame] | 220 | .vmap = i915_gem_dmabuf_vmap, |
| 221 | .vunmap = i915_gem_dmabuf_vunmap, |
Dave Airlie | ec6f1bb | 2012-08-16 10:15:34 +1000 | [diff] [blame] | 222 | .begin_cpu_access = i915_gem_begin_cpu_access, |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | struct dma_buf *i915_gem_prime_export(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 226 | struct drm_gem_object *gem_obj, int flags) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 227 | { |
| 228 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 229 | |
Dave Airlie | 5b42427 | 2012-12-20 10:51:09 +1000 | [diff] [blame] | 230 | return dma_buf_export(obj, &i915_dmabuf_ops, obj->base.size, flags); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 233 | static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj) |
| 234 | { |
| 235 | struct sg_table *sg; |
| 236 | |
| 237 | sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL); |
| 238 | if (IS_ERR(sg)) |
| 239 | return PTR_ERR(sg); |
| 240 | |
| 241 | obj->pages = sg; |
| 242 | obj->has_dma_mapping = true; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj) |
| 247 | { |
| 248 | dma_buf_unmap_attachment(obj->base.import_attach, |
| 249 | obj->pages, DMA_BIDIRECTIONAL); |
| 250 | obj->has_dma_mapping = false; |
| 251 | } |
| 252 | |
| 253 | static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = { |
| 254 | .get_pages = i915_gem_object_get_pages_dmabuf, |
| 255 | .put_pages = i915_gem_object_put_pages_dmabuf, |
| 256 | }; |
| 257 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 258 | struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 259 | struct dma_buf *dma_buf) |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 260 | { |
| 261 | struct dma_buf_attachment *attach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 262 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 263 | int ret; |
| 264 | |
| 265 | /* is this one of own objects? */ |
| 266 | if (dma_buf->ops == &i915_dmabuf_ops) { |
| 267 | obj = dma_buf->priv; |
| 268 | /* is it from our device? */ |
| 269 | if (obj->base.dev == dev) { |
Seung-Woo Kim | be8a42a | 2012-09-27 15:30:06 +0900 | [diff] [blame] | 270 | /* |
| 271 | * Importing dmabuf exported from out own gem increases |
| 272 | * refcount on gem itself instead of f_count of dmabuf. |
| 273 | */ |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 274 | drm_gem_object_reference(&obj->base); |
| 275 | return &obj->base; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* need to attach */ |
| 280 | attach = dma_buf_attach(dma_buf, dev->dev); |
| 281 | if (IS_ERR(attach)) |
| 282 | return ERR_CAST(attach); |
| 283 | |
Imre Deak | 011c228 | 2013-04-19 11:11:56 +1000 | [diff] [blame^] | 284 | get_dma_buf(dma_buf); |
| 285 | |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 286 | obj = i915_gem_object_alloc(dev); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 287 | if (obj == NULL) { |
| 288 | ret = -ENOMEM; |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 289 | goto fail_detach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 292 | ret = drm_gem_private_object_init(dev, &obj->base, dma_buf->size); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 293 | if (ret) { |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 294 | i915_gem_object_free(obj); |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 295 | goto fail_detach; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 298 | i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 299 | obj->base.import_attach = attach; |
| 300 | |
| 301 | return &obj->base; |
| 302 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 303 | fail_detach: |
| 304 | dma_buf_detach(dma_buf, attach); |
Imre Deak | 011c228 | 2013-04-19 11:11:56 +1000 | [diff] [blame^] | 305 | dma_buf_put(dma_buf); |
| 306 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 307 | return ERR_PTR(ret); |
| 308 | } |