public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sami Mujawar" <sami.mujawar@arm.com>
To: <devel@edk2.groups.io>
Cc: Sami Mujawar <sami.mujawar@arm.com>, <Alexei.Fedorov@arm.com>,
	<pierre.gondois@arm.com>, <ard.biesheuvel@arm.com>,
	<Matteo.Carlini@arm.com>, <Ben.Adderson@arm.com>, <nd@arm.com>
Subject: [PATCH v1 27/30] DynamicTablesPkg: SSDT Serial Port Fixup library
Date: Wed, 12 Aug 2020 16:22:33 +0100	[thread overview]
Message-ID: <20200812152236.31164-28-sami.mujawar@arm.com> (raw)
In-Reply-To: <20200812152236.31164-1-sami.mujawar@arm.com>

From: Pierre Gondois <pierre.gondois@arm.com>

According to Arm Server Base Boot Requirements,
Platform Design Document version 1.2 revision D,
September 2, 2019, section '4.2.1.8 SPCR'; The
SPCR console device must be included in the DSDT.

Additionally, it is often desirable to describe the
serial ports available on a platform so that they
are available for use by a rich OS.

To facilitate the description of serial ports on a
platform a common SSDT Serial Port Fixup library is
introduced. It provides interfaces to build a SSDT
serial port definition block table based on the
serial port information.

The SSDT Serial Port Fixup library is used by the
SPCR, DBG2 and SSDT Serial Port generator to describe
the serial port information in a definition block.

  +------------+   +------------+   +------------+
  |  SPCR Gen  |   |  DBG2 Gen  |   | SERIAL Gen |
  +------------+   +------------+   +------------+
        +----------------------------------+
        |  SSDT Serial Port Fixup library  |
        +----------------------------------+

The SSDT Serial Port Fixup library:
  - Parses the SSDT Serial Port template using the
    AmlLib library to generate an AML tree.
  - Updates the _UID, _HID and _CID values.
  - Fixes up the Serial port base address, length
    and the interrupt number in the _CRS descriptor.
  - Fixes up the serial-port name.
  - Serialises the AML Tree to a buffer containing
    the definition block data.
  The definition block data is then installed by the
  corresponding table generator.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 DynamicTablesPkg/DynamicTables.dsc.inc                                            |   1 +
 DynamicTablesPkg/DynamicTablesPkg.ci.yaml                                         |   3 +
 DynamicTablesPkg/DynamicTablesPkg.dec                                             |   3 +
 DynamicTablesPkg/DynamicTablesPkg.dsc                                             |   1 +
 DynamicTablesPkg/Include/AcpiTableGenerator.h                                     |   7 +-
 DynamicTablesPkg/Include/ArmNameSpaceObjects.h                                    |   9 +-
 DynamicTablesPkg/Include/Library/SsdtSerialPortFixupLib.h                         |  68 +++
 DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.c   | 524 ++++++++++++++++++++
 DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.inf |  30 ++
 DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortTemplate.asl |  60 +++
 10 files changed, 703 insertions(+), 3 deletions(-)

diff --git a/DynamicTablesPkg/DynamicTables.dsc.inc b/DynamicTablesPkg/DynamicTables.dsc.inc
index 928529f797b49f2f0dd52508a9aad557f568719a..0063fc3c671f4ae6089dd4df76074445364c6011 100644
--- a/DynamicTablesPkg/DynamicTables.dsc.inc
+++ b/DynamicTablesPkg/DynamicTables.dsc.inc
@@ -14,6 +14,7 @@ [BuildOptions]
 
 [LibraryClasses.common]
   AmlLib|DynamicTablesPkg/Library/Common/AmlLib/AmlLib.inf
+  SsdtSerialPortFixupLib|DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.inf
   TableHelperLib|DynamicTablesPkg/Library/Common/TableHelperLib/TableHelperLib.inf
 
 [Components.common]
