Skip to contentSkip to conversation

remoteproc: k3: support for graceful shutdown of remote cores

PATCH In review Beleswar Padhi 3 participants 17 messages Nov 25, 2025 – Jan 21 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 remoteproc: k3: support for graceful shutdown of remote cores

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
 	RP_MBOX_SUSPEND_SYSTEM	= 0xFFFFFF11,
 	RP_MBOX_SUSPEND_ACK	= 0xFFFFFF12,
 	RP_MBOX_SUSPEND_CANCEL	= 0xFFFFFF13,
-	RP_MBOX_END_MSG		= 0xFFFFFF14,
+	RP_MBOX_SHUTDOWN	= 0xFFFFFF14,
+	RP_MBOX_SHUTDOWN_ACK	= 0xFFFFFF15,
+	RP_MBOX_END_MSG		= 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +72 -0

97 lines not on this page · raw message

drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK	(SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI		0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
 	u32 ti_sci_id;
 	struct mbox_chan *mbox;
 	struct mbox_client client;
+	struct completion shutdown_complete;
 	void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	init_completion(&kproc->shutdown_complete);
+
 	ret = k3_rproc_of_get_memories(pdev, kproc);
 	if (ret)
 		return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	init_completion(&kproc->shutdown_complete);
+
 	ret = k3_rproc_of_get_memories(pdev, kproc);
 	if (ret)
 		return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -0
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
 	struct k3_r5_cluster *cluster = core->cluster;
 	int ret;
 
+	ret = notify_shutdown_rproc(kproc);
+	if (ret)
+		return ret;
+
 	/* halt all applicable cores */
 	if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
 		list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
 			goto out;
 		}
 
+		init_completion(&kproc->shutdown_complete);
 init_rmem:
 		k3_r5_adjust_tcm_sizes(kproc);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 6 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Quoted 7 lines

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

What happens if the remote core is unable to action the shutdown request (maybe it has crashed).

Is there a way to cleanup resources which the remote core allocated without rebooting the whole system?

Patrick

Quoted 255 lines

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
        RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
        RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
        RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-       RP_MBOX_END_MSG         = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+       RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +72 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *     Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
        case RP_MBOX_ECHO_REPLY:
                dev_info(dev, "received echo reply from %s\n", rproc->name);
                break;
+       case RP_MBOX_SHUTDOWN_ACK:
+               dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+               complete(&kproc->shutdown_complete);
+               break;
        default:
                /* silently handle all other valid messages */
                if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+       int ret;
+       u64 boot_vec;
+       u32 cfg, ctrl, stat;
+
+       ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+       if (ret)
+               return false;
+
+       return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+       bool wfi_status = false;
+       int ret;
+
+       reinit_completion(&kproc->shutdown_complete);
+
+       ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+       if (ret < 0) {
+               dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+               return ret;
+       }
+
+       ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                         msecs_to_jiffies(5000));
+       if (ret == 0) {
+               dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                       __func__);
+               return -EBUSY;
+       }
+
+       ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                                200, 2000);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
        struct k3_rproc *kproc = rproc->priv;
+       int ret;
+
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
 
        return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK      (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI          0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
        u32 ti_sci_id;
        struct mbox_chan *mbox;
        struct mbox_client client;
+       struct completion shutdown_complete;
        void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
        struct k3_r5_cluster *cluster = core->cluster;
        int ret;
 
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
+
        /* halt all applicable cores */
        if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
                list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                        goto out;
                }
 
+               init_completion(&kproc->shutdown_complete);
 init_rmem:
                k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#2 lore raw

Hi Patrick, Mathieu,

Quoted 13 lines · Patrick Oppenlander
Quoted 5 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Quoted 7 lines

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Quoted 2 lines

What happens if the remote core is unable to action the shutdown request

We abort the shutdown sequence if the remoteproc does not respond with an ACK within the timeout assuming rproc is busy doing some work.

Quoted 1 line

(maybe it has crashed).

remoteproc core has the infra to handle rproc crash. It initiates a recovery mechanism by stopping and starting the rproc with the same firmware.

Are you suggesting that we check if rproc_stop() is invoked from a recovery context, and forcefully reset the rproc without sending/waiting for SHUTDOWN msg as a crashed core can't respond to mbox irqs?

Quoted 3 lines

Is there a way to cleanup resources which the remote core allocated without rebooting the whole system?

For SW resources (like mem, vdev): Yes
However, I feel this is currently missing in rproc core. We should be making a call to rproc_resource_cleanup() in rproc_boot_recovery()'s error paths and in rproc_crash_handler_work() in case of subsequent crashes.

^^ Mathieu, thoughts about the above?

For HW resources: No
In TI Device Manager (DM) firmware, only the entity which requested a resource can relinquish it, no other host can do that cleanup on behalf of that entity. So, we can't do much here.

Thanks,
Beleswar

Quoted 257 lines

Patrick

Quoted 254 lines

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
        RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
        RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
        RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-       RP_MBOX_END_MSG         = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+       RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +72 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *     Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
        case RP_MBOX_ECHO_REPLY:
                dev_info(dev, "received echo reply from %s\n", rproc->name);
                break;
+       case RP_MBOX_SHUTDOWN_ACK:
+               dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+               complete(&kproc->shutdown_complete);
+               break;
        default:
                /* silently handle all other valid messages */
                if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+       int ret;
+       u64 boot_vec;
+       u32 cfg, ctrl, stat;
+
+       ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+       if (ret)
+               return false;
+
+       return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+       bool wfi_status = false;
+       int ret;
+
+       reinit_completion(&kproc->shutdown_complete);
+
+       ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+       if (ret < 0) {
+               dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+               return ret;
+       }
+
+       ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                         msecs_to_jiffies(5000));
+       if (ret == 0) {
+               dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                       __func__);
+               return -EBUSY;
+       }
+
+       ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                                200, 2000);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
        struct k3_rproc *kproc = rproc->priv;
