2018-08-23 23:53:58 +08:00
|
|
|
/*
|
2024-12-31 21:48:34 +08:00
|
|
|
* Copyright (C) 2018-2025 Intel Corporation
|
2018-09-18 15:11:08 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
2018-08-23 23:53:58 +08:00
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/linux/os_context_linux.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2021-04-01 22:12:51 +08:00
|
|
|
#include "shared/source/debug_settings/debug_settings_manager.h"
|
2021-01-26 04:43:48 +08:00
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
|
|
|
#include "shared/source/execution_environment/root_device_environment.h"
|
2021-03-12 18:46:18 +08:00
|
|
|
#include "shared/source/helpers/engine_node_helper.h"
|
2021-02-10 23:13:50 +08:00
|
|
|
#include "shared/source/helpers/hw_info.h"
|
2023-05-24 00:06:03 +08:00
|
|
|
#include "shared/source/helpers/ptr_math.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/linux/drm_neo.h"
|
2023-01-23 22:55:52 +08:00
|
|
|
#include "shared/source/os_interface/linux/ioctl_helper.h"
|
2025-02-19 17:29:27 +08:00
|
|
|
#include "shared/source/os_interface/linux/os_library_linux.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/os_context.h"
|
2021-05-21 07:17:57 +08:00
|
|
|
#include "shared/source/os_interface/os_interface.h"
|
2023-03-10 20:28:11 +08:00
|
|
|
#include "shared/source/os_interface/product_helper.h"
|
2023-03-31 19:41:17 +08:00
|
|
|
#include "shared/source/os_interface/sys_calls_common.h"
|
2025-02-19 17:29:27 +08:00
|
|
|
#include "shared/source/release_helper/release_helper.h"
|
2018-08-23 23:53:58 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2018-11-08 20:05:46 +08:00
|
|
|
|
2022-11-17 03:31:25 +08:00
|
|
|
OsContext *OsContextLinux::create(OSInterface *osInterface, uint32_t rootDeviceIndex, uint32_t contextId, const EngineDescriptor &engineDescriptor) {
|
2025-01-31 23:26:12 +08:00
|
|
|
if (osInterface && osInterface->getDriverModel()->getDriverModelType() == DriverModelType::drm) {
|
2022-11-17 03:31:25 +08:00
|
|
|
return new OsContextLinux(*osInterface->getDriverModel()->as<Drm>(), rootDeviceIndex, contextId, engineDescriptor);
|
2018-11-08 20:05:46 +08:00
|
|
|
}
|
2022-11-17 03:31:25 +08:00
|
|
|
return new OsContext(rootDeviceIndex, contextId, engineDescriptor);
|
2018-08-23 23:53:58 +08:00
|
|
|
}
|
|
|
|
|
2022-11-17 03:31:25 +08:00
|
|
|
OsContextLinux::OsContextLinux(Drm &drm, uint32_t rootDeviceIndex, uint32_t contextId, const EngineDescriptor &engineDescriptor)
|
|
|
|
: OsContext(rootDeviceIndex, contextId, engineDescriptor),
|
2023-05-24 00:06:03 +08:00
|
|
|
drm(drm) {
|
|
|
|
pagingFence.fill(0u);
|
|
|
|
fenceVal.fill(0u);
|
2025-02-19 17:29:27 +08:00
|
|
|
this->isOpenVinoLoaded();
|
2023-05-24 00:06:03 +08:00
|
|
|
}
|
2021-04-16 00:14:04 +08:00
|
|
|
|
2024-06-06 19:23:55 +08:00
|
|
|
bool OsContextLinux::initializeContext(bool allocateInterrupt) {
|
2021-02-10 23:13:50 +08:00
|
|
|
auto hwInfo = drm.getRootDeviceEnvironment().getHardwareInfo();
|
|
|
|
auto defaultEngineType = getChosenEngineType(*hwInfo);
|
|
|
|
|
2021-03-09 02:50:32 +08:00
|
|
|
if (engineType == defaultEngineType && !isLowPriority() && !isInternalEngine()) {
|
2021-02-10 23:13:50 +08:00
|
|
|
this->setDefaultContext(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool submitDirect = false;
|
|
|
|
this->isDirectSubmissionAvailable(*drm.getRootDeviceEnvironment().getHardwareInfo(), submitDirect);
|
|
|
|
|
2021-04-30 00:02:10 +08:00
|
|
|
if (drm.isPerContextVMRequired()) {
|
|
|
|
this->drmVmIds.resize(deviceBitfield.size(), 0);
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:29:45 +08:00
|
|
|
for (auto deviceIndex = 0u; deviceIndex < deviceBitfield.size(); deviceIndex++) {
|
|
|
|
if (deviceBitfield.test(deviceIndex)) {
|
2020-07-07 15:34:31 +08:00
|
|
|
auto drmVmId = drm.getVirtualMemoryAddressSpace(deviceIndex);
|
2020-08-17 18:07:39 +08:00
|
|
|
|
|
|
|
if (drm.isPerContextVMRequired()) {
|
2023-03-17 19:50:23 +08:00
|
|
|
drmVmId = deviceIndex;
|
|
|
|
[[maybe_unused]] auto ret = drm.createDrmVirtualMemory(drmVmId);
|
2020-08-31 13:04:58 +08:00
|
|
|
DEBUG_BREAK_IF(drmVmId == 0);
|
|
|
|
DEBUG_BREAK_IF(ret != 0);
|
2024-07-09 20:27:40 +08:00
|
|
|
|
2023-03-17 19:50:23 +08:00
|
|
|
if (ret != 0) {
|
2024-07-09 20:27:40 +08:00
|
|
|
drmVmId = 0;
|
2023-03-17 19:50:23 +08:00
|
|
|
}
|
2021-04-30 00:02:10 +08:00
|
|
|
UNRECOVERABLE_IF(this->drmVmIds.size() <= deviceIndex);
|
|
|
|
this->drmVmIds[deviceIndex] = drmVmId;
|
2020-08-17 18:07:39 +08:00
|
|
|
}
|
2023-03-17 19:50:23 +08:00
|
|
|
|
2024-06-06 19:23:55 +08:00
|
|
|
auto drmContextId = drm.getIoctlHelper()->createDrmContext(drm, *this, drmVmId, deviceIndex, allocateInterrupt);
|
2023-03-17 19:50:23 +08:00
|
|
|
if (drmContextId < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-07-09 20:27:40 +08:00
|
|
|
if (drm.isPerContextVMRequired() && this->drmVmIds[deviceIndex] == 0) {
|
|
|
|
drmVmId = 0;
|
|
|
|
[[maybe_unused]] auto ret = drm.queryVmId(drmContextId, drmVmId);
|
|
|
|
DEBUG_BREAK_IF(drmVmId == 0);
|
|
|
|
DEBUG_BREAK_IF(ret != 0);
|
|
|
|
|
|
|
|
this->drmVmIds[deviceIndex] = drmVmId;
|
|
|
|
}
|
|
|
|
|
2023-03-17 19:50:23 +08:00
|
|
|
this->drmContextIds.push_back(drmContextId);
|
2019-07-11 20:29:45 +08:00
|
|
|
}
|
2018-12-11 15:21:56 +08:00
|
|
|
}
|
2023-02-12 06:03:06 +08:00
|
|
|
return true;
|
2018-12-11 15:21:56 +08:00
|
|
|
}
|
|
|
|
|
2025-02-19 17:29:27 +08:00
|
|
|
void OsContextLinux::isOpenVinoLoaded() {
|
|
|
|
std::call_once(this->ovLoadedFlag, [this]() {
|
|
|
|
this->ovLoaded = NEO::Linux::isLibraryLoaded("libopenvino_intel_gpu_plugin.so");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-20 21:08:46 +08:00
|
|
|
bool OsContextLinux::isDirectSubmissionSupported() const {
|
|
|
|
auto &rootDeviceEnvironment = this->getDrm().getRootDeviceEnvironment();
|
|
|
|
auto &productHelper = rootDeviceEnvironment.getHelper<ProductHelper>();
|
2025-02-19 17:29:27 +08:00
|
|
|
const auto releaseHelper = rootDeviceEnvironment.getReleaseHelper();
|
2023-01-02 19:52:22 +08:00
|
|
|
|
2025-02-19 17:29:27 +08:00
|
|
|
return (this->getDrm().isVmBindAvailable() || (this->ovLoaded && releaseHelper && releaseHelper->isDirectSubmissionLightSupported())) && productHelper.isDirectSubmissionSupported(rootDeviceEnvironment.getReleaseHelper());
|
2021-03-31 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
2020-07-02 17:49:46 +08:00
|
|
|
Drm &OsContextLinux::getDrm() const {
|
|
|
|
return this->drm;
|
|
|
|
}
|
2025-02-11 22:31:01 +08:00
|
|
|
bool OsContextLinux::isDirectSubmissionLightActive() const {
|
|
|
|
return this->isDirectSubmissionActive() && !this->getDrm().isVmBindAvailable();
|
|
|
|
}
|
2020-07-02 17:49:46 +08:00
|
|
|
|
2021-02-05 23:27:21 +08:00
|
|
|
void OsContextLinux::waitForPagingFence() {
|
|
|
|
for (auto drmIterator = 0u; drmIterator < this->deviceBitfield.size(); drmIterator++) {
|
|
|
|
if (this->deviceBitfield.test(drmIterator)) {
|
2023-05-24 00:06:03 +08:00
|
|
|
this->waitForBind(drmIterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OsContextLinux::waitForBind(uint32_t drmIterator) {
|
2025-02-05 14:40:13 +08:00
|
|
|
|
|
|
|
auto fenceAddressAndValToWait = getFenceAddressAndValToWait(drmIterator, false);
|
|
|
|
|
|
|
|
const auto fenceAddressToWait = fenceAddressAndValToWait.first;
|
|
|
|
const auto fenceValToWait = fenceAddressAndValToWait.second;
|
|
|
|
|
|
|
|
if (fenceAddressToWait != 0u) {
|
|
|
|
drm.waitUserFence(0u, fenceAddressToWait, fenceValToWait, Drm::ValueWidth::u64, -1, drm.getIoctlHelper()->getWaitUserFenceSoftFlag(), false, NEO::InterruptId::notUsed, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::pair<uint64_t, uint64_t> OsContextLinux::getFenceAddressAndValToWait(uint32_t vmHandleId, bool isLocked) {
|
2023-05-24 00:06:03 +08:00
|
|
|
if (drm.isPerContextVMRequired()) {
|
2025-02-05 14:40:13 +08:00
|
|
|
std::pair<uint64_t, uint64_t> fenceAddressAndValToWait = std::make_pair(0, 0);
|
|
|
|
std::unique_lock<std::mutex> lock;
|
|
|
|
|
|
|
|
if (!isLocked) {
|
|
|
|
lock = drm.lockBindFenceMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pagingFence[vmHandleId] >= fenceVal[vmHandleId])) {
|
|
|
|
|
|
|
|
auto fenceAddress = castToUint64(&this->pagingFence[vmHandleId]);
|
|
|
|
auto fenceValue = this->fenceVal[vmHandleId];
|
|
|
|
fenceAddressAndValToWait = std::make_pair(fenceAddress, fenceValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLocked) {
|
|
|
|
lock.unlock();
|
2021-02-05 23:27:21 +08:00
|
|
|
}
|
2023-05-24 00:06:03 +08:00
|
|
|
|
2025-02-05 14:40:13 +08:00
|
|
|
return fenceAddressAndValToWait;
|
2023-05-24 00:06:03 +08:00
|
|
|
|
|
|
|
} else {
|
2025-02-05 14:40:13 +08:00
|
|
|
return drm.getFenceAddressAndValToWait(vmHandleId, isLocked);
|
2021-02-05 23:27:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:13:00 +08:00
|
|
|
void OsContextLinux::reInitializeContext() {}
|
|
|
|
|
2023-03-31 19:41:17 +08:00
|
|
|
uint64_t OsContextLinux::getOfflineDumpContextId(uint32_t deviceIndex) const {
|
|
|
|
if (deviceIndex < drmContextIds.size()) {
|
|
|
|
const auto processId = SysCalls::getProcessId();
|
|
|
|
const auto drmContextId = drmContextIds[deviceIndex];
|
|
|
|
return static_cast<uint64_t>(processId) << 32 |
|
|
|
|
static_cast<uint64_t>(drmContextId);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-27 18:17:17 +08:00
|
|
|
OsContextLinux::~OsContextLinux() {
|
2021-04-16 00:14:04 +08:00
|
|
|
if (contextInitialized) {
|
|
|
|
for (auto drmContextId : drmContextIds) {
|
2025-04-24 17:09:59 +08:00
|
|
|
drm.destroyDrmContext(drmContextId);
|
2021-04-16 00:14:04 +08:00
|
|
|
}
|
2019-07-11 20:29:45 +08:00
|
|
|
}
|
2021-10-07 15:26:03 +08:00
|
|
|
drmContextIds.clear();
|
|
|
|
drmVmIds.clear();
|
2018-11-26 21:04:52 +08:00
|
|
|
}
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|