3 * Copyright (c) 2011-2012, ARM Limited. All rights reserved.
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
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.
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>
26 #include <Ppi/GuidedSectionExtraction.h>
27 #include <Guid/LzmaDecompress.h>
28 #include <Guid/ArmGlobalVariableHob.h>
31 #include "LzmaDecompress.h"
33 #define IS_XIP() (((UINT32)FixedPcdGet32 (PcdFdBaseAddress) > (UINT32)(FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize))) || \
34 ((FixedPcdGet32 (PcdFdBaseAddress) + FixedPcdGet32 (PcdFdSize)) < FixedPcdGet32 (PcdSystemMemoryBase)))
36 // Not used when PrePi in run in XIP mode
37 UINTN mGlobalVariableBase
= 0;
40 PrePiCommonExceptionEntry (
47 ExtractGuidedSectionLibConstructor (
53 LzmaDecompressLibConstructor (
59 BuildGlobalVariableHob (
60 IN EFI_PHYSICAL_ADDRESS GlobalVariableBase
,
61 IN UINT32 GlobalVariableSize
64 ARM_HOB_GLOBAL_VARIABLE
*Hob
;
66 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, sizeof (ARM_HOB_GLOBAL_VARIABLE
));
69 CopyGuid (&(Hob
->Header
.Name
), &gArmGlobalVariableGuid
);
70 Hob
->GlobalVariableBase
= GlobalVariableBase
;
71 Hob
->GlobalVariableSize
= GlobalVariableSize
;
76 IN UINTN UefiMemoryBase
,
78 IN UINTN GlobalVariableBase
,
79 IN UINT64 StartTimeStamp
82 EFI_HOB_HANDOFF_INFO_TABLE
* HobList
;
88 // If ensure the FD is either part of the System Memory or totally outside of the System Memory (XIP)
90 ((FixedPcdGet32 (PcdFdBaseAddress
) >= FixedPcdGet32 (PcdSystemMemoryBase
)) &&
91 ((UINT32
)(FixedPcdGet32 (PcdFdBaseAddress
) + FixedPcdGet32 (PcdFdSize
)) <= (UINT32
)(FixedPcdGet32 (PcdSystemMemoryBase
) + FixedPcdGet32 (PcdSystemMemorySize
)))));
93 // Enable program flow prediction, if supported.
94 ArmEnableBranchPrediction ();
96 if (FixedPcdGet32(PcdVFPEnabled
)) {
100 // Initialize the Serial Port
101 SerialPortInitialize ();
102 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"UEFI firmware (version %s built at %a on %a)\n\r",
103 (CHAR16
*)PcdGetPtr(PcdFirmwareVersionString
), __TIME__
, __DATE__
);
104 SerialPortWrite ((UINT8
*) Buffer
, CharCount
);
106 // Initialize the Debug Agent for Source Level Debugging
107 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC
, NULL
, NULL
);
108 SaveAndSetDebugTimerInterrupt (TRUE
);
110 // Declare the PI/UEFI memory region
111 HobList
= HobConstructor (
112 (VOID
*)UefiMemoryBase
,
113 FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
),
114 (VOID
*)UefiMemoryBase
,
115 (VOID
*)StacksBase
// The top of the UEFI Memory is reserved for the stacks
117 PrePeiSetHobList (HobList
);
119 // Initialize MMU and Memory HOBs (Resource Descriptor HOBs)
120 Status
= MemoryPeim (UefiMemoryBase
, FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
));
121 ASSERT_EFI_ERROR (Status
);
123 // Create the Stacks HOB (reserve the memory for all stacks)
124 if (ArmIsMpCore ()) {
125 StacksSize
= PcdGet32 (PcdCPUCorePrimaryStackSize
) + (FixedPcdGet32(PcdClusterCount
) * 4 * FixedPcdGet32(PcdCPUCoreSecondaryStackSize
));
127 StacksSize
= PcdGet32 (PcdCPUCorePrimaryStackSize
);
129 BuildStackHob (StacksBase
, StacksSize
);
131 // Declare the Global Variable HOB
132 BuildGlobalVariableHob (GlobalVariableBase
, FixedPcdGet32 (PcdPeiGlobalVariableSize
));
134 //TODO: Call CpuPei as a library
135 BuildCpuHob (PcdGet8 (PcdPrePiCpuMemorySize
), PcdGet8 (PcdPrePiCpuIoSize
));
138 SetBootMode (ArmPlatformGetBootMode ());
140 // Initialize Platform HOBs (CpuHob and FvHob)
141 Status
= PlatformPeim ();
142 ASSERT_EFI_ERROR (Status
);
144 // Now, the HOB List has been initialized, we can register performance information
145 PERF_START (NULL
, "PEI", NULL
, StartTimeStamp
);
147 // SEC phase needs to run library constructors by hand.
148 ExtractGuidedSectionLibConstructor ();
149 LzmaDecompressLibConstructor ();
151 // Build HOBs to pass up our version of stuff the DXE Core needs to save space
152 BuildPeCoffLoaderHob ();
153 BuildExtractSectionHob (
154 &gLzmaCustomDecompressGuid
,
155 LzmaGuidedSectionGetInfo
,
156 LzmaGuidedSectionExtraction
159 // Assume the FV that contains the SEC (our code) also contains a compressed FV.
160 Status
= DecompressFirstFv ();
161 ASSERT_EFI_ERROR (Status
);
163 // Load the DXE Core and transfer control to it
164 Status
= LoadDxeCoreFromFv (NULL
, 0);
165 ASSERT_EFI_ERROR (Status
);
171 IN UINTN UefiMemoryBase
,
173 IN UINTN GlobalVariableBase
176 UINT64 StartTimeStamp
;
178 // Initialize the platform specific controllers
179 ArmPlatformInitialize (MpId
);
181 if (IS_PRIMARY_CORE(MpId
) && PerformanceMeasurementEnabled ()) {
182 // Initialize the Timer Library to setup the Timer HW controller
184 // We cannot call yet the PerformanceLib because the HOB List has not been initialized
185 StartTimeStamp
= GetPerformanceCounter ();
191 ArmCleanInvalidateDataCache ();
193 // Invalidate instruction cache
194 ArmInvalidateInstructionCache ();
196 //TODO:Drain Write Buffer
198 // Enable Instruction & Data caches
199 ArmEnableDataCache ();
200 ArmEnableInstructionCache ();
202 // Define the Global Variable region when we are not running in XIP
204 if (IS_PRIMARY_CORE(MpId
)) {
205 mGlobalVariableBase
= GlobalVariableBase
;
207 ArmCpuSynchronizeSignal (ARM_CPU_EVENT_DEFAULT
);
210 // Wait the Primay core has defined the address of the Global Variable region
211 ArmCpuSynchronizeWait (ARM_CPU_EVENT_DEFAULT
);
215 // Write VBAR - The Vector table must be 32-byte aligned
216 ASSERT (((UINT32
)PrePiVectorTable
& ((1 << 5)-1)) == 0);
217 ArmWriteVBar ((UINT32
)PrePiVectorTable
);
219 // If not primary Jump to Secondary Main
220 if (IS_PRIMARY_CORE(MpId
)) {
221 // Goto primary Main.
222 PrimaryMain (UefiMemoryBase
, StacksBase
, GlobalVariableBase
, StartTimeStamp
);
224 SecondaryMain (MpId
);
227 // DXE Core should always load and never return
232 PrePiCommonExceptionEntry (
242 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Reset Exception at 0x%X\n\r",LR
);
245 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Undefined Exception at 0x%X\n\r",LR
);
248 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"SWI Exception at 0x%X\n\r",LR
);
251 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"PrefetchAbort Exception at 0x%X\n\r",LR
);
254 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"DataAbort Exception at 0x%X\n\r",LR
);
257 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Reserved Exception at 0x%X\n\r",LR
);
260 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"IRQ Exception at 0x%X\n\r",LR
);
263 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"FIQ Exception at 0x%X\n\r",LR
);
266 CharCount
= AsciiSPrint (Buffer
,sizeof (Buffer
),"Unknown Exception at 0x%X\n\r",LR
);
269 SerialPortWrite ((UINT8
*) Buffer
, CharCount
);