]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
17649a0540faca66eea185fd90141821b9a2ddfd
[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);
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 void
289 get_subid(struct device *d, word *subvp, word *subdp)
290 {
291   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
292
293   if (htype == PCI_HEADER_TYPE_NORMAL)
294     {
295       *subvp = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
296       *subdp = get_conf_word(d, PCI_SUBSYSTEM_ID);
297     }
298   else if (htype == PCI_HEADER_TYPE_CARDBUS && d->config_cached >= 128)
299     {
300       *subvp = get_conf_word(d, PCI_CB_SUBSYSTEM_VENDOR_ID);
301       *subdp = get_conf_word(d, PCI_CB_SUBSYSTEM_ID);
302     }
303   else
304     *subvp = *subdp = 0xffff;
305 }
306
307 static void
308 show_terse(struct device *d)
309 {
310   int c;
311   struct pci_dev *p = d->dev;
312   char classbuf[128], devbuf[128];
313
314   show_slot_name(d);
315   printf(" %s: %s",
316          pci_lookup_name(pacc, classbuf, sizeof(classbuf),
317                          PCI_LOOKUP_CLASS,
318                          p->device_class),
319          pci_lookup_name(pacc, devbuf, sizeof(devbuf),
320                          PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
321                          p->vendor_id, p->device_id));
322   if (c = get_conf_byte(d, PCI_REVISION_ID))
323     printf(" (rev %02x)", c);
324   if (verbose)
325     {
326       char *x;
327       c = get_conf_byte(d, PCI_CLASS_PROG);
328       x = pci_lookup_name(pacc, devbuf, sizeof(devbuf),
329                           PCI_LOOKUP_PROGIF | PCI_LOOKUP_NO_NUMBERS,
330                           p->device_class, c);
331       if (c || x)
332         {
333           printf(" (prog-if %02x", c);
334           if (x)
335             printf(" [%s]", x);
336           putchar(')');
337         }
338     }
339   putchar('\n');
340
341   if (verbose || opt_kernel)
342     {
343       word subsys_v, subsys_d;
344       char ssnamebuf[256];
345
346       pci_fill_info(p, PCI_FILL_LABEL);
347
348       if (p->label)
349         printf("\tDeviceName: %s", p->label);
350       get_subid(d, &subsys_v, &subsys_d);
351       if (subsys_v && subsys_v != 0xffff)
352         printf("\tSubsystem: %s\n",
353                 pci_lookup_name(pacc, ssnamebuf, sizeof(ssnamebuf),
354                         PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
355                         p->vendor_id, p->device_id, subsys_v, subsys_d));
356     }
357 }
358
359 /*** Verbose output ***/
360
361 static void
362 show_size(u64 x)
363 {
364   static const char suffix[][2] = { "", "K", "M", "G", "T" };
365   unsigned i;
366   if (!x)
367     return;
368   for (i = 0; i < (sizeof(suffix) / sizeof(*suffix) - 1); i++) {
369     if (x % 1024)
370       break;
371     x /= 1024;
372   }
373   printf(" [size=%u%s]", (unsigned)x, suffix[i]);
374 }
375
376 static void
377 show_range(char *prefix, u64 base, u64 limit, int bits)
378 {
379   printf("%s:", prefix);
380   if (base <= limit || verbose > 2)
381     {
382       if (bits > 32)
383         printf(" %016" PCI_U64_FMT_X "-%016" PCI_U64_FMT_X, base, limit);
384       else
385         printf(" %08x-%08x", (unsigned) base, (unsigned) limit);
386     }
387   if (base <= limit)
388     show_size(limit - base + 1);
389   else
390     printf(" [disabled]");
391   printf(" [%d-bit]", bits);
392   putchar('\n');
393 }
394
395 static void
396 show_bases(struct device *d, int cnt)
397 {
398   struct pci_dev *p = d->dev;
399   word cmd = get_conf_word(d, PCI_COMMAND);
400   int i;
401   int virtual = 0;
402
403   for (i=0; i<cnt; i++)
404     {
405       pciaddr_t pos = p->base_addr[i];
406       pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
407       pciaddr_t ioflg = (p->known_fields & PCI_FILL_IO_FLAGS) ? p->flags[i] : 0;
408       u32 flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
409       u32 hw_lower;
410       u32 hw_upper = 0;
411       int broken = 0;
412
413       if (flg == 0xffffffff)
414         flg = 0;
415       if (!pos && !flg && !len)
416         continue;
417
418       if (verbose > 1)
419         printf("\tRegion %d: ", i);
420       else
421         putchar('\t');
422
423       /* Read address as seen by the hardware */
424       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
425         hw_lower = flg & PCI_BASE_ADDRESS_IO_MASK;
426       else
427         {
428           hw_lower = flg & PCI_BASE_ADDRESS_MEM_MASK;
429           if ((flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64)
430             {
431               if (i >= cnt - 1)
432                 broken = 1;
433               else
434                 {
435                   i++;
436                   hw_upper = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
437                 }
438             }
439         }
440
441       /* Detect virtual regions, which are reported by the OS, but unassigned in the device */
442       if (pos && !hw_lower && !hw_upper && !(ioflg & PCI_IORESOURCE_PCI_EA_BEI))
443         {
444           flg = pos;
445           virtual = 1;
446         }
447
448       /* Print base address */
449       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
450         {
451           pciaddr_t a = pos & PCI_BASE_ADDRESS_IO_MASK;
452           printf("I/O ports at ");
453           if (a || (cmd & PCI_COMMAND_IO))
454             printf(PCIADDR_PORT_FMT, a);
455           else if (hw_lower)
456             printf("<ignored>");
457           else
458             printf("<unassigned>");
459           if (virtual)
460             printf(" [virtual]");
461           else if (!(cmd & PCI_COMMAND_IO))
462             printf(" [disabled]");
463         }
464       else
465         {
466           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
467           pciaddr_t a = pos & PCI_ADDR_MEM_MASK;
468
469           printf("Memory at ");
470           if (broken)
471             printf("<broken-64-bit-slot>");
472           else if (a)
473             printf(PCIADDR_T_FMT, a);
474           else if (hw_lower || hw_upper)
475             printf("<ignored>");
476           else
477             printf("<unassigned>");
478           printf(" (%s, %sprefetchable)",
479                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
480                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
481                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M" : "type 3",
482                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-");
483           if (virtual)
484             printf(" [virtual]");
485           else if (!(cmd & PCI_COMMAND_MEMORY))
486             printf(" [disabled]");
487         }
488
489       if (ioflg & PCI_IORESOURCE_PCI_EA_BEI)
490         printf(" [enhanced]");
491
492       show_size(len);
493       putchar('\n');
494     }
495 }
496
497 static void
498 show_rom(struct device *d, int reg)
499 {
500   struct pci_dev *p = d->dev;
501   pciaddr_t rom = p->rom_base_addr;
502   pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->rom_size : 0;
503   pciaddr_t ioflg = (p->known_fields & PCI_FILL_IO_FLAGS) ? p->rom_flags : 0;
504   u32 flg = get_conf_long(d, reg);
505   word cmd = get_conf_word(d, PCI_COMMAND);
506   int virtual = 0;
507
508   if (!rom && !flg && !len)
509     return;
510
511   if ((rom & PCI_ROM_ADDRESS_MASK) && !(flg & PCI_ROM_ADDRESS_MASK) && !(ioflg & PCI_IORESOURCE_PCI_EA_BEI))
512     {
513       flg = rom;
514       virtual = 1;
515     }
516
517   printf("\tExpansion ROM at ");
518   if (rom & PCI_ROM_ADDRESS_MASK)
519     printf(PCIADDR_T_FMT, rom & PCI_ROM_ADDRESS_MASK);
520   else if (flg & PCI_ROM_ADDRESS_MASK)
521     printf("<ignored>");
522   else
523     printf("<unassigned>");
524
525   if (virtual)
526     printf(" [virtual]");
527
528   if (!(flg & PCI_ROM_ADDRESS_ENABLE))
529     printf(" [disabled]");
530   else if (!virtual && !(cmd & PCI_COMMAND_MEMORY))
531     printf(" [disabled by cmd]");
532
533   if (ioflg & PCI_IORESOURCE_PCI_EA_BEI)
534       printf(" [enhanced]");
535
536   show_size(len);
537   putchar('\n');
538 }
539
540 static void
541 show_htype0(struct device *d)
542 {
543   show_bases(d, 6);
544   show_rom(d, PCI_ROM_ADDRESS);
545   show_caps(d, PCI_CAPABILITY_LIST);
546 }
547
548 static void
549 show_htype1(struct device *d)
550 {
551   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
552   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
553   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
554   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
555   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
556   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
557   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
558   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
559   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
560   word sec_stat = get_conf_word(d, PCI_SEC_STATUS);
561   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
562
563   show_bases(d, 2);
564   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
565          get_conf_byte(d, PCI_PRIMARY_BUS),
566          get_conf_byte(d, PCI_SECONDARY_BUS),
567          get_conf_byte(d, PCI_SUBORDINATE_BUS),
568          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
569
570   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
571       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
572     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
573   else
574     {
575       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
576       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
577       if (io_type == PCI_IO_RANGE_TYPE_32)
578         {
579           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
580           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
581         }
582       show_range("\tI/O behind bridge", io_base, io_limit+0xfff, (io_type == PCI_IO_RANGE_TYPE_32) ? 32 : 16);
583     }
584
585   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);
593     }
594
595   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
596       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
597     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
598   else
599     {
600       u64 pref_base_64 = (pref_base & PCI_PREF_RANGE_MASK) << 16;
601       u64 pref_limit_64 = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
602       if (pref_type == PCI_PREF_RANGE_TYPE_64)
603         {
604           pref_base_64 |= (u64) get_conf_long(d, PCI_PREF_BASE_UPPER32) << 32;
605           pref_limit_64 |= (u64) get_conf_long(d, PCI_PREF_LIMIT_UPPER32) << 32;
606         }
607       show_range("\tPrefetchable memory behind bridge", pref_base_64, pref_limit_64 + 0xfffff, (pref_type == PCI_PREF_RANGE_TYPE_64) ? 64 : 32);
608     }
609
610   if (verbose > 1)
611     printf("\tSecondary status: 66MHz%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c <SERR%c <PERR%c\n",
612              FLAG(sec_stat, PCI_STATUS_66MHZ),
613              FLAG(sec_stat, PCI_STATUS_FAST_BACK),
614              FLAG(sec_stat, PCI_STATUS_PARITY),
615              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
616              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
617              ((sec_stat & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
618              FLAG(sec_stat, PCI_STATUS_SIG_TARGET_ABORT),
619              FLAG(sec_stat, PCI_STATUS_REC_TARGET_ABORT),
620              FLAG(sec_stat, PCI_STATUS_REC_MASTER_ABORT),
621              FLAG(sec_stat, PCI_STATUS_SIG_SYSTEM_ERROR),
622              FLAG(sec_stat, PCI_STATUS_DETECTED_PARITY));
623
624   show_rom(d, PCI_ROM_ADDRESS1);
625
626   if (verbose > 1)
627     {
628       printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c VGA16%c MAbort%c >Reset%c FastB2B%c\n",
629         FLAG(brc, PCI_BRIDGE_CTL_PARITY),
630         FLAG(brc, PCI_BRIDGE_CTL_SERR),
631         FLAG(brc, PCI_BRIDGE_CTL_NO_ISA),
632         FLAG(brc, PCI_BRIDGE_CTL_VGA),
633         FLAG(brc, PCI_BRIDGE_CTL_VGA_16BIT),
634         FLAG(brc, PCI_BRIDGE_CTL_MASTER_ABORT),
635         FLAG(brc, PCI_BRIDGE_CTL_BUS_RESET),
636         FLAG(brc, PCI_BRIDGE_CTL_FAST_BACK));
637       printf("\t\tPriDiscTmr%c SecDiscTmr%c DiscTmrStat%c DiscTmrSERREn%c\n",
638         FLAG(brc, PCI_BRIDGE_CTL_PRI_DISCARD_TIMER),
639         FLAG(brc, PCI_BRIDGE_CTL_SEC_DISCARD_TIMER),
640         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_STATUS),
641         FLAG(brc, PCI_BRIDGE_CTL_DISCARD_TIMER_SERR_EN));
642     }
643
644   show_caps(d, PCI_CAPABILITY_LIST);
645 }
646
647 static void
648 show_htype2(struct device *d)
649 {
650   int i;
651   word cmd = get_conf_word(d, PCI_COMMAND);
652   word brc = get_conf_word(d, PCI_CB_BRIDGE_CONTROL);
653   word exca;
654   int verb = verbose > 2;
655
656   show_bases(d, 1);
657   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
658          get_conf_byte(d, PCI_CB_PRIMARY_BUS),
659          get_conf_byte(d, PCI_CB_CARD_BUS),
660          get_conf_byte(d, PCI_CB_SUBORDINATE_BUS),
661          get_conf_byte(d, PCI_CB_LATENCY_TIMER));
662   for (i=0; i<2; i++)
663     {
664       int p = 8*i;
665       u32 base = get_conf_long(d, PCI_CB_MEMORY_BASE_0 + p);
666       u32 limit = get_conf_long(d, PCI_CB_MEMORY_LIMIT_0 + p);
667       limit = limit + 0xfff;
668       if (base <= limit || verb)
669         printf("\tMemory window %d: %08x-%08x%s%s\n", i, base, limit,
670                (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]",
671                (brc & (PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 << i)) ? " (prefetchable)" : "");
672     }
673   for (i=0; i<2; i++)
674     {
675       int p = 8*i;
676       u32 base = get_conf_long(d, PCI_CB_IO_BASE_0 + p);
677       u32 limit = get_conf_long(d, PCI_CB_IO_LIMIT_0 + p);
678       if (!(base & PCI_IO_RANGE_TYPE_32))
679         {
680           base &= 0xffff;
681           limit &= 0xffff;
682         }
683       base &= PCI_CB_IO_RANGE_MASK;
684       limit = (limit & PCI_CB_IO_RANGE_MASK) + 3;
685       if (base <= limit || verb)
686         printf("\tI/O window %d: %08x-%08x%s\n", i, base, limit,
687                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
688     }
689
690   if (get_conf_word(d, PCI_CB_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
691     printf("\tSecondary status: SERR\n");
692   if (verbose > 1)
693     printf("\tBridgeCtl: Parity%c SERR%c ISA%c VGA%c MAbort%c >Reset%c 16bInt%c PostWrite%c\n",
694            FLAG(brc, PCI_CB_BRIDGE_CTL_PARITY),
695            FLAG(brc, PCI_CB_BRIDGE_CTL_SERR),
696            FLAG(brc, PCI_CB_BRIDGE_CTL_ISA),
697            FLAG(brc, PCI_CB_BRIDGE_CTL_VGA),
698            FLAG(brc, PCI_CB_BRIDGE_CTL_MASTER_ABORT),
699            FLAG(brc, PCI_CB_BRIDGE_CTL_CB_RESET),
700            FLAG(brc, PCI_CB_BRIDGE_CTL_16BIT_INT),
701            FLAG(brc, PCI_CB_BRIDGE_CTL_POST_WRITES));
702
703   if (d->config_cached < 128)
704     {
705       printf("\t<access denied to the rest>\n");
706       return;
707     }
708
709   exca = get_conf_word(d, PCI_CB_LEGACY_MODE_BASE);
710   if (exca)
711     printf("\t16-bit legacy interface ports at %04x\n", exca);
712   show_caps(d, PCI_CB_CAPABILITY_LIST);
713 }
714
715 static void
716 show_verbose(struct device *d)
717 {
718   struct pci_dev *p = d->dev;
719   word status = get_conf_word(d, PCI_STATUS);
720   word cmd = get_conf_word(d, PCI_COMMAND);
721   word class = p->device_class;
722   byte bist = get_conf_byte(d, PCI_BIST);
723   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
724   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
725   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
726   byte max_lat, min_gnt;
727   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
728   unsigned int irq;
729   char *dt_node, *iommu_group;
730
731   show_terse(d);
732
733   pci_fill_info(p, PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES |
734     PCI_FILL_PHYS_SLOT | PCI_FILL_NUMA_NODE | PCI_FILL_DT_NODE | PCI_FILL_IOMMU_GROUP);
735   irq = p->irq;
736
737   switch (htype)
738     {
739     case PCI_HEADER_TYPE_NORMAL:
740       if (class == PCI_CLASS_BRIDGE_PCI)
741         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
742       max_lat = get_conf_byte(d, PCI_MAX_LAT);
743       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
744       break;
745     case PCI_HEADER_TYPE_BRIDGE:
746       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
747         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
748       min_gnt = max_lat = 0;
749       break;
750     case PCI_HEADER_TYPE_CARDBUS:
751       if ((class >> 8) != PCI_BASE_CLASS_BRIDGE)
752         printf("\t!!! Invalid class %04x for header type %02x\n", class, htype);
753       min_gnt = max_lat = 0;
754       break;
755     default:
756       printf("\t!!! Unknown header type %02x\n", htype);
757       return;
758     }
759
760   if (p->phy_slot)
761     printf("\tPhysical Slot: %s\n", p->phy_slot);
762
763   if (dt_node = pci_get_string_property(p, PCI_FILL_DT_NODE))
764     printf("\tDevice tree node: %s\n", dt_node);
765
766   if (verbose > 1)
767     {
768       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",
769              FLAG(cmd, PCI_COMMAND_IO),
770              FLAG(cmd, PCI_COMMAND_MEMORY),
771              FLAG(cmd, PCI_COMMAND_MASTER),
772              FLAG(cmd, PCI_COMMAND_SPECIAL),
773              FLAG(cmd, PCI_COMMAND_INVALIDATE),
774              FLAG(cmd, PCI_COMMAND_VGA_PALETTE),
775              FLAG(cmd, PCI_COMMAND_PARITY),
776              FLAG(cmd, PCI_COMMAND_WAIT),
777              FLAG(cmd, PCI_COMMAND_SERR),
778              FLAG(cmd, PCI_COMMAND_FAST_BACK),
779              FLAG(cmd, PCI_COMMAND_DISABLE_INTx));
780       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",
781              FLAG(status, PCI_STATUS_CAP_LIST),
782              FLAG(status, PCI_STATUS_66MHZ),
783              FLAG(status, PCI_STATUS_UDF),
784              FLAG(status, PCI_STATUS_FAST_BACK),
785              FLAG(status, PCI_STATUS_PARITY),
786              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
787              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
788              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
789              FLAG(status, PCI_STATUS_SIG_TARGET_ABORT),
790              FLAG(status, PCI_STATUS_REC_TARGET_ABORT),
791              FLAG(status, PCI_STATUS_REC_MASTER_ABORT),
792              FLAG(status, PCI_STATUS_SIG_SYSTEM_ERROR),
793              FLAG(status, PCI_STATUS_DETECTED_PARITY),
794              FLAG(status, PCI_STATUS_INTx));
795       if (cmd & PCI_COMMAND_MASTER)
796         {
797           printf("\tLatency: %d", latency);
798           if (min_gnt || max_lat)
799             {
800               printf(" (");
801               if (min_gnt)
802                 printf("%dns min", min_gnt*250);
803               if (min_gnt && max_lat)
804                 printf(", ");
805               if (max_lat)
806                 printf("%dns max", max_lat*250);
807               putchar(')');
808             }
809           if (cache_line)
810             printf(", Cache Line Size: %d bytes", cache_line * 4);
811           putchar('\n');
812         }
813       if (int_pin || irq)
814         printf("\tInterrupt: pin %c routed to IRQ " PCIIRQ_FMT "\n",
815                (int_pin ? 'A' + int_pin - 1 : '?'), irq);
816       if (p->numa_node != -1)
817         printf("\tNUMA node: %d\n", p->numa_node);
818       if (iommu_group = pci_get_string_property(p, PCI_FILL_IOMMU_GROUP))
819         printf("\tIOMMU group: %s\n", iommu_group);
820     }
821   else
822     {
823       printf("\tFlags: ");
824       if (cmd & PCI_COMMAND_MASTER)
825         printf("bus master, ");
826       if (cmd & PCI_COMMAND_VGA_PALETTE)
827         printf("VGA palette snoop, ");
828       if (cmd & PCI_COMMAND_WAIT)
829         printf("stepping, ");
830       if (cmd & PCI_COMMAND_FAST_BACK)
831         printf("fast Back2Back, ");
832       if (status & PCI_STATUS_66MHZ)
833         printf("66MHz, ");
834       if (status & PCI_STATUS_UDF)
835         printf("user-definable features, ");
836       printf("%s devsel",
837              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
838              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
839              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
840       if (cmd & PCI_COMMAND_MASTER)
841         printf(", latency %d", latency);
842       if (irq)
843         printf(", IRQ " PCIIRQ_FMT, irq);
844       if (p->numa_node != -1)
845         printf(", NUMA node %d", p->numa_node);
846       if (iommu_group = pci_get_string_property(p, PCI_FILL_IOMMU_GROUP))
847         printf(", IOMMU group %s", iommu_group);
848       putchar('\n');
849     }
850
851   if (bist & PCI_BIST_CAPABLE)
852     {
853       if (bist & PCI_BIST_START)
854         printf("\tBIST is running\n");
855       else
856         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
857     }
858
859   switch (htype)
860     {
861     case PCI_HEADER_TYPE_NORMAL:
862       show_htype0(d);
863       break;
864     case PCI_HEADER_TYPE_BRIDGE:
865       show_htype1(d);
866       break;
867     case PCI_HEADER_TYPE_CARDBUS:
868       show_htype2(d);
869       break;
870     }
871 }
872
873 /*** Machine-readable dumps ***/
874
875 static void
876 show_hex_dump(struct device *d)
877 {
878   unsigned int i, cnt;
879
880   cnt = d->config_cached;
881   if (opt_hex >= 3 && config_fetch(d, cnt, 256-cnt))
882     {
883       cnt = 256;
884       if (opt_hex >= 4 && config_fetch(d, 256, 4096-256))
885         cnt = 4096;
886     }
887
888   for (i=0; i<cnt; i++)
889     {
890       if (! (i & 15))
891         printf("%02x:", i);
892       printf(" %02x", get_conf_byte(d, i));
893       if ((i & 15) == 15)
894         putchar('\n');
895     }
896 }
897
898 static void
899 print_shell_escaped(char *c)
900 {
901   printf(" \"");
902   while (*c)
903     {
904       if (*c == '"' || *c == '\\')
905         putchar('\\');
906       putchar(*c++);
907     }
908   putchar('"');
909 }
910
911 static void
912 show_machine(struct device *d)
913 {
914   struct pci_dev *p = d->dev;
915   int c;
916   word sv_id, sd_id;
917   char classbuf[128], vendbuf[128], devbuf[128], svbuf[128], sdbuf[128];
918   char *dt_node, *iommu_group;
919
920   get_subid(d, &sv_id, &sd_id);
921
922   if (verbose)
923     {
924       pci_fill_info(p, PCI_FILL_PHYS_SLOT | PCI_FILL_NUMA_NODE | PCI_FILL_DT_NODE | PCI_FILL_IOMMU_GROUP);
925       printf((opt_machine >= 2) ? "Slot:\t" : "Device:\t");
926       show_slot_name(d);
927       putchar('\n');
928       printf("Class:\t%s\n",
929              pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
930       printf("Vendor:\t%s\n",
931              pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
932       printf("Device:\t%s\n",
933              pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
934       if (sv_id && sv_id != 0xffff)
935         {
936           printf("SVendor:\t%s\n",
937                  pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
938           printf("SDevice:\t%s\n",
939                  pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
940         }
941       if (p->phy_slot)
942         printf("PhySlot:\t%s\n", p->phy_slot);
943       if (c = get_conf_byte(d, PCI_REVISION_ID))
944         printf("Rev:\t%02x\n", c);
945       if (c = get_conf_byte(d, PCI_CLASS_PROG))
946         printf("ProgIf:\t%02x\n", c);
947       if (opt_kernel)
948         show_kernel_machine(d);
949       if (p->numa_node != -1)
950         printf("NUMANode:\t%d\n", p->numa_node);
951       if (dt_node = pci_get_string_property(p, PCI_FILL_DT_NODE))
952         printf("DTNode:\t%s\n", dt_node);
953       if (iommu_group = pci_get_string_property(p, PCI_FILL_IOMMU_GROUP))
954         printf("IOMMUGroup:\t%s\n", iommu_group);
955     }
956   else
957     {
958       show_slot_name(d);
959       print_shell_escaped(pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, p->device_class));
960       print_shell_escaped(pci_lookup_name(pacc, vendbuf, sizeof(vendbuf), PCI_LOOKUP_VENDOR, p->vendor_id, p->device_id));
961       print_shell_escaped(pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id));
962       if (c = get_conf_byte(d, PCI_REVISION_ID))
963         printf(" -r%02x", c);
964       if (c = get_conf_byte(d, PCI_CLASS_PROG))
965         printf(" -p%02x", c);
966       if (sv_id && sv_id != 0xffff)
967         {
968           print_shell_escaped(pci_lookup_name(pacc, svbuf, sizeof(svbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_VENDOR, sv_id));
969           print_shell_escaped(pci_lookup_name(pacc, sdbuf, sizeof(sdbuf), PCI_LOOKUP_SUBSYSTEM | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id, sv_id, sd_id));
970         }
971       else
972         printf(" \"\" \"\"");
973       putchar('\n');
974     }
975 }
976
977 /*** Main show function ***/
978
979 void
980 show_device(struct device *d)
981 {
982   if (opt_machine)
983     show_machine(d);
984   else
985     {
986       if (verbose)
987         show_verbose(d);
988       else
989         show_terse(d);
990       if (opt_kernel || verbose)
991         show_kernel(d);
992     }
993   if (opt_hex)
994     show_hex_dump(d);
995   if (verbose || opt_hex)
996     putchar('\n');
997 }
998
999 static void
1000 show(void)
1001 {
1002   struct device *d;
1003
1004   for (d=first_dev; d; d=d->next)
1005     if (pci_filter_match(&filter, d->dev))
1006       show_device(d);
1007 }
1008
1009 /* Main */
1010
1011 int
1012 main(int argc, char **argv)
1013 {
1014   int i;
1015   char *msg;
1016
1017   if (argc == 2 && !strcmp(argv[1], "--version"))
1018     {
1019       puts("lspci version " PCIUTILS_VERSION);
1020       return 0;
1021     }
1022
1023   pacc = pci_alloc();
1024   pacc->error = die;
1025   pci_filter_init(pacc, &filter);
1026
1027   while ((i = getopt(argc, argv, options)) != -1)
1028     switch (i)
1029       {
1030       case 'n':
1031         pacc->numeric_ids++;
1032         break;
1033       case 'v':
1034         verbose++;
1035         break;
1036       case 'b':
1037         pacc->buscentric = 1;
1038         break;
1039       case 's':
1040         if (msg = pci_filter_parse_slot(&filter, optarg))
1041           die("-s: %s", msg);
1042         opt_filter = 1;
1043         break;
1044       case 'd':
1045         if (msg = pci_filter_parse_id(&filter, optarg))
1046           die("-d: %s", msg);
1047         opt_filter = 1;
1048         break;
1049       case 'x':
1050         opt_hex++;
1051         break;
1052       case 'P':
1053         opt_path++;
1054         need_topology = 1;
1055         break;
1056       case 't':
1057         opt_tree++;
1058         need_topology = 1;
1059         break;
1060       case 'i':
1061         pci_set_name_list_path(pacc, optarg, 0);
1062         break;
1063       case 'm':
1064         opt_machine++;
1065         break;
1066       case 'p':
1067         opt_pcimap = optarg;
1068         break;
1069 #ifdef PCI_OS_LINUX
1070       case 'k':
1071         opt_kernel++;
1072         break;
1073 #endif
1074       case 'M':
1075         opt_map_mode++;
1076         break;
1077       case 'D':
1078         opt_domains = 2;
1079         break;
1080 #ifdef PCI_USE_DNS
1081       case 'q':
1082         opt_query_dns++;
1083         break;
1084       case 'Q':
1085         opt_query_all = 1;
1086         break;
1087 #else
1088       case 'q':
1089       case 'Q':
1090         die("DNS queries are not available in this version");
1091 #endif
1092       default:
1093         if (parse_generic_option(i, pacc, optarg))
1094           break;
1095       bad:
1096         fprintf(stderr, help_msg, pacc->id_file_name);
1097         return 1;
1098       }
1099   if (optind < argc)
1100     goto bad;
1101
1102   if (opt_query_dns)
1103     {
1104       pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK;
1105       if (opt_query_dns > 1)
1106         pacc->id_lookup_mode |= PCI_LOOKUP_REFRESH_CACHE;
1107     }
1108   if (opt_query_all)
1109     pacc->id_lookup_mode |= PCI_LOOKUP_NETWORK | PCI_LOOKUP_SKIP_LOCAL;
1110
1111   pci_init(pacc);
1112   if (opt_map_mode)
1113     {
1114       if (need_topology)
1115         die("Bus mapping mode does not recognize bus topology");
1116       map_the_bus();
1117     }
1118   else
1119     {
1120       scan_devices();
1121       sort_them();
1122       if (need_topology)
1123         grow_tree();
1124       if (opt_tree)
1125         show_forest(opt_filter ? &filter : NULL);
1126       else
1127         show();
1128     }
1129   show_kernel_cleanup();
1130   pci_cleanup(pacc);
1131
1132   return (seen_errors ? 2 : 0);
1133 }