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
;
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
!= EventId
) &&
96 (ARM_SVC_ID_FFA_MSG_SEND_DIRECT_REQ
!= EventId
))
98 DEBUG ((DEBUG_ERROR
, "UnRecognized Event - 0x%x\n", EventId
));
99 return EFI_INVALID_PARAMETER
;
102 // Perform parameter validation of NsCommBufferAddr
103 if (NsCommBufferAddr
== (UINTN
)NULL
) {
104 return EFI_INVALID_PARAMETER
;
107 if (NsCommBufferAddr
< mNsCommBuffer
.PhysicalStart
) {
108 return EFI_ACCESS_DENIED
;
111 if ((NsCommBufferAddr
+ sizeof (EFI_MM_COMMUNICATE_HEADER
)) >=
112 (mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
))
114 return EFI_INVALID_PARAMETER
;
117 // Find out the size of the buffer passed
118 NsCommBufferSize
= ((EFI_MM_COMMUNICATE_HEADER
*)NsCommBufferAddr
)->MessageLength
+
119 sizeof (EFI_MM_COMMUNICATE_HEADER
);
121 // perform bounds check.
122 if (NsCommBufferAddr
+ NsCommBufferSize
>=
123 mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
)
125 return EFI_ACCESS_DENIED
;
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
,
134 (VOID
**)&GuidedEventContext
137 if (Status
!= EFI_SUCCESS
) {
138 DEBUG ((DEBUG_ERROR
, "Mem alloc failed - 0x%x\n", EventId
));
139 return EFI_OUT_OF_RESOURCES
;
142 // X1 contains the VA of the normal world memory accessible from
144 CopyMem (GuidedEventContext
, (CONST VOID
*)NsCommBufferAddr
, NsCommBufferSize
);
146 // Stash the pointer to the allocated Event Context for this CPU
147 PerCpuGuidedEventContext
[CpuNumber
] = GuidedEventContext
;
149 ZeroMem (&MmEntryPointContext
, sizeof (EFI_MM_ENTRY_CONTEXT
));
151 MmEntryPointContext
.CurrentlyExecutingCpu
= CpuNumber
;
152 MmEntryPointContext
.NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
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
;
160 if (mMmEntryPoint
== NULL
) {
161 DEBUG ((DEBUG_ERROR
, "Mm Entry point Not Found\n"));
162 return EFI_UNSUPPORTED
;
165 mMmEntryPoint (&MmEntryPointContext
);
167 // Free the memory allocation done earlier and reset the per-cpu context
168 ASSERT (GuidedEventContext
);
169 CopyMem ((VOID
*)NsCommBufferAddr
, (CONST VOID
*)GuidedEventContext
, NsCommBufferSize
);
171 Status
= mMmst
->MmFreePool ((VOID
*)GuidedEventContext
);
172 if (Status
!= EFI_SUCCESS
) {
173 return EFI_OUT_OF_RESOURCES
;
176 PerCpuGuidedEventContext
[CpuNumber
] = NULL
;
182 Registers the MM foundation entry point.
184 @param [in] This Pointer to the MM Configuration protocol.
185 @param [in] MmEntryPoint Function pointer to the MM Entry point.
187 @retval EFI_SUCCESS Success.
191 MmFoundationEntryRegister (
192 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
193 IN EFI_MM_ENTRY_POINT MmEntryPoint
196 // store the entry point in a global
197 mMmEntryPoint
= MmEntryPoint
;
202 This function is the main entry point for an MM handler dispatch
203 or communicate-based callback.
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
212 @param CommBufferSize The size of the CommBuffer.
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
229 ASSERT (Context
== NULL
);
230 ASSERT (CommBuffer
== NULL
);
231 ASSERT (CommBufferSize
== NULL
);
233 CpuNumber
= mMmst
->CurrentlyExecutingCpu
;
234 if (PerCpuGuidedEventContext
[CpuNumber
] == NULL
) {
235 return EFI_NOT_FOUND
;
240 "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
241 PerCpuGuidedEventContext
[CpuNumber
],
242 PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
245 Status
= mMmst
->MmiManage (
246 &PerCpuGuidedEventContext
[CpuNumber
]->HeaderGuid
,
248 PerCpuGuidedEventContext
[CpuNumber
]->Data
,
249 &PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
252 if (Status
!= EFI_SUCCESS
) {
253 DEBUG ((DEBUG_WARN
, "Unable to manage Guided Event - %d\n", Status
));