]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/MemoryTest/NullMemoryTestDxe/NullMemoryTest.c
a9bd1015013104d99e63d9f609452f6fe67e4bba
[mirror_edk2.git] / MdeModulePkg / Universal / MemoryTest / NullMemoryTestDxe / NullMemoryTest.c
1 /** @file
2 Implementation of Generic Memory Test Protocol which does not perform real memory test.
3
4 Copyright (c) 2006 - 2018, 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
16 #include "NullMemoryTest.h"
17
18 UINT64 mTestedSystemMemory = 0;
19 UINT64 mTotalSystemMemory = 0;
20 EFI_HANDLE mGenericMemoryTestHandle;
21
22 EFI_GENERIC_MEMORY_TEST_PROTOCOL mGenericMemoryTest = {
23 InitializeMemoryTest,
24 GenPerformMemoryTest,
25 GenMemoryTestFinished,
26 GenCompatibleRangeTest
27 };
28
29 /**
30 Entry point of the NULL memory test driver.
31
32 This function is the entry point of the NULL memory test driver.
33 It simply installs the Generic Memory Test Protocol.
34
35 @param ImageHandle The firmware allocated handle for the EFI image.
36 @param SystemTable A pointer to the EFI System Table.
37
38 @retval EFI_SUCCESS Generic Memory Test Protocol is successfully installed.
39
40 **/
41 EFI_STATUS
42 EFIAPI
43 GenericMemoryTestEntryPoint (
44 IN EFI_HANDLE ImageHandle,
45 IN EFI_SYSTEM_TABLE *SystemTable
46 )
47 {
48 EFI_STATUS Status;
49
50 Status = gBS->InstallProtocolInterface (
51 &mGenericMemoryTestHandle,
52 &gEfiGenericMemTestProtocolGuid,
53 EFI_NATIVE_INTERFACE,
54 &mGenericMemoryTest
55 );
56 ASSERT_EFI_ERROR (Status);
57
58 return EFI_SUCCESS;
59 }
60
61 /**
62 Convert the memory range to tested.
63
64 @param BaseAddress Base address of the memory range.
65 @param Length Length of the memory range.
66 @param Capabilities Capabilities of the memory range.
67
68 @retval EFI_SUCCESS The memory range is converted to tested.
69 @retval others Error happens.
70 **/
71 EFI_STATUS
72 ConvertToTestedMemory (
73 IN UINT64 BaseAddress,
74 IN UINT64 Length,
75 IN UINT64 Capabilities
76 )
77 {
78 EFI_STATUS Status;
79 Status = gDS->RemoveMemorySpace (
80 BaseAddress,
81 Length
82 );
83 if (!EFI_ERROR (Status)) {
84 Status = gDS->AddMemorySpace (
85 ((Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) ?
86 EfiGcdMemoryTypeMoreReliable : EfiGcdMemoryTypeSystemMemory,
87 BaseAddress,
88 Length,
89 Capabilities &~
90 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)
91 );
92 }
93 return Status;
94 }
95
96 /**
97 Initialize the generic memory test.
98
99 This function implements EFI_GENERIC_MEMORY_TEST_PROTOCOL.MemoryTestInit.
100 It simply promotes untested reserved memory to system memory without real test.
101
102 @param This Protocol instance pointer.
103 @param Level The coverage level of the memory test.
104 @param RequireSoftECCInit Indicate if the memory need software ECC init.
105
106 @retval EFI_SUCCESS The generic memory test initialized correctly.
107 @retval EFI_NO_MEDIA There is not any non-tested memory found, in this
108 function if not any non-tesed memory found means
109 that the memory test driver have not detect any
110 non-tested extended memory of current system.
111
112 **/
113 EFI_STATUS
114 EFIAPI
115 InitializeMemoryTest (
116 IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This,
117 IN EXTENDMEM_COVERAGE_LEVEL Level,
118 OUT BOOLEAN *RequireSoftECCInit
119 )
120 {
121 EFI_STATUS Status;
122 UINTN NumberOfDescriptors;
123 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;
124 UINTN Index;
125
126 gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);
127 for (Index = 0; Index < NumberOfDescriptors; Index++) {
128 if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&
129 (MemorySpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==
130 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)
131 ) {
132 //
133 // For those reserved memory that have not been tested, simply promote to system memory.
134 //
135 Status = ConvertToTestedMemory (
136 MemorySpaceMap[Index].BaseAddress,
137 MemorySpaceMap[Index].Length,
138 MemorySpaceMap[Index].Capabilities
139 );
140 ASSERT_EFI_ERROR (Status);
141 mTestedSystemMemory += MemorySpaceMap[Index].Length;
142 mTotalSystemMemory += MemorySpaceMap[Index].Length;
143 } else if ((MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) ||
144 (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeMoreReliable)) {
145 mTotalSystemMemory += MemorySpaceMap[Index].Length;
146 }
147 }
148
149 FreePool (MemorySpaceMap);
150
151 *RequireSoftECCInit = FALSE;
152 return EFI_SUCCESS;
153 }
154
155 /**
156 Perform the memory test.
157
158 This function implements EFI_GENERIC_MEMORY_TEST_PROTOCOL.PerformMemoryTest.
159 It simply returns EFI_NOT_FOUND.
160
161 @param This Protocol instance pointer.
162 @param TestedMemorySize Return the tested extended memory size.
163 @param TotalMemorySize Return the whole system physical memory size, this
164 value may be changed if in some case some error
165 DIMMs be disabled.
166 @param ErrorOut Any time the memory error occurs, this will be
167 TRUE.
168 @param IfTestAbort Indicate if the user press "ESC" to skip the memory
169 test.
170
171 @retval EFI_SUCCESS One block of memory test ok, the block size is hide
172 internally.
173 @retval EFI_NOT_FOUND Indicate all the non-tested memory blocks have
174 already go through.
175 @retval EFI_DEVICE_ERROR Mis-compare error, and no agent can handle it
176
177 **/
178 EFI_STATUS
179 EFIAPI
180 GenPerformMemoryTest (
181 IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This,
182 IN OUT UINT64 *TestedMemorySize,
183 OUT UINT64 *TotalMemorySize,
184 OUT BOOLEAN *ErrorOut,
185 IN BOOLEAN TestAbort
186 )
187 {
188 *ErrorOut = FALSE;
189 *TestedMemorySize = mTestedSystemMemory;
190 *TotalMemorySize = mTotalSystemMemory;
191
192 return EFI_NOT_FOUND;
193
194 }
195
196 /**
197 The memory test finished.
198
199 This function implements EFI_GENERIC_MEMORY_TEST_PROTOCOL.Finished.
200 It simply returns EFI_SUCCESS.
201
202 @param This Protocol instance pointer.
203
204 @retval EFI_SUCCESS Successful free all the generic memory test driver
205 allocated resource and notify to platform memory
206 test driver that memory test finished.
207
208 **/
209 EFI_STATUS
210 EFIAPI
211 GenMemoryTestFinished (
212 IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This
213 )
214 {
215 return EFI_SUCCESS;
216 }
217
218 /**
219 Provide capability to test compatible range which used by some special
220 driver required using memory range before BDS perform memory test.
221
222 This function implements EFI_GENERIC_MEMORY_TEST_PROTOCOL.CompatibleRangeTest.
223 It simply sets the memory range to system memory.
224
225 @param This Protocol instance pointer.
226 @param StartAddress The start address of the memory range.
227 @param Length The memory range's length.
228
229 @retval EFI_SUCCESS The compatible memory range pass the memory test.
230 @retval EFI_INVALID_PARAMETER The compatible memory range must be below 16M.
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 GenCompatibleRangeTest (
236 IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This,
237 IN EFI_PHYSICAL_ADDRESS StartAddress,
238 IN UINT64 Length
239 )
240 {
241 EFI_STATUS Status;
242 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;
243 EFI_PHYSICAL_ADDRESS CurrentBase;
244 UINT64 CurrentLength;
245
246 //
247 // Check if the parameter is below 16MB
248 //
249 if (StartAddress + Length > SIZE_16MB) {
250 return EFI_INVALID_PARAMETER;
251 }
252 CurrentBase = StartAddress;
253 do {
254 //
255 // Check the required memory range status; if the required memory range span
256 // the different GCD memory descriptor, it may be cause different action.
257 //
258 Status = gDS->GetMemorySpaceDescriptor (
259 CurrentBase,
260 &Descriptor
261 );
262 if (EFI_ERROR (Status)) {
263 return Status;
264 }
265
266 if (Descriptor.GcdMemoryType == EfiGcdMemoryTypeReserved &&
267 (Descriptor.Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==
268 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)
269 ) {
270 CurrentLength = Descriptor.BaseAddress + Descriptor.Length - CurrentBase;
271 if (CurrentBase + CurrentLength > StartAddress + Length) {
272 CurrentLength = StartAddress + Length - CurrentBase;
273 }
274 Status = ConvertToTestedMemory (
275 CurrentBase,
276 CurrentLength,
277 Descriptor.Capabilities
278 );
279 if (EFI_ERROR (Status)) {
280 return Status;
281 }
282 }
283 CurrentBase = Descriptor.BaseAddress + Descriptor.Length;
284 } while (CurrentBase < StartAddress + Length);
285 //
286 // Here means the required range already be tested, so just return success.
287 //
288 return EFI_SUCCESS;
289 }
290