xref: /freebsd-src/sys/dev/drm2/ttm/ttm_object.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1*592ffb21SWarner Losh /**************************************************************************
2*592ffb21SWarner Losh  *
3*592ffb21SWarner Losh  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*592ffb21SWarner Losh  * All Rights Reserved.
5*592ffb21SWarner Losh  *
6*592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
7*592ffb21SWarner Losh  * copy of this software and associated documentation files (the
8*592ffb21SWarner Losh  * "Software"), to deal in the Software without restriction, including
9*592ffb21SWarner Losh  * without limitation the rights to use, copy, modify, merge, publish,
10*592ffb21SWarner Losh  * distribute, sub license, and/or sell copies of the Software, and to
11*592ffb21SWarner Losh  * permit persons to whom the Software is furnished to do so, subject to
12*592ffb21SWarner Losh  * the following conditions:
13*592ffb21SWarner Losh  *
14*592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the
15*592ffb21SWarner Losh  * next paragraph) shall be included in all copies or substantial portions
16*592ffb21SWarner Losh  * of the Software.
17*592ffb21SWarner Losh  *
18*592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*592ffb21SWarner Losh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*592ffb21SWarner Losh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*592ffb21SWarner Losh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*592ffb21SWarner Losh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*592ffb21SWarner Losh  *
26*592ffb21SWarner Losh  **************************************************************************/
27*592ffb21SWarner Losh /*
28*592ffb21SWarner Losh  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29*592ffb21SWarner Losh  */
30*592ffb21SWarner Losh /** @file ttm_object.h
31*592ffb21SWarner Losh  *
32*592ffb21SWarner Losh  * Base- and reference object implementation for the various
33*592ffb21SWarner Losh  * ttm objects. Implements reference counting, minimal security checks
34*592ffb21SWarner Losh  * and release on file close.
35*592ffb21SWarner Losh  */
36*592ffb21SWarner Losh 
37*592ffb21SWarner Losh #ifndef _TTM_OBJECT_H_
38*592ffb21SWarner Losh #define _TTM_OBJECT_H_
39*592ffb21SWarner Losh 
40*592ffb21SWarner Losh #include <dev/drm2/drm_hashtab.h>
41*592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_memory.h>
42*592ffb21SWarner Losh 
43*592ffb21SWarner Losh /**
44*592ffb21SWarner Losh  * enum ttm_ref_type
45*592ffb21SWarner Losh  *
46*592ffb21SWarner Losh  * Describes what type of reference a ref object holds.
47*592ffb21SWarner Losh  *
48*592ffb21SWarner Losh  * TTM_REF_USAGE is a simple refcount on a base object.
49*592ffb21SWarner Losh  *
50*592ffb21SWarner Losh  * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
51*592ffb21SWarner Losh  * buffer object.
52*592ffb21SWarner Losh  *
53*592ffb21SWarner Losh  * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
54*592ffb21SWarner Losh  * buffer object.
55*592ffb21SWarner Losh  *
56*592ffb21SWarner Losh  */
57*592ffb21SWarner Losh 
58*592ffb21SWarner Losh enum ttm_ref_type {
59*592ffb21SWarner Losh 	TTM_REF_USAGE,
60*592ffb21SWarner Losh 	TTM_REF_SYNCCPU_READ,
61*592ffb21SWarner Losh 	TTM_REF_SYNCCPU_WRITE,
62*592ffb21SWarner Losh 	TTM_REF_NUM
63*592ffb21SWarner Losh };
64*592ffb21SWarner Losh 
65*592ffb21SWarner Losh /**
66*592ffb21SWarner Losh  * enum ttm_object_type
67*592ffb21SWarner Losh  *
68*592ffb21SWarner Losh  * One entry per ttm object type.
69*592ffb21SWarner Losh  * Device-specific types should use the
70*592ffb21SWarner Losh  * ttm_driver_typex types.
71*592ffb21SWarner Losh  */
72*592ffb21SWarner Losh 
73*592ffb21SWarner Losh enum ttm_object_type {
74*592ffb21SWarner Losh 	ttm_fence_type,
75*592ffb21SWarner Losh 	ttm_buffer_type,
76*592ffb21SWarner Losh 	ttm_lock_type,
77*592ffb21SWarner Losh 	ttm_driver_type0 = 256,
78*592ffb21SWarner Losh 	ttm_driver_type1,
79*592ffb21SWarner Losh 	ttm_driver_type2,
80*592ffb21SWarner Losh 	ttm_driver_type3,
81*592ffb21SWarner Losh 	ttm_driver_type4,
82*592ffb21SWarner Losh 	ttm_driver_type5
83*592ffb21SWarner Losh };
84*592ffb21SWarner Losh 
85*592ffb21SWarner Losh struct ttm_object_file;
86*592ffb21SWarner Losh struct ttm_object_device;
87*592ffb21SWarner Losh 
88*592ffb21SWarner Losh /**
89*592ffb21SWarner Losh  * struct ttm_base_object
90*592ffb21SWarner Losh  *
91*592ffb21SWarner Losh  * @hash: hash entry for the per-device object hash.
92*592ffb21SWarner Losh  * @type: derived type this object is base class for.
93*592ffb21SWarner Losh  * @shareable: Other ttm_object_files can access this object.
94*592ffb21SWarner Losh  *
95*592ffb21SWarner Losh  * @tfile: Pointer to ttm_object_file of the creator.
96*592ffb21SWarner Losh  * NULL if the object was not created by a user request.
97*592ffb21SWarner Losh  * (kernel object).
98*592ffb21SWarner Losh  *
99*592ffb21SWarner Losh  * @refcount: Number of references to this object, not
100*592ffb21SWarner Losh  * including the hash entry. A reference to a base object can
101*592ffb21SWarner Losh  * only be held by a ref object.
102*592ffb21SWarner Losh  *
103*592ffb21SWarner Losh  * @refcount_release: A function to be called when there are
104*592ffb21SWarner Losh  * no more references to this object. This function should
105*592ffb21SWarner Losh  * destroy the object (or make sure destruction eventually happens),
106*592ffb21SWarner Losh  * and when it is called, the object has
107*592ffb21SWarner Losh  * already been taken out of the per-device hash. The parameter
108*592ffb21SWarner Losh  * "base" should be set to NULL by the function.
109*592ffb21SWarner Losh  *
110*592ffb21SWarner Losh  * @ref_obj_release: A function to be called when a reference object
111*592ffb21SWarner Losh  * with another ttm_ref_type than TTM_REF_USAGE is deleted.
112*592ffb21SWarner Losh  * This function may, for example, release a lock held by a user-space
113*592ffb21SWarner Losh  * process.
114*592ffb21SWarner Losh  *
115*592ffb21SWarner Losh  * This struct is intended to be used as a base struct for objects that
116*592ffb21SWarner Losh  * are visible to user-space. It provides a global name, race-safe
117*592ffb21SWarner Losh  * access and refcounting, minimal access contol and hooks for unref actions.
118*592ffb21SWarner Losh  */
119*592ffb21SWarner Losh 
120*592ffb21SWarner Losh struct ttm_base_object {
121*592ffb21SWarner Losh 	/* struct rcu_head rhead;XXXKIB */
122*592ffb21SWarner Losh 	struct drm_hash_item hash;
123*592ffb21SWarner Losh 	enum ttm_object_type object_type;
124*592ffb21SWarner Losh 	bool shareable;
125*592ffb21SWarner Losh 	struct ttm_object_file *tfile;
126*592ffb21SWarner Losh 	u_int refcount;
127*592ffb21SWarner Losh 	void (*refcount_release) (struct ttm_base_object **base);
128*592ffb21SWarner Losh 	void (*ref_obj_release) (struct ttm_base_object *base,
129*592ffb21SWarner Losh 				 enum ttm_ref_type ref_type);
130*592ffb21SWarner Losh };
131*592ffb21SWarner Losh 
132*592ffb21SWarner Losh /**
133*592ffb21SWarner Losh  * ttm_base_object_init
134*592ffb21SWarner Losh  *
135*592ffb21SWarner Losh  * @tfile: Pointer to a struct ttm_object_file.
136*592ffb21SWarner Losh  * @base: The struct ttm_base_object to initialize.
137*592ffb21SWarner Losh  * @shareable: This object is shareable with other applcations.
138*592ffb21SWarner Losh  * (different @tfile pointers.)
139*592ffb21SWarner Losh  * @type: The object type.
140*592ffb21SWarner Losh  * @refcount_release: See the struct ttm_base_object description.
141*592ffb21SWarner Losh  * @ref_obj_release: See the struct ttm_base_object description.
142*592ffb21SWarner Losh  *
143*592ffb21SWarner Losh  * Initializes a struct ttm_base_object.
144*592ffb21SWarner Losh  */
145*592ffb21SWarner Losh 
146*592ffb21SWarner Losh extern int ttm_base_object_init(struct ttm_object_file *tfile,
147*592ffb21SWarner Losh 				struct ttm_base_object *base,
148*592ffb21SWarner Losh 				bool shareable,
149*592ffb21SWarner Losh 				enum ttm_object_type type,
150*592ffb21SWarner Losh 				void (*refcount_release) (struct ttm_base_object
151*592ffb21SWarner Losh 							  **),
152*592ffb21SWarner Losh 				void (*ref_obj_release) (struct ttm_base_object
153*592ffb21SWarner Losh 							 *,
154*592ffb21SWarner Losh 							 enum ttm_ref_type
155*592ffb21SWarner Losh 							 ref_type));
156*592ffb21SWarner Losh 
157*592ffb21SWarner Losh /**
158*592ffb21SWarner Losh  * ttm_base_object_lookup
159*592ffb21SWarner Losh  *
160*592ffb21SWarner Losh  * @tfile: Pointer to a struct ttm_object_file.
161*592ffb21SWarner Losh  * @key: Hash key
162*592ffb21SWarner Losh  *
163*592ffb21SWarner Losh  * Looks up a struct ttm_base_object with the key @key.
164*592ffb21SWarner Losh  * Also verifies that the object is visible to the application, by
165*592ffb21SWarner Losh  * comparing the @tfile argument and checking the object shareable flag.
166*592ffb21SWarner Losh  */
167*592ffb21SWarner Losh 
168*592ffb21SWarner Losh extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
169*592ffb21SWarner Losh 						      *tfile, uint32_t key);
170*592ffb21SWarner Losh 
171*592ffb21SWarner Losh /**
172*592ffb21SWarner Losh  * ttm_base_object_unref
173*592ffb21SWarner Losh  *
174*592ffb21SWarner Losh  * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
175*592ffb21SWarner Losh  *
176*592ffb21SWarner Losh  * Decrements the base object refcount and clears the pointer pointed to by
177*592ffb21SWarner Losh  * p_base.
178*592ffb21SWarner Losh  */
179*592ffb21SWarner Losh 
180*592ffb21SWarner Losh extern void ttm_base_object_unref(struct ttm_base_object **p_base);
181*592ffb21SWarner Losh 
182*592ffb21SWarner Losh /**
183*592ffb21SWarner Losh  * ttm_ref_object_add.
184*592ffb21SWarner Losh  *
185*592ffb21SWarner Losh  * @tfile: A struct ttm_object_file representing the application owning the
186*592ffb21SWarner Losh  * ref_object.
187*592ffb21SWarner Losh  * @base: The base object to reference.
188*592ffb21SWarner Losh  * @ref_type: The type of reference.
189*592ffb21SWarner Losh  * @existed: Upon completion, indicates that an identical reference object
190*592ffb21SWarner Losh  * already existed, and the refcount was upped on that object instead.
191*592ffb21SWarner Losh  *
192*592ffb21SWarner Losh  * Adding a ref object to a base object is basically like referencing the
193*592ffb21SWarner Losh  * base object, but a user-space application holds the reference. When the
194*592ffb21SWarner Losh  * file corresponding to @tfile is closed, all its reference objects are
195*592ffb21SWarner Losh  * deleted. A reference object can have different types depending on what
196*592ffb21SWarner Losh  * it's intended for. It can be refcounting to prevent object destruction,
197*592ffb21SWarner Losh  * When user-space takes a lock, it can add a ref object to that lock to
198*592ffb21SWarner Losh  * make sure the lock is released if the application dies. A ref object
199*592ffb21SWarner Losh  * will hold a single reference on a base object.
200*592ffb21SWarner Losh  */
201*592ffb21SWarner Losh extern int ttm_ref_object_add(struct ttm_object_file *tfile,
202*592ffb21SWarner Losh 			      struct ttm_base_object *base,
203*592ffb21SWarner Losh 			      enum ttm_ref_type ref_type, bool *existed);
204*592ffb21SWarner Losh /**
205*592ffb21SWarner Losh  * ttm_ref_object_base_unref
206*592ffb21SWarner Losh  *
207*592ffb21SWarner Losh  * @key: Key representing the base object.
208*592ffb21SWarner Losh  * @ref_type: Ref type of the ref object to be dereferenced.
209*592ffb21SWarner Losh  *
210*592ffb21SWarner Losh  * Unreference a ref object with type @ref_type
211*592ffb21SWarner Losh  * on the base object identified by @key. If there are no duplicate
212*592ffb21SWarner Losh  * references, the ref object will be destroyed and the base object
213*592ffb21SWarner Losh  * will be unreferenced.
214*592ffb21SWarner Losh  */
215*592ffb21SWarner Losh extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
216*592ffb21SWarner Losh 				     unsigned long key,
217*592ffb21SWarner Losh 				     enum ttm_ref_type ref_type);
218*592ffb21SWarner Losh 
219*592ffb21SWarner Losh /**
220*592ffb21SWarner Losh  * ttm_object_file_init - initialize a struct ttm_object file
221*592ffb21SWarner Losh  *
222*592ffb21SWarner Losh  * @tdev: A struct ttm_object device this file is initialized on.
223*592ffb21SWarner Losh  * @hash_order: Order of the hash table used to hold the reference objects.
224*592ffb21SWarner Losh  *
225*592ffb21SWarner Losh  * This is typically called by the file_ops::open function.
226*592ffb21SWarner Losh  */
227*592ffb21SWarner Losh 
228*592ffb21SWarner Losh extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
229*592ffb21SWarner Losh 						    *tdev,
230*592ffb21SWarner Losh 						    unsigned int hash_order);
231*592ffb21SWarner Losh 
232*592ffb21SWarner Losh /**
233*592ffb21SWarner Losh  * ttm_object_file_release - release data held by a ttm_object_file
234*592ffb21SWarner Losh  *
235*592ffb21SWarner Losh  * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
236*592ffb21SWarner Losh  * *p_tfile will be set to NULL by this function.
237*592ffb21SWarner Losh  *
238*592ffb21SWarner Losh  * Releases all data associated by a ttm_object_file.
239*592ffb21SWarner Losh  * Typically called from file_ops::release. The caller must
240*592ffb21SWarner Losh  * ensure that there are no concurrent users of tfile.
241*592ffb21SWarner Losh  */
242*592ffb21SWarner Losh 
243*592ffb21SWarner Losh extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
244*592ffb21SWarner Losh 
245*592ffb21SWarner Losh /**
246*592ffb21SWarner Losh  * ttm_object device init - initialize a struct ttm_object_device
247*592ffb21SWarner Losh  *
248*592ffb21SWarner Losh  * @hash_order: Order of hash table used to hash the base objects.
249*592ffb21SWarner Losh  *
250*592ffb21SWarner Losh  * This function is typically called on device initialization to prepare
251*592ffb21SWarner Losh  * data structures needed for ttm base and ref objects.
252*592ffb21SWarner Losh  */
253*592ffb21SWarner Losh 
254*592ffb21SWarner Losh extern struct ttm_object_device *ttm_object_device_init
255*592ffb21SWarner Losh     (struct ttm_mem_global *mem_glob, unsigned int hash_order);
256*592ffb21SWarner Losh 
257*592ffb21SWarner Losh /**
258*592ffb21SWarner Losh  * ttm_object_device_release - release data held by a ttm_object_device
259*592ffb21SWarner Losh  *
260*592ffb21SWarner Losh  * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
261*592ffb21SWarner Losh  * *p_tdev will be set to NULL by this function.
262*592ffb21SWarner Losh  *
263*592ffb21SWarner Losh  * Releases all data associated by a ttm_object_device.
264*592ffb21SWarner Losh  * Typically called from driver::unload before the destruction of the
265*592ffb21SWarner Losh  * device private data structure.
266*592ffb21SWarner Losh  */
267*592ffb21SWarner Losh 
268*592ffb21SWarner Losh extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
269*592ffb21SWarner Losh 
270*592ffb21SWarner Losh #endif
271