]> git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Acpi/Arm/AcpiMcfgLibArm/McfgGenerator.c
DynamicTablesPkg: Replace BSD License with BSD+Patent License
[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 DEBUG ((
145 DEBUG_ERROR,
146 "ERROR: MCFG: Requested table revision = %d, is not supported."
147 "Supported table revision: Minimum = %d, Maximum = %d\n",
148 AcpiTableInfo->AcpiTableRevision,
149 This->MinAcpiTableRevision,
150 This->AcpiTableRevision
151 ));
152 return EFI_INVALID_PARAMETER;
153 }
154
155 *Table = NULL;
156 Status = GetEArmObjPciConfigSpaceInfo (
157 CfgMgrProtocol,
158 CM_NULL_TOKEN,
159 &PciConfigSpaceInfoList,
160 &ConfigurationSpaceCount
161 );
162 if (EFI_ERROR (Status)) {
163 DEBUG ((DEBUG_ERROR,
164 "ERROR: MCFG: Failed to get PCI Configuration Space Information." \
165 " Status = %r\n",
166 Status
167 ));
168 goto error_handler;
169 }
170
171 if (ConfigurationSpaceCount == 0) {
172 DEBUG ((
173 DEBUG_ERROR,
174 "ERROR: MCFG: Configuration Space Count = %d\n",
175 ConfigurationSpaceCount
176 ));
177 Status = EFI_INVALID_PARAMETER;
178 ASSERT (ConfigurationSpaceCount != 0);
179 goto error_handler;
180 }
181
182 DEBUG ((
183 DEBUG_INFO,
184 "MCFG: Configuration Space Count = %d\n",
185 ConfigurationSpaceCount
186 ));
187
188 // Calculate the MCFG Table Size
189 TableSize = sizeof (MCFG_TABLE) +
190 ((sizeof (MCFG_CFG_SPACE_ADDR) * ConfigurationSpaceCount));
191
192 *Table = (EFI_ACPI_DESCRIPTION_HEADER*)AllocateZeroPool (TableSize);
193 if (*Table == NULL) {
194 Status = EFI_OUT_OF_RESOURCES;
195 DEBUG ((
196 DEBUG_ERROR,
197 "ERROR: MCFG: Failed to allocate memory for MCFG Table, Size = %d," \
198 " Status = %r\n",
199 TableSize,
200 Status
201 ));
202 goto error_handler;
203 }
204
205 Mcfg = (MCFG_TABLE*)*Table;
206 DEBUG ((
207 DEBUG_INFO,
208 "MCFG: Mcfg = 0x%p TableSize = 0x%x\n",
209 Mcfg,
210 TableSize
211 ));
212
213 Status = AddAcpiHeader (
214 CfgMgrProtocol,
215 This,
216 &Mcfg->Header,
217 AcpiTableInfo,
218 TableSize
219 );
220 if (EFI_ERROR (Status)) {
221 DEBUG ((
222 DEBUG_ERROR,
223 "ERROR: MCFG: Failed to add ACPI header. Status = %r\n",
224 Status
225 ));
226 goto error_handler;
227 }
228
229 Mcfg->Reserved = EFI_ACPI_RESERVED_QWORD;
230
231 AddPciConfigurationSpaceList (
232 Mcfg,
233 sizeof (MCFG_TABLE),
234 PciConfigSpaceInfoList,
235 ConfigurationSpaceCount
236 );
237
238 return EFI_SUCCESS;
239
240 error_handler:
241 if (*Table != NULL) {
242 FreePool (*Table);
243 *Table = NULL;
244 }
245 return Status;
246 }
247
248 /** Free any resources allocated for constructing the MCFG
249
250 @param [in] This Pointer to the table generator.
251 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
252 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
253 Protocol Interface.
254 @param [in, out] Table Pointer to the ACPI Table.
255
256 @retval EFI_SUCCESS The resources were freed successfully.
257 @retval EFI_INVALID_PARAMETER The table pointer is NULL or invalid.
258 **/
259 STATIC
260 EFI_STATUS
261 FreeMcfgTableResources (
262 IN CONST ACPI_TABLE_GENERATOR * CONST This,
263 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO * CONST AcpiTableInfo,
264 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL * CONST CfgMgrProtocol,
265 IN OUT EFI_ACPI_DESCRIPTION_HEADER ** CONST Table
266 )
267 {
268 ASSERT (This != NULL);
269 ASSERT (AcpiTableInfo != NULL);
270 ASSERT (CfgMgrProtocol != NULL);
271 ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
272 ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
273
274 if ((Table == NULL) || (*Table == NULL)) {
275 DEBUG ((DEBUG_ERROR, "ERROR: MCFG: Invalid Table Pointer\n"));
276 ASSERT ((Table != NULL) && (*Table != NULL));
277 return EFI_INVALID_PARAMETER;
278 }
279
280 FreePool (*Table);
281 *Table = NULL;
282 return EFI_SUCCESS;
283 }
284
285 /** This macro defines the MCFG Table Generator revision.
286 */
287 #define MCFG_GENERATOR_REVISION CREATE_REVISION (1, 0)
288
289 /** The interface for the MCFG Table Generator.
290 */
291 STATIC
292 CONST
293 ACPI_TABLE_GENERATOR McfgGenerator = {
294 // Generator ID
295 CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdMcfg),
296 // Generator Description
297 L"ACPI.STD.MCFG.GENERATOR",
298 // ACPI Table Signature
299 EFI_ACPI_6_2_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE,
300 // ACPI Table Revision supported by this Generator
301 EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION,
302 // Minimum supported ACPI Table Revision
303 EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION,
304 // Creator ID
305 TABLE_GENERATOR_CREATOR_ID_ARM,
306 // Creator Revision
307 MCFG_GENERATOR_REVISION,
308 // Build Table function
309 BuildMcfgTable,
310 // Free Resource function
311 FreeMcfgTableResources,
312 // Extended build function not needed
313 NULL,
314 // Extended build function not implemented by the generator.
315 // Hence extended free resource function is not required.
316 NULL
317 };
318
319 /** Register the Generator with the ACPI Table Factory.
320
321 @param [in] ImageHandle The handle to the image.
322 @param [in] SystemTable Pointer to the System Table.
323
324 @retval EFI_SUCCESS The Generator is registered.
325 @retval EFI_INVALID_PARAMETER A parameter is invalid.
326 @retval EFI_ALREADY_STARTED The Generator for the Table ID
327 is already registered.
328 **/
329 EFI_STATUS
330 EFIAPI
331 AcpiMcfgLibConstructor (
332 IN CONST EFI_HANDLE ImageHandle,
333 IN EFI_SYSTEM_TABLE * CONST SystemTable
334 )
335 {
336 EFI_STATUS Status;
337 Status = RegisterAcpiTableGenerator (&McfgGenerator);
338 DEBUG ((DEBUG_INFO, "MCFG: Register Generator. Status = %r\n", Status));
339 ASSERT_EFI_ERROR (Status);
340 return Status;
341 }
342
343 /** Deregister the Generator from the ACPI Table Factory.
344
345 @param [in] ImageHandle The handle to the image.
346 @param [in] SystemTable Pointer to the System Table.
347
348 @retval EFI_SUCCESS The Generator is deregistered.
349 @retval EFI_INVALID_PARAMETER A parameter is invalid.
350 @retval EFI_NOT_FOUND The Generator is not registered.
351 **/
352 EFI_STATUS
353 EFIAPI
354 AcpiMcfgLibDestructor (
355 IN CONST EFI_HANDLE ImageHandle,
356 IN EFI_SYSTEM_TABLE * CONST SystemTable
357 )
358 {
359 EFI_STATUS Status;
360 Status = DeregisterAcpiTableGenerator (&McfgGenerator);
361 DEBUG ((DEBUG_INFO, "MCFG: Deregister Generator. Status = %r\n", Status));
362 ASSERT_EFI_ERROR (Status);
363 return Status;
364 }