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