1 /*
2  * Copyright (c) 2019 Intel Corporation Inc.
3  * Copyright (c) 2018 Nordic Semiconductor ASA
4  * Copyright (c) 2018 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <stdio.h>
10 #include <stddef.h>
11 #include <zephyr/logging/log_backend.h>
12 #include <zephyr/logging/log_core.h>
13 #include <zephyr/logging/log_output.h>
14 #include <zephyr/logging/log_backend_std.h>
15 #include <xtensa/simcall.h>
16 
17 #define CHAR_BUF_SIZE (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ? \
18 		1 : CONFIG_LOG_BACKEND_XTENSA_OUTPUT_BUFFER_SIZE)
19 
20 static uint8_t xtensa_log_buf[CHAR_BUF_SIZE];
21 static uint32_t log_format_current = CONFIG_LOG_BACKEND_XTENSA_SIM_OUTPUT_DEFAULT;
22 
char_out(uint8_t * data,size_t length,void * ctx)23 static int char_out(uint8_t *data, size_t length, void *ctx)
24 {
25 	register int a2 __asm__ ("a2") = SYS_write;
26 	register int a3 __asm__ ("a3") = 1;
27 	register int a4 __asm__ ("a4") = (int) data;
28 	register int a5 __asm__ ("a5") = length;
29 
30 	__asm__ volatile("simcall"
31 			 : "=a"(a2), "=a"(a3)
32 			 : "a"(a2), "a"(a3), "a"(a4), "a"(a5));
33 
34 	return length;
35 }
36 
37 LOG_OUTPUT_DEFINE(log_output_xsim, char_out,
38 		  xtensa_log_buf, sizeof(xtensa_log_buf));
39 
process(const struct log_backend * const backend,union log_msg_generic * msg)40 static void process(const struct log_backend *const backend,
41 		    union log_msg_generic *msg)
42 {
43 	uint32_t flags = log_backend_std_get_flags();
44 
45 	log_format_func_t log_output_func = log_format_func_t_get(log_format_current);
46 
47 	log_output_func(&log_output_xsim, &msg->log, flags);
48 }
49 
format_set(const struct log_backend * const backend,uint32_t log_type)50 static int format_set(const struct log_backend *const backend, uint32_t log_type)
51 {
52 	log_format_current = log_type;
53 	return 0;
54 }
55 
panic(struct log_backend const * const backend)56 static void panic(struct log_backend const *const backend)
57 {
58 	log_backend_std_panic(&log_output_xsim);
59 }
60 
dropped(const struct log_backend * const backend,uint32_t cnt)61 static void dropped(const struct log_backend *const backend, uint32_t cnt)
62 {
63 	ARG_UNUSED(backend);
64 
65 	log_backend_std_dropped(&log_output_xsim, cnt);
66 }
67 
68 const struct log_backend_api log_backend_xtensa_sim_api = {
69 	.process = process,
70 	.panic = panic,
71 	.dropped = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ? NULL : dropped,
72 	.format_set = format_set,
73 };
74 
75 LOG_BACKEND_DEFINE(log_backend_xtensa_sim, log_backend_xtensa_sim_api, true);
76