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