]> git.proxmox.com Git - mirror_edk2.git/blobdiff - DynamicTablesPkg/Library/Acpi/Arm/AcpiMadtLibArm/MadtGenerator.c
DynamicTablesPkg: Test for duplicate UIDs in MADT generator
[mirror_edk2.git] / DynamicTablesPkg / Library / Acpi / Arm / AcpiMadtLibArm / MadtGenerator.c
index 06794758308e028b7d8da06670b1793cbdb3ae81..613bf665d9cecc6495f5ac84c2515ea89f476194 100644 (file)
@@ -2,13 +2,7 @@
   MADT 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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
   - ACPI 6.2 Specification - Errata A, September 2017\r
@@ -146,28 +140,96 @@ AddGICC (
   Gicc->Reserved2[2] = EFI_ACPI_RESERVED_BYTE;\r
 }\r
 \r
+/**\r
+  Function to test if two GIC CPU Interface information structures have the\r
+  same ACPI Processor UID.\r
+\r
+  @param [in]  GicCInfo1          Pointer to the first GICC info structure.\r
+  @param [in]  GicCInfo2          Pointer to the second GICC info structure.\r
+  @param [in]  Index1             Index of GicCInfo1 in the shared list of GIC\r
+                                  CPU Interface Info structures.\r
+  @param [in]  Index2             Index of GicCInfo2 in the shared list of GIC\r
+                                  CPU Interface Info structures.\r
+\r
+  @retval TRUE                    GicCInfo1 and GicCInfo2 have the same UID.\r
+  @retval FALSE                   GicCInfo1 and GicCInfo2 have different UIDs.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsAcpiUidEqual (\r
+  IN  CONST VOID          * GicCInfo1,\r
+  IN  CONST VOID          * GicCInfo2,\r
+  IN        UINTN           Index1,\r
+  IN        UINTN           Index2\r
+  )\r
+{\r
+  UINT32      Uid1;\r
+  UINT32      Uid2;\r
+\r
+  ASSERT ((GicCInfo1 != NULL) && (GicCInfo2 != NULL));\r
+\r
+  Uid1 = ((CM_ARM_GICC_INFO*)GicCInfo1)->AcpiProcessorUid;\r
+  Uid2 = ((CM_ARM_GICC_INFO*)GicCInfo2)->AcpiProcessorUid;\r
+\r
+  if (Uid1 == Uid2) {\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "ERROR: MADT: GICC Info Structures %d and %d have the same ACPI " \\r
+      "Processor UID: 0x%x.\n",\r
+      Index1,\r
+      Index2,\r
+      Uid1\r
+      ));\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 /** Add the GIC CPU Interface Information to the MADT Table.\r
 \r
-  @param [in]  Gicc      Pointer to GIC CPU Interface\r
-                         structure list.\r
-  @param [in]  GicCInfo  Pointer to the GIC CPU\r
-                         Information list.\r
-  @param [in]  GicCCount Count of GIC CPU Interfaces.\r
+  This function also checks for duplicate ACPI Processor UIDs.\r
+\r
+  @param [in]  Gicc                 Pointer to GIC CPU Interface structure list.\r
+  @param [in]  GicCInfo             Pointer to the GIC CPU Information list.\r
+  @param [in]  GicCCount            Count of GIC CPU Interfaces.\r
+\r
+  @retval EFI_SUCCESS               GIC CPU Interface Information was added\r
+                                    successfully.\r
+  @retval EFI_INVALID_PARAMETER     One or more invalid GIC CPU Info values were\r
+                                    provided and the generator failed to add the\r
+                                    information to the table.\r
 **/\r
 STATIC\r
-VOID\r
+EFI_STATUS\r
 AddGICCList (\r
   IN  EFI_ACPI_6_2_GIC_STRUCTURE  * Gicc,\r
   IN  CONST CM_ARM_GICC_INFO      * GicCInfo,\r
   IN        UINT32                  GicCCount\r
   )\r
 {\r
+  BOOLEAN   IsAcpiProcUidDuplicated;\r
+\r
   ASSERT (Gicc != NULL);\r
   ASSERT (GicCInfo != NULL);\r
 \r
+  IsAcpiProcUidDuplicated = FindDuplicateValue (\r
+                              GicCInfo,\r
+                              GicCCount,\r
+                              sizeof (CM_ARM_GICC_INFO),\r
+                              IsAcpiUidEqual\r
+                              );\r
+  // Duplicate ACPI Processor UID was found so the GICC info provided\r
+  // is invalid\r
+  if (IsAcpiProcUidDuplicated) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   while (GicCCount-- != 0) {\r
     AddGICC (Gicc++, GicCInfo++);\r
   }\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /** Update the GIC Distributor Information in the MADT Table.\r
@@ -192,7 +254,9 @@ AddGICD (
   // UINT16 Reserved\r
   Gicd->Reserved1 = EFI_ACPI_RESERVED_WORD;\r
   // UINT32 Identifier\r
-  Gicd->GicId = GicDInfo->GicId;\r
+  // One, and only one, GIC distributor structure must be present\r
+  // in the MADT for an ARM based system\r
+  Gicd->GicId = 0;\r
   // UINT64 PhysicalBaseAddress\r
   Gicd->PhysicalBaseAddress = GicDInfo->PhysicalBaseAddress;\r
   // UINT32 VectorBase\r
@@ -569,7 +633,7 @@ BuildMadtTable (
              CfgMgrProtocol,\r
              This,\r
              &Madt->Header,\r
-             AcpiTableInfo->AcpiTableRevision,\r
+             AcpiTableInfo,\r
              TableSize\r
              );\r
   if (EFI_ERROR (Status)) {\r
@@ -581,11 +645,19 @@ BuildMadtTable (
     goto error_handler;\r
   }\r
 \r
-  AddGICCList (\r
+  Status = AddGICCList (\r
     (EFI_ACPI_6_2_GIC_STRUCTURE*)((UINT8*)Madt + GicCOffset),\r
     GicCInfo,\r
     GicCCount\r
     );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "ERROR: MADT: Failed to add GICC structures. Status = %r\n",\r
+      Status\r
+      ));\r
+    goto error_handler;\r
+  }\r
 \r
   AddGICD (\r
     (EFI_ACPI_6_2_GIC_DISTRIBUTOR_STRUCTURE*)((UINT8*)Madt + GicDOffset),\r