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/AArch64/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>
26 #include "StandaloneMmCpu.h"
28 // GUID to identify HOB with whereabouts of communication buffer with Normal
30 extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid
;
32 // GUID to identify HOB where the entry point of this CPU driver will be
33 // populated to allow the entry point driver to invoke it upon receipt of an
35 extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid
;
38 // Private copy of the MM system table for future use
40 EFI_MM_SYSTEM_TABLE
*mMmst
= NULL
;
43 // Globals used to initialize the protocol
45 STATIC EFI_HANDLE mMmCpuHandle
= NULL
;
50 IN CONST EFI_GUID
*HobGuid
,
54 EFI_HOB_GUID_TYPE
*Hob
;
56 if ((HobList
== NULL
) || (HobGuid
== NULL
) || (HobData
== NULL
)) {
57 return EFI_INVALID_PARAMETER
;
60 Hob
= GetNextGuidHob (HobGuid
, HobList
);
65 *HobData
= GET_GUID_HOB_DATA (Hob
);
66 if (*HobData
== NULL
) {
74 StandaloneMmCpuInitialize (
75 IN EFI_HANDLE ImageHandle
, // not actual imagehandle
76 IN EFI_MM_SYSTEM_TABLE
*SystemTable
// not actual systemtable
79 ARM_TF_CPU_DRIVER_EP_DESCRIPTOR
*CpuDriverEntryPointDesc
;
80 EFI_CONFIGURATION_TABLE
*ConfigurationTable
;
81 MP_INFORMATION_HOB_DATA
*MpInformationHobData
;
82 EFI_MMRAM_DESCRIPTOR
*NsCommBufMmramRange
;
84 EFI_HANDLE DispatchHandle
;
90 ASSERT (SystemTable
!= NULL
);
93 // publish the MM config protocol so the MM core can register its entry point
94 Status
= mMmst
->MmInstallProtocolInterface (
96 &gEfiMmConfigurationProtocolGuid
,
100 if (EFI_ERROR (Status
)) {
104 // register the root MMI handler
105 Status
= mMmst
->MmiHandlerRegister (
106 PiMmCpuTpFwRootMmiHandler
,
110 if (EFI_ERROR (Status
)) {
114 // Retrieve the Hoblist from the MMST to extract the details of the NS
115 // communication buffer that has been reserved by S-EL1/EL3
116 ConfigurationTable
= mMmst
->MmConfigurationTable
;
117 for (Index
= 0; Index
< mMmst
->NumberOfTableEntries
; Index
++) {
118 if (CompareGuid (&gEfiHobListGuid
, &(ConfigurationTable
[Index
].VendorGuid
))) {
123 // Bail out if the Hoblist could not be found
124 if (Index
>= mMmst
->NumberOfTableEntries
) {
125 DEBUG ((DEBUG_INFO
, "Hoblist not found - 0x%x\n", Index
));
126 return EFI_OUT_OF_RESOURCES
;
129 HobStart
= ConfigurationTable
[Index
].VendorTable
;
132 // Locate the HOB with the buffer to populate the entry point of this driver
134 Status
= GetGuidedHobData (
136 &gEfiArmTfCpuDriverEpDescriptorGuid
,
137 (VOID
**) &CpuDriverEntryPointDesc
139 if (EFI_ERROR (Status
)) {
140 DEBUG ((DEBUG_INFO
, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status
));
144 // Share the entry point of the CPU driver
145 DEBUG ((DEBUG_INFO
, "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",
146 (UINT64
) CpuDriverEntryPointDesc
->ArmTfCpuDriverEpPtr
,
147 (UINT64
) PiMmStandaloneArmTfCpuDriverEntry
));
148 *(CpuDriverEntryPointDesc
->ArmTfCpuDriverEpPtr
) = PiMmStandaloneArmTfCpuDriverEntry
;
150 // Find the descriptor that contains the whereabouts of the buffer for
151 // communication with the Normal world.
152 Status
= GetGuidedHobData (
154 &gEfiStandaloneMmNonSecureBufferGuid
,
155 (VOID
**) &NsCommBufMmramRange
157 if (EFI_ERROR (Status
)) {
158 DEBUG ((DEBUG_INFO
, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status
));
162 DEBUG ((DEBUG_INFO
, "mNsCommBuffer.PhysicalStart - 0x%lx\n", (UINT64
) NsCommBufMmramRange
->PhysicalStart
));
163 DEBUG ((DEBUG_INFO
, "mNsCommBuffer.PhysicalSize - 0x%lx\n", (UINT64
) NsCommBufMmramRange
->PhysicalSize
));
165 CopyMem (&mNsCommBuffer
, NsCommBufMmramRange
, sizeof(EFI_MMRAM_DESCRIPTOR
));
166 DEBUG ((DEBUG_INFO
, "mNsCommBuffer: 0x%016lx - 0x%lx\n", mNsCommBuffer
.CpuStart
, mNsCommBuffer
.PhysicalSize
));
169 // Extract the MP information from the Hoblist
171 Status
= GetGuidedHobData (
173 &gMpInformationHobGuid
,
174 (VOID
**) &MpInformationHobData
176 if (EFI_ERROR (Status
)) {
177 DEBUG ((DEBUG_INFO
, "MpInformationHob extraction failed - 0x%x\n", Status
));
182 // Allocate memory for the MP information and copy over the MP information
183 // passed by Trusted Firmware. Use the number of processors passed in the HOB
184 // to copy the processor information
186 MpInfoSize
= sizeof (MP_INFORMATION_HOB_DATA
) +
187 (sizeof (EFI_PROCESSOR_INFORMATION
) *
188 MpInformationHobData
->NumberOfProcessors
);
189 Status
= mMmst
->MmAllocatePool (
190 EfiRuntimeServicesData
,
192 (VOID
**) &mMpInformationHobData
194 if (EFI_ERROR (Status
)) {
195 DEBUG ((DEBUG_INFO
, "mMpInformationHobData mem alloc failed - 0x%x\n", Status
));
199 CopyMem (mMpInformationHobData
, MpInformationHobData
, MpInfoSize
);
201 // Print MP information
202 DEBUG ((DEBUG_INFO
, "mMpInformationHobData: 0x%016lx - 0x%lx\n",
203 mMpInformationHobData
->NumberOfProcessors
,
204 mMpInformationHobData
->NumberOfEnabledProcessors
));
205 for (Index
= 0; Index
< mMpInformationHobData
->NumberOfProcessors
; Index
++) {
206 DEBUG ((DEBUG_INFO
, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",
207 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].ProcessorId
,
208 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].Location
.Package
,
209 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].Location
.Core
,
210 mMpInformationHobData
->ProcessorInfoBuffer
[Index
].Location
.Thread
));
214 // Allocate memory for a table to hold pointers to a
215 // EFI_MM_COMMUNICATE_HEADER for each CPU
217 ArraySize
= sizeof (EFI_MM_COMMUNICATE_HEADER
*) *
218 mMpInformationHobData
->NumberOfEnabledProcessors
;
219 Status
= mMmst
->MmAllocatePool (
220 EfiRuntimeServicesData
,
222 (VOID
**) &PerCpuGuidedEventContext
224 if (EFI_ERROR (Status
)) {
225 DEBUG ((DEBUG_INFO
, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status
));