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