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