]> git.proxmox.com Git - mirror_edk2.git/blob - DynamicTablesPkg/Library/Acpi/Arm/AcpiDbg2LibArm/Dbg2Generator.c
DynamicTablesPkg: Add OEM Info
[mirror_edk2.git] / DynamicTablesPkg / Library / Acpi / Arm / AcpiDbg2LibArm / Dbg2Generator.c
1 /** @file
2 DBG2 Table Generator
3
4 Copyright (c) 2017 - 2019, ARM Limited. All rights reserved.
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 @par Reference(s):
14 - Microsoft Debug Port Table 2 (DBG2) Specification - December 10, 2015.
15
16 **/
17
18 #include <IndustryStandard/DebugPort2Table.h>
19 #include <Library/AcpiLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PL011UartLib.h>
22 #include <Protocol/AcpiTable.h>
23 #include <Protocol/SerialIo.h>
24
25 // Module specific include files.
26 #include <AcpiTableGenerator.h>
27 #include <ConfigurationManagerObject.h>
28 #include <ConfigurationManagerHelper.h>
29 #include <Library/TableHelperLib.h>
30 #include <Protocol/ConfigurationManagerProtocol.h>
31
32 /** ARM standard DBG2 Table Generator
33
34 Constructs the DBG2 table for PL011 or SBSA UART peripherals.
35
36 Requirements:
37 The following Configuration Manager Object(s) are required by
38 this Generator:
39 - EArmObjSerialDebugPortInfo
40 */
41
42 #pragma pack(1)
43
44 /** The number of debug ports represented by the Table.
45 */
46 #define DBG2_NUM_DEBUG_PORTS 1
47
48 /** The number of Generic Address Registers
49 presented in the debug device information.
50 */
51 #define DBG2_NUMBER_OF_GENERIC_ADDRESS_REGISTERS 1
52
53 /** The index for the debug port 1 in the Debug port information list.
54 */
55 #define DBG_PORT_INDEX_PORT1 0
56
57 /** A string representing the name of the debug port 1.
58 */
59 #define NAME_STR_PORT1 "COM1"
60
61 /** The length of the namespace string.
62 */
63 #define DBG2_NAMESPACESTRING_FIELD_SIZE sizeof (NAME_STR_PORT1)
64
65 /** The PL011 UART address range length.
66 */
67 #define PL011_UART_LENGTH 0x1000
68
69 /** A structure that provides the OS with the required information
70 for initializing a debugger connection.
71 */
72 typedef struct {
73 /// The debug device information for the platform
74 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT Dbg2Device;
75
76 /// The base address register for the serial port
77 EFI_ACPI_6_2_GENERIC_ADDRESS_STRUCTURE BaseAddressRegister;
78
79 /// The address size
80 UINT32 AddressSize;
81
82 /// The debug port name string
83 UINT8 NameSpaceString[DBG2_NAMESPACESTRING_FIELD_SIZE];
84 } DBG2_DEBUG_DEVICE_INFORMATION;
85
86 /** A structure representing the information about the debug port(s)
87 available on the platform.
88 */
89 typedef struct {
90 /// The DBG2 table header
91 EFI_ACPI_DEBUG_PORT_2_DESCRIPTION_TABLE Description;
92
93 /// Debug port information list
94 DBG2_DEBUG_DEVICE_INFORMATION Dbg2DeviceInfo[DBG2_NUM_DEBUG_PORTS];
95 } DBG2_TABLE;
96
97 /** A helper macro used for initializing the debug port device
98 information structure.
99
100 @param [in] NumReg The number of generic address registers.
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 NumReg, \
108 SubType, \
109 UartBase, \
110 UartAddrLen, \
111 UartNameStr \
112 ) { \
113 { \
114 /* UINT8 Revision */ \
115 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION, \
116 /* UINT16 Length */ \
117 sizeof (DBG2_DEBUG_DEVICE_INFORMATION), \
118 /* UINT8 NumberofGenericAddressRegisters */ \
119 NumReg, \
120 /* UINT16 NameSpaceStringLength */ \
121 DBG2_NAMESPACESTRING_FIELD_SIZE, \
122 /* UINT16 NameSpaceStringOffset */ \
123 OFFSET_OF (DBG2_DEBUG_DEVICE_INFORMATION, NameSpaceString), \
124 /* UINT16 OemDataLength */ \
125 0, \
126 /* UINT16 OemDataOffset */ \
127 0, \
128 /* UINT16 Port Type */ \
129 EFI_ACPI_DBG2_PORT_TYPE_SERIAL, \
130 /* UINT16 Port Subtype */ \
131 SubType, \
132 /* UINT8 Reserved[2] */ \
133 {EFI_ACPI_RESERVED_BYTE, EFI_ACPI_RESERVED_BYTE}, \
134 /* UINT16 BaseAddressRegister Offset */ \
135 OFFSET_OF (DBG2_DEBUG_DEVICE_INFORMATION, BaseAddressRegister), \
136 /* UINT16 AddressSize Offset */ \
137 OFFSET_OF (DBG2_DEBUG_DEVICE_INFORMATION, AddressSize) \
138 }, \
139 /* EFI_ACPI_6_2_GENERIC_ADDRESS_STRUCTURE BaseAddressRegister */ \
140 ARM_GAS32 (UartBase), \
141 /* UINT32 AddressSize */ \
142 UartAddrLen, \
143 /* UINT8 NameSpaceString[MAX_DBG2_NAME_LEN] */ \
144 UartNameStr \
145 }
146
147 /** The DBG2 Table template definition.
148
149 Note: fields marked with "{Template}" will be set dynamically
150 */
151 STATIC
152 DBG2_TABLE AcpiDbg2 = {
153 {
154 ACPI_HEADER (
155 EFI_ACPI_6_2_DEBUG_PORT_2_TABLE_SIGNATURE,
156 DBG2_TABLE,
157 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION
158 ),
159 OFFSET_OF (DBG2_TABLE, Dbg2DeviceInfo),
160 DBG2_NUM_DEBUG_PORTS
161 },
162 {
163 /*
164 * Debug port 1
165 */
166 DBG2_DEBUG_PORT_DDI (
167 DBG2_NUMBER_OF_GENERIC_ADDRESS_REGISTERS,
168 0, // {Template}: Serial Port Subtype
169 0, // {Template}: Serial Port Base Address
170 PL011_UART_LENGTH,
171 NAME_STR_PORT1
172 )
173 }
174 };
175
176 #pragma pack()
177
178 /** This macro expands to a function that retrieves the Serial
179 debug port information from the Configuration Manager
180 */
181 GET_OBJECT_LIST (
182 EObjNameSpaceArm,
183 EArmObjSerialDebugPortInfo,
184 CM_ARM_SERIAL_PORT_INFO
185 );
186
187 /** Initialize the PL011 UART with the parameters obtained from
188 the Configuration Manager.
189
190 @param [in] SerialPortInfo Pointer to the Serial Port Information.
191
192 @retval EFI_SUCCESS Success.
193 @retval EFI_INVALID_PARAMETER The parameters for serial port initialization
194 are invalid.
195 **/
196 STATIC
197 EFI_STATUS
198 SetupDebugUart (
199 IN CONST CM_ARM_SERIAL_PORT_INFO * CONST SerialPortInfo
200 )
201 {
202 EFI_STATUS Status;
203 UINT64 BaudRate;
204 UINT32 ReceiveFifoDepth;
205 EFI_PARITY_TYPE Parity;
206 UINT8 DataBits;
207 EFI_STOP_BITS_TYPE StopBits;
208
209 ASSERT (SerialPortInfo != NULL);
210
211 // Initialize the Serial Debug UART
212 DEBUG ((DEBUG_INFO, "Initializing Serial Debug UART...\n"));
213 ReceiveFifoDepth = 0; // Use the default value for FIFO depth
214 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);
215 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);
216 StopBits = (EFI_STOP_BITS_TYPE)FixedPcdGet8 (PcdUartDefaultStopBits);
217
218 BaudRate = SerialPortInfo->BaudRate;
219 Status = PL011UartInitializePort (
220 (UINTN)SerialPortInfo->BaseAddress,
221 SerialPortInfo->Clock,
222 &BaudRate,
223 &ReceiveFifoDepth,
224 &Parity,
225 &DataBits,
226 &StopBits
227 );
228
229 DEBUG ((DEBUG_INFO, "Debug UART Configuration:\n"));
230 DEBUG ((DEBUG_INFO, "UART Base = 0x%lx\n", SerialPortInfo->BaseAddress));
231 DEBUG ((DEBUG_INFO, "Clock = %d\n", SerialPortInfo->Clock));
232 DEBUG ((DEBUG_INFO, "Baudrate = %ld\n", BaudRate));
233 DEBUG ((DEBUG_INFO, "Configuring Debug UART. Status = %r\n", Status));
234
235 ASSERT_EFI_ERROR (Status);
236 return Status;
237 }
238
239 /** Construct the DBG2 ACPI table
240
241 The BuildDbg2Table function is called by the Dynamic Table Manager
242 to construct the DBG2 ACPI table.
243
244 This function invokes the Configuration Manager protocol interface
245 to get the required hardware information for generating the ACPI
246 table.
247
248 If this function allocates any resources then they must be freed
249 in the FreeXXXXTableResources function.
250
251 @param [in] This Pointer to the table generator.
252 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
253 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
254 Protocol Interface.
255 @param [out] Table Pointer to the constructed ACPI Table.
256
257 @retval EFI_SUCCESS Table generated successfully.
258 @retval EFI_INVALID_PARAMETER A parameter is invalid.
259 @retval EFI_NOT_FOUND The required object was not found.
260 @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration
261 Manager is less than the Object size for the
262 requested object.
263 **/
264 STATIC
265 EFI_STATUS
266 EFIAPI
267 BuildDbg2Table (
268 IN CONST ACPI_TABLE_GENERATOR * CONST This,
269 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO * CONST AcpiTableInfo,
270 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL * CONST CfgMgrProtocol,
271 OUT EFI_ACPI_DESCRIPTION_HEADER ** CONST Table
272 )
273 {
274 EFI_STATUS Status;
275 CM_ARM_SERIAL_PORT_INFO * SerialPortInfo;
276
277 ASSERT (This != NULL);
278 ASSERT (AcpiTableInfo != NULL);
279 ASSERT (CfgMgrProtocol != NULL);
280 ASSERT (Table != NULL);
281 ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
282 ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
283
284 if ((AcpiTableInfo->AcpiTableRevision < This->MinAcpiTableRevision) ||
285 (AcpiTableInfo->AcpiTableRevision > This->AcpiTableRevision)) {
286 DEBUG ((
287 DEBUG_ERROR,
288 "ERROR: DBG2: Requested table revision = %d, is not supported."
289 "Supported table revision: Minimum = %d, Maximum = %d\n",
290 AcpiTableInfo->AcpiTableRevision,
291 This->MinAcpiTableRevision,
292 This->AcpiTableRevision
293 ));
294 return EFI_INVALID_PARAMETER;
295 }
296
297 *Table = NULL;
298
299 Status = GetEArmObjSerialDebugPortInfo (
300 CfgMgrProtocol,
301 CM_NULL_TOKEN,
302 &SerialPortInfo,
303 NULL
304 );
305 if (EFI_ERROR (Status)) {
306 DEBUG ((
307 DEBUG_ERROR,
308 "ERROR: DBG2: Failed to get serial port information. Status = %r\n",
309 Status
310 ));
311 goto error_handler;
312 }
313
314 if (SerialPortInfo->BaseAddress == 0) {
315 Status = EFI_INVALID_PARAMETER;
316 DEBUG ((
317 DEBUG_ERROR,
318 "ERROR: DBG2: Uart port base address is invalid. BaseAddress = 0x%lx\n",
319 SerialPortInfo->BaseAddress
320 ));
321 goto error_handler;
322 }
323
324 if ((SerialPortInfo->PortSubtype !=
325 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_PL011_UART) &&
326 (SerialPortInfo->PortSubtype !=
327 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART_2X) &&
328 (SerialPortInfo->PortSubtype !=
329 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART) &&
330 (SerialPortInfo->PortSubtype !=
331 EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_DCC)) {
332 Status = EFI_INVALID_PARAMETER;
333 DEBUG ((
334 DEBUG_ERROR,
335 "ERROR: DBG2: Uart port sybtype is invalid. PortSubtype = 0x%x\n",
336 SerialPortInfo->PortSubtype
337 ));
338 goto error_handler;
339 }
340
341 Status = AddAcpiHeader (
342 CfgMgrProtocol,
343 This,
344 (EFI_ACPI_DESCRIPTION_HEADER*)&AcpiDbg2,
345 AcpiTableInfo,
346 sizeof (DBG2_TABLE)
347 );
348 if (EFI_ERROR (Status)) {
349 DEBUG ((
350 DEBUG_ERROR,
351 "ERROR: DBG2: Failed to add ACPI header. Status = %r\n",
352 Status
353 ));
354 goto error_handler;
355 }
356
357 // Update the base address
358 AcpiDbg2.Dbg2DeviceInfo[DBG_PORT_INDEX_PORT1].BaseAddressRegister.Address =
359 SerialPortInfo->BaseAddress;
360
361 // Update the serial port subtype
362 AcpiDbg2.Dbg2DeviceInfo[DBG_PORT_INDEX_PORT1].Dbg2Device.PortSubtype =
363 SerialPortInfo->PortSubtype;
364
365 // Initialize the serial port
366 Status = SetupDebugUart (SerialPortInfo);
367 if (EFI_ERROR (Status)) {
368 DEBUG ((
369 DEBUG_ERROR,
370 "ERROR: DBG2: Failed to configure debug serial port. Status = %r\n",
371 Status
372 ));
373 goto error_handler;
374 }
375
376 *Table = (EFI_ACPI_DESCRIPTION_HEADER*)&AcpiDbg2;
377
378 error_handler:
379 return Status;
380 }
381
382 /** This macro defines the DBG2 Table Generator revision.
383 */
384 #define DBG2_GENERATOR_REVISION CREATE_REVISION (1, 0)
385
386 /** The interface for the DBG2 Table Generator.
387 */
388 STATIC
389 CONST
390 ACPI_TABLE_GENERATOR Dbg2Generator = {
391 // Generator ID
392 CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdDbg2),
393 // Generator Description
394 L"ACPI.STD.DBG2.GENERATOR",
395 // ACPI Table Signature
396 EFI_ACPI_6_2_DEBUG_PORT_2_TABLE_SIGNATURE,
397 // ACPI Table Revision supported by this Generator
398 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION,
399 // Minimum supported ACPI Table Revision
400 EFI_ACPI_DBG2_DEBUG_DEVICE_INFORMATION_STRUCT_REVISION,
401 // Creator ID
402 TABLE_GENERATOR_CREATOR_ID_ARM,
403 // Creator Revision
404 DBG2_GENERATOR_REVISION,
405 // Build Table function
406 BuildDbg2Table,
407 // No additional resources are allocated by the generator.
408 // Hence the Free Resource function is not required.
409 NULL,
410 // Extended build function not needed
411 NULL,
412 // Extended build function not implemented by the generator.
413 // Hence extended free resource function is not required.
414 NULL
415 };
416
417 /** Register the Generator with the ACPI Table Factory.
418
419 @param [in] ImageHandle The handle to the image.
420 @param [in] SystemTable Pointer to the System Table.
421
422 @retval EFI_SUCCESS The Generator is registered.
423 @retval EFI_INVALID_PARAMETER A parameter is invalid.
424 @retval EFI_ALREADY_STARTED The Generator for the Table ID
425 is already registered.
426 **/
427 EFI_STATUS
428 EFIAPI
429 AcpiDbg2LibConstructor (
430 IN CONST EFI_HANDLE ImageHandle,
431 IN EFI_SYSTEM_TABLE * CONST SystemTable
432 )
433 {
434 EFI_STATUS Status;
435 Status = RegisterAcpiTableGenerator (&Dbg2Generator);
436 DEBUG ((DEBUG_INFO, "DBG2: Register Generator. Status = %r\n", Status));
437 ASSERT_EFI_ERROR (Status);
438
439 return Status;
440 }
441
442 /** Deregister the Generator from the ACPI Table Factory.
443
444 @param [in] ImageHandle The handle to the image.
445 @param [in] SystemTable Pointer to the System Table.
446
447 @retval EFI_SUCCESS The Generator is deregistered.
448 @retval EFI_INVALID_PARAMETER A parameter is invalid.
449 @retval EFI_NOT_FOUND The Generator is not registered.
450 **/
451 EFI_STATUS
452 EFIAPI
453 AcpiDbg2LibDestructor (
454 IN CONST EFI_HANDLE ImageHandle,
455 IN EFI_SYSTEM_TABLE * CONST SystemTable
456 )
457 {
458 EFI_STATUS Status;
459 Status = DeregisterAcpiTableGenerator (&Dbg2Generator);
460 DEBUG ((DEBUG_INFO, "DBG2: Deregister Generator. Status = %r\n", Status));
461 ASSERT_EFI_ERROR (Status);
462 return Status;
463 }