2017-12-21 07:45:38 +08:00
/*
2022-01-07 22:53:31 +08:00
* Copyright (C) 2018-2022 Intel Corporation
2017-12-21 07:45:38 +08:00
*
2018-09-20 11:54:29 +08:00
* SPDX-License-Identifier: MIT
2017-12-21 07:45:38 +08:00
*
*/
2020-02-24 05:44:01 +08:00
#include "shared/source/command_stream/linear_stream.h"
2020-07-17 17:28:59 +08:00
#include "shared/source/direct_submission/linux/drm_direct_submission.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/execution_environment/execution_environment.h"
2021-09-08 07:30:06 +08:00
#include "shared/source/gmm_helper/client_context/gmm_client_context.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/gmm_helper/gmm_helper.h"
#include "shared/source/gmm_helper/page_table_mngr.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/flush_stamp.h"
2020-05-15 19:52:11 +08:00
#include "shared/source/helpers/hw_helper.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/helpers/preamble.h"
#include "shared/source/memory_manager/residency.h"
#include "shared/source/os_interface/linux/drm_allocation.h"
#include "shared/source/os_interface/linux/drm_buffer_object.h"
2021-09-23 17:03:22 +08:00
#include "shared/source/os_interface/linux/drm_command_stream.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/os_interface/linux/drm_engine_mapper.h"
#include "shared/source/os_interface/linux/drm_memory_manager.h"
2020-05-08 16:04:06 +08:00
#include "shared/source/os_interface/linux/drm_memory_operations_handler.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/os_interface/linux/drm_neo.h"
2022-05-18 01:16:13 +08:00
#include "shared/source/os_interface/linux/drm_wrappers.h"
2020-02-24 05:44:01 +08:00
#include "shared/source/os_interface/linux/os_context_linux.h"
2021-05-21 07:17:57 +08:00
#include "shared/source/os_interface/os_interface.h"
2020-02-24 17:22:30 +08:00
2017-12-21 07:45:38 +08:00
#include <cstdlib>
#include <cstring>
2019-03-26 18:59:46 +08:00
namespace NEO {
2017-12-21 07:45:38 +08:00
template <typename GfxFamily>
2020-10-29 22:33:35 +08:00
DrmCommandStreamReceiver<GfxFamily>::DrmCommandStreamReceiver(ExecutionEnvironment &executionEnvironment,
uint32_t rootDeviceIndex,
const DeviceBitfield deviceBitfield,
gemCloseWorkerMode mode)
2020-10-28 23:08:37 +08:00
: BaseClass(executionEnvironment, rootDeviceIndex, deviceBitfield), gemCloseWorkerOperationMode(mode) {
2018-09-17 21:42:15 +08:00
2022-04-20 19:26:46 +08:00
this->completionFenceOffset = Drm::completionFenceOffset;
2020-05-15 19:52:11 +08:00
auto rootDeviceEnvironment = executionEnvironment.rootDeviceEnvironments[rootDeviceIndex].get();
2021-05-21 07:17:57 +08:00
this->drm = rootDeviceEnvironment->osInterface->getDriverModel()->as<Drm>();
2018-08-30 15:27:47 +08:00
residency.reserve(512);
execObjectsStorage.reserve(512);
2020-05-15 19:52:11 +08:00
2021-06-28 21:10:21 +08:00
if (this->drm->isVmBindAvailable()) {
gemCloseWorkerOperationMode = gemCloseWorkerMode::gemCloseWorkerInactive;
}
if (DebugManager.flags.EnableGemCloseWorker.get() != -1) {
gemCloseWorkerOperationMode = DebugManager.flags.EnableGemCloseWorker.get() ? gemCloseWorkerMode::gemCloseWorkerActive : gemCloseWorkerMode::gemCloseWorkerInactive;
}
2020-05-15 19:52:11 +08:00
auto hwInfo = rootDeviceEnvironment->getHardwareInfo();
auto localMemoryEnabled = HwHelper::get(hwInfo->platform.eRenderCoreFamily).getEnableLocalMemory(*hwInfo);
2021-12-20 22:34:39 +08:00
this->dispatchMode = localMemoryEnabled ? DispatchMode::BatchedDispatch : DispatchMode::ImmediateDispatch;
2020-05-15 19:52:11 +08:00
2022-01-21 05:20:56 +08:00
if (ApiSpecificConfig::getApiType() == ApiSpecificConfig::L0) {
this->dispatchMode = DispatchMode::ImmediateDispatch;
}
2020-05-15 19:52:11 +08:00
if (DebugManager.flags.CsrDispatchMode.get()) {
this->dispatchMode = static_cast<DispatchMode>(DebugManager.flags.CsrDispatchMode.get());
}
2021-06-15 19:31:12 +08:00
int overrideUserFenceForCompletionWait = DebugManager.flags.EnableUserFenceForCompletionWait.get();
if (overrideUserFenceForCompletionWait != -1) {
useUserFenceWait = !!(overrideUserFenceForCompletionWait);
2021-06-04 20:23:20 +08:00
}
2021-06-15 19:31:12 +08:00
int overrideUserFenceUseCtxId = DebugManager.flags.EnableUserFenceUseCtxId.get();
if (overrideUserFenceUseCtxId != -1) {
useContextForUserFenceWait = !!(overrideUserFenceUseCtxId);
2021-06-04 20:23:20 +08:00
}
2021-06-17 19:55:28 +08:00
useNotifyEnableForPostSync = useUserFenceWait;
2021-06-22 23:44:29 +08:00
int overrideUseNotifyEnableForPostSync = DebugManager.flags.OverrideNotifyEnableForTagUpdatePostSync.get();
if (overrideUseNotifyEnableForPostSync != -1) {
useNotifyEnableForPostSync = !!(overrideUseNotifyEnableForPostSync);
}
2021-06-18 09:11:36 +08:00
kmdWaitTimeout = DebugManager.flags.SetKmdWaitTimeout.get();
2017-12-21 07:45:38 +08:00
}
2022-03-25 21:00:53 +08:00
template <typename GfxFamily>
inline DrmCommandStreamReceiver<GfxFamily>::~DrmCommandStreamReceiver() {
if (this->isUpdateTagFromWaitEnabled()) {
this->waitForCompletionWithTimeout(WaitParams{false, false, 0}, this->peekTaskCount());
}
}
2017-12-21 07:45:38 +08:00
template <typename GfxFamily>
2022-01-07 22:53:31 +08:00
SubmissionStatus DrmCommandStreamReceiver<GfxFamily>::flush(BatchBuffer &batchBuffer, ResidencyContainer &allocationsForResidency) {
2020-04-30 19:24:34 +08:00
this->printDeviceIndex();
2017-12-21 07:45:38 +08:00
DrmAllocation *alloc = static_cast<DrmAllocation *>(batchBuffer.commandBufferAllocation);
DEBUG_BREAK_IF(!alloc);
BufferObject *bb = alloc->getBO();
2019-09-23 22:30:31 +08:00
if (bb == nullptr) {
2022-01-07 22:53:31 +08:00
return SubmissionStatus::OUT_OF_MEMORY;
2019-09-23 22:30:31 +08:00
}
2017-12-21 07:45:38 +08:00
2019-08-21 18:50:47 +08:00
if (this->lastSentSliceCount != batchBuffer.sliceCount) {
if (drm->setQueueSliceCount(batchBuffer.sliceCount)) {
this->lastSentSliceCount = batchBuffer.sliceCount;
}
}
2020-05-25 15:02:14 +08:00
auto memoryOperationsInterface = static_cast<DrmMemoryOperationsHandler *>(this->executionEnvironment.rootDeviceEnvironments[this->rootDeviceIndex]->memoryOperationsInterface.get());
2020-11-27 20:02:43 +08:00
std::unique_lock<std::mutex> lock;
if (!this->directSubmission.get() && !this->blitterDirectSubmission.get()) {
lock = memoryOperationsInterface->lockHandlerIfUsed();
}
2022-11-15 19:21:56 +08:00
auto submissionStatus = this->printBOsForSubmit(allocationsForResidency, *batchBuffer.commandBufferAllocation);
if (submissionStatus != SubmissionStatus::SUCCESS) {
return submissionStatus;
}
2021-02-19 23:23:35 +08:00
2022-09-30 02:31:22 +08:00
if (this->drm->isVmBindAvailable()) {
allocationsForResidency.push_back(batchBuffer.commandBufferAllocation);
}
2022-01-07 22:53:31 +08:00
MemoryOperationsStatus retVal = memoryOperationsInterface->mergeWithResidencyContainer(this->osContext, allocationsForResidency);
if (retVal != MemoryOperationsStatus::SUCCESS) {
if (retVal == MemoryOperationsStatus::OUT_OF_MEMORY) {
return SubmissionStatus::OUT_OF_MEMORY;
}
return SubmissionStatus::FAILED;
}
2020-05-08 16:04:06 +08:00
2021-04-29 16:58:16 +08:00
if (this->directSubmission.get()) {
2021-10-29 19:54:52 +08:00
this->startControllingDirectSubmissions();
2022-01-07 22:53:31 +08:00
bool ret = this->directSubmission->dispatchCommandBuffer(batchBuffer, *this->flushStamp.get());
if (ret == false) {
return SubmissionStatus::FAILED;
}
return SubmissionStatus::SUCCESS;
2020-07-17 17:28:59 +08:00
}
2020-10-19 21:36:57 +08:00
if (this->blitterDirectSubmission.get()) {
2021-10-29 19:54:52 +08:00
this->startControllingDirectSubmissions();
2022-01-07 22:53:31 +08:00
bool ret = this->blitterDirectSubmission->dispatchCommandBuffer(batchBuffer, *this->flushStamp.get());
if (ret == false) {
return SubmissionStatus::FAILED;
}
return SubmissionStatus::SUCCESS;
2020-10-19 21:36:57 +08:00
}
2020-07-17 17:28:59 +08:00
2021-06-18 09:11:36 +08:00
if (isUserFenceWaitActive()) {
2021-08-09 17:52:54 +08:00
this->flushStamp->setStamp(latestSentTaskCount);
2021-06-04 20:23:20 +08:00
} else {
this->flushStamp->setStamp(bb->peekHandle());
}
2022-03-29 19:51:44 +08:00
auto readBackMode = DebugManager.flags.ReadBackCommandBufferAllocation.get();
bool readBackAllowed = ((batchBuffer.commandBufferAllocation->isAllocatedInLocalMemoryPool() && readBackMode == 1) || readBackMode == 2);
if (readBackAllowed) {
readBackAllocation(ptrOffset(batchBuffer.commandBufferAllocation->getUnderlyingBuffer(), batchBuffer.startOffset));
}
2021-10-12 09:32:45 +08:00
auto ret = this->flushInternal(batchBuffer, allocationsForResidency);
2018-05-11 15:57:59 +08:00
2019-10-31 03:05:41 +08:00
if (this->gemCloseWorkerOperationMode == gemCloseWorkerMode::gemCloseWorkerActive) {
2019-09-23 22:30:31 +08:00
bb->reference();
this->getMemoryManager()->peekGemCloseWorker()->push(bb);
2017-12-21 07:45:38 +08:00
}
2022-09-13 22:26:03 +08:00
return ret;
2017-12-21 07:45:38 +08:00
}
2022-03-29 19:51:44 +08:00
template <typename GfxFamily>
void DrmCommandStreamReceiver<GfxFamily>::readBackAllocation(void *source) {
reserved = *reinterpret_cast<volatile uint32_t *>(source);
}
2021-02-19 23:23:35 +08:00
template <typename GfxFamily>
2022-11-15 19:21:56 +08:00
SubmissionStatus DrmCommandStreamReceiver<GfxFamily>::printBOsForSubmit(ResidencyContainer &allocationsForResidency, GraphicsAllocation &cmdBufferAllocation) {
2021-02-19 23:23:35 +08:00
if (DebugManager.flags.PrintBOsForSubmit.get()) {
std::vector<BufferObject *> bosForSubmit;
for (auto drmIterator = 0u; drmIterator < osContext->getDeviceBitfield().size(); drmIterator++) {
if (osContext->getDeviceBitfield().test(drmIterator)) {
for (auto gfxAllocation = allocationsForResidency.begin(); gfxAllocation != allocationsForResidency.end(); gfxAllocation++) {
auto drmAllocation = static_cast<DrmAllocation *>(*gfxAllocation);
2022-11-15 19:21:56 +08:00
auto retCode = drmAllocation->makeBOsResident(osContext, drmIterator, &bosForSubmit, true);
if (retCode) {
return Drm::getSubmissionStatusFromReturnCode(retCode);
}
2021-02-19 23:23:35 +08:00
}
auto drmCmdBufferAllocation = static_cast<DrmAllocation *>(&cmdBufferAllocation);
2022-11-15 19:21:56 +08:00
auto retCode = drmCmdBufferAllocation->makeBOsResident(osContext, drmIterator, &bosForSubmit, true);
if (retCode) {
return Drm::getSubmissionStatusFromReturnCode(retCode);
}
2021-02-19 23:23:35 +08:00
}
}
printf("Buffer object for submit\n");
for (const auto &bo : bosForSubmit) {
2021-02-24 04:12:14 +08:00
printf("BO-%d, range: %" SCNx64 " - %" SCNx64 ", size: %" SCNdPTR "\n", bo->peekHandle(), bo->peekAddress(), ptrOffset(bo->peekAddress(), bo->peekSize()), bo->peekSize());
2021-02-19 23:23:35 +08:00
}
printf("\n");
}
2022-11-15 19:21:56 +08:00
return SubmissionStatus::SUCCESS;
2021-02-19 23:23:35 +08:00
}
2019-09-23 22:30:31 +08:00
template <typename GfxFamily>
2022-01-21 02:13:07 +08:00
int DrmCommandStreamReceiver<GfxFamily>::exec(const BatchBuffer &batchBuffer, uint32_t vmHandleId, uint32_t drmContextId, uint32_t index) {
2019-09-23 22:30:31 +08:00
DrmAllocation *alloc = static_cast<DrmAllocation *>(batchBuffer.commandBufferAllocation);
DEBUG_BREAK_IF(!alloc);
BufferObject *bb = alloc->getBO();
DEBUG_BREAK_IF(!bb);
2022-06-15 00:47:02 +08:00
auto osContextLinux = static_cast<OsContextLinux *>(this->osContext);
auto execFlags = osContextLinux->getEngineFlag() | drm->getIoctlHelper()->getDrmParamValue(DrmParam::ExecNoReloc);
2019-09-23 22:30:31 +08:00
2022-09-30 02:31:22 +08:00
// requiredSize determinant:
// * vmBind UNAVAILABLE => residency holds all allocations except for the command buffer
// * vmBind AVAILABLE => residency holds command buffer as well
2019-09-23 22:30:31 +08:00
auto requiredSize = this->residency.size() + 1;
if (requiredSize > this->execObjectsStorage.size()) {
this->execObjectsStorage.resize(requiredSize);
}
2022-01-20 02:14:10 +08:00
uint64_t completionGpuAddress = 0;
2022-11-22 21:53:59 +08:00
TaskCountType completionValue = 0;
2022-09-30 02:31:22 +08:00
if (this->drm->isVmBindAvailable() && this->drm->completionFenceSupport()) {
2022-01-21 02:13:07 +08:00
completionGpuAddress = getTagAllocation()->getGpuAddress() + (index * this->postSyncWriteOffset) + Drm::completionFenceOffset;
2022-01-20 02:14:10 +08:00
completionValue = this->latestSentTaskCount;
}
2021-10-12 09:32:45 +08:00
int ret = bb->exec(static_cast<uint32_t>(alignUp(batchBuffer.usedSize - batchBuffer.startOffset, 8)),
2020-05-21 18:21:13 +08:00
batchBuffer.startOffset, execFlags,
2019-09-23 22:30:31 +08:00
batchBuffer.requiresCoherency,
2020-08-11 20:00:41 +08:00
this->osContext,
2020-07-31 13:45:48 +08:00
vmHandleId,
2019-09-23 22:30:31 +08:00
drmContextId,
2020-05-06 18:37:15 +08:00
this->residency.data(), this->residency.size(),
2022-01-20 02:14:10 +08:00
this->execObjectsStorage.data(),
completionGpuAddress,
completionValue);
2019-09-23 22:30:31 +08:00
this->residency.clear();
2021-10-12 09:32:45 +08:00
return ret;
2019-09-23 22:30:31 +08:00
}
2017-12-21 07:45:38 +08:00
template <typename GfxFamily>
2022-11-15 18:04:19 +08:00
SubmissionStatus DrmCommandStreamReceiver<GfxFamily>::processResidency(const ResidencyContainer &inputAllocationsForResidency, uint32_t handleId) {
2022-09-30 02:31:22 +08:00
if (drm->isVmBindAvailable()) {
2022-11-15 18:04:19 +08:00
return SubmissionStatus::SUCCESS;
2022-09-30 02:31:22 +08:00
}
int ret = 0;
for (auto &alloc : inputAllocationsForResidency) {
auto drmAlloc = static_cast<DrmAllocation *>(alloc);
ret = drmAlloc->makeBOsResident(osContext, handleId, &this->residency, false);
if (ret != 0) {
break;
2022-05-27 11:58:07 +08:00
}
2017-12-21 07:45:38 +08:00
}
2022-09-30 02:31:22 +08:00
2022-11-15 18:04:19 +08:00
return Drm::getSubmissionStatusFromReturnCode(ret);
2017-12-21 07:45:38 +08:00
}
template <typename GfxFamily>
void DrmCommandStreamReceiver<GfxFamily>::makeNonResident(GraphicsAllocation &gfxAllocation) {
// Vector is moved to command buffer inside flush.
// If flush wasn't called we need to make all objects non-resident.
// If makeNonResident is called before flush, vector will be cleared.
2018-12-03 17:05:36 +08:00
if (gfxAllocation.isResident(this->osContext->getContextId())) {
2017-12-21 07:45:38 +08:00
if (this->residency.size() != 0) {
this->residency.clear();
}
2019-07-29 22:11:51 +08:00
for (auto fragmentId = 0u; fragmentId < gfxAllocation.fragmentsStorage.fragmentCount; fragmentId++) {
gfxAllocation.fragmentsStorage.fragmentStorageData[fragmentId].residency->resident[osContext->getContextId()] = false;
2017-12-21 07:45:38 +08:00
}
}
2022-01-25 21:24:45 +08:00
if (!gfxAllocation.isAlwaysResident(this->osContext->getContextId())) {
gfxAllocation.releaseResidencyInOsContext(this->osContext->getContextId());
}
2017-12-21 07:45:38 +08:00
}
template <typename GfxFamily>
2019-07-29 22:11:51 +08:00
DrmMemoryManager *DrmCommandStreamReceiver<GfxFamily>::getMemoryManager() const {
return static_cast<DrmMemoryManager *>(CommandStreamReceiver::getMemoryManager());
2017-12-21 07:45:38 +08:00
}
2019-11-07 01:14:30 +08:00
template <typename GfxFamily>
GmmPageTableMngr *DrmCommandStreamReceiver<GfxFamily>::createPageTableManager() {
2021-09-08 07:30:06 +08:00
auto rootDeviceEnvironment = this->executionEnvironment.rootDeviceEnvironments[this->rootDeviceIndex].get();
auto gmmClientContext = rootDeviceEnvironment->getGmmClientContext();
GMM_DEVICE_INFO deviceInfo{};
GMM_DEVICE_CALLBACKS_INT deviceCallbacks{};
deviceInfo.pDeviceCb = &deviceCallbacks;
gmmClientContext->setGmmDeviceInfo(&deviceInfo);
auto gmmPageTableMngr = GmmPageTableMngr::create(gmmClientContext, TT_TYPE::AUXTT, nullptr);
2019-11-07 01:14:30 +08:00
gmmPageTableMngr->setCsrHandle(this);
2021-09-08 07:30:06 +08:00
2021-09-29 23:59:41 +08:00
this->pageTableManager.reset(gmmPageTableMngr);
2021-09-08 07:30:06 +08:00
2019-11-07 01:14:30 +08:00
return gmmPageTableMngr;
}
2017-12-21 07:45:38 +08:00
template <typename GfxFamily>
2021-09-17 21:05:26 +08:00
bool DrmCommandStreamReceiver<GfxFamily>::waitForFlushStamp(FlushStamp &flushStamp) {
2021-06-04 20:23:20 +08:00
auto waitValue = static_cast<uint32_t>(flushStamp);
2021-06-18 09:11:36 +08:00
if (isUserFenceWaitActive()) {
2021-09-17 21:05:26 +08:00
waitUserFence(waitValue);
2021-06-04 20:23:20 +08:00
} else {
2021-06-18 09:11:36 +08:00
this->drm->waitHandle(waitValue, kmdWaitTimeout);
2021-06-04 20:23:20 +08:00
}
2017-12-21 07:45:38 +08:00
return true;
}
2021-02-17 18:33:41 +08:00
template <typename GfxFamily>
2021-06-15 19:31:12 +08:00
bool DrmCommandStreamReceiver<GfxFamily>::isKmdWaitModeActive() {
if (this->drm->isVmBindAvailable()) {
2021-06-18 09:11:36 +08:00
return useUserFenceWait;
2021-06-15 19:31:12 +08:00
}
return true;
2021-02-17 18:33:41 +08:00
}
2021-06-18 09:11:36 +08:00
template <typename GfxFamily>
inline bool DrmCommandStreamReceiver<GfxFamily>::isUserFenceWaitActive() {
return (this->drm->isVmBindAvailable() && useUserFenceWait);
}
2019-03-26 18:59:46 +08:00
} // namespace NEO