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/ArmStdSmc.h>
27 #include "StandaloneMmCpu.h"
31 MmFoundationEntryRegister (
32 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
33 IN EFI_MM_ENTRY_POINT MmEntryPoint
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.
47 EFI_MM_COMMUNICATE_HEADER
**PerCpuGuidedEventContext
= NULL
;
49 // Descriptor with whereabouts of memory used for communication with the normal world
50 EFI_MMRAM_DESCRIPTOR mNsCommBuffer
;
52 MP_INFORMATION_HOB_DATA
*mMpInformationHobData
;
54 EFI_MM_CONFIGURATION_PROTOCOL mMmConfig
= {
56 MmFoundationEntryRegister
59 STATIC EFI_MM_ENTRY_POINT mMmEntryPoint
= NULL
;
62 The PI Standalone MM entry point for the TF-A CPU driver.
64 @param [in] EventId The event Id.
65 @param [in] CpuNumber The CPU number.
66 @param [in] NsCommBufferAddr Address of the NS common buffer.
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.
75 PiMmStandaloneArmTfCpuDriverEntry (
78 IN UINTN NsCommBufferAddr
81 EFI_MM_COMMUNICATE_HEADER
*GuidedEventContext
;
82 EFI_MM_ENTRY_CONTEXT MmEntryPointContext
;
84 UINTN NsCommBufferSize
;
86 DEBUG ((DEBUG_INFO
, "Received event - 0x%x on cpu %d\n", EventId
, CpuNumber
));
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.
94 if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64
!= EventId
) {
95 DEBUG ((DEBUG_INFO
, "UnRecognized Event - 0x%x\n", EventId
));
96 return EFI_INVALID_PARAMETER
;
99 // Perform parameter validation of NsCommBufferAddr
100 if (NsCommBufferAddr
== (UINTN
)NULL
) {
101 return EFI_INVALID_PARAMETER
;
104 if (NsCommBufferAddr
< mNsCommBuffer
.PhysicalStart
) {
105 return EFI_ACCESS_DENIED
;
108 if ((NsCommBufferAddr
+ sizeof (EFI_MM_COMMUNICATE_HEADER
)) >=
109 (mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
)) {
110 return EFI_INVALID_PARAMETER
;
113 // Find out the size of the buffer passed
114 NsCommBufferSize
= ((EFI_MM_COMMUNICATE_HEADER
*) NsCommBufferAddr
)->MessageLength
+
115 sizeof (EFI_MM_COMMUNICATE_HEADER
);
117 // perform bounds check.
118 if (NsCommBufferAddr
+ NsCommBufferSize
>=
119 mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
) {
120 return EFI_ACCESS_DENIED
;
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
,
129 (VOID
**) &GuidedEventContext
132 if (Status
!= EFI_SUCCESS
) {
133 DEBUG ((DEBUG_INFO
, "Mem alloc failed - 0x%x\n", EventId
));
134 return EFI_OUT_OF_RESOURCES
;
137 // X1 contains the VA of the normal world memory accessible from
139 CopyMem (GuidedEventContext
, (CONST VOID
*) NsCommBufferAddr
, NsCommBufferSize
);
141 // Stash the pointer to the allocated Event Context for this CPU
142 PerCpuGuidedEventContext
[CpuNumber
] = GuidedEventContext
;
144 ZeroMem (&MmEntryPointContext
, sizeof (EFI_MM_ENTRY_CONTEXT
));
146 MmEntryPointContext
.CurrentlyExecutingCpu
= CpuNumber
;
147 MmEntryPointContext
.NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
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
;
155 if (mMmEntryPoint
== NULL
) {
156 DEBUG ((DEBUG_INFO
, "Mm Entry point Not Found\n"));
157 return EFI_UNSUPPORTED
;
160 mMmEntryPoint (&MmEntryPointContext
);
162 // Free the memory allocation done earlier and reset the per-cpu context
163 ASSERT (GuidedEventContext
);
164 CopyMem ((VOID
*)NsCommBufferAddr
, (CONST VOID
*) GuidedEventContext
, NsCommBufferSize
);
166 Status
= mMmst
->MmFreePool ((VOID
*) GuidedEventContext
);
167 if (Status
!= EFI_SUCCESS
) {
168 return EFI_OUT_OF_RESOURCES
;
170 PerCpuGuidedEventContext
[CpuNumber
] = NULL
;
176 Registers the MM foundation entry point.
178 @param [in] This Pointer to the MM Configuration protocol.
179 @param [in] MmEntryPoint Function pointer to the MM Entry point.
181 @retval EFI_SUCCESS Success.
185 MmFoundationEntryRegister (
186 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
187 IN EFI_MM_ENTRY_POINT MmEntryPoint
190 // store the entry point in a global
191 mMmEntryPoint
= MmEntryPoint
;
196 This function is the main entry point for an MM handler dispatch
197 or communicate-based callback.
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.
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
220 ASSERT (Context
== NULL
);
221 ASSERT (CommBuffer
== NULL
);
222 ASSERT (CommBufferSize
== NULL
);
224 CpuNumber
= mMmst
->CurrentlyExecutingCpu
;
225 if (PerCpuGuidedEventContext
[CpuNumber
] == NULL
) {
226 return EFI_NOT_FOUND
;
229 DEBUG ((DEBUG_INFO
, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
230 PerCpuGuidedEventContext
[CpuNumber
],
231 PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
));
233 Status
= mMmst
->MmiManage (
234 &PerCpuGuidedEventContext
[CpuNumber
]->HeaderGuid
,
236 PerCpuGuidedEventContext
[CpuNumber
]->Data
,
237 &PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
240 if (Status
!= EFI_SUCCESS
) {
241 DEBUG ((DEBUG_WARN
, "Unable to manage Guided Event - %d\n", Status
));