Home | History | Annotate | Line # | Download | only in fdt
intc_fdt.c revision 1.1
      1 /*	$NetBSD: intc_fdt.c,v 1.1 2023/05/07 12:41:48 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 <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: intc_fdt.c,v 1.1 2023/05/07 12:41:48 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/bus.h>
     38 #include <sys/cpu.h>
     39 #include <sys/device.h>
     40 #include <sys/evcnt.h>
     41 #include <sys/kmem.h>
     42 #include <sys/intr.h>
     43 
     44 #include <dev/fdt/fdtvar.h>
     45 
     46 #include <machine/frame.h>
     47 #include <machine/machdep.h>
     48 #include <machine/sysreg.h>
     49 
     50 static const struct device_compatible_entry compat_data[] = {
     51 	{ .compat = "riscv,cpu-intc" },
     52 	DEVICE_COMPAT_EOL
     53 };
     54 
     55 
     56 struct intc_irqhandler;
     57 struct intc_irq;
     58 struct intc_softc;
     59 
     60 typedef int (*intcih_t)(void *);
     61 
     62 struct intc_irqhandler {
     63 	struct intc_irq		 *ih_irq;
     64 	intcih_t		  ih_fn;
     65 	void			 *ih_arg;
     66 	TAILQ_ENTRY(intc_irqhandler)
     67 				  ih_next;
     68 };
     69 
     70 struct intc_irq {
     71 	struct intc_fdt_softc	*intr_sc;
     72 	void			*intr_ih;
     73 	void			*intr_arg;
     74 	int			 intr_refcnt;
     75 	int			 intr_ipl;
     76 	int			 intr_source;
     77 	int			 intr_istflags;
     78 	struct evcnt *pcpu_evs;
     79 	TAILQ_HEAD(, intc_irqhandler)
     80 				 intr_handlers;
     81 };
     82 
     83 
     84 struct intc_fdt_softc {
     85 	device_t		 sc_dev;
     86 	bus_space_tag_t		 sc_bst;
     87 	bus_space_handle_t	 sc_bsh;
     88 
     89 	struct intc_irq		*sc_irq[IRQ_NSOURCES];
     90 
     91 	struct evcnt		*sc_evs[IRQ_NSOURCES];
     92 
     93 	struct cpu_info 	*sc_ci;
     94 	cpuid_t			 sc_hartid;
     95 };
     96 
     97 static struct intc_fdt_softc *intc_sc;
     98 
     99 
    100 static void *
    101 intc_intr_establish(struct intc_fdt_softc *sc, u_int source, u_int ipl,
    102     u_int istflags, int (*func)(void *), void *arg, const char *xname)
    103 {
    104 	if (source > IRQ_NSOURCES)
    105 		return NULL;
    106 
    107 	const device_t dev = sc->sc_dev;
    108 	struct intc_irq *irq = sc->sc_irq[source];
    109 	if (irq == NULL) {
    110 		irq = kmem_alloc(sizeof(*irq), KM_SLEEP);
    111 		irq->intr_sc = sc;
    112 		irq->intr_refcnt = 0;
    113 		irq->intr_arg = arg;
    114 		irq->intr_ipl = ipl;
    115 		irq->intr_istflags = istflags;
    116 		irq->intr_source = source;
    117 		TAILQ_INIT(&irq->intr_handlers);
    118 		sc->sc_irq[source] = irq;
    119 	} else {
    120 		if (irq->intr_arg == NULL || arg == NULL) {
    121 			device_printf(dev,
    122 			    "cannot share irq with NULL-arg handler\n");
    123 			return NULL;
    124 		}
    125 		if (irq->intr_ipl != ipl) {
    126 			device_printf(dev,
    127 			    "cannot share irq with different ipl\n");
    128 			return NULL;
    129 		}
    130 		if (irq->intr_istflags != istflags) {
    131 			device_printf(dev,
    132 			    "cannot share irq between mpsafe/non-mpsafe\n");
    133 			return NULL;
    134 		}
    135 	}
    136 
    137 	struct intc_irqhandler *irqh = kmem_alloc(sizeof(*irqh), KM_SLEEP);
    138 	irqh->ih_irq = irq;
    139 	irqh->ih_fn = func;
    140 	irqh->ih_arg = arg;
    141 
    142 	irq->intr_refcnt++;
    143 	TAILQ_INSERT_TAIL(&irq->intr_handlers, irqh, ih_next);
    144 
    145 	/*
    146 	 * XXX interrupt_distribute(9) assumes that any interrupt
    147 	 * handle can be used as an input to the MD interrupt_distribute
    148 	 * implementationm, so we are forced to return the handle
    149 	 * we got back from intr_establish().  Upshot is that the
    150 	 * input to bcm2835_icu_fdt_disestablish() is ambiguous for
    151 	 * shared IRQs, rendering them un-disestablishable.
    152 	 */
    153 
    154 	return irqh;
    155 }
    156 
    157 
    158 static void *
    159 intc_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    160     int (*func)(void *), void *arg, const char *xname)
    161 {
    162 	struct intc_fdt_softc * const sc = device_private(dev);
    163 
    164 	/*
    165 	 * 1st (and only) cell is the interrupt source, e.g.
    166 	 *  1  IRQ_SUPERVISOR_SOFTWARE
    167 	 *  5  IRQ_SUPERVISOR_TIMER
    168 	 *  9  IRQ_SUPERVISOR_EXTERNAL
    169 	 */
    170 
    171 	const u_int source = be32toh(specifier[0]);
    172 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    173 
    174 	return intc_intr_establish(sc, source, ipl, mpsafe, func, arg, xname);
    175 }
    176 
    177 static void
    178 intc_fdt_disestablish(device_t dev, void *ih)
    179 {
    180 #if 0
    181 	struct intc_fdt_softc * const sc = device_private(dev);
    182 #endif
    183 
    184 }
    185 
    186 static const char * const intc_sources[IRQ_NSOURCES] = {
    187 	/* Software interrupts */
    188 	"(reserved)",
    189 	"Supervisor software",
    190 	"Virtual Supervisor software",
    191 	"Machine software",
    192 
    193 	/* Timer interrupts */
    194 	"(reserved)",
    195 	"Supervisor timer",
    196 	"Virtual Supervisor timer",
    197 	"Machine timer",
    198 
    199 	/* External interrupts */
    200 	"(reserved)",
    201 	"Supervisor external",
    202 	"Virtual Supervisor external",
    203 	"Machine external",
    204 
    205 	"(reserved)",
    206 	"Supervisor guest external.",
    207 	"(reserved)",
    208 	"(reserved)"
    209 };
    210 
    211 static bool
    212 intc_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    213 {
    214 	if (!specifier)
    215 		return false;
    216 
    217 	struct intc_fdt_softc * const sc = device_private(dev);
    218 	if (sc->sc_ci == NULL)
    219 		return false;
    220 
    221 	const u_int source = be32toh(specifier[0]);
    222 	snprintf(buf, buflen, "cpu%lu/%s #%u", sc->sc_hartid,
    223 	    intc_sources[source], source);
    224 
    225 	return true;
    226 }
    227 
    228 
    229 struct fdtbus_interrupt_controller_func intc_fdt_funcs = {
    230 	.establish = intc_fdt_establish,
    231 	.disestablish = intc_fdt_disestablish,
    232 	.intrstr = intc_fdt_intrstr
    233 };
    234 
    235 
    236 static void
    237 intc_intr_handler(struct trapframe *tf, register_t epc, register_t status,
    238     register_t cause)
    239 {
    240 	struct cpu_info * const ci = curcpu();
    241 	const int source = CAUSE_CODE(cause);
    242 
    243 	KASSERT(CAUSE_INTERRUPT_P(cause));
    244 
    245 	struct intc_fdt_softc * const sc = intc_sc;
    246 
    247 	ci->ci_intr_depth++;
    248 	struct intc_irq *irq = sc->sc_irq[source];
    249 	KASSERTMSG(irq != NULL, "source %d\n", source);
    250 	if (irq) {
    251 		struct intc_irqhandler *iih;
    252 
    253 		bool mpsafe = (irq->intr_istflags & IST_MPSAFE) != 0;
    254 		struct clockframe cf = {
    255 			.cf_epc = epc,
    256 			.cf_status = status,
    257 			.cf_intr_depth = ci->ci_intr_depth
    258 		};
    259 
    260 		if (!mpsafe) {
    261 			KERNEL_LOCK(1, NULL);
    262 		}
    263 
    264 		TAILQ_FOREACH(iih, &irq->intr_handlers, ih_next) {
    265 			int handled =
    266 			    iih->ih_fn(iih->ih_arg ? iih->ih_arg : &cf);
    267 			if (handled)
    268 				break;
    269 		}
    270 
    271 		if (!mpsafe) {
    272 			KERNEL_UNLOCK_ONE(NULL);
    273 		}
    274 	}
    275 	ci->ci_intr_depth--;
    276 }
    277 
    278 
    279 
    280 static int
    281 intc_match(device_t parent, cfdata_t cf, void *aux)
    282 {
    283 	struct fdt_attach_args * const faa = aux;
    284 	return of_compatible_match(faa->faa_phandle, compat_data);
    285 }
    286 
    287 static void
    288 intc_attach(device_t parent, device_t self, void *aux)
    289 {
    290 	const struct fdt_attach_args * const faa = aux;
    291 	const int phandle = faa->faa_phandle;
    292 
    293 	int error = fdtbus_register_interrupt_controller(self, phandle,
    294             &intc_fdt_funcs);
    295 	if (error) {
    296 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    297 		return;
    298 	}
    299 
    300 	struct cpu_info * const ci = device_private(parent);
    301 	if (ci == NULL) {
    302 		aprint_naive(": disabled\n");
    303 		aprint_normal(": disabled\n");
    304 		return;
    305 	}
    306 	aprint_naive("\n");
    307 	aprint_normal(": local interrupt controller\n");
    308 
    309 	struct intc_fdt_softc * const sc = device_private(self);
    310 	intc_sc = sc;
    311 
    312 	riscv_intr_set_handler(intc_intr_handler);
    313 
    314 	sc->sc_dev = self;
    315 	sc->sc_ci = ci;
    316 	sc->sc_hartid = ci->ci_cpuid;
    317 
    318 	intc_intr_establish(sc, IRQ_SUPERVISOR_TIMER, IPL_SCHED, IST_MPSAFE,
    319 	    riscv_timer_intr, NULL, "clock");
    320 #ifdef MULTIPROCESSOR
    321 	intc_intr_establish(sc, IRQ_SUPERVISOR_SOFTWARE, IPL_HIGH, IST_MPSAFE,
    322 	    riscv_ipi_intr, NULL, "ipi");
    323 #endif
    324 }
    325 
    326 CFATTACH_DECL_NEW(intc_fdt, sizeof(struct intc_fdt_softc),
    327 	intc_match, intc_attach, NULL, NULL);
    328