/** @file

  Multiple APIC Description Table (MADT)

  Copyright (c) 2012 - 2014, ARM Ltd. All rights reserved.<BR>
  Copyright (c) 2014 - 2016, AMD Inc. All rights reserved.<BR>
  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <IndustryStandard/Acpi61.h>

#include "AcpiPlatform.h"

#define GICM_SPI_COUNT      (0x100)
#define GICM_SPI_BASE       (0x40)

#define GIC_BASE            (FixedPcdGet64 (PcdGicInterruptInterfaceBase))
#define GICD_BASE           (FixedPcdGet64 (PcdGicDistributorBase))
#define GICV_BASE           (FixedPcdGet64 (PcdGicVirtualInterruptInterfaceBase))
#define GICH_BASE           (FixedPcdGet64 (PcdGicHypervisorInterruptInterfaceBase))
#define VGIC_MAINT_INT      (FixedPcdGet32 (PcdGicVirtualMaintenanceInterrupt))
#define GICVR_BASE          (FixedPcdGet64 (PcdGicVirtualRegisterInterfaceBase))
#define GIC_MSI_FRAME       (FixedPcdGet64 (PcdGicMSIFrameBase))
#define GIC_VERSION         (FixedPcdGet8  (PcdGicVersion))
#define CORES_PER_CLUSTER   (FixedPcdGet32 (PcdSocCoresPerCluster))


/* Macro to populate EFI_ACPI_5_1_GIC_STRUCTURE */
#define AMD_GIC(CpuNum, ClusterId, CoreId, PerfInt)  {                        \
  EFI_ACPI_5_1_GIC,                     /* UINT8 Type */                      \
  sizeof (EFI_ACPI_5_1_GIC_STRUCTURE),  /* UINT8 Length */                    \
  EFI_ACPI_RESERVED_WORD,               /* UINT16 Reserved */                 \
  CpuNum,                               /* UINT32 CPUInterfaceNumber */       \
  (ClusterId << 8) | CoreId,            /* UINT32 AcpiProcessorUid */         \
  0,                                    /* UINT32 Flags */                    \
  0,                                    /* UINT32 ParkingProtocolVersion */   \
  PerfInt,                              /* UINT32 PerformanceInterruptGsiv */ \
  0,                                    /* UINT64 ParkedAddress */            \
  GIC_BASE,                             /* UINT64 PhysicalBaseAddress */      \
  GICV_BASE,                            /* UINT64 GICV */                     \
  GICH_BASE,                            /* UINT64 GICH */                     \
  VGIC_MAINT_INT,                       /* UINT32 VGICMaintenanceInterrupt */ \
  GICVR_BASE,                           /* UINT64 GICRBaseAddress */          \
  (ClusterId << 8) | CoreId             /* UINT64 MPIDR */                    \
  }


#pragma pack(push, 1)
typedef struct {
  EFI_ACPI_5_1_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER Header;
  EFI_ACPI_5_1_GIC_STRUCTURE                          GicC[MAX_CORES];
  EFI_ACPI_5_1_GIC_DISTRIBUTOR_STRUCTURE              GicD;
  EFI_ACPI_5_1_GIC_MSI_FRAME_STRUCTURE                GicM;
} EFI_ACPI_5_1_ARM_MADT_STRUCTURE;
#pragma pack(pop)


STATIC EFI_ACPI_5_1_ARM_MADT_STRUCTURE AcpiMadt = {
  {
    AMD_ACPI_HEADER (EFI_ACPI_5_1_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE,
                     EFI_ACPI_5_1_ARM_MADT_STRUCTURE,
                     EFI_ACPI_5_1_MULTIPLE_APIC_DESCRIPTION_TABLE_REVISION),
    GIC_BASE,                                 // UINT32  LocalApicAddress
    0                                         // UINT32  Flags
  },
  {
    AMD_GIC (0, 0, 0, 39),
    AMD_GIC (1, 0, 1, 40),
    AMD_GIC (2, 1, 0, 41),
    AMD_GIC (3, 1, 1, 42),
    AMD_GIC (4, 2, 0, 43),
    AMD_GIC (5, 2, 1, 44),
    AMD_GIC (6, 3, 0, 45),
    AMD_GIC (7, 3, 1, 46),
  },
  /*
   * GIC Distributor
   */
  {
    EFI_ACPI_5_1_GICD,                                // UINT8 Type
    sizeof (EFI_ACPI_5_1_GIC_DISTRIBUTOR_STRUCTURE),  // UINT8 Length
    EFI_ACPI_RESERVED_WORD,                           // UINT16 Reserved1
    0,                                                // UINT32 GicId
    GICD_BASE,                                        // UINT64 PhysicalBaseAddress
    0,                                                // UINT32 SystemVectorBase
    EFI_ACPI_RESERVED_BYTE,                           // UINT8 GicVersion
    {                                                 // UINT8 Reserved2[3]
      EFI_ACPI_RESERVED_BYTE,
      EFI_ACPI_RESERVED_BYTE,
      EFI_ACPI_RESERVED_BYTE,
    }
  },
  /*
   * GIC MSI Frame
   */
  {
    EFI_ACPI_5_1_GIC_MSI_FRAME,                      // UINT8 Type
    sizeof(EFI_ACPI_5_1_GIC_MSI_FRAME_STRUCTURE),    // UINT8 Length
    EFI_ACPI_RESERVED_WORD,                          // UINT16 Reserved1
    0,                                               // UINT32 GicMsiFrameId
    GIC_MSI_FRAME,                                   // UINT64 PhysicalBaseAddress
    0,                                               // UINT32 Flags
    GICM_SPI_COUNT,                                  // UINT16 SPICount
    GICM_SPI_BASE                                    // UINT16 SPIBase
  }
};

VOID* CONST ReferenceAcpiTable = &AcpiMadt;