+       int ret;
+
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
 
        return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK      (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI          0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
        u32 ti_sci_id;
        struct mbox_chan *mbox;
        struct mbox_client client;
+       struct completion shutdown_complete;
        void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
        struct k3_r5_cluster *cluster = core->cluster;
        int ret;
 
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
+
        /* halt all applicable cores */
        if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
                list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                        goto out;
                }
 
+               init_completion(&kproc->shutdown_complete);
 init_rmem:
                k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

maintainer
#3 lore raw
Quoted 39 lines · Beleswar Prasad Padhi

Hi Patrick, Mathieu,

Quoted 13 lines · Patrick Oppenlander
Quoted 5 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Quoted 7 lines

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Quoted 2 lines

What happens if the remote core is unable to action the shutdown request

We abort the shutdown sequence if the remoteproc does not respond with an ACK within the timeout assuming rproc is busy doing some work.

Quoted 1 line

(maybe it has crashed).

remoteproc core has the infra to handle rproc crash. It initiates a recovery mechanism by stopping and starting the rproc with the same firmware.

Are you suggesting that we check if rproc_stop() is invoked from a recovery context, and forcefully reset the rproc without sending/waiting for SHUTDOWN msg as a crashed core can't respond to mbox irqs?

Quoted 3 lines

Is there a way to cleanup resources which the remote core allocated without rebooting the whole system?

Quoted 8 lines

For SW resources (like mem, vdev): Yes
However, I feel this is currently missing in rproc core. We should be making a call to rproc_resource_cleanup() in rproc_boot_recovery()'s error paths and in rproc_crash_handler_work() in case of subsequent crashes.

^^ Mathieu, thoughts about the above?

Given the backlog of patchsets I have to review, Plumbers in two weeks and the December holidays, I won't be able to look at this issue before January.

Thanks,
Mathieu

Quoted 265 lines

For HW resources: No
In TI Device Manager (DM) firmware, only the entity which requested a resource can relinquish it, no other host can do that cleanup on behalf of that entity. So, we can't do much here.

Thanks,
Beleswar

Quoted 257 lines

Patrick

Quoted 254 lines

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
        RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
        RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
        RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-       RP_MBOX_END_MSG         = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+       RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +72 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *     Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
        case RP_MBOX_ECHO_REPLY:
                dev_info(dev, "received echo reply from %s\n", rproc->name);
                break;
+       case RP_MBOX_SHUTDOWN_ACK:
+               dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+               complete(&kproc->shutdown_complete);
+               break;
        default:
                /* silently handle all other valid messages */
                if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+       int ret;
+       u64 boot_vec;
+       u32 cfg, ctrl, stat;
+
+       ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+       if (ret)
+               return false;
+
+       return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+       bool wfi_status = false;
+       int ret;
+
+       reinit_completion(&kproc->shutdown_complete);
+
+       ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+       if (ret < 0) {
+               dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+               return ret;
+       }
+
+       ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                         msecs_to_jiffies(5000));
+       if (ret == 0) {
+               dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                       __func__);
+               return -EBUSY;
+       }
+
+       ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                                200, 2000);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
        struct k3_rproc *kproc = rproc->priv;
+       int ret;
+
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
 
        return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK      (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI          0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
        u32 ti_sci_id;
        struct mbox_chan *mbox;
        struct mbox_client client;
+       struct completion shutdown_complete;
        void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
        struct k3_r5_cluster *cluster = core->cluster;
        int ret;
 
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
+
        /* halt all applicable cores */
        if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
                list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                        goto out;
                }
 
+               init_completion(&kproc->shutdown_complete);
 init_rmem:
                k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

maintainer
#4 lore raw
In reply to Beleswar Prasad Padhi
Quoted 26 lines · Beleswar Prasad Padhi

Hi Patrick, Mathieu,

Quoted 13 lines · Patrick Oppenlander
Quoted 5 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Quoted 7 lines

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Quoted 2 lines

What happens if the remote core is unable to action the shutdown request

We abort the shutdown sequence if the remoteproc does not respond with an ACK within the timeout assuming rproc is busy doing some work.

Quoted 1 line

(maybe it has crashed).

Quoted 7 lines

remoteproc core has the infra to handle rproc crash. It initiates a recovery mechanism by stopping and starting the rproc with the same firmware.

Are you suggesting that we check if rproc_stop() is invoked from a recovery context, and forcefully reset the rproc without sending/waiting for SHUTDOWN msg as a crashed core can't respond to mbox irqs?

I think that is a fair ask.

Quoted 5 lines
Quoted 4 lines

Is there a way to cleanup resources which the remote core allocated without rebooting the whole system?

Quoted 7 lines

For SW resources (like mem, vdev): Yes
However, I feel this is currently missing in rproc core. We should be making a call to rproc_resource_cleanup() in rproc_boot_recovery()'s error paths and in rproc_crash_handler_work() in case of subsequent crashes.

^^ Mathieu, thoughts about the above?

So far a crash has been treated like a rproc_stop()/rproc_start() operation. Doing resource cleanup as part of a crash recovery would akin to a rproc_shutdown()/rproc_boot() operation, introducing a lot of churn and backward compatibility issues.

Quoted 266 lines

For HW resources: No
In TI Device Manager (DM) firmware, only the entity which requested a resource can relinquish it, no other host can do that cleanup on behalf of that entity. So, we can't do much here.

Thanks,
Beleswar

Quoted 257 lines

Patrick

Quoted 254 lines

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
        RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
        RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
        RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-       RP_MBOX_END_MSG         = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+       RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +72 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *     Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
        case RP_MBOX_ECHO_REPLY:
                dev_info(dev, "received echo reply from %s\n", rproc->name);
                break;
+       case RP_MBOX_SHUTDOWN_ACK:
+               dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+               complete(&kproc->shutdown_complete);
+               break;
        default:
                /* silently handle all other valid messages */
                if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+       int ret;
+       u64 boot_vec;
+       u32 cfg, ctrl, stat;
+
+       ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+       if (ret)
+               return false;
+
+       return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+       bool wfi_status = false;
+       int ret;
+
+       reinit_completion(&kproc->shutdown_complete);
+
+       ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+       if (ret < 0) {
+               dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+               return ret;
+       }
+
+       ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                         msecs_to_jiffies(5000));
+       if (ret == 0) {
+               dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                       __func__);
+               return -EBUSY;
+       }
+
+       ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                                200, 2000);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
        struct k3_rproc *kproc = rproc->priv;
+       int ret;
+
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
 
        return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK      (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI          0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
        u32 ti_sci_id;
        struct mbox_chan *mbox;
        struct mbox_client client;
+       struct completion shutdown_complete;
        void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
        struct k3_r5_cluster *cluster = core->cluster;
        int ret;
 
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
+
        /* halt all applicable cores */
        if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
                list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                        goto out;
                }
 
+               init_completion(&kproc->shutdown_complete);
 init_rmem:
                k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#5 lore raw
Quoted 25 lines · Mathieu Poirier
Quoted 24 lines · Beleswar Prasad Padhi

Hi Patrick, Mathieu,

Quoted 13 lines · Patrick Oppenlander
Quoted 5 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Quoted 7 lines

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Quoted 2 lines

What happens if the remote core is unable to action the shutdown request

We abort the shutdown sequence if the remoteproc does not respond with an ACK within the timeout assuming rproc is busy doing some work.

Quoted 1 line

(maybe it has crashed).

Quoted 8 lines
Quoted 7 lines

remoteproc core has the infra to handle rproc crash. It initiates a recovery mechanism by stopping and starting the rproc with the same firmware.

Are you suggesting that we check if rproc_stop() is invoked from a recovery context, and forcefully reset the rproc without sending/waiting for SHUTDOWN msg as a crashed core can't respond to mbox irqs?

I think that is a fair ask.

Would need to pass on the bool crashed parameter to the stop() rproc_ops from rproc_stop(). Will address in v3...

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Is there a way to cleanup resources which the remote core allocated without rebooting the whole system?

For SW resources (like mem, vdev): Yes
However, I feel this is currently missing in rproc core. We should be making a call to rproc_resource_cleanup() in rproc_boot_recovery()'s error paths and in rproc_crash_handler_work() in case of subsequent crashes.

Quoted 5 lines
Quoted 1 line

^^ Mathieu, thoughts about the above?

So far a crash has been treated like a rproc_stop()/rproc_start() operation. Doing resource cleanup as part of a crash recovery would akin to a rproc_shutdown()/rproc_boot() operation, introducing a lot of churn and backward compatibility issues.

Makes sense to me. We're better off.

Thanks,
Beleswar

Quoted 265 lines
Quoted 265 lines

For HW resources: No
In TI Device Manager (DM) firmware, only the entity which requested a resource can relinquish it, no other host can do that cleanup on behalf of that entity. So, we can't do much here.

Thanks,
Beleswar

Quoted 256 lines

Patrick

Quoted 254 lines

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
        RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
        RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
        RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-       RP_MBOX_END_MSG         = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+       RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+       RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +72 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *     Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
        case RP_MBOX_ECHO_REPLY:
                dev_info(dev, "received echo reply from %s\n", rproc->name);
                break;
+       case RP_MBOX_SHUTDOWN_ACK:
+               dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+               complete(&kproc->shutdown_complete);
+               break;
        default:
                /* silently handle all other valid messages */
                if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+       int ret;
+       u64 boot_vec;
+       u32 cfg, ctrl, stat;
+
+       ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+       if (ret)
+               return false;
+
+       return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+       bool wfi_status = false;
+       int ret;
+
+       reinit_completion(&kproc->shutdown_complete);
+
+       ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+       if (ret < 0) {
+               dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+               return ret;
+       }
+
+       ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                         msecs_to_jiffies(5000));
+       if (ret == 0) {
+               dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                       __func__);
+               return -EBUSY;
+       }
+
+       ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                                200, 2000);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
        struct k3_rproc *kproc = rproc->priv;
+       int ret;
+
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
 
        return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK      (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI          0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
        u32 ti_sci_id;
        struct mbox_chan *mbox;
        struct mbox_client client;
+       struct completion shutdown_complete;
        void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
        if (ret)
                return ret;
 
+       init_completion(&kproc->shutdown_complete);
+
        ret = k3_rproc_of_get_memories(pdev, kproc);
        if (ret)
                return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
        struct k3_r5_cluster *cluster = core->cluster;
        int ret;
 
+       ret = notify_shutdown_rproc(kproc);
+       if (ret)
+               return ret;
+
        /* halt all applicable cores */
        if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
                list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                        goto out;
                }
 
+               init_completion(&kproc->shutdown_complete);
 init_rmem:
                k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

maintainer
#6 lore raw
In reply to Beleswar Padhi

Hi Beleswar,

Quoted 153 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
 	RP_MBOX_SUSPEND_SYSTEM	= 0xFFFFFF11,
 	RP_MBOX_SUSPEND_ACK	= 0xFFFFFF12,
 	RP_MBOX_SUSPEND_CANCEL	= 0xFFFFFF13,
-	RP_MBOX_END_MSG		= 0xFFFFFF14,
+	RP_MBOX_SHUTDOWN	= 0xFFFFFF14,
+	RP_MBOX_SHUTDOWN_ACK	= 0xFFFFFF15,
+	RP_MBOX_END_MSG		= 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +48 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *	Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
 	case RP_MBOX_ECHO_REPLY:
 		dev_info(dev, "received echo reply from %s\n", rproc->name);
 		break;
+	case RP_MBOX_SHUTDOWN_ACK:
+		dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+		complete(&kproc->shutdown_complete);
+		break;
 	default:
 		/* silently handle all other valid messages */
 		if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+	int ret;
