]> git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Acpi/Arm/AcpiMcfgLibArm/McfgGenerator.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / DynamicTablesPkg / Library / Acpi / Arm / AcpiMcfgLibArm / McfgGenerator.c
1 /** @file
2 MCFG Table Generator
3
4 Copyright (c) 2017 - 2019, ARM Limited. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 @par Reference(s):
8 - PCI Firmware Specification - Revision 3.2, January 26, 2015.
9
10 **/
11
12 #include <IndustryStandard/MemoryMappedConfigurationSpaceAccessTable.h>
13 #include <Library/AcpiLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Protocol/AcpiTable.h>
17
18 // Module specific include files.
19 #include <AcpiTableGenerator.h>
20 #include <ConfigurationManagerObject.h>
21 #include <ConfigurationManagerHelper.h>
22 #include <Library/TableHelperLib.h>
23 #include <Protocol/ConfigurationManagerProtocol.h>
24
25 /** ARM standard MCFG Generator
26
27 Requirements:
28 The following Configuration Manager Object(s) are required by
29 this Generator:
30 - EArmObjPciConfigSpaceInfo
31 */
32
33 #pragma pack(1)
34
35 /** This typedef is used to shorten the name of the MCFG Table
36 header structure.
37 */
38 typedef
39 EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_BASE_ADDRESS_TABLE_HEADER
40 MCFG_TABLE;
41
42 /** This typedef is used to shorten the name of the Enhanced
43 Configuration Space address structure.
44 */
45 typedef
46 EFI_ACPI_MEMORY_MAPPED_ENHANCED_CONFIGURATION_SPACE_BASE_ADDRESS_ALLOCATION_STRUCTURE
47 MCFG_CFG_SPACE_ADDR;
48
49 #pragma pack()
50
51 /** Retrieve the PCI Configuration Space Information.
52 */
53 GET_OBJECT_LIST (
54 EObjNameSpaceArm,
55 EArmObjPciConfigSpaceInfo,
56 CM_ARM_PCI_CONFIG_SPACE_INFO
57 );
58
59 /** Add the PCI Enhanced Configuration Space Information to the MCFG Table.
60
61 @param [in] Mcfg Pointer to MCFG Table.
62 @param [in] PciCfgSpaceOffset Offset for the PCI Configuration Space
63 Info structure in the MCFG Table.
64 @param [in] PciCfgSpaceInfoList Pointer to the PCI Configuration Space
65 Info List.
66 @param [in] PciCfgSpaceCount Count of PCI Configuration Space Info.
67 **/
68 STATIC
69 VOID
70 AddPciConfigurationSpaceList (
71 IN MCFG_TABLE *CONST Mcfg,
72 IN CONST UINT32 PciCfgSpaceOffset,
73 IN CONST CM_ARM_PCI_CONFIG_SPACE_INFO *PciCfgSpaceInfoList,
74 IN UINT32 PciCfgSpaceCount
75 )
76 {
77 MCFG_CFG_SPACE_ADDR *PciCfgSpace;
78
79 ASSERT (Mcfg != NULL);
80 ASSERT (PciCfgSpaceInfoList != NULL);
81
82 PciCfgSpace = (MCFG_CFG_SPACE_ADDR *)((UINT8 *)Mcfg + PciCfgSpaceOffset);
83
84 while (PciCfgSpaceCount-- != 0) {
85 // Add PCI Configuration Space entry
86 PciCfgSpace->BaseAddress = PciCfgSpaceInfoList->BaseAddress;
87 PciCfgSpace->PciSegmentGroupNumber =
88 PciCfgSpaceInfoList->PciSegmentGroupNumber;
89 PciCfgSpace->StartBusNumber = PciCfgSpaceInfoList->StartBusNumber;
90 PciCfgSpace->EndBusNumber = PciCfgSpaceInfoList->EndBusNumber;
91 PciCfgSpace->Reserved = EFI_ACPI_RESERVED_DWORD;
92 PciCfgSpace++;
93 PciCfgSpaceInfoList++;
94 }
95 }
96
97 /** Construct the MCFG ACPI table.
98
99 This function invokes the Configuration Manager protocol interface
100 to get the required hardware information for generating the ACPI
101 table.
102
103 If this function allocates any resources then they must be freed
104 in the FreeXXXXTableResources function.
105
106 @param [in] This Pointer to the table generator.
107 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
108 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
109 Protocol Interface.
110 @param [out] Table Pointer to the constructed ACPI Table.
111
112 @retval EFI_SUCCESS Table generated successfully.
113 @retval EFI_INVALID_PARAMETER A parameter is invalid.
114 @retval EFI_NOT_FOUND The required object was not found.
115 @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration
116 Manager is less than the Object size for the
117 requested object.
118 **/
119 STATIC
120 EFI_STATUS
121 EFIAPI
122 BuildMcfgTable (
123 IN CONST ACPI_TABLE_GENERATOR *CONST This,
124 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO *CONST AcpiTableInfo,
125 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
126 OUT EFI_ACPI_DESCRIPTION_HEADER **CONST Table
127 )
128 {
129 EFI_STATUS Status;
130 UINT32 TableSize;
131 UINT32 ConfigurationSpaceCount;
132 CM_ARM_PCI_CONFIG_SPACE_INFO *PciConfigSpaceInfoList;
133 MCFG_TABLE *Mcfg;
134
135 ASSERT (This != NULL);
136 ASSERT (AcpiTableInfo != NULL);
137 ASSERT (CfgMgrProtocol != NULL);
138 ASSERT (Table != NULL);
139 ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
140 ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
141
142 if ((AcpiTableInfo->AcpiTableRevision < This->MinAcpiTableRevision) ||
143 (AcpiTableInfo->AcpiTableRevision > This->AcpiTableRevision))
144 {
145 DEBUG ((
146 DEBUG_ERROR,
147 "ERROR: MCFG: Requested table revision = %d, is not supported."
148 "Supported table revision: Minimum = %d, Maximum = %d\n",
149 AcpiTableInfo->AcpiTableRevision,
150 This->MinAcpiTableRevision,
151 This->AcpiTableRevision
152 ));
153 return EFI_INVALID_PARAMETER;
154 }
155
156 *Table = NULL;
157 Status = GetEArmObjPciConfigSpaceInfo (
158 CfgMgrProtocol,
159 CM_NULL_TOKEN,
160 &PciConfigSpaceInfoList,
161 &ConfigurationSpaceCount
162 );
163 if (EFI_ERROR (Status)) {
164 DEBUG ((
165 DEBUG_ERROR,
166 "ERROR: MCFG: Failed to get PCI Configuration Space Information." \
167 " Status = %r\n",
168 Status
169 ));
170 goto error_handler;
171 }
172
173 if (ConfigurationSpaceCount == 0) {
174 DEBUG ((
175 DEBUG_ERROR,
176 "ERROR: MCFG: Configuration Space Count = %d\n",
177 ConfigurationSpaceCount
178 ));
179 Status = EFI_INVALID_PARAMETER;
180 ASSERT (ConfigurationSpaceCount != 0);
181 goto error_handler;
182 }
183
184 DEBUG ((
185 DEBUG_INFO,
186 "MCFG: Configuration Space Count = %d\n",
187 ConfigurationSpaceCount
188 ));
189
190 // Calculate the MCFG Table Size
191 TableSize = sizeof (MCFG_TABLE) +
192 ((sizeof (MCFG_CFG_SPACE_ADDR) * ConfigurationSpaceCount));
193
194 *Table = (EFI_ACPI_DESCRIPTION_HEADER *)AllocateZeroPool (TableSize);
195 if (*Table == NULL) {
196 Status = EFI_OUT_OF_RESOURCES;
197 DEBUG ((
198 DEBUG_ERROR,
199 "ERROR: MCFG: Failed to allocate memory for MCFG Table, Size = %d," \
200 " Status = %r\n",
201 TableSize,
202 Status
203 ));
204 goto error_handler;
205 }
206
207 Mcfg = (MCFG_TABLE *)*Table;
208 DEBUG ((
209 DEBUG_INFO,
210 "MCFG: Mcfg = 0x%p TableSize = 0x%x\n",
211 Mcfg,
212 TableSize
213 ));
214
215 Status = AddAcpiHeader (
216 CfgMgrProtocol,
217 This,
218 &Mcfg->Header,
219 AcpiTableInfo,
220 TableSize
221 );
222 if (EFI_ERROR (Status)) {
223 DEBUG ((
224 DEBUG_ERROR,
225 "ERROR: MCFG: Failed to add ACPI header. Status = %r\n",
226 Status
227 ));
228 goto error_handler;
229 }
230
231 Mcfg->Reserved = EFI_ACPI_RESERVED_QWORD;
232
233 AddPciConfigurationSpaceList (
234 Mcfg,
235 sizeof (MCFG_TABLE),
236 PciConfigSpaceInfoList,
237 ConfigurationSpaceCount
238 );
239
240 return EFI_SUCCESS;
241
242 error_handler:
243 if (*Table != NULL) {
244 FreePool (*Table);
245 *Table = NULL;
246 }
247
248 return Status;
249 }
250
251 /** Free any resources allocated for constructing the MCFG
252
253 @param [in] This Pointer to the table generator.
254 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
255 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
256 Protocol Interface.
257 @param [in, out] Table Pointer to the ACPI Table.
258
259 @retval EFI_SUCCESS The resources were freed successfully.
260 @retval EFI_INVALID_PARAMETER The table pointer is NULL or invalid.
261 **/
262 STATIC
263 EFI_STATUS
264 FreeMcfgTableResources (
265 IN CONST ACPI_TABLE_GENERATOR *CONST This,
266 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO *CONST AcpiTableInfo,
267 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
268 IN OUT EFI_ACPI_DESCRIPTION_HEADER **CONST Table
269 )
270 {
271 ASSERT (This != NULL);
272 ASSERT (AcpiTableInfo != NULL);
273 ASSERT (CfgMgrProtocol != NULL);
274 ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
275 ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
276
277 if ((Table == NULL) || (*Table == NULL)) {
278 DEBUG ((DEBUG_ERROR, "ERROR: MCFG: Invalid Table Pointer\n"));
279 ASSERT ((Table != NULL) && (*Table != NULL));
280 return EFI_INVALID_PARAMETER;
281 }
282
283 FreePool (*Table);
284 *Table = NULL;
285 return EFI_SUCCESS;
286 }
287
288 /** This macro defines the MCFG Table Generator revision.
289 */
290 #define MCFG_GENERATOR_REVISION CREATE_REVISION (1, 0)
291
292 /** The interface for the MCFG Table Generator.
293 */
294 STATIC
295 CONST
296 ACPI_TABLE_GENERATOR McfgGenerator = {
297 // Generator ID
298 CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdMcfg),
299 // Generator Description
300 L"ACPI.STD.MCFG.GENERATOR",
301 // ACPI Table Signature
302 EFI_ACPI_6_2_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE,
303 // ACPI Table Revision supported by this Generator
304 EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION,
305 // Minimum supported ACPI Table Revision
306 EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION,
307 // Creator ID
308 TABLE_GENERATOR_CREATOR_ID_ARM,
309 // Creator Revision
310 MCFG_GENERATOR_REVISION,
311 // Build Table function
312 BuildMcfgTable,
313 // Free Resource function
314 FreeMcfgTableResources,
315 // Extended build function not needed
316 NULL,
317 // Extended build function not implemented by the generator.
318 // Hence extended free resource function is not required.
319 NULL
320 };
321
322 /** Register the Generator with the ACPI Table Factory.
323
324 @param [in] ImageHandle The handle to the image.
325 @param [in] SystemTable Pointer to the System Table.
326
327 @retval EFI_SUCCESS The Generator is registered.
328 @retval EFI_INVALID_PARAMETER A parameter is invalid.
329 @retval EFI_ALREADY_STARTED The Generator for the Table ID
330 is already registered.
331 **/
332 EFI_STATUS
333 EFIAPI
334 AcpiMcfgLibConstructor (
335 IN EFI_HANDLE ImageHandle,
336 IN EFI_SYSTEM_TABLE *SystemTable
337 )
338 {
339 EFI_STATUS Status;
340
341 Status = RegisterAcpiTableGenerator (&McfgGenerator);
342 DEBUG ((DEBUG_INFO, "MCFG: Register Generator. Status = %r\n", Status));
343 ASSERT_EFI_ERROR (Status);
344 return Status;
345 }
346
347 /** Deregister the Generator from the ACPI Table Factory.
348
349 @param [in] ImageHandle The handle to the image.
350 @param [in] SystemTable Pointer to the System Table.
351
352 @retval EFI_SUCCESS The Generator is deregistered.
353 @retval EFI_INVALID_PARAMETER A parameter is invalid.
354 @retval EFI_NOT_FOUND The Generator is not registered.
355 **/
356 EFI_STATUS
357 EFIAPI
358 AcpiMcfgLibDestructor (
359 IN EFI_HANDLE ImageHandle,
360 IN EFI_SYSTEM_TABLE *SystemTable
361 )
362 {
363 EFI_STATUS Status;
364
365 Status = DeregisterAcpiTableGenerator (&McfgGenerator);
366 DEBUG ((DEBUG_INFO, "MCFG: Deregister Generator. Status = %r\n", Status));
367 ASSERT_EFI_ERROR (Status);
368 return Status;
369 }