bootstage.c 12 KB

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