1 /* 2 * Fence mechanism for dma-buf and to allow for asynchronous dma access 3 * 4 * Copyright (C) 2012 Canonical Ltd 5 * Copyright (C) 2012 Texas Instruments 6 * 7 * Authors: 8 * Rob Clark <robdclark@gmail.com> 9 * Maarten Lankhorst <maarten.lankhorst@canonical.com> 10 * 11 * This program is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License version 2 as published by 13 * the Free Software Foundation. 14 * 15 * This program is distributed in the hope that it will be useful, but WITHOUT 16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 18 * more details. 19 */ 20 21 #include <linux/slab.h> 22 #include <linux/export.h> 23 #include <linux/atomic.h> 24 #include <linux/dma-fence.h> 25 #include <linux/sched/signal.h> 26 27 #define CREATE_TRACE_POINTS 28 #include <trace/events/dma_fence.h> 29 30 EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit); 31 EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal); 32 EXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled); 33 34 static DEFINE_SPINLOCK(dma_fence_stub_lock); 35 static struct dma_fence dma_fence_stub; 36 37 /* 38 * fence context counter: each execution context should have its own 39 * fence context, this allows checking if fences belong to the same 40 * context or not. One device can have multiple separate contexts, 41 * and they're used if some engine can run independently of another. 42 */ 43 static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1); 44 45 /** 46 * DOC: DMA fences overview 47 * 48 * DMA fences, represented by &struct dma_fence, are the kernel internal 49 * synchronization primitive for DMA operations like GPU rendering, video 50 * encoding/decoding, or displaying buffers on a screen. 51 * 52 * A fence is initialized using dma_fence_init() and completed using 53 * dma_fence_signal(). Fences are associated with a context, allocated through 54 * dma_fence_context_alloc(), and all fences on the same context are 55 * fully ordered. 56 * 57 * Since the purposes of fences is to facilitate cross-device and 58 * cross-application synchronization, there's multiple ways to use one: 59 * 60 * - Individual fences can be exposed as a &sync_file, accessed as a file 61 * descriptor from userspace, created by calling sync_file_create(). This is 62 * called explicit fencing, since userspace passes around explicit 63 * synchronization points. 64 * 65 * - Some subsystems also have their own explicit fencing primitives, like 66 * &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying 67 * fence to be updated. 68 * 69 * - Then there's also implicit fencing, where the synchronization points are 70 * implicitly passed around as part of shared &dma_buf instances. Such 71 * implicit fences are stored in &struct reservation_object through the 72 * &dma_buf.resv pointer. 73 */ 74 75 static const char *dma_fence_stub_get_name(struct dma_fence *fence) 76 { 77 return "stub"; 78 } 79 80 static const struct dma_fence_ops dma_fence_stub_ops = { 81 .get_driver_name = dma_fence_stub_get_name, 82 .get_timeline_name = dma_fence_stub_get_name, 83 }; 84 85 /** 86 * dma_fence_get_stub - return a signaled fence 87 * 88 * Return a stub fence which is already signaled. 89 */ 90 struct dma_fence *dma_fence_get_stub(void) 91 { 92 spin_lock(&dma_fence_stub_lock); 93 if (!dma_fence_stub.ops) { 94 dma_fence_init(&dma_fence_stub, 95 &dma_fence_stub_ops, 96 &dma_fence_stub_lock, 97 0, 0); 98 dma_fence_signal_locked(&dma_fence_stub); 99 } 100 spin_unlock(&dma_fence_stub_lock); 101 102 return dma_fence_get(&dma_fence_stub); 103 } 104 EXPORT_SYMBOL(dma_fence_get_stub); 105 106 /** 107 * dma_fence_context_alloc - allocate an array of fence contexts 108 * @num: amount of contexts to allocate 109 * 110 * This function will return the first index of the number of fence contexts 111 * allocated. The fence context is used for setting &dma_fence.context to a 112 * unique number by passing the context to dma_fence_init(). 113 */ 114 u64 dma_fence_context_alloc(unsigned num) 115 { 116 WARN_ON(!num); 117 return atomic64_add_return(num, &dma_fence_context_counter) - num; 118 } 119 EXPORT_SYMBOL(dma_fence_context_alloc); 120 121 /** 122 * dma_fence_signal_locked - signal completion of a fence 123 * @fence: the fence to signal 124 * 125 * Signal completion for software callbacks on a fence, this will unblock 126 * dma_fence_wait() calls and run all the callbacks added with 127 * dma_fence_add_callback(). Can be called multiple times, but since a fence 128 * can only go from the unsignaled to the signaled state and not back, it will 129 * only be effective the first time. 130 * 131 * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock 132 * held. 133 * 134 * Returns 0 on success and a negative error value when @fence has been 135 * signalled already. 136 */ 137 int dma_fence_signal_locked(struct dma_fence *fence) 138 { 139 struct dma_fence_cb *cur, *tmp; 140 int ret = 0; 141 142 lockdep_assert_held(fence->lock); 143 144 if (WARN_ON(!fence)) 145 return -EINVAL; 146 147 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { 148 ret = -EINVAL; 149 150 /* 151 * we might have raced with the unlocked dma_fence_signal, 152 * still run through all callbacks 153 */ 154 } else { 155 fence->timestamp = ktime_get(); 156 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags); 157 trace_dma_fence_signaled(fence); 158 } 159 160 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) { 161 list_del_init(&cur->node); 162 cur->func(fence, cur); 163 } 164 return ret; 165 } 166 EXPORT_SYMBOL(dma_fence_signal_locked); 167 168 /** 169 * dma_fence_signal - signal completion of a fence 170 * @fence: the fence to signal 171 * 172 * Signal completion for software callbacks on a fence, this will unblock 173 * dma_fence_wait() calls and run all the callbacks added with 174 * dma_fence_add_callback(). Can be called multiple times, but since a fence 175 * can only go from the unsignaled to the signaled state and not back, it will 176 * only be effective the first time. 177 * 178 * Returns 0 on success and a negative error value when @fence has been 179 * signalled already. 180 */ 181 int dma_fence_signal(struct dma_fence *fence) 182 { 183 unsigned long flags; 184 185 if (!fence) 186 return -EINVAL; 187 188 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 189 return -EINVAL; 190 191 fence->timestamp = ktime_get(); 192 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags); 193 trace_dma_fence_signaled(fence); 194 195 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) { 196 struct dma_fence_cb *cur, *tmp; 197 198 spin_lock_irqsave(fence->lock, flags); 199 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) { 200 list_del_init(&cur->node); 201 cur->func(fence, cur); 202 } 203 spin_unlock_irqrestore(fence->lock, flags); 204 } 205 return 0; 206 } 207 EXPORT_SYMBOL(dma_fence_signal); 208 209 /** 210 * dma_fence_wait_timeout - sleep until the fence gets signaled 211 * or until timeout elapses 212 * @fence: the fence to wait on 213 * @intr: if true, do an interruptible wait 214 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 215 * 216 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the 217 * remaining timeout in jiffies on success. Other error values may be 218 * returned on custom implementations. 219 * 220 * Performs a synchronous wait on this fence. It is assumed the caller 221 * directly or indirectly (buf-mgr between reservation and committing) 222 * holds a reference to the fence, otherwise the fence might be 223 * freed before return, resulting in undefined behavior. 224 * 225 * See also dma_fence_wait() and dma_fence_wait_any_timeout(). 226 */ 227 signed long 228 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout) 229 { 230 signed long ret; 231 232 if (WARN_ON(timeout < 0)) 233 return -EINVAL; 234 235 trace_dma_fence_wait_start(fence); 236 if (fence->ops->wait) 237 ret = fence->ops->wait(fence, intr, timeout); 238 else 239 ret = dma_fence_default_wait(fence, intr, timeout); 240 trace_dma_fence_wait_end(fence); 241 return ret; 242 } 243 EXPORT_SYMBOL(dma_fence_wait_timeout); 244 245 /** 246 * dma_fence_release - default relese function for fences 247 * @kref: &dma_fence.recfount 248 * 249 * This is the default release functions for &dma_fence. Drivers shouldn't call 250 * this directly, but instead call dma_fence_put(). 251 */ 252 void dma_fence_release(struct kref *kref) 253 { 254 struct dma_fence *fence = 255 container_of(kref, struct dma_fence, refcount); 256 257 trace_dma_fence_destroy(fence); 258 259 /* Failed to signal before release, could be a refcounting issue */ 260 WARN_ON(!list_empty(&fence->cb_list)); 261 262 if (fence->ops->release) 263 fence->ops->release(fence); 264 else 265 dma_fence_free(fence); 266 } 267 EXPORT_SYMBOL(dma_fence_release); 268 269 /** 270 * dma_fence_free - default release function for &dma_fence. 271 * @fence: fence to release 272 * 273 * This is the default implementation for &dma_fence_ops.release. It calls 274 * kfree_rcu() on @fence. 275 */ 276 void dma_fence_free(struct dma_fence *fence) 277 { 278 kfree_rcu(fence, rcu); 279 } 280 EXPORT_SYMBOL(dma_fence_free); 281 282 /** 283 * dma_fence_enable_sw_signaling - enable signaling on fence 284 * @fence: the fence to enable 285 * 286 * This will request for sw signaling to be enabled, to make the fence 287 * complete as soon as possible. This calls &dma_fence_ops.enable_signaling 288 * internally. 289 */ 290 void dma_fence_enable_sw_signaling(struct dma_fence *fence) 291 { 292 unsigned long flags; 293 294 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 295 &fence->flags) && 296 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && 297 fence->ops->enable_signaling) { 298 trace_dma_fence_enable_signal(fence); 299 300 spin_lock_irqsave(fence->lock, flags); 301 302 if (!fence->ops->enable_signaling(fence)) 303 dma_fence_signal_locked(fence); 304 305 spin_unlock_irqrestore(fence->lock, flags); 306 } 307 } 308 EXPORT_SYMBOL(dma_fence_enable_sw_signaling); 309 310 /** 311 * dma_fence_add_callback - add a callback to be called when the fence 312 * is signaled 313 * @fence: the fence to wait on 314 * @cb: the callback to register 315 * @func: the function to call 316 * 317 * @cb will be initialized by dma_fence_add_callback(), no initialization 318 * by the caller is required. Any number of callbacks can be registered 319 * to a fence, but a callback can only be registered to one fence at a time. 320 * 321 * Note that the callback can be called from an atomic context. If 322 * fence is already signaled, this function will return -ENOENT (and 323 * *not* call the callback). 324 * 325 * Add a software callback to the fence. Same restrictions apply to 326 * refcount as it does to dma_fence_wait(), however the caller doesn't need to 327 * keep a refcount to fence afterward dma_fence_add_callback() has returned: 328 * when software access is enabled, the creator of the fence is required to keep 329 * the fence alive until after it signals with dma_fence_signal(). The callback 330 * itself can be called from irq context. 331 * 332 * Returns 0 in case of success, -ENOENT if the fence is already signaled 333 * and -EINVAL in case of error. 334 */ 335 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb, 336 dma_fence_func_t func) 337 { 338 unsigned long flags; 339 int ret = 0; 340 bool was_set; 341 342 if (WARN_ON(!fence || !func)) 343 return -EINVAL; 344 345 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { 346 INIT_LIST_HEAD(&cb->node); 347 return -ENOENT; 348 } 349 350 spin_lock_irqsave(fence->lock, flags); 351 352 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 353 &fence->flags); 354 355 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 356 ret = -ENOENT; 357 else if (!was_set && fence->ops->enable_signaling) { 358 trace_dma_fence_enable_signal(fence); 359 360 if (!fence->ops->enable_signaling(fence)) { 361 dma_fence_signal_locked(fence); 362 ret = -ENOENT; 363 } 364 } 365 366 if (!ret) { 367 cb->func = func; 368 list_add_tail(&cb->node, &fence->cb_list); 369 } else 370 INIT_LIST_HEAD(&cb->node); 371 spin_unlock_irqrestore(fence->lock, flags); 372 373 return ret; 374 } 375 EXPORT_SYMBOL(dma_fence_add_callback); 376 377 /** 378 * dma_fence_get_status - returns the status upon completion 379 * @fence: the dma_fence to query 380 * 381 * This wraps dma_fence_get_status_locked() to return the error status 382 * condition on a signaled fence. See dma_fence_get_status_locked() for more 383 * details. 384 * 385 * Returns 0 if the fence has not yet been signaled, 1 if the fence has 386 * been signaled without an error condition, or a negative error code 387 * if the fence has been completed in err. 388 */ 389 int dma_fence_get_status(struct dma_fence *fence) 390 { 391 unsigned long flags; 392 int status; 393 394 spin_lock_irqsave(fence->lock, flags); 395 status = dma_fence_get_status_locked(fence); 396 spin_unlock_irqrestore(fence->lock, flags); 397 398 return status; 399 } 400 EXPORT_SYMBOL(dma_fence_get_status); 401 402 /** 403 * dma_fence_remove_callback - remove a callback from the signaling list 404 * @fence: the fence to wait on 405 * @cb: the callback to remove 406 * 407 * Remove a previously queued callback from the fence. This function returns 408 * true if the callback is successfully removed, or false if the fence has 409 * already been signaled. 410 * 411 * *WARNING*: 412 * Cancelling a callback should only be done if you really know what you're 413 * doing, since deadlocks and race conditions could occur all too easily. For 414 * this reason, it should only ever be done on hardware lockup recovery, 415 * with a reference held to the fence. 416 * 417 * Behaviour is undefined if @cb has not been added to @fence using 418 * dma_fence_add_callback() beforehand. 419 */ 420 bool 421 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb) 422 { 423 unsigned long flags; 424 bool ret; 425 426 spin_lock_irqsave(fence->lock, flags); 427 428 ret = !list_empty(&cb->node); 429 if (ret) 430 list_del_init(&cb->node); 431 432 spin_unlock_irqrestore(fence->lock, flags); 433 434 return ret; 435 } 436 EXPORT_SYMBOL(dma_fence_remove_callback); 437 438 struct default_wait_cb { 439 struct dma_fence_cb base; 440 struct task_struct *task; 441 }; 442 443 static void 444 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb) 445 { 446 struct default_wait_cb *wait = 447 container_of(cb, struct default_wait_cb, base); 448 449 wake_up_state(wait->task, TASK_NORMAL); 450 } 451 452 /** 453 * dma_fence_default_wait - default sleep until the fence gets signaled 454 * or until timeout elapses 455 * @fence: the fence to wait on 456 * @intr: if true, do an interruptible wait 457 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 458 * 459 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the 460 * remaining timeout in jiffies on success. If timeout is zero the value one is 461 * returned if the fence is already signaled for consistency with other 462 * functions taking a jiffies timeout. 463 */ 464 signed long 465 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout) 466 { 467 struct default_wait_cb cb; 468 unsigned long flags; 469 signed long ret = timeout ? timeout : 1; 470 bool was_set; 471 472 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 473 return ret; 474 475 spin_lock_irqsave(fence->lock, flags); 476 477 if (intr && signal_pending(current)) { 478 ret = -ERESTARTSYS; 479 goto out; 480 } 481 482 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 483 &fence->flags); 484 485 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 486 goto out; 487 488 if (!was_set && fence->ops->enable_signaling) { 489 trace_dma_fence_enable_signal(fence); 490 491 if (!fence->ops->enable_signaling(fence)) { 492 dma_fence_signal_locked(fence); 493 goto out; 494 } 495 } 496 497 if (!timeout) { 498 ret = 0; 499 goto out; 500 } 501 502 cb.base.func = dma_fence_default_wait_cb; 503 cb.task = current; 504 list_add(&cb.base.node, &fence->cb_list); 505 506 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) { 507 if (intr) 508 __set_current_state(TASK_INTERRUPTIBLE); 509 else 510 __set_current_state(TASK_UNINTERRUPTIBLE); 511 spin_unlock_irqrestore(fence->lock, flags); 512 513 ret = schedule_timeout(ret); 514 515 spin_lock_irqsave(fence->lock, flags); 516 if (ret > 0 && intr && signal_pending(current)) 517 ret = -ERESTARTSYS; 518 } 519 520 if (!list_empty(&cb.base.node)) 521 list_del(&cb.base.node); 522 __set_current_state(TASK_RUNNING); 523 524 out: 525 spin_unlock_irqrestore(fence->lock, flags); 526 return ret; 527 } 528 EXPORT_SYMBOL(dma_fence_default_wait); 529 530 static bool 531 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count, 532 uint32_t *idx) 533 { 534 int i; 535 536 for (i = 0; i < count; ++i) { 537 struct dma_fence *fence = fences[i]; 538 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { 539 if (idx) 540 *idx = i; 541 return true; 542 } 543 } 544 return false; 545 } 546 547 /** 548 * dma_fence_wait_any_timeout - sleep until any fence gets signaled 549 * or until timeout elapses 550 * @fences: array of fences to wait on 551 * @count: number of fences to wait on 552 * @intr: if true, do an interruptible wait 553 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT 554 * @idx: used to store the first signaled fence index, meaningful only on 555 * positive return 556 * 557 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if 558 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies 559 * on success. 560 * 561 * Synchronous waits for the first fence in the array to be signaled. The 562 * caller needs to hold a reference to all fences in the array, otherwise a 563 * fence might be freed before return, resulting in undefined behavior. 564 * 565 * See also dma_fence_wait() and dma_fence_wait_timeout(). 566 */ 567 signed long 568 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count, 569 bool intr, signed long timeout, uint32_t *idx) 570 { 571 struct default_wait_cb *cb; 572 signed long ret = timeout; 573 unsigned i; 574 575 if (WARN_ON(!fences || !count || timeout < 0)) 576 return -EINVAL; 577 578 if (timeout == 0) { 579 for (i = 0; i < count; ++i) 580 if (dma_fence_is_signaled(fences[i])) { 581 if (idx) 582 *idx = i; 583 return 1; 584 } 585 586 return 0; 587 } 588 589 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL); 590 if (cb == NULL) { 591 ret = -ENOMEM; 592 goto err_free_cb; 593 } 594 595 for (i = 0; i < count; ++i) { 596 struct dma_fence *fence = fences[i]; 597 598 cb[i].task = current; 599 if (dma_fence_add_callback(fence, &cb[i].base, 600 dma_fence_default_wait_cb)) { 601 /* This fence is already signaled */ 602 if (idx) 603 *idx = i; 604 goto fence_rm_cb; 605 } 606 } 607 608 while (ret > 0) { 609 if (intr) 610 set_current_state(TASK_INTERRUPTIBLE); 611 else 612 set_current_state(TASK_UNINTERRUPTIBLE); 613 614 if (dma_fence_test_signaled_any(fences, count, idx)) 615 break; 616 617 ret = schedule_timeout(ret); 618 619 if (ret > 0 && intr && signal_pending(current)) 620 ret = -ERESTARTSYS; 621 } 622 623 __set_current_state(TASK_RUNNING); 624 625 fence_rm_cb: 626 while (i-- > 0) 627 dma_fence_remove_callback(fences[i], &cb[i].base); 628 629 err_free_cb: 630 kfree(cb); 631 632 return ret; 633 } 634 EXPORT_SYMBOL(dma_fence_wait_any_timeout); 635 636 /** 637 * dma_fence_init - Initialize a custom fence. 638 * @fence: the fence to initialize 639 * @ops: the dma_fence_ops for operations on this fence 640 * @lock: the irqsafe spinlock to use for locking this fence 641 * @context: the execution context this fence is run on 642 * @seqno: a linear increasing sequence number for this context 643 * 644 * Initializes an allocated fence, the caller doesn't have to keep its 645 * refcount after committing with this fence, but it will need to hold a 646 * refcount again if &dma_fence_ops.enable_signaling gets called. 647 * 648 * context and seqno are used for easy comparison between fences, allowing 649 * to check which fence is later by simply using dma_fence_later(). 650 */ 651 void 652 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, 653 spinlock_t *lock, u64 context, u64 seqno) 654 { 655 BUG_ON(!lock); 656 BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name); 657 658 kref_init(&fence->refcount); 659 fence->ops = ops; 660 INIT_LIST_HEAD(&fence->cb_list); 661 fence->lock = lock; 662 fence->context = context; 663 fence->seqno = seqno; 664 fence->flags = 0UL; 665 fence->error = 0; 666 667 trace_dma_fence_init(fence); 668 } 669 EXPORT_SYMBOL(dma_fence_init); 670