2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Ruslan Bukin <br@bsdpad.com>
6 * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
8 * Portions of this software were developed by SRI International and the
9 * University of Cambridge Computer Laboratory (Department of Computer Science
10 * and Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of
11 * the DARPA SSITH research programme.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
48 #include <machine/bus.h>
49 #include <machine/intr.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
57 #define PLIC_MAX_IRQS 1024
59 #define PLIC_PRIORITY_BASE 0x000000U
61 #define PLIC_ENABLE_BASE 0x002000U
62 #define PLIC_ENABLE_STRIDE 0x80U
64 #define PLIC_CONTEXT_BASE 0x200000U
65 #define PLIC_CONTEXT_STRIDE 0x1000U
66 #define PLIC_CONTEXT_THRESHOLD 0x0U
67 #define PLIC_CONTEXT_CLAIM 0x4U
69 #define PLIC_PRIORITY(n) (PLIC_PRIORITY_BASE + (n) * sizeof(uint32_t))
70 #define PLIC_ENABLE(sc, n, h) \
71 (sc->contexts[h].enable_offset + ((n) / 32) * sizeof(uint32_t))
72 #define PLIC_THRESHOLD(sc, h) \
73 (sc->contexts[h].context_offset + PLIC_CONTEXT_THRESHOLD)
74 #define PLIC_CLAIM(sc, h) \
75 (sc->contexts[h].context_offset + PLIC_CONTEXT_CLAIM)
77 static pic_disable_intr_t plic_disable_intr;
78 static pic_enable_intr_t plic_enable_intr;
79 static pic_map_intr_t plic_map_intr;
80 static pic_setup_intr_t plic_setup_intr;
81 static pic_post_ithread_t plic_post_ithread;
82 static pic_pre_ithread_t plic_pre_ithread;
83 static pic_bind_intr_t plic_bind_intr;
86 struct intr_irqsrc isrc;
91 bus_size_t enable_offset;
92 bus_size_t context_offset;
97 struct resource * intc_res;
98 struct plic_irqsrc isrcs[PLIC_MAX_IRQS];
99 struct plic_context contexts[MAXCPU];
103 #define RD4(sc, reg) \
104 bus_read_4(sc->intc_res, (reg))
105 #define WR4(sc, reg, val) \
106 bus_write_4(sc->intc_res, (reg), (val))
108 static u_int plic_irq_cpu;
111 riscv_hartid_to_cpu(int hartid)
116 if (pcpu_find(i)->pc_hart == hartid)
124 plic_get_hartid(device_t dev, phandle_t intc)
128 /* Check the interrupt controller layout. */
129 if (OF_searchencprop(intc, "#interrupt-cells", &hart,
130 sizeof(hart)) == -1) {
132 "Could not find #interrupt-cells for phandle %u\n", intc);
137 * The parent of the interrupt-controller is the CPU we are
138 * interested in, so search for its hart ID.
140 if (OF_searchencprop(OF_parent(intc), "reg", (pcell_t *)&hart,
141 sizeof(hart)) == -1) {
142 device_printf(dev, "Could not find hartid\n");
150 plic_irq_dispatch(struct plic_softc *sc, u_int irq,
151 struct trapframe *tf)
153 struct plic_irqsrc *src;
155 src = &sc->isrcs[irq];
157 if (intr_isrc_dispatch(&src->isrc, tf) != 0)
158 device_printf(sc->dev, "Stray irq %u detected\n", irq);
164 struct plic_softc *sc;
165 struct trapframe *tf;
170 cpu = PCPU_GET(cpuid);
172 /* Claim any pending interrupt. */
173 pending = RD4(sc, PLIC_CLAIM(sc, cpu));
175 tf = curthread->td_intr_frame;
176 plic_irq_dispatch(sc, pending, tf);
179 return (FILTER_HANDLED);
183 plic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
185 struct plic_softc *sc;
186 struct plic_irqsrc *src;
188 sc = device_get_softc(dev);
189 src = (struct plic_irqsrc *)isrc;
191 WR4(sc, PLIC_PRIORITY(src->irq), 0);
195 plic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
197 struct plic_softc *sc;
198 struct plic_irqsrc *src;
200 sc = device_get_softc(dev);
201 src = (struct plic_irqsrc *)isrc;
203 WR4(sc, PLIC_PRIORITY(src->irq), 1);
207 plic_map_intr(device_t dev, struct intr_map_data *data,
208 struct intr_irqsrc **isrcp)
210 struct intr_map_data_fdt *daf;
211 struct plic_softc *sc;
213 sc = device_get_softc(dev);
215 if (data->type != INTR_MAP_DATA_FDT)
218 daf = (struct intr_map_data_fdt *)data;
219 if (daf->ncells != 1 || daf->cells[0] > sc->ndev)
222 *isrcp = &sc->isrcs[daf->cells[0]].isrc;
228 plic_probe(device_t dev)
231 if (!ofw_bus_status_okay(dev))
234 if (!ofw_bus_is_compatible(dev, "riscv,plic0") &&
235 !ofw_bus_is_compatible(dev, "sifive,plic-1.0.0"))
238 device_set_desc(dev, "RISC-V PLIC");
240 return (BUS_PROBE_DEFAULT);
244 plic_attach(device_t dev)
246 struct plic_irqsrc *isrcs;
247 struct plic_softc *sc;
248 struct intr_pic *pic;
262 sc = device_get_softc(dev);
266 node = ofw_bus_get_node(dev);
267 if ((OF_getencprop(node, "riscv,ndev", &sc->ndev,
268 sizeof(sc->ndev))) < 0) {
270 "Error: could not get number of devices\n");
274 if (sc->ndev >= PLIC_MAX_IRQS) {
276 "Error: invalid ndev (%d)\n", sc->ndev);
280 /* Request memory resources */
282 sc->intc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
284 if (sc->intc_res == NULL) {
286 "Error: could not allocate memory resources\n");
290 /* Register the interrupt sources */
292 name = device_get_nameunit(sc->dev);
293 for (irq = 1; irq <= sc->ndev; irq++) {
294 isrcs[irq].irq = irq;
295 error = intr_isrc_register(&isrcs[irq].isrc, sc->dev,
296 0, "%s,%u", name, irq);
300 WR4(sc, PLIC_PRIORITY(irq), 0);
304 * Calculate the per-cpu enable and context register offsets.
306 * This is tricky for a few reasons. The PLIC divides the interrupt
307 * enable, threshold, and claim bits by "context", where each context
308 * routes to a Core-Local Interrupt Controller (CLIC).
310 * The tricky part is that the PLIC spec imposes no restrictions on how
311 * these contexts are laid out. So for example, there is no guarantee
312 * that each CPU will have both a machine mode and supervisor context,
313 * or that different PLIC implementations will organize the context
314 * registers in the same way. On top of this, we must handle the fact
315 * that cpuid != hartid, as they may have been renumbered during boot.
316 * We perform the following steps:
318 * 1. Examine the PLIC's "interrupts-extended" property and skip any
319 * entries that are not for supervisor external interrupts.
321 * 2. Walk up the device tree to find the corresponding CPU, and grab
324 * 3. Convert the hart to a cpuid, and calculate the register offsets
325 * based on the context number.
327 nintr = OF_getencprop_alloc_multi(node, "interrupts-extended",
328 sizeof(uint32_t), (void **)&cells);
330 device_printf(dev, "Could not read interrupts-extended\n");
334 /* interrupts-extended is a list of phandles and interrupt types. */
335 for (i = 0, context = 0; i < nintr; i += 2, context++) {
336 /* Skip M-mode external interrupts */
337 if (cells[i + 1] != IRQ_EXTERNAL_SUPERVISOR)
340 /* Get the hart ID from the CLIC's phandle. */
341 hart = plic_get_hartid(dev, OF_node_from_xref(cells[i]));
347 /* Get the corresponding cpuid. */
348 cpu = riscv_hartid_to_cpu(hart);
350 device_printf(dev, "Invalid hart!\n");
355 /* Set the enable and context register offsets for the CPU. */
356 sc->contexts[cpu].enable_offset = PLIC_ENABLE_BASE +
357 context * PLIC_ENABLE_STRIDE;
358 sc->contexts[cpu].context_offset = PLIC_CONTEXT_BASE +
359 context * PLIC_CONTEXT_STRIDE;
363 /* Set the threshold for each CPU to accept all priorities. */
365 WR4(sc, PLIC_THRESHOLD(sc, cpu), 0);
367 xref = OF_xref_from_node(node);
368 pic = intr_pic_register(sc->dev, xref);
372 csr_set(sie, SIE_SEIE);
374 return (intr_pic_claim_root(sc->dev, xref, plic_intr, sc, 0));
378 plic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
381 plic_disable_intr(dev, isrc);
385 plic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
387 struct plic_softc *sc;
388 struct plic_irqsrc *src;
391 sc = device_get_softc(dev);
392 src = (struct plic_irqsrc *)isrc;
394 cpu = CPU_FFS(&isrc->isrc_cpu) - 1;
396 /* Complete the interrupt. */
397 WR4(sc, PLIC_CLAIM(sc, cpu), src->irq);
398 plic_enable_intr(dev, isrc);
402 plic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
403 struct resource *res, struct intr_map_data *data)
405 struct plic_softc *sc;
406 struct plic_irqsrc *src;
408 sc = device_get_softc(dev);
409 src = (struct plic_irqsrc *)isrc;
411 CPU_ZERO(&isrc->isrc_cpu);
412 plic_bind_intr(dev, isrc);
418 plic_bind_intr(device_t dev, struct intr_irqsrc *isrc)
420 struct plic_softc *sc;
421 struct plic_irqsrc *src;
425 sc = device_get_softc(dev);
426 src = (struct plic_irqsrc *)isrc;
428 /* Disable the interrupt source on all CPUs. */
430 reg = RD4(sc, PLIC_ENABLE(sc, src->irq, cpu));
431 reg &= ~(1 << (src->irq % 32));
432 WR4(sc, PLIC_ENABLE(sc, src->irq, cpu), reg);
435 if (CPU_EMPTY(&isrc->isrc_cpu)) {
436 cpu = plic_irq_cpu = intr_irq_next_cpu(plic_irq_cpu, &all_cpus);
437 CPU_SETOF(cpu, &isrc->isrc_cpu);
440 * We will only bind to a single CPU so select the first
443 cpu = CPU_FFS(&isrc->isrc_cpu) - 1;
446 /* Enable the interrupt on the selected CPU only. */
447 reg = RD4(sc, PLIC_ENABLE(sc, src->irq, cpu));
448 reg |= (1 << (src->irq % 32));
449 WR4(sc, PLIC_ENABLE(sc, src->irq, cpu), reg);
454 static device_method_t plic_methods[] = {
455 DEVMETHOD(device_probe, plic_probe),
456 DEVMETHOD(device_attach, plic_attach),
458 DEVMETHOD(pic_disable_intr, plic_disable_intr),
459 DEVMETHOD(pic_enable_intr, plic_enable_intr),
460 DEVMETHOD(pic_map_intr, plic_map_intr),
461 DEVMETHOD(pic_pre_ithread, plic_pre_ithread),
462 DEVMETHOD(pic_post_ithread, plic_post_ithread),
463 DEVMETHOD(pic_post_filter, plic_post_ithread),
464 DEVMETHOD(pic_setup_intr, plic_setup_intr),
465 DEVMETHOD(pic_bind_intr, plic_bind_intr),
470 static driver_t plic_driver = {
473 sizeof(struct plic_softc),
476 static devclass_t plic_devclass;
478 EARLY_DRIVER_MODULE(plic, simplebus, plic_driver, plic_devclass,
479 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);