/**
 * @file IioCfgUpdateDxeLightningRidgeEXECB2.h
 *
 * @brief IioCfgUpdateDxeLightningRidgeEXECB2 - UEFI DXE driver for registering
 *        IIO configuration data for LightningRidgeEXECB2 via UBA framework.
 *
 * MODULE TYPE: DXE Driver, UEFI PHASE: DXE
 *
 * PURPOSE:
 *   Part of Lenovo's UBA framework. At entry:
 *     1. Initializes UEFI globals (gImageHandle, gST, gBS, gRT)
 *     2. Locates HOB list via EFI_HOB_LIST_GUID in config table
 *     3. Locates UBA board-type protocol via gBS->LocateProtocol()
 *     4. Registers 4 IIO config protocol instances (one per CPU socket)
 *        via board-type protocol's registration function (offset 0x10)
 *     5. Provides debug via UBA Debug Protocol (GUID 36232936-...)
 *
 * DIFFERENCES FROM EXECB1: IIO config table has 19 entries vs 21
 *   (entries 0x01 and 0x48 removed)
 *
 * @note Sibling modules: EXECB1, EXECB3, EXECB4, EXRP, NeonCityFPGA.
 */

#ifndef _IIO_CFG_UPDATE_DXE_LIGHTNINGRIDGE_EXECB2_H_
#define _IIO_CFG_UPDATE_DXE_LIGHTNINGRIDGE_EXECB2_H_

#include "../uefi_headers/Uefi.h"

// ============================================================================
// GUID Definitions
// ============================================================================

#define EFI_HOB_LIST_GUID \
  { 0x7739F24C, 0x93D7, 0x11D4, { 0x9A, 0x3A, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D } }

#define UBA_BOARD_TYPE_PROTOCOL_GUID \
  { 0xE03E0D46, 0x5263, 0x4845, { 0xB0, 0xA4, 0x58, 0xD5, 0x7B, 0x31, 0x77, 0xE2 } }

#define UBA_IIO_CFG_UPDATE_PROTOCOL_GUID_0 \
  { 0x6FE6C559, 0x4F35, 0x4111, { 0x98, 0xE1, 0x33, 0x2A, 0x25, 0x15, 0x12, 0xF3 } }

#define UBA_IIO_CFG_UPDATE_PROTOCOL_GUID_1 \
  { 0x0F722F2A, 0x650F, 0x448A, { 0xAB, 0xB7, 0x04, 0xEE, 0xCD, 0x75, 0xBB, 0x30 } }

#define UBA_IIO_CFG_UPDATE_PROTOCOL_GUID_2 \
  { 0xEBD11A00, 0x8C5C, 0x4F71, { 0xBB, 0x9E, 0x53, 0x94, 0x03, 0x2B, 0x01, 0xF4 } }

#define UBA_IIO_CFG_UPDATE_PROTOCOL_GUID_3 \
  { 0x123BD082, 0x3201, 0x465C, { 0xB1, 0x39, 0x0C, 0xB8, 0xC7, 0x72, 0x08, 0xF8 } }

#define UBA_DEBUG_PROTOCOL_GUID \
  { 0x36232936, 0x0E76, 0x31C8, { 0xA1, 0x3A, 0x3A, 0xF2, 0xFC, 0x1C, 0x39, 0x32 } }

// ============================================================================
// Structure Definitions
// ============================================================================

///
/// UBA_IIO_CFG_UPDATE_PROTOCOL (48 bytes, signature "PIIO" = 0x4F494950)
/// Binary layout at 0xC30:
///   +0x00: UINT32 Signature         = "PIIO"
///   +0x04: UINT32 Version           = 1
///   +0x08: UINT64 TotalSize         = 0x0D60
///   +0x10: UINT64 ConfigTableOffset = 0x3C
///   +0x18: UINT64 CallbackFunction  = 0x50C (ReturnNotFound)
///   +0x20: UINT64 ResourceTblOffset = 0x0C60
///   +0x28: UINT64 ResourceTblSize   = 0xFC
///
typedef struct {
  UINT32    Signature;
  UINT32    Version;
  UINT64    TotalSize;
  UINT64    ConfigTableOffset;
  UINT64    CallbackFunction;
  UINT64    ResourceTableOffset;
  UINT64    ResourceTableSize;
} UBA_IIO_CFG_UPDATE_PROTOCOL;

