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