xref: /linux/drivers/irqchip/irq-sifive-plic.c (revision 5919f587ea37bf59a6fd7a2b91aef7b45c626343)
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) "riscv-plic: " fmt
7 #include <linux/acpi.h>
8 #include <linux/cpu.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/irq.h>
12 #include <linux/irqchip.h>
13 #include <linux/irqchip/chained_irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/platform_device.h>
20 #include <linux/spinlock.h>
21 #include <linux/syscore_ops.h>
22 #include <asm/smp.h>
23 
24 /*
25  * This driver implements a version of the RISC-V PLIC with the actual layout
26  * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
27  *
28  *     https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
29  *
30  * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
31  * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
32  * Spec.
33  */
34 
35 #define MAX_DEVICES			1024
36 #define MAX_CONTEXTS			15872
37 
38 /*
39  * Each interrupt source has a priority register associated with it.
40  * We always hardwire it to one in Linux.
41  */
42 #define PRIORITY_BASE			0
43 #define     PRIORITY_PER_ID		4
44 
45 /*
46  * Each hart context has a vector of interrupt enable bits associated with it.
47  * There's one bit for each interrupt source.
48  */
49 #define CONTEXT_ENABLE_BASE		0x2000
50 #define     CONTEXT_ENABLE_SIZE		0x80
51 
52 #define PENDING_BASE			0x1000
53 
54 /*
55  * Each hart context has a set of control registers associated with it.  Right
56  * now there's only two: a source priority threshold over which the hart will
57  * take an interrupt, and a register to claim interrupts.
58  */
59 #define CONTEXT_BASE			0x200000
60 #define     CONTEXT_SIZE		0x1000
61 #define     CONTEXT_THRESHOLD		0x00
62 #define     CONTEXT_CLAIM		0x04
63 
64 #define	PLIC_DISABLE_THRESHOLD		0x7
65 #define	PLIC_ENABLE_THRESHOLD		0
66 
67 #define PLIC_QUIRK_EDGE_INTERRUPT	0
68 #define PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM	1
69 
70 struct plic_priv {
71 	struct fwnode_handle	*fwnode;
72 	struct cpumask		lmask;
73 	struct irq_domain	*irqdomain;
74 	void __iomem		*regs;
75 	unsigned long		plic_quirks;
76 	/* device interrupts + 1 to compensate for the reserved hwirq 0 */
77 	unsigned int __private	total_irqs;
78 	unsigned int		irq_groups;
79 	unsigned long		*prio_save;
80 	u32			gsi_base;
81 	int			acpi_plic_id;
82 };
83 
84 struct plic_handler {
85 	bool			present;
86 	void __iomem		*hart_base;
87 	/*
88 	 * Protect mask operations on the registers given that we can't
89 	 * assume atomic memory operations work on them.
90 	 */
91 	raw_spinlock_t		enable_lock;
92 	void __iomem		*enable_base;
93 	u32			*enable_save;
94 	struct plic_priv	*priv;
95 };
96 
97 /*
98  * Macro to deal with the insanity of hardware interrupt 0 being reserved */
99 #define for_each_device_irq(iter, priv)	\
100 	for (unsigned int iter = 1; iter < ACCESS_PRIVATE(priv, total_irqs); iter++)
101 
102 static int plic_parent_irq __ro_after_init;
103 static bool plic_global_setup_done __ro_after_init;
104 static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
105 
106 static int plic_irq_set_type(struct irq_data *d, unsigned int type);
107 
108 static void __plic_toggle(struct plic_handler *handler, int hwirq, int enable)
109 {
110 	u32 __iomem *base = handler->enable_base;
111 	u32 hwirq_mask = BIT(hwirq % 32);
112 	int group = hwirq / 32;
113 	u32 value;
114 
115 	value = readl(base + group);
116 
117 	if (enable)
118 		value |= hwirq_mask;
119 	else
120 		value &= ~hwirq_mask;
121 
122 	handler->enable_save[group] = value;
123 	writel(value, base + group);
124 }
125 
126 static void plic_toggle(struct plic_handler *handler, int hwirq, int enable)
127 {
128 	unsigned long flags;
129 
130 	raw_spin_lock_irqsave(&handler->enable_lock, flags);
131 	__plic_toggle(handler, hwirq, enable);
132 	raw_spin_unlock_irqrestore(&handler->enable_lock, flags);
133 }
134 
135 static inline void plic_irq_toggle(const struct cpumask *mask,
136 				   struct irq_data *d, int enable)
137 {
138 	int cpu;
139 
140 	for_each_cpu(cpu, mask) {
141 		struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
142 
143 		plic_toggle(handler, d->hwirq, enable);
144 	}
145 }
146 
147 static void plic_irq_unmask(struct irq_data *d)
148 {
149 	struct plic_priv *priv = irq_data_get_irq_chip_data(d);
150 
151 	writel(1, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
152 }
153 
154 static void plic_irq_mask(struct irq_data *d)
155 {
156 	struct plic_priv *priv = irq_data_get_irq_chip_data(d);
157 
158 	writel(0, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
159 }
160 
161 static void plic_irq_enable(struct irq_data *d)
162 {
163 	plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
164 	plic_irq_unmask(d);
165 }
166 
167 static void plic_irq_disable(struct irq_data *d)
168 {
169 	plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
170 }
171 
172 static void plic_irq_eoi(struct irq_data *d)
173 {
174 	struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
175 	u32 __iomem *reg;
176 	bool enabled;
177 
178 	reg = handler->enable_base + (d->hwirq / 32) * sizeof(u32);
179 	enabled = readl(reg) & BIT(d->hwirq % 32);
180 
181 	if (unlikely(!enabled)) {
182 		plic_toggle(handler, d->hwirq, 1);
183 		writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
184 		plic_toggle(handler, d->hwirq, 0);
185 	} else {
186 		writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
187 	}
188 }
189 
190 #ifdef CONFIG_SMP
191 static int plic_set_affinity(struct irq_data *d,
192 			     const struct cpumask *mask_val, bool force)
193 {
194 	unsigned int cpu;
195 	struct plic_priv *priv = irq_data_get_irq_chip_data(d);
196 
197 	if (force)
198 		cpu = cpumask_first_and(&priv->lmask, mask_val);
199 	else
200 		cpu = cpumask_first_and_and(&priv->lmask, mask_val, cpu_online_mask);
201 
202 	if (cpu >= nr_cpu_ids)
203 		return -EINVAL;
204 
205 	/* Invalidate the original routing entry */
206 	plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
207 
208 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
209 
210 	/* Setting the new routing entry if irq is enabled */
211 	if (!irqd_irq_disabled(d))
212 		plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
213 
214 	return IRQ_SET_MASK_OK_DONE;
215 }
216 #endif
217 
218 static struct irq_chip plic_edge_chip = {
219 	.name		= "SiFive PLIC",
220 	.irq_enable	= plic_irq_enable,
221 	.irq_disable	= plic_irq_disable,
222 	.irq_ack	= plic_irq_eoi,
223 	.irq_mask	= plic_irq_mask,
224 	.irq_unmask	= plic_irq_unmask,
225 #ifdef CONFIG_SMP
226 	.irq_set_affinity = plic_set_affinity,
227 #endif
228 	.irq_set_type	= plic_irq_set_type,
229 	.flags		= IRQCHIP_SKIP_SET_WAKE |
230 			  IRQCHIP_AFFINITY_PRE_STARTUP,
231 };
232 
233 static struct irq_chip plic_chip = {
234 	.name		= "SiFive PLIC",
235 	.irq_enable	= plic_irq_enable,
236 	.irq_disable	= plic_irq_disable,
237 	.irq_mask	= plic_irq_mask,
238 	.irq_unmask	= plic_irq_unmask,
239 	.irq_eoi	= plic_irq_eoi,
240 #ifdef CONFIG_SMP
241 	.irq_set_affinity = plic_set_affinity,
242 #endif
243 	.irq_set_type	= plic_irq_set_type,
244 	.flags		= IRQCHIP_SKIP_SET_WAKE |
245 			  IRQCHIP_AFFINITY_PRE_STARTUP,
246 };
247 
248 static int plic_irq_set_type(struct irq_data *d, unsigned int type)
249 {
250 	struct plic_priv *priv = irq_data_get_irq_chip_data(d);
251 
252 	if (!test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
253 		return IRQ_SET_MASK_OK_NOCOPY;
254 
255 	switch (type) {
256 	case IRQ_TYPE_EDGE_RISING:
257 		irq_set_chip_handler_name_locked(d, &plic_edge_chip,
258 						 handle_edge_irq, NULL);
259 		break;
260 	case IRQ_TYPE_LEVEL_HIGH:
261 		irq_set_chip_handler_name_locked(d, &plic_chip,
262 						 handle_fasteoi_irq, NULL);
263 		break;
264 	default:
265 		return -EINVAL;
266 	}
267 
268 	return IRQ_SET_MASK_OK;
269 }
270 
271 static int plic_irq_suspend(void *data)
272 {
273 	struct plic_priv *priv = this_cpu_ptr(&plic_handlers)->priv;
274 
275 	for_each_device_irq(irq, priv) {
276 		__assign_bit(irq, priv->prio_save,
277 			     readl(priv->regs + PRIORITY_BASE + irq * PRIORITY_PER_ID));
278 	}
279 
280 	return 0;
281 }
282 
283 static void plic_irq_resume(void *data)
284 {
285 	struct plic_priv *priv = this_cpu_ptr(&plic_handlers)->priv;
286 	unsigned int index, cpu;
287 	unsigned long flags;
288 	u32 __iomem *reg;
289 
290 	for_each_device_irq(irq, priv) {
291 		index = BIT_WORD(irq);
292 		writel((priv->prio_save[index] & BIT_MASK(irq)) ? 1 : 0,
293 		       priv->regs + PRIORITY_BASE + irq * PRIORITY_PER_ID);
294 	}
295 
296 	for_each_present_cpu(cpu) {
297 		struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
298 
299 		if (!handler->present)
300 			continue;
301 
302 		raw_spin_lock_irqsave(&handler->enable_lock, flags);
303 		for (unsigned int i = 0; i < priv->irq_groups; i++) {
304 			reg = handler->enable_base + i * sizeof(u32);
305 			writel(handler->enable_save[i], reg);
306 		}
307 		raw_spin_unlock_irqrestore(&handler->enable_lock, flags);
308 	}
309 }
310 
311 static const struct syscore_ops plic_irq_syscore_ops = {
312 	.suspend	= plic_irq_suspend,
313 	.resume		= plic_irq_resume,
314 };
315 
316 static struct syscore plic_irq_syscore = {
317 	.ops = &plic_irq_syscore_ops,
318 };
319 
320 static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
321 			      irq_hw_number_t hwirq)
322 {
323 	struct plic_priv *priv = d->host_data;
324 
325 	irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
326 			    handle_fasteoi_irq, NULL, NULL);
327 	irq_set_noprobe(irq);
328 	irq_set_affinity(irq, &priv->lmask);
329 	return 0;
330 }
331 
332 static int plic_irq_domain_translate(struct irq_domain *d,
333 				     struct irq_fwspec *fwspec,
334 				     unsigned long *hwirq,
335 				     unsigned int *type)
336 {
337 	struct plic_priv *priv = d->host_data;
338 
339 	/* For DT, gsi_base is always zero. */
340 	if (fwspec->param[0] >= priv->gsi_base)
341 		fwspec->param[0] = fwspec->param[0] - priv->gsi_base;
342 
343 	if (test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
344 		return irq_domain_translate_twocell(d, fwspec, hwirq, type);
345 
346 	return irq_domain_translate_onecell(d, fwspec, hwirq, type);
347 }
348 
349 static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
350 				 unsigned int nr_irqs, void *arg)
351 {
352 	int i, ret;
353 	irq_hw_number_t hwirq;
354 	unsigned int type;
355 	struct irq_fwspec *fwspec = arg;
356 
357 	ret = plic_irq_domain_translate(domain, fwspec, &hwirq, &type);
358 	if (ret)
359 		return ret;
360 
361 	for (i = 0; i < nr_irqs; i++) {
362 		ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
363 		if (ret)
364 			return ret;
365 	}
366 
367 	return 0;
368 }
369 
370 static const struct irq_domain_ops plic_irqdomain_ops = {
371 	.translate	= plic_irq_domain_translate,
372 	.alloc		= plic_irq_domain_alloc,
373 	.free		= irq_domain_free_irqs_top,
374 };
375 
376 /*
377  * Handling an interrupt is a two-step process: first you claim the interrupt
378  * by reading the claim register, then you complete the interrupt by writing
379  * that source ID back to the same claim register.  This automatically enables
380  * and disables the interrupt, so there's nothing else to do.
381  */
382 static void plic_handle_irq(struct irq_desc *desc)
383 {
384 	struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
385 	struct irq_chip *chip = irq_desc_get_chip(desc);
386 	void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
387 	irq_hw_number_t hwirq;
388 
389 	WARN_ON_ONCE(!handler->present);
390 
391 	chained_irq_enter(chip, desc);
392 
393 	while ((hwirq = readl(claim))) {
394 		int err = generic_handle_domain_irq(handler->priv->irqdomain,
395 						    hwirq);
396 		if (unlikely(err)) {
397 			pr_warn_ratelimited("%pfwP: can't find mapping for hwirq %lu\n",
398 					    handler->priv->fwnode, hwirq);
399 		}
400 	}
401 
402 	chained_irq_exit(chip, desc);
403 }
404 
405 static u32 cp100_isolate_pending_irq(int nr_irq_groups, struct plic_handler *handler)
406 {
407 	u32 __iomem *pending = handler->priv->regs + PENDING_BASE;
408 	u32 __iomem *enable = handler->enable_base;
409 	u32 pending_irqs = 0;
410 	int i, j;
411 
412 	/* Look for first pending interrupt */
413 	for (i = 0; i < nr_irq_groups; i++) {
414 		/* Any pending interrupts would be annihilated, so skip checking them */
415 		if (!handler->enable_save[i])
416 			continue;
417 
418 		pending_irqs = handler->enable_save[i] & readl_relaxed(pending + i);
419 		if (pending_irqs)
420 			break;
421 	}
422 
423 	if (!pending_irqs)
424 		return 0;
425 
426 	/* Isolate lowest set bit */
427 	pending_irqs &= -pending_irqs;
428 
429 	/* Disable all interrupts but the first pending one */
430 	for (j = 0; j < nr_irq_groups; j++) {
431 		u32 new_mask = j == i ? pending_irqs : 0;
432 
433 		if (new_mask != handler->enable_save[j])
434 			writel_relaxed(new_mask, enable + j);
435 	}
436 	return pending_irqs;
437 }
438 
439 static irq_hw_number_t cp100_get_hwirq(struct plic_handler *handler, void __iomem *claim)
440 {
441 	int nr_irq_groups = handler->priv->irq_groups;
442 	u32 __iomem *enable = handler->enable_base;
443 	irq_hw_number_t hwirq = 0;
444 	u32 iso_mask;
445 	int i;
446 
447 	guard(raw_spinlock)(&handler->enable_lock);
448 
449 	/* Existing enable state is already cached in enable_save */
450 	iso_mask = cp100_isolate_pending_irq(nr_irq_groups, handler);
451 	if (!iso_mask)
452 		return 0;
453 
454 	/*
455 	 * Interrupts delivered to hardware still become pending, but only
456 	 * interrupts that are both pending and enabled can be claimed.
457 	 * Clearing the enable bit for all interrupts but the first pending
458 	 * one avoids a hardware bug that occurs during read from the claim
459 	 * register with more than one eligible interrupt.
460 	 */
461 	hwirq = readl(claim);
462 
463 	/* Restore previous state */
464 	for (i = 0; i < nr_irq_groups; i++) {
465 		u32 written = i == hwirq / 32 ? iso_mask : 0;
466 		u32 stored = handler->enable_save[i];
467 
468 		if (stored != written)
469 			writel_relaxed(stored, enable + i);
470 	}
471 	return hwirq;
472 }
473 
474 static void plic_handle_irq_cp100(struct irq_desc *desc)
475 {
476 	struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
477 	struct irq_chip *chip = irq_desc_get_chip(desc);
478 	void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
479 	irq_hw_number_t hwirq;
480 
481 	WARN_ON_ONCE(!handler->present);
482 
483 	chained_irq_enter(chip, desc);
484 
485 	while ((hwirq = cp100_get_hwirq(handler, claim))) {
486 		int err = generic_handle_domain_irq(handler->priv->irqdomain, hwirq);
487 
488 		if (unlikely(err)) {
489 			pr_warn_ratelimited("%pfwP: can't find mapping for hwirq %lu\n",
490 					    handler->priv->fwnode, hwirq);
491 		}
492 	}
493 
494 	chained_irq_exit(chip, desc);
495 }
496 
497 static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
498 {
499 	/* priority must be > threshold to trigger an interrupt */
500 	writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
501 }
502 
503 static int plic_dying_cpu(unsigned int cpu)
504 {
505 	if (plic_parent_irq)
506 		disable_percpu_irq(plic_parent_irq);
507 
508 	return 0;
509 }
510 
511 static int plic_starting_cpu(unsigned int cpu)
512 {
513 	struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
514 
515 	if (plic_parent_irq)
516 		enable_percpu_irq(plic_parent_irq,
517 				  irq_get_trigger_type(plic_parent_irq));
518 	else
519 		pr_warn("%pfwP: cpu%d: parent irq not available\n",
520 			handler->priv->fwnode, cpu);
521 	plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
522 
523 	return 0;
524 }
525 
526 static const struct of_device_id plic_match[] = {
527 	{ .compatible = "sifive,plic-1.0.0" },
528 	{ .compatible = "riscv,plic0" },
529 	{ .compatible = "andestech,nceplic100",
530 	  .data = (const void *)BIT(PLIC_QUIRK_EDGE_INTERRUPT) },
531 	{ .compatible = "thead,c900-plic",
532 	  .data = (const void *)BIT(PLIC_QUIRK_EDGE_INTERRUPT) },
533 	{ .compatible = "ultrarisc,cp100-plic",
534 	  .data = (const void *)BIT(PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM) },
535 	{}
536 };
537 
538 #ifdef CONFIG_ACPI
539 
540 static const struct acpi_device_id plic_acpi_match[] = {
541 	{ .id = "RSCV0001" },
542 	{ }
543 };
544 MODULE_DEVICE_TABLE(acpi, plic_acpi_match);
545 
546 #endif
547 static int plic_parse_nr_irqs_and_contexts(struct fwnode_handle *fwnode,
548 					   u32 *nr_irqs, u32 *nr_contexts,
549 					   u32 *gsi_base, u32 *id)
550 {
551 	int rc;
552 
553 	if (!is_of_node(fwnode)) {
554 		rc = riscv_acpi_get_gsi_info(fwnode, gsi_base, id, nr_irqs, NULL);
555 		if (rc) {
556 			pr_err("%pfwP: failed to find GSI mapping\n", fwnode);
557 			return rc;
558 		}
559 
560 		*nr_contexts = acpi_rintc_get_plic_nr_contexts(*id);
561 		if (WARN_ON(!*nr_contexts)) {
562 			pr_err("%pfwP: no PLIC context available\n", fwnode);
563 			return -EINVAL;
564 		}
565 
566 		return 0;
567 	}
568 
569 	rc = of_property_read_u32(to_of_node(fwnode), "riscv,ndev", nr_irqs);
570 	if (rc) {
571 		pr_err("%pfwP: riscv,ndev property not available\n", fwnode);
572 		return rc;
573 	}
574 
575 	*nr_contexts = of_irq_count(to_of_node(fwnode));
576 	if (WARN_ON(!(*nr_contexts))) {
577 		pr_err("%pfwP: no PLIC context available\n", fwnode);
578 		return -EINVAL;
579 	}
580 
581 	*gsi_base = 0;
582 	*id = 0;
583 
584 	return 0;
585 }
586 
587 static int plic_parse_context_parent(struct fwnode_handle *fwnode, u32 context,
588 				     u32 *parent_hwirq, int *parent_cpu, u32 id)
589 {
590 	struct of_phandle_args parent;
591 	unsigned long hartid;
592 	int rc;
593 
594 	if (!is_of_node(fwnode)) {
595 		hartid = acpi_rintc_ext_parent_to_hartid(id, context);
596 		if (hartid == INVALID_HARTID)
597 			return -EINVAL;
598 
599 		*parent_cpu = riscv_hartid_to_cpuid(hartid);
600 		*parent_hwirq = RV_IRQ_EXT;
601 		return 0;
602 	}
603 
604 	rc = of_irq_parse_one(to_of_node(fwnode), context, &parent);
605 	if (rc)
606 		return rc;
607 
608 	rc = riscv_of_parent_hartid(parent.np, &hartid);
609 	if (rc)
610 		return rc;
611 
612 	*parent_hwirq = parent.args[0];
613 	*parent_cpu = riscv_hartid_to_cpuid(hartid);
614 	return 0;
615 }
616 
617 static int plic_probe(struct fwnode_handle *fwnode)
618 {
619 	int error = 0, nr_contexts, nr_handlers = 0, cpu, i;
620 	unsigned long plic_quirks = 0;
621 	struct plic_handler *handler;
622 	u32 nr_irqs, parent_hwirq;
623 	struct plic_priv *priv;
624 	void __iomem *regs;
625 	int id, context_id;
626 	u32 gsi_base;
627 
628 	if (is_of_node(fwnode)) {
629 		const struct of_device_id *id;
630 
631 		id = of_match_node(plic_match, to_of_node(fwnode));
632 		if (id)
633 			plic_quirks = (unsigned long)id->data;
634 
635 		regs = of_iomap(to_of_node(fwnode), 0);
636 		if (!regs)
637 			return -ENOMEM;
638 	} else {
639 		regs = devm_platform_ioremap_resource(to_platform_device(fwnode->dev), 0);
640 		if (IS_ERR(regs))
641 			return PTR_ERR(regs);
642 	}
643 
644 	error = plic_parse_nr_irqs_and_contexts(fwnode, &nr_irqs, &nr_contexts, &gsi_base, &id);
645 	if (error)
646 		return error;
647 
648 	priv = kzalloc_obj(*priv);
649 	if (!priv)
650 		return -ENOMEM;
651 
652 	priv->fwnode = fwnode;
653 	priv->plic_quirks = plic_quirks;
654 	/*
655 	 * The firmware provides the number of device interrupts. As
656 	 * hardware interrupt 0 is reserved, the number of total interrupts
657 	 * is nr_irqs + 1.
658 	 */
659 	nr_irqs++;
660 	ACCESS_PRIVATE(priv, total_irqs) = nr_irqs;
661 	/* Precalculate the number of register groups */
662 	priv->irq_groups = DIV_ROUND_UP(nr_irqs, 32);
663 
664 	priv->regs = regs;
665 	priv->gsi_base = gsi_base;
666 	priv->acpi_plic_id = id;
667 
668 	priv->prio_save = bitmap_zalloc(nr_irqs, GFP_KERNEL);
669 	if (!priv->prio_save)
670 		return -ENOMEM;
671 
672 	for (i = 0; i < nr_contexts; i++) {
673 		error = plic_parse_context_parent(fwnode, i, &parent_hwirq, &cpu,
674 						  priv->acpi_plic_id);
675 		if (error) {
676 			pr_warn("%pfwP: hwirq for context%d not found\n", fwnode, i);
677 			continue;
678 		}
679 
680 		if (is_of_node(fwnode)) {
681 			context_id = i;
682 		} else {
683 			context_id = acpi_rintc_get_plic_context(priv->acpi_plic_id, i);
684 			if (context_id == INVALID_CONTEXT) {
685 				pr_warn("%pfwP: invalid context id for context%d\n", fwnode, i);
686 				continue;
687 			}
688 		}
689 
690 		/*
691 		 * Skip contexts other than external interrupts for our
692 		 * privilege level.
693 		 */
694 		if (parent_hwirq != RV_IRQ_EXT) {
695 			/* Disable S-mode enable bits if running in M-mode. */
696 			if (IS_ENABLED(CONFIG_RISCV_M_MODE)) {
697 				u32 __iomem *enable_base = priv->regs +	CONTEXT_ENABLE_BASE +
698 							   i * CONTEXT_ENABLE_SIZE;
699 
700 				for (int j = 0; j < priv->irq_groups; j++)
701 					writel(0, enable_base + j);
702 			}
703 			continue;
704 		}
705 
706 		if (cpu < 0) {
707 			pr_warn("%pfwP: Invalid cpuid for context %d\n", fwnode, i);
708 			continue;
709 		}
710 
711 		/*
712 		 * When running in M-mode we need to ignore the S-mode handler.
713 		 * Here we assume it always comes later, but that might be a
714 		 * little fragile.
715 		 */
716 		handler = per_cpu_ptr(&plic_handlers, cpu);
717 		if (handler->present) {
718 			pr_warn("%pfwP: handler already present for context %d.\n", fwnode, i);
719 			plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
720 			goto done;
721 		}
722 
723 		cpumask_set_cpu(cpu, &priv->lmask);
724 		handler->present = true;
725 		handler->hart_base = priv->regs + CONTEXT_BASE +
726 			context_id * CONTEXT_SIZE;
727 		raw_spin_lock_init(&handler->enable_lock);
728 		handler->enable_base = priv->regs + CONTEXT_ENABLE_BASE +
729 			context_id * CONTEXT_ENABLE_SIZE;
730 		handler->priv = priv;
731 
732 		handler->enable_save = kcalloc(priv->irq_groups, sizeof(*handler->enable_save),
733 					       GFP_KERNEL);
734 		if (!handler->enable_save)
735 			return -ENOMEM;
736 done:
737 		for_each_device_irq(hwirq, priv) {
738 			plic_toggle(handler, hwirq, 0);
739 			writel(1, priv->regs + PRIORITY_BASE + hwirq * PRIORITY_PER_ID);
740 		}
741 		nr_handlers++;
742 	}
743 
744 	priv->irqdomain = irq_domain_create_linear(fwnode, nr_irqs, &plic_irqdomain_ops, priv);
745 	if (!priv->irqdomain)
746 		return -ENOMEM;
747 
748 	/*
749 	 * We can have multiple PLIC instances so setup global state
750 	 * and register syscore operations only once after context
751 	 * handlers of all online CPUs are initialized.
752 	 */
753 	if (!plic_global_setup_done) {
754 		struct irq_domain *domain;
755 		bool global_setup = true;
756 
757 		for_each_online_cpu(cpu) {
758 			handler = per_cpu_ptr(&plic_handlers, cpu);
759 			if (!handler->present) {
760 				global_setup = false;
761 				break;
762 			}
763 		}
764 
765 		if (global_setup) {
766 			void (*handler_fn)(struct irq_desc *) = plic_handle_irq;
767 
768 			if (test_bit(PLIC_QUIRK_CP100_CLAIM_REGISTER_ERRATUM, &handler->priv->plic_quirks))
769 				handler_fn = plic_handle_irq_cp100;
770 
771 			/* Find parent domain and register chained handler */
772 			domain = irq_find_matching_fwnode(riscv_get_intc_hwnode(), DOMAIN_BUS_ANY);
773 			if (domain)
774 				plic_parent_irq = irq_create_mapping(domain, RV_IRQ_EXT);
775 			if (plic_parent_irq)
776 				irq_set_chained_handler(plic_parent_irq, handler_fn);
777 
778 			cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
779 					  "irqchip/sifive/plic:starting",
780 					  plic_starting_cpu, plic_dying_cpu);
781 			register_syscore(&plic_irq_syscore);
782 			plic_global_setup_done = true;
783 		}
784 	}
785 
786 #ifdef CONFIG_ACPI
787 	if (!acpi_disabled)
788 		acpi_dev_clear_dependencies(ACPI_COMPANION(fwnode->dev));
789 #endif
790 
791 	pr_info("%pfwP: mapped %d interrupts with %d handlers for %d contexts.\n",
792 		fwnode, nr_irqs, nr_handlers, nr_contexts);
793 	return 0;
794 }
795 
796 static int plic_platform_probe(struct platform_device *pdev)
797 {
798 	if (plic_probe(pdev->dev.fwnode))
799 		panic("%pfwP: Failed to initialize\n", pdev->dev.fwnode);
800 	return 0;
801 }
802 
803 static struct platform_driver plic_driver = {
804 	.driver = {
805 		.name		= "riscv-plic",
806 		.of_match_table	= plic_match,
807 		.suppress_bind_attrs = true,
808 		.acpi_match_table = ACPI_PTR(plic_acpi_match),
809 	},
810 	.probe = plic_platform_probe,
811 };
812 builtin_platform_driver(plic_driver);
813 
814 static int __init plic_early_probe(struct device_node *node,
815 				   struct device_node *parent)
816 {
817 	if (plic_probe(&node->fwnode))
818 		panic("%pfwP: Failed to initialize\n", &node->fwnode);
819 	return 0;
820 }
821 
822 IRQCHIP_DECLARE(riscv, "allwinner,sun20i-d1-plic", plic_early_probe);
823