pci_kn20aa.c revision 1.13 1 /* $NetBSD: pci_kn20aa.c,v 1.13 1996/08/15 22:17:44 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 8:
170 kn20aa_irq = 16;
171 break;
172
173 default:
174 panic("pci_kn20aa_map_int: invalid device number %d\n",
175 device);
176 }
177
178 kn20aa_irq += buspin - 1;
179 if (kn20aa_irq > KN20AA_MAX_IRQ)
180 panic("pci_kn20aa_map_int: kn20aa_irq too large (%d)\n",
181 kn20aa_irq);
182
183 *ihp = kn20aa_irq;
184 return (0);
185 }
186
187 const char *
188 dec_kn20aa_intr_string(ccv, ih)
189 void *ccv;
190 pci_intr_handle_t ih;
191 {
192 struct cia_config *ccp = ccv;
193 static char irqstr[15]; /* 11 + 2 + NULL + sanity */
194
195 if (ih > KN20AA_MAX_IRQ)
196 panic("dec_kn20aa_a50_intr_string: bogus kn20aa IRQ 0x%x\n",
197 ih);
198
199 sprintf(irqstr, "kn20aa irq %d", ih);
200 return (irqstr);
201 }
202
203 void *
204 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
205 void *ccv, *arg;
206 pci_intr_handle_t ih;
207 int level;
208 int (*func) __P((void *));
209 {
210 struct cia_config *ccp = ccv;
211 void *cookie;
212
213 if (ih > KN20AA_MAX_IRQ)
214 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%x\n",
215 ih);
216
217 cookie = kn20aa_attach_intr(&kn20aa_pci_intrs[ih], level, func, arg);
218 kn20aa_enable_intr(ih);
219 return (cookie);
220 }
221
222 void
223 dec_kn20aa_intr_disestablish(ccv, cookie)
224 void *ccv, *cookie;
225 {
226 struct cia_config *ccp = ccv;
227
228 panic("dec_kn20aa_intr_disestablish not implemented"); /* XXX */
229 }
230
231 /*
232 * caught a stray interrupt; notify if not too many seen already.
233 */
234 void
235 kn20aa_pci_strayintr(irq)
236 int irq;
237 {
238
239 kn20aa_pci_strayintrcnt[irq]++;
240 if (kn20aa_pci_strayintrcnt[irq] == PCI_STRAY_MAX)
241 kn20aa_disable_intr(irq);
242
243 log(LOG_ERR, "stray kn20aa irq %d\n", irq);
244 if (kn20aa_pci_strayintrcnt[irq] == PCI_STRAY_MAX)
245 log(LOG_ERR, "disabling interrupts on kn20aa irq %d\n", irq);
246 }
247
248 void
249 kn20aa_iointr(framep, vec)
250 void *framep;
251 unsigned long vec;
252 {
253 struct kn20aa_intrhand *ih;
254 int irq, handled;
255
256 if (vec >= 0x900) {
257 if (vec >= 0x900 + (KN20AA_MAX_IRQ << 4))
258 panic("kn20aa_iointr: vec 0x%x out of range\n", vec);
259 irq = (vec - 0x900) >> 4;
260
261 #ifdef EVCNT_COUNTERS
262 kn20aa_intr_evcnt.ev_count++;
263 #else
264 if (KN20AA_MAX_IRQ != INTRCNT_KN20AA_IRQ_LEN)
265 panic("kn20aa interrupt counter sizes inconsistent");
266 intrcnt[INTRCNT_KN20AA_IRQ + irq]++;
267 #endif
268
269 for (ih = kn20aa_pci_intrs[irq].tqh_first, handled = 0;
270 ih != NULL; ih = ih->ih_q.tqe_next) {
271 int rv;
272
273 rv = (*ih->ih_fun)(ih->ih_arg);
274
275 ih->ih_count++;
276 handled = handled || (rv != 0);
277 }
278 if (!handled)
279 kn20aa_pci_strayintr(irq);
280 return;
281 }
282 if (vec >= 0x800) {
283 #if NSIO
284 sio_iointr(framep, vec);
285 #endif
286 return;
287 }
288 panic("kn20aa_iointr: weird vec 0x%x\n", vec);
289 }
290
291 void
292 kn20aa_enable_intr(irq)
293 int irq;
294 {
295
296 /*
297 * From disassembling small bits of the OSF/1 kernel:
298 * the following appears to enable a given interrupt request.
299 * "blech." I'd give valuable body parts for better docs or
300 * for a good decompiler.
301 */
302 alpha_mb();
303 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */
304 alpha_mb();
305 }
306
307 void
308 kn20aa_disable_intr(irq)
309 int irq;
310 {
311
312 alpha_mb();
313 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */
314 alpha_mb();
315 }
316
317 struct kn20aa_intrhand *
318 kn20aa_attach_intr(chain, level, func, arg)
319 struct kn20aa_intrchain *chain;
320 int level;
321 int (*func) __P((void *));
322 void *arg;
323 {
324 struct kn20aa_intrhand *nintrhand;
325
326 nintrhand = (struct kn20aa_intrhand *)
327 malloc(sizeof *nintrhand, M_DEVBUF, M_WAITOK);
328
329 nintrhand->ih_fun = func;
330 nintrhand->ih_arg = arg;
331 nintrhand->ih_count = 0;
332 nintrhand->ih_level = level;
333 TAILQ_INSERT_TAIL(chain, nintrhand, ih_q);
334
335 return (nintrhand);
336 }
337