2 * $Id: lspci.c,v 1.19 1999/01/22 21:04:54 mj Exp $
4 * Linux PCI Utilities -- List All PCI Devices
6 * Copyright (c) 1997--1999 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 struct pci_filter filter; /* Device filter */
25 static int show_tree; /* Show bus tree */
26 static int machine_readable; /* Generate machine-readable output */
28 static char options[] = "nvbxs:d:ti:mg" GENERIC_OPTIONS ;
30 static char help_msg[] = "\
31 Usage: lspci [<switches>]\n\
34 -n\t\tShow numeric ID's\n\
35 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
36 -x\t\tShow hex-dump of config space\n\
37 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
38 -d [<vendor>]:[<device>]\tShow only selected devices\n\
39 -t\t\tShow bus tree\n\
40 -m\t\tProduce machine-readable output\n\
41 -i <file>\tUse specified ID database instead of %s\n"
45 /* Communication with libpci */
47 static struct pci_access *pacc;
49 /* Format strings used for IRQ numbers and memory addresses */
52 #define IRQ_FORMAT "%08x"
54 #define IRQ_FORMAT "%d"
57 #ifdef HAVE_64BIT_LONG_INT
58 #define LONG_FORMAT "%016lx"
60 #define LONG_FORMAT "%08lx"
63 /* Our view of the PCI bus */
72 static struct device *first_dev;
78 int how_much = (show_hex > 2) ? 256 : 64;
82 for(p=pacc->devices; p; p=p->next)
84 if (!pci_filter_match(&filter, p))
86 d = xmalloc(sizeof(struct device));
90 if (!pci_read_block(p, 0, d->config, how_much))
91 die("Unable to read %d bytes of configuration space.", how_much);
92 if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
94 /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
95 if (!pci_read_block(p, 0, d->config+64, 64))
96 die("Unable to read cardbus bridge extension data.");
99 d->config_cnt = how_much;
100 pci_setup_buffer(p, d->config);
101 pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
105 /* Config space accesses */
108 get_conf_byte(struct device *d, unsigned int pos)
110 return d->config[pos];
114 get_conf_word(struct device *d, unsigned int pos)
116 return d->config[pos] | (d->config[pos+1] << 8);
120 get_conf_long(struct device *d, unsigned int pos)
122 return d->config[pos] |
123 (d->config[pos+1] << 8) |
124 (d->config[pos+2] << 16) |
125 (d->config[pos+3] << 24);
131 compare_them(const void *A, const void *B)
133 const struct pci_dev *a = (*(const struct device **)A)->dev;
134 const struct pci_dev *b = (*(const struct device **)B)->dev;
144 if (a->func < b->func)
146 if (a->func > b->func)
154 struct device **index, **h, **last_dev;
159 for(d=first_dev; d; d=d->next)
161 h = index = alloca(sizeof(struct device *) * cnt);
162 for(d=first_dev; d; d=d->next)
164 qsort(index, cnt, sizeof(struct device *), compare_them);
165 last_dev = &first_dev;
170 last_dev = &(*h)->next;
179 show_terse(struct device *d)
182 struct pci_dev *p = d->dev;
183 byte classbuf[128], devbuf[128];
185 printf("%02x:%02x.%x %s: %s",
189 pci_lookup_name(pacc, classbuf, sizeof(classbuf),
191 get_conf_word(d, PCI_CLASS_DEVICE), 0),
192 pci_lookup_name(pacc, devbuf, sizeof(devbuf),
193 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
194 p->vendor_id, p->device_id));
195 if (c = get_conf_byte(d, PCI_REVISION_ID))
196 printf(" (rev %02x)", c);
197 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
198 printf(" (prog-if %02x)", c);
203 show_bases(struct device *d, int cnt)
205 struct pci_dev *p = d->dev;
206 word cmd = get_conf_word(d, PCI_COMMAND);
212 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
213 pos = p->base_addr[i];
214 if (flg == 0xffffffff)
219 printf("\tRegion %d: ", i);
222 if (pos && !flg) /* Reported by the OS, but not by the device */
224 printf("[virtual] ");
227 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
229 unsigned long a = pos & PCI_BASE_ADDRESS_IO_MASK;
230 printf("I/O ports at ");
233 else if (flg & PCI_BASE_ADDRESS_IO_MASK)
236 printf("<unassigned>");
237 if (!(cmd & PCI_COMMAND_IO))
238 printf(" [disabled]");
242 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
243 unsigned long a = pos & PCI_BASE_ADDRESS_MEM_MASK;
247 printf("Memory at ");
248 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
252 printf("<invalid-64bit-slot>\n");
258 z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
262 printf("%08x%08lx", z, a);
264 printf("<unassigned>");
272 printf(LONG_FORMAT, a);
274 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
276 printf(" (%s, %sprefetchable)",
277 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
278 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
279 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
280 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
281 if (!(cmd & PCI_COMMAND_MEMORY))
282 printf(" [disabled]");
289 show_htype0(struct device *d)
291 unsigned long rom = d->dev->rom_base_addr;
295 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
296 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
300 show_htype1(struct device *d)
302 struct pci_dev *p = d->dev;
303 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
304 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
305 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
306 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
307 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
308 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
309 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
310 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
311 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
312 unsigned long rom = p->rom_base_addr;
313 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
316 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
317 get_conf_byte(d, PCI_PRIMARY_BUS),
318 get_conf_byte(d, PCI_SECONDARY_BUS),
319 get_conf_byte(d, PCI_SUBORDINATE_BUS),
320 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
322 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
323 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
324 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
327 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
328 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
329 if (io_type == PCI_IO_RANGE_TYPE_32)
331 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
332 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
335 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
338 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
340 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
343 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
344 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
345 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
348 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
349 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
350 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
353 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
354 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
355 if (pref_type == PCI_PREF_RANGE_TYPE_32)
356 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
358 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
359 get_conf_long(d, PCI_PREF_BASE_UPPER32),
361 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
365 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
366 printf("\tSecondary status: SERR\n");
369 printf("\tExpansion ROM at %08lx%s\n", rom & PCI_ROM_ADDRESS_MASK,
370 (rom & PCI_ROM_ADDRESS_ENABLE) ? "" : " [disabled]");
373 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
374 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
375 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
376 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
377 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
378 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
379 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
380 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
384 show_htype2(struct device *d)
387 word cmd = get_conf_word(d, PCI_COMMAND);
388 word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
389 word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
392 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
393 get_conf_byte(d, PCI_CB_PRIMARY_BUS),
394 get_conf_byte(d, PCI_CB_CARD_BUS),
395 get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
396 get_conf_byte(d, PCI_CB_LATENCY_TIMER));
400 u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
401 u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
403 printf("Memory window %d: %08x-%08x%s%s\n", i, base, limit,
404 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
405 (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
410 u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
411 u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
412 if (!(base & PCI_IO_RANGE_TYPE_32))
417 base &= PCI_CB_IO_RANGE_MASK;
420 limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
421 printf("I/O window %d: %08x-%08x%s\n", i, base, limit,
422 (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
425 if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
426 printf("\tSecondary status: SERR\n");
428 printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
429 (brc & PCI_CB_BRIDGE_CTL_PARITY) ? '+' : '-',
430 (brc & PCI_CB_BRIDGE_CTL_SERR) ? '+' : '-',
431 (brc & PCI_CB_BRIDGE_CTL_ISA) ? '+' : '-',
432 (brc & PCI_CB_BRIDGE_CTL_VGA) ? '+' : '-',
433 (brc & PCI_CB_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
434 (brc & PCI_CB_BRIDGE_CTL_CB_RESET) ? '+' : '-',
435 (brc & PCI_CB_BRIDGE_CTL_16BIT_INT) ? '+' : '-',
436 (brc & PCI_CB_BRIDGE_CTL_POST_WRITES) ? '+' : '-');
438 printf("\t16-bit legacy interface ports at %04x\n", exca);
442 show_verbose(struct device *d)
444 struct pci_dev *p = d->dev;
445 word status = get_conf_word(d, PCI_STATUS);
446 word cmd = get_conf_word(d, PCI_COMMAND);
447 word class = get_conf_word(d, PCI_CLASS_DEVICE);
448 byte bist = get_conf_byte(d, PCI_BIST);
449 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
450 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
451 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
452 byte max_lat, min_gnt;
453 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
454 unsigned int irq = p->irq;
455 word subsys_v, subsys_d;
462 case PCI_HEADER_TYPE_NORMAL:
463 if (class == PCI_CLASS_BRIDGE_PCI)
466 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
469 max_lat = get_conf_byte(d, PCI_MAX_LAT);
470 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
471 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
472 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
474 case PCI_HEADER_TYPE_BRIDGE:
475 if (class != PCI_CLASS_BRIDGE_PCI)
477 irq = int_pin = min_gnt = max_lat = 0;
478 subsys_v = subsys_d = 0;
480 case PCI_HEADER_TYPE_CARDBUS:
481 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
483 min_gnt = max_lat = 0;
484 subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
485 subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
488 printf("\t!!! Unknown header type %02x\n", htype);
492 if (verbose && subsys_v && subsys_v != 0xffff)
493 printf("\tSubsystem: %s\n",
494 pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
495 PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
496 subsys_v, subsys_d));
500 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
501 (cmd & PCI_COMMAND_IO) ? '+' : '-',
502 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
503 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
504 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
505 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
506 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
507 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
508 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
509 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
510 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
511 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
512 (status & PCI_STATUS_66MHZ) ? '+' : '-',
513 (status & PCI_STATUS_UDF) ? '+' : '-',
514 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
515 (status & PCI_STATUS_PARITY) ? '+' : '-',
516 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
517 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
518 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
519 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
520 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
521 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
522 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
523 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
524 if (cmd & PCI_COMMAND_MASTER)
526 printf("\tLatency: ");
528 printf("%d min, ", min_gnt);
530 printf("%d max, ", max_lat);
531 printf("%d set", latency);
533 printf(", cache line size %02x", cache_line);
537 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
538 (int_pin ? 'A' + int_pin - 1 : '?'), irq);
543 if (cmd & PCI_COMMAND_MASTER)
544 printf("bus master, ");
545 if (cmd & PCI_COMMAND_VGA_PALETTE)
546 printf("VGA palette snoop, ");
547 if (cmd & PCI_COMMAND_WAIT)
548 printf("stepping, ");
549 if (cmd & PCI_COMMAND_FAST_BACK)
550 printf("fast Back2Back, ");
551 if (status & PCI_STATUS_66MHZ)
553 if (status & PCI_STATUS_UDF)
554 printf("user-definable features, ");
556 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
557 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
558 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
559 if (cmd & PCI_COMMAND_MASTER)
560 printf(", latency %d", latency);
562 printf(", IRQ " IRQ_FORMAT, irq);
566 if (bist & PCI_BIST_CAPABLE)
568 if (bist & PCI_BIST_START)
569 printf("\tBIST is running\n");
571 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
576 case PCI_HEADER_TYPE_NORMAL:
579 case PCI_HEADER_TYPE_BRIDGE:
582 case PCI_HEADER_TYPE_CARDBUS:
589 show_hex_dump(struct device *d)
593 for(i=0; i<d->config_cnt; i++)
597 printf(" %02x", get_conf_byte(d, i));
604 show_machine(struct device *d)
606 struct pci_dev *p = d->dev;
608 word sv_id=0, sd_id=0;
609 char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
611 switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
613 case PCI_HEADER_TYPE_NORMAL:
614 sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
615 sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
617 case PCI_HEADER_TYPE_CARDBUS:
618 sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
619 sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
625 printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
626 printf("Class:\t%s\n",
627 pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0));
628 printf("Vendor:\t%s\n",
629 pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
630 printf("Device:\t%s\n",
631 pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
632 if (sv_id && sv_id != 0xffff)
634 printf("SVendor:\t%s\n",
635 pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id));
636 printf("SDevice:\t%s\n",
637 pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
639 if (c = get_conf_byte(d, PCI_REVISION_ID))
640 printf("Rev:\t%02x\n", c);
641 if (c = get_conf_byte(d, PCI_CLASS_PROG))
642 printf("ProgIf:\t%02x\n", c);
646 printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
647 printf("\"%s\" \"%s\" \"%s\"",
648 pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
649 get_conf_word(d, PCI_CLASS_DEVICE), 0),
650 pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
651 p->vendor_id, p->device_id),
652 pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
653 p->vendor_id, p->device_id));
654 if (c = get_conf_byte(d, PCI_REVISION_ID))
655 printf(" -r%02x", c);
656 if (c = get_conf_byte(d, PCI_CLASS_PROG))
657 printf(" -p%02x", c);
658 if (sv_id && sv_id != 0xffff)
659 printf(" \"%s\" \"%s\"",
660 pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id, sd_id),
661 pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, sv_id, sd_id));
663 printf(" \"\" \"\"");
673 for(d=first_dev; d; d=d->next)
675 if (machine_readable)
683 if (verbose || show_hex)
691 struct bridge *chain; /* Single-linked list of bridges */
692 struct bridge *next, *child; /* Tree of bridges */
693 struct bus *first_bus; /* List of busses connected to this bridge */
694 unsigned int primary, secondary, subordinate; /* Bus numbers */
695 struct device *br_dev;
701 struct device *first_dev, **last_dev;
704 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
707 find_bus(struct bridge *b, unsigned int n)
711 for(bus=b->first_bus; bus; bus=bus->sibling)
712 if (bus->number == n)
718 new_bus(struct bridge *b, unsigned int n)
720 struct bus *bus = xmalloc(sizeof(struct bus));
722 bus = xmalloc(sizeof(struct bus));
724 bus->sibling = b->first_bus;
725 bus->first_dev = NULL;
726 bus->last_dev = &bus->first_dev;
732 insert_dev(struct device *d, struct bridge *b)
734 struct pci_dev *p = d->dev;
737 if (! (bus = find_bus(b, p->bus)))
740 for(c=b->child; c; c=c->next)
741 if (c->secondary <= p->bus && p->bus <= c->subordinate)
742 return insert_dev(d, c);
743 bus = new_bus(b, p->bus);
745 /* Simple insertion at the end _does_ guarantee the correct order as the
746 * original device list was sorted by (bus, devfn) lexicographically
747 * and all devices on the new list have the same bus number.
750 bus->last_dev = &d->next;
757 struct device *d, *d2;
758 struct bridge **last_br, *b;
760 /* Build list of bridges */
762 last_br = &host_bridge.chain;
763 for(d=first_dev; d; d=d->next)
765 word class = get_conf_word(d, PCI_CLASS_DEVICE);
766 byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
767 if (class == PCI_CLASS_BRIDGE_PCI &&
768 (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
770 b = xmalloc(sizeof(struct bridge));
771 if (ht == PCI_HEADER_TYPE_BRIDGE)
773 b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
774 b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
775 b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
779 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
780 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
781 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
785 b->next = b->child = NULL;
792 /* Create a bridge tree */
794 for(b=&host_bridge; b; b=b->chain)
796 struct bridge *c, *best;
798 for(c=&host_bridge; c; c=c->chain)
799 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
800 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
804 b->next = best->child;
809 /* Insert secondary bus for each bridge */
811 for(b=&host_bridge; b; b=b->chain)
812 if (!find_bus(b, b->secondary))
813 new_bus(b, b->secondary);
815 /* Create bus structs and link devices */
820 insert_dev(d, &host_bridge);
826 print_it(byte *line, byte *p)
832 if (*p == '+' || *p == '|')
838 static void show_tree_bridge(struct bridge *, byte *, byte *);
841 show_tree_dev(struct device *d, byte *line, byte *p)
843 struct pci_dev *q = d->dev;
847 p += sprintf(p, "%02x.%x", q->dev, q->func);
848 for(b=&host_bridge; b; b=b->chain)
851 if (b->secondary == b->subordinate)
852 p += sprintf(p, "-[%02x]-", b->secondary);
854 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
855 show_tree_bridge(b, line, p);
859 p += sprintf(p, " %s",
860 pci_lookup_name(pacc, namebuf, sizeof(namebuf),
861 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
862 q->vendor_id, q->device_id));
867 show_tree_bus(struct bus *b, byte *line, byte *p)
871 else if (!b->first_dev->next)
875 show_tree_dev(b->first_dev, line, p);
879 struct device *d = b->first_dev;
884 show_tree_dev(d, line, p+2);
889 show_tree_dev(d, line, p+2);
894 show_tree_bridge(struct bridge *b, byte *line, byte *p)
897 if (!b->first_bus->sibling)
899 if (b == &host_bridge)
900 p += sprintf(p, "[%02x]-", b->first_bus->number);
901 show_tree_bus(b->first_bus, line, p);
905 struct bus *u = b->first_bus;
910 k = p + sprintf(p, "+-[%02x]-", u->number);
911 show_tree_bus(u, line, k);
914 k = p + sprintf(p, "\\-[%02x]-", u->number);
915 show_tree_bus(u, line, k);
925 show_tree_bridge(&host_bridge, line, line);
931 main(int argc, char **argv)
936 if (argc == 2 && !strcmp(argv[1], "--version"))
938 puts("lspci version " PCIUTILS_VERSION);
944 pci_filter_init(pacc, &filter);
946 while ((i = getopt(argc, argv, options)) != -1)
950 pacc->numeric_ids = 1;
956 pacc->buscentric = 1;
960 if (msg = pci_filter_parse_slot(&filter, optarg))
964 if (msg = pci_filter_parse_id(&filter, optarg))
974 pacc->id_file_name = optarg;
980 if (parse_generic_option(i, pacc, optarg))
983 fprintf(stderr, help_msg, pacc->id_file_name);