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