Skip to contentSkip to conversation

xen/riscv: make machine_restart() reboot

PATCH Superseded Anandu Padinhare 2 participants 9 messages Dec 2 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/riscv: make machine_restart() reboot

On RISC-V, machine_restart() ignores its delay and powers off instead of rebooting, so a panic's "Reboot in five seconds..." ends in an immediate power-off.

Patch 1 implements udelay() using the time CSR, as Linux does. Until the timebase frequency is known, it returns without waiting.

Patch 2 uses the SBI SRST extension, when present, to reboot after the delay. Without SRST, it falls back to the legacy SHUTDOWN call as before, now after the delay. Like Arm and x86, it calls console_start_sync() first, so pending console output isn't lost.

Patch 3 passes -no-reboot to QEMU in the RISC-V smoke test. Without it, a panic there would now reboot until the test times out, instead of failing the job straight away.

A panic that recurs early in every boot (e.g. "-cpu rv64,svpbmt=off" on QEMU virt) is now a reboot loop. "noreboot" still powers off. machine_halt() still uses the legacy SHUTDOWN call. I intend to switch it to SRST in a follow-up.

Tested on QEMU 10.2.1 virt with OpenSBI v1.8: early and late panics, "noreboot", SRST missing or failing, timebase-frequency overridden in the DT, and the smoke test script with a passing and a panicking Xen. Not tested on real hardware.

Changes in v2:
- Patch 1: reword the commit message and return early when cpu_khz is 0 (Baptiste).
- Patch 2: call console_start_sync() first and add comments (Baptiste).
- New patch 3: pass -no-reboot to QEMU in the RISC-V smoke test (Baptiste).

v1: https://lore.kernel.org/xen-devel/MA0PR01MB92178FEAFBB95A225781351CFD412@MA0PR01MB9217.INDPRD01.PROD.OUTLOOK.COM/

Anandu Padinhare (3):
xen/riscv: implement udelay()
xen/riscv: use the SBI SRST extension to reboot in machine_restart()
CI: Pass -no-reboot to QEMU in the RISC-V smoke test

 automation/scripts/qemu-smoke-riscv64.sh |  1 +
 xen/arch/riscv/Makefile                  |  1 +
 xen/arch/riscv/delay.c                   | 21 +++++++++++++++++++++
 xen/arch/riscv/include/asm/sbi.h         | 11 +++++++++++
 xen/arch/riscv/sbi.c                     | 21 +++++++++++++++++++++
 xen/arch/riscv/shutdown.c                | 16 +++++++++++-----
 xen/arch/riscv/stubs.c                   |  7 -------
 7 files changed, 66 insertions(+), 12 deletions(-)
 create mode 100644 xen/arch/riscv/delay.c

base-commit: 091d3c200281ea44846ccdf1e5d39985321c643e

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/3 xen/riscv: implement udelay()

udelay() is still a stub that hits BUG_ON(), so mdelay() can't be used on RISC-V, for example to honour the delay in machine_restart().

Implement udelay() by busy-waiting on the time CSR, as Linux's RISC-V __delay() does. Convert the delay to timer ticks once, multiplying before dividing and rounding up so that short delays aren't cut to zero ticks, then spin until that many ticks have passed.

cpu_khz is only set by preinit_xen_time(). Before that it is 0, and udelay() returns without waiting.

Assisted-by Claude

---
Changes in v2:
- Reword the commit message and return early when cpu_khz is 0 (Baptiste).

 xen/arch/riscv/Makefile |  1 +
 xen/arch/riscv/delay.c  | 21 +++++++++++++++++++++
 xen/arch/riscv/stubs.c  |  7 -------
 3 files changed, 22 insertions(+), 7 deletions(-)
 create mode 100644 xen/arch/riscv/delay.c
xen/arch/riscv/Makefile +1 -0
index 665ec26933..a9aa7d1b2f 100644
@@ -1,6 +1,7 @@
 obj-y += aia.o
 obj-y += aplic.o
 obj-y += cpufeature.o
+obj-y += delay.o
 obj-y += device.init.o
 obj-y += domain.o
 obj-y += domain-build.init.o
