blob: 37079859afc86e6cef120cd4d8a924388c3f6e3b [file] [log] [blame]
Thomas Hellstrom88071532009-12-06 21:46:24 +01001/**************************************************************************
2 *
Thomas Hellstrom65981f72013-11-13 01:48:31 -08003 * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
Thomas Hellstrom88071532009-12-06 21:46:24 +01004 * 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>
Thomas Hellstrom65981f72013-11-13 01:48:31 -080029 *
30 * While no substantial code is shared, the prime code is inspired by
31 * drm_prime.c, with
32 * Authors:
33 * Dave Airlie <airlied@redhat.com>
34 * Rob Clark <rob.clark@linaro.org>
Thomas Hellstrom88071532009-12-06 21:46:24 +010035 */
36/** @file ttm_ref_object.c
37 *
38 * Base- and reference object implementation for the various
39 * ttm objects. Implements reference counting, minimal security checks
40 * and release on file close.
41 */
42
Thomas Hellstrom65981f72013-11-13 01:48:31 -080043
Thomas Hellstrom88071532009-12-06 21:46:24 +010044/**
45 * struct ttm_object_file
46 *
47 * @tdev: Pointer to the ttm_object_device.
48 *
49 * @lock: Lock that protects the ref_list list and the
50 * ref_hash hash tables.
51 *
52 * @ref_list: List of ttm_ref_objects to be destroyed at
53 * file release.
54 *
55 * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
56 * for fast lookup of ref objects given a base object.
57 */
58
Joe Perches25d04792012-03-16 21:43:50 -070059#define pr_fmt(fmt) "[TTM] " fmt
60
David Howells760285e2012-10-02 18:01:07 +010061#include <drm/ttm/ttm_object.h>
62#include <drm/ttm/ttm_module.h>
Thomas Hellstrom88071532009-12-06 21:46:24 +010063#include <linux/list.h>
64#include <linux/spinlock.h>
65#include <linux/slab.h>
66#include <linux/module.h>
Arun Sharma600634972011-07-26 16:09:06 -070067#include <linux/atomic.h>
Thomas Hellstrom88071532009-12-06 21:46:24 +010068
69struct ttm_object_file {
70 struct ttm_object_device *tdev;
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +010071 spinlock_t lock;
Thomas Hellstrom88071532009-12-06 21:46:24 +010072 struct list_head ref_list;
73 struct drm_open_hash ref_hash[TTM_REF_NUM];
74 struct kref refcount;
75};
76
77/**
78 * struct ttm_object_device
79 *
80 * @object_lock: lock that protects the object_hash hash table.
81 *
82 * @object_hash: hash table for fast lookup of object global names.
83 *
84 * @object_count: Per device object count.
85 *
86 * This is the per-device data structure needed for ttm object management.
87 */
88
89struct ttm_object_device {
Thomas Hellstromcdad0522012-11-06 11:31:50 +000090 spinlock_t object_lock;
Thomas Hellstrom88071532009-12-06 21:46:24 +010091 struct drm_open_hash object_hash;
92 atomic_t object_count;
93 struct ttm_mem_global *mem_glob;
Thomas Hellstrom65981f72013-11-13 01:48:31 -080094 struct dma_buf_ops ops;
95 void (*dmabuf_release)(struct dma_buf *dma_buf);
96 size_t dma_buf_size;
Thomas Hellstrom88071532009-12-06 21:46:24 +010097};
98
99/**
100 * struct ttm_ref_object
101 *
102 * @hash: Hash entry for the per-file object reference hash.
103 *
104 * @head: List entry for the per-file list of ref-objects.
105 *
106 * @kref: Ref count.
107 *
108 * @obj: Base object this ref object is referencing.
109 *
110 * @ref_type: Type of ref object.
111 *
112 * This is similar to an idr object, but it also has a hash table entry
113 * that allows lookup with a pointer to the referenced object as a key. In
114 * that way, one can easily detect whether a base object is referenced by
115 * a particular ttm_object_file. It also carries a ref count to avoid creating
116 * multiple ref objects if a ttm_object_file references the same base
117 * object more than once.
118 */
119
120struct ttm_ref_object {
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100121 struct rcu_head rcu_head;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100122 struct drm_hash_item hash;
123 struct list_head head;
124 struct kref kref;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100125 enum ttm_ref_type ref_type;
Richard Kennedydd5fde62010-01-26 17:10:48 +0000126 struct ttm_base_object *obj;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100127 struct ttm_object_file *tfile;
128};
129
Thomas Hellstrom65981f72013-11-13 01:48:31 -0800130static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
131
Thomas Hellstrom88071532009-12-06 21:46:24 +0100132static inline struct ttm_object_file *
133ttm_object_file_ref(struct ttm_object_file *tfile)
134{
135 kref_get(&tfile->refcount);
136 return tfile;
137}
138
139static void ttm_object_file_destroy(struct kref *kref)
140{
141 struct ttm_object_file *tfile =
142 container_of(kref, struct ttm_object_file, refcount);
143
144 kfree(tfile);
145}
146
147
148static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
149{
150 struct ttm_object_file *tfile = *p_tfile;
151
152 *p_tfile = NULL;
153 kref_put(&tfile->refcount, ttm_object_file_destroy);
154}
155
156
157int ttm_base_object_init(struct ttm_object_file *tfile,
158 struct ttm_base_object *base,
159 bool shareable,
160 enum ttm_object_type object_type,
161 void (*refcount_release) (struct ttm_base_object **),
162 void (*ref_obj_release) (struct ttm_base_object *,
163 enum ttm_ref_type ref_type))
164{
165 struct ttm_object_device *tdev = tfile->tdev;
166 int ret;
167
168 base->shareable = shareable;
169 base->tfile = ttm_object_file_ref(tfile);
170 base->refcount_release = refcount_release;
171 base->ref_obj_release = ref_obj_release;
172 base->object_type = object_type;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100173 kref_init(&base->refcount);
Thomas Hellstrom52939082012-11-20 12:16:51 +0000174 spin_lock(&tdev->object_lock);
175 ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
176 &base->hash,
177 (unsigned long)base, 31, 0, 0);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000178 spin_unlock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100179 if (unlikely(ret != 0))
180 goto out_err0;
181
182 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
183 if (unlikely(ret != 0))
184 goto out_err1;
185
186 ttm_base_object_unref(&base);
187
188 return 0;
189out_err1:
Thomas Hellstromae8df2a2012-11-20 12:16:50 +0000190 spin_lock(&tdev->object_lock);
Thomas Hellstrom52939082012-11-20 12:16:51 +0000191 (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
Thomas Hellstromae8df2a2012-11-20 12:16:50 +0000192 spin_unlock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100193out_err0:
194 return ret;
195}
196EXPORT_SYMBOL(ttm_base_object_init);
197
198static void ttm_release_base(struct kref *kref)
199{
200 struct ttm_base_object *base =
201 container_of(kref, struct ttm_base_object, refcount);
202 struct ttm_object_device *tdev = base->tfile->tdev;
203
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000204 spin_lock(&tdev->object_lock);
Thomas Hellstrom52939082012-11-20 12:16:51 +0000205 (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000206 spin_unlock(&tdev->object_lock);
Thomas Hellstrom52939082012-11-20 12:16:51 +0000207
208 /*
209 * Note: We don't use synchronize_rcu() here because it's far
210 * too slow. It's up to the user to free the object using
211 * call_rcu() or ttm_base_object_kfree().
212 */
213
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100214 ttm_object_file_unref(&base->tfile);
215 if (base->refcount_release)
Thomas Hellstrom88071532009-12-06 21:46:24 +0100216 base->refcount_release(&base);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100217}
218
219void ttm_base_object_unref(struct ttm_base_object **p_base)
220{
221 struct ttm_base_object *base = *p_base;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100222
223 *p_base = NULL;
224
Paul Bollecdb650a2011-02-27 01:34:08 +0100225 kref_put(&base->refcount, ttm_release_base);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100226}
227EXPORT_SYMBOL(ttm_base_object_unref);
228
229struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
230 uint32_t key)
231{
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100232 struct ttm_base_object *base = NULL;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100233 struct drm_hash_item *hash;
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100234 struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
Thomas Hellstrom88071532009-12-06 21:46:24 +0100235 int ret;
236
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000237 rcu_read_lock();
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100238 ret = drm_ht_find_item_rcu(ht, key, &hash);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100239
240 if (likely(ret == 0)) {
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100241 base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
242 if (!kref_get_unless_zero(&base->refcount))
243 base = NULL;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100244 }
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000245 rcu_read_unlock();
Thomas Hellstrom88071532009-12-06 21:46:24 +0100246
Thomas Hellstrom88071532009-12-06 21:46:24 +0100247 return base;
248}
249EXPORT_SYMBOL(ttm_base_object_lookup);
250
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100251struct ttm_base_object *
252ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
253{
254 struct ttm_base_object *base = NULL;
255 struct drm_hash_item *hash;
256 struct drm_open_hash *ht = &tdev->object_hash;
257 int ret;
258
259 rcu_read_lock();
260 ret = drm_ht_find_item_rcu(ht, key, &hash);
261
262 if (likely(ret == 0)) {
263 base = drm_hash_entry(hash, struct ttm_base_object, hash);
264 if (!kref_get_unless_zero(&base->refcount))
265 base = NULL;
266 }
267 rcu_read_unlock();
268
269 return base;
270}
271EXPORT_SYMBOL(ttm_base_object_lookup_for_ref);
272
Thomas Hellstrom88071532009-12-06 21:46:24 +0100273int ttm_ref_object_add(struct ttm_object_file *tfile,
274 struct ttm_base_object *base,
275 enum ttm_ref_type ref_type, bool *existed)
276{
277 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
278 struct ttm_ref_object *ref;
279 struct drm_hash_item *hash;
280 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
281 int ret = -EINVAL;
282
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100283 if (base->tfile != tfile && !base->shareable)
284 return -EPERM;
285
Thomas Hellstrom88071532009-12-06 21:46:24 +0100286 if (existed != NULL)
287 *existed = true;
288
289 while (ret == -EINVAL) {
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100290 rcu_read_lock();
291 ret = drm_ht_find_item_rcu(ht, base->hash.key, &hash);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100292
293 if (ret == 0) {
294 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100295 if (!kref_get_unless_zero(&ref->kref)) {
296 rcu_read_unlock();
297 break;
298 }
Thomas Hellstrom88071532009-12-06 21:46:24 +0100299 }
300
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100301 rcu_read_unlock();
Thomas Hellstrom88071532009-12-06 21:46:24 +0100302 ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
303 false, false);
304 if (unlikely(ret != 0))
305 return ret;
306 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
307 if (unlikely(ref == NULL)) {
308 ttm_mem_global_free(mem_glob, sizeof(*ref));
309 return -ENOMEM;
310 }
311
312 ref->hash.key = base->hash.key;
313 ref->obj = base;
314 ref->tfile = tfile;
315 ref->ref_type = ref_type;
316 kref_init(&ref->kref);
317
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100318 spin_lock(&tfile->lock);
319 ret = drm_ht_insert_item_rcu(ht, &ref->hash);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100320
321 if (likely(ret == 0)) {
322 list_add_tail(&ref->head, &tfile->ref_list);
323 kref_get(&base->refcount);
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100324 spin_unlock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100325 if (existed != NULL)
326 *existed = false;
327 break;
328 }
329
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100330 spin_unlock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100331 BUG_ON(ret != -EINVAL);
332
333 ttm_mem_global_free(mem_glob, sizeof(*ref));
334 kfree(ref);
335 }
336
337 return ret;
338}
339EXPORT_SYMBOL(ttm_ref_object_add);
340
341static void ttm_ref_object_release(struct kref *kref)
342{
343 struct ttm_ref_object *ref =
344 container_of(kref, struct ttm_ref_object, kref);
345 struct ttm_base_object *base = ref->obj;
346 struct ttm_object_file *tfile = ref->tfile;
347 struct drm_open_hash *ht;
348 struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
349
350 ht = &tfile->ref_hash[ref->ref_type];
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100351 (void)drm_ht_remove_item_rcu(ht, &ref->hash);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100352 list_del(&ref->head);
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100353 spin_unlock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100354
355 if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
356 base->ref_obj_release(base, ref->ref_type);
357
358 ttm_base_object_unref(&ref->obj);
359 ttm_mem_global_free(mem_glob, sizeof(*ref));
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100360 kfree_rcu(ref, rcu_head);
361 spin_lock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100362}
363
364int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
365 unsigned long key, enum ttm_ref_type ref_type)
366{
367 struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
368 struct ttm_ref_object *ref;
369 struct drm_hash_item *hash;
370 int ret;
371
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100372 spin_lock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100373 ret = drm_ht_find_item(ht, key, &hash);
374 if (unlikely(ret != 0)) {
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100375 spin_unlock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100376 return -EINVAL;
377 }
378 ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
379 kref_put(&ref->kref, ttm_ref_object_release);
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100380 spin_unlock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100381 return 0;
382}
383EXPORT_SYMBOL(ttm_ref_object_base_unref);
384
385void ttm_object_file_release(struct ttm_object_file **p_tfile)
386{
387 struct ttm_ref_object *ref;
388 struct list_head *list;
389 unsigned int i;
390 struct ttm_object_file *tfile = *p_tfile;
391
392 *p_tfile = NULL;
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100393 spin_lock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100394
395 /*
396 * Since we release the lock within the loop, we have to
397 * restart it from the beginning each time.
398 */
399
400 while (!list_empty(&tfile->ref_list)) {
401 list = tfile->ref_list.next;
402 ref = list_entry(list, struct ttm_ref_object, head);
403 ttm_ref_object_release(&ref->kref);
404 }
405
406 for (i = 0; i < TTM_REF_NUM; ++i)
407 drm_ht_remove(&tfile->ref_hash[i]);
408
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100409 spin_unlock(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100410 ttm_object_file_unref(&tfile);
411}
412EXPORT_SYMBOL(ttm_object_file_release);
413
414struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
415 unsigned int hash_order)
416{
417 struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
418 unsigned int i;
419 unsigned int j = 0;
420 int ret;
421
422 if (unlikely(tfile == NULL))
423 return NULL;
424
Thomas Hellstrom05efb1a2013-12-18 14:13:29 +0100425 spin_lock_init(&tfile->lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100426 tfile->tdev = tdev;
427 kref_init(&tfile->refcount);
428 INIT_LIST_HEAD(&tfile->ref_list);
429
430 for (i = 0; i < TTM_REF_NUM; ++i) {
431 ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
432 if (ret) {
433 j = i;
434 goto out_err;
435 }
436 }
437
438 return tfile;
439out_err:
440 for (i = 0; i < j; ++i)
441 drm_ht_remove(&tfile->ref_hash[i]);
442
443 kfree(tfile);
444
445 return NULL;
446}
447EXPORT_SYMBOL(ttm_object_file_init);
448
Thomas Hellstrom65981f72013-11-13 01:48:31 -0800449struct ttm_object_device *
450ttm_object_device_init(struct ttm_mem_global *mem_glob,
451 unsigned int hash_order,
452 const struct dma_buf_ops *ops)
Thomas Hellstrom88071532009-12-06 21:46:24 +0100453{
454 struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
455 int ret;
456
457 if (unlikely(tdev == NULL))
458 return NULL;
459
460 tdev->mem_glob = mem_glob;
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000461 spin_lock_init(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100462 atomic_set(&tdev->object_count, 0);
463 ret = drm_ht_create(&tdev->object_hash, hash_order);
Thomas Hellstrom65981f72013-11-13 01:48:31 -0800464 if (ret != 0)
465 goto out_no_object_hash;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100466
Thomas Hellstrom65981f72013-11-13 01:48:31 -0800467 tdev->ops = *ops;
468 tdev->dmabuf_release = tdev->ops.release;
469 tdev->ops.release = ttm_prime_dmabuf_release;
470 tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
471 ttm_round_pot(sizeof(struct file));
472 return tdev;
Thomas Hellstrom88071532009-12-06 21:46:24 +0100473
Thomas Hellstrom65981f72013-11-13 01:48:31 -0800474out_no_object_hash:
Thomas Hellstrom88071532009-12-06 21:46:24 +0100475 kfree(tdev);
476 return NULL;
477}
478EXPORT_SYMBOL(ttm_object_device_init);
479
480void ttm_object_device_release(struct ttm_object_device **p_tdev)
481{
482 struct ttm_object_device *tdev = *p_tdev;
483
484 *p_tdev = NULL;
485
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000486 spin_lock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100487 drm_ht_remove(&tdev->object_hash);
Thomas Hellstromcdad0522012-11-06 11:31:50 +0000488 spin_unlock(&tdev->object_lock);
Thomas Hellstrom88071532009-12-06 21:46:24 +0100489
490 kfree(tdev);
491}
492EXPORT_SYMBOL(ttm_object_device_release);
Thomas Hellstrom65981f72013-11-13 01:48:31 -0800493
494/**
495 * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
496 *
497 * @dma_buf: Non-refcounted pointer to a struct dma-buf.
498 *
499 * Obtain a file reference from a lookup structure that doesn't refcount
500 * the file, but synchronizes with its release method to make sure it has
501 * not been freed yet. See for example kref_get_unless_zero documentation.
502 * Returns true if refcounting succeeds, false otherwise.
503 *
504 * Nobody really wants this as a public API yet, so let it mature here
505 * for some time...
506 */
507static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
508{
509 return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
510}
511
512/**
513 * ttm_prime_refcount_release - refcount release method for a prime object.
514 *
515 * @p_base: Pointer to ttm_base_object pointer.
516 *
517 * This is a wrapper that calls the refcount_release founction of the
518 * underlying object. At the same time it cleans up the prime object.
519 * This function is called when all references to the base object we
520 * derive from are gone.
521 */
522static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
523{
524 struct ttm_base_object *base = *p_base;
525 struct ttm_prime_object *prime;
526
527 *p_base = NULL;
528 prime = container_of(base, struct ttm_prime_object, base);
529 BUG_ON(prime->dma_buf != NULL);
530 mutex_destroy(&prime->mutex);
531 if (prime->refcount_release)
532 prime->refcount_release(&base);
533}
534
535/**
536 * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
537 *
538 * @dma_buf:
539 *
540 * This function first calls the dma_buf release method the driver
541 * provides. Then it cleans up our dma_buf pointer used for lookup,
542 * and finally releases the reference the dma_buf has on our base
543 * object.
544 */
545static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
546{
547 struct ttm_prime_object *prime =
548 (struct ttm_prime_object *) dma_buf->priv;
549 struct ttm_base_object *base = &prime->base;
550 struct ttm_object_device *tdev = base->tfile->tdev;
551
552 if (tdev->dmabuf_release)
553 tdev->dmabuf_release(dma_buf);
554 mutex_lock(&prime->mutex);
555 if (prime->dma_buf == dma_buf)
556 prime->dma_buf = NULL;
557 mutex_unlock(&prime->mutex);
558 ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
559 ttm_base_object_unref(&base);
560}
561
562/**
563 * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
564 *
565 * @tfile: A struct ttm_object_file identifying the caller.
566 * @fd: The prime / dmabuf fd.
567 * @handle: The returned handle.
568 *
569 * This function returns a handle to an object that previously exported
570 * a dma-buf. Note that we don't handle imports yet, because we simply
571 * have no consumers of that implementation.
572 */
573int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
574 int fd, u32 *handle)
575{
576 struct ttm_object_device *tdev = tfile->tdev;
577 struct dma_buf *dma_buf;
578 struct ttm_prime_object *prime;
579 struct ttm_base_object *base;
580 int ret;
581
582 dma_buf = dma_buf_get(fd);
583 if (IS_ERR(dma_buf))
584 return PTR_ERR(dma_buf);
585
586 if (dma_buf->ops != &tdev->ops)
587 return -ENOSYS;
588
589 prime = (struct ttm_prime_object *) dma_buf->priv;
590 base = &prime->base;
591 *handle = base->hash.key;
592 ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL);
593
594 dma_buf_put(dma_buf);
595
596 return ret;
597}
598EXPORT_SYMBOL_GPL(ttm_prime_fd_to_handle);
599
600/**
601 * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
602 *
603 * @tfile: Struct ttm_object_file identifying the caller.
604 * @handle: Handle to the object we're exporting from.
605 * @flags: flags for dma-buf creation. We just pass them on.
606 * @prime_fd: The returned file descriptor.
607 *
608 */
609int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
610 uint32_t handle, uint32_t flags,
611 int *prime_fd)
612{
613 struct ttm_object_device *tdev = tfile->tdev;
614 struct ttm_base_object *base;
615 struct dma_buf *dma_buf;
616 struct ttm_prime_object *prime;
617 int ret;
618
619 base = ttm_base_object_lookup(tfile, handle);
620 if (unlikely(base == NULL ||
621 base->object_type != ttm_prime_type)) {
622 ret = -ENOENT;
623 goto out_unref;
624 }
625
626 prime = container_of(base, struct ttm_prime_object, base);
627 if (unlikely(!base->shareable)) {
628 ret = -EPERM;
629 goto out_unref;
630 }
631
632 ret = mutex_lock_interruptible(&prime->mutex);
633 if (unlikely(ret != 0)) {
634 ret = -ERESTARTSYS;
635 goto out_unref;
636 }
637
638 dma_buf = prime->dma_buf;
639 if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
640
641 /*
642 * Need to create a new dma_buf, with memory accounting.
643 */
644 ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
645 false, true);
646 if (unlikely(ret != 0)) {
647 mutex_unlock(&prime->mutex);
648 goto out_unref;
649 }
650
651 dma_buf = dma_buf_export(prime, &tdev->ops,
652 prime->size, flags);
653 if (IS_ERR(dma_buf)) {
654 ret = PTR_ERR(dma_buf);
655 ttm_mem_global_free(tdev->mem_glob,
656 tdev->dma_buf_size);
657 mutex_unlock(&prime->mutex);
658 goto out_unref;
659 }
660
661 /*
662 * dma_buf has taken the base object reference
663 */
664 base = NULL;
665 prime->dma_buf = dma_buf;
666 }
667 mutex_unlock(&prime->mutex);
668
669 ret = dma_buf_fd(dma_buf, flags);
670 if (ret >= 0) {
671 *prime_fd = ret;
672 ret = 0;
673 } else
674 dma_buf_put(dma_buf);
675
676out_unref:
677 if (base)
678 ttm_base_object_unref(&base);
679 return ret;
680}
681EXPORT_SYMBOL_GPL(ttm_prime_handle_to_fd);
682
683/**
684 * ttm_prime_object_init - Initialize a ttm_prime_object
685 *
686 * @tfile: struct ttm_object_file identifying the caller
687 * @size: The size of the dma_bufs we export.
688 * @prime: The object to be initialized.
689 * @shareable: See ttm_base_object_init
690 * @type: See ttm_base_object_init
691 * @refcount_release: See ttm_base_object_init
692 * @ref_obj_release: See ttm_base_object_init
693 *
694 * Initializes an object which is compatible with the drm_prime model
695 * for data sharing between processes and devices.
696 */
697int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
698 struct ttm_prime_object *prime, bool shareable,
699 enum ttm_object_type type,
700 void (*refcount_release) (struct ttm_base_object **),
701 void (*ref_obj_release) (struct ttm_base_object *,
702 enum ttm_ref_type ref_type))
703{
704 mutex_init(&prime->mutex);
705 prime->size = PAGE_ALIGN(size);
706 prime->real_type = type;
707 prime->dma_buf = NULL;
708 prime->refcount_release = refcount_release;
709 return ttm_base_object_init(tfile, &prime->base, shareable,
710 ttm_prime_type,
711 ttm_prime_refcount_release,
712 ref_obj_release);
713}
714EXPORT_SYMBOL(ttm_prime_object_init);