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.
15 #include <linux/compiler.h>
16 #include <linux/libfdt.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
24 struct bootstage_record {
28 int flags; /* see enum bootstage_flags */
32 struct bootstage_data {
35 struct bootstage_record record[RECORD_COUNT];
39 BOOTSTAGE_VERSION = 0,
40 BOOTSTAGE_MAGIC = 0xb00757a3,
44 struct bootstage_hdr {
45 u32 version; /* BOOTSTAGE_VERSION */
46 u32 count; /* Number of records */
47 u32 size; /* Total data size (non-zero if valid) */
48 u32 magic; /* Magic number */
49 u32 next_id; /* Next ID to use for bootstage */
52 int bootstage_relocate(void)
54 struct bootstage_data *data = gd->bootstage;
58 * Duplicate all strings. They may point to an old location in the
59 * program .text section that can eventually get trashed.
61 debug("Relocating %d records\n", data->rec_count);
62 for (i = 0; i < data->rec_count; i++)
63 data->record[i].name = strdup(data->record[i].name);
68 struct bootstage_record *find_id(struct bootstage_data *data,
71 struct bootstage_record *rec;
72 struct bootstage_record *end;
74 for (rec = data->record, end = rec + data->rec_count; rec < end;
83 struct bootstage_record *ensure_id(struct bootstage_data *data,
86 struct bootstage_record *rec;
88 rec = find_id(data, id);
89 if (!rec && data->rec_count < RECORD_COUNT) {
90 rec = &data->record[data->rec_count++];
98 ulong bootstage_add_record(enum bootstage_id id, const char *name,
99 int flags, ulong mark)
101 struct bootstage_data *data = gd->bootstage;
102 struct bootstage_record *rec;
105 * initf_bootstage() is called very early during boot but since hang()
106 * calls bootstage_error() we can be called before bootstage is set up.
107 * Add a check to avoid this.
111 if (flags & BOOTSTAGEF_ALLOC)
112 id = data->next_id++;
114 /* Only record the first event for each */
115 rec = find_id(data, id);
116 if (!rec && data->rec_count < RECORD_COUNT) {
117 rec = &data->record[data->rec_count++];
124 /* Tell the board about this progress */
125 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
131 ulong bootstage_mark(enum bootstage_id id)
133 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
136 ulong bootstage_error(enum bootstage_id id)
138 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
139 timer_get_boot_us());
142 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
146 if (id == BOOTSTAGE_ID_ALLOC)
147 flags = BOOTSTAGEF_ALLOC;
149 return bootstage_add_record(id, name, flags, timer_get_boot_us());
152 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
155 __maybe_unused char *end;
158 /* First work out the length we need to allocate */
166 str = malloc(len + 1);
170 p += snprintf(p, end - p, "%s,", file);
172 p += snprintf(p, end - p, "%d", linenum);
174 p += snprintf(p, end - p, ": %s", func);
176 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
179 uint32_t bootstage_start(enum bootstage_id id, const char *name)
181 struct bootstage_data *data = gd->bootstage;
182 struct bootstage_record *rec = ensure_id(data, id);
183 ulong start_us = timer_get_boot_us();
186 rec->start_us = start_us;
193 uint32_t bootstage_accum(enum bootstage_id id)
195 struct bootstage_data *data = gd->bootstage;
196 struct bootstage_record *rec = ensure_id(data, id);
201 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
202 rec->time_us += duration;
208 * Get a record name as a printable string
210 * @param buf Buffer to put name if needed
211 * @param len Length of buffer
212 * @param rec Boot stage record to get the name from
213 * @return pointer to name, either from the record or pointing to buf.
215 static const char *get_record_name(char *buf, int len,
216 const struct bootstage_record *rec)
220 else if (rec->id >= BOOTSTAGE_ID_USER)
221 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
223 snprintf(buf, len, "id=%d", rec->id);
228 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
234 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
236 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
237 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
239 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
244 static int h_compare_record(const void *r1, const void *r2)
246 const struct bootstage_record *rec1 = r1, *rec2 = r2;
248 return rec1->time_us > rec2->time_us ? 1 : -1;
251 #ifdef CONFIG_OF_LIBFDT
253 * Add all bootstage timings to a device tree.
255 * @param blob Device tree blob
256 * @return 0 on success, != 0 on failure.
258 static int add_bootstages_devicetree(struct fdt_header *blob)
260 struct bootstage_data *data = gd->bootstage;
270 * Create the node for bootstage.
271 * The address of flat device tree is set up by the command bootm.
273 bootstage = fdt_add_subnode(blob, 0, "bootstage");
278 * Insert the timings to the device tree in the reverse order so
279 * that they can be printed in the Linux kernel in the right order.
281 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
282 struct bootstage_record *rec = &data->record[recnum];
285 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
288 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
292 /* add properties to the node. */
293 if (fdt_setprop_string(blob, node, "name",
294 get_record_name(buf, sizeof(buf), rec)))
297 /* Check if this is a 'mark' or 'accum' record */
298 if (fdt_setprop_cell(blob, node,
299 rec->start_us ? "accum" : "mark",
307 int bootstage_fdt_add_report(void)
309 if (add_bootstages_devicetree(working_fdt))
310 puts("bootstage: Failed to add to device tree\n");
316 void bootstage_report(void)
318 struct bootstage_data *data = gd->bootstage;
319 struct bootstage_record *rec = data->record;
323 printf("Timer summary in microseconds (%d records):\n",
325 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
327 prev = print_time_record(rec, 0);
329 /* Sort records by increasing time */
330 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
332 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
333 if (rec->id && !rec->start_us)
334 prev = print_time_record(rec, prev);
336 if (data->rec_count > RECORD_COUNT)
337 printf("Overflowed internal boot id table by %d entries\n"
338 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
339 data->rec_count - RECORD_COUNT);
341 puts("\nAccumulated time:\n");
342 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
344 prev = print_time_record(rec, -1);
349 * Append data to a memory buffer
351 * Write data to the buffer if there is space. Whether there is space or not,
352 * the buffer pointer is incremented.
354 * @param ptrp Pointer to buffer, updated by this function
355 * @param end Pointer to end of buffer
356 * @param data Data to write to buffer
357 * @param size Size of data
359 static void append_data(char **ptrp, char *end, const void *data, int size)
367 memcpy(ptr, data, size);
370 int bootstage_stash(void *base, int size)
372 const struct bootstage_data *data = gd->bootstage;
373 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
374 const struct bootstage_record *rec;
376 char *ptr = base, *end = ptr + size;
379 if (hdr + 1 > (struct bootstage_hdr *)end) {
380 debug("%s: Not enough space for bootstage hdr\n", __func__);
384 /* Write an arbitrary version number */
385 hdr->version = BOOTSTAGE_VERSION;
387 hdr->count = data->rec_count;
389 hdr->magic = BOOTSTAGE_MAGIC;
390 hdr->next_id = data->next_id;
393 /* Write the records, silently stopping when we run out of space */
394 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
395 append_data(&ptr, end, rec, sizeof(*rec));
397 /* Write the name strings */
398 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
401 name = get_record_name(buf, sizeof(buf), rec);
402 append_data(&ptr, end, name, strlen(name) + 1);
405 /* Check for buffer overflow */
407 debug("%s: Not enough space for bootstage stash\n", __func__);
411 /* Update total data size */
412 hdr->size = ptr - (char *)base;
413 debug("Stashed %d records\n", hdr->count);
418 int bootstage_unstash(const void *base, int size)
420 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
421 struct bootstage_data *data = gd->bootstage;
422 const char *ptr = base, *end = ptr + size;
423 struct bootstage_record *rec;
428 end = (char *)(~(uintptr_t)0);
430 if (hdr + 1 > (struct bootstage_hdr *)end) {
431 debug("%s: Not enough space for bootstage hdr\n", __func__);
435 if (hdr->magic != BOOTSTAGE_MAGIC) {
436 debug("%s: Invalid bootstage magic\n", __func__);
440 if (ptr + hdr->size > end) {
441 debug("%s: Bootstage data runs past buffer end\n", __func__);
445 if (hdr->count * sizeof(*rec) > hdr->size) {
446 debug("%s: Bootstage has %d records needing %lu bytes, but "
447 "only %d bytes is available\n", __func__, hdr->count,
448 (ulong)hdr->count * sizeof(*rec), hdr->size);
452 if (hdr->version != BOOTSTAGE_VERSION) {
453 debug("%s: Bootstage data version %#0x unrecognised\n",
454 __func__, hdr->version);
458 if (data->rec_count + hdr->count > RECORD_COUNT) {
459 debug("%s: Bootstage has %d records, we have space for %d\n"
460 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
461 __func__, hdr->count, RECORD_COUNT - data->rec_count);
467 /* Read the records */
468 rec_size = hdr->count * sizeof(*data->record);
469 memcpy(data->record + data->rec_count, ptr, rec_size);
471 /* Read the name strings */
473 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
476 if (spl_phase() == PHASE_SPL)
477 rec->name = strdup(ptr);
479 /* Assume no data corruption here */
480 ptr += strlen(ptr) + 1;
483 /* Mark the records as read */
484 data->rec_count += hdr->count;
485 data->next_id = hdr->next_id;
486 debug("Unstashed %d records\n", hdr->count);
491 int bootstage_get_size(void)
493 return sizeof(struct bootstage_data);
496 int bootstage_init(bool first)
498 struct bootstage_data *data;
499 int size = sizeof(struct bootstage_data);
501 gd->bootstage = (struct bootstage_data *)malloc(size);
504 data = gd->bootstage;
505 memset(data, '\0', size);
507 data->next_id = BOOTSTAGE_ID_USER;
508 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);