bootstage.c 11 KB

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