X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FLibrary%2FBasePciCf8Lib%2FPciLib.c;h=64bd1557fc5d86ca5c0ea47cdd90993c3175c1e7;hp=7b52a7c9256dce90a861da04491a1188ffa82df8;hb=af2bb549adeefd4e4ae266b8574270218dd5da7c;hpb=cd14fe3dcf9445a2b3c9e8e3eef78c23c4d2b7cd diff --git a/MdePkg/Library/BasePciCf8Lib/PciLib.c b/MdePkg/Library/BasePciCf8Lib/PciLib.c index 7b52a7c925..64bd1557fc 100644 --- a/MdePkg/Library/BasePciCf8Lib/PciLib.c +++ b/MdePkg/Library/BasePciCf8Lib/PciLib.c @@ -10,10 +10,16 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: PciLib.c - **/ + +#include + +#include +#include +#include +#include + // // Declare I/O Ports used to perform PCI Confguration Cycles // @@ -237,7 +243,7 @@ PciCf8AndThenOr8 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 7, then ASSERT(). If EndBit is greater than 7, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to read. @param StartBit The ordinal of the least significant bit in the bit field. @@ -277,7 +283,7 @@ PciCf8BitFieldRead8 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 7, then ASSERT(). If EndBit is greater than 7, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -323,7 +329,7 @@ PciCf8BitFieldWrite8 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 7, then ASSERT(). If EndBit is greater than 7, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -369,7 +375,7 @@ PciCf8BitFieldOr8 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 7, then ASSERT(). If EndBit is greater than 7, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -417,7 +423,7 @@ PciCf8BitFieldAnd8 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 7, then ASSERT(). If EndBit is greater than 7, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -639,7 +645,7 @@ PciCf8AndThenOr16 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 15, then ASSERT(). If EndBit is greater than 15, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to read. @param StartBit The ordinal of the least significant bit in the bit field. @@ -680,7 +686,7 @@ PciCf8BitFieldRead16 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 15, then ASSERT(). If EndBit is greater than 15, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -727,7 +733,7 @@ PciCf8BitFieldWrite16 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 15, then ASSERT(). If EndBit is greater than 15, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -774,7 +780,7 @@ PciCf8BitFieldOr16 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 15, then ASSERT(). If EndBit is greater than 15, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -823,7 +829,7 @@ PciCf8BitFieldAnd16 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 15, then ASSERT(). If EndBit is greater than 15, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -1045,7 +1051,7 @@ PciCf8AndThenOr32 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 31, then ASSERT(). If EndBit is greater than 31, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to read. @param StartBit The ordinal of the least significant bit in the bit field. @@ -1086,7 +1092,7 @@ PciCf8BitFieldRead32 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 31, then ASSERT(). If EndBit is greater than 31, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -1133,7 +1139,7 @@ PciCf8BitFieldWrite32 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 31, then ASSERT(). If EndBit is greater than 31, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -1180,7 +1186,7 @@ PciCf8BitFieldOr32 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 31, then ASSERT(). If EndBit is greater than 31, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -1229,7 +1235,7 @@ PciCf8BitFieldAnd32 ( If the register specified by Address >= 0x100, then ASSERT(). If StartBit is greater than 31, then ASSERT(). If EndBit is greater than 31, then ASSERT(). - If EndBit is less than or equal to StartBit, then ASSERT(). + If EndBit is less than StartBit, then ASSERT(). @param Address PCI configuration register to write. @param StartBit The ordinal of the least significant bit in the bit field. @@ -1277,8 +1283,7 @@ PciCf8BitFieldAndThenOr32( If StartAddress > 0x0FFFFFFF, then ASSERT(). If the register specified by StartAddress >= 0x100, then ASSERT(). If ((StartAddress & 0xFFF) + Size) > 0x100, then ASSERT(). - If (StartAddress + Size - 1) > 0x0FFFFFFF, then ASSERT(). - If Buffer is NULL, then ASSERT(). + If Size > 0 and Buffer is NULL, then ASSERT(). @param StartAddress Starting address that encodes the PCI Bus, Device, Function and Register. @@ -1296,58 +1301,71 @@ PciCf8ReadBuffer ( OUT VOID *Buffer ) { - UINTN EndAddress; + UINTN ReturnValue; ASSERT_INVALID_PCI_ADDRESS (StartAddress, 0); ASSERT (((StartAddress & 0xFFF) + Size) <= 0x100); + + if (Size == 0) { + return Size; + } + ASSERT (Buffer != NULL); - EndAddress = StartAddress + Size; + // + // Save Size for return + // + ReturnValue = Size; - if (StartAddress < EndAddress && (StartAddress & 1)) { + if ((StartAddress & 1) != 0) { // // Read a byte if StartAddress is byte aligned // - *(UINT8*)Buffer = PciCf8Read8 (StartAddress); + *(volatile UINT8 *)Buffer = PciCf8Read8 (StartAddress); StartAddress += sizeof (UINT8); + Size -= sizeof (UINT8); Buffer = (UINT8*)Buffer + 1; } - if (StartAddress < EndAddress && (StartAddress & 2)) { + if (Size >= sizeof (UINT16) && (StartAddress & 2) != 0) { // // Read a word if StartAddress is word aligned // - *(UINT16*)Buffer = PciCf8Read16 (StartAddress); + WriteUnaligned16 ((UINT16 *)Buffer, (UINT16) PciCf8Read16 (StartAddress)); + StartAddress += sizeof (UINT16); + Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; } - while (EndAddress - StartAddress >= 4) { + while (Size >= sizeof (UINT32)) { // // Read as many double words as possible // - *(UINT32*)Buffer = PciCf8Read32 (StartAddress); + WriteUnaligned32 ((UINT32 *)Buffer, (UINT32) PciCf8Read32 (StartAddress)); StartAddress += sizeof (UINT32); + Size -= sizeof (UINT32); Buffer = (UINT32*)Buffer + 1; } - if ((EndAddress & 2) != 0) { + if (Size >= sizeof (UINT16)) { // // Read the last remaining word if exist // - *(UINT16*)Buffer = PciCf8Read16 (StartAddress); + WriteUnaligned16 ((UINT16 *)Buffer, (UINT16) PciCf8Read16 (StartAddress)); StartAddress += sizeof (UINT16); + Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; } - if (EndAddress & 1) { + if (Size >= sizeof (UINT8)) { // // Read the last remaining byte if exist // - *(UINT8*)Buffer = PciCf8Read8 (StartAddress); + *(volatile UINT8 *)Buffer = PciCf8Read8 (StartAddress); } - return Size; + return ReturnValue; } /** @@ -1365,8 +1383,7 @@ PciCf8ReadBuffer ( If StartAddress > 0x0FFFFFFF, then ASSERT(). If the register specified by StartAddress >= 0x100, then ASSERT(). If ((StartAddress & 0xFFF) + Size) > 0x100, then ASSERT(). - If (StartAddress + Size - 1) > 0x0FFFFFFF, then ASSERT(). - If Buffer is NULL, then ASSERT(). + If Size > 0 and Buffer is NULL, then ASSERT(). @param StartAddress Starting address that encodes the PCI Bus, Device, Function and Register. @@ -1384,56 +1401,68 @@ PciCf8WriteBuffer ( IN VOID *Buffer ) { - UINTN EndAddress; + UINTN ReturnValue; ASSERT_INVALID_PCI_ADDRESS (StartAddress, 0); ASSERT (((StartAddress & 0xFFF) + Size) <= 0x100); + + if (Size == 0) { + return 0; + } + ASSERT (Buffer != NULL); - EndAddress = StartAddress + Size; + // + // Save Size for return + // + ReturnValue = Size; - if ((StartAddress < EndAddress) && ((StartAddress & 1)!= 0)) { + if ((StartAddress & 1) != 0) { // // Write a byte if StartAddress is byte aligned // PciCf8Write8 (StartAddress, *(UINT8*)Buffer); StartAddress += sizeof (UINT8); + Size -= sizeof (UINT8); Buffer = (UINT8*)Buffer + 1; } - if (StartAddress < EndAddress && (StartAddress & 2)) { + if (Size >= sizeof (UINT16) && (StartAddress & 2) != 0) { // // Write a word if StartAddress is word aligned // - PciCf8Write16 (StartAddress, *(UINT16*)Buffer); + PciCf8Write16 (StartAddress, ReadUnaligned16 ((UINT16*)Buffer)); StartAddress += sizeof (UINT16); + Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; } - while (EndAddress - StartAddress >= 4) { + while (Size >= sizeof (UINT32)) { // // Write as many double words as possible // - PciCf8Write32 (StartAddress, *(UINT32*)Buffer); + PciCf8Write32 (StartAddress, ReadUnaligned32 ((UINT32*)Buffer)); StartAddress += sizeof (UINT32); + Size -= sizeof (UINT32); Buffer = (UINT32*)Buffer + 1; } - if (EndAddress & 2) { + if (Size >= sizeof (UINT16)) { // // Write the last remaining word if exist // - PciCf8Write16 (StartAddress, *(UINT16*)Buffer); + PciCf8Write16 (StartAddress, ReadUnaligned16 ((UINT16*)Buffer)); StartAddress += sizeof (UINT16); + Size -= sizeof (UINT16); Buffer = (UINT16*)Buffer + 1; } - if (EndAddress & 1) { + if (Size >= sizeof (UINT8)) { // // Write the last remaining byte if exist // PciCf8Write8 (StartAddress, *(UINT8*)Buffer); } - return Size; + return ReturnValue; }