Skip to contentSkip to conversation

lib: sbi: Introduce sbi_entry_count() function

PATCH New Anup Patel 2 participants 4 messages Mar 20, 2023 – Apr 6, 2023

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/2 lib: sbi: Introduce sbi_entry_count() function

We introduce sbi_entry_count() function which counts the number of times a HART enters OpenSBI via cold-boot or warm-boot path.

---
 include/sbi/sbi_init.h |  2 ++
 lib/sbi/sbi_init.c     | 42 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 37 insertions(+), 7 deletions(-)
include/sbi/sbi_init.h +2 -0
index 74eb1c0..9640fee 100644
@@ -16,6 +16,8 @@ struct sbi_scratch;
 
 void __noreturn sbi_init(struct sbi_scratch *scratch);
 
+unsigned long sbi_entry_count(u32 hartid);
+
 unsigned long sbi_init_count(u32 hartid);
 
 void __noreturn sbi_exit(struct sbi_scratch *scratch);
lib/sbi/sbi_init.c +35 -7

91 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 lib: sbi_hsm: Fix sbi_hsm_hart_start() for platform with hart hotplug

It possible that a platform supports hart hotplug (i.e. both hart_start and hart_stop callbacks available) and all harts are start simultaneously at platform boot-time. In this situation, the sbi_hsm_hart_start() will call hsm_device_hart_start() for secondary harts at platform boot-time which will fail because secondary harts were already started.

To fix above, we call hsm_device_hart_start() from sbi_hsm_hart_start() only when entry_count is same as init_count for the secondary hart.

---
 lib/sbi/sbi_hsm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_hsm.c +4 -2
index 16e3846..b261410 100644
@@ -301,7 +301,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
 		       const struct sbi_domain *dom,
 		       u32 hartid, ulong saddr, ulong smode, ulong arg1)
 {
-	unsigned long init_count;
+	unsigned long init_count, entry_count;
 	unsigned int hstate;
 	struct sbi_scratch *rscratch;
 	struct sbi_hsm_data *hdata;
@@ -325,6 +325,8 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
 		return SBI_EINVAL;
 
 	init_count = sbi_init_count(hartid);
+	entry_count = sbi_entry_count(hartid);
+
 	rscratch->next_arg1 = arg1;
 	rscratch->next_addr = saddr;
 	rscratch->next_mode = smode;
@@ -350,7 +352,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
 		goto err;
 	}
 
