Added support to ICH9 chipset family
[coreboot.git] / util / inteltool / inteltool.c
1 /*
2  * inteltool - dump all registers on an Intel CPU + chipset based system.
3  *
4  * Copyright (C) 2008-2010 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_82443LX, "82443LX" },
33         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443BX, "82443BX" },
34         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443BX_NO_AGP, "82443BX without AGP" },
35         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810, "i810" },
36         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810DC, "i810-DC100" },
37         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82830M, "i830M" },
38         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845, "i845" },
39         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82915, "82915G/P/GV/GL/PL/910GL" },
40         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945P, "i945P" },
41         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945GM, "i945GM" },
42         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PM965, "PM965" },
43         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82975X, "i975X" },
44         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82Q35, "Q35" },
45         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82G33, "P35/G33/G31/P31" },
46         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82Q33, "Q33" },
47         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_X58, "X58" },
48         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_GS45, "GS45ME-GMCH" },
49         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH10R, "ICH10R" },
50         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9DH, "ICH9DH" },
51         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9DO, "ICH9DO" },
52         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9R, "ICH9R" },
53         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9, "ICH9" },
54         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9M, "ICH9M" },
55         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9ME, "ICH9M-E" },
56         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH8M, "ICH8-M" },
57         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7MDH, "ICH7-M DH" },
58         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7M, "ICH7-M" },
59         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7, "ICH7" },
60         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7DH, "ICH7DH" },
61         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6, "ICH6" },
62         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4M, "ICH4-M" },
63         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4, "ICH4" },
64         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH2, "ICH2" },
65         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH0, "ICH0" },
66         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH, "ICH" },
67         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371XX, "82371AB/EB/MB" },
68 };
69
70 #ifndef __DARWIN__
71 static int fd_mem;
72
73 void *map_physical(unsigned long phys_addr, size_t len)
74 {
75         void *virt_addr;
76
77         virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
78                     fd_mem, (off_t) phys_addr);
79
80         if (virt_addr == MAP_FAILED) {
81                 printf("Error mapping physical memory 0x%08lx[0x%x]\n", phys_addr, len);
82                 return NULL;
83         }
84
85         return virt_addr;
86 }
87
88 void unmap_physical(void *virt_addr, size_t len)
89 {
90         munmap(virt_addr, len);
91 }
92 #endif
93
94 void print_version(void)
95 {
96         printf("inteltool v%s -- ", INTELTOOL_VERSION);
97         printf("Copyright (C) 2008 coresystems GmbH\n\n");
98         printf(
99     "This program is free software: you can redistribute it and/or modify\n"
100     "it under the terms of the GNU General Public License as published by\n"
101     "the Free Software Foundation, version 2 of the License.\n\n"
102     "This program is distributed in the hope that it will be useful,\n"
103     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
104     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
105     "GNU General Public License for more details.\n\n"
106     "You should have received a copy of the GNU General Public License\n"
107     "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n");
108 }
109
110 void print_usage(const char *name)
111 {
112         printf("usage: %s [-vh?grpmedPMa]\n", name);
113         printf("\n"
114              "   -v | --version:                   print the version\n"
115              "   -h | --help:                      print this help\n\n"
116              "   -g | --gpio:                      dump soutbridge GPIO registers\n"
117              "   -r | --rcba:                      dump soutbridge RCBA registers\n"
118              "   -p | --pmbase:                    dump soutbridge Power Management registers\n\n"
119              "   -m | --mchbar:                    dump northbridge Memory Controller registers\n"
120              "   -e | --epbar:                     dump northbridge EPBAR registers\n"
121              "   -d | --dmibar:                    dump northbridge DMIBAR registers\n"
122              "   -P | --pciexpress:                dump northbridge PCIEXBAR registers\n\n"
123              "   -M | --msrs:                      dump CPU MSRs\n"
124              "   -a | --all:                       dump all known registers\n"
125              "\n");
126         exit(1);
127 }
128
129 int main(int argc, char *argv[])
130 {
131         struct pci_access *pacc;
132         struct pci_dev *sb = NULL, *nb, *dev;
133         int i, opt, option_index = 0;
134         unsigned int id;
135
136         char *sbname = "unknown", *nbname = "unknown";
137
138         int dump_gpios = 0, dump_mchbar = 0, dump_rcba = 0;
139         int dump_pmbase = 0, dump_epbar = 0, dump_dmibar = 0;
140         int dump_pciexbar = 0, dump_coremsrs = 0;
141
142         static struct option long_options[] = {
143                 {"version", 0, 0, 'v'},
144                 {"help", 0, 0, 'h'},
145                 {"gpios", 0, 0, 'g'},
146                 {"mchbar", 0, 0, 'm'},
147                 {"rcba", 0, 0, 'r'},
148                 {"pmbase", 0, 0, 'p'},
149                 {"epbar", 0, 0, 'e'},
150                 {"dmibar", 0, 0, 'd'},
151                 {"pciexpress", 0, 0, 'P'},
152                 {"msrs", 0, 0, 'M'},
153                 {"all", 0, 0, 'a'},
154                 {0, 0, 0, 0}
155         };
156
157         while ((opt = getopt_long(argc, argv, "vh?grpmedPMa",
158                                   long_options, &option_index)) != EOF) {
159                 switch (opt) {
160                 case 'v':
161                         print_version();
162                         exit(0);
163                         break;
164                 case 'g':
165                         dump_gpios = 1;
166                         break;
167                 case 'm':
168                         dump_mchbar = 1;
169                         break;
170                 case 'r':
171                         dump_rcba = 1;
172                         break;
173                 case 'p':
174                         dump_pmbase = 1;
175                         break;
176                 case 'e':
177                         dump_epbar = 1;
178                         break;
179                 case 'd':
180                         dump_dmibar = 1;
181                         break;
182                 case 'P':
183                         dump_pciexbar = 1;
184                         break;
185                 case 'M':
186                         dump_coremsrs = 1;
187                         break;
188                 case 'a':
189                         dump_gpios = 1;
190                         dump_mchbar = 1;
191                         dump_rcba = 1;
192                         dump_pmbase = 1;
193                         dump_epbar = 1;
194                         dump_dmibar = 1;
195                         dump_pciexbar = 1;
196                         dump_coremsrs = 1;
197                         break;
198                 case 'h':
199                 case '?':
200                 default:
201                         print_usage(argv[0]);
202                         exit(0);
203                         break;
204                 }
205         }
206
207         if (iopl(3)) {
208                 printf("You need to be root.\n");
209                 exit(1);
210         }
211
212 #ifndef __DARWIN__
213         if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
214                 perror("Can not open /dev/mem");
215                 exit(1);
216         }
217 #endif
218
219         pacc = pci_alloc();
220         pci_init(pacc);
221         pci_scan_bus(pacc);
222
223         /* Find the required devices */
224         for (dev = pacc->devices; dev; dev = dev->next) {
225                 pci_fill_info(dev, PCI_FILL_CLASS);
226                 /* The ISA/LPC bridge can be 0x1f, 0x07, or 0x04 so we probe. */
227                 if (dev->device_class == 0x0601) { /* ISA/LPC bridge */
228                         if (sb == NULL)
229                                 sb = dev;
230                         else
231                                 fprintf(stderr, "Multiple devices with class ID"
232                                         " 0x0601, using %02x%02x:%02x.%02x\n",
233                                         dev->domain, dev->bus, dev->dev,
234                                         dev->func);
235                 }
236         }
237
238         if (!sb) {
239                 printf("No southbridge found.\n");
240                 exit(1);
241         }
242
243         pci_fill_info(sb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
244
245         if (sb->vendor_id != PCI_VENDOR_ID_INTEL) {
246                 printf("Not an Intel(R) southbridge.\n");
247                 exit(1);
248         }
249
250         nb = pci_get_dev(pacc, 0, 0, 0x00, 0);
251         if (!nb) {
252                 printf("No northbridge found.\n");
253                 exit(1);
254         }
255
256         pci_fill_info(nb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
257
258         if (nb->vendor_id != PCI_VENDOR_ID_INTEL) {
259                 printf("Not an Intel(R) northbridge.\n");
260                 exit(1);
261         }
262
263         id = cpuid(1);
264         printf("Intel CPU: Family %x, Model %x\n",
265                         (id >> 8) & 0xf, (id >> 4) & 0xf);
266
267         /* Determine names */
268         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
269                 if (nb->device_id == supported_chips_list[i].device_id)
270                         nbname = supported_chips_list[i].name;
271         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
272                 if (sb->device_id == supported_chips_list[i].device_id)
273                         sbname = supported_chips_list[i].name;
274
275         printf("Intel Northbridge: %04x:%04x (%s)\n",
276                 nb->vendor_id, nb->device_id, nbname);
277
278         printf("Intel Southbridge: %04x:%04x (%s)\n",
279                 sb->vendor_id, sb->device_id, sbname);
280
281         /* Now do the deed */
282
283         if (dump_gpios) {
284                 print_gpios(sb);
285                 printf("\n\n");
286         }
287
288         if (dump_rcba) {
289                 print_rcba(sb);
290                 printf("\n\n");
291         }
292
293         if (dump_pmbase) {
294                 print_pmbase(sb);
295                 printf("\n\n");
296         }
297
298         if (dump_mchbar) {
299                 print_mchbar(nb);
300                 printf("\n\n");
301         }
302
303         if (dump_epbar) {
304                 print_epbar(nb);
305                 printf("\n\n");
306         }
307
308         if (dump_dmibar) {
309                 print_dmibar(nb);
310                 printf("\n\n");
311         }
312
313         if (dump_pciexbar) {
314                 print_pciexbar(nb);
315                 printf("\n\n");
316         }
317
318         if (dump_coremsrs) {
319                 print_intel_core_msrs();
320                 printf("\n\n");
321         }
322
323         /* Clean up */
324         pci_free_dev(nb);
325         // pci_free_dev(sb); // TODO: glibc detected "double free or corruption"
326         pci_cleanup(pacc);
327
328         return 0;
329 }