Documentation: Fix sphinx configuration
[coreboot.git] / src / console / printk.c
blobc5e5f97aab7be19e8ee905891ca52905fd921120
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * blatantly copied from linux/kernel/printk.c
5 */
7 #include <console/cbmem_console.h>
8 #include <console/console.h>
9 #include <console/streams.h>
10 #include <console/vtxprintf.h>
11 #include <smp/spinlock.h>
12 #include <smp/node.h>
13 #include <stddef.h>
14 #include <trace.h>
15 #include <timer.h>
17 DECLARE_SPIN_LOCK(console_lock)
19 #define TRACK_CONSOLE_TIME (!ENV_SMM && CONFIG(HAVE_MONOTONIC_TIMER))
21 static struct mono_time mt_start, mt_stop;
22 static long console_usecs;
24 static void console_time_run(void)
26 if (TRACK_CONSOLE_TIME && boot_cpu())
27 timer_monotonic_get(&mt_start);
30 static void console_time_stop(void)
32 if (TRACK_CONSOLE_TIME && boot_cpu()) {
33 timer_monotonic_get(&mt_stop);
34 console_usecs += mono_time_diff_microseconds(&mt_start, &mt_stop);
38 void console_time_report(void)
40 if (!TRACK_CONSOLE_TIME)
41 return;
43 printk(BIOS_DEBUG, "BS: " ENV_STRING " times (exec / console): total (unknown) / %ld ms\n",
44 DIV_ROUND_CLOSEST(console_usecs, USECS_PER_MSEC));
47 long console_time_get_and_reset(void)
49 if (!TRACK_CONSOLE_TIME)
50 return 0;
52 long elapsed = console_usecs;
53 console_usecs = 0;
54 return elapsed;
57 void do_putchar(unsigned char byte)
59 console_time_run();
60 console_tx_byte(byte);
61 console_time_stop();
64 static void wrap_putchar(unsigned char byte, void *data)
66 console_tx_byte(byte);
69 static void wrap_putchar_cbmemc(unsigned char byte, void *data)
71 __cbmemc_tx_byte(byte);
74 int do_vprintk(int msg_level, const char *fmt, va_list args)
76 int i, log_this;
78 if (CONFIG(SQUELCH_EARLY_SMP) && ENV_ROMSTAGE_OR_BEFORE && !boot_cpu())
79 return 0;
81 log_this = console_log_level(msg_level);
82 if (log_this < CONSOLE_LOG_FAST)
83 return 0;
85 DISABLE_TRACE;
86 spin_lock(&console_lock);
88 console_time_run();
90 if (log_this == CONSOLE_LOG_FAST) {
91 i = vtxprintf(wrap_putchar_cbmemc, fmt, args, NULL);
92 } else {
93 i = vtxprintf(wrap_putchar, fmt, args, NULL);
94 console_tx_flush();
97 console_time_stop();
99 spin_unlock(&console_lock);
100 ENABLE_TRACE;
102 return i;
105 int do_printk(int msg_level, const char *fmt, ...)
107 va_list args;
108 int i;
110 va_start(args, fmt);
111 i = do_vprintk(msg_level, fmt, args);
112 va_end(args);
114 return i;