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