xref: /linux-6.15/include/drm/drm_syncobj.h (revision dd7ece7f)
1 /*
2  * Copyright © 2017 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *
25  */
26 #ifndef __DRM_SYNCOBJ_H__
27 #define __DRM_SYNCOBJ_H__
28 
29 #include <linux/dma-fence.h>
30 
31 struct drm_file;
32 
33 struct drm_syncobj_cb;
34 
35 enum drm_syncobj_type {
36 	DRM_SYNCOBJ_TYPE_BINARY,
37 	DRM_SYNCOBJ_TYPE_TIMELINE
38 };
39 
40 /**
41  * struct drm_syncobj - sync object.
42  *
43  * This structure defines a generic sync object which is timeline based.
44  */
45 struct drm_syncobj {
46 	/**
47 	 * @refcount: Reference count of this object.
48 	 */
49 	struct kref refcount;
50 	/**
51 	 * @type: indicate syncobj type
52 	 */
53 	enum drm_syncobj_type type;
54 	/**
55 	 * @wq: wait signal operation work queue
56 	 */
57 	wait_queue_head_t	wq;
58 	/**
59 	 * @timeline_context: fence context used by timeline
60 	 */
61 	u64 timeline_context;
62 	/**
63 	 * @timeline: syncobj timeline value, which indicates point is signaled.
64 	 */
65 	u64 timeline;
66 	/**
67 	 * @signal_point: which indicates the latest signaler point.
68 	 */
69 	u64 signal_point;
70 	/**
71 	 * @signal_pt_list: signaler point list.
72 	 */
73 	struct list_head signal_pt_list;
74 
75 	/**
76          * @cb_list: List of callbacks to call when the &fence gets replaced.
77          */
78 	struct list_head cb_list;
79 	/**
80 	 * @pt_lock: Protects pt list.
81 	 */
82 	spinlock_t pt_lock;
83 	/**
84 	 * @cb_mutex: Protects syncobj cb list.
85 	 */
86 	struct mutex cb_mutex;
87 	/**
88 	 * @file: A file backing for this syncobj.
89 	 */
90 	struct file *file;
91 };
92 
93 typedef void (*drm_syncobj_func_t)(struct drm_syncobj *syncobj,
94 				   struct drm_syncobj_cb *cb);
95 
96 /**
97  * struct drm_syncobj_cb - callback for drm_syncobj_add_callback
98  * @node: used by drm_syncob_add_callback to append this struct to
99  *       &drm_syncobj.cb_list
100  * @func: drm_syncobj_func_t to call
101  *
102  * This struct will be initialized by drm_syncobj_add_callback, additional
103  * data can be passed along by embedding drm_syncobj_cb in another struct.
104  * The callback will get called the next time drm_syncobj_replace_fence is
105  * called.
106  */
107 struct drm_syncobj_cb {
108 	struct list_head node;
109 	drm_syncobj_func_t func;
110 };
111 
112 void drm_syncobj_free(struct kref *kref);
113 
114 /**
115  * drm_syncobj_get - acquire a syncobj reference
116  * @obj: sync object
117  *
118  * This acquires an additional reference to @obj. It is illegal to call this
119  * without already holding a reference. No locks required.
120  */
121 static inline void
122 drm_syncobj_get(struct drm_syncobj *obj)
123 {
124 	kref_get(&obj->refcount);
125 }
126 
127 /**
128  * drm_syncobj_put - release a reference to a sync object.
129  * @obj: sync object.
130  */
131 static inline void
132 drm_syncobj_put(struct drm_syncobj *obj)
133 {
134 	kref_put(&obj->refcount, drm_syncobj_free);
135 }
136 
137 struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
138 				     u32 handle);
139 void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, u64 point,
140 			       struct dma_fence *fence);
141 int drm_syncobj_find_fence(struct drm_file *file_private,
142 			   u32 handle, u64 point, u64 flags,
143 			   struct dma_fence **fence);
144 void drm_syncobj_free(struct kref *kref);
145 int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
146 		       struct dma_fence *fence);
147 int drm_syncobj_get_handle(struct drm_file *file_private,
148 			   struct drm_syncobj *syncobj, u32 *handle);
149 int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
150 int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
151 			     struct dma_fence **fence);
152 
153 #endif
154