2 * $Id: lspci.c,v 1.5 1998/01/27 11:50:08 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 if (read(fd, d->config, how_much) != how_much)
164 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
178 /* Config space accesses */
181 get_conf_byte(struct device *d, unsigned int pos)
183 return d->config[pos];
187 get_conf_word(struct device *d, unsigned int pos)
189 return d->config[pos] | (d->config[pos+1] << 8);
193 get_conf_long(struct device *d, unsigned int pos)
195 return d->config[pos] |
196 (d->config[pos+1] << 8) |
197 (d->config[pos+2] << 16) |
198 (d->config[pos+3] << 24);
204 compare_them(const void *A, const void *B)
206 const struct device *a = *(const struct device **)A;
207 const struct device *b = *(const struct device **)B;
213 if (a->devfn < b->devfn)
215 if (a->devfn > b->devfn)
223 struct device **index, **h;
228 for(d=first_dev; d; d=d->next)
230 h = index = alloca(sizeof(struct device *) * cnt);
231 for(d=first_dev; d; d=d->next)
233 qsort(index, cnt, sizeof(struct device *), compare_them);
234 last_dev = &first_dev;
239 last_dev = &(*h)->next;
248 show_terse(struct device *d)
252 printf("%02x:%02x.%x %s: %s",
256 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
257 lookup_device_full(d->vendid, d->devid));
258 if (c = get_conf_byte(d, PCI_REVISION_ID))
259 printf(" (rev %02x)", c);
260 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
261 printf(" (prog-if %02x)", c);
266 show_bases(struct device *d, int cnt)
268 word cmd = get_conf_word(d, PCI_COMMAND);
274 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
278 pos = d->kernel_base_addr[i];
279 if (!pos || pos == 0xffffffff)
281 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
283 if (cmd & PCI_COMMAND_IO)
286 printf("\tRegion %d: ", i);
289 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
292 else if (cmd & PCI_COMMAND_MEMORY)
294 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
296 printf("\tRegion %d: ", i);
299 printf("Memory at ");
300 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
305 if (!buscentric_view)
306 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
311 printf("%08lx (%s, %sprefetchable)\n",
312 pos & PCI_BASE_ADDRESS_MEM_MASK,
313 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
314 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
315 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
316 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
322 show_htype0(struct device *d)
324 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
330 word cmd = get_conf_word(d, PCI_COMMAND);
331 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
332 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
337 show_htype1(struct device *d)
339 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
340 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
341 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
342 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
343 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
344 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
345 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
346 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
347 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
348 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
349 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
352 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
353 get_conf_byte(d, PCI_PRIMARY_BUS),
354 get_conf_byte(d, PCI_SECONDARY_BUS),
355 get_conf_byte(d, PCI_SUBORDINATE_BUS),
356 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
358 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
359 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
360 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
363 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
364 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
365 if (io_type == PCI_IO_RANGE_TYPE_32)
367 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
368 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
371 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
374 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
376 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
379 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
380 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
381 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
384 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
385 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
386 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
389 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
390 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
391 if (pref_type == PCI_PREF_RANGE_TYPE_32)
392 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
394 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
395 get_conf_long(d, PCI_PREF_BASE_UPPER32),
397 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
401 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
402 printf("\tSecondary status: SERR\n");
406 word cmd = get_conf_word(d, PCI_COMMAND);
407 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
408 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
412 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
413 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
414 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
415 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
416 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
417 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
418 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
419 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
423 show_verbose(struct device *d)
425 word status = get_conf_word(d, PCI_STATUS);
426 word cmd = get_conf_word(d, PCI_COMMAND);
427 word class = get_conf_word(d, PCI_CLASS_DEVICE);
428 byte bist = get_conf_byte(d, PCI_BIST);
429 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
430 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
431 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
432 byte max_lat, min_gnt;
433 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
434 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
435 unsigned int irq, ex_htype;
436 word subsys_v, subsys_d;
442 case PCI_CLASS_BRIDGE_PCI:
448 if (ex_htype != htype)
450 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
457 max_lat = get_conf_byte(d, PCI_MAX_LAT);
458 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
459 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
460 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
463 irq = int_line = int_pin = min_gnt = max_lat = 0;
464 subsys_v = subsys_d = 0;
467 printf("\t!!! Unknown header type %02x\n", htype);
479 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
480 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
481 (cmd & PCI_COMMAND_IO) ? '+' : '-',
482 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
483 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
484 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
485 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
486 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
487 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
488 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
489 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
490 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
491 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
492 (status & PCI_STATUS_66MHZ) ? '+' : '-',
493 (status & PCI_STATUS_UDF) ? '+' : '-',
494 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
495 (status & PCI_STATUS_PARITY) ? '+' : '-',
496 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
497 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
498 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
499 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
500 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
501 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
502 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
503 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
504 if (cmd & PCI_COMMAND_MASTER)
506 printf("\tLatency: ");
508 printf("%d min, ", min_gnt);
510 printf("%d max, ", max_lat);
511 printf("%d set", latency);
513 printf(", cache line size %02x", cache_line);
517 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
522 if (cmd & PCI_COMMAND_MASTER)
523 printf("bus master, ");
524 if (cmd & PCI_COMMAND_VGA_PALETTE)
525 printf("VGA palette snoop, ");
526 if (cmd & PCI_COMMAND_WAIT)
527 printf("stepping, ");
528 if (cmd & PCI_COMMAND_FAST_BACK)
529 printf("fast Back2Back, ");
530 if (status & PCI_STATUS_66MHZ)
532 if (status & PCI_STATUS_UDF)
533 printf("user-definable features, ");
535 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
536 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
537 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
538 if (cmd & PCI_COMMAND_MASTER)
539 printf(", latency %d", latency);
542 printf(", IRQ " IRQ_FORMAT, irq);
548 if (bist & PCI_BIST_CAPABLE)
550 if (bist & PCI_BIST_START)
551 printf("\tBIST is running\n");
553 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
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));
588 for(d=first_dev; d; d=d->next)
596 if (verbose || show_hex)
604 struct bridge *chain; /* Single-linked list of bridges */
605 struct bridge *next, *child; /* Tree of bridges */
606 struct bus *first_bus; /* List of busses connected to this bridge */
607 unsigned int primary, secondary, subordinate; /* Bus numbers */
608 struct device *br_dev;
614 struct device *first_dev, **last_dev;
617 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
620 find_bus(struct bridge *b, unsigned int n)
624 for(bus=b->first_bus; bus; bus=bus->sibling)
625 if (bus->number == n)
631 new_bus(struct bridge *b, unsigned int n)
633 struct bus *bus = xmalloc(sizeof(struct bus));
635 bus = xmalloc(sizeof(struct bus));
637 bus->sibling = b->first_bus;
638 bus->first_dev = NULL;
639 bus->last_dev = &bus->first_dev;
645 insert_dev(struct device *d, struct bridge *b)
649 if (! (bus = find_bus(b, d->bus)))
652 for(c=b->child; c; c=c->next)
653 if (c->secondary <= d->bus && d->bus <= c->subordinate)
654 return insert_dev(d, c);
655 bus = new_bus(b, d->bus);
657 /* Simple insertion at the end _does_ guarantee the correct order as the
658 * original device list was sorted by (bus, devfn) lexicographically
659 * and all devices on the new list have the same bus number.
662 bus->last_dev = &d->next;
669 struct device *d, *d2;
670 struct bridge *first_br, *b;
672 /* Build list of bridges */
674 first_br = &host_bridge;
675 for(d=first_dev; d; d=d->next)
677 word class = get_conf_word(d, PCI_CLASS_DEVICE);
678 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
680 b = xmalloc(sizeof(struct bridge));
681 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
682 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
683 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
686 b->next = b->child = NULL;
692 /* Create a bridge tree */
694 for(b=first_br; b; b=b->chain)
696 struct bridge *c, *best;
698 for(c=first_br; c; c=c->chain)
699 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
700 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
704 b->next = best->child;
709 /* Insert secondary bus for each bridge */
711 for(b=first_br; b; b=b->chain)
712 if (!find_bus(b, b->secondary))
713 new_bus(b, b->secondary);
715 /* Create bus structs and link devices */
720 insert_dev(d, &host_bridge);
726 print_it(byte *line, byte *p)
738 static void show_tree_bridge(struct bridge *, byte *, byte *);
741 show_tree_dev(struct device *d, byte *line, byte *p)
745 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
746 for(b=&host_bridge; b; b=b->chain)
749 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
750 show_tree_bridge(b, line, p);
754 p += sprintf(p, " %s", lookup_device_full(d->vendid, d->devid));
759 show_tree_bus(struct bus *b, byte *line, byte *p)
763 else if (!b->first_dev->next)
767 show_tree_dev(b->first_dev, line, p);
771 struct device *d = b->first_dev;
776 show_tree_dev(d, line, p+2);
781 show_tree_dev(d, line, p+2);
786 show_tree_bridge(struct bridge *b, byte *line, byte *p)
789 if (!b->first_bus->sibling)
791 if (b == &host_bridge)
792 p += sprintf(p, "[%02x]-", b->first_bus->number);
793 show_tree_bus(b->first_bus, line, p);
797 struct bus *u = b->first_bus;
802 k = p + sprintf(p, "+-[%02x]-", u->number);
803 show_tree_bus(u, line, k);
806 k = p + sprintf(p, "\\-[%02x]-", u->number);
807 show_tree_bus(u, line, k);
817 show_tree_bridge(&host_bridge, line, line);
823 main(int argc, char **argv)
827 while ((i = getopt(argc, argv, options)) != -1)
831 show_numeric_ids = 1;
840 bus_filter = strtol(optarg, NULL, 16);
843 slot_filter = strtol(optarg, NULL, 16);
846 func_filter = strtol(optarg, NULL, 16);
849 vend_filter = strtol(optarg, NULL, 16);
852 dev_filter = strtol(optarg, NULL, 16);
868 fprintf(stderr, help_msg);