bootstage.c 12 KB

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