/** @file
  DMA Remapping Reporting (DMAR) ACPI table definition from Intel(R)
  Virtualization Technology for Directed I/O (VT-D) Architecture Specification.

  Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

  @par Revision Reference:
    - Intel(R) Virtualization Technology for Directed I/O (VT-D) Architecture
      Specification v4.0, Dated June 2022.
      https://software.intel.com/content/dam/develop/external/us/en/documents/vt-directed-io-spec.pdf

  @par Glossary:
    - HPET - High Precision Event Timer
    - NUMA - Non-uniform Memory Access
**/

#pragma once

#include <IndustryStandard/Acpi.h>

#pragma pack(1)

///
/// DMA-Remapping Reporting Structure definitions from section 8.1
///@{
#define EFI_ACPI_DMAR_REVISION  0x01

#define EFI_ACPI_DMAR_FLAGS_INTR_REMAP                     BIT0
#define EFI_ACPI_DMAR_FLAGS_X2APIC_OPT_OUT                 BIT1
#define EFI_ACPI_DMAR_FLAGS_DMA_CTRL_PLATFORM_OPT_IN_FLAG  BIT2
///@}

///
/// Remapping Structure Types definitions from section 8.2
///@{
#define EFI_ACPI_DMAR_TYPE_DRHD  0x00
#define EFI_ACPI_DMAR_TYPE_RMRR  0x01
#define EFI_ACPI_DMAR_TYPE_ATSR  0x02
#define EFI_ACPI_DMAR_TYPE_RHSA  0x03
#define EFI_ACPI_DMAR_TYPE_ANDD  0x04
#define EFI_ACPI_DMAR_TYPE_SATC  0x05
#define EFI_ACPI_DMAR_TYPE_SIDP  0x06
///@}

///
/// DMA-Remapping Hardware Unit definitions from section 8.3
///
#define EFI_ACPI_DMAR_DRHD_FLAGS_INCLUDE_PCI_ALL  BIT0

///
/// DMA-Remapping Device Scope Entry Structure definitions from section 8.3.1
///@{
#define EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT           0x01
#define EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_BRIDGE             0x02
#define EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_IOAPIC                 0x03
#define EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_MSI_CAPABLE_HPET       0x04
#define EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_ACPI_NAMESPACE_DEVICE  0x05

#define EFI_ACPI_DEVICE_SCOPE_REQ_WO_PASID_NESTED_NOTALLOWED  BIT0
#define EFI_ACPI_DEVICE_SCOPE_REQ_WO_PASID_PWSNP_NOTALLOWED   BIT1
#define EFI_ACPI_DEVICE_SCOPE_REQ_WO_PASID_PGSNP_NOTALLOWED   BIT2
#define EFI_ACPI_DEVICE_SCOPE_REQ_WO_PASID_ATC_HARDENED       BIT3
#define EFI_ACPI_DEVICE_SCOPE_REQ_WO_PASID_ATC_REQUIRED       BIT4
///@}

///
/// Root Port ATS Capability Reporting Structure definitions from section 8.5
///
#define EFI_ACPI_DMAR_ATSR_FLAGS_ALL_PORTS  BIT0

///
/// Definition for DMA Remapping Structure Header
///
typedef struct {
  UINT16    Type;
  UINT16    Length;
} EFI_ACPI_DMAR_STRUCTURE_HEADER;

///
/// Definition for DMA-Remapping PCI Path
///
typedef struct {
  UINT8    Device;
  UINT8    Function;
} EFI_ACPI_DMAR_PCI_PATH;

///
/// Device Scope Structure is defined in section 8.3.1
///
typedef struct {
  UINT8    Type;
  UINT8    Length;
  UINT8    Flags;
  UINT8    Reserved;
  UINT8    EnumerationId;
  UINT8    StartBusNumber;
} EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER;

