]>
Commit | Line | Data |
---|---|---|
7c636bd0 | 1 | /** @file\r |
2 | \r | |
2e5fb984 | 3 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r |
7c636bd0 | 4 | \r |
9d510e61 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
7c636bd0 | 6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include "LightMemoryTest.h"\r | |
10 | \r | |
11 | //\r | |
12 | // Global:\r | |
13 | // Since this driver will only ever produce one instance of the memory test\r | |
14 | // protocol, so we do not need to dynamically allocate the PrivateData.\r | |
15 | //\r | |
16 | EFI_PHYSICAL_ADDRESS mCurrentAddress;\r | |
17 | LIST_ENTRY *mCurrentLink;\r | |
18 | NONTESTED_MEMORY_RANGE *mCurrentRange;\r | |
19 | UINT64 mTestedSystemMemory;\r | |
20 | UINT64 mNonTestedSystemMemory;\r | |
21 | \r | |
22 | UINT32 GenericMemoryTestMonoPattern[GENERIC_CACHELINE_SIZE / 4] = {\r | |
23 | 0x5a5a5a5a,\r | |
24 | 0xa5a5a5a5,\r | |
25 | 0x5a5a5a5a,\r | |
26 | 0xa5a5a5a5,\r | |
27 | 0x5a5a5a5a,\r | |
28 | 0xa5a5a5a5,\r | |
29 | 0x5a5a5a5a,\r | |
30 | 0xa5a5a5a5,\r | |
31 | 0x5a5a5a5a,\r | |
32 | 0xa5a5a5a5,\r | |
33 | 0x5a5a5a5a,\r | |
34 | 0xa5a5a5a5,\r | |
35 | 0x5a5a5a5a,\r | |
36 | 0xa5a5a5a5,\r | |
37 | 0x5a5a5a5a,\r | |
38 | 0xa5a5a5a5\r | |
39 | };\r | |
40 | \r | |
41 | /**\r | |
42 | Compares the contents of two buffers.\r | |
43 | \r | |
44 | This function compares Length bytes of SourceBuffer to Length bytes of DestinationBuffer.\r | |
45 | If all Length bytes of the two buffers are identical, then 0 is returned. Otherwise, the\r | |
46 | value returned is the first mismatched byte in SourceBuffer subtracted from the first\r | |
47 | mismatched byte in DestinationBuffer.\r | |
d1102dba | 48 | \r |
7c636bd0 | 49 | If Length = 0, then ASSERT().\r |
50 | \r | |
51 | @param[in] DestinationBuffer The pointer to the destination buffer to compare.\r | |
52 | @param[in] SourceBuffer The pointer to the source buffer to compare.\r | |
53 | @param[in] Length The number of bytes to compare.\r | |
54 | \r | |
55 | @return 0 All Length bytes of the two buffers are identical.\r | |
56 | @retval Non-zero The first mismatched byte in SourceBuffer subtracted from the first\r | |
57 | mismatched byte in DestinationBuffer.\r | |
d1102dba | 58 | \r |
7c636bd0 | 59 | **/\r |
60 | INTN\r | |
61 | EFIAPI\r | |
62 | CompareMemWithoutCheckArgument (\r | |
63 | IN CONST VOID *DestinationBuffer,\r | |
64 | IN CONST VOID *SourceBuffer,\r | |
65 | IN UINTN Length\r | |
66 | )\r | |
67 | {\r | |
68 | ASSERT (Length > 0);\r | |
69 | while ((--Length != 0) &&\r | |
70 | (*(INT8*)DestinationBuffer == *(INT8*)SourceBuffer)) {\r | |
71 | DestinationBuffer = (INT8*)DestinationBuffer + 1;\r | |
72 | SourceBuffer = (INT8*)SourceBuffer + 1;\r | |
73 | }\r | |
74 | return (INTN)*(UINT8*)DestinationBuffer - (INTN)*(UINT8*)SourceBuffer;\r | |
75 | }\r | |
76 | \r | |
77 | /**\r | |
78 | Construct the system base memory range through GCD service.\r | |
79 | \r | |
80 | @param[in] Private Point to generic memory test driver's private data.\r | |
81 | \r | |
82 | @retval EFI_SUCCESS Successful construct the base memory range through GCD service.\r | |
83 | @retval EFI_OUT_OF_RESOURCE Could not allocate needed resource from base memory.\r | |
84 | @retval Others Failed to construct base memory range through GCD service.\r | |
d1102dba | 85 | \r |
7c636bd0 | 86 | **/\r |
87 | EFI_STATUS\r | |
88 | ConstructBaseMemoryRange (\r | |
89 | IN GENERIC_MEMORY_TEST_PRIVATE *Private\r | |
90 | )\r | |
91 | {\r | |
92 | UINTN NumberOfDescriptors;\r | |
93 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r | |
94 | UINTN Index;\r | |
95 | \r | |
96 | //\r | |
97 | // Base memory will always below 4G\r | |
98 | //\r | |
99 | gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r | |
100 | \r | |
101 | for (Index = 0; Index < NumberOfDescriptors; Index++) {\r | |
2e5fb984 RN |
102 | if ((MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeSystemMemory) ||\r |
103 | (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeMoreReliable)) {\r | |
7c636bd0 | 104 | Private->BaseMemorySize += MemorySpaceMap[Index].Length;\r |
105 | }\r | |
106 | }\r | |
107 | \r | |
108 | return EFI_SUCCESS;\r | |
109 | }\r | |
110 | \r | |
111 | /**\r | |
112 | Destroy the link list base on the correspond link list type.\r | |
113 | \r | |
114 | @param[in] Private Point to generic memory test driver's private data.\r | |
d1102dba | 115 | \r |
7c636bd0 | 116 | **/\r |
117 | VOID\r | |
118 | DestroyLinkList (\r | |
119 | IN GENERIC_MEMORY_TEST_PRIVATE *Private\r | |
120 | )\r | |
121 | {\r | |
122 | LIST_ENTRY *Link;\r | |
123 | NONTESTED_MEMORY_RANGE *NontestedRange;\r | |
124 | \r | |
125 | Link = Private->NonTestedMemRanList.BackLink;\r | |
126 | \r | |
127 | while (Link != &Private->NonTestedMemRanList) {\r | |
128 | RemoveEntryList (Link);\r | |
129 | NontestedRange = NONTESTED_MEMORY_RANGE_FROM_LINK (Link);\r | |
130 | gBS->FreePool (NontestedRange);\r | |
131 | Link = Private->NonTestedMemRanList.BackLink;;\r | |
132 | }\r | |
133 | }\r | |
134 | \r | |
2e5fb984 RN |
135 | /**\r |
136 | Convert the memory range to tested.\r | |
137 | \r | |
138 | @param BaseAddress Base address of the memory range.\r | |
139 | @param Length Length of the memory range.\r | |
140 | @param Capabilities Capabilities of the memory range.\r | |
141 | \r | |
142 | @retval EFI_SUCCESS The memory range is converted to tested.\r | |
143 | @retval others Error happens.\r | |
144 | **/\r | |
145 | EFI_STATUS\r | |
146 | ConvertToTestedMemory (\r | |
147 | IN UINT64 BaseAddress,\r | |
148 | IN UINT64 Length,\r | |
149 | IN UINT64 Capabilities\r | |
150 | )\r | |
151 | {\r | |
152 | EFI_STATUS Status;\r | |
153 | Status = gDS->RemoveMemorySpace (\r | |
154 | BaseAddress,\r | |
155 | Length\r | |
156 | );\r | |
157 | if (!EFI_ERROR (Status)) {\r | |
158 | Status = gDS->AddMemorySpace (\r | |
159 | ((Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) ?\r | |
160 | EfiGcdMemoryTypeMoreReliable : EfiGcdMemoryTypeSystemMemory,\r | |
161 | BaseAddress,\r | |
162 | Length,\r | |
163 | Capabilities &~\r | |
164 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r | |
165 | );\r | |
166 | }\r | |
167 | return Status;\r | |
168 | }\r | |
169 | \r | |
7c636bd0 | 170 | /**\r |
171 | Add the extened memory to whole system memory map.\r | |
172 | \r | |
173 | @param[in] Private Point to generic memory test driver's private data.\r | |
174 | \r | |
175 | @retval EFI_SUCCESS Successful add all the extended memory to system memory map.\r | |
176 | @retval Others Failed to add the tested extended memory.\r | |
d1102dba | 177 | \r |
7c636bd0 | 178 | **/\r |
179 | EFI_STATUS\r | |
180 | UpdateMemoryMap (\r | |
181 | IN GENERIC_MEMORY_TEST_PRIVATE *Private\r | |
182 | )\r | |
183 | {\r | |
184 | LIST_ENTRY *Link;\r | |
185 | NONTESTED_MEMORY_RANGE *Range;\r | |
186 | \r | |
187 | Link = Private->NonTestedMemRanList.ForwardLink;\r | |
188 | \r | |
189 | while (Link != &Private->NonTestedMemRanList) {\r | |
190 | Range = NONTESTED_MEMORY_RANGE_FROM_LINK (Link);\r | |
191 | \r | |
2e5fb984 RN |
192 | ConvertToTestedMemory (\r |
193 | Range->StartAddress,\r | |
194 | Range->Length,\r | |
195 | Range->Capabilities &~\r | |
196 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r | |
197 | );\r | |
7c636bd0 | 198 | Link = Link->ForwardLink;\r |
199 | }\r | |
200 | \r | |
201 | return EFI_SUCCESS;\r | |
202 | }\r | |
203 | \r | |
204 | /**\r | |
205 | Test a range of the memory directly .\r | |
206 | \r | |
207 | @param[in] Private Point to generic memory test driver's private data.\r | |
208 | @param[in] StartAddress Starting address of the memory range to be tested.\r | |
209 | @param[in] Length Length in bytes of the memory range to be tested.\r | |
210 | @param[in] Capabilities The bit mask of attributes that the memory range supports.\r | |
211 | \r | |
212 | @retval EFI_SUCCESS Successful test the range of memory.\r | |
213 | @retval Others Failed to test the range of memory.\r | |
d1102dba | 214 | \r |
7c636bd0 | 215 | **/\r |
216 | EFI_STATUS\r | |
217 | DirectRangeTest (\r | |
218 | IN GENERIC_MEMORY_TEST_PRIVATE *Private,\r | |
219 | IN EFI_PHYSICAL_ADDRESS StartAddress,\r | |
220 | IN UINT64 Length,\r | |
221 | IN UINT64 Capabilities\r | |
222 | )\r | |
223 | {\r | |
224 | EFI_STATUS Status;\r | |
225 | \r | |
226 | //\r | |
227 | // Perform a dummy memory test, so directly write the pattern to all range\r | |
228 | //\r | |
229 | WriteMemory (Private, StartAddress, Length);\r | |
230 | \r | |
231 | //\r | |
232 | // Verify the memory range\r | |
233 | //\r | |
234 | Status = VerifyMemory (Private, StartAddress, Length);\r | |
235 | if (EFI_ERROR (Status)) {\r | |
236 | return Status;\r | |
237 | }\r | |
238 | //\r | |
239 | // Add the tested compatible memory to system memory using GCD service\r | |
240 | //\r | |
2e5fb984 RN |
241 | ConvertToTestedMemory (\r |
242 | StartAddress,\r | |
243 | Length,\r | |
244 | Capabilities &~\r | |
245 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r | |
246 | );\r | |
7c636bd0 | 247 | \r |
248 | return EFI_SUCCESS;\r | |
249 | }\r | |
250 | \r | |
251 | /**\r | |
252 | Construct the system non-tested memory range through GCD service.\r | |
253 | \r | |
254 | @param[in] Private Point to generic memory test driver's private data.\r | |
255 | \r | |
256 | @retval EFI_SUCCESS Successful construct the non-tested memory range through GCD service.\r | |
257 | @retval EFI_OUT_OF_RESOURCE Could not allocate needed resource from base memory.\r | |
258 | @retval Others Failed to construct non-tested memory range through GCD service.\r | |
d1102dba | 259 | \r |
7c636bd0 | 260 | **/\r |
261 | EFI_STATUS\r | |
262 | ConstructNonTestedMemoryRange (\r | |
263 | IN GENERIC_MEMORY_TEST_PRIVATE *Private\r | |
264 | )\r | |
265 | {\r | |
266 | NONTESTED_MEMORY_RANGE *Range;\r | |
267 | BOOLEAN NoFound;\r | |
268 | UINTN NumberOfDescriptors;\r | |
269 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r | |
270 | UINTN Index;\r | |
271 | \r | |
272 | //\r | |
273 | // Non tested memory range may be span 4G here\r | |
274 | //\r | |
275 | NoFound = TRUE;\r | |
276 | \r | |
277 | gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r | |
278 | \r | |
279 | for (Index = 0; Index < NumberOfDescriptors; Index++) {\r | |
280 | if (MemorySpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&\r | |
281 | (MemorySpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r | |
282 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)\r | |
283 | ) {\r | |
284 | NoFound = FALSE;\r | |
285 | //\r | |
286 | // Light version do not need to process >4G memory range\r | |
287 | //\r | |
288 | gBS->AllocatePool (\r | |
289 | EfiBootServicesData,\r | |
290 | sizeof (NONTESTED_MEMORY_RANGE),\r | |
291 | (VOID **) &Range\r | |
292 | );\r | |
293 | \r | |
294 | Range->Signature = EFI_NONTESTED_MEMORY_RANGE_SIGNATURE;\r | |
295 | Range->StartAddress = MemorySpaceMap[Index].BaseAddress;\r | |
296 | Range->Length = MemorySpaceMap[Index].Length;\r | |
297 | Range->Capabilities = MemorySpaceMap[Index].Capabilities;\r | |
298 | \r | |
299 | mNonTestedSystemMemory += MemorySpaceMap[Index].Length;\r | |
300 | InsertTailList (&Private->NonTestedMemRanList, &Range->Link);\r | |
301 | }\r | |
302 | }\r | |
303 | \r | |
304 | if (NoFound) {\r | |
305 | return EFI_NOT_FOUND;\r | |
306 | }\r | |
307 | \r | |
308 | return EFI_SUCCESS;\r | |
309 | }\r | |
310 | \r | |
311 | /**\r | |
312 | Write the memory test pattern into a range of physical memory.\r | |
313 | \r | |
314 | @param[in] Private Point to generic memory test driver's private data.\r | |
315 | @param[in] Start The memory range's start address.\r | |
316 | @param[in] Size The memory range's size.\r | |
317 | \r | |
318 | @retval EFI_SUCCESS Successful write the test pattern into the non-tested memory.\r | |
319 | @retval Others The test pattern may not really write into the physical memory.\r | |
d1102dba | 320 | \r |
7c636bd0 | 321 | **/\r |
322 | EFI_STATUS\r | |
323 | WriteMemory (\r | |
324 | IN GENERIC_MEMORY_TEST_PRIVATE *Private,\r | |
325 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
326 | IN UINT64 Size\r | |
327 | )\r | |
328 | {\r | |
329 | EFI_PHYSICAL_ADDRESS Address;\r | |
330 | \r | |
331 | Address = Start;\r | |
332 | \r | |
333 | //\r | |
334 | // Add 4G memory address check for IA32 platform\r | |
335 | // NOTE: Without page table, there is no way to use memory above 4G.\r | |
336 | //\r | |
337 | if (Start + Size > MAX_ADDRESS) {\r | |
338 | return EFI_SUCCESS;\r | |
339 | }\r | |
340 | \r | |
341 | while (Address < (Start + Size)) {\r | |
342 | CopyMem ((VOID *) (UINTN) Address, Private->MonoPattern, Private->MonoTestSize);\r | |
343 | Address += Private->CoverageSpan;\r | |
344 | }\r | |
345 | //\r | |
346 | // bug bug: we may need GCD service to make the code cache and data uncache,\r | |
347 | // if GCD do not support it or return fail, then just flush the whole cache.\r | |
348 | //\r | |
349 | if (Private->Cpu != NULL) {\r | |
350 | Private->Cpu->FlushDataCache (Private->Cpu, Start, Size, EfiCpuFlushTypeWriteBackInvalidate);\r | |
351 | }\r | |
352 | \r | |
353 | return EFI_SUCCESS;\r | |
354 | }\r | |
355 | \r | |
356 | /**\r | |
357 | Verify the range of physical memory which covered by memory test pattern.\r | |
358 | \r | |
359 | This function will also do not return any informatin just cause system reset,\r | |
360 | because the handle error encount fatal error and disable the bad DIMMs.\r | |
361 | \r | |
362 | @param[in] Private Point to generic memory test driver's private data.\r | |
363 | @param[in] Start The memory range's start address.\r | |
364 | @param[in] Size The memory range's size.\r | |
365 | \r | |
366 | @retval EFI_SUCCESS Successful verify the range of memory, no errors' location found.\r | |
367 | @retval Others The range of memory have errors contained.\r | |
d1102dba | 368 | \r |
7c636bd0 | 369 | **/\r |
370 | EFI_STATUS\r | |
371 | VerifyMemory (\r | |
372 | IN GENERIC_MEMORY_TEST_PRIVATE *Private,\r | |
373 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
374 | IN UINT64 Size\r | |
375 | )\r | |
376 | {\r | |
377 | EFI_PHYSICAL_ADDRESS Address;\r | |
378 | INTN ErrorFound;\r | |
379 | EFI_MEMORY_EXTENDED_ERROR_DATA *ExtendedErrorData;\r | |
380 | \r | |
381 | Address = Start;\r | |
382 | ExtendedErrorData = NULL;\r | |
383 | \r | |
384 | //\r | |
385 | // Add 4G memory address check for IA32 platform\r | |
386 | // NOTE: Without page table, there is no way to use memory above 4G.\r | |
387 | //\r | |
388 | if (Start + Size > MAX_ADDRESS) {\r | |
389 | return EFI_SUCCESS;\r | |
390 | }\r | |
391 | \r | |
392 | //\r | |
393 | // Use the software memory test to check whether have detected miscompare\r | |
394 | // error here. If there is miscompare error here then check if generic\r | |
395 | // memory test driver can disable the bad DIMM.\r | |
396 | //\r | |
397 | while (Address < (Start + Size)) {\r | |
398 | ErrorFound = CompareMemWithoutCheckArgument (\r | |
399 | (VOID *) (UINTN) (Address),\r | |
400 | Private->MonoPattern,\r | |
401 | Private->MonoTestSize\r | |
402 | );\r | |
403 | if (ErrorFound != 0) {\r | |
404 | //\r | |
405 | // Report uncorrectable errors\r | |
406 | //\r | |
407 | ExtendedErrorData = AllocateZeroPool (sizeof (EFI_MEMORY_EXTENDED_ERROR_DATA));\r | |
408 | if (ExtendedErrorData == NULL) {\r | |
409 | return EFI_OUT_OF_RESOURCES;\r | |
410 | }\r | |
411 | \r | |
412 | ExtendedErrorData->DataHeader.HeaderSize = (UINT16) sizeof (EFI_STATUS_CODE_DATA);\r | |
413 | ExtendedErrorData->DataHeader.Size = (UINT16) (sizeof (EFI_MEMORY_EXTENDED_ERROR_DATA) - sizeof (EFI_STATUS_CODE_DATA));\r | |
414 | ExtendedErrorData->Granularity = EFI_MEMORY_ERROR_DEVICE;\r | |
415 | ExtendedErrorData->Operation = EFI_MEMORY_OPERATION_READ;\r | |
416 | ExtendedErrorData->Syndrome = 0x0;\r | |
417 | ExtendedErrorData->Address = Address;\r | |
418 | ExtendedErrorData->Resolution = 0x40;\r | |
419 | \r | |
420 | REPORT_STATUS_CODE_EX (\r | |
421 | EFI_ERROR_CODE,\r | |
422 | EFI_COMPUTING_UNIT_MEMORY | EFI_CU_MEMORY_EC_UNCORRECTABLE,\r | |
423 | 0,\r | |
424 | &gEfiGenericMemTestProtocolGuid,\r | |
425 | NULL,\r | |
426 | (UINT8 *) ExtendedErrorData + sizeof (EFI_STATUS_CODE_DATA),\r | |
427 | ExtendedErrorData->DataHeader.Size\r | |
d1102dba | 428 | );\r |
7c636bd0 | 429 | \r |
430 | return EFI_DEVICE_ERROR;\r | |
431 | }\r | |
432 | \r | |
433 | Address += Private->CoverageSpan;\r | |
434 | }\r | |
435 | \r | |
436 | return EFI_SUCCESS;\r | |
437 | }\r | |
438 | \r | |
439 | /**\r | |
440 | Initialize the generic memory test.\r | |
441 | \r | |
d1102dba LG |
442 | @param[in] This The protocol instance pointer.\r |
443 | @param[in] Level The coverage level of the memory test.\r | |
444 | @param[out] RequireSoftECCInit Indicate if the memory need software ECC init.\r | |
7c636bd0 | 445 | \r |
d1102dba LG |
446 | @retval EFI_SUCCESS The generic memory test is initialized correctly.\r |
447 | @retval EFI_NO_MEDIA The system had no memory to be tested.\r | |
7c636bd0 | 448 | \r |
449 | **/\r | |
450 | EFI_STATUS\r | |
451 | EFIAPI\r | |
452 | InitializeMemoryTest (\r | |
453 | IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This,\r | |
454 | IN EXTENDMEM_COVERAGE_LEVEL Level,\r | |
455 | OUT BOOLEAN *RequireSoftECCInit\r | |
456 | )\r | |
457 | {\r | |
458 | EFI_STATUS Status;\r | |
459 | GENERIC_MEMORY_TEST_PRIVATE *Private;\r | |
460 | EFI_CPU_ARCH_PROTOCOL *Cpu;\r | |
461 | \r | |
462 | Private = GENERIC_MEMORY_TEST_PRIVATE_FROM_THIS (This);\r | |
463 | *RequireSoftECCInit = FALSE;\r | |
464 | \r | |
465 | //\r | |
466 | // This is initialize for default value, but some value may be reset base on\r | |
467 | // platform memory test driver.\r | |
468 | //\r | |
469 | Private->CoverLevel = Level;\r | |
470 | Private->BdsBlockSize = TEST_BLOCK_SIZE;\r | |
471 | Private->MonoPattern = GenericMemoryTestMonoPattern;\r | |
472 | Private->MonoTestSize = GENERIC_CACHELINE_SIZE;\r | |
473 | \r | |
474 | //\r | |
475 | // Initialize several internal link list\r | |
476 | //\r | |
477 | InitializeListHead (&Private->NonTestedMemRanList);\r | |
478 | \r | |
479 | //\r | |
480 | // Construct base memory range\r | |
481 | //\r | |
482 | ConstructBaseMemoryRange (Private);\r | |
483 | \r | |
484 | //\r | |
485 | // get the cpu arch protocol to support flash cache\r | |
486 | //\r | |
487 | Status = gBS->LocateProtocol (\r | |
488 | &gEfiCpuArchProtocolGuid,\r | |
489 | NULL,\r | |
490 | (VOID **) &Cpu\r | |
491 | );\r | |
492 | if (!EFI_ERROR (Status)) {\r | |
493 | Private->Cpu = Cpu;\r | |
494 | }\r | |
495 | //\r | |
496 | // Create the CoverageSpan of the memory test base on the coverage level\r | |
497 | //\r | |
498 | switch (Private->CoverLevel) {\r | |
499 | case EXTENSIVE:\r | |
500 | Private->CoverageSpan = GENERIC_CACHELINE_SIZE;\r | |
501 | break;\r | |
502 | \r | |
503 | case SPARSE:\r | |
504 | Private->CoverageSpan = SPARSE_SPAN_SIZE;\r | |
505 | break;\r | |
506 | \r | |
507 | //\r | |
508 | // Even the BDS do not need to test any memory, but in some case it\r | |
509 | // still need to init ECC memory.\r | |
510 | //\r | |
511 | default:\r | |
512 | Private->CoverageSpan = QUICK_SPAN_SIZE;\r | |
513 | break;\r | |
514 | }\r | |
515 | //\r | |
516 | // This is the first time we construct the non-tested memory range, if no\r | |
517 | // extended memory found, we know the system have not any extended memory\r | |
518 | // need to be test\r | |
519 | //\r | |
520 | Status = ConstructNonTestedMemoryRange (Private);\r | |
521 | if (Status == EFI_NOT_FOUND) {\r | |
522 | return EFI_NO_MEDIA;\r | |
523 | }\r | |
524 | //\r | |
525 | // ready to perform the R/W/V memory test\r | |
526 | //\r | |
527 | mTestedSystemMemory = Private->BaseMemorySize;\r | |
528 | mCurrentLink = Private->NonTestedMemRanList.ForwardLink;\r | |
529 | mCurrentRange = NONTESTED_MEMORY_RANGE_FROM_LINK (mCurrentLink);\r | |
530 | mCurrentAddress = mCurrentRange->StartAddress;\r | |
531 | \r | |
532 | return EFI_SUCCESS;\r | |
533 | }\r | |
534 | \r | |
535 | /**\r | |
536 | Perform the memory test.\r | |
537 | \r | |
d1102dba LG |
538 | @param[in] This The protocol instance pointer.\r |
539 | @param[out] TestedMemorySize Return the tested extended memory size.\r | |
540 | @param[out] TotalMemorySize Return the whole system physical memory size.\r | |
541 | The total memory size does not include memory in a slot with a disabled DIMM.\r | |
7c636bd0 | 542 | @param[out] ErrorOut TRUE if the memory error occured.\r |
d1102dba | 543 | @param[in] IfTestAbort Indicates that the user pressed "ESC" to skip the memory test.\r |
7c636bd0 | 544 | \r |
545 | @retval EFI_SUCCESS One block of memory passed the test.\r | |
546 | @retval EFI_NOT_FOUND All memory blocks have already been tested.\r | |
547 | @retval EFI_DEVICE_ERROR Memory device error occured, and no agent can handle it.\r | |
548 | \r | |
549 | **/\r | |
550 | EFI_STATUS\r | |
551 | EFIAPI\r | |
552 | GenPerformMemoryTest (\r | |
553 | IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This,\r | |
554 | OUT UINT64 *TestedMemorySize,\r | |
555 | OUT UINT64 *TotalMemorySize,\r | |
556 | OUT BOOLEAN *ErrorOut,\r | |
557 | IN BOOLEAN TestAbort\r | |
558 | )\r | |
559 | {\r | |
560 | EFI_STATUS Status;\r | |
561 | GENERIC_MEMORY_TEST_PRIVATE *Private;\r | |
562 | EFI_MEMORY_RANGE_EXTENDED_DATA *RangeData;\r | |
563 | UINT64 BlockBoundary;\r | |
564 | \r | |
565 | Private = GENERIC_MEMORY_TEST_PRIVATE_FROM_THIS (This);\r | |
566 | *ErrorOut = FALSE;\r | |
567 | RangeData = NULL;\r | |
568 | BlockBoundary = 0;\r | |
569 | \r | |
570 | //\r | |
571 | // In extensive mode the boundary of "mCurrentRange->Length" may will lost\r | |
0a18956d | 572 | // some range that is not Private->BdsBlockSize size boundary, so need\r |
7c636bd0 | 573 | // the software mechanism to confirm all memory location be covered.\r |
574 | //\r | |
575 | if (mCurrentAddress < (mCurrentRange->StartAddress + mCurrentRange->Length)) {\r | |
576 | if ((mCurrentAddress + Private->BdsBlockSize) <= (mCurrentRange->StartAddress + mCurrentRange->Length)) {\r | |
577 | BlockBoundary = Private->BdsBlockSize;\r | |
578 | } else {\r | |
579 | BlockBoundary = mCurrentRange->StartAddress + mCurrentRange->Length - mCurrentAddress;\r | |
580 | }\r | |
581 | //\r | |
582 | // If TestAbort is true, means user cancel the memory test\r | |
583 | //\r | |
584 | if (!TestAbort && Private->CoverLevel != IGNORE) {\r | |
585 | //\r | |
586 | // Report status code of every memory range\r | |
587 | //\r | |
588 | RangeData = AllocateZeroPool (sizeof (EFI_MEMORY_RANGE_EXTENDED_DATA));\r | |
589 | if (RangeData == NULL) {\r | |
590 | return EFI_OUT_OF_RESOURCES;\r | |
591 | }\r | |
592 | RangeData->DataHeader.HeaderSize = (UINT16) sizeof (EFI_STATUS_CODE_DATA);\r | |
593 | RangeData->DataHeader.Size = (UINT16) (sizeof (EFI_MEMORY_RANGE_EXTENDED_DATA) - sizeof (EFI_STATUS_CODE_DATA));\r | |
594 | RangeData->Start = mCurrentAddress;\r | |
595 | RangeData->Length = BlockBoundary;\r | |
596 | \r | |
597 | REPORT_STATUS_CODE_EX (\r | |
598 | EFI_PROGRESS_CODE,\r | |
599 | EFI_COMPUTING_UNIT_MEMORY | EFI_CU_MEMORY_PC_TEST,\r | |
600 | 0,\r | |
601 | &gEfiGenericMemTestProtocolGuid,\r | |
602 | NULL,\r | |
603 | (UINT8 *) RangeData + sizeof (EFI_STATUS_CODE_DATA),\r | |
604 | RangeData->DataHeader.Size\r | |
605 | );\r | |
606 | \r | |
607 | //\r | |
608 | // The software memory test (R/W/V) perform here. It will detect the\r | |
609 | // memory mis-compare error.\r | |
610 | //\r | |
611 | WriteMemory (Private, mCurrentAddress, BlockBoundary);\r | |
612 | \r | |
613 | Status = VerifyMemory (Private, mCurrentAddress, BlockBoundary);\r | |
614 | if (EFI_ERROR (Status)) {\r | |
615 | //\r | |
616 | // If perform here, means there is mis-compare error, and no agent can\r | |
617 | // handle it, so we return to BDS EFI_DEVICE_ERROR.\r | |
618 | //\r | |
619 | *ErrorOut = TRUE;\r | |
620 | return EFI_DEVICE_ERROR;\r | |
621 | }\r | |
622 | }\r | |
623 | \r | |
624 | mTestedSystemMemory += BlockBoundary;\r | |
625 | *TestedMemorySize = mTestedSystemMemory;\r | |
626 | \r | |
627 | //\r | |
628 | // If the memory test restart after the platform driver disable dimms,\r | |
629 | // the NonTestSystemMemory may be changed, but the base memory size will\r | |
630 | // not changed, so we can get the current total memory size.\r | |
631 | //\r | |
632 | *TotalMemorySize = Private->BaseMemorySize + mNonTestedSystemMemory;\r | |
633 | \r | |
634 | //\r | |
635 | // Update the current test address pointing to next BDS BLOCK\r | |
636 | //\r | |
637 | mCurrentAddress += Private->BdsBlockSize;\r | |
638 | \r | |
639 | return EFI_SUCCESS;\r | |
640 | }\r | |
641 | //\r | |
642 | // Change to next non tested memory range\r | |
643 | //\r | |
644 | mCurrentLink = mCurrentLink->ForwardLink;\r | |
645 | if (mCurrentLink != &Private->NonTestedMemRanList) {\r | |
646 | mCurrentRange = NONTESTED_MEMORY_RANGE_FROM_LINK (mCurrentLink);\r | |
647 | mCurrentAddress = mCurrentRange->StartAddress;\r | |
648 | return EFI_SUCCESS;\r | |
649 | } else {\r | |
650 | //\r | |
651 | // Here means all the memory test have finished\r | |
652 | //\r | |
653 | *TestedMemorySize = mTestedSystemMemory;\r | |
654 | *TotalMemorySize = Private->BaseMemorySize + mNonTestedSystemMemory;\r | |
655 | return EFI_NOT_FOUND;\r | |
656 | }\r | |
657 | \r | |
658 | }\r | |
659 | \r | |
660 | /**\r | |
661 | Finish the memory test.\r | |
662 | \r | |
d1102dba | 663 | @param[in] This The protocol instance pointer.\r |
7c636bd0 | 664 | \r |
665 | @retval EFI_SUCCESS Success. All resources used in the memory test are freed.\r | |
666 | \r | |
667 | **/\r | |
668 | EFI_STATUS\r | |
669 | EFIAPI\r | |
670 | GenMemoryTestFinished (\r | |
671 | IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This\r | |
672 | )\r | |
673 | {\r | |
674 | EFI_STATUS Status;\r | |
675 | GENERIC_MEMORY_TEST_PRIVATE *Private;\r | |
676 | \r | |
677 | Private = GENERIC_MEMORY_TEST_PRIVATE_FROM_THIS (This);\r | |
678 | \r | |
679 | //\r | |
680 | // Perform Data and Address line test\r | |
681 | //\r | |
682 | Status = PerformAddressDataLineTest (Private);\r | |
683 | ASSERT_EFI_ERROR (Status);\r | |
684 | \r | |
685 | //\r | |
686 | // Add the non tested memory range to system memory map through GCD service\r | |
687 | //\r | |
688 | UpdateMemoryMap (Private);\r | |
689 | \r | |
690 | //\r | |
691 | // we need to free all the memory allocate\r | |
692 | //\r | |
693 | DestroyLinkList (Private);\r | |
694 | \r | |
695 | return EFI_SUCCESS;\r | |
696 | }\r | |
697 | \r | |
698 | /**\r | |
699 | Provides the capability to test the compatible range used by some special drivers.\r | |
700 | \r | |
d1102dba | 701 | @param[in] This The protocol instance pointer.\r |
7c636bd0 | 702 | @param[in] StartAddress The start address of the compatible memory range that\r |
703 | must be below 16M.\r | |
d1102dba LG |
704 | @param[in] Length The compatible memory range's length.\r |
705 | \r | |
706 | @retval EFI_SUCCESS The compatible memory range pass the memory test.\r | |
7c636bd0 | 707 | @retval EFI_INVALID_PARAMETER The compatible memory range are not below Low 16M.\r |
708 | \r | |
709 | **/\r | |
710 | EFI_STATUS\r | |
711 | EFIAPI\r | |
712 | GenCompatibleRangeTest (\r | |
713 | IN EFI_GENERIC_MEMORY_TEST_PROTOCOL *This,\r | |
714 | IN EFI_PHYSICAL_ADDRESS StartAddress,\r | |
715 | IN UINT64 Length\r | |
716 | )\r | |
717 | {\r | |
718 | EFI_STATUS Status;\r | |
719 | GENERIC_MEMORY_TEST_PRIVATE *Private;\r | |
720 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r | |
721 | EFI_PHYSICAL_ADDRESS CurrentBase;\r | |
722 | UINT64 CurrentLength;\r | |
723 | \r | |
724 | Private = GENERIC_MEMORY_TEST_PRIVATE_FROM_THIS (This);\r | |
725 | \r | |
726 | //\r | |
727 | // Check if the parameter is below 16MB\r | |
728 | //\r | |
729 | if (StartAddress + Length > 0x1000000) {\r | |
730 | return EFI_INVALID_PARAMETER;\r | |
731 | }\r | |
732 | CurrentBase = StartAddress;\r | |
733 | do {\r | |
734 | //\r | |
735 | // Check the required memory range status; if the required memory range span\r | |
736 | // the different GCD memory descriptor, it may be cause different action.\r | |
737 | //\r | |
738 | Status = gDS->GetMemorySpaceDescriptor (\r | |
739 | CurrentBase,\r | |
740 | &Descriptor\r | |
741 | );\r | |
742 | if (EFI_ERROR (Status)) {\r | |
743 | return Status;\r | |
744 | }\r | |
745 | \r | |
746 | if (Descriptor.GcdMemoryType == EfiGcdMemoryTypeReserved &&\r | |
747 | (Descriptor.Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r | |
748 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)\r | |
749 | ) {\r | |
750 | CurrentLength = Descriptor.BaseAddress + Descriptor.Length - CurrentBase;\r | |
751 | if (CurrentBase + CurrentLength > StartAddress + Length) {\r | |
752 | CurrentLength = StartAddress + Length - CurrentBase;\r | |
753 | }\r | |
754 | Status = DirectRangeTest (\r | |
755 | Private,\r | |
756 | CurrentBase,\r | |
757 | CurrentLength,\r | |
758 | Descriptor.Capabilities\r | |
759 | );\r | |
760 | if (EFI_ERROR (Status)) {\r | |
761 | return Status;\r | |
762 | }\r | |
763 | }\r | |
764 | CurrentBase = Descriptor.BaseAddress + Descriptor.Length;\r | |
765 | } while (CurrentBase < StartAddress + Length);\r | |
766 | //\r | |
767 | // Here means the required range already be tested, so just return success.\r | |
768 | //\r | |
769 | return EFI_SUCCESS;\r | |
770 | }\r | |
771 | \r | |
772 | /**\r | |
773 | Perform the address line walking ones test.\r | |
774 | \r | |
775 | @param[in] Private Point to generic memory test driver's private data.\r | |
d1102dba LG |
776 | \r |
777 | @retval EFI_SUCCESS Successful finished walking ones test.\r | |
778 | @retval EFI_OUT_OF_RESOURCE Could not get resource in base memory.\r | |
7c636bd0 | 779 | @retval EFI_ACCESS_DENIED Code may can not run here because if walking one test\r |
780 | failed, system may be already halt.\r | |
781 | \r | |
782 | **/\r | |
783 | EFI_STATUS\r | |
784 | PerformAddressDataLineTest (\r | |
785 | IN GENERIC_MEMORY_TEST_PRIVATE *Private\r | |
786 | )\r | |
787 | {\r | |
788 | LIST_ENTRY *ExtendedLink;\r | |
789 | NONTESTED_MEMORY_RANGE *ExtendedRange;\r | |
790 | BOOLEAN InExtendedRange;\r | |
791 | EFI_PHYSICAL_ADDRESS TestAddress;\r | |
792 | \r | |
793 | //\r | |
794 | // Light version no data line test, only perform the address line test\r | |
795 | //\r | |
796 | TestAddress = (EFI_PHYSICAL_ADDRESS) 0x1;\r | |
797 | while (TestAddress < MAX_ADDRESS && TestAddress > 0) {\r | |
798 | //\r | |
799 | // only test if the address falls in the enabled range\r | |
800 | //\r | |
801 | InExtendedRange = FALSE;\r | |
802 | ExtendedLink = Private->NonTestedMemRanList.BackLink;\r | |
803 | while (ExtendedLink != &Private->NonTestedMemRanList) {\r | |
804 | ExtendedRange = NONTESTED_MEMORY_RANGE_FROM_LINK (ExtendedLink);\r | |
805 | if ((TestAddress >= ExtendedRange->StartAddress) &&\r | |
806 | (TestAddress < (ExtendedRange->StartAddress + ExtendedRange->Length))\r | |
807 | ) {\r | |
808 | InExtendedRange = TRUE;\r | |
809 | }\r | |
810 | \r | |
811 | ExtendedLink = ExtendedLink->BackLink;\r | |
812 | }\r | |
813 | \r | |
814 | if (InExtendedRange) {\r | |
815 | *(EFI_PHYSICAL_ADDRESS *) (UINTN) TestAddress = TestAddress;\r | |
816 | Private->Cpu->FlushDataCache (Private->Cpu, TestAddress, 1, EfiCpuFlushTypeWriteBackInvalidate);\r | |
817 | if (*(EFI_PHYSICAL_ADDRESS *) (UINTN) TestAddress != TestAddress) {\r | |
818 | return EFI_ACCESS_DENIED;\r | |
819 | }\r | |
820 | }\r | |
821 | \r | |
822 | TestAddress = LShiftU64 (TestAddress, 1);\r | |
823 | }\r | |
824 | \r | |
825 | return EFI_SUCCESS;\r | |
826 | }\r | |
827 | //\r | |
828 | // Driver entry here\r | |
829 | //\r | |
830 | GENERIC_MEMORY_TEST_PRIVATE mGenericMemoryTestPrivate = {\r | |
831 | EFI_GENERIC_MEMORY_TEST_PRIVATE_SIGNATURE,\r | |
832 | NULL,\r | |
833 | NULL,\r | |
834 | {\r | |
835 | InitializeMemoryTest,\r | |
836 | GenPerformMemoryTest,\r | |
837 | GenMemoryTestFinished,\r | |
838 | GenCompatibleRangeTest\r | |
839 | },\r | |
840 | (EXTENDMEM_COVERAGE_LEVEL) 0,\r | |
841 | 0,\r | |
842 | 0,\r | |
843 | NULL,\r | |
844 | 0,\r | |
845 | 0,\r | |
846 | {\r | |
847 | NULL,\r | |
848 | NULL\r | |
849 | }\r | |
850 | };\r | |
851 | \r | |
852 | /**\r | |
853 | The generic memory test driver's entry point.\r | |
854 | \r | |
855 | It initializes private data to default value.\r | |
856 | \r | |
d1102dba | 857 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r |
7c636bd0 | 858 | @param[in] SystemTable A pointer to the EFI System Table.\r |
d1102dba | 859 | \r |
7c636bd0 | 860 | @retval EFI_SUCCESS The entry point is executed successfully.\r |
861 | @retval EFI_NOT_FOUND Can't find HandOff Hob in HobList.\r | |
862 | @retval other Some error occurs when executing this entry point.\r | |
863 | \r | |
864 | **/\r | |
865 | EFI_STATUS\r | |
866 | EFIAPI\r | |
867 | GenericMemoryTestEntryPoint (\r | |
868 | IN EFI_HANDLE ImageHandle,\r | |
869 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
870 | )\r | |
871 | {\r | |
872 | EFI_STATUS Status;\r | |
873 | VOID *HobList;\r | |
874 | EFI_BOOT_MODE BootMode;\r | |
875 | EFI_PEI_HOB_POINTERS Hob;\r | |
876 | \r | |
877 | //\r | |
878 | // Use the generic pattern to test compatible memory range\r | |
879 | //\r | |
880 | mGenericMemoryTestPrivate.MonoPattern = GenericMemoryTestMonoPattern;\r | |
881 | mGenericMemoryTestPrivate.MonoTestSize = GENERIC_CACHELINE_SIZE;\r | |
882 | \r | |
883 | //\r | |
884 | // Get the platform boot mode\r | |
885 | //\r | |
886 | HobList = GetHobList ();\r | |
887 | \r | |
888 | Hob.Raw = HobList;\r | |
889 | if (Hob.Header->HobType != EFI_HOB_TYPE_HANDOFF) {\r | |
890 | return EFI_NOT_FOUND;\r | |
891 | }\r | |
892 | \r | |
893 | BootMode = Hob.HandoffInformationTable->BootMode;\r | |
894 | \r | |
895 | //\r | |
896 | // Get the platform boot mode and create the default memory test coverage\r | |
897 | // level and span size for compatible memory test using\r | |
898 | //\r | |
899 | switch (BootMode) {\r | |
900 | case BOOT_WITH_FULL_CONFIGURATION:\r | |
901 | case BOOT_WITH_DEFAULT_SETTINGS:\r | |
902 | mGenericMemoryTestPrivate.CoverageSpan = SPARSE_SPAN_SIZE;\r | |
903 | break;\r | |
904 | \r | |
905 | case BOOT_WITH_FULL_CONFIGURATION_PLUS_DIAGNOSTICS:\r | |
906 | mGenericMemoryTestPrivate.CoverageSpan = GENERIC_CACHELINE_SIZE;\r | |
907 | break;\r | |
908 | \r | |
909 | default:\r | |
910 | mGenericMemoryTestPrivate.CoverageSpan = QUICK_SPAN_SIZE;\r | |
911 | break;\r | |
912 | }\r | |
913 | //\r | |
914 | // Install the protocol\r | |
915 | //\r | |
916 | Status = gBS->InstallProtocolInterface (\r | |
917 | &mGenericMemoryTestPrivate.Handle,\r | |
918 | &gEfiGenericMemTestProtocolGuid,\r | |
919 | EFI_NATIVE_INTERFACE,\r | |
920 | &mGenericMemoryTestPrivate.GenericMemoryTest\r | |
921 | );\r | |
922 | \r | |
923 | return Status;\r | |
924 | }\r |