Jacob Garber | 07201d7 | 2020-09-08 12:25:44 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | /* It is derived from the x86info project, which is GPLv2-licensed. */ |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 4 | |
| 5 | #include "coreinfo.h" |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 6 | |
Julius Werner | eab2a29 | 2019-03-05 16:55:15 -0800 | [diff] [blame] | 7 | #if CONFIG(MODULE_CPUINFO) |
Jordan Crouse | 646ee3e | 2008-05-06 21:32:52 +0000 | [diff] [blame] | 8 | #include <arch/rdtsc.h> |
Uwe Hermann | ab5b3e0 | 2008-03-31 20:30:18 +0000 | [diff] [blame] | 9 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 10 | #define VENDOR_INTEL 0x756e6547 |
| 11 | #define VENDOR_AMD 0x68747541 |
| 12 | #define VENDOR_CYRIX 0x69727943 |
| 13 | #define VENDOR_IDT 0x746e6543 |
| 14 | #define VENDOR_GEODE 0x646f6547 |
| 15 | #define VENDOR_RISE 0x52697365 |
| 16 | #define VENDOR_RISE2 0x65736952 |
| 17 | #define VENDOR_SIS 0x20536953 |
| 18 | |
| 19 | /* CPUID 0x00000001 EDX flags */ |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 20 | static const char *generic_cap_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 21 | "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce", |
| 22 | "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov", |
| 23 | "pat", "pse36", "psn", "clflsh", NULL, "ds", "acpi", "mmx", |
| 24 | "fxsr", "sse", "sse2", "ss", "ht", "tm", NULL, "pbe" |
| 25 | }; |
| 26 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 27 | /* CPUID 0x00000001 ECX flags */ |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 28 | static const char *intel_cap_generic_ecx_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 29 | "sse3", NULL, NULL, "monitor", "ds-cpl", "vmx", NULL, "est", |
| 30 | "tm2", "ssse3", "cntx-id", NULL, NULL, "cx16", "xTPR", NULL, |
| 31 | NULL, NULL, "dca", NULL, NULL, NULL, NULL, NULL, |
| 32 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
| 33 | }; |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 34 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 35 | /* CPUID 0x80000001 EDX flags */ |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 36 | static const char *intel_cap_extended_edx_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 37 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 38 | NULL, NULL, NULL, "SYSCALL", NULL, NULL, NULL, NULL, |
| 39 | NULL, NULL, NULL, NULL, "xd", NULL, NULL, NULL, |
| 40 | NULL, NULL, NULL, NULL, NULL, "em64t", NULL, NULL, |
| 41 | }; |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 42 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 43 | /* CPUID 0x80000001 ECX flags */ |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 44 | static const char *intel_cap_extended_ecx_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 45 | "lahf_lm", NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 46 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 47 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 48 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 49 | }; |
| 50 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 51 | static const char *amd_cap_generic_ecx_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 52 | "sse3", NULL, NULL, "mwait", NULL, NULL, NULL, NULL, |
| 53 | NULL, NULL, NULL, NULL, NULL, "cmpxchg16b", NULL, NULL, |
| 54 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, "popcnt", |
| 55 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
| 56 | }; |
| 57 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 58 | static const char *amd_cap_extended_edx_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 59 | "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce", |
| 60 | "cx8", "apic", NULL, "sep", "mtrr", "pge", "mca", "cmov", |
| 61 | "pat", "pse36", NULL, "mp", "nx", NULL, "mmxext", "mmx", |
| 62 | "fxsr", "ffxsr", "page1gb", "rdtscp", |
| 63 | NULL, "lm", "3dnowext", "3dnow" |
| 64 | }; /* "mp" defined for CPUs prior to AMD family 0xf */ |
| 65 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 66 | static const char *amd_cap_extended_ecx_flags[] = { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 67 | "lahf/sahf", "CmpLegacy", "svm", "ExtApicSpace", |
| 68 | "LockMovCr0", "abm", "sse4a", "misalignsse", |
| 69 | "3dnowPref", "osvw", "ibs", NULL, "skinit", "wdt", NULL, NULL, |
| 70 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 71 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
| 72 | }; |
| 73 | |
Jacob Garber | 37bec0b | 2019-06-28 10:10:37 -0600 | [diff] [blame] | 74 | static uint32_t vendor; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 75 | static unsigned int cpu_khz; |
| 76 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 77 | static void decode_flags(WINDOW *win, unsigned long reg, const char **flags, |
| 78 | int *row) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 79 | { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 80 | int i; |
| 81 | int lrow = *row; |
| 82 | |
| 83 | wmove(win, lrow, 2); |
| 84 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 85 | for (i = 0; i < 32; i++) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 86 | if (flags[i] == NULL) |
| 87 | continue; |
| 88 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 89 | if (reg & (1 << i)) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 90 | wprintw(win, "%s ", flags[i]); |
| 91 | |
| 92 | if (i && (i % 16) == 0) { |
| 93 | lrow++; |
| 94 | wmove(win, lrow, 2); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | *row = lrow; |
| 99 | } |
| 100 | |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 101 | static void get_features(WINDOW *win, int *row) |
| 102 | { |
Jacob Garber | 37bec0b | 2019-06-28 10:10:37 -0600 | [diff] [blame] | 103 | uint32_t eax, ebx, ecx, edx; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 104 | int lrow = *row; |
| 105 | |
| 106 | wmove(win, lrow++, 1); |
| 107 | wprintw(win, "Features: "); |
| 108 | |
| 109 | docpuid(0x00000001, &eax, &ebx, &ecx, &edx); |
| 110 | decode_flags(win, edx, generic_cap_flags, &lrow); |
| 111 | |
| 112 | lrow++; |
| 113 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 114 | switch (vendor) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 115 | case VENDOR_AMD: |
| 116 | wmove(win, lrow++, 1); |
| 117 | wprintw(win, "AMD Extended Flags: "); |
| 118 | decode_flags(win, ecx, amd_cap_generic_ecx_flags, &lrow); |
| 119 | docpuid(0x80000001, &eax, &ebx, &ecx, &edx); |
| 120 | decode_flags(win, edx, amd_cap_extended_edx_flags, &lrow); |
| 121 | decode_flags(win, ecx, amd_cap_extended_ecx_flags, &lrow); |
| 122 | break; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 123 | case VENDOR_INTEL: |
| 124 | wmove(win, lrow++, 1); |
| 125 | wprintw(win, "Intel Extended Flags: "); |
| 126 | decode_flags(win, ecx, intel_cap_generic_ecx_flags, &lrow); |
| 127 | docpuid(0x80000001, &eax, &ebx, &ecx, &edx); |
| 128 | decode_flags(win, edx, intel_cap_extended_edx_flags, &lrow); |
| 129 | decode_flags(win, ecx, intel_cap_extended_ecx_flags, &lrow); |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | *row = lrow; |
| 134 | } |
| 135 | |
| 136 | static void do_name(WINDOW *win, int row) |
| 137 | { |
Uwe Hermann | 35845a2 | 2008-03-20 20:05:22 +0000 | [diff] [blame] | 138 | char name[49], *p; |
Jacob Garber | 37bec0b | 2019-06-28 10:10:37 -0600 | [diff] [blame] | 139 | uint32_t eax, ebx, ecx, edx; |
| 140 | int t; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 141 | |
| 142 | p = name; |
| 143 | |
Jacob Garber | 37bec0b | 2019-06-28 10:10:37 -0600 | [diff] [blame] | 144 | for (uint32_t i = 0x80000002; i <= 0x80000004; i++) { |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 145 | docpuid(i, &eax, &ebx, &ecx, &edx); |
| 146 | |
| 147 | if (eax == 0) |
| 148 | break; |
| 149 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 150 | for (t = 0; t < 4; t++) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 151 | *p++ = eax >> (8 * t); |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 152 | for (t = 0; t < 4; t++) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 153 | *p++ = ebx >> (8 * t); |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 154 | for (t = 0; t < 4; t++) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 155 | *p++ = ecx >> (8 * t); |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 156 | for (t = 0; t < 4; t++) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 157 | *p++ = edx >> (8 * t); |
| 158 | } |
| 159 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 160 | mvwprintw(win, row, 1, "Processor: %s", name); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 163 | static int cpuinfo_module_redraw(WINDOW *win) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 164 | { |
Jacob Garber | 37bec0b | 2019-06-28 10:10:37 -0600 | [diff] [blame] | 165 | uint32_t eax, ebx, ecx, edx; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 166 | unsigned int brand; |
Uwe Hermann | 35845a2 | 2008-03-20 20:05:22 +0000 | [diff] [blame] | 167 | char *vstr; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 168 | int row = 2; |
| 169 | |
| 170 | print_module_title(win, "CPU Information"); |
| 171 | |
| 172 | docpuid(0, NULL, &vendor, NULL, NULL); |
| 173 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 174 | switch (vendor) { |
| 175 | case VENDOR_INTEL: |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 176 | vstr = "Intel"; |
| 177 | break; |
| 178 | case VENDOR_AMD: |
| 179 | vstr = "AMD"; |
| 180 | break; |
| 181 | case VENDOR_CYRIX: |
| 182 | vstr = "Cyrix"; |
| 183 | break; |
| 184 | case VENDOR_IDT: |
| 185 | vstr = "IDT"; |
| 186 | break; |
| 187 | case VENDOR_GEODE: |
| 188 | vstr = "NatSemi Geode"; |
| 189 | break; |
| 190 | case VENDOR_RISE: |
| 191 | case VENDOR_RISE2: |
| 192 | vstr = "RISE"; |
| 193 | break; |
| 194 | case VENDOR_SIS: |
| 195 | vstr = "SiS"; |
Uwe Hermann | 35845a2 | 2008-03-20 20:05:22 +0000 | [diff] [blame] | 196 | break; |
| 197 | default: |
| 198 | vstr = "Unknown"; |
| 199 | break; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | mvwprintw(win, row++, 1, "Vendor: %s", vstr); |
| 203 | |
| 204 | do_name(win, row++); |
| 205 | |
| 206 | docpuid(0x00000001, &eax, &ebx, &ecx, &edx); |
| 207 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 208 | mvwprintw(win, row++, 1, "Family: %X", (eax >> 8) & 0x0f); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 209 | mvwprintw(win, row++, 1, "Model: %X", |
| 210 | ((eax >> 4) & 0xf) | ((eax >> 16) & 0xf) << 4); |
| 211 | |
| 212 | mvwprintw(win, row++, 1, "Stepping: %X", eax & 0xf); |
| 213 | |
| 214 | if (vendor == VENDOR_AMD) { |
| 215 | docpuid(0x80000001, &eax, &ebx, &ecx, &edx); |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 216 | brand = ((ebx >> 9) & 0x1f); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 217 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 218 | mvwprintw(win, row++, 1, "Brand: %X", brand); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 221 | if (cpu_khz != 0) |
Elyes HAOUAS | 824b4b8 | 2020-02-15 09:27:11 +0100 | [diff] [blame] | 222 | mvwprintw(win, row++, 1, "CPU Speed: %d MHz", cpu_khz / 1000); |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 223 | else |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 224 | mvwprintw(win, row++, 1, "CPU Speed: Error"); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 225 | |
| 226 | row++; |
| 227 | get_features(win, &row); |
Uwe Hermann | 35845a2 | 2008-03-20 20:05:22 +0000 | [diff] [blame] | 228 | |
| 229 | return 0; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 232 | static unsigned int getticks(void) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 233 | { |
| 234 | unsigned long long start, end; |
| 235 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 236 | /* Read the number of ticks during the period. */ |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 237 | start = rdtsc(); |
| 238 | mdelay(100); |
| 239 | end = rdtsc(); |
| 240 | |
Uwe Hermann | 3a406fe | 2008-03-20 01:11:28 +0000 | [diff] [blame] | 241 | return (unsigned int)((end - start) / 100); |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Uwe Hermann | 0bfb5c4 | 2008-03-23 15:34:04 +0000 | [diff] [blame] | 244 | static int cpuinfo_module_init(void) |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 245 | { |
| 246 | cpu_khz = getticks(); |
Uwe Hermann | 35845a2 | 2008-03-20 20:05:22 +0000 | [diff] [blame] | 247 | return 0; |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | struct coreinfo_module cpuinfo_module = { |
| 251 | .name = "CPU Info", |
| 252 | .init = cpuinfo_module_init, |
| 253 | .redraw = cpuinfo_module_redraw, |
Jordan Crouse | 7249f79 | 2008-03-20 00:11:05 +0000 | [diff] [blame] | 254 | }; |
Uwe Hermann | ab5b3e0 | 2008-03-31 20:30:18 +0000 | [diff] [blame] | 255 | |
| 256 | #else |
| 257 | |
| 258 | struct coreinfo_module cpuinfo_module = { |
| 259 | }; |
| 260 | |
| 261 | #endif |