Home | History | Annotate | Line # | Download | only in virt68k
      1 /*	$NetBSD: intr.c,v 1.6 2026/11/13 21:14:18 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 2023 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass, Gordon W. Ross, and Jason R. Thorpe.
      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 /*
     33  * Link and dispatch interrupts.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.6 2026/11/13 21:14:18 thorpej Exp $");
     38 
     39 #define _VIRT68K_INTR_PRIVATE
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kmem.h>
     44 #include <sys/vmmeter.h>
     45 #include <sys/device.h>
     46 #include <sys/cpu.h>
     47 #include <sys/bus.h>
     48 #include <sys/intr.h>
     49 
     50 #include <machine/vectors.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <dev/goldfish/gfpicvar.h>
     55 
     56 struct intrhand {
     57 	LIST_ENTRY(intrhand)	ih_link;
     58 	int			(*ih_func)(void *);
     59 	void			*ih_arg;
     60 	struct evcnt		*ih_evcnt;
     61 	int			ih_irq;
     62 };
     63 
     64 static const char * const cpu_irq_group = "cpu irq";
     65 
     66 static const char * const pic_irq_names[] = {
     67 	"irq 1",  "irq 2",  "irq 3",  "irq 4",
     68 	"irq 5",  "irq 6",  "irq 7",  "irq 8",
     69 	"irq 9",  "irq 10", "irq 11", "irq 12",
     70 	"irq 13", "irq 14", "irq 15", "irq 16",
     71 	"irq 17", "irq 18", "irq 19", "irq 20",
     72 	"irq 21", "irq 22", "irq 23", "irq 24",
     73 	"irq 25", "irq 26", "irq 27", "irq 28",
     74 	"irq 29", "irq 30", "irq 31", "irq 32",
     75 };
     76 
     77 #define	PIC_EVCNTS(g)							  \
     78 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[0]), \
     79 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[1]), \
     80 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[2]), \
     81 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[3]), \
     82 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[4]), \
     83 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[5]), \
     84 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[6]), \
     85 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[7]), \
     86 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[8]), \
     87 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[9]), \
     88 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[10]),\
     89 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[11]),\
     90 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[12]),\
     91 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[13]),\
     92 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[14]),\
     93 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[15]),\
     94 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[16]),\
     95 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[17]),\
     96 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[18]),\
     97 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[19]),\
     98 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[20]),\
     99 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[21]),\
    100 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[22]),\
    101 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[23]),\
    102 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[24]),\
    103 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[25]),\
    104 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[26]),\
    105 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[27]),\
    106 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[28]),\
    107 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[29]),\
    108 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[30]),\
    109 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, NULL, pic_irq_names[31])
    110 
    111 static struct evcnt intr_evcnt[] = {
    112 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "spur"),
    113 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "lev1"),
    114 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "lev2"),
    115 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "lev3"),
    116 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "lev4"),
    117 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "lev5"),
    118 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "lev6"),
    119 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, cpu_irq_group, "nmi"),
    120 	PIC_EVCNTS(pic_irq_groups[0]),
    121 	PIC_EVCNTS(pic_irq_groups[1]),
    122 	PIC_EVCNTS(pic_irq_groups[2]),
    123 	PIC_EVCNTS(pic_irq_groups[3]),
    124 	PIC_EVCNTS(pic_irq_groups[4]),
    125 	PIC_EVCNTS(pic_irq_groups[5]),
    126 };
    127 __CTASSERT(__arraycount(intr_evcnt) == NIRQ);
    128 
    129 #define	IHLIST_COUNT	 (NIRQ - IRQ_PIC_BASE)
    130 #define	IRQ_TO_IHLIST(x) ((x) - IRQ_PIC_BASE)
    131 #define	IRQ_TO_IPL(x)	 ((IRQ_TO_IHLIST(x) >> 5) + 1)
    132 static LIST_HEAD(intrhand_list, intrhand) intrhands[IHLIST_COUNT];
    133 
    134 static struct gfpic_softc *pics[NPIC];
    135 
    136 static inline int
    137 ipl_to_pic(const int ipl)
    138 {
    139 	if (__predict_true(ipl >= 1 && ipl <= 6)) {
    140 		return (ipl - 1);
    141 	}
    142 	return -1;
    143 }
    144 
    145 /*
    146  * intr_init --
    147  *	Initialize the interrupt subsystem.
    148  */
    149 void
    150 intr_init(void)
    151 {
    152 	int i;
    153 
    154 	for (i = 0; i < IHLIST_COUNT; i++) {
    155 		LIST_INIT(&intrhands[i]);
    156 	}
    157 
    158 	/*
    159 	 * Attach the CPU IRQ event counters first.  The
    160 	 * PIC event counters will be attached as the PICs
    161 	 * are registered with us.
    162 	 */
    163 	for (i = 0; i < IRQ_PIC_BASE; i++) {
    164 		evcnt_attach_static(&intr_evcnt[i]);
    165 	}
    166 }
    167 
    168 /*
    169  * intr_register_pic --
    170  *	Register a Goldfish PIC at the specified CPU IRQ.
    171  */
    172 void
    173 intr_register_pic(device_t dev, int ipl)
    174 {
    175 	const int idx = ipl_to_pic(ipl);
    176 
    177 	KASSERT(idx >= 0);
    178 
    179 	const int base = IRQ_PIC_BASE + (idx * NIRQ_PER_PIC);
    180 	int pirq;
    181 
    182 	KASSERT(pics[idx] == NULL);
    183 	pics[idx] = device_private(dev);
    184 
    185 	for (pirq = 0; pirq < NIRQ_PER_PIC; pirq++) {
    186 		intr_evcnt[base + pirq].ev_group = device_xname(dev);
    187 		evcnt_attach_static(&intr_evcnt[base + pirq]);
    188 	}
    189 }
    190 
    191 /*
    192  * intr_establish --
    193  *	Establish an interrupt handler at the specified system IRQ.
    194  *	XXX We don't do anything with the flags yet.
    195  */
    196 void *
    197 intr_establish(int (*func)(void *), void *arg, int irq, int ipl,
    198     int flags __unused)
    199 {
    200 	struct intrhand *ih;
    201 	int s;
    202 
    203 	if (irq < IRQ_PIC_BASE || irq >= NIRQ) {
    204 		return NULL;
    205 	}
    206 	if (ipl < IPL_BIO || ipl > IPL_SCHED) {
    207 		return NULL;
    208 	}
    209 
    210 	const int pic = IRQ_TO_PIC(irq);
    211 	if (pics[pic] == NULL) {
    212 		return NULL;
    213 	}
    214 
    215 	ih = kmem_zalloc(sizeof(*ih), KM_SLEEP);
    216 	ih->ih_func = func;
    217 	ih->ih_arg = arg;
    218 	ih->ih_irq = irq;
    219 	ih->ih_evcnt = &intr_evcnt[irq];
    220 
    221 	s = splhigh();
    222 	LIST_INSERT_HEAD(&intrhands[IRQ_TO_IHLIST(irq)], ih, ih_link);
    223 	gfpic_enable(pics[pic], IRQ_TO_PIRQ(irq));
    224 	splx(s);
    225 
    226 	return ih;
    227 }
    228 
    229 /*
    230  * intr_disestablish --
    231  *	Remove an interrupt handler.
    232  */
    233 void
    234 intr_disestablish(void *v)
    235 {
    236 	struct intrhand *ih = v;
    237 	const int pic = IRQ_TO_PIC(ih->ih_irq);
    238 	int s;
    239 
    240 	KASSERT(pics[pic] != NULL);
    241 
    242 	s = splhigh();
    243 	LIST_REMOVE(ih, ih_link);
    244 	if (LIST_EMPTY(&intrhands[IRQ_TO_IHLIST(ih->ih_irq)])) {
    245 		gfpic_disable(pics[pic], IRQ_TO_PIRQ(ih->ih_irq));
    246 	}
    247 	splx(s);
    248 }
    249 
    250 /*
    251  * intr_string --
    252  *	Return a string in the specified buffer describing the
    253  *	interrupt.
    254  */
    255 const char *
    256 intr_string(void *v, char *buf, size_t bufsize)
    257 {
    258 	struct intrhand *ih = v;
    259 	const int ipl = IRQ_TO_IPL(ih->ih_irq);
    260 
    261 	snprintf(buf, bufsize, "%s %s (IPL %d)", ih->ih_evcnt->ev_group,
    262 	    ih->ih_evcnt->ev_name, ipl);
    263 	return buf;
    264 }
    265 
    266 /* Auto-vectored interrupts start at vector 0x18. */
    267 #define	VEC_AVINTR	0x18
    268 
    269 void
    270 m68k_intr_autovec(struct clockframe frame)
    271 {
    272 	const int ipl = VECO_TO_VECI(frame.cf_vo) - VEC_AVINTR;
    273 	const int pic = ipl_to_pic(ipl);
    274 	int pirq;
    275 
    276 	if (__predict_false(pic < 0)) {
    277 		return;
    278 	}
    279 
    280 	if (pics[pic] == NULL) {
    281 		printf("Interrupt without a cause on CPU ipl %d\n", ipl);
    282 		return;
    283 	}
    284 
    285 	const int base = pic * NIRQ_PER_PIC;
    286 	struct intrhand *ih;
    287 	bool rv = false;
    288 
    289 	while ((pirq = gfpic_pending(pics[pic])) >= 0) {
    290 		LIST_FOREACH(ih, &intrhands[base + pirq], ih_link) {
    291 			void *arg = ih->ih_arg ? ih->ih_arg : &frame;
    292 			if (ih->ih_func(arg)) {
    293 				ih->ih_evcnt->ev_count++;
    294 				rv = true;
    295 			}
    296 		}
    297 	}
    298 	if (!rv) {
    299 		printf("Spurious interrupt on CPU ipl %d\n", ipl);
    300 	}
    301 }
    302