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