timestamp.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is part of the coreboot project.
  3. *
  4. * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <asm/arch/timestamp.h>
  10. #include <asm/arch/sysinfo.h>
  11. #include <linux/compiler.h>
  12. struct timestamp_entry {
  13. uint32_t entry_id;
  14. uint64_t entry_stamp;
  15. } __packed;
  16. struct timestamp_table {
  17. uint64_t base_time;
  18. uint32_t max_entries;
  19. uint32_t num_entries;
  20. struct timestamp_entry entries[0]; /* Variable number of entries */
  21. } __packed;
  22. static struct timestamp_table *ts_table __attribute__((section(".data")));
  23. void timestamp_init(void)
  24. {
  25. #ifdef CONFIG_SYS_X86_TSC_TIMER
  26. uint64_t base_time;
  27. #endif
  28. ts_table = lib_sysinfo.tstamp_table;
  29. #ifdef CONFIG_SYS_X86_TSC_TIMER
  30. /*
  31. * If coreboot is built with CONFIG_COLLECT_TIMESTAMPS, use the value
  32. * of base_time in coreboot's timestamp table as our timer base,
  33. * otherwise TSC counter value will be used.
  34. *
  35. * Sometimes even coreboot is built with CONFIG_COLLECT_TIMESTAMPS,
  36. * the value of base_time in the timestamp table is still zero, so
  37. * we must exclude this case too (this is currently seen on booting
  38. * coreboot in qemu)
  39. */
  40. if (ts_table && ts_table->base_time)
  41. base_time = ts_table->base_time;
  42. else
  43. base_time = rdtsc();
  44. timer_set_base(base_time);
  45. #endif
  46. timestamp_add_now(TS_U_BOOT_INITTED);
  47. }
  48. void timestamp_add(enum timestamp_id id, uint64_t ts_time)
  49. {
  50. struct timestamp_entry *tse;
  51. if (!ts_table || (ts_table->num_entries == ts_table->max_entries))
  52. return;
  53. tse = &ts_table->entries[ts_table->num_entries++];
  54. tse->entry_id = id;
  55. tse->entry_stamp = ts_time - ts_table->base_time;
  56. }
  57. void timestamp_add_now(enum timestamp_id id)
  58. {
  59. timestamp_add(id, rdtsc());
  60. }
  61. int timestamp_add_to_bootstage(void)
  62. {
  63. uint i;
  64. if (!ts_table)
  65. return -1;
  66. for (i = 0; i < ts_table->num_entries; i++) {
  67. struct timestamp_entry *tse = &ts_table->entries[i];
  68. const char *name = NULL;
  69. switch (tse->entry_id) {
  70. case TS_START_ROMSTAGE:
  71. name = "start-romstage";
  72. break;
  73. case TS_BEFORE_INITRAM:
  74. name = "before-initram";
  75. break;
  76. case TS_DEVICE_INITIALIZE:
  77. name = "device-initialize";
  78. break;
  79. case TS_DEVICE_DONE:
  80. name = "device-done";
  81. break;
  82. case TS_SELFBOOT_JUMP:
  83. name = "selfboot-jump";
  84. break;
  85. }
  86. if (name) {
  87. bootstage_add_record(0, name, BOOTSTAGEF_ALLOC,
  88. tse->entry_stamp /
  89. get_tbclk_mhz());
  90. }
  91. }
  92. return 0;
  93. }