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