1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017 SiFive
4 * Copyright (C) 2018 Christoph Hellwig
7 #include <linux/interrupt.h>
10 #include <linux/irqchip.h>
11 #include <linux/irqchip/chained_irq.h>
12 #include <linux/irqdomain.h>
13 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/spinlock.h>
19 #include <linux/syscore_ops.h>
23 * This driver implements a version of the RISC-V PLIC with the actual layout
24 * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
26 * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
28 * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
29 * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
33 #define MAX_DEVICES 1024
34 #define MAX_CONTEXTS 15872
37 * Each interrupt source has a priority register associated with it.
38 * We always hardwire it to one in Linux.
40 #define PRIORITY_BASE 0
41 #define PRIORITY_PER_ID 4
44 * Each hart context has a vector of interrupt enable bits associated with it.
45 * There's one bit for each interrupt source.
47 #define CONTEXT_ENABLE_BASE 0x2000
48 #define CONTEXT_ENABLE_SIZE 0x80
51 * Each hart context has a set of control registers associated with it. Right
52 * now there's only two: a source priority threshold over which the hart will
53 * take an interrupt, and a register to claim interrupts.
55 #define CONTEXT_BASE 0x200000
56 #define CONTEXT_SIZE 0x1000
57 #define CONTEXT_THRESHOLD 0x00
58 #define CONTEXT_CLAIM 0x04
60 #define PLIC_DISABLE_THRESHOLD 0x7
61 #define PLIC_ENABLE_THRESHOLD 0
63 #define PLIC_QUIRK_EDGE_INTERRUPT 0
68 struct irq_domain *irqdomain;
70 unsigned long plic_quirks;
72 unsigned long *prio_save;
77 void __iomem *hart_base;
79 * Protect mask operations on the registers given that we can't
80 * assume atomic memory operations work on them.
82 raw_spinlock_t enable_lock;
83 void __iomem *enable_base;
85 struct plic_priv *priv;
87 static int plic_parent_irq __ro_after_init;
88 static bool plic_cpuhp_setup_done __ro_after_init;
89 static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
91 static int plic_irq_set_type(struct irq_data *d, unsigned int type);
93 static void __plic_toggle(void __iomem *enable_base, int hwirq, int enable)
95 u32 __iomem *reg = enable_base + (hwirq / 32) * sizeof(u32);
96 u32 hwirq_mask = 1 << (hwirq % 32);
99 writel(readl(reg) | hwirq_mask, reg);
101 writel(readl(reg) & ~hwirq_mask, reg);
104 static void plic_toggle(struct plic_handler *handler, int hwirq, int enable)
108 raw_spin_lock_irqsave(&handler->enable_lock, flags);
109 __plic_toggle(handler->enable_base, hwirq, enable);
110 raw_spin_unlock_irqrestore(&handler->enable_lock, flags);
113 static inline void plic_irq_toggle(const struct cpumask *mask,
114 struct irq_data *d, int enable)
118 for_each_cpu(cpu, mask) {
119 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
121 plic_toggle(handler, d->hwirq, enable);
125 static void plic_irq_enable(struct irq_data *d)
127 plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
130 static void plic_irq_disable(struct irq_data *d)
132 plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
135 static void plic_irq_unmask(struct irq_data *d)
137 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
139 writel(1, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
142 static void plic_irq_mask(struct irq_data *d)
144 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
146 writel(0, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
149 static void plic_irq_eoi(struct irq_data *d)
151 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
153 if (unlikely(irqd_irq_disabled(d))) {
154 plic_toggle(handler, d->hwirq, 1);
155 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
156 plic_toggle(handler, d->hwirq, 0);
158 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
163 static int plic_set_affinity(struct irq_data *d,
164 const struct cpumask *mask_val, bool force)
167 struct cpumask amask;
168 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
170 cpumask_and(&amask, &priv->lmask, mask_val);
173 cpu = cpumask_first(&amask);
175 cpu = cpumask_any_and(&amask, cpu_online_mask);
177 if (cpu >= nr_cpu_ids)
182 irq_data_update_effective_affinity(d, cpumask_of(cpu));
184 if (!irqd_irq_disabled(d))
187 return IRQ_SET_MASK_OK_DONE;
191 static struct irq_chip plic_edge_chip = {
192 .name = "SiFive PLIC",
193 .irq_enable = plic_irq_enable,
194 .irq_disable = plic_irq_disable,
195 .irq_ack = plic_irq_eoi,
196 .irq_mask = plic_irq_mask,
197 .irq_unmask = plic_irq_unmask,
199 .irq_set_affinity = plic_set_affinity,
201 .irq_set_type = plic_irq_set_type,
202 .flags = IRQCHIP_SKIP_SET_WAKE |
203 IRQCHIP_AFFINITY_PRE_STARTUP,
206 static struct irq_chip plic_chip = {
207 .name = "SiFive PLIC",
208 .irq_enable = plic_irq_enable,
209 .irq_disable = plic_irq_disable,
210 .irq_mask = plic_irq_mask,
211 .irq_unmask = plic_irq_unmask,
212 .irq_eoi = plic_irq_eoi,
214 .irq_set_affinity = plic_set_affinity,
216 .irq_set_type = plic_irq_set_type,
217 .flags = IRQCHIP_SKIP_SET_WAKE |
218 IRQCHIP_AFFINITY_PRE_STARTUP,
221 static int plic_irq_set_type(struct irq_data *d, unsigned int type)
223 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
225 if (!test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
226 return IRQ_SET_MASK_OK_NOCOPY;
229 case IRQ_TYPE_EDGE_RISING:
230 irq_set_chip_handler_name_locked(d, &plic_edge_chip,
231 handle_edge_irq, NULL);
233 case IRQ_TYPE_LEVEL_HIGH:
234 irq_set_chip_handler_name_locked(d, &plic_chip,
235 handle_fasteoi_irq, NULL);
241 return IRQ_SET_MASK_OK;
244 static int plic_irq_suspend(void)
249 struct plic_priv *priv;
251 priv = per_cpu_ptr(&plic_handlers, smp_processor_id())->priv;
253 for (i = 0; i < priv->nr_irqs; i++)
254 if (readl(priv->regs + PRIORITY_BASE + i * PRIORITY_PER_ID))
255 __set_bit(i, priv->prio_save);
257 __clear_bit(i, priv->prio_save);
259 for_each_cpu(cpu, cpu_present_mask) {
260 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
262 if (!handler->present)
265 raw_spin_lock_irqsave(&handler->enable_lock, flags);
266 for (i = 0; i < DIV_ROUND_UP(priv->nr_irqs, 32); i++) {
267 reg = handler->enable_base + i * sizeof(u32);
268 handler->enable_save[i] = readl(reg);
270 raw_spin_unlock_irqrestore(&handler->enable_lock, flags);
276 static void plic_irq_resume(void)
278 unsigned int i, index, cpu;
281 struct plic_priv *priv;
283 priv = per_cpu_ptr(&plic_handlers, smp_processor_id())->priv;
285 for (i = 0; i < priv->nr_irqs; i++) {
287 writel((priv->prio_save[index] & BIT_MASK(i)) ? 1 : 0,
288 priv->regs + PRIORITY_BASE + i * PRIORITY_PER_ID);
291 for_each_cpu(cpu, cpu_present_mask) {
292 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
294 if (!handler->present)
297 raw_spin_lock_irqsave(&handler->enable_lock, flags);
298 for (i = 0; i < DIV_ROUND_UP(priv->nr_irqs, 32); i++) {
299 reg = handler->enable_base + i * sizeof(u32);
300 writel(handler->enable_save[i], reg);
302 raw_spin_unlock_irqrestore(&handler->enable_lock, flags);
306 static struct syscore_ops plic_irq_syscore_ops = {
307 .suspend = plic_irq_suspend,
308 .resume = plic_irq_resume,
311 static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
312 irq_hw_number_t hwirq)
314 struct plic_priv *priv = d->host_data;
316 irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
317 handle_fasteoi_irq, NULL, NULL);
318 irq_set_noprobe(irq);
319 irq_set_affinity(irq, &priv->lmask);
323 static int plic_irq_domain_translate(struct irq_domain *d,
324 struct irq_fwspec *fwspec,
325 unsigned long *hwirq,
328 struct plic_priv *priv = d->host_data;
330 if (test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
331 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
333 return irq_domain_translate_onecell(d, fwspec, hwirq, type);
336 static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
337 unsigned int nr_irqs, void *arg)
340 irq_hw_number_t hwirq;
342 struct irq_fwspec *fwspec = arg;
344 ret = plic_irq_domain_translate(domain, fwspec, &hwirq, &type);
348 for (i = 0; i < nr_irqs; i++) {
349 ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
357 static const struct irq_domain_ops plic_irqdomain_ops = {
358 .translate = plic_irq_domain_translate,
359 .alloc = plic_irq_domain_alloc,
360 .free = irq_domain_free_irqs_top,
364 * Handling an interrupt is a two-step process: first you claim the interrupt
365 * by reading the claim register, then you complete the interrupt by writing
366 * that source ID back to the same claim register. This automatically enables
367 * and disables the interrupt, so there's nothing else to do.
369 static void plic_handle_irq(struct irq_desc *desc)
371 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
372 struct irq_chip *chip = irq_desc_get_chip(desc);
373 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
374 irq_hw_number_t hwirq;
376 WARN_ON_ONCE(!handler->present);
378 chained_irq_enter(chip, desc);
380 while ((hwirq = readl(claim))) {
381 int err = generic_handle_domain_irq(handler->priv->irqdomain,
384 dev_warn_ratelimited(handler->priv->dev,
385 "can't find mapping for hwirq %lu\n", hwirq);
389 chained_irq_exit(chip, desc);
392 static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
394 /* priority must be > threshold to trigger an interrupt */
395 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
398 static int plic_dying_cpu(unsigned int cpu)
401 disable_percpu_irq(plic_parent_irq);
406 static int plic_starting_cpu(unsigned int cpu)
408 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
411 enable_percpu_irq(plic_parent_irq,
412 irq_get_trigger_type(plic_parent_irq));
414 dev_warn(handler->priv->dev, "cpu%d: parent irq not available\n", cpu);
415 plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
420 static const struct of_device_id plic_match[] = {
421 { .compatible = "sifive,plic-1.0.0" },
422 { .compatible = "riscv,plic0" },
423 { .compatible = "andestech,nceplic100",
424 .data = (const void *)BIT(PLIC_QUIRK_EDGE_INTERRUPT) },
425 { .compatible = "thead,c900-plic",
426 .data = (const void *)BIT(PLIC_QUIRK_EDGE_INTERRUPT) },
430 static int plic_parse_nr_irqs_and_contexts(struct platform_device *pdev,
431 u32 *nr_irqs, u32 *nr_contexts)
433 struct device *dev = &pdev->dev;
437 * Currently, only OF fwnode is supported so extend this
438 * function for ACPI support.
440 if (!is_of_node(dev->fwnode))
443 rc = of_property_read_u32(to_of_node(dev->fwnode), "riscv,ndev", nr_irqs);
445 dev_err(dev, "riscv,ndev property not available\n");
449 *nr_contexts = of_irq_count(to_of_node(dev->fwnode));
450 if (WARN_ON(!(*nr_contexts))) {
451 dev_err(dev, "no PLIC context available\n");
458 static int plic_parse_context_parent(struct platform_device *pdev, u32 context,
459 u32 *parent_hwirq, int *parent_cpu)
461 struct device *dev = &pdev->dev;
462 struct of_phandle_args parent;
463 unsigned long hartid;
467 * Currently, only OF fwnode is supported so extend this
468 * function for ACPI support.
470 if (!is_of_node(dev->fwnode))
473 rc = of_irq_parse_one(to_of_node(dev->fwnode), context, &parent);
477 rc = riscv_of_parent_hartid(parent.np, &hartid);
481 *parent_hwirq = parent.args[0];
482 *parent_cpu = riscv_hartid_to_cpuid(hartid);
486 static int plic_probe(struct platform_device *pdev)
488 int error = 0, nr_contexts, nr_handlers = 0, cpu, i;
489 struct device *dev = &pdev->dev;
490 unsigned long plic_quirks = 0;
491 struct plic_handler *handler;
492 u32 nr_irqs, parent_hwirq;
493 struct irq_domain *domain;
494 struct plic_priv *priv;
495 irq_hw_number_t hwirq;
498 if (is_of_node(dev->fwnode)) {
499 const struct of_device_id *id;
501 id = of_match_node(plic_match, to_of_node(dev->fwnode));
503 plic_quirks = (unsigned long)id->data;
506 error = plic_parse_nr_irqs_and_contexts(pdev, &nr_irqs, &nr_contexts);
510 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
515 priv->plic_quirks = plic_quirks;
516 priv->nr_irqs = nr_irqs;
518 priv->regs = devm_platform_ioremap_resource(pdev, 0);
519 if (WARN_ON(!priv->regs))
522 priv->prio_save = devm_bitmap_zalloc(dev, nr_irqs, GFP_KERNEL);
523 if (!priv->prio_save)
526 for (i = 0; i < nr_contexts; i++) {
527 error = plic_parse_context_parent(pdev, i, &parent_hwirq, &cpu);
529 dev_warn(dev, "hwirq for context%d not found\n", i);
534 * Skip contexts other than external interrupts for our
537 if (parent_hwirq != RV_IRQ_EXT) {
538 /* Disable S-mode enable bits if running in M-mode. */
539 if (IS_ENABLED(CONFIG_RISCV_M_MODE)) {
540 void __iomem *enable_base = priv->regs +
541 CONTEXT_ENABLE_BASE +
542 i * CONTEXT_ENABLE_SIZE;
544 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
545 __plic_toggle(enable_base, hwirq, 0);
551 dev_warn(dev, "Invalid cpuid for context %d\n", i);
555 /* Find parent domain and register chained handler */
556 domain = irq_find_matching_fwnode(riscv_get_intc_hwnode(), DOMAIN_BUS_ANY);
557 if (!plic_parent_irq && domain) {
558 plic_parent_irq = irq_create_mapping(domain, RV_IRQ_EXT);
560 irq_set_chained_handler(plic_parent_irq, plic_handle_irq);
564 * When running in M-mode we need to ignore the S-mode handler.
565 * Here we assume it always comes later, but that might be a
568 handler = per_cpu_ptr(&plic_handlers, cpu);
569 if (handler->present) {
570 dev_warn(dev, "handler already present for context %d.\n", i);
571 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
575 cpumask_set_cpu(cpu, &priv->lmask);
576 handler->present = true;
577 handler->hart_base = priv->regs + CONTEXT_BASE +
579 raw_spin_lock_init(&handler->enable_lock);
580 handler->enable_base = priv->regs + CONTEXT_ENABLE_BASE +
581 i * CONTEXT_ENABLE_SIZE;
582 handler->priv = priv;
584 handler->enable_save = devm_kcalloc(dev, DIV_ROUND_UP(nr_irqs, 32),
585 sizeof(*handler->enable_save), GFP_KERNEL);
586 if (!handler->enable_save)
587 goto fail_cleanup_contexts;
589 for (hwirq = 1; hwirq <= nr_irqs; hwirq++) {
590 plic_toggle(handler, hwirq, 0);
591 writel(1, priv->regs + PRIORITY_BASE +
592 hwirq * PRIORITY_PER_ID);
597 priv->irqdomain = irq_domain_add_linear(to_of_node(dev->fwnode), nr_irqs + 1,
598 &plic_irqdomain_ops, priv);
599 if (WARN_ON(!priv->irqdomain))
600 goto fail_cleanup_contexts;
603 * We can have multiple PLIC instances so setup cpuhp state
604 * and register syscore operations only once after context
605 * handlers of all online CPUs are initialized.
607 if (!plic_cpuhp_setup_done) {
609 for_each_online_cpu(cpu) {
610 handler = per_cpu_ptr(&plic_handlers, cpu);
611 if (!handler->present) {
617 cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
618 "irqchip/sifive/plic:starting",
619 plic_starting_cpu, plic_dying_cpu);
620 register_syscore_ops(&plic_irq_syscore_ops);
621 plic_cpuhp_setup_done = true;
625 dev_info(dev, "mapped %d interrupts with %d handlers for %d contexts.\n",
626 nr_irqs, nr_handlers, nr_contexts);
629 fail_cleanup_contexts:
630 for (i = 0; i < nr_contexts; i++) {
631 if (plic_parse_context_parent(pdev, i, &parent_hwirq, &cpu))
633 if (parent_hwirq != RV_IRQ_EXT || cpu < 0)
636 handler = per_cpu_ptr(&plic_handlers, cpu);
637 handler->present = false;
638 handler->hart_base = NULL;
639 handler->enable_base = NULL;
640 handler->enable_save = NULL;
641 handler->priv = NULL;
646 static struct platform_driver plic_driver = {
648 .name = "riscv-plic",
649 .of_match_table = plic_match,
653 builtin_platform_driver(plic_driver);