]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/bootstage.c
5026705a86d996ffc4c7a38fcecbb0e8b81b484c
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 struct bootstage_data
{
32 struct bootstage_record record
[BOOTSTAGE_ID_COUNT
];
36 BOOTSTAGE_VERSION
= 0,
37 BOOTSTAGE_MAGIC
= 0xb00757a3,
41 struct bootstage_hdr
{
42 uint32_t version
; /* BOOTSTAGE_VERSION */
43 uint32_t count
; /* Number of records */
44 uint32_t size
; /* Total data size (non-zero if valid) */
45 uint32_t magic
; /* Unused */
48 int bootstage_relocate(void)
50 struct bootstage_data
*data
= gd
->bootstage
;
54 * Duplicate all strings. They may point to an old location in the
55 * program .text section that can eventually get trashed.
57 for (i
= 0; i
< BOOTSTAGE_ID_COUNT
; i
++)
58 if (data
->record
[i
].name
)
59 data
->record
[i
].name
= strdup(data
->record
[i
].name
);
64 ulong
bootstage_add_record(enum bootstage_id id
, const char *name
,
65 int flags
, ulong mark
)
67 struct bootstage_data
*data
= gd
->bootstage
;
68 struct bootstage_record
*rec
;
70 if (flags
& BOOTSTAGEF_ALLOC
)
73 if (id
< BOOTSTAGE_ID_COUNT
) {
74 rec
= &data
->record
[id
];
76 /* Only record the first event for each */
85 /* Tell the board about this progress */
86 show_boot_progress(flags
& BOOTSTAGEF_ERROR
? -id
: id
);
91 ulong
bootstage_mark(enum bootstage_id id
)
93 return bootstage_add_record(id
, NULL
, 0, timer_get_boot_us());
96 ulong
bootstage_error(enum bootstage_id id
)
98 return bootstage_add_record(id
, NULL
, BOOTSTAGEF_ERROR
,
102 ulong
bootstage_mark_name(enum bootstage_id id
, const char *name
)
106 if (id
== BOOTSTAGE_ID_ALLOC
)
107 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();
145 return rec
->start_us
;
148 uint32_t bootstage_accum(enum bootstage_id id
)
150 struct bootstage_data
*data
= gd
->bootstage
;
151 struct bootstage_record
*rec
= &data
->record
[id
];
154 duration
= (uint32_t)timer_get_boot_us() - rec
->start_us
;
155 rec
->time_us
+= duration
;
160 * Get a record name as a printable string
162 * @param buf Buffer to put name if needed
163 * @param len Length of buffer
164 * @param rec Boot stage record to get the name from
165 * @return pointer to name, either from the record or pointing to buf.
167 static const char *get_record_name(char *buf
, int len
,
168 struct bootstage_record
*rec
)
172 else if (rec
->id
>= BOOTSTAGE_ID_USER
)
173 snprintf(buf
, len
, "user_%d", rec
->id
- BOOTSTAGE_ID_USER
);
175 snprintf(buf
, len
, "id=%d", rec
->id
);
180 static uint32_t print_time_record(struct bootstage_record
*rec
, uint32_t prev
)
186 print_grouped_ull(rec
->time_us
, BOOTSTAGE_DIGITS
);
188 print_grouped_ull(rec
->time_us
, BOOTSTAGE_DIGITS
);
189 print_grouped_ull(rec
->time_us
- prev
, BOOTSTAGE_DIGITS
);
191 printf(" %s\n", get_record_name(buf
, sizeof(buf
), rec
));
196 static int h_compare_record(const void *r1
, const void *r2
)
198 const struct bootstage_record
*rec1
= r1
, *rec2
= r2
;
200 return rec1
->time_us
> rec2
->time_us
? 1 : -1;
203 #ifdef CONFIG_OF_LIBFDT
205 * Add all bootstage timings to a device tree.
207 * @param blob Device tree blob
208 * @return 0 on success, != 0 on failure.
210 static int add_bootstages_devicetree(struct fdt_header
*blob
)
212 struct bootstage_data
*data
= gd
->bootstage
;
222 * Create the node for bootstage.
223 * The address of flat device tree is set up by the command bootm.
225 bootstage
= fdt_add_subnode(blob
, 0, "bootstage");
230 * Insert the timings to the device tree in the reverse order so
231 * that they can be printed in the Linux kernel in the right order.
233 for (id
= BOOTSTAGE_ID_COUNT
- 1, i
= 0; id
>= 0; id
--, i
++) {
234 struct bootstage_record
*rec
= &data
->record
[id
];
237 if (id
!= BOOTSTAGE_ID_AWAKE
&& rec
->time_us
== 0)
240 node
= fdt_add_subnode(blob
, bootstage
, simple_itoa(i
));
244 /* add properties to the node. */
245 if (fdt_setprop_string(blob
, node
, "name",
246 get_record_name(buf
, sizeof(buf
), rec
)))
249 /* Check if this is a 'mark' or 'accum' record */
250 if (fdt_setprop_cell(blob
, node
,
251 rec
->start_us
? "accum" : "mark",
259 int bootstage_fdt_add_report(void)
261 if (add_bootstages_devicetree(working_fdt
))
262 puts("bootstage: Failed to add to device tree\n");
268 void bootstage_report(void)
270 struct bootstage_data
*data
= gd
->bootstage
;
271 struct bootstage_record
*rec
= data
->record
;
275 puts("Timer summary in microseconds:\n");
276 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
278 prev
= print_time_record(rec
, 0);
280 /* Sort records by increasing time */
281 qsort(data
->record
, ARRAY_SIZE(data
->record
), sizeof(*rec
),
284 for (id
= 0; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
285 if (rec
->time_us
!= 0 && !rec
->start_us
)
286 prev
= print_time_record(rec
, prev
);
288 if (data
->next_id
> BOOTSTAGE_ID_COUNT
)
289 printf("(Overflowed internal boot id table by %d entries\n"
290 "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
291 data
->next_id
- BOOTSTAGE_ID_COUNT
);
293 puts("\nAccumulated time:\n");
294 for (id
= 0, rec
= data
->record
; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
296 prev
= print_time_record(rec
, -1);
301 * Append data to a memory buffer
303 * Write data to the buffer if there is space. Whether there is space or not,
304 * the buffer pointer is incremented.
306 * @param ptrp Pointer to buffer, updated by this function
307 * @param end Pointer to end of buffer
308 * @param data Data to write to buffer
309 * @param size Size of data
311 static void append_data(char **ptrp
, char *end
, const void *data
, int size
)
319 memcpy(ptr
, data
, size
);
322 int bootstage_stash(void *base
, int size
)
324 struct bootstage_data
*data
= gd
->bootstage
;
325 struct bootstage_hdr
*hdr
= (struct bootstage_hdr
*)base
;
326 struct bootstage_record
*rec
;
328 char *ptr
= base
, *end
= ptr
+ size
;
332 if (hdr
+ 1 > (struct bootstage_hdr
*)end
) {
333 debug("%s: Not enough space for bootstage hdr\n", __func__
);
337 /* Write an arbitrary version number */
338 hdr
->version
= BOOTSTAGE_VERSION
;
340 /* Count the number of records, and write that value first */
341 for (rec
= data
->record
, id
= count
= 0; id
< BOOTSTAGE_ID_COUNT
;
343 if (rec
->time_us
!= 0)
348 hdr
->magic
= BOOTSTAGE_MAGIC
;
351 /* Write the records, silently stopping when we run out of space */
352 for (rec
= data
->record
, id
= 0; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
353 if (rec
->time_us
!= 0)
354 append_data(&ptr
, end
, rec
, sizeof(*rec
));
357 /* Write the name strings */
358 for (rec
= data
->record
, id
= 0; id
< BOOTSTAGE_ID_COUNT
; id
++, rec
++) {
359 if (rec
->time_us
!= 0) {
362 name
= get_record_name(buf
, sizeof(buf
), rec
);
363 append_data(&ptr
, end
, name
, strlen(name
) + 1);
367 /* Check for buffer overflow */
369 debug("%s: Not enough space for bootstage stash\n", __func__
);
373 /* Update total data size */
374 hdr
->size
= ptr
- (char *)base
;
375 printf("Stashed %d records\n", hdr
->count
);
380 int bootstage_unstash(void *base
, int size
)
382 struct bootstage_data
*data
= gd
->bootstage
;
383 struct bootstage_hdr
*hdr
= (struct bootstage_hdr
*)base
;
384 struct bootstage_record
*rec
;
385 char *ptr
= base
, *end
= ptr
+ size
;
390 end
= (char *)(~(uintptr_t)0);
392 if (hdr
+ 1 > (struct bootstage_hdr
*)end
) {
393 debug("%s: Not enough space for bootstage hdr\n", __func__
);
397 if (hdr
->magic
!= BOOTSTAGE_MAGIC
) {
398 debug("%s: Invalid bootstage magic\n", __func__
);
402 if (ptr
+ hdr
->size
> end
) {
403 debug("%s: Bootstage data runs past buffer end\n", __func__
);
407 if (hdr
->count
* sizeof(*rec
) > hdr
->size
) {
408 debug("%s: Bootstage has %d records needing %lu bytes, but "
409 "only %d bytes is available\n", __func__
, hdr
->count
,
410 (ulong
)hdr
->count
* sizeof(*rec
), hdr
->size
);
414 if (hdr
->version
!= BOOTSTAGE_VERSION
) {
415 debug("%s: Bootstage data version %#0x unrecognised\n",
416 __func__
, hdr
->version
);
420 if (data
->next_id
+ hdr
->count
> BOOTSTAGE_ID_COUNT
) {
421 debug("%s: Bootstage has %d records, we have space for %d\n"
422 "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
423 __func__
, hdr
->count
, BOOTSTAGE_ID_COUNT
- data
->next_id
);
429 /* Read the records */
430 rec_size
= hdr
->count
* sizeof(*data
->record
);
431 memcpy(data
->record
+ data
->next_id
, ptr
, rec_size
);
433 /* Read the name strings */
435 for (rec
= data
->record
+ data
->next_id
, id
= 0; id
< hdr
->count
;
439 /* Assume no data corruption here */
440 ptr
+= strlen(ptr
) + 1;
443 /* Mark the records as read */
444 data
->next_id
+= hdr
->count
;
445 printf("Unstashed %d records\n", hdr
->count
);
450 int bootstage_init(bool first
)
452 struct bootstage_data
*data
;
453 int size
= sizeof(struct bootstage_data
);
455 gd
->bootstage
= (struct bootstage_data
*)malloc(size
);
458 data
= gd
->bootstage
;
459 memset(data
, '\0', size
);
461 bootstage_add_record(BOOTSTAGE_ID_AWAKE
, "reset", 0, 0);