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.
13 #include <linux/libfdt.h>
15 #include <linux/compiler.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
23 struct bootstage_record {
27 int flags; /* see enum bootstage_flags */
31 struct bootstage_data {
34 struct bootstage_record record[RECORD_COUNT];
38 BOOTSTAGE_VERSION = 0,
39 BOOTSTAGE_MAGIC = 0xb00757a3,
43 struct bootstage_hdr {
44 u32 version; /* BOOTSTAGE_VERSION */
45 u32 count; /* Number of records */
46 u32 size; /* Total data size (non-zero if valid) */
47 u32 magic; /* Magic number */
48 u32 next_id; /* Next ID to use for bootstage */
51 int bootstage_relocate(void)
53 struct bootstage_data *data = gd->bootstage;
57 * Duplicate all strings. They may point to an old location in the
58 * program .text section that can eventually get trashed.
60 debug("Relocating %d records\n", data->rec_count);
61 for (i = 0; i < data->rec_count; i++)
62 data->record[i].name = strdup(data->record[i].name);
67 struct bootstage_record *find_id(struct bootstage_data *data,
70 struct bootstage_record *rec;
71 struct bootstage_record *end;
73 for (rec = data->record, end = rec + data->rec_count; rec < end;
82 struct bootstage_record *ensure_id(struct bootstage_data *data,
85 struct bootstage_record *rec;
87 rec = find_id(data, id);
88 if (!rec && data->rec_count < RECORD_COUNT) {
89 rec = &data->record[data->rec_count++];
97 ulong bootstage_add_record(enum bootstage_id id, const char *name,
98 int flags, ulong mark)
100 struct bootstage_data *data = gd->bootstage;
101 struct bootstage_record *rec;
104 * initf_bootstage() is called very early during boot but since hang()
105 * calls bootstage_error() we can be called before bootstage is set up.
106 * Add a check to avoid this.
110 if (flags & BOOTSTAGEF_ALLOC)
111 id = data->next_id++;
113 /* Only record the first event for each */
114 rec = find_id(data, id);
115 if (!rec && data->rec_count < RECORD_COUNT) {
116 rec = &data->record[data->rec_count++];
123 /* Tell the board about this progress */
124 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
130 ulong bootstage_mark(enum bootstage_id id)
132 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
135 ulong bootstage_error(enum bootstage_id id)
137 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
138 timer_get_boot_us());
141 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
145 if (id == BOOTSTAGE_ID_ALLOC)
146 flags = BOOTSTAGEF_ALLOC;
148 return bootstage_add_record(id, name, flags, timer_get_boot_us());
151 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
154 __maybe_unused char *end;
157 /* First work out the length we need to allocate */
165 str = malloc(len + 1);
169 p += snprintf(p, end - p, "%s,", file);
171 p += snprintf(p, end - p, "%d", linenum);
173 p += snprintf(p, end - p, ": %s", func);
175 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
178 uint32_t bootstage_start(enum bootstage_id id, const char *name)
180 struct bootstage_data *data = gd->bootstage;
181 struct bootstage_record *rec = ensure_id(data, id);
182 ulong start_us = timer_get_boot_us();
185 rec->start_us = start_us;
192 uint32_t bootstage_accum(enum bootstage_id id)
194 struct bootstage_data *data = gd->bootstage;
195 struct bootstage_record *rec = ensure_id(data, id);
200 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
201 rec->time_us += duration;
207 * Get a record name as a printable string
209 * @param buf Buffer to put name if needed
210 * @param len Length of buffer
211 * @param rec Boot stage record to get the name from
212 * @return pointer to name, either from the record or pointing to buf.
214 static const char *get_record_name(char *buf, int len,
215 const struct bootstage_record *rec)
219 else if (rec->id >= BOOTSTAGE_ID_USER)
220 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
222 snprintf(buf, len, "id=%d", rec->id);
227 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
233 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
235 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
236 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
238 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
243 static int h_compare_record(const void *r1, const void *r2)
245 const struct bootstage_record *rec1 = r1, *rec2 = r2;
247 return rec1->time_us > rec2->time_us ? 1 : -1;
250 #ifdef CONFIG_OF_LIBFDT
252 * Add all bootstage timings to a device tree.
254 * @param blob Device tree blob
255 * @return 0 on success, != 0 on failure.
257 static int add_bootstages_devicetree(struct fdt_header *blob)
259 struct bootstage_data *data = gd->bootstage;
269 * Create the node for bootstage.
270 * The address of flat device tree is set up by the command bootm.
272 bootstage = fdt_add_subnode(blob, 0, "bootstage");
277 * Insert the timings to the device tree in the reverse order so
278 * that they can be printed in the Linux kernel in the right order.
280 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
281 struct bootstage_record *rec = &data->record[recnum];
284 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
287 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
291 /* add properties to the node. */
292 if (fdt_setprop_string(blob, node, "name",
293 get_record_name(buf, sizeof(buf), rec)))
296 /* Check if this is a 'mark' or 'accum' record */
297 if (fdt_setprop_cell(blob, node,
298 rec->start_us ? "accum" : "mark",
306 int bootstage_fdt_add_report(void)
308 if (add_bootstages_devicetree(working_fdt))
309 puts("bootstage: Failed to add to device tree\n");
315 void bootstage_report(void)
317 struct bootstage_data *data = gd->bootstage;
318 struct bootstage_record *rec = data->record;
322 printf("Timer summary in microseconds (%d records):\n",
324 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
326 prev = print_time_record(rec, 0);
328 /* Sort records by increasing time */
329 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
331 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
332 if (rec->id && !rec->start_us)
333 prev = print_time_record(rec, prev);
335 if (data->rec_count > RECORD_COUNT)
336 printf("Overflowed internal boot id table by %d entries\n"
337 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
338 data->rec_count - RECORD_COUNT);
340 puts("\nAccumulated time:\n");
341 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
343 prev = print_time_record(rec, -1);
348 * Append data to a memory buffer
350 * Write data to the buffer if there is space. Whether there is space or not,
351 * the buffer pointer is incremented.
353 * @param ptrp Pointer to buffer, updated by this function
354 * @param end Pointer to end of buffer
355 * @param data Data to write to buffer
356 * @param size Size of data
358 static void append_data(char **ptrp, char *end, const void *data, int size)
366 memcpy(ptr, data, size);
369 int bootstage_stash(void *base, int size)
371 const struct bootstage_data *data = gd->bootstage;
372 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
373 const struct bootstage_record *rec;
375 char *ptr = base, *end = ptr + size;
378 if (hdr + 1 > (struct bootstage_hdr *)end) {
379 debug("%s: Not enough space for bootstage hdr\n", __func__);
383 /* Write an arbitrary version number */
384 hdr->version = BOOTSTAGE_VERSION;
386 hdr->count = data->rec_count;
388 hdr->magic = BOOTSTAGE_MAGIC;
389 hdr->next_id = data->next_id;
392 /* Write the records, silently stopping when we run out of space */
393 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
394 append_data(&ptr, end, rec, sizeof(*rec));
396 /* Write the name strings */
397 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
400 name = get_record_name(buf, sizeof(buf), rec);
401 append_data(&ptr, end, name, strlen(name) + 1);
404 /* Check for buffer overflow */
406 debug("%s: Not enough space for bootstage stash\n", __func__);
410 /* Update total data size */
411 hdr->size = ptr - (char *)base;
412 debug("Stashed %d records\n", hdr->count);
417 int bootstage_unstash(const void *base, int size)
419 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
420 struct bootstage_data *data = gd->bootstage;
421 const char *ptr = base, *end = ptr + size;
422 struct bootstage_record *rec;
427 end = (char *)(~(uintptr_t)0);
429 if (hdr + 1 > (struct bootstage_hdr *)end) {
430 debug("%s: Not enough space for bootstage hdr\n", __func__);
434 if (hdr->magic != BOOTSTAGE_MAGIC) {
435 debug("%s: Invalid bootstage magic\n", __func__);
439 if (ptr + hdr->size > end) {
440 debug("%s: Bootstage data runs past buffer end\n", __func__);
444 if (hdr->count * sizeof(*rec) > hdr->size) {
445 debug("%s: Bootstage has %d records needing %lu bytes, but "
446 "only %d bytes is available\n", __func__, hdr->count,
447 (ulong)hdr->count * sizeof(*rec), hdr->size);
451 if (hdr->version != BOOTSTAGE_VERSION) {
452 debug("%s: Bootstage data version %#0x unrecognised\n",
453 __func__, hdr->version);
457 if (data->rec_count + hdr->count > RECORD_COUNT) {
458 debug("%s: Bootstage has %d records, we have space for %d\n"
459 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
460 __func__, hdr->count, RECORD_COUNT - data->rec_count);
466 /* Read the records */
467 rec_size = hdr->count * sizeof(*data->record);
468 memcpy(data->record + data->rec_count, ptr, rec_size);
470 /* Read the name strings */
472 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
476 /* Assume no data corruption here */
477 ptr += strlen(ptr) + 1;
480 /* Mark the records as read */
481 data->rec_count += hdr->count;
482 data->next_id = hdr->next_id;
483 debug("Unstashed %d records\n", hdr->count);
488 int bootstage_get_size(void)
490 return sizeof(struct bootstage_data);
493 int bootstage_init(bool first)
495 struct bootstage_data *data;
496 int size = sizeof(struct bootstage_data);
498 gd->bootstage = (struct bootstage_data *)malloc(size);
501 data = gd->bootstage;
502 memset(data, '\0', size);
504 data->next_id = BOOTSTAGE_ID_USER;
505 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);