]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Universal / WatchdogTimerDxe / WatchdogTimer.c
1 /** @file
2 Implementation of Watchdog Timer Architectural Protocol using UEFI APIs.
3
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 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 "WatchdogTimer.h"
16
17 //
18 // Handle for the Watchdog Timer Architectural Protocol instance produced by this driver
19 //
20 EFI_HANDLE mWatchdogTimerHandle = NULL;
21
22 //
23 // The Watchdog Timer Architectural Protocol instance produced by this driver
24 //
25 EFI_WATCHDOG_TIMER_ARCH_PROTOCOL mWatchdogTimer = {
26 WatchdogTimerDriverRegisterHandler,
27 WatchdogTimerDriverSetTimerPeriod,
28 WatchdogTimerDriverGetTimerPeriod
29 };
30
31 //
32 // The watchdog timer period in 100 ns units
33 //
34 UINT64 mWatchdogTimerPeriod = 0;
35
36 //
37 // The notification function to call if the watchdig timer fires
38 //
39 EFI_WATCHDOG_TIMER_NOTIFY mWatchdogTimerNotifyFunction = NULL;
40
41 //
42 // The one-shot timer event that is armed when the watchdog timer is enabled
43 //
44 EFI_EVENT mWatchdogTimerEvent;
45
46
47 /**
48 Notification function that is called if the watchdog timer is fired.
49
50 Notification function for the one-shot timer event that was signaled
51 when the watchdog timer expired. If a handler has been registered with
52 the Watchdog Timer Architectural Protocol, then that handler is called
53 passing in the time period that has passed that cause the watchdog timer
54 to fire. Then, a call to the Runtime Service ResetSystem() is made to
55 reset the platform.
56
57 @param Timer The one-shot timer event that was signaled when the
58 watchdog timer expired.
59 @param Context The context that was registered when the event Timer was created.
60
61 **/
62 VOID
63 EFIAPI
64 WatchdogTimerDriverExpires (
65 IN EFI_EVENT Timer,
66 IN VOID *Context
67 )
68 {
69 REPORT_STATUS_CODE (EFI_ERROR_CODE | EFI_ERROR_MINOR, (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_TIMER_EXPIRED));
70
71 //
72 // If a notification function has been registered, then call it
73 //
74 if (mWatchdogTimerNotifyFunction != NULL) {
75 mWatchdogTimerNotifyFunction (mWatchdogTimerPeriod);
76 }
77 //
78 // Reset the platform
79 //
80 gRT->ResetSystem (EfiResetCold, EFI_TIMEOUT, 0, NULL);
81 }
82
83
84 /**
85 Registers a handler that is to be invoked when the watchdog timer fires.
86
87 This function registers a handler that is to be invoked when the watchdog
88 timer fires. By default, the EFI_WATCHDOG_TIMER protocol will call the
89 Runtime Service ResetSystem() when the watchdog timer fires. If a
90 NotifyFunction is registered, then the NotifyFunction will be called before
91 the Runtime Service ResetSystem() is called. If NotifyFunction is NULL, then
92 the watchdog handler is unregistered. If a watchdog handler is registered,
93 then EFI_SUCCESS is returned. If an attempt is made to register a handler
94 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
95 If an attempt is made to uninstall a handler when a handler is not installed,
96 then return EFI_INVALID_PARAMETER.
97
98 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
99 @param NotifyFunction The function to call when the watchdog timer fires. If this
100 is NULL, then the handler will be unregistered.
101
102 @retval EFI_SUCCESS The watchdog timer handler was registered or unregistered.
103 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already registered.
104 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not previously registered.
105
106 **/
107 EFI_STATUS
108 EFIAPI
109 WatchdogTimerDriverRegisterHandler (
110 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
111 IN EFI_WATCHDOG_TIMER_NOTIFY NotifyFunction
112 )
113 {
114 //
115 // If NotifyFunction is NULL, and a handler was not previously registered,
116 // return EFI_INVALID_PARAMETER.
117 //
118 if (NotifyFunction == NULL && mWatchdogTimerNotifyFunction == NULL) {
119 return EFI_INVALID_PARAMETER;
120 }
121 //
122 // If NotifyFunction is not NULL, and a handler is already registered,
123 // return EFI_ALREADY_STARTED.
124 //
125 if (NotifyFunction != NULL && mWatchdogTimerNotifyFunction != NULL) {
126 return EFI_ALREADY_STARTED;
127 }
128
129 mWatchdogTimerNotifyFunction = NotifyFunction;
130
131 return EFI_SUCCESS;
132 }
133
134 /**
135 Sets the amount of time in the future to fire the watchdog timer.
136
137 This function sets the amount of time to wait before firing the watchdog
138 timer to TimerPeriod 100 ns units. If TimerPeriod is 0, then the watchdog
139 timer is disabled.
140
141 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
142 @param TimerPeriod The amount of time in 100 ns units to wait before the watchdog
143 timer is fired. If TimerPeriod is zero, then the watchdog
144 timer is disabled.
145
146 @retval EFI_SUCCESS The watchdog timer has been programmed to fire in Time
147 100 ns units.
148 @retval EFI_DEVICE_ERROR A watchdog timer could not be programmed due to a device
149 error.
150
151 **/
152 EFI_STATUS
153 EFIAPI
154 WatchdogTimerDriverSetTimerPeriod (
155 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
156 IN UINT64 TimerPeriod
157 )
158 {
159 mWatchdogTimerPeriod = TimerPeriod;
160
161 return gBS->SetTimer (
162 mWatchdogTimerEvent,
163 (mWatchdogTimerPeriod == 0) ? TimerCancel : TimerRelative,
164 mWatchdogTimerPeriod
165 );
166 }
167
168 /**
169 Retrieves the amount of time in 100 ns units that the system will wait before firing the watchdog timer.
170
171 This function retrieves the amount of time the system will wait before firing
172 the watchdog timer. This period is returned in TimerPeriod, and EFI_SUCCESS
173 is returned. If TimerPeriod is NULL, then EFI_INVALID_PARAMETER is returned.
174
175 @param This The EFI_WATCHDOG_TIMER_ARCH_PROTOCOL instance.
176 @param TimerPeriod A pointer to the amount of time in 100 ns units that the system
177 will wait before the watchdog timer is fired. If TimerPeriod of
178 zero is returned, then the watchdog timer is disabled.
179
180 @retval EFI_SUCCESS The amount of time that the system will wait before
181 firing the watchdog timer was returned in TimerPeriod.
182 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
183
184 **/
185 EFI_STATUS
186 EFIAPI
187 WatchdogTimerDriverGetTimerPeriod (
188 IN EFI_WATCHDOG_TIMER_ARCH_PROTOCOL *This,
189 IN UINT64 *TimerPeriod
190 )
191 {
192 if (TimerPeriod == NULL) {
193 return EFI_INVALID_PARAMETER;
194 }
195
196 *TimerPeriod = mWatchdogTimerPeriod;
197
198 return EFI_SUCCESS;
199 }
200
201 /**
202 Entry point of the Watchdog Timer Architectural Protocol driver.
203
204 @param ImageHandle The image handle of this driver.
205 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
206
207 @retval EFI_SUCCESS Watchdog Timer Architectural Protocol successfully installed.
208
209 **/
210 EFI_STATUS
211 EFIAPI
212 WatchdogTimerDriverInitialize (
213 IN EFI_HANDLE ImageHandle,
214 IN EFI_SYSTEM_TABLE *SystemTable
215 )
216 {
217 EFI_STATUS Status;
218
219 //
220 // Make sure the Watchdog Timer Architectural Protocol has not been installed in the system yet.
221 //
222 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiWatchdogTimerArchProtocolGuid);
223
224 //
225 // Create the timer event to implement a simple watchdog timer
226 //
227 Status = gBS->CreateEvent (
228 EVT_TIMER | EVT_NOTIFY_SIGNAL,
229 TPL_NOTIFY,
230 WatchdogTimerDriverExpires,
231 NULL,
232 &mWatchdogTimerEvent
233 );
234 ASSERT_EFI_ERROR (Status);
235
236 //
237 // Install the Watchdog Timer Arch Protocol onto a new handle
238 //
239 Status = gBS->InstallMultipleProtocolInterfaces (
240 &mWatchdogTimerHandle,
241 &gEfiWatchdogTimerArchProtocolGuid,
242 &mWatchdogTimer,
243 NULL
244 );
245 ASSERT_EFI_ERROR (Status);
246
247 return EFI_SUCCESS;
248 }