bootstage.c 12 KB

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