Home | History | Annotate | Line # | Download | only in fdt
intc_fdt.c revision 1.3
      1 /*	$NetBSD: intc_fdt.c,v 1.3 2023/12/25 13:21:30 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "opt_multiprocessor.h"
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: intc_fdt.c,v 1.3 2023/12/25 13:21:30 skrll Exp $");
     36 
     37 #include <sys/param.h>
     38 
     39 #include <sys/bus.h>
     40 #include <sys/cpu.h>
     41 #include <sys/device.h>
     42 #include <sys/evcnt.h>
     43 #include <sys/kmem.h>
     44 #include <sys/intr.h>
     45 
     46 #include <dev/fdt/fdtvar.h>
     47 
     48 #include <machine/frame.h>
     49 #include <machine/machdep.h>
     50 #include <machine/sysreg.h>
     51 
     52 static const struct device_compatible_entry compat_data[] = {
     53 	{ .compat = "riscv,cpu-intc" },
     54 	DEVICE_COMPAT_EOL
     55 };
     56 
     57 
     58 struct intc_irqhandler;
     59 struct intc_irq;
     60 struct intc_softc;
     61 
     62 typedef int (*intcih_t)(void *);
     63 
     64 struct intc_irqhandler {
     65 	struct intc_irq		 *ih_irq;
     66 	intcih_t		  ih_fn;
     67 	void			 *ih_arg;
     68 	TAILQ_ENTRY(intc_irqhandler)
     69 				  ih_next;
     70 };
     71 
     72 struct intc_irq {
     73 	struct intc_fdt_softc	*intr_sc;
     74 	void			*intr_ih;
     75 	void			*intr_arg;
     76 	int			 intr_refcnt;
     77 	int			 intr_ipl;
     78 	int			 intr_source;
     79 	int			 intr_istflags;
     80 	struct evcnt *pcpu_evs;
     81 	TAILQ_HEAD(, intc_irqhandler)
     82 				 intr_handlers;
     83 };
     84 
     85 
     86 struct intc_fdt_softc {
     87 	device_t		 sc_dev;
     88 	bus_space_tag_t		 sc_bst;
     89 	bus_space_handle_t	 sc_bsh;
     90 
     91 	struct intc_irq		*sc_irq[IRQ_NSOURCES];
     92 
     93 	struct evcnt		 sc_evs[IRQ_NSOURCES];
     94 
     95 	struct cpu_info 	*sc_ci;
     96 	cpuid_t			 sc_hartid;
     97 };
     98 
     99 static const char * const intc_sources[IRQ_NSOURCES] = {
    100 	/* Software interrupts */
    101 	"(reserved)",
    102 	"Supervisor software",
    103 	"Virtual Supervisor software",
    104 	"Machine software",
    105 
    106 	/* Timer interrupts */
    107 	"(reserved)",
    108 	"Supervisor timer",
    109 	"Virtual Supervisor timer",
    110 	"Machine timer",
    111 
    112 	/* External interrupts */
    113 	"(reserved)",
    114 	"Supervisor external",
    115 	"Virtual Supervisor external",
    116 	"Machine external",
    117 
    118 	"(reserved)",
    119 	"Supervisor guest external.",
    120 	"(reserved)",
    121 	"(reserved)"
    122 };
    123 
    124 
    125 static void *
    126 intc_intr_establish(struct intc_fdt_softc *sc, u_int source, u_int ipl,
    127     u_int istflags, int (*func)(void *), void *arg, const char *xname)
    128 {
    129 	if (source > IRQ_NSOURCES)
    130 		return NULL;
    131 
    132 	const device_t dev = sc->sc_dev;
    133 	struct intc_irq *irq = sc->sc_irq[source];
    134 	if (irq == NULL) {
    135 		irq = kmem_alloc(sizeof(*irq), KM_SLEEP);
    136 		irq->intr_sc = sc;
    137 		irq->intr_refcnt = 0;
    138 		irq->intr_arg = arg;
    139 		irq->intr_ipl = ipl;
    140 		irq->intr_istflags = istflags;
    141 		irq->intr_source = source;
    142 		TAILQ_INIT(&irq->intr_handlers);
    143 		sc->sc_irq[source] = irq;
    144 
    145 		evcnt_attach_dynamic(&sc->sc_evs[source], EVCNT_TYPE_INTR, NULL,
    146 		    device_xname(sc->sc_dev), intc_sources[source]);
    147 	} else {
    148 		if (irq->intr_arg == NULL || arg == NULL) {
    149 			device_printf(dev,
    150 			    "cannot share irq with NULL-arg handler\n");
    151 			return NULL;
    152 		}
    153 		if (irq->intr_ipl != ipl) {
    154 			device_printf(dev,
    155 			    "cannot share irq with different ipl\n");
    156 			return NULL;
    157 		}
    158 		if (irq->intr_istflags != istflags) {
    159 			device_printf(dev,
    160 			    "cannot share irq between mpsafe/non-mpsafe\n");
    161 			return NULL;
    162 		}
    163 	}
    164 
    165 	struct intc_irqhandler *irqh = kmem_alloc(sizeof(*irqh), KM_SLEEP);
    166 	irqh->ih_irq = irq;
    167 	irqh->ih_fn = func;
    168 	irqh->ih_arg = arg;
    169 
    170 	irq->intr_refcnt++;
    171 	TAILQ_INSERT_TAIL(&irq->intr_handlers, irqh, ih_next);
    172 
    173 	/*
    174 	 * XXX interrupt_distribute(9) assumes that any interrupt
    175 	 * handle can be used as an input to the MD interrupt_distribute
    176 	 * implementationm, so we are forced to return the handle
    177 	 * we got back from intr_establish().  Upshot is that the
    178 	 * input to bcm2835_icu_fdt_disestablish() is ambiguous for
    179 	 * shared IRQs, rendering them un-disestablishable.
    180 	 */
    181 
    182 	return irqh;
    183 }
    184 
    185 
    186 static void *
    187 intc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    188     int (*func)(void *), void *arg, const char *xname)
    189 {
    190 	struct intc_fdt_softc * const sc = device_private(dev);
    191 
    192 	/*
    193 	 * 1st (and only) cell is the interrupt source, e.g.
    194 	 *  1  IRQ_SUPERVISOR_SOFTWARE
    195 	 *  5  IRQ_SUPERVISOR_TIMER
    196 	 *  9  IRQ_SUPERVISOR_EXTERNAL
    197 	 */
    198 
    199 	const u_int source = be32toh(specifier[0]);
    200 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    201 
    202 	return intc_intr_establish(sc, source, ipl, mpsafe, func, arg, xname);
    203 }
    204 
    205 static void
    206 intc_fdt_disestablish(device_t dev, void *ih)
    207 {
    208 #if 0
    209 	struct intc_fdt_softc * const sc = device_private(dev);
    210 #endif
    211 
    212 }
    213 
    214 static bool
    215 intc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    216 {
    217 	if (!specifier)
    218 		return false;
    219 
    220 	struct intc_fdt_softc * const sc = device_private(dev);
    221 	if (sc->sc_ci == NULL)
    222 		return false;
    223 
    224 	const u_int source = be32toh(specifier[0]);
    225 	snprintf(buf, buflen, "cpu%lu/%s #%u", sc->sc_hartid,
    226 	    intc_sources[source], source);
    227 
    228 	return true;
    229 }
    230 
    231 
    232 struct fdtbus_interrupt_controller_func intc_fdt_funcs = {
    233 	.establish = intc_fdt_establish,
    234 	.disestablish = intc_fdt_disestablish,
    235 	.intrstr = intc_fdt_intrstr
    236 };
    237 
    238 
    239 static void
    240 intc_intr_handler(struct trapframe *tf, register_t epc, register_t status,
    241     register_t cause)
    242 {
    243 	const int ppl = splhigh();
    244 	struct cpu_info * const ci = curcpu();
    245 	unsigned long pending;
    246 	int ipl;
    247 
    248 	KASSERT(CAUSE_INTERRUPT_P(cause));
    249 
    250 	struct intc_fdt_softc * const sc = ci->ci_intcsoftc;
    251 
    252 	ci->ci_intr_depth++;
    253 	ci->ci_data.cpu_nintr++;
    254 
    255 	while (ppl < (ipl = splintr(&pending))) {
    256 		if (pending == 0)
    257 			continue;
    258 
    259 		splx(ipl);
    260 
    261 		int source = ffs(pending) - 1;
    262 		struct intc_irq *irq = sc->sc_irq[source];
    263 		sc->sc_evs[source].ev_count++;
    264 
    265 		KASSERTMSG(irq != NULL, "source %d\n", source);
    266 
    267 		if (irq) {
    268 			struct intc_irqhandler *iih;
    269 
    270 			bool mpsafe =
    271 			    source != IRQ_SUPERVISOR_EXTERNAL ||
    272 			    (irq->intr_istflags & IST_MPSAFE) != 0;
    273 			struct clockframe cf = {
    274 				.cf_epc = epc,
    275 				.cf_status = status,
    276 				.cf_intr_depth = ci->ci_intr_depth
    277 			};
    278 
    279 			if (!mpsafe) {
    280 				KERNEL_LOCK(1, NULL);
    281 			}
    282 
    283 			TAILQ_FOREACH(iih, &irq->intr_handlers, ih_next) {
    284 				int handled =
    285 				    iih->ih_fn(iih->ih_arg ? iih->ih_arg : &cf);
    286 				if (handled)
    287 					break;
    288 			}
    289 
    290 			if (!mpsafe) {
    291 				KERNEL_UNLOCK_ONE(NULL);
    292 			}
    293 		}
    294 		splhigh();
    295 	}
    296 	ci->ci_intr_depth--;
    297 	splx(ppl);
    298 }
    299 
    300 
    301 
    302 static int
    303 intc_match(device_t parent, cfdata_t cf, void *aux)
    304 {
    305 	struct fdt_attach_args * const faa = aux;
    306 	return of_compatible_match(faa->faa_phandle, compat_data);
    307 }
    308 
    309 static void
    310 intc_attach(device_t parent, device_t self, void *aux)
    311 {
    312 	const struct fdt_attach_args * const faa = aux;
    313 	const int phandle = faa->faa_phandle;
    314 
    315 	int error = fdtbus_register_interrupt_controller(self, phandle,
    316             &intc_fdt_funcs);
    317 	if (error) {
    318 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    319 		return;
    320 	}
    321 
    322 	struct cpu_info * const ci = device_private(parent);
    323 	if (ci == NULL) {
    324 		aprint_naive(": disabled\n");
    325 		aprint_normal(": disabled\n");
    326 		return;
    327 	}
    328 	aprint_naive("\n");
    329 	aprint_normal(": local interrupt controller\n");
    330 
    331 	struct intc_fdt_softc * const sc = device_private(self);
    332 
    333 	riscv_intr_set_handler(intc_intr_handler);
    334 
    335 	sc->sc_dev = self;
    336 	sc->sc_ci = ci;
    337 	sc->sc_hartid = ci->ci_cpuid;
    338 	ci->ci_intcsoftc = sc;
    339 
    340 	intc_intr_establish(sc, IRQ_SUPERVISOR_TIMER, IPL_SCHED, IST_MPSAFE,
    341 	    riscv_timer_intr, NULL, "clock");
    342 #ifdef MULTIPROCESSOR
    343 	intc_intr_establish(sc, IRQ_SUPERVISOR_SOFTWARE, IPL_HIGH, IST_MPSAFE,
    344 	    riscv_ipi_intr, NULL, "ipi");
    345 #endif
    346 }
    347 
    348 CFATTACH_DECL_NEW(intc_fdt, sizeof(struct intc_fdt_softc),
    349 	intc_match, intc_attach, NULL, NULL);
    350