dma-buf: rename and cleanup dma_resv_get_excl v3
[linux-block.git] / include / linux / dma-resv.h
CommitLineData
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
48extern 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 57struct 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 70struct 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 89static 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
0c6b522a
CK
95#ifdef CONFIG_DEBUG_MUTEXES
96void dma_resv_reset_shared_max(struct dma_resv *obj);
97#else
98static inline void dma_resv_reset_shared_max(struct dma_resv *obj) {}
99#endif
100
122020af 101/**
52791eee 102 * dma_resv_lock - lock the reservation object
122020af
CW
103 * @obj: the reservation object
104 * @ctx: the locking context
105 *
106 * Locks the reservation object for exclusive access and modification. Note,
107 * that the lock is only against other writers, readers will run concurrently
108 * with a writer under RCU. The seqlock is used to notify readers if they
109 * overlap with a writer.
110 *
111 * As the reservation object may be locked by multiple parties in an
112 * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
113 * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
114 * object may be locked by itself by passing NULL as @ctx.
115 */
52791eee
CK
116static inline int dma_resv_lock(struct dma_resv *obj,
117 struct ww_acquire_ctx *ctx)
122020af
CW
118{
119 return ww_mutex_lock(&obj->lock, ctx);
120}
121
5d276a1a 122/**
52791eee 123 * dma_resv_lock_interruptible - lock the reservation object
5d276a1a
CK
124 * @obj: the reservation object
125 * @ctx: the locking context
126 *
127 * Locks the reservation object interruptible for exclusive access and
128 * modification. Note, that the lock is only against other writers, readers
129 * will run concurrently with a writer under RCU. The seqlock is used to
130 * notify readers if they overlap with a writer.
131 *
132 * As the reservation object may be locked by multiple parties in an
133 * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
134 * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
135 * object may be locked by itself by passing NULL as @ctx.
136 */
52791eee
CK
137static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
138 struct ww_acquire_ctx *ctx)
5d276a1a
CK
139{
140 return ww_mutex_lock_interruptible(&obj->lock, ctx);
141}
142
0dbd555a 143/**
52791eee 144 * dma_resv_lock_slow - slowpath lock the reservation object
0dbd555a
CK
145 * @obj: the reservation object
146 * @ctx: the locking context
147 *
148 * Acquires the reservation object after a die case. This function
52791eee 149 * will sleep until the lock becomes available. See dma_resv_lock() as
0dbd555a
CK
150 * well.
151 */
52791eee
CK
152static inline void dma_resv_lock_slow(struct dma_resv *obj,
153 struct ww_acquire_ctx *ctx)
0dbd555a
CK
154{
155 ww_mutex_lock_slow(&obj->lock, ctx);
156}
157
158/**
52791eee 159 * dma_resv_lock_slow_interruptible - slowpath lock the reservation
0dbd555a
CK
160 * object, interruptible
161 * @obj: the reservation object
162 * @ctx: the locking context
163 *
164 * Acquires the reservation object interruptible after a die case. This function
165 * will sleep until the lock becomes available. See
52791eee 166 * dma_resv_lock_interruptible() as well.
0dbd555a 167 */
52791eee
CK
168static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
169 struct ww_acquire_ctx *ctx)
0dbd555a
CK
170{
171 return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
172}
5d276a1a 173
2955b73d 174/**
52791eee 175 * dma_resv_trylock - trylock the reservation object
2955b73d
CW
176 * @obj: the reservation object
177 *
178 * Tries to lock the reservation object for exclusive access and modification.
179 * Note, that the lock is only against other writers, readers will run
180 * concurrently with a writer under RCU. The seqlock is used to notify readers
181 * if they overlap with a writer.
182 *
183 * Also note that since no context is provided, no deadlock protection is
184 * possible.
185 *
186 * Returns true if the lock was acquired, false otherwise.
187 */
52791eee 188static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
2955b73d
CW
189{
190 return ww_mutex_trylock(&obj->lock);
191}
192
0dbd555a 193/**
52791eee 194 * dma_resv_is_locked - is the reservation object locked
0dbd555a
CK
195 * @obj: the reservation object
196 *
197 * Returns true if the mutex is locked, false if unlocked.
198 */
52791eee 199static inline bool dma_resv_is_locked(struct dma_resv *obj)
0dbd555a
CK
200{
201 return ww_mutex_is_locked(&obj->lock);
202}
203
204/**
52791eee 205 * dma_resv_locking_ctx - returns the context used to lock the object
0dbd555a
CK
206 * @obj: the reservation object
207 *
208 * Returns the context used to lock a reservation object or NULL if no context
209 * was used or the object is not locked at all.
210 */
52791eee 211static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
0dbd555a
CK
212{
213 return READ_ONCE(obj->lock.ctx);
214}
215
122020af 216/**
52791eee 217 * dma_resv_unlock - unlock the reservation object
122020af
CW
218 * @obj: the reservation object
219 *
220 * Unlocks the reservation object following exclusive access.
221 */
52791eee 222static inline void dma_resv_unlock(struct dma_resv *obj)
122020af 223{
0c6b522a 224 dma_resv_reset_shared_max(obj);
122020af
CW
225 ww_mutex_unlock(&obj->lock);
226}
227
b016cd6e 228/**
6edbd6ab 229 * dma_resv_exclusive - return the object's exclusive fence
b016cd6e
CW
230 * @obj: the reservation object
231 *
6edbd6ab
CK
232 * Returns the exclusive fence (if any). Caller must either hold the objects
233 * through dma_resv_lock() or the RCU read side lock through rcu_read_lock(),
234 * or one of the variants of each
b016cd6e
CW
235 *
236 * RETURNS
237 * The exclusive fence or NULL
238 */
239static inline struct dma_fence *
6edbd6ab 240dma_resv_excl_fence(struct dma_resv *obj)
b016cd6e 241{
6edbd6ab 242 return rcu_dereference_check(obj->fence_excl, dma_resv_held(obj));
b016cd6e
CW
243}
244
245/**
246 * dma_resv_get_excl_rcu - get the reservation object's
247 * exclusive fence, without lock held.
248 * @obj: the reservation object
249 *
250 * If there is an exclusive fence, this atomically increments it's
251 * reference count and returns it.
252 *
253 * RETURNS
254 * The exclusive fence or NULL if none
255 */
256static inline struct dma_fence *
257dma_resv_get_excl_rcu(struct dma_resv *obj)
258{
259 struct dma_fence *fence;
260
261 if (!rcu_access_pointer(obj->fence_excl))
262 return NULL;
263
264 rcu_read_lock();
265 fence = dma_fence_get_rcu_safe(&obj->fence_excl);
266 rcu_read_unlock();
267
268 return fence;
269}
270
52791eee
CK
271void dma_resv_init(struct dma_resv *obj);
272void dma_resv_fini(struct dma_resv *obj);
273int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
274void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
04a5faa8 275
52791eee 276void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
04a5faa8 277
52791eee
CK
278int dma_resv_get_fences_rcu(struct dma_resv *obj,
279 struct dma_fence **pfence_excl,
280 unsigned *pshared_count,
281 struct dma_fence ***pshared);
3c3b177a 282
52791eee 283int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
7faf952a 284
52791eee
CK
285long dma_resv_wait_timeout_rcu(struct dma_resv *obj, bool wait_all, bool intr,
286 unsigned long timeout);
3c3b177a 287
52791eee 288bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all);
3c3b177a 289
786d7257 290#endif /* _LINUX_RESERVATION_H */