2 Implementation of Watchdog Timer Architectural Protocol using UEFI APIs.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "WatchdogTimer.h"
12 // Handle for the Watchdog Timer Architectural Protocol instance produced by this driver
14 EFI_HANDLE mWatchdogTimerHandle
= NULL
;
17 // The Watchdog Timer Architectural Protocol instance produced by this driver
19 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
20 WatchdogTimerDriverRegisterHandler
,
21 WatchdogTimerDriverSetTimerPeriod
,
22 WatchdogTimerDriverGetTimerPeriod
26 // The watchdog timer period in 100 ns units
28 UINT64 mWatchdogTimerPeriod
= 0;
31 // The notification function to call if the watchdig timer fires
33 EFI_WATCHDOG_TIMER_NOTIFY mWatchdogTimerNotifyFunction
= NULL
;
36 // The one-shot timer event that is armed when the watchdog timer is enabled
38 EFI_EVENT mWatchdogTimerEvent
;
42 Notification function that is called if the watchdog timer is fired.
44 Notification function for the one-shot timer event that was signaled
45 when the watchdog timer expired. If a handler has been registered with
46 the Watchdog Timer Architectural Protocol, then that handler is called
47 passing in the time period that has passed that cause the watchdog timer
48 to fire. Then, a call to the Runtime Service ResetSystem() is made to
51 @param Timer The one-shot timer event that was signaled when the
52 watchdog timer expired.
53 @param Context The context that was registered when the event Timer was created.
58 WatchdogTimerDriverExpires (
63 REPORT_STATUS_CODE (EFI_ERROR_CODE
| EFI_ERROR_MINOR
, (EFI_COMPUTING_UNIT_HOST_PROCESSOR
| EFI_CU_HP_EC_TIMER_EXPIRED
));
66 // If a notification function has been registered, then call it
68 if (mWatchdogTimerNotifyFunction
!= NULL
) {
69 mWatchdogTimerNotifyFunction (mWatchdogTimerPeriod
);
72 DEBUG ((EFI_D_ERROR
, "Watchdog Timer resetting system\n"));
77 gRT
->ResetSystem (EfiResetCold
, EFI_TIMEOUT
, 0, NULL
);
82 Registers a handler that is to be invoked when the watchdog timer fires.
84 This function registers a handler that is to be invoked when the watchdog
85 timer fires. By default, the EFI_WATCHDOG_TIMER protocol will call the
86 Runtime Service ResetSystem() when the watchdog timer fires. If a
87 NotifyFunction is registered, then the NotifyFunction will be called before
88 the Runtime Service ResetSystem() is called. If NotifyFunction is NULL, then
89 the watchdog handler is unregistered. If a watchdog handler is registered,
90 then EFI_SUCCESS is returned. If an attempt is made to register a handler
91 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
92 If an attempt is made to uninstall a handler when a handler is not installed,
93 then return EFI_INVALID_PARAMETER.
95 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
96 @param NotifyFunction The function to call when the watchdog timer fires. If this
97 is NULL, then the handler will be unregistered.
99 @retval EFI_SUCCESS The watchdog timer handler was registered or unregistered.
100 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already registered.
101 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not previously registered.
106 WatchdogTimerDriverRegisterHandler (
107 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
108 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
112 // If NotifyFunction is NULL, and a handler was not previously registered,
113 // return EFI_INVALID_PARAMETER.
115 if (NotifyFunction
== NULL
&& mWatchdogTimerNotifyFunction
== NULL
) {
116 return EFI_INVALID_PARAMETER
;
119 // If NotifyFunction is not NULL, and a handler is already registered,
120 // return EFI_ALREADY_STARTED.
122 if (NotifyFunction
!= NULL
&& mWatchdogTimerNotifyFunction
!= NULL
) {
123 return EFI_ALREADY_STARTED
;
126 mWatchdogTimerNotifyFunction
= NotifyFunction
;
132 Sets the amount of time in the future to fire the watchdog timer.
134 This function sets the amount of time to wait before firing the watchdog
135 timer to TimerPeriod 100 ns units. If TimerPeriod is 0, then the watchdog
138 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
139 @param TimerPeriod The amount of time in 100 ns units to wait before the watchdog
140 timer is fired. If TimerPeriod is zero, then the watchdog
143 @retval EFI_SUCCESS The watchdog timer has been programmed to fire in Time
145 @retval EFI_DEVICE_ERROR A watchdog timer could not be programmed due to a device
151 WatchdogTimerDriverSetTimerPeriod (
152 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
153 IN UINT64 TimerPeriod
156 mWatchdogTimerPeriod
= TimerPeriod
;
158 return gBS
->SetTimer (
160 (mWatchdogTimerPeriod
== 0) ? TimerCancel
: TimerRelative
,
166 Retrieves the amount of time in 100 ns units that the system will wait before firing the watchdog timer.
168 This function retrieves the amount of time the system will wait before firing
169 the watchdog timer. This period is returned in TimerPeriod, and EFI_SUCCESS
170 is returned. If TimerPeriod is NULL, then EFI_INVALID_PARAMETER is returned.
172 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
173 @param TimerPeriod A pointer to the amount of time in 100 ns units that the system
174 will wait before the watchdog timer is fired. If TimerPeriod of
175 zero is returned, then the watchdog timer is disabled.
177 @retval EFI_SUCCESS The amount of time that the system will wait before
178 firing the watchdog timer was returned in TimerPeriod.
179 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
184 WatchdogTimerDriverGetTimerPeriod (
185 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
186 IN UINT64
*TimerPeriod
189 if (TimerPeriod
== NULL
) {
190 return EFI_INVALID_PARAMETER
;
193 *TimerPeriod
= mWatchdogTimerPeriod
;
199 Entry point of the Watchdog Timer Architectural Protocol driver.
201 @param ImageHandle The image handle of this driver.
202 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
204 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.
209 WatchdogTimerDriverInitialize (
210 IN EFI_HANDLE ImageHandle
,
211 IN EFI_SYSTEM_TABLE
*SystemTable
217 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
219 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
222 // Create the timer event to implement a simple watchdog timer
224 Status
= gBS
->CreateEvent (
225 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
227 WatchdogTimerDriverExpires
,
231 ASSERT_EFI_ERROR (Status
);
234 // Install the Watchdog Timer Arch Protocol onto a new handle
236 Status
= gBS
->InstallMultipleProtocolInterfaces (
237 &mWatchdogTimerHandle
,
238 &gEfiWatchdogTimerArchProtocolGuid
,
242 ASSERT_EFI_ERROR (Status
);