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

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