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