]> mj.ucw.cz Git - pciutils.git/blob - lspci.c
c2a986dce8816146a045aa2f3ba2c64ddacbbb30
[pciutils.git] / lspci.c
1 /*
2  *      $Id: lspci.c,v 1.1 1997/12/23 10:29:18 mj Exp $
3  *
4  *      Linux PCI Utilities -- List All PCI Devices
5  *
6  *      Copyright (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.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 <fcntl.h>
15 #include <unistd.h>
16 #include <linux/pci.h>
17
18 #include "pciutils.h"
19
20 /* Options */
21
22 static int verbose;                     /* Show detailed information */
23 static int buscentric_view;             /* Show bus addresses/IRQ's instead of CPU-visible ones */
24 static int show_hex;                    /* Show contents of config space as hexadecimal numbers */
25 static int bus_filter = -1;             /* Bus, slot, function, vendor and device ID filtering */
26 static int slot_filter = -1;
27 static int func_filter = -1;
28 static int vend_filter = -1;
29 static int dev_filter = -1;
30
31 static char options[] = "nvbxB:S:F:V:D:";
32
33 static char help_msg[] = "\
34 Usage: lspci [<switches>]\n\
35 \n\
36 -v\tBe verbose\n\
37 -n\tShow numeric ID's\n\
38 -b\tBus-centric view (PCI addresses and IRQ's instead of those seen by the CPU)\n\
39 -x\tShow hex-dump of config space (-xx shows full 256 bytes)\n\
40 -B <bus>, -S <slot>, -F <func>, -V <vendor>, -D <device>  Show only selected devices\n\
41 ";
42
43 /* Our view of the PCI bus */
44
45 struct device {
46   struct device *next;
47   byte bus, devfn;
48   word vendid, devid;
49   unsigned int kernel_irq;
50   unsigned long kernel_base_addr[6];
51   byte config[256];
52 };
53
54 static struct device *first_dev, **last_dev = &first_dev;
55
56 /* Miscellaneous routines */
57
58 void *
59 xmalloc(unsigned int howmuch)
60 {
61   void *p = malloc(howmuch);
62   if (!p)
63     {
64       fprintf(stderr, "lspci: Unable to allocate %d bytes of memory\n", howmuch);
65       exit(1);
66     }
67   return p;
68 }
69
70 /* Filtering */
71
72 static inline int
73 filter_out(struct device *d)
74 {
75   return (bus_filter >= 0 && d->bus != bus_filter ||
76           slot_filter >= 0 && PCI_SLOT(d->devfn) != slot_filter ||
77           func_filter >= 0 && PCI_FUNC(d->devfn) != func_filter ||
78           vend_filter >= 0 && d->vendid != vend_filter ||
79           dev_filter >= 0 && d->devid != dev_filter);
80 }
81
82 /* Interface for /proc/bus/pci */
83
84 static void
85 scan_dev_list(void)
86 {
87   FILE *f;
88   byte line[256];
89
90   if (! (f = fopen(PROC_BUS_PCI "/devices", "r")))
91     {
92       perror("Unable to open " PROC_BUS_PCI "/devices");
93       exit(1);
94     }
95   while (fgets(line, sizeof(line), f))
96     {
97       struct device *d = xmalloc(sizeof(struct device));
98       unsigned int dfn, vend;
99
100       sscanf(line, "%x %x %x %lx %lx %lx %lx %lx %lx",
101              &dfn,
102              &vend,
103              &d->kernel_irq,
104              &d->kernel_base_addr[0],
105              &d->kernel_base_addr[1],
106              &d->kernel_base_addr[2],
107              &d->kernel_base_addr[3],
108              &d->kernel_base_addr[4],
109              &d->kernel_base_addr[5]);
110       d->bus = dfn >> 8U;
111       d->devfn = dfn & 0xff;
112       d->vendid = vend >> 16U;
113       d->devid = vend & 0xffff;
114       if (!filter_out(d))
115         {
116           *last_dev = d;
117           last_dev = &d->next;
118           d->next = NULL;
119         }
120     }
121   fclose(f);
122 }
123
124 static inline void
125 make_proc_pci_name(struct device *d, char *p)
126 {
127   sprintf(p, PROC_BUS_PCI "/%02x/%02x.%x",
128           d->bus, PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
129 }
130
131 static void
132 scan_config(void)
133 {
134   struct device *d;
135   char name[64];
136   int fd;
137   int how_much = (show_hex > 1) ? 256 : 64;
138
139   for(d=first_dev; d; d=d->next)
140     {
141       make_proc_pci_name(d, name);
142       if ((fd = open(name, O_RDONLY)) < 0)
143         {
144           fprintf(stderr, "lspci: Unable to open %s: %m\n", name);
145           exit(1);
146         }
147       if (read(fd, d->config, how_much) != how_much)
148         {
149           fprintf(stderr, "lspci: Error reading %s: %m\n", name);
150           exit(1);
151         }
152       close(fd);
153     }
154 }
155
156 static void
157 scan_proc(void)
158 {
159   scan_dev_list();
160   scan_config();
161 }
162
163 /* Config space accesses */
164
165 static inline byte
166 get_conf_byte(struct device *d, unsigned int pos)
167 {
168   return d->config[pos];
169 }
170
171 static word
172 get_conf_word(struct device *d, unsigned int pos)
173 {
174   return d->config[pos] | (d->config[pos+1] << 8);
175 }
176
177 static u32
178 get_conf_long(struct device *d, unsigned int pos)
179 {
180   return d->config[pos] |
181     (d->config[pos+1] << 8) |
182     (d->config[pos+2] << 16) |
183     (d->config[pos+3] << 24);
184 }
185
186 /* Sorting */
187
188 static int
189 compare_them(const void *A, const void *B)
190 {
191   const struct device *a = *(const struct device **)A;
192   const struct device *b = *(const struct device **)B;
193
194   if (a->bus < b->bus)
195     return -1;
196   if (a->bus > b->bus)
197     return 1;
198   if (a->devfn < b->devfn)
199     return -1;
200   if (a->devfn > b->devfn)
201     return 1;
202   return 0;
203 }
204
205 static void
206 sort_them(void)
207 {
208   struct device **index, **h;
209   int cnt;
210   struct device *d;
211
212   cnt = 0;
213   for(d=first_dev; d; d=d->next)
214     cnt++;
215   h = index = alloca(sizeof(struct device *) * cnt);
216   for(d=first_dev; d; d=d->next)
217     *h++ = d;
218   qsort(index, cnt, sizeof(struct device *), compare_them);
219   last_dev = &first_dev;
220   h = index;
221   while (cnt--)
222     {
223       *last_dev = *h;
224       last_dev = &(*h)->next;
225       h++;
226     }
227   *last_dev = NULL;
228 }
229
230 /* Output */
231
232 static void
233 show_terse(struct device *d)
234 {
235   int c;
236
237   printf("%02x:%02x.%x %s: %s",
238          d->bus,
239          PCI_SLOT(d->devfn),
240          PCI_FUNC(d->devfn),
241          lookup_class(get_conf_word(d, PCI_CLASS_DEVICE)),
242          lookup_device_full(d->vendid, d->devid));
243   if (c = get_conf_byte(d, PCI_REVISION_ID))
244     printf(" (rev %02x)", c);
245   if (verbose && (c = get_conf_byte(d, PCI_CLASS_PROG)))
246     printf(" (prog-if %02x)", c);
247   putchar('\n');
248 }
249
250 static void
251 show_bases(struct device *d, int cnt)
252 {
253   word cmd = get_conf_word(d, PCI_COMMAND);
254   int i;
255
256   for(i=0; i<6; i++)
257     {
258       unsigned long pos;
259       unsigned int flg = get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i);
260       if (buscentric_view)
261         pos = flg;
262       else
263         pos = d->kernel_base_addr[i];
264       if (!pos || pos == 0xffffffff)
265         continue;
266       if (verbose > 1)
267         printf("\tRegion %d: ", i);
268       else
269         putchar('\t');
270       if (flg & PCI_BASE_ADDRESS_SPACE_IO)
271         printf("I/O ports at %04lx%s\n",
272                pos & PCI_BASE_ADDRESS_IO_MASK,
273                (cmd & PCI_COMMAND_IO) ? "" : " [disabled]");
274       else
275         {
276           int t = flg & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
277           printf("Memory at ");
278           if (t == PCI_BASE_ADDRESS_MEM_TYPE_64)
279             {
280               if (i < cnt - 1)
281                 {
282                   i++;
283                   if (!buscentric_view)
284                     printf("%08x", get_conf_long(d, PCI_BASE_ADDRESS_0 + 4*i));
285                 }
286               else
287                 printf("????????");
288             }
289           printf("%08lx (%s, %sprefetchable)%s\n",
290                  pos & PCI_BASE_ADDRESS_MEM_MASK,
291                  (t == PCI_BASE_ADDRESS_MEM_TYPE_32) ? "32-bit" :
292                  (t == PCI_BASE_ADDRESS_MEM_TYPE_64) ? "64-bit" :
293                  (t == PCI_BASE_ADDRESS_MEM_TYPE_1M) ? "low-1M 32-bit" : "???",
294                  (flg & PCI_BASE_ADDRESS_MEM_PREFETCH) ? "" : "non-",
295                  (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
296         }
297     }
298 }
299
300 static void
301 show_htype0(struct device *d)
302 {
303   u32 rom = get_conf_long(d, PCI_ROM_ADDRESS);
304
305   show_bases(d, 6);
306
307   if (rom & 1)
308     {
309       word cmd = get_conf_word(d, PCI_COMMAND);
310       printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
311              (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
312     }
313 }
314
315 static void
316 show_htype1(struct device *d)
317 {
318   u32 io_base = get_conf_byte(d, PCI_IO_BASE);
319   u32 io_limit = get_conf_byte(d, PCI_IO_LIMIT);
320   u32 io_type = io_base & PCI_IO_RANGE_TYPE_MASK;
321   u32 mem_base = get_conf_word(d, PCI_MEMORY_BASE);
322   u32 mem_limit = get_conf_word(d, PCI_MEMORY_LIMIT);
323   u32 mem_type = mem_base & PCI_MEMORY_RANGE_TYPE_MASK;
324   u32 pref_base = get_conf_word(d, PCI_PREF_MEMORY_BASE);
325   u32 pref_limit = get_conf_word(d, PCI_PREF_MEMORY_LIMIT);
326   u32 pref_type = pref_base & PCI_PREF_RANGE_TYPE_MASK;
327   u32 rom = get_conf_long(d, PCI_ROM_ADDRESS1);
328   word brc = get_conf_word(d, PCI_BRIDGE_CONTROL);
329
330   show_bases(d, 2);
331   printf("\tBus: primary=%02x, secondary=%02x, subordinate=%02x, sec-latency=%d\n",
332          get_conf_byte(d, PCI_PRIMARY_BUS),
333          get_conf_byte(d, PCI_SECONDARY_BUS),
334          get_conf_byte(d, PCI_SUBORDINATE_BUS),
335          get_conf_byte(d, PCI_SEC_LATENCY_TIMER));
336
337   if (io_type != (io_limit & PCI_IO_RANGE_TYPE_MASK) ||
338       (io_type != PCI_IO_RANGE_TYPE_16 && io_type != PCI_IO_RANGE_TYPE_32))
339     printf("\t!!! Unknown I/O range types %x/%x\n", io_base, io_limit);
340   else
341     {
342       io_base = (io_base & PCI_IO_RANGE_MASK) << 8;
343       io_limit = (io_limit & PCI_IO_RANGE_MASK) << 8;
344       if (io_type == PCI_IO_RANGE_TYPE_32)
345         {
346           io_base |= (get_conf_word(d, PCI_IO_BASE_UPPER16) << 16);
347           io_limit |= (get_conf_word(d, PCI_IO_LIMIT_UPPER16) << 16);
348         }
349       if (io_base)
350         printf("\tI/O behind bridge: %08x-%08x\n", io_base, io_limit+0xfff);
351     }
352
353   if (mem_type != (mem_limit & PCI_MEMORY_RANGE_TYPE_MASK) ||
354       mem_type)
355     printf("\t!!! Unknown memory range types %x/%x\n", mem_base, mem_limit);
356   else if (mem_base)
357     {
358       mem_base = (mem_base & PCI_MEMORY_RANGE_MASK) << 16;
359       mem_limit = (mem_limit & PCI_MEMORY_RANGE_MASK) << 16;
360       printf("\tMemory behind bridge: %08x-%08x\n", mem_base, mem_limit + 0xfffff);
361     }
362
363   if (pref_type != (pref_limit & PCI_PREF_RANGE_TYPE_MASK) ||
364       (pref_type != PCI_PREF_RANGE_TYPE_32 && pref_type != PCI_PREF_RANGE_TYPE_64))
365     printf("\t!!! Unknown prefetchable memory range types %x/%x\n", pref_base, pref_limit);
366   else if (pref_base)
367     {
368       pref_base = (pref_base & PCI_PREF_RANGE_MASK) << 16;
369       pref_limit = (pref_limit & PCI_PREF_RANGE_MASK) << 16;
370       if (pref_type == PCI_PREF_RANGE_TYPE_32)
371         printf("\tPrefetchable memory behind bridge: %08x-%08x\n", pref_base, pref_limit);
372       else
373         printf("\tPrefetchable memory behind bridge: %08x%08x-%08x%08x\n",
374                get_conf_long(d, PCI_PREF_BASE_UPPER32),
375                pref_base,
376                get_conf_long(d, PCI_PREF_LIMIT_UPPER32),
377                pref_limit);
378     }
379
380   if (get_conf_word(d, PCI_SEC_STATUS) & PCI_STATUS_SIG_SYSTEM_ERROR)
381     printf("\tSecondary status: SERR\n");
382
383   if (rom & 1)
384     {
385       word cmd = get_conf_word(d, PCI_COMMAND);
386       printf("\tExpansion ROM at %08x%s\n", rom & ~0xfff,
387              (cmd & PCI_COMMAND_MEMORY) ? "" : " [disabled]");
388     }
389
390   if (verbose > 1)
391     printf("\tBridgeCtl: Parity%c SERR%c NoISA%c VGA%c MAbort%c >Reset%c FastB2B%c\n",
392            (brc & PCI_BRIDGE_CTL_PARITY) ? '+' : '-',
393            (brc & PCI_BRIDGE_CTL_SERR) ? '+' : '-',
394            (brc & PCI_BRIDGE_CTL_NO_ISA) ? '+' : '-',
395            (brc & PCI_BRIDGE_CTL_VGA) ? '+' : '-',
396            (brc & PCI_BRIDGE_CTL_MASTER_ABORT) ? '+' : '-',
397            (brc & PCI_BRIDGE_CTL_BUS_RESET) ? '+' : '-',
398            (brc & PCI_BRIDGE_CTL_FAST_BACK) ? '+' : '-');
399 }
400
401 static void
402 show_verbose(struct device *d)
403 {
404   word status = get_conf_word(d, PCI_STATUS);
405   word cmd = get_conf_word(d, PCI_COMMAND);
406   word class = get_conf_word(d, PCI_CLASS_DEVICE);
407   byte bist = get_conf_byte(d, PCI_BIST);
408   byte htype = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
409   byte latency = get_conf_byte(d, PCI_LATENCY_TIMER);
410   byte cache_line = get_conf_byte(d, PCI_CACHE_LINE_SIZE);
411   byte max_lat, min_gnt;
412   byte int_pin = get_conf_byte(d, PCI_INTERRUPT_PIN);
413   byte int_line = get_conf_byte(d, PCI_INTERRUPT_LINE);
414   unsigned int irq, ex_htype;
415   word subsys_v, subsys_d;
416
417   show_terse(d);
418
419   switch (class)
420     {
421     case PCI_CLASS_BRIDGE_PCI:
422       ex_htype = 1;
423       break;
424     default:
425       ex_htype = 0;
426     }
427   if (ex_htype != htype)
428     {
429       printf("\t!!! Header type %02x doesn't match class code %04x\n", htype, class);
430       return;
431     }
432
433   switch (htype)
434     {
435     case 0:
436       max_lat = get_conf_byte(d, PCI_MAX_LAT);
437       min_gnt = get_conf_byte(d, PCI_MIN_GNT);
438       subsys_v = get_conf_word(d, PCI_SUBSYSTEM_VENDOR_ID);
439       subsys_d = get_conf_word(d, PCI_SUBSYSTEM_ID);
440       break;
441     case 1:
442       irq = int_line = int_pin = min_gnt = max_lat = 0;
443       subsys_v = subsys_d = 0;
444       break;
445     default:
446       printf("\t!!! Unknown header type %02x\n", htype);
447       return;
448     }
449
450   if (buscentric_view)
451     irq = int_line;
452   else
453     irq = d->kernel_irq;
454
455   if (verbose > 1)
456     {
457       if (subsys_v)
458         printf("\tSubsystem ID: %04x:%04x\n", subsys_v, subsys_d);
459       printf("\tControl: I/O%c Mem%c BusMaster%c SpecCycle%c MemWINV%c VGASnoop%c ParErr%c Stepping%c SERR%c FastB2B%c\n",
460              (cmd & PCI_COMMAND_IO) ? '+' : '-',
461              (cmd & PCI_COMMAND_MEMORY) ? '+' : '-',
462              (cmd & PCI_COMMAND_MASTER) ? '+' : '-',
463              (cmd & PCI_COMMAND_SPECIAL) ? '+' : '-',
464              (cmd & PCI_COMMAND_INVALIDATE) ? '+' : '-',
465              (cmd & PCI_COMMAND_VGA_PALETTE) ? '+' : '-',
466              (cmd & PCI_COMMAND_PARITY) ? '+' : '-',
467              (cmd & PCI_COMMAND_WAIT) ? '+' : '-',
468              (cmd & PCI_COMMAND_SERR) ? '+' : '-',
469              (cmd & PCI_COMMAND_FAST_BACK) ? '+' : '-');
470       printf("\tStatus: 66Mhz%c UDF%c FastB2B%c ParErr%c DEVSEL=%s >TAbort%c <TAbort%c <MAbort%c >SERR%c <PERR%c\n",
471              (status & PCI_STATUS_66MHZ) ? '+' : '-',
472              (status & PCI_STATUS_UDF) ? '+' : '-',
473              (status & PCI_STATUS_FAST_BACK) ? '+' : '-',
474              (status & PCI_STATUS_PARITY) ? '+' : '-',
475              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
476              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
477              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??",
478              (status & PCI_STATUS_SIG_TARGET_ABORT) ? '+' : '-',
479              (status & PCI_STATUS_REC_TARGET_ABORT) ? '+' : '-',
480              (status & PCI_STATUS_REC_MASTER_ABORT) ? '+' : '-',
481              (status & PCI_STATUS_SIG_SYSTEM_ERROR) ? '+' : '-',
482              (status & PCI_STATUS_DETECTED_PARITY) ? '+' : '-');
483       if (cmd & PCI_COMMAND_MASTER)
484         {
485           printf("\tLatency: ");
486           if (min_gnt)
487             printf("%d min, ", min_gnt);
488           if (max_lat)
489             printf("%d max, ", max_lat);
490           printf("%d set", latency);
491           if (cache_line)
492             printf(", cache line size %02x", cache_line);
493           putchar('\n');
494         }
495       if (int_pin)
496         printf("\tInterrupt: pin %c routed to IRQ %d\n", 'A' + int_pin - 1, irq);
497     }
498   else
499     {
500       printf("\tFlags: ");
501       if (cmd & PCI_COMMAND_MASTER)
502         printf("bus master, ");
503       if (cmd & PCI_COMMAND_VGA_PALETTE)
504         printf("VGA palette snoop, ");
505       if (cmd & PCI_COMMAND_WAIT)
506         printf("stepping, ");
507       if (cmd & PCI_COMMAND_FAST_BACK)
508         printf("fast Back2Back, ");
509       if (status & PCI_STATUS_66MHZ)
510         printf("66Mhz, ");
511       if (status & PCI_STATUS_UDF)
512         printf("user-definable features, ");
513       printf("%s devsel",
514              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_SLOW) ? "slow" :
515              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_MEDIUM) ? "medium" :
516              ((status & PCI_STATUS_DEVSEL_MASK) == PCI_STATUS_DEVSEL_FAST) ? "fast" : "??");
517       if (cmd & PCI_COMMAND_MASTER)
518         printf(", latency %d", latency);
519       if (int_pin)
520         if (d->kernel_irq)
521           printf(", IRQ %d", irq);
522         else
523           printf(", IRQ ?");
524       putchar('\n');
525     }
526
527   if (bist & PCI_BIST_CAPABLE)
528     {
529       if (bist & PCI_BIST_START)
530         printf("\tBIST is running\n");
531       else
532         printf("\tBIST result: %02x\n", bist & PCI_BIST_CODE_MASK);
533     }
534
535   switch (htype)
536     {
537     case 0:
538       show_htype0(d);
539       break;
540     case 1:
541       show_htype1(d);
542       break;
543     }
544 }
545
546 static void
547 show_hex_dump(struct device *d)
548 {
549   int i;
550   int limit = (show_hex > 1) ? 256 : 64;
551
552   for(i=0; i<limit; i++)
553     {
554       if (! (i & 15))
555         printf("%02x:", i);
556       printf(" %02x", get_conf_byte(d, i));
557       if ((i & 15) == 15)
558         putchar('\n');
559     }
560 }
561
562 static void
563 show(void)
564 {
565   struct device *d;
566
567   for(d=first_dev; d; d=d->next)
568     {
569       if (verbose)
570         show_verbose(d);
571       else
572         show_terse(d);
573       if (show_hex)
574         show_hex_dump(d);
575       if (verbose || show_hex)
576         putchar('\n');
577     }
578 }
579
580 /* Main */
581
582 int
583 main(int argc, char **argv)
584 {
585   int i;
586
587   while ((i = getopt(argc, argv, options)) != -1)
588     switch (i)
589       {
590       case 'n':
591         show_numeric_ids = 1;
592         break;
593       case 'v':
594         verbose++;
595         break;
596       case 'b':
597         buscentric_view = 1;
598         break;
599       case 'B':
600         bus_filter = strtol(optarg, NULL, 16);
601         break;
602       case 'S':
603         slot_filter = strtol(optarg, NULL, 16);
604         break;
605       case 'F':
606         func_filter = strtol(optarg, NULL, 16);
607         break;
608       case 'V':
609         vend_filter = strtol(optarg, NULL, 16);
610         break;
611       case 'D':
612         dev_filter = strtol(optarg, NULL, 16);
613         break;
614       case 'x':
615         show_hex++;
616         break;
617       default:
618       bad:
619         fprintf(stderr, help_msg);
620         return 1;
621       }
622   if (optind < argc)
623     goto bad;
624
625   scan_proc();
626   sort_them();
627   show();
628
629   return 0;
630 }