]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/SmmPciExpressLib/PciExpressLib.c
MdePkg: PciExpressLib support variable size MMCONF
[mirror_edk2.git] / MdePkg / Library / SmmPciExpressLib / PciExpressLib.c
index 35b9f775a80b06555e728c44e8eea055e6677f64..97bd32c8d20169dc4c69b9b18cdfd0c6b9e6d6f3 100644 (file)
 #include <Library/PcdLib.h>\r
 \r
 ///\r
-/// Module global that contains the base physical address of the PCI Express MMIO range.\r
+/// Module global that contains the base physical address and size of the PCI Express MMIO range.\r
 ///\r
 UINTN mSmmPciExpressLibPciExpressBaseAddress = 0;\r
+UINTN mSmmPciExpressLibPciExpressBaseSize = 0;\r
 \r
 /**\r
  The constructor function caches the PCI Express Base Address\r
@@ -40,9 +41,10 @@ SmmPciExpressLibConstructor (
  )\r
 {\r
  //\r
- // Cache the physical address of the PCI Express MMIO range into a module global variable\r
+ // Cache the physical address and size of the PCI Express MMIO range into a module global variable\r
  //\r
  mSmmPciExpressLibPciExpressBaseAddress = (UINTN) PcdGet64 (PcdPciExpressBaseAddress);\r
+ mSmmPciExpressLibPciExpressBaseSize = (UINTN) PcdGet64 (PcdPciExpressBaseSize);\r
 \r
  return EFI_SUCCESS;\r
 }\r
@@ -97,8 +99,12 @@ PciExpressRegisterForRuntimeAccess (
  mSmmPciExpressLibPciExpressBaseAddress is initialized in the library constructor from PCD entry\r
  PcdPciExpressBaseAddress.\r
 \r
+ If Address > 0x0FFFFFFF, then ASSERT().\r
+\r
  @param Address The address that encodes the PCI Bus, Device, Function and Register.\r
- @return MMIO address corresponding to Address.\r
+\r
+ @retval (UINTN)-1 Invalid PCI address.\r
+ @retval other     MMIO address corresponding to Address.\r
 \r
 **/\r
 UINTN\r
@@ -110,6 +116,12 @@ GetPciExpressAddress (
  // Make sure Address is valid\r
  //\r
  ASSERT_INVALID_PCI_ADDRESS (Address);\r
+ //\r
+ // Make sure the Address is in MMCONF address space\r
+ //\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINTN) -1;\r
+ }\r
  return mSmmPciExpressLibPciExpressBaseAddress + Address;\r
 }\r
 \r
