Skip to content

Commit f52c81c

Browse files
feature: supports core device handle with zesInit
Related-To: NEO-13805 Signed-off-by: Kulkarni, Ashwin Kumar <[email protected]>
1 parent 8363740 commit f52c81c

17 files changed

+536
-5
lines changed

level_zero/sysman/source/device/os_sysman.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct OsSysman {
2424
static OsSysman *create(SysmanDeviceImp *pSysmanImp);
2525
virtual uint32_t getSubDeviceCount() = 0;
2626
virtual const NEO::HardwareInfo &getHardwareInfo() const = 0;
27+
virtual void getDeviceUuids(std::vector<std::string> &deviceUuids) = 0;
2728
};
2829

2930
} // namespace Sysman

level_zero/sysman/source/device/sysman_device.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ SysmanDevice *SysmanDevice::fromHandle(zes_device_handle_t handle) {
3434
return nullptr;
3535
}
3636
if (std::find(globalSysmanDriver->sysmanDevices.begin(), globalSysmanDriver->sysmanDevices.end(), sysmanDevice) == globalSysmanDriver->sysmanDevices.end()) {
37-
PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "SysmanDevice::fromHandle: Device not found in sysmanDevices list%s\n", "");
38-
return nullptr;
37+
return globalSysmanDriver->getSysmanDeviceFromCoreDeviceHandle(handle);
3938
}
4039
return sysmanDevice;
4140
}

level_zero/sysman/source/device/sysman_device.h

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ struct SysmanDevice : _ze_device_handle_t {
138138
static ze_result_t deviceEnumEnabledVF(zes_device_handle_t hDevice, uint32_t *pCount, zes_vf_handle_t *phVFhandle);
139139

140140
virtual OsSysman *deviceGetOsInterface() = 0;
141+
virtual void getDeviceUuids(std::vector<std::string> &deviceUuids) = 0;
141142
};
142143

143144
} // namespace Sysman

level_zero/sysman/source/device/sysman_device_imp.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ ze_result_t SysmanDeviceImp::fabricPortGetMultiPortThroughput(uint32_t numPorts,
211211
return pFabricPortHandleContext->fabricPortGetMultiPortThroughput(numPorts, phPort, pThroughput);
212212
}
213213

214+
void SysmanDeviceImp::getDeviceUuids(std::vector<std::string> &deviceUuids) {
215+
return pOsSysman->getDeviceUuids(deviceUuids);
216+
}
217+
214218
OsSysman *SysmanDeviceImp::deviceGetOsInterface() {
215219
return pOsSysman;
216220
}