diff --git a/DynamicTablesPkg/DynamicTablesPkg.ci.yaml b/DynamicTablesPkg/DynamicTablesPkg.ci.yaml
index 5ee20357326f4b79444d63418f56aae0b00508de..c0d09e79fdf7f6003b5bbda45abc82a0caf4e53f 100644
--- a/DynamicTablesPkg/DynamicTablesPkg.ci.yaml
+++ b/DynamicTablesPkg/DynamicTablesPkg.ci.yaml
@@ -69,6 +69,8 @@
         "IgnoreFiles": [],           # use gitignore syntax to ignore errors
                                      # in matching files
         "ExtendWords": [
+           "ARMHB",                  # ARMHB000
+           "ARMLTD",
            "EISAID",
            "CCIDX",
            "CCSIDR",
@@ -81,6 +83,7 @@
            "MPIDR",
            "pytool",
            "Roadmap",
+           "ssdtserialporttemplate",
            "SMMUV",
            "standardised",
            "TABLEEX",
diff --git a/DynamicTablesPkg/DynamicTablesPkg.dec b/DynamicTablesPkg/DynamicTablesPkg.dec
index 57e6815fa159c5ce8d5cb0c930375e7322bff211..f36a6e8bb7c17f82acff3c766b15202b064a64b5 100644
--- a/DynamicTablesPkg/DynamicTablesPkg.dec
+++ b/DynamicTablesPkg/DynamicTablesPkg.dec
@@ -20,6 +20,9 @@ [LibraryClasses]
   ##  @libraryclass  Defines a set of APIs for Dynamic AML generation.
   AmlLib|Include/Library/AmlLib/AmlLib.h
 
+  ##  @libraryclass  Defines a set of methods for fixing up a SSDT Serial Port.
+  SsdtSerialPortFixupLib|Include/Library/SsdtSerialPortFixupLib.h
+
   ##  @libraryclass  Defines a set of helper methods.
   TableHelperLib|Include/Library/TableHelperLib.h
 
diff --git a/DynamicTablesPkg/DynamicTablesPkg.dsc b/DynamicTablesPkg/DynamicTablesPkg.dsc
index add6b192ad4187e2769b374ef68f93a65a1af8b6..0232bda459c87016e1e62f52a5c98347e551b14c 100644
--- a/DynamicTablesPkg/DynamicTablesPkg.dsc
+++ b/DynamicTablesPkg/DynamicTablesPkg.dsc
@@ -37,6 +37,7 @@ [LibraryClasses.ARM, LibraryClasses.AARCH64]
 
 [Components.common]
   DynamicTablesPkg/Library/Common/AmlLib/AmlLib.inf
+  DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.inf
   DynamicTablesPkg/Library/Common/TableHelperLib/TableHelperLib.inf
 
 [BuildOptions]
diff --git a/DynamicTablesPkg/Include/AcpiTableGenerator.h b/DynamicTablesPkg/Include/AcpiTableGenerator.h
index b55feb4e7507f0e4e1e0a64c3c4b8068f17dad47..ef5018c312c1abbc205a06b037ffd6063cf02f0a 100644
--- a/DynamicTablesPkg/Include/AcpiTableGenerator.h
+++ b/DynamicTablesPkg/Include/AcpiTableGenerator.h
@@ -1,6 +1,6 @@
 /** @file
 
-  Copyright (c) 2017 - 2019, ARM Limited. All rights reserved.
+  Copyright (c) 2017 - 2020, Arm Limited. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -55,6 +55,10 @@ The Dynamic Tables Framework implements the following ACPI table generators:
             the Configuration Manager and builds the PPTT table.
   - SRAT  : The SRAT generator collates the system resource affinity information
             from the Configuration Manager and builds the SRAT table.
+  - SSDT Serial-Port:
+            The SSDT Serial generator collates the Serial port information
+            from the Configuration Manager and patches the SSDT Serial Port
+            template to build the SSDT Serial port table.
 */
 
 /** The ACPI_TABLE_GENERATOR_ID type describes ACPI table generator ID.
@@ -78,6 +82,7 @@ typedef enum StdAcpiTableId {
   EStdAcpiTableIdIort,                          ///< IORT Generator
   EStdAcpiTableIdPptt,                          ///< PPTT Generator
   EStdAcpiTableIdSrat,                          ///< SRAT Generator
+  EStdAcpiTableIdSsdtSerialPort,                ///< SSDT Serial-Port Generator
   EStdAcpiTableIdMax
 } ESTD_ACPI_TABLE_ID;
 
diff --git a/DynamicTablesPkg/Include/ArmNameSpaceObjects.h b/DynamicTablesPkg/Include/ArmNameSpaceObjects.h
index 57a282d5cb6883d0dabb74ceac7c8905ed3ef43a..b2534a6505d6fb695f0751bbb09d365bd93d092e 100644
--- a/DynamicTablesPkg/Include/ArmNameSpaceObjects.h
+++ b/DynamicTablesPkg/Include/ArmNameSpaceObjects.h
@@ -1,6 +1,6 @@
 /** @file
 
-  Copyright (c) 2017 - 2020, ARM Limited. All rights reserved.
+  Copyright (c) 2017 - 2020, Arm Limited. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -56,6 +56,7 @@ typedef enum ArmObjectID {
   EArmObjDeviceHandleAcpi,             ///< 32 - Device Handle Acpi
   EArmObjDeviceHandlePci,              ///< 33 - Device Handle Pci
   EArmObjGenericInitiatorAffinityInfo, ///< 34 - Generic Initiator Affinity
+  EArmObjSerialPortInfo,               ///< 35 - Generic Serial Port Info
   EArmObjMax
 } EARM_OBJECT_ID;
 
@@ -270,7 +271,8 @@ typedef struct CmArmGicItsInfo {
     Serial Port information for the Platform.
 
     ID: EArmObjSerialConsolePortInfo or
-        EArmObjSerialDebugPortInfo
+        EArmObjSerialDebugPortInfo or
+        EArmObjSerialPortInfo
 */
 typedef struct CmArmSerialPortInfo {
   /// The physical base address for the serial port
@@ -287,6 +289,9 @@ typedef struct CmArmSerialPortInfo {
 
   /// Serial Port subtype
   UINT16  PortSubtype;
+
+  /// The Base address length
+  UINT64  BaseAddressLength;
 } CM_ARM_SERIAL_PORT_INFO;
 
 /** A structure that describes the
diff --git a/DynamicTablesPkg/Include/Library/SsdtSerialPortFixupLib.h b/DynamicTablesPkg/Include/Library/SsdtSerialPortFixupLib.h
new file mode 100644
index 0000000000000000000000000000000000000000..4605f3f34b1d9a2a3af975f801077d6f523e0530
--- /dev/null
+++ b/DynamicTablesPkg/Include/Library/SsdtSerialPortFixupLib.h
@@ -0,0 +1,68 @@
+/** @file
+  Ssdt Serial Port Fixup Library
+
+  Copyright (c) 2020, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#ifndef SSDT_SERIAL_PORT_LIB_H_
+#define SSDT_SERIAL_PORT_LIB_H_
+
+/** Build a SSDT table describing the input serial port.
+
+  The table created by this function must be freed by FreeSsdtSerialTable.
+
+  @param [in]  AcpiTableInfo    Pointer to the ACPI table information.
+  @param [in]  SerialPortInfo   Serial port to describe in the SSDT table.
+  @param [in]  Name             The Name to give to the Device.
+                                Must be a NULL-terminated ASL NameString
+                                e.g.: "DEV0", "DV15.DEV0", etc.
+  @param [in]  Uid              UID for the Serial Port.
+  @param [out] Table            If success, pointer to the created SSDT table.
+
+  @retval EFI_SUCCESS            Table generated successfully.
+  @retval EFI_INVALID_PARAMETER  A parameter is invalid.
+  @retval EFI_NOT_FOUND          Could not find information.
+  @retval EFI_OUT_OF_RESOURCES   Could not allocate memory.
+**/
+EFI_STATUS
+EFIAPI
+BuildSsdtSerialPortTable (
+  IN  CONST CM_STD_OBJ_ACPI_TABLE_INFO    *  AcpiTableInfo,
+  IN  CONST CM_ARM_SERIAL_PORT_INFO       *  SerialPortInfo,
+  IN  CONST CHAR8                         *  Name,
+  IN  CONST UINT64                           Uid,
+  OUT       EFI_ACPI_DESCRIPTION_HEADER  **  Table
+  );
+
+/** Free an SSDT table previously created by
+    the BuildSsdtSerialTable function.
+
+  @param [in] Table   Pointer to a SSDT table allocated by
+                      the BuildSsdtSerialTable function.
+
+  @retval EFI_SUCCESS           Success.
+**/
+EFI_STATUS
+EFIAPI
+FreeSsdtSerialPortTable (
+  IN EFI_ACPI_DESCRIPTION_HEADER  * Table
+  );
+
+/** Validate the Serial Port Information.
+
+  @param [in]  SerialPortInfoTable    Table of CM_ARM_SERIAL_PORT_INFO.
+  @param [in]  SerialPortCount        Count of SerialPort in the table.
+
+  @retval EFI_SUCCESS             Success.
+  @retval EFI_INVALID_PARAMETER   Invalid parameter.
+**/
+EFI_STATUS
+EFIAPI
+ValidateSerialPortInfo (
+  IN  CONST CM_ARM_SERIAL_PORT_INFO  * SerialPortInfoTable,
+  IN        UINT32                     SerialPortCount
+  );
+
+#endif // SSDT_SERIAL_PORT_LIB_H_
diff --git a/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.c b/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.c
new file mode 100644
index 0000000000000000000000000000000000000000..944bfd6eaaabc9dbc7223c9888fb5f11eeb1bda9
--- /dev/null
+++ b/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.c
@@ -0,0 +1,524 @@
+/** @file
+  SSDT Serial Port Fixup Library.
+
+  Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Reference(s):
+  - Arm Server Base Boot Requirements (SBBR), s4.2.1.8 "SPCR".
+  - Microsoft Debug Port Table 2 (DBG2) Specification - December 10, 2015.
+**/
+
+#include <IndustryStandard/DebugPort2Table.h>
+#include <Library/AcpiLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Protocol/AcpiTable.h>
+
+// Module specific include files.
+#include <AcpiTableGenerator.h>
+#include <ConfigurationManagerObject.h>
+#include <ConfigurationManagerHelper.h>
+#include <Library/AmlLib/AmlLib.h>
+#include <Library/TableHelperLib.h>
+#include <Protocol/ConfigurationManagerProtocol.h>
+
+/** C array containing the compiled AML template.
+    This symbol is defined in the auto generated C file
+    containing the AML bytecode array.
+*/
+extern CHAR8  ssdtserialporttemplate_aml_code[];
+
+/** UART address range length.
+*/
+#define MIN_UART_ADDRESS_LENGTH         0x1000U
+
+/** Validate the Serial Port Information.
+
+  @param [in]  SerialPortInfoTable    Table of CM_ARM_SERIAL_PORT_INFO.
+  @param [in]  SerialPortCount        Count of SerialPort in the table.
+
+  @retval EFI_SUCCESS             Success.
+  @retval EFI_INVALID_PARAMETER   Invalid parameter.
+**/
+EFI_STATUS
+EFIAPI
+ValidateSerialPortInfo (
+  IN  CONST CM_ARM_SERIAL_PORT_INFO  * SerialPortInfoTable,
+  IN        UINT32                     SerialPortCount
+  )
+{
+  UINT32                            Index;
+  CONST CM_ARM_SERIAL_PORT_INFO   * SerialPortInfo;
+
+  if  ((SerialPortInfoTable == NULL)  ||
+       (SerialPortCount == 0)) {
+    ASSERT (0);
+    return EFI_INVALID_PARAMETER;
+  }
+
+  for (Index = 0; Index < SerialPortCount; Index++) {
+    SerialPortInfo = &SerialPortInfoTable[Index];
+    ASSERT (SerialPortInfo != NULL);
+
+    if ((SerialPortInfo == NULL ) ||
+        (SerialPortInfo->BaseAddress == 0)) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "ERROR: UART port base address is invalid. BaseAddress = 0x%llx\n",
+        SerialPortInfo->BaseAddress
+        ));
+      return EFI_INVALID_PARAMETER;
+    }
+
+    if ((SerialPortInfo->PortSubtype !=
+         EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_PL011_UART) &&
+        (SerialPortInfo->PortSubtype !=
+         EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART_2X) &&
+        (SerialPortInfo->PortSubtype !=
+         EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART) &&
+        (SerialPortInfo->PortSubtype !=
+         EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_DCC) &&
+        (SerialPortInfo->PortSubtype !=
+         EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_FULL_16550)) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "ERROR: UART port subtype is invalid."
+        " UART Base  = 0x%llx, PortSubtype = 0x%x\n",
+        SerialPortInfo->BaseAddress,
+        SerialPortInfo->PortSubtype
+        ));
+      return EFI_INVALID_PARAMETER;
+    }
+
+    DEBUG ((DEBUG_INFO, "UART Configuration:\n"));
+    DEBUG ((
+      DEBUG_INFO,
+      "  UART Base  = 0x%llx\n", SerialPortInfo->BaseAddress
+      ));
+    DEBUG ((
+      DEBUG_INFO,
+      "  Length     = 0x%llx\n",
+      SerialPortInfo->BaseAddressLength
+      ));
+    DEBUG ((DEBUG_INFO, "  Clock      = %lu\n", SerialPortInfo->Clock));
+    DEBUG ((DEBUG_INFO, "  BaudRate   = %llu\n", SerialPortInfo->BaudRate));
+    DEBUG ((DEBUG_INFO, "  Interrupt  = %lu\n", SerialPortInfo->Interrupt));
+  } // for
+
+  return EFI_SUCCESS;
+}
+
+/** Fixup the Serial Port Ids (_UID, _HID, _CID).
+
+  @param  [in]  RootNodeHandle  Pointer to the root of an AML tree.
+  @param  [in]  Uid             UID for the Serial Port.
+  @param  [in]  SerialPortInfo  Pointer to a Serial Port Information
+                                structure.
+                                Get the Serial Port Information from there.
+
+  @retval  EFI_SUCCESS            The function completed successfully.
+  @retval  EFI_INVALID_PARAMETER  Invalid parameter.
+  @retval  EFI_NOT_FOUND          Could not find information.
+  @retval  EFI_OUT_OF_RESOURCES   Out of resources.
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+FixupIds (
+  IN  OUT       AML_ROOT_NODE_HANDLE        RootNodeHandle,
+  IN      CONST UINT64                      Uid,
+  IN      CONST CM_ARM_SERIAL_PORT_INFO   * SerialPortInfo
+  )
+{
+  EFI_STATUS                Status;
+  AML_OBJECT_NODE_HANDLE    NameOpIdNode;
+  CONST CHAR8             * HidString;
+  CONST CHAR8             * CidString;
+
+  // Get the _CID and _HID value to write.
+  switch (SerialPortInfo->PortSubtype) {
+    case EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_FULL_16550:
+    {
+      HidString = "PNP0501";
+      CidString = "PNP0500";
+      break;
+    }
+    case EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_PL011_UART:
+    {
+      HidString = "ARMH0011";
+      CidString = "ARMHB000";
+      break;
+    }
+    case EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART:
+    case EFI_ACPI_DBG2_PORT_SUBTYPE_SERIAL_ARM_SBSA_GENERIC_UART_2X:
+    {
+      HidString = "ARMH0011";
+      CidString = "";
+      break;
+    }
+    default:
+    {
+      return EFI_INVALID_PARAMETER;
+    }
+  } // switch
+
+  // Get the _UID NameOp object defined by the "Name ()" statement,
+  // and update its value.
+  Status = AmlFindNode (
+             RootNodeHandle,
+             "\\_SB_.COM0._UID",
+             &NameOpIdNode
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = AmlNameOpUpdateInteger (NameOpIdNode, (UINT64)Uid);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Get the _HID NameOp object defined by the "Name ()" statement,
+  // and update its value.
+  Status = AmlFindNode (
+             RootNodeHandle,
+             "\\_SB_.COM0._HID",
+             &NameOpIdNode
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = AmlNameOpUpdateString (NameOpIdNode, HidString);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Get the _CID NameOp object defined by the "Name ()" statement,
+  // and update its value.
+  Status = AmlFindNode (
+             RootNodeHandle,
+             "\\_SB_.COM0._CID",
+             &NameOpIdNode
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // If we have a CID then update a _CID node else delete the node.
+  if (AsciiStrLen (CidString) != 0) {
+    Status = AmlNameOpUpdateString (NameOpIdNode, CidString);
+  } else {
+    // First detach the node from the tree.
+    Status = AmlDetachNode (NameOpIdNode);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+
+    // Delete the detached node.
+    Status = AmlDeleteTree (NameOpIdNode);
+  }
+
+  return Status;
+}
+
+/** Fixup the Serial Port _CRS values (BaseAddress, ...).
+
+  @param  [in]  RootNodeHandle  Pointer to the root of an AML tree.
+  @param  [in]  SerialPortInfo  Pointer to a Serial Port Information
+                                structure.
+                                Get the Serial Port Information from there.
+
+  @retval  EFI_SUCCESS            The function completed successfully.
+  @retval  EFI_INVALID_PARAMETER  Invalid parameter.
+  @retval  EFI_NOT_FOUND          Could not find information.
+  @retval  EFI_OUT_OF_RESOURCES   Out of resources.
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+FixupCrs (
+  IN  OUT       AML_ROOT_NODE_HANDLE        RootNodeHandle,
+  IN      CONST CM_ARM_SERIAL_PORT_INFO   * SerialPortInfo
+  )
+{
+  EFI_STATUS                Status;
+  AML_OBJECT_NODE_HANDLE    NameOpCrsNode;
+  AML_DATA_NODE_HANDLE      QWordRdNode;
+  AML_DATA_NODE_HANDLE      InterruptRdNode;
+
+  // Get the "_CRS" object defined by the "Name ()" statement.
+  Status = AmlFindNode (
+             RootNodeHandle,
+             "\\_SB_.COM0._CRS",
+             &NameOpCrsNode
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Get the first Rd node in the "_CRS" object.
+  Status = AmlNameOpCrsGetFirstRdNode (NameOpCrsNode, &QWordRdNode);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  if (QWordRdNode == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Update the Serial Port base address and length.
+  Status = AmlUpdateRdQWord (
+             QWordRdNode,
+             SerialPortInfo->BaseAddress,
+             ((SerialPortInfo->BaseAddressLength < MIN_UART_ADDRESS_LENGTH) ?
+                 MIN_UART_ADDRESS_LENGTH: SerialPortInfo->BaseAddressLength)
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Get the Interrupt node.
+  // It is the second Resource Data element in the NameOpCrsNode's
+  // variable list of arguments.
+  Status = AmlNameOpCrsGetNextRdNode (QWordRdNode, &InterruptRdNode);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  if (InterruptRdNode == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  // Update the interrupt number.
+  return AmlUpdateRdInterrupt (InterruptRdNode, SerialPortInfo->Interrupt);
+}
+
+/** Fixup the Serial Port device name.
+
+  @param  [in]  RootNodeHandle  Pointer to the root of an AML tree.
+  @param  [in]  SerialPortInfo  Pointer to a Serial Port Information
+                                structure.
+                                Get the Serial Port Information from there.
+  @param  [in]  Name            The Name to give to the Device.
+                                Must be a NULL-terminated ASL NameString
+                                e.g.: "DEV0", "DV15.DEV0", etc.
+
+  @retval  EFI_SUCCESS            The function completed successfully.
+  @retval  EFI_INVALID_PARAMETER  Invalid parameter.
+  @retval  EFI_NOT_FOUND          Could not find information.
+  @retval  EFI_OUT_OF_RESOURCES   Out of resources.
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+FixupName (
+  IN  OUT       AML_ROOT_NODE_HANDLE        RootNodeHandle,
+  IN      CONST CM_ARM_SERIAL_PORT_INFO   * SerialPortInfo,
+  IN      CONST CHAR8                     * Name
+  )
+{
+  EFI_STATUS                Status;
+  AML_OBJECT_NODE_HANDLE    DeviceNode;
+
+  // Get the COM0 variable defined by the "Device ()" statement.
+  Status = AmlFindNode (RootNodeHandle, "\\_SB_.COM0", &DeviceNode);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Update the Device's name.
+  return AmlDeviceOpUpdateName (DeviceNode, (CHAR8*)Name);
+}
+
+/** Fixup the Serial Port Information in the AML tree.
+
+  For each template value:
+   - find the node to update;
+   - update the value.
+
+  @param  [in]  RootNodeHandle  Pointer to the root of the AML tree.
+  @param  [in]  SerialPortInfo  Pointer to a Serial Port Information
+                                structure.
+                                Get the Serial Port Information from there.
+  @param  [in]  Name            The Name to give to the Device.
+                                Must be a NULL-terminated ASL NameString
+                                e.g.: "DEV0", "DV15.DEV0", etc.
+  @param  [in]  Uid             UID for the Serial Port.
+  @param  [out] Table           If success, contains the serialized
+                                SSDT table.
+
+  @retval  EFI_SUCCESS            The function completed successfully.
+  @retval  EFI_INVALID_PARAMETER  Invalid parameter.
+  @retval  EFI_NOT_FOUND          Could not find information.
+  @retval  EFI_OUT_OF_RESOURCES   Out of resources.
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+FixupSerialPortInfo (
+  IN  OUT       AML_ROOT_NODE_HANDLE              RootNodeHandle,
+  IN      CONST CM_ARM_SERIAL_PORT_INFO         * SerialPortInfo,
+  IN      CONST CHAR8                           * Name,
+  IN      CONST UINT64                            Uid,
+      OUT       EFI_ACPI_DESCRIPTION_HEADER    ** Table
+  )
+{
+  EFI_STATUS                Status;
+
+  ASSERT (RootNodeHandle != NULL);
+  ASSERT (SerialPortInfo != NULL);
+  ASSERT (Name != NULL);
+  ASSERT (Table != NULL);
+
+  // Fixup the _UID, _HID and _CID values.
+  Status = FixupIds (RootNodeHandle, Uid, SerialPortInfo);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Fixup the _CRS values.
+  Status = FixupCrs (RootNodeHandle, SerialPortInfo);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Fixup the serial-port name.
+  // This MUST be done at the end, otherwise AML paths won't be valid anymore.
+  return FixupName (RootNodeHandle, SerialPortInfo, Name);
+}
+
+/** Free an SSDT table previously created by
+    the BuildSsdtSerialTable function.
+
+  @param [in] Table   Pointer to a SSDT table allocated by
+                      the BuildSsdtSerialTable function.
+
+  @retval EFI_SUCCESS           Success.
+**/
+EFI_STATUS
+EFIAPI
+FreeSsdtSerialPortTable (
+  IN EFI_ACPI_DESCRIPTION_HEADER  * Table
+  )
+{
+  ASSERT (Table != NULL);
+  FreePool (Table);
+  return EFI_SUCCESS;
+}
+
+/** Build a SSDT table describing the input serial port.
+
+  The table created by this function must be freed by FreeSsdtSerialTable.
+
+  @param [in]  AcpiTableInfo    Pointer to the ACPI table information.
+  @param [in]  SerialPortInfo   Serial port to describe in the SSDT table.
+  @param [in]  Name             The Name to give to the Device.
+                                Must be a NULL-terminated ASL NameString
+                                e.g.: "DEV0", "DV15.DEV0", etc.
+  @param [in]  Uid              UID for the Serial Port.
+  @param [out] Table            If success, pointer to the created SSDT table.
+
+  @retval EFI_SUCCESS            Table generated successfully.
+  @retval EFI_INVALID_PARAMETER  A parameter is invalid.
+  @retval EFI_NOT_FOUND          Could not find information.
+  @retval EFI_OUT_OF_RESOURCES   Could not allocate memory.
+**/
+EFI_STATUS
+EFIAPI
+BuildSsdtSerialPortTable (
+  IN  CONST CM_STD_OBJ_ACPI_TABLE_INFO    *  AcpiTableInfo,
+  IN  CONST CM_ARM_SERIAL_PORT_INFO       *  SerialPortInfo,
+  IN  CONST CHAR8                         *  Name,
+  IN  CONST UINT64                           Uid,
+  OUT       EFI_ACPI_DESCRIPTION_HEADER  **  Table
+  )
+{
+  EFI_STATUS              Status;
+  EFI_STATUS              Status1;
+  AML_ROOT_NODE_HANDLE    RootNodeHandle;
+
+  ASSERT (AcpiTableInfo != NULL);
+  ASSERT (SerialPortInfo != NULL);
+  ASSERT (Name != NULL);
+  ASSERT (Table != NULL);
+
+  // Validate the Serial Port Info.
+  Status = ValidateSerialPortInfo (SerialPortInfo, 1);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  // Parse the SSDT Serial Port Template.
+  Status = AmlParseDefinitionBlock (
+             (EFI_ACPI_DESCRIPTION_HEADER*)ssdtserialporttemplate_aml_code,
+             &RootNodeHandle
+             );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "ERROR: SSDT-SERIAL-PORT-FIXUP:"
+      " Failed to parse SSDT Serial Port Template. Status = %r\n",
+      Status
+      ));
+    return Status;
+  }
+
+  // Fixup the template values.
+  Status = FixupSerialPortInfo (
+             RootNodeHandle,
+             SerialPortInfo,
+             Name,
+             Uid,
+             Table
+             );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "ERROR: SSDT-SERIAL-PORT-FIXUP: Failed to fixup SSDT Serial Port Table."
+      " Status = %r\n",
+      Status
+      ));
+    goto exit_handler;
+  }
+
+  // Serialize the tree.
+  Status = AmlSerializeDefinitionBlock (
+             RootNodeHandle,
+             Table
+             );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "ERROR: SSDT-SERIAL-PORT-FIXUP: Failed to Serialize SSDT Table Data."
+      " Status = %r\n",
+      Status
+      ));
+  }
+
+exit_handler:
+  // Cleanup
+  if (RootNodeHandle != NULL) {
+    Status1 = AmlDeleteTree (RootNodeHandle);
+    if (EFI_ERROR (Status1)) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "ERROR: SSDT-SERIAL-PORT-FIXUP: Failed to cleanup AML tree."
+        " Status = %r\n",
+        Status1
+        ));
+      // If Status was success but we failed to delete the AML Tree
+      // return Status1 else return the original error code, i.e. Status.
+      if (!EFI_ERROR (Status)) {
+        return Status1;
+      }
+    }
+  }
+
+  return Status;
+}
diff --git a/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.inf b/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.inf
new file mode 100644
index 0000000000000000000000000000000000000000..af3d404393f5f1385ab2d40f45f7222ab66f9b3a
--- /dev/null
+++ b/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortFixupLib.inf
@@ -0,0 +1,30 @@
+## @file
+#  SSDT Serial Port fixup Library
+#
+#  Copyright (c) 2020, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+  INF_VERSION    = 0x0001001B
+  BASE_NAME      = DynamicSsdtSerialPortFixupLib
+  FILE_GUID      = AC5978CC-5B62-4466-AD04-23644C2C38C2
+  VERSION_STRING = 1.0
+  MODULE_TYPE    = DXE_DRIVER
+  LIBRARY_CLASS  = SsdtSerialPortFixupLib
+
+[Sources]
+  SsdtSerialPortFixupLib.c
+  SsdtSerialPortTemplate.asl
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+  DynamicTablesPkg/DynamicTablesPkg.dec
+
+[LibraryClasses]
+  AmlLib
+  BaseLib
+
diff --git a/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortTemplate.asl b/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortTemplate.asl
new file mode 100644
index 0000000000000000000000000000000000000000..fcae2160ac3df6c3e9e48115b0b7195c3001c782
--- /dev/null
+++ b/DynamicTablesPkg/Library/Common/SsdtSerialPortFixupLib/SsdtSerialPortTemplate.asl
@@ -0,0 +1,60 @@
+/** @file
+  SSDT Serial Template
+
+  Copyright (c) 2019 - 2020, Arm Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Reference(s):
+  - Arm Server Base Boot Requirements (SBBR), s4.2.1.8 "SPCR".
+
+  @par Glossary:
+    - {template} - Data fixed up using AML Fixup APIs.
+**/
+
+DefinitionBlock ("SsdtSerialPortTemplate.aml", "SSDT", 2, "ARMLTD", "SERIAL", 1) {
+  Scope (_SB) {
+    // UART PL011
+    Device (COM0) {                                       // {template}
+      Name (_UID, 0x0)                                    // {template}
+      Name (_HID, "HID0000")                              // {template}
+      Name (_CID, "CID0000")                              // {template}
+
+      Method(_STA) {
+        Return(0xF)
+      }
+
+      Name (_CRS, ResourceTemplate() {
+        QWordMemory (
+          ,                   // ResourceUsage
+          ,                   // Decode
+          ,                   // IsMinFixed
+          ,                   // IsMaxFixed
+          ,                   // Cacheable
+          ReadWrite,          // ReadAndWrite
+          0x0,                // AddressGranularity
+          0xA0000000,         // AddressMinimum           // {template}
+          0xAFFFFFFF,         // AddressMaximum           // {template}
+          0,                  // AddressTranslation
+          0x10000000,         // RangeLength              // {template}
+          ,                   // ResourceSourceIndex
+          ,                   // ResourceSource
+          ,                   // DescriptorName
+          ,                   // MemoryRangeType
+                              // TranslationType
+        ) // QWordMemory
+        Interrupt (
+          ResourceConsumer,   // ResourceUsage
+          Level,              // EdgeLevel
+          ActiveHigh,         // ActiveLevel
+          Exclusive,          // Shared
+          ,                   // ResourceSourceIndex
+          ,                   // ResourceSource
+                              // DescriptorName
+          ) {
+            0xA5                                          // {template}
+        } // Interrupt
+      }) // Name
+    } // Device
+  } // Scope (_SB)
+}
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'


  parent reply	other threads:[~2020-08-12 15:23 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-12 15:22 [PATCH v1 00/30] Add Dynamic AML generation support Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 01/30] DynamicTablesPkg: Introduction to Dynamic AML Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 02/30] DynamicTablesPkg: AmlLib definitions Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 03/30] DynamicTablesPkg: AML grammar definition Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 04/30] DynamicTablesPkg: AML node definitions Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 05/30] DynamicTablesPkg: AML tree interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 06/30] DynamicTablesPkg: AML tree enumerator Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 07/30] DynamicTablesPkg: AML tree traversal Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 08/30] DynamicTablesPkg: AML tree iterator Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 09/30] DynamicTablesPkg: AML tree/node cloning Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 10/30] DynamicTablesPkg: AML utility interfaces Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 11/30] DynamicTablesPkg: AML and ASL string helper Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 12/30] DynamicTablesPkg: AML stream interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 13/30] DynamicTablesPkg: AML serialise interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 14/30] DynamicTablesPkg: AML debug logging Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 15/30] DynamicTablesPkg: AML ACPI Namespace interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 16/30] DynamicTablesPkg: AML Parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 17/30] DynamicTablesPkg: AML resource data helper Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 18/30] DynamicTablesPkg: AML resource data parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 19/30] DynamicTablesPkg: AML Method parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 20/30] DynamicTablesPkg: AML Field list parser Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 21/30] DynamicTablesPkg: AML Codegen Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 22/30] DynamicTablesPkg: AML Resource Data Codegen Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 23/30] DynamicTablesPkg: AML Core interface Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 24/30] DynamicTablesPkg: AmlLib APIs Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 25/30] DynamicTablesPkg: Dynamic AML: Add AmlLib library Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 26/30] DynamicTablesPkg: Add AsciiFromHex helper function Sami Mujawar
2020-08-12 15:22 ` Sami Mujawar [this message]
2020-08-12 15:22 ` [PATCH v1 28/30] DynamicTablesPkg: SSDT Serial Port generator Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 29/30] DynamicTablesPkg: Add SSDT Serial port for SPCR Sami Mujawar
2020-08-12 15:22 ` [PATCH v1 30/30] DynamicTablesPkg: Add SSDT Serial port for DBG2 Sami Mujawar
2020-08-13 15:16 ` [edk2-devel] [PATCH v1 00/30] Add Dynamic AML generation support Alexei Fedorov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200812152236.31164-28-sami.mujawar@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox