plic_fdt.c revision 1.2
11.2Sskrll/* $NetBSD: plic_fdt.c,v 1.2 2023/09/02 09:29:59 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.2Sskrll__KERNEL_RCSID(0, "$NetBSD: plic_fdt.c,v 1.2 2023/09/02 09:29:59 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.2Sskrll 741.1Sskrllstatic void 751.1Sskrllplic_fdt_intr_disestablish(device_t dev, void *cookie) 761.1Sskrll{ 771.1Sskrll 781.2Sskrll plic_intr_disestablish(cookie); 791.1Sskrll} 801.1Sskrll 811.1Sskrll 821.1Sskrllstatic bool 831.1Sskrllplic_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen) 841.1Sskrll{ 851.1Sskrll /* 1st cell is the interrupt number */ 861.1Sskrll const int irq = be32toh(specifier[0]); 871.1Sskrll 881.1Sskrll snprintf(buf, buflen, "%s irq %d", device_xname(dev), irq); 891.1Sskrll 901.1Sskrll return true; 911.1Sskrll} 921.1Sskrll 931.1Sskrllstatic struct fdtbus_interrupt_controller_func plic_funcs = { 941.1Sskrll .establish = plic_fdt_intr_establish, 951.1Sskrll .disestablish = plic_fdt_intr_disestablish, 961.1Sskrll .intrstr = plic_intrstr, 971.1Sskrll}; 981.1Sskrll 991.1Sskrllstatic int 1001.1Sskrllplic_fdt_match(device_t parent, cfdata_t cf, void *aux) 1011.1Sskrll{ 1021.1Sskrll struct fdt_attach_args * const faa = aux; 1031.1Sskrll 1041.1Sskrll return of_compatible_match(faa->faa_phandle, compat_data); 1051.1Sskrll} 1061.1Sskrll 1071.1Sskrllstatic void 1081.1Sskrllplic_fdt_attach(device_t parent, device_t self, void *aux) 1091.1Sskrll{ 1101.1Sskrll struct plic_softc * const sc = device_private(self); 1111.1Sskrll struct fdt_attach_args * const faa = aux; 1121.1Sskrll const int phandle = faa->faa_phandle; 1131.1Sskrll bus_addr_t addr; 1141.1Sskrll bus_size_t size; 1151.1Sskrll const uint32_t *data; 1161.1Sskrll int error, context, len; 1171.1Sskrll 1181.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1191.1Sskrll aprint_error(": couldn't get registers\n"); 1201.1Sskrll return; 1211.1Sskrll } 1221.1Sskrll 1231.1Sskrll sc->sc_dev = self; 1241.1Sskrll sc->sc_bst = faa->faa_bst; 1251.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1261.1Sskrll aprint_error(": couldn't get registers\n"); 1271.1Sskrll return; 1281.1Sskrll } 1291.1Sskrll 1301.1Sskrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1311.1Sskrll aprint_error(": couldn't map registers\n"); 1321.1Sskrll return; 1331.1Sskrll } 1341.1Sskrll 1351.1Sskrll error = of_getprop_uint32(phandle, "riscv,ndev", &sc->sc_ndev); 1361.1Sskrll if (error) { 1371.1Sskrll aprint_error("couldn't get supported number of external " 1381.1Sskrll "interrupts\n"); 1391.1Sskrll return; 1401.1Sskrll } 1411.1Sskrll if (sc->sc_ndev > PLIC_MAX_IRQ) { 1421.1Sskrll aprint_error(": invalid number of external interrupts (%u)\n", 1431.1Sskrll sc->sc_ndev); 1441.1Sskrll return; 1451.1Sskrll } 1461.1Sskrll aprint_verbose("\n"); 1471.1Sskrll 1481.1Sskrll /* 1491.1Sskrll * PLIC context device mappings is documented at 1501.1Sskrll * https://www.kernel.org/doc/Documentation/devicetree/bindings/interrupt-controller/sifive%2Cplic-1.0.0.yaml 1511.1Sskrll * We need to walk the "interrupts-extended" property of 1521.1Sskrll * and register handlers for the defined contexts. 1531.1Sskrll * 1541.1Sskrll * XXX 1551.1Sskrll * This is usually an abstraction violation to inspect 1561.1Sskrll * the current node's properties directly. We do it 1571.1Sskrll * in this case because the current binding spec defines 1581.1Sskrll * this case. We do a bit of error checking to make 1591.1Sskrll * sure all the documented assumptions are correct. 1601.1Sskrll */ 1611.1Sskrll 1621.1Sskrll data = fdtbus_get_prop(phandle, "interrupts-extended", &len); 1631.1Sskrll if (data == NULL) { 1641.1Sskrll aprint_error_dev(self, "couldn't get context data\n"); 1651.1Sskrll return; 1661.1Sskrll } 1671.1Sskrll context = 0; 1681.1Sskrll while (len > 0) { 1691.1Sskrll const int pphandle = be32toh(data[0]); 1701.1Sskrll const int xref = fdtbus_get_phandle_from_native(pphandle); 1711.1Sskrll const int intr_source = be32toh(data[1]); 1721.1Sskrll uint32_t intr_cells; 1731.1Sskrll 1741.1Sskrll error = of_getprop_uint32(xref, "#interrupt-cells", &intr_cells); 1751.1Sskrll if (error) { 1761.1Sskrll aprint_error_dev(self, "couldn't get cell length " 1771.1Sskrll "for parent CPU for context %d", context); 1781.1Sskrll return; 1791.1Sskrll } 1801.1Sskrll 1811.1Sskrll if (intr_source != -1) { 1821.1Sskrll /* What do we want to pass as arg to plic_intr */ 1831.1Sskrll void *ih = fdtbus_intr_establish_xname(phandle, 1841.1Sskrll context, IPL_VM, FDT_INTR_MPSAFE, 1851.1Sskrll plic_intr, sc, device_xname(self)); 1861.1Sskrll if (ih == NULL) { 1871.1Sskrll aprint_error_dev(self, "couldn't install " 1881.1Sskrll "interrupt handler\n"); 1891.1Sskrll } else { 1901.1Sskrll char intrstr[128]; 1911.1Sskrll bool ok = fdtbus_intr_str(phandle, context, 1921.1Sskrll intrstr, sizeof(intrstr)); 1931.1Sskrll aprint_verbose_dev(self, "interrupt %s handler " 1941.1Sskrll "installed\n", ok ? intrstr : "(unk)"); 1951.1Sskrll } 1961.1Sskrll 1971.1Sskrll if (intr_source == IRQ_SUPERVISOR_EXTERNAL) { 1981.1Sskrll /* 1991.1Sskrll * When finding context info, parent _must_ be a 2001.1Sskrll * compatbile clint device. 2011.1Sskrll */ 2021.1Sskrll bus_addr_t cpuid; 2031.1Sskrll int cpu_ref; 2041.1Sskrll static const struct device_compatible_entry clint_compat_data[] = { 2051.1Sskrll { .compat = "riscv,cpu-intc" }, 2061.1Sskrll DEVICE_COMPAT_EOL 2071.1Sskrll }; 2081.1Sskrll 2091.1Sskrll if (of_compatible_match(xref, clint_compat_data)) { 2101.1Sskrll /* get cpuid for the parent node */ 2111.1Sskrll cpu_ref = OF_parent(xref); 2121.1Sskrll fdtbus_get_reg(cpu_ref, 0, &cpuid, NULL); 2131.1Sskrll 2141.1Sskrll KASSERT(context <= PLIC_MAX_CONTEXT); 2151.1Sskrll sc->sc_context[cpuid] = context; 2161.1Sskrll aprint_verbose_dev(self, 2171.1Sskrll "cpu %"PRId64" context %d\n", 2181.1Sskrll cpuid, context); 2191.1Sskrll } else { 2201.1Sskrll aprint_error_dev(self, "incompatiable CLINT " 2211.1Sskrll " for PLIC for context %d\n", context); 2221.1Sskrll } 2231.1Sskrll 2241.1Sskrll } 2251.1Sskrll } 2261.1Sskrll len -= (intr_cells + 1) * 4; 2271.1Sskrll data += (intr_cells + 1); 2281.1Sskrll context++; 2291.1Sskrll } 2301.1Sskrll 2311.1Sskrll aprint_verbose_dev(self, ""); 2321.1Sskrll error = plic_attach_common(sc, addr, size); 2331.1Sskrll if (error != 0) { 2341.1Sskrll return; 2351.1Sskrll } 2361.1Sskrll 2371.1Sskrll /* Setup complete, register this FDT bus. */ 2381.1Sskrll error = fdtbus_register_interrupt_controller(self, phandle, 2391.1Sskrll &plic_funcs); 2401.1Sskrll if (error != 0) { 2411.1Sskrll aprint_error(": couldn't register with fdtbus: %d\n", error); 2421.1Sskrll } 2431.1Sskrll} 2441.1Sskrll 2451.1SskrllCFATTACH_DECL_NEW(plic_fdt, sizeof(struct plic_softc), 2461.1Sskrll plic_fdt_match, plic_fdt_attach, NULL, NULL); 247