blob: f225bf680b6de190ce1cbb4aa0e67a5b3d5f603a [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 */
Chris Wilsonad778f82016-08-04 16:32:42 +010026
Daniel Vetter1286ff72012-05-10 15:25:09 +020027#include <linux/dma-buf.h>
Chris Wilsonad778f82016-08-04 16:32:42 +010028#include <linux/reservation.h>
29
30#include <drm/drmP.h>
31
32#include "i915_drv.h"
Daniel Vetter1286ff72012-05-10 15:25:09 +020033
Daniel Vetter608806a2013-08-08 09:10:38 +020034static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
35{
36 return to_intel_bo(buf->priv);
37}
38
Dave Airlie6a101cb2012-05-23 14:09:32 +010039static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
Chris Wilson9da3da62012-06-01 15:20:22 +010040 enum dma_data_direction dir)
Daniel Vetter1286ff72012-05-10 15:25:09 +020041{
Daniel Vetter608806a2013-08-08 09:10:38 +020042 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
Chris Wilson9da3da62012-06-01 15:20:22 +010043 struct sg_table *st;
44 struct scatterlist *src, *dst;
45 int ret, i;
Daniel Vetter1286ff72012-05-10 15:25:09 +020046
Chris Wilsona4f5ea62016-10-28 13:58:35 +010047 ret = i915_gem_object_pin_pages(obj);
Chris Wilson5cfacde2013-08-26 19:50:55 -030048 if (ret)
Chris Wilson7dd737f2016-10-28 13:58:38 +010049 goto err;
Chris Wilson5cfacde2013-08-26 19:50:55 -030050
Chris Wilson9da3da62012-06-01 15:20:22 +010051 /* Copy sg so that we make an independent mapping */
52 st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
53 if (st == NULL) {
Chris Wilson5cfacde2013-08-26 19:50:55 -030054 ret = -ENOMEM;
Chris Wilson7dd737f2016-10-28 13:58:38 +010055 goto err_unpin_pages;
Chris Wilson9da3da62012-06-01 15:20:22 +010056 }
57
Chris Wilsona4f5ea62016-10-28 13:58:35 +010058 ret = sg_alloc_table(st, obj->mm.pages->nents, GFP_KERNEL);
Chris Wilson5cfacde2013-08-26 19:50:55 -030059 if (ret)
60 goto err_free;
Chris Wilson9da3da62012-06-01 15:20:22 +010061
Chris Wilsona4f5ea62016-10-28 13:58:35 +010062 src = obj->mm.pages->sgl;
Chris Wilson9da3da62012-06-01 15:20:22 +010063 dst = st->sgl;
Chris Wilsona4f5ea62016-10-28 13:58:35 +010064 for (i = 0; i < obj->mm.pages->nents; i++) {
Imre Deak67d5a502013-02-18 19:28:02 +020065 sg_set_page(dst, sg_page(src), src->length, 0);
Chris Wilson9da3da62012-06-01 15:20:22 +010066 dst = sg_next(dst);
67 src = sg_next(src);
68 }
69
70 if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
Chris Wilson7dd737f2016-10-28 13:58:38 +010071 ret = -ENOMEM;
Chris Wilson5cfacde2013-08-26 19:50:55 -030072 goto err_free_sg;
Chris Wilson9da3da62012-06-01 15:20:22 +010073 }
74
Chris Wilson9da3da62012-06-01 15:20:22 +010075 return st;
Chris Wilson5cfacde2013-08-26 19:50:55 -030076
77err_free_sg:
78 sg_free_table(st);
79err_free:
80 kfree(st);
Chris Wilson7dd737f2016-10-28 13:58:38 +010081err_unpin_pages:
Chris Wilson5cfacde2013-08-26 19:50:55 -030082 i915_gem_object_unpin_pages(obj);
Chris Wilson5cfacde2013-08-26 19:50:55 -030083err:
84 return ERR_PTR(ret);
Daniel Vetter1286ff72012-05-10 15:25:09 +020085}
86
Dave Airlie6a101cb2012-05-23 14:09:32 +010087static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
Chris Wilson2f745ad2012-09-04 21:02:58 +010088 struct sg_table *sg,
89 enum dma_data_direction dir)
Daniel Vetter1286ff72012-05-10 15:25:09 +020090{
Daniel Vetter608806a2013-08-08 09:10:38 +020091 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
Daniel Vetterf2142662013-08-08 09:10:37 +020092
Daniel Vetter1286ff72012-05-10 15:25:09 +020093 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
94 sg_free_table(sg);
95 kfree(sg);
Daniel Vetterf2142662013-08-08 09:10:37 +020096
97 i915_gem_object_unpin_pages(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +020098}
99
Dave Airlie9a70cc22012-05-22 13:09:21 +0100100static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
101{
Daniel Vetter608806a2013-08-08 09:10:38 +0200102 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100103
Chris Wilson7dd737f2016-10-28 13:58:38 +0100104 return i915_gem_object_pin_map(obj, I915_MAP_WB);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100105}
106
107static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
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
Chris Wilson0a798eb2016-04-08 12:11:11 +0100111 i915_gem_object_unpin_map(obj);
Dave Airlie9a70cc22012-05-22 13:09:21 +0100112}
113
Daniel Vetter1286ff72012-05-10 15:25:09 +0200114static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
115{
116 return NULL;
117}
118
119static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
120{
121
122}
123static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
124{
125 return NULL;
126}
127
128static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
129{
130
131}
132
Dave Airlie2dad9d42012-05-29 15:11:22 +0100133static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
134{
Tiago Vignatti2dbf0d92015-12-22 19:36:48 -0200135 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
136 int ret;
137
138 if (obj->base.size < vma->vm_end - vma->vm_start)
139 return -EINVAL;
140
141 if (!obj->base.filp)
142 return -ENODEV;
143
Miklos Szeredif74ac012017-02-20 16:51:23 +0100144 ret = call_mmap(obj->base.filp, vma);
Tiago Vignatti2dbf0d92015-12-22 19:36:48 -0200145 if (ret)
146 return ret;
147
148 fput(vma->vm_file);
149 vma->vm_file = get_file(obj->base.filp);
150
151 return 0;
Dave Airlie2dad9d42012-05-29 15:11:22 +0100152}
153
Tiago Vignatti831e9da2015-12-22 19:36:45 -0200154static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000155{
Daniel Vetter608806a2013-08-08 09:10:38 +0200156 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000157 struct drm_device *dev = obj->base.dev;
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000158 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
Chris Wilson7dd737f2016-10-28 13:58:38 +0100159 int err;
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000160
Chris Wilson7dd737f2016-10-28 13:58:38 +0100161 err = i915_gem_object_pin_pages(obj);
162 if (err)
163 return err;
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000164
Chris Wilson7dd737f2016-10-28 13:58:38 +0100165 err = i915_mutex_lock_interruptible(dev);
166 if (err)
167 goto out;
168
169 err = i915_gem_object_set_to_cpu_domain(obj, write);
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000170 mutex_unlock(&dev->struct_mutex);
Chris Wilson7dd737f2016-10-28 13:58:38 +0100171
172out:
173 i915_gem_object_unpin_pages(obj);
174 return err;
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000175}
176
Chris Wilson18b862d2016-03-18 20:02:39 +0000177static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
Tiago Vignatti346400c2015-12-22 19:36:47 -0200178{
179 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
180 struct drm_device *dev = obj->base.dev;
Chris Wilson7dd737f2016-10-28 13:58:38 +0100181 int err;
Tiago Vignatti346400c2015-12-22 19:36:47 -0200182
Chris Wilson7dd737f2016-10-28 13:58:38 +0100183 err = i915_gem_object_pin_pages(obj);
184 if (err)
185 return err;
Tiago Vignatti346400c2015-12-22 19:36:47 -0200186
Chris Wilson7dd737f2016-10-28 13:58:38 +0100187 err = i915_mutex_lock_interruptible(dev);
188 if (err)
189 goto out;
190
191 err = i915_gem_object_set_to_gtt_domain(obj, false);
Tiago Vignatti346400c2015-12-22 19:36:47 -0200192 mutex_unlock(&dev->struct_mutex);
193
Chris Wilson7dd737f2016-10-28 13:58:38 +0100194out:
195 i915_gem_object_unpin_pages(obj);
196 return err;
Tiago Vignatti346400c2015-12-22 19:36:47 -0200197}
198
Dave Airlie6a101cb2012-05-23 14:09:32 +0100199static const struct dma_buf_ops i915_dmabuf_ops = {
Daniel Vetter1286ff72012-05-10 15:25:09 +0200200 .map_dma_buf = i915_gem_map_dma_buf,
201 .unmap_dma_buf = i915_gem_unmap_dma_buf,
Daniel Vetterc1d67982013-08-15 00:02:30 +0200202 .release = drm_gem_dmabuf_release,
Logan Gunthorpef9b67f02017-04-19 13:36:10 -0600203 .map = i915_gem_dmabuf_kmap,
204 .map_atomic = i915_gem_dmabuf_kmap_atomic,
205 .unmap = i915_gem_dmabuf_kunmap,
206 .unmap_atomic = i915_gem_dmabuf_kunmap_atomic,
Dave Airlie2dad9d42012-05-29 15:11:22 +0100207 .mmap = i915_gem_dmabuf_mmap,
Dave Airlie9a70cc22012-05-22 13:09:21 +0100208 .vmap = i915_gem_dmabuf_vmap,
209 .vunmap = i915_gem_dmabuf_vunmap,
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000210 .begin_cpu_access = i915_gem_begin_cpu_access,
Tiago Vignatti346400c2015-12-22 19:36:47 -0200211 .end_cpu_access = i915_gem_end_cpu_access,
Daniel Vetter1286ff72012-05-10 15:25:09 +0200212};
213
214struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
Chris Wilson9da3da62012-06-01 15:20:22 +0100215 struct drm_gem_object *gem_obj, int flags)
Daniel Vetter1286ff72012-05-10 15:25:09 +0200216{
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100217 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Sumit Semwald8fbe342015-01-23 12:53:43 +0530218 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
219
220 exp_info.ops = &i915_dmabuf_ops;
221 exp_info.size = gem_obj->size;
222 exp_info.flags = flags;
223 exp_info.priv = gem_obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100224 exp_info.resv = obj->resv;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530225
Chris Wilson5cc9ed42014-05-16 14:22:37 +0100226 if (obj->ops->dmabuf_export) {
227 int ret = obj->ops->dmabuf_export(obj);
228 if (ret)
229 return ERR_PTR(ret);
230 }
231
Chris Wilsond07f0e52016-10-28 13:58:44 +0100232 return drm_gem_dmabuf_export(dev, &exp_info);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200233}
234
Chris Wilson03ac84f2016-10-28 13:58:36 +0100235static struct sg_table *
236i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
Chris Wilson2f745ad2012-09-04 21:02:58 +0100237{
Chris Wilson03ac84f2016-10-28 13:58:36 +0100238 return dma_buf_map_attachment(obj->base.import_attach,
239 DMA_BIDIRECTIONAL);
Chris Wilson2f745ad2012-09-04 21:02:58 +0100240}
241
Chris Wilson03ac84f2016-10-28 13:58:36 +0100242static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj,
243 struct sg_table *pages)
Chris Wilson2f745ad2012-09-04 21:02:58 +0100244{
Chris Wilson03ac84f2016-10-28 13:58:36 +0100245 dma_buf_unmap_attachment(obj->base.import_attach, pages,
246 DMA_BIDIRECTIONAL);
Chris Wilson2f745ad2012-09-04 21:02:58 +0100247}
248
249static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
250 .get_pages = i915_gem_object_get_pages_dmabuf,
251 .put_pages = i915_gem_object_put_pages_dmabuf,
252};
253
Daniel Vetter1286ff72012-05-10 15:25:09 +0200254struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
Chris Wilson9da3da62012-06-01 15:20:22 +0100255 struct dma_buf *dma_buf)
Daniel Vetter1286ff72012-05-10 15:25:09 +0200256{
257 struct dma_buf_attachment *attach;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200258 struct drm_i915_gem_object *obj;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200259 int ret;
260
261 /* is this one of own objects? */
262 if (dma_buf->ops == &i915_dmabuf_ops) {
Daniel Vetter608806a2013-08-08 09:10:38 +0200263 obj = dma_buf_to_obj(dma_buf);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200264 /* is it from our device? */
265 if (obj->base.dev == dev) {
Seung-Woo Kimbe8a42a2012-09-27 15:30:06 +0900266 /*
267 * Importing dmabuf exported from out own gem increases
268 * refcount on gem itself instead of f_count of dmabuf.
269 */
Chris Wilson25dc5562016-07-20 13:31:52 +0100270 return &i915_gem_object_get(obj)->base;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200271 }
272 }
273
274 /* need to attach */
275 attach = dma_buf_attach(dma_buf, dev->dev);
276 if (IS_ERR(attach))
277 return ERR_CAST(attach);
278
Imre Deak011c22822013-04-19 11:11:56 +1000279 get_dma_buf(dma_buf);
280
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000281 obj = i915_gem_object_alloc(to_i915(dev));
Daniel Vetter1286ff72012-05-10 15:25:09 +0200282 if (obj == NULL) {
283 ret = -ENOMEM;
Chris Wilson2f745ad2012-09-04 21:02:58 +0100284 goto fail_detach;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200285 }
286
David Herrmann89c82332013-07-11 11:56:32 +0200287 drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
Chris Wilson2f745ad2012-09-04 21:02:58 +0100288 i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
Daniel Vetter1286ff72012-05-10 15:25:09 +0200289 obj->base.import_attach = attach;
Chris Wilsond07f0e52016-10-28 13:58:44 +0100290 obj->resv = dma_buf->resv;
Daniel Vetter1286ff72012-05-10 15:25:09 +0200291
Chris Wilson30bc06c2016-07-20 09:21:14 +0100292 /* We use GTT as shorthand for a coherent domain, one that is
293 * neither in the GPU cache nor in the CPU cache, where all
294 * writes are immediately visible in memory. (That's not strictly
295 * true, but it's close! There are internal buffers such as the
296 * write-combined buffer or a delay through the chipset for GTT
297 * writes that do require us to treat GTT as a separate cache domain.)
298 */
299 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
300 obj->base.write_domain = 0;
301
Daniel Vetter1286ff72012-05-10 15:25:09 +0200302 return &obj->base;
303
Daniel Vetter1286ff72012-05-10 15:25:09 +0200304fail_detach:
305 dma_buf_detach(dma_buf, attach);
Imre Deak011c22822013-04-19 11:11:56 +1000306 dma_buf_put(dma_buf);
307
Daniel Vetter1286ff72012-05-10 15:25:09 +0200308 return ERR_PTR(ret);
309}
Chris Wilson6cca22e2017-02-13 17:15:36 +0000310
311#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
312#include "selftests/mock_dmabuf.c"
313#include "selftests/i915_gem_dmabuf.c"
314#endif