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 | |
Kyösti Mälkki | 77f43e9 | 2014-02-04 19:50:07 +0200 | [diff] [blame] | 62 | static void wrap_putchar(unsigned char byte, void *data) |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 63 | { |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 64 | console_tx_byte(byte); |
| 65 | } |
| 66 | |
| 67 | static void wrap_putchar_cbmemc(unsigned char byte, void *data) |
| 68 | { |
| 69 | __cbmemc_tx_byte(byte); |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Jacob Garber | deb99af | 2019-06-03 15:19:47 -0600 | [diff] [blame] | 72 | int do_vprintk(int msg_level, const char *fmt, va_list args) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 73 | { |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 74 | int i, log_this; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 75 | |
Kyösti Mälkki | 21160a7 | 2019-08-17 17:29:36 +0300 | [diff] [blame] | 76 | if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu()) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 77 | return 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 78 | |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 79 | log_this = console_log_level(msg_level); |
| 80 | if (log_this < CONSOLE_LOG_FAST) |
Kyösti Mälkki | b2d2596 | 2014-01-27 15:09:13 +0200 | [diff] [blame] | 81 | return 0; |
Kyösti Mälkki | b2d2596 | 2014-01-27 15:09:13 +0200 | [diff] [blame] | 82 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 83 | spin_lock(&console_lock); |
| 84 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 85 | console_time_run(); |
| 86 | |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 87 | if (log_this == CONSOLE_LOG_FAST) { |
| 88 | i = vtxprintf(wrap_putchar_cbmemc, fmt, args, NULL); |
| 89 | } else { |
| 90 | i = vtxprintf(wrap_putchar, fmt, args, NULL); |
| 91 | console_tx_flush(); |
| 92 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 93 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 94 | console_time_stop(); |
| 95 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 96 | spin_unlock(&console_lock); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 97 | |
| 98 | return i; |
| 99 | } |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 100 | |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 101 | int do_printk(int msg_level, const char *fmt, ...) |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 102 | { |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 103 | va_list args; |
| 104 | int i; |
| 105 | |
| 106 | va_start(args, fmt); |
Jacob Garber | deb99af | 2019-06-03 15:19:47 -0600 | [diff] [blame] | 107 | i = do_vprintk(msg_level, fmt, args); |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 108 | va_end(args); |
| 109 | |
| 110 | return i; |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 111 | } |