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