3 * Copyright (c) 2013-2018, ARM Limited. All rights reserved.
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/IoLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiRuntimeServicesTableLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/ArmGenericTimerCounterLib.h>
21 #include <Protocol/HardwareInterrupt2.h>
22 #include <Protocol/WatchdogTimer.h>
24 #include "GenericWatchdog.h"
26 /* The number of 100ns periods (the unit of time passed to these functions)
28 #define TIME_UNITS_PER_SECOND 10000000
30 // Tick frequency of the generic timer basis of the generic watchdog.
31 STATIC UINTN mTimerFrequencyHz
= 0;
33 /* In cases where the compare register was set manually, information about
34 how long the watchdog was asked to wait cannot be retrieved from hardware.
35 It is therefore stored here. 0 means the timer is not running. */
36 STATIC UINT64 mNumTimerTicks
= 0;
38 STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL
*mInterruptProtocol
;
39 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify
;
43 WatchdogWriteOffsetRegister (
47 MmioWrite32 (GENERIC_WDOG_OFFSET_REG
, Value
);
52 WatchdogWriteCompareRegister (
56 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW
, Value
& MAX_UINT32
);
57 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH
, (Value
>> 32) & MAX_UINT32
);
66 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG
, GENERIC_WDOG_ENABLED
);
75 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG
, GENERIC_WDOG_DISABLED
);
78 /** On exiting boot services we must make sure the Watchdog Timer
84 WatchdogExitBootServicesEvent (
93 /* This function is called when the watchdog's first signal (WS0) goes high.
94 It uses the ResetSystem Runtime Service to reset the board.
99 WatchdogInterruptHandler (
100 IN HARDWARE_INTERRUPT_SOURCE Source
,
101 IN EFI_SYSTEM_CONTEXT SystemContext
104 STATIC CONST CHAR16 ResetString
[]= L
"The generic watchdog timer ran out.";
109 mInterruptProtocol
->EndOfInterrupt (mInterruptProtocol
, Source
);
112 // The notify function should be called with the elapsed number of ticks
113 // since the watchdog was armed, which should exceed the timer period.
114 // We don't actually know the elapsed number of ticks, so let's return
115 // the timer period plus 1.
117 if (mWatchdogNotify
!= NULL
) {
118 TimerPeriod
= ((TIME_UNITS_PER_SECOND
/ mTimerFrequencyHz
) * mNumTimerTicks
);
119 mWatchdogNotify (TimerPeriod
+ 1);
122 gRT
->ResetSystem (EfiResetCold
, EFI_TIMEOUT
, StrSize (ResetString
),
123 (CHAR16
*)ResetString
);
125 // If we got here then the reset didn't work
130 This function registers the handler NotifyFunction so it is called every time
131 the watchdog timer expires. It also passes the amount of time since the last
132 handler call to the NotifyFunction.
133 If NotifyFunction is not NULL and a handler is not already registered,
134 then the new handler is registered and EFI_SUCCESS is returned.
135 If NotifyFunction is NULL, and a handler is already registered,
136 then that handler is unregistered.
137 If an attempt is made to register a handler when a handler is already
138 registered, then EFI_ALREADY_STARTED is returned.
139 If an attempt is made to unregister a handler when a handler is not
140 registered, then EFI_INVALID_PARAMETER is returned.
142 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
143 @param NotifyFunction The function to call when a timer interrupt fires.
144 This function executes at TPL_HIGH_LEVEL. The DXE
145 Core will register a handler for the timer interrupt,
146 so it can know how much time has passed. This
147 information is used to signal timer based events.
148 NULL will unregister the handler.
150 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
156 WatchdogRegisterHandler (
157 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
158 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
161 if (mWatchdogNotify
== NULL
&& NotifyFunction
== NULL
) {
162 return EFI_INVALID_PARAMETER
;
165 if (mWatchdogNotify
!= NULL
&& NotifyFunction
!= NULL
) {
166 return EFI_ALREADY_STARTED
;
169 mWatchdogNotify
= NotifyFunction
;
174 This function sets the amount of time to wait before firing the watchdog
175 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog
178 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
179 @param TimerPeriod The amount of time in 100ns units to wait before
180 the watchdog timer is fired. If TimerPeriod is zero,
181 then the watchdog timer is disabled.
183 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
184 in TimerPeriod 100ns units.
190 WatchdogSetTimerPeriod (
191 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
192 IN UINT64 TimerPeriod
// In 100ns units
197 // if TimerPeriod is 0, this is a request to stop the watchdog.
198 if (TimerPeriod
== 0) {
204 // Work out how many timer ticks will equate to TimerPeriod
205 mNumTimerTicks
= (mTimerFrequencyHz
* TimerPeriod
) / TIME_UNITS_PER_SECOND
;
207 /* If the number of required ticks is greater than the max the watchdog's
208 offset register (WOR) can hold, we need to manually compute and set
209 the compare register (WCV) */
210 if (mNumTimerTicks
> MAX_UINT32
) {
211 /* We need to enable the watchdog *before* writing to the compare register,
212 because enabling the watchdog causes an "explicit refresh", which
213 clobbers the compare register (WCV). In order to make sure this doesn't
214 trigger an interrupt, set the offset to max. */
215 WatchdogWriteOffsetRegister (MAX_UINT32
);
217 SystemCount
= ArmGenericTimerGetSystemCount ();
218 WatchdogWriteCompareRegister (SystemCount
+ mNumTimerTicks
);
220 WatchdogWriteOffsetRegister ((UINT32
)mNumTimerTicks
);
228 This function retrieves the period of timer interrupts in 100ns units,
229 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
230 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
231 returned, then the timer is currently disabled.
233 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
234 @param TimerPeriod A pointer to the timer period to retrieve in
235 100ns units. If 0 is returned, then the timer is
239 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
240 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
246 WatchdogGetTimerPeriod (
247 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
248 OUT UINT64
*TimerPeriod
251 if (TimerPeriod
== NULL
) {
252 return EFI_INVALID_PARAMETER
;
255 *TimerPeriod
= ((TIME_UNITS_PER_SECOND
/ mTimerFrequencyHz
) * mNumTimerTicks
);
261 Interface structure for the Watchdog Architectural Protocol.
263 @par Protocol Description:
264 This protocol provides a service to set the amount of time to wait
265 before firing the watchdog timer, and it also provides a service to
266 register a handler that is invoked when the watchdog timer fires.
268 @par When the watchdog timer fires, control will be passed to a handler
269 if one has been registered. If no handler has been registered,
270 or the registered handler returns, then the system will be
271 reset by calling the Runtime Service ResetSystem().
273 @param RegisterHandler
274 Registers a handler that will be called each time the
275 watchdogtimer interrupt fires. TimerPeriod defines the minimum
276 time between timer interrupts, so TimerPeriod will also
277 be the minimum time between calls to the registered
279 NOTE: If the watchdog resets the system in hardware, then
280 this function will not have any chance of executing.
282 @param SetTimerPeriod
283 Sets the period of the timer interrupt in 100ns units.
284 This function is optional, and may return EFI_UNSUPPORTED.
285 If this function is supported, then the timer period will
286 be rounded up to the nearest supported timer period.
288 @param GetTimerPeriod
289 Retrieves the period of the timer interrupt in 100ns units.
292 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
293 WatchdogRegisterHandler
,
294 WatchdogSetTimerPeriod
,
295 WatchdogGetTimerPeriod
298 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
302 GenericWatchdogEntry (
303 IN EFI_HANDLE ImageHandle
,
304 IN EFI_SYSTEM_TABLE
*SystemTable
310 Status
= gBS
->LocateProtocol (&gHardwareInterrupt2ProtocolGuid
, NULL
,
311 (VOID
**)&mInterruptProtocol
);
312 ASSERT_EFI_ERROR (Status
);
314 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
316 This will avoid conflicts with the universal watchdog */
317 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
319 mTimerFrequencyHz
= ArmGenericTimerGetTimerFreq ();
320 ASSERT (mTimerFrequencyHz
!= 0);
322 // Install interrupt handler
323 Status
= mInterruptProtocol
->RegisterInterruptSource (mInterruptProtocol
,
324 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),
325 WatchdogInterruptHandler
);
326 if (EFI_ERROR (Status
)) {
330 Status
= mInterruptProtocol
->SetTriggerType (mInterruptProtocol
,
331 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),
332 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING
);
333 if (EFI_ERROR (Status
)) {
334 goto UnregisterHandler
;
337 // Install the Timer Architectural Protocol onto a new handle
339 Status
= gBS
->InstallMultipleProtocolInterfaces (&Handle
,
340 &gEfiWatchdogTimerArchProtocolGuid
, &mWatchdogTimer
,
342 if (EFI_ERROR (Status
)) {
343 goto UnregisterHandler
;
346 // Register for an ExitBootServicesEvent
347 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_NOTIFY
,
348 WatchdogExitBootServicesEvent
, NULL
,
349 &mEfiExitBootServicesEvent
);
350 ASSERT_EFI_ERROR (Status
);
358 // Unregister the handler
359 mInterruptProtocol
->RegisterInterruptSource (mInterruptProtocol
,
360 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),