[PATCH v4 09/14] accel/thames: Recover from faults and timeouts
From: Tomeu Vizoso
Date: Sun Nov 29 2026 - 06:37:58 EST
A Kernel that faults, or runs past the 500 ms device timeout the kernel
writes in every Ring entry, ends in its own fault record, which the
firmware fills and userspace reads through the existing query IOCTL; the
Context continues.
The host's timeout, 2000 ms, is longer than the device's: a Job still on
the Ring when it fires is on a core that stalled. The kernel stops and
starts the core through remoteproc, reads the handshake again and, as
the firmware empties the Ring at boot, pushes the Ring's other Jobs
again on their same fences, so a Kernel may run twice; the uAPI says so.
When someone else stops a core through remoteproc, the Jobs on its Ring
fail with -ENODEV. Their memory is not freed until the core attaches
again, since the system firmware may refuse the stop and leave the core
running them.
A Context with 3 faults among its last 10 Jobs is banned: its Jobs not
yet run are cancelled with -ECANCELED and its later submits refused.
The thresholds let a client survive a stray fault and stop one that
faults repeatedly, each fault costing the core a fault path and, on a
timeout, a reset.
Signed-off-by: Tomeu Vizoso <tomeu@xxxxxxxxxxxxxxx>
---
v4:
- Do not move the Ring's head before a reset; the firmware empties the
Ring at boot.
- The remoteproc subdevice and the offline state move to the driver
patch, the scheduler's readiness to the job patch.
- The timeout message gives the Kernel's own run time.
- The uAPI comments state each errno, and that a failed reset takes the
core offline.
---
drivers/accel/thames/thames_core.c | 69 +++++-
drivers/accel/thames/thames_core.h | 9 +
drivers/accel/thames/thames_device.h | 2 +
drivers/accel/thames/thames_drv.c | 33 +++
drivers/accel/thames/thames_drv.h | 6 +
drivers/accel/thames/thames_job.c | 456 ++++++++++++++++++++++++++++++++++-
drivers/accel/thames/thames_job.h | 8 +
include/uapi/drm/thames_accel.h | 99 +++++++-
8 files changed, 677 insertions(+), 5 deletions(-)
diff --git a/drivers/accel/thames/thames_core.c b/drivers/accel/thames/thames_core.c
index 73350784979e..512de3d880d0 100644
--- a/drivers/accel/thames/thames_core.c
+++ b/drivers/accel/thames/thames_core.c
@@ -28,6 +28,8 @@
#define THAMES_RESTART_TIMEOUT_MS 5000
+#define THAMES_IDLE_TIMEOUT_MS 2000
+
/*
* The IPC memory is the core's first memory-region, which remoteproc allocates
* from only for virtio rings and carveouts this firmware does not have.
@@ -422,6 +424,7 @@ static bool thames_core_rproc_is_ours(struct thames_core *core)
static void thames_core_subdev_stop(struct rproc_subdev *subdev, bool crashed)
{
struct thames_core *core = container_of(subdev, struct thames_core, subdev);
+ bool idle;
if (thames_core_rproc_is_ours(core))
return;
@@ -430,6 +433,19 @@ static void thames_core_subdev_stop(struct rproc_subdev *subdev, bool crashed)
dev_warn(core->dev, THAMES_CORE_FMT
" is stopped through remoteproc; it is offline until remoteproc starts it again\n",
THAMES_CORE_ARG(core));
+
+ /* Its fence lock may be held by a thread that never returns. */
+ if (core->completion_stuck)
+ return;
+
+ if (!crashed &&
+ read_poll_timeout(thames_core_idles, idle, idle, USEC_PER_MSEC,
+ THAMES_IDLE_TIMEOUT_MS * USEC_PER_MSEC, false, core))
+ dev_crit(core->dev, THAMES_CORE_FMT " is not known to idle; remoteproc stops it all the same\n",
+ THAMES_CORE_ARG(core));
+
+ thames_job_complete(core);
+ thames_job_fail_submitted(core);
}
/* Under the core's rproc lock. */
@@ -462,8 +478,10 @@ static int thames_core_attach(struct thames_core *core)
core->ring_tail = head;
thames_context_rewrite_entries(core);
thames_transfers_rewrite(core);
+ thames_job_release_held(core);
core->suspended = false;
+ core->unfinished_seqno = 0;
if (!tdev->has_caps) {
tdev->caps = caps;
@@ -484,6 +502,13 @@ static int thames_core_subdev_start(struct rproc_subdev *subdev)
if (thames_core_rproc_is_ours(core))
return 0;
+ if (core->completion_stuck) {
+ dev_crit(core->dev, THAMES_CORE_FMT
+ " stays offline: a completion of its never finished, a reboot is needed to use it again\n",
+ THAMES_CORE_ARG(core));
+ return 0;
+ }
+
err = thames_core_attach(core);
if (err) {
dev_crit(core->dev, THAMES_CORE_FMT
@@ -615,6 +640,7 @@ void thames_core_fini(struct thames_core *core)
scoped_guard(mutex, &core->rproc->lock)
rproc_remove_subdev(core->rproc, &core->subdev);
+ thames_job_drain(core);
if (core->mbox_chan)
thames_core_free_doorbell(core);
thames_job_fini(core);
@@ -649,11 +675,50 @@ bool thames_core_idles(struct thames_core *core)
return READ_ONCE(record->kind) == DRM_THAMES_FAULT_FIRMWARE;
}
+/* Runs on reset_wq, the ordered queue the timeout handler runs on. */
+int thames_core_reset(struct thames_core *core)
+{
+ struct thames_caps caps = {};
+ struct rproc *rproc = core->rproc;
+ bool idle;
+ int err;
+
+ err = read_poll_timeout(thames_core_idles, idle, idle, USEC_PER_MSEC,
+ THAMES_IDLE_TIMEOUT_MS * USEC_PER_MSEC, false, core);
+ if (err) {
+ dev_crit(core->dev, THAMES_CORE_FMT " is not known to idle; not stopping it\n",
+ THAMES_CORE_ARG(core));
+ return -EBUSY;
+ }
+
+ dev_warn(core->dev, "Resetting " THAMES_CORE_FMT "\n", THAMES_CORE_ARG(core));
+
+ err = thames_core_rproc_shutdown(core);
+ if (!err && rproc->state != RPROC_OFFLINE) {
+ dev_err(core->dev, THAMES_CORE_FMT " is kept running by another power count; not resetting it\n",
+ THAMES_CORE_ARG(core));
+ thames_core_rproc_boot(core);
+ return -EBUSY;
+ }
+ if (err)
+ return err;
+
+ err = thames_core_rproc_boot(core);
+ if (err)
+ return err;
+
+ err = thames_core_wait_handshake(core);
+ if (err)
+ return err;
+
+ return thames_core_read_handshake(core, &caps);
+}
+
/*
* rproc_shutdown() stops the core only when it drops the last power count. The
* core holds one: the count remoteproc took to boot or attach it, or the one
- * the driver's own rproc_boot() took back after a resume. A count anyone else
- * holds keeps the core running, and the suspend is refused.
+ * the driver's own rproc_boot() took back after a reset or a resume. A count
+ * anyone else holds keeps the core running, and the suspend is refused.
*/
int thames_core_suspend(struct thames_core *core)
{
diff --git a/drivers/accel/thames/thames_core.h b/drivers/accel/thames/thames_core.h
index 29a34dc124a7..df39365576c9 100644
--- a/drivers/accel/thames/thames_core.h
+++ b/drivers/accel/thames/thames_core.h
@@ -60,6 +60,14 @@ struct thames_core {
struct rproc_subdev subdev;
struct task_struct *rproc_caller;
+ /*
+ * @completion_stuck: two timeouts found the Job of seqno
+ * @unfinished_seqno taken off @submitted by thames_job_complete() but
+ * with its finished fence not signalled.
+ */
+ u64 unfinished_seqno;
+ bool completion_stuck;
+
struct drm_gpu_scheduler sched;
u64 fence_context;
u64 emit_seqno;
@@ -104,6 +112,7 @@ void thames_core_set_online(struct thames_core *core);
int thames_core_init(struct thames_core *core);
void thames_core_add_subdev(struct thames_core *core);
void thames_core_fini(struct thames_core *core);
+int thames_core_reset(struct thames_core *core);
bool thames_core_idles(struct thames_core *core);
int thames_core_suspend(struct thames_core *core);
int thames_core_resume(struct thames_core *core);
diff --git a/drivers/accel/thames/thames_device.h b/drivers/accel/thames/thames_device.h
index 3e2ee3dbbbea..da618dfea579 100644
--- a/drivers/accel/thames/thames_device.h
+++ b/drivers/accel/thames/thames_device.h
@@ -59,6 +59,8 @@ struct thames_device {
struct ida ctx_ida;
+ struct workqueue_struct *reset_wq;
+
/*
* The firmware keeps the last Context serial and mapping generation it
* saw across a reload of this driver, so both count from the boot clock
diff --git a/drivers/accel/thames/thames_drv.c b/drivers/accel/thames/thames_drv.c
index 1ae667d6501b..a51938ed492d 100644
--- a/drivers/accel/thames/thames_drv.c
+++ b/drivers/accel/thames/thames_drv.c
@@ -58,6 +58,7 @@ static int thames_open(struct drm_device *dev, struct drm_file *file)
thames_priv->tdev = tdev;
kref_init(&thames_priv->refcount);
mutex_init(&thames_priv->mm_lock);
+ spin_lock_init(&thames_priv->fault_lock);
file->driver_priv = thames_priv;
@@ -172,6 +173,25 @@ static int thames_query_copy_out(struct drm_thames_dev_query *args, const void *
return 0;
}
+static int thames_query_job_fault(struct thames_file_priv *file_priv,
+ struct drm_thames_dev_query *args)
+{
+ struct drm_thames_job_fault fault = {};
+ int ret;
+
+ if (args->size < offsetofend(struct drm_thames_job_fault, job))
+ return -EINVAL;
+
+ if (get_user(fault.job, (u32 __user *)u64_to_user_ptr(args->pointer)))
+ return -EFAULT;
+
+ ret = thames_job_query_fault(file_priv, &fault);
+ if (ret)
+ return ret;
+
+ return thames_query_copy_out(args, &fault, sizeof(fault));
+}
+
static int thames_query_caps(struct drm_device *dev, struct drm_thames_dev_query *args)
{
struct thames_device *tdev = to_thames_device(dev);
@@ -191,6 +211,7 @@ static int thames_query_caps(struct drm_device *dev, struct drm_thames_dev_query
static int thames_ioctl_dev_query(struct drm_device *dev, void *data, struct drm_file *file)
{
+ struct thames_file_priv *file_priv = file->driver_priv;
struct drm_thames_dev_query *args = data;
switch (args->type) {
@@ -200,6 +221,12 @@ static int thames_ioctl_dev_query(struct drm_device *dev, void *data, struct drm
return 0;
}
return thames_query_caps(dev, args);
+ case DRM_THAMES_DEV_QUERY_JOB_FAULT:
+ if (!args->pointer) {
+ args->size = sizeof(struct drm_thames_job_fault);
+ return 0;
+ }
+ return thames_query_job_fault(file_priv, args);
default:
return -EINVAL;
}
@@ -339,12 +366,18 @@ static struct thames_device *thames_device_init(struct platform_device *pdev,
atomic64_set(&tdev->last_context_serial, seed);
atomic64_set(&tdev->mapping_generation, seed);
+ tdev->reset_wq = alloc_ordered_workqueue("thames-reset", 0);
+ if (!tdev->reset_wq)
+ return ERR_PTR(-ENOMEM);
+
return tdev;
}
static void thames_device_fini(struct thames_device *tdev)
{
WARN_ON(tdev->num_cores > 0);
+
+ destroy_workqueue(tdev->reset_wq);
}
static int thames_platform_probe(struct platform_device *pdev)
diff --git a/drivers/accel/thames/thames_drv.h b/drivers/accel/thames/thames_drv.h
index bc3cb7dcd11a..73328ee0cf1e 100644
--- a/drivers/accel/thames/thames_drv.h
+++ b/drivers/accel/thames/thames_drv.h
@@ -68,6 +68,12 @@ struct thames_file_priv {
struct drm_gem_dma_object *user_stack;
struct drm_gem_dma_object *save_area;
+
+ /* Protects fault_history and the setting of banned. */
+ spinlock_t fault_lock;
+ /* Bit 0 is the newest completed Job, set when it faulted. */
+ u16 fault_history;
+ bool banned;
};
void thames_file_priv_put(struct thames_file_priv *priv);
diff --git a/drivers/accel/thames/thames_job.c b/drivers/accel/thames/thames_job.c
index ee0a883065f9..33c9063782ac 100644
--- a/drivers/accel/thames/thames_job.c
+++ b/drivers/accel/thames/thames_job.c
@@ -9,7 +9,9 @@
#include <linux/dma-fence-chain.h>
#include <linux/iopoll.h>
#include <linux/mailbox_client.h>
+#include <linux/math64.h>
#include <linux/platform_device.h>
+#include <linux/time64.h>
#include <linux/xarray.h>
#include <drm/drm_drv.h>
@@ -45,6 +47,8 @@ static void thames_job_put(struct thames_job *job);
* it fires is on a stalled core.
*/
#define THAMES_DEVICE_TIMEOUT_MS 500
+#define THAMES_HOST_TIMEOUT_MS 2000
+static_assert(THAMES_HOST_TIMEOUT_MS > THAMES_DEVICE_TIMEOUT_MS);
struct thames_submit_bos {
struct kref refcount;
@@ -72,6 +76,24 @@ static void thames_submit_bos_put(struct thames_submit_bos *bos)
kref_put(&bos->refcount, thames_submit_bos_free);
}
+#define THAMES_BAN_FAULTS 3
+#define THAMES_BAN_SPAN 10
+static_assert(THAMES_BAN_SPAN <= 16);
+
+static_assert(THAMES_FAULT_KIND_KERNEL == DRM_THAMES_FAULT_KERNEL);
+static_assert(THAMES_FAULT_KIND_FIRMWARE == DRM_THAMES_FAULT_FIRMWARE);
+static_assert(THAMES_FAULT_KIND_TIMEOUT == DRM_THAMES_FAULT_TIMEOUT);
+static_assert(THAMES_FAULT_REG_IERR == DRM_THAMES_REG_IERR);
+static_assert(THAMES_FAULT_REG_IEAR == DRM_THAMES_REG_IEAR);
+static_assert(THAMES_FAULT_REG_IESR == DRM_THAMES_REG_IESR);
+static_assert(THAMES_FAULT_REG_NRP == DRM_THAMES_REG_NRP);
+static_assert(THAMES_FAULT_REG_NTSR == DRM_THAMES_REG_NTSR);
+static_assert(THAMES_FAULT_REG_SE0_FAR == DRM_THAMES_REG_SE0_FAR);
+static_assert(THAMES_FAULT_REG_SE0_FSR == DRM_THAMES_REG_SE0_FSR);
+static_assert(THAMES_FAULT_REG_SE1_FAR == DRM_THAMES_REG_SE1_FAR);
+static_assert(THAMES_FAULT_REG_SE1_FSR == DRM_THAMES_REG_SE1_FSR);
+static_assert(THAMES_FAULT_REGISTERS == DRM_THAMES_FAULT_REGISTERS);
+
static const char *thames_fence_get_driver_name(struct dma_fence *fence)
{
return "thames";
@@ -147,11 +169,96 @@ static int thames_job_hw_submit(struct thames_core *core, struct thames_sched_jo
return 0;
}
+/*
+ * Copied once: the record is the Job's only if its seqno matches, since a later
+ * Job's slot keeps an earlier record.
+ */
+static bool thames_job_read_fault(struct thames_core *core, struct thames_sched_job *job,
+ struct thames_fault_record *record)
+{
+ memcpy(record, &core->ipc->fault_records[job->ring_index], sizeof(*record));
+
+ if (record->seqno != job->seqno)
+ return false;
+
+ if (record->kind < DRM_THAMES_FAULT_KERNEL || record->kind > DRM_THAMES_FAULT_TIMEOUT) {
+ dev_warn_ratelimited(core->dev,
+ "Job seqno %llu: fault record of unknown kind %u refused\n",
+ job->seqno, record->kind);
+ return false;
+ }
+
+ return true;
+}
+
+/*
+ * The firmware faulted with its Ring empty: it writes the record at its head's
+ * slot under that slot's stale seqno and takes no doorbell after.
+ */
+static bool thames_job_read_empty_ring_fault(struct thames_core *core, struct thames_sched_job *job,
+ struct thames_fault_record *record)
+{
+ u32 head = READ_ONCE(core->ipc->ring.head);
+
+ if (head % THAMES_RING_DEPTH != job->ring_index)
+ return false;
+
+ /* Read the fault record after the head that says it is there. */
+ rmb();
+ memcpy(record, &core->ipc->fault_records[job->ring_index], sizeof(*record));
+
+ return record->kind == DRM_THAMES_FAULT_FIRMWARE;
+}
+
+static void thames_job_set_fault(struct thames_sched_job *sched_job,
+ const struct thames_fault_record *record)
+{
+ struct thames_job *job = sched_job->job;
+
+ guard(mutex)(&job->lock);
+
+ job->fault_kind = record->kind;
+ memcpy(job->fault_registers, record->registers, sizeof(job->fault_registers));
+ job->fault_valid = true;
+}
+
+static void thames_context_count_job(struct thames_core *core, struct thames_sched_job *job,
+ bool faulted)
+{
+ struct thames_file_priv *priv = job->file_priv;
+ bool ban = false;
+ u16 history;
+
+ scoped_guard(spinlock, &priv->fault_lock) {
+ history = (priv->fault_history << 1) | faulted;
+ history &= GENMASK(THAMES_BAN_SPAN - 1, 0);
+ priv->fault_history = history;
+
+ if (!priv->banned && hweight16(history) >= THAMES_BAN_FAULTS) {
+ WRITE_ONCE(priv->banned, true);
+ ban = true;
+ }
+ }
+
+ if (ban)
+ dev_warn(core->dev,
+ "Context %u banned: %u of its last %u Jobs faulted (Job seqno %llu)\n",
+ priv->context_id, hweight16(history), THAMES_BAN_SPAN, job->seqno);
+}
+
+static bool thames_context_banned(struct thames_file_priv *priv)
+{
+ return READ_ONCE(priv->banned);
+}
+
void thames_job_complete(struct thames_core *core)
{
struct thames_sched_job *job, *tmp;
+ struct thames_fault_record record;
LIST_HEAD(done);
+ u32 hz = core->tdev->caps.timestamp_hz;
u64 completed, signalled;
+ bool faulted, firmware_fault = false;
completed = READ_ONCE(core->ipc->fence.seqno);
/* Read the entries after the Fence that covers them. */
@@ -174,8 +281,32 @@ void thames_job_complete(struct thames_core *core)
job->stamps[4] = fw->kernel_entry_stamp;
job->stamps[5] = fw->kernel_exit_stamp;
- if (fw->fence_value == THAMES_FENCE_ERROR)
+ faulted = false;
+ if (fw->fence_value == THAMES_FENCE_ERROR) {
+ if (thames_job_read_fault(core, job, &record)) {
+ thames_job_set_fault(job, &record);
+ faulted = record.kind == DRM_THAMES_FAULT_KERNEL ||
+ record.kind == DRM_THAMES_FAULT_TIMEOUT;
+ }
+ if (faulted && record.kind == DRM_THAMES_FAULT_TIMEOUT)
+ dev_warn_ratelimited(core->dev,
+ "Job seqno %llu of Context %u: its Kernel ran past the device's timeout (entry to exit %llu us)\n",
+ job->seqno, job->file_priv->context_id,
+ mul_u64_u32_div(job->stamps[5] -
+ job->stamps[4],
+ USEC_PER_SEC, hz));
+ else if (faulted)
+ dev_warn_ratelimited(core->dev,
+ "Job seqno %llu of Context %u: its Kernel faulted\n",
+ job->seqno, job->file_priv->context_id);
+ else
+ dev_warn_ratelimited(core->dev,
+ "Job seqno %llu: firmware could not honour the entry\n",
+ job->seqno);
dma_fence_set_error(job->done_fence, -EIO);
+ }
+
+ thames_context_count_job(core, job, faulted);
list_del_init(&job->node);
signalled = arch_timer_read_counter();
@@ -183,6 +314,19 @@ void thames_job_complete(struct thames_core *core)
trace_thames_job_done(core, job, signalled);
thames_sched_job_put(job);
}
+
+ /*
+ * The firmware's own fault moves neither the Fence nor the head: it
+ * rings the doorbell and parks in IDLE.
+ */
+ scoped_guard(mutex, &core->job_lock) {
+ job = list_first_entry_or_null(&core->submitted, struct thames_sched_job, node);
+ if (job && !thames_core_is_dead(core) && thames_job_read_fault(core, job, &record))
+ firmware_fault = record.kind == DRM_THAMES_FAULT_FIRMWARE;
+ }
+
+ if (firmware_fault)
+ drm_sched_fault(&core->sched);
}
static int thames_acquire_object_fences(struct drm_gem_object **bos, int bo_count,
@@ -215,6 +359,12 @@ static void thames_attach_object_fences(struct drm_gem_object **bos, int bo_coun
/* Nothing here fails, so a submit call pushes every Job or none. */
static void thames_job_push(struct thames_sched_job *sched_job)
{
+ struct thames_job *job = sched_job->job;
+
+ /* Before the push, so the submission's own record cannot be lost. */
+ scoped_guard(mutex, &job->lock)
+ job->fault_valid = false;
+
scoped_guard(mutex, &sched_job->tdev->sched_lock) {
drm_sched_job_arm(&sched_job->base);
@@ -272,6 +422,12 @@ static struct dma_fence *thames_job_run(struct drm_sched_job *sched_job)
if (unlikely(job->base.s_fence->finished.error))
return NULL;
+ if (thames_core_is_dead(core))
+ return ERR_PTR(-ENODEV);
+
+ if (thames_context_banned(job->file_priv))
+ return ERR_PTR(-ECANCELED);
+
/* The done fence keeps the seqno of the Job's first run. */
job->seqno = ++core->emit_seqno;
if (!dma_fence_was_initialized(job->done_fence))
@@ -287,6 +443,224 @@ static struct dma_fence *thames_job_run(struct drm_sched_job *sched_job)
return dma_fence_get(job->done_fence);
}
+void thames_job_release_held(struct thames_core *core)
+{
+ struct thames_sched_job *job, *tmp;
+ LIST_HEAD(held);
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_init(&core->held, &held);
+
+ list_for_each_entry_safe(job, tmp, &held, node) {
+ list_del_init(&job->node);
+ thames_sched_job_put(job);
+ }
+}
+
+/*
+ * Held, not freed: the system firmware may refuse the stop and the core run on.
+ */
+void thames_job_fail_submitted(struct thames_core *core)
+{
+ struct thames_sched_job *job;
+ LIST_HEAD(lost);
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_init(&core->submitted, &lost);
+
+ list_for_each_entry(job, &lost, node)
+ thames_job_fail_done_fence(job, -ENODEV);
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_tail_init(&lost, &core->held);
+}
+
+static void thames_job_recover(struct thames_core *core, struct drm_sched_job *sched_job,
+ struct thames_sched_job *bad,
+ const struct thames_fault_record *record)
+{
+ struct drm_gpu_scheduler *sched = &core->sched;
+ struct thames_sched_job *job;
+ struct drm_sched_job *s_job;
+ unsigned int resubmitted = 0;
+ LIST_HEAD(lost);
+ int err;
+
+ drm_sched_wqueue_stop(sched);
+
+ /* Failed before drm_sched_stop(), so the stop finds it complete. */
+ if (bad) {
+ scoped_guard(mutex, &core->job_lock)
+ list_del_init(&bad->node);
+
+ dev_warn(core->dev, "Job seqno %llu of Context %u: the firmware faulted\n",
+ bad->seqno, bad->file_priv->context_id);
+ thames_job_set_fault(bad, record);
+ thames_context_count_job(core, bad, true);
+ dma_fence_set_error(bad->done_fence, -EIO);
+ dma_fence_signal(bad->done_fence);
+ thames_sched_job_put(bad);
+ } else {
+ dev_warn(core->dev, THAMES_CORE_FMT
+ ": the firmware faulted with its Ring empty (IERR 0x%llx, NRP 0x%llx)\n",
+ THAMES_CORE_ARG(core), record->registers[THAMES_FAULT_REG_IERR],
+ record->registers[THAMES_FAULT_REG_NRP]);
+ }
+
+ drm_sched_stop(sched, sched_job);
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_init(&core->submitted, &lost);
+
+ /*
+ * The Jobs on lost stay allocated until the reset has stopped the core,
+ * which may still be running one of their Kernels.
+ */
+ err = thames_core_reset(core);
+ if (err) {
+ thames_core_test_and_set_offline(core);
+ dev_crit(core->dev, THAMES_CORE_FMT
+ " could not be reset (%d); it is offline until it is stopped and started again through remoteproc\n",
+ THAMES_CORE_ARG(core), err);
+
+ list_for_each_entry(job, &lost, node)
+ thames_job_fail_done_fence(job, -ENODEV);
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_tail_init(&lost, &core->held);
+
+ drm_sched_start(sched, -ENODEV);
+ return;
+ }
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_tail_init(&lost, &core->held);
+
+ thames_job_release_held(core);
+
+ drm_sched_for_each_pending_job(s_job, sched, NULL) {
+ struct dma_fence *fence = thames_job_run(s_job);
+
+ if (IS_ERR_OR_NULL(fence)) {
+ thames_job_fail_done_fence(to_thames_sched_job(s_job),
+ fence ? PTR_ERR(fence) : -ECANCELED);
+ continue;
+ }
+
+ s_job->s_fence->parent = fence;
+ resubmitted++;
+ }
+
+ dev_info(core->dev, THAMES_CORE_FMT " restarted, %u Jobs resubmitted\n",
+ THAMES_CORE_ARG(core), resubmitted);
+
+ drm_sched_start(sched, 0);
+}
+
+/*
+ * Only job_lock: the core's fence lock may be held by a completion thread that
+ * died.
+ */
+static bool thames_job_in_completion(struct thames_core *core, struct thames_sched_job *job)
+{
+ struct thames_sched_job *pending;
+
+ if (dma_fence_is_signaled(&job->base.s_fence->finished))
+ return false;
+
+ guard(mutex)(&core->job_lock);
+ list_for_each_entry(pending, &core->submitted, node) {
+ if (pending == job)
+ return false;
+ }
+
+ return true;
+}
+
+static enum drm_gpu_sched_stat thames_job_timedout(struct drm_sched_job *sched_job)
+{
+ struct thames_sched_job *job = to_thames_sched_job(sched_job);
+ struct thames_sched_job *pending, *oldest = NULL;
+ struct thames_core *core = container_of(sched_job->sched, struct thames_core, sched);
+ struct thames_fault_record record;
+ LIST_HEAD(dead_jobs);
+ bool found = false;
+
+ if (core->completion_stuck)
+ return DRM_GPU_SCHED_STAT_ENODEV;
+
+ if (thames_job_in_completion(core, job)) {
+ if (core->unfinished_seqno != job->seqno) {
+ core->unfinished_seqno = job->seqno;
+ return DRM_GPU_SCHED_STAT_NO_HANG;
+ }
+
+ core->completion_stuck = true;
+ thames_core_test_and_set_offline(core);
+ dev_crit(core->dev, "Job seqno %llu on " THAMES_CORE_FMT
+ ": its completion never finished; taking the core offline, a reboot is needed to use it again\n",
+ job->seqno, THAMES_CORE_ARG(core));
+ return DRM_GPU_SCHED_STAT_ENODEV;
+ }
+
+ thames_job_complete(core);
+
+ scoped_guard(mutex, &core->job_lock) {
+ oldest = list_first_entry_or_null(&core->submitted, struct thames_sched_job, node);
+ list_for_each_entry(pending, &core->submitted, node) {
+ if (pending == job) {
+ found = true;
+ break;
+ }
+ }
+ }
+
+ if (!found)
+ return DRM_GPU_SCHED_STAT_NO_HANG;
+
+ if (!thames_core_is_dead(core) && thames_job_read_fault(core, oldest, &record) &&
+ record.kind == DRM_THAMES_FAULT_FIRMWARE) {
+ thames_job_recover(core, sched_job, oldest, &record);
+ return DRM_GPU_SCHED_STAT_RESET;
+ }
+
+ if (!thames_core_is_dead(core) && thames_job_read_empty_ring_fault(core, oldest, &record)) {
+ thames_job_recover(core, sched_job, NULL, &record);
+ return DRM_GPU_SCHED_STAT_RESET;
+ }
+
+ /*
+ * Without a firmware fault the core may still be running the Kernel: it
+ * is taken offline, never stopped.
+ */
+ if (thames_core_test_and_set_offline(core))
+ dev_crit(core->dev, "Job seqno %llu timed out on " THAMES_CORE_FMT
+ "; taking the core offline until it is stopped and started again through remoteproc\n",
+ job->seqno, THAMES_CORE_ARG(core));
+
+ drm_sched_stop(&core->sched, sched_job);
+
+ /*
+ * The core may still be running these Jobs' Kernels: core->held keeps
+ * the reference thames_job_hw_submit() took on each, and with it the
+ * Job's Context and buffers.
+ */
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_init(&core->submitted, &dead_jobs);
+
+ list_for_each_entry(pending, &dead_jobs, node) {
+ dma_fence_set_error(pending->done_fence, -ENODEV);
+ dma_fence_signal(pending->done_fence);
+ }
+
+ scoped_guard(mutex, &core->job_lock)
+ list_splice_tail_init(&dead_jobs, &core->held);
+
+ drm_sched_start(&core->sched, -ENODEV);
+
+ return DRM_GPU_SCHED_STAT_RESET;
+}
+
/*
* A Job whose done fence was initialised and has not signalled is on a core's
* Ring, and its Kernel may still run, so the Job moves to core->held instead of
@@ -308,6 +682,7 @@ static void thames_job_cancel(struct drm_sched_job *sched_job)
static const struct drm_sched_backend_ops thames_sched_ops = {
.run_job = thames_job_run,
+ .timedout_job = thames_job_timedout,
.free_job = thames_sched_job_free,
.cancel_job = thames_job_cancel,
};
@@ -319,7 +694,8 @@ int thames_job_init(struct thames_core *core)
.ops = &thames_sched_ops,
.num_rqs = 1,
.credit_limit = THAMES_RING_DEPTH,
- .timeout = MAX_SCHEDULE_TIMEOUT,
+ .timeout = msecs_to_jiffies(THAMES_HOST_TIMEOUT_MS),
+ .timeout_wq = core->tdev->reset_wq,
.name = dev_name(core->dev),
.dev = core->dev,
};
@@ -329,6 +705,9 @@ int thames_job_init(struct thames_core *core)
spin_lock_init(&core->fence_lock);
mutex_init(&core->job_lock);
+ atomic_set(&core->dead, 0);
+ core->unfinished_seqno = 0;
+ core->completion_stuck = false;
INIT_LIST_HEAD(&core->submitted);
INIT_LIST_HEAD(&core->held);
@@ -418,6 +797,9 @@ int thames_job_suspend(struct thames_core *core)
cancel_delayed_work_sync(&sched->work_tdr);
drm_sched_wqueue_stop(sched);
+ if (thames_core_is_dead(core))
+ return 0;
+
scoped_guard(mutex, &core->job_lock)
pending = list_count_nodes(&core->submitted);
wait_ms = max_t(unsigned int, pending, 1) * THAMES_SUSPEND_TIMEOUT_MS;
@@ -450,6 +832,40 @@ void thames_job_resume(struct thames_core *core)
drm_sched_wqueue_start(&core->sched);
}
+static bool thames_job_ring_drained(struct thames_core *core)
+{
+ thames_job_complete(core);
+
+ guard(mutex)(&core->job_lock);
+ return list_empty(&core->submitted);
+}
+
+void thames_job_drain(struct thames_core *core)
+{
+ struct drm_gpu_scheduler *sched = &core->sched;
+ size_t pending;
+ bool drained;
+
+ drm_sched_wqueue_stop(sched);
+ cancel_delayed_work_sync(&sched->work_tdr);
+ drm_sched_wqueue_stop(sched);
+
+ if (thames_core_is_dead(core))
+ return;
+
+ scoped_guard(mutex, &core->job_lock)
+ pending = list_count_nodes(&core->submitted);
+
+ if (!pending)
+ return;
+
+ read_poll_timeout(thames_job_ring_drained, drained, drained, USEC_PER_MSEC,
+ pending * THAMES_SUSPEND_TIMEOUT_MS * USEC_PER_MSEC, false, core);
+
+ cancel_delayed_work_sync(&sched->work_tdr);
+ drm_sched_wqueue_stop(sched);
+}
+
int thames_job_open(struct thames_file_priv *thames_priv)
{
struct thames_device *tdev = thames_priv->tdev;
@@ -768,6 +1184,7 @@ static void thames_submit_syncs_signal(struct thames_submit_syncs *syncs, struct
int thames_ioctl_job_submit(struct drm_device *dev, void *data, struct drm_file *file)
{
struct drm_thames_job_submit *args = data;
+ struct thames_file_priv *file_priv = file->driver_priv;
struct thames_submit_syncs syncs = {};
struct thames_submit_bos *bos = NULL;
struct ww_acquire_ctx acquire_ctx;
@@ -789,6 +1206,13 @@ int thames_ioctl_job_submit(struct drm_device *dev, void *data, struct drm_file
if (IS_ERR(handles))
return PTR_ERR(handles);
+ if (thames_context_banned(file_priv)) {
+ dev_warn_ratelimited(dev->dev, "Context %u is banned; submission refused\n",
+ file_priv->context_id);
+ ret = -ECANCELED;
+ goto out_free;
+ }
+
ret = thames_submit_syncs_get(file, &args->syncs, &syncs);
if (ret)
goto out_syncs;
@@ -856,6 +1280,7 @@ int thames_ioctl_job_submit(struct drm_device *dev, void *data, struct drm_file
thames_submit_bos_put(bos);
out_syncs:
thames_submit_syncs_free(&syncs);
+out_free:
kvfree(handles);
return ret;
}
@@ -879,3 +1304,30 @@ int thames_ioctl_job_destroy(struct drm_device *dev, void *data, struct drm_file
return 0;
}
+int thames_job_query_fault(struct thames_file_priv *file_priv, struct drm_thames_job_fault *fault)
+{
+ struct thames_job *job;
+ int ret = 0;
+
+ xa_lock(&file_priv->jobs);
+ job = xa_load(&file_priv->jobs, fault->job);
+ if (job)
+ kref_get(&job->refcount);
+ xa_unlock(&file_priv->jobs);
+
+ if (!job)
+ return -ENOENT;
+
+ scoped_guard(mutex, &job->lock) {
+ if (job->fault_valid) {
+ fault->kind = job->fault_kind;
+ memcpy(fault->registers, job->fault_registers, sizeof(fault->registers));
+ } else {
+ ret = -ENOENT;
+ }
+ }
+
+ thames_job_put(job);
+
+ return ret;
+}
diff --git a/drivers/accel/thames/thames_job.h b/drivers/accel/thames/thames_job.h
index ff8f785ad999..d5cf39051ba9 100644
--- a/drivers/accel/thames/thames_job.h
+++ b/drivers/accel/thames/thames_job.h
@@ -60,17 +60,25 @@ struct thames_job {
/* Protects fault_valid, fault_kind and fault_registers. */
struct mutex lock;
+
+ bool fault_valid;
+ u32 fault_kind;
+ u64 fault_registers[DRM_THAMES_FAULT_REGISTERS];
};
int thames_ioctl_job_create(struct drm_device *dev, void *data, struct drm_file *file);
int thames_ioctl_job_submit(struct drm_device *dev, void *data, struct drm_file *file);
int thames_ioctl_job_destroy(struct drm_device *dev, void *data, struct drm_file *file);
+int thames_job_query_fault(struct thames_file_priv *file_priv, struct drm_thames_job_fault *fault);
void thames_job_complete(struct thames_core *core);
+void thames_job_fail_submitted(struct thames_core *core);
+void thames_job_release_held(struct thames_core *core);
int thames_job_init(struct thames_core *core);
int thames_job_suspend(struct thames_core *core);
void thames_job_resume(struct thames_core *core);
+void thames_job_drain(struct thames_core *core);
void thames_job_fini(struct thames_core *core);
int thames_job_open(struct thames_file_priv *thames_priv);
void thames_job_close(struct thames_file_priv *thames_priv);
diff --git a/include/uapi/drm/thames_accel.h b/include/uapi/drm/thames_accel.h
index 1a34955a5509..2c2d997415cd 100644
--- a/include/uapi/drm/thames_accel.h
+++ b/include/uapi/drm/thames_accel.h
@@ -258,6 +258,11 @@ enum drm_thames_dev_query_type {
/** @DRM_THAMES_DEV_QUERY_CAPS: The capability query, &drm_thames_caps. */
DRM_THAMES_DEV_QUERY_CAPS = 0,
+ /**
+ * @DRM_THAMES_DEV_QUERY_JOB_FAULT: The fault record of a failed Job,
+ * &drm_thames_job_fault.
+ */
+ DRM_THAMES_DEV_QUERY_JOB_FAULT,
};
/**
@@ -339,7 +344,12 @@ enum drm_thames_fault_kind {
/**
* @DRM_THAMES_FAULT_FIRMWARE: The firmware took an exception in
- * supervisor mode while running this Job. The core was reset.
+ * supervisor mode while running this Job. The kernel driver resets the
+ * core and runs the other Jobs that were on its Ring again, on the same
+ * fences. Such a Job may have started before the reset, so its Kernel
+ * may run twice, the second time from its start: a Kernel that reads
+ * what it wrote in place must not rely on a single run. When the reset
+ * fails, the core goes offline and those Jobs fail with -ENODEV.
*/
DRM_THAMES_FAULT_FIRMWARE,
@@ -353,6 +363,93 @@ enum drm_thames_fault_kind {
DRM_THAMES_FAULT_TIMEOUT,
};
+/**
+ * enum drm_thames_fault_register - Index into &drm_thames_job_fault.registers
+ *
+ * The control registers the fault handler saves, by the names SPRUIP0 gives
+ * them. A register another core generation reports is appended here and at the
+ * end of &drm_thames_job_fault.registers, which the query's size convention
+ * lets grow; a register the firmware did not save reads 0.
+ */
+enum drm_thames_fault_register {
+ /** @DRM_THAMES_REG_IERR: Internal exception report register. */
+ DRM_THAMES_REG_IERR = 0,
+
+ /** @DRM_THAMES_REG_IEAR: Internal exception address register. */
+ DRM_THAMES_REG_IEAR,
+
+ /** @DRM_THAMES_REG_IESR: Internal exception status register. */
+ DRM_THAMES_REG_IESR,
+
+ /** @DRM_THAMES_REG_NRP: The return pointer saved at the exception, the faulting PC. */
+ DRM_THAMES_REG_NRP,
+
+ /** @DRM_THAMES_REG_NTSR: The task state register saved at the exception. */
+ DRM_THAMES_REG_NTSR,
+
+ /**
+ * @DRM_THAMES_REG_SE0_FAR: Streaming engine 0's fault address register:
+ * the virtual address of the earliest fault on stream 0, 128-byte
+ * aligned; 0 when no stream has faulted since one last closed, or the
+ * engine could not capture the address.
+ */
+ DRM_THAMES_REG_SE0_FAR,
+
+ /**
+ * @DRM_THAMES_REG_SE0_FSR: Streaming engine 0's fault status register:
+ * bit 12 ERROR and a 12-bit syndrome below it, SPRUIP0 figures 3-139 to
+ * 3-144.
+ */
+ DRM_THAMES_REG_SE0_FSR,
+
+ /**
+ * @DRM_THAMES_REG_SE1_FAR: Streaming engine 1's fault address register:
+ * the virtual address of the earliest fault on stream 1, 128-byte
+ * aligned; 0 when no stream has faulted since one last closed, or the
+ * engine could not capture the address.
+ */
+ DRM_THAMES_REG_SE1_FAR,
+
+ /**
+ * @DRM_THAMES_REG_SE1_FSR: Streaming engine 1's fault status register:
+ * bit 12 ERROR and a 12-bit syndrome below it, SPRUIP0 figures 3-139 to
+ * 3-144.
+ */
+ DRM_THAMES_REG_SE1_FSR,
+};
+
+#define DRM_THAMES_FAULT_REGISTERS 9
+
+/**
+ * struct drm_thames_job_fault - Argument of DRM_THAMES_DEV_QUERY_JOB_FAULT
+ *
+ * The fault the device reported for the Job's last failed submission, with
+ * the saved registers raw, for userspace to decode. Every kind counts
+ * towards the Context's ban. The record is cleared whenever the Job is
+ * submitted, a refused submit call not counting, and set when a submission
+ * of it fails with a fault. The query returns -ENOENT while the record is
+ * clear or when @job names no Job, and -EINVAL when
+ * &drm_thames_dev_query.size does not cover @job. A client that submits a
+ * Job again before its last submission completed may read the earlier
+ * one's record.
+ */
+struct drm_thames_job_fault {
+ /** @job: Handle of the Job, input. */
+ __u32 job;
+
+ /** @kind: One of &enum drm_thames_fault_kind. */
+ __u32 kind;
+
+ /**
+ * @registers: The saved control registers, indexed by
+ * &enum drm_thames_fault_register. For a timeout only NRP, NTSR and the
+ * four stream registers, the Kernel's PC and TSR and the streams' state
+ * when the timer interrupted it; every register is 0 when the timeout
+ * is transfers that did not drain after the Kernel returned.
+ */
+ __u64 registers[DRM_THAMES_FAULT_REGISTERS];
+};
+
/**
* struct drm_thames_dev_query - Arguments passed to DRM_IOCTL_THAMES_DEV_QUERY
*/
--
2.54.0