drivers/dma-buf: dma-buf.c: fix a typo
[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
8938d484 50struct dma_resv_list;
04a5faa8 51
dad6c394 52/**
52791eee 53 * struct dma_resv - a reservation object manages fences for a buffer
d9edf92d
DV
54 *
55 * There are multiple uses for this, with sometimes slightly different rules in
56 * how the fence slots are used.
57 *
58 * One use is to synchronize cross-driver access to a struct dma_buf, either for
59 * dynamic buffer management or just to handle implicit synchronization between
60 * different users of the buffer in userspace. See &dma_buf.resv for a more
61 * in-depth discussion.
62 *
63 * The other major use is to manage access and locking within a driver in a
64 * buffer based memory manager. struct ttm_buffer_object is the canonical
65 * example here, since this is where reservation objects originated from. But
66 * use in drivers is spreading and some drivers also manage struct
67 * drm_gem_object with the same scheme.
dad6c394 68 */
52791eee 69struct dma_resv {
d9edf92d
DV
70 /**
71 * @lock:
72 *
73 * Update side lock. Don't use directly, instead use the wrapper
74 * functions like dma_resv_lock() and dma_resv_unlock().
75 *
76 * Drivers which use the reservation object to manage memory dynamically
77 * also use this lock to protect buffer object state like placement,
78 * allocation policies or throughout command submission.
79 */
786d7257 80 struct ww_mutex lock;
d9edf92d
DV
81
82 /**
83 * @seq:
84 *
85 * Sequence count for managing RCU read-side synchronization, allows
86 * read-only access to @fence_excl and @fence while ensuring we take a
87 * consistent snapshot.
88 */
cd29f220 89 seqcount_ww_mutex_t seq;
0ba6b8fb 90
d9edf92d
DV
91 /**
92 * @fence_excl:
93 *
94 * The exclusive fence, if there is one currently.
95 *
15325e3c
CK
96 * To guarantee that no fences are lost, this new fence must signal
97 * only after the previous exclusive fence has signalled. If
98 * semantically only a new access is added without actually treating the
99 * previous one as a dependency the exclusive fences can be strung
100 * together using struct dma_fence_chain.
d9edf92d
DV
101 *
102 * Note that actual semantics of what an exclusive or shared fence mean
103 * is defined by the user, for reservation objects shared across drivers
104 * see &dma_buf.resv.
105 */
f54d1867 106 struct dma_fence __rcu *fence_excl;
d9edf92d
DV
107
108 /**
109 * @fence:
110 *
111 * List of current shared fences.
112 *
113 * There are no ordering constraints of shared fences against the
114 * exclusive fence slot. If a waiter needs to wait for all access, it
115 * has to wait for both sets of fences to signal.
116 *
117 * A new fence is added by calling dma_resv_add_shared_fence(). Since
118 * this often needs to be done past the point of no return in command
119 * submission it cannot fail, and therefore sufficient slots need to be
120 * reserved by calling dma_resv_reserve_shared().
121 *
122 * Note that actual semantics of what an exclusive or shared fence mean
123 * is defined by the user, for reservation objects shared across drivers
124 * see &dma_buf.resv.
125 */
52791eee 126 struct dma_resv_list __rcu *fence;
786d7257
ML
127};
128
c921ff37
CK
129/**
130 * struct dma_resv_iter - current position into the dma_resv fences
131 *
132 * Don't touch this directly in the driver, use the accessor function instead.
d80976d9
DV
133 *
134 * IMPORTANT
135 *
136 * When using the lockless iterators like dma_resv_iter_next_unlocked() or
137 * dma_resv_for_each_fence_unlocked() beware that the iterator can be restarted.
138 * Code which accumulates statistics or similar needs to check for this with
139 * dma_resv_iter_is_restarted().
c921ff37
CK
140 */
141struct dma_resv_iter {
142 /** @obj: The dma_resv object we iterate over */
143 struct dma_resv *obj;
144
145 /** @all_fences: If all fences should be returned */
146 bool all_fences;
147
148 /** @fence: the currently handled fence */
149 struct dma_fence *fence;
150
151 /** @seq: sequence number to check for modifications */
152 unsigned int seq;
153
154 /** @index: index into the shared fences */
155 unsigned int index;
156
5e51cc00 157 /** @fences: the shared fences; private, *MUST* not dereference */
c921ff37
CK
158 struct dma_resv_list *fences;
159
5e51cc00
TU
160 /** @shared_count: number of shared fences */
161 unsigned int shared_count;
162
c921ff37
CK
163 /** @is_restarted: true if this is the first returned fence */
164 bool is_restarted;
165};
166
167struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor);
168struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor);
5baaac31
CK
169struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor);
170struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor);
c921ff37
CK
171
172/**
173 * dma_resv_iter_begin - initialize a dma_resv_iter object
174 * @cursor: The dma_resv_iter object to initialize
175 * @obj: The dma_resv object which we want to iterate over
176 * @all_fences: If all fences should be returned or just the exclusive one
177 */
178static inline void dma_resv_iter_begin(struct dma_resv_iter *cursor,
179 struct dma_resv *obj,
180 bool all_fences)
181{
182 cursor->obj = obj;
183 cursor->all_fences = all_fences;
184 cursor->fence = NULL;
185}
186
187/**
188 * dma_resv_iter_end - cleanup a dma_resv_iter object
189 * @cursor: the dma_resv_iter object which should be cleaned up
190 *
191 * Make sure that the reference to the fence in the cursor is properly
192 * dropped.
193 */
194static inline void dma_resv_iter_end(struct dma_resv_iter *cursor)
195{
196 dma_fence_put(cursor->fence);
197}
198
199/**
200 * dma_resv_iter_is_exclusive - test if the current fence is the exclusive one
201 * @cursor: the cursor of the current position
202 *
203 * Returns true if the currently returned fence is the exclusive one.
204 */
205static inline bool dma_resv_iter_is_exclusive(struct dma_resv_iter *cursor)
206{
207 return cursor->index == 0;
208}
209
210/**
211 * dma_resv_iter_is_restarted - test if this is the first fence after a restart
212 * @cursor: the cursor with the current position
213 *
214 * Return true if this is the first fence in an iteration after a restart.
215 */
216static inline bool dma_resv_iter_is_restarted(struct dma_resv_iter *cursor)
217{
218 return cursor->is_restarted;
219}
220
221/**
222 * dma_resv_for_each_fence_unlocked - unlocked fence iterator
223 * @cursor: a struct dma_resv_iter pointer
224 * @fence: the current fence
225 *
226 * Iterate over the fences in a struct dma_resv object without holding the
227 * &dma_resv.lock and using RCU instead. The cursor needs to be initialized
228 * with dma_resv_iter_begin() and cleaned up with dma_resv_iter_end(). Inside
229 * the iterator a reference to the dma_fence is held and the RCU lock dropped.
d80976d9
DV
230 *
231 * Beware that the iterator can be restarted when the struct dma_resv for
232 * @cursor is modified. Code which accumulates statistics or similar needs to
233 * check for this with dma_resv_iter_is_restarted(). For this reason prefer the
234 * lock iterator dma_resv_for_each_fence() whenever possible.
c921ff37
CK
235 */
236#define dma_resv_for_each_fence_unlocked(cursor, fence) \
237 for (fence = dma_resv_iter_first_unlocked(cursor); \
238 fence; fence = dma_resv_iter_next_unlocked(cursor))
239
5baaac31
CK
240/**
241 * dma_resv_for_each_fence - fence iterator
242 * @cursor: a struct dma_resv_iter pointer
243 * @obj: a dma_resv object pointer
244 * @all_fences: true if all fences should be returned
245 * @fence: the current fence
246 *
247 * Iterate over the fences in a struct dma_resv object while holding the
248 * &dma_resv.lock. @all_fences controls if the shared fences are returned as
249 * well. The cursor initialisation is part of the iterator and the fence stays
250 * valid as long as the lock is held and so no extra reference to the fence is
251 * taken.
252 */
253#define dma_resv_for_each_fence(cursor, obj, all_fences, fence) \
254 for (dma_resv_iter_begin(cursor, obj, all_fences), \
255 fence = dma_resv_iter_first(cursor); fence; \
256 fence = dma_resv_iter_next(cursor))
257
52791eee
CK
258#define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
259#define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
04a5faa8 260
0c6b522a
CK
261#ifdef CONFIG_DEBUG_MUTEXES
262void dma_resv_reset_shared_max(struct dma_resv *obj);
263#else
264static inline void dma_resv_reset_shared_max(struct dma_resv *obj) {}
265#endif
266
122020af 267/**
52791eee 268 * dma_resv_lock - lock the reservation object
122020af
CW
269 * @obj: the reservation object
270 * @ctx: the locking context
271 *
272 * Locks the reservation object for exclusive access and modification. Note,
273 * that the lock is only against other writers, readers will run concurrently
274 * with a writer under RCU. The seqlock is used to notify readers if they
275 * overlap with a writer.
276 *
277 * As the reservation object may be locked by multiple parties in an
278 * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
279 * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
280 * object may be locked by itself by passing NULL as @ctx.
d9edf92d
DV
281 *
282 * When a die situation is indicated by returning -EDEADLK all locks held by
283 * @ctx must be unlocked and then dma_resv_lock_slow() called on @obj.
284 *
285 * Unlocked by calling dma_resv_unlock().
286 *
287 * See also dma_resv_lock_interruptible() for the interruptible variant.
122020af 288 */
52791eee
CK
289static inline int dma_resv_lock(struct dma_resv *obj,
290 struct ww_acquire_ctx *ctx)
122020af
CW
291{
292 return ww_mutex_lock(&obj->lock, ctx);
293}
294
5d276a1a 295/**
52791eee 296 * dma_resv_lock_interruptible - lock the reservation object
5d276a1a
CK
297 * @obj: the reservation object
298 * @ctx: the locking context
299 *
300 * Locks the reservation object interruptible for exclusive access and
301 * modification. Note, that the lock is only against other writers, readers
302 * will run concurrently with a writer under RCU. The seqlock is used to
303 * notify readers if they overlap with a writer.
304 *
305 * As the reservation object may be locked by multiple parties in an
306 * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
307 * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
308 * object may be locked by itself by passing NULL as @ctx.
d9edf92d
DV
309 *
310 * When a die situation is indicated by returning -EDEADLK all locks held by
311 * @ctx must be unlocked and then dma_resv_lock_slow_interruptible() called on
312 * @obj.
313 *
314 * Unlocked by calling dma_resv_unlock().
5d276a1a 315 */
52791eee
CK
316static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
317 struct ww_acquire_ctx *ctx)
5d276a1a
CK
318{
319 return ww_mutex_lock_interruptible(&obj->lock, ctx);
320}
321
0dbd555a 322/**
52791eee 323 * dma_resv_lock_slow - slowpath lock the reservation object
0dbd555a
CK
324 * @obj: the reservation object
325 * @ctx: the locking context
326 *
327 * Acquires the reservation object after a die case. This function
52791eee 328 * will sleep until the lock becomes available. See dma_resv_lock() as
0dbd555a 329 * well.
d9edf92d
DV
330 *
331 * See also dma_resv_lock_slow_interruptible() for the interruptible variant.
0dbd555a 332 */
52791eee
CK
333static inline void dma_resv_lock_slow(struct dma_resv *obj,
334 struct ww_acquire_ctx *ctx)
0dbd555a
CK
335{
336 ww_mutex_lock_slow(&obj->lock, ctx);
337}
338
339/**
52791eee 340 * dma_resv_lock_slow_interruptible - slowpath lock the reservation
0dbd555a
CK
341 * object, interruptible
342 * @obj: the reservation object
343 * @ctx: the locking context
344 *
345 * Acquires the reservation object interruptible after a die case. This function
346 * will sleep until the lock becomes available. See
52791eee 347 * dma_resv_lock_interruptible() as well.
0dbd555a 348 */
52791eee
CK
349static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
350 struct ww_acquire_ctx *ctx)
0dbd555a
CK
351{
352 return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
353}
5d276a1a 354
2955b73d 355/**
52791eee 356 * dma_resv_trylock - trylock the reservation object
2955b73d
CW
357 * @obj: the reservation object
358 *
359 * Tries to lock the reservation object for exclusive access and modification.
360 * Note, that the lock is only against other writers, readers will run
361 * concurrently with a writer under RCU. The seqlock is used to notify readers
362 * if they overlap with a writer.
363 *
364 * Also note that since no context is provided, no deadlock protection is
d9edf92d 365 * possible, which is also not needed for a trylock.
2955b73d
CW
366 *
367 * Returns true if the lock was acquired, false otherwise.
368 */
52791eee 369static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
2955b73d 370{
12235da8 371 return ww_mutex_trylock(&obj->lock, NULL);
2955b73d
CW
372}
373
0dbd555a 374/**
52791eee 375 * dma_resv_is_locked - is the reservation object locked
0dbd555a
CK
376 * @obj: the reservation object
377 *
378 * Returns true if the mutex is locked, false if unlocked.
379 */
52791eee 380static inline bool dma_resv_is_locked(struct dma_resv *obj)
0dbd555a
CK
381{
382 return ww_mutex_is_locked(&obj->lock);
383}
384
385/**
52791eee 386 * dma_resv_locking_ctx - returns the context used to lock the object
0dbd555a
CK
387 * @obj: the reservation object
388 *
389 * Returns the context used to lock a reservation object or NULL if no context
390 * was used or the object is not locked at all.
d9edf92d
DV
391 *
392 * WARNING: This interface is pretty horrible, but TTM needs it because it
393 * doesn't pass the struct ww_acquire_ctx around in some very long callchains.
394 * Everyone else just uses it to check whether they're holding a reservation or
395 * not.
0dbd555a 396 */
52791eee 397static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
0dbd555a
CK
398{
399 return READ_ONCE(obj->lock.ctx);
400}
401
122020af 402/**
52791eee 403 * dma_resv_unlock - unlock the reservation object
122020af
CW
404 * @obj: the reservation object
405 *
406 * Unlocks the reservation object following exclusive access.
407 */
52791eee 408static inline void dma_resv_unlock(struct dma_resv *obj)
122020af 409{
0c6b522a 410 dma_resv_reset_shared_max(obj);
122020af
CW
411 ww_mutex_unlock(&obj->lock);
412}
413
b016cd6e 414/**
82c850c1 415 * dma_resv_excl_fence - return the object's exclusive fence
b016cd6e
CW
416 * @obj: the reservation object
417 *
6edbd6ab
CK
418 * Returns the exclusive fence (if any). Caller must either hold the objects
419 * through dma_resv_lock() or the RCU read side lock through rcu_read_lock(),
420 * or one of the variants of each
b016cd6e
CW
421 *
422 * RETURNS
423 * The exclusive fence or NULL
424 */
425static inline struct dma_fence *
6edbd6ab 426dma_resv_excl_fence(struct dma_resv *obj)
b016cd6e 427{
6edbd6ab 428 return rcu_dereference_check(obj->fence_excl, dma_resv_held(obj));
b016cd6e
CW
429}
430
52791eee
CK
431void dma_resv_init(struct dma_resv *obj);
432void dma_resv_fini(struct dma_resv *obj);
433int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
434void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
548e7432
CK
435void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
436 struct dma_fence *fence);
52791eee 437void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
75ab2b36
CK
438int dma_resv_get_fences(struct dma_resv *obj, bool write,
439 unsigned int *num_fences, struct dma_fence ***fences);
52791eee 440int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
d3fae3b3
CK
441long dma_resv_wait_timeout(struct dma_resv *obj, bool wait_all, bool intr,
442 unsigned long timeout);
443bool dma_resv_test_signaled(struct dma_resv *obj, bool test_all);
a25efb38 444void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq);
3c3b177a 445
786d7257 446#endif /* _LINUX_RESERVATION_H */