1921526a4a269a8389cbf65b50020beea33f9de0
[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
51 /* CPUID 0x80000001 EDX flags */
52 const char *intel_cap_extended_edx_flags[] = {
53         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
54         NULL, NULL, NULL, "SYSCALL", NULL, NULL, NULL, NULL,
55         NULL, NULL, NULL, NULL, "xd", NULL, NULL, NULL,
56         NULL, NULL, NULL, NULL, NULL, "em64t", NULL, NULL,
57 };
58
59 /* CPUID 0x80000001 ECX flags */
60 const char *intel_cap_extended_ecx_flags[] = {
61         "lahf_lm", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
62         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
63         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
64         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
65 };
66
67 const char *amd_cap_generic_ecx_flags[] = {
68         "sse3", NULL, NULL, "mwait", NULL, NULL, NULL, NULL,
69         NULL, NULL, NULL, NULL, NULL, "cmpxchg16b", NULL, NULL,
70         NULL, NULL, NULL, NULL, NULL, NULL, NULL, "popcnt",
71         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
72 };
73
74 const char *amd_cap_extended_edx_flags[] = {
75         "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
76         "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov",
77         "pat", "pse36", NULL, "mp", "nx", NULL, "mmxext", "mmx",
78         "fxsr", "ffxsr", "page1gb", "rdtscp",
79         NULL, "lm", "3dnowext", "3dnow"
80 }; /* "mp" defined for CPUs prior to AMD family 0xf */
81
82 const char *amd_cap_extended_ecx_flags[] = {
83         "lahf/sahf", "CmpLegacy", "svm", "ExtApicSpace",
84         "LockMovCr0", "abm", "sse4a", "misalignsse",
85         "3dnowPref", "osvw", "ibs", NULL, "skinit", "wdt", NULL, NULL,
86         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
87         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
88 };
89
90 static unsigned long vendor;
91 static unsigned int cpu_khz;
92
93 void decode_flags(WINDOW *win, unsigned long reg, const char **flags, int *row)
94 {
95         int index = 0;
96         int i;
97         int lrow = *row;
98
99         wmove(win, lrow, 2);
100
101         for (i = 0; i < 32; i++) {
102                 if (flags[i] == NULL)
103                         continue;
104
105                 if (reg & (1 << i))
106                         wprintw(win, "%s ", flags[i]);
107
108                 if (i && (i % 16) == 0) {
109                         lrow++;
110                         wmove(win, lrow, 2);
111                 }
112         }
113
114         *row = lrow;
115 }
116
117 static void get_features(WINDOW *win, int *row)
118 {
119         unsigned long eax, ebx, ecx, edx;
120         int index = 0;
121         int lrow = *row;
122
123         wmove(win, lrow++, 1);
124         wprintw(win, "Features: ");
125
126         docpuid(0x00000001, &eax, &ebx, &ecx, &edx);
127         decode_flags(win, edx, generic_cap_flags, &lrow);
128
129         lrow++;
130
131         switch (vendor) {
132         case VENDOR_AMD:
133                 wmove(win, lrow++, 1);
134                 wprintw(win, "AMD Extended Flags: ");
135                 decode_flags(win, ecx, amd_cap_generic_ecx_flags, &lrow);
136                 docpuid(0x80000001, &eax, &ebx, &ecx, &edx);
137                 decode_flags(win, edx, amd_cap_extended_edx_flags, &lrow);
138                 decode_flags(win, ecx, amd_cap_extended_ecx_flags, &lrow);
139                 break;
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], name[49], *p;
156         unsigned long eax, ebx, ecx, edx;
157         int i, t;
158
159         p = name;
160
161         for (i = 0x80000002; i <= 0x80000004; i++) {
162                 docpuid(i, &eax, &ebx, &ecx, &edx);
163
164                 if (eax == 0)
165                         break;
166
167                 for (t = 0; t < 4; t++)
168                         *p++ = eax >> (8 * t);
169                 for (t = 0; t < 4; t++)
170                         *p++ = ebx >> (8 * t);
171                 for (t = 0; t < 4; t++)
172                         *p++ = ecx >> (8 * t);
173                 for (t = 0; t < 4; t++)
174                         *p++ = edx >> (8 * t);
175         }
176
177         mvwprintw(win, row, 1, "Processor: %s", name);
178 }
179
180 int cpuinfo_module_redraw(WINDOW * win)
181 {
182         unsigned long eax, ebx, ecx, edx;
183         unsigned int brand;
184         char str[80], *vstr;
185         int row = 2;
186
187         print_module_title(win, "CPU Information");
188
189         docpuid(0, NULL, &vendor, NULL, NULL);
190
191         switch (vendor) {
192         case VENDOR_INTEL:
193                 vstr = "Intel";
194                 break;
195         case VENDOR_AMD:
196                 vstr = "AMD";
197                 break;
198         case VENDOR_CYRIX:
199                 vstr = "Cyrix";
200                 break;
201         case VENDOR_IDT:
202                 vstr = "IDT";
203                 break;
204         case VENDOR_GEODE:
205                 vstr = "NatSemi Geode";
206                 break;
207         case VENDOR_RISE:
208         case VENDOR_RISE2:
209                 vstr = "RISE";
210                 break;
211         case VENDOR_SIS:
212                 vstr = "SiS";
213         }
214
215         mvwprintw(win, row++, 1, "Vendor: %s", vstr);
216
217         do_name(win, row++);
218
219         docpuid(0x00000001, &eax, &ebx, &ecx, &edx);
220
221         mvwprintw(win, row++, 1, "Family: %X", (eax >> 8) & 0x0f);
222         mvwprintw(win, row++, 1, "Model: %X",
223                   ((eax >> 4) & 0xf) | ((eax >> 16) & 0xf) << 4);
224
225         mvwprintw(win, row++, 1, "Stepping: %X", eax & 0xf);
226
227         if (vendor == VENDOR_AMD) {
228                 docpuid(0x80000001, &eax, &ebx, &ecx, &edx);
229                 brand = ((ebx >> 9) & 0x1f);
230
231                 mvwprintw(win, row++, 1, "Brand: %X", brand);
232         }
233
234         if (cpu_khz != 0)
235                 mvwprintw(win, row++, 1, "CPU Speed: %d Mhz", cpu_khz / 1000);
236         else
237                 mvwprintw(win, row++, 1, "CPU Speed: Error");
238
239         row++;
240         get_features(win, &row);
241 }
242
243 unsigned int getticks(void)
244 {
245         unsigned long long start, end;
246
247         /* Read the number of ticks during the period. */
248         start = rdtsc();
249         mdelay(100);
250         end = rdtsc();
251
252         return (unsigned int)((end - start) / 100);
253 }
254
255 int cpuinfo_module_init(void)
256 {
257         cpu_khz = getticks();
258 }
259
260 struct coreinfo_module cpuinfo_module = {
261         .name = "CPU Info",
262         .init = cpuinfo_module_init,
263         .redraw = cpuinfo_module_redraw,
264         .handle = NULL,
265 };