1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011, Google Inc. All rights reserved.
7 * This module records the progress of boot and arbitrary commands, and
8 * permits accurate timestamping of each.
11 #define LOG_CATEGORY LOGC_BOOT
13 #include <bootstage.h>
19 #include <asm/global_data.h>
20 #include <linux/compiler.h>
21 #include <linux/libfdt.h>
23 DECLARE_GLOBAL_DATA_PTR;
26 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
29 struct bootstage_record {
33 int flags; /* see enum bootstage_flags */
37 struct bootstage_data {
40 struct bootstage_record record[RECORD_COUNT];
44 BOOTSTAGE_VERSION = 0,
45 BOOTSTAGE_MAGIC = 0xb00757a3,
49 struct bootstage_hdr {
50 u32 version; /* BOOTSTAGE_VERSION */
51 u32 count; /* Number of records */
52 u32 size; /* Total data size (non-zero if valid) */
53 u32 magic; /* Magic number */
54 u32 next_id; /* Next ID to use for bootstage */
57 int bootstage_relocate(void *to)
59 struct bootstage_data *data;
63 debug("Copying bootstage from %p to %p\n", gd->bootstage, to);
64 memcpy(to, gd->bootstage, sizeof(struct bootstage_data));
65 data = gd->bootstage = to;
67 /* Figure out where to relocate the strings to */
68 ptr = (char *)(data + 1);
71 * Duplicate all strings. They may point to an old location in the
72 * program .text section that can eventually get trashed.
74 debug("Relocating %d records\n", data->rec_count);
75 for (i = 0; i < data->rec_count; i++) {
76 const char *from = data->record[i].name;
79 data->record[i].name = ptr;
80 ptr += strlen(ptr) + 1;
86 struct bootstage_record *find_id(struct bootstage_data *data,
89 struct bootstage_record *rec;
90 struct bootstage_record *end;
92 for (rec = data->record, end = rec + data->rec_count; rec < end;
101 struct bootstage_record *ensure_id(struct bootstage_data *data,
102 enum bootstage_id id)
104 struct bootstage_record *rec;
106 rec = find_id(data, id);
107 if (!rec && data->rec_count < RECORD_COUNT) {
108 rec = &data->record[data->rec_count++];
116 ulong bootstage_add_record(enum bootstage_id id, const char *name,
117 int flags, ulong mark)
119 struct bootstage_data *data = gd->bootstage;
120 struct bootstage_record *rec;
123 * initf_bootstage() is called very early during boot but since hang()
124 * calls bootstage_error() we can be called before bootstage is set up.
125 * Add a check to avoid this.
129 if (flags & BOOTSTAGEF_ALLOC)
130 id = data->next_id++;
132 /* Only record the first event for each */
133 rec = find_id(data, id);
135 if (data->rec_count < RECORD_COUNT) {
136 rec = &data->record[data->rec_count++];
142 log_warning("Bootstage space exhausted\n");
146 /* Tell the board about this progress */
147 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
152 ulong bootstage_error_name(enum bootstage_id id, const char *name)
154 return bootstage_add_record(id, name, BOOTSTAGEF_ERROR,
155 timer_get_boot_us());
158 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
162 if (id == BOOTSTAGE_ID_ALLOC)
163 flags = BOOTSTAGEF_ALLOC;
165 return bootstage_add_record(id, name, flags, timer_get_boot_us());
168 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
171 __maybe_unused char *end;
174 /* First work out the length we need to allocate */
182 str = malloc(len + 1);
186 p += snprintf(p, end - p, "%s,", file);
188 p += snprintf(p, end - p, "%d", linenum);
190 p += snprintf(p, end - p, ": %s", func);
192 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
195 uint32_t bootstage_start(enum bootstage_id id, const char *name)
197 struct bootstage_data *data = gd->bootstage;
198 struct bootstage_record *rec = ensure_id(data, id);
199 ulong start_us = timer_get_boot_us();
202 rec->start_us = start_us;
209 uint32_t bootstage_accum(enum bootstage_id id)
211 struct bootstage_data *data = gd->bootstage;
212 struct bootstage_record *rec = ensure_id(data, id);
217 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
218 rec->time_us += duration;
224 * Get a record name as a printable string
226 * @param buf Buffer to put name if needed
227 * @param len Length of buffer
228 * @param rec Boot stage record to get the name from
229 * Return: pointer to name, either from the record or pointing to buf.
231 static const char *get_record_name(char *buf, int len,
232 const struct bootstage_record *rec)
236 else if (rec->id >= BOOTSTAGE_ID_USER)
237 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
239 snprintf(buf, len, "id=%d", rec->id);
244 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
250 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
252 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
253 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
255 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
260 static int h_compare_record(const void *r1, const void *r2)
262 const struct bootstage_record *rec1 = r1, *rec2 = r2;
264 return rec1->time_us > rec2->time_us ? 1 : -1;
267 #ifdef CONFIG_OF_LIBFDT
269 * Add all bootstage timings to a device tree.
271 * @param blob Device tree blob
272 * Return: 0 on success, != 0 on failure.
274 static int add_bootstages_devicetree(struct fdt_header *blob)
276 struct bootstage_data *data = gd->bootstage;
286 * Create the node for bootstage.
287 * The address of flat device tree is set up by the command bootm.
289 bootstage = fdt_add_subnode(blob, 0, "bootstage");
294 * Insert the timings to the device tree in the reverse order so
295 * that they can be printed in the Linux kernel in the right order.
297 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
298 struct bootstage_record *rec = &data->record[recnum];
301 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
304 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
308 /* add properties to the node. */
309 if (fdt_setprop_string(blob, node, "name",
310 get_record_name(buf, sizeof(buf), rec)))
313 /* Check if this is a 'mark' or 'accum' record */
314 if (fdt_setprop_cell(blob, node,
315 rec->start_us ? "accum" : "mark",
323 int bootstage_fdt_add_report(void)
325 if (add_bootstages_devicetree(working_fdt))
326 puts("bootstage: Failed to add to device tree\n");
332 void bootstage_report(void)
334 struct bootstage_data *data = gd->bootstage;
335 struct bootstage_record *rec = data->record;
339 printf("Timer summary in microseconds (%d records):\n",
341 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
343 prev = print_time_record(rec, 0);
345 /* Sort records by increasing time */
346 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
348 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
349 if (rec->id && !rec->start_us)
350 prev = print_time_record(rec, prev);
352 if (data->rec_count > RECORD_COUNT)
353 printf("Overflowed internal boot id table by %d entries\n"
354 "Please increase CONFIG_(SPL_TPL_)BOOTSTAGE_RECORD_COUNT\n",
355 data->rec_count - RECORD_COUNT);
357 puts("\nAccumulated time:\n");
358 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
360 prev = print_time_record(rec, -1);
365 * Append data to a memory buffer
367 * Write data to the buffer if there is space. Whether there is space or not,
368 * the buffer pointer is incremented.
370 * @param ptrp Pointer to buffer, updated by this function
371 * @param end Pointer to end of buffer
372 * @param data Data to write to buffer
373 * @param size Size of data
375 static void append_data(char **ptrp, char *end, const void *data, int size)
383 memcpy(ptr, data, size);
386 int bootstage_stash(void *base, int size)
388 const struct bootstage_data *data = gd->bootstage;
389 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
390 const struct bootstage_record *rec;
392 char *ptr = base, *end = ptr + size;
395 if (hdr + 1 > (struct bootstage_hdr *)end) {
396 debug("%s: Not enough space for bootstage hdr\n", __func__);
400 /* Write an arbitrary version number */
401 hdr->version = BOOTSTAGE_VERSION;
403 hdr->count = data->rec_count;
405 hdr->magic = BOOTSTAGE_MAGIC;
406 hdr->next_id = data->next_id;
409 /* Write the records, silently stopping when we run out of space */
410 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
411 append_data(&ptr, end, rec, sizeof(*rec));
413 /* Write the name strings */
414 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
417 name = get_record_name(buf, sizeof(buf), rec);
418 append_data(&ptr, end, name, strlen(name) + 1);
421 /* Check for buffer overflow */
423 debug("%s: Not enough space for bootstage stash\n", __func__);
427 /* Update total data size */
428 hdr->size = ptr - (char *)base;
429 debug("Stashed %d records\n", hdr->count);
434 int bootstage_unstash(const void *base, int size)
436 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
437 struct bootstage_data *data = gd->bootstage;
438 const char *ptr = base, *end = ptr + size;
439 struct bootstage_record *rec;
444 end = (char *)(~(uintptr_t)0);
446 if (hdr + 1 > (struct bootstage_hdr *)end) {
447 debug("%s: Not enough space for bootstage hdr\n", __func__);
451 if (hdr->magic != BOOTSTAGE_MAGIC) {
452 debug("%s: Invalid bootstage magic\n", __func__);
456 if (ptr + hdr->size > end) {
457 debug("%s: Bootstage data runs past buffer end\n", __func__);
461 if (hdr->count * sizeof(*rec) > hdr->size) {
462 debug("%s: Bootstage has %d records needing %lu bytes, but "
463 "only %d bytes is available\n", __func__, hdr->count,
464 (ulong)hdr->count * sizeof(*rec), hdr->size);
468 if (hdr->version != BOOTSTAGE_VERSION) {
469 debug("%s: Bootstage data version %#0x unrecognised\n",
470 __func__, hdr->version);
474 if (data->rec_count + hdr->count > RECORD_COUNT) {
475 debug("%s: Bootstage has %d records, we have space for %d\n"
476 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
477 __func__, hdr->count, RECORD_COUNT - data->rec_count);
483 /* Read the records */
484 rec_size = hdr->count * sizeof(*data->record);
485 memcpy(data->record + data->rec_count, ptr, rec_size);
487 /* Read the name strings */
489 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
492 if (spl_phase() == PHASE_SPL)
493 rec->name = strdup(ptr);
495 /* Assume no data corruption here */
496 ptr += strlen(ptr) + 1;
499 /* Mark the records as read */
500 data->rec_count += hdr->count;
501 data->next_id = hdr->next_id;
502 debug("Unstashed %d records\n", hdr->count);
507 #if IS_ENABLED(CONFIG_BOOTSTAGE_STASH)
508 int _bootstage_stash_default(void)
510 return bootstage_stash(map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR, 0),
511 CONFIG_BOOTSTAGE_STASH_SIZE);
514 int _bootstage_unstash_default(void)
516 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
517 CONFIG_BOOTSTAGE_STASH_SIZE);
519 return bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
523 int bootstage_get_size(void)
525 struct bootstage_data *data = gd->bootstage;
526 struct bootstage_record *rec;
530 size = sizeof(struct bootstage_data);
531 for (rec = data->record, i = 0; i < data->rec_count;
533 size += strlen(rec->name) + 1;
538 int bootstage_init(bool first)
540 struct bootstage_data *data;
541 int size = sizeof(struct bootstage_data);
543 gd->bootstage = (struct bootstage_data *)malloc(size);
546 data = gd->bootstage;
547 memset(data, '\0', size);
549 data->next_id = BOOTSTAGE_ID_USER;
550 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);