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 void trace_set_level(int level);
30 int trace_get_level(void);
31 void plat_trace_ext_puts(const char *str);
32 
33 /* Internal functions used by the macros below */
34 void trace_vprintf(const char *func, int line, int level, bool level_ok,
35 		   const char *fmt, va_list args) __printf(5, 0);
36 void trace_printf(const char *func, int line, int level, bool level_ok,
37 		  const char *fmt, ...) __printf(5, 6);
38 
39 #define trace_printf_helper(level, level_ok, ...) \
40 	trace_printf(__func__, __LINE__, (level), (level_ok), \
41 		     __VA_ARGS__)
42 
43 /* Formatted trace tagged with level independent */
44 #if (TRACE_LEVEL <= 0)
45 #define MSG(...)   (void)0
46 #else
47 #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
48 #endif
49 
50 /* Formatted trace tagged with TRACE_ERROR level */
51 #if (TRACE_LEVEL < TRACE_ERROR)
52 #define EMSG(...)   (void)0
53 #else
54 #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
55 #endif
56 
57 /* Formatted trace tagged with TRACE_INFO level */
58 #if (TRACE_LEVEL < TRACE_INFO)
59 #define IMSG(...)   (void)0
60 #else
61 #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
62 #endif
63 
64 /* Formatted trace tagged with TRACE_DEBUG level */
65 #if (TRACE_LEVEL < TRACE_DEBUG)
66 #define DMSG(...)   (void)0
67 #else
68 #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
69 #endif
70 
71 /* Formatted trace tagged with TRACE_FLOW level */
72 #if (TRACE_LEVEL < TRACE_FLOW)
73 #define FMSG(...)   (void)0
74 #else
75 #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
76 #endif
77 
78 /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
79 #define INMSG(...)     FMSG("> " __VA_ARGS__)
80 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
81 #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
82 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
83  * an error message if r != 0 */
84 #define OUTRMSG(r)			\
85 	do {				\
86 		OUTMSG("r=[%x]", r);	\
87 		return r;		\
88 	} while (0)
89 
90 void dhex_dump(const char *function, int line, int level,
91 	       const void *buf, int len);
92 #if (TRACE_LEVEL < TRACE_DEBUG)
93 #define DHEXDUMP(buf, len) (void)0
94 #else
95 #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
96 				     buf, len)
97 #endif
98 
99 
100 /* Trace api without trace formatting */
101 
102 #define trace_printf_helper_raw(level, level_ok, ...) \
103 	trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__)
104 
105 /* No formatted trace tagged with level independent */
106 #if (TRACE_LEVEL <= 0)
107 #define MSG_RAW(...)   (void)0
108 #else
109 #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
110 #endif
111 
112 /* No formatted trace tagged with TRACE_ERROR level */
113 #if (TRACE_LEVEL < TRACE_ERROR)
114 #define EMSG_RAW(...)   (void)0
115 #else
116 #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
117 #endif
118 
119 /* No formatted trace tagged with TRACE_INFO level */
120 #if (TRACE_LEVEL < TRACE_INFO)
121 #define IMSG_RAW(...)   (void)0
122 #else
123 #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
124 #endif
125 
126 /* No formatted trace tagged with TRACE_DEBUG level */
127 #if (TRACE_LEVEL < TRACE_DEBUG)
128 #define DMSG_RAW(...)   (void)0
129 #else
130 #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
131 #endif
132 
133 /* No formatted trace tagged with TRACE_FLOW level */
134 #if (TRACE_LEVEL < TRACE_FLOW)
135 #define FMSG_RAW(...)   (void)0
136 #else
137 #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
138 #endif
139 
140 #if (TRACE_LEVEL <= 0)
141 #define SMSG(...)   (void)0
142 #else
143 /*
144  * Synchronised flushed trace, an Always message straight to HW trace IP.
145  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
146  * in another context.
147  */
148 #define SMSG(...)   \
149 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__)
150 
151 #endif /* TRACE_LEVEL */
152 #endif /* TRACE_H */
153