2 * $Id: lspci.c,v 1.3 1997/12/27 11:57:12 mj Exp $
4 * Linux PCI Utilities -- List All PCI Devices
6 * Copyright (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
16 #include <linux/pci.h>
22 static int verbose; /* Show detailed information */
23 static int buscentric_view; /* Show bus addresses/IRQ's instead of CPU-visible ones */
24 static int show_hex; /* Show contents of config space as hexadecimal numbers */
25 static int bus_filter = -1; /* Bus, slot, function, vendor and device ID filtering */
26 static int slot_filter = -1;
27 static int func_filter = -1;
28 static int vend_filter = -1;
29 static int dev_filter = -1;
30 static int show_tree; /* Show bus tree */
32 static char options[] = "nvbxB:S:F:V:D:t";
34 static char help_msg[] = "\
35 Usage: lspci [<switches>]\n\
38 -n\tShow numeric ID's\n\
39 -b\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
40 -x\tShow hex-dump of config space (-xx shows full 256 bytes)\n\
41 -B <bus>, -S <slot>, -F <func>, -V <vendor>, -D <device> Show only selected devices\n\
45 /* Format strings used for IRQ numbers */
48 #define IRQ_FORMAT "%08x"
50 #define IRQ_FORMAT "%d"
53 /* Our view of the PCI bus */
59 unsigned int kernel_irq;
60 unsigned long kernel_base_addr[6];
64 static struct device *first_dev, **last_dev = &first_dev;
66 /* Miscellaneous routines */
69 xmalloc(unsigned int howmuch)
71 void *p = malloc(howmuch);
74 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
83 filter_out(struct device *d)
85 return (bus_filter >= 0 && d->bus != bus_filter ||
86 slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
87 func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
88 vend_filter >= 0 && d->vendid != vend_filter ||
89 dev_filter >= 0 && d->devid != dev_filter);
92 /* Interface for /proc/bus/pci */
100 if (! (f = fopen(PROC_BUS_PCI "/devices", "r")))
102 perror("Unable to open " PROC_BUS_PCI "/devices");
105 while (fgets(line, sizeof(line), f))
107 struct device *d = xmalloc(sizeof(struct device));
108 unsigned int dfn, vend;
110 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
114 &d->kernel_base_addr[0],
115 &d->kernel_base_addr[1],
116 &d->kernel_base_addr[2],
117 &d->kernel_base_addr[3],
118 &d->kernel_base_addr[4],
119 &d->kernel_base_addr[5]);
121 d->devfn = dfn & 0xff;
122 d->vendid = vend >> 16U;
123 d->devid = vend & 0xffff;
135 make_proc_pci_name(struct device *d, char *p)
137 sprintf(p, PROC_BUS_PCI "/%02x/%02x.%x",
138 d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
147 int how_much = (show_hex > 1) ? 256 : 64;
149 for(d=first_dev; d; d=d->next)
151 make_proc_pci_name(d, name);
152 if ((fd = open(name, O_RDONLY)) < 0)
154 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
157 if (read(fd, d->config, how_much) != how_much)
159 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
173 /* Config space accesses */
176 get_conf_byte(struct device *d, unsigned int pos)
178 return d->config[pos];
182 get_conf_word(struct device *d, unsigned int pos)
184 return d->config[pos] | (d->config[pos+1] << 8);
188 get_conf_long(struct device *d, unsigned int pos)
190 return d->config[pos] |
191 (d->config[pos+1] << 8) |
192 (d->config[pos+2] << 16) |
193 (d->config[pos+3] << 24);
199 compare_them(const void *A, const void *B)
201 const struct device *a = *(const struct device **)A;
202 const struct device *b = *(const struct device **)B;
208 if (a->devfn < b->devfn)
210 if (a->devfn > b->devfn)
218 struct device **index, **h;
223 for(d=first_dev; d; d=d->next)
225 h = index = alloca(sizeof(struct device *) * cnt);
226 for(d=first_dev; d; d=d->next)
228 qsort(index, cnt, sizeof(struct device *), compare_them);
229 last_dev = &first_dev;
234 last_dev = &(*h)->next;
243 show_terse(struct device *d)
247 printf("%02x:%02x.%x %s: %s",
251 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
252 lookup_device_full(d->vendid, d->devid));
253 if (c = get_conf_byte(d, PCI_REVISION_ID))
254 printf(" (rev %02x)", c);
255 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
256 printf(" (prog-if %02x)", c);
261 show_bases(struct device *d, int cnt)
263 word cmd = get_conf_word(d, PCI_COMMAND);
269 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
273 pos = d->kernel_base_addr[i];
274 if (!pos || pos == 0xffffffff)
277 printf("\tRegion %d: ", i);
280 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
282 if (cmd & PCI_COMMAND_IO)
283 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
285 else if (cmd & PCI_COMMAND_MEMORY)
287 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
288 printf("Memory at ");
289 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
294 if (!buscentric_view)
295 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
300 printf("%08lx (%s, %sprefetchable)\n",
301 pos & PCI_BASE_ADDRESS_MEM_MASK,
302 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
303 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
304 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
305 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
311 show_htype0(struct device *d)
313 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
319 word cmd = get_conf_word(d, PCI_COMMAND);
320 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
321 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
326 show_htype1(struct device *d)
328 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
329 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
330 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
331 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
332 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
333 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
334 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
335 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
336 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
337 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
338 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
341 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
342 get_conf_byte(d, PCI_PRIMARY_BUS),
343 get_conf_byte(d, PCI_SECONDARY_BUS),
344 get_conf_byte(d, PCI_SUBORDINATE_BUS),
345 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
347 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
348 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
349 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
352 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
353 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
354 if (io_type == PCI_IO_RANGE_TYPE_32)
356 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
357 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
360 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
363 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
365 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
368 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
369 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
370 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
373 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
374 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
375 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
378 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
379 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
380 if (pref_type == PCI_PREF_RANGE_TYPE_32)
381 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
383 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
384 get_conf_long(d, PCI_PREF_BASE_UPPER32),
386 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
390 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
391 printf("\tSecondary status: SERR\n");
395 word cmd = get_conf_word(d, PCI_COMMAND);
396 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
397 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
401 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
402 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
403 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
404 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
405 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
406 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
407 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
408 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
412 show_verbose(struct device *d)
414 word status = get_conf_word(d, PCI_STATUS);
415 word cmd = get_conf_word(d, PCI_COMMAND);
416 word class = get_conf_word(d, PCI_CLASS_DEVICE);
417 byte bist = get_conf_byte(d, PCI_BIST);
418 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
419 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
420 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
421 byte max_lat, min_gnt;
422 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
423 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
424 unsigned int irq, ex_htype;
425 word subsys_v, subsys_d;
431 case PCI_CLASS_BRIDGE_PCI:
437 if (ex_htype != htype)
439 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);
452 irq = int_line = int_pin = min_gnt = max_lat = 0;
453 subsys_v = subsys_d = 0;
456 printf("\t!!! Unknown header type %02x\n", htype);
468 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
469 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
470 (cmd & PCI_COMMAND_IO) ? '+' : '-',
471 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
472 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
473 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
474 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
475 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
476 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
477 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
478 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
479 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
480 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
481 (status & PCI_STATUS_66MHZ) ? '+' : '-',
482 (status & PCI_STATUS_UDF) ? '+' : '-',
483 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
484 (status & PCI_STATUS_PARITY) ? '+' : '-',
485 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
486 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
487 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
488 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
489 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
490 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
491 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
492 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
493 if (cmd & PCI_COMMAND_MASTER)
495 printf("\tLatency: ");
497 printf("%d min, ", min_gnt);
499 printf("%d max, ", max_lat);
500 printf("%d set", latency);
502 printf(", cache line size %02x", cache_line);
506 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
511 if (cmd & PCI_COMMAND_MASTER)
512 printf("bus master, ");
513 if (cmd & PCI_COMMAND_VGA_PALETTE)
514 printf("VGA palette snoop, ");
515 if (cmd & PCI_COMMAND_WAIT)
516 printf("stepping, ");
517 if (cmd & PCI_COMMAND_FAST_BACK)
518 printf("fast Back2Back, ");
519 if (status & PCI_STATUS_66MHZ)
521 if (status & PCI_STATUS_UDF)
522 printf("user-definable features, ");
524 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
525 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
526 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
527 if (cmd & PCI_COMMAND_MASTER)
528 printf(", latency %d", latency);
531 printf(", IRQ " IRQ_FORMAT, irq);
537 if (bist & PCI_BIST_CAPABLE)
539 if (bist & PCI_BIST_START)
540 printf("\tBIST is running\n");
542 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
557 show_hex_dump(struct device *d)
560 int limit = (show_hex > 1) ? 256 : 64;
562 for(i=0; i<limit; i++)
566 printf(" %02x", get_conf_byte(d, i));
577 for(d=first_dev; d; d=d->next)
585 if (verbose || show_hex)
593 struct bridge *chain; /* Single-linked list of bridges */
594 struct bridge *next, *child; /* Tree of bridges */
595 struct bus *first_bus; /* List of busses connected to this bridge */
596 unsigned int primary, secondary, subordinate; /* Bus numbers */
597 struct device *br_dev;
603 struct device *first_dev, **last_dev;
606 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
609 find_bus(struct bridge *b, unsigned int n)
613 for(bus=b->first_bus; bus; bus=bus->sibling)
614 if (bus->number == n)
620 new_bus(struct bridge *b, unsigned int n)
622 struct bus *bus = xmalloc(sizeof(struct bus));
624 bus = xmalloc(sizeof(struct bus));
626 bus->sibling = b->first_bus;
627 bus->first_dev = NULL;
628 bus->last_dev = &bus->first_dev;
634 insert_dev(struct device *d, struct bridge *b)
638 if (! (bus = find_bus(b, d->bus)))
641 for(c=b->child; c; c=c->next)
642 if (c->secondary <= d->bus && d->bus <= c->subordinate)
643 return insert_dev(d, c);
644 bus = new_bus(b, d->bus);
646 /* Simple insertion at the end _does_ guarantee the correct order as the
647 * original device list was sorted by (bus, devfn) lexicographically
648 * and all devices on the new list have the same bus number.
651 bus->last_dev = &d->next;
658 struct device *d, *d2;
659 struct bridge *first_br, *b;
661 /* Build list of bridges */
663 first_br = &host_bridge;
664 for(d=first_dev; d; d=d->next)
666 word class = get_conf_word(d, PCI_CLASS_DEVICE);
667 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
669 b = xmalloc(sizeof(struct bridge));
670 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
671 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
672 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
675 b->next = b->child = NULL;
681 /* Create a bridge tree */
683 for(b=first_br; b; b=b->chain)
685 struct bridge *c, *best;
687 for(c=first_br; c; c=c->chain)
688 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
689 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
693 b->next = best->child;
698 /* Insert secondary bus for each bridge */
700 for(b=first_br; b; b=b->chain)
701 if (!find_bus(b, b->secondary))
702 new_bus(b, b->secondary);
704 /* Create bus structs and link devices */
709 insert_dev(d, &host_bridge);
715 print_it(byte *line, byte *p)
727 static void show_tree_bridge(struct bridge *, byte *, byte *);
730 show_tree_dev(struct device *d, byte *line, byte *p)
734 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
735 for(b=&host_bridge; b; b=b->chain)
738 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
739 show_tree_bridge(b, line, p);
746 show_tree_bus(struct bus *b, byte *line, byte *p)
750 else if (!b->first_dev->next)
754 show_tree_dev(b->first_dev, line, p);
758 struct device *d = b->first_dev;
763 show_tree_dev(d, line, p+2);
768 show_tree_dev(d, line, p+2);
773 show_tree_bridge(struct bridge *b, byte *line, byte *p)
776 if (!b->first_bus->sibling)
778 if (b == &host_bridge)
779 p += sprintf(p, "[%02x]-", b->first_bus->number);
780 show_tree_bus(b->first_bus, line, p);
784 struct bus *u = b->first_bus;
789 k = p + sprintf(p, "+-[%02x]-", u->number);
790 show_tree_bus(u, line, k);
793 k = p + sprintf(p, "\\-[%02x]-", u->number);
794 show_tree_bus(u, line, k);
804 show_tree_bridge(&host_bridge, line, line);
810 main(int argc, char **argv)
814 while ((i = getopt(argc, argv, options)) != -1)
818 show_numeric_ids = 1;
827 bus_filter = strtol(optarg, NULL, 16);
830 slot_filter = strtol(optarg, NULL, 16);
833 func_filter = strtol(optarg, NULL, 16);
836 vend_filter = strtol(optarg, NULL, 16);
839 dev_filter = strtol(optarg, NULL, 16);
849 fprintf(stderr, help_msg);