bootstage.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. enum bootstage_flags {
  32. BOOTSTAGEF_ERROR = 1 << 0, /* Error record */
  33. BOOTSTAGEF_ALLOC = 1 << 1, /* Allocate an id */
  34. };
  35. struct bootstage_record {
  36. ulong time_us;
  37. const char *name;
  38. int flags; /* see enum bootstage_flags */
  39. enum bootstage_id id;
  40. };
  41. static struct bootstage_record record[BOOTSTAGE_ID_COUNT] = { {1} };
  42. static int next_id = BOOTSTAGE_ID_USER;
  43. ulong bootstage_add_record(enum bootstage_id id, const char *name,
  44. int flags)
  45. {
  46. struct bootstage_record *rec;
  47. ulong mark = timer_get_boot_us();
  48. if (flags & BOOTSTAGEF_ALLOC)
  49. id = next_id++;
  50. if (id < BOOTSTAGE_ID_COUNT) {
  51. rec = &record[id];
  52. /* Only record the first event for each */
  53. if (!rec->time_us) {
  54. rec->time_us = mark;
  55. rec->name = name;
  56. rec->flags = flags;
  57. rec->id = id;
  58. }
  59. }
  60. /* Tell the board about this progress */
  61. show_boot_progress(flags & BOOTSTAGEF_ERROR ? -id : id);
  62. return mark;
  63. }
  64. ulong bootstage_mark(enum bootstage_id id)
  65. {
  66. return bootstage_add_record(id, NULL, 0);
  67. }
  68. ulong bootstage_error(enum bootstage_id id)
  69. {
  70. return bootstage_add_record(id, NULL, BOOTSTAGEF_ERROR);
  71. }
  72. ulong bootstage_mark_name(enum bootstage_id id, const char *name)
  73. {
  74. int flags = 0;
  75. if (id == BOOTSTAGE_ID_ALLOC)
  76. flags = BOOTSTAGEF_ALLOC;
  77. return bootstage_add_record(id, name, flags);
  78. }
  79. static void print_time(unsigned long us_time)
  80. {
  81. char str[15], *s;
  82. int grab = 3;
  83. /* We don't seem to have %'d in U-Boot */
  84. sprintf(str, "%12lu", us_time);
  85. for (s = str + 3; *s; s += grab) {
  86. if (s != str + 3)
  87. putc(s[-1] != ' ' ? ',' : ' ');
  88. printf("%.*s", grab, s);
  89. grab = 3;
  90. }
  91. }
  92. static uint32_t print_time_record(enum bootstage_id id,
  93. struct bootstage_record *rec, uint32_t prev)
  94. {
  95. print_time(rec->time_us);
  96. print_time(rec->time_us - prev);
  97. if (rec->name)
  98. printf(" %s\n", rec->name);
  99. else if (id >= BOOTSTAGE_ID_USER)
  100. printf(" user_%d\n", id - BOOTSTAGE_ID_USER);
  101. else
  102. printf(" id=%d\n", id);
  103. return rec->time_us;
  104. }
  105. static int h_compare_record(const void *r1, const void *r2)
  106. {
  107. const struct bootstage_record *rec1 = r1, *rec2 = r2;
  108. return rec1->time_us > rec2->time_us ? 1 : -1;
  109. }
  110. void bootstage_report(void)
  111. {
  112. struct bootstage_record *rec = record;
  113. int id;
  114. uint32_t prev;
  115. puts("Timer summary in microseconds:\n");
  116. printf("%11s%11s %s\n", "Mark", "Elapsed", "Stage");
  117. /* Fake the first record - we could get it from early boot */
  118. rec->name = "reset";
  119. rec->time_us = 0;
  120. prev = print_time_record(BOOTSTAGE_ID_AWAKE, rec, 0);
  121. /* Sort records by increasing time */
  122. qsort(record, ARRAY_SIZE(record), sizeof(*rec), h_compare_record);
  123. for (id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
  124. if (rec->time_us != 0)
  125. prev = print_time_record(rec->id, rec, prev);
  126. }
  127. if (next_id > BOOTSTAGE_ID_COUNT)
  128. printf("(Overflowed internal boot id table by %d entries\n"
  129. "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
  130. next_id - BOOTSTAGE_ID_COUNT);
  131. }
  132. ulong __timer_get_boot_us(void)
  133. {
  134. static ulong base_time;
  135. /*
  136. * We can't implement this properly. Return 0 on the first call and
  137. * larger values after that.
  138. */
  139. if (base_time)
  140. return get_timer(base_time) * 1000;
  141. base_time = get_timer(0);
  142. return 0;
  143. }
  144. ulong timer_get_boot_us(void)
  145. __attribute__((weak, alias("__timer_get_boot_us")));