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