bootstage.c 12 KB

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