xen/arch/riscv/delay.c new file +21 -0
new file mode 100644
index 0000000000..e6ba9b3a5a
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/delay.h>
+#include <xen/macros.h>
+#include <xen/time.h>
+
+#include <asm/processor.h>
+
+void udelay(unsigned long usecs)
+{
+    cycles_t ticks, start;
+
+    if ( !cpu_khz )
+        return;
+
+    ticks = DIV_ROUND_UP((cycles_t)usecs * cpu_khz, 1000);
+    start = get_cycles();
+
+    while ( get_cycles() - start < ticks )
+        cpu_relax();
+}
xen/arch/riscv/stubs.c +0 -7
index 3a7953593d..98231e39cc 100644
@@ -240,13 +240,6 @@ int guest_physmap_remove_page(struct domain *d, gfn_t gfn, mfn_t mfn,
     BUG_ON("unimplemented");
 }
 
-/* delay.c */
-
-void udelay(unsigned long usecs)
-{
-    BUG_ON("unimplemented");
-}
-
 /* guest_access.h */
 
 static inline unsigned long raw_clear_guest(void *to, unsigned int len)
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 1/3] xen/riscv: implement udelay()

Thanks for v2!

xen/arch/riscv/delay.c

Quoted 3 lines

[ ... 39 lines skipped ... ]

xen/arch/riscv/delay.c new file +0 -0

Do we need a new file for a single function? cpu_khz and get_s_time() already live in time.c, and Arm puts its udelay() there too (xen/arch/arm/time.c). Moving it there would avoid the new file and the Makefile change.

Signature · 1 line
Baptiste Le Duc <[email protected]>
#3 lore raw

Re: [PATCH v2 1/3] xen/riscv: implement udelay()

Quoted 4 lines · Baptiste Le Duc

Do we need a new file for a single function? cpu_khz and get_s_time() already live in time.c, and Arm puts its udelay() there too (xen/arch/arm/time.c). Moving it there would avoid the new file and the Makefile change.

x86 keeps its udelay() on its own in xen/arch/x86/delay.c, which is the layout I followed, but I'm happy to move it into time.c in v3 if that's preferred.

Thanks,
Anandu

#4 lore raw

Re: [PATCH v2 1/3] xen/riscv: implement udelay()

Quoted 9 lines · Anandu Padinhare
Quoted 4 lines · Baptiste Le Duc

Do we need a new file for a single function? cpu_khz and get_s_time() already live in time.c, and Arm puts its udelay() there too (xen/arch/arm/time.c). Moving it there would avoid the new file and the Makefile change.

x86 keeps its udelay() on its own in xen/arch/x86/delay.c, which is the layout I followed, but I'm happy to move it into time.c in v3 if that's preferred.

Yes I saw that, but we'd prefer to keep in time.c for the moment, thx.

Signature · 1 line
Baptiste Le Duc <[email protected]>
#5 lore raw

PATCH v2 2/3 xen/riscv: use the SBI SRST extension to reboot in machine_restart()

In reply to Anandu Padinhare

machine_restart() just calls machine_halt(), which uses the legacy SBI v0.1 SHUTDOWN call, so every reboot request, including the one after a panic ("Reboot in five seconds..."), powers the system off instead of rebooting it. Commit 6471badeeec9 ("RISCV/shutdown: Implement machine_{halt,restart}()") left SRST support for later.

Probe for the SBI System Reset (SRST) extension in sbi_init() and add sbi_system_reset(). Use it in machine_restart() to request a cold reboot. If SRST isn't available or the call returns, fall back to machine_halt() as before. A failed reset request is logged.

Now that udelay() exists, also honour delay_millisecs before resetting or falling back.

As Arm and x86 do, call console_start_sync() first, so that pending console output is flushed before the reset. panic() already does this, but other callers, such as the 'R' debug key, don't.

A panic after sbi_init() but before preinit_xen_time() gets no delay, so it now reboots straight away, and one that happens on every boot becomes a reboot loop. With "noreboot", Xen powers off at the panic, as before.

Assisted-by Claude

---
Changes in v2:
- Call console_start_sync() first and add comments (Baptiste).

 xen/arch/riscv/include/asm/sbi.h | 11 +++++++++++
 xen/arch/riscv/sbi.c             | 21 +++++++++++++++++++++
 xen/arch/riscv/shutdown.c        | 16 +++++++++++-----
 3 files changed, 43 insertions(+), 5 deletions(-)
