bootstage.c 10 KB

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