/** @file
  I/O and MMIO Library Services that do I/O and also enable the I/O operatation
  to be replayed during an S3 resume.
  
  Copyright (c) 2006 -2012, Intel Corporation. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions
  of the BSD License which accompanies this distribution.  The
  full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Base.h>

#include <Library/S3IoLib.h>
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <Library/S3BootScriptLib.h>


/**
  Saves an I/O port value to the boot script.

  This internal worker function saves an I/O port value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Width         The width of I/O port.
  @param  Port          The I/O port to write.
  @param  Buffer        The buffer containing value.

**/
VOID
InternalSaveIoWriteValueToBootScript (
  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,
  IN UINTN                  Port,
  IN VOID                   *Buffer
  )
{
  RETURN_STATUS                Status;
  
  Status = S3BootScriptSaveIoWrite (
             Width,
             Port,
             1,
             Buffer
             );
  ASSERT (Status == RETURN_SUCCESS);
}
  
/**
  Saves an 8-bit I/O port value to the boot script.

  This internal worker function saves an 8-bit I/O port value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT8
InternalSaveIoWrite8ValueToBootScript (
  IN UINTN              Port,
  IN UINT8              Value
  )
{
  InternalSaveIoWriteValueToBootScript (S3BootScriptWidthUint8, Port, &Value);

  return Value;
}

/**
  Reads an 8-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
  This function must guarantee that all I/O read and write operations are
  serialized.

  If 8-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to read.

  @return The value read.

**/
UINT8
EFIAPI
S3IoRead8 (
  IN UINTN              Port
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoRead8 (Port));
}

/**
  Writes an 8-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Writes the 8-bit I/O port specified by Port with the value specified by Value
  and returns Value. This function must guarantee that all I/O read and write
  operations are serialized.

  If 8-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value to write to the I/O port.

  @return The value written the I/O port.

**/
UINT8
EFIAPI
S3IoWrite8 (
  IN UINTN              Port,
  IN UINT8              Value
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoWrite8 (Port, Value));
}

/**
  Reads an 8-bit I/O port, performs a bitwise OR, and writes the
  result back to the 8-bit I/O port and saves the value in the S3 script to be
  replayed on S3 resume.

  Reads the 8-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 8-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized.

  If 8-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoOr8 (
  IN UINTN              Port,
  IN UINT8              OrData
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoOr8 (Port, OrData));
}

/**
  Reads an 8-bit I/O port, performs a bitwise AND, and writes the result back
  to the 8-bit I/O port  and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 8-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 8-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized.

  If 8-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoAnd8 (
  IN UINTN              Port,
  IN UINT8              AndData
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoAnd8 (Port, AndData));
}

/**
  Reads an 8-bit I/O port, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 8-bit I/O port and saves 
  the value in the S3 script to be replayed on S3 resume.

  Reads the 8-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, performs a bitwise OR
  between the result of the AND operation and the value specified by OrData,
  and writes the result to the 8-bit I/O port specified by Port. The value
  written to the I/O port is returned. This function must guarantee that all
  I/O read and write operations are serialized.

  If 8-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoAndThenOr8 (
  IN UINTN              Port,
  IN UINT8              AndData,
  IN UINT8              OrData
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoAndThenOr8 (Port, AndData, OrData));
}

/**
  Reads a bit field of an I/O register and saves the value in the S3 script to
  be replayed on S3 resume.

  Reads the bit field in an 8-bit I/O register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 8-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Port          The I/O port to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.

  @return The value read.

**/
UINT8
EFIAPI
S3IoBitFieldRead8 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoBitFieldRead8 (Port, StartBit, EndBit));
}

/**
  Writes a bit field to an I/O register and saves the value in the S3 script to
  be replayed on S3 resume.

  Writes Value to the bit field of the I/O register. The bit field is specified
  by the StartBit and the EndBit. All other bits in the destination I/O
  register are preserved. The value written to the I/O port is returned. Extra
  left bits in Value are stripped.

  If 8-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  Value         New value of the bit field.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoBitFieldWrite8 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              Value
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoBitFieldWrite8 (Port, StartBit, EndBit, Value));
}

/**
  Reads a bit field in an 8-bit port, performs a bitwise OR, and writes the
  result back to the bit field in the 8-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 8-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 8-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized. Extra left bits in OrData are stripped.

  If 8-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoBitFieldOr8 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              OrData
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoBitFieldOr8 (Port, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in an 8-bit port, performs a bitwise AND, and writes the
  result back to the bit field in the 8-bit port  and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 8-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 8-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized. Extra left bits in AndData are stripped.

  If 8-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoBitFieldAnd8 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              AndData
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoBitFieldAnd8 (Port, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in an 8-bit port, performs a bitwise AND followed by a
  bitwise OR, and writes the result back to the bit field in the
  8-bit port and saves the value in the S3 script to be replayed on S3 resume.

  Reads the 8-bit I/O port specified by Port, performs a bitwise AND followed
  by a bitwise OR between the read result and the value specified by
  AndData, and writes the result to the 8-bit I/O port specified by Port. The
  value written to the I/O port is returned. This function must guarantee that
  all I/O read and write operations are serialized. Extra left bits in both
  AndData and OrData are stripped.

  If 8-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT8
EFIAPI
S3IoBitFieldAndThenOr8 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              AndData,
  IN UINT8              OrData
  )
{
  return InternalSaveIoWrite8ValueToBootScript (Port, IoBitFieldAndThenOr8 (Port, StartBit, EndBit, AndData, OrData));
}

/**
  Saves a 16-bit I/O port value to the boot script.

  This internal worker function saves a 16-bit I/O port value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT16
InternalSaveIoWrite16ValueToBootScript (
  IN UINTN              Port,
  IN UINT16             Value
  )
{
  InternalSaveIoWriteValueToBootScript (S3BootScriptWidthUint16, Port, &Value);
  
  return Value;
}

/**
  Reads a 16-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
  This function must guarantee that all I/O read and write operations are
  serialized.

  If 16-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to read.

  @return The value read.

**/
UINT16
EFIAPI
S3IoRead16 (
  IN UINTN              Port
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoRead16 (Port));
}

/**
  Writes a 16-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Writes the 16-bit I/O port specified by Port with the value specified by Value
  and returns Value. This function must guarantee that all I/O read and write
  operations are serialized.

  If 16-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value to write to the I/O port.

  @return The value written the I/O port.

**/
UINT16
EFIAPI
S3IoWrite16 (
  IN UINTN              Port,
  IN UINT16             Value
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoWrite16 (Port, Value));
}

/**
  Reads a 16-bit I/O port, performs a bitwise OR, and writes the
  result back to the 16-bit I/O port and saves the value in the S3 script to 
  be replayed on S3 resume.

  Reads the 16-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 16-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized.

  If 16-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoOr16 (
  IN UINTN              Port,
  IN UINT16             OrData
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoOr16 (Port, OrData));
}

/**
  Reads a 16-bit I/O port, performs a bitwise AND, and writes the result back
  to the 16-bit I/O port  and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 16-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 16-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized.

  If 16-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoAnd16 (
  IN UINTN              Port,
  IN UINT16             AndData
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoAnd16 (Port, AndData));
}

/**
  Reads a 16-bit I/O port, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 16-bit I/O port and saves
  the value in the S3 script to be replayed on S3 resume.

  Reads the 16-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, performs a bitwise OR
  between the result of the AND operation and the value specified by OrData,
  and writes the result to the 16-bit I/O port specified by Port. The value
  written to the I/O port is returned. This function must guarantee that all
  I/O read and write operations are serialized.

  If 16-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoAndThenOr16 (
  IN UINTN              Port,
  IN UINT16             AndData,
  IN UINT16             OrData
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoAndThenOr16 (Port, AndData, OrData));
}

/**
  Reads a bit field of an I/O register saves the value in the S3 script to be
  replayed on S3 resume.

  Reads the bit field in a 16-bit I/O register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 16-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Port          The I/O port to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.

  @return The value read.

**/
UINT16
EFIAPI
S3IoBitFieldRead16 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoBitFieldRead16 (Port, StartBit, EndBit));
}

/**
  Writes a bit field to an I/O register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Writes Value to the bit field of the I/O register. The bit field is specified
  by the StartBit and the EndBit. All other bits in the destination I/O
  register are preserved. The value written to the I/O port is returned. Extra
  left bits in Value are stripped.

  If 16-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  Value         New value of the bit field.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoBitFieldWrite16 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             Value
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoBitFieldWrite16 (Port, StartBit, EndBit, Value));
}

/**
  Reads a bit field in a 16-bit port, performs a bitwise OR, and writes the
  result back to the bit field in the 16-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 16-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 16-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized. Extra left bits in OrData are stripped.

  If 16-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoBitFieldOr16 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             OrData
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoBitFieldOr16 (Port, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in a 16-bit port, performs a bitwise AND, and writes the
  result back to the bit field in the 16-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 16-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 16-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized. Extra left bits in AndData are stripped.

  If 16-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoBitFieldAnd16 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             AndData
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoBitFieldAnd16 (Port, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in a 16-bit port, performs a bitwise AND followed by a
  bitwise OR, and writes the result back to the bit field in the
  16-bit port  and saves the value in the S3 script to be replayed on S3 
  resume.

  Reads the 16-bit I/O port specified by Port, performs a bitwise AND followed
  by a bitwise OR between the read result and the value specified by
  AndData, and writes the result to the 16-bit I/O port specified by Port. The
  value written to the I/O port is returned. This function must guarantee that
  all I/O read and write operations are serialized. Extra left bits in both
  AndData and OrData are stripped.

  If 16-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT16
EFIAPI
S3IoBitFieldAndThenOr16 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             AndData,
  IN UINT16             OrData
  )
{
  return InternalSaveIoWrite16ValueToBootScript (Port, IoBitFieldAndThenOr16 (Port, StartBit, EndBit, AndData, OrData));
}

/**
  Saves a 32-bit I/O port value to the boot script.

  This internal worker function saves a 32-bit I/O port value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT32
InternalSaveIoWrite32ValueToBootScript (
  IN UINTN              Port,
  IN UINT32             Value
  )
{
  InternalSaveIoWriteValueToBootScript (S3BootScriptWidthUint32, Port, &Value);
  
  return Value;
}

/**
  Reads a 32-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
  This function must guarantee that all I/O read and write operations are
  serialized.

  If 32-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to read.

  @return The value read.

**/
UINT32
EFIAPI
S3IoRead32 (
  IN UINTN              Port
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoRead32 (Port));
}

/**
  Writes a 32-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Writes the 32-bit I/O port specified by Port with the value specified by Value
  and returns Value. This function must guarantee that all I/O read and write
  operations are serialized.

  If 32-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value to write to the I/O port.

  @return The value written the I/O port.

**/
UINT32
EFIAPI
S3IoWrite32 (
  IN UINTN              Port,
  IN UINT32             Value
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoWrite32 (Port, Value));
}

/**
  Reads a 32-bit I/O port, performs a bitwise OR, and writes the
  result back to the 32-bit I/O port and saves the value in the S3 script to 
  be replayed on S3 resume.

  Reads the 32-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 32-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized.

  If 32-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoOr32 (
  IN UINTN              Port,
  IN UINT32             OrData
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoOr32 (Port, OrData));
}

/**
  Reads a 32-bit I/O port, performs a bitwise AND, and writes the result back
  to the 32-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 32-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 32-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized.

  If 32-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoAnd32 (
  IN UINTN              Port,
  IN UINT32             AndData
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoAnd32 (Port, AndData));
}

/**
  Reads a 32-bit I/O port, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 32-bit I/O port and saves 
  the value in the S3 script to be replayed on S3 resume.

  Reads the 32-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, performs a bitwise OR
  between the result of the AND operation and the value specified by OrData,
  and writes the result to the 32-bit I/O port specified by Port. The value
  written to the I/O port is returned. This function must guarantee that all
  I/O read and write operations are serialized.

  If 32-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoAndThenOr32 (
  IN UINTN              Port,
  IN UINT32             AndData,
  IN UINT32             OrData
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoAndThenOr32 (Port, AndData, OrData));
}

/**
  Reads a bit field of an I/O register and saves the value in the S3 script to
  be replayed on S3 resume.

  Reads the bit field in a 32-bit I/O register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 32-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Port          The I/O port to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.

  @return The value read.

**/
UINT32
EFIAPI
S3IoBitFieldRead32 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoBitFieldRead32 (Port, StartBit, EndBit));
}

