blob: 4a3de4783231bae44690d5d3d43d2e64464f24dc [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>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020013#include <trace.h>
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020014#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000015
Myles Watson2e672732009-11-12 16:38:03 +000016DECLARE_SPIN_LOCK(console_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000017
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020018#define TRACK_CONSOLE_TIME (!ENV_SMM && CONFIG(HAVE_MONOTONIC_TIMER))
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020019
20static struct mono_time mt_start, mt_stop;
21static long console_usecs;
22
23static void console_time_run(void)
24{
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020025 if (TRACK_CONSOLE_TIME && boot_cpu())
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020026 timer_monotonic_get(&mt_start);
27}
28
29static void console_time_stop(void)
30{
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020031 if (TRACK_CONSOLE_TIME && boot_cpu()) {
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020032 timer_monotonic_get(&mt_stop);
33 console_usecs += mono_time_diff_microseconds(&mt_start, &mt_stop);
34 }
35}
36
37void console_time_report(void)
38{
39 if (!TRACK_CONSOLE_TIME)
40 return;
41
Kyösti Mälkki94694a82019-11-02 18:14:31 +020042 printk(BIOS_DEBUG, "BS: " ENV_STRING " times (exec / console): total (unknown) / %ld ms\n",
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020043 DIV_ROUND_CLOSEST(console_usecs, USECS_PER_MSEC));
44}
45
46long console_time_get_and_reset(void)
47{
48 if (!TRACK_CONSOLE_TIME)
49 return 0;
50
51 long elapsed = console_usecs;
52 console_usecs = 0;
53 return elapsed;
54}
55
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020056void do_putchar(unsigned char byte)
57{
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020058 console_time_run();
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020059 console_tx_byte(byte);
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020060 console_time_stop();
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020061}
62
Kyösti Mälkki77f43e92014-02-04 19:50:07 +020063static void wrap_putchar(unsigned char byte, void *data)
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020064{
Kyösti Mälkkie613d702019-02-12 14:16:21 +020065 console_tx_byte(byte);
66}
67
68static void wrap_putchar_cbmemc(unsigned char byte, void *data)
69{
70 __cbmemc_tx_byte(byte);
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020071}
72
Jacob Garberdeb99af2019-06-03 15:19:47 -060073int do_vprintk(int msg_level, const char *fmt, va_list args)
Eric Biederman8ca8d762003-04-22 19:02:15 +000074{
Kyösti Mälkkie613d702019-02-12 14:16:21 +020075 int i, log_this;
Eric Biederman8ca8d762003-04-22 19:02:15 +000076
Kyösti Mälkki21160a72019-08-17 17:29:36 +030077 if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu())
Eric Biederman8ca8d762003-04-22 19:02:15 +000078 return 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000079
Kyösti Mälkkie613d702019-02-12 14:16:21 +020080 log_this = console_log_level(msg_level);
81 if (log_this < CONSOLE_LOG_FAST)
Kyösti Mälkkib2d25962014-01-27 15:09:13 +020082 return 0;
Kyösti Mälkkib2d25962014-01-27 15:09:13 +020083
Rudolf Marek7f0e9302011-09-02 23:23:41 +020084 DISABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000085 spin_lock(&console_lock);
86
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020087 console_time_run();
88
Kyösti Mälkkie613d702019-02-12 14:16:21 +020089 if (log_this == CONSOLE_LOG_FAST) {
90 i = vtxprintf(wrap_putchar_cbmemc, fmt, args, NULL);
91 } else {
92 i = vtxprintf(wrap_putchar, fmt, args, NULL);
93 console_tx_flush();
94 }
Eric Biederman8ca8d762003-04-22 19:02:15 +000095
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020096 console_time_stop();
97
Eric Biederman8ca8d762003-04-22 19:02:15 +000098 spin_unlock(&console_lock);
Rudolf Marek7f0e9302011-09-02 23:23:41 +020099 ENABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000100
101 return i;
102}
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200103
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200104int do_printk(int msg_level, const char *fmt, ...)
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200105{
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200106 va_list args;
107 int i;
108
109 va_start(args, fmt);
Jacob Garberdeb99af2019-06-03 15:19:47 -0600110 i = do_vprintk(msg_level, fmt, args);
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200111 va_end(args);
112
113 return i;
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200114}