bootstage.c 12 KB

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