xref: /linux-6.15/include/drm/drm_syncobj.h (revision c7a47229)
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 
29dd7ece7fSJani Nikula #include <linux/dma-fence.h>
3044f8a139SChristian König #include <linux/dma-fence-chain.h>
31dd7ece7fSJani Nikula 
32dd7ece7fSJani Nikula struct drm_file;
33e9083420SDave Airlie 
34e9083420SDave Airlie /**
35e9083420SDave Airlie  * struct drm_syncobj - sync object.
36e9083420SDave Airlie  *
37131280a1SEric Anholt  * This structure defines a generic sync object which wraps a &dma_fence.
38e9083420SDave Airlie  */
39e9083420SDave Airlie struct drm_syncobj {
40e9083420SDave Airlie 	/**
41924fe8dfSDaniel Vetter 	 * @refcount: Reference count of this object.
42e9083420SDave Airlie 	 */
43e9083420SDave Airlie 	struct kref refcount;
44e9083420SDave Airlie 	/**
45131280a1SEric Anholt 	 * @fence:
46131280a1SEric Anholt 	 * NULL or a pointer to the fence bound to this object.
47131280a1SEric Anholt 	 *
48131280a1SEric Anholt 	 * This field should not be used directly. Use drm_syncobj_fence_get()
49131280a1SEric Anholt 	 * and drm_syncobj_replace_fence() instead.
50e9083420SDave Airlie 	 */
51131280a1SEric Anholt 	struct dma_fence __rcu *fence;
52e9083420SDave Airlie 	/**
53924fe8dfSDaniel Vetter 	 * @cb_list: List of callbacks to call when the &fence gets replaced.
549c19fb10SJason Ekstrand 	 */
559c19fb10SJason Ekstrand 	struct list_head cb_list;
569c19fb10SJason Ekstrand 	/**
57*c7a47229SSimon Ser 	 * @ev_fd_list: List of registered eventfd.
58*c7a47229SSimon Ser 	 */
59*c7a47229SSimon Ser 	struct list_head ev_fd_list;
60*c7a47229SSimon Ser 	/**
61*c7a47229SSimon Ser 	 * @lock: Protects &cb_list and &ev_fd_list, and write-locks &fence.
629c19fb10SJason Ekstrand 	 */
63131280a1SEric Anholt 	spinlock_t lock;
649c19fb10SJason Ekstrand 	/**
65924fe8dfSDaniel Vetter 	 * @file: A file backing for this syncobj.
66e9083420SDave Airlie 	 */
67e9083420SDave Airlie 	struct file *file;
68e9083420SDave Airlie };
69e9083420SDave Airlie 
70e9083420SDave Airlie void drm_syncobj_free(struct kref *kref);
71e9083420SDave Airlie 
72e9083420SDave Airlie /**
73e9083420SDave Airlie  * drm_syncobj_get - acquire a syncobj reference
74e9083420SDave Airlie  * @obj: sync object
75e9083420SDave Airlie  *
76924fe8dfSDaniel Vetter  * This acquires an additional reference to @obj. It is illegal to call this
77e9083420SDave Airlie  * without already holding a reference. No locks required.
78e9083420SDave Airlie  */
79e9083420SDave Airlie static inline void
drm_syncobj_get(struct drm_syncobj * obj)80e9083420SDave Airlie drm_syncobj_get(struct drm_syncobj *obj)
81e9083420SDave Airlie {
82e9083420SDave Airlie 	kref_get(&obj->refcount);
83e9083420SDave Airlie }
84e9083420SDave Airlie 
85e9083420SDave Airlie /**
86e9083420SDave Airlie  * drm_syncobj_put - release a reference to a sync object.
87e9083420SDave Airlie  * @obj: sync object.
88e9083420SDave Airlie  */
89e9083420SDave Airlie static inline void
drm_syncobj_put(struct drm_syncobj * obj)90e9083420SDave Airlie drm_syncobj_put(struct drm_syncobj *obj)
91e9083420SDave Airlie {
92e9083420SDave Airlie 	kref_put(&obj->refcount, drm_syncobj_free);
93e9083420SDave Airlie }
94e9083420SDave Airlie 
95131280a1SEric Anholt /**
96131280a1SEric Anholt  * drm_syncobj_fence_get - get a reference to a fence in a sync object
97131280a1SEric Anholt  * @syncobj: sync object.
98131280a1SEric Anholt  *
99131280a1SEric Anholt  * This acquires additional reference to &drm_syncobj.fence contained in @obj,
100131280a1SEric Anholt  * if not NULL. It is illegal to call this without already holding a reference.
101131280a1SEric Anholt  * No locks required.
102131280a1SEric Anholt  *
103131280a1SEric Anholt  * Returns:
104131280a1SEric Anholt  * Either the fence of @obj or NULL if there's none.
105131280a1SEric Anholt  */
106131280a1SEric Anholt static inline struct dma_fence *
drm_syncobj_fence_get(struct drm_syncobj * syncobj)107131280a1SEric Anholt drm_syncobj_fence_get(struct drm_syncobj *syncobj)
108131280a1SEric Anholt {
109131280a1SEric Anholt 	struct dma_fence *fence;
110131280a1SEric Anholt 
111131280a1SEric Anholt 	rcu_read_lock();
112131280a1SEric Anholt 	fence = dma_fence_get_rcu_safe(&syncobj->fence);
113131280a1SEric Anholt 	rcu_read_unlock();
114131280a1SEric Anholt 
115131280a1SEric Anholt 	return fence;
116131280a1SEric Anholt }
117131280a1SEric Anholt 
118e9083420SDave Airlie struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
119e9083420SDave Airlie 				     u32 handle);
12044f8a139SChristian König void drm_syncobj_add_point(struct drm_syncobj *syncobj,
12144f8a139SChristian König 			   struct dma_fence_chain *chain,
12244f8a139SChristian König 			   struct dma_fence *fence,
12344f8a139SChristian König 			   uint64_t point);
1240b258ed1SChristian König void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
125e9083420SDave Airlie 			       struct dma_fence *fence);
126afaf5923SJason Ekstrand int drm_syncobj_find_fence(struct drm_file *file_private,
127649fdce2SChunming Zhou 			   u32 handle, u64 point, u64 flags,
128e9083420SDave Airlie 			   struct dma_fence **fence);
129e9083420SDave Airlie void drm_syncobj_free(struct kref *kref);
1301321fd2cSMarek Olšák int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
1311321fd2cSMarek Olšák 		       struct dma_fence *fence);
1321321fd2cSMarek Olšák int drm_syncobj_get_handle(struct drm_file *file_private,
1331321fd2cSMarek Olšák 			   struct drm_syncobj *syncobj, u32 *handle);
134684fd0afSMarek Olšák int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
135e9083420SDave Airlie 
136e9083420SDave Airlie #endif
137