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