1 /*
2  * Copyright (c) 2019 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log_backend.h>
8 #include <zephyr/logging/log_core.h>
9 #include <zephyr/logging/log_output.h>
10 #include <zephyr/logging/log_backend_std.h>
11 
12 /*
13  * A lock is needed as log_process() and log_panic() have no internal locks
14  * to prevent concurrency. Meaning if log_process is called after
15  * log_panic was called previously, log_process may happen from another
16  * CPU and calling context than the log processing thread running in
17  * the background. On an SMP system this is a race.
18  *
19  * This caused a race on the output trace such that the logging output
20  * was garbled and useless.
21  */
22 static struct k_spinlock lock;
23 
24 static uint32_t log_format_current = CONFIG_LOG_BACKEND_ADSP_OUTPUT_DEFAULT;
25 void winstream_console_trace_out(int8_t *str, size_t len);
26 
char_out(uint8_t * data,size_t length,void * ctx)27 static int char_out(uint8_t *data, size_t length, void *ctx)
28 {
29 	winstream_console_trace_out(data, length);
30 
31 	return length;
32 }
33 
34 
35 /**
36  * 80 bytes seems to catch most sensibly sized log message lines
37  * in one go letting the trace out call output whole complete
38  * lines. This avoids the overhead of a spin lock in the trace_out
39  * more often as well as avoiding entwined characters from printk if
40  * LOG_PRINTK=n.
41  */
42 #define LOG_BUF_SIZE 80
43 static uint8_t log_buf[LOG_BUF_SIZE];
44 
45 LOG_OUTPUT_DEFINE(log_output_adsp, char_out, log_buf, sizeof(log_buf));
46 
format_flags(void)47 static uint32_t format_flags(void)
48 {
49 	uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
50 
51 	if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
52 		flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
53 	}
54 
55 	return flags;
56 }
57 
panic(struct log_backend const * const backend)58 static void panic(struct log_backend const *const backend)
59 {
60 	k_spinlock_key_t key = k_spin_lock(&lock);
61 
62 	log_backend_std_panic(&log_output_adsp);
63 
64 	k_spin_unlock(&lock, key);
65 }
66 
dropped(const struct log_backend * const backend,uint32_t cnt)67 static inline void dropped(const struct log_backend *const backend,
68 			   uint32_t cnt)
69 {
70 	log_output_dropped_process(&log_output_adsp, cnt);
71 }
72 
process(const struct log_backend * const backend,union log_msg_generic * msg)73 static void process(const struct log_backend *const backend,
74 		union log_msg_generic *msg)
75 {
76 	log_format_func_t log_output_func = log_format_func_t_get(log_format_current);
77 
78 	k_spinlock_key_t key = k_spin_lock(&lock);
79 
80 	log_output_func(&log_output_adsp, &msg->log, format_flags());
81 
82 	k_spin_unlock(&lock, key);
83 }
84 
format_set(const struct log_backend * const backend,uint32_t log_type)85 static int format_set(const struct log_backend *const backend, uint32_t log_type)
86 {
87 	log_format_current = log_type;
88 	return 0;
89 }
90 
91 const struct log_backend_api log_backend_adsp_api = {
92 	.process = process,
93 	.dropped = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ? NULL : dropped,
94 	.panic = panic,
95 	.format_set = format_set,
96 };
97 
98 LOG_BACKEND_DEFINE(log_backend_adsp, log_backend_adsp_api, true);
99