Skip to content

Commit 520ee8f

Browse files
committed
ReBarDxe: replace fls with asm and remove uneeded pcie check
1 parent 2a1dd1e commit 520ee8f

File tree

2 files changed

+19
-63
lines changed

2 files changed

+19
-63
lines changed

ReBarDxe/ReBar.c

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
#include "include/pciRegs.h"
1010
#include "include/PciHostBridgeResourceAllocation.h"
1111

12-
#define CAP_POINTER 0x34
13-
#define PCIE_DEVICE 0x10
12+
#ifdef _MSC_VER
13+
#pragma warning(disable:28251)
14+
#include <intrin.h>
15+
#pragma warning(default:28251)
16+
#endif
1417

1518
#define PCI_POSSIBLE_ERROR(val) ((val) == 0xffffffff)
1619

@@ -29,37 +32,18 @@ static EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *pciRootBridgeIo;
2932

3033
static EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL_PREPROCESS_CONTROLLER o_PreprocessController;
3134

32-
INTN fls(UINTN x)
35+
INTN fls(UINT32 x)
3336
{
34-
INTN r = 32;
37+
UINT32 r;
38+
39+
#ifdef _MSC_VER
40+
_BitScanReverse64(&r, x);
41+
#else
42+
asm("bsrl %1,%0"
43+
: "=r" (r)
44+
: "rm" (x), "0" (-1));
45+
#endif
3546

36-
if (!x)
37-
return 0;
38-
if (!(x & 0xffff0000u))
39-
{
40-
x <<= 16;
41-
r -= 16;
42-
}
43-
if (!(x & 0xff000000u))
44-
{
45-
x <<= 8;
46-
r -= 8;
47-
}
48-
if (!(x & 0xf0000000u))
49-
{
50-
x <<= 4;
51-
r -= 4;
52-
}
53-
if (!(x & 0xc0000000u))
54-
{
55-
x <<= 2;
56-
r -= 2;
57-
}
58-
if (!(x & 0x80000000u))
59-
{
60-
x <<= 1;
61-
r -= 1;
62-
}
6347
return r;
6448
}
6549

@@ -103,30 +87,6 @@ EFI_STATUS pciWriteConfigByte(UINTN pciAddress, INTN pos, UINT8 *buf)
10387
return pciRootBridgeIo->Pci.Write(pciRootBridgeIo, EfiPciWidthUint8, pciAddrOffset(pciAddress, pos), 1, buf);
10488
}
10589

106-
BOOLEAN isPCIeDevice(UINTN pciAddress)
107-
{
108-
UINT8 buf = 0xff;
109-
UINT16 offset = CAP_POINTER;
110-
INTN tempPciAddress;
111-
112-
pciReadConfigByte(pciAddress, offset, &buf);
113-
offset = buf;
114-
while (buf)
115-
{
116-
tempPciAddress = offset;
117-
pciReadConfigByte(pciAddress, tempPciAddress, &buf);
118-
if (buf != PCIE_DEVICE)
119-
{
120-
tempPciAddress += 1;
121-
pciReadConfigByte(pciAddress, tempPciAddress, &buf);
122-
offset = buf;
123-
}
124-
else
125-
return TRUE;
126-
}
127-
return FALSE;
128-
}
129-
13090
// adapted from linux pci_find_ext_capability
13191
UINT16 pciFindExtCapability(UINTN pciAddress, INTN cap)
13292
{
@@ -137,10 +97,6 @@ UINT16 pciFindExtCapability(UINTN pciAddress, INTN cap)
13797
/* minimum 8 bytes per capability */
13898
ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
13999

140-
/* Only PCIe devices support extended configuration space */
141-
if (!isPCIeDevice(pciAddress))
142-
return 0;
143-
144100
if (EFI_ERROR(pciReadConfigDword(pciAddress, pos, &header)))
145101
return 0;
146102
/*
@@ -249,7 +205,7 @@ VOID reBarSetupDevice(EFI_HANDLE handle, EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADD
249205
if (!rBarS)
250206
continue;
251207
// start with size from fls
252-
for (UINT8 n = MIN((UINT8)fls(rBarS) - 1, reBarState); n > 0; n--) {
208+
for (UINT8 n = MIN((UINT8)fls(rBarS), reBarState); n > 0; n--) {
253209
// check if size is supported
254210
if (rBarS & (1 << n)) {
255211
pciRebarSetSize(pciAddress, epos, bar, n);

ReBarDxe/ReBarDxe.inf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
gEfiPciRootBridgeIoProtocolGuid
2828

2929
[BuildOptions]
30-
GCC:*_*_*_CC_FLAGS = -flto -DUSING_LTO -Wextra -Wno-unused-parameter -DDXE
30+
GCC:*_*_*_CC_FLAGS = -flto -DUSING_LTO -Wextra -Wno-unused-parameter
3131
GCC:*_*_*_DLINK_FLAGS = -flto
3232
GCC:*_CLANGPDB_*_CC_FLAGS = -Weverything -Wno-documentation -Wno-missing-variable-declarations -Wno-missing-prototypes -Wno-reserved-macro-identifier -Wno-gnu-zero-variadic-macro-arguments -Wno-padded -Wno-reserved-identifier -Wno-strict-prototypes -Wno-documentation-pedantic -Wno-unused-macros
33-
MSFT:*_*_*_CC_FLAGS = /GL /DUSING_LTO /analyze /W4 /DDXE
34-
MSFT:*_*_*_DLINK_FLAGS = /LTCG /NOCOFFGRPINFO /BASE:0x180000000 /ALIGN:0x1000
33+
MSFT:*_*_*_CC_FLAGS = /GL /DUSING_LTO /analyze /W4
34+
MSFT:*_*_*_DLINK_FLAGS = /LTCG /NOCOFFGRPINFO /BASE:0x180000000
3535
GCC:RELEASE_*_*_CC_FLAGS = -DMDEPKG_NDEBUG
3636
INTEL:RELEASE_*_*_CC_FLAGS = /D MDEPKG_NDEBUG
3737
MSFT:RELEASE_*_*_CC_FLAGS = /D MDEPKG_NDEBUG

0 commit comments

Comments
 (0)