bootstage.h 12 KB

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