Cosmetics, whitespace, coding style, partially ident-aided (trivial).
[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 <errno.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <getopt.h>
29 #include <sys/mman.h>
30 #include <sys/io.h>
31 #include <pci/pci.h>
32
33 #define INTELTOOL_VERSION "1.0"
34
35 /* Tested chipsets: */
36 #define PCI_VENDOR_ID_INTEL             0x8086
37 #define PCI_DEVICE_ID_INTEL_ICH         0x2410
38 #define PCI_DEVICE_ID_INTEL_ICH0        0x2420
39 #define PCI_DEVICE_ID_INTEL_ICH4        0x24c0
40 #define PCI_DEVICE_ID_INTEL_ICH4M       0x24cc
41 #define PCI_DEVICE_ID_INTEL_ICH7DH      0x27b0
42 #define PCI_DEVICE_ID_INTEL_ICH7        0x27b8
43 #define PCI_DEVICE_ID_INTEL_ICH7M       0x27b9
44 #define PCI_DEVICE_ID_INTEL_ICH7MDH     0x27bd
45 #define PCI_DEVICE_ID_INTEL_82945GM     0x27a0
46
47 static const struct {
48         uint16_t vendor_id, device_id;
49         char *name;
50 } supported_chips_list[] = {
51         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82945GM, "i945GM" },
52         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7MDH, "ICH7-M DH" },
53         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7M, "ICH7-M" },
54         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7, "ICH7" },
55         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7DH, "ICH7DH" },
56         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4M, "ICH4-M" },
57         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH4, "ICH4" },
58         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH0, "ICH0" },
59         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH, "ICH" }
60 };
61
62 #define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0])))
63
64 int fd_mem;
65 int fd_msr;
66
67 typedef struct { uint32_t hi, lo; } msr_t;
68 typedef struct { uint16_t addr; int size; char *name; } io_register_t;
69
70 static const io_register_t ich0_gpio_registers[] = {
71         { 0x00, 4, "GPIO_USE_SEL" },
72         { 0x04, 4, "GP_IO_SEL" },
73         { 0x08, 4, "RESERVED" },
74         { 0x0c, 4, "GP_LVL" },
75         { 0x10, 4, "RESERVED" },
76         { 0x14, 4, "GPO_TTL" },
77         { 0x18, 4, "GPO_BLINK" },
78         { 0x1c, 4, "RESERVED" },
79         { 0x20, 4, "RESERVED" },
80         { 0x24, 4, "RESERVED" },
81         { 0x28, 4, "RESERVED" },
82         { 0x2c, 4, "GPI_INV" },
83         { 0x30, 4, "RESERVED" },
84         { 0x34, 4, "RESERVED" },
85         { 0x38, 4, "RESERVED" },
86         { 0x3C, 4, "RESERVED" }
87 };
88
89 static const io_register_t ich4_gpio_registers[] = {
90         { 0x00, 4, "GPIO_USE_SEL" },
91         { 0x04, 4, "GP_IO_SEL" },
92         { 0x08, 4, "RESERVED" },
93         { 0x0c, 4, "GP_LVL" },
94         { 0x10, 4, "RESERVED" },
95         { 0x14, 4, "GPO_TTL" },
96         { 0x18, 4, "GPO_BLINK" },
97         { 0x1c, 4, "RESERVED" },
98         { 0x20, 4, "RESERVED" },
99         { 0x24, 4, "RESERVED" },
100         { 0x28, 4, "RESERVED" },
101         { 0x2c, 4, "GPI_INV" },
102         { 0x30, 4, "GPIO_USE_SEL2" },
103         { 0x34, 4, "GP_IO_SEL2" },
104         { 0x38, 4, "GP_LVL2" },
105         { 0x3C, 4, "RESERVED" }
106 };
107
108 static const io_register_t ich7_gpio_registers[] = {
109         { 0x00, 4, "GPIO_USE_SEL" },
110         { 0x04, 4, "GP_IO_SEL" },
111         { 0x08, 4, "RESERVED" },
112         { 0x0c, 4, "GP_LVL" },
113         { 0x10, 4, "RESERVED" },
114         { 0x14, 4, "RESERVED" },
115         { 0x18, 4, "GPO_BLINK" },
116         { 0x1c, 4, "RESERVED" },
117         { 0x20, 4, "RESERVED" },
118         { 0x24, 4, "RESERVED" },
119         { 0x28, 4, "RESERVED" },
120         { 0x2c, 4, "GPI_INV" },
121         { 0x30, 4, "GPIO_USE_SEL2" },
122         { 0x34, 4, "GP_IO_SEL2" },
123         { 0x38, 4, "GP_LVL2" },
124         { 0x3C, 4, "RESERVED" }
125 };
126
127 int print_gpios(struct pci_dev *sb)
128 {
129         int i, size;
130         uint16_t gpiobase;
131         const io_register_t *gpio_registers;
132
133         printf("\n============= GPIOS =============\n\n");
134
135         switch (sb->device_id) {
136         case PCI_DEVICE_ID_INTEL_ICH7:
137         case PCI_DEVICE_ID_INTEL_ICH7M:
138         case PCI_DEVICE_ID_INTEL_ICH7DH:
139         case PCI_DEVICE_ID_INTEL_ICH7MDH:
140                 gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
141                 gpio_registers = ich7_gpio_registers;
142                 size = ARRAY_SIZE(ich7_gpio_registers);
143                 break;
144         case PCI_DEVICE_ID_INTEL_ICH4:
145         case PCI_DEVICE_ID_INTEL_ICH4M:
146                 gpiobase = pci_read_word(sb, 0x58) & 0xfffc;
147                 gpio_registers = ich4_gpio_registers;
148                 size = ARRAY_SIZE(ich4_gpio_registers);
149                 break;
150         case PCI_DEVICE_ID_INTEL_ICH:
151         case PCI_DEVICE_ID_INTEL_ICH0:
152                 gpiobase = pci_read_word(sb, 0x58) & 0xfffc;
153                 gpio_registers = ich0_gpio_registers;
154                 size = ARRAY_SIZE(ich0_gpio_registers);
155                 break;
156         case 0x1234: // Dummy for non-existent functionality
157                 printf("This southbridge does not have GPIOBASE.\n");
158                 return 1;
159         default:
160                 printf("Error: Dumping GPIOs on this southbridge is not (yet) supported.\n");
161                 return 1;
162         }
163
164         printf("GPIOBASE = 0x%04x (IO)\n\n", gpiobase);
165
166         for (i = 0; i < size; i++) {
167                 switch (gpio_registers[i].size) {
168                 case 4:
169                         printf("gpiobase+0x%04x: 0x%08x (%s)\n",
170                                 gpio_registers[i].addr,
171                                 inl(gpiobase+gpio_registers[i].addr),
172                                 gpio_registers[i].name);
173                         break;
174                 case 2:
175                         printf("gpiobase+0x%04x: 0x%04x     (%s)\n",
176                                 gpio_registers[i].addr,
177                                 inw(gpiobase+gpio_registers[i].addr),
178                                 gpio_registers[i].name);
179                         break;
180                 case 1:
181                         printf("gpiobase+0x%04x: 0x%02x       (%s)\n",
182                                 gpio_registers[i].addr,
183                                 inb(gpiobase+gpio_registers[i].addr),
184                                 gpio_registers[i].name);
185                         break;
186                 }
187         }
188
189         return 0;
190 }
191
192 int print_rcba(struct pci_dev *sb)
193 {
194         int i, size = 0x4000;
195         volatile uint8_t *rcba;
196         uint32_t rcba_phys;
197
198         printf("\n============= RCBA ==============\n\n");
199
200         switch (sb->device_id) {
201         case PCI_DEVICE_ID_INTEL_ICH7:
202         case PCI_DEVICE_ID_INTEL_ICH7M:
203         case PCI_DEVICE_ID_INTEL_ICH7DH:
204         case PCI_DEVICE_ID_INTEL_ICH7MDH:
205                 rcba_phys = pci_read_long(sb, 0xf0) & 0xfffffffe;
206                 break;
207         case PCI_DEVICE_ID_INTEL_ICH:
208         case PCI_DEVICE_ID_INTEL_ICH0:
209         case PCI_DEVICE_ID_INTEL_ICH4:
210         case PCI_DEVICE_ID_INTEL_ICH4M:
211                 printf("This southbridge does not have RCBA.\n");
212                 return 1;
213         default:
214                 printf("Error: Dumping RCBA on this southbridge is not (yet) supported.\n");
215                 return 1;
216         }
217
218         rcba = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
219                     fd_mem, (off_t) rcba_phys);
220         
221         if (rcba == MAP_FAILED) {
222                 perror("Error mapping RCBA");
223                 exit(1);
224         }
225
226         printf("RCBA = 0x%08x (MEM)\n\n", rcba_phys);
227
228         for (i = 0; i < size; i += 4) {
229                 if (*(uint32_t *)(rcba + i))
230                         printf("0x%04x: 0x%08x\n", i, *(uint32_t *)(rcba + i));
231         }
232
233         munmap((void *)rcba, size);
234         return 0;
235 }
236
237 int print_pmbase(struct pci_dev *sb)
238 {
239         int i, size = 0x80;
240         uint16_t pmbase;
241
242         printf("\n============= PMBASE ============\n\n");
243
244         switch (sb->device_id) {
245         case PCI_DEVICE_ID_INTEL_ICH7:
246         case PCI_DEVICE_ID_INTEL_ICH7M:
247         case PCI_DEVICE_ID_INTEL_ICH7DH:
248         case PCI_DEVICE_ID_INTEL_ICH7MDH:
249                 pmbase = pci_read_word(sb, 0x40) & 0xfffc;
250                 break;
251         case 0x1234: // Dummy for non-existent functionality
252                 printf("This southbridge does not have PMBASE.\n");
253                 return 1;
254         default:
255                 printf("Error: Dumping PMBASE on this southbridge is not (yet) supported.\n");
256                 return 1;
257         }
258
259         printf("PMBASE = 0x%04x (IO)\n\n", pmbase);
260
261         for (i = 0; i < size; i += 4) {
262                 printf("pmbase+0x%04x: 0x%08x\n", i, inl(pmbase + i));
263         }
264
265         return 0;
266 }
267
268 /*
269  * (G)MCH MMIO Config Space
270  */
271 int print_mchbar(struct pci_dev *nb)
272 {
273         int i, size = (16 * 1024);
274         volatile uint8_t *mchbar;
275         uint32_t mchbar_phys;
276
277         printf("\n============= MCHBAR ============\n\n");
278
279         switch (nb->device_id) {
280         case PCI_DEVICE_ID_INTEL_82945GM:
281                 mchbar_phys = pci_read_long(nb, 0x44) & 0xfffffffe;
282                 break;
283         case 0x1234: // Dummy for non-existent functionality
284                 printf("This northbrigde does not have MCHBAR.\n");
285                 return 1;
286         default:
287                 printf("Error: Dumping MCHBAR on this northbridge is not (yet) supported.\n");
288                 return 1;
289         }
290
291         mchbar = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
292                       fd_mem, (off_t) mchbar_phys);
293         
294         if (mchbar == MAP_FAILED) {
295                 perror("Error mapping MCHBAR");
296                 exit(1);
297         }
298
299         printf("MCHBAR = 0x%08x (MEM)\n\n", mchbar_phys);
300
301         for (i = 0; i < size; i += 4) {
302                 if (*(uint32_t *)(mchbar + i))
303                         printf("0x%04x: 0x%08x\n", i, *(uint32_t *)(mchbar+i));
304         }
305
306         munmap((void *)mchbar, size);
307         return 0;
308 }
309
310 /*
311  * Egress Port Root Complex MMIO configuration space
312  */
313 int print_epbar(struct pci_dev *nb)
314 {
315         int i, size = (4 * 1024);
316         volatile uint8_t *epbar;
317         uint32_t epbar_phys;
318
319         printf("\n============= EPBAR =============\n\n");
320
321         switch (nb->device_id) {
322         case PCI_DEVICE_ID_INTEL_82945GM:
323                 epbar_phys = pci_read_long(nb, 0x40) & 0xfffffffe;
324                 break;
325         case 0x1234: // Dummy for non-existent functionality
326                 printf("This northbrigde does not have EPBAR.\n");
327                 return 1;
328         default:
329                 printf("Error: Dumping EPBAR on this northbridge is not (yet) supported.\n");
330                 return 1;
331         }
332
333         epbar = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
334                      fd_mem, (off_t) epbar_phys);
335         
336         if (epbar == MAP_FAILED) {
337                 perror("Error mapping EPBAR");
338                 exit(1);
339         }
340
341         printf("EPBAR = 0x%08x (MEM)\n\n", epbar_phys);
342         for (i = 0; i < size; i += 4) {
343                 if (*(uint32_t *)(epbar + i))
344                         printf("0x%04x: 0x%08x\n", i, *(uint32_t *)(epbar+i));
345         }
346
347         munmap((void *)epbar, size);
348         return 0;
349 }
350
351 /*
352  * MCH-ICH Serial Interconnect Ingress Root Complex MMIO configuration space
353  */
354 int print_dmibar(struct pci_dev *nb)
355 {
356         int i, size = (4 * 1024);
357         volatile uint8_t *dmibar;
358         uint32_t dmibar_phys;
359
360         printf("\n============= DMIBAR ============\n\n");
361
362         switch (nb->device_id) {
363         case PCI_DEVICE_ID_INTEL_82945GM:
364                 dmibar_phys = pci_read_long(nb, 0x4c) & 0xfffffffe;
365                 break;
366         case 0x1234: // Dummy for non-existent functionality
367                 printf("This northbrigde does not have DMIBAR.\n");
368                 return 1;
369         default:
370                 printf("Error: Dumping DMIBAR on this northbridge is not (yet) supported.\n");
371                 return 1;
372         }
373
374         dmibar = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
375                       fd_mem, (off_t) dmibar_phys);
376         
377         if (dmibar == MAP_FAILED) {
378                 perror("Error mapping DMIBAR");
379                 exit(1);
380         }
381
382         printf("DMIBAR = 0x%08x (MEM)\n\n", dmibar_phys);
383         for (i = 0; i < size; i += 4) {
384                 if (*(uint32_t *)(dmibar + i))
385                         printf("0x%04x: 0x%08x\n", i, *(uint32_t *)(dmibar+i));
386         }
387
388         munmap((void *)dmibar, size);
389         return 0;
390 }
391
392 /*
393  * PCIe MMIO configuration space
394  */
395 int print_pciexbar(struct pci_dev *nb)
396 {
397         uint32_t pciexbar_reg;
398         uint32_t pciexbar_phys;
399         volatile uint8_t *pciexbar;
400         int max_busses, devbase, i;
401         int bus, dev, fn;
402
403         printf("========= PCIEXBAR ========\n\n");
404
405         switch (nb->device_id) {
406         case PCI_DEVICE_ID_INTEL_82945GM:
407                 pciexbar_reg = pci_read_long(nb, 0x48);
408                 break;
409         case 0x1234: // Dummy for non-existent functionality
410                 printf("Error: This northbrigde does not have PCIEXBAR.\n");
411                 return 1;
412         default:
413                 printf("Error: Dumping PCIEXBAR on this northbridge is not (yet) supported.\n");
414                 return 1;
415         }
416
417         if (!(pciexbar_reg & (1 << 0))) {
418                 printf("PCIEXBAR register is disabled.\n");
419                 return 0;
420         }
421
422         switch ((pciexbar_reg >> 1) & 3) {
423         case 0: // 256MB
424                 pciexbar_phys = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28));
425                 max_busses = 256;
426                 break;
427         case 1: // 128M
428                 pciexbar_phys = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
429                 max_busses = 128;
430                 break;
431         case 2: // 64M
432                 pciexbar_phys = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27)|(1 << 26));
433                 max_busses = 64;
434                 break;
435         default: // RSVD
436                 printf("Undefined address base. Bailing out.\n");
437                 return 1;
438         }       
439
440         printf("PCIEXBAR: 0x%08x\n", pciexbar_phys);
441
442         pciexbar = mmap(0, (max_busses * 1024 * 1024), PROT_WRITE | PROT_READ,
443                         MAP_SHARED, fd_mem, (off_t) pciexbar_phys);
444         
445         if (pciexbar == MAP_FAILED) {
446                 perror("Error mapping PCIEXBAR");
447                 exit(1);
448         }
449         
450         for (bus = 0; bus < max_busses; bus++) {
451                 for (dev = 0; dev < 32; dev++) {
452                         for (fn = 0; fn < 8; fn++) {
453                                 devbase = (bus * 1024 * 1024) + (dev * 32 * 1024) + (fn * 4 * 1024);
454
455                                 if (*(uint16_t *)(pciexbar + devbase) == 0xffff)
456                                         continue;
457                                 
458                                 /* This is a heuristics. Anyone got a better check? */
459                                 if( (*(uint32_t *)(pciexbar + devbase + 256) == 0xffffffff) &&
460                                         (*(uint32_t *)(pciexbar + devbase + 512) == 0xffffffff) ) {
461 #if DEBUG
462                                         printf("Skipped non-PCIe device %02x:%02x.%01x\n", bus, dev, fn);
463 #endif
464                                         continue;
465                                 }
466
467                                 printf("\nPCIe %02x:%02x.%01x extended config space:", bus, dev, fn);
468                                 for (i = 0; i < 4096; i++) {
469                                         if((i % 0x10) == 0)
470                                                 printf("\n%04x:", i);
471                                         printf(" %02x", *(pciexbar+devbase+i));
472                                 }
473                                 printf("\n");
474                         }
475                 }
476         }
477
478         munmap((void *)pciexbar, (max_busses * 1024 * 1024));
479
480         return 0;
481 }
482
483 int msr_readerror = 0;
484
485 msr_t rdmsr(int addr)
486 {
487         uint8_t buf[8];
488         msr_t msr = { 0xffffffff, 0xffffffff };
489
490         if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
491                 perror("Could not lseek() to MSR");
492                 close(fd_msr);
493                 exit(1);
494         }
495
496         if (read(fd_msr, buf, 8) == 8) {
497                 msr.lo = *(uint32_t *)buf;
498                 msr.hi = *(uint32_t *)(buf + 4);
499
500                 return msr;
501         }
502
503         if (errno == 5) {
504                 printf(" (*)"); // Not all bits of the MSR could be read
505                 msr_readerror = 1;
506         } else {
507                 // A severe error.
508                 perror("Could not read() MSR");
509                 close(fd_msr);
510                 exit(1);
511         }
512
513         return msr;
514 }
515
516 int print_intel_core_msrs(void)
517 {
518         unsigned int i, core;
519         msr_t msr;
520
521 #define IA32_PLATFORM_ID                0x0017
522 #define EBL_CR_POWERON                  0x002a
523 #define FSB_CLK_STS                     0x00cd
524 #define IA32_TIME_STAMP_COUNTER         0x0010
525 #define IA32_APIC_BASE                  0x001b
526
527         typedef struct {
528                 int number;
529                 char *name;
530         } msr_entry_t;
531
532         msr_entry_t global_msrs[] = {
533                 { 0x0017, "IA32_PLATFORM_ID" },
534                 { 0x002a, "EBL_CR_POWERON" },
535                 { 0x00cd, "FSB_CLOCK_STS" },
536                 { 0x00ce, "FSB_CLOCK_VCC" },
537                 { 0x00e2, "CLOCK_CST_CONFIG_CONTROL" },
538                 { 0x00e3, "PMG_IO_BASE_ADDR" },
539                 { 0x00e4, "PMG_IO_CAPTURE_ADDR" },
540                 { 0x00ee, "EXT_CONFIG" },
541                 { 0x011e, "BBL_CR_CTL3" },
542                 { 0x0194, "CLOCK_FLEX_MAX" },
543                 { 0x0198, "IA32_PERF_STATUS" },
544                 { 0x01a0, "IA32_MISC_ENABLES" },
545                 { 0x01aa, "PIC_SENS_CFG" },
546                 { 0x0400, "IA32_MC0_CTL" },
547                 { 0x0401, "IA32_MC0_STATUS" },
548                 { 0x0402, "IA32_MC0_ADDR" },
549                 //{ 0x0403, "IA32_MC0_MISC" }, // Seems to be RO
550                 { 0x040c, "IA32_MC4_CTL" },
551                 { 0x040d, "IA32_MC4_STATUS" },
552                 { 0x040e, "IA32_MC4_ADDR" },
553                 //{ 0x040f, "IA32_MC4_MISC" } // Seems to be RO
554         };
555
556         msr_entry_t per_core_msrs[] = {
557                 { 0x0010, "IA32_TIME_STAMP_COUNTER" },
558                 { 0x001b, "IA32_APIC_BASE" },
559                 { 0x003a, "IA32_FEATURE_CONTROL" },
560                 { 0x003f, "IA32_TEMPERATURE_OFFSET" },
561                 //{ 0x0079, "IA32_BIOS_UPDT_TRIG" }, // Seems to be RO
562                 { 0x008b, "IA32_BIOS_SIGN_ID" },
563                 { 0x00e7, "IA32_MPERF" },
564                 { 0x00e8, "IA32_APERF" },
565                 { 0x00fe, "IA32_MTRRCAP" },
566                 { 0x015f, "DTS_CAL_CTRL" },
567                 { 0x0179, "IA32_MCG_CAP" },
568                 { 0x017a, "IA32_MCG_STATUS" },
569                 { 0x0199, "IA32_PERF_CONTROL" },
570                 { 0x019a, "IA32_CLOCK_MODULATION" },
571                 { 0x019b, "IA32_THERM_INTERRUPT" },
572                 { 0x019c, "IA32_THERM_STATUS" },
573                 { 0x019d, "GV_THERM" },
574                 { 0x01d9, "IA32_DEBUGCTL" },
575                 { 0x0200, "IA32_MTRR_PHYSBASE0" },
576                 { 0x0201, "IA32_MTRR_PHYSMASK0" },
577                 { 0x0202, "IA32_MTRR_PHYSBASE1" },
578                 { 0x0203, "IA32_MTRR_PHYSMASK1" },
579                 { 0x0204, "IA32_MTRR_PHYSBASE2" },
580                 { 0x0205, "IA32_MTRR_PHYSMASK2" },
581                 { 0x0206, "IA32_MTRR_PHYSBASE3" },
582                 { 0x0207, "IA32_MTRR_PHYSMASK3" },
583                 { 0x0208, "IA32_MTRR_PHYSBASE4" },
584                 { 0x0209, "IA32_MTRR_PHYSMASK4" },
585                 { 0x020a, "IA32_MTRR_PHYSBASE5" },
586                 { 0x020b, "IA32_MTRR_PHYSMASK5" },
587                 { 0x020c, "IA32_MTRR_PHYSBASE6" },
588                 { 0x020d, "IA32_MTRR_PHYSMASK6" },
589                 { 0x020e, "IA32_MTRR_PHYSBASE7" },
590                 { 0x020f, "IA32_MTRR_PHYSMASK7" },
591                 { 0x0250, "IA32_MTRR_FIX64K_00000" },
592                 { 0x0258, "IA32_MTRR_FIX16K_80000" },
593                 { 0x0259, "IA32_MTRR_FIX16K_A0000" },
594                 { 0x0268, "IA32_MTRR_FIX4K_C0000" },
595                 { 0x0269, "IA32_MTRR_FIX4K_C8000" },
596                 { 0x026a, "IA32_MTRR_FIX4K_D0000" },
597                 { 0x026b, "IA32_MTRR_FIX4K_D8000" },
598                 { 0x026c, "IA32_MTRR_FIX4K_E0000" },
599                 { 0x026d, "IA32_MTRR_FIX4K_E8000" },
600                 { 0x026e, "IA32_MTRR_FIX4K_F0000" },
601                 { 0x026f, "IA32_MTRR_FIX4K_F8000" },
602                 { 0x02ff, "IA32_MTRR_DEF_TYPE" },
603                 //{ 0x00c000080, "IA32_CR_EFER" }, // Seems to be RO
604         };
605
606         fd_msr = open("/dev/cpu/0/msr", O_RDWR);
607         if (fd_msr < 0) {
608                 perror("Error while opening /dev/cpu/0/msr");
609                 printf("Did you run 'modprobe msr'?\n");
610                 return -1;
611         }
612
613         printf("\n===================== SHARED MSRs (All Cores) =====================\n");
614
615         for (i = 0; i < ARRAY_SIZE(global_msrs); i++) {
616                 msr = rdmsr(global_msrs[i].number);
617                 printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
618                        global_msrs[i].number, msr.hi, msr.lo,
619                        global_msrs[i].name);
620         }
621
622         close(fd_msr);
623
624         for (core = 0; core < 8; core++) {
625                 char msrfilename[64];
626                 memset(msrfilename, 0, 64);
627                 sprintf(msrfilename, "/dev/cpu/%d/msr", core);
628
629                 fd_msr = open(msrfilename, O_RDWR);
630
631                 /* If the file is not there, we're probably through. No error,
632                  * since we successfully opened /dev/cpu/0/msr before.
633                  */
634                 if (fd_msr < 0)
635                         break;
636
637                 printf("\n====================== UNIQUE MSRs  (core %d) ======================\n", core);
638
639                 for (i = 0; i < ARRAY_SIZE(per_core_msrs); i++) {
640                         msr = rdmsr(per_core_msrs[i].number);
641                         printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
642                                per_core_msrs[i].number, msr.hi, msr.lo,
643                                per_core_msrs[i].name);
644                 }
645
646                 close(fd_msr);
647         }
648
649         if (msr_readerror)
650                 printf("\n(*) Some MSRs could not be read. The marked values are unreliable.\n");
651
652         return 0;
653 }
654
655 void print_version(void)
656 {
657         printf("inteltool v%s -- ", INTELTOOL_VERSION);
658         printf("Copyright (C) 2008 coresystems GmbH\n\n");
659         printf(
660     "This program is free software: you can redistribute it and/or modify\n"
661     "it under the terms of the GNU General Public License as published by\n"
662     "the Free Software Foundation, version 2 of the License.\n\n"
663     "This program is distributed in the hope that it will be useful,\n"
664     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
665     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
666     "GNU General Public License for more details.\n\n"
667     "You should have received a copy of the GNU General Public License\n"
668     "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n");
669 }
670
671 void print_usage(const char *name)
672 {
673         printf("usage: %s [-vh?grpmedPMa]\n", name);
674         printf("\n"
675              "   -v | --version:                   print the version\n"
676              "   -h | --help:                      print this help\n\n"
677              "   -g | --gpio:                      dump soutbridge GPIO registers\n"
678              "   -r | --rcba:                      dump soutbridge RCBA registers\n"
679              "   -p | --pmbase:                    dump soutbridge Power Management registers\n\n"
680              "   -m | --mchbar:                    dump northbridge Memory Controller registers\n"
681              "   -e | --epbar:                     dump northbridge EPBAR registers\n"
682              "   -d | --dmibar:                    dump northbridge DMIBAR registers\n"
683              "   -P | --pciexpress:                dump northbridge PCIEXBAR registers\n\n"
684              "   -M | --msrs:                      dump CPU MSRs\n"
685              "   -a | --all:                       dump all known registers\n"
686              "\n");
687         exit(1);
688 }
689
690 int main(int argc, char *argv[])
691 {
692         struct pci_access *pacc;
693         struct pci_dev *sb, *nb;
694         int i, opt, option_index = 0;
695
696         char *sbname = "unknown", *nbname = "unknown";
697
698         int dump_gpios = 0, dump_mchbar = 0, dump_rcba = 0;
699         int dump_pmbase = 0, dump_epbar = 0, dump_dmibar = 0;
700         int dump_pciexbar = 0, dump_coremsrs = 0;
701
702         static struct option long_options[] = {
703                 {"version", 0, 0, 'v'},
704                 {"help", 0, 0, 'h'},
705                 {"gpios", 0, 0, 'g'},
706                 {"mchbar", 0, 0, 'm'},
707                 {"rcba", 0, 0, 'r'},
708                 {"pmbase", 0, 0, 'p'},
709                 {"epbar", 0, 0, 'e'},
710                 {"dmibar", 0, 0, 'd'},
711                 {"pciexpress", 0, 0, 'P'},
712                 {"msrs", 0, 0, 'M'},
713                 {"all", 0, 0, 'a'},
714                 {0, 0, 0, 0}
715         };
716
717         while ((opt = getopt_long(argc, argv, "vh?grpmedPMa",
718                                   long_options, &option_index)) != EOF) {
719                 switch (opt) {
720                 case 'v':
721                         print_version();
722                         exit(0);
723                         break;
724                 case 'g':
725                         dump_gpios = 1;
726                         break;
727                 case 'm':
728                         dump_mchbar = 1;
729                         break;
730                 case 'r':
731                         dump_rcba = 1;
732                         break;
733                 case 'p':
734                         dump_pmbase = 1;
735                         break;
736                 case 'e':
737                         dump_epbar = 1;
738                         break;
739                 case 'd':
740                         dump_dmibar = 1;
741                         break;
742                 case 'P':
743                         dump_pciexbar = 1;
744                         break;
745                 case 'M':
746                         dump_coremsrs = 1;
747                         break;
748                 case 'a':
749                         dump_gpios = 1;
750                         dump_mchbar = 1;
751                         dump_rcba = 1;
752                         dump_pmbase = 1;
753                         dump_epbar = 1;
754                         dump_dmibar = 1;
755                         dump_pciexbar = 1;
756                         dump_coremsrs = 1;
757                         break;
758                 case 'h':
759                 case '?':
760                 default:
761                         print_usage(argv[0]);
762                         exit(0);
763                         break;
764                 }
765         }
766
767         if (iopl(3)) {
768                 printf("You need to be root.\n");
769                 exit(1);
770         }
771
772         if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
773                 perror("Can not open /dev/mem");
774                 exit(1);
775         }
776
777         pacc = pci_alloc();
778         pci_init(pacc);
779         pci_scan_bus(pacc);
780
781         /* Find the required devices */
782
783         sb = pci_get_dev(pacc, 0, 0, 0x1f, 0);
784         if (!sb) {
785                 printf("No southbridge found.\n");
786                 exit(1);
787         }
788
789         pci_fill_info(sb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
790
791         if (sb->vendor_id != PCI_VENDOR_ID_INTEL) {
792                 printf("Not an Intel(R) southbridge.\n");
793                 exit(1);
794         }
795
796         nb = pci_get_dev(pacc, 0, 0, 0x00, 0);
797         if (!nb) {
798                 printf("No northbridge found.\n");
799                 exit(1);
800         }
801
802         pci_fill_info(nb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
803
804         if (nb->vendor_id != PCI_VENDOR_ID_INTEL) {
805                 printf("Not an Intel(R) northbridge.\n");
806                 exit(1);
807         }
808
809         /* TODO check cpuid, too */
810
811         /* Determine names */
812         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
813                 if (nb->device_id == supported_chips_list[i].device_id)
814                         nbname = supported_chips_list[i].name;
815         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
816                 if (sb->device_id == supported_chips_list[i].device_id)
817                         sbname = supported_chips_list[i].name;
818
819         printf("Intel Northbridge: %04x:%04x (%s)\n", 
820                 nb->vendor_id, nb->device_id, nbname);
821
822         printf("Intel Southbridge: %04x:%04x (%s)\n", 
823                 sb->vendor_id, sb->device_id, sbname);
824
825         /* Now do the deed */
826
827         if (dump_gpios) {
828                 print_gpios(sb);
829                 printf("\n\n");
830         }
831
832         if (dump_rcba) {
833                 print_rcba(sb);
834                 printf("\n\n");
835         }
836
837         if (dump_pmbase) {
838                 print_pmbase(sb);
839                 printf("\n\n");
840         }
841
842         if (dump_mchbar) {
843                 print_mchbar(nb);
844                 printf("\n\n");
845         }
846
847         if (dump_epbar) {
848                 print_epbar(nb);
849                 printf("\n\n");
850         }
851
852         if (dump_dmibar) {
853                 print_dmibar(nb);
854                 printf("\n\n");
855         }
856
857         if (dump_pciexbar) {
858                 print_pciexbar(nb);
859                 printf("\n\n");
860         }
861
862         if (dump_coremsrs) {
863                 print_intel_core_msrs();
864                 printf("\n\n");
865         }
866
867         /* Clean up */
868         pci_free_dev(nb);
869         pci_free_dev(sb);
870         pci_cleanup(pacc);
871
872         return 0;
873 }