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