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