diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index bbeabb1cc6..0baa25ae53 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -12,6 +12,7 @@ #include "level_zero/core/source/device/device_imp.h" #include "level_zero/core/source/driver/driver_handle_imp.h" +#include "level_zero/core/source/helpers/properties_parser.h" #include "level_zero/core/source/image/image.h" #include "level_zero/core/source/memory/memory_operations_helper.h" @@ -102,50 +103,13 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, return ZE_RESULT_ERROR_DEVICE_LOST; } - bool relaxedSizeAllowed = NEO::DebugManager.flags.AllowUnrestrictedSize.get(); - if (deviceDesc->pNext) { - const ze_base_desc_t *extendedDesc = reinterpret_cast(deviceDesc->pNext); - if (extendedDesc->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC) { - const ze_external_memory_export_desc_t *externalMemoryExportDesc = - reinterpret_cast(extendedDesc); - // ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF supported by default for all - // device allocations for the purpose of IPC, so just check correct - // flag is passed. - if (externalMemoryExportDesc->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD) { - return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION; - } - } else if (extendedDesc->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_FD) { - const ze_external_memory_import_fd_t *externalMemoryImportDesc = - reinterpret_cast(extendedDesc); - if (externalMemoryImportDesc->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD) { - return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION; - } - ze_ipc_memory_flags_t flags = {}; - *ptr = this->driverHandle->importFdHandle(hDevice, flags, externalMemoryImportDesc->fd, nullptr); - if (nullptr == *ptr) { - return ZE_RESULT_ERROR_INVALID_ARGUMENT; - } - return ZE_RESULT_SUCCESS; - } else if (extendedDesc->stype == ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC) { - const ze_relaxed_allocation_limits_exp_desc_t *relaxedLimitsDesc = - reinterpret_cast(extendedDesc); - if (!(relaxedLimitsDesc->flags & ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE)) { - return ZE_RESULT_ERROR_INVALID_ARGUMENT; - } - relaxedSizeAllowed = true; - } - } + StructuresLookupTable lookupTable = {}; - if (relaxedSizeAllowed == false && - (size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().maxMemAllocSize)) { - *ptr = nullptr; - return ZE_RESULT_ERROR_UNSUPPORTED_SIZE; - } + lookupTable.relaxedSizeAllowed = NEO::DebugManager.flags.AllowUnrestrictedSize.get(); + auto parseResult = prepareL0StructuresLookupTable(lookupTable, deviceDesc->pNext); - if (relaxedSizeAllowed && - (size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize)) { - *ptr = nullptr; - return ZE_RESULT_ERROR_UNSUPPORTED_SIZE; + if (parseResult != ZE_RESULT_SUCCESS) { + return parseResult; } auto neoDevice = Device::fromHandle(hDevice)->getNEODevice(); @@ -154,6 +118,37 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield(); + if (lookupTable.isSharedHandle) { + if (lookupTable.sharedHandleType.isDMABUFHandle) { + ze_ipc_memory_flags_t flags = {}; + *ptr = this->driverHandle->importFdHandle(hDevice, flags, lookupTable.sharedHandleType.fd, nullptr); + if (nullptr == *ptr) { + return ZE_RESULT_ERROR_INVALID_ARGUMENT; + } + } else { + UNRECOVERABLE_IF(!lookupTable.sharedHandleType.isNTHandle); + *ptr = this->driverHandle->importNTHandle(hDevice, lookupTable.sharedHandleType.ntHnadle); + if (*ptr == nullptr) { + return ZE_RESULT_ERROR_INVALID_ARGUMENT; + } + } + return ZE_RESULT_SUCCESS; + } + + if (lookupTable.relaxedSizeAllowed == false && + (size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().maxMemAllocSize)) { + *ptr = nullptr; + return ZE_RESULT_ERROR_UNSUPPORTED_SIZE; + } + + if (lookupTable.relaxedSizeAllowed && + (size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().globalMemSize)) { + *ptr = nullptr; + return ZE_RESULT_ERROR_UNSUPPORTED_SIZE; + } + + deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield(); + NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY, this->driverHandle->rootDeviceIndices, deviceBitfields); unifiedMemoryProperties.allocationFlags.flags.shareable = 1u; unifiedMemoryProperties.device = neoDevice; diff --git a/level_zero/core/source/driver/driver_handle_imp.cpp b/level_zero/core/source/driver/driver_handle_imp.cpp index 033b412696..8c608584dd 100644 --- a/level_zero/core/source/driver/driver_handle_imp.cpp +++ b/level_zero/core/source/driver/driver_handle_imp.cpp @@ -462,6 +462,27 @@ NEO::GraphicsAllocation *DriverHandleImp::getPeerAllocation(Device *device, return alloc; } +void *DriverHandleImp::importNTHandle(ze_device_handle_t hDevice, void *handle) { + auto neoDevice = Device::fromHandle(hDevice)->getNEODevice(); + + auto alloc = this->getMemoryManager()->createGraphicsAllocationFromNTHandle(handle, neoDevice->getRootDeviceIndex(), NEO::GraphicsAllocation::AllocationType::SHARED_BUFFER); + + if (alloc == nullptr) { + return nullptr; + } + + NEO::SvmAllocationData allocData(neoDevice->getRootDeviceIndex()); + allocData.gpuAllocations.addAllocation(alloc); + allocData.cpuAllocation = nullptr; + allocData.size = alloc->getUnderlyingBufferSize(); + allocData.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY; + allocData.device = neoDevice; + + this->getSvmAllocsManager()->insertSVMAlloc(allocData); + + return reinterpret_cast(alloc->getGpuAddress()); +} + ze_result_t DriverHandleImp::checkMemoryAccessFromDevice(Device *device, const void *ptr) { auto allocation = svmAllocsManager->getSVMAlloc(ptr); if (allocation == nullptr) { diff --git a/level_zero/core/source/driver/driver_handle_imp.h b/level_zero/core/source/driver/driver_handle_imp.h index d9663de3ea..936899756e 100644 --- a/level_zero/core/source/driver/driver_handle_imp.h +++ b/level_zero/core/source/driver/driver_handle_imp.h @@ -35,6 +35,7 @@ struct DriverHandleImp : public DriverHandle { NEO::MemoryManager *getMemoryManager() override; void setMemoryManager(NEO::MemoryManager *memoryManager) override; MOCKABLE_VIRTUAL void *importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_flags_t flags, uint64_t handle, NEO::GraphicsAllocation **pAlloc); + MOCKABLE_VIRTUAL void *importNTHandle(ze_device_handle_t hDevice, void *handle); ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) override; NEO::SVMAllocsManager *getSvmAllocsManager() override; ze_result_t initialize(std::vector> neoDevices); diff --git a/level_zero/core/source/helpers/properties_parser.h b/level_zero/core/source/helpers/properties_parser.h index a0a4e960d8..69bf30e6f5 100644 --- a/level_zero/core/source/helpers/properties_parser.h +++ b/level_zero/core/source/helpers/properties_parser.h @@ -51,6 +51,7 @@ inline NEO::ImageDescriptor convertDescriptor(const ze_image_desc_t &imageDesc) } struct StructuresLookupTable { + bool exportMemory; bool isSharedHandle; struct SharedHandleType { bool isSupportedHandle; @@ -65,6 +66,7 @@ struct StructuresLookupTable { uint32_t planeIndex; NEO::ImageDescriptor imageDescriptor; } imageProperties; + bool relaxedSizeAllowed; }; inline ze_result_t prepareL0StructuresLookupTable(StructuresLookupTable &lookupTable, const void *desc) { @@ -101,6 +103,21 @@ inline ze_result_t prepareL0StructuresLookupTable(StructuresLookupTable &lookupT lookupTable.areImageProperties = true; lookupTable.imageProperties.isPlanarExtension = true; lookupTable.imageProperties.planeIndex = imageViewDesc->planeIndex; + } else if (extendedDesc->stype == ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC) { + const ze_relaxed_allocation_limits_exp_desc_t *relaxedLimitsDesc = + reinterpret_cast(extendedDesc); + if (!(relaxedLimitsDesc->flags & ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE)) { + return ZE_RESULT_ERROR_INVALID_ARGUMENT; + } + lookupTable.relaxedSizeAllowed = true; + } else if (extendedDesc->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC) { + const ze_external_memory_export_desc_t *externalMemoryExportDesc = + reinterpret_cast(extendedDesc); + if (externalMemoryExportDesc->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF || externalMemoryExportDesc->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32) { + lookupTable.exportMemory = true; + } else { + return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION; + } } else { return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION; } @@ -108,6 +125,10 @@ inline ze_result_t prepareL0StructuresLookupTable(StructuresLookupTable &lookupT extendedDesc = reinterpret_cast(extendedDesc->pNext); } + if (lookupTable.areImageProperties && lookupTable.exportMemory) { + return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION; + } + return ZE_RESULT_SUCCESS; } } // namespace L0 diff --git a/level_zero/core/source/image/image_hw.inl b/level_zero/core/source/image/image_hw.inl index 11e6aa8806..9c406b2444 100644 --- a/level_zero/core/source/image/image_hw.inl +++ b/level_zero/core/source/image/image_hw.inl @@ -87,7 +87,7 @@ ze_result_t ImageCoreFamily::initialize(Device *device, const ze_ if (!verifyResult) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } - allocation = device->getNEODevice()->getMemoryManager()->createGraphicsAllocationFromNTHandle(lookupTable.sharedHandleType.ntHnadle, device->getNEODevice()->getRootDeviceIndex()); + allocation = device->getNEODevice()->getMemoryManager()->createGraphicsAllocationFromNTHandle(lookupTable.sharedHandleType.ntHnadle, device->getNEODevice()->getRootDeviceIndex(), NEO::GraphicsAllocation::AllocationType::SHARED_IMAGE); } } else { NEO::AllocationProperties properties(device->getRootDeviceIndex(), true, imgInfo, NEO::GraphicsAllocation::AllocationType::IMAGE, device->getNEODevice()->getDeviceBitfield()); diff --git a/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp b/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp index 3e261c44f0..0fd00d6aec 100644 --- a/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp +++ b/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp @@ -139,6 +139,84 @@ TEST_F(DriverVersionTest, whenCallingGetDriverPropertiesRepeatedlyThenTheSameUui } } +using ImportNTHandle = Test; + +class MemoryManagerNTHandleMock : public NEO::OsAgnosticMemoryManager { + public: + MemoryManagerNTHandleMock(NEO::ExecutionEnvironment &executionEnvironment) : NEO::OsAgnosticMemoryManager(executionEnvironment) {} + + NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { + auto graphicsAllocation = createMemoryAllocation(allocType, nullptr, reinterpret_cast(1), 1, + 4096u, reinterpret_cast(handle), MemoryPool::SystemCpuInaccessible, + rootDeviceIndex, false, false, false); + graphicsAllocation->setSharedHandle(static_cast(reinterpret_cast(handle))); + graphicsAllocation->set32BitAllocation(false); + graphicsAllocation->setDefaultGmm(new Gmm(executionEnvironment.rootDeviceEnvironments[0]->getGmmClientContext(), nullptr, 1, 0, false)); + return graphicsAllocation; + } +}; + +HWTEST_F(ImportNTHandle, givenNTHandleWhenCreatingDeviceMemoryThenSuccessIsReturned) { + using RENDER_SURFACE_STATE = typename FamilyType::RENDER_SURFACE_STATE; + + ze_device_mem_alloc_desc_t devProperties = {}; + devProperties.stype = ZE_STRUCTURE_TYPE_DEVICE_MEMORY_PROPERTIES; + + uint64_t imageHandle = 0x1; + ze_external_memory_import_win32_handle_t importNTHandle = {}; + importNTHandle.handle = &imageHandle; + importNTHandle.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32; + importNTHandle.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_WIN32; + devProperties.pNext = &importNTHandle; + + NEO::MockDevice *neoDevice = nullptr; + neoDevice = NEO::MockDevice::createWithNewExecutionEnvironment(NEO::defaultHwInfo.get()); + NEO::MemoryManager *prevMemoryManager = driverHandle->getMemoryManager(); + NEO::MemoryManager *currMemoryManager = new MemoryManagerNTHandleMock(*neoDevice->executionEnvironment); + driverHandle->setMemoryManager(currMemoryManager); + neoDevice->injectMemoryManager(currMemoryManager); + + ze_result_t result = ZE_RESULT_SUCCESS; + auto device = L0::Device::create(driverHandle.get(), neoDevice, 0, false, &result); + + context->addDeviceAndSubDevices(device); + + void *ptr; + + result = context->allocDeviceMem(device, &devProperties, 100, 1, &ptr); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + + auto alloc = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); + + ASSERT_EQ(alloc->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex())->peekSharedHandle(), NEO::toOsHandle(importNTHandle.handle)); + result = context->freeMem(ptr); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + + driverHandle->setMemoryManager(prevMemoryManager); + delete device; +} + +HWTEST_F(ImportNTHandle, givenNotExistingNTHandleWhenCreatingDeviceMemoryThenErrorIsReturned) { + using RENDER_SURFACE_STATE = typename FamilyType::RENDER_SURFACE_STATE; + + ze_device_mem_alloc_desc_t devProperties = {}; + devProperties.stype = ZE_STRUCTURE_TYPE_DEVICE_MEMORY_PROPERTIES; + + uint64_t imageHandle = 0x1; + ze_external_memory_import_win32_handle_t importNTHandle = {}; + importNTHandle.handle = &imageHandle; + importNTHandle.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32; + importNTHandle.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_WIN32; + devProperties.pNext = &importNTHandle; + + void *ptr; + + auto result = context->allocDeviceMem(device, &devProperties, 100, 1, &ptr); + + EXPECT_EQ(result, ZE_RESULT_ERROR_INVALID_ARGUMENT); +} + TEST(DriverTestFamilySupport, whenInitializingDriverOnSupportedFamilyThenDriverIsCreated) { NEO::MockCompilerEnableGuard mock(true); ze_result_t returnValue; diff --git a/level_zero/core/test/unit_tests/sources/event/test_event.cpp b/level_zero/core/test/unit_tests/sources/event/test_event.cpp index c907f139d6..2315a58e60 100644 --- a/level_zero/core/test/unit_tests/sources/event/test_event.cpp +++ b/level_zero/core/test/unit_tests/sources/event/test_event.cpp @@ -34,7 +34,7 @@ class MemoryManagerEventPoolFailMock : public NEO::MemoryManager { NEO::GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override { return nullptr; } void addAllocationToHostPtrManager(NEO::GraphicsAllocation *memory) override{}; void removeAllocationFromHostPtrManager(NEO::GraphicsAllocation *memory) override{}; - NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; }; + NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { return nullptr; }; AllocationStatus populateOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override { return AllocationStatus::Success; }; void cleanOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{}; void freeGraphicsMemoryImpl(NEO::GraphicsAllocation *gfxAllocation) override{}; diff --git a/level_zero/core/test/unit_tests/sources/helper/properties_parser_tests.cpp b/level_zero/core/test/unit_tests/sources/helper/properties_parser_tests.cpp index 21fee06797..606b8f6b50 100644 --- a/level_zero/core/test/unit_tests/sources/helper/properties_parser_tests.cpp +++ b/level_zero/core/test/unit_tests/sources/helper/properties_parser_tests.cpp @@ -119,6 +119,67 @@ TEST(L0StructuresLookupTableTests, givenL0StructuresWithNTHandleWhenPrepareLooku EXPECT_EQ(l0LookupTable.sharedHandleType.ntHnadle, importNTHandle.handle); } +TEST(L0StructuresLookupTableTests, givenL0StructuresWithSupportedExportHandlesWhenPrepareLookupTableThenProperFieldsInLookupTableAreSet) { + ze_external_memory_import_win32_handle_t exportStruct = {}; + exportStruct.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC; + exportStruct.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32; + + StructuresLookupTable l0LookupTable = {}; + auto result = prepareL0StructuresLookupTable(l0LookupTable, &exportStruct); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + + EXPECT_TRUE(l0LookupTable.exportMemory); + exportStruct.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF; + + l0LookupTable = {}; + result = prepareL0StructuresLookupTable(l0LookupTable, &exportStruct); + + EXPECT_EQ(result, ZE_RESULT_SUCCESS); + + EXPECT_TRUE(l0LookupTable.exportMemory); +} + +TEST(L0StructuresLookupTableTests, givenL0StructuresWithUnsupportedExportHandlesWhenPrepareLookupTableThenUnsuppoertedErrorIsReturned) { + ze_external_memory_import_win32_handle_t exportStruct = {}; + exportStruct.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC; + exportStruct.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_D3D11_TEXTURE; + + StructuresLookupTable l0LookupTable = {}; + auto result = prepareL0StructuresLookupTable(l0LookupTable, &exportStruct); + + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION); + + EXPECT_FALSE(l0LookupTable.exportMemory); + exportStruct.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD; + + l0LookupTable = {}; + result = prepareL0StructuresLookupTable(l0LookupTable, &exportStruct); + + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION); + + EXPECT_FALSE(l0LookupTable.exportMemory); +} + +TEST(L0StructuresLookupTableTests, givenL0StructuresWithSupportedExportHandlesAndImageDescWhenPrepareLookupTableThenUnsupportedErrorIsReturned) { + ze_image_desc_t imageDesc = {}; + imageDesc.stype = ZE_STRUCTURE_TYPE_IMAGE_DESC; + + ze_external_memory_import_win32_handle_t exportStruct = {}; + exportStruct.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC; + exportStruct.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32; + + exportStruct.pNext = &imageDesc; + + StructuresLookupTable l0LookupTable = {}; + auto result = prepareL0StructuresLookupTable(l0LookupTable, &exportStruct); + + EXPECT_EQ(result, ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION); + + EXPECT_TRUE(l0LookupTable.exportMemory); + EXPECT_TRUE(l0LookupTable.areImageProperties); +} + TEST(L0StructuresLookupTableTests, givenL0StructuresWithUnsuportedOptionsWhenPrepareLookupTableThenProperFieldsInLookupTableAreSet) { uint64_t handle = 0x02; ze_external_memory_import_win32_handle_t importNTHandle = {}; diff --git a/level_zero/core/test/unit_tests/sources/image/test_image.cpp b/level_zero/core/test/unit_tests/sources/image/test_image.cpp index d2c2d04649..c975d5b5fc 100644 --- a/level_zero/core/test/unit_tests/sources/image/test_image.cpp +++ b/level_zero/core/test/unit_tests/sources/image/test_image.cpp @@ -324,7 +324,7 @@ HWTEST2_F(ImageCreate, givenOpaqueFdWhenCreatingImageThenUnsuportedErrorIsReturn ASSERT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION, ret); } -HWTEST2_F(ImageCreate, givenInvalidExensionStructWhenCreatingImageThenUnsuportedErrorIsReturned, ImageSupport) { +HWTEST2_F(ImageCreate, givenExportStructWhenCreatingImageThenUnsupportedErrorIsReturned, ImageSupport) { using RENDER_SURFACE_STATE = typename FamilyType::RENDER_SURFACE_STATE; ze_image_desc_t desc = {}; @@ -345,6 +345,7 @@ HWTEST2_F(ImageCreate, givenInvalidExensionStructWhenCreatingImageThenUnsuported ze_external_memory_export_fd_t exportFd = {}; exportFd.fd = 1; exportFd.stype = ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_DESC; + exportFd.flags = ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32; desc.pNext = &exportFd; auto imageHW = std::make_unique>>(); @@ -356,7 +357,7 @@ class MemoryManagerNTHandleMock : public NEO::OsAgnosticMemoryManager { public: MemoryManagerNTHandleMock(NEO::ExecutionEnvironment &executionEnvironment) : NEO::OsAgnosticMemoryManager(executionEnvironment) {} - NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { + NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { auto graphicsAllocation = createMemoryAllocation(GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY, nullptr, reinterpret_cast(1), 1, 4096u, reinterpret_cast(handle), MemoryPool::SystemCpuInaccessible, rootDeviceIndex, false, false, false); diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 6a3ffefdba..bf9857b024 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -496,7 +496,7 @@ TEST_F(MemoryRelaxedSizeTests, } TEST_F(MemoryRelaxedSizeTests, - givenCallToDeviceAllocWithLargerThanAllowedSizeAndRelaxedDescriptorWithWrongStypeThenUnsupportedSizeIsReturned) { + givenCallToDeviceAllocWithLargerThanAllowedSizeAndRelaxedDescriptorWithWrongStypeThenUnsupportedEnumerationIsReturned) { size_t size = device->getNEODevice()->getDeviceInfo().maxMemAllocSize + 1; size_t alignment = 1u; void *ptr = nullptr; @@ -510,7 +510,7 @@ TEST_F(MemoryRelaxedSizeTests, ze_result_t result = context->allocDeviceMem(device->toHandle(), &deviceDesc, size, alignment, &ptr); - EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result); + EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION, result); EXPECT_EQ(nullptr, ptr); } @@ -1300,7 +1300,7 @@ class MemoryManagerIpcMock : public NEO::MemoryManager { NEO::GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override { return nullptr; } void addAllocationToHostPtrManager(NEO::GraphicsAllocation *memory) override{}; void removeAllocationFromHostPtrManager(NEO::GraphicsAllocation *memory) override{}; - NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; }; + NEO::GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { return nullptr; }; AllocationStatus populateOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override { return AllocationStatus::Success; }; void cleanOsHandles(NEO::OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{}; void freeGraphicsMemoryImpl(NEO::GraphicsAllocation *gfxAllocation) override{}; diff --git a/opencl/source/sharings/d3d/d3d_texture.cpp b/opencl/source/sharings/d3d/d3d_texture.cpp index 1c962023db..ea7a9daaa9 100644 --- a/opencl/source/sharings/d3d/d3d_texture.cpp +++ b/opencl/source/sharings/d3d/d3d_texture.cpp @@ -76,7 +76,7 @@ Image *D3DTexture::create2d(Context *context, D3DTexture2d *d3dTexture, cl_ if (textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED_NTHANDLE) { sharingFcns->getSharedNTHandle(textureStaging, &sharedHandle); if (memoryManager->verifyHandle(toOsHandle(sharedHandle), rootDeviceIndex, true)) { - alloc = memoryManager->createGraphicsAllocationFromNTHandle(sharedHandle, rootDeviceIndex); + alloc = memoryManager->createGraphicsAllocationFromNTHandle(sharedHandle, rootDeviceIndex, GraphicsAllocation::AllocationType::SHARED_IMAGE); } else { err.set(CL_INVALID_D3D11_RESOURCE_KHR); return nullptr; @@ -167,7 +167,7 @@ Image *D3DTexture::create3d(Context *context, D3DTexture3d *d3dTexture, cl_ if (textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED_NTHANDLE) { sharingFcns->getSharedNTHandle(textureStaging, &sharedHandle); if (memoryManager->verifyHandle(toOsHandle(sharedHandle), rootDeviceIndex, true)) { - alloc = memoryManager->createGraphicsAllocationFromNTHandle(sharedHandle, rootDeviceIndex); + alloc = memoryManager->createGraphicsAllocationFromNTHandle(sharedHandle, rootDeviceIndex, GraphicsAllocation::AllocationType::SHARED_IMAGE); } else { err.set(CL_INVALID_D3D11_RESOURCE_KHR); return nullptr; diff --git a/opencl/source/sharings/unified/unified_sharing.cpp b/opencl/source/sharings/unified/unified_sharing.cpp index 9b358fdd77..c2dc3ee49f 100644 --- a/opencl/source/sharings/unified/unified_sharing.cpp +++ b/opencl/source/sharings/unified/unified_sharing.cpp @@ -36,7 +36,7 @@ GraphicsAllocation *UnifiedSharing::createGraphicsAllocation(Context *context, U auto memoryManager = context->getMemoryManager(); switch (description.type) { case UnifiedSharingHandleType::Win32Nt: { - return memoryManager->createGraphicsAllocationFromNTHandle(description.handle, context->getDevice(0)->getRootDeviceIndex()); + return memoryManager->createGraphicsAllocationFromNTHandle(description.handle, context->getDevice(0)->getRootDeviceIndex(), GraphicsAllocation::AllocationType::SHARED_IMAGE); } case UnifiedSharingHandleType::LinuxFd: case UnifiedSharingHandleType::Win32Shared: { diff --git a/opencl/test/unit_test/device/device_caps_tests.cpp b/opencl/test/unit_test/device/device_caps_tests.cpp index 7f6116892c..c3f0a0dd17 100644 --- a/opencl/test/unit_test/device/device_caps_tests.cpp +++ b/opencl/test/unit_test/device/device_caps_tests.cpp @@ -1362,7 +1362,7 @@ TEST_F(DeviceGetCapsTest, givenFlagEnabled64kbPagesWhenCallConstructorMemoryMana void addAllocationToHostPtrManager(GraphicsAllocation *memory) override{}; void removeAllocationFromHostPtrManager(GraphicsAllocation *memory) override{}; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override { return nullptr; }; - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; }; + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { return nullptr; }; AllocationStatus populateOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override { return AllocationStatus::Success; }; void cleanOsHandles(OsHandleStorage &handleStorage, uint32_t rootDeviceIndex) override{}; void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override{}; diff --git a/opencl/test/unit_test/fixtures/d3d_test_fixture.h b/opencl/test/unit_test/fixtures/d3d_test_fixture.h index 86b4e23377..0ed83a2a9a 100644 --- a/opencl/test/unit_test/fixtures/d3d_test_fixture.h +++ b/opencl/test/unit_test/fixtures/d3d_test_fixture.h @@ -55,7 +55,7 @@ class D3DTests : public PlatformFixture, public ::testing::Test { gmmOwnershipPassed = true; return alloc; } - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { if (failAlloc) { return nullptr; } diff --git a/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp b/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp index 2fbcdac34a..e377255ae0 100644 --- a/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp +++ b/opencl/test/unit_test/memory_manager/memory_manager_tests.cpp @@ -1114,7 +1114,7 @@ TEST(OsAgnosticMemoryManager, givenDefaultMemoryManagerWhenCreateGraphicsAllocat TEST(OsAgnosticMemoryManager, givenMemoryManagerWhenCreateAllocationFromNtHandleIsCalledThenReturnNullptr) { MockExecutionEnvironment executionEnvironment(defaultHwInfo.get()); OsAgnosticMemoryManager memoryManager(executionEnvironment); - auto graphicsAllocation = memoryManager.createGraphicsAllocationFromNTHandle((void *)1, 0); + auto graphicsAllocation = memoryManager.createGraphicsAllocationFromNTHandle((void *)1, 0, GraphicsAllocation::AllocationType::SHARED_IMAGE); EXPECT_EQ(nullptr, graphicsAllocation); } diff --git a/opencl/test/unit_test/mocks/mock_memory_manager.h b/opencl/test/unit_test/mocks/mock_memory_manager.h index 7b0fb911d2..35b00306ed 100644 --- a/opencl/test/unit_test/mocks/mock_memory_manager.h +++ b/opencl/test/unit_test/mocks/mock_memory_manager.h @@ -240,7 +240,7 @@ class FailMemoryManager : public MockMemoryManager { GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override { return nullptr; } - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { return nullptr; } diff --git a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp index e79fe765a2..faeeb7883b 100644 --- a/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp +++ b/opencl/test/unit_test/os_interface/linux/drm_memory_manager_tests.cpp @@ -2433,7 +2433,7 @@ TEST_F(DrmMemoryManagerTest, givenTwoGraphicsAllocationsThatDoesnShareTheSameBuf } TEST_F(DrmMemoryManagerWithExplicitExpectationsTest, givenDrmMemoryManagerWhenCreateAllocationFromNtHandleIsCalledThenReturnNullptr) { - auto graphicsAllocation = memoryManager->createGraphicsAllocationFromNTHandle(reinterpret_cast(1), 0); + auto graphicsAllocation = memoryManager->createGraphicsAllocationFromNTHandle(reinterpret_cast(1), 0, GraphicsAllocation::AllocationType::SHARED_IMAGE); EXPECT_EQ(nullptr, graphicsAllocation); } diff --git a/opencl/test/unit_test/os_interface/windows/wddm_memory_manager_tests.cpp b/opencl/test/unit_test/os_interface/windows/wddm_memory_manager_tests.cpp index 0e2355e46c..16569e9ae1 100644 --- a/opencl/test/unit_test/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/opencl/test/unit_test/os_interface/windows/wddm_memory_manager_tests.cpp @@ -605,7 +605,7 @@ TEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWhenCreateFromNTHandleIsCall std::unique_ptr gmm(new Gmm(rootDeviceEnvironment->getGmmClientContext(), pSysMem, 4096u, 0, false)); setSizesFcn(gmm->gmmResourceInfo.get(), 1u, 1024u, 1u); - auto *gpuAllocation = memoryManager->createGraphicsAllocationFromNTHandle(reinterpret_cast(1), 0); + auto *gpuAllocation = memoryManager->createGraphicsAllocationFromNTHandle(reinterpret_cast(1), 0, GraphicsAllocation::AllocationType::SHARED_IMAGE); auto wddmAlloc = static_cast(gpuAllocation); ASSERT_NE(nullptr, gpuAllocation); EXPECT_EQ(NT_RESOURCE_HANDLE, wddmAlloc->resourceHandle); diff --git a/opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h b/opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h index 163917df01..7550a57d24 100644 --- a/opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h +++ b/opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h @@ -57,7 +57,7 @@ struct UnifiedSharingContextFixture : ::testing::Test { template struct UnifiedSharingMockMemoryManager : MockMemoryManager { - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { if (!validMemoryManager) { return nullptr; } diff --git a/opencl/test/unit_test/sharings/unified/unified_sharing_image_tests.cpp b/opencl/test/unit_test/sharings/unified/unified_sharing_image_tests.cpp index 4a638654ca..3ec05b5969 100644 --- a/opencl/test/unit_test/sharings/unified/unified_sharing_image_tests.cpp +++ b/opencl/test/unit_test/sharings/unified/unified_sharing_image_tests.cpp @@ -105,8 +105,8 @@ class MockHwHelper : public HwHelperHw { }; struct MemoryManagerReturningCompressedAllocations : UnifiedSharingMockMemoryManager { - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { - auto allocation = UnifiedSharingMockMemoryManager::createGraphicsAllocationFromNTHandle(handle, rootDeviceIndex); + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { + auto allocation = UnifiedSharingMockMemoryManager::createGraphicsAllocationFromNTHandle(handle, rootDeviceIndex, GraphicsAllocation::AllocationType::SHARED_IMAGE); auto gmm = allocation->getDefaultGmm(); auto mockGmmResourceInfo = std::make_unique(gmm->gmmResourceInfo->peekGmmResourceInfo()); diff --git a/opencl/test/unit_test/sharings/unified/unified_sharing_tests.cpp b/opencl/test/unit_test/sharings/unified/unified_sharing_tests.cpp index 93693c4dbf..d853437cbf 100644 --- a/opencl/test/unit_test/sharings/unified/unified_sharing_tests.cpp +++ b/opencl/test/unit_test/sharings/unified/unified_sharing_tests.cpp @@ -163,7 +163,7 @@ struct UnifiedSharingCreateAllocationTests : UnifiedSharingTestsWithMemoryManage struct MemoryManagerCheckingAllocationMethod : MockMemoryManager { using MockMemoryManager::MockMemoryManager; - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { this->createFromNTHandleCalled = true; this->handle = toOsHandle(handle); return nullptr; diff --git a/shared/source/memory_manager/memory_manager.h b/shared/source/memory_manager/memory_manager.h index 190403d29c..52074ee8e5 100644 --- a/shared/source/memory_manager/memory_manager.h +++ b/shared/source/memory_manager/memory_manager.h @@ -90,7 +90,7 @@ class MemoryManager { virtual bool verifyHandle(osHandle handle, uint32_t rootDeviceIndex, bool) { return true; } virtual GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) = 0; virtual void closeSharedHandle(GraphicsAllocation *graphicsAllocation){}; - virtual GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) = 0; + virtual GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) = 0; virtual bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation); diff --git a/shared/source/memory_manager/os_agnostic_memory_manager.h b/shared/source/memory_manager/os_agnostic_memory_manager.h index d85e205707..9b771037eb 100644 --- a/shared/source/memory_manager/os_agnostic_memory_manager.h +++ b/shared/source/memory_manager/os_agnostic_memory_manager.h @@ -70,7 +70,7 @@ class OsAgnosticMemoryManager : public MemoryManager { void initialize(bool aubUsage); ~OsAgnosticMemoryManager() override; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override; - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; } + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { return nullptr; } void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override; void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override; diff --git a/shared/source/os_interface/linux/drm_memory_manager.h b/shared/source/os_interface/linux/drm_memory_manager.h index 43c81ab65e..1bb8de630c 100644 --- a/shared/source/os_interface/linux/drm_memory_manager.h +++ b/shared/source/os_interface/linux/drm_memory_manager.h @@ -38,7 +38,7 @@ class DrmMemoryManager : public MemoryManager { GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override; void closeSharedHandle(GraphicsAllocation *gfxAllocation) override; GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) override; - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { return nullptr; } + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override { return nullptr; } uint64_t getSystemSharedMemory(uint32_t rootDeviceIndex) override; uint64_t getLocalMemorySize(uint32_t rootDeviceIndex, uint32_t deviceBitfield) override; diff --git a/shared/source/os_interface/windows/wddm_memory_manager.cpp b/shared/source/os_interface/windows/wddm_memory_manager.cpp index 49e6070eb0..9550ff4ef1 100644 --- a/shared/source/os_interface/windows/wddm_memory_manager.cpp +++ b/shared/source/os_interface/windows/wddm_memory_manager.cpp @@ -409,8 +409,8 @@ GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromSharedHandle( return createAllocationFromHandle(handle, requireSpecificBitness, false, properties.allocationType, properties.rootDeviceIndex); } -GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) { - return createAllocationFromHandle(toOsHandle(handle), false, true, GraphicsAllocation::AllocationType::SHARED_IMAGE, rootDeviceIndex); +GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) { + return createAllocationFromHandle(toOsHandle(handle), false, true, allocType, rootDeviceIndex); } void WddmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) { diff --git a/shared/source/os_interface/windows/wddm_memory_manager.h b/shared/source/os_interface/windows/wddm_memory_manager.h index e2632396b4..e0ca263650 100644 --- a/shared/source/os_interface/windows/wddm_memory_manager.h +++ b/shared/source/os_interface/windows/wddm_memory_manager.h @@ -44,7 +44,7 @@ class WddmMemoryManager : public MemoryManager { void handleFenceCompletion(GraphicsAllocation *allocation) override; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override; - GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override; + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex, GraphicsAllocation::AllocationType allocType) override; void addAllocationToHostPtrManager(GraphicsAllocation *memory) override; void removeAllocationFromHostPtrManager(GraphicsAllocation *memory) override;