1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2020-2022 Martin Whitaker.
// Copyright (C) 2004-2025 Sam Demeulemeester.
#include <stdbool.h>
#include <stdint.h>
#include "cpuid.h"
#include "cpuinfo.h"
#include "hwctrl.h"
#include "i2c_x86.h"
#include "io.h"
#include "keyboard.h"
#include "memctrl.h"
#include "serial.h"
#include "pmem.h"
#include "smbios.h"
#include "spd.h"
#include "temperature.h"
#include "tsc.h"
#include "barrier.h"
#include "spinlock.h"
#include "config.h"
#include "error.h"
#include "build_version.h"
#include "tests.h"
#include "display.h"
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
#define POP_STAT_R 12
#define POP_STAT_C 18
#define POP_STAT_W 44
#define POP_STAT_H 11
#define POP_STAT_LAST_R (POP_STAT_R + POP_STAT_H - 1)
#define POP_STAT_LAST_C (POP_STAT_C + POP_STAT_W - 1)
#define POP_STATUS_REGION POP_STAT_R, POP_STAT_C, POP_STAT_LAST_R, POP_STAT_LAST_C
#define SPINNER_PERIOD 100 // milliseconds
#define NUM_SPIN_STATES 4
static const char spin_state[NUM_SPIN_STATES] = { '|', '/', '-', '\\' };
static const char cpu_mode_str[3][4] = { "PAR", "SEQ", "RR " };
//------------------------------------------------------------------------------
// Private Variables
//------------------------------------------------------------------------------
static bool scroll_lock = false;
static bool scroll_wait = false;
static int spin_idx = 0; // current spinner position
static int pass_ticks = 0; // current value (ticks_per_pass is final value)
static int test_ticks = 0; // current value (ticks_per_test is final value)
static int pass_bar_length = 0; // currently displayed length
static int test_bar_length = 0; // currently displayed length
static uint64_t run_start_time = 0; // TSC time stamp
static uint64_t next_spin_time = 0; // TSC time stamp
static int prev_sec = -1; // previous second
static bool timed_update_done = false; // update cycle status
bool big_status_displayed = false;
static uint16_t popup_status_save_buffer[POP_STAT_W * POP_STAT_H];
//------------------------------------------------------------------------------
// Variables
//------------------------------------------------------------------------------
int scroll_message_row;
int max_cpu_temp = 0;
display_mode_t display_mode = DISPLAY_MODE_NA;
screen_palette_t palette = {BLUE, WHITE, WHITE, BLACK, WHITE, BLUE, BLACK};
//------------------------------------------------------------------------------
// Private Functions
//------------------------------------------------------------------------------
static void set_screen_palette(screen_palette_t *mt_palette)
{
if (dark_mode) {
*mt_palette = (screen_palette_t){
.background = BLACK,
.foreground = WHITE,
.title_background = WHITE,
.title_foreground = BLACK,
.footer_background = WHITE,
.footer_foreground = BLACK,
.popup_background = WHITE
};
} else {
*mt_palette = (screen_palette_t){
.background = BLUE,
.foreground = WHITE,
.title_background = WHITE,
.title_foreground = BLACK,
.footer_background = WHITE,
.footer_foreground = BLUE,
.popup_background = BLACK
};
}
}
//------------------------------------------------------------------------------
// Public Functions
//------------------------------------------------------------------------------
void display_init(void)
{
cursor_off();
set_screen_palette(&palette);
set_background_colour(palette.background);
clear_screen();
/* The commented horizontal lines provide visual cue for where and how
* they will appear on the screen. They are drawn down below using
* Extended ASCII characters.
*/
set_foreground_colour(palette.title_foreground);
set_background_colour(palette.title_background);
clear_screen_region(0, 0, 0, 27);
prints(0, 0, " Memtest86+ v" MT_VERSION);
set_foreground_colour(RED);
printc(0, 15, '+');
set_foreground_colour(palette.foreground);
set_background_colour(palette.background);
prints(1, 0, "CLK/Temp: N/A | Pass %");
prints(2, 0, "L1 Cache: N/A | Test %");
prints(3, 0, "L2 Cache: N/A | Test #");
prints(4, 0, "L3 Cache: N/A | Testing:");
prints(5, 0, "Memory : N/A | Pattern:");
// prints(6, 0, "--------------------------------------------------------------------------------");
prints(7, 0, "CPU: SMP: N/A | Time: Status: Init.");
prints(8, 0, "Using: | Pass: Errors:");
// prints(9, 0, "--------------------------------------------------------------------------------");
if (ecc_status.ecc_enabled) {
prints(8, 57, "Err: ECC:");
}
for (int i = 0;i < 80; i++) {
print_char(6, i, 0xc4);
print_char(9, i, 0xc4);
}
for (int i = 0; i < 6; i++) {
print_char(i, 28, 0xb3);
}
for (int i = 7; i < 10; i++) {
print_char(i, 42, 0xb3);
}
print_char(6, 28, 0xc1);
print_char(6, 42, 0xc2);
print_char(9, 42, 0xc1);
set_foreground_colour(palette.footer_foreground);
set_background_colour(palette.footer_background);
clear_screen_region(ROW_FOOTER, 0, ROW_FOOTER, SCREEN_WIDTH - 1);
prints(ROW_FOOTER, 0, " <ESC> Exit <F1> Configuration <Space> Scroll Lock");
prints(ROW_FOOTER, 64, MT_VERSION "." GIT_HASH);
#if defined (__x86_64__)
prints(ROW_FOOTER, 74, ".x64");
#elif defined (__i386__)
prints(ROW_FOOTER, 74, ".x32");
#elif defined (__loongarch_lp64)
prints(ROW_FOOTER, 74, ".la64");
#endif
set_foreground_colour(palette.foreground);
set_background_colour(palette.background);
if (cpu_model) {
display_cpu_model(cpu_model);
}
if (clks_per_msec) {
display_cpu_clk((int)(clks_per_msec / 1000));
}
#if TESTWORD_WIDTH < 64
if (cpuid_info.flags.lm) {
display_cpu_addr_mode(" [LM]");
} else if (cpuid_info.flags.pae) {
display_cpu_addr_mode("[PAE]");
}
#endif
if (l1_cache) {
display_l1_cache_size(l1_cache);
}
if (l2_cache) {
display_l2_cache_size(l2_cache);
}
if (l3_cache) {
display_l3_cache_size(l3_cache);
}
if (l1_cache_speed) {
display_l1_cache_speed(l1_cache_speed);
}
if (l2_cache_speed) {
display_l2_cache_speed(l2_cache_speed);
}
if (l3_cache_speed) {
display_l3_cache_speed(l3_cache_speed);
}
if (ram_speed) {
display_ram_speed(ram_speed);
}
if (num_pm_pages) {
// Round to nearest MB.
display_memory_size(1024 * ((num_pm_pages + 128) / 256));
}
scroll_message_row = ROW_SCROLL_T;
}
void display_cpu_topology(void)
{
extern int num_enabled_cpus;
int num_cpu_sockets = 1;
// Display Thread Count and Thread Dispatch Mode
if (smp_enabled) {
if (cpuid_info.topology.is_hybrid && cpuid_info.topology.ecore_count > 0 && exclude_ecores) {
display_threading(num_enabled_cpus - cpuid_info.topology.ecore_count, cpu_mode_str[cpu_mode]);
} else {
display_threading(num_enabled_cpus, cpu_mode_str[cpu_mode]);
}
} else {
display_threading_disabled();
}
// If topology failed, assume topology according to APIC
if (cpuid_info.topology.core_count <= 0) {
cpuid_info.topology.core_count = num_enabled_cpus;
cpuid_info.topology.thread_count = num_enabled_cpus;
if(cpuid_info.flags.htt && num_enabled_cpus >= 2 && num_enabled_cpus % 2 == 0) {
cpuid_info.topology.core_count /= 2;
}
}
// Compute number of sockets according to individual CPU core count
if (num_enabled_cpus > cpuid_info.topology.thread_count &&
num_enabled_cpus % cpuid_info.topology.thread_count == 0) {
num_cpu_sockets = num_enabled_cpus / cpuid_info.topology.thread_count;
}
// Display P/E-Core count for Hybrid CPUs.
if (cpuid_info.topology.is_hybrid) {
if (cpuid_info.topology.pcore_count > 1) {
if (cpuid_info.flags.htt &&
(cpuid_info.topology.thread_count - cpuid_info.topology.ecore_count) == cpuid_info.topology.pcore_count) {
cpuid_info.topology.pcore_count /= 2;
}
display_cpu_topo_hybrid(cpuid_info.topology.pcore_count,
cpuid_info.topology.ecore_count,
cpuid_info.topology.thread_count);
} else {
display_cpu_topo_hybrid_short(cpuid_info.topology.thread_count);
}
return;
}
// Condensed display for multi-socket motherboard
if (num_cpu_sockets > 1) {
display_cpu_topo_multi_socket(num_cpu_sockets,
num_cpu_sockets * cpuid_info.topology.core_count,
num_cpu_sockets * cpuid_info.topology.thread_count);
return;
}
if (cpuid_info.topology.thread_count < 100) {
display_cpu_topo(cpuid_info.topology.core_count,
cpuid_info.topology.thread_count);
} else {
display_cpu_topo_short(cpuid_info.topology.core_count,
cpuid_info.topology.thread_count);
}
}
void post_display_init(void)
{
print_smbios_startup_info();
print_spd_startup_info();
if (imc.freq) {
// Try to get RAM information from IMC
display_spec_mode("IMC: ");
if (imc.type[3] == '5') {
display_spec_ddr5(imc.freq, imc.type, imc.tCL, imc.tCL_dec, imc.tRCD, imc.tRP, imc.tRAS);
} else {
display_spec_ddr(imc.freq, imc.type, imc.tCL, imc.tCL_dec, imc.tRCD, imc.tRP, imc.tRAS);
}
display_mode = DISPLAY_MODE_IMC;
} else if (ram.freq > 0 && ram.tCL > 0) {
// If not available, grab max memory specs from SPD
display_spec_mode("RAM: ");
if (ram.freq <= 166) {
display_spec_sdr(ram.freq, ram.type, ram.tCL, ram.tRCD, ram.tRP, ram.tRAS);
} else {
display_spec_ddr(ram.freq, ram.type, ram.tCL, ram.tCL_dec, ram.tRCD, ram.tRP, ram.tRAS);
}
display_mode = DISPLAY_MODE_SPD;
} else {
// If nothing available, fallback to "Using Core" Display
display_mode = DISPLAY_MODE_NA;
}
}
void display_start_run(void)
{
if (!enable_trace && !enable_sm) {
clear_message_area();
}
clear_screen_region(7, 49, 7, 57); // run time
if (ecc_status.ecc_enabled) {
clear_screen_region(8, 49, 8, 53); // pass number
clear_screen_region(8, 61, 8, 68); // error count
clear_screen_region(8, 74, 8, SCREEN_WIDTH - 1); // ecc error count
} else {
clear_screen_region(8, 49, 8, 59); // pass number
clear_screen_region(8, 68, 8, SCREEN_WIDTH - 1); // error count
}
display_pass_count(0);
error_count = 0;
display_error_count();
if (clks_per_msec > 0) {
// If we've measured the CPU speed, we know the TSC is available.
run_start_time = get_tsc();
next_spin_time = run_start_time + SPINNER_PERIOD * clks_per_msec;
}
display_spinner('-');
display_status("Testing");
if (enable_tty){
tty_full_redraw();
}
}
void display_start_pass(void)
{
clear_screen_region(1, 39, 1, SCREEN_WIDTH - 1); // progress bar
display_pass_percentage(0);
pass_bar_length = 0;
pass_ticks = 0;
}
void display_start_test(void)
{
clear_screen_region(2, 39, 3, SCREEN_WIDTH - 1); // progress bar, test details
clear_screen_region(4, 39, 4, SCREEN_WIDTH - 6); // Avoid erasing paging mode
clear_screen_region(5, 39, 5, SCREEN_WIDTH - 1);
clear_screen_region(3, 36, 3, 37); // test number
display_test_percentage(0);
display_test_number(test_num);
display_test_description(test_list[test_num].description);
test_bar_length = 0;
test_ticks = 0;
#if 0
uint64_t current_time = get_tsc();
int secs = (current_time - run_start_time) / (1000 * (uint64_t)clks_per_msec);
int mins = secs / 60; secs %= 60;
int hours = mins / 60; mins %= 60;
do_trace(0, "T %i: %i:%02i:%02i", test_num, hours, mins, secs);
#endif
}
void display_error_count(void)
{
if (ecc_status.ecc_enabled) {
display_err_count_with_ecc(error_count, error_count_cecc);
} else {
display_err_count_without_ecc(error_count);
}
}
void display_temperature(void)
{
if (enable_temp_cpu) {
// Display CPU Temperature
int actual_cpu_temp = get_cpu_temp();
if (actual_cpu_temp == 0) {
if (max_cpu_temp == 0) {
enable_temp_cpu = false;
}
return;
}
if (max_cpu_temp < actual_cpu_temp ) {
max_cpu_temp = actual_cpu_temp;
}
int offset = actual_cpu_temp / 100 + max_cpu_temp / 100;
display_cpu_temperature(actual_cpu_temp, max_cpu_temp, offset);
}
if (enable_temp_ram) {
// Display RAM Temperature (DDR5+ Only) - LA64 unsupported yet
if (dmi_memory_device->type == DMI_DDR5 && !strstr(cpuid_info.vendor_id.str, "Loongson")) {
for (int i = 0; i < MAX_SPD_SLOT; i++) {
if (!ram_slot_info[i].isPopulated || !ram_slot_info[i].hasTempSensor)
continue;
int ram_temp = get_ram_temp(i);
if (ram_temp > 0) {
display_ram_temperature(ram_temp, ram_slot_info[i].display_idx)
}
}
}
}
}
void display_big_status(bool pass)
{
if (!enable_big_status || big_status_displayed) {
return;
}
save_screen_region(POP_STATUS_REGION, popup_status_save_buffer);
set_background_colour(palette.popup_background);
set_foreground_colour(pass ? GREEN : RED);
clear_screen_region(POP_STATUS_REGION);
if (pass) {
prints(POP_STAT_R+1, POP_STAT_C+5, "###### ## ##### ##### ");
prints(POP_STAT_R+2, POP_STAT_C+5, "## ## #### ## ## ## ## ");
prints(POP_STAT_R+3, POP_STAT_C+5, "## ## ## ## ## ## ");
prints(POP_STAT_R+4, POP_STAT_C+5, "###### ## ## ##### ##### ");
prints(POP_STAT_R+5, POP_STAT_C+5, "## ######## ## ## ");
prints(POP_STAT_R+6, POP_STAT_C+5, "## ## ## ## ## ## ## ");
prints(POP_STAT_R+7, POP_STAT_C+5, "## ## ## ##### ##### ");
} else {
prints(POP_STAT_R+1, POP_STAT_C+5, "####### ## ###### ## ");
prints(POP_STAT_R+2, POP_STAT_C+5, "## #### ## ## ");
prints(POP_STAT_R+3, POP_STAT_C+5, "## ## ## ## ## ");
prints(POP_STAT_R+4, POP_STAT_C+5, "##### ## ## ## ## ");
prints(POP_STAT_R+5, POP_STAT_C+5, "## ######## ## ## ");
prints(POP_STAT_R+6, POP_STAT_C+5, "## ## ## ## ## ");
prints(POP_STAT_R+7, POP_STAT_C+5, "## ## ## ###### ###### ");
}
prints(POP_STAT_R+8, POP_STAT_C+5, " ");
prints(POP_STAT_R+9, POP_STAT_C+5, "Press any key to remove this banner ");
set_foreground_colour(palette.foreground);
set_background_colour(palette.background);
big_status_displayed = true;
}
void restore_big_status(void)
{
if (!big_status_displayed) {
return;
}
restore_screen_region(POP_STATUS_REGION, popup_status_save_buffer);
big_status_displayed = false;
}
void check_input(void)
{
char input_key = get_key();
if (input_key == '\0') {
return;
} else if (big_status_displayed) {
restore_big_status();
enable_big_status = false;
}
switch (input_key) {
case ESC:
clear_message_area();
display_notice("Rebooting...");
reboot();
break;
case '1':
config_menu(false);
break;
case ' ':
set_scroll_lock(!scroll_lock);
break;
case '\n':
scroll_wait = false;
break;
default:
break;
}
}
void set_scroll_lock(bool enabled)
{
scroll_lock = enabled;
set_foreground_colour(palette.footer_foreground);
prints(ROW_FOOTER, 48, scroll_lock ? "unlock" : "lock ");
set_foreground_colour(palette.foreground);
}
void toggle_scroll_lock(void)
{
set_scroll_lock(!scroll_lock);
}
void scroll(void)
{
if (scroll_message_row < ROW_SCROLL_B) {
scroll_message_row++;
} else {
if (scroll_lock) {
display_footer_message("<Enter> Single step ");
}
scroll_wait = true;
do {
check_input();
} while (scroll_wait && scroll_lock);
scroll_wait = false;
clear_footer_message();
scroll_screen_region(ROW_SCROLL_T, 0, ROW_SCROLL_B, SCREEN_WIDTH - 1);
}
}
void do_tick(int my_cpu)
{
int act_sec = 0;
bool use_spin_wait = (power_save < POWER_SAVE_HIGH);
if (use_spin_wait) {
barrier_spin_wait(run_barrier);
} else {
barrier_halt_wait(run_barrier);
}
if (master_cpu == my_cpu) {
check_input();
error_update();
}
if (use_spin_wait) {
barrier_spin_wait(run_barrier);
} else {
barrier_halt_wait(run_barrier);
}
// Only the master CPU does the update.
if (master_cpu != my_cpu) {
return;
}
test_ticks++;
pass_ticks++;
pass_type_t pass_type = (pass_num == 0) ? FAST_PASS : FULL_PASS;
int pct = 0;
if (ticks_per_test[pass_type][test_num] > 0) {
pct = 100 * test_ticks / ticks_per_test[pass_type][test_num];
if (pct > 100) {
pct = 100;
}
}
display_test_percentage(pct);
display_test_bar((BAR_LENGTH * pct) / 100);
pct = 0;
if (ticks_per_pass[pass_type] > 0) {
pct = 100 * pass_ticks / ticks_per_pass[pass_type];
if (pct > 100) {
pct = 100;
}
}
display_pass_percentage(pct);
display_pass_bar((BAR_LENGTH * pct) / 100);
bool update_spinner = true;
if (clks_per_msec > 0) {
uint64_t current_time = get_tsc();
int secs = (current_time - run_start_time) / (1000 * (uint64_t)clks_per_msec);
int mins = secs / 60; secs %= 60; act_sec = secs;
int hours = mins / 60; mins %= 60;
display_run_time(hours, mins, secs);
if (current_time >= next_spin_time) {
next_spin_time = current_time + SPINNER_PERIOD * clks_per_msec;
} else {
update_spinner = false;
}
}
/* ---------------
* Timed functions
* --------------- */
// update spinner every SPINNER_PERIOD ms
if (update_spinner) {
spin_idx = (spin_idx + 1) % NUM_SPIN_STATES;
display_spinner(spin_state[spin_idx]);
}
// This only tick one time per second
if (!timed_update_done) {
// Display FAIL banner if (new) errors detected
if (err_banner_redraw && !big_status_displayed && error_count > 1) {
display_big_status(false);
}
// Check ECC Errors
memctrl_poll_ecc();
// Update temperature
display_temperature();
// Update TTY one time every TTY_UPDATE_PERIOD second(s)
if (enable_tty) {
if (act_sec % tty_update_period == 0) {
tty_partial_redraw();
}
}
timed_update_done = true;
}
if (act_sec != prev_sec) {
prev_sec = act_sec;
timed_update_done = false;
}
}
void do_trace(int my_cpu, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
spin_lock(error_mutex);
scroll();
printi(scroll_message_row, 0, my_cpu, 2, false, false);
vprintf(scroll_message_row, 4, fmt, args);
spin_unlock(error_mutex);
va_end(args);
}
|