GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / gpu / drm / ttm / ttm_object.c
1 /**************************************************************************
2  *
3  * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  *
30  * While no substantial code is shared, the prime code is inspired by
31  * drm_prime.c, with
32  * Authors:
33  *      Dave Airlie <airlied@redhat.com>
34  *      Rob Clark <rob.clark@linaro.org>
35  */
36 /** @file ttm_ref_object.c
37  *
38  * Base- and reference object implementation for the various
39  * ttm objects. Implements reference counting, minimal security checks
40  * and release on file close.
41  */
42
43
44 /**
45  * struct ttm_object_file
46  *
47  * @tdev: Pointer to the ttm_object_device.
48  *
49  * @lock: Lock that protects the ref_list list and the
50  * ref_hash hash tables.
51  *
52  * @ref_list: List of ttm_ref_objects to be destroyed at
53  * file release.
54  *
55  * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
56  * for fast lookup of ref objects given a base object.
57  */
58
59 #define pr_fmt(fmt) "[TTM] " fmt
60
61 #include <drm/ttm/ttm_object.h>
62 #include <drm/ttm/ttm_module.h>
63 #include <linux/list.h>
64 #include <linux/spinlock.h>
65 #include <linux/slab.h>
66 #include <linux/module.h>
67 #include <linux/atomic.h>
68
69 struct ttm_object_file {
70         struct ttm_object_device *tdev;
71         spinlock_t lock;
72         struct list_head ref_list;
73         struct drm_open_hash ref_hash[TTM_REF_NUM];
74         struct kref refcount;
75 };
76
77 /**
78  * struct ttm_object_device
79  *
80  * @object_lock: lock that protects the object_hash hash table.
81  *
82  * @object_hash: hash table for fast lookup of object global names.
83  *
84  * @object_count: Per device object count.
85  *
86  * This is the per-device data structure needed for ttm object management.
87  */
88
89 struct ttm_object_device {
90         spinlock_t object_lock;
91         struct drm_open_hash object_hash;
92         atomic_t object_count;
93         struct ttm_mem_global *mem_glob;
94         struct dma_buf_ops ops;
95         void (*dmabuf_release)(struct dma_buf *dma_buf);
96         size_t dma_buf_size;
97 };
98
99 /**
100  * struct ttm_ref_object
101  *
102  * @hash: Hash entry for the per-file object reference hash.
103  *
104  * @head: List entry for the per-file list of ref-objects.
105  *
106  * @kref: Ref count.
107  *
108  * @obj: Base object this ref object is referencing.
109  *
110  * @ref_type: Type of ref object.
111  *
112  * This is similar to an idr object, but it also has a hash table entry
113  * that allows lookup with a pointer to the referenced object as a key. In
114  * that way, one can easily detect whether a base object is referenced by
115  * a particular ttm_object_file. It also carries a ref count to avoid creating
116  * multiple ref objects if a ttm_object_file references the same base
117  * object more than once.
118  */
119
120 struct ttm_ref_object {
121         struct rcu_head rcu_head;
122         struct drm_hash_item hash;
123         struct list_head head;
124         struct kref kref;
125         enum ttm_ref_type ref_type;
126         struct ttm_base_object *obj;
127         struct ttm_object_file *tfile;
128 };
129
130 static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
131
132 static inline struct ttm_object_file *
133 ttm_object_file_ref(struct ttm_object_file *tfile)
134 {
135         kref_get(&tfile->refcount);
136         return tfile;
137 }
138
139 static void ttm_object_file_destroy(struct kref *kref)
140 {
141         struct ttm_object_file *tfile =
142                 container_of(kref, struct ttm_object_file, refcount);
143
144         kfree(tfile);
145 }
146
147
148 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
149 {
150         struct ttm_object_file *tfile = *p_tfile;
151
152         *p_tfile = NULL;
153         kref_put(&tfile->refcount, ttm_object_file_destroy);
154 }
155
156
157 int ttm_base_object_init(struct ttm_object_file *tfile,
158                          struct ttm_base_object *base,
159                          bool shareable,
160                          enum ttm_object_type object_type,
161                          void (*refcount_release) (struct ttm_base_object **),
162                          void (*ref_obj_release) (struct ttm_base_object *,
163                                                   enum ttm_ref_type ref_type))
164 {
165         struct ttm_object_device *tdev = tfile->tdev;
166         int ret;
167
168         base->shareable = shareable;
169         base->tfile = ttm_object_file_ref(tfile);
170         base->refcount_release = refcount_release;
171         base->ref_obj_release = ref_obj_release;
172         base->object_type = object_type;
173         kref_init(&base->refcount);
174         spin_lock(&tdev->object_lock);
175         ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
176                                             &base->hash,
177                                             (unsigned long)base, 31, 0, 0);
178         spin_unlock(&tdev->object_lock);
179         if (unlikely(ret != 0))
180                 goto out_err0;
181
182         ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
183         if (unlikely(ret != 0))
184                 goto out_err1;
185
186         ttm_base_object_unref(&base);
187
188         return 0;
189 out_err1:
190         spin_lock(&tdev->object_lock);
191         (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
192         spin_unlock(&tdev->object_lock);
193 out_err0:
194         return ret;
195 }
196 EXPORT_SYMBOL(ttm_base_object_init);
197
198 static void ttm_release_base(struct kref *kref)
199 {
200         struct ttm_base_object *base =
201             container_of(kref, struct ttm_base_object, refcount);
202         struct ttm_object_device *tdev = base->tfile->tdev;
203
204         spin_lock(&tdev->object_lock);
205         (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
206         spin_unlock(&tdev->object_lock);
207
208         /*
209          * Note: We don't use synchronize_rcu() here because it's far
210          * too slow. It's up to the user to free the object using
211          * call_rcu() or ttm_base_object_kfree().
212          */
213
214         ttm_object_file_unref(&base->tfile);
215         if (base->refcount_release)
216                 base->refcount_release(&base);
217 }
218
219 void ttm_base_object_unref(struct ttm_base_object **p_base)
220 {
221         struct ttm_base_object *base = *p_base;
222
223         *p_base = NULL;
224
225         kref_put(&base->refcount, ttm_release_base);
226 }
227 EXPORT_SYMBOL(ttm_base_object_unref);
228
229 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
230                                                uint32_t key)
231 {
232         struct ttm_base_object *base = NULL;
233         struct drm_hash_item *hash;
234         struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
235         int ret;
236
237         rcu_read_lock();
238         ret = drm_ht_find_item_rcu(ht, key, &hash);
239
240         if (likely(ret == 0)) {
241                 base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
242                 if (!kref_get_unless_zero(&base->refcount))
243                         base = NULL;
244         }
245         rcu_read_unlock();
246
247         return base;
248 }
249 EXPORT_SYMBOL(ttm_base_object_lookup);
250
251 struct ttm_base_object *
252 ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
253 {
254         struct ttm_base_object *base = NULL;
255         struct drm_hash_item *hash;
256         struct drm_open_hash *ht = &tdev->object_hash;
257         int ret;
258
259         rcu_read_lock();
260         ret = drm_ht_find_item_rcu(ht, key, &hash);
261
262         if (likely(ret == 0)) {
263                 base = drm_hash_entry(hash, struct ttm_base_object, hash);
264                 if (!kref_get_unless_zero(&base->refcount))
265                         base = NULL;
266         }
267         rcu_read_unlock();
268
269         return base;
270 }
271 EXPORT_SYMBOL(ttm_base_object_lookup_for_ref);
272
273 /**
274  * ttm_ref_object_exists - Check whether a caller has a valid ref object
275  * (has opened) a base object.
276  *
277  * @tfile: Pointer to a struct ttm_object_file identifying the caller.
278  * @base: Pointer to a struct base object.
279  *
280  * Checks wether the caller identified by @tfile has put a valid USAGE
281  * reference object on the base object identified by @base.
282  */
283 bool ttm_ref_object_exists(struct ttm_object_file *tfile,
284                            struct ttm_base_object *base)
285 {
286         struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
287         struct drm_hash_item *hash;
288         struct ttm_ref_object *ref;
289
290         rcu_read_lock();
291         if (unlikely(drm_ht_find_item_rcu(ht, base->hash.key, &hash) != 0))
292                 goto out_false;
293
294         /*
295          * Verify that the ref object is really pointing to our base object.
296          * Our base object could actually be dead, and the ref object pointing
297          * to another base object with the same handle.
298          */
299         ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
300         if (unlikely(base != ref->obj))
301                 goto out_false;
302
303         /*
304          * Verify that the ref->obj pointer was actually valid!
305          */
306         rmb();
307         if (unlikely(kref_read(&ref->kref) == 0))
308                 goto out_false;
309
310         rcu_read_unlock();
311         return true;
312
313  out_false:
314         rcu_read_unlock();
315         return false;
316 }
317 EXPORT_SYMBOL(ttm_ref_object_exists);
318
319 int ttm_ref_object_add(struct ttm_object_file *tfile,
320                        struct ttm_base_object *base,
321                        enum ttm_ref_type ref_type, bool *existed,
322                        bool require_existed)
323 {
324         struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
325         struct ttm_ref_object *ref;
326         struct drm_hash_item *hash;
327         struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
328         int ret = -EINVAL;
329
330         if (base->tfile != tfile && !base->shareable)
331                 return -EPERM;
332
333         if (existed != NULL)
334                 *existed = true;
335
336         while (ret == -EINVAL) {
337                 rcu_read_lock();
338                 ret = drm_ht_find_item_rcu(ht, base->hash.key, &hash);
339
340                 if (ret == 0) {
341                         ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
342                         if (kref_get_unless_zero(&ref->kref)) {
343                                 rcu_read_unlock();
344                                 break;
345                         }
346                 }
347
348                 rcu_read_unlock();
349                 if (require_existed)
350                         return -EPERM;
351
352                 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
353                                            false, false);
354                 if (unlikely(ret != 0))
355                         return ret;
356                 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
357                 if (unlikely(ref == NULL)) {
358                         ttm_mem_global_free(mem_glob, sizeof(*ref));
359                         return -ENOMEM;
360                 }
361
362                 ref->hash.key = base->hash.key;
363                 ref->obj = base;
364                 ref->tfile = tfile;
365                 ref->ref_type = ref_type;
366                 kref_init(&ref->kref);
367
368                 spin_lock(&tfile->lock);
369                 ret = drm_ht_insert_item_rcu(ht, &ref->hash);
370
371                 if (likely(ret == 0)) {
372                         list_add_tail(&ref->head, &tfile->ref_list);
373                         kref_get(&base->refcount);
374                         spin_unlock(&tfile->lock);
375                         if (existed != NULL)
376                                 *existed = false;
377                         break;
378                 }
379
380                 spin_unlock(&tfile->lock);
381                 BUG_ON(ret != -EINVAL);
382
383                 ttm_mem_global_free(mem_glob, sizeof(*ref));
384                 kfree(ref);
385         }
386
387         return ret;
388 }
389 EXPORT_SYMBOL(ttm_ref_object_add);
390
391 static void ttm_ref_object_release(struct kref *kref)
392 {
393         struct ttm_ref_object *ref =
394             container_of(kref, struct ttm_ref_object, kref);
395         struct ttm_base_object *base = ref->obj;
396         struct ttm_object_file *tfile = ref->tfile;
397         struct drm_open_hash *ht;
398         struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
399
400         ht = &tfile->ref_hash[ref->ref_type];
401         (void)drm_ht_remove_item_rcu(ht, &ref->hash);
402         list_del(&ref->head);
403         spin_unlock(&tfile->lock);
404
405         if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
406                 base->ref_obj_release(base, ref->ref_type);
407
408         ttm_base_object_unref(&ref->obj);
409         ttm_mem_global_free(mem_glob, sizeof(*ref));
410         kfree_rcu(ref, rcu_head);
411         spin_lock(&tfile->lock);
412 }
413
414 int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
415                               unsigned long key, enum ttm_ref_type ref_type)
416 {
417         struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
418         struct ttm_ref_object *ref;
419         struct drm_hash_item *hash;
420         int ret;
421
422         spin_lock(&tfile->lock);
423         ret = drm_ht_find_item(ht, key, &hash);
424         if (unlikely(ret != 0)) {
425                 spin_unlock(&tfile->lock);
426                 return -EINVAL;
427         }
428         ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
429         kref_put(&ref->kref, ttm_ref_object_release);
430         spin_unlock(&tfile->lock);
431         return 0;
432 }
433 EXPORT_SYMBOL(ttm_ref_object_base_unref);
434
435 void ttm_object_file_release(struct ttm_object_file **p_tfile)
436 {
437         struct ttm_ref_object *ref;
438         struct list_head *list;
439         unsigned int i;
440         struct ttm_object_file *tfile = *p_tfile;
441
442         *p_tfile = NULL;
443         spin_lock(&tfile->lock);
444
445         /*
446          * Since we release the lock within the loop, we have to
447          * restart it from the beginning each time.
448          */
449
450         while (!list_empty(&tfile->ref_list)) {
451                 list = tfile->ref_list.next;
452                 ref = list_entry(list, struct ttm_ref_object, head);
453                 ttm_ref_object_release(&ref->kref);
454         }
455
456         spin_unlock(&tfile->lock);
457         for (i = 0; i < TTM_REF_NUM; ++i)
458                 drm_ht_remove(&tfile->ref_hash[i]);
459
460         ttm_object_file_unref(&tfile);
461 }
462 EXPORT_SYMBOL(ttm_object_file_release);
463
464 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
465                                              unsigned int hash_order)
466 {
467         struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
468         unsigned int i;
469         unsigned int j = 0;
470         int ret;
471
472         if (unlikely(tfile == NULL))
473                 return NULL;
474
475         spin_lock_init(&tfile->lock);
476         tfile->tdev = tdev;
477         kref_init(&tfile->refcount);
478         INIT_LIST_HEAD(&tfile->ref_list);
479
480         for (i = 0; i < TTM_REF_NUM; ++i) {
481                 ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
482                 if (ret) {
483                         j = i;
484                         goto out_err;
485                 }
486         }
487
488         return tfile;
489 out_err:
490         for (i = 0; i < j; ++i)
491                 drm_ht_remove(&tfile->ref_hash[i]);
492
493         kfree(tfile);
494
495         return NULL;
496 }
497 EXPORT_SYMBOL(ttm_object_file_init);
498
499 struct ttm_object_device *
500 ttm_object_device_init(struct ttm_mem_global *mem_glob,
501                        unsigned int hash_order,
502                        const struct dma_buf_ops *ops)
503 {
504         struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
505         int ret;
506
507         if (unlikely(tdev == NULL))
508                 return NULL;
509
510         tdev->mem_glob = mem_glob;
511         spin_lock_init(&tdev->object_lock);
512         atomic_set(&tdev->object_count, 0);
513         ret = drm_ht_create(&tdev->object_hash, hash_order);
514         if (ret != 0)
515                 goto out_no_object_hash;
516
517         tdev->ops = *ops;
518         tdev->dmabuf_release = tdev->ops.release;
519         tdev->ops.release = ttm_prime_dmabuf_release;
520         tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
521                 ttm_round_pot(sizeof(struct file));
522         return tdev;
523
524 out_no_object_hash:
525         kfree(tdev);
526         return NULL;
527 }
528 EXPORT_SYMBOL(ttm_object_device_init);
529
530 void ttm_object_device_release(struct ttm_object_device **p_tdev)
531 {
532         struct ttm_object_device *tdev = *p_tdev;
533
534         *p_tdev = NULL;
535
536         drm_ht_remove(&tdev->object_hash);
537
538         kfree(tdev);
539 }
540 EXPORT_SYMBOL(ttm_object_device_release);
541
542 /**
543  * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
544  *
545  * @dma_buf: Non-refcounted pointer to a struct dma-buf.
546  *
547  * Obtain a file reference from a lookup structure that doesn't refcount
548  * the file, but synchronizes with its release method to make sure it has
549  * not been freed yet. See for example kref_get_unless_zero documentation.
550  * Returns true if refcounting succeeds, false otherwise.
551  *
552  * Nobody really wants this as a public API yet, so let it mature here
553  * for some time...
554  */
555 static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
556 {
557         return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
558 }
559
560 /**
561  * ttm_prime_refcount_release - refcount release method for a prime object.
562  *
563  * @p_base: Pointer to ttm_base_object pointer.
564  *
565  * This is a wrapper that calls the refcount_release founction of the
566  * underlying object. At the same time it cleans up the prime object.
567  * This function is called when all references to the base object we
568  * derive from are gone.
569  */
570 static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
571 {
572         struct ttm_base_object *base = *p_base;
573         struct ttm_prime_object *prime;
574
575         *p_base = NULL;
576         prime = container_of(base, struct ttm_prime_object, base);
577         BUG_ON(prime->dma_buf != NULL);
578         mutex_destroy(&prime->mutex);
579         if (prime->refcount_release)
580                 prime->refcount_release(&base);
581 }
582
583 /**
584  * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
585  *
586  * @dma_buf:
587  *
588  * This function first calls the dma_buf release method the driver
589  * provides. Then it cleans up our dma_buf pointer used for lookup,
590  * and finally releases the reference the dma_buf has on our base
591  * object.
592  */
593 static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
594 {
595         struct ttm_prime_object *prime =
596                 (struct ttm_prime_object *) dma_buf->priv;
597         struct ttm_base_object *base = &prime->base;
598         struct ttm_object_device *tdev = base->tfile->tdev;
599
600         if (tdev->dmabuf_release)
601                 tdev->dmabuf_release(dma_buf);
602         mutex_lock(&prime->mutex);
603         if (prime->dma_buf == dma_buf)
604                 prime->dma_buf = NULL;
605         mutex_unlock(&prime->mutex);
606         ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
607         ttm_base_object_unref(&base);
608 }
609
610 /**
611  * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
612  *
613  * @tfile: A struct ttm_object_file identifying the caller.
614  * @fd: The prime / dmabuf fd.
615  * @handle: The returned handle.
616  *
617  * This function returns a handle to an object that previously exported
618  * a dma-buf. Note that we don't handle imports yet, because we simply
619  * have no consumers of that implementation.
620  */
621 int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
622                            int fd, u32 *handle)
623 {
624         struct ttm_object_device *tdev = tfile->tdev;
625         struct dma_buf *dma_buf;
626         struct ttm_prime_object *prime;
627         struct ttm_base_object *base;
628         int ret;
629
630         dma_buf = dma_buf_get(fd);
631         if (IS_ERR(dma_buf))
632                 return PTR_ERR(dma_buf);
633
634         if (dma_buf->ops != &tdev->ops)
635                 return -ENOSYS;
636
637         prime = (struct ttm_prime_object *) dma_buf->priv;
638         base = &prime->base;
639         *handle = base->hash.key;
640         ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
641
642         dma_buf_put(dma_buf);
643
644         return ret;
645 }
646 EXPORT_SYMBOL_GPL(ttm_prime_fd_to_handle);
647
648 /**
649  * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
650  *
651  * @tfile: Struct ttm_object_file identifying the caller.
652  * @handle: Handle to the object we're exporting from.
653  * @flags: flags for dma-buf creation. We just pass them on.
654  * @prime_fd: The returned file descriptor.
655  *
656  */
657 int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
658                            uint32_t handle, uint32_t flags,
659                            int *prime_fd)
660 {
661         struct ttm_object_device *tdev = tfile->tdev;
662         struct ttm_base_object *base;
663         struct dma_buf *dma_buf;
664         struct ttm_prime_object *prime;
665         int ret;
666
667         base = ttm_base_object_lookup(tfile, handle);
668         if (unlikely(base == NULL ||
669                      base->object_type != ttm_prime_type)) {
670                 ret = -ENOENT;
671                 goto out_unref;
672         }
673
674         prime = container_of(base, struct ttm_prime_object, base);
675         if (unlikely(!base->shareable)) {
676                 ret = -EPERM;
677                 goto out_unref;
678         }
679
680         ret = mutex_lock_interruptible(&prime->mutex);
681         if (unlikely(ret != 0)) {
682                 ret = -ERESTARTSYS;
683                 goto out_unref;
684         }
685
686         dma_buf = prime->dma_buf;
687         if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
688                 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
689
690                 exp_info.ops = &tdev->ops;
691                 exp_info.size = prime->size;
692                 exp_info.flags = flags;
693                 exp_info.priv = prime;
694
695                 /*
696                  * Need to create a new dma_buf, with memory accounting.
697                  */
698                 ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
699                                            false, true);
700                 if (unlikely(ret != 0)) {
701                         mutex_unlock(&prime->mutex);
702                         goto out_unref;
703                 }
704
705                 dma_buf = dma_buf_export(&exp_info);
706                 if (IS_ERR(dma_buf)) {
707                         ret = PTR_ERR(dma_buf);
708                         ttm_mem_global_free(tdev->mem_glob,
709                                             tdev->dma_buf_size);
710                         mutex_unlock(&prime->mutex);
711                         goto out_unref;
712                 }
713
714                 /*
715                  * dma_buf has taken the base object reference
716                  */
717                 base = NULL;
718                 prime->dma_buf = dma_buf;
719         }
720         mutex_unlock(&prime->mutex);
721
722         ret = dma_buf_fd(dma_buf, flags);
723         if (ret >= 0) {
724                 *prime_fd = ret;
725                 ret = 0;
726         } else
727                 dma_buf_put(dma_buf);
728
729 out_unref:
730         if (base)
731                 ttm_base_object_unref(&base);
732         return ret;
733 }
734 EXPORT_SYMBOL_GPL(ttm_prime_handle_to_fd);
735
736 /**
737  * ttm_prime_object_init - Initialize a ttm_prime_object
738  *
739  * @tfile: struct ttm_object_file identifying the caller
740  * @size: The size of the dma_bufs we export.
741  * @prime: The object to be initialized.
742  * @shareable: See ttm_base_object_init
743  * @type: See ttm_base_object_init
744  * @refcount_release: See ttm_base_object_init
745  * @ref_obj_release: See ttm_base_object_init
746  *
747  * Initializes an object which is compatible with the drm_prime model
748  * for data sharing between processes and devices.
749  */
750 int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
751                           struct ttm_prime_object *prime, bool shareable,
752                           enum ttm_object_type type,
753                           void (*refcount_release) (struct ttm_base_object **),
754                           void (*ref_obj_release) (struct ttm_base_object *,
755                                                    enum ttm_ref_type ref_type))
756 {
757         mutex_init(&prime->mutex);
758         prime->size = PAGE_ALIGN(size);
759         prime->real_type = type;
760         prime->dma_buf = NULL;
761         prime->refcount_release = refcount_release;
762         return ttm_base_object_init(tfile, &prime->base, shareable,
763                                     ttm_prime_type,
764                                     ttm_prime_refcount_release,
765                                     ref_obj_release);
766 }
767 EXPORT_SYMBOL(ttm_prime_object_init);