3 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
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.
21 Tiano PEIM to provide the platform support functionality.
22 This file implements the Platform Memory Range PPI
26 #include "PlatformEarlyInit.h"
29 // Need min. of 48MB PEI phase
31 #define PEI_MIN_MEMORY_SIZE (6 * 0x800000)
32 #define PEI_RECOVERY_MIN_MEMORY_SIZE (6 * 0x800000)
35 // This is the memory needed for PEI to start up DXE.
37 // Over-estimating this size will lead to higher fragmentation
38 // of main memory. Under-estimation of this will cause catastrophic
39 // failure of PEI to load DXE. Generally, the failure may only be
40 // realized during capsule updates.
42 #define PRERESERVED_PEI_MEMORY ( \
43 EFI_SIZE_TO_PAGES (3 * 0x800000) /* PEI Core memory based stack */ \
46 EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation
[] = {
47 { EfiACPIReclaimMemory
, 0x40 }, // 0x40 pages = 256k for ASL
48 { EfiACPIMemoryNVS
, 0x100 }, // 0x100 pages = 1 MB for S3, SMM, HII, etc
49 { EfiReservedMemoryType
, 0x600 }, // 48k for BIOS Reserved
50 { EfiMemoryMappedIO
, 0 },
51 { EfiMemoryMappedIOPortSpace
, 0 },
53 { EfiRuntimeServicesCode
, 0x200 },
54 { EfiRuntimeServicesData
, 0x100 },
55 { EfiLoaderCode
, 0x100 },
56 { EfiLoaderData
, 0x100 },
57 { EfiBootServicesCode
, 0x800 },
58 { EfiBootServicesData
, 0x2500},
59 { EfiConventionalMemory
, 0 },
60 { EfiUnusableMemory
, 0 },
61 { EfiMaxMemoryType
, 0 }
67 IN CONST EFI_PEI_SERVICES
**PeiServices
,
68 OUT UINT64
*LowMemoryLength
,
69 OUT UINT64
*HighMemoryLength
77 IN CONST EFI_PEI_SERVICES
**PeiServices
81 PEI_CACHE_PPI
*CachePpi
;
83 EFI_BOOT_MODE BootMode
;
86 UINT64 MemoryLengthUc
;
87 UINT64 MaxMemoryLength
;
88 UINT64 LowMemoryLength
;
89 UINT64 HighMemoryLength
;
91 MTRR_SETTINGS MtrrSetting
;
96 Status
= (**PeiServices
).LocatePpi (
98 &gPeiCachePpiGuid
, // GUID
100 NULL
, // EFI_PEI_PPI_DESCRIPTOR
101 (void **)&CachePpi
// PPI
103 if (!EFI_ERROR(Status
)) {
105 // Clear the CAR Settings (Default Cache Type => UC)
107 DEBUG ((EFI_D_INFO
, "Reset cache attribute and disable CAR. \n"));
108 CachePpi
->ResetCache(
109 (EFI_PEI_SERVICES
**)PeiServices
,
116 // Variable initialization
119 HighMemoryLength
= 0;
122 Status
= (*PeiServices
)->GetBootMode (
128 // Determine memory usage
136 LowMemoryLength
= (EFI_PHYSICAL_ADDRESS
)MmPci32( 0, 0, 2, 0, 0x70);
137 LowMemoryLength
&= 0xFFF00000ULL
;
139 MaxMemoryLength
= LowMemoryLength
;
142 // Round up to nearest 256MB with high memory and 64MB w/o high memory
144 if (HighMemoryLength
!= 0 ) {
145 MemOverflow
= (LowMemoryLength
& 0x0fffffff);
146 if (MemOverflow
!= 0) {
147 MaxMemoryLength
= LowMemoryLength
+ (0x10000000 - MemOverflow
);
150 MemOverflow
= (LowMemoryLength
& 0x03ffffff);
151 if (MemOverflow
!= 0) {
152 MaxMemoryLength
= LowMemoryLength
+ (0x4000000 - MemOverflow
);
156 ZeroMem (&MtrrSetting
, sizeof(MTRR_SETTINGS
));
157 for (Index
= 0; Index
< 2; Index
++) {
158 MtrrSetting
.Fixed
.Mtrr
[Index
]=0x0606060606060606;
160 for (Index
= 2; Index
< 11; Index
++) {
161 MtrrSetting
.Fixed
.Mtrr
[Index
]=0x0505050505050505;
165 // Cache the flash area to improve the boot performance in PEI phase
168 MtrrSetting
.Variables
.Mtrr
[0].Base
= (FixedPcdGet32 (PcdFlashAreaBaseAddress
) | CacheWriteProtected
);
169 MtrrSetting
.Variables
.Mtrr
[0].Mask
= ((~((UINT64
)(FixedPcdGet32 (PcdFlashAreaSize
) - 1))) & MTRR_LIB_CACHE_VALID_ADDRESS
) | MTRR_LIB_CACHE_MTRR_ENABLED
;
173 while (MaxMemoryLength
> MemOverflow
){
174 MtrrSetting
.Variables
.Mtrr
[Index
].Base
= (MemOverflow
& MTRR_LIB_CACHE_VALID_ADDRESS
) | CacheWriteBack
;
175 MemoryLength
= MaxMemoryLength
- MemOverflow
;
176 MemoryLength
= GetPowerOfTwo64 (MemoryLength
);
177 MtrrSetting
.Variables
.Mtrr
[Index
].Mask
= ((~(MemoryLength
- 1)) & MTRR_LIB_CACHE_VALID_ADDRESS
) | MTRR_LIB_CACHE_MTRR_ENABLED
;
179 MemOverflow
+= MemoryLength
;
183 MemoryLength
= LowMemoryLength
;
185 while (MaxMemoryLength
!= MemoryLength
) {
186 MemoryLengthUc
= GetPowerOfTwo64 (MaxMemoryLength
- MemoryLength
);
188 MtrrSetting
.Variables
.Mtrr
[Index
].Base
= ((MaxMemoryLength
- MemoryLengthUc
) & MTRR_LIB_CACHE_VALID_ADDRESS
) | CacheUncacheable
;
189 MtrrSetting
.Variables
.Mtrr
[Index
].Mask
= ((~(MemoryLengthUc
- 1)) & MTRR_LIB_CACHE_VALID_ADDRESS
) | MTRR_LIB_CACHE_MTRR_ENABLED
;
190 MaxMemoryLength
-= MemoryLengthUc
;
194 MemOverflow
=0x100000000;
195 while (HighMemoryLength
> 0) {
196 MtrrSetting
.Variables
.Mtrr
[Index
].Base
= (MemOverflow
& MTRR_LIB_CACHE_VALID_ADDRESS
) | CacheWriteBack
;
197 MemoryLength
= HighMemoryLength
;
198 MemoryLength
= GetPowerOfTwo64 (MemoryLength
);
200 if (MemoryLength
> MemOverflow
){
201 MemoryLength
= MemOverflow
;
204 MtrrSetting
.Variables
.Mtrr
[Index
].Mask
= ((~(MemoryLength
- 1)) & MTRR_LIB_CACHE_VALID_ADDRESS
) | MTRR_LIB_CACHE_MTRR_ENABLED
;
206 MemOverflow
+= MemoryLength
;
207 HighMemoryLength
-= MemoryLength
;
212 for (Index
= 0; Index
< MTRR_NUMBER_OF_VARIABLE_MTRR
; Index
++) {
213 if (MtrrSetting
.Variables
.Mtrr
[Index
].Base
== 0){
216 DEBUG ((EFI_D_INFO
, "Base=%lx, Mask=%lx\n",MtrrSetting
.Variables
.Mtrr
[Index
].Base
,MtrrSetting
.Variables
.Mtrr
[Index
].Mask
));
220 // set FE/E bits for IA32_MTRR_DEF_TYPE
222 MtrrSetting
.MtrrDefType
|= 3 <<10;
224 MtrrSetAllMtrrs(&MtrrSetting
);
228 MtrrDebugPrintAllMtrrs ();
236 IN CONST EFI_PEI_SERVICES
**PeiServices
240 // This is not needed for now.
248 IN CONST EFI_PEI_SERVICES
**PeiServices
,
249 OUT UINT64
*LowMemoryLength
,
250 OUT UINT64
*HighMemoryLength
254 EFI_PEI_HOB_POINTERS Hob
;
256 *HighMemoryLength
= 0;
257 *LowMemoryLength
= 0x100000;
260 // Get the HOB list for processing
262 Status
= (*PeiServices
)->GetHobList (PeiServices
, (void **)&Hob
.Raw
);
263 if (EFI_ERROR(Status
)) {
268 // Collect memory ranges
270 while (!END_OF_HOB_LIST (Hob
)) {
271 if (Hob
.Header
->HobType
== EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
) {
272 if (Hob
.ResourceDescriptor
->ResourceType
== EFI_RESOURCE_SYSTEM_MEMORY
) {
274 // Need memory above 1MB to be collected here
276 if (Hob
.ResourceDescriptor
->PhysicalStart
>= 0x100000 &&
277 Hob
.ResourceDescriptor
->PhysicalStart
< (EFI_PHYSICAL_ADDRESS
) 0x100000000) {
278 *LowMemoryLength
+= (UINT64
) (Hob
.ResourceDescriptor
->ResourceLength
);
279 } else if (Hob
.ResourceDescriptor
->PhysicalStart
>= (EFI_PHYSICAL_ADDRESS
) 0x100000000) {
280 *HighMemoryLength
+= (UINT64
) (Hob
.ResourceDescriptor
->ResourceLength
);
284 Hob
.Raw
= GET_NEXT_HOB (Hob
);
292 Publish Memory Type Information.
296 @retval EFI_SUCCESS Success.
297 @retval Others Errors have occurred.
302 PublishMemoryTypeInfo (
307 EFI_PEI_READ_ONLY_VARIABLE2_PPI
*Variable
;
309 EFI_MEMORY_TYPE_INFORMATION MemoryData
[EfiMaxMemoryType
+ 1];
311 Status
= PeiServicesLocatePpi (
312 &gEfiPeiReadOnlyVariable2PpiGuid
,
317 if (EFI_ERROR(Status
)) {
318 DEBUG((EFI_D_ERROR
, "WARNING: Locating Pei variable failed 0x%x \n", Status
));
319 DEBUG((EFI_D_ERROR
, "Build Hob from default\n"));
321 // Build the default GUID'd HOB for DXE
324 &gEfiMemoryTypeInformationGuid
,
325 mDefaultMemoryTypeInformation
,
326 sizeof (mDefaultMemoryTypeInformation
)
333 DataSize
= sizeof (MemoryData
);
336 // This variable is saved in BDS stage. Now read it back
338 Status
= Variable
->GetVariable (
340 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME
,
341 &gEfiMemoryTypeInformationGuid
,
346 if (EFI_ERROR (Status
)) {
350 DEBUG((EFI_D_ERROR
, "Build Hob from default\n"));
352 &gEfiMemoryTypeInformationGuid
,
353 mDefaultMemoryTypeInformation
,
354 sizeof (mDefaultMemoryTypeInformation
)
359 // Build the GUID'd HOB for DXE from variable
361 DEBUG((EFI_D_ERROR
, "Build Hob from variable \n"));
363 &gEfiMemoryTypeInformationGuid
,