bootstage.c 12 KB

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