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