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