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