xref: /linux-6.15/include/drm/drm_syncobj.h (revision 43cf1fc0)
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_syncobj_cb;
32 
33 enum drm_syncobj_type {
34 	DRM_SYNCOBJ_TYPE_BINARY,
35 	DRM_SYNCOBJ_TYPE_TIMELINE
36 };
37 
38 /**
39  * struct drm_syncobj - sync object.
40  *
41  * This structure defines a generic sync object which is timeline based.
42  */
43 struct drm_syncobj {
44 	/**
45 	 * @refcount: Reference count of this object.
46 	 */
47 	struct kref refcount;
48 	/**
49 	 * @type: indicate syncobj type
50 	 */
51 	enum drm_syncobj_type type;
52 	/**
53 	 * @wq: wait signal operation work queue
54 	 */
55 	wait_queue_head_t	wq;
56 	/**
57 	 * @timeline_context: fence context used by timeline
58 	 */
59 	u64 timeline_context;
60 	/**
61 	 * @timeline: syncobj timeline value, which indicates point is signaled.
62 	 */
63 	u64 timeline;
64 	/**
65 	 * @signal_point: which indicates the latest signaler point.
66 	 */
67 	u64 signal_point;
68 	/**
69 	 * @signal_pt_list: signaler point list.
70 	 */
71 	struct list_head signal_pt_list;
72 
73 	/**
74          * @cb_list: List of callbacks to call when the &fence gets replaced.
75          */
76 	struct list_head cb_list;
77 	/**
78 	 * @pt_lock: Protects pt list.
79 	 */
80 	spinlock_t pt_lock;
81 	/**
82 	 * @cb_mutex: Protects syncobj cb list.
83 	 */
84 	struct mutex cb_mutex;
85 	/**
86 	 * @file: A file backing for this syncobj.
87 	 */
88 	struct file *file;
89 };
90 
91 typedef void (*drm_syncobj_func_t)(struct drm_syncobj *syncobj,
92 				   struct drm_syncobj_cb *cb);
93 
94 /**
95  * struct drm_syncobj_cb - callback for drm_syncobj_add_callback
96  * @node: used by drm_syncob_add_callback to append this struct to
97  *       &drm_syncobj.cb_list
98  * @func: drm_syncobj_func_t to call
99  *
100  * This struct will be initialized by drm_syncobj_add_callback, additional
101  * data can be passed along by embedding drm_syncobj_cb in another struct.
102  * The callback will get called the next time drm_syncobj_replace_fence is
103  * called.
104  */
105 struct drm_syncobj_cb {
106 	struct list_head node;
107 	drm_syncobj_func_t func;
108 };
109 
110 void drm_syncobj_free(struct kref *kref);
111 
112 /**
113  * drm_syncobj_get - acquire a syncobj reference
114  * @obj: sync object
115  *
116  * This acquires an additional reference to @obj. It is illegal to call this
117  * without already holding a reference. No locks required.
118  */
119 static inline void
120 drm_syncobj_get(struct drm_syncobj *obj)
121 {
122 	kref_get(&obj->refcount);
123 }
124 
125 /**
126  * drm_syncobj_put - release a reference to a sync object.
127  * @obj: sync object.
128  */
129 static inline void
130 drm_syncobj_put(struct drm_syncobj *obj)
131 {
132 	kref_put(&obj->refcount, drm_syncobj_free);
133 }
134 
135 struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
136 				     u32 handle);
137 void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, u64 point,
138 			       struct dma_fence *fence);
139 int drm_syncobj_find_fence(struct drm_file *file_private,
140 			   u32 handle, u64 point, u64 flags,
141 			   struct dma_fence **fence);
142 void drm_syncobj_free(struct kref *kref);
143 int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
144 		       struct dma_fence *fence);
145 int drm_syncobj_get_handle(struct drm_file *file_private,
146 			   struct drm_syncobj *syncobj, u32 *handle);
147 int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
148 int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
149 			     struct dma_fence **fence);
150 
151 #endif
152