tsc_timer.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <asm/io.h>
  9. #include <asm/i8254.h>
  10. #include <asm/ibmpc.h>
  11. #include <asm/msr.h>
  12. #include <asm/u-boot-x86.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. void timer_set_base(u64 base)
  15. {
  16. gd->arch.tsc_base = base;
  17. }
  18. /*
  19. * Get the number of CPU time counter ticks since it was read first time after
  20. * restart. This yields a free running counter guaranteed to take almost 6
  21. * years to wrap around even at 100GHz clock rate.
  22. */
  23. u64 __attribute__((no_instrument_function)) get_ticks(void)
  24. {
  25. u64 now_tick = rdtsc();
  26. /* We assume that 0 means the base hasn't been set yet */
  27. if (!gd->arch.tsc_base)
  28. panic("No tick base available");
  29. return now_tick - gd->arch.tsc_base;
  30. }
  31. #define PLATFORM_INFO_MSR 0xce
  32. /* Get the speed of the TSC timer in MHz */
  33. unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void)
  34. {
  35. u32 ratio;
  36. u64 platform_info = native_read_msr(PLATFORM_INFO_MSR);
  37. /* 100MHz times Max Non Turbo ratio */
  38. ratio = (platform_info >> 8) & 0xff;
  39. return 100 * ratio;
  40. }
  41. unsigned long get_tbclk(void)
  42. {
  43. return get_tbclk_mhz() * 1000 * 1000;
  44. }
  45. static ulong get_ms_timer(void)
  46. {
  47. return (get_ticks() * 1000) / get_tbclk();
  48. }
  49. ulong get_timer(ulong base)
  50. {
  51. return get_ms_timer() - base;
  52. }
  53. ulong __attribute__((no_instrument_function)) timer_get_us(void)
  54. {
  55. return get_ticks() / get_tbclk_mhz();
  56. }
  57. ulong timer_get_boot_us(void)
  58. {
  59. return timer_get_us();
  60. }
  61. void __udelay(unsigned long usec)
  62. {
  63. u64 now = get_ticks();
  64. u64 stop;
  65. stop = now + usec * get_tbclk_mhz();
  66. while ((int64_t)(stop - get_ticks()) > 0)
  67. ;
  68. }
  69. int timer_init(void)
  70. {
  71. #ifdef CONFIG_SYS_PCAT_TIMER
  72. /* Set up the PCAT timer if required */
  73. pcat_timer_init();
  74. #endif
  75. return 0;
  76. }