blob: cf74cfa8204530b31ed5bcb2f38b4c0f09a62aee [file] [log] [blame]
Christoph Hellwig8237f8b2018-07-26 16:27:00 +02001// 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
Atish Patraccbe80b2020-03-02 15:11:45 -08007#include <linux/cpu.h>
Christoph Hellwig8237f8b2018-07-26 16:27:00 +02008#include <linux/interrupt.h>
9#include <linux/io.h>
10#include <linux/irq.h>
11#include <linux/irqchip.h>
Anup Patel6b7ce8922020-06-01 14:45:40 +053012#include <linux/irqchip/chained_irq.h>
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020013#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>
Atish Patraf99fb602018-10-02 12:15:05 -070020#include <asm/smp.h>
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020021
22/*
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:
25 *
26 * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
27 *
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
30 * Spec.
31 */
32
33#define MAX_DEVICES 1024
34#define MAX_CONTEXTS 15872
35
36/*
37 * Each interrupt source has a priority register associated with it.
38 * We always hardwire it to one in Linux.
39 */
40#define PRIORITY_BASE 0
41#define PRIORITY_PER_ID 4
42
43/*
44 * Each hart context has a vector of interrupt enable bits associated with it.
45 * There's one bit for each interrupt source.
46 */
47#define ENABLE_BASE 0x2000
48#define ENABLE_PER_HART 0x80
49
50/*
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.
54 */
55#define CONTEXT_BASE 0x200000
56#define CONTEXT_PER_HART 0x1000
57#define CONTEXT_THRESHOLD 0x00
58#define CONTEXT_CLAIM 0x04
59
Atish Patrad727be72020-04-02 18:46:09 -070060#define PLIC_DISABLE_THRESHOLD 0x7
Atish Patraccbe80b2020-03-02 15:11:45 -080061#define PLIC_ENABLE_THRESHOLD 0
62
Atish Patraf1ad1132020-03-02 15:11:46 -080063struct plic_priv {
64 struct cpumask lmask;
65 struct irq_domain *irqdomain;
66 void __iomem *regs;
67};
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020068
69struct plic_handler {
70 bool present;
Anup Patel86c7cbf2019-02-12 18:22:43 +053071 void __iomem *hart_base;
72 /*
73 * Protect mask operations on the registers given that we can't
74 * assume atomic memory operations work on them.
75 */
76 raw_spinlock_t enable_lock;
77 void __iomem *enable_base;
Atish Patraf1ad1132020-03-02 15:11:46 -080078 struct plic_priv *priv;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020079};
Jisheng Zhange03b7c12021-03-30 02:09:11 +080080static int plic_parent_irq __ro_after_init;
81static bool plic_cpuhp_setup_done __ro_after_init;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020082static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
83
Anup Patel86c7cbf2019-02-12 18:22:43 +053084static inline void plic_toggle(struct plic_handler *handler,
85 int hwirq, int enable)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020086{
Anup Patel86c7cbf2019-02-12 18:22:43 +053087 u32 __iomem *reg = handler->enable_base + (hwirq / 32) * sizeof(u32);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020088 u32 hwirq_mask = 1 << (hwirq % 32);
89
Anup Patel86c7cbf2019-02-12 18:22:43 +053090 raw_spin_lock(&handler->enable_lock);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020091 if (enable)
92 writel(readl(reg) | hwirq_mask, reg);
93 else
94 writel(readl(reg) & ~hwirq_mask, reg);
Anup Patel86c7cbf2019-02-12 18:22:43 +053095 raw_spin_unlock(&handler->enable_lock);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020096}
97
Anup Patelcc9f04f2019-02-12 18:22:46 +053098static inline void plic_irq_toggle(const struct cpumask *mask,
Atish Patraf1ad1132020-03-02 15:11:46 -080099 struct irq_data *d, int enable)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200100{
101 int cpu;
Greentime Huf9ac7bb2020-10-29 10:37:38 +0800102 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200103
Atish Patraf1ad1132020-03-02 15:11:46 -0800104 writel(enable, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
Anup Patelcc9f04f2019-02-12 18:22:46 +0530105 for_each_cpu(cpu, mask) {
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200106 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
107
Atish Patraf1ad1132020-03-02 15:11:46 -0800108 if (handler->present &&
109 cpumask_test_cpu(cpu, &handler->priv->lmask))
110 plic_toggle(handler, d->hwirq, enable);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200111 }
112}
113
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100114static void plic_irq_unmask(struct irq_data *d)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200115{
Atish Patraf1ad1132020-03-02 15:11:46 -0800116 struct cpumask amask;
117 unsigned int cpu;
Greentime Huf9ac7bb2020-10-29 10:37:38 +0800118 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
Atish Patraf1ad1132020-03-02 15:11:46 -0800119
120 cpumask_and(&amask, &priv->lmask, cpu_online_mask);
121 cpu = cpumask_any_and(irq_data_get_affinity_mask(d),
122 &amask);
Anup Patelcc9f04f2019-02-12 18:22:46 +0530123 if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
124 return;
Atish Patraf1ad1132020-03-02 15:11:46 -0800125 plic_irq_toggle(cpumask_of(cpu), d, 1);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200126}
127
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100128static void plic_irq_mask(struct irq_data *d)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200129{
Greentime Huf9ac7bb2020-10-29 10:37:38 +0800130 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
Atish Patraf1ad1132020-03-02 15:11:46 -0800131
132 plic_irq_toggle(&priv->lmask, d, 0);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200133}
134
Anup Patelcc9f04f2019-02-12 18:22:46 +0530135#ifdef CONFIG_SMP
136static int plic_set_affinity(struct irq_data *d,
137 const struct cpumask *mask_val, bool force)
138{
139 unsigned int cpu;
Atish Patraf1ad1132020-03-02 15:11:46 -0800140 struct cpumask amask;
Greentime Huf9ac7bb2020-10-29 10:37:38 +0800141 struct plic_priv *priv = irq_data_get_irq_chip_data(d);
Atish Patraf1ad1132020-03-02 15:11:46 -0800142
143 cpumask_and(&amask, &priv->lmask, mask_val);
Anup Patelcc9f04f2019-02-12 18:22:46 +0530144
145 if (force)
Atish Patraf1ad1132020-03-02 15:11:46 -0800146 cpu = cpumask_first(&amask);
Anup Patelcc9f04f2019-02-12 18:22:46 +0530147 else
Atish Patraf1ad1132020-03-02 15:11:46 -0800148 cpu = cpumask_any_and(&amask, cpu_online_mask);
Anup Patelcc9f04f2019-02-12 18:22:46 +0530149
150 if (cpu >= nr_cpu_ids)
151 return -EINVAL;
152
Atish Patraf1ad1132020-03-02 15:11:46 -0800153 plic_irq_toggle(&priv->lmask, d, 0);
Greentime Hua7480c52020-10-20 16:15:32 +0800154 plic_irq_toggle(cpumask_of(cpu), d, !irqd_irq_masked(d));
Anup Patelcc9f04f2019-02-12 18:22:46 +0530155
156 irq_data_update_effective_affinity(d, cpumask_of(cpu));
157
158 return IRQ_SET_MASK_OK_DONE;
159}
160#endif
161
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100162static void plic_irq_eoi(struct irq_data *d)
163{
164 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
165
166 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
167}
168
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200169static struct irq_chip plic_chip = {
170 .name = "SiFive PLIC",
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100171 .irq_mask = plic_irq_mask,
172 .irq_unmask = plic_irq_unmask,
173 .irq_eoi = plic_irq_eoi,
Anup Patelcc9f04f2019-02-12 18:22:46 +0530174#ifdef CONFIG_SMP
175 .irq_set_affinity = plic_set_affinity,
176#endif
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200177};
178
179static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
180 irq_hw_number_t hwirq)
181{
Anup Patel2458ed32020-05-18 14:44:39 +0530182 struct plic_priv *priv = d->host_data;
183
Yash Shah466008f2019-12-10 16:41:11 +0530184 irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
185 handle_fasteoi_irq, NULL, NULL);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200186 irq_set_noprobe(irq);
Anup Patel2458ed32020-05-18 14:44:39 +0530187 irq_set_affinity(irq, &priv->lmask);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200188 return 0;
189}
190
Yash Shah466008f2019-12-10 16:41:11 +0530191static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
192 unsigned int nr_irqs, void *arg)
193{
194 int i, ret;
195 irq_hw_number_t hwirq;
196 unsigned int type;
197 struct irq_fwspec *fwspec = arg;
198
199 ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
200 if (ret)
201 return ret;
202
203 for (i = 0; i < nr_irqs; i++) {
204 ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
205 if (ret)
206 return ret;
207 }
208
209 return 0;
210}
211
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200212static const struct irq_domain_ops plic_irqdomain_ops = {
Yash Shah466008f2019-12-10 16:41:11 +0530213 .translate = irq_domain_translate_onecell,
214 .alloc = plic_irq_domain_alloc,
215 .free = irq_domain_free_irqs_top,
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200216};
217
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200218/*
219 * Handling an interrupt is a two-step process: first you claim the interrupt
220 * by reading the claim register, then you complete the interrupt by writing
221 * that source ID back to the same claim register. This automatically enables
222 * and disables the interrupt, so there's nothing else to do.
223 */
Anup Patel6b7ce8922020-06-01 14:45:40 +0530224static void plic_handle_irq(struct irq_desc *desc)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200225{
226 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
Anup Patel6b7ce8922020-06-01 14:45:40 +0530227 struct irq_chip *chip = irq_desc_get_chip(desc);
Anup Patel86c7cbf2019-02-12 18:22:43 +0530228 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200229 irq_hw_number_t hwirq;
230
231 WARN_ON_ONCE(!handler->present);
232
Anup Patel6b7ce8922020-06-01 14:45:40 +0530233 chained_irq_enter(chip, desc);
234
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200235 while ((hwirq = readl(claim))) {
Marc Zyngier046a6ee2021-05-04 17:42:18 +0100236 int err = generic_handle_domain_irq(handler->priv->irqdomain,
237 hwirq);
238 if (unlikely(err))
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200239 pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
240 hwirq);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200241 }
Anup Patel6b7ce8922020-06-01 14:45:40 +0530242
243 chained_irq_exit(chip, desc);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200244}
245
Atish Patraccbe80b2020-03-02 15:11:45 -0800246static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
247{
248 /* priority must be > threshold to trigger an interrupt */
249 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
250}
251
252static int plic_dying_cpu(unsigned int cpu)
253{
Anup Patel6b7ce8922020-06-01 14:45:40 +0530254 if (plic_parent_irq)
255 disable_percpu_irq(plic_parent_irq);
Atish Patraccbe80b2020-03-02 15:11:45 -0800256
257 return 0;
258}
259
260static int plic_starting_cpu(unsigned int cpu)
261{
262 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
263
Anup Patel6b7ce8922020-06-01 14:45:40 +0530264 if (plic_parent_irq)
265 enable_percpu_irq(plic_parent_irq,
266 irq_get_trigger_type(plic_parent_irq));
267 else
268 pr_warn("cpu%d: parent irq not available\n", cpu);
Atish Patraccbe80b2020-03-02 15:11:45 -0800269 plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
270
271 return 0;
272}
273
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200274static int __init plic_init(struct device_node *node,
275 struct device_node *parent)
276{
Anup Patel6adfe8d2019-02-12 18:22:45 +0530277 int error = 0, nr_contexts, nr_handlers = 0, i;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200278 u32 nr_irqs;
Atish Patraf1ad1132020-03-02 15:11:46 -0800279 struct plic_priv *priv;
Anup Patel2234ae82020-05-18 14:44:40 +0530280 struct plic_handler *handler;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200281
Atish Patraf1ad1132020-03-02 15:11:46 -0800282 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
283 if (!priv)
284 return -ENOMEM;
285
286 priv->regs = of_iomap(node, 0);
287 if (WARN_ON(!priv->regs)) {
288 error = -EIO;
289 goto out_free_priv;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200290 }
291
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200292 error = -EINVAL;
293 of_property_read_u32(node, "riscv,ndev", &nr_irqs);
294 if (WARN_ON(!nr_irqs))
295 goto out_iounmap;
296
Anup Patel6adfe8d2019-02-12 18:22:45 +0530297 nr_contexts = of_irq_count(node);
298 if (WARN_ON(!nr_contexts))
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200299 goto out_iounmap;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200300
301 error = -ENOMEM;
Atish Patraf1ad1132020-03-02 15:11:46 -0800302 priv->irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
303 &plic_irqdomain_ops, priv);
304 if (WARN_ON(!priv->irqdomain))
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200305 goto out_iounmap;
306
Anup Patel6adfe8d2019-02-12 18:22:45 +0530307 for (i = 0; i < nr_contexts; i++) {
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200308 struct of_phandle_args parent;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200309 irq_hw_number_t hwirq;
Atish Patraf99fb602018-10-02 12:15:05 -0700310 int cpu, hartid;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200311
312 if (of_irq_parse_one(node, i, &parent)) {
313 pr_err("failed to parse parent for context %d.\n", i);
314 continue;
315 }
316
Christoph Hellwiga4c37332019-10-28 13:10:32 +0100317 /*
318 * Skip contexts other than external interrupts for our
319 * privilege level.
320 */
Paul Walmsley2f3035d2019-12-20 03:09:49 -0800321 if (parent.args[0] != RV_IRQ_EXT)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200322 continue;
323
Anup Pateld175d692020-06-01 14:45:39 +0530324 hartid = riscv_of_parent_hartid(parent.np);
Atish Patraf99fb602018-10-02 12:15:05 -0700325 if (hartid < 0) {
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200326 pr_warn("failed to parse hart ID for context %d.\n", i);
327 continue;
328 }
329
Atish Patraf99fb602018-10-02 12:15:05 -0700330 cpu = riscv_hartid_to_cpuid(hartid);
Atish Patrafc03aca2019-02-12 03:10:11 -0800331 if (cpu < 0) {
332 pr_warn("Invalid cpuid for context %d\n", i);
333 continue;
334 }
335
Anup Patel6b7ce8922020-06-01 14:45:40 +0530336 /* Find parent domain and register chained handler */
337 if (!plic_parent_irq && irq_find_host(parent.np)) {
338 plic_parent_irq = irq_of_parse_and_map(node, i);
339 if (plic_parent_irq)
340 irq_set_chained_handler(plic_parent_irq,
341 plic_handle_irq);
342 }
343
Christoph Hellwig9ce06492019-09-03 11:32:20 +0200344 /*
345 * When running in M-mode we need to ignore the S-mode handler.
346 * Here we assume it always comes later, but that might be a
347 * little fragile.
348 */
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200349 handler = per_cpu_ptr(&plic_handlers, cpu);
Anup Patel3fecb5a2019-02-12 18:22:44 +0530350 if (handler->present) {
351 pr_warn("handler already present for context %d.\n", i);
Atish Patraccbe80b2020-03-02 15:11:45 -0800352 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
Christoph Hellwig9ce06492019-09-03 11:32:20 +0200353 goto done;
Anup Patel3fecb5a2019-02-12 18:22:44 +0530354 }
355
Atish Patraf1ad1132020-03-02 15:11:46 -0800356 cpumask_set_cpu(cpu, &priv->lmask);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200357 handler->present = true;
Anup Patel86c7cbf2019-02-12 18:22:43 +0530358 handler->hart_base =
Atish Patraf1ad1132020-03-02 15:11:46 -0800359 priv->regs + CONTEXT_BASE + i * CONTEXT_PER_HART;
Anup Patel86c7cbf2019-02-12 18:22:43 +0530360 raw_spin_lock_init(&handler->enable_lock);
361 handler->enable_base =
Atish Patraf1ad1132020-03-02 15:11:46 -0800362 priv->regs + ENABLE_BASE + i * ENABLE_PER_HART;
363 handler->priv = priv;
Christoph Hellwig9ce06492019-09-03 11:32:20 +0200364done:
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200365 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
Anup Patel86c7cbf2019-02-12 18:22:43 +0530366 plic_toggle(handler, hwirq, 0);
Anup Patel6adfe8d2019-02-12 18:22:45 +0530367 nr_handlers++;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200368 }
369
Anup Patel2234ae82020-05-18 14:44:40 +0530370 /*
371 * We can have multiple PLIC instances so setup cpuhp state only
372 * when context handler for current/boot CPU is present.
373 */
374 handler = this_cpu_ptr(&plic_handlers);
375 if (handler->present && !plic_cpuhp_setup_done) {
376 cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
Atish Patraccbe80b2020-03-02 15:11:45 -0800377 "irqchip/sifive/plic:starting",
378 plic_starting_cpu, plic_dying_cpu);
Anup Patel2234ae82020-05-18 14:44:40 +0530379 plic_cpuhp_setup_done = true;
380 }
381
Anup Patel0e375f52020-05-18 14:44:41 +0530382 pr_info("%pOFP: mapped %d interrupts with %d handlers for"
383 " %d contexts.\n", node, nr_irqs, nr_handlers, nr_contexts);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200384 return 0;
385
386out_iounmap:
Atish Patraf1ad1132020-03-02 15:11:46 -0800387 iounmap(priv->regs);
388out_free_priv:
389 kfree(priv);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200390 return error;
391}
392
393IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
394IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */