You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/10/18 11:35:06 UTC
(nuttx) 03/03: mpfs/mpfs_plic: Add procedure to initialize per hart PLIC state
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit cf9530593445245deb6126597923be1bc86b6738
Author: Ville Juven <vi...@unikie.com>
AuthorDate: Fri Oct 18 11:24:15 2024 +0300
mpfs/mpfs_plic: Add procedure to initialize per hart PLIC state
MPFS implements external interrupt control on a per-hart basis i.e. there
are PLIC control registers for each hart separately. This means we need
a procedure to initialize such registers for each hart individually,
instead of only for the boot hart like it is now.
Fix this by implementing mpfs_plic_init_hart which can be called by each
hart as needed.
Note: it is not a good idea to initialize all harts from the boot hart,
as the boot hart may not know which harts are used by NuttX in AMP
configuration. It is better that the hart initializes itself.
Note: The hartid must be provided as explicit parameter, as it cannot
be queried via riscv_mhartid() yet; the per-cpu structure is initialized
later on which means riscv_mhartid() would return 0 for all harts except
the boot hart.
---
arch/risc-v/src/mpfs/mpfs_irq.c | 35 +--------
arch/risc-v/src/mpfs/mpfs_plic.c | 147 ++++++++++++++++++++++++++++++++------
arch/risc-v/src/mpfs/mpfs_plic.h | 16 +++++
arch/risc-v/src/mpfs/mpfs_start.c | 10 +++
4 files changed, 153 insertions(+), 55 deletions(-)
diff --git a/arch/risc-v/src/mpfs/mpfs_irq.c b/arch/risc-v/src/mpfs/mpfs_irq.c
index ff5cec7f3f..b0b3e1bb4c 100644
--- a/arch/risc-v/src/mpfs/mpfs_irq.c
+++ b/arch/risc-v/src/mpfs/mpfs_irq.c
@@ -52,28 +52,9 @@ void up_irqinitialize(void)
up_irq_save();
- /* Complete possibly claimed IRQs in PLIC (for current hart) in case
- * of warm reboot, e.g. after a crash in the middle of IRQ handler.
- * This has no effect on non-claimed or disabled interrupts.
- */
+ /* Initialize PLIC for current hart */
- uintptr_t claim_address = mpfs_plic_get_claimbase();
-
- for (int irq = MPFS_IRQ_EXT_START; irq < NR_IRQS; irq++)
- {
- putreg32(irq - MPFS_IRQ_EXT_START, claim_address);
- }
-
- /* Disable all global interrupts for current hart */
-
- uintptr_t iebase = mpfs_plic_get_iebase();
-
- putreg32(0x0, iebase + 0);
- putreg32(0x0, iebase + 4);
- putreg32(0x0, iebase + 8);
- putreg32(0x0, iebase + 12);
- putreg32(0x0, iebase + 16);
- putreg32(0x0, iebase + 20);
+ mpfs_plic_init_hart(riscv_mhartid());
/* Colorize the interrupt stack for debug purposes */
@@ -82,18 +63,6 @@ void up_irqinitialize(void)
riscv_stack_color(g_intstackalloc, intstack_size);
#endif
- /* Set priority for all global interrupts to 1 (lowest) */
-
- for (int id = 1; id <= NR_IRQS; id++)
- {
- putreg32(1, (uintptr_t)(MPFS_PLIC_PRIORITY + (4 * id)));
- }
-
- /* Set irq threshold to 0 (permits all global interrupts) */
-
- uintptr_t threshold_address = mpfs_plic_get_thresholdbase();
- putreg32(0, threshold_address);
-
/* Attach the common interrupt handler */
riscv_exception_attach();
diff --git a/arch/risc-v/src/mpfs/mpfs_plic.c b/arch/risc-v/src/mpfs/mpfs_plic.c
index 3de194c2e1..8bd555d8e6 100644
--- a/arch/risc-v/src/mpfs/mpfs_plic.c
+++ b/arch/risc-v/src/mpfs/mpfs_plic.c
@@ -52,84 +52,90 @@
#endif
/****************************************************************************
- * Public Functions
+ * Private Functions
****************************************************************************/
/****************************************************************************
- * Name: mpfs_plic_get_iebase
+ * Name: get_iebase
*
* Description:
- * Context aware way to query PLIC interrupt enable base address
+ * Get base address for interrupt enable bits for a specific hart.
+ *
+ * Input Parameters:
+ * hartid - Hart ID to query.
*
* Returned Value:
- * Interrupt enable base address
+ * Interrupt enable base address.
*
****************************************************************************/
-uintptr_t mpfs_plic_get_iebase(void)
+static uintptr_t get_iebase(uintptr_t hartid)
{
uintptr_t iebase;
- uintptr_t hart_id = riscv_mhartid();
- if (hart_id == 0)
+ if (hartid == 0)
{
iebase = MPFS_PLIC_H0_MIE0;
}
else
{
iebase = MPFS_PLIC_H1_MIE0 + MPFS_PLIC_IEPRIV_OFFSET +
- (hart_id - 1) * MPFS_HART_MIE_OFFSET;
+ (hartid - 1) * MPFS_HART_MIE_OFFSET;
}
return iebase;
}
/****************************************************************************
- * Name: mpfs_plic_get_claimbase
+ * Name: get_claimbase
*
* Description:
- * Context aware way to query PLIC interrupt claim base address
+ * Get base address for interrupt claim for a specific hart.
+ *
+ * Input Parameters:
+ * hartid - Hart ID to query.
*
* Returned Value:
- * Interrupt enable claim address
+ * Interrupt enable claim address.
*
****************************************************************************/
-uintptr_t mpfs_plic_get_claimbase(void)
+uintptr_t get_claimbase(uintptr_t hartid)
{
uintptr_t claim_address;
- uintptr_t hart_id = riscv_mhartid();
- if (hart_id == 0)
+ if (hartid == 0)
{
claim_address = MPFS_PLIC_H0_MCLAIM;
}
else
{
claim_address = MPFS_PLIC_H1_MCLAIM + MPFS_PLIC_CLAIMPRIV_OFFSET +
- (hart_id - 1) * MPFS_PLIC_NEXTHART_OFFSET;
+ (hartid - 1) * MPFS_PLIC_NEXTHART_OFFSET;
}
return claim_address;
}
/****************************************************************************
- * Name: mpfs_plic_get_thresholdbase
+ * Name: get_thresholdbase
*
* Description:
- * Context aware way to query PLIC interrupt threshold base address
+ * Get base address for interrupt threshold for a specific hart.
+ *
+ * Input Parameters:
+ * hartid - Hart ID to query.
*
* Returned Value:
- * Interrupt enable threshold address
+ * Interrupt enable threshold address.
*
****************************************************************************/
-uintptr_t mpfs_plic_get_thresholdbase(void)
+uintptr_t get_thresholdbase(uintptr_t hartid)
{
uintptr_t threshold_address;
- uintptr_t hart_id = riscv_mhartid();
- if (hart_id == 0)
+ if (hartid == 0)
{
threshold_address = MPFS_PLIC_H0_MTHRESHOLD;
}
@@ -137,8 +143,105 @@ uintptr_t mpfs_plic_get_thresholdbase(void)
{
threshold_address = MPFS_PLIC_H1_MTHRESHOLD +
MPFS_PLIC_THRESHOLDPRIV_OFFSET +
- (hart_id - 1) * MPFS_PLIC_NEXTHART_OFFSET;
+ (hartid - 1) * MPFS_PLIC_NEXTHART_OFFSET;
}
return threshold_address;
}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mpfs_plic_init_hart
+ *
+ * Description:
+ * Initialize current hart's PLIC.
+ *
+ * Input Parameters:
+ * hartid - Hart ID to init.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+void mpfs_plic_init_hart(uintptr_t hartid)
+{
+ /* Disable all global interrupts for current hart */
+
+ uintptr_t iebase = get_iebase(hartid);
+
+ putreg32(0x0, iebase + 0);
+ putreg32(0x0, iebase + 4);
+ putreg32(0x0, iebase + 8);
+ putreg32(0x0, iebase + 12);
+ putreg32(0x0, iebase + 16);
+ putreg32(0x0, iebase + 20);
+
+ /* Complete possibly claimed IRQs in PLIC (for current hart) in case
+ * of warm reboot, e.g. after a crash in the middle of IRQ handler.
+ * This has no effect on non-claimed or disabled interrupts.
+ */
+
+ uintptr_t claim_address = get_claimbase(hartid);
+
+ for (int irq = MPFS_IRQ_EXT_START; irq < NR_IRQS; irq++)
+ {
+ putreg32(irq - MPFS_IRQ_EXT_START, claim_address);
+ }
+
+ /* Set irq threshold to 0 (permits all global interrupts) */
+
+ uintptr_t threshold_address = get_thresholdbase(hartid);
+ putreg32(0, threshold_address);
+}
+
+/****************************************************************************
+ * Name: mpfs_plic_get_iebase
+ *
+ * Description:
+ * Context aware way to query PLIC interrupt enable base address
+ *
+ * Returned Value:
+ * Interrupt enable base address
+ *
+ ****************************************************************************/
+
+uintptr_t mpfs_plic_get_iebase(void)
+{
+ return get_iebase(riscv_mhartid());
+}
+
+/****************************************************************************
+ * Name: mpfs_plic_get_claimbase
+ *
+ * Description:
+ * Context aware way to query PLIC interrupt claim base address
+ *
+ * Returned Value:
+ * Interrupt enable claim address
+ *
+ ****************************************************************************/
+
+uintptr_t mpfs_plic_get_claimbase(void)
+{
+ return get_claimbase(riscv_mhartid());
+}
+
+/****************************************************************************
+ * Name: mpfs_plic_get_thresholdbase
+ *
+ * Description:
+ * Context aware way to query PLIC interrupt threshold base address
+ *
+ * Returned Value:
+ * Interrupt enable threshold address
+ *
+ ****************************************************************************/
+
+uintptr_t mpfs_plic_get_thresholdbase(void)
+{
+ return get_thresholdbase(riscv_mhartid());
+}
diff --git a/arch/risc-v/src/mpfs/mpfs_plic.h b/arch/risc-v/src/mpfs/mpfs_plic.h
index 76d1d38294..b7a0910da8 100644
--- a/arch/risc-v/src/mpfs/mpfs_plic.h
+++ b/arch/risc-v/src/mpfs/mpfs_plic.h
@@ -31,6 +31,22 @@
* Public Function Prototypes
****************************************************************************/
+/****************************************************************************
+ * Name: mpfs_plic_init_hart
+ *
+ * Description:
+ * Initialize current hart's PLIC.
+ *
+ * Input Parameters:
+ * hartid - Hart ID to init.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+void mpfs_plic_init_hart(uintptr_t hartid);
+
/****************************************************************************
* Name: mpfs_plic_get_iebase
*
diff --git a/arch/risc-v/src/mpfs/mpfs_start.c b/arch/risc-v/src/mpfs/mpfs_start.c
index 3e18a4b96b..fb82c2af01 100644
--- a/arch/risc-v/src/mpfs/mpfs_start.c
+++ b/arch/risc-v/src/mpfs/mpfs_start.c
@@ -195,6 +195,16 @@ void __mpfs_start(uint64_t mhartid)
cpux:
#ifdef CONFIG_SMP
+ /* Disable local interrupts */
+
+ up_irq_save();
+
+ /* Initialize local PLIC */
+
+ mpfs_plic_init_hart(mhartid);
+
+ /* Then wait for the boot core to start us */
+
riscv_cpu_boot(riscv_hartid_to_cpuid(mhartid));
#endif