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