2 * $Id: lspci.c,v 1.1 1997/12/23 10:29:18 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;
31 static char options[] = "nvbxB:S:F:V:D:";
33 static char help_msg[] = "\
34 Usage: lspci [<switches>]\n\
37 -n\tShow numeric ID's\n\
38 -b\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
39 -x\tShow hex-dump of config space (-xx shows full 256 bytes)\n\
40 -B <bus>, -S <slot>, -F <func>, -V <vendor>, -D <device> Show only selected devices\n\
43 /* Our view of the PCI bus */
49 unsigned int kernel_irq;
50 unsigned long kernel_base_addr[6];
54 static struct device *first_dev, **last_dev = &first_dev;
56 /* Miscellaneous routines */
59 xmalloc(unsigned int howmuch)
61 void *p = malloc(howmuch);
64 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
73 filter_out(struct device *d)
75 return (bus_filter >= 0 && d->bus != bus_filter ||
76 slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
77 func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
78 vend_filter >= 0 && d->vendid != vend_filter ||
79 dev_filter >= 0 && d->devid != dev_filter);
82 /* Interface for /proc/bus/pci */
90 if (! (f = fopen(PROC_BUS_PCI "/devices", "r")))
92 perror("Unable to open " PROC_BUS_PCI "/devices");
95 while (fgets(line, sizeof(line), f))
97 struct device *d = xmalloc(sizeof(struct device));
98 unsigned int dfn, vend;
100 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
104 &d->kernel_base_addr[0],
105 &d->kernel_base_addr[1],
106 &d->kernel_base_addr[2],
107 &d->kernel_base_addr[3],
108 &d->kernel_base_addr[4],
109 &d->kernel_base_addr[5]);
111 d->devfn = dfn & 0xff;
112 d->vendid = vend >> 16U;
113 d->devid = vend & 0xffff;
125 make_proc_pci_name(struct device *d, char *p)
127 sprintf(p, PROC_BUS_PCI "/%02x/%02x.%x",
128 d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
137 int how_much = (show_hex > 1) ? 256 : 64;
139 for(d=first_dev; d; d=d->next)
141 make_proc_pci_name(d, name);
142 if ((fd = open(name, O_RDONLY)) < 0)
144 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
147 if (read(fd, d->config, how_much) != how_much)
149 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
163 /* Config space accesses */
166 get_conf_byte(struct device *d, unsigned int pos)
168 return d->config[pos];
172 get_conf_word(struct device *d, unsigned int pos)
174 return d->config[pos] | (d->config[pos+1] << 8);
178 get_conf_long(struct device *d, unsigned int pos)
180 return d->config[pos] |
181 (d->config[pos+1] << 8) |
182 (d->config[pos+2] << 16) |
183 (d->config[pos+3] << 24);
189 compare_them(const void *A, const void *B)
191 const struct device *a = *(const struct device **)A;
192 const struct device *b = *(const struct device **)B;
198 if (a->devfn < b->devfn)
200 if (a->devfn > b->devfn)
208 struct device **index, **h;
213 for(d=first_dev; d; d=d->next)
215 h = index = alloca(sizeof(struct device *) * cnt);
216 for(d=first_dev; d; d=d->next)
218 qsort(index, cnt, sizeof(struct device *), compare_them);
219 last_dev = &first_dev;
224 last_dev = &(*h)->next;
233 show_terse(struct device *d)
237 printf("%02x:%02x.%x %s: %s",
241 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
242 lookup_device_full(d->vendid, d->devid));
243 if (c = get_conf_byte(d, PCI_REVISION_ID))
244 printf(" (rev %02x)", c);
245 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
246 printf(" (prog-if %02x)", c);
251 show_bases(struct device *d, int cnt)
253 word cmd = get_conf_word(d, PCI_COMMAND);
259 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
263 pos = d->kernel_base_addr[i];
264 if (!pos || pos == 0xffffffff)
267 printf("\tRegion %d: ", i);
270 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
271 printf("I/O ports at %04lx%s\n",
272 pos & PCI_BASE_ADDRESS_IO_MASK,
273 (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
276 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
277 printf("Memory at ");
278 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
283 if (!buscentric_view)
284 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
289 printf("%08lx (%s, %sprefetchable)%s\n",
290 pos & PCI_BASE_ADDRESS_MEM_MASK,
291 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
292 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
293 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
294 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-",
295 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
301 show_htype0(struct device *d)
303 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
309 word cmd = get_conf_word(d, PCI_COMMAND);
310 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
311 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
316 show_htype1(struct device *d)
318 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
319 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
320 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
321 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
322 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
323 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
324 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
325 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
326 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
327 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
328 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
331 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
332 get_conf_byte(d, PCI_PRIMARY_BUS),
333 get_conf_byte(d, PCI_SECONDARY_BUS),
334 get_conf_byte(d, PCI_SUBORDINATE_BUS),
335 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
337 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
338 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
339 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
342 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
343 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
344 if (io_type == PCI_IO_RANGE_TYPE_32)
346 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
347 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
350 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
353 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
355 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
358 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
359 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
360 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
363 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
364 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
365 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
368 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
369 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
370 if (pref_type == PCI_PREF_RANGE_TYPE_32)
371 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
373 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
374 get_conf_long(d, PCI_PREF_BASE_UPPER32),
376 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
380 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
381 printf("\tSecondary status: SERR\n");
385 word cmd = get_conf_word(d, PCI_COMMAND);
386 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
387 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
391 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
392 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
393 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
394 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
395 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
396 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
397 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
398 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
402 show_verbose(struct device *d)
404 word status = get_conf_word(d, PCI_STATUS);
405 word cmd = get_conf_word(d, PCI_COMMAND);
406 word class = get_conf_word(d, PCI_CLASS_DEVICE);
407 byte bist = get_conf_byte(d, PCI_BIST);
408 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
409 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
410 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
411 byte max_lat, min_gnt;
412 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
413 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
414 unsigned int irq, ex_htype;
415 word subsys_v, subsys_d;
421 case PCI_CLASS_BRIDGE_PCI:
427 if (ex_htype != htype)
429 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
436 max_lat = get_conf_byte(d, PCI_MAX_LAT);
437 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
438 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
439 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
442 irq = int_line = int_pin = min_gnt = max_lat = 0;
443 subsys_v = subsys_d = 0;
446 printf("\t!!! Unknown header type %02x\n", htype);
458 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
459 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
460 (cmd & PCI_COMMAND_IO) ? '+' : '-',
461 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
462 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
463 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
464 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
465 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
466 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
467 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
468 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
469 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
470 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
471 (status & PCI_STATUS_66MHZ) ? '+' : '-',
472 (status & PCI_STATUS_UDF) ? '+' : '-',
473 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
474 (status & PCI_STATUS_PARITY) ? '+' : '-',
475 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
476 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
477 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
478 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
479 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
480 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
481 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
482 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
483 if (cmd & PCI_COMMAND_MASTER)
485 printf("\tLatency: ");
487 printf("%d min, ", min_gnt);
489 printf("%d max, ", max_lat);
490 printf("%d set", latency);
492 printf(", cache line size %02x", cache_line);
496 printf("\tInterrupt: pin %c routed to IRQ %d\n", 'A' + int_pin - 1, irq);
501 if (cmd & PCI_COMMAND_MASTER)
502 printf("bus master, ");
503 if (cmd & PCI_COMMAND_VGA_PALETTE)
504 printf("VGA palette snoop, ");
505 if (cmd & PCI_COMMAND_WAIT)
506 printf("stepping, ");
507 if (cmd & PCI_COMMAND_FAST_BACK)
508 printf("fast Back2Back, ");
509 if (status & PCI_STATUS_66MHZ)
511 if (status & PCI_STATUS_UDF)
512 printf("user-definable features, ");
514 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
515 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
516 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
517 if (cmd & PCI_COMMAND_MASTER)
518 printf(", latency %d", latency);
521 printf(", IRQ %d", irq);
527 if (bist & PCI_BIST_CAPABLE)
529 if (bist & PCI_BIST_START)
530 printf("\tBIST is running\n");
532 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
547 show_hex_dump(struct device *d)
550 int limit = (show_hex > 1) ? 256 : 64;
552 for(i=0; i<limit; i++)
556 printf(" %02x", get_conf_byte(d, i));
567 for(d=first_dev; d; d=d->next)
575 if (verbose || show_hex)
583 main(int argc, char **argv)
587 while ((i = getopt(argc, argv, options)) != -1)
591 show_numeric_ids = 1;
600 bus_filter = strtol(optarg, NULL, 16);
603 slot_filter = strtol(optarg, NULL, 16);
606 func_filter = strtol(optarg, NULL, 16);
609 vend_filter = strtol(optarg, NULL, 16);
612 dev_filter = strtol(optarg, NULL, 16);
619 fprintf(stderr, help_msg);