1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 /** @file ttm_object.h
32 * Base- and reference object implementation for the various
33 * ttm objects. Implements reference counting, minimal security checks
34 * and release on file close.
37 #ifndef _TTM_OBJECT_H_
38 #define _TTM_OBJECT_H_
40 #include <linux/list.h>
41 #include <drm/drm_hashtab.h>
42 #include <linux/kref.h>
43 #include <linux/rcupdate.h>
44 #include <linux/dma-buf.h>
46 #include "ttm_memory.h"
51 * Describes what type of reference a ref object holds.
53 * TTM_REF_USAGE is a simple refcount on a base object.
55 * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
58 * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
66 TTM_REF_SYNCCPU_WRITE
,
71 * enum ttm_object_type
73 * One entry per ttm object type.
74 * Device-specific types should use the
75 * ttm_driver_typex types.
78 enum ttm_object_type
{
83 ttm_driver_type0
= 256,
91 struct ttm_object_file
;
92 struct ttm_object_device
;
95 * struct ttm_base_object
97 * @hash: hash entry for the per-device object hash.
98 * @type: derived type this object is base class for.
99 * @shareable: Other ttm_object_files can access this object.
101 * @tfile: Pointer to ttm_object_file of the creator.
102 * NULL if the object was not created by a user request.
105 * @refcount: Number of references to this object, not
106 * including the hash entry. A reference to a base object can
107 * only be held by a ref object.
109 * @refcount_release: A function to be called when there are
110 * no more references to this object. This function should
111 * destroy the object (or make sure destruction eventually happens),
112 * and when it is called, the object has
113 * already been taken out of the per-device hash. The parameter
114 * "base" should be set to NULL by the function.
116 * @ref_obj_release: A function to be called when a reference object
117 * with another ttm_ref_type than TTM_REF_USAGE is deleted.
118 * This function may, for example, release a lock held by a user-space
121 * This struct is intended to be used as a base struct for objects that
122 * are visible to user-space. It provides a global name, race-safe
123 * access and refcounting, minimal access contol and hooks for unref actions.
126 struct ttm_base_object
{
127 struct rcu_head rhead
;
128 struct drm_hash_item hash
;
129 enum ttm_object_type object_type
;
131 struct ttm_object_file
*tfile
;
132 struct kref refcount
;
133 void (*refcount_release
) (struct ttm_base_object
**base
);
134 void (*ref_obj_release
) (struct ttm_base_object
*base
,
135 enum ttm_ref_type ref_type
);
140 * struct ttm_prime_object - Modified base object that is prime-aware
142 * @base: struct ttm_base_object that we derive from
143 * @mutex: Mutex protecting the @dma_buf member.
144 * @size: Size of the dma_buf associated with this object
145 * @real_type: Type of the underlying object. Needed since we're setting
146 * the value of @base::object_type to ttm_prime_type
147 * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
149 * @refcount_release: The underlying object's release method. Needed since
150 * we set @base::refcount_release to our own release method.
153 struct ttm_prime_object
{
154 struct ttm_base_object base
;
157 enum ttm_object_type real_type
;
158 struct dma_buf
*dma_buf
;
159 void (*refcount_release
) (struct ttm_base_object
**);
163 * ttm_base_object_init
165 * @tfile: Pointer to a struct ttm_object_file.
166 * @base: The struct ttm_base_object to initialize.
167 * @shareable: This object is shareable with other applcations.
168 * (different @tfile pointers.)
169 * @type: The object type.
170 * @refcount_release: See the struct ttm_base_object description.
171 * @ref_obj_release: See the struct ttm_base_object description.
173 * Initializes a struct ttm_base_object.
176 extern int ttm_base_object_init(struct ttm_object_file
*tfile
,
177 struct ttm_base_object
*base
,
179 enum ttm_object_type type
,
180 void (*refcount_release
) (struct ttm_base_object
182 void (*ref_obj_release
) (struct ttm_base_object
188 * ttm_base_object_lookup
190 * @tfile: Pointer to a struct ttm_object_file.
193 * Looks up a struct ttm_base_object with the key @key.
196 extern struct ttm_base_object
*ttm_base_object_lookup(struct ttm_object_file
197 *tfile
, uint32_t key
);
200 * ttm_base_object_lookup_for_ref
202 * @tdev: Pointer to a struct ttm_object_device.
205 * Looks up a struct ttm_base_object with the key @key.
206 * This function should only be used when the struct tfile associated with the
207 * caller doesn't yet have a reference to the base object.
210 extern struct ttm_base_object
*
211 ttm_base_object_lookup_for_ref(struct ttm_object_device
*tdev
, uint32_t key
);
214 * ttm_base_object_unref
216 * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
218 * Decrements the base object refcount and clears the pointer pointed to by
222 extern void ttm_base_object_unref(struct ttm_base_object
**p_base
);
225 * ttm_ref_object_add.
227 * @tfile: A struct ttm_object_file representing the application owning the
229 * @base: The base object to reference.
230 * @ref_type: The type of reference.
231 * @existed: Upon completion, indicates that an identical reference object
232 * already existed, and the refcount was upped on that object instead.
233 * @require_existed: Fail with -EPERM if an identical ref object didn't
236 * Checks that the base object is shareable and adds a ref object to it.
238 * Adding a ref object to a base object is basically like referencing the
239 * base object, but a user-space application holds the reference. When the
240 * file corresponding to @tfile is closed, all its reference objects are
241 * deleted. A reference object can have different types depending on what
242 * it's intended for. It can be refcounting to prevent object destruction,
243 * When user-space takes a lock, it can add a ref object to that lock to
244 * make sure the lock is released if the application dies. A ref object
245 * will hold a single reference on a base object.
247 extern int ttm_ref_object_add(struct ttm_object_file
*tfile
,
248 struct ttm_base_object
*base
,
249 enum ttm_ref_type ref_type
, bool *existed
,
250 bool require_existed
);
252 extern bool ttm_ref_object_exists(struct ttm_object_file
*tfile
,
253 struct ttm_base_object
*base
);
256 * ttm_ref_object_base_unref
258 * @key: Key representing the base object.
259 * @ref_type: Ref type of the ref object to be dereferenced.
261 * Unreference a ref object with type @ref_type
262 * on the base object identified by @key. If there are no duplicate
263 * references, the ref object will be destroyed and the base object
264 * will be unreferenced.
266 extern int ttm_ref_object_base_unref(struct ttm_object_file
*tfile
,
268 enum ttm_ref_type ref_type
);
271 * ttm_object_file_init - initialize a struct ttm_object file
273 * @tdev: A struct ttm_object device this file is initialized on.
274 * @hash_order: Order of the hash table used to hold the reference objects.
276 * This is typically called by the file_ops::open function.
279 extern struct ttm_object_file
*ttm_object_file_init(struct ttm_object_device
281 unsigned int hash_order
);
284 * ttm_object_file_release - release data held by a ttm_object_file
286 * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
287 * *p_tfile will be set to NULL by this function.
289 * Releases all data associated by a ttm_object_file.
290 * Typically called from file_ops::release. The caller must
291 * ensure that there are no concurrent users of tfile.
294 extern void ttm_object_file_release(struct ttm_object_file
**p_tfile
);
297 * ttm_object device init - initialize a struct ttm_object_device
299 * @mem_glob: struct ttm_mem_global for memory accounting.
300 * @hash_order: Order of hash table used to hash the base objects.
301 * @ops: DMA buf ops for prime objects of this device.
303 * This function is typically called on device initialization to prepare
304 * data structures needed for ttm base and ref objects.
307 extern struct ttm_object_device
*
308 ttm_object_device_init(struct ttm_mem_global
*mem_glob
,
309 unsigned int hash_order
,
310 const struct dma_buf_ops
*ops
);
313 * ttm_object_device_release - release data held by a ttm_object_device
315 * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
316 * *p_tdev will be set to NULL by this function.
318 * Releases all data associated by a ttm_object_device.
319 * Typically called from driver::unload before the destruction of the
320 * device private data structure.
323 extern void ttm_object_device_release(struct ttm_object_device
**p_tdev
);
325 #define ttm_base_object_kfree(__object, __base)\
326 kfree_rcu(__object, __base.rhead)
328 extern int ttm_prime_object_init(struct ttm_object_file
*tfile
,
330 struct ttm_prime_object
*prime
,
332 enum ttm_object_type type
,
333 void (*refcount_release
)
334 (struct ttm_base_object
**),
335 void (*ref_obj_release
)
336 (struct ttm_base_object
*,
337 enum ttm_ref_type ref_type
));
339 static inline enum ttm_object_type
340 ttm_base_object_type(struct ttm_base_object
*base
)
342 return (base
->object_type
== ttm_prime_type
) ?
343 container_of(base
, struct ttm_prime_object
, base
)->real_type
:
346 extern int ttm_prime_fd_to_handle(struct ttm_object_file
*tfile
,
347 int fd
, u32
*handle
);
348 extern int ttm_prime_handle_to_fd(struct ttm_object_file
*tfile
,
349 uint32_t handle
, uint32_t flags
,
352 #define ttm_prime_object_kfree(__obj, __prime) \
353 kfree_rcu(__obj, __prime.base.rhead)