intr.c revision 1.2 1 /* $NetBSD: intr.c,v 1.2 2007/12/11 16:51:14 ad 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.2 2007/12/11 16:51:14 ad 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 if (irq != 5) {
195 name = extintr_names[irq - 5];
196 } else if (level == IPL_BIO) {
197 name = "ehci";
198 } else if (level == IPL_NET) {
199 name = "rtk";
200 } else {
201 name = "unknown";
202 }
203 evcnt_attach_dynamic(&ih->ih_evcnt, EVCNT_TYPE_INTR,
204 NULL, "ext", name);
205 *p = ih;
206
207 if (++eih->eih_nih == 1) {
208 /* Unmask interrupt */
209 _reg_bset_1(LANDISK_INTEN, (1 << (irq - 5)));
210 }
211
212 splx(s);
213
214 return (ih);
215 }
216
217 void
218 extintr_disestablish(void *aux)
219 {
220 struct intrhand *ih = aux;
221 struct intrhand **p, *q;
222 struct extintr_handler *eih;
223 int irq;
224 int s;
225
226 KDASSERT(ih != NULL);
227
228 s = _cpu_intr_suspend();
229
230 irq = ih->ih_irq;
231 eih = &extintr_handler[irq];
232
233 /*
234 * Remove the handler from the chain.
235 * This is O(n^2), too.
236 */
237 for (p = &eih->eih_ih; (q = *p) != NULL && q != ih; p = &q->ih_next)
238 continue;
239 if (q == NULL)
240 panic("extintr_disestablish: handler not registered");
241
242 *p = q->ih_next;
243
244 evcnt_detach(&ih->ih_evcnt);
245
246 free((void *)ih, M_DEVBUF);
247
248 if (--eih->eih_nih == 0) {
249 intc_intr_disestablish(eih->eih_func);
250
251 /* Mask interrupt */
252 _reg_bclr_1(LANDISK_INTEN, (1 << irq));
253 }
254
255 splx(s);
256 }
257
258 void
259 extintr_enable(void *aux)
260 {
261 struct intrhand *ih = aux;
262 struct intrhand *p, *q;
263 struct extintr_handler *eih;
264 int irq;
265 int cnt;
266 int s;
267
268 KDASSERT(ih != NULL);
269
270 s = _cpu_intr_suspend();
271
272 irq = ih->ih_irq;
273 KDASSERT(irq >= 0 && irq < 8);
274 eih = &extintr_handler[irq];
275 for (cnt = 0, p = eih->eih_ih, q = NULL; p != NULL; p = p->ih_next) {
276 if (p->ih_enable) {
277 cnt++;
278 }
279 if (p == ih) {
280 q = p;
281 p->ih_enable = 1;
282 }
283 }
284 KDASSERT(q != NULL);
285
286 if (cnt == 0) {
287 /* Unmask interrupt */
288 _reg_bset_1(LANDISK_INTEN, (1 << irq));
289 }
290
291 splx(s);
292 }
293
294 void
295 extintr_disable(void *aux)
296 {
297 struct intrhand *ih = aux;
298 struct intrhand *p, *q;
299 struct extintr_handler *eih;
300 int irq;
301 int cnt;
302 int s;
303
304 KDASSERT(ih != NULL);
305
306 s = _cpu_intr_suspend();
307
308 irq = ih->ih_irq;
309 KDASSERT(irq >= 0 && irq < 8);
310 eih = &extintr_handler[irq];
311 for (cnt = 0, p = eih->eih_ih, q = NULL; p != NULL; p = p->ih_next) {
312 if (p == ih) {
313 q = p;
314 p->ih_enable = 0;
315 }
316 if (!ih->ih_enable) {
317 cnt++;
318 }
319 }
320 KDASSERT(q != NULL);
321
322 if (cnt == 0) {
323 /* Mask interrupt */
324 _reg_bclr_1(LANDISK_INTEN, (1 << irq));
325 }
326
327 splx(s);
328 }
329
330 void
331 extintr_disable_by_num(int irq)
332 {
333 struct extintr_handler *eih;
334 struct intrhand *ih;
335 int s;
336
337 KDASSERT(irq >= 5 && irq <= 12);
338
339 s = _cpu_intr_suspend();
340 eih = &extintr_handler[irq - 5];
341 for (ih = eih->eih_ih; ih != NULL; ih = ih->ih_next) {
342 ih->ih_enable = 0;
343 }
344 /* Mask interrupt */
345 _reg_bclr_1(LANDISK_INTEN, (1 << irq));
346 splx(s);
347 }
348
349 static int
350 fakeintr(void *arg)
351 {
352
353 return 0;
354 }
355
356 static int
357 extintr_intr_handler(void *arg)
358 {
359 struct extintr_handler *eih = arg;
360 struct intrhand *ih;
361 int r;
362
363 if (__predict_true(eih != NULL)) {
364 for (ih = eih->eih_ih; ih != NULL; ih = ih->ih_next) {
365 if (__predict_true(ih->ih_enable)) {
366 r = (*ih->ih_fun)(ih->ih_arg);
367 if (__predict_true(r != 0)) {
368 ih->ih_evcnt.ev_count++;
369 }
370 }
371 }
372 return 1;
373 }
374 return 0;
375 }
376