pci_kn8ae.c revision 1.14 1 /* $NetBSD: pci_kn8ae.c,v 1.14 1999/02/12 06:25:14 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1997 by Matthew Jacob
5 * NASA AMES Research Center.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34
35 __KERNEL_RCSID(0, "$NetBSD: pci_kn8ae.c,v 1.14 1999/02/12 06:25:14 thorpej Exp $");
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/syslog.h>
45
46 #include <vm/vm.h>
47
48 #include <machine/autoconf.h>
49
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52
53 #include <alpha/pci/dwlpxreg.h>
54 #include <alpha/pci/dwlpxvar.h>
55 #include <alpha/pci/pci_kn8ae.h>
56
57 #ifndef EVCNT_COUNTERS
58 #include <machine/intrcnt.h>
59 #endif
60
61 int dec_kn8ae_intr_map __P((void *, pcitag_t, int, int,
62 pci_intr_handle_t *));
63 const char *dec_kn8ae_intr_string __P((void *, pci_intr_handle_t));
64 void *dec_kn8ae_intr_establish __P((void *, pci_intr_handle_t,
65 int, int (*func)(void *), void *));
66 void dec_kn8ae_intr_disestablish __P((void *, void *));
67
68 struct vectab {
69 int (*func) __P((void *));
70 void *arg;
71 } vectab[DWLPX_NIONODE][DWLPX_NHOSE][DWLPX_MAXDEV];
72 static u_int32_t imaskcache[DWLPX_NIONODE][DWLPX_NHOSE][NHPC];
73
74 #ifdef EVCNT_COUNTERS
75 struct evcnt kn8ae_intr_evcnt;
76 #endif
77
78 int kn8ae_spurious __P((void *));
79 void kn8ae_iointr __P((void *framep, unsigned long vec));
80 void kn8ae_enadis_intr __P((pci_intr_handle_t, int));
81 void kn8ae_enable_intr __P((pci_intr_handle_t irq));
82 void kn8ae_disable_intr __P((pci_intr_handle_t irq));
83
84 void
85 pci_kn8ae_pickintr(ccp, first)
86 struct dwlpx_config *ccp;
87 int first;
88 {
89 int io, hose, dev;
90 pci_chipset_tag_t pc = &ccp->cc_pc;
91
92 pc->pc_intr_v = ccp;
93 pc->pc_intr_map = dec_kn8ae_intr_map;
94 pc->pc_intr_string = dec_kn8ae_intr_string;
95 pc->pc_intr_establish = dec_kn8ae_intr_establish;
96 pc->pc_intr_disestablish = dec_kn8ae_intr_disestablish;
97
98 /* Not supported on KN8AE. */
99 pc->pc_pciide_compat_intr_establish = NULL;
100
101 if (!first) {
102 return;
103 }
104
105 for (io = 0; io < DWLPX_NIONODE; io++) {
106 for (hose = 0; hose < DWLPX_NHOSE; hose++) {
107 for (dev = 0; dev < DWLPX_MAXDEV; dev++) {
108 vectab[io][hose][dev].func = kn8ae_spurious;
109 vectab[io][hose][dev].arg = (void *)
110 (u_int64_t) DWLPX_MVEC(io, hose, dev);
111 }
112 }
113 }
114 for (io = 0; io < DWLPX_NIONODE; io++) {
115 for (hose = 0; hose < DWLPX_NHOSE; hose++) {
116 for (dev = 0; dev < NHPC; dev++) {
117 imaskcache[io][hose][dev] = DWLPX_IMASK_DFLT;
118 }
119 }
120 }
121 set_iointr(kn8ae_iointr);
122 }
123
124 int
125 dec_kn8ae_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 int device, ionode, hose;
132 struct dwlpx_config *ccp = ccv;
133 pci_chipset_tag_t pc = &ccp->cc_pc;
134
135 if (buspin == 0) {
136 /* No IRQ used. */
137 return 1;
138 }
139 if (buspin > 4) {
140 printf("dec_kn8ae_intr_map: bad interrupt pin %d\n", buspin);
141 return 1;
142 }
143 alpha_pci_decompose_tag(pc, bustag, NULL, &device, NULL);
144 ionode = ccp->cc_sc->dwlpx_node - 4;
145 hose = ccp->cc_sc->dwlpx_hosenum;
146
147 /*
148 * handle layout:
149 * bits 0..15 DWLPX_MVEC(ionode, hose, device)
150 * bits 16-24 buspin (1..N)
151 * bits 24-31 IPL
152 */
153 *ihp = DWLPX_MVEC(ionode, hose, device) | (buspin << 16) | (14 << 24);
154 return (0);
155 }
156
157 const char *
158 dec_kn8ae_intr_string(ccv, ih)
159 void *ccv;
160 pci_intr_handle_t ih;
161 {
162 static char irqstr[64];
163 sprintf(irqstr, "kn8ae irq %ld vector 0x%lx PCI Interrupt Pin %c",
164 (ih >> 24), ih & 0xffff, (int)(((ih >> 16) & 0x7) - 1) + 'A');
165 return (irqstr);
166 }
167
168 void *
169 dec_kn8ae_intr_establish(ccv, ih, level, func, arg)
170 void *ccv;
171 pci_intr_handle_t ih;
172 int level;
173 int (*func) __P((void *));
174 void *arg;
175 {
176 struct dwlpx_config *ccp = ccv;
177 void *cookie = NULL;
178 int ionode, hose, device, s;
179 struct vectab *vp;
180
181 ionode = ccp->cc_sc->dwlpx_node - 4;
182 hose = ccp->cc_sc->dwlpx_hosenum;
183 device = DWLPX_MVEC_PCISLOT(ih);
184
185 if (ionode < 0 || ionode >= DWLPX_NIONODE) {
186 panic("dec_kn8ae_intr_establish: bad ionode %d\n", ionode);
187 }
188 if (hose < 0 || hose >= DWLPX_NHOSE) {
189 panic("dec_kn8ae_intr_establish: bad hose %d\n", hose);
190 }
191 if (device < 0 || device >= DWLPX_MAXDEV) {
192 panic("dec_kn8ae_intr_establish: bad device %d\n", device);
193 }
194
195 vp = &vectab[ionode][hose][device];
196 if (vp->func != kn8ae_spurious) {
197 printf("dec_kn8ae_intr_establish: vector 0x%lx already used\n",
198 ih & 0xffff);
199 return (cookie);
200 }
201
202 s = splhigh();
203 vp->func = func;
204 vp->arg = arg;
205 (void) splx(s);
206 kn8ae_enable_intr(ih);
207 cookie = (void *) (u_int64_t) DWLPX_MVEC(ionode, hose, device);
208 return (cookie);
209 }
210
211 void
212 dec_kn8ae_intr_disestablish(ccv, cookie)
213 void *ccv, *cookie;
214 {
215 int ionode, hose, device, s;
216 struct vectab *vp;
217
218 ionode = DWLPX_MVEC_IONODE(cookie);
219 hose = DWLPX_MVEC_HOSE(cookie);
220 device = DWLPX_MVEC_PCISLOT(cookie);
221 if (ionode < 0 || ionode >= DWLPX_NIONODE ||
222 hose < 0 || hose >= DWLPX_NHOSE ||
223 device < 0 || device >= DWLPX_MAXDEV) {
224 return;
225 }
226 vp = &vectab[ionode][hose][device];
227 s = splhigh();
228 vp->func = kn8ae_spurious;
229 vp->arg = cookie;
230 (void) splx(s);
231 }
232
233 int
234 kn8ae_spurious(arg)
235 void *arg;
236 {
237 int ionode, hose, device;
238 ionode = DWLPX_MVEC_IONODE(arg);
239 hose = DWLPX_MVEC_HOSE(arg);
240 device = DWLPX_MVEC_PCISLOT(arg);
241 printf("Spurious Interrupt from TLSB Node %d Hose %d Slot %d\n",
242 ionode + 4, hose, device);
243 return (-1);
244 }
245
246
247 void
248 kn8ae_iointr(framep, vec)
249 void *framep;
250 unsigned long vec;
251 {
252 struct vectab *vp;
253 int ionode, hose, device;
254 if ((vec & DWLPX_VEC_EMARK) != 0) {
255 #ifdef EVCNT_COUNTERS
256 kn8ae_intr_evcnt.ev_count++;
257 #else
258 intrcnt[INTRCNT_KN8AE_IRQ]++;
259 #endif
260 dwlpx_iointr(framep, vec);
261 return;
262 }
263 if ((vec & DWLPX_VEC_MARK) == 0) {
264 panic("kn8ae_iointr: vec 0x%lx\n", vec);
265 /* NOTREACHED */
266 }
267 ionode = DWLPX_MVEC_IONODE(vec);
268 hose = DWLPX_MVEC_HOSE(vec);
269 device = DWLPX_MVEC_PCISLOT(vec);
270 #ifdef EVCNT_COUNTERS
271 kn8ae_intr_evcnt.ev_count++;
272 #else
273 intrcnt[INTRCNT_KN8AE_IRQ+1]++;
274 #endif
275
276 if (ionode < 0 || ionode >= DWLPX_NIONODE ||
277 hose < 0 || hose >= DWLPX_NHOSE ||
278 device < 0 || device >= DWLPX_MAXDEV) {
279 panic("kn8ae_iointr: malformed vector 0x%lx\n", vec);
280 /* NOTREACHED */
281 }
282 vp = &vectab[ionode][hose][device];
283 if ((*vp->func)(vp->arg) == 0) {
284 #if 0
285 printf("kn8ae_iointr: TLSB Node %d Hose %d Slot %d - "
286 " unclaimed interrupt\n", ionode + 4, hose, device);
287 #endif
288 }
289 }
290
291 void
292 kn8ae_enadis_intr(irq, onoff)
293 pci_intr_handle_t irq;
294 int onoff;
295 {
296 unsigned long paddr;
297 u_int32_t val;
298 int ionode, hose, device, hpc, busp, s;
299
300 ionode = DWLPX_MVEC_IONODE(irq);
301 hose = DWLPX_MVEC_HOSE(irq);
302 device = DWLPX_MVEC_PCISLOT(irq);
303 busp = 1 << (((irq >> 16) & 0xff) - 1);
304 paddr = (1LL << 39);
305 paddr |= (unsigned long) ionode << 36;
306 paddr |= (unsigned long) hose << 34;
307 if (device < 4) {
308 hpc = 0;
309 } else if (device < 8) {
310 hpc = 1;
311 device -= 4;
312 } else {
313 hpc = 2;
314 device -= 8;
315 }
316 busp <<= (device << 2);
317 val = imaskcache[ionode][hose][hpc];
318 if (onoff)
319 val |= busp;
320 else
321 val &= ~busp;
322 imaskcache[ionode][hose][hpc] = val;
323 #if 0
324 printf("kn8ae_%s_intr: irq %lx imsk 0x%x hpc %d TLSB node %d hose %d\n",
325 onoff? "enable" : "disable", irq, val, hpc, ionode + 4, hose);
326 #endif
327 s = splhigh();
328 REGVAL(PCIA_IMASK(hpc) + paddr) = val;
329 alpha_mb();
330 (void) splx(s);
331 }
332
333 void
334 kn8ae_enable_intr(irq)
335 pci_intr_handle_t irq;
336 {
337 kn8ae_enadis_intr(irq, 1);
338 }
339
340 void
341 kn8ae_disable_intr(irq)
342 pci_intr_handle_t irq;
343 {
344 kn8ae_enadis_intr(irq, 0);
345 }
346