+	u64 boot_vec;
+	u32 cfg, ctrl, stat;
+
+	ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+	if (ret)
+		return false;
+
+	return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+	bool wfi_status = false;
+	int ret;
+
+	reinit_completion(&kproc->shutdown_complete);
+
+	ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+	if (ret < 0) {
+		dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+		return ret;

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 10 lines
drivers/remoteproc/ti_k3_common.c +10 -0
+	}
+
+	ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+					  msecs_to_jiffies(5000));
+	if (ret == 0) {
+		dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+			__func__);
+		return -EBUSY;
+	}
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+	ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+				 200, 2000);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
 	struct k3_rproc *kproc = rproc->priv;
+	int ret;
+
+	ret = notify_shutdown_rproc(kproc);
+	if (ret)
+		return ret;
 
 	return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK	(SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI		0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
 	u32 ti_sci_id;
 	struct mbox_chan *mbox;
 	struct mbox_client client;
+	struct completion shutdown_complete;
 	void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	init_completion(&kproc->shutdown_complete);
+
 	ret = k3_rproc_of_get_memories(pdev, kproc);
 	if (ret)
 		return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	init_completion(&kproc->shutdown_complete);
+
 	ret = k3_rproc_of_get_memories(pdev, kproc);
 	if (ret)
 		return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -0
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
 	struct k3_r5_cluster *cluster = core->cluster;
 	int ret;
 
+	ret = notify_shutdown_rproc(kproc);
+	if (ret)
+		return ret;
+
 	/* halt all applicable cores */
 	if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
 		list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
 			goto out;
 		}
 
+		init_completion(&kproc->shutdown_complete);
 init_rmem:
 		k3_r5_adjust_tcm_sizes(kproc);
 
Signature · 1 line
2.34.1
#7 lore raw

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

Quoted 162 lines · Mathieu Poirier

Hi Beleswar,

Quoted 159 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
 	RP_MBOX_SUSPEND_SYSTEM	= 0xFFFFFF11,
 	RP_MBOX_SUSPEND_ACK	= 0xFFFFFF12,
 	RP_MBOX_SUSPEND_CANCEL	= 0xFFFFFF13,
-	RP_MBOX_END_MSG		= 0xFFFFFF14,
+	RP_MBOX_SHUTDOWN	= 0xFFFFFF14,
+	RP_MBOX_SHUTDOWN_ACK	= 0xFFFFFF15,
+	RP_MBOX_END_MSG		= 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +54 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *	Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
 	case RP_MBOX_ECHO_REPLY:
 		dev_info(dev, "received echo reply from %s\n", rproc->name);
 		break;
+	case RP_MBOX_SHUTDOWN_ACK:
+		dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+		complete(&kproc->shutdown_complete);
+		break;
 	default:
 		/* silently handle all other valid messages */
 		if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+	int ret;
+	u64 boot_vec;
+	u32 cfg, ctrl, stat;
+
+	ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+	if (ret)
+		return false;
+
+	return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+	bool wfi_status = false;
+	int ret;
+
+	reinit_completion(&kproc->shutdown_complete);
+
+	ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+	if (ret < 0) {
+		dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+		return ret;
+	}
+
+	ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+					  msecs_to_jiffies(5000));
+	if (ret == 0) {
+		dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 4 lines
drivers/remoteproc/ti_k3_common.c +4 -0
+			__func__);
+		return -EBUSY;
+	}
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 107 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+	ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+				 200, 2000);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
 	struct k3_rproc *kproc = rproc->priv;
+	int ret;
+
+	ret = notify_shutdown_rproc(kproc);
+	if (ret)
+		return ret;
 
 	return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK	(SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI		0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
 	u32 ti_sci_id;
 	struct mbox_chan *mbox;
 	struct mbox_client client;
+	struct completion shutdown_complete;
 	void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	init_completion(&kproc->shutdown_complete);
+
 	ret = k3_rproc_of_get_memories(pdev, kproc);
 	if (ret)
 		return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
+	init_completion(&kproc->shutdown_complete);
+
 	ret = k3_rproc_of_get_memories(pdev, kproc);
 	if (ret)
 		return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -0
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
 	struct k3_r5_cluster *cluster = core->cluster;
 	int ret;
 
+	ret = notify_shutdown_rproc(kproc);
+	if (ret)
+		return ret;
+
 	/* halt all applicable cores */
 	if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
 		list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
 			goto out;
 		}
 
+		init_completion(&kproc->shutdown_complete);
 init_rmem:
 		k3_r5_adjust_tcm_sizes(kproc);
 
Signature · 1 line
2.34.1
maintainer
#8 lore raw
Quoted 7 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 173 lines · Mathieu Poirier
Quoted 162 lines

Hi Beleswar,

Quoted 159 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +54 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 4 lines
drivers/remoteproc/ti_k3_common.c +4 -0
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Quoted 114 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 107 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#9 lore raw
Quoted 193 lines · Mathieu Poirier
Quoted 7 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 173 lines · Mathieu Poirier
Quoted 162 lines

Hi Beleswar,

Quoted 159 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +54 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 4 lines
drivers/remoteproc/ti_k3_common.c +4 -0
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

A flag in a resource table enabling the new behaviour could work, but we would probably need another way to do the new thing, maybe with a devicetree flag. Why? Because people are running TI's kernel, which has had this feature since Feb 2025, and may want to migrate to a mainline kernel. Those firmwares already use the handshake.

If we want to be nice to existing users, we should provide a way to be compatible with existing firmwares which don't support RP_MBOX_SHUTDOWN, and with existing firmwares which do.

That said, restarting remote processors on k3 was quite broken without the shutdown handshake, often leading to hangs/crashes requiring a full system reboot to recover. This is why I previously asked about recovery if the remoteproc crashes or is unable to handle the shutdown request.

I suspect that most real world users who are actually restarting remoteprocs on k3 are already using the handshake with TI's kernel.

IMHO it's probably fine to just send RP_MBOX_SHUTDOWN to firmwares which don't support it, and handle old firmwares via the same recovery path as new firmwares which have crashed. This would mean that upgrading a system with an old firmware to a new kernel will have an additional delay when shutting down a remoteproc, but realistically, this shutdown path was broken anyway.

