Fsp1.1 update.
[mirror_edk2.git] / IntelFspPkg / Library / BaseFspPlatformLib / FspPlatformMemory.c
1 /** @file
2
3 Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include <PiPei.h>
15 #include <Library/BaseLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/HobLib.h>
21 #include <Library/PeiServicesLib.h>
22 #include <Library/FspCommonLib.h>
23 #include <Guid/GuidHobFsp.h>
24 #include <FspGlobalData.h>
25 #include <FspApi.h>
26
27 /**
28 Get system memory from HOB.
29
30 @param[in,out] LowMemoryLength less than 4G memory length
31 @param[in,out] HighMemoryLength greater than 4G memory length
32 **/
33 VOID
34 EFIAPI
35 FspGetSystemMemorySize (
36 IN OUT UINT64 *LowMemoryLength,
37 IN OUT UINT64 *HighMemoryLength
38 )
39 {
40 EFI_PEI_HOB_POINTERS Hob;
41
42 *HighMemoryLength = 0;
43 *LowMemoryLength = SIZE_1MB;
44 //
45 // Get the HOB list for processing
46 //
47 Hob.Raw = GetHobList ();
48
49 //
50 // Collect memory ranges
51 //
52 while (!END_OF_HOB_LIST (Hob)) {
53 if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
54 if (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
55 //
56 // Need memory above 1MB to be collected here
57 //
58 if (Hob.ResourceDescriptor->PhysicalStart >= BASE_1MB &&
59 Hob.ResourceDescriptor->PhysicalStart < (EFI_PHYSICAL_ADDRESS) BASE_4GB) {
60 *LowMemoryLength += (UINT64) (Hob.ResourceDescriptor->ResourceLength);
61 } else if (Hob.ResourceDescriptor->PhysicalStart >= (EFI_PHYSICAL_ADDRESS) BASE_4GB) {
62 *HighMemoryLength += (UINT64) (Hob.ResourceDescriptor->ResourceLength);
63 }
64 }
65 }
66 Hob.Raw = GET_NEXT_HOB (Hob);
67 }
68 }
69
70 /**
71 Migrate bootloader data before destroying CAR.
72
73 **/
74 VOID
75 EFIAPI
76 FspMigrateTemporaryMemory (
77 VOID
78 )
79 {
80 FSP_INIT_RT_COMMON_BUFFER *FspInitRtBuffer;
81 UINT32 BootLoaderTempRamStart;
82 UINT32 BootLoaderTempRamEnd;
83 UINT32 BootLoaderTempRamSize;
84 UINT32 OffsetGap;
85 UINT32 FspParamPtr;
86 FSP_INIT_PARAMS *FspInitParams;
87 UINT32 *NewStackTop;
88 VOID *BootLoaderTempRamHob;
89 VOID *UpdDataRgnPtr;
90 VOID *PlatformDataPtr;
91 UINT8 ApiMode;
92
93 ApiMode = GetFspApiCallingMode ();
94
95 //
96 // Get the temporary memory range used by the bootloader
97 //
98 BootLoaderTempRamStart = PcdGet32(PcdTemporaryRamBase);
99 BootLoaderTempRamSize = PcdGet32(PcdTemporaryRamSize) - PcdGet32(PcdFspTemporaryRamSize);
100 BootLoaderTempRamEnd = BootLoaderTempRamStart + BootLoaderTempRamSize;
101
102 //
103 // Build a Boot Loader Temporary Memory GUID HOB
104 //
105 if (ApiMode == 0) {
106 BootLoaderTempRamHob = BuildGuidHob (&gFspBootLoaderTemporaryMemoryGuid, BootLoaderTempRamSize);
107 } else {
108 BootLoaderTempRamHob = (VOID *)AllocatePool (BootLoaderTempRamSize);
109 }
110 ASSERT(BootLoaderTempRamHob != NULL);
111
112 CopyMem (BootLoaderTempRamHob, (VOID *)BootLoaderTempRamStart, BootLoaderTempRamSize);
113 OffsetGap = (UINT32)BootLoaderTempRamHob - BootLoaderTempRamStart;
114
115 //
116 // Set a new stack frame for the continuation function
117 //
118 if (ApiMode == 0) {
119 FspInitParams = (FSP_INIT_PARAMS *)GetFspApiParameter ();
120 FspInitRtBuffer = (FSP_INIT_RT_COMMON_BUFFER *)FspInitParams->RtBufferPtr;
121 NewStackTop = (UINT32 *)FspInitRtBuffer->StackTop - 1;
122 SetFspCoreStackPointer (NewStackTop);
123 }
124
125 //
126 // Fix the FspInit Parameter Pointers to the new location.
127 //
128 FspParamPtr = GetFspApiParameter ();
129 if (FspParamPtr >= BootLoaderTempRamStart && FspParamPtr < BootLoaderTempRamEnd) {
130 SetFspApiParameter(FspParamPtr + OffsetGap);
131 }
132
133 FspInitParams = (FSP_INIT_PARAMS *)GetFspApiParameter ();
134 if ((UINT32)(FspInitParams->RtBufferPtr) >= BootLoaderTempRamStart &&
135 (UINT32)(FspInitParams->RtBufferPtr) < BootLoaderTempRamEnd) {
136 FspInitParams->RtBufferPtr = (VOID *)((UINT32)(FspInitParams->RtBufferPtr) + OffsetGap);
137 }
138
139 if ((UINT32)(FspInitParams->NvsBufferPtr) >= BootLoaderTempRamStart &&
140 (UINT32)(FspInitParams->NvsBufferPtr) < BootLoaderTempRamEnd) {
141 FspInitParams->NvsBufferPtr = (VOID *)((UINT32)(FspInitParams->NvsBufferPtr) + OffsetGap);
142 }
143
144 if ((UINT32)(((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr) >= BootLoaderTempRamStart &&
145 (UINT32)(((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr) < BootLoaderTempRamEnd) {
146 ((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr = \
147 (VOID *)((UINT32)(((FSP_INIT_RT_COMMON_BUFFER *)(FspInitParams->RtBufferPtr))->UpdDataRgnPtr) + OffsetGap);
148 }
149
150 //
151 // Update UPD pointer in FSP Global Data
152 //
153 UpdDataRgnPtr = ((FSP_INIT_RT_COMMON_BUFFER *)FspInitParams->RtBufferPtr)->UpdDataRgnPtr;
154 if (UpdDataRgnPtr != NULL) {
155 SetFspUpdDataPointer (UpdDataRgnPtr);
156 }
157
158 //
159 // Update Platform data pointer in FSP Global Data
160 //
161 PlatformDataPtr = GetFspPlatformDataPointer ();
162 if (((UINT32)PlatformDataPtr >= BootLoaderTempRamStart) &&
163 ((UINT32)PlatformDataPtr < BootLoaderTempRamEnd)) {
164 SetFspPlatformDataPointer ((UINT8 *)PlatformDataPtr + OffsetGap);
165 }
166 }