bootstage.c 12 KB

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