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
14 #include <bootstage.h>
20 #include <asm/global_data.h>
21 #include <linux/compiler.h>
22 #include <linux/libfdt.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
30 struct bootstage_record {
34 int flags; /* see enum bootstage_flags */
38 struct bootstage_data {
41 struct bootstage_record record[RECORD_COUNT];
45 BOOTSTAGE_VERSION = 0,
46 BOOTSTAGE_MAGIC = 0xb00757a3,
50 struct bootstage_hdr {
51 u32 version; /* BOOTSTAGE_VERSION */
52 u32 count; /* Number of records */
53 u32 size; /* Total data size (non-zero if valid) */
54 u32 magic; /* Magic number */
55 u32 next_id; /* Next ID to use for bootstage */
58 int bootstage_relocate(void)
60 struct bootstage_data *data = gd->bootstage;
64 /* Figure out where to relocate the strings to */
65 ptr = (char *)(data + 1);
68 * Duplicate all strings. They may point to an old location in the
69 * program .text section that can eventually get trashed.
71 debug("Relocating %d records\n", data->rec_count);
72 for (i = 0; i < data->rec_count; i++) {
73 const char *from = data->record[i].name;
76 data->record[i].name = ptr;
77 ptr += strlen(ptr) + 1;
83 struct bootstage_record *find_id(struct bootstage_data *data,
86 struct bootstage_record *rec;
87 struct bootstage_record *end;
89 for (rec = data->record, end = rec + data->rec_count; rec < end;
98 struct bootstage_record *ensure_id(struct bootstage_data *data,
101 struct bootstage_record *rec;
103 rec = find_id(data, id);
104 if (!rec && data->rec_count < RECORD_COUNT) {
105 rec = &data->record[data->rec_count++];
113 ulong bootstage_add_record(enum bootstage_id id, const char *name,
114 int flags, ulong mark)
116 struct bootstage_data *data = gd->bootstage;
117 struct bootstage_record *rec;
120 * initf_bootstage() is called very early during boot but since hang()
121 * calls bootstage_error() we can be called before bootstage is set up.
122 * Add a check to avoid this.
126 if (flags & BOOTSTAGEF_ALLOC)
127 id = data->next_id++;
129 /* Only record the first event for each */
130 rec = find_id(data, id);
132 if (data->rec_count < RECORD_COUNT) {
133 rec = &data->record[data->rec_count++];
139 log_warning("Bootstage space exhausted\n");
143 /* Tell the board about this progress */
144 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
149 ulong bootstage_error_name(enum bootstage_id id, const char *name)
151 return bootstage_add_record(id, name, BOOTSTAGEF_ERROR,
152 timer_get_boot_us());
155 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
159 if (id == BOOTSTAGE_ID_ALLOC)
160 flags = BOOTSTAGEF_ALLOC;
162 return bootstage_add_record(id, name, flags, timer_get_boot_us());
165 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
168 __maybe_unused char *end;
171 /* First work out the length we need to allocate */
179 str = malloc(len + 1);
183 p += snprintf(p, end - p, "%s,", file);
185 p += snprintf(p, end - p, "%d", linenum);
187 p += snprintf(p, end - p, ": %s", func);
189 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
192 uint32_t bootstage_start(enum bootstage_id id, const char *name)
194 struct bootstage_data *data = gd->bootstage;
195 struct bootstage_record *rec = ensure_id(data, id);
196 ulong start_us = timer_get_boot_us();
199 rec->start_us = start_us;
206 uint32_t bootstage_accum(enum bootstage_id id)
208 struct bootstage_data *data = gd->bootstage;
209 struct bootstage_record *rec = ensure_id(data, id);
214 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
215 rec->time_us += duration;
221 * Get a record name as a printable string
223 * @param buf Buffer to put name if needed
224 * @param len Length of buffer
225 * @param rec Boot stage record to get the name from
226 * Return: pointer to name, either from the record or pointing to buf.
228 static const char *get_record_name(char *buf, int len,
229 const struct bootstage_record *rec)
233 else if (rec->id >= BOOTSTAGE_ID_USER)
234 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
236 snprintf(buf, len, "id=%d", rec->id);
241 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
247 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
249 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
250 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
252 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
257 static int h_compare_record(const void *r1, const void *r2)
259 const struct bootstage_record *rec1 = r1, *rec2 = r2;
261 return rec1->time_us > rec2->time_us ? 1 : -1;
264 #ifdef CONFIG_OF_LIBFDT
266 * Add all bootstage timings to a device tree.
268 * @param blob Device tree blob
269 * Return: 0 on success, != 0 on failure.
271 static int add_bootstages_devicetree(struct fdt_header *blob)
273 struct bootstage_data *data = gd->bootstage;
283 * Create the node for bootstage.
284 * The address of flat device tree is set up by the command bootm.
286 bootstage = fdt_add_subnode(blob, 0, "bootstage");
291 * Insert the timings to the device tree in the reverse order so
292 * that they can be printed in the Linux kernel in the right order.
294 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
295 struct bootstage_record *rec = &data->record[recnum];
298 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
301 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
305 /* add properties to the node. */
306 if (fdt_setprop_string(blob, node, "name",
307 get_record_name(buf, sizeof(buf), rec)))
310 /* Check if this is a 'mark' or 'accum' record */
311 if (fdt_setprop_cell(blob, node,
312 rec->start_us ? "accum" : "mark",
320 int bootstage_fdt_add_report(void)
322 if (add_bootstages_devicetree(working_fdt))
323 puts("bootstage: Failed to add to device tree\n");
329 void bootstage_report(void)
331 struct bootstage_data *data = gd->bootstage;
332 struct bootstage_record *rec = data->record;
336 printf("Timer summary in microseconds (%d records):\n",
338 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
340 prev = print_time_record(rec, 0);
342 /* Sort records by increasing time */
343 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
345 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
346 if (rec->id && !rec->start_us)
347 prev = print_time_record(rec, prev);
349 if (data->rec_count > RECORD_COUNT)
350 printf("Overflowed internal boot id table by %d entries\n"
351 "Please increase CONFIG_(SPL_TPL_)BOOTSTAGE_RECORD_COUNT\n",
352 data->rec_count - RECORD_COUNT);
354 puts("\nAccumulated time:\n");
355 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
357 prev = print_time_record(rec, -1);
362 * Append data to a memory buffer
364 * Write data to the buffer if there is space. Whether there is space or not,
365 * the buffer pointer is incremented.
367 * @param ptrp Pointer to buffer, updated by this function
368 * @param end Pointer to end of buffer
369 * @param data Data to write to buffer
370 * @param size Size of data
372 static void append_data(char **ptrp, char *end, const void *data, int size)
380 memcpy(ptr, data, size);
383 int bootstage_stash(void *base, int size)
385 const struct bootstage_data *data = gd->bootstage;
386 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
387 const struct bootstage_record *rec;
389 char *ptr = base, *end = ptr + size;
392 if (hdr + 1 > (struct bootstage_hdr *)end) {
393 debug("%s: Not enough space for bootstage hdr\n", __func__);
397 /* Write an arbitrary version number */
398 hdr->version = BOOTSTAGE_VERSION;
400 hdr->count = data->rec_count;
402 hdr->magic = BOOTSTAGE_MAGIC;
403 hdr->next_id = data->next_id;
406 /* Write the records, silently stopping when we run out of space */
407 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
408 append_data(&ptr, end, rec, sizeof(*rec));
410 /* Write the name strings */
411 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
414 name = get_record_name(buf, sizeof(buf), rec);
415 append_data(&ptr, end, name, strlen(name) + 1);
418 /* Check for buffer overflow */
420 debug("%s: Not enough space for bootstage stash\n", __func__);
424 /* Update total data size */
425 hdr->size = ptr - (char *)base;
426 debug("Stashed %d records\n", hdr->count);
431 int bootstage_unstash(const void *base, int size)
433 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
434 struct bootstage_data *data = gd->bootstage;
435 const char *ptr = base, *end = ptr + size;
436 struct bootstage_record *rec;
441 end = (char *)(~(uintptr_t)0);
443 if (hdr + 1 > (struct bootstage_hdr *)end) {
444 debug("%s: Not enough space for bootstage hdr\n", __func__);
448 if (hdr->magic != BOOTSTAGE_MAGIC) {
449 debug("%s: Invalid bootstage magic\n", __func__);
453 if (ptr + hdr->size > end) {
454 debug("%s: Bootstage data runs past buffer end\n", __func__);
458 if (hdr->count * sizeof(*rec) > hdr->size) {
459 debug("%s: Bootstage has %d records needing %lu bytes, but "
460 "only %d bytes is available\n", __func__, hdr->count,
461 (ulong)hdr->count * sizeof(*rec), hdr->size);
465 if (hdr->version != BOOTSTAGE_VERSION) {
466 debug("%s: Bootstage data version %#0x unrecognised\n",
467 __func__, hdr->version);
471 if (data->rec_count + hdr->count > RECORD_COUNT) {
472 debug("%s: Bootstage has %d records, we have space for %d\n"
473 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
474 __func__, hdr->count, RECORD_COUNT - data->rec_count);
480 /* Read the records */
481 rec_size = hdr->count * sizeof(*data->record);
482 memcpy(data->record + data->rec_count, ptr, rec_size);
484 /* Read the name strings */
486 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
489 if (spl_phase() == PHASE_SPL)
490 rec->name = strdup(ptr);
492 /* Assume no data corruption here */
493 ptr += strlen(ptr) + 1;
496 /* Mark the records as read */
497 data->rec_count += hdr->count;
498 data->next_id = hdr->next_id;
499 debug("Unstashed %d records\n", hdr->count);
504 int _bootstage_stash_default(void)
506 return bootstage_stash(map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR, 0),
507 CONFIG_BOOTSTAGE_STASH_SIZE);
510 int _bootstage_unstash_default(void)
512 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
513 CONFIG_BOOTSTAGE_STASH_SIZE);
515 return bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
518 int bootstage_get_size(void)
520 struct bootstage_data *data = gd->bootstage;
521 struct bootstage_record *rec;
525 size = sizeof(struct bootstage_data);
526 for (rec = data->record, i = 0; i < data->rec_count;
528 size += strlen(rec->name) + 1;
533 int bootstage_init(bool first)
535 struct bootstage_data *data;
536 int size = sizeof(struct bootstage_data);
538 gd->bootstage = (struct bootstage_data *)malloc(size);
541 data = gd->bootstage;
542 memset(data, '\0', size);
544 data->next_id = BOOTSTAGE_ID_USER;
545 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);