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