]> Git Repo - J-u-boot.git/blame - common/bootstage.c
Merge patch series "Fix device removal order for Apple dart iommu"
[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 {
680dff6f
JK
252 if (prev > rec->time_us)
253 prev = 0;
b8bcaa3a
SG
254 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
255 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
0e996773 256 }
94fd1316
SG
257 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
258
3a608ca0
SG
259 return rec->time_us;
260}
261
94fd1316
SG
262#ifdef CONFIG_OF_LIBFDT
263/**
264 * Add all bootstage timings to a device tree.
265 *
266 * @param blob Device tree blob
185f812c 267 * Return: 0 on success, != 0 on failure.
94fd1316
SG
268 */
269static int add_bootstages_devicetree(struct fdt_header *blob)
270{
b383d6c0 271 struct bootstage_data *data = gd->bootstage;
94fd1316
SG
272 int bootstage;
273 char buf[20];
03ecac31 274 int recnum;
94fd1316
SG
275 int i;
276
277 if (!blob)
278 return 0;
279
280 /*
281 * Create the node for bootstage.
282 * The address of flat device tree is set up by the command bootm.
283 */
284 bootstage = fdt_add_subnode(blob, 0, "bootstage");
285 if (bootstage < 0)
e003310a 286 return -EINVAL;
94fd1316
SG
287
288 /*
289 * Insert the timings to the device tree in the reverse order so
290 * that they can be printed in the Linux kernel in the right order.
291 */
03ecac31
SG
292 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
293 struct bootstage_record *rec = &data->record[recnum];
94fd1316
SG
294 int node;
295
03ecac31 296 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
94fd1316
SG
297 continue;
298
299 node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
300 if (node < 0)
301 break;
302
303 /* add properties to the node. */
304 if (fdt_setprop_string(blob, node, "name",
03ecac31 305 get_record_name(buf, sizeof(buf), rec)))
e003310a 306 return -EINVAL;
94fd1316
SG
307
308 /* Check if this is a 'mark' or 'accum' record */
309 if (fdt_setprop_cell(blob, node,
310 rec->start_us ? "accum" : "mark",
311 rec->time_us))
e003310a 312 return -EINVAL;
94fd1316
SG
313 }
314
315 return 0;
316}
317
318int bootstage_fdt_add_report(void)
319{
320 if (add_bootstages_devicetree(working_fdt))
321 puts("bootstage: Failed to add to device tree\n");
322
323 return 0;
324}
325#endif
326
3a608ca0
SG
327void bootstage_report(void)
328{
b383d6c0
SG
329 struct bootstage_data *data = gd->bootstage;
330 struct bootstage_record *rec = data->record;
3a608ca0 331 uint32_t prev;
03ecac31 332 int i;
3a608ca0 333
03ecac31
SG
334 printf("Timer summary in microseconds (%d records):\n",
335 data->rec_count);
3a608ca0
SG
336 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
337
b383d6c0 338 prev = print_time_record(rec, 0);
3a608ca0 339
03ecac31
SG
340 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
341 if (rec->id && !rec->start_us)
b383d6c0 342 prev = print_time_record(rec, prev);
3a608ca0 343 }
03ecac31
SG
344 if (data->rec_count > RECORD_COUNT)
345 printf("Overflowed internal boot id table by %d entries\n"
5c10c8ba 346 "Please increase CONFIG_(PHASE_)BOOTSTAGE_RECORD_COUNT\n",
03ecac31 347 data->rec_count - RECORD_COUNT);
0e996773
SG
348
349 puts("\nAccumulated time:\n");
03ecac31 350 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
0e996773 351 if (rec->start_us)
b383d6c0 352 prev = print_time_record(rec, -1);
0e996773 353 }
3a608ca0 354}
3786980d 355
fcf509b8
SG
356/**
357 * Append data to a memory buffer
358 *
359 * Write data to the buffer if there is space. Whether there is space or not,
360 * the buffer pointer is incremented.
361 *
362 * @param ptrp Pointer to buffer, updated by this function
363 * @param end Pointer to end of buffer
364 * @param data Data to write to buffer
365 * @param size Size of data
366 */
367static void append_data(char **ptrp, char *end, const void *data, int size)
368{
369 char *ptr = *ptrp;
370
371 *ptrp += size;
372 if (*ptrp > end)
373 return;
374
375 memcpy(ptr, data, size);
376}
377
378int bootstage_stash(void *base, int size)
379{
9d2542d0 380 const struct bootstage_data *data = gd->bootstage;
fcf509b8 381 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
9d2542d0 382 const struct bootstage_record *rec;
fcf509b8
SG
383 char buf[20];
384 char *ptr = base, *end = ptr + size;
03ecac31 385 int i;
fcf509b8
SG
386
387 if (hdr + 1 > (struct bootstage_hdr *)end) {
388 debug("%s: Not enough space for bootstage hdr\n", __func__);
e003310a 389 return -ENOSPC;
fcf509b8
SG
390 }
391
392 /* Write an arbitrary version number */
393 hdr->version = BOOTSTAGE_VERSION;
394
ed54bdaf 395 hdr->count = data->rec_count;
fcf509b8
SG
396 hdr->size = 0;
397 hdr->magic = BOOTSTAGE_MAGIC;
53a4f253 398 hdr->next_id = data->next_id;
fcf509b8
SG
399 ptr += sizeof(*hdr);
400
401 /* Write the records, silently stopping when we run out of space */
ed54bdaf 402 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
c91001f6 403 append_data(&ptr, end, rec, sizeof(*rec));
fcf509b8
SG
404
405 /* Write the name strings */
03ecac31 406 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
c91001f6 407 const char *name;
fcf509b8 408
c91001f6
SG
409 name = get_record_name(buf, sizeof(buf), rec);
410 append_data(&ptr, end, name, strlen(name) + 1);
fcf509b8
SG
411 }
412
413 /* Check for buffer overflow */
414 if (ptr > end) {
415 debug("%s: Not enough space for bootstage stash\n", __func__);
e003310a 416 return -ENOSPC;
fcf509b8
SG
417 }
418
419 /* Update total data size */
420 hdr->size = ptr - (char *)base;
ff00226e 421 debug("Stashed %d records\n", hdr->count);
fcf509b8
SG
422
423 return 0;
424}
425
9d2542d0 426int bootstage_unstash(const void *base, int size)
fcf509b8 427{
9d2542d0 428 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
b383d6c0 429 struct bootstage_data *data = gd->bootstage;
9d2542d0 430 const char *ptr = base, *end = ptr + size;
fcf509b8 431 struct bootstage_record *rec;
fcf509b8 432 uint rec_size;
03ecac31 433 int i;
fcf509b8
SG
434
435 if (size == -1)
436 end = (char *)(~(uintptr_t)0);
437
438 if (hdr + 1 > (struct bootstage_hdr *)end) {
439 debug("%s: Not enough space for bootstage hdr\n", __func__);
e003310a 440 return -EPERM;
fcf509b8
SG
441 }
442
443 if (hdr->magic != BOOTSTAGE_MAGIC) {
444 debug("%s: Invalid bootstage magic\n", __func__);
e003310a 445 return -ENOENT;
fcf509b8
SG
446 }
447
448 if (ptr + hdr->size > end) {
449 debug("%s: Bootstage data runs past buffer end\n", __func__);
e003310a 450 return -ENOSPC;
fcf509b8
SG
451 }
452
453 if (hdr->count * sizeof(*rec) > hdr->size) {
5d3bd345 454 debug("%s: Bootstage has %d records needing %lu bytes, but "
fcf509b8 455 "only %d bytes is available\n", __func__, hdr->count,
5d3bd345 456 (ulong)hdr->count * sizeof(*rec), hdr->size);
e003310a 457 return -ENOSPC;
fcf509b8
SG
458 }
459
460 if (hdr->version != BOOTSTAGE_VERSION) {
461 debug("%s: Bootstage data version %#0x unrecognised\n",
462 __func__, hdr->version);
e003310a 463 return -EINVAL;
fcf509b8
SG
464 }
465
03ecac31 466 if (data->rec_count + hdr->count > RECORD_COUNT) {
fcf509b8 467 debug("%s: Bootstage has %d records, we have space for %d\n"
5c10c8ba 468 "Please increase CONFIG_(PHASE_)BOOTSTAGE_RECORD_COUNT\n",
03ecac31 469 __func__, hdr->count, RECORD_COUNT - data->rec_count);
e003310a 470 return -ENOSPC;
fcf509b8
SG
471 }
472
473 ptr += sizeof(*hdr);
474
475 /* Read the records */
b383d6c0 476 rec_size = hdr->count * sizeof(*data->record);
03ecac31 477 memcpy(data->record + data->rec_count, ptr, rec_size);
fcf509b8
SG
478
479 /* Read the name strings */
480 ptr += rec_size;
03ecac31
SG
481 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
482 i++, rec++) {
fcf509b8 483 rec->name = ptr;
456bdb70 484 if (xpl_phase() == PHASE_SPL)
65b2d96f 485 rec->name = strdup(ptr);
fcf509b8
SG
486
487 /* Assume no data corruption here */
488 ptr += strlen(ptr) + 1;
489 }
490
491 /* Mark the records as read */
03ecac31 492 data->rec_count += hdr->count;
53a4f253 493 data->next_id = hdr->next_id;
ff00226e 494 debug("Unstashed %d records\n", hdr->count);
fcf509b8
SG
495
496 return 0;
497}
b383d6c0 498
b81e31a1 499#if IS_ENABLED(CONFIG_BOOTSTAGE_STASH)
17ba5010
SG
500int _bootstage_stash_default(void)
501{
502 return bootstage_stash(map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR, 0),
503 CONFIG_BOOTSTAGE_STASH_SIZE);
504}
505
506int _bootstage_unstash_default(void)
507{
508 const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
509 CONFIG_BOOTSTAGE_STASH_SIZE);
510
511 return bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
512}
b81e31a1 513#endif
17ba5010 514
48008ec7 515int bootstage_get_size(bool add_strings)
25e7dc6a 516{
ac9cd480 517 int size;
ac9cd480
SG
518
519 size = sizeof(struct bootstage_data);
48008ec7
SG
520 if (add_strings) {
521 struct bootstage_data *data = gd->bootstage;
522 struct bootstage_record *rec;
523 int i;
524
525 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
526 size += strlen(rec->name) + 1;
527 }
ac9cd480
SG
528
529 return size;
25e7dc6a
SG
530}
531
b383d6c0
SG
532int bootstage_init(bool first)
533{
534 struct bootstage_data *data;
535 int size = sizeof(struct bootstage_data);
536
537 gd->bootstage = (struct bootstage_data *)malloc(size);
538 if (!gd->bootstage)
539 return -ENOMEM;
540 data = gd->bootstage;
541 memset(data, '\0', size);
03ecac31
SG
542 if (first) {
543 data->next_id = BOOTSTAGE_ID_USER;
b383d6c0 544 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);
03ecac31 545 }
b383d6c0
SG
546
547 return 0;
548}
This page took 0.542452 seconds and 4 git commands to generate.