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: 9 * Rob Clark <robdclark@gmail.com> 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 42 #include <linux/ww_mutex.h> 43 #include <linux/dma-fence.h> 44 #include <linux/slab.h> 45 #include <linux/seqlock.h> 46 #include <linux/rcupdate.h> 47 48 extern struct ww_class reservation_ww_class; 49 50 /** 51 * struct dma_resv_list - a list of shared fences 52 * @rcu: for internal use 53 * @shared_count: table of shared fences 54 * @shared_max: for growing shared fence table 55 * @shared: shared fence table 56 */ 57 struct dma_resv_list { 58 struct rcu_head rcu; 59 u32 shared_count, shared_max; 60 struct dma_fence __rcu *shared[]; 61 }; 62 63 /** 64 * struct dma_resv - a reservation object manages fences for a buffer 65 * 66 * There are multiple uses for this, with sometimes slightly different rules in 67 * how the fence slots are used. 68 * 69 * One use is to synchronize cross-driver access to a struct dma_buf, either for 70 * dynamic buffer management or just to handle implicit synchronization between 71 * different users of the buffer in userspace. See &dma_buf.resv for a more 72 * in-depth discussion. 73 * 74 * The other major use is to manage access and locking within a driver in a 75 * buffer based memory manager. struct ttm_buffer_object is the canonical 76 * example here, since this is where reservation objects originated from. But 77 * use in drivers is spreading and some drivers also manage struct 78 * drm_gem_object with the same scheme. 79 */ 80 struct dma_resv { 81 /** 82 * @lock: 83 * 84 * Update side lock. Don't use directly, instead use the wrapper 85 * functions like dma_resv_lock() and dma_resv_unlock(). 86 * 87 * Drivers which use the reservation object to manage memory dynamically 88 * also use this lock to protect buffer object state like placement, 89 * allocation policies or throughout command submission. 90 */ 91 struct ww_mutex lock; 92 93 /** 94 * @seq: 95 * 96 * Sequence count for managing RCU read-side synchronization, allows 97 * read-only access to @fence_excl and @fence while ensuring we take a 98 * consistent snapshot. 99 */ 100 seqcount_ww_mutex_t seq; 101 102 /** 103 * @fence_excl: 104 * 105 * The exclusive fence, if there is one currently. 106 * 107 * There are two ways to update this fence: 108 * 109 * - First by calling dma_resv_add_excl_fence(), which replaces all 110 * fences attached to the reservation object. To guarantee that no 111 * fences are lost, this new fence must signal only after all previous 112 * fences, both shared and exclusive, have signalled. In some cases it 113 * is convenient to achieve that by attaching a struct dma_fence_array 114 * with all the new and old fences. 115 * 116 * - Alternatively the fence can be set directly, which leaves the 117 * shared fences unchanged. To guarantee that no fences are lost, this 118 * new fence must signal only after the previous exclusive fence has 119 * signalled. Since the shared fences are staying intact, it is not 120 * necessary to maintain any ordering against those. If semantically 121 * only a new access is added without actually treating the previous 122 * one as a dependency the exclusive fences can be strung together 123 * using struct dma_fence_chain. 124 * 125 * Note that actual semantics of what an exclusive or shared fence mean 126 * is defined by the user, for reservation objects shared across drivers 127 * see &dma_buf.resv. 128 */ 129 struct dma_fence __rcu *fence_excl; 130 131 /** 132 * @fence: 133 * 134 * List of current shared fences. 135 * 136 * There are no ordering constraints of shared fences against the 137 * exclusive fence slot. If a waiter needs to wait for all access, it 138 * has to wait for both sets of fences to signal. 139 * 140 * A new fence is added by calling dma_resv_add_shared_fence(). Since 141 * this often needs to be done past the point of no return in command 142 * submission it cannot fail, and therefore sufficient slots need to be 143 * reserved by calling dma_resv_reserve_shared(). 144 * 145 * Note that actual semantics of what an exclusive or shared fence mean 146 * is defined by the user, for reservation objects shared across drivers 147 * see &dma_buf.resv. 148 */ 149 struct dma_resv_list __rcu *fence; 150 }; 151 152 /** 153 * struct dma_resv_iter - current position into the dma_resv fences 154 * 155 * Don't touch this directly in the driver, use the accessor function instead. 156 */ 157 struct dma_resv_iter { 158 /** @obj: The dma_resv object we iterate over */ 159 struct dma_resv *obj; 160 161 /** @all_fences: If all fences should be returned */ 162 bool all_fences; 163 164 /** @fence: the currently handled fence */ 165 struct dma_fence *fence; 166 167 /** @seq: sequence number to check for modifications */ 168 unsigned int seq; 169 170 /** @index: index into the shared fences */ 171 unsigned int index; 172 173 /** @fences: the shared fences */ 174 struct dma_resv_list *fences; 175 176 /** @is_restarted: true if this is the first returned fence */ 177 bool is_restarted; 178 }; 179 180 struct dma_fence *dma_resv_iter_first_unlocked(struct dma_resv_iter *cursor); 181 struct dma_fence *dma_resv_iter_next_unlocked(struct dma_resv_iter *cursor); 182 183 /** 184 * dma_resv_iter_begin - initialize a dma_resv_iter object 185 * @cursor: The dma_resv_iter object to initialize 186 * @obj: The dma_resv object which we want to iterate over 187 * @all_fences: If all fences should be returned or just the exclusive one 188 */ 189 static inline void dma_resv_iter_begin(struct dma_resv_iter *cursor, 190 struct dma_resv *obj, 191 bool all_fences) 192 { 193 cursor->obj = obj; 194 cursor->all_fences = all_fences; 195 cursor->fence = NULL; 196 } 197 198 /** 199 * dma_resv_iter_end - cleanup a dma_resv_iter object 200 * @cursor: the dma_resv_iter object which should be cleaned up 201 * 202 * Make sure that the reference to the fence in the cursor is properly 203 * dropped. 204 */ 205 static inline void dma_resv_iter_end(struct dma_resv_iter *cursor) 206 { 207 dma_fence_put(cursor->fence); 208 } 209 210 /** 211 * dma_resv_iter_is_exclusive - test if the current fence is the exclusive one 212 * @cursor: the cursor of the current position 213 * 214 * Returns true if the currently returned fence is the exclusive one. 215 */ 216 static inline bool dma_resv_iter_is_exclusive(struct dma_resv_iter *cursor) 217 { 218 return cursor->index == 0; 219 } 220 221 /** 222 * dma_resv_iter_is_restarted - test if this is the first fence after a restart 223 * @cursor: the cursor with the current position 224 * 225 * Return true if this is the first fence in an iteration after a restart. 226 */ 227 static inline bool dma_resv_iter_is_restarted(struct dma_resv_iter *cursor) 228 { 229 return cursor->is_restarted; 230 } 231 232 /** 233 * dma_resv_for_each_fence_unlocked - unlocked fence iterator 234 * @cursor: a struct dma_resv_iter pointer 235 * @fence: the current fence 236 * 237 * Iterate over the fences in a struct dma_resv object without holding the 238 * &dma_resv.lock and using RCU instead. The cursor needs to be initialized 239 * with dma_resv_iter_begin() and cleaned up with dma_resv_iter_end(). Inside 240 * the iterator a reference to the dma_fence is held and the RCU lock dropped. 241 * When the dma_resv is modified the iteration starts over again. 242 */ 243 #define dma_resv_for_each_fence_unlocked(cursor, fence) \ 244 for (fence = dma_resv_iter_first_unlocked(cursor); \ 245 fence; fence = dma_resv_iter_next_unlocked(cursor)) 246 247 #define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base) 248 #define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base) 249 250 #ifdef CONFIG_DEBUG_MUTEXES 251 void dma_resv_reset_shared_max(struct dma_resv *obj); 252 #else 253 static inline void dma_resv_reset_shared_max(struct dma_resv *obj) {} 254 #endif 255 256 /** 257 * dma_resv_lock - lock the reservation object 258 * @obj: the reservation object 259 * @ctx: the locking context 260 * 261 * Locks the reservation object for exclusive access and modification. Note, 262 * that the lock is only against other writers, readers will run concurrently 263 * with a writer under RCU. The seqlock is used to notify readers if they 264 * overlap with a writer. 265 * 266 * As the reservation object may be locked by multiple parties in an 267 * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle 268 * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation 269 * object may be locked by itself by passing NULL as @ctx. 270 * 271 * When a die situation is indicated by returning -EDEADLK all locks held by 272 * @ctx must be unlocked and then dma_resv_lock_slow() called on @obj. 273 * 274 * Unlocked by calling dma_resv_unlock(). 275 * 276 * See also dma_resv_lock_interruptible() for the interruptible variant. 277 */ 278 static inline int dma_resv_lock(struct dma_resv *obj, 279 struct ww_acquire_ctx *ctx) 280 { 281 return ww_mutex_lock(&obj->lock, ctx); 282 } 283 284 /** 285 * dma_resv_lock_interruptible - lock the reservation object 286 * @obj: the reservation object 287 * @ctx: the locking context 288 * 289 * Locks the reservation object interruptible for exclusive access and 290 * modification. Note, that the lock is only against other writers, readers 291 * will run concurrently with a writer under RCU. The seqlock is used to 292 * notify readers if they overlap with a writer. 293 * 294 * As the reservation object may be locked by multiple parties in an 295 * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle 296 * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation 297 * object may be locked by itself by passing NULL as @ctx. 298 * 299 * When a die situation is indicated by returning -EDEADLK all locks held by 300 * @ctx must be unlocked and then dma_resv_lock_slow_interruptible() called on 301 * @obj. 302 * 303 * Unlocked by calling dma_resv_unlock(). 304 */ 305 static inline int dma_resv_lock_interruptible(struct dma_resv *obj, 306 struct ww_acquire_ctx *ctx) 307 { 308 return ww_mutex_lock_interruptible(&obj->lock, ctx); 309 } 310 311 /** 312 * dma_resv_lock_slow - slowpath lock the reservation object 313 * @obj: the reservation object 314 * @ctx: the locking context 315 * 316 * Acquires the reservation object after a die case. This function 317 * will sleep until the lock becomes available. See dma_resv_lock() as 318 * well. 319 * 320 * See also dma_resv_lock_slow_interruptible() for the interruptible variant. 321 */ 322 static inline void dma_resv_lock_slow(struct dma_resv *obj, 323 struct ww_acquire_ctx *ctx) 324 { 325 ww_mutex_lock_slow(&obj->lock, ctx); 326 } 327 328 /** 329 * dma_resv_lock_slow_interruptible - slowpath lock the reservation 330 * object, interruptible 331 * @obj: the reservation object 332 * @ctx: the locking context 333 * 334 * Acquires the reservation object interruptible after a die case. This function 335 * will sleep until the lock becomes available. See 336 * dma_resv_lock_interruptible() as well. 337 */ 338 static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj, 339 struct ww_acquire_ctx *ctx) 340 { 341 return ww_mutex_lock_slow_interruptible(&obj->lock, ctx); 342 } 343 344 /** 345 * dma_resv_trylock - trylock the reservation object 346 * @obj: the reservation object 347 * 348 * Tries to lock the reservation object for exclusive access and modification. 349 * Note, that the lock is only against other writers, readers will run 350 * concurrently with a writer under RCU. The seqlock is used to notify readers 351 * if they overlap with a writer. 352 * 353 * Also note that since no context is provided, no deadlock protection is 354 * possible, which is also not needed for a trylock. 355 * 356 * Returns true if the lock was acquired, false otherwise. 357 */ 358 static inline bool __must_check dma_resv_trylock(struct dma_resv *obj) 359 { 360 return ww_mutex_trylock(&obj->lock, NULL); 361 } 362 363 /** 364 * dma_resv_is_locked - is the reservation object locked 365 * @obj: the reservation object 366 * 367 * Returns true if the mutex is locked, false if unlocked. 368 */ 369 static inline bool dma_resv_is_locked(struct dma_resv *obj) 370 { 371 return ww_mutex_is_locked(&obj->lock); 372 } 373 374 /** 375 * dma_resv_locking_ctx - returns the context used to lock the object 376 * @obj: the reservation object 377 * 378 * Returns the context used to lock a reservation object or NULL if no context 379 * was used or the object is not locked at all. 380 * 381 * WARNING: This interface is pretty horrible, but TTM needs it because it 382 * doesn't pass the struct ww_acquire_ctx around in some very long callchains. 383 * Everyone else just uses it to check whether they're holding a reservation or 384 * not. 385 */ 386 static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj) 387 { 388 return READ_ONCE(obj->lock.ctx); 389 } 390 391 /** 392 * dma_resv_unlock - unlock the reservation object 393 * @obj: the reservation object 394 * 395 * Unlocks the reservation object following exclusive access. 396 */ 397 static inline void dma_resv_unlock(struct dma_resv *obj) 398 { 399 dma_resv_reset_shared_max(obj); 400 ww_mutex_unlock(&obj->lock); 401 } 402 403 /** 404 * dma_resv_excl_fence - return the object's exclusive fence 405 * @obj: the reservation object 406 * 407 * Returns the exclusive fence (if any). Caller must either hold the objects 408 * through dma_resv_lock() or the RCU read side lock through rcu_read_lock(), 409 * or one of the variants of each 410 * 411 * RETURNS 412 * The exclusive fence or NULL 413 */ 414 static inline struct dma_fence * 415 dma_resv_excl_fence(struct dma_resv *obj) 416 { 417 return rcu_dereference_check(obj->fence_excl, dma_resv_held(obj)); 418 } 419 420 /** 421 * dma_resv_get_excl_unlocked - get the reservation object's 422 * exclusive fence, without lock held. 423 * @obj: the reservation object 424 * 425 * If there is an exclusive fence, this atomically increments it's 426 * reference count and returns it. 427 * 428 * RETURNS 429 * The exclusive fence or NULL if none 430 */ 431 static inline struct dma_fence * 432 dma_resv_get_excl_unlocked(struct dma_resv *obj) 433 { 434 struct dma_fence *fence; 435 436 if (!rcu_access_pointer(obj->fence_excl)) 437 return NULL; 438 439 rcu_read_lock(); 440 fence = dma_fence_get_rcu_safe(&obj->fence_excl); 441 rcu_read_unlock(); 442 443 return fence; 444 } 445 446 /** 447 * dma_resv_shared_list - get the reservation object's shared fence list 448 * @obj: the reservation object 449 * 450 * Returns the shared fence list. Caller must either hold the objects 451 * through dma_resv_lock() or the RCU read side lock through rcu_read_lock(), 452 * or one of the variants of each 453 */ 454 static inline struct dma_resv_list *dma_resv_shared_list(struct dma_resv *obj) 455 { 456 return rcu_dereference_check(obj->fence, dma_resv_held(obj)); 457 } 458 459 void dma_resv_init(struct dma_resv *obj); 460 void dma_resv_fini(struct dma_resv *obj); 461 int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences); 462 void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence); 463 void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence); 464 int dma_resv_get_fences(struct dma_resv *obj, struct dma_fence **pfence_excl, 465 unsigned *pshared_count, struct dma_fence ***pshared); 466 int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src); 467 long dma_resv_wait_timeout(struct dma_resv *obj, bool wait_all, bool intr, 468 unsigned long timeout); 469 bool dma_resv_test_signaled(struct dma_resv *obj, bool test_all); 470 471 #endif /* _LINUX_RESERVATION_H */ 472