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