2 * $Id: lspci.c,v 1.25 1999/04/26 19:45:57 mj Exp $
4 * Linux PCI Utilities -- List All PCI Devices
6 * Copyright (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
21 static int verbose; /* Show detailed information */
22 static int buscentric_view; /* Show bus addresses/IRQ's instead of CPU-visible ones */
23 static int show_hex; /* Show contents of config space as hexadecimal numbers */
24 static struct pci_filter filter; /* Device filter */
25 static int show_tree; /* Show bus tree */
26 static int machine_readable; /* Generate machine-readable output */
27 static int map_mode; /* Bus mapping mode enabled */
29 static char options[] = "nvbxs:d:ti:mgM" GENERIC_OPTIONS ;
31 static char help_msg[] = "\
32 Usage: lspci [<switches>]\n\
35 -n\t\tShow numeric ID's\n\
36 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
37 -x\t\tShow hex-dump of config space\n\
38 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
39 -d [<vendor>]:[<device>]\tShow only selected devices\n\
40 -t\t\tShow bus tree\n\
41 -m\t\tProduce machine-readable output\n\
42 -i <file>\tUse specified ID database instead of %s\n\
43 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
47 /* Communication with libpci */
49 static struct pci_access *pacc;
51 /* Format strings used for IRQ numbers and memory addresses */
54 #define IRQ_FORMAT "%08x"
56 #define IRQ_FORMAT "%d"
59 #ifdef HAVE_64BIT_ADDRESS
60 #define ADDR_FORMAT "%016Lx"
62 #define ADDR_FORMAT "%08lx"
66 #define IO_FORMAT "%016Lx"
67 #elif defined(HAVE_64BIT_ADDRESS)
68 #define IO_FORMAT "%04Lx"
70 #define IO_FORMAT "%04lx"
73 /* Our view of the PCI bus */
78 unsigned int config_cnt;
82 static struct device *first_dev;
84 static struct device *
85 scan_device(struct pci_dev *p)
87 int how_much = (show_hex > 2) ? 256 : 64;
90 if (!pci_filter_match(&filter, p))
92 d = xmalloc(sizeof(struct device));
95 if (!pci_read_block(p, 0, d->config, how_much))
96 die("Unable to read %d bytes of configuration space.", how_much);
97 if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
99 /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
100 if (!pci_read_block(p, 0, d->config+64, 64))
101 die("Unable to read cardbus bridge extension data.");
104 d->config_cnt = how_much;
105 pci_setup_cache(p, d->config, d->config_cnt);
106 pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
117 for(p=pacc->devices; p; p=p->next)
118 if (d = scan_device(p))
128 static int is_root = -1;
131 is_root = !geteuid();
136 config_fetch(struct device *d, unsigned int pos, unsigned int len)
138 if (pos + len < d->config_cnt)
140 if (pacc->method != PCI_ACCESS_DUMP && !check_root())
142 return pci_read_block(d->dev, pos, d->config + pos, len);
145 /* Config space accesses */
148 get_conf_byte(struct device *d, unsigned int pos)
150 return d->config[pos];
154 get_conf_word(struct device *d, unsigned int pos)
156 return d->config[pos] | (d->config[pos+1] << 8);
160 get_conf_long(struct device *d, unsigned int pos)
162 return d->config[pos] |
163 (d->config[pos+1] << 8) |
164 (d->config[pos+2] << 16) |
165 (d->config[pos+3] << 24);
171 compare_them(const void *A, const void *B)
173 const struct pci_dev *a = (*(const struct device **)A)->dev;
174 const struct pci_dev *b = (*(const struct device **)B)->dev;
184 if (a->func < b->func)
186 if (a->func > b->func)
194 struct device **index, **h, **last_dev;
199 for(d=first_dev; d; d=d->next)
201 h = index = alloca(sizeof(struct device *) * cnt);
202 for(d=first_dev; d; d=d->next)
204 qsort(index, cnt, sizeof(struct device *), compare_them);
205 last_dev = &first_dev;
210 last_dev = &(*h)->next;
218 #define FLAG(x,y) ((x & y) ? '+' : '-')
221 show_terse(struct device *d)
224 struct pci_dev *p = d->dev;
225 byte classbuf[128], devbuf[128];
227 printf("%02x:%02x.%x %s: %s",
231 pci_lookup_name(pacc, classbuf, sizeof(classbuf),
233 get_conf_word(d, PCI_CLASS_DEVICE), 0),
234 pci_lookup_name(pacc, devbuf, sizeof(devbuf),
235 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
236 p->vendor_id, p->device_id));
237 if (c = get_conf_byte(d, PCI_REVISION_ID))
238 printf(" (rev %02x)", c);
239 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
240 printf(" (prog-if %02x)", c);
245 show_bases(struct device *d, int cnt)
247 struct pci_dev *p = d->dev;
248 word cmd = get_conf_word(d, PCI_COMMAND);
253 pciaddr_t pos = p->base_addr[i];
254 u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
255 if (flg == 0xffffffff)
260 printf("\tRegion %d: ", i);
263 if (pos && !flg) /* Reported by the OS, but not by the device */
265 printf("[virtual] ");
268 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
270 pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
271 printf("I/O ports at ");
273 printf(IO_FORMAT, a);
274 else if (flg & PCI_BASE_ADDRESS_IO_MASK)
277 printf("<unassigned>");
278 if (!(cmd & PCI_COMMAND_IO))
279 printf(" [disabled]");
283 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
284 pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
288 printf("Memory at ");
289 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
293 printf("<invalid-64bit-slot>\n");
299 z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
303 printf("%08x" ADDR_FORMAT, z, a);
305 printf("<unassigned>");
313 printf(ADDR_FORMAT, a);
315 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
317 printf(" (%s, %sprefetchable)",
318 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
319 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
320 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
321 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
322 if (!(cmd & PCI_COMMAND_MEMORY))
323 printf(" [disabled]");
330 show_pm(struct device *d, int where, int cap)
334 printf("Power Management version %d\n", cap & PCI_PM_CAP_VER_MASK);
337 printf("\t\tFlags: PMEClk%c AuxPwr%c DSI%c D1%c D2%c PME%c\n",
338 FLAG(cap, PCI_PM_CAP_PME_CLOCK),
339 FLAG(cap, PCI_PM_CAP_AUX_POWER),
340 FLAG(cap, PCI_PM_CAP_DSI),
341 FLAG(cap, PCI_PM_CAP_D1),
342 FLAG(cap, PCI_PM_CAP_D2),
343 FLAG(cap, PCI_PM_CAP_PME));
344 config_fetch(d, where + PCI_PM_CTRL, PCI_PM_SIZEOF - PCI_PM_CTRL);
345 t = get_conf_word(d, where + PCI_PM_CTRL);
346 printf("\t\tStatus: D%d PME-Enable%c DSel=%x DScale=%x PME%c\n",
347 t & PCI_PM_CTRL_STATE_MASK,
348 FLAG(t, PCI_PM_CTRL_PME_ENABLE),
349 (t & PCI_PM_CTRL_DATA_SEL_MASK) >> 9,
350 (t & PCI_PM_CTRL_DATA_SCALE_MASK) >> 13,
351 FLAG(t, PCI_PM_CTRL_PME_STATUS));
355 show_agp(struct device *d, int where, int cap)
360 printf("AGP version %x.%x\n", cap/16, cap%16);
363 config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS);
364 t = get_conf_long(d, where + PCI_AGP_STATUS);
365 printf("\t\tStatus: RQ=%d SBA%c 64bit%c FW%c Rate=%s%s%s\n",
366 (t & PCI_AGP_STATUS_RQ_MASK) >> 24U,
367 FLAG(t, PCI_AGP_STATUS_SBA),
368 FLAG(t, PCI_AGP_STATUS_64BIT),
369 FLAG(t, PCI_AGP_STATUS_FW),
370 (t & PCI_AGP_STATUS_RATE4) ? "4" : "",
371 (t & PCI_AGP_STATUS_RATE2) ? "2" : "",
372 (t & PCI_AGP_STATUS_RATE1) ? "1" : "");
373 printf("\t\tCommand: RQ=%d SBA%c AGP%c 64bit%c FW%c Rate=%s%s%s\n",
374 (t & PCI_AGP_COMMAND_RQ_MASK) >> 24U,
375 FLAG(t, PCI_AGP_COMMAND_SBA),
376 FLAG(t, PCI_AGP_COMMAND_AGP),
377 FLAG(t, PCI_AGP_COMMAND_64BIT),
378 FLAG(t, PCI_AGP_COMMAND_FW),
379 (t & PCI_AGP_COMMAND_RATE4) ? "4" : "",
380 (t & PCI_AGP_COMMAND_RATE2) ? "2" : "",
381 (t & PCI_AGP_COMMAND_RATE1) ? "1" : "");
385 show_htype0(struct device *d)
387 unsigned long rom = d->dev->rom_base_addr;
391 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
392 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
393 if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
395 int where = get_conf_byte(d, PCI_CAPABILITY_LIST);
399 printf("\tCapabilities: ");
400 if (!config_fetch(d, where, 4))
402 puts("<available only to root>");
405 id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
406 next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT);
407 cap = get_conf_word(d, where + PCI_CAP_FLAGS);
408 printf("[%02x] ", where);
411 printf("<chain broken>\n");
417 show_pm(d, where, cap);
420 show_agp(d, where, cap);
423 printf("#%02x [%04x]", id, cap);
431 show_htype1(struct device *d)
433 struct pci_dev *p = d->dev;
434 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
435 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
436 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
437 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
438 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
439 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
440 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
441 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
442 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
443 unsigned long rom = p->rom_base_addr;
444 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
447 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
448 get_conf_byte(d, PCI_PRIMARY_BUS),
449 get_conf_byte(d, PCI_SECONDARY_BUS),
450 get_conf_byte(d, PCI_SUBORDINATE_BUS),
451 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
453 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
454 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
455 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
458 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
459 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
460 if (io_type == PCI_IO_RANGE_TYPE_32)
462 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
463 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
466 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
469 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
471 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
474 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
475 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
476 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
479 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
480 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
481 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
484 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
485 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
486 if (pref_type == PCI_PREF_RANGE_TYPE_32)
487 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
489 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
490 get_conf_long(d, PCI_PREF_BASE_UPPER32),
492 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
496 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
497 printf("\tSecondary status: SERR\n");
500 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
501 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
504 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
505 FLAG(brc, PCI_BRIDGE_CTL_PARITY),
506 FLAG(brc, PCI_BRIDGE_CTL_SERR),
507 FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
508 FLAG(brc, PCI_BRIDGE_CTL_VGA),
509 FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
510 FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
511 FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
515 show_htype2(struct device *d)
518 word cmd = get_conf_word(d, PCI_COMMAND);
519 word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
520 word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
523 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
524 get_conf_byte(d, PCI_CB_PRIMARY_BUS),
525 get_conf_byte(d, PCI_CB_CARD_BUS),
526 get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
527 get_conf_byte(d, PCI_CB_LATENCY_TIMER));
531 u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
532 u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
534 printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
535 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
536 (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
541 u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
542 u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
543 if (!(base & PCI_IO_RANGE_TYPE_32))
548 base &= PCI_CB_IO_RANGE_MASK;
551 limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
552 printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
553 (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
556 if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
557 printf("\tSecondary status: SERR\n");
559 printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
560 FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
561 FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
562 FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
563 FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
564 FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
565 FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
566 FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
567 FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
569 printf("\t16-bit legacy interface ports at %04x\n", exca);
573 show_verbose(struct device *d)
575 struct pci_dev *p = d->dev;
576 word status = get_conf_word(d, PCI_STATUS);
577 word cmd = get_conf_word(d, PCI_COMMAND);
578 word class = get_conf_word(d, PCI_CLASS_DEVICE);
579 byte bist = get_conf_byte(d, PCI_BIST);
580 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
581 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
582 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
583 byte max_lat, min_gnt;
584 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
585 unsigned int irq = p->irq;
586 word subsys_v, subsys_d;
593 case PCI_HEADER_TYPE_NORMAL:
594 if (class == PCI_CLASS_BRIDGE_PCI)
597 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
600 max_lat = get_conf_byte(d, PCI_MAX_LAT);
601 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
602 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
603 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
605 case PCI_HEADER_TYPE_BRIDGE:
606 if (class != PCI_CLASS_BRIDGE_PCI)
608 irq = int_pin = min_gnt = max_lat = 0;
609 subsys_v = subsys_d = 0;
611 case PCI_HEADER_TYPE_CARDBUS:
612 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
614 min_gnt = max_lat = 0;
615 subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
616 subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
619 printf("\t!!! Unknown header type %02x\n", htype);
623 if (verbose && subsys_v && subsys_v != 0xffff)
624 printf("\tSubsystem: %s\n",
625 pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
626 PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
627 subsys_v, subsys_d));
631 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
632 FLAG(cmd, PCI_COMMAND_IO),
633 FLAG(cmd, PCI_COMMAND_MEMORY),
634 FLAG(cmd, PCI_COMMAND_MASTER),
635 FLAG(cmd, PCI_COMMAND_SPECIAL),
636 FLAG(cmd, PCI_COMMAND_INVALIDATE),
637 FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
638 FLAG(cmd, PCI_COMMAND_PARITY),
639 FLAG(cmd, PCI_COMMAND_WAIT),
640 FLAG(cmd, PCI_COMMAND_SERR),
641 FLAG(cmd, PCI_COMMAND_FAST_BACK));
642 printf("\tStatus: Cap%c 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
643 FLAG(status, PCI_STATUS_CAP_LIST),
644 FLAG(status, PCI_STATUS_66MHZ),
645 FLAG(status, PCI_STATUS_UDF),
646 FLAG(status, PCI_STATUS_FAST_BACK),
647 FLAG(status, PCI_STATUS_PARITY),
648 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
649 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
650 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
651 FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
652 FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
653 FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
654 FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
655 FLAG(status, PCI_STATUS_DETECTED_PARITY));
656 if (cmd & PCI_COMMAND_MASTER)
658 printf("\tLatency: ");
660 printf("%d min, ", min_gnt);
662 printf("%d max, ", max_lat);
663 printf("%d set", latency);
665 printf(", cache line size %02x", cache_line);
669 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
670 (int_pin ? 'A' + int_pin - 1 : '?'), irq);
675 if (cmd & PCI_COMMAND_MASTER)
676 printf("bus master, ");
677 if (cmd & PCI_COMMAND_VGA_PALETTE)
678 printf("VGA palette snoop, ");
679 if (cmd & PCI_COMMAND_WAIT)
680 printf("stepping, ");
681 if (cmd & PCI_COMMAND_FAST_BACK)
682 printf("fast Back2Back, ");
683 if (status & PCI_STATUS_66MHZ)
685 if (status & PCI_STATUS_UDF)
686 printf("user-definable features, ");
688 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
689 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
690 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
691 if (cmd & PCI_COMMAND_MASTER)
692 printf(", latency %d", latency);
694 printf(", IRQ " IRQ_FORMAT, irq);
698 if (bist & PCI_BIST_CAPABLE)
700 if (bist & PCI_BIST_START)
701 printf("\tBIST is running\n");
703 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
708 case PCI_HEADER_TYPE_NORMAL:
711 case PCI_HEADER_TYPE_BRIDGE:
714 case PCI_HEADER_TYPE_CARDBUS:
721 show_hex_dump(struct device *d)
725 for(i=0; i<d->config_cnt; i++)
729 printf(" %02x", get_conf_byte(d, i));
736 show_machine(struct device *d)
738 struct pci_dev *p = d->dev;
740 word sv_id=0, sd_id=0;
741 char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
743 switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
745 case PCI_HEADER_TYPE_NORMAL:
746 sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
747 sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
749 case PCI_HEADER_TYPE_CARDBUS:
750 sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
751 sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
757 printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
758 printf("Class:\t%s\n",
759 pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0));
760 printf("Vendor:\t%s\n",
761 pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
762 printf("Device:\t%s\n",
763 pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
764 if (sv_id && sv_id != 0xffff)
766 printf("SVendor:\t%s\n",
767 pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id));
768 printf("SDevice:\t%s\n",
769 pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
771 if (c = get_conf_byte(d, PCI_REVISION_ID))
772 printf("Rev:\t%02x\n", c);
773 if (c = get_conf_byte(d, PCI_CLASS_PROG))
774 printf("ProgIf:\t%02x\n", c);
778 printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
779 printf("\"%s\" \"%s\" \"%s\"",
780 pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
781 get_conf_word(d, PCI_CLASS_DEVICE), 0),
782 pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
783 p->vendor_id, p->device_id),
784 pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
785 p->vendor_id, p->device_id));
786 if (c = get_conf_byte(d, PCI_REVISION_ID))
787 printf(" -r%02x", c);
788 if (c = get_conf_byte(d, PCI_CLASS_PROG))
789 printf(" -p%02x", c);
790 if (sv_id && sv_id != 0xffff)
791 printf(" \"%s\" \"%s\"",
792 pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id),
793 pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
795 printf(" \"\" \"\"");
801 show_device(struct device *d)
803 if (machine_readable)
811 if (verbose || show_hex)
820 for(d=first_dev; d; d=d->next)
827 struct bridge *chain; /* Single-linked list of bridges */
828 struct bridge *next, *child; /* Tree of bridges */
829 struct bus *first_bus; /* List of busses connected to this bridge */
830 unsigned int primary, secondary, subordinate; /* Bus numbers */
831 struct device *br_dev;
837 struct device *first_dev, **last_dev;
840 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
843 find_bus(struct bridge *b, unsigned int n)
847 for(bus=b->first_bus; bus; bus=bus->sibling)
848 if (bus->number == n)
854 new_bus(struct bridge *b, unsigned int n)
856 struct bus *bus = xmalloc(sizeof(struct bus));
858 bus = xmalloc(sizeof(struct bus));
860 bus->sibling = b->first_bus;
861 bus->first_dev = NULL;
862 bus->last_dev = &bus->first_dev;
868 insert_dev(struct device *d, struct bridge *b)
870 struct pci_dev *p = d->dev;
873 if (! (bus = find_bus(b, p->bus)))
876 for(c=b->child; c; c=c->next)
877 if (c->secondary <= p->bus && p->bus <= c->subordinate)
878 return insert_dev(d, c);
879 bus = new_bus(b, p->bus);
881 /* Simple insertion at the end _does_ guarantee the correct order as the
882 * original device list was sorted by (bus, devfn) lexicographically
883 * and all devices on the new list have the same bus number.
886 bus->last_dev = &d->next;
893 struct device *d, *d2;
894 struct bridge **last_br, *b;
896 /* Build list of bridges */
898 last_br = &host_bridge.chain;
899 for(d=first_dev; d; d=d->next)
901 word class = get_conf_word(d, PCI_CLASS_DEVICE);
902 byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
903 if (class == PCI_CLASS_BRIDGE_PCI &&
904 (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
906 b = xmalloc(sizeof(struct bridge));
907 if (ht == PCI_HEADER_TYPE_BRIDGE)
909 b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
910 b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
911 b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
915 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
916 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
917 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
921 b->next = b->child = NULL;
928 /* Create a bridge tree */
930 for(b=&host_bridge; b; b=b->chain)
932 struct bridge *c, *best;
934 for(c=&host_bridge; c; c=c->chain)
935 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
936 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
940 b->next = best->child;
945 /* Insert secondary bus for each bridge */
947 for(b=&host_bridge; b; b=b->chain)
948 if (!find_bus(b, b->secondary))
949 new_bus(b, b->secondary);
951 /* Create bus structs and link devices */
956 insert_dev(d, &host_bridge);
962 print_it(byte *line, byte *p)
968 if (*p == '+' || *p == '|')
974 static void show_tree_bridge(struct bridge *, byte *, byte *);
977 show_tree_dev(struct device *d, byte *line, byte *p)
979 struct pci_dev *q = d->dev;
983 p += sprintf(p, "%02x.%x", q->dev, q->func);
984 for(b=&host_bridge; b; b=b->chain)
987 if (b->secondary == b->subordinate)
988 p += sprintf(p, "-[%02x]-", b->secondary);
990 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
991 show_tree_bridge(b, line, p);
995 p += sprintf(p, " %s",
996 pci_lookup_name(pacc, namebuf, sizeof(namebuf),
997 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
998 q->vendor_id, q->device_id));
1003 show_tree_bus(struct bus *b, byte *line, byte *p)
1007 else if (!b->first_dev->next)
1011 show_tree_dev(b->first_dev, line, p);
1015 struct device *d = b->first_dev;
1020 show_tree_dev(d, line, p+2);
1025 show_tree_dev(d, line, p+2);
1030 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1033 if (!b->first_bus->sibling)
1035 if (b == &host_bridge)
1036 p += sprintf(p, "[%02x]-", b->first_bus->number);
1037 show_tree_bus(b->first_bus, line, p);
1041 struct bus *u = b->first_bus;
1046 k = p + sprintf(p, "+-[%02x]-", u->number);
1047 show_tree_bus(u, line, k);
1050 k = p + sprintf(p, "\\-[%02x]-", u->number);
1051 show_tree_bus(u, line, k);
1061 show_tree_bridge(&host_bridge, line, line);
1064 /* Bus mapping mode */
1067 struct bus_bridge *next;
1068 byte this, dev, func, first, last, bug;
1074 struct bus_bridge *bridges, *via;
1077 static struct bus_info *bus_info;
1080 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1082 struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1083 struct pci_dev *p = d->dev;
1085 b->next = bi->bridges;
1087 b->this = get_conf_byte(d, np);
1090 b->first = get_conf_byte(d, ns);
1091 b->last = get_conf_byte(d, nl);
1092 printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1093 p->bus, p->dev, p->func, b->this, b->first, b->last);
1094 if (b->this != p->bus)
1095 printf("!!! Bridge points to invalid primary bus.\n");
1096 if (b->first > b->last)
1098 printf("!!! Bridge points to invalid bus range.\n");
1107 int verbose = pacc->debugging;
1108 struct bus_info *bi = bus_info + bus;
1112 printf("Mapping bus %02x\n", bus);
1113 for(dev = 0; dev < 32; dev++)
1114 if (filter.slot < 0 || filter.slot == dev)
1117 for(func = 0; func < func_limit; func++)
1118 if (filter.func < 0 || filter.func == func)
1120 struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1121 u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1122 if (vendor && vendor != 0xffff)
1124 if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1127 printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1129 if (d = scan_device(p))
1132 switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1134 case PCI_HEADER_TYPE_BRIDGE:
1135 map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1137 case PCI_HEADER_TYPE_CARDBUS:
1138 map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1144 printf("But it was filtered out.\n");
1152 do_map_bridges(int bus, int min, int max)
1154 struct bus_info *bi = bus_info + bus;
1155 struct bus_bridge *b;
1158 for(b=bi->bridges; b; b=b->next)
1160 if (bus_info[b->first].guestbook)
1162 else if (b->first < min || b->last > max)
1166 bus_info[b->first].via = b;
1167 do_map_bridges(b->first, b->first, b->last);
1177 printf("\nSummary of buses:\n\n");
1178 for(i=0; i<256; i++)
1179 if (bus_info[i].exists && !bus_info[i].guestbook)
1180 do_map_bridges(i, 0, 255);
1181 for(i=0; i<256; i++)
1183 struct bus_info *bi = bus_info + i;
1184 struct bus_bridge *b = bi->via;
1188 printf("%02x: ", i);
1190 printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1192 printf("Primary host bus\n");
1194 printf("Secondary host bus (?)\n");
1196 for(b=bi->bridges; b; b=b->next)
1198 printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1202 printf(" <overlap bug>");
1205 printf(" <crossing bug>");
1216 if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1217 pacc->method == PCI_ACCESS_DUMP)
1218 printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1219 else if (!check_root())
1220 die("Only root can map the bus.");
1221 bus_info = xmalloc(sizeof(struct bus_info) * 256);
1222 bzero(bus_info, sizeof(struct bus_info) * 256);
1223 if (filter.bus >= 0)
1224 do_map_bus(filter.bus);
1228 for(bus=0; bus<256; bus++)
1237 main(int argc, char **argv)
1242 if (argc == 2 && !strcmp(argv[1], "--version"))
1244 puts("lspci version " PCIUTILS_VERSION);
1250 pci_filter_init(pacc, &filter);
1252 while ((i = getopt(argc, argv, options)) != -1)
1256 pacc->numeric_ids = 1;
1262 pacc->buscentric = 1;
1263 buscentric_view = 1;
1266 if (msg = pci_filter_parse_slot(&filter, optarg))
1270 if (msg = pci_filter_parse_id(&filter, optarg))
1280 pacc->id_file_name = optarg;
1289 if (parse_generic_option(i, pacc, optarg))
1292 fprintf(stderr, help_msg, pacc->id_file_name);