2 * $Id: lspci.c,v 1.7 1998/02/09 12:32:54 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 int bus_filter = -1; /* Bus, slot, function, vendor and device ID filtering */
25 static int slot_filter = -1;
26 static int func_filter = -1;
27 static int vend_filter = -1;
28 static int dev_filter = -1;
29 static int show_tree; /* Show bus tree */
30 static char *pci_dir = PROC_BUS_PCI;
32 static char options[] = "nvbxB:S:F:V:D:ti:p:";
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\
43 -i <file>\tUse specified ID database instead of " ETC_PCI_IDS "\n\
44 -p <dir>\tUse specified bus directory instead of " PROC_BUS_PCI "\n\
47 /* Format strings used for IRQ numbers */
50 #define IRQ_FORMAT "%08x"
52 #define IRQ_FORMAT "%d"
55 /* Our view of the PCI bus */
61 unsigned int kernel_irq;
62 unsigned long kernel_base_addr[6];
66 static struct device *first_dev, **last_dev = &first_dev;
68 /* Miscellaneous routines */
71 xmalloc(unsigned int howmuch)
73 void *p = malloc(howmuch);
76 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
85 filter_out(struct device *d)
87 return (bus_filter >= 0 && d->bus != bus_filter ||
88 slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
89 func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
90 vend_filter >= 0 && d->vendid != vend_filter ||
91 dev_filter >= 0 && d->devid != dev_filter);
94 /* Interface for /proc/bus/pci */
103 sprintf(name, "%s/devices", pci_dir);
104 if (! (f = fopen(name, "r")))
109 while (fgets(line, sizeof(line), f))
111 struct device *d = xmalloc(sizeof(struct device));
112 unsigned int dfn, vend;
114 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
118 &d->kernel_base_addr[0],
119 &d->kernel_base_addr[1],
120 &d->kernel_base_addr[2],
121 &d->kernel_base_addr[3],
122 &d->kernel_base_addr[4],
123 &d->kernel_base_addr[5]);
125 d->devfn = dfn & 0xff;
126 d->vendid = vend >> 16U;
127 d->devid = vend & 0xffff;
139 make_proc_pci_name(struct device *d, char *p)
141 sprintf(p, "%s/%02x/%02x.%x",
142 pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
151 int how_much = (show_hex > 1) ? 256 : 64;
153 for(d=first_dev; d; d=d->next)
155 make_proc_pci_name(d, name);
156 if ((fd = open(name, O_RDONLY)) < 0)
158 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
161 res = read(fd, d->config, how_much);
164 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
169 fprintf(stderr, "lspci: Only %d bytes of config space available to you\n", res);
183 /* Config space accesses */
186 get_conf_byte(struct device *d, unsigned int pos)
188 return d->config[pos];
192 get_conf_word(struct device *d, unsigned int pos)
194 return d->config[pos] | (d->config[pos+1] << 8);
198 get_conf_long(struct device *d, unsigned int pos)
200 return d->config[pos] |
201 (d->config[pos+1] << 8) |
202 (d->config[pos+2] << 16) |
203 (d->config[pos+3] << 24);
209 compare_them(const void *A, const void *B)
211 const struct device *a = *(const struct device **)A;
212 const struct device *b = *(const struct device **)B;
218 if (a->devfn < b->devfn)
220 if (a->devfn > b->devfn)
228 struct device **index, **h;
233 for(d=first_dev; d; d=d->next)
235 h = index = alloca(sizeof(struct device *) * cnt);
236 for(d=first_dev; d; d=d->next)
238 qsort(index, cnt, sizeof(struct device *), compare_them);
239 last_dev = &first_dev;
244 last_dev = &(*h)->next;
253 show_terse(struct device *d)
257 printf("%02x:%02x.%x %s: %s",
261 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
262 lookup_device_full(d->vendid, d->devid));
263 if (c = get_conf_byte(d, PCI_REVISION_ID))
264 printf(" (rev %02x)", c);
265 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
266 printf(" (prog-if %02x)", c);
271 show_bases(struct device *d, int cnt)
273 word cmd = get_conf_word(d, PCI_COMMAND);
279 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
283 pos = d->kernel_base_addr[i];
284 if (!pos || pos == 0xffffffff)
286 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
288 if (cmd & PCI_COMMAND_IO)
291 printf("\tRegion %d: ", i);
294 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
297 else if (cmd & PCI_COMMAND_MEMORY)
299 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
301 printf("\tRegion %d: ", i);
304 printf("Memory at ");
305 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
310 if (!buscentric_view)
311 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
316 printf("%08lx (%s, %sprefetchable)\n",
317 pos & PCI_BASE_ADDRESS_MEM_MASK,
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 32-bit" : "???",
321 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
327 show_htype0(struct device *d)
329 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
335 word cmd = get_conf_word(d, PCI_COMMAND);
336 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
337 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
342 show_htype1(struct device *d)
344 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
345 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
346 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
347 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
348 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
349 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
350 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
351 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
352 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
353 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
354 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
357 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
358 get_conf_byte(d, PCI_PRIMARY_BUS),
359 get_conf_byte(d, PCI_SECONDARY_BUS),
360 get_conf_byte(d, PCI_SUBORDINATE_BUS),
361 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
363 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
364 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
365 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
368 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
369 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
370 if (io_type == PCI_IO_RANGE_TYPE_32)
372 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
373 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
376 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
379 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
381 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
384 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
385 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
386 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
389 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
390 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
391 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
394 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
395 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
396 if (pref_type == PCI_PREF_RANGE_TYPE_32)
397 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
399 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
400 get_conf_long(d, PCI_PREF_BASE_UPPER32),
402 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
406 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
407 printf("\tSecondary status: SERR\n");
411 word cmd = get_conf_word(d, PCI_COMMAND);
412 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
413 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
417 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
418 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
419 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
420 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
421 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
422 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
423 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
424 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
428 show_htype2(struct device *d)
433 show_verbose(struct device *d)
435 word status = get_conf_word(d, PCI_STATUS);
436 word cmd = get_conf_word(d, PCI_COMMAND);
437 word class = get_conf_word(d, PCI_CLASS_DEVICE);
438 byte bist = get_conf_byte(d, PCI_BIST);
439 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
440 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
441 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
442 byte max_lat, min_gnt;
443 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
444 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
446 word subsys_v, subsys_d;
452 case PCI_HEADER_TYPE_NORMAL:
453 if (class == PCI_CLASS_BRIDGE_PCI)
456 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
459 max_lat = get_conf_byte(d, PCI_MAX_LAT);
460 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
461 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
462 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
464 case PCI_HEADER_TYPE_BRIDGE:
465 if (class != PCI_CLASS_BRIDGE_PCI)
467 irq = int_line = int_pin = min_gnt = max_lat = 0;
468 subsys_v = subsys_d = 0;
470 case PCI_HEADER_TYPE_CARDBUS:
471 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
473 irq = int_line = int_pin = min_gnt = max_lat = 0;
474 subsys_v = subsys_d = 0;
477 printf("\t!!! Unknown header type %02x\n", htype);
489 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
490 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
491 (cmd & PCI_COMMAND_IO) ? '+' : '-',
492 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
493 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
494 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
495 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
496 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
497 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
498 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
499 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
500 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
501 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
502 (status & PCI_STATUS_66MHZ) ? '+' : '-',
503 (status & PCI_STATUS_UDF) ? '+' : '-',
504 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
505 (status & PCI_STATUS_PARITY) ? '+' : '-',
506 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
507 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
508 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
509 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
510 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
511 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
512 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
513 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
514 if (cmd & PCI_COMMAND_MASTER)
516 printf("\tLatency: ");
518 printf("%d min, ", min_gnt);
520 printf("%d max, ", max_lat);
521 printf("%d set", latency);
523 printf(", cache line size %02x", cache_line);
527 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
532 if (cmd & PCI_COMMAND_MASTER)
533 printf("bus master, ");
534 if (cmd & PCI_COMMAND_VGA_PALETTE)
535 printf("VGA palette snoop, ");
536 if (cmd & PCI_COMMAND_WAIT)
537 printf("stepping, ");
538 if (cmd & PCI_COMMAND_FAST_BACK)
539 printf("fast Back2Back, ");
540 if (status & PCI_STATUS_66MHZ)
542 if (status & PCI_STATUS_UDF)
543 printf("user-definable features, ");
545 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
546 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
547 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
548 if (cmd & PCI_COMMAND_MASTER)
549 printf(", latency %d", latency);
552 printf(", IRQ " IRQ_FORMAT, irq);
558 if (bist & PCI_BIST_CAPABLE)
560 if (bist & PCI_BIST_START)
561 printf("\tBIST is running\n");
563 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
568 case PCI_HEADER_TYPE_NORMAL:
571 case PCI_HEADER_TYPE_BRIDGE:
574 case PCI_HEADER_TYPE_CARDBUS:
581 show_hex_dump(struct device *d)
584 int limit = (show_hex > 1) ? 256 : 64;
586 for(i=0; i<limit; i++)
590 printf(" %02x", get_conf_byte(d, i));
601 for(d=first_dev; d; d=d->next)
609 if (verbose || show_hex)
617 struct bridge *chain; /* Single-linked list of bridges */
618 struct bridge *next, *child; /* Tree of bridges */
619 struct bus *first_bus; /* List of busses connected to this bridge */
620 unsigned int primary, secondary, subordinate; /* Bus numbers */
621 struct device *br_dev;
627 struct device *first_dev, **last_dev;
630 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
633 find_bus(struct bridge *b, unsigned int n)
637 for(bus=b->first_bus; bus; bus=bus->sibling)
638 if (bus->number == n)
644 new_bus(struct bridge *b, unsigned int n)
646 struct bus *bus = xmalloc(sizeof(struct bus));
648 bus = xmalloc(sizeof(struct bus));
650 bus->sibling = b->first_bus;
651 bus->first_dev = NULL;
652 bus->last_dev = &bus->first_dev;
658 insert_dev(struct device *d, struct bridge *b)
662 if (! (bus = find_bus(b, d->bus)))
665 for(c=b->child; c; c=c->next)
666 if (c->secondary <= d->bus && d->bus <= c->subordinate)
667 return insert_dev(d, c);
668 bus = new_bus(b, d->bus);
670 /* Simple insertion at the end _does_ guarantee the correct order as the
671 * original device list was sorted by (bus, devfn) lexicographically
672 * and all devices on the new list have the same bus number.
675 bus->last_dev = &d->next;
682 struct device *d, *d2;
683 struct bridge *first_br, *b;
685 /* Build list of bridges */
687 first_br = &host_bridge;
688 for(d=first_dev; d; d=d->next)
690 word class = get_conf_word(d, PCI_CLASS_DEVICE);
691 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
693 b = xmalloc(sizeof(struct bridge));
694 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
695 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
696 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
699 b->next = b->child = NULL;
705 /* Create a bridge tree */
707 for(b=first_br; b; b=b->chain)
709 struct bridge *c, *best;
711 for(c=first_br; c; c=c->chain)
712 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
713 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
717 b->next = best->child;
722 /* Insert secondary bus for each bridge */
724 for(b=first_br; b; b=b->chain)
725 if (!find_bus(b, b->secondary))
726 new_bus(b, b->secondary);
728 /* Create bus structs and link devices */
733 insert_dev(d, &host_bridge);
739 print_it(byte *line, byte *p)
751 static void show_tree_bridge(struct bridge *, byte *, byte *);
754 show_tree_dev(struct device *d, byte *line, byte *p)
758 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
759 for(b=&host_bridge; b; b=b->chain)
762 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
763 show_tree_bridge(b, line, p);
767 p += sprintf(p, " %s", lookup_device_full(d->vendid, d->devid));
772 show_tree_bus(struct bus *b, byte *line, byte *p)
776 else if (!b->first_dev->next)
780 show_tree_dev(b->first_dev, line, p);
784 struct device *d = b->first_dev;
789 show_tree_dev(d, line, p+2);
794 show_tree_dev(d, line, p+2);
799 show_tree_bridge(struct bridge *b, byte *line, byte *p)
802 if (!b->first_bus->sibling)
804 if (b == &host_bridge)
805 p += sprintf(p, "[%02x]-", b->first_bus->number);
806 show_tree_bus(b->first_bus, line, p);
810 struct bus *u = b->first_bus;
815 k = p + sprintf(p, "+-[%02x]-", u->number);
816 show_tree_bus(u, line, k);
819 k = p + sprintf(p, "\\-[%02x]-", u->number);
820 show_tree_bus(u, line, k);
830 show_tree_bridge(&host_bridge, line, line);
836 main(int argc, char **argv)
840 while ((i = getopt(argc, argv, options)) != -1)
844 show_numeric_ids = 1;
853 bus_filter = strtol(optarg, NULL, 16);
856 slot_filter = strtol(optarg, NULL, 16);
859 func_filter = strtol(optarg, NULL, 16);
862 vend_filter = strtol(optarg, NULL, 16);
865 dev_filter = strtol(optarg, NULL, 16);
881 fprintf(stderr, help_msg);