2 * Header file for reservations for dma-buf and ttm
4 * Copyright(C) 2011 Linaro Limited. All rights reserved.
5 * Copyright (C) 2012-2013 Canonical Ltd
6 * Copyright (C) 2012 Texas Instruments
9 * Rob Clark <robdclark@gmail.com>
10 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
11 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
13 * Based on bo.c which bears the following copyright notice,
14 * but is dual licensed:
16 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
17 * All Rights Reserved.
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:
27 * The above copyright notice and this permission notice (including the
28 * next paragraph) shall be included in all copies or substantial portions
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.
39 #ifndef _LINUX_RESERVATION_H
40 #define _LINUX_RESERVATION_H
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>
48 extern struct ww_class reservation_ww_class;
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
57 struct reservation_object_list {
59 u32 shared_count, shared_max;
60 struct dma_fence __rcu *shared[];
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
70 struct reservation_object {
73 struct dma_fence __rcu *fence_excl;
74 struct reservation_object_list __rcu *fence;
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)
82 * reservation_object_get_excl - get the reservation object's
83 * exclusive fence, with update-side lock held
84 * @obj: the reservation object
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.
91 * The exclusive fence or NULL
93 static inline struct dma_fence *
94 reservation_object_get_excl(struct reservation_object *obj)
96 return rcu_dereference_protected(obj->fence_excl,
97 reservation_object_held(obj));
101 * reservation_object_get_list - get the reservation object's
102 * shared fence list, with update-side lock held
103 * @obj: the reservation object
105 * Returns the shared fence list. Does NOT take references to
106 * the fence. The obj->lock must be held.
108 static inline struct reservation_object_list *
109 reservation_object_get_list(struct reservation_object *obj)
111 return rcu_dereference_protected(obj->fence,
112 reservation_object_held(obj));
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
121 * Make sure we have a consisten exclusive fence and shared fence list.
122 * Must be called with rcu read side lock held.
125 reservation_object_fences(struct reservation_object *obj,
126 struct dma_fence **excl,
127 struct reservation_object_list **list,
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);
139 * reservation_object_get_excl_rcu - get the reservation object's
140 * exclusive fence, without lock held.
141 * @obj: the reservation object
143 * If there is an exclusive fence, this atomically increments it's
144 * reference count and returns it.
147 * The exclusive fence or NULL if none
149 static inline struct dma_fence *
150 reservation_object_get_excl_rcu(struct reservation_object *obj)
152 struct dma_fence *fence;
154 if (!rcu_access_pointer(obj->fence_excl))
158 fence = dma_fence_get_rcu_safe(&obj->fence_excl);
165 * reservation_object_lock - lock the reservation object
166 * @obj: the reservation object
167 * @ctx: the locking context
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.
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.
180 reservation_object_lock(struct reservation_object *obj,
181 struct ww_acquire_ctx *ctx)
183 return ww_mutex_lock(&obj->lock, ctx);
187 * reservation_object_lock_interruptible - lock the reservation object
188 * @obj: the reservation object
189 * @ctx: the locking context
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.
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.
202 reservation_object_lock_interruptible(struct reservation_object *obj,
203 struct ww_acquire_ctx *ctx)
205 return ww_mutex_lock_interruptible(&obj->lock, ctx);
209 * reservation_object_lock_slow - slowpath lock the reservation object
210 * @obj: the reservation object
211 * @ctx: the locking context
213 * Acquires the reservation object after a die case. This function
214 * will sleep until the lock becomes available. See reservation_object_lock() as
218 reservation_object_lock_slow(struct reservation_object *obj,
219 struct ww_acquire_ctx *ctx)
221 ww_mutex_lock_slow(&obj->lock, ctx);
225 * reservation_object_lock_slow_interruptible - slowpath lock the reservation
226 * object, interruptible
227 * @obj: the reservation object
228 * @ctx: the locking context
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.
235 reservation_object_lock_slow_interruptible(struct reservation_object *obj,
236 struct ww_acquire_ctx *ctx)
238 return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
242 * reservation_object_trylock - trylock the reservation object
243 * @obj: the reservation object
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.
250 * Also note that since no context is provided, no deadlock protection is
253 * Returns true if the lock was acquired, false otherwise.
255 static inline bool __must_check
256 reservation_object_trylock(struct reservation_object *obj)
258 return ww_mutex_trylock(&obj->lock);
262 * reservation_object_is_locked - is the reservation object locked
263 * @obj: the reservation object
265 * Returns true if the mutex is locked, false if unlocked.
268 reservation_object_is_locked(struct reservation_object *obj)
270 return ww_mutex_is_locked(&obj->lock);
274 * reservation_object_locking_ctx - returns the context used to lock the object
275 * @obj: the reservation object
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.
280 static inline struct ww_acquire_ctx *
281 reservation_object_locking_ctx(struct reservation_object *obj)
283 return READ_ONCE(obj->lock.ctx);
287 * reservation_object_unlock - unlock the reservation object
288 * @obj: the reservation object
290 * Unlocks the reservation object following exclusive access.
293 reservation_object_unlock(struct reservation_object *obj)
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);
301 fence->shared_max = fence->shared_count;
304 ww_mutex_unlock(&obj->lock);
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);
314 void reservation_object_add_excl_fence(struct reservation_object *obj,
315 struct dma_fence *fence);
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);
322 int reservation_object_copy_fences(struct reservation_object *dst,
323 struct reservation_object *src);
325 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
326 bool wait_all, bool intr,
327 unsigned long timeout);
329 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
332 #endif /* _LINUX_RESERVATION_H */