/**
  Writes a bit field to an I/O register and saves the value in the S3 script to
  be replayed on S3 resume.

  Writes Value to the bit field of the I/O register. The bit field is specified
  by the StartBit and the EndBit. All other bits in the destination I/O
  register are preserved. The value written to the I/O port is returned. Extra
  left bits in Value are stripped.

  If 32-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  Value         New value of the bit field.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoBitFieldWrite32 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             Value
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoBitFieldWrite32 (Port, StartBit, EndBit, Value));
}

/**
  Reads a bit field in a 32-bit port, performs a bitwise OR, and writes the
  result back to the bit field in the 32-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 32-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 32-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized. Extra left bits in OrData are stripped.

  If 32-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoBitFieldOr32 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             OrData
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoBitFieldOr32 (Port, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in a 32-bit port, performs a bitwise AND, and writes the
  result back to the bit field in the 32-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 32-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 32-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized. Extra left bits in AndData are stripped.

  If 32-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoBitFieldAnd32 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             AndData
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoBitFieldAnd32 (Port, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in a 32-bit port, performs a bitwise AND followed by a
  bitwise OR, and writes the result back to the bit field in the
  32-bit port and saves the value in the S3 script to be replayed on S3 
  resume.

  Reads the 32-bit I/O port specified by Port, performs a bitwise AND followed
  by a bitwise OR between the read result and the value specified by
  AndData, and writes the result to the 32-bit I/O port specified by Port. The
  value written to the I/O port is returned. This function must guarantee that
  all I/O read and write operations are serialized. Extra left bits in both
  AndData and OrData are stripped.

  If 32-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT32
EFIAPI
S3IoBitFieldAndThenOr32 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             AndData,
  IN UINT32             OrData
  )
{
  return InternalSaveIoWrite32ValueToBootScript (Port, IoBitFieldAndThenOr32 (Port, StartBit, EndBit, AndData, OrData));
}

/**
  Saves a 64-bit I/O port value to the boot script.

  This internal worker function saves a 64-bit I/O port value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT64
InternalSaveIoWrite64ValueToBootScript (
  IN UINTN              Port,
  IN UINT64             Value
  )
{
  InternalSaveIoWriteValueToBootScript (S3BootScriptWidthUint64, Port, &Value);
  
  return Value;
}

/**
  Reads a 64-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
  This function must guarantee that all I/O read and write operations are
  serialized.

  If 64-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to read.

  @return The value read.

**/
UINT64
EFIAPI
S3IoRead64 (
  IN UINTN              Port
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoRead64 (Port));
}

/**
  Writes a 64-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Writes the 64-bit I/O port specified by Port with the value specified by Value
  and returns Value. This function must guarantee that all I/O read and write
  operations are serialized.

  If 64-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  Value         The value to write to the I/O port.

  @return The value written the I/O port.

**/
UINT64
EFIAPI
S3IoWrite64 (
  IN UINTN              Port,
  IN UINT64             Value
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoWrite64 (Port, Value));
}

/**
  Reads a 64-bit I/O port, performs a bitwise OR, and writes the
  result back to the 64-bit I/O port and saves the value in the S3 script to 
  be replayed on S3 resume.

  Reads the 64-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 64-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized.

  If 64-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoOr64 (
  IN UINTN              Port,
  IN UINT64             OrData
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoOr64 (Port, OrData));
}

/**
  Reads a 64-bit I/O port, performs a bitwise AND, and writes the result back
  to the 64-bit I/O port and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 64-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 64-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized.

  If 64-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoAnd64 (
  IN UINTN              Port,
  IN UINT64             AndData
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoAnd64 (Port, AndData));
}

/**
  Reads a 64-bit I/O port, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 64-bit I/O port and saves
  the value in the S3 script to be replayed on S3 resume.

  Reads the 64-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, performs a bitwise OR
  between the result of the AND operation and the value specified by OrData,
  and writes the result to the 64-bit I/O port specified by Port. The value
  written to the I/O port is returned. This function must guarantee that all
  I/O read and write operations are serialized.

  If 64-bit I/O port operations are not supported, then ASSERT().

  @param  Port          The I/O port to write.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoAndThenOr64 (
  IN UINTN              Port,
  IN UINT64             AndData,
  IN UINT64             OrData
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoAndThenOr64 (Port, AndData, OrData));
}

/**
  Reads a bit field of an I/O register and saves the value in the S3 script to
  be replayed on S3 resume.

  Reads the bit field in a 64-bit I/O register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 64-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Port          The I/O port to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.

  @return The value read.

**/
UINT64
EFIAPI
S3IoBitFieldRead64 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoBitFieldRead64 (Port, StartBit, EndBit));
}

