Home | History | Annotate | Line # | Download | only in m68k
      1 /*	$NetBSD: m68k_intr.c,v 1.14 2026/11/13 21:14:17 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 2023, 2024 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  * Common interrupt handling for m68k platforms.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: m68k_intr.c,v 1.14 2026/11/13 21:14:17 thorpej Exp $");
     38 
     39 #define	_M68K_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/once.h>
     49 #include <sys/intr.h>
     50 
     51 #include <machine/vectors.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #ifdef __HAVE_M68K_INTR_VECTORED
     56 #ifndef MACHINE_USERVEC_START
     57 #define	MACHINE_USERVEC_START	VECI_USRVEC_START
     58 #endif
     59 
     60 #define	NVECHANDS	(NVECTORS - MACHINE_USERVEC_START)
     61 
     62 #ifndef INTR_FREEVEC
     63 #define	INTR_FREEVEC	badtrap
     64 #endif
     65 
     66 extern char INTR_FREEVEC[];
     67 extern char intrstub_vectored[];
     68 #endif /* __HAVE_M68K_INTR_VECTORED */
     69 
     70 /* A dummy event counter where interrupt stats go to die. */
     71 static struct evcnt bitbucket;
     72 
     73 static struct m68k_intrhand_list m68k_intrhands_autovec[NAUTOVECTORS];
     74 #ifdef __HAVE_M68K_INTR_VECTORED
     75 static struct m68k_intrhand *m68k_intrhands_vectored[NVECHANDS];
     76 #endif
     77 
     78 #ifndef __HAVE_LEGACY_INTRCNT
     79 #ifndef MACHINE_INTREVCNT_NAMES
     80 #define	MACHINE_INTREVCNT_NAMES						\
     81 	{ "spur", "lev1", "lev2", "lev3", "lev4", "lev5", "lev6", "nmi" }
     82 #endif
     83 static const char * const m68k_intr_evcnt_names[] = MACHINE_INTREVCNT_NAMES;
     84 __CTASSERT(__arraycount(m68k_intr_evcnt_names) == NAUTOVECTORS);
     85 static const char m68k_intr_evcnt_group[] = "cpu";
     86 
     87 #define	INTRCNT_INIT(x)							\
     88 	EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, m68k_intr_evcnt_group,	\
     89 			  m68k_intr_evcnt_names[(x)])
     90 
     91 struct evcnt m68k_intr_evcnt[] = {
     92 	INTRCNT_INIT(0), INTRCNT_INIT(1), INTRCNT_INIT(2), INTRCNT_INIT(3),
     93 	INTRCNT_INIT(4), INTRCNT_INIT(5), INTRCNT_INIT(6), INTRCNT_INIT(7),
     94 };
     95 __CTASSERT(__arraycount(m68k_intr_evcnt) == NAUTOVECTORS);
     96 
     97 #undef INTRCNT_INIT
     98 #endif /* __HAVE_LEGACY_INTRCNT */
     99 
    100 const uint16_t ipl2psl_table[NIPL] = {
    101 	[IPL_NONE]	 = PSL_S | PSL_IPL0,
    102 	[IPL_SOFTBIO]	 = PSL_S | MACHINE_PSL_IPL_SOFTBIO,
    103 	[IPL_SOFTCLOCK]	 = PSL_S | MACHINE_PSL_IPL_SOFTCLOCK,
    104 	[IPL_SOFTNET]	 = PSL_S | MACHINE_PSL_IPL_SOFTNET,
    105 	[IPL_SOFTSERIAL] = PSL_S | MACHINE_PSL_IPL_SOFTSERIAL,
    106 	[IPL_VM]	 = PSL_S | MACHINE_PSL_IPL_VM,
    107 	[IPL_SCHED]	 = PSL_S | MACHINE_PSL_IPL_SCHED,
    108 	[IPL_HIGH]	 = PSL_S | PSL_IPL7,
    109 };
    110 
    111 /*
    112  * m68k_spurintr --
    113  *	Interrupt handler for the "spurious interrupt" that comes in
    114  *	on auto-vector level 0.  All we do is claim it; it gets counted
    115  *	during the normal course of auto-vector interrupt handling.
    116  */
    117 static int
    118 m68k_spurintr(void *arg)
    119 {
    120 	return 1;
    121 }
    122 
    123 static struct m68k_intrhand m68k_spurintr_ih = {
    124 	.ih_func  = m68k_spurintr,
    125 	.ih_arg   = m68k_spurintr,
    126 	.ih_evcnt = &bitbucket,
    127 };
    128 
    129 static struct m68k_intrhand *
    130 m68k_ih_stdalloc(int km_flag)
    131 {
    132 	return kmem_zalloc(sizeof(struct m68k_intrhand), km_flag);
    133 }
    134 
    135 static void
    136 m68k_ih_stdfree(struct m68k_intrhand *ih)
    137 {
    138 	kmem_free(ih, sizeof(*ih));
    139 }
    140 
    141 static const struct m68k_ih_allocfuncs m68k_ih_stdallocfuncs = {
    142 	.alloc = m68k_ih_stdalloc,
    143 	.free  = m68k_ih_stdfree,
    144 };
    145 
    146 static const struct m68k_ih_allocfuncs *ih_allocfuncs;
    147 
    148 static struct m68k_intrhand *
    149 m68k_ih_alloc(void)
    150 {
    151 	KASSERT(ih_allocfuncs != NULL);
    152 	return ih_allocfuncs->alloc(KM_SLEEP);
    153 }
    154 
    155 static void
    156 m68k_ih_free(struct m68k_intrhand *ih)
    157 {
    158 	KASSERT(ih_allocfuncs != NULL);
    159 	if (__predict_true(ih != &m68k_spurintr_ih)) {
    160 		ih_allocfuncs->free(ih);
    161 	}
    162 }
    163 
    164 #ifdef __HAVE_M68K_INTR_VECTORED
    165 
    166 #define	INTRVEC_SLOT(vec)	\
    167 	(&m68k_intrhands_vectored[(vec) - MACHINE_USERVEC_START])
    168 
    169 #define	m68k_intrvec_handler(vec)	(*INTRVEC_SLOT(vec))
    170 
    171 static bool
    172 m68k_intrvec_add(struct m68k_intrhand *ih)
    173 {
    174 	if (ih->ih_vec < MACHINE_USERVEC_START || ih->ih_vec >= NVECTORS) {
    175 		aprint_error("%s: vector=0x%x (invalid)\n", __func__,
    176 		    ih->ih_vec);
    177 		return false;
    178 	}
    179 
    180 	struct m68k_intrhand **slot =
    181 	    &m68k_intrhands_vectored[ih->ih_vec - MACHINE_USERVEC_START];
    182 
    183 	if (*slot != NULL) {
    184 		aprint_error("%s: vector=0x%x (in use)\n", __func__,
    185 		    ih->ih_vec);
    186 		return false;
    187 	}
    188 
    189 	if (vec_get_entry(ih->ih_vec) != INTR_FREEVEC) {
    190 		aprint_error("%s: vector=0x%x (unavailable)\n", __func__,
    191 		    ih->ih_vec);
    192 		return false;
    193 	}
    194 
    195 	*slot = ih;
    196 	vec_set_entry(ih->ih_vec, intrstub_vectored);
    197 	return true;
    198 }
    199 
    200 static void
    201 m68k_intrvec_remove(struct m68k_intrhand *ih)
    202 {
    203 	KASSERT(ih->ih_vec >= MACHINE_USERVEC_START);
    204 	KASSERT(ih->ih_vec < NVECTORS);
    205 
    206 	struct m68k_intrhand **slot =
    207 	    &m68k_intrhands_vectored[ih->ih_vec - MACHINE_USERVEC_START];
    208 
    209 	KASSERT(*slot == ih);
    210 	KASSERT(vec_get_entry(ih->ih_vec) == intrstub_vectored);
    211 
    212 	vec_set_entry(ih->ih_vec, INTR_FREEVEC);
    213 	*slot = NULL;
    214 }
    215 
    216 /* XXX This is horrible and should burn to the ground. */
    217 void *
    218 m68k_intrvec_intrhand(int vec)
    219 {
    220 	KASSERT(vec >= MACHINE_USERVEC_START);
    221 	KASSERT(vec < NVECTORS);
    222 
    223 	return m68k_intrhands_vectored[vec - MACHINE_USERVEC_START];
    224 }
    225 
    226 #endif /* __HAVE_M68K_INTR_VECTORED */
    227 
    228 /*
    229  * m68k_intr_init --
    230  *	Initialize the interrupt subsystem.
    231  */
    232 void
    233 m68k_intr_init(const struct m68k_ih_allocfuncs *allocfuncs)
    234 {
    235 	int i;
    236 
    237 	KASSERT(ih_allocfuncs == NULL);
    238 	if (allocfuncs == NULL) {
    239 		ih_allocfuncs = &m68k_ih_stdallocfuncs;
    240 	} else {
    241 		ih_allocfuncs = allocfuncs;
    242 	}
    243 
    244 	for (i = 0; i < NAUTOVECTORS; i++) {
    245 		LIST_INIT(&m68k_intrhands_autovec[i]);
    246 #ifndef __HAVE_LEGACY_INTRCNT
    247 		evcnt_attach_static(&m68k_intr_evcnt[i]);
    248 #endif
    249 	}
    250 	LIST_INSERT_HEAD(&m68k_intrhands_autovec[0],
    251 	    &m68k_spurintr_ih, ih_link);
    252 }
    253 
    254 /*
    255  * m68k_intr_establish --
    256  *	Establish an interrupt handler at the specified vector.
    257  *	XXX We don't do anything with the flags yet.
    258  */
    259 void *
    260 m68k_intr_establish(int (*func)(void *), void *arg, struct evcnt *ev,
    261     int vec, int ipl, int isrpri, int flags __unused)
    262 {
    263 	struct m68k_intrhand *ih;
    264 	int s;
    265 
    266 	/*
    267 	 * If a platform doesn't want special behavior, we don't
    268 	 * require them to call m68k_intr_init(); we just handle
    269 	 * it here.
    270 	 *
    271 	 * XXX m68k_intr_init() might be called really early, so
    272 	 * XXX can't use a once control.
    273 	 */
    274 	if (__predict_false(ih_allocfuncs == NULL)) {
    275 		m68k_intr_init(NULL);
    276 	}
    277 
    278 	/* These are m68k IPLs, not IPL_* values. */
    279 	if (ipl < 0 || ipl > 7) {
    280 		return NULL;
    281 	}
    282 
    283 #ifdef __HAVE_M68K_INTR_VECTORED
    284 	KASSERT(vec >= 0);
    285 	KASSERT(vec < NVECTORS);
    286 #else
    287 	KASSERT(vec == 0);
    288 #endif /* __HAVE_M68K_INTR_VECTORED */
    289 
    290 	ih = m68k_ih_alloc();
    291 	ih->ih_func = func;
    292 	ih->ih_arg = arg;
    293 	ih->ih_vec = vec;
    294 	ih->ih_ipl = ipl;
    295 	ih->ih_isrpri = isrpri;
    296 	if ((ih->ih_evcnt = ev) == NULL) {
    297 		ih->ih_evcnt = &bitbucket;
    298 	}
    299 
    300 #ifdef __HAVE_M68K_INTR_VECTORED
    301 	if (vec != 0) {
    302 		if (vec_get_entry(vec) != INTR_FREEVEC) {
    303 			m68k_ih_free(ih);
    304 			return NULL;
    305 		}
    306 		if (! m68k_intrvec_add(ih)) {
    307 			m68k_ih_free(ih);
    308 			return NULL;
    309 		}
    310 		return ih;
    311 	}
    312 #endif
    313 
    314 	/*
    315 	 * Some devices are particularly sensitive to interrupt
    316 	 * handling latency.  Unbuffered serial ports, for example,
    317 	 * can lose data if their interrupts aren't handled with
    318 	 * reasonable speed.  For this reason, we sort interrupt
    319 	 * handlers by an abstract "ISR" priority, inserting higher-
    320 	 * priority interrupts before lower-priority interrupts.
    321 	 */
    322 	struct m68k_intrhand_list * const list = &m68k_intrhands_autovec[ipl];
    323 	struct m68k_intrhand *curih;
    324 
    325 	s = splhigh();
    326 	if (LIST_EMPTY(list)) {
    327 		LIST_INSERT_HEAD(list, ih, ih_link);
    328 		goto done;
    329 	}
    330 	for (curih = LIST_FIRST(list);
    331 	     LIST_NEXT(curih, ih_link) != NULL;
    332 	     curih = LIST_NEXT(curih, ih_link)) {
    333 		if (ih->ih_isrpri > curih->ih_isrpri) {
    334 			LIST_INSERT_BEFORE(curih, ih, ih_link);
    335 			goto done;
    336 		}
    337 	}
    338 	LIST_INSERT_AFTER(curih, ih, ih_link);
    339  done:
    340 	splx(s);
    341 
    342 	return ih;
    343 }
    344 
    345 /*
    346  * m68k_intr_disestablish --
    347  *	Remove an interrupt handler.  Returns true if the handler
    348  *	list for this vector is now empty.
    349  */
    350 bool
    351 m68k_intr_disestablish(void *v)
    352 {
    353 	struct m68k_intrhand *ih = v;
    354 	int s;
    355 	bool empty;
    356 
    357 #ifdef __HAVE_M68K_INTR_VECTORED
    358 	if (ih->ih_vec != 0) {
    359 		KASSERT(vec_get_entry(ih->ih_vec) == intrstub_vectored);
    360 		m68k_intrvec_remove(ih);
    361 		empty = true;
    362 	} else
    363 #endif
    364 	{
    365 		s = splhigh();
    366 		LIST_REMOVE(ih, ih_link);
    367 		empty = LIST_EMPTY(&m68k_intrhands_autovec[ih->ih_ipl]);
    368 		splx(s);
    369 	}
    370 
    371 	m68k_ih_free(ih);
    372 
    373 	return empty;
    374 }
    375 
    376 void	m68k_intr_autovec(struct clockframe);
    377 
    378 #ifndef MACHINE_AUTOVEC_IGNORE_STRAY
    379 #define	MACHINE_AUTOVEC_IGNORE_STRAY(ipl)	0
    380 #endif
    381 
    382 /*
    383  * m68k_intr_autovec --
    384  *	Run the interrupt handlers for an auto-vectored interrupt.
    385  *	Called from the assembly glue in m68k_intr_stubs.s
    386  */
    387 void
    388 m68k_intr_autovec(struct clockframe frame)
    389 {
    390 	const int ipl = VECO_TO_VECI(frame.cf_vo) - VECI_INTRAV0;
    391 	struct m68k_intrhand *ih;
    392 	bool rv = false;
    393 
    394 	m68k_count_intr(ipl);
    395 
    396 	LIST_FOREACH(ih, &m68k_intrhands_autovec[ipl], ih_link) {
    397 		void *arg = ih->ih_arg ? ih->ih_arg : &frame;
    398 		if (ih->ih_func(arg)) {
    399 			ih->ih_evcnt->ev_count++;
    400 			rv = true;
    401 		}
    402 	}
    403 	if (!rv && !MACHINE_AUTOVEC_IGNORE_STRAY(ipl)) {
    404 		printf("Stray level %d interrupt\n", ipl);
    405 	}
    406 
    407 	ATOMIC_CAS_CHECK(&frame);
    408 }
    409 
    410 #ifdef __HAVE_M68K_INTR_VECTORED
    411 void	m68k_intr_vectored(struct clockframe);
    412 
    413 /*
    414  * m68k_intr_vectored --
    415  *	Run a vectored interrupt handler.
    416  *	Called from the assembly glue in m68k_intr_stubs.s
    417  */
    418 void
    419 m68k_intr_vectored(struct clockframe frame)
    420 {
    421 	const int vec = VECO_TO_VECI(frame.cf_vo);
    422 	const int ipl = (getsr() >> 8) & 7;
    423 	struct m68k_intrhand *ih;
    424 
    425 	m68k_count_intr(ipl);
    426 
    427 #ifdef DIAGNOSTIC
    428 	if (vec < MACHINE_USERVEC_START || vec >= NVECTORS) {
    429 		printf("%s: vector=0x%x (invalid)\n", __func__, vec);
    430 		goto out;
    431 	}
    432 #endif
    433 	ih = m68k_intrvec_handler(vec);
    434 	if (ih == NULL) {
    435 		printf("%s: vector=0x%x (no handler?)\n", __func__, vec);
    436 		vec_set_entry(vec, INTR_FREEVEC);
    437 	}
    438 
    439 	if (__predict_true((*ih->ih_func)(ih->ih_arg ? ih->ih_arg
    440 						     : &frame) != 0)) {
    441 		ih->ih_evcnt->ev_count++;
    442 	} else {
    443 		printf("Stray level %d interrupt vector=0x%x\n",
    444 		    ipl, vec);
    445 	}
    446 #ifdef DIAGNOSTIC
    447  out:
    448 #endif
    449 	ATOMIC_CAS_CHECK(&frame);
    450 }
    451 #endif /* __HAVE_M68K_INTR_VECTORED */
    452