corinfo: Inital release of the coreinfo code
[coreboot.git] / payloads / coreinfo / cpuinfo_module.c
1 /*
2  * This file is part of the coreinfo project.
3  *
4  * It is derived from the x86info project, which is GPLv2-licensed.
5  *
6  * Copyright (C) 2001-2007 Dave Jones <davej@codemonkey.org.uk>
7  * Copyright (C) 2008 Advanced Micro Devices, Inc.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; version 2 of the License.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include "coreinfo.h"
24 #include <arch/rdtsc.h>
25
26 #define VENDOR_INTEL 0x756e6547
27 #define VENDOR_AMD   0x68747541
28 #define VENDOR_CYRIX 0x69727943
29 #define VENDOR_IDT   0x746e6543
30 #define VENDOR_GEODE 0x646f6547
31 #define VENDOR_RISE  0x52697365
32 #define VENDOR_RISE2 0x65736952
33 #define VENDOR_SIS   0x20536953
34
35 /* CPUID 0x00000001 EDX flags */
36 const char *generic_cap_flags[] = {
37         "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
38         "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov",
39         "pat", "pse36", "psn", "clflsh", NULL, "ds", "acpi", "mmx",
40         "fxsr", "sse", "sse2", "ss", "ht", "tm", NULL, "pbe"
41 };
42
43   /* CPUID 0x00000001 ECX flags */
44 const char *intel_cap_generic_ecx_flags[] = {
45         "sse3", NULL, NULL, "monitor", "ds-cpl", "vmx", NULL, "est",
46         "tm2", "ssse3", "cntx-id", NULL, NULL, "cx16", "xTPR", NULL,
47         NULL, NULL, "dca", NULL, NULL, NULL, NULL, NULL,
48         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
49 };
50 /* CPUID 0x80000001 EDX flags */
51 const char *intel_cap_extended_edx_flags[] = {
52         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
53         NULL, NULL, NULL, "SYSCALL", NULL, NULL, NULL, NULL,
54         NULL, NULL, NULL, NULL, "xd", NULL, NULL, NULL,
55         NULL, NULL, NULL, NULL, NULL, "em64t", NULL, NULL,
56 };
57 /* CPUID 0x80000001 ECX flags */
58 const char *intel_cap_extended_ecx_flags[] = {
59         "lahf_lm", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
60         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
61         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
62         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
63 };
64
65 const char *amd_cap_generic_ecx_flags[] = {
66         "sse3", NULL, NULL, "mwait", NULL, NULL, NULL, NULL,
67         NULL, NULL, NULL, NULL, NULL, "cmpxchg16b", NULL, NULL,
68         NULL, NULL, NULL, NULL, NULL, NULL, NULL, "popcnt",
69         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
70 };
71
72 const char *amd_cap_extended_edx_flags[] = {
73         "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
74         "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov",
75         "pat", "pse36", NULL, "mp", "nx", NULL, "mmxext", "mmx",
76         "fxsr", "ffxsr", "page1gb", "rdtscp",
77         NULL, "lm", "3dnowext", "3dnow"
78 }; /* "mp" defined for CPUs prior to AMD family 0xf */
79
80 const char *amd_cap_extended_ecx_flags[] = {
81         "lahf/sahf", "CmpLegacy", "svm", "ExtApicSpace",
82         "LockMovCr0", "abm", "sse4a", "misalignsse",
83         "3dnowPref", "osvw", "ibs", NULL, "skinit", "wdt", NULL, NULL,
84         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
85         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
86 };
87
88 static unsigned long vendor;
89 static unsigned int cpu_khz;
90
91 void decode_flags(WINDOW *win, unsigned long reg, const char **flags, int *row)
92 {
93         int index = 0;
94         int i;
95         int lrow = *row;
96
97         wmove(win, lrow, 2);
98
99         for(i = 0; i < 32; i++) {
100                 if (flags[i] == NULL)
101                         continue;
102
103                 if (reg & (1 << i)) 
104                         wprintw(win, "%s ", flags[i]);
105
106                 if (i && (i % 16) == 0) {
107                         lrow++;
108                         wmove(win, lrow, 2);
109                 }
110         }
111
112         *row = lrow;
113 }
114
115
116 static void get_features(WINDOW *win, int *row)
117 {
118         unsigned long eax, ebx, ecx, edx;
119         int index = 0;
120         int lrow = *row;
121
122         wmove(win, lrow++, 1);
123         wprintw(win, "Features: ");
124
125         docpuid(0x00000001, &eax, &ebx, &ecx, &edx);
126         decode_flags(win, edx, generic_cap_flags, &lrow);
127
128         lrow++;
129
130         switch(vendor) {
131         case VENDOR_AMD:
132                 wmove(win, lrow++, 1);
133                 wprintw(win, "AMD Extended Flags: ");
134                 decode_flags(win, ecx, amd_cap_generic_ecx_flags, &lrow);
135                 docpuid(0x80000001, &eax, &ebx, &ecx, &edx);
136                 decode_flags(win, edx, amd_cap_extended_edx_flags, &lrow);
137                 decode_flags(win, ecx, amd_cap_extended_ecx_flags, &lrow);
138                 break;
139
140         case VENDOR_INTEL:
141                 wmove(win, lrow++, 1);
142                 wprintw(win, "Intel Extended Flags: ");
143                 decode_flags(win, ecx, intel_cap_generic_ecx_flags, &lrow);
144                 docpuid(0x80000001, &eax, &ebx, &ecx, &edx);
145                 decode_flags(win, edx, intel_cap_extended_edx_flags, &lrow);
146                 decode_flags(win, ecx, intel_cap_extended_ecx_flags, &lrow);
147                 break;
148         }
149
150         *row = lrow;
151 }
152
153 static void do_name(WINDOW *win, int row)
154 {
155         char str[80];
156         unsigned long eax, ebx, ecx, edx;
157         int i, t;
158         char name[49], *p;
159
160         p = name;
161
162         for(i = 0x80000002; i <= 0x80000004; i++) {
163                 docpuid(i, &eax, &ebx, &ecx, &edx);
164
165                 if (eax == 0)
166                         break;
167
168                 for(t = 0; t < 4; t++)
169                         *p++ = eax >> (8 * t);
170                 for(t = 0; t < 4; t++)
171                         *p++ = ebx >> (8 * t);
172                 for(t = 0; t < 4; t++)
173                         *p++ = ecx >> (8 * t);
174                 for(t = 0; t < 4; t++)
175                         *p++ = edx >> (8 * t);
176         }
177
178         mvwprintw(win, row,1, "Processor: %s", name);
179 }
180
181 int cpuinfo_module_redraw(WINDOW *win)
182 {
183         unsigned long eax, ebx, ecx, edx;
184
185         unsigned int brand;
186         char str[80];
187         char *vstr;
188         int row = 2;
189
190         print_module_title(win, "CPU Information");
191
192         docpuid(0, NULL, &vendor, NULL, NULL);
193
194         switch(vendor) {
195         case  VENDOR_INTEL:
196                 vstr = "Intel";
197                 break;
198         case VENDOR_AMD:
199                 vstr = "AMD";
200                 break;
201         case VENDOR_CYRIX:
202                 vstr = "Cyrix";
203                 break;
204         case VENDOR_IDT:
205                 vstr = "IDT";
206                 break;
207         case VENDOR_GEODE:
208                 vstr = "NatSemi Geode";
209                 break;
210         case VENDOR_RISE:
211         case VENDOR_RISE2:
212                 vstr = "RISE";
213                 break;
214         case VENDOR_SIS:
215                 vstr = "SiS";
216         }
217
218         mvwprintw(win, row++, 1, "Vendor: %s", vstr);
219
220         do_name(win, row++);
221
222         docpuid(0x00000001, &eax, &ebx, &ecx, &edx);
223
224         mvwprintw(win, row++, 1, "Family: %X",(eax >> 8) & 0x0f);
225         mvwprintw(win, row++, 1, "Model: %X",
226                   ((eax >> 4) & 0xf) | ((eax >> 16) & 0xf) << 4);
227
228         mvwprintw(win, row++, 1, "Stepping: %X", eax & 0xf);
229
230         if (vendor == VENDOR_AMD) {
231                 docpuid(0x80000001, &eax, &ebx, &ecx, &edx);
232                 brand = ((ebx >> 9) & 0x1F);
233
234                 mvwprintw(win, row++, 1,"Brand: %X", brand);
235         }
236
237         if (cpu_khz != 0) {
238                 mvwprintw(win, row++, 1, "CPU Speed: %d Mhz",
239                                 cpu_khz / 1000);
240         }
241         else {
242                 mvwprintw(win, row++, 1, "CPU Speed: Error");
243         }
244
245         row++;
246         get_features(win, &row);
247 }
248
249 unsigned int getticks(void) 
250 {
251         unsigned long long start, end;
252
253         /* Read the number of ticks during the period */
254
255         start = rdtsc();
256         mdelay(100);
257         end = rdtsc();
258
259         return (unsigned int) ((end - start) / 100);
260 }
261
262 int cpuinfo_module_init(void)
263 {
264         cpu_khz = getticks();
265 }
266
267 struct coreinfo_module cpuinfo_module = {
268         .name = "CPU Info",
269         .init = cpuinfo_module_init,
270         .redraw = cpuinfo_module_redraw,
271         .handle = NULL,
272 };