/*
 * Copyright (c) 2013 Linaro Limited
 *. All rights reserved.
 * SPDX-License-Identifier: BSD-2-Clause-Patent
 *
 * Contributors:
 *     Yi Li - yi.li@linaro.org
 *
 *  Based on the files under ArmPlatformPkg/ArmJunoPkg/AcpiTables/
*/

#include <IndustryStandard/Acpi.h>
#include "Hi1610Platform.h"
#include <Library/AcpiLib.h>
#include <Library/AcpiNextLib.h>


//
// Define the number of each table type.
// This is where the table layout is modified.
//
#define EFI_ACPI_PROCESSOR_LOCAL_APIC_SAPIC_AFFINITY_STRUCTURE_COUNT  4
#define EFI_ACPI_MEMORY_AFFINITY_STRUCTURE_COUNT                      4


#pragma pack(1)
typedef struct {
  EFI_ACPI_5_0_SYSTEM_RESOURCE_AFFINITY_TABLE_HEADER          Header;
  EFI_ACPI_5_0_PROCESSOR_LOCAL_APIC_SAPIC_AFFINITY_STRUCTURE  Apic;
  EFI_ACPI_5_0_MEMORY_AFFINITY_STRUCTURE                      Memory[2];
  EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE                        GICC[16];
} EFI_ACPI_5_0_SYSTEM_RESOURCE_AFFINITY_TABLE;

#pragma pack()


//
// Static Resource Affinity Table definition
//
EFI_ACPI_5_0_SYSTEM_RESOURCE_AFFINITY_TABLE Srat = {
  {
    {EFI_ACPI_5_0_SYSTEM_RESOURCE_AFFINITY_TABLE_SIGNATURE,
    sizeof (EFI_ACPI_5_0_SYSTEM_RESOURCE_AFFINITY_TABLE),
    EFI_ACPI_5_0_SYSTEM_RESOURCE_AFFINITY_TABLE_REVISION,
    0x00,                                                     // Checksum will be updated at runtime
    {EFI_ACPI_ARM_OEM_ID},
    EFI_ACPI_ARM_OEM_TABLE_ID,
    EFI_ACPI_ARM_OEM_REVISION,
    EFI_ACPI_ARM_CREATOR_ID,
    EFI_ACPI_ARM_CREATOR_REVISION},
    /*Reserved*/
    0x00000001,                                  // Reserved to be 1 for backward compatibility
    EFI_ACPI_RESERVED_QWORD
  },
  /**/
  {
      0x00,                                     // Subtable Type:Processor Local APIC/SAPIC Affinity
      sizeof(EFI_ACPI_5_0_PROCESSOR_LOCAL_APIC_SAPIC_AFFINITY_STRUCTURE),                                     //Length
      0x00,                                     //Proximity Domain Low(8)
      0x00,                                     //Apic ID
      0x00000001,                               //Flags
      0x00,                                     //Local Sapic EID
      {0,0,0},                                     //Proximity Domain High(24)
      0x00000000,                               //ClockDomain
  },
  //
  //
  // Memory Affinity
  //
  {
    EFI_ACPI_5_0_MEMORY_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000000,0x00000000,0x40000000,0x00000000,0x00000001),
    EFI_ACPI_5_0_MEMORY_AFFINITY_STRUCTURE_INIT(0x00000000,0x40000000,0x00000002,0xC0000000,0x00000001,0x00000001),
  },

  /*Processor Local x2APIC Affinity*/
  //{
  //   0x02,                                              // Subtable Type:Processor Local x2APIC Affinity
  //   sizeof(EFI_ACPI_5_0_PROCESSOR_LOCAL_X2APIC_AFFINITY_STRUCTURE),
  //   {0,0},                                             //Reserved1
  //   0x00000000,                                        //Proximity Domain
  //   0x00000000,                                        //Apic ID
  //   0x00000001,                                        //Flags
  //   0x00000000,                                        //Clock Domain
  //   {0,0,0,0},                                         //Reserved2
  //},

  {
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000000,0x00000001,0x00000000),   //GICC Affinity Processor 0
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000001,0x00000001,0x00000000),   //GICC Affinity Processor 1
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000002,0x00000001,0x00000000),   //GICC Affinity Processor 2
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000003,0x00000001,0x00000000),   //GICC Affinity Processor 3
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000004,0x00000001,0x00000000),   //GICC Affinity Processor 4
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000005,0x00000001,0x00000000),   //GICC Affinity Processor 5
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000006,0x00000001,0x00000000),   //GICC Affinity Processor 6
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000007,0x00000001,0x00000000),   //GICC Affinity Processor 7
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000008,0x00000001,0x00000000),   //GICC Affinity Processor 8
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x00000009,0x00000001,0x00000000),   //GICC Affinity Processor 9
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x0000000A,0x00000001,0x00000000),   //GICC Affinity Processor 10
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x0000000B,0x00000001,0x00000000),   //GICC Affinity Processor 11
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x0000000C,0x00000001,0x00000000),   //GICC Affinity Processor 12
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x0000000D,0x00000001,0x00000000),   //GICC Affinity Processor 13
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x0000000E,0x00000001,0x00000000),   //GICC Affinity Processor 14
    EFI_ACPI_5_1_GICC_AFFINITY_STRUCTURE_INIT(0x00000000,0x0000000F,0x00000001,0x00000000)    //GICC Affinity Processor 15
  },
};

//
// Reference the table being generated to prevent the optimizer from removing the
// data structure from the executable
//
VOID* CONST ReferenceAcpiTable = &Srat;

