Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 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> |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 24 | #include <linux/dma-fence.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 25 | #include <linux/sched/signal.h> |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 26 | |
| 27 | #define CREATE_TRACE_POINTS |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 28 | #include <trace/events/dma_fence.h> |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 29 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 30 | EXPORT_TRACEPOINT_SYMBOL(dma_fence_annotate_wait_on); |
| 31 | EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit); |
Chris Wilson | 8c96c67 | 2017-01-24 11:57:58 +0000 | [diff] [blame] | 32 | EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 33 | |
Thierry Reding | e9f3b79 | 2014-08-08 12:42:32 +0200 | [diff] [blame] | 34 | /* |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 35 | * fence context counter: each execution context should have its own |
| 36 | * fence context, this allows checking if fences belong to the same |
| 37 | * context or not. One device can have multiple separate contexts, |
| 38 | * and they're used if some engine can run independently of another. |
| 39 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 40 | static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 43 | * dma_fence_context_alloc - allocate an array of fence contexts |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 44 | * @num: [in] amount of contexts to allocate |
| 45 | * |
| 46 | * This function will return the first index of the number of fences allocated. |
| 47 | * The fence context is used for setting fence->context to a unique number. |
| 48 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 49 | u64 dma_fence_context_alloc(unsigned num) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 50 | { |
| 51 | BUG_ON(!num); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 52 | return atomic64_add_return(num, &dma_fence_context_counter) - num; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 53 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 54 | EXPORT_SYMBOL(dma_fence_context_alloc); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 55 | |
| 56 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 57 | * dma_fence_signal_locked - signal completion of a fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 58 | * @fence: the fence to signal |
| 59 | * |
| 60 | * Signal completion for software callbacks on a fence, this will unblock |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 61 | * dma_fence_wait() calls and run all the callbacks added with |
| 62 | * dma_fence_add_callback(). Can be called multiple times, but since a fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 63 | * can only go from unsignaled to signaled state, it will only be effective |
| 64 | * the first time. |
| 65 | * |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 66 | * Unlike dma_fence_signal, this function must be called with fence->lock held. |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 67 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 68 | int dma_fence_signal_locked(struct dma_fence *fence) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 69 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 70 | struct dma_fence_cb *cur, *tmp; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 71 | int ret = 0; |
| 72 | |
Rob Clark | 78010cd | 2016-10-24 15:57:10 -0400 | [diff] [blame] | 73 | lockdep_assert_held(fence->lock); |
| 74 | |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 75 | if (WARN_ON(!fence)) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | if (!ktime_to_ns(fence->timestamp)) { |
| 79 | fence->timestamp = ktime_get(); |
| 80 | smp_mb__before_atomic(); |
| 81 | } |
| 82 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 83 | if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 84 | ret = -EINVAL; |
| 85 | |
| 86 | /* |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 87 | * we might have raced with the unlocked dma_fence_signal, |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 88 | * still run through all callbacks |
| 89 | */ |
| 90 | } else |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 91 | trace_dma_fence_signaled(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 92 | |
| 93 | list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) { |
| 94 | list_del_init(&cur->node); |
| 95 | cur->func(fence, cur); |
| 96 | } |
| 97 | return ret; |
| 98 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 99 | EXPORT_SYMBOL(dma_fence_signal_locked); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 100 | |
| 101 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 102 | * dma_fence_signal - signal completion of a fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 103 | * @fence: the fence to signal |
| 104 | * |
| 105 | * Signal completion for software callbacks on a fence, this will unblock |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 106 | * dma_fence_wait() calls and run all the callbacks added with |
| 107 | * dma_fence_add_callback(). Can be called multiple times, but since a fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 108 | * can only go from unsignaled to signaled state, it will only be effective |
| 109 | * the first time. |
| 110 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 111 | int dma_fence_signal(struct dma_fence *fence) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 112 | { |
| 113 | unsigned long flags; |
| 114 | |
| 115 | if (!fence) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | if (!ktime_to_ns(fence->timestamp)) { |
| 119 | fence->timestamp = ktime_get(); |
| 120 | smp_mb__before_atomic(); |
| 121 | } |
| 122 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 123 | if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 126 | trace_dma_fence_signaled(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 127 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 128 | if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) { |
| 129 | struct dma_fence_cb *cur, *tmp; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 130 | |
| 131 | spin_lock_irqsave(fence->lock, flags); |
| 132 | list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) { |
| 133 | list_del_init(&cur->node); |
| 134 | cur->func(fence, cur); |
| 135 | } |
| 136 | spin_unlock_irqrestore(fence->lock, flags); |
| 137 | } |
| 138 | return 0; |
| 139 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 140 | EXPORT_SYMBOL(dma_fence_signal); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 143 | * dma_fence_wait_timeout - sleep until the fence gets signaled |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 144 | * or until timeout elapses |
| 145 | * @fence: [in] the fence to wait on |
| 146 | * @intr: [in] if true, do an interruptible wait |
| 147 | * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT |
| 148 | * |
| 149 | * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the |
| 150 | * remaining timeout in jiffies on success. Other error values may be |
| 151 | * returned on custom implementations. |
| 152 | * |
| 153 | * Performs a synchronous wait on this fence. It is assumed the caller |
| 154 | * directly or indirectly (buf-mgr between reservation and committing) |
| 155 | * holds a reference to the fence, otherwise the fence might be |
| 156 | * freed before return, resulting in undefined behavior. |
| 157 | */ |
| 158 | signed long |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 159 | dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 160 | { |
| 161 | signed long ret; |
| 162 | |
| 163 | if (WARN_ON(timeout < 0)) |
| 164 | return -EINVAL; |
| 165 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 166 | trace_dma_fence_wait_start(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 167 | ret = fence->ops->wait(fence, intr, timeout); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 168 | trace_dma_fence_wait_end(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 169 | return ret; |
| 170 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 171 | EXPORT_SYMBOL(dma_fence_wait_timeout); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 172 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 173 | void dma_fence_release(struct kref *kref) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 174 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 175 | struct dma_fence *fence = |
| 176 | container_of(kref, struct dma_fence, refcount); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 177 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 178 | trace_dma_fence_destroy(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 179 | |
| 180 | BUG_ON(!list_empty(&fence->cb_list)); |
| 181 | |
| 182 | if (fence->ops->release) |
| 183 | fence->ops->release(fence); |
| 184 | else |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 185 | dma_fence_free(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 186 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 187 | EXPORT_SYMBOL(dma_fence_release); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 188 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 189 | void dma_fence_free(struct dma_fence *fence) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 190 | { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 191 | kfree_rcu(fence, rcu); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 192 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 193 | EXPORT_SYMBOL(dma_fence_free); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 194 | |
| 195 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 196 | * dma_fence_enable_sw_signaling - enable signaling on fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 197 | * @fence: [in] the fence to enable |
| 198 | * |
| 199 | * this will request for sw signaling to be enabled, to make the fence |
| 200 | * complete as soon as possible |
| 201 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 202 | void dma_fence_enable_sw_signaling(struct dma_fence *fence) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 203 | { |
| 204 | unsigned long flags; |
| 205 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 206 | if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, |
| 207 | &fence->flags) && |
| 208 | !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
| 209 | trace_dma_fence_enable_signal(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 210 | |
| 211 | spin_lock_irqsave(fence->lock, flags); |
| 212 | |
| 213 | if (!fence->ops->enable_signaling(fence)) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 214 | dma_fence_signal_locked(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 215 | |
| 216 | spin_unlock_irqrestore(fence->lock, flags); |
| 217 | } |
| 218 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 219 | EXPORT_SYMBOL(dma_fence_enable_sw_signaling); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 220 | |
| 221 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 222 | * dma_fence_add_callback - add a callback to be called when the fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 223 | * is signaled |
| 224 | * @fence: [in] the fence to wait on |
| 225 | * @cb: [in] the callback to register |
| 226 | * @func: [in] the function to call |
| 227 | * |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 228 | * cb will be initialized by dma_fence_add_callback, no initialization |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 229 | * by the caller is required. Any number of callbacks can be registered |
| 230 | * to a fence, but a callback can only be registered to one fence at a time. |
| 231 | * |
| 232 | * Note that the callback can be called from an atomic context. If |
| 233 | * fence is already signaled, this function will return -ENOENT (and |
| 234 | * *not* call the callback) |
| 235 | * |
| 236 | * Add a software callback to the fence. Same restrictions apply to |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 237 | * refcount as it does to dma_fence_wait, however the caller doesn't need to |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 238 | * keep a refcount to fence afterwards: when software access is enabled, |
| 239 | * the creator of the fence is required to keep the fence alive until |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 240 | * after it signals with dma_fence_signal. The callback itself can be called |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 241 | * from irq context. |
| 242 | * |
| 243 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 244 | int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb, |
| 245 | dma_fence_func_t func) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 246 | { |
| 247 | unsigned long flags; |
| 248 | int ret = 0; |
| 249 | bool was_set; |
| 250 | |
| 251 | if (WARN_ON(!fence || !func)) |
| 252 | return -EINVAL; |
| 253 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 254 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 255 | INIT_LIST_HEAD(&cb->node); |
| 256 | return -ENOENT; |
| 257 | } |
| 258 | |
| 259 | spin_lock_irqsave(fence->lock, flags); |
| 260 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 261 | was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, |
| 262 | &fence->flags); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 263 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 264 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 265 | ret = -ENOENT; |
| 266 | else if (!was_set) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 267 | trace_dma_fence_enable_signal(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 268 | |
| 269 | if (!fence->ops->enable_signaling(fence)) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 270 | dma_fence_signal_locked(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 271 | ret = -ENOENT; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (!ret) { |
| 276 | cb->func = func; |
| 277 | list_add_tail(&cb->node, &fence->cb_list); |
| 278 | } else |
| 279 | INIT_LIST_HEAD(&cb->node); |
| 280 | spin_unlock_irqrestore(fence->lock, flags); |
| 281 | |
| 282 | return ret; |
| 283 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 284 | EXPORT_SYMBOL(dma_fence_add_callback); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 285 | |
| 286 | /** |
Chris Wilson | d6c99f4 | 2017-01-04 14:12:21 +0000 | [diff] [blame] | 287 | * dma_fence_get_status - returns the status upon completion |
| 288 | * @fence: [in] the dma_fence to query |
| 289 | * |
| 290 | * This wraps dma_fence_get_status_locked() to return the error status |
| 291 | * condition on a signaled fence. See dma_fence_get_status_locked() for more |
| 292 | * details. |
| 293 | * |
| 294 | * Returns 0 if the fence has not yet been signaled, 1 if the fence has |
| 295 | * been signaled without an error condition, or a negative error code |
| 296 | * if the fence has been completed in err. |
| 297 | */ |
| 298 | int dma_fence_get_status(struct dma_fence *fence) |
| 299 | { |
| 300 | unsigned long flags; |
| 301 | int status; |
| 302 | |
| 303 | spin_lock_irqsave(fence->lock, flags); |
| 304 | status = dma_fence_get_status_locked(fence); |
| 305 | spin_unlock_irqrestore(fence->lock, flags); |
| 306 | |
| 307 | return status; |
| 308 | } |
| 309 | EXPORT_SYMBOL(dma_fence_get_status); |
| 310 | |
| 311 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 312 | * dma_fence_remove_callback - remove a callback from the signaling list |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 313 | * @fence: [in] the fence to wait on |
| 314 | * @cb: [in] the callback to remove |
| 315 | * |
| 316 | * Remove a previously queued callback from the fence. This function returns |
Masanari Iida | f353d71 | 2014-10-22 00:00:14 +0900 | [diff] [blame] | 317 | * true if the callback is successfully removed, or false if the fence has |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 318 | * already been signaled. |
| 319 | * |
| 320 | * *WARNING*: |
| 321 | * Cancelling a callback should only be done if you really know what you're |
| 322 | * doing, since deadlocks and race conditions could occur all too easily. For |
| 323 | * this reason, it should only ever be done on hardware lockup recovery, |
| 324 | * with a reference held to the fence. |
| 325 | */ |
| 326 | bool |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 327 | dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 328 | { |
| 329 | unsigned long flags; |
| 330 | bool ret; |
| 331 | |
| 332 | spin_lock_irqsave(fence->lock, flags); |
| 333 | |
| 334 | ret = !list_empty(&cb->node); |
| 335 | if (ret) |
| 336 | list_del_init(&cb->node); |
| 337 | |
| 338 | spin_unlock_irqrestore(fence->lock, flags); |
| 339 | |
| 340 | return ret; |
| 341 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 342 | EXPORT_SYMBOL(dma_fence_remove_callback); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 343 | |
| 344 | struct default_wait_cb { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 345 | struct dma_fence_cb base; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 346 | struct task_struct *task; |
| 347 | }; |
| 348 | |
| 349 | static void |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 350 | dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 351 | { |
| 352 | struct default_wait_cb *wait = |
| 353 | container_of(cb, struct default_wait_cb, base); |
| 354 | |
| 355 | wake_up_state(wait->task, TASK_NORMAL); |
| 356 | } |
| 357 | |
| 358 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 359 | * dma_fence_default_wait - default sleep until the fence gets signaled |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 360 | * or until timeout elapses |
| 361 | * @fence: [in] the fence to wait on |
| 362 | * @intr: [in] if true, do an interruptible wait |
| 363 | * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT |
| 364 | * |
| 365 | * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the |
Alex Deucher | bcc004b | 2016-11-07 16:16:13 -0500 | [diff] [blame] | 366 | * remaining timeout in jiffies on success. If timeout is zero the value one is |
| 367 | * returned if the fence is already signaled for consistency with other |
| 368 | * functions taking a jiffies timeout. |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 369 | */ |
| 370 | signed long |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 371 | dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 372 | { |
| 373 | struct default_wait_cb cb; |
| 374 | unsigned long flags; |
Alex Deucher | bcc004b | 2016-11-07 16:16:13 -0500 | [diff] [blame] | 375 | signed long ret = timeout ? timeout : 1; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 376 | bool was_set; |
| 377 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 378 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
Alex Deucher | bcc004b | 2016-11-07 16:16:13 -0500 | [diff] [blame] | 379 | return ret; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 380 | |
| 381 | spin_lock_irqsave(fence->lock, flags); |
| 382 | |
| 383 | if (intr && signal_pending(current)) { |
| 384 | ret = -ERESTARTSYS; |
| 385 | goto out; |
| 386 | } |
| 387 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 388 | was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, |
| 389 | &fence->flags); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 390 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 391 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 392 | goto out; |
| 393 | |
| 394 | if (!was_set) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 395 | trace_dma_fence_enable_signal(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 396 | |
| 397 | if (!fence->ops->enable_signaling(fence)) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 398 | dma_fence_signal_locked(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 399 | goto out; |
| 400 | } |
| 401 | } |
| 402 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 403 | cb.base.func = dma_fence_default_wait_cb; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 404 | cb.task = current; |
| 405 | list_add(&cb.base.node, &fence->cb_list); |
| 406 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 407 | while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) { |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 408 | if (intr) |
| 409 | __set_current_state(TASK_INTERRUPTIBLE); |
| 410 | else |
| 411 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 412 | spin_unlock_irqrestore(fence->lock, flags); |
| 413 | |
| 414 | ret = schedule_timeout(ret); |
| 415 | |
| 416 | spin_lock_irqsave(fence->lock, flags); |
| 417 | if (ret > 0 && intr && signal_pending(current)) |
| 418 | ret = -ERESTARTSYS; |
| 419 | } |
| 420 | |
| 421 | if (!list_empty(&cb.base.node)) |
| 422 | list_del(&cb.base.node); |
| 423 | __set_current_state(TASK_RUNNING); |
| 424 | |
| 425 | out: |
| 426 | spin_unlock_irqrestore(fence->lock, flags); |
| 427 | return ret; |
| 428 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 429 | EXPORT_SYMBOL(dma_fence_default_wait); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 430 | |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 431 | static bool |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 432 | dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count, |
| 433 | uint32_t *idx) |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 434 | { |
| 435 | int i; |
| 436 | |
| 437 | for (i = 0; i < count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 438 | struct dma_fence *fence = fences[i]; |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 439 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
| 440 | if (idx) |
| 441 | *idx = i; |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 442 | return true; |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 443 | } |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 449 | * dma_fence_wait_any_timeout - sleep until any fence gets signaled |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 450 | * or until timeout elapses |
| 451 | * @fences: [in] array of fences to wait on |
| 452 | * @count: [in] number of fences to wait on |
| 453 | * @intr: [in] if true, do an interruptible wait |
| 454 | * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 455 | * @idx: [out] the first signaled fence index, meaningful only on |
| 456 | * positive return |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 457 | * |
| 458 | * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if |
| 459 | * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies |
| 460 | * on success. |
| 461 | * |
| 462 | * Synchronous waits for the first fence in the array to be signaled. The |
| 463 | * caller needs to hold a reference to all fences in the array, otherwise a |
| 464 | * fence might be freed before return, resulting in undefined behavior. |
| 465 | */ |
| 466 | signed long |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 467 | dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count, |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 468 | bool intr, signed long timeout, uint32_t *idx) |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 469 | { |
| 470 | struct default_wait_cb *cb; |
| 471 | signed long ret = timeout; |
| 472 | unsigned i; |
| 473 | |
| 474 | if (WARN_ON(!fences || !count || timeout < 0)) |
| 475 | return -EINVAL; |
| 476 | |
| 477 | if (timeout == 0) { |
| 478 | for (i = 0; i < count; ++i) |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 479 | if (dma_fence_is_signaled(fences[i])) { |
| 480 | if (idx) |
| 481 | *idx = i; |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 482 | return 1; |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 483 | } |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL); |
| 489 | if (cb == NULL) { |
| 490 | ret = -ENOMEM; |
| 491 | goto err_free_cb; |
| 492 | } |
| 493 | |
| 494 | for (i = 0; i < count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 495 | struct dma_fence *fence = fences[i]; |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 496 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 497 | if (fence->ops->wait != dma_fence_default_wait) { |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 498 | ret = -EINVAL; |
| 499 | goto fence_rm_cb; |
| 500 | } |
| 501 | |
| 502 | cb[i].task = current; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 503 | if (dma_fence_add_callback(fence, &cb[i].base, |
| 504 | dma_fence_default_wait_cb)) { |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 505 | /* This fence is already signaled */ |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 506 | if (idx) |
| 507 | *idx = i; |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 508 | goto fence_rm_cb; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | while (ret > 0) { |
| 513 | if (intr) |
| 514 | set_current_state(TASK_INTERRUPTIBLE); |
| 515 | else |
| 516 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 517 | |
monk.liu | 7392b4b | 2016-11-04 16:16:09 -0400 | [diff] [blame] | 518 | if (dma_fence_test_signaled_any(fences, count, idx)) |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 519 | break; |
| 520 | |
| 521 | ret = schedule_timeout(ret); |
| 522 | |
| 523 | if (ret > 0 && intr && signal_pending(current)) |
| 524 | ret = -ERESTARTSYS; |
| 525 | } |
| 526 | |
| 527 | __set_current_state(TASK_RUNNING); |
| 528 | |
| 529 | fence_rm_cb: |
| 530 | while (i-- > 0) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 531 | dma_fence_remove_callback(fences[i], &cb[i].base); |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 532 | |
| 533 | err_free_cb: |
| 534 | kfree(cb); |
| 535 | |
| 536 | return ret; |
| 537 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 538 | EXPORT_SYMBOL(dma_fence_wait_any_timeout); |
Christian König | a519435 | 2015-10-20 16:34:16 +0200 | [diff] [blame] | 539 | |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 540 | /** |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 541 | * dma_fence_init - Initialize a custom fence. |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 542 | * @fence: [in] the fence to initialize |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 543 | * @ops: [in] the dma_fence_ops for operations on this fence |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 544 | * @lock: [in] the irqsafe spinlock to use for locking this fence |
| 545 | * @context: [in] the execution context this fence is run on |
| 546 | * @seqno: [in] a linear increasing sequence number for this context |
| 547 | * |
| 548 | * Initializes an allocated fence, the caller doesn't have to keep its |
| 549 | * refcount after committing with this fence, but it will need to hold a |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 550 | * refcount again if dma_fence_ops.enable_signaling gets called. This can |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 551 | * be used for other implementing other types of fence. |
| 552 | * |
| 553 | * context and seqno are used for easy comparison between fences, allowing |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 554 | * to check which fence is later by simply using dma_fence_later. |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 555 | */ |
| 556 | void |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 557 | dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, |
| 558 | spinlock_t *lock, u64 context, unsigned seqno) |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 559 | { |
| 560 | BUG_ON(!lock); |
| 561 | BUG_ON(!ops || !ops->wait || !ops->enable_signaling || |
| 562 | !ops->get_driver_name || !ops->get_timeline_name); |
| 563 | |
| 564 | kref_init(&fence->refcount); |
| 565 | fence->ops = ops; |
| 566 | INIT_LIST_HEAD(&fence->cb_list); |
| 567 | fence->lock = lock; |
| 568 | fence->context = context; |
| 569 | fence->seqno = seqno; |
| 570 | fence->flags = 0UL; |
Chris Wilson | a009e97 | 2017-01-04 14:12:22 +0000 | [diff] [blame] | 571 | fence->error = 0; |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 572 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 573 | trace_dma_fence_init(fence); |
Maarten Lankhorst | e941759 | 2014-07-01 12:57:14 +0200 | [diff] [blame] | 574 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 575 | EXPORT_SYMBOL(dma_fence_init); |