Patrick

Quoted 115 lines
Quoted 114 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 107 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#10 lore raw
Quoted 197 lines · Patrick Oppenlander
Quoted 187 lines · Mathieu Poirier
Quoted 6 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 171 lines · Mathieu Poirier
Quoted 161 lines

Hi Beleswar,

Quoted 158 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +53 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 5 lines
drivers/remoteproc/ti_k3_common.c +5 -0
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Quoted 3 lines

A flag in a resource table enabling the new behaviour could work, but we would probably need another way to do the new thing, maybe with a devicetree flag.

That's hacky. Device tree is for hardware description only. We should not be putting SW required quirks in DT. It should be rightly placed in the vendor specific resource table.

Quoted 3 lines

Why? Because people are running TI's kernel, which
has had this feature since Feb 2025, and may want to migrate to a mainline kernel. Those firmwares already use the handshake.

Why should Upstream Linux care about the mess-ups in the vendor specific kernel? It should be the other way around, the feature should have been a part of the upstream kernel first, otherwise such a rework is expected.

Quoted 4 lines

If we want to be nice to existing users, we should provide a way to be compatible with existing firmwares which don't support RP_MBOX_SHUTDOWN, and with existing firmwares which do.

Existing users of *TI Kernel*, not *Upstream Kernel*. This is a miss from TI side not to have worked the feature upstream first, and for the same reason upstream need not care about maintaining backward compat with vendor specific features.

Thanks,
Beleswar

Quoted 132 lines

That said, restarting remote processors on k3 was quite broken without the shutdown handshake, often leading to hangs/crashes requiring a full system reboot to recover. This is why I previously asked about recovery if the remoteproc crashes or is unable to handle the shutdown request.

I suspect that most real world users who are actually restarting remoteprocs on k3 are already using the handshake with TI's kernel.

IMHO it's probably fine to just send RP_MBOX_SHUTDOWN to firmwares which don't support it, and handle old firmwares via the same recovery path as new firmwares which have crashed. This would mean that upgrading a system with an old firmware to a new kernel will have an additional delay when shutting down a remoteproc, but realistically, this shutdown path was broken anyway.

Patrick

Quoted 113 lines
Quoted 113 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 106 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#11 lore raw
Quoted 200 lines · Beleswar Prasad Padhi
Quoted 197 lines · Patrick Oppenlander
Quoted 195 lines · Mathieu Poirier
Quoted 6 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 171 lines · Mathieu Poirier
Quoted 161 lines

Hi Beleswar,

Quoted 158 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +53 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 5 lines
drivers/remoteproc/ti_k3_common.c +5 -0
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Quoted 8 lines
Quoted 3 lines

A flag in a resource table enabling the new behaviour could work, but we would probably need another way to do the new thing, maybe with a devicetree flag.

That's hacky. Device tree is for hardware description only. We should not be putting SW required quirks in DT. It should be rightly placed in the vendor specific resource table.

There's plenty of places in devicetree such things are already done (most stuff starting with "linux,", and plenty of others).

Quoted 16 lines
Quoted 3 lines

Why? Because people are running TI's kernel, which
has had this feature since Feb 2025, and may want to migrate to a mainline kernel. Those firmwares already use the handshake.

Why should Upstream Linux care about the mess-ups in the vendor specific kernel? It should be the other way around, the feature should have been a part of the upstream kernel first, otherwise such a rework is expected.

Quoted 4 lines

If we want to be nice to existing users, we should provide a way to be compatible with existing firmwares which don't support RP_MBOX_SHUTDOWN, and with existing firmwares which do.

Quoted 5 lines

Existing users of *TI Kernel*, not *Upstream Kernel*. This is a miss from TI side not to have worked the feature upstream first, and for the same reason upstream need not care about maintaining backward compat with vendor specific features.

Sure. I hope that TI does better in the future.

Unfortunately that doesn't help here, and it definitely doesn't change the fact that there are real users and real products out there which are built on and have shipped relying on the behaviours in TI's kernel, because the upstream support was broken.

Patrick

Quoted 135 lines

Thanks,
Beleswar

Quoted 132 lines

That said, restarting remote processors on k3 was quite broken without the shutdown handshake, often leading to hangs/crashes requiring a full system reboot to recover. This is why I previously asked about recovery if the remoteproc crashes or is unable to handle the shutdown request.

I suspect that most real world users who are actually restarting remoteprocs on k3 are already using the handshake with TI's kernel.

IMHO it's probably fine to just send RP_MBOX_SHUTDOWN to firmwares which don't support it, and handle old firmwares via the same recovery path as new firmwares which have crashed. This would mean that upgrading a system with an old firmware to a new kernel will have an additional delay when shutting down a remoteproc, but realistically, this shutdown path was broken anyway.

Patrick

Quoted 113 lines
Quoted 113 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 106 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#12 lore raw
Quoted 3 lines · Patrick Oppenlander
Quoted 2 lines · Beleswar Prasad Padhi

On 15/01/26 03:57, Patrick Oppenlander wrote:

[...]

Quoted 55 lines
Quoted 55 lines
Quoted 54 lines
Quoted 43 lines
Quoted 36 lines
Quoted 28 lines
Quoted 28 lines

[...]

(quoted diff) +27 -0
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

A flag in a resource table enabling the new behaviour could work, but we would probably need another way to do the new thing, maybe with a devicetree flag.

Quoted 5 lines
Quoted 3 lines

That's hacky. Device tree is for hardware description only. We should not be putting SW required quirks in DT. It should be rightly placed in the vendor specific resource table.

There's plenty of places in devicetree such things are already done (most stuff starting with "linux,", and plenty of others).

Yeah, but we should not keep repeating the same mistake :) DT is for non-discoverable hardware. This sort of info can always be grabbed from querying firmware at runtime or from the resource table at build time. The Devicetree is not just used by Linux, but various other projects (bootloaders like U-Boot to count) which might not make use of those custom "Linux" properties we put here.

