diff options
-rw-r--r-- | drivers/gpu/drm/ttm/Makefile | 3 | ||||
-rw-r--r-- | drivers/gpu/drm/ttm/ttm_object.c | 452 | ||||
-rw-r--r-- | include/drm/ttm/ttm_object.h | 267 |
3 files changed, 721 insertions, 1 deletions
diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile index b0a9de7a57c2..a9cc9f895360 100644 --- a/drivers/gpu/drm/ttm/Makefile +++ b/drivers/gpu/drm/ttm/Makefile | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | ccflags-y := -Iinclude/drm | 4 | ccflags-y := -Iinclude/drm |
5 | ttm-y := ttm_agp_backend.o ttm_memory.o ttm_tt.o ttm_bo.o \ | 5 | ttm-y := ttm_agp_backend.o ttm_memory.o ttm_tt.o ttm_bo.o \ |
6 | ttm_bo_util.o ttm_bo_vm.o ttm_module.o ttm_global.o | 6 | ttm_bo_util.o ttm_bo_vm.o ttm_module.o ttm_global.o \ |
7 | ttm_object.o | ||
7 | 8 | ||
8 | obj-$(CONFIG_DRM_TTM) += ttm.o | 9 | obj-$(CONFIG_DRM_TTM) += ttm.o |
diff --git a/drivers/gpu/drm/ttm/ttm_object.c b/drivers/gpu/drm/ttm/ttm_object.c new file mode 100644 index 000000000000..1099abac824b --- /dev/null +++ b/drivers/gpu/drm/ttm/ttm_object.c | |||
@@ -0,0 +1,452 @@ | |||
1 | /************************************************************************** | ||
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 | |||
52 | #include "ttm/ttm_object.h" | ||
53 | #include "ttm/ttm_module.h" | ||
54 | #include <linux/list.h> | ||
55 | #include <linux/spinlock.h> | ||
56 | #include <linux/slab.h> | ||
57 | #include <linux/module.h> | ||
58 | #include <asm/atomic.h> | ||
59 | |||
60 | struct ttm_object_file { | ||
61 | struct ttm_object_device *tdev; | ||
62 | rwlock_t lock; | ||
63 | struct list_head ref_list; | ||
64 | struct drm_open_hash ref_hash[TTM_REF_NUM]; | ||
65 | struct kref refcount; | ||
66 | }; | ||
67 | |||
68 | /** | ||
69 | * struct ttm_object_device | ||
70 | * | ||
71 | * @object_lock: lock that protects the object_hash hash table. | ||
72 | * | ||
73 | * @object_hash: hash table for fast lookup of object global names. | ||
74 | * | ||
75 | * @object_count: Per device object count. | ||
76 | * | ||
77 | * This is the per-device data structure needed for ttm object management. | ||
78 | */ | ||
79 | |||
80 | struct ttm_object_device { | ||
81 | rwlock_t object_lock; | ||
82 | struct drm_open_hash object_hash; | ||
83 | atomic_t object_count; | ||
84 | struct ttm_mem_global *mem_glob; | ||
85 | }; | ||
86 | |||
87 | /** | ||
88 | * struct ttm_ref_object | ||
89 | * | ||
90 | * @hash: Hash entry for the per-file object reference hash. | ||
91 | * | ||
92 | * @head: List entry for the per-file list of ref-objects. | ||
93 | * | ||
94 | * @kref: Ref count. | ||
95 | * | ||
96 | * @obj: Base object this ref object is referencing. | ||
97 | * | ||
98 | * @ref_type: Type of ref object. | ||
99 | * | ||
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. | ||
106 | */ | ||
107 | |||
108 | struct ttm_ref_object { | ||
109 | struct drm_hash_item hash; | ||
110 | struct list_head head; | ||
111 | struct kref kref; | ||
112 | struct ttm_base_object *obj; | ||
113 | enum ttm_ref_type ref_type; | ||
114 | struct ttm_object_file *tfile; | ||
115 | }; | ||
116 | |||
117 | static inline struct ttm_object_file * | ||
118 | ttm_object_file_ref(struct ttm_object_file *tfile) | ||
119 | { | ||
120 | kref_get(&tfile->refcount); | ||
121 | return tfile; | ||
122 | } | ||
123 | |||
124 | static void ttm_object_file_destroy(struct kref *kref) | ||
125 | { | ||
126 | struct ttm_object_file *tfile = | ||
127 | container_of(kref, struct ttm_object_file, refcount); | ||
128 | |||
129 | kfree(tfile); | ||
130 | } | ||
131 | |||
132 | |||
133 | static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile) | ||
134 | { | ||
135 | struct ttm_object_file *tfile = *p_tfile; | ||
136 | |||
137 | *p_tfile = NULL; | ||
138 | kref_put(&tfile->refcount, ttm_object_file_destroy); | ||
139 | } | ||
140 | |||
141 | |||
142 | int ttm_base_object_init(struct ttm_object_file *tfile, | ||
143 | struct ttm_base_object *base, | ||
144 | bool shareable, | ||
145 | enum ttm_object_type object_type, | ||
146 | void (*refcount_release) (struct ttm_base_object **), | ||
147 | void (*ref_obj_release) (struct ttm_base_object *, | ||
148 | enum ttm_ref_type ref_type)) | ||
149 | { | ||
150 | struct ttm_object_device *tdev = tfile->tdev; | ||
151 | int ret; | ||
152 | |||
153 | base->shareable = shareable; | ||
154 | base->tfile = ttm_object_file_ref(tfile); | ||
155 | base->refcount_release = refcount_release; | ||
156 | base->ref_obj_release = ref_obj_release; | ||
157 | base->object_type = object_type; | ||
158 | write_lock(&tdev->object_lock); | ||
159 | kref_init(&base->refcount); | ||
160 | ret = drm_ht_just_insert_please(&tdev->object_hash, | ||
161 | &base->hash, | ||
162 | (unsigned long)base, 31, 0, 0); | ||
163 | write_unlock(&tdev->object_lock); | ||
164 | if (unlikely(ret != 0)) | ||
165 | goto out_err0; | ||
166 | |||
167 | ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL); | ||
168 | if (unlikely(ret != 0)) | ||
169 | goto out_err1; | ||
170 | |||
171 | ttm_base_object_unref(&base); | ||
172 | |||
173 | return 0; | ||
174 | out_err1: | ||
175 | (void)drm_ht_remove_item(&tdev->object_hash, &base->hash); | ||
176 | out_err0: | ||
177 | return ret; | ||
178 | } | ||
179 | EXPORT_SYMBOL(ttm_base_object_init); | ||
180 | |||
181 | static void ttm_release_base(struct kref *kref) | ||
182 | { | ||
183 | struct ttm_base_object *base = | ||
184 | container_of(kref, struct ttm_base_object, refcount); | ||
185 | struct ttm_object_device *tdev = base->tfile->tdev; | ||
186 | |||
187 | (void)drm_ht_remove_item(&tdev->object_hash, &base->hash); | ||
188 | write_unlock(&tdev->object_lock); | ||
189 | if (base->refcount_release) { | ||
190 | ttm_object_file_unref(&base->tfile); | ||
191 | base->refcount_release(&base); | ||
192 | } | ||
193 | write_lock(&tdev->object_lock); | ||
194 | } | ||
195 | |||
196 | void ttm_base_object_unref(struct ttm_base_object **p_base) | ||
197 | { | ||
198 | struct ttm_base_object *base = *p_base; | ||
199 | struct ttm_object_device *tdev = base->tfile->tdev; | ||
200 | |||
201 | *p_base = NULL; | ||
202 | |||
203 | /* | ||
204 | * Need to take the lock here to avoid racing with | ||
205 | * users trying to look up the object. | ||
206 | */ | ||
207 | |||
208 | write_lock(&tdev->object_lock); | ||
209 | (void)kref_put(&base->refcount, &ttm_release_base); | ||
210 | write_unlock(&tdev->object_lock); | ||
211 | } | ||
212 | EXPORT_SYMBOL(ttm_base_object_unref); | ||
213 | |||
214 | struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile, | ||
215 | uint32_t key) | ||
216 | { | ||
217 | struct ttm_object_device *tdev = tfile->tdev; | ||
218 | struct ttm_base_object *base; | ||
219 | struct drm_hash_item *hash; | ||
220 | int ret; | ||
221 | |||
222 | read_lock(&tdev->object_lock); | ||
223 | ret = drm_ht_find_item(&tdev->object_hash, key, &hash); | ||
224 | |||
225 | if (likely(ret == 0)) { | ||
226 | base = drm_hash_entry(hash, struct ttm_base_object, hash); | ||
227 | kref_get(&base->refcount); | ||
228 | } | ||
229 | read_unlock(&tdev->object_lock); | ||
230 | |||
231 | if (unlikely(ret != 0)) | ||
232 | return NULL; | ||
233 | |||
234 | if (tfile != base->tfile && !base->shareable) { | ||
235 | printk(KERN_ERR TTM_PFX | ||
236 | "Attempted access of non-shareable object.\n"); | ||
237 | ttm_base_object_unref(&base); | ||
238 | return NULL; | ||
239 | } | ||
240 | |||
241 | return base; | ||
242 | } | ||
243 | EXPORT_SYMBOL(ttm_base_object_lookup); | ||
244 | |||
245 | int ttm_ref_object_add(struct ttm_object_file *tfile, | ||
246 | struct ttm_base_object *base, | ||
247 | enum ttm_ref_type ref_type, bool *existed) | ||
248 | { | ||
249 | struct drm_open_hash *ht = &tfile->ref_hash[ref_type]; | ||
250 | struct ttm_ref_object *ref; | ||
251 | struct drm_hash_item *hash; | ||
252 | struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob; | ||
253 | int ret = -EINVAL; | ||
254 | |||
255 | if (existed != NULL) | ||
256 | *existed = true; | ||
257 | |||
258 | while (ret == -EINVAL) { | ||
259 | read_lock(&tfile->lock); | ||
260 | ret = drm_ht_find_item(ht, base->hash.key, &hash); | ||
261 | |||
262 | if (ret == 0) { | ||
263 | ref = drm_hash_entry(hash, struct ttm_ref_object, hash); | ||
264 | kref_get(&ref->kref); | ||
265 | read_unlock(&tfile->lock); | ||
266 | break; | ||
267 | } | ||
268 | |||
269 | read_unlock(&tfile->lock); | ||
270 | ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref), | ||
271 | false, false); | ||
272 | if (unlikely(ret != 0)) | ||
273 | return ret; | ||
274 | ref = kmalloc(sizeof(*ref), GFP_KERNEL); | ||
275 | if (unlikely(ref == NULL)) { | ||
276 | ttm_mem_global_free(mem_glob, sizeof(*ref)); | ||
277 | return -ENOMEM; | ||
278 | } | ||
279 | |||
280 | ref->hash.key = base->hash.key; | ||
281 | ref->obj = base; | ||
282 | ref->tfile = tfile; | ||
283 | ref->ref_type = ref_type; | ||
284 | kref_init(&ref->kref); | ||
285 | |||
286 | write_lock(&tfile->lock); | ||
287 | ret = drm_ht_insert_item(ht, &ref->hash); | ||
288 | |||
289 | if (likely(ret == 0)) { | ||
290 | list_add_tail(&ref->head, &tfile->ref_list); | ||
291 | kref_get(&base->refcount); | ||
292 | write_unlock(&tfile->lock); | ||
293 | if (existed != NULL) | ||
294 | *existed = false; | ||
295 | break; | ||
296 | } | ||
297 | |||
298 | write_unlock(&tfile->lock); | ||
299 | BUG_ON(ret != -EINVAL); | ||
300 | |||
301 | ttm_mem_global_free(mem_glob, sizeof(*ref)); | ||
302 | kfree(ref); | ||
303 | } | ||
304 | |||
305 | return ret; | ||
306 | } | ||
307 | EXPORT_SYMBOL(ttm_ref_object_add); | ||
308 | |||
309 | static void ttm_ref_object_release(struct kref *kref) | ||
310 | { | ||
311 | struct ttm_ref_object *ref = | ||
312 | container_of(kref, struct ttm_ref_object, kref); | ||
313 | struct ttm_base_object *base = ref->obj; | ||
314 | struct ttm_object_file *tfile = ref->tfile; | ||
315 | struct drm_open_hash *ht; | ||
316 | struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob; | ||
317 | |||
318 | ht = &tfile->ref_hash[ref->ref_type]; | ||
319 | (void)drm_ht_remove_item(ht, &ref->hash); | ||
320 | list_del(&ref->head); | ||
321 | write_unlock(&tfile->lock); | ||
322 | |||
323 | if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release) | ||
324 | base->ref_obj_release(base, ref->ref_type); | ||
325 | |||
326 | ttm_base_object_unref(&ref->obj); | ||
327 | ttm_mem_global_free(mem_glob, sizeof(*ref)); | ||
328 | kfree(ref); | ||
329 | write_lock(&tfile->lock); | ||
330 | } | ||
331 | |||
332 | int ttm_ref_object_base_unref(struct ttm_object_file *tfile, | ||
333 | unsigned long key, enum ttm_ref_type ref_type) | ||
334 | { | ||
335 | struct drm_open_hash *ht = &tfile->ref_hash[ref_type]; | ||
336 | struct ttm_ref_object *ref; | ||
337 | struct drm_hash_item *hash; | ||
338 | int ret; | ||
339 | |||
340 | write_lock(&tfile->lock); | ||
341 | ret = drm_ht_find_item(ht, key, &hash); | ||
342 | if (unlikely(ret != 0)) { | ||
343 | write_unlock(&tfile->lock); | ||
344 | return -EINVAL; | ||
345 | } | ||
346 | ref = drm_hash_entry(hash, struct ttm_ref_object, hash); | ||
347 | kref_put(&ref->kref, ttm_ref_object_release); | ||
348 | write_unlock(&tfile->lock); | ||
349 | return 0; | ||
350 | } | ||
351 | EXPORT_SYMBOL(ttm_ref_object_base_unref); | ||
352 | |||
353 | void ttm_object_file_release(struct ttm_object_file **p_tfile) | ||
354 | { | ||
355 | struct ttm_ref_object *ref; | ||
356 | struct list_head *list; | ||
357 | unsigned int i; | ||
358 | struct ttm_object_file *tfile = *p_tfile; | ||
359 | |||
360 | *p_tfile = NULL; | ||
361 | write_lock(&tfile->lock); | ||
362 | |||
363 | /* | ||
364 | * Since we release the lock within the loop, we have to | ||
365 | * restart it from the beginning each time. | ||
366 | */ | ||
367 | |||
368 | while (!list_empty(&tfile->ref_list)) { | ||
369 | list = tfile->ref_list.next; | ||
370 | ref = list_entry(list, struct ttm_ref_object, head); | ||
371 | ttm_ref_object_release(&ref->kref); | ||
372 | } | ||
373 | |||
374 | for (i = 0; i < TTM_REF_NUM; ++i) | ||
375 | drm_ht_remove(&tfile->ref_hash[i]); | ||
376 | |||
377 | write_unlock(&tfile->lock); | ||
378 | ttm_object_file_unref(&tfile); | ||
379 | } | ||
380 | EXPORT_SYMBOL(ttm_object_file_release); | ||
381 | |||
382 | struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev, | ||
383 | unsigned int hash_order) | ||
384 | { | ||
385 | struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL); | ||
386 | unsigned int i; | ||
387 | unsigned int j = 0; | ||
388 | int ret; | ||
389 | |||
390 | if (unlikely(tfile == NULL)) | ||
391 | return NULL; | ||
392 | |||
393 | rwlock_init(&tfile->lock); | ||
394 | tfile->tdev = tdev; | ||
395 | kref_init(&tfile->refcount); | ||
396 | INIT_LIST_HEAD(&tfile->ref_list); | ||
397 | |||
398 | for (i = 0; i < TTM_REF_NUM; ++i) { | ||
399 | ret = drm_ht_create(&tfile->ref_hash[i], hash_order); | ||
400 | if (ret) { | ||
401 | j = i; | ||
402 | goto out_err; | ||
403 | } | ||
404 | } | ||
405 | |||
406 | return tfile; | ||
407 | out_err: | ||
408 | for (i = 0; i < j; ++i) | ||
409 | drm_ht_remove(&tfile->ref_hash[i]); | ||
410 | |||
411 | kfree(tfile); | ||
412 | |||
413 | return NULL; | ||
414 | } | ||
415 | EXPORT_SYMBOL(ttm_object_file_init); | ||
416 | |||
417 | struct ttm_object_device *ttm_object_device_init(struct ttm_mem_global | ||
418 | *mem_glob, | ||
419 | unsigned int hash_order) | ||
420 | { | ||
421 | struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL); | ||
422 | int ret; | ||
423 | |||
424 | if (unlikely(tdev == NULL)) | ||
425 | return NULL; | ||
426 | |||
427 | tdev->mem_glob = mem_glob; | ||
428 | rwlock_init(&tdev->object_lock); | ||
429 | atomic_set(&tdev->object_count, 0); | ||
430 | ret = drm_ht_create(&tdev->object_hash, hash_order); | ||
431 | |||
432 | if (likely(ret == 0)) | ||
433 | return tdev; | ||
434 | |||
435 | kfree(tdev); | ||
436 | return NULL; | ||
437 | } | ||
438 | EXPORT_SYMBOL(ttm_object_device_init); | ||
439 | |||
440 | void ttm_object_device_release(struct ttm_object_device **p_tdev) | ||
441 | { | ||
442 | struct ttm_object_device *tdev = *p_tdev; | ||
443 | |||
444 | *p_tdev = NULL; | ||
445 | |||
446 | write_lock(&tdev->object_lock); | ||
447 | drm_ht_remove(&tdev->object_hash); | ||
448 | write_unlock(&tdev->object_lock); | ||
449 | |||
450 | kfree(tdev); | ||
451 | } | ||
452 | EXPORT_SYMBOL(ttm_object_device_release); | ||
diff --git a/include/drm/ttm/ttm_object.h b/include/drm/ttm/ttm_object.h new file mode 100644 index 000000000000..703ca4db0a29 --- /dev/null +++ b/include/drm/ttm/ttm_object.h | |||
@@ -0,0 +1,267 @@ | |||
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 <linux/list.h> | ||
41 | #include "drm_hashtab.h" | ||
42 | #include <linux/kref.h> | ||
43 | #include <ttm/ttm_memory.h> | ||
44 | |||
45 | /** | ||
46 | * enum ttm_ref_type | ||
47 | * | ||
48 | * Describes what type of reference a ref object holds. | ||
49 | * | ||
50 | * TTM_REF_USAGE is a simple refcount on a base object. | ||
51 | * | ||
52 | * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a | ||
53 | * buffer object. | ||
54 | * | ||
55 | * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a | ||
56 | * buffer object. | ||
57 | * | ||
58 | */ | ||
59 | |||
60 | enum ttm_ref_type { | ||
61 | TTM_REF_USAGE, | ||
62 | TTM_REF_SYNCCPU_READ, | ||
63 | TTM_REF_SYNCCPU_WRITE, | ||
64 | TTM_REF_NUM | ||
65 | }; | ||
66 | |||
67 | /** | ||
68 | * enum ttm_object_type | ||
69 | * | ||
70 | * One entry per ttm object type. | ||
71 | * Device-specific types should use the | ||
72 | * ttm_driver_typex types. | ||
73 | */ | ||
74 | |||
75 | enum ttm_object_type { | ||
76 | ttm_fence_type, | ||
77 | ttm_buffer_type, | ||
78 | ttm_lock_type, | ||
79 | ttm_driver_type0 = 256, | ||
80 | ttm_driver_type1 | ||
81 | }; | ||
82 | |||
83 | struct ttm_object_file; | ||
84 | struct ttm_object_device; | ||
85 | |||
86 | /** | ||
87 | * struct ttm_base_object | ||
88 | * | ||
89 | * @hash: hash entry for the per-device object hash. | ||
90 | * @type: derived type this object is base class for. | ||
91 | * @shareable: Other ttm_object_files can access this object. | ||
92 | * | ||
93 | * @tfile: Pointer to ttm_object_file of the creator. | ||
94 | * NULL if the object was not created by a user request. | ||
95 | * (kernel object). | ||
96 | * | ||
97 | * @refcount: Number of references to this object, not | ||
98 | * including the hash entry. A reference to a base object can | ||
99 | * only be held by a ref object. | ||
100 | * | ||
101 | * @refcount_release: A function to be called when there are | ||
102 | * no more references to this object. This function should | ||
103 | * destroy the object (or make sure destruction eventually happens), | ||
104 | * and when it is called, the object has | ||
105 | * already been taken out of the per-device hash. The parameter | ||
106 | * "base" should be set to NULL by the function. | ||
107 | * | ||
108 | * @ref_obj_release: A function to be called when a reference object | ||
109 | * with another ttm_ref_type than TTM_REF_USAGE is deleted. | ||
110 | * this function may, for example, release a lock held by a user-space | ||
111 | * process. | ||
112 | * | ||
113 | * This struct is intended to be used as a base struct for objects that | ||
114 | * are visible to user-space. It provides a global name, race-safe | ||
115 | * access and refcounting, minimal access contol and hooks for unref actions. | ||
116 | */ | ||
117 | |||
118 | struct ttm_base_object { | ||
119 | struct drm_hash_item hash; | ||
120 | enum ttm_object_type object_type; | ||
121 | bool shareable; | ||
122 | struct ttm_object_file *tfile; | ||
123 | struct kref refcount; | ||
124 | void (*refcount_release) (struct ttm_base_object **base); | ||
125 | void (*ref_obj_release) (struct ttm_base_object *base, | ||
126 | enum ttm_ref_type ref_type); | ||
127 | }; | ||
128 | |||
129 | /** | ||
130 | * ttm_base_object_init | ||
131 | * | ||
132 | * @tfile: Pointer to a struct ttm_object_file. | ||
133 | * @base: The struct ttm_base_object to initialize. | ||
134 | * @shareable: This object is shareable with other applcations. | ||
135 | * (different @tfile pointers.) | ||
136 | * @type: The object type. | ||
137 | * @refcount_release: See the struct ttm_base_object description. | ||
138 | * @ref_obj_release: See the struct ttm_base_object description. | ||
139 | * | ||
140 | * Initializes a struct ttm_base_object. | ||
141 | */ | ||
142 | |||
143 | extern int ttm_base_object_init(struct ttm_object_file *tfile, | ||
144 | struct ttm_base_object *base, | ||
145 | bool shareable, | ||
146 | enum ttm_object_type type, | ||
147 | void (*refcount_release) (struct ttm_base_object | ||
148 | **), | ||
149 | void (*ref_obj_release) (struct ttm_base_object | ||
150 | *, | ||
151 | enum ttm_ref_type | ||
152 | ref_type)); | ||
153 | |||
154 | /** | ||
155 | * ttm_base_object_lookup | ||
156 | * | ||
157 | * @tfile: Pointer to a struct ttm_object_file. | ||
158 | * @key: Hash key | ||
159 | * | ||
160 | * Looks up a struct ttm_base_object with the key @key. | ||
161 | * Also verifies that the object is visible to the application, by | ||
162 | * comparing the @tfile argument and checking the object shareable flag. | ||
163 | */ | ||
164 | |||
165 | extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file | ||
166 | *tfile, uint32_t key); | ||
167 | |||
168 | /** | ||
169 | * ttm_base_object_unref | ||
170 | * | ||
171 | * @p_base: Pointer to a pointer referncing a struct ttm_base_object. | ||
172 | * | ||
173 | * Decrements the base object refcount and clears the pointer pointed to by | ||
174 | * p_base. | ||
175 | */ | ||
176 | |||
177 | extern void ttm_base_object_unref(struct ttm_base_object **p_base); | ||
178 | |||
179 | /** | ||
180 | * ttm_ref_object_add. | ||
181 | * | ||
182 | * @tfile: A struct ttm_object_file representing the application owning the | ||
183 | * ref_object. | ||
184 | * @base: The base object to reference. | ||
185 | * @ref_type: The type of reference. | ||
186 | * @existed: Upon completion, indicates that an identical reference object | ||
187 | * already existed, and the refcount was upped on that object instead. | ||
188 | * | ||
189 | * Adding a ref object to a base object is basically like referencing the | ||
190 | * base object, but a user-space application holds the reference. When the | ||
191 | * file corresponding to @tfile is closed, all its reference objects are | ||
192 | * deleted. A reference object can have different types depending on what | ||
193 | * it's intended for. It can be refcounting to prevent object destruction, | ||
194 | * When user-space takes a lock, it can add a ref object to that lock to | ||
195 | * make sure the lock is released if the application dies. A ref object | ||
196 | * will hold a single reference on a base object. | ||
197 | */ | ||
198 | extern int ttm_ref_object_add(struct ttm_object_file *tfile, | ||
199 | struct ttm_base_object *base, | ||
200 | enum ttm_ref_type ref_type, bool *existed); | ||
201 | /** | ||
202 | * ttm_ref_object_base_unref | ||
203 | * | ||
204 | * @key: Key representing the base object. | ||
205 | * @ref_type: Ref type of the ref object to be dereferenced. | ||
206 | * | ||
207 | * Unreference a ref object with type @ref_type | ||
208 | * on the base object identified by @key. If there are no duplicate | ||
209 | * references, the ref object will be destroyed and the base object | ||
210 | * will be unreferenced. | ||
211 | */ | ||
212 | extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile, | ||
213 | unsigned long key, | ||
214 | enum ttm_ref_type ref_type); | ||
215 | |||
216 | /** | ||
217 | * ttm_object_file_init - initialize a struct ttm_object file | ||
218 | * | ||
219 | * @tdev: A struct ttm_object device this file is initialized on. | ||
220 | * @hash_order: Order of the hash table used to hold the reference objects. | ||
221 | * | ||
222 | * This is typically called by the file_ops::open function. | ||
223 | */ | ||
224 | |||
225 | extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device | ||
226 | *tdev, | ||
227 | unsigned int hash_order); | ||
228 | |||
229 | /** | ||
230 | * ttm_object_file_release - release data held by a ttm_object_file | ||
231 | * | ||
232 | * @p_tfile: Pointer to pointer to the ttm_object_file object to release. | ||
233 | * *p_tfile will be set to NULL by this function. | ||
234 | * | ||
235 | * Releases all data associated by a ttm_object_file. | ||
236 | * Typically called from file_ops::release. The caller must | ||
237 | * ensure that there are no concurrent users of tfile. | ||
238 | */ | ||
239 | |||
240 | extern void ttm_object_file_release(struct ttm_object_file **p_tfile); | ||
241 | |||
242 | /** | ||
243 | * ttm_object device init - initialize a struct ttm_object_device | ||
244 | * | ||
245 | * @hash_order: Order of hash table used to hash the base objects. | ||
246 | * | ||
247 | * This function is typically called on device initialization to prepare | ||
248 | * data structures needed for ttm base and ref objects. | ||
249 | */ | ||
250 | |||
251 | extern struct ttm_object_device *ttm_object_device_init | ||
252 | (struct ttm_mem_global *mem_glob, unsigned int hash_order); | ||
253 | |||
254 | /** | ||
255 | * ttm_object_device_release - release data held by a ttm_object_device | ||
256 | * | ||
257 | * @p_tdev: Pointer to pointer to the ttm_object_device object to release. | ||
258 | * *p_tdev will be set to NULL by this function. | ||
259 | * | ||
260 | * Releases all data associated by a ttm_object_device. | ||
261 | * Typically called from driver::unload before the destruction of the | ||
262 | * device private data structure. | ||
263 | */ | ||
264 | |||
265 | extern void ttm_object_device_release(struct ttm_object_device **p_tdev); | ||
266 | |||
267 | #endif | ||