1*e3e4e02dSriastradh /*	$NetBSD: ttm_object.c,v 1.3 2022/02/17 01:21:02 riastradh Exp $	*/
21571a7a1Sriastradh 
31571a7a1Sriastradh /* SPDX-License-Identifier: GPL-2.0 OR MIT */
41571a7a1Sriastradh /**************************************************************************
51571a7a1Sriastradh  *
61571a7a1Sriastradh  * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
71571a7a1Sriastradh  * All Rights Reserved.
81571a7a1Sriastradh  *
91571a7a1Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
101571a7a1Sriastradh  * copy of this software and associated documentation files (the
111571a7a1Sriastradh  * "Software"), to deal in the Software without restriction, including
121571a7a1Sriastradh  * without limitation the rights to use, copy, modify, merge, publish,
131571a7a1Sriastradh  * distribute, sub license, and/or sell copies of the Software, and to
141571a7a1Sriastradh  * permit persons to whom the Software is furnished to do so, subject to
151571a7a1Sriastradh  * the following conditions:
161571a7a1Sriastradh  *
171571a7a1Sriastradh  * The above copyright notice and this permission notice (including the
181571a7a1Sriastradh  * next paragraph) shall be included in all copies or substantial portions
191571a7a1Sriastradh  * of the Software.
201571a7a1Sriastradh  *
211571a7a1Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
221571a7a1Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
231571a7a1Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
241571a7a1Sriastradh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
251571a7a1Sriastradh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
261571a7a1Sriastradh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
271571a7a1Sriastradh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
281571a7a1Sriastradh  *
291571a7a1Sriastradh  **************************************************************************/
301571a7a1Sriastradh /*
311571a7a1Sriastradh  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
321571a7a1Sriastradh  *
331571a7a1Sriastradh  * While no substantial code is shared, the prime code is inspired by
341571a7a1Sriastradh  * drm_prime.c, with
351571a7a1Sriastradh  * Authors:
361571a7a1Sriastradh  *      Dave Airlie <airlied@redhat.com>
371571a7a1Sriastradh  *      Rob Clark <rob.clark@linaro.org>
381571a7a1Sriastradh  */
391571a7a1Sriastradh /** @file ttm_ref_object.c
401571a7a1Sriastradh  *
411571a7a1Sriastradh  * Base- and reference object implementation for the various
421571a7a1Sriastradh  * ttm objects. Implements reference counting, minimal security checks
431571a7a1Sriastradh  * and release on file close.
441571a7a1Sriastradh  */
451571a7a1Sriastradh 
461571a7a1Sriastradh 
471571a7a1Sriastradh /**
481571a7a1Sriastradh  * struct ttm_object_file
491571a7a1Sriastradh  *
501571a7a1Sriastradh  * @tdev: Pointer to the ttm_object_device.
511571a7a1Sriastradh  *
521571a7a1Sriastradh  * @lock: Lock that protects the ref_list list and the
531571a7a1Sriastradh  * ref_hash hash tables.
541571a7a1Sriastradh  *
551571a7a1Sriastradh  * @ref_list: List of ttm_ref_objects to be destroyed at
561571a7a1Sriastradh  * file release.
571571a7a1Sriastradh  *
581571a7a1Sriastradh  * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
591571a7a1Sriastradh  * for fast lookup of ref objects given a base object.
601571a7a1Sriastradh  */
611571a7a1Sriastradh 
621571a7a1Sriastradh #include <sys/cdefs.h>
63*e3e4e02dSriastradh __KERNEL_RCSID(0, "$NetBSD: ttm_object.c,v 1.3 2022/02/17 01:21:02 riastradh Exp $");
641571a7a1Sriastradh 
651571a7a1Sriastradh #define pr_fmt(fmt) "[TTM] " fmt
661571a7a1Sriastradh 
671571a7a1Sriastradh #include <drm/ttm/ttm_module.h>
681571a7a1Sriastradh #include <linux/list.h>
691571a7a1Sriastradh #include <linux/spinlock.h>
701571a7a1Sriastradh #include <linux/slab.h>
711571a7a1Sriastradh #include <linux/atomic.h>
721571a7a1Sriastradh #include "ttm_object.h"
731571a7a1Sriastradh 
74*e3e4e02dSriastradh #include <linux/nbsd-namespace.h>
75*e3e4e02dSriastradh 
761571a7a1Sriastradh struct ttm_object_file {
771571a7a1Sriastradh 	struct ttm_object_device *tdev;
781571a7a1Sriastradh 	spinlock_t lock;
791571a7a1Sriastradh 	struct list_head ref_list;
801571a7a1Sriastradh 	struct drm_open_hash ref_hash[TTM_REF_NUM];
811571a7a1Sriastradh 	struct kref refcount;
821571a7a1Sriastradh };
831571a7a1Sriastradh 
841571a7a1Sriastradh /**
851571a7a1Sriastradh  * struct ttm_object_device
861571a7a1Sriastradh  *
871571a7a1Sriastradh  * @object_lock: lock that protects the object_hash hash table.
881571a7a1Sriastradh  *
891571a7a1Sriastradh  * @object_hash: hash table for fast lookup of object global names.
901571a7a1Sriastradh  *
911571a7a1Sriastradh  * @object_count: Per device object count.
921571a7a1Sriastradh  *
931571a7a1Sriastradh  * This is the per-device data structure needed for ttm object management.
941571a7a1Sriastradh  */
951571a7a1Sriastradh 
961571a7a1Sriastradh struct ttm_object_device {
971571a7a1Sriastradh 	spinlock_t object_lock;
981571a7a1Sriastradh 	struct drm_open_hash object_hash;
991571a7a1Sriastradh 	atomic_t object_count;
1001571a7a1Sriastradh 	struct ttm_mem_global *mem_glob;
1011571a7a1Sriastradh 	struct dma_buf_ops ops;
1021571a7a1Sriastradh 	void (*dmabuf_release)(struct dma_buf *dma_buf);
1031571a7a1Sriastradh 	size_t dma_buf_size;
1041571a7a1Sriastradh 	struct idr idr;
1051571a7a1Sriastradh };
1061571a7a1Sriastradh 
1071571a7a1Sriastradh /**
1081571a7a1Sriastradh  * struct ttm_ref_object
1091571a7a1Sriastradh  *
1101571a7a1Sriastradh  * @hash: Hash entry for the per-file object reference hash.
1111571a7a1Sriastradh  *
1121571a7a1Sriastradh  * @head: List entry for the per-file list of ref-objects.
1131571a7a1Sriastradh  *
1141571a7a1Sriastradh  * @kref: Ref count.
1151571a7a1Sriastradh  *
1161571a7a1Sriastradh  * @obj: Base object this ref object is referencing.
1171571a7a1Sriastradh  *
1181571a7a1Sriastradh  * @ref_type: Type of ref object.
1191571a7a1Sriastradh  *
1201571a7a1Sriastradh  * This is similar to an idr object, but it also has a hash table entry
1211571a7a1Sriastradh  * that allows lookup with a pointer to the referenced object as a key. In
1221571a7a1Sriastradh  * that way, one can easily detect whether a base object is referenced by
1231571a7a1Sriastradh  * a particular ttm_object_file. It also carries a ref count to avoid creating
1241571a7a1Sriastradh  * multiple ref objects if a ttm_object_file references the same base
1251571a7a1Sriastradh  * object more than once.
1261571a7a1Sriastradh  */
1271571a7a1Sriastradh 
1281571a7a1Sriastradh struct ttm_ref_object {
1291571a7a1Sriastradh 	struct rcu_head rcu_head;
1301571a7a1Sriastradh 	struct drm_hash_item hash;
1311571a7a1Sriastradh 	struct list_head head;
1321571a7a1Sriastradh 	struct kref kref;
1331571a7a1Sriastradh 	enum ttm_ref_type ref_type;
1341571a7a1Sriastradh 	struct ttm_base_object *obj;
1351571a7a1Sriastradh 	struct ttm_object_file *tfile;
1361571a7a1Sriastradh };
1371571a7a1Sriastradh 
1381571a7a1Sriastradh static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
1391571a7a1Sriastradh 
1401571a7a1Sriastradh static inline struct ttm_object_file *
ttm_object_file_ref(struct ttm_object_file * tfile)1411571a7a1Sriastradh ttm_object_file_ref(struct ttm_object_file *tfile)
1421571a7a1Sriastradh {
1431571a7a1Sriastradh 	kref_get(&tfile->refcount);
1441571a7a1Sriastradh 	return tfile;
1451571a7a1Sriastradh }
1461571a7a1Sriastradh 
ttm_object_file_destroy(struct kref * kref)1471571a7a1Sriastradh static void ttm_object_file_destroy(struct kref *kref)
1481571a7a1Sriastradh {
1491571a7a1Sriastradh 	struct ttm_object_file *tfile =
1501571a7a1Sriastradh 		container_of(kref, struct ttm_object_file, refcount);
1511571a7a1Sriastradh 
1521571a7a1Sriastradh 	kfree(tfile);
1531571a7a1Sriastradh }
1541571a7a1Sriastradh 
1551571a7a1Sriastradh 
ttm_object_file_unref(struct ttm_object_file ** p_tfile)1561571a7a1Sriastradh static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
1571571a7a1Sriastradh {
1581571a7a1Sriastradh 	struct ttm_object_file *tfile = *p_tfile;
1591571a7a1Sriastradh 
1601571a7a1Sriastradh 	*p_tfile = NULL;
1611571a7a1Sriastradh 	kref_put(&tfile->refcount, ttm_object_file_destroy);
1621571a7a1Sriastradh }
1631571a7a1Sriastradh 
1641571a7a1Sriastradh 
ttm_base_object_init(struct ttm_object_file * tfile,struct ttm_base_object * base,bool shareable,enum ttm_object_type object_type,void (* refcount_release)(struct ttm_base_object **),void (* ref_obj_release)(struct ttm_base_object *,enum ttm_ref_type ref_type))1651571a7a1Sriastradh int ttm_base_object_init(struct ttm_object_file *tfile,
1661571a7a1Sriastradh 			 struct ttm_base_object *base,
1671571a7a1Sriastradh 			 bool shareable,
1681571a7a1Sriastradh 			 enum ttm_object_type object_type,
1691571a7a1Sriastradh 			 void (*refcount_release) (struct ttm_base_object **),
1701571a7a1Sriastradh 			 void (*ref_obj_release) (struct ttm_base_object *,
1711571a7a1Sriastradh 						  enum ttm_ref_type ref_type))
1721571a7a1Sriastradh {
1731571a7a1Sriastradh 	struct ttm_object_device *tdev = tfile->tdev;
1741571a7a1Sriastradh 	int ret;
1751571a7a1Sriastradh 
1761571a7a1Sriastradh 	base->shareable = shareable;
1771571a7a1Sriastradh 	base->tfile = ttm_object_file_ref(tfile);
1781571a7a1Sriastradh 	base->refcount_release = refcount_release;
1791571a7a1Sriastradh 	base->ref_obj_release = ref_obj_release;
1801571a7a1Sriastradh 	base->object_type = object_type;
1811571a7a1Sriastradh 	kref_init(&base->refcount);
1821571a7a1Sriastradh 	idr_preload(GFP_KERNEL);
1831571a7a1Sriastradh 	spin_lock(&tdev->object_lock);
1841571a7a1Sriastradh 	ret = idr_alloc(&tdev->idr, base, 1, 0, GFP_NOWAIT);
1851571a7a1Sriastradh 	spin_unlock(&tdev->object_lock);
1861571a7a1Sriastradh 	idr_preload_end();
1871571a7a1Sriastradh 	if (ret < 0)
1881571a7a1Sriastradh 		return ret;
1891571a7a1Sriastradh 
1901571a7a1Sriastradh 	base->handle = ret;
1911571a7a1Sriastradh 	ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
1921571a7a1Sriastradh 	if (unlikely(ret != 0))
1931571a7a1Sriastradh 		goto out_err1;
1941571a7a1Sriastradh 
1951571a7a1Sriastradh 	ttm_base_object_unref(&base);
1961571a7a1Sriastradh 
1971571a7a1Sriastradh 	return 0;
1981571a7a1Sriastradh out_err1:
1991571a7a1Sriastradh 	spin_lock(&tdev->object_lock);
2001571a7a1Sriastradh 	idr_remove(&tdev->idr, base->handle);
2011571a7a1Sriastradh 	spin_unlock(&tdev->object_lock);
2021571a7a1Sriastradh 	return ret;
2031571a7a1Sriastradh }
2041571a7a1Sriastradh 
ttm_release_base(struct kref * kref)2051571a7a1Sriastradh static void ttm_release_base(struct kref *kref)
2061571a7a1Sriastradh {
2071571a7a1Sriastradh 	struct ttm_base_object *base =
2081571a7a1Sriastradh 	    container_of(kref, struct ttm_base_object, refcount);
2091571a7a1Sriastradh 	struct ttm_object_device *tdev = base->tfile->tdev;
2101571a7a1Sriastradh 
2111571a7a1Sriastradh 	spin_lock(&tdev->object_lock);
2121571a7a1Sriastradh 	idr_remove(&tdev->idr, base->handle);
2131571a7a1Sriastradh 	spin_unlock(&tdev->object_lock);
2141571a7a1Sriastradh 
2151571a7a1Sriastradh 	/*
2161571a7a1Sriastradh 	 * Note: We don't use synchronize_rcu() here because it's far
2171571a7a1Sriastradh 	 * too slow. It's up to the user to free the object using
2181571a7a1Sriastradh 	 * call_rcu() or ttm_base_object_kfree().
2191571a7a1Sriastradh 	 */
2201571a7a1Sriastradh 
2211571a7a1Sriastradh 	ttm_object_file_unref(&base->tfile);
2221571a7a1Sriastradh 	if (base->refcount_release)
2231571a7a1Sriastradh 		base->refcount_release(&base);
2241571a7a1Sriastradh }
2251571a7a1Sriastradh 
ttm_base_object_unref(struct ttm_base_object ** p_base)2261571a7a1Sriastradh void ttm_base_object_unref(struct ttm_base_object **p_base)
2271571a7a1Sriastradh {
2281571a7a1Sriastradh 	struct ttm_base_object *base = *p_base;
2291571a7a1Sriastradh 
2301571a7a1Sriastradh 	*p_base = NULL;
2311571a7a1Sriastradh 
2321571a7a1Sriastradh 	kref_put(&base->refcount, ttm_release_base);
2331571a7a1Sriastradh }
2341571a7a1Sriastradh 
2351571a7a1Sriastradh /**
2361571a7a1Sriastradh  * ttm_base_object_noref_lookup - look up a base object without reference
2371571a7a1Sriastradh  * @tfile: The struct ttm_object_file the object is registered with.
2381571a7a1Sriastradh  * @key: The object handle.
2391571a7a1Sriastradh  *
2401571a7a1Sriastradh  * This function looks up a ttm base object and returns a pointer to it
2411571a7a1Sriastradh  * without refcounting the pointer. The returned pointer is only valid
2421571a7a1Sriastradh  * until ttm_base_object_noref_release() is called, and the object
2431571a7a1Sriastradh  * pointed to by the returned pointer may be doomed. Any persistent usage
2441571a7a1Sriastradh  * of the object requires a refcount to be taken using kref_get_unless_zero().
2451571a7a1Sriastradh  * Iff this function returns successfully it needs to be paired with
2461571a7a1Sriastradh  * ttm_base_object_noref_release() and no sleeping- or scheduling functions
2471571a7a1Sriastradh  * may be called inbetween these function callse.
2481571a7a1Sriastradh  *
2491571a7a1Sriastradh  * Return: A pointer to the object if successful or NULL otherwise.
2501571a7a1Sriastradh  */
2511571a7a1Sriastradh struct ttm_base_object *
ttm_base_object_noref_lookup(struct ttm_object_file * tfile,uint32_t key)2521571a7a1Sriastradh ttm_base_object_noref_lookup(struct ttm_object_file *tfile, uint32_t key)
2531571a7a1Sriastradh {
2541571a7a1Sriastradh 	struct drm_hash_item *hash;
2551571a7a1Sriastradh 	struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
2561571a7a1Sriastradh 	int ret;
2571571a7a1Sriastradh 
2581571a7a1Sriastradh 	rcu_read_lock();
2591571a7a1Sriastradh 	ret = drm_ht_find_item_rcu(ht, key, &hash);
2601571a7a1Sriastradh 	if (ret) {
2611571a7a1Sriastradh 		rcu_read_unlock();
2621571a7a1Sriastradh 		return NULL;
2631571a7a1Sriastradh 	}
2641571a7a1Sriastradh 
2651571a7a1Sriastradh 	__release(RCU);
2661571a7a1Sriastradh 	return drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
2671571a7a1Sriastradh }
2681571a7a1Sriastradh EXPORT_SYMBOL(ttm_base_object_noref_lookup);
2691571a7a1Sriastradh 
ttm_base_object_lookup(struct ttm_object_file * tfile,uint32_t key)2701571a7a1Sriastradh struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
2711571a7a1Sriastradh 					       uint32_t key)
2721571a7a1Sriastradh {
2731571a7a1Sriastradh 	struct ttm_base_object *base = NULL;
2741571a7a1Sriastradh 	struct drm_hash_item *hash;
2751571a7a1Sriastradh 	struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
2761571a7a1Sriastradh 	int ret;
2771571a7a1Sriastradh 
2781571a7a1Sriastradh 	rcu_read_lock();
2791571a7a1Sriastradh 	ret = drm_ht_find_item_rcu(ht, key, &hash);
2801571a7a1Sriastradh 
2811571a7a1Sriastradh 	if (likely(ret == 0)) {
2821571a7a1Sriastradh 		base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
2831571a7a1Sriastradh 		if (!kref_get_unless_zero(&base->refcount))
2841571a7a1Sriastradh 			base = NULL;
2851571a7a1Sriastradh 	}
2861571a7a1Sriastradh 	rcu_read_unlock();
2871571a7a1Sriastradh 
2881571a7a1Sriastradh 	return base;
2891571a7a1Sriastradh }
2901571a7a1Sriastradh 
2911571a7a1Sriastradh struct ttm_base_object *
ttm_base_object_lookup_for_ref(struct ttm_object_device * tdev,uint32_t key)2921571a7a1Sriastradh ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
2931571a7a1Sriastradh {
2941571a7a1Sriastradh 	struct ttm_base_object *base;
2951571a7a1Sriastradh 
2961571a7a1Sriastradh 	rcu_read_lock();
2971571a7a1Sriastradh 	base = idr_find(&tdev->idr, key);
2981571a7a1Sriastradh 
2991571a7a1Sriastradh 	if (base && !kref_get_unless_zero(&base->refcount))
3001571a7a1Sriastradh 		base = NULL;
3011571a7a1Sriastradh 	rcu_read_unlock();
3021571a7a1Sriastradh 
3031571a7a1Sriastradh 	return base;
3041571a7a1Sriastradh }
3051571a7a1Sriastradh 
3061571a7a1Sriastradh /**
3071571a7a1Sriastradh  * ttm_ref_object_exists - Check whether a caller has a valid ref object
3081571a7a1Sriastradh  * (has opened) a base object.
3091571a7a1Sriastradh  *
3101571a7a1Sriastradh  * @tfile: Pointer to a struct ttm_object_file identifying the caller.
3111571a7a1Sriastradh  * @base: Pointer to a struct base object.
3121571a7a1Sriastradh  *
3131571a7a1Sriastradh  * Checks wether the caller identified by @tfile has put a valid USAGE
3141571a7a1Sriastradh  * reference object on the base object identified by @base.
3151571a7a1Sriastradh  */
ttm_ref_object_exists(struct ttm_object_file * tfile,struct ttm_base_object * base)3161571a7a1Sriastradh bool ttm_ref_object_exists(struct ttm_object_file *tfile,
3171571a7a1Sriastradh 			   struct ttm_base_object *base)
3181571a7a1Sriastradh {
3191571a7a1Sriastradh 	struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
3201571a7a1Sriastradh 	struct drm_hash_item *hash;
3211571a7a1Sriastradh 	struct ttm_ref_object *ref;
3221571a7a1Sriastradh 
3231571a7a1Sriastradh 	rcu_read_lock();
3241571a7a1Sriastradh 	if (unlikely(drm_ht_find_item_rcu(ht, base->handle, &hash) != 0))
3251571a7a1Sriastradh 		goto out_false;
3261571a7a1Sriastradh 
3271571a7a1Sriastradh 	/*
3281571a7a1Sriastradh 	 * Verify that the ref object is really pointing to our base object.
3291571a7a1Sriastradh 	 * Our base object could actually be dead, and the ref object pointing
3301571a7a1Sriastradh 	 * to another base object with the same handle.
3311571a7a1Sriastradh 	 */
3321571a7a1Sriastradh 	ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
3331571a7a1Sriastradh 	if (unlikely(base != ref->obj))
3341571a7a1Sriastradh 		goto out_false;
3351571a7a1Sriastradh 
3361571a7a1Sriastradh 	/*
3371571a7a1Sriastradh 	 * Verify that the ref->obj pointer was actually valid!
3381571a7a1Sriastradh 	 */
3391571a7a1Sriastradh 	rmb();
3401571a7a1Sriastradh 	if (unlikely(kref_read(&ref->kref) == 0))
3411571a7a1Sriastradh 		goto out_false;
3421571a7a1Sriastradh 
3431571a7a1Sriastradh 	rcu_read_unlock();
3441571a7a1Sriastradh 	return true;
3451571a7a1Sriastradh 
3461571a7a1Sriastradh  out_false:
3471571a7a1Sriastradh 	rcu_read_unlock();
3481571a7a1Sriastradh 	return false;
3491571a7a1Sriastradh }
3501571a7a1Sriastradh 
ttm_ref_object_add(struct ttm_object_file * tfile,struct ttm_base_object * base,enum ttm_ref_type ref_type,bool * existed,bool require_existed)3511571a7a1Sriastradh int ttm_ref_object_add(struct ttm_object_file *tfile,
3521571a7a1Sriastradh 		       struct ttm_base_object *base,
3531571a7a1Sriastradh 		       enum ttm_ref_type ref_type, bool *existed,
3541571a7a1Sriastradh 		       bool require_existed)
3551571a7a1Sriastradh {
3561571a7a1Sriastradh 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
3571571a7a1Sriastradh 	struct ttm_ref_object *ref;
3581571a7a1Sriastradh 	struct drm_hash_item *hash;
3591571a7a1Sriastradh 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
3601571a7a1Sriastradh 	struct ttm_operation_ctx ctx = {
3611571a7a1Sriastradh 		.interruptible = false,
3621571a7a1Sriastradh 		.no_wait_gpu = false
3631571a7a1Sriastradh 	};
3641571a7a1Sriastradh 	int ret = -EINVAL;
3651571a7a1Sriastradh 
3661571a7a1Sriastradh 	if (base->tfile != tfile && !base->shareable)
3671571a7a1Sriastradh 		return -EPERM;
3681571a7a1Sriastradh 
3691571a7a1Sriastradh 	if (existed != NULL)
3701571a7a1Sriastradh 		*existed = true;
3711571a7a1Sriastradh 
3721571a7a1Sriastradh 	while (ret == -EINVAL) {
3731571a7a1Sriastradh 		rcu_read_lock();
3741571a7a1Sriastradh 		ret = drm_ht_find_item_rcu(ht, base->handle, &hash);
3751571a7a1Sriastradh 
3761571a7a1Sriastradh 		if (ret == 0) {
3771571a7a1Sriastradh 			ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
3781571a7a1Sriastradh 			if (kref_get_unless_zero(&ref->kref)) {
3791571a7a1Sriastradh 				rcu_read_unlock();
3801571a7a1Sriastradh 				break;
3811571a7a1Sriastradh 			}
3821571a7a1Sriastradh 		}
3831571a7a1Sriastradh 
3841571a7a1Sriastradh 		rcu_read_unlock();
3851571a7a1Sriastradh 		if (require_existed)
3861571a7a1Sriastradh 			return -EPERM;
3871571a7a1Sriastradh 
3881571a7a1Sriastradh 		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
3891571a7a1Sriastradh 					   &ctx);
3901571a7a1Sriastradh 		if (unlikely(ret != 0))
3911571a7a1Sriastradh 			return ret;
3921571a7a1Sriastradh 		ref = kmalloc(sizeof(*ref), GFP_KERNEL);
3931571a7a1Sriastradh 		if (unlikely(ref == NULL)) {
3941571a7a1Sriastradh 			ttm_mem_global_free(mem_glob, sizeof(*ref));
3951571a7a1Sriastradh 			return -ENOMEM;
3961571a7a1Sriastradh 		}
3971571a7a1Sriastradh 
3981571a7a1Sriastradh 		ref->hash.key = base->handle;
3991571a7a1Sriastradh 		ref->obj = base;
4001571a7a1Sriastradh 		ref->tfile = tfile;
4011571a7a1Sriastradh 		ref->ref_type = ref_type;
4021571a7a1Sriastradh 		kref_init(&ref->kref);
4031571a7a1Sriastradh 
4041571a7a1Sriastradh 		spin_lock(&tfile->lock);
4051571a7a1Sriastradh 		ret = drm_ht_insert_item_rcu(ht, &ref->hash);
4061571a7a1Sriastradh 
4071571a7a1Sriastradh 		if (likely(ret == 0)) {
4081571a7a1Sriastradh 			list_add_tail(&ref->head, &tfile->ref_list);
4091571a7a1Sriastradh 			kref_get(&base->refcount);
4101571a7a1Sriastradh 			spin_unlock(&tfile->lock);
4111571a7a1Sriastradh 			if (existed != NULL)
4121571a7a1Sriastradh 				*existed = false;
4131571a7a1Sriastradh 			break;
4141571a7a1Sriastradh 		}
4151571a7a1Sriastradh 
4161571a7a1Sriastradh 		spin_unlock(&tfile->lock);
4171571a7a1Sriastradh 		BUG_ON(ret != -EINVAL);
4181571a7a1Sriastradh 
4191571a7a1Sriastradh 		ttm_mem_global_free(mem_glob, sizeof(*ref));
4201571a7a1Sriastradh 		kfree(ref);
4211571a7a1Sriastradh 	}
4221571a7a1Sriastradh 
4231571a7a1Sriastradh 	return ret;
4241571a7a1Sriastradh }
4251571a7a1Sriastradh 
4261571a7a1Sriastradh static void __releases(tfile->lock) __acquires(tfile->lock)
ttm_ref_object_release(struct kref * kref)4271571a7a1Sriastradh ttm_ref_object_release(struct kref *kref)
4281571a7a1Sriastradh {
4291571a7a1Sriastradh 	struct ttm_ref_object *ref =
4301571a7a1Sriastradh 	    container_of(kref, struct ttm_ref_object, kref);
4311571a7a1Sriastradh 	struct ttm_base_object *base = ref->obj;
4321571a7a1Sriastradh 	struct ttm_object_file *tfile = ref->tfile;
4331571a7a1Sriastradh 	struct drm_open_hash *ht;
4341571a7a1Sriastradh 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
4351571a7a1Sriastradh 
4361571a7a1Sriastradh 	ht = &tfile->ref_hash[ref->ref_type];
4371571a7a1Sriastradh 	(void)drm_ht_remove_item_rcu(ht, &ref->hash);
4381571a7a1Sriastradh 	list_del(&ref->head);
4391571a7a1Sriastradh 	spin_unlock(&tfile->lock);
4401571a7a1Sriastradh 
4411571a7a1Sriastradh 	if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
4421571a7a1Sriastradh 		base->ref_obj_release(base, ref->ref_type);
4431571a7a1Sriastradh 
4441571a7a1Sriastradh 	ttm_base_object_unref(&ref->obj);
4451571a7a1Sriastradh 	ttm_mem_global_free(mem_glob, sizeof(*ref));
4461571a7a1Sriastradh 	kfree_rcu(ref, rcu_head);
4471571a7a1Sriastradh 	spin_lock(&tfile->lock);
4481571a7a1Sriastradh }
4491571a7a1Sriastradh 
ttm_ref_object_base_unref(struct ttm_object_file * tfile,unsigned long key,enum ttm_ref_type ref_type)4501571a7a1Sriastradh int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
4511571a7a1Sriastradh 			      unsigned long key, enum ttm_ref_type ref_type)
4521571a7a1Sriastradh {
4531571a7a1Sriastradh 	struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
4541571a7a1Sriastradh 	struct ttm_ref_object *ref;
4551571a7a1Sriastradh 	struct drm_hash_item *hash;
4561571a7a1Sriastradh 	int ret;
4571571a7a1Sriastradh 
4581571a7a1Sriastradh 	spin_lock(&tfile->lock);
4591571a7a1Sriastradh 	ret = drm_ht_find_item(ht, key, &hash);
4601571a7a1Sriastradh 	if (unlikely(ret != 0)) {
4611571a7a1Sriastradh 		spin_unlock(&tfile->lock);
4621571a7a1Sriastradh 		return -EINVAL;
4631571a7a1Sriastradh 	}
4641571a7a1Sriastradh 	ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
4651571a7a1Sriastradh 	kref_put(&ref->kref, ttm_ref_object_release);
4661571a7a1Sriastradh 	spin_unlock(&tfile->lock);
4671571a7a1Sriastradh 	return 0;
4681571a7a1Sriastradh }
4691571a7a1Sriastradh 
ttm_object_file_release(struct ttm_object_file ** p_tfile)4701571a7a1Sriastradh void ttm_object_file_release(struct ttm_object_file **p_tfile)
4711571a7a1Sriastradh {
4721571a7a1Sriastradh 	struct ttm_ref_object *ref;
4731571a7a1Sriastradh 	struct list_head *list;
4741571a7a1Sriastradh 	unsigned int i;
4751571a7a1Sriastradh 	struct ttm_object_file *tfile = *p_tfile;
4761571a7a1Sriastradh 
4771571a7a1Sriastradh 	*p_tfile = NULL;
4781571a7a1Sriastradh 	spin_lock(&tfile->lock);
4791571a7a1Sriastradh 
4801571a7a1Sriastradh 	/*
4811571a7a1Sriastradh 	 * Since we release the lock within the loop, we have to
4821571a7a1Sriastradh 	 * restart it from the beginning each time.
4831571a7a1Sriastradh 	 */
4841571a7a1Sriastradh 
4851571a7a1Sriastradh 	while (!list_empty(&tfile->ref_list)) {
4861571a7a1Sriastradh 		list = tfile->ref_list.next;
4871571a7a1Sriastradh 		ref = list_entry(list, struct ttm_ref_object, head);
4881571a7a1Sriastradh 		ttm_ref_object_release(&ref->kref);
4891571a7a1Sriastradh 	}
4901571a7a1Sriastradh 
4911571a7a1Sriastradh 	spin_unlock(&tfile->lock);
4921571a7a1Sriastradh 	for (i = 0; i < TTM_REF_NUM; ++i)
4931571a7a1Sriastradh 		drm_ht_remove(&tfile->ref_hash[i]);
4941571a7a1Sriastradh 
4951571a7a1Sriastradh 	ttm_object_file_unref(&tfile);
4961571a7a1Sriastradh }
4971571a7a1Sriastradh 
ttm_object_file_init(struct ttm_object_device * tdev,unsigned int hash_order)4981571a7a1Sriastradh struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
4991571a7a1Sriastradh 					     unsigned int hash_order)
5001571a7a1Sriastradh {
5011571a7a1Sriastradh 	struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
5021571a7a1Sriastradh 	unsigned int i;
5031571a7a1Sriastradh 	unsigned int j = 0;
5041571a7a1Sriastradh 	int ret;
5051571a7a1Sriastradh 
5061571a7a1Sriastradh 	if (unlikely(tfile == NULL))
5071571a7a1Sriastradh 		return NULL;
5081571a7a1Sriastradh 
5091571a7a1Sriastradh 	spin_lock_init(&tfile->lock);
5101571a7a1Sriastradh 	tfile->tdev = tdev;
5111571a7a1Sriastradh 	kref_init(&tfile->refcount);
5121571a7a1Sriastradh 	INIT_LIST_HEAD(&tfile->ref_list);
5131571a7a1Sriastradh 
5141571a7a1Sriastradh 	for (i = 0; i < TTM_REF_NUM; ++i) {
5151571a7a1Sriastradh 		ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
5161571a7a1Sriastradh 		if (ret) {
5171571a7a1Sriastradh 			j = i;
5181571a7a1Sriastradh 			goto out_err;
5191571a7a1Sriastradh 		}
5201571a7a1Sriastradh 	}
5211571a7a1Sriastradh 
5221571a7a1Sriastradh 	return tfile;
5231571a7a1Sriastradh out_err:
5241571a7a1Sriastradh 	for (i = 0; i < j; ++i)
5251571a7a1Sriastradh 		drm_ht_remove(&tfile->ref_hash[i]);
5261571a7a1Sriastradh 
5271571a7a1Sriastradh 	kfree(tfile);
5281571a7a1Sriastradh 
5291571a7a1Sriastradh 	return NULL;
5301571a7a1Sriastradh }
5311571a7a1Sriastradh 
5321571a7a1Sriastradh struct ttm_object_device *
ttm_object_device_init(struct ttm_mem_global * mem_glob,unsigned int hash_order,const struct dma_buf_ops * ops)5331571a7a1Sriastradh ttm_object_device_init(struct ttm_mem_global *mem_glob,
5341571a7a1Sriastradh 		       unsigned int hash_order,
5351571a7a1Sriastradh 		       const struct dma_buf_ops *ops)
5361571a7a1Sriastradh {
5371571a7a1Sriastradh 	struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
5381571a7a1Sriastradh 	int ret;
5391571a7a1Sriastradh 
5401571a7a1Sriastradh 	if (unlikely(tdev == NULL))
5411571a7a1Sriastradh 		return NULL;
5421571a7a1Sriastradh 
5431571a7a1Sriastradh 	tdev->mem_glob = mem_glob;
5441571a7a1Sriastradh 	spin_lock_init(&tdev->object_lock);
5451571a7a1Sriastradh 	atomic_set(&tdev->object_count, 0);
5461571a7a1Sriastradh 	ret = drm_ht_create(&tdev->object_hash, hash_order);
5471571a7a1Sriastradh 	if (ret != 0)
5481571a7a1Sriastradh 		goto out_no_object_hash;
5491571a7a1Sriastradh 
5501571a7a1Sriastradh 	idr_init(&tdev->idr);
5511571a7a1Sriastradh 	tdev->ops = *ops;
5521571a7a1Sriastradh 	tdev->dmabuf_release = tdev->ops.release;
5531571a7a1Sriastradh 	tdev->ops.release = ttm_prime_dmabuf_release;
5541571a7a1Sriastradh 	tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
5551571a7a1Sriastradh 		ttm_round_pot(sizeof(struct file));
5561571a7a1Sriastradh 	return tdev;
5571571a7a1Sriastradh 
5581571a7a1Sriastradh out_no_object_hash:
5591571a7a1Sriastradh 	kfree(tdev);
5601571a7a1Sriastradh 	return NULL;
5611571a7a1Sriastradh }
5621571a7a1Sriastradh 
ttm_object_device_release(struct ttm_object_device ** p_tdev)5631571a7a1Sriastradh void ttm_object_device_release(struct ttm_object_device **p_tdev)
5641571a7a1Sriastradh {
5651571a7a1Sriastradh 	struct ttm_object_device *tdev = *p_tdev;
5661571a7a1Sriastradh 
5671571a7a1Sriastradh 	*p_tdev = NULL;
5681571a7a1Sriastradh 
5691571a7a1Sriastradh 	WARN_ON_ONCE(!idr_is_empty(&tdev->idr));
5701571a7a1Sriastradh 	idr_destroy(&tdev->idr);
5711571a7a1Sriastradh 	drm_ht_remove(&tdev->object_hash);
5721571a7a1Sriastradh 
5731571a7a1Sriastradh 	kfree(tdev);
5741571a7a1Sriastradh }
5751571a7a1Sriastradh 
5761571a7a1Sriastradh /**
5771571a7a1Sriastradh  * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
5781571a7a1Sriastradh  *
5791571a7a1Sriastradh  * @dma_buf: Non-refcounted pointer to a struct dma-buf.
5801571a7a1Sriastradh  *
5811571a7a1Sriastradh  * Obtain a file reference from a lookup structure that doesn't refcount
5821571a7a1Sriastradh  * the file, but synchronizes with its release method to make sure it has
5831571a7a1Sriastradh  * not been freed yet. See for example kref_get_unless_zero documentation.
5841571a7a1Sriastradh  * Returns true if refcounting succeeds, false otherwise.
5851571a7a1Sriastradh  *
5861571a7a1Sriastradh  * Nobody really wants this as a public API yet, so let it mature here
5871571a7a1Sriastradh  * for some time...
5881571a7a1Sriastradh  */
get_dma_buf_unless_doomed(struct dma_buf * dmabuf)5891571a7a1Sriastradh static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
5901571a7a1Sriastradh {
591*e3e4e02dSriastradh #ifdef __NetBSD__
592*e3e4e02dSriastradh 	/* XXX move this to linux_dma_buf.c */
593*e3e4e02dSriastradh 	unsigned cnt;
594*e3e4e02dSriastradh 
595*e3e4e02dSriastradh 	do {
596*e3e4e02dSriastradh 		cnt = atomic_load_relaxed(&dmabuf->db_refcnt);
597*e3e4e02dSriastradh 		if (cnt == 0)
598*e3e4e02dSriastradh 			return false;
599*e3e4e02dSriastradh 	} while (atomic_cas_uint(&dmabuf->db_refcnt, cnt, cnt + 1) != cnt);
600*e3e4e02dSriastradh 	return true;
601*e3e4e02dSriastradh #else
6021571a7a1Sriastradh 	return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
603*e3e4e02dSriastradh #endif
6041571a7a1Sriastradh }
6051571a7a1Sriastradh 
6061571a7a1Sriastradh /**
6071571a7a1Sriastradh  * ttm_prime_refcount_release - refcount release method for a prime object.
6081571a7a1Sriastradh  *
6091571a7a1Sriastradh  * @p_base: Pointer to ttm_base_object pointer.
6101571a7a1Sriastradh  *
6111571a7a1Sriastradh  * This is a wrapper that calls the refcount_release founction of the
6121571a7a1Sriastradh  * underlying object. At the same time it cleans up the prime object.
6131571a7a1Sriastradh  * This function is called when all references to the base object we
6141571a7a1Sriastradh  * derive from are gone.
6151571a7a1Sriastradh  */
ttm_prime_refcount_release(struct ttm_base_object ** p_base)6161571a7a1Sriastradh static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
6171571a7a1Sriastradh {
6181571a7a1Sriastradh 	struct ttm_base_object *base = *p_base;
6191571a7a1Sriastradh 	struct ttm_prime_object *prime;
6201571a7a1Sriastradh 
6211571a7a1Sriastradh 	*p_base = NULL;
6221571a7a1Sriastradh 	prime = container_of(base, struct ttm_prime_object, base);
6231571a7a1Sriastradh 	BUG_ON(prime->dma_buf != NULL);
6241571a7a1Sriastradh 	mutex_destroy(&prime->mutex);
6251571a7a1Sriastradh 	if (prime->refcount_release)
6261571a7a1Sriastradh 		prime->refcount_release(&base);
6271571a7a1Sriastradh }
6281571a7a1Sriastradh 
6291571a7a1Sriastradh /**
6301571a7a1Sriastradh  * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
6311571a7a1Sriastradh  *
6321571a7a1Sriastradh  * @dma_buf:
6331571a7a1Sriastradh  *
6341571a7a1Sriastradh  * This function first calls the dma_buf release method the driver
6351571a7a1Sriastradh  * provides. Then it cleans up our dma_buf pointer used for lookup,
6361571a7a1Sriastradh  * and finally releases the reference the dma_buf has on our base
6371571a7a1Sriastradh  * object.
6381571a7a1Sriastradh  */
ttm_prime_dmabuf_release(struct dma_buf * dma_buf)6391571a7a1Sriastradh static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
6401571a7a1Sriastradh {
6411571a7a1Sriastradh 	struct ttm_prime_object *prime =
6421571a7a1Sriastradh 		(struct ttm_prime_object *) dma_buf->priv;
6431571a7a1Sriastradh 	struct ttm_base_object *base = &prime->base;
6441571a7a1Sriastradh 	struct ttm_object_device *tdev = base->tfile->tdev;
6451571a7a1Sriastradh 
6461571a7a1Sriastradh 	if (tdev->dmabuf_release)
6471571a7a1Sriastradh 		tdev->dmabuf_release(dma_buf);
6481571a7a1Sriastradh 	mutex_lock(&prime->mutex);
6491571a7a1Sriastradh 	if (prime->dma_buf == dma_buf)
6501571a7a1Sriastradh 		prime->dma_buf = NULL;
6511571a7a1Sriastradh 	mutex_unlock(&prime->mutex);
6521571a7a1Sriastradh 	ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
6531571a7a1Sriastradh 	ttm_base_object_unref(&base);
6541571a7a1Sriastradh }
6551571a7a1Sriastradh 
6561571a7a1Sriastradh /**
6571571a7a1Sriastradh  * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
6581571a7a1Sriastradh  *
6591571a7a1Sriastradh  * @tfile: A struct ttm_object_file identifying the caller.
6601571a7a1Sriastradh  * @fd: The prime / dmabuf fd.
6611571a7a1Sriastradh  * @handle: The returned handle.
6621571a7a1Sriastradh  *
6631571a7a1Sriastradh  * This function returns a handle to an object that previously exported
6641571a7a1Sriastradh  * a dma-buf. Note that we don't handle imports yet, because we simply
6651571a7a1Sriastradh  * have no consumers of that implementation.
6661571a7a1Sriastradh  */
ttm_prime_fd_to_handle(struct ttm_object_file * tfile,int fd,u32 * handle)6671571a7a1Sriastradh int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
6681571a7a1Sriastradh 			   int fd, u32 *handle)
6691571a7a1Sriastradh {
6701571a7a1Sriastradh 	struct ttm_object_device *tdev = tfile->tdev;
6711571a7a1Sriastradh 	struct dma_buf *dma_buf;
6721571a7a1Sriastradh 	struct ttm_prime_object *prime;
6731571a7a1Sriastradh 	struct ttm_base_object *base;
6741571a7a1Sriastradh 	int ret;
6751571a7a1Sriastradh 
6761571a7a1Sriastradh 	dma_buf = dma_buf_get(fd);
6771571a7a1Sriastradh 	if (IS_ERR(dma_buf))
6781571a7a1Sriastradh 		return PTR_ERR(dma_buf);
6791571a7a1Sriastradh 
6801571a7a1Sriastradh 	if (dma_buf->ops != &tdev->ops)
6811571a7a1Sriastradh 		return -ENOSYS;
6821571a7a1Sriastradh 
6831571a7a1Sriastradh 	prime = (struct ttm_prime_object *) dma_buf->priv;
6841571a7a1Sriastradh 	base = &prime->base;
6851571a7a1Sriastradh 	*handle = base->handle;
6861571a7a1Sriastradh 	ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
6871571a7a1Sriastradh 
6881571a7a1Sriastradh 	dma_buf_put(dma_buf);
6891571a7a1Sriastradh 
6901571a7a1Sriastradh 	return ret;
6911571a7a1Sriastradh }
6921571a7a1Sriastradh 
6931571a7a1Sriastradh /**
6941571a7a1Sriastradh  * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
6951571a7a1Sriastradh  *
6961571a7a1Sriastradh  * @tfile: Struct ttm_object_file identifying the caller.
6971571a7a1Sriastradh  * @handle: Handle to the object we're exporting from.
6981571a7a1Sriastradh  * @flags: flags for dma-buf creation. We just pass them on.
6991571a7a1Sriastradh  * @prime_fd: The returned file descriptor.
7001571a7a1Sriastradh  *
7011571a7a1Sriastradh  */
ttm_prime_handle_to_fd(struct ttm_object_file * tfile,uint32_t handle,uint32_t flags,int * prime_fd)7021571a7a1Sriastradh int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
7031571a7a1Sriastradh 			   uint32_t handle, uint32_t flags,
7041571a7a1Sriastradh 			   int *prime_fd)
7051571a7a1Sriastradh {
7061571a7a1Sriastradh 	struct ttm_object_device *tdev = tfile->tdev;
7071571a7a1Sriastradh 	struct ttm_base_object *base;
7081571a7a1Sriastradh 	struct dma_buf *dma_buf;
7091571a7a1Sriastradh 	struct ttm_prime_object *prime;
7101571a7a1Sriastradh 	int ret;
7111571a7a1Sriastradh 
7121571a7a1Sriastradh 	base = ttm_base_object_lookup(tfile, handle);
7131571a7a1Sriastradh 	if (unlikely(base == NULL ||
7141571a7a1Sriastradh 		     base->object_type != ttm_prime_type)) {
7151571a7a1Sriastradh 		ret = -ENOENT;
7161571a7a1Sriastradh 		goto out_unref;
7171571a7a1Sriastradh 	}
7181571a7a1Sriastradh 
7191571a7a1Sriastradh 	prime = container_of(base, struct ttm_prime_object, base);
7201571a7a1Sriastradh 	if (unlikely(!base->shareable)) {
7211571a7a1Sriastradh 		ret = -EPERM;
7221571a7a1Sriastradh 		goto out_unref;
7231571a7a1Sriastradh 	}
7241571a7a1Sriastradh 
7251571a7a1Sriastradh 	ret = mutex_lock_interruptible(&prime->mutex);
7261571a7a1Sriastradh 	if (unlikely(ret != 0)) {
7271571a7a1Sriastradh 		ret = -ERESTARTSYS;
7281571a7a1Sriastradh 		goto out_unref;
7291571a7a1Sriastradh 	}
7301571a7a1Sriastradh 
7311571a7a1Sriastradh 	dma_buf = prime->dma_buf;
7321571a7a1Sriastradh 	if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
7331571a7a1Sriastradh 		DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
7341571a7a1Sriastradh 		struct ttm_operation_ctx ctx = {
7351571a7a1Sriastradh 			.interruptible = true,
7361571a7a1Sriastradh 			.no_wait_gpu = false
7371571a7a1Sriastradh 		};
7381571a7a1Sriastradh 		exp_info.ops = &tdev->ops;
7391571a7a1Sriastradh 		exp_info.size = prime->size;
7401571a7a1Sriastradh 		exp_info.flags = flags;
7411571a7a1Sriastradh 		exp_info.priv = prime;
7421571a7a1Sriastradh 
7431571a7a1Sriastradh 		/*
7441571a7a1Sriastradh 		 * Need to create a new dma_buf, with memory accounting.
7451571a7a1Sriastradh 		 */
7461571a7a1Sriastradh 		ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
7471571a7a1Sriastradh 					   &ctx);
7481571a7a1Sriastradh 		if (unlikely(ret != 0)) {
7491571a7a1Sriastradh 			mutex_unlock(&prime->mutex);
7501571a7a1Sriastradh 			goto out_unref;
7511571a7a1Sriastradh 		}
7521571a7a1Sriastradh 
7531571a7a1Sriastradh 		dma_buf = dma_buf_export(&exp_info);
7541571a7a1Sriastradh 		if (IS_ERR(dma_buf)) {
7551571a7a1Sriastradh 			ret = PTR_ERR(dma_buf);
7561571a7a1Sriastradh 			ttm_mem_global_free(tdev->mem_glob,
7571571a7a1Sriastradh 					    tdev->dma_buf_size);
7581571a7a1Sriastradh 			mutex_unlock(&prime->mutex);
7591571a7a1Sriastradh 			goto out_unref;
7601571a7a1Sriastradh 		}
7611571a7a1Sriastradh 
7621571a7a1Sriastradh 		/*
7631571a7a1Sriastradh 		 * dma_buf has taken the base object reference
7641571a7a1Sriastradh 		 */
7651571a7a1Sriastradh 		base = NULL;
7661571a7a1Sriastradh 		prime->dma_buf = dma_buf;
7671571a7a1Sriastradh 	}
7681571a7a1Sriastradh 	mutex_unlock(&prime->mutex);
7691571a7a1Sriastradh 
7701571a7a1Sriastradh 	ret = dma_buf_fd(dma_buf, flags);
7711571a7a1Sriastradh 	if (ret >= 0) {
7721571a7a1Sriastradh 		*prime_fd = ret;
7731571a7a1Sriastradh 		ret = 0;
7741571a7a1Sriastradh 	} else
7751571a7a1Sriastradh 		dma_buf_put(dma_buf);
7761571a7a1Sriastradh 
7771571a7a1Sriastradh out_unref:
7781571a7a1Sriastradh 	if (base)
7791571a7a1Sriastradh 		ttm_base_object_unref(&base);
7801571a7a1Sriastradh 	return ret;
7811571a7a1Sriastradh }
7821571a7a1Sriastradh 
7831571a7a1Sriastradh /**
7841571a7a1Sriastradh  * ttm_prime_object_init - Initialize a ttm_prime_object
7851571a7a1Sriastradh  *
7861571a7a1Sriastradh  * @tfile: struct ttm_object_file identifying the caller
7871571a7a1Sriastradh  * @size: The size of the dma_bufs we export.
7881571a7a1Sriastradh  * @prime: The object to be initialized.
7891571a7a1Sriastradh  * @shareable: See ttm_base_object_init
7901571a7a1Sriastradh  * @type: See ttm_base_object_init
7911571a7a1Sriastradh  * @refcount_release: See ttm_base_object_init
7921571a7a1Sriastradh  * @ref_obj_release: See ttm_base_object_init
7931571a7a1Sriastradh  *
7941571a7a1Sriastradh  * Initializes an object which is compatible with the drm_prime model
7951571a7a1Sriastradh  * for data sharing between processes and devices.
7961571a7a1Sriastradh  */
ttm_prime_object_init(struct ttm_object_file * tfile,size_t size,struct ttm_prime_object * prime,bool shareable,enum ttm_object_type type,void (* refcount_release)(struct ttm_base_object **),void (* ref_obj_release)(struct ttm_base_object *,enum ttm_ref_type ref_type))7971571a7a1Sriastradh int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
7981571a7a1Sriastradh 			  struct ttm_prime_object *prime, bool shareable,
7991571a7a1Sriastradh 			  enum ttm_object_type type,
8001571a7a1Sriastradh 			  void (*refcount_release) (struct ttm_base_object **),
8011571a7a1Sriastradh 			  void (*ref_obj_release) (struct ttm_base_object *,
8021571a7a1Sriastradh 						   enum ttm_ref_type ref_type))
8031571a7a1Sriastradh {
8041571a7a1Sriastradh 	mutex_init(&prime->mutex);
8051571a7a1Sriastradh 	prime->size = PAGE_ALIGN(size);
8061571a7a1Sriastradh 	prime->real_type = type;
8071571a7a1Sriastradh 	prime->dma_buf = NULL;
8081571a7a1Sriastradh 	prime->refcount_release = refcount_release;
8091571a7a1Sriastradh 	return ttm_base_object_init(tfile, &prime->base, shareable,
8101571a7a1Sriastradh 				    ttm_prime_type,
8111571a7a1Sriastradh 				    ttm_prime_refcount_release,
8121571a7a1Sriastradh 				    ref_obj_release);
8131571a7a1Sriastradh }
814