Thanks,
Beleswar

Quoted 1 line

[...]

maintainer
#13 lore raw
In reply to Patrick Oppenlander
Quoted 205 lines · Patrick Oppenlander
Quoted 193 lines · Mathieu Poirier
Quoted 7 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 173 lines · Mathieu Poirier
Quoted 162 lines

Hi Beleswar,

Quoted 159 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +54 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 4 lines
drivers/remoteproc/ti_k3_common.c +4 -0
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Quoted 6 lines

A flag in a resource table enabling the new behaviour could work, but we would probably need another way to do the new thing, maybe with a devicetree flag. Why? Because people are running TI's kernel, which has had this feature since Feb 2025, and may want to migrate to a mainline kernel. Those firmwares already use the handshake.

We can't expect upstream to be compatible with what is happening in vendor kernels, it simply doesn't scale. Moreover, a devicetree flag would mean two ways of supporting the same feature, which quickly becomes a maintenance nightmare.

Quoted 4 lines

If we want to be nice to existing users, we should provide a way to be compatible with existing firmwares which don't support RP_MBOX_SHUTDOWN, and with existing firmwares which do.

That is indeed what I suggested.

Quoted 9 lines

That said, restarting remote processors on k3 was quite broken without the shutdown handshake, often leading to hangs/crashes requiring a full system reboot to recover. This is why I previously asked about recovery if the remoteproc crashes or is unable to handle the shutdown request.

I suspect that most real world users who are actually restarting remoteprocs on k3 are already using the handshake with TI's kernel.

Quoted 7 lines

IMHO it's probably fine to just send RP_MBOX_SHUTDOWN to firmwares which don't support it, and handle old firmwares via the same recovery path as new firmwares which have crashed. This would mean that upgrading a system with an old firmware to a new kernel will have an additional delay when shutting down a remoteproc, but realistically, this shutdown path was broken anyway.

I'm sure there are systems where the extra delay will cause trouble, hence the need to add the flexibility to keep the original behavior when the remote processor isn't expecting a handshake at shutdown time.

Quoted 117 lines

Patrick

Quoted 115 lines
Quoted 114 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 107 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#14 lore raw
Quoted 220 lines · Mathieu Poirier
Quoted 205 lines · Patrick Oppenlander
Quoted 193 lines · Mathieu Poirier
Quoted 7 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 173 lines · Mathieu Poirier
Quoted 162 lines

Hi Beleswar,

Quoted 159 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +54 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 4 lines
drivers/remoteproc/ti_k3_common.c +4 -0
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Quoted 6 lines

A flag in a resource table enabling the new behaviour could work, but we would probably need another way to do the new thing, maybe with a devicetree flag. Why? Because people are running TI's kernel, which has had this feature since Feb 2025, and may want to migrate to a mainline kernel. Those firmwares already use the handshake.

We can't expect upstream to be compatible with what is happening in vendor kernels, it simply doesn't scale. Moreover, a devicetree flag would mean two ways of supporting the same feature, which quickly becomes a maintenance nightmare.

Quoted 6 lines
Quoted 4 lines

If we want to be nice to existing users, we should provide a way to be compatible with existing firmwares which don't support RP_MBOX_SHUTDOWN, and with existing firmwares which do.

That is indeed what I suggested.

Then we can't add a feature flag to the resource table as existing firmwares which expect RP_MBOX_SHUTDOWN won't have the flag set.

Quoted 17 lines
Quoted 9 lines

That said, restarting remote processors on k3 was quite broken without the shutdown handshake, often leading to hangs/crashes requiring a full system reboot to recover. This is why I previously asked about recovery if the remoteproc crashes or is unable to handle the shutdown request.

I suspect that most real world users who are actually restarting remoteprocs on k3 are already using the handshake with TI's kernel.

Quoted 7 lines

IMHO it's probably fine to just send RP_MBOX_SHUTDOWN to firmwares which don't support it, and handle old firmwares via the same recovery path as new firmwares which have crashed. This would mean that upgrading a system with an old firmware to a new kernel will have an additional delay when shutting down a remoteproc, but realistically, this shutdown path was broken anyway.

Quoted 4 lines

I'm sure there are systems where the extra delay will cause trouble, hence the need to add the flexibility to keep the original behavior when the remote processor isn't expecting a handshake at shutdown time.

Personally I think this is unlikely to cause problems as shutting down the remoteproc in this way really is very unreliable. We would be adding a small additional delay to an already buggy code path. Systems in the wild likely fall into two categories:
1. Remoteproc is started and never stopped.
2. Remoteproc already uses RP_MBOX_SHUTDOWN.

Our product falls into the 2nd category. We ship firmwares which support RP_MBOX_SHUTDOWN, and use TI's kernel for now. We will switch to an LTS kernel at some point as we have an expected product life of at least 20 years. Changing our remoteproc firmwares to add an additional flag to the resource table may sound trivial, but may invalidate expensive compliance certifications as the firmware hashes will change. We can work around this by carrying an additional kernel patch, but even though it's a trivial thing to do, it still incurs a non-zero maintenance cost in the long term.

Patrick

Quoted 117 lines
Quoted 117 lines

Patrick

Quoted 115 lines
Quoted 114 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 107 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

#15 lore raw
In reply to Mathieu Poirier
Quoted 187 lines · Mathieu Poirier
Quoted 6 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 171 lines · Mathieu Poirier
Quoted 161 lines

Hi Beleswar,

Quoted 158 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +53 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 5 lines
drivers/remoteproc/ti_k3_common.c +5 -0
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Fair... I have let the internal firmware teams know about this requirement. Once we have a compatible firmware, I will refresh v3 of this patch on Linux side.

Thanks,
Beleswar

Quoted 114 lines
Quoted 114 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 106 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1

