bootstage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * This file implements recording of each stage of the boot process. It is
  4. * intended to implement timing of each stage, reporting this information
  5. * to the user and passing it to the OS for logging / further analysis.
  6. * Note that it requires timer_get_boot_us() to be defined by the board
  7. *
  8. * Copyright (c) 2011 The Chromium OS Authors.
  9. */
  10. #ifndef _BOOTSTAGE_H
  11. #define _BOOTSTAGE_H
  12. /* Flags for each bootstage record */
  13. enum bootstage_flags {
  14. BOOTSTAGEF_ERROR = 1 << 0, /* Error record */
  15. BOOTSTAGEF_ALLOC = 1 << 1, /* Allocate an id */
  16. };
  17. /* bootstate sub-IDs used for kernel and ramdisk ranges */
  18. enum {
  19. BOOTSTAGE_SUB_FORMAT,
  20. BOOTSTAGE_SUB_FORMAT_OK,
  21. BOOTSTAGE_SUB_NO_UNIT_NAME,
  22. BOOTSTAGE_SUB_UNIT_NAME,
  23. BOOTSTAGE_SUB_SUBNODE,
  24. BOOTSTAGE_SUB_CHECK,
  25. BOOTSTAGE_SUB_HASH = 5,
  26. BOOTSTAGE_SUB_CHECK_ARCH = 5,
  27. BOOTSTAGE_SUB_CHECK_ALL,
  28. BOOTSTAGE_SUB_GET_DATA,
  29. BOOTSTAGE_SUB_CHECK_ALL_OK = 7,
  30. BOOTSTAGE_SUB_GET_DATA_OK,
  31. BOOTSTAGE_SUB_LOAD,
  32. };
  33. /*
  34. * A list of boot stages that we know about. Each of these indicates the
  35. * state that we are at, and the action that we are about to perform. For
  36. * errors, we issue an error for an item when it fails. Therefore the
  37. * normal sequence is:
  38. *
  39. * progress action1
  40. * progress action2
  41. * progress action3
  42. *
  43. * and an error condition where action 3 failed would be:
  44. *
  45. * progress action1
  46. * progress action2
  47. * progress action3
  48. * error on action3
  49. */
  50. enum bootstage_id {
  51. BOOTSTAGE_ID_START = 0,
  52. BOOTSTAGE_ID_CHECK_MAGIC, /* Checking image magic */
  53. BOOTSTAGE_ID_CHECK_HEADER, /* Checking image header */
  54. BOOTSTAGE_ID_CHECK_CHECKSUM, /* Checking image checksum */
  55. BOOTSTAGE_ID_CHECK_ARCH, /* Checking architecture */
  56. BOOTSTAGE_ID_CHECK_IMAGETYPE = 5,/* Checking image type */
  57. BOOTSTAGE_ID_DECOMP_IMAGE, /* Decompressing image */
  58. BOOTSTAGE_ID_KERNEL_LOADED, /* Kernel has been loaded */
  59. BOOTSTAGE_ID_DECOMP_UNIMPL = 7, /* Odd decompression algorithm */
  60. BOOTSTAGE_ID_CHECK_BOOT_OS, /* Calling OS-specific boot function */
  61. BOOTSTAGE_ID_BOOT_OS_RETURNED, /* Tried to boot OS, but it returned */
  62. BOOTSTAGE_ID_CHECK_RAMDISK = 9, /* Checking ram disk */
  63. BOOTSTAGE_ID_RD_MAGIC, /* Checking ram disk magic */
  64. BOOTSTAGE_ID_RD_HDR_CHECKSUM, /* Checking ram disk heder checksum */
  65. BOOTSTAGE_ID_RD_CHECKSUM, /* Checking ram disk checksum */
  66. BOOTSTAGE_ID_COPY_RAMDISK = 12, /* Copying ram disk into place */
  67. BOOTSTAGE_ID_RAMDISK, /* Checking for valid ramdisk */
  68. BOOTSTAGE_ID_NO_RAMDISK, /* No ram disk found (not an error) */
  69. BOOTSTAGE_ID_RUN_OS = 15, /* Exiting U-Boot, entering OS */
  70. BOOTSTAGE_ID_NEED_RESET = 30,
  71. BOOTSTAGE_ID_POST_FAIL, /* Post failure */
  72. BOOTSTAGE_ID_POST_FAIL_R, /* Post failure reported after reloc */
  73. /*
  74. * This set is reported only by x86, and the meaning is different. In
  75. * this case we are reporting completion of a particular stage.
  76. * This should probably change in the x86 code (which doesn't report
  77. * errors in any case), but discussion this can perhaps wait until we
  78. * have a generic board implementation.
  79. */
  80. BOOTSTAGE_ID_BOARD_INIT_R, /* We have relocated */
  81. BOOTSTAGE_ID_BOARD_GLOBAL_DATA, /* Global data is set up */
  82. BOOTSTAGE_ID_BOARD_INIT_SEQ, /* We completed the init sequence */
  83. BOOTSTAGE_ID_BOARD_FLASH, /* We have configured flash banks */
  84. BOOTSTAGE_ID_BOARD_FLASH_37, /* In case you didn't hear... */
  85. BOOTSTAGE_ID_BOARD_ENV, /* Environment is relocated & ready */
  86. BOOTSTAGE_ID_BOARD_PCI, /* PCI is up */
  87. BOOTSTAGE_ID_BOARD_INTERRUPTS, /* Exceptions / interrupts ready */
  88. BOOTSTAGE_ID_BOARD_DONE, /* Board init done, off to main loop */
  89. /* ^^^ here ends the x86 sequence */
  90. /* Boot stages related to loading a kernel from an IDE device */
  91. BOOTSTAGE_ID_IDE_START = 41,
  92. BOOTSTAGE_ID_IDE_ADDR,
  93. BOOTSTAGE_ID_IDE_BOOT_DEVICE,
  94. BOOTSTAGE_ID_IDE_TYPE,
  95. BOOTSTAGE_ID_IDE_PART,
  96. BOOTSTAGE_ID_IDE_PART_INFO,
  97. BOOTSTAGE_ID_IDE_PART_TYPE,
  98. BOOTSTAGE_ID_IDE_PART_READ,
  99. BOOTSTAGE_ID_IDE_FORMAT,
  100. BOOTSTAGE_ID_IDE_CHECKSUM, /* 50 */
  101. BOOTSTAGE_ID_IDE_READ,
  102. /* Boot stages related to loading a kernel from an NAND device */
  103. BOOTSTAGE_ID_NAND_PART,
  104. BOOTSTAGE_ID_NAND_SUFFIX,
  105. BOOTSTAGE_ID_NAND_BOOT_DEVICE,
  106. BOOTSTAGE_ID_NAND_HDR_READ = 55,
  107. BOOTSTAGE_ID_NAND_AVAILABLE = 55,
  108. BOOTSTAGE_ID_NAND_TYPE = 57,
  109. BOOTSTAGE_ID_NAND_READ,
  110. /* Boot stages related to loading a kernel from an network device */
  111. BOOTSTAGE_ID_NET_CHECKSUM = 60,
  112. BOOTSTAGE_ID_NET_ETH_START = 64,
  113. BOOTSTAGE_ID_NET_ETH_INIT,
  114. BOOTSTAGE_ID_NET_START = 80,
  115. BOOTSTAGE_ID_NET_NETLOOP_OK,
  116. BOOTSTAGE_ID_NET_LOADED,
  117. BOOTSTAGE_ID_NET_DONE_ERR,
  118. BOOTSTAGE_ID_NET_DONE,
  119. BOOTSTAGE_ID_FIT_FDT_START = 90,
  120. /*
  121. * Boot stages related to loading a FIT image. Some of these are a
  122. * bit wonky.
  123. */
  124. BOOTSTAGE_ID_FIT_KERNEL_START = 100,
  125. BOOTSTAGE_ID_FIT_CONFIG = 110,
  126. BOOTSTAGE_ID_FIT_TYPE,
  127. BOOTSTAGE_ID_FIT_KERNEL_INFO,
  128. BOOTSTAGE_ID_FIT_COMPRESSION,
  129. BOOTSTAGE_ID_FIT_OS,
  130. BOOTSTAGE_ID_FIT_LOADADDR,
  131. BOOTSTAGE_ID_OVERWRITTEN,
  132. /* Next 10 IDs used by BOOTSTAGE_SUB_... */
  133. BOOTSTAGE_ID_FIT_RD_START = 120, /* Ramdisk stages */
  134. /* Next 10 IDs used by BOOTSTAGE_SUB_... */
  135. BOOTSTAGE_ID_FIT_SETUP_START = 130, /* x86 setup stages */
  136. BOOTSTAGE_ID_IDE_FIT_READ = 140,
  137. BOOTSTAGE_ID_IDE_FIT_READ_OK,
  138. BOOTSTAGE_ID_NAND_FIT_READ = 150,
  139. BOOTSTAGE_ID_NAND_FIT_READ_OK,
  140. BOOTSTAGE_ID_FIT_LOADABLE_START = 160, /* for Loadable Images */
  141. /*
  142. * These boot stages are new, higher level, and not directly related
  143. * to the old boot progress numbers. They are useful for recording
  144. * rough boot timing information.
  145. */
  146. BOOTSTAGE_ID_AWAKE,
  147. BOOTSTAGE_ID_START_TPL,
  148. BOOTSTAGE_ID_END_TPL,
  149. BOOTSTAGE_ID_START_SPL,
  150. BOOTSTAGE_ID_END_SPL,
  151. BOOTSTAGE_ID_START_UBOOT_F,
  152. BOOTSTAGE_ID_START_UBOOT_R,
  153. BOOTSTAGE_ID_USB_START,
  154. BOOTSTAGE_ID_ETH_START,
  155. BOOTSTAGE_ID_BOOTP_START,
  156. BOOTSTAGE_ID_BOOTP_STOP,
  157. BOOTSTAGE_ID_BOOTM_START,
  158. BOOTSTAGE_ID_BOOTM_HANDOFF,
  159. BOOTSTAGE_ID_MAIN_LOOP,
  160. BOOTSTAGE_ID_ENTER_CLI_LOOP,
  161. BOOTSTAGE_KERNELREAD_START,
  162. BOOTSTAGE_KERNELREAD_STOP,
  163. BOOTSTAGE_ID_BOARD_INIT,
  164. BOOTSTAGE_ID_BOARD_INIT_DONE,
  165. BOOTSTAGE_ID_CPU_AWAKE,
  166. BOOTSTAGE_ID_MAIN_CPU_AWAKE,
  167. BOOTSTAGE_ID_MAIN_CPU_READY,
  168. BOOTSTAGE_ID_ACCUM_LCD,
  169. BOOTSTAGE_ID_ACCUM_SCSI,
  170. BOOTSTAGE_ID_ACCUM_SPI,
  171. BOOTSTAGE_ID_ACCUM_DECOMP,
  172. BOOTSTAGE_ID_ACCUM_OF_LIVE,
  173. BOOTSTAGE_ID_FPGA_INIT,
  174. BOOTSTATE_ID_ACCUM_DM_SPL,
  175. BOOTSTATE_ID_ACCUM_DM_F,
  176. BOOTSTATE_ID_ACCUM_DM_R,
  177. /* a few spare for the user, from here */
  178. BOOTSTAGE_ID_USER,
  179. BOOTSTAGE_ID_ALLOC,
  180. };
  181. /*
  182. * Return the time since boot in microseconds, This is needed for bootstage
  183. * and should be defined in CPU- or board-specific code. If undefined then
  184. * you will get a link error.
  185. */
  186. ulong timer_get_boot_us(void);
  187. #if defined(USE_HOSTCC)
  188. #define show_boot_progress(val) do {} while (0)
  189. #else
  190. /**
  191. * Board code can implement show_boot_progress() if needed.
  192. *
  193. * @param val Progress state (enum bootstage_id), or -id if an error
  194. * has occurred.
  195. */
  196. void show_boot_progress(int val);
  197. #endif
  198. #if !defined(USE_HOSTCC)
  199. #if CONFIG_IS_ENABLED(BOOTSTAGE)
  200. #define ENABLE_BOOTSTAGE
  201. #endif
  202. #endif
  203. #ifdef ENABLE_BOOTSTAGE
  204. /* This is the full bootstage implementation */
  205. /**
  206. * Relocate existing bootstage records
  207. *
  208. * Call this after relocation has happened and after malloc has been initted.
  209. * We need to copy any pointers in bootstage records that were added pre-
  210. * relocation, since memory can be overwritten later.
  211. * @return Always returns 0, to indicate success
  212. */
  213. int bootstage_relocate(void);
  214. /**
  215. * Add a new bootstage record
  216. *
  217. * @param id Bootstage ID to use (ignored if flags & BOOTSTAGEF_ALLOC)
  218. * @param name Name of record, or NULL for none
  219. * @param flags Flags (BOOTSTAGEF_...)
  220. * @param mark Time to record in this record, in microseconds
  221. */
  222. ulong bootstage_add_record(enum bootstage_id id, const char *name,
  223. int flags, ulong mark);
  224. /**
  225. * Mark a time stamp for the current boot stage.
  226. */
  227. ulong bootstage_mark(enum bootstage_id id);
  228. ulong bootstage_error(enum bootstage_id id);
  229. ulong bootstage_mark_name(enum bootstage_id id, const char *name);
  230. /**
  231. * Mark a time stamp in the given function and line number
  232. *
  233. * See BOOTSTAGE_MARKER() for a convenient macro.
  234. *
  235. * @param file Filename to record (NULL if none)
  236. * @param func Function name to record
  237. * @param linenum Line number to record
  238. * @return recorded time stamp
  239. */
  240. ulong bootstage_mark_code(const char *file, const char *func,
  241. int linenum);
  242. /**
  243. * Mark the start of a bootstage activity. The end will be marked later with
  244. * bootstage_accum() and at that point we accumulate the time taken. Calling
  245. * this function turns the given id into a accumulator rather than and
  246. * absolute mark in time. Accumulators record the total amount of time spent
  247. * in an activty during boot.
  248. *
  249. * @param id Bootstage id to record this timestamp against
  250. * @param name Textual name to display for this id in the report (maybe NULL)
  251. * @return start timestamp in microseconds
  252. */
  253. uint32_t bootstage_start(enum bootstage_id id, const char *name);
  254. /**
  255. * Mark the end of a bootstage activity
  256. *
  257. * After previously marking the start of an activity with bootstage_start(),
  258. * call this function to mark the end. You can call these functions in pairs
  259. * as many times as you like.
  260. *
  261. * @param id Bootstage id to record this timestamp against
  262. * @return time spent in this iteration of the activity (i.e. the time now
  263. * less the start time recorded in the last bootstage_start() call
  264. * with this id.
  265. */
  266. uint32_t bootstage_accum(enum bootstage_id id);
  267. /* Print a report about boot time */
  268. void bootstage_report(void);
  269. /**
  270. * Add bootstage information to the device tree
  271. *
  272. * @return 0 if ok, -ve on error
  273. */
  274. int bootstage_fdt_add_report(void);
  275. /**
  276. * Stash bootstage data into memory
  277. *
  278. * @param base Base address of memory buffer
  279. * @param size Size of memory buffer
  280. * @return 0 if stashed ok, -1 if out of space
  281. */
  282. int bootstage_stash(void *base, int size);
  283. /**
  284. * Read bootstage data from memory
  285. *
  286. * Bootstage data is read from memory and placed in the bootstage table
  287. * in the user records.
  288. *
  289. * @param base Base address of memory buffer
  290. * @param size Size of memory buffer (-1 if unknown)
  291. * @return 0 if unstashed ok, -ENOENT if bootstage info not found, -ENOSPC if
  292. * there is not space for read the stacked data, or other error if
  293. * something else went wrong
  294. */
  295. int bootstage_unstash(const void *base, int size);
  296. /**
  297. * bootstage_get_size() - Get the size of the bootstage data
  298. *
  299. * @return size of boostage data in bytes
  300. */
  301. int bootstage_get_size(void);
  302. /**
  303. * bootstage_init() - Prepare bootstage for use
  304. *
  305. * @first: true if this is the first time bootstage is set up. This causes it
  306. * to add a 'reset' record with a time of 0.
  307. */
  308. int bootstage_init(bool first);
  309. #else
  310. static inline ulong bootstage_add_record(enum bootstage_id id,
  311. const char *name, int flags, ulong mark)
  312. {
  313. return 0;
  314. }
  315. /*
  316. * This is a dummy implementation which just calls show_boot_progress(),
  317. * and won't even do that unless CONFIG_SHOW_BOOT_PROGRESS is defined
  318. */
  319. static inline int bootstage_relocate(void)
  320. {
  321. return 0;
  322. }
  323. static inline ulong bootstage_mark(enum bootstage_id id)
  324. {
  325. show_boot_progress(id);
  326. return 0;
  327. }
  328. static inline ulong bootstage_error(enum bootstage_id id)
  329. {
  330. show_boot_progress(-id);
  331. return 0;
  332. }
  333. static inline ulong bootstage_mark_name(enum bootstage_id id, const char *name)
  334. {
  335. show_boot_progress(id);
  336. return 0;
  337. }
  338. static inline ulong bootstage_mark_code(const char *file, const char *func,
  339. int linenum)
  340. {
  341. return 0;
  342. }
  343. static inline uint32_t bootstage_start(enum bootstage_id id, const char *name)
  344. {
  345. return 0;
  346. }
  347. static inline uint32_t bootstage_accum(enum bootstage_id id)
  348. {
  349. return 0;
  350. }
  351. static inline int bootstage_stash(void *base, int size)
  352. {
  353. return 0; /* Pretend to succeed */
  354. }
  355. static inline int bootstage_unstash(const void *base, int size)
  356. {
  357. return 0; /* Pretend to succeed */
  358. }
  359. static inline int bootstage_get_size(void)
  360. {
  361. return 0;
  362. }
  363. static inline int bootstage_init(bool first)
  364. {
  365. return 0;
  366. }
  367. #endif /* ENABLE_BOOTSTAGE */
  368. /* Helper macro for adding a bootstage to a line of code */
  369. #define BOOTSTAGE_MARKER() \
  370. bootstage_mark_code(__FILE__, __func__, __LINE__)
  371. #endif