1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/** @file
PCH RESET Runtime Driver
@copyright
Copyright (c) 1999 - 2012 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
This file contains an 'Intel Peripheral Driver' and uniquely
identified as "Intel Reference Module" and is
licensed for Intel CPUs and chipsets under the terms of your
license agreement with Intel or your vendor. This file may
be modified by the user, subject to additional terms of the
license agreement
**/
#include "PchReset.h"
PCH_RESET_INSTANCE *mPchResetInstance;
STATIC UINT8 mDaysOfMonthInfo[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/**
Check if it is leap year
@param[in] Year year to be check
@retval True year is leap year
@retval FALSE year is not a leap year
**/
BOOLEAN
IsLeapYear (
IN UINT16 Year
)
{
return (Year % 4 == 0) && ((Year % 100 != 0) || (Year % 400 == 0));
}
/**
Set System Wakeup Alarm.
@param[in] WakeAfter Time offset in seconds to wake from S3
@retval EFI_SUCCESS Timer started successfully
**/
STATIC
EFI_STATUS
SetSystemWakeupAlarm (
IN UINT32 WakeAfter
)
{
EFI_STATUS Status;
EFI_TIME Time;
EFI_TIME_CAPABILITIES Capabilities;
UINT32 Reminder;
UINT16 PmBase;
UINT8 DayOfMonth;
///
/// For an instant wake 2 seconds is a safe value
///
if (WakeAfter < 2) {
WakeAfter = 2;
}
Status = EfiGetTime (&Time, &Capabilities);
if (EFI_ERROR (Status)) {
return Status;
}
Reminder = WakeAfter + (UINT32) Time.Second;
Time.Second = Reminder % 60;
Reminder = Reminder / 60;
Reminder = Reminder + (UINT32) Time.Minute;
Time.Minute = Reminder % 60;
Reminder = Reminder / 60;
Reminder = Reminder + (UINT32) Time.Hour;
Time.Hour = Reminder % 24;
Reminder = Reminder / 24;
if (Reminder > 0) {
Reminder = Reminder + (UINT32) Time.Day;
if ((Time.Month == 2) && IsLeapYear (Time.Year)) {
DayOfMonth = 29;
} else {
DayOfMonth = mDaysOfMonthInfo[Time.Month - 1];
}
if (Reminder > DayOfMonth) {
Time.Day = (UINT8)Reminder - DayOfMonth;
Reminder = 1;
} else {
Time.Day = (UINT8)Reminder;
Reminder = 0;
}
}
if (Reminder > 0) {
if (Time.Month == 12) {
Time.Month = 1;
Time.Year = Time.Year + 1;
} else {
Time.Month = Time.Month + 1;
}
}
Status = EfiSetWakeupTime (TRUE, &Time);
if (EFI_ERROR (Status)) {
return Status;
}
PmBase = (UINT16) (PciRead32 (
PCI_LIB_ADDRESS (DEFAULT_PCI_BUS_NUMBER_PCH,
PCI_DEVICE_NUMBER_PCH_LPC,
PCI_FUNCTION_NUMBER_PCH_LPC,
R_PCH_LPC_ACPI_BASE)
) & B_PCH_LPC_ACPI_BASE_BAR);
///
/// Clear RTC PM1 status
///
IoWrite16 (PmBase + R_PCH_ACPI_PM1_STS, B_PCH_ACPI_PM1_STS_RTC);
///
/// set RTC_EN bit in PM1_EN to wake up from the alarm
///
IoWrite16 (
PmBase + R_PCH_ACPI_PM1_EN,
(IoRead16 (PmBase + R_PCH_ACPI_PM1_EN) | B_PCH_ACPI_PM1_EN_RTC)
);
return Status;
}
// AMI_OVERRIDE, [EIP111666] >>>
EFI_GUID gPchGetResetTypeGuid = PCH_RESET_PROTOCOL_GUID;
EFI_STATUS
EFIAPI
PchResetExitBootServicesEvent (
IN EFI_EVENT Event,
IN VOID *Context
)
/*++
Routine Description:
PCH initialization before ExitBootServices / LegacyBoot events
Useful for operations which must happen later than at EndOfPost event
Arguments:
Event A pointer to the Event that triggered the callback.
Context A pointer to private data registered with the callback function.
Returns:
EFI_SUCCESS The function completed successfully
--*/
{
//
// Closed the event to avoid call twice
//
UINT8 LegacyBoot;
gBS->CloseEvent (Event);
gRT->SetVariable (
L"InLegacyBoot",
&gPchGetResetTypeGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS |
EFI_VARIABLE_RUNTIME_ACCESS,
sizeof(UINT8),
&LegacyBoot
);
return EFI_SUCCESS;
}
// AMI_OVERRIDE, [EIP111666] <<<
/**
Initialize the state information for the Timer Architectural Protocol
@param[in] ImageHandle Image handle of the loaded driver
@param[in] SystemTable Pointer to the System Table
@retval EFI_SUCCESS Thread can be successfully created
@retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
@retval EFI_DEVICE_ERROR Cannot create the timer service
**/
EFI_STATUS
EFIAPI
InstallPchReset (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
UINT64 Length;
// AMI_OVERRIDE, NBDXE.c already done. >>>
/* UINT64 BaseAddress;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemorySpaceDescriptor;
UINT64 Attributes;*/
// AMI_OVERRIDE, NBDXE.c already done. <<<
// AMI_OVERRIDE, [EIP111666] >>>
EFI_EVENT LegacyBootEvent;
// AMI_OVERRIDE, [EIP111666] <<<
DEBUG ((EFI_D_INFO, "InstallPchReset() Start\n"));
Status = PciLibConstructor ();
ASSERT_EFI_ERROR (Status);
// AMI_OVERRIDE, NBDXE.c already done. >>>
/*
BaseAddress = MmPciAddress(0,
DEFAULT_PCI_BUS_NUMBER_PCH,
PCI_DEVICE_NUMBER_PCH_LPC,
PCI_FUNCTION_NUMBER_PCH_LPC,
0
);
Length = 4096;
Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &MemorySpaceDescriptor);
ASSERT_EFI_ERROR (Status);
Attributes = MemorySpaceDescriptor.Attributes | EFI_MEMORY_RUNTIME;
Status = gDS->SetMemorySpaceAttributes (
BaseAddress,
Length,
Attributes
);
DEBUG ((EFI_D_INFO, "Status = %r\n",Status));
ASSERT_EFI_ERROR (Status);*/
// AMI_OVERRIDE, NBDXE.c already done. <<<
Length = 4096;
// AMI_OVERRIDE_FOR_FIRST_BOOT
Status = PciLibRegisterMemory (
PCI_LIB_ADDRESS (0,
0,
0,
0),
(UINTN) Length
);
// AMI_OVERRIDE_FOR_FIRST_BOOT
Status = PciLibRegisterMemory (
PCI_LIB_ADDRESS (DEFAULT_PCI_BUS_NUMBER_PCH,
PCI_DEVICE_NUMBER_PCH_LPC,
PCI_FUNCTION_NUMBER_PCH_LPC,
0),
(UINTN) Length
);
ASSERT_EFI_ERROR (Status);
///
/// Allocate Runtime memory for the PchReset protocol instance.
///
mPchResetInstance = AllocateRuntimeZeroPool (sizeof (PCH_RESET_INSTANCE));
if (mPchResetInstance == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Status = PchResetProtocolConstructor (mPchResetInstance);
if (EFI_ERROR (Status)) {
return Status;
}
///
/// Install protocol interface
///
Status = gBS->InstallMultipleProtocolInterfaces (
&mPchResetInstance->Handle,
&gPchResetProtocolGuid,
&mPchResetInstance->PchResetProtocol,
NULL
);
ASSERT_EFI_ERROR (Status);
// AMI_OVERRIDE, [EIP111666] >>>
Status = EfiCreateEventLegacyBootEx (
EFI_TPL_CALLBACK,
PchResetExitBootServicesEvent,
NULL,
&LegacyBootEvent
);
// AMI_OVERRIDE, [EIP111666] <<<
///
/// The Lib Deconstruct will automatically be called when entrypoint return error.
///
DEBUG ((EFI_D_INFO, "InstallPchReset() End\n"));
return Status;
}
#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
/**
If need be, do any special reset required for capsules. For this
implementation where we're called from the ResetSystem() api,
just set our capsule variable and return to let the caller
do a soft reset.
@param[in] None
@retval None
**/
VOID
CapsuleReset (
VOID
)
{
EFI_STATUS Status;
UINTN Size;
UINTN CapsuleDataPtr;
UINT32 Data32;
UINT32 Eflags;
UINT16 PmBase;
///
/// Check if there are pending capsules to process
///
Size = sizeof (CapsuleDataPtr);
Status = EfiGetVariable (
EFI_CAPSULE_VARIABLE_NAME,
&gEfiCapsuleVendorGuid,
NULL,
&Size,
(VOID *) &CapsuleDataPtr
);
if (Status == EFI_SUCCESS) {
///
/// Wake up system 2 seconds after putting system into S3 to complete the reset operation.
///
SetSystemWakeupAlarm (2);
///
/// Process capsules across a system reset.
///
PmBase = PciRead16 (
PCI_LIB_ADDRESS (DEFAULT_PCI_BUS_NUMBER_PCH,
PCI_DEVICE_NUMBER_PCH_LPC,
PCI_FUNCTION_NUMBER_PCH_LPC,
R_PCH_LPC_ACPI_BASE)
) & B_PCH_LPC_ACPI_BASE_BAR;
ASSERT (PmBase != 0);
Data32 = IoRead32 ((UINTN) (PmBase + R_PCH_ACPI_PM1_CNT));
Data32 = (UINT32) ((Data32 & ~(B_PCH_ACPI_PM1_CNT_SLP_TYP + B_PCH_ACPI_PM1_CNT_SLP_EN)) | V_PCH_ACPI_PM1_CNT_S3);
Eflags = (UINT32) AsmReadEflags ();
if ((Eflags & 0x200)) {
DisableInterrupts ();
}
AsmWbinvd ();
AsmWriteCr0 (AsmReadCr0 () | 0x060000000);
IoWrite32 (
(UINTN) (PmBase + R_PCH_ACPI_PM1_CNT),
(UINT32) Data32
);
Data32 = Data32 | B_PCH_ACPI_PM1_CNT_SLP_EN;
IoWrite32 (
(UINTN) (PmBase + R_PCH_ACPI_PM1_CNT),
(UINT32) Data32
);
if ((Eflags & 0x200)) {
EnableInterrupts ();
}
///
/// Should not return
///
EFI_DEADLOOP ();
}
}
#endif
/**
Execute call back function for Pch Reset.
@param[in] PchResetType Pch Reset Types which includes PowerCycle, Globalreset.
@retval EFI_SUCCESS The callback function has been done successfully
@retval EFI_NOT_FOUND Failed to find Pch Reset Callback protocol. Or, none of
callback protocol is installed.
@retval Others Do not do any reset from PCH
**/
EFI_STATUS
EFIAPI
PchResetCallback (
IN PCH_RESET_TYPE PchResetType
)
{
EFI_STATUS Status;
UINTN NumHandles;
EFI_HANDLE *HandleBuffer;
UINTN Index;
PCH_RESET_CALLBACK_PROTOCOL *PchResetCallback;
// AMI_OVERRIDE, [EIP111666] >>>
UINTN VariableSize = 1;
UINT8 TempBuffer;
BOOLEAN LegacyBoot = FALSE;
// AMI_OVERRIDE, [EIP111666] <<<
// AMI_OVERRIDE, [EIP111666] >>>
Status = gRT->GetVariable (
L"InLegacyBoot",
&gPchGetResetTypeGuid,
NULL,
&VariableSize,
&TempBuffer
);
// If variable found, we are in runtime.
if(!EFI_ERROR(Status))
LegacyBoot = TRUE;
if (!(EfiAtRuntime () || LegacyBoot)) {
// AMI_OVERRIDE, [EIP111666] <<<
///
/// Retrieve all instances of Pch Reset Callback protocol
///
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gPchResetCallbackProtocolGuid,
NULL,
&NumHandles,
&HandleBuffer
);
if (EFI_ERROR (Status)) {
///
/// Those drivers that need to install Pch Reset Callback protocol have the responsibility
/// to make sure themselves execute before Pch Reset Runtime driver.
///
if (Status == EFI_NOT_FOUND) {
DEBUG ((EFI_D_ERROR | EFI_D_INFO, "Or, none of Pch Reset callback protocol is installed.\n"));
}
return Status;
}
for (Index = 0; Index < NumHandles; Index++) {
Status = gBS->HandleProtocol (
HandleBuffer[Index],
&gPchResetCallbackProtocolGuid,
(VOID **) &PchResetCallback
);
ASSERT_EFI_ERROR (Status);
if (!EFI_ERROR (Status)) {
PchResetCallback->ResetCallback (PchResetType);
} else {
DEBUG ((EFI_D_ERROR | EFI_D_INFO, "Failed to locate Pch Reset Callback protocol.\n"));
return Status;
}
}
}
#if (EFI_SPECIFICATION_VERSION >= 0x00020000)
if (PchResetType == WarmReset) {
CapsuleReset ();
}
#endif
return EFI_SUCCESS;
}
/**
Fixup internal data pointers so that the services can be called in virtual mode.
@param[in] Event The event registered.
@param[in] Context Event context. Not used in this event handler.
@retval None
**/
EFI_RUNTIMESERVICE
VOID
PchResetVirtualAddressChangeEvent (
IN EFI_EVENT Event,
IN VOID *Context
)
{
gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID *) &(mPchResetInstance->PchResetProtocol.Reset));
gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID *) &(mPchResetInstance->PchRootComplexBar));
gRT->ConvertPointer (EFI_INTERNAL_POINTER, (VOID *) &(mPchResetInstance));
}
|