scanpci: print meaningful info on BASEROM
[platform/upstream/libpciaccess.git] / scanpci / scanpci.c
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <inttypes.h>
33
34 #ifdef HAVE_ERR_H
35 #include <err.h>
36 #else
37 # include <errno.h>
38 # include <string.h>
39 # define err(exitcode, format, args...) \
40    errx(exitcode, format ": %s", ## args, strerror(errno))
41 # define errx(exitcode, format, args...) \
42    { warnx(format, ## args); exit(exitcode); }
43 # define warn(format, args...) \
44    warnx(format ": %s", ## args, strerror(errno))
45 # define warnx(format, args...) \
46    fprintf(stderr, format "\n", ## args)
47 #endif
48
49 #include "pciaccess.h"
50 #include "pciaccess_private.h"
51
52
53 static void
54 print_pci_bridge( const struct pci_bridge_info * info )
55 {
56     printf( "  Bus: primary=%02"PRIx8", secondary=%02"PRIx8", subordinate=%02"PRIx8", "
57             "sec-latency=%"PRIu8"\n",
58             info->primary_bus,
59             info->secondary_bus,
60             info->subordinate_bus,
61             info->secondary_latency_timer );
62     printf( "  I/O behind bridge: %08"PRIx32"-%08"PRIx32"\n",
63             info->io_base,
64             info->io_limit );
65     printf( "  Memory behind bridge: %08"PRIx32"-%08"PRIx32"\n",
66             info->mem_base,
67             info->mem_limit );
68     printf( "  Prefetchable memory behind bridge: %08"PRIx64"-%08"PRIx64"\n",
69             info->prefetch_mem_base,
70             info->prefetch_mem_limit );
71 }
72
73 static void
74 print_pci_device( struct pci_device * dev, int verbose )
75 {
76     const char * dev_name;
77     const char * vend_name;
78
79     vend_name = pci_device_get_vendor_name( dev );
80     dev_name = pci_device_get_device_name( dev );
81     if ( dev_name == NULL ) {
82         dev_name = "Device unknown";
83     }
84
85     printf("\npci ");
86     if (dev->domain != 0)
87         printf("domain 0x%04x ", dev->domain);
88     printf("bus 0x%04x cardnum 0x%02x function 0x%02x:"
89            " vendor 0x%04x device 0x%04x\n",
90            dev->bus,
91            dev->dev,
92            dev->func,
93            dev->vendor_id,
94            dev->device_id );
95     if ( vend_name != NULL ) {
96         printf( " %s %s\n", vend_name, dev_name );
97     }
98     else {
99         printf( " %s\n", dev_name );
100     }
101
102     if ( verbose ) {
103         unsigned   i;
104         uint16_t  command, status;
105         uint8_t   bist;
106         uint8_t   header_type;
107         uint8_t   latency_timer;
108         uint8_t   cache_line_size;
109         uint8_t   max_latency;
110         uint8_t   min_grant;
111         uint8_t   int_pin;
112
113
114         vend_name = pci_device_get_subvendor_name( dev );
115         dev_name = pci_device_get_subdevice_name( dev );
116         if ( dev_name == NULL ) {
117             dev_name = "Card unknown";
118         }
119
120         printf( " CardVendor 0x%04x card 0x%04x (",
121                 dev->subvendor_id,
122                 dev->subdevice_id );
123         if ( vend_name != NULL ) {
124             printf( "%s, %s)\n", vend_name, dev_name );
125         }
126         else {
127             printf( "%s)\n", dev_name );
128         }
129
130         pci_device_cfg_read_u16( dev, & command, 4 );
131         pci_device_cfg_read_u16( dev, & status,  6 );
132         printf( "  STATUS    0x%04x  COMMAND 0x%04x\n",
133                 status,
134                 command );
135         printf( "  CLASS     0x%02x 0x%02x 0x%02x  REVISION 0x%02x\n",
136                 (dev->device_class >> 16) & 0x0ff,
137                 (dev->device_class >>  8) & 0x0ff,
138                 (dev->device_class >>  0) & 0x0ff,
139                 dev->revision );
140
141         pci_device_cfg_read_u8( dev, & cache_line_size, 12 );
142         pci_device_cfg_read_u8( dev, & latency_timer, 13 );
143         pci_device_cfg_read_u8( dev, & header_type, 14 );
144         pci_device_cfg_read_u8( dev, & bist, 15 );
145
146         printf( "  BIST      0x%02x  HEADER 0x%02x  LATENCY 0x%02x  CACHE 0x%02x\n",
147                 bist,
148                 header_type,
149                 latency_timer,
150                 cache_line_size );
151
152         pci_device_probe( dev );
153         for ( i = 0 ; i < 6 ; i++ ) {
154             if ( dev->regions[i].base_addr != 0 ) {
155                 printf( "  BASE%u     0x%08"PRIxPTR" SIZE %zu  %s",
156                         i,
157                         (intptr_t) dev->regions[i].base_addr,
158                         (size_t) dev->regions[i].size,
159                         (dev->regions[i].is_IO) ? "I/O" : "MEM" );
160
161                 if ( ! dev->regions[i].is_IO ) {
162                     if ( dev->regions[i].is_prefetchable ) {
163                         printf( " PREFETCHABLE" );
164                     }
165                 }
166
167                 printf( "\n" );
168             }
169         }
170
171         if ( dev->rom_size ) {
172             struct pci_device_private *priv =
173                 (struct pci_device_private *) dev;
174
175                 printf( "  BASEROM   0x%08"PRIxPTR" SIZE %zu\n",
176                         (intptr_t) priv->rom_base, (size_t) dev->rom_size);
177         }
178
179         pci_device_cfg_read_u8( dev, & int_pin, 61 );
180         pci_device_cfg_read_u8( dev, & min_grant, 62 );
181         pci_device_cfg_read_u8( dev, & max_latency, 63 );
182
183         printf( "  MAX_LAT   0x%02x  MIN_GNT 0x%02x  INT_PIN 0x%02x  INT_LINE 0x%02x\n",
184                 max_latency,
185                 min_grant,
186                 int_pin,
187                 dev->irq );
188
189         if ( (dev->device_class >> 16) == 0x06 ) {
190             const void * info;
191
192             if ( (info = pci_device_get_bridge_info(dev)) != NULL ) {
193                 print_pci_bridge( (const struct pci_bridge_info *) info );
194             }
195             else if ( (info = pci_device_get_pcmcia_bridge_info(dev)) != NULL ) {
196                 /* Nothing yet. */
197             }
198         }
199     }
200 }
201
202
203 int main( int argc, char ** argv )
204 {
205     struct pci_device_iterator * iter;
206     struct pci_device * dev;
207     int ret;
208     int verbose = 0;
209     int c;
210     int errors = 0;
211
212     while ((c = getopt(argc, argv, "v")) != -1) {
213         switch (c) {
214         case 'v':
215             verbose = 1;
216             break;
217         case '?':
218             errors++;
219         }
220     }
221     if (errors != 0) {
222         fprintf(stderr, "usage: %s [-v]\n", argv[0]);
223         exit(2);
224     }
225
226     ret = pci_system_init();
227     if (ret != 0)
228         err(1, "Couldn't initialize PCI system");
229
230     iter = pci_slot_match_iterator_create( NULL );
231
232     while ( (dev = pci_device_next( iter )) != NULL ) {
233         print_pci_device( dev, verbose );
234     }
235
236     pci_system_cleanup();
237     return 0;
238 }