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