///
/// UBA_IIO_CFG_UPDATE_TABLE_ENTRY (12 bytes each)
///
typedef struct {
  UINT8     Type;
  UINT8     Reserved0;
  UINT16    Reserved1;
  UINT32    Reserved2;
  UINT8     Value;
  UINT8     Port;
  UINT16    AddressType;
} UBA_IIO_CFG_UPDATE_TABLE_ENTRY;

#define UBA_IIO_CFG_UPDATE_TABLE_ENTRY_SIZE  12

// ============================================================================
// Constants
// ============================================================================

#define UBA_IIO_CFG_UPDATE_SIGNATURE         SIGNATURE_32('P', 'I', 'I', 'O')
#define IIO_CFG_UPDATE_PROTOCOL_COUNT        4
#define CMOS_DEBUG_LEVEL_REGISTER            0x4B
#define RTC_INDEX_PORT                       0x70
#define RTC_DATA_PORT                        0x71
#define BOARD_CONFIG_MMIO_ADDR               0xFDAF0490ULL
#define DEBUG_INFO                           0x80000000
#define DEBUG_DIAG_ERROR                     0x80000004
#define DEBUG_VERBOSE_DIAG_ERROR             0x80000006
#define BOARD_TYPE_CMOS_NEEDS_MMIO           0
#define BOARD_TYPE_STANDARD_MAX              3
#define BOARD_TYPE_MMIO_MASK                 2
#define BOARD_TYPE_MMIO_FLAG_BIT             BIT0
#define CMOS_NMI_BIT_MASK                    0x80

// ============================================================================
// Global Variable Declarations
// ============================================================================

extern EFI_SYSTEM_TABLE     *gST;
extern EFI_BOOT_SERVICES    *gBS;
extern EFI_RUNTIME_SERVICES *gRT;
extern EFI_HANDLE           gImageHandle;
extern VOID                 *gHobList;
extern VOID                 *gDebugProtocol;
extern UINT8                gBoardType;

// ============================================================================
// Function Declarations
// ============================================================================

EFI_STATUS
EFIAPI
_ModuleEntryPoint (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  );

EFI_STATUS
EFIAPI
RegisterIioConfig (
  VOID
  );

VOID *
EFIAPI
GetDebugProtocol (
  VOID
  );

UINTN
EFIAPI
DebugPrint (
  IN UINTN       ErrorLevel,
  IN CONST CHAR8 *Format,
  ...
  );

UINTN
EFIAPI
DebugAssert (
  IN CONST CHAR8  *FileName,
  IN UINTN        LineNumber,
  IN CONST CHAR8  *Description
  );

VOID *
EFIAPI
GetHobList (
  IN EFI_HANDLE  ImageHandle
  );

BOOLEAN
EFIAPI
IsHobListGuid (
  IN EFI_HANDLE  ImageHandle,
  IN EFI_GUID    *GuidPtr
  );

EFI_STATUS
EFIAPI
ReturnNotFound (
  VOID
  );

UINT64
EFIAPI
ReadUnaligned64 (
  IN CONST VOID  *Buffer
  );

VOID *
EFIAPI
AllocatePool (
  IN UINTN Size
  );

VOID
EFIAPI
FreePool (
  IN VOID *Buffer
  );

// ============================================================================
// UBA Board-Type Protocol Interface
// ============================================================================

typedef struct {
  UINT64   Reserved0;
  UINT64   Reserved1;

  /// Registration function at offset 0x10.
  EFI_STATUS
  (EFIAPI *RegisterConfig) (
    IN VOID                    *This,
    IN EFI_GUID                *ProtocolGuid,
    IN UBA_IIO_CFG_UPDATE_PROTOCOL *ConfigData,
    IN UINT64                  ConfigDataSize
    );
} UBA_BOARD_TYPE_PROTOCOL;

#endif /* _IIO_CFG_UPDATE_DXE_LIGHTNINGRIDGE_EXECB2_H_ */