bootstage.c 12 KB

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