iPXE
Macros | Functions | Variables
efi_timer.c File Reference

iPXE timer API for EFI More...

#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <ipxe/timer.h>
#include <ipxe/init.h>
#include <ipxe/efi/efi.h>

Go to the source code of this file.

Macros

#define EFI_JIFFIES_PER_SEC   32
 Number of jiffies per second. More...
 
#define colour   &efi_jiffies
 Colour for debug messages. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void efi_udelay (unsigned long usecs)
 Delay for a fixed number of microseconds. More...
 
static unsigned long efi_currticks (void)
 Get current system time in ticks. More...
 
static EFIAPI void efi_tick (EFI_EVENT event __unused, void *context __unused)
 Timer tick. More...
 
static void efi_tick_startup (void)
 Start timer tick. More...
 
static void efi_tick_shutdown (int booting __unused)
 Stop timer tick. More...
 
struct startup_fn efi_tick_startup_fn __startup_fn (STARTUP_EARLY)
 Timer tick startup function. More...
 
struct timer efi_timer __timer (TIMER_NORMAL)
 EFI timer. More...
 

Variables

static unsigned long efi_jiffies
 Current tick count. More...
 
static EFI_EVENT efi_tick_event
 Timer tick event. More...
 

Detailed Description

iPXE timer API for EFI

Definition in file efi_timer.c.

Macro Definition Documentation

◆ EFI_JIFFIES_PER_SEC

#define EFI_JIFFIES_PER_SEC   32

Number of jiffies per second.

This is a policy decision.

Definition at line 44 of file efi_timer.c.

◆ colour

#define colour   &efi_jiffies

Colour for debug messages.

Definition at line 53 of file efi_timer.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ efi_udelay()

static void efi_udelay ( unsigned long  usecs)
static

Delay for a fixed number of microseconds.

Parameters
usecsNumber of microseconds for which to delay

Definition at line 60 of file efi_timer.c.

60  {
62  EFI_STATUS efirc;
63  int rc;
64 
65  if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
66  rc = -EEFI ( efirc );
67  DBGC ( colour, "EFI could not delay for %ldus: %s\n",
68  usecs, strerror ( rc ) );
69  /* Probably screwed */
70  }
71 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
EFI_STALL Stall
Definition: UefiSpec.h:1974
#define colour
Colour for debug messages.
Definition: efi_timer.c:53
#define DBGC(...)
Definition: compiler.h:505
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab

References EFI_SYSTEM_TABLE::BootServices, colour, DBGC, EEFI, efi_systab, rc, EFI_BOOT_SERVICES::Stall, and strerror().

◆ efi_currticks()

static unsigned long efi_currticks ( void  )
static

Get current system time in ticks.

Return values
ticksCurrent time, in ticks

Definition at line 78 of file efi_timer.c.

