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