use seperate array for core 2 cpus (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         static const msr_entry_t model6fx_global_msrs[] = {
624                 { 0x0017, "IA32_PLATFORM_ID" },
625                 { 0x002a, "EBL_CR_POWERON" },
626                 { 0x003f, "IA32_TEMPERATURE_OFFSET" },
627                 { 0x00a8, "EMTTM_CR_TABLE0" },
628                 { 0x00a9, "EMTTM_CR_TABLE1" },
629                 { 0x00aa, "EMTTM_CR_TABLE2" },
630                 { 0x00ab, "EMTTM_CR_TABLE3" },
631                 { 0x00ac, "EMTTM_CR_TABLE4" },
632                 { 0x00ad, "EMTTM_CR_TABLE5" },
633                 { 0x00cd, "FSB_CLOCK_STS" },
634                 { 0x00e2, "PMG_CST_CONFIG_CONTROL" },
635                 { 0x00e3, "PMG_IO_BASE_ADDR" },
636                 { 0x00e4, "PMG_IO_CAPTURE_ADDR" },
637                 { 0x00ee, "EXT_CONFIG" },
638                 { 0x011e, "BBL_CR_CTL3" },
639                 { 0x0194, "CLOCK_FLEX_MAX" },
640                 { 0x0198, "IA32_PERF_STATUS" },
641                 { 0x01a0, "IA32_MISC_ENABLES" },
642                 { 0x01aa, "PIC_SENS_CFG" },
643                 { 0x0400, "IA32_MC0_CTL" },
644                 { 0x0401, "IA32_MC0_STATUS" },
645                 { 0x0402, "IA32_MC0_ADDR" },
646                 //{ 0x0403, "IA32_MC0_MISC" }, // Seems to be RO
647                 { 0x040c, "IA32_MC4_CTL" },
648                 { 0x040d, "IA32_MC4_STATUS" },
649                 { 0x040e, "IA32_MC4_ADDR" },
650                 //{ 0x040f, "IA32_MC4_MISC" } // Seems to be RO
651         };
652
653         static const msr_entry_t model6fx_per_core_msrs[] = {
654                 { 0x0010, "IA32_TIME_STAMP_COUNTER" },
655                 { 0x001b, "IA32_APIC_BASE" },
656                 { 0x003a, "IA32_FEATURE_CONTROL" },
657                 //{ 0x0079, "IA32_BIOS_UPDT_TRIG" }, // Seems to be RO
658                 { 0x008b, "IA32_BIOS_SIGN_ID" },
659                 { 0x00e1, "SMM_CST_MISC_INFO" },
660                 { 0x00e7, "IA32_MPERF" },
661                 { 0x00e8, "IA32_APERF" },
662                 { 0x00fe, "IA32_MTRRCAP" },
663                 { 0x0179, "IA32_MCG_CAP" },
664                 { 0x017a, "IA32_MCG_STATUS" },
665                 { 0x0199, "IA32_PERF_CONTROL" },
666                 { 0x019a, "IA32_THERM_CTL" },
667                 { 0x019b, "IA32_THERM_INTERRUPT" },
668                 { 0x019c, "IA32_THERM_STATUS" },
669                 { 0x019d, "MSR_THERM2_CTL" },
670                 { 0x01d9, "IA32_DEBUGCTL" },
671                 { 0x0200, "IA32_MTRR_PHYSBASE0" },
672                 { 0x0201, "IA32_MTRR_PHYSMASK0" },
673                 { 0x0202, "IA32_MTRR_PHYSBASE1" },
674                 { 0x0203, "IA32_MTRR_PHYSMASK1" },
675                 { 0x0204, "IA32_MTRR_PHYSBASE2" },
676                 { 0x0205, "IA32_MTRR_PHYSMASK2" },
677                 { 0x0206, "IA32_MTRR_PHYSBASE3" },
678                 { 0x0207, "IA32_MTRR_PHYSMASK3" },
679                 { 0x0208, "IA32_MTRR_PHYSBASE4" },
680                 { 0x0209, "IA32_MTRR_PHYSMASK4" },
681                 { 0x020a, "IA32_MTRR_PHYSBASE5" },
682                 { 0x020b, "IA32_MTRR_PHYSMASK5" },
683                 { 0x020c, "IA32_MTRR_PHYSBASE6" },
684                 { 0x020d, "IA32_MTRR_PHYSMASK6" },
685                 { 0x020e, "IA32_MTRR_PHYSBASE7" },
686                 { 0x020f, "IA32_MTRR_PHYSMASK7" },
687                 { 0x0250, "IA32_MTRR_FIX64K_00000" },
688                 { 0x0258, "IA32_MTRR_FIX16K_80000" },
689                 { 0x0259, "IA32_MTRR_FIX16K_A0000" },
690                 { 0x0268, "IA32_MTRR_FIX4K_C0000" },
691                 { 0x0269, "IA32_MTRR_FIX4K_C8000" },
692                 { 0x026a, "IA32_MTRR_FIX4K_D0000" },
693                 { 0x026b, "IA32_MTRR_FIX4K_D8000" },
694                 { 0x026c, "IA32_MTRR_FIX4K_E0000" },
695                 { 0x026d, "IA32_MTRR_FIX4K_E8000" },
696                 { 0x026e, "IA32_MTRR_FIX4K_F0000" },
697                 { 0x026f, "IA32_MTRR_FIX4K_F8000" },
698                 { 0x02ff, "IA32_MTRR_DEF_TYPE" },
699                 //{ 0x00c000080, "IA32_CR_EFER" }, // Seems to be RO
700         };
701
702         typedef struct {
703                 unsigned int model;
704                 const msr_entry_t *global_msrs;
705                 unsigned int num_global_msrs;
706                 const msr_entry_t *per_core_msrs;
707                 unsigned int num_per_core_msrs;
708         } cpu_t;
709
710         cpu_t cpulist[] = {
711                 { 0x006e0, model6ex_global_msrs, ARRAY_SIZE(model6ex_global_msrs), model6ex_per_core_msrs, ARRAY_SIZE(model6ex_per_core_msrs) },
712                 { 0x006f0, model6fx_global_msrs, ARRAY_SIZE(model6fx_global_msrs), model6fx_per_core_msrs, ARRAY_SIZE(model6fx_per_core_msrs) },
713         };
714
715         cpu_t *cpu = NULL;
716
717         /* Get CPU family and model, not the stepping 
718          * (TODO: extended family/model)
719          */
720         id = cpuid(1) & 0xff0;
721         for (i = 0; i < ARRAY_SIZE(cpulist); i++) {
722                 if(cpulist[i].model == id) {
723                         cpu = &cpulist[i];
724                         break;
725                 }
726         }
727
728         if (!cpu) {
729                 printf("Error: Dumping MSRs on this CPU (0x%06x) is not (yet) supported.\n", id);
730                 return -1;
731         }
732
733         fd_msr = open("/dev/cpu/0/msr", O_RDWR);
734         if (fd_msr < 0) {
735                 perror("Error while opening /dev/cpu/0/msr");
736                 printf("Did you run 'modprobe msr'?\n");
737                 return -1;
738         }
739
740         printf("\n===================== SHARED MSRs (All Cores) =====================\n");
741
742         for (i = 0; i < cpu->num_global_msrs; i++) {
743                 msr = rdmsr(cpu->global_msrs[i].number);
744                 printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
745                        cpu->global_msrs[i].number, msr.hi, msr.lo,
746                        cpu->global_msrs[i].name);
747         }
748
749         close(fd_msr);
750
751         for (core = 0; core < 8; core++) {
752                 char msrfilename[64];
753                 memset(msrfilename, 0, 64);
754                 sprintf(msrfilename, "/dev/cpu/%d/msr", core);
755
756                 fd_msr = open(msrfilename, O_RDWR);
757
758                 /* If the file is not there, we're probably through. No error,
759                  * since we successfully opened /dev/cpu/0/msr before.
760                  */
761                 if (fd_msr < 0)
762                         break;
763
764                 printf("\n====================== UNIQUE MSRs  (core %d) ======================\n", core);
765
766                 for (i = 0; i < cpu->num_per_core_msrs; i++) {
767                         msr = rdmsr(cpu->per_core_msrs[i].number);
768                         printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
769                                cpu->per_core_msrs[i].number, msr.hi, msr.lo,
770                                cpu->per_core_msrs[i].name);
771                 }
772
773                 close(fd_msr);
774         }
775
776         if (msr_readerror)
777                 printf("\n(*) Some MSRs could not be read. The marked values are unreliable.\n");
778
779         return 0;
780 }
781
782 void print_version(void)
783 {
784         printf("inteltool v%s -- ", INTELTOOL_VERSION);
785         printf("Copyright (C) 2008 coresystems GmbH\n\n");
786         printf(
787     "This program is free software: you can redistribute it and/or modify\n"
788     "it under the terms of the GNU General Public License as published by\n"
789     "the Free Software Foundation, version 2 of the License.\n\n"
790     "This program is distributed in the hope that it will be useful,\n"
791     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
792     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
793     "GNU General Public License for more details.\n\n"
794     "You should have received a copy of the GNU General Public License\n"
795     "along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\n");
796 }
797
798 void print_usage(const char *name)
799 {
800         printf("usage: %s [-vh?grpmedPMa]\n", name);
801         printf("\n"
802              "   -v | --version:                   print the version\n"
803              "   -h | --help:                      print this help\n\n"
804              "   -g | --gpio:                      dump soutbridge GPIO registers\n"
805              "   -r | --rcba:                      dump soutbridge RCBA registers\n"
806              "   -p | --pmbase:                    dump soutbridge Power Management registers\n\n"
807              "   -m | --mchbar:                    dump northbridge Memory Controller registers\n"
808              "   -e | --epbar:                     dump northbridge EPBAR registers\n"
809              "   -d | --dmibar:                    dump northbridge DMIBAR registers\n"
810              "   -P | --pciexpress:                dump northbridge PCIEXBAR registers\n\n"
811              "   -M | --msrs:                      dump CPU MSRs\n"
812              "   -a | --all:                       dump all known registers\n"
813              "\n");
814         exit(1);
815 }
816
817 int main(int argc, char *argv[])
818 {
819         struct pci_access *pacc;
820         struct pci_dev *sb, *nb;
821         int i, opt, option_index = 0;
822         unsigned int id;
823
824         char *sbname = "unknown", *nbname = "unknown";
825
826         int dump_gpios = 0, dump_mchbar = 0, dump_rcba = 0;
827         int dump_pmbase = 0, dump_epbar = 0, dump_dmibar = 0;
828         int dump_pciexbar = 0, dump_coremsrs = 0;
829
830         static struct option long_options[] = {
831                 {"version", 0, 0, 'v'},
832                 {"help", 0, 0, 'h'},
833                 {"gpios", 0, 0, 'g'},
834                 {"mchbar", 0, 0, 'm'},
835                 {"rcba", 0, 0, 'r'},
836                 {"pmbase", 0, 0, 'p'},
837                 {"epbar", 0, 0, 'e'},
838                 {"dmibar", 0, 0, 'd'},
839                 {"pciexpress", 0, 0, 'P'},
840                 {"msrs", 0, 0, 'M'},
841                 {"all", 0, 0, 'a'},
842                 {0, 0, 0, 0}
843         };
844
845         while ((opt = getopt_long(argc, argv, "vh?grpmedPMa",
846                                   long_options, &option_index)) != EOF) {
847                 switch (opt) {
848                 case 'v':
849                         print_version();
850                         exit(0);
851                         break;
852                 case 'g':
853                         dump_gpios = 1;
854                         break;
855                 case 'm':
856                         dump_mchbar = 1;
857                         break;
858                 case 'r':
859                         dump_rcba = 1;
860                         break;
861                 case 'p':
862                         dump_pmbase = 1;
863                         break;
864                 case 'e':
865                         dump_epbar = 1;
866                         break;
867                 case 'd':
868                         dump_dmibar = 1;
869                         break;
870                 case 'P':
871                         dump_pciexbar = 1;
872                         break;
873                 case 'M':
874                         dump_coremsrs = 1;
875                         break;
876                 case 'a':
877                         dump_gpios = 1;
878                         dump_mchbar = 1;
879                         dump_rcba = 1;
880                         dump_pmbase = 1;
881                         dump_epbar = 1;
882                         dump_dmibar = 1;
883                         dump_pciexbar = 1;
884                         dump_coremsrs = 1;
885                         break;
886                 case 'h':
887                 case '?':
888                 default:
889                         print_usage(argv[0]);
890                         exit(0);
891                         break;
892                 }
893         }
894
895         if (iopl(3)) {
896                 printf("You need to be root.\n");
897                 exit(1);
898         }
899
900         if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
901                 perror("Can not open /dev/mem");
902                 exit(1);
903         }
904
905         pacc = pci_alloc();
906         pci_init(pacc);
907         pci_scan_bus(pacc);
908
909         /* Find the required devices */
910
911         sb = pci_get_dev(pacc, 0, 0, 0x1f, 0);
912         if (!sb) {
913                 printf("No southbridge found.\n");
914                 exit(1);
915         }
916
917         pci_fill_info(sb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
918
919         if (sb->vendor_id != PCI_VENDOR_ID_INTEL) {
920                 printf("Not an Intel(R) southbridge.\n");
921                 exit(1);
922         }
923
924         nb = pci_get_dev(pacc, 0, 0, 0x00, 0);
925         if (!nb) {
926                 printf("No northbridge found.\n");
927                 exit(1);
928         }
929
930         pci_fill_info(nb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
931
932         if (nb->vendor_id != PCI_VENDOR_ID_INTEL) {
933                 printf("Not an Intel(R) northbridge.\n");
934                 exit(1);
935         }
936
937         id = cpuid(1);
938         printf("Intel CPU: Family %x, Model %x\n", 
939                         (id >> 8) & 0xf, (id >> 4) & 0xf);
940
941         /* Determine names */
942         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
943                 if (nb->device_id == supported_chips_list[i].device_id)
944                         nbname = supported_chips_list[i].name;
945         for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
946                 if (sb->device_id == supported_chips_list[i].device_id)
947                         sbname = supported_chips_list[i].name;
948
949         printf("Intel Northbridge: %04x:%04x (%s)\n", 
950                 nb->vendor_id, nb->device_id, nbname);
951
952         printf("Intel Southbridge: %04x:%04x (%s)\n", 
953                 sb->vendor_id, sb->device_id, sbname);
954
955         /* Now do the deed */
956
957         if (dump_gpios) {
958                 print_gpios(sb);
959                 printf("\n\n");
960         }
961
962         if (dump_rcba) {
963                 print_rcba(sb);
964                 printf("\n\n");
965         }
966
967         if (dump_pmbase) {
968                 print_pmbase(sb);
969                 printf("\n\n");
970         }
971
972         if (dump_mchbar) {
973                 print_mchbar(nb);
974                 printf("\n\n");
975         }
976
977         if (dump_epbar) {
978                 print_epbar(nb);
979                 printf("\n\n");
980         }
981
982         if (dump_dmibar) {
983                 print_dmibar(nb);
984                 printf("\n\n");
985         }
986
987         if (dump_pciexbar) {
988                 print_pciexbar(nb);
989                 printf("\n\n");
990         }
991
992         if (dump_coremsrs) {
993                 print_intel_core_msrs();
994                 printf("\n\n");
995         }
996
997         /* Clean up */
998         pci_free_dev(nb);
999         pci_free_dev(sb);
1000         pci_cleanup(pacc);
1001
1002         return 0;
1003 }