]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
Add basic support for AGP3 fields.
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.43 2002/12/26 20:24:50 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997--2002 Martin Mares <mj@ucw.cz>
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 <stdarg.h>
15 #include <unistd.h>
16
17 #include "pciutils.h"
18
19 /* Options */
20
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 */
27 static int map_mode;                    /* Bus mapping mode enabled */
28
29 static char options[] = "nvbxs:d:ti:mgM" GENERIC_OPTIONS ;
30
31 static char help_msg[] = "\
32 Usage: lspci [<switches>]\n\
33 \n\
34 -v\t\tBe verbose\n\
35 -n\t\tShow numeric ID's\n\
36 -b\t\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
37 -x\t\tShow hex-dump of the standard portion of config space\n\
38 -xxx\t\tShow hex-dump of the whole config space (dangerous; root only)\n\
39 -s [[<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n\
40 -d [<vendor>]:[<device>]\tShow only selected devices\n\
41 -t\t\tShow bus tree\n\
42 -m\t\tProduce machine-readable output\n\
43 -i <file>\tUse specified ID database instead of %s\n\
44 -M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
45 GENERIC_HELP
46 ;
47
48 /* Communication with libpci */
49
50 static struct pci_access *pacc;
51
52 /* Format strings used for IRQ numbers and memory addresses */
53
54 #ifdef ARCH_SPARC64
55 #define IRQ_FORMAT "%08x"
56 #else
57 #define IRQ_FORMAT "%d"
58 #endif
59
60 #ifdef HAVE_64BIT_ADDRESS
61 #ifdef HAVE_LONG_ADDRESS
62 #define ADDR_FORMAT "%016Lx"
63 #else
64 #define ADDR_FORMAT "%016lx"
65 #endif
66 #else
67 #define ADDR_FORMAT "%08lx"
68 #endif
69
70 #ifdef ARCH_SPARC64
71 #define IO_FORMAT "%016Lx"
72 #elif defined(HAVE_LONG_ADDRESS)
73 #define IO_FORMAT "%04Lx"
74 #else
75 #define IO_FORMAT "%04lx"
76 #endif
77
78 /*
79  *  If we aren't being compiled by GCC, use malloc() instead of alloca().
80  *  This increases our memory footprint, but only slightly since we don't
81  *  use alloca() much.
82  */
83
84 #ifndef __GNUC__
85 #define alloca malloc
86 #endif
87
88 /* Our view of the PCI bus */
89
90 struct device {
91   struct device *next;
92   struct pci_dev *dev;
93   unsigned int config_cnt;
94   byte config[256];
95 };
96
97 static struct device *first_dev;
98
99 static struct device *
100 scan_device(struct pci_dev *p)
101 {
102   int how_much = (show_hex > 2) ? 256 : 64;
103   struct device *d;
104
105   if (!pci_filter_match(&filter, p))
106     return NULL;
107   d = xmalloc(sizeof(struct device));
108   bzero(d, sizeof(*d));
109   d->dev = p;
110   if (!pci_read_block(p, 0, d->config, how_much))
111     die("Unable to read %d bytes of configuration space.", how_much);
112   if (how_much < 128 && (d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
113     {
114       /* For cardbus bridges, we need to fetch 64 bytes more to get the full standard header... */
115       if (!pci_read_block(p, 64, d->config+64, 64))
116         die("Unable to read cardbus bridge extension data.");
117       how_much = 128;
118     }
119   d->config_cnt = how_much;
120   pci_setup_cache(p, d->config, d->config_cnt);
121   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES);
122   return d;
123 }
124
125 static void
126 scan_devices(void)
127 {
128   struct device *d;
129   struct pci_dev *p;
130
131   pci_scan_bus(pacc);
132   for(p=pacc->devices; p; p=p->next)
133     if (d = scan_device(p))
134       {
135         d->next = first_dev;
136         first_dev = d;
137       }
138 }
139
140 static int
141 check_root(void)
142 {
143   static int is_root = -1;
144
145   if (is_root < 0)
146     is_root = !geteuid();
147   return is_root;
148 }
149
150 static int
151 config_fetch(struct device *d, unsigned int pos, unsigned int len)
152 {
153   if (pos + len < d->config_cnt)
154     return 1;
155   if (pacc->method != PCI_ACCESS_DUMP && !check_root())
156     return 0;
157   return pci_read_block(d->dev, pos, d->config + pos, len);
158 }
159
160 /* Config space accesses */
161
162 static inline byte
163 get_conf_byte(struct device *d, unsigned int pos)
164 {
165   return d->config[pos];
166 }
167
168 static word
169 get_conf_word(struct device *d, unsigned int pos)
170 {
171   return d->config[pos] | (d->config[pos+1] << 8);
172 }
173
174 static u32
175 get_conf_long(struct device *d, unsigned int pos)
176 {
177   return d->config[pos] |
178     (d->config[pos+1] << 8) |
179     (d->config[pos+2] << 16) |
180     (d->config[pos+3] << 24);
181 }
182
183 /* Sorting */
184
185 static int
186 compare_them(const void *A, const void *B)
187 {
188   const struct pci_dev *a = (*(const struct device **)A)->dev;
189   const struct pci_dev *b = (*(const struct device **)B)->dev;
190
191   if (a->bus < b->bus)
192     return -1;
193   if (a->bus > b->bus)
194     return 1;
195   if (a->dev < b->dev)
196     return -1;
197   if (a->dev > b->dev)
198     return 1;
199   if (a->func < b->func)
200     return -1;
201   if (a->func > b->func)
202     return 1;
203   return 0;
204 }
205
206 static void
207 sort_them(void)
208 {
209   struct device **index, **h, **last_dev;
210   int cnt;
211   struct device *d;
212
213   cnt = 0;
214   for(d=first_dev; d; d=d->next)
215     cnt++;
216   h = index = alloca(sizeof(struct device *) * cnt);
217   for(d=first_dev; d; d=d->next)
218     *h++ = d;
219   qsort(index, cnt, sizeof(struct device *), compare_them);
220   last_dev = &first_dev;
221   h = index;
222   while (cnt--)
223     {
224       *last_dev = *h;
225       last_dev = &(*h)->next;
226       h++;
227     }
228   *last_dev = NULL;
229 }
230
231 /* Normal output */
232
233 #define FLAG(x,y) ((x & y) ? '+' : '-')
234
235 static void
236 show_terse(struct device *d)
237 {
238   int c;
239   struct pci_dev *p = d->dev;
240   byte classbuf[128], devbuf[128];
241
242   printf("%02x:%02x.%x %s: %s",
243          p->bus,
244          p->dev,
245          p->func,
246          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
247                          PCI_LOOKUP_CLASS,
248                          get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
249          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
250                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
251                          p->vendor_id, p->device_id, 0, 0));
252   if (c = get_conf_byte(d, PCI_REVISION_ID))
253     printf(" (rev %02x)", c);
254   if (verbose)
255     {
256       char *x;
257       c = get_conf_byte(d, PCI_CLASS_PROG);
258       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
259                           PCI_LOOKUP_PROGIF,
260                           get_conf_word(d, PCI_CLASS_DEVICE), c, 0, 0);
261       if (c || x)
262         {
263           printf(" (prog-if %02x", c);
264           if (x)
265             printf(" [%s]", x);
266           putchar(')');
267         }
268     }
269   putchar('\n');
270 }
271
272 static void
273 show_size(pciaddr_t x)
274 {
275   if (!x)
276     return;
277   printf(" [size=");
278   if (x < 1024)
279     printf("%d", (int) x);
280   else if (x < 1048576)
281     printf("%dK", (int)(x / 1024));
282   else if (x < 0x80000000)
283     printf("%dM", (int)(x / 1048576));
284   else
285     printf(ADDR_FORMAT, x);
286   putchar(']');
287 }
288
289 static void
290 show_bases(struct device *d, int cnt)
291 {
292   struct pci_dev *p = d->dev;
293   word cmd = get_conf_word(d, PCI_COMMAND);
294   int i;
295
296   for(i=0; i<cnt; i++)
297     {
298       pciaddr_t pos = p->base_addr[i];
299       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
300       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
301       if (flg == 0xffffffff)
302         flg = 0;
303       if (!pos && !flg && !len)
304         continue;
305       if (verbose > 1)
306         printf("\tRegion %d: ", i);
307       else
308         putchar('\t');
309       if (pos && !flg)                  /* Reported by the OS, but not by the device */
310         {
311           printf("[virtual] ");
312           flg = pos;
313         }
314       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
315         {
316           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
317           printf("I/O ports at ");
318           if (a)
319             printf(IO_FORMAT, a);
320           else if (flg & PCI_BASE_ADDRESS_IO_MASK)
321             printf("<ignored>");
322           else
323             printf("<unassigned>");
324           if (!(cmd & PCI_COMMAND_IO))
325             printf(" [disabled]");
326         }
327       else
328         {
329           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
330           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
331           int done = 0;
332           u32 z = 0;
333
334           printf("Memory at ");
335           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
336             {
337               if (i >= cnt - 1)
338                 {
339                   printf("<invalid-64bit-slot>");
340                   done = 1;
341                 }
342               else
343                 {
344                   i++;
345                   z = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
346                   if (buscentric_view)
347                     {
348                       if (a || z)
349                         printf("%08x" ADDR_FORMAT, z, a);
350                       else
351                         printf("<unassigned>");
352                       done = 1;
353                     }
354                 }
355             }
356           if (!done)
357             {
358               if (a)
359                 printf(ADDR_FORMAT, a);
360               else
361                 printf(((flg & PCI_BASE_ADDRESS_MEM_MASK) || z) ? "<ignored>" : "<unassigned>");
362             }
363           printf(" (%s, %sprefetchable)",
364                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
365                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
366                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
367                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
368           if (!(cmd & PCI_COMMAND_MEMORY))
369             printf(" [disabled]");
370         }
371       show_size(len);
372       putchar('\n');
373     }
374 }
375
376 static void
377 show_pm(struct device *d, int where, int cap)
378 {
379   int t, b;
380   static int pm_aux_current[8] = { 0, 55, 100, 160, 220, 270, 320, 375 };
381
382   printf("Power Management version %d\n", cap & PCI_PM_CAP_VER_MASK);
383   if (verbose < 2)
384     return;
385   printf("\t\tFlags: PMEClk%c DSI%c D1%c D2%c AuxCurrent=%dmA PME(D0%c,D1%c,D2%c,D3hot%c,D3cold%c)\n",
386          FLAG(cap, PCI_PM_CAP_PME_CLOCK),
387          FLAG(cap, PCI_PM_CAP_DSI),
388          FLAG(cap, PCI_PM_CAP_D1),
389          FLAG(cap, PCI_PM_CAP_D2),
390          pm_aux_current[(cap >> 6) & 7],
391          FLAG(cap, PCI_PM_CAP_PME_D0),
392          FLAG(cap, PCI_PM_CAP_PME_D1),
393          FLAG(cap, PCI_PM_CAP_PME_D2),
394          FLAG(cap, PCI_PM_CAP_PME_D3_HOT),
395          FLAG(cap, PCI_PM_CAP_PME_D3_COLD));
396   config_fetch(d, where + PCI_PM_CTRL, PCI_PM_SIZEOF - PCI_PM_CTRL);
397   t = get_conf_word(d, where + PCI_PM_CTRL);
398   printf("\t\tStatus: D%d PME-Enable%c DSel=%d DScale=%d PME%c\n",
399          t & PCI_PM_CTRL_STATE_MASK,
400          FLAG(t, PCI_PM_CTRL_PME_ENABLE),
401          (t & PCI_PM_CTRL_DATA_SEL_MASK) >> 9,
402          (t & PCI_PM_CTRL_DATA_SCALE_MASK) >> 13,
403          FLAG(t, PCI_PM_CTRL_PME_STATUS));
404   b = get_conf_byte(d, where + PCI_PM_PPB_EXTENSIONS);
405   if (b)
406     printf("\t\tBridge: PM%c B3%c\n",
407            FLAG(t, PCI_PM_BPCC_ENABLE),
408            FLAG(~t, PCI_PM_PPB_B2_B3));
409 }
410
411 static void
412 format_agp_rate(int rate, char *buf, int agp3)
413 {
414   char *c = buf;
415   int i;
416
417   for(i=0; i<=2; i++)
418     if (rate & (1 << i))
419       {
420         if (c != buf)
421           *c++ = ',';
422         *c++ = 'x';
423         *c++ = '0' + (1 << (i + 2*agp3));
424       }
425   if (c != buf)
426     *c = 0;
427   else
428     strcpy(buf, "<none>");
429 }
430
431 static void
432 show_agp(struct device *d, int where, int cap)
433 {
434   u32 t;
435   char rate[8];
436   int ver, rev;
437   int agp3 = 0;
438
439   ver = (cap >> 4) & 0x0f;
440   rev = cap & 0x0f;
441   printf("AGP version %x.%x\n", ver, rev);
442   if (verbose < 2)
443     return;
444   config_fetch(d, where + PCI_AGP_STATUS, PCI_AGP_SIZEOF - PCI_AGP_STATUS);
445   t = get_conf_long(d, where + PCI_AGP_STATUS);
446   if (ver >= 3 && (t & PCI_AGP_STATUS_AGP3))
447     agp3 = 1;
448   format_agp_rate(t & 7, rate, agp3);
449   printf("\t\tStatus: RQ=%d Iso%c ArqSz=%d Cal=%d SBA%c ITACoh%c GART64%c HTrans%c 64bit%c FW%c AGP3%c Rate=%s\n",
450          ((t & PCI_AGP_STATUS_RQ_MASK) >> 24U) + 1,
451          FLAG(t, PCI_AGP_STATUS_ISOCH),
452          ((t & PCI_AGP_STATUS_ARQSZ_MASK) >> 13),
453          ((t & PCI_AGP_STATUS_CAL_MASK) >> 10),
454          FLAG(t, PCI_AGP_STATUS_SBA),
455          FLAG(t, PCI_AGP_STATUS_ITA_COH),
456          FLAG(t, PCI_AGP_STATUS_GART64),
457          FLAG(t, PCI_AGP_STATUS_HTRANS),
458          FLAG(t, PCI_AGP_STATUS_64BIT),
459          FLAG(t, PCI_AGP_STATUS_FW),
460          FLAG(t, PCI_AGP_STATUS_AGP3),
461          rate);
462   t = get_conf_long(d, where + PCI_AGP_COMMAND);
463   format_agp_rate(t & 7, rate, agp3);
464   printf("\t\tCommand: RQ=%d ArqSz=%d Cal=%d SBA%c AGP%c GART64%c 64bit%c FW%c Rate=%s\n",
465          ((t & PCI_AGP_COMMAND_RQ_MASK) >> 24U) + 1,
466          ((t & PCI_AGP_COMMAND_ARQSZ_MASK) >> 13),
467          ((t & PCI_AGP_COMMAND_CAL_MASK) >> 10),
468          FLAG(t, PCI_AGP_COMMAND_SBA),
469          FLAG(t, PCI_AGP_COMMAND_AGP),
470          FLAG(t, PCI_AGP_COMMAND_GART64),
471          FLAG(t, PCI_AGP_COMMAND_64BIT),
472          FLAG(t, PCI_AGP_COMMAND_FW),
473          rate);
474 }
475
476 static void
477 show_pcix_nobridge(struct device *d, int where)
478 {
479   u16 command = get_conf_word(d, where + PCI_PCIX_COMMAND);
480   u32 status = get_conf_long(d, where + PCI_PCIX_STATUS);
481   printf("PCI-X non-bridge device.\n");
482   if (verbose < 2)
483     return;
484   printf("\t\tCommand: DPERE%c ERO%c RBC=%d OST=%d\n",
485          FLAG(command, PCI_PCIX_COMMAND_DPERE),
486          FLAG(command, PCI_PCIX_COMMAND_ERO),
487          ((command & PCI_PCIX_COMMAND_MAX_MEM_READ_BYTE_COUNT) >> 2U),
488          ((command & PCI_PCIX_COMMAND_MAX_OUTSTANDING_SPLIT_TRANS) >> 4U));
489   printf("\t\tStatus: Bus=%u Dev=%u Func=%u 64bit%c 133MHz%c SCD%c USC%c, DC=%s, DMMRBC=%u, DMOST=%u, DMCRS=%u, RSCEM%c",
490          ((status >> 8) & 0xffU), // bus
491          ((status >> 3) & 0x1fU), // dev
492          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
493          FLAG(status, PCI_PCIX_STATUS_64BIT),
494          FLAG(status, PCI_PCIX_STATUS_133MHZ),
495          FLAG(status, PCI_PCIX_STATUS_SC_DISCARDED),
496          FLAG(status, PCI_PCIX_STATUS_UNEXPECTED_SC),
497          ((status & PCI_PCIX_STATUS_DEVICE_COMPLEXITY) ? "bridge" : "simple"),
498          ((status >> 21) & 3U),
499          ((status >> 23) & 7U),
500          ((status >> 26) & 7U),
501          FLAG(status, PCI_PCIX_STATUS_RCVD_SC_ERR_MESS));
502 }
503
504 static void
505 show_pcix_bridge(struct device *d, int where)
506 {
507   u16 secstatus;
508   u32 status, upstcr, downstcr;
509   printf("PCI-X bridge device.\n");
510   if (verbose < 2)
511     return;
512   secstatus = get_conf_word(d, where + PCI_PCIX_BRIDGE_SEC_STATUS);
513   printf("\t\tSecondary Status: 64bit%c, 133MHz%c, SCD%c, USC%c, SCO%c, SRD%c Freq=%d\n",
514          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_64BIT),
515          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_133MHZ),
516          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_DISCARDED),
517          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_UNEXPECTED_SC),
518          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SC_OVERRUN),
519          FLAG(secstatus, PCI_PCIX_BRIDGE_SEC_STATUS_SPLIT_REQUEST_DELAYED),
520          ((secstatus >> 6) & 7));
521   status = get_conf_long(d, where + PCI_PCIX_BRIDGE_STATUS);
522   printf("\t\tStatus: Bus=%u Dev=%u Func=%u 64bit%c 133MHz%c SCD%c USC%c, SCO%c, SRD%c\n", 
523          ((status >> 8) & 0xff), // bus
524          ((status >> 3) & 0x1f), // dev
525          (status & PCI_PCIX_BRIDGE_STATUS_FUNCTION), // function
526          FLAG(status, PCI_PCIX_BRIDGE_STATUS_64BIT),
527          FLAG(status, PCI_PCIX_BRIDGE_STATUS_133MHZ),
528          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_DISCARDED),
529          FLAG(status, PCI_PCIX_BRIDGE_STATUS_UNEXPECTED_SC),
530          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SC_OVERRUN),
531          FLAG(status, PCI_PCIX_BRIDGE_STATUS_SPLIT_REQUEST_DELAYED));
532   upstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_UPSTREAM_SPLIT_TRANS_CTRL);
533   printf("\t\t: Upstream: Capacity=%u, Commitment Limit=%u\n",
534          (upstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
535          (upstcr >> 16) & 0xffff);
536   downstcr = get_conf_long(d, where + PCI_PCIX_BRIDGE_DOWNSTREAM_SPLIT_TRANS_CTRL);
537   printf("\t\t: Downstream: Capacity=%u, Commitment Limit=%u\n",
538          (downstcr & PCI_PCIX_BRIDGE_STR_CAPACITY),
539          (downstcr >> 16) & 0xffff);
540 }
541
542 static void
543 show_pcix(struct device *d, int where)
544 {
545   switch (d->dev->hdrtype)
546     {
547     case PCI_HEADER_TYPE_NORMAL:
548       show_pcix_nobridge(d, where);
549       break;
550     case PCI_HEADER_TYPE_BRIDGE:
551       show_pcix_bridge(d, where);
552       break;
553     }
554 }
555
556 static void
557 show_rom(struct device *d)
558 {
559   struct pci_dev *p = d->dev;
560   pciaddr_t rom = p->rom_base_addr;
561   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
562
563   if (!rom && !len)
564     return;
565   printf("\tExpansion ROM at ");
566   if (rom & PCI_ROM_ADDRESS_MASK)
567     printf(ADDR_FORMAT, rom & PCI_ROM_ADDRESS_MASK);
568   else
569     printf("<unassigned>");
570   if (!(rom & PCI_ROM_ADDRESS_ENABLE))
571     printf(" [disabled]");
572   show_size(len);
573   putchar('\n');
574 }
575
576 static void
577 show_msi(struct device *d, int where, int cap)
578 {
579   int is64;
580   u32 t;
581   u16 w;
582
583   printf("Message Signalled Interrupts: 64bit%c Queue=%d/%d Enable%c\n",
584          FLAG(cap, PCI_MSI_FLAGS_64BIT),
585          (cap & PCI_MSI_FLAGS_QSIZE) >> 4,
586          (cap & PCI_MSI_FLAGS_QMASK) >> 1,
587          FLAG(cap, PCI_MSI_FLAGS_ENABLE));
588   if (verbose < 2)
589     return;
590   is64 = cap & PCI_MSI_FLAGS_64BIT;
591   config_fetch(d, where + PCI_MSI_ADDRESS_LO, (is64 ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32) + 2 - PCI_MSI_ADDRESS_LO);
592   printf("\t\tAddress: ");
593   if (is64)
594     {
595       t = get_conf_long(d, where + PCI_MSI_ADDRESS_HI);
596       w = get_conf_word(d, where + PCI_MSI_DATA_64);
597       printf("%08x", t);
598     }
599   else
600     w = get_conf_word(d, where + PCI_MSI_DATA_32);
601   t = get_conf_long(d, where + PCI_MSI_ADDRESS_LO);
602   printf("%08x  Data: %04x\n", t, w);
603 }
604
605 static void
606 show_slotid(int cap)
607 {
608   int esr = cap & 0xff;
609   int chs = cap >> 8;
610
611   printf("Slot ID: %d slots, First%c, chassis %02x\n",
612          esr & PCI_SID_ESR_NSLOTS,
613          FLAG(esr, PCI_SID_ESR_FIC),
614          chs);
615 }
616
617 static void
618 show_caps(struct device *d)
619 {
620   if (get_conf_word(d, PCI_STATUS) & PCI_STATUS_CAP_LIST)
621     {
622       int where = get_conf_byte(d, PCI_CAPABILITY_LIST) & ~3;
623       while (where)
624         {
625           int id, next, cap;
626           printf("\tCapabilities: ");
627           if (!config_fetch(d, where, 4))
628             {
629               puts("<available only to root>");
630               break;
631             }
632           id = get_conf_byte(d, where + PCI_CAP_LIST_ID);
633           next = get_conf_byte(d, where + PCI_CAP_LIST_NEXT) & ~3;
634           cap = get_conf_word(d, where + PCI_CAP_FLAGS);
635           printf("[%02x] ", where);
636           if (id == 0xff)
637             {
638               printf("<chain broken>\n");
639               break;
640             }
641           switch (id)
642             {
643             case PCI_CAP_ID_PM:
644               show_pm(d, where, cap);
645               break;
646             case PCI_CAP_ID_AGP:
647               show_agp(d, where, cap);
648               break;
649             case PCI_CAP_ID_VPD:
650               printf("Vital Product Data\n");
651               break;
652             case PCI_CAP_ID_SLOTID:
653               show_slotid(cap);
654               break;
655             case PCI_CAP_ID_MSI:
656               show_msi(d, where, cap);
657               break;
658             case PCI_CAP_ID_PCIX:
659               show_pcix(d, where);
660               break;
661             default:
662               printf("#%02x [%04x]\n", id, cap);
663             }
664           where = next;
665         }
666     }
667 }
668
669 static void
670 show_htype0(struct device *d)
671 {
672   show_bases(d, 6);
673   show_rom(d);
674   show_caps(d);
675 }
676
677 static void
678 show_htype1(struct device *d)
679 {
680   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
681   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
682   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
683   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
684   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
685   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
686   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
687   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
688   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
689   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
690   int verb = verbose > 2;
691
692   show_bases(d, 2);
693   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
694          get_conf_byte(d, PCI_PRIMARY_BUS),
695          get_conf_byte(d, PCI_SECONDARY_BUS),
696          get_conf_byte(d, PCI_SUBORDINATE_BUS),
697          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
698
699   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
700       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
701     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
702   else
703     {
704       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
705       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
706       if (io_type == PCI_IO_RANGE_TYPE_32)
707         {
708           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
709           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
710         }
711       if (io_base <= io_limit || verb)
712         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
713     }
714
715   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
716       mem_type)
717     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
718   else
719     {
720       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
721       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
722       if (mem_base <= mem_limit || verb)
723         printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
724     }
725
726   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
727       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
728     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
729   else
730     {
731       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
732       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
733       if (pref_base <= pref_limit || verb)
734         {
735           if (pref_type == PCI_PREF_RANGE_TYPE_32)
736             printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit + 0xfffff);
737           else
738             printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
739                    get_conf_long(d, PCI_PREF_BASE_UPPER32),
740                    pref_base,
741                    get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
742                    pref_limit);
743         }
744     }
745
746   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
747     printf("\tSecondary status: SERR\n");
748
749   show_rom(d);
750
751   if (verbose > 1)
752     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
753            FLAG(brc, PCI_BRIDGE_CTL_PARITY),
754            FLAG(brc, PCI_BRIDGE_CTL_SERR),
755            FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
756            FLAG(brc, PCI_BRIDGE_CTL_VGA),
757            FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
758            FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
759            FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
760
761   show_caps(d);
762 }
763
764 static void
765 show_htype2(struct device *d)
766 {
767   int i;
768   word cmd = get_conf_word(d, PCI_COMMAND);
769   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
770   word exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
771   int verb = verbose > 2;
772
773   show_bases(d, 1);
774   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
775          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
776          get_conf_byte(d, PCI_CB_CARD_BUS),
777          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
778          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
779   for(i=0; i<2; i++)
780     {
781       int p = 8*i;
782       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
783       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
784       if (limit > base || verb)
785         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
786                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
787                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
788     }
789   for(i=0; i<2; i++)
790     {
791       int p = 8*i;
792       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
793       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
794       if (!(base & PCI_IO_RANGE_TYPE_32))
795         {
796           base &= 0xffff;
797           limit &= 0xffff;
798         }
799       base &= PCI_CB_IO_RANGE_MASK;
800       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
801       if (base <= limit || verb)
802         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
803                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
804     }
805
806   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
807     printf("\tSecondary status: SERR\n");
808   if (verbose > 1)
809     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
810            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
811            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
812            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
813            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
814            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
815            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
816            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
817            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
818   if (exca)
819     printf("\t16-bit legacy interface ports at %04x\n", exca);
820 }
821
822 static void
823 show_verbose(struct device *d)
824 {
825   struct pci_dev *p = d->dev;
826   word status = get_conf_word(d, PCI_STATUS);
827   word cmd = get_conf_word(d, PCI_COMMAND);
828   word class = get_conf_word(d, PCI_CLASS_DEVICE);
829   byte bist = get_conf_byte(d, PCI_BIST);
830   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
831   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
832   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
833   byte max_lat, min_gnt;
834   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
835   unsigned int irq = p->irq;
836   word subsys_v, subsys_d;
837   char ssnamebuf[256];
838
839   show_terse(d);
840
841   switch (htype)
842     {
843     case PCI_HEADER_TYPE_NORMAL:
844       if (class == PCI_CLASS_BRIDGE_PCI)
845         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
846       max_lat = get_conf_byte(d, PCI_MAX_LAT);
847       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
848       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
849       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
850       break;
851     case PCI_HEADER_TYPE_BRIDGE:
852       if (class != PCI_CLASS_BRIDGE_PCI)
853         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
854       irq = int_pin = min_gnt = max_lat = 0;
855       subsys_v = subsys_d = 0;
856       break;
857     case PCI_HEADER_TYPE_CARDBUS:
858       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
859         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
860       min_gnt = max_lat = 0;
861       subsys_v = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
862       subsys_d = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
863       break;
864     default:
865       printf("\t!!! Unknown header type %02x\n", htype);
866       return;
867     }
868
869   if (subsys_v && subsys_v != 0xffff)
870     printf("\tSubsystem: %s\n",
871            pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
872                            PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
873                            p->vendor_id, p->device_id, subsys_v, subsys_d));
874
875   if (verbose > 1)
876     {
877       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
878              FLAG(cmd, PCI_COMMAND_IO),
879              FLAG(cmd, PCI_COMMAND_MEMORY),
880              FLAG(cmd, PCI_COMMAND_MASTER),
881              FLAG(cmd, PCI_COMMAND_SPECIAL),
882              FLAG(cmd, PCI_COMMAND_INVALIDATE),
883              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
884              FLAG(cmd, PCI_COMMAND_PARITY),
885              FLAG(cmd, PCI_COMMAND_WAIT),
886              FLAG(cmd, PCI_COMMAND_SERR),
887              FLAG(cmd, PCI_COMMAND_FAST_BACK));
888       printf("\tStatus: Cap%c 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
889              FLAG(status, PCI_STATUS_CAP_LIST),
890              FLAG(status, PCI_STATUS_66MHZ),
891              FLAG(status, PCI_STATUS_UDF),
892              FLAG(status, PCI_STATUS_FAST_BACK),
893              FLAG(status, PCI_STATUS_PARITY),
894              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
895              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
896              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
897              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
898              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
899              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
900              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
901              FLAG(status, PCI_STATUS_DETECTED_PARITY));
902       if (cmd & PCI_COMMAND_MASTER)
903         {
904           printf("\tLatency: %d", latency);
905           if (min_gnt || max_lat)
906             {
907               printf(" (");
908               if (min_gnt)
909                 printf("%dns min", min_gnt*250);
910               if (min_gnt && max_lat)
911                 printf(", ");
912               if (max_lat)
913                 printf("%dns max", max_lat*250);
914               putchar(')');
915             }
916           if (cache_line)
917             printf(", cache line size %02x", cache_line);
918           putchar('\n');
919         }
920       if (int_pin || irq)
921         printf("\tInterrupt: pin %c routed to IRQ " IRQ_FORMAT "\n",
922                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
923     }
924   else
925     {
926       printf("\tFlags: ");
927       if (cmd & PCI_COMMAND_MASTER)
928         printf("bus master, ");
929       if (cmd & PCI_COMMAND_VGA_PALETTE)
930         printf("VGA palette snoop, ");
931       if (cmd & PCI_COMMAND_WAIT)
932         printf("stepping, ");
933       if (cmd & PCI_COMMAND_FAST_BACK)
934         printf("fast Back2Back, ");
935       if (status & PCI_STATUS_66MHZ)
936         printf("66Mhz, ");
937       if (status & PCI_STATUS_UDF)
938         printf("user-definable features, ");
939       printf("%s devsel",
940              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
941              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
942              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
943       if (cmd & PCI_COMMAND_MASTER)
944         printf(", latency %d", latency);
945       if (irq)
946         printf(", IRQ " IRQ_FORMAT, irq);
947       putchar('\n');
948     }
949
950   if (bist & PCI_BIST_CAPABLE)
951     {
952       if (bist & PCI_BIST_START)
953         printf("\tBIST is running\n");
954       else
955         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
956     }
957
958   switch (htype)
959     {
960     case PCI_HEADER_TYPE_NORMAL:
961       show_htype0(d);
962       break;
963     case PCI_HEADER_TYPE_BRIDGE:
964       show_htype1(d);
965       break;
966     case PCI_HEADER_TYPE_CARDBUS:
967       show_htype2(d);
968       break;
969     }
970 }
971
972 static void
973 show_hex_dump(struct device *d)
974 {
975   unsigned int i;
976
977   for(i=0; i<d->config_cnt; i++)
978     {
979       if (! (i & 15))
980         printf("%02x:", i);
981       printf(" %02x", get_conf_byte(d, i));
982       if ((i & 15) == 15)
983         putchar('\n');
984     }
985 }
986
987 static void
988 show_machine(struct device *d)
989 {
990   struct pci_dev *p = d->dev;
991   int c;
992   word sv_id=0, sd_id=0;
993   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
994
995   switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
996     {
997     case PCI_HEADER_TYPE_NORMAL:
998       sv_id = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
999       sd_id = get_conf_word(d, PCI_SUBSYSTEM_ID);
1000       break;
1001     case PCI_HEADER_TYPE_CARDBUS:
1002       sv_id = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
1003       sd_id = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
1004       break;
1005     }
1006
1007   if (verbose)
1008     {
1009       printf("Device:\t%02x:%02x.%x\n", p->bus, p->dev, p->func);
1010       printf("Class:\t%s\n",
1011              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0));
1012       printf("Vendor:\t%s\n",
1013              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, 0, 0));
1014       printf("Device:\t%s\n",
1015              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, 0, 0));
1016       if (sv_id && sv_id != 0xffff)
1017         {
1018           printf("SVendor:\t%s\n",
1019                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id));
1020           printf("SDevice:\t%s\n",
1021                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1022         }
1023       if (c = get_conf_byte(d, PCI_REVISION_ID))
1024         printf("Rev:\t%02x\n", c);
1025       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1026         printf("ProgIf:\t%02x\n", c);
1027     }
1028   else
1029     {
1030       printf("%02x:%02x.%x ", p->bus, p->dev, p->func);
1031       printf("\"%s\" \"%s\" \"%s\"",
1032              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS,
1033                              get_conf_word(d, PCI_CLASS_DEVICE), 0, 0, 0),
1034              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR,
1035                              p->vendor_id, p->device_id, 0, 0),
1036              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE,
1037                              p->vendor_id, p->device_id, 0, 0));
1038       if (c = get_conf_byte(d, PCI_REVISION_ID))
1039         printf(" -r%02x", c);
1040       if (c = get_conf_byte(d, PCI_CLASS_PROG))
1041         printf(" -p%02x", c);
1042       if (sv_id && sv_id != 0xffff)
1043         printf(" \"%s\" \"%s\"",
1044                pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id, sv_id, sd_id),
1045                pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
1046       else
1047         printf(" \"\" \"\"");
1048       putchar('\n');
1049     }
1050 }
1051
1052 static void
1053 show_device(struct device *d)
1054 {
1055   if (machine_readable)
1056     show_machine(d);
1057   else if (verbose)
1058     show_verbose(d);
1059   else
1060     show_terse(d);
1061   if (show_hex)
1062     show_hex_dump(d);
1063   if (verbose || show_hex)
1064     putchar('\n');
1065 }
1066
1067 static void
1068 show(void)
1069 {
1070   struct device *d;
1071
1072   for(d=first_dev; d; d=d->next)
1073     show_device(d);
1074 }
1075
1076 /* Tree output */
1077
1078 struct bridge {
1079   struct bridge *chain;                 /* Single-linked list of bridges */
1080   struct bridge *next, *child;          /* Tree of bridges */
1081   struct bus *first_bus;                /* List of busses connected to this bridge */
1082   unsigned int primary, secondary, subordinate; /* Bus numbers */
1083   struct device *br_dev;
1084 };
1085
1086 struct bus {
1087   unsigned int number;
1088   struct bus *sibling;
1089   struct device *first_dev, **last_dev;
1090 };
1091
1092 static struct bridge host_bridge = { NULL, NULL, NULL, NULL, ~0, 0, ~0, NULL };
1093
1094 static struct bus *
1095 find_bus(struct bridge *b, unsigned int n)
1096 {
1097   struct bus *bus;
1098
1099   for(bus=b->first_bus; bus; bus=bus->sibling)
1100     if (bus->number == n)
1101       break;
1102   return bus;
1103 }
1104
1105 static struct bus *
1106 new_bus(struct bridge *b, unsigned int n)
1107 {
1108   struct bus *bus = xmalloc(sizeof(struct bus));
1109
1110   bus = xmalloc(sizeof(struct bus));
1111   bus->number = n;
1112   bus->sibling = b->first_bus;
1113   bus->first_dev = NULL;
1114   bus->last_dev = &bus->first_dev;
1115   b->first_bus = bus;
1116   return bus;
1117 }
1118
1119 static void
1120 insert_dev(struct device *d, struct bridge *b)
1121 {
1122   struct pci_dev *p = d->dev;
1123   struct bus *bus;
1124
1125   if (! (bus = find_bus(b, p->bus)))
1126     {
1127       struct bridge *c;
1128       for(c=b->child; c; c=c->next)
1129         if (c->secondary <= p->bus && p->bus <= c->subordinate)
1130           {
1131             insert_dev(d, c);
1132             return;
1133           }
1134       bus = new_bus(b, p->bus);
1135     }
1136   /* Simple insertion at the end _does_ guarantee the correct order as the
1137    * original device list was sorted by (bus, devfn) lexicographically
1138    * and all devices on the new list have the same bus number.
1139    */
1140   *bus->last_dev = d;
1141   bus->last_dev = &d->next;
1142   d->next = NULL;
1143 }
1144
1145 static void
1146 grow_tree(void)
1147 {
1148   struct device *d, *d2;
1149   struct bridge **last_br, *b;
1150
1151   /* Build list of bridges */
1152
1153   last_br = &host_bridge.chain;
1154   for(d=first_dev; d; d=d->next)
1155     {
1156       word class = get_conf_word(d, PCI_CLASS_DEVICE);
1157       byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
1158       if (class == PCI_CLASS_BRIDGE_PCI &&
1159           (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
1160         {
1161           b = xmalloc(sizeof(struct bridge));
1162           if (ht == PCI_HEADER_TYPE_BRIDGE)
1163             {
1164               b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
1165               b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
1166               b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
1167             }
1168           else
1169             {
1170               b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
1171               b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
1172               b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
1173             }
1174           *last_br = b;
1175           last_br = &b->chain;
1176           b->next = b->child = NULL;
1177           b->first_bus = NULL;
1178           b->br_dev = d;
1179         }
1180     }
1181   *last_br = NULL;
1182
1183   /* Create a bridge tree */
1184
1185   for(b=&host_bridge; b; b=b->chain)
1186     {
1187       struct bridge *c, *best;
1188       best = NULL;
1189       for(c=&host_bridge; c; c=c->chain)
1190         if (c != b && b->primary >= c->secondary && b->primary <= c->subordinate &&
1191             (!best || best->subordinate - best->primary > c->subordinate - c->primary))
1192           best = c;
1193       if (best)
1194         {
1195           b->next = best->child;
1196           best->child = b;
1197         }
1198     }
1199
1200   /* Insert secondary bus for each bridge */
1201
1202   for(b=&host_bridge; b; b=b->chain)
1203     if (!find_bus(b, b->secondary))
1204       new_bus(b, b->secondary);
1205
1206   /* Create bus structs and link devices */
1207
1208   for(d=first_dev; d;)
1209     {
1210       d2 = d->next;
1211       insert_dev(d, &host_bridge);
1212       d = d2;
1213     }
1214 }
1215
1216 static void
1217 print_it(byte *line, byte *p)
1218 {
1219   *p++ = '\n';
1220   *p = 0;
1221   fputs(line, stdout);
1222   for(p=line; *p; p++)
1223     if (*p == '+' || *p == '|')
1224       *p = '|';
1225     else
1226       *p = ' ';
1227 }
1228
1229 static void show_tree_bridge(struct bridge *, byte *, byte *);
1230
1231 static void
1232 show_tree_dev(struct device *d, byte *line, byte *p)
1233 {
1234   struct pci_dev *q = d->dev;
1235   struct bridge *b;
1236   char namebuf[256];
1237
1238   p += sprintf(p, "%02x.%x", q->dev, q->func);
1239   for(b=&host_bridge; b; b=b->chain)
1240     if (b->br_dev == d)
1241       {
1242         if (b->secondary == b->subordinate)
1243           p += sprintf(p, "-[%02x]-", b->secondary);
1244         else
1245           p += sprintf(p, "-[%02x-%02x]-", b->secondary, b->subordinate);
1246         show_tree_bridge(b, line, p);
1247         return;
1248       }
1249   if (verbose)
1250     p += sprintf(p, "  %s",
1251                  pci_lookup_name(pacc, namebuf, sizeof(namebuf),
1252                                  PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
1253                                  q->vendor_id, q->device_id, 0, 0));
1254   print_it(line, p);
1255 }
1256
1257 static void
1258 show_tree_bus(struct bus *b, byte *line, byte *p)
1259 {
1260   if (!b->first_dev)
1261     print_it(line, p);
1262   else if (!b->first_dev->next)
1263     {
1264       *p++ = '-';
1265       *p++ = '-';
1266       show_tree_dev(b->first_dev, line, p);
1267     }
1268   else
1269     {
1270       struct device *d = b->first_dev;
1271       while (d->next)
1272         {
1273           p[0] = '+';
1274           p[1] = '-';
1275           show_tree_dev(d, line, p+2);
1276           d = d->next;
1277         }
1278       p[0] = '\\';
1279       p[1] = '-';
1280       show_tree_dev(d, line, p+2);
1281     }
1282 }
1283
1284 static void
1285 show_tree_bridge(struct bridge *b, byte *line, byte *p)
1286 {
1287   *p++ = '-';
1288   if (!b->first_bus->sibling)
1289     {
1290       if (b == &host_bridge)
1291         p += sprintf(p, "[%02x]-", b->first_bus->number);
1292       show_tree_bus(b->first_bus, line, p);
1293     }
1294   else
1295     {
1296       struct bus *u = b->first_bus;
1297       byte *k;
1298
1299       while (u->sibling)
1300         {
1301           k = p + sprintf(p, "+-[%02x]-", u->number);
1302           show_tree_bus(u, line, k);
1303           u = u->sibling;
1304         }
1305       k = p + sprintf(p, "\\-[%02x]-", u->number);
1306       show_tree_bus(u, line, k);
1307     }
1308 }
1309
1310 static void
1311 show_forest(void)
1312 {
1313   char line[256];
1314
1315   grow_tree();
1316   show_tree_bridge(&host_bridge, line, line);
1317 }
1318
1319 /* Bus mapping mode */
1320
1321 struct bus_bridge {
1322   struct bus_bridge *next;
1323   byte this, dev, func, first, last, bug;
1324 };
1325
1326 struct bus_info {
1327   byte exists;
1328   byte guestbook;
1329   struct bus_bridge *bridges, *via;
1330 };
1331
1332 static struct bus_info *bus_info;
1333
1334 static void
1335 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
1336 {
1337   struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
1338   struct pci_dev *p = d->dev;
1339
1340   b->next = bi->bridges;
1341   bi->bridges = b;
1342   b->this = get_conf_byte(d, np);
1343   b->dev = p->dev;
1344   b->func = p->func;
1345   b->first = get_conf_byte(d, ns);
1346   b->last = get_conf_byte(d, nl);
1347   printf("## %02x.%02x:%d is a bridge from %02x to %02x-%02x\n",
1348          p->bus, p->dev, p->func, b->this, b->first, b->last);
1349   if (b->this != p->bus)
1350     printf("!!! Bridge points to invalid primary bus.\n");
1351   if (b->first > b->last)
1352     {
1353       printf("!!! Bridge points to invalid bus range.\n");
1354       b->last = b->first;
1355     }
1356 }
1357
1358 static void
1359 do_map_bus(int bus)
1360 {
1361   int dev, func;
1362   int verbose = pacc->debugging;
1363   struct bus_info *bi = bus_info + bus;
1364   struct device *d;
1365
1366   if (verbose)
1367     printf("Mapping bus %02x\n", bus);
1368   for(dev = 0; dev < 32; dev++)
1369     if (filter.slot < 0 || filter.slot == dev)
1370       {
1371         int func_limit = 1;
1372         for(func = 0; func < func_limit; func++)
1373           if (filter.func < 0 || filter.func == func)
1374             {
1375               struct pci_dev *p = pci_get_dev(pacc, bus, dev, func);
1376               u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
1377               if (vendor && vendor != 0xffff)
1378                 {
1379                   if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
1380                     func_limit = 8;
1381                   if (verbose)
1382                     printf("Discovered device %02x:%02x.%d\n", bus, dev, func);
1383                   bi->exists = 1;
1384                   if (d = scan_device(p))
1385                     {
1386                       show_device(d);
1387                       switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
1388                         {
1389                         case PCI_HEADER_TYPE_BRIDGE:
1390                           map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
1391                           break;
1392                         case PCI_HEADER_TYPE_CARDBUS:
1393                           map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
1394                           break;
1395                         }
1396                       free(d);
1397                     }
1398                   else if (verbose)
1399                     printf("But it was filtered out.\n");
1400                 }
1401               pci_free_dev(p);
1402             }
1403       }
1404 }
1405
1406 static void
1407 do_map_bridges(int bus, int min, int max)
1408 {
1409   struct bus_info *bi = bus_info + bus;
1410   struct bus_bridge *b;
1411
1412   bi->guestbook = 1;
1413   for(b=bi->bridges; b; b=b->next)
1414     {
1415       if (bus_info[b->first].guestbook)
1416         b->bug = 1;
1417       else if (b->first < min || b->last > max)
1418         b->bug = 2;
1419       else
1420         {
1421           bus_info[b->first].via = b;
1422           do_map_bridges(b->first, b->first, b->last);
1423         }
1424     }
1425 }
1426
1427 static void
1428 map_bridges(void)
1429 {
1430   int i;
1431
1432   printf("\nSummary of buses:\n\n");
1433   for(i=0; i<256; i++)
1434     if (bus_info[i].exists && !bus_info[i].guestbook)
1435       do_map_bridges(i, 0, 255);
1436   for(i=0; i<256; i++)
1437     {
1438       struct bus_info *bi = bus_info + i;
1439       struct bus_bridge *b = bi->via;
1440
1441       if (bi->exists)
1442         {
1443           printf("%02x: ", i);
1444           if (b)
1445             printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
1446           else if (!i)
1447             printf("Primary host bus\n");
1448           else
1449             printf("Secondary host bus (?)\n");
1450         }
1451       for(b=bi->bridges; b; b=b->next)
1452         {
1453           printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
1454           switch (b->bug)
1455             {
1456             case 1:
1457               printf(" <overlap bug>");
1458               break;
1459             case 2:
1460               printf(" <crossing bug>");
1461               break;
1462             }
1463           putchar('\n');
1464         }
1465     }
1466 }
1467
1468 static void
1469 map_the_bus(void)
1470 {
1471   if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
1472       pacc->method == PCI_ACCESS_DUMP)
1473     printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
1474   else if (!check_root())
1475     die("Only root can map the bus.");
1476   bus_info = xmalloc(sizeof(struct bus_info) * 256);
1477   bzero(bus_info, sizeof(struct bus_info) * 256);
1478   if (filter.bus >= 0)
1479     do_map_bus(filter.bus);
1480   else
1481     {
1482       int bus;
1483       for(bus=0; bus<256; bus++)
1484         do_map_bus(bus);
1485     }
1486   map_bridges();
1487 }
1488
1489 /* Main */
1490
1491 int
1492 main(int argc, char **argv)
1493 {
1494   int i;
1495   char *msg;
1496
1497   if (argc == 2 && !strcmp(argv[1], "--version"))
1498     {
1499       puts("lspci version " PCIUTILS_VERSION);
1500       return 0;
1501     }
1502
1503   pacc = pci_alloc();
1504   pacc->error = die;
1505   pci_filter_init(pacc, &filter);
1506
1507   while ((i = getopt(argc, argv, options)) != -1)
1508     switch (i)
1509       {
1510       case 'n':
1511         pacc->numeric_ids = 1;
1512         break;
1513       case 'v':
1514         verbose++;
1515         break;
1516       case 'b':
1517         pacc->buscentric = 1;
1518         buscentric_view = 1;
1519         break;
1520       case 's':
1521         if (msg = pci_filter_parse_slot(&filter, optarg))
1522           die("-f: %s", msg);
1523         break;
1524       case 'd':
1525         if (msg = pci_filter_parse_id(&filter, optarg))
1526           die("-d: %s", msg);
1527         break;
1528       case 'x':
1529         show_hex++;
1530         break;
1531       case 't':
1532         show_tree++;
1533         break;
1534       case 'i':
1535         pacc->id_file_name = optarg;
1536         break;
1537       case 'm':
1538         machine_readable++;
1539         break;
1540       case 'M':
1541         map_mode++;
1542         break;
1543       default:
1544         if (parse_generic_option(i, pacc, optarg))
1545           break;
1546       bad:
1547         fprintf(stderr, help_msg, pacc->id_file_name);
1548         return 1;
1549       }
1550   if (optind < argc)
1551     goto bad;
1552
1553   pci_init(pacc);
1554   if (map_mode)
1555     map_the_bus();
1556   else
1557     {
1558       scan_devices();
1559       sort_them();
1560       if (show_tree)
1561         show_forest();
1562       else
1563         show();
1564     }
1565   pci_cleanup(pacc);
1566
1567   return 0;
1568 }