]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/PrePi/PrePi.c
ArmPkg: Create MpCoreInfo PPI and HOB to describe CPU Cores on a MPCore platform
[mirror_edk2.git] / ArmPlatformPkg / PrePi / PrePi.c
1 /** @file
2 *
3 * Copyright (c) 2011, 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/BaseMemoryLib.h>
19 #include <Library/PrePiLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/PeCoffGetEntryPointLib.h>
23 #include <Library/PrePiHobListPointerLib.h>
24 #include <Library/TimerLib.h>
25 #include <Library/PerformanceLib.h>
26
27 #include <Ppi/GuidedSectionExtraction.h>
28 #include <Guid/LzmaDecompress.h>
29 #include <Guid/ArmGlobalVariableHob.h>
30
31 #include "PrePi.h"
32 #include "LzmaDecompress.h"
33
34 #define IS_XIP() ((FixedPcdGet32 (PcdFdBaseAddress) > (FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize))) || \
35 ((FixedPcdGet32 (PcdFdBaseAddress) + FixedPcdGet32 (PcdFdSize)) < FixedPcdGet32 (PcdSystemMemoryBase)))
36
37 // Not used when PrePi in run in XIP mode
38 UINTN mGlobalVariableBase = 0;
39
40 VOID
41 PrePiCommonExceptionEntry (
42 IN UINT32 Entry,
43 IN UINT32 LR
44 );
45
46 EFI_STATUS
47 EFIAPI
48 ExtractGuidedSectionLibConstructor (
49 VOID
50 );
51
52 EFI_STATUS
53 EFIAPI
54 LzmaDecompressLibConstructor (
55 VOID
56 );
57
58 VOID
59 EFIAPI
60 BuildGlobalVariableHob (
61 IN EFI_PHYSICAL_ADDRESS GlobalVariableBase,
62 IN UINT32 GlobalVariableSize
63 )
64 {
65 ARM_HOB_GLOBAL_VARIABLE *Hob;
66
67 Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, sizeof (ARM_HOB_GLOBAL_VARIABLE));
68 ASSERT(Hob != NULL);
69
70 CopyGuid (&(Hob->Header.Name), &gArmGlobalVariableGuid);
71 Hob->GlobalVariableBase = GlobalVariableBase;
72 Hob->GlobalVariableSize = GlobalVariableSize;
73 }
74
75 VOID
76 PrePiMain (
77 IN UINTN UefiMemoryBase,
78 IN UINTN StacksBase,
79 IN UINTN GlobalVariableBase,
80 IN UINT64 StartTimeStamp
81 )
82 {
83 EFI_HOB_HANDOFF_INFO_TABLE* HobList;
84 EFI_STATUS Status;
85 CHAR8 Buffer[100];
86 UINTN CharCount;
87 UINTN StacksSize;
88
89 // If ensure the FD is either part of the System Memory or totally outside of the System Memory (XIP)
90 ASSERT (IS_XIP() ||
91 ((FixedPcdGet32 (PcdFdBaseAddress) >= FixedPcdGet32 (PcdSystemMemoryBase)) &&
92 ((FixedPcdGet32 (PcdFdBaseAddress) + FixedPcdGet32 (PcdFdSize)) <= (FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize)))));
93
94 // Enable program flow prediction, if supported.
95 ArmEnableBranchPrediction ();
96
97 if (FixedPcdGet32(PcdVFPEnabled)) {
98 ArmEnableVFP();
99 }
100
101 // Initialize the Serial Port
102 SerialPortInitialize ();
103 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"UEFI firmware built at %a on %a\n\r",__TIME__, __DATE__);
104 SerialPortWrite ((UINT8 *) Buffer, CharCount);
105
106 // Initialize the Debug Agent for Source Level Debugging
107 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);
108 SaveAndSetDebugTimerInterrupt (TRUE);
109
110 if (!IS_XIP()) {
111 mGlobalVariableBase = GlobalVariableBase;
112 }
113
114 // Declare the PI/UEFI memory region
115 HobList = HobConstructor (
116 (VOID*)UefiMemoryBase,
117 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize),
118 (VOID*)UefiMemoryBase,
119 (VOID*)StacksBase // The top of the UEFI Memory is reserved for the stacks
120 );
121 PrePeiSetHobList (HobList);
122
123 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
124 Status = MemoryPeim (UefiMemoryBase, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize));
125 ASSERT_EFI_ERROR (Status);
126
127 // Create the Stacks HOB (reserve the memory for all stacks)
128 StacksSize = PcdGet32 (PcdCPUCorePrimaryStackSize) + (FixedPcdGet32(PcdClusterCount) * 4 * FixedPcdGet32(PcdCPUCoreSecondaryStackSize));
129 BuildStackHob (StacksBase, StacksSize);
130
131 // Declare the Global Variable HOB
132 BuildGlobalVariableHob (GlobalVariableBase, FixedPcdGet32 (PcdPeiGlobalVariableSize));
133
134 //TODO: Call CpuPei as a library
135 BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize), PcdGet8 (PcdPrePiCpuIoSize));
136
137 // Set the Boot Mode
138 SetBootMode (ArmPlatformGetBootMode ());
139
140 // Initialize Platform HOBs (CpuHob and FvHob)
141 Status = PlatformPeim ();
142 ASSERT_EFI_ERROR (Status);
143
144 BuildMemoryTypeInformationHob ();
145
146 // Now, the HOB List has been initialized, we can register performance information
147 PERF_START (NULL, "PEI", NULL, StartTimeStamp);
148
149 // SEC phase needs to run library constructors by hand.
150 ExtractGuidedSectionLibConstructor ();
151 LzmaDecompressLibConstructor ();
152
153 // Build HOBs to pass up our version of stuff the DXE Core needs to save space
154 BuildPeCoffLoaderHob ();
155 BuildExtractSectionHob (
156 &gLzmaCustomDecompressGuid,
157 LzmaGuidedSectionGetInfo,
158 LzmaGuidedSectionExtraction
159 );
160
161 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
162 Status = DecompressFirstFv ();
163 ASSERT_EFI_ERROR (Status);
164
165 // Load the DXE Core and transfer control to it
166 Status = LoadDxeCoreFromFv (NULL, 0);
167 ASSERT_EFI_ERROR (Status);
168 }
169
170 VOID
171 CEntryPoint (
172 IN UINTN MpId,
173 IN UINTN UefiMemoryBase,
174 IN UINTN StacksBase,
175 IN UINTN GlobalVariableBase
176 )
177 {
178 UINT64 StartTimeStamp;
179
180 if (IS_PRIMARY_CORE(MpId) && PerformanceMeasurementEnabled ()) {
181 // Initialize the Timer Library to setup the Timer HW controller
182 TimerConstructor ();
183 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
184 StartTimeStamp = GetPerformanceCounter ();
185 } else {
186 StartTimeStamp = 0;
187 }
188
189 // Clean Data cache
190 ArmCleanInvalidateDataCache ();
191
192 // Invalidate instruction cache
193 ArmInvalidateInstructionCache ();
194
195 //TODO:Drain Write Buffer
196
197 // Enable Instruction & Data caches
198 ArmEnableDataCache ();
199 ArmEnableInstructionCache ();
200
201 // Write VBAR - The Vector table must be 32-byte aligned
202 ASSERT (((UINT32)PrePiVectorTable & ((1 << 5)-1)) == 0);
203 ArmWriteVBar ((UINT32)PrePiVectorTable);
204
205 // If not primary Jump to Secondary Main
206 if (IS_PRIMARY_CORE(MpId)) {
207 // Goto primary Main.
208 PrimaryMain (UefiMemoryBase, StacksBase, GlobalVariableBase, StartTimeStamp);
209 } else {
210 SecondaryMain (MpId);
211 }
212
213 // DXE Core should always load and never return
214 ASSERT (FALSE);
215 }
216
217 VOID
218 PrePiCommonExceptionEntry (
219 IN UINT32 Entry,
220 IN UINT32 LR
221 )
222 {
223 CHAR8 Buffer[100];
224 UINTN CharCount;
225
226 switch (Entry) {
227 case 0:
228 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Reset Exception at 0x%X\n\r",LR);
229 break;
230 case 1:
231 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Undefined Exception at 0x%X\n\r",LR);
232 break;
233 case 2:
234 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"SWI Exception at 0x%X\n\r",LR);
235 break;
236 case 3:
237 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"PrefetchAbort Exception at 0x%X\n\r",LR);
238 break;
239 case 4:
240 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"DataAbort Exception at 0x%X\n\r",LR);
241 break;
242 case 5:
243 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Reserved Exception at 0x%X\n\r",LR);
244 break;
245 case 6:
246 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"IRQ Exception at 0x%X\n\r",LR);
247 break;
248 case 7:
249 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"FIQ Exception at 0x%X\n\r",LR);
250 break;
251 default:
252 CharCount = AsciiSPrint (Buffer,sizeof (Buffer),"Unknown Exception at 0x%X\n\r",LR);
253 break;
254 }
255 SerialPortWrite ((UINT8 *) Buffer, CharCount);
256 while(1);
257 }