Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

efi_timer.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: %lx\n",
  50. usecs, 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: %lx\n", efirc );
  66. /* Probably screwed */
  67. return -1UL;
  68. }
  69. return ( time >> EFI_TIMER0_SHIFT );
  70. }
  71. /**
  72. * Get number of ticks per second
  73. *
  74. * @ret ticks_per_sec Number of ticks per second
  75. */
  76. static unsigned long efi_ticks_per_sec ( void ) {
  77. static unsigned long ticks_per_sec = 0;
  78. /* Calibrate timer, if necessary. EFI does nominally provide
  79. * the timer speed via the (optional) TimerPeriod parameter to
  80. * the GetTimerValue() call, but it gets the speed slightly
  81. * wrong. By up to three orders of magnitude. Not helpful.
  82. */
  83. if ( ! ticks_per_sec ) {
  84. unsigned long start;
  85. unsigned long elapsed;
  86. DBG ( "Calibrating EFI timer with a %d ms delay\n",
  87. EFI_CALIBRATE_DELAY_MS );
  88. start = currticks();
  89. mdelay ( EFI_CALIBRATE_DELAY_MS );
  90. elapsed = ( currticks() - start );
  91. ticks_per_sec = ( elapsed * ( 1000 / EFI_CALIBRATE_DELAY_MS ));
  92. DBG ( "EFI CPU timer calibrated at %ld ticks in %d ms (%ld "
  93. "ticks/sec)\n", elapsed, EFI_CALIBRATE_DELAY_MS,
  94. ticks_per_sec );
  95. }
  96. return ticks_per_sec;
  97. }
  98. PROVIDE_TIMER ( efi, udelay, efi_udelay );
  99. PROVIDE_TIMER ( efi, currticks, efi_currticks );
  100. PROVIDE_TIMER ( efi, ticks_per_sec, efi_ticks_per_sec );