]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Universal/WatchdogTimer/Dxe/WatchDogTimer.c
Partially make EdkModulePkg pass intel IPF compiler with /W4 /WX switched on.
[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
1cc8ee78 58STATIC\r
878ddf1f 59VOID\r
60EFIAPI\r
61WatchdogTimerDriverExpires (\r
62 IN EFI_EVENT Timer,\r
63 IN VOID *Context\r
64 )\r
65/*++\r
66\r
67 Routine Description:\r
68\r
69 Notification function that is called if the watchdog timer is fired. If a \r
70 handler has been registered with the Watchdog Timer Architectural Protocol,\r
71 then that handler is called passing in the time period that has passed that\r
72 cause the watchdog timer to fire. Then, a call to the Runtime Service \r
73 ResetSystem() is made to reset the platform.\r
74 \r
75 Arguments:\r
76\r
77 Timer - The one-shot timer event that was signaled when the watchdog timer \r
78 expired.\r
79\r
80 Context - The context that was registered when the event Timer was created.\r
81\r
82 Returns:\r
83\r
84 None.\r
85\r
86--*/\r
87{\r
88 //\r
89 // Report error code before exiting\r
90 //\r
91 REPORT_STATUS_CODE (\r
92 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
93 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_TIMER_EXPIRED)\r
94 );\r
95\r
96 //\r
97 // If a notification function has been registered, then call it\r
98 //\r
99 if (mWatchdogTimerNotifyFunction != NULL) {\r
100 mWatchdogTimerNotifyFunction (mWatchdogTimerPeriod);\r
101 }\r
102 //\r
103 // Reset the platform\r
104 //\r
105 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);\r
106}\r
107\r
108EFI_STATUS\r
109EFIAPI\r
110WatchdogTimerDriverRegisterHandler (\r
111 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
112 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction\r
113 )\r
114/*++\r
115\r
116Routine Description:\r
117\r
118 This function registers a handler that is to be invoked when the watchdog \r
119 timer fires. By default, the EFI_WATCHDOG_TIMER protocol will call the \r
120 Runtime Service ResetSystem() when the watchdog timer fires. If a \r
121 NotifyFunction is registered, then the NotifyFunction will be called before \r
122 the Runtime Service ResetSystem() is called. If NotifyFunction is NULL, then \r
123 the watchdog handler is unregistered. If a watchdog handler is registered, \r
124 then EFI_SUCCESS is returned. If an attempt is made to register a handler \r
125 when a handler is already registered, then EFI_ALREADY_STARTED is returned. \r
126 If an attempt is made to uninstall a handler when a handler is not installed, \r
127 then return EFI_INVALID_PARAMETER.\r
128\r
129Arguments:\r
130\r
131 This - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
132\r
133 NotifyFunction - The function to call when the watchdog timer fires. If this\r
134 is NULL, then the handler will be unregistered.\r
135\r
136Returns: \r
137\r
138 EFI_SUCCESS - The watchdog timer handler was registered or \r
139 unregistered.\r
140\r
141 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already \r
142 registered.\r
143\r
144 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not \r
145 previously registered.\r
146\r
147--*/\r
148{\r
149 if (NotifyFunction == NULL && mWatchdogTimerNotifyFunction == NULL) {\r
150 return EFI_INVALID_PARAMETER;\r
151 }\r
152\r
153 if (NotifyFunction != NULL && mWatchdogTimerNotifyFunction != NULL) {\r
154 return EFI_ALREADY_STARTED;\r
155 }\r
156\r
157 mWatchdogTimerNotifyFunction = NotifyFunction;\r
158\r
159 return EFI_SUCCESS;\r
160}\r
161\r
162EFI_STATUS\r
163EFIAPI\r
164WatchdogTimerDriverSetTimerPeriod (\r
165 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
166 IN UINT64 TimerPeriod\r
167 )\r
168/*++\r
169\r
170Routine Description:\r
171\r
172 This function sets the amount of time to wait before firing the watchdog \r
173 timer to TimerPeriod 100 nS units. If TimerPeriod is 0, then the watchdog \r
174 timer is disabled.\r
175\r
176Arguments:\r
177\r
178 This - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
179\r
180 TimerPeriod - The amount of time in 100 nS units to wait before the watchdog \r
181 timer is fired. If TimerPeriod is zero, then the watchdog \r
182 timer is disabled.\r
183 \r
184Returns: \r
185\r
186 EFI_SUCCESS - The watchdog timer has been programmed to fire in Time \r
187 100 nS units.\r
188\r
189 EFI_DEVICE_ERROR - A watchdog timer could not be programmed due to a device \r
190 error.\r
191\r
192--*/\r
193{\r
194 mWatchdogTimerPeriod = TimerPeriod;\r
195\r
196 return gBS->SetTimer (\r
197 mWatchdogTimerEvent,\r
198 (mWatchdogTimerPeriod == 0) ? TimerCancel : TimerRelative,\r
199 mWatchdogTimerPeriod\r
200 );\r
201}\r
202\r
203EFI_STATUS\r
204EFIAPI\r
205WatchdogTimerDriverGetTimerPeriod (\r
206 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,\r
207 IN UINT64 *TimerPeriod\r
208 )\r
209/*++\r
210\r
211Routine Description:\r
212\r
213 This function retrieves the amount of time the system will wait before firing \r
214 the watchdog timer. This period is returned in TimerPeriod, and EFI_SUCCESS \r
215 is returned. If TimerPeriod is NULL, then EFI_INVALID_PARAMETER is returned.\r
216\r
217Arguments:\r
218\r
219 This - The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.\r
220\r
221 TimerPeriod - A pointer to the amount of time in 100 nS units that the system \r
222 will wait before the watchdog timer is fired. If TimerPeriod of\r
223 zero is returned, then the watchdog timer is disabled.\r
224 \r
225Returns: \r
226\r
227 EFI_SUCCESS - The amount of time that the system will wait before \r
228 firing the watchdog timer was returned in TimerPeriod.\r
229\r
230 EFI_INVALID_PARAMETER - TimerPeriod is NULL.\r
231\r
232--*/\r
233{\r
234 if (TimerPeriod == NULL) {\r
235 return EFI_INVALID_PARAMETER;\r
236 }\r
237\r
238 *TimerPeriod = mWatchdogTimerPeriod;\r
239\r
240 return EFI_SUCCESS;\r
241}\r
242\r
243EFI_STATUS\r
244EFIAPI\r
245WatchdogTimerDriverInitialize (\r
246 IN EFI_HANDLE ImageHandle,\r
247 IN EFI_SYSTEM_TABLE *SystemTable\r
248 )\r
249/*++\r
250\r
251Routine Description:\r
252\r
253 Initialize the Watchdog Timer Architectural Protocol driver\r
254\r
255Arguments:\r
256\r
257 ImageHandle - ImageHandle of the loaded driver\r
258\r
259 SystemTable - Pointer to the System Table\r
260\r
261Returns:\r
262\r
263 EFI_SUCCESS - Timer Architectural Protocol created\r
264\r
265 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.\r
266 \r
267 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.\r
268\r
269--*/\r
270{\r
271 EFI_STATUS Status;\r
272\r
273 REPORT_STATUS_CODE (\r
274 EFI_PROGRESS_CODE,\r
275 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_SW_PC_INIT_BEGIN)\r
276 );\r
277 //\r
278 // Make sure the Watchdog Timer Architectural Protocol is not already installed in the system\r
279 //\r
280 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);\r
281\r
282 //\r
283 // Create the timer event used to implement a simple watchdog timer\r
284 //\r
285 Status = gBS->CreateEvent (\r
286 EFI_EVENT_TIMER | EFI_EVENT_NOTIFY_SIGNAL,\r
287 EFI_TPL_NOTIFY,\r
288 WatchdogTimerDriverExpires,\r
289 NULL,\r
290 &mWatchdogTimerEvent\r
291 );\r
292 ASSERT_EFI_ERROR (Status);\r
293\r
294 //\r
295 // Install the Watchdog Timer Arch Protocol onto a new handle\r
296 //\r
297 Status = gBS->InstallMultipleProtocolInterfaces (\r
298 &mWatchdogTimerHandle,\r
299 &gEfiWatchdogTimerArchProtocolGuid,\r
300 &mWatchdogTimer,\r
301 NULL\r
302 );\r
303 ASSERT_EFI_ERROR (Status);\r
304\r
305 REPORT_STATUS_CODE (\r
306 EFI_PROGRESS_CODE,\r
307 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_SW_PC_INIT_END)\r
308 );\r
309\r
310 return Status;\r
311}\r