/**
  DMA-remapping hardware unit definition (DRHD) structure is defined in
  section 8.3. This uniquely represents a remapping hardware unit present
  in the platform. There must be at least one instance of this structure
  for each PCI segment in the platform.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;

  /**
    - Bit[0]: INCLUDE_PCI_ALL
              - If Set, this remapping hardware unit has under its scope all
                PCI compatible devices in the specified Segment, except devices
                reported under the scope of other remapping hardware units for
                the same Segment.
              - If Clear, this remapping hardware unit has under its scope only
                devices in the specified Segment that are explicitly identified
                through the DeviceScope field.
    - Bits[7:1] Reserved.
  **/
  UINT8     Flags;

  /**
    - Bits[3:0]: Indicates the size of the remapping hardware register set for
                 this remapping unit. If the value in this field is N, the size
                 of the register set is 2^N 4 KB pages
    - Bits[7:4]: Reserved.
  **/
  UINT8     Size;
  ///
  /// The PCI Segment associated with this unit.
  ///
  UINT16    SegmentNumber;
  ///
  /// Base address of remapping hardware register-set for this unit.
  ///
  UINT64    RegisterBaseAddress;
} EFI_ACPI_DMAR_DRHD_HEADER;

/**
  Reserved Memory Region Reporting Structure (RMRR) is described in section 8.4
  Reserved memory ranges that may be DMA targets may be reported through the
  RMRR structures, along with the devices that requires access to the specified
  reserved memory region.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;
  UINT8                             Reserved[2];
  ///
  /// PCI Segment Number associated with devices identified through
  /// the Device Scope field.
  ///
  UINT16                            SegmentNumber;
  ///
  /// Base address of 4KB-aligned reserved memory region
  ///
  UINT64                            ReservedMemoryRegionBaseAddress;

  /**
    Last address of the reserved memory region. Value in this field must be
    greater than the value in Reserved Memory Region Base Address field.
    The reserved memory region size (Limit - Base + 1) must be an integer
    multiple of 4KB.
  **/
  UINT64                            ReservedMemoryRegionLimitAddress;
} EFI_ACPI_DMAR_RMRR_HEADER;

/**
  Root Port ATS Capability Reporting (ATSR) structure is defined in section 8.5.
  This structure is applicable only for platforms supporting Device-TLBs as
  reported through the Extended Capability Register. For each PCI Segment in
  the platform that supports Device-TLBs, BIOS provides an ATSR structure. The
  ATSR structures identifies PCI-Express Root-Ports supporting Address
  Translation Services (ATS) transactions. Software must enable ATS on endpoint
  devices behind a Root Port only if the Root Port is reported as supporting
  ATS transactions.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;

  /**
    - Bit[0]: ALL_PORTS:
              - If Set, indicates all PCI Express Root Ports in the specified
                PCI Segment supports ATS transactions.
              - If Clear, indicates ATS transactions are supported only on
                Root Ports identified through the Device Scope field.
    - Bits[7:1] Reserved.
  **/
  UINT8     Flags;
  UINT8     Reserved;
  ///
  /// The PCI Segment associated with this ATSR structure
  ///
  UINT16    SegmentNumber;
} EFI_ACPI_DMAR_ATSR_HEADER;

/**
  Remapping Hardware Static Affinity (RHSA) is an optional structure defined
  in section 8.6. This is intended to be used only on NUMA platforms with
  Remapping hardware units and memory spanned across multiple nodes.
  When used, there must be a RHSA structure for each Remapping hardware unit
  reported through DRHD structure.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;
  UINT8                             Reserved[4];
  ///
  /// Register Base Address of this Remap hardware unit reported in the
  /// corresponding DRHD structure.
  ///
  UINT64                            RegisterBaseAddress;
  ///
  /// Proximity Domain to which the Remap hardware unit identified by the
  /// Register Base Address field belongs.
  ///
  UINT32                            ProximityDomain;
} EFI_ACPI_DMAR_RHSA_HEADER;

/**
  An ACPI Name-space Device Declaration (ANDD) structure is defined in section
  8.7 and uniquely represents an ACPI name-space enumerated device capable of
  issuing DMA requests in the platform. ANDD structures are used in conjunction
  with Device-Scope entries of type ACPI_NAMESPACE_DEVICE.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;
  UINT8                             Reserved[3];

  /**
    Each ACPI device enumerated through an ANDD structure must have a unique
    value for this field. To report an ACPI device with ACPI Device Number
    value of X, under the scope of a DRHD unit, a Device-Scope entry of type
    ACPI_NAMESPACE_DEVICE is used with value of X in the Enumeration ID field.
    The Start Bus Number and Path fields in the Device-Scope together
    provides the 16-bit source-id allocated by platform for the ACPI device.
  **/
  UINT8    AcpiDeviceNumber;
} EFI_ACPI_DMAR_ANDD_HEADER;

