mipssim_intr.c revision 1.1 1 1.1 simonb /* $NetBSD: mipssim_intr.c,v 1.1 2021/01/27 05:24:16 simonb 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.1 simonb __KERNEL_RCSID(0, "$NetBSD: mipssim_intr.c,v 1.1 2021/01/27 05:24:16 simonb 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.1 simonb
39 1.1 simonb #include <mips/locore.h>
40 1.1 simonb #include <machine/intr.h>
41 1.1 simonb
42 1.1 simonb /*
43 1.1 simonb * This is a mask of bits to clear in the SR when we go to a
44 1.1 simonb * given hardware interrupt priority level.
45 1.1 simonb */
46 1.1 simonb static const struct ipl_sr_map mipssim_ipl_sr_map = {
47 1.1 simonb .sr_bits = {
48 1.1 simonb [IPL_NONE] = 0,
49 1.1 simonb [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
50 1.1 simonb [IPL_SOFTNET] = MIPS_SOFT_INT_MASK,
51 1.1 simonb [IPL_VM] = MIPS_SOFT_INT_MASK
52 1.1 simonb | MIPS_INT_MASK_0 | MIPS_INT_MASK_2,
53 1.1 simonb [IPL_SCHED] = MIPS_SOFT_INT_MASK
54 1.1 simonb | MIPS_INT_MASK_0 | MIPS_INT_MASK_2
55 1.1 simonb | MIPS_INT_MASK_5,
56 1.1 simonb [IPL_DDB] = MIPS_INT_MASK,
57 1.1 simonb [IPL_HIGH] = MIPS_INT_MASK,
58 1.1 simonb },
59 1.1 simonb };
60 1.1 simonb
61 1.1 simonb /* XXX - add evcnt bits to <machine/intr.h> struct evbmips_intrhand */
62 1.1 simonb struct intrhand {
63 1.1 simonb LIST_ENTRY(intrhand) ih_q;
64 1.1 simonb struct evcnt ih_count;
65 1.1 simonb int (*ih_func)(void *);
66 1.1 simonb void *ih_arg;
67 1.1 simonb int ih_irq;
68 1.1 simonb };
69 1.1 simonb
70 1.1 simonb
71 1.1 simonb /*
72 1.1 simonb * Use CPU interrupts INT0 .. INT4. Clock interrupts (INT5)
73 1.1 simonb * are handled in cpu_intr() before evbmips_iointr() is called.
74 1.1 simonb */
75 1.1 simonb #define NINTR 5 /* MIPS INT0 - INT4 */
76 1.1 simonb
77 1.1 simonb struct intrhand intrs[NINTR];
78 1.1 simonb const char * const intrnames[NINTR] = {
79 1.1 simonb "int 0 (mipsnet)",
80 1.1 simonb "int 1 (unused)",
81 1.1 simonb "int 2 (uart)",
82 1.1 simonb "int 3 (unused)",
83 1.1 simonb "int 4 (unused)",
84 1.1 simonb };
85 1.1 simonb
86 1.1 simonb void mipssim_irq(int);
87 1.1 simonb
88 1.1 simonb void
89 1.1 simonb evbmips_intr_init(void)
90 1.1 simonb {
91 1.1 simonb int i;
92 1.1 simonb
93 1.1 simonb ipl_sr_map = mipssim_ipl_sr_map;
94 1.1 simonb
95 1.1 simonb /* zero all handlers */
96 1.1 simonb for (i = 0; i < NINTR; i++) {
97 1.1 simonb intrs[i].ih_func = NULL;
98 1.1 simonb intrs[i].ih_arg = NULL;
99 1.1 simonb intrs[i].ih_irq = i;
100 1.1 simonb evcnt_attach_dynamic(&intrs[i].ih_count, EVCNT_TYPE_INTR,
101 1.1 simonb NULL, "cpu", intrnames[i]);
102 1.1 simonb }
103 1.1 simonb }
104 1.1 simonb
105 1.1 simonb void
106 1.1 simonb evbmips_iointr(int ipl, uint32_t ipending, struct clockframe *cf)
107 1.1 simonb {
108 1.1 simonb
109 1.1 simonb for (int level = NINTR - 1; level >= 0; level--) {
110 1.1 simonb struct intrhand *ih;
111 1.1 simonb
112 1.1 simonb if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
113 1.1 simonb continue;
114 1.1 simonb
115 1.1 simonb ih = &intrs[level];
116 1.1 simonb
117 1.1 simonb ih->ih_count.ev_count++;
118 1.1 simonb if (ih->ih_func) {
119 1.1 simonb (*ih->ih_func)(ih->ih_arg);
120 1.1 simonb }
121 1.1 simonb }
122 1.1 simonb }
123 1.1 simonb
124 1.1 simonb void *
125 1.1 simonb evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
126 1.1 simonb {
127 1.1 simonb struct intrhand *ih;
128 1.1 simonb int s;
129 1.1 simonb
130 1.1 simonb if ((irq < 0) || (irq >= NINTR)) {
131 1.1 simonb aprint_error("%s: invalid irq %d\n", __func__, irq);
132 1.1 simonb return NULL;
133 1.1 simonb }
134 1.1 simonb
135 1.1 simonb ih = &intrs[irq];
136 1.1 simonb
137 1.1 simonb s = splhigh();
138 1.1 simonb ih->ih_func = func;
139 1.1 simonb ih->ih_arg = arg;
140 1.1 simonb
141 1.1 simonb /* now enable the IRQ (nothing to do here?) */
142 1.1 simonb
143 1.1 simonb splx(s);
144 1.1 simonb
145 1.1 simonb return ih;
146 1.1 simonb }
147 1.1 simonb
148 1.1 simonb void
149 1.1 simonb evbmips_intr_disestablish(void *cookie)
150 1.1 simonb {
151 1.1 simonb panic("untested %s", __func__); /* XXX! */
152 1.1 simonb
153 1.1 simonb struct intrhand *ih = cookie;
154 1.1 simonb int s;
155 1.1 simonb
156 1.1 simonb s = splhigh();
157 1.1 simonb
158 1.1 simonb /* now disable the IRQ (nothing to do here?) */
159 1.1 simonb
160 1.1 simonb ih->ih_func = NULL;
161 1.1 simonb ih->ih_arg = NULL;
162 1.1 simonb
163 1.1 simonb splx(s);
164 1.1 simonb }
165