xref: /linux-6.15/include/drm/drm_syncobj.h (revision 9c19fb10)
1e9083420SDave Airlie /*
2e9083420SDave Airlie  * Copyright © 2017 Red Hat
3e9083420SDave Airlie  *
4e9083420SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
5e9083420SDave Airlie  * copy of this software and associated documentation files (the "Software"),
6e9083420SDave Airlie  * to deal in the Software without restriction, including without limitation
7e9083420SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e9083420SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
9e9083420SDave Airlie  * Software is furnished to do so, subject to the following conditions:
10e9083420SDave Airlie  *
11e9083420SDave Airlie  * The above copyright notice and this permission notice (including the next
12e9083420SDave Airlie  * paragraph) shall be included in all copies or substantial portions of the
13e9083420SDave Airlie  * Software.
14e9083420SDave Airlie  *
15e9083420SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e9083420SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e9083420SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e9083420SDave Airlie  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e9083420SDave Airlie  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e9083420SDave Airlie  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e9083420SDave Airlie  * IN THE SOFTWARE.
22e9083420SDave Airlie  *
23e9083420SDave Airlie  * Authors:
24e9083420SDave Airlie  *
25e9083420SDave Airlie  */
26e9083420SDave Airlie #ifndef __DRM_SYNCOBJ_H__
27e9083420SDave Airlie #define __DRM_SYNCOBJ_H__
28e9083420SDave Airlie 
29e9083420SDave Airlie #include "linux/dma-fence.h"
30e9083420SDave Airlie 
31*9c19fb10SJason Ekstrand struct drm_syncobj_cb;
32*9c19fb10SJason Ekstrand 
33e9083420SDave Airlie /**
34e9083420SDave Airlie  * struct drm_syncobj - sync object.
35e9083420SDave Airlie  *
36e9083420SDave Airlie  * This structure defines a generic sync object which wraps a dma fence.
37e9083420SDave Airlie  */
38e9083420SDave Airlie struct drm_syncobj {
39e9083420SDave Airlie 	/**
40e9083420SDave Airlie 	 * @refcount:
41e9083420SDave Airlie 	 *
42e9083420SDave Airlie 	 * Reference count of this object.
43e9083420SDave Airlie 	 */
44e9083420SDave Airlie 	struct kref refcount;
45e9083420SDave Airlie 	/**
46e9083420SDave Airlie 	 * @fence:
47e9083420SDave Airlie 	 * NULL or a pointer to the fence bound to this object.
48*9c19fb10SJason Ekstrand 	 *
49*9c19fb10SJason Ekstrand 	 * This field should not be used directly.  Use drm_syncobj_fence_get
50*9c19fb10SJason Ekstrand 	 * and drm_syncobj_replace_fence instead.
51e9083420SDave Airlie 	 */
52e9083420SDave Airlie 	struct dma_fence *fence;
53e9083420SDave Airlie 	/**
54*9c19fb10SJason Ekstrand 	 * @cb_list:
55*9c19fb10SJason Ekstrand 	 * List of callbacks to call when the fence gets replaced
56*9c19fb10SJason Ekstrand 	 */
57*9c19fb10SJason Ekstrand 	struct list_head cb_list;
58*9c19fb10SJason Ekstrand 	/**
59*9c19fb10SJason Ekstrand 	 * @lock:
60*9c19fb10SJason Ekstrand 	 * locks cb_list and write-locks fence.
61*9c19fb10SJason Ekstrand 	 */
62*9c19fb10SJason Ekstrand 	spinlock_t lock;
63*9c19fb10SJason Ekstrand 	/**
64e9083420SDave Airlie 	 * @file:
65e9083420SDave Airlie 	 * a file backing for this syncobj.
66e9083420SDave Airlie 	 */
67e9083420SDave Airlie 	struct file *file;
68e9083420SDave Airlie };
69e9083420SDave Airlie 
70*9c19fb10SJason Ekstrand typedef void (*drm_syncobj_func_t)(struct drm_syncobj *syncobj,
71*9c19fb10SJason Ekstrand 				   struct drm_syncobj_cb *cb);
72*9c19fb10SJason Ekstrand 
73*9c19fb10SJason Ekstrand /**
74*9c19fb10SJason Ekstrand  * struct drm_syncobj_cb - callback for drm_syncobj_add_callback
75*9c19fb10SJason Ekstrand  * @node: used by drm_syncob_add_callback to append this struct to
76*9c19fb10SJason Ekstrand  *	  syncobj::cb_list
77*9c19fb10SJason Ekstrand  * @func: drm_syncobj_func_t to call
78*9c19fb10SJason Ekstrand  *
79*9c19fb10SJason Ekstrand  * This struct will be initialized by drm_syncobj_add_callback, additional
80*9c19fb10SJason Ekstrand  * data can be passed along by embedding drm_syncobj_cb in another struct.
81*9c19fb10SJason Ekstrand  * The callback will get called the next time drm_syncobj_replace_fence is
82*9c19fb10SJason Ekstrand  * called.
83*9c19fb10SJason Ekstrand  */
84*9c19fb10SJason Ekstrand struct drm_syncobj_cb {
85*9c19fb10SJason Ekstrand 	struct list_head node;
86*9c19fb10SJason Ekstrand 	drm_syncobj_func_t func;
87*9c19fb10SJason Ekstrand };
88*9c19fb10SJason Ekstrand 
89e9083420SDave Airlie void drm_syncobj_free(struct kref *kref);
90e9083420SDave Airlie 
91e9083420SDave Airlie /**
92e9083420SDave Airlie  * drm_syncobj_get - acquire a syncobj reference
93e9083420SDave Airlie  * @obj: sync object
94e9083420SDave Airlie  *
95e9083420SDave Airlie  * This acquires additional reference to @obj. It is illegal to call this
96e9083420SDave Airlie  * without already holding a reference. No locks required.
97e9083420SDave Airlie  */
98e9083420SDave Airlie static inline void
99e9083420SDave Airlie drm_syncobj_get(struct drm_syncobj *obj)
100e9083420SDave Airlie {
101e9083420SDave Airlie 	kref_get(&obj->refcount);
102e9083420SDave Airlie }
103e9083420SDave Airlie 
104e9083420SDave Airlie /**
105e9083420SDave Airlie  * drm_syncobj_put - release a reference to a sync object.
106e9083420SDave Airlie  * @obj: sync object.
107e9083420SDave Airlie  */
108e9083420SDave Airlie static inline void
109e9083420SDave Airlie drm_syncobj_put(struct drm_syncobj *obj)
110e9083420SDave Airlie {
111e9083420SDave Airlie 	kref_put(&obj->refcount, drm_syncobj_free);
112e9083420SDave Airlie }
113e9083420SDave Airlie 
114309a5482SJason Ekstrand static inline struct dma_fence *
115309a5482SJason Ekstrand drm_syncobj_fence_get(struct drm_syncobj *syncobj)
116309a5482SJason Ekstrand {
117309a5482SJason Ekstrand 	struct dma_fence *fence;
118309a5482SJason Ekstrand 
119309a5482SJason Ekstrand 	rcu_read_lock();
120309a5482SJason Ekstrand 	fence = dma_fence_get_rcu_safe(&syncobj->fence);
121309a5482SJason Ekstrand 	rcu_read_unlock();
122309a5482SJason Ekstrand 
123309a5482SJason Ekstrand 	return fence;
124309a5482SJason Ekstrand }
125309a5482SJason Ekstrand 
126e9083420SDave Airlie struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
127e9083420SDave Airlie 				     u32 handle);
128*9c19fb10SJason Ekstrand void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
129*9c19fb10SJason Ekstrand 			      struct drm_syncobj_cb *cb,
130*9c19fb10SJason Ekstrand 			      drm_syncobj_func_t func);
131*9c19fb10SJason Ekstrand void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
132*9c19fb10SJason Ekstrand 				 struct drm_syncobj_cb *cb);
13300fc2c26SChris Wilson void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
134e9083420SDave Airlie 			       struct dma_fence *fence);
135afaf5923SJason Ekstrand int drm_syncobj_find_fence(struct drm_file *file_private,
136e9083420SDave Airlie 			   u32 handle,
137e9083420SDave Airlie 			   struct dma_fence **fence);
138e9083420SDave Airlie void drm_syncobj_free(struct kref *kref);
139e9083420SDave Airlie 
140e9083420SDave Airlie #endif
141