xref: /openbmc/linux/include/linux/dma-resv.h (revision d7d5a21d)
152791eeeSChristian König /*
252791eeeSChristian König  * Header file for reservations for dma-buf and ttm
352791eeeSChristian König  *
452791eeeSChristian König  * Copyright(C) 2011 Linaro Limited. All rights reserved.
552791eeeSChristian König  * Copyright (C) 2012-2013 Canonical Ltd
652791eeeSChristian König  * Copyright (C) 2012 Texas Instruments
752791eeeSChristian König  *
852791eeeSChristian König  * Authors:
952791eeeSChristian König  * Rob Clark <robdclark@gmail.com>
1052791eeeSChristian König  * Maarten Lankhorst <maarten.lankhorst@canonical.com>
1152791eeeSChristian König  * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
1252791eeeSChristian König  *
1352791eeeSChristian König  * Based on bo.c which bears the following copyright notice,
1452791eeeSChristian König  * but is dual licensed:
1552791eeeSChristian König  *
1652791eeeSChristian König  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
1752791eeeSChristian König  * All Rights Reserved.
1852791eeeSChristian König  *
1952791eeeSChristian König  * Permission is hereby granted, free of charge, to any person obtaining a
2052791eeeSChristian König  * copy of this software and associated documentation files (the
2152791eeeSChristian König  * "Software"), to deal in the Software without restriction, including
2252791eeeSChristian König  * without limitation the rights to use, copy, modify, merge, publish,
2352791eeeSChristian König  * distribute, sub license, and/or sell copies of the Software, and to
2452791eeeSChristian König  * permit persons to whom the Software is furnished to do so, subject to
2552791eeeSChristian König  * the following conditions:
2652791eeeSChristian König  *
2752791eeeSChristian König  * The above copyright notice and this permission notice (including the
2852791eeeSChristian König  * next paragraph) shall be included in all copies or substantial portions
2952791eeeSChristian König  * of the Software.
3052791eeeSChristian König  *
3152791eeeSChristian König  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3252791eeeSChristian König  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3352791eeeSChristian König  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
3452791eeeSChristian König  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
3552791eeeSChristian König  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
3652791eeeSChristian König  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
3752791eeeSChristian König  * USE OR OTHER DEALINGS IN THE SOFTWARE.
3852791eeeSChristian König  */
3952791eeeSChristian König #ifndef _LINUX_RESERVATION_H
4052791eeeSChristian König #define _LINUX_RESERVATION_H
4152791eeeSChristian König 
4252791eeeSChristian König #include <linux/ww_mutex.h>
4352791eeeSChristian König #include <linux/dma-fence.h>
4452791eeeSChristian König #include <linux/slab.h>
4552791eeeSChristian König #include <linux/seqlock.h>
4652791eeeSChristian König #include <linux/rcupdate.h>
4752791eeeSChristian König 
4852791eeeSChristian König extern struct ww_class reservation_ww_class;
4952791eeeSChristian König 
508938d484SChristian König struct dma_resv_list;
5152791eeeSChristian König 
5252791eeeSChristian König /**
537bc80a54SChristian König  * enum dma_resv_usage - how the fences from a dma_resv obj are used
547bc80a54SChristian König  *
557bc80a54SChristian König  * This enum describes the different use cases for a dma_resv object and
567bc80a54SChristian König  * controls which fences are returned when queried.
577bc80a54SChristian König  *
580cc848a7SChristian König  * An important fact is that there is the order KERNEL<WRITE<READ<BOOKKEEP and
59b29895e1SChristian König  * when the dma_resv object is asked for fences for one use case the fences
60b29895e1SChristian König  * for the lower use case are returned as well.
61b29895e1SChristian König  *
62b29895e1SChristian König  * For example when asking for WRITE fences then the KERNEL fences are returned
63b29895e1SChristian König  * as well. Similar when asked for READ fences then both WRITE and KERNEL
64b29895e1SChristian König  * fences are returned as well.
650b8613a2SChristian König  *
660b8613a2SChristian König  * Already used fences can be promoted in the sense that a fence with
670b8613a2SChristian König  * DMA_RESV_USAGE_BOOKKEEP could become DMA_RESV_USAGE_READ by adding it again
680b8613a2SChristian König  * with this usage. But fences can never be degraded in the sense that a fence
690b8613a2SChristian König  * with DMA_RESV_USAGE_WRITE could become DMA_RESV_USAGE_READ.
707bc80a54SChristian König  */
717bc80a54SChristian König enum dma_resv_usage {
727bc80a54SChristian König 	/**
73b29895e1SChristian König 	 * @DMA_RESV_USAGE_KERNEL: For in kernel memory management only.
74b29895e1SChristian König 	 *
75b29895e1SChristian König 	 * This should only be used for things like copying or clearing memory
76b29895e1SChristian König 	 * with a DMA hardware engine for the purpose of kernel memory
77b29895e1SChristian König 	 * management.
78b29895e1SChristian König 	 *
79b29895e1SChristian König 	 * Drivers *always* must wait for those fences before accessing the
80b29895e1SChristian König 	 * resource protected by the dma_resv object. The only exception for
81b29895e1SChristian König 	 * that is when the resource is known to be locked down in place by
82b29895e1SChristian König 	 * pinning it previously.
83b29895e1SChristian König 	 */
84b29895e1SChristian König 	DMA_RESV_USAGE_KERNEL,
85b29895e1SChristian König 
86b29895e1SChristian König 	/**
877bc80a54SChristian König 	 * @DMA_RESV_USAGE_WRITE: Implicit write synchronization.
887bc80a54SChristian König 	 *
897bc80a54SChristian König 	 * This should only be used for userspace command submissions which add
907bc80a54SChristian König 	 * an implicit write dependency.
917bc80a54SChristian König 	 */
927bc80a54SChristian König 	DMA_RESV_USAGE_WRITE,
937bc80a54SChristian König 
947bc80a54SChristian König 	/**
957bc80a54SChristian König 	 * @DMA_RESV_USAGE_READ: Implicit read synchronization.
967bc80a54SChristian König 	 *
977bc80a54SChristian König 	 * This should only be used for userspace command submissions which add
987bc80a54SChristian König 	 * an implicit read dependency.
997bc80a54SChristian König 	 */
1007bc80a54SChristian König 	DMA_RESV_USAGE_READ,
1010cc848a7SChristian König 
1020cc848a7SChristian König 	/**
1030cc848a7SChristian König 	 * @DMA_RESV_USAGE_BOOKKEEP: No implicit sync.
1040cc848a7SChristian König 	 *
1050cc848a7SChristian König 	 * This should be used by submissions which don't want to participate in
1060b8613a2SChristian König 	 * any implicit synchronization.
1070cc848a7SChristian König 	 *
1080b8613a2SChristian König 	 * The most common case are preemption fences, page table updates, TLB
1090b8613a2SChristian König 	 * flushes as well as explicit synced user submissions.
1100b8613a2SChristian König 	 *
1110b8613a2SChristian König 	 * Explicit synced user user submissions can be promoted to
1120b8613a2SChristian König 	 * DMA_RESV_USAGE_READ or DMA_RESV_USAGE_WRITE as needed using
1130b8613a2SChristian König 	 * dma_buf_import_sync_file() when implicit synchronization should
1140b8613a2SChristian König 	 * become necessary after initial adding of the fence.
1150cc848a7SChristian König 	 */
1160cc848a7SChristian König 	DMA_RESV_USAGE_BOOKKEEP
1177bc80a54SChristian König };
1187bc80a54SChristian König 
1197bc80a54SChristian König /**
1207bc80a54SChristian König  * dma_resv_usage_rw - helper for implicit sync
1217bc80a54SChristian König  * @write: true if we create a new implicit sync write
1227bc80a54SChristian König  *
1237bc80a54SChristian König  * This returns the implicit synchronization usage for write or read accesses,
1247bc80a54SChristian König  * see enum dma_resv_usage and &dma_buf.resv.
1257bc80a54SChristian König  */
dma_resv_usage_rw(bool write)1267bc80a54SChristian König static inline enum dma_resv_usage dma_resv_usage_rw(bool write)
1277bc80a54SChristian König {
1287bc80a54SChristian König 	/* This looks confusing at first sight, but is indeed correct.
1297bc80a54SChristian König 	 *
1307bc80a54SChristian König 	 * The rational is that new write operations needs to wait for the
1317bc80a54SChristian König 	 * existing read and write operations to finish.
1327bc80a54SChristian König 	 * But a new read operation only needs to wait for the existing write
1337bc80a54SChristian König 	 * operations to finish.
1347bc80a54SChristian König 	 */
1357bc80a54SChristian König 	return write ? DMA_RESV_USAGE_READ : DMA_RESV_USAGE_WRITE;
1367bc80a54SChristian König }
1377bc80a54SChristian König 
1387bc80a54SChristian König /**
13952791eeeSChristian König  * struct dma_resv - a reservation object manages fences for a buffer
140d9edf92dSDaniel Vetter  *
141047a1b87SChristian König  * This is a container for dma_fence objects which needs to handle multiple use
142047a1b87SChristian König  * cases.
143d9edf92dSDaniel Vetter  *
144d9edf92dSDaniel Vetter  * One use is to synchronize cross-driver access to a struct dma_buf, either for
145d9edf92dSDaniel Vetter  * dynamic buffer management or just to handle implicit synchronization between
146d9edf92dSDaniel Vetter  * different users of the buffer in userspace. See &dma_buf.resv for a more
147d9edf92dSDaniel Vetter  * in-depth discussion.
148d9edf92dSDaniel Vetter  *
149d9edf92dSDaniel Vetter  * The other major use is to manage access and locking within a driver in a
150d9edf92dSDaniel Vetter  * buffer based memory manager. struct ttm_buffer_object is the canonical
151d9edf92dSDaniel Vetter  * example here, since this is where reservation objects originated from. But
152d9edf92dSDaniel Vetter  * use in drivers is spreading and some drivers also manage struct
153d9edf92dSDaniel Vetter  * drm_gem_object with the same scheme.
15452791eeeSChristian König  */
15552791eeeSChristian König struct dma_resv {
156d9edf92dSDaniel Vetter 	/**
157d9edf92dSDaniel Vetter 	 * @lock:
158d9edf92dSDaniel Vetter 	 *
159d9edf92dSDaniel Vetter 	 * Update side lock. Don't use directly, instead use the wrapper
160d9edf92dSDaniel Vetter 	 * functions like dma_resv_lock() and dma_resv_unlock().
161d9edf92dSDaniel Vetter 	 *
162d9edf92dSDaniel Vetter 	 * Drivers which use the reservation object to manage memory dynamically
163d9edf92dSDaniel Vetter 	 * also use this lock to protect buffer object state like placement,
164d9edf92dSDaniel Vetter 	 * allocation policies or throughout command submission.
165d9edf92dSDaniel Vetter 	 */
16652791eeeSChristian König 	struct ww_mutex lock;
167d9edf92dSDaniel Vetter 
168d9edf92dSDaniel Vetter 	/**
169047a1b87SChristian König 	 * @fences:
170d9edf92dSDaniel Vetter 	 *
171047a1b87SChristian König 	 * Array of fences which where added to the dma_resv object
172d9edf92dSDaniel Vetter 	 *
173047a1b87SChristian König 	 * A new fence is added by calling dma_resv_add_fence(). Since this
174047a1b87SChristian König 	 * often needs to be done past the point of no return in command
175d9edf92dSDaniel Vetter 	 * submission it cannot fail, and therefore sufficient slots need to be
176c8d4c18bSChristian König 	 * reserved by calling dma_resv_reserve_fences().
177d9edf92dSDaniel Vetter 	 */
178047a1b87SChristian König 	struct dma_resv_list __rcu *fences;
17952791eeeSChristian König };
18052791eeeSChristian König 
181c921ff37SChristian König /**
182c921ff37SChristian König  * struct dma_resv_iter - current position into the dma_resv fences
183c921ff37SChristian König  *
184c921ff37SChristian König  * Don't touch this directly in the driver, use the accessor function instead.
185d80976d9SDaniel Vetter  *
186d80976d9SDaniel Vetter  * IMPORTANT
187d80976d9SDaniel Vetter  *
188d80976d9SDaniel Vetter  * When using the lockless iterators like dma_resv_iter_next_unlocked() or
189d80976d9SDaniel Vetter  * dma_resv_for_each_fence_unlocked() beware that the iterator can be restarted.
190d80976d9SDaniel Vetter  * Code which accumulates statistics or similar needs to check for this with
191d80976d9SDaniel Vetter  * dma_resv_iter_is_restarted().
192c921ff37SChristian König  */
193c921ff37SChristian König struct dma_resv_iter {
194c921ff37SChristian König 	/** @obj: The dma_resv object we iterate over */
195c921ff37SChristian König 	struct dma_resv *obj;
196c921ff37SChristian König 
1977bc80a54SChristian König 	/** @usage: Return fences with this usage or lower. */
1987bc80a54SChristian König 	enum dma_resv_usage usage;
199c921ff37SChristian König 
200c921ff37SChristian König 	/** @fence: the currently handled fence */
201c921ff37SChristian König 	struct dma_fence *fence;
202c921ff37SChristian König 
20373511edfSChristian König 	/** @fence_usage: the usage of the current fence */
20473511edfSChristian König 	enum dma_resv_usage fence_usage;
20573511edfSChristian König 
206c921ff37SChristian König 	/** @index: index into the shared fences */
207c921ff37SChristian König 	unsigned int index;
208c921ff37SChristian König 
2095e51cc00STvrtko Ursulin 	/** @fences: the shared fences; private, *MUST* not dereference  */
210c921ff37SChristian König 	struct dma_resv_list *fences;
211c921ff37SChristian König 
212047a1b87SChristian König 	/** @num_fences: number of fences */
213047a1b87SChristian König 	unsigned int num_fences;
2145e51cc00STvrtko Ursulin 
215c921ff37SChristian König 	/** @is_restarted: true if this is the first returned fence */
216c921ff37SChristian König 	bool is_restarted;
217c921ff37SChristian König };
218c921ff37SChristian König 
219c921ff37SChristian König struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor);
220c921ff37SChristian König struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor);
2215baaac31SChristian König struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor);
2225baaac31SChristian König struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor);
223c921ff37SChristian König 
224c921ff37SChristian König /**
225c921ff37SChristian König  * dma_resv_iter_begin - initialize a dma_resv_iter object
226c921ff37SChristian König  * @cursor: The dma_resv_iter object to initialize
227c921ff37SChristian König  * @obj: The dma_resv object which we want to iterate over
2287bc80a54SChristian König  * @usage: controls which fences to include, see enum dma_resv_usage.
229c921ff37SChristian König  */
dma_resv_iter_begin(struct dma_resv_iter * cursor,struct dma_resv * obj,enum dma_resv_usage usage)230c921ff37SChristian König static inline void dma_resv_iter_begin(struct dma_resv_iter *cursor,
231c921ff37SChristian König 				       struct dma_resv *obj,
2327bc80a54SChristian König 				       enum dma_resv_usage usage)
233c921ff37SChristian König {
234c921ff37SChristian König 	cursor->obj = obj;
2357bc80a54SChristian König 	cursor->usage = usage;
236c921ff37SChristian König 	cursor->fence = NULL;
237c921ff37SChristian König }
238c921ff37SChristian König 
239c921ff37SChristian König /**
240c921ff37SChristian König  * dma_resv_iter_end - cleanup a dma_resv_iter object
241c921ff37SChristian König  * @cursor: the dma_resv_iter object which should be cleaned up
242c921ff37SChristian König  *
243c921ff37SChristian König  * Make sure that the reference to the fence in the cursor is properly
244c921ff37SChristian König  * dropped.
245c921ff37SChristian König  */
dma_resv_iter_end(struct dma_resv_iter * cursor)246c921ff37SChristian König static inline void dma_resv_iter_end(struct dma_resv_iter *cursor)
247c921ff37SChristian König {
248c921ff37SChristian König 	dma_fence_put(cursor->fence);
249c921ff37SChristian König }
250c921ff37SChristian König 
251c921ff37SChristian König /**
25273511edfSChristian König  * dma_resv_iter_usage - Return the usage of the current fence
253c921ff37SChristian König  * @cursor: the cursor of the current position
254c921ff37SChristian König  *
25573511edfSChristian König  * Returns the usage of the currently processed fence.
256c921ff37SChristian König  */
25773511edfSChristian König static inline enum dma_resv_usage
dma_resv_iter_usage(struct dma_resv_iter * cursor)25873511edfSChristian König dma_resv_iter_usage(struct dma_resv_iter *cursor)
259c921ff37SChristian König {
26073511edfSChristian König 	return cursor->fence_usage;
261c921ff37SChristian König }
262c921ff37SChristian König 
263c921ff37SChristian König /**
264c921ff37SChristian König  * dma_resv_iter_is_restarted - test if this is the first fence after a restart
265c921ff37SChristian König  * @cursor: the cursor with the current position
266c921ff37SChristian König  *
267c921ff37SChristian König  * Return true if this is the first fence in an iteration after a restart.
268c921ff37SChristian König  */
dma_resv_iter_is_restarted(struct dma_resv_iter * cursor)269c921ff37SChristian König static inline bool dma_resv_iter_is_restarted(struct dma_resv_iter *cursor)
270c921ff37SChristian König {
271c921ff37SChristian König 	return cursor->is_restarted;
272c921ff37SChristian König }
273c921ff37SChristian König 
274c921ff37SChristian König /**
275c921ff37SChristian König  * dma_resv_for_each_fence_unlocked - unlocked fence iterator
276c921ff37SChristian König  * @cursor: a struct dma_resv_iter pointer
277c921ff37SChristian König  * @fence: the current fence
278c921ff37SChristian König  *
279c921ff37SChristian König  * Iterate over the fences in a struct dma_resv object without holding the
280c921ff37SChristian König  * &dma_resv.lock and using RCU instead. The cursor needs to be initialized
281c921ff37SChristian König  * with dma_resv_iter_begin() and cleaned up with dma_resv_iter_end(). Inside
282c921ff37SChristian König  * the iterator a reference to the dma_fence is held and the RCU lock dropped.
283d80976d9SDaniel Vetter  *
284d80976d9SDaniel Vetter  * Beware that the iterator can be restarted when the struct dma_resv for
285d80976d9SDaniel Vetter  * @cursor is modified. Code which accumulates statistics or similar needs to
286d80976d9SDaniel Vetter  * check for this with dma_resv_iter_is_restarted(). For this reason prefer the
287d80976d9SDaniel Vetter  * lock iterator dma_resv_for_each_fence() whenever possible.
288c921ff37SChristian König  */
289c921ff37SChristian König #define dma_resv_for_each_fence_unlocked(cursor, fence)			\
290c921ff37SChristian König 	for (fence = dma_resv_iter_first_unlocked(cursor);		\
291c921ff37SChristian König 	     fence; fence = dma_resv_iter_next_unlocked(cursor))
292c921ff37SChristian König 
2935baaac31SChristian König /**
2945baaac31SChristian König  * dma_resv_for_each_fence - fence iterator
2955baaac31SChristian König  * @cursor: a struct dma_resv_iter pointer
2965baaac31SChristian König  * @obj: a dma_resv object pointer
2977bc80a54SChristian König  * @usage: controls which fences to return
2985baaac31SChristian König  * @fence: the current fence
2995baaac31SChristian König  *
3005baaac31SChristian König  * Iterate over the fences in a struct dma_resv object while holding the
3015baaac31SChristian König  * &dma_resv.lock. @all_fences controls if the shared fences are returned as
3025baaac31SChristian König  * well. The cursor initialisation is part of the iterator and the fence stays
3035baaac31SChristian König  * valid as long as the lock is held and so no extra reference to the fence is
3045baaac31SChristian König  * taken.
3055baaac31SChristian König  */
3067bc80a54SChristian König #define dma_resv_for_each_fence(cursor, obj, usage, fence)	\
3077bc80a54SChristian König 	for (dma_resv_iter_begin(cursor, obj, usage),	\
3085baaac31SChristian König 	     fence = dma_resv_iter_first(cursor); fence;	\
3095baaac31SChristian König 	     fence = dma_resv_iter_next(cursor))
3105baaac31SChristian König 
31152791eeeSChristian König #define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
31252791eeeSChristian König #define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
31352791eeeSChristian König 
3140c6b522aSChristian König #ifdef CONFIG_DEBUG_MUTEXES
31573511edfSChristian König void dma_resv_reset_max_fences(struct dma_resv *obj);
3160c6b522aSChristian König #else
dma_resv_reset_max_fences(struct dma_resv * obj)31773511edfSChristian König static inline void dma_resv_reset_max_fences(struct dma_resv *obj) {}
3180c6b522aSChristian König #endif
3190c6b522aSChristian König 
32052791eeeSChristian König /**
32152791eeeSChristian König  * dma_resv_lock - lock the reservation object
32252791eeeSChristian König  * @obj: the reservation object
32352791eeeSChristian König  * @ctx: the locking context
32452791eeeSChristian König  *
32552791eeeSChristian König  * Locks the reservation object for exclusive access and modification. Note,
32652791eeeSChristian König  * that the lock is only against other writers, readers will run concurrently
32752791eeeSChristian König  * with a writer under RCU. The seqlock is used to notify readers if they
32852791eeeSChristian König  * overlap with a writer.
32952791eeeSChristian König  *
33052791eeeSChristian König  * As the reservation object may be locked by multiple parties in an
33152791eeeSChristian König  * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
33252791eeeSChristian König  * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
33352791eeeSChristian König  * object may be locked by itself by passing NULL as @ctx.
334d9edf92dSDaniel Vetter  *
335d9edf92dSDaniel Vetter  * When a die situation is indicated by returning -EDEADLK all locks held by
336d9edf92dSDaniel Vetter  * @ctx must be unlocked and then dma_resv_lock_slow() called on @obj.
337d9edf92dSDaniel Vetter  *
338d9edf92dSDaniel Vetter  * Unlocked by calling dma_resv_unlock().
339d9edf92dSDaniel Vetter  *
340d9edf92dSDaniel Vetter  * See also dma_resv_lock_interruptible() for the interruptible variant.
34152791eeeSChristian König  */
dma_resv_lock(struct dma_resv * obj,struct ww_acquire_ctx * ctx)34252791eeeSChristian König static inline int dma_resv_lock(struct dma_resv *obj,
34352791eeeSChristian König 				struct ww_acquire_ctx *ctx)
34452791eeeSChristian König {
34552791eeeSChristian König 	return ww_mutex_lock(&obj->lock, ctx);
34652791eeeSChristian König }
34752791eeeSChristian König 
34852791eeeSChristian König /**
34952791eeeSChristian König  * dma_resv_lock_interruptible - lock the reservation object
35052791eeeSChristian König  * @obj: the reservation object
35152791eeeSChristian König  * @ctx: the locking context
35252791eeeSChristian König  *
35352791eeeSChristian König  * Locks the reservation object interruptible for exclusive access and
35452791eeeSChristian König  * modification. Note, that the lock is only against other writers, readers
35552791eeeSChristian König  * will run concurrently with a writer under RCU. The seqlock is used to
35652791eeeSChristian König  * notify readers if they overlap with a writer.
35752791eeeSChristian König  *
35852791eeeSChristian König  * As the reservation object may be locked by multiple parties in an
35952791eeeSChristian König  * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
36052791eeeSChristian König  * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
36152791eeeSChristian König  * object may be locked by itself by passing NULL as @ctx.
362d9edf92dSDaniel Vetter  *
363d9edf92dSDaniel Vetter  * When a die situation is indicated by returning -EDEADLK all locks held by
364d9edf92dSDaniel Vetter  * @ctx must be unlocked and then dma_resv_lock_slow_interruptible() called on
365d9edf92dSDaniel Vetter  * @obj.
366d9edf92dSDaniel Vetter  *
367d9edf92dSDaniel Vetter  * Unlocked by calling dma_resv_unlock().
36852791eeeSChristian König  */
dma_resv_lock_interruptible(struct dma_resv * obj,struct ww_acquire_ctx * ctx)36952791eeeSChristian König static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
37052791eeeSChristian König 					      struct ww_acquire_ctx *ctx)
37152791eeeSChristian König {
37252791eeeSChristian König 	return ww_mutex_lock_interruptible(&obj->lock, ctx);
37352791eeeSChristian König }
37452791eeeSChristian König 
37552791eeeSChristian König /**
37652791eeeSChristian König  * dma_resv_lock_slow - slowpath lock the reservation object
37752791eeeSChristian König  * @obj: the reservation object
37852791eeeSChristian König  * @ctx: the locking context
37952791eeeSChristian König  *
38052791eeeSChristian König  * Acquires the reservation object after a die case. This function
38152791eeeSChristian König  * will sleep until the lock becomes available. See dma_resv_lock() as
38252791eeeSChristian König  * well.
383d9edf92dSDaniel Vetter  *
384d9edf92dSDaniel Vetter  * See also dma_resv_lock_slow_interruptible() for the interruptible variant.
38552791eeeSChristian König  */
dma_resv_lock_slow(struct dma_resv * obj,struct ww_acquire_ctx * ctx)38652791eeeSChristian König static inline void dma_resv_lock_slow(struct dma_resv *obj,
38752791eeeSChristian König 				      struct ww_acquire_ctx *ctx)
38852791eeeSChristian König {
38952791eeeSChristian König 	ww_mutex_lock_slow(&obj->lock, ctx);
39052791eeeSChristian König }
39152791eeeSChristian König 
39252791eeeSChristian König /**
39352791eeeSChristian König  * dma_resv_lock_slow_interruptible - slowpath lock the reservation
39452791eeeSChristian König  * object, interruptible
39552791eeeSChristian König  * @obj: the reservation object
39652791eeeSChristian König  * @ctx: the locking context
39752791eeeSChristian König  *
39852791eeeSChristian König  * Acquires the reservation object interruptible after a die case. This function
39952791eeeSChristian König  * will sleep until the lock becomes available. See
40052791eeeSChristian König  * dma_resv_lock_interruptible() as well.
40152791eeeSChristian König  */
dma_resv_lock_slow_interruptible(struct dma_resv * obj,struct ww_acquire_ctx * ctx)40252791eeeSChristian König static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
40352791eeeSChristian König 						   struct ww_acquire_ctx *ctx)
40452791eeeSChristian König {
40552791eeeSChristian König 	return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
40652791eeeSChristian König }
40752791eeeSChristian König 
40852791eeeSChristian König /**
40952791eeeSChristian König  * dma_resv_trylock - trylock the reservation object
41052791eeeSChristian König  * @obj: the reservation object
41152791eeeSChristian König  *
41252791eeeSChristian König  * Tries to lock the reservation object for exclusive access and modification.
41352791eeeSChristian König  * Note, that the lock is only against other writers, readers will run
41452791eeeSChristian König  * concurrently with a writer under RCU. The seqlock is used to notify readers
41552791eeeSChristian König  * if they overlap with a writer.
41652791eeeSChristian König  *
41752791eeeSChristian König  * Also note that since no context is provided, no deadlock protection is
418d9edf92dSDaniel Vetter  * possible, which is also not needed for a trylock.
41952791eeeSChristian König  *
42052791eeeSChristian König  * Returns true if the lock was acquired, false otherwise.
42152791eeeSChristian König  */
dma_resv_trylock(struct dma_resv * obj)42252791eeeSChristian König static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
42352791eeeSChristian König {
42412235da8SMaarten Lankhorst 	return ww_mutex_trylock(&obj->lock, NULL);
42552791eeeSChristian König }
42652791eeeSChristian König 
42752791eeeSChristian König /**
42852791eeeSChristian König  * dma_resv_is_locked - is the reservation object locked
42952791eeeSChristian König  * @obj: the reservation object
43052791eeeSChristian König  *
43152791eeeSChristian König  * Returns true if the mutex is locked, false if unlocked.
43252791eeeSChristian König  */
dma_resv_is_locked(struct dma_resv * obj)43352791eeeSChristian König static inline bool dma_resv_is_locked(struct dma_resv *obj)
43452791eeeSChristian König {
43552791eeeSChristian König 	return ww_mutex_is_locked(&obj->lock);
43652791eeeSChristian König }
43752791eeeSChristian König 
43852791eeeSChristian König /**
43952791eeeSChristian König  * dma_resv_locking_ctx - returns the context used to lock the object
44052791eeeSChristian König  * @obj: the reservation object
44152791eeeSChristian König  *
44252791eeeSChristian König  * Returns the context used to lock a reservation object or NULL if no context
44352791eeeSChristian König  * was used or the object is not locked at all.
444d9edf92dSDaniel Vetter  *
445d9edf92dSDaniel Vetter  * WARNING: This interface is pretty horrible, but TTM needs it because it
446d9edf92dSDaniel Vetter  * doesn't pass the struct ww_acquire_ctx around in some very long callchains.
447d9edf92dSDaniel Vetter  * Everyone else just uses it to check whether they're holding a reservation or
448d9edf92dSDaniel Vetter  * not.
44952791eeeSChristian König  */
dma_resv_locking_ctx(struct dma_resv * obj)45052791eeeSChristian König static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
45152791eeeSChristian König {
45252791eeeSChristian König 	return READ_ONCE(obj->lock.ctx);
45352791eeeSChristian König }
45452791eeeSChristian König 
45552791eeeSChristian König /**
45652791eeeSChristian König  * dma_resv_unlock - unlock the reservation object
45752791eeeSChristian König  * @obj: the reservation object
45852791eeeSChristian König  *
45952791eeeSChristian König  * Unlocks the reservation object following exclusive access.
46052791eeeSChristian König  */
dma_resv_unlock(struct dma_resv * obj)46152791eeeSChristian König static inline void dma_resv_unlock(struct dma_resv *obj)
46252791eeeSChristian König {
46373511edfSChristian König 	dma_resv_reset_max_fences(obj);
46452791eeeSChristian König 	ww_mutex_unlock(&obj->lock);
46552791eeeSChristian König }
46652791eeeSChristian König 
46752791eeeSChristian König void dma_resv_init(struct dma_resv *obj);
46852791eeeSChristian König void dma_resv_fini(struct dma_resv *obj);
469c8d4c18bSChristian König int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences);
47073511edfSChristian König void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
47173511edfSChristian König 			enum dma_resv_usage usage);
472548e7432SChristian König void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
47373511edfSChristian König 			     struct dma_fence *fence,
47473511edfSChristian König 			     enum dma_resv_usage usage);
4757bc80a54SChristian König int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
47675ab2b36SChristian König 			unsigned int *num_fences, struct dma_fence ***fences);
4777bc80a54SChristian König int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
47892cedee6SChristian König 			   struct dma_fence **fence);
47952791eeeSChristian König int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
4807bc80a54SChristian König long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
4817bc80a54SChristian König 			   bool intr, unsigned long timeout);
482*d7d5a21dSRob Clark void dma_resv_set_deadline(struct dma_resv *obj, enum dma_resv_usage usage,
483*d7d5a21dSRob Clark 			   ktime_t deadline);
4847bc80a54SChristian König bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage);
485a25efb38SChristian König void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq);
48652791eeeSChristian König 
48752791eeeSChristian König #endif /* _LINUX_RESERVATION_H */
488