From 4270857a76ccc5b840bf229ccb972e5d57b030b6 Mon Sep 17 00:00:00 2001 From: Tim Newsome Date: Thu, 6 Oct 2022 10:45:05 -0700 Subject: [PATCH] target/riscv: Clean up halt_go for multiple harts. Also add an early exit for if any harts are unavailable. Change-Id: I0875d4d213c9faf87b219d8d57e440881366c8f8 Signed-off-by: Tim Newsome --- src/target/riscv/riscv-013.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 9febdd9f8..19fe2ada8 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -4208,14 +4208,19 @@ static int riscv013_halt_go(struct target *target) dmcontrol |= DM_DMCONTROL_HASEL; dmcontrol = set_hartsel(dmcontrol, info->index); dmi_write(target, DM_DMCONTROL, dmcontrol); - for (size_t i = 0; i < 256; ++i) - if (riscv_is_halted(target)) - break; - - if (!riscv_is_halted(target)) { - uint32_t dmstatus; + uint32_t dmstatus; + for (size_t i = 0; i < 256; ++i) { if (dmstatus_read(target, &dmstatus, true) != ERROR_OK) return ERROR_FAIL; + /* When no harts are running, there's no point in continuing this loop. */ + if (!get_field(dmstatus, DM_DMSTATUS_ALLRUNNING)) + break; + } + + /* We declare success if no harts are running. One or more of them may be + * unavailable, though. */ + + if ((get_field(dmstatus, DM_DMSTATUS_ANYRUNNING))) { if (dmi_read(target, &dmcontrol, DM_DMCONTROL) != ERROR_OK) return ERROR_FAIL;