drm/drm-prime: cache dma_buf import context
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_prime.c
1 /*
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>
31 #include <drm/drmP.h>
32 #include <drm/drm_gem.h>
33
34 #include "drm_internal.h"
35
36 /*
37  * DMA-BUF/GEM Object references and lifetime overview:
38  *
39  * On the export the dma_buf holds a reference to the exporting GEM
40  * object. It takes this reference in handle_to_fd_ioctl, when it
41  * first calls .prime_export and stores the exporting GEM object in
42  * the dma_buf priv. This reference is released when the dma_buf
43  * object goes away in the driver .release function.
44  *
45  * On the import the importing GEM object holds a reference to the
46  * dma_buf (which in turn holds a ref to the exporting GEM object).
47  * It takes that reference in the fd_to_handle ioctl.
48  * It calls dma_buf_get, creates an attachment to it and stores the
49  * attachment in the GEM object. When this attachment is destroyed
50  * when the imported object is destroyed, we remove the attachment
51  * and drop the reference to the dma_buf.
52  *
53  * Thus the chain of references always flows in one direction
54  * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
55  *
56  * Self-importing: if userspace is using PRIME as a replacement for flink
57  * then it will get a fd->handle request for a GEM object that it created.
58  * Drivers should detect this situation and return back the gem object
59  * from the dma-buf private.  Prime will do this automatically for drivers that
60  * use the drm_gem_prime_{import,export} helpers.
61  */
62
63 struct drm_prime_member {
64         struct list_head entry;
65         struct dma_buf *dma_buf;
66         uint32_t handle;
67 };
68
69 struct drm_prime_attachment {
70         struct sg_table *sgt;
71         enum dma_data_direction dir;
72 };
73
74 struct drm_prime_callback_data {
75         struct drm_gem_object *obj;
76         struct sg_table *sgt;
77 };
78
79 static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
80                                     struct dma_buf *dma_buf, uint32_t handle)
81 {
82         struct drm_prime_member *member;
83
84         member = kmalloc(sizeof(*member), GFP_KERNEL);
85         if (!member)
86                 return -ENOMEM;
87
88         get_dma_buf(dma_buf);
89         member->dma_buf = dma_buf;
90         member->handle = handle;
91         list_add(&member->entry, &prime_fpriv->head);
92         return 0;
93 }
94
95 static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
96                                                       uint32_t handle)
97 {
98         struct drm_prime_member *member;
99
100         list_for_each_entry(member, &prime_fpriv->head, entry) {
101                 if (member->handle == handle)
102                         return member->dma_buf;
103         }
104
105         return NULL;
106 }
107
108 static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
109                                        struct dma_buf *dma_buf,
110                                        uint32_t *handle)
111 {
112         struct drm_prime_member *member;
113
114         list_for_each_entry(member, &prime_fpriv->head, entry) {
115                 if (member->dma_buf == dma_buf) {
116                         *handle = member->handle;
117                         return 0;
118                 }
119         }
120         return -ENOENT;
121 }
122
123 static int drm_gem_map_attach(struct dma_buf *dma_buf,
124                               struct device *target_dev,
125                               struct dma_buf_attachment *attach)
126 {
127         struct drm_prime_attachment *prime_attach;
128         struct drm_gem_object *obj = dma_buf->priv;
129         struct drm_device *dev = obj->dev;
130
131         prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
132         if (!prime_attach)
133                 return -ENOMEM;
134
135         prime_attach->dir = DMA_NONE;
136         attach->priv = prime_attach;
137
138         if (!dev->driver->gem_prime_pin)
139                 return 0;
140
141         return dev->driver->gem_prime_pin(obj);
142 }
143
144 static void drm_gem_map_detach(struct dma_buf *dma_buf,
145                                struct dma_buf_attachment *attach)
146 {
147         struct drm_prime_attachment *prime_attach = attach->priv;
148         struct drm_gem_object *obj = dma_buf->priv;
149         struct drm_device *dev = obj->dev;
150         struct sg_table *sgt;
151
152         if (dev->driver->gem_prime_unpin)
153                 dev->driver->gem_prime_unpin(obj);
154
155         if (!prime_attach)
156                 return;
157
158         sgt = prime_attach->sgt;
159         if (sgt) {
160                 if (prime_attach->dir != DMA_NONE)
161                         dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
162                                         prime_attach->dir);
163                 sg_free_table(sgt);
164         }
165
166         kfree(sgt);
167         kfree(prime_attach);
168         attach->priv = NULL;
169 }
170
171 void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
172                                         struct dma_buf *dma_buf)
173 {
174         struct drm_prime_member *member, *safe;
175
176         list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
177                 if (member->dma_buf == dma_buf) {
178                         dma_buf_put(dma_buf);
179                         list_del(&member->entry);
180                         kfree(member);
181                 }
182         }
183 }
184
185 static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
186                                             enum dma_data_direction dir)
187 {
188         struct drm_prime_attachment *prime_attach = attach->priv;
189         struct drm_gem_object *obj = attach->dmabuf->priv;
190         struct sg_table *sgt;
191
192         if (WARN_ON(dir == DMA_NONE || !prime_attach))
193                 return ERR_PTR(-EINVAL);
194
195         /* return the cached mapping when possible */
196         if (prime_attach->dir == dir)
197                 return prime_attach->sgt;
198
199         /*
200          * two mappings with different directions for the same attachment are
201          * not allowed
202          */
203         if (WARN_ON(prime_attach->dir != DMA_NONE))
204                 return ERR_PTR(-EBUSY);
205
206         sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
207
208         if (!IS_ERR(sgt)) {
209                 if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
210                         sg_free_table(sgt);
211                         kfree(sgt);
212                         sgt = ERR_PTR(-ENOMEM);
213                 } else {
214                         prime_attach->sgt = sgt;
215                         prime_attach->dir = dir;
216                 }
217         }
218
219         return sgt;
220 }
221
222 static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
223                                   struct sg_table *sgt,
224                                   enum dma_data_direction dir)
225 {
226         /* nothing to be done here */
227 }
228
229 /**
230  * drm_gem_dmabuf_release - dma_buf release implementation for GEM
231  * @dma_buf: buffer to be released
232  *
233  * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
234  * must use this in their dma_buf ops structure as the release callback.
235  */
236 void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
237 {
238         struct drm_gem_object *obj = dma_buf->priv;
239
240         /* drop the reference on the export fd holds */
241         drm_gem_object_unreference_unlocked(obj);
242 }
243 EXPORT_SYMBOL(drm_gem_dmabuf_release);
244
245 static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
246 {
247         struct drm_gem_object *obj = dma_buf->priv;
248         struct drm_device *dev = obj->dev;
249
250         return dev->driver->gem_prime_vmap(obj);
251 }
252
253 static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
254 {
255         struct drm_gem_object *obj = dma_buf->priv;
256         struct drm_device *dev = obj->dev;
257
258         dev->driver->gem_prime_vunmap(obj, vaddr);
259 }
260
261 static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
262                                         unsigned long page_num)
263 {
264         return NULL;
265 }
266
267 static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
268                                          unsigned long page_num, void *addr)
269 {
270
271 }
272 static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
273                                  unsigned long page_num)
274 {
275         return NULL;
276 }
277
278 static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
279                                   unsigned long page_num, void *addr)
280 {
281
282 }
283
284 static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
285                                struct vm_area_struct *vma)
286 {
287         struct drm_gem_object *obj = dma_buf->priv;
288         struct drm_device *dev = obj->dev;
289
290         if (!dev->driver->gem_prime_mmap)
291                 return -ENOSYS;
292
293         return dev->driver->gem_prime_mmap(obj, vma);
294 }
295
296 static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
297         .attach = drm_gem_map_attach,
298         .detach = drm_gem_map_detach,
299         .map_dma_buf = drm_gem_map_dma_buf,
300         .unmap_dma_buf = drm_gem_unmap_dma_buf,
301         .release = drm_gem_dmabuf_release,
302         .kmap = drm_gem_dmabuf_kmap,
303         .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
304         .kunmap = drm_gem_dmabuf_kunmap,
305         .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
306         .mmap = drm_gem_dmabuf_mmap,
307         .vmap = drm_gem_dmabuf_vmap,
308         .vunmap = drm_gem_dmabuf_vunmap,
309 };
310
311 /**
312  * DOC: PRIME Helpers
313  *
314  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
315  * simpler APIs by using the helper functions @drm_gem_prime_export and
316  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
317  * six lower-level driver callbacks:
318  *
319  * Export callbacks:
320  *
321  *  - @gem_prime_pin (optional): prepare a GEM object for exporting
322  *
323  *  - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
324  *
325  *  - @gem_prime_vmap: vmap a buffer exported by your driver
326  *
327  *  - @gem_prime_vunmap: vunmap a buffer exported by your driver
328  *
329  *  - @gem_prime_mmap (optional): mmap a buffer exported by your driver
330  *
331  * Import callback:
332  *
333  *  - @gem_prime_import_sg_table (import): produce a GEM object from another
334  *    driver's scatter/gather table
335  */
336
337 /**
338  * drm_gem_prime_export - helper library implementation of the export callback
339  * @dev: drm_device to export from
340  * @obj: GEM object to export
341  * @flags: flags like DRM_CLOEXEC and DRM_RDWR
342  *
343  * This is the implementation of the gem_prime_export functions for GEM drivers
344  * using the PRIME helpers.
345  */
346 struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
347                                      struct drm_gem_object *obj,
348                                      int flags)
349 {
350         struct dma_buf_export_info exp_info = {
351                 .exp_name = KBUILD_MODNAME, /* white lie for debug */
352                 .owner = dev->driver->fops->owner,
353                 .ops = &drm_gem_prime_dmabuf_ops,
354                 .size = obj->size,
355                 .flags = flags,
356                 .priv = obj,
357         };
358
359         if (dev->driver->gem_prime_res_obj)
360                 exp_info.resv = dev->driver->gem_prime_res_obj(obj);
361
362         return dma_buf_export(&exp_info);
363 }
364 EXPORT_SYMBOL(drm_gem_prime_export);
365
366 static struct dma_buf *export_and_register_object(struct drm_device *dev,
367                                                   struct drm_gem_object *obj,
368                                                   uint32_t flags)
369 {
370         struct dma_buf *dmabuf;
371
372         /* prevent races with concurrent gem_close. */
373         if (obj->handle_count == 0) {
374                 dmabuf = ERR_PTR(-ENOENT);
375                 return dmabuf;
376         }
377
378         dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
379         if (IS_ERR(dmabuf)) {
380                 /* normally the created dma-buf takes ownership of the ref,
381                  * but if that fails then drop the ref
382                  */
383                 return dmabuf;
384         }
385
386         /*
387          * Note that callers do not need to clean up the export cache
388          * since the check for obj->handle_count guarantees that someone
389          * will clean it up.
390          */
391         obj->dma_buf = dmabuf;
392         get_dma_buf(obj->dma_buf);
393         /* Grab a new ref since the callers is now used by the dma-buf */
394         drm_gem_object_reference(obj);
395
396         return dmabuf;
397 }
398
399 /**
400  * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
401  * @dev: dev to export the buffer from
402  * @file_priv: drm file-private structure
403  * @handle: buffer handle to export
404  * @flags: flags like DRM_CLOEXEC
405  * @prime_fd: pointer to storage for the fd id of the create dma-buf
406  *
407  * This is the PRIME export function which must be used mandatorily by GEM
408  * drivers to ensure correct lifetime management of the underlying GEM object.
409  * The actual exporting from GEM object to a dma-buf is done through the
410  * gem_prime_export driver callback.
411  */
412 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
413                                struct drm_file *file_priv, uint32_t handle,
414                                uint32_t flags,
415                                int *prime_fd)
416 {
417         struct drm_gem_object *obj;
418         int ret = 0;
419         struct dma_buf *dmabuf;
420
421         mutex_lock(&file_priv->prime.lock);
422         obj = drm_gem_object_lookup(dev, file_priv, handle);
423         if (!obj)  {
424                 ret = -ENOENT;
425                 goto out_unlock;
426         }
427
428         dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
429         if (dmabuf) {
430                 get_dma_buf(dmabuf);
431                 goto out_have_handle;
432         }
433
434         mutex_lock(&dev->object_name_lock);
435         /* re-export the original imported object */
436         if (obj->import_attach) {
437                 dmabuf = obj->import_attach->dmabuf;
438                 get_dma_buf(dmabuf);
439                 goto out_have_obj;
440         }
441
442         if (obj->dma_buf) {
443                 get_dma_buf(obj->dma_buf);
444                 dmabuf = obj->dma_buf;
445                 goto out_have_obj;
446         }
447
448         dmabuf = export_and_register_object(dev, obj, flags);
449         if (IS_ERR(dmabuf)) {
450                 /* normally the created dma-buf takes ownership of the ref,
451                  * but if that fails then drop the ref
452                  */
453                 ret = PTR_ERR(dmabuf);
454                 mutex_unlock(&dev->object_name_lock);
455                 goto out;
456         }
457
458 out_have_obj:
459         /*
460          * If we've exported this buffer then cheat and add it to the import list
461          * so we get the correct handle back. We must do this under the
462          * protection of dev->object_name_lock to ensure that a racing gem close
463          * ioctl doesn't miss to remove this buffer handle from the cache.
464          */
465         ret = drm_prime_add_buf_handle(&file_priv->prime,
466                                        dmabuf, handle);
467         mutex_unlock(&dev->object_name_lock);
468         if (ret)
469                 goto fail_put_dmabuf;
470
471 out_have_handle:
472         ret = dma_buf_fd(dmabuf, flags);
473         /*
474          * We must _not_ remove the buffer from the handle cache since the newly
475          * created dma buf is already linked in the global obj->dma_buf pointer,
476          * and that is invariant as long as a userspace gem handle exists.
477          * Closing the handle will clean out the cache anyway, so we don't leak.
478          */
479         if (ret < 0) {
480                 goto fail_put_dmabuf;
481         } else {
482                 *prime_fd = ret;
483                 ret = 0;
484         }
485
486         goto out;
487
488 fail_put_dmabuf:
489         dma_buf_put(dmabuf);
490 out:
491         drm_gem_object_unreference_unlocked(obj);
492 out_unlock:
493         mutex_unlock(&file_priv->prime.lock);
494
495         return ret;
496 }
497 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
498
499 static void drm_gem_prime_dmabuf_release_callback(void *data)
500 {
501         struct drm_prime_callback_data *cb_data = data;
502
503         if (cb_data && cb_data->obj && cb_data->obj->import_attach) {
504                 struct dma_buf_attachment *attach = cb_data->obj->import_attach;
505                 struct sg_table *sgt = cb_data->sgt;
506
507                 if (sgt)
508                         dma_buf_unmap_attachment(attach, sgt,
509                                                  DMA_BIDIRECTIONAL);
510                 dma_buf_detach(attach->dmabuf, attach);
511                 drm_gem_object_unreference_unlocked(cb_data->obj);
512                 kfree(cb_data);
513         }
514 }
515
516 /**
517  * drm_gem_prime_import - helper library implementation of the import callback
518  * @dev: drm_device to import into
519  * @dma_buf: dma-buf object to import
520  *
521  * This is the implementation of the gem_prime_import functions for GEM drivers
522  * using the PRIME helpers.
523  */
524 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
525                                             struct dma_buf *dma_buf)
526 {
527         struct dma_buf_attachment *attach;
528         struct sg_table *sgt;
529         struct drm_gem_object *obj;
530         struct drm_prime_callback_data *cb_data;
531         int ret;
532
533         if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
534                 obj = dma_buf->priv;
535                 if (obj->dev == dev) {
536                         /*
537                          * Importing dmabuf exported from out own gem increases
538                          * refcount on gem itself instead of f_count of dmabuf.
539                          */
540                         drm_gem_object_reference(obj);
541                         return obj;
542                 }
543         }
544
545         cb_data = dma_buf_get_release_callback_data(dma_buf,
546                                         drm_gem_prime_dmabuf_release_callback);
547         if (cb_data && cb_data->obj && cb_data->obj->dev == dev) {
548                 drm_gem_object_reference(cb_data->obj);
549                 return cb_data->obj;
550         }
551
552         if (!dev->driver->gem_prime_import_sg_table)
553                 return ERR_PTR(-EINVAL);
554
555         attach = dma_buf_attach(dma_buf, dev->dev);
556         if (IS_ERR(attach))
557                 return ERR_CAST(attach);
558
559         get_dma_buf(dma_buf);
560         cb_data = kmalloc(sizeof(*cb_data), GFP_KERNEL);
561         if (!cb_data) {
562                 ret = -ENOMEM;
563                 goto fail_detach;
564         }
565
566         sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
567         if (IS_ERR(sgt)) {
568                 ret = PTR_ERR(sgt);
569                 goto fail_free;
570         }
571
572         obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
573         if (IS_ERR(obj)) {
574                 ret = PTR_ERR(obj);
575                 goto fail_unmap;
576         }
577         obj->import_attach = attach;
578         cb_data->obj = obj;
579         cb_data->sgt = sgt;
580         dma_buf_set_release_callback(dma_buf,
581                         drm_gem_prime_dmabuf_release_callback, cb_data);
582         dma_buf_put(dma_buf);
583         drm_gem_object_reference(obj);
584
585         return obj;
586
587 fail_unmap:
588         dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
589 fail_free:
590         kfree(cb_data);
591 fail_detach:
592         dma_buf_detach(dma_buf, attach);
593         dma_buf_put(dma_buf);
594
595         return ERR_PTR(ret);
596 }
597 EXPORT_SYMBOL(drm_gem_prime_import);
598
599 /**
600  * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
601  * @dev: dev to export the buffer from
602  * @file_priv: drm file-private structure
603  * @prime_fd: fd id of the dma-buf which should be imported
604  * @handle: pointer to storage for the handle of the imported buffer object
605  *
606  * This is the PRIME import function which must be used mandatorily by GEM
607  * drivers to ensure correct lifetime management of the underlying GEM object.
608  * The actual importing of GEM object from the dma-buf is done through the
609  * gem_import_export driver callback.
610  */
611 int drm_gem_prime_fd_to_handle(struct drm_device *dev,
612                                struct drm_file *file_priv, int prime_fd,
613                                uint32_t *handle)
614 {
615         struct dma_buf *dma_buf;
616         struct drm_gem_object *obj;
617         int ret;
618
619         dma_buf = dma_buf_get(prime_fd);
620         if (IS_ERR(dma_buf))
621                 return PTR_ERR(dma_buf);
622
623         mutex_lock(&file_priv->prime.lock);
624
625         ret = drm_prime_lookup_buf_handle(&file_priv->prime,
626                         dma_buf, handle);
627         if (ret == 0)
628                 goto out_put;
629
630         /* never seen this one, need to import */
631         mutex_lock(&dev->object_name_lock);
632         obj = dev->driver->gem_prime_import(dev, dma_buf);
633         if (IS_ERR(obj)) {
634                 ret = PTR_ERR(obj);
635                 goto out_unlock;
636         }
637
638         if (obj->dma_buf) {
639                 WARN_ON(obj->dma_buf != dma_buf);
640         } else {
641                 obj->dma_buf = dma_buf;
642                 get_dma_buf(dma_buf);
643         }
644
645         /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
646         ret = drm_gem_handle_create_tail(file_priv, obj, handle);
647         drm_gem_object_unreference_unlocked(obj);
648         if (ret)
649                 goto out_put;
650
651         ret = drm_prime_add_buf_handle(&file_priv->prime,
652                         dma_buf, *handle);
653         if (ret)
654                 goto fail;
655
656         mutex_unlock(&file_priv->prime.lock);
657
658         dma_buf_put(dma_buf);
659
660         return 0;
661
662 fail:
663         /* hmm, if driver attached, we are relying on the free-object path
664          * to detach.. which seems ok..
665          */
666         drm_gem_handle_delete(file_priv, *handle);
667 out_unlock:
668         mutex_unlock(&dev->object_name_lock);
669 out_put:
670         dma_buf_put(dma_buf);
671         mutex_unlock(&file_priv->prime.lock);
672         return ret;
673 }
674 EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
675
676 int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
677                                  struct drm_file *file_priv)
678 {
679         struct drm_prime_handle *args = data;
680
681         if (!drm_core_check_feature(dev, DRIVER_PRIME))
682                 return -EINVAL;
683
684         if (!dev->driver->prime_handle_to_fd)
685                 return -ENOSYS;
686
687         /* check flags are valid */
688         if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
689                 return -EINVAL;
690
691         return dev->driver->prime_handle_to_fd(dev, file_priv,
692                         args->handle, args->flags, &args->fd);
693 }
694
695 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
696                                  struct drm_file *file_priv)
697 {
698         struct drm_prime_handle *args = data;
699
700         if (!drm_core_check_feature(dev, DRIVER_PRIME))
701                 return -EINVAL;
702
703         if (!dev->driver->prime_fd_to_handle)
704                 return -ENOSYS;
705
706         return dev->driver->prime_fd_to_handle(dev, file_priv,
707                         args->fd, &args->handle);
708 }
709
710 /**
711  * drm_prime_pages_to_sg - converts a page array into an sg list
712  * @pages: pointer to the array of page pointers to convert
713  * @nr_pages: length of the page vector
714  *
715  * This helper creates an sg table object from a set of pages
716  * the driver is responsible for mapping the pages into the
717  * importers address space for use with dma_buf itself.
718  */
719 struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
720 {
721         struct sg_table *sg = NULL;
722         int ret;
723
724         sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
725         if (!sg) {
726                 ret = -ENOMEM;
727                 goto out;
728         }
729
730         ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
731                                 nr_pages << PAGE_SHIFT, GFP_KERNEL);
732         if (ret)
733                 goto out;
734
735         return sg;
736 out:
737         kfree(sg);
738         return ERR_PTR(ret);
739 }
740 EXPORT_SYMBOL(drm_prime_pages_to_sg);
741
742 /**
743  * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
744  * @sgt: scatter-gather table to convert
745  * @pages: array of page pointers to store the page array in
746  * @addrs: optional array to store the dma bus address of each page
747  * @max_pages: size of both the passed-in arrays
748  *
749  * Exports an sg table into an array of pages and addresses. This is currently
750  * required by the TTM driver in order to do correct fault handling.
751  */
752 int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
753                                      dma_addr_t *addrs, int max_pages)
754 {
755         unsigned count;
756         struct scatterlist *sg;
757         struct page *page;
758         u32 len;
759         int pg_index;
760         dma_addr_t addr;
761
762         pg_index = 0;
763         for_each_sg(sgt->sgl, sg, sgt->nents, count) {
764                 len = sg->length;
765                 page = sg_page(sg);
766                 addr = sg_dma_address(sg);
767
768                 while (len > 0) {
769                         if (WARN_ON(pg_index >= max_pages))
770                                 return -1;
771                         pages[pg_index] = page;
772                         if (addrs)
773                                 addrs[pg_index] = addr;
774
775                         page++;
776                         addr += PAGE_SIZE;
777                         len -= PAGE_SIZE;
778                         pg_index++;
779                 }
780         }
781         return 0;
782 }
783 EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
784
785 /**
786  * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
787  * @obj: GEM object which was created from a dma-buf
788  * @sg: the sg-table which was pinned at import time
789  *
790  * This is the cleanup functions which GEM drivers need to call when they use
791  * @drm_gem_prime_import to import dma-bufs.
792  */
793 void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
794 {
795         struct dma_buf_attachment *attach;
796         struct dma_buf *dma_buf;
797         attach = obj->import_attach;
798         if (sg)
799                 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
800         dma_buf = attach->dmabuf;
801         dma_buf_detach(attach->dmabuf, attach);
802         /* remove the reference */
803         dma_buf_put(dma_buf);
804 }
805 EXPORT_SYMBOL(drm_prime_gem_destroy);
806
807 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
808 {
809         INIT_LIST_HEAD(&prime_fpriv->head);
810         mutex_init(&prime_fpriv->lock);
811 }
812
813 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
814 {
815         /* by now drm_gem_release should've made sure the list is empty */
816         WARN_ON(!list_empty(&prime_fpriv->head));
817 }