11.8Sskrll/* $NetBSD: plic_fdt.c,v 1.8 2024/08/11 08:29:12 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.8Sskrll__KERNEL_RCSID(0, "$NetBSD: plic_fdt.c,v 1.8 2024/08/11 08:29:12 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.4Sskrll#include <riscv/fdt/riscv_fdtvar.h> 461.1Sskrll#include <riscv/dev/plicreg.h> 471.1Sskrll#include <riscv/dev/plicvar.h> 481.1Sskrll 491.1Sskrllstatic const struct device_compatible_entry compat_data[] = { 501.7Sskrll { .compat = "riscv,plic0" }, 511.7Sskrll { .compat = "sifive,plic-1.0.0" }, 521.8Sskrll { .compat = "thead,c900-plic" }, 531.7Sskrll DEVICE_COMPAT_EOL 541.1Sskrll}; 551.1Sskrll 561.1Sskrllstatic void * 571.1Sskrllplic_fdt_intr_establish(device_t dev, u_int *specifier, int ipl, int flags, 581.1Sskrll int (*func)(void *), void *arg, const char *xname) 591.1Sskrll{ 601.1Sskrll struct plic_softc * const sc = device_private(dev); 611.1Sskrll struct plic_intrhand *ih; 621.1Sskrll 631.1Sskrll /* 1st cell is the interrupt number */ 641.1Sskrll const u_int irq = be32toh(specifier[0]); 651.1Sskrll if (irq > sc->sc_ndev) { 661.1Sskrll aprint_error_dev(dev, "irq %d greater than max irq %d\n", 671.1Sskrll irq, sc->sc_ndev); 681.1Sskrll return NULL; 691.1Sskrll } 701.1Sskrll ih = plic_intr_establish_xname(irq, ipl, 711.1Sskrll (flags & FDT_INTR_MPSAFE) != 0 ? IST_MPSAFE : 0, func, arg, xname); 721.1Sskrll 731.1Sskrll return ih; 741.1Sskrll} 751.2Sskrll 761.1Sskrllstatic void 771.1Sskrllplic_fdt_intr_disestablish(device_t dev, void *cookie) 781.1Sskrll{ 791.1Sskrll 801.2Sskrll plic_intr_disestablish(cookie); 811.1Sskrll} 821.1Sskrll 831.1Sskrll 841.1Sskrllstatic bool 851.1Sskrllplic_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen) 861.1Sskrll{ 871.1Sskrll /* 1st cell is the interrupt number */ 881.1Sskrll const int irq = be32toh(specifier[0]); 891.1Sskrll 901.1Sskrll snprintf(buf, buflen, "%s irq %d", device_xname(dev), irq); 911.1Sskrll 921.7Sskrll return true; 931.1Sskrll} 941.1Sskrll 951.1Sskrllstatic struct fdtbus_interrupt_controller_func plic_funcs = { 961.1Sskrll .establish = plic_fdt_intr_establish, 971.1Sskrll .disestablish = plic_fdt_intr_disestablish, 981.1Sskrll .intrstr = plic_intrstr, 991.1Sskrll}; 1001.1Sskrll 1011.1Sskrllstatic int 1021.1Sskrllplic_fdt_match(device_t parent, cfdata_t cf, void *aux) 1031.1Sskrll{ 1041.1Sskrll struct fdt_attach_args * const faa = aux; 1051.1Sskrll 1061.1Sskrll return of_compatible_match(faa->faa_phandle, compat_data); 1071.1Sskrll} 1081.1Sskrll 1091.4Sskrll 1101.4Sskrllstatic void 1111.4Sskrllplic_fdt_attach_source(device_t self, int phandle, int context, int xref, 1121.4Sskrll int intr_source) 1131.4Sskrll{ 1141.4Sskrll struct plic_softc * const sc = device_private(self); 1151.4Sskrll static const struct device_compatible_entry clint_compat_data[] = { 1161.4Sskrll { .compat = "riscv,cpu-intc" }, 1171.4Sskrll DEVICE_COMPAT_EOL 1181.4Sskrll }; 1191.4Sskrll 1201.4Sskrll if (!of_compatible_match(xref, clint_compat_data)) { 1211.5Sandvar aprint_error_dev(self, "incompatible CLINT " 1221.4Sskrll "for PLIC for context %d\n", context); 1231.4Sskrll return; 1241.4Sskrll } 1251.4Sskrll 1261.4Sskrll const int cpu_ref = OF_parent(xref); 1271.4Sskrll if (!riscv_fdt_cpu_okay(cpu_ref)) { 1281.4Sskrll aprint_verbose_dev(self, "inactive HART " 1291.4Sskrll "for PLIC for context %d\n", context); 1301.4Sskrll return; 1311.4Sskrll } 1321.4Sskrll 1331.4Sskrll /* What do we want to pass as arg to plic_intr */ 1341.4Sskrll void *ih = fdtbus_intr_establish_xname(phandle, 1351.4Sskrll context, IPL_VM, FDT_INTR_MPSAFE, 1361.4Sskrll plic_intr, sc, device_xname(self)); 1371.4Sskrll if (ih == NULL) { 1381.4Sskrll aprint_error_dev(self, "couldn't install " 1391.4Sskrll "interrupt handler\n"); 1401.4Sskrll } else { 1411.4Sskrll char intrstr[128]; 1421.4Sskrll bool ok = fdtbus_intr_str(phandle, context, 1431.4Sskrll intrstr, sizeof(intrstr)); 1441.4Sskrll aprint_verbose_dev(self, "interrupt %s handler " 1451.4Sskrll "installed\n", ok ? intrstr : "(unk)"); 1461.4Sskrll } 1471.4Sskrll 1481.4Sskrll if (intr_source == IRQ_SUPERVISOR_EXTERNAL) { 1491.4Sskrll bus_addr_t hartid; 1501.4Sskrll /* get cpuid for the parent node */ 1511.4Sskrll fdtbus_get_reg(cpu_ref, 0, &hartid, NULL); 1521.4Sskrll 1531.4Sskrll KASSERT(context <= PLIC_MAX_CONTEXT); 1541.4Sskrll sc->sc_context[hartid] = context; 1551.4Sskrll aprint_verbose_dev(self, "hart %"PRId64" context %d\n", 1561.4Sskrll hartid, context); 1571.4Sskrll } 1581.4Sskrll} 1591.4Sskrll 1601.4Sskrll 1611.1Sskrllstatic void 1621.1Sskrllplic_fdt_attach(device_t parent, device_t self, void *aux) 1631.1Sskrll{ 1641.1Sskrll struct plic_softc * const sc = device_private(self); 1651.1Sskrll struct fdt_attach_args * const faa = aux; 1661.1Sskrll const int phandle = faa->faa_phandle; 1671.1Sskrll bus_addr_t addr; 1681.1Sskrll bus_size_t size; 1691.1Sskrll const uint32_t *data; 1701.1Sskrll int error, context, len; 1711.1Sskrll 1721.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1731.1Sskrll aprint_error(": couldn't get registers\n"); 1741.1Sskrll return; 1751.1Sskrll } 1761.1Sskrll 1771.1Sskrll sc->sc_dev = self; 1781.1Sskrll sc->sc_bst = faa->faa_bst; 1791.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1801.1Sskrll aprint_error(": couldn't get registers\n"); 1811.1Sskrll return; 1821.1Sskrll } 1831.1Sskrll 1841.1Sskrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1851.1Sskrll aprint_error(": couldn't map registers\n"); 1861.1Sskrll return; 1871.1Sskrll } 1881.1Sskrll 1891.1Sskrll error = of_getprop_uint32(phandle, "riscv,ndev", &sc->sc_ndev); 1901.1Sskrll if (error) { 1911.1Sskrll aprint_error("couldn't get supported number of external " 1921.1Sskrll "interrupts\n"); 1931.1Sskrll return; 1941.1Sskrll } 1951.1Sskrll if (sc->sc_ndev > PLIC_MAX_IRQ) { 1961.1Sskrll aprint_error(": invalid number of external interrupts (%u)\n", 1971.1Sskrll sc->sc_ndev); 1981.1Sskrll return; 1991.1Sskrll } 2001.1Sskrll aprint_verbose("\n"); 2011.1Sskrll 2021.1Sskrll /* 2031.1Sskrll * PLIC context device mappings is documented at 2041.1Sskrll * https://www.kernel.org/doc/Documentation/devicetree/bindings/interrupt-controller/sifive%2Cplic-1.0.0.yaml 2051.1Sskrll * We need to walk the "interrupts-extended" property of 2061.1Sskrll * and register handlers for the defined contexts. 2071.1Sskrll * 2081.1Sskrll * XXX 2091.1Sskrll * This is usually an abstraction violation to inspect 2101.1Sskrll * the current node's properties directly. We do it 2111.1Sskrll * in this case because the current binding spec defines 2121.1Sskrll * this case. We do a bit of error checking to make 2131.1Sskrll * sure all the documented assumptions are correct. 2141.1Sskrll */ 2151.1Sskrll 2161.1Sskrll data = fdtbus_get_prop(phandle, "interrupts-extended", &len); 2171.1Sskrll if (data == NULL) { 2181.1Sskrll aprint_error_dev(self, "couldn't get context data\n"); 2191.1Sskrll return; 2201.1Sskrll } 2211.1Sskrll context = 0; 2221.1Sskrll while (len > 0) { 2231.1Sskrll const int pphandle = be32toh(data[0]); 2241.1Sskrll const int xref = fdtbus_get_phandle_from_native(pphandle); 2251.1Sskrll const int intr_source = be32toh(data[1]); 2261.1Sskrll uint32_t intr_cells; 2271.1Sskrll 2281.1Sskrll error = of_getprop_uint32(xref, "#interrupt-cells", &intr_cells); 2291.1Sskrll if (error) { 2301.1Sskrll aprint_error_dev(self, "couldn't get cell length " 2311.1Sskrll "for parent CPU for context %d", context); 2321.1Sskrll return; 2331.1Sskrll } 2341.1Sskrll 2351.1Sskrll if (intr_source != -1) { 2361.4Sskrll plic_fdt_attach_source(self, phandle, context, xref, 2371.4Sskrll intr_source); 2381.1Sskrll } 2391.1Sskrll len -= (intr_cells + 1) * 4; 2401.1Sskrll data += (intr_cells + 1); 2411.1Sskrll context++; 2421.1Sskrll } 2431.1Sskrll 2441.6Sskrll aprint_verbose_dev(self, "attaching"); 2451.1Sskrll error = plic_attach_common(sc, addr, size); 2461.1Sskrll if (error != 0) { 2471.1Sskrll return; 2481.1Sskrll } 2491.1Sskrll 2501.1Sskrll /* Setup complete, register this FDT bus. */ 2511.1Sskrll error = fdtbus_register_interrupt_controller(self, phandle, 2521.1Sskrll &plic_funcs); 2531.1Sskrll if (error != 0) { 2541.1Sskrll aprint_error(": couldn't register with fdtbus: %d\n", error); 2551.1Sskrll } 2561.1Sskrll} 2571.1Sskrll 2581.1SskrllCFATTACH_DECL_NEW(plic_fdt, sizeof(struct plic_softc), 2591.1Sskrll plic_fdt_match, plic_fdt_attach, NULL, NULL); 260