3 Generic watchdog timer services implemenetation using UEFI APIs and
4 install watchdog timer architecture protocol.
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
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.
17 #include "WatchdogTimer.h"
20 // Handle for the Watchdog Timer Architectural Protocol instance produced by this driver
22 EFI_HANDLE mWatchdogTimerHandle
= NULL
;
25 // The Watchdog Timer Architectural Protocol instance produced by this driver
27 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
28 WatchdogTimerDriverRegisterHandler
,
29 WatchdogTimerDriverSetTimerPeriod
,
30 WatchdogTimerDriverGetTimerPeriod
34 // The watchdog timer period in 100 nS units
36 UINT64 mWatchdogTimerPeriod
= 0;
39 // The notification function to call if the watchdig timer fires
41 EFI_WATCHDOG_TIMER_NOTIFY mWatchdogTimerNotifyFunction
= NULL
;
44 // The one-shot timer event that is armed when the watchdog timer is enabled
46 EFI_EVENT mWatchdogTimerEvent
;
50 Notification function that is called if the watchdog timer is fired. If a
51 handler has been registered with the Watchdog Timer Architectural Protocol,
52 then that handler is called passing in the time period that has passed that
53 cause the watchdog timer to fire. Then, a call to the Runtime Service
54 ResetSystem() is made to reset the platform.
57 @param Timer The one-shot timer event that was signaled when the watchdog timer
59 @param Context The context that was registered when the event Timer was created.
66 WatchdogTimerDriverExpires (
71 REPORT_STATUS_CODE (EFI_ERROR_CODE
| EFI_ERROR_MINOR
, PcdGet32 (PcdStatusCodeValueEfiWatchDogTimerExpired
));
74 // If a notification function has been registered, then call it
76 if (mWatchdogTimerNotifyFunction
!= NULL
) {
77 mWatchdogTimerNotifyFunction (mWatchdogTimerPeriod
);
82 gRT
->ResetSystem (EfiResetCold
, EFI_TIMEOUT
, 0, NULL
);
87 This function registers a handler that is to be invoked when the watchdog
88 timer fires. By default, the EFI_WATCHDOG_TIMER protocol will call the
89 Runtime Service ResetSystem() when the watchdog timer fires. If a
90 NotifyFunction is registered, then the NotifyFunction will be called before
91 the Runtime Service ResetSystem() is called. If NotifyFunction is NULL, then
92 the watchdog handler is unregistered. If a watchdog handler is registered,
93 then EFI_SUCCESS is returned. If an attempt is made to register a handler
94 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
95 If an attempt is made to uninstall a handler when a handler is not installed,
96 then return EFI_INVALID_PARAMETER.
98 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
99 @param NotifyFunction The function to call when the watchdog timer fires. If this
100 is NULL, then the handler will be unregistered.
102 @return EFI_SUCCESS The watchdog timer handler was registered or unregistered.
103 @return EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already registered.
104 @return EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not previously registered.
109 WatchdogTimerDriverRegisterHandler (
110 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
111 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
114 if (NotifyFunction
== NULL
&& mWatchdogTimerNotifyFunction
== NULL
) {
115 return EFI_INVALID_PARAMETER
;
118 if (NotifyFunction
!= NULL
&& mWatchdogTimerNotifyFunction
!= NULL
) {
119 return EFI_ALREADY_STARTED
;
122 mWatchdogTimerNotifyFunction
= NotifyFunction
;
128 This function sets the amount of time to wait before firing the watchdog
129 timer to TimerPeriod 100 nS units. If TimerPeriod is 0, then the watchdog
132 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
133 @param TimerPeriod The amount of time in 100 nS units to wait before the watchdog
134 timer is fired. If TimerPeriod is zero, then the watchdog
137 @return EFI_SUCCESS The watchdog timer has been programmed to fire in Time
139 @return EFI_DEVICE_ERROR A watchdog timer could not be programmed due to a device
145 WatchdogTimerDriverSetTimerPeriod (
146 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
147 IN UINT64 TimerPeriod
150 mWatchdogTimerPeriod
= TimerPeriod
;
152 return gBS
->SetTimer (
154 (mWatchdogTimerPeriod
== 0) ? TimerCancel
: TimerRelative
,
160 This function retrieves the amount of time the system will wait before firing
161 the watchdog timer. This period is returned in TimerPeriod, and EFI_SUCCESS
162 is returned. If TimerPeriod is NULL, then EFI_INVALID_PARAMETER is returned.
164 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
165 @param TimerPeriod A pointer to the amount of time in 100 nS units that the system
166 will wait before the watchdog timer is fired. If TimerPeriod of
167 zero is returned, then the watchdog timer is disabled.
169 @return EFI_SUCCESS The amount of time that the system will wait before
170 firing the watchdog timer was returned in TimerPeriod.
171 @return EFI_INVALID_PARAMETER TimerPeriod is NULL.
176 WatchdogTimerDriverGetTimerPeriod (
177 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
178 IN UINT64
*TimerPeriod
181 if (TimerPeriod
== NULL
) {
182 return EFI_INVALID_PARAMETER
;
185 *TimerPeriod
= mWatchdogTimerPeriod
;
191 Initialize the Watchdog Timer Architectural Protocol driver.
193 @param ImageHandle ImageHandle of the loaded driver.
194 @param SystemTable Pointer to the System Table.
196 @return EFI_SUCCESS Timer Architectural Protocol created.
201 WatchdogTimerDriverInitialize (
202 IN EFI_HANDLE ImageHandle
,
203 IN EFI_SYSTEM_TABLE
*SystemTable
209 // Make sure the Watchdog Timer Architectural Protocol is not already installed in the system
211 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
214 // Create the timer event used to implement a simple watchdog timer
216 Status
= gBS
->CreateEvent (
217 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
219 WatchdogTimerDriverExpires
,
223 ASSERT_EFI_ERROR (Status
);
226 // Install the Watchdog Timer Arch Protocol onto a new handle
228 Status
= gBS
->InstallMultipleProtocolInterfaces (
229 &mWatchdogTimerHandle
,
230 &gEfiWatchdogTimerArchProtocolGuid
,
234 ASSERT_EFI_ERROR (Status
);