2023-03-30 21:31:57 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/*
|
|
|
|
* Copyright © 2022 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
2023-02-25 00:15:38 +00:00
|
|
|
#include "xe_preempt_fence.h"
|
|
|
|
|
2023-03-30 21:31:57 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
|
2023-08-01 10:28:14 +00:00
|
|
|
#include "xe_exec_queue.h"
|
2023-03-30 21:31:57 +00:00
|
|
|
#include "xe_vm.h"
|
|
|
|
|
|
|
|
static void preempt_fence_work_func(struct work_struct *w)
|
|
|
|
{
|
|
|
|
bool cookie = dma_fence_begin_signalling();
|
|
|
|
struct xe_preempt_fence *pfence =
|
|
|
|
container_of(w, typeof(*pfence), preempt_work);
|
2023-07-31 15:30:02 +00:00
|
|
|
struct xe_exec_queue *q = pfence->q;
|
2023-03-30 21:31:57 +00:00
|
|
|
|
|
|
|
if (pfence->error)
|
|
|
|
dma_fence_set_error(&pfence->base, pfence->error);
|
|
|
|
else
|
2023-07-31 15:30:02 +00:00
|
|
|
q->ops->suspend_wait(q);
|
2023-03-30 21:31:57 +00:00
|
|
|
|
|
|
|
dma_fence_signal(&pfence->base);
|
2024-04-18 14:46:31 +00:00
|
|
|
/*
|
|
|
|
* Opt for keep everything in the fence critical section. This looks really strange since we
|
|
|
|
* have just signalled the fence, however the preempt fences are all signalled via single
|
|
|
|
* global ordered-wq, therefore anything that happens in this callback can easily block
|
|
|
|
* progress on the entire wq, which itself may prevent other published preempt fences from
|
|
|
|
* ever signalling. Therefore try to keep everything here in the callback in the fence
|
|
|
|
* critical section. For example if something below grabs a scary lock like vm->lock,
|
|
|
|
* lockdep should complain since we also hold that lock whilst waiting on preempt fences to
|
|
|
|
* complete.
|
|
|
|
*/
|
2023-07-31 15:30:02 +00:00
|
|
|
xe_vm_queue_rebind_worker(q->vm);
|
|
|
|
xe_exec_queue_put(q);
|
2024-04-18 14:46:31 +00:00
|
|
|
dma_fence_end_signalling(cookie);
|
2023-03-30 21:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
preempt_fence_get_driver_name(struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
return "xe";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
preempt_fence_get_timeline_name(struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
return "preempt";
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool preempt_fence_enable_signaling(struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
struct xe_preempt_fence *pfence =
|
|
|
|
container_of(fence, typeof(*pfence), base);
|
2023-07-31 15:30:02 +00:00
|
|
|
struct xe_exec_queue *q = pfence->q;
|
2023-03-30 21:31:57 +00:00
|
|
|
|
2023-07-31 15:30:02 +00:00
|
|
|
pfence->error = q->ops->suspend(q);
|
2024-04-01 22:19:11 +00:00
|
|
|
queue_work(q->vm->xe->preempt_fence_wq, &pfence->preempt_work);
|
2023-03-30 21:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dma_fence_ops preempt_fence_ops = {
|
|
|
|
.get_driver_name = preempt_fence_get_driver_name,
|
|
|
|
.get_timeline_name = preempt_fence_get_timeline_name,
|
|
|
|
.enable_signaling = preempt_fence_enable_signaling,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xe_preempt_fence_alloc() - Allocate a preempt fence with minimal
|
|
|
|
* initialization
|
|
|
|
*
|
|
|
|
* Allocate a preempt fence, and initialize its list head.
|
|
|
|
* If the preempt_fence allocated has been armed with
|
|
|
|
* xe_preempt_fence_arm(), it must be freed using dma_fence_put(). If not,
|
|
|
|
* it must be freed using xe_preempt_fence_free().
|
|
|
|
*
|
|
|
|
* Return: A struct xe_preempt_fence pointer used for calling into
|
|
|
|
* xe_preempt_fence_arm() or xe_preempt_fence_free().
|
|
|
|
* An error pointer on error.
|
|
|
|
*/
|
|
|
|
struct xe_preempt_fence *xe_preempt_fence_alloc(void)
|
|
|
|
{
|
|
|
|
struct xe_preempt_fence *pfence;
|
|
|
|
|
|
|
|
pfence = kmalloc(sizeof(*pfence), GFP_KERNEL);
|
|
|
|
if (!pfence)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&pfence->link);
|
|
|
|
INIT_WORK(&pfence->preempt_work, preempt_fence_work_func);
|
|
|
|
|
|
|
|
return pfence;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xe_preempt_fence_free() - Free a preempt fence allocated using
|
|
|
|
* xe_preempt_fence_alloc().
|
|
|
|
* @pfence: pointer obtained from xe_preempt_fence_alloc();
|
|
|
|
*
|
|
|
|
* Free a preempt fence that has not yet been armed.
|
|
|
|
*/
|
|
|
|
void xe_preempt_fence_free(struct xe_preempt_fence *pfence)
|
|
|
|
{
|
|
|
|
list_del(&pfence->link);
|
|
|
|
kfree(pfence);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xe_preempt_fence_arm() - Arm a preempt fence allocated using
|
|
|
|
* xe_preempt_fence_alloc().
|
|
|
|
* @pfence: The struct xe_preempt_fence pointer returned from
|
|
|
|
* xe_preempt_fence_alloc().
|
2023-07-31 15:30:02 +00:00
|
|
|
* @q: The struct xe_exec_queue used for arming.
|
2023-03-30 21:31:57 +00:00
|
|
|
* @context: The dma-fence context used for arming.
|
|
|
|
* @seqno: The dma-fence seqno used for arming.
|
|
|
|
*
|
|
|
|
* Inserts the preempt fence into @context's timeline, takes @link off any
|
2023-07-31 15:30:02 +00:00
|
|
|
* list, and registers the struct xe_exec_queue as the xe_engine to be preempted.
|
2023-03-30 21:31:57 +00:00
|
|
|
*
|
|
|
|
* Return: A pointer to a struct dma_fence embedded into the preempt fence.
|
|
|
|
* This function doesn't error.
|
|
|
|
*/
|
|
|
|
struct dma_fence *
|
2023-07-31 15:30:02 +00:00
|
|
|
xe_preempt_fence_arm(struct xe_preempt_fence *pfence, struct xe_exec_queue *q,
|
2023-03-30 21:31:57 +00:00
|
|
|
u64 context, u32 seqno)
|
|
|
|
{
|
|
|
|
list_del_init(&pfence->link);
|
2023-07-31 15:30:02 +00:00
|
|
|
pfence->q = xe_exec_queue_get(q);
|
2023-03-30 21:31:57 +00:00
|
|
|
dma_fence_init(&pfence->base, &preempt_fence_ops,
|
2024-06-13 17:03:48 +00:00
|
|
|
&q->lr.lock, context, seqno);
|
2023-03-30 21:31:57 +00:00
|
|
|
|
|
|
|
return &pfence->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* xe_preempt_fence_create() - Helper to create and arm a preempt fence.
|
2023-07-31 15:30:02 +00:00
|
|
|
* @q: The struct xe_exec_queue used for arming.
|
2023-03-30 21:31:57 +00:00
|
|
|
* @context: The dma-fence context used for arming.
|
|
|
|
* @seqno: The dma-fence seqno used for arming.
|
|
|
|
*
|
|
|
|
* Allocates and inserts the preempt fence into @context's timeline,
|
2023-07-31 15:30:02 +00:00
|
|
|
* and registers @e as the struct xe_exec_queue to be preempted.
|
2023-03-30 21:31:57 +00:00
|
|
|
*
|
|
|
|
* Return: A pointer to the resulting struct dma_fence on success. An error
|
|
|
|
* pointer on error. In particular if allocation fails it returns
|
|
|
|
* ERR_PTR(-ENOMEM);
|
|
|
|
*/
|
|
|
|
struct dma_fence *
|
2023-07-31 15:30:02 +00:00
|
|
|
xe_preempt_fence_create(struct xe_exec_queue *q,
|
2023-03-30 21:31:57 +00:00
|
|
|
u64 context, u32 seqno)
|
|
|
|
{
|
|
|
|
struct xe_preempt_fence *pfence;
|
|
|
|
|
|
|
|
pfence = xe_preempt_fence_alloc();
|
|
|
|
if (IS_ERR(pfence))
|
|
|
|
return ERR_CAST(pfence);
|
|
|
|
|
2023-07-31 15:30:02 +00:00
|
|
|
return xe_preempt_fence_arm(pfence, q, context, seqno);
|
2023-03-30 21:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool xe_fence_is_xe_preempt(const struct dma_fence *fence)
|
|
|
|
{
|
|
|
|
return fence->ops == &preempt_fence_ops;
|
|
|
|
}
|