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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <ipxe/timer.h>
  28. #include <ipxe/init.h>
  29. #include <ipxe/efi/efi.h>
  30. /** @file
  31. *
  32. * iPXE timer API for EFI
  33. *
  34. */
  35. /** Current tick count */
  36. static unsigned long efi_jiffies;
  37. /** Timer tick event */
  38. static EFI_EVENT efi_tick_event;
  39. /** Colour for debug messages */
  40. #define colour &efi_jiffies
  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. int rc;
  50. if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
  51. rc = -EEFI ( efirc );
  52. DBGC ( colour, "EFI could not delay for %ldus: %s\n",
  53. usecs, strerror ( rc ) );
  54. /* Probably screwed */
  55. }
  56. }
  57. /**
  58. * Get current system time in ticks
  59. *
  60. * @ret ticks Current time, in ticks
  61. */
  62. static unsigned long efi_currticks ( void ) {
  63. /* EFI provides no clean way for device drivers to shut down
  64. * in preparation for handover to a booted operating system.
  65. * The platform firmware simply doesn't bother to call the
  66. * drivers' Stop() methods. Instead, drivers must register an
  67. * EVT_SIGNAL_EXIT_BOOT_SERVICES event to be signalled when
  68. * ExitBootServices() is called, and clean up without any
  69. * reference to the EFI driver model.
  70. *
  71. * Unfortunately, all timers silently stop working when
  72. * ExitBootServices() is called. Even more unfortunately, and
  73. * for no discernible reason, this happens before any
  74. * EVT_SIGNAL_EXIT_BOOT_SERVICES events are signalled. The
  75. * net effect of this entertaining design choice is that any
  76. * timeout loops on the shutdown path (e.g. for gracefully
  77. * closing outstanding TCP connections) may wait indefinitely.
  78. *
  79. * There is no way to report failure from currticks(), since
  80. * the API lazily assumes that the host system continues to
  81. * travel through time in the usual direction. Work around
  82. * EFI's violation of this assumption by falling back to a
  83. * simple free-running monotonic counter.
  84. */
  85. if ( efi_shutdown_in_progress )
  86. efi_jiffies++;
  87. return efi_jiffies;
  88. }
  89. /**
  90. * Timer tick
  91. *
  92. * @v event Timer tick event
  93. * @v context Event context
  94. */
  95. static EFIAPI void efi_tick ( EFI_EVENT event __unused,
  96. void *context __unused ) {
  97. /* Increment tick count */
  98. efi_jiffies++;
  99. }
  100. /**
  101. * Start timer tick
  102. *
  103. */
  104. static void efi_tick_startup ( void ) {
  105. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  106. EFI_STATUS efirc;
  107. int rc;
  108. /* Create timer tick event */
  109. if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
  110. TPL_CALLBACK, efi_tick, NULL,
  111. &efi_tick_event ) ) != 0 ) {
  112. rc = -EEFI ( efirc );
  113. DBGC ( colour, "EFI could not create timer tick: %s\n",
  114. strerror ( rc ) );
  115. /* Nothing we can do about it */
  116. return;
  117. }
  118. /* Start timer tick */
  119. if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerPeriodic,
  120. ( 10000000 / EFI_TICKS_PER_SEC ) ) ) !=0){
  121. rc = -EEFI ( efirc );
  122. DBGC ( colour, "EFI could not start timer tick: %s\n",
  123. strerror ( rc ) );
  124. /* Nothing we can do about it */
  125. return;
  126. }
  127. DBGC ( colour, "EFI timer started at %d ticks per second\n",
  128. EFI_TICKS_PER_SEC );
  129. }
  130. /**
  131. * Stop timer tick
  132. *
  133. * @v booting System is shutting down in order to boot
  134. */
  135. static void efi_tick_shutdown ( int booting __unused ) {
  136. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  137. EFI_STATUS efirc;
  138. int rc;
  139. /* Stop timer tick */
  140. if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerCancel, 0 ) ) != 0 ){
  141. rc = -EEFI ( efirc );
  142. DBGC ( colour, "EFI could not stop timer tick: %s\n",
  143. strerror ( rc ) );
  144. /* Self-destruct initiated */
  145. return;
  146. }
  147. DBGC ( colour, "EFI timer stopped\n" );
  148. /* Destroy timer tick event */
  149. if ( ( efirc = bs->CloseEvent ( efi_tick_event ) ) != 0 ) {
  150. rc = -EEFI ( efirc );
  151. DBGC ( colour, "EFI could not destroy timer tick: %s\n",
  152. strerror ( rc ) );
  153. /* Probably non-fatal */
  154. return;
  155. }
  156. }
  157. /** Timer tick startup function */
  158. struct startup_fn efi_tick_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  159. .startup = efi_tick_startup,
  160. .shutdown = efi_tick_shutdown,
  161. };
  162. PROVIDE_TIMER ( efi, udelay, efi_udelay );
  163. PROVIDE_TIMER ( efi, currticks, efi_currticks );
  164. PROVIDE_TIMER_INLINE ( efi, ticks_per_sec );