]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
ArmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / ArmPkg / Drivers / GenericWatchdogDxe / GenericWatchdogDxe.c
1 /** @file
2 *
3 * Copyright (c) 2013-2018, ARM Limited. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause-Patent
6 *
7 **/
8
9 #include <PiDxe.h>
10
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>
20
21 #include <Protocol/HardwareInterrupt2.h>
22 #include <Protocol/WatchdogTimer.h>
23
24 #include "GenericWatchdog.h"
25
26 /* The number of 100ns periods (the unit of time passed to these functions)
27 in a second */
28 #define TIME_UNITS_PER_SECOND 10000000
29
30 // Tick frequency of the generic timer basis of the generic watchdog.
31 STATIC UINTN mTimerFrequencyHz = 0;
32
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;
37
38 STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;
39 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;
40
41 STATIC
42 VOID
43 WatchdogWriteOffsetRegister (
44 UINT32 Value
45 )
46 {
47 MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);
48 }
49
50 STATIC
51 VOID
52 WatchdogWriteCompareRegister (
53 UINT64 Value
54 )
55 {
56 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW, Value & MAX_UINT32);
57 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH, (Value >> 32) & MAX_UINT32);
58 }
59
60 STATIC
61 VOID
62 WatchdogEnable (
63 VOID
64 )
65 {
66 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);
67 }
68
69 STATIC
70 VOID
71 WatchdogDisable (
72 VOID
73 )
74 {
75 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);
76 }
77
78 /** On exiting boot services we must make sure the Watchdog Timer
79 is stopped.
80 **/
81 STATIC
82 VOID
83 EFIAPI
84 WatchdogExitBootServicesEvent (
85 IN EFI_EVENT Event,
86 IN VOID *Context
87 )
88 {
89 WatchdogDisable ();
90 mNumTimerTicks = 0;
91 }
92
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.
95 */
96 STATIC
97 VOID
98 EFIAPI
99 WatchdogInterruptHandler (
100 IN HARDWARE_INTERRUPT_SOURCE Source,
101 IN EFI_SYSTEM_CONTEXT SystemContext
102 )
103 {
104 STATIC CONST CHAR16 ResetString[]= L"The generic watchdog timer ran out.";
105 UINT64 TimerPeriod;
106
107 WatchdogDisable ();
108
109 mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source);
110
111 //
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.
116 //
117 if (mWatchdogNotify != NULL) {
118 TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);
119 mWatchdogNotify (TimerPeriod + 1);
120 }
121
122 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, StrSize (ResetString),
123 (CHAR16 *)ResetString);
124
125 // If we got here then the reset didn't work
126 ASSERT (FALSE);
127 }
128
129 /**
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.
141
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.
149
150 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
151
152 **/
153 STATIC
154 EFI_STATUS
155 EFIAPI
156 WatchdogRegisterHandler (
157 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
158 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
159 )
160 {
161 if (mWatchdogNotify == NULL && NotifyFunction == NULL) {
162 return EFI_INVALID_PARAMETER;
163 }
164
165 if (mWatchdogNotify != NULL && NotifyFunction != NULL) {
166 return EFI_ALREADY_STARTED;
167 }
168
169 mWatchdogNotify = NotifyFunction;
170 return EFI_SUCCESS;
171 }
172
173 /**
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
176 timer is disabled.
177
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.
182
183 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
184 in TimerPeriod 100ns units.
185
186 **/
187 STATIC
188 EFI_STATUS
189 EFIAPI
190 WatchdogSetTimerPeriod (
191 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
192 IN UINT64 TimerPeriod // In 100ns units
193 )
194 {
195 UINTN SystemCount;
196
197 // if TimerPeriod is 0, this is a request to stop the watchdog.
198 if (TimerPeriod == 0) {
199 mNumTimerTicks = 0;
200 WatchdogDisable ();
201 return EFI_SUCCESS;
202 }
203
204 // Work out how many timer ticks will equate to TimerPeriod
205 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;
206
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);
216 WatchdogEnable ();
217 SystemCount = ArmGenericTimerGetSystemCount ();
218 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);
219 } else {
220 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);
221 WatchdogEnable ();
222 }
223
224 return EFI_SUCCESS;
225 }
226
227 /**
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.
232
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
236 currently disabled.
237
238
239 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
240 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
241
242 **/
243 STATIC
244 EFI_STATUS
245 EFIAPI
246 WatchdogGetTimerPeriod (
247 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
248 OUT UINT64 *TimerPeriod
249 )
250 {
251 if (TimerPeriod == NULL) {
252 return EFI_INVALID_PARAMETER;
253 }
254
255 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);
256
257 return EFI_SUCCESS;
258 }
259
260 /**
261 Interface structure for the Watchdog Architectural Protocol.
262
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.
267
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().
272
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
278 handler.
279 NOTE: If the watchdog resets the system in hardware, then
280 this function will not have any chance of executing.
281
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.
287
288 @param GetTimerPeriod
289 Retrieves the period of the timer interrupt in 100ns units.
290
291 **/
292 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
293 WatchdogRegisterHandler,
294 WatchdogSetTimerPeriod,
295 WatchdogGetTimerPeriod
296 };
297
298 STATIC EFI_EVENT mEfiExitBootServicesEvent;
299
300 EFI_STATUS
301 EFIAPI
302 GenericWatchdogEntry (
303 IN EFI_HANDLE ImageHandle,
304 IN EFI_SYSTEM_TABLE *SystemTable
305 )
306 {
307 EFI_STATUS Status;
308 EFI_HANDLE Handle;
309
310 Status = gBS->LocateProtocol (&gHardwareInterrupt2ProtocolGuid, NULL,
311 (VOID **)&mInterruptProtocol);
312 ASSERT_EFI_ERROR (Status);
313
314 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
315 in the system yet.
316 This will avoid conflicts with the universal watchdog */
317 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
318
319 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();
320 ASSERT (mTimerFrequencyHz != 0);
321
322 // Install interrupt handler
323 Status = mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,
324 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
325 WatchdogInterruptHandler);
326 if (EFI_ERROR (Status)) {
327 return Status;
328 }
329
330 Status = mInterruptProtocol->SetTriggerType (mInterruptProtocol,
331 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
332 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING);
333 if (EFI_ERROR (Status)) {
334 goto UnregisterHandler;
335 }
336
337 // Install the Timer Architectural Protocol onto a new handle
338 Handle = NULL;
339 Status = gBS->InstallMultipleProtocolInterfaces (&Handle,
340 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,
341 NULL);
342 if (EFI_ERROR (Status)) {
343 goto UnregisterHandler;
344 }
345
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);
351
352 mNumTimerTicks = 0;
353 WatchdogDisable ();
354
355 return EFI_SUCCESS;
356
357 UnregisterHandler:
358 // Unregister the handler
359 mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,
360 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
361 NULL);
362 return Status;
363 }