pci_kn20aa.c revision 1.15 1 /* $NetBSD: pci_kn20aa.c,v 1.15 1996/08/19 04:19:01 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/malloc.h>
36 #include <sys/device.h>
37 #include <sys/syslog.h>
38
39 #include <vm/vm.h>
40
41 #include <machine/autoconf.h>
42
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45
46 #include <alpha/pci/ciareg.h>
47 #include <alpha/pci/ciavar.h>
48
49 #include <alpha/pci/pci_kn20aa.h>
50
51 #ifndef EVCNT_COUNTERS
52 #include <machine/intrcnt.h>
53 #endif
54
55 #include "sio.h"
56 #if NSIO
57 #include <alpha/pci/siovar.h>
58 #endif
59
60 int dec_kn20aa_intr_map __P((void *, pcitag_t, int, int,
61 pci_intr_handle_t *));
62 const char *dec_kn20aa_intr_string __P((void *, pci_intr_handle_t));
63 void *dec_kn20aa_intr_establish __P((void *, pci_intr_handle_t,
64 int, int (*func)(void *), void *));
65 void dec_kn20aa_intr_disestablish __P((void *, void *));
66
67 #define KN20AA_PCEB_IRQ 31
68 #define KN20AA_MAX_IRQ 32
69 #define PCI_STRAY_MAX 5
70
71 struct kn20aa_intrhand {
72 TAILQ_ENTRY(kn20aa_intrhand) ih_q;
73 int (*ih_fun)();
74 void *ih_arg;
75 u_long ih_count;
76 int ih_level;
77 };
78 TAILQ_HEAD(kn20aa_intrchain, kn20aa_intrhand);
79
80 struct kn20aa_intrchain kn20aa_pci_intrs[KN20AA_MAX_IRQ];
81 int kn20aa_pci_strayintrcnt[KN20AA_MAX_IRQ];
82 #ifdef EVCNT_COUNTERS
83 struct evcnt kn20aa_intr_evcnt;
84 #endif
85
86 void kn20aa_pci_strayintr __P((int irq));
87 void kn20aa_iointr __P((void *framep, unsigned long vec));
88 void kn20aa_enable_intr __P((int irq));
89 void kn20aa_disable_intr __P((int irq));
90 struct kn20aa_intrhand *kn20aa_attach_intr __P((struct kn20aa_intrchain *,
91 int, int (*) (void *), void *));
92
93 void
94 pci_kn20aa_pickintr(ccp)
95 struct cia_config *ccp;
96 {
97 int i;
98 struct kn20aa_intrhand *nintrhand;
99 bus_chipset_tag_t bc = &ccp->cc_bc;
100 pci_chipset_tag_t pc = &ccp->cc_pc;
101
102 for (i = 0; i < KN20AA_MAX_IRQ; i++)
103 TAILQ_INIT(&kn20aa_pci_intrs[i]);
104
105 pc->pc_intr_v = ccp;
106 pc->pc_intr_map = dec_kn20aa_intr_map;
107 pc->pc_intr_string = dec_kn20aa_intr_string;
108 pc->pc_intr_establish = dec_kn20aa_intr_establish;
109 pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
110
111 #if NSIO
112 sio_intr_setup(bc);
113 #endif
114
115 set_iointr(kn20aa_iointr);
116
117 #if NSIO
118 kn20aa_enable_intr(KN20AA_PCEB_IRQ);
119 #if 0 /* XXX init PCEB interrupt handler? */
120 kn20aa_attach_intr(&kn20aa_pci_intrs[KN20AA_PCEB_IRQ], ???, ???, ???);
121 #endif
122 #endif
123 }
124
125 int
126 dec_kn20aa_intr_map(ccv, bustag, buspin, line, ihp)
127 void *ccv;
128 pcitag_t bustag;
129 int buspin, line;
130 pci_intr_handle_t *ihp;
131 {
132 struct cia_config *ccp = ccv;
133 pci_chipset_tag_t pc = &ccp->cc_pc;
134 int device;
135 int kn20aa_irq;
136 void *ih;
137
138 if (buspin == 0) {
139 /* No IRQ used. */
140 return 1;
141 }
142 if (buspin > 4) {
143 printf("pci_map_int: bad interrupt pin %d\n", buspin);
144 return 1;
145 }
146
147 /*
148 * Slot->interrupt translation. Appears to work, though it
149 * may not hold up forever.
150 *
151 * The DEC engineers who did this hardware obviously engaged
152 * in random drug testing.
153 */
154 pci_decompose_tag(pc, bustag, NULL, &device, NULL);
155 switch (device) {
156 case 11:
157 case 12:
158 kn20aa_irq = ((device - 11) + 0) * 4;
159 break;
160
161 case 7:
162 kn20aa_irq = 8;
163 break;
164
165 case 9:
166 kn20aa_irq = 12;
167 break;
168
169 case 6: /* 21040 on AlphaStation 500 */
170 kn20aa_irq = 13;
171 break;
172
173 case 8:
174 kn20aa_irq = 16;
175 break;
176
177 default:
178 panic("pci_kn20aa_map_int: invalid device number %d\n",
179 device);
180 }
181
182 kn20aa_irq += buspin - 1;
183 if (kn20aa_irq > KN20AA_MAX_IRQ)
184 panic("pci_kn20aa_map_int: kn20aa_irq too large (%d)\n",
185 kn20aa_irq);
186
187 *ihp = kn20aa_irq;
188 return (0);
189 }
190
191 const char *
192 dec_kn20aa_intr_string(ccv, ih)
193 void *ccv;
194 pci_intr_handle_t ih;
195 {
196 struct cia_config *ccp = ccv;
197 static char irqstr[15]; /* 11 + 2 + NULL + sanity */
198
199 if (ih > KN20AA_MAX_IRQ)
200 panic("dec_kn20aa_a50_intr_string: bogus kn20aa IRQ 0x%x\n",
201 ih);
202
203 sprintf(irqstr, "kn20aa irq %d", ih);
204 return (irqstr);
205 }
206
207 void *
208 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
209 void *ccv, *arg;
210 pci_intr_handle_t ih;
211 int level;
212 int (*func) __P((void *));
213 {
214 struct cia_config *ccp = ccv;
215 void *cookie;
216
217 if (ih > KN20AA_MAX_IRQ)
218 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%x\n",
219 ih);
220
221 cookie = kn20aa_attach_intr(&kn20aa_pci_intrs[ih], level, func, arg);
222 kn20aa_enable_intr(ih);
223 return (cookie);
224 }
225
226 void
227 dec_kn20aa_intr_disestablish(ccv, cookie)
228 void *ccv, *cookie;
229 {
230 struct cia_config *ccp = ccv;
231
232 panic("dec_kn20aa_intr_disestablish not implemented"); /* XXX */
233 }
234
235 /*
236 * caught a stray interrupt; notify if not too many seen already.
237 */
238 void
239 kn20aa_pci_strayintr(irq)
240 int irq;
241 {
242
243 kn20aa_pci_strayintrcnt[irq]++;
244 if (kn20aa_pci_strayintrcnt[irq] == PCI_STRAY_MAX)
245 kn20aa_disable_intr(irq);
246
247 log(LOG_ERR, "stray kn20aa irq %d\n", irq);
248 if (kn20aa_pci_strayintrcnt[irq] == PCI_STRAY_MAX)
249 log(LOG_ERR, "disabling interrupts on kn20aa irq %d\n", irq);
250 }
251
252 void
253 kn20aa_iointr(framep, vec)
254 void *framep;
255 unsigned long vec;
256 {
257 struct kn20aa_intrhand *ih;
258 int irq, handled;
259
260 if (vec >= 0x900) {
261 if (vec >= 0x900 + (KN20AA_MAX_IRQ << 4))
262 panic("kn20aa_iointr: vec 0x%x out of range\n", vec);
263 irq = (vec - 0x900) >> 4;
264
265 #ifdef EVCNT_COUNTERS
266 kn20aa_intr_evcnt.ev_count++;
267 #else
268 if (KN20AA_MAX_IRQ != INTRCNT_KN20AA_IRQ_LEN)
269 panic("kn20aa interrupt counter sizes inconsistent");
270 intrcnt[INTRCNT_KN20AA_IRQ + irq]++;
271 #endif
272
273 for (ih = kn20aa_pci_intrs[irq].tqh_first, handled = 0;
274 ih != NULL; ih = ih->ih_q.tqe_next) {
275 int rv;
276
277 rv = (*ih->ih_fun)(ih->ih_arg);
278
279 ih->ih_count++;
280 handled = handled || (rv != 0);
281 }
282 if (!handled)
283 kn20aa_pci_strayintr(irq);
284 return;
285 }
286 if (vec >= 0x800) {
287 #if NSIO
288 sio_iointr(framep, vec);
289 #endif
290 return;
291 }
292 panic("kn20aa_iointr: weird vec 0x%x\n", vec);
293 }
294
295 void
296 kn20aa_enable_intr(irq)
297 int irq;
298 {
299
300 /*
301 * From disassembling small bits of the OSF/1 kernel:
302 * the following appears to enable a given interrupt request.
303 * "blech." I'd give valuable body parts for better docs or
304 * for a good decompiler.
305 */
306 alpha_mb();
307 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */
308 alpha_mb();
309 }
310
311 void
312 kn20aa_disable_intr(irq)
313 int irq;
314 {
315
316 alpha_mb();
317 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */
318 alpha_mb();
319 }
320
321 struct kn20aa_intrhand *
322 kn20aa_attach_intr(chain, level, func, arg)
323 struct kn20aa_intrchain *chain;
324 int level;
325 int (*func) __P((void *));
326 void *arg;
327 {
328 struct kn20aa_intrhand *nintrhand;
329
330 nintrhand = (struct kn20aa_intrhand *)
331 malloc(sizeof *nintrhand, M_DEVBUF, M_WAITOK);
332
333 nintrhand->ih_fun = func;
334 nintrhand->ih_arg = arg;
335 nintrhand->ih_count = 0;
336 nintrhand->ih_level = level;
337 TAILQ_INSERT_TAIL(chain, nintrhand, ih_q);
338
339 return (nintrhand);
340 }
341