[PATCH v2 7/8] remoteproc: qcom: pas: Enforce coupled stop/crash for HPASS ADSP clusters

From: Shawn Guo

Date: Tue Dec 01 2026 - 02:02:51 EST


The firmware of a Nord HPASS ADSP cluster cannot tolerate one member
being stopped or restarted independently of the others: doing so leaves
sysmon/glink_ssr notify requests to siblings timing out, and the whole
cluster unable to recover without manually stopping all three and
restarting them in dependency order.

Coordinate an explicit stop or crash of any member across the whole
cluster in two phases, since hardware testing showed that whichever
member's PAS/TZ hardware power-off happens chronologically last always
fails its own graceful shutdown ack (both the sysmon SSCTL QMI ack and
the SMEM-bit/IRQ ack), regardless of whether that member is the cluster
root.

Phase 1 fans the stop out to every participant concurrently via a
per-member work item and waits, at a barrier, for every participant to
finish requesting and awaiting its own graceful ack while the whole
cluster is still fully powered. Only once every participant has cleared
the barrier does phase 2 let each of them proceed to its actual PAS/TZ
hardware power-off, so no member is ever asking firmware to ack a
shutdown after a sibling's hardware is already gone. The fan-out has to
be deferred to a work item rather than calling rproc_shutdown() inline,
because qcom_pas_stop() runs with the remoteproc core holding the
stopping member's own rproc->lock, and reaching for a sibling's
rproc->lock from inside that critical section would risk an ABBA
deadlock against a concurrent operation on that sibling.

Membership in a round is decided once, by
qcom_pas_cluster_member_stops(), and recorded per member, so that the
participant count and the two counters it seeds can never diverge. The
crashed member that triggered the round is a participant even though its
rproc->state is still RPROC_CRASHED -- the core only sets RPROC_OFFLINE
after ops->stop() returns. So is a sibling that crashed concurrently:
its own recovery drives it through qcom_pas_stop() and thus the barrier,
and leaving it out would let it power off unsynchronized and start again
while the root is still being torn down. Such a sibling is neither
stopped by a work item, which would drop the power reference its
recovery holds, nor restarted by the cascade, which would boot it twice.

A participant whose work item does not get it into qcom_pas_stop()
after all, because rproc_shutdown() only dropped one of several power
references, or because the member is being removed and the work item
was cancelled, is withdrawn from the round, so that the rest of the
cluster is not left waiting for it.

On a crash, cascade the stop the same way but also auto-restart the
whole cluster root-first once every participant has fully powered off,
reusing the root_booted completion to keep non-root members'
qcom_pas_start() from racing ahead of the root's boot. The members to
restart are marked when the round starts, and the cascade picks them one
at a time under the cluster lock, as membership may change while it
runs. An explicit stop leaves the ordered restart to users or
higher-level policy.

Instances whose descriptor names no cluster are completely unaffected,
since qcom_pas.cluster stays NULL for them.

Signed-off-by: Shawn Guo <shengchao.guo@xxxxxxxxxxxxxxxx>
---
drivers/remoteproc/qcom_q6v5_pas.c | 357 +++++++++++++++++++++++++++++
1 file changed, 357 insertions(+)

diff --git a/drivers/remoteproc/qcom_q6v5_pas.c b/drivers/remoteproc/qcom_q6v5_pas.c
index 5c96b2b82903..3f6c2c7f2562 100644
--- a/drivers/remoteproc/qcom_q6v5_pas.c
+++ b/drivers/remoteproc/qcom_q6v5_pas.c
@@ -32,6 +32,7 @@
#include <linux/soc/qcom/qmi_tmd.h>
#include <linux/soc/qcom/smem.h>
#include <linux/soc/qcom/smem_state.h>
+#include <linux/workqueue.h>
#include <dt-bindings/power/qcom,rpmhpd.h>
#include <dt-bindings/thermal/qcom,pas.h>

@@ -168,7 +169,11 @@ struct qcom_pas {

struct qcom_pas_cluster *cluster;
struct list_head cluster_node;
+ struct work_struct stop_work;
bool is_cluster_root;
+ bool in_cluster_stop;
+ bool self_recovering;
+ bool cascade_boot;
};

