]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 (
123 EfiResetCold,
124 EFI_TIMEOUT,
125 StrSize (ResetString),
126 (CHAR16 *)ResetString
127 );
128
129 // If we got here then the reset didn't work
130 ASSERT (FALSE);
131 }
132
133 /**
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.
145
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.
153
154 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
155
156 **/
157 STATIC
158 EFI_STATUS
159 EFIAPI
160 WatchdogRegisterHandler (
161 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
162 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
163 )
164 {
165 if ((mWatchdogNotify == NULL) && (NotifyFunction == NULL)) {
166 return EFI_INVALID_PARAMETER;
167 }
168
169 if ((mWatchdogNotify != NULL) && (NotifyFunction != NULL)) {
170 return EFI_ALREADY_STARTED;
171 }
172
173 mWatchdogNotify = NotifyFunction;
174 return EFI_SUCCESS;
175 }
176
177 /**
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
180 timer is disabled.
181
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.
186
187 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
188 in TimerPeriod 100ns units.
189
190 **/
191 STATIC
192 EFI_STATUS
193 EFIAPI
194 WatchdogSetTimerPeriod (
195 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
196 IN UINT64 TimerPeriod // In 100ns units
197 )
198 {
199 UINTN SystemCount;
200
201 // if TimerPeriod is 0, this is a request to stop the watchdog.
202 if (TimerPeriod == 0) {
203 mNumTimerTicks = 0;
204 WatchdogDisable ();
205 return EFI_SUCCESS;
206 }
207
208 // Work out how many timer ticks will equate to TimerPeriod
209 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;
210
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);
220 WatchdogEnable ();
221 SystemCount = ArmGenericTimerGetSystemCount ();
222 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);
223 } else {
224 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);
225 WatchdogEnable ();
226 }
227
228 return EFI_SUCCESS;
229 }
230
231 /**
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.
236
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
240 currently disabled.
241
242
243 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
244 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
245
246 **/
247 STATIC
248 EFI_STATUS
249 EFIAPI
250 WatchdogGetTimerPeriod (
251 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
252 OUT UINT64 *TimerPeriod
253 )
254 {
255 if (TimerPeriod == NULL) {
256 return EFI_INVALID_PARAMETER;
257 }
258
259 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);
260
261 return EFI_SUCCESS;
262 }
263
264 /**
265 Interface structure for the Watchdog Architectural Protocol.
266
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.
271
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().
276
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
282 handler.
283 NOTE: If the watchdog resets the system in hardware, then
284 this function will not have any chance of executing.
285
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.
291
292 @param GetTimerPeriod
293 Retrieves the period of the timer interrupt in 100ns units.
294
295 **/
296 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
297 WatchdogRegisterHandler,
298 WatchdogSetTimerPeriod,
299 WatchdogGetTimerPeriod
300 };
301
302 STATIC EFI_EVENT mEfiExitBootServicesEvent;
303
304 EFI_STATUS
305 EFIAPI
306 GenericWatchdogEntry (
307 IN EFI_HANDLE ImageHandle,
308 IN EFI_SYSTEM_TABLE *SystemTable
309 )
310 {
311 EFI_STATUS Status;
312 EFI_HANDLE Handle;
313
314 Status = gBS->LocateProtocol (
315 &gHardwareInterrupt2ProtocolGuid,
316 NULL,
317 (VOID **)&mInterruptProtocol
318 );
319 ASSERT_EFI_ERROR (Status);
320
321 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
322 in the system yet.
323 This will avoid conflicts with the universal watchdog */
324 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
325
326 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();
327 ASSERT (mTimerFrequencyHz != 0);
328
329 // Install interrupt handler
330 Status = mInterruptProtocol->RegisterInterruptSource (
331 mInterruptProtocol,
332 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
333 WatchdogInterruptHandler
334 );
335 if (EFI_ERROR (Status)) {
336 return Status;
337 }
338
339 Status = mInterruptProtocol->SetTriggerType (
340 mInterruptProtocol,
341 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
342 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING
343 );
344 if (EFI_ERROR (Status)) {
345 goto UnregisterHandler;
346 }
347
348 // Install the Timer Architectural Protocol onto a new handle
349 Handle = NULL;
350 Status = gBS->InstallMultipleProtocolInterfaces (
351 &Handle,
352 &gEfiWatchdogTimerArchProtocolGuid,
353 &mWatchdogTimer,
354 NULL
355 );
356 if (EFI_ERROR (Status)) {
357 goto UnregisterHandler;
358 }
359
360 // Register for an ExitBootServicesEvent
361 Status = gBS->CreateEvent (
362 EVT_SIGNAL_EXIT_BOOT_SERVICES,
363 TPL_NOTIFY,
364 WatchdogExitBootServicesEvent,
365 NULL,
366 &mEfiExitBootServicesEvent
367 );
368 ASSERT_EFI_ERROR (Status);
369
370 mNumTimerTicks = 0;
371 WatchdogDisable ();
372
373 return EFI_SUCCESS;
374
375 UnregisterHandler:
376 // Unregister the handler
377 mInterruptProtocol->RegisterInterruptSource (
378 mInterruptProtocol,
379 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
380 NULL
381 );
382 return Status;
383 }