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