bootstage.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2011, Google Inc. All rights reserved.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /*
  23. * This module records the progress of boot and arbitrary commands, and
  24. * permits accurate timestamping of each.
  25. *
  26. * TBD: Pass timings to kernel in the FDT
  27. */
  28. #include <common.h>
  29. #include <libfdt.h>
  30. #include <malloc.h>
  31. #include <linux/compiler.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. struct bootstage_record {
  34. ulong time_us;
  35. uint32_t start_us;
  36. const char *name;
  37. int flags; /* see enum bootstage_flags */
  38. enum bootstage_id id;
  39. };
  40. static struct bootstage_record record[BOOTSTAGE_ID_COUNT] = { {1} };
  41. static int next_id = BOOTSTAGE_ID_USER;
  42. enum {
  43. BOOTSTAGE_VERSION = 0,
  44. BOOTSTAGE_MAGIC = 0xb00757a3,
  45. };
  46. struct bootstage_hdr {
  47. uint32_t version; /* BOOTSTAGE_VERSION */
  48. uint32_t count; /* Number of records */
  49. uint32_t size; /* Total data size (non-zero if valid) */
  50. uint32_t magic; /* Unused */
  51. };
  52. int bootstage_relocate(void)
  53. {
  54. int i;
  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. for (i = 0; i < BOOTSTAGE_ID_COUNT; i++)
  60. if (record[i].name)
  61. record[i].name = strdup(record[i].name);
  62. return 0;
  63. }
  64. ulong bootstage_add_record(enum bootstage_id id, const char *name,
  65. int flags, ulong mark)
  66. {
  67. struct bootstage_record *rec;
  68. if (flags & BOOTSTAGEF_ALLOC)
  69. id = next_id++;
  70. if (id < BOOTSTAGE_ID_COUNT) {
  71. rec = &record[id];
  72. /* Only record the first event for each */
  73. if (!rec->time_us) {
  74. rec->time_us = mark;
  75. rec->name = name;
  76. rec->flags = flags;
  77. rec->id = id;
  78. }
  79. }
  80. /* Tell the board about this progress */
  81. show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
  82. return mark;
  83. }
  84. ulong bootstage_mark(enum bootstage_id id)
  85. {
  86. return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
  87. }
  88. ulong bootstage_error(enum bootstage_id id)
  89. {
  90. return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
  91. timer_get_boot_us());
  92. }
  93. ulong bootstage_mark_name(enum bootstage_id id, const char *name)
  94. {
  95. int flags = 0;
  96. if (id == BOOTSTAGE_ID_ALLOC)
  97. flags = BOOTSTAGEF_ALLOC;
  98. return bootstage_add_record(id, name, flags, timer_get_boot_us());
  99. }
  100. ulong bootstage_mark_code(const char *file, const char *func, int linenum)
  101. {
  102. char *str, *p;
  103. __maybe_unused char *end;
  104. int len = 0;
  105. /* First work out the length we need to allocate */
  106. if (linenum != -1)
  107. len = 11;
  108. if (func)
  109. len += strlen(func);
  110. if (file)
  111. len += strlen(file);
  112. str = malloc(len + 1);
  113. p = str;
  114. end = p + len;
  115. if (file)
  116. p += snprintf(p, end - p, "%s,", file);
  117. if (linenum != -1)
  118. p += snprintf(p, end - p, "%d", linenum);
  119. if (func)
  120. p += snprintf(p, end - p, ": %s", func);
  121. return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
  122. }
  123. uint32_t bootstage_start(enum bootstage_id id, const char *name)
  124. {
  125. struct bootstage_record *rec = &record[id];
  126. rec->start_us = timer_get_boot_us();
  127. rec->name = name;
  128. return rec->start_us;
  129. }
  130. uint32_t bootstage_accum(enum bootstage_id id)
  131. {
  132. struct bootstage_record *rec = &record[id];
  133. uint32_t duration;
  134. duration = (uint32_t)timer_get_boot_us() - rec->start_us;
  135. rec->time_us += duration;
  136. return duration;
  137. }
  138. static void print_time(unsigned long us_time)
  139. {
  140. char str[15], *s;
  141. int grab = 3;
  142. /* We don't seem to have %'d in U-Boot */
  143. sprintf(str, "%12lu", us_time);
  144. for (s = str + 3; *s; s += grab) {
  145. if (s != str + 3)
  146. putc(s[-1] != ' ' ? ',' : ' ');
  147. printf("%.*s", grab, s);
  148. grab = 3;
  149. }
  150. }
  151. /**
  152. * Get a record name as a printable string
  153. *
  154. * @param buf Buffer to put name if needed
  155. * @param len Length of buffer
  156. * @param rec Boot stage record to get the name from
  157. * @return pointer to name, either from the record or pointing to buf.
  158. */
  159. static const char *get_record_name(char *buf, int len,
  160. struct bootstage_record *rec)
  161. {
  162. if (rec->name)
  163. return rec->name;
  164. else if (rec->id >= BOOTSTAGE_ID_USER)
  165. snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
  166. else
  167. snprintf(buf, len, "id=%d", rec->id);
  168. return buf;
  169. }
  170. static uint32_t print_time_record(enum bootstage_id id,
  171. struct bootstage_record *rec, uint32_t prev)
  172. {
  173. char buf[20];
  174. if (prev == -1U) {
  175. printf("%11s", "");
  176. print_time(rec->time_us);
  177. } else {
  178. print_time(rec->time_us);
  179. print_time(rec->time_us - prev);
  180. }
  181. printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
  182. return rec->time_us;
  183. }
  184. static int h_compare_record(const void *r1, const void *r2)
  185. {
  186. const struct bootstage_record *rec1 = r1, *rec2 = r2;
  187. return rec1->time_us > rec2->time_us ? 1 : -1;
  188. }
  189. #ifdef CONFIG_OF_LIBFDT
  190. /**
  191. * Add all bootstage timings to a device tree.
  192. *
  193. * @param blob Device tree blob
  194. * @return 0 on success, != 0 on failure.
  195. */
  196. static int add_bootstages_devicetree(struct fdt_header *blob)
  197. {
  198. int bootstage;
  199. char buf[20];
  200. int id;
  201. int i;
  202. if (!blob)
  203. return 0;
  204. /*
  205. * Create the node for bootstage.
  206. * The address of flat device tree is set up by the command bootm.
  207. */
  208. bootstage = fdt_add_subnode(blob, 0, "bootstage");
  209. if (bootstage < 0)
  210. return -1;
  211. /*
  212. * Insert the timings to the device tree in the reverse order so
  213. * that they can be printed in the Linux kernel in the right order.
  214. */
  215. for (id = BOOTSTAGE_ID_COUNT - 1, i = 0; id >= 0; id--, i++) {
  216. struct bootstage_record *rec = &record[id];
  217. int node;
  218. if (id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
  219. continue;
  220. node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
  221. if (node < 0)
  222. break;
  223. /* add properties to the node. */
  224. if (fdt_setprop_string(blob, node, "name",
  225. get_record_name(buf, sizeof(buf), rec)))
  226. return -1;
  227. /* Check if this is a 'mark' or 'accum' record */
  228. if (fdt_setprop_cell(blob, node,
  229. rec->start_us ? "accum" : "mark",
  230. rec->time_us))
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. int bootstage_fdt_add_report(void)
  236. {
  237. if (add_bootstages_devicetree(working_fdt))
  238. puts("bootstage: Failed to add to device tree\n");
  239. return 0;
  240. }
  241. #endif
  242. void bootstage_report(void)
  243. {
  244. struct bootstage_record *rec = record;
  245. int id;
  246. uint32_t prev;
  247. puts("Timer summary in microseconds:\n");
  248. printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
  249. /* Fake the first record - we could get it from early boot */
  250. rec->name = "reset";
  251. rec->time_us = 0;
  252. prev = print_time_record(BOOTSTAGE_ID_AWAKE, rec, 0);
  253. /* Sort records by increasing time */
  254. qsort(record, ARRAY_SIZE(record), sizeof(*rec), h_compare_record);
  255. for (id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  256. if (rec->time_us != 0 && !rec->start_us)
  257. prev = print_time_record(rec->id, rec, prev);
  258. }
  259. if (next_id > BOOTSTAGE_ID_COUNT)
  260. printf("(Overflowed internal boot id table by %d entries\n"
  261. "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
  262. next_id - BOOTSTAGE_ID_COUNT);
  263. puts("\nAccumulated time:\n");
  264. for (id = 0, rec = record; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  265. if (rec->start_us)
  266. prev = print_time_record(id, rec, -1);
  267. }
  268. }
  269. ulong __timer_get_boot_us(void)
  270. {
  271. static ulong base_time;
  272. /*
  273. * We can't implement this properly. Return 0 on the first call and
  274. * larger values after that.
  275. */
  276. if (base_time)
  277. return get_timer(base_time) * 1000;
  278. base_time = get_timer(0);
  279. return 0;
  280. }
  281. ulong timer_get_boot_us(void)
  282. __attribute__((weak, alias("__timer_get_boot_us")));
  283. /**
  284. * Append data to a memory buffer
  285. *
  286. * Write data to the buffer if there is space. Whether there is space or not,
  287. * the buffer pointer is incremented.
  288. *
  289. * @param ptrp Pointer to buffer, updated by this function
  290. * @param end Pointer to end of buffer
  291. * @param data Data to write to buffer
  292. * @param size Size of data
  293. */
  294. static void append_data(char **ptrp, char *end, const void *data, int size)
  295. {
  296. char *ptr = *ptrp;
  297. *ptrp += size;
  298. if (*ptrp > end)
  299. return;
  300. memcpy(ptr, data, size);
  301. }
  302. int bootstage_stash(void *base, int size)
  303. {
  304. struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
  305. struct bootstage_record *rec;
  306. char buf[20];
  307. char *ptr = base, *end = ptr + size;
  308. uint32_t count;
  309. int id;
  310. if (hdr + 1 > (struct bootstage_hdr *)end) {
  311. debug("%s: Not enough space for bootstage hdr\n", __func__);
  312. return -1;
  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 = record, id = count = 0; id < BOOTSTAGE_ID_COUNT;
  318. id++, rec++) {
  319. if (rec->time_us != 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 = record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  328. if (rec->time_us != 0)
  329. append_data(&ptr, end, rec, sizeof(*rec));
  330. }
  331. /* Write the name strings */
  332. for (rec = record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  333. if (rec->time_us != 0) {
  334. const char *name;
  335. name = get_record_name(buf, sizeof(buf), rec);
  336. append_data(&ptr, end, name, strlen(name) + 1);
  337. }
  338. }
  339. /* Check for buffer overflow */
  340. if (ptr > end) {
  341. debug("%s: Not enough space for bootstage stash\n", __func__);
  342. return -1;
  343. }
  344. /* Update total data size */
  345. hdr->size = ptr - (char *)base;
  346. printf("Stashed %d records\n", hdr->count);
  347. return 0;
  348. }
  349. int bootstage_unstash(void *base, int size)
  350. {
  351. struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
  352. struct bootstage_record *rec;
  353. char *ptr = base, *end = ptr + size;
  354. uint rec_size;
  355. int id;
  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 -1;
  361. }
  362. if (hdr->magic != BOOTSTAGE_MAGIC) {
  363. debug("%s: Invalid bootstage magic\n", __func__);
  364. return -1;
  365. }
  366. if (ptr + hdr->size > end) {
  367. debug("%s: Bootstage data runs past buffer end\n", __func__);
  368. return -1;
  369. }
  370. if (hdr->count * sizeof(*rec) > hdr->size) {
  371. debug("%s: Bootstage has %d records needing %d bytes, but "
  372. "only %d bytes is available\n", __func__, hdr->count,
  373. hdr->count * sizeof(*rec), hdr->size);
  374. return -1;
  375. }
  376. if (hdr->version != BOOTSTAGE_VERSION) {
  377. debug("%s: Bootstage data version %#0x unrecognised\n",
  378. __func__, hdr->version);
  379. return -1;
  380. }
  381. if (next_id + hdr->count > BOOTSTAGE_ID_COUNT) {
  382. debug("%s: Bootstage has %d records, we have space for %d\n"
  383. "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
  384. __func__, hdr->count, BOOTSTAGE_ID_COUNT - next_id);
  385. return -1;
  386. }
  387. ptr += sizeof(*hdr);
  388. /* Read the records */
  389. rec_size = hdr->count * sizeof(*record);
  390. memcpy(record + next_id, ptr, rec_size);
  391. /* Read the name strings */
  392. ptr += rec_size;
  393. for (rec = record + next_id, id = 0; id < hdr->count; id++, rec++) {
  394. rec->name = ptr;
  395. /* Assume no data corruption here */
  396. ptr += strlen(ptr) + 1;
  397. }
  398. /* Mark the records as read */
  399. next_id += hdr->count;
  400. printf("Unstashed %d records\n", hdr->count);
  401. return 0;
  402. }