1 /**************************************************************************
3 * Copyright (c) 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_ref_object.c
32 * Base- and reference object implementation for the various
33 * ttm objects. Implements reference counting, minimal security checks
34 * and release on file close.
36 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_object.c 247835 2013-03-05 09:49:34Z kib $
40 * struct ttm_object_file
42 * @tdev: Pointer to the ttm_object_device.
44 * @lock: Lock that protects the ref_list list and the
45 * ref_hash hash tables.
47 * @ref_list: List of ttm_ref_objects to be destroyed at
50 * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
51 * for fast lookup of ref objects given a base object.
54 #define pr_fmt(fmt) "[TTM] " fmt
57 #include <dev/drm/ttm/ttm_object.h>
58 #include <dev/drm/ttm/ttm_module.h>
60 struct ttm_object_file {
61 struct ttm_object_device *tdev;
63 struct list_head ref_list;
64 struct drm_open_hash ref_hash[TTM_REF_NUM];
69 * struct ttm_object_device
71 * @object_lock: lock that protects the object_hash hash table.
73 * @object_hash: hash table for fast lookup of object global names.
75 * @object_count: Per device object count.
77 * This is the per-device data structure needed for ttm object management.
80 struct ttm_object_device {
81 struct lock object_lock;
82 struct drm_open_hash object_hash;
83 atomic_t object_count;
84 struct ttm_mem_global *mem_glob;
88 * struct ttm_ref_object
90 * @hash: Hash entry for the per-file object reference hash.
92 * @head: List entry for the per-file list of ref-objects.
96 * @obj: Base object this ref object is referencing.
98 * @ref_type: Type of ref object.
100 * This is similar to an idr object, but it also has a hash table entry
101 * that allows lookup with a pointer to the referenced object as a key. In
102 * that way, one can easily detect whether a base object is referenced by
103 * a particular ttm_object_file. It also carries a ref count to avoid creating
104 * multiple ref objects if a ttm_object_file references the same base
105 * object more than once.
108 struct ttm_ref_object {
109 struct drm_hash_item hash;
110 struct list_head head;
112 enum ttm_ref_type ref_type;
113 struct ttm_base_object *obj;
114 struct ttm_object_file *tfile;
117 MALLOC_DEFINE(M_TTM_OBJ_FILE, "ttm_obj_file", "TTM File Objects");
119 static inline struct ttm_object_file *
120 ttm_object_file_ref(struct ttm_object_file *tfile)
122 refcount_acquire(&tfile->refcount);
126 static void ttm_object_file_destroy(struct ttm_object_file *tfile)
129 drm_free(tfile, M_TTM_OBJ_FILE);
133 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
135 struct ttm_object_file *tfile = *p_tfile;
138 if (refcount_release(&tfile->refcount))
139 ttm_object_file_destroy(tfile);
143 int ttm_base_object_init(struct ttm_object_file *tfile,
144 struct ttm_base_object *base,
146 enum ttm_object_type object_type,
147 void (*rcount_release) (struct ttm_base_object **),
148 void (*ref_obj_release) (struct ttm_base_object *,
149 enum ttm_ref_type ref_type))
151 struct ttm_object_device *tdev = tfile->tdev;
154 base->shareable = shareable;
155 base->tfile = ttm_object_file_ref(tfile);
156 base->refcount_release = rcount_release;
157 base->ref_obj_release = ref_obj_release;
158 base->object_type = object_type;
159 refcount_init(&base->refcount, 1);
160 lockinit(&tdev->object_lock, "ttmbao", 0, LK_CANRECURSE);
161 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
162 ret = drm_ht_just_insert_please(&tdev->object_hash,
164 (unsigned long)base, 31, 0, 0);
165 lockmgr(&tdev->object_lock, LK_RELEASE);
166 if (unlikely(ret != 0))
169 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
170 if (unlikely(ret != 0))
173 ttm_base_object_unref(&base);
177 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
178 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
179 lockmgr(&tdev->object_lock, LK_RELEASE);
184 static void ttm_release_base(struct ttm_base_object *base)
186 struct ttm_object_device *tdev = base->tfile->tdev;
188 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
189 lockmgr(&tdev->object_lock, LK_RELEASE);
191 * Note: We don't use synchronize_rcu() here because it's far
192 * too slow. It's up to the user to free the object using
193 * call_rcu() or ttm_base_object_kfree().
196 if (base->refcount_release) {
197 ttm_object_file_unref(&base->tfile);
198 base->refcount_release(&base);
200 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
203 void ttm_base_object_unref(struct ttm_base_object **p_base)
205 struct ttm_base_object *base = *p_base;
206 struct ttm_object_device *tdev = base->tfile->tdev;
211 * Need to take the lock here to avoid racing with
212 * users trying to look up the object.
215 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
216 if (refcount_release(&base->refcount))
217 ttm_release_base(base);
218 lockmgr(&tdev->object_lock, LK_RELEASE);
221 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
224 struct ttm_object_device *tdev = tfile->tdev;
225 struct ttm_base_object *base;
226 struct drm_hash_item *hash;
229 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
230 ret = drm_ht_find_item(&tdev->object_hash, key, &hash);
233 base = drm_hash_entry(hash, struct ttm_base_object, hash);
234 refcount_acquire(&base->refcount);
236 lockmgr(&tdev->object_lock, LK_RELEASE);
238 if (unlikely(ret != 0))
241 if (tfile != base->tfile && !base->shareable) {
242 kprintf("[TTM] Attempted access of non-shareable object %p\n",
244 ttm_base_object_unref(&base);
251 MALLOC_DEFINE(M_TTM_OBJ_REF, "ttm_obj_ref", "TTM Ref Objects");
253 int ttm_ref_object_add(struct ttm_object_file *tfile,
254 struct ttm_base_object *base,
255 enum ttm_ref_type ref_type, bool *existed)
257 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
258 struct ttm_ref_object *ref;
259 struct drm_hash_item *hash;
260 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
266 while (ret == -EINVAL) {
267 lockmgr(&tfile->lock, LK_EXCLUSIVE);
268 ret = drm_ht_find_item(ht, base->hash.key, &hash);
271 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
272 refcount_acquire(&ref->kref);
273 lockmgr(&tfile->lock, LK_RELEASE);
277 lockmgr(&tfile->lock, LK_RELEASE);
278 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
280 if (unlikely(ret != 0))
282 ref = kmalloc(sizeof(*ref), M_TTM_OBJ_REF, M_WAITOK);
283 if (unlikely(ref == NULL)) {
284 ttm_mem_global_free(mem_glob, sizeof(*ref));
288 ref->hash.key = base->hash.key;
291 ref->ref_type = ref_type;
292 refcount_init(&ref->kref, 1);
294 lockmgr(&tfile->lock, LK_EXCLUSIVE);
295 ret = drm_ht_insert_item(ht, &ref->hash);
298 list_add_tail(&ref->head, &tfile->ref_list);
299 refcount_acquire(&base->refcount);
300 lockmgr(&tfile->lock, LK_RELEASE);
306 lockmgr(&tfile->lock, LK_RELEASE);
307 KKASSERT(ret == -EINVAL);
309 ttm_mem_global_free(mem_glob, sizeof(*ref));
310 drm_free(ref, M_TTM_OBJ_REF);
316 static void ttm_ref_object_release(struct ttm_ref_object *ref)
318 struct ttm_base_object *base = ref->obj;
319 struct ttm_object_file *tfile = ref->tfile;
320 struct drm_open_hash *ht;
321 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
323 ht = &tfile->ref_hash[ref->ref_type];
324 (void)drm_ht_remove_item(ht, &ref->hash);
325 list_del(&ref->head);
326 lockmgr(&tfile->lock, LK_RELEASE);
328 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
329 base->ref_obj_release(base, ref->ref_type);
331 ttm_base_object_unref(&ref->obj);
332 ttm_mem_global_free(mem_glob, sizeof(*ref));
333 drm_free(ref, M_TTM_OBJ_REF);
334 lockmgr(&tfile->lock, LK_EXCLUSIVE);
337 int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
338 unsigned long key, enum ttm_ref_type ref_type)
340 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
341 struct ttm_ref_object *ref;
342 struct drm_hash_item *hash;
345 lockmgr(&tfile->lock, LK_EXCLUSIVE);
346 ret = drm_ht_find_item(ht, key, &hash);
347 if (unlikely(ret != 0)) {
348 lockmgr(&tfile->lock, LK_RELEASE);
351 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
352 if (refcount_release(&ref->kref))
353 ttm_ref_object_release(ref);
354 lockmgr(&tfile->lock, LK_RELEASE);
358 void ttm_object_file_release(struct ttm_object_file **p_tfile)
360 struct ttm_ref_object *ref;
361 struct list_head *list;
363 struct ttm_object_file *tfile = *p_tfile;
366 lockmgr(&tfile->lock, LK_EXCLUSIVE);
369 * Since we release the lock within the loop, we have to
370 * restart it from the beginning each time.
373 while (!list_empty(&tfile->ref_list)) {
374 list = tfile->ref_list.next;
375 ref = list_entry(list, struct ttm_ref_object, head);
376 ttm_ref_object_release(ref);
379 for (i = 0; i < TTM_REF_NUM; ++i)
380 drm_ht_remove(&tfile->ref_hash[i]);
382 lockmgr(&tfile->lock, LK_RELEASE);
383 ttm_object_file_unref(&tfile);
386 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
387 unsigned int hash_order)
389 struct ttm_object_file *tfile;
394 tfile = kmalloc(sizeof(*tfile), M_TTM_OBJ_FILE, M_WAITOK);
395 lockinit(&tfile->lock, "ttmfo", 0, LK_CANRECURSE);
397 refcount_init(&tfile->refcount, 1);
398 INIT_LIST_HEAD(&tfile->ref_list);
400 for (i = 0; i < TTM_REF_NUM; ++i) {
401 ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
410 for (i = 0; i < j; ++i)
411 drm_ht_remove(&tfile->ref_hash[i]);
413 drm_free(tfile, M_TTM_OBJ_FILE);
418 MALLOC_DEFINE(M_TTM_OBJ_DEV, "ttm_obj_dev", "TTM Device Objects");
420 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
422 unsigned int hash_order)
424 struct ttm_object_device *tdev;
427 tdev = kmalloc(sizeof(*tdev), M_TTM_OBJ_DEV, M_WAITOK);
428 tdev->mem_glob = mem_glob;
429 lockinit(&tdev->object_lock, "ttmdo", 0, LK_CANRECURSE);
430 atomic_set(&tdev->object_count, 0);
431 ret = drm_ht_create(&tdev->object_hash, hash_order);
436 drm_free(tdev, M_TTM_OBJ_DEV);
440 void ttm_object_device_release(struct ttm_object_device **p_tdev)
442 struct ttm_object_device *tdev = *p_tdev;
446 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
447 drm_ht_remove(&tdev->object_hash);
448 lockmgr(&tdev->object_lock, LK_RELEASE);
450 drm_free(tdev, M_TTM_OBJ_DEV);