pcibios.c revision 1.18 1 /* $NetBSD: pcibios.c,v 1.18 2004/04/30 02:45:37 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1999, by UCHIYAMA Yasushi
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. The name of the developer may NOT be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 /*
66 * Interface to the PCI BIOS and PCI Interrupt Routing table.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: pcibios.c,v 1.18 2004/04/30 02:45:37 christos Exp $");
71
72 #include "opt_pcibios.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/device.h>
77 #include <sys/malloc.h>
78
79 #include <dev/isa/isareg.h>
80 #include <machine/isa_machdep.h>
81
82 #include <dev/pci/pcireg.h>
83 #include <dev/pci/pcivar.h>
84 #include <dev/pci/pcidevs.h>
85
86 #include <i386/pci/pcibios.h>
87 #ifdef PCIBIOS_INTR_FIXUP
88 #include <i386/pci/pci_intr_fixup.h>
89 #endif
90 #ifdef PCIBIOS_BUS_FIXUP
91 #include <i386/pci/pci_bus_fixup.h>
92 #endif
93 #ifdef PCIBIOS_ADDR_FIXUP
94 #include <i386/pci/pci_addr_fixup.h>
95 #endif
96
97 #include <machine/bios32.h>
98
99 #ifdef PCIBIOSVERBOSE
100 int pcibiosverbose = 1;
101 #endif
102
103 int pcibios_present;
104
105 struct pcibios_pir_header pcibios_pir_header;
106 struct pcibios_intr_routing *pcibios_pir_table;
107 int pcibios_pir_table_nentries;
108 int pcibios_max_bus;
109
110 struct bios32_entry pcibios_entry;
111
112 void pcibios_pir_init(void);
113
114 int pcibios_get_status(u_int32_t *, u_int32_t *, u_int32_t *,
115 u_int32_t *, u_int32_t *, u_int32_t *, u_int32_t *);
116 int pcibios_get_intr_routing(struct pcibios_intr_routing *,
117 int *, u_int16_t *);
118
119 int pcibios_return_code(u_int16_t, const char *);
120
121 void pcibios_print_exclirq(void);
122
123 #ifdef PCIBIOS_LIBRETTO_FIXUP
124 /* for Libretto L2/L3 hack */
125 static void pcibios_fixup_pir_table(void);
126 static void pcibios_fixup_pir_table_mask(struct pcibios_linkmap *);
127
128 struct pcibios_linkmap pir_mask[] = {
129 { 2, 0x0040 },
130 { 7, 0x0080 },
131 { 8, 0x0020 },
132 { 0, 0x0000 }
133 };
134 #endif
135
136 #ifdef PCIINTR_DEBUG
137 void pcibios_print_pir_table(void);
138 #endif
139
140 #define PCI_IRQ_TABLE_START 0xf0000
141 #define PCI_IRQ_TABLE_END 0xfffff
142
143 static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
144 struct pci_bridge_hook_arg {
145 void (*func)(pci_chipset_tag_t, pcitag_t, void *);
146 void *arg;
147 };
148
149 void
150 pcibios_init()
151 {
152 struct bios32_entry_info ei;
153 u_int32_t rev_maj, rev_min, mech1, mech2, scmech1, scmech2;
154
155 if (bios32_service(BIOS32_MAKESIG('$', 'P', 'C', 'I'),
156 &pcibios_entry, &ei) == 0) {
157 /*
158 * No PCI BIOS found; will fall back on old
159 * mechanism.
160 */
161 return;
162 }
163
164 /*
165 * We've located the PCI BIOS service; get some information
166 * about it.
167 */
168 if (pcibios_get_status(&rev_maj, &rev_min, &mech1, &mech2,
169 &scmech1, &scmech2, &pcibios_max_bus) != PCIBIOS_SUCCESS) {
170 /*
171 * We can't use the PCI BIOS; will fall back on old
172 * mechanism.
173 */
174 return;
175 }
176
177 printf("PCI BIOS rev. %d.%d found at 0x%lx\n", rev_maj, rev_min >> 4,
178 ei.bei_entry);
179 #ifdef PCIBIOSVERBOSE
180 printf("pcibios: config mechanism %s%s, special cycles %s%s, "
181 "last bus %d\n",
182 mech1 ? "[1]" : "[x]",
183 mech2 ? "[2]" : "[x]",
184 scmech1 ? "[1]" : "[x]",
185 scmech2 ? "[2]" : "[x]",
186 pcibios_max_bus);
187
188 #endif
189
190 /*
191 * The PCI BIOS tells us the config mechanism; fill it in now
192 * so that pci_mode_detect() doesn't have to look for it.
193 */
194 pci_mode = mech1 ? 1 : 2;
195
196 pcibios_present = 1;
197
198 /*
199 * Find the PCI IRQ Routing table.
200 */
201 pcibios_pir_init();
202
203 #ifdef PCIBIOS_INTR_FIXUP
204 if (pcibios_pir_table != NULL) {
205 int rv;
206 u_int16_t pciirq;
207
208 /*
209 * Fixup interrupt routing.
210 */
211 rv = pci_intr_fixup(NULL, X86_BUS_SPACE_IO, &pciirq);
212 switch (rv) {
213 case -1:
214 /* Non-fatal error. */
215 printf("Warning: unable to fix up PCI interrupt "
216 "routing\n");
217 break;
218
219 case 1:
220 /* Fatal error. */
221 panic("pcibios_init: interrupt fixup failed");
222 break;
223 }
224
225 /*
226 * XXX Clear `pciirq' from the ISA interrupt allocation
227 * XXX mask.
228 */
229 }
230 #endif
231
232 #ifdef PCIBIOS_BUS_FIXUP
233 pcibios_max_bus = pci_bus_fixup(NULL, 0);
234 #ifdef PCIBIOSVERBOSE
235 printf("PCI bus #%d is the last bus\n", pcibios_max_bus);
236 #endif
237 #endif
238
239 #ifdef PCIBIOS_ADDR_FIXUP
240 pci_addr_fixup(NULL, pcibios_max_bus);
241 #endif
242 }
243
244 void
245 pcibios_pir_init()
246 {
247 char devinfo[256];
248 paddr_t pa;
249 caddr_t p;
250 unsigned char cksum;
251 u_int16_t tablesize;
252 u_int8_t rev_maj, rev_min;
253 int i;
254
255 for (pa = PCI_IRQ_TABLE_START; pa < PCI_IRQ_TABLE_END; pa += 16) {
256 p = (caddr_t)ISA_HOLE_VADDR(pa);
257 if (*(int *)p != BIOS32_MAKESIG('$', 'P', 'I', 'R')) {
258 /*
259 * XXX: Some laptops (Toshiba/Libretto L series)
260 * use _PIR instead of $PIR. So we try that too.
261 */
262 if (*(int *)p != BIOS32_MAKESIG('_', 'P', 'I', 'R'))
263 continue;
264 }
265
266 rev_min = *(p + 4);
267 rev_maj = *(p + 5);
268 tablesize = *(u_int16_t *)(p + 6);
269
270 cksum = 0;
271 for (i = 0; i < tablesize; i++)
272 cksum += *(unsigned char *)(p + i);
273
274 printf("PCI IRQ Routing Table rev. %d.%d found at 0x%lx, "
275 "size %d bytes (%d entries)\n", rev_maj, rev_min, pa,
276 tablesize, (tablesize - 32) / 16);
277
278 if (cksum != 0) {
279 printf("pcibios_pir_init: bad IRQ table checksum\n");
280 continue;
281 }
282
283 if (tablesize < 32 || (tablesize % 16) != 0) {
284 printf("pcibios_pir_init: bad IRQ table size\n");
285 continue;
286 }
287
288 if (rev_maj != 1 || rev_min != 0) {
289 printf("pcibios_pir_init: unsupported IRQ table "
290 "version\n");
291 continue;
292 }
293
294 /*
295 * We can handle this table! Make a copy of it.
296 */
297 memcpy(&pcibios_pir_header, p, 32);
298 pcibios_pir_table = malloc(tablesize - 32, M_DEVBUF,
299 M_NOWAIT);
300 if (pcibios_pir_table == NULL) {
301 printf("pcibios_pir_init: no memory for $PIR\n");
302 return;
303 }
304 memcpy(pcibios_pir_table, p + 32, tablesize - 32);
305 pcibios_pir_table_nentries = (tablesize - 32) / 16;
306
307 printf("PCI Interrupt Router at %03d:%02d:%01d",
308 pcibios_pir_header.router_bus,
309 PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
310 PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
311 if (pcibios_pir_header.compat_router != 0) {
312 pci_devinfo(pcibios_pir_header.compat_router, 0, 0,
313 devinfo, sizeof(devinfo));
314 printf(" (%s compatible)", devinfo);
315 }
316 printf("\n");
317 pcibios_print_exclirq();
318
319 #ifdef PCIBIOS_LIBRETTO_FIXUP
320 /* for Libretto L2/L3 hack */
321 pcibios_fixup_pir_table();
322 #endif
323 #ifdef PCIINTR_DEBUG
324 pcibios_print_pir_table();
325 #endif
326 return;
327 }
328
329 /*
330 * If there was no PIR table found, try using the PCI BIOS
331 * Get Interrupt Routing call.
332 *
333 * XXX The interface to this call sucks; just allocate enough
334 * XXX room for 32 entries.
335 */
336 pcibios_pir_table_nentries = 32;
337 pcibios_pir_table = malloc(pcibios_pir_table_nentries *
338 sizeof(*pcibios_pir_table), M_DEVBUF, M_NOWAIT);
339 if (pcibios_pir_table == NULL) {
340 printf("pcibios_pir_init: no memory for $PIR\n");
341 return;
342 }
343 if (pcibios_get_intr_routing(pcibios_pir_table,
344 &pcibios_pir_table_nentries,
345 &pcibios_pir_header.exclusive_irq) != PCIBIOS_SUCCESS) {
346 printf("No PCI IRQ Routing information available.\n");
347 free(pcibios_pir_table, M_DEVBUF);
348 pcibios_pir_table = NULL;
349 pcibios_pir_table_nentries = 0;
350 return;
351 }
352 printf("PCI BIOS has %d Interrupt Routing table entries\n",
353 pcibios_pir_table_nentries);
354 pcibios_print_exclirq();
355
356 #ifdef PCIBIOS_LIBRETTO_FIXUP
357 /* for Libretto L2/L3 hack */
358 pcibios_fixup_pir_table();
359 #endif
360 #ifdef PCIINTR_DEBUG
361 pcibios_print_pir_table();
362 #endif
363 }
364
365 int
366 pcibios_get_status(u_int32_t *rev_maj, u_int32_t *rev_min,
367 u_int32_t *mech1, u_int32_t *mech2, u_int32_t *scmech1, u_int32_t *scmech2,
368 u_int32_t *maxbus)
369 {
370 u_int16_t ax, bx, cx;
371 u_int32_t edx;
372 int rv;
373
374 __asm __volatile("lcall *(%%edi) ; \
375 jc 1f ; \
376 xor %%ah, %%ah ; \
377 1:"
378 : "=a" (ax), "=b" (bx), "=c" (cx), "=d" (edx)
379 : "0" (0xb101), "D" (&pcibios_entry));
380
381 rv = pcibios_return_code(ax, "pcibios_get_status");
382 if (rv != PCIBIOS_SUCCESS)
383 return (rv);
384
385 if (edx != BIOS32_MAKESIG('P', 'C', 'I', ' '))
386 return (PCIBIOS_SERVICE_NOT_PRESENT); /* XXX */
387
388 /*
389 * Fill in the various pieces if info we're looking for.
390 */
391 *mech1 = ax & 1;
392 *mech2 = ax & (1 << 1);
393 *scmech1 = ax & (1 << 4);
394 *scmech2 = ax & (1 << 5);
395 *rev_maj = (bx >> 8) & 0xff;
396 *rev_min = bx & 0xff;
397 *maxbus = cx & 0xff;
398
399 return (PCIBIOS_SUCCESS);
400 }
401
402 int
403 pcibios_get_intr_routing(struct pcibios_intr_routing *table,
404 int *nentries, u_int16_t *exclirq)
405 {
406 u_int16_t ax, bx;
407 int rv;
408 struct {
409 u_int16_t size;
410 caddr_t offset;
411 u_int16_t segment;
412 } __attribute__((__packed__)) args;
413
414 args.size = *nentries * sizeof(*table);
415 args.offset = (caddr_t)table;
416 args.segment = GSEL(GDATA_SEL, SEL_KPL);
417
418 memset(table, 0, args.size);
419
420 __asm __volatile("lcall *(%%esi) ; \
421 jc 1f ; \
422 xor %%ah, %%ah ; \
423 1: movw %w2, %%ds ; \
424 movw %w2, %%es"
425 : "=a" (ax), "=b" (bx)
426 : "r" GSEL(GDATA_SEL, SEL_KPL), "0" (0xb10e), "1" (0),
427 "D" (&args), "S" (&pcibios_entry));
428
429 rv = pcibios_return_code(ax, "pcibios_get_intr_routing");
430 if (rv != PCIBIOS_SUCCESS)
431 return (rv);
432
433 *nentries = args.size / sizeof(*table);
434 *exclirq = bx;
435
436 return (PCIBIOS_SUCCESS);
437 }
438
439 int
440 pcibios_return_code(u_int16_t ax, const char *func)
441 {
442 const char *errstr;
443 int rv = ax >> 8;
444
445 switch (rv) {
446 case PCIBIOS_SUCCESS:
447 return (PCIBIOS_SUCCESS);
448
449 case PCIBIOS_SERVICE_NOT_PRESENT:
450 errstr = "service not present";
451 break;
452
453 case PCIBIOS_FUNCTION_NOT_SUPPORTED:
454 errstr = "function not supported";
455 break;
456
457 case PCIBIOS_BAD_VENDOR_ID:
458 errstr = "bad vendor ID";
459 break;
460
461 case PCIBIOS_DEVICE_NOT_FOUND:
462 errstr = "device not found";
463 break;
464
465 case PCIBIOS_BAD_REGISTER_NUMBER:
466 errstr = "bad register number";
467 break;
468
469 case PCIBIOS_SET_FAILED:
470 errstr = "set failed";
471 break;
472
473 case PCIBIOS_BUFFER_TOO_SMALL:
474 errstr = "buffer too small";
475 break;
476
477 default:
478 printf("%s: unknown return code 0x%x\n", func, rv);
479 return (rv);
480 }
481
482 printf("%s: %s\n", func, errstr);
483 return (rv);
484 }
485
486 void
487 pcibios_print_exclirq()
488 {
489 int i;
490
491 if (pcibios_pir_header.exclusive_irq) {
492 printf("PCI Exclusive IRQs:");
493 for (i = 0; i < 16; i++) {
494 if (pcibios_pir_header.exclusive_irq & (1 << i))
495 printf(" %d", i);
496 }
497 printf("\n");
498 }
499 }
500
501 #ifdef PCIBIOS_LIBRETTO_FIXUP
502 /* for Libretto L2/L3 hack */
503 static void
504 pcibios_fixup_pir_table()
505 {
506 struct pcibios_linkmap *m;
507
508 for (m = pir_mask; m->link != 0; m++)
509 pcibios_fixup_pir_table_mask(m);
510 }
511
512 void
513 pcibios_fixup_pir_table_mask(mask)
514 struct pcibios_linkmap *mask;
515 {
516 int i, j;
517
518 for (i = 0; i < pcibios_pir_table_nentries; i++) {
519 for (j = 0; j < 4; j++) {
520 if (pcibios_pir_table[i].linkmap[j].link == mask->link) {
521 pcibios_pir_table[i].linkmap[j].bitmap
522 &= mask->bitmap;
523 }
524 }
525 }
526 }
527 #endif
528
529 #ifdef PCIINTR_DEBUG
530 void
531 pcibios_print_pir_table()
532 {
533 int i, j;
534
535 for (i = 0; i < pcibios_pir_table_nentries; i++) {
536 printf("PIR Entry %d:\n", i);
537 printf("\tBus: %d Device: %d\n",
538 pcibios_pir_table[i].bus,
539 PIR_DEVFUNC_DEVICE(pcibios_pir_table[i].device));
540 for (j = 0; j < 4; j++) {
541 printf("\t\tINT%c: link 0x%02x bitmap 0x%04x\n",
542 'A' + j,
543 pcibios_pir_table[i].linkmap[j].link,
544 pcibios_pir_table[i].linkmap[j].bitmap);
545 }
546 }
547 }
548 #endif
549
550 void
551 pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
552 void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
553 {
554 pci_device_foreach_min(pc, 0, maxbus, func, context);
555 }
556
557 void
558 pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
559 void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
560 {
561 const struct pci_quirkdata *qd;
562 int bus, device, function, maxdevs, nfuncs;
563 pcireg_t id, bhlcr;
564 pcitag_t tag;
565
566 for (bus = minbus; bus <= maxbus; bus++) {
567 maxdevs = pci_bus_maxdevs(pc, bus);
568 for (device = 0; device < maxdevs; device++) {
569 tag = pci_make_tag(pc, bus, device, 0);
570 id = pci_conf_read(pc, tag, PCI_ID_REG);
571
572 /* Invalid vendor ID value? */
573 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
574 continue;
575 /* XXX Not invalid, but we've done this ~forever. */
576 if (PCI_VENDOR(id) == 0)
577 continue;
578
579 qd = pci_lookup_quirkdata(PCI_VENDOR(id),
580 PCI_PRODUCT(id));
581
582 bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
583 if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
584 (qd != NULL &&
585 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
586 nfuncs = 8;
587 else
588 nfuncs = 1;
589
590 for (function = 0; function < nfuncs; function++) {
591 tag = pci_make_tag(pc, bus, device, function);
592 id = pci_conf_read(pc, tag, PCI_ID_REG);
593
594 /* Invalid vendor ID value? */
595 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
596 continue;
597 /*
598 * XXX Not invalid, but we've done this
599 * ~forever.
600 */
601 if (PCI_VENDOR(id) == 0)
602 continue;
603 (*func)(pc, tag, context);
604 }
605 }
606 }
607 }
608
609 void
610 pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
611 void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
612 {
613 struct pci_bridge_hook_arg bridge_hook;
614
615 bridge_hook.func = func;
616 bridge_hook.arg = ctx;
617
618 pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
619 &bridge_hook);
620 }
621
622 void
623 pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
624 {
625 struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
626 pcireg_t reg;
627
628 reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
629 if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
630 (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
631 PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
632 (*bridge_hook->func)(pc, tag, bridge_hook->arg);
633 }
634 }
635