level_zero/sysman/source/device/sysman_device_imp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableAndNonMovableClass {
9393
bool deviceEventListen(zes_event_type_flags_t &pEvent, uint64_t timeout) override;
9494
ze_result_t fabricPortGetMultiPortThroughput(uint32_t numPorts, zes_fabric_port_handle_t *phPort, zes_fabric_port_throughput_t **pThroughput) override;
9595
ze_result_t deviceEnumEnabledVF(uint32_t *pCount, zes_vf_handle_t *phVFhandle) override;
96-
9796
OsSysman *deviceGetOsInterface() override;
97+
void getDeviceUuids(std::vector<std::string> &deviceUuids) override;
9898

9999
private:
100100
NEO::ExecutionEnvironment *executionEnvironment = nullptr;

level_zero/sysman/source/driver/sysman_driver_handle_imp.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,55 @@ struct SysmanDriverHandleImp *globalSysmanDriver;
2828

2929
SysmanDriverHandleImp::SysmanDriverHandleImp() = default;
3030

31+
void SysmanDriverHandleImp::updateUuidMap(SysmanDevice *sysmanDevice) {
32+
std::vector<std::string> uuidArr;
33+
sysmanDevice->getDeviceUuids(uuidArr);
34+
for (auto &uuid : uuidArr) {
35+
uuidDeviceMap[uuid] = sysmanDevice;
36+
}
37+
return;
38+
}
39+
40+
SysmanDevice *SysmanDriverHandleImp::findSysmanDeviceFromCoreToSysmanDeviceMap(ze_device_handle_t handle) {
41+
auto iterator = coreToSysmanDeviceMap.find(handle);
42+
if (iterator != coreToSysmanDeviceMap.end()) {
43+
SysmanDevice *sysmanDevice = iterator->second;
44+
return sysmanDevice;
45+
}
46+
return nullptr;
47+
}
48+
49+
SysmanDevice *SysmanDriverHandleImp::getSysmanDeviceFromCoreDeviceHandle(ze_device_handle_t hDevice) {
50+
const std::lock_guard<std::mutex> lock(this->coreToSysmanDeviceMapLock);
51+
if (hDevice == nullptr) {
52+
return nullptr;
53+
}
54+
55+
SysmanDevice *sysmanDevice = findSysmanDeviceFromCoreToSysmanDeviceMap(hDevice);
56+
if (sysmanDevice != nullptr) {
57+
return sysmanDevice;
58+
}
59+
60+
ze_device_properties_t deviceProperties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES};
61+
Device::fromHandle(hDevice)->getProperties(&deviceProperties);
62+
std::string uuid(reinterpret_cast<char const *>(deviceProperties.uuid.id));
63+
auto it = uuidDeviceMap.find(uuid);
64+
if (it == uuidDeviceMap.end()) {
65+
PRINT_DEBUG_STRING(NEO::debugManager.flags.PrintDebugMessages.get(), stderr, "SysmanDriverHandleImp::getSysmanDeviceFromCoreDeviceHandle() - sysman device handle equivalent to core device handle not found!! %s\n", "");
66+
return nullptr;
67+
}
68+
sysmanDevice = it->second;
69+
coreToSysmanDeviceMap[hDevice] = sysmanDevice;
70+
71+
return sysmanDevice;
72+
}
73+
3174
ze_result_t SysmanDriverHandleImp::initialize(NEO::ExecutionEnvironment &executionEnvironment) {
3275
for (uint32_t rootDeviceIndex = 0u; rootDeviceIndex < executionEnvironment.rootDeviceEnvironments.size(); rootDeviceIndex++) {
3376
auto pSysmanDevice = SysmanDevice::create(executionEnvironment, rootDeviceIndex);
3477
if (pSysmanDevice != nullptr) {
3578
this->sysmanDevices.push_back(pSysmanDevice);
79+
updateUuidMap(pSysmanDevice);
3680
}
3781
}
3882

level_zero/sysman/source/driver/sysman_driver_handle_imp.h

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99
#include "level_zero/sysman/source/driver/sysman_driver_handle.h"
1010

11+
#include <mutex>
1112
#include <string>
1213
#include <unordered_map>
1314

@@ -29,6 +30,16 @@ struct SysmanDriverHandleImp : SysmanDriverHandle {
2930
uint32_t numDevices = 0;
3031
ze_result_t getExtensionFunctionAddress(const char *pFuncName, void **pfunc) override;
3132
struct OsSysmanDriver *pOsSysmanDriver = nullptr;
33+
SysmanDevice *getSysmanDeviceFromCoreDeviceHandle(ze_device_handle_t hDevice);
34+
35+
private:
36+
void updateUuidMap(SysmanDevice *sysmanDevice);
37+
SysmanDevice *findSysmanDeviceFromCoreToSysmanDeviceMap(ze_device_handle_t handle);
38+
std::mutex coreToSysmanDeviceMapLock;
39+
std::unordered_map<ze_device_handle_t, SysmanDevice *> coreToSysmanDeviceMap;
40+
41+
protected:
42+
std::unordered_map<std::string, SysmanDevice *> uuidDeviceMap;
3243
};
3344

3445
extern struct SysmanDriverHandleImp *globalSysmanDriver;

level_zero/sysman/source/shared/linux/zes_os_sysman_imp.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,100 @@ bool LinuxSysmanImp::getTelemData(uint32_t subDeviceId, std::string &telemDir, s
496496
return true;
497497
}
498498

499+
void LinuxSysmanImp::getDeviceUuids(std::vector<std::string> &deviceUuids) {
500+
constexpr uint32_t rootDeviceCount = 1;
501+
uint32_t totalUuidCountForDevice = this->getSubDeviceCount() + rootDeviceCount;
502+
deviceUuids.clear();
503+
for (uint32_t index = 0; index < totalUuidCountForDevice; index++) {
504+
std::array<uint8_t, NEO::ProductHelper::uuidSize> deviceUuid;
505+
bool uuidValid = this->getUuidFromSubDeviceInfo(index, deviceUuid);
506+
if (uuidValid) {
507+
uint8_t uuid[ZE_MAX_DEVICE_UUID_SIZE] = {};
508+
std::copy_n(std::begin(deviceUuid), ZE_MAX_DEVICE_UUID_SIZE, std::begin(uuid));
509+
std::string uuidString(reinterpret_cast<char const *>(uuid));
510+
deviceUuids.push_back(uuidString);
511+
}
512+
}
513+
}
514+
515+
bool LinuxSysmanImp::generateUuidFromPciAndSubDeviceInfo(uint32_t subDeviceID, const NEO::PhysicalDevicePciBusInfo &pciBusInfo, std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid) {
516+
if (pciBusInfo.pciDomain != NEO::PhysicalDevicePciBusInfo::invalidValue) {
517+
uuid.fill(0);
518+
519+
// Device UUID uniquely identifies a device within a system.
520+
// We generate it based on device information along with PCI information
521+
// This guarantees uniqueness of UUIDs on a system even when multiple
522+
// identical Intel GPUs are present.
523+
524+
// We want to have UUID matching between different GPU APIs (including outside
525+
// of compute_runtime project - i.e. other than L0 or OCL). This structure definition
526+
// has been agreed upon by various Intel driver teams.
527+
//
528+
// Consult other driver teams before changing this.
529+
//
530+
531+
struct DeviceUUID {
532+
uint16_t vendorID;
533+
uint16_t deviceID;
534+
uint16_t revisionID;
535+
uint16_t pciDomain;
536+
uint8_t pciBus;
537+
uint8_t pciDev;
538+
uint8_t pciFunc;
539+
uint8_t reserved[4];
540+
uint8_t subDeviceID;
541+
};
542+
543+
auto &hwInfo = getParentSysmanDeviceImp()->getHardwareInfo();
544+
DeviceUUID deviceUUID = {};
545+
deviceUUID.vendorID = 0x8086; // Intel
546+
deviceUUID.deviceID = hwInfo.platform.usDeviceID;
547+
deviceUUID.revisionID = hwInfo.platform.usRevId;
548+
deviceUUID.pciDomain = static_cast<uint16_t>(pciBusInfo.pciDomain);
549+
deviceUUID.pciBus = static_cast<uint8_t>(pciBusInfo.pciBus);
550+
deviceUUID.pciDev = static_cast<uint8_t>(pciBusInfo.pciDevice);
551+
deviceUUID.pciFunc = static_cast<uint8_t>(pciBusInfo.pciFunction);
552+
deviceUUID.subDeviceID = subDeviceID;
553+
554+
static_assert(sizeof(DeviceUUID) == NEO::ProductHelper::uuidSize);
555+
556+
memcpy_s(uuid.data(), NEO::ProductHelper::uuidSize, &deviceUUID, sizeof(DeviceUUID));
557+
558+
return true;
559+
}
560+
return false;
561+
}
562+
563+
bool LinuxSysmanImp::getUuidFromSubDeviceInfo(uint32_t subDeviceID, std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid) {
564+
auto subDeviceCount = getSubDeviceCount();
565+
if (uuidVec.size() == 0) {
566+
constexpr uint32_t rootDeviceCount = 1;
567+
uuidVec.resize(subDeviceCount + rootDeviceCount);
568+
}
569+
if (getParentSysmanDeviceImp()->getRootDeviceEnvironment().osInterface != nullptr) {
570+
auto driverModel = getParentSysmanDeviceImp()->getRootDeviceEnvironment().osInterface->getDriverModel();
571+
auto &gfxCoreHelper = getParentSysmanDeviceImp()->getRootDeviceEnvironment().getHelper<NEO::GfxCoreHelper>();
572+
auto &productHelper = getParentSysmanDeviceImp()->getRootDeviceEnvironment().getHelper<NEO::ProductHelper>();
573+
if (NEO::debugManager.flags.EnableChipsetUniqueUUID.get() != 0) {
574+
if (gfxCoreHelper.isChipsetUniqueUUIDSupported()) {
575+
auto hwDeviceId = getSysmanHwDeviceIdInstance();
576+
this->uuidVec[subDeviceID].isValid = productHelper.getUuid(driverModel, subDeviceCount, subDeviceID, this->uuidVec[subDeviceID].id);
577+
}
578+
}
579+
580+
if (!this->uuidVec[subDeviceID].isValid) {
581+
NEO::PhysicalDevicePciBusInfo pciBusInfo = driverModel->getPciBusInfo();
582+
this->uuidVec[subDeviceID].isValid = generateUuidFromPciAndSubDeviceInfo(subDeviceID, pciBusInfo, this->uuidVec[subDeviceID].id);
583+
}
584+
585+
if (this->uuidVec[subDeviceID].isValid) {
586+
uuid = this->uuidVec[subDeviceID].id;
587+
}
588+
}
589+
590+
return this->uuidVec[subDeviceID].isValid;
591+
}
592+
499593
OsSysman *OsSysman::create(SysmanDeviceImp *pParentSysmanDeviceImp) {
500594
LinuxSysmanImp *pLinuxSysmanImp = new LinuxSysmanImp(pParentSysmanDeviceImp);
501595
return static_cast<OsSysman *>(pLinuxSysmanImp);

level_zero/sysman/source/shared/linux/zes_os_sysman_imp.h

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class LinuxSysmanImp : public OsSysman, NEO::NonCopyableAndNonMovableClass {
5050
SysmanDeviceImp *getSysmanDeviceImp();
5151
SysmanProductHelper *getSysmanProductHelper();
5252
uint32_t getSubDeviceCount() override;
53+
void getDeviceUuids(std::vector<std::string> &deviceUuids) override;
5354
const NEO::HardwareInfo &getHardwareInfo() const override { return pParentSysmanDeviceImp->getHardwareInfo(); }
5455
std::string getPciCardBusDirectoryPath(std::string realPciPath);
5556
uint32_t getMemoryType();
@@ -78,6 +79,8 @@ class LinuxSysmanImp : public OsSysman, NEO::NonCopyableAndNonMovableClass {
7879
SysmanKmdInterface *getSysmanKmdInterface() { return pSysmanKmdInterface.get(); }
7980
static ze_result_t getResult(int err);
8081
bool getTelemData(uint32_t subDeviceId, std::string &telemDir, std::string &guid, uint64_t &telemOffset);
82+
bool getUuidFromSubDeviceInfo(uint32_t subDeviceID, std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid);
83+
bool generateUuidFromPciAndSubDeviceInfo(uint32_t subDeviceID, const NEO::PhysicalDevicePciBusInfo &pciBusInfo, std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid);
8184

8285
protected:
8386
std::unique_ptr<SysmanProductHelper> pSysmanProductHelper;
@@ -94,6 +97,11 @@ class LinuxSysmanImp : public OsSysman, NEO::NonCopyableAndNonMovableClass {
9497
std::map<uint32_t, std::unique_ptr<PlatformMonitoringTech::TelemData>> mapOfSubDeviceIdToTelemData;
9598
std::map<uint32_t, std::string> telemNodesInPciPath;
9699
std::unique_ptr<PlatformMonitoringTech::TelemData> pTelemData = nullptr;
100+
struct Uuid {
101+
bool isValid = false;
102+
std::array<uint8_t, NEO::ProductHelper::uuidSize> id;
103+
};
104+
std::vector<Uuid> uuidVec;
97105

98106
private:
99107
LinuxSysmanImp() = delete;

level_zero/sysman/source/shared/windows/zes_os_sysman_imp.cpp

+77
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,83 @@ KmdSysManager &WddmSysmanImp::getKmdSysManager() {
114114
return *pKmdSysManager;
115115
}
116116

117+
void WddmSysmanImp::getDeviceUuids(std::vector<std::string> &deviceUuids) {
118+
deviceUuids.clear();
119+
std::array<uint8_t, NEO::ProductHelper::uuidSize> deviceUuid;
120+
bool uuidValid = this->getUuid(deviceUuid);
121+
if (uuidValid) {
122+
uint8_t uuid[ZE_MAX_DEVICE_UUID_SIZE] = {};
123+
std::copy_n(std::begin(deviceUuid), ZE_MAX_DEVICE_UUID_SIZE, std::begin(uuid));
124+
std::string uuidString(reinterpret_cast<char const *>(uuid));
125+
deviceUuids.push_back(uuidString);
126+
}
127+
}
128+
129+
bool WddmSysmanImp::getUuid(std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid) {
130+
if (getSysmanDeviceImp()->getRootDeviceEnvironment().osInterface != nullptr) {
131+
auto driverModel = getSysmanDeviceImp()->getRootDeviceEnvironment().osInterface->getDriverModel();
132+
if (!this->uuid.isValid) {
133+
NEO::PhysicalDevicePciBusInfo pciBusInfo = driverModel->getPciBusInfo();
134+
this->uuid.isValid = generateUuidFromPciBusInfo(pciBusInfo, this->uuid.id);
135+
}
136+
137+
if (this->uuid.isValid) {
138+
uuid = this->uuid.id;
139+
}
140+
}
141+
142+
return this->uuid.isValid;
143+
}
144+
145+
bool WddmSysmanImp::generateUuidFromPciBusInfo(const NEO::PhysicalDevicePciBusInfo &pciBusInfo, std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid) {
146+
if (pciBusInfo.pciDomain != NEO::PhysicalDevicePciBusInfo::invalidValue) {
147+
uuid.fill(0);
148+
149+
// Device UUID uniquely identifies a device within a system.
150+
// We generate it based on device information along with PCI information
151+
// This guarantees uniqueness of UUIDs on a system even when multiple
152+
// identical Intel GPUs are present.
153+
//
154+
155+
// We want to have UUID matching between different GPU APIs (including outside
156+
// of compute_runtime project - i.e. other than L0 or OCL). This structure definition
157+
// has been agreed upon by various Intel driver teams.
158+
//
159+
// Consult other driver teams before changing this.
160+
//
161+
162+
struct DeviceUUID {
163+
uint16_t vendorID;
164+
uint16_t deviceID;
165+
uint16_t revisionID;
166+
uint16_t pciDomain;
167+
uint8_t pciBus;
168+
uint8_t pciDev;
169+
uint8_t pciFunc;
170+
uint8_t reserved[4];
171+
uint8_t subDeviceID;
172+
};
173+
auto &hwInfo = getSysmanDeviceImp()->getHardwareInfo();
174+
DeviceUUID deviceUUID = {};
175+
deviceUUID.vendorID = 0x8086; // Intel
176+
deviceUUID.deviceID = hwInfo.platform.usDeviceID;
177+
deviceUUID.revisionID = hwInfo.platform.usRevId;
178+
deviceUUID.pciDomain = static_cast<uint16_t>(pciBusInfo.pciDomain);
179+
deviceUUID.pciBus = static_cast<uint8_t>(pciBusInfo.pciBus);
180+
deviceUUID.pciDev = static_cast<uint8_t>(pciBusInfo.pciDevice);
181+
deviceUUID.pciFunc = static_cast<uint8_t>(pciBusInfo.pciFunction);
182+
deviceUUID.subDeviceID = 0;
183+
184+
static_assert(sizeof(DeviceUUID) == NEO::ProductHelper::uuidSize);
185+
186+
memcpy_s(uuid.data(), NEO::ProductHelper::uuidSize, &deviceUUID, sizeof(DeviceUUID));
187+
188+
return true;
189+
}
190+
191+
return false;
192+
}
193+
117194
OsSysman *OsSysman::create(SysmanDeviceImp *pParentSysmanDeviceImp) {
118195
WddmSysmanImp *pWddmSysmanImp = new WddmSysmanImp(pParentSysmanDeviceImp);
119196
return static_cast<OsSysman *>(pWddmSysmanImp);

level_zero/sysman/source/shared/windows/zes_os_sysman_imp.h

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#pragma once
99
#include "shared/source/helpers/hw_info.h"
1010
#include "shared/source/helpers/non_copyable_or_moveable.h"
11+
#include "shared/source/os_interface/driver_info.h"
12+
#include "shared/source/os_interface/product_helper.h"
1113

1214
#include "level_zero/sysman/source/device/os_sysman.h"
1315
#include "level_zero/sysman/source/device/sysman_device.h"
@@ -36,18 +38,25 @@ class WddmSysmanImp : public OsSysman, NEO::NonCopyableAndNonMovableClass {
3638
void releaseFwUtilInterface();
3739

3840
uint32_t getSubDeviceCount() override;
41+
void getDeviceUuids(std::vector<std::string> &deviceUuids) override;
3942
SysmanDeviceImp *getSysmanDeviceImp();
4043
const NEO::HardwareInfo &getHardwareInfo() const override { return pParentSysmanDeviceImp->getHardwareInfo(); }
4144
PRODUCT_FAMILY getProductFamily() const { return pParentSysmanDeviceImp->getProductFamily(); }
4245
SysmanProductHelper *getSysmanProductHelper();
4346
PlatformMonitoringTech *getSysmanPmt();
47+
bool getUuid(std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid);
48+
bool generateUuidFromPciBusInfo(const NEO::PhysicalDevicePciBusInfo &pciBusInfo, std::array<uint8_t, NEO::ProductHelper::uuidSize> &uuid);
4449

4550
protected:
4651
FirmwareUtil *pFwUtilInterface = nullptr;
4752
KmdSysManager *pKmdSysManager = nullptr;
4853
SysmanDevice *pDevice = nullptr;
4954
std::unique_ptr<PlatformMonitoringTech> pPmt;
5055
std::unique_ptr<SysmanProductHelper> pSysmanProductHelper;
56+
struct {
57+
bool isValid = false;
58+
std::array<uint8_t, NEO::ProductHelper::uuidSize> id;
59+
} uuid;
5160

5261
private:
5362
SysmanDeviceImp *pParentSysmanDeviceImp = nullptr;

level_zero/sysman/test/unit_tests/sources/linux/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if(UNIX)
1414
${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_driver.cpp
1515
${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_hw_device_id.cpp
1616
${CMAKE_CURRENT_SOURCE_DIR}/mock_sysman_hw_device_id.h
17+
${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_core_handle_support.cpp
1718
)
1819
endif()
1920

0 commit comments

Comments
 (0)