Skip to contentSkip to conversation

lib: sbi: Refactor sbi_ipi_raw_send/clear interface

PATCH In review Bo Gan 2 participants 2 messages Sep 13 – Sep 14 SiFive drivers

lore
Share & tools
lore

Bo Gan

author
#0 lore raw

PATCH 1/1 lib: sbi: Refactor sbi_ipi_raw_send/clear interface

Commit 94f0f8465622 ("lib: sbi: Extends sbi_ipi_raw_send() to use all available IPI devices") changed the sbi_ipi_raw_send interface to allow sending IPI via all registered IPI devices. The intention was to address specific use cases on some Sifive platforms, where the default IMSIC IPI device is not usable for hart wakeup in HSM start function. Sending IPI via all devices does seem to have "fixed" the problem, as it's utilizing the CLINT as well.

However this approach is problematic:

a. If a platform has > 1 IPI devices, and all are functional for HSM
wakups, then multiple IPIs would be triggered for the waking hart.
b. These IPIs could arrive at the target hart at any time, and later
ones can even arrive after the hart's fully woken up, initialized everything, and returned to the next privilege mode. a call to sbi_ipi_raw_clear(true) is thus not enough to clear all pending IPIs, because others are still in-flight.

It's exactly what we are observing on SpacemiT's K3 platform. (both IMSIC and ACLINT are available).

To fix the issue without making it too complicated, first, establish a general rule that there can be only 1 active IPI device for a IPI user (e.g., HSM) at any given time. If the user of IPI needs sending IPIs via the non-default device, specify it explicitly, instead of invoking all devices, and hoping some of them work. Specifically:

1. Each IPI device is assigned a type tag.
2. No more sending IPI via all devices. Either use sbi_ipi_raw_send for the default IPI device, or sbi_ipi_device_raw_send for the specific type of device
3. sbi_ipi_raw_clear(true) is now sbi_ipi_raw_clear_all()

sbi_ipi_raw_send(hartindex) pairs with sbi_ipi_raw_clear() sbi_ipi_device_raw_send(...) pairs with sbi_ipi_raw_clear_all() There's currently no need for a sbi_ipi_device_raw_clear, because sbi_ipi_device_raw_send is intended for special use, such as HSM. During hart wakeup, it's safe to clear from all IPI devices, as the hart HSM state hasn't changed to STARTED yet, so no need to worry about losing IPIs triggered from other users (TLB, SSE...).

Fixes 94f0f8465622 ("lib: sbi: Extends sbi_ipi_raw_send() to use all available IPI devices")
---
 include/sbi/sbi_ipi.h                | 17 +++++-
 lib/sbi/sbi_hsm.c                    |  2 +-
 lib/sbi/sbi_init.c                   |  2 +-
 lib/sbi/sbi_ipi.c                    | 77 +++++++++++++++++-----------
 lib/utils/hsm/fdt_hsm_andes_atcsmu.c |  2 +-
 lib/utils/hsm/fdt_hsm_sifive_tmc0.c  |  5 +-
 lib/utils/ipi/aclint_mswi.c          |  1 +
 lib/utils/ipi/andes_plicsw.c         |  1 +
 lib/utils/irqchip/imsic.c            |  1 +
 platform/generic/eswin/eic770x.c     |  4 +-
 10 files changed, 72 insertions(+), 40 deletions(-)
include/sbi/sbi_ipi.h +15 -2
index 2c231041..7fc88aff 100644
@@ -18,11 +18,20 @@
 
 /* clang-format on */
 
+enum sbi_ipi_device_type {
+	SBI_IPI_DEVICE_ACLINT,
+	SBI_IPI_DEVICE_IMSIC,
+	SBI_IPI_DEVICE_ANDES_PLICSW,
+};
+
 /** IPI hardware device */
 struct sbi_ipi_device {
 	/** Name of the IPI device */
 	char name[32];
 
+	/** Type of the IPI device */
+	enum sbi_ipi_device_type type;
+
 	/** Ratings of the IPI device (higher is better) */
 	unsigned long rating;
 
@@ -88,9 +97,13 @@ int sbi_ipi_send_halt(ulong hmask, ulong hbase);
 
 void sbi_ipi_process(void);
 
-int sbi_ipi_raw_send(u32 hartindex, bool all_devices);
+int sbi_ipi_raw_send(u32 hartindex);
+
+int sbi_ipi_device_raw_send(enum sbi_ipi_device_type type, u32 hartindex);
+
+void sbi_ipi_raw_clear(void);
 
-void sbi_ipi_raw_clear(bool all_devices);
+void sbi_ipi_raw_clear_all(void);
 
 const struct sbi_ipi_device *sbi_ipi_get_device(void);
 
lib/sbi/sbi_hsm.c +1 -1
index 0a355f9c..9b87e632 100644
@@ -364,7 +364,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
 	   (hsm_device_has_hart_secondary_boot() && !init_count)) {
 		rc = hsm_device_hart_start(hartid, scratch->warmboot_addr);
 	} else {
-		rc = sbi_ipi_raw_send(hartindex, true);
+		rc = sbi_ipi_raw_send(hartindex);
 	}
 
 	if (!rc)
lib/sbi/sbi_init.c +1 -1
index acd2f8b6..2af69d2f 100644
@@ -557,7 +557,7 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)
 	if (hstate == SBI_HSM_STATE_SUSPENDED) {
 		init_warm_resume(scratch, hartid);
 	} else {
-		sbi_ipi_raw_clear(true);
+		sbi_ipi_raw_clear_all();
 		init_warm_startup(scratch, hartid);
 	}
 }