@@ -125,7 +137,8 @@ GetPciExpressAddress (
  @param Address The address that encodes the PCI Bus, Device, Function and\r
  Register.\r
 \r
- @return The read value from the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The read value from the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -134,6 +147,9 @@ PciExpressRead8 (
  IN UINTN Address\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioRead8 (GetPciExpressAddress (Address));\r
 }\r
 \r
@@ -150,7 +166,8 @@ PciExpressRead8 (
  Register.\r
  @param Value The value to write.\r
 \r
- @return The value written to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -160,6 +177,9 @@ PciExpressWrite8 (
  IN UINT8 Value\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioWrite8 (GetPciExpressAddress (Address), Value);\r
 }\r
 \r
@@ -180,7 +200,8 @@ PciExpressWrite8 (
  Register.\r
  @param OrData The value to OR with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -190,6 +211,9 @@ PciExpressOr8 (
  IN UINT8 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioOr8 (GetPciExpressAddress (Address), OrData);\r
 }\r
 \r
@@ -210,7 +234,8 @@ PciExpressOr8 (
  Register.\r
  @param AndData The value to AND with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -220,6 +245,9 @@ PciExpressAnd8 (
  IN UINT8 AndData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioAnd8 (GetPciExpressAddress (Address), AndData);\r
 }\r
 \r
@@ -242,7 +270,8 @@ PciExpressAnd8 (
  @param AndData The value to AND with the PCI configuration register.\r
  @param OrData The value to OR with the result of the AND operation.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -253,6 +282,9 @@ PciExpressAndThenOr8 (
  IN UINT8 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioAndThenOr8 (\r
  GetPciExpressAddress (Address),\r
  AndData,\r
@@ -278,7 +310,8 @@ PciExpressAndThenOr8 (
  @param EndBit The ordinal of the most significant bit in the bit field.\r
  Range 0..7.\r
 \r
- @return The value of the bit field read from the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value of the bit field read from the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -289,6 +322,9 @@ PciExpressBitFieldRead8 (
  IN UINTN EndBit\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioBitFieldRead8 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -317,7 +353,8 @@ PciExpressBitFieldRead8 (
  Range 0..7.\r
  @param Value The new value of the bit field.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -329,6 +366,9 @@ PciExpressBitFieldWrite8 (
  IN UINT8 Value\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioBitFieldWrite8 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -361,7 +401,8 @@ PciExpressBitFieldWrite8 (
  Range 0..7.\r
  @param OrData The value to OR with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -373,6 +414,9 @@ PciExpressBitFieldOr8 (
  IN UINT8 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioBitFieldOr8 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -405,7 +449,8 @@ PciExpressBitFieldOr8 (
  Range 0..7.\r
  @param AndData The value to AND with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -417,6 +462,9 @@ PciExpressBitFieldAnd8 (
  IN UINT8 AndData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioBitFieldAnd8 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -453,7 +501,8 @@ PciExpressBitFieldAnd8 (
  @param AndData The value to AND with the PCI configuration register.\r
  @param OrData The value to OR with the result of the AND operation.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT8\r
@@ -466,6 +515,9 @@ PciExpressBitFieldAndThenOr8 (
  IN UINT8 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT8) -1;\r
+ }\r
  return MmioBitFieldAndThenOr8 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -488,7 +540,8 @@ PciExpressBitFieldAndThenOr8 (
  @param Address The address that encodes the PCI Bus, Device, Function and\r
  Register.\r
 \r
- @return The read value from the PCI configuration register.\r
+ @retval 0xFF  Invalid PCI address.\r
+ @retval other The read value from the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -497,6 +550,9 @@ PciExpressRead16 (
  IN UINTN Address\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioRead16 (GetPciExpressAddress (Address));\r
 }\r
 \r
@@ -514,7 +570,8 @@ PciExpressRead16 (
  Register.\r
  @param Value The value to write.\r
 \r
- @return The value written to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -524,6 +581,9 @@ PciExpressWrite16 (
  IN UINT16 Value\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioWrite16 (GetPciExpressAddress (Address), Value);\r
 }\r
 \r
@@ -545,7 +605,8 @@ PciExpressWrite16 (
  Register.\r
  @param OrData The value to OR with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -555,6 +616,9 @@ PciExpressOr16 (
  IN UINT16 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioOr16 (GetPciExpressAddress (Address), OrData);\r
 }\r
 \r
@@ -576,7 +640,8 @@ PciExpressOr16 (
  Register.\r
  @param AndData The value to AND with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -586,6 +651,9 @@ PciExpressAnd16 (
  IN UINT16 AndData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioAnd16 (GetPciExpressAddress (Address), AndData);\r
 }\r
 \r
@@ -609,7 +677,8 @@ PciExpressAnd16 (
  @param AndData The value to AND with the PCI configuration register.\r
  @param OrData The value to OR with the result of the AND operation.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -620,6 +689,9 @@ PciExpressAndThenOr16 (
  IN UINT16 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioAndThenOr16 (\r
  GetPciExpressAddress (Address),\r
  AndData,\r
@@ -646,7 +718,8 @@ PciExpressAndThenOr16 (
  @param EndBit The ordinal of the most significant bit in the bit field.\r
  Range 0..15.\r
 \r
- @return The value of the bit field read from the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value of the bit field read from the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -657,6 +730,9 @@ PciExpressBitFieldRead16 (
  IN UINTN EndBit\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioBitFieldRead16 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -686,7 +762,8 @@ PciExpressBitFieldRead16 (
  Range 0..15.\r
  @param Value The new value of the bit field.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -698,6 +775,9 @@ PciExpressBitFieldWrite16 (
  IN UINT16 Value\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioBitFieldWrite16 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -731,7 +811,8 @@ PciExpressBitFieldWrite16 (
  Range 0..15.\r
  @param OrData The value to OR with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -743,6 +824,9 @@ PciExpressBitFieldOr16 (
  IN UINT16 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioBitFieldOr16 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -776,7 +860,8 @@ PciExpressBitFieldOr16 (
  Range 0..15.\r
  @param AndData The value to AND with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -788,6 +873,9 @@ PciExpressBitFieldAnd16 (
  IN UINT16 AndData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioBitFieldAnd16 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -825,7 +913,8 @@ PciExpressBitFieldAnd16 (
  @param AndData The value to AND with the PCI configuration register.\r
  @param OrData The value to OR with the result of the AND operation.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFF  Invalid PCI address.\r
+ @retval other   The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT16\r
@@ -838,6 +927,9 @@ PciExpressBitFieldAndThenOr16 (
  IN UINT16 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT16) -1;\r
+ }\r
  return MmioBitFieldAndThenOr16 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -860,7 +952,8 @@ PciExpressBitFieldAndThenOr16 (
  @param Address The address that encodes the PCI Bus, Device, Function and\r
  Register.\r
 \r
- @return The read value from the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The read value from the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -869,6 +962,9 @@ PciExpressRead32 (
  IN UINTN Address\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioRead32 (GetPciExpressAddress (Address));\r
 }\r
 \r
@@ -886,7 +982,8 @@ PciExpressRead32 (
  Register.\r
  @param Value The value to write.\r
 \r
- @return The value written to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -896,6 +993,9 @@ PciExpressWrite32 (
  IN UINT32 Value\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioWrite32 (GetPciExpressAddress (Address), Value);\r
 }\r
 \r
@@ -917,7 +1017,8 @@ PciExpressWrite32 (
  Register.\r
  @param OrData The value to OR with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -927,6 +1028,9 @@ PciExpressOr32 (
  IN UINT32 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioOr32 (GetPciExpressAddress (Address), OrData);\r
 }\r
 \r
@@ -948,7 +1052,8 @@ PciExpressOr32 (
  Register.\r
  @param AndData The value to AND with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -958,6 +1063,9 @@ PciExpressAnd32 (
  IN UINT32 AndData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioAnd32 (GetPciExpressAddress (Address), AndData);\r
 }\r
 \r
@@ -981,7 +1089,8 @@ PciExpressAnd32 (
  @param AndData The value to AND with the PCI configuration register.\r
  @param OrData The value to OR with the result of the AND operation.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -992,6 +1101,9 @@ PciExpressAndThenOr32 (
  IN UINT32 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioAndThenOr32 (\r
  GetPciExpressAddress (Address),\r
  AndData,\r
@@ -1018,7 +1130,8 @@ PciExpressAndThenOr32 (
  @param EndBit The ordinal of the most significant bit in the bit field.\r
  Range 0..31.\r
 \r
- @return The value of the bit field read from the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value of the bit field read from the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -1029,6 +1142,9 @@ PciExpressBitFieldRead32 (
  IN UINTN EndBit\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioBitFieldRead32 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -1058,7 +1174,8 @@ PciExpressBitFieldRead32 (
  Range 0..31.\r
  @param Value The new value of the bit field.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -1070,6 +1187,9 @@ PciExpressBitFieldWrite32 (
  IN UINT32 Value\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioBitFieldWrite32 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -1103,7 +1223,8 @@ PciExpressBitFieldWrite32 (
  Range 0..31.\r
  @param OrData The value to OR with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -1115,6 +1236,9 @@ PciExpressBitFieldOr32 (
  IN UINT32 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioBitFieldOr32 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -1148,7 +1272,8 @@ PciExpressBitFieldOr32 (
  Range 0..31.\r
  @param AndData The value to AND with the PCI configuration register.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -1160,6 +1285,9 @@ PciExpressBitFieldAnd32 (
  IN UINT32 AndData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioBitFieldAnd32 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -1197,7 +1325,8 @@ PciExpressBitFieldAnd32 (
  @param AndData The value to AND with the PCI configuration register.\r
  @param OrData The value to OR with the result of the AND operation.\r
 \r
- @return The value written back to the PCI configuration register.\r
+ @retval 0xFFFFFFFF Invalid PCI address.\r
+ @retval other      The value written back to the PCI configuration register.\r
 \r
 **/\r
 UINT32\r
@@ -1210,6 +1339,9 @@ PciExpressBitFieldAndThenOr32 (
  IN UINT32 OrData\r
  )\r
 {\r
+ if (Address >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINT32) -1;\r
+ }\r
  return MmioBitFieldAndThenOr32 (\r
  GetPciExpressAddress (Address),\r
  StartBit,\r
@@ -1239,7 +1371,8 @@ PciExpressBitFieldAndThenOr32 (
  @param Size The size in bytes of the transfer.\r
  @param Buffer The pointer to a buffer receiving the data read.\r
 \r
- @return Size read data from StartAddress.\r
+ @retval (UINTN)-1 Invalid PCI address.\r
+ @retval other     Size read data from StartAddress.\r
 \r
 **/\r
 UINTN\r
@@ -1258,6 +1391,13 @@ PciExpressReadBuffer (
  ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
  ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);\r
 \r
+ //\r
+ // Make sure the Address is in MMCONF address space\r
+ //\r
+ if (StartAddress >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINTN) -1;\r
+ }\r
+\r
  if (Size == 0) {\r
  return Size;\r
  }\r
@@ -1342,7 +1482,8 @@ PciExpressReadBuffer (
  @param Size The size in bytes of the transfer.\r
  @param Buffer The pointer to a buffer containing the data to write.\r
 \r
- @return Size written to StartAddress.\r
+ @retval (UINTN)-1 Invalid PCI address.\r
+ @retval other     Size written to StartAddress.\r
 \r
 **/\r
 UINTN\r
@@ -1361,6 +1502,13 @@ PciExpressWriteBuffer (
  ASSERT_INVALID_PCI_ADDRESS (StartAddress);\r
  ASSERT (((StartAddress & 0xFFF) + Size) <= 0x1000);\r
 \r
+ //\r
+ // Make sure the Address is in MMCONF address space\r
+ //\r
+ if (StartAddress >= mSmmPciExpressLibPciExpressBaseSize) {\r
+   return (UINTN) -1;\r
+ }\r
+\r
 \r
  if (Size == 0) {\r
  return 0;\r