]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/reservation.h
dma-buf: nuke reservation_object seq number
[linux.git] / include / linux / reservation.h
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:
9  * Rob Clark <robdclark@gmail.com>
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
42 #include <linux/ww_mutex.h>
43 #include <linux/dma-fence.h>
44 #include <linux/slab.h>
45 #include <linux/seqlock.h>
46 #include <linux/rcupdate.h>
47
48 extern struct ww_class reservation_ww_class;
49
50 /**
51  * struct reservation_object_list - a list of shared fences
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  */
57 struct reservation_object_list {
58         struct rcu_head rcu;
59         u32 shared_count, shared_max;
60         struct dma_fence __rcu *shared[];
61 };
62
63 /**
64  * struct reservation_object - a reservation object manages fences for a buffer
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
69  */
70 struct reservation_object {
71         struct ww_mutex lock;
72
73         struct dma_fence __rcu *fence_excl;
74         struct reservation_object_list __rcu *fence;
75 };
76
77 #define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
78 #define reservation_object_assert_held(obj) \
79         lockdep_assert_held(&(obj)->lock.base)
80
81 /**
82  * reservation_object_get_excl - get the reservation object's
83  * exclusive fence, with update-side lock held
84  * @obj: the reservation object
85  *
86  * Returns the exclusive fence (if any).  Does NOT take a
87  * reference. Writers must hold obj->lock, readers may only
88  * hold a RCU read side lock.
89  *
90  * RETURNS
91  * The exclusive fence or NULL
92  */
93 static inline struct dma_fence *
94 reservation_object_get_excl(struct reservation_object *obj)
95 {
96         return rcu_dereference_protected(obj->fence_excl,
97                                          reservation_object_held(obj));
98 }
99
100 /**
101  * reservation_object_get_list - get the reservation object's
102  * shared fence list, with update-side lock held
103  * @obj: the reservation object
104  *
105  * Returns the shared fence list.  Does NOT take references to
106  * the fence.  The obj->lock must be held.
107  */
108 static inline struct reservation_object_list *
109 reservation_object_get_list(struct reservation_object *obj)
110 {
111         return rcu_dereference_protected(obj->fence,
112                                          reservation_object_held(obj));
113 }
114
115 /**
116  * reservation_object_fences - read consistent fence pointers
117  * @obj: reservation object where we get the fences from
118  * @excl: pointer for the exclusive fence
119  * @list: pointer for the shared fence list
120  *
121  * Make sure we have a consisten exclusive fence and shared fence list.
122  * Must be called with rcu read side lock held.
123  */
124 static inline void
125 reservation_object_fences(struct reservation_object *obj,
126                           struct dma_fence **excl,
127                           struct reservation_object_list **list,
128                           u32 *shared_count)
129 {
130         do {
131                 *excl = rcu_dereference(obj->fence_excl);
132                 *list = rcu_dereference(obj->fence);
133                 *shared_count = *list ? (*list)->shared_count : 0;
134                 smp_rmb(); /* See reservation_object_add_excl_fence */
135         } while (rcu_access_pointer(obj->fence_excl) != *excl);
136 }
137
138 /**
139  * reservation_object_get_excl_rcu - get the reservation object's
140  * exclusive fence, without lock held.
141  * @obj: the reservation object
142  *
143  * If there is an exclusive fence, this atomically increments it's
144  * reference count and returns it.
145  *
146  * RETURNS
147  * The exclusive fence or NULL if none
148  */
149 static inline struct dma_fence *
150 reservation_object_get_excl_rcu(struct reservation_object *obj)
151 {
152         struct dma_fence *fence;
153
154         if (!rcu_access_pointer(obj->fence_excl))
155                 return NULL;
156
157         rcu_read_lock();
158         fence = dma_fence_get_rcu_safe(&obj->fence_excl);
159         rcu_read_unlock();
160
161         return fence;
162 }
163
164 /**
165  * reservation_object_lock - lock the reservation object
166  * @obj: the reservation object
167  * @ctx: the locking context
168  *
169  * Locks the reservation object for exclusive access and modification. Note,
170  * that the lock is only against other writers, readers will run concurrently
171  * with a writer under RCU. The seqlock is used to notify readers if they
172  * overlap with a writer.
173  *
174  * As the reservation object may be locked by multiple parties in an
175  * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
176  * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
177  * object may be locked by itself by passing NULL as @ctx.
178  */
179 static inline int
180 reservation_object_lock(struct reservation_object *obj,
181                         struct ww_acquire_ctx *ctx)
182 {
183         return ww_mutex_lock(&obj->lock, ctx);
184 }
185
186 /**
187  * reservation_object_lock_interruptible - lock the reservation object
188  * @obj: the reservation object
189  * @ctx: the locking context
190  *
191  * Locks the reservation object interruptible for exclusive access and
192  * modification. Note, that the lock is only against other writers, readers
193  * will run concurrently with a writer under RCU. The seqlock is used to
194  * notify readers if they overlap with a writer.
195  *
196  * As the reservation object may be locked by multiple parties in an
197  * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
198  * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
199  * object may be locked by itself by passing NULL as @ctx.
200  */
201 static inline int
202 reservation_object_lock_interruptible(struct reservation_object *obj,
203                                       struct ww_acquire_ctx *ctx)
204 {
205         return ww_mutex_lock_interruptible(&obj->lock, ctx);
206 }
207
208 /**
209  * reservation_object_lock_slow - slowpath lock the reservation object
210  * @obj: the reservation object
211  * @ctx: the locking context
212  *
213  * Acquires the reservation object after a die case. This function
214  * will sleep until the lock becomes available. See reservation_object_lock() as
215  * well.
216  */
217 static inline void
218 reservation_object_lock_slow(struct reservation_object *obj,
219                              struct ww_acquire_ctx *ctx)
220 {
221         ww_mutex_lock_slow(&obj->lock, ctx);
222 }
223
224 /**
225  * reservation_object_lock_slow_interruptible - slowpath lock the reservation
226  * object, interruptible
227  * @obj: the reservation object
228  * @ctx: the locking context
229  *
230  * Acquires the reservation object interruptible after a die case. This function
231  * will sleep until the lock becomes available. See
232  * reservation_object_lock_interruptible() as well.
233  */
234 static inline int
235 reservation_object_lock_slow_interruptible(struct reservation_object *obj,
236                                            struct ww_acquire_ctx *ctx)
237 {
238         return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
239 }
240
241 /**
242  * reservation_object_trylock - trylock the reservation object
243  * @obj: the reservation object
244  *
245  * Tries to lock the reservation object for exclusive access and modification.
246  * Note, that the lock is only against other writers, readers will run
247  * concurrently with a writer under RCU. The seqlock is used to notify readers
248  * if they overlap with a writer.
249  *
250  * Also note that since no context is provided, no deadlock protection is
251  * possible.
252  *
253  * Returns true if the lock was acquired, false otherwise.
254  */
255 static inline bool __must_check
256 reservation_object_trylock(struct reservation_object *obj)
257 {
258         return ww_mutex_trylock(&obj->lock);
259 }
260
261 /**
262  * reservation_object_is_locked - is the reservation object locked
263  * @obj: the reservation object
264  *
265  * Returns true if the mutex is locked, false if unlocked.
266  */
267 static inline bool
268 reservation_object_is_locked(struct reservation_object *obj)
269 {
270         return ww_mutex_is_locked(&obj->lock);
271 }
272
273 /**
274  * reservation_object_locking_ctx - returns the context used to lock the object
275  * @obj: the reservation object
276  *
277  * Returns the context used to lock a reservation object or NULL if no context
278  * was used or the object is not locked at all.
279  */
280 static inline struct ww_acquire_ctx *
281 reservation_object_locking_ctx(struct reservation_object *obj)
282 {
283         return READ_ONCE(obj->lock.ctx);
284 }
285
286 /**
287  * reservation_object_unlock - unlock the reservation object
288  * @obj: the reservation object
289  *
290  * Unlocks the reservation object following exclusive access.
291  */
292 static inline void
293 reservation_object_unlock(struct reservation_object *obj)
294 {
295 #ifdef CONFIG_DEBUG_MUTEXES
296         /* Test shared fence slot reservation */
297         if (rcu_access_pointer(obj->fence)) {
298                 struct reservation_object_list *fence =
299                         reservation_object_get_list(obj);
300
301                 fence->shared_max = fence->shared_count;
302         }
303 #endif
304         ww_mutex_unlock(&obj->lock);
305 }
306
307 void reservation_object_init(struct reservation_object *obj);
308 void reservation_object_fini(struct reservation_object *obj);
309 int reservation_object_reserve_shared(struct reservation_object *obj,
310                                       unsigned int num_fences);
311 void reservation_object_add_shared_fence(struct reservation_object *obj,
312                                          struct dma_fence *fence);
313
314 void reservation_object_add_excl_fence(struct reservation_object *obj,
315                                        struct dma_fence *fence);
316
317 int reservation_object_get_fences_rcu(struct reservation_object *obj,
318                                       struct dma_fence **pfence_excl,
319                                       unsigned *pshared_count,
320                                       struct dma_fence ***pshared);
321
322 int reservation_object_copy_fences(struct reservation_object *dst,
323                                    struct reservation_object *src);
324
325 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
326                                          bool wait_all, bool intr,
327                                          unsigned long timeout);
328
329 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
330                                           bool test_all);
331
332 #endif /* _LINUX_RESERVATION_H */