blob: 80bbe43a2e92a5b363d46d784fda9accd6f8d3f9 [file] [log] [blame]
Daniel Vetter1286ff72012-05-10 15:25:09 +02001/*
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 Howells760285e2012-10-02 18:01:07 +010026#include <drm/drmP.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020027#include "i915_drv.h"
28#include <linux/dma-buf.h>
29
Daniel Vetter608806a2013-08-08 09:10:38 +020030static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
31{
32 return to_intel_bo(buf->priv);
33}
34
Dave Airlie6a101cb2012-05-23 14:09:32 +010035static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
Chris Wilson9da3da62012-06-01 15:20:22 +010036 enum dma_data_direction dir)
Daniel Vetter1286ff72012-05-10 15:25:09 +020037{
Daniel Vetter608806a2013-08-08 09:10:38 +020038 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
Chris Wilson9da3da62012-06-01 15:20:22 +010039 struct sg_table *st;
40 struct scatterlist *src, *dst;
41 int ret, i;
Daniel Vetter1286ff72012-05-10 15:25:09 +020042
Chris Wilson9da3da62012-06-01 15:20:22 +010043 ret = i915_mutex_lock_interruptible(obj->base.dev);
Daniel Vetter1286ff72012-05-10 15:25:09 +020044 if (ret)
Chris Wilson5cfacde2013-08-26 19:50:55 -030045 goto err;
Daniel Vetter1286ff72012-05-10 15:25:09 +020046
Chris Wilson37e680a2012-06-07 15:38:42 +010047 ret = i915_gem_object_get_pages(obj);
Chris Wilson5cfacde2013-08-26 19:50:55 -030048 if (ret)
49 goto err_unlock;
50
51 i915_gem_object_pin_pages(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +020052
Chris Wilson9da3da62012-06-01 15:20:22 +010053 /* Copy sg so that we make an independent mapping */
54 st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
55 if (st == NULL) {
Chris Wilson5cfacde2013-08-26 19:50:55 -030056 ret = -ENOMEM;
57 goto err_unpin;
Chris Wilson9da3da62012-06-01 15:20:22 +010058 }
59
60 ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
Chris Wilson5cfacde2013-08-26 19:50:55 -030061 if (ret)
62 goto err_free;
Chris Wilson9da3da62012-06-01 15:20:22 +010063
64 src = obj->pages->sgl;
65 dst = st->sgl;
66 for (i = 0; i < obj->pages->nents; i++) {
Imre Deak67d5a502013-02-18 19:28:02 +020067 sg_set_page(dst, sg_page(src), src->length, 0);
Chris Wilson9da3da62012-06-01 15:20:22 +010068 dst = sg_next(dst);
69 src = sg_next(src);
70 }
71
72 if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
Chris Wilson5cfacde2013-08-26 19:50:55 -030073 ret =-ENOMEM;
74 goto err_free_sg;
Chris Wilson9da3da62012-06-01 15:20:22 +010075 }
76
Chris Wilson9da3da62012-06-01 15:20:22 +010077 mutex_unlock(&obj->base.dev->struct_mutex);
78 return st;
Chris Wilson5cfacde2013-08-26 19:50:55 -030079
80err_free_sg:
81 sg_free_table(st);
82err_free:
83 kfree(st);
84err_unpin:
85 i915_gem_object_unpin_pages(obj);
86err_unlock:
87 mutex_unlock(&obj->base.dev->struct_mutex);
88err:
89 return ERR_PTR(ret);
Daniel Vetter1286ff72012-05-10 15:25:09 +020090}
91
Dave Airlie6a101cb2012-05-23 14:09:32 +010092static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
Chris Wilson2f745ad2012-09-04 21:02:58 +010093 struct sg_table *sg,
94 enum dma_data_direction dir)
Daniel Vetter1286ff72012-05-10 15:25:09 +020095{
Daniel Vetter608806a2013-08-08 09:10:38 +020096 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
Daniel Vetterf2142662013-08-08 09:10:37 +020097
Daniel Vetter1286ff72012-05-10 15:25:09 +020098 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
99 sg_free_table(sg);
100 kfree(sg);
Daniel Vetterf2142662013-08-08 09:10:37 +0200101
Chris Wilson6d192452016-04-08 12:11:09 +0100102 mutex_lock(&obj->base.dev->struct_mutex);
Daniel Vetterf2142662013-08-08 09:10:37 +0200103 i915_gem_object_unpin_pages(obj);
Daniel Vetterf2142662013-08-08 09:10:37 +0200104 mutex_unlock(&obj->base.dev->struct_mutex);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200105}
106
Dave Airlie9a70cc22012-05-22 13:09:21 +0100107static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
108{
Daniel Vetter608806a2013-08-08 09:10:38 +0200109 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100110 struct drm_device *dev = obj->base.dev;
Chris Wilson0a798eb2016-04-08 12:11:11 +0100111 void *addr;
112 int ret;
Dave Airlie9a70cc22012-05-22 13:09:21 +0100113
114 ret = i915_mutex_lock_interruptible(dev);
115 if (ret)
116 return ERR_PTR(ret);
117
Chris Wilson0a798eb2016-04-08 12:11:11 +0100118 addr = i915_gem_object_pin_map(obj);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100119 mutex_unlock(&dev->struct_mutex);
Chris Wilson9da3da62012-06-01 15:20:22 +0100120
Chris Wilson0a798eb2016-04-08 12:11:11 +0100121 return addr;
Dave Airlie9a70cc22012-05-22 13:09:21 +0100122}
123
124static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
125{
Daniel Vetter608806a2013-08-08 09:10:38 +0200126 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100127 struct drm_device *dev = obj->base.dev;
Dave Airlie9a70cc22012-05-22 13:09:21 +0100128
Chris Wilsonce7ec7682014-04-07 17:01:47 -0300129 mutex_lock(&dev->struct_mutex);
Chris Wilson0a798eb2016-04-08 12:11:11 +0100130 i915_gem_object_unpin_map(obj);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100131 mutex_unlock(&dev->struct_mutex);
132}
133
Daniel Vetter1286ff72012-05-10 15:25:09 +0200134static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
135{
136 return NULL;
137}
138
139static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
140{
141
142}
143static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
144{
145 return NULL;
146}
147
148static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
149{
150
151}
152
Dave Airlie2dad9d42012-05-29 15:11:22 +0100153static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
154{
Tiago Vignatti2dbf0d92015-12-22 19:36:48 -0200155 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
156 int ret;
157
158 if (obj->base.size < vma->vm_end - vma->vm_start)
159 return -EINVAL;
160
161 if (!obj->base.filp)
162 return -ENODEV;
163
164 ret = obj->base.filp->f_op->mmap(obj->base.filp, vma);
165 if (ret)
166 return ret;
167
168 fput(vma->vm_file);
169 vma->vm_file = get_file(obj->base.filp);
170
171 return 0;
Dave Airlie2dad9d42012-05-29 15:11:22 +0100172}
173
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200174static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000175{
Daniel Vetter608806a2013-08-08 09:10:38 +0200176 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000177 struct drm_device *dev = obj->base.dev;
178 int ret;
179 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
180
181 ret = i915_mutex_lock_interruptible(dev);
182 if (ret)
183 return ret;
184
185 ret = i915_gem_object_set_to_cpu_domain(obj, write);
186 mutex_unlock(&dev->struct_mutex);
187 return ret;
188}
189
Chris Wilson18b862d2016-03-18 20:02:39 +0000190static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
Tiago Vignatti346400c2015-12-22 19:36:47 -0200191{
192 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
193 struct drm_device *dev = obj->base.dev;
Tiago Vignatti346400c2015-12-22 19:36:47 -0200194 int ret;
195
Chris Wilson18b862d2016-03-18 20:02:39 +0000196 ret = i915_mutex_lock_interruptible(dev);
197 if (ret)
198 return ret;
Tiago Vignatti346400c2015-12-22 19:36:47 -0200199
200 ret = i915_gem_object_set_to_gtt_domain(obj, false);
Tiago Vignatti346400c2015-12-22 19:36:47 -0200201 mutex_unlock(&dev->struct_mutex);
202
Chris Wilson18b862d2016-03-18 20:02:39 +0000203 return ret;
Tiago Vignatti346400c2015-12-22 19:36:47 -0200204}
205
Dave Airlie6a101cb2012-05-23 14:09:32 +0100206static const struct dma_buf_ops i915_dmabuf_ops = {
Daniel Vetter1286ff72012-05-10 15:25:09 +0200207 .map_dma_buf = i915_gem_map_dma_buf,
208 .unmap_dma_buf = i915_gem_unmap_dma_buf,
Daniel Vetterc1d67982013-08-15 00:02:30 +0200209 .release = drm_gem_dmabuf_release,
Daniel Vetter1286ff72012-05-10 15:25:09 +0200210 .kmap = i915_gem_dmabuf_kmap,
211 .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
212 .kunmap = i915_gem_dmabuf_kunmap,
213 .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
Dave Airlie2dad9d42012-05-29 15:11:22 +0100214 .mmap = i915_gem_dmabuf_mmap,
Dave Airlie9a70cc22012-05-22 13:09:21 +0100215 .vmap = i915_gem_dmabuf_vmap,
216 .vunmap = i915_gem_dmabuf_vunmap,
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000217 .begin_cpu_access = i915_gem_begin_cpu_access,
Tiago Vignatti346400c2015-12-22 19:36:47 -0200218 .end_cpu_access = i915_gem_end_cpu_access,
Daniel Vetter1286ff72012-05-10 15:25:09 +0200219};
220
221struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
Chris Wilson9da3da62012-06-01 15:20:22 +0100222 struct drm_gem_object *gem_obj, int flags)
Daniel Vetter1286ff72012-05-10 15:25:09 +0200223{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100224 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Sumit Semwald8fbe342015-01-23 12:53:43 +0530225 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
226
227 exp_info.ops = &i915_dmabuf_ops;
228 exp_info.size = gem_obj->size;
229 exp_info.flags = flags;
230 exp_info.priv = gem_obj;
231
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100232
233 if (obj->ops->dmabuf_export) {
234 int ret = obj->ops->dmabuf_export(obj);
235 if (ret)
236 return ERR_PTR(ret);
237 }
238
Sumit Semwald8fbe342015-01-23 12:53:43 +0530239 return dma_buf_export(&exp_info);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200240}
241
Chris Wilson2f745ad2012-09-04 21:02:58 +0100242static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
243{
244 struct sg_table *sg;
245
246 sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL);
247 if (IS_ERR(sg))
248 return PTR_ERR(sg);
249
250 obj->pages = sg;
Chris Wilson2f745ad2012-09-04 21:02:58 +0100251 return 0;
252}
253
254static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj)
255{
256 dma_buf_unmap_attachment(obj->base.import_attach,
257 obj->pages, DMA_BIDIRECTIONAL);
Chris Wilson2f745ad2012-09-04 21:02:58 +0100258}
259
260static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
261 .get_pages = i915_gem_object_get_pages_dmabuf,
262 .put_pages = i915_gem_object_put_pages_dmabuf,
263};
264
Daniel Vetter1286ff72012-05-10 15:25:09 +0200265struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
Chris Wilson9da3da62012-06-01 15:20:22 +0100266 struct dma_buf *dma_buf)
Daniel Vetter1286ff72012-05-10 15:25:09 +0200267{
268 struct dma_buf_attachment *attach;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200269 struct drm_i915_gem_object *obj;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200270 int ret;
271
272 /* is this one of own objects? */
273 if (dma_buf->ops == &i915_dmabuf_ops) {
Daniel Vetter608806a2013-08-08 09:10:38 +0200274 obj = dma_buf_to_obj(dma_buf);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200275 /* is it from our device? */
276 if (obj->base.dev == dev) {
Seung-Woo Kimbe8a42a2012-09-27 15:30:06 +0900277 /*
278 * Importing dmabuf exported from out own gem increases
279 * refcount on gem itself instead of f_count of dmabuf.
280 */
Daniel Vetter1286ff72012-05-10 15:25:09 +0200281 drm_gem_object_reference(&obj->base);
282 return &obj->base;
283 }
284 }
285
286 /* need to attach */
287 attach = dma_buf_attach(dma_buf, dev->dev);
288 if (IS_ERR(attach))
289 return ERR_CAST(attach);
290
Imre Deak011c2282013-04-19 11:11:56 +1000291 get_dma_buf(dma_buf);
292
Chris Wilson42dcedd2012-11-15 11:32:30 +0000293 obj = i915_gem_object_alloc(dev);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200294 if (obj == NULL) {
295 ret = -ENOMEM;
Chris Wilson2f745ad2012-09-04 21:02:58 +0100296 goto fail_detach;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200297 }
298
David Herrmann89c82332013-07-11 11:56:32 +0200299 drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
Chris Wilson2f745ad2012-09-04 21:02:58 +0100300 i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200301 obj->base.import_attach = attach;
302
303 return &obj->base;
304
Daniel Vetter1286ff72012-05-10 15:25:09 +0200305fail_detach:
306 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000307 dma_buf_put(dma_buf);
308
Daniel Vetter1286ff72012-05-10 15:25:09 +0200309 return ERR_PTR(ret);
310}