1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017 SiFive
4 * Copyright (C) 2018 Christoph Hellwig
6 #define pr_fmt(fmt) "plic: " fmt
8 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/irqchip.h>
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/platform_device.h>
19 #include <linux/spinlock.h>
20 #include <linux/syscore_ops.h>
24 * This driver implements a version of the RISC-V PLIC with the actual layout
25 * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
27 * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
29 * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
30 * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
34 #define MAX_DEVICES 1024
35 #define MAX_CONTEXTS 15872
38 * Each interrupt source has a priority register associated with it.
39 * We always hardwire it to one in Linux.
41 #define PRIORITY_BASE 0
42 #define PRIORITY_PER_ID 4
45 * Each hart context has a vector of interrupt enable bits associated with it.
46 * There's one bit for each interrupt source.
48 #define CONTEXT_ENABLE_BASE 0x2000
49 #define CONTEXT_ENABLE_SIZE 0x80
52 * Each hart context has a set of control registers associated with it. Right
53 * now there's only two: a source priority threshold over which the hart will
54 * take an interrupt, and a register to claim interrupts.
56 #define CONTEXT_BASE 0x200000
57 #define CONTEXT_SIZE 0x1000
58 #define CONTEXT_THRESHOLD 0x00
59 #define CONTEXT_CLAIM 0x04
61 #define PLIC_DISABLE_THRESHOLD 0x7
62 #define PLIC_ENABLE_THRESHOLD 0
64 #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)
106 raw_spin_lock(&handler->enable_lock);
107 __plic_toggle(handler->enable_base, hwirq, enable);
108 raw_spin_unlock(&handler->enable_lock);
111 static inline void plic_irq_toggle(const struct cpumask *mask,
112 struct irq_data *d, int enable)
116 for_each_cpu(cpu, mask) {
117 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
119 plic_toggle(handler, d->hwirq, enable);
123 static void plic_irq_enable(struct irq_data *d)
125 plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
128 static void plic_irq_disable(struct irq_data *d)
130 plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
133 static void plic_irq_unmask(struct irq_data *d)
135 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
137 writel(1, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
140 static void plic_irq_mask(struct irq_data *d)
142 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
144 writel(0, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
147 static void plic_irq_eoi(struct irq_data *d)
149 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
151 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
155 static int plic_set_affinity(struct irq_data *d,
156 const struct cpumask *mask_val, bool force)
159 struct cpumask amask;
160 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
162 cpumask_and(&amask, &priv->lmask, mask_val);
165 cpu = cpumask_first(&amask);
167 cpu = cpumask_any_and(&amask, cpu_online_mask);
169 if (cpu >= nr_cpu_ids)
174 irq_data_update_effective_affinity(d, cpumask_of(cpu));
176 if (!irqd_irq_disabled(d))
179 return IRQ_SET_MASK_OK_DONE;
183 static struct irq_chip plic_edge_chip = {
184 .name = "SiFive PLIC",
185 .irq_enable = plic_irq_enable,
186 .irq_disable = plic_irq_disable,
187 .irq_ack = plic_irq_eoi,
188 .irq_mask = plic_irq_mask,
189 .irq_unmask = plic_irq_unmask,
191 .irq_set_affinity = plic_set_affinity,
193 .irq_set_type = plic_irq_set_type,
194 .flags = IRQCHIP_SKIP_SET_WAKE |
195 IRQCHIP_AFFINITY_PRE_STARTUP,
198 static struct irq_chip plic_chip = {
199 .name = "SiFive PLIC",
200 .irq_enable = plic_irq_enable,
201 .irq_disable = plic_irq_disable,
202 .irq_mask = plic_irq_mask,
203 .irq_unmask = plic_irq_unmask,
204 .irq_eoi = plic_irq_eoi,
206 .irq_set_affinity = plic_set_affinity,
208 .irq_set_type = plic_irq_set_type,
209 .flags = IRQCHIP_SKIP_SET_WAKE |
210 IRQCHIP_AFFINITY_PRE_STARTUP,
213 static int plic_irq_set_type(struct irq_data *d, unsigned int type)
215 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
217 if (!test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
218 return IRQ_SET_MASK_OK_NOCOPY;
221 case IRQ_TYPE_EDGE_RISING:
222 irq_set_chip_handler_name_locked(d, &plic_edge_chip,
223 handle_edge_irq, NULL);
225 case IRQ_TYPE_LEVEL_HIGH:
226 irq_set_chip_handler_name_locked(d, &plic_chip,
227 handle_fasteoi_irq, NULL);
233 return IRQ_SET_MASK_OK;
236 static int plic_irq_suspend(void)
240 struct plic_priv *priv;
242 priv = per_cpu_ptr(&plic_handlers, smp_processor_id())->priv;
244 for (i = 0; i < priv->nr_irqs; i++)
245 if (readl(priv->regs + PRIORITY_BASE + i * PRIORITY_PER_ID))
246 __set_bit(i, priv->prio_save);
248 __clear_bit(i, priv->prio_save);
250 for_each_cpu(cpu, cpu_present_mask) {
251 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
253 if (!handler->present)
256 raw_spin_lock(&handler->enable_lock);
257 for (i = 0; i < DIV_ROUND_UP(priv->nr_irqs, 32); i++) {
258 reg = handler->enable_base + i * sizeof(u32);
259 handler->enable_save[i] = readl(reg);
261 raw_spin_unlock(&handler->enable_lock);
267 static void plic_irq_resume(void)
269 unsigned int i, index, cpu;
271 struct plic_priv *priv;
273 priv = per_cpu_ptr(&plic_handlers, smp_processor_id())->priv;
275 for (i = 0; i < priv->nr_irqs; i++) {
277 writel((priv->prio_save[index] & BIT_MASK(i)) ? 1 : 0,
278 priv->regs + PRIORITY_BASE + i * PRIORITY_PER_ID);
281 for_each_cpu(cpu, cpu_present_mask) {
282 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
284 if (!handler->present)
287 raw_spin_lock(&handler->enable_lock);
288 for (i = 0; i < DIV_ROUND_UP(priv->nr_irqs, 32); i++) {
289 reg = handler->enable_base + i * sizeof(u32);
290 writel(handler->enable_save[i], reg);
292 raw_spin_unlock(&handler->enable_lock);
296 static struct syscore_ops plic_irq_syscore_ops = {
297 .suspend = plic_irq_suspend,
298 .resume = plic_irq_resume,
301 static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
302 irq_hw_number_t hwirq)
304 struct plic_priv *priv = d->host_data;
306 irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
307 handle_fasteoi_irq, NULL, NULL);
308 irq_set_noprobe(irq);
309 irq_set_affinity(irq, &priv->lmask);
313 static int plic_irq_domain_translate(struct irq_domain *d,
314 struct irq_fwspec *fwspec,
315 unsigned long *hwirq,
318 struct plic_priv *priv = d->host_data;
320 if (test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
321 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
323 return irq_domain_translate_onecell(d, fwspec, hwirq, type);
326 static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
327 unsigned int nr_irqs, void *arg)
330 irq_hw_number_t hwirq;
332 struct irq_fwspec *fwspec = arg;
334 ret = plic_irq_domain_translate(domain, fwspec, &hwirq, &type);
338 for (i = 0; i < nr_irqs; i++) {
339 ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
347 static const struct irq_domain_ops plic_irqdomain_ops = {
348 .translate = plic_irq_domain_translate,
349 .alloc = plic_irq_domain_alloc,
350 .free = irq_domain_free_irqs_top,
354 * Handling an interrupt is a two-step process: first you claim the interrupt
355 * by reading the claim register, then you complete the interrupt by writing
356 * that source ID back to the same claim register. This automatically enables
357 * and disables the interrupt, so there's nothing else to do.
359 static void plic_handle_irq(struct irq_desc *desc)
361 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
362 struct irq_chip *chip = irq_desc_get_chip(desc);
363 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
364 irq_hw_number_t hwirq;
366 WARN_ON_ONCE(!handler->present);
368 chained_irq_enter(chip, desc);
370 while ((hwirq = readl(claim))) {
371 int err = generic_handle_domain_irq(handler->priv->irqdomain,
374 pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
378 chained_irq_exit(chip, desc);
381 static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
383 /* priority must be > threshold to trigger an interrupt */
384 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
387 static int plic_dying_cpu(unsigned int cpu)
390 disable_percpu_irq(plic_parent_irq);
395 static int plic_starting_cpu(unsigned int cpu)
397 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
400 enable_percpu_irq(plic_parent_irq,
401 irq_get_trigger_type(plic_parent_irq));
403 pr_warn("cpu%d: parent irq not available\n", cpu);
404 plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
409 static int __init __plic_init(struct device_node *node,
410 struct device_node *parent,
411 unsigned long plic_quirks)
413 int error = 0, nr_contexts, nr_handlers = 0, i;
415 struct plic_priv *priv;
416 struct plic_handler *handler;
419 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
423 priv->plic_quirks = plic_quirks;
425 priv->regs = of_iomap(node, 0);
426 if (WARN_ON(!priv->regs)) {
432 of_property_read_u32(node, "riscv,ndev", &nr_irqs);
433 if (WARN_ON(!nr_irqs))
436 priv->nr_irqs = nr_irqs;
438 priv->prio_save = bitmap_alloc(nr_irqs, GFP_KERNEL);
439 if (!priv->prio_save)
440 goto out_free_priority_reg;
442 nr_contexts = of_irq_count(node);
443 if (WARN_ON(!nr_contexts))
444 goto out_free_priority_reg;
447 priv->irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
448 &plic_irqdomain_ops, priv);
449 if (WARN_ON(!priv->irqdomain))
450 goto out_free_priority_reg;
452 for (i = 0; i < nr_contexts; i++) {
453 struct of_phandle_args parent;
454 irq_hw_number_t hwirq;
456 unsigned long hartid;
458 if (of_irq_parse_one(node, i, &parent)) {
459 pr_err("failed to parse parent for context %d.\n", i);
464 * Skip contexts other than external interrupts for our
467 if (parent.args[0] != RV_IRQ_EXT) {
468 /* Disable S-mode enable bits if running in M-mode. */
469 if (IS_ENABLED(CONFIG_RISCV_M_MODE)) {
470 void __iomem *enable_base = priv->regs +
471 CONTEXT_ENABLE_BASE +
472 i * CONTEXT_ENABLE_SIZE;
474 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
475 __plic_toggle(enable_base, hwirq, 0);
480 error = riscv_of_parent_hartid(parent.np, &hartid);
482 pr_warn("failed to parse hart ID for context %d.\n", i);
486 cpu = riscv_hartid_to_cpuid(hartid);
488 pr_warn("Invalid cpuid for context %d\n", i);
492 /* Find parent domain and register chained handler */
493 if (!plic_parent_irq && irq_find_host(parent.np)) {
494 plic_parent_irq = irq_of_parse_and_map(node, i);
496 irq_set_chained_handler(plic_parent_irq,
501 * When running in M-mode we need to ignore the S-mode handler.
502 * Here we assume it always comes later, but that might be a
505 handler = per_cpu_ptr(&plic_handlers, cpu);
506 if (handler->present) {
507 pr_warn("handler already present for context %d.\n", i);
508 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
512 cpumask_set_cpu(cpu, &priv->lmask);
513 handler->present = true;
514 handler->hart_base = priv->regs + CONTEXT_BASE +
516 raw_spin_lock_init(&handler->enable_lock);
517 handler->enable_base = priv->regs + CONTEXT_ENABLE_BASE +
518 i * CONTEXT_ENABLE_SIZE;
519 handler->priv = priv;
521 handler->enable_save = kcalloc(DIV_ROUND_UP(nr_irqs, 32),
522 sizeof(*handler->enable_save), GFP_KERNEL);
523 if (!handler->enable_save)
524 goto out_free_enable_reg;
526 for (hwirq = 1; hwirq <= nr_irqs; hwirq++) {
527 plic_toggle(handler, hwirq, 0);
528 writel(1, priv->regs + PRIORITY_BASE +
529 hwirq * PRIORITY_PER_ID);
535 * We can have multiple PLIC instances so setup cpuhp state only
536 * when context handler for current/boot CPU is present.
538 handler = this_cpu_ptr(&plic_handlers);
539 if (handler->present && !plic_cpuhp_setup_done) {
540 cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
541 "irqchip/sifive/plic:starting",
542 plic_starting_cpu, plic_dying_cpu);
543 plic_cpuhp_setup_done = true;
545 register_syscore_ops(&plic_irq_syscore_ops);
547 pr_info("%pOFP: mapped %d interrupts with %d handlers for"
548 " %d contexts.\n", node, nr_irqs, nr_handlers, nr_contexts);
552 for_each_cpu(cpu, cpu_present_mask) {
553 handler = per_cpu_ptr(&plic_handlers, cpu);
554 kfree(handler->enable_save);
556 out_free_priority_reg:
557 kfree(priv->prio_save);
565 static int __init plic_init(struct device_node *node,
566 struct device_node *parent)
568 return __plic_init(node, parent, 0);
571 IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
572 IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */
574 static int __init plic_edge_init(struct device_node *node,
575 struct device_node *parent)
577 return __plic_init(node, parent, BIT(PLIC_QUIRK_EDGE_INTERRUPT));
580 IRQCHIP_DECLARE(andestech_nceplic100, "andestech,nceplic100", plic_edge_init);
581 IRQCHIP_DECLARE(thead_c900_plic, "thead,c900-plic", plic_edge_init);