/**
@@ -179,6 +184,12 @@ struct qcom_pas {
* @members: list of struct qcom_pas, linked via cluster_node
* @root: the member owning the resources shared across the cluster
* @root_booted: signaled once @root has started, cleared when it goes down
+ * @cascade_work: deferred work that restarts the cluster after a crash
+ * @cascade_crashed: true if the current round was triggered by a crash
+ * @stop_in_progress: re-entrancy guard: a coordinated stop is in flight
+ * @stop_pending: participants still owing a phase-1 graceful-ack attempt
+ * @stop_barrier: released once @stop_pending reaches 0
+ * @stop_done_pending: participants still owing a phase-2 hardware power-off
* @refcount: number of members currently attached to this cluster
*
* PAS instances whose descriptors reference the same qcom_pas_cluster_desc
@@ -193,6 +204,14 @@ struct qcom_pas_cluster {
struct qcom_pas *root;
struct completion root_booted;

+ struct work_struct cascade_work;
+ bool cascade_crashed;
+ bool stop_in_progress;
+
+ int stop_pending;
+ struct completion stop_barrier;
+ int stop_done_pending;
+
int refcount;
};

@@ -207,9 +226,106 @@ struct qcom_pas_cluster {
*/
#define QCOM_PAS_CLUSTER_ROOT_BOOT_TIMEOUT (10 * HZ)

+/*
+ * How long a member waits at the phase-1 barrier for the rest of the cluster,
+ * in case a participant never reaches it at all (e.g. rproc_stop() bails out
+ * before calling ops->stop).
+ */
+#define QCOM_PAS_CLUSTER_STOP_TIMEOUT (20 * HZ)
+
static LIST_HEAD(qcom_pas_cluster_list);
static DEFINE_MUTEX(qcom_pas_cluster_list_lock);

