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