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