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

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