2 * $Id: lspci.c,v 1.6 1998/02/08 10:58: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.
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 */
31 static char *pci_dir = PROC_BUS_PCI;
33 static char options[] = "nvbxB:S:F:V:D:ti:p:";
35 static char help_msg[] = "\
36 Usage: lspci [<switches>]\n\
39 -n\tShow numeric ID's\n\
40 -b\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
41 -x\tShow hex-dump of config space (-xx shows full 256 bytes)\n\
42 -B <bus>, -S <slot>, -F <func>, -V <vendor>, -D <device> Show only selected devices\n\
44 -i <file>\tUse specified ID database instead of " ETC_PCI_IDS "\n\
45 -p <dir>\tUse specified bus directory instead of " PROC_BUS_PCI "\n\
48 /* Format strings used for IRQ numbers */
51 #define IRQ_FORMAT "%08x"
53 #define IRQ_FORMAT "%d"
56 /* Our view of the PCI bus */
62 unsigned int kernel_irq;
63 unsigned long kernel_base_addr[6];
67 static struct device *first_dev, **last_dev = &first_dev;
69 /* Miscellaneous routines */
72 xmalloc(unsigned int howmuch)
74 void *p = malloc(howmuch);
77 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
86 filter_out(struct device *d)
88 return (bus_filter >= 0 && d->bus != bus_filter ||
89 slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
90 func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
91 vend_filter >= 0 && d->vendid != vend_filter ||
92 dev_filter >= 0 && d->devid != dev_filter);
95 /* Interface for /proc/bus/pci */
104 sprintf(name, "%s/devices", pci_dir);
105 if (! (f = fopen(name, "r")))
110 while (fgets(line, sizeof(line), f))
112 struct device *d = xmalloc(sizeof(struct device));
113 unsigned int dfn, vend;
115 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
119 &d->kernel_base_addr[0],
120 &d->kernel_base_addr[1],
121 &d->kernel_base_addr[2],
122 &d->kernel_base_addr[3],
123 &d->kernel_base_addr[4],
124 &d->kernel_base_addr[5]);
126 d->devfn = dfn & 0xff;
127 d->vendid = vend >> 16U;
128 d->devid = vend & 0xffff;
140 make_proc_pci_name(struct device *d, char *p)
142 sprintf(p, "%s/%02x/%02x.%x",
143 pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
152 int how_much = (show_hex > 1) ? 256 : 64;
154 for(d=first_dev; d; d=d->next)
156 make_proc_pci_name(d, name);
157 if ((fd = open(name, O_RDONLY)) < 0)
159 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
162 res = read(fd, d->config, how_much);
165 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
170 fprintf(stderr, "lspci: Only %d bytes of config space available to you\n", res);
184 /* Config space accesses */
187 get_conf_byte(struct device *d, unsigned int pos)
189 return d->config[pos];
193 get_conf_word(struct device *d, unsigned int pos)
195 return d->config[pos] | (d->config[pos+1] << 8);
199 get_conf_long(struct device *d, unsigned int pos)
201 return d->config[pos] |
202 (d->config[pos+1] << 8) |
203 (d->config[pos+2] << 16) |
204 (d->config[pos+3] << 24);
210 compare_them(const void *A, const void *B)
212 const struct device *a = *(const struct device **)A;
213 const struct device *b = *(const struct device **)B;
219 if (a->devfn < b->devfn)
221 if (a->devfn > b->devfn)
229 struct device **index, **h;
234 for(d=first_dev; d; d=d->next)
236 h = index = alloca(sizeof(struct device *) * cnt);
237 for(d=first_dev; d; d=d->next)
239 qsort(index, cnt, sizeof(struct device *), compare_them);
240 last_dev = &first_dev;
245 last_dev = &(*h)->next;
254 show_terse(struct device *d)
258 printf("%02x:%02x.%x %s: %s",
262 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
263 lookup_device_full(d->vendid, d->devid));
264 if (c = get_conf_byte(d, PCI_REVISION_ID))
265 printf(" (rev %02x)", c);
266 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
267 printf(" (prog-if %02x)", c);
272 show_bases(struct device *d, int cnt)
274 word cmd = get_conf_word(d, PCI_COMMAND);
280 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
284 pos = d->kernel_base_addr[i];
285 if (!pos || pos == 0xffffffff)
287 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
289 if (cmd & PCI_COMMAND_IO)
292 printf("\tRegion %d: ", i);
295 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
298 else if (cmd & PCI_COMMAND_MEMORY)
300 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
302 printf("\tRegion %d: ", i);
305 printf("Memory at ");
306 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
311 if (!buscentric_view)
312 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
317 printf("%08lx (%s, %sprefetchable)\n",
318 pos & PCI_BASE_ADDRESS_MEM_MASK,
319 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
320 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
321 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
322 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
328 show_htype0(struct device *d)
330 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
336 word cmd = get_conf_word(d, PCI_COMMAND);
337 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
338 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
343 show_htype1(struct device *d)
345 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
346 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
347 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
348 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
349 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
350 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
351 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
352 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
353 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
354 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
355 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
358 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
359 get_conf_byte(d, PCI_PRIMARY_BUS),
360 get_conf_byte(d, PCI_SECONDARY_BUS),
361 get_conf_byte(d, PCI_SUBORDINATE_BUS),
362 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
364 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
365 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
366 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
369 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
370 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
371 if (io_type == PCI_IO_RANGE_TYPE_32)
373 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
374 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
377 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
380 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
382 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
385 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
386 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
387 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
390 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
391 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
392 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
395 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
396 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
397 if (pref_type == PCI_PREF_RANGE_TYPE_32)
398 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
400 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
401 get_conf_long(d, PCI_PREF_BASE_UPPER32),
403 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
407 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
408 printf("\tSecondary status: SERR\n");
412 word cmd = get_conf_word(d, PCI_COMMAND);
413 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
414 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
418 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
419 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
420 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
421 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
422 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
423 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
424 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
425 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
429 show_htype2(struct device *d)
434 show_verbose(struct device *d)
436 word status = get_conf_word(d, PCI_STATUS);
437 word cmd = get_conf_word(d, PCI_COMMAND);
438 word class = get_conf_word(d, PCI_CLASS_DEVICE);
439 byte bist = get_conf_byte(d, PCI_BIST);
440 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
441 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
442 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
443 byte max_lat, min_gnt;
444 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
445 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
447 word subsys_v, subsys_d;
453 case PCI_HEADER_TYPE_NORMAL:
454 if (class == PCI_CLASS_BRIDGE_PCI)
457 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
460 max_lat = get_conf_byte(d, PCI_MAX_LAT);
461 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
462 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
463 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
465 case PCI_HEADER_TYPE_BRIDGE:
466 if (class != PCI_CLASS_BRIDGE_PCI)
468 irq = int_line = int_pin = min_gnt = max_lat = 0;
469 subsys_v = subsys_d = 0;
471 case PCI_HEADER_TYPE_CARDBUS:
472 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
474 irq = int_line = int_pin = min_gnt = max_lat = 0;
475 subsys_v = subsys_d = 0;
478 printf("\t!!! Unknown header type %02x\n", htype);
490 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
491 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
492 (cmd & PCI_COMMAND_IO) ? '+' : '-',
493 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
494 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
495 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
496 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
497 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
498 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
499 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
500 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
501 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
502 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
503 (status & PCI_STATUS_66MHZ) ? '+' : '-',
504 (status & PCI_STATUS_UDF) ? '+' : '-',
505 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
506 (status & PCI_STATUS_PARITY) ? '+' : '-',
507 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
508 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
509 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
510 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
511 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
512 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
513 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
514 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
515 if (cmd & PCI_COMMAND_MASTER)
517 printf("\tLatency: ");
519 printf("%d min, ", min_gnt);
521 printf("%d max, ", max_lat);
522 printf("%d set", latency);
524 printf(", cache line size %02x", cache_line);
528 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
533 if (cmd & PCI_COMMAND_MASTER)
534 printf("bus master, ");
535 if (cmd & PCI_COMMAND_VGA_PALETTE)
536 printf("VGA palette snoop, ");
537 if (cmd & PCI_COMMAND_WAIT)
538 printf("stepping, ");
539 if (cmd & PCI_COMMAND_FAST_BACK)
540 printf("fast Back2Back, ");
541 if (status & PCI_STATUS_66MHZ)
543 if (status & PCI_STATUS_UDF)
544 printf("user-definable features, ");
546 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
547 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
548 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
549 if (cmd & PCI_COMMAND_MASTER)
550 printf(", latency %d", latency);
553 printf(", IRQ " IRQ_FORMAT, irq);
559 if (bist & PCI_BIST_CAPABLE)
561 if (bist & PCI_BIST_START)
562 printf("\tBIST is running\n");
564 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
569 case PCI_HEADER_TYPE_NORMAL:
572 case PCI_HEADER_TYPE_BRIDGE:
575 case PCI_HEADER_TYPE_CARDBUS:
582 show_hex_dump(struct device *d)
585 int limit = (show_hex > 1) ? 256 : 64;
587 for(i=0; i<limit; i++)
591 printf(" %02x", get_conf_byte(d, i));
602 for(d=first_dev; d; d=d->next)
610 if (verbose || show_hex)
618 struct bridge *chain; /* Single-linked list of bridges */
619 struct bridge *next, *child; /* Tree of bridges */
620 struct bus *first_bus; /* List of busses connected to this bridge */
621 unsigned int primary, secondary, subordinate; /* Bus numbers */
622 struct device *br_dev;
628 struct device *first_dev, **last_dev;
631 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
634 find_bus(struct bridge *b, unsigned int n)
638 for(bus=b->first_bus; bus; bus=bus->sibling)
639 if (bus->number == n)
645 new_bus(struct bridge *b, unsigned int n)
647 struct bus *bus = xmalloc(sizeof(struct bus));
649 bus = xmalloc(sizeof(struct bus));
651 bus->sibling = b->first_bus;
652 bus->first_dev = NULL;
653 bus->last_dev = &bus->first_dev;
659 insert_dev(struct device *d, struct bridge *b)
663 if (! (bus = find_bus(b, d->bus)))
666 for(c=b->child; c; c=c->next)
667 if (c->secondary <= d->bus && d->bus <= c->subordinate)
668 return insert_dev(d, c);
669 bus = new_bus(b, d->bus);
671 /* Simple insertion at the end _does_ guarantee the correct order as the
672 * original device list was sorted by (bus, devfn) lexicographically
673 * and all devices on the new list have the same bus number.
676 bus->last_dev = &d->next;
683 struct device *d, *d2;
684 struct bridge *first_br, *b;
686 /* Build list of bridges */
688 first_br = &host_bridge;
689 for(d=first_dev; d; d=d->next)
691 word class = get_conf_word(d, PCI_CLASS_DEVICE);
692 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
694 b = xmalloc(sizeof(struct bridge));
695 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
696 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
697 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
700 b->next = b->child = NULL;
706 /* Create a bridge tree */
708 for(b=first_br; b; b=b->chain)
710 struct bridge *c, *best;
712 for(c=first_br; c; c=c->chain)
713 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
714 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
718 b->next = best->child;
723 /* Insert secondary bus for each bridge */
725 for(b=first_br; b; b=b->chain)
726 if (!find_bus(b, b->secondary))
727 new_bus(b, b->secondary);
729 /* Create bus structs and link devices */
734 insert_dev(d, &host_bridge);
740 print_it(byte *line, byte *p)
752 static void show_tree_bridge(struct bridge *, byte *, byte *);
755 show_tree_dev(struct device *d, byte *line, byte *p)
759 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
760 for(b=&host_bridge; b; b=b->chain)
763 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
764 show_tree_bridge(b, line, p);
768 p += sprintf(p, " %s", lookup_device_full(d->vendid, d->devid));
773 show_tree_bus(struct bus *b, byte *line, byte *p)
777 else if (!b->first_dev->next)
781 show_tree_dev(b->first_dev, line, p);
785 struct device *d = b->first_dev;
790 show_tree_dev(d, line, p+2);
795 show_tree_dev(d, line, p+2);
800 show_tree_bridge(struct bridge *b, byte *line, byte *p)
803 if (!b->first_bus->sibling)
805 if (b == &host_bridge)
806 p += sprintf(p, "[%02x]-", b->first_bus->number);
807 show_tree_bus(b->first_bus, line, p);
811 struct bus *u = b->first_bus;
816 k = p + sprintf(p, "+-[%02x]-", u->number);
817 show_tree_bus(u, line, k);
820 k = p + sprintf(p, "\\-[%02x]-", u->number);
821 show_tree_bus(u, line, k);
831 show_tree_bridge(&host_bridge, line, line);
837 main(int argc, char **argv)
841 while ((i = getopt(argc, argv, options)) != -1)
845 show_numeric_ids = 1;
854 bus_filter = strtol(optarg, NULL, 16);
857 slot_filter = strtol(optarg, NULL, 16);
860 func_filter = strtol(optarg, NULL, 16);
863 vend_filter = strtol(optarg, NULL, 16);
866 dev_filter = strtol(optarg, NULL, 16);
882 fprintf(stderr, help_msg);