]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Drivers/GenericWatchdogDxe/GenericWatchdogDxe.c
ArmPkg/GenericWatchdogDxe: implement RegisterHandler() method
[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 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;
46
47 STATIC
48 VOID
49 WatchdogWriteOffsetRegister (
50 UINT32 Value
51 )
52 {
53 MmioWrite32 (GENERIC_WDOG_OFFSET_REG, Value);
54 }
55
56 STATIC
57 VOID
58 WatchdogWriteCompareRegister (
59 UINT64 Value
60 )
61 {
62 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_LOW, Value & MAX_UINT32);
63 MmioWrite32 (GENERIC_WDOG_COMPARE_VALUE_REG_HIGH, (Value >> 32) & MAX_UINT32);
64 }
65
66 STATIC
67 VOID
68 WatchdogEnable (
69 VOID
70 )
71 {
72 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_ENABLED);
73 }
74
75 STATIC
76 VOID
77 WatchdogDisable (
78 VOID
79 )
80 {
81 MmioWrite32 (GENERIC_WDOG_CONTROL_STATUS_REG, GENERIC_WDOG_DISABLED);
82 }
83
84 /** On exiting boot services we must make sure the Watchdog Timer
85 is stopped.
86 **/
87 STATIC
88 VOID
89 EFIAPI
90 WatchdogExitBootServicesEvent (
91 IN EFI_EVENT Event,
92 IN VOID *Context
93 )
94 {
95 WatchdogDisable ();
96 mNumTimerTicks = 0;
97 }
98
99 /* This function is called when the watchdog's first signal (WS0) goes high.
100 It uses the ResetSystem Runtime Service to reset the board.
101 */
102 STATIC
103 VOID
104 EFIAPI
105 WatchdogInterruptHandler (
106 IN HARDWARE_INTERRUPT_SOURCE Source,
107 IN EFI_SYSTEM_CONTEXT SystemContext
108 )
109 {
110 STATIC CONST CHAR16 ResetString[]= L"The generic watchdog timer ran out.";
111 UINT64 TimerPeriod;
112
113 WatchdogDisable ();
114
115 mInterruptProtocol->EndOfInterrupt (mInterruptProtocol, Source);
116
117 //
118 // The notify function should be called with the elapsed number of ticks
119 // since the watchdog was armed, which should exceed the timer period.
120 // We don't actually know the elapsed number of ticks, so let's return
121 // the timer period plus 1.
122 //
123 if (mWatchdogNotify != NULL) {
124 TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);
125 mWatchdogNotify (TimerPeriod + 1);
126 }
127
128 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, StrSize (ResetString),
129 (CHAR16 *)ResetString);
130
131 // If we got here then the reset didn't work
132 ASSERT (FALSE);
133 }
134
135 /**
136 This function registers the handler NotifyFunction so it is called every time
137 the watchdog timer expires. It also passes the amount of time since the last
138 handler call to the NotifyFunction.
139 If NotifyFunction is not NULL and a handler is not already registered,
140 then the new handler is registered and EFI_SUCCESS is returned.
141 If NotifyFunction is NULL, and a handler is already registered,
142 then that handler is unregistered.
143 If an attempt is made to register a handler when a handler is already
144 registered, then EFI_ALREADY_STARTED is returned.
145 If an attempt is made to unregister a handler when a handler is not
146 registered, then EFI_INVALID_PARAMETER is returned.
147
148 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
149 @param NotifyFunction The function to call when a timer interrupt fires.
150 This function executes at TPL_HIGH_LEVEL. The DXE
151 Core will register a handler for the timer interrupt,
152 so it can know how much time has passed. This
153 information is used to signal timer based events.
154 NULL will unregister the handler.
155
156 @retval EFI_UNSUPPORTED The code does not support NotifyFunction.
157
158 **/
159 STATIC
160 EFI_STATUS
161 EFIAPI
162 WatchdogRegisterHandler (
163 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
164 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
165 )
166 {
167 if (mWatchdogNotify == NULL && NotifyFunction == NULL) {
168 return EFI_INVALID_PARAMETER;
169 }
170
171 if (mWatchdogNotify != NULL && NotifyFunction != NULL) {
172 return EFI_ALREADY_STARTED;
173 }
174
175 mWatchdogNotify = NotifyFunction;
176 return EFI_SUCCESS;
177 }
178
179 /**
180 This function sets the amount of time to wait before firing the watchdog
181 timer to TimerPeriod 100ns units. If TimerPeriod is 0, then the watchdog
182 timer is disabled.
183
184 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
185 @param TimerPeriod The amount of time in 100ns units to wait before
186 the watchdog timer is fired. If TimerPeriod is zero,
187 then the watchdog timer is disabled.
188
189 @retval EFI_SUCCESS The watchdog timer has been programmed to fire
190 in TimerPeriod 100ns units.
191
192 **/
193 STATIC
194 EFI_STATUS
195 EFIAPI
196 WatchdogSetTimerPeriod (
197 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
198 IN UINT64 TimerPeriod // In 100ns units
199 )
200 {
201 UINTN SystemCount;
202
203 // if TimerPeriod is 0, this is a request to stop the watchdog.
204 if (TimerPeriod == 0) {
205 mNumTimerTicks = 0;
206 WatchdogDisable ();
207 return EFI_SUCCESS;
208 }
209
210 // Work out how many timer ticks will equate to TimerPeriod
211 mNumTimerTicks = (mTimerFrequencyHz * TimerPeriod) / TIME_UNITS_PER_SECOND;
212
213 /* If the number of required ticks is greater than the max the watchdog's
214 offset register (WOR) can hold, we need to manually compute and set
215 the compare register (WCV) */
216 if (mNumTimerTicks > MAX_UINT32) {
217 /* We need to enable the watchdog *before* writing to the compare register,
218 because enabling the watchdog causes an "explicit refresh", which
219 clobbers the compare register (WCV). In order to make sure this doesn't
220 trigger an interrupt, set the offset to max. */
221 WatchdogWriteOffsetRegister (MAX_UINT32);
222 WatchdogEnable ();
223 SystemCount = ArmGenericTimerGetSystemCount ();
224 WatchdogWriteCompareRegister (SystemCount + mNumTimerTicks);
225 } else {
226 WatchdogWriteOffsetRegister ((UINT32)mNumTimerTicks);
227 WatchdogEnable ();
228 }
229
230 return EFI_SUCCESS;
231 }
232
233 /**
234 This function retrieves the period of timer interrupts in 100ns units,
235 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
236 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
237 returned, then the timer is currently disabled.
238
239 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
240 @param TimerPeriod A pointer to the timer period to retrieve in
241 100ns units. If 0 is returned, then the timer is
242 currently disabled.
243
244
245 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
246 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
247
248 **/
249 STATIC
250 EFI_STATUS
251 EFIAPI
252 WatchdogGetTimerPeriod (
253 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
254 OUT UINT64 *TimerPeriod
255 )
256 {
257 if (TimerPeriod == NULL) {
258 return EFI_INVALID_PARAMETER;
259 }
260
261 *TimerPeriod = ((TIME_UNITS_PER_SECOND / mTimerFrequencyHz) * mNumTimerTicks);
262
263 return EFI_SUCCESS;
264 }
265
266 /**
267 Interface structure for the Watchdog Architectural Protocol.
268
269 @par Protocol Description:
270 This protocol provides a service to set the amount of time to wait
271 before firing the watchdog timer, and it also provides a service to
272 register a handler that is invoked when the watchdog timer fires.
273
274 @par When the watchdog timer fires, control will be passed to a handler
275 if one has been registered. If no handler has been registered,
276 or the registered handler returns, then the system will be
277 reset by calling the Runtime Service ResetSystem().
278
279 @param RegisterHandler
280 Registers a handler that will be called each time the
281 watchdogtimer interrupt fires. TimerPeriod defines the minimum
282 time between timer interrupts, so TimerPeriod will also
283 be the minimum time between calls to the registered
284 handler.
285 NOTE: If the watchdog resets the system in hardware, then
286 this function will not have any chance of executing.
287
288 @param SetTimerPeriod
289 Sets the period of the timer interrupt in 100ns units.
290 This function is optional, and may return EFI_UNSUPPORTED.
291 If this function is supported, then the timer period will
292 be rounded up to the nearest supported timer period.
293
294 @param GetTimerPeriod
295 Retrieves the period of the timer interrupt in 100ns units.
296
297 **/
298 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
299 WatchdogRegisterHandler,
300 WatchdogSetTimerPeriod,
301 WatchdogGetTimerPeriod
302 };
303
304 STATIC EFI_EVENT mEfiExitBootServicesEvent;
305
306 EFI_STATUS
307 EFIAPI
308 GenericWatchdogEntry (
309 IN EFI_HANDLE ImageHandle,
310 IN EFI_SYSTEM_TABLE *SystemTable
311 )
312 {
313 EFI_STATUS Status;
314 EFI_HANDLE Handle;
315
316 Status = gBS->LocateProtocol (&gHardwareInterrupt2ProtocolGuid, NULL,
317 (VOID **)&mInterruptProtocol);
318 ASSERT_EFI_ERROR (Status);
319
320 /* Make sure the Watchdog Timer Architectural Protocol has not been installed
321 in the system yet.
322 This will avoid conflicts with the universal watchdog */
323 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
324
325 mTimerFrequencyHz = ArmGenericTimerGetTimerFreq ();
326 ASSERT (mTimerFrequencyHz != 0);
327
328 // Install interrupt handler
329 Status = mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,
330 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
331 WatchdogInterruptHandler);
332 if (EFI_ERROR (Status)) {
333 return Status;
334 }
335
336 Status = mInterruptProtocol->SetTriggerType (mInterruptProtocol,
337 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
338 EFI_HARDWARE_INTERRUPT2_TRIGGER_EDGE_RISING);
339 if (EFI_ERROR (Status)) {
340 goto UnregisterHandler;
341 }
342
343 // Install the Timer Architectural Protocol onto a new handle
344 Handle = NULL;
345 Status = gBS->InstallMultipleProtocolInterfaces (&Handle,
346 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,
347 NULL);
348 if (EFI_ERROR (Status)) {
349 goto UnregisterHandler;
350 }
351
352 // Register for an ExitBootServicesEvent
353 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,
354 WatchdogExitBootServicesEvent, NULL,
355 &mEfiExitBootServicesEvent);
356 ASSERT_EFI_ERROR (Status);
357
358 mNumTimerTicks = 0;
359 WatchdogDisable ();
360
361 return EFI_SUCCESS;
362
363 UnregisterHandler:
364 // Unregister the handler
365 mInterruptProtocol->RegisterInterruptSource (mInterruptProtocol,
366 FixedPcdGet32 (PcdGenericWatchdogEl2IntrNum),
367 NULL);
368 return Status;
369 }