78  {
80 
81  /* UEFI manages to ingeniously combine the worst aspects of
82  * both polling and interrupt-driven designs. There is no way
83  * to support proper interrupt-driven operation, since there
84  * is no way to hook in an interrupt service routine. A
85  * mockery of interrupts is provided by UEFI timers, which
86  * trigger at a preset rate and can fire at any time.
87  *
88  * We therefore have all of the downsides of a polling design
89  * (inefficiency and inability to sleep until something
90  * interesting happens) combined with all of the downsides of
91  * an interrupt-driven design (the complexity of code that
92  * could be preempted at any time).
93  *
94  * The UEFI specification expects us to litter the entire
95  * codebase with calls to RaiseTPL() as needed for sections of
96  * code that are not reentrant. Since this doesn't actually
97  * gain us any substantive benefits (since even with such
98  * calls we would still be suffering from the limitations of a
99  * polling design), we instead choose to run at TPL_CALLBACK
100  * almost all of the time, dropping to a lower TPL to allow
101  * timer ticks to occur.
102  *
103  * We record the external TPL at the point of entry into iPXE,
104  * and drop back only as far as this external TPL. This
105  * avoids the unexpected behaviour that may arise from having
106  * iPXE temporarily drop to TPL_APPLICATION in the middle of
107  * an entry point invoked at TPL_CALLBACK. The side effect is
108  * that iPXE's view of the system time is effectively frozen
109  * for the duration of any call made in to iPXE at
110  * TPL_CALLBACK or higher.
111  *
112  *
113  * For added excitement, UEFI provides no clean way for device
114  * drivers to shut down in preparation for handover to a
115  * booted operating system. The platform firmware simply
116  * doesn't bother to call the drivers' Stop() methods.
117  * Instead, all non-trivial drivers must register an
118  * EVT_SIGNAL_EXIT_BOOT_SERVICES event to be signalled when
119  * ExitBootServices() is called, and clean up without any
120  * reference to the EFI driver model.
121  *
122  * Unfortunately, all timers silently stop working when
123  * ExitBootServices() is called. Even more unfortunately, and
124  * for no discernible reason, this happens before any
125  * EVT_SIGNAL_EXIT_BOOT_SERVICES events are signalled. The
126  * net effect of this entertaining design choice is that any
127  * timeout loops on the shutdown path (e.g. for gracefully
128  * closing outstanding TCP connections) may wait indefinitely.
129  *
130  * There is no way to report failure from currticks(), since
131  * the API lazily assumes that the host system continues to
132  * travel through time in the usual direction. Work around
133  * EFI's violation of this assumption by falling back to a
134  * simple free-running monotonic counter during shutdown.
135  */
136  if ( efi_shutdown_in_progress ) {
137  efi_jiffies++;
138  } else {
140  bs->RaiseTPL ( efi_internal_tpl );
141  }
142 
143  return ( efi_jiffies * ( TICKS_PER_SEC / EFI_JIFFIES_PER_SEC ) );
144 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
#define TICKS_PER_SEC
Number of ticks per second.
Definition: timer.h:15
EFI_RAISE_TPL RaiseTPL
Definition: UefiSpec.h:1926
static unsigned long efi_jiffies
Current tick count.
Definition: efi_timer.c:47
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition: efi_init.c:52
EFI Boot Services Table.
Definition: UefiSpec.h:1917
#define EFI_JIFFIES_PER_SEC
Number of jiffies per second.
Definition: efi_timer.c:44
EFI_SYSTEM_TABLE * efi_systab
EFI_RESTORE_TPL RestoreTPL
Definition: UefiSpec.h:1927
int efi_shutdown_in_progress
EFI shutdown is in progress.
Definition: efi_init.c:58
EFI_TPL efi_external_tpl
External task priority level.
Definition: efi_init.c:55

References EFI_SYSTEM_TABLE::BootServices, efi_external_tpl, efi_internal_tpl, efi_jiffies, EFI_JIFFIES_PER_SEC, efi_shutdown_in_progress, efi_systab, EFI_BOOT_SERVICES::RaiseTPL, EFI_BOOT_SERVICES::RestoreTPL, and TICKS_PER_SEC.

◆ efi_tick()

static EFIAPI void efi_tick ( EFI_EVENT event  __unused,
void *context  __unused 
)
static

Timer tick.

Parameters
eventTimer tick event
contextEvent context

Definition at line 152 of file efi_timer.c.

153  {
154 
155  /* Increment tick count */
156  efi_jiffies++;
157 }
static unsigned long efi_jiffies
Current tick count.
Definition: efi_timer.c:47

References efi_jiffies.

Referenced by efi_tick_startup().

◆ efi_tick_startup()

static void efi_tick_startup ( void  )
static

Start timer tick.

Definition at line 163 of file efi_timer.c.

163  {
165  EFI_STATUS efirc;
166  int rc;
167 
168  /* Create timer tick event */
169  if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
171  &efi_tick_event ) ) != 0 ) {
172  rc = -EEFI ( efirc );
173  DBGC ( colour, "EFI could not create timer tick: %s\n",
174  strerror ( rc ) );
175  /* Nothing we can do about it */
176  return;
177  }
178 
179  /* Start timer tick */
180  if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerPeriodic,
181  ( 10000000 / EFI_JIFFIES_PER_SEC ) ))!=0){
182  rc = -EEFI ( efirc );
183  DBGC ( colour, "EFI could not start timer tick: %s\n",
184  strerror ( rc ) );
185  /* Nothing we can do about it */
186  return;
187  }
188  DBGC ( colour, "EFI timer started at %d ticks per second\n",
190 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static EFI_EVENT efi_tick_event
Timer tick event.
Definition: efi_timer.c:50
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
An event is to be signaled periodically at a specified interval from the current time.
Definition: UefiSpec.h:537
#define colour
Colour for debug messages.
Definition: efi_timer.c:53
#define DBGC(...)
Definition: compiler.h:505
EFI_SET_TIMER SetTimer
Definition: UefiSpec.h:1942
EFI_CREATE_EVENT CreateEvent
Definition: UefiSpec.h:1941
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
#define TPL_CALLBACK
Definition: UefiSpec.h:638
#define EVT_NOTIFY_SIGNAL
Definition: UefiSpec.h:443
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
#define EFI_JIFFIES_PER_SEC
Number of jiffies per second.
Definition: efi_timer.c:44
EFI_SYSTEM_TABLE * efi_systab
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static EFIAPI void efi_tick(EFI_EVENT event __unused, void *context __unused)
Timer tick.
Definition: efi_timer.c:152
#define EVT_TIMER
Definition: UefiSpec.h:440

References EFI_SYSTEM_TABLE::BootServices, colour, EFI_BOOT_SERVICES::CreateEvent, DBGC, EEFI, EFI_JIFFIES_PER_SEC, efi_systab, efi_tick(), efi_tick_event, EVT_NOTIFY_SIGNAL, EVT_TIMER, NULL, rc, EFI_BOOT_SERVICES::SetTimer, strerror(), TimerPeriodic, and TPL_CALLBACK.

◆ efi_tick_shutdown()

static void efi_tick_shutdown ( int booting  __unused)
static

Stop timer tick.

Parameters
bootingSystem is shutting down in order to boot

Definition at line 197 of file efi_timer.c.

197  {
199  EFI_STATUS efirc;
200  int rc;
201 
202  /* Stop timer tick */
203  if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerCancel, 0 ) ) != 0 ){
204  rc = -EEFI ( efirc );
205  DBGC ( colour, "EFI could not stop timer tick: %s\n",
206  strerror ( rc ) );
207  /* Self-destruct initiated */
208  return;
209  }
210  DBGC ( colour, "EFI timer stopped\n" );
211 
212  /* Destroy timer tick event */
213  if ( ( efirc = bs->CloseEvent ( efi_tick_event ) ) != 0 ) {
214  rc = -EEFI ( efirc );
215  DBGC ( colour, "EFI could not destroy timer tick: %s\n",
216  strerror ( rc ) );
217  /* Probably non-fatal */
218  return;
219  }
220 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static EFI_EVENT efi_tick_event
Timer tick event.
Definition: efi_timer.c:50
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
#define colour
Colour for debug messages.
Definition: efi_timer.c:53
#define DBGC(...)
Definition: compiler.h:505
EFI_CLOSE_EVENT CloseEvent
Definition: UefiSpec.h:1945
EFI_SET_TIMER SetTimer
Definition: UefiSpec.h:1942
An event's timer settings is to be cancelled and not trigger time is to be set/.
Definition: UefiSpec.h:533
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseEvent, colour, DBGC, EEFI, efi_systab, efi_tick_event, rc, EFI_BOOT_SERVICES::SetTimer, strerror(), and TimerCancel.

◆ __startup_fn()

struct startup_fn efi_tick_startup_fn __startup_fn ( STARTUP_EARLY  )

Timer tick startup function.

◆ __timer()

struct timer efi_timer __timer ( TIMER_NORMAL  )

EFI timer.

Variable Documentation

◆ efi_jiffies

unsigned long efi_jiffies
static

Current tick count.

Definition at line 47 of file efi_timer.c.

Referenced by efi_currticks(), and efi_tick().

◆ efi_tick_event

EFI_EVENT efi_tick_event
static

Timer tick event.

Definition at line 50 of file efi_timer.c.

Referenced by efi_tick_shutdown(), and efi_tick_startup().