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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <limits.h>
  20. #include <assert.h>
  21. #include <unistd.h>
  22. #include <gpxe/timer.h>
  23. #include <gpxe/efi/efi.h>
  24. #include <gpxe/efi/Protocol/Cpu.h>
  25. /** @file
  26. *
  27. * gPXE timer API for EFI
  28. *
  29. */
  30. /** Scale factor to apply to CPU timer 0
  31. *
  32. * The timer is scaled down in order to ensure that reasonable values
  33. * for "number of ticks" don't exceed the size of an unsigned long.
  34. */
  35. #define EFI_TIMER0_SHIFT 12
  36. /** Calibration time */
  37. #define EFI_CALIBRATE_DELAY_MS 1
  38. /** CPU protocol */
  39. static EFI_CPU_ARCH_PROTOCOL *cpu_arch;
  40. EFI_REQUIRE_PROTOCOL ( EFI_CPU_ARCH_PROTOCOL, &cpu_arch );
  41. /**
  42. * Delay for a fixed number of microseconds
  43. *
  44. * @v usecs Number of microseconds for which to delay
  45. */
  46. static void efi_udelay ( unsigned long usecs ) {
  47. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  48. EFI_STATUS efirc;
  49. if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
  50. DBG ( "EFI could not delay for %ldus: %s\n",
  51. usecs, efi_strerror ( efirc ) );
  52. /* Probably screwed */
  53. }
  54. }
  55. /**
  56. * Get current system time in ticks
  57. *
  58. * @ret ticks Current time, in ticks
  59. */
  60. static unsigned long efi_currticks ( void ) {
  61. UINT64 time;
  62. EFI_STATUS efirc;
  63. /* Read CPU timer 0 (TSC) */
  64. if ( ( efirc = cpu_arch->GetTimerValue ( cpu_arch, 0, &time,
  65. NULL ) ) != 0 ) {
  66. DBG ( "EFI could not read CPU timer: %s\n",
  67. efi_strerror ( efirc ) );
  68. /* Probably screwed */
  69. return -1UL;
  70. }
  71. return ( time >> EFI_TIMER0_SHIFT );
  72. }
  73. /**
  74. * Get number of ticks per second
  75. *
  76. * @ret ticks_per_sec Number of ticks per second
  77. */
  78. static unsigned long efi_ticks_per_sec ( void ) {
  79. static unsigned long ticks_per_sec = 0;
  80. /* Calibrate timer, if necessary. EFI does nominally provide
  81. * the timer speed via the (optional) TimerPeriod parameter to
  82. * the GetTimerValue() call, but it gets the speed slightly
  83. * wrong. By up to three orders of magnitude. Not helpful.
  84. */
  85. if ( ! ticks_per_sec ) {
  86. unsigned long start;
  87. unsigned long elapsed;
  88. DBG ( "Calibrating EFI timer with a %d ms delay\n",
  89. EFI_CALIBRATE_DELAY_MS );
  90. start = currticks();
  91. mdelay ( EFI_CALIBRATE_DELAY_MS );
  92. elapsed = ( currticks() - start );
  93. ticks_per_sec = ( elapsed * ( 1000 / EFI_CALIBRATE_DELAY_MS ));
  94. DBG ( "EFI CPU timer calibrated at %ld ticks in %d ms (%ld "
  95. "ticks/sec)\n", elapsed, EFI_CALIBRATE_DELAY_MS,
  96. ticks_per_sec );
  97. }
  98. return ticks_per_sec;
  99. }
  100. PROVIDE_TIMER ( efi, udelay, efi_udelay );
  101. PROVIDE_TIMER ( efi, currticks, efi_currticks );
  102. PROVIDE_TIMER ( efi, ticks_per_sec, efi_ticks_per_sec );