xen/arch/riscv/include/asm/sbi.h +11 -0
index 2a245d0b42..eda426b623 100644
@@ -29,6 +29,7 @@
 
 #define SBI_EXT_BASE                    0x10
 #define SBI_EXT_RFENCE                  0x52464E43
+#define SBI_EXT_SRST                    0x53525354
 #define SBI_EXT_TIME                    0x54494D45
 
 /* SBI function IDs for BASE extension */
@@ -49,6 +50,13 @@
 #define SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA       0x5
 #define SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID  0x6
 
+/* SBI function IDs for SRST extension */
+#define SBI_EXT_SRST_RESET              0x0
+
+/* SBI SRST reset types and reasons */
+#define SBI_SRST_RESET_TYPE_COLD_REBOOT 0x1
+#define SBI_SRST_RESET_REASON_NONE      0x0
+
 /* SBI function IDs for TIME extension */
 #define SBI_EXT_TIME_SET_TIMER          0x0
 
@@ -84,6 +92,9 @@ void sbi_console_putchar(int ch);
 
 void sbi_shutdown(void);
 
+/* Returns only if SRST isn't available or the reset failed. */
+void sbi_system_reset(unsigned int type, unsigned int reason);
+
 /*
  * Check underlying SBI implementation has RFENCE
  *
xen/arch/riscv/sbi.c +21 -0
index 3576e26033..6898dc0dea 100644
@@ -25,6 +25,7 @@
 #include <asm/time.h>
 
 static unsigned long __ro_after_init sbi_spec_version = SBI_SPEC_VERSION_DEFAULT;
+static bool __ro_after_init sbi_has_srst;
 
 struct sbiret sbi_ecall(unsigned long ext, unsigned long fid,
                         unsigned long arg0, unsigned long arg1,
@@ -82,6 +83,20 @@ void sbi_shutdown(void)
     sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0, 0, 0, 0, 0, 0, 0);
 }
 
+void sbi_system_reset(unsigned int type, unsigned int reason)
+{
+    struct sbiret ret;
+
+    if ( !sbi_has_srst )
+        return;
+
+    ret = sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
+                    0, 0, 0, 0);
+
+    printk(XENLOG_ERR "%s: type=%u reason=%u failed (error %ld)\n",
+           __func__, type, reason, ret.error);
+}
+
 static unsigned int sbi_major_version(void)
 {
     return MASK_EXTR(sbi_spec_version, SBI_SPEC_VERSION_MAJOR_MASK);
@@ -381,6 +396,12 @@ int __init sbi_init(void)
             set_xen_timer = sbi_set_timer_v02;
         else
             set_xen_timer = sbi_set_timer_v01;
+
+        if ( sbi_probe_extension(SBI_EXT_SRST) > 0 )
+        {
+            sbi_has_srst = true;
+            dprintk(XENLOG_INFO, "SBI v0.3 SRST extension detected\n");
+        }
     }
     else
         panic("Ooops. SBI spec version 0.1 detected. Need to add support");
xen/arch/riscv/shutdown.c +11 -5
index 3513d9af47..7047a0df3e 100644
@@ -1,4 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <xen/console.h>
+#include <xen/delay.h>
 #include <xen/shutdown.h>
 
 #include <asm/sbi.h>
@@ -17,11 +19,15 @@ void machine_halt(void)
 
 void machine_restart(unsigned int delay_millisecs)
 {
-    /*
-     * TODO: mdelay(delay_millisecs)
-     * TODO: Probe for #SRST support, where sbi_system_reset() has a
-     *       shutdown/reboot parameter.
-     */
+    /* Flush pending console output and make later printk() synchronous. */
+    console_start_sync();
 
+    mdelay(delay_millisecs);
+
+    /* OpenSBI halts the other harts itself before resetting the system. */
+    sbi_system_reset(SBI_SRST_RESET_TYPE_COLD_REBOOT,
+                     SBI_SRST_RESET_REASON_NONE);
+
+    /* Fall back to halting if SRST isn't supported or failed. */
     machine_halt();
 }
Signature · 1 line
2.53.0
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] xen/riscv: use the SBI SRST extension to reboot in machine_restart()

Quoted 11 lines

