bootstage.c 12 KB

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