maintainer
#16 lore raw
Quoted 200 lines · Beleswar Prasad Padhi
Quoted 187 lines · Mathieu Poirier
Quoted 6 lines · Beleswar Prasad Padhi

Hi Mathieu,

Sorry for the delay in response here. Somehow all the messages in this thread ended up in spam. Didn't realize there were new msgs until I looked up on lore.

I've been getting weird automated email replies from TI.

Quoted 171 lines · Mathieu Poirier
Quoted 161 lines

Hi Beleswar,

Quoted 158 lines · Beleswar Padhi

From: Richard Genoud <[email protected]>

Introduce software IPC handshake between the host running Linux and the remote processors to gracefully stop/reset the remote core.

Upon a stop request, remoteproc driver sends a RP_MBOX_SHUTDOWN mailbox message to the remotecore.
The remote core is expected to:
- relinquish all the resources acquired through Device Manager (DM)
- disable its interrupts
- send back a mailbox acknowledgment RP_MBOX_SHUDOWN_ACK
- enter WFI state.

Meanwhile, the K3 remoteproc driver does:
- wait for the RP_MBOX_SHUTDOWN_ACK from the remote core
- wait for the remoteproc to enter WFI state
- reset the remote core through device manager

Based on work from: Hari Nagalla <[email protected]>

[[email protected]: Extend support to all rprocs]

---
v2: Changelog:
1. Extend graceful shutdown support for all rprocs (R5, DSP, M4)
2. Halt core only if SHUTDOWN_ACK is received from rproc and it has entered WFI state.
3. Convert return type of is_core_in_wfi() to bool. Works better with readx_poll_timeout() condition.
4. Cast RP_MBOX_SHUTDOWN to uintptr_t to suppress compiler warnings when void* is 64 bit.
5. Wrapped Graceful shutdown code in the form of notify_shutdown_rproc function.
6. Updated commit message to fix minor typos and such.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Testing done:
1. Tested Boot across all TI K3 EVM/SK boards.
2. Tested IPC on all TI K3 J7* EVM/SK boards (& AM62x SK).
4. Tested R5 rprocs can now be shutdown and powered back on from userspace.
3. Tested that each patch in the series generates no new warnings/errors.

 drivers/remoteproc/omap_remoteproc.h      |  9 ++-
 drivers/remoteproc/ti_k3_common.c         | 72 +++++++++++++++++++++++
 drivers/remoteproc/ti_k3_common.h         |  4 ++
 drivers/remoteproc/ti_k3_dsp_remoteproc.c |  2 +
 drivers/remoteproc/ti_k3_m4_remoteproc.c  |  2 +
 drivers/remoteproc/ti_k3_r5_remoteproc.c  |  5 ++
 6 files changed, 93 insertions(+), 1 deletion(-)
drivers/remoteproc/omap_remoteproc.h +8 -1
index 828e13256c023..c008f11fa2a43 100644
@@ -42,6 +42,11 @@
  * @RP_MBOX_SUSPEND_CANCEL: a cancel suspend response from a remote processor
  * on a suspend request
  *
+ * @RP_MBOX_SHUTDOWN: shutdown request for the remote processor
+ *
+ * @RP_MBOX_SHUTDOWN_ACK: successful response from remote processor for a
+ * shutdown request. The remote processor should be in WFI state short after.
+ *
  * Introduce new message definitions if any here.
  *
  * @RP_MBOX_END_MSG: Indicates end of known/defined messages from remote core
@@ -59,7 +64,9 @@ enum omap_rp_mbox_messages {
     RP_MBOX_SUSPEND_SYSTEM  = 0xFFFFFF11,
     RP_MBOX_SUSPEND_ACK     = 0xFFFFFF12,
     RP_MBOX_SUSPEND_CANCEL  = 0xFFFFFF13,
-    RP_MBOX_END_MSG         = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN        = 0xFFFFFF14,
+    RP_MBOX_SHUTDOWN_ACK    = 0xFFFFFF15,
+    RP_MBOX_END_MSG         = 0xFFFFFF16,
 };
 
 #endif /* _OMAP_RPMSG_H */
drivers/remoteproc/ti_k3_common.c +53 -0
index 56b71652e449f..5d469f65115c3 100644
@@ -18,7 +18,9 @@
  *  Hari Nagalla <[email protected]>
  */
 
+#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
@@ -69,6 +71,10 @@ void k3_rproc_mbox_callback(struct mbox_client *client, void *data)
     case RP_MBOX_ECHO_REPLY:
             dev_info(dev, "received echo reply from %s\n", rproc->name);
             break;
+    case RP_MBOX_SHUTDOWN_ACK:
+            dev_dbg(dev, "received shutdown_ack from %s\n", rproc->name);
+            complete(&kproc->shutdown_complete);
+            break;
     default:
             /* silently handle all other valid messages */
             if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
@@ -188,6 +194,67 @@ int k3_rproc_request_mbox(struct rproc *rproc)
 }
 EXPORT_SYMBOL_GPL(k3_rproc_request_mbox);
 
