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);
125 StrSize (ResetString
),
126 (CHAR16
*)ResetString
129 // If we got here then the reset didn't work
134 This function registers the handler NotifyFunction so it is called every time
135 the watchdog timer expires. It also passes the amount of time since the last
136 handler call to the NotifyFunction.
137 If NotifyFunction is not NULL and a handler is not already registered,
138 then the new handler is registered and EFI_SUCCESS is returned.
139 If NotifyFunction is NULL, and a handler is already registered,
140 then that handler is unregistered.
141 If an attempt is made to register a handler when a handler is already
142 registered, then EFI_ALREADY_STARTED is returned.
143 If an attempt is made to unregister a handler when a handler is not
144 registered, then EFI_INVALID_PARAMETER is returned.
146 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
147 @param NotifyFunction The function to call when a timer interrupt fires.
148 This function executes at TPL_HIGH_LEVEL. The DXE
149 Core will register a handler for the timer interrupt,
150 so it can know how much time has passed. This
151 information is used to signal timer based events.
152 NULL will unregister the handler.
154 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
160 WatchdogRegisterHandler (
161 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
162 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
165 if ((mWatchdogNotify
== NULL
) && (NotifyFunction
== NULL
)) {
166 return EFI_INVALID_PARAMETER
;
169 if ((mWatchdogNotify
!= NULL
) && (NotifyFunction
!= NULL
)) {
170 return EFI_ALREADY_STARTED
;
173 mWatchdogNotify
= NotifyFunction
;
178 This function sets the amount of time to wait before firing the watchdog
179 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog
182 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
183 @param TimerPeriod The amount of time in 100ns units to wait before
184 the watchdog timer is fired. If TimerPeriod is zero,
185 then the watchdog timer is disabled.
187 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
188 in TimerPeriod 100ns units.
194 WatchdogSetTimerPeriod (
195 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
196 IN UINT64 TimerPeriod
// In 100ns units
201 // if TimerPeriod is 0, this is a request to stop the watchdog.
202 if (TimerPeriod
== 0) {
208 // Work out how many timer ticks will equate to TimerPeriod
209 mNumTimerTicks
= (mTimerFrequencyHz
* TimerPeriod
) / TIME_UNITS_PER_SECOND
;
211 /* If the number of required ticks is greater than the max the watchdog's
212 offset register (WOR) can hold, we need to manually compute and set
213 the compare register (WCV) */
214 if (mNumTimerTicks
> MAX_UINT32
) {
215 /* We need to enable the watchdog *before* writing to the compare register,
216 because enabling the watchdog causes an "explicit refresh", which
217 clobbers the compare register (WCV). In order to make sure this doesn't
218 trigger an interrupt, set the offset to max. */
219 WatchdogWriteOffsetRegister (MAX_UINT32
);
221 SystemCount
= ArmGenericTimerGetSystemCount ();
222 WatchdogWriteCompareRegister (SystemCount
+ mNumTimerTicks
);
224 WatchdogWriteOffsetRegister ((UINT32
)mNumTimerTicks
);
232 This function retrieves the period of timer interrupts in 100ns units,
233 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
234 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
235 returned, then the timer is currently disabled.
237 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
238 @param TimerPeriod A pointer to the timer period to retrieve in
239 100ns units. If 0 is returned, then the timer is
243 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
244 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
250 WatchdogGetTimerPeriod (
251 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL
*This
,
252 OUT UINT64
*TimerPeriod
255 if (TimerPeriod
== NULL
) {
256 return EFI_INVALID_PARAMETER
;
259 *TimerPeriod
= ((TIME_UNITS_PER_SECOND
/ mTimerFrequencyHz
) * mNumTimerTicks
);
265 Interface structure for the Watchdog Architectural Protocol.
267 @par Protocol Description:
268 This protocol provides a service to set the amount of time to wait
269 before firing the watchdog timer, and it also provides a service to
270 register a handler that is invoked when the watchdog timer fires.
272 @par When the watchdog timer fires, control will be passed to a handler
273 if one has been registered. If no handler has been registered,
274 or the registered handler returns, then the system will be
275 reset by calling the Runtime Service ResetSystem().
277 @param RegisterHandler
278 Registers a handler that will be called each time the
279 watchdogtimer interrupt fires. TimerPeriod defines the minimum
280 time between timer interrupts, so TimerPeriod will also
281 be the minimum time between calls to the registered
283 NOTE: If the watchdog resets the system in hardware, then
284 this function will not have any chance of executing.
286 @param SetTimerPeriod
287 Sets the period of the timer interrupt in 100ns units.
288 This function is optional, and may return EFI_UNSUPPORTED.
289 If this function is supported, then the timer period will
290 be rounded up to the nearest supported timer period.
292 @param GetTimerPeriod
293 Retrieves the period of the timer interrupt in 100ns units.
296 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer
= {
297 WatchdogRegisterHandler
,
298 WatchdogSetTimerPeriod
,
299 WatchdogGetTimerPeriod
302 STATIC EFI_EVENT mEfiExitBootServicesEvent
;
306 GenericWatchdogEntry (
307 IN EFI_HANDLE ImageHandle
,
308 IN EFI_SYSTEM_TABLE
*SystemTable
314 Status
= gBS
->LocateProtocol (
315 &gHardwareInterrupt2ProtocolGuid
,
317 (VOID
**)&mInterruptProtocol
319 ASSERT_EFI_ERROR (Status
);
321 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
323 This will avoid conflicts with the universal watchdog */
324 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiWatchdogTimerArchProtocolGuid
);
326 mTimerFrequencyHz
= ArmGenericTimerGetTimerFreq ();
327 ASSERT (mTimerFrequencyHz
!= 0);
329 // Install interrupt handler
330 Status
= mInterruptProtocol
->RegisterInterruptSource (
332 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),
333 WatchdogInterruptHandler
335 if (EFI_ERROR (Status
)) {
339 Status
= mInterruptProtocol
->SetTriggerType (
341 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),
342 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING
344 if (EFI_ERROR (Status
)) {
345 goto UnregisterHandler
;
348 // Install the Timer Architectural Protocol onto a new handle
350 Status
= gBS
->InstallMultipleProtocolInterfaces (
352 &gEfiWatchdogTimerArchProtocolGuid
,
356 if (EFI_ERROR (Status
)) {
357 goto UnregisterHandler
;
360 // Register for an ExitBootServicesEvent
361 Status
= gBS
->CreateEvent (
362 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
364 WatchdogExitBootServicesEvent
,
366 &mEfiExitBootServicesEvent
368 ASSERT_EFI_ERROR (Status
);
376 // Unregister the handler
377 mInterruptProtocol
->RegisterInterruptSource (
379 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum
),