2020-03-06 18:09:57 +08:00
|
|
|
/*
|
2025-01-09 10:25:53 +08:00
|
|
|
* Copyright (C) 2020-2025 Intel Corporation
|
2020-03-06 18:09:57 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-03-19 13:21:57 +08:00
|
|
|
#include "level_zero/core/source/driver/driver.h"
|
2020-03-06 18:09:57 +08:00
|
|
|
|
|
|
|
#include "shared/source/device/device.h"
|
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
2023-10-06 07:12:34 +08:00
|
|
|
#include "shared/source/helpers/gfx_core_helper.h"
|
2020-07-13 21:14:16 +08:00
|
|
|
#include "shared/source/os_interface/debug_env_reader.h"
|
2020-03-06 18:09:57 +08:00
|
|
|
#include "shared/source/os_interface/device_factory.h"
|
2024-07-23 20:50:37 +08:00
|
|
|
#include "shared/source/os_interface/sys_calls_common.h"
|
2023-08-03 01:33:48 +08:00
|
|
|
#include "shared/source/pin/pin.h"
|
2020-03-06 18:09:57 +08:00
|
|
|
|
2020-03-19 13:21:57 +08:00
|
|
|
#include "level_zero/core/source/device/device.h"
|
2020-07-16 20:01:25 +08:00
|
|
|
#include "level_zero/core/source/driver/driver_handle_imp.h"
|
2020-03-19 13:21:57 +08:00
|
|
|
#include "level_zero/core/source/driver/driver_imp.h"
|
2020-07-16 20:01:25 +08:00
|
|
|
#include "level_zero/tools/source/metrics/metric.h"
|
2020-03-06 18:09:57 +08:00
|
|
|
|
2024-05-18 16:54:36 +08:00
|
|
|
#include "driver_version.h"
|
2024-06-07 18:29:38 +08:00
|
|
|
#include "log_manager.h"
|
2024-05-18 16:54:36 +08:00
|
|
|
|
2020-03-09 23:14:18 +08:00
|
|
|
#include <memory>
|
2024-07-23 20:50:37 +08:00
|
|
|
#include <mutex>
|
2020-03-06 18:09:57 +08:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
namespace L0 {
|
|
|
|
|
2023-11-24 01:09:04 +08:00
|
|
|
_ze_driver_handle_t *globalDriverHandle;
|
|
|
|
bool levelZeroDriverInitialized = false;
|
2023-11-15 17:12:53 +08:00
|
|
|
uint32_t driverCount = 0;
|
2020-03-06 18:09:57 +08:00
|
|
|
|
2020-07-16 20:01:25 +08:00
|
|
|
void DriverImp::initialize(ze_result_t *result) {
|
|
|
|
*result = ZE_RESULT_ERROR_UNINITIALIZED;
|
2024-07-23 20:50:37 +08:00
|
|
|
pid = NEO::SysCalls::getCurrentProcessId();
|
2020-03-06 18:09:57 +08:00
|
|
|
|
2020-07-13 21:14:16 +08:00
|
|
|
NEO::EnvironmentVariableReader envReader;
|
|
|
|
L0EnvVariables envVariables = {};
|
|
|
|
envVariables.affinityMask =
|
|
|
|
envReader.getSetting("ZE_AFFINITY_MASK", std::string(""));
|
|
|
|
envVariables.programDebugging =
|
2023-01-18 22:41:50 +08:00
|
|
|
envReader.getSetting("ZET_ENABLE_PROGRAM_DEBUGGING", 0);
|
2020-07-16 20:01:25 +08:00
|
|
|
envVariables.metrics =
|
2020-08-06 00:03:45 +08:00
|
|
|
envReader.getSetting("ZET_ENABLE_METRICS", false);
|
2020-07-17 01:41:28 +08:00
|
|
|
envVariables.pin =
|
2020-08-06 00:03:45 +08:00
|
|
|
envReader.getSetting("ZET_ENABLE_PROGRAM_INSTRUMENTATION", false);
|
2020-08-06 05:03:39 +08:00
|
|
|
envVariables.sysman =
|
|
|
|
envReader.getSetting("ZES_ENABLE_SYSMAN", false);
|
2020-12-17 01:59:34 +08:00
|
|
|
envVariables.pciIdDeviceOrder =
|
|
|
|
envReader.getSetting("ZE_ENABLE_PCI_ID_DEVICE_ORDER", false);
|
2023-02-28 00:53:08 +08:00
|
|
|
envVariables.fp64Emulation =
|
|
|
|
envReader.getSetting("NEO_FP64_EMULATION", false);
|
2020-07-13 21:14:16 +08:00
|
|
|
|
2020-03-06 18:09:57 +08:00
|
|
|
auto executionEnvironment = new NEO::ExecutionEnvironment();
|
|
|
|
UNRECOVERABLE_IF(nullptr == executionEnvironment);
|
|
|
|
|
2023-11-30 16:32:25 +08:00
|
|
|
if (!NEO::debugManager.flags.ExperimentalEnableL0DebuggerForOpenCL.get()) {
|
2023-03-16 20:03:21 +08:00
|
|
|
const auto dbgMode = NEO::getDebuggingMode(envVariables.programDebugging);
|
|
|
|
executionEnvironment->setDebuggingMode(dbgMode);
|
2020-07-13 21:14:16 +08:00
|
|
|
}
|
|
|
|
|
2024-05-18 16:54:36 +08:00
|
|
|
// Logging enablement if opted
|
2024-06-07 18:29:38 +08:00
|
|
|
NEO::initLogger();
|
2024-04-23 03:25:51 +08:00
|
|
|
|
2023-02-28 00:53:08 +08:00
|
|
|
if (envVariables.fp64Emulation) {
|
|
|
|
executionEnvironment->setFP64EmulationEnabled();
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:37:09 +08:00
|
|
|
executionEnvironment->setMetricsEnabled(envVariables.metrics);
|
|
|
|
|
2020-03-06 18:09:57 +08:00
|
|
|
executionEnvironment->incRefInternal();
|
2020-05-07 05:26:28 +08:00
|
|
|
auto neoDevices = NEO::DeviceFactory::createDevices(*executionEnvironment);
|
2020-03-06 18:09:57 +08:00
|
|
|
executionEnvironment->decRefInternal();
|
2020-05-07 05:26:28 +08:00
|
|
|
if (!neoDevices.empty()) {
|
2023-11-24 01:09:04 +08:00
|
|
|
globalDriverHandle = DriverHandle::create(std::move(neoDevices), envVariables, result);
|
|
|
|
if (globalDriverHandle != nullptr) {
|
2023-11-15 17:12:53 +08:00
|
|
|
driverCount = 1;
|
2020-07-16 20:01:25 +08:00
|
|
|
*result = ZE_RESULT_SUCCESS;
|
|
|
|
|
|
|
|
if (envVariables.metrics) {
|
2022-01-12 16:24:41 +08:00
|
|
|
*result = MetricDeviceContext::enableMetricApi();
|
2020-07-17 01:41:28 +08:00
|
|
|
}
|
2023-11-15 17:12:53 +08:00
|
|
|
if (*result != ZE_RESULT_SUCCESS) {
|
2023-11-24 01:09:04 +08:00
|
|
|
delete globalDriver;
|
|
|
|
globalDriverHandle = nullptr;
|
|
|
|
globalDriver = nullptr;
|
2023-11-15 17:12:53 +08:00
|
|
|
driverCount = 0;
|
2024-08-27 20:11:46 +08:00
|
|
|
} else if (envVariables.pin) {
|
2024-08-29 17:20:20 +08:00
|
|
|
std::unique_lock<std::mutex> mtx{this->gtpinInitMtx};
|
|
|
|
this->gtPinInitializationNeeded = true;
|
2023-11-15 17:12:53 +08:00
|
|
|
}
|
2020-03-06 18:09:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 20:01:25 +08:00
|
|
|
ze_result_t DriverImp::initStatus(ZE_RESULT_ERROR_UNINITIALIZED);
|
2020-03-06 18:09:57 +08:00
|
|
|
|
2024-11-05 02:49:10 +08:00
|
|
|
ze_result_t DriverImp::driverInit() {
|
2020-03-06 18:09:57 +08:00
|
|
|
std::call_once(initDriverOnce, [this]() {
|
2020-07-16 20:01:25 +08:00
|
|
|
ze_result_t result;
|
2020-03-06 18:09:57 +08:00
|
|
|
this->initialize(&result);
|
|
|
|
initStatus = result;
|
|
|
|
});
|
2020-07-16 20:01:25 +08:00
|
|
|
return initStatus;
|
2020-03-06 18:09:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-05 02:49:10 +08:00
|
|
|
ze_result_t DriverImp::driverHandleGet(uint32_t *pCount, ze_driver_handle_t *phDriverHandles) {
|
|
|
|
// Only attempt to Init GtPin when driverHandleGet is called requesting handles.
|
|
|
|
if (phDriverHandles != nullptr && *pCount > 0) {
|
|
|
|
Driver::get()->tryInitGtpin();
|
|
|
|
}
|
2020-03-06 18:09:57 +08:00
|
|
|
if (*pCount == 0) {
|
|
|
|
*pCount = driverCount;
|
|
|
|
return ZE_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pCount > driverCount) {
|
|
|
|
*pCount = driverCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (phDriverHandles == nullptr) {
|
2021-03-22 06:15:54 +08:00
|
|
|
return ZE_RESULT_ERROR_INVALID_NULL_POINTER;
|
2020-03-06 18:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < *pCount; i++) {
|
2023-11-24 01:09:04 +08:00
|
|
|
phDriverHandles[i] = globalDriverHandle;
|
2020-03-06 18:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ZE_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2024-08-29 17:20:20 +08:00
|
|
|
void DriverImp::tryInitGtpin() {
|
|
|
|
if (!this->gtPinInitializationNeeded) {
|
|
|
|
return;
|
2024-08-27 20:11:46 +08:00
|
|
|
}
|
2024-08-29 17:20:20 +08:00
|
|
|
std::unique_lock<std::mutex> mtx{this->gtpinInitMtx};
|
|
|
|
if (this->gtPinInitializationNeeded) {
|
|
|
|
this->gtPinInitializationNeeded = false;
|
2024-08-27 20:11:46 +08:00
|
|
|
std::string gtpinFuncName{"OpenGTPin"};
|
2024-08-29 17:20:20 +08:00
|
|
|
NEO::PinContext::init(gtpinFuncName);
|
2024-08-27 20:11:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:09:57 +08:00
|
|
|
static DriverImp driverImp;
|
|
|
|
Driver *Driver::driver = &driverImp;
|
2024-07-23 20:50:37 +08:00
|
|
|
std::mutex driverInitMutex;
|
2020-03-06 18:09:57 +08:00
|
|
|
|
2024-11-05 02:49:10 +08:00
|
|
|
ze_result_t initDriver() {
|
|
|
|
auto pid = NEO::SysCalls::getCurrentProcessId();
|
|
|
|
|
|
|
|
ze_result_t result = Driver::get()->driverInit();
|
|
|
|
|
|
|
|
if (Driver::get()->getPid() != pid) {
|
|
|
|
std::lock_guard<std::mutex> lock(driverInitMutex);
|
|
|
|
|
|
|
|
if (Driver::get()->getPid() != pid) {
|
|
|
|
Driver::get()->initialize(&result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ZE_RESULT_SUCCESS) {
|
|
|
|
L0::levelZeroDriverInitialized = true;
|
|
|
|
} else {
|
|
|
|
L0::levelZeroDriverInitialized = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-05 05:27:08 +08:00
|
|
|
ze_result_t init(ze_init_flags_t flags) {
|
2023-06-07 04:44:34 +08:00
|
|
|
if (flags && !(flags & ZE_INIT_FLAG_GPU_ONLY)) {
|
2023-11-24 01:09:04 +08:00
|
|
|
L0::levelZeroDriverInitialized = false;
|
2021-08-05 05:27:08 +08:00
|
|
|
return ZE_RESULT_ERROR_UNINITIALIZED;
|
2023-06-07 04:44:34 +08:00
|
|
|
} else {
|
2024-11-05 02:49:10 +08:00
|
|
|
return initDriver();
|
|
|
|
}
|
|
|
|
}
|
2024-07-23 20:50:37 +08:00
|
|
|
|
2024-11-05 02:49:10 +08:00
|
|
|
ze_result_t initDrivers(uint32_t *pCount, ze_driver_handle_t *phDrivers, ze_init_driver_type_desc_t *desc) {
|
|
|
|
ze_result_t result = ZE_RESULT_ERROR_UNINITIALIZED;
|
|
|
|
if (desc->flags & ZE_INIT_DRIVER_TYPE_FLAG_GPU) {
|
|
|
|
result = initDriver();
|
2023-06-07 04:44:34 +08:00
|
|
|
if (result == ZE_RESULT_SUCCESS) {
|
2024-11-05 02:49:10 +08:00
|
|
|
result = Driver::get()->driverHandleGet(pCount, phDrivers);
|
2023-06-07 04:44:34 +08:00
|
|
|
}
|
|
|
|
}
|
2024-11-05 02:49:10 +08:00
|
|
|
return result;
|
2021-08-05 05:27:08 +08:00
|
|
|
}
|
2024-11-05 02:49:10 +08:00
|
|
|
|
2020-03-06 18:09:57 +08:00
|
|
|
} // namespace L0
|