Skip to content

Commit 5caa519

Browse files
boa-zRbb666
authored andcommitted
[libcpu][risc-v] Fix PLIC completion order in non-DM mode
handler_plic_irq() completes a claimed interrupt before dispatching its handler. If a level-triggered source is still asserted at completion time, the PLIC gateway forwards the request again, so the same interrupt can be dispatched twice. Run the handler first, then complete. Completing after the handler means a handler may have masked its own source; the PLIC ignores a completion for an interrupt that is not enabled for the target, so temporarily enable the source around the completion, as the Linux SiFive PLIC driver does. Link: #12010
1 parent f50c9bf commit 5caa519

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎libcpu/risc-v/common/interrupt.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,12 @@ static rt_bool_t handler_irq(int hwirq, void *data)
430430
#if INTC_TYPE != INTC_TYPE_CLIC
431431
static rt_bool_t handler_plic_irq(int hwirq, void *data)
432432
{
433+
rt_bool_t res = handler_irq(hwirq, data);
434+
435+
/* Complete after handling to avoid re-pending level interrupts. */
433436
plic_irq_ack_or_eoi(rt_hw_cpu_id(), hwirq);
434437

435-
return handler_irq(hwirq, data);
438+
return res;
436439
}
437440

438441
void plic_init(void)

‎libcpu/risc-v/common/pic/pic-sifive-plic.c‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,41 @@ static void plic_irq_mask_raw(int cpu, int hwirq)
170170
static void plic_irq_ack_or_eoi(int cpu, int hwirq)
171171
{
172172
struct plic_percpu *percpu = &plic_percpu[cpu];
173+
rt_uint32_t *enable_reg, enable_mask;
173174

174-
if (percpu->valid)
175+
if (!percpu->valid)
176+
{
177+
return;
178+
}
179+
180+
enable_reg = percpu->enable_base + (hwirq / 32) * sizeof(rt_uint32_t);
181+
enable_mask = 1U << (hwirq % 32);
182+
183+
/*
184+
* The PLIC ignores completion for a disabled source.
185+
* Temporarily enable it if the handler masked the interrupt.
186+
*/
187+
if (HWREG32(enable_reg) & enable_mask)
175188
{
176189
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
177190
}
191+
else
192+
{
193+
rt_ubase_t level = rt_spin_lock_irqsave(&percpu->enable_lock);
194+
195+
if (HWREG32(enable_reg) & enable_mask)
196+
{
197+
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
198+
}
199+
else
200+
{
201+
HWREG32(enable_reg) |= enable_mask;
202+
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
203+
HWREG32(enable_reg) &= ~enable_mask;
204+
}
205+
206+
rt_spin_unlock_irqrestore(&percpu->enable_lock, level);
207+
}
178208
}
179209

180210
static rt_bool_t plic_handler_isr_raw(struct plic *plic,

0 commit comments

Comments
 (0)