]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c
StandaloneMmPkg: Allow sending FFA Direct Request message to StandaloneMm
[mirror_edk2.git] / StandaloneMmPkg / Drivers / StandaloneMmCpu / AArch64 / EventHandle.c
CommitLineData
275d4bd4
SV
1/** @file\r
2\r
3 Copyright (c) 2016 HP Development Company, L.P.\r
a9da96ac 4 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.\r
275d4bd4 5\r
86094561 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
275d4bd4
SV
7\r
8**/\r
9\r
10#include <Base.h>\r
11#include <Pi/PiMmCis.h>\r
12\r
13\r
14#include <Library/ArmSvcLib.h>\r
15#include <Library/ArmLib.h>\r
16#include <Library/BaseMemoryLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/HobLib.h>\r
19\r
20#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT\r
21\r
22#include <Guid/ZeroGuid.h>\r
23#include <Guid/MmramMemoryReserve.h>\r
24\r
68e5ecc4 25#include <IndustryStandard/ArmFfaSvc.h>\r
275d4bd4
SV
26#include <IndustryStandard/ArmStdSmc.h>\r
27\r
28#include "StandaloneMmCpu.h"\r
29\r
30EFI_STATUS\r
31EFIAPI\r
32MmFoundationEntryRegister (\r
33 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,\r
34 IN EFI_MM_ENTRY_POINT MmEntryPoint\r
35 );\r
36\r
37//\r
38// On ARM platforms every event is expected to have a GUID associated with\r
39// it. It will be used by the MM Entry point to find the handler for the\r
40// event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the\r
41// caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver\r
42// (e.g. during an asynchronous event). In either case, this context is\r
43// maintained in an array which has an entry for each CPU. The pointer to this\r
44// array is held in PerCpuGuidedEventContext. Memory is allocated once the\r
45// number of CPUs in the system are made known through the\r
46// MP_INFORMATION_HOB_DATA.\r
47//\r
48EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;\r
49\r
50// Descriptor with whereabouts of memory used for communication with the normal world\r
51EFI_MMRAM_DESCRIPTOR mNsCommBuffer;\r
52\r
53MP_INFORMATION_HOB_DATA *mMpInformationHobData;\r
54\r
55EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {\r
56 0,\r
57 MmFoundationEntryRegister\r
58};\r
59\r
60STATIC EFI_MM_ENTRY_POINT mMmEntryPoint = NULL;\r
61\r
2da602fa
SM
62/**\r
63 The PI Standalone MM entry point for the TF-A CPU driver.\r
64\r
65 @param [in] EventId The event Id.\r
66 @param [in] CpuNumber The CPU number.\r
67 @param [in] NsCommBufferAddr Address of the NS common buffer.\r
68\r
69 @retval EFI_SUCCESS Success.\r
70 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
71 @retval EFI_ACCESS_DENIED Access not permitted.\r
72 @retval EFI_OUT_OF_RESOURCES Out of resources.\r
73 @retval EFI_UNSUPPORTED Operation not supported.\r
74**/\r
275d4bd4 75EFI_STATUS\r
c8102727 76PiMmStandaloneArmTfCpuDriverEntry (\r
275d4bd4
SV
77 IN UINTN EventId,\r
78 IN UINTN CpuNumber,\r
79 IN UINTN NsCommBufferAddr\r
80 )\r
81{\r
eda1ffac
SM
82 EFI_MM_COMMUNICATE_HEADER *GuidedEventContext;\r
83 EFI_MM_ENTRY_CONTEXT MmEntryPointContext;\r
275d4bd4
SV
84 EFI_STATUS Status;\r
85 UINTN NsCommBufferSize;\r
86\r
87 DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));\r
88\r
89 Status = EFI_SUCCESS;\r
90 //\r
91 // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon\r
92 // receipt of a synchronous MM request. Use the Event ID to distinguish\r
93 // between synchronous and asynchronous events.\r
94 //\r
68e5ecc4
IA
95 if ((ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) &&\r
96 (ARM_SVC_ID_FFA_MSG_SEND_DIRECT_REQ_AARCH64 != EventId)) {\r
275d4bd4
SV
97 DEBUG ((DEBUG_INFO, "UnRecognized Event - 0x%x\n", EventId));\r
98 return EFI_INVALID_PARAMETER;\r
99 }\r
100\r
101 // Perform parameter validation of NsCommBufferAddr\r
a9da96ac
SM
102 if (NsCommBufferAddr == (UINTN)NULL) {\r
103 return EFI_INVALID_PARAMETER;\r
104 }\r
105\r
106 if (NsCommBufferAddr < mNsCommBuffer.PhysicalStart) {\r
275d4bd4 107 return EFI_ACCESS_DENIED;\r
a9da96ac 108 }\r
275d4bd4
SV
109\r
110 if ((NsCommBufferAddr + sizeof (EFI_MM_COMMUNICATE_HEADER)) >=\r
a9da96ac 111 (mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)) {\r
275d4bd4 112 return EFI_INVALID_PARAMETER;\r
a9da96ac 113 }\r
275d4bd4
SV
114\r
115 // Find out the size of the buffer passed\r
116 NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +\r
117 sizeof (EFI_MM_COMMUNICATE_HEADER);\r
118\r
119 // perform bounds check.\r
120 if (NsCommBufferAddr + NsCommBufferSize >=\r
a9da96ac 121 mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize) {\r
275d4bd4 122 return EFI_ACCESS_DENIED;\r
a9da96ac 123 }\r
275d4bd4 124\r
eda1ffac 125 GuidedEventContext = NULL;\r
275d4bd4
SV
126 // Now that the secure world can see the normal world buffer, allocate\r
127 // memory to copy the communication buffer to the secure world.\r
128 Status = mMmst->MmAllocatePool (\r
129 EfiRuntimeServicesData,\r
130 NsCommBufferSize,\r
131 (VOID **) &GuidedEventContext\r
132 );\r
133\r
134 if (Status != EFI_SUCCESS) {\r
135 DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));\r
136 return EFI_OUT_OF_RESOURCES;\r
137 }\r
138\r
139 // X1 contains the VA of the normal world memory accessible from\r
140 // S-EL0\r
141 CopyMem (GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);\r
142\r
143 // Stash the pointer to the allocated Event Context for this CPU\r
144 PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;\r
145\r
eda1ffac
SM
146 ZeroMem (&MmEntryPointContext, sizeof (EFI_MM_ENTRY_CONTEXT));\r
147\r
275d4bd4
SV
148 MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;\r
149 MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;\r
150\r
151 // Populate the MM system table with MP and state information\r
152 mMmst->CurrentlyExecutingCpu = CpuNumber;\r
153 mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;\r
154 mMmst->CpuSaveStateSize = 0;\r
155 mMmst->CpuSaveState = NULL;\r
156\r
157 if (mMmEntryPoint == NULL) {\r
158 DEBUG ((DEBUG_INFO, "Mm Entry point Not Found\n"));\r
159 return EFI_UNSUPPORTED;\r
160 }\r
161\r
162 mMmEntryPoint (&MmEntryPointContext);\r
163\r
164 // Free the memory allocation done earlier and reset the per-cpu context\r
165 ASSERT (GuidedEventContext);\r
166 CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);\r
167\r
168 Status = mMmst->MmFreePool ((VOID *) GuidedEventContext);\r
169 if (Status != EFI_SUCCESS) {\r
170 return EFI_OUT_OF_RESOURCES;\r
171 }\r
172 PerCpuGuidedEventContext[CpuNumber] = NULL;\r
173\r
174 return Status;\r
175}\r
176\r
2da602fa
SM
177/**\r
178 Registers the MM foundation entry point.\r
179\r
180 @param [in] This Pointer to the MM Configuration protocol.\r
181 @param [in] MmEntryPoint Function pointer to the MM Entry point.\r
182\r
183 @retval EFI_SUCCESS Success.\r
184**/\r
275d4bd4
SV
185EFI_STATUS\r
186EFIAPI\r
187MmFoundationEntryRegister (\r
188 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,\r
189 IN EFI_MM_ENTRY_POINT MmEntryPoint\r
190 )\r
191{\r
192 // store the entry point in a global\r
193 mMmEntryPoint = MmEntryPoint;\r
194 return EFI_SUCCESS;\r
195}\r
196\r
197/**\r
198 This function is the main entry point for an MM handler dispatch\r
199 or communicate-based callback.\r
200\r
9a0f88b5
SM
201 @param DispatchHandle The unique handle assigned to this handler by\r
202 MmiHandlerRegister().\r
203 @param Context Points to an optional handler context which was\r
204 specified when the handler was registered.\r
275d4bd4 205 @param CommBuffer A pointer to a collection of data in memory that will\r
9a0f88b5
SM
206 be conveyed from a non-MM environment into an\r
207 MM environment.\r
275d4bd4
SV
208 @param CommBufferSize The size of the CommBuffer.\r
209\r
210 @return Status Code\r
211\r
212**/\r
213EFI_STATUS\r
214EFIAPI\r
215PiMmCpuTpFwRootMmiHandler (\r
216 IN EFI_HANDLE DispatchHandle,\r
217 IN CONST VOID *Context, OPTIONAL\r
218 IN OUT VOID *CommBuffer, OPTIONAL\r
219 IN OUT UINTN *CommBufferSize OPTIONAL\r
220 )\r
221{\r
222 EFI_STATUS Status;\r
223 UINTN CpuNumber;\r
224\r
225 ASSERT (Context == NULL);\r
226 ASSERT (CommBuffer == NULL);\r
227 ASSERT (CommBufferSize == NULL);\r
228\r
229 CpuNumber = mMmst->CurrentlyExecutingCpu;\r
a9da96ac 230 if (PerCpuGuidedEventContext[CpuNumber] == NULL) {\r
275d4bd4 231 return EFI_NOT_FOUND;\r
a9da96ac 232 }\r
275d4bd4
SV
233\r
234 DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",\r
235 PerCpuGuidedEventContext[CpuNumber],\r
236 PerCpuGuidedEventContext[CpuNumber]->MessageLength));\r
237\r
238 Status = mMmst->MmiManage (\r
239 &PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,\r
240 NULL,\r
241 PerCpuGuidedEventContext[CpuNumber]->Data,\r
242 &PerCpuGuidedEventContext[CpuNumber]->MessageLength\r
243 );\r
244\r
245 if (Status != EFI_SUCCESS) {\r
246 DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));\r
247 }\r
248\r
249 return Status;\r
250}\r