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