-	if (hsm_device_has_hart_hotplug() ||
+	if ((hsm_device_has_hart_hotplug() && (entry_count == init_count)) ||
 	   (hsm_device_has_hart_secondary_boot() && !init_count)) {
 		rc = hsm_device_hart_start(hartid, scratch->warmboot_addr);
 	} else {
Signature · 1 line
2.34.1
Applied
#2 lore raw

PATCH 2/2 lib: sbi_hsm: Fix sbi_hsm_hart_start() for platform with hart hotplug

Quoted 11 lines · On Mon, Mar 20, 2023 at 9:29?PM Anup Patel <[email protected]> wrote:

It possible that a platform supports hart hotplug (i.e. both hart_start and hart_stop callbacks available) and all harts are start simultaneously at platform boot-time. In this situation, the sbi_hsm_hart_start() will call hsm_device_hart_start() for secondary harts at platform boot-time which will fail because secondary harts were already started.

To fix above, we call hsm_device_hart_start() from sbi_hsm_hart_start() only when entry_count is same as init_count for the secondary hart.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 38 lines
---
 lib/sbi/sbi_hsm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_hsm.c +4 -3
index 16e3846..b261410 100644
@@ -301,7 +301,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
                       const struct sbi_domain *dom,
                       u32 hartid, ulong saddr, ulong smode, ulong arg1)
 {
-       unsigned long init_count;
+       unsigned long init_count, entry_count;
        unsigned int hstate;
        struct sbi_scratch *rscratch;
        struct sbi_hsm_data *hdata;
@@ -325,6 +325,8 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
                return SBI_EINVAL;
 
        init_count = sbi_init_count(hartid);
+       entry_count = sbi_entry_count(hartid);
+
        rscratch->next_arg1 = arg1;
        rscratch->next_addr = saddr;
        rscratch->next_mode = smode;
@@ -350,7 +352,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
                goto err;
        }
 
-       if (hsm_device_has_hart_hotplug() ||
+       if ((hsm_device_has_hart_hotplug() && (entry_count == init_count)) ||
           (hsm_device_has_hart_secondary_boot() && !init_count)) {
                rc = hsm_device_hart_start(hartid, scratch->warmboot_addr);
        } else {
--

2.34.1

Applied
#3 lore raw

PATCH 1/2 lib: sbi: Introduce sbi_entry_count() function

In reply to Anup Patel
Quoted 5 lines · On Mon, Mar 20, 2023 at 9:29?PM Anup Patel <[email protected]> wrote:

We introduce sbi_entry_count() function which counts the number of times a HART enters OpenSBI via cold-boot or warm-boot path.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 122 lines
---
 include/sbi/sbi_init.h |  2 ++
 lib/sbi/sbi_init.c     | 42 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 37 insertions(+), 7 deletions(-)
include/sbi/sbi_init.h +2 -0
index 74eb1c0..9640fee 100644
@@ -16,6 +16,8 @@ struct sbi_scratch;
 
 void __noreturn sbi_init(struct sbi_scratch *scratch);
 
+unsigned long sbi_entry_count(u32 hartid);
+
 unsigned long sbi_init_count(u32 hartid);
 
 void __noreturn sbi_exit(struct sbi_scratch *scratch);
lib/sbi/sbi_init.c +35 -8
index ffa214c..f184248 100644
@@ -238,12 +238,13 @@ static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
        spin_unlock(&coldboot_lock);
 }
 
+static unsigned long entry_count_offset;
 static unsigned long init_count_offset;
 
 static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 {
        int rc;
-       unsigned long *init_count;
+       unsigned long *count;
        const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 
        /* Note: This has to be first thing in coldboot init sequence */
@@ -256,10 +257,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
        if (rc)
                sbi_hart_hang();
 
+       entry_count_offset = sbi_scratch_alloc_offset(__SIZEOF_POINTER__);
+       if (!entry_count_offset)
+               sbi_hart_hang();
+
        init_count_offset = sbi_scratch_alloc_offset(__SIZEOF_POINTER__);
        if (!init_count_offset)
                sbi_hart_hang();
 
+       count = sbi_scratch_offset_ptr(scratch, entry_count_offset);
+       (*count)++;
+
        rc = sbi_hsm_init(scratch, hartid, true);
        if (rc)
                sbi_hart_hang();
@@ -352,8 +360,8 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
 
        wake_coldboot_harts(scratch, hartid);
 
-       init_count = sbi_scratch_offset_ptr(scratch, init_count_offset);
-       (*init_count)++;
+       count = sbi_scratch_offset_ptr(scratch, init_count_offset);
+       (*count)++;
 
        sbi_hsm_hart_start_finish(scratch, hartid);
 }
@@ -362,12 +370,15 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
                                         u32 hartid)
 {
        int rc;
-       unsigned long *init_count;
+       unsigned long *count;
        const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 
-       if (!init_count_offset)
+       if (!entry_count_offset || !init_count_offset)
                sbi_hart_hang();
 
+       count = sbi_scratch_offset_ptr(scratch, entry_count_offset);
+       (*count)++;
+
        rc = sbi_hsm_init(scratch, hartid, false);
        if (rc)
                sbi_hart_hang();
@@ -408,8 +419,8 @@ static void __noreturn init_warm_startup(struct sbi_scratch *scratch,
        if (rc)
                sbi_hart_hang();
 
-       init_count = sbi_scratch_offset_ptr(scratch, init_count_offset);
-       (*init_count)++;
+       count = sbi_scratch_offset_ptr(scratch, init_count_offset);
+       (*count)++;
 
        sbi_hsm_hart_start_finish(scratch, hartid);
 }
@@ -521,6 +532,23 @@ void __noreturn sbi_init(struct sbi_scratch *scratch)
                init_warmboot(scratch, hartid);
 }
 
+unsigned long sbi_entry_count(u32 hartid)
+{
+       struct sbi_scratch *scratch;
+       unsigned long *entry_count;
+
+       if (!entry_count_offset)
+               return 0;
+
+       scratch = sbi_hartid_to_scratch(hartid);
+       if (!scratch)
+               return 0;
+
+       entry_count = sbi_scratch_offset_ptr(scratch, entry_count_offset);
+
+       return *entry_count;
+}
+
 unsigned long sbi_init_count(u32 hartid)
 {
        struct sbi_scratch *scratch;
--

2.34.1