2018-08-23 17:53:58 +02:00
|
|
|
/*
|
2024-12-31 13:48:34 +00:00
|
|
|
* Copyright (C) 2018-2025 Intel Corporation
|
2018-09-18 09:11:08 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
|
|
|
|
*/
|
2018-08-23 17:53:58 +02:00
|
|
|
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/linux/os_context_linux.h"
|
2019-02-27 11:39:32 +01:00
|
|
|
|
2021-04-01 14:12:51 +00:00
|
|
|
#include "shared/source/debug_settings/debug_settings_manager.h"
|
2021-01-25 20:43:48 +00:00
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
|
|
|
|
#include "shared/source/execution_environment/root_device_environment.h"
|
2021-03-12 10:46:18 +00:00
|
|
|
#include "shared/source/helpers/engine_node_helper.h"
|
2021-02-10 15:13:50 +00:00
|
|
|
#include "shared/source/helpers/hw_info.h"
|
2023-05-23 16:06:03 +00:00
|
|
|
#include "shared/source/helpers/ptr_math.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/linux/drm_neo.h"
|
2023-01-23 14:55:52 +00:00
|
|
|
#include "shared/source/os_interface/linux/ioctl_helper.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/os_context.h"
|
2021-05-21 01:17:57 +02:00
|
|
|
#include "shared/source/os_interface/os_interface.h"
|
2023-03-10 12:28:11 +00:00
|
|
|
#include "shared/source/os_interface/product_helper.h"
|
2023-03-31 11:41:17 +00:00
|
|
|
#include "shared/source/os_interface/sys_calls_common.h"
|
2018-08-23 17:53:58 +02:00
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2018-11-08 13:05:46 +01:00
|
|
|
|
2022-11-16 19:31:25 +00:00
|
|
|
OsContext *OsContextLinux::create(OSInterface *osInterface, uint32_t rootDeviceIndex, uint32_t contextId, const EngineDescriptor &engineDescriptor) {
|
2025-01-31 15:26:12 +00:00
|
|
|
if (osInterface && osInterface->getDriverModel()->getDriverModelType() == DriverModelType::drm) {
|
2022-11-16 19:31:25 +00:00
|
|
|
return new OsContextLinux(*osInterface->getDriverModel()->as<Drm>(), rootDeviceIndex, contextId, engineDescriptor);
|
2018-11-08 13:05:46 +01:00
|
|
|
}
|
2022-11-16 19:31:25 +00:00
|
|
|
return new OsContext(rootDeviceIndex, contextId, engineDescriptor);
|
2018-08-23 17:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-16 19:31:25 +00:00
|
|
|
OsContextLinux::OsContextLinux(Drm &drm, uint32_t rootDeviceIndex, uint32_t contextId, const EngineDescriptor &engineDescriptor)
|
|
|
|
|
: OsContext(rootDeviceIndex, contextId, engineDescriptor),
|
2023-05-23 16:06:03 +00:00
|
|
|
drm(drm) {
|
|
|
|
|
pagingFence.fill(0u);
|
|
|
|
|
fenceVal.fill(0u);
|
|
|
|
|
}
|
2021-04-15 16:14:04 +00:00
|
|
|
|
2024-06-06 11:23:55 +00:00
|
|
|
bool OsContextLinux::initializeContext(bool allocateInterrupt) {
|
2021-02-10 15:13:50 +00:00
|
|
|
auto hwInfo = drm.getRootDeviceEnvironment().getHardwareInfo();
|
|
|
|
|
auto defaultEngineType = getChosenEngineType(*hwInfo);
|
|
|
|
|
|
2021-03-08 18:50:32 +00:00
|
|
|
if (engineType == defaultEngineType && !isLowPriority() && !isInternalEngine()) {
|
2021-02-10 15:13:50 +00:00
|
|
|
this->setDefaultContext(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool submitDirect = false;
|
|
|
|
|
this->isDirectSubmissionAvailable(*drm.getRootDeviceEnvironment().getHardwareInfo(), submitDirect);
|
|
|
|
|
|
2021-04-29 16:02:10 +00:00
|
|
|
if (drm.isPerContextVMRequired()) {
|
|
|
|
|
this->drmVmIds.resize(deviceBitfield.size(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 14:29:45 +02:00
|
|
|
for (auto deviceIndex = 0u; deviceIndex < deviceBitfield.size(); deviceIndex++) {
|
|
|
|
|
if (deviceBitfield.test(deviceIndex)) {
|
2020-07-07 09:34:31 +02:00
|
|
|
auto drmVmId = drm.getVirtualMemoryAddressSpace(deviceIndex);
|
2020-08-17 12:07:39 +02:00
|
|
|
|
|
|
|
|
if (drm.isPerContextVMRequired()) {
|
2023-03-17 11:50:23 +00:00
|
|
|
drmVmId = deviceIndex;
|
|
|
|
|
[[maybe_unused]] auto ret = drm.createDrmVirtualMemory(drmVmId);
|
2020-08-31 07:04:58 +02:00
|
|
|
DEBUG_BREAK_IF(drmVmId == 0);
|
|
|
|
|
DEBUG_BREAK_IF(ret != 0);
|
2024-07-09 12:27:40 +00:00
|
|
|
|
2023-03-17 11:50:23 +00:00
|
|
|
if (ret != 0) {
|
2024-07-09 12:27:40 +00:00
|
|
|
drmVmId = 0;
|
2023-03-17 11:50:23 +00:00
|
|
|
}
|
2021-04-29 16:02:10 +00:00
|
|
|
UNRECOVERABLE_IF(this->drmVmIds.size() <= deviceIndex);
|
|
|
|
|
this->drmVmIds[deviceIndex] = drmVmId;
|
2020-08-17 12:07:39 +02:00
|
|
|
}
|
2023-03-17 11:50:23 +00:00
|
|
|
|
2024-06-06 11:23:55 +00:00
|
|
|
auto drmContextId = drm.getIoctlHelper()->createDrmContext(drm, *this, drmVmId, deviceIndex, allocateInterrupt);
|
2023-03-17 11:50:23 +00:00
|
|
|
if (drmContextId < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 12:27:40 +00: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 11:50:23 +00:00
|
|
|
this->drmContextIds.push_back(drmContextId);
|
2019-07-11 14:29:45 +02:00
|
|
|
}
|
2018-12-11 08:21:56 +01:00
|
|
|
}
|
2023-02-11 22:03:06 +00:00
|
|
|
return true;
|
2018-12-11 08:21:56 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-20 13:08:46 +00:00
|
|
|
bool OsContextLinux::isDirectSubmissionSupported() const {
|
|
|
|
|
auto &rootDeviceEnvironment = this->getDrm().getRootDeviceEnvironment();
|
|
|
|
|
auto &productHelper = rootDeviceEnvironment.getHelper<ProductHelper>();
|
2023-01-02 11:52:22 +00:00
|
|
|
|
2023-09-20 13:08:46 +00:00
|
|
|
return this->getDrm().isVmBindAvailable() && productHelper.isDirectSubmissionSupported(rootDeviceEnvironment.getReleaseHelper());
|
2021-03-31 13:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-02 11:49:46 +02:00
|
|
|
Drm &OsContextLinux::getDrm() const {
|
|
|
|
|
return this->drm;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 15:27:21 +00:00
|
|
|
void OsContextLinux::waitForPagingFence() {
|
|
|
|
|
for (auto drmIterator = 0u; drmIterator < this->deviceBitfield.size(); drmIterator++) {
|
|
|
|
|
if (this->deviceBitfield.test(drmIterator)) {
|
2023-05-23 16:06:03 +00:00
|
|
|
this->waitForBind(drmIterator);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OsContextLinux::waitForBind(uint32_t drmIterator) {
|
2025-02-05 06:40:13 +00: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-23 16:06:03 +00:00
|
|
|
if (drm.isPerContextVMRequired()) {
|
2025-02-05 06:40:13 +00: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 15:27:21 +00:00
|
|
|
}
|
2023-05-23 16:06:03 +00:00
|
|
|
|
2025-02-05 06:40:13 +00:00
|
|
|
return fenceAddressAndValToWait;
|
2023-05-23 16:06:03 +00:00
|
|
|
|
|
|
|
|
} else {
|
2025-02-05 06:40:13 +00:00
|
|
|
return drm.getFenceAddressAndValToWait(vmHandleId, isLocked);
|
2021-02-05 15:27:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 02:13:00 +00:00
|
|
|
void OsContextLinux::reInitializeContext() {}
|
|
|
|
|
|
2023-03-31 11:41:17 +00: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 11:17:17 +01:00
|
|
|
OsContextLinux::~OsContextLinux() {
|
2021-04-15 16:14:04 +00:00
|
|
|
if (contextInitialized) {
|
|
|
|
|
for (auto drmContextId : drmContextIds) {
|
|
|
|
|
drm.destroyDrmContext(drmContextId);
|
|
|
|
|
}
|
2019-07-11 14:29:45 +02:00
|
|
|
}
|
2021-10-07 07:26:03 +00:00
|
|
|
drmContextIds.clear();
|
|
|
|
|
drmVmIds.clear();
|
2018-11-26 14:04:52 +01:00
|
|
|
}
|
2019-03-26 11:59:46 +01:00
|
|
|
} // namespace NEO
|