2 * $Id: lspci.c,v 1.10 1998/03/31 21:02:16 mj Exp $
4 * Linux PCI Utilities -- List All PCI Devices
6 * Copyright (c) 1997, 1998 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 char *pci_dir = PROC_BUS_PCI;
29 static char options[] = "nvbxs:d:ti:p:m";
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 (-xx shows full 256 bytes)\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 " ETC_PCI_IDS "\n\
43 -p <dir>\tUse specified bus directory instead of " PROC_BUS_PCI "\n\
46 /* Format strings used for IRQ numbers */
49 #define IRQ_FORMAT "%08x"
51 #define IRQ_FORMAT "%d"
54 /* Our view of the PCI bus */
60 unsigned int kernel_irq;
61 unsigned long kernel_base_addr[6];
65 static struct device *first_dev, **last_dev = &first_dev;
67 /* Miscellaneous routines */
70 xmalloc(unsigned int howmuch)
72 void *p = malloc(howmuch);
75 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
81 /* Interface for /proc/bus/pci */
90 sprintf(name, "%s/devices", pci_dir);
91 if (! (f = fopen(name, "r")))
96 while (fgets(line, sizeof(line), f))
98 struct device *d = xmalloc(sizeof(struct device));
99 unsigned int dfn, vend;
101 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
105 &d->kernel_base_addr[0],
106 &d->kernel_base_addr[1],
107 &d->kernel_base_addr[2],
108 &d->kernel_base_addr[3],
109 &d->kernel_base_addr[4],
110 &d->kernel_base_addr[5]);
112 d->devfn = dfn & 0xff;
113 d->vendid = vend >> 16U;
114 d->devid = vend & 0xffff;
115 if (filter_match(&filter, d->bus, d->devfn, d->vendid, d->devid))
126 make_proc_pci_name(struct device *d, char *p)
128 sprintf(p, "%s/%02x/%02x.%x",
129 pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
138 int how_much = (show_hex > 1) ? 256 : 64;
140 for(d=first_dev; d; d=d->next)
142 make_proc_pci_name(d, name);
143 if ((fd = open(name, O_RDONLY)) < 0)
145 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
148 res = read(fd, d->config, how_much);
151 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
156 fprintf(stderr, "lspci: Only %d bytes of config space available to you\n", res);
170 /* Config space accesses */
173 get_conf_byte(struct device *d, unsigned int pos)
175 return d->config[pos];
179 get_conf_word(struct device *d, unsigned int pos)
181 return d->config[pos] | (d->config[pos+1] << 8);
185 get_conf_long(struct device *d, unsigned int pos)
187 return d->config[pos] |
188 (d->config[pos+1] << 8) |
189 (d->config[pos+2] << 16) |
190 (d->config[pos+3] << 24);
196 compare_them(const void *A, const void *B)
198 const struct device *a = *(const struct device **)A;
199 const struct device *b = *(const struct device **)B;
205 if (a->devfn < b->devfn)
207 if (a->devfn > b->devfn)
215 struct device **index, **h;
220 for(d=first_dev; d; d=d->next)
222 h = index = alloca(sizeof(struct device *) * cnt);
223 for(d=first_dev; d; d=d->next)
225 qsort(index, cnt, sizeof(struct device *), compare_them);
226 last_dev = &first_dev;
231 last_dev = &(*h)->next;
240 show_terse(struct device *d)
244 printf("%02x:%02x.%x %s: %s",
248 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
249 lookup_device_full(d->vendid, d->devid));
250 if (c = get_conf_byte(d, PCI_REVISION_ID))
251 printf(" (rev %02x)", c);
252 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
253 printf(" (prog-if %02x)", c);
258 show_bases(struct device *d, int cnt)
260 word cmd = get_conf_word(d, PCI_COMMAND);
266 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
270 pos = d->kernel_base_addr[i];
271 if (!pos || pos == 0xffffffff)
273 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
275 if (cmd & PCI_COMMAND_IO)
278 printf("\tRegion %d: ", i);
281 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
284 else if (cmd & PCI_COMMAND_MEMORY)
286 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
288 printf("\tRegion %d: ", i);
291 printf("Memory at ");
292 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
297 if (!buscentric_view)
298 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
303 printf("%08lx (%s, %sprefetchable)\n",
304 pos & PCI_BASE_ADDRESS_MEM_MASK,
305 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
306 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
307 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
308 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
314 show_htype0(struct device *d)
316 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
322 word cmd = get_conf_word(d, PCI_COMMAND);
323 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
324 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
329 show_htype1(struct device *d)
331 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
332 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
333 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
334 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
335 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
336 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
337 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
338 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
339 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
340 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
341 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
344 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
345 get_conf_byte(d, PCI_PRIMARY_BUS),
346 get_conf_byte(d, PCI_SECONDARY_BUS),
347 get_conf_byte(d, PCI_SUBORDINATE_BUS),
348 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
350 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
351 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
352 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
355 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
356 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
357 if (io_type == PCI_IO_RANGE_TYPE_32)
359 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
360 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
363 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
366 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
368 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
371 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
372 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
373 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
376 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
377 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
378 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
381 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
382 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
383 if (pref_type == PCI_PREF_RANGE_TYPE_32)
384 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
386 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
387 get_conf_long(d, PCI_PREF_BASE_UPPER32),
389 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
393 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
394 printf("\tSecondary status: SERR\n");
398 word cmd = get_conf_word(d, PCI_COMMAND);
399 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
400 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
404 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
405 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
406 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
407 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
408 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
409 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
410 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
411 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
415 show_htype2(struct device *d)
420 show_verbose(struct device *d)
422 word status = get_conf_word(d, PCI_STATUS);
423 word cmd = get_conf_word(d, PCI_COMMAND);
424 word class = get_conf_word(d, PCI_CLASS_DEVICE);
425 byte bist = get_conf_byte(d, PCI_BIST);
426 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
427 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
428 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
429 byte max_lat, min_gnt;
430 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
431 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
433 word subsys_v, subsys_d;
439 case PCI_HEADER_TYPE_NORMAL:
440 if (class == PCI_CLASS_BRIDGE_PCI)
443 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
446 max_lat = get_conf_byte(d, PCI_MAX_LAT);
447 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
448 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
449 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
451 case PCI_HEADER_TYPE_BRIDGE:
452 if (class != PCI_CLASS_BRIDGE_PCI)
454 irq = int_line = int_pin = min_gnt = max_lat = 0;
455 subsys_v = subsys_d = 0;
457 case PCI_HEADER_TYPE_CARDBUS:
458 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
460 irq = int_line = int_pin = min_gnt = max_lat = 0;
461 subsys_v = subsys_d = 0;
464 printf("\t!!! Unknown header type %02x\n", htype);
476 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
477 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
478 (cmd & PCI_COMMAND_IO) ? '+' : '-',
479 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
480 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
481 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
482 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
483 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
484 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
485 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
486 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
487 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
488 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
489 (status & PCI_STATUS_66MHZ) ? '+' : '-',
490 (status & PCI_STATUS_UDF) ? '+' : '-',
491 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
492 (status & PCI_STATUS_PARITY) ? '+' : '-',
493 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
494 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
495 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
496 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
497 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
498 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
499 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
500 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
501 if (cmd & PCI_COMMAND_MASTER)
503 printf("\tLatency: ");
505 printf("%d min, ", min_gnt);
507 printf("%d max, ", max_lat);
508 printf("%d set", latency);
510 printf(", cache line size %02x", cache_line);
514 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
519 if (cmd & PCI_COMMAND_MASTER)
520 printf("bus master, ");
521 if (cmd & PCI_COMMAND_VGA_PALETTE)
522 printf("VGA palette snoop, ");
523 if (cmd & PCI_COMMAND_WAIT)
524 printf("stepping, ");
525 if (cmd & PCI_COMMAND_FAST_BACK)
526 printf("fast Back2Back, ");
527 if (status & PCI_STATUS_66MHZ)
529 if (status & PCI_STATUS_UDF)
530 printf("user-definable features, ");
532 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
533 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
534 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
535 if (cmd & PCI_COMMAND_MASTER)
536 printf(", latency %d", latency);
539 printf(", IRQ " IRQ_FORMAT, irq);
545 if (bist & PCI_BIST_CAPABLE)
547 if (bist & PCI_BIST_START)
548 printf("\tBIST is running\n");
550 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
555 case PCI_HEADER_TYPE_NORMAL:
558 case PCI_HEADER_TYPE_BRIDGE:
561 case PCI_HEADER_TYPE_CARDBUS:
568 show_hex_dump(struct device *d)
571 int limit = (show_hex > 1) ? 256 : 64;
573 for(i=0; i<limit; i++)
577 printf(" %02x", get_conf_byte(d, i));
584 show_machine(struct device *d)
590 printf("Device:\t%02x:%02x.%x\n", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
591 printf("Class:\t%s\n", lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)));
592 printf("Vendor:\t%s\n", lookup_vendor(d->vendid));
593 printf("Device:\t%s\n", lookup_device(d->vendid, d->devid));
594 if (c = get_conf_byte(d, PCI_REVISION_ID))
595 printf("Rev:\t%02x\n", c);
596 if (c = get_conf_byte(d, PCI_CLASS_PROG))
597 printf("ProgIf:\t%02x\n", c);
601 printf("%02x:%02x.%x ", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
602 printf("\"%s\" \"%s\" \"%s\"",
603 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
604 lookup_vendor(d->vendid),
605 lookup_device(d->vendid, d->devid));
606 if (c = get_conf_byte(d, PCI_REVISION_ID))
607 printf(" -r%02x", c);
608 if (c = get_conf_byte(d, PCI_CLASS_PROG))
609 printf(" -p%02x", c);
619 for(d=first_dev; d; d=d->next)
621 if (machine_readable)
629 if (verbose || show_hex)
637 struct bridge *chain; /* Single-linked list of bridges */
638 struct bridge *next, *child; /* Tree of bridges */
639 struct bus *first_bus; /* List of busses connected to this bridge */
640 unsigned int primary, secondary, subordinate; /* Bus numbers */
641 struct device *br_dev;
647 struct device *first_dev, **last_dev;
650 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
653 find_bus(struct bridge *b, unsigned int n)
657 for(bus=b->first_bus; bus; bus=bus->sibling)
658 if (bus->number == n)
664 new_bus(struct bridge *b, unsigned int n)
666 struct bus *bus = xmalloc(sizeof(struct bus));
668 bus = xmalloc(sizeof(struct bus));
670 bus->sibling = b->first_bus;
671 bus->first_dev = NULL;
672 bus->last_dev = &bus->first_dev;
678 insert_dev(struct device *d, struct bridge *b)
682 if (! (bus = find_bus(b, d->bus)))
685 for(c=b->child; c; c=c->next)
686 if (c->secondary <= d->bus && d->bus <= c->subordinate)
687 return insert_dev(d, c);
688 bus = new_bus(b, d->bus);
690 /* Simple insertion at the end _does_ guarantee the correct order as the
691 * original device list was sorted by (bus, devfn) lexicographically
692 * and all devices on the new list have the same bus number.
695 bus->last_dev = &d->next;
702 struct device *d, *d2;
703 struct bridge **last_br, *b;
705 /* Build list of bridges */
707 last_br = &host_bridge.chain;
708 for(d=first_dev; d; d=d->next)
710 word class = get_conf_word(d, PCI_CLASS_DEVICE);
711 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
713 b = xmalloc(sizeof(struct bridge));
714 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
715 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
716 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
719 b->next = b->child = NULL;
726 /* Create a bridge tree */
728 for(b=&host_bridge; b; b=b->chain)
730 struct bridge *c, *best;
732 for(c=&host_bridge; c; c=c->chain)
733 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
734 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
738 b->next = best->child;
743 /* Insert secondary bus for each bridge */
745 for(b=&host_bridge; b; b=b->chain)
746 if (!find_bus(b, b->secondary))
747 new_bus(b, b->secondary);
749 /* Create bus structs and link devices */
754 insert_dev(d, &host_bridge);
760 print_it(byte *line, byte *p)
766 if (*p == '+' || *p == '|')
772 static void show_tree_bridge(struct bridge *, byte *, byte *);
775 show_tree_dev(struct device *d, byte *line, byte *p)
779 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
780 for(b=&host_bridge; b; b=b->chain)
783 if (b->secondary == b->subordinate)
784 p += sprintf(p, "-[%02x]-", b->secondary);
786 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
787 show_tree_bridge(b, line, p);
791 p += sprintf(p, " %s", lookup_device_full(d->vendid, d->devid));
796 show_tree_bus(struct bus *b, byte *line, byte *p)
800 else if (!b->first_dev->next)
804 show_tree_dev(b->first_dev, line, p);
808 struct device *d = b->first_dev;
813 show_tree_dev(d, line, p+2);
818 show_tree_dev(d, line, p+2);
823 show_tree_bridge(struct bridge *b, byte *line, byte *p)
826 if (!b->first_bus->sibling)
828 if (b == &host_bridge)
829 p += sprintf(p, "[%02x]-", b->first_bus->number);
830 show_tree_bus(b->first_bus, line, p);
834 struct bus *u = b->first_bus;
839 k = p + sprintf(p, "+-[%02x]-", u->number);
840 show_tree_bus(u, line, k);
843 k = p + sprintf(p, "\\-[%02x]-", u->number);
844 show_tree_bus(u, line, k);
854 show_tree_bridge(&host_bridge, line, line);
860 main(int argc, char **argv)
865 filter_init(&filter);
866 while ((i = getopt(argc, argv, options)) != -1)
870 show_numeric_ids = 1;
879 if (msg = filter_parse_slot(&filter, optarg))
881 fprintf(stderr, "lspci: -f: %s\n", msg);
886 if (msg = filter_parse_id(&filter, optarg))
888 fprintf(stderr, "lspci: -d: %s\n", msg);
909 fprintf(stderr, help_msg);