pci_kn20aa.c revision 1.10 1 /* $NetBSD: pci_kn20aa.c,v 1.10 1996/08/14 05:44:31 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 struct kn20aa_intrhand *kn20aa_attach_intr __P((struct kn20aa_intrchain *,
90 int, int (*) (void *), void *));
91
92 void
93 pci_kn20aa_pickintr(ccp)
94 struct cia_config *ccp;
95 {
96 int i;
97 struct kn20aa_intrhand *nintrhand;
98 bus_chipset_tag_t bc = &ccp->cc_bc;
99 pci_chipset_tag_t pc = &ccp->cc_pc;
100
101 for (i = 0; i < KN20AA_MAX_IRQ; i++)
102 TAILQ_INIT(&kn20aa_pci_intrs[i]);
103
104 pc->pc_intr_v = ccp;
105 pc->pc_intr_map = dec_kn20aa_intr_map;
106 pc->pc_intr_string = dec_kn20aa_intr_string;
107 pc->pc_intr_establish = dec_kn20aa_intr_establish;
108 pc->pc_intr_disestablish = dec_kn20aa_intr_disestablish;
109
110 #if NSIO
111 sio_intr_setup(bc);
112 #endif
113
114 set_iointr(kn20aa_iointr);
115
116 #if NSIO
117 kn20aa_enable_intr(KN20AA_PCEB_IRQ);
118 #if 0 /* XXX init PCEB interrupt handler? */
119 kn20aa_attach_intr(&kn20aa_pci_intrs[KN20AA_PCEB_IRQ], ???, ???, ???);
120 #endif
121 #endif
122 }
123
124 int
125 dec_kn20aa_intr_map(ccv, bustag, buspin, line, ihp)
126 void *ccv;
127 pcitag_t bustag;
128 int buspin, line;
129 pci_intr_handle_t *ihp;
130 {
131 struct cia_config *ccp = ccv;
132 pci_chipset_tag_t pc = &ccp->cc_pc;
133 int device;
134 int kn20aa_irq;
135 void *ih;
136
137 if (buspin == 0) {
138 /* No IRQ used. */
139 return 1;
140 }
141 if (buspin > 4) {
142 printf("pci_map_int: bad interrupt pin %d\n", buspin);
143 return 1;
144 }
145
146 /*
147 * Slot->interrupt translation. Appears to work, though it
148 * may not hold up forever.
149 *
150 * The DEC engineers who did this hardware obviously engaged
151 * in random drug testing.
152 */
153 pci_decompose_tag(pc, bustag, NULL, &device, NULL);
154 switch (device) {
155 case 11:
156 case 12:
157 kn20aa_irq = ((device - 11) + 0) * 4;
158 break;
159
160 case 7:
161 kn20aa_irq = 8;
162 break;
163
164 case 9:
165 kn20aa_irq = 12;
166 break;
167
168 case 8:
169 kn20aa_irq = 16;
170 break;
171
172 default:
173 panic("pci_kn20aa_map_int: invalid device number %d\n",
174 device);
175 }
176
177 kn20aa_irq += buspin - 1;
178 if (kn20aa_irq > KN20AA_MAX_IRQ)
179 panic("pci_kn20aa_map_int: kn20aa_irq too large (%d)\n",
180 kn20aa_irq);
181
182 *ihp = kn20aa_irq;
183 return (0);
184 }
185
186 const char *
187 dec_kn20aa_intr_string(ccv, ih)
188 void *ccv;
189 pci_intr_handle_t ih;
190 {
191 struct cia_config *ccp = ccv;
192 static char irqstr[15]; /* 11 + 2 + NULL + sanity */
193
194 if (ih > KN20AA_MAX_IRQ)
195 panic("dec_kn20aa_a50_intr_string: bogus kn20aa IRQ 0x%x\n",
196 ih);
197
198 sprintf(irqstr, "kn20aa irq %d", ih);
199 return (irqstr);
200 }
201
202 void *
203 dec_kn20aa_intr_establish(ccv, ih, level, func, arg)
204 void *ccv, *arg;
205 pci_intr_handle_t ih;
206 int level;
207 int (*func) __P((void *));
208 {
209 struct cia_config *ccp = ccv;
210 void *cookie;
211
212 if (ih > KN20AA_MAX_IRQ)
213 panic("dec_kn20aa_intr_establish: bogus kn20aa IRQ 0x%x\n",
214 ih);
215
216 cookie = kn20aa_attach_intr(&kn20aa_pci_intrs[ih], level, func, arg);
217 kn20aa_enable_intr(ih);
218 return (cookie);
219 }
220
221 void
222 dec_kn20aa_intr_disestablish(ccv, cookie)
223 void *ccv, *cookie;
224 {
225 struct cia_config *ccp = ccv;
226
227 panic("dec_kn20aa_intr_disestablish not implemented"); /* XXX */
228 }
229
230 /*
231 * caught a stray interrupt; notify if not too many seen already.
232 */
233 void
234 kn20aa_pci_strayintr(irq)
235 int irq;
236 {
237
238 if (++kn20aa_pci_strayintrcnt[irq] <= PCI_STRAY_MAX)
239 log(LOG_ERR, "stray PCI interrupt %d%s\n", irq,
240 kn20aa_pci_strayintrcnt[irq] >= PCI_STRAY_MAX ?
241 "; stopped logging" : "");
242 }
243
244 void
245 kn20aa_iointr(framep, vec)
246 void *framep;
247 unsigned long vec;
248 {
249 struct kn20aa_intrhand *ih;
250 int irq, handled;
251
252 if (vec >= 0x900) {
253 if (vec >= 0x900 + (KN20AA_MAX_IRQ << 4))
254 panic("kn20aa_iointr: vec 0x%x out of range\n", vec);
255 irq = (vec - 0x900) >> 4;
256
257 #ifdef EVCNT_COUNTERS
258 kn20aa_intr_evcnt.ev_count++;
259 #else
260 if (KN20AA_MAX_IRQ != INTRCNT_KN20AA_IRQ_LEN)
261 panic("kn20aa interrupt counter sizes inconsistent");
262 intrcnt[INTRCNT_KN20AA_IRQ + irq]++;
263 #endif
264
265 for (ih = kn20aa_pci_intrs[irq].tqh_first, handled = 0;
266 ih != NULL; ih = ih->ih_q.tqe_next) {
267 int rv;
268
269 rv = (*ih->ih_fun)(ih->ih_arg);
270
271 ih->ih_count++;
272 handled = handled || (rv != 0);
273 }
274 if (!handled)
275 kn20aa_pci_strayintr(irq);
276 return;
277 }
278 if (vec >= 0x800) {
279 #if NSIO
280 sio_iointr(framep, vec);
281 #endif
282 return;
283 }
284 panic("kn20aa_iointr: weird vec 0x%x\n", vec);
285 }
286
287 void
288 kn20aa_enable_intr(irq)
289 int irq;
290 {
291
292 /*
293 * From disassembling small bits of the OSF/1 kernel:
294 * the following appears to enable a given interrupt request.
295 * "blech." I'd give valuable body parts for better docs or
296 * for a good decompiler.
297 */
298 alpha_mb();
299 REGVAL(0x8780000000L + 0x40L) |= (1 << irq); /* XXX */
300 alpha_mb();
301 }
302
303 void
304 kn20aa_disable_intr(irq)
305 int irq;
306 {
307
308 alpha_mb();
309 REGVAL(0x8780000000L + 0x40L) &= ~(1 << irq); /* XXX */
310 alpha_mb();
311 }
312
313 struct kn20aa_intrhand *
314 kn20aa_attach_intr(chain, level, func, arg)
315 struct kn20aa_intrchain *chain;
316 int level;
317 int (*func) __P((void *));
318 void *arg;
319 {
320 struct kn20aa_intrhand *nintrhand;
321
322 nintrhand = (struct kn20aa_intrhand *)
323 malloc(sizeof *nintrhand, M_DEVBUF, M_WAITOK);
324
325 nintrhand->ih_fun = func;
326 nintrhand->ih_arg = arg;
327 nintrhand->ih_count = 0;
328 nintrhand->ih_level = level;
329 TAILQ_INSERT_TAIL(chain, nintrhand, ih_q);
330
331 return (nintrhand);
332 }
333