/**
  Writes a bit field to an I/O register and saves the value in the S3 script to
  be replayed on S3 resume.

  Writes Value to the bit field of the I/O register. The bit field is specified
  by the StartBit and the EndBit. All other bits in the destination I/O
  register are preserved. The value written to the I/O port is returned. Extra
  left bits in Value are stripped.

  If 64-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  Value         New value of the bit field.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoBitFieldWrite64 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             Value
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoBitFieldWrite64 (Port, StartBit, EndBit, Value));
}

/**
  Reads a bit field in a 64-bit port, performs a bitwise OR, and writes the
  result back to the bit field in the 64-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 64-bit I/O port specified by Port, performs a bitwise OR
  between the read result and the value specified by OrData, and writes the
  result to the 64-bit I/O port specified by Port. The value written to the I/O
  port is returned. This function must guarantee that all I/O read and write
  operations are serialized. Extra left bits in OrData are stripped.

  If 64-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  OrData        The value to OR with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoBitFieldOr64 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             OrData
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoBitFieldOr64 (Port, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in a 64-bit port, performs a bitwise AND, and writes the
  result back to the bit field in the 64-bit port and saves the value in the 
  S3 script to be replayed on S3 resume.

  Reads the 64-bit I/O port specified by Port, performs a bitwise AND between
  the read result and the value specified by AndData, and writes the result to
  the 64-bit I/O port specified by Port. The value written to the I/O port is
  returned. This function must guarantee that all I/O read and write operations
  are serialized. Extra left bits in AndData are stripped.

  If 64-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  AndData       The value to AND with the read value from the I/O port.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoBitFieldAnd64 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             AndData
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoBitFieldAnd64 (Port, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in a 64-bit port, performs a bitwise AND followed by a
  bitwise OR, and writes the result back to the bit field in the
  64-bit port and saves the value in the S3 script to be replayed on S3 
  resume.

  Reads the 64-bit I/O port specified by Port, performs a bitwise AND followed
  by a bitwise OR between the read result and the value specified by
  AndData, and writes the result to the 64-bit I/O port specified by Port. The
  value written to the I/O port is returned. This function must guarantee that
  all I/O read and write operations are serialized. Extra left bits in both
  AndData and OrData are stripped.

  If 64-bit I/O port operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Port          The I/O port to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  AndData       The value to AND with the read value from the I/O port.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the I/O port.

**/
UINT64
EFIAPI
S3IoBitFieldAndThenOr64 (
  IN UINTN              Port,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             AndData,
  IN UINT64             OrData
  )
{
  return InternalSaveIoWrite64ValueToBootScript (Port, IoBitFieldAndThenOr64 (Port, StartBit, EndBit, AndData, OrData));
}

/**
  Saves an MMIO register value to the boot script.

  This internal worker function saves an MMIO register value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Width         The width of MMIO register.
  @param  Address       The MMIO register to write.
  @param  Buffer        The buffer containing value.

**/
VOID
InternalSaveMmioWriteValueToBootScript (
  IN S3_BOOT_SCRIPT_LIB_WIDTH  Width,
  IN UINTN                  Address,
  IN VOID                   *Buffer
  )
{
  RETURN_STATUS            Status;

  Status = S3BootScriptSaveMemWrite (
             Width,
             Address,
             1,
             Buffer
             );
  ASSERT (Status == RETURN_SUCCESS);
}

/**
  Saves an 8-bit MMIO register value to the boot script.

  This internal worker function saves an 8-bit MMIO register value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT8
InternalSaveMmioWrite8ValueToBootScript (
  IN UINTN              Address,
  IN UINT8              Value
  )
{
  InternalSaveMmioWriteValueToBootScript (S3BootScriptWidthUint8, Address, &Value);

  return Value;
}

/**
  Reads an 8-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
  returned. This function must guarantee that all MMIO read and write
  operations are serialized.

  If 8-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to read.

  @return The value read.

**/
UINT8
EFIAPI
S3MmioRead8 (
  IN UINTN              Address
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioRead8 (Address));
}

/**
  Writes an 8-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Writes the 8-bit MMIO register specified by Address with the value specified
  by Value and returns Value. This function must guarantee that all MMIO read
  and write operations are serialized.

  If 8-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value to write to the MMIO register.

  @return The value written the MMIO register.

**/
UINT8
EFIAPI
S3MmioWrite8 (
  IN UINTN              Address,
  IN UINT8              Value
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioWrite8 (Address, Value));
}

/**
  Reads an 8-bit MMIO register, performs a bitwise OR, and writes the
  result back to the 8-bit MMIO register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Reads the 8-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 8-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized.

  If 8-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioOr8 (
  IN UINTN              Address,
  IN UINT8              OrData
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioOr8 (Address, OrData));
}

/**
  Reads an 8-bit MMIO register, performs a bitwise AND, and writes the result
  back to the 8-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 8-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 8-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized.

  If 8-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioAnd8 (
  IN UINTN              Address,
  IN UINT8              AndData
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioAnd8 (Address, AndData));
}

/**
  Reads an 8-bit MMIO register, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 8-bit MMIO register and saves 
  the value in the S3 script to be replayed on S3 resume.

  Reads the 8-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, performs a
  bitwise OR between the result of the AND operation and the value specified by
  OrData, and writes the result to the 8-bit MMIO register specified by
  Address. The value written to the MMIO register is returned. This function
  must guarantee that all MMIO read and write operations are serialized.

  If 8-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioAndThenOr8 (
  IN UINTN              Address,
  IN UINT8              AndData,
  IN UINT8              OrData
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioAndThenOr8 (Address, AndData, OrData));
}

/**
  Reads a bit field of a MMIO register and saves the value in the S3 script to
  be replayed on S3 resume.

  Reads the bit field in an 8-bit MMIO register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 8-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Address       MMIO register to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.

  @return The value read.

**/
UINT8
EFIAPI
S3MmioBitFieldRead8 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioBitFieldRead8 (Address, StartBit, EndBit));
}

