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
56 #include <dev/drm/drmP.h>
57 #include <dev/drm/drm.h>
58 #include <dev/drm/ttm/ttm_object.h>
59 #include <dev/drm/ttm/ttm_module.h>
61 struct ttm_object_file {
62 struct ttm_object_device *tdev;
64 struct list_head ref_list;
65 struct drm_open_hash ref_hash[TTM_REF_NUM];
70 * struct ttm_object_device
72 * @object_lock: lock that protects the object_hash hash table.
74 * @object_hash: hash table for fast lookup of object global names.
76 * @object_count: Per device object count.
78 * This is the per-device data structure needed for ttm object management.
81 struct ttm_object_device {
82 struct lock object_lock;
83 struct drm_open_hash object_hash;
84 atomic_t object_count;
85 struct ttm_mem_global *mem_glob;
89 * struct ttm_ref_object
91 * @hash: Hash entry for the per-file object reference hash.
93 * @head: List entry for the per-file list of ref-objects.
97 * @obj: Base object this ref object is referencing.
99 * @ref_type: Type of ref object.
101 * This is similar to an idr object, but it also has a hash table entry
102 * that allows lookup with a pointer to the referenced object as a key. In
103 * that way, one can easily detect whether a base object is referenced by
104 * a particular ttm_object_file. It also carries a ref count to avoid creating
105 * multiple ref objects if a ttm_object_file references the same base
106 * object more than once.
109 struct ttm_ref_object {
110 struct drm_hash_item hash;
111 struct list_head head;
113 enum ttm_ref_type ref_type;
114 struct ttm_base_object *obj;
115 struct ttm_object_file *tfile;
118 MALLOC_DEFINE(M_TTM_OBJ_FILE, "ttm_obj_file", "TTM File Objects");
120 static inline struct ttm_object_file *
121 ttm_object_file_ref(struct ttm_object_file *tfile)
123 refcount_acquire(&tfile->refcount);
127 static void ttm_object_file_destroy(struct ttm_object_file *tfile)
130 drm_free(tfile, M_TTM_OBJ_FILE);
134 static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
136 struct ttm_object_file *tfile = *p_tfile;
139 if (refcount_release(&tfile->refcount))
140 ttm_object_file_destroy(tfile);
144 int ttm_base_object_init(struct ttm_object_file *tfile,
145 struct ttm_base_object *base,
147 enum ttm_object_type object_type,
148 void (*rcount_release) (struct ttm_base_object **),
149 void (*ref_obj_release) (struct ttm_base_object *,
150 enum ttm_ref_type ref_type))
152 struct ttm_object_device *tdev = tfile->tdev;
155 base->shareable = shareable;
156 base->tfile = ttm_object_file_ref(tfile);
157 base->refcount_release = rcount_release;
158 base->ref_obj_release = ref_obj_release;
159 base->object_type = object_type;
160 refcount_init(&base->refcount, 1);
161 lockinit(&tdev->object_lock, "ttmbao", 0, LK_CANRECURSE);
162 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
163 ret = drm_ht_just_insert_please(&tdev->object_hash,
165 (unsigned long)base, 31, 0, 0);
166 lockmgr(&tdev->object_lock, LK_RELEASE);
167 if (unlikely(ret != 0))
170 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
171 if (unlikely(ret != 0))
174 ttm_base_object_unref(&base);
178 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
179 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
180 lockmgr(&tdev->object_lock, LK_RELEASE);
185 static void ttm_release_base(struct ttm_base_object *base)
187 struct ttm_object_device *tdev = base->tfile->tdev;
189 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
190 lockmgr(&tdev->object_lock, LK_RELEASE);
192 * Note: We don't use synchronize_rcu() here because it's far
193 * too slow. It's up to the user to free the object using
194 * call_rcu() or ttm_base_object_kfree().
197 if (base->refcount_release) {
198 ttm_object_file_unref(&base->tfile);
199 base->refcount_release(&base);
201 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
204 void ttm_base_object_unref(struct ttm_base_object **p_base)
206 struct ttm_base_object *base = *p_base;
207 struct ttm_object_device *tdev = base->tfile->tdev;
212 * Need to take the lock here to avoid racing with
213 * users trying to look up the object.
216 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
217 if (refcount_release(&base->refcount))
218 ttm_release_base(base);
219 lockmgr(&tdev->object_lock, LK_RELEASE);
222 struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
225 struct ttm_object_device *tdev = tfile->tdev;
226 struct ttm_base_object *base;
227 struct drm_hash_item *hash;
230 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
231 ret = drm_ht_find_item(&tdev->object_hash, key, &hash);
234 base = drm_hash_entry(hash, struct ttm_base_object, hash);
235 refcount_acquire(&base->refcount);
237 lockmgr(&tdev->object_lock, LK_RELEASE);
239 if (unlikely(ret != 0))
242 if (tfile != base->tfile && !base->shareable) {
243 kprintf("[TTM] Attempted access of non-shareable object %p\n",
245 ttm_base_object_unref(&base);
252 MALLOC_DEFINE(M_TTM_OBJ_REF, "ttm_obj_ref", "TTM Ref Objects");
254 int ttm_ref_object_add(struct ttm_object_file *tfile,
255 struct ttm_base_object *base,
256 enum ttm_ref_type ref_type, bool *existed)
258 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
259 struct ttm_ref_object *ref;
260 struct drm_hash_item *hash;
261 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
267 while (ret == -EINVAL) {
268 lockmgr(&tfile->lock, LK_EXCLUSIVE);
269 ret = drm_ht_find_item(ht, base->hash.key, &hash);
272 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
273 refcount_acquire(&ref->kref);
274 lockmgr(&tfile->lock, LK_RELEASE);
278 lockmgr(&tfile->lock, LK_RELEASE);
279 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
281 if (unlikely(ret != 0))
283 ref = kmalloc(sizeof(*ref), M_TTM_OBJ_REF, M_WAITOK);
284 if (unlikely(ref == NULL)) {
285 ttm_mem_global_free(mem_glob, sizeof(*ref));
289 ref->hash.key = base->hash.key;
292 ref->ref_type = ref_type;
293 refcount_init(&ref->kref, 1);
295 lockmgr(&tfile->lock, LK_EXCLUSIVE);
296 ret = drm_ht_insert_item(ht, &ref->hash);
299 list_add_tail(&ref->head, &tfile->ref_list);
300 refcount_acquire(&base->refcount);
301 lockmgr(&tfile->lock, LK_RELEASE);
307 lockmgr(&tfile->lock, LK_RELEASE);
308 KKASSERT(ret == -EINVAL);
310 ttm_mem_global_free(mem_glob, sizeof(*ref));
311 drm_free(ref, M_TTM_OBJ_REF);
317 static void ttm_ref_object_release(struct ttm_ref_object *ref)
319 struct ttm_base_object *base = ref->obj;
320 struct ttm_object_file *tfile = ref->tfile;
321 struct drm_open_hash *ht;
322 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
324 ht = &tfile->ref_hash[ref->ref_type];
325 (void)drm_ht_remove_item(ht, &ref->hash);
326 list_del(&ref->head);
327 lockmgr(&tfile->lock, LK_RELEASE);
329 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
330 base->ref_obj_release(base, ref->ref_type);
332 ttm_base_object_unref(&ref->obj);
333 ttm_mem_global_free(mem_glob, sizeof(*ref));
334 drm_free(ref, M_TTM_OBJ_REF);
335 lockmgr(&tfile->lock, LK_EXCLUSIVE);
338 int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
339 unsigned long key, enum ttm_ref_type ref_type)
341 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
342 struct ttm_ref_object *ref;
343 struct drm_hash_item *hash;
346 lockmgr(&tfile->lock, LK_EXCLUSIVE);
347 ret = drm_ht_find_item(ht, key, &hash);
348 if (unlikely(ret != 0)) {
349 lockmgr(&tfile->lock, LK_RELEASE);
352 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
353 if (refcount_release(&ref->kref))
354 ttm_ref_object_release(ref);
355 lockmgr(&tfile->lock, LK_RELEASE);
359 void ttm_object_file_release(struct ttm_object_file **p_tfile)
361 struct ttm_ref_object *ref;
362 struct list_head *list;
364 struct ttm_object_file *tfile = *p_tfile;
367 lockmgr(&tfile->lock, LK_EXCLUSIVE);
370 * Since we release the lock within the loop, we have to
371 * restart it from the beginning each time.
374 while (!list_empty(&tfile->ref_list)) {
375 list = tfile->ref_list.next;
376 ref = list_entry(list, struct ttm_ref_object, head);
377 ttm_ref_object_release(ref);
380 for (i = 0; i < TTM_REF_NUM; ++i)
381 drm_ht_remove(&tfile->ref_hash[i]);
383 lockmgr(&tfile->lock, LK_RELEASE);
384 ttm_object_file_unref(&tfile);
387 struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
388 unsigned int hash_order)
390 struct ttm_object_file *tfile;
395 tfile = kmalloc(sizeof(*tfile), M_TTM_OBJ_FILE, M_WAITOK);
396 lockinit(&tfile->lock, "ttmfo", 0, LK_CANRECURSE);
398 refcount_init(&tfile->refcount, 1);
399 INIT_LIST_HEAD(&tfile->ref_list);
401 for (i = 0; i < TTM_REF_NUM; ++i) {
402 ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
411 for (i = 0; i < j; ++i)
412 drm_ht_remove(&tfile->ref_hash[i]);
414 drm_free(tfile, M_TTM_OBJ_FILE);
419 MALLOC_DEFINE(M_TTM_OBJ_DEV, "ttm_obj_dev", "TTM Device Objects");
421 struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
423 unsigned int hash_order)
425 struct ttm_object_device *tdev;
428 tdev = kmalloc(sizeof(*tdev), M_TTM_OBJ_DEV, M_WAITOK);
429 tdev->mem_glob = mem_glob;
430 lockinit(&tdev->object_lock, "ttmdo", 0, LK_CANRECURSE);
431 atomic_set(&tdev->object_count, 0);
432 ret = drm_ht_create(&tdev->object_hash, hash_order);
437 drm_free(tdev, M_TTM_OBJ_DEV);
441 void ttm_object_device_release(struct ttm_object_device **p_tdev)
443 struct ttm_object_device *tdev = *p_tdev;
447 lockmgr(&tdev->object_lock, LK_EXCLUSIVE);
448 drm_ht_remove(&tdev->object_hash);
449 lockmgr(&tdev->object_lock, LK_RELEASE);
451 drm_free(tdev, M_TTM_OBJ_DEV);