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