mini fix to reliably compile inteltool on darwin, and on Linux both on x86/x86_64.
[coreboot.git] / util / inteltool / inteltool.c
1 /*
2  * inteltool - dump all registers on an Intel CPU + chipset based system.
3  *
4  * Copyright (C) 2008 by coresystems GmbH 
5  *  written by Stefan Reinauer <stepan@coresystems.de> 
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include "inteltool.h"
27
28 static const struct {
29         uint16_t vendor_id, device_id;
30         char *name;
31 } supported_chips_list[] = {
32         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845, "i845" },
33         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945P, "i945P" },
34         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945GM, "i945GM" },
35         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PM965, "PM965" },
36         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82975X, "i975X" },
37         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8M, "ICH8-M" },
38         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7MDH, "ICH7-M DH" },
39         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7M, "ICH7-M" },
40         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7, "ICH7" },
41         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7DH, "ICH7DH" },
42         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4M, "ICH4-M" },
43         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4, "ICH4" },
44         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH2, "ICH2" },
45         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH0, "ICH0" },
46         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH, "ICH" }
47 };
48
49 #ifndef DARWIN
50 static int fd_mem;
51
52 void *map_physical(unsigned long phys_addr, int len)
53 {
54         void *virt_addr;
55
56         virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
57                     fd_mem, (off_t) phys_addr);
58         
59         if (virt_addr == MAP_FAILED) {
60                 printf("Error mapping physical memory 0x%08lx[0x%x]\n", phys_addr, len);
61                 return NULL;
62         }
63
64         return virt_addr;
65 }
66
67 void unmap_physical(void *virt_addr, int len)
68 {
69         munmap(virt_addr, len);
70 }
71 #endif
72
73 void print_version(void)
74 {
75         printf("inteltool v%s -- ", INTELTOOL_VERSION);
76         printf("Copyright (C) 2008 coresystems GmbH\n\n");
77         printf(
78     "This program is free software: you can redistribute it and/or modify\n"
79     "it under the terms of the GNU General Public License as published by\n"
80     "the Free Software Foundation, version 2 of the License.\n\n"
81     "This program is distributed in the hope that it will be useful,\n"
82     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
83     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
84     "GNU General Public License for more details.\n\n"
85     "You should have received a copy of the GNU General Public License\n"
86     "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n");
87 }
88
89 void print_usage(const char *name)
90 {
91         printf("usage: %s [-vh?grpmedPMa]\n", name);
92         printf("\n"
93              "   -v | --version:                   print the version\n"
94              "   -h | --help:                      print this help\n\n"
95              "   -g | --gpio:                      dump soutbridge GPIO registers\n"
96              "   -r | --rcba:                      dump soutbridge RCBA registers\n"
97              "   -p | --pmbase:                    dump soutbridge Power Management registers\n\n"
98              "   -m | --mchbar:                    dump northbridge Memory Controller registers\n"
99              "   -e | --epbar:                     dump northbridge EPBAR registers\n"
100              "   -d | --dmibar:                    dump northbridge DMIBAR registers\n"
101              "   -P | --pciexpress:                dump northbridge PCIEXBAR registers\n\n"
102              "   -M | --msrs:                      dump CPU MSRs\n"
103              "   -a | --all:                       dump all known registers\n"
104              "\n");
105         exit(1);
106 }
107
108 int main(int argc, char *argv[])
109 {
110         struct pci_access *pacc;
111         struct pci_dev *sb, *nb;
112         int i, opt, option_index = 0;
113         unsigned int id;
114
115         char *sbname = "unknown", *nbname = "unknown";
116
117         int dump_gpios = 0, dump_mchbar = 0, dump_rcba = 0;
118         int dump_pmbase = 0, dump_epbar = 0, dump_dmibar = 0;
119         int dump_pciexbar = 0, dump_coremsrs = 0;
120
121         static struct option long_options[] = {
122                 {"version", 0, 0, 'v'},
123                 {"help", 0, 0, 'h'},
124                 {"gpios", 0, 0, 'g'},
125                 {"mchbar", 0, 0, 'm'},
126                 {"rcba", 0, 0, 'r'},
127                 {"pmbase", 0, 0, 'p'},
128                 {"epbar", 0, 0, 'e'},
129                 {"dmibar", 0, 0, 'd'},
130                 {"pciexpress", 0, 0, 'P'},
131                 {"msrs", 0, 0, 'M'},
132                 {"all", 0, 0, 'a'},
133                 {0, 0, 0, 0}
134         };
135
136         while ((opt = getopt_long(argc, argv, "vh?grpmedPMa",
137                                   long_options, &option_index)) != EOF) {
138                 switch (opt) {
139                 case 'v':
140                         print_version();
141                         exit(0);
142                         break;
143                 case 'g':
144                         dump_gpios = 1;
145                         break;
146                 case 'm':
147                         dump_mchbar = 1;
148                         break;
149                 case 'r':
150                         dump_rcba = 1;
151                         break;
152                 case 'p':
153                         dump_pmbase = 1;
154                         break;
155                 case 'e':
156                         dump_epbar = 1;
157                         break;
158                 case 'd':
159                         dump_dmibar = 1;
160                         break;
161                 case 'P':
162                         dump_pciexbar = 1;
163                         break;
164                 case 'M':
165                         dump_coremsrs = 1;
166                         break;
167                 case 'a':
168                         dump_gpios = 1;
169                         dump_mchbar = 1;
170                         dump_rcba = 1;
171                         dump_pmbase = 1;
172                         dump_epbar = 1;
173                         dump_dmibar = 1;
174                         dump_pciexbar = 1;
175                         dump_coremsrs = 1;
176                         break;
177                 case 'h':
178                 case '?':
179                 default:
180                         print_usage(argv[0]);
181                         exit(0);
182                         break;
183                 }
184         }
185
186         if (iopl(3)) {
187                 printf("You need to be root.\n");
188                 exit(1);
189         }
190
191 #ifndef DARWIN
192         if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
193                 perror("Can not open /dev/mem");
194                 exit(1);
195         }
196 #endif
197
198         pacc = pci_alloc();
199         pci_init(pacc);
200         pci_scan_bus(pacc);
201
202         /* Find the required devices */
203
204         sb = pci_get_dev(pacc, 0, 0, 0x1f, 0);
205         if (!sb) {
206                 printf("No southbridge found.\n");
207                 exit(1);
208         }
209
210         pci_fill_info(sb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
211
212         if (sb->vendor_id != PCI_VENDOR_ID_INTEL) {
213                 printf("Not an Intel(R) southbridge.\n");
214                 exit(1);
215         }
216
217         nb = pci_get_dev(pacc, 0, 0, 0x00, 0);
218         if (!nb) {
219                 printf("No northbridge found.\n");
220                 exit(1);
221         }
222
223         pci_fill_info(nb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
224
225         if (nb->vendor_id != PCI_VENDOR_ID_INTEL) {
226                 printf("Not an Intel(R) northbridge.\n");
227                 exit(1);
228         }
229
230         id = cpuid(1);
231         printf("Intel CPU: Family %x, Model %x\n", 
232                         (id >> 8) & 0xf, (id >> 4) & 0xf);
233
234         /* Determine names */
235         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
236                 if (nb->device_id == supported_chips_list[i].device_id)
237                         nbname = supported_chips_list[i].name;
238         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
239                 if (sb->device_id == supported_chips_list[i].device_id)
240                         sbname = supported_chips_list[i].name;
241
242         printf("Intel Northbridge: %04x:%04x (%s)\n", 
243                 nb->vendor_id, nb->device_id, nbname);
244
245         printf("Intel Southbridge: %04x:%04x (%s)\n", 
246                 sb->vendor_id, sb->device_id, sbname);
247
248         /* Now do the deed */
249
250         if (dump_gpios) {
251                 print_gpios(sb);
252                 printf("\n\n");
253         }
254
255         if (dump_rcba) {
256                 print_rcba(sb);
257                 printf("\n\n");
258         }
259
260         if (dump_pmbase) {
261                 print_pmbase(sb);
262                 printf("\n\n");
263         }
264
265         if (dump_mchbar) {
266                 print_mchbar(nb);
267                 printf("\n\n");
268         }
269
270         if (dump_epbar) {
271                 print_epbar(nb);
272                 printf("\n\n");
273         }
274
275         if (dump_dmibar) {
276                 print_dmibar(nb);
277                 printf("\n\n");
278         }
279
280         if (dump_pciexbar) {
281                 print_pciexbar(nb);
282                 printf("\n\n");
283         }
284
285         if (dump_coremsrs) {
286                 print_intel_core_msrs();
287                 printf("\n\n");
288         }
289
290         /* Clean up */
291         pci_free_dev(nb);
292         pci_free_dev(sb);
293         pci_cleanup(pacc);
294
295         return 0;
296 }