sio_pic.c revision 1.15 1 1.15 cgd /* $NetBSD: sio_pic.c,v 1.15 1996/11/13 21:13:34 cgd Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.6 cgd * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Author: Chris G. Demetriou
8 1.1 cgd *
9 1.1 cgd * Permission to use, copy, modify and distribute this software and
10 1.1 cgd * its documentation is hereby granted, provided that both the copyright
11 1.1 cgd * notice and this permission notice appear in all copies of the
12 1.1 cgd * software, derivative works or modified versions, and any portions
13 1.1 cgd * thereof, and that both notices appear in supporting documentation.
14 1.1 cgd *
15 1.1 cgd * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 cgd * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1 cgd *
19 1.1 cgd * Carnegie Mellon requests users of this software to return to
20 1.1 cgd *
21 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 cgd * School of Computer Science
23 1.1 cgd * Carnegie Mellon University
24 1.1 cgd * Pittsburgh PA 15213-3890
25 1.1 cgd *
26 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
27 1.1 cgd * rights to redistribute these changes.
28 1.1 cgd */
29 1.1 cgd
30 1.1 cgd #include <sys/param.h>
31 1.1 cgd #include <sys/systm.h>
32 1.1 cgd #include <sys/device.h>
33 1.1 cgd #include <sys/malloc.h>
34 1.1 cgd #include <sys/syslog.h>
35 1.1 cgd
36 1.4 cgd #include <machine/intr.h>
37 1.4 cgd #include <machine/bus.h>
38 1.4 cgd
39 1.2 cgd #include <dev/isa/isareg.h>
40 1.1 cgd #include <dev/isa/isavar.h>
41 1.2 cgd #include <alpha/pci/siovar.h>
42 1.2 cgd
43 1.2 cgd #ifndef EVCNT_COUNTERS
44 1.2 cgd #include <machine/intrcnt.h>
45 1.2 cgd #endif
46 1.1 cgd
47 1.2 cgd #include "sio.h"
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * To add to the long history of wonderful PROM console traits,
51 1.1 cgd * AlphaStation PROMs don't reset themselves completely on boot!
52 1.1 cgd * Therefore, if an interrupt was turned on when the kernel was
53 1.1 cgd * started, we're not going to EVER turn it off... I don't know
54 1.1 cgd * what will happen if new interrupts (that the PROM console doesn't
55 1.1 cgd * want) are turned on. I'll burn that bridge when I come to it.
56 1.1 cgd */
57 1.1 cgd #define BROKEN_PROM_CONSOLE
58 1.1 cgd
59 1.2 cgd /*
60 1.2 cgd * Private functions and variables.
61 1.2 cgd */
62 1.4 cgd static void sio_strayintr __P((int));
63 1.4 cgd
64 1.14 cgd bus_space_tag_t sio_iot;
65 1.14 cgd bus_space_handle_t sio_ioh_icu1, sio_ioh_icu2, sio_ioh_elcr;
66 1.1 cgd
67 1.1 cgd /*
68 1.1 cgd * Interrupt handler chains. sio_intr_establish() inserts a handler into
69 1.1 cgd * the list. The handler is called with its (single) argument.
70 1.1 cgd */
71 1.1 cgd struct intrhand {
72 1.15 cgd int (*ih_fun) __P((void *));
73 1.1 cgd void *ih_arg;
74 1.1 cgd u_long ih_count;
75 1.1 cgd struct intrhand *ih_next;
76 1.1 cgd int ih_level;
77 1.1 cgd int ih_irq;
78 1.1 cgd };
79 1.1 cgd
80 1.1 cgd #define ICU_LEN 16 /* number of ISA IRQs */
81 1.1 cgd
82 1.1 cgd static struct intrhand *sio_intrhand[ICU_LEN];
83 1.3 mycroft static int sio_intrsharetype[ICU_LEN];
84 1.1 cgd static u_long sio_strayintrcnt[ICU_LEN];
85 1.2 cgd #ifdef EVCNT_COUNTERS
86 1.2 cgd struct evcnt sio_intr_evcnt;
87 1.2 cgd #endif
88 1.1 cgd
89 1.1 cgd #ifndef STRAY_MAX
90 1.1 cgd #ifdef BROKEN_PROM_CONSOLE
91 1.1 cgd /*
92 1.1 cgd * If prom console is broken, because initial interrupt settings
93 1.1 cgd * must be kept, there's no way to escape stray interrupts.
94 1.1 cgd */
95 1.1 cgd #define STRAY_MAX 0
96 1.1 cgd #else
97 1.1 cgd #define STRAY_MAX 5
98 1.1 cgd #endif
99 1.1 cgd #endif
100 1.1 cgd
101 1.1 cgd #ifdef BROKEN_PROM_CONSOLE
102 1.1 cgd /*
103 1.1 cgd * If prom console is broken, must remember the initial interrupt
104 1.1 cgd * settings and enforce them. WHEE!
105 1.1 cgd */
106 1.1 cgd u_int8_t initial_ocw1[2];
107 1.1 cgd u_int8_t initial_elcr[2];
108 1.1 cgd #define INITIALLY_ENABLED(irq) \
109 1.1 cgd ((initial_ocw1[(irq) / 8] & (1 << ((irq) % 8))) == 0)
110 1.1 cgd #define INITIALLY_LEVEL_TRIGGERED(irq) \
111 1.1 cgd ((initial_elcr[(irq) / 8] & (1 << ((irq) % 8))) != 0)
112 1.1 cgd #else
113 1.1 cgd #define INITIALLY_ENABLED(irq) ((irq) == 2 ? 1 : 0)
114 1.1 cgd #define INITIALLY_LEVEL_TRIGGERED(irq) 0
115 1.1 cgd #endif
116 1.1 cgd
117 1.15 cgd void sio_setirqstat __P((int, int, int));
118 1.15 cgd
119 1.1 cgd void
120 1.1 cgd sio_setirqstat(irq, enabled, type)
121 1.1 cgd int irq, enabled;
122 1.3 mycroft int type;
123 1.1 cgd {
124 1.1 cgd u_int8_t ocw1[2], elcr[2];
125 1.1 cgd int icu, bit;
126 1.1 cgd
127 1.1 cgd #if 0
128 1.13 christos printf("sio_setirqstat: irq %d: %s, %s\n", irq,
129 1.1 cgd enabled ? "enabled" : "disabled", isa_intr_typename(type));
130 1.1 cgd #endif
131 1.1 cgd
132 1.2 cgd sio_intrsharetype[irq] = type;
133 1.1 cgd
134 1.1 cgd icu = irq / 8;
135 1.1 cgd bit = irq % 8;
136 1.1 cgd
137 1.14 cgd ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
138 1.14 cgd ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
139 1.14 cgd elcr[0] = bus_space_read_1(sio_iot, sio_ioh_elcr, 0); /* XXX */
140 1.14 cgd elcr[1] = bus_space_read_1(sio_iot, sio_ioh_elcr, 1); /* XXX */
141 1.1 cgd
142 1.1 cgd /*
143 1.1 cgd * interrupt enable: set bit to mask (disable) interrupt.
144 1.1 cgd */
145 1.1 cgd if (enabled)
146 1.1 cgd ocw1[icu] &= ~(1 << bit);
147 1.1 cgd else
148 1.1 cgd ocw1[icu] |= 1 << bit;
149 1.1 cgd
150 1.1 cgd /*
151 1.1 cgd * interrupt type select: set bit to get level-triggered.
152 1.1 cgd */
153 1.3 mycroft if (type == IST_LEVEL)
154 1.1 cgd elcr[icu] |= 1 << bit;
155 1.1 cgd else
156 1.1 cgd elcr[icu] &= ~(1 << bit);
157 1.1 cgd
158 1.1 cgd #ifdef not_here
159 1.1 cgd /* see the init function... */
160 1.1 cgd ocw1[0] &= ~0x04; /* always enable IRQ2 on first PIC */
161 1.1 cgd elcr[0] &= ~0x07; /* IRQ[0-2] must be edge-triggered */
162 1.1 cgd elcr[1] &= ~0x21; /* IRQ[13,8] must be edge-triggered */
163 1.1 cgd #endif
164 1.1 cgd
165 1.1 cgd #ifdef BROKEN_PROM_CONSOLE
166 1.1 cgd /*
167 1.1 cgd * make sure that the initially clear bits (unmasked interrupts)
168 1.1 cgd * are never set, and that the initially-level-triggered
169 1.1 cgd * intrrupts always remain level-triggered, to keep the prom happy.
170 1.1 cgd */
171 1.1 cgd if ((ocw1[0] & ~initial_ocw1[0]) != 0 ||
172 1.1 cgd (ocw1[1] & ~initial_ocw1[1]) != 0 ||
173 1.1 cgd (elcr[0] & initial_elcr[0]) != initial_elcr[0] ||
174 1.1 cgd (elcr[1] & initial_elcr[1]) != initial_elcr[1]) {
175 1.13 christos printf("sio_sis: initial: ocw = (%2x,%2x), elcr = (%2x,%2x)\n",
176 1.1 cgd initial_ocw1[0], initial_ocw1[1],
177 1.1 cgd initial_elcr[0], initial_elcr[1]);
178 1.13 christos printf(" current: ocw = (%2x,%2x), elcr = (%2x,%2x)\n",
179 1.1 cgd ocw1[0], ocw1[1], elcr[0], elcr[1]);
180 1.1 cgd panic("sio_setirqstat: hosed");
181 1.1 cgd }
182 1.1 cgd #endif
183 1.1 cgd
184 1.14 cgd bus_space_write_1(sio_iot, sio_ioh_icu1, 1, ocw1[0]);
185 1.14 cgd bus_space_write_1(sio_iot, sio_ioh_icu2, 1, ocw1[1]);
186 1.14 cgd bus_space_write_1(sio_iot, sio_ioh_elcr, 0, elcr[0]); /* XXX */
187 1.14 cgd bus_space_write_1(sio_iot, sio_ioh_elcr, 1, elcr[1]); /* XXX */
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd void
191 1.14 cgd sio_intr_setup(iot)
192 1.14 cgd bus_space_tag_t iot;
193 1.1 cgd {
194 1.1 cgd int i;
195 1.1 cgd
196 1.14 cgd sio_iot = iot;
197 1.4 cgd
198 1.14 cgd if (bus_space_map(sio_iot, IO_ICU1, IO_ICUSIZE, 0, &sio_ioh_icu1) ||
199 1.14 cgd bus_space_map(sio_iot, IO_ICU2, IO_ICUSIZE, 0, &sio_ioh_icu2) ||
200 1.14 cgd bus_space_map(sio_iot, 0x4d0, 2, 0, &sio_ioh_elcr))
201 1.4 cgd panic("sio_intr_setup: can't map I/O ports");
202 1.2 cgd
203 1.1 cgd #ifdef BROKEN_PROM_CONSOLE
204 1.1 cgd /*
205 1.1 cgd * Remember the initial values, because the prom is stupid.
206 1.1 cgd */
207 1.14 cgd initial_ocw1[0] = bus_space_read_1(sio_iot, sio_ioh_icu1, 1);
208 1.14 cgd initial_ocw1[1] = bus_space_read_1(sio_iot, sio_ioh_icu2, 1);
209 1.14 cgd initial_elcr[0] = bus_space_read_1(sio_iot, sio_ioh_elcr, 0); /* XXX */
210 1.14 cgd initial_elcr[1] = bus_space_read_1(sio_iot, sio_ioh_elcr, 1); /* XXX */
211 1.1 cgd #if 0
212 1.13 christos printf("initial_ocw1[0] = 0x%x\n", initial_ocw1[0]);
213 1.13 christos printf("initial_ocw1[1] = 0x%x\n", initial_ocw1[1]);
214 1.13 christos printf("initial_elcr[0] = 0x%x\n", initial_elcr[0]);
215 1.13 christos printf("initial_elcr[1] = 0x%x\n", initial_elcr[1]);
216 1.1 cgd #endif
217 1.1 cgd #endif
218 1.1 cgd
219 1.1 cgd /*
220 1.1 cgd * set up initial values for interrupt enables.
221 1.1 cgd */
222 1.1 cgd for (i = 0; i < ICU_LEN; i++) {
223 1.1 cgd switch (i) {
224 1.1 cgd case 0:
225 1.1 cgd case 1:
226 1.1 cgd case 8:
227 1.1 cgd case 13:
228 1.1 cgd /*
229 1.1 cgd * IRQs 0, 1, 8, and 13 must always be
230 1.1 cgd * edge-triggered.
231 1.1 cgd */
232 1.1 cgd if (INITIALLY_LEVEL_TRIGGERED(i))
233 1.13 christos printf("sio_intr_setup: %d LT!\n", i);
234 1.3 mycroft sio_setirqstat(i, INITIALLY_ENABLED(i), IST_EDGE);
235 1.1 cgd break;
236 1.1 cgd
237 1.1 cgd case 2:
238 1.1 cgd /*
239 1.1 cgd * IRQ 2 must be edge-triggered, and should be
240 1.1 cgd * enabled (otherwise IRQs 8-15 are ignored).
241 1.1 cgd */
242 1.1 cgd if (INITIALLY_LEVEL_TRIGGERED(i))
243 1.13 christos printf("sio_intr_setup: %d LT!\n", i);
244 1.1 cgd if (!INITIALLY_ENABLED(i))
245 1.13 christos printf("sio_intr_setup: %d not enabled!\n", i);
246 1.3 mycroft sio_setirqstat(i, 1, IST_EDGE);
247 1.1 cgd break;
248 1.1 cgd
249 1.1 cgd default:
250 1.1 cgd /*
251 1.1 cgd * Otherwise, disable the IRQ and set its
252 1.1 cgd * type to (effectively) "unknown."
253 1.1 cgd */
254 1.1 cgd sio_setirqstat(i, INITIALLY_ENABLED(i),
255 1.3 mycroft INITIALLY_LEVEL_TRIGGERED(i) ? IST_LEVEL :
256 1.3 mycroft IST_NONE);
257 1.1 cgd break;
258 1.1 cgd }
259 1.1 cgd }
260 1.1 cgd }
261 1.1 cgd
262 1.4 cgd const char *
263 1.4 cgd sio_intr_string(v, irq)
264 1.4 cgd void *v;
265 1.4 cgd int irq;
266 1.4 cgd {
267 1.7 cgd static char irqstr[12]; /* 8 + 2 + NULL + sanity */
268 1.4 cgd
269 1.4 cgd if (irq == 0 || irq >= ICU_LEN || irq == 2)
270 1.4 cgd panic("sio_intr_string: bogus IRQ 0x%x\n", irq);
271 1.4 cgd
272 1.13 christos sprintf(irqstr, "isa irq %d", irq);
273 1.4 cgd return (irqstr);
274 1.4 cgd }
275 1.4 cgd
276 1.1 cgd void *
277 1.4 cgd sio_intr_establish(v, irq, type, level, ih_fun, ih_arg)
278 1.4 cgd void *v, *ih_arg;
279 1.4 cgd int irq;
280 1.3 mycroft int type;
281 1.3 mycroft int level;
282 1.1 cgd int (*ih_fun)(void *);
283 1.1 cgd {
284 1.1 cgd struct intrhand **p, *c, *ih;
285 1.1 cgd extern int cold;
286 1.1 cgd
287 1.1 cgd /* no point in sleeping unless someone can free memory. */
288 1.1 cgd ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
289 1.1 cgd if (ih == NULL)
290 1.1 cgd panic("sio_intr_establish: can't malloc handler info");
291 1.1 cgd
292 1.3 mycroft if (irq > ICU_LEN || type == IST_NONE)
293 1.1 cgd panic("sio_intr_establish: bogus irq or type");
294 1.1 cgd
295 1.2 cgd switch (sio_intrsharetype[irq]) {
296 1.3 mycroft case IST_EDGE:
297 1.3 mycroft case IST_LEVEL:
298 1.2 cgd if (type == sio_intrsharetype[irq])
299 1.1 cgd break;
300 1.3 mycroft case IST_PULSE:
301 1.9 cgd if (type != IST_NONE) {
302 1.9 cgd if (sio_intrhand[irq] == NULL) {
303 1.13 christos printf("sio_intr_establish: irq %d: warning: using %s on %s\n",
304 1.9 cgd irq, isa_intr_typename(type),
305 1.9 cgd isa_intr_typename(sio_intrsharetype[irq]));
306 1.9 cgd type = sio_intrsharetype[irq];
307 1.9 cgd } else {
308 1.9 cgd panic("sio_intr_establish: irq %d: can't share %s with %s",
309 1.9 cgd irq, isa_intr_typename(type),
310 1.9 cgd isa_intr_typename(sio_intrsharetype[irq]));
311 1.9 cgd }
312 1.9 cgd }
313 1.1 cgd break;
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd * Figure out where to put the handler.
318 1.1 cgd * This is O(N^2), but we want to preserve the order, and N is
319 1.1 cgd * generally small.
320 1.1 cgd */
321 1.1 cgd for (p = &sio_intrhand[irq]; (c = *p) != NULL; p = &c->ih_next)
322 1.1 cgd ;
323 1.1 cgd
324 1.1 cgd /*
325 1.1 cgd * Poke the real handler in now.
326 1.1 cgd */
327 1.1 cgd ih->ih_fun = ih_fun;
328 1.1 cgd ih->ih_arg = ih_arg;
329 1.1 cgd ih->ih_count = 0;
330 1.1 cgd ih->ih_next = NULL;
331 1.1 cgd ih->ih_level = 0; /* XXX meaningless on alpha */
332 1.1 cgd ih->ih_irq = irq;
333 1.1 cgd *p = ih;
334 1.1 cgd
335 1.1 cgd sio_setirqstat(irq, 1, type);
336 1.1 cgd
337 1.1 cgd return ih;
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd void
341 1.4 cgd sio_intr_disestablish(v, cookie)
342 1.4 cgd void *v;
343 1.4 cgd void *cookie;
344 1.1 cgd {
345 1.1 cgd
346 1.15 cgd printf("sio_intr_disestablish(%p)\n", cookie);
347 1.1 cgd /* XXX */
348 1.1 cgd
349 1.1 cgd /* XXX NEVER ALLOW AN INITIALLY-ENABLED INTERRUPT TO BE DISABLED */
350 1.1 cgd /* XXX NEVER ALLOW AN INITIALLY-LT INTERRUPT TO BECOME UNTYPED */
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd /*
354 1.1 cgd * caught a stray interrupt; notify if not too many seen already.
355 1.1 cgd */
356 1.1 cgd void
357 1.1 cgd sio_strayintr(irq)
358 1.4 cgd int irq;
359 1.1 cgd {
360 1.1 cgd
361 1.11 cgd sio_strayintrcnt[irq]++;
362 1.11 cgd
363 1.11 cgd #ifdef notyet
364 1.11 cgd if (sio_strayintrcnt[irq] == STRAY_MAX)
365 1.11 cgd sio_disable_intr(irq);
366 1.11 cgd
367 1.11 cgd log(LOG_ERR, "stray isa irq %d\n", irq);
368 1.11 cgd if (sio_strayintrcnt[irq] == STRAY_MAX)
369 1.11 cgd log(LOG_ERR, "disabling interrupts on isa irq %d\n", irq);
370 1.11 cgd #else
371 1.11 cgd if (sio_strayintrcnt[irq] <= STRAY_MAX)
372 1.11 cgd log(LOG_ERR, "stray isa irq %d%s\n", irq,
373 1.1 cgd sio_strayintrcnt[irq] >= STRAY_MAX ?
374 1.1 cgd "; stopped logging" : "");
375 1.11 cgd #endif
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd void
379 1.1 cgd sio_iointr(framep, vec)
380 1.1 cgd void *framep;
381 1.10 cgd unsigned long vec;
382 1.1 cgd {
383 1.1 cgd int irq, handled;
384 1.1 cgd struct intrhand *ih;
385 1.1 cgd
386 1.1 cgd irq = (vec - 0x800) >> 4;
387 1.1 cgd #ifdef DIAGNOSTIC
388 1.1 cgd if (irq > ICU_LEN || irq < 0)
389 1.1 cgd panic("sio_iointr: irq out of range (%d)", irq);
390 1.1 cgd #endif
391 1.1 cgd
392 1.2 cgd #ifdef EVCNT_COUNTERS
393 1.2 cgd sio_intr_evcnt.ev_count++;
394 1.2 cgd #else
395 1.2 cgd if (ICU_LEN != INTRCNT_ISA_IRQ_LEN)
396 1.2 cgd panic("sio interrupt counter sizes inconsistent");
397 1.2 cgd intrcnt[INTRCNT_ISA_IRQ + irq]++;
398 1.2 cgd #endif
399 1.2 cgd
400 1.1 cgd /*
401 1.1 cgd * We cdr down the intrhand chain, calling each handler with
402 1.1 cgd * its appropriate argument;
403 1.1 cgd *
404 1.1 cgd * The handler returns one of three values:
405 1.1 cgd * 0 - This interrupt wasn't for me.
406 1.1 cgd * 1 - This interrupt was for me.
407 1.1 cgd * -1 - This interrupt might have been for me, but I don't know.
408 1.1 cgd * If there are no handlers, or they all return 0, we flags it as a
409 1.1 cgd * `stray' interrupt. On a system with level-triggered interrupts,
410 1.1 cgd * we could terminate immediately when one of them returns 1; but
411 1.1 cgd * this is PC-ish!
412 1.1 cgd */
413 1.1 cgd for (ih = sio_intrhand[irq], handled = 0; ih != NULL;
414 1.1 cgd ih = ih->ih_next) {
415 1.1 cgd int rv;
416 1.1 cgd
417 1.1 cgd rv = (*ih->ih_fun)(ih->ih_arg);
418 1.1 cgd
419 1.1 cgd ih->ih_count++;
420 1.1 cgd handled = handled || (rv != 0);
421 1.1 cgd }
422 1.1 cgd
423 1.1 cgd if (!handled)
424 1.1 cgd sio_strayintr(irq);
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * Some versions of the machines which use the SIO
428 1.1 cgd * (or is it some PALcode revisions on those machines?)
429 1.1 cgd * require the non-specific EOI to be fed to the PIC(s)
430 1.1 cgd * by the interrupt handler.
431 1.1 cgd */
432 1.1 cgd if (irq > 7)
433 1.14 cgd bus_space_write_1(sio_iot,
434 1.4 cgd sio_ioh_icu2, 0, 0x20 | (irq & 0x07)); /* XXX */
435 1.14 cgd bus_space_write_1(sio_iot,
436 1.4 cgd sio_ioh_icu1, 0, 0x20 | (irq > 7 ? 2 : irq)); /* XXX */
437 1.1 cgd }
438