iPXE
efi_timer.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <errno.h>
29#include <unistd.h>
30#include <ipxe/timer.h>
31#include <ipxe/init.h>
32#include <ipxe/efi/efi.h>
33
34/** @file
35 *
36 * iPXE timer API for EFI
37 *
38 */
39
40/**
41 * Number of jiffies per second
42 *
43 * This is a policy decision.
44 */
45#define EFI_JIFFIES_PER_SEC 32
46
47/** Current tick count */
48static unsigned long efi_jiffies;
49
50/** Timer tick event */
52
53/** Colour for debug messages */
54#define colour &efi_jiffies
55
56/**
57 * Delay for a fixed number of microseconds
58 *
59 * @v usecs Number of microseconds for which to delay
60 */
61static void efi_udelay ( unsigned long usecs ) {
62 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
63 EFI_STATUS efirc;
64 int rc;
65
66 if ( ( efirc = bs->Stall ( usecs ) ) != 0 ) {
67 rc = -EEFI ( efirc );
68 DBGC ( colour, "EFI could not delay for %ldus: %s\n",
69 usecs, strerror ( rc ) );
70 /* Probably screwed */
71 }
72}
73
74/**
75 * Get current system time in ticks
76 *
77 * @ret ticks Current time, in ticks
78 */
79static unsigned long efi_currticks ( void ) {
80 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
81
82 /* UEFI manages to ingeniously combine the worst aspects of
83 * both polling and interrupt-driven designs. There is no way
84 * to support proper interrupt-driven operation, since there
85 * is no way to hook in an interrupt service routine. A
86 * mockery of interrupts is provided by UEFI timers, which
87 * trigger at a preset rate and can fire at any time.
88 *
89 * We therefore have all of the downsides of a polling design
90 * (inefficiency and inability to sleep until something
91 * interesting happens) combined with all of the downsides of
92 * an interrupt-driven design (the complexity of code that
93 * could be preempted at any time).
94 *
95 * The UEFI specification expects us to litter the entire
96 * codebase with calls to RaiseTPL() as needed for sections of
97 * code that are not reentrant. Since this doesn't actually
98 * gain us any substantive benefits (since even with such
99 * calls we would still be suffering from the limitations of a
100 * polling design), we instead choose to run at TPL_CALLBACK
101 * almost all of the time, dropping to a lower TPL to allow
102 * timer ticks to occur.
103 *
104 * We record the external TPL at the point of entry into iPXE,
105 * and drop back only as far as this external TPL. This
106 * avoids the unexpected behaviour that may arise from having
107 * iPXE temporarily drop to TPL_APPLICATION in the middle of
108 * an entry point invoked at TPL_CALLBACK. The side effect is
109 * that iPXE's view of the system time is effectively frozen
110 * for the duration of any call made in to iPXE at
111 * TPL_CALLBACK or higher.
112 *
113 *
114 * For added excitement, UEFI provides no clean way for device
115 * drivers to shut down in preparation for handover to a
116 * booted operating system. The platform firmware simply
117 * doesn't bother to call the drivers' Stop() methods.
118 * Instead, all non-trivial drivers must register an
119 * EVT_SIGNAL_EXIT_BOOT_SERVICES event to be signalled when
120 * ExitBootServices() is called, and clean up without any
121 * reference to the EFI driver model.
122 *
123 * Unfortunately, all timers silently stop working when
124 * ExitBootServices() is called. Even more unfortunately, and
125 * for no discernible reason, this happens before any
126 * EVT_SIGNAL_EXIT_BOOT_SERVICES events are signalled. The
127 * net effect of this entertaining design choice is that any
128 * timeout loops on the shutdown path (e.g. for gracefully
129 * closing outstanding TCP connections) may wait indefinitely.
130 *
131 * There is no way to report failure from currticks(), since
132 * the API lazily assumes that the host system continues to
133 * travel through time in the usual direction. Work around
134 * EFI's violation of this assumption by falling back to a
135 * simple free-running monotonic counter during shutdown.
136 */
138 efi_jiffies++;
139 } else {
142 }
143
145}
146
147/**
148 * Timer tick
149 *
150 * @v event Timer tick event
151 * @v context Event context
152 */
153static EFIAPI void efi_tick ( EFI_EVENT event __unused,
154 void *context __unused ) {
155
156 /* Increment tick count */
157 efi_jiffies++;
158}
159
160/**
161 * Start timer tick
162 *
163 */
164static void efi_tick_startup ( void ) {
165 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
166 EFI_STATUS efirc;
167 int rc;
168
169 /* Create timer tick event */
170 if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
172 &efi_tick_event ) ) != 0 ) {
173 rc = -EEFI ( efirc );
174 DBGC ( colour, "EFI could not create timer tick: %s\n",
175 strerror ( rc ) );
176 /* Nothing we can do about it */
177 return;
178 }
179
180 /* Start timer tick */
181 if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerPeriodic,
182 ( 10000000 / EFI_JIFFIES_PER_SEC ) ))!=0){
183 rc = -EEFI ( efirc );
184 DBGC ( colour, "EFI could not start timer tick: %s\n",
185 strerror ( rc ) );
186 /* Nothing we can do about it */
187 return;
188 }
189 DBGC ( colour, "EFI timer started at %d ticks per second\n",
191}
192
193/**
194 * Stop timer tick
195 *
196 * @v booting System is shutting down in order to boot
197 */
198static void efi_tick_shutdown ( int booting __unused ) {
199 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
200 EFI_STATUS efirc;
201 int rc;
202
203 /* Stop timer tick */
204 if ( ( efirc = bs->SetTimer ( efi_tick_event, TimerCancel, 0 ) ) != 0 ){
205 rc = -EEFI ( efirc );
206 DBGC ( colour, "EFI could not stop timer tick: %s\n",
207 strerror ( rc ) );
208 /* Self-destruct initiated */
209 return;
210 }
211 DBGC ( colour, "EFI timer stopped\n" );
212
213 /* Destroy timer tick event */
214 if ( ( efirc = bs->CloseEvent ( efi_tick_event ) ) != 0 ) {
215 rc = -EEFI ( efirc );
216 DBGC ( colour, "EFI could not destroy timer tick: %s\n",
217 strerror ( rc ) );
218 /* Probably non-fatal */
219 return;
220 }
221}
222
223/** Timer tick startup function */
224struct startup_fn efi_tick_startup_fn __startup_fn ( STARTUP_EARLY ) = {
225 .name = "efi_tick",
226 .startup = efi_tick_startup,
227 .shutdown = efi_tick_shutdown,
228};
229
230/** EFI timer */
231struct timer efi_timer __timer ( TIMER_NORMAL ) = {
232 .name = "efi",
233 .currticks = efi_currticks,
234 .udelay = efi_udelay,
235};
#define EFIAPI
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
#define TPL_CALLBACK
Definition UefiSpec.h:649
@ TimerCancel
An event's timer settings is to be cancelled and not trigger time is to be set/.
Definition UefiSpec.h:544
@ TimerPeriodic
An event is to be signaled periodically at a specified interval from the current time.
Definition UefiSpec.h:548
#define EVT_TIMER
Definition UefiSpec.h:451
#define EVT_NOTIFY_SIGNAL
Definition UefiSpec.h:454
#define colour
Colour for debug messages.
Definition acpi.c:42
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
EFI_TPL efi_external_tpl
External task priority level.
Definition efi_init.c:57
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition efi_init.c:54
int efi_shutdown_in_progress
EFI shutdown is in progress.
Definition efi_init.c:60
static unsigned long efi_jiffies
Current tick count.
Definition efi_timer.c:48
static void efi_udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition efi_timer.c:61
static EFI_EVENT efi_tick_event
Timer tick event.
Definition efi_timer.c:51
#define EFI_JIFFIES_PER_SEC
Number of jiffies per second.
Definition efi_timer.c:45
static unsigned long efi_currticks(void)
Get current system time in ticks.
Definition efi_timer.c:79
static void efi_tick_shutdown(int booting __unused)
Stop timer tick.
Definition efi_timer.c:198
static void efi_tick_startup(void)
Start timer tick.
Definition efi_timer.c:164
static EFIAPI void efi_tick(EFI_EVENT event __unused, void *context __unused)
Timer tick.
Definition efi_timer.c:153
Error codes.
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGC(...)
Definition compiler.h:505
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define STARTUP_EARLY
Early startup.
Definition init.h:64
#define TIMER_NORMAL
Normal timer.
Definition timer.h:64
EFI API.
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:175
#define EFI_EVENT
Definition efi.h:54
EFI_SYSTEM_TABLE * efi_systab
iPXE timers
#define TICKS_PER_SEC
Number of ticks per second.
Definition timer.h:16
#define __timer(order)
Declare a timer.
Definition timer.h:56
String functions.
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
Definition init.h:53
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
EFI Boot Services Table.
Definition UefiSpec.h:1931
EFI_RESTORE_TPL RestoreTPL
Definition UefiSpec.h:1941
EFI_SET_TIMER SetTimer
Definition UefiSpec.h:1956
EFI_CREATE_EVENT CreateEvent
Definition UefiSpec.h:1955
EFI_CLOSE_EVENT CloseEvent
Definition UefiSpec.h:1959
EFI_RAISE_TPL RaiseTPL
Definition UefiSpec.h:1940
EFI_STALL Stall
Definition UefiSpec.h:1988
A startup/shutdown function.
Definition init.h:43
A timer.
Definition timer.h:29