]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/bootstage.c
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.
12 * TBD: Pass timings to kernel in the FDT
18 #include <linux/compiler.h>
20 DECLARE_GLOBAL_DATA_PTR
;
22 struct bootstage_record
{
26 int flags
; /* see enum bootstage_flags */
30 static struct bootstage_record record
[BOOTSTAGE_ID_COUNT
] = { {1} };
31 static int next_id
= BOOTSTAGE_ID_USER
;
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)
51 * Duplicate all strings. They may point to an old location in the
52 * program .text section that can eventually get trashed.
54 for (i
= 0; i
< BOOTSTAGE_ID_COUNT
; i
++)
56 record
[i
].name
= strdup(record
[i
].name
);
61 ulong
bootstage_add_record(enum bootstage_id id
, const char *name
,
62 int flags
, ulong mark
)
64 struct bootstage_record
*rec
;
66 if (flags
& BOOTSTAGEF_ALLOC
)
69 if (id
< BOOTSTAGE_ID_COUNT
) {
72 /* Only record the first event for each */
81 /* Tell the board about this progress */
82 show_boot_progress(flags
& BOOTSTAGEF_ERROR
? -id
: id
);
87 ulong
bootstage_mark(enum bootstage_id id
)
89 return bootstage_add_record(id
, NULL
, 0, timer_get_boot_us());
92 ulong
bootstage_error(enum bootstage_id id
)
94 return bootstage_add_record(id
, NULL
, BOOTSTAGEF_ERROR
,
98 ulong
bootstage_mark_name(enum bootstage_id id
, const char *name
)
102 if (id
== BOOTSTAGE_ID_ALLOC
)
103 flags
= BOOTSTAGEF_ALLOC
;
104 return bootstage_add_record(id
, name
, flags
, timer_get_boot_us());
107 ulong
bootstage_mark_code(const char *file
, const char *func
, int linenum
)
110 __maybe_unused
char *end
;
113 /* First work out the length we need to allocate */
121 str
= malloc(len
+ 1);
125 p
+= snprintf(p
, end
- p
, "%s,", file
);
127 p
+= snprintf(p
, end
- p
, "%d", linenum
);
129 p
+= snprintf(p
, end
- p
, ": %s", func
);
131 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC
, str
);
134 uint32_t bootstage_start(enum bootstage_id id
, const char *name
)
136 struct bootstage_record
*rec
= &record
[id
];
138 rec
->start_us
= timer_get_boot_us();
140 return rec
->start_us
;
143 uint32_t bootstage_accum(enum bootstage_id id
)
145 struct bootstage_record
*rec
= &record
[id
];
148 duration
= (uint32_t)timer_get_boot_us() - rec
->start_us
;
149 rec
->time_us
+= duration
;
154 * Get a record name as a printable string
156 * @param buf Buffer to put name if needed
157 * @param len Length of buffer
158 * @param rec Boot stage record to get the name from
159 * @return pointer to name, either from the record or pointing to buf.
161 static const char *get_record_name(char *buf
, int len
,
162 struct bootstage_record
*rec
)
166 else if (rec
->id
>= BOOTSTAGE_ID_USER
)
167 snprintf(buf
, len
, "user_%d", rec
->id
- BOOTSTAGE_ID_USER
);
169 snprintf(buf
, len
, "id=%d", rec
->id
);
174 static uint32_t print_time_record(enum bootstage_id id
,
175 struct bootstage_record
*rec
, uint32_t prev
)
181 print_grouped_ull(rec
->time_us
, BOOTSTAGE_DIGITS
);
183 print_grouped_ull(rec
->time_us
, BOOTSTAGE_DIGITS
);
184 print_grouped_ull(rec
->time_us
- prev
, BOOTSTAGE_DIGITS
);
186 printf(" %s\n", get_record_name(buf
, sizeof(buf
), rec
));
191 static int h_compare_record(const void *r1
, const void *r2
)
193 const struct bootstage_record
*rec1
= r1
, *rec2
= r2
;
195 return rec1
->time_us
> rec2
->time_us
? 1 : -1;
198 #ifdef CONFIG_OF_LIBFDT
200 * Add all bootstage timings to a device tree.
202 * @param blob Device tree blob
203 * @return 0 on success, != 0 on failure.
205 static int add_bootstages_devicetree(struct fdt_header
*blob
)
216 * Create the node for bootstage.
217 * The address of flat device tree is set up by the command bootm.
219 bootstage
= fdt_add_subnode(blob
, 0, "bootstage");
224 * Insert the timings to the device tree in the reverse order so
225 * that they can be printed in the Linux kernel in the right order.
227 for (id
= BOOTSTAGE_ID_COUNT
- 1, i
= 0; id
>= 0; id
--, i
++) {
228 struct bootstage_record
*rec
= &record
[id
];
231 if (id
!= BOOTSTAGE_ID_AWAKE
&& rec
->time_us
== 0)
234 node
= fdt_add_subnode(blob
, bootstage
, simple_itoa(i
));
238 /* add properties to the node. */
239 if (fdt_setprop_string(blob
, node
, "name",
240 get_record_name(buf
, sizeof(buf
), rec
)))
243 /* Check if this is a 'mark' or 'accum' record */
244 if (fdt_setprop_cell(blob
, node
,
245 rec
->start_us
? "accum" : "mark",
253 int bootstage_fdt_add_report(void)
255 if (add_bootstages_devicetree(working_fdt
))
256 puts("bootstage: Failed to add to device tree\n");
262 void bootstage_report(void)
264 struct bootstage_record
*rec
= record
;
268 puts("Timer summary in microseconds:\n");
269 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
271 /* Fake the first record - we could get it from early boot */
274 prev
= print_time_record(BOOTSTAGE_ID_AWAKE
, rec
, 0);
276 /* Sort records by increasing time */
277 qsort(record
, ARRAY_SIZE(record
), sizeof(*rec
), h_compare_record
);
279 for (id
= 0; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
280 if (rec
->time_us
!= 0 && !rec
->start_us
)
281 prev
= print_time_record(rec
->id
, rec
, prev
);
283 if (next_id
> BOOTSTAGE_ID_COUNT
)
284 printf("(Overflowed internal boot id table by %d entries\n"
285 "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
286 next_id
- BOOTSTAGE_ID_COUNT
);
288 puts("\nAccumulated time:\n");
289 for (id
= 0, rec
= record
; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
291 prev
= print_time_record(id
, rec
, -1);
295 ulong
__timer_get_boot_us(void)
297 static ulong base_time
;
300 * We can't implement this properly. Return 0 on the first call and
301 * larger values after that.
304 return get_timer(base_time
) * 1000;
305 base_time
= get_timer(0);
309 ulong
timer_get_boot_us(void)
310 __attribute__((weak
, alias("__timer_get_boot_us")));
313 * Append data to a memory buffer
315 * Write data to the buffer if there is space. Whether there is space or not,
316 * the buffer pointer is incremented.
318 * @param ptrp Pointer to buffer, updated by this function
319 * @param end Pointer to end of buffer
320 * @param data Data to write to buffer
321 * @param size Size of data
323 static void append_data(char **ptrp
, char *end
, const void *data
, int size
)
331 memcpy(ptr
, data
, size
);
334 int bootstage_stash(void *base
, int size
)
336 struct bootstage_hdr
*hdr
= (struct bootstage_hdr
*)base
;
337 struct bootstage_record
*rec
;
339 char *ptr
= base
, *end
= ptr
+ size
;
343 if (hdr
+ 1 > (struct bootstage_hdr
*)end
) {
344 debug("%s: Not enough space for bootstage hdr\n", __func__
);
348 /* Write an arbitrary version number */
349 hdr
->version
= BOOTSTAGE_VERSION
;
351 /* Count the number of records, and write that value first */
352 for (rec
= record
, id
= count
= 0; id
< BOOTSTAGE_ID_COUNT
;
354 if (rec
->time_us
!= 0)
359 hdr
->magic
= BOOTSTAGE_MAGIC
;
362 /* Write the records, silently stopping when we run out of space */
363 for (rec
= record
, id
= 0; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
364 if (rec
->time_us
!= 0)
365 append_data(&ptr
, end
, rec
, sizeof(*rec
));
368 /* Write the name strings */
369 for (rec
= record
, id
= 0; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
370 if (rec
->time_us
!= 0) {
373 name
= get_record_name(buf
, sizeof(buf
), rec
);
374 append_data(&ptr
, end
, name
, strlen(name
) + 1);
378 /* Check for buffer overflow */
380 debug("%s: Not enough space for bootstage stash\n", __func__
);
384 /* Update total data size */
385 hdr
->size
= ptr
- (char *)base
;
386 printf("Stashed %d records\n", hdr
->count
);
391 int bootstage_unstash(void *base
, int size
)
393 struct bootstage_hdr
*hdr
= (struct bootstage_hdr
*)base
;
394 struct bootstage_record
*rec
;
395 char *ptr
= base
, *end
= ptr
+ size
;
400 end
= (char *)(~(uintptr_t)0);
402 if (hdr
+ 1 > (struct bootstage_hdr
*)end
) {
403 debug("%s: Not enough space for bootstage hdr\n", __func__
);
407 if (hdr
->magic
!= BOOTSTAGE_MAGIC
) {
408 debug("%s: Invalid bootstage magic\n", __func__
);
412 if (ptr
+ hdr
->size
> end
) {
413 debug("%s: Bootstage data runs past buffer end\n", __func__
);
417 if (hdr
->count
* sizeof(*rec
) > hdr
->size
) {
418 debug("%s: Bootstage has %d records needing %lu bytes, but "
419 "only %d bytes is available\n", __func__
, hdr
->count
,
420 (ulong
)hdr
->count
* sizeof(*rec
), hdr
->size
);
424 if (hdr
->version
!= BOOTSTAGE_VERSION
) {
425 debug("%s: Bootstage data version %#0x unrecognised\n",
426 __func__
, hdr
->version
);
430 if (next_id
+ hdr
->count
> BOOTSTAGE_ID_COUNT
) {
431 debug("%s: Bootstage has %d records, we have space for %d\n"
432 "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
433 __func__
, hdr
->count
, BOOTSTAGE_ID_COUNT
- next_id
);
439 /* Read the records */
440 rec_size
= hdr
->count
* sizeof(*record
);
441 memcpy(record
+ next_id
, ptr
, rec_size
);
443 /* Read the name strings */
445 for (rec
= record
+ next_id
, id
= 0; id
< hdr
->count
; id
++, rec
++) {
448 /* Assume no data corruption here */
449 ptr
+= strlen(ptr
) + 1;
452 /* Mark the records as read */
453 next_id
+= hdr
->count
;
454 printf("Unstashed %d records\n", hdr
->count
);