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