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.
17 #include <linux/compiler.h>
18 #include <linux/libfdt.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
26 struct bootstage_record {
30 int flags; /* see enum bootstage_flags */
34 struct bootstage_data {
37 struct bootstage_record record[RECORD_COUNT];
41 BOOTSTAGE_VERSION = 0,
42 BOOTSTAGE_MAGIC = 0xb00757a3,
46 struct bootstage_hdr {
47 u32 version; /* BOOTSTAGE_VERSION */
48 u32 count; /* Number of records */
49 u32 size; /* Total data size (non-zero if valid) */
50 u32 magic; /* Magic number */
51 u32 next_id; /* Next ID to use for bootstage */
54 int bootstage_relocate(void)
56 struct bootstage_data *data = gd->bootstage;
60 /* Figure out where to relocate the strings to */
61 ptr = (char *)(data + 1);
64 * Duplicate all strings. They may point to an old location in the
65 * program .text section that can eventually get trashed.
67 debug("Relocating %d records\n", data->rec_count);
68 for (i = 0; i < data->rec_count; i++) {
69 const char *from = data->record[i].name;
72 data->record[i].name = ptr;
73 ptr += strlen(ptr) + 1;
79 struct bootstage_record *find_id(struct bootstage_data *data,
82 struct bootstage_record *rec;
83 struct bootstage_record *end;
85 for (rec = data->record, end = rec + data->rec_count; rec < end;
94 struct bootstage_record *ensure_id(struct bootstage_data *data,
97 struct bootstage_record *rec;
99 rec = find_id(data, id);
100 if (!rec && data->rec_count < RECORD_COUNT) {
101 rec = &data->record[data->rec_count++];
109 ulong bootstage_add_record(enum bootstage_id id, const char *name,
110 int flags, ulong mark)
112 struct bootstage_data *data = gd->bootstage;
113 struct bootstage_record *rec;
116 * initf_bootstage() is called very early during boot but since hang()
117 * calls bootstage_error() we can be called before bootstage is set up.
118 * Add a check to avoid this.
122 if (flags & BOOTSTAGEF_ALLOC)
123 id = data->next_id++;
125 /* Only record the first event for each */
126 rec = find_id(data, id);
127 if (!rec && data->rec_count < RECORD_COUNT) {
128 rec = &data->record[data->rec_count++];
135 /* Tell the board about this progress */
136 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
142 ulong bootstage_mark(enum bootstage_id id)
144 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
147 ulong bootstage_error(enum bootstage_id id)
149 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
150 timer_get_boot_us());
153 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
157 if (id == BOOTSTAGE_ID_ALLOC)
158 flags = BOOTSTAGEF_ALLOC;
160 return bootstage_add_record(id, name, flags, timer_get_boot_us());
163 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
166 __maybe_unused char *end;
169 /* First work out the length we need to allocate */
177 str = malloc(len + 1);
181 p += snprintf(p, end - p, "%s,", file);
183 p += snprintf(p, end - p, "%d", linenum);
185 p += snprintf(p, end - p, ": %s", func);
187 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
190 uint32_t bootstage_start(enum bootstage_id id, const char *name)
192 struct bootstage_data *data = gd->bootstage;
193 struct bootstage_record *rec = ensure_id(data, id);
194 ulong start_us = timer_get_boot_us();
197 rec->start_us = start_us;
204 uint32_t bootstage_accum(enum bootstage_id id)
206 struct bootstage_data *data = gd->bootstage;
207 struct bootstage_record *rec = ensure_id(data, id);
212 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
213 rec->time_us += duration;
219 * Get a record name as a printable string
221 * @param buf Buffer to put name if needed
222 * @param len Length of buffer
223 * @param rec Boot stage record to get the name from
224 * @return pointer to name, either from the record or pointing to buf.
226 static const char *get_record_name(char *buf, int len,
227 const struct bootstage_record *rec)
231 else if (rec->id >= BOOTSTAGE_ID_USER)
232 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
234 snprintf(buf, len, "id=%d", rec->id);
239 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
245 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
247 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
248 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
250 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
255 static int h_compare_record(const void *r1, const void *r2)
257 const struct bootstage_record *rec1 = r1, *rec2 = r2;
259 return rec1->time_us > rec2->time_us ? 1 : -1;
262 #ifdef CONFIG_OF_LIBFDT
264 * Add all bootstage timings to a device tree.
266 * @param blob Device tree blob
267 * @return 0 on success, != 0 on failure.
269 static int add_bootstages_devicetree(struct fdt_header *blob)
271 struct bootstage_data *data = gd->bootstage;
281 * Create the node for bootstage.
282 * The address of flat device tree is set up by the command bootm.
284 bootstage = fdt_add_subnode(blob, 0, "bootstage");
289 * Insert the timings to the device tree in the reverse order so
290 * that they can be printed in the Linux kernel in the right order.
292 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
293 struct bootstage_record *rec = &data->record[recnum];
296 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
299 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
303 /* add properties to the node. */
304 if (fdt_setprop_string(blob, node, "name",
305 get_record_name(buf, sizeof(buf), rec)))
308 /* Check if this is a 'mark' or 'accum' record */
309 if (fdt_setprop_cell(blob, node,
310 rec->start_us ? "accum" : "mark",
318 int bootstage_fdt_add_report(void)
320 if (add_bootstages_devicetree(working_fdt))
321 puts("bootstage: Failed to add to device tree\n");
327 void bootstage_report(void)
329 struct bootstage_data *data = gd->bootstage;
330 struct bootstage_record *rec = data->record;
334 printf("Timer summary in microseconds (%d records):\n",
336 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
338 prev = print_time_record(rec, 0);
340 /* Sort records by increasing time */
341 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
343 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
344 if (rec->id && !rec->start_us)
345 prev = print_time_record(rec, prev);
347 if (data->rec_count > RECORD_COUNT)
348 printf("Overflowed internal boot id table by %d entries\n"
349 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
350 data->rec_count - RECORD_COUNT);
352 puts("\nAccumulated time:\n");
353 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
355 prev = print_time_record(rec, -1);
360 * Append data to a memory buffer
362 * Write data to the buffer if there is space. Whether there is space or not,
363 * the buffer pointer is incremented.
365 * @param ptrp Pointer to buffer, updated by this function
366 * @param end Pointer to end of buffer
367 * @param data Data to write to buffer
368 * @param size Size of data
370 static void append_data(char **ptrp, char *end, const void *data, int size)
378 memcpy(ptr, data, size);
381 int bootstage_stash(void *base, int size)
383 const struct bootstage_data *data = gd->bootstage;
384 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
385 const struct bootstage_record *rec;
387 char *ptr = base, *end = ptr + size;
390 if (hdr + 1 > (struct bootstage_hdr *)end) {
391 debug("%s: Not enough space for bootstage hdr\n", __func__);
395 /* Write an arbitrary version number */
396 hdr->version = BOOTSTAGE_VERSION;
398 hdr->count = data->rec_count;
400 hdr->magic = BOOTSTAGE_MAGIC;
401 hdr->next_id = data->next_id;
404 /* Write the records, silently stopping when we run out of space */
405 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
406 append_data(&ptr, end, rec, sizeof(*rec));
408 /* Write the name strings */
409 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
412 name = get_record_name(buf, sizeof(buf), rec);
413 append_data(&ptr, end, name, strlen(name) + 1);
416 /* Check for buffer overflow */
418 debug("%s: Not enough space for bootstage stash\n", __func__);
422 /* Update total data size */
423 hdr->size = ptr - (char *)base;
424 debug("Stashed %d records\n", hdr->count);
429 int bootstage_unstash(const void *base, int size)
431 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
432 struct bootstage_data *data = gd->bootstage;
433 const char *ptr = base, *end = ptr + size;
434 struct bootstage_record *rec;
439 end = (char *)(~(uintptr_t)0);
441 if (hdr + 1 > (struct bootstage_hdr *)end) {
442 debug("%s: Not enough space for bootstage hdr\n", __func__);
446 if (hdr->magic != BOOTSTAGE_MAGIC) {
447 debug("%s: Invalid bootstage magic\n", __func__);
451 if (ptr + hdr->size > end) {
452 debug("%s: Bootstage data runs past buffer end\n", __func__);
456 if (hdr->count * sizeof(*rec) > hdr->size) {
457 debug("%s: Bootstage has %d records needing %lu bytes, but "
458 "only %d bytes is available\n", __func__, hdr->count,
459 (ulong)hdr->count * sizeof(*rec), hdr->size);
463 if (hdr->version != BOOTSTAGE_VERSION) {
464 debug("%s: Bootstage data version %#0x unrecognised\n",
465 __func__, hdr->version);
469 if (data->rec_count + hdr->count > RECORD_COUNT) {
470 debug("%s: Bootstage has %d records, we have space for %d\n"
471 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
472 __func__, hdr->count, RECORD_COUNT - data->rec_count);
478 /* Read the records */
479 rec_size = hdr->count * sizeof(*data->record);
480 memcpy(data->record + data->rec_count, ptr, rec_size);
482 /* Read the name strings */
484 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
487 if (spl_phase() == PHASE_SPL)
488 rec->name = strdup(ptr);
490 /* Assume no data corruption here */
491 ptr += strlen(ptr) + 1;
494 /* Mark the records as read */
495 data->rec_count += hdr->count;
496 data->next_id = hdr->next_id;
497 debug("Unstashed %d records\n", hdr->count);
502 int bootstage_get_size(void)
504 struct bootstage_data *data = gd->bootstage;
505 struct bootstage_record *rec;
509 size = sizeof(struct bootstage_data);
510 for (rec = data->record, i = 0; i < data->rec_count;
512 size += strlen(rec->name) + 1;
517 int bootstage_init(bool first)
519 struct bootstage_data *data;
520 int size = sizeof(struct bootstage_data);
522 gd->bootstage = (struct bootstage_data *)malloc(size);
525 data = gd->bootstage;
526 memset(data, '\0', size);
528 data->next_id = BOOTSTAGE_ID_USER;
529 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);