]> git.ipfire.org Git - thirdparty/pciutils.git/blame - lspci.c
PCI-to-PCI bridges were not displayed properly in the tree format.
[thirdparty/pciutils.git] / lspci.c
CommitLineData
98e39e09 1/*
008407bd 2 * $Id: lspci.c,v 1.9 1998/03/19 15:56:43 mj Exp $
98e39e09
MM
3 *
4 * Linux PCI Utilities -- List All PCI Devices
5 *
18928b91 6 * Copyright (c) 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
98e39e09
MM
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
14#include <fcntl.h>
15#include <unistd.h>
98e39e09
MM
16
17#include "pciutils.h"
18
19/* Options */
20
21static int verbose; /* Show detailed information */
22static int buscentric_view; /* Show bus addresses/IRQ's instead of CPU-visible ones */
23static int show_hex; /* Show contents of config space as hexadecimal numbers */
24static int bus_filter = -1; /* Bus, slot, function, vendor and device ID filtering */
25static int slot_filter = -1;
26static int func_filter = -1;
27static int vend_filter = -1;
28static int dev_filter = -1;
6d0dc0fd 29static int show_tree; /* Show bus tree */
0a33d0ec 30static int machine_readable; /* Generate machine-readable output */
18928b91 31static char *pci_dir = PROC_BUS_PCI;
98e39e09 32
0a33d0ec 33static char options[] = "nvbxB:S:F:V:D:ti:p:m";
98e39e09
MM
34
35static char help_msg[] = "\
36Usage: lspci [<switches>]\n\
37\n\
38-v\tBe verbose\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\
6d0dc0fd 43-t\tShow bus tree\n\
0a33d0ec 44-m\tProduce machine-readable output\n\
18928b91
MM
45-i <file>\tUse specified ID database instead of " ETC_PCI_IDS "\n\
46-p <dir>\tUse specified bus directory instead of " PROC_BUS_PCI "\n\
98e39e09
MM
47";
48
f17b962b
MM
49/* Format strings used for IRQ numbers */
50
2f48f637 51#ifdef ARCH_SPARC64
f17b962b
MM
52#define IRQ_FORMAT "%08x"
53#else
54#define IRQ_FORMAT "%d"
55#endif
56
98e39e09
MM
57/* Our view of the PCI bus */
58
59struct device {
60 struct device *next;
61 byte bus, devfn;
62 word vendid, devid;
63 unsigned int kernel_irq;
64 unsigned long kernel_base_addr[6];
65 byte config[256];
66};
67
68static struct device *first_dev, **last_dev = &first_dev;
69
70/* Miscellaneous routines */
71
72void *
73xmalloc(unsigned int howmuch)
74{
75 void *p = malloc(howmuch);
76 if (!p)
77 {
78 fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
79 exit(1);
80 }
81 return p;
82}
83
84/* Filtering */
85
86static inline int
87filter_out(struct device *d)
88{
89 return (bus_filter >= 0 && d->bus != bus_filter ||
90 slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
91 func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
92 vend_filter >= 0 && d->vendid != vend_filter ||
93 dev_filter >= 0 && d->devid != dev_filter);
94}
95
96/* Interface for /proc/bus/pci */
97
98static void
99scan_dev_list(void)
100{
101 FILE *f;
102 byte line[256];
18928b91 103 byte name[256];
98e39e09 104
18928b91
MM
105 sprintf(name, "%s/devices", pci_dir);
106 if (! (f = fopen(name, "r")))
98e39e09 107 {
18928b91 108 perror(name);
98e39e09
MM
109 exit(1);
110 }
111 while (fgets(line, sizeof(line), f))
112 {
113 struct device *d = xmalloc(sizeof(struct device));
114 unsigned int dfn, vend;
115
116 sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
117 &dfn,
118 &vend,
119 &d->kernel_irq,
120 &d->kernel_base_addr[0],
121 &d->kernel_base_addr[1],
122 &d->kernel_base_addr[2],
123 &d->kernel_base_addr[3],
124 &d->kernel_base_addr[4],
125 &d->kernel_base_addr[5]);
126 d->bus = dfn >> 8U;
127 d->devfn = dfn & 0xff;
128 d->vendid = vend >> 16U;
129 d->devid = vend & 0xffff;
130 if (!filter_out(d))
131 {
132 *last_dev = d;
133 last_dev = &d->next;
134 d->next = NULL;
135 }
136 }
137 fclose(f);
138}
139
140static inline void
141make_proc_pci_name(struct device *d, char *p)
142{
18928b91
MM
143 sprintf(p, "%s/%02x/%02x.%x",
144 pci_dir, d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
98e39e09
MM
145}
146
147static void
148scan_config(void)
149{
150 struct device *d;
151 char name[64];
2f48f637 152 int fd, res;
98e39e09
MM
153 int how_much = (show_hex > 1) ? 256 : 64;
154
155 for(d=first_dev; d; d=d->next)
156 {
157 make_proc_pci_name(d, name);
158 if ((fd = open(name, O_RDONLY)) < 0)
159 {
160 fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
161 exit(1);
162 }
2f48f637
MM
163 res = read(fd, d->config, how_much);
164 if (res < 0)
98e39e09
MM
165 {
166 fprintf(stderr, "lspci: Error reading %s: %m\n", name);
167 exit(1);
168 }
2f48f637
MM
169 if (res != how_much)
170 {
171 fprintf(stderr, "lspci: Only %d bytes of config space available to you\n", res);
172 exit(1);
173 }
98e39e09
MM
174 close(fd);
175 }
176}
177
178static void
179scan_proc(void)
180{
181 scan_dev_list();
182 scan_config();
183}
184
185/* Config space accesses */
186
187static inline byte
188get_conf_byte(struct device *d, unsigned int pos)
189{
190 return d->config[pos];
191}
192
193static word
194get_conf_word(struct device *d, unsigned int pos)
195{
196 return d->config[pos] | (d->config[pos+1] << 8);
197}
198
199static u32
200get_conf_long(struct device *d, unsigned int pos)
201{
202 return d->config[pos] |
203 (d->config[pos+1] << 8) |
204 (d->config[pos+2] << 16) |
205 (d->config[pos+3] << 24);
206}
207
208/* Sorting */
209
210static int
211compare_them(const void *A, const void *B)
212{
213 const struct device *a = *(const struct device **)A;
214 const struct device *b = *(const struct device **)B;
215
216 if (a->bus < b->bus)
217 return -1;
218 if (a->bus > b->bus)
219 return 1;
220 if (a->devfn < b->devfn)
221 return -1;
222 if (a->devfn > b->devfn)
223 return 1;
224 return 0;
225}
226
227static void
228sort_them(void)
229{
230 struct device **index, **h;
231 int cnt;
232 struct device *d;
233
234 cnt = 0;
235 for(d=first_dev; d; d=d->next)
236 cnt++;
237 h = index = alloca(sizeof(struct device *) * cnt);
238 for(d=first_dev; d; d=d->next)
239 *h++ = d;
240 qsort(index, cnt, sizeof(struct device *), compare_them);
241 last_dev = &first_dev;
242 h = index;
243 while (cnt--)
244 {
245 *last_dev = *h;
246 last_dev = &(*h)->next;
247 h++;
248 }
249 *last_dev = NULL;
250}
251
6d0dc0fd 252/* Normal output */
98e39e09
MM
253
254static void
255show_terse(struct device *d)
256{
257 int c;
258
259 printf("%02x:%02x.%x %s: %s",
260 d->bus,
261 PCI_SLOT(d->devfn),
262 PCI_FUNC(d->devfn),
263 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
264 lookup_device_full(d->vendid, d->devid));
265 if (c = get_conf_byte(d, PCI_REVISION_ID))
266 printf(" (rev %02x)", c);
267 if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
268 printf(" (prog-if %02x)", c);
269 putchar('\n');
270}
271
272static void
273show_bases(struct device *d, int cnt)
274{
275 word cmd = get_conf_word(d, PCI_COMMAND);
276 int i;
277
278 for(i=0; i<6; i++)
279 {
280 unsigned long pos;
281 unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
282 if (buscentric_view)
283 pos = flg;
284 else
285 pos = d->kernel_base_addr[i];
286 if (!pos || pos == 0xffffffff)
287 continue;
98e39e09 288 if (flg & PCI_BASE_ADDRESS_SPACE_IO)
f17b962b
MM
289 {
290 if (cmd & PCI_COMMAND_IO)
a3690506
MM
291 {
292 if (verbose > 1)
293 printf("\tRegion %d: ", i);
294 else
295 putchar('\t');
296 printf("I/O ports at %04lx\n", pos & PCI_BASE_ADDRESS_IO_MASK);
297 }
f17b962b
MM
298 }
299 else if (cmd & PCI_COMMAND_MEMORY)
98e39e09
MM
300 {
301 int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
a3690506
MM
302 if (verbose > 1)
303 printf("\tRegion %d: ", i);
304 else
305 putchar('\t');
98e39e09
MM
306 printf("Memory at ");
307 if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
308 {
309 if (i < cnt - 1)
310 {
311 i++;
312 if (!buscentric_view)
313 printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
314 }
315 else
316 printf("????????");
317 }
f17b962b 318 printf("%08lx (%s, %sprefetchable)\n",
98e39e09
MM
319 pos & PCI_BASE_ADDRESS_MEM_MASK,
320 (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
321 (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
322 (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
f17b962b 323 (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
98e39e09
MM
324 }
325 }
326}
327
328static void
329show_htype0(struct device *d)
330{
331 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
332
333 show_bases(d, 6);
334
335 if (rom & 1)
336 {
337 word cmd = get_conf_word(d, PCI_COMMAND);
338 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
339 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
340 }
341}
342
343static void
344show_htype1(struct device *d)
345{
346 u32 io_base = get_conf_byte(d, PCI_IO_BASE);
347 u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
348 u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
349 u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
350 u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
351 u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
352 u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
353 u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
354 u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
355 u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
356 word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
357
358 show_bases(d, 2);
359 printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
360 get_conf_byte(d, PCI_PRIMARY_BUS),
361 get_conf_byte(d, PCI_SECONDARY_BUS),
362 get_conf_byte(d, PCI_SUBORDINATE_BUS),
363 get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
364
365 if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
366 (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
367 printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
368 else
369 {
370 io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
371 io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
372 if (io_type == PCI_IO_RANGE_TYPE_32)
373 {
374 io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
375 io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
376 }
377 if (io_base)
378 printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
379 }
380
381 if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
382 mem_type)
383 printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
384 else if (mem_base)
385 {
386 mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
387 mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
388 printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
389 }
390
391 if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
392 (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
393 printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
394 else if (pref_base)
395 {
396 pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
397 pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
398 if (pref_type == PCI_PREF_RANGE_TYPE_32)
399 printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
400 else
401 printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
402 get_conf_long(d, PCI_PREF_BASE_UPPER32),
403 pref_base,
404 get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
405 pref_limit);
406 }
407
408 if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
409 printf("\tSecondary status: SERR\n");
410
411 if (rom & 1)
412 {
413 word cmd = get_conf_word(d, PCI_COMMAND);
414 printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
415 (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
416 }
417
418 if (verbose > 1)
419 printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
420 (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
421 (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
422 (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
423 (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
424 (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
425 (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
426 (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
427}
428
2f48f637
MM
429static void
430show_htype2(struct device *d)
431{
432}
433
98e39e09
MM
434static void
435show_verbose(struct device *d)
436{
437 word status = get_conf_word(d, PCI_STATUS);
438 word cmd = get_conf_word(d, PCI_COMMAND);
439 word class = get_conf_word(d, PCI_CLASS_DEVICE);
440 byte bist = get_conf_byte(d, PCI_BIST);
441 byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
442 byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
443 byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
444 byte max_lat, min_gnt;
445 byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
446 byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
2f48f637 447 unsigned int irq;
98e39e09
MM
448 word subsys_v, subsys_d;
449
450 show_terse(d);
451
98e39e09
MM
452 switch (htype)
453 {
2f48f637
MM
454 case PCI_HEADER_TYPE_NORMAL:
455 if (class == PCI_CLASS_BRIDGE_PCI)
456 {
457 badhdr:
458 printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
459 return;
460 }
98e39e09
MM
461 max_lat = get_conf_byte(d, PCI_MAX_LAT);
462 min_gnt = get_conf_byte(d, PCI_MIN_GNT);
463 subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
464 subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
465 break;
2f48f637
MM
466 case PCI_HEADER_TYPE_BRIDGE:
467 if (class != PCI_CLASS_BRIDGE_PCI)
468 goto badhdr;
469 irq = int_line = int_pin = min_gnt = max_lat = 0;
470 subsys_v = subsys_d = 0;
471 break;
472 case PCI_HEADER_TYPE_CARDBUS:
473 if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
474 goto badhdr;
98e39e09
MM
475 irq = int_line = int_pin = min_gnt = max_lat = 0;
476 subsys_v = subsys_d = 0;
477 break;
478 default:
479 printf("\t!!! Unknown header type %02x\n", htype);
480 return;
481 }
482
483 if (buscentric_view)
484 irq = int_line;
485 else
486 irq = d->kernel_irq;
487
488 if (verbose > 1)
489 {
490 if (subsys_v)
491 printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
492 printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
493 (cmd & PCI_COMMAND_IO) ? '+' : '-',
494 (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
495 (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
496 (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
497 (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
498 (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
499 (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
500 (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
501 (cmd & PCI_COMMAND_SERR) ? '+' : '-',
502 (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
503 printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
504 (status & PCI_STATUS_66MHZ) ? '+' : '-',
505 (status & PCI_STATUS_UDF) ? '+' : '-',
506 (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
507 (status & PCI_STATUS_PARITY) ? '+' : '-',
508 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
509 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
510 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
511 (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
512 (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
513 (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
514 (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
515 (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
516 if (cmd & PCI_COMMAND_MASTER)
517 {
518 printf("\tLatency: ");
519 if (min_gnt)
520 printf("%d min, ", min_gnt);
521 if (max_lat)
522 printf("%d max, ", max_lat);
523 printf("%d set", latency);
524 if (cache_line)
525 printf(", cache line size %02x", cache_line);
526 putchar('\n');
527 }
528 if (int_pin)
f17b962b 529 printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n", 'A' + int_pin - 1, irq);
98e39e09
MM
530 }
531 else
532 {
533 printf("\tFlags: ");
534 if (cmd & PCI_COMMAND_MASTER)
535 printf("bus master, ");
536 if (cmd & PCI_COMMAND_VGA_PALETTE)
537 printf("VGA palette snoop, ");
538 if (cmd & PCI_COMMAND_WAIT)
539 printf("stepping, ");
540 if (cmd & PCI_COMMAND_FAST_BACK)
541 printf("fast Back2Back, ");
542 if (status & PCI_STATUS_66MHZ)
543 printf("66Mhz, ");
544 if (status & PCI_STATUS_UDF)
545 printf("user-definable features, ");
546 printf("%s devsel",
547 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
548 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
549 ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
550 if (cmd & PCI_COMMAND_MASTER)
551 printf(", latency %d", latency);
552 if (int_pin)
553 if (d->kernel_irq)
f17b962b 554 printf(", IRQ " IRQ_FORMAT, irq);
98e39e09
MM
555 else
556 printf(", IRQ ?");
557 putchar('\n');
558 }
559
560 if (bist & PCI_BIST_CAPABLE)
561 {
562 if (bist & PCI_BIST_START)
563 printf("\tBIST is running\n");
564 else
565 printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
566 }
567
568 switch (htype)
569 {
2f48f637 570 case PCI_HEADER_TYPE_NORMAL:
98e39e09
MM
571 show_htype0(d);
572 break;
2f48f637 573 case PCI_HEADER_TYPE_BRIDGE:
98e39e09
MM
574 show_htype1(d);
575 break;
2f48f637
MM
576 case PCI_HEADER_TYPE_CARDBUS:
577 show_htype2(d);
578 break;
98e39e09
MM
579 }
580}
581
582static void
583show_hex_dump(struct device *d)
584{
585 int i;
586 int limit = (show_hex > 1) ? 256 : 64;
587
588 for(i=0; i<limit; i++)
589 {
590 if (! (i & 15))
591 printf("%02x:", i);
592 printf(" %02x", get_conf_byte(d, i));
593 if ((i & 15) == 15)
594 putchar('\n');
595 }
596}
597
0a33d0ec
MM
598static void
599show_machine(struct device *d)
600{
601 int c;
602
603 if (verbose)
604 {
605 printf("Device:\t%02x:%02x.%x\n", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
606 printf("Class:\t%s\n", lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)));
607 printf("Vendor:\t%s\n", lookup_vendor(d->vendid));
608 printf("Device:\t%s\n", lookup_device(d->vendid, d->devid));
609 if (c = get_conf_byte(d, PCI_REVISION_ID))
610 printf("Rev:\t%02x\n", c);
611 if (c = get_conf_byte(d, PCI_CLASS_PROG))
612 printf("ProgIf:\t%02x\n", c);
613 }
614 else
615 {
616 printf("%02x:%02x.%x ", d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
617 printf("\"%s\" \"%s\" \"%s\"",
618 lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
619 lookup_vendor(d->vendid),
620 lookup_device(d->vendid, d->devid));
621 if (c = get_conf_byte(d, PCI_REVISION_ID))
622 printf(" -r%02x", c);
623 if (c = get_conf_byte(d, PCI_CLASS_PROG))
624 printf(" -p%02x", c);
625 putchar('\n');
626 }
627}
628
98e39e09
MM
629static void
630show(void)
631{
632 struct device *d;
633
634 for(d=first_dev; d; d=d->next)
635 {
0a33d0ec
MM
636 if (machine_readable)
637 show_machine(d);
638 else if (verbose)
98e39e09
MM
639 show_verbose(d);
640 else
641 show_terse(d);
642 if (show_hex)
643 show_hex_dump(d);
644 if (verbose || show_hex)
645 putchar('\n');
646 }
647}
648
6d0dc0fd
MM
649/* Tree output */
650
651struct bridge {
652 struct bridge *chain; /* Single-linked list of bridges */
653 struct bridge *next, *child; /* Tree of bridges */
654 struct bus *first_bus; /* List of busses connected to this bridge */
655 unsigned int primary, secondary, subordinate; /* Bus numbers */
656 struct device *br_dev;
657};
658
659struct bus {
660 unsigned int number;
661 struct bus *sibling;
662 struct device *first_dev, **last_dev;
663};
664
665static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
666
667static struct bus *
668find_bus(struct bridge *b, unsigned int n)
669{
670 struct bus *bus;
671
672 for(bus=b->first_bus; bus; bus=bus->sibling)
673 if (bus->number == n)
674 break;
675 return bus;
676}
677
678static struct bus *
679new_bus(struct bridge *b, unsigned int n)
680{
681 struct bus *bus = xmalloc(sizeof(struct bus));
682
683 bus = xmalloc(sizeof(struct bus));
684 bus->number = n;
685 bus->sibling = b->first_bus;
686 bus->first_dev = NULL;
687 bus->last_dev = &bus->first_dev;
688 b->first_bus = bus;
689 return bus;
690}
691
692static void
693insert_dev(struct device *d, struct bridge *b)
694{
695 struct bus *bus;
696
697 if (! (bus = find_bus(b, d->bus)))
698 {
699 struct bridge *c;
700 for(c=b->child; c; c=c->next)
701 if (c->secondary <= d->bus && d->bus <= c->subordinate)
702 return insert_dev(d, c);
703 bus = new_bus(b, d->bus);
704 }
705 /* Simple insertion at the end _does_ guarantee the correct order as the
706 * original device list was sorted by (bus, devfn) lexicographically
707 * and all devices on the new list have the same bus number.
708 */
709 *bus->last_dev = d;
710 bus->last_dev = &d->next;
711 d->next = NULL;
712}
713
714static void
715grow_tree(void)
716{
717 struct device *d, *d2;
008407bd 718 struct bridge **last_br, *b;
6d0dc0fd
MM
719
720 /* Build list of bridges */
721
008407bd 722 last_br = &host_bridge.chain;
6d0dc0fd
MM
723 for(d=first_dev; d; d=d->next)
724 {
725 word class = get_conf_word(d, PCI_CLASS_DEVICE);
726 if (class == PCI_CLASS_BRIDGE_PCI && (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f) == 1)
727 {
728 b = xmalloc(sizeof(struct bridge));
729 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
730 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
731 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
008407bd
MM
732 *last_br = b;
733 last_br = &b->chain;
6d0dc0fd
MM
734 b->next = b->child = NULL;
735 b->first_bus = NULL;
736 b->br_dev = d;
737 }
738 }
008407bd 739 *last_br = NULL;
6d0dc0fd
MM
740
741 /* Create a bridge tree */
742
008407bd 743 for(b=&host_bridge; b; b=b->chain)
6d0dc0fd
MM
744 {
745 struct bridge *c, *best;
746 best = NULL;
008407bd 747 for(c=&host_bridge; c; c=c->chain)
6d0dc0fd
MM
748 if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
749 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
750 best = c;
751 if (best)
752 {
753 b->next = best->child;
754 best->child = b;
755 }
756 }
757
758 /* Insert secondary bus for each bridge */
759
008407bd 760 for(b=&host_bridge; b; b=b->chain)
6d0dc0fd
MM
761 if (!find_bus(b, b->secondary))
762 new_bus(b, b->secondary);
763
764 /* Create bus structs and link devices */
765
766 for(d=first_dev; d;)
767 {
768 d2 = d->next;
769 insert_dev(d, &host_bridge);
770 d = d2;
771 }
772}
773
774static void
775print_it(byte *line, byte *p)
776{
777 *p++ = '\n';
778 *p = 0;
779 fputs(line, stdout);
780 for(p=line; *p; p++)
008407bd 781 if (*p == '+' || *p == '|')
6d0dc0fd
MM
782 *p = '|';
783 else
784 *p = ' ';
785}
786
787static void show_tree_bridge(struct bridge *, byte *, byte *);
788
789static void
790show_tree_dev(struct device *d, byte *line, byte *p)
791{
792 struct bridge *b;
793
794 p += sprintf(p, "%02x.%x", PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
795 for(b=&host_bridge; b; b=b->chain)
796 if (b->br_dev == d)
797 {
008407bd
MM
798 if (b->secondary == b->subordinate)
799 p += sprintf(p, "-[%02x]-", b->secondary);
800 else
801 p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
6d0dc0fd
MM
802 show_tree_bridge(b, line, p);
803 return;
804 }
18928b91
MM
805 if (verbose)
806 p += sprintf(p, " %s", lookup_device_full(d->vendid, d->devid));
6d0dc0fd
MM
807 print_it(line, p);
808}
809
810static void
811show_tree_bus(struct bus *b, byte *line, byte *p)
812{
813 if (!b->first_dev)
814 print_it(line, p);
815 else if (!b->first_dev->next)
816 {
817 *p++ = '-';
818 *p++ = '-';
819 show_tree_dev(b->first_dev, line, p);
820 }
821 else
822 {
823 struct device *d = b->first_dev;
824 while (d->next)
825 {
826 p[0] = '+';
827 p[1] = '-';
828 show_tree_dev(d, line, p+2);
829 d = d->next;
830 }
831 p[0] = '\\';
832 p[1] = '-';
833 show_tree_dev(d, line, p+2);
834 }
835}
836
837static void
838show_tree_bridge(struct bridge *b, byte *line, byte *p)
839{
840 *p++ = '-';
841 if (!b->first_bus->sibling)
842 {
843 if (b == &host_bridge)
844 p += sprintf(p, "[%02x]-", b->first_bus->number);
845 show_tree_bus(b->first_bus, line, p);
846 }
847 else
848 {
849 struct bus *u = b->first_bus;
850 byte *k;
851
852 while (u->sibling)
853 {
854 k = p + sprintf(p, "+-[%02x]-", u->number);
855 show_tree_bus(u, line, k);
856 u = u->sibling;
857 }
858 k = p + sprintf(p, "\\-[%02x]-", u->number);
859 show_tree_bus(u, line, k);
860 }
861}
862
863static void
864show_forest(void)
865{
866 char line[256];
867
868 grow_tree();
869 show_tree_bridge(&host_bridge, line, line);
870}
871
98e39e09
MM
872/* Main */
873
874int
875main(int argc, char **argv)
876{
877 int i;
878
879 while ((i = getopt(argc, argv, options)) != -1)
880 switch (i)
881 {
882 case 'n':
883 show_numeric_ids = 1;
884 break;
885 case 'v':
886 verbose++;
887 break;
888 case 'b':
889 buscentric_view = 1;
890 break;
891 case 'B':
892 bus_filter = strtol(optarg, NULL, 16);
893 break;
894 case 'S':
895 slot_filter = strtol(optarg, NULL, 16);
896 break;
897 case 'F':
898 func_filter = strtol(optarg, NULL, 16);
899 break;
900 case 'V':
901 vend_filter = strtol(optarg, NULL, 16);
902 break;
903 case 'D':
904 dev_filter = strtol(optarg, NULL, 16);
905 break;
906 case 'x':
907 show_hex++;
908 break;
6d0dc0fd
MM
909 case 't':
910 show_tree++;
911 break;
18928b91
MM
912 case 'i':
913 pci_ids = optarg;
914 break;
915 case 'p':
916 pci_dir = optarg;
917 break;
0a33d0ec
MM
918 case 'm':
919 machine_readable++;
920 break;
98e39e09
MM
921 default:
922 bad:
923 fprintf(stderr, help_msg);
924 return 1;
925 }
926 if (optind < argc)
927 goto bad;
928
929 scan_proc();
930 sort_them();
6d0dc0fd
MM
931 if (show_tree)
932 show_forest();
933 else
934 show();
98e39e09
MM
935
936 return 0;
937}