3 Copyright (c) 2016 HP Development Company, L.P.
4 Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #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
;
53 MP_INFORMATION_HOB_DATA
*mMpInformationHobData
;
55 EFI_MM_CONFIGURATION_PROTOCOL mMmConfig
= {
57 MmFoundationEntryRegister
60 STATIC EFI_MM_ENTRY_POINT mMmEntryPoint
= NULL
;
63 The PI Standalone MM entry point for the TF-A CPU driver.
65 @param [in] EventId The event Id.
66 @param [in] CpuNumber The CPU number.
67 @param [in] NsCommBufferAddr Address of the NS common buffer.
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.
76 PiMmStandaloneArmTfCpuDriverEntry (
79 IN UINTN NsCommBufferAddr
82 EFI_MM_COMMUNICATE_HEADER
*GuidedEventContext
;
83 EFI_MM_ENTRY_CONTEXT MmEntryPointContext
;
85 UINTN NsCommBufferSize
;
87 DEBUG ((DEBUG_INFO
, "Received event - 0x%x on cpu %d\n", EventId
, CpuNumber
));
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.
95 if ((ARM_SMC_ID_MM_COMMUNICATE_AARCH64
!= EventId
) &&
96 (ARM_SVC_ID_FFA_MSG_SEND_DIRECT_REQ_AARCH64
!= EventId
)) {
97 DEBUG ((DEBUG_INFO
, "UnRecognized Event - 0x%x\n", EventId
));
98 return EFI_INVALID_PARAMETER
;
101 // Perform parameter validation of NsCommBufferAddr
102 if (NsCommBufferAddr
== (UINTN
)NULL
) {
103 return EFI_INVALID_PARAMETER
;
106 if (NsCommBufferAddr
< mNsCommBuffer
.PhysicalStart
) {
107 return EFI_ACCESS_DENIED
;
110 if ((NsCommBufferAddr
+ sizeof (EFI_MM_COMMUNICATE_HEADER
)) >=
111 (mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
)) {
112 return EFI_INVALID_PARAMETER
;
115 // Find out the size of the buffer passed
116 NsCommBufferSize
= ((EFI_MM_COMMUNICATE_HEADER
*) NsCommBufferAddr
)->MessageLength
+
117 sizeof (EFI_MM_COMMUNICATE_HEADER
);
119 // perform bounds check.
120 if (NsCommBufferAddr
+ NsCommBufferSize
>=
121 mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
) {
122 return EFI_ACCESS_DENIED
;
125 GuidedEventContext
= NULL
;
126 // Now that the secure world can see the normal world buffer, allocate
127 // memory to copy the communication buffer to the secure world.
128 Status
= mMmst
->MmAllocatePool (
129 EfiRuntimeServicesData
,
131 (VOID
**) &GuidedEventContext
134 if (Status
!= EFI_SUCCESS
) {
135 DEBUG ((DEBUG_INFO
, "Mem alloc failed - 0x%x\n", EventId
));
136 return EFI_OUT_OF_RESOURCES
;
139 // X1 contains the VA of the normal world memory accessible from
141 CopyMem (GuidedEventContext
, (CONST VOID
*) NsCommBufferAddr
, NsCommBufferSize
);
143 // Stash the pointer to the allocated Event Context for this CPU
144 PerCpuGuidedEventContext
[CpuNumber
] = GuidedEventContext
;
146 ZeroMem (&MmEntryPointContext
, sizeof (EFI_MM_ENTRY_CONTEXT
));
148 MmEntryPointContext
.CurrentlyExecutingCpu
= CpuNumber
;
149 MmEntryPointContext
.NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
151 // Populate the MM system table with MP and state information
152 mMmst
->CurrentlyExecutingCpu
= CpuNumber
;
153 mMmst
->NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
154 mMmst
->CpuSaveStateSize
= 0;
155 mMmst
->CpuSaveState
= NULL
;
157 if (mMmEntryPoint
== NULL
) {
158 DEBUG ((DEBUG_INFO
, "Mm Entry point Not Found\n"));
159 return EFI_UNSUPPORTED
;
162 mMmEntryPoint (&MmEntryPointContext
);
164 // Free the memory allocation done earlier and reset the per-cpu context
165 ASSERT (GuidedEventContext
);
166 CopyMem ((VOID
*)NsCommBufferAddr
, (CONST VOID
*) GuidedEventContext
, NsCommBufferSize
);
168 Status
= mMmst
->MmFreePool ((VOID
*) GuidedEventContext
);
169 if (Status
!= EFI_SUCCESS
) {
170 return EFI_OUT_OF_RESOURCES
;
172 PerCpuGuidedEventContext
[CpuNumber
] = NULL
;
178 Registers the MM foundation entry point.
180 @param [in] This Pointer to the MM Configuration protocol.
181 @param [in] MmEntryPoint Function pointer to the MM Entry point.
183 @retval EFI_SUCCESS Success.
187 MmFoundationEntryRegister (
188 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
189 IN EFI_MM_ENTRY_POINT MmEntryPoint
192 // store the entry point in a global
193 mMmEntryPoint
= MmEntryPoint
;
198 This function is the main entry point for an MM handler dispatch
199 or communicate-based callback.
201 @param DispatchHandle The unique handle assigned to this handler by
202 MmiHandlerRegister().
203 @param Context Points to an optional handler context which was
204 specified when the handler was registered.
205 @param CommBuffer A pointer to a collection of data in memory that will
206 be conveyed from a non-MM environment into an
208 @param CommBufferSize The size of the CommBuffer.
215 PiMmCpuTpFwRootMmiHandler (
216 IN EFI_HANDLE DispatchHandle
,
217 IN CONST VOID
*Context
, OPTIONAL
218 IN OUT VOID
*CommBuffer
, OPTIONAL
219 IN OUT UINTN
*CommBufferSize OPTIONAL
225 ASSERT (Context
== NULL
);
226 ASSERT (CommBuffer
== NULL
);
227 ASSERT (CommBufferSize
== NULL
);
229 CpuNumber
= mMmst
->CurrentlyExecutingCpu
;
230 if (PerCpuGuidedEventContext
[CpuNumber
] == NULL
) {
231 return EFI_NOT_FOUND
;
234 DEBUG ((DEBUG_INFO
, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
235 PerCpuGuidedEventContext
[CpuNumber
],
236 PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
));
238 Status
= mMmst
->MmiManage (
239 &PerCpuGuidedEventContext
[CpuNumber
]->HeaderGuid
,
241 PerCpuGuidedEventContext
[CpuNumber
]->Data
,
242 &PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
245 if (Status
!= EFI_SUCCESS
) {
246 DEBUG ((DEBUG_WARN
, "Unable to manage Guided Event - %d\n", Status
));