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