1 /*        $NetBSD: drm_syncobj.h,v 1.2 2021/12/18 23:45:46 riastradh Exp $      */
2 
3 /*
4  * Copyright © 2017 Red Hat
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  *
25  * Authors:
26  *
27  */
28 #ifndef __DRM_SYNCOBJ_H__
29 #define __DRM_SYNCOBJ_H__
30 
31 #include <linux/dma-fence.h>
32 #include <linux/dma-fence-chain.h>
33 
34 struct drm_file;
35 
36 /**
37  * struct drm_syncobj - sync object.
38  *
39  * This structure defines a generic sync object which wraps a &dma_fence.
40  */
41 struct drm_syncobj {
42           /**
43            * @refcount: Reference count of this object.
44            */
45           struct kref refcount;
46           /**
47            * @fence:
48            * NULL or a pointer to the fence bound to this object.
49            *
50            * This field should not be used directly. Use drm_syncobj_fence_get()
51            * and drm_syncobj_replace_fence() instead.
52            */
53           struct dma_fence __rcu *fence;
54           /**
55            * @cb_list: List of callbacks to call when the &fence gets replaced.
56            */
57           struct list_head cb_list;
58           /**
59            * @lock: Protects &cb_list and write-locks &fence.
60            */
61           spinlock_t lock;
62           /**
63            * @file: A file backing for this syncobj.
64            */
65           struct file *file;
66 };
67 
68 void drm_syncobj_free(struct kref *kref);
69 
70 /**
71  * drm_syncobj_get - acquire a syncobj reference
72  * @obj: sync object
73  *
74  * This acquires an additional reference to @obj. It is illegal to call this
75  * without already holding a reference. No locks required.
76  */
77 static inline void
drm_syncobj_get(struct drm_syncobj * obj)78 drm_syncobj_get(struct drm_syncobj *obj)
79 {
80           kref_get(&obj->refcount);
81 }
82 
83 /**
84  * drm_syncobj_put - release a reference to a sync object.
85  * @obj: sync object.
86  */
87 static inline void
drm_syncobj_put(struct drm_syncobj * obj)88 drm_syncobj_put(struct drm_syncobj *obj)
89 {
90           kref_put(&obj->refcount, drm_syncobj_free);
91 }
92 
93 /**
94  * drm_syncobj_fence_get - get a reference to a fence in a sync object
95  * @syncobj: sync object.
96  *
97  * This acquires additional reference to &drm_syncobj.fence contained in @obj,
98  * if not NULL. It is illegal to call this without already holding a reference.
99  * No locks required.
100  *
101  * Returns:
102  * Either the fence of @obj or NULL if there's none.
103  */
104 static inline struct dma_fence *
drm_syncobj_fence_get(struct drm_syncobj * syncobj)105 drm_syncobj_fence_get(struct drm_syncobj *syncobj)
106 {
107           struct dma_fence *fence;
108 
109           rcu_read_lock();
110           fence = dma_fence_get_rcu_safe(&syncobj->fence);
111           rcu_read_unlock();
112 
113           return fence;
114 }
115 
116 struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
117                                              u32 handle);
118 void drm_syncobj_add_point(struct drm_syncobj *syncobj,
119                                  struct dma_fence_chain *chain,
120                                  struct dma_fence *fence,
121                                  uint64_t point);
122 void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
123                                      struct dma_fence *fence);
124 int drm_syncobj_find_fence(struct drm_file *file_private,
125                                  u32 handle, u64 point, u64 flags,
126                                  struct dma_fence **fence);
127 void drm_syncobj_free(struct kref *kref);
128 int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
129                            struct dma_fence *fence);
130 int drm_syncobj_get_handle(struct drm_file *file_private,
131                                  struct drm_syncobj *syncobj, u32 *handle);
132 int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
133 
134 #endif
135