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