blob: 7c7f37393f993897702e9285980e7262994e0a44 [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>
12#include <linux/irqdomain.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/of_address.h>
16#include <linux/of_irq.h>
17#include <linux/platform_device.h>
18#include <linux/spinlock.h>
Atish Patraf99fb602018-10-02 12:15:05 -070019#include <asm/smp.h>
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020020
21/*
22 * This driver implements a version of the RISC-V PLIC with the actual layout
23 * specified in chapter 8 of the SiFive U5 Coreplex Series Manual:
24 *
25 * https://static.dev.sifive.com/U54-MC-RVCoreIP.pdf
26 *
27 * The largest number supported by devices marked as 'sifive,plic-1.0.0', is
28 * 1024, of which device 0 is defined as non-existent by the RISC-V Privileged
29 * Spec.
30 */
31
32#define MAX_DEVICES 1024
33#define MAX_CONTEXTS 15872
34
35/*
36 * Each interrupt source has a priority register associated with it.
37 * We always hardwire it to one in Linux.
38 */
39#define PRIORITY_BASE 0
40#define PRIORITY_PER_ID 4
41
42/*
43 * Each hart context has a vector of interrupt enable bits associated with it.
44 * There's one bit for each interrupt source.
45 */
46#define ENABLE_BASE 0x2000
47#define ENABLE_PER_HART 0x80
48
49/*
50 * Each hart context has a set of control registers associated with it. Right
51 * now there's only two: a source priority threshold over which the hart will
52 * take an interrupt, and a register to claim interrupts.
53 */
54#define CONTEXT_BASE 0x200000
55#define CONTEXT_PER_HART 0x1000
56#define CONTEXT_THRESHOLD 0x00
57#define CONTEXT_CLAIM 0x04
58
Atish Patraccbe80b2020-03-02 15:11:45 -080059#define PLIC_DISABLE_THRESHOLD 0xf
60#define PLIC_ENABLE_THRESHOLD 0
61
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020062static void __iomem *plic_regs;
63
64struct plic_handler {
65 bool present;
Anup Patel86c7cbf2019-02-12 18:22:43 +053066 void __iomem *hart_base;
67 /*
68 * Protect mask operations on the registers given that we can't
69 * assume atomic memory operations work on them.
70 */
71 raw_spinlock_t enable_lock;
72 void __iomem *enable_base;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020073};
74static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
75
Anup Patel86c7cbf2019-02-12 18:22:43 +053076static inline void plic_toggle(struct plic_handler *handler,
77 int hwirq, int enable)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020078{
Anup Patel86c7cbf2019-02-12 18:22:43 +053079 u32 __iomem *reg = handler->enable_base + (hwirq / 32) * sizeof(u32);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020080 u32 hwirq_mask = 1 << (hwirq % 32);
81
Anup Patel86c7cbf2019-02-12 18:22:43 +053082 raw_spin_lock(&handler->enable_lock);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020083 if (enable)
84 writel(readl(reg) | hwirq_mask, reg);
85 else
86 writel(readl(reg) & ~hwirq_mask, reg);
Anup Patel86c7cbf2019-02-12 18:22:43 +053087 raw_spin_unlock(&handler->enable_lock);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020088}
89
Anup Patelcc9f04f2019-02-12 18:22:46 +053090static inline void plic_irq_toggle(const struct cpumask *mask,
91 int hwirq, int enable)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020092{
93 int cpu;
94
Anup Patelcc9f04f2019-02-12 18:22:46 +053095 writel(enable, plic_regs + PRIORITY_BASE + hwirq * PRIORITY_PER_ID);
96 for_each_cpu(cpu, mask) {
Christoph Hellwig8237f8b2018-07-26 16:27:00 +020097 struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
98
99 if (handler->present)
Anup Patelcc9f04f2019-02-12 18:22:46 +0530100 plic_toggle(handler, hwirq, enable);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200101 }
102}
103
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100104static void plic_irq_unmask(struct irq_data *d)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200105{
Anup Patelcc9f04f2019-02-12 18:22:46 +0530106 unsigned int cpu = cpumask_any_and(irq_data_get_affinity_mask(d),
107 cpu_online_mask);
108 if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
109 return;
110 plic_irq_toggle(cpumask_of(cpu), d->hwirq, 1);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200111}
112
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100113static void plic_irq_mask(struct irq_data *d)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200114{
Anup Patelcc9f04f2019-02-12 18:22:46 +0530115 plic_irq_toggle(cpu_possible_mask, d->hwirq, 0);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200116}
117
Anup Patelcc9f04f2019-02-12 18:22:46 +0530118#ifdef CONFIG_SMP
119static int plic_set_affinity(struct irq_data *d,
120 const struct cpumask *mask_val, bool force)
121{
122 unsigned int cpu;
123
124 if (force)
125 cpu = cpumask_first(mask_val);
126 else
127 cpu = cpumask_any_and(mask_val, cpu_online_mask);
128
129 if (cpu >= nr_cpu_ids)
130 return -EINVAL;
131
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100132 plic_irq_toggle(cpu_possible_mask, d->hwirq, 0);
133 plic_irq_toggle(cpumask_of(cpu), d->hwirq, 1);
Anup Patelcc9f04f2019-02-12 18:22:46 +0530134
135 irq_data_update_effective_affinity(d, cpumask_of(cpu));
136
137 return IRQ_SET_MASK_OK_DONE;
138}
139#endif
140
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100141static void plic_irq_eoi(struct irq_data *d)
142{
143 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
144
145 writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
146}
147
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200148static struct irq_chip plic_chip = {
149 .name = "SiFive PLIC",
Marc Zyngierbb0fed12019-09-15 15:17:45 +0100150 .irq_mask = plic_irq_mask,
151 .irq_unmask = plic_irq_unmask,
152 .irq_eoi = plic_irq_eoi,
Anup Patelcc9f04f2019-02-12 18:22:46 +0530153#ifdef CONFIG_SMP
154 .irq_set_affinity = plic_set_affinity,
155#endif
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200156};
157
158static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
159 irq_hw_number_t hwirq)
160{
Yash Shah466008f2019-12-10 16:41:11 +0530161 irq_domain_set_info(d, irq, hwirq, &plic_chip, d->host_data,
162 handle_fasteoi_irq, NULL, NULL);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200163 irq_set_noprobe(irq);
164 return 0;
165}
166
Yash Shah466008f2019-12-10 16:41:11 +0530167static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
168 unsigned int nr_irqs, void *arg)
169{
170 int i, ret;
171 irq_hw_number_t hwirq;
172 unsigned int type;
173 struct irq_fwspec *fwspec = arg;
174
175 ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
176 if (ret)
177 return ret;
178
179 for (i = 0; i < nr_irqs; i++) {
180 ret = plic_irqdomain_map(domain, virq + i, hwirq + i);
181 if (ret)
182 return ret;
183 }
184
185 return 0;
186}
187
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200188static const struct irq_domain_ops plic_irqdomain_ops = {
Yash Shah466008f2019-12-10 16:41:11 +0530189 .translate = irq_domain_translate_onecell,
190 .alloc = plic_irq_domain_alloc,
191 .free = irq_domain_free_irqs_top,
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200192};
193
194static struct irq_domain *plic_irqdomain;
195
196/*
197 * Handling an interrupt is a two-step process: first you claim the interrupt
198 * by reading the claim register, then you complete the interrupt by writing
199 * that source ID back to the same claim register. This automatically enables
200 * and disables the interrupt, so there's nothing else to do.
201 */
202static void plic_handle_irq(struct pt_regs *regs)
203{
204 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
Anup Patel86c7cbf2019-02-12 18:22:43 +0530205 void __iomem *claim = handler->hart_base + CONTEXT_CLAIM;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200206 irq_hw_number_t hwirq;
207
208 WARN_ON_ONCE(!handler->present);
209
Christoph Hellwiga4c37332019-10-28 13:10:32 +0100210 csr_clear(CSR_IE, IE_EIE);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200211 while ((hwirq = readl(claim))) {
212 int irq = irq_find_mapping(plic_irqdomain, hwirq);
213
214 if (unlikely(irq <= 0))
215 pr_warn_ratelimited("can't find mapping for hwirq %lu\n",
216 hwirq);
217 else
218 generic_handle_irq(irq);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200219 }
Christoph Hellwiga4c37332019-10-28 13:10:32 +0100220 csr_set(CSR_IE, IE_EIE);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200221}
222
223/*
224 * Walk up the DT tree until we find an active RISC-V core (HART) node and
225 * extract the cpuid from it.
226 */
227static int plic_find_hart_id(struct device_node *node)
228{
229 for (; node; node = node->parent) {
230 if (of_device_is_compatible(node, "riscv"))
Palmer Dabbeltb2f8cfa72018-10-02 12:15:00 -0700231 return riscv_of_processor_hartid(node);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200232 }
233
234 return -1;
235}
236
Atish Patraccbe80b2020-03-02 15:11:45 -0800237static void plic_set_threshold(struct plic_handler *handler, u32 threshold)
238{
239 /* priority must be > threshold to trigger an interrupt */
240 writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
241}
242
243static int plic_dying_cpu(unsigned int cpu)
244{
245 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
246
247 csr_clear(CSR_IE, IE_EIE);
248 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
249
250 return 0;
251}
252
253static int plic_starting_cpu(unsigned int cpu)
254{
255 struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
256
257 csr_set(CSR_IE, IE_EIE);
258 plic_set_threshold(handler, PLIC_ENABLE_THRESHOLD);
259
260 return 0;
261}
262
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200263static int __init plic_init(struct device_node *node,
264 struct device_node *parent)
265{
Anup Patel6adfe8d2019-02-12 18:22:45 +0530266 int error = 0, nr_contexts, nr_handlers = 0, i;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200267 u32 nr_irqs;
268
269 if (plic_regs) {
270 pr_warn("PLIC already present.\n");
271 return -ENXIO;
272 }
273
274 plic_regs = of_iomap(node, 0);
275 if (WARN_ON(!plic_regs))
276 return -EIO;
277
278 error = -EINVAL;
279 of_property_read_u32(node, "riscv,ndev", &nr_irqs);
280 if (WARN_ON(!nr_irqs))
281 goto out_iounmap;
282
Anup Patel6adfe8d2019-02-12 18:22:45 +0530283 nr_contexts = of_irq_count(node);
284 if (WARN_ON(!nr_contexts))
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200285 goto out_iounmap;
Anup Patel6adfe8d2019-02-12 18:22:45 +0530286 if (WARN_ON(nr_contexts < num_possible_cpus()))
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200287 goto out_iounmap;
288
289 error = -ENOMEM;
290 plic_irqdomain = irq_domain_add_linear(node, nr_irqs + 1,
291 &plic_irqdomain_ops, NULL);
292 if (WARN_ON(!plic_irqdomain))
293 goto out_iounmap;
294
Anup Patel6adfe8d2019-02-12 18:22:45 +0530295 for (i = 0; i < nr_contexts; i++) {
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200296 struct of_phandle_args parent;
297 struct plic_handler *handler;
298 irq_hw_number_t hwirq;
Atish Patraf99fb602018-10-02 12:15:05 -0700299 int cpu, hartid;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200300
301 if (of_irq_parse_one(node, i, &parent)) {
302 pr_err("failed to parse parent for context %d.\n", i);
303 continue;
304 }
305
Christoph Hellwiga4c37332019-10-28 13:10:32 +0100306 /*
307 * Skip contexts other than external interrupts for our
308 * privilege level.
309 */
Paul Walmsley2f3035d2019-12-20 03:09:49 -0800310 if (parent.args[0] != RV_IRQ_EXT)
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200311 continue;
312
Atish Patraf99fb602018-10-02 12:15:05 -0700313 hartid = plic_find_hart_id(parent.np);
314 if (hartid < 0) {
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200315 pr_warn("failed to parse hart ID for context %d.\n", i);
316 continue;
317 }
318
Atish Patraf99fb602018-10-02 12:15:05 -0700319 cpu = riscv_hartid_to_cpuid(hartid);
Atish Patrafc03aca2019-02-12 03:10:11 -0800320 if (cpu < 0) {
321 pr_warn("Invalid cpuid for context %d\n", i);
322 continue;
323 }
324
Christoph Hellwig9ce06492019-09-03 11:32:20 +0200325 /*
326 * When running in M-mode we need to ignore the S-mode handler.
327 * Here we assume it always comes later, but that might be a
328 * little fragile.
329 */
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200330 handler = per_cpu_ptr(&plic_handlers, cpu);
Anup Patel3fecb5a2019-02-12 18:22:44 +0530331 if (handler->present) {
332 pr_warn("handler already present for context %d.\n", i);
Atish Patraccbe80b2020-03-02 15:11:45 -0800333 plic_set_threshold(handler, PLIC_DISABLE_THRESHOLD);
Christoph Hellwig9ce06492019-09-03 11:32:20 +0200334 goto done;
Anup Patel3fecb5a2019-02-12 18:22:44 +0530335 }
336
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200337 handler->present = true;
Anup Patel86c7cbf2019-02-12 18:22:43 +0530338 handler->hart_base =
339 plic_regs + CONTEXT_BASE + i * CONTEXT_PER_HART;
340 raw_spin_lock_init(&handler->enable_lock);
341 handler->enable_base =
342 plic_regs + ENABLE_BASE + i * ENABLE_PER_HART;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200343
Christoph Hellwig9ce06492019-09-03 11:32:20 +0200344done:
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200345 for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
Anup Patel86c7cbf2019-02-12 18:22:43 +0530346 plic_toggle(handler, hwirq, 0);
Anup Patel6adfe8d2019-02-12 18:22:45 +0530347 nr_handlers++;
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200348 }
349
Atish Patraccbe80b2020-03-02 15:11:45 -0800350 cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
351 "irqchip/sifive/plic:starting",
352 plic_starting_cpu, plic_dying_cpu);
Anup Patel6adfe8d2019-02-12 18:22:45 +0530353 pr_info("mapped %d interrupts with %d handlers for %d contexts.\n",
354 nr_irqs, nr_handlers, nr_contexts);
Christoph Hellwig8237f8b2018-07-26 16:27:00 +0200355 set_handle_irq(plic_handle_irq);
356 return 0;
357
358out_iounmap:
359 iounmap(plic_regs);
360 return error;
361}
362
363IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
364IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */