[efi] Attempt to fetch autoexec script via TFTP
[ipxe.git] / src / interface / efi / efi_timer.c
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
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <ipxe/timer.h>
30 #include <ipxe/init.h>
31 #include <ipxe/efi/efi.h>
32
33 /** @file
34 *
35 * iPXE timer API for EFI
36 *
37 */
38
39 /**
40 * Number of jiffies per second
41 *
42 * This is a policy decision.
43 */
44 #define EFI_JIFFIES_PER_SEC 32
45
46 /** Current tick count */
47 static unsigned long efi_jiffies;
48
49 /** Timer tick event */
50 static EFI_EVENT efi_tick_event;
51
52 /** Colour for debug messages */
53 #define colour &efi_jiffies
54
55 /**
56 * Delay for a fixed number of microseconds
57 *
58 * @v usecs Number of microseconds for which to delay
59 */
60 static void efi_udelay ( unsigned long usecs ) {
61 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
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 }
72
73 /**
74 * Get current system time in ticks
75 *
76 * @ret ticks Current time, in ticks
77 */
78 static unsigned long efi_currticks ( void ) {
79 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
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 {
139 bs->RestoreTPL ( efi_external_tpl );
140 bs->RaiseTPL ( efi_internal_tpl );
141 }
142
143 return ( efi_jiffies * ( TICKS_PER_SEC / EFI_JIFFIES_PER_SEC ) );
144 }
145
146 /**
147 * Timer tick
148 *
149 * @v event Timer tick event
150 * @v context Event context
151 */
152 static EFIAPI void efi_tick ( EFI_EVENT event __unused,
153 void *context __unused ) {
154
155 /* Increment tick count */
156 efi_jiffies++;
157 }
158
159 /**
160 * Start timer tick
161 *
162 */
163 static void efi_tick_startup ( void ) {
164 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
165 EFI_STATUS efirc;
166 int rc;
167
168 /* Create timer tick event */
169 if ( ( efirc = bs->CreateEvent ( ( EVT_TIMER | EVT_NOTIFY_SIGNAL ),
170 TPL_CALLBACK, efi_tick, NULL,
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",
189 EFI_JIFFIES_PER_SEC );
190 }
191
192 /**
193 * Stop timer tick
194 *
195 * @v booting System is shutting down in order to boot
196 */
197 static void efi_tick_shutdown ( int booting __unused ) {
198 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
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 }
221
222 /** Timer tick startup function */
223 struct startup_fn efi_tick_startup_fn __startup_fn ( STARTUP_EARLY ) = {
224 .name = "efi_tick",
225 .startup = efi_tick_startup,
226 .shutdown = efi_tick_shutdown,
227 };
228
229 /** EFI timer */
230 struct timer efi_timer __timer ( TIMER_NORMAL ) = {
231 .name = "efi",
232 .currticks = efi_currticks,
233 .udelay = efi_udelay,
234 };