diff --git a/arch/risc-v/src/mpfs/mpfs_ihc.c b/arch/risc-v/src/mpfs/mpfs_ihc.c index 3310f6070153e..bc1b04a50a647 100644 --- a/arch/risc-v/src/mpfs/mpfs_ihc.c +++ b/arch/risc-v/src/mpfs/mpfs_ihc.c @@ -55,7 +55,7 @@ * Pre-processor Definitions ****************************************************************************/ -#ifdef CONFIG_MPFS_IHC_DEBUG +#ifdef CONFIG_DEBUG_ERROR # define ihcerr _err # define ihcwarn _warn # define ihcinfo _info @@ -337,9 +337,6 @@ static uint32_t mpfs_ihc_context_to_remote_hart_id(ihc_channel_t channel) } else { - DEBUGASSERT(LIBERO_SETTING_CONTEXT_A_HART_EN > 0); - DEBUGASSERT(LIBERO_SETTING_CONTEXT_B_HART_EN > 0); - /* Determine context we are in */ if (channel == IHC_CHANNEL_TO_CONTEXTA) @@ -352,7 +349,7 @@ static uint32_t mpfs_ihc_context_to_remote_hart_id(ihc_channel_t channel) } else { - DEBUGPANIC(); + PANIC(); } hart_idx = 0; @@ -369,8 +366,6 @@ static uint32_t mpfs_ihc_context_to_remote_hart_id(ihc_channel_t channel) } } - DEBUGASSERT(hart != UNDEFINED_HART_ID); - return hart; } @@ -431,7 +426,11 @@ static uint32_t mpfs_ihc_context_to_local_hart_id(ihc_channel_t channel) } } - DEBUGASSERT(hart < MPFS_NUM_HARTS); + if (hart >= MPFS_NUM_HARTS) + { + ihcerr("Could not find the originating hart!\n"); + PANIC(); + } return hart; } @@ -505,7 +504,11 @@ static void mpfs_ihc_worker(void *arg) } while ((ctrl_reg & (RMP_MESSAGE_PRESENT)) && --retries); - DEBUGASSERT(retries != 0); + if (retries == 0) + { + ihcerr("Could not send the message!\n"); + PANIC(); + } modifyreg32(MPFS_IHC_CTRL(g_work_arg.mhartid, g_work_arg.rhartid), 0, ACK_INT); @@ -536,19 +539,25 @@ static void mpfs_ihc_rx_message(ihc_channel_t channel, uint32_t mhartid, uint32_t rhartid = mpfs_ihc_context_to_remote_hart_id(channel); uint32_t ctrl_reg = getreg32(MPFS_IHC_CTRL(mhartid, rhartid)); + if (rhartid == UNDEFINED_HART_ID) + { + ihcerr("Remote hart not identified!\n"); + return; + } + /* Check if we have a message */ if (mhartid == CONTEXTB_HARTID) { uintptr_t msg_in = MPFS_IHC_MSG_IN(mhartid, rhartid); - DEBUGASSERT(msg == NULL); mpfs_ihc_rx_handler((uint32_t *)msg_in); } else { /* This path is meant for the OpenSBI vendor extension only */ - DEBUGPANIC(); + ihcerr("Wrong recipient!\n"); + PANIC(); } /* Set MP to 0. Note this generates an interrupt on the other hart @@ -691,7 +700,11 @@ static void mpfs_ihc_local_context_init(uint32_t hart_to_configure) { uint32_t rhartid = 0; - DEBUGASSERT(hart_to_configure < MPFS_NUM_HARTS); + if (hart_to_configure >= MPFS_NUM_HARTS) + { + ihcerr("Configuring too many harts!\n"); + PANIC(); + } while (rhartid < MPFS_NUM_HARTS) { @@ -758,7 +771,18 @@ static int mpfs_ihc_tx_message(ihc_channel_t channel, uint32_t *message) uint32_t ctrl_reg; uint32_t retries = 10000; - DEBUGASSERT(message_size <= IHC_MAX_MESSAGE_SIZE); + if (rhartid == UNDEFINED_HART_ID) + { + /* Something went wrong */ + + ihcerr("Remote hart not found!\n"); + PANIC(); + } + else if (message_size > IHC_MAX_MESSAGE_SIZE) + { + ihcerr("Sent message too large!\n"); + PANIC(); + } /* Check if the system is busy. All we can try is wait. */ @@ -885,8 +909,6 @@ mpfs_rptun_get_resource(struct rptun_dev_s *dev) /* Only slave supported so far */ - DEBUGASSERT(!priv->master); - if (priv->shmem != NULL) { return &priv->shmem->rsc; @@ -942,6 +964,12 @@ mpfs_rptun_get_resource(struct rptun_dev_s *dev) g_rptun_initialized = true; + /* Don't enable this too early; if the master is already up, irqs will + * likely hang the system as no ACKs may be sent yet. + */ + + up_enable_irq(g_plic_irq); + return &priv->shmem->rsc; } @@ -1063,7 +1091,10 @@ static int mpfs_rptun_notify(struct rptun_dev_s *dev, uint32_t notifyid) } while ((ret != OK) && --retries); - DEBUGASSERT(ret == OK); + if (retries == 0) + { + return -EIO; + } } return ret; @@ -1256,7 +1287,11 @@ static void mpfs_rptun_worker(void *arg) return; } - DEBUGASSERT((g_vq_idx - VRING0_NOTIFYID) < VRINGS); + if (g_vq_idx >= VRINGS) + { + return; + } + info = &g_mpfs_virtqueue_table[g_vq_idx - VRING0_NOTIFYID]; virtqueue_notification((struct virtqueue *)info->data); } @@ -1293,7 +1328,11 @@ static int mpfs_rptun_thread(int argc, char *argv[]) return 0; } - DEBUGASSERT((g_vq_idx - VRING0_NOTIFYID) < VRINGS); + if (g_vq_idx >= VRINGS) + { + return; + } + info = &g_mpfs_virtqueue_table[g_vq_idx - VRING0_NOTIFYID]; virtqueue_notification((struct virtqueue *)info->data); @@ -1363,11 +1402,7 @@ int mpfs_ihc_init(void) /* Attach and enable the applicable irq */ ret = irq_attach(g_plic_irq, mpfs_ihc_interrupt, NULL); - if (ret == OK) - { - up_enable_irq(g_plic_irq); - } - else + if (ret != OK) { ihcerr("ERROR: Not able to attach irq\n"); return ret; diff --git a/arch/risc-v/src/mpfs/mpfs_ihc_sbi.c b/arch/risc-v/src/mpfs/mpfs_ihc_sbi.c index aa77f022fd2b3..a59e7255ccc92 100644 --- a/arch/risc-v/src/mpfs/mpfs_ihc_sbi.c +++ b/arch/risc-v/src/mpfs/mpfs_ihc_sbi.c @@ -218,7 +218,10 @@ static uint32_t mpfs_ihc_sbi_context_to_remote_hart_id(ihc_channel_t channel) hart_idx++; } - DEBUGASSERT(hart != UNDEFINED_HART_ID); + if (hart == UNDEFINED_HART_ID) + { + PANIC(); + } return hart; } @@ -244,7 +247,10 @@ static uint32_t mpfs_ihc_sbi_context_to_local_hart_id(ihc_channel_t channel) uint32_t harts_in_context = LIBERO_SETTING_CONTEXT_A_HART_EN; uint32_t hart_next = 0; - DEBUGASSERT(channel > IHC_CHANNEL_TO_HART4); + if (channel <= IHC_CHANNEL_TO_HART4) + { + PANIC(); + } hart_idx = 0; while (hart_idx < MPFS_NUM_HARTS) @@ -271,7 +277,11 @@ static uint32_t mpfs_ihc_sbi_context_to_local_hart_id(ihc_channel_t channel) hart_idx++; } - DEBUGASSERT(hart < MPFS_NUM_HARTS); + if (hart >= MPFS_NUM_HARTS) + { + PANIC(); + } + return hart; } @@ -303,8 +313,7 @@ static void mpfs_ihc_sbi_message_present_handler(uint32_t *message, bool is_mp) { struct ihc_sbi_rx_msg_s *msg; - uintptr_t message_ihc = (uintptr_t)MPFS_IHC_MSG_IN(mhartid, rhartid); - uint32_t message_size_ihc = getreg32(MPFS_IHC_MSG_SIZE(mhartid, rhartid)); + uintptr_t message_ihc = (uintptr_t)MPFS_IHC_MSG_IN(mhartid, rhartid); msg = (struct ihc_sbi_rx_msg_s *)message; @@ -324,8 +333,6 @@ static void mpfs_ihc_sbi_message_present_handler(uint32_t *message, msg->irq_type = ACK_IRQ | MP_IRQ; msg->ihc_msg = *(struct mpfs_ihc_msg_s *)message_ihc; } - - DEBUGASSERT(sizeof(msg->ihc_msg) >= message_size_ihc); } /**************************************************************************** @@ -350,17 +357,23 @@ static void mpfs_ihc_sbi_message_present_handler(uint32_t *message, static void mpfs_ihc_sbi_rx_message(uint32_t rhartid, uint32_t mhartid, bool is_ack, bool is_mp, uint32_t *msg) { + /* Msg must be always present */ + + if (msg == NULL) + { + PANIC(); + } + if (is_ack && !is_mp) { if (mhartid == CONTEXTB_HARTID) { - DEBUGPANIC(); + PANIC(); } else { /* This path is meant for the OpenSBI vendor extension only */ - DEBUGASSERT(msg != NULL); mpfs_ihc_sbi_message_present_handler(msg, mhartid, rhartid, is_ack, is_mp); @@ -375,13 +388,12 @@ static void mpfs_ihc_sbi_rx_message(uint32_t rhartid, uint32_t mhartid, if (mhartid == CONTEXTB_HARTID) { - DEBUGPANIC(); + PANIC(); } else { /* This path is meant for the OpenSBI vendor extension only */ - DEBUGASSERT(msg != NULL); mpfs_ihc_sbi_message_present_handler(msg, mhartid, rhartid, is_ack, is_mp); } @@ -394,7 +406,6 @@ static void mpfs_ihc_sbi_rx_message(uint32_t rhartid, uint32_t mhartid, } else if (is_ack && is_mp) { - DEBUGASSERT(msg != NULL); mpfs_ihc_sbi_message_present_handler(msg, mhartid, rhartid, is_ack, is_mp); @@ -460,7 +471,10 @@ static void mpfs_ihc_sbi_local_context_init(uint32_t hart_to_configure) { uint32_t rhartid = 0; - DEBUGASSERT(hart_to_configure < MPFS_NUM_HARTS); + if (hart_to_configure >= MPFS_NUM_HARTS) + { + PANIC(); + } while (rhartid < MPFS_NUM_HARTS) { @@ -551,7 +565,10 @@ static int mpfs_ihc_sbi_tx_message(ihc_channel_t channel, uint32_t *message) uint32_t ctrl_reg; uint32_t retries = 10000; - DEBUGASSERT(message_size <= IHC_MAX_MESSAGE_SIZE); + if (message_size > IHC_MAX_MESSAGE_SIZE) + { + return -EINVAL; + } /* Check if the system is busy. All we can try is wait. */