Blame view

include/linux/dma-resv.h 9.06 KB
786d7257e   Maarten Lankhorst   reservation: cros...
1
2
3
4
5
6
7
8
  /*
   * 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:
0ba6b8fb9   Maarten Lankhorst   reservation: add ...
9
   * Rob Clark <robdclark@gmail.com>
786d7257e   Maarten Lankhorst   reservation: cros...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
   * 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
1b375dc30   Maarten Lankhorst   mutex: Move ww_mu...
41
  #include <linux/ww_mutex.h>
f54d18670   Chris Wilson   dma-buf: Rename s...
42
  #include <linux/dma-fence.h>
0ba6b8fb9   Maarten Lankhorst   reservation: add ...
43
  #include <linux/slab.h>
3c3b177a9   Maarten Lankhorst   reservation: add ...
44
45
  #include <linux/seqlock.h>
  #include <linux/rcupdate.h>
786d7257e   Maarten Lankhorst   reservation: cros...
46
47
  
  extern struct ww_class reservation_ww_class;
dad6c3945   Rob Clark   reservation: add ...
48
  /**
52791eeec   Christian König   dma-buf: rename r...
49
   * struct dma_resv_list - a list of shared fences
dad6c3945   Rob Clark   reservation: add ...
50
51
52
53
54
   * @rcu: for internal use
   * @shared_count: table of shared fences
   * @shared_max: for growing shared fence table
   * @shared: shared fence table
   */
52791eeec   Christian König   dma-buf: rename r...
55
  struct dma_resv_list {
3c3b177a9   Maarten Lankhorst   reservation: add ...
56
  	struct rcu_head rcu;
04a5faa8c   Maarten Lankhorst   reservation: upda...
57
  	u32 shared_count, shared_max;
f54d18670   Chris Wilson   dma-buf: Rename s...
58
  	struct dma_fence __rcu *shared[];
04a5faa8c   Maarten Lankhorst   reservation: upda...
59
  };
dad6c3945   Rob Clark   reservation: add ...
60
  /**
52791eeec   Christian König   dma-buf: rename r...
61
   * struct dma_resv - a reservation object manages fences for a buffer
dad6c3945   Rob Clark   reservation: add ...
62
63
64
65
   * @lock: update side lock
   * @seq: sequence count for managing RCU read-side synchronization
   * @fence_excl: the exclusive fence, if there is one currently
   * @fence: list of current shared fences
dad6c3945   Rob Clark   reservation: add ...
66
   */
52791eeec   Christian König   dma-buf: rename r...
67
  struct dma_resv {
786d7257e   Maarten Lankhorst   reservation: cros...
68
  	struct ww_mutex lock;
cd29f2201   Ahmed S. Darwish   dma-buf: Use sequ...
69
  	seqcount_ww_mutex_t seq;
0ba6b8fb9   Maarten Lankhorst   reservation: add ...
70

f54d18670   Chris Wilson   dma-buf: Rename s...
71
  	struct dma_fence __rcu *fence_excl;
52791eeec   Christian König   dma-buf: rename r...
72
  	struct dma_resv_list __rcu *fence;
786d7257e   Maarten Lankhorst   reservation: cros...
73
  };
52791eeec   Christian König   dma-buf: rename r...
74
75
  #define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
  #define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
04a5faa8c   Maarten Lankhorst   reservation: upda...
76

dad6c3945   Rob Clark   reservation: add ...
77
  /**
52791eeec   Christian König   dma-buf: rename r...
78
   * dma_resv_get_list - get the reservation object's
dad6c3945   Rob Clark   reservation: add ...
79
80
81
82
83
84
   * shared fence list, with update-side lock held
   * @obj: the reservation object
   *
   * Returns the shared fence list.  Does NOT take references to
   * the fence.  The obj->lock must be held.
   */
52791eeec   Christian König   dma-buf: rename r...
85
  static inline struct dma_resv_list *dma_resv_get_list(struct dma_resv *obj)
04a5faa8c   Maarten Lankhorst   reservation: upda...
86
  {
3c3b177a9   Maarten Lankhorst   reservation: add ...
87
  	return rcu_dereference_protected(obj->fence,
52791eeec   Christian König   dma-buf: rename r...
88
  					 dma_resv_held(obj));
04a5faa8c   Maarten Lankhorst   reservation: upda...
89
  }
dad6c3945   Rob Clark   reservation: add ...
90
  /**
52791eeec   Christian König   dma-buf: rename r...
91
   * dma_resv_lock - lock the reservation object
122020af8   Chris Wilson   dma-buf: Provide ...
92
93
94
95
96
97
98
99
100
101
102
103
104
   * @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.
   */
52791eeec   Christian König   dma-buf: rename r...
105
106
  static inline int dma_resv_lock(struct dma_resv *obj,
  				struct ww_acquire_ctx *ctx)
122020af8   Chris Wilson   dma-buf: Provide ...
107
108
109
110
111
  {
  	return ww_mutex_lock(&obj->lock, ctx);
  }
  
  /**
52791eeec   Christian König   dma-buf: rename r...
112
   * dma_resv_lock_interruptible - lock the reservation object
5d276a1ac   Christian König   dma-buf: add rese...
113
114
115
116
117
118
119
120
121
122
123
124
125
   * @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.
   */
52791eeec   Christian König   dma-buf: rename r...
126
127
  static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
  					      struct ww_acquire_ctx *ctx)
5d276a1ac   Christian König   dma-buf: add rese...
128
129
130
  {
  	return ww_mutex_lock_interruptible(&obj->lock, ctx);
  }
0dbd555a0   Christian König   dma-buf: add more...
131
  /**
52791eeec   Christian König   dma-buf: rename r...
132
   * dma_resv_lock_slow - slowpath lock the reservation object
0dbd555a0   Christian König   dma-buf: add more...
133
134
135
136
   * @obj: the reservation object
   * @ctx: the locking context
   *
   * Acquires the reservation object after a die case. This function
52791eeec   Christian König   dma-buf: rename r...
137
   * will sleep until the lock becomes available. See dma_resv_lock() as
0dbd555a0   Christian König   dma-buf: add more...
138
139
   * well.
   */
52791eeec   Christian König   dma-buf: rename r...
140
141
  static inline void dma_resv_lock_slow(struct dma_resv *obj,
  				      struct ww_acquire_ctx *ctx)
0dbd555a0   Christian König   dma-buf: add more...
142
143
144
145
146
  {
  	ww_mutex_lock_slow(&obj->lock, ctx);
  }
  
  /**
52791eeec   Christian König   dma-buf: rename r...
147
   * dma_resv_lock_slow_interruptible - slowpath lock the reservation
0dbd555a0   Christian König   dma-buf: add more...
148
149
150
151
152
153
   * 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
52791eeec   Christian König   dma-buf: rename r...
154
   * dma_resv_lock_interruptible() as well.
0dbd555a0   Christian König   dma-buf: add more...
155
   */
52791eeec   Christian König   dma-buf: rename r...
156
157
  static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
  						   struct ww_acquire_ctx *ctx)
0dbd555a0   Christian König   dma-buf: add more...
158
159
160
  {
  	return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
  }
5d276a1ac   Christian König   dma-buf: add rese...
161
162
  
  /**
52791eeec   Christian König   dma-buf: rename r...
163
   * dma_resv_trylock - trylock the reservation object
2955b73de   Chris Wilson   dma-buf/reservati...
164
165
166
167
168
169
170
171
172
173
174
175
   * @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
   * possible.
   *
   * Returns true if the lock was acquired, false otherwise.
   */
52791eeec   Christian König   dma-buf: rename r...
176
  static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
2955b73de   Chris Wilson   dma-buf/reservati...
177
178
179
180
181
  {
  	return ww_mutex_trylock(&obj->lock);
  }
  
  /**
52791eeec   Christian König   dma-buf: rename r...
182
   * dma_resv_is_locked - is the reservation object locked
0dbd555a0   Christian König   dma-buf: add more...
183
184
185
186
   * @obj: the reservation object
   *
   * Returns true if the mutex is locked, false if unlocked.
   */
52791eeec   Christian König   dma-buf: rename r...
187
  static inline bool dma_resv_is_locked(struct dma_resv *obj)
0dbd555a0   Christian König   dma-buf: add more...
188
189
190
191
192
  {
  	return ww_mutex_is_locked(&obj->lock);
  }
  
  /**
52791eeec   Christian König   dma-buf: rename r...
193
   * dma_resv_locking_ctx - returns the context used to lock the object
0dbd555a0   Christian König   dma-buf: add more...
194
195
196
197
198
   * @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.
   */
