You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_timer.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <limits.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <ipxe/timer.h>
  24. #include <ipxe/efi/efi.h>
  25. #include <ipxe/efi/Protocol/Cpu.h>
  26. /** @file
  27. *
  28. * iPXE timer API for EFI
  29. *
  30. */
  31. /** Scale factor to apply to CPU timer 0
  32. *
  33. * The timer is scaled down in order to ensure that reasonable values
  34. * for "number of ticks" don't exceed the size of an unsigned long.
  35. */
  36. #define EFI_TIMER0_SHIFT 12
  37. /** Calibration time */
  38. #define EFI_CALIBRATE_DELAY_MS 1
  39. /** CPU protocol */
  40. static EFI_CPU_ARCH_PROTOCOL *cpu_arch;
  41. EFI_REQUIRE_PROTOCOL ( EFI_CPU_ARCH_PROTOCOL, &cpu_arch );
  42. /**
  43. * Delay for a fixed number of microseconds
  44. *
  45. * @v usecs Number of microseconds for which to delay
  46. */
  47. static void efi_udelay ( unsigned long usecs ) {
  48. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  49. EFI_STATUS efirc;
  50. if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
  51. DBG ( "EFI could not delay for %ldus: %s\n",
  52. usecs, efi_strerror ( efirc ) );
  53. /* Probably screwed */
  54. }
  55. }
  56. /**
  57. * Get current system time in ticks
  58. *
  59. * @ret ticks Current time, in ticks
  60. */
  61. static unsigned long efi_currticks ( void ) {
  62. UINT64 time;
  63. EFI_STATUS efirc;
  64. /* Read CPU timer 0 (TSC) */
  65. if ( ( efirc = cpu_arch->GetTimerValue ( cpu_arch, 0, &time,
  66. NULL ) ) != 0 ) {
  67. DBG ( "EFI could not read CPU timer: %s\n",
  68. efi_strerror ( efirc ) );
  69. /* Probably screwed */
  70. return -1UL;
  71. }
  72. return ( time >> EFI_TIMER0_SHIFT );
  73. }
  74. /**
  75. * Get number of ticks per second
  76. *
  77. * @ret ticks_per_sec Number of ticks per second
  78. */
  79. static unsigned long efi_ticks_per_sec ( void ) {
  80. static unsigned long ticks_per_sec = 0;
  81. /* Calibrate timer, if necessary. EFI does nominally provide
  82. * the timer speed via the (optional) TimerPeriod parameter to
  83. * the GetTimerValue() call, but it gets the speed slightly
  84. * wrong. By up to three orders of magnitude. Not helpful.
  85. */
  86. if ( ! ticks_per_sec ) {
  87. unsigned long start;
  88. unsigned long elapsed;
  89. DBG ( "Calibrating EFI timer with a %d ms delay\n",
  90. EFI_CALIBRATE_DELAY_MS );
  91. start = currticks();
  92. mdelay ( EFI_CALIBRATE_DELAY_MS );
  93. elapsed = ( currticks() - start );
  94. ticks_per_sec = ( elapsed * ( 1000 / EFI_CALIBRATE_DELAY_MS ));
  95. DBG ( "EFI CPU timer calibrated at %ld ticks in %d ms (%ld "
  96. "ticks/sec)\n", elapsed, EFI_CALIBRATE_DELAY_MS,
  97. ticks_per_sec );
  98. }
  99. return ticks_per_sec;
  100. }
  101. PROVIDE_TIMER ( efi, udelay, efi_udelay );
  102. PROVIDE_TIMER ( efi, currticks, efi_currticks );
  103. PROVIDE_TIMER ( efi, ticks_per_sec, efi_ticks_per_sec );