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