/** @file

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

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

**/

#include <IndustryStandard/Acpi.h>

#include "AcpiTables.h"

#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;
} SYNQUACER_PPTT_CORE;

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

typedef struct {
  EFI_ACPI_6_2_PPTT_STRUCTURE_PROCESSOR                     Package;
  UINT32                                                    Offset[1];
  EFI_ACPI_6_2_PPTT_STRUCTURE_CACHE                         L3Cache;
  SYNQUACER_PPTT_CLUSTER                                    Clusters[12];
} SYNQUACER_PPTT_PACKAGE;

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

#define PPTT_CORE(pid, cid, id) {                                             \
  {                                                                           \
    EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,                                         \
    FIELD_OFFSET (SYNQUACER_PPTT_CORE, DCache),                               \
    {},                                                                       \
    {                                                                         \
      0,                                        /* PhysicalPackage */         \
      EFI_ACPI_6_2_PPTT_PROCESSOR_ID_VALID,     /* AcpiProcessorIdValid */    \
    },                                                                        \
    FIELD_OFFSET (SYNQUACER_PPTT_TABLE,                                       \
                  Packages[pid].Clusters[cid]), /* Parent */                  \
    2 * (cid) + (id),                           /* AcpiProcessorId */         \
    2,                                          /* NumberOfPrivateResources */\
  }, {                                                                        \
    FIELD_OFFSET (SYNQUACER_PPTT_TABLE,                                       \
                  Packages[pid].Clusters[cid].Cores[id].DCache),              \
    FIELD_OFFSET (SYNQUACER_PPTT_TABLE,                                       \
                  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 */                                                  \
    128,          /* NumberOfSets */                                          \
    4,            /* 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 */                                      \
    SIZE_32KB,    /* Size */                                                  \
    256,          /* NumberOfSets */                                          \
    2,            /* 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 (SYNQUACER_PPTT_CLUSTER, L2Cache),                           \
    {},                                                                       \
    {                                                                         \
      0,                                      /* PhysicalPackage */           \
      EFI_ACPI_6_2_PPTT_PROCESSOR_ID_INVALID, /* AcpiProcessorIdValid */      \
    },                                                                        \
    FIELD_OFFSET (SYNQUACER_PPTT_TABLE, Packages[pid]), /* Parent */          \
    0,                                        /* AcpiProcessorId */           \
    1,                                        /* NumberOfPrivateResources */  \
  }, {                                                                        \
    FIELD_OFFSET (SYNQUACER_PPTT_TABLE, 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_256KB,   /* 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),                                                   \
  }                                                                           \
}

STATIC SYNQUACER_PPTT_TABLE mSynQuacerPpttTable = {
  {
    __ACPI_HEADER(EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_STRUCTURE_SIGNATURE,
                  SYNQUACER_PPTT_TABLE,
                  EFI_ACPI_6_2_PROCESSOR_PROPERTIES_TOPOLOGY_TABLE_REVISION),
  },
  {
    {
      {
        EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR,
        FIELD_OFFSET (SYNQUACER_PPTT_PACKAGE, L3Cache),
        {},
        {
          1,                                      /* PhysicalPackage */
          EFI_ACPI_6_2_PPTT_PROCESSOR_ID_INVALID, /* AcpiProcessorIdValid */
        },
        0,                                        /* Parent */
        0,                                        /* AcpiProcessorId */
        1,                                        /* NumberOfPrivateResources */
      }, {
        FIELD_OFFSET (SYNQUACER_PPTT_TABLE, 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_4MB,                                 /* Size */
        4096,                                     /* 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_CLUSTER (0, 0),
        PPTT_CLUSTER (0, 1),
        PPTT_CLUSTER (0, 2),
        PPTT_CLUSTER (0, 3),
        PPTT_CLUSTER (0, 4),
        PPTT_CLUSTER (0, 5),
        PPTT_CLUSTER (0, 6),
        PPTT_CLUSTER (0, 7),
        PPTT_CLUSTER (0, 8),
        PPTT_CLUSTER (0, 9),
        PPTT_CLUSTER (0, 10),
        PPTT_CLUSTER (0, 11),
      }
    }
  }
};

VOID * CONST ReferenceAcpiTable = &mSynQuacerPpttTable;
