]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePciExpressLib/PciLib.c
Update the PciXXXReadBuffer() for better coding style.
[mirror_edk2.git] / MdePkg / Library / BasePciExpressLib / PciLib.c
index 125da5793b0e10b2ec0e7bae0004314bae680558..fab8107af38316ec5833a55e1229cde802f18e5b 100644 (file)
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-  Module Name:  PciLib.c\r
-\r
 **/\r
 \r
 **/\r
 \r
+\r
+#include <Base.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/PciExpressLib.h>\r
+#include <Library/IoLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PcdLib.h>\r
+\r
+\r
 /**\r
   Assert the validity of a PCI address. A valid PCI address should contain 1's\r
   only in the low 28 bits.\r
 /**\r
   Assert the validity of a PCI address. A valid PCI address should contain 1's\r
   only in the low 28 bits.\r
   ASSERT (((A) & ~0xfffffff) == 0)\r
 \r
 \r
   ASSERT (((A) & ~0xfffffff) == 0)\r
 \r
 \r
-UINTN\r
-EFIAPI\r
+/**\r
+  Gets the base address of PCI Express.\r
+  \r
+  This internal functions retrieves PCI Express Base Address via a PCD entry\r
+  PcdPciExpressBaseAddress.\r
+  \r
+  @return The base address of PCI Express.\r
+\r
+**/\r
+STATIC\r
+volatile VOID*\r
 GetPciExpressBaseAddress (\r
   VOID\r
   )\r
 {\r
 GetPciExpressBaseAddress (\r
   VOID\r
   )\r
 {\r
-  return (UINTN)PcdGet64 (PcdPciExpressBaseAddress);\r
+  return (VOID*)(UINTN) PcdGet64 (PcdPciExpressBaseAddress);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -62,7 +79,7 @@ PciExpressRead8 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioRead8 (GetPciExpressBaseAddress () + Address);\r
+  return MmioRead8 ((UINTN) GetPciExpressBaseAddress () + Address);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -89,7 +106,7 @@ PciExpressWrite8 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioWrite8 (GetPciExpressBaseAddress () + Address, Value);\r
+  return MmioWrite8 ((UINTN) GetPciExpressBaseAddress () + Address, Value);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -120,7 +137,7 @@ PciExpressOr8 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioOr8 (GetPciExpressBaseAddress () + Address, OrData);\r
+  return MmioOr8 ((UINTN) GetPciExpressBaseAddress () + Address, OrData);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -151,7 +168,7 @@ PciExpressAnd8 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioAnd8 (GetPciExpressBaseAddress () + Address, AndData);\r
+  return MmioAnd8 ((UINTN) GetPciExpressBaseAddress () + Address, AndData);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -186,7 +203,7 @@ PciExpressAndThenOr8 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioAndThenOr8 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioAndThenOr8 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            AndData,\r
            OrData\r
            );\r
            AndData,\r
            OrData\r
            );\r
@@ -202,7 +219,7 @@ PciExpressAndThenOr8 (
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to read.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to read.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -223,7 +240,7 @@ PciExpressBitFieldRead8 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldRead8 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldRead8 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit\r
            );\r
            StartBit,\r
            EndBit\r
            );\r
