bootstage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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_SPL,
  148. BOOTSTAGE_ID_END_SPL,
  149. BOOTSTAGE_ID_START_UBOOT_F,
  150. BOOTSTAGE_ID_START_UBOOT_R,
  151. BOOTSTAGE_ID_USB_START,
  152. BOOTSTAGE_ID_ETH_START,
  153. BOOTSTAGE_ID_BOOTP_START,
  154. BOOTSTAGE_ID_BOOTP_STOP,
  155. BOOTSTAGE_ID_BOOTM_START,
  156. BOOTSTAGE_ID_BOOTM_HANDOFF,
  157. BOOTSTAGE_ID_MAIN_LOOP,
  158. BOOTSTAGE_KERNELREAD_START,
  159. BOOTSTAGE_KERNELREAD_STOP,
  160. BOOTSTAGE_ID_BOARD_INIT,
  161. BOOTSTAGE_ID_BOARD_INIT_DONE,
  162. BOOTSTAGE_ID_CPU_AWAKE,
  163. BOOTSTAGE_ID_MAIN_CPU_AWAKE,
  164. BOOTSTAGE_ID_MAIN_CPU_READY,
  165. BOOTSTAGE_ID_ACCUM_LCD,
  166. BOOTSTAGE_ID_ACCUM_SCSI,
  167. BOOTSTAGE_ID_ACCUM_SPI,
  168. BOOTSTAGE_ID_ACCUM_DECOMP,
  169. BOOTSTAGE_ID_ACCUM_OF_LIVE,
  170. BOOTSTAGE_ID_FPGA_INIT,
  171. BOOTSTATE_ID_ACCUM_DM_SPL,
  172. BOOTSTATE_ID_ACCUM_DM_F,
  173. BOOTSTATE_ID_ACCUM_DM_R,
  174. /* a few spare for the user, from here */
  175. BOOTSTAGE_ID_USER,
  176. BOOTSTAGE_ID_ALLOC,
  177. };
  178. /*
  179. * Return the time since boot in microseconds, This is needed for bootstage
  180. * and should be defined in CPU- or board-specific code. If undefined then
  181. * you will get a link error.
  182. */
  183. ulong timer_get_boot_us(void);
  184. #if defined(USE_HOSTCC)
  185. #define show_boot_progress(val) do {} while (0)
  186. #else
  187. /**
  188. * Board code can implement show_boot_progress() if needed.
  189. *
  190. * @param val Progress state (enum bootstage_id), or -id if an error
  191. * has occurred.
  192. */
  193. void show_boot_progress(int val);
  194. #endif
  195. #if !defined(USE_HOSTCC)
  196. #if CONFIG_IS_ENABLED(BOOTSTAGE)
  197. #define ENABLE_BOOTSTAGE
  198. #endif
  199. #endif
  200. #ifdef ENABLE_BOOTSTAGE
  201. /* This is the full bootstage implementation */
  202. /**
  203. * Relocate existing bootstage records
  204. *
  205. * Call this after relocation has happened and after malloc has been initted.
  206. * We need to copy any pointers in bootstage records that were added pre-
  207. * relocation, since memory can be overwritten later.
  208. * @return Always returns 0, to indicate success
  209. */
  210. int bootstage_relocate(void);
  211. /**
  212. * Add a new bootstage record
  213. *
  214. * @param id Bootstage ID to use (ignored if flags & BOOTSTAGEF_ALLOC)
  215. * @param name Name of record, or NULL for none
  216. * @param flags Flags (BOOTSTAGEF_...)
  217. * @param mark Time to record in this record, in microseconds
  218. */
  219. ulong bootstage_add_record(enum bootstage_id id, const char *name,
  220. int flags, ulong mark);
  221. /**
  222. * Mark a time stamp for the current boot stage.
  223. */
  224. ulong bootstage_mark(enum bootstage_id id);
  225. ulong bootstage_error(enum bootstage_id id);
  226. ulong bootstage_mark_name(enum bootstage_id id, const char *name);
  227. /**
  228. * Mark a time stamp in the given function and line number
  229. *
  230. * See BOOTSTAGE_MARKER() for a convenient macro.
  231. *
  232. * @param file Filename to record (NULL if none)
  233. * @param func Function name to record
  234. * @param linenum Line number to record
  235. * @return recorded time stamp
  236. */
  237. ulong bootstage_mark_code(const char *file, const char *func,
  238. int linenum);
  239. /**
  240. * Mark the start of a bootstage activity. The end will be marked later with
  241. * bootstage_accum() and at that point we accumulate the time taken. Calling
  242. * this function turns the given id into a accumulator rather than and
  243. * absolute mark in time. Accumulators record the total amount of time spent
  244. * in an activty during boot.
  245. *
  246. * @param id Bootstage id to record this timestamp against
  247. * @param name Textual name to display for this id in the report (maybe NULL)
  248. * @return start timestamp in microseconds
  249. */
  250. uint32_t bootstage_start(enum bootstage_id id, const char *name);
  251. /**
  252. * Mark the end of a bootstage activity
  253. *
  254. * After previously marking the start of an activity with bootstage_start(),
  255. * call this function to mark the end. You can call these functions in pairs
  256. * as many times as you like.
  257. *
  258. * @param id Bootstage id to record this timestamp against
  259. * @return time spent in this iteration of the activity (i.e. the time now
  260. * less the start time recorded in the last bootstage_start() call
  261. * with this id.
  262. */
  263. uint32_t bootstage_accum(enum bootstage_id id);
  264. /* Print a report about boot time */
  265. void bootstage_report(void);
  266. /**
  267. * Add bootstage information to the device tree
  268. *
  269. * @return 0 if ok, -ve on error
  270. */
  271. int bootstage_fdt_add_report(void);
  272. /**
  273. * Stash bootstage data into memory
  274. *
  275. * @param base Base address of memory buffer
  276. * @param size Size of memory buffer
  277. * @return 0 if stashed ok, -1 if out of space
  278. */
  279. int bootstage_stash(void *base, int size);
  280. /**
  281. * Read bootstage data from memory
  282. *
  283. * Bootstage data is read from memory and placed in the bootstage table
  284. * in the user records.
  285. *
  286. * @param base Base address of memory buffer
  287. * @param size Size of memory buffer (-1 if unknown)
  288. * @return 0 if unstashed ok, -ENOENT if bootstage info not found, -ENOSPC if
  289. * there is not space for read the stacked data, or other error if
  290. * something else went wrong
  291. */
  292. int bootstage_unstash(const void *base, int size);
  293. /**
  294. * bootstage_get_size() - Get the size of the bootstage data
  295. *
  296. * @return size of boostage data in bytes
  297. */
  298. int bootstage_get_size(void);
  299. /**
  300. * bootstage_init() - Prepare bootstage for use
  301. *
  302. * @first: true if this is the first time bootstage is set up. This causes it
  303. * to add a 'reset' record with a time of 0.
  304. */
  305. int bootstage_init(bool first);
  306. #else
  307. static inline ulong bootstage_add_record(enum bootstage_id id,
  308. const char *name, int flags, ulong mark)
  309. {
  310. return 0;
  311. }
  312. /*
  313. * This is a dummy implementation which just calls show_boot_progress(),
  314. * and won't even do that unless CONFIG_SHOW_BOOT_PROGRESS is defined
  315. */
  316. static inline int bootstage_relocate(void)
  317. {
  318. return 0;
  319. }
  320. static inline ulong bootstage_mark(enum bootstage_id id)
  321. {
  322. show_boot_progress(id);
  323. return 0;
  324. }
  325. static inline ulong bootstage_error(enum bootstage_id id)
  326. {
  327. show_boot_progress(-id);
  328. return 0;
  329. }
  330. static inline ulong bootstage_mark_name(enum bootstage_id id, const char *name)
  331. {
  332. show_boot_progress(id);
  333. return 0;
  334. }
  335. static inline ulong bootstage_mark_code(const char *file, const char *func,
  336. int linenum)
  337. {
  338. return 0;
  339. }
  340. static inline uint32_t bootstage_start(enum bootstage_id id, const char *name)
  341. {
  342. return 0;
  343. }
  344. static inline uint32_t bootstage_accum(enum bootstage_id id)
  345. {
  346. return 0;
  347. }
  348. static inline int bootstage_stash(void *base, int size)
  349. {
  350. return 0; /* Pretend to succeed */
  351. }
  352. static inline int bootstage_unstash(const void *base, int size)
  353. {
  354. return 0; /* Pretend to succeed */
  355. }
  356. static inline int bootstage_get_size(void)
  357. {
  358. return 0;
  359. }
  360. static inline int bootstage_init(bool first)
  361. {
  362. return 0;
  363. }
  364. #endif /* ENABLE_BOOTSTAGE */
  365. /* Helper macro for adding a bootstage to a line of code */
  366. #define BOOTSTAGE_MARKER() \
  367. bootstage_mark_code(__FILE__, __func__, __LINE__)
  368. #endif