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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_BOOT_SERVICES *bs = efi_systab->BootServices;
  70. /* UEFI manages to ingeniously combine the worst aspects of
  71. * both polling and interrupt-driven designs. There is no way
  72. * to support proper interrupt-driven operation, since there
  73. * is no way to hook in an interrupt service routine. A
  74. * mockery of interrupts is provided by UEFI timers, which
  75. * trigger at a preset rate and can fire at any time.
  76. *
  77. * We therefore have all of the downsides of a polling design
  78. * (inefficiency and inability to sleep until something
  79. * interesting happens) combined with all of the downsides of
  80. * an interrupt-driven design (the complexity of code that
  81. * could be preempted at any time).
  82. *
  83. * The UEFI specification expects us to litter the entire
  84. * codebase with calls to RaiseTPL() as needed for sections of
  85. * code that are not reentrant. Since this doesn't actually
  86. * gain us any substantive benefits (since even with such
  87. * calls we would still be suffering from the limitations of a
  88. * polling design), we instead choose to run at TPL_CALLBACK
  89. * almost all of the time, dropping to TPL_APPLICATION to
  90. * allow timer ticks to occur.
  91. *
  92. *
  93. * For added excitement, UEFI provides no clean way for device
  94. * drivers to shut down in preparation for handover to a
  95. * booted operating system. The platform firmware simply
  96. * doesn't bother to call the drivers' Stop() methods.
  97. * Instead, all non-trivial drivers must register an
  98. * EVT_SIGNAL_EXIT_BOOT_SERVICES event to be signalled when
  99. * ExitBootServices() is called, and clean up without any
  100. * reference to the EFI driver model.
  101. *
  102. * Unfortunately, all timers silently stop working when
  103. * ExitBootServices() is called. Even more unfortunately, and
  104. * for no discernible reason, this happens before any
  105. * EVT_SIGNAL_EXIT_BOOT_SERVICES events are signalled. The
  106. * net effect of this entertaining design choice is that any
  107. * timeout loops on the shutdown path (e.g. for gracefully
  108. * closing outstanding TCP connections) may wait indefinitely.
  109. *
  110. * There is no way to report failure from currticks(), since
  111. * the API lazily assumes that the host system continues to
  112. * travel through time in the usual direction. Work around
  113. * EFI's violation of this assumption by falling back to a
  114. * simple free-running monotonic counter during shutdown.
  115. */
  116. if ( efi_shutdown_in_progress ) {
  117. efi_jiffies++;
  118. } else {
  119. bs->RestoreTPL ( TPL_APPLICATION );
  120. bs->RaiseTPL ( TPL_CALLBACK );
  121. }
  122. return ( efi_jiffies * ( TICKS_PER_SEC / EFI_JIFFIES_PER_SEC ) );
  123. }
  124. /**
  125. * Timer tick
  126. *
  127. * @v event Timer tick event
  128. * @v context Event context
  129. */
  130. static EFIAPI void efi_tick ( EFI_EVENT event __unused,
  131. void *context __unused ) {
  132. /* Increment tick count */
  133. efi_jiffies++;
  134. }
  135. /**
  136. * Start timer tick
  137. *
  138. */
  139. static void efi_tick_startup ( void ) {
  140. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  141. EFI_STATUS efirc;
  142. int rc;
  143. /* Create timer tick event */
  144. if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
  145. TPL_CALLBACK, efi_tick, NULL,
  146. &efi_tick_event ) ) != 0 ) {
  147. rc = -EEFI ( efirc );
  148. DBGC ( colour, "EFI could not create timer tick: %s\n",
  149. strerror ( rc ) );
  150. /* Nothing we can do about it */
  151. return;
  152. }
  153. /* Start timer tick */
  154. if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerPeriodic,
  155. ( 10000000 / EFI_JIFFIES_PER_SEC ) ))!=0){
  156. rc = -EEFI ( efirc );
  157. DBGC ( colour, "EFI could not start timer tick: %s\n",
  158. strerror ( rc ) );
  159. /* Nothing we can do about it */
  160. return;
  161. }
  162. DBGC ( colour, "EFI timer started at %d ticks per second\n",
  163. EFI_JIFFIES_PER_SEC );
  164. }
  165. /**
  166. * Stop timer tick
  167. *
  168. * @v booting System is shutting down in order to boot
  169. */
  170. static void efi_tick_shutdown ( int booting __unused ) {
  171. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  172. EFI_STATUS efirc;
  173. int rc;
  174. /* Stop timer tick */
  175. if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerCancel, 0 ) ) != 0 ){
  176. rc = -EEFI ( efirc );
  177. DBGC ( colour, "EFI could not stop timer tick: %s\n",
  178. strerror ( rc ) );
  179. /* Self-destruct initiated */
  180. return;
  181. }
  182. DBGC ( colour, "EFI timer stopped\n" );
  183. /* Destroy timer tick event */
  184. if ( ( efirc = bs->CloseEvent ( efi_tick_event ) ) != 0 ) {
  185. rc = -EEFI ( efirc );
  186. DBGC ( colour, "EFI could not destroy timer tick: %s\n",
  187. strerror ( rc ) );
  188. /* Probably non-fatal */
  189. return;
  190. }
  191. }
  192. /** Timer tick startup function */
  193. struct startup_fn efi_tick_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  194. .name = "efi_tick",
  195. .startup = efi_tick_startup,
  196. .shutdown = efi_tick_shutdown,
  197. };
  198. /** EFI timer */
  199. struct timer efi_timer __timer ( TIMER_NORMAL ) = {
  200. .name = "efi",
  201. .currticks = efi_currticks,
  202. .udelay = efi_udelay,
  203. };