3 Copyright (c) 2016 HP Development Company, L.P.
4 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Pi/PiMmCis.h>
20 #include <Library/ArmSvcLib.h>
21 #include <Library/ArmLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/HobLib.h>
26 #include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT
28 #include <Guid/ZeroGuid.h>
29 #include <Guid/MmramMemoryReserve.h>
31 #include <IndustryStandard/ArmStdSmc.h>
33 #include "StandaloneMmCpu.h"
37 MmFoundationEntryRegister (
38 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
39 IN EFI_MM_ENTRY_POINT MmEntryPoint
43 // On ARM platforms every event is expected to have a GUID associated with
44 // it. It will be used by the MM Entry point to find the handler for the
45 // event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the
46 // caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver
47 // (e.g. during an asynchronous event). In either case, this context is
48 // maintained in an array which has an entry for each CPU. The pointer to this
49 // array is held in PerCpuGuidedEventContext. Memory is allocated once the
50 // number of CPUs in the system are made known through the
51 // MP_INFORMATION_HOB_DATA.
53 EFI_MM_COMMUNICATE_HEADER
**PerCpuGuidedEventContext
= NULL
;
55 // Descriptor with whereabouts of memory used for communication with the normal world
56 EFI_MMRAM_DESCRIPTOR mNsCommBuffer
;
58 MP_INFORMATION_HOB_DATA
*mMpInformationHobData
;
60 EFI_MM_CONFIGURATION_PROTOCOL mMmConfig
= {
62 MmFoundationEntryRegister
65 STATIC EFI_MM_ENTRY_POINT mMmEntryPoint
= NULL
;
68 PiMmStandloneArmTfCpuDriverEntry (
71 IN UINTN NsCommBufferAddr
74 EFI_MM_COMMUNICATE_HEADER
*GuidedEventContext
= NULL
;
75 EFI_MM_ENTRY_CONTEXT MmEntryPointContext
= {0};
77 UINTN NsCommBufferSize
;
79 DEBUG ((DEBUG_INFO
, "Received event - 0x%x on cpu %d\n", EventId
, CpuNumber
));
83 // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon
84 // receipt of a synchronous MM request. Use the Event ID to distinguish
85 // between synchronous and asynchronous events.
87 if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64
!= EventId
) {
88 DEBUG ((DEBUG_INFO
, "UnRecognized Event - 0x%x\n", EventId
));
89 return EFI_INVALID_PARAMETER
;
92 // Perform parameter validation of NsCommBufferAddr
93 if (NsCommBufferAddr
&& (NsCommBufferAddr
< mNsCommBuffer
.PhysicalStart
))
94 return EFI_ACCESS_DENIED
;
96 if ((NsCommBufferAddr
+ sizeof (EFI_MM_COMMUNICATE_HEADER
)) >=
97 (mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
))
98 return EFI_INVALID_PARAMETER
;
100 // Find out the size of the buffer passed
101 NsCommBufferSize
= ((EFI_MM_COMMUNICATE_HEADER
*) NsCommBufferAddr
)->MessageLength
+
102 sizeof (EFI_MM_COMMUNICATE_HEADER
);
104 // perform bounds check.
105 if (NsCommBufferAddr
+ NsCommBufferSize
>=
106 mNsCommBuffer
.PhysicalStart
+ mNsCommBuffer
.PhysicalSize
)
107 return EFI_ACCESS_DENIED
;
110 // Now that the secure world can see the normal world buffer, allocate
111 // memory to copy the communication buffer to the secure world.
112 Status
= mMmst
->MmAllocatePool (
113 EfiRuntimeServicesData
,
115 (VOID
**) &GuidedEventContext
118 if (Status
!= EFI_SUCCESS
) {
119 DEBUG ((DEBUG_INFO
, "Mem alloc failed - 0x%x\n", EventId
));
120 return EFI_OUT_OF_RESOURCES
;
123 // X1 contains the VA of the normal world memory accessible from
125 CopyMem (GuidedEventContext
, (CONST VOID
*) NsCommBufferAddr
, NsCommBufferSize
);
127 // Stash the pointer to the allocated Event Context for this CPU
128 PerCpuGuidedEventContext
[CpuNumber
] = GuidedEventContext
;
130 MmEntryPointContext
.CurrentlyExecutingCpu
= CpuNumber
;
131 MmEntryPointContext
.NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
133 // Populate the MM system table with MP and state information
134 mMmst
->CurrentlyExecutingCpu
= CpuNumber
;
135 mMmst
->NumberOfCpus
= mMpInformationHobData
->NumberOfProcessors
;
136 mMmst
->CpuSaveStateSize
= 0;
137 mMmst
->CpuSaveState
= NULL
;
139 if (mMmEntryPoint
== NULL
) {
140 DEBUG ((DEBUG_INFO
, "Mm Entry point Not Found\n"));
141 return EFI_UNSUPPORTED
;
144 mMmEntryPoint (&MmEntryPointContext
);
146 // Free the memory allocation done earlier and reset the per-cpu context
147 ASSERT (GuidedEventContext
);
148 CopyMem ((VOID
*)NsCommBufferAddr
, (CONST VOID
*) GuidedEventContext
, NsCommBufferSize
);
150 Status
= mMmst
->MmFreePool ((VOID
*) GuidedEventContext
);
151 if (Status
!= EFI_SUCCESS
) {
152 return EFI_OUT_OF_RESOURCES
;
154 PerCpuGuidedEventContext
[CpuNumber
] = NULL
;
161 MmFoundationEntryRegister (
162 IN CONST EFI_MM_CONFIGURATION_PROTOCOL
*This
,
163 IN EFI_MM_ENTRY_POINT MmEntryPoint
166 // store the entry point in a global
167 mMmEntryPoint
= MmEntryPoint
;
172 This function is the main entry point for an MM handler dispatch
173 or communicate-based callback.
175 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().
176 @param Context Points to an optional handler context which was specified when the handler was registered.
177 @param CommBuffer A pointer to a collection of data in memory that will
178 be conveyed from a non-MM environment into an MM environment.
179 @param CommBufferSize The size of the CommBuffer.
186 PiMmCpuTpFwRootMmiHandler (
187 IN EFI_HANDLE DispatchHandle
,
188 IN CONST VOID
*Context
, OPTIONAL
189 IN OUT VOID
*CommBuffer
, OPTIONAL
190 IN OUT UINTN
*CommBufferSize OPTIONAL
196 ASSERT (Context
== NULL
);
197 ASSERT (CommBuffer
== NULL
);
198 ASSERT (CommBufferSize
== NULL
);
200 CpuNumber
= mMmst
->CurrentlyExecutingCpu
;
201 if (!PerCpuGuidedEventContext
[CpuNumber
])
202 return EFI_NOT_FOUND
;
204 DEBUG ((DEBUG_INFO
, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",
205 PerCpuGuidedEventContext
[CpuNumber
],
206 PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
));
208 Status
= mMmst
->MmiManage (
209 &PerCpuGuidedEventContext
[CpuNumber
]->HeaderGuid
,
211 PerCpuGuidedEventContext
[CpuNumber
]->Data
,
212 &PerCpuGuidedEventContext
[CpuNumber
]->MessageLength
215 if (Status
!= EFI_SUCCESS
) {
216 DEBUG ((DEBUG_WARN
, "Unable to manage Guided Event - %d\n", Status
));