]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/SP805WatchdogDxe/SP805Watchdog.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPlatformPkg / Drivers / SP805WatchdogDxe / SP805Watchdog.c
1 /** @file
2 *
3 * Copyright (c) 2011-2013, ARM Limited. All rights reserved.
4 * Copyright (c) 2018, Linaro Limited. All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause-Patent
7 *
8 **/
9
10
11 #include <PiDxe.h>
12
13 #include <Library/BaseLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/IoLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/UefiRuntimeServicesTableLib.h>
19
20 #include <Protocol/HardwareInterrupt.h>
21 #include <Protocol/WatchdogTimer.h>
22
23 #include "SP805Watchdog.h"
24
25 STATIC EFI_EVENT mEfiExitBootServicesEvent;
26 STATIC EFI_HARDWARE_INTERRUPT_PROTOCOL *mInterrupt;
27 STATIC EFI_WATCHDOG_TIMER_NOTIFY mWatchdogNotify;
28 STATIC UINT32 mTimerPeriod;
29
30 /**
31 Make sure the SP805 registers are unlocked for writing.
32
33 Note: The SP805 Watchdog Timer supports locking of its registers,
34 i.e. it inhibits all writes to avoid rogue software accidentally
35 corrupting their contents.
36 **/
37 STATIC
38 VOID
39 SP805Unlock (
40 VOID
41 )
42 {
43 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_LOCKED) {
44 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_SPECIAL_UNLOCK_CODE);
45 }
46 }
47
48 /**
49 Make sure the SP805 registers are locked and can not be overwritten.
50
51 Note: The SP805 Watchdog Timer supports locking of its registers,
52 i.e. it inhibits all writes to avoid rogue software accidentally
53 corrupting their contents.
54 **/
55 STATIC
56 VOID
57 SP805Lock (
58 VOID
59 )
60 {
61 if (MmioRead32 (SP805_WDOG_LOCK_REG) == SP805_WDOG_LOCK_IS_UNLOCKED) {
62 // To lock it, just write in any number (except the special unlock code).
63 MmioWrite32 (SP805_WDOG_LOCK_REG, SP805_WDOG_LOCK_IS_LOCKED);
64 }
65 }
66
67 STATIC
68 VOID
69 EFIAPI
70 SP805InterruptHandler (
71 IN HARDWARE_INTERRUPT_SOURCE Source,
72 IN EFI_SYSTEM_CONTEXT SystemContext
73 )
74 {
75 SP805Unlock ();
76 MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq
77 SP805Lock ();
78
79 mInterrupt->EndOfInterrupt (mInterrupt, Source);
80
81 //
82 // The notify function should be called with the elapsed number of ticks
83 // since the watchdog was armed, which should exceed the timer period.
84 // We don't actually know the elapsed number of ticks, so let's return
85 // the timer period plus 1.
86 //
87 if (mWatchdogNotify != NULL) {
88 mWatchdogNotify (mTimerPeriod + 1);
89 }
90
91 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);
92 }
93
94 /**
95 Stop the SP805 watchdog timer from counting down by disabling interrupts.
96 **/
97 STATIC
98 VOID
99 SP805Stop (
100 VOID
101 )
102 {
103 // Disable interrupts
104 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) != 0) {
105 MmioAnd32 (SP805_WDOG_CONTROL_REG, ~SP805_WDOG_CTRL_INTEN);
106 }
107 }
108
109 /**
110 Starts the SP805 counting down by enabling interrupts.
111 The count down will start from the value stored in the Load register,
112 not from the value where it was previously stopped.
113 **/
114 STATIC
115 VOID
116 SP805Start (
117 VOID
118 )
119 {
120 // Enable interrupts
121 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_INTEN) == 0) {
122 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_INTEN);
123 }
124 }
125
126 /**
127 On exiting boot services we must make sure the SP805 Watchdog Timer
128 is stopped.
129 **/
130 STATIC
131 VOID
132 EFIAPI
133 ExitBootServicesEvent (
134 IN EFI_EVENT Event,
135 IN VOID *Context
136 )
137 {
138 SP805Unlock ();
139 SP805Stop ();
140 SP805Lock ();
141 }
142
143 /**
144 This function registers the handler NotifyFunction so it is called every time
145 the watchdog timer expires. It also passes the amount of time since the last
146 handler call to the NotifyFunction.
147 If NotifyFunction is not NULL and a handler is not already registered,
148 then the new handler is registered and EFI_SUCCESS is returned.
149 If NotifyFunction is NULL, and a handler is already registered,
150 then that handler is unregistered.
151 If an attempt is made to register a handler when a handler is already registered,
152 then EFI_ALREADY_STARTED is returned.
153 If an attempt is made to unregister a handler when a handler is not registered,
154 then EFI_INVALID_PARAMETER is returned.
155
156 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
157 @param NotifyFunction The function to call when a timer interrupt fires. This
158 function executes at TPL_HIGH_LEVEL. The DXE Core will
159 register a handler for the timer interrupt, so it can know
160 how much time has passed. This information is used to
161 signal timer based events. NULL will unregister the handler.
162
163 @retval EFI_SUCCESS The watchdog timer handler was registered.
164 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
165 registered.
166 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
167 previously registered.
168
169 **/
170 STATIC
171 EFI_STATUS
172 EFIAPI
173 SP805RegisterHandler (
174 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
175 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
176 )
177 {
178 if (mWatchdogNotify == NULL && NotifyFunction == NULL) {
179 return EFI_INVALID_PARAMETER;
180 }
181
182 if (mWatchdogNotify != NULL && NotifyFunction != NULL) {
183 return EFI_ALREADY_STARTED;
184 }
185
186 mWatchdogNotify = NotifyFunction;
187 return EFI_SUCCESS;
188 }
189
190 /**
191
192 This function adjusts the period of timer interrupts to the value specified
193 by TimerPeriod. If the timer period is updated, then the selected timer
194 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
195 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
196 If an error occurs while attempting to update the timer period, then the
197 timer hardware will be put back in its state prior to this call, and
198 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
199 is disabled. This is not the same as disabling the CPU's interrupts.
200 Instead, it must either turn off the timer hardware, or it must adjust the
201 interrupt controller so that a CPU interrupt is not generated when the timer
202 interrupt fires.
203
204 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
205 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
206 the timer hardware is not programmable, then EFI_UNSUPPORTED is
207 returned. If the timer is programmable, then the timer period
208 will be rounded up to the nearest timer period that is supported
209 by the timer hardware. If TimerPeriod is set to 0, then the
210 timer interrupts will be disabled.
211
212
213 @retval EFI_SUCCESS The timer period was changed.
214 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
215 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
216
217 **/
218 STATIC
219 EFI_STATUS
220 EFIAPI
221 SP805SetTimerPeriod (
222 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
223 IN UINT64 TimerPeriod // In 100ns units
224 )
225 {
226 EFI_STATUS Status;
227 UINT64 Ticks64bit;
228
229 SP805Unlock ();
230
231 Status = EFI_SUCCESS;
232
233 if (TimerPeriod == 0) {
234 // This is a watchdog stop request
235 SP805Stop ();
236 } else {
237 // Calculate the Watchdog ticks required for a delay of (TimerTicks * 100) nanoseconds
238 // The SP805 will count down to zero and generate an interrupt.
239 //
240 // WatchdogTicks = ((TimerPeriod * 100 * SP805_CLOCK_FREQUENCY) / 1GHz);
241 //
242 // i.e.:
243 //
244 // WatchdogTicks = (TimerPeriod * SP805_CLOCK_FREQUENCY) / 10 MHz ;
245
246 Ticks64bit = MultU64x32 (TimerPeriod, PcdGet32 (PcdSP805WatchdogClockFrequencyInHz));
247 Ticks64bit = DivU64x32 (Ticks64bit, 10 * 1000 * 1000);
248
249 // The registers in the SP805 are only 32 bits
250 if (Ticks64bit > MAX_UINT32) {
251 // We could load the watchdog with the maximum supported value but
252 // if a smaller value was requested, this could have the watchdog
253 // triggering before it was intended.
254 // Better generate an error to let the caller know.
255 Status = EFI_DEVICE_ERROR;
256 goto EXIT;
257 }
258
259 // Update the watchdog with a 32-bit value.
260 MmioWrite32 (SP805_WDOG_LOAD_REG, (UINT32)Ticks64bit);
261
262 // Start the watchdog
263 SP805Start ();
264 }
265
266 mTimerPeriod = TimerPeriod;
267
268 EXIT:
269 // Ensure the watchdog is locked before exiting.
270 SP805Lock ();
271 ASSERT_EFI_ERROR (Status);
272 return Status;
273 }
274
275 /**
276 This function retrieves the period of timer interrupts in 100 ns units,
277 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
278 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
279 returned, then the timer is currently disabled.
280
281 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
282 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
283 0 is returned, then the timer is currently disabled.
284
285
286 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
287 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
288
289 **/
290 STATIC
291 EFI_STATUS
292 EFIAPI
293 SP805GetTimerPeriod (
294 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
295 OUT UINT64 *TimerPeriod
296 )
297 {
298 if (TimerPeriod == NULL) {
299 return EFI_INVALID_PARAMETER;
300 }
301
302 *TimerPeriod = mTimerPeriod;
303 return EFI_SUCCESS;
304 }
305
306 /**
307 Interface structure for the Watchdog Architectural Protocol.
308
309 @par Protocol Description:
310 This protocol provides a service to set the amount of time to wait
311 before firing the watchdog timer, and it also provides a service to
312 register a handler that is invoked when the watchdog timer fires.
313
314 @par When the watchdog timer fires, control will be passed to a handler
315 if one has been registered. If no handler has been registered,
316 or the registered handler returns, then the system will be
317 reset by calling the Runtime Service ResetSystem().
318
319 @param RegisterHandler
320 Registers a handler that will be called each time the
321 watchdogtimer interrupt fires. TimerPeriod defines the minimum
322 time between timer interrupts, so TimerPeriod will also
323 be the minimum time between calls to the registered
324 handler.
325 NOTE: If the watchdog resets the system in hardware, then
326 this function will not have any chance of executing.
327
328 @param SetTimerPeriod
329 Sets the period of the timer interrupt in 100 nS units.
330 This function is optional, and may return EFI_UNSUPPORTED.
331 If this function is supported, then the timer period will
332 be rounded up to the nearest supported timer period.
333
334 @param GetTimerPeriod
335 Retrieves the period of the timer interrupt in 100 nS units.
336
337 **/
338 STATIC EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
339 SP805RegisterHandler,
340 SP805SetTimerPeriod,
341 SP805GetTimerPeriod
342 };
343
344 /**
345 Initialize the state information for the Watchdog Timer Architectural Protocol.
346
347 @param ImageHandle of the loaded driver
348 @param SystemTable Pointer to the System Table
349
350 @retval EFI_SUCCESS Protocol registered
351 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
352 @retval EFI_DEVICE_ERROR Hardware problems
353
354 **/
355 EFI_STATUS
356 EFIAPI
357 SP805Initialize (
358 IN EFI_HANDLE ImageHandle,
359 IN EFI_SYSTEM_TABLE *SystemTable
360 )
361 {
362 EFI_STATUS Status;
363 EFI_HANDLE Handle;
364
365 // Find the interrupt controller protocol. ASSERT if not found.
366 Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL,
367 (VOID **)&mInterrupt);
368 ASSERT_EFI_ERROR (Status);
369
370 // Unlock access to the SP805 registers
371 SP805Unlock ();
372
373 // Stop the watchdog from triggering unexpectedly
374 SP805Stop ();
375
376 // Set the watchdog to reset the board when triggered
377 // This is a last resort in case the interrupt handler fails
378 if ((MmioRead32 (SP805_WDOG_CONTROL_REG) & SP805_WDOG_CTRL_RESEN) == 0) {
379 MmioOr32 (SP805_WDOG_CONTROL_REG, SP805_WDOG_CTRL_RESEN);
380 }
381
382 // Clear any pending interrupts
383 MmioWrite32 (SP805_WDOG_INT_CLR_REG, 0); // write of any value clears the irq
384
385 // Prohibit any rogue access to SP805 registers
386 SP805Lock ();
387
388 if (PcdGet32 (PcdSP805WatchdogInterrupt) > 0) {
389 Status = mInterrupt->RegisterInterruptSource (mInterrupt,
390 PcdGet32 (PcdSP805WatchdogInterrupt),
391 SP805InterruptHandler);
392 if (EFI_ERROR (Status)) {
393 DEBUG ((DEBUG_ERROR, "%a: failed to register watchdog interrupt - %r\n",
394 __FUNCTION__, Status));
395 return Status;
396 }
397 } else {
398 DEBUG ((DEBUG_WARN, "%a: no interrupt specified, running in RESET mode only\n",
399 __FUNCTION__));
400 }
401
402 //
403 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
404 // This will avoid conflicts with the universal watchdog
405 //
406 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
407
408 // Register for an ExitBootServicesEvent
409 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,
410 ExitBootServicesEvent, NULL, &mEfiExitBootServicesEvent);
411 if (EFI_ERROR (Status)) {
412 Status = EFI_OUT_OF_RESOURCES;
413 goto EXIT;
414 }
415
416 // Install the Timer Architectural Protocol onto a new handle
417 Handle = NULL;
418 Status = gBS->InstallMultipleProtocolInterfaces (
419 &Handle,
420 &gEfiWatchdogTimerArchProtocolGuid, &mWatchdogTimer,
421 NULL
422 );
423 if (EFI_ERROR (Status)) {
424 Status = EFI_OUT_OF_RESOURCES;
425 goto EXIT;
426 }
427
428 EXIT:
429 ASSERT_EFI_ERROR (Status);
430 return Status;
431 }