Skip to content

Commit 539d147

Browse files
Charles MirabileKAGA-KOKO
authored andcommitted
irqchip/sifive-plic: Add support for UltraRISC DP1000 PLIC
Add a new compatible for the plic found in UltraRISC DP1000 with a quirk to work around a known hardware bug with IRQ claiming in the UR-CP100 cores. When claiming an interrupt on UR-CP100 cores, all other interrupts must be disabled before the claim register is accessed to prevent incorrect handling of the interrupt. This is a hardware bug in the CP100 core implementation, not specific to the DP1000 SoC. When the PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM flag is present, a specialized handler (plic_handle_irq_cp100) disables all interrupts except for the first pending one before reading the claim register, and then restores the interrupts before further processing of the claimed interrupt continues. This implementation leverages the enable_save optimization, which maintains the current interrupt enable state in memory, avoiding additional register reads during the workaround. The driver matches on "ultrarisc,cp100-plic" to apply the quirk to all SoCs using UR-CP100 cores, regardless of the specific SoC implementation. This has no impact on other platforms. [ tglx: Condensed the code a bit, massaged change log and comments ] Co-developed-by: Zhang Xincheng <zhangxincheng@ultrarisc.com> Signed-off-by: Zhang Xincheng <zhangxincheng@ultrarisc.com> Signed-off-by: Charles Mirabile <cmirabil@redhat.com> Signed-off-by: Lucas Zampieri <lzampier@redhat.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Samuel Holland <samuel.holland@sifive.com> Link: https://patch.msgid.link/20251024083647.475239-5-lzampier@redhat.com
1 parent 14ff9e5 commit 539d147

1 file changed

Lines changed: 103 additions & 1 deletion

File tree

drivers/irqchip/irq-sifive-plic.c

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#define CONTEXT_ENABLE_BASE 0x2000
5050
#define CONTEXT_ENABLE_SIZE 0x80
5151

52+
#define PENDING_BASE 0x1000
53+
5254
/*
5355
* Each hart context has a set of control registers associated with it. Right
5456
* now there's only two: a source priority threshold over which the hart will
@@ -63,6 +65,7 @@
6365
#define PLIC_ENABLE_THRESHOLD 0
6466

6567
#define PLIC_QUIRK_EDGE_INTERRUPT 0
68+
#define PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM 1
6669

6770
struct plic_priv {
6871
struct fwnode_handle *fwnode;
@@ -388,6 +391,98 @@ static void plic_handle_irq(struct irq_desc *desc)
388391
chained_irq_exit(chip, desc);
389392
}
390393

394+
static u32 cp100_isolate_pending_irq(int nr_irq_groups, struct plic_handler *handler)
395+
{
396+
u32 __iomem *pending = handler->priv->regs + PENDING_BASE;
397+
u32 __iomem *enable = handler->enable_base;
398+
u32 pending_irqs = 0;
399+
int i, j;
400+
401+
/* Look for first pending interrupt */
402+
for (i = 0; i < nr_irq_groups; i++) {
403+
/* Any pending interrupts would be annihilated, so skip checking them */
404+
if (!handler->enable_save[i])
405+
continue;
406+
407+
pending_irqs = handler->enable_save[i] & readl_relaxed(pending + i);
408+
if (pending_irqs)
409+
break;
410+
}
411+
412+
if (!pending_irqs)
413+
return 0;
414+
415+
/* Isolate lowest set bit */
416+
pending_irqs &= -pending_irqs;
417+
418+
/* Disable all interrupts but the first pending one */
419+
for (j = 0; j < nr_irq_groups; j++) {
420+
u32 new_mask = j == i ? pending_irqs : 0;
421+
422+
if (new_mask != handler->enable_save[j])
423+
writel_relaxed(new_mask, enable + j);
424+
}
425+
return pending_irqs;
426+
}
427+
428+
static irq_hw_number_t cp100_get_hwirq(struct plic_handler *handler, void __iomem *claim)
429+
{
430+
int nr_irq_groups = DIV_ROUND_UP(handler->priv->nr_irqs, 32);
431+
u32 __iomem *enable = handler->enable_base;
432+
irq_hw_number_t hwirq = 0;
433+
u32 iso_mask;
434+
int i;
435+
436+
guard(raw_spinlock)(&handler->enable_lock);
437+
438+
/* Existing enable state is already cached in enable_save */
439+
iso_mask = cp100_isolate_pending_irq(nr_irq_groups, handler);
440+
if (!iso_mask)
441+
return 0;
442+
443+
/*
444+
* Interrupts delievered to hardware still become pending, but only
445+
* interrupts that are both pending and enabled can be claimed.
446+
* Clearing the enable bit for all interrupts but the first pending
447+
* one avoids a hardware bug that occurs during read from the claim
448+
* register with more than one eligible interrupt.
449+
*/
450+
hwirq = readl(claim);
451+
452+
/* Restore previous state */
453+
for (i = 0; i < nr_irq_groups; i++) {
454+
u32 written = i == hwirq / 32 ? iso_mask : 0;
455+
u32 stored = handler->enable_save[i];
456+
457+
if (stored != written)
458+
writel_relaxed(stored, enable + i);
459+
}
460+
return hwirq;
461+
}
462+
463+
static void plic_handle_irq_cp100(struct irq_desc *desc)
464+
{
465+
struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
466+
struct irq_chip *chip = irq_desc_get_chip(desc);
467+
void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
468+
irq_hw_number_t hwirq;
469+
470+
WARN_ON_ONCE(!handler->present);
471+
472+
chained_irq_enter(chip, desc);
473+
474+
while ((hwirq = cp100_get_hwirq(handler, claim))) {
475+
int err = generic_handle_domain_irq(handler->priv->irqdomain, hwirq);
476+
477+
if (unlikely(err)) {
478+
pr_warn_ratelimited("%pfwP: can't find mapping for hwirq %lu\n",
479+
handler->priv->fwnode, hwirq);
480+
}
481+
}
482+
483+
chained_irq_exit(chip, desc);
484+
}
485+
391486
static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
392487
{
393488
/* priority must be > threshold to trigger an interrupt */
@@ -424,6 +519,8 @@ static const struct of_device_id plic_match[] = {
424519
.data = (const void *)BIT(PLIC_QUIRK_EDGE_INTERRUPT) },
425520
{ .compatible = "thead,c900-plic",
426521
.data = (const void *)BIT(PLIC_QUIRK_EDGE_INTERRUPT) },
522+
{ .compatible = "ultrarisc,cp100-plic",
523+
.data = (const void *)BIT(PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM) },
427524
{}
428525
};
429526

@@ -658,12 +755,17 @@ static int plic_probe(struct fwnode_handle *fwnode)
658755
}
659756

660757
if (global_setup) {
758+
void (*handler_fn)(struct irq_desc *) = plic_handle_irq;
759+
760+
if (test_bit(PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM, &handler->priv->plic_quirks))
761+
handler_fn = plic_handle_irq_cp100;
762+
661763
/* Find parent domain and register chained handler */
662764
domain = irq_find_matching_fwnode(riscv_get_intc_hwnode(), DOMAIN_BUS_ANY);
663765
if (domain)
664766
plic_parent_irq = irq_create_mapping(domain, RV_IRQ_EXT);
665767
if (plic_parent_irq)
666-
irq_set_chained_handler(plic_parent_irq, plic_handle_irq);
768+
irq_set_chained_handler(plic_parent_irq, handler_fn);
667769

668770
cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
669771
"irqchip/sifive/plic:starting",

0 commit comments

Comments
 (0)