Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2017 SiFive
4 * Copyright (C) 2018 Christoph Hellwig
5 */
6#define pr_fmt(fmt) "plic: " fmt
7#include <linux/cpu.h>
8#include <linux/interrupt.h>
9#include <linux/io.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>
15#include <linux/of.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>
21#include <asm/smp.h>
22
23/*
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:
26 *
27 * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
28 *
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
31 * Spec.
32 */
33
34#define MAX_DEVICES 1024
35#define MAX_CONTEXTS 15872
36
37/*
38 * Each interrupt source has a priority register associated with it.
39 * We always hardwire it to one in Linux.
40 */
41#define PRIORITY_BASE 0
42#define PRIORITY_PER_ID 4
43
44/*
45 * Each hart context has a vector of interrupt enable bits associated with it.
46 * There's one bit for each interrupt source.
47 */
48#define CONTEXT_ENABLE_BASE 0x2000
49#define CONTEXT_ENABLE_SIZE 0x80
50
51/*
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.
55 */
56#define CONTEXT_BASE 0x200000
57#define CONTEXT_SIZE 0x1000
58#define CONTEXT_THRESHOLD 0x00
59#define CONTEXT_CLAIM 0x04
60
61#define PLIC_DISABLE_THRESHOLD 0x7
62#define PLIC_ENABLE_THRESHOLD 0
63
64#define PLIC_QUIRK_EDGE_INTERRUPT 0
65
66struct plic_priv {
67 struct cpumask lmask;
68 struct irq_domain *irqdomain;
69 void __iomem *regs;
70 unsigned long plic_quirks;
71 unsigned int nr_irqs;
72 unsigned long *prio_save;
73};
74
75struct plic_handler {
76 bool present;
77 void __iomem *hart_base;
78 /*
79 * Protect mask operations on the registers given that we can't
80 * assume atomic memory operations work on them.
81 */
82 raw_spinlock_t enable_lock;
83 void __iomem *enable_base;
84 u32 *enable_save;
85 struct plic_priv *priv;
86};
87static int plic_parent_irq __ro_after_init;
88static bool plic_cpuhp_setup_done __ro_after_init;
89static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
90
91static int plic_irq_set_type(struct irq_data *d, unsigned int type);
92
93static void __plic_toggle(void __iomem *enable_base, int hwirq, int enable)
94{
95 u32 __iomem *reg = enable_base + (hwirq / 32) * sizeof(u32);
96 u32 hwirq_mask = 1 << (hwirq % 32);
97
98 if (enable)
99 writel(readl(reg) | hwirq_mask, reg);
100 else
101 writel(readl(reg) & ~hwirq_mask, reg);
102}
103
104static void plic_toggle(struct plic_handler *handler, int hwirq, int enable)
105{
106 raw_spin_lock(&handler->enable_lock);
107 __plic_toggle(handler->enable_base, hwirq, enable);
108 raw_spin_unlock(&handler->enable_lock);
109}
110
111static inline void plic_irq_toggle(const struct cpumask *mask,
112 struct irq_data *d, int enable)
113{
114 int cpu;
115
116 for_each_cpu(cpu, mask) {
117 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
118
119 plic_toggle(handler, d->hwirq, enable);
120 }
121}
122
123static void plic_irq_enable(struct irq_data *d)
124{
125 plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
126}
127
128static void plic_irq_disable(struct irq_data *d)
129{
130 plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
131}
132
133static void plic_irq_unmask(struct irq_data *d)
134{
135 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
136
137 writel(1, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
138}
139
140static void plic_irq_mask(struct irq_data *d)
141{
142 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
143
144 writel(0, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
145}
146
147static void plic_irq_eoi(struct irq_data *d)
148{
149 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
150
151 if (unlikely(irqd_irq_disabled(d))) {
152 plic_toggle(handler, d->hwirq, 1);
153 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
154 plic_toggle(handler, d->hwirq, 0);
155 } else {
156 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
157 }
158}
159
160#ifdef CONFIG_SMP
161static int plic_set_affinity(struct irq_data *d,
162 const struct cpumask *mask_val, bool force)
163{
164 unsigned int cpu;
165 struct cpumask amask;
166 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
167
168 cpumask_and(&amask, &priv->lmask, mask_val);
169
170 if (force)
171 cpu = cpumask_first(&amask);
172 else
173 cpu = cpumask_any_and(&amask, cpu_online_mask);
174
175 if (cpu >= nr_cpu_ids)
176 return -EINVAL;
177
178 plic_irq_disable(d);
179
180 irq_data_update_effective_affinity(d, cpumask_of(cpu));
181
182 if (!irqd_irq_disabled(d))
183 plic_irq_enable(d);
184
185 return IRQ_SET_MASK_OK_DONE;
186}
187#endif
188
189static struct irq_chip plic_edge_chip = {
190 .name = "SiFive PLIC",
191 .irq_enable = plic_irq_enable,
192 .irq_disable = plic_irq_disable,
193 .irq_ack = plic_irq_eoi,
194 .irq_mask = plic_irq_mask,
195 .irq_unmask = plic_irq_unmask,
196#ifdef CONFIG_SMP
197 .irq_set_affinity = plic_set_affinity,
198#endif
199 .irq_set_type = plic_irq_set_type,
200 .flags = IRQCHIP_SKIP_SET_WAKE |
201 IRQCHIP_AFFINITY_PRE_STARTUP,
202};
203
204static struct irq_chip plic_chip = {
205 .name = "SiFive PLIC",
206 .irq_enable = plic_irq_enable,
207 .irq_disable = plic_irq_disable,
208 .irq_mask = plic_irq_mask,
209 .irq_unmask = plic_irq_unmask,
210 .irq_eoi = plic_irq_eoi,
211#ifdef CONFIG_SMP
212 .irq_set_affinity = plic_set_affinity,
213#endif
214 .irq_set_type = plic_irq_set_type,
215 .flags = IRQCHIP_SKIP_SET_WAKE |
216 IRQCHIP_AFFINITY_PRE_STARTUP,
217};
218
219static int plic_irq_set_type(struct irq_data *d, unsigned int type)
220{
221 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
222
223 if (!test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
224 return IRQ_SET_MASK_OK_NOCOPY;
225
226 switch (type) {
227 case IRQ_TYPE_EDGE_RISING:
228 irq_set_chip_handler_name_locked(d, &plic_edge_chip,
229 handle_edge_irq, NULL);
230 break;
231 case IRQ_TYPE_LEVEL_HIGH:
232 irq_set_chip_handler_name_locked(d, &plic_chip,
233 handle_fasteoi_irq, NULL);
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 return IRQ_SET_MASK_OK;
240}
241
242static int plic_irq_suspend(void)
243{
244 unsigned int i, cpu;
245 u32 __iomem *reg;
246 struct plic_priv *priv;
247
248 priv = per_cpu_ptr(&plic_handlers, smp_processor_id())->priv;
249
250 for (i = 0; i < priv->nr_irqs; i++)
251 if (readl(priv->regs + PRIORITY_BASE + i * PRIORITY_PER_ID))
252 __set_bit(i, priv->prio_save);
253 else
254 __clear_bit(i, priv->prio_save);
255
256 for_each_cpu(cpu, cpu_present_mask) {
257 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
258
259 if (!handler->present)
260 continue;
261
262 raw_spin_lock(&handler->enable_lock);
263 for (i = 0; i < DIV_ROUND_UP(priv->nr_irqs, 32); i++) {
264 reg = handler->enable_base + i * sizeof(u32);
265 handler->enable_save[i] = readl(reg);
266 }
267 raw_spin_unlock(&handler->enable_lock);
268 }
269
270 return 0;
271}
272
273static void plic_irq_resume(void)
274{
275 unsigned int i, index, cpu;
276 u32 __iomem *reg;
277 struct plic_priv *priv;
278
279 priv = per_cpu_ptr(&plic_handlers, smp_processor_id())->priv;
280
281 for (i = 0; i < priv->nr_irqs; i++) {
282 index = BIT_WORD(i);
283 writel((priv->prio_save[index] & BIT_MASK(i)) ? 1 : 0,
284 priv->regs + PRIORITY_BASE + i * PRIORITY_PER_ID);
285 }
286
287 for_each_cpu(cpu, cpu_present_mask) {
288 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
289
290 if (!handler->present)
291 continue;
292
293 raw_spin_lock(&handler->enable_lock);
294 for (i = 0; i < DIV_ROUND_UP(priv->nr_irqs, 32); i++) {
295 reg = handler->enable_base + i * sizeof(u32);
296 writel(handler->enable_save[i], reg);
297 }
298 raw_spin_unlock(&handler->enable_lock);
299 }
300}
301
302static struct syscore_ops plic_irq_syscore_ops = {
303 .suspend = plic_irq_suspend,
304 .resume = plic_irq_resume,
305};
306
307static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
308 irq_hw_number_t hwirq)
309{
310 struct plic_priv *priv = d->host_data;
311
312 irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
313 handle_fasteoi_irq, NULL, NULL);
314 irq_set_noprobe(irq);
315 irq_set_affinity(irq, &priv->lmask);
316 return 0;
317}
318
319static int plic_irq_domain_translate(struct irq_domain *d,
320 struct irq_fwspec *fwspec,
321 unsigned long *hwirq,
322 unsigned int *type)
323{
324 struct plic_priv *priv = d->host_data;
325
326 if (test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
327 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
328
329 return irq_domain_translate_onecell(d, fwspec, hwirq, type);
330}
331
332static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
333 unsigned int nr_irqs, void *arg)
334{
335 int i, ret;
336 irq_hw_number_t hwirq;
337 unsigned int type;
338 struct irq_fwspec *fwspec = arg;
339
340 ret = plic_irq_domain_translate(domain, fwspec, &hwirq, &type);
341 if (ret)
342 return ret;
343
344 for (i = 0; i < nr_irqs; i++) {
345 ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
346 if (ret)
347 return ret;
348 }
349
350 return 0;
351}
352
353static const struct irq_domain_ops plic_irqdomain_ops = {
354 .translate = plic_irq_domain_translate,
355 .alloc = plic_irq_domain_alloc,
356 .free = irq_domain_free_irqs_top,
357};
358
359/*
360 * Handling an interrupt is a two-step process: first you claim the interrupt
361 * by reading the claim register, then you complete the interrupt by writing
362 * that source ID back to the same claim register. This automatically enables
363 * and disables the interrupt, so there's nothing else to do.
364 */
365static void plic_handle_irq(struct irq_desc *desc)
366{
367 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
368 struct irq_chip *chip = irq_desc_get_chip(desc);
369 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
370 irq_hw_number_t hwirq;
371
372 WARN_ON_ONCE(!handler->present);
373
374 chained_irq_enter(chip, desc);
375
376 while ((hwirq = readl(claim))) {
377 int err = generic_handle_domain_irq(handler->priv->irqdomain,
378 hwirq);
379 if (unlikely(err))
380 pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
381 hwirq);
382 }
383
384 chained_irq_exit(chip, desc);
385}
386
387static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
388{
389 /* priority must be > threshold to trigger an interrupt */
390 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
391}
392
393static int plic_dying_cpu(unsigned int cpu)
394{
395 if (plic_parent_irq)
396 disable_percpu_irq(plic_parent_irq);
397
398 return 0;
399}
400
401static int plic_starting_cpu(unsigned int cpu)
402{
403 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
404
405 if (plic_parent_irq)
406 enable_percpu_irq(plic_parent_irq,
407 irq_get_trigger_type(plic_parent_irq));
408 else
409 pr_warn("cpu%d: parent irq not available\n", cpu);
410 plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
411
412 return 0;
413}
414
415static int __init __plic_init(struct device_node *node,
416 struct device_node *parent,
417 unsigned long plic_quirks)
418{
419 int error = 0, nr_contexts, nr_handlers = 0, i;
420 u32 nr_irqs;
421 struct plic_priv *priv;
422 struct plic_handler *handler;
423 unsigned int cpu;
424
425 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
426 if (!priv)
427 return -ENOMEM;
428
429 priv->plic_quirks = plic_quirks;
430
431 priv->regs = of_iomap(node, 0);
432 if (WARN_ON(!priv->regs)) {
433 error = -EIO;
434 goto out_free_priv;
435 }
436
437 error = -EINVAL;
438 of_property_read_u32(node, "riscv,ndev", &nr_irqs);
439 if (WARN_ON(!nr_irqs))
440 goto out_iounmap;
441
442 priv->nr_irqs = nr_irqs;
443
444 priv->prio_save = bitmap_alloc(nr_irqs, GFP_KERNEL);
445 if (!priv->prio_save)
446 goto out_free_priority_reg;
447
448 nr_contexts = of_irq_count(node);
449 if (WARN_ON(!nr_contexts))
450 goto out_free_priority_reg;
451
452 error = -ENOMEM;
453 priv->irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
454 &plic_irqdomain_ops, priv);
455 if (WARN_ON(!priv->irqdomain))
456 goto out_free_priority_reg;
457
458 for (i = 0; i < nr_contexts; i++) {
459 struct of_phandle_args parent;
460 irq_hw_number_t hwirq;
461 int cpu;
462 unsigned long hartid;
463
464 if (of_irq_parse_one(node, i, &parent)) {
465 pr_err("failed to parse parent for context %d.\n", i);
466 continue;
467 }
468
469 /*
470 * Skip contexts other than external interrupts for our
471 * privilege level.
472 */
473 if (parent.args[0] != RV_IRQ_EXT) {
474 /* Disable S-mode enable bits if running in M-mode. */
475 if (IS_ENABLED(CONFIG_RISCV_M_MODE)) {
476 void __iomem *enable_base = priv->regs +
477 CONTEXT_ENABLE_BASE +
478 i * CONTEXT_ENABLE_SIZE;
479
480 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
481 __plic_toggle(enable_base, hwirq, 0);
482 }
483 continue;
484 }
485
486 error = riscv_of_parent_hartid(parent.np, &hartid);
487 if (error < 0) {
488 pr_warn("failed to parse hart ID for context %d.\n", i);
489 continue;
490 }
491
492 cpu = riscv_hartid_to_cpuid(hartid);
493 if (cpu < 0) {
494 pr_warn("Invalid cpuid for context %d\n", i);
495 continue;
496 }
497
498 /* Find parent domain and register chained handler */
499 if (!plic_parent_irq && irq_find_host(parent.np)) {
500 plic_parent_irq = irq_of_parse_and_map(node, i);
501 if (plic_parent_irq)
502 irq_set_chained_handler(plic_parent_irq,
503 plic_handle_irq);
504 }
505
506 /*
507 * When running in M-mode we need to ignore the S-mode handler.
508 * Here we assume it always comes later, but that might be a
509 * little fragile.
510 */
511 handler = per_cpu_ptr(&plic_handlers, cpu);
512 if (handler->present) {
513 pr_warn("handler already present for context %d.\n", i);
514 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
515 goto done;
516 }
517
518 cpumask_set_cpu(cpu, &priv->lmask);
519 handler->present = true;
520 handler->hart_base = priv->regs + CONTEXT_BASE +
521 i * CONTEXT_SIZE;
522 raw_spin_lock_init(&handler->enable_lock);
523 handler->enable_base = priv->regs + CONTEXT_ENABLE_BASE +
524 i * CONTEXT_ENABLE_SIZE;
525 handler->priv = priv;
526
527 handler->enable_save = kcalloc(DIV_ROUND_UP(nr_irqs, 32),
528 sizeof(*handler->enable_save), GFP_KERNEL);
529 if (!handler->enable_save)
530 goto out_free_enable_reg;
531done:
532 for (hwirq = 1; hwirq <= nr_irqs; hwirq++) {
533 plic_toggle(handler, hwirq, 0);
534 writel(1, priv->regs + PRIORITY_BASE +
535 hwirq * PRIORITY_PER_ID);
536 }
537 nr_handlers++;
538 }
539
540 /*
541 * We can have multiple PLIC instances so setup cpuhp state
542 * and register syscore operations only when context handler
543 * for current/boot CPU is present.
544 */
545 handler = this_cpu_ptr(&plic_handlers);
546 if (handler->present && !plic_cpuhp_setup_done) {
547 cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
548 "irqchip/sifive/plic:starting",
549 plic_starting_cpu, plic_dying_cpu);
550 register_syscore_ops(&plic_irq_syscore_ops);
551 plic_cpuhp_setup_done = true;
552 }
553
554 pr_info("%pOFP: mapped %d interrupts with %d handlers for"
555 " %d contexts.\n", node, nr_irqs, nr_handlers, nr_contexts);
556 return 0;
557
558out_free_enable_reg:
559 for_each_cpu(cpu, cpu_present_mask) {
560 handler = per_cpu_ptr(&plic_handlers, cpu);
561 kfree(handler->enable_save);
562 }
563out_free_priority_reg:
564 kfree(priv->prio_save);
565out_iounmap:
566 iounmap(priv->regs);
567out_free_priv:
568 kfree(priv);
569 return error;
570}
571
572static int __init plic_init(struct device_node *node,
573 struct device_node *parent)
574{
575 return __plic_init(node, parent, 0);
576}
577
578IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
579IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */
580
581static int __init plic_edge_init(struct device_node *node,
582 struct device_node *parent)
583{
584 return __plic_init(node, parent, BIT(PLIC_QUIRK_EDGE_INTERRUPT));
585}
586
587IRQCHIP_DECLARE(andestech_nceplic100, "andestech,nceplic100", plic_edge_init);
588IRQCHIP_DECLARE(thead_c900_plic, "thead,c900-plic", plic_edge_init);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2017 SiFive
4 * Copyright (C) 2018 Christoph Hellwig
5 */
6#define pr_fmt(fmt) "plic: " fmt
7#include <linux/interrupt.h>
8#include <linux/io.h>
9#include <linux/irq.h>
10#include <linux/irqchip.h>
11#include <linux/irqdomain.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_address.h>
15#include <linux/of_irq.h>
16#include <linux/platform_device.h>
17#include <linux/spinlock.h>
18#include <asm/smp.h>
19
20/*
21 * This driver implements a version of the RISC-V PLIC with the actual layout
22 * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
23 *
24 * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
25 *
26 * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
27 * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
28 * Spec.
29 */
30
31#define MAX_DEVICES 1024
32#define MAX_CONTEXTS 15872
33
34/*
35 * Each interrupt source has a priority register associated with it.
36 * We always hardwire it to one in Linux.
37 */
38#define PRIORITY_BASE 0
39#define PRIORITY_PER_ID 4
40
41/*
42 * Each hart context has a vector of interrupt enable bits associated with it.
43 * There's one bit for each interrupt source.
44 */
45#define ENABLE_BASE 0x2000
46#define ENABLE_PER_HART 0x80
47
48/*
49 * Each hart context has a set of control registers associated with it. Right
50 * now there's only two: a source priority threshold over which the hart will
51 * take an interrupt, and a register to claim interrupts.
52 */
53#define CONTEXT_BASE 0x200000
54#define CONTEXT_PER_HART 0x1000
55#define CONTEXT_THRESHOLD 0x00
56#define CONTEXT_CLAIM 0x04
57
58static void __iomem *plic_regs;
59
60struct plic_handler {
61 bool present;
62 void __iomem *hart_base;
63 /*
64 * Protect mask operations on the registers given that we can't
65 * assume atomic memory operations work on them.
66 */
67 raw_spinlock_t enable_lock;
68 void __iomem *enable_base;
69};
70static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
71
72static inline void plic_toggle(struct plic_handler *handler,
73 int hwirq, int enable)
74{
75 u32 __iomem *reg = handler->enable_base + (hwirq / 32) * sizeof(u32);
76 u32 hwirq_mask = 1 << (hwirq % 32);
77
78 raw_spin_lock(&handler->enable_lock);
79 if (enable)
80 writel(readl(reg) | hwirq_mask, reg);
81 else
82 writel(readl(reg) & ~hwirq_mask, reg);
83 raw_spin_unlock(&handler->enable_lock);
84}
85
86static inline void plic_irq_toggle(const struct cpumask *mask,
87 int hwirq, int enable)
88{
89 int cpu;
90
91 writel(enable, plic_regs + PRIORITY_BASE + hwirq * PRIORITY_PER_ID);
92 for_each_cpu(cpu, mask) {
93 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
94
95 if (handler->present)
96 plic_toggle(handler, hwirq, enable);
97 }
98}
99
100static void plic_irq_unmask(struct irq_data *d)
101{
102 unsigned int cpu = cpumask_any_and(irq_data_get_affinity_mask(d),
103 cpu_online_mask);
104 if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
105 return;
106 plic_irq_toggle(cpumask_of(cpu), d->hwirq, 1);
107}
108
109static void plic_irq_mask(struct irq_data *d)
110{
111 plic_irq_toggle(cpu_possible_mask, d->hwirq, 0);
112}
113
114#ifdef CONFIG_SMP
115static int plic_set_affinity(struct irq_data *d,
116 const struct cpumask *mask_val, bool force)
117{
118 unsigned int cpu;
119
120 if (force)
121 cpu = cpumask_first(mask_val);
122 else
123 cpu = cpumask_any_and(mask_val, cpu_online_mask);
124
125 if (cpu >= nr_cpu_ids)
126 return -EINVAL;
127
128 plic_irq_toggle(cpu_possible_mask, d->hwirq, 0);
129 plic_irq_toggle(cpumask_of(cpu), d->hwirq, 1);
130
131 irq_data_update_effective_affinity(d, cpumask_of(cpu));
132
133 return IRQ_SET_MASK_OK_DONE;
134}
135#endif
136
137static void plic_irq_eoi(struct irq_data *d)
138{
139 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
140
141 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
142}
143
144static struct irq_chip plic_chip = {
145 .name = "SiFive PLIC",
146 .irq_mask = plic_irq_mask,
147 .irq_unmask = plic_irq_unmask,
148 .irq_eoi = plic_irq_eoi,
149#ifdef CONFIG_SMP
150 .irq_set_affinity = plic_set_affinity,
151#endif
152};
153
154static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
155 irq_hw_number_t hwirq)
156{
157 irq_set_chip_and_handler(irq, &plic_chip, handle_fasteoi_irq);
158 irq_set_chip_data(irq, NULL);
159 irq_set_noprobe(irq);
160 return 0;
161}
162
163static const struct irq_domain_ops plic_irqdomain_ops = {
164 .map = plic_irqdomain_map,
165 .xlate = irq_domain_xlate_onecell,
166};
167
168static struct irq_domain *plic_irqdomain;
169
170/*
171 * Handling an interrupt is a two-step process: first you claim the interrupt
172 * by reading the claim register, then you complete the interrupt by writing
173 * that source ID back to the same claim register. This automatically enables
174 * and disables the interrupt, so there's nothing else to do.
175 */
176static void plic_handle_irq(struct pt_regs *regs)
177{
178 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
179 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
180 irq_hw_number_t hwirq;
181
182 WARN_ON_ONCE(!handler->present);
183
184 csr_clear(sie, SIE_SEIE);
185 while ((hwirq = readl(claim))) {
186 int irq = irq_find_mapping(plic_irqdomain, hwirq);
187
188 if (unlikely(irq <= 0))
189 pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
190 hwirq);
191 else
192 generic_handle_irq(irq);
193 }
194 csr_set(sie, SIE_SEIE);
195}
196
197/*
198 * Walk up the DT tree until we find an active RISC-V core (HART) node and
199 * extract the cpuid from it.
200 */
201static int plic_find_hart_id(struct device_node *node)
202{
203 for (; node; node = node->parent) {
204 if (of_device_is_compatible(node, "riscv"))
205 return riscv_of_processor_hartid(node);
206 }
207
208 return -1;
209}
210
211static int __init plic_init(struct device_node *node,
212 struct device_node *parent)
213{
214 int error = 0, nr_contexts, nr_handlers = 0, i;
215 u32 nr_irqs;
216
217 if (plic_regs) {
218 pr_warn("PLIC already present.\n");
219 return -ENXIO;
220 }
221
222 plic_regs = of_iomap(node, 0);
223 if (WARN_ON(!plic_regs))
224 return -EIO;
225
226 error = -EINVAL;
227 of_property_read_u32(node, "riscv,ndev", &nr_irqs);
228 if (WARN_ON(!nr_irqs))
229 goto out_iounmap;
230
231 nr_contexts = of_irq_count(node);
232 if (WARN_ON(!nr_contexts))
233 goto out_iounmap;
234 if (WARN_ON(nr_contexts < num_possible_cpus()))
235 goto out_iounmap;
236
237 error = -ENOMEM;
238 plic_irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
239 &plic_irqdomain_ops, NULL);
240 if (WARN_ON(!plic_irqdomain))
241 goto out_iounmap;
242
243 for (i = 0; i < nr_contexts; i++) {
244 struct of_phandle_args parent;
245 struct plic_handler *handler;
246 irq_hw_number_t hwirq;
247 int cpu, hartid;
248 u32 threshold = 0;
249
250 if (of_irq_parse_one(node, i, &parent)) {
251 pr_err("failed to parse parent for context %d.\n", i);
252 continue;
253 }
254
255 /* skip contexts other than supervisor external interrupt */
256 if (parent.args[0] != IRQ_S_EXT)
257 continue;
258
259 hartid = plic_find_hart_id(parent.np);
260 if (hartid < 0) {
261 pr_warn("failed to parse hart ID for context %d.\n", i);
262 continue;
263 }
264
265 cpu = riscv_hartid_to_cpuid(hartid);
266 if (cpu < 0) {
267 pr_warn("Invalid cpuid for context %d\n", i);
268 continue;
269 }
270
271 /*
272 * When running in M-mode we need to ignore the S-mode handler.
273 * Here we assume it always comes later, but that might be a
274 * little fragile.
275 */
276 handler = per_cpu_ptr(&plic_handlers, cpu);
277 if (handler->present) {
278 pr_warn("handler already present for context %d.\n", i);
279 threshold = 0xffffffff;
280 goto done;
281 }
282
283 handler->present = true;
284 handler->hart_base =
285 plic_regs + CONTEXT_BASE + i * CONTEXT_PER_HART;
286 raw_spin_lock_init(&handler->enable_lock);
287 handler->enable_base =
288 plic_regs + ENABLE_BASE + i * ENABLE_PER_HART;
289
290done:
291 /* priority must be > threshold to trigger an interrupt */
292 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
293 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
294 plic_toggle(handler, hwirq, 0);
295 nr_handlers++;
296 }
297
298 pr_info("mapped %d interrupts with %d handlers for %d contexts.\n",
299 nr_irqs, nr_handlers, nr_contexts);
300 set_handle_irq(plic_handle_irq);
301 return 0;
302
303out_iounmap:
304 iounmap(plic_regs);
305 return error;
306}
307
308IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
309IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */