bootstage.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. DECLARE_GLOBAL_DATA_PTR;
  31. struct bootstage_record {
  32. ulong time_us;
  33. uint32_t start_us;
  34. const char *name;
  35. int flags; /* see enum bootstage_flags */
  36. enum bootstage_id id;
  37. };
  38. static struct bootstage_record record[BOOTSTAGE_ID_COUNT] = { {1} };
  39. static int next_id = BOOTSTAGE_ID_USER;
  40. enum {
  41. BOOTSTAGE_VERSION = 0,
  42. BOOTSTAGE_MAGIC = 0xb00757a3,
  43. };
  44. struct bootstage_hdr {
  45. uint32_t version; /* BOOTSTAGE_VERSION */
  46. uint32_t count; /* Number of records */
  47. uint32_t size; /* Total data size (non-zero if valid) */
  48. uint32_t magic; /* Unused */
  49. };
  50. ulong bootstage_add_record(enum bootstage_id id, const char *name,
  51. int flags, ulong mark)
  52. {
  53. struct bootstage_record *rec;
  54. if (flags & BOOTSTAGEF_ALLOC)
  55. id = next_id++;
  56. if (id < BOOTSTAGE_ID_COUNT) {
  57. rec = &record[id];
  58. /* Only record the first event for each */
  59. if (!rec->time_us) {
  60. rec->time_us = mark;
  61. rec->name = name;
  62. rec->flags = flags;
  63. rec->id = id;
  64. }
  65. }
  66. /* Tell the board about this progress */
  67. show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
  68. return mark;
  69. }
  70. ulong bootstage_mark(enum bootstage_id id)
  71. {
  72. return bootstage_add_record(id, NULL, 0, timer_get_boot_us());
  73. }
  74. ulong bootstage_error(enum bootstage_id id)
  75. {
  76. return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR,
  77. timer_get_boot_us());
  78. }
  79. ulong bootstage_mark_name(enum bootstage_id id, const char *name)
  80. {
  81. int flags = 0;
  82. if (id == BOOTSTAGE_ID_ALLOC)
  83. flags = BOOTSTAGEF_ALLOC;
  84. return bootstage_add_record(id, name, flags, timer_get_boot_us());
  85. }
  86. uint32_t bootstage_start(enum bootstage_id id, const char *name)
  87. {
  88. struct bootstage_record *rec = &record[id];
  89. rec->start_us = timer_get_boot_us();
  90. rec->name = name;
  91. return rec->start_us;
  92. }
  93. uint32_t bootstage_accum(enum bootstage_id id)
  94. {
  95. struct bootstage_record *rec = &record[id];
  96. uint32_t duration;
  97. duration = (uint32_t)timer_get_boot_us() - rec->start_us;
  98. rec->time_us += duration;
  99. return duration;
  100. }
  101. static void print_time(unsigned long us_time)
  102. {
  103. char str[15], *s;
  104. int grab = 3;
  105. /* We don't seem to have %'d in U-Boot */
  106. sprintf(str, "%12lu", us_time);
  107. for (s = str + 3; *s; s += grab) {
  108. if (s != str + 3)
  109. putc(s[-1] != ' ' ? ',' : ' ');
  110. printf("%.*s", grab, s);
  111. grab = 3;
  112. }
  113. }
  114. /**
  115. * Get a record name as a printable string
  116. *
  117. * @param buf Buffer to put name if needed
  118. * @param len Length of buffer
  119. * @param rec Boot stage record to get the name from
  120. * @return pointer to name, either from the record or pointing to buf.
  121. */
  122. static const char *get_record_name(char *buf, int len,
  123. struct bootstage_record *rec)
  124. {
  125. if (rec->name)
  126. return rec->name;
  127. else if (rec->id >= BOOTSTAGE_ID_USER)
  128. snprintf(buf, len, "user_%d", rec->id - BOOTSTAGE_ID_USER);
  129. else
  130. snprintf(buf, len, "id=%d", rec->id);
  131. return buf;
  132. }
  133. static uint32_t print_time_record(enum bootstage_id id,
  134. struct bootstage_record *rec, uint32_t prev)
  135. {
  136. char buf[20];
  137. if (prev == -1U) {
  138. printf("%11s", "");
  139. print_time(rec->time_us);
  140. } else {
  141. print_time(rec->time_us);
  142. print_time(rec->time_us - prev);
  143. }
  144. printf(" %s\n", get_record_name(buf, sizeof(buf), rec));
  145. return rec->time_us;
  146. }
  147. static int h_compare_record(const void *r1, const void *r2)
  148. {
  149. const struct bootstage_record *rec1 = r1, *rec2 = r2;
  150. return rec1->time_us > rec2->time_us ? 1 : -1;
  151. }
  152. #ifdef CONFIG_OF_LIBFDT
  153. /**
  154. * Add all bootstage timings to a device tree.
  155. *
  156. * @param blob Device tree blob
  157. * @return 0 on success, != 0 on failure.
  158. */
  159. static int add_bootstages_devicetree(struct fdt_header *blob)
  160. {
  161. int bootstage;
  162. char buf[20];
  163. int id;
  164. int i;
  165. if (!blob)
  166. return 0;
  167. /*
  168. * Create the node for bootstage.
  169. * The address of flat device tree is set up by the command bootm.
  170. */
  171. bootstage = fdt_add_subnode(blob, 0, "bootstage");
  172. if (bootstage < 0)
  173. return -1;
  174. /*
  175. * Insert the timings to the device tree in the reverse order so
  176. * that they can be printed in the Linux kernel in the right order.
  177. */
  178. for (id = BOOTSTAGE_ID_COUNT - 1, i = 0; id >= 0; id--, i++) {
  179. struct bootstage_record *rec = &record[id];
  180. int node;
  181. if (id != BOOTSTAGE_ID_AWAKE && rec->time_us == 0)
  182. continue;
  183. node = fdt_add_subnode(blob, bootstage, simple_itoa(i));
  184. if (node < 0)
  185. break;
  186. /* add properties to the node. */
  187. if (fdt_setprop_string(blob, node, "name",
  188. get_record_name(buf, sizeof(buf), rec)))
  189. return -1;
  190. /* Check if this is a 'mark' or 'accum' record */
  191. if (fdt_setprop_cell(blob, node,
  192. rec->start_us ? "accum" : "mark",
  193. rec->time_us))
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. int bootstage_fdt_add_report(void)
  199. {
  200. if (add_bootstages_devicetree(working_fdt))
  201. puts("bootstage: Failed to add to device tree\n");
  202. return 0;
  203. }
  204. #endif
  205. void bootstage_report(void)
  206. {
  207. struct bootstage_record *rec = record;
  208. int id;
  209. uint32_t prev;
  210. puts("Timer summary in microseconds:\n");
  211. printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
  212. /* Fake the first record - we could get it from early boot */
  213. rec->name = "reset";
  214. rec->time_us = 0;
  215. prev = print_time_record(BOOTSTAGE_ID_AWAKE, rec, 0);
  216. /* Sort records by increasing time */
  217. qsort(record, ARRAY_SIZE(record), sizeof(*rec), h_compare_record);
  218. for (id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  219. if (rec->time_us != 0 && !rec->start_us)
  220. prev = print_time_record(rec->id, rec, prev);
  221. }
  222. if (next_id > BOOTSTAGE_ID_COUNT)
  223. printf("(Overflowed internal boot id table by %d entries\n"
  224. "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
  225. next_id - BOOTSTAGE_ID_COUNT);
  226. puts("\nAccumulated time:\n");
  227. for (id = 0, rec = record; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  228. if (rec->start_us)
  229. prev = print_time_record(id, rec, -1);
  230. }
  231. }
  232. ulong __timer_get_boot_us(void)
  233. {
  234. static ulong base_time;
  235. /*
  236. * We can't implement this properly. Return 0 on the first call and
  237. * larger values after that.
  238. */
  239. if (base_time)
  240. return get_timer(base_time) * 1000;
  241. base_time = get_timer(0);
  242. return 0;
  243. }
  244. ulong timer_get_boot_us(void)
  245. __attribute__((weak, alias("__timer_get_boot_us")));
  246. /**
  247. * Append data to a memory buffer
  248. *
  249. * Write data to the buffer if there is space. Whether there is space or not,
  250. * the buffer pointer is incremented.
  251. *
  252. * @param ptrp Pointer to buffer, updated by this function
  253. * @param end Pointer to end of buffer
  254. * @param data Data to write to buffer
  255. * @param size Size of data
  256. */
  257. static void append_data(char **ptrp, char *end, const void *data, int size)
  258. {
  259. char *ptr = *ptrp;
  260. *ptrp += size;
  261. if (*ptrp > end)
  262. return;
  263. memcpy(ptr, data, size);
  264. }
  265. int bootstage_stash(void *base, int size)
  266. {
  267. struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
  268. struct bootstage_record *rec;
  269. char buf[20];
  270. char *ptr = base, *end = ptr + size;
  271. uint32_t count;
  272. int id;
  273. if (hdr + 1 > (struct bootstage_hdr *)end) {
  274. debug("%s: Not enough space for bootstage hdr\n", __func__);
  275. return -1;
  276. }
  277. /* Write an arbitrary version number */
  278. hdr->version = BOOTSTAGE_VERSION;
  279. /* Count the number of records, and write that value first */
  280. for (rec = record, id = count = 0; id < BOOTSTAGE_ID_COUNT;
  281. id++, rec++) {
  282. if (rec->time_us != 0)
  283. count++;
  284. }
  285. hdr->count = count;
  286. hdr->size = 0;
  287. hdr->magic = BOOTSTAGE_MAGIC;
  288. ptr += sizeof(*hdr);
  289. /* Write the records, silently stopping when we run out of space */
  290. for (rec = record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  291. if (rec->time_us != 0)
  292. append_data(&ptr, end, rec, sizeof(*rec));
  293. }
  294. /* Write the name strings */
  295. for (rec = record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  296. if (rec->time_us != 0) {
  297. const char *name;
  298. name = get_record_name(buf, sizeof(buf), rec);
  299. append_data(&ptr, end, name, strlen(name) + 1);
  300. }
  301. }
  302. /* Check for buffer overflow */
  303. if (ptr > end) {
  304. debug("%s: Not enough space for bootstage stash\n", __func__);
  305. return -1;
  306. }
  307. /* Update total data size */
  308. hdr->size = ptr - (char *)base;
  309. printf("Stashed %d records\n", hdr->count);
  310. return 0;
  311. }
  312. int bootstage_unstash(void *base, int size)
  313. {
  314. struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
  315. struct bootstage_record *rec;
  316. char *ptr = base, *end = ptr + size;
  317. uint rec_size;
  318. int id;
  319. if (size == -1)
  320. end = (char *)(~(uintptr_t)0);
  321. if (hdr + 1 > (struct bootstage_hdr *)end) {
  322. debug("%s: Not enough space for bootstage hdr\n", __func__);
  323. return -1;
  324. }
  325. if (hdr->magic != BOOTSTAGE_MAGIC) {
  326. debug("%s: Invalid bootstage magic\n", __func__);
  327. return -1;
  328. }
  329. if (ptr + hdr->size > end) {
  330. debug("%s: Bootstage data runs past buffer end\n", __func__);
  331. return -1;
  332. }
  333. if (hdr->count * sizeof(*rec) > hdr->size) {
  334. debug("%s: Bootstage has %d records needing %d bytes, but "
  335. "only %d bytes is available\n", __func__, hdr->count,
  336. hdr->count * sizeof(*rec), hdr->size);
  337. return -1;
  338. }
  339. if (hdr->version != BOOTSTAGE_VERSION) {
  340. debug("%s: Bootstage data version %#0x unrecognised\n",
  341. __func__, hdr->version);
  342. return -1;
  343. }
  344. if (next_id + hdr->count > BOOTSTAGE_ID_COUNT) {
  345. debug("%s: Bootstage has %d records, we have space for %d\n"
  346. "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
  347. __func__, hdr->count, BOOTSTAGE_ID_COUNT - next_id);
  348. return -1;
  349. }
  350. ptr += sizeof(*hdr);
  351. /* Read the records */
  352. rec_size = hdr->count * sizeof(*record);
  353. memcpy(record + next_id, ptr, rec_size);
  354. /* Read the name strings */
  355. ptr += rec_size;
  356. for (rec = record + next_id, id = 0; id < hdr->count; id++, rec++) {
  357. rec->name = ptr;
  358. /* Assume no data corruption here */
  359. ptr += strlen(ptr) + 1;
  360. }
  361. /* Mark the records as read */
  362. next_id += hdr->count;
  363. printf("Unstashed %d records\n", hdr->count);
  364. return 0;
  365. }