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
3033static 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
13191UINT16 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 );
0 commit comments