]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/WatchdogTimer/Dxe/WatchDogTimer.c
1. adjust contents layout of SPD header editor, FPD header editor.
[mirror_edk2.git] / EdkModulePkg / Universal / WatchdogTimer / Dxe / WatchDogTimer.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 WatchDogTimer.c
15
16 Abstract:
17
18 Generic watchdog timer implemenetation using EFI APIs
19
20 Revision History
21
22 --*/
23
24 #include "WatchDogTimer.h"
25
26 //
27 // Handle for the Watchdog Timer Architectural Protocol instance produced by this driver
28 //
29 EFI_HANDLE mWatchdogTimerHandle = NULL;
30
31 //
32 // The Watchdog Timer Architectural Protocol instance produced by this driver
33 //
34 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
35 WatchdogTimerDriverRegisterHandler,
36 WatchdogTimerDriverSetTimerPeriod,
37 WatchdogTimerDriverGetTimerPeriod
38 };
39
40 //
41 // The watchdog timer period in 100 nS units
42 //
43 UINT64 mWatchdogTimerPeriod = 0;
44
45 //
46 // The notification function to call if the watchdig timer fires
47 //
48 EFI_WATCHDOG_TIMER_NOTIFY mWatchdogTimerNotifyFunction = NULL;
49
50 //
51 // The one-shot timer event that is armed when the watchdog timer is enabled
52 //
53 EFI_EVENT mWatchdogTimerEvent;
54
55 //
56 // Worker Functions
57 //
58 VOID
59 EFIAPI
60 WatchdogTimerDriverExpires (
61 IN EFI_EVENT Timer,
62 IN VOID *Context
63 )
64 /*++
65
66 Routine Description:
67
68 Notification function that is called if the watchdog timer is fired. If a
69 handler has been registered with the Watchdog Timer Architectural Protocol,
70 then that handler is called passing in the time period that has passed that
71 cause the watchdog timer to fire. Then, a call to the Runtime Service
72 ResetSystem() is made to reset the platform.
73
74 Arguments:
75
76 Timer - The one-shot timer event that was signaled when the watchdog timer
77 expired.
78
79 Context - The context that was registered when the event Timer was created.
80
81 Returns:
82
83 None.
84
85 --*/
86 {
87 //
88 // Report error code before exiting
89 //
90 REPORT_STATUS_CODE (
91 EFI_ERROR_CODE | EFI_ERROR_MINOR,
92 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_TIMER_EXPIRED)
93 );
94
95 //
96 // If a notification function has been registered, then call it
97 //
98 if (mWatchdogTimerNotifyFunction != NULL) {
99 mWatchdogTimerNotifyFunction (mWatchdogTimerPeriod);
100 }
101 //
102 // Reset the platform
103 //
104 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);
105 }
106
107 EFI_STATUS
108 EFIAPI
109 WatchdogTimerDriverRegisterHandler (
110 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
111 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
112 )
113 /*++
114
115 Routine Description:
116
117 This function registers a handler that is to be invoked when the watchdog
118 timer fires. By default, the EFI_WATCHDOG_TIMER protocol will call the
119 Runtime Service ResetSystem() when the watchdog timer fires. If a
120 NotifyFunction is registered, then the NotifyFunction will be called before
121 the Runtime Service ResetSystem() is called. If NotifyFunction is NULL, then
122 the watchdog handler is unregistered. If a watchdog handler is registered,
123 then EFI_SUCCESS is returned. If an attempt is made to register a handler
124 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
125 If an attempt is made to uninstall a handler when a handler is not installed,
126 then return EFI_INVALID_PARAMETER.
127
128 Arguments:
129
130 This - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
131
132 NotifyFunction - The function to call when the watchdog timer fires. If this
133 is NULL, then the handler will be unregistered.
134
135 Returns:
136
137 EFI_SUCCESS - The watchdog timer handler was registered or
138 unregistered.
139
140 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
141 registered.
142
143 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
144 previously registered.
145
146 --*/
147 {
148 if (NotifyFunction == NULL && mWatchdogTimerNotifyFunction == NULL) {
149 return EFI_INVALID_PARAMETER;
150 }
151
152 if (NotifyFunction != NULL && mWatchdogTimerNotifyFunction != NULL) {
153 return EFI_ALREADY_STARTED;
154 }
155
156 mWatchdogTimerNotifyFunction = NotifyFunction;
157
158 return EFI_SUCCESS;
159 }
160
161 EFI_STATUS
162 EFIAPI
163 WatchdogTimerDriverSetTimerPeriod (
164 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
165 IN UINT64 TimerPeriod
166 )
167 /*++
168
169 Routine Description:
170
171 This function sets the amount of time to wait before firing the watchdog
172 timer to TimerPeriod 100 nS units. If TimerPeriod is 0, then the watchdog
173 timer is disabled.
174
175 Arguments:
176
177 This - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
178
179 TimerPeriod - The amount of time in 100 nS units to wait before the watchdog
180 timer is fired. If TimerPeriod is zero, then the watchdog
181 timer is disabled.
182
183 Returns:
184
185 EFI_SUCCESS - The watchdog timer has been programmed to fire in Time
186 100 nS units.
187
188 EFI_DEVICE_ERROR - A watchdog timer could not be programmed due to a device
189 error.
190
191 --*/
192 {
193 mWatchdogTimerPeriod = TimerPeriod;
194
195 return gBS->SetTimer (
196 mWatchdogTimerEvent,
197 (mWatchdogTimerPeriod == 0) ? TimerCancel : TimerRelative,
198 mWatchdogTimerPeriod
199 );
200 }
201
202 EFI_STATUS
203 EFIAPI
204 WatchdogTimerDriverGetTimerPeriod (
205 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
206 IN UINT64 *TimerPeriod
207 )
208 /*++
209
210 Routine Description:
211
212 This function retrieves the amount of time the system will wait before firing
213 the watchdog timer. This period is returned in TimerPeriod, and EFI_SUCCESS
214 is returned. If TimerPeriod is NULL, then EFI_INVALID_PARAMETER is returned.
215
216 Arguments:
217
218 This - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
219
220 TimerPeriod - A pointer to the amount of time in 100 nS units that the system
221 will wait before the watchdog timer is fired. If TimerPeriod of
222 zero is returned, then the watchdog timer is disabled.
223
224 Returns:
225
226 EFI_SUCCESS - The amount of time that the system will wait before
227 firing the watchdog timer was returned in TimerPeriod.
228
229 EFI_INVALID_PARAMETER - TimerPeriod is NULL.
230
231 --*/
232 {
233 if (TimerPeriod == NULL) {
234 return EFI_INVALID_PARAMETER;
235 }
236
237 *TimerPeriod = mWatchdogTimerPeriod;
238
239 return EFI_SUCCESS;
240 }
241
242 EFI_STATUS
243 EFIAPI
244 WatchdogTimerDriverInitialize (
245 IN EFI_HANDLE ImageHandle,
246 IN EFI_SYSTEM_TABLE *SystemTable
247 )
248 /*++
249
250 Routine Description:
251
252 Initialize the Watchdog Timer Architectural Protocol driver
253
254 Arguments:
255
256 ImageHandle - ImageHandle of the loaded driver
257
258 SystemTable - Pointer to the System Table
259
260 Returns:
261
262 EFI_SUCCESS - Timer Architectural Protocol created
263
264 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
265
266 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.
267
268 --*/
269 {
270 EFI_STATUS Status;
271
272 REPORT_STATUS_CODE (
273 EFI_PROGRESS_CODE,
274 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_SW_PC_INIT_BEGIN)
275 );
276 //
277 // Make sure the Watchdog Timer Architectural Protocol is not already installed in the system
278 //
279 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
280
281 //
282 // Create the timer event used to implement a simple watchdog timer
283 //
284 Status = gBS->CreateEvent (
285 EFI_EVENT_TIMER | EFI_EVENT_NOTIFY_SIGNAL,
286 EFI_TPL_NOTIFY,
287 WatchdogTimerDriverExpires,
288 NULL,
289 &mWatchdogTimerEvent
290 );
291 ASSERT_EFI_ERROR (Status);
292
293 //
294 // Install the Watchdog Timer Arch Protocol onto a new handle
295 //
296 Status = gBS->InstallMultipleProtocolInterfaces (
297 &mWatchdogTimerHandle,
298 &gEfiWatchdogTimerArchProtocolGuid,
299 &mWatchdogTimer,
300 NULL
301 );
302 ASSERT_EFI_ERROR (Status);
303
304 REPORT_STATUS_CODE (
305 EFI_PROGRESS_CODE,
306 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_SW_PC_INIT_END)
307 );
308
309 return Status;
310 }