]> git.ipfire.org Git - thirdparty/u-boot.git/blame - common/bootstage.c
Merge tag 'u-boot-rockchip-20200531' of https://gitlab.denx.de/u-boot/custodians...
[thirdparty/u-boot.git] / common / bootstage.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3a608ca0
SG
2/*
3 * Copyright (c) 2011, Google Inc. All rights reserved.
3a608ca0
SG
4 */
5
6
7/*
8 * This module records the progress of boot and arbitrary commands, and
9 * permits accurate timestamping of each.
3a608ca0
SG
10 */
11
12#include <common.h>
52f24238 13#include <bootstage.h>
db41d65a 14#include <hang.h>
f7ae49fc 15#include <log.h>
fb7db41c 16#include <malloc.h>
8bef79bf 17#include <sort.h>
65b2d96f 18#include <spl.h>
fb7db41c 19#include <linux/compiler.h>
65b2d96f 20#include <linux/libfdt.h>
3a608ca0
SG
21
22DECLARE_GLOBAL_DATA_PTR;
23
03ecac31 24enum {
d69bb0ec 25 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
03ecac31
SG
26};
27
3a608ca0
SG
28struct bootstage_record {
29 ulong time_us;
094e06a5 30 uint32_t start_us;
3a608ca0
SG
31 const char *name;
32 int flags; /* see enum bootstage_flags */
33 enum bootstage_id id;
34};
35
b383d6c0 36struct bootstage_data {
03ecac31 37 uint rec_count;
b383d6c0 38 uint next_id;
03ecac31 39 struct bootstage_record record[RECORD_COUNT];
b383d6c0 40};
3a608ca0 41
fcf509b8
SG
42enum {
43 BOOTSTAGE_VERSION = 0,
44 BOOTSTAGE_MAGIC = 0xb00757a3,
b8bcaa3a 45 BOOTSTAGE_DIGITS = 9,
fcf509b8
SG
46};
47
48struct bootstage_hdr {
53a4f253
SG
49 u32 version; /* BOOTSTAGE_VERSION */
50 u32 count; /* Number of records */
51 u32 size; /* Total data size (non-zero if valid) */
52 u32 magic; /* Magic number */
53 u32 next_id; /* Next ID to use for bootstage */
fcf509b8
SG
54};
55
150678a5
DA
56int bootstage_relocate(void)
57{
b383d6c0 58 struct bootstage_data *data = gd->bootstage;
150678a5 59 int i;
ac9cd480
SG
60 char *ptr;
61
62 /* Figure out where to relocate the strings to */
63 ptr = (char *)(data + 1);
150678a5
DA
64
65 /*
66 * Duplicate all strings. They may point to an old location in the
67 * program .text section that can eventually get trashed.
68 */
03ecac31 69 debug("Relocating %d records\n", data->rec_count);
ac9cd480
SG
70 for (i = 0; i < data->rec_count; i++) {
71 const char *from = data->record[i].name;
72
73 strcpy(ptr, from);
74 data->record[i].name = ptr;
75 ptr += strlen(ptr) + 1;
76 }
150678a5
DA
77
78 return 0;
79}
80
03ecac31
SG
81struct bootstage_record *find_id(struct bootstage_data *data,
82 enum bootstage_id id)
83{
84 struct bootstage_record *rec;
85 struct bootstage_record *end;
86
87 for (rec = data->record, end = rec + data->rec_count; rec < end;
88 rec++) {
89 if (rec->id == id)
90 return rec;
91 }
92
93 return NULL;
94}
95
96struct bootstage_record *ensure_id(struct bootstage_data *data,
97 enum bootstage_id id)
98{
99 struct bootstage_record *rec;
100
101 rec = find_id(data, id);
102 if (!rec && data->rec_count < RECORD_COUNT) {
103 rec = &data->record[data->rec_count++];
104 rec->id = id;
105 return rec;
106 }
107
108 return rec;
109}
110
3a608ca0 111ulong bootstage_add_record(enum bootstage_id id, const char *name,
094e06a5 112 int flags, ulong mark)
3a608ca0 113{
b383d6c0 114 struct bootstage_data *data = gd->bootstage;
3a608ca0 115 struct bootstage_record *rec;
3a608ca0 116
17357725
SG
117 /*
118 * initf_bootstage() is called very early during boot but since hang()
119 * calls bootstage_error() we can be called before bootstage is set up.
120 * Add a check to avoid this.
121 */
122 if (!data)
123 return mark;
3a608ca0 124 if (flags & BOOTSTAGEF_ALLOC)
b383d6c0 125 id = data->next_id++;
3a608ca0 126
03ecac31
SG
127 /* Only record the first event for each */
128 rec = find_id(data, id);
129 if (!rec && data->rec_count < RECORD_COUNT) {
130 rec = &data->record[data->rec_count++];
131 rec->time_us = mark;
132 rec->name = name;
133 rec->flags = flags;
134 rec->id = id;
3a608ca0
SG
135 }
136
137 /* Tell the board about this progress */
138 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
cbcd6970 139
3a608ca0
SG
140 return mark;
141}
142
143
144ulong bootstage_mark(enum bootstage_id id)
145{
094e06a5 146 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
3a608ca0
SG
147}
148
149ulong bootstage_error(enum bootstage_id id)
150{
094e06a5
SG
151 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
152 timer_get_boot_us());
3a608ca0
SG
153}
154
155ulong bootstage_mark_name(enum bootstage_id id, const char *name)
156{
157 int flags = 0;
158
159 if (id == BOOTSTAGE_ID_ALLOC)
160 flags = BOOTSTAGEF_ALLOC;
cbcd6970 161
094e06a5 162 return bootstage_add_record(id, name, flags, timer_get_boot_us());
3a608ca0
SG
163}
164
fb7db41c
SG
165ulong bootstage_mark_code(const char *file, const char *func, int linenum)
166{
167 char *str, *p;
168 __maybe_unused char *end;
169 int len = 0;
170
171 /* First work out the length we need to allocate */
172 if (linenum != -1)
173 len = 11;
174 if (func)
175 len += strlen(func);
176 if (file)
177 len += strlen(file);
178
179 str = malloc(len + 1);
180 p = str;
181 end = p + len;
182 if (file)
183 p += snprintf(p, end - p, "%s,", file);
184 if (linenum != -1)
185 p += snprintf(p, end - p, "%d", linenum);
186 if (func)
187 p += snprintf(p, end - p, ": %s", func);
188
189 return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
190}
191
0e996773
SG
192uint32_t bootstage_start(enum bootstage_id id, const char *name)
193{
b383d6c0 194 struct bootstage_data *data = gd->bootstage;
03ecac31
SG
195 struct bootstage_record *rec = ensure_id(data, id);
196 ulong start_us = timer_get_boot_us();
0e996773 197
03ecac31
SG
198 if (rec) {
199 rec->start_us = start_us;
200 rec->name = name;
201 }
cbcd6970 202
03ecac31 203 return start_us;
0e996773
SG
204}
205
206uint32_t bootstage_accum(enum bootstage_id id)
207{
b383d6c0 208 struct bootstage_data *data = gd->bootstage;
03ecac31 209 struct bootstage_record *rec = ensure_id(data, id);
0e996773
SG
210 uint32_t duration;
211
03ecac31
SG
212 if (!rec)
213 return 0;
0e996773
SG
214 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
215 rec->time_us += duration;
cbcd6970 216
0e996773
SG
217 return duration;
218}
219
94fd1316
SG
220/**
221 * Get a record name as a printable string
222 *
223 * @param buf Buffer to put name if needed
224 * @param len Length of buffer
225 * @param rec Boot stage record to get the name from
226 * @return pointer to name, either from the record or pointing to buf.
227 */
228static const char *get_record_name(char *buf, int len,
9d2542d0 229 const struct bootstage_record *rec)
94fd1316
SG
230{
231 if (rec->name)
232 return rec->name;
233 else if (rec->id >= BOOTSTAGE_ID_USER)
234 snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
235 else
236 snprintf(buf, len, "id=%d", rec->id);
237
238 return buf;
239}
240
b383d6c0 241static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
3a608ca0 242{
94fd1316
SG
243 char buf[20];
244
0e996773
SG
245 if (prev == -1U) {
246 printf("%11s", "");
b8bcaa3a 247 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
0e996773 248 } else {
b8bcaa3a
SG
249 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
250 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
0e996773 251 }
94fd1316
SG
252 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
253
3a608ca0
SG
254 return rec->time_us;
255}
256
257static int h_compare_record(const void *r1, const void *r2)
258{
259 const struct bootstage_record *rec1 = r1, *rec2 = r2;
260
261 return rec1->time_us > rec2->time_us ? 1 : -1;
262}
263
94fd1316
SG
264#ifdef CONFIG_OF_LIBFDT
265/**
266 * Add all bootstage timings to a device tree.
267 *
268 * @param blob Device tree blob
269 * @return 0 on success, != 0 on failure.
270 */
271static int add_bootstages_devicetree(struct fdt_header *blob)
272{
b383d6c0 273 struct bootstage_data *data = gd->bootstage;
94fd1316
SG
274 int bootstage;
275 char buf[20];
03ecac31 276 int recnum;
94fd1316
SG
277 int i;
278
279 if (!blob)
280 return 0;
281
282 /*
283 * Create the node for bootstage.
284 * The address of flat device tree is set up by the command bootm.
285 */
286 bootstage = fdt_add_subnode(blob, 0, "bootstage");
287 if (bootstage < 0)
e003310a 288 return -EINVAL;
94fd1316
SG
289
290 /*
291 * Insert the timings to the device tree in the reverse order so
292 * that they can be printed in the Linux kernel in the right order.
293 */
03ecac31
SG
294 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
295 struct bootstage_record *rec = &data->record[recnum];
94fd1316
SG
296 int node;
297
03ecac31 298 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
94fd1316
SG
299 continue;
300
301 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
302 if (node < 0)
303 break;
304
305 /* add properties to the node. */
306 if (fdt_setprop_string(blob, node, "name",
03ecac31 307 get_record_name(buf, sizeof(buf), rec)))
e003310a 308 return -EINVAL;
94fd1316
SG
309
310 /* Check if this is a 'mark' or 'accum' record */
311 if (fdt_setprop_cell(blob, node,
312 rec->start_us ? "accum" : "mark",
313 rec->time_us))
e003310a 314 return -EINVAL;
94fd1316
SG
315 }
316
317 return 0;
318}
319
320int bootstage_fdt_add_report(void)
321{
322 if (add_bootstages_devicetree(working_fdt))
323 puts("bootstage: Failed to add to device tree\n");
324
325 return 0;
326}
327#endif
328
3a608ca0
SG
329void bootstage_report(void)
330{
b383d6c0
SG
331 struct bootstage_data *data = gd->bootstage;
332 struct bootstage_record *rec = data->record;
3a608ca0 333 uint32_t prev;
03ecac31 334 int i;
3a608ca0 335
03ecac31
SG
336 printf("Timer summary in microseconds (%d records):\n",
337 data->rec_count);
3a608ca0
SG
338 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
339
b383d6c0 340 prev = print_time_record(rec, 0);
3a608ca0
SG
341
342 /* Sort records by increasing time */
03ecac31 343 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
3a608ca0 344
03ecac31
SG
345 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
346 if (rec->id && !rec->start_us)
b383d6c0 347 prev = print_time_record(rec, prev);
3a608ca0 348 }
03ecac31
SG
349 if (data->rec_count > RECORD_COUNT)
350 printf("Overflowed internal boot id table by %d entries\n"
d69bb0ec 351 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
03ecac31 352 data->rec_count - RECORD_COUNT);
0e996773
SG
353
354 puts("\nAccumulated time:\n");
03ecac31 355 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
0e996773 356 if (rec->start_us)
b383d6c0 357 prev = print_time_record(rec, -1);
0e996773 358 }
3a608ca0 359}
3786980d 360
fcf509b8
SG
361/**
362 * Append data to a memory buffer
363 *
364 * Write data to the buffer if there is space. Whether there is space or not,
365 * the buffer pointer is incremented.
366 *
367 * @param ptrp Pointer to buffer, updated by this function
368 * @param end Pointer to end of buffer
369 * @param data Data to write to buffer
370 * @param size Size of data
371 */
372static void append_data(char **ptrp, char *end, const void *data, int size)
373{
374 char *ptr = *ptrp;
375
376 *ptrp += size;
377 if (*ptrp > end)
378 return;
379
380 memcpy(ptr, data, size);
381}
382
383int bootstage_stash(void *base, int size)
384{
9d2542d0 385 const struct bootstage_data *data = gd->bootstage;
fcf509b8 386 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
9d2542d0 387 const struct bootstage_record *rec;
fcf509b8
SG
388 char buf[20];
389 char *ptr = base, *end = ptr + size;
03ecac31 390 int i;
fcf509b8
SG
391
392 if (hdr + 1 > (struct bootstage_hdr *)end) {
393 debug("%s: Not enough space for bootstage hdr\n", __func__);
e003310a 394 return -ENOSPC;
fcf509b8
SG
395 }
396
397 /* Write an arbitrary version number */
398 hdr->version = BOOTSTAGE_VERSION;
399
ed54bdaf 400 hdr->count = data->rec_count;
fcf509b8
SG
401 hdr->size = 0;
402 hdr->magic = BOOTSTAGE_MAGIC;
53a4f253 403 hdr->next_id = data->next_id;
fcf509b8
SG
404 ptr += sizeof(*hdr);
405
406 /* Write the records, silently stopping when we run out of space */
ed54bdaf 407 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
c91001f6 408 append_data(&ptr, end, rec, sizeof(*rec));
fcf509b8
SG
409
410 /* Write the name strings */
03ecac31 411 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
c91001f6 412 const char *name;
fcf509b8 413
c91001f6
SG
414 name = get_record_name(buf, sizeof(buf), rec);
415 append_data(&ptr, end, name, strlen(name) + 1);
fcf509b8
SG
416 }
417
418 /* Check for buffer overflow */
419 if (ptr > end) {
420 debug("%s: Not enough space for bootstage stash\n", __func__);
e003310a 421 return -ENOSPC;
fcf509b8
SG
422 }
423
424 /* Update total data size */
425 hdr->size = ptr - (char *)base;
ff00226e 426 debug("Stashed %d records\n", hdr->count);
fcf509b8
SG
427
428 return 0;
429}
430
9d2542d0 431int bootstage_unstash(const void *base, int size)
fcf509b8 432{
9d2542d0 433 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
b383d6c0 434 struct bootstage_data *data = gd->bootstage;
9d2542d0 435 const char *ptr = base, *end = ptr + size;
fcf509b8 436 struct bootstage_record *rec;
fcf509b8 437 uint rec_size;
03ecac31 438 int i;
fcf509b8
SG
439
440 if (size == -1)
441 end = (char *)(~(uintptr_t)0);
442
443 if (hdr + 1 > (struct bootstage_hdr *)end) {
444 debug("%s: Not enough space for bootstage hdr\n", __func__);
e003310a 445 return -EPERM;
fcf509b8
SG
446 }
447
448 if (hdr->magic != BOOTSTAGE_MAGIC) {
449 debug("%s: Invalid bootstage magic\n", __func__);
e003310a 450 return -ENOENT;
fcf509b8
SG
451 }
452
453 if (ptr + hdr->size > end) {
454 debug("%s: Bootstage data runs past buffer end\n", __func__);
e003310a 455 return -ENOSPC;
fcf509b8
SG
456 }
457
458 if (hdr->count * sizeof(*rec) > hdr->size) {
5d3bd345 459 debug("%s: Bootstage has %d records needing %lu bytes, but "
fcf509b8 460 "only %d bytes is available\n", __func__, hdr->count,
5d3bd345 461 (ulong)hdr->count * sizeof(*rec), hdr->size);
e003310a 462 return -ENOSPC;
fcf509b8
SG
463 }
464
465 if (hdr->version != BOOTSTAGE_VERSION) {
466 debug("%s: Bootstage data version %#0x unrecognised\n",
467 __func__, hdr->version);
e003310a 468 return -EINVAL;
fcf509b8
SG
469 }
470
03ecac31 471 if (data->rec_count + hdr->count > RECORD_COUNT) {
fcf509b8 472 debug("%s: Bootstage has %d records, we have space for %d\n"
d69bb0ec 473 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
03ecac31 474 __func__, hdr->count, RECORD_COUNT - data->rec_count);
e003310a 475 return -ENOSPC;
fcf509b8
SG
476 }
477
478 ptr += sizeof(*hdr);
479
480 /* Read the records */
b383d6c0 481 rec_size = hdr->count * sizeof(*data->record);
03ecac31 482 memcpy(data->record + data->rec_count, ptr, rec_size);
fcf509b8
SG
483
484 /* Read the name strings */
485 ptr += rec_size;
03ecac31
SG
486 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
487 i++, rec++) {
fcf509b8 488 rec->name = ptr;
65b2d96f
SG
489 if (spl_phase() == PHASE_SPL)
490 rec->name = strdup(ptr);
fcf509b8
SG
491
492 /* Assume no data corruption here */
493 ptr += strlen(ptr) + 1;
494 }
495
496 /* Mark the records as read */
03ecac31 497 data->rec_count += hdr->count;
53a4f253 498 data->next_id = hdr->next_id;
ff00226e 499 debug("Unstashed %d records\n", hdr->count);
fcf509b8
SG
500
501 return 0;
502}
b383d6c0 503
25e7dc6a
SG
504int bootstage_get_size(void)
505{
ac9cd480
SG
506 struct bootstage_data *data = gd->bootstage;
507 struct bootstage_record *rec;
508 int size;
509 int i;
510
511 size = sizeof(struct bootstage_data);
512 for (rec = data->record, i = 0; i < data->rec_count;
513 i++, rec++)
514 size += strlen(rec->name) + 1;
515
516 return size;
25e7dc6a
SG
517}
518
b383d6c0
SG
519int bootstage_init(bool first)
520{
521 struct bootstage_data *data;
522 int size = sizeof(struct bootstage_data);
523
524 gd->bootstage = (struct bootstage_data *)malloc(size);
525 if (!gd->bootstage)
526 return -ENOMEM;
527 data = gd->bootstage;
528 memset(data, '\0', size);
03ecac31
SG
529 if (first) {
530 data->next_id = BOOTSTAGE_ID_USER;
b383d6c0 531 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);
03ecac31 532 }
b383d6c0
SG
533
534 return 0;
535}