1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _RUN_INTEGRATION_PAL_LOG_H_
8 #define _RUN_INTEGRATION_PAL_LOG_H_
9 
10 #include <inttypes.h>
11 
12 #include "test_pal_thread.h"
13 #include "dx_reg_base_host.h"
14 #include "dx_env.h"
15 #include "test_proj.h"
16 
17 #define RUNIT_DEBUG_STACK 0
18 
19 #if defined(DX_PLAT_ZYNQ7000)
20 #define CLOCK_HZ 666000000
21 #define RUNIT_DEBUG_EXEC_TIME 0
22 #elif defined(DX_PLAT_MPS2_PLUS)
23 #define CLOCK_HZ 25000000
24 #define RUNIT_DEBUG_EXEC_TIME 1
25 #endif
26 
27 /************************************************************
28  *
29  * macros
30  *
31  ************************************************************/
32 extern uint8_t gRunItPrintEnable;
33 
34 /** print a line */
35 #define RUNIT_PRINT(format, ...)  \
36                 printf(format, ##__VA_ARGS__)
37 
38 #if RUNIT_DEBUG_EXEC_TIME
39 #define RUNIT_START_EXEC_TIME() \
40     do { \
41     } while(0)
42 #define RUNIT_PRINT_EXEC_TIME() \
43     do { \
44         printf(" %8"PRIu32"ms", runIt_getMicroSecPublic(runIt_getCyclesPublic() - startTime) / 1000) ;\
45     } while(0)
46 #else
47 #define RUNIT_START_EXEC_TIME() do {} while(0)
48 #define RUNIT_PRINT_EXEC_TIME() do {} while(0)
49 #endif /* RUNIT_DEBUG_STACK */
50 
51 #if RUNIT_DEBUG_STACK
52 #define RUNIT_PRINT_STACK()  \
53                 if (gRunItPrintEnable == 1) {\
54                     char stack[10]; \
55                     snprintf(stack, 9, "%d", gRunItStackSize - Test_PalThreadStackHighestWatermark(NULL)); \
56                     RUNIT_PRINT(" %5.5s", stack); \
57                 } while(0)
58 #else
59 #define RUNIT_PRINT_STACK() do {} while(0)
60 #endif /* RUNIT_DEBUG_STACK */
61 
62 /** Print the function name, line and debug level */
63 #define RUNIT_PRINT_HEADER() \
64                 if (gRunItPrintEnable == 1) { \
65                     char dash[] = "------------------------------------------------------------------------------------------------------------------------------------"; \
66                     RUNIT_PRINT("       %-44.44s %-75.75s %-10.10s", "test_name", "description", "status"); \
67                     if (RUNIT_DEBUG_STACK) printf(" %-5.5s", "stack"); \
68                     if (RUNIT_DEBUG_EXEC_TIME) printf(" %-10.10s", "exec time"); \
69                     RUNIT_PRINT("\n      %-44.44s %-75.75s %-10.10s", dash, dash, dash); \
70                     if (RUNIT_DEBUG_STACK) printf(" %-5.5s", dash); \
71                     if (RUNIT_DEBUG_EXEC_TIME) printf(" %-10.10s", dash); \
72                     RUNIT_PRINT("\n"); \
73                 }
74 
75 #define RUNIT_PRINT_FUNC_AND_LEVEL(_lvl) \
76                 if (gRunItPrintEnable == 1) { \
77                     char _buff[50]; \
78                     snprintf(_buff, 49, "%-.25s:%d", __func__, __LINE__); \
79                     RUNIT_PRINT("%-35.35s: %-5.5s: ", _buff, _lvl); \
80                 }
81 
82 /** print the test result */
83 #define RUNIT_TEST_RESULT(_testName) \
84                 if (gRunItPrintEnable == 1) {\
85                     char label[121] = {0}; \
86                     snprintf(label, 120, "%s.........................................................................................................", _testName); \
87                     RUNIT_PRINT("Test: %-120.120s %-10.10s", label, rc == RUNIT_ERROR__OK ? "Passed" : "Failed"); \
88                     RUNIT_PRINT_STACK(); \
89                     RUNIT_PRINT_EXEC_TIME(); \
90                     RUNIT_PRINT("\n"); \
91                 }
92 
93 
94 #define RUNIT_SUB_TEST_RESULT_W_PARAMS(_testName, _format, ...) \
95                 if (gRunItPrintEnable == 1) {\
96                     char buff[100]; \
97                     snprintf(buff, 99, _format, ##__VA_ARGS__); \
98                     RUNIT_PRINT("Test:     %-40.40s %-75.75s %-10.10s", _testName, buff, rc == RUNIT_ERROR__OK ? "Passed" : "Failed"); \
99                     RUNIT_PRINT_STACK(); \
100                     RUNIT_PRINT_EXEC_TIME(); \
101                     RUNIT_PRINT("\n"); \
102                 }
103 
104 #define RUNIT_SUB_TEST_RESULT(_testName) \
105     RUNIT_SUB_TEST_RESULT_W_PARAMS(_testName, " ")
106 
107 /** print the test start line */
108 #define RUNIT_TEST_START(_testName) \
109                 uint32_t startTime = runIt_getCyclesPublic(); \
110                 (void)startTime; \
111                 if (gRunItPrintEnable == 1) {\
112                     RUNIT_PRINT("Test: %-120.120s %-10.10s", _testName, "                  "); \
113                     RUNIT_PRINT_STACK(); \
114                     RUNIT_START_EXEC_TIME(); \
115                     RUNIT_PRINT("\n"); \
116                 }
117 
118 /** print the sub test start line */
119 #define RUNIT_SUB_TEST_START(_testName) \
120                 uint32_t startTime = runIt_getCyclesPublic(); \
121                 (void)startTime; \
122                 do { \
123                     RUNIT_START_EXEC_TIME(); \
124                 } while(0)
125 
126 
127 /** print an error message */
128 #define RUNIT_PRINT_ERROR(format, ...)  \
129                 do {\
130                     RUNIT_PRINT_FUNC_AND_LEVEL("error"); \
131                     RUNIT_PRINT(format, ##__VA_ARGS__); \
132                 } while(0)
133 
134 
135 
136 /** print an debug message */
137 #if defined(TEST_DEBUG)
138 #define RUNIT_PRINT_DBG(format, ...)    \
139                 do {\
140                     RUNIT_PRINT_FUNC_AND_LEVEL("debug"); \
141                     RUNIT_PRINT(format, ##__VA_ARGS__); \
142                 } while(0)
143 
144 /** print an error message */
145 #define RUNIT_PRINT_BUF(_buff, _size, _label)  \
146                 do {\
147                     uint32_t i = 0, j = 0;\
148                     for (i = 0; i * 16 + j < _size; i++, j = 0)\
149                     { \
150                         char tmpBuff[256] = {0}; \
151                         for (j = 0; i * 16 + j < _size && j < 16; j++) \
152                         { \
153                             sprintf(tmpBuff + strlen(tmpBuff), "%02x", ((uint8_t*)_buff)[i * 16 + j]); \
154                         } \
155                         RUNIT_PRINT_FUNC_AND_LEVEL("debug"); \
156                         RUNIT_PRINT("%-10.10s %04"PRIx32": %s\n", _label, i * 16, tmpBuff); \
157                     } \
158                 } while(0)
159 #else
160 #define RUNIT_PRINT_DBG(format, ...) do {} while(0)
161 #define RUNIT_PRINT_BUF(_buff, _size, _label) do {} while(0)
162 #endif
163 
164 static __inline uint32_t runIt_getCyclesPublic(void);
runIt_getCyclesPublic(void)165 static __inline uint32_t runIt_getCyclesPublic(void)
166 {
167     return *(volatile uint32_t *)(processMap.processTeeHwEnvBaseAddr + DX_ENV_COUNTER_RD_REG_OFFSET);
168 }
169 
170 
171 /**
172 *
173 * @param cycles
174 * @return          microSeconds
175 */
176 static __inline uint32_t runIt_getMicroSecPublic(uint32_t cycles);
runIt_getMicroSecPublic(uint32_t cycles)177 static __inline uint32_t runIt_getMicroSecPublic(uint32_t cycles)
178 {
179     static const uint32_t MICROSECONDS = 1000000;
180 
181     return (uint32_t)((uint64_t)cycles * (uint64_t)MICROSECONDS / CLOCK_HZ);
182 }
183 
184 
185 #endif //_RUN_INTEGRATION_PAL_LOG_H_
186