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;
20 struct bootstage_record {
24 int flags; /* see enum bootstage_flags */
28 struct bootstage_data {
30 struct bootstage_record record[BOOTSTAGE_ID_COUNT];
34 BOOTSTAGE_VERSION = 0,
35 BOOTSTAGE_MAGIC = 0xb00757a3,
39 struct bootstage_hdr {
40 uint32_t version; /* BOOTSTAGE_VERSION */
41 uint32_t count; /* Number of records */
42 uint32_t size; /* Total data size (non-zero if valid) */
43 uint32_t magic; /* Unused */
46 int bootstage_relocate(void)
48 struct bootstage_data *data = gd->bootstage;
52 * Duplicate all strings. They may point to an old location in the
53 * program .text section that can eventually get trashed.
55 for (i = 0; i < BOOTSTAGE_ID_COUNT; i++)
56 if (data->record[i].name)
57 data->record[i].name = strdup(data->record[i].name);
62 ulong bootstage_add_record(enum bootstage_id id, const char *name,
63 int flags, ulong mark)
65 struct bootstage_data *data = gd->bootstage;
66 struct bootstage_record *rec;
68 if (flags & BOOTSTAGEF_ALLOC)
71 if (id < BOOTSTAGE_ID_COUNT) {
72 rec = &data->record[id];
74 /* Only record the first event for each */
83 /* Tell the board about this progress */
84 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
90 ulong bootstage_mark(enum bootstage_id id)
92 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
95 ulong bootstage_error(enum bootstage_id id)
97 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
101 ulong bootstage_mark_name(enum bootstage_id id, const char *name)
105 if (id == BOOTSTAGE_ID_ALLOC)
106 flags = BOOTSTAGEF_ALLOC;
108 return bootstage_add_record(id, name, flags, timer_get_boot_us());
111 ulong bootstage_mark_code(const char *file, const char *func, int linenum)
114 __maybe_unused char *end;
117 /* First work out the length we need to allocate */
125 str = malloc(len + 1);
129 p += snprintf(p, end - p, "%s,", file);
131 p += snprintf(p, end - p, "%d", linenum);
133 p += snprintf(p, end - p, ": %s", func);
135 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
138 uint32_t bootstage_start(enum bootstage_id id, const char *name)
140 struct bootstage_data *data = gd->bootstage;
141 struct bootstage_record *rec = &data->record[id];
143 rec->start_us = timer_get_boot_us();
146 return rec->start_us;
149 uint32_t bootstage_accum(enum bootstage_id id)
151 struct bootstage_data *data = gd->bootstage;
152 struct bootstage_record *rec = &data->record[id];
155 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
156 rec->time_us += duration;
162 * Get a record name as a printable string
164 * @param buf Buffer to put name if needed
165 * @param len Length of buffer
166 * @param rec Boot stage record to get the name from
167 * @return pointer to name, either from the record or pointing to buf.
169 static const char *get_record_name(char *buf, int len,
170 struct bootstage_record *rec)
174 else if (rec->id >= BOOTSTAGE_ID_USER)
175 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
177 snprintf(buf, len, "id=%d", rec->id);
182 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
188 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
190 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
191 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
193 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
198 static int h_compare_record(const void *r1, const void *r2)
200 const struct bootstage_record *rec1 = r1, *rec2 = r2;
202 return rec1->time_us > rec2->time_us ? 1 : -1;
205 #ifdef CONFIG_OF_LIBFDT
207 * Add all bootstage timings to a device tree.
209 * @param blob Device tree blob
210 * @return 0 on success, != 0 on failure.
212 static int add_bootstages_devicetree(struct fdt_header *blob)
214 struct bootstage_data *data = gd->bootstage;
224 * Create the node for bootstage.
225 * The address of flat device tree is set up by the command bootm.
227 bootstage = fdt_add_subnode(blob, 0, "bootstage");
232 * Insert the timings to the device tree in the reverse order so
233 * that they can be printed in the Linux kernel in the right order.
235 for (id = BOOTSTAGE_ID_COUNT - 1, i = 0; id >= 0; id--, i++) {
236 struct bootstage_record *rec = &data->record[id];
239 if (id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
242 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
246 /* add properties to the node. */
247 if (fdt_setprop_string(blob, node, "name",
248 get_record_name(buf, sizeof(buf), rec)))
251 /* Check if this is a 'mark' or 'accum' record */
252 if (fdt_setprop_cell(blob, node,
253 rec->start_us ? "accum" : "mark",
261 int bootstage_fdt_add_report(void)
263 if (add_bootstages_devicetree(working_fdt))
264 puts("bootstage: Failed to add to device tree\n");
270 void bootstage_report(void)
272 struct bootstage_data *data = gd->bootstage;
273 struct bootstage_record *rec = data->record;
277 puts("Timer summary in microseconds:\n");
278 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
280 prev = print_time_record(rec, 0);
282 /* Sort records by increasing time */
283 qsort(data->record, ARRAY_SIZE(data->record), sizeof(*rec),
286 for (id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
287 if (rec->time_us != 0 && !rec->start_us)
288 prev = print_time_record(rec, prev);
290 if (data->next_id > BOOTSTAGE_ID_COUNT)
291 printf("(Overflowed internal boot id table by %d entries\n"
292 "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
293 data->next_id - BOOTSTAGE_ID_COUNT);
295 puts("\nAccumulated time:\n");
296 for (id = 0, rec = data->record; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
298 prev = print_time_record(rec, -1);
303 * Append data to a memory buffer
305 * Write data to the buffer if there is space. Whether there is space or not,
306 * the buffer pointer is incremented.
308 * @param ptrp Pointer to buffer, updated by this function
309 * @param end Pointer to end of buffer
310 * @param data Data to write to buffer
311 * @param size Size of data
313 static void append_data(char **ptrp, char *end, const void *data, int size)
321 memcpy(ptr, data, size);
324 int bootstage_stash(void *base, int size)
326 struct bootstage_data *data = gd->bootstage;
327 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
328 struct bootstage_record *rec;
330 char *ptr = base, *end = ptr + size;
334 if (hdr + 1 > (struct bootstage_hdr *)end) {
335 debug("%s: Not enough space for bootstage hdr\n", __func__);
339 /* Write an arbitrary version number */
340 hdr->version = BOOTSTAGE_VERSION;
342 /* Count the number of records, and write that value first */
343 for (rec = data->record, id = count = 0; id < BOOTSTAGE_ID_COUNT;
345 if (rec->time_us != 0)
350 hdr->magic = BOOTSTAGE_MAGIC;
353 /* Write the records, silently stopping when we run out of space */
354 for (rec = data->record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
355 if (rec->time_us != 0)
356 append_data(&ptr, end, rec, sizeof(*rec));
359 /* Write the name strings */
360 for (rec = data->record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
361 if (rec->time_us != 0) {
364 name = get_record_name(buf, sizeof(buf), rec);
365 append_data(&ptr, end, name, strlen(name) + 1);
369 /* Check for buffer overflow */
371 debug("%s: Not enough space for bootstage stash\n", __func__);
375 /* Update total data size */
376 hdr->size = ptr - (char *)base;
377 printf("Stashed %d records\n", hdr->count);
382 int bootstage_unstash(void *base, int size)
384 struct bootstage_data *data = gd->bootstage;
385 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
386 struct bootstage_record *rec;
387 char *ptr = base, *end = ptr + size;
392 end = (char *)(~(uintptr_t)0);
394 if (hdr + 1 > (struct bootstage_hdr *)end) {
395 debug("%s: Not enough space for bootstage hdr\n", __func__);
399 if (hdr->magic != BOOTSTAGE_MAGIC) {
400 debug("%s: Invalid bootstage magic\n", __func__);
404 if (ptr + hdr->size > end) {
405 debug("%s: Bootstage data runs past buffer end\n", __func__);
409 if (hdr->count * sizeof(*rec) > hdr->size) {
410 debug("%s: Bootstage has %d records needing %lu bytes, but "
411 "only %d bytes is available\n", __func__, hdr->count,
412 (ulong)hdr->count * sizeof(*rec), hdr->size);
416 if (hdr->version != BOOTSTAGE_VERSION) {
417 debug("%s: Bootstage data version %#0x unrecognised\n",
418 __func__, hdr->version);
422 if (data->next_id + hdr->count > BOOTSTAGE_ID_COUNT) {
423 debug("%s: Bootstage has %d records, we have space for %d\n"
424 "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
425 __func__, hdr->count, BOOTSTAGE_ID_COUNT - data->next_id);
431 /* Read the records */
432 rec_size = hdr->count * sizeof(*data->record);
433 memcpy(data->record + data->next_id, ptr, rec_size);
435 /* Read the name strings */
437 for (rec = data->record + data->next_id, id = 0; id < hdr->count;
441 /* Assume no data corruption here */
442 ptr += strlen(ptr) + 1;
445 /* Mark the records as read */
446 data->next_id += hdr->count;
447 printf("Unstashed %d records\n", hdr->count);
452 int bootstage_init(bool first)
454 struct bootstage_data *data;
455 int size = sizeof(struct bootstage_data);
457 gd->bootstage = (struct bootstage_data *)malloc(size);
460 data = gd->bootstage;
461 memset(data, '\0', size);
463 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);