2013-06-27 11:48:16 +00:00
|
|
|
/*
|
|
|
|
* Header file for reservations for dma-buf and ttm
|
|
|
|
*
|
|
|
|
* Copyright(C) 2011 Linaro Limited. All rights reserved.
|
|
|
|
* Copyright (C) 2012-2013 Canonical Ltd
|
|
|
|
* Copyright (C) 2012 Texas Instruments
|
|
|
|
*
|
|
|
|
* Authors:
|
2014-07-01 10:57:37 +00:00
|
|
|
* Rob Clark <robdclark@gmail.com>
|
2013-06-27 11:48:16 +00:00
|
|
|
* Maarten Lankhorst <maarten.lankhorst@canonical.com>
|
|
|
|
* Thomas Hellstrom <thellstrom-at-vmware-dot-com>
|
|
|
|
*
|
|
|
|
* Based on bo.c which bears the following copyright notice,
|
|
|
|
* but is dual licensed:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef _LINUX_RESERVATION_H
|
|
|
|
#define _LINUX_RESERVATION_H
|
|
|
|
|
2013-07-05 07:29:32 +00:00
|
|
|
#include <linux/ww_mutex.h>
|
2016-10-25 12:00:45 +00:00
|
|
|
#include <linux/dma-fence.h>
|
2014-07-01 10:57:37 +00:00
|
|
|
#include <linux/slab.h>
|
2014-07-01 10:58:00 +00:00
|
|
|
#include <linux/seqlock.h>
|
|
|
|
#include <linux/rcupdate.h>
|
2013-06-27 11:48:16 +00:00
|
|
|
|
|
|
|
extern struct ww_class reservation_ww_class;
|
|
|
|
|
2021-09-24 12:19:22 +00:00
|
|
|
struct dma_resv_list;
|
2014-07-01 10:57:54 +00:00
|
|
|
|
2021-11-09 10:08:18 +00:00
|
|
|
/**
|
|
|
|
* enum dma_resv_usage - how the fences from a dma_resv obj are used
|
|
|
|
*
|
|
|
|
* This enum describes the different use cases for a dma_resv object and
|
|
|
|
* controls which fences are returned when queried.
|
|
|
|
*
|
2021-11-09 10:08:18 +00:00
|
|
|
* An important fact is that there is the order KERNEL<WRITE<READ<BOOKKEEP and
|
2021-11-26 13:12:42 +00:00
|
|
|
* when the dma_resv object is asked for fences for one use case the fences
|
|
|
|
* for the lower use case are returned as well.
|
|
|
|
*
|
|
|
|
* For example when asking for WRITE fences then the KERNEL fences are returned
|
|
|
|
* as well. Similar when asked for READ fences then both WRITE and KERNEL
|
|
|
|
* fences are returned as well.
|
2021-11-09 10:08:18 +00:00
|
|
|
*/
|
|
|
|
enum dma_resv_usage {
|
2021-11-26 13:12:42 +00:00
|
|
|
/**
|
|
|
|
* @DMA_RESV_USAGE_KERNEL: For in kernel memory management only.
|
|
|
|
*
|
|
|
|
* This should only be used for things like copying or clearing memory
|
|
|
|
* with a DMA hardware engine for the purpose of kernel memory
|
|
|
|
* management.
|
|
|
|
*
|
|
|
|
* Drivers *always* must wait for those fences before accessing the
|
|
|
|
* resource protected by the dma_resv object. The only exception for
|
|
|
|
* that is when the resource is known to be locked down in place by
|
|
|
|
* pinning it previously.
|
|
|
|
*/
|
|
|
|
DMA_RESV_USAGE_KERNEL,
|
|
|
|
|
2021-11-09 10:08:18 +00:00
|
|
|
/**
|
|
|
|
* @DMA_RESV_USAGE_WRITE: Implicit write synchronization.
|
|
|
|
*
|
|
|
|
* This should only be used for userspace command submissions which add
|
|
|
|
* an implicit write dependency.
|
|
|
|
*/
|
|
|
|
DMA_RESV_USAGE_WRITE,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @DMA_RESV_USAGE_READ: Implicit read synchronization.
|
|
|
|
*
|
|
|
|
* This should only be used for userspace command submissions which add
|
|
|
|
* an implicit read dependency.
|
|
|
|
*/
|
|
|
|
DMA_RESV_USAGE_READ,
|
2021-11-09 10:08:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @DMA_RESV_USAGE_BOOKKEEP: No implicit sync.
|
|
|
|
*
|
|
|
|
* This should be used by submissions which don't want to participate in
|
|
|
|
* implicit synchronization.
|
|
|
|
*
|
|
|
|
* The most common case are preemption fences as well as page table
|
|
|
|
* updates and their TLB flushes.
|
|
|
|
*/
|
|
|
|
DMA_RESV_USAGE_BOOKKEEP
|
2021-11-09 10:08:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_resv_usage_rw - helper for implicit sync
|
|
|
|
* @write: true if we create a new implicit sync write
|
|
|
|
*
|
|
|
|
* This returns the implicit synchronization usage for write or read accesses,
|
|
|
|
* see enum dma_resv_usage and &dma_buf.resv.
|
|
|
|
*/
|
|
|
|
static inline enum dma_resv_usage dma_resv_usage_rw(bool write)
|
|
|
|
{
|
|
|
|
/* This looks confusing at first sight, but is indeed correct.
|
|
|
|
*
|
|
|
|
* The rational is that new write operations needs to wait for the
|
|
|
|
* existing read and write operations to finish.
|
|
|
|
* But a new read operation only needs to wait for the existing write
|
|
|
|
* operations to finish.
|
|
|
|
*/
|
|
|
|
return write ? DMA_RESV_USAGE_READ : DMA_RESV_USAGE_WRITE;
|
|
|
|
}
|
|
|
|
|
2016-03-31 20:26:51 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* struct dma_resv - a reservation object manages fences for a buffer
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
2021-11-23 08:33:07 +00:00
|
|
|
* This is a container for dma_fence objects which needs to handle multiple use
|
|
|
|
* cases.
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
|
|
|
* One use is to synchronize cross-driver access to a struct dma_buf, either for
|
|
|
|
* dynamic buffer management or just to handle implicit synchronization between
|
|
|
|
* different users of the buffer in userspace. See &dma_buf.resv for a more
|
|
|
|
* in-depth discussion.
|
|
|
|
*
|
|
|
|
* The other major use is to manage access and locking within a driver in a
|
|
|
|
* buffer based memory manager. struct ttm_buffer_object is the canonical
|
|
|
|
* example here, since this is where reservation objects originated from. But
|
|
|
|
* use in drivers is spreading and some drivers also manage struct
|
|
|
|
* drm_gem_object with the same scheme.
|
2016-03-31 20:26:51 +00:00
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
struct dma_resv {
|
2021-08-05 10:47:05 +00:00
|
|
|
/**
|
|
|
|
* @lock:
|
|
|
|
*
|
|
|
|
* Update side lock. Don't use directly, instead use the wrapper
|
|
|
|
* functions like dma_resv_lock() and dma_resv_unlock().
|
|
|
|
*
|
|
|
|
* Drivers which use the reservation object to manage memory dynamically
|
|
|
|
* also use this lock to protect buffer object state like placement,
|
|
|
|
* allocation policies or throughout command submission.
|
|
|
|
*/
|
2013-06-27 11:48:16 +00:00
|
|
|
struct ww_mutex lock;
|
2021-08-05 10:47:05 +00:00
|
|
|
|
|
|
|
/**
|
2021-11-23 08:33:07 +00:00
|
|
|
* @fences:
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
2021-11-23 08:33:07 +00:00
|
|
|
* Array of fences which where added to the dma_resv object
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
2021-11-23 08:33:07 +00:00
|
|
|
* A new fence is added by calling dma_resv_add_fence(). Since this
|
|
|
|
* often needs to be done past the point of no return in command
|
2021-08-05 10:47:05 +00:00
|
|
|
* submission it cannot fail, and therefore sufficient slots need to be
|
2021-11-16 14:20:45 +00:00
|
|
|
* reserved by calling dma_resv_reserve_fences().
|
2021-08-05 10:47:05 +00:00
|
|
|
*/
|
2021-11-23 08:33:07 +00:00
|
|
|
struct dma_resv_list __rcu *fences;
|
2013-06-27 11:48:16 +00:00
|
|
|
};
|
|
|
|
|
2021-06-15 13:10:03 +00:00
|
|
|
/**
|
|
|
|
* struct dma_resv_iter - current position into the dma_resv fences
|
|
|
|
*
|
|
|
|
* Don't touch this directly in the driver, use the accessor function instead.
|
2021-11-30 15:27:55 +00:00
|
|
|
*
|
|
|
|
* IMPORTANT
|
|
|
|
*
|
|
|
|
* When using the lockless iterators like dma_resv_iter_next_unlocked() or
|
|
|
|
* dma_resv_for_each_fence_unlocked() beware that the iterator can be restarted.
|
|
|
|
* Code which accumulates statistics or similar needs to check for this with
|
|
|
|
* dma_resv_iter_is_restarted().
|
2021-06-15 13:10:03 +00:00
|
|
|
*/
|
|
|
|
struct dma_resv_iter {
|
|
|
|
/** @obj: The dma_resv object we iterate over */
|
|
|
|
struct dma_resv *obj;
|
|
|
|
|
2021-11-09 10:08:18 +00:00
|
|
|
/** @usage: Return fences with this usage or lower. */
|
|
|
|
enum dma_resv_usage usage;
|
2021-06-15 13:10:03 +00:00
|
|
|
|
|
|
|
/** @fence: the currently handled fence */
|
|
|
|
struct dma_fence *fence;
|
|
|
|
|
2021-11-09 10:08:18 +00:00
|
|
|
/** @fence_usage: the usage of the current fence */
|
|
|
|
enum dma_resv_usage fence_usage;
|
|
|
|
|
2021-06-15 13:10:03 +00:00
|
|
|
/** @index: index into the shared fences */
|
|
|
|
unsigned int index;
|
|
|
|
|
2021-10-08 09:50:07 +00:00
|
|
|
/** @fences: the shared fences; private, *MUST* not dereference */
|
2021-06-15 13:10:03 +00:00
|
|
|
struct dma_resv_list *fences;
|
|
|
|
|
2021-11-23 08:33:07 +00:00
|
|
|
/** @num_fences: number of fences */
|
|
|
|
unsigned int num_fences;
|
2021-10-08 09:50:07 +00:00
|
|
|
|
2021-06-15 13:10:03 +00:00
|
|
|
/** @is_restarted: true if this is the first returned fence */
|
|
|
|
bool is_restarted;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor);
|
|
|
|
struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor);
|
2021-06-15 13:10:03 +00:00
|
|
|
struct dma_fence *dma_resv_iter_first(struct dma_resv_iter *cursor);
|
|
|
|
struct dma_fence *dma_resv_iter_next(struct dma_resv_iter *cursor);
|
2021-06-15 13:10:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_resv_iter_begin - initialize a dma_resv_iter object
|
|
|
|
* @cursor: The dma_resv_iter object to initialize
|
|
|
|
* @obj: The dma_resv object which we want to iterate over
|
2021-11-09 10:08:18 +00:00
|
|
|
* @usage: controls which fences to include, see enum dma_resv_usage.
|
2021-06-15 13:10:03 +00:00
|
|
|
*/
|
|
|
|
static inline void dma_resv_iter_begin(struct dma_resv_iter *cursor,
|
|
|
|
struct dma_resv *obj,
|
2021-11-09 10:08:18 +00:00
|
|
|
enum dma_resv_usage usage)
|
2021-06-15 13:10:03 +00:00
|
|
|
{
|
|
|
|
cursor->obj = obj;
|
2021-11-09 10:08:18 +00:00
|
|
|
cursor->usage = usage;
|
2021-06-15 13:10:03 +00:00
|
|
|
cursor->fence = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_resv_iter_end - cleanup a dma_resv_iter object
|
|
|
|
* @cursor: the dma_resv_iter object which should be cleaned up
|
|
|
|
*
|
|
|
|
* Make sure that the reference to the fence in the cursor is properly
|
|
|
|
* dropped.
|
|
|
|
*/
|
|
|
|
static inline void dma_resv_iter_end(struct dma_resv_iter *cursor)
|
|
|
|
{
|
|
|
|
dma_fence_put(cursor->fence);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-09 10:08:18 +00:00
|
|
|
* dma_resv_iter_usage - Return the usage of the current fence
|
2021-06-15 13:10:03 +00:00
|
|
|
* @cursor: the cursor of the current position
|
|
|
|
*
|
2021-11-09 10:08:18 +00:00
|
|
|
* Returns the usage of the currently processed fence.
|
2021-06-15 13:10:03 +00:00
|
|
|
*/
|
2021-11-09 10:08:18 +00:00
|
|
|
static inline enum dma_resv_usage
|
|
|
|
dma_resv_iter_usage(struct dma_resv_iter *cursor)
|
2021-06-15 13:10:03 +00:00
|
|
|
{
|
2021-11-09 10:08:18 +00:00
|
|
|
return cursor->fence_usage;
|
2021-06-15 13:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_resv_iter_is_restarted - test if this is the first fence after a restart
|
|
|
|
* @cursor: the cursor with the current position
|
|
|
|
*
|
|
|
|
* Return true if this is the first fence in an iteration after a restart.
|
|
|
|
*/
|
|
|
|
static inline bool dma_resv_iter_is_restarted(struct dma_resv_iter *cursor)
|
|
|
|
{
|
|
|
|
return cursor->is_restarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_resv_for_each_fence_unlocked - unlocked fence iterator
|
|
|
|
* @cursor: a struct dma_resv_iter pointer
|
|
|
|
* @fence: the current fence
|
|
|
|
*
|
|
|
|
* Iterate over the fences in a struct dma_resv object without holding the
|
|
|
|
* &dma_resv.lock and using RCU instead. The cursor needs to be initialized
|
|
|
|
* with dma_resv_iter_begin() and cleaned up with dma_resv_iter_end(). Inside
|
|
|
|
* the iterator a reference to the dma_fence is held and the RCU lock dropped.
|
2021-11-30 15:27:55 +00:00
|
|
|
*
|
|
|
|
* Beware that the iterator can be restarted when the struct dma_resv for
|
|
|
|
* @cursor is modified. Code which accumulates statistics or similar needs to
|
|
|
|
* check for this with dma_resv_iter_is_restarted(). For this reason prefer the
|
|
|
|
* lock iterator dma_resv_for_each_fence() whenever possible.
|
2021-06-15 13:10:03 +00:00
|
|
|
*/
|
|
|
|
#define dma_resv_for_each_fence_unlocked(cursor, fence) \
|
|
|
|
for (fence = dma_resv_iter_first_unlocked(cursor); \
|
|
|
|
fence; fence = dma_resv_iter_next_unlocked(cursor))
|
|
|
|
|
2021-06-15 13:10:03 +00:00
|
|
|
/**
|
|
|
|
* dma_resv_for_each_fence - fence iterator
|
|
|
|
* @cursor: a struct dma_resv_iter pointer
|
|
|
|
* @obj: a dma_resv object pointer
|
2021-11-09 10:08:18 +00:00
|
|
|
* @usage: controls which fences to return
|
2021-06-15 13:10:03 +00:00
|
|
|
* @fence: the current fence
|
|
|
|
*
|
|
|
|
* Iterate over the fences in a struct dma_resv object while holding the
|
|
|
|
* &dma_resv.lock. @all_fences controls if the shared fences are returned as
|
|
|
|
* well. The cursor initialisation is part of the iterator and the fence stays
|
|
|
|
* valid as long as the lock is held and so no extra reference to the fence is
|
|
|
|
* taken.
|
|
|
|
*/
|
2021-11-09 10:08:18 +00:00
|
|
|
#define dma_resv_for_each_fence(cursor, obj, usage, fence) \
|
|
|
|
for (dma_resv_iter_begin(cursor, obj, usage), \
|
2021-06-15 13:10:03 +00:00
|
|
|
fence = dma_resv_iter_first(cursor); fence; \
|
|
|
|
fence = dma_resv_iter_next(cursor))
|
|
|
|
|
2019-08-11 08:06:32 +00:00
|
|
|
#define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
|
|
|
|
#define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
|
2014-07-01 10:57:54 +00:00
|
|
|
|
2021-05-06 12:16:01 +00:00
|
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
2021-11-09 10:08:18 +00:00
|
|
|
void dma_resv_reset_max_fences(struct dma_resv *obj);
|
2021-05-06 12:16:01 +00:00
|
|
|
#else
|
2021-11-09 10:08:18 +00:00
|
|
|
static inline void dma_resv_reset_max_fences(struct dma_resv *obj) {}
|
2021-05-06 12:16:01 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-15 15:46:42 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_lock - lock the reservation object
|
2016-11-15 15:46:42 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Locks the reservation object for exclusive access and modification. Note,
|
|
|
|
* that the lock is only against other writers, readers will run concurrently
|
|
|
|
* with a writer under RCU. The seqlock is used to notify readers if they
|
|
|
|
* overlap with a writer.
|
|
|
|
*
|
|
|
|
* As the reservation object may be locked by multiple parties in an
|
|
|
|
* undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
|
|
|
|
* is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
|
|
|
|
* object may be locked by itself by passing NULL as @ctx.
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
|
|
|
* When a die situation is indicated by returning -EDEADLK all locks held by
|
|
|
|
* @ctx must be unlocked and then dma_resv_lock_slow() called on @obj.
|
|
|
|
*
|
|
|
|
* Unlocked by calling dma_resv_unlock().
|
|
|
|
*
|
|
|
|
* See also dma_resv_lock_interruptible() for the interruptible variant.
|
2016-11-15 15:46:42 +00:00
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline int dma_resv_lock(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2016-11-15 15:46:42 +00:00
|
|
|
{
|
|
|
|
return ww_mutex_lock(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
2017-11-09 08:59:05 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_lock_interruptible - lock the reservation object
|
2017-11-09 08:59:05 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Locks the reservation object interruptible for exclusive access and
|
|
|
|
* modification. Note, that the lock is only against other writers, readers
|
|
|
|
* will run concurrently with a writer under RCU. The seqlock is used to
|
|
|
|
* notify readers if they overlap with a writer.
|
|
|
|
*
|
|
|
|
* As the reservation object may be locked by multiple parties in an
|
|
|
|
* undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
|
|
|
|
* is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
|
|
|
|
* object may be locked by itself by passing NULL as @ctx.
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
|
|
|
* When a die situation is indicated by returning -EDEADLK all locks held by
|
|
|
|
* @ctx must be unlocked and then dma_resv_lock_slow_interruptible() called on
|
|
|
|
* @obj.
|
|
|
|
*
|
|
|
|
* Unlocked by calling dma_resv_unlock().
|
2017-11-09 08:59:05 +00:00
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2017-11-09 08:59:05 +00:00
|
|
|
{
|
|
|
|
return ww_mutex_lock_interruptible(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:41:50 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_lock_slow - slowpath lock the reservation object
|
2019-07-31 07:41:50 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Acquires the reservation object after a die case. This function
|
2019-08-11 08:06:32 +00:00
|
|
|
* will sleep until the lock becomes available. See dma_resv_lock() as
|
2019-07-31 07:41:50 +00:00
|
|
|
* well.
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
|
|
|
* See also dma_resv_lock_slow_interruptible() for the interruptible variant.
|
2019-07-31 07:41:50 +00:00
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline void dma_resv_lock_slow(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2019-07-31 07:41:50 +00:00
|
|
|
{
|
|
|
|
ww_mutex_lock_slow(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_lock_slow_interruptible - slowpath lock the reservation
|
2019-07-31 07:41:50 +00:00
|
|
|
* object, interruptible
|
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Acquires the reservation object interruptible after a die case. This function
|
|
|
|
* will sleep until the lock becomes available. See
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_lock_interruptible() as well.
|
2019-07-31 07:41:50 +00:00
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2019-07-31 07:41:50 +00:00
|
|
|
{
|
|
|
|
return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
|
|
|
|
}
|
2017-11-09 08:59:05 +00:00
|
|
|
|
2017-02-21 09:30:00 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_trylock - trylock the reservation object
|
2017-02-21 09:30:00 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Tries to lock the reservation object for exclusive access and modification.
|
|
|
|
* Note, that the lock is only against other writers, readers will run
|
|
|
|
* concurrently with a writer under RCU. The seqlock is used to notify readers
|
|
|
|
* if they overlap with a writer.
|
|
|
|
*
|
|
|
|
* Also note that since no context is provided, no deadlock protection is
|
2021-08-05 10:47:05 +00:00
|
|
|
* possible, which is also not needed for a trylock.
|
2017-02-21 09:30:00 +00:00
|
|
|
*
|
|
|
|
* Returns true if the lock was acquired, false otherwise.
|
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
|
2017-02-21 09:30:00 +00:00
|
|
|
{
|
kernel/locking: Add context to ww_mutex_trylock()
i915 will soon gain an eviction path that trylock a whole lot of locks
for eviction, getting dmesg failures like below:
BUG: MAX_LOCK_DEPTH too low!
turning off the locking correctness validator.
depth: 48 max: 48!
48 locks held by i915_selftest/5776:
#0: ffff888101a79240 (&dev->mutex){....}-{3:3}, at: __driver_attach+0x88/0x160
#1: ffffc900009778c0 (reservation_ww_class_acquire){+.+.}-{0:0}, at: i915_vma_pin.constprop.63+0x39/0x1b0 [i915]
#2: ffff88800cf74de8 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_vma_pin.constprop.63+0x5f/0x1b0 [i915]
#3: ffff88810c7f9e38 (&vm->mutex/1){+.+.}-{3:3}, at: i915_vma_pin_ww+0x1c4/0x9d0 [i915]
#4: ffff88810bad5768 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
#5: ffff88810bad60e8 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
...
#46: ffff88811964d768 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
#47: ffff88811964e0e8 (reservation_ww_class_mutex){+.+.}-{3:3}, at: i915_gem_evict_something+0x110/0x860 [i915]
INFO: lockdep is turned off.
Fixing eviction to nest into ww_class_acquire is a high priority, but
it requires a rework of the entire driver, which can only be done one
step at a time.
As an intermediate solution, add an acquire context to
ww_mutex_trylock, which allows us to do proper nesting annotations on
the trylocks, making the above lockdep splat disappear.
This is also useful in regulator_lock_nested, which may avoid dropping
regulator_nesting_mutex in the uncontended path, so use it there.
TTM may be another user for this, where we could lock a buffer in a
fastpath with list locks held, without dropping all locks we hold.
[peterz: rework actual ww_mutex_trylock() implementations]
Signed-off-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/YUBGPdDDjKlxAuXJ@hirez.programming.kicks-ass.net
2021-09-09 09:32:18 +00:00
|
|
|
return ww_mutex_trylock(&obj->lock, NULL);
|
2017-02-21 09:30:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:41:50 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_is_locked - is the reservation object locked
|
2019-07-31 07:41:50 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns true if the mutex is locked, false if unlocked.
|
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline bool dma_resv_is_locked(struct dma_resv *obj)
|
2019-07-31 07:41:50 +00:00
|
|
|
{
|
|
|
|
return ww_mutex_is_locked(&obj->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_locking_ctx - returns the context used to lock the object
|
2019-07-31 07:41:50 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns the context used to lock a reservation object or NULL if no context
|
|
|
|
* was used or the object is not locked at all.
|
2021-08-05 10:47:05 +00:00
|
|
|
*
|
|
|
|
* WARNING: This interface is pretty horrible, but TTM needs it because it
|
|
|
|
* doesn't pass the struct ww_acquire_ctx around in some very long callchains.
|
|
|
|
* Everyone else just uses it to check whether they're holding a reservation or
|
|
|
|
* not.
|
2019-07-31 07:41:50 +00:00
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
|
2019-07-31 07:41:50 +00:00
|
|
|
{
|
|
|
|
return READ_ONCE(obj->lock.ctx);
|
|
|
|
}
|
|
|
|
|
2016-11-15 15:46:42 +00:00
|
|
|
/**
|
2019-08-11 08:06:32 +00:00
|
|
|
* dma_resv_unlock - unlock the reservation object
|
2016-11-15 15:46:42 +00:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Unlocks the reservation object following exclusive access.
|
|
|
|
*/
|
2019-08-11 08:06:32 +00:00
|
|
|
static inline void dma_resv_unlock(struct dma_resv *obj)
|
2016-11-15 15:46:42 +00:00
|
|
|
{
|
2021-11-09 10:08:18 +00:00
|
|
|
dma_resv_reset_max_fences(obj);
|
2016-11-15 15:46:42 +00:00
|
|
|
ww_mutex_unlock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2019-08-11 08:06:32 +00:00
|
|
|
void dma_resv_init(struct dma_resv *obj);
|
|
|
|
void dma_resv_fini(struct dma_resv *obj);
|
2021-11-16 14:20:45 +00:00
|
|
|
int dma_resv_reserve_fences(struct dma_resv *obj, unsigned int num_fences);
|
2021-11-09 10:08:18 +00:00
|
|
|
void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
|
|
|
|
enum dma_resv_usage usage);
|
2021-09-24 08:55:45 +00:00
|
|
|
void dma_resv_replace_fences(struct dma_resv *obj, uint64_t context,
|
2021-11-09 10:08:18 +00:00
|
|
|
struct dma_fence *fence,
|
|
|
|
enum dma_resv_usage usage);
|
2021-11-09 10:08:18 +00:00
|
|
|
int dma_resv_get_fences(struct dma_resv *obj, enum dma_resv_usage usage,
|
2021-10-28 11:19:22 +00:00
|
|
|
unsigned int *num_fences, struct dma_fence ***fences);
|
2021-11-09 10:08:18 +00:00
|
|
|
int dma_resv_get_singleton(struct dma_resv *obj, enum dma_resv_usage usage,
|
2021-11-03 09:02:08 +00:00
|
|
|
struct dma_fence **fence);
|
2019-08-11 08:06:32 +00:00
|
|
|
int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
|
2021-11-09 10:08:18 +00:00
|
|
|
long dma_resv_wait_timeout(struct dma_resv *obj, enum dma_resv_usage usage,
|
|
|
|
bool intr, unsigned long timeout);
|
|
|
|
bool dma_resv_test_signaled(struct dma_resv *obj, enum dma_resv_usage usage);
|
2021-09-23 11:57:42 +00:00
|
|
|
void dma_resv_describe(struct dma_resv *obj, struct seq_file *seq);
|
2014-07-01 10:58:00 +00:00
|
|
|
|
2013-06-27 11:48:16 +00:00
|
|
|
#endif /* _LINUX_RESERVATION_H */
|