Angel Pons | b706ab3 | 2020-04-02 23:48:09 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Angel Pons | b706ab3 | 2020-04-02 23:48:09 +0200 | [diff] [blame] | 2 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 3 | /* |
Damien Roth | 4e7e987 | 2016-01-16 18:59:51 -0700 | [diff] [blame] | 4 | * blatantly copied from linux/kernel/printk.c |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 5 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 7 | #include <console/cbmem_console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 8 | #include <console/console.h> |
Kyösti Mälkki | 4076072 | 2014-02-27 19:30:18 +0200 | [diff] [blame] | 9 | #include <console/streams.h> |
Edward O'Callaghan | 0ddb826 | 2014-06-17 18:37:08 +1000 | [diff] [blame] | 10 | #include <console/vtxprintf.h> |
| 11 | #include <smp/spinlock.h> |
| 12 | #include <smp/node.h> |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 13 | #include <timer.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 14 | |
Myles Watson | 2e67273 | 2009-11-12 16:38:03 +0000 | [diff] [blame] | 15 | DECLARE_SPIN_LOCK(console_lock) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | |
Kyösti Mälkki | 05fe16c | 2019-12-01 08:38:11 +0200 | [diff] [blame] | 17 | #define TRACK_CONSOLE_TIME (!ENV_SMM && CONFIG(HAVE_MONOTONIC_TIMER)) |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 18 | |
| 19 | static struct mono_time mt_start, mt_stop; |
| 20 | static long console_usecs; |
| 21 | |
| 22 | static void console_time_run(void) |
| 23 | { |
Kyösti Mälkki | 05fe16c | 2019-12-01 08:38:11 +0200 | [diff] [blame] | 24 | if (TRACK_CONSOLE_TIME && boot_cpu()) |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 25 | timer_monotonic_get(&mt_start); |
| 26 | } |
| 27 | |
| 28 | static void console_time_stop(void) |
| 29 | { |
Kyösti Mälkki | 05fe16c | 2019-12-01 08:38:11 +0200 | [diff] [blame] | 30 | if (TRACK_CONSOLE_TIME && boot_cpu()) { |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 31 | timer_monotonic_get(&mt_stop); |
| 32 | console_usecs += mono_time_diff_microseconds(&mt_start, &mt_stop); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void console_time_report(void) |
| 37 | { |
| 38 | if (!TRACK_CONSOLE_TIME) |
| 39 | return; |
| 40 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 41 | printk(BIOS_DEBUG, "BS: " ENV_STRING " times (exec / console): total (unknown) / %ld ms\n", |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 42 | DIV_ROUND_CLOSEST(console_usecs, USECS_PER_MSEC)); |
| 43 | } |
| 44 | |
| 45 | long console_time_get_and_reset(void) |
| 46 | { |
| 47 | if (!TRACK_CONSOLE_TIME) |
| 48 | return 0; |
| 49 | |
| 50 | long elapsed = console_usecs; |
| 51 | console_usecs = 0; |
| 52 | return elapsed; |
| 53 | } |
| 54 | |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 55 | void do_putchar(unsigned char byte) |
| 56 | { |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 57 | console_time_run(); |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 58 | console_tx_byte(byte); |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 59 | console_time_stop(); |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Julius Werner | b6cf642 | 2022-02-02 17:21:02 -0800 | [diff] [blame] | 62 | union log_state { |
| 63 | void *as_ptr; |
| 64 | struct { |
| 65 | uint8_t level; |
| 66 | uint8_t speed; |
| 67 | }; |
| 68 | }; |
| 69 | |
Mario Scheithauer | f605611 | 2022-02-22 09:46:33 +0100 | [diff] [blame] | 70 | #define LOG_FAST(state) (HAS_ONLY_FAST_CONSOLES || ((state).speed == CONSOLE_LOG_FAST)) |
Julius Werner | 266041f | 2022-02-03 17:25:44 -0800 | [diff] [blame] | 71 | |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 72 | static void wrap_interactive_printf(const char *fmt, ...) |
| 73 | { |
| 74 | va_list args; |
| 75 | va_start(args, fmt); |
| 76 | vtxprintf(console_interactive_tx_byte, fmt, args, NULL); |
Julius Werner | ababf01 | 2022-02-08 16:37:41 -0800 | [diff] [blame] | 77 | va_end(args); |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void line_start(union log_state state) |
| 81 | { |
| 82 | if (state.level > BIOS_LOG_PREFIX_MAX_LEVEL) |
| 83 | return; |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 84 | |
| 85 | /* Stored consoles just get a single control char marker to save space. If we are in |
| 86 | LOG_FAST mode, just write the marker to CBMC and exit -- the rest of this function |
| 87 | implements the LOG_ALL case. */ |
| 88 | unsigned char marker = BIOS_LOG_LEVEL_TO_MARKER(state.level); |
Julius Werner | 266041f | 2022-02-03 17:25:44 -0800 | [diff] [blame] | 89 | if (LOG_FAST(state)) { |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 90 | __cbmemc_tx_byte(marker); |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 91 | return; |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 92 | } |
| 93 | console_stored_tx_byte(marker, NULL); |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 94 | |
Julius Werner | a120e0d | 2022-01-21 15:24:12 -0800 | [diff] [blame] | 95 | /* Interactive consoles get a `[DEBUG] ` style readable prefix, |
| 96 | and potentially an escape sequence for highlighting. */ |
| 97 | if (CONFIG(CONSOLE_USE_ANSI_ESCAPES)) |
| 98 | wrap_interactive_printf(BIOS_LOG_ESCAPE_PATTERN, bios_log_escape[state.level]); |
Igor Bagnucki | f0d6521 | 2022-03-03 11:41:57 +0100 | [diff] [blame] | 99 | if (CONFIG(CONSOLE_USE_LOGLEVEL_PREFIX)) |
| 100 | wrap_interactive_printf(BIOS_LOG_PREFIX_PATTERN, bios_log_prefix[state.level]); |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Julius Werner | a120e0d | 2022-01-21 15:24:12 -0800 | [diff] [blame] | 103 | static void line_end(union log_state state) |
| 104 | { |
Julius Werner | 266041f | 2022-02-03 17:25:44 -0800 | [diff] [blame] | 105 | if (CONFIG(CONSOLE_USE_ANSI_ESCAPES) && !LOG_FAST(state)) |
Julius Werner | a120e0d | 2022-01-21 15:24:12 -0800 | [diff] [blame] | 106 | wrap_interactive_printf(BIOS_LOG_ESCAPE_RESET); |
| 107 | } |
| 108 | |
Kyösti Mälkki | 77f43e9 | 2014-02-04 19:50:07 +0200 | [diff] [blame] | 109 | static void wrap_putchar(unsigned char byte, void *data) |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 110 | { |
Julius Werner | b6cf642 | 2022-02-02 17:21:02 -0800 | [diff] [blame] | 111 | union log_state state = { .as_ptr = data }; |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 112 | static bool line_started = false; |
| 113 | |
| 114 | if (byte == '\n') { |
Julius Werner | a120e0d | 2022-01-21 15:24:12 -0800 | [diff] [blame] | 115 | line_end(state); |
Julius Werner | 7cd8ba6 | 2022-01-21 15:15:29 -0800 | [diff] [blame] | 116 | line_started = false; |
| 117 | } else if (!line_started) { |
| 118 | line_start(state); |
| 119 | line_started = true; |
| 120 | } |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 121 | |
Julius Werner | 266041f | 2022-02-03 17:25:44 -0800 | [diff] [blame] | 122 | if (LOG_FAST(state)) |
Julius Werner | b6cf642 | 2022-02-02 17:21:02 -0800 | [diff] [blame] | 123 | __cbmemc_tx_byte(byte); |
| 124 | else |
| 125 | console_tx_byte(byte); |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Nico Huber | 7cc14ac | 2021-03-27 20:03:02 +0100 | [diff] [blame] | 128 | int vprintk(int msg_level, const char *fmt, va_list args) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 129 | { |
Julius Werner | b6cf642 | 2022-02-02 17:21:02 -0800 | [diff] [blame] | 130 | union log_state state = { .level = msg_level }; |
| 131 | int i; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 132 | |
Kyösti Mälkki | 21160a7 | 2019-08-17 17:29:36 +0300 | [diff] [blame] | 133 | if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu()) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 134 | return 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 135 | |
Julius Werner | b6cf642 | 2022-02-02 17:21:02 -0800 | [diff] [blame] | 136 | state.speed = console_log_level(msg_level); |
| 137 | if (state.speed < CONSOLE_LOG_FAST) |
Kyösti Mälkki | b2d2596 | 2014-01-27 15:09:13 +0200 | [diff] [blame] | 138 | return 0; |
Kyösti Mälkki | b2d2596 | 2014-01-27 15:09:13 +0200 | [diff] [blame] | 139 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 140 | spin_lock(&console_lock); |
| 141 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 142 | console_time_run(); |
| 143 | |
Julius Werner | b6cf642 | 2022-02-02 17:21:02 -0800 | [diff] [blame] | 144 | i = vtxprintf(wrap_putchar, fmt, args, state.as_ptr); |
Julius Werner | 266041f | 2022-02-03 17:25:44 -0800 | [diff] [blame] | 145 | if (LOG_FAST(state)) |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 146 | console_tx_flush(); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 147 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 148 | console_time_stop(); |
| 149 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 150 | spin_unlock(&console_lock); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 151 | |
| 152 | return i; |
| 153 | } |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 154 | |
Nico Huber | 7cc14ac | 2021-03-27 20:03:02 +0100 | [diff] [blame] | 155 | int printk(int msg_level, const char *fmt, ...) |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 156 | { |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 157 | va_list args; |
| 158 | int i; |
| 159 | |
| 160 | va_start(args, fmt); |
Nico Huber | 7cc14ac | 2021-03-27 20:03:02 +0100 | [diff] [blame] | 161 | i = vprintk(msg_level, fmt, args); |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 162 | va_end(args); |
| 163 | |
| 164 | return i; |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 165 | } |