plic_fdt.c revision 1.1
11.1Sskrll/* $NetBSD: plic_fdt.c,v 1.1 2023/05/07 12:41:48 skrll Exp $ */ 21.1Sskrll 31.1Sskrll/*- 41.1Sskrll * Copyright (c) 2022 The NetBSD Foundation, Inc. 51.1Sskrll * All rights reserved. 61.1Sskrll * 71.1Sskrll * Portions of this code is derived from software contributed to The NetBSD 81.1Sskrll * Foundation by Simon Burge and Nick Hudson. 91.1Sskrll * 101.1Sskrll * Redistribution and use in source and binary forms, with or without 111.1Sskrll * modification, are permitted provided that the following conditions 121.1Sskrll * are met: 131.1Sskrll * 1. Redistributions of source code must retain the above copyright 141.1Sskrll * notice, this list of conditions and the following disclaimer. 151.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright 161.1Sskrll * notice, this list of conditions and the following disclaimer in the 171.1Sskrll * documentation and/or other materials provided with the distribution. 181.1Sskrll * 191.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sskrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sskrll * POSSIBILITY OF SUCH DAMAGE. 301.1Sskrll */ 311.1Sskrll 321.1Sskrll#include <sys/cdefs.h> 331.1Sskrll__KERNEL_RCSID(0, "$NetBSD: plic_fdt.c,v 1.1 2023/05/07 12:41:48 skrll Exp $"); 341.1Sskrll 351.1Sskrll#include <sys/param.h> 361.1Sskrll 371.1Sskrll#include <sys/bus.h> 381.1Sskrll#include <sys/cpu.h> 391.1Sskrll#include <sys/device.h> 401.1Sskrll#include <sys/intr.h> 411.1Sskrll 421.1Sskrll#include <dev/fdt/fdtvar.h> 431.1Sskrll 441.1Sskrll#include <riscv/sysreg.h> 451.1Sskrll#include <riscv/dev/plicreg.h> 461.1Sskrll#include <riscv/dev/plicvar.h> 471.1Sskrll 481.1Sskrllstatic const struct device_compatible_entry compat_data[] = { 491.1Sskrll { .compat = "riscv,plic0" }, 501.1Sskrll { .compat = "sifive,plic-1.0.0" }, 511.1Sskrll DEVICE_COMPAT_EOL 521.1Sskrll}; 531.1Sskrll 541.1Sskrllstatic void * 551.1Sskrllplic_fdt_intr_establish(device_t dev, u_int *specifier, int ipl, int flags, 561.1Sskrll int (*func)(void *), void *arg, const char *xname) 571.1Sskrll{ 581.1Sskrll struct plic_softc * const sc = device_private(dev); 591.1Sskrll struct plic_intrhand *ih; 601.1Sskrll 611.1Sskrll /* 1st cell is the interrupt number */ 621.1Sskrll const u_int irq = be32toh(specifier[0]); 631.1Sskrll if (irq > sc->sc_ndev) { 641.1Sskrll aprint_error_dev(dev, "irq %d greater than max irq %d\n", 651.1Sskrll irq, sc->sc_ndev); 661.1Sskrll return NULL; 671.1Sskrll } 681.1Sskrll ih = plic_intr_establish_xname(irq, ipl, 691.1Sskrll (flags & FDT_INTR_MPSAFE) != 0 ? IST_MPSAFE : 0, func, arg, xname); 701.1Sskrll 711.1Sskrll return ih; 721.1Sskrll} 731.1Sskrllstatic void 741.1Sskrllplic_fdt_intr_disestablish(device_t dev, void *cookie) 751.1Sskrll{ 761.1Sskrll struct plic_softc * const sc = device_private(dev); 771.1Sskrll struct plic_intrhand * const ih = cookie; 781.1Sskrll const u_int cidx = ih->ih_cidx; 791.1Sskrll const u_int irq = ih->ih_irq; 801.1Sskrll 811.1Sskrll plic_disable(sc, cidx, irq); 821.1Sskrll plic_set_priority(sc, irq, 0); 831.1Sskrll 841.1Sskrll memset(&sc->sc_intr[irq], 0, sizeof(*sc->sc_intr)); 851.1Sskrll} 861.1Sskrll 871.1Sskrll 881.1Sskrllstatic bool 891.1Sskrllplic_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen) 901.1Sskrll{ 911.1Sskrll /* 1st cell is the interrupt number */ 921.1Sskrll const int irq = be32toh(specifier[0]); 931.1Sskrll 941.1Sskrll snprintf(buf, buflen, "%s irq %d", device_xname(dev), irq); 951.1Sskrll 961.1Sskrll return true; 971.1Sskrll} 981.1Sskrll 991.1Sskrllstatic struct fdtbus_interrupt_controller_func plic_funcs = { 1001.1Sskrll .establish = plic_fdt_intr_establish, 1011.1Sskrll .disestablish = plic_fdt_intr_disestablish, 1021.1Sskrll .intrstr = plic_intrstr, 1031.1Sskrll}; 1041.1Sskrll 1051.1Sskrllstatic int 1061.1Sskrllplic_fdt_match(device_t parent, cfdata_t cf, void *aux) 1071.1Sskrll{ 1081.1Sskrll struct fdt_attach_args * const faa = aux; 1091.1Sskrll 1101.1Sskrll return of_compatible_match(faa->faa_phandle, compat_data); 1111.1Sskrll} 1121.1Sskrll 1131.1Sskrllstatic void 1141.1Sskrllplic_fdt_attach(device_t parent, device_t self, void *aux) 1151.1Sskrll{ 1161.1Sskrll struct plic_softc * const sc = device_private(self); 1171.1Sskrll struct fdt_attach_args * const faa = aux; 1181.1Sskrll const int phandle = faa->faa_phandle; 1191.1Sskrll bus_addr_t addr; 1201.1Sskrll bus_size_t size; 1211.1Sskrll const uint32_t *data; 1221.1Sskrll int error, context, len; 1231.1Sskrll 1241.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1251.1Sskrll aprint_error(": couldn't get registers\n"); 1261.1Sskrll return; 1271.1Sskrll } 1281.1Sskrll 1291.1Sskrll sc->sc_dev = self; 1301.1Sskrll sc->sc_bst = faa->faa_bst; 1311.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1321.1Sskrll aprint_error(": couldn't get registers\n"); 1331.1Sskrll return; 1341.1Sskrll } 1351.1Sskrll 1361.1Sskrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1371.1Sskrll aprint_error(": couldn't map registers\n"); 1381.1Sskrll return; 1391.1Sskrll } 1401.1Sskrll 1411.1Sskrll error = of_getprop_uint32(phandle, "riscv,ndev", &sc->sc_ndev); 1421.1Sskrll if (error) { 1431.1Sskrll aprint_error("couldn't get supported number of external " 1441.1Sskrll "interrupts\n"); 1451.1Sskrll return; 1461.1Sskrll } 1471.1Sskrll if (sc->sc_ndev > PLIC_MAX_IRQ) { 1481.1Sskrll aprint_error(": invalid number of external interrupts (%u)\n", 1491.1Sskrll sc->sc_ndev); 1501.1Sskrll return; 1511.1Sskrll } 1521.1Sskrll aprint_verbose("\n"); 1531.1Sskrll 1541.1Sskrll /* 1551.1Sskrll * PLIC context device mappings is documented at 1561.1Sskrll * https://www.kernel.org/doc/Documentation/devicetree/bindings/interrupt-controller/sifive%2Cplic-1.0.0.yaml 1571.1Sskrll * We need to walk the "interrupts-extended" property of 1581.1Sskrll * and register handlers for the defined contexts. 1591.1Sskrll * 1601.1Sskrll * XXX 1611.1Sskrll * This is usually an abstraction violation to inspect 1621.1Sskrll * the current node's properties directly. We do it 1631.1Sskrll * in this case because the current binding spec defines 1641.1Sskrll * this case. We do a bit of error checking to make 1651.1Sskrll * sure all the documented assumptions are correct. 1661.1Sskrll */ 1671.1Sskrll 1681.1Sskrll data = fdtbus_get_prop(phandle, "interrupts-extended", &len); 1691.1Sskrll if (data == NULL) { 1701.1Sskrll aprint_error_dev(self, "couldn't get context data\n"); 1711.1Sskrll return; 1721.1Sskrll } 1731.1Sskrll context = 0; 1741.1Sskrll while (len > 0) { 1751.1Sskrll const int pphandle = be32toh(data[0]); 1761.1Sskrll const int xref = fdtbus_get_phandle_from_native(pphandle); 1771.1Sskrll const int intr_source = be32toh(data[1]); 1781.1Sskrll uint32_t intr_cells; 1791.1Sskrll 1801.1Sskrll error = of_getprop_uint32(xref, "#interrupt-cells", &intr_cells); 1811.1Sskrll if (error) { 1821.1Sskrll aprint_error_dev(self, "couldn't get cell length " 1831.1Sskrll "for parent CPU for context %d", context); 1841.1Sskrll return; 1851.1Sskrll } 1861.1Sskrll 1871.1Sskrll if (intr_source != -1) { 1881.1Sskrll /* What do we want to pass as arg to plic_intr */ 1891.1Sskrll void *ih = fdtbus_intr_establish_xname(phandle, 1901.1Sskrll context, IPL_VM, FDT_INTR_MPSAFE, 1911.1Sskrll plic_intr, sc, device_xname(self)); 1921.1Sskrll if (ih == NULL) { 1931.1Sskrll aprint_error_dev(self, "couldn't install " 1941.1Sskrll "interrupt handler\n"); 1951.1Sskrll } else { 1961.1Sskrll char intrstr[128]; 1971.1Sskrll bool ok = fdtbus_intr_str(phandle, context, 1981.1Sskrll intrstr, sizeof(intrstr)); 1991.1Sskrll aprint_verbose_dev(self, "interrupt %s handler " 2001.1Sskrll "installed\n", ok ? intrstr : "(unk)"); 2011.1Sskrll } 2021.1Sskrll 2031.1Sskrll if (intr_source == IRQ_SUPERVISOR_EXTERNAL) { 2041.1Sskrll /* 2051.1Sskrll * When finding context info, parent _must_ be a 2061.1Sskrll * compatbile clint device. 2071.1Sskrll */ 2081.1Sskrll bus_addr_t cpuid; 2091.1Sskrll int cpu_ref; 2101.1Sskrll static const struct device_compatible_entry clint_compat_data[] = { 2111.1Sskrll { .compat = "riscv,cpu-intc" }, 2121.1Sskrll DEVICE_COMPAT_EOL 2131.1Sskrll }; 2141.1Sskrll 2151.1Sskrll if (of_compatible_match(xref, clint_compat_data)) { 2161.1Sskrll /* get cpuid for the parent node */ 2171.1Sskrll cpu_ref = OF_parent(xref); 2181.1Sskrll fdtbus_get_reg(cpu_ref, 0, &cpuid, NULL); 2191.1Sskrll 2201.1Sskrll KASSERT(context <= PLIC_MAX_CONTEXT); 2211.1Sskrll sc->sc_context[cpuid] = context; 2221.1Sskrll aprint_verbose_dev(self, 2231.1Sskrll "cpu %"PRId64" context %d\n", 2241.1Sskrll cpuid, context); 2251.1Sskrll } else { 2261.1Sskrll aprint_error_dev(self, "incompatiable CLINT " 2271.1Sskrll " for PLIC for context %d\n", context); 2281.1Sskrll } 2291.1Sskrll 2301.1Sskrll } 2311.1Sskrll } 2321.1Sskrll len -= (intr_cells + 1) * 4; 2331.1Sskrll data += (intr_cells + 1); 2341.1Sskrll context++; 2351.1Sskrll } 2361.1Sskrll 2371.1Sskrll aprint_verbose_dev(self, ""); 2381.1Sskrll error = plic_attach_common(sc, addr, size); 2391.1Sskrll if (error != 0) { 2401.1Sskrll return; 2411.1Sskrll } 2421.1Sskrll 2431.1Sskrll /* Setup complete, register this FDT bus. */ 2441.1Sskrll error = fdtbus_register_interrupt_controller(self, phandle, 2451.1Sskrll &plic_funcs); 2461.1Sskrll if (error != 0) { 2471.1Sskrll aprint_error(": couldn't register with fdtbus: %d\n", error); 2481.1Sskrll } 2491.1Sskrll} 2501.1Sskrll 2511.1SskrllCFATTACH_DECL_NEW(plic_fdt, sizeof(struct plic_softc), 2521.1Sskrll plic_fdt_match, plic_fdt_attach, NULL, NULL); 253