]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StandaloneMmPkg / Drivers / StandaloneMmCpu / AArch64 / EventHandle.c
CommitLineData
275d4bd4
SV
1/** @file\r
2\r
3 Copyright (c) 2016 HP Development Company, L.P.\r
4 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.\r
5\r
86094561 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
275d4bd4
SV
7\r
8**/\r
9\r
10#include <Base.h>\r
11#include <Pi/PiMmCis.h>\r
12\r
13\r
14#include <Library/ArmSvcLib.h>\r
15#include <Library/ArmLib.h>\r
16#include <Library/BaseMemoryLib.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/HobLib.h>\r
19\r
20#include <Protocol/DebugSupport.h> // for EFI_SYSTEM_CONTEXT\r
21\r
22#include <Guid/ZeroGuid.h>\r
23#include <Guid/MmramMemoryReserve.h>\r
24\r
25#include <IndustryStandard/ArmStdSmc.h>\r
26\r
27#include "StandaloneMmCpu.h"\r
28\r
29EFI_STATUS\r
30EFIAPI\r
31MmFoundationEntryRegister (\r
32 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,\r
33 IN EFI_MM_ENTRY_POINT MmEntryPoint\r
34 );\r
35\r
36//\r
37// On ARM platforms every event is expected to have a GUID associated with\r
38// it. It will be used by the MM Entry point to find the handler for the\r
39// event. It will either be populated in a EFI_MM_COMMUNICATE_HEADER by the\r
40// caller of the event (e.g. MM_COMMUNICATE SMC) or by the CPU driver\r
41// (e.g. during an asynchronous event). In either case, this context is\r
42// maintained in an array which has an entry for each CPU. The pointer to this\r
43// array is held in PerCpuGuidedEventContext. Memory is allocated once the\r
44// number of CPUs in the system are made known through the\r
45// MP_INFORMATION_HOB_DATA.\r
46//\r
47EFI_MM_COMMUNICATE_HEADER **PerCpuGuidedEventContext = NULL;\r
48\r
49// Descriptor with whereabouts of memory used for communication with the normal world\r
50EFI_MMRAM_DESCRIPTOR mNsCommBuffer;\r
51\r
52MP_INFORMATION_HOB_DATA *mMpInformationHobData;\r
53\r
54EFI_MM_CONFIGURATION_PROTOCOL mMmConfig = {\r
55 0,\r
56 MmFoundationEntryRegister\r
57};\r
58\r
59STATIC EFI_MM_ENTRY_POINT mMmEntryPoint = NULL;\r
60\r
61EFI_STATUS\r
c8102727 62PiMmStandaloneArmTfCpuDriverEntry (\r
275d4bd4
SV
63 IN UINTN EventId,\r
64 IN UINTN CpuNumber,\r
65 IN UINTN NsCommBufferAddr\r
66 )\r
67{\r
68 EFI_MM_COMMUNICATE_HEADER *GuidedEventContext = NULL;\r
69 EFI_MM_ENTRY_CONTEXT MmEntryPointContext = {0};\r
70 EFI_STATUS Status;\r
71 UINTN NsCommBufferSize;\r
72\r
73 DEBUG ((DEBUG_INFO, "Received event - 0x%x on cpu %d\n", EventId, CpuNumber));\r
74\r
75 Status = EFI_SUCCESS;\r
76 //\r
77 // ARM TF passes SMC FID of the MM_COMMUNICATE interface as the Event ID upon\r
78 // receipt of a synchronous MM request. Use the Event ID to distinguish\r
79 // between synchronous and asynchronous events.\r
80 //\r
81 if (ARM_SMC_ID_MM_COMMUNICATE_AARCH64 != EventId) {\r
82 DEBUG ((DEBUG_INFO, "UnRecognized Event - 0x%x\n", EventId));\r
83 return EFI_INVALID_PARAMETER;\r
84 }\r
85\r
86 // Perform parameter validation of NsCommBufferAddr\r
87 if (NsCommBufferAddr && (NsCommBufferAddr < mNsCommBuffer.PhysicalStart))\r
88 return EFI_ACCESS_DENIED;\r
89\r
90 if ((NsCommBufferAddr + sizeof (EFI_MM_COMMUNICATE_HEADER)) >=\r
91 (mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize))\r
92 return EFI_INVALID_PARAMETER;\r
93\r
94 // Find out the size of the buffer passed\r
95 NsCommBufferSize = ((EFI_MM_COMMUNICATE_HEADER *) NsCommBufferAddr)->MessageLength +\r
96 sizeof (EFI_MM_COMMUNICATE_HEADER);\r
97\r
98 // perform bounds check.\r
99 if (NsCommBufferAddr + NsCommBufferSize >=\r
100 mNsCommBuffer.PhysicalStart + mNsCommBuffer.PhysicalSize)\r
101 return EFI_ACCESS_DENIED;\r
102\r
103\r
104 // Now that the secure world can see the normal world buffer, allocate\r
105 // memory to copy the communication buffer to the secure world.\r
106 Status = mMmst->MmAllocatePool (\r
107 EfiRuntimeServicesData,\r
108 NsCommBufferSize,\r
109 (VOID **) &GuidedEventContext\r
110 );\r
111\r
112 if (Status != EFI_SUCCESS) {\r
113 DEBUG ((DEBUG_INFO, "Mem alloc failed - 0x%x\n", EventId));\r
114 return EFI_OUT_OF_RESOURCES;\r
115 }\r
116\r
117 // X1 contains the VA of the normal world memory accessible from\r
118 // S-EL0\r
119 CopyMem (GuidedEventContext, (CONST VOID *) NsCommBufferAddr, NsCommBufferSize);\r
120\r
121 // Stash the pointer to the allocated Event Context for this CPU\r
122 PerCpuGuidedEventContext[CpuNumber] = GuidedEventContext;\r
123\r
124 MmEntryPointContext.CurrentlyExecutingCpu = CpuNumber;\r
125 MmEntryPointContext.NumberOfCpus = mMpInformationHobData->NumberOfProcessors;\r
126\r
127 // Populate the MM system table with MP and state information\r
128 mMmst->CurrentlyExecutingCpu = CpuNumber;\r
129 mMmst->NumberOfCpus = mMpInformationHobData->NumberOfProcessors;\r
130 mMmst->CpuSaveStateSize = 0;\r
131 mMmst->CpuSaveState = NULL;\r
132\r
133 if (mMmEntryPoint == NULL) {\r
134 DEBUG ((DEBUG_INFO, "Mm Entry point Not Found\n"));\r
135 return EFI_UNSUPPORTED;\r
136 }\r
137\r
138 mMmEntryPoint (&MmEntryPointContext);\r
139\r
140 // Free the memory allocation done earlier and reset the per-cpu context\r
141 ASSERT (GuidedEventContext);\r
142 CopyMem ((VOID *)NsCommBufferAddr, (CONST VOID *) GuidedEventContext, NsCommBufferSize);\r
143\r
144 Status = mMmst->MmFreePool ((VOID *) GuidedEventContext);\r
145 if (Status != EFI_SUCCESS) {\r
146 return EFI_OUT_OF_RESOURCES;\r
147 }\r
148 PerCpuGuidedEventContext[CpuNumber] = NULL;\r
149\r
150 return Status;\r
151}\r
152\r
153EFI_STATUS\r
154EFIAPI\r
155MmFoundationEntryRegister (\r
156 IN CONST EFI_MM_CONFIGURATION_PROTOCOL *This,\r
157 IN EFI_MM_ENTRY_POINT MmEntryPoint\r
158 )\r
159{\r
160 // store the entry point in a global\r
161 mMmEntryPoint = MmEntryPoint;\r
162 return EFI_SUCCESS;\r
163}\r
164\r
165/**\r
166 This function is the main entry point for an MM handler dispatch\r
167 or communicate-based callback.\r
168\r
169 @param DispatchHandle The unique handle assigned to this handler by MmiHandlerRegister().\r
170 @param Context Points to an optional handler context which was specified when the handler was registered.\r
171 @param CommBuffer A pointer to a collection of data in memory that will\r
172 be conveyed from a non-MM environment into an MM environment.\r
173 @param CommBufferSize The size of the CommBuffer.\r
174\r
175 @return Status Code\r
176\r
177**/\r
178EFI_STATUS\r
179EFIAPI\r
180PiMmCpuTpFwRootMmiHandler (\r
181 IN EFI_HANDLE DispatchHandle,\r
182 IN CONST VOID *Context, OPTIONAL\r
183 IN OUT VOID *CommBuffer, OPTIONAL\r
184 IN OUT UINTN *CommBufferSize OPTIONAL\r
185 )\r
186{\r
187 EFI_STATUS Status;\r
188 UINTN CpuNumber;\r
189\r
190 ASSERT (Context == NULL);\r
191 ASSERT (CommBuffer == NULL);\r
192 ASSERT (CommBufferSize == NULL);\r
193\r
194 CpuNumber = mMmst->CurrentlyExecutingCpu;\r
195 if (!PerCpuGuidedEventContext[CpuNumber])\r
196 return EFI_NOT_FOUND;\r
197\r
198 DEBUG ((DEBUG_INFO, "CommBuffer - 0x%x, CommBufferSize - 0x%x\n",\r
199 PerCpuGuidedEventContext[CpuNumber],\r
200 PerCpuGuidedEventContext[CpuNumber]->MessageLength));\r
201\r
202 Status = mMmst->MmiManage (\r
203 &PerCpuGuidedEventContext[CpuNumber]->HeaderGuid,\r
204 NULL,\r
205 PerCpuGuidedEventContext[CpuNumber]->Data,\r
206 &PerCpuGuidedEventContext[CpuNumber]->MessageLength\r
207 );\r
208\r
209 if (Status != EFI_SUCCESS) {\r
210 DEBUG ((DEBUG_WARN, "Unable to manage Guided Event - %d\n", Status));\r
211 }\r
212\r
213 return Status;\r
214}\r