intr.c revision 1.3 1 /* $NetBSD: intr.c,v 1.3 2008/05/16 16:24:17 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2005 NONAKA Kimihiro
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.3 2008/05/16 16:24:17 tsutsui Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/device.h>
37
38 #include <sh3/exception.h>
39
40 #include <machine/intr.h>
41
42 #define _N_EXTINTR 8
43
44 #define LANDISK_INTEN 0xb0000005
45 #define INTEN_ALL_MASK 0x00
46
47 struct intrhand {
48 int (*ih_fun)(void *);
49 void *ih_arg;
50 struct intrhand *ih_next;
51 int ih_enable;
52 int ih_level;
53 int ih_irq;
54 struct evcnt ih_evcnt;
55 };
56
57 struct extintr_handler {
58 int (*eih_func)(void *eih_arg);
59 void *eih_arg;
60 struct intrhand *eih_ih;
61 int eih_nih;
62 };
63
64 static struct extintr_handler extintr_handler[_N_EXTINTR];
65
66 static const char *extintr_names[_N_EXTINTR] = {
67 "irq5", "irq6", "irq7", "irq8",
68 "irq9", "irq10", "irq11", "irq12"
69 };
70
71 static int fakeintr(void *arg);
72 static int extintr_intr_handler(void *arg);
73
74 void
75 intc_intr(int ssr, int spc, int ssp)
76 {
77 struct intc_intrhand *ih;
78 struct clockframe cf;
79 int evtcode;
80
81 evtcode = _reg_read_4(SH4_INTEVT);
82 ih = EVTCODE_IH(evtcode);
83 KDASSERT(ih->ih_func);
84
85 switch (evtcode) {
86 #if 0
87 #define IRL(irq) (0x200 + ((irq) << 5))
88 case IRL(5): case IRL(6): case IRL(7): case IRL(8):
89 case IRL(9): case IRL(10): case IRL(11): case IRL(12):
90 {
91 int level;
92 uint8_t inten, bit;
93
94 bit = 1 << (EVTCODE_TO_MAP_INDEX(evtcode) - 5);
95 inten = _reg_read_1(LANDISK_INTEN);
96 _reg_write_1(LANDISK_INTEN, inten & ~bit);
97 level = (_IPL_NSOFT + 1) << 4; /* disable softintr */
98 ssr &= 0xf0;
99 if (level < ssr)
100 level = ssr;
101 (void)_cpu_intr_resume(level);
102 (*ih->ih_func)(ih->ih_arg);
103 _reg_write_1(LANDISK_INTEN, inten);
104 break;
105 }
106 #endif
107 default:
108 (void)_cpu_intr_resume(ih->ih_level);
109 (*ih->ih_func)(ih->ih_arg);
110 break;
111
112 case SH_INTEVT_TMU0_TUNI0:
113 (void)_cpu_intr_resume(ih->ih_level);
114 cf.spc = spc;
115 cf.ssr = ssr;
116 cf.ssp = ssp;
117 (*ih->ih_func)(&cf);
118 break;
119
120 case SH_INTEVT_NMI:
121 printf("NMI ignored.\n");
122 break;
123 }
124 }
125
126 void
127 intr_init(void)
128 {
129
130 _reg_write_1(LANDISK_INTEN, INTEN_ALL_MASK);
131 }
132
133 void *
134 extintr_establish(int irq, int level, int (*ih_fun)(void *), void *ih_arg)
135 {
136 static struct intrhand fakehand = {fakeintr};
137 struct extintr_handler *eih;
138 struct intrhand **p, *q, *ih;
139 const char *name;
140 int evtcode;
141 int s;
142
143 KDASSERT(irq >= 5 && irq <= 12);
144
145 ih = malloc(sizeof(*ih), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
146 if (ih == NULL)
147 panic("intr_establish: can't malloc handler info");
148
149 s = _cpu_intr_suspend();
150
151 switch (level) {
152 default:
153 #if defined(DEBUG)
154 panic("extintr_establish: unknown level %d", level);
155 /*NOTREACHED*/
156 #endif
157 case IPL_VM:
158 break;
159 }
160
161 eih = &extintr_handler[irq - 5];
162 if (eih->eih_func == NULL) {
163 evtcode = 0x200 + (irq << 5);
164 eih->eih_func = intc_intr_establish(evtcode, IST_LEVEL, level,
165 extintr_intr_handler, eih);
166 }
167
168 /*
169 * Figure out where to put the handler.
170 * This is O(N^2), but we want to preserve the order, and N is
171 * generally small.
172 */
173 for (p = &eih->eih_ih; (q = *p) != NULL; p = &q->ih_next)
174 continue;
175
176 /*
177 * Actually install a fake handler momentarily, since we might be doing
178 * this with interrupts enabled and don't want the real routine called
179 * until masking is set up.
180 */
181 fakehand.ih_level = level;
182 *p = &fakehand;
183
184 /*
185 * Poke the real handler in now.
186 */
187 memset(ih, 0, sizeof(*ih));
188 ih->ih_fun = ih_fun;
189 ih->ih_arg = ih_arg;
190 ih->ih_next = NULL;
191 ih->ih_enable = 1;
192 ih->ih_level = level;
193 ih->ih_irq = irq - 5;
194 name = extintr_names[irq - 5];
195 evcnt_attach_dynamic(&ih->ih_evcnt, EVCNT_TYPE_INTR,
196 NULL, "ext", name);
197 *p = ih;
198
199 if (++eih->eih_nih == 1) {
200 /* Unmask interrupt */
201 _reg_bset_1(LANDISK_INTEN, (1 << (irq - 5)));
202 }
203
204 splx(s);
205
206 return (ih);
207 }
208
209 void
210 extintr_disestablish(void *aux)
211 {
212 struct intrhand *ih = aux;
213 struct intrhand **p, *q;
214 struct extintr_handler *eih;
215 int irq;
216 int s;
217
218 KDASSERT(ih != NULL);
219
220 s = _cpu_intr_suspend();
221
222 irq = ih->ih_irq;
223 eih = &extintr_handler[irq];
224
225 /*
226 * Remove the handler from the chain.
227 * This is O(n^2), too.
228 */
229 for (p = &eih->eih_ih; (q = *p) != NULL && q != ih; p = &q->ih_next)
230 continue;
231 if (q == NULL)
232 panic("extintr_disestablish: handler not registered");
233
234 *p = q->ih_next;
235
236 evcnt_detach(&ih->ih_evcnt);
237
238 free((void *)ih, M_DEVBUF);
239
240 if (--eih->eih_nih == 0) {
241 intc_intr_disestablish(eih->eih_func);
242
243 /* Mask interrupt */
244 _reg_bclr_1(LANDISK_INTEN, (1 << irq));
245 }
246
247 splx(s);
248 }
249
250 void
251 extintr_enable(void *aux)
252 {
253 struct intrhand *ih = aux;
254 struct intrhand *p, *q;
255 struct extintr_handler *eih;
256 int irq;
257 int cnt;
258 int s;
259
260 KDASSERT(ih != NULL);
261
262 s = _cpu_intr_suspend();
263
264 irq = ih->ih_irq;
265 KDASSERT(irq >= 0 && irq < 8);
266 eih = &extintr_handler[irq];
267 for (cnt = 0, p = eih->eih_ih, q = NULL; p != NULL; p = p->ih_next) {
268 if (p->ih_enable) {
269 cnt++;
270 }
271 if (p == ih) {
272 q = p;
273 p->ih_enable = 1;
274 }
275 }
276 KDASSERT(q != NULL);
277
278 if (cnt == 0) {
279 /* Unmask interrupt */
280 _reg_bset_1(LANDISK_INTEN, (1 << irq));
281 }
282
283 splx(s);
284 }
285
286 void
287 extintr_disable(void *aux)
288 {
289 struct intrhand *ih = aux;
290 struct intrhand *p, *q;
291 struct extintr_handler *eih;
292 int irq;
293 int cnt;
294 int s;
295
296 KDASSERT(ih != NULL);
297
298 s = _cpu_intr_suspend();
299
300 irq = ih->ih_irq;
301 KDASSERT(irq >= 0 && irq < 8);
302 eih = &extintr_handler[irq];
303 for (cnt = 0, p = eih->eih_ih, q = NULL; p != NULL; p = p->ih_next) {
304 if (p == ih) {
305 q = p;
306 p->ih_enable = 0;
307 }
308 if (!ih->ih_enable) {
309 cnt++;
310 }
311 }
312 KDASSERT(q != NULL);
313
314 if (cnt == 0) {
315 /* Mask interrupt */
316 _reg_bclr_1(LANDISK_INTEN, (1 << irq));
317 }
318
319 splx(s);
320 }
321
322 void
323 extintr_disable_by_num(int irq)
324 {
325 struct extintr_handler *eih;
326 struct intrhand *ih;
327 int s;
328
329 KDASSERT(irq >= 5 && irq <= 12);
330
331 s = _cpu_intr_suspend();
332 eih = &extintr_handler[irq - 5];
333 for (ih = eih->eih_ih; ih != NULL; ih = ih->ih_next) {
334 ih->ih_enable = 0;
335 }
336 /* Mask interrupt */
337 _reg_bclr_1(LANDISK_INTEN, (1 << irq));
338 splx(s);
339 }
340
341 static int
342 fakeintr(void *arg)
343 {
344
345 return 0;
346 }
347
348 static int
349 extintr_intr_handler(void *arg)
350 {
351 struct extintr_handler *eih = arg;
352 struct intrhand *ih;
353 int r;
354
355 if (__predict_true(eih != NULL)) {
356 for (ih = eih->eih_ih; ih != NULL; ih = ih->ih_next) {
357 if (__predict_true(ih->ih_enable)) {
358 r = (*ih->ih_fun)(ih->ih_arg);
359 if (__predict_true(r != 0)) {
360 ih->ih_evcnt.ev_count++;
361 }
362 }
363 }
364 return 1;
365 }
366 return 0;
367 }
368