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