bde93dee9db66873d3aab88b251bf238a912e2a6
[coreboot.git] / util / inteltool / cpu.c
1 /*
2  * inteltool - dump all registers on an Intel CPU + chipset based system.
3  *
4  * Copyright (C) 2008 by coresystems GmbH 
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "inteltool.h"
28
29 int fd_msr;
30
31 unsigned int cpuid(unsigned int op)
32 {
33         unsigned int ret;
34         unsigned int dummy2, dummy3, dummy4;
35         asm volatile ( 
36                 "pushl %%ebx    \n"
37                 "cpuid          \n"
38                 "movl %%ebx, %1 \n"
39                 "popl %%ebx     \n"
40                 : "=a" (ret), "=r" (dummy2), "=c" (dummy3), "=d" (dummy4)
41                 : "a" (op)
42                 : "cc"
43         );
44         return ret;
45 }
46
47 #ifndef DARWIN
48 int msr_readerror = 0;
49
50 msr_t rdmsr(int addr)
51 {
52         uint8_t buf[8];
53         msr_t msr = { 0xffffffff, 0xffffffff };
54
55         if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
56                 perror("Could not lseek() to MSR");
57                 close(fd_msr);
58                 exit(1);
59         }
60
61         if (read(fd_msr, buf, 8) == 8) {
62                 msr.lo = *(uint32_t *)buf;
63                 msr.hi = *(uint32_t *)(buf + 4);
64
65                 return msr;
66         }
67
68         if (errno == 5) {
69                 printf(" (*)"); // Not all bits of the MSR could be read
70                 msr_readerror = 1;
71         } else {
72                 // A severe error.
73                 perror("Could not read() MSR");
74                 close(fd_msr);
75                 exit(1);
76         }
77
78         return msr;
79 }
80 #endif
81
82 int print_intel_core_msrs(void)
83 {
84         unsigned int i, core, id;
85         msr_t msr;
86
87 #define IA32_PLATFORM_ID                0x0017
88 #define EBL_CR_POWERON                  0x002a
89 #define FSB_CLK_STS                     0x00cd
90 #define IA32_TIME_STAMP_COUNTER         0x0010
91 #define IA32_APIC_BASE                  0x001b
92
93         typedef struct {
94                 int number;
95                 char *name;
96         } msr_entry_t;
97
98         static const msr_entry_t model6ex_global_msrs[] = {
99                 { 0x0017, "IA32_PLATFORM_ID" },
100                 { 0x002a, "EBL_CR_POWERON" },
101                 { 0x00cd, "FSB_CLOCK_STS" },
102                 { 0x00ce, "FSB_CLOCK_VCC" },
103                 { 0x00e2, "CLOCK_CST_CONFIG_CONTROL" },
104                 { 0x00e3, "PMG_IO_BASE_ADDR" },
105                 { 0x00e4, "PMG_IO_CAPTURE_ADDR" },
106                 { 0x00ee, "EXT_CONFIG" },
107                 { 0x011e, "BBL_CR_CTL3" },
108                 { 0x0194, "CLOCK_FLEX_MAX" },
109                 { 0x0198, "IA32_PERF_STATUS" },
110                 { 0x01a0, "IA32_MISC_ENABLES" },
111                 { 0x01aa, "PIC_SENS_CFG" },
112                 { 0x0400, "IA32_MC0_CTL" },
113                 { 0x0401, "IA32_MC0_STATUS" },
114                 { 0x0402, "IA32_MC0_ADDR" },
115                 //{ 0x0403, "IA32_MC0_MISC" }, // Seems to be RO
116                 { 0x040c, "IA32_MC4_CTL" },
117                 { 0x040d, "IA32_MC4_STATUS" },
118                 { 0x040e, "IA32_MC4_ADDR" },
119                 //{ 0x040f, "IA32_MC4_MISC" } // Seems to be RO
120         };
121
122         static const msr_entry_t model6ex_per_core_msrs[] = {
123                 { 0x0010, "IA32_TIME_STAMP_COUNTER" },
124                 { 0x001b, "IA32_APIC_BASE" },
125                 { 0x003a, "IA32_FEATURE_CONTROL" },
126                 { 0x003f, "IA32_TEMPERATURE_OFFSET" },
127                 //{ 0x0079, "IA32_BIOS_UPDT_TRIG" }, // Seems to be RO
128                 { 0x008b, "IA32_BIOS_SIGN_ID" },
129                 { 0x00e7, "IA32_MPERF" },
130                 { 0x00e8, "IA32_APERF" },
131                 { 0x00fe, "IA32_MTRRCAP" },
132                 { 0x015f, "DTS_CAL_CTRL" },
133                 { 0x0179, "IA32_MCG_CAP" },
134                 { 0x017a, "IA32_MCG_STATUS" },
135                 { 0x0199, "IA32_PERF_CONTROL" },
136                 { 0x019a, "IA32_CLOCK_MODULATION" },
137                 { 0x019b, "IA32_THERM_INTERRUPT" },
138                 { 0x019c, "IA32_THERM_STATUS" },
139                 { 0x019d, "GV_THERM" },
140                 { 0x01d9, "IA32_DEBUGCTL" },
141                 { 0x0200, "IA32_MTRR_PHYSBASE0" },
142                 { 0x0201, "IA32_MTRR_PHYSMASK0" },
143                 { 0x0202, "IA32_MTRR_PHYSBASE1" },
144                 { 0x0203, "IA32_MTRR_PHYSMASK1" },
145                 { 0x0204, "IA32_MTRR_PHYSBASE2" },
146                 { 0x0205, "IA32_MTRR_PHYSMASK2" },
147                 { 0x0206, "IA32_MTRR_PHYSBASE3" },
148                 { 0x0207, "IA32_MTRR_PHYSMASK3" },
149                 { 0x0208, "IA32_MTRR_PHYSBASE4" },
150                 { 0x0209, "IA32_MTRR_PHYSMASK4" },
151                 { 0x020a, "IA32_MTRR_PHYSBASE5" },
152                 { 0x020b, "IA32_MTRR_PHYSMASK5" },
153                 { 0x020c, "IA32_MTRR_PHYSBASE6" },
154                 { 0x020d, "IA32_MTRR_PHYSMASK6" },
155                 { 0x020e, "IA32_MTRR_PHYSBASE7" },
156                 { 0x020f, "IA32_MTRR_PHYSMASK7" },
157                 { 0x0250, "IA32_MTRR_FIX64K_00000" },
158                 { 0x0258, "IA32_MTRR_FIX16K_80000" },
159                 { 0x0259, "IA32_MTRR_FIX16K_A0000" },
160                 { 0x0268, "IA32_MTRR_FIX4K_C0000" },
161                 { 0x0269, "IA32_MTRR_FIX4K_C8000" },
162                 { 0x026a, "IA32_MTRR_FIX4K_D0000" },
163                 { 0x026b, "IA32_MTRR_FIX4K_D8000" },
164                 { 0x026c, "IA32_MTRR_FIX4K_E0000" },
165                 { 0x026d, "IA32_MTRR_FIX4K_E8000" },
166                 { 0x026e, "IA32_MTRR_FIX4K_F0000" },
167                 { 0x026f, "IA32_MTRR_FIX4K_F8000" },
168                 { 0x02ff, "IA32_MTRR_DEF_TYPE" },
169                 //{ 0x00c000080, "IA32_CR_EFER" }, // Seems to be RO
170         };
171
172         static const msr_entry_t model6fx_global_msrs[] = {
173                 { 0x0017, "IA32_PLATFORM_ID" },
174                 { 0x002a, "EBL_CR_POWERON" },
175                 { 0x003f, "IA32_TEMPERATURE_OFFSET" },
176                 { 0x00a8, "EMTTM_CR_TABLE0" },
177                 { 0x00a9, "EMTTM_CR_TABLE1" },
178                 { 0x00aa, "EMTTM_CR_TABLE2" },
179                 { 0x00ab, "EMTTM_CR_TABLE3" },
180                 { 0x00ac, "EMTTM_CR_TABLE4" },
181                 { 0x00ad, "EMTTM_CR_TABLE5" },
182                 { 0x00cd, "FSB_CLOCK_STS" },
183                 { 0x00e2, "PMG_CST_CONFIG_CONTROL" },
184                 { 0x00e3, "PMG_IO_BASE_ADDR" },
185                 { 0x00e4, "PMG_IO_CAPTURE_ADDR" },
186                 { 0x00ee, "EXT_CONFIG" },
187                 { 0x011e, "BBL_CR_CTL3" },
188                 { 0x0194, "CLOCK_FLEX_MAX" },
189                 { 0x0198, "IA32_PERF_STATUS" },
190                 { 0x01a0, "IA32_MISC_ENABLES" },
191                 { 0x01aa, "PIC_SENS_CFG" },
192                 { 0x0400, "IA32_MC0_CTL" },
193                 { 0x0401, "IA32_MC0_STATUS" },
194                 { 0x0402, "IA32_MC0_ADDR" },
195                 //{ 0x0403, "IA32_MC0_MISC" }, // Seems to be RO
196                 { 0x040c, "IA32_MC4_CTL" },
197                 { 0x040d, "IA32_MC4_STATUS" },
198                 { 0x040e, "IA32_MC4_ADDR" },
199                 //{ 0x040f, "IA32_MC4_MISC" } // Seems to be RO
200         };
201
202         static const msr_entry_t model6fx_per_core_msrs[] = {
203                 { 0x0010, "IA32_TIME_STAMP_COUNTER" },
204                 { 0x001b, "IA32_APIC_BASE" },
205                 { 0x003a, "IA32_FEATURE_CONTROL" },
206                 //{ 0x0079, "IA32_BIOS_UPDT_TRIG" }, // Seems to be RO
207                 { 0x008b, "IA32_BIOS_SIGN_ID" },
208                 { 0x00e1, "SMM_CST_MISC_INFO" },
209                 { 0x00e7, "IA32_MPERF" },
210                 { 0x00e8, "IA32_APERF" },
211                 { 0x00fe, "IA32_MTRRCAP" },
212                 { 0x0179, "IA32_MCG_CAP" },
213                 { 0x017a, "IA32_MCG_STATUS" },
214                 { 0x0199, "IA32_PERF_CONTROL" },
215                 { 0x019a, "IA32_THERM_CTL" },
216                 { 0x019b, "IA32_THERM_INTERRUPT" },
217                 { 0x019c, "IA32_THERM_STATUS" },
218                 { 0x019d, "MSR_THERM2_CTL" },
219                 { 0x01d9, "IA32_DEBUGCTL" },
220                 { 0x0200, "IA32_MTRR_PHYSBASE0" },
221                 { 0x0201, "IA32_MTRR_PHYSMASK0" },
222                 { 0x0202, "IA32_MTRR_PHYSBASE1" },
223                 { 0x0203, "IA32_MTRR_PHYSMASK1" },
224                 { 0x0204, "IA32_MTRR_PHYSBASE2" },
225                 { 0x0205, "IA32_MTRR_PHYSMASK2" },
226                 { 0x0206, "IA32_MTRR_PHYSBASE3" },
227                 { 0x0207, "IA32_MTRR_PHYSMASK3" },
228                 { 0x0208, "IA32_MTRR_PHYSBASE4" },
229                 { 0x0209, "IA32_MTRR_PHYSMASK4" },
230                 { 0x020a, "IA32_MTRR_PHYSBASE5" },
231                 { 0x020b, "IA32_MTRR_PHYSMASK5" },
232                 { 0x020c, "IA32_MTRR_PHYSBASE6" },
233                 { 0x020d, "IA32_MTRR_PHYSMASK6" },
234                 { 0x020e, "IA32_MTRR_PHYSBASE7" },
235                 { 0x020f, "IA32_MTRR_PHYSMASK7" },
236                 { 0x0250, "IA32_MTRR_FIX64K_00000" },
237                 { 0x0258, "IA32_MTRR_FIX16K_80000" },
238                 { 0x0259, "IA32_MTRR_FIX16K_A0000" },
239                 { 0x0268, "IA32_MTRR_FIX4K_C0000" },
240                 { 0x0269, "IA32_MTRR_FIX4K_C8000" },
241                 { 0x026a, "IA32_MTRR_FIX4K_D0000" },
242                 { 0x026b, "IA32_MTRR_FIX4K_D8000" },
243                 { 0x026c, "IA32_MTRR_FIX4K_E0000" },
244                 { 0x026d, "IA32_MTRR_FIX4K_E8000" },
245                 { 0x026e, "IA32_MTRR_FIX4K_F0000" },
246                 { 0x026f, "IA32_MTRR_FIX4K_F8000" },
247                 { 0x02ff, "IA32_MTRR_DEF_TYPE" },
248                 //{ 0x00c000080, "IA32_CR_EFER" }, // Seems to be RO
249         };
250
251         typedef struct {
252                 unsigned int model;
253                 const msr_entry_t *global_msrs;
254                 unsigned int num_global_msrs;
255                 const msr_entry_t *per_core_msrs;
256                 unsigned int num_per_core_msrs;
257         } cpu_t;
258
259         cpu_t cpulist[] = {
260                 { 0x006e0, model6ex_global_msrs, ARRAY_SIZE(model6ex_global_msrs), model6ex_per_core_msrs, ARRAY_SIZE(model6ex_per_core_msrs) },
261                 { 0x006f0, model6fx_global_msrs, ARRAY_SIZE(model6fx_global_msrs), model6fx_per_core_msrs, ARRAY_SIZE(model6fx_per_core_msrs) },
262         };
263
264         cpu_t *cpu = NULL;
265
266         /* Get CPU family and model, not the stepping 
267          * (TODO: extended family/model)
268          */
269         id = cpuid(1) & 0xff0;
270         for (i = 0; i < ARRAY_SIZE(cpulist); i++) {
271                 if(cpulist[i].model == id) {
272                         cpu = &cpulist[i];
273                         break;
274                 }
275         }
276
277         if (!cpu) {
278                 printf("Error: Dumping MSRs on this CPU (0x%06x) is not (yet) supported.\n", id);
279                 return -1;
280         }
281
282 #ifndef DARWIN
283         fd_msr = open("/dev/cpu/0/msr", O_RDWR);
284         if (fd_msr < 0) {
285                 perror("Error while opening /dev/cpu/0/msr");
286                 printf("Did you run 'modprobe msr'?\n");
287                 return -1;
288         }
289 #endif
290
291         printf("\n===================== SHARED MSRs (All Cores) =====================\n");
292
293         for (i = 0; i < cpu->num_global_msrs; i++) {
294                 msr = rdmsr(cpu->global_msrs[i].number);
295                 printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
296                        cpu->global_msrs[i].number, msr.hi, msr.lo,
297                        cpu->global_msrs[i].name);
298         }
299
300         close(fd_msr);
301
302         for (core = 0; core < 8; core++) {
303 #ifndef DARWIN
304                 char msrfilename[64];
305                 memset(msrfilename, 0, 64);
306                 sprintf(msrfilename, "/dev/cpu/%d/msr", core);
307
308                 fd_msr = open(msrfilename, O_RDWR);
309
310                 /* If the file is not there, we're probably through. No error,
311                  * since we successfully opened /dev/cpu/0/msr before.
312                  */
313                 if (fd_msr < 0)
314                         break;
315 #endif
316                 printf("\n====================== UNIQUE MSRs  (core %d) ======================\n", core);
317
318                 for (i = 0; i < cpu->num_per_core_msrs; i++) {
319                         msr = rdmsr(cpu->per_core_msrs[i].number);
320                         printf(" MSR 0x%08X = 0x%08X:0x%08X (%s)\n",
321                                cpu->per_core_msrs[i].number, msr.hi, msr.lo,
322                                cpu->per_core_msrs[i].name);
323                 }
324 #ifndef DARWIN
325                 close(fd_msr);
326 #endif
327         }
328
329 #ifndef DARWIN
330         if (msr_readerror)
331                 printf("\n(*) Some MSRs could not be read. The marked values are unreliable.\n");
332 #endif
333         return 0;
334 }
335
336