2 * Copyright (c) 2011, Google Inc. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0+
9 * This module records the progress of boot and arbitrary commands, and
10 * permits accurate timestamping of each.
16 #include <linux/compiler.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 uint32_t version; /* BOOTSTAGE_VERSION */
46 uint32_t count; /* Number of records */
47 uint32_t size; /* Total data size (non-zero if valid) */
48 uint32_t magic; /* Unused */
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;
103 if (flags & BOOTSTAGEF_ALLOC)
104 id = data->next_id++;
106 /* Only record the first event for each */
107 rec = find_id(data, id);
108 if (!rec && data->rec_count < RECORD_COUNT) {
109 rec = &data->record[data->rec_count++];
116 /* Tell the board about this progress */
117 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
123 ulong bootstage_mark(enum bootstage_id id)
125 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
128 ulong bootstage_error(enum bootstage_id id)
130 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
131 timer_get_boot_us());
134 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
138 if (id == BOOTSTAGE_ID_ALLOC)
139 flags = BOOTSTAGEF_ALLOC;
141 return bootstage_add_record(id, name, flags, timer_get_boot_us());
144 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
147 __maybe_unused char *end;
150 /* First work out the length we need to allocate */
158 str = malloc(len + 1);
162 p += snprintf(p, end - p, "%s,", file);
164 p += snprintf(p, end - p, "%d", linenum);
166 p += snprintf(p, end - p, ": %s", func);
168 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
171 uint32_t bootstage_start(enum bootstage_id id, const char *name)
173 struct bootstage_data *data = gd->bootstage;
174 struct bootstage_record *rec = ensure_id(data, id);
175 ulong start_us = timer_get_boot_us();
178 rec->start_us = start_us;
185 uint32_t bootstage_accum(enum bootstage_id id)
187 struct bootstage_data *data = gd->bootstage;
188 struct bootstage_record *rec = ensure_id(data, id);
193 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
194 rec->time_us += duration;
200 * Get a record name as a printable string
202 * @param buf Buffer to put name if needed
203 * @param len Length of buffer
204 * @param rec Boot stage record to get the name from
205 * @return pointer to name, either from the record or pointing to buf.
207 static const char *get_record_name(char *buf, int len,
208 const struct bootstage_record *rec)
212 else if (rec->id >= BOOTSTAGE_ID_USER)
213 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
215 snprintf(buf, len, "id=%d", rec->id);
220 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
226 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
228 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
229 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
231 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
236 static int h_compare_record(const void *r1, const void *r2)
238 const struct bootstage_record *rec1 = r1, *rec2 = r2;
240 return rec1->time_us > rec2->time_us ? 1 : -1;
243 #ifdef CONFIG_OF_LIBFDT
245 * Add all bootstage timings to a device tree.
247 * @param blob Device tree blob
248 * @return 0 on success, != 0 on failure.
250 static int add_bootstages_devicetree(struct fdt_header *blob)
252 struct bootstage_data *data = gd->bootstage;
262 * Create the node for bootstage.
263 * The address of flat device tree is set up by the command bootm.
265 bootstage = fdt_add_subnode(blob, 0, "bootstage");
270 * Insert the timings to the device tree in the reverse order so
271 * that they can be printed in the Linux kernel in the right order.
273 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
274 struct bootstage_record *rec = &data->record[recnum];
277 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
280 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
284 /* add properties to the node. */
285 if (fdt_setprop_string(blob, node, "name",
286 get_record_name(buf, sizeof(buf), rec)))
289 /* Check if this is a 'mark' or 'accum' record */
290 if (fdt_setprop_cell(blob, node,
291 rec->start_us ? "accum" : "mark",
299 int bootstage_fdt_add_report(void)
301 if (add_bootstages_devicetree(working_fdt))
302 puts("bootstage: Failed to add to device tree\n");
308 void bootstage_report(void)
310 struct bootstage_data *data = gd->bootstage;
311 struct bootstage_record *rec = data->record;
315 printf("Timer summary in microseconds (%d records):\n",
317 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
319 prev = print_time_record(rec, 0);
321 /* Sort records by increasing time */
322 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
324 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
325 if (rec->id && !rec->start_us)
326 prev = print_time_record(rec, prev);
328 if (data->rec_count > RECORD_COUNT)
329 printf("Overflowed internal boot id table by %d entries\n"
330 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
331 data->rec_count - RECORD_COUNT);
333 puts("\nAccumulated time:\n");
334 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
336 prev = print_time_record(rec, -1);
341 * Append data to a memory buffer
343 * Write data to the buffer if there is space. Whether there is space or not,
344 * the buffer pointer is incremented.
346 * @param ptrp Pointer to buffer, updated by this function
347 * @param end Pointer to end of buffer
348 * @param data Data to write to buffer
349 * @param size Size of data
351 static void append_data(char **ptrp, char *end, const void *data, int size)
359 memcpy(ptr, data, size);
362 int bootstage_stash(void *base, int size)
364 const struct bootstage_data *data = gd->bootstage;
365 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
366 const struct bootstage_record *rec;
368 char *ptr = base, *end = ptr + size;
372 if (hdr + 1 > (struct bootstage_hdr *)end) {
373 debug("%s: Not enough space for bootstage hdr\n", __func__);
377 /* Write an arbitrary version number */
378 hdr->version = BOOTSTAGE_VERSION;
380 /* Count the number of records, and write that value first */
381 for (rec = data->record, i = count = 0; i < data->rec_count;
388 hdr->magic = BOOTSTAGE_MAGIC;
391 /* Write the records, silently stopping when we run out of space */
392 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
393 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 debug("Unstashed %d records\n", hdr->count);
487 int bootstage_get_size(void)
489 return sizeof(struct bootstage_data);
492 int bootstage_init(bool first)
494 struct bootstage_data *data;
495 int size = sizeof(struct bootstage_data);
497 gd->bootstage = (struct bootstage_data *)malloc(size);
500 data = gd->bootstage;
501 memset(data, '\0', size);
503 data->next_id = BOOTSTAGE_ID_USER;
504 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);