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