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