lib/sbi/sbi_ipi.c +47 -30

127 lines not on this page · raw message

lib/utils/hsm/fdt_hsm_andes_atcsmu.c +1 -1
index 115916d5..5a148c3f 100644
@@ -126,7 +126,7 @@ static int ae350_hart_start(u32 hartid, ulong saddr)
 	 */
 	if (!sbi_init_count(hartindex) || (is_andes(25) && hartid == 0) ||
 	    sleep_type == SBI_SUSP_AE350_LIGHT_SLEEP)
-		return sbi_ipi_raw_send(hartindex, false);
+		return sbi_ipi_raw_send(hartindex);
 
 	atcsmu_set_command(WAKEUP_CMD, hartid);
 	return 0;
lib/utils/hsm/fdt_hsm_sifive_tmc0.c +2 -3
index 2690a638..d5534c16 100644
@@ -259,9 +259,8 @@ static int sifive_tmc0_start(u32 hartid, ulong saddr)
 	 * In system suspend, the IMSIC will be reset in SiFive platform so
 	 * we use the CLINT IPI as the wake event.
 	 */
-	sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid), true);
-
-	return SBI_OK;
+	return sbi_ipi_device_raw_send(SBI_IPI_DEVICE_ACLINT,
+				       sbi_hartid_to_hartindex(hartid));
 }
 
 static int sifive_tmc0_stop(void)
lib/utils/ipi/aclint_mswi.c +1 -0
index d4acacf0..19683cea 100644
@@ -62,6 +62,7 @@ static void mswi_ipi_clear(void)
 
 static struct sbi_ipi_device aclint_mswi = {
 	.name = "aclint-mswi",
+	.type = SBI_IPI_DEVICE_ACLINT,
 	.rating = 100,
 	.ipi_send = mswi_ipi_send,
 	.ipi_clear = mswi_ipi_clear
lib/utils/ipi/andes_plicsw.c +1 -0
index 3621e3cb..85b53826 100644
@@ -61,6 +61,7 @@ static void plicsw_ipi_clear(void)
 
 static struct sbi_ipi_device plicsw_ipi = {
 	.name      = "andes_plicsw",
+	.type      = SBI_IPI_DEVICE_ANDES_PLICSW,
 	.rating    = 200,
 	.ipi_send  = plicsw_ipi_send,
 	.ipi_clear = plicsw_ipi_clear
lib/utils/irqchip/imsic.c +1 -0
index f6da9f7e..a13fc4c6 100644
@@ -221,6 +221,7 @@ static void imsic_ipi_send(u32 hart_index)
 
 static struct sbi_ipi_device imsic_ipi_device = {
 	.name		= "aia-imsic",
+	.type		= SBI_IPI_DEVICE_IMSIC,
 	.rating		= 300,
 	.ipi_send	= imsic_ipi_send
 };
platform/generic/eswin/eic770x.c +2 -2
index c71198a8..6cc5c61b 100644
@@ -32,7 +32,7 @@ static int eic770x_hart_start(u32 hartid, ulong saddr)
 	 * returns, putting the hart in atomic_read(&hdata->state)
 	 * loop in sbi_hsm_hart_wait. We wake it up if it's in wfi()
 	 */
-	return sbi_ipi_raw_send(hartindex, true);
+	return sbi_ipi_raw_send(hartindex);
 }
 
 static int eic770x_hart_stop()
@@ -86,7 +86,7 @@ void eic770x_cease_other_harts(void)
 		 *  3. Given the fence o, r, any previous ipi_clear
 		 *     can't fall-through the read of eic770x_power_down
 		 */
-		sbi_ipi_raw_send(i, false);
+		sbi_ipi_raw_send(i);
 		to_cease[die] |= EIC770X_MC_CEASE_BIT(core);
 	}
 
Signature · 7 lines
2.34.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw
Quoted 28 lines · Bo Gan

Commit 94f0f8465622 ("lib: sbi: Extends sbi_ipi_raw_send() to use all available IPI devices") changed the sbi_ipi_raw_send interface to allow sending IPI via all registered IPI devices. The intention was to address specific use cases on some Sifive platforms, where the default IMSIC IPI device is not usable for hart wakeup in HSM start function. Sending IPI via all devices does seem to have "fixed" the problem, as it's utilizing the CLINT as well.

However this approach is problematic:

a. If a platform has > 1 IPI devices, and all are functional for HSM
wakups, then multiple IPIs would be triggered for the waking hart.
b. These IPIs could arrive at the target hart at any time, and later
ones can even arrive after the hart's fully woken up, initialized everything, and returned to the next privilege mode. a call to sbi_ipi_raw_clear(true) is thus not enough to clear all pending IPIs, because others are still in-flight.

It's exactly what we are observing on SpacemiT's K3 platform. (both IMSIC and ACLINT are available).

To fix the issue without making it too complicated, first, establish a general rule that there can be only 1 active IPI device for a IPI user (e.g., HSM) at any given time. If the user of IPI needs sending IPIs via the non-default device, specify it explicitly, instead of invoking all devices, and hoping some of them work. Specifically:

Quoted 5 lines

1. Each IPI device is assigned a type tag.
2. No more sending IPI via all devices. Either use sbi_ipi_raw_send for the default IPI device, or sbi_ipi_device_raw_send for the specific type of device
3. sbi_ipi_raw_clear(true) is now sbi_ipi_raw_clear_all()

Clearly, this patch has multiple changes which can be broken down into separate patches.

Create a series with smaller patches which preserve git bisectability.

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi