1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include "xe_hw_fence.h"
8 #include <linux/device.h>
9 #include <linux/slab.h>
12 #include "xe_device.h"
14 #include "xe_hw_engine.h"
15 #include "xe_macros.h"
19 static struct kmem_cache *xe_hw_fence_slab;
21 int __init xe_hw_fence_module_init(void)
23 xe_hw_fence_slab = kmem_cache_create("xe_hw_fence",
24 sizeof(struct xe_hw_fence), 0,
25 SLAB_HWCACHE_ALIGN, NULL);
26 if (!xe_hw_fence_slab)
32 void xe_hw_fence_module_exit(void)
35 kmem_cache_destroy(xe_hw_fence_slab);
38 static struct xe_hw_fence *fence_alloc(void)
40 return kmem_cache_zalloc(xe_hw_fence_slab, GFP_KERNEL);
43 static void fence_free(struct rcu_head *rcu)
45 struct xe_hw_fence *fence =
46 container_of(rcu, struct xe_hw_fence, dma.rcu);
48 if (!WARN_ON_ONCE(!fence))
49 kmem_cache_free(xe_hw_fence_slab, fence);
52 static void hw_fence_irq_run_cb(struct irq_work *work)
54 struct xe_hw_fence_irq *irq = container_of(work, typeof(*irq), work);
55 struct xe_hw_fence *fence, *next;
58 tmp = dma_fence_begin_signalling();
59 spin_lock(&irq->lock);
61 list_for_each_entry_safe(fence, next, &irq->pending, irq_link) {
62 struct dma_fence *dma_fence = &fence->dma;
64 trace_xe_hw_fence_try_signal(fence);
65 if (dma_fence_is_signaled_locked(dma_fence)) {
66 trace_xe_hw_fence_signal(fence);
67 list_del_init(&fence->irq_link);
68 dma_fence_put(dma_fence);
72 spin_unlock(&irq->lock);
73 dma_fence_end_signalling(tmp);
76 void xe_hw_fence_irq_init(struct xe_hw_fence_irq *irq)
78 spin_lock_init(&irq->lock);
79 init_irq_work(&irq->work, hw_fence_irq_run_cb);
80 INIT_LIST_HEAD(&irq->pending);
84 void xe_hw_fence_irq_finish(struct xe_hw_fence_irq *irq)
86 struct xe_hw_fence *fence, *next;
91 if (XE_WARN_ON(!list_empty(&irq->pending))) {
92 tmp = dma_fence_begin_signalling();
93 spin_lock_irqsave(&irq->lock, flags);
94 list_for_each_entry_safe(fence, next, &irq->pending, irq_link) {
95 list_del_init(&fence->irq_link);
96 err = dma_fence_signal_locked(&fence->dma);
97 dma_fence_put(&fence->dma);
100 spin_unlock_irqrestore(&irq->lock, flags);
101 dma_fence_end_signalling(tmp);
105 void xe_hw_fence_irq_run(struct xe_hw_fence_irq *irq)
107 irq_work_queue(&irq->work);
110 void xe_hw_fence_irq_stop(struct xe_hw_fence_irq *irq)
112 spin_lock_irq(&irq->lock);
113 irq->enabled = false;
114 spin_unlock_irq(&irq->lock);
117 void xe_hw_fence_irq_start(struct xe_hw_fence_irq *irq)
119 spin_lock_irq(&irq->lock);
121 spin_unlock_irq(&irq->lock);
123 irq_work_queue(&irq->work);
126 void xe_hw_fence_ctx_init(struct xe_hw_fence_ctx *ctx, struct xe_gt *gt,
127 struct xe_hw_fence_irq *irq, const char *name)
131 ctx->dma_fence_ctx = dma_fence_context_alloc(1);
132 ctx->next_seqno = XE_FENCE_INITIAL_SEQNO;
133 snprintf(ctx->name, sizeof(ctx->name), "%s", name);
136 void xe_hw_fence_ctx_finish(struct xe_hw_fence_ctx *ctx)
140 static struct xe_hw_fence *to_xe_hw_fence(struct dma_fence *fence);
142 static struct xe_hw_fence_irq *xe_hw_fence_irq(struct xe_hw_fence *fence)
144 return container_of(fence->dma.lock, struct xe_hw_fence_irq, lock);
147 static const char *xe_hw_fence_get_driver_name(struct dma_fence *dma_fence)
149 struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
151 return dev_name(fence->xe->drm.dev);
154 static const char *xe_hw_fence_get_timeline_name(struct dma_fence *dma_fence)
156 struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
161 static bool xe_hw_fence_signaled(struct dma_fence *dma_fence)
163 struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
164 struct xe_device *xe = fence->xe;
165 u32 seqno = xe_map_rd(xe, &fence->seqno_map, 0, u32);
167 return dma_fence->error ||
168 !__dma_fence_is_later(dma_fence->seqno, seqno, dma_fence->ops);
171 static bool xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
173 struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
174 struct xe_hw_fence_irq *irq = xe_hw_fence_irq(fence);
176 dma_fence_get(dma_fence);
177 list_add_tail(&fence->irq_link, &irq->pending);
179 /* SW completed (no HW IRQ) so kick handler to signal fence */
180 if (xe_hw_fence_signaled(dma_fence))
181 xe_hw_fence_irq_run(irq);
186 static void xe_hw_fence_release(struct dma_fence *dma_fence)
188 struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
190 XE_WARN_ON(!list_empty(&fence->irq_link));
191 call_rcu(&dma_fence->rcu, fence_free);
194 static const struct dma_fence_ops xe_hw_fence_ops = {
195 .get_driver_name = xe_hw_fence_get_driver_name,
196 .get_timeline_name = xe_hw_fence_get_timeline_name,
197 .enable_signaling = xe_hw_fence_enable_signaling,
198 .signaled = xe_hw_fence_signaled,
199 .release = xe_hw_fence_release,
202 static struct xe_hw_fence *to_xe_hw_fence(struct dma_fence *fence)
204 if (XE_WARN_ON(fence->ops != &xe_hw_fence_ops))
207 return container_of(fence, struct xe_hw_fence, dma);
211 * xe_hw_fence_alloc() - Allocate an hw fence.
213 * Allocate but don't initialize an hw fence.
215 * Return: Pointer to the allocated fence or
216 * negative error pointer on error.
218 struct dma_fence *xe_hw_fence_alloc(void)
220 struct xe_hw_fence *hw_fence = fence_alloc();
223 return ERR_PTR(-ENOMEM);
225 return &hw_fence->dma;
229 * xe_hw_fence_free() - Free an hw fence.
230 * @fence: Pointer to the fence to free.
232 * Frees an hw fence that hasn't yet been
235 void xe_hw_fence_free(struct dma_fence *fence)
237 fence_free(&fence->rcu);
241 * xe_hw_fence_init() - Initialize an hw fence.
242 * @fence: Pointer to the fence to initialize.
243 * @ctx: Pointer to the struct xe_hw_fence_ctx fence context.
244 * @seqno_map: Pointer to the map into where the seqno is blitted.
246 * Initializes a pre-allocated hw fence.
247 * After initialization, the fence is subject to normal
248 * dma-fence refcounting.
250 void xe_hw_fence_init(struct dma_fence *fence, struct xe_hw_fence_ctx *ctx,
251 struct iosys_map seqno_map)
253 struct xe_hw_fence *hw_fence =
254 container_of(fence, typeof(*hw_fence), dma);
256 hw_fence->xe = gt_to_xe(ctx->gt);
257 snprintf(hw_fence->name, sizeof(hw_fence->name), "%s", ctx->name);
258 hw_fence->seqno_map = seqno_map;
259 INIT_LIST_HEAD(&hw_fence->irq_link);
261 dma_fence_init(fence, &xe_hw_fence_ops, &ctx->irq->lock,
262 ctx->dma_fence_ctx, ctx->next_seqno++);
264 trace_xe_hw_fence_create(hw_fence);