Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Damien Roth | 4e7e987 | 2016-01-16 18:59:51 -0700 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
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 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * blatantly copied from linux/kernel/printk.c |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 14 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 15 | |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 16 | #include <console/cbmem_console.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 17 | #include <console/console.h> |
Kyösti Mälkki | 4076072 | 2014-02-27 19:30:18 +0200 | [diff] [blame] | 18 | #include <console/streams.h> |
Edward O'Callaghan | 0ddb826 | 2014-06-17 18:37:08 +1000 | [diff] [blame] | 19 | #include <console/vtxprintf.h> |
| 20 | #include <smp/spinlock.h> |
| 21 | #include <smp/node.h> |
| 22 | #include <stddef.h> |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 23 | #include <trace.h> |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 24 | #include <timer.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | |
Myles Watson | 2e67273 | 2009-11-12 16:38:03 +0000 | [diff] [blame] | 26 | DECLARE_SPIN_LOCK(console_lock) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 27 | |
Kyösti Mälkki | 05fe16c | 2019-12-01 08:38:11 +0200 | [diff] [blame] | 28 | #define TRACK_CONSOLE_TIME (!ENV_SMM && CONFIG(HAVE_MONOTONIC_TIMER)) |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 29 | |
| 30 | static struct mono_time mt_start, mt_stop; |
| 31 | static long console_usecs; |
| 32 | |
| 33 | static void console_time_run(void) |
| 34 | { |
Kyösti Mälkki | 05fe16c | 2019-12-01 08:38:11 +0200 | [diff] [blame] | 35 | if (TRACK_CONSOLE_TIME && boot_cpu()) |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 36 | timer_monotonic_get(&mt_start); |
| 37 | } |
| 38 | |
| 39 | static void console_time_stop(void) |
| 40 | { |
Kyösti Mälkki | 05fe16c | 2019-12-01 08:38:11 +0200 | [diff] [blame] | 41 | if (TRACK_CONSOLE_TIME && boot_cpu()) { |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 42 | timer_monotonic_get(&mt_stop); |
| 43 | console_usecs += mono_time_diff_microseconds(&mt_start, &mt_stop); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void console_time_report(void) |
| 48 | { |
| 49 | if (!TRACK_CONSOLE_TIME) |
| 50 | return; |
| 51 | |
Kyösti Mälkki | 94694a8 | 2019-11-02 18:14:31 +0200 | [diff] [blame] | 52 | 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] | 53 | DIV_ROUND_CLOSEST(console_usecs, USECS_PER_MSEC)); |
| 54 | } |
| 55 | |
| 56 | long console_time_get_and_reset(void) |
| 57 | { |
| 58 | if (!TRACK_CONSOLE_TIME) |
| 59 | return 0; |
| 60 | |
| 61 | long elapsed = console_usecs; |
| 62 | console_usecs = 0; |
| 63 | return elapsed; |
| 64 | } |
| 65 | |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 66 | void do_putchar(unsigned char byte) |
| 67 | { |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 68 | console_time_run(); |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 69 | console_tx_byte(byte); |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 70 | console_time_stop(); |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Kyösti Mälkki | 77f43e9 | 2014-02-04 19:50:07 +0200 | [diff] [blame] | 73 | static void wrap_putchar(unsigned char byte, void *data) |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 74 | { |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 75 | console_tx_byte(byte); |
| 76 | } |
| 77 | |
| 78 | static void wrap_putchar_cbmemc(unsigned char byte, void *data) |
| 79 | { |
| 80 | __cbmemc_tx_byte(byte); |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Jacob Garber | deb99af | 2019-06-03 15:19:47 -0600 | [diff] [blame] | 83 | int do_vprintk(int msg_level, const char *fmt, va_list args) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 84 | { |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 85 | int i, log_this; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 86 | |
Kyösti Mälkki | 21160a7 | 2019-08-17 17:29:36 +0300 | [diff] [blame] | 87 | if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu()) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 88 | return 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 89 | |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 90 | log_this = console_log_level(msg_level); |
| 91 | if (log_this < CONSOLE_LOG_FAST) |
Kyösti Mälkki | b2d2596 | 2014-01-27 15:09:13 +0200 | [diff] [blame] | 92 | return 0; |
Kyösti Mälkki | b2d2596 | 2014-01-27 15:09:13 +0200 | [diff] [blame] | 93 | |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 94 | DISABLE_TRACE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 95 | spin_lock(&console_lock); |
| 96 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 97 | console_time_run(); |
| 98 | |
Kyösti Mälkki | e613d70 | 2019-02-12 14:16:21 +0200 | [diff] [blame] | 99 | if (log_this == CONSOLE_LOG_FAST) { |
| 100 | i = vtxprintf(wrap_putchar_cbmemc, fmt, args, NULL); |
| 101 | } else { |
| 102 | i = vtxprintf(wrap_putchar, fmt, args, NULL); |
| 103 | console_tx_flush(); |
| 104 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 105 | |
Kyösti Mälkki | 45ddb43 | 2019-11-02 14:12:18 +0200 | [diff] [blame] | 106 | console_time_stop(); |
| 107 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 108 | spin_unlock(&console_lock); |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 109 | ENABLE_TRACE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 110 | |
| 111 | return i; |
| 112 | } |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 113 | |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 114 | int do_printk(int msg_level, const char *fmt, ...) |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 115 | { |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 116 | va_list args; |
| 117 | int i; |
| 118 | |
| 119 | va_start(args, fmt); |
Jacob Garber | deb99af | 2019-06-03 15:19:47 -0600 | [diff] [blame] | 120 | i = do_vprintk(msg_level, fmt, args); |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 121 | va_end(args); |
| 122 | |
| 123 | return i; |
Kyösti Mälkki | b3356bb | 2014-03-06 16:55:05 +0200 | [diff] [blame] | 124 | } |