/** @file

  Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
  Copyright (c) 2018, Marvell International Ltd. All rights reserved.<BR>

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

**/

#include <IndustryStandard/Acpi.h>

#include "AcpiHeader.h"

#define NUM_CORES                           FixedPcdGet64 (PcdCoreCount)

#define FIELD_OFFSET(type, name)            __builtin_offsetof(type, name)

#pragma pack(1)
typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Core;
  UINT32                                                    Offset[2];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         DCache;
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         ICache;
} ACPI_6_2_PPTT_CORE;

typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Cluster;
  UINT32                                                    Offset[1];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         L2Cache;
  ACPI_6_2_PPTT_CORE                                        Cores[2];
} ACPI_6_2_PPTT_CLUSTER;

typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Package;
  UINT32                                                    Offset[1];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         L3Cache;
  ACPI_6_2_PPTT_CLUSTER                                     Clusters[NUM_CORES / 2];
} ACPI_6_2_PPTT_PACKAGE;

typedef struct {
  EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_HEADER   Pptt;
  ACPI_6_2_PPTT_PACKAGE                                     Packages[1];
} ACPI_6_2_PPTT_STRUCTURE;
#pragma pack()

#define PPTT_CORE(pid, cid, id) {                                              \
  {                                                                            \
    EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,                                          \
    FIELD_OFFSET (ACPI_6_2_PPTT_CORE, DCache),                                 \
    {},                                                                        \
    {                                                                          \
      0,                                        /* PhysicalPackage */          \
      EFI_ACPI_6_2_PPTT_PROCESSOR_ID_VALID,     /* AcpiProcessorIdValid */     \
    },                                                                         \
    FIELD_OFFSET (ACPI_6_2_PPTT_STRUCTURE,                                     \
                  Packages[pid].Clusters[cid]), /* Parent */                   \
    256 * (cid) + (id),                         /* AcpiProcessorId */          \
    2,                                          /* NumberOfPrivateResources */ \
  }, {                                                                         \
    FIELD_OFFSET (ACPI_6_2_PPTT_STRUCTURE,                                     \
                  Packages[pid].Clusters[cid].Cores[id].DCache),               \
    FIELD_OFFSET (ACPI_6_2_PPTT_STRUCTURE,                                     \
                  Packages[pid].Clusters[cid].Cores[id].ICache),               \
  }, {                                                                         \
    EFI_ACPI_6_2_PPTT_TYPE_CACHE,                                              \
    sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),                                \
    {},                                                                        \
    {                                                                          \
      1,          /* SizePropertyValid */                                      \
      1,          /* NumberOfSetsValid */                                      \
      1,          /* AssociativityValid */                                     \
      1,          /* AllocationTypeValid */                                    \
      1,          /* CacheTypeValid */                                         \
      1,          /* WritePolicyValid */                                       \
      1,          /* LineSizeValid */                                          \
    },                                                                         \
    0,            /* NextLevelOfCache */                                       \
    SIZE_32KB,    /* Size */                                                   \
    256,          /* NumberOfSets */                                           \
    2,            /* Associativity */                                          \
    {                                                                          \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_ALLOCATION_READ_WRITE,                     \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_DATA,                           \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_WRITE_POLICY_WRITE_BACK,                   \
    },                                                                         \
    64            /* LineSize */                                               \
  }, {                                                                         \
    EFI_ACPI_6_2_PPTT_TYPE_CACHE,                                              \
    sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),                                \
    {},                                                                        \
    {                                                                          \
      1,          /* SizePropertyValid */                                      \
      1,          /* NumberOfSetsValid */                                      \
      1,          /* AssociativityValid */                                     \
      1,          /* AllocationTypeValid */                                    \
      1,          /* CacheTypeValid */                                         \
      0,          /* WritePolicyValid */                                       \
      1,          /* LineSizeValid */                                          \
    },                                                                         \
    0,            /* NextLevelOfCache */                                       \
    3 * SIZE_16KB,    /* Size */                                               \
    256,          /* NumberOfSets */                                           \
    3,            /* Associativity */                                          \
    {                                                                          \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_ALLOCATION_READ,    /* AllocationType */   \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_INSTRUCTION,                    \
      0,                                                /* WritePolicy */      \
    },                                                                         \
    64            /* LineSize */                                               \
  }                                                                            \
}

