Home | History | Annotate | Line # | Download | only in mipssim
      1  1.2  reinoud /* $NetBSD: mipssim_intr.c,v 1.2 2021/02/15 22:39:46 reinoud Exp $ */
      2  1.1   simonb 
      3  1.1   simonb /*-
      4  1.1   simonb  * Copyright (c) 2014 Michael Lorenz
      5  1.1   simonb  * All rights reserved.
      6  1.1   simonb  *
      7  1.1   simonb  * Redistribution and use in source and binary forms, with or without
      8  1.1   simonb  * modification, are permitted provided that the following conditions
      9  1.1   simonb  * are met:
     10  1.1   simonb  * 1. Redistributions of source code must retain the above copyright
     11  1.1   simonb  *    notice, this list of conditions and the following disclaimer.
     12  1.1   simonb  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   simonb  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   simonb  *    documentation and/or other materials provided with the distribution.
     15  1.1   simonb  *
     16  1.1   simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1   simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1   simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1   simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1   simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1   simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1   simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1   simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1   simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1   simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1   simonb  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1   simonb  */
     28  1.1   simonb 
     29  1.1   simonb #include <sys/cdefs.h>
     30  1.2  reinoud __KERNEL_RCSID(0, "$NetBSD: mipssim_intr.c,v 1.2 2021/02/15 22:39:46 reinoud Exp $");
     31  1.1   simonb 
     32  1.1   simonb #define __INTR_PRIVATE
     33  1.1   simonb 
     34  1.1   simonb #include <sys/param.h>
     35  1.1   simonb #include <sys/cpu.h>
     36  1.1   simonb #include <sys/kernel.h>
     37  1.1   simonb #include <sys/systm.h>
     38  1.2  reinoud #include <sys/kmem.h>
     39  1.1   simonb 
     40  1.1   simonb #include <mips/locore.h>
     41  1.1   simonb #include <machine/intr.h>
     42  1.1   simonb 
     43  1.1   simonb /*
     44  1.1   simonb  * This is a mask of bits to clear in the SR when we go to a
     45  1.1   simonb  * given hardware interrupt priority level.
     46  1.1   simonb  */
     47  1.1   simonb static const struct ipl_sr_map mipssim_ipl_sr_map = {
     48  1.1   simonb     .sr_bits = {
     49  1.1   simonb 	[IPL_NONE] =		0,
     50  1.1   simonb 	[IPL_SOFTCLOCK] =	MIPS_SOFT_INT_MASK_0,
     51  1.1   simonb 	[IPL_SOFTNET] =		MIPS_SOFT_INT_MASK,
     52  1.1   simonb 	[IPL_VM] =		MIPS_SOFT_INT_MASK
     53  1.2  reinoud 				    | MIPS_INT_MASK_0 | MIPS_INT_MASK_1
     54  1.2  reinoud 				    | MIPS_INT_MASK_2,
     55  1.1   simonb 	[IPL_SCHED] =		MIPS_SOFT_INT_MASK
     56  1.2  reinoud 				    | MIPS_INT_MASK_0 | MIPS_INT_MASK_1
     57  1.2  reinoud 				    | MIPS_INT_MASK_2 | MIPS_INT_MASK_5,
     58  1.1   simonb 	[IPL_DDB] =		MIPS_INT_MASK,
     59  1.1   simonb 	[IPL_HIGH] =		MIPS_INT_MASK,
     60  1.1   simonb     },
     61  1.1   simonb };
     62  1.1   simonb 
     63  1.1   simonb /* XXX - add evcnt bits to <machine/intr.h> struct evbmips_intrhand */
     64  1.1   simonb struct intrhand {
     65  1.1   simonb 	LIST_ENTRY(intrhand) ih_q;
     66  1.1   simonb 	int (*ih_func)(void *);
     67  1.1   simonb 	void *ih_arg;
     68  1.1   simonb 	int ih_irq;
     69  1.1   simonb };
     70  1.1   simonb 
     71  1.1   simonb 
     72  1.1   simonb /*
     73  1.1   simonb  * Use CPU interrupts INT0 .. INT4.  Clock interrupts (INT5)
     74  1.1   simonb  * are handled in cpu_intr() before evbmips_iointr() is called.
     75  1.1   simonb  */
     76  1.1   simonb #define	NINTR		5	/* MIPS INT0 - INT4 */
     77  1.1   simonb 
     78  1.2  reinoud LIST_HEAD(intrlist, intrhand) intrs[NINTR];
     79  1.2  reinoud struct evcnt ih_count[NINTR];
     80  1.2  reinoud 
     81  1.1   simonb const char * const intrnames[NINTR] = {
     82  1.1   simonb 	"int 0 (mipsnet)",
     83  1.2  reinoud 	"int 1 (virtio)",
     84  1.1   simonb 	"int 2 (uart)",
     85  1.1   simonb 	"int 3 (unused)",
     86  1.1   simonb 	"int 4 (unused)",
     87  1.1   simonb };
     88  1.1   simonb 
     89  1.1   simonb void mipssim_irq(int);
     90  1.1   simonb 
     91  1.1   simonb void
     92  1.1   simonb evbmips_intr_init(void)
     93  1.1   simonb {
     94  1.1   simonb 	int i;
     95  1.1   simonb 
     96  1.1   simonb 	ipl_sr_map = mipssim_ipl_sr_map;
     97  1.1   simonb 
     98  1.1   simonb 	/* zero all handlers */
     99  1.1   simonb 	for (i = 0; i < NINTR; i++) {
    100  1.2  reinoud 		LIST_INIT(&intrs[i]);
    101  1.2  reinoud 		evcnt_attach_dynamic(&ih_count[i], EVCNT_TYPE_INTR,
    102  1.1   simonb 		    NULL, "cpu", intrnames[i]);
    103  1.1   simonb 	}
    104  1.1   simonb }
    105  1.1   simonb 
    106  1.1   simonb void
    107  1.1   simonb evbmips_iointr(int ipl, uint32_t ipending, struct clockframe *cf)
    108  1.1   simonb {
    109  1.2  reinoud 	struct intrlist *list;
    110  1.1   simonb 
    111  1.1   simonb 	for (int level = NINTR - 1; level >= 0; level--) {
    112  1.1   simonb 		struct intrhand *ih;
    113  1.1   simonb 
    114  1.1   simonb 		if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
    115  1.1   simonb 			continue;
    116  1.1   simonb 
    117  1.2  reinoud 		ih_count[level].ev_count++;
    118  1.2  reinoud 		list = &intrs[level];
    119  1.1   simonb 
    120  1.2  reinoud 		LIST_FOREACH(ih, list, ih_q) {
    121  1.2  reinoud 			if (ih->ih_func) {
    122  1.2  reinoud 				(*ih->ih_func)(ih->ih_arg);
    123  1.2  reinoud 			}
    124  1.1   simonb 		}
    125  1.1   simonb 	}
    126  1.1   simonb }
    127  1.1   simonb 
    128  1.1   simonb void *
    129  1.1   simonb evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
    130  1.1   simonb {
    131  1.2  reinoud 	struct intrlist *list;
    132  1.1   simonb 	struct intrhand *ih;
    133  1.1   simonb 	int s;
    134  1.1   simonb 
    135  1.1   simonb 	if ((irq < 0) || (irq >= NINTR)) {
    136  1.1   simonb 		aprint_error("%s: invalid irq %d\n", __func__, irq);
    137  1.1   simonb 		return NULL;
    138  1.1   simonb 	}
    139  1.1   simonb 
    140  1.2  reinoud 	list = &intrs[irq];
    141  1.2  reinoud 	ih = kmem_alloc(sizeof(struct intrhand), KM_SLEEP);
    142  1.1   simonb 
    143  1.1   simonb 	s = splhigh();
    144  1.2  reinoud 
    145  1.1   simonb 	ih->ih_func = func;
    146  1.1   simonb 	ih->ih_arg = arg;
    147  1.2  reinoud 	ih->ih_irq = irq;
    148  1.2  reinoud 	LIST_INSERT_HEAD(list, ih, ih_q);
    149  1.1   simonb 
    150  1.1   simonb 	/* now enable the IRQ (nothing to do here?) */
    151  1.1   simonb 
    152  1.1   simonb 	splx(s);
    153  1.1   simonb 
    154  1.1   simonb 	return ih;
    155  1.1   simonb }
    156  1.1   simonb 
    157  1.1   simonb void
    158  1.1   simonb evbmips_intr_disestablish(void *cookie)
    159  1.1   simonb {
    160  1.1   simonb 	panic("untested %s", __func__);	/* XXX! */
    161  1.1   simonb 
    162  1.1   simonb 	struct intrhand *ih = cookie;
    163  1.1   simonb 	int s;
    164  1.1   simonb 
    165  1.1   simonb 	s = splhigh();
    166  1.1   simonb 
    167  1.1   simonb 	/* now disable the IRQ (nothing to do here?) */
    168  1.1   simonb 
    169  1.1   simonb 	ih->ih_func = NULL;
    170  1.1   simonb 	ih->ih_arg = NULL;
    171  1.2  reinoud 	LIST_REMOVE(ih, ih_q);
    172  1.2  reinoud 	kmem_free(ih, sizeof(struct intrhand));
    173  1.1   simonb 
    174  1.1   simonb 	splx(s);
    175  1.1   simonb }
    176