--- /dev/null
+/** @file\r
+\r
+ Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
+ Copyright (c) 2016 HP Development Company, L.P.\r
+ Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.\r
+\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Pi/PiMmCis.h>\r
+#include <Library/AArch64/StandaloneMmCoreEntryPoint.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/ArmSvcLib.h>\r
+#include <Library/ArmLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/HobLib.h>\r
+\r
+#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT\r
+\r
+#include <Guid/ZeroGuid.h>\r
+#include <Guid/MmramMemoryReserve.h>\r
+\r
+\r
+#include "StandaloneMmCpu.h"\r
+\r
+// GUID to identify HOB with whereabouts of communication buffer with Normal\r
+// World\r
+extern EFI_GUID gEfiStandaloneMmNonSecureBufferGuid;\r
+\r
+// GUID to identify HOB where the entry point of this CPU driver will be\r
+// populated to allow the entry point driver to invoke it upon receipt of an\r
+// event\r
+extern EFI_GUID gEfiArmTfCpuDriverEpDescriptorGuid;\r
+\r
+//\r
+// Private copy of the MM system table for future use\r
+//\r
+EFI_MM_SYSTEM_TABLE *mMmst = NULL;\r
+\r
+//\r
+// Globals used to initialize the protocol\r
+//\r
+STATIC EFI_HANDLE mMmCpuHandle = NULL;\r
+\r
+EFI_STATUS\r
+GetGuidedHobData (\r
+ IN VOID *HobList,\r
+ IN CONST EFI_GUID *HobGuid,\r
+ OUT VOID **HobData\r
+ )\r
+{\r
+ EFI_HOB_GUID_TYPE *Hob;\r
+\r
+ if (!HobList || !HobGuid || !HobData)\r
+ return EFI_INVALID_PARAMETER;\r
+\r
+ Hob = GetNextGuidHob (HobGuid, HobList);\r
+ if (!Hob)\r
+ return EFI_NOT_FOUND;\r
+\r
+ *HobData = GET_GUID_HOB_DATA (Hob);\r
+ if (!HobData)\r
+ return EFI_NOT_FOUND;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+StandloneMmCpuInitialize (\r
+ IN EFI_HANDLE ImageHandle, // not actual imagehandle\r
+ IN EFI_MM_SYSTEM_TABLE *SystemTable // not actual systemtable\r
+ )\r
+{\r
+ ARM_TF_CPU_DRIVER_EP_DESCRIPTOR *CpuDriverEntryPointDesc;\r
+ EFI_CONFIGURATION_TABLE *ConfigurationTable;\r
+ MP_INFORMATION_HOB_DATA *MpInformationHobData;\r
+ EFI_MMRAM_DESCRIPTOR *NsCommBufMmramRange;\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE DispatchHandle;\r
+ UINT32 MpInfoSize;\r
+ UINTN Index;\r
+ UINTN ArraySize;\r
+ VOID *HobStart;\r
+\r
+ ASSERT (SystemTable != NULL);\r
+ mMmst = SystemTable;\r
+\r
+ // publish the MM config protocol so the MM core can register its entry point\r
+ Status = mMmst->MmInstallProtocolInterface (\r
+ &mMmCpuHandle,\r
+ &gEfiMmConfigurationProtocolGuid,\r
+ EFI_NATIVE_INTERFACE,\r
+ &mMmConfig\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ // register the root MMI handler\r
+ Status = mMmst->MmiHandlerRegister (\r
+ PiMmCpuTpFwRootMmiHandler,\r
+ NULL,\r
+ &DispatchHandle\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ // Retrieve the Hoblist from the MMST to extract the details of the NS\r
+ // communication buffer that has been reserved by S-EL1/EL3\r
+ ConfigurationTable = mMmst->MmConfigurationTable;\r
+ for (Index = 0; Index < mMmst->NumberOfTableEntries; Index++) {\r
+ if (CompareGuid (&gEfiHobListGuid, &(ConfigurationTable[Index].VendorGuid))) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ // Bail out if the Hoblist could not be found\r
+ if (Index >= mMmst->NumberOfTableEntries) {\r
+ DEBUG ((DEBUG_INFO, "Hoblist not found - 0x%x\n", Index));\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ HobStart = ConfigurationTable[Index].VendorTable;\r
+\r
+ //\r
+ // Locate the HOB with the buffer to populate the entry point of this driver\r
+ //\r
+ Status = GetGuidedHobData (\r
+ HobStart,\r
+ &gEfiArmTfCpuDriverEpDescriptorGuid,\r
+ (VOID **) &CpuDriverEntryPointDesc\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_INFO, "ArmTfCpuDriverEpDesc HOB data extraction failed - 0x%x\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ // Share the entry point of the CPU driver\r
+ DEBUG ((DEBUG_INFO, "Sharing Cpu Driver EP *0x%lx = 0x%lx\n",\r
+ (UINT64) CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr,\r
+ (UINT64) PiMmStandloneArmTfCpuDriverEntry));\r
+ *(CpuDriverEntryPointDesc->ArmTfCpuDriverEpPtr) = PiMmStandloneArmTfCpuDriverEntry;\r
+\r
+ // Find the descriptor that contains the whereabouts of the buffer for\r
+ // communication with the Normal world.\r
+ Status = GetGuidedHobData (\r
+ HobStart,\r
+ &gEfiStandaloneMmNonSecureBufferGuid,\r
+ (VOID **) &NsCommBufMmramRange\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_INFO, "NsCommBufMmramRange HOB data extraction failed - 0x%x\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalStart - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalStart));\r
+ DEBUG ((DEBUG_INFO, "mNsCommBuffer.PhysicalSize - 0x%lx\n", (UINT64) NsCommBufMmramRange->PhysicalSize));\r
+\r
+ CopyMem (&mNsCommBuffer, NsCommBufMmramRange, sizeof(EFI_MMRAM_DESCRIPTOR));\r
+ DEBUG ((DEBUG_INFO, "mNsCommBuffer: 0x%016lx - 0x%lx\n", mNsCommBuffer.CpuStart, mNsCommBuffer.PhysicalSize));\r
+\r
+ //\r
+ // Extract the MP information from the Hoblist\r
+ //\r
+ Status = GetGuidedHobData (\r
+ HobStart,\r
+ &gMpInformationHobGuid,\r
+ (VOID **) &MpInformationHobData\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_INFO, "MpInformationHob extraction failed - 0x%x\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Allocate memory for the MP information and copy over the MP information\r
+ // passed by Trusted Firmware. Use the number of processors passed in the HOB\r
+ // to copy the processor information\r
+ //\r
+ MpInfoSize = sizeof (MP_INFORMATION_HOB_DATA) +\r
+ (sizeof (EFI_PROCESSOR_INFORMATION) *\r
+ MpInformationHobData->NumberOfProcessors);\r
+ Status = mMmst->MmAllocatePool (\r
+ EfiRuntimeServicesData,\r
+ MpInfoSize,\r
+ (VOID **) &mMpInformationHobData\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_INFO, "mMpInformationHobData mem alloc failed - 0x%x\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ CopyMem (mMpInformationHobData, MpInformationHobData, MpInfoSize);\r
+\r
+ // Print MP information\r
+ DEBUG ((DEBUG_INFO, "mMpInformationHobData: 0x%016lx - 0x%lx\n",\r
+ mMpInformationHobData->NumberOfProcessors,\r
+ mMpInformationHobData->NumberOfEnabledProcessors));\r
+ for (Index = 0; Index < mMpInformationHobData->NumberOfProcessors; Index++) {\r
+ DEBUG ((DEBUG_INFO, "mMpInformationHobData[0x%lx]: %d, %d, %d\n",\r
+ mMpInformationHobData->ProcessorInfoBuffer[Index].ProcessorId,\r
+ mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Package,\r
+ mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Core,\r
+ mMpInformationHobData->ProcessorInfoBuffer[Index].Location.Thread));\r
+ }\r
+\r
+ //\r
+ // Allocate memory for a table to hold pointers to a\r
+ // EFI_MM_COMMUNICATE_HEADER for each CPU\r
+ //\r
+ ArraySize = sizeof (EFI_MM_COMMUNICATE_HEADER *) *\r
+ mMpInformationHobData->NumberOfEnabledProcessors;\r
+ Status = mMmst->MmAllocatePool (\r
+ EfiRuntimeServicesData,\r
+ ArraySize,\r
+ (VOID **) &PerCpuGuidedEventContext\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_INFO, "PerCpuGuidedEventContext mem alloc failed - 0x%x\n", Status));\r
+ return Status;\r
+ }\r
+ return Status;\r
+}\r