blob: 117ffe32a7afba9b1b8e58665c45221bfc806775 [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
Seung-Woo Kimce92e3c2013-06-26 10:21:41 +090071static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
72{
73 struct drm_prime_member *member;
74
75 member = kmalloc(sizeof(*member), GFP_KERNEL);
76 if (!member)
77 return -ENOMEM;
78
79 get_dma_buf(dma_buf);
80 member->dma_buf = dma_buf;
81 member->handle = handle;
82 list_add(&member->entry, &prime_fpriv->head);
83 return 0;
84}
Dave Airlie32488772011-11-25 15:21:02 +000085
Maarten Lankhorstca793f72013-04-09 09:52:54 +020086static int drm_gem_map_attach(struct dma_buf *dma_buf,
87 struct device *target_dev,
88 struct dma_buf_attachment *attach)
89{
Joonyoung Shim538d6662013-06-19 15:03:05 +090090 struct drm_prime_attachment *prime_attach;
Maarten Lankhorstca793f72013-04-09 09:52:54 +020091 struct drm_gem_object *obj = dma_buf->priv;
92 struct drm_device *dev = obj->dev;
93
Joonyoung Shim538d6662013-06-19 15:03:05 +090094 prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
95 if (!prime_attach)
96 return -ENOMEM;
97
98 prime_attach->dir = DMA_NONE;
99 attach->priv = prime_attach;
100
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200101 if (!dev->driver->gem_prime_pin)
102 return 0;
103
104 return dev->driver->gem_prime_pin(obj);
105}
106
107static void drm_gem_map_detach(struct dma_buf *dma_buf,
108 struct dma_buf_attachment *attach)
109{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900110 struct drm_prime_attachment *prime_attach = attach->priv;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200111 struct drm_gem_object *obj = dma_buf->priv;
112 struct drm_device *dev = obj->dev;
Joonyoung Shim538d6662013-06-19 15:03:05 +0900113 struct sg_table *sgt;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200114
115 if (dev->driver->gem_prime_unpin)
116 dev->driver->gem_prime_unpin(obj);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900117
118 if (!prime_attach)
119 return;
120
121 sgt = prime_attach->sgt;
122
123 if (prime_attach->dir != DMA_NONE)
124 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
125 prime_attach->dir);
126
127 sg_free_table(sgt);
128 kfree(sgt);
129 kfree(prime_attach);
130 attach->priv = NULL;
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200131}
132
Aaron Plattner89177642013-01-15 20:47:42 +0000133static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
134 enum dma_data_direction dir)
135{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900136 struct drm_prime_attachment *prime_attach = attach->priv;
Aaron Plattner89177642013-01-15 20:47:42 +0000137 struct drm_gem_object *obj = attach->dmabuf->priv;
138 struct sg_table *sgt;
139
Joonyoung Shim538d6662013-06-19 15:03:05 +0900140 if (WARN_ON(dir == DMA_NONE || !prime_attach))
141 return ERR_PTR(-EINVAL);
142
143 /* return the cached mapping when possible */
144 if (prime_attach->dir == dir)
145 return prime_attach->sgt;
146
147 /*
148 * two mappings with different directions for the same attachment are
149 * not allowed
150 */
151 if (WARN_ON(prime_attach->dir != DMA_NONE))
152 return ERR_PTR(-EBUSY);
153
Aaron Plattner89177642013-01-15 20:47:42 +0000154 mutex_lock(&obj->dev->struct_mutex);
155
156 sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
157
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900158 if (!IS_ERR(sgt)) {
YoungJun Chob720d542013-06-24 15:34:21 +0900159 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
160 sg_free_table(sgt);
161 kfree(sgt);
162 sgt = ERR_PTR(-ENOMEM);
Joonyoung Shim538d6662013-06-19 15:03:05 +0900163 } else {
164 prime_attach->sgt = sgt;
165 prime_attach->dir = dir;
YoungJun Chob720d542013-06-24 15:34:21 +0900166 }
167 }
Aaron Plattner89177642013-01-15 20:47:42 +0000168
169 mutex_unlock(&obj->dev->struct_mutex);
170 return sgt;
171}
172
173static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
174 struct sg_table *sgt, enum dma_data_direction dir)
175{
Joonyoung Shim538d6662013-06-19 15:03:05 +0900176 /* nothing to be done here */
Aaron Plattner89177642013-01-15 20:47:42 +0000177}
178
179static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
180{
181 struct drm_gem_object *obj = dma_buf->priv;
182
183 if (obj->export_dma_buf == dma_buf) {
184 /* drop the reference on the export fd holds */
185 obj->export_dma_buf = NULL;
186 drm_gem_object_unreference_unlocked(obj);
187 }
188}
189
190static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
191{
192 struct drm_gem_object *obj = dma_buf->priv;
193 struct drm_device *dev = obj->dev;
194
195 return dev->driver->gem_prime_vmap(obj);
196}
197
198static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
199{
200 struct drm_gem_object *obj = dma_buf->priv;
201 struct drm_device *dev = obj->dev;
202
203 dev->driver->gem_prime_vunmap(obj, vaddr);
204}
205
206static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
207 unsigned long page_num)
208{
209 return NULL;
210}
211
212static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
213 unsigned long page_num, void *addr)
214{
215
216}
217static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
218 unsigned long page_num)
219{
220 return NULL;
221}
222
223static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
224 unsigned long page_num, void *addr)
225{
226
227}
228
229static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
230 struct vm_area_struct *vma)
231{
232 return -EINVAL;
233}
234
235static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
Maarten Lankhorstca793f72013-04-09 09:52:54 +0200236 .attach = drm_gem_map_attach,
237 .detach = drm_gem_map_detach,
Aaron Plattner89177642013-01-15 20:47:42 +0000238 .map_dma_buf = drm_gem_map_dma_buf,
239 .unmap_dma_buf = drm_gem_unmap_dma_buf,
240 .release = drm_gem_dmabuf_release,
241 .kmap = drm_gem_dmabuf_kmap,
242 .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
243 .kunmap = drm_gem_dmabuf_kunmap,
244 .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
245 .mmap = drm_gem_dmabuf_mmap,
246 .vmap = drm_gem_dmabuf_vmap,
247 .vunmap = drm_gem_dmabuf_vunmap,
248};
249
250/**
251 * DOC: PRIME Helpers
252 *
253 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
254 * simpler APIs by using the helper functions @drm_gem_prime_export and
255 * @drm_gem_prime_import. These functions implement dma-buf support in terms of
256 * five lower-level driver callbacks:
257 *
258 * Export callbacks:
259 *
260 * - @gem_prime_pin (optional): prepare a GEM object for exporting
261 *
262 * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
263 *
264 * - @gem_prime_vmap: vmap a buffer exported by your driver
265 *
266 * - @gem_prime_vunmap: vunmap a buffer exported by your driver
267 *
268 * Import callback:
269 *
270 * - @gem_prime_import_sg_table (import): produce a GEM object from another
271 * driver's scatter/gather table
272 */
273
274struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
275 struct drm_gem_object *obj, int flags)
276{
Laurent Pinchartebc0bad2013-06-19 03:14:20 +0200277 return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
Aaron Plattner89177642013-01-15 20:47:42 +0000278}
279EXPORT_SYMBOL(drm_gem_prime_export);
280
Dave Airlie32488772011-11-25 15:21:02 +0000281int drm_gem_prime_handle_to_fd(struct drm_device *dev,
282 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
283 int *prime_fd)
284{
285 struct drm_gem_object *obj;
286 void *buf;
Dave Airlie219b4732013-04-22 09:54:36 +1000287 int ret = 0;
288 struct dma_buf *dmabuf;
Dave Airlie32488772011-11-25 15:21:02 +0000289
290 obj = drm_gem_object_lookup(dev, file_priv, handle);
291 if (!obj)
292 return -ENOENT;
293
294 mutex_lock(&file_priv->prime.lock);
295 /* re-export the original imported object */
296 if (obj->import_attach) {
Dave Airlie219b4732013-04-22 09:54:36 +1000297 dmabuf = obj->import_attach->dmabuf;
298 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000299 }
300
301 if (obj->export_dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000302 dmabuf = obj->export_dma_buf;
303 goto out_have_obj;
Dave Airlie32488772011-11-25 15:21:02 +0000304 }
Dave Airlie219b4732013-04-22 09:54:36 +1000305
306 buf = dev->driver->gem_prime_export(dev, obj, flags);
307 if (IS_ERR(buf)) {
308 /* normally the created dma-buf takes ownership of the ref,
309 * but if that fails then drop the ref
310 */
311 ret = PTR_ERR(buf);
312 goto out;
313 }
314 obj->export_dma_buf = buf;
315
Dave Airlie0ff926c2012-05-20 17:31:16 +0100316 /* if we've exported this buffer the cheat and add it to the import list
317 * so we get the correct handle back
318 */
Dave Airlie219b4732013-04-22 09:54:36 +1000319 ret = drm_prime_add_buf_handle(&file_priv->prime,
320 obj->export_dma_buf, handle);
321 if (ret)
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900322 goto fail_put_dmabuf;
Dave Airlie0ff926c2012-05-20 17:31:16 +0100323
Dave Airlie219b4732013-04-22 09:54:36 +1000324 *prime_fd = dma_buf_fd(buf, flags);
Dave Airlie32488772011-11-25 15:21:02 +0000325 mutex_unlock(&file_priv->prime.lock);
326 return 0;
Dave Airlie219b4732013-04-22 09:54:36 +1000327
328out_have_obj:
329 get_dma_buf(dmabuf);
330 *prime_fd = dma_buf_fd(dmabuf, flags);
YoungJun Cho7d8f06a2013-06-26 10:21:40 +0900331 goto out;
332
333fail_put_dmabuf:
334 /* clear NOT to be checked when releasing dma_buf */
335 obj->export_dma_buf = NULL;
336 dma_buf_put(buf);
Dave Airlie219b4732013-04-22 09:54:36 +1000337out:
338 drm_gem_object_unreference_unlocked(obj);
339 mutex_unlock(&file_priv->prime.lock);
340 return ret;
Dave Airlie32488772011-11-25 15:21:02 +0000341}
342EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
343
Aaron Plattner89177642013-01-15 20:47:42 +0000344struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
345 struct dma_buf *dma_buf)
346{
347 struct dma_buf_attachment *attach;
348 struct sg_table *sgt;
349 struct drm_gem_object *obj;
350 int ret;
351
352 if (!dev->driver->gem_prime_import_sg_table)
353 return ERR_PTR(-EINVAL);
354
355 if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
356 obj = dma_buf->priv;
357 if (obj->dev == dev) {
358 /*
359 * Importing dmabuf exported from out own gem increases
360 * refcount on gem itself instead of f_count of dmabuf.
361 */
362 drm_gem_object_reference(obj);
Aaron Plattner89177642013-01-15 20:47:42 +0000363 return obj;
364 }
365 }
366
367 attach = dma_buf_attach(dma_buf, dev->dev);
368 if (IS_ERR(attach))
Thomas Meyerf2a5da42013-06-01 10:09:27 +0000369 return ERR_CAST(attach);
Aaron Plattner89177642013-01-15 20:47:42 +0000370
Imre Deak011c2282013-04-19 11:11:56 +1000371 get_dma_buf(dma_buf);
372
Aaron Plattner89177642013-01-15 20:47:42 +0000373 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
374 if (IS_ERR_OR_NULL(sgt)) {
375 ret = PTR_ERR(sgt);
376 goto fail_detach;
377 }
378
379 obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
380 if (IS_ERR(obj)) {
381 ret = PTR_ERR(obj);
382 goto fail_unmap;
383 }
384
385 obj->import_attach = attach;
386
387 return obj;
388
389fail_unmap:
390 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
391fail_detach:
392 dma_buf_detach(dma_buf, attach);
Imre Deak011c2282013-04-19 11:11:56 +1000393 dma_buf_put(dma_buf);
394
Aaron Plattner89177642013-01-15 20:47:42 +0000395 return ERR_PTR(ret);
396}
397EXPORT_SYMBOL(drm_gem_prime_import);
398
Dave Airlie32488772011-11-25 15:21:02 +0000399int drm_gem_prime_fd_to_handle(struct drm_device *dev,
400 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
401{
402 struct dma_buf *dma_buf;
403 struct drm_gem_object *obj;
404 int ret;
405
406 dma_buf = dma_buf_get(prime_fd);
407 if (IS_ERR(dma_buf))
408 return PTR_ERR(dma_buf);
409
410 mutex_lock(&file_priv->prime.lock);
411
Dave Airlie219b4732013-04-22 09:54:36 +1000412 ret = drm_prime_lookup_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000413 dma_buf, handle);
414 if (!ret) {
415 ret = 0;
416 goto out_put;
417 }
418
419 /* never seen this one, need to import */
420 obj = dev->driver->gem_prime_import(dev, dma_buf);
421 if (IS_ERR(obj)) {
422 ret = PTR_ERR(obj);
423 goto out_put;
424 }
425
426 ret = drm_gem_handle_create(file_priv, obj, handle);
427 drm_gem_object_unreference_unlocked(obj);
428 if (ret)
429 goto out_put;
430
Dave Airlie219b4732013-04-22 09:54:36 +1000431 ret = drm_prime_add_buf_handle(&file_priv->prime,
Dave Airlie32488772011-11-25 15:21:02 +0000432 dma_buf, *handle);
433 if (ret)
434 goto fail;
435
436 mutex_unlock(&file_priv->prime.lock);
Imre Deak011c2282013-04-19 11:11:56 +1000437
438 dma_buf_put(dma_buf);
439
Dave Airlie32488772011-11-25 15:21:02 +0000440 return 0;
441
442fail:
443 /* hmm, if driver attached, we are relying on the free-object path
444 * to detach.. which seems ok..
445 */
446 drm_gem_object_handle_unreference_unlocked(obj);
447out_put:
448 dma_buf_put(dma_buf);
449 mutex_unlock(&file_priv->prime.lock);
450 return ret;
451}
452EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
453
454int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
455 struct drm_file *file_priv)
456{
457 struct drm_prime_handle *args = data;
458 uint32_t flags;
459
460 if (!drm_core_check_feature(dev, DRIVER_PRIME))
461 return -EINVAL;
462
463 if (!dev->driver->prime_handle_to_fd)
464 return -ENOSYS;
465
466 /* check flags are valid */
467 if (args->flags & ~DRM_CLOEXEC)
468 return -EINVAL;
469
470 /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
471 flags = args->flags & DRM_CLOEXEC;
472
473 return dev->driver->prime_handle_to_fd(dev, file_priv,
474 args->handle, flags, &args->fd);
475}
476
477int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
478 struct drm_file *file_priv)
479{
480 struct drm_prime_handle *args = data;
481
482 if (!drm_core_check_feature(dev, DRIVER_PRIME))
483 return -EINVAL;
484
485 if (!dev->driver->prime_fd_to_handle)
486 return -ENOSYS;
487
488 return dev->driver->prime_fd_to_handle(dev, file_priv,
489 args->fd, &args->handle);
490}
491
492/*
493 * drm_prime_pages_to_sg
494 *
495 * this helper creates an sg table object from a set of pages
496 * the driver is responsible for mapping the pages into the
497 * importers address space
498 */
499struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
500{
501 struct sg_table *sg = NULL;
Dave Airlie32488772011-11-25 15:21:02 +0000502 int ret;
503
504 sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900505 if (!sg) {
506 ret = -ENOMEM;
Dave Airlie32488772011-11-25 15:21:02 +0000507 goto out;
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900508 }
Dave Airlie32488772011-11-25 15:21:02 +0000509
Rahul Sharmadca25cb2013-01-28 08:38:48 -0500510 ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
511 nr_pages << PAGE_SHIFT, GFP_KERNEL);
Dave Airlie32488772011-11-25 15:21:02 +0000512 if (ret)
513 goto out;
514
Dave Airlie32488772011-11-25 15:21:02 +0000515 return sg;
516out:
517 kfree(sg);
YoungJun Cho7e3d88f2013-06-24 16:40:53 +0900518 return ERR_PTR(ret);
Dave Airlie32488772011-11-25 15:21:02 +0000519}
520EXPORT_SYMBOL(drm_prime_pages_to_sg);
521
Dave Airlie51ab7ba2012-05-18 15:40:33 +0100522/* export an sg table into an array of pages and addresses
523 this is currently required by the TTM driver in order to do correct fault
524 handling */
525int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
526 dma_addr_t *addrs, int max_pages)
527{
528 unsigned count;
529 struct scatterlist *sg;
530 struct page *page;
531 u32 len, offset;
532 int pg_index;
533 dma_addr_t addr;
534
535 pg_index = 0;
536 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
537 len = sg->length;
538 offset = sg->offset;
539 page = sg_page(sg);
540 addr = sg_dma_address(sg);
541
542 while (len > 0) {
543 if (WARN_ON(pg_index >= max_pages))
544 return -1;
545 pages[pg_index] = page;
546 if (addrs)
547 addrs[pg_index] = addr;
548
549 page++;
550 addr += PAGE_SIZE;
551 len -= PAGE_SIZE;
552 pg_index++;
553 }
554 }
555 return 0;
556}
557EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
Dave Airlie32488772011-11-25 15:21:02 +0000558/* helper function to cleanup a GEM/prime object */
559void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
560{
561 struct dma_buf_attachment *attach;
562 struct dma_buf *dma_buf;
563 attach = obj->import_attach;
564 if (sg)
565 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
566 dma_buf = attach->dmabuf;
567 dma_buf_detach(attach->dmabuf, attach);
568 /* remove the reference */
569 dma_buf_put(dma_buf);
570}
571EXPORT_SYMBOL(drm_prime_gem_destroy);
572
573void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
574{
575 INIT_LIST_HEAD(&prime_fpriv->head);
576 mutex_init(&prime_fpriv->lock);
577}
578EXPORT_SYMBOL(drm_prime_init_file_private);
579
580void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
581{
Imre Deak98b76232013-04-24 19:04:57 +0300582 /* by now drm_gem_release should've made sure the list is empty */
583 WARN_ON(!list_empty(&prime_fpriv->head));
Dave Airlie32488772011-11-25 15:21:02 +0000584}
585EXPORT_SYMBOL(drm_prime_destroy_file_private);
586
Dave Airlie219b4732013-04-22 09:54:36 +1000587int 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 +0000588{
589 struct drm_prime_member *member;
590
591 list_for_each_entry(member, &prime_fpriv->head, entry) {
592 if (member->dma_buf == dma_buf) {
593 *handle = member->handle;
594 return 0;
595 }
596 }
597 return -ENOENT;
598}
Dave Airlie219b4732013-04-22 09:54:36 +1000599EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
Dave Airlie32488772011-11-25 15:21:02 +0000600
Dave Airlie219b4732013-04-22 09:54:36 +1000601void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
Dave Airlie32488772011-11-25 15:21:02 +0000602{
603 struct drm_prime_member *member, *safe;
604
605 mutex_lock(&prime_fpriv->lock);
606 list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
607 if (member->dma_buf == dma_buf) {
Dave Airlie219b4732013-04-22 09:54:36 +1000608 dma_buf_put(dma_buf);
Dave Airlie32488772011-11-25 15:21:02 +0000609 list_del(&member->entry);
610 kfree(member);
611 }
612 }
613 mutex_unlock(&prime_fpriv->lock);
614}
Dave Airlie219b4732013-04-22 09:54:36 +1000615EXPORT_SYMBOL(drm_prime_remove_buf_handle);