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.
16 #include <linux/compiler.h>
17 #include <linux/libfdt.h>
19 DECLARE_GLOBAL_DATA_PTR;
22 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
25 struct bootstage_record {
29 int flags; /* see enum bootstage_flags */
33 struct bootstage_data {
36 struct bootstage_record record[RECORD_COUNT];
40 BOOTSTAGE_VERSION = 0,
41 BOOTSTAGE_MAGIC = 0xb00757a3,
45 struct bootstage_hdr {
46 u32 version; /* BOOTSTAGE_VERSION */
47 u32 count; /* Number of records */
48 u32 size; /* Total data size (non-zero if valid) */
49 u32 magic; /* Magic number */
50 u32 next_id; /* Next ID to use for bootstage */
53 int bootstage_relocate(void)
55 struct bootstage_data *data = gd->bootstage;
59 /* Figure out where to relocate the strings to */
60 ptr = (char *)(data + 1);
63 * Duplicate all strings. They may point to an old location in the
64 * program .text section that can eventually get trashed.
66 debug("Relocating %d records\n", data->rec_count);
67 for (i = 0; i < data->rec_count; i++) {
68 const char *from = data->record[i].name;
71 data->record[i].name = ptr;
72 ptr += strlen(ptr) + 1;
78 struct bootstage_record *find_id(struct bootstage_data *data,
81 struct bootstage_record *rec;
82 struct bootstage_record *end;
84 for (rec = data->record, end = rec + data->rec_count; rec < end;
93 struct bootstage_record *ensure_id(struct bootstage_data *data,
96 struct bootstage_record *rec;
98 rec = find_id(data, id);
99 if (!rec && data->rec_count < RECORD_COUNT) {
100 rec = &data->record[data->rec_count++];
108 ulong bootstage_add_record(enum bootstage_id id, const char *name,
109 int flags, ulong mark)
111 struct bootstage_data *data = gd->bootstage;
112 struct bootstage_record *rec;
115 * initf_bootstage() is called very early during boot but since hang()
116 * calls bootstage_error() we can be called before bootstage is set up.
117 * Add a check to avoid this.
121 if (flags & BOOTSTAGEF_ALLOC)
122 id = data->next_id++;
124 /* Only record the first event for each */
125 rec = find_id(data, id);
126 if (!rec && data->rec_count < RECORD_COUNT) {
127 rec = &data->record[data->rec_count++];
134 /* Tell the board about this progress */
135 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
141 ulong bootstage_mark(enum bootstage_id id)
143 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
146 ulong bootstage_error(enum bootstage_id id)
148 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
149 timer_get_boot_us());
152 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
156 if (id == BOOTSTAGE_ID_ALLOC)
157 flags = BOOTSTAGEF_ALLOC;
159 return bootstage_add_record(id, name, flags, timer_get_boot_us());
162 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
165 __maybe_unused char *end;
168 /* First work out the length we need to allocate */
176 str = malloc(len + 1);
180 p += snprintf(p, end - p, "%s,", file);
182 p += snprintf(p, end - p, "%d", linenum);
184 p += snprintf(p, end - p, ": %s", func);
186 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
189 uint32_t bootstage_start(enum bootstage_id id, const char *name)
191 struct bootstage_data *data = gd->bootstage;
192 struct bootstage_record *rec = ensure_id(data, id);
193 ulong start_us = timer_get_boot_us();
196 rec->start_us = start_us;
203 uint32_t bootstage_accum(enum bootstage_id id)
205 struct bootstage_data *data = gd->bootstage;
206 struct bootstage_record *rec = ensure_id(data, id);
211 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
212 rec->time_us += duration;
218 * Get a record name as a printable string
220 * @param buf Buffer to put name if needed
221 * @param len Length of buffer
222 * @param rec Boot stage record to get the name from
223 * @return pointer to name, either from the record or pointing to buf.
225 static const char *get_record_name(char *buf, int len,
226 const struct bootstage_record *rec)
230 else if (rec->id >= BOOTSTAGE_ID_USER)
231 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
233 snprintf(buf, len, "id=%d", rec->id);
238 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
244 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
246 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
247 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
249 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
254 static int h_compare_record(const void *r1, const void *r2)
256 const struct bootstage_record *rec1 = r1, *rec2 = r2;
258 return rec1->time_us > rec2->time_us ? 1 : -1;
261 #ifdef CONFIG_OF_LIBFDT
263 * Add all bootstage timings to a device tree.
265 * @param blob Device tree blob
266 * @return 0 on success, != 0 on failure.
268 static int add_bootstages_devicetree(struct fdt_header *blob)
270 struct bootstage_data *data = gd->bootstage;
280 * Create the node for bootstage.
281 * The address of flat device tree is set up by the command bootm.
283 bootstage = fdt_add_subnode(blob, 0, "bootstage");
288 * Insert the timings to the device tree in the reverse order so
289 * that they can be printed in the Linux kernel in the right order.
291 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
292 struct bootstage_record *rec = &data->record[recnum];
295 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
298 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
302 /* add properties to the node. */
303 if (fdt_setprop_string(blob, node, "name",
304 get_record_name(buf, sizeof(buf), rec)))
307 /* Check if this is a 'mark' or 'accum' record */
308 if (fdt_setprop_cell(blob, node,
309 rec->start_us ? "accum" : "mark",
317 int bootstage_fdt_add_report(void)
319 if (add_bootstages_devicetree(working_fdt))
320 puts("bootstage: Failed to add to device tree\n");
326 void bootstage_report(void)
328 struct bootstage_data *data = gd->bootstage;
329 struct bootstage_record *rec = data->record;
333 printf("Timer summary in microseconds (%d records):\n",
335 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
337 prev = print_time_record(rec, 0);
339 /* Sort records by increasing time */
340 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
342 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
343 if (rec->id && !rec->start_us)
344 prev = print_time_record(rec, prev);
346 if (data->rec_count > RECORD_COUNT)
347 printf("Overflowed internal boot id table by %d entries\n"
348 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
349 data->rec_count - RECORD_COUNT);
351 puts("\nAccumulated time:\n");
352 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
354 prev = print_time_record(rec, -1);
359 * Append data to a memory buffer
361 * Write data to the buffer if there is space. Whether there is space or not,
362 * the buffer pointer is incremented.
364 * @param ptrp Pointer to buffer, updated by this function
365 * @param end Pointer to end of buffer
366 * @param data Data to write to buffer
367 * @param size Size of data
369 static void append_data(char **ptrp, char *end, const void *data, int size)
377 memcpy(ptr, data, size);
380 int bootstage_stash(void *base, int size)
382 const struct bootstage_data *data = gd->bootstage;
383 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
384 const struct bootstage_record *rec;
386 char *ptr = base, *end = ptr + size;
389 if (hdr + 1 > (struct bootstage_hdr *)end) {
390 debug("%s: Not enough space for bootstage hdr\n", __func__);
394 /* Write an arbitrary version number */
395 hdr->version = BOOTSTAGE_VERSION;
397 hdr->count = data->rec_count;
399 hdr->magic = BOOTSTAGE_MAGIC;
400 hdr->next_id = data->next_id;
403 /* Write the records, silently stopping when we run out of space */
404 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
405 append_data(&ptr, end, rec, sizeof(*rec));
407 /* Write the name strings */
408 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
411 name = get_record_name(buf, sizeof(buf), rec);
412 append_data(&ptr, end, name, strlen(name) + 1);
415 /* Check for buffer overflow */
417 debug("%s: Not enough space for bootstage stash\n", __func__);
421 /* Update total data size */
422 hdr->size = ptr - (char *)base;
423 debug("Stashed %d records\n", hdr->count);
428 int bootstage_unstash(const void *base, int size)
430 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
431 struct bootstage_data *data = gd->bootstage;
432 const char *ptr = base, *end = ptr + size;
433 struct bootstage_record *rec;
438 end = (char *)(~(uintptr_t)0);
440 if (hdr + 1 > (struct bootstage_hdr *)end) {
441 debug("%s: Not enough space for bootstage hdr\n", __func__);
445 if (hdr->magic != BOOTSTAGE_MAGIC) {
446 debug("%s: Invalid bootstage magic\n", __func__);
450 if (ptr + hdr->size > end) {
451 debug("%s: Bootstage data runs past buffer end\n", __func__);
455 if (hdr->count * sizeof(*rec) > hdr->size) {
456 debug("%s: Bootstage has %d records needing %lu bytes, but "
457 "only %d bytes is available\n", __func__, hdr->count,
458 (ulong)hdr->count * sizeof(*rec), hdr->size);
462 if (hdr->version != BOOTSTAGE_VERSION) {
463 debug("%s: Bootstage data version %#0x unrecognised\n",
464 __func__, hdr->version);
468 if (data->rec_count + hdr->count > RECORD_COUNT) {
469 debug("%s: Bootstage has %d records, we have space for %d\n"
470 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
471 __func__, hdr->count, RECORD_COUNT - data->rec_count);
477 /* Read the records */
478 rec_size = hdr->count * sizeof(*data->record);
479 memcpy(data->record + data->rec_count, ptr, rec_size);
481 /* Read the name strings */
483 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
486 if (spl_phase() == PHASE_SPL)
487 rec->name = strdup(ptr);
489 /* Assume no data corruption here */
490 ptr += strlen(ptr) + 1;
493 /* Mark the records as read */
494 data->rec_count += hdr->count;
495 data->next_id = hdr->next_id;
496 debug("Unstashed %d records\n", hdr->count);
501 int bootstage_get_size(void)
503 struct bootstage_data *data = gd->bootstage;
504 struct bootstage_record *rec;
508 size = sizeof(struct bootstage_data);
509 for (rec = data->record, i = 0; i < data->rec_count;
511 size += strlen(rec->name) + 1;
516 int bootstage_init(bool first)
518 struct bootstage_data *data;
519 int size = sizeof(struct bootstage_data);
521 gd->bootstage = (struct bootstage_data *)malloc(size);
524 data = gd->bootstage;
525 memset(data, '\0', size);
527 data->next_id = BOOTSTAGE_ID_USER;
528 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);