--- /dev/null
+/** @file\r
+ SPCR Table Generator\r
+\r
+ Copyright (c) 2017 - 2019, ARM Limited. All rights reserved.\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+ @par Reference(s):\r
+ - Microsoft Serial Port Console Redirection Table\r
+ Specification - Version 1.03 - August 10, 2015.\r
+\r
+**/\r
+\r
+#include <IndustryStandard/DebugPort2Table.h>\r
+#include <IndustryStandard/SerialPortConsoleRedirectionTable.h>\r
+#include <Library/AcpiLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Protocol/AcpiTable.h>\r
+\r
+// Module specific include files.\r
+#include <AcpiTableGenerator.h>\r
+#include <ConfigurationManagerObject.h>\r
+#include <ConfigurationManagerHelper.h>\r
+#include <Library/TableHelperLib.h>\r
+#include <Protocol/ConfigurationManagerProtocol.h>\r
+\r
+/** ARM standard SPCR Table Generator\r
+\r
+ Constructs the SPCR table for PL011 or SBSA UART peripherals.\r
+\r
+Requirements:\r
+ The following Configuration Manager Object(s) are required by\r
+ this Generator:\r
+ - EArmObjSerialConsolePortInfo\r
+\r
+NOTE: This implementation ignores the possibility that the Serial settings may\r
+ be modified from the UEFI Shell. A more complex handler would be needed\r
+ to (e.g.) recover serial port settings from the UART, or non-volatile\r
+ storage.\r
+*/\r
+\r
+#pragma pack(1)\r
+\r
+/** This macro defines the no flow control option.\r
+*/\r
+#define SPCR_FLOW_CONTROL_NONE 0\r
+\r
+/**A template for generating the SPCR Table.\r
+\r
+ Note: fields marked "{Template}" will be updated dynamically.\r
+*/\r
+STATIC\r
+EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE AcpiSpcr = {\r
+ ACPI_HEADER (\r
+ EFI_ACPI_6_2_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE,\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE,\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_REVISION\r
+ ),\r
+ 0, // {Template}: Serial Port Subtype\r
+ {\r
+ EFI_ACPI_RESERVED_BYTE,\r
+ EFI_ACPI_RESERVED_BYTE,\r
+ EFI_ACPI_RESERVED_BYTE\r
+ },\r
+ ARM_GAS32 (0), // {Template}: Serial Port Base Address\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_INTERRUPT_TYPE_GIC,\r
+ 0, // Not used on ARM\r
+ 0, // {Template}: Serial Port Interrupt\r
+ 0, // {Template}: Serial Port Baudrate\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_PARITY_NO_PARITY,\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_STOP_BITS_1,\r
+ SPCR_FLOW_CONTROL_NONE,\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_TERMINAL_TYPE_ANSI,\r
+ EFI_ACPI_RESERVED_BYTE,\r
+ 0xFFFF,\r
+ 0xFFFF,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00,\r
+ 0x00000000,\r
+ 0x00,\r
+ EFI_ACPI_RESERVED_DWORD\r
+};\r
+\r
+#pragma pack()\r
+\r
+/** This macro expands to a function that retrieves the Serial\r
+ Port Information from the Configuration Manager.\r
+*/\r
+GET_OBJECT_LIST (\r
+ EObjNameSpaceArm,\r
+ EArmObjSerialConsolePortInfo,\r
+ CM_ARM_SERIAL_PORT_INFO\r
+ )\r
+\r
+/** Construct the SPCR ACPI table.\r
+\r
+ This function invokes the Configuration Manager protocol interface\r
+ to get the required hardware information for generating the ACPI\r
+ table.\r
+\r
+ If this function allocates any resources then they must be freed\r
+ in the FreeXXXXTableResources function.\r
+\r
+ @param [in] This Pointer to the table generator.\r
+ @param [in] AcpiTableInfo Pointer to the ACPI Table Info.\r
+ @param [in] CfgMgrProtocol Pointer to the Configuration Manager\r
+ Protocol Interface.\r
+ @param [out] Table Pointer to the constructed ACPI Table.\r
+\r
+ @retval EFI_SUCCESS Table generated successfully.\r
+ @retval EFI_INVALID_PARAMETER A parameter is invalid.\r
+ @retval EFI_NOT_FOUND The required object was not found.\r
+ @retval EFI_UNSUPPORTED An unsupported baudrate was specified by the\r
+ Configuration Manager.\r
+ @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration\r
+ Manager is less than the Object size for the\r
+ requested object.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+BuildSpcrTable (\r
+ IN CONST ACPI_TABLE_GENERATOR * CONST This,\r
+ IN CONST CM_STD_OBJ_ACPI_TABLE_INFO * CONST AcpiTableInfo,\r
+ IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL * CONST CfgMgrProtocol,\r
+ OUT EFI_ACPI_DESCRIPTION_HEADER ** CONST Table\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ CM_ARM_SERIAL_PORT_INFO * SerialPortInfo;\r
+\r
+ ASSERT (This != NULL);\r
+ ASSERT (AcpiTableInfo != NULL);\r
+ ASSERT (CfgMgrProtocol != NULL);\r
+ ASSERT (Table != NULL);\r
+ ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);\r
+ ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);\r
+\r
+ if ((AcpiTableInfo->AcpiTableRevision < This->MinAcpiTableRevision) ||\r
+ (AcpiTableInfo->AcpiTableRevision > This->AcpiTableRevision)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ERROR: SPCR: Requested table revision = %d, is not supported."\r
+ "Supported table revision: Minimum = %d, Maximum = %d\n",\r
+ AcpiTableInfo->AcpiTableRevision,\r
+ This->MinAcpiTableRevision,\r
+ This->AcpiTableRevision\r
+ ));\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ *Table = NULL;\r
+\r
+ Status = GetEArmObjSerialConsolePortInfo (\r
+ CfgMgrProtocol,\r
+ CM_NULL_TOKEN,\r
+ &SerialPortInfo,\r
+ NULL\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ERROR: SPCR: Failed to get serial port information. Status = %r\n",\r
+ Status\r
+ ));\r
+ goto error_handler;\r
+ }\r
+\r
+ if (SerialPortInfo->BaseAddress == 0) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ERROR: SPCR: Uart port base address is invalid. BaseAddress = 0x%lx\n",\r
+ SerialPortInfo->BaseAddress\r
+ ));\r
+ goto error_handler;\r
+ }\r
+\r
+ if ((SerialPortInfo->PortSubtype !=\r
+ EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_PL011_UART) &&\r
+ (SerialPortInfo->PortSubtype !=\r
+ EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART_2X) &&\r
+ (SerialPortInfo->PortSubtype !=\r
+ EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART) &&\r
+ (SerialPortInfo->PortSubtype !=\r
+ EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_DCC)) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ERROR: SPCR: Uart port sybtype is invalid. PortSubtype = 0x%x\n",\r
+ SerialPortInfo->PortSubtype\r
+ ));\r
+ goto error_handler;\r
+ }\r
+\r
+ DEBUG ((DEBUG_INFO, "SPCR UART Configuration:\n"));\r
+ DEBUG ((DEBUG_INFO, " UART Base = 0x%lx\n", SerialPortInfo->BaseAddress));\r
+ DEBUG ((DEBUG_INFO, " Clock = %d\n", SerialPortInfo->Clock));\r
+ DEBUG ((DEBUG_INFO, " Baudrate = %ld\n", SerialPortInfo->BaudRate));\r
+ DEBUG ((DEBUG_INFO, " Interrupt = %d\n", SerialPortInfo->Interrupt));\r
+\r
+ Status = AddAcpiHeader (\r
+ CfgMgrProtocol,\r
+ This,\r
+ (EFI_ACPI_DESCRIPTION_HEADER*)&AcpiSpcr,\r
+ AcpiTableInfo->AcpiTableRevision,\r
+ sizeof (EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE)\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ERROR: SPCR: Failed to add ACPI header. Status = %r\n",\r
+ Status\r
+ ));\r
+ goto error_handler;\r
+ }\r
+\r
+ // Update the serial port subtype\r
+ AcpiSpcr.InterfaceType = SerialPortInfo->PortSubtype;\r
+\r
+ // Update the base address\r
+ AcpiSpcr.BaseAddress.Address = SerialPortInfo->BaseAddress;\r
+\r
+ // Update the UART interrupt\r
+ AcpiSpcr.GlobalSystemInterrupt = SerialPortInfo->Interrupt;\r
+\r
+ switch (SerialPortInfo->BaudRate) {\r
+ case 9600:\r
+ AcpiSpcr.BaudRate =\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_BAUD_RATE_9600;\r
+ break;\r
+ case 19200:\r
+ AcpiSpcr.BaudRate =\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_BAUD_RATE_19200;\r
+ break;\r
+ case 57600:\r
+ AcpiSpcr.BaudRate =\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_BAUD_RATE_57600;\r
+ break;\r
+ case 115200:\r
+ AcpiSpcr.BaudRate =\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_BAUD_RATE_115200;\r
+ break;\r
+ default:\r
+ Status = EFI_UNSUPPORTED;\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ERROR: SPCR: Invalid Baud Rate %ld, Status = %r\n",\r
+ SerialPortInfo->BaudRate,\r
+ Status\r
+ ));\r
+ goto error_handler;\r
+ } // switch\r
+\r
+ *Table = (EFI_ACPI_DESCRIPTION_HEADER*)&AcpiSpcr;\r
+\r
+error_handler:\r
+ return Status;\r
+}\r
+\r
+/** This macro defines the SPCR Table Generator revision.\r
+*/\r
+#define SPCR_GENERATOR_REVISION CREATE_REVISION (1, 0)\r
+\r
+/** The interface for the SPCR Table Generator.\r
+*/\r
+STATIC\r
+CONST\r
+ACPI_TABLE_GENERATOR SpcrGenerator = {\r
+ // Generator ID\r
+ CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdSpcr),\r
+ // Generator Description\r
+ L"ACPI.STD.SPCR.GENERATOR",\r
+ // ACPI Table Signature\r
+ EFI_ACPI_6_2_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE,\r
+ // ACPI Table Revision supported by this Generator\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_REVISION,\r
+ // Minimum supported ACPI Table Revision\r
+ EFI_ACPI_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_REVISION,\r
+ // Creator ID\r
+ TABLE_GENERATOR_CREATOR_ID_ARM,\r
+ // Creator Revision\r
+ SPCR_GENERATOR_REVISION,\r
+ // Build Table function\r
+ BuildSpcrTable,\r
+ // No additional resources are allocated by the generator.\r
+ // Hence the Free Resource function is not required.\r
+ NULL,\r
+ // Extended build function not needed\r
+ NULL,\r
+ // Extended build function not implemented by the generator.\r
+ // Hence extended free resource function is not required.\r
+ NULL\r
+};\r
+\r
+/** Register the Generator with the ACPI Table Factory.\r
+\r
+ @param [in] ImageHandle The handle to the image.\r
+ @param [in] SystemTable Pointer to the System Table.\r
+\r
+ @retval EFI_SUCCESS The Generator is registered.\r
+ @retval EFI_INVALID_PARAMETER A parameter is invalid.\r
+ @retval EFI_ALREADY_STARTED The Generator for the Table ID\r
+ is already registered.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AcpiSpcrLibConstructor (\r
+ IN CONST EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE * CONST SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ Status = RegisterAcpiTableGenerator (&SpcrGenerator);\r
+ DEBUG ((DEBUG_INFO, "SPCR: Register Generator. Status = %r\n", Status));\r
+ ASSERT_EFI_ERROR (Status);\r
+ return Status;\r
+}\r
+\r
+/** Deregister the Generator from the ACPI Table Factory.\r
+\r
+ @param [in] ImageHandle The handle to the image.\r
+ @param [in] SystemTable Pointer to the System Table.\r
+\r
+ @retval EFI_SUCCESS The Generator is deregistered.\r
+ @retval EFI_INVALID_PARAMETER A parameter is invalid.\r
+ @retval EFI_NOT_FOUND The Generator is not registered.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AcpiSpcrLibDestructor (\r
+ IN CONST EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE * CONST SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ Status = DeregisterAcpiTableGenerator (&SpcrGenerator);\r
+ DEBUG ((DEBUG_INFO, "SPCR: Deregister Generator. Status = %r\n", Status));\r
+ ASSERT_EFI_ERROR (Status);\r
+ return Status;\r
+}\r