3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2016 HP Development Company, L.P.
5 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Pi/PiMmCis.h>
13 #include <Library/Arm/StandaloneMmCoreEntryPoint.h>
14 #include <Library/DebugLib.h>
15 #include <Library/ArmSvcLib.h>
16 #include <Library/ArmLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/HobLib.h>
20 #include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
22 #include <Guid/ZeroGuid.h>
23 #include <Guid/MmramMemoryReserve.h>
25 #include "StandaloneMmCpu.h"
27 // GUID to identify HOB with whereabouts of communication buffer with Normal
29 extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid
;
31 // GUID to identify HOB where the entry point of this CPU driver will be
32 // populated to allow the entry point driver to invoke it upon receipt of an
34 extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid
;
37 // Private copy of the MM system table for future use
39 EFI_MM_SYSTEM_TABLE
*mMmst
= NULL
;
42 // Globals used to initialize the protocol
44 STATIC EFI_HANDLE mMmCpuHandle
= NULL
;
46 /** Returns the HOB data for the matching HOB GUID.
48 @param [in] HobList Pointer to the HOB list.
49 @param [in] HobGuid The GUID for the HOB.
50 @param [out] HobData Pointer to the HOB data.
52 @retval EFI_SUCCESS The function completed successfully.
53 @retval EFI_INVALID_PARAMETER Invalid parameter.
54 @retval EFI_NOT_FOUND Could not find HOB with matching GUID.
59 IN CONST EFI_GUID
*HobGuid
,
63 EFI_HOB_GUID_TYPE
*Hob
;
65 if ((HobList
== NULL
) || (HobGuid
== NULL
) || (HobData
== NULL
)) {
66 return EFI_INVALID_PARAMETER
;
69 Hob
= GetNextGuidHob (HobGuid
, HobList
);
74 *HobData
= GET_GUID_HOB_DATA (Hob
);
75 if (*HobData
== NULL
) {
82 /** Entry point for the Standalone MM CPU driver.
84 @param [in] ImageHandle Unused. Not actual image handle.
85 @param [in] SystemTable Pointer to MM System table.
87 @retval EFI_SUCCESS The function completed successfully.
88 @retval EFI_INVALID_PARAMETER Invalid parameter.
89 @retval EFI_OUT_OF_RESOURCES Out of resources.
90 @retval EFI_NOT_FOUND Failed to find the HOB for the CPU
91 driver endpoint descriptor.
94 StandaloneMmCpuInitialize (
95 IN EFI_HANDLE ImageHandle
, // not actual imagehandle
96 IN EFI_MM_SYSTEM_TABLE
*SystemTable
// not actual systemtable
99 ARM_TF_CPU_DRIVER_EP_DESCRIPTOR
*CpuDriverEntryPointDesc
;
100 EFI_CONFIGURATION_TABLE
*ConfigurationTable
;
101 MP_INFORMATION_HOB_DATA
*MpInformationHobData
;
102 EFI_MMRAM_DESCRIPTOR
*NsCommBufMmramRange
;
104 EFI_HANDLE DispatchHandle
;
110 ASSERT (SystemTable
!= NULL
);
113 // publish the MM config protocol so the MM core can register its entry point
114 Status
= mMmst
->MmInstallProtocolInterface (
116 &gEfiMmConfigurationProtocolGuid
,
117 EFI_NATIVE_INTERFACE
,
120 if (EFI_ERROR (Status
)) {
124 // register the root MMI handler
125 Status
= mMmst
->MmiHandlerRegister (
126 PiMmCpuTpFwRootMmiHandler
,
130 if (EFI_ERROR (Status
)) {
134 // Retrieve the Hoblist from the MMST to extract the details of the NS
135 // communication buffer that has been reserved by S-EL1/EL3
136 ConfigurationTable
= mMmst
->MmConfigurationTable
;
137 for (Index
= 0; Index
< mMmst
->NumberOfTableEntries
; Index
++) {
138 if (CompareGuid (&gEfiHobListGuid
, &(ConfigurationTable
[Index
].VendorGuid
))) {
143 // Bail out if the Hoblist could not be found
144 if (Index
>= mMmst
->NumberOfTableEntries
) {
145 DEBUG ((DEBUG_INFO
, "Hoblist not found - 0x%x\n", Index
));
146 return EFI_OUT_OF_RESOURCES
;
149 HobStart
= ConfigurationTable
[Index
].VendorTable
;
152 // Locate the HOB with the buffer to populate the entry point of this driver
154 Status
= GetGuidedHobData (
156 &gEfiArmTfCpuDriverEpDescriptorGuid
,
157 (VOID
**)&CpuDriverEntryPointDesc
159 if (EFI_ERROR (Status
)) {
160 DEBUG ((DEBUG_INFO
, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status
));
164 // Share the entry point of the CPU driver
167 "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
168 (UINTN
)CpuDriverEntryPointDesc
->ArmTfCpuDriverEpPtr
,
169 (UINTN
)PiMmStandaloneArmTfCpuDriverEntry
171 *(CpuDriverEntryPointDesc
->ArmTfCpuDriverEpPtr
) = PiMmStandaloneArmTfCpuDriverEntry
;
173 // Find the descriptor that contains the whereabouts of the buffer for
174 // communication with the Normal world.
175 Status
= GetGuidedHobData (
177 &gEfiStandaloneMmNonSecureBufferGuid
,
178 (VOID
**)&NsCommBufMmramRange
180 if (EFI_ERROR (Status
)) {
181 DEBUG ((DEBUG_INFO
, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status
));
185 DEBUG ((DEBUG_INFO
, "mNsCommBuffer.PhysicalStart - 0x%lx\n", (UINTN
)NsCommBufMmramRange
->PhysicalStart
));
186 DEBUG ((DEBUG_INFO
, "mNsCommBuffer.PhysicalSize - 0x%lx\n", (UINTN
)NsCommBufMmramRange
->PhysicalSize
));
188 CopyMem (&mNsCommBuffer
, NsCommBufMmramRange
, sizeof (EFI_MMRAM_DESCRIPTOR
));
189 DEBUG ((DEBUG_INFO
, "mNsCommBuffer: 0x%016lx - 0x%lx\n", mNsCommBuffer
.CpuStart
, mNsCommBuffer
.PhysicalSize
));
192 // Extract the MP information from the Hoblist
194 Status
= GetGuidedHobData (
196 &gMpInformationHobGuid
,
197 (VOID
**)&MpInformationHobData
199 if (EFI_ERROR (Status
)) {
200 DEBUG ((DEBUG_INFO
, "MpInformationHob extraction failed - 0x%x\n", Status
));
205 // Allocate memory for the MP information and copy over the MP information
206 // passed by Trusted Firmware. Use the number of processors passed in the HOB
207 // to copy the processor information
209 MpInfoSize
= sizeof (MP_INFORMATION_HOB_DATA
) +
210 (sizeof (EFI_PROCESSOR_INFORMATION
) *
211 MpInformationHobData
->NumberOfProcessors
);
212 Status
= mMmst
->MmAllocatePool (
213 EfiRuntimeServicesData
,
215 (VOID
**)&mMpInformationHobData
217 if (EFI_ERROR (Status
)) {
218 DEBUG ((DEBUG_INFO
, "mMpInformationHobData mem alloc failed - 0x%x\n", Status
));
222 CopyMem (mMpInformationHobData
, MpInformationHobData
, MpInfoSize
);
224 // Print MP information
227 "mMpInformationHobData: 0x%016lx - 0x%lx\n",
228 mMpInformationHobData
->NumberOfProcessors
,
229 mMpInformationHobData
->NumberOfEnabledProcessors
231 for (Index
= 0; Index
< mMpInformationHobData
->NumberOfProcessors
; Index
++) {
234 "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
235 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].ProcessorId
,
236 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].Location
.Package
,
237 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].Location
.Core
,
238 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].Location
.Thread
243 // Allocate memory for a table to hold pointers to a
244 // EFI_MM_COMMUNICATE_HEADER for each CPU
246 ArraySize
= sizeof (EFI_MM_COMMUNICATE_HEADER
*) *
247 mMpInformationHobData
->NumberOfEnabledProcessors
;
248 Status
= mMmst
->MmAllocatePool (
249 EfiRuntimeServicesData
,
251 (VOID
**)&PerCpuGuidedEventContext
253 if (EFI_ERROR (Status
)) {
254 DEBUG ((DEBUG_INFO
, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status
));