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 uint32_t version; /* BOOTSTAGE_VERSION */
45 uint32_t count; /* Number of records */
46 uint32_t size; /* Total data size (non-zero if valid) */
47 uint32_t magic; /* Unused */
50 int bootstage_relocate(void)
52 struct bootstage_data *data = gd->bootstage;
56 * Duplicate all strings. They may point to an old location in the
57 * program .text section that can eventually get trashed.
59 debug("Relocating %d records\n", data->rec_count);
60 for (i = 0; i < data->rec_count; i++)
61 data->record[i].name = strdup(data->record[i].name);
66 struct bootstage_record *find_id(struct bootstage_data *data,
69 struct bootstage_record *rec;
70 struct bootstage_record *end;
72 for (rec = data->record, end = rec + data->rec_count; rec < end;
81 struct bootstage_record *ensure_id(struct bootstage_data *data,
84 struct bootstage_record *rec;
86 rec = find_id(data, id);
87 if (!rec && data->rec_count < RECORD_COUNT) {
88 rec = &data->record[data->rec_count++];
96 ulong bootstage_add_record(enum bootstage_id id, const char *name,
97 int flags, ulong mark)
99 struct bootstage_data *data = gd->bootstage;
100 struct bootstage_record *rec;
102 if (flags & BOOTSTAGEF_ALLOC)
103 id = data->next_id++;
105 /* Only record the first event for each */
106 rec = find_id(data, id);
107 if (!rec && data->rec_count < RECORD_COUNT) {
108 rec = &data->record[data->rec_count++];
115 /* Tell the board about this progress */
116 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
122 ulong bootstage_mark(enum bootstage_id id)
124 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
127 ulong bootstage_error(enum bootstage_id id)
129 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
130 timer_get_boot_us());
133 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
137 if (id == BOOTSTAGE_ID_ALLOC)
138 flags = BOOTSTAGEF_ALLOC;
140 return bootstage_add_record(id, name, flags, timer_get_boot_us());
143 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
146 __maybe_unused char *end;
149 /* First work out the length we need to allocate */
157 str = malloc(len + 1);
161 p += snprintf(p, end - p, "%s,", file);
163 p += snprintf(p, end - p, "%d", linenum);
165 p += snprintf(p, end - p, ": %s", func);
167 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
170 uint32_t bootstage_start(enum bootstage_id id, const char *name)
172 struct bootstage_data *data = gd->bootstage;
173 struct bootstage_record *rec = ensure_id(data, id);
174 ulong start_us = timer_get_boot_us();
177 rec->start_us = start_us;
184 uint32_t bootstage_accum(enum bootstage_id id)
186 struct bootstage_data *data = gd->bootstage;
187 struct bootstage_record *rec = ensure_id(data, id);
192 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
193 rec->time_us += duration;
199 * Get a record name as a printable string
201 * @param buf Buffer to put name if needed
202 * @param len Length of buffer
203 * @param rec Boot stage record to get the name from
204 * @return pointer to name, either from the record or pointing to buf.
206 static const char *get_record_name(char *buf, int len,
207 const struct bootstage_record *rec)
211 else if (rec->id >= BOOTSTAGE_ID_USER)
212 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
214 snprintf(buf, len, "id=%d", rec->id);
219 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
225 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
227 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
228 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
230 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
235 static int h_compare_record(const void *r1, const void *r2)
237 const struct bootstage_record *rec1 = r1, *rec2 = r2;
239 return rec1->time_us > rec2->time_us ? 1 : -1;
242 #ifdef CONFIG_OF_LIBFDT
244 * Add all bootstage timings to a device tree.
246 * @param blob Device tree blob
247 * @return 0 on success, != 0 on failure.
249 static int add_bootstages_devicetree(struct fdt_header *blob)
251 struct bootstage_data *data = gd->bootstage;
261 * Create the node for bootstage.
262 * The address of flat device tree is set up by the command bootm.
264 bootstage = fdt_add_subnode(blob, 0, "bootstage");
269 * Insert the timings to the device tree in the reverse order so
270 * that they can be printed in the Linux kernel in the right order.
272 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
273 struct bootstage_record *rec = &data->record[recnum];
276 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
279 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
283 /* add properties to the node. */
284 if (fdt_setprop_string(blob, node, "name",
285 get_record_name(buf, sizeof(buf), rec)))
288 /* Check if this is a 'mark' or 'accum' record */
289 if (fdt_setprop_cell(blob, node,
290 rec->start_us ? "accum" : "mark",
298 int bootstage_fdt_add_report(void)
300 if (add_bootstages_devicetree(working_fdt))
301 puts("bootstage: Failed to add to device tree\n");
307 void bootstage_report(void)
309 struct bootstage_data *data = gd->bootstage;
310 struct bootstage_record *rec = data->record;
314 printf("Timer summary in microseconds (%d records):\n",
316 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
318 prev = print_time_record(rec, 0);
320 /* Sort records by increasing time */
321 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
323 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
324 if (rec->id && !rec->start_us)
325 prev = print_time_record(rec, prev);
327 if (data->rec_count > RECORD_COUNT)
328 printf("Overflowed internal boot id table by %d entries\n"
329 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
330 data->rec_count - RECORD_COUNT);
332 puts("\nAccumulated time:\n");
333 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
335 prev = print_time_record(rec, -1);
340 * Append data to a memory buffer
342 * Write data to the buffer if there is space. Whether there is space or not,
343 * the buffer pointer is incremented.
345 * @param ptrp Pointer to buffer, updated by this function
346 * @param end Pointer to end of buffer
347 * @param data Data to write to buffer
348 * @param size Size of data
350 static void append_data(char **ptrp, char *end, const void *data, int size)
358 memcpy(ptr, data, size);
361 int bootstage_stash(void *base, int size)
363 const struct bootstage_data *data = gd->bootstage;
364 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
365 const struct bootstage_record *rec;
367 char *ptr = base, *end = ptr + size;
371 if (hdr + 1 > (struct bootstage_hdr *)end) {
372 debug("%s: Not enough space for bootstage hdr\n", __func__);
376 /* Write an arbitrary version number */
377 hdr->version = BOOTSTAGE_VERSION;
379 /* Count the number of records, and write that value first */
380 for (rec = data->record, i = count = 0; i < data->rec_count;
387 hdr->magic = BOOTSTAGE_MAGIC;
390 /* Write the records, silently stopping when we run out of space */
391 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
392 append_data(&ptr, end, rec, sizeof(*rec));
395 /* Write the name strings */
396 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
399 name = get_record_name(buf, sizeof(buf), rec);
400 append_data(&ptr, end, name, strlen(name) + 1);
403 /* Check for buffer overflow */
405 debug("%s: Not enough space for bootstage stash\n", __func__);
409 /* Update total data size */
410 hdr->size = ptr - (char *)base;
411 debug("Stashed %d records\n", hdr->count);
416 int bootstage_unstash(const void *base, int size)
418 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
419 struct bootstage_data *data = gd->bootstage;
420 const char *ptr = base, *end = ptr + size;
421 struct bootstage_record *rec;
426 end = (char *)(~(uintptr_t)0);
428 if (hdr + 1 > (struct bootstage_hdr *)end) {
429 debug("%s: Not enough space for bootstage hdr\n", __func__);
433 if (hdr->magic != BOOTSTAGE_MAGIC) {
434 debug("%s: Invalid bootstage magic\n", __func__);
438 if (ptr + hdr->size > end) {
439 debug("%s: Bootstage data runs past buffer end\n", __func__);
443 if (hdr->count * sizeof(*rec) > hdr->size) {
444 debug("%s: Bootstage has %d records needing %lu bytes, but "
445 "only %d bytes is available\n", __func__, hdr->count,
446 (ulong)hdr->count * sizeof(*rec), hdr->size);
450 if (hdr->version != BOOTSTAGE_VERSION) {
451 debug("%s: Bootstage data version %#0x unrecognised\n",
452 __func__, hdr->version);
456 if (data->rec_count + hdr->count > RECORD_COUNT) {
457 debug("%s: Bootstage has %d records, we have space for %d\n"
458 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
459 __func__, hdr->count, RECORD_COUNT - data->rec_count);
465 /* Read the records */
466 rec_size = hdr->count * sizeof(*data->record);
467 memcpy(data->record + data->rec_count, ptr, rec_size);
469 /* Read the name strings */
471 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
475 /* Assume no data corruption here */
476 ptr += strlen(ptr) + 1;
479 /* Mark the records as read */
480 data->rec_count += hdr->count;
481 debug("Unstashed %d records\n", hdr->count);
486 int bootstage_get_size(void)
488 return sizeof(struct bootstage_data);
491 int bootstage_init(bool first)
493 struct bootstage_data *data;
494 int size = sizeof(struct bootstage_data);
496 gd->bootstage = (struct bootstage_data *)malloc(size);
499 data = gd->bootstage;
500 memset(data, '\0', size);
502 data->next_id = BOOTSTAGE_ID_USER;
503 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);