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