2 * $Id: lspci.c,v 1.4 1998/01/01 19:36:23 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)
276 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
278 if (cmd & PCI_COMMAND_IO)
281 printf("\tRegion %d: ", i);
284 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
287 else if (cmd & PCI_COMMAND_MEMORY)
289 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
291 printf("\tRegion %d: ", i);
294 printf("Memory at ");
295 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
300 if (!buscentric_view)
301 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
306 printf("%08lx (%s, %sprefetchable)\n",
307 pos & PCI_BASE_ADDRESS_MEM_MASK,
308 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
309 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
310 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
311 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
317 show_htype0(struct device *d)
319 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
325 word cmd = get_conf_word(d, PCI_COMMAND);
326 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
327 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
332 show_htype1(struct device *d)
334 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
335 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
336 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
337 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
338 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
339 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
340 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
341 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
342 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
343 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
344 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
347 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
348 get_conf_byte(d, PCI_PRIMARY_BUS),
349 get_conf_byte(d, PCI_SECONDARY_BUS),
350 get_conf_byte(d, PCI_SUBORDINATE_BUS),
351 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
353 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
354 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
355 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
358 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
359 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
360 if (io_type == PCI_IO_RANGE_TYPE_32)
362 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
363 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
366 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
369 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
371 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
374 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
375 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
376 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
379 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
380 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
381 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
384 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
385 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
386 if (pref_type == PCI_PREF_RANGE_TYPE_32)
387 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
389 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
390 get_conf_long(d, PCI_PREF_BASE_UPPER32),
392 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
396 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
397 printf("\tSecondary status: SERR\n");
401 word cmd = get_conf_word(d, PCI_COMMAND);
402 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
403 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
407 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
408 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
409 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
410 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
411 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
412 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
413 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
414 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
418 show_verbose(struct device *d)
420 word status = get_conf_word(d, PCI_STATUS);
421 word cmd = get_conf_word(d, PCI_COMMAND);
422 word class = get_conf_word(d, PCI_CLASS_DEVICE);
423 byte bist = get_conf_byte(d, PCI_BIST);
424 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
425 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
426 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
427 byte max_lat, min_gnt;
428 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
429 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
430 unsigned int irq, ex_htype;
431 word subsys_v, subsys_d;
437 case PCI_CLASS_BRIDGE_PCI:
443 if (ex_htype != htype)
445 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
452 max_lat = get_conf_byte(d, PCI_MAX_LAT);
453 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
454 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
455 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
458 irq = int_line = int_pin = min_gnt = max_lat = 0;
459 subsys_v = subsys_d = 0;
462 printf("\t!!! Unknown header type %02x\n", htype);
474 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
475 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
476 (cmd & PCI_COMMAND_IO) ? '+' : '-',
477 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
478 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
479 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
480 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
481 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
482 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
483 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
484 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
485 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
486 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
487 (status & PCI_STATUS_66MHZ) ? '+' : '-',
488 (status & PCI_STATUS_UDF) ? '+' : '-',
489 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
490 (status & PCI_STATUS_PARITY) ? '+' : '-',
491 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
492 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
493 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
494 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
495 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
496 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
497 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
498 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
499 if (cmd & PCI_COMMAND_MASTER)
501 printf("\tLatency: ");
503 printf("%d min, ", min_gnt);
505 printf("%d max, ", max_lat);
506 printf("%d set", latency);
508 printf(", cache line size %02x", cache_line);
512 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
517 if (cmd & PCI_COMMAND_MASTER)
518 printf("bus master, ");
519 if (cmd & PCI_COMMAND_VGA_PALETTE)
520 printf("VGA palette snoop, ");
521 if (cmd & PCI_COMMAND_WAIT)
522 printf("stepping, ");
523 if (cmd & PCI_COMMAND_FAST_BACK)
524 printf("fast Back2Back, ");
525 if (status & PCI_STATUS_66MHZ)
527 if (status & PCI_STATUS_UDF)
528 printf("user-definable features, ");
530 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
531 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
532 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
533 if (cmd & PCI_COMMAND_MASTER)
534 printf(", latency %d", latency);
537 printf(", IRQ " IRQ_FORMAT, irq);
543 if (bist & PCI_BIST_CAPABLE)
545 if (bist & PCI_BIST_START)
546 printf("\tBIST is running\n");
548 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
563 show_hex_dump(struct device *d)
566 int limit = (show_hex > 1) ? 256 : 64;
568 for(i=0; i<limit; i++)
572 printf(" %02x", get_conf_byte(d, i));
583 for(d=first_dev; d; d=d->next)
591 if (verbose || show_hex)
599 struct bridge *chain; /* Single-linked list of bridges */
600 struct bridge *next, *child; /* Tree of bridges */
601 struct bus *first_bus; /* List of busses connected to this bridge */
602 unsigned int primary, secondary, subordinate; /* Bus numbers */
603 struct device *br_dev;
609 struct device *first_dev, **last_dev;
612 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
615 find_bus(struct bridge *b, unsigned int n)
619 for(bus=b->first_bus; bus; bus=bus->sibling)
620 if (bus->number == n)
626 new_bus(struct bridge *b, unsigned int n)
628 struct bus *bus = xmalloc(sizeof(struct bus));
630 bus = xmalloc(sizeof(struct bus));
632 bus->sibling = b->first_bus;
633 bus->first_dev = NULL;
634 bus->last_dev = &bus->first_dev;
640 insert_dev(struct device *d, struct bridge *b)
644 if (! (bus = find_bus(b, d->bus)))
647 for(c=b->child; c; c=c->next)
648 if (c->secondary <= d->bus && d->bus <= c->subordinate)
649 return insert_dev(d, c);
650 bus = new_bus(b, d->bus);
652 /* Simple insertion at the end _does_ guarantee the correct order as the
653 * original device list was sorted by (bus, devfn) lexicographically
654 * and all devices on the new list have the same bus number.
657 bus->last_dev = &d->next;
664 struct device *d, *d2;
665 struct bridge *first_br, *b;
667 /* Build list of bridges */
669 first_br = &host_bridge;
670 for(d=first_dev; d; d=d->next)
672 word class = get_conf_word(d, PCI_CLASS_DEVICE);
673 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
675 b = xmalloc(sizeof(struct bridge));
676 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
677 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
678 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
681 b->next = b->child = NULL;
687 /* Create a bridge tree */
689 for(b=first_br; b; b=b->chain)
691 struct bridge *c, *best;
693 for(c=first_br; c; c=c->chain)
694 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
695 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
699 b->next = best->child;
704 /* Insert secondary bus for each bridge */
706 for(b=first_br; b; b=b->chain)
707 if (!find_bus(b, b->secondary))
708 new_bus(b, b->secondary);
710 /* Create bus structs and link devices */
715 insert_dev(d, &host_bridge);
721 print_it(byte *line, byte *p)
733 static void show_tree_bridge(struct bridge *, byte *, byte *);
736 show_tree_dev(struct device *d, byte *line, byte *p)
740 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
741 for(b=&host_bridge; b; b=b->chain)
744 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
745 show_tree_bridge(b, line, p);
752 show_tree_bus(struct bus *b, byte *line, byte *p)
756 else if (!b->first_dev->next)
760 show_tree_dev(b->first_dev, line, p);
764 struct device *d = b->first_dev;
769 show_tree_dev(d, line, p+2);
774 show_tree_dev(d, line, p+2);
779 show_tree_bridge(struct bridge *b, byte *line, byte *p)
782 if (!b->first_bus->sibling)
784 if (b == &host_bridge)
785 p += sprintf(p, "[%02x]-", b->first_bus->number);
786 show_tree_bus(b->first_bus, line, p);
790 struct bus *u = b->first_bus;
795 k = p + sprintf(p, "+-[%02x]-", u->number);
796 show_tree_bus(u, line, k);
799 k = p + sprintf(p, "\\-[%02x]-", u->number);
800 show_tree_bus(u, line, k);
810 show_tree_bridge(&host_bridge, line, line);
816 main(int argc, char **argv)
820 while ((i = getopt(argc, argv, options)) != -1)
824 show_numeric_ids = 1;
833 bus_filter = strtol(optarg, NULL, 16);
836 slot_filter = strtol(optarg, NULL, 16);
839 func_filter = strtol(optarg, NULL, 16);
842 vend_filter = strtol(optarg, NULL, 16);
845 dev_filter = strtol(optarg, NULL, 16);
855 fprintf(stderr, help_msg);