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