@@ -240,7 +257,7 @@ PciExpressBitFieldRead8 (
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -263,7 +280,7 @@ PciExpressBitFieldWrite8 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldWrite8 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldWrite8 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            Value\r
            StartBit,\r
            EndBit,\r
            Value\r
@@ -284,7 +301,7 @@ PciExpressBitFieldWrite8 (
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -307,7 +324,7 @@ PciExpressBitFieldOr8 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldOr8 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldOr8 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            OrData\r
            StartBit,\r
            EndBit,\r
            OrData\r
@@ -328,7 +345,7 @@ PciExpressBitFieldOr8 (
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -351,7 +368,7 @@ PciExpressBitFieldAnd8 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAnd8 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAnd8 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            AndData\r
            StartBit,\r
            EndBit,\r
            AndData\r
@@ -374,7 +391,7 @@ PciExpressBitFieldAnd8 (
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If Address > 0x0FFFFFFF, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -399,7 +416,7 @@ PciExpressBitFieldAndThenOr8 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAndThenOr8 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAndThenOr8 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            AndData,\r
            StartBit,\r
            EndBit,\r
            AndData,\r
@@ -430,7 +447,7 @@ PciExpressRead16 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioRead16 (GetPciExpressBaseAddress () + Address);\r
+  return MmioRead16 ((UINTN) GetPciExpressBaseAddress () + Address);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -458,7 +475,7 @@ PciExpressWrite16 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioWrite16 (GetPciExpressBaseAddress () + Address, Value);\r
+  return MmioWrite16 ((UINTN) GetPciExpressBaseAddress () + Address, Value);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -490,7 +507,7 @@ PciExpressOr16 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioOr16 (GetPciExpressBaseAddress () + Address, OrData);\r
+  return MmioOr16 ((UINTN) GetPciExpressBaseAddress () + Address, OrData);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -522,7 +539,7 @@ PciExpressAnd16 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioAnd16 (GetPciExpressBaseAddress () + Address, AndData);\r
+  return MmioAnd16 ((UINTN) GetPciExpressBaseAddress () + Address, AndData);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -558,7 +575,7 @@ PciExpressAndThenOr16 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioAndThenOr16 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioAndThenOr16 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            AndData,\r
            OrData\r
            );\r
            AndData,\r
            OrData\r
            );\r
@@ -575,7 +592,7 @@ PciExpressAndThenOr16 (
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to read.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to read.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -596,7 +613,7 @@ PciExpressBitFieldRead16 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldRead16 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldRead16 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit\r
            );\r
            StartBit,\r
            EndBit\r
            );\r
@@ -614,7 +631,7 @@ PciExpressBitFieldRead16 (
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -637,7 +654,7 @@ PciExpressBitFieldWrite16 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldWrite16 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldWrite16 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            Value\r
            StartBit,\r
            EndBit,\r
            Value\r
@@ -659,7 +676,7 @@ PciExpressBitFieldWrite16 (
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -682,7 +699,7 @@ PciExpressBitFieldOr16 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldOr16 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldOr16 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            OrData\r
            StartBit,\r
            EndBit,\r
            OrData\r
@@ -704,7 +721,7 @@ PciExpressBitFieldOr16 (
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -727,7 +744,7 @@ PciExpressBitFieldAnd16 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAnd16 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAnd16 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            AndData\r
            StartBit,\r
            EndBit,\r
            AndData\r
@@ -751,7 +768,7 @@ PciExpressBitFieldAnd16 (
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If Address is not aligned on a 16-bit boundary, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -776,7 +793,7 @@ PciExpressBitFieldAndThenOr16 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAndThenOr16 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAndThenOr16 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            AndData,\r
            StartBit,\r
            EndBit,\r
            AndData,\r
@@ -807,7 +824,7 @@ PciExpressRead32 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioRead32 (GetPciExpressBaseAddress () + Address);\r
+  return MmioRead32 ((UINTN) GetPciExpressBaseAddress () + Address);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -835,7 +852,7 @@ PciExpressWrite32 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioWrite32 (GetPciExpressBaseAddress () + Address, Value);\r
+  return MmioWrite32 ((UINTN) GetPciExpressBaseAddress () + Address, Value);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -867,7 +884,7 @@ PciExpressOr32 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioOr32 (GetPciExpressBaseAddress () + Address, OrData);\r
+  return MmioOr32 ((UINTN) GetPciExpressBaseAddress () + Address, OrData);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -899,7 +916,7 @@ PciExpressAnd32 (
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   )\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
-  return MmioAnd32 (GetPciExpressBaseAddress () + Address, AndData);\r
+  return MmioAnd32 ((UINTN) GetPciExpressBaseAddress () + Address, AndData);\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -935,7 +952,7 @@ PciExpressAndThenOr32 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioAndThenOr32 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioAndThenOr32 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            AndData,\r
            OrData\r
            );\r
            AndData,\r
            OrData\r
            );\r
@@ -952,7 +969,7 @@ PciExpressAndThenOr32 (
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to read.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to read.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -973,7 +990,7 @@ PciExpressBitFieldRead32 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldRead32 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldRead32 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit\r
            );\r
            StartBit,\r
            EndBit\r
            );\r
@@ -991,7 +1008,7 @@ PciExpressBitFieldRead32 (
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -1014,7 +1031,7 @@ PciExpressBitFieldWrite32 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldWrite32 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldWrite32 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            Value\r
            StartBit,\r
            EndBit,\r
            Value\r
@@ -1036,7 +1053,7 @@ PciExpressBitFieldWrite32 (
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -1059,7 +1076,7 @@ PciExpressBitFieldOr32 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldOr32 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldOr32 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            OrData\r
            StartBit,\r
            EndBit,\r
            OrData\r
@@ -1081,7 +1098,7 @@ PciExpressBitFieldOr32 (
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -1104,7 +1121,7 @@ PciExpressBitFieldAnd32 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAnd32 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAnd32 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            AndData\r
            StartBit,\r
            EndBit,\r
            AndData\r
@@ -1128,7 +1145,7 @@ PciExpressBitFieldAnd32 (
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
   If Address is not aligned on a 32-bit boundary, then ASSERT().\r
   If StartBit is greater than 31, then ASSERT().\r
   If EndBit is greater than 31, then ASSERT().\r
-  If EndBit is less than or equal to StartBit, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
 \r
   @param  Address   PCI configuration register to write.\r
   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
@@ -1153,7 +1170,7 @@ PciExpressBitFieldAndThenOr32 (
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAndThenOr32 (\r
 {\r
   ASSERT_INVALID_PCI_ADDRESS (Address);\r
   return MmioBitFieldAndThenOr32 (\r
-           GetPciExpressBaseAddress () + Address,\r
+           (UINTN) GetPciExpressBaseAddress () + Address,\r
            StartBit,\r
            EndBit,\r
            AndData,\r
            StartBit,\r
            EndBit,\r
            AndData,\r
@@ -1174,8 +1191,7 @@ PciExpressBitFieldAndThenOr32 (
 \r
   If StartAddress > 0x0FFFFFFF, then ASSERT().\r
   If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().\r
 \r
   If StartAddress > 0x0FFFFFFF, then ASSERT().\r
   If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().\r
-  If (StartAddress + Size - 1)  > 0x0FFFFFFF, then ASSERT().\r
-  If Buffer is NULL, then ASSERT().\r
+  If Size > 0 and Buffer is NULL, then ASSERT().\r
 \r
   @param  StartAddress  Starting address that encodes the PCI Bus, Device,\r
                         Function and Register.\r
 \r
   @param  StartAddress  Starting address that encodes the PCI Bus, Device,\r
                         Function and Register.\r
@@ -1193,57 +1209,72 @@ PciExpressReadBuffer (
   OUT     VOID                      *Buffer\r
   )\r
 {\r
   OUT     VOID                      *Buffer\r
   )\r
 {\r
-  UINTN                             EndAddress;\r
+  UINTN   ReturnValue;\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
+  ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);\r
+\r
+  if (Size == 0) {\r
+    return Size;\r
+  }\r
+\r
   ASSERT (Buffer != NULL);\r
 \r
   ASSERT (Buffer != NULL);\r
 \r
-  EndAddress = StartAddress + Size;\r
+  //\r
+  // Save Size for return\r
+  //\r
+  ReturnValue = Size;\r
 \r
 \r
-  if (StartAddress < EndAddress && (StartAddress & 1)) {\r
+  if ((StartAddress & 1) != 0) {\r
     //\r
     // Read a byte if StartAddress is byte aligned\r
     //\r
     //\r
     // Read a byte if StartAddress is byte aligned\r
     //\r
-    *(UINT8*)Buffer = PciExpressRead8 (StartAddress);\r
+    *(volatile UINT8 *)Buffer = PciExpressRead8 (StartAddress);\r
     StartAddress += sizeof (UINT8);\r
     StartAddress += sizeof (UINT8);\r
+    Size -= sizeof (UINT8);\r
     Buffer = (UINT8*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT8*)Buffer + 1;\r
   }\r
 \r
-  if (StartAddress < EndAddress && (StartAddress & 2)) {\r
+  if (Size >= sizeof (UINT16) && (StartAddress & 2) != 0) {\r
     //\r
     // Read a word if StartAddress is word aligned\r
     //\r
     //\r
     // Read a word if StartAddress is word aligned\r
     //\r
-    *(UINT16*)Buffer = PciExpressRead16 (StartAddress);\r
+    WriteUnaligned16 ((UINT16 *) Buffer, (UINT16) PciExpressRead16 (StartAddress));\r
+\r
     StartAddress += sizeof (UINT16);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  while (EndAddress - StartAddress >= 4) {\r
+  while (Size >= sizeof (UINT32)) {\r
     //\r
     // Read as many double words as possible\r
     //\r
     //\r
     // Read as many double words as possible\r
     //\r
-    *(UINT32*)Buffer = PciExpressRead32 (StartAddress);\r
+    WriteUnaligned32 ((UINT32 *) Buffer, (UINT32) PciExpressRead32 (StartAddress));\r
+\r
     StartAddress += sizeof (UINT32);\r
     StartAddress += sizeof (UINT32);\r
+    Size -= sizeof (UINT32);\r
     Buffer = (UINT32*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT32*)Buffer + 1;\r
   }\r
 \r
-  if ((EndAddress & 2) != 0) {\r
+  if (Size >= sizeof (UINT16)) {\r
     //\r
     // Read the last remaining word if exist\r
     //\r
     //\r
     // Read the last remaining word if exist\r
     //\r
-    *(UINT16*)Buffer = PciExpressRead16 (StartAddress);\r
+    WriteUnaligned16 ((UINT16 *) Buffer, (UINT16) PciExpressRead16 (StartAddress));\r
     StartAddress += sizeof (UINT16);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  if (EndAddress & 1) {\r
+  if (Size >= sizeof (UINT8)) {\r
     //\r
     // Read the last remaining byte if exist\r
     //\r
     //\r
     // Read the last remaining byte if exist\r
     //\r
-    *(UINT8*)Buffer = PciExpressRead8 (StartAddress);\r
+    *(volatile UINT8 *)Buffer = PciExpressRead8 (StartAddress);\r
   }\r
 \r
   }\r
 \r
-  return Size;\r
+  return ReturnValue;\r
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
@@ -1260,8 +1291,7 @@ PciExpressReadBuffer (
 \r
   If StartAddress > 0x0FFFFFFF, then ASSERT().\r
   If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().\r
 \r
   If StartAddress > 0x0FFFFFFF, then ASSERT().\r
   If ((StartAddress & 0xFFF) + Size) > 0x1000, then ASSERT().\r
-  If (StartAddress + Size - 1)  > 0x0FFFFFFF, then ASSERT().\r
-  If Buffer is NULL, then ASSERT().\r
+  If Size > 0 and Buffer is NULL, then ASSERT().\r
 \r
   @param  StartAddress  Starting address that encodes the PCI Bus, Device,\r
                         Function and Register.\r
 \r
   @param  StartAddress  Starting address that encodes the PCI Bus, Device,\r
                         Function and Register.\r
@@ -1279,55 +1309,68 @@ PciExpressWriteBuffer (
   IN      VOID                      *Buffer\r
   )\r
 {\r
   IN      VOID                      *Buffer\r
   )\r
 {\r
-  UINTN                             EndAddress;\r
+  UINTN                             ReturnValue;\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
 \r
   ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
+  ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);\r
+\r
+  if (Size == 0) {\r
+    return 0;\r
+  }\r
+\r
   ASSERT (Buffer != NULL);\r
 \r
   ASSERT (Buffer != NULL);\r
 \r
-  EndAddress = StartAddress + Size;\r
+  //\r
+  // Save Size for return\r
+  //\r
+  ReturnValue = Size;\r
 \r
 \r
-  if ((StartAddress < EndAddress) && ((StartAddress & 1)!= 0)) {\r
+  if ((StartAddress & 1) != 0) {\r
     //\r
     // Write a byte if StartAddress is byte aligned\r
     //\r
     PciExpressWrite8 (StartAddress, *(UINT8*)Buffer);\r
     StartAddress += sizeof (UINT8);\r
     //\r
     // Write a byte if StartAddress is byte aligned\r
     //\r
     PciExpressWrite8 (StartAddress, *(UINT8*)Buffer);\r
     StartAddress += sizeof (UINT8);\r
+    Size -= sizeof (UINT8);\r
     Buffer = (UINT8*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT8*)Buffer + 1;\r
   }\r
 \r
-  if (StartAddress < EndAddress && (StartAddress & 2)) {\r
+  if (Size >= sizeof (UINT16) && (StartAddress & 2) != 0) {\r
     //\r
     // Write a word if StartAddress is word aligned\r
     //\r
     //\r
     // Write a word if StartAddress is word aligned\r
     //\r
-    PciExpressWrite16 (StartAddress, *(UINT16*)Buffer);\r
+    PciExpressWrite16 (StartAddress, ReadUnaligned16 ((UINT16*)Buffer));\r
     StartAddress += sizeof (UINT16);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  while (EndAddress - StartAddress >= 4) {\r
+  while (Size >= sizeof (UINT32)) {\r
     //\r
     // Write as many double words as possible\r
     //\r
     //\r
     // Write as many double words as possible\r
     //\r
-    PciExpressWrite32 (StartAddress, *(UINT32*)Buffer);\r
+    PciExpressWrite32 (StartAddress, ReadUnaligned32 ((UINT32*)Buffer));\r
     StartAddress += sizeof (UINT32);\r
     StartAddress += sizeof (UINT32);\r
+    Size -= sizeof (UINT32);\r
     Buffer = (UINT32*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT32*)Buffer + 1;\r
   }\r
 \r
-  if (EndAddress & 2) {\r
+  if (Size >= sizeof (UINT16)) {\r
     //\r
     // Write the last remaining word if exist\r
     //\r
     //\r
     // Write the last remaining word if exist\r
     //\r
-    PciExpressWrite16 (StartAddress, *(UINT16*)Buffer);\r
+    PciExpressWrite16 (StartAddress, ReadUnaligned16 ((UINT16*)Buffer));\r
     StartAddress += sizeof (UINT16);\r
     StartAddress += sizeof (UINT16);\r
+    Size -= sizeof (UINT16);\r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
     Buffer = (UINT16*)Buffer + 1;\r
   }\r
 \r
-  if (EndAddress & 1) {\r
+  if (Size >= sizeof (UINT8)) {\r
     //\r
     // Write the last remaining byte if exist\r
     //\r
     PciExpressWrite8 (StartAddress, *(UINT8*)Buffer);\r
   }\r
 \r
     //\r
     // Write the last remaining byte if exist\r
     //\r
     PciExpressWrite8 (StartAddress, *(UINT8*)Buffer);\r
   }\r
 \r
-  return Size;\r
+  return ReturnValue;\r
 }\r
 }\r