/**
  Writes a bit field to an MMIO register and saves the value in the S3 script to
  be replayed on S3 resume.

  Writes Value to the bit field of the MMIO register. The bit field is
  specified by the StartBit and the EndBit. All other bits in the destination
  MMIO register are preserved. The new value of the 8-bit register is returned.

  If 8-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  Value         New value of the bit field.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioBitFieldWrite8 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              Value
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioBitFieldWrite8 (Address, StartBit, EndBit, Value));
}

/**
  Reads a bit field in an 8-bit MMIO register, performs a bitwise OR, and
  writes the result back to the bit field in the 8-bit MMIO register and saves
  the value in the S3 script to be replayed on S3 resume.

  Reads the 8-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 8-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized. Extra left bits in OrData
  are stripped.

  If 8-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioBitFieldOr8 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              OrData
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioBitFieldOr8 (Address, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in an 8-bit MMIO register, performs a bitwise AND, and
  writes the result back to the bit field in the 8-bit MMIO register and saves
  the value in the S3 script to be replayed on S3 resume.

  Reads the 8-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 8-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized. Extra left bits in AndData are
  stripped.

  If 8-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioBitFieldAnd8 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              AndData
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioBitFieldAnd8 (Address, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in an 8-bit MMIO register, performs a bitwise AND followed
  by a bitwise OR, and writes the result back to the bit field in the
  8-bit MMIO register  and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 8-bit MMIO register specified by Address, performs a bitwise AND
  followed by a bitwise OR between the read result and the value
  specified by AndData, and writes the result to the 8-bit MMIO register
  specified by Address. The value written to the MMIO register is returned.
  This function must guarantee that all MMIO read and write operations are
  serialized. Extra left bits in both AndData and OrData are stripped.

  If 8-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 7, then ASSERT().
  If EndBit is greater than 7, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..7.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..7.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT8
EFIAPI
S3MmioBitFieldAndThenOr8 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT8              AndData,
  IN UINT8              OrData
  )
{
  return InternalSaveMmioWrite8ValueToBootScript (Address, MmioBitFieldAndThenOr8 (Address, StartBit, EndBit, AndData, OrData));
}

/**
  Saves a 16-bit MMIO register value to the boot script.

  This internal worker function saves a 16-bit MMIO register value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT16
InternalSaveMmioWrite16ValueToBootScript (
  IN UINTN              Address,
  IN UINT16             Value
  )
{
  InternalSaveMmioWriteValueToBootScript (S3BootScriptWidthUint16, Address, &Value);
  
  return Value;
}

/**
  Reads a 16-bit MMIO register and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
  returned. This function must guarantee that all MMIO read and write
  operations are serialized.

  If 16-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to read.

  @return The value read.

**/
UINT16
EFIAPI
S3MmioRead16 (
  IN UINTN              Address
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioRead16 (Address));
}

/**
  Writes a 16-bit MMIO register and saves the value in the S3 script to be replayed
  on S3 resume.

  Writes the 16-bit MMIO register specified by Address with the value specified
  by Value and returns Value. This function must guarantee that all MMIO read
  and write operations are serialized and saves the value in the S3 script to be
  replayed on S3 resume.

  If 16-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value to write to the MMIO register.

  @return The value written the MMIO register.

**/
UINT16
EFIAPI
S3MmioWrite16 (
  IN UINTN              Address,
  IN UINT16             Value
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioWrite16 (Address, Value));
}

/**
  Reads a 16-bit MMIO register, performs a bitwise OR, and writes the
  result back to the 16-bit MMIO register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Reads the 16-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 16-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized.

  If 16-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioOr16 (
  IN UINTN              Address,
  IN UINT16             OrData
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioOr16 (Address, OrData));
}

/**
  Reads a 16-bit MMIO register, performs a bitwise AND, and writes the result
  back to the 16-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 16-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 16-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized.

  If 16-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioAnd16 (
  IN UINTN              Address,
  IN UINT16             AndData
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioAnd16 (Address, AndData));
}

/**
  Reads a 16-bit MMIO register, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 16-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 16-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, performs a
  bitwise OR between the result of the AND operation and the value specified by
  OrData, and writes the result to the 16-bit MMIO register specified by
  Address. The value written to the MMIO register is returned. This function
  must guarantee that all MMIO read and write operations are serialized.

  If 16-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioAndThenOr16 (
  IN UINTN              Address,
  IN UINT16             AndData,
  IN UINT16             OrData
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioAndThenOr16 (Address, AndData, OrData));
}

/**
  Reads a bit field of a MMIO register and saves the value in the S3 script to
  be replayed on S3 resume.

  Reads the bit field in a 16-bit MMIO register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 16-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Address       MMIO register to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.

  @return The value read.

**/
UINT16
EFIAPI
S3MmioBitFieldRead16 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioBitFieldRead16 (Address, StartBit, EndBit));
}

/**
  Writes a bit field to a MMIO register and saves the value in the S3 script to
  be replayed on S3 resume.

  Writes Value to the bit field of the MMIO register. The bit field is
  specified by the StartBit and the EndBit. All other bits in the destination
  MMIO register are preserved. The new value of the 16-bit register is returned.

  If 16-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  Value         New value of the bit field.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioBitFieldWrite16 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             Value
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioBitFieldWrite16 (Address, StartBit, EndBit, Value));
}

/**
  Reads a bit field in a 16-bit MMIO register, performs a bitwise OR, and
  writes the result back to the bit field in the 16-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 16-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 16-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized. Extra left bits in OrData
  are stripped.

  If 16-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioBitFieldOr16 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             OrData
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioBitFieldOr16 (Address, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in a 16-bit MMIO register, performs a bitwise AND, and
  writes the result back to the bit field in the 16-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 16-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 16-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized. Extra left bits in AndData are
  stripped.

  If 16-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioBitFieldAnd16 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             AndData
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioBitFieldAnd16 (Address, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in a 16-bit MMIO register, performs a bitwise AND followed
  by a bitwise OR, and writes the result back to the bit field in the
  16-bit MMIO register and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 16-bit MMIO register specified by Address, performs a bitwise AND
  followed by a bitwise OR between the read result and the value
  specified by AndData, and writes the result to the 16-bit MMIO register
  specified by Address. The value written to the MMIO register is returned.
  This function must guarantee that all MMIO read and write operations are
  serialized. Extra left bits in both AndData and OrData are stripped.

  If 16-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 15, then ASSERT().
  If EndBit is greater than 15, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..15.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..15.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT16
EFIAPI
S3MmioBitFieldAndThenOr16 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT16             AndData,
  IN UINT16             OrData
  )
{
  return InternalSaveMmioWrite16ValueToBootScript (Address, MmioBitFieldAndThenOr16 (Address, StartBit, EndBit, AndData, OrData));
}

/**
  Saves a 32-bit MMIO register value to the boot script.

  This internal worker function saves a 32-bit MMIO register value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT32
InternalSaveMmioWrite32ValueToBootScript (
  IN UINTN              Address,
  IN UINT32             Value
  )
{
  InternalSaveMmioWriteValueToBootScript (S3BootScriptWidthUint32, Address, &Value);

  return Value;
}

/**
  Reads a 32-bit MMIO register saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
  returned. This function must guarantee that all MMIO read and write
  operations are serialized.

  If 32-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to read.

  @return The value read.

**/
UINT32
EFIAPI
S3MmioRead32 (
  IN UINTN              Address
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioRead32 (Address));
}

