]> git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Acpi/Arm/AcpiDbg2LibArm/Dbg2Generator.c
a7508d4a8834fd2038946a62de39e9cd0894bf79
[mirror_edk2.git] / DynamicTablesPkg / Library / Acpi / Arm / AcpiDbg2LibArm / Dbg2Generator.c
1 /** @file
2 DBG2 Table Generator
3
4 Copyright (c) 2017 - 2021, Arm Limited. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 @par Reference(s):
9 - Microsoft Debug Port Table 2 (DBG2) Specification - December 10, 2015.
10
11 **/
12
13 #include <IndustryStandard/DebugPort2Table.h>
14 #include <Library/AcpiLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/PL011UartLib.h>
18 #include <Protocol/AcpiTable.h>
19 #include <Protocol/SerialIo.h>
20
21 // Module specific include files.
22 #include <AcpiTableGenerator.h>
23 #include <ConfigurationManagerObject.h>
24 #include <ConfigurationManagerHelper.h>
25 #include <Library/SsdtSerialPortFixupLib.h>
26 #include <Library/TableHelperLib.h>
27 #include <Protocol/ConfigurationManagerProtocol.h>
28
29 /** ARM standard DBG2 Table Generator
30
31 Constructs the DBG2 table for PL011 or SBSA UART peripherals.
32
33 Requirements:
34 The following Configuration Manager Object(s) are required by
35 this Generator:
36 - EArmObjSerialDebugPortInfo
37 */
38
39 #pragma pack(1)
40
41 /** The number of debug ports represented by the Table.
42 */
43 #define DBG2_NUM_DEBUG_PORTS 1
44
45 /** The number of Generic Address Registers
46 presented in the debug device information.
47 */
48 #define DBG2_NUMBER_OF_GENERIC_ADDRESS_REGISTERS 1
49
50 /** The index for the debug port 0 in the Debug port information list.
51 */
52 #define INDEX_DBG_PORT0 0
53
54 /** A string representing the name of the debug port 0.
55 */
56 #define NAME_STR_DBG_PORT0 "COM0"
57
58 /** An UID representing the debug port 0.
59 */
60 #define UID_DBG_PORT0 0
61
62 /** The length of the namespace string.
63 */
64 #define DBG2_NAMESPACESTRING_FIELD_SIZE sizeof (NAME_STR_DBG_PORT0)
65
66 /** The PL011 UART address range length.
67 */
68 #define PL011_UART_LENGTH 0x1000
69
70 /** A structure that provides the OS with the required information
71 for initializing a debugger connection.
72 */
73 typedef struct {
74 /// The debug device information for the platform
75 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT Dbg2Device;
76
77 /// The base address register for the serial port
78 EFI_ACPI_6_2_GENERIC_ADDRESS_STRUCTURE BaseAddressRegister;
79
80 /// The address size
81 UINT32 AddressSize;
82
83 /// The debug port name string
84 UINT8 NameSpaceString[DBG2_NAMESPACESTRING_FIELD_SIZE];
85 } DBG2_DEBUG_DEVICE_INFORMATION;
86
87 /** A structure representing the information about the debug port(s)
88 available on the platform.
89 */
90 typedef struct {
91 /// The DBG2 table header
92 EFI_ACPI_DEBUG_PORT_2_DESCRIPTION_TABLE Description;
93
94 /// Debug port information list
95 DBG2_DEBUG_DEVICE_INFORMATION Dbg2DeviceInfo[DBG2_NUM_DEBUG_PORTS];
96 } DBG2_TABLE;
97
98 /** A helper macro used for initializing the debug port device
99 information structure.
100
101 @param [in] SubType The DBG Port SubType.
102 @param [in] UartBase The UART port base address.
103 @param [in] UartAddrLen The UART port address range length.
104 @param [in] UartNameStr The UART port name string.
105 **/
106 #define DBG2_DEBUG_PORT_DDI( \
107 SubType, \
108 UartBase, \
109 UartAddrLen, \
110 UartNameStr \
111 ) { \
112 { \
113 /* UINT8 Revision */ \
114 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION, \
115 /* UINT16 Length */ \
116 sizeof (DBG2_DEBUG_DEVICE_INFORMATION), \
117 /* UINT8 NumberofGenericAddressRegisters */ \
118 DBG2_NUMBER_OF_GENERIC_ADDRESS_REGISTERS, \
119 /* UINT16 NameSpaceStringLength */ \
120 DBG2_NAMESPACESTRING_FIELD_SIZE, \
121 /* UINT16 NameSpaceStringOffset */ \
122 OFFSET_OF (DBG2_DEBUG_DEVICE_INFORMATION, NameSpaceString), \
123 /* UINT16 OemDataLength */ \
124 0, \
125 /* UINT16 OemDataOffset */ \
126 0, \
127 /* UINT16 Port Type */ \
128 EFI_ACPI_DBG2_PORT_TYPE_SERIAL, \
129 /* UINT16 Port Subtype */ \
130 SubType, \
131 /* UINT8 Reserved[2] */ \
132 {EFI_ACPI_RESERVED_BYTE, EFI_ACPI_RESERVED_BYTE}, \
133 /* UINT16 BaseAddressRegister Offset */ \
134 OFFSET_OF (DBG2_DEBUG_DEVICE_INFORMATION, BaseAddressRegister), \
135 /* UINT16 AddressSize Offset */ \
136 OFFSET_OF (DBG2_DEBUG_DEVICE_INFORMATION, AddressSize) \
137 }, \
138 /* EFI_ACPI_6_2_GENERIC_ADDRESS_STRUCTURE BaseAddressRegister */ \
139 ARM_GAS32 (UartBase), \
140 /* UINT32 AddressSize */ \
141 UartAddrLen, \
142 /* UINT8 NameSpaceString[MAX_DBG2_NAME_LEN] */ \
143 UartNameStr \
144 }
145
146 /** The DBG2 Table template definition.
147
148 Note: fields marked with "{Template}" will be set dynamically
149 */
150 STATIC
151 DBG2_TABLE AcpiDbg2 = {
152 {
153 ACPI_HEADER (
154 EFI_ACPI_6_2_DEBUG_PORT_2_TABLE_SIGNATURE,
155 DBG2_TABLE,
156 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION
157 ),
158 OFFSET_OF (DBG2_TABLE, Dbg2DeviceInfo),
159 DBG2_NUM_DEBUG_PORTS
160 },
161 {
162 /*
163 * Debug port 1
164 */
165 DBG2_DEBUG_PORT_DDI (
166 0, // {Template}: Serial Port Subtype
167 0, // {Template}: Serial Port Base Address
168 PL011_UART_LENGTH,
169 NAME_STR_DBG_PORT0
170 )
171 }
172 };
173
174 #pragma pack()
175
176 /** This macro expands to a function that retrieves the Serial
177 debug port information from the Configuration Manager
178 */
179 GET_OBJECT_LIST (
180 EObjNameSpaceArm,
181 EArmObjSerialDebugPortInfo,
182 CM_ARM_SERIAL_PORT_INFO
183 );
184
185 /** Initialize the PL011/SBSA UART with the parameters obtained from
186 the Configuration Manager.
187
188 @param [in] SerialPortInfo Pointer to the Serial Port Information.
189
190 @retval EFI_SUCCESS Success.
191 @retval EFI_INVALID_PARAMETER The parameters for serial port initialization
192 are invalid.
193 **/
194 STATIC
195 EFI_STATUS
196 SetupDebugUart (
197 IN CONST CM_ARM_SERIAL_PORT_INFO * CONST SerialPortInfo
198 )
199 {
200 EFI_STATUS Status;
201 UINT64 BaudRate;
202 UINT32 ReceiveFifoDepth;
203 EFI_PARITY_TYPE Parity;
204 UINT8 DataBits;
205 EFI_STOP_BITS_TYPE StopBits;
206
207 ASSERT (SerialPortInfo != NULL);
208
209 // Initialize the Serial Debug UART
210 DEBUG ((DEBUG_INFO, "Initializing Serial Debug UART...\n"));
211 ReceiveFifoDepth = 0; // Use the default value for FIFO depth
212 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);
213 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);
214 StopBits = (EFI_STOP_BITS_TYPE)FixedPcdGet8 (PcdUartDefaultStopBits);
215
216 BaudRate = SerialPortInfo->BaudRate;
217 Status = PL011UartInitializePort (
218 (UINTN)SerialPortInfo->BaseAddress,
219 SerialPortInfo->Clock,
220 &BaudRate,
221 &ReceiveFifoDepth,
222 &Parity,
223 &DataBits,
224 &StopBits
225 );
226
227 ASSERT_EFI_ERROR (Status);
228 return Status;
229 }
230
231 /** Free any resources allocated for constructing the tables.
232
233 @param [in] This Pointer to the ACPI table generator.
234 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
235 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
236 Protocol Interface.
237 @param [in, out] Table Pointer to an array of pointers
238 to ACPI Table(s).
239 @param [in] TableCount Number of ACPI table(s).
240
241 @retval EFI_SUCCESS The resources were freed successfully.
242 @retval EFI_INVALID_PARAMETER The table pointer is NULL or invalid.
243 **/
244 STATIC
245 EFI_STATUS
246 EFIAPI
247 FreeDbg2TableEx (
248 IN CONST ACPI_TABLE_GENERATOR * CONST This,
249 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO * CONST AcpiTableInfo,
250 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL * CONST CfgMgrProtocol,
251 IN OUT EFI_ACPI_DESCRIPTION_HEADER *** CONST Table,
252 IN CONST UINTN TableCount
253 )
254 {
255 EFI_STATUS Status;
256 EFI_ACPI_DESCRIPTION_HEADER ** TableList;
257
258 ASSERT (This != NULL);
259 ASSERT (AcpiTableInfo != NULL);
260 ASSERT (CfgMgrProtocol != NULL);
261 ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
262 ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
263
264 if ((Table == NULL) ||
265 (*Table == NULL) ||
266 (TableCount != 2)) {
267 DEBUG ((DEBUG_ERROR, "ERROR: DBG2: Invalid Table Pointer\n"));
268 return EFI_INVALID_PARAMETER;
269 }
270
271 TableList = *Table;
272
273 if ((TableList[1] == NULL) ||
274 (TableList[1]->Signature !=
275 EFI_ACPI_6_3_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE)) {
276 DEBUG ((DEBUG_ERROR, "ERROR: DBG2: Invalid SSDT table pointer.\n"));
277 return EFI_INVALID_PARAMETER;
278 }
279
280 // Only need to free the SSDT table at index 1. The DBG2 table is static.
281 Status = FreeSsdtSerialPortTable (TableList[1]);
282 ASSERT_EFI_ERROR (Status);
283
284 // Free the table list.
285 FreePool (*Table);
286
287 return Status;
288 }
289
290 /** Construct the DBG2 ACPI table and its associated SSDT table.
291
292 This function invokes the Configuration Manager protocol interface
293 to get the required hardware information for generating the ACPI
294 table.
295
296 If this function allocates any resources then they must be freed
297 in the FreeXXXXTableResourcesEx function.
298
299 @param [in] This Pointer to the ACPI table generator.
300 @param [in] AcpiTableInfo Pointer to the ACPI table information.
301 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
302 Protocol interface.
303 @param [out] Table Pointer to a list of generated ACPI table(s).
304 @param [out] TableCount Number of generated ACPI table(s).
305
306 @retval EFI_SUCCESS Table generated successfully.
307 @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration
308 Manager is less than the Object size for
309 the requested object.
310 @retval EFI_INVALID_PARAMETER A parameter is invalid.
311 @retval EFI_NOT_FOUND Could not find information.
312 @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
313 @retval EFI_UNSUPPORTED Unsupported configuration.
314 **/
315 STATIC
316 EFI_STATUS
317 EFIAPI
318 BuildDbg2TableEx (
319 IN CONST ACPI_TABLE_GENERATOR * This,
320 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO * CONST AcpiTableInfo,
321 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL * CONST CfgMgrProtocol,
322 OUT EFI_ACPI_DESCRIPTION_HEADER *** Table,
323 OUT UINTN * CONST TableCount
324 )
325 {
326 EFI_STATUS Status;
327 CM_ARM_SERIAL_PORT_INFO * SerialPortInfo;
328 UINT32 SerialPortCount;
329 EFI_ACPI_DESCRIPTION_HEADER ** TableList;
330
331 ASSERT (This != NULL);
332 ASSERT (AcpiTableInfo != NULL);
333 ASSERT (CfgMgrProtocol != NULL);
334 ASSERT (Table != NULL);
335 ASSERT (TableCount != NULL);
336 ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
337 ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
338
339 if ((AcpiTableInfo->AcpiTableRevision < This->MinAcpiTableRevision) ||
340 (AcpiTableInfo->AcpiTableRevision > This->AcpiTableRevision)) {
341 DEBUG ((
342 DEBUG_ERROR,
343 "ERROR: DBG2: Requested table revision = %d, is not supported."
344 "Supported table revision: Minimum = %d, Maximum = %d\n",
345 AcpiTableInfo->AcpiTableRevision,
346 This->MinAcpiTableRevision,
347 This->AcpiTableRevision
348 ));
349 return EFI_INVALID_PARAMETER;
350 }
351
352 *Table = NULL;
353
354 Status = GetEArmObjSerialDebugPortInfo (
355 CfgMgrProtocol,
356 CM_NULL_TOKEN,
357 &SerialPortInfo,
358 &SerialPortCount
359 );
360 if (EFI_ERROR (Status)) {
361 DEBUG ((
362 DEBUG_ERROR,
363 "ERROR: DBG2: Failed to get serial port information. Status = %r\n",
364 Status
365 ));
366 return Status;
367 }
368
369 if (SerialPortCount == 0) {
370 DEBUG ((
371 DEBUG_ERROR,
372 "ERROR: DBG2: Serial port information not found. Status = %r\n",
373 EFI_NOT_FOUND
374 ));
375 return EFI_NOT_FOUND;
376 }
377
378 // Only use the first DBG2 port information.
379 Status = ValidateSerialPortInfo (SerialPortInfo, 1);
380 if (EFI_ERROR (Status)) {
381 DEBUG ((
382 DEBUG_ERROR,
383 "ERROR: DBG2: Invalid serial port information. Status = %r\n",
384 Status
385 ));
386 return Status;
387 }
388
389 // Allocate a table to store pointers to the DBG2 and SSDT tables.
390 TableList = (EFI_ACPI_DESCRIPTION_HEADER**)
391 AllocateZeroPool (sizeof (EFI_ACPI_DESCRIPTION_HEADER*) * 2);
392 if (TableList == NULL) {
393 Status = EFI_OUT_OF_RESOURCES;
394 DEBUG ((
395 DEBUG_ERROR,
396 "ERROR: DBG2: Failed to allocate memory for Table List," \
397 " Status = %r\n",
398 Status
399 ));
400 return Status;
401 }
402
403 Status = AddAcpiHeader (
404 CfgMgrProtocol,
405 This,
406 (EFI_ACPI_DESCRIPTION_HEADER*)&AcpiDbg2,
407 AcpiTableInfo,
408 sizeof (DBG2_TABLE)
409 );
410 if (EFI_ERROR (Status)) {
411 DEBUG ((
412 DEBUG_ERROR,
413 "ERROR: DBG2: Failed to add ACPI header. Status = %r\n",
414 Status
415 ));
416 goto error_handler;
417 }
418
419 // Update the base address
420 AcpiDbg2.Dbg2DeviceInfo[INDEX_DBG_PORT0].BaseAddressRegister.Address =
421 SerialPortInfo->BaseAddress;
422
423 // Set the access size
424 if (SerialPortInfo->AccessSize >= EFI_ACPI_6_3_QWORD) {
425 Status = EFI_INVALID_PARAMETER;
426 DEBUG ((
427 DEBUG_ERROR,
428 "ERROR: DBG2: Access size must be <= 3 (DWORD). Status = %r\n",
429 Status
430 ));
431 goto error_handler;
432 } else if (SerialPortInfo->AccessSize == EFI_ACPI_6_3_UNDEFINED) {
433 // 0 Undefined (legacy reasons)
434 // Default to DWORD access size as the access
435 // size field was introduced at a later date
436 // and some ConfigurationManager implementations
437 // may not be providing this field data
438 AcpiDbg2.Dbg2DeviceInfo[INDEX_DBG_PORT0].BaseAddressRegister.AccessSize =
439 EFI_ACPI_6_3_DWORD;
440 } else {
441 AcpiDbg2.Dbg2DeviceInfo[INDEX_DBG_PORT0].BaseAddressRegister.AccessSize =
442 SerialPortInfo->AccessSize;
443 }
444
445 // Update the serial port subtype
446 AcpiDbg2.Dbg2DeviceInfo[INDEX_DBG_PORT0].Dbg2Device.PortSubtype =
447 SerialPortInfo->PortSubtype;
448
449 if ((SerialPortInfo->PortSubtype ==
450 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_PL011_UART) ||
451 (SerialPortInfo->PortSubtype ==
452 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART_2X) ||
453 (SerialPortInfo->PortSubtype ==
454 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART)) {
455 // Initialize the serial port
456 Status = SetupDebugUart (SerialPortInfo);
457 if (EFI_ERROR (Status)) {
458 DEBUG ((
459 DEBUG_ERROR,
460 "ERROR: DBG2: Failed to configure debug serial port. Status = %r\n",
461 Status
462 ));
463 goto error_handler;
464 }
465 }
466
467 TableList[0] = (EFI_ACPI_DESCRIPTION_HEADER*)&AcpiDbg2;
468
469 // Build a SSDT table describing the serial port.
470 Status = BuildSsdtSerialPortTable (
471 AcpiTableInfo,
472 SerialPortInfo,
473 NAME_STR_DBG_PORT0,
474 UID_DBG_PORT0,
475 &TableList[1]
476 );
477 if (EFI_ERROR (Status)) {
478 DEBUG ((
479 DEBUG_ERROR,
480 "ERROR: DBG2: Failed to build associated SSDT table. Status = %r\n",
481 Status
482 ));
483 goto error_handler;
484 }
485
486 *TableCount = 2;
487 *Table = TableList;
488
489 return Status;
490
491 error_handler:
492 if (TableList != NULL) {
493 FreePool (TableList);
494 }
495
496 return Status;
497 }
498
499 /** This macro defines the DBG2 Table Generator revision.
500 */
501 #define DBG2_GENERATOR_REVISION CREATE_REVISION (1, 0)
502
503 /** The interface for the DBG2 Table Generator.
504 */
505 STATIC
506 CONST
507 ACPI_TABLE_GENERATOR Dbg2Generator = {
508 // Generator ID
509 CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdDbg2),
510 // Generator Description
511 L"ACPI.STD.DBG2.GENERATOR",
512 // ACPI Table Signature
513 EFI_ACPI_6_3_DEBUG_PORT_2_TABLE_SIGNATURE,
514 // ACPI Table Revision supported by this Generator
515 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION,
516 // Minimum supported ACPI Table Revision
517 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION,
518 // Creator ID
519 TABLE_GENERATOR_CREATOR_ID_ARM,
520 // Creator Revision
521 DBG2_GENERATOR_REVISION,
522 // Build table function. Use the extended version instead.
523 NULL,
524 // Free table function. Use the extended version instead.
525 NULL,
526 // Extended Build table function.
527 BuildDbg2TableEx,
528 // Extended free function.
529 FreeDbg2TableEx
530 };
531
532 /** Register the Generator with the ACPI Table Factory.
533
534 @param [in] ImageHandle The handle to the image.
535 @param [in] SystemTable Pointer to the System Table.
536
537 @retval EFI_SUCCESS The Generator is registered.
538 @retval EFI_INVALID_PARAMETER A parameter is invalid.
539 @retval EFI_ALREADY_STARTED The Generator for the Table ID
540 is already registered.
541 **/
542 EFI_STATUS
543 EFIAPI
544 AcpiDbg2LibConstructor (
545 IN EFI_HANDLE ImageHandle,
546 IN EFI_SYSTEM_TABLE * SystemTable
547 )
548 {
549 EFI_STATUS Status;
550 Status = RegisterAcpiTableGenerator (&Dbg2Generator);
551 DEBUG ((DEBUG_INFO, "DBG2: Register Generator. Status = %r\n", Status));
552 ASSERT_EFI_ERROR (Status);
553 return Status;
554 }
555
556 /** Deregister the Generator from the ACPI Table Factory.
557
558 @param [in] ImageHandle The handle to the image.
559 @param [in] SystemTable Pointer to the System Table.
560
561 @retval EFI_SUCCESS The Generator is deregistered.
562 @retval EFI_INVALID_PARAMETER A parameter is invalid.
563 @retval EFI_NOT_FOUND The Generator is not registered.
564 **/
565 EFI_STATUS
566 EFIAPI
567 AcpiDbg2LibDestructor (
568 IN EFI_HANDLE ImageHandle,
569 IN EFI_SYSTEM_TABLE * SystemTable
570 )
571 {
572 EFI_STATUS Status;
573 Status = DeregisterAcpiTableGenerator (&Dbg2Generator);
574 DEBUG ((DEBUG_INFO, "DBG2: Deregister Generator. Status = %r\n", Status));
575 ASSERT_EFI_ERROR (Status);
576 return Status;
577 }