machine_restart() just calls machine_halt(), which uses the legacy SBI v0.1 SHUTDOWN call, so every reboot request, including the one after a panic ("Reboot in five seconds..."), powers the system off instead of rebooting it. Commit 6471badeeec9 ("RISCV/shutdown: Implement machine_{halt,restart}()") left SRST support for later.

Probe for the SBI System Reset (SRST) extension in sbi_init() and add sbi_system_reset(). Use it in machine_restart() to request a cold reboot. If SRST isn't available or the call returns, fall back to machine_halt() as before. A failed reset request is logged.

Quoted 6 lines

Now that udelay() exists, also honour delay_millisecs before resetting or falling back.

As Arm and x86 do, call console_start_sync() first, so that pending console output is flushed before the reset. panic() already does this, but other callers, such as the 'R' debug key, don't.

Very clear like that, thanks.

xen/arch/riscv/include/asm/sbi.h line 56

Quoted 6 lines

[ ... 32 lines skipped ... ]

xen/arch/riscv/include/asm/sbi.h +4 -0
+/* SBI function IDs for SRST extension */
+#define SBI_EXT_SRST_RESET              0x0
+
+/* SBI SRST reset types and reasons */

Nit: Could we separate types from reasons as the list may evolve?

xen/arch/riscv/sbi.c void sbi_shutdown(void) · line 97

Quoted 13 lines

[ ... 31 lines skipped ... ]

xen/arch/riscv/sbi.c +12 -0
+void sbi_system_reset(unsigned int type, unsigned int reason)
+{
+    struct sbiret ret;
+
+    if ( !sbi_has_srst )
+        return;
+
+    ret = sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
+                    0, 0, 0, 0);
+
+    printk(XENLOG_ERR "%s: type=%u reason=%u failed (error %ld)\n",
+           __func__, type, reason, ret.error);

__func__ in a plain printk is not needed, the message already says what failed.

Quoted 1 line

+}

With that fixed, lgtm!

Signature · 1 line
Baptiste Le Duc <[email protected]>
#7 lore raw

PATCH v2 3/3 CI: Pass -no-reboot to QEMU in the RISC-V smoke test

In reply to Anandu Padinhare

Now that machine_restart() reboots on RISC-V, a Xen panic during the smoke test makes QEMU reset and boot Xen again, over and over, until console.exp gives up at its default 1500 second timeout. Before, a panic powered QEMU off and the job failed straight away.

Pass -no-reboot, as the Arm and x86 smoke tests do, so that QEMU exits when Xen resets and the job fails straight away again. A successful boot is not affected.

---
Changes in v2:
- New patch.

Baptiste's QTB series ("[PATCH v3 6/6] CI: drop the riscv64 expect-based smoke test") removes this script, and he will pass -no-reboot in the QTB riscv64 machine too. Until that series is in, this keeps the current job failing fast.

 automation/scripts/qemu-smoke-riscv64.sh | 1 +
 1 file changed, 1 insertion(+)
automation/scripts/qemu-smoke-riscv64.sh +1 -0
index c0b1082a08..dd7e8efa70 100755
@@ -10,6 +10,7 @@ export TEST_CMD="qemu-system-riscv64 \
     -cpu rv64,svpbmt=on \
     -smp 1 \
     -nographic \
+    -no-reboot \
     -m 2g \
     -kernel binaries/xen"
 
Signature · 1 line
2.53.0
Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] CI: Pass -no-reboot to QEMU in the RISC-V smoke test

Quoted 10 lines

Now that machine_restart() reboots on RISC-V, a Xen panic during the smoke test makes QEMU reset and boot Xen again, over and over, until console.exp gives up at its default 1500 second timeout. Before, a panic powered QEMU off and the job failed straight away.

Pass -no-reboot, as the Arm and x86 smoke tests do, so that QEMU exits when Xen resets and the job fails straight away again. A successful boot is not affected.

Quoted 8 lines

---
Changes in v2:
- New patch.

Baptiste's QTB series ("[PATCH v3 6/6] CI: drop the riscv64 expect-based smoke test") removes this script, and he will pass -no-reboot in the QTB riscv64 machine too. Until that series is in, this keeps the current job failing fast.

Don't hesitate to review the series if you have time too btw ;) It could really help us.

Signature · 1 line
Baptiste Le Duc <[email protected]>