blob: e57c675db840a61449f596e5c69177844545a4f5 [file] [log] [blame]
Dave Airlie32488772011-11-25 15:21:02 +00001/*
2 * Copyright © 2012 Red Hat
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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Dave Airlie <airlied@redhat.com>
25 * Rob Clark <rob.clark@linaro.org>
26 *
27 */
28
29#include <linux/export.h>
30#include <linux/dma-buf.h>
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
Dave Airlie32488772011-11-25 15:21:02 +000032
33/*
34 * DMA-BUF/GEM Object references and lifetime overview:
35 *
36 * On the export the dma_buf holds a reference to the exporting GEM
37 * object. It takes this reference in handle_to_fd_ioctl, when it
38 * first calls .prime_export and stores the exporting GEM object in
39 * the dma_buf priv. This reference is released when the dma_buf
40 * object goes away in the driver .release function.
41 *
42 * On the import the importing GEM object holds a reference to the
43 * dma_buf (which in turn holds a ref to the exporting GEM object).
44 * It takes that reference in the fd_to_handle ioctl.
45 * It calls dma_buf_get, creates an attachment to it and stores the
46 * attachment in the GEM object. When this attachment is destroyed
47 * when the imported object is destroyed, we remove the attachment
48 * and drop the reference to the dma_buf.
49 *
50 * Thus the chain of references always flows in one direction
51 * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
52 *
53 * Self-importing: if userspace is using PRIME as a replacement for flink
54 * then it will get a fd->handle request for a GEM object that it created.
55 * Drivers should detect this situation and return back the gem object
Aaron Plattner89177642013-01-15 20:47:42 +000056 * from the dma-buf private. Prime will do this automatically for drivers that
57 * use the drm_gem_prime_{import,export} helpers.
Dave Airlie32488772011-11-25 15:21:02 +000058 */
59
60struct drm_prime_member {
61 struct list_head entry;
62 struct dma_buf *dma_buf;
63 uint32_t handle;
64};
Dave Airlie219b4732013-04-22 09:54:36 +100065static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
Dave Airlie32488772011-11-25 15:21:02 +000066
Maarten Lankhorstca793f72013-04-09 09:52:54 +020067static int drm_gem_map_attach(struct dma_buf *dma_buf,
68 struct device *target_dev,
69 struct dma_buf_attachment *attach)
70{
71 struct drm_gem_object *obj = dma_buf->priv;
72 struct drm_device *dev = obj->dev;
73
74 if (!dev->driver->gem_prime_pin)
75 return 0;
76
77 return dev->driver->gem_prime_pin(obj);
78}
79
80static void drm_gem_map_detach(struct dma_buf *dma_buf,
81 struct dma_buf_attachment *attach)
82{
83 struct drm_gem_object *obj = dma_buf->priv;
84 struct drm_device *dev = obj->dev;
85
86 if (dev->driver->gem_prime_unpin)
87 dev->driver->gem_prime_unpin(obj);
88}
89
Aaron Plattner89177642013-01-15 20:47:42 +000090static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
91 enum dma_data_direction dir)
92{
93 struct drm_gem_object *obj = attach->dmabuf->priv;
94 struct sg_table *sgt;
95
96 mutex_lock(&obj->dev->struct_mutex);
97
98 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
99
100 if (!IS_ERR_OR_NULL(sgt))
101 dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
102
103 mutex_unlock(&obj->dev->struct_mutex);
104 return sgt;
105}
106
107static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
108 struct sg_table *sgt, enum dma_data_direction dir)
109{
110 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
111 sg_free_table(sgt);
112 kfree(sgt);
113}
114
115static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
116{
117 struct drm_gem_object *obj = dma_buf->priv;
118
119 if (obj->export_dma_buf == dma_buf) {
120 /* drop the reference on the export fd holds */
121 obj->export_dma_buf = NULL;
122 drm_gem_object_unreference_unlocked(obj);
123 }
124}
125
126static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
127{
128 struct drm_gem_object *obj = dma_buf->priv;
129 struct drm_device *dev = obj->dev;
130
131 return dev->driver->gem_prime_vmap(obj);
132}
133
134static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
135{
136 struct drm_gem_object *obj = dma_buf->priv;
137 struct drm_device *dev = obj->dev;
138
139 dev->driver->gem_prime_vunmap(obj, vaddr);
140}
141
142static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
143 unsigned long page_num)
144{
145 return NULL;
146}
147
148static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
149 unsigned long page_num, void *addr)
150{
151
152}
153static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
154 unsigned long page_num)
155{
156 return NULL;
157}
158
159static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
160 unsigned long page_num, void *addr)
161{
162
163}
164
165static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
166 struct vm_area_struct *vma)
167{
168 return -EINVAL;
169}
170
171static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200172 .attach = drm_gem_map_attach,
173 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000174 .map_dma_buf = drm_gem_map_dma_buf,
175 .unmap_dma_buf = drm_gem_unmap_dma_buf,
176 .release = drm_gem_dmabuf_release,
177 .kmap = drm_gem_dmabuf_kmap,
178 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
179 .kunmap = drm_gem_dmabuf_kunmap,
180 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
181 .mmap = drm_gem_dmabuf_mmap,
182 .vmap = drm_gem_dmabuf_vmap,
183 .vunmap = drm_gem_dmabuf_vunmap,
184};
185
186/**
187 * DOC: PRIME Helpers
188 *
189 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
190 * simpler APIs by using the helper functions @drm_gem_prime_export and
191 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
192 * five lower-level driver callbacks:
193 *
194 * Export callbacks:
195 *
196 * - @gem_prime_pin (optional): prepare a GEM object for exporting
197 *
198 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
199 *
200 * - @gem_prime_vmap: vmap a buffer exported by your driver
201 *
202 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
203 *
204 * Import callback:
205 *
206 * - @gem_prime_import_sg_table (import): produce a GEM object from another
207 * driver's scatter/gather table
208 */
209
210struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
211 struct drm_gem_object *obj, int flags)
212{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200213 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000214}
215EXPORT_SYMBOL(drm_gem_prime_export);
216
Dave Airlie32488772011-11-25 15:21:02 +0000217int drm_gem_prime_handle_to_fd(struct drm_device *dev,
218 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
219 int *prime_fd)
220{
221 struct drm_gem_object *obj;
222 void *buf;
Dave Airlie219b4732013-04-22 09:54:36 +1000223 int ret = 0;
224 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000225
226 obj = drm_gem_object_lookup(dev, file_priv, handle);
227 if (!obj)
228 return -ENOENT;
229
230 mutex_lock(&file_priv->prime.lock);
231 /* re-export the original imported object */
232 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000233 dmabuf = obj->import_attach->dmabuf;
234 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000235 }
236
237 if (obj->export_dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000238 dmabuf = obj->export_dma_buf;
239 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000240 }
Dave Airlie219b4732013-04-22 09:54:36 +1000241
242 buf = dev->driver->gem_prime_export(dev, obj, flags);
243 if (IS_ERR(buf)) {
244 /* normally the created dma-buf takes ownership of the ref,
245 * but if that fails then drop the ref
246 */
247 ret = PTR_ERR(buf);
248 goto out;
249 }
250 obj->export_dma_buf = buf;
251
Dave Airlie0ff926c2012-05-20 17:31:16 +0100252 /* if we've exported this buffer the cheat and add it to the import list
253 * so we get the correct handle back
254 */
Dave Airlie219b4732013-04-22 09:54:36 +1000255 ret = drm_prime_add_buf_handle(&file_priv->prime,
256 obj->export_dma_buf, handle);
257 if (ret)
258 goto out;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100259
Dave Airlie219b4732013-04-22 09:54:36 +1000260 *prime_fd = dma_buf_fd(buf, flags);
Dave Airlie32488772011-11-25 15:21:02 +0000261 mutex_unlock(&file_priv->prime.lock);
262 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000263
264out_have_obj:
265 get_dma_buf(dmabuf);
266 *prime_fd = dma_buf_fd(dmabuf, flags);
267out:
268 drm_gem_object_unreference_unlocked(obj);
269 mutex_unlock(&file_priv->prime.lock);
270 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000271}
272EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
273
Aaron Plattner89177642013-01-15 20:47:42 +0000274struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
275 struct dma_buf *dma_buf)
276{
277 struct dma_buf_attachment *attach;
278 struct sg_table *sgt;
279 struct drm_gem_object *obj;
280 int ret;
281
282 if (!dev->driver->gem_prime_import_sg_table)
283 return ERR_PTR(-EINVAL);
284
285 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
286 obj = dma_buf->priv;
287 if (obj->dev == dev) {
288 /*
289 * Importing dmabuf exported from out own gem increases
290 * refcount on gem itself instead of f_count of dmabuf.
291 */
292 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000293 return obj;
294 }
295 }
296
297 attach = dma_buf_attach(dma_buf, dev->dev);
298 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000299 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000300
Imre Deak011c2282013-04-19 11:11:56 +1000301 get_dma_buf(dma_buf);
302
Aaron Plattner89177642013-01-15 20:47:42 +0000303 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
304 if (IS_ERR_OR_NULL(sgt)) {
305 ret = PTR_ERR(sgt);
306 goto fail_detach;
307 }
308
309 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
310 if (IS_ERR(obj)) {
311 ret = PTR_ERR(obj);
312 goto fail_unmap;
313 }
314
315 obj->import_attach = attach;
316
317 return obj;
318
319fail_unmap:
320 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
321fail_detach:
322 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000323 dma_buf_put(dma_buf);
324
Aaron Plattner89177642013-01-15 20:47:42 +0000325 return ERR_PTR(ret);
326}
327EXPORT_SYMBOL(drm_gem_prime_import);
328
Dave Airlie32488772011-11-25 15:21:02 +0000329int drm_gem_prime_fd_to_handle(struct drm_device *dev,
330 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
331{
332 struct dma_buf *dma_buf;
333 struct drm_gem_object *obj;
334 int ret;
335
336 dma_buf = dma_buf_get(prime_fd);
337 if (IS_ERR(dma_buf))
338 return PTR_ERR(dma_buf);
339
340 mutex_lock(&file_priv->prime.lock);
341
Dave Airlie219b4732013-04-22 09:54:36 +1000342 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000343 dma_buf, handle);
344 if (!ret) {
345 ret = 0;
346 goto out_put;
347 }
348
349 /* never seen this one, need to import */
350 obj = dev->driver->gem_prime_import(dev, dma_buf);
351 if (IS_ERR(obj)) {
352 ret = PTR_ERR(obj);
353 goto out_put;
354 }
355
356 ret = drm_gem_handle_create(file_priv, obj, handle);
357 drm_gem_object_unreference_unlocked(obj);
358 if (ret)
359 goto out_put;
360
Dave Airlie219b4732013-04-22 09:54:36 +1000361 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000362 dma_buf, *handle);
363 if (ret)
364 goto fail;
365
366 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c2282013-04-19 11:11:56 +1000367
368 dma_buf_put(dma_buf);
369
Dave Airlie32488772011-11-25 15:21:02 +0000370 return 0;
371
372fail:
373 /* hmm, if driver attached, we are relying on the free-object path
374 * to detach.. which seems ok..
375 */
376 drm_gem_object_handle_unreference_unlocked(obj);
377out_put:
378 dma_buf_put(dma_buf);
379 mutex_unlock(&file_priv->prime.lock);
380 return ret;
381}
382EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
383
384int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
385 struct drm_file *file_priv)
386{
387 struct drm_prime_handle *args = data;
388 uint32_t flags;
389
390 if (!drm_core_check_feature(dev, DRIVER_PRIME))
391 return -EINVAL;
392
393 if (!dev->driver->prime_handle_to_fd)
394 return -ENOSYS;
395
396 /* check flags are valid */
397 if (args->flags & ~DRM_CLOEXEC)
398 return -EINVAL;
399
400 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
401 flags = args->flags & DRM_CLOEXEC;
402
403 return dev->driver->prime_handle_to_fd(dev, file_priv,
404 args->handle, flags, &args->fd);
405}
406
407int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
408 struct drm_file *file_priv)
409{
410 struct drm_prime_handle *args = data;
411
412 if (!drm_core_check_feature(dev, DRIVER_PRIME))
413 return -EINVAL;
414
415 if (!dev->driver->prime_fd_to_handle)
416 return -ENOSYS;
417
418 return dev->driver->prime_fd_to_handle(dev, file_priv,
419 args->fd, &args->handle);
420}
421
422/*
423 * drm_prime_pages_to_sg
424 *
425 * this helper creates an sg table object from a set of pages
426 * the driver is responsible for mapping the pages into the
427 * importers address space
428 */
429struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
430{
431 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000432 int ret;
433
434 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
435 if (!sg)
436 goto out;
437
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500438 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
439 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000440 if (ret)
441 goto out;
442
Dave Airlie32488772011-11-25 15:21:02 +0000443 return sg;
444out:
445 kfree(sg);
446 return NULL;
447}
448EXPORT_SYMBOL(drm_prime_pages_to_sg);
449
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100450/* export an sg table into an array of pages and addresses
451 this is currently required by the TTM driver in order to do correct fault
452 handling */
453int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
454 dma_addr_t *addrs, int max_pages)
455{
456 unsigned count;
457 struct scatterlist *sg;
458 struct page *page;
459 u32 len, offset;
460 int pg_index;
461 dma_addr_t addr;
462
463 pg_index = 0;
464 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
465 len = sg->length;
466 offset = sg->offset;
467 page = sg_page(sg);
468 addr = sg_dma_address(sg);
469
470 while (len > 0) {
471 if (WARN_ON(pg_index >= max_pages))
472 return -1;
473 pages[pg_index] = page;
474 if (addrs)
475 addrs[pg_index] = addr;
476
477 page++;
478 addr += PAGE_SIZE;
479 len -= PAGE_SIZE;
480 pg_index++;
481 }
482 }
483 return 0;
484}
485EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000486/* helper function to cleanup a GEM/prime object */
487void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
488{
489 struct dma_buf_attachment *attach;
490 struct dma_buf *dma_buf;
491 attach = obj->import_attach;
492 if (sg)
493 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
494 dma_buf = attach->dmabuf;
495 dma_buf_detach(attach->dmabuf, attach);
496 /* remove the reference */
497 dma_buf_put(dma_buf);
498}
499EXPORT_SYMBOL(drm_prime_gem_destroy);
500
501void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
502{
503 INIT_LIST_HEAD(&prime_fpriv->head);
504 mutex_init(&prime_fpriv->lock);
505}
506EXPORT_SYMBOL(drm_prime_init_file_private);
507
508void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
509{
Imre Deak98b76232013-04-24 19:04:57 +0300510 /* by now drm_gem_release should've made sure the list is empty */
511 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000512}
513EXPORT_SYMBOL(drm_prime_destroy_file_private);
514
Dave Airlie219b4732013-04-22 09:54:36 +1000515static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
Dave Airlie32488772011-11-25 15:21:02 +0000516{
517 struct drm_prime_member *member;
518
519 member = kmalloc(sizeof(*member), GFP_KERNEL);
520 if (!member)
521 return -ENOMEM;
522
Dave Airlie219b4732013-04-22 09:54:36 +1000523 get_dma_buf(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000524 member->dma_buf = dma_buf;
525 member->handle = handle;
526 list_add(&member->entry, &prime_fpriv->head);
527 return 0;
528}
Dave Airlie32488772011-11-25 15:21:02 +0000529
Dave Airlie219b4732013-04-22 09:54:36 +1000530int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
Dave Airlie32488772011-11-25 15:21:02 +0000531{
532 struct drm_prime_member *member;
533
534 list_for_each_entry(member, &prime_fpriv->head, entry) {
535 if (member->dma_buf == dma_buf) {
536 *handle = member->handle;
537 return 0;
538 }
539 }
540 return -ENOENT;
541}
Dave Airlie219b4732013-04-22 09:54:36 +1000542EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000543
Dave Airlie219b4732013-04-22 09:54:36 +1000544void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000545{
546 struct drm_prime_member *member, *safe;
547
548 mutex_lock(&prime_fpriv->lock);
549 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
550 if (member->dma_buf == dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000551 dma_buf_put(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000552 list_del(&member->entry);
553 kfree(member);
554 }
555 }
556 mutex_unlock(&prime_fpriv->lock);
557}
Dave Airlie219b4732013-04-22 09:54:36 +1000558EXPORT_SYMBOL(drm_prime_remove_buf_handle);