/**
  Writes a 32-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Writes the 32-bit MMIO register specified by Address with the value specified
  by Value and returns Value. This function must guarantee that all MMIO read
  and write operations are serialized.

  If 32-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value to write to the MMIO register.

  @return The value written the MMIO register.

**/
UINT32
EFIAPI
S3MmioWrite32 (
  IN UINTN              Address,
  IN UINT32             Value
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioWrite32 (Address, Value));
}

/**
  Reads a 32-bit MMIO register, performs a bitwise OR, and writes the
  result back to the 32-bit MMIO register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Reads the 32-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 32-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized.

  If 32-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioOr32 (
  IN UINTN              Address,
  IN UINT32             OrData
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioOr32 (Address, OrData));
}

/**
  Reads a 32-bit MMIO register, performs a bitwise AND, and writes the result
  back to the 32-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 32-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 32-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized.

  If 32-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioAnd32 (
  IN UINTN              Address,
  IN UINT32             AndData
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioAnd32 (Address, AndData));
}

/**
  Reads a 32-bit MMIO register, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 32-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 32-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, performs a
  bitwise OR between the result of the AND operation and the value specified by
  OrData, and writes the result to the 32-bit MMIO register specified by
  Address. The value written to the MMIO register is returned. This function
  must guarantee that all MMIO read and write operations are serialized.

  If 32-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioAndThenOr32 (
  IN UINTN              Address,
  IN UINT32             AndData,
  IN UINT32             OrData
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioAndThenOr32 (Address, AndData, OrData));
}

/**
  Reads a bit field of a MMIO register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Reads the bit field in a 32-bit MMIO register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 32-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Address       MMIO register to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.

  @return The value read.

**/
UINT32
EFIAPI
S3MmioBitFieldRead32 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioBitFieldRead32 (Address, StartBit, EndBit));
}

/**
  Writes a bit field to a MMIO register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Writes Value to the bit field of the MMIO register. The bit field is
  specified by the StartBit and the EndBit. All other bits in the destination
  MMIO register are preserved. The new value of the 32-bit register is returned.

  If 32-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  Value         New value of the bit field.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioBitFieldWrite32 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             Value
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioBitFieldWrite32 (Address, StartBit, EndBit, Value));
}

/**
  Reads a bit field in a 32-bit MMIO register, performs a bitwise OR, and
  writes the result back to the bit field in the 32-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 32-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 32-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized. Extra left bits in OrData
  are stripped.

  If 32-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioBitFieldOr32 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             OrData
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioBitFieldOr32 (Address, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in a 32-bit MMIO register, performs a bitwise AND, and
  writes the result back to the bit field in the 32-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 32-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 32-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized. Extra left bits in AndData are
  stripped.

  If 32-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioBitFieldAnd32 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             AndData
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioBitFieldAnd32 (Address, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in a 32-bit MMIO register, performs a bitwise AND followed
  by a bitwise OR, and writes the result back to the bit field in the
  32-bit MMIO register and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 32-bit MMIO register specified by Address, performs a bitwise AND
  followed by a bitwise OR between the read result and the value
  specified by AndData, and writes the result to the 32-bit MMIO register
  specified by Address. The value written to the MMIO register is returned.
  This function must guarantee that all MMIO read and write operations are
  serialized. Extra left bits in both AndData and OrData are stripped.

  If 32-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 31, then ASSERT().
  If EndBit is greater than 31, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..31.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..31.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT32
EFIAPI
S3MmioBitFieldAndThenOr32 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT32             AndData,
  IN UINT32             OrData
  )
{
  return InternalSaveMmioWrite32ValueToBootScript (Address, MmioBitFieldAndThenOr32 (Address, StartBit, EndBit, AndData, OrData));
}

/**
  Saves a 64-bit MMIO register value to the boot script.

  This internal worker function saves a 64-bit MMIO register value in the S3 script
  to be replayed on S3 resume. 

  If the saving process fails, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value saved to boot script.

  @return Value.

**/
UINT64
InternalSaveMmioWrite64ValueToBootScript (
  IN UINTN              Address,
  IN UINT64             Value
  )
{
  InternalSaveMmioWriteValueToBootScript (S3BootScriptWidthUint64, Address, &Value);

  return Value;
}

/**
  Reads a 64-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
  returned. This function must guarantee that all MMIO read and write
  operations are serialized.

  If 64-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to read.

  @return The value read.

**/
UINT64
EFIAPI
S3MmioRead64 (
  IN UINTN              Address
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioRead64 (Address));
}

/**
  Writes a 64-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Writes the 64-bit MMIO register specified by Address with the value specified
  by Value and returns Value. This function must guarantee that all MMIO read
  and write operations are serialized.

  If 64-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  Value         The value to write to the MMIO register.

  @return The value written the MMIO register.

**/
UINT64
EFIAPI
S3MmioWrite64 (
  IN UINTN              Address,
  IN UINT64             Value
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioWrite64 (Address, Value));
}

