sys/dev/drm/linux_fence.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | /* * Copyright (c) 2019-2020 Jonathan Gray <jsg@openbsd.org> * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include <linux/slab.h> #include <linux/dma-fence.h> void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, spinlock_t *lock, u64 context, unsigned seqno) { fence->ops = ops; fence->lock = lock; fence->context = context; fence->seqno = seqno; fence->flags = 0; fence->error = 0; kref_init(&fence->refcount); INIT_LIST_HEAD(&fence->cb_list); } void dma_fence_release(struct kref *ref) { struct dma_fence *fence = container_of(ref, struct dma_fence, refcount); if (fence->ops && fence->ops->release) fence->ops->release(fence); else kfree(fence); } long dma_fence_wait_timeout(struct dma_fence *fence, bool intr, long timeout) { if (timeout < 0) return -EINVAL; if (fence->ops->wait) return fence->ops->wait(fence, intr, timeout); else return dma_fence_default_wait(fence, intr, timeout); } static atomic64_t drm_fence_context_count = ATOMIC_INIT(1); u64 dma_fence_context_alloc(unsigned num) { return atomic64_add_return(num, &drm_fence_context_count) - num; } struct default_wait_cb { struct dma_fence_cb base; struct task_struct *task; }; static void dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb) { struct default_wait_cb *wait = container_of(cb, struct default_wait_cb, base); wake_up_process(wait->task); } long dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout) { long ret = timeout ? timeout : 1; unsigned long end; int err; struct default_wait_cb cb; bool was_set; if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) return ret; crit_enter(); lockmgr(fence->lock, LK_EXCLUSIVE); was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags); if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) goto out; if (!was_set && fence->ops->enable_signaling) { if (!fence->ops->enable_signaling(fence)) { dma_fence_signal_locked(fence); goto out; } } if (timeout == 0) { ret = 0; goto out; } cb.base.func = dma_fence_default_wait_cb; cb.task = current; list_add(&cb.base.node, &fence->cb_list); end = jiffies + timeout; for (ret = timeout; ret > 0; ret = MAX(0, end - jiffies)) { if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { break; } if (intr) { __set_current_state(TASK_INTERRUPTIBLE); } else { __set_current_state(TASK_UNINTERRUPTIBLE); } crit_exit(); /* wake_up_process() directly uses task_struct pointers as sleep identifiers */ err = lksleep(current, fence->lock, intr ? PCATCH : 0, "dmafence", ret); crit_enter(); if (err == EINTR || err == ERESTART) { ret = -ERESTARTSYS; break; } } if (!list_empty(&cb.base.node)) list_del(&cb.base.node); __set_current_state(TASK_RUNNING); out: crit_exit(); lockmgr(fence->lock, LK_RELEASE); return ret; } static bool dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count, uint32_t *idx) { int i; for (i = 0; i < count; ++i) { struct dma_fence *fence = fences[i]; if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { if (idx) *idx = i; return true; } } return false; } long dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count, bool intr, long timeout, uint32_t *idx) { struct default_wait_cb *cb; long ret = timeout; unsigned long end; int i, err; if (timeout == 0) { for (i = 0; i < count; i++) { if (dma_fence_is_signaled(fences[i])) { if (idx) *idx = i; return 1; } } return 0; } cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL); if (cb == NULL) return -ENOMEM; for (i = 0; i < count; i++) { struct dma_fence *fence = fences[i]; cb[i].task = current; if (dma_fence_add_callback(fence, &cb[i].base, dma_fence_default_wait_cb)) { if (idx) *idx = i; goto cb_cleanup; } } end = jiffies + timeout; for (ret = timeout; ret > 0; ret = MAX(0, end - jiffies)) { if (dma_fence_test_signaled_any(fences, count, idx)) break; err = tsleep(current, intr ? PCATCH : 0, "dfwat", ret); if (err == EINTR || err == ERESTART) { ret = -ERESTARTSYS; break; } } cb_cleanup: while (i-- > 0) dma_fence_remove_callback(fences[i], &cb[i].base); kfree(cb); return ret; } int dma_fence_signal_locked(struct dma_fence *fence) { struct dma_fence_cb *cur, *tmp; struct list_head cb_list; if (fence == NULL) return -EINVAL; if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) return -EINVAL; list_replace(&fence->cb_list, &cb_list); fence->timestamp = ktime_get(); set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags); list_for_each_entry_safe(cur, tmp, &cb_list, node) { INIT_LIST_HEAD(&cur->node); cur->func(fence, cur); } return 0; } int dma_fence_signal(struct dma_fence *fence) { int r; if (fence == NULL) return -EINVAL; crit_enter(); lockmgr(fence->lock, LK_EXCLUSIVE); r = dma_fence_signal_locked(fence); lockmgr(fence->lock, LK_RELEASE); crit_exit(); return r; } void dma_fence_enable_sw_signaling(struct dma_fence *fence) { if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags) && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && fence->ops->enable_signaling) { crit_enter(); lockmgr(fence->lock, LK_EXCLUSIVE); if (!fence->ops->enable_signaling(fence)) dma_fence_signal_locked(fence); lockmgr(fence->lock, LK_RELEASE); crit_exit(); } } int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb, dma_fence_func_t func) { int ret = 0; bool was_set; if (WARN_ON(!fence || !func)) return -EINVAL; if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { INIT_LIST_HEAD(&cb->node); return -ENOENT; } crit_enter(); lockmgr(fence->lock, LK_EXCLUSIVE); was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags); if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) ret = -ENOENT; else if (!was_set && fence->ops->enable_signaling) { if (!fence->ops->enable_signaling(fence)) { dma_fence_signal_locked(fence); ret = -ENOENT; } } if (!ret) { cb->func = func; list_add_tail(&cb->node, &fence->cb_list); } else INIT_LIST_HEAD(&cb->node); lockmgr(fence->lock, LK_RELEASE); crit_exit(); return ret; } bool dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb) { bool ret; crit_enter(); lockmgr(fence->lock, LK_EXCLUSIVE); ret = !list_empty(&cb->node); if (ret) list_del_init(&cb->node); lockmgr(fence->lock, LK_RELEASE); crit_exit(); return ret; } void dma_fence_free(struct dma_fence *fence) { kfree(fence); } |