blob: 3ef28f32bedb10d6dcd48bfe033619e93cf8a126 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Damien Roth4e7e9872016-01-16 18:59:51 -07002 * This file is part of the coreboot project.
Eric Biederman8ca8d762003-04-22 19:02:15 +00003 *
Damien Roth4e7e9872016-01-16 18:59:51 -07004 * 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 Biederman8ca8d762003-04-22 19:02:15 +000014 */
Eric Biederman8ca8d762003-04-22 19:02:15 +000015
Kyösti Mälkkie613d702019-02-12 14:16:21 +020016#include <console/cbmem_console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000017#include <console/console.h>
Kyösti Mälkki40760722014-02-27 19:30:18 +020018#include <console/streams.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +100019#include <console/vtxprintf.h>
20#include <smp/spinlock.h>
21#include <smp/node.h>
22#include <stddef.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020023#include <trace.h>
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020024#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000025
Myles Watson2e672732009-11-12 16:38:03 +000026DECLARE_SPIN_LOCK(console_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000027
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020028#define TRACK_CONSOLE_TIME (!ENV_SMM && CONFIG(HAVE_MONOTONIC_TIMER))
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020029
30static struct mono_time mt_start, mt_stop;
31static long console_usecs;
32
33static void console_time_run(void)
34{
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020035 if (TRACK_CONSOLE_TIME && boot_cpu())
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020036 timer_monotonic_get(&mt_start);
37}
38
39static void console_time_stop(void)
40{
Kyösti Mälkki05fe16c2019-12-01 08:38:11 +020041 if (TRACK_CONSOLE_TIME && boot_cpu()) {
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020042 timer_monotonic_get(&mt_stop);
43 console_usecs += mono_time_diff_microseconds(&mt_start, &mt_stop);
44 }
45}
46
47void console_time_report(void)
48{
49 if (!TRACK_CONSOLE_TIME)
50 return;
51
Kyösti Mälkki94694a82019-11-02 18:14:31 +020052 printk(BIOS_DEBUG, "BS: " ENV_STRING " times (exec / console): total (unknown) / %ld ms\n",
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020053 DIV_ROUND_CLOSEST(console_usecs, USECS_PER_MSEC));
54}
55
56long 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älkki657e0be2014-02-04 19:03:57 +020066void do_putchar(unsigned char byte)
67{
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020068 console_time_run();
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020069 console_tx_byte(byte);
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020070 console_time_stop();
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020071}
72
Kyösti Mälkki77f43e92014-02-04 19:50:07 +020073static void wrap_putchar(unsigned char byte, void *data)
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020074{
Kyösti Mälkkie613d702019-02-12 14:16:21 +020075 console_tx_byte(byte);
76}
77
78static void wrap_putchar_cbmemc(unsigned char byte, void *data)
79{
80 __cbmemc_tx_byte(byte);
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020081}
82
Jacob Garberdeb99af2019-06-03 15:19:47 -060083int do_vprintk(int msg_level, const char *fmt, va_list args)
Eric Biederman8ca8d762003-04-22 19:02:15 +000084{
Kyösti Mälkkie613d702019-02-12 14:16:21 +020085 int i, log_this;
Eric Biederman8ca8d762003-04-22 19:02:15 +000086
Kyösti Mälkki21160a72019-08-17 17:29:36 +030087 if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu())
Eric Biederman8ca8d762003-04-22 19:02:15 +000088 return 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +000089
Kyösti Mälkkie613d702019-02-12 14:16:21 +020090 log_this = console_log_level(msg_level);
91 if (log_this < CONSOLE_LOG_FAST)
Kyösti Mälkkib2d25962014-01-27 15:09:13 +020092 return 0;
Kyösti Mälkkib2d25962014-01-27 15:09:13 +020093
Rudolf Marek7f0e9302011-09-02 23:23:41 +020094 DISABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000095 spin_lock(&console_lock);
96
Kyösti Mälkki45ddb432019-11-02 14:12:18 +020097 console_time_run();
98
Kyösti Mälkkie613d702019-02-12 14:16:21 +020099 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 Biederman8ca8d762003-04-22 19:02:15 +0000105
Kyösti Mälkki45ddb432019-11-02 14:12:18 +0200106 console_time_stop();
107
Eric Biederman8ca8d762003-04-22 19:02:15 +0000108 spin_unlock(&console_lock);
Rudolf Marek7f0e9302011-09-02 23:23:41 +0200109 ENABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000110
111 return i;
112}
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200113
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200114int do_printk(int msg_level, const char *fmt, ...)
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200115{
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200116 va_list args;
117 int i;
118
119 va_start(args, fmt);
Jacob Garberdeb99af2019-06-03 15:19:47 -0600120 i = do_vprintk(msg_level, fmt, args);
Kyösti Mälkki7132f252019-02-14 23:08:29 +0200121 va_end(args);
122
123 return i;
Kyösti Mälkkib3356bb2014-03-06 16:55:05 +0200124}