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 <logging/log_backend.h>
12 #include <logging/log_core.h>
13 #include <logging/log_msg.h>
14 #include <logging/log_output.h>
15 #include <logging/log_backend_std.h>
16 #include <xtensa/simcall.h>
17 
18 #define CHAR_BUF_SIZE (IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? \
19 		1 : CONFIG_LOG_BACKEND_XTENSA_OUTPUT_BUFFER_SIZE)
20 
21 static uint8_t xtensa_log_buf[CHAR_BUF_SIZE];
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 
put(const struct log_backend * const backend,struct log_msg * msg)40 static void put(const struct log_backend *const backend,
41 		struct log_msg *msg)
42 {
43 	log_backend_std_put(&log_output_xsim, 0, msg);
44 
45 }
46 
process(const struct log_backend * const backend,union log_msg2_generic * msg)47 static void process(const struct log_backend *const backend,
48 		    union log_msg2_generic *msg)
49 {
50 	uint32_t flags = log_backend_std_get_flags();
51 
52 	log_output_msg2_process(&log_output_xsim, &msg->log, flags);
53 }
54 
panic(struct log_backend const * const backend)55 static void panic(struct log_backend const *const backend)
56 {
57 	log_backend_std_panic(&log_output_xsim);
58 }
59 
dropped(const struct log_backend * const backend,uint32_t cnt)60 static void dropped(const struct log_backend *const backend, uint32_t cnt)
61 {
62 	ARG_UNUSED(backend);
63 
64 	log_backend_std_dropped(&log_output_xsim, cnt);
65 }
66 
sync_string(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * fmt,va_list ap)67 static void sync_string(const struct log_backend *const backend,
68 		     struct log_msg_ids src_level, uint32_t timestamp,
69 		     const char *fmt, va_list ap)
70 {
71 	log_backend_std_sync_string(&log_output_xsim, 0, src_level,
72 				    timestamp, fmt, ap);
73 }
74 
sync_hexdump(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * metadata,const uint8_t * data,uint32_t length)75 static void sync_hexdump(const struct log_backend *const backend,
76 			 struct log_msg_ids src_level, uint32_t timestamp,
77 			 const char *metadata, const uint8_t *data, uint32_t length)
78 {
79 	log_backend_std_sync_hexdump(&log_output_xsim, 0, src_level,
80 				     timestamp, metadata, data, length);
81 }
82 
83 const struct log_backend_api log_backend_xtensa_sim_api = {
84 	.process = IS_ENABLED(CONFIG_LOG2) ? process : NULL,
85 	.put = IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? put : NULL,
86 	.put_sync_string = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
87 			sync_string : NULL,
88 	.put_sync_hexdump = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
89 			sync_hexdump : NULL,
90 	.panic = panic,
91 	.dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
92 };
93 
94 LOG_BACKEND_DEFINE(log_backend_xtensa_sim, log_backend_xtensa_sim_api, true);
95