]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/PrePi/PrePi.c
ArmPlatformPkg/ArmPlatformLib: Added support for ArmPlatformIsPrimaryCore()
[mirror_edk2.git] / ArmPlatformPkg / PrePi / PrePi.c
1 /** @file
2 *
3 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
4 *
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
9 *
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.
12 *
13 **/
14
15 #include <PiPei.h>
16
17 #include <Library/DebugAgentLib.h>
18 #include <Library/PrePiLib.h>
19 #include <Library/PrintLib.h>
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/PrePiHobListPointerLib.h>
22 #include <Library/TimerLib.h>
23 #include <Library/PerformanceLib.h>
24
25 #include <Ppi/GuidedSectionExtraction.h>
26 #include <Ppi/ArmMpCoreInfo.h>
27 #include <Guid/LzmaDecompress.h>
28 #include <Guid/ArmGlobalVariableHob.h>
29
30 #include "PrePi.h"
31 #include "LzmaDecompress.h"
32
33 #define IS_XIP() (((UINT32)FixedPcdGet32 (PcdFdBaseAddress) > (UINT32)(FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize))) || \
34 ((FixedPcdGet32 (PcdFdBaseAddress) + FixedPcdGet32 (PcdFdSize)) < FixedPcdGet32 (PcdSystemMemoryBase)))
35
36 // Not used when PrePi in run in XIP mode
37 UINTN mGlobalVariableBase = 0;
38
39 EFI_STATUS
40 EFIAPI
41 ExtractGuidedSectionLibConstructor (
42 VOID
43 );
44
45 EFI_STATUS
46 EFIAPI
47 LzmaDecompressLibConstructor (
48 VOID
49 );
50
51 VOID
52 EFIAPI
53 BuildGlobalVariableHob (
54 IN EFI_PHYSICAL_ADDRESS GlobalVariableBase,
55 IN UINT32 GlobalVariableSize
56 )
57 {
58 ARM_HOB_GLOBAL_VARIABLE *Hob;
59
60 Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, sizeof (ARM_HOB_GLOBAL_VARIABLE));
61 ASSERT(Hob != NULL);
62
63 CopyGuid (&(Hob->Header.Name), &gArmGlobalVariableGuid);
64 Hob->GlobalVariableBase = GlobalVariableBase;
65 Hob->GlobalVariableSize = GlobalVariableSize;
66 }
67
68 EFI_STATUS
69 GetPlatformPpi (
70 IN EFI_GUID *PpiGuid,
71 OUT VOID **Ppi
72 )
73 {
74 UINTN PpiListSize;
75 UINTN PpiListCount;
76 EFI_PEI_PPI_DESCRIPTOR *PpiList;
77 UINTN Index;
78
79 PpiListSize = 0;
80 ArmPlatformGetPlatformPpiList (&PpiListSize, &PpiList);
81 PpiListCount = PpiListSize / sizeof(EFI_PEI_PPI_DESCRIPTOR);
82 for (Index = 0; Index < PpiListCount; Index++, PpiList++) {
83 if (CompareGuid (PpiList->Guid, PpiGuid) == TRUE) {
84 *Ppi = PpiList->Ppi;
85 return EFI_SUCCESS;
86 }
87 }
88
89 return EFI_NOT_FOUND;
90 }
91
92 VOID
93 PrePiMain (
94 IN UINTN UefiMemoryBase,
95 IN UINTN StacksBase,
96 IN UINTN GlobalVariableBase,
97 IN UINT64 StartTimeStamp
98 )
99 {
100 EFI_HOB_HANDOFF_INFO_TABLE* HobList;
101 ARM_MP_CORE_INFO_PPI* ArmMpCoreInfoPpi;
102 UINTN ArmCoreCount;
103 ARM_CORE_INFO* ArmCoreInfoTable;
104 EFI_STATUS Status;
105 CHAR8 Buffer[100];
106 UINTN CharCount;
107 UINTN StacksSize;
108
109 // If ensure the FD is either part of the System Memory or totally outside of the System Memory (XIP)
110 ASSERT (IS_XIP() ||
111 ((FixedPcdGet32 (PcdFdBaseAddress) >= FixedPcdGet32 (PcdSystemMemoryBase)) &&
112 ((UINT32)(FixedPcdGet32 (PcdFdBaseAddress) + FixedPcdGet32 (PcdFdSize)) <= (UINT32)(FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize)))));
113
114 // Enable program flow prediction, if supported.
115 ArmEnableBranchPrediction ();
116
117 if (FixedPcdGet32(PcdVFPEnabled)) {
118 ArmEnableVFP();
119 }
120
121 // Initialize the Serial Port
122 SerialPortInitialize ();
123 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"UEFI firmware (version %s built at %a on %a)\n\r",
124 (CHAR16*)PcdGetPtr(PcdFirmwareVersionString), __TIME__, __DATE__);
125 SerialPortWrite ((UINT8 *) Buffer, CharCount);
126
127 // Initialize the Debug Agent for Source Level Debugging
128 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);
129 SaveAndSetDebugTimerInterrupt (TRUE);
130
131 // Declare the PI/UEFI memory region
132 HobList = HobConstructor (
133 (VOID*)UefiMemoryBase,
134 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize),
135 (VOID*)UefiMemoryBase,
136 (VOID*)StacksBase // The top of the UEFI Memory is reserved for the stacks
137 );
138 PrePeiSetHobList (HobList);
139
140 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
141 Status = MemoryPeim (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
142 ASSERT_EFI_ERROR (Status);
143
144 // Create the Stacks HOB (reserve the memory for all stacks)
145 if (ArmIsMpCore ()) {
146 StacksSize = PcdGet32 (PcdCPUCorePrimaryStackSize) + (FixedPcdGet32(PcdClusterCount) * 4 * FixedPcdGet32(PcdCPUCoreSecondaryStackSize));
147 } else {
148 StacksSize = PcdGet32 (PcdCPUCorePrimaryStackSize);
149 }
150 BuildStackHob (StacksBase, StacksSize);
151
152 // Declare the Global Variable HOB
153 BuildGlobalVariableHob (GlobalVariableBase, FixedPcdGet32 (PcdPeiGlobalVariableSize));
154
155 //TODO: Call CpuPei as a library
156 BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize), PcdGet8 (PcdPrePiCpuIoSize));
157
158 if (ArmIsMpCore ()) {
159 // Only MP Core platform need to produce gArmMpCoreInfoPpiGuid
160 Status = GetPlatformPpi (&gArmMpCoreInfoPpiGuid, (VOID**)&ArmMpCoreInfoPpi);
161
162 // On MP Core Platform we must implement the ARM MP Core Info PPI (gArmMpCoreInfoPpiGuid)
163 ASSERT_EFI_ERROR (Status);
164
165 // Build the MP Core Info Table
166 ArmCoreCount = 0;
167 Status = ArmMpCoreInfoPpi->GetMpCoreInfo (&ArmCoreCount, &ArmCoreInfoTable);
168 if (!EFI_ERROR(Status) && (ArmCoreCount > 0)) {
169 // Build MPCore Info HOB
170 BuildGuidDataHob (&gArmMpCoreInfoGuid, ArmCoreInfoTable, sizeof (ARM_CORE_INFO) * ArmCoreCount);
171 }
172 }
173
174 // Set the Boot Mode
175 SetBootMode (ArmPlatformGetBootMode ());
176
177 // Initialize Platform HOBs (CpuHob and FvHob)
178 Status = PlatformPeim ();
179 ASSERT_EFI_ERROR (Status);
180
181 // Now, the HOB List has been initialized, we can register performance information
182 PERF_START (NULL, "PEI", NULL, StartTimeStamp);
183
184 // SEC phase needs to run library constructors by hand.
185 ExtractGuidedSectionLibConstructor ();
186 LzmaDecompressLibConstructor ();
187
188 // Build HOBs to pass up our version of stuff the DXE Core needs to save space
189 BuildPeCoffLoaderHob ();
190 BuildExtractSectionHob (
191 &gLzmaCustomDecompressGuid,
192 LzmaGuidedSectionGetInfo,
193 LzmaGuidedSectionExtraction
194 );
195
196 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
197 Status = DecompressFirstFv ();
198 ASSERT_EFI_ERROR (Status);
199
200 // Load the DXE Core and transfer control to it
201 Status = LoadDxeCoreFromFv (NULL, 0);
202 ASSERT_EFI_ERROR (Status);
203 }
204
205 VOID
206 CEntryPoint (
207 IN UINTN MpId,
208 IN UINTN UefiMemoryBase,
209 IN UINTN StacksBase,
210 IN UINTN GlobalVariableBase
211 )
212 {
213 UINT64 StartTimeStamp;
214
215 ASSERT(!ArmIsMpCore() || (PcdGet32 (PcdCoreCount) > 1));
216
217 // Initialize the platform specific controllers
218 ArmPlatformInitialize (MpId);
219
220 if (ArmPlatformIsPrimaryCore (MpId) && PerformanceMeasurementEnabled ()) {
221 // Initialize the Timer Library to setup the Timer HW controller
222 TimerConstructor ();
223 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
224 StartTimeStamp = GetPerformanceCounter ();
225 } else {
226 StartTimeStamp = 0;
227 }
228
229 // Clean Data cache
230 ArmCleanInvalidateDataCache ();
231
232 // Invalidate instruction cache
233 ArmInvalidateInstructionCache ();
234
235 //TODO:Drain Write Buffer
236
237 // Enable Instruction & Data caches
238 ArmEnableDataCache ();
239 ArmEnableInstructionCache ();
240
241 // Define the Global Variable region when we are not running in XIP
242 if (!IS_XIP()) {
243 if (ArmPlatformIsPrimaryCore (MpId)) {
244 mGlobalVariableBase = GlobalVariableBase;
245 if (ArmIsMpCore()) {
246 // Signal the Global Variable Region is defined (event: ARM_CPU_EVENT_DEFAULT)
247 ArmCallSEV ();
248 }
249 } else {
250 // Wait the Primay core has defined the address of the Global Variable region (event: ARM_CPU_EVENT_DEFAULT)
251 ArmCallWFE ();
252 }
253 }
254
255 // If not primary Jump to Secondary Main
256 if (ArmPlatformIsPrimaryCore (MpId)) {
257 // Goto primary Main.
258 PrimaryMain (UefiMemoryBase, StacksBase, GlobalVariableBase, StartTimeStamp);
259 } else {
260 SecondaryMain (MpId);
261 }
262
263 // DXE Core should always load and never return
264 ASSERT (FALSE);
265 }
266