/**
  Reads a 64-bit MMIO register, performs a bitwise OR, and writes the
  result back to the 64-bit MMIO register and saves the value in the S3 script 
  to be replayed on S3 resume.

  Reads the 64-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 64-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized.

  If 64-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioOr64 (
  IN UINTN              Address,
  IN UINT64             OrData
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioOr64 (Address, OrData));
}

/**
  Reads a 64-bit MMIO register, performs a bitwise AND, and writes the result
  back to the 64-bit MMIO register and saves the value in the S3 script to be 
  replayed on S3 resume.

  Reads the 64-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 64-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized.

  If 64-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioAnd64 (
  IN UINTN              Address,
  IN UINT64             AndData
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioAnd64 (Address, AndData));
}

/**
  Reads a 64-bit MMIO register, performs a bitwise AND followed by a bitwise
  inclusive OR, and writes the result back to the 64-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 64-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, performs a
  bitwise OR between the result of the AND operation and the value specified by
  OrData, and writes the result to the 64-bit MMIO register specified by
  Address. The value written to the MMIO register is returned. This function
  must guarantee that all MMIO read and write operations are serialized.

  If 64-bit MMIO register operations are not supported, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioAndThenOr64 (
  IN UINTN              Address,
  IN UINT64             AndData,
  IN UINT64             OrData
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioAndThenOr64 (Address, AndData, OrData));
}

/**
  Reads a bit field of a MMIO register saves the value in the S3 script to
  be replayed on S3 resume.

  Reads the bit field in a 64-bit MMIO register. The bit field is specified by
  the StartBit and the EndBit. The value of the bit field is returned.

  If 64-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().

  @param  Address       MMIO register to read.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.

  @return The value read.

**/
UINT64
EFIAPI
S3MmioBitFieldRead64 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioBitFieldRead64 (Address, StartBit, EndBit));
}

/**
  Writes a bit field to a MMIO register and saves the value in the S3 script to
  be replayed on S3 resume.

  Writes Value to the bit field of the MMIO register. The bit field is
  specified by the StartBit and the EndBit. All other bits in the destination
  MMIO register are preserved. The new value of the 64-bit register is returned.

  If 64-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  Value         New value of the bit field.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioBitFieldWrite64 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             Value
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioBitFieldWrite64 (Address, StartBit, EndBit, Value));
}

/**
  Reads a bit field in a 64-bit MMIO register, performs a bitwise OR, and
  writes the result back to the bit field in the 64-bit MMIO register and 
  saves the value in the S3 script to be replayed on S3 resume.

  Reads the 64-bit MMIO register specified by Address, performs a bitwise
  inclusive OR between the read result and the value specified by OrData, and
  writes the result to the 64-bit MMIO register specified by Address. The value
  written to the MMIO register is returned. This function must guarantee that
  all MMIO read and write operations are serialized. Extra left bits in OrData
  are stripped.

  If 64-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  OrData        The value to OR with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioBitFieldOr64 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             OrData
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioBitFieldOr64 (Address, StartBit, EndBit, OrData));
}

/**
  Reads a bit field in a 64-bit MMIO register, performs a bitwise AND, and
  writes the result back to the bit field in the 64-bit MMIO register and saves
  the value in the S3 script to be replayed on S3 resume.

  Reads the 64-bit MMIO register specified by Address, performs a bitwise AND
  between the read result and the value specified by AndData, and writes the
  result to the 64-bit MMIO register specified by Address. The value written to
  the MMIO register is returned. This function must guarantee that all MMIO
  read and write operations are serialized. Extra left bits in AndData are
  stripped.

  If 64-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  AndData       The value to AND with the read value from the MMIO register.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioBitFieldAnd64 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             AndData
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioBitFieldAnd64 (Address, StartBit, EndBit, AndData));
}

/**
  Reads a bit field in a 64-bit MMIO register, performs a bitwise AND followed
  by a bitwise OR, and writes the result back to the bit field in the
  64-bit MMIO register and saves the value in the S3 script to be replayed
  on S3 resume.

  Reads the 64-bit MMIO register specified by Address, performs a bitwise AND
  followed by a bitwise OR between the read result and the value
  specified by AndData, and writes the result to the 64-bit MMIO register
  specified by Address. The value written to the MMIO register is returned.
  This function must guarantee that all MMIO read and write operations are
  serialized. Extra left bits in both AndData and OrData are stripped.

  If 64-bit MMIO register operations are not supported, then ASSERT().
  If StartBit is greater than 63, then ASSERT().
  If EndBit is greater than 63, then ASSERT().
  If EndBit is less than StartBit, then ASSERT().
  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
  If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().

  @param  Address       The MMIO register to write.
  @param  StartBit      The ordinal of the least significant bit in the bit field.
                        Range 0..63.
  @param  EndBit        The ordinal of the most significant bit in the bit field.
                        Range 0..63.
  @param  AndData       The value to AND with the read value from the MMIO register.
  @param  OrData        The value to OR with the result of the AND operation.

  @return The value written back to the MMIO register.

**/
UINT64
EFIAPI
S3MmioBitFieldAndThenOr64 (
  IN UINTN              Address,
  IN UINTN              StartBit,
  IN UINTN              EndBit,
  IN UINT64             AndData,
  IN UINT64             OrData
  )
{
  return InternalSaveMmioWrite64ValueToBootScript (Address, MmioBitFieldAndThenOr64 (Address, StartBit, EndBit, AndData, OrData));
}

/**
  Copy data from MMIO region to system memory by using 8-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from MMIO region specified by starting address StartAddress 
  to system memory specified by Buffer by using 8-bit access. The total 
  number of byte to be copied is specified by Length. Buffer is returned.
  
  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().


  @param  StartAddress    Starting address for the MMIO region to be copied from.
  @param  Length          Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer receiving the data read.

  @return Buffer

**/
UINT8 *
EFIAPI
S3MmioReadBuffer8 (
  IN  UINTN       StartAddress,
  IN  UINTN       Length,
  OUT UINT8       *Buffer
  )
{
  UINT8       *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioReadBuffer8 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint8,
             StartAddress,
             Length / sizeof (UINT8),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}

/**
  Copy data from MMIO region to system memory by using 16-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from MMIO region specified by starting address StartAddress 
  to system memory specified by Buffer by using 16-bit access. The total 
  number of byte to be copied is specified by Length. Buffer is returned.
  
  If StartAddress is not aligned on a 16-bit boundary, then ASSERT().

  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().

  If Length is not aligned on a 16-bit boundary, then ASSERT().
  If Buffer is not aligned on a 16-bit boundary, then ASSERT().

  @param  StartAddress    Starting address for the MMIO region to be copied from.
  @param  Length          Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer receiving the data read.

  @return Buffer

**/
UINT16 *
EFIAPI
S3MmioReadBuffer16 (
  IN  UINTN       StartAddress,
  IN  UINTN       Length,
  OUT UINT16      *Buffer
  )
{
  UINT16       *ReturnBuffer;
  RETURN_STATUS   Status;

  ReturnBuffer = MmioReadBuffer16 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint16,
             StartAddress,
             Length / sizeof (UINT16),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}

