Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 7 | #include <mapmem.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 8 | #include <time.h> |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 9 | #include <trace.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/sections.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Marek BehĂșn | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 16 | static char trace_enabled __section(".data"); |
| 17 | static char trace_inited __section(".data"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 18 | |
| 19 | /* The header block at the start of the trace memory area */ |
| 20 | struct trace_hdr { |
| 21 | int func_count; /* Total number of function call sites */ |
| 22 | u64 call_count; /* Total number of tracked function calls */ |
| 23 | u64 untracked_count; /* Total number of untracked function calls */ |
| 24 | int funcs_used; /* Total number of functions used */ |
| 25 | |
| 26 | /* |
| 27 | * Call count for each function. This is indexed by the word offset |
| 28 | * of the function from gd->relocaddr |
| 29 | */ |
| 30 | uintptr_t *call_accum; |
| 31 | |
| 32 | /* Function trace list */ |
| 33 | struct trace_call *ftrace; /* The function call records */ |
| 34 | ulong ftrace_size; /* Num. of ftrace records we have space for */ |
| 35 | ulong ftrace_count; /* Num. of ftrace records written */ |
| 36 | ulong ftrace_too_deep_count; /* Functions that were too deep */ |
| 37 | |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 38 | int depth; /* Depth of function calls */ |
| 39 | int depth_limit; /* Depth limit to trace to */ |
| 40 | int max_depth; /* Maximum depth seen so far */ |
| 41 | int min_depth; /* Minimum depth seen so far */ |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Simon Glass | bebc141 | 2022-12-21 16:08:22 -0700 | [diff] [blame] | 44 | /* Pointer to start of trace buffer */ |
| 45 | static struct trace_hdr *hdr __section(".data"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 46 | |
| 47 | static inline uintptr_t __attribute__((no_instrument_function)) |
| 48 | func_ptr_to_num(void *func_ptr) |
| 49 | { |
| 50 | uintptr_t offset = (uintptr_t)func_ptr; |
| 51 | |
| 52 | #ifdef CONFIG_SANDBOX |
| 53 | offset -= (uintptr_t)&_init; |
| 54 | #else |
| 55 | if (gd->flags & GD_FLG_RELOC) |
| 56 | offset -= gd->relocaddr; |
| 57 | else |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 58 | offset -= CONFIG_TEXT_BASE; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 59 | #endif |
| 60 | return offset / FUNC_SITE_SIZE; |
| 61 | } |
| 62 | |
Heinrich Schuchardt | d3d6afa | 2020-10-15 12:30:09 +0200 | [diff] [blame] | 63 | #if defined(CONFIG_EFI_LOADER) && (defined(CONFIG_ARM) || defined(CONFIG_RISCV)) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * trace_gd - the value of the gd register |
| 67 | */ |
Heinrich Schuchardt | 7f642cb | 2020-05-27 20:04:22 +0200 | [diff] [blame] | 68 | static volatile gd_t *trace_gd; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * trace_save_gd() - save the value of the gd register |
| 72 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 73 | static void notrace trace_save_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 74 | { |
| 75 | trace_gd = gd; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * trace_swap_gd() - swap between U-Boot and application gd register value |
| 80 | * |
| 81 | * An UEFI application may change the value of the register that gd lives in. |
| 82 | * But some of our functions like get_ticks() access this register. So we |
| 83 | * have to set the gd register to the U-Boot value when entering a trace |
| 84 | * point and set it back to the application value when exiting the trace point. |
| 85 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 86 | static void notrace trace_swap_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 87 | { |
Heinrich Schuchardt | 7f642cb | 2020-05-27 20:04:22 +0200 | [diff] [blame] | 88 | volatile gd_t *temp_gd = trace_gd; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 89 | |
| 90 | trace_gd = gd; |
Heinrich Schuchardt | 7f642cb | 2020-05-27 20:04:22 +0200 | [diff] [blame] | 91 | set_gd(temp_gd); |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | #else |
| 95 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 96 | static void notrace trace_save_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 100 | static void notrace trace_swap_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 101 | { |
| 102 | } |
| 103 | |
| 104 | #endif |
| 105 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 106 | static void notrace add_ftrace(void *func_ptr, void *caller, ulong flags) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 107 | { |
| 108 | if (hdr->depth > hdr->depth_limit) { |
| 109 | hdr->ftrace_too_deep_count++; |
| 110 | return; |
| 111 | } |
| 112 | if (hdr->ftrace_count < hdr->ftrace_size) { |
| 113 | struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count]; |
| 114 | |
| 115 | rec->func = func_ptr_to_num(func_ptr); |
| 116 | rec->caller = func_ptr_to_num(caller); |
| 117 | rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK); |
| 118 | } |
| 119 | hdr->ftrace_count++; |
| 120 | } |
| 121 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 122 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 123 | * __cyg_profile_func_enter() - record function entry |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 124 | * |
| 125 | * We add to our tally for this function and add to the list of called |
| 126 | * functions. |
| 127 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 128 | * @func_ptr: pointer to function being entered |
| 129 | * @caller: pointer to function which called this function |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 130 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 131 | void notrace __cyg_profile_func_enter(void *func_ptr, void *caller) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 132 | { |
| 133 | if (trace_enabled) { |
| 134 | int func; |
| 135 | |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 136 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 137 | add_ftrace(func_ptr, caller, FUNCF_ENTRY); |
| 138 | func = func_ptr_to_num(func_ptr); |
| 139 | if (func < hdr->func_count) { |
| 140 | hdr->call_accum[func]++; |
| 141 | hdr->call_count++; |
| 142 | } else { |
| 143 | hdr->untracked_count++; |
| 144 | } |
| 145 | hdr->depth++; |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 146 | if (hdr->depth > hdr->max_depth) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 147 | hdr->max_depth = hdr->depth; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 148 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 153 | * __cyg_profile_func_exit() - record function exit |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 154 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 155 | * @func_ptr: pointer to function being entered |
| 156 | * @caller: pointer to function which called this function |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 157 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 158 | void notrace __cyg_profile_func_exit(void *func_ptr, void *caller) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 159 | { |
| 160 | if (trace_enabled) { |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 161 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 162 | hdr->depth--; |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 163 | add_ftrace(func_ptr, caller, FUNCF_EXIT); |
| 164 | if (hdr->depth < hdr->min_depth) |
| 165 | hdr->min_depth = hdr->depth; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 166 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 171 | * trace_list_functions() - produce a list of called functions |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 172 | * |
| 173 | * The information is written into the supplied buffer - a header followed |
| 174 | * by a list of function records. |
| 175 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 176 | * @buff: buffer to place list into |
| 177 | * @buff_size: size of buffer |
| 178 | * @needed: returns size of buffer needed, which may be |
| 179 | * greater than buff_size if we ran out of space. |
| 180 | * Return: 0 if ok, -ENOSPC if space was exhausted |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 181 | */ |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 182 | int trace_list_functions(void *buff, size_t buff_size, size_t *needed) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 183 | { |
| 184 | struct trace_output_hdr *output_hdr = NULL; |
| 185 | void *end, *ptr = buff; |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 186 | size_t func; |
| 187 | size_t upto; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 188 | |
| 189 | end = buff ? buff + buff_size : NULL; |
| 190 | |
| 191 | /* Place some header information */ |
| 192 | if (ptr + sizeof(struct trace_output_hdr) < end) |
| 193 | output_hdr = ptr; |
| 194 | ptr += sizeof(struct trace_output_hdr); |
| 195 | |
| 196 | /* Add information about each function */ |
| 197 | for (func = upto = 0; func < hdr->func_count; func++) { |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 198 | size_t calls = hdr->call_accum[func]; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 199 | |
| 200 | if (!calls) |
| 201 | continue; |
| 202 | |
| 203 | if (ptr + sizeof(struct trace_output_func) < end) { |
| 204 | struct trace_output_func *stats = ptr; |
| 205 | |
| 206 | stats->offset = func * FUNC_SITE_SIZE; |
| 207 | stats->call_count = calls; |
| 208 | upto++; |
| 209 | } |
| 210 | ptr += sizeof(struct trace_output_func); |
| 211 | } |
| 212 | |
| 213 | /* Update the header */ |
| 214 | if (output_hdr) { |
| 215 | output_hdr->rec_count = upto; |
| 216 | output_hdr->type = TRACE_CHUNK_FUNCS; |
| 217 | } |
| 218 | |
| 219 | /* Work out how must of the buffer we used */ |
| 220 | *needed = ptr - buff; |
| 221 | if (ptr > end) |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 222 | return -ENOSPC; |
| 223 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 227 | /** |
| 228 | * trace_list_functions() - produce a list of function calls |
| 229 | * |
| 230 | * The information is written into the supplied buffer - a header followed |
| 231 | * by a list of function records. |
| 232 | * |
| 233 | * @buff: buffer to place list into |
| 234 | * @buff_size: size of buffer |
| 235 | * @needed: returns size of buffer needed, which may be |
| 236 | * greater than buff_size if we ran out of space. |
| 237 | * Return: 0 if ok, -ENOSPC if space was exhausted |
| 238 | */ |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 239 | int trace_list_calls(void *buff, size_t buff_size, size_t *needed) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 240 | { |
| 241 | struct trace_output_hdr *output_hdr = NULL; |
| 242 | void *end, *ptr = buff; |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 243 | size_t rec, upto; |
| 244 | size_t count; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 245 | |
| 246 | end = buff ? buff + buff_size : NULL; |
| 247 | |
| 248 | /* Place some header information */ |
| 249 | if (ptr + sizeof(struct trace_output_hdr) < end) |
| 250 | output_hdr = ptr; |
| 251 | ptr += sizeof(struct trace_output_hdr); |
| 252 | |
| 253 | /* Add information about each call */ |
| 254 | count = hdr->ftrace_count; |
| 255 | if (count > hdr->ftrace_size) |
| 256 | count = hdr->ftrace_size; |
| 257 | for (rec = upto = 0; rec < count; rec++) { |
| 258 | if (ptr + sizeof(struct trace_call) < end) { |
| 259 | struct trace_call *call = &hdr->ftrace[rec]; |
| 260 | struct trace_call *out = ptr; |
| 261 | |
| 262 | out->func = call->func * FUNC_SITE_SIZE; |
| 263 | out->caller = call->caller * FUNC_SITE_SIZE; |
| 264 | out->flags = call->flags; |
| 265 | upto++; |
| 266 | } |
| 267 | ptr += sizeof(struct trace_call); |
| 268 | } |
| 269 | |
| 270 | /* Update the header */ |
| 271 | if (output_hdr) { |
Simon Glass | d9044e5 | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 272 | memset(output_hdr, '\0', sizeof(*output_hdr)); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 273 | output_hdr->rec_count = upto; |
| 274 | output_hdr->type = TRACE_CHUNK_CALLS; |
Simon Glass | d9044e5 | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 275 | output_hdr->version = TRACE_VERSION; |
| 276 | output_hdr->text_base = CONFIG_TEXT_BASE; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Work out how must of the buffer we used */ |
| 280 | *needed = ptr - buff; |
| 281 | if (ptr > end) |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 282 | return -ENOSPC; |
| 283 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 287 | /** |
| 288 | * trace_print_stats() - print basic information about tracing |
| 289 | */ |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 290 | void trace_print_stats(void) |
| 291 | { |
| 292 | ulong count; |
| 293 | |
| 294 | #ifndef FTRACE |
| 295 | puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n"); |
| 296 | puts("You will likely get zeroed data here\n"); |
| 297 | #endif |
| 298 | if (!trace_inited) { |
| 299 | printf("Trace is disabled\n"); |
| 300 | return; |
| 301 | } |
| 302 | print_grouped_ull(hdr->func_count, 10); |
| 303 | puts(" function sites\n"); |
| 304 | print_grouped_ull(hdr->call_count, 10); |
| 305 | puts(" function calls\n"); |
| 306 | print_grouped_ull(hdr->untracked_count, 10); |
| 307 | puts(" untracked function calls\n"); |
| 308 | count = min(hdr->ftrace_count, hdr->ftrace_size); |
| 309 | print_grouped_ull(count, 10); |
| 310 | puts(" traced function calls"); |
| 311 | if (hdr->ftrace_count > hdr->ftrace_size) { |
| 312 | printf(" (%lu dropped due to overflow)", |
| 313 | hdr->ftrace_count - hdr->ftrace_size); |
| 314 | } |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 315 | |
| 316 | /* Add in minimum depth since the trace did not start at top level */ |
| 317 | printf("\n%15d maximum observed call depth\n", |
| 318 | hdr->max_depth - hdr->min_depth); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 319 | printf("%15d call depth limit\n", hdr->depth_limit); |
| 320 | print_grouped_ull(hdr->ftrace_too_deep_count, 10); |
| 321 | puts(" calls not traced due to depth\n"); |
| 322 | } |
| 323 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 324 | void notrace trace_set_enabled(int enabled) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 325 | { |
| 326 | trace_enabled = enabled != 0; |
| 327 | } |
| 328 | |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 329 | static int get_func_count(void) |
| 330 | { |
| 331 | /* Detect no support for mon_len since this means tracing cannot work */ |
| 332 | if (IS_ENABLED(CONFIG_SANDBOX) && !gd->mon_len) { |
| 333 | puts("Tracing is not supported on this board\n"); |
| 334 | return -ENOTSUPP; |
| 335 | } |
| 336 | |
| 337 | return gd->mon_len / FUNC_SITE_SIZE; |
| 338 | } |
| 339 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 340 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 341 | * trace_init() - initialize the tracing system and enable it |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 342 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 343 | * @buff: Pointer to trace buffer |
| 344 | * @buff_size: Size of trace buffer |
| 345 | * Return: 0 if ok |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 346 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 347 | int notrace trace_init(void *buff, size_t buff_size) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 348 | { |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 349 | int func_count = get_func_count(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 350 | size_t needed; |
| 351 | int was_disabled = !trace_enabled; |
| 352 | |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 353 | if (func_count < 0) |
| 354 | return func_count; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 355 | trace_save_gd(); |
| 356 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 357 | if (!was_disabled) { |
| 358 | #ifdef CONFIG_TRACE_EARLY |
| 359 | char *end; |
| 360 | ulong used; |
| 361 | |
| 362 | /* |
| 363 | * Copy over the early trace data if we have it. Disable |
| 364 | * tracing while we are doing this. |
| 365 | */ |
| 366 | trace_enabled = 0; |
| 367 | hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, |
| 368 | CONFIG_TRACE_EARLY_SIZE); |
Simon Glass | 1c6eb07 | 2019-04-08 13:20:52 -0600 | [diff] [blame] | 369 | end = (char *)&hdr->ftrace[min(hdr->ftrace_count, |
| 370 | hdr->ftrace_size)]; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 371 | used = end - (char *)hdr; |
| 372 | printf("trace: copying %08lx bytes of early data from %x to %08lx\n", |
| 373 | used, CONFIG_TRACE_EARLY_ADDR, |
| 374 | (ulong)map_to_sysmem(buff)); |
| 375 | memcpy(buff, hdr, used); |
| 376 | #else |
| 377 | puts("trace: already enabled\n"); |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 378 | return -EALREADY; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 379 | #endif |
| 380 | } |
| 381 | hdr = (struct trace_hdr *)buff; |
| 382 | needed = sizeof(*hdr) + func_count * sizeof(uintptr_t); |
| 383 | if (needed > buff_size) { |
| 384 | printf("trace: buffer size %zd bytes: at least %zd needed\n", |
| 385 | buff_size, needed); |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 386 | return -ENOSPC; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 389 | if (was_disabled) { |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 390 | memset(hdr, '\0', needed); |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 391 | hdr->min_depth = INT_MAX; |
| 392 | } |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 393 | hdr->func_count = func_count; |
| 394 | hdr->call_accum = (uintptr_t *)(hdr + 1); |
| 395 | |
| 396 | /* Use any remaining space for the timed function trace */ |
| 397 | hdr->ftrace = (struct trace_call *)(buff + needed); |
| 398 | hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace); |
Simon Glass | d9044e5 | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 399 | hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 400 | |
| 401 | puts("trace: enabled\n"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 402 | trace_enabled = 1; |
| 403 | trace_inited = 1; |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 404 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | #ifdef CONFIG_TRACE_EARLY |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 409 | /** |
| 410 | * trace_early_init() - initialize the tracing system for early tracing |
| 411 | * |
| 412 | * Return: 0 if ok, -ENOSPC if not enough memory is available |
| 413 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 414 | int notrace trace_early_init(void) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 415 | { |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 416 | int func_count = get_func_count(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 417 | size_t buff_size = CONFIG_TRACE_EARLY_SIZE; |
| 418 | size_t needed; |
| 419 | |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 420 | if (func_count < 0) |
| 421 | return func_count; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 422 | /* We can ignore additional calls to this function */ |
| 423 | if (trace_enabled) |
| 424 | return 0; |
| 425 | |
| 426 | hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE); |
| 427 | needed = sizeof(*hdr) + func_count * sizeof(uintptr_t); |
| 428 | if (needed > buff_size) { |
| 429 | printf("trace: buffer size is %zd bytes, at least %zd needed\n", |
| 430 | buff_size, needed); |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 431 | return -ENOSPC; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | memset(hdr, '\0', needed); |
| 435 | hdr->call_accum = (uintptr_t *)(hdr + 1); |
| 436 | hdr->func_count = func_count; |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame^] | 437 | hdr->min_depth = INT_MAX; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 438 | |
| 439 | /* Use any remaining space for the timed function trace */ |
| 440 | hdr->ftrace = (struct trace_call *)((char *)hdr + needed); |
| 441 | hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace); |
Heinrich Schuchardt | da0fb5f | 2019-06-02 13:30:09 +0200 | [diff] [blame] | 442 | hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 443 | printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR); |
| 444 | |
| 445 | trace_enabled = 1; |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 446 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | #endif |