Merge branch 'akpm' (patches from Andrew)
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / i915_sw_fence_work.c
blob997b2998f1f2158ea53e4b202f55154ecce5cbb4
1 // SPDX-License-Identifier: MIT
3 /*
4 * Copyright © 2019 Intel Corporation
5 */
7 #include "i915_sw_fence_work.h"
9 static void fence_complete(struct dma_fence_work *f)
11 if (f->ops->release)
12 f->ops->release(f);
13 dma_fence_signal(&f->dma);
16 static void fence_work(struct work_struct *work)
18 struct dma_fence_work *f = container_of(work, typeof(*f), work);
19 int err;
21 err = f->ops->work(f);
22 if (err)
23 dma_fence_set_error(&f->dma, err);
25 fence_complete(f);
26 dma_fence_put(&f->dma);
29 static int __i915_sw_fence_call
30 fence_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
32 struct dma_fence_work *f = container_of(fence, typeof(*f), chain);
34 switch (state) {
35 case FENCE_COMPLETE:
36 if (fence->error)
37 dma_fence_set_error(&f->dma, fence->error);
39 if (!f->dma.error) {
40 dma_fence_get(&f->dma);
41 queue_work(system_unbound_wq, &f->work);
42 } else {
43 fence_complete(f);
45 break;
47 case FENCE_FREE:
48 dma_fence_put(&f->dma);
49 break;
52 return NOTIFY_DONE;
55 static const char *get_driver_name(struct dma_fence *fence)
57 return "dma-fence";
60 static const char *get_timeline_name(struct dma_fence *fence)
62 struct dma_fence_work *f = container_of(fence, typeof(*f), dma);
64 return f->ops->name ?: "work";
67 static void fence_release(struct dma_fence *fence)
69 struct dma_fence_work *f = container_of(fence, typeof(*f), dma);
71 i915_sw_fence_fini(&f->chain);
73 BUILD_BUG_ON(offsetof(typeof(*f), dma));
74 dma_fence_free(&f->dma);
77 static const struct dma_fence_ops fence_ops = {
78 .get_driver_name = get_driver_name,
79 .get_timeline_name = get_timeline_name,
80 .release = fence_release,
83 void dma_fence_work_init(struct dma_fence_work *f,
84 const struct dma_fence_work_ops *ops)
86 f->ops = ops;
87 spin_lock_init(&f->lock);
88 dma_fence_init(&f->dma, &fence_ops, &f->lock, 0, 0);
89 i915_sw_fence_init(&f->chain, fence_notify);
90 INIT_WORK(&f->work, fence_work);
93 int dma_fence_work_chain(struct dma_fence_work *f, struct dma_fence *signal)
95 if (!signal)
96 return 0;
98 return __i915_sw_fence_await_dma_fence(&f->chain, signal, &f->cb);