]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
lspci: Decode PCIe 6.0 Slot Power Limit values
[pciutils.git] / lspci.c
1 /*
2  *      The PCI Utilities -- List All PCI Devices
3  *
4  *      Copyright (c) 1997--2020 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13
14 #include "lspci.h"
15
16 /* Options */
17
18 int verbose;                            /* Show detailed information */
19 static int opt_hex;                     /* Show contents of config space as hexadecimal numbers */
20 struct pci_filter filter;               /* Device filter */
21 static int opt_filter;                  /* Any filter was given */
22 static int opt_tree;                    /* Show bus tree */
23 static int opt_path;                    /* Show bridge path */
24 static int opt_machine;                 /* Generate machine-readable output */
25 static int opt_map_mode;                /* Bus mapping mode enabled */
26 static int opt_domains;                 /* Show domain numbers (0=disabled, 1=auto-detected, 2=requested) */
27 static int opt_kernel;                  /* Show kernel drivers */
28 static int opt_query_dns;               /* Query the DNS (0=disabled, 1=enabled, 2=refresh cache) */
29 static int opt_query_all;               /* Query the DNS for all entries */
30 char *opt_pcimap;                       /* Override path to Linux modules.pcimap */
31
32 const char program_name[] = "lspci";
33
34 static char options[] = "nvbxs:d:tPi:mgp:qkMDQ" GENERIC_OPTIONS ;
35
36 static char help_msg[] =
37 "Usage: lspci [<switches>]\n"
38 "\n"
39 "Basic display modes:\n"
40 "-mm\t\tProduce machine-readable output (single -m for an obsolete format)\n"
41 "-t\t\tShow bus tree\n"
42 "\n"
43 "Display options:\n"
44 "-v\t\tBe verbose (-vv or -vvv for higher verbosity)\n"
45 #ifdef PCI_OS_LINUX
46 "-k\t\tShow kernel drivers handling each device\n"
47 #endif
48 "-x\t\tShow hex-dump of the standard part of the config space\n"
49 "-xxx\t\tShow hex-dump of the whole config space (dangerous; root only)\n"
50 "-xxxx\t\tShow hex-dump of the 4096-byte extended config space (root only)\n"
51 "-b\t\tBus-centric view (addresses and IRQ's as seen by the bus)\n"
52 "-D\t\tAlways show domain numbers\n"
53 "-P\t\tDisplay bridge path in addition to bus and device number\n"
54 "-PP\t\tDisplay bus path in addition to bus and device number\n"
55 "\n"
56 "Resolving of device ID's to names:\n"
57 "-n\t\tShow numeric ID's\n"
58 "-nn\t\tShow both textual and numeric ID's (names & numbers)\n"
59 #ifdef PCI_USE_DNS
60 "-q\t\tQuery the PCI ID database for unknown ID's via DNS\n"
61 "-qq\t\tAs above, but re-query locally cached entries\n"
62 "-Q\t\tQuery the PCI ID database for all ID's via DNS\n"
63 #endif
64 "\n"
65 "Selection of devices:\n"
66 "-s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]]\tShow only devices in selected slots\n"
67 "-d [<vendor>]:[<device>][:<class>]\t\tShow only devices with specified ID's\n"
68 "\n"
69 "Other options:\n"
70 "-i <file>\tUse specified ID database instead of %s\n"
71 #ifdef PCI_OS_LINUX
72 "-p <file>\tLook up kernel modules in a given file instead of default modules.pcimap\n"
73 #endif
74 "-M\t\tEnable `bus mapping' mode (dangerous; root only)\n"
75 "\n"
76 "PCI access options:\n"
77 GENERIC_HELP
78 ;
79
80 /*** Our view of the PCI bus ***/
81
82 struct pci_access *pacc;
83 struct device *first_dev;
84 static int seen_errors;
85 static int need_topology;
86
87 int
88 config_fetch(struct device *d, unsigned int pos, unsigned int len)
89 {
90   unsigned int end = pos+len;
91   int result;
92
93   while (pos < d->config_bufsize && len && d->present[pos])
94     pos++, len--;
95   while (pos+len <= d->config_bufsize && len && d->present[pos+len-1])
96     len--;
97   if (!len)
98     return 1;
99
100   if (end > d->config_bufsize)
101     {
102       int orig_size = d->config_bufsize;
103       while (end > d->config_bufsize)
104         d->config_bufsize *= 2;
105       d->config = xrealloc(d->config, d->config_bufsize);
106       d->present = xrealloc(d->present, d->config_bufsize);
107       memset(d->present + orig_size, 0, d->config_bufsize - orig_size);
108     }
109   result = pci_read_block(d->dev, pos, d->config + pos, len);
110   if (result)
111     memset(d->present + pos, 1, len);
112   return result;
113 }
114
115 struct device *
116 scan_device(struct pci_dev *p)
117 {
118   struct device *d;
119
120   if (p->domain && !opt_domains)
121     opt_domains = 1;
122   if (!pci_filter_match(&filter, p) && !need_topology)
123     return NULL;
124   d = xmalloc(sizeof(struct device));
125   memset(d, 0, sizeof(*d));
126   d->dev = p;
127   d->config_cached = d->config_bufsize = 64;
128   d->config = xmalloc(64);
129   d->present = xmalloc(64);
130   memset(d->present, 1, 64);
131   if (!pci_read_block(p, 0, d->config, 64))
132     {
133       fprintf(stderr, "lspci: Unable to read the standard configuration space header of device %04x:%02x:%02x.%d\n",
134               p->domain, p->bus, p->dev, p->func);
135       seen_errors++;
136       return NULL;
137     }
138   if ((d->config[PCI_HEADER_TYPE] & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
139     {
140       /* For cardbus bridges, we need to fetch 64 bytes more to get the
141        * full standard header... */
142       if (config_fetch(d, 64, 64))
143         d->config_cached += 64;
144     }
145   pci_setup_cache(p, d->config, d->config_cached);
146   pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_CLASS_EXT | PCI_FILL_SUBSYS | (need_topology ? PCI_FILL_PARENT : 0));
147   return d;
148 }
149
150 static void
151 scan_devices(void)
152 {
153   struct device *d;
154   struct pci_dev *p;
155
156   pci_scan_bus(pacc);
157   for (p=pacc->devices; p; p=p->next)
158     if (d = scan_device(p))
159       {
160         d->next = first_dev;
161         first_dev = d;
162       }
163 }
164
165 /*** Config space accesses ***/
166
167 static void
168 check_conf_range(struct device *d, unsigned int pos, unsigned int len)
169 {
170   while (len)
171     if (!d->present[pos])
172       die("Internal bug: Accessing non-read configuration byte at position %x", pos);
173     else
174       pos++, len--;
175 }
176
177 byte
178 get_conf_byte(struct device *d, unsigned int pos)
179 {
180   check_conf_range(d, pos, 1);
181   return d->config[pos];
182 }
183
184 word
185 get_conf_word(struct device *d, unsigned int pos)
186 {
187   check_conf_range(d, pos, 2);
188   return d->config[pos] | (d->config[pos+1] << 8);
189 }
190
191 u32
192 get_conf_long(struct device *d, unsigned int pos)
193 {
194   check_conf_range(d, pos, 4);
195   return d->config[pos] |
196     (d->config[pos+1] << 8) |
197     (d->config[pos+2] << 16) |
198     (d->config[pos+3] << 24);
199 }
200
201 /*** Sorting ***/
202
203 static int
204 compare_them(const void *A, const void *B)
205 {
206   const struct pci_dev *a = (*(const struct device **)A)->dev;
207   const struct pci_dev *b = (*(const struct device **)B)->dev;
208
209   if (a->domain < b->domain)
210     return -1;
211   if (a->domain > b->domain)
212     return 1;
213   if (a->bus < b->bus)
214     return -1;
215   if (a->bus > b->bus)
216     return 1;
217   if (a->dev < b->dev)
218     return -1;
219   if (a->dev > b->dev)
220     return 1;
221   if (a->func < b->func)
222     return -1;
223   if (a->func > b->func)
224     return 1;
225   return 0;
226 }
227
228 static void
229 sort_them(void)
230 {
231   struct device **index, **h, **last_dev;
232   int cnt;
233   struct device *d;
234
235   cnt = 0;
236   for (d=first_dev; d; d=d->next)
237     cnt++;
238   h = index = alloca(sizeof(struct device *) * cnt);
239   for (d=first_dev; d; d=d->next)
240     *h++ = d;
241   qsort(index, cnt, sizeof(struct device *), compare_them);
242   last_dev = &first_dev;
243   h = index;
244   while (cnt--)
245     {
246       *last_dev = *h;
247       last_dev = &(*h)->next;
248       h++;
249     }
250   *last_dev = NULL;
251 }
252
253 /*** Normal output ***/
254
255 static void
256 show_slot_path(struct device *d)
257 {
258   struct pci_dev *p = d->dev;
259
260   if (opt_path)
261     {
262       struct bus *bus = d->parent_bus;
263       struct bridge *br = bus->parent_bridge;
264
265       if (br && br->br_dev)
266         {
267           show_slot_path(br->br_dev);
268           if (opt_path > 1)
269             printf("/%02x:%02x.%d", p->bus, p->dev, p->func);
270           else
271             printf("/%02x.%d", p->dev, p->func);
272           return;
273         }
274     }
275   printf("%02x:%02x.%d", p->bus, p->dev, p->func);
276 }
277
278 static void
279 show_slot_name(struct device *d)
280 {
281   struct pci_dev *p = d->dev;
282
283   if (!opt_machine ? opt_domains : (p->domain || opt_domains >= 2))
284     printf("%04x:", p->domain);
285   show_slot_path(d);
286 }
287
288 static void
289 show_terse(struct device *d)
290 {
291   int c;
292   struct pci_dev *p = d->dev;
293   char classbuf[128], devbuf[128];
294
295   show_slot_name(d);
296   printf(" %s: %s",
297          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
298                          PCI_LOOKUP_CLASS,
299                          p->device_class),
300          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
301                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
302                          p->vendor_id, p->device_id));
303   if ((p->known_fields & PCI_FILL_CLASS_EXT) && p->rev_id)
304     printf(" (rev %02x)", p->rev_id);
305   if (verbose)
306     {
307       char *x;
308       c = (p->known_fields & PCI_FILL_CLASS_EXT) ? p->prog_if : 0;
309       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
310                           PCI_LOOKUP_PROGIF | PCI_LOOKUP_NO_NUMBERS,
311                           p->device_class, c);
312       if (c || x)
313         {
314           printf(" (prog-if %02x", c);
315           if (x)
316             printf(" [%s]", x);
317           putchar(')');
318         }
319     }
320   putchar('\n');
321
322   if (verbose || opt_kernel)
323     {
324       char ssnamebuf[256];
325
326       pci_fill_info(p, PCI_FILL_LABEL);
327
328       if (p->label)
329         printf("\tDeviceName: %s", p->label);
330       if ((p->known_fields & PCI_FILL_SUBSYS) &&
331           p->subsys_vendor_id && p->subsys_vendor_id != 0xffff)
332         printf("\tSubsystem: %s\n",
333                 pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
334                         PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
335                         p->vendor_id, p->device_id, p->subsys_vendor_id, p->subsys_id));
336     }
337 }
338
339 /*** Verbose output ***/
340
341 static void
342 show_size(u64 x)
343 {
344   static const char suffix[][2] = { "", "K", "M", "G", "T" };
345   unsigned i;
346   if (!x)
347     return;
348   for (i = 0; i < (sizeof(suffix) / sizeof(*suffix) - 1); i++) {
349     if (x % 1024)
350       break;
351     x /= 1024;
352   }
353   printf(" [size=%u%s]", (unsigned)x, suffix[i]);
354 }
355
356 static void
357 show_range(char *prefix, u64 base, u64 limit, int bits, int disabled)
358 {
359   printf("%s:", prefix);
360   if (base <= limit || verbose > 2)
361     printf(" %0*" PCI_U64_FMT_X "-%0*" PCI_U64_FMT_X, (bits+3)/4, base, (bits+3)/4, limit);
362   if (!disabled && base <= limit)
363     show_size(limit - base + 1);
364   else
365     printf(" [disabled]");
366   printf(" [%d-bit]", bits);
367   putchar('\n');
368 }
369
370 static void
371 show_bases(struct device *d, int cnt)
372 {
373   struct pci_dev *p = d->dev;
374   word cmd = get_conf_word(d, PCI_COMMAND);
375   int i;
376   int virtual = 0;
377
378   for (i=0; i<cnt; i++)
379     {
380       pciaddr_t pos = p->base_addr[i];
381       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
382       pciaddr_t ioflg = (p->known_fields & PCI_FILL_IO_FLAGS) ? p->flags[i] : 0;
383       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
384       u32 hw_lower;
385       u32 hw_upper = 0;
386       int broken = 0;
387
388       if (flg == 0xffffffff)
389         flg = 0;
390       if (!pos && !flg && !len)
391         continue;
392
393       if (verbose > 1)
394         printf("\tRegion %d: ", i);
395       else
396         putchar('\t');
397
398       /* Read address as seen by the hardware */
399       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
400         hw_lower = flg & PCI_BASE_ADDRESS_IO_MASK;
401       else
402         {
403           hw_lower = flg & PCI_BASE_ADDRESS_MEM_MASK;
404           if ((flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64)
405             {
406               if (i >= cnt - 1)
407                 broken = 1;
408               else
409                 {
410                   i++;
411                   hw_upper = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
412                 }
413             }
414         }
415
416       /* Detect virtual regions, which are reported by the OS, but unassigned in the device */
417       if (pos && !hw_lower && !hw_upper && !(ioflg & PCI_IORESOURCE_PCI_EA_BEI))
418         {
419           flg = pos;
420           virtual = 1;
421         }
422
423       /* Print base address */
424       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
425         {
426           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
427           printf("I/O ports at ");
428           if (a || (cmd & PCI_COMMAND_IO))
429             printf(PCIADDR_PORT_FMT, a);
430           else if (hw_lower)
431             printf("<ignored>");
432           else
433             printf("<unassigned>");
434           if (virtual)
435             printf(" [virtual]");
436           else if (!(cmd & PCI_COMMAND_IO))
437             printf(" [disabled]");
438         }
439       else
440         {
441           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
442           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
443
444           printf("Memory at ");
445           if (broken)
446             printf("<broken-64-bit-slot>");
447           else if (a)
448             printf(PCIADDR_T_FMT, a);
449           else if (hw_lower || hw_upper)
450             printf("<ignored>");
451           else
452             printf("<unassigned>");
453           printf(" (%s, %sprefetchable)",
454                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
455                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
456                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
457                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
458           if (virtual)
459             printf(" [virtual]");
460           else if (!(cmd & PCI_COMMAND_MEMORY))
461             printf(" [disabled]");
462         }
463
464       if (ioflg & PCI_IORESOURCE_PCI_EA_BEI)
465         printf(" [enhanced]");
466
467       show_size(len);
468       putchar('\n');
469     }
470 }
471
472 static void
473 show_rom(struct device *d, int reg)
474 {
475   struct pci_dev *p = d->dev;
476   pciaddr_t rom = p->rom_base_addr;
477   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
478   pciaddr_t ioflg = (p->known_fields & PCI_FILL_IO_FLAGS) ? p->rom_flags : 0;
479   u32 flg = get_conf_long(d, reg);
480   word cmd = get_conf_word(d, PCI_COMMAND);
481   int virtual = 0;
482
483   if (!rom && !flg && !len)
484     return;
485
486   if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK) && !(ioflg & PCI_IORESOURCE_PCI_EA_BEI))
487     {
488       flg = rom;
489       virtual = 1;
490     }
491
492   printf("\tExpansion ROM at ");
493   if (rom & PCI_ROM_ADDRESS_MASK)
494     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
495   else if (flg & PCI_ROM_ADDRESS_MASK)
496     printf("<ignored>");
497   else
498     printf("<unassigned>");
499
500   if (virtual)
501     printf(" [virtual]");
502
503   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
504     printf(" [disabled]");
505   else if (!virtual && !(cmd & PCI_COMMAND_MEMORY))
506     printf(" [disabled by cmd]");
507
508   if (ioflg & PCI_IORESOURCE_PCI_EA_BEI)
509       printf(" [enhanced]");
510
511   show_size(len);
512   putchar('\n');
513 }
514
515 static void
516 show_htype0(struct device *d)
517 {
518   show_bases(d, 6);
519   show_rom(d, PCI_ROM_ADDRESS);
520   show_caps(d, PCI_CAPABILITY_LIST);
521 }
522
523 static void
524 show_htype1(struct device *d)
525 {
526   struct pci_dev *p = d->dev;
527   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
528   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
529   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
530   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
531   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
532   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
533   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
534   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
535   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
536   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
537   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
538   int io_disabled = (p->known_fields & PCI_FILL_BRIDGE_BASES) && !p->bridge_size[0];
539   int mem_disabled = (p->known_fields & PCI_FILL_BRIDGE_BASES) && !p->bridge_size[1];
540   int pref_disabled = (p->known_fields & PCI_FILL_BRIDGE_BASES) && !p->bridge_size[2];
541   int io_bits, pref_bits;
542
543   show_bases(d, 2);
544   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
545          get_conf_byte(d, PCI_PRIMARY_BUS),
546          get_conf_byte(d, PCI_SECONDARY_BUS),
547          get_conf_byte(d, PCI_SUBORDINATE_BUS),
548          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
549
550   if ((p->known_fields & PCI_FILL_BRIDGE_BASES) && !io_disabled)
551     {
552       io_base = p->bridge_base_addr[0] & PCI_IO_RANGE_MASK;
553       io_limit = io_base + p->bridge_size[0] - 1;
554       io_type = p->bridge_base_addr[0] & PCI_IO_RANGE_TYPE_MASK;
555       io_bits = (io_type == PCI_IO_RANGE_TYPE_32) ? 32 : 16;
556       show_range("\tI/O behind bridge", io_base, io_limit, io_bits, io_disabled);
557     }
558   else if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
559       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
560     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
561   else
562     {
563       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
564       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
565       if (io_type == PCI_IO_RANGE_TYPE_32)
566         {
567           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
568           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
569         }
570       /* I/O is unsupported if both base and limit are zeros and resource is disabled */
571       if (!(io_base == 0x0 && io_limit == 0x0 && io_disabled))
572         {
573           io_limit += 0xfff;
574           io_bits = (io_type == PCI_IO_RANGE_TYPE_32) ? 32 : 16;
575           show_range("\tI/O behind bridge", io_base, io_limit, io_bits, io_disabled);
576         }
577     }
578
579   if ((p->known_fields & PCI_FILL_BRIDGE_BASES) && !mem_disabled)
580     {
581       mem_base = p->bridge_base_addr[1] & PCI_MEMORY_RANGE_MASK;
582       mem_limit = mem_base + p->bridge_size[1] - 1;
583       show_range("\tMemory behind bridge", mem_base, mem_limit, 32, mem_disabled);
584     }
585   else if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
586       mem_type)
587     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
588   else
589     {
590       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
591       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
592       show_range("\tMemory behind bridge", mem_base, mem_limit + 0xfffff, 32, mem_disabled);
593     }
594
595   if ((p->known_fields & PCI_FILL_BRIDGE_BASES) && !pref_disabled)
596     {
597       u64 pref_base_64 = p->bridge_base_addr[2] & PCI_MEMORY_RANGE_MASK;
598       u64 pref_limit_64 = pref_base_64 + p->bridge_size[2] - 1;
599       pref_type = p->bridge_base_addr[2] & PCI_MEMORY_RANGE_TYPE_MASK;
600       pref_bits = (pref_type == PCI_PREF_RANGE_TYPE_64) ? 64 : 32;
601       show_range("\tPrefetchable memory behind bridge", pref_base_64, pref_limit_64, pref_bits, pref_disabled);
602     }
603   else if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
604       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
605     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
606   else
607     {
608       u64 pref_base_64 = (pref_base & PCI_PREF_RANGE_MASK) << 16;
609       u64 pref_limit_64 = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
610       if (pref_type == PCI_PREF_RANGE_TYPE_64)
611         {
612           pref_base_64 |= (u64) get_conf_long(d, PCI_PREF_BASE_UPPER32) << 32;
613           pref_limit_64 |= (u64) get_conf_long(d, PCI_PREF_LIMIT_UPPER32) << 32;
614         }
615       /* Prefetchable memory is unsupported if both base and limit are zeros and resource is disabled */
616       if (!(pref_base_64 == 0x0 && pref_limit_64 == 0x0 && pref_disabled))
617         {
618           pref_limit_64 += 0xfffff;
619           pref_bits = (pref_type == PCI_PREF_RANGE_TYPE_64) ? 64 : 32;
620           show_range("\tPrefetchable memory behind bridge", pref_base_64, pref_limit_64, pref_bits, pref_disabled);
621         }
622     }
623
624   if (verbose > 1)
625     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
626              FLAG(sec_stat, PCI_STATUS_66MHZ),
627              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
628              FLAG(sec_stat, PCI_STATUS_PARITY),
629              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
630              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
631              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
632              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
633              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
634              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
635              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
636              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
637
638   show_rom(d, PCI_ROM_ADDRESS1);
639
640   if (verbose > 1)
641     {
642       printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c VGA16%c MAbort%c >Reset%c FastB2B%c\n",
643         FLAG(brc, PCI_BRIDGE_CTL_PARITY),
644         FLAG(brc, PCI_BRIDGE_CTL_SERR),
645         FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
646         FLAG(brc, PCI_BRIDGE_CTL_VGA),
647         FLAG(brc, PCI_BRIDGE_CTL_VGA_16BIT),
648         FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
649         FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
650         FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
651       printf("\t\tPriDiscTmr%c SecDiscTmr%c DiscTmrStat%c DiscTmrSERREn%c\n",
652         FLAG(brc, PCI_BRIDGE_CTL_PRI_DISCARD_TIMER),
653         FLAG(brc, PCI_BRIDGE_CTL_SEC_DISCARD_TIMER),
654         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_STATUS),
655         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_SERR_EN));
656     }
657
658   show_caps(d, PCI_CAPABILITY_LIST);
659 }
660
661 static void
662 show_htype2(struct device *d)
663 {
664   int i;
665   word cmd = get_conf_word(d, PCI_COMMAND);
666   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
667   word exca;
668   int verb = verbose > 2;
669
670   show_bases(d, 1);
671   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
672          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
673          get_conf_byte(d, PCI_CB_CARD_BUS),
674          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
675          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
676   for (i=0; i<2; i++)
677     {
678       int p = 8*i;
679       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
680       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
681       limit = limit + 0xfff;
682       if (base <= limit || verb)
683         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
684                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
685                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
686     }
687   for (i=0; i<2; i++)
688     {
689       int p = 8*i;
690       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
691       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
692       if (!(base & PCI_IO_RANGE_TYPE_32))
693         {
694           base &= 0xffff;
695           limit &= 0xffff;
696         }
697       base &= PCI_CB_IO_RANGE_MASK;
698       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
699       if (base <= limit || verb)
700         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
701                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
702     }
703
704   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
705     printf("\tSecondary status: SERR\n");
706   if (verbose > 1)
707     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
708            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
709            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
710            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
711            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
712            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
713            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
714            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
715            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
716
717   if (d->config_cached < 128)
718     {
719       printf("\t<access denied to the rest>\n");
720       return;
721     }
722
723   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
724   if (exca)
725     printf("\t16-bit legacy interface ports at %04x\n", exca);
726   show_caps(d, PCI_CB_CAPABILITY_LIST);
727 }
728
729 static void
730 show_verbose(struct device *d)
731 {
732   struct pci_dev *p = d->dev;
733   word status = get_conf_word(d, PCI_STATUS);
734   word cmd = get_conf_word(d, PCI_COMMAND);
735   word class = p->device_class;
736   byte bist = get_conf_byte(d, PCI_BIST);
737   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
738   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
739   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
740   byte max_lat, min_gnt;
741   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
742   unsigned int irq;
743   char *dt_node, *iommu_group;
744
745   show_terse(d);
746
747   pci_fill_info(p, PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES |
748     PCI_FILL_PHYS_SLOT | PCI_FILL_NUMA_NODE | PCI_FILL_DT_NODE | PCI_FILL_IOMMU_GROUP |
749     PCI_FILL_BRIDGE_BASES | PCI_FILL_CLASS_EXT | PCI_FILL_SUBSYS);
750   irq = p->irq;
751
752   switch (htype)
753     {
754     case PCI_HEADER_TYPE_NORMAL:
755       if (class == PCI_CLASS_BRIDGE_PCI)
756         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
757       max_lat = get_conf_byte(d, PCI_MAX_LAT);
758       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
759       break;
760     case PCI_HEADER_TYPE_BRIDGE:
761       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
762         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
763       min_gnt = max_lat = 0;
764       break;
765     case PCI_HEADER_TYPE_CARDBUS:
766       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
767         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
768       min_gnt = max_lat = 0;
769       break;
770     default:
771       printf("\t!!! Unknown header type %02x\n", htype);
772       return;
773     }
774
775   if (p->phy_slot)
776     printf("\tPhysical Slot: %s\n", p->phy_slot);
777
778   if (dt_node = pci_get_string_property(p, PCI_FILL_DT_NODE))
779     printf("\tDevice tree node: %s\n", dt_node);
780
781   if (verbose > 1)
782     {
783       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c DisINTx%c\n",
784              FLAG(cmd, PCI_COMMAND_IO),
785              FLAG(cmd, PCI_COMMAND_MEMORY),
786              FLAG(cmd, PCI_COMMAND_MASTER),
787              FLAG(cmd, PCI_COMMAND_SPECIAL),
788              FLAG(cmd, PCI_COMMAND_INVALIDATE),
789              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
790              FLAG(cmd, PCI_COMMAND_PARITY),
791              FLAG(cmd, PCI_COMMAND_WAIT),
792              FLAG(cmd, PCI_COMMAND_SERR),
793              FLAG(cmd, PCI_COMMAND_FAST_BACK),
794              FLAG(cmd, PCI_COMMAND_DISABLE_INTx));
795       printf("\tStatus: Cap%c 66MHz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c INTx%c\n",
796              FLAG(status, PCI_STATUS_CAP_LIST),
797              FLAG(status, PCI_STATUS_66MHZ),
798              FLAG(status, PCI_STATUS_UDF),
799              FLAG(status, PCI_STATUS_FAST_BACK),
800              FLAG(status, PCI_STATUS_PARITY),
801              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
802              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
803              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
804              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
805              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
806              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
807              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
808              FLAG(status, PCI_STATUS_DETECTED_PARITY),
809              FLAG(status, PCI_STATUS_INTx));
810       if (cmd & PCI_COMMAND_MASTER)
811         {
812           printf("\tLatency: %d", latency);
813           if (min_gnt || max_lat)
814             {
815               printf(" (");
816               if (min_gnt)
817                 printf("%dns min", min_gnt*250);
818               if (min_gnt && max_lat)
819                 printf(", ");
820               if (max_lat)
821                 printf("%dns max", max_lat*250);
822               putchar(')');
823             }
824           if (cache_line)
825             printf(", Cache Line Size: %d bytes", cache_line * 4);
826           putchar('\n');
827         }
828       if (int_pin || irq)
829         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
830                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
831       if (p->numa_node != -1)
832         printf("\tNUMA node: %d\n", p->numa_node);
833       if (iommu_group = pci_get_string_property(p, PCI_FILL_IOMMU_GROUP))
834         printf("\tIOMMU group: %s\n", iommu_group);
835     }
836   else
837     {
838       printf("\tFlags: ");
839       if (cmd & PCI_COMMAND_MASTER)
840         printf("bus master, ");
841       if (cmd & PCI_COMMAND_VGA_PALETTE)
842         printf("VGA palette snoop, ");
843       if (cmd & PCI_COMMAND_WAIT)
844         printf("stepping, ");
845       if (cmd & PCI_COMMAND_FAST_BACK)
846         printf("fast Back2Back, ");
847       if (status & PCI_STATUS_66MHZ)
848         printf("66MHz, ");
849       if (status & PCI_STATUS_UDF)
850         printf("user-definable features, ");
851       printf("%s devsel",
852              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
853              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
854              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
855       if (cmd & PCI_COMMAND_MASTER)
856         printf(", latency %d", latency);
857       if (irq)
858         printf(", IRQ " PCIIRQ_FMT, irq);
859       if (p->numa_node != -1)
860         printf(", NUMA node %d", p->numa_node);
861       if (iommu_group = pci_get_string_property(p, PCI_FILL_IOMMU_GROUP))
862         printf(", IOMMU group %s", iommu_group);
863       putchar('\n');
864     }
865
866   if (bist & PCI_BIST_CAPABLE)
867     {
868       if (bist & PCI_BIST_START)
869         printf("\tBIST is running\n");
870       else
871         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
872     }
873
874   switch (htype)
875     {
876     case PCI_HEADER_TYPE_NORMAL:
877       show_htype0(d);
878       break;
879     case PCI_HEADER_TYPE_BRIDGE:
880       show_htype1(d);
881       break;
882     case PCI_HEADER_TYPE_CARDBUS:
883       show_htype2(d);
884       break;
885     }
886 }
887
888 /*** Machine-readable dumps ***/
889
890 static void
891 show_hex_dump(struct device *d)
892 {
893   unsigned int i, cnt;
894
895   cnt = d->config_cached;
896   if (opt_hex >= 3 && config_fetch(d, cnt, 256-cnt))
897     {
898       cnt = 256;
899       if (opt_hex >= 4 && config_fetch(d, 256, 4096-256))
900         cnt = 4096;
901     }
902
903   for (i=0; i<cnt; i++)
904     {
905       if (! (i & 15))
906         printf("%02x:", i);
907       printf(" %02x", get_conf_byte(d, i));
908       if ((i & 15) == 15)
909         putchar('\n');
910     }
911 }
912
913 static void
914 print_shell_escaped(char *c)
915 {
916   printf(" \"");
917   while (*c)
918     {
919       if (*c == '"' || *c == '\\')
920         putchar('\\');
921       putchar(*c++);
922     }
923   putchar('"');
924 }
925
926 static void
927 show_machine(struct device *d)
928 {
929   struct pci_dev *p = d->dev;
930   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
931   char *dt_node, *iommu_group;
932
933   if (verbose)
934     {
935       pci_fill_info(p, PCI_FILL_PHYS_SLOT | PCI_FILL_NUMA_NODE | PCI_FILL_DT_NODE | PCI_FILL_IOMMU_GROUP);
936       printf((opt_machine >= 2) ? "Slot:\t" : "Device:\t");
937       show_slot_name(d);
938       putchar('\n');
939       printf("Class:\t%s\n",
940              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
941       printf("Vendor:\t%s\n",
942              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
943       printf("Device:\t%s\n",
944              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
945       if ((p->known_fields & PCI_FILL_SUBSYS) &&
946           p->subsys_vendor_id && p->subsys_vendor_id != 0xffff)
947         {
948           printf("SVendor:\t%s\n",
949                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->subsys_vendor_id));
950           printf("SDevice:\t%s\n",
951                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, p->subsys_vendor_id, p->subsys_id));
952         }
953       if (p->phy_slot)
954         printf("PhySlot:\t%s\n", p->phy_slot);
955       if ((p->known_fields & PCI_FILL_CLASS_EXT) && p->rev_id)
956         printf("Rev:\t%02x\n", p->rev_id);
957       if (p->known_fields & PCI_FILL_CLASS_EXT)
958         printf("ProgIf:\t%02x\n", p->prog_if);
959       if (opt_kernel)
960         show_kernel_machine(d);
961       if (p->numa_node != -1)
962         printf("NUMANode:\t%d\n", p->numa_node);
963       if (dt_node = pci_get_string_property(p, PCI_FILL_DT_NODE))
964         printf("DTNode:\t%s\n", dt_node);
965       if (iommu_group = pci_get_string_property(p, PCI_FILL_IOMMU_GROUP))
966         printf("IOMMUGroup:\t%s\n", iommu_group);
967     }
968   else
969     {
970       show_slot_name(d);
971       print_shell_escaped(pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
972       print_shell_escaped(pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
973       print_shell_escaped(pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
974       if ((p->known_fields & PCI_FILL_CLASS_EXT) && p->rev_id)
975         printf(" -r%02x", p->rev_id);
976       if (p->known_fields & PCI_FILL_CLASS_EXT)
977         printf(" -p%02x", p->prog_if);
978       if ((p->known_fields & PCI_FILL_SUBSYS) &&
979           p->subsys_vendor_id && p->subsys_vendor_id != 0xffff)
980         {
981           print_shell_escaped(pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, p->subsys_vendor_id));
982           print_shell_escaped(pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, p->subsys_vendor_id, p->subsys_id));
983         }
984       else
985         printf(" \"\" \"\"");
986       putchar('\n');
987     }
988 }
989
990 /*** Main show function ***/
991
992 void
993 show_device(struct device *d)
994 {
995   if (opt_machine)
996     show_machine(d);
997   else
998     {
999       if (verbose)
1000         show_verbose(d);
1001       else
1002         show_terse(d);
1003       if (opt_kernel || verbose)
1004         show_kernel(d);
1005     }
1006   if (opt_hex)
1007     show_hex_dump(d);
1008   if (verbose || opt_hex)
1009     putchar('\n');
1010 }
1011
1012 static void
1013 show(void)
1014 {
1015   struct device *d;
1016
1017   for (d=first_dev; d; d=d->next)
1018     if (pci_filter_match(&filter, d->dev))
1019       show_device(d);
1020 }
1021
1022 /* Main */
1023
1024 int
1025 main(int argc, char **argv)
1026 {
1027   int i;
1028   char *msg;
1029
1030   if (argc == 2 && !strcmp(argv[1], "--version"))
1031     {
1032       puts("lspci version " PCIUTILS_VERSION);
1033       return 0;
1034     }
1035
1036   pacc = pci_alloc();
1037   pacc->error = die;
1038   pci_filter_init(pacc, &filter);
1039
1040   while ((i = getopt(argc, argv, options)) != -1)
1041     switch (i)
1042       {
1043       case 'n':
1044         pacc->numeric_ids++;
1045         break;
1046       case 'v':
1047         verbose++;
1048         break;
1049       case 'b':
1050         pacc->buscentric = 1;
1051         break;
1052       case 's':
1053         if (msg = pci_filter_parse_slot(&filter, optarg))
1054           die("-s: %s", msg);
1055         opt_filter = 1;
1056         break;
1057       case 'd':
1058         if (msg = pci_filter_parse_id(&filter, optarg))
1059           die("-d: %s", msg);
1060         opt_filter = 1;
1061         break;
1062       case 'x':
1063         opt_hex++;
1064         break;
1065       case 'P':
1066         opt_path++;
1067         need_topology = 1;
1068         break;
1069       case 't':
1070         opt_tree++;
1071         need_topology = 1;
1072         break;
1073       case 'i':
1074         pci_set_name_list_path(pacc, optarg, 0);
1075         break;
1076       case 'm':
1077         opt_machine++;
1078         break;
1079       case 'p':
1080         opt_pcimap = optarg;
1081         break;
1082 #ifdef PCI_OS_LINUX
1083       case 'k':
1084         opt_kernel++;
1085         break;
1086 #endif
1087       case 'M':
1088         opt_map_mode++;
1089         break;
1090       case 'D':
1091         opt_domains = 2;
1092         break;
1093 #ifdef PCI_USE_DNS
1094       case 'q':
1095         opt_query_dns++;
1096         break;
1097       case 'Q':
1098         opt_query_all = 1;
1099         break;
1100 #else
1101       case 'q':
1102       case 'Q':
1103         die("DNS queries are not available in this version");
1104 #endif
1105       default:
1106         if (parse_generic_option(i, pacc, optarg))
1107           break;
1108       bad:
1109         fprintf(stderr, help_msg, pacc->id_file_name);
1110         return 1;
1111       }
1112   if (optind < argc)
1113     goto bad;
1114
1115   if (opt_query_dns)
1116     {
1117       pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK;
1118       if (opt_query_dns > 1)
1119         pacc->id_lookup_mode |= PCI_LOOKUP_REFRESH_CACHE;
1120     }
1121   if (opt_query_all)
1122     pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK | PCI_LOOKUP_SKIP_LOCAL;
1123
1124   pci_init(pacc);
1125   if (opt_map_mode)
1126     {
1127       if (need_topology)
1128         die("Bus mapping mode does not recognize bus topology");
1129       map_the_bus();
1130     }
1131   else
1132     {
1133       scan_devices();
1134       sort_them();
1135       if (need_topology)
1136         grow_tree();
1137       if (opt_tree)
1138         show_forest(opt_filter ? &filter : NULL);
1139       else
1140         show();
1141     }
1142   show_kernel_cleanup();
1143   pci_cleanup(pacc);
1144
1145   return (seen_errors ? 2 : 0);
1146 }