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)
59 struct bootstage_data *data = gd->bootstage;
63 /* Figure out where to relocate the strings to */
64 ptr = (char *)(data + 1);
67 * Duplicate all strings. They may point to an old location in the
68 * program .text section that can eventually get trashed.
70 debug("Relocating %d records\n", data->rec_count);
71 for (i = 0; i < data->rec_count; i++) {
72 const char *from = data->record[i].name;
75 data->record[i].name = ptr;
76 ptr += strlen(ptr) + 1;
82 struct bootstage_record *find_id(struct bootstage_data *data,
85 struct bootstage_record *rec;
86 struct bootstage_record *end;
88 for (rec = data->record, end = rec + data->rec_count; rec < end;
97 struct bootstage_record *ensure_id(struct bootstage_data *data,
100 struct bootstage_record *rec;
102 rec = find_id(data, id);
103 if (!rec && data->rec_count < RECORD_COUNT) {
104 rec = &data->record[data->rec_count++];
112 ulong bootstage_add_record(enum bootstage_id id, const char *name,
113 int flags, ulong mark)
115 struct bootstage_data *data = gd->bootstage;
116 struct bootstage_record *rec;
119 * initf_bootstage() is called very early during boot but since hang()
120 * calls bootstage_error() we can be called before bootstage is set up.
121 * Add a check to avoid this.
125 if (flags & BOOTSTAGEF_ALLOC)
126 id = data->next_id++;
128 /* Only record the first event for each */
129 rec = find_id(data, id);
131 if (data->rec_count < RECORD_COUNT) {
132 rec = &data->record[data->rec_count++];
138 log_warning("Bootstage space exhausted\n");
142 /* Tell the board about this progress */
143 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
148 ulong bootstage_error_name(enum bootstage_id id, const char *name)
150 return bootstage_add_record(id, name, BOOTSTAGEF_ERROR,
151 timer_get_boot_us());
154 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
158 if (id == BOOTSTAGE_ID_ALLOC)
159 flags = BOOTSTAGEF_ALLOC;
161 return bootstage_add_record(id, name, flags, timer_get_boot_us());
164 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
167 __maybe_unused char *end;
170 /* First work out the length we need to allocate */
178 str = malloc(len + 1);
182 p += snprintf(p, end - p, "%s,", file);
184 p += snprintf(p, end - p, "%d", linenum);
186 p += snprintf(p, end - p, ": %s", func);
188 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
191 uint32_t bootstage_start(enum bootstage_id id, const char *name)
193 struct bootstage_data *data = gd->bootstage;
194 struct bootstage_record *rec = ensure_id(data, id);
195 ulong start_us = timer_get_boot_us();
198 rec->start_us = start_us;
205 uint32_t bootstage_accum(enum bootstage_id id)
207 struct bootstage_data *data = gd->bootstage;
208 struct bootstage_record *rec = ensure_id(data, id);
213 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
214 rec->time_us += duration;
220 * Get a record name as a printable string
222 * @param buf Buffer to put name if needed
223 * @param len Length of buffer
224 * @param rec Boot stage record to get the name from
225 * Return: pointer to name, either from the record or pointing to buf.
227 static const char *get_record_name(char *buf, int len,
228 const struct bootstage_record *rec)
232 else if (rec->id >= BOOTSTAGE_ID_USER)
233 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
235 snprintf(buf, len, "id=%d", rec->id);
240 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
246 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
248 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
249 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
251 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
256 static int h_compare_record(const void *r1, const void *r2)
258 const struct bootstage_record *rec1 = r1, *rec2 = r2;
260 return rec1->time_us > rec2->time_us ? 1 : -1;
263 #ifdef CONFIG_OF_LIBFDT
265 * Add all bootstage timings to a device tree.
267 * @param blob Device tree blob
268 * Return: 0 on success, != 0 on failure.
270 static int add_bootstages_devicetree(struct fdt_header *blob)
272 struct bootstage_data *data = gd->bootstage;
282 * Create the node for bootstage.
283 * The address of flat device tree is set up by the command bootm.
285 bootstage = fdt_add_subnode(blob, 0, "bootstage");
290 * Insert the timings to the device tree in the reverse order so
291 * that they can be printed in the Linux kernel in the right order.
293 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
294 struct bootstage_record *rec = &data->record[recnum];
297 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
300 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
304 /* add properties to the node. */
305 if (fdt_setprop_string(blob, node, "name",
306 get_record_name(buf, sizeof(buf), rec)))
309 /* Check if this is a 'mark' or 'accum' record */
310 if (fdt_setprop_cell(blob, node,
311 rec->start_us ? "accum" : "mark",
319 int bootstage_fdt_add_report(void)
321 if (add_bootstages_devicetree(working_fdt))
322 puts("bootstage: Failed to add to device tree\n");
328 void bootstage_report(void)
330 struct bootstage_data *data = gd->bootstage;
331 struct bootstage_record *rec = data->record;
335 printf("Timer summary in microseconds (%d records):\n",
337 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
339 prev = print_time_record(rec, 0);
341 /* Sort records by increasing time */
342 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
344 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
345 if (rec->id && !rec->start_us)
346 prev = print_time_record(rec, prev);
348 if (data->rec_count > RECORD_COUNT)
349 printf("Overflowed internal boot id table by %d entries\n"
350 "Please increase CONFIG_(SPL_TPL_)BOOTSTAGE_RECORD_COUNT\n",
351 data->rec_count - RECORD_COUNT);
353 puts("\nAccumulated time:\n");
354 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
356 prev = print_time_record(rec, -1);
361 * Append data to a memory buffer
363 * Write data to the buffer if there is space. Whether there is space or not,
364 * the buffer pointer is incremented.
366 * @param ptrp Pointer to buffer, updated by this function
367 * @param end Pointer to end of buffer
368 * @param data Data to write to buffer
369 * @param size Size of data
371 static void append_data(char **ptrp, char *end, const void *data, int size)
379 memcpy(ptr, data, size);
382 int bootstage_stash(void *base, int size)
384 const struct bootstage_data *data = gd->bootstage;
385 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
386 const struct bootstage_record *rec;
388 char *ptr = base, *end = ptr + size;
391 if (hdr + 1 > (struct bootstage_hdr *)end) {
392 debug("%s: Not enough space for bootstage hdr\n", __func__);
396 /* Write an arbitrary version number */
397 hdr->version = BOOTSTAGE_VERSION;
399 hdr->count = data->rec_count;
401 hdr->magic = BOOTSTAGE_MAGIC;
402 hdr->next_id = data->next_id;
405 /* Write the records, silently stopping when we run out of space */
406 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
407 append_data(&ptr, end, rec, sizeof(*rec));
409 /* Write the name strings */
410 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
413 name = get_record_name(buf, sizeof(buf), rec);
414 append_data(&ptr, end, name, strlen(name) + 1);
417 /* Check for buffer overflow */
419 debug("%s: Not enough space for bootstage stash\n", __func__);
423 /* Update total data size */
424 hdr->size = ptr - (char *)base;
425 debug("Stashed %d records\n", hdr->count);
430 int bootstage_unstash(const void *base, int size)
432 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
433 struct bootstage_data *data = gd->bootstage;
434 const char *ptr = base, *end = ptr + size;
435 struct bootstage_record *rec;
440 end = (char *)(~(uintptr_t)0);
442 if (hdr + 1 > (struct bootstage_hdr *)end) {
443 debug("%s: Not enough space for bootstage hdr\n", __func__);
447 if (hdr->magic != BOOTSTAGE_MAGIC) {
448 debug("%s: Invalid bootstage magic\n", __func__);
452 if (ptr + hdr->size > end) {
453 debug("%s: Bootstage data runs past buffer end\n", __func__);
457 if (hdr->count * sizeof(*rec) > hdr->size) {
458 debug("%s: Bootstage has %d records needing %lu bytes, but "
459 "only %d bytes is available\n", __func__, hdr->count,
460 (ulong)hdr->count * sizeof(*rec), hdr->size);
464 if (hdr->version != BOOTSTAGE_VERSION) {
465 debug("%s: Bootstage data version %#0x unrecognised\n",
466 __func__, hdr->version);
470 if (data->rec_count + hdr->count > RECORD_COUNT) {
471 debug("%s: Bootstage has %d records, we have space for %d\n"
472 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
473 __func__, hdr->count, RECORD_COUNT - data->rec_count);
479 /* Read the records */
480 rec_size = hdr->count * sizeof(*data->record);
481 memcpy(data->record + data->rec_count, ptr, rec_size);
483 /* Read the name strings */
485 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
488 if (spl_phase() == PHASE_SPL)
489 rec->name = strdup(ptr);
491 /* Assume no data corruption here */
492 ptr += strlen(ptr) + 1;
495 /* Mark the records as read */
496 data->rec_count += hdr->count;
497 data->next_id = hdr->next_id;
498 debug("Unstashed %d records\n", hdr->count);
503 int _bootstage_stash_default(void)
505 return bootstage_stash(map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR, 0),
506 CONFIG_BOOTSTAGE_STASH_SIZE);
509 int _bootstage_unstash_default(void)
511 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
512 CONFIG_BOOTSTAGE_STASH_SIZE);
514 return bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
517 int bootstage_get_size(void)
519 struct bootstage_data *data = gd->bootstage;
520 struct bootstage_record *rec;
524 size = sizeof(struct bootstage_data);
525 for (rec = data->record, i = 0; i < data->rec_count;
527 size += strlen(rec->name) + 1;
532 int bootstage_init(bool first)
534 struct bootstage_data *data;
535 int size = sizeof(struct bootstage_data);
537 gd->bootstage = (struct bootstage_data *)malloc(size);
540 data = gd->bootstage;
541 memset(data, '\0', size);
543 data->next_id = BOOTSTAGE_ID_USER;
544 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);