Skip to content

Commit 9afba5f

Browse files
committed
[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 95e82ec commit 9afba5f

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,16 @@ 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+
/*
436+
* Complete the interrupt only after its handler has run. Completing
437+
* a still-asserted level source earlier lets the gateway forward it
438+
* again, so the same interrupt can be dispatched twice.
439+
*/
433440
plic_irq_ack_or_eoi(rt_hw_cpu_id(), hwirq);
434441

435-
return handler_irq(hwirq, data);
442+
return res;
436443
}
437444

438445
void plic_init(void)

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,43 @@ 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 = 1 << (hwirq % 32);
182+
183+
/*
184+
* The PLIC ignores a completion for an interrupt that is not enabled
185+
* for the target. A handler may mask its own interrupt before it is
186+
* completed, so re-enable it around the completion, as the Linux
187+
* SiFive PLIC driver does.
188+
*/
189+
if (HWREG32(enable_reg) & enable_mask)
175190
{
176191
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
177192
}
193+
else
194+
{
195+
rt_ubase_t level = rt_spin_lock_irqsave(&percpu->enable_lock);
196+
197+
if (HWREG32(enable_reg) & enable_mask)
198+
{
199+
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
200+
}
201+
else
202+
{
203+
HWREG32(enable_reg) |= enable_mask;
204+
HWREG32(percpu->hart_base + CONTEXT_CLAIM) = hwirq;
205+
HWREG32(enable_reg) &= ~enable_mask;
206+
}
207+
208+
rt_spin_unlock_irqrestore(&percpu->enable_lock, level);
209+
}
178210
}
179211

180212
static rt_bool_t plic_handler_isr_raw(struct plic *plic,

0 commit comments

Comments
 (0)