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