bootstage.c 12 KB

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