Skip to content

Commit 478edfc

Browse files
Sysman: Add support to check if resizable Bar supported
Signed-off-by: Jitendra Sharma <[email protected]>
1 parent c96c1b0 commit 478edfc

File tree

9 files changed

+275
-11
lines changed

9 files changed

+275
-11
lines changed

level_zero/tools/source/sysman/linux/fs_access.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,8 @@ bool SysfsAccess::isMyDeviceFile(const std::string dev) {
577577
return false;
578578
}
579579

580+
bool SysfsAccess::isRootUser() {
581+
return FsAccess::isRootUser();
582+
}
583+
580584
} // namespace L0

level_zero/tools/source/sysman/linux/fs_access.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class SysfsAccess : protected FsAccess {
111111
MOCKABLE_VIRTUAL bool fileExists(const std::string file) override;
112112
MOCKABLE_VIRTUAL bool isMyDeviceFile(const std::string dev);
113113
MOCKABLE_VIRTUAL bool directoryExists(const std::string path) override;
114+
MOCKABLE_VIRTUAL bool isRootUser() override;
114115

115116
private:
116117
SysfsAccess(const std::string file);

level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "sysman/pci/pci_imp.h"
1414

15+
#include <linux/pci_regs.h>
16+
1517
namespace L0 {
1618

1719
const std::string LinuxPciImp::deviceDir("device");
@@ -150,7 +152,34 @@ ze_result_t LinuxPciImp::initializeBarProperties(std::vector<zes_pci_bar_propert
150152
return result;
151153
}
152154

155+
// Parse PCIe configuration space to see if resizable Bar is supported
153156
bool LinuxPciImp::resizableBarSupported() {
157+
uint32_t pos = PCI_CFG_SPACE_SIZE;
158+
uint32_t header = 0;
159+
160+
if (!configMemory) {
161+
return false;
162+
}
163+
164+
// Minimum 8 bytes per capability. Hence maximum capabilities that
165+
// could be present in PCI extended configuration space are
166+
// represented by loopCount.
167+
auto loopCount = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
168+
header = getDwordFromConfig(pos);
169+
if (!header) {
170+
return false;
171+
}
172+
173+
while (loopCount-- > 0) {
174+
if (PCI_EXT_CAP_ID(header) == PCI_EXT_CAP_ID_REBAR) {
175+
return true;
176+
}
177+
pos = PCI_EXT_CAP_NEXT(header);
178+
if (pos < PCI_CFG_SPACE_SIZE) {
179+
return false;
180+
}
181+
header = getDwordFromConfig(pos);
182+
}
154183
return false;
155184
}
156185

@@ -161,12 +190,30 @@ bool LinuxPciImp::resizableBarEnabled() {
161190
ze_result_t LinuxPciImp::getState(zes_pci_state_t *state) {
162191
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
163192
}
193+
194+
void LinuxPciImp::pciExtendedConfigRead() {
195+
std::string pciConfigNode;
196+
pSysfsAccess->getRealPath("device/config", pciConfigNode);
197+
int fdConfig = -1;
198+
fdConfig = this->openFunction(pciConfigNode.c_str(), O_RDONLY);
199+
if (fdConfig < 0) {
200+
return;
201+
}
202+
configMemory = std::make_unique<uint8_t[]>(PCI_CFG_SPACE_EXP_SIZE);
203+
memset(configMemory.get(), 0, PCI_CFG_SPACE_EXP_SIZE);
204+
this->preadFunction(fdConfig, configMemory.get(), PCI_CFG_SPACE_EXP_SIZE, 0);
205+
this->closeFunction(fdConfig);
206+
}
207+
164208
LinuxPciImp::LinuxPciImp(OsSysman *pOsSysman) {
165209
pLinuxSysmanImp = static_cast<LinuxSysmanImp *>(pOsSysman);
166210
pSysfsAccess = &pLinuxSysmanImp->getSysfsAccess();
167211
pfsAccess = &pLinuxSysmanImp->getFsAccess();
168212
Device *pDevice = pLinuxSysmanImp->getDeviceHandle();
169213
isLmemSupported = pDevice->getDriverHandle()->getMemoryManager()->isLocalMemorySupported(pDevice->getRootDeviceIndex());
214+
if (pSysfsAccess->isRootUser()) {
215+
pciExtendedConfigRead();
216+
}
170217
}
171218

172219
OsPci *OsPci::create(OsSysman *pOsSysman) {

level_zero/tools/source/sysman/pci/linux/os_pci_imp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99
#include "shared/source/helpers/non_copyable_or_moveable.h"
10+
#include "shared/source/os_interface/linux/sys_calls.h"
1011

1112
#include "sysman/linux/os_sysman_imp.h"
1213
#include "sysman/pci/os_pci.h"
@@ -34,13 +35,22 @@ class LinuxPciImp : public OsPci, NEO::NonCopyableOrMovableClass {
3435
SysfsAccess *pSysfsAccess = nullptr;
3536
FsAccess *pfsAccess = nullptr;
3637
LinuxSysmanImp *pLinuxSysmanImp = nullptr;
38+
std::unique_ptr<uint8_t[]> configMemory;
39+
void pciExtendedConfigRead();
40+
decltype(&NEO::SysCalls::open) openFunction = NEO::SysCalls::open;
41+
decltype(&NEO::SysCalls::close) closeFunction = NEO::SysCalls::close;
42+
decltype(&NEO::SysCalls::pread) preadFunction = NEO::SysCalls::pread;
3743

3844
private:
3945
static const std::string deviceDir;
4046
static const std::string resourceFile;
4147
static const std::string maxLinkSpeedFile;
4248
static const std::string maxLinkWidthFile;
4349
bool isLmemSupported = false;
50+
uint32_t getDwordFromConfig(uint32_t pos) {
51+
return configMemory[pos] | (configMemory[pos + 1] << 8) |
52+
(configMemory[pos + 2] << 16) | (configMemory[pos + 3] << 24);
53+
}
4454
};
4555

4656
} // namespace L0

level_zero/tools/source/sysman/pci/pci_imp.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ ze_result_t PciImp::pciGetInitializedBars(uint32_t *pCount, zes_pci_bar_properti
106106
pBarPropsExt->index = pciBarProperties[i]->index;
107107
pBarPropsExt->size = pciBarProperties[i]->size;
108108
pBarPropsExt->type = pciBarProperties[i]->type;
109-
pBarPropsExt->resizableBarEnabled = resizableBarEnabled;
110-
pBarPropsExt->resizableBarSupported = resizableBarSupported;
109+
pBarPropsExt->resizableBarEnabled = static_cast<ze_bool_t>(resizableBarEnabled);
110+
pBarPropsExt->resizableBarSupported = static_cast<ze_bool_t>(resizableBarSupported);
111111
}
112112
}
113113
}
@@ -117,11 +117,8 @@ ze_result_t PciImp::pciGetInitializedBars(uint32_t *pCount, zes_pci_bar_properti
117117
ze_result_t PciImp::pciGetState(zes_pci_state_t *pState) {
118118
return pOsPci->getState(pState);
119119
}
120-
void PciImp::init() {
121-
if (pOsPci == nullptr) {
122-
pOsPci = OsPci::create(pOsSysman);
123-
}
124-
UNRECOVERABLE_IF(nullptr == pOsPci);
120+
121+
void PciImp::pciGetStaticFields() {
125122
pOsPci->getProperties(&pciProperties);
126123
resizableBarEnabled = pOsPci->resizableBarEnabled();
127124
resizableBarSupported = pOsPci->resizableBarSupported();
@@ -154,6 +151,15 @@ void PciImp::init() {
154151
pOsPci->initializeBarProperties(pciBarProperties);
155152
}
156153

154+
void PciImp::init() {
155+
if (pOsPci == nullptr) {
156+
pOsPci = OsPci::create(pOsSysman);
157+
}
158+
UNRECOVERABLE_IF(nullptr == pOsPci);
159+
160+
pciGetStaticFields();
161+
}
162+
157163
PciImp::~PciImp() {
158164
for (zes_pci_bar_properties_t *pProperties : pciBarProperties) {
159165
delete pProperties;

level_zero/tools/source/sysman/pci/pci_imp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class PciImp : public Pci, NEO::NonCopyableOrMovableClass {
2525
ze_result_t pciStaticProperties(zes_pci_properties_t *pProperties) override;
2626
ze_result_t pciGetInitializedBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) override;
2727
ze_result_t pciGetState(zes_pci_state_t *pState) override;
28+
void pciGetStaticFields();
2829

2930
PciImp() = default;
3031
PciImp(OsSysman *pOsSysman) : pOsSysman(pOsSysman){};

level_zero/tools/test/black_box_tests/zello_sysman.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,21 @@ void testSysmanPci(ze_device_handle_t &device) {
302302
std::cout << "Bar count = " << count << std::endl;
303303
}
304304
std::vector<zes_pci_bar_properties_t> pciBarProps(count);
305+
zes_pci_bar_properties_1_2_t props1_2 = {};
306+
memset(&props1_2, 0, sizeof(zes_pci_bar_properties_1_2_t));
307+
for (uint32_t i = 0; i < count; i++) {
308+
pciBarProps[i].pNext = static_cast<void *>(&props1_2);
309+
pciBarProps[i].stype = ZES_STRUCTURE_TYPE_PCI_BAR_PROPERTIES_1_2;
310+
}
305311
VALIDATECALL(zesDevicePciGetBars(device, &count, pciBarProps.data()));
306312
if (verbose) {
307313
for (uint32_t i = 0; i < count; i++) {
308314
std::cout << "pciBarProps.type = " << std::hex << pciBarProps[i].type << std::endl;
309315
std::cout << "pciBarProps.index = " << std::hex << pciBarProps[i].index << std::endl;
310316
std::cout << "pciBarProps.base = " << std::hex << pciBarProps[i].base << std::endl;
311317
std::cout << "pciBarProps.size = " << std::hex << pciBarProps[i].size << std::endl;
318+
std::cout << "pci_bar_properties_1_2_t.resizableBarSupported = " << static_cast<uint32_t>(props1_2.resizableBarSupported) << std::endl;
319+
std::cout << "pci_bar_properties_1_2_t.resizableBarEnabled = " << static_cast<uint32_t>(props1_2.resizableBarEnabled) << std::endl;
312320
}
313321
}
314322
}

level_zero/tools/test/unit_tests/sources/sysman/pci/linux/mock_sysfs_pci.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const std::string maxLinkSpeedFile("device/max_link_speed");
2121
const std::string maxLinkWidthFile("device/max_link_width");
2222
const std::string mockBdf = "0000:00:02.0";
2323
const std::string mockRealPath = "/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/0000:02:01.0/" + mockBdf;
24+
const std::string mockRealPathConfig = mockRealPath + "/config";
2425
const std::string mockRealPath2LevelsUp = "/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0";
2526

2627
constexpr double mockMaxLinkSpeed = 2.5;
@@ -83,6 +84,11 @@ struct Mock<PciSysfsAccess> : public PciSysfsAccess {
8384
MOCK_METHOD(ze_result_t, read, (const std::string file, std::vector<std::string> &val), (override));
8485
MOCK_METHOD(ze_result_t, readSymLink, (const std::string file, std::string &buf), (override));
8586
MOCK_METHOD(ze_result_t, getRealPath, (const std::string file, std::string &buf), (override));
87+
MOCK_METHOD(bool, isRootUser, (), (override));
88+
89+
bool checkRootUser() {
90+
return true;
91+
}
8692

8793
ze_result_t getValDouble(const std::string file, double &val) {
8894
if (file.compare(maxLinkSpeedFile) == 0) {
@@ -120,6 +126,10 @@ struct Mock<PciSysfsAccess> : public PciSysfsAccess {
120126
val = mockRealPath;
121127
return ZE_RESULT_SUCCESS;
122128
}
129+
if (file.compare("device/config") == 0) {
130+
val = mockRealPathConfig;
131+
return ZE_RESULT_SUCCESS;
132+
}
123133
return ZE_RESULT_ERROR_NOT_AVAILABLE;
124134
}
125135

@@ -136,7 +146,13 @@ struct Mock<PciSysfsAccess> : public PciSysfsAccess {
136146

137147
class PublicLinuxPciImp : public L0::LinuxPciImp {
138148
public:
149+
PublicLinuxPciImp(OsSysman *pOsSysman) : LinuxPciImp(pOsSysman) {}
150+
using LinuxPciImp::closeFunction;
151+
using LinuxPciImp::configMemory;
152+
using LinuxPciImp::openFunction;
153+
using LinuxPciImp::pciExtendedConfigRead;
139154
using LinuxPciImp::pfsAccess;
155+
using LinuxPciImp::preadFunction;
140156
using LinuxPciImp::pSysfsAccess;
141157
};
142158

0 commit comments

Comments
 (0)