1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011, Google Inc. All rights reserved.
8 * This module records the progress of boot and arbitrary commands, and
9 * permits accurate timestamping of each.
12 #define LOG_CATEGORY LOGC_BOOT
15 #include <bootstage.h>
21 #include <asm/global_data.h>
22 #include <linux/compiler.h>
23 #include <linux/libfdt.h>
25 DECLARE_GLOBAL_DATA_PTR;
28 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
31 struct bootstage_record {
35 int flags; /* see enum bootstage_flags */
39 struct bootstage_data {
42 struct bootstage_record record[RECORD_COUNT];
46 BOOTSTAGE_VERSION = 0,
47 BOOTSTAGE_MAGIC = 0xb00757a3,
51 struct bootstage_hdr {
52 u32 version; /* BOOTSTAGE_VERSION */
53 u32 count; /* Number of records */
54 u32 size; /* Total data size (non-zero if valid) */
55 u32 magic; /* Magic number */
56 u32 next_id; /* Next ID to use for bootstage */
59 int bootstage_relocate(void)
61 struct bootstage_data *data = gd->bootstage;
65 /* Figure out where to relocate the strings to */
66 ptr = (char *)(data + 1);
69 * Duplicate all strings. They may point to an old location in the
70 * program .text section that can eventually get trashed.
72 debug("Relocating %d records\n", data->rec_count);
73 for (i = 0; i < data->rec_count; i++) {
74 const char *from = data->record[i].name;
77 data->record[i].name = ptr;
78 ptr += strlen(ptr) + 1;
84 struct bootstage_record *find_id(struct bootstage_data *data,
87 struct bootstage_record *rec;
88 struct bootstage_record *end;
90 for (rec = data->record, end = rec + data->rec_count; rec < end;
99 struct bootstage_record *ensure_id(struct bootstage_data *data,
100 enum bootstage_id id)
102 struct bootstage_record *rec;
104 rec = find_id(data, id);
105 if (!rec && data->rec_count < RECORD_COUNT) {
106 rec = &data->record[data->rec_count++];
114 ulong bootstage_add_record(enum bootstage_id id, const char *name,
115 int flags, ulong mark)
117 struct bootstage_data *data = gd->bootstage;
118 struct bootstage_record *rec;
121 * initf_bootstage() is called very early during boot but since hang()
122 * calls bootstage_error() we can be called before bootstage is set up.
123 * Add a check to avoid this.
127 if (flags & BOOTSTAGEF_ALLOC)
128 id = data->next_id++;
130 /* Only record the first event for each */
131 rec = find_id(data, id);
133 if (data->rec_count < RECORD_COUNT) {
134 rec = &data->record[data->rec_count++];
140 log_warning("Bootstage space exhasuted\n");
144 /* Tell the board about this progress */
145 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
150 ulong bootstage_error_name(enum bootstage_id id, const char *name)
152 return bootstage_add_record(id, name, BOOTSTAGEF_ERROR,
153 timer_get_boot_us());
156 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
160 if (id == BOOTSTAGE_ID_ALLOC)
161 flags = BOOTSTAGEF_ALLOC;
163 return bootstage_add_record(id, name, flags, timer_get_boot_us());
166 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
169 __maybe_unused char *end;
172 /* First work out the length we need to allocate */
180 str = malloc(len + 1);
184 p += snprintf(p, end - p, "%s,", file);
186 p += snprintf(p, end - p, "%d", linenum);
188 p += snprintf(p, end - p, ": %s", func);
190 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
193 uint32_t bootstage_start(enum bootstage_id id, const char *name)
195 struct bootstage_data *data = gd->bootstage;
196 struct bootstage_record *rec = ensure_id(data, id);
197 ulong start_us = timer_get_boot_us();
200 rec->start_us = start_us;
207 uint32_t bootstage_accum(enum bootstage_id id)
209 struct bootstage_data *data = gd->bootstage;
210 struct bootstage_record *rec = ensure_id(data, id);
215 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
216 rec->time_us += duration;
222 * Get a record name as a printable string
224 * @param buf Buffer to put name if needed
225 * @param len Length of buffer
226 * @param rec Boot stage record to get the name from
227 * Return: pointer to name, either from the record or pointing to buf.
229 static const char *get_record_name(char *buf, int len,
230 const struct bootstage_record *rec)
234 else if (rec->id >= BOOTSTAGE_ID_USER)
235 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
237 snprintf(buf, len, "id=%d", rec->id);
242 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
248 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
250 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
251 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
253 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
258 static int h_compare_record(const void *r1, const void *r2)
260 const struct bootstage_record *rec1 = r1, *rec2 = r2;
262 return rec1->time_us > rec2->time_us ? 1 : -1;
265 #ifdef CONFIG_OF_LIBFDT
267 * Add all bootstage timings to a device tree.
269 * @param blob Device tree blob
270 * Return: 0 on success, != 0 on failure.
272 static int add_bootstages_devicetree(struct fdt_header *blob)
274 struct bootstage_data *data = gd->bootstage;
284 * Create the node for bootstage.
285 * The address of flat device tree is set up by the command bootm.
287 bootstage = fdt_add_subnode(blob, 0, "bootstage");
292 * Insert the timings to the device tree in the reverse order so
293 * that they can be printed in the Linux kernel in the right order.
295 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
296 struct bootstage_record *rec = &data->record[recnum];
299 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
302 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
306 /* add properties to the node. */
307 if (fdt_setprop_string(blob, node, "name",
308 get_record_name(buf, sizeof(buf), rec)))
311 /* Check if this is a 'mark' or 'accum' record */
312 if (fdt_setprop_cell(blob, node,
313 rec->start_us ? "accum" : "mark",
321 int bootstage_fdt_add_report(void)
323 if (add_bootstages_devicetree(working_fdt))
324 puts("bootstage: Failed to add to device tree\n");
330 void bootstage_report(void)
332 struct bootstage_data *data = gd->bootstage;
333 struct bootstage_record *rec = data->record;
337 printf("Timer summary in microseconds (%d records):\n",
339 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
341 prev = print_time_record(rec, 0);
343 /* Sort records by increasing time */
344 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
346 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
347 if (rec->id && !rec->start_us)
348 prev = print_time_record(rec, prev);
350 if (data->rec_count > RECORD_COUNT)
351 printf("Overflowed internal boot id table by %d entries\n"
352 "Please increase CONFIG_(SPL_TPL_)BOOTSTAGE_RECORD_COUNT\n",
353 data->rec_count - RECORD_COUNT);
355 puts("\nAccumulated time:\n");
356 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
358 prev = print_time_record(rec, -1);
363 * Append data to a memory buffer
365 * Write data to the buffer if there is space. Whether there is space or not,
366 * the buffer pointer is incremented.
368 * @param ptrp Pointer to buffer, updated by this function
369 * @param end Pointer to end of buffer
370 * @param data Data to write to buffer
371 * @param size Size of data
373 static void append_data(char **ptrp, char *end, const void *data, int size)
381 memcpy(ptr, data, size);
384 int bootstage_stash(void *base, int size)
386 const struct bootstage_data *data = gd->bootstage;
387 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
388 const struct bootstage_record *rec;
390 char *ptr = base, *end = ptr + size;
393 if (hdr + 1 > (struct bootstage_hdr *)end) {
394 debug("%s: Not enough space for bootstage hdr\n", __func__);
398 /* Write an arbitrary version number */
399 hdr->version = BOOTSTAGE_VERSION;
401 hdr->count = data->rec_count;
403 hdr->magic = BOOTSTAGE_MAGIC;
404 hdr->next_id = data->next_id;
407 /* Write the records, silently stopping when we run out of space */
408 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
409 append_data(&ptr, end, rec, sizeof(*rec));
411 /* Write the name strings */
412 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
415 name = get_record_name(buf, sizeof(buf), rec);
416 append_data(&ptr, end, name, strlen(name) + 1);
419 /* Check for buffer overflow */
421 debug("%s: Not enough space for bootstage stash\n", __func__);
425 /* Update total data size */
426 hdr->size = ptr - (char *)base;
427 debug("Stashed %d records\n", hdr->count);
432 int bootstage_unstash(const void *base, int size)
434 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
435 struct bootstage_data *data = gd->bootstage;
436 const char *ptr = base, *end = ptr + size;
437 struct bootstage_record *rec;
442 end = (char *)(~(uintptr_t)0);
444 if (hdr + 1 > (struct bootstage_hdr *)end) {
445 debug("%s: Not enough space for bootstage hdr\n", __func__);
449 if (hdr->magic != BOOTSTAGE_MAGIC) {
450 debug("%s: Invalid bootstage magic\n", __func__);
454 if (ptr + hdr->size > end) {
455 debug("%s: Bootstage data runs past buffer end\n", __func__);
459 if (hdr->count * sizeof(*rec) > hdr->size) {
460 debug("%s: Bootstage has %d records needing %lu bytes, but "
461 "only %d bytes is available\n", __func__, hdr->count,
462 (ulong)hdr->count * sizeof(*rec), hdr->size);
466 if (hdr->version != BOOTSTAGE_VERSION) {
467 debug("%s: Bootstage data version %#0x unrecognised\n",
468 __func__, hdr->version);
472 if (data->rec_count + hdr->count > RECORD_COUNT) {
473 debug("%s: Bootstage has %d records, we have space for %d\n"
474 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
475 __func__, hdr->count, RECORD_COUNT - data->rec_count);
481 /* Read the records */
482 rec_size = hdr->count * sizeof(*data->record);
483 memcpy(data->record + data->rec_count, ptr, rec_size);
485 /* Read the name strings */
487 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
490 if (spl_phase() == PHASE_SPL)
491 rec->name = strdup(ptr);
493 /* Assume no data corruption here */
494 ptr += strlen(ptr) + 1;
497 /* Mark the records as read */
498 data->rec_count += hdr->count;
499 data->next_id = hdr->next_id;
500 debug("Unstashed %d records\n", hdr->count);
505 int bootstage_get_size(void)
507 struct bootstage_data *data = gd->bootstage;
508 struct bootstage_record *rec;
512 size = sizeof(struct bootstage_data);
513 for (rec = data->record, i = 0; i < data->rec_count;
515 size += strlen(rec->name) + 1;
520 int bootstage_init(bool first)
522 struct bootstage_data *data;
523 int size = sizeof(struct bootstage_data);
525 gd->bootstage = (struct bootstage_data *)malloc(size);
528 data = gd->bootstage;
529 memset(data, '\0', size);
531 data->next_id = BOOTSTAGE_ID_USER;
532 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);