52791eeec   Christian König   dma-buf: rename r...
199
  static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
0dbd555a0   Christian König   dma-buf: add more...
200
201
202
203
204
  {
  	return READ_ONCE(obj->lock.ctx);
  }
  
  /**
52791eeec   Christian König   dma-buf: rename r...
205
   * dma_resv_unlock - unlock the reservation object
122020af8   Chris Wilson   dma-buf: Provide ...
206
207
208
209
   * @obj: the reservation object
   *
   * Unlocks the reservation object following exclusive access.
   */
52791eeec   Christian König   dma-buf: rename r...
210
  static inline void dma_resv_unlock(struct dma_resv *obj)
122020af8   Chris Wilson   dma-buf: Provide ...
211
  {
99fe21a76   Christian König   dma-buf: test sha...
212
213
  #ifdef CONFIG_DEBUG_MUTEXES
  	/* Test shared fence slot reservation */
5740671e5   Chris Wilson   dma-fence/reserva...
214
  	if (rcu_access_pointer(obj->fence)) {
52791eeec   Christian König   dma-buf: rename r...
215
  		struct dma_resv_list *fence = dma_resv_get_list(obj);
5740671e5   Chris Wilson   dma-fence/reserva...
216
217
218
  
  		fence->shared_max = fence->shared_count;
  	}
99fe21a76   Christian König   dma-buf: test sha...
219
  #endif
122020af8   Chris Wilson   dma-buf: Provide ...
220
221
  	ww_mutex_unlock(&obj->lock);
  }
b016cd6ed   Chris Wilson   dma-buf: Restore ...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  /**
   * dma_resv_get_excl - get the reservation object's
   * exclusive fence, with update-side lock held
   * @obj: the reservation object
   *
   * Returns the exclusive fence (if any).  Does NOT take a
   * reference. Writers must hold obj->lock, readers may only
   * hold a RCU read side lock.
   *
   * RETURNS
   * The exclusive fence or NULL
   */
  static inline struct dma_fence *
  dma_resv_get_excl(struct dma_resv *obj)
  {
  	return rcu_dereference_protected(obj->fence_excl,
  					 dma_resv_held(obj));
  }
  
  /**
   * dma_resv_get_excl_rcu - get the reservation object's
   * exclusive fence, without lock held.
   * @obj: the reservation object
   *
   * If there is an exclusive fence, this atomically increments it's
   * reference count and returns it.
   *
   * RETURNS
   * The exclusive fence or NULL if none
   */
  static inline struct dma_fence *
  dma_resv_get_excl_rcu(struct dma_resv *obj)
  {
  	struct dma_fence *fence;
  
  	if (!rcu_access_pointer(obj->fence_excl))
  		return NULL;
  
  	rcu_read_lock();
  	fence = dma_fence_get_rcu_safe(&obj->fence_excl);
  	rcu_read_unlock();
  
  	return fence;
  }
52791eeec   Christian König   dma-buf: rename r...
266
267
268
269
  void dma_resv_init(struct dma_resv *obj);
  void dma_resv_fini(struct dma_resv *obj);
  int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
  void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
04a5faa8c   Maarten Lankhorst   reservation: upda...
270

52791eeec   Christian König   dma-buf: rename r...
271
  void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
04a5faa8c   Maarten Lankhorst   reservation: upda...
272

52791eeec   Christian König   dma-buf: rename r...
273
274
275
276
  int dma_resv_get_fences_rcu(struct dma_resv *obj,
  			    struct dma_fence **pfence_excl,
  			    unsigned *pshared_count,
  			    struct dma_fence ***pshared);
3c3b177a9   Maarten Lankhorst   reservation: add ...
277

52791eeec   Christian König   dma-buf: rename r...
278
  int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
7faf952a3   Christian König   dma-buf: add rese...
279

52791eeec   Christian König   dma-buf: rename r...
280
281
  long dma_resv_wait_timeout_rcu(struct dma_resv *obj, bool wait_all, bool intr,
  			       unsigned long timeout);
3c3b177a9   Maarten Lankhorst   reservation: add ...
282

52791eeec   Christian König   dma-buf: rename r...
283
  bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all);
3c3b177a9   Maarten Lankhorst   reservation: add ...
284

786d7257e   Maarten Lankhorst   reservation: cros...
285
  #endif /* _LINUX_RESERVATION_H */