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. #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_error_name(enum bootstage_id id, const char *name)
  124. {
  125. return bootstage_add_record(id, name, BOOTSTAGEF_ERROR,
  126. timer_get_boot_us());
  127. }
  128. ulong bootstage_mark_name(enum bootstage_id id, const char *name)
  129. {
  130. int flags = 0;
  131. if (id == BOOTSTAGE_ID_ALLOC)
  132. flags = BOOTSTAGEF_ALLOC;
  133. return bootstage_add_record(id, name, flags, timer_get_boot_us());
  134. }
  135. ulong bootstage_mark_code(const char *file, const char *func, int linenum)
  136. {
  137. char *str, *p;
  138. __maybe_unused char *end;
  139. int len = 0;
  140. /* First work out the length we need to allocate */
  141. if (linenum != -1)
  142. len = 11;
  143. if (func)
  144. len += strlen(func);
  145. if (file)
  146. len += strlen(file);
  147. str = malloc(len + 1);
  148. p = str;
  149. end = p + len;
  150. if (file)
  151. p += snprintf(p, end - p, "%s,", file);
  152. if (linenum != -1)
  153. p += snprintf(p, end - p, "%d", linenum);
  154. if (func)
  155. p += snprintf(p, end - p, ": %s", func);
  156. return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
  157. }
  158. uint32_t bootstage_start(enum bootstage_id id, const char *name)
  159. {
  160. struct bootstage_data *data = gd->bootstage;
  161. struct bootstage_record *rec = ensure_id(data, id);
  162. ulong start_us = timer_get_boot_us();
  163. if (rec) {
  164. rec->start_us = start_us;
  165. rec->name = name;
  166. }
  167. return start_us;
  168. }
  169. uint32_t bootstage_accum(enum bootstage_id id)
  170. {
  171. struct bootstage_data *data = gd->bootstage;
  172. struct bootstage_record *rec = ensure_id(data, id);
  173. uint32_t duration;
  174. if (!rec)
  175. return 0;
  176. duration = (uint32_t)timer_get_boot_us() - rec->start_us;
  177. rec->time_us += duration;
  178. return duration;
  179. }
  180. /**
  181. * Get a record name as a printable string
  182. *
  183. * @param buf Buffer to put name if needed
  184. * @param len Length of buffer
  185. * @param rec Boot stage record to get the name from
  186. * Return: pointer to name, either from the record or pointing to buf.
  187. */
  188. static const char *get_record_name(char *buf, int len,
  189. const struct bootstage_record *rec)
  190. {
  191. if (rec->name)
  192. return rec->name;
  193. else if (rec->id >= BOOTSTAGE_ID_USER)
  194. snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
  195. else
  196. snprintf(buf, len, "id=%d", rec->id);
  197. return buf;
  198. }
  199. static uint32_t print_time_record(struct bootstage_record *rec, uint32_t prev)
  200. {
  201. char buf[20];
  202. if (prev == -1U) {
  203. printf("%11s", "");
  204. print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
  205. } else {
  206. print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
  207. print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
  208. }
  209. printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
  210. return rec->time_us;
  211. }
  212. static int h_compare_record(const void *r1, const void *r2)
  213. {
  214. const struct bootstage_record *rec1 = r1, *rec2 = r2;
  215. return rec1->time_us > rec2->time_us ? 1 : -1;
  216. }
  217. #ifdef CONFIG_OF_LIBFDT
  218. /**
  219. * Add all bootstage timings to a device tree.
  220. *
  221. * @param blob Device tree blob
  222. * Return: 0 on success, != 0 on failure.
  223. */
  224. static int add_bootstages_devicetree(struct fdt_header *blob)
  225. {
  226. struct bootstage_data *data = gd->bootstage;
  227. int bootstage;
  228. char buf[20];
  229. int recnum;
  230. int i;
  231. if (!blob)
  232. return 0;
  233. /*
  234. * Create the node for bootstage.
  235. * The address of flat device tree is set up by the command bootm.
  236. */
  237. bootstage = fdt_add_subnode(blob, 0, "bootstage");
  238. if (bootstage < 0)
  239. return -EINVAL;
  240. /*
  241. * Insert the timings to the device tree in the reverse order so
  242. * that they can be printed in the Linux kernel in the right order.
  243. */
  244. for (recnum = data->rec_count - 1, i = 0; recnum >= 0; recnum--, i++) {
  245. struct bootstage_record *rec = &data->record[recnum];
  246. int node;
  247. if (rec->id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
  248. continue;
  249. node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
  250. if (node < 0)
  251. break;
  252. /* add properties to the node. */
  253. if (fdt_setprop_string(blob, node, "name",
  254. get_record_name(buf, sizeof(buf), rec)))
  255. return -EINVAL;
  256. /* Check if this is a 'mark' or 'accum' record */
  257. if (fdt_setprop_cell(blob, node,
  258. rec->start_us ? "accum" : "mark",
  259. rec->time_us))
  260. return -EINVAL;
  261. }
  262. return 0;
  263. }
  264. int bootstage_fdt_add_report(void)
  265. {
  266. if (add_bootstages_devicetree(working_fdt))
  267. puts("bootstage: Failed to add to device tree\n");
  268. return 0;
  269. }
  270. #endif
  271. void bootstage_report(void)
  272. {
  273. struct bootstage_data *data = gd->bootstage;
  274. struct bootstage_record *rec = data->record;
  275. uint32_t prev;
  276. int i;
  277. printf("Timer summary in microseconds (%d records):\n",
  278. data->rec_count);
  279. printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
  280. prev = print_time_record(rec, 0);
  281. /* Sort records by increasing time */
  282. qsort(data->record, data->rec_count, sizeof(*rec), h_compare_record);
  283. for (i = 1, rec++; i < data->rec_count; i++, rec++) {
  284. if (rec->id && !rec->start_us)
  285. prev = print_time_record(rec, prev);
  286. }
  287. if (data->rec_count > RECORD_COUNT)
  288. printf("Overflowed internal boot id table by %d entries\n"
  289. "Please increase CONFIG_(SPL_TPL_)BOOTSTAGE_RECORD_COUNT\n",
  290. data->rec_count - RECORD_COUNT);
  291. puts("\nAccumulated time:\n");
  292. for (i = 0, rec = data->record; i < data->rec_count; i++, rec++) {
  293. if (rec->start_us)
  294. prev = print_time_record(rec, -1);
  295. }
  296. }
  297. /**
  298. * Append data to a memory buffer
  299. *
  300. * Write data to the buffer if there is space. Whether there is space or not,
  301. * the buffer pointer is incremented.
  302. *
  303. * @param ptrp Pointer to buffer, updated by this function
  304. * @param end Pointer to end of buffer
  305. * @param data Data to write to buffer
  306. * @param size Size of data
  307. */
  308. static void append_data(char **ptrp, char *end, const void *data, int size)
  309. {
  310. char *ptr = *ptrp;
  311. *ptrp += size;
  312. if (*ptrp > end)
  313. return;
  314. memcpy(ptr, data, size);
  315. }
  316. int bootstage_stash(void *base, int size)
  317. {
  318. const struct bootstage_data *data = gd->bootstage;
  319. struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
  320. const struct bootstage_record *rec;
  321. char buf[20];
  322. char *ptr = base, *end = ptr + size;
  323. int i;
  324. if (hdr + 1 > (struct bootstage_hdr *)end) {
  325. debug("%s: Not enough space for bootstage hdr\n", __func__);
  326. return -ENOSPC;
  327. }
  328. /* Write an arbitrary version number */
  329. hdr->version = BOOTSTAGE_VERSION;
  330. hdr->count = data->rec_count;
  331. hdr->size = 0;
  332. hdr->magic = BOOTSTAGE_MAGIC;
  333. hdr->next_id = data->next_id;
  334. ptr += sizeof(*hdr);
  335. /* Write the records, silently stopping when we run out of space */
  336. for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
  337. append_data(&ptr, end, rec, sizeof(*rec));
  338. /* Write the name strings */
  339. for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
  340. const char *name;
  341. name = get_record_name(buf, sizeof(buf), rec);
  342. append_data(&ptr, end, name, strlen(name) + 1);
  343. }
  344. /* Check for buffer overflow */
  345. if (ptr > end) {
  346. debug("%s: Not enough space for bootstage stash\n", __func__);
  347. return -ENOSPC;
  348. }
  349. /* Update total data size */
  350. hdr->size = ptr - (char *)base;
  351. debug("Stashed %d records\n", hdr->count);
  352. return 0;
  353. }
  354. int bootstage_unstash(const void *base, int size)
  355. {
  356. const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
  357. struct bootstage_data *data = gd->bootstage;
  358. const char *ptr = base, *end = ptr + size;
  359. struct bootstage_record *rec;
  360. uint rec_size;
  361. int i;
  362. if (size == -1)
  363. end = (char *)(~(uintptr_t)0);
  364. if (hdr + 1 > (struct bootstage_hdr *)end) {
  365. debug("%s: Not enough space for bootstage hdr\n", __func__);
  366. return -EPERM;
  367. }
  368. if (hdr->magic != BOOTSTAGE_MAGIC) {
  369. debug("%s: Invalid bootstage magic\n", __func__);
  370. return -ENOENT;
  371. }
  372. if (ptr + hdr->size > end) {
  373. debug("%s: Bootstage data runs past buffer end\n", __func__);
  374. return -ENOSPC;
  375. }
  376. if (hdr->count * sizeof(*rec) > hdr->size) {
  377. debug("%s: Bootstage has %d records needing %lu bytes, but "
  378. "only %d bytes is available\n", __func__, hdr->count,
  379. (ulong)hdr->count * sizeof(*rec), hdr->size);
  380. return -ENOSPC;
  381. }
  382. if (hdr->version != BOOTSTAGE_VERSION) {
  383. debug("%s: Bootstage data version %#0x unrecognised\n",
  384. __func__, hdr->version);
  385. return -EINVAL;
  386. }
  387. if (data->rec_count + hdr->count > RECORD_COUNT) {
  388. debug("%s: Bootstage has %d records, we have space for %d\n"
  389. "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
  390. __func__, hdr->count, RECORD_COUNT - data->rec_count);
  391. return -ENOSPC;
  392. }
  393. ptr += sizeof(*hdr);
  394. /* Read the records */
  395. rec_size = hdr->count * sizeof(*data->record);
  396. memcpy(data->record + data->rec_count, ptr, rec_size);
  397. /* Read the name strings */
  398. ptr += rec_size;
  399. for (rec = data->record + data->next_id, i = 0; i < hdr->count;
  400. i++, rec++) {
  401. rec->name = ptr;
  402. if (spl_phase() == PHASE_SPL)
  403. rec->name = strdup(ptr);
  404. /* Assume no data corruption here */
  405. ptr += strlen(ptr) + 1;
  406. }
  407. /* Mark the records as read */
  408. data->rec_count += hdr->count;
  409. data->next_id = hdr->next_id;
  410. debug("Unstashed %d records\n", hdr->count);
  411. return 0;
  412. }
  413. int bootstage_get_size(void)
  414. {
  415. struct bootstage_data *data = gd->bootstage;
  416. struct bootstage_record *rec;
  417. int size;
  418. int i;
  419. size = sizeof(struct bootstage_data);
  420. for (rec = data->record, i = 0; i < data->rec_count;
  421. i++, rec++)
  422. size += strlen(rec->name) + 1;
  423. return size;
  424. }
  425. int bootstage_init(bool first)
  426. {
  427. struct bootstage_data *data;
  428. int size = sizeof(struct bootstage_data);
  429. gd->bootstage = (struct bootstage_data *)malloc(size);
  430. if (!gd->bootstage)
  431. return -ENOMEM;
  432. data = gd->bootstage;
  433. memset(data, '\0', size);
  434. if (first) {
  435. data->next_id = BOOTSTAGE_ID_USER;
  436. bootstage_add_record(BOOTSTAGE_ID_AWAKE, "reset", 0, 0);
  437. }
  438. return 0;
  439. }