1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 #ifndef TRACE_H
6 #define TRACE_H
7 
8 #include <compiler.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <trace_levels.h>
13 
14 #define MAX_PRINT_SIZE      256
15 #define MAX_FUNC_PRINT_SIZE 32
16 
17 #ifndef TRACE_LEVEL
18 #define TRACE_LEVEL TRACE_MAX
19 #endif
20 
21 /*
22  * Symbols provided by the entity that uses this API.
23  */
24 extern int trace_level;
25 extern const char trace_ext_prefix[];
26 void trace_ext_puts(const char *str);
27 int trace_ext_get_thread_id(void);
28 int trace_ext_get_core_id(void);
29 int trace_ext_get_guest_id(void);
30 void trace_set_level(int level);
31 int trace_get_level(void);
32 void plat_trace_ext_puts(const char *str);
33 
34 /* Internal functions used by the macros below */
35 void trace_vprintf(const char *func, int line, int level, bool level_ok,
36 		   const char *fmt, va_list args) __printf(5, 0);
37 void trace_printf(const char *func, int line, int level, bool level_ok,
38 		  const char *fmt, ...) __printf(5, 6);
39 
40 #define trace_printf_helper(level, level_ok, ...) \
41 	trace_printf(__func__, __LINE__, (level), (level_ok), \
42 		     __VA_ARGS__)
43 
44 /* Formatted trace tagged with level independent */
45 #if (TRACE_LEVEL <= 0)
46 #define MSG(...)   (void)0
47 #else
48 #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
49 #endif
50 
51 /* Formatted trace tagged with TRACE_ERROR level */
52 #if (TRACE_LEVEL < TRACE_ERROR)
53 #define EMSG(...)   (void)0
54 #else
55 #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
56 #endif
57 
58 /* Formatted trace tagged with TRACE_INFO level */
59 #if (TRACE_LEVEL < TRACE_INFO)
60 #define IMSG(...)   (void)0
61 #else
62 #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
63 #endif
64 
65 /* Formatted trace tagged with TRACE_DEBUG level */
66 #if (TRACE_LEVEL < TRACE_DEBUG)
67 #define DMSG(...)   (void)0
68 #else
69 #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
70 #endif
71 
72 /* Formatted trace tagged with TRACE_FLOW level */
73 #if (TRACE_LEVEL < TRACE_FLOW)
74 #define FMSG(...)   (void)0
75 #else
76 #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
77 #endif
78 
79 /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
80 #define INMSG(...)     FMSG("> " __VA_ARGS__)
81 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
82 #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
83 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
84  * an error message if r != 0 */
85 #define OUTRMSG(r)			\
86 	do {				\
87 		OUTMSG("r=[%x]", r);	\
88 		return r;		\
89 	} while (0)
90 
91 void dhex_dump(const char *function, int line, int level,
92 	       const void *buf, int len);
93 #if (TRACE_LEVEL < TRACE_DEBUG)
94 #define DHEXDUMP(buf, len) (void)0
95 #else
96 #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
97 				     buf, len)
98 #endif
99 
100 
101 /* Trace api without trace formatting */
102 
103 #define trace_printf_helper_raw(level, level_ok, ...) \
104 	trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__)
105 
106 /* No formatted trace tagged with level independent */
107 #if (TRACE_LEVEL <= 0)
108 #define MSG_RAW(...)   (void)0
109 #else
110 #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
111 #endif
112 
113 /* No formatted trace tagged with TRACE_ERROR level */
114 #if (TRACE_LEVEL < TRACE_ERROR)
115 #define EMSG_RAW(...)   (void)0
116 #else
117 #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
118 #endif
119 
120 /* No formatted trace tagged with TRACE_INFO level */
121 #if (TRACE_LEVEL < TRACE_INFO)
122 #define IMSG_RAW(...)   (void)0
123 #else
124 #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
125 #endif
126 
127 /* No formatted trace tagged with TRACE_DEBUG level */
128 #if (TRACE_LEVEL < TRACE_DEBUG)
129 #define DMSG_RAW(...)   (void)0
130 #else
131 #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
132 #endif
133 
134 /* No formatted trace tagged with TRACE_FLOW level */
135 #if (TRACE_LEVEL < TRACE_FLOW)
136 #define FMSG_RAW(...)   (void)0
137 #else
138 #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
139 #endif
140 
141 #if (TRACE_LEVEL <= 0)
142 #define SMSG(...)   (void)0
143 #else
144 /*
145  * Synchronised flushed trace, an Always message straight to HW trace IP.
146  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
147  * in another context.
148  */
149 #define SMSG(...)   \
150 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__)
151 
152 #endif /* TRACE_LEVEL */
153 #endif /* TRACE_H */
154