blob: 85d9bfb1e6c8bbf0212d5b60e53f6b8b25e614ef [file] [log] [blame]
Angel Ponsb706ab32020-04-02 23:48:09 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Angel Ponsb706ab32020-04-02 23:48:09 +02002
Eric Biederman8ca8d762003-04-22 19:02:15 +00003/*
Damien Roth4e7e9872016-01-16 18:59:51 -07004 * blatantly copied from linux/kernel/printk.c
Eric Biederman8ca8d762003-04-22 19:02:15 +00005 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00006
Kyösti Mälkkie613d702019-02-12 14:16:21 +02007#include <console/cbmem_console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00008#include <console/console.h>
Kyösti Mälkki40760722014-02-27 19:30:18 +02009#include <console/streams.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +100010#include <console/vtxprintf.h>
11#include <smp/spinlock.h>
12#include <smp/node.h>
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020013#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000014
Myles Watson2e672732009-11-12 16:38:03 +000015DECLARE_SPIN_LOCK(console_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000016
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020017#define TRACK_CONSOLE_TIME (!ENV_SMM && CONFIG(HAVE_MONOTONIC_TIMER))
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020018
19static struct mono_time mt_start, mt_stop;
20static long console_usecs;
21
22static void console_time_run(void)
23{
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020024 if (TRACK_CONSOLE_TIME && boot_cpu())
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020025 timer_monotonic_get(&mt_start);
26}
27
28static void console_time_stop(void)
29{
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020030 if (TRACK_CONSOLE_TIME && boot_cpu()) {
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020031 timer_monotonic_get(&mt_stop);
32 console_usecs += mono_time_diff_microseconds(&mt_start, &mt_stop);
33 }
34}
35
36void console_time_report(void)
37{
38 if (!TRACK_CONSOLE_TIME)
39 return;
40
Kyösti Mälkki94694a82019-11-02 18:14:31 +020041 printk(BIOS_DEBUG, "BS: " ENV_STRING " times (exec / console): total (unknown) / %ld ms\n",
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020042 DIV_ROUND_CLOSEST(console_usecs, USECS_PER_MSEC));
43}
44
45long 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älkki657e0be2014-02-04 19:03:57 +020055void do_putchar(unsigned char byte)
56{
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020057 console_time_run();
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020058 console_tx_byte(byte);
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020059 console_time_stop();
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020060}
61
Kyösti Mälkki77f43e92014-02-04 19:50:07 +020062static void wrap_putchar(unsigned char byte, void *data)
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020063{
Kyösti Mälkkie613d702019-02-12 14:16:21 +020064 console_tx_byte(byte);
65}
66
67static void wrap_putchar_cbmemc(unsigned char byte, void *data)
68{
69 __cbmemc_tx_byte(byte);
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020070}
71
Jacob Garberdeb99af2019-06-03 15:19:47 -060072int do_vprintk(int msg_level, const char *fmt, va_list args)
Eric Biederman8ca8d762003-04-22 19:02:15 +000073{
Kyösti Mälkkie613d702019-02-12 14:16:21 +020074 int i, log_this;
Eric Biederman8ca8d762003-04-22 19:02:15 +000075
Kyösti Mälkki21160a72019-08-17 17:29:36 +030076 if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu())
Eric Biederman8ca8d762003-04-22 19:02:15 +000077 return 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000078
Kyösti Mälkkie613d702019-02-12 14:16:21 +020079 log_this = console_log_level(msg_level);
80 if (log_this < CONSOLE_LOG_FAST)
Kyösti Mälkkib2d25962014-01-27 15:09:13 +020081 return 0;
Kyösti Mälkkib2d25962014-01-27 15:09:13 +020082
Eric Biederman8ca8d762003-04-22 19:02:15 +000083 spin_lock(&console_lock);
84
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020085 console_time_run();
86
Kyösti Mälkkie613d702019-02-12 14:16:21 +020087 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 Biederman8ca8d762003-04-22 19:02:15 +000093
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020094 console_time_stop();
95
Eric Biederman8ca8d762003-04-22 19:02:15 +000096 spin_unlock(&console_lock);
Eric Biederman8ca8d762003-04-22 19:02:15 +000097
98 return i;
99}
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200100
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200101int do_printk(int msg_level, const char *fmt, ...)
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200102{
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200103 va_list args;
104 int i;
105
106 va_start(args, fmt);
Jacob Garberdeb99af2019-06-03 15:19:47 -0600107 i = do_vprintk(msg_level, fmt, args);
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200108 va_end(args);
109
110 return i;
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200111}