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
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Pi/PiMmCis.h>
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>
20 #include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
22 #include <Guid/ZeroGuid.h>
23 #include <Guid/MmramMemoryReserve.h>
25 #include <IndustryStandard/ArmFfaSvc.h>
26 #include <IndustryStandard/ArmStdSmc.h>
28 #include "StandaloneMmCpu.h"
32 MmFoundationEntryRegister (
33 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
34 IN EFI_MM_ENTRY_POINT MmEntryPoint
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.
48 EFI_MM_COMMUNICATE_HEADER
**PerCpuGuidedEventContext
= NULL
;
50 // Descriptor with whereabouts of memory used for communication with the normal world
51 EFI_MMRAM_DESCRIPTOR mNsCommBuffer
;
52 EFI_MMRAM_DESCRIPTOR mSCommBuffer
;
54 MP_INFORMATION_HOB_DATA
*mMpInformationHobData
;
56 EFI_MM_CONFIGURATION_PROTOCOL mMmConfig
= {
58 MmFoundationEntryRegister
61 STATIC EFI_MM_ENTRY_POINT mMmEntryPoint
= NULL
;
64 Perform bounds check on the common buffer.
66 @param [in] BufferAddr Address of the common buffer.
68 @retval EFI_SUCCESS Success.
69 @retval EFI_ACCESS_DENIED Access not permitted.
77 UINT64 NsCommBufferEnd
;
78 UINT64 SCommBufferEnd
;
81 NsCommBufferEnd
= mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
;
82 SCommBufferEnd
= mSCommBuffer
.PhysicalStart
+ mSCommBuffer
.PhysicalSize
;
84 if ((BufferAddr
>= mNsCommBuffer
.PhysicalStart
) &&
85 (BufferAddr
< NsCommBufferEnd
))
87 CommBufferEnd
= NsCommBufferEnd
;
88 } else if ((BufferAddr
>= mSCommBuffer
.PhysicalStart
) &&
89 (BufferAddr
< SCommBufferEnd
))
91 CommBufferEnd
= SCommBufferEnd
;
93 return EFI_ACCESS_DENIED
;
96 if ((CommBufferEnd
- BufferAddr
) < sizeof (EFI_MM_COMMUNICATE_HEADER
)) {
97 return EFI_ACCESS_DENIED
;
100 // perform bounds check.
101 if ((CommBufferEnd
- BufferAddr
- sizeof (EFI_MM_COMMUNICATE_HEADER
)) <
102 ((EFI_MM_COMMUNICATE_HEADER
*)BufferAddr
)->MessageLength
)
104 return EFI_ACCESS_DENIED
;
111 The PI Standalone MM entry point for the TF-A CPU driver.
113 @param [in] EventId The event Id.
114 @param [in] CpuNumber The CPU number.
115 @param [in] NsCommBufferAddr Address of the NS common buffer.
117 @retval EFI_SUCCESS Success.
118 @retval EFI_INVALID_PARAMETER A parameter was invalid.
119 @retval EFI_ACCESS_DENIED Access not permitted.
120 @retval EFI_OUT_OF_RESOURCES Out of resources.
121 @retval EFI_UNSUPPORTED Operation not supported.
124 PiMmStandaloneArmTfCpuDriverEntry (
127 IN UINTN NsCommBufferAddr
130 EFI_MM_COMMUNICATE_HEADER
*GuidedEventContext
;
131 EFI_MM_ENTRY_CONTEXT MmEntryPointContext
;
133 UINTN NsCommBufferSize
;
135 DEBUG ((DEBUG_INFO
, "Received event - 0x%x on cpu %d\n", EventId
, CpuNumber
));
137 Status
= EFI_SUCCESS
;
139 // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
140 // receipt of a synchronous MM request. Use the Event ID to distinguish
141 // between synchronous and asynchronous events.
143 if ((ARM_SMC_ID_MM_COMMUNICATE
!= EventId
) &&
144 (ARM_SVC_ID_FFA_MSG_SEND_DIRECT_REQ
!= EventId
))
146 DEBUG ((DEBUG_ERROR
, "UnRecognized Event - 0x%x\n", EventId
));
147 return EFI_INVALID_PARAMETER
;
150 // Perform parameter validation of NsCommBufferAddr
151 if (NsCommBufferAddr
== (UINTN
)NULL
) {
152 return EFI_INVALID_PARAMETER
;
155 Status
= CheckBufferAddr (NsCommBufferAddr
);
156 if (EFI_ERROR (Status
)) {
157 DEBUG ((DEBUG_ERROR
, "Check Buffer failed: %r\n", Status
));
161 // Find out the size of the buffer passed
162 NsCommBufferSize
= ((EFI_MM_COMMUNICATE_HEADER
*)NsCommBufferAddr
)->MessageLength
+
163 sizeof (EFI_MM_COMMUNICATE_HEADER
);
165 GuidedEventContext
= NULL
;
166 // Now that the secure world can see the normal world buffer, allocate
167 // memory to copy the communication buffer to the secure world.
168 Status
= mMmst
->MmAllocatePool (
169 EfiRuntimeServicesData
,
171 (VOID
**)&GuidedEventContext
174 if (Status
!= EFI_SUCCESS
) {
175 DEBUG ((DEBUG_ERROR
, "Mem alloc failed - 0x%x\n", EventId
));
176 return EFI_OUT_OF_RESOURCES
;
179 // X1 contains the VA of the normal world memory accessible from
181 CopyMem (GuidedEventContext
, (CONST VOID
*)NsCommBufferAddr
, NsCommBufferSize
);
183 // Stash the pointer to the allocated Event Context for this CPU
184 PerCpuGuidedEventContext
[CpuNumber
] = GuidedEventContext
;
186 ZeroMem (&MmEntryPointContext
, sizeof (EFI_MM_ENTRY_CONTEXT
));
188 MmEntryPointContext
.CurrentlyExecutingCpu
= CpuNumber
;
189 MmEntryPointContext
.NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
191 // Populate the MM system table with MP and state information
192 mMmst
->CurrentlyExecutingCpu
= CpuNumber
;
193 mMmst
->NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
194 mMmst
->CpuSaveStateSize
= 0;
195 mMmst
->CpuSaveState
= NULL
;
197 if (mMmEntryPoint
== NULL
) {
198 DEBUG ((DEBUG_ERROR
, "Mm Entry point Not Found\n"));
199 return EFI_UNSUPPORTED
;
202 mMmEntryPoint (&MmEntryPointContext
);
204 // Free the memory allocation done earlier and reset the per-cpu context
205 ASSERT (GuidedEventContext
);
206 CopyMem ((VOID
*)NsCommBufferAddr
, (CONST VOID
*)GuidedEventContext
, NsCommBufferSize
);
208 Status
= mMmst
->MmFreePool ((VOID
*)GuidedEventContext
);
209 if (Status
!= EFI_SUCCESS
) {
210 return EFI_OUT_OF_RESOURCES
;
213 PerCpuGuidedEventContext
[CpuNumber
] = NULL
;
219 Registers the MM foundation entry point.
221 @param [in] This Pointer to the MM Configuration protocol.
222 @param [in] MmEntryPoint Function pointer to the MM Entry point.
224 @retval EFI_SUCCESS Success.
228 MmFoundationEntryRegister (
229 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
230 IN EFI_MM_ENTRY_POINT MmEntryPoint
233 // store the entry point in a global
234 mMmEntryPoint
= MmEntryPoint
;
239 This function is the main entry point for an MM handler dispatch
240 or communicate-based callback.
242 @param DispatchHandle The unique handle assigned to this handler by
243 MmiHandlerRegister().
244 @param Context Points to an optional handler context which was
245 specified when the handler was registered.
246 @param CommBuffer A pointer to a collection of data in memory that will
247 be conveyed from a non-MM environment into an
249 @param CommBufferSize The size of the CommBuffer.
256 PiMmCpuTpFwRootMmiHandler (
257 IN EFI_HANDLE DispatchHandle
,
258 IN CONST VOID
*Context OPTIONAL
,
259 IN OUT VOID
*CommBuffer OPTIONAL
,
260 IN OUT UINTN
*CommBufferSize OPTIONAL
266 ASSERT (Context
== NULL
);
267 ASSERT (CommBuffer
== NULL
);
268 ASSERT (CommBufferSize
== NULL
);
270 CpuNumber
= mMmst
->CurrentlyExecutingCpu
;
271 if (PerCpuGuidedEventContext
[CpuNumber
] == NULL
) {
272 return EFI_NOT_FOUND
;
277 "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
278 PerCpuGuidedEventContext
[CpuNumber
],
279 PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
282 Status
= mMmst
->MmiManage (
283 &PerCpuGuidedEventContext
[CpuNumber
]->HeaderGuid
,
285 PerCpuGuidedEventContext
[CpuNumber
]->Data
,
286 &PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
289 if (Status
!= EFI_SUCCESS
) {
290 DEBUG ((DEBUG_WARN
, "Unable to manage Guided Event - %d\n", Status
));