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