+/**
+ * is_core_in_wfi - Utility function to check core status
+ * @kproc: remote core pointer used for checking core status
+ *
+ * This utility function is invoked by the shutdown sequence to ensure
+ * the remote core is in wfi, before asserting a reset.
+ */
+bool is_core_in_wfi(struct k3_rproc *kproc)
+{
+    int ret;
+    u64 boot_vec;
+    u32 cfg, ctrl, stat;
+
+    ret = ti_sci_proc_get_status(kproc->tsp, &boot_vec, &cfg, &ctrl, &stat);
+    if (ret)
+            return false;
+
+    return (bool)(stat & PROC_BOOT_STATUS_FLAG_CPU_WFI);
+}
+EXPORT_SYMBOL_GPL(is_core_in_wfi);
+
+/**
+ * notify_shutdown_rproc - Prepare the remoteproc for a shutdown
+ * @kproc: remote core pointer used for sending mbox msg
+ *
+ * This function sends the shutdown prepare message to remote processor and
+ * waits for an ACK. Further, it checks if the remote processor has entered
+ * into WFI mode. It is invoked in shutdown sequence to ensure the rproc
+ * has relinquished its resources before asserting a reset, so the shutdown
+ * happens cleanly.
+ */
+int notify_shutdown_rproc(struct k3_rproc *kproc)
+{
+    bool wfi_status = false;
+    int ret;
+
+    reinit_completion(&kproc->shutdown_complete);
+
+    ret = mbox_send_message(kproc->mbox, (void *)(uintptr_t)RP_MBOX_SHUTDOWN);
+    if (ret < 0) {
+            dev_err(kproc->dev, "PM mbox_send_message failed: %d\n", ret);
+            return ret;
+    }
+
+    ret = wait_for_completion_timeout(&kproc->shutdown_complete,
+                                      msecs_to_jiffies(5000));
+    if (ret == 0) {
Quoted 8 lines

drivers/remoteproc/ti_k3_common.c int k3_rproc_request_mbox(struct rproc *rproc) · line 248

Quoted 5 lines
drivers/remoteproc/ti_k3_common.c +5 -0
+            dev_err(kproc->dev, "%s: timeout waiting for rproc completion event\n",
+                    __func__);
+            return -EBUSY;
+    }
+

Won't that create an issue on systems with an older FW that doesn't send a RP_MBOX_SHUDOWN_ACK message? Unless I'm missing something, this kind of feature needs to be backward compatible.

Quoted 7 lines

I feel it would be unsafe to just abruptly power off a core without some handshake.. The core could be executing something, there could be pending bus transactions leading to system hangs etc.. We start the IPC mechanism with a handshake, so we should end it with a handshake too.. And for firmwares that don't support this handshake, IMO its better to reject the shutdown request. What do you think?

Quoted 8 lines

We can't affect the behavior of systems where old FW is coupled with a new kernel. If people want to address the bugs you referred to, they can update their FW as they see fit. As such things need to be backward compatible. NXP has recently implemented a handshake mechanism such as this, but to assert the readiness of a booting remote processor. They used the vendor specific resource table to store a flag that enables the handshake - I suggest using the same heuristic to implement this feature.

Quoted 3 lines

Fair... I have let the internal firmware teams know about this requirement. Once we have a compatible firmware, I will refresh v3 of this patch on Linux side.

Very well.

Quoted 118 lines

Thanks,
Beleswar

Quoted 114 lines
Quoted 114 lines

For older TI firmwares also, doing rproc_stop() resulted in those intermittent bugs as mentioned above. So we never really supported the stop() feature until now.

Thanks,
Beleswar

Quoted 106 lines

Thanks,
Mathieu

Quoted 103 lines
drivers/remoteproc/ti_k3_common.c +14 -0
+    ret = readx_poll_timeout(is_core_in_wfi, kproc, wfi_status, wfi_status,
+                             200, 2000);
+    if (ret)
+            return ret;
+
+    return 0;
+}
+EXPORT_SYMBOL_GPL(notify_shutdown_rproc);
+
 /*
  * The K3 DSP and M4 cores have a local reset that affects only the CPU, and a
  * generic module reset that powers on the device and allows the internal
@@ -288,6 +355,11 @@ EXPORT_SYMBOL_GPL(k3_rproc_start);
 int k3_rproc_stop(struct rproc *rproc)
 {
     struct k3_rproc *kproc = rproc->priv;
+    int ret;
+
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
 
     return k3_rproc_reset(kproc);
 }
drivers/remoteproc/ti_k3_common.h +4 -0
index aee3c28dbe510..2a025f4894b82 100644
@@ -22,6 +22,7 @@
 #define REMOTEPROC_TI_K3_COMMON_H
 
 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK   (SZ_16M - 1)
+#define PROC_BOOT_STATUS_FLAG_CPU_WFI               0x00000002
 
 /**
  * struct k3_rproc_mem - internal memory structure
@@ -92,6 +93,7 @@ struct k3_rproc {
     u32 ti_sci_id;
     struct mbox_chan *mbox;
     struct mbox_client client;
+    struct completion shutdown_complete;
     void *priv;
 };
 
@@ -115,4 +117,6 @@ int k3_rproc_of_get_memories(struct platform_device *pdev,
 void k3_mem_release(void *data);
 int k3_reserved_mem_init(struct k3_rproc *kproc);
 void k3_release_tsp(void *data);
+bool is_core_in_wfi(struct k3_rproc *kproc);
+int notify_shutdown_rproc(struct k3_rproc *kproc);
 #endif /* REMOTEPROC_TI_K3_COMMON_H */
drivers/remoteproc/ti_k3_dsp_remoteproc.c +2 -0
index d6ceea6dc920e..156ae09d8ee25 100644
@@ -133,6 +133,8 @@ static int k3_dsp_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_m4_remoteproc.c +2 -0
index 3a11fd24eb52b..64d99071279b0 100644
@@ -90,6 +90,8 @@ static int k3_m4_rproc_probe(struct platform_device *pdev)
     if (ret)
             return ret;
 
+    init_completion(&kproc->shutdown_complete);
+
     ret = k3_rproc_of_get_memories(pdev, kproc);
     if (ret)
             return ret;
drivers/remoteproc/ti_k3_r5_remoteproc.c +5 -1
index 04f23295ffc10..8748dc6089cc2 100644
@@ -533,6 +533,10 @@ static int k3_r5_rproc_stop(struct rproc *rproc)
     struct k3_r5_cluster *cluster = core->cluster;
     int ret;
 
+    ret = notify_shutdown_rproc(kproc);
+    if (ret)
+            return ret;
+
     /* halt all applicable cores */
     if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
             list_for_each_entry(core, &cluster->cores, elem) {
@@ -1129,6 +1133,7 @@ static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
                     goto out;
             }
 
+            init_completion(&kproc->shutdown_complete);
 init_rmem:
             k3_r5_adjust_tcm_sizes(kproc);
 
--

2.34.1