]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/Acpi/AcpiS3SaveDxe/AcpiVariableThunkPlatform.c
IntelFrameworkModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / Acpi / AcpiS3SaveDxe / AcpiVariableThunkPlatform.c
1 /** @file
2 This is an implementation of the AcpiVariable platform field for ECP platform.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 ==
9
10 typedef struct {
11 EFI_PHYSICAL_ADDRESS AcpiReservedMemoryBase; <<===
12 UINT32 AcpiReservedMemorySize; <<===
13 EFI_PHYSICAL_ADDRESS S3ReservedLowMemoryBase;
14 EFI_PHYSICAL_ADDRESS AcpiBootScriptTable;
15 EFI_PHYSICAL_ADDRESS RuntimeScriptTableBase;
16 EFI_PHYSICAL_ADDRESS AcpiFacsTable;
17 UINT64 SystemMemoryLength; <<===
18 ACPI_CPU_DATA_COMPATIBILITY AcpiCpuData;
19 EFI_PHYSICAL_ADDRESS VideoOpromAddress;
20 UINT32 VideoOpromSize;
21 EFI_PHYSICAL_ADDRESS S3DebugBufferAddress;
22 EFI_PHYSICAL_ADDRESS S3ResumeNvsEntryPoint;
23 } ACPI_VARIABLE_SET_COMPATIBILITY;
24
25 **/
26
27 #include <FrameworkDxe.h>
28 #include <Library/BaseLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Library/HobLib.h>
33 #include <Library/PcdLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/UefiLib.h>
36 #include <Protocol/FrameworkMpService.h>
37 #include <Protocol/VariableLock.h>
38 #include <Guid/AcpiVariableCompatibility.h>
39
40 GLOBAL_REMOVE_IF_UNREFERENCED
41 ACPI_VARIABLE_SET_COMPATIBILITY *mAcpiVariableSetCompatibility = NULL;
42
43 /**
44 Allocate memory below 4G memory address.
45
46 This function allocates memory below 4G memory address.
47
48 @param MemoryType Memory type of memory to allocate.
49 @param Size Size of memory to allocate.
50
51 @return Allocated address for output.
52
53 **/
54 VOID*
55 AllocateMemoryBelow4G (
56 IN EFI_MEMORY_TYPE MemoryType,
57 IN UINTN Size
58 );
59
60 /**
61 Hook point for AcpiVariableThunkPlatform for S3Ready.
62
63 **/
64 VOID
65 S3ReadyThunkPlatform (
66 VOID
67 )
68 {
69 EFI_PHYSICAL_ADDRESS AcpiMemoryBase;
70 UINT32 AcpiMemorySize;
71 EFI_PEI_HOB_POINTERS Hob;
72 UINT64 MemoryLength;
73
74 DEBUG ((EFI_D_INFO, "S3ReadyThunkPlatform\n"));
75
76 if (mAcpiVariableSetCompatibility == NULL) {
77 return;
78 }
79
80 //
81 // Allocate ACPI reserved memory under 4G
82 //
83 AcpiMemoryBase = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateMemoryBelow4G (EfiReservedMemoryType, PcdGet32 (PcdS3AcpiReservedMemorySize));
84 ASSERT (AcpiMemoryBase != 0);
85 AcpiMemorySize = PcdGet32 (PcdS3AcpiReservedMemorySize);
86
87 //
88 // Calculate the system memory length by memory hobs
89 //
90 MemoryLength = 0x100000;
91 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
92 ASSERT (Hob.Raw != NULL);
93 while ((Hob.Raw != NULL) && (!END_OF_HOB_LIST (Hob))) {
94 if (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
95 //
96 // Skip the memory region below 1MB
97 //
98 if (Hob.ResourceDescriptor->PhysicalStart >= 0x100000) {
99 MemoryLength += Hob.ResourceDescriptor->ResourceLength;
100 }
101 }
102 Hob.Raw = GET_NEXT_HOB (Hob);
103 Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
104 }
105
106 mAcpiVariableSetCompatibility->AcpiReservedMemoryBase = AcpiMemoryBase;
107 mAcpiVariableSetCompatibility->AcpiReservedMemorySize = AcpiMemorySize;
108 mAcpiVariableSetCompatibility->SystemMemoryLength = MemoryLength;
109
110 DEBUG((EFI_D_INFO, "AcpiVariableThunkPlatform: AcpiMemoryBase is 0x%8x\n", mAcpiVariableSetCompatibility->AcpiReservedMemoryBase));
111 DEBUG((EFI_D_INFO, "AcpiVariableThunkPlatform: AcpiMemorySize is 0x%8x\n", mAcpiVariableSetCompatibility->AcpiReservedMemorySize));
112 DEBUG((EFI_D_INFO, "AcpiVariableThunkPlatform: SystemMemoryLength is 0x%8x\n", mAcpiVariableSetCompatibility->SystemMemoryLength));
113
114 return ;
115 }
116
117 /**
118 Register callback function upon VariableLockProtocol
119 to lock ACPI_GLOBAL_VARIABLE variable to avoid malicious code to update it.
120
121 @param[in] Event Event whose notification function is being invoked.
122 @param[in] Context Pointer to the notification function's context.
123 **/
124 VOID
125 EFIAPI
126 VariableLockAcpiGlobalVariable (
127 IN EFI_EVENT Event,
128 IN VOID *Context
129 )
130 {
131 EFI_STATUS Status;
132 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
133 //
134 // Mark ACPI_GLOBAL_VARIABLE variable to read-only if the Variable Lock protocol exists
135 //
136 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **) &VariableLock);
137 if (!EFI_ERROR (Status)) {
138 Status = VariableLock->RequestToLock (VariableLock, ACPI_GLOBAL_VARIABLE, &gEfiAcpiVariableCompatiblityGuid);
139 ASSERT_EFI_ERROR (Status);
140 }
141 }
142
143 /**
144 Hook point for AcpiVariableThunkPlatform for InstallAcpiS3Save.
145 **/
146 VOID
147 InstallAcpiS3SaveThunk (
148 VOID
149 )
150 {
151 EFI_STATUS Status;
152 FRAMEWORK_EFI_MP_SERVICES_PROTOCOL *FrameworkMpService;
153 UINTN VarSize;
154 VOID *Registration;
155
156 Status = gBS->LocateProtocol (
157 &gFrameworkEfiMpServiceProtocolGuid,
158 NULL,
159 (VOID**) &FrameworkMpService
160 );
161 if (!EFI_ERROR (Status)) {
162 //
163 // On ECP platform, if framework CPU drivers are in use, The compatible version of ACPI variable set
164 // should be produced by CPU driver.
165 //
166 VarSize = sizeof (mAcpiVariableSetCompatibility);
167 Status = gRT->GetVariable (
168 ACPI_GLOBAL_VARIABLE,
169 &gEfiAcpiVariableCompatiblityGuid,
170 NULL,
171 &VarSize,
172 &mAcpiVariableSetCompatibility
173 );
174 if (EFI_ERROR (Status) || (VarSize != sizeof (mAcpiVariableSetCompatibility))) {
175 DEBUG ((EFI_D_ERROR, "FATAL ERROR: AcpiVariableSetCompatibility was not saved by CPU driver correctly. OS S3 may fail!\n"));
176 mAcpiVariableSetCompatibility = NULL;
177 }
178 } else {
179 //
180 // Allocate/initialize the compatible version of Acpi Variable Set since Framework chipset/platform
181 // driver need this variable. ACPI_GLOBAL_VARIABLE variable is not used in runtime phase,
182 // so RT attribute is not needed for it.
183 //
184 mAcpiVariableSetCompatibility = AllocateMemoryBelow4G (EfiACPIMemoryNVS, sizeof(ACPI_VARIABLE_SET_COMPATIBILITY));
185 Status = gRT->SetVariable (
186 ACPI_GLOBAL_VARIABLE,
187 &gEfiAcpiVariableCompatiblityGuid,
188 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_NON_VOLATILE,
189 sizeof(mAcpiVariableSetCompatibility),
190 &mAcpiVariableSetCompatibility
191 );
192 if (!EFI_ERROR (Status)) {
193 //
194 // Register callback function upon VariableLockProtocol
195 // to lock ACPI_GLOBAL_VARIABLE variable to avoid malicious code to update it.
196 //
197 EfiCreateProtocolNotifyEvent (
198 &gEdkiiVariableLockProtocolGuid,
199 TPL_CALLBACK,
200 VariableLockAcpiGlobalVariable,
201 NULL,
202 &Registration
203 );
204 } else {
205 DEBUG ((EFI_D_ERROR, "FATAL ERROR: AcpiVariableSetCompatibility cannot be saved: %r. OS S3 may fail!\n", Status));
206 gBS->FreePages (
207 (EFI_PHYSICAL_ADDRESS) (UINTN) mAcpiVariableSetCompatibility,
208 EFI_SIZE_TO_PAGES (sizeof (ACPI_VARIABLE_SET_COMPATIBILITY))
209 );
210 mAcpiVariableSetCompatibility = NULL;
211 }
212 }
213
214 DEBUG((EFI_D_INFO, "AcpiVariableSetCompatibility is 0x%8x\n", mAcpiVariableSetCompatibility));
215 }