]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
285727fc0e84c670ee6dfa719c7fba20f6fafb64
[mirror_edk2.git] / ArmPkg / Drivers / GenericWatchdogDxe / GenericWatchdogDxe.c
1 /** @file
2 *
3 * Copyright (c) 2013-2018, ARM Limited. All rights reserved.
4 *
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD
7 * License which accompanies this distribution. The full text of the license
8 * may be found at http://opensource.org/licenses/bsd-license.php
9 *
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 *
13 **/
14
15 #include <PiDxe.h>
16
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UefiRuntimeServicesTableLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/ArmGenericTimerCounterLib.h>
26
27 #include <Protocol/HardwareInterrupt2.h>
28 #include <Protocol/WatchdogTimer.h>
29
30 #include "GenericWatchdog.h"
31
32 /* The number of 100ns periods (the unit of time passed to these functions)
33 in a second */
34 #define TIME_UNITS_PER_SECOND 10000000
35
36 // Tick frequency of the generic timer basis of the generic watchdog.
37 STATIC UINTN mTimerFrequencyHz = 0;
38
39 /* In cases where the compare register was set manually, information about
40 how long the watchdog was asked to wait cannot be retrieved from hardware.
41 It is therefore stored here. 0 means the timer is not running. */
42 STATIC UINT64 mNumTimerTicks = 0;
43
44 STATIC EFI_HARDWARE_INTERRUPT2_PROTOCOL *mInterruptProtocol;
45
46 STATIC
47 VOID
48 WatchdogWriteOffsetRegister (
49 UINT32 Value
50 )
51 {
52 MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);
53 }
54
55 STATIC
56 VOID
57 WatchdogWriteCompareRegister (
58 UINT64 Value
59 )
60 {
61 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW, Value & MAX_UINT32);
62 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH, (Value >> 32) & MAX_UINT32);
63 }
64
65 STATIC
66 VOID
67 WatchdogEnable (
68 VOID
69 )
70 {
71 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);
72 }
73
74 STATIC
75 VOID
76 WatchdogDisable (
77 VOID
78 )
79 {
80 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);
81 }
82
83 /** On exiting boot services we must make sure the Watchdog Timer
84 is stopped.
85 **/
86 STATIC
87 VOID
88 EFIAPI
89 WatchdogExitBootServicesEvent (
90 IN EFI_EVENT Event,
91 IN VOID *Context
92 )
93 {
94 WatchdogDisable ();
95 mNumTimerTicks = 0;
96 }
97
98 /* This function is called when the watchdog's first signal (WS0) goes high.
99 It uses the ResetSystem Runtime Service to reset the board.
100 */
101 STATIC
102 VOID
103 EFIAPI
104 WatchdogInterruptHandler (
105 IN HARDWARE_INTERRUPT_SOURCE Source,
106 IN EFI_SYSTEM_CONTEXT SystemContext
107 )
108 {
109 STATIC CONST CHAR16 ResetString[]= L"The generic watchdog timer ran out.";
110
111 WatchdogDisable ();
112
113 mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source);
114
115 gRT->ResetSystem (
116 EfiResetCold,
117 EFI_TIMEOUT,
118 StrSize (ResetString),
119 (VOID *) &ResetString
120 );
121
122 // If we got here then the reset didn't work
123 ASSERT (FALSE);
124 }
125
126 /**
127 This function registers the handler NotifyFunction so it is called every time
128 the watchdog timer expires. It also passes the amount of time since the last
129 handler call to the NotifyFunction.
130 If NotifyFunction is not NULL and a handler is not already registered,
131 then the new handler is registered and EFI_SUCCESS is returned.
132 If NotifyFunction is NULL, and a handler is already registered,
133 then that handler is unregistered.
134 If an attempt is made to register a handler when a handler is already
135 registered, then EFI_ALREADY_STARTED is returned.
136 If an attempt is made to unregister a handler when a handler is not
137 registered, then EFI_INVALID_PARAMETER is returned.
138
139 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
140 @param NotifyFunction The function to call when a timer interrupt fires.
141 This function executes at TPL_HIGH_LEVEL. The DXE
142 Core will register a handler for the timer interrupt,
143 so it can know how much time has passed. This
144 information is used to signal timer based events.
145 NULL will unregister the handler.
146
147 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
148
149 **/
150 STATIC
151 EFI_STATUS
152 EFIAPI
153 WatchdogRegisterHandler (
154 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
155 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
156 )
157 {
158 // ERROR: This function is not supported.
159 // The watchdog will reset the board
160 return EFI_UNSUPPORTED;
161 }
162
163 /**
164 This function sets the amount of time to wait before firing the watchdog
165 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog
166 timer is disabled.
167
168 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
169 @param TimerPeriod The amount of time in 100ns units to wait before
170 the watchdog timer is fired. If TimerPeriod is zero,
171 then the watchdog timer is disabled.
172
173 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
174 in TimerPeriod 100ns units.
175
176 **/
177 STATIC
178 EFI_STATUS
179 EFIAPI
180 WatchdogSetTimerPeriod (
181 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
182 IN UINT64 TimerPeriod // In 100ns units
183 )
184 {
185 UINTN SystemCount;
186
187 // if TimerPeriod is 0, this is a request to stop the watchdog.
188 if (TimerPeriod == 0) {
189 mNumTimerTicks = 0;
190 WatchdogDisable ();
191 return EFI_SUCCESS;
192 }
193
194 // Work out how many timer ticks will equate to TimerPeriod
195 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;
196
197 /* If the number of required ticks is greater than the max the watchdog's
198 offset register (WOR) can hold, we need to manually compute and set
199 the compare register (WCV) */
200 if (mNumTimerTicks > MAX_UINT32) {
201 /* We need to enable the watchdog *before* writing to the compare register,
202 because enabling the watchdog causes an "explicit refresh", which
203 clobbers the compare register (WCV). In order to make sure this doesn't
204 trigger an interrupt, set the offset to max. */
205 WatchdogWriteOffsetRegister (MAX_UINT32);
206 WatchdogEnable ();
207 SystemCount = ArmGenericTimerGetSystemCount ();
208 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);
209 } else {
210 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);
211 WatchdogEnable ();
212 }
213
214 return EFI_SUCCESS;
215 }
216
217 /**
218 This function retrieves the period of timer interrupts in 100ns units,
219 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
220 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
221 returned, then the timer is currently disabled.
222
223 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
224 @param TimerPeriod A pointer to the timer period to retrieve in
225 100ns units. If 0 is returned, then the timer is
226 currently disabled.
227
228
229 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
230 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
231
232 **/
233 STATIC
234 EFI_STATUS
235 EFIAPI
236 WatchdogGetTimerPeriod (
237 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
238 OUT UINT64 *TimerPeriod
239 )
240 {
241 if (TimerPeriod == NULL) {
242 return EFI_INVALID_PARAMETER;
243 }
244
245 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);
246
247 return EFI_SUCCESS;
248 }
249
250 /**
251 Interface structure for the Watchdog Architectural Protocol.
252
253 @par Protocol Description:
254 This protocol provides a service to set the amount of time to wait
255 before firing the watchdog timer, and it also provides a service to
256 register a handler that is invoked when the watchdog timer fires.
257
258 @par When the watchdog timer fires, control will be passed to a handler
259 if one has been registered. If no handler has been registered,
260 or the registered handler returns, then the system will be
261 reset by calling the Runtime Service ResetSystem().
262
263 @param RegisterHandler
264 Registers a handler that will be called each time the
265 watchdogtimer interrupt fires. TimerPeriod defines the minimum
266 time between timer interrupts, so TimerPeriod will also
267 be the minimum time between calls to the registered
268 handler.
269 NOTE: If the watchdog resets the system in hardware, then
270 this function will not have any chance of executing.
271
272 @param SetTimerPeriod
273 Sets the period of the timer interrupt in 100ns units.
274 This function is optional, and may return EFI_UNSUPPORTED.
275 If this function is supported, then the timer period will
276 be rounded up to the nearest supported timer period.
277
278 @param GetTimerPeriod
279 Retrieves the period of the timer interrupt in 100ns units.
280
281 **/
282 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
283 WatchdogRegisterHandler,
284 WatchdogSetTimerPeriod,
285 WatchdogGetTimerPeriod
286 };
287
288 STATIC EFI_EVENT mEfiExitBootServicesEvent;
289
290 EFI_STATUS
291 EFIAPI
292 GenericWatchdogEntry (
293 IN EFI_HANDLE ImageHandle,
294 IN EFI_SYSTEM_TABLE *SystemTable
295 )
296 {
297 EFI_STATUS Status;
298 EFI_HANDLE Handle;
299
300 Status = gBS->LocateProtocol (&gHardwareInterrupt2ProtocolGuid, NULL,
301 (VOID **)&mInterruptProtocol);
302 ASSERT_EFI_ERROR (Status);
303
304 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
305 in the system yet.
306 This will avoid conflicts with the universal watchdog */
307 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
308
309 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();
310 ASSERT (mTimerFrequencyHz != 0);
311
312 // Install interrupt handler
313 Status = mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,
314 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
315 WatchdogInterruptHandler);
316 if (EFI_ERROR (Status)) {
317 return Status;
318 }
319
320 Status = mInterruptProtocol->SetTriggerType (mInterruptProtocol,
321 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
322 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING);
323 if (EFI_ERROR (Status)) {
324 goto UnregisterHandler;
325 }
326
327 // Install the Timer Architectural Protocol onto a new handle
328 Handle = NULL;
329 Status = gBS->InstallMultipleProtocolInterfaces (&Handle,
330 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,
331 NULL);
332 if (EFI_ERROR (Status)) {
333 goto UnregisterHandler;
334 }
335
336 // Register for an ExitBootServicesEvent
337 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,
338 WatchdogExitBootServicesEvent, NULL,
339 &mEfiExitBootServicesEvent);
340 ASSERT_EFI_ERROR (Status);
341
342 mNumTimerTicks = 0;
343 WatchdogDisable ();
344
345 return EFI_SUCCESS;
346
347 UnregisterHandler:
348 // Unregister the handler
349 mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,
350 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
351 NULL);
352 return Status;
353 }