+static void qcom_pas_cluster_withdraw(struct qcom_pas *pas);
+
+static void qcom_pas_stop_work_fn(struct work_struct *work)
+{
+ struct qcom_pas *pas = container_of(work, struct qcom_pas, stop_work);
+
+ rproc_shutdown(pas->rproc);
+
+ /*
+ * rproc_shutdown() only gets to qcom_pas_stop() once the last rproc
+ * power reference is dropped, and not at all if the member went down
+ * in the meantime. Either way the member then never reaches the
+ * barrier, so take it out of the round rather than leave the rest of
+ * the cluster waiting on it. A no-op if it did take part.
+ */
+ qcom_pas_cluster_withdraw(pas);
+}
+
+/*
+ * Pick the next member the cascade has to boot, root first, see
+ * qcom_pas_cluster_cascade_work().
+ */
+static struct qcom_pas *qcom_pas_cluster_next_boot(struct qcom_pas_cluster *cluster)
+{
+ struct qcom_pas *pas;
+
+ guard(mutex)(&cluster->lock);
+
+ if (cluster->root && cluster->root->cascade_boot) {
+ cluster->root->cascade_boot = false;
+ return cluster->root;
+ }
+
+ list_for_each_entry(pas, &cluster->members, cluster_node) {
+ if (pas->cascade_boot) {
+ pas->cascade_boot = false;
+ return pas;
+ }
+ }
+
+ return NULL;
+}
+
+/**
+ * qcom_pas_cluster_cascade_work() - restart a crashed cluster, root first
+ * @work: the cluster's cascade_work
+ *
+ * Only ever scheduled from qcom_pas_cluster_stop_complete(), i.e. only after
+ * every participant has finished its own phase-2 hardware power-off, and only
+ * when the round that just finished was a crash; explicit stops never
+ * auto-restart.
+ */
+static void qcom_pas_cluster_cascade_work(struct work_struct *work)
+{
+ struct qcom_pas_cluster *cluster = container_of(work, struct qcom_pas_cluster,
+ cascade_work);
+ struct qcom_pas *pas;
+ int ret;
+
+ /*
+ * Only the participants qcom_pas_cluster_trigger_stop() marked with
+ * cascade_boot are booted from here. Members driven by their own
+ * crash-recovery thread are not marked: the origin, and any sibling
+ * that crashed concurrently with it. Calling rproc_boot() on those
+ * would either be a silent no-op or leak a power reference, since
+ * rproc_boot_recovery() keeps the one it was entered with. Members
+ * that joined or left since the round started are not marked either,
+ * and each one is picked under cluster->lock, so that membership may
+ * change while rproc_boot() runs.
+ *
+ * The root, when marked, has to be booted before any other member:
+ * a dependent member's qcom_pas_start() blocks on cluster->root_booted,
+ * and booting it first from this single-threaded work item would make
+ * it wait out its timeout on a root boot this same thread has not
+ * issued yet. A dependent booted while the root is still being
+ * restarted by its own recovery blocks there until it is up.
+ */
+ while ((pas = qcom_pas_cluster_next_boot(cluster))) {
+ ret = rproc_boot(pas->rproc);
+ if (!ret)
+ continue;
+
+ dev_err(pas->dev, "failed to restart cluster member: %d\n", ret);
+
+ /* Without the root none of its dependents can boot either */
+ if (pas->is_cluster_root)
+ break;
+ }
+}
+
static struct qcom_pas_cluster *
qcom_pas_cluster_get(const struct qcom_pas_cluster_desc *desc)
{
@@ -232,6 +348,8 @@ qcom_pas_cluster_get(const struct qcom_pas_cluster_desc *desc)
mutex_init(&cluster->lock);
INIT_LIST_HEAD(&cluster->members);
init_completion(&cluster->root_booted);
+ init_completion(&cluster->stop_barrier);
+ INIT_WORK(&cluster->cascade_work, qcom_pas_cluster_cascade_work);
cluster->refcount = 1;
list_add_tail(&cluster->list, &qcom_pas_cluster_list);

@@ -271,6 +389,7 @@ static int qcom_pas_cluster_init(struct qcom_pas *pas,
return -ENOMEM;

pas->is_cluster_root = desc->cluster_root;
+ INIT_WORK(&pas->stop_work, qcom_pas_stop_work_fn);

scoped_guard(mutex, &pas->cluster->lock) {
list_add_tail(&pas->cluster_node, &pas->cluster->members);
@@ -288,12 +407,23 @@ static void qcom_pas_cluster_exit(struct qcom_pas *pas)
if (!cluster)
return;

+ /* Leave the cluster first, so that no new round can schedule stop_work */
scoped_guard(mutex, &cluster->lock) {
list_del(&pas->cluster_node);
if (cluster->root == pas)
cluster->root = NULL;
}

+ /*
+ * A stop_work cancelled before it ran leaves this member owing the
+ * current round its stop, so withdraw it from the round, which may
+ * end it. Let a cascade started by that, or by any earlier round,
+ * finish restarting the remaining members, rather than cancelling it.
+ */
+ cancel_work_sync(&pas->stop_work);
+ qcom_pas_cluster_withdraw(pas);
+ flush_work(&cluster->cascade_work);
+
qcom_pas_cluster_put(cluster);
pas->cluster = NULL;
}
@@ -354,6 +484,222 @@ static int qcom_pas_cluster_wait_for_root(struct qcom_pas *pas)
return 0;
}

+/*
+ * A sibling that crashed concurrently with @origin is driven by its own
+ * recovery: that recovery calls into qcom_pas_stop(), so the sibling does reach
+ * this round's barrier, and it restarts the sibling afterwards, so the cascade
+ * must not restart it a second time.
+ *
+ * With recovery disabled nothing will drive its stop, so it cannot be enrolled.
+ */
+static bool qcom_pas_cluster_member_self_recovers(struct qcom_pas *member,
+ struct qcom_pas *origin)
+{
+ return member != origin &&
+ member->rproc->state == RPROC_CRASHED &&
+ !member->rproc->recovery_disabled;
+}
+
+/*
+ * A member takes part in a coordinated stop if its hardware is still powered:
+ * either it is running or attached, or it crashed -- the origin, whose
+ * rproc->state is RPROC_CRASHED and only becomes RPROC_OFFLINE once its
+ * ops->stop() has returned, or a sibling that crashed concurrently and is being
+ * torn down by its own recovery.
+ *
+ * Leaving a concurrently crashed sibling out of the round would let it race
+ * ahead of the round it is nonetheless part of: it would power its hardware off
+ * without waiting for the rest of the cluster to be acked, and then start again
+ * while the root is still being torn down.
+ */
+static bool qcom_pas_cluster_member_stops(struct qcom_pas *member,
+ struct qcom_pas *origin)
+{
+ if (member == origin)
+ return true;
+
+ if (qcom_pas_cluster_member_self_recovers(member, origin))
+ return true;
+
+ return member->rproc->state == RPROC_RUNNING ||
+ member->rproc->state == RPROC_ATTACHED;
+}
+
+/**
+ * qcom_pas_cluster_trigger_stop() - begin a coordinated cluster stop
+ * @pas: the member that is being stopped or has crashed
+ * @crashed: true if @pas crashed, rather than being stopped explicitly
+ *
+ * Marks every member whose hardware is still powered as a participant of this
+ * round and fires off each *other* participant's own full stop concurrently
+ * via its stop_work, instead of one after another. This lets every
+ * participant's phase-1 graceful-ack attempt (see qcom_pas_stop()) run while
+ * all of them are still fully powered, so nobody is asking firmware to ack a
+ * shutdown after a sibling's hardware is already gone.
+ *
+ * A no-op if a coordinated stop is already in flight, e.g. when @pas is a
+ * sibling whose own stop was itself triggered by this same round: @pas is
+ * already marked as a participant and just goes on to take part in it.
+ *
+ * Must not call rproc_shutdown()/rproc_boot() directly from here: this runs
+ * from inside qcom_pas_stop(), which the remoteproc core calls with @pas's own
+ * rproc->lock held, and taking a sibling's rproc->lock synchronously from
+ * within that critical section would risk an ABBA deadlock against a
+ * concurrent operation on the sibling.
+ */
+static void qcom_pas_cluster_trigger_stop(struct qcom_pas *pas, bool crashed)
+{
+ struct qcom_pas_cluster *cluster = pas->cluster;
+ struct qcom_pas *member;
+ int active = 0;
+
+ guard(mutex)(&cluster->lock);
+
+ if (cluster->stop_in_progress)
+ return;
+
+ cluster->stop_in_progress = true;
+ cluster->cascade_crashed = crashed;
+
+ list_for_each_entry(member, &cluster->members, cluster_node) {
+ member->self_recovering = qcom_pas_cluster_member_self_recovers(member, pas);
+ member->in_cluster_stop = qcom_pas_cluster_member_stops(member, pas);
+ member->cascade_boot = member->in_cluster_stop && member != pas &&
+ !member->self_recovering;
+ if (member->in_cluster_stop)
+ active++;
+ }
+
+ cluster->stop_pending = active;
+ cluster->stop_done_pending = active;
+ reinit_completion(&cluster->stop_barrier);
+
+ /*
+ * Fan out while still holding the lock, so that membership cannot
+ * change between counting the participants and scheduling them, and so
+ * that a stop_work running immediately blocks in
+ * qcom_pas_cluster_stop_barrier() until the counters above are in
+ * place. schedule_work() does not sleep, so it is safe from here.
+ *
+ * A self-recovering participant is left alone: its own recovery already
+ * drives it through qcom_pas_stop(), and calling rproc_shutdown() on it
+ * from here would drop the rproc power reference that recovery holds.
+ */
+ list_for_each_entry(member, &cluster->members, cluster_node) {
+ if (member == pas || !member->in_cluster_stop ||
+ member->self_recovering)
+ continue;
+
+ schedule_work(&member->stop_work);
+ }
+}
+
+/**
+ * qcom_pas_cluster_stop_barrier() - wait for the whole cluster's phase-1 ack
+ * @pas: the member calling this from inside its own qcom_pas_stop()
+ *
+ * Blocks this member's own hardware power-off until every other participant
+ * has also finished its phase-1 graceful-ack attempt (see qcom_pas_stop()).
+ * Bounded by QCOM_PAS_CLUSTER_STOP_TIMEOUT, proceeding to phase 2 anyway
+ * rather than hanging forever.
+ *
+ * A member that is not a participant of the current round, having already been
+ * powered off before it started, has no ack to contribute and must not touch
+ * the counters.
+ */
+static void qcom_pas_cluster_stop_barrier(struct qcom_pas *pas)
+{
+ struct qcom_pas_cluster *cluster = pas->cluster;
+
+ scoped_guard(mutex, &cluster->lock) {
+ if (!pas->in_cluster_stop)
+ return;
+ if (--cluster->stop_pending == 0)
+ complete_all(&cluster->stop_barrier);
+ }
+
+ if (!wait_for_completion_timeout(&cluster->stop_barrier,
+ QCOM_PAS_CLUSTER_STOP_TIMEOUT))
+ dev_warn(pas->dev, "timed out waiting for cluster stop barrier\n");
+}
+
+/*
+ * Drop @pas from the current round, which ends with the last participant.
+ * Returns true if that was the last one, and the round was triggered by a
+ * crash, i.e. the caller has to schedule the root-first restart cascade.
+ */
+static bool qcom_pas_cluster_leave_round(struct qcom_pas *pas)
+{
+ struct qcom_pas_cluster *cluster = pas->cluster;
+
+ lockdep_assert_held(&cluster->lock);
+
+ pas->in_cluster_stop = false;
+
+ if (--cluster->stop_done_pending != 0)
+ return false;
+
+ cluster->stop_in_progress = false;
+
+ return cluster->cascade_crashed;
+}
+
+/**
+ * qcom_pas_cluster_stop_complete() - record this member's phase-2 completion
+ * @pas: the member calling this from inside its own qcom_pas_stop()
+ *
+ * The last participant to call this, i.e. the last to finish powering off its
+ * own hardware, ends the round and, if it was triggered by a crash, schedules
+ * the root-first restart cascade.
+ */
+static void qcom_pas_cluster_stop_complete(struct qcom_pas *pas)
+{
+ struct qcom_pas_cluster *cluster = pas->cluster;
+ bool crashed;
+
+ scoped_guard(mutex, &cluster->lock) {
+ if (!pas->in_cluster_stop)
+ return;
+
+ crashed = qcom_pas_cluster_leave_round(pas);
+ }
+
+ if (crashed)
+ schedule_work(&cluster->cascade_work);
+}
+
+/**
+ * qcom_pas_cluster_withdraw() - take a participant out of the current round
+ * @pas: a participant that will not go through qcom_pas_stop() after all
+ *
+ * For a participant whose stop_work did not get it to qcom_pas_stop(), either
+ * because rproc_shutdown() returned without stopping it or because the work
+ * was cancelled before it ran. It never reached the barrier, so account for
+ * both its phase-1 and phase-2 share, letting the round end without it. It is
+ * not restarted by the cascade either, as it was never stopped.
+ */
+static void qcom_pas_cluster_withdraw(struct qcom_pas *pas)
+{
+ struct qcom_pas_cluster *cluster = pas->cluster;
+ bool crashed;
+
+ scoped_guard(mutex, &cluster->lock) {
+ if (!pas->in_cluster_stop)
+ return;
+
+ dev_warn(pas->dev, "not stopped along with the cluster\n");
+
+ pas->cascade_boot = false;
+ if (--cluster->stop_pending == 0)
+ complete_all(&cluster->stop_barrier);
+
+ crashed = qcom_pas_cluster_leave_round(pas);
+ }
+
+ if (crashed)
+ schedule_work(&cluster->cascade_work);
+}
+
static void qcom_pas_segment_dump(struct rproc *rproc,
struct rproc_dump_segment *segment,
void *dest, size_t offset, size_t size)
@@ -656,10 +1002,18 @@ static int qcom_pas_stop(struct rproc *rproc)
reinit_completion(&pas->cluster->root_booted);
}

+ if (pas->cluster)
+ qcom_pas_cluster_trigger_stop(pas, rproc->state == RPROC_CRASHED);
+
+ /* Phase 1: request and await this member's own graceful ack */
ret = qcom_q6v5_request_stop(&pas->q6v5, pas->sysmon);
if (ret == -ETIMEDOUT)
dev_err(pas->dev, "timed out on wait\n");

+ if (pas->cluster)
+ qcom_pas_cluster_stop_barrier(pas);
+
+ /* Phase 2: the whole cluster has acked, power the hardware off */
ret = qcom_pas_shutdown(pas->pas_id);
if (ret && pas->decrypt_shutdown)
ret = qcom_pas_shutdown_poll_decrypt(pas);
@@ -687,6 +1041,9 @@ static int qcom_pas_stop(struct rproc *rproc)
if (pas->smem_host_id)
ret = qcom_smem_bust_hwspin_lock_by_host(pas->smem_host_id);

+ if (pas->cluster)
+ qcom_pas_cluster_stop_complete(pas);
+
return ret;
}

--
2.43.0