#define PPTT_CLUSTER(pid, cid) {                                                 \
  {                                                                              \
    EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,                                            \
    FIELD_OFFSET (ACPI_6_2_PPTT_CLUSTER, L2Cache),                               \
    {},                                                                          \
    {                                                                            \
      0,                                      /* PhysicalPackage */              \
      EFI_ACPI_6_2_PPTT_PROCESSOR_ID_INVALID, /* AcpiProcessorIdValid */         \
    },                                                                           \
    FIELD_OFFSET (ACPI_6_2_PPTT_STRUCTURE, Packages[pid]), /* Parent */          \
    0,                                        /* AcpiProcessorId */              \
    1,                                        /* NumberOfPrivateResources */     \
  }, {                                                                           \
    FIELD_OFFSET (ACPI_6_2_PPTT_STRUCTURE, Packages[pid].Clusters[cid].L2Cache), \
  }, {                                                                           \
    EFI_ACPI_6_2_PPTT_TYPE_CACHE,                                                \
    sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),                                  \
    {},                                                                          \
    {                                                                            \
      1,          /* SizePropertyValid */                                        \
      1,          /* NumberOfSetsValid */                                        \
      1,          /* AssociativityValid */                                       \
      1,          /* AllocationTypeValid */                                      \
      1,          /* CacheTypeValid */                                           \
      1,          /* WritePolicyValid */                                         \
      1,          /* LineSizeValid */                                            \
    },                                                                           \
    0,            /* NextLevelOfCache */                                         \
    SIZE_512KB,   /* Size */                                                     \
    256,          /* NumberOfSets */                                             \
    16,           /* Associativity */                                            \
    {                                                                            \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_ALLOCATION_READ_WRITE,                       \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_UNIFIED,                          \
      EFI_ACPI_6_2_CACHE_ATTRIBUTES_WRITE_POLICY_WRITE_BACK,                     \
    },                                                                           \
    64            /* LineSize */                                                 \
  }, {                                                                           \
    PPTT_CORE(pid, cid, 0),                                                      \
    PPTT_CORE(pid, cid, 1),                                                      \
  }                                                                              \
}

ACPI_6_2_PPTT_STRUCTURE Pptt = {
  {
    __ACPI_HEADER(EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_STRUCTURE_SIGNATURE,
                    ACPI_6_2_PPTT_STRUCTURE,
                    EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_REVISION),
  },
  {
    {
      {
        EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,
        FIELD_OFFSET (ACPI_6_2_PPTT_PACKAGE, L3Cache),
        {},
        {
          1,                                      /* PhysicalPackage */
          EFI_ACPI_6_2_PPTT_PROCESSOR_ID_INVALID, /* AcpiProcessorIdValid */
        },
        0,                                        /* Parent */
        0,                                        /* AcpiProcessorId */
        1,                                        /* NumberOfPrivateResources */
      }, {
        FIELD_OFFSET (ACPI_6_2_PPTT_STRUCTURE, Packages[0].L3Cache),
      }, {
        EFI_ACPI_6_2_PPTT_TYPE_CACHE,
        sizeof (EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE),
        {},
        {
          1,                                      /* SizePropertyValid */
          1,                                      /* NumberOfSetsValid */
          1,                                      /* AssociativityValid */
          1,                                      /* AllocationTypeValid */
          1,                                      /* CacheTypeValid */
          1,                                      /* WritePolicyValid */
          1,                                      /* LineSizeValid */
        },
        0,                                        /* NextLevelOfCache */
        SIZE_1MB,                                 /* Size */
        2048,                                     /* NumberOfSets */
        8,                                        /* Associativity */
        {
          0,                                      /* AllocationType */
          EFI_ACPI_6_2_CACHE_ATTRIBUTES_CACHE_TYPE_UNIFIED,
          EFI_ACPI_6_2_CACHE_ATTRIBUTES_WRITE_POLICY_WRITE_BACK,
        },
        64                                        /* LineSize */
      }, {
        PPTT_CLUSTER (0, 0),
#if NUM_CORES > 3
        PPTT_CLUSTER (0, 1),
#endif
      }
    }
  }
};

VOID * CONST ReferenceAcpiTable = &Pptt;
