From b72fcad1bec28617a4c6ab8f25e40f8c9d8723cd Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Tue, 28 Jun 2022 17:56:14 +0000 Subject: [PATCH] Drm: call ioctl using ioctl helper if possible Related-To: NEO-6999 Signed-off-by: Mateusz Jablonski --- .../source/debug/linux/prelim/drm_helper.cpp | 2 +- .../os_interface/linux/device_time_drm.cpp | 18 +++++---- .../os_interface/linux/drm_buffer_object.cpp | 6 ++- .../os_interface/linux/drm_memory_manager.cpp | 39 ++++++++++++------- shared/source/os_interface/linux/drm_neo.cpp | 34 ++++++++-------- .../linux/device_command_stream_fixture.h | 8 +++- 6 files changed, 64 insertions(+), 43 deletions(-) diff --git a/level_zero/tools/source/debug/linux/prelim/drm_helper.cpp b/level_zero/tools/source/debug/linux/prelim/drm_helper.cpp index 9fa375e562..27d30afa4c 100644 --- a/level_zero/tools/source/debug/linux/prelim/drm_helper.cpp +++ b/level_zero/tools/source/debug/linux/prelim/drm_helper.cpp @@ -15,7 +15,7 @@ namespace L0 { int DrmHelper::ioctl(Device *device, NEO::DrmIoctl request, void *arg) { auto drm = device->getOsInterface().getDriverModel()->as(); - return drm->ioctl(request, arg); + return drm->getIoctlHelper()->ioctl(drm, request, arg); } int DrmHelper::getErrno(Device *device) { diff --git a/shared/source/os_interface/linux/device_time_drm.cpp b/shared/source/os_interface/linux/device_time_drm.cpp index bfad2452be..5f47f2dd7e 100644 --- a/shared/source/os_interface/linux/device_time_drm.cpp +++ b/shared/source/os_interface/linux/device_time_drm.cpp @@ -31,10 +31,11 @@ void DeviceTimeDrm::timestampTypeDetect() { return; reg.offset = (REG_GLOBAL_TIMESTAMP_LDW | 1); - err = pDrm->ioctl(DrmIoctl::RegRead, ®); + auto ioctlHelper = pDrm->getIoctlHelper(); + err = ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®); if (err) { reg.offset = REG_GLOBAL_TIMESTAMP_UN; - err = pDrm->ioctl(DrmIoctl::RegRead, ®); + err = ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®); if (err) { getGpuTime = &DeviceTimeDrm::getGpuTime32; } else { @@ -50,7 +51,8 @@ bool DeviceTimeDrm::getGpuTime32(uint64_t *timestamp) { reg.offset = REG_GLOBAL_TIMESTAMP_LDW; - if (pDrm->ioctl(DrmIoctl::RegRead, ®)) { + auto ioctlHelper = pDrm->getIoctlHelper(); + if (ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®)) { return false; } *timestamp = reg.value >> 32; @@ -62,7 +64,8 @@ bool DeviceTimeDrm::getGpuTime36(uint64_t *timestamp) { reg.offset = REG_GLOBAL_TIMESTAMP_LDW | 1; - if (pDrm->ioctl(DrmIoctl::RegRead, ®)) { + auto ioctlHelper = pDrm->getIoctlHelper(); + if (ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®)) { return false; } *timestamp = reg.value; @@ -78,11 +81,12 @@ bool DeviceTimeDrm::getGpuTimeSplitted(uint64_t *timestamp) { regHi.offset = REG_GLOBAL_TIMESTAMP_UN; regLo.offset = REG_GLOBAL_TIMESTAMP_LDW; - err += pDrm->ioctl(DrmIoctl::RegRead, ®Hi); + auto ioctlHelper = pDrm->getIoctlHelper(); + err += ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®Hi); do { tmpHi = regHi.value; - err += pDrm->ioctl(DrmIoctl::RegRead, ®Lo); - err += pDrm->ioctl(DrmIoctl::RegRead, ®Hi); + err += ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®Lo); + err += ioctlHelper->ioctl(pDrm, DrmIoctl::RegRead, ®Hi); } while (err == 0 && regHi.value != tmpHi && --loop); if (err) { diff --git a/shared/source/os_interface/linux/drm_buffer_object.cpp b/shared/source/os_interface/linux/drm_buffer_object.cpp index 12c1851961..81070e633e 100644 --- a/shared/source/os_interface/linux/drm_buffer_object.cpp +++ b/shared/source/os_interface/linux/drm_buffer_object.cpp @@ -68,7 +68,8 @@ bool BufferObject::close() { PRINT_DEBUG_STRING(DebugManager.flags.PrintBOCreateDestroyResult.get(), stdout, "Calling gem close on handle: BO-%d\n", this->handle); - int ret = this->drm->ioctl(DrmIoctl::GemClose, &close); + auto ioctlHelper = this->drm->getIoctlHelper(); + int ret = ioctlHelper->ioctl(drm, DrmIoctl::GemClose, &close); if (ret != 0) { int err = errno; PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(GEM_CLOSE) failed with %d. errno=%d(%s)\n", ret, err, strerror(err)); @@ -101,8 +102,9 @@ bool BufferObject::setTiling(uint32_t mode, uint32_t stride) { setTiling.handle = this->handle; setTiling.tilingMode = mode; setTiling.stride = stride; + auto ioctlHelper = this->drm->getIoctlHelper(); - if (this->drm->ioctl(DrmIoctl::GemSetTiling, &setTiling) != 0) { + if (ioctlHelper->ioctl(drm, DrmIoctl::GemSetTiling, &setTiling) != 0) { return false; } diff --git a/shared/source/os_interface/linux/drm_memory_manager.cpp b/shared/source/os_interface/linux/drm_memory_manager.cpp index 2549aec9ba..9807496a14 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.cpp +++ b/shared/source/os_interface/linux/drm_memory_manager.cpp @@ -246,8 +246,9 @@ NEO::BufferObject *DrmMemoryManager::allocUserptr(uintptr_t address, size_t size userptr.userSize = size; auto &drm = this->getDrm(rootDeviceIndex); + auto ioctlHelper = drm.getIoctlHelper(); - if (drm.ioctl(DrmIoctl::GemUserptr, &userptr) != 0) { + if (ioctlHelper->ioctl(&drm, DrmIoctl::GemUserptr, &userptr) != 0) { return nullptr; } @@ -520,8 +521,9 @@ GraphicsAllocation *DrmMemoryManager::allocateMemoryByKMD(const AllocationData & create.size = bufferSize; auto &drm = getDrm(allocationData.rootDeviceIndex); + auto ioctlHelper = drm.getIoctlHelper(); - [[maybe_unused]] auto ret = drm.ioctl(DrmIoctl::GemCreate, &create); + [[maybe_unused]] auto ret = ioctlHelper->ioctl(&drm, DrmIoctl::GemCreate, &create); DEBUG_BREAK_IF(ret != 0); auto patIndex = drm.getPatIndex(gmm.get(), allocationData.type, CacheRegion::Default, CachePolicy::WriteBack, false); @@ -553,8 +555,9 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImageImpl(const A create.size = allocationData.imgInfo->size; auto &drm = this->getDrm(allocationData.rootDeviceIndex); + auto ioctlHelper = drm.getIoctlHelper(); - [[maybe_unused]] auto ret = drm.ioctl(DrmIoctl::GemCreate, &create); + [[maybe_unused]] auto ret = ioctlHelper->ioctl(&drm, DrmIoctl::GemCreate, &create); DEBUG_BREAK_IF(ret != 0); auto patIndex = drm.getPatIndex(gmm.get(), allocationData.type, CacheRegion::Default, CachePolicy::WriteBack, false); @@ -565,7 +568,6 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImageImpl(const A } bo->setAddress(gpuRange); - auto ioctlHelper = drm.getIoctlHelper(); [[maybe_unused]] auto ret2 = bo->setTiling(ioctlHelper->getDrmParamValue(DrmParam::TilingY), static_cast(allocationData.imgInfo->rowPitch)); DEBUG_BREAK_IF(ret2 != true); @@ -682,12 +684,13 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromMultipleShared auto &drm = this->getDrm(properties.rootDeviceIndex); bool areBosSharedObjects = true; + auto ioctlHelper = drm.getIoctlHelper(); for (auto handle : handles) { PrimeHandle openFd = {0, 0, 0}; openFd.fileDescriptor = handle; - auto ret = this->getDrm(properties.rootDeviceIndex).ioctl(DrmIoctl::PrimeFdToHandle, &openFd); + auto ret = ioctlHelper->ioctl(&drm, DrmIoctl::PrimeFdToHandle, &openFd); if (ret != 0) { [[maybe_unused]] int err = errno; @@ -772,8 +775,9 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o openFd.fileDescriptor = handle; auto &drm = this->getDrm(properties.rootDeviceIndex); + auto ioctlHelper = drm.getIoctlHelper(); - auto ret = drm.ioctl(DrmIoctl::PrimeFdToHandle, &openFd); + auto ret = ioctlHelper->ioctl(&drm, DrmIoctl::PrimeFdToHandle, &openFd); if (ret != 0) { [[maybe_unused]] int err = errno; @@ -825,7 +829,8 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o if (properties.imgInfo) { GemGetTiling getTiling{}; getTiling.handle = boHandle; - ret = drm.ioctl(DrmIoctl::GemGetTiling, &getTiling); + auto ioctlHelper = drm.getIoctlHelper(); + ret = ioctlHelper->ioctl(&drm, DrmIoctl::GemGetTiling, &getTiling); if (ret == 0) { auto ioctlHelper = drm.getIoctlHelper(); @@ -1045,7 +1050,9 @@ bool DrmMemoryManager::setDomainCpu(GraphicsAllocation &graphicsAllocation, bool setDomain.readDomains = I915_GEM_DOMAIN_CPU; setDomain.writeDomain = writeEnable ? I915_GEM_DOMAIN_CPU : 0; - return getDrm(graphicsAllocation.getRootDeviceIndex()).ioctl(DrmIoctl::GemSetDomain, &setDomain) == 0; + auto &drm = this->getDrm(graphicsAllocation.getRootDeviceIndex()); + auto ioctlHelper = drm.getIoctlHelper(); + return ioctlHelper->ioctl(&drm, DrmIoctl::GemSetDomain, &setDomain) == 0; } void *DrmMemoryManager::lockResourceImpl(GraphicsAllocation &graphicsAllocation) { @@ -1076,13 +1083,15 @@ void DrmMemoryManager::unlockResourceImpl(GraphicsAllocation &graphicsAllocation return unlockBufferObject(static_cast(graphicsAllocation).getBO()); } -int DrmMemoryManager::obtainFdFromHandle(int boHandle, uint32_t rootDeviceindex) { +int DrmMemoryManager::obtainFdFromHandle(int boHandle, uint32_t rootDeviceIndex) { PrimeHandle openFd{}; openFd.flags = DRM_CLOEXEC | DRM_RDWR; openFd.handle = boHandle; - getDrm(rootDeviceindex).ioctl(DrmIoctl::PrimeHandleToFd, &openFd); + auto &drm = this->getDrm(rootDeviceIndex); + auto ioctlHelper = drm.getIoctlHelper(); + ioctlHelper->ioctl(&drm, DrmIoctl::PrimeHandleToFd, &openFd); return openFd.fileDescriptor; } @@ -1568,11 +1577,12 @@ bool DrmMemoryManager::retrieveMmapOffsetForBufferObject(uint32_t rootDeviceInde GemMmapOffset mmapOffset = {}; mmapOffset.handle = bo.peekHandle(); mmapOffset.flags = isLocalMemorySupported(rootDeviceIndex) ? mmapOffsetFixed : flags; - auto &drm = getDrm(rootDeviceIndex); - auto ret = drm.ioctl(DrmIoctl::GemMmapOffset, &mmapOffset); + auto &drm = this->getDrm(rootDeviceIndex); + auto ioctlHelper = drm.getIoctlHelper(); + auto ret = ioctlHelper->ioctl(&drm, DrmIoctl::GemMmapOffset, &mmapOffset); if (ret != 0 && isLocalMemorySupported(rootDeviceIndex)) { mmapOffset.flags = flags; - ret = drm.ioctl(DrmIoctl::GemMmapOffset, &mmapOffset); + ret = ioctlHelper->ioctl(&drm, DrmIoctl::GemMmapOffset, &mmapOffset); } if (ret != 0) { int err = drm.getErrno(); @@ -1818,8 +1828,9 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl auto &drm = this->getDrm(properties.rootDeviceIndex); auto patIndex = drm.getPatIndex(nullptr, properties.allocationType, CacheRegion::Default, CachePolicy::WriteBack, false); + auto ioctlHelper = drm.getIoctlHelper(); - auto ret = drm.ioctl(DrmIoctl::PrimeFdToHandle, &openFd); + auto ret = ioctlHelper->ioctl(&drm, DrmIoctl::PrimeFdToHandle, &openFd); if (ret != 0) { int err = drm.getErrno(); PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(PRIME_FD_TO_HANDLE) failed with %d. errno=%d(%s)\n", ret, err, strerror(err)); diff --git a/shared/source/os_interface/linux/drm_neo.cpp b/shared/source/os_interface/linux/drm_neo.cpp index 9771eedcac..6acf329018 100644 --- a/shared/source/os_interface/linux/drm_neo.cpp +++ b/shared/source/os_interface/linux/drm_neo.cpp @@ -119,7 +119,7 @@ int Drm::getParamIoctl(DrmParam param, int *dstValue) { getParam.param = getDrmParamValue(param, ioctlHelper.get()); getParam.value = dstValue; - int retVal = ioctl(DrmIoctl::Getparam, &getParam); + int retVal = ioctlHelper ? ioctlHelper->ioctl(this, DrmIoctl::Getparam, &getParam) : ioctl(DrmIoctl::Getparam, &getParam); if (DebugManager.flags.PrintIoctlEntries.get()) { printf("DRM_IOCTL_I915_GETPARAM: param: %s, output value: %d, retCode:% d\n", getDrmParamString(param).c_str(), @@ -152,7 +152,7 @@ int Drm::enableTurboBoost() { contextParam.param = I915_CONTEXT_PRIVATE_PARAM_BOOST; contextParam.value = 1; - return ioctl(DrmIoctl::GemContextSetparam, &contextParam); + return ioctlHelper->ioctl(this, DrmIoctl::GemContextSetparam, &contextParam); } int Drm::getEnabledPooledEu(int &enabled) { @@ -175,7 +175,7 @@ int Drm::queryGttSize(uint64_t >tSizeOutput) { GemContextParam contextParam = {0}; contextParam.param = I915_CONTEXT_PARAM_GTT_SIZE; - int ret = ioctl(DrmIoctl::GemContextGetparam, &contextParam); + int ret = ioctlHelper->ioctl(this, DrmIoctl::GemContextGetparam, &contextParam); if (ret == 0) { gttSizeOutput = contextParam.value; } @@ -191,7 +191,7 @@ bool Drm::isGpuHangDetected(OsContext &osContext) { ResetStats resetStats{}; resetStats.contextId = drmContextId; - const auto retVal{ioctl(DrmIoctl::GetResetStats, &resetStats)}; + const auto retVal{ioctlHelper->ioctl(this, DrmIoctl::GetResetStats, &resetStats)}; UNRECOVERABLE_IF(retVal != 0); if (resetStats.batchActive > 0 || resetStats.batchPending > 0) { @@ -219,7 +219,7 @@ void Drm::setLowPriorityContextParam(uint32_t drmContextId) { gcp.param = I915_CONTEXT_PARAM_PRIORITY; gcp.value = -1023; - auto retVal = ioctl(DrmIoctl::GemContextSetparam, &gcp); + auto retVal = ioctlHelper->ioctl(this, DrmIoctl::GemContextSetparam, &gcp); UNRECOVERABLE_IF(retVal != 0); } @@ -231,7 +231,7 @@ int Drm::getQueueSliceCount(GemContextParamSseu *sseu) { contextParam.value = reinterpret_cast(sseu); contextParam.size = sizeof(struct GemContextParamSseu); - return ioctl(DrmIoctl::GemContextGetparam, &contextParam); + return ioctlHelper->ioctl(this, DrmIoctl::GemContextGetparam, &contextParam); } uint64_t Drm::getSliceMask(uint64_t sliceCount) { @@ -246,7 +246,7 @@ bool Drm::setQueueSliceCount(uint64_t sliceCount) { contextParam.contextId = 0; contextParam.value = reinterpret_cast(&sseu); contextParam.size = sizeof(struct GemContextParamSseu); - int retVal = ioctl(DrmIoctl::GemContextSetparam, &contextParam); + int retVal = ioctlHelper->ioctl(this, DrmIoctl::GemContextSetparam, &contextParam); if (retVal == 0) { return true; } @@ -258,7 +258,7 @@ void Drm::checkNonPersistentContextsSupport() { GemContextParam contextParam = {}; contextParam.param = I915_CONTEXT_PARAM_PERSISTENCE; - auto retVal = ioctl(DrmIoctl::GemContextGetparam, &contextParam); + auto retVal = ioctlHelper->ioctl(this, DrmIoctl::GemContextGetparam, &contextParam); if (retVal == 0 && contextParam.value == 1) { nonPersistentContextsSupported = true; } else { @@ -271,7 +271,7 @@ void Drm::setNonPersistentContext(uint32_t drmContextId) { contextParam.contextId = drmContextId; contextParam.param = I915_CONTEXT_PARAM_PERSISTENCE; - ioctl(DrmIoctl::GemContextSetparam, &contextParam); + ioctlHelper->ioctl(this, DrmIoctl::GemContextSetparam, &contextParam); } void Drm::setUnrecoverableContext(uint32_t drmContextId) { @@ -281,7 +281,7 @@ void Drm::setUnrecoverableContext(uint32_t drmContextId) { contextParam.value = 0; contextParam.size = 0; - ioctl(DrmIoctl::GemContextSetparam, &contextParam); + ioctlHelper->ioctl(this, DrmIoctl::GemContextSetparam, &contextParam); } uint32_t Drm::createDrmContext(uint32_t drmVmId, bool isDirectSubmissionRequested, bool isCooperativeContextRequested) { @@ -314,7 +314,7 @@ uint32_t Drm::createDrmContext(uint32_t drmVmId, bool isDirectSubmissionRequeste if (isCooperativeContextRequested) { return ioctlHelper->createCooperativeContext(this, gcc); } - auto ioctlResult = ioctl(DrmIoctl::GemContextCreateExt, &gcc); + auto ioctlResult = ioctlHelper->ioctl(this, DrmIoctl::GemContextCreateExt, &gcc); UNRECOVERABLE_IF(ioctlResult != 0); return gcc.contextId; @@ -323,7 +323,7 @@ uint32_t Drm::createDrmContext(uint32_t drmVmId, bool isDirectSubmissionRequeste void Drm::destroyDrmContext(uint32_t drmContextId) { GemContextDestroy destroy{}; destroy.contextId = drmContextId; - auto retVal = ioctl(DrmIoctl::GemContextDestroy, &destroy); + auto retVal = ioctlHelper->ioctl(this, DrmIoctl::GemContextDestroy, &destroy); UNRECOVERABLE_IF(retVal != 0); } @@ -339,7 +339,7 @@ int Drm::queryVmId(uint32_t drmContextId, uint32_t &vmId) { param.contextId = drmContextId; param.value = 0; param.param = I915_CONTEXT_PARAM_VM; - auto retVal = this->ioctl(DrmIoctl::GemContextGetparam, ¶m); + auto retVal = ioctlHelper->ioctl(this, DrmIoctl::GemContextGetparam, ¶m); vmId = static_cast(param.value); @@ -538,7 +538,7 @@ std::vector Drm::query(uint32_t queryId, uint32_t queryItemFlags) { query.itemsPtr = reinterpret_cast(&queryItem); query.numItems = 1; - auto ret = this->ioctl(DrmIoctl::Query, &query); + auto ret = ioctlHelper->ioctl(this, DrmIoctl::Query, &query); if (ret != 0 || queryItem.length <= 0) { return {}; } @@ -546,7 +546,7 @@ std::vector Drm::query(uint32_t queryId, uint32_t queryItemFlags) { auto data = std::vector(queryItem.length, 0); queryItem.dataPtr = castToUint64(data.data()); - ret = this->ioctl(DrmIoctl::Query, &query); + ret = ioctlHelper->ioctl(this, DrmIoctl::Query, &query); if (ret != 0 || queryItem.length <= 0) { return {}; } @@ -734,7 +734,7 @@ int Drm::waitHandle(uint32_t waitHandle, int64_t timeout) { wait.boHandle = waitHandle; wait.timeoutNs = timeout; - int ret = ioctl(DrmIoctl::GemWait, &wait); + int ret = ioctlHelper->ioctl(this, DrmIoctl::GemWait, &wait); if (ret != 0) { int err = errno; PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(I915_GEM_WAIT) failed with %d. errno=%d(%s)\n", ret, err, strerror(err)); @@ -1178,7 +1178,7 @@ unsigned int Drm::bindDrmContext(uint32_t drmContextId, uint32_t deviceIndex, au param.param = I915_CONTEXT_PARAM_ENGINES; param.value = castToUint64(&contextEngines); - auto ioctlValue = ioctl(DrmIoctl::GemContextSetparam, ¶m); + auto ioctlValue = ioctlHelper->ioctl(this, DrmIoctl::GemContextSetparam, ¶m); UNRECOVERABLE_IF(ioctlValue != 0); retVal = static_cast(ioctlHelper->getDrmParamValue(DrmParam::ExecDefault)); diff --git a/shared/test/common/os_interface/linux/device_command_stream_fixture.h b/shared/test/common/os_interface/linux/device_command_stream_fixture.h index 8a647f597f..1e2e437cf2 100644 --- a/shared/test/common/os_interface/linux/device_command_stream_fixture.h +++ b/shared/test/common/os_interface/linux/device_command_stream_fixture.h @@ -61,14 +61,18 @@ class Ioctls { class DrmMockSuccess : public Drm { public: using Drm::setupIoctlHelper; - DrmMockSuccess(int fd, RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(fd, mockPciPath), rootDeviceEnvironment) {} + DrmMockSuccess(int fd, RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(fd, mockPciPath), rootDeviceEnvironment) { + setupIoctlHelper(NEO::defaultHwInfo->platform.eProductFamily); + } int ioctl(DrmIoctl request, void *arg) override { return 0; }; }; class DrmMockFail : public Drm { public: - DrmMockFail(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(mockFd, mockPciPath), rootDeviceEnvironment) {} + DrmMockFail(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique(mockFd, mockPciPath), rootDeviceEnvironment) { + setupIoctlHelper(NEO::defaultHwInfo->platform.eProductFamily); + } int ioctl(DrmIoctl request, void *arg) override { return -1; }; };