blob: 340caabc04246c3e4d1a0034b37ae76ec8e88bde [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};
Joonyoung Shim538d6662013-06-19 15:03:05 +090065
66struct drm_prime_attachment {
67 struct sg_table *sgt;
68 enum dma_data_direction dir;
69};
70
Dave Airlie219b4732013-04-22 09:54:36 +100071static 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 +000072
Maarten Lankhorstca793f72013-04-09 09:52:54 +020073static int drm_gem_map_attach(struct dma_buf *dma_buf,
74 struct device *target_dev,
75 struct dma_buf_attachment *attach)
76{
Joonyoung Shim538d6662013-06-19 15:03:05 +090077 struct drm_prime_attachment *prime_attach;
Maarten Lankhorstca793f72013-04-09 09:52:54 +020078 struct drm_gem_object *obj = dma_buf->priv;
79 struct drm_device *dev = obj->dev;
80
Joonyoung Shim538d6662013-06-19 15:03:05 +090081 prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
82 if (!prime_attach)
83 return -ENOMEM;
84
85 prime_attach->dir = DMA_NONE;
86 attach->priv = prime_attach;
87
Maarten Lankhorstca793f72013-04-09 09:52:54 +020088 if (!dev->driver->gem_prime_pin)
89 return 0;
90
91 return dev->driver->gem_prime_pin(obj);
92}
93
94static void drm_gem_map_detach(struct dma_buf *dma_buf,
95 struct dma_buf_attachment *attach)
96{
Joonyoung Shim538d6662013-06-19 15:03:05 +090097 struct drm_prime_attachment *prime_attach = attach->priv;
Maarten Lankhorstca793f72013-04-09 09:52:54 +020098 struct drm_gem_object *obj = dma_buf->priv;
99 struct drm_device *dev = obj->dev;
Joonyoung Shim538d6662013-06-19 15:03:05 +0900100 struct sg_table *sgt;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200101
102 if (dev->driver->gem_prime_unpin)
103 dev->driver->gem_prime_unpin(obj);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900104
105 if (!prime_attach)
106 return;
107
108 sgt = prime_attach->sgt;
109
110 if (prime_attach->dir != DMA_NONE)
111 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
112 prime_attach->dir);
113
114 sg_free_table(sgt);
115 kfree(sgt);
116 kfree(prime_attach);
117 attach->priv = NULL;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200118}
119
Aaron Plattner89177642013-01-15 20:47:42 +0000120static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
121 enum dma_data_direction dir)
122{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900123 struct drm_prime_attachment *prime_attach = attach->priv;
Aaron Plattner89177642013-01-15 20:47:42 +0000124 struct drm_gem_object *obj = attach->dmabuf->priv;
125 struct sg_table *sgt;
126
Joonyoung Shim538d6662013-06-19 15:03:05 +0900127 if (WARN_ON(dir == DMA_NONE || !prime_attach))
128 return ERR_PTR(-EINVAL);
129
130 /* return the cached mapping when possible */
131 if (prime_attach->dir == dir)
132 return prime_attach->sgt;
133
134 /*
135 * two mappings with different directions for the same attachment are
136 * not allowed
137 */
138 if (WARN_ON(prime_attach->dir != DMA_NONE))
139 return ERR_PTR(-EBUSY);
140
Aaron Plattner89177642013-01-15 20:47:42 +0000141 mutex_lock(&obj->dev->struct_mutex);
142
143 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
144
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900145 if (!IS_ERR(sgt)) {
YoungJun Chob720d542013-06-24 15:34:21 +0900146 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
147 sg_free_table(sgt);
148 kfree(sgt);
149 sgt = ERR_PTR(-ENOMEM);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900150 } else {
151 prime_attach->sgt = sgt;
152 prime_attach->dir = dir;
YoungJun Chob720d542013-06-24 15:34:21 +0900153 }
154 }
Aaron Plattner89177642013-01-15 20:47:42 +0000155
156 mutex_unlock(&obj->dev->struct_mutex);
157 return sgt;
158}
159
160static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
161 struct sg_table *sgt, enum dma_data_direction dir)
162{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900163 /* nothing to be done here */
Aaron Plattner89177642013-01-15 20:47:42 +0000164}
165
166static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
167{
168 struct drm_gem_object *obj = dma_buf->priv;
169
170 if (obj->export_dma_buf == dma_buf) {
171 /* drop the reference on the export fd holds */
172 obj->export_dma_buf = NULL;
173 drm_gem_object_unreference_unlocked(obj);
174 }
175}
176
177static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
178{
179 struct drm_gem_object *obj = dma_buf->priv;
180 struct drm_device *dev = obj->dev;
181
182 return dev->driver->gem_prime_vmap(obj);
183}
184
185static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
186{
187 struct drm_gem_object *obj = dma_buf->priv;
188 struct drm_device *dev = obj->dev;
189
190 dev->driver->gem_prime_vunmap(obj, vaddr);
191}
192
193static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
194 unsigned long page_num)
195{
196 return NULL;
197}
198
199static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
200 unsigned long page_num, void *addr)
201{
202
203}
204static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
205 unsigned long page_num)
206{
207 return NULL;
208}
209
210static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
211 unsigned long page_num, void *addr)
212{
213
214}
215
216static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
217 struct vm_area_struct *vma)
218{
219 return -EINVAL;
220}
221
222static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200223 .attach = drm_gem_map_attach,
224 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000225 .map_dma_buf = drm_gem_map_dma_buf,
226 .unmap_dma_buf = drm_gem_unmap_dma_buf,
227 .release = drm_gem_dmabuf_release,
228 .kmap = drm_gem_dmabuf_kmap,
229 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
230 .kunmap = drm_gem_dmabuf_kunmap,
231 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
232 .mmap = drm_gem_dmabuf_mmap,
233 .vmap = drm_gem_dmabuf_vmap,
234 .vunmap = drm_gem_dmabuf_vunmap,
235};
236
237/**
238 * DOC: PRIME Helpers
239 *
240 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
241 * simpler APIs by using the helper functions @drm_gem_prime_export and
242 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
243 * five lower-level driver callbacks:
244 *
245 * Export callbacks:
246 *
247 * - @gem_prime_pin (optional): prepare a GEM object for exporting
248 *
249 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
250 *
251 * - @gem_prime_vmap: vmap a buffer exported by your driver
252 *
253 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
254 *
255 * Import callback:
256 *
257 * - @gem_prime_import_sg_table (import): produce a GEM object from another
258 * driver's scatter/gather table
259 */
260
261struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
262 struct drm_gem_object *obj, int flags)
263{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200264 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000265}
266EXPORT_SYMBOL(drm_gem_prime_export);
267
Dave Airlie32488772011-11-25 15:21:02 +0000268int drm_gem_prime_handle_to_fd(struct drm_device *dev,
269 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
270 int *prime_fd)
271{
272 struct drm_gem_object *obj;
273 void *buf;
Dave Airlie219b4732013-04-22 09:54:36 +1000274 int ret = 0;
275 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000276
277 obj = drm_gem_object_lookup(dev, file_priv, handle);
278 if (!obj)
279 return -ENOENT;
280
281 mutex_lock(&file_priv->prime.lock);
282 /* re-export the original imported object */
283 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000284 dmabuf = obj->import_attach->dmabuf;
285 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000286 }
287
288 if (obj->export_dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000289 dmabuf = obj->export_dma_buf;
290 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000291 }
Dave Airlie219b4732013-04-22 09:54:36 +1000292
293 buf = dev->driver->gem_prime_export(dev, obj, flags);
294 if (IS_ERR(buf)) {
295 /* normally the created dma-buf takes ownership of the ref,
296 * but if that fails then drop the ref
297 */
298 ret = PTR_ERR(buf);
299 goto out;
300 }
301 obj->export_dma_buf = buf;
302
Dave Airlie0ff926c2012-05-20 17:31:16 +0100303 /* if we've exported this buffer the cheat and add it to the import list
304 * so we get the correct handle back
305 */
Dave Airlie219b4732013-04-22 09:54:36 +1000306 ret = drm_prime_add_buf_handle(&file_priv->prime,
307 obj->export_dma_buf, handle);
308 if (ret)
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900309 goto fail_put_dmabuf;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100310
Dave Airlie219b4732013-04-22 09:54:36 +1000311 *prime_fd = dma_buf_fd(buf, flags);
Dave Airlie32488772011-11-25 15:21:02 +0000312 mutex_unlock(&file_priv->prime.lock);
313 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000314
315out_have_obj:
316 get_dma_buf(dmabuf);
317 *prime_fd = dma_buf_fd(dmabuf, flags);
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900318 goto out;
319
320fail_put_dmabuf:
321 /* clear NOT to be checked when releasing dma_buf */
322 obj->export_dma_buf = NULL;
323 dma_buf_put(buf);
Dave Airlie219b4732013-04-22 09:54:36 +1000324out:
325 drm_gem_object_unreference_unlocked(obj);
326 mutex_unlock(&file_priv->prime.lock);
327 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000328}
329EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
330
Aaron Plattner89177642013-01-15 20:47:42 +0000331struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
332 struct dma_buf *dma_buf)
333{
334 struct dma_buf_attachment *attach;
335 struct sg_table *sgt;
336 struct drm_gem_object *obj;
337 int ret;
338
339 if (!dev->driver->gem_prime_import_sg_table)
340 return ERR_PTR(-EINVAL);
341
342 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
343 obj = dma_buf->priv;
344 if (obj->dev == dev) {
345 /*
346 * Importing dmabuf exported from out own gem increases
347 * refcount on gem itself instead of f_count of dmabuf.
348 */
349 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000350 return obj;
351 }
352 }
353
354 attach = dma_buf_attach(dma_buf, dev->dev);
355 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000356 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000357
Imre Deak011c22822013-04-19 11:11:56 +1000358 get_dma_buf(dma_buf);
359
Aaron Plattner89177642013-01-15 20:47:42 +0000360 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
361 if (IS_ERR_OR_NULL(sgt)) {
362 ret = PTR_ERR(sgt);
363 goto fail_detach;
364 }
365
366 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
367 if (IS_ERR(obj)) {
368 ret = PTR_ERR(obj);
369 goto fail_unmap;
370 }
371
372 obj->import_attach = attach;
373
374 return obj;
375
376fail_unmap:
377 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
378fail_detach:
379 dma_buf_detach(dma_buf, attach);
Imre Deak011c22822013-04-19 11:11:56 +1000380 dma_buf_put(dma_buf);
381
Aaron Plattner89177642013-01-15 20:47:42 +0000382 return ERR_PTR(ret);
383}
384EXPORT_SYMBOL(drm_gem_prime_import);
385
Dave Airlie32488772011-11-25 15:21:02 +0000386int drm_gem_prime_fd_to_handle(struct drm_device *dev,
387 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
388{
389 struct dma_buf *dma_buf;
390 struct drm_gem_object *obj;
391 int ret;
392
393 dma_buf = dma_buf_get(prime_fd);
394 if (IS_ERR(dma_buf))
395 return PTR_ERR(dma_buf);
396
397 mutex_lock(&file_priv->prime.lock);
398
Dave Airlie219b4732013-04-22 09:54:36 +1000399 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000400 dma_buf, handle);
401 if (!ret) {
402 ret = 0;
403 goto out_put;
404 }
405
406 /* never seen this one, need to import */
407 obj = dev->driver->gem_prime_import(dev, dma_buf);
408 if (IS_ERR(obj)) {
409 ret = PTR_ERR(obj);
410 goto out_put;
411 }
412
413 ret = drm_gem_handle_create(file_priv, obj, handle);
414 drm_gem_object_unreference_unlocked(obj);
415 if (ret)
416 goto out_put;
417
Dave Airlie219b4732013-04-22 09:54:36 +1000418 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000419 dma_buf, *handle);
420 if (ret)
421 goto fail;
422
423 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c22822013-04-19 11:11:56 +1000424
425 dma_buf_put(dma_buf);
426
Dave Airlie32488772011-11-25 15:21:02 +0000427 return 0;
428
429fail:
430 /* hmm, if driver attached, we are relying on the free-object path
431 * to detach.. which seems ok..
432 */
433 drm_gem_object_handle_unreference_unlocked(obj);
434out_put:
435 dma_buf_put(dma_buf);
436 mutex_unlock(&file_priv->prime.lock);
437 return ret;
438}
439EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
440
441int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
442 struct drm_file *file_priv)
443{
444 struct drm_prime_handle *args = data;
445 uint32_t flags;
446
447 if (!drm_core_check_feature(dev, DRIVER_PRIME))
448 return -EINVAL;
449
450 if (!dev->driver->prime_handle_to_fd)
451 return -ENOSYS;
452
453 /* check flags are valid */
454 if (args->flags & ~DRM_CLOEXEC)
455 return -EINVAL;
456
457 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
458 flags = args->flags & DRM_CLOEXEC;
459
460 return dev->driver->prime_handle_to_fd(dev, file_priv,
461 args->handle, flags, &args->fd);
462}
463
464int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
465 struct drm_file *file_priv)
466{
467 struct drm_prime_handle *args = data;
468
469 if (!drm_core_check_feature(dev, DRIVER_PRIME))
470 return -EINVAL;
471
472 if (!dev->driver->prime_fd_to_handle)
473 return -ENOSYS;
474
475 return dev->driver->prime_fd_to_handle(dev, file_priv,
476 args->fd, &args->handle);
477}
478
479/*
480 * drm_prime_pages_to_sg
481 *
482 * this helper creates an sg table object from a set of pages
483 * the driver is responsible for mapping the pages into the
484 * importers address space
485 */
486struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
487{
488 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000489 int ret;
490
491 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900492 if (!sg) {
493 ret = -ENOMEM;
Dave Airlie32488772011-11-25 15:21:02 +0000494 goto out;
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900495 }
Dave Airlie32488772011-11-25 15:21:02 +0000496
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500497 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
498 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000499 if (ret)
500 goto out;
501
Dave Airlie32488772011-11-25 15:21:02 +0000502 return sg;
503out:
504 kfree(sg);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900505 return ERR_PTR(ret);
Dave Airlie32488772011-11-25 15:21:02 +0000506}
507EXPORT_SYMBOL(drm_prime_pages_to_sg);
508
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100509/* export an sg table into an array of pages and addresses
510 this is currently required by the TTM driver in order to do correct fault
511 handling */
512int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
513 dma_addr_t *addrs, int max_pages)
514{
515 unsigned count;
516 struct scatterlist *sg;
517 struct page *page;
518 u32 len, offset;
519 int pg_index;
520 dma_addr_t addr;
521
522 pg_index = 0;
523 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
524 len = sg->length;
525 offset = sg->offset;
526 page = sg_page(sg);
527 addr = sg_dma_address(sg);
528
529 while (len > 0) {
530 if (WARN_ON(pg_index >= max_pages))
531 return -1;
532 pages[pg_index] = page;
533 if (addrs)
534 addrs[pg_index] = addr;
535
536 page++;
537 addr += PAGE_SIZE;
538 len -= PAGE_SIZE;
539 pg_index++;
540 }
541 }
542 return 0;
543}
544EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000545/* helper function to cleanup a GEM/prime object */
546void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
547{
548 struct dma_buf_attachment *attach;
549 struct dma_buf *dma_buf;
550 attach = obj->import_attach;
551 if (sg)
552 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
553 dma_buf = attach->dmabuf;
554 dma_buf_detach(attach->dmabuf, attach);
555 /* remove the reference */
556 dma_buf_put(dma_buf);
557}
558EXPORT_SYMBOL(drm_prime_gem_destroy);
559
560void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
561{
562 INIT_LIST_HEAD(&prime_fpriv->head);
563 mutex_init(&prime_fpriv->lock);
564}
565EXPORT_SYMBOL(drm_prime_init_file_private);
566
567void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
568{
Imre Deak98b76232013-04-24 19:04:57 +0300569 /* by now drm_gem_release should've made sure the list is empty */
570 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000571}
572EXPORT_SYMBOL(drm_prime_destroy_file_private);
573
Dave Airlie219b4732013-04-22 09:54:36 +1000574static 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 +0000575{
576 struct drm_prime_member *member;
577
578 member = kmalloc(sizeof(*member), GFP_KERNEL);
579 if (!member)
580 return -ENOMEM;
581
Dave Airlie219b4732013-04-22 09:54:36 +1000582 get_dma_buf(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000583 member->dma_buf = dma_buf;
584 member->handle = handle;
585 list_add(&member->entry, &prime_fpriv->head);
586 return 0;
587}
Dave Airlie32488772011-11-25 15:21:02 +0000588
Dave Airlie219b4732013-04-22 09:54:36 +1000589int 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 +0000590{
591 struct drm_prime_member *member;
592
593 list_for_each_entry(member, &prime_fpriv->head, entry) {
594 if (member->dma_buf == dma_buf) {
595 *handle = member->handle;
596 return 0;
597 }
598 }
599 return -ENOENT;
600}
Dave Airlie219b4732013-04-22 09:54:36 +1000601EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000602
Dave Airlie219b4732013-04-22 09:54:36 +1000603void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000604{
605 struct drm_prime_member *member, *safe;
606
607 mutex_lock(&prime_fpriv->lock);
608 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
609 if (member->dma_buf == dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000610 dma_buf_put(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000611 list_del(&member->entry);
612 kfree(member);
613 }
614 }
615 mutex_unlock(&prime_fpriv->lock);
616}
Dave Airlie219b4732013-04-22 09:54:36 +1000617EXPORT_SYMBOL(drm_prime_remove_buf_handle);