/**
  Copy data from MMIO region to system memory by using 32-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from MMIO region specified by starting address StartAddress 
  to system memory specified by Buffer by using 32-bit access. The total 
  number of byte to be copied is specified by Length. Buffer is returned.
  
  If StartAddress is not aligned on a 32-bit boundary, then ASSERT().

  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().

  If Length is not aligned on a 32-bit boundary, then ASSERT().
  If Buffer is not aligned on a 32-bit boundary, then ASSERT().

  @param  StartAddress    Starting address for the MMIO region to be copied from.
  @param  Length          Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer receiving the data read.

  @return Buffer

**/
UINT32 *
EFIAPI
S3MmioReadBuffer32 (
  IN  UINTN       StartAddress,
  IN  UINTN       Length,
  OUT UINT32      *Buffer
  )
{
  UINT32      *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioReadBuffer32 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint32,
             StartAddress,
             Length / sizeof (UINT32),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}

/**
  Copy data from MMIO region to system memory by using 64-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from MMIO region specified by starting address StartAddress 
  to system memory specified by Buffer by using 64-bit access. The total 
  number of byte to be copied is specified by Length. Buffer is returned.
  
  If StartAddress is not aligned on a 64-bit boundary, then ASSERT().

  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().

  If Length is not aligned on a 64-bit boundary, then ASSERT().
  If Buffer is not aligned on a 64-bit boundary, then ASSERT().

  @param  StartAddress    Starting address for the MMIO region to be copied from.
  @param  Length          Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer receiving the data read.

  @return Buffer

**/
UINT64 *
EFIAPI
S3MmioReadBuffer64 (
  IN  UINTN       StartAddress,
  IN  UINTN       Length,
  OUT UINT64      *Buffer
  )
{
  UINT64      *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioReadBuffer64 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint64,
             StartAddress,
             Length / sizeof (UINT64),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}


/**
  Copy data from system memory to MMIO region by using 8-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from system memory specified by Buffer to MMIO region specified 
  by starting address StartAddress by using 8-bit access. The total number 
  of byte to be copied is specified by Length. Buffer is returned.
  
  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().


  @param  StartAddress    Starting address for the MMIO region to be copied to.
  @param  Length     Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer containing the data to write.

  @return Buffer

**/
UINT8 *
EFIAPI
S3MmioWriteBuffer8 (
  IN  UINTN         StartAddress,
  IN  UINTN         Length,
  IN  CONST UINT8   *Buffer
  )
{
  UINT8       *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioWriteBuffer8 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint8,
             StartAddress,
             Length / sizeof (UINT8),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}

/**
  Copy data from system memory to MMIO region by using 16-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from system memory specified by Buffer to MMIO region specified 
  by starting address StartAddress by using 16-bit access. The total number 
  of byte to be copied is specified by Length. Buffer is returned.
  
  If StartAddress is not aligned on a 16-bit boundary, then ASSERT().

  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().

  If Length is not aligned on a 16-bit boundary, then ASSERT().

  If Buffer is not aligned on a 16-bit boundary, then ASSERT().

  @param  StartAddress    Starting address for the MMIO region to be copied to.
  @param  Length     Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer containing the data to write.

  @return Buffer

**/
UINT16 *
EFIAPI
S3MmioWriteBuffer16 (
  IN  UINTN        StartAddress,
  IN  UINTN        Length,
  IN  CONST UINT16 *Buffer
  )
{
  UINT16      *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioWriteBuffer16 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint16,
             StartAddress,
             Length / sizeof (UINT16),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}


/**
  Copy data from system memory to MMIO region by using 32-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from system memory specified by Buffer to MMIO region specified 
  by starting address StartAddress by using 32-bit access. The total number 
  of byte to be copied is specified by Length. Buffer is returned.
  
  If StartAddress is not aligned on a 32-bit boundary, then ASSERT().

  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().

  If Length is not aligned on a 32-bit boundary, then ASSERT().

  If Buffer is not aligned on a 32-bit boundary, then ASSERT().

  @param  StartAddress    Starting address for the MMIO region to be copied to.
  @param  Length     Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer containing the data to write.

  @return Buffer

**/
UINT32 *
EFIAPI
S3MmioWriteBuffer32 (
  IN  UINTN        StartAddress,
  IN  UINTN        Length,
  IN  CONST UINT32 *Buffer
  )
{
  UINT32      *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioWriteBuffer32 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint32,
             StartAddress,
             Length / sizeof (UINT32),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}

/**
  Copy data from system memory to MMIO region by using 64-bit access
  and saves the value in the S3 script to be replayed on S3 resume.

  Copy data from system memory specified by Buffer to MMIO region specified 
  by starting address StartAddress by using 64-bit access. The total number 
  of byte to be copied is specified by Length. Buffer is returned.
  
  If StartAddress is not aligned on a 64-bit boundary, then ASSERT().

  If Length is greater than (MAX_ADDRESS - StartAddress + 1), then ASSERT(). 
  If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().

  If Length is not aligned on a 64-bit boundary, then ASSERT().

  If Buffer is not aligned on a 64-bit boundary, then ASSERT().

  @param  StartAddress    Starting address for the MMIO region to be copied to.
  @param  Length     Size in bytes of the copy.
  @param  Buffer          Pointer to a system memory buffer containing the data to write.

  @return Buffer

**/
UINT64 *
EFIAPI
S3MmioWriteBuffer64 (
  IN  UINTN        StartAddress,
  IN  UINTN        Length,
  IN  CONST UINT64 *Buffer
  )
{
  UINT64      *ReturnBuffer;
  RETURN_STATUS  Status;

  ReturnBuffer = MmioWriteBuffer64 (StartAddress, Length, Buffer);

  Status = S3BootScriptSaveMemWrite (
             S3BootScriptWidthUint64,
             StartAddress,
             Length / sizeof (UINT64),
             ReturnBuffer
             );
  ASSERT (Status == RETURN_SUCCESS);

  return ReturnBuffer;
}

