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