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
;
103 * initf_bootstage() is called very early during boot but since hang()
104 * calls bootstage_error() we can be called before bootstage is set up.
105 * Add a check to avoid this.
109 if (flags
& BOOTSTAGEF_ALLOC
)
110 id
= data
->next_id
++;
112 /* Only record the first event for each */
113 rec
= find_id(data
, id
);
114 if (!rec
&& data
->rec_count
< RECORD_COUNT
) {
115 rec
= &data
->record
[data
->rec_count
++];
122 /* Tell the board about this progress */
123 show_boot_progress(flags
& BOOTSTAGEF_ERROR
? -id
: id
);
129 ulong
bootstage_mark(enum bootstage_id id
)
131 return bootstage_add_record(id
, NULL
, 0, timer_get_boot_us());
134 ulong
bootstage_error(enum bootstage_id id
)
136 return bootstage_add_record(id
, NULL
, BOOTSTAGEF_ERROR
,
137 timer_get_boot_us());
140 ulong
bootstage_mark_name(enum bootstage_id id
, const char *name
)
144 if (id
== BOOTSTAGE_ID_ALLOC
)
145 flags
= BOOTSTAGEF_ALLOC
;
147 return bootstage_add_record(id
, name
, flags
, timer_get_boot_us());
150 ulong
bootstage_mark_code(const char *file
, const char *func
, int linenum
)
153 __maybe_unused
char *end
;
156 /* First work out the length we need to allocate */
164 str
= malloc(len
+ 1);
168 p
+= snprintf(p
, end
- p
, "%s,", file
);
170 p
+= snprintf(p
, end
- p
, "%d", linenum
);
172 p
+= snprintf(p
, end
- p
, ": %s", func
);
174 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC
, str
);
177 uint32_t bootstage_start(enum bootstage_id id
, const char *name
)
179 struct bootstage_data
*data
= gd
->bootstage
;
180 struct bootstage_record
*rec
= ensure_id(data
, id
);
181 ulong start_us
= timer_get_boot_us();
184 rec
->start_us
= start_us
;
191 uint32_t bootstage_accum(enum bootstage_id id
)
193 struct bootstage_data
*data
= gd
->bootstage
;
194 struct bootstage_record
*rec
= ensure_id(data
, id
);
199 duration
= (uint32_t)timer_get_boot_us() - rec
->start_us
;
200 rec
->time_us
+= duration
;
206 * Get a record name as a printable string
208 * @param buf Buffer to put name if needed
209 * @param len Length of buffer
210 * @param rec Boot stage record to get the name from
211 * @return pointer to name, either from the record or pointing to buf.
213 static const char *get_record_name(char *buf
, int len
,
214 const struct bootstage_record
*rec
)
218 else if (rec
->id
>= BOOTSTAGE_ID_USER
)
219 snprintf(buf
, len
, "user_%d", rec
->id
- BOOTSTAGE_ID_USER
);
221 snprintf(buf
, len
, "id=%d", rec
->id
);
226 static uint32_t print_time_record(struct bootstage_record
*rec
, uint32_t prev
)
232 print_grouped_ull(rec
->time_us
, BOOTSTAGE_DIGITS
);
234 print_grouped_ull(rec
->time_us
, BOOTSTAGE_DIGITS
);
235 print_grouped_ull(rec
->time_us
- prev
, BOOTSTAGE_DIGITS
);
237 printf(" %s\n", get_record_name(buf
, sizeof(buf
), rec
));
242 static int h_compare_record(const void *r1
, const void *r2
)
244 const struct bootstage_record
*rec1
= r1
, *rec2
= r2
;
246 return rec1
->time_us
> rec2
->time_us
? 1 : -1;
249 #ifdef CONFIG_OF_LIBFDT
251 * Add all bootstage timings to a device tree.
253 * @param blob Device tree blob
254 * @return 0 on success, != 0 on failure.
256 static int add_bootstages_devicetree(struct fdt_header
*blob
)
258 struct bootstage_data
*data
= gd
->bootstage
;
268 * Create the node for bootstage.
269 * The address of flat device tree is set up by the command bootm.
271 bootstage
= fdt_add_subnode(blob
, 0, "bootstage");
276 * Insert the timings to the device tree in the reverse order so
277 * that they can be printed in the Linux kernel in the right order.
279 for (recnum
= data
->rec_count
- 1, i
= 0; recnum
>= 0; recnum
--, i
++) {
280 struct bootstage_record
*rec
= &data
->record
[recnum
];
283 if (rec
->id
!= BOOTSTAGE_ID_AWAKE
&& rec
->time_us
== 0)
286 node
= fdt_add_subnode(blob
, bootstage
, simple_itoa(i
));
290 /* add properties to the node. */
291 if (fdt_setprop_string(blob
, node
, "name",
292 get_record_name(buf
, sizeof(buf
), rec
)))
295 /* Check if this is a 'mark' or 'accum' record */
296 if (fdt_setprop_cell(blob
, node
,
297 rec
->start_us
? "accum" : "mark",
305 int bootstage_fdt_add_report(void)
307 if (add_bootstages_devicetree(working_fdt
))
308 puts("bootstage: Failed to add to device tree\n");
314 void bootstage_report(void)
316 struct bootstage_data
*data
= gd
->bootstage
;
317 struct bootstage_record
*rec
= data
->record
;
321 printf("Timer summary in microseconds (%d records):\n",
323 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
325 prev
= print_time_record(rec
, 0);
327 /* Sort records by increasing time */
328 qsort(data
->record
, data
->rec_count
, sizeof(*rec
), h_compare_record
);
330 for (i
= 1, rec
++; i
< data
->rec_count
; i
++, rec
++) {
331 if (rec
->id
&& !rec
->start_us
)
332 prev
= print_time_record(rec
, prev
);
334 if (data
->rec_count
> RECORD_COUNT
)
335 printf("Overflowed internal boot id table by %d entries\n"
336 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
337 data
->rec_count
- RECORD_COUNT
);
339 puts("\nAccumulated time:\n");
340 for (i
= 0, rec
= data
->record
; i
< data
->rec_count
; i
++, rec
++) {
342 prev
= print_time_record(rec
, -1);
347 * Append data to a memory buffer
349 * Write data to the buffer if there is space. Whether there is space or not,
350 * the buffer pointer is incremented.
352 * @param ptrp Pointer to buffer, updated by this function
353 * @param end Pointer to end of buffer
354 * @param data Data to write to buffer
355 * @param size Size of data
357 static void append_data(char **ptrp
, char *end
, const void *data
, int size
)
365 memcpy(ptr
, data
, size
);
368 int bootstage_stash(void *base
, int size
)
370 const struct bootstage_data
*data
= gd
->bootstage
;
371 struct bootstage_hdr
*hdr
= (struct bootstage_hdr
*)base
;
372 const struct bootstage_record
*rec
;
374 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 /* Count the number of records, and write that value first */
387 for (rec
= data
->record
, i
= count
= 0; i
< data
->rec_count
;
394 hdr
->magic
= BOOTSTAGE_MAGIC
;
397 /* Write the records, silently stopping when we run out of space */
398 for (rec
= data
->record
, i
= 0; i
< data
->rec_count
; i
++, rec
++) {
399 append_data(&ptr
, end
, rec
, sizeof(*rec
));
402 /* Write the name strings */
403 for (rec
= data
->record
, i
= 0; i
< data
->rec_count
; i
++, rec
++) {
406 name
= get_record_name(buf
, sizeof(buf
), rec
);
407 append_data(&ptr
, end
, name
, strlen(name
) + 1);
410 /* Check for buffer overflow */
412 debug("%s: Not enough space for bootstage stash\n", __func__
);
416 /* Update total data size */
417 hdr
->size
= ptr
- (char *)base
;
418 debug("Stashed %d records\n", hdr
->count
);
423 int bootstage_unstash(const void *base
, int size
)
425 const struct bootstage_hdr
*hdr
= (struct bootstage_hdr
*)base
;
426 struct bootstage_data
*data
= gd
->bootstage
;
427 const char *ptr
= base
, *end
= ptr
+ size
;
428 struct bootstage_record
*rec
;
433 end
= (char *)(~(uintptr_t)0);
435 if (hdr
+ 1 > (struct bootstage_hdr
*)end
) {
436 debug("%s: Not enough space for bootstage hdr\n", __func__
);
440 if (hdr
->magic
!= BOOTSTAGE_MAGIC
) {
441 debug("%s: Invalid bootstage magic\n", __func__
);
445 if (ptr
+ hdr
->size
> end
) {
446 debug("%s: Bootstage data runs past buffer end\n", __func__
);
450 if (hdr
->count
* sizeof(*rec
) > hdr
->size
) {
451 debug("%s: Bootstage has %d records needing %lu bytes, but "
452 "only %d bytes is available\n", __func__
, hdr
->count
,
453 (ulong
)hdr
->count
* sizeof(*rec
), hdr
->size
);
457 if (hdr
->version
!= BOOTSTAGE_VERSION
) {
458 debug("%s: Bootstage data version %#0x unrecognised\n",
459 __func__
, hdr
->version
);
463 if (data
->rec_count
+ hdr
->count
> RECORD_COUNT
) {
464 debug("%s: Bootstage has %d records, we have space for %d\n"
465 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
466 __func__
, hdr
->count
, RECORD_COUNT
- data
->rec_count
);
472 /* Read the records */
473 rec_size
= hdr
->count
* sizeof(*data
->record
);
474 memcpy(data
->record
+ data
->rec_count
, ptr
, rec_size
);
476 /* Read the name strings */
478 for (rec
= data
->record
+ data
->next_id
, i
= 0; i
< hdr
->count
;
482 /* Assume no data corruption here */
483 ptr
+= strlen(ptr
) + 1;
486 /* Mark the records as read */
487 data
->rec_count
+= hdr
->count
;
488 debug("Unstashed %d records\n", hdr
->count
);
493 int bootstage_get_size(void)
495 return sizeof(struct bootstage_data
);
498 int bootstage_init(bool first
)
500 struct bootstage_data
*data
;
501 int size
= sizeof(struct bootstage_data
);
503 gd
->bootstage
= (struct bootstage_data
*)malloc(size
);
506 data
= gd
->bootstage
;
507 memset(data
, '\0', size
);
509 data
->next_id
= BOOTSTAGE_ID_USER
;
510 bootstage_add_record(BOOTSTAGE_ID_AWAKE
, "reset", 0, 0);