blob: 0daf2122a91dc386f58498ecaedff7869982a976 [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
YoungJun Chob720d542013-06-24 15:34:21 +0900100 if (!IS_ERR_OR_NULL(sgt)) {
101 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
102 sg_free_table(sgt);
103 kfree(sgt);
104 sgt = ERR_PTR(-ENOMEM);
105 }
106 }
Aaron Plattner89177642013-01-15 20:47:42 +0000107
108 mutex_unlock(&obj->dev->struct_mutex);
109 return sgt;
110}
111
112static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
113 struct sg_table *sgt, enum dma_data_direction dir)
114{
115 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
116 sg_free_table(sgt);
117 kfree(sgt);
118}
119
120static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
121{
122 struct drm_gem_object *obj = dma_buf->priv;
123
124 if (obj->export_dma_buf == dma_buf) {
125 /* drop the reference on the export fd holds */
126 obj->export_dma_buf = NULL;
127 drm_gem_object_unreference_unlocked(obj);
128 }
129}
130
131static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
132{
133 struct drm_gem_object *obj = dma_buf->priv;
134 struct drm_device *dev = obj->dev;
135
136 return dev->driver->gem_prime_vmap(obj);
137}
138
139static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
140{
141 struct drm_gem_object *obj = dma_buf->priv;
142 struct drm_device *dev = obj->dev;
143
144 dev->driver->gem_prime_vunmap(obj, vaddr);
145}
146
147static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
148 unsigned long page_num)
149{
150 return NULL;
151}
152
153static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
154 unsigned long page_num, void *addr)
155{
156
157}
158static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
159 unsigned long page_num)
160{
161 return NULL;
162}
163
164static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
165 unsigned long page_num, void *addr)
166{
167
168}
169
170static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
171 struct vm_area_struct *vma)
172{
173 return -EINVAL;
174}
175
176static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200177 .attach = drm_gem_map_attach,
178 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000179 .map_dma_buf = drm_gem_map_dma_buf,
180 .unmap_dma_buf = drm_gem_unmap_dma_buf,
181 .release = drm_gem_dmabuf_release,
182 .kmap = drm_gem_dmabuf_kmap,
183 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
184 .kunmap = drm_gem_dmabuf_kunmap,
185 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
186 .mmap = drm_gem_dmabuf_mmap,
187 .vmap = drm_gem_dmabuf_vmap,
188 .vunmap = drm_gem_dmabuf_vunmap,
189};
190
191/**
192 * DOC: PRIME Helpers
193 *
194 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
195 * simpler APIs by using the helper functions @drm_gem_prime_export and
196 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
197 * five lower-level driver callbacks:
198 *
199 * Export callbacks:
200 *
201 * - @gem_prime_pin (optional): prepare a GEM object for exporting
202 *
203 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
204 *
205 * - @gem_prime_vmap: vmap a buffer exported by your driver
206 *
207 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
208 *
209 * Import callback:
210 *
211 * - @gem_prime_import_sg_table (import): produce a GEM object from another
212 * driver's scatter/gather table
213 */
214
215struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
216 struct drm_gem_object *obj, int flags)
217{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200218 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000219}
220EXPORT_SYMBOL(drm_gem_prime_export);
221
Dave Airlie32488772011-11-25 15:21:02 +0000222int drm_gem_prime_handle_to_fd(struct drm_device *dev,
223 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
224 int *prime_fd)
225{
226 struct drm_gem_object *obj;
227 void *buf;
Dave Airlie219b4732013-04-22 09:54:36 +1000228 int ret = 0;
229 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000230
231 obj = drm_gem_object_lookup(dev, file_priv, handle);
232 if (!obj)
233 return -ENOENT;
234
235 mutex_lock(&file_priv->prime.lock);
236 /* re-export the original imported object */
237 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000238 dmabuf = obj->import_attach->dmabuf;
239 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000240 }
241
242 if (obj->export_dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000243 dmabuf = obj->export_dma_buf;
244 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000245 }
Dave Airlie219b4732013-04-22 09:54:36 +1000246
247 buf = dev->driver->gem_prime_export(dev, obj, flags);
248 if (IS_ERR(buf)) {
249 /* normally the created dma-buf takes ownership of the ref,
250 * but if that fails then drop the ref
251 */
252 ret = PTR_ERR(buf);
253 goto out;
254 }
255 obj->export_dma_buf = buf;
256
Dave Airlie0ff926c2012-05-20 17:31:16 +0100257 /* if we've exported this buffer the cheat and add it to the import list
258 * so we get the correct handle back
259 */
Dave Airlie219b4732013-04-22 09:54:36 +1000260 ret = drm_prime_add_buf_handle(&file_priv->prime,
261 obj->export_dma_buf, handle);
262 if (ret)
263 goto out;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100264
Dave Airlie219b4732013-04-22 09:54:36 +1000265 *prime_fd = dma_buf_fd(buf, flags);
Dave Airlie32488772011-11-25 15:21:02 +0000266 mutex_unlock(&file_priv->prime.lock);
267 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000268
269out_have_obj:
270 get_dma_buf(dmabuf);
271 *prime_fd = dma_buf_fd(dmabuf, flags);
272out:
273 drm_gem_object_unreference_unlocked(obj);
274 mutex_unlock(&file_priv->prime.lock);
275 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000276}
277EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
278
Aaron Plattner89177642013-01-15 20:47:42 +0000279struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
280 struct dma_buf *dma_buf)
281{
282 struct dma_buf_attachment *attach;
283 struct sg_table *sgt;
284 struct drm_gem_object *obj;
285 int ret;
286
287 if (!dev->driver->gem_prime_import_sg_table)
288 return ERR_PTR(-EINVAL);
289
290 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
291 obj = dma_buf->priv;
292 if (obj->dev == dev) {
293 /*
294 * Importing dmabuf exported from out own gem increases
295 * refcount on gem itself instead of f_count of dmabuf.
296 */
297 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000298 return obj;
299 }
300 }
301
302 attach = dma_buf_attach(dma_buf, dev->dev);
303 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000304 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000305
Imre Deak011c2282013-04-19 11:11:56 +1000306 get_dma_buf(dma_buf);
307
Aaron Plattner89177642013-01-15 20:47:42 +0000308 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
309 if (IS_ERR_OR_NULL(sgt)) {
310 ret = PTR_ERR(sgt);
311 goto fail_detach;
312 }
313
314 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
315 if (IS_ERR(obj)) {
316 ret = PTR_ERR(obj);
317 goto fail_unmap;
318 }
319
320 obj->import_attach = attach;
321
322 return obj;
323
324fail_unmap:
325 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
326fail_detach:
327 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000328 dma_buf_put(dma_buf);
329
Aaron Plattner89177642013-01-15 20:47:42 +0000330 return ERR_PTR(ret);
331}
332EXPORT_SYMBOL(drm_gem_prime_import);
333
Dave Airlie32488772011-11-25 15:21:02 +0000334int drm_gem_prime_fd_to_handle(struct drm_device *dev,
335 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
336{
337 struct dma_buf *dma_buf;
338 struct drm_gem_object *obj;
339 int ret;
340
341 dma_buf = dma_buf_get(prime_fd);
342 if (IS_ERR(dma_buf))
343 return PTR_ERR(dma_buf);
344
345 mutex_lock(&file_priv->prime.lock);
346
Dave Airlie219b4732013-04-22 09:54:36 +1000347 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000348 dma_buf, handle);
349 if (!ret) {
350 ret = 0;
351 goto out_put;
352 }
353
354 /* never seen this one, need to import */
355 obj = dev->driver->gem_prime_import(dev, dma_buf);
356 if (IS_ERR(obj)) {
357 ret = PTR_ERR(obj);
358 goto out_put;
359 }
360
361 ret = drm_gem_handle_create(file_priv, obj, handle);
362 drm_gem_object_unreference_unlocked(obj);
363 if (ret)
364 goto out_put;
365
Dave Airlie219b4732013-04-22 09:54:36 +1000366 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000367 dma_buf, *handle);
368 if (ret)
369 goto fail;
370
371 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c2282013-04-19 11:11:56 +1000372
373 dma_buf_put(dma_buf);
374
Dave Airlie32488772011-11-25 15:21:02 +0000375 return 0;
376
377fail:
378 /* hmm, if driver attached, we are relying on the free-object path
379 * to detach.. which seems ok..
380 */
381 drm_gem_object_handle_unreference_unlocked(obj);
382out_put:
383 dma_buf_put(dma_buf);
384 mutex_unlock(&file_priv->prime.lock);
385 return ret;
386}
387EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
388
389int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
390 struct drm_file *file_priv)
391{
392 struct drm_prime_handle *args = data;
393 uint32_t flags;
394
395 if (!drm_core_check_feature(dev, DRIVER_PRIME))
396 return -EINVAL;
397
398 if (!dev->driver->prime_handle_to_fd)
399 return -ENOSYS;
400
401 /* check flags are valid */
402 if (args->flags & ~DRM_CLOEXEC)
403 return -EINVAL;
404
405 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
406 flags = args->flags & DRM_CLOEXEC;
407
408 return dev->driver->prime_handle_to_fd(dev, file_priv,
409 args->handle, flags, &args->fd);
410}
411
412int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
413 struct drm_file *file_priv)
414{
415 struct drm_prime_handle *args = data;
416
417 if (!drm_core_check_feature(dev, DRIVER_PRIME))
418 return -EINVAL;
419
420 if (!dev->driver->prime_fd_to_handle)
421 return -ENOSYS;
422
423 return dev->driver->prime_fd_to_handle(dev, file_priv,
424 args->fd, &args->handle);
425}
426
427/*
428 * drm_prime_pages_to_sg
429 *
430 * this helper creates an sg table object from a set of pages
431 * the driver is responsible for mapping the pages into the
432 * importers address space
433 */
434struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
435{
436 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000437 int ret;
438
439 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
440 if (!sg)
441 goto out;
442
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500443 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
444 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000445 if (ret)
446 goto out;
447
Dave Airlie32488772011-11-25 15:21:02 +0000448 return sg;
449out:
450 kfree(sg);
451 return NULL;
452}
453EXPORT_SYMBOL(drm_prime_pages_to_sg);
454
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100455/* export an sg table into an array of pages and addresses
456 this is currently required by the TTM driver in order to do correct fault
457 handling */
458int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
459 dma_addr_t *addrs, int max_pages)
460{
461 unsigned count;
462 struct scatterlist *sg;
463 struct page *page;
464 u32 len, offset;
465 int pg_index;
466 dma_addr_t addr;
467
468 pg_index = 0;
469 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
470 len = sg->length;
471 offset = sg->offset;
472 page = sg_page(sg);
473 addr = sg_dma_address(sg);
474
475 while (len > 0) {
476 if (WARN_ON(pg_index >= max_pages))
477 return -1;
478 pages[pg_index] = page;
479 if (addrs)
480 addrs[pg_index] = addr;
481
482 page++;
483 addr += PAGE_SIZE;
484 len -= PAGE_SIZE;
485 pg_index++;
486 }
487 }
488 return 0;
489}
490EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000491/* helper function to cleanup a GEM/prime object */
492void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
493{
494 struct dma_buf_attachment *attach;
495 struct dma_buf *dma_buf;
496 attach = obj->import_attach;
497 if (sg)
498 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
499 dma_buf = attach->dmabuf;
500 dma_buf_detach(attach->dmabuf, attach);
501 /* remove the reference */
502 dma_buf_put(dma_buf);
503}
504EXPORT_SYMBOL(drm_prime_gem_destroy);
505
506void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
507{
508 INIT_LIST_HEAD(&prime_fpriv->head);
509 mutex_init(&prime_fpriv->lock);
510}
511EXPORT_SYMBOL(drm_prime_init_file_private);
512
513void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
514{
Imre Deak98b76232013-04-24 19:04:57 +0300515 /* by now drm_gem_release should've made sure the list is empty */
516 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000517}
518EXPORT_SYMBOL(drm_prime_destroy_file_private);
519
Dave Airlie219b4732013-04-22 09:54:36 +1000520static 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 +0000521{
522 struct drm_prime_member *member;
523
524 member = kmalloc(sizeof(*member), GFP_KERNEL);
525 if (!member)
526 return -ENOMEM;
527
Dave Airlie219b4732013-04-22 09:54:36 +1000528 get_dma_buf(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000529 member->dma_buf = dma_buf;
530 member->handle = handle;
531 list_add(&member->entry, &prime_fpriv->head);
532 return 0;
533}
Dave Airlie32488772011-11-25 15:21:02 +0000534
Dave Airlie219b4732013-04-22 09:54:36 +1000535int 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 +0000536{
537 struct drm_prime_member *member;
538
539 list_for_each_entry(member, &prime_fpriv->head, entry) {
540 if (member->dma_buf == dma_buf) {
541 *handle = member->handle;
542 return 0;
543 }
544 }
545 return -ENOENT;
546}
Dave Airlie219b4732013-04-22 09:54:36 +1000547EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000548
Dave Airlie219b4732013-04-22 09:54:36 +1000549void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000550{
551 struct drm_prime_member *member, *safe;
552
553 mutex_lock(&prime_fpriv->lock);
554 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
555 if (member->dma_buf == dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000556 dma_buf_put(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000557 list_del(&member->entry);
558 kfree(member);
559 }
560 }
561 mutex_unlock(&prime_fpriv->lock);
562}
Dave Airlie219b4732013-04-22 09:54:36 +1000563EXPORT_SYMBOL(drm_prime_remove_buf_handle);