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