/**
  An SoC Integrated Address Translation Cache (SATC) reporting structure is
  defined in section 8.8.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;

  /**
    - Bit[0]: ATC_REQUIRED:
              - If Set, indicates that every SoC integrated device enumerated
                in this table has a functional requirement to enable its ATC
                (via the ATS capability) for device operation.
              - If Clear, any device enumerated in this table can operate when
                its respective ATC is not enabled (albeit with reduced
                performance or functionality).
    - Bits[7:1] Reserved.
  **/
  UINT8     Flags;
  UINT8     Reserved;
  ///
  /// The PCI Segment associated with this SATC structure. All SoC integrated
  /// devices within a PCI segment with same value for Flags field must be
  /// enumerated in the same SATC structure.
  ///
  UINT16    SegmentNumber;
} EFI_ACPI_DMAR_SATC_HEADER;

/**
  SoC Integrated Device Property (SIDP) Reporting Structure is defined in
  section 8.9.
**/
typedef struct {
  EFI_ACPI_DMAR_STRUCTURE_HEADER    Header;

  UINT16                            Reserved;
  ///
  /// The PCI Segment associated with this SIDP structure.
  ///
  UINT16                            SegmentNumber;
} EFI_ACPI_DMAR_SIDP_HEADER;

/**
  DMA Remapping Reporting Structure Header as defined in section 8.1
  This header will be followed by list of Remapping Structures listed below
    - DMA Remapping Hardware Unit Definition (DRHD)
    - Reserved Memory Region Reporting (RMRR)
    - Root Port ATS Capability Reporting (ATSR)
    - Remapping Hardware Static Affinity (RHSA)
    - ACPI Name-space Device Declaration (ANDD)
    - SoC Integrated Address Translation Cache reporting (SATC)
    - SoC Integrated Device Property reporting (SIDP)
  These structure types must by reported in numerical order.
  i.e., All remapping structures of type 0 (DRHD) enumerated before remapping
  structures of type 1 (RMRR), and so forth.
**/
typedef struct {
  EFI_ACPI_DESCRIPTION_HEADER    Header;

  /**
    This field indicates the maximum DMA physical addressability supported by
    this platform. The system address map reported by the BIOS indicates what
    portions of this addresses are populated. The Host Address Width (HAW) of
    the platform is computed as (N+1), where N is the value reported in this
    field.
    For example, for a platform supporting 40 bits of physical addressability,
    the value of 100111b is reported in this field.
  **/
  UINT8    HostAddressWidth;

  /**
    - Bit[0]:   INTR_REMAP - If Clear, the platform does not support interrupt
                remapping. If Set, the platform supports interrupt remapping.
    - Bit[1]:   X2APIC_OPT_OUT - For firmware compatibility reasons, platform
                firmware may Set this field to request system software to opt
                out of enabling Extended xAPIC (X2APIC) mode. This field is
                valid only when the INTR_REMAP field (bit 0) is Set.
    - Bit[2]:   DMA_CTRL_PLATFORM_OPT_IN_FLAG - Platform firmware is
                recommended to Set this field to report any platform initiated
                DMA is restricted to only reserved memory regions (reported in
                RMRR structures) when transferring control to system software
                such as on ExitBootServices().
    - Bits[7:3] Reserved.
  **/
  UINT8    Flags;
  UINT8    Reserved[10];
} EFI_ACPI_DMAR_HEADER;

#pragma pack()
