]>
Commit | Line | Data |
---|---|---|
786d7257 ML |
1 | /* |
2 | * Header file for reservations for dma-buf and ttm | |
3 | * | |
4 | * Copyright(C) 2011 Linaro Limited. All rights reserved. | |
5 | * Copyright (C) 2012-2013 Canonical Ltd | |
6 | * Copyright (C) 2012 Texas Instruments | |
7 | * | |
8 | * Authors: | |
0ba6b8fb | 9 | * Rob Clark <robdclark@gmail.com> |
786d7257 ML |
10 | * Maarten Lankhorst <maarten.lankhorst@canonical.com> |
11 | * Thomas Hellstrom <thellstrom-at-vmware-dot-com> | |
12 | * | |
13 | * Based on bo.c which bears the following copyright notice, | |
14 | * but is dual licensed: | |
15 | * | |
16 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA | |
17 | * All Rights Reserved. | |
18 | * | |
19 | * Permission is hereby granted, free of charge, to any person obtaining a | |
20 | * copy of this software and associated documentation files (the | |
21 | * "Software"), to deal in the Software without restriction, including | |
22 | * without limitation the rights to use, copy, modify, merge, publish, | |
23 | * distribute, sub license, and/or sell copies of the Software, and to | |
24 | * permit persons to whom the Software is furnished to do so, subject to | |
25 | * the following conditions: | |
26 | * | |
27 | * The above copyright notice and this permission notice (including the | |
28 | * next paragraph) shall be included in all copies or substantial portions | |
29 | * of the Software. | |
30 | * | |
31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
32 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
33 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | |
34 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | |
35 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
36 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
37 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | |
38 | */ | |
39 | #ifndef _LINUX_RESERVATION_H | |
40 | #define _LINUX_RESERVATION_H | |
41 | ||
1b375dc3 | 42 | #include <linux/ww_mutex.h> |
f54d1867 | 43 | #include <linux/dma-fence.h> |
0ba6b8fb | 44 | #include <linux/slab.h> |
3c3b177a ML |
45 | #include <linux/seqlock.h> |
46 | #include <linux/rcupdate.h> | |
786d7257 ML |
47 | |
48 | extern struct ww_class reservation_ww_class; | |
49 | ||
dad6c394 | 50 | /** |
52791eee | 51 | * struct dma_resv_list - a list of shared fences |
dad6c394 RC |
52 | * @rcu: for internal use |
53 | * @shared_count: table of shared fences | |
54 | * @shared_max: for growing shared fence table | |
55 | * @shared: shared fence table | |
56 | */ | |
52791eee | 57 | struct dma_resv_list { |
3c3b177a | 58 | struct rcu_head rcu; |
04a5faa8 | 59 | u32 shared_count, shared_max; |
f54d1867 | 60 | struct dma_fence __rcu *shared[]; |
04a5faa8 ML |
61 | }; |
62 | ||
dad6c394 | 63 | /** |
52791eee | 64 | * struct dma_resv - a reservation object manages fences for a buffer |
dad6c394 RC |
65 | * @lock: update side lock |
66 | * @seq: sequence count for managing RCU read-side synchronization | |
67 | * @fence_excl: the exclusive fence, if there is one currently | |
68 | * @fence: list of current shared fences | |
dad6c394 | 69 | */ |
52791eee | 70 | struct dma_resv { |
786d7257 | 71 | struct ww_mutex lock; |
cd29f220 | 72 | seqcount_ww_mutex_t seq; |
0ba6b8fb | 73 | |
f54d1867 | 74 | struct dma_fence __rcu *fence_excl; |
52791eee | 75 | struct dma_resv_list __rcu *fence; |
786d7257 ML |
76 | }; |
77 | ||
52791eee CK |
78 | #define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base) |
79 | #define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base) | |
04a5faa8 | 80 | |
dad6c394 | 81 | /** |
52791eee | 82 | * dma_resv_get_list - get the reservation object's |
dad6c394 RC |
83 | * shared fence list, with update-side lock held |
84 | * @obj: the reservation object | |
85 | * | |
86 | * Returns the shared fence list. Does NOT take references to | |
87 | * the fence. The obj->lock must be held. | |
88 | */ | |
52791eee | 89 | static inline struct dma_resv_list *dma_resv_get_list(struct dma_resv *obj) |
04a5faa8 | 90 | { |
3c3b177a | 91 | return rcu_dereference_protected(obj->fence, |
52791eee | 92 | dma_resv_held(obj)); |
04a5faa8 ML |
93 | } |
94 | ||
122020af | 95 | /** |
52791eee | 96 | * dma_resv_lock - lock the reservation object |
122020af CW |
97 | * @obj: the reservation object |
98 | * @ctx: the locking context | |
99 | * | |
100 | * Locks the reservation object for exclusive access and modification. Note, | |
101 | * that the lock is only against other writers, readers will run concurrently | |
102 | * with a writer under RCU. The seqlock is used to notify readers if they | |
103 | * overlap with a writer. | |
104 | * | |
105 | * As the reservation object may be locked by multiple parties in an | |
106 | * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle | |
107 | * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation | |
108 | * object may be locked by itself by passing NULL as @ctx. | |
109 | */ | |
52791eee CK |
110 | static inline int dma_resv_lock(struct dma_resv *obj, |
111 | struct ww_acquire_ctx *ctx) | |
122020af CW |
112 | { |
113 | return ww_mutex_lock(&obj->lock, ctx); | |
114 | } | |
115 | ||
5d276a1a | 116 | /** |
52791eee | 117 | * dma_resv_lock_interruptible - lock the reservation object |
5d276a1a CK |
118 | * @obj: the reservation object |
119 | * @ctx: the locking context | |
120 | * | |
121 | * Locks the reservation object interruptible for exclusive access and | |
122 | * modification. Note, that the lock is only against other writers, readers | |
123 | * will run concurrently with a writer under RCU. The seqlock is used to | |
124 | * notify readers if they overlap with a writer. | |
125 | * | |
126 | * As the reservation object may be locked by multiple parties in an | |
127 | * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle | |
128 | * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation | |
129 | * object may be locked by itself by passing NULL as @ctx. | |
130 | */ | |
52791eee CK |
131 | static inline int dma_resv_lock_interruptible(struct dma_resv *obj, |
132 | struct ww_acquire_ctx *ctx) | |
5d276a1a CK |
133 | { |
134 | return ww_mutex_lock_interruptible(&obj->lock, ctx); | |
135 | } | |
136 | ||
0dbd555a | 137 | /** |
52791eee | 138 | * dma_resv_lock_slow - slowpath lock the reservation object |
0dbd555a CK |
139 | * @obj: the reservation object |
140 | * @ctx: the locking context | |
141 | * | |
142 | * Acquires the reservation object after a die case. This function | |
52791eee | 143 | * will sleep until the lock becomes available. See dma_resv_lock() as |
0dbd555a CK |
144 | * well. |
145 | */ | |
52791eee CK |
146 | static inline void dma_resv_lock_slow(struct dma_resv *obj, |
147 | struct ww_acquire_ctx *ctx) | |
0dbd555a CK |
148 | { |
149 | ww_mutex_lock_slow(&obj->lock, ctx); | |
150 | } | |
151 | ||
152 | /** | |
52791eee | 153 | * dma_resv_lock_slow_interruptible - slowpath lock the reservation |
0dbd555a CK |
154 | * object, interruptible |
155 | * @obj: the reservation object | |
156 | * @ctx: the locking context | |
157 | * | |
158 | * Acquires the reservation object interruptible after a die case. This function | |
159 | * will sleep until the lock becomes available. See | |
52791eee | 160 | * dma_resv_lock_interruptible() as well. |
0dbd555a | 161 | */ |
52791eee CK |
162 | static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj, |
163 | struct ww_acquire_ctx *ctx) | |
0dbd555a CK |
164 | { |
165 | return ww_mutex_lock_slow_interruptible(&obj->lock, ctx); | |
166 | } | |
5d276a1a | 167 | |
2955b73d | 168 | /** |
52791eee | 169 | * dma_resv_trylock - trylock the reservation object |
2955b73d CW |
170 | * @obj: the reservation object |
171 | * | |
172 | * Tries to lock the reservation object for exclusive access and modification. | |
173 | * Note, that the lock is only against other writers, readers will run | |
174 | * concurrently with a writer under RCU. The seqlock is used to notify readers | |
175 | * if they overlap with a writer. | |
176 | * | |
177 | * Also note that since no context is provided, no deadlock protection is | |
178 | * possible. | |
179 | * | |
180 | * Returns true if the lock was acquired, false otherwise. | |
181 | */ | |
52791eee | 182 | static inline bool __must_check dma_resv_trylock(struct dma_resv *obj) |
2955b73d CW |
183 | { |
184 | return ww_mutex_trylock(&obj->lock); | |
185 | } | |
186 | ||
0dbd555a | 187 | /** |
52791eee | 188 | * dma_resv_is_locked - is the reservation object locked |
0dbd555a CK |
189 | * @obj: the reservation object |
190 | * | |
191 | * Returns true if the mutex is locked, false if unlocked. | |
192 | */ | |
52791eee | 193 | static inline bool dma_resv_is_locked(struct dma_resv *obj) |
0dbd555a CK |
194 | { |
195 | return ww_mutex_is_locked(&obj->lock); | |
196 | } | |
197 | ||
198 | /** | |
52791eee | 199 | * dma_resv_locking_ctx - returns the context used to lock the object |
0dbd555a CK |
200 | * @obj: the reservation object |
201 | * | |
202 | * Returns the context used to lock a reservation object or NULL if no context | |
203 | * was used or the object is not locked at all. | |
204 | */ | |
52791eee | 205 | static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj) |
0dbd555a CK |
206 | { |
207 | return READ_ONCE(obj->lock.ctx); | |
208 | } | |
209 | ||
122020af | 210 | /** |
52791eee | 211 | * dma_resv_unlock - unlock the reservation object |
122020af CW |
212 | * @obj: the reservation object |
213 | * | |
214 | * Unlocks the reservation object following exclusive access. | |
215 | */ | |
52791eee | 216 | static inline void dma_resv_unlock(struct dma_resv *obj) |
122020af | 217 | { |
99fe21a7 CK |
218 | #ifdef CONFIG_DEBUG_MUTEXES |
219 | /* Test shared fence slot reservation */ | |
5740671e | 220 | if (rcu_access_pointer(obj->fence)) { |
52791eee | 221 | struct dma_resv_list *fence = dma_resv_get_list(obj); |
5740671e CW |
222 | |
223 | fence->shared_max = fence->shared_count; | |
224 | } | |
99fe21a7 | 225 | #endif |
122020af CW |
226 | ww_mutex_unlock(&obj->lock); |
227 | } | |
228 | ||
b016cd6e CW |
229 | /** |
230 | * dma_resv_get_excl - get the reservation object's | |
231 | * exclusive fence, with update-side lock held | |
232 | * @obj: the reservation object | |
233 | * | |
234 | * Returns the exclusive fence (if any). Does NOT take a | |
235 | * reference. Writers must hold obj->lock, readers may only | |
236 | * hold a RCU read side lock. | |
237 | * | |
238 | * RETURNS | |
239 | * The exclusive fence or NULL | |
240 | */ | |
241 | static inline struct dma_fence * | |
242 | dma_resv_get_excl(struct dma_resv *obj) | |
243 | { | |
244 | return rcu_dereference_protected(obj->fence_excl, | |
245 | dma_resv_held(obj)); | |
246 | } | |
247 | ||
248 | /** | |
249 | * dma_resv_get_excl_rcu - get the reservation object's | |
250 | * exclusive fence, without lock held. | |
251 | * @obj: the reservation object | |
252 | * | |
253 | * If there is an exclusive fence, this atomically increments it's | |
254 | * reference count and returns it. | |
255 | * | |
256 | * RETURNS | |
257 | * The exclusive fence or NULL if none | |
258 | */ | |
259 | static inline struct dma_fence * | |
260 | dma_resv_get_excl_rcu(struct dma_resv *obj) | |
261 | { | |
262 | struct dma_fence *fence; | |
263 | ||
264 | if (!rcu_access_pointer(obj->fence_excl)) | |
265 | return NULL; | |
266 | ||
267 | rcu_read_lock(); | |
268 | fence = dma_fence_get_rcu_safe(&obj->fence_excl); | |
269 | rcu_read_unlock(); | |
270 | ||
271 | return fence; | |
272 | } | |
273 | ||
52791eee CK |
274 | void dma_resv_init(struct dma_resv *obj); |
275 | void dma_resv_fini(struct dma_resv *obj); | |
276 | int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences); | |
277 | void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence); | |
04a5faa8 | 278 | |
52791eee | 279 | void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence); |
04a5faa8 | 280 | |
52791eee CK |
281 | int dma_resv_get_fences_rcu(struct dma_resv *obj, |
282 | struct dma_fence **pfence_excl, | |
283 | unsigned *pshared_count, | |
284 | struct dma_fence ***pshared); | |
3c3b177a | 285 | |
52791eee | 286 | int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src); |
7faf952a | 287 | |
52791eee CK |
288 | long dma_resv_wait_timeout_rcu(struct dma_resv *obj, bool wait_all, bool intr, |
289 | unsigned long timeout); | |
3c3b177a | 290 | |
52791eee | 291 | bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all); |
3c3b177a | 292 | |
786d7257 | 293 | #endif /* _LINUX_RESERVATION_H */ |