1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2011, Google Inc. All rights reserved.
4 */
5
6
7 /*
8 * This module records the progress of boot and arbitrary commands, and
9 * permits accurate timestamping of each.
10 */
11
12 #define LOG_CATEGORY LOGC_BOOT
13
14 #include <common.h>
15 #include <bootstage.h>
16 #include <hang.h>
17 #include <log.h>
18 #include <malloc.h>
19 #include <sort.h>
20 #include <spl.h>
21 #include <asm/global_data.h>
22 #include <linux/compiler.h>
23 #include <linux/libfdt.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 enum {
28 RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
29 };
30
31 struct bootstage_record {
32 ulong time_us;
33 uint32_t start_us;
34 const char *name;
35 int flags; /* see enum bootstage_flags */
36 enum bootstage_id id;
37 };
38
39 struct bootstage_data {
40 uint rec_count;
41 uint next_id;
42 struct bootstage_record record[RECORD_COUNT];
43 };
44
45 enum {
46 BOOTSTAGE_VERSION = 0,
47 BOOTSTAGE_MAGIC = 0xb00757a3,
48 BOOTSTAGE_DIGITS = 9,
49 };
50
51 struct bootstage_hdr {
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 */
57 };
58
bootstage_relocate(void)59 int bootstage_relocate(void)
60 {
61 struct bootstage_data *data = gd->bootstage;
62 int i;
63 char *ptr;
64
65 /* Figure out where to relocate the strings to */
66 ptr = (char *)(data + 1);
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 */
72 debug("Relocating %d records\n", data->rec_count);
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 }
80
81 return 0;
82 }
83
find_id(struct bootstage_data * data,enum bootstage_id id)84 struct 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
ensure_id(struct bootstage_data * data,enum bootstage_id id)99 struct 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
bootstage_add_record(enum bootstage_id id,const char * name,int flags,ulong mark)114 ulong bootstage_add_record(enum bootstage_id id, const char *name,
115 int flags, ulong mark)
116 {
117 struct bootstage_data *data = gd->bootstage;
118 struct bootstage_record *rec;
119
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;
127 if (flags & BOOTSTAGEF_ALLOC)
128 id = data->next_id++;
129
130 /* Only record the first event for each */
131 rec = find_id(data, id);
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 }
142 }
143
144 /* Tell the board about this progress */
145 show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
146
147 return mark;
148 }
149
150
bootstage_mark(enum bootstage_id id)151 ulong bootstage_mark(enum bootstage_id id)
152 {
153 return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
154 }
155
bootstage_error(enum bootstage_id id)156 ulong bootstage_error(enum bootstage_id id)
157 {
158 return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
159 timer_get_boot_us());
160 }
161
bootstage_mark_name(enum bootstage_id id,const char * name)162 ulong 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;
168
169 return bootstage_add_record(id, name, flags, timer_get_boot_us());
170 }
171
bootstage_mark_code(const char * file,const char * func,int linenum)172 ulong 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
bootstage_start(enum bootstage_id id,const char * name)199 uint32_t bootstage_start(enum bootstage_id id, const char *name)
200 {
201 struct bootstage_data *data = gd->bootstage;
202 struct bootstage_record *rec = ensure_id(data, id);
203 ulong start_us = timer_get_boot_us();
204
205 if (rec) {
206 rec->start_us = start_us;
207 rec->name = name;
208 }
209
210 return start_us;
211 }
212
bootstage_accum(enum bootstage_id id)213 uint32_t bootstage_accum(enum bootstage_id id)
214 {
215 struct bootstage_data *data = gd->bootstage;
216 struct bootstage_record *rec = ensure_id(data, id);
217 uint32_t duration;
218
219 if (!rec)
220 return 0;
221 duration = (uint32_t)timer_get_boot_us() - rec->start_us;
222 rec->time_us += duration;
223
224 return duration;
225 }
226
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
233 * @return pointer to name, either from the record or pointing to buf.
234 */
get_record_name(char * buf,int len,const struct bootstage_record * rec)235 static const char *get_record_name(char *buf, int len,
236 const struct bootstage_record *rec)
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
print_time_record(struct bootstage_record * rec,uint32_t prev)248 static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
249 {
250 char buf[20];
251
252 if (prev == -1U) {
253 printf("%11s", "");
254 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
255 } else {
256 print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
257 print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
258 }
259 printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
260
261 return rec->time_us;
262 }
263
h_compare_record(const void * r1,const void * r2)264 static 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
271 #ifdef CONFIG_OF_LIBFDT
272 /**
273 * Add all bootstage timings to a device tree.
274 *
275 * @param blob Device tree blob
276 * @return 0 on success, != 0 on failure.
277 */
add_bootstages_devicetree(struct fdt_header * blob)278 static int add_bootstages_devicetree(struct fdt_header *blob)
279 {
280 struct bootstage_data *data = gd->bootstage;
281 int bootstage;
282 char buf[20];
283 int recnum;
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)
295 return -EINVAL;
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 */
301 for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
302 struct bootstage_record *rec = &data->record[recnum];
303 int node;
304
305 if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
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",
314 get_record_name(buf, sizeof(buf), rec)))
315 return -EINVAL;
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))
321 return -EINVAL;
322 }
323
324 return 0;
325 }
326
bootstage_fdt_add_report(void)327 int 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
bootstage_report(void)336 void bootstage_report(void)
337 {
338 struct bootstage_data *data = gd->bootstage;
339 struct bootstage_record *rec = data->record;
340 uint32_t prev;
341 int i;
342
343 printf("Timer summary in microseconds (%d records):\n",
344 data->rec_count);
345 printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
346
347 prev = print_time_record(rec, 0);
348
349 /* Sort records by increasing time */
350 qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
351
352 for (i = 1, rec++; i < data->rec_count; i++, rec++) {
353 if (rec->id && !rec->start_us)
354 prev = print_time_record(rec, prev);
355 }
356 if (data->rec_count > RECORD_COUNT)
357 printf("Overflowed internal boot id table by %d entries\n"
358 "Please increase CONFIG_(SPL_TPL_)BOOTSTAGE_RECORD_COUNT\n",
359 data->rec_count - RECORD_COUNT);
360
361 puts("\nAccumulated time:\n");
362 for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
363 if (rec->start_us)
364 prev = print_time_record(rec, -1);
365 }
366 }
367
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 */
append_data(char ** ptrp,char * end,const void * data,int size)379 static 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
bootstage_stash(void * base,int size)390 int bootstage_stash(void *base, int size)
391 {
392 const struct bootstage_data *data = gd->bootstage;
393 struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
394 const struct bootstage_record *rec;
395 char buf[20];
396 char *ptr = base, *end = ptr + size;
397 int i;
398
399 if (hdr + 1 > (struct bootstage_hdr *)end) {
400 debug("%s: Not enough space for bootstage hdr\n", __func__);
401 return -ENOSPC;
402 }
403
404 /* Write an arbitrary version number */
405 hdr->version = BOOTSTAGE_VERSION;
406
407 hdr->count = data->rec_count;
408 hdr->size = 0;
409 hdr->magic = BOOTSTAGE_MAGIC;
410 hdr->next_id = data->next_id;
411 ptr += sizeof(*hdr);
412
413 /* Write the records, silently stopping when we run out of space */
414 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
415 append_data(&ptr, end, rec, sizeof(*rec));
416
417 /* Write the name strings */
418 for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
419 const char *name;
420
421 name = get_record_name(buf, sizeof(buf), rec);
422 append_data(&ptr, end, name, strlen(name) + 1);
423 }
424
425 /* Check for buffer overflow */
426 if (ptr > end) {
427 debug("%s: Not enough space for bootstage stash\n", __func__);
428 return -ENOSPC;
429 }
430
431 /* Update total data size */
432 hdr->size = ptr - (char *)base;
433 debug("Stashed %d records\n", hdr->count);
434
435 return 0;
436 }
437
bootstage_unstash(const void * base,int size)438 int bootstage_unstash(const void *base, int size)
439 {
440 const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
441 struct bootstage_data *data = gd->bootstage;
442 const char *ptr = base, *end = ptr + size;
443 struct bootstage_record *rec;
444 uint rec_size;
445 int i;
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__);
452 return -EPERM;
453 }
454
455 if (hdr->magic != BOOTSTAGE_MAGIC) {
456 debug("%s: Invalid bootstage magic\n", __func__);
457 return -ENOENT;
458 }
459
460 if (ptr + hdr->size > end) {
461 debug("%s: Bootstage data runs past buffer end\n", __func__);
462 return -ENOSPC;
463 }
464
465 if (hdr->count * sizeof(*rec) > hdr->size) {
466 debug("%s: Bootstage has %d records needing %lu bytes, but "
467 "only %d bytes is available\n", __func__, hdr->count,
468 (ulong)hdr->count * sizeof(*rec), hdr->size);
469 return -ENOSPC;
470 }
471
472 if (hdr->version != BOOTSTAGE_VERSION) {
473 debug("%s: Bootstage data version %#0x unrecognised\n",
474 __func__, hdr->version);
475 return -EINVAL;
476 }
477
478 if (data->rec_count + hdr->count > RECORD_COUNT) {
479 debug("%s: Bootstage has %d records, we have space for %d\n"
480 "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
481 __func__, hdr->count, RECORD_COUNT - data->rec_count);
482 return -ENOSPC;
483 }
484
485 ptr += sizeof(*hdr);
486
487 /* Read the records */
488 rec_size = hdr->count * sizeof(*data->record);
489 memcpy(data->record + data->rec_count, ptr, rec_size);
490
491 /* Read the name strings */
492 ptr += rec_size;
493 for (rec = data->record + data->next_id, i = 0; i < hdr->count;
494 i++, rec++) {
495 rec->name = ptr;
496 if (spl_phase() == PHASE_SPL)
497 rec->name = strdup(ptr);
498
499 /* Assume no data corruption here */
500 ptr += strlen(ptr) + 1;
501 }
502
503 /* Mark the records as read */
504 data->rec_count += hdr->count;
505 data->next_id = hdr->next_id;
506 debug("Unstashed %d records\n", hdr->count);
507
508 return 0;
509 }
510
bootstage_get_size(void)511 int bootstage_get_size(void)
512 {
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;
524 }
525
bootstage_init(bool first)526 int 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);
536 if (first) {
537 data->next_id = BOOTSTAGE_ID_USER;
538 bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);
539 }
540
541 return 0;
542 }
543