--- /dev/null
+/** @file\r
+\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
+\r
+\r
+#include <Library/ArmSvcLib.h>\r
+#include <Library/ArmLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.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
+#include <IndustryStandard/ArmStdSmc.h>\r
+\r
+#include "StandaloneMmCpu.h"\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+MmFoundationEntryRegister (\r
+ IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,\r
+ IN EFI_MM_ENTRY_POINT MmEntryPoint\r
+ );\r
+\r
+//\r
+// On ARM platforms every event is expected to have a GUID associated with\r
+// it. It will be used by the MM Entry point to find the handler for the\r
+// event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the\r
+// caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver\r
+// (e.g. during an asynchronous event). In either case, this context is\r
+// maintained in an array which has an entry for each CPU. The pointer to this\r
+// array is held in PerCpuGuidedEventContext. Memory is allocated once the\r
+// number of CPUs in the system are made known through the\r
+// MP_INFORMATION_HOB_DATA.\r
+//\r
+EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;\r
+\r
+// Descriptor with whereabouts of memory used for communication with the normal world\r
+EFI_MMRAM_DESCRIPTOR mNsCommBuffer;\r
+\r
+MP_INFORMATION_HOB_DATA *mMpInformationHobData;\r
+\r
+EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {\r
+ 0,\r
+ MmFoundationEntryRegister\r
+};\r
+\r
+STATIC EFI_MM_ENTRY_POINT mMmEntryPoint = NULL;\r
+\r
+EFI_STATUS\r
+PiMmStandloneArmTfCpuDriverEntry (\r
+ IN UINTN EventId,\r
+ IN UINTN CpuNumber,\r
+ IN UINTN NsCommBufferAddr\r
+ )\r
+{\r
+ EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;\r
+ EFI_MM_ENTRY_CONTEXT MmEntryPointContext = {0};\r
+ EFI_STATUS Status;\r
+ UINTN NsCommBufferSize;\r
+\r
+ DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));\r
+\r
+ Status = EFI_SUCCESS;\r
+ //\r
+ // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon\r
+ // receipt of a synchronous MM request. Use the Event ID to distinguish\r
+ // between synchronous and asynchronous events.\r
+ //\r
+ if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {\r
+ DEBUG ((DEBUG_INFO, "UnRecognized Event - 0x%x\n", EventId));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ // Perform parameter validation of NsCommBufferAddr\r
+ if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))\r
+ return EFI_ACCESS_DENIED;\r
+\r
+ if ((NsCommBufferAddr + sizeof (EFI_MM_COMMUNICATE_HEADER)) >=\r
+ (mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize))\r
+ return EFI_INVALID_PARAMETER;\r
+\r
+ // Find out the size of the buffer passed\r
+ NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +\r
+ sizeof (EFI_MM_COMMUNICATE_HEADER);\r
+\r
+ // perform bounds check.\r
+ if (NsCommBufferAddr + NsCommBufferSize >=\r
+ mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)\r
+ return EFI_ACCESS_DENIED;\r
+\r
+\r
+ // Now that the secure world can see the normal world buffer, allocate\r
+ // memory to copy the communication buffer to the secure world.\r
+ Status = mMmst->MmAllocatePool (\r
+ EfiRuntimeServicesData,\r
+ NsCommBufferSize,\r
+ (VOID **) &GuidedEventContext\r
+ );\r
+\r
+ if (Status != EFI_SUCCESS) {\r
+ DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ // X1 contains the VA of the normal world memory accessible from\r
+ // S-EL0\r
+ CopyMem (GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);\r
+\r
+ // Stash the pointer to the allocated Event Context for this CPU\r
+ PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;\r
+\r
+ MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;\r
+ MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;\r
+\r
+ // Populate the MM system table with MP and state information\r
+ mMmst->CurrentlyExecutingCpu = CpuNumber;\r
+ mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;\r
+ mMmst->CpuSaveStateSize = 0;\r
+ mMmst->CpuSaveState = NULL;\r
+\r
+ if (mMmEntryPoint == NULL) {\r
+ DEBUG ((DEBUG_INFO, "Mm Entry point Not Found\n"));\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ mMmEntryPoint (&MmEntryPointContext);\r
+\r
+ // Free the memory allocation done earlier and reset the per-cpu context\r
+ ASSERT (GuidedEventContext);\r
+ CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);\r
+\r
+ Status = mMmst->MmFreePool ((VOID *) GuidedEventContext);\r
+ if (Status != EFI_SUCCESS) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ PerCpuGuidedEventContext[CpuNumber] = NULL;\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+MmFoundationEntryRegister (\r
+ IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,\r
+ IN EFI_MM_ENTRY_POINT MmEntryPoint\r
+ )\r
+{\r
+ // store the entry point in a global\r
+ mMmEntryPoint = MmEntryPoint;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This function is the main entry point for an MM handler dispatch\r
+ or communicate-based callback.\r
+\r
+ @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().\r
+ @param Context Points to an optional handler context which was specified when the handler was registered.\r
+ @param CommBuffer A pointer to a collection of data in memory that will\r
+ be conveyed from a non-MM environment into an MM environment.\r
+ @param CommBufferSize The size of the CommBuffer.\r
+\r
+ @return Status Code\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PiMmCpuTpFwRootMmiHandler (\r
+ IN EFI_HANDLE DispatchHandle,\r
+ IN CONST VOID *Context, OPTIONAL\r
+ IN OUT VOID *CommBuffer, OPTIONAL\r
+ IN OUT UINTN *CommBufferSize OPTIONAL\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN CpuNumber;\r
+\r
+ ASSERT (Context == NULL);\r
+ ASSERT (CommBuffer == NULL);\r
+ ASSERT (CommBufferSize == NULL);\r
+\r
+ CpuNumber = mMmst->CurrentlyExecutingCpu;\r
+ if (!PerCpuGuidedEventContext[CpuNumber])\r
+ return EFI_NOT_FOUND;\r
+\r
+ DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",\r
+ PerCpuGuidedEventContext[CpuNumber],\r
+ PerCpuGuidedEventContext[CpuNumber]->MessageLength));\r
+\r
+ Status = mMmst->MmiManage (\r
+ &PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,\r
+ NULL,\r
+ PerCpuGuidedEventContext[CpuNumber]->Data,\r
+ &PerCpuGuidedEventContext[CpuNumber]->MessageLength\r
+ );\r
+\r
+ if (Status != EFI_SUCCESS) {\r
+ DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));\r
+ }\r
+\r
+ return Status;\r
+}\r