blob: f18eeb458139baee015721e8889adfc2959dd219 [file] [log] [blame]
Thomas Hellstrom88071532009-12-06 21:46:24 +01001/**************************************************************************
2 *
3 * Copyright (c) 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_ref_object.c
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/**
38 * struct ttm_object_file
39 *
40 * @tdev: Pointer to the ttm_object_device.
41 *
42 * @lock: Lock that protects the ref_list list and the
43 * ref_hash hash tables.
44 *
45 * @ref_list: List of ttm_ref_objects to be destroyed at
46 * file release.
47 *
48 * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
49 * for fast lookup of ref objects given a base object.
50 */
51
Joe Perches25d04792012-03-16 21:43:50 -070052#define pr_fmt(fmt) "[TTM] " fmt
53
David Howells760285e2012-10-02 18:01:07 +010054#include <drm/ttm/ttm_object.h>
55#include <drm/ttm/ttm_module.h>
Thomas Hellstrom88071532009-12-06 21:46:24 +010056#include <linux/list.h>
57#include <linux/spinlock.h>
58#include <linux/slab.h>
59#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Thomas Hellstrom88071532009-12-06 21:46:24 +010061
62struct ttm_object_file {
63 struct ttm_object_device *tdev;
64 rwlock_t lock;
65 struct list_head ref_list;
66 struct drm_open_hash ref_hash[TTM_REF_NUM];
67 struct kref refcount;
68};
69
70/**
71 * struct ttm_object_device
72 *
73 * @object_lock: lock that protects the object_hash hash table.
74 *
75 * @object_hash: hash table for fast lookup of object global names.
76 *
77 * @object_count: Per device object count.
78 *
79 * This is the per-device data structure needed for ttm object management.
80 */
81
82struct ttm_object_device {
Thomas Hellstromcdad0522012-11-06 11:31:50 +000083 spinlock_t object_lock;
Thomas Hellstrom88071532009-12-06 21:46:24 +010084 struct drm_open_hash object_hash;
85 atomic_t object_count;
86 struct ttm_mem_global *mem_glob;
87};
88
89/**
90 * struct ttm_ref_object
91 *
92 * @hash: Hash entry for the per-file object reference hash.
93 *
94 * @head: List entry for the per-file list of ref-objects.
95 *
96 * @kref: Ref count.
97 *
98 * @obj: Base object this ref object is referencing.
99 *
100 * @ref_type: Type of ref object.
101 *
102 * This is similar to an idr object, but it also has a hash table entry
103 * that allows lookup with a pointer to the referenced object as a key. In
104 * that way, one can easily detect whether a base object is referenced by
105 * a particular ttm_object_file. It also carries a ref count to avoid creating
106 * multiple ref objects if a ttm_object_file references the same base
107 * object more than once.
108 */
109
110struct ttm_ref_object {
111 struct drm_hash_item hash;
112 struct list_head head;
113 struct kref kref;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100114 enum ttm_ref_type ref_type;
Richard Kennedydd5fde62010-01-26 17:10:48 +0000115 struct ttm_base_object *obj;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100116 struct ttm_object_file *tfile;
117};
118
119static inline struct ttm_object_file *
120ttm_object_file_ref(struct ttm_object_file *tfile)
121{
122 kref_get(&tfile->refcount);
123 return tfile;
124}
125
126static void ttm_object_file_destroy(struct kref *kref)
127{
128 struct ttm_object_file *tfile =
129 container_of(kref, struct ttm_object_file, refcount);
130
131 kfree(tfile);
132}
133
134
135static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
136{
137 struct ttm_object_file *tfile = *p_tfile;
138
139 *p_tfile = NULL;
140 kref_put(&tfile->refcount, ttm_object_file_destroy);
141}
142
143
144int ttm_base_object_init(struct ttm_object_file *tfile,
145 struct ttm_base_object *base,
146 bool shareable,
147 enum ttm_object_type object_type,
148 void (*refcount_release) (struct ttm_base_object **),
149 void (*ref_obj_release) (struct ttm_base_object *,
150 enum ttm_ref_type ref_type))
151{
152 struct ttm_object_device *tdev = tfile->tdev;
153 int ret;
154
155 base->shareable = shareable;
156 base->tfile = ttm_object_file_ref(tfile);
157 base->refcount_release = refcount_release;
158 base->ref_obj_release = ref_obj_release;
159 base->object_type = object_type;
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000160 spin_lock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100161 kref_init(&base->refcount);
162 ret = drm_ht_just_insert_please(&tdev->object_hash,
163 &base->hash,
164 (unsigned long)base, 31, 0, 0);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000165 spin_unlock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100166 if (unlikely(ret != 0))
167 goto out_err0;
168
169 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
170 if (unlikely(ret != 0))
171 goto out_err1;
172
173 ttm_base_object_unref(&base);
174
175 return 0;
176out_err1:
177 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
178out_err0:
179 return ret;
180}
181EXPORT_SYMBOL(ttm_base_object_init);
182
183static void ttm_release_base(struct kref *kref)
184{
185 struct ttm_base_object *base =
186 container_of(kref, struct ttm_base_object, refcount);
187 struct ttm_object_device *tdev = base->tfile->tdev;
188
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000189 spin_lock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100190 (void)drm_ht_remove_item(&tdev->object_hash, &base->hash);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000191 spin_unlock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100192 if (base->refcount_release) {
193 ttm_object_file_unref(&base->tfile);
194 base->refcount_release(&base);
195 }
Thomas Hellstrom88071532009-12-06 21:46:24 +0100196}
197
198void ttm_base_object_unref(struct ttm_base_object **p_base)
199{
200 struct ttm_base_object *base = *p_base;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100201
202 *p_base = NULL;
203
Paul Bollecdb650a2011-02-27 01:34:08 +0100204 kref_put(&base->refcount, ttm_release_base);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100205}
206EXPORT_SYMBOL(ttm_base_object_unref);
207
208struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
209 uint32_t key)
210{
211 struct ttm_object_device *tdev = tfile->tdev;
212 struct ttm_base_object *base;
213 struct drm_hash_item *hash;
214 int ret;
215
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000216 rcu_read_lock();
Thomas Hellstrom88071532009-12-06 21:46:24 +0100217 ret = drm_ht_find_item(&tdev->object_hash, key, &hash);
218
219 if (likely(ret == 0)) {
220 base = drm_hash_entry(hash, struct ttm_base_object, hash);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000221 ret = kref_get_unless_zero(&base->refcount) ? 0 : -EINVAL;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100222 }
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000223 rcu_read_unlock();
Thomas Hellstrom88071532009-12-06 21:46:24 +0100224
225 if (unlikely(ret != 0))
226 return NULL;
227
228 if (tfile != base->tfile && !base->shareable) {
Joe Perches25d04792012-03-16 21:43:50 -0700229 pr_err("Attempted access of non-shareable object\n");
Thomas Hellstrom88071532009-12-06 21:46:24 +0100230 ttm_base_object_unref(&base);
231 return NULL;
232 }
233
234 return base;
235}
236EXPORT_SYMBOL(ttm_base_object_lookup);
237
238int ttm_ref_object_add(struct ttm_object_file *tfile,
239 struct ttm_base_object *base,
240 enum ttm_ref_type ref_type, bool *existed)
241{
242 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
243 struct ttm_ref_object *ref;
244 struct drm_hash_item *hash;
245 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
246 int ret = -EINVAL;
247
248 if (existed != NULL)
249 *existed = true;
250
251 while (ret == -EINVAL) {
252 read_lock(&tfile->lock);
253 ret = drm_ht_find_item(ht, base->hash.key, &hash);
254
255 if (ret == 0) {
256 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
257 kref_get(&ref->kref);
258 read_unlock(&tfile->lock);
259 break;
260 }
261
262 read_unlock(&tfile->lock);
263 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
264 false, false);
265 if (unlikely(ret != 0))
266 return ret;
267 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
268 if (unlikely(ref == NULL)) {
269 ttm_mem_global_free(mem_glob, sizeof(*ref));
270 return -ENOMEM;
271 }
272
273 ref->hash.key = base->hash.key;
274 ref->obj = base;
275 ref->tfile = tfile;
276 ref->ref_type = ref_type;
277 kref_init(&ref->kref);
278
279 write_lock(&tfile->lock);
280 ret = drm_ht_insert_item(ht, &ref->hash);
281
282 if (likely(ret == 0)) {
283 list_add_tail(&ref->head, &tfile->ref_list);
284 kref_get(&base->refcount);
285 write_unlock(&tfile->lock);
286 if (existed != NULL)
287 *existed = false;
288 break;
289 }
290
291 write_unlock(&tfile->lock);
292 BUG_ON(ret != -EINVAL);
293
294 ttm_mem_global_free(mem_glob, sizeof(*ref));
295 kfree(ref);
296 }
297
298 return ret;
299}
300EXPORT_SYMBOL(ttm_ref_object_add);
301
302static void ttm_ref_object_release(struct kref *kref)
303{
304 struct ttm_ref_object *ref =
305 container_of(kref, struct ttm_ref_object, kref);
306 struct ttm_base_object *base = ref->obj;
307 struct ttm_object_file *tfile = ref->tfile;
308 struct drm_open_hash *ht;
309 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
310
311 ht = &tfile->ref_hash[ref->ref_type];
312 (void)drm_ht_remove_item(ht, &ref->hash);
313 list_del(&ref->head);
314 write_unlock(&tfile->lock);
315
316 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
317 base->ref_obj_release(base, ref->ref_type);
318
319 ttm_base_object_unref(&ref->obj);
320 ttm_mem_global_free(mem_glob, sizeof(*ref));
321 kfree(ref);
322 write_lock(&tfile->lock);
323}
324
325int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
326 unsigned long key, enum ttm_ref_type ref_type)
327{
328 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
329 struct ttm_ref_object *ref;
330 struct drm_hash_item *hash;
331 int ret;
332
333 write_lock(&tfile->lock);
334 ret = drm_ht_find_item(ht, key, &hash);
335 if (unlikely(ret != 0)) {
336 write_unlock(&tfile->lock);
337 return -EINVAL;
338 }
339 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
340 kref_put(&ref->kref, ttm_ref_object_release);
341 write_unlock(&tfile->lock);
342 return 0;
343}
344EXPORT_SYMBOL(ttm_ref_object_base_unref);
345
346void ttm_object_file_release(struct ttm_object_file **p_tfile)
347{
348 struct ttm_ref_object *ref;
349 struct list_head *list;
350 unsigned int i;
351 struct ttm_object_file *tfile = *p_tfile;
352
353 *p_tfile = NULL;
354 write_lock(&tfile->lock);
355
356 /*
357 * Since we release the lock within the loop, we have to
358 * restart it from the beginning each time.
359 */
360
361 while (!list_empty(&tfile->ref_list)) {
362 list = tfile->ref_list.next;
363 ref = list_entry(list, struct ttm_ref_object, head);
364 ttm_ref_object_release(&ref->kref);
365 }
366
367 for (i = 0; i < TTM_REF_NUM; ++i)
368 drm_ht_remove(&tfile->ref_hash[i]);
369
370 write_unlock(&tfile->lock);
371 ttm_object_file_unref(&tfile);
372}
373EXPORT_SYMBOL(ttm_object_file_release);
374
375struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
376 unsigned int hash_order)
377{
378 struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
379 unsigned int i;
380 unsigned int j = 0;
381 int ret;
382
383 if (unlikely(tfile == NULL))
384 return NULL;
385
386 rwlock_init(&tfile->lock);
387 tfile->tdev = tdev;
388 kref_init(&tfile->refcount);
389 INIT_LIST_HEAD(&tfile->ref_list);
390
391 for (i = 0; i < TTM_REF_NUM; ++i) {
392 ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
393 if (ret) {
394 j = i;
395 goto out_err;
396 }
397 }
398
399 return tfile;
400out_err:
401 for (i = 0; i < j; ++i)
402 drm_ht_remove(&tfile->ref_hash[i]);
403
404 kfree(tfile);
405
406 return NULL;
407}
408EXPORT_SYMBOL(ttm_object_file_init);
409
410struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global
411 *mem_glob,
412 unsigned int hash_order)
413{
414 struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
415 int ret;
416
417 if (unlikely(tdev == NULL))
418 return NULL;
419
420 tdev->mem_glob = mem_glob;
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000421 spin_lock_init(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100422 atomic_set(&tdev->object_count, 0);
423 ret = drm_ht_create(&tdev->object_hash, hash_order);
424
425 if (likely(ret == 0))
426 return tdev;
427
428 kfree(tdev);
429 return NULL;
430}
431EXPORT_SYMBOL(ttm_object_device_init);
432
433void ttm_object_device_release(struct ttm_object_device **p_tdev)
434{
435 struct ttm_object_device *tdev = *p_tdev;
436
437 *p_tdev = NULL;
438
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000439 spin_lock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100440 drm_ht_remove(&tdev->object_hash);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000441 spin_unlock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100442
443 kfree(tdev);
444}
445EXPORT_SYMBOL(ttm_object_device_release);