1 // SPDX-License-Identifier: GPL-2.0-only
3 * Syncpoint dma_fence implementation
5 * Copyright (c) 2020, NVIDIA Corporation.
8 #include <linux/dma-fence.h>
9 #include <linux/file.h>
11 #include <linux/slab.h>
12 #include <linux/sync_file.h>
18 static DEFINE_SPINLOCK(lock);
20 struct host1x_syncpt_fence {
21 struct dma_fence base;
25 struct host1x_syncpt *sp;
28 struct host1x_waitlist *waiter;
31 struct delayed_work timeout_work;
34 static const char *host1x_syncpt_fence_get_driver_name(struct dma_fence *f)
39 static const char *host1x_syncpt_fence_get_timeline_name(struct dma_fence *f)
44 static struct host1x_syncpt_fence *to_host1x_fence(struct dma_fence *f)
46 return container_of(f, struct host1x_syncpt_fence, base);
49 static bool host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
51 struct host1x_syncpt_fence *sf = to_host1x_fence(f);
54 if (host1x_syncpt_is_expired(sf->sp, sf->threshold))
60 * The dma_fence framework requires the fence driver to keep a
61 * reference to any fences for which 'enable_signaling' has been
62 * called (and that have not been signalled).
64 * We provide a userspace API to create arbitrary syncpoint fences,
65 * so we cannot normally guarantee that all fences get signalled.
66 * As such, setup a timeout, so that long-lasting fences will get
69 schedule_delayed_work(&sf->timeout_work, msecs_to_jiffies(30000));
71 err = host1x_intr_add_action(sf->sp->host, sf->sp, sf->threshold,
72 HOST1X_INTR_ACTION_SIGNAL_FENCE, f,
73 sf->waiter, &sf->waiter_ref);
75 cancel_delayed_work_sync(&sf->timeout_work);
80 /* intr framework takes ownership of waiter */
84 * The fence may get signalled at any time after the above call,
85 * so we need to initialize all state used by signalling
92 static void host1x_syncpt_fence_release(struct dma_fence *f)
94 struct host1x_syncpt_fence *sf = to_host1x_fence(f);
102 const struct dma_fence_ops host1x_syncpt_fence_ops = {
103 .get_driver_name = host1x_syncpt_fence_get_driver_name,
104 .get_timeline_name = host1x_syncpt_fence_get_timeline_name,
105 .enable_signaling = host1x_syncpt_fence_enable_signaling,
106 .release = host1x_syncpt_fence_release,
109 void host1x_fence_signal(struct host1x_syncpt_fence *f)
111 if (atomic_xchg(&f->signaling, 1))
115 * Cancel pending timeout work - if it races, it will
116 * not get 'f->signaling' and return.
118 cancel_delayed_work_sync(&f->timeout_work);
120 host1x_intr_put_ref(f->sp->host, f->sp->id, f->waiter_ref, false);
122 dma_fence_signal(&f->base);
123 dma_fence_put(&f->base);
126 static void do_fence_timeout(struct work_struct *work)
128 struct delayed_work *dwork = (struct delayed_work *)work;
129 struct host1x_syncpt_fence *f =
130 container_of(dwork, struct host1x_syncpt_fence, timeout_work);
132 if (atomic_xchg(&f->signaling, 1))
136 * Cancel pending timeout work - if it races, it will
137 * not get 'f->signaling' and return.
139 host1x_intr_put_ref(f->sp->host, f->sp->id, f->waiter_ref, true);
141 dma_fence_set_error(&f->base, -ETIMEDOUT);
142 dma_fence_signal(&f->base);
143 dma_fence_put(&f->base);
146 struct dma_fence *host1x_fence_create(struct host1x_syncpt *sp, u32 threshold)
148 struct host1x_syncpt_fence *fence;
150 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
152 return ERR_PTR(-ENOMEM);
154 fence->waiter = kzalloc(sizeof(*fence->waiter), GFP_KERNEL);
155 if (!fence->waiter) {
157 return ERR_PTR(-ENOMEM);
161 fence->threshold = threshold;
163 dma_fence_init(&fence->base, &host1x_syncpt_fence_ops, &lock,
164 dma_fence_context_alloc(1), 0);
166 INIT_DELAYED_WORK(&fence->timeout_work, do_fence_timeout);
170 EXPORT_SYMBOL(host1x_fence_create);