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>
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
65 struct irq_domain *irqdomain;
71 void __iomem *hart_base;
73 * Protect mask operations on the registers given that we can't
74 * assume atomic memory operations work on them.
76 raw_spinlock_t enable_lock;
77 void __iomem *enable_base;
78 struct plic_priv *priv;
80 static int plic_parent_irq __ro_after_init;
81 static bool plic_cpuhp_setup_done __ro_after_init;
82 static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
84 static void __plic_toggle(void __iomem *enable_base, int hwirq, int enable)
86 u32 __iomem *reg = enable_base + (hwirq / 32) * sizeof(u32);
87 u32 hwirq_mask = 1 << (hwirq % 32);
90 writel(readl(reg) | hwirq_mask, reg);
92 writel(readl(reg) & ~hwirq_mask, reg);
95 static void plic_toggle(struct plic_handler *handler, int hwirq, int enable)
97 raw_spin_lock(&handler->enable_lock);
98 __plic_toggle(handler->enable_base, hwirq, enable);
99 raw_spin_unlock(&handler->enable_lock);
102 static inline void plic_irq_toggle(const struct cpumask *mask,
103 struct irq_data *d, int enable)
106 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
108 writel(enable, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
109 for_each_cpu(cpu, mask) {
110 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
112 if (handler->present &&
113 cpumask_test_cpu(cpu, &handler->priv->lmask))
114 plic_toggle(handler, d->hwirq, enable);
118 static void plic_irq_unmask(struct irq_data *d)
120 struct cpumask amask;
122 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
124 cpumask_and(&amask, &priv->lmask, cpu_online_mask);
125 cpu = cpumask_any_and(irq_data_get_affinity_mask(d),
127 if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
129 plic_irq_toggle(cpumask_of(cpu), d, 1);
132 static void plic_irq_mask(struct irq_data *d)
134 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
136 plic_irq_toggle(&priv->lmask, d, 0);
140 static int plic_set_affinity(struct irq_data *d,
141 const struct cpumask *mask_val, bool force)
144 struct cpumask amask;
145 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
147 cpumask_and(&amask, &priv->lmask, mask_val);
150 cpu = cpumask_first(&amask);
152 cpu = cpumask_any_and(&amask, cpu_online_mask);
154 if (cpu >= nr_cpu_ids)
157 plic_irq_toggle(&priv->lmask, d, 0);
158 plic_irq_toggle(cpumask_of(cpu), d, !irqd_irq_masked(d));
160 irq_data_update_effective_affinity(d, cpumask_of(cpu));
162 return IRQ_SET_MASK_OK_DONE;
166 static void plic_irq_eoi(struct irq_data *d)
168 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
170 if (irqd_irq_masked(d)) {
172 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
175 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
179 static struct irq_chip plic_chip = {
180 .name = "SiFive PLIC",
181 .irq_mask = plic_irq_mask,
182 .irq_unmask = plic_irq_unmask,
183 .irq_eoi = plic_irq_eoi,
185 .irq_set_affinity = plic_set_affinity,
189 static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
190 irq_hw_number_t hwirq)
192 struct plic_priv *priv = d->host_data;
194 irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
195 handle_fasteoi_irq, NULL, NULL);
196 irq_set_noprobe(irq);
197 irq_set_affinity(irq, &priv->lmask);
201 static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
202 unsigned int nr_irqs, void *arg)
205 irq_hw_number_t hwirq;
207 struct irq_fwspec *fwspec = arg;
209 ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
213 for (i = 0; i < nr_irqs; i++) {
214 ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
222 static const struct irq_domain_ops plic_irqdomain_ops = {
223 .translate = irq_domain_translate_onecell,
224 .alloc = plic_irq_domain_alloc,
225 .free = irq_domain_free_irqs_top,
229 * Handling an interrupt is a two-step process: first you claim the interrupt
230 * by reading the claim register, then you complete the interrupt by writing
231 * that source ID back to the same claim register. This automatically enables
232 * and disables the interrupt, so there's nothing else to do.
234 static void plic_handle_irq(struct irq_desc *desc)
236 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
237 struct irq_chip *chip = irq_desc_get_chip(desc);
238 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
239 irq_hw_number_t hwirq;
241 WARN_ON_ONCE(!handler->present);
243 chained_irq_enter(chip, desc);
245 while ((hwirq = readl(claim))) {
246 int err = generic_handle_domain_irq(handler->priv->irqdomain,
249 pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
253 chained_irq_exit(chip, desc);
256 static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
258 /* priority must be > threshold to trigger an interrupt */
259 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
262 static int plic_dying_cpu(unsigned int cpu)
265 disable_percpu_irq(plic_parent_irq);
270 static int plic_starting_cpu(unsigned int cpu)
272 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
275 enable_percpu_irq(plic_parent_irq,
276 irq_get_trigger_type(plic_parent_irq));
278 pr_warn("cpu%d: parent irq not available\n", cpu);
279 plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
284 static int __init plic_init(struct device_node *node,
285 struct device_node *parent)
287 int error = 0, nr_contexts, nr_handlers = 0, i;
289 struct plic_priv *priv;
290 struct plic_handler *handler;
292 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
296 priv->regs = of_iomap(node, 0);
297 if (WARN_ON(!priv->regs)) {
303 of_property_read_u32(node, "riscv,ndev", &nr_irqs);
304 if (WARN_ON(!nr_irqs))
307 nr_contexts = of_irq_count(node);
308 if (WARN_ON(!nr_contexts))
312 priv->irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
313 &plic_irqdomain_ops, priv);
314 if (WARN_ON(!priv->irqdomain))
317 for (i = 0; i < nr_contexts; i++) {
318 struct of_phandle_args parent;
319 irq_hw_number_t hwirq;
322 if (of_irq_parse_one(node, i, &parent)) {
323 pr_err("failed to parse parent for context %d.\n", i);
328 * Skip contexts other than external interrupts for our
331 if (parent.args[0] != RV_IRQ_EXT) {
332 /* Disable S-mode enable bits if running in M-mode. */
333 if (IS_ENABLED(CONFIG_RISCV_M_MODE)) {
334 void __iomem *enable_base = priv->regs +
335 CONTEXT_ENABLE_BASE +
336 i * CONTEXT_ENABLE_SIZE;
338 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
339 __plic_toggle(enable_base, hwirq, 0);
344 hartid = riscv_of_parent_hartid(parent.np);
346 pr_warn("failed to parse hart ID for context %d.\n", i);
350 cpu = riscv_hartid_to_cpuid(hartid);
352 pr_warn("Invalid cpuid for context %d\n", i);
356 /* Find parent domain and register chained handler */
357 if (!plic_parent_irq && irq_find_host(parent.np)) {
358 plic_parent_irq = irq_of_parse_and_map(node, i);
360 irq_set_chained_handler(plic_parent_irq,
365 * When running in M-mode we need to ignore the S-mode handler.
366 * Here we assume it always comes later, but that might be a
369 handler = per_cpu_ptr(&plic_handlers, cpu);
370 if (handler->present) {
371 pr_warn("handler already present for context %d.\n", i);
372 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
376 cpumask_set_cpu(cpu, &priv->lmask);
377 handler->present = true;
378 handler->hart_base = priv->regs + CONTEXT_BASE +
380 raw_spin_lock_init(&handler->enable_lock);
381 handler->enable_base = priv->regs + CONTEXT_ENABLE_BASE +
382 i * CONTEXT_ENABLE_SIZE;
383 handler->priv = priv;
385 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
386 plic_toggle(handler, hwirq, 0);
391 * We can have multiple PLIC instances so setup cpuhp state only
392 * when context handler for current/boot CPU is present.
394 handler = this_cpu_ptr(&plic_handlers);
395 if (handler->present && !plic_cpuhp_setup_done) {
396 cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
397 "irqchip/sifive/plic:starting",
398 plic_starting_cpu, plic_dying_cpu);
399 plic_cpuhp_setup_done = true;
402 pr_info("%pOFP: mapped %d interrupts with %d handlers for"
403 " %d contexts.\n", node, nr_irqs, nr_handlers, nr_contexts);
413 IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
414 IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */
415 IRQCHIP_DECLARE(thead_c900_plic, "thead,c900-plic", plic_init); /* for firmware driver */