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