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