From 933bb45dcc521774239e71ee61fb1aa01925929f Mon Sep 17 00:00:00 2001 From: "Vilvaraj, T J Vivek" Date: Thu, 13 Aug 2020 16:04:11 +0530 Subject: [PATCH] Boilerplate Code for Sysman Firmware API Change-Id: I8299bdf4b388bb05de7640240d033ba680e5ca14 Signed-off-by: mraghuwa Signed-off-by: Vilvaraj, T J Vivek --- level_zero/api/sysman/zes_sysman.cpp | 4 +- .../source/sysman/firmware/CMakeLists.txt | 25 +++++ .../tools/source/sysman/firmware/firmware.cpp | 45 +++++++++ .../tools/source/sysman/firmware/firmware.h | 46 +++++++++ .../source/sysman/firmware/firmware_imp.cpp | 36 +++++++ .../source/sysman/firmware/firmware_imp.h | 30 ++++++ .../sysman/firmware/linux/CMakeLists.txt | 21 +++++ .../sysman/firmware/linux/os_firmware_imp.cpp | 24 +++++ .../sysman/firmware/linux/os_firmware_imp.h | 26 +++++ .../source/sysman/firmware/os_firmware.h | 23 +++++ .../sysman/firmware/windows/CMakeLists.txt | 20 ++++ .../firmware/windows/os_firmware_imp.cpp | 21 +++++ .../sysman/firmware/windows/os_firmware_imp.h | 20 ++++ level_zero/tools/source/sysman/sysman.h | 2 + level_zero/tools/source/sysman/sysman_imp.cpp | 9 ++ level_zero/tools/source/sysman/sysman_imp.h | 2 + .../sources/sysman/firmware/CMakeLists.txt | 11 +++ .../sysman/firmware/linux/CMakeLists.txt | 13 +++ .../linux/test_zes_sysman_firmware.cpp | 94 +++++++++++++++++++ 19 files changed, 470 insertions(+), 2 deletions(-) create mode 100644 level_zero/tools/source/sysman/firmware/CMakeLists.txt create mode 100644 level_zero/tools/source/sysman/firmware/firmware.cpp create mode 100644 level_zero/tools/source/sysman/firmware/firmware.h create mode 100644 level_zero/tools/source/sysman/firmware/firmware_imp.cpp create mode 100644 level_zero/tools/source/sysman/firmware/firmware_imp.h create mode 100644 level_zero/tools/source/sysman/firmware/linux/CMakeLists.txt create mode 100644 level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.cpp create mode 100644 level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.h create mode 100644 level_zero/tools/source/sysman/firmware/os_firmware.h create mode 100644 level_zero/tools/source/sysman/firmware/windows/CMakeLists.txt create mode 100644 level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.cpp create mode 100644 level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/firmware/CMakeLists.txt create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/CMakeLists.txt create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/test_zes_sysman_firmware.cpp diff --git a/level_zero/api/sysman/zes_sysman.cpp b/level_zero/api/sysman/zes_sysman.cpp index eb3025de2f..f4dd01fd80 100644 --- a/level_zero/api/sysman/zes_sysman.cpp +++ b/level_zero/api/sysman/zes_sysman.cpp @@ -375,14 +375,14 @@ zesDeviceEnumFirmwares( zes_device_handle_t hDevice, uint32_t *pCount, zes_firmware_handle_t *phFirmware) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::SysmanDevice::fromHandle(hDevice)->firmwareGet(pCount, phFirmware); } ZE_APIEXPORT ze_result_t ZE_APICALL zesFirmwareGetProperties( zes_firmware_handle_t hFirmware, zes_firmware_properties_t *pProperties) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::Firmware::fromHandle(hFirmware)->firmwareGetProperties(pProperties); } ZE_APIEXPORT ze_result_t ZE_APICALL diff --git a/level_zero/tools/source/sysman/firmware/CMakeLists.txt b/level_zero/tools/source/sysman/firmware/CMakeLists.txt new file mode 100644 index 0000000000..020738067d --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/CMakeLists.txt @@ -0,0 +1,25 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +set(L0_SRCS_TOOLS_SYSMAN_FIRMWARE + ${CMAKE_CURRENT_SOURCE_DIR}/firmware.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/firmware.h + ${CMAKE_CURRENT_SOURCE_DIR}/firmware_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/firmware_imp.h + ${CMAKE_CURRENT_SOURCE_DIR}/os_firmware.h +) + +target_sources(${L0_STATIC_LIB_NAME} + PRIVATE + ${L0_SRCS_TOOLS_SYSMAN_FIRMWARE} + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt +) + +add_subdirectories() + +# Make our source files visible to parent +set_property(GLOBAL PROPERTY L0_SRCS_TOOLS_SYSMAN_FIRMWARE ${L0_SRCS_TOOLS_SYSMAN_FIRMWARE}) + diff --git a/level_zero/tools/source/sysman/firmware/firmware.cpp b/level_zero/tools/source/sysman/firmware/firmware.cpp new file mode 100644 index 0000000000..62e6f952c8 --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/firmware.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "firmware.h" + +#include "shared/source/helpers/basic_math.h" + +#include "firmware_imp.h" + +namespace L0 { + +FirmwareHandleContext::~FirmwareHandleContext() { + for (Firmware *pFirmware : handleList) { + delete pFirmware; + } + handleList.clear(); +} + +void FirmwareHandleContext::init() { + Firmware *pFirmware = new FirmwareImp(pOsSysman); + if (pFirmware->isFirmwareEnabled == true) { + handleList.push_back(pFirmware); + } else { + delete pFirmware; + } +} + +ze_result_t FirmwareHandleContext::firmwareGet(uint32_t *pCount, zes_firmware_handle_t *phFirmware) { + uint32_t handleListSize = static_cast(handleList.size()); + uint32_t numToCopy = std::min(*pCount, handleListSize); + if (0 == *pCount || *pCount > handleListSize) { + *pCount = handleListSize; + } + if (nullptr != phFirmware) { + for (uint32_t i = 0; i < numToCopy; i++) { + phFirmware[i] = handleList[i]->toHandle(); + } + } + return ZE_RESULT_SUCCESS; +} +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/firmware.h b/level_zero/tools/source/sysman/firmware/firmware.h new file mode 100644 index 0000000000..f307959bcb --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/firmware.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include + +#include + +struct _zes_firmware_handle_t { + virtual ~_zes_firmware_handle_t() = default; +}; + +namespace L0 { + +struct OsSysman; + +class Firmware : _zes_firmware_handle_t { + public: + virtual ~Firmware() {} + virtual ze_result_t firmwareGetProperties(zes_firmware_properties_t *pProperties) = 0; + + inline zes_firmware_handle_t toHandle() { return this; } + + static Firmware *fromHandle(zes_firmware_handle_t handle) { + return static_cast(handle); + } + bool isFirmwareEnabled = false; +}; + +struct FirmwareHandleContext { + FirmwareHandleContext(OsSysman *pOsSysman) : pOsSysman(pOsSysman){}; + ~FirmwareHandleContext(); + + void init(); + + ze_result_t firmwareGet(uint32_t *pCount, zes_firmware_handle_t *phFirmware); + + OsSysman *pOsSysman = nullptr; + std::vector handleList = {}; +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/firmware_imp.cpp b/level_zero/tools/source/sysman/firmware/firmware_imp.cpp new file mode 100644 index 0000000000..0471765688 --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/firmware_imp.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "firmware_imp.h" + +#include "shared/source/helpers/debug_helpers.h" + +#include "os_firmware.h" + +#include + +namespace L0 { + +ze_result_t FirmwareImp::firmwareGetProperties(zes_firmware_properties_t *pProperties) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +void FirmwareImp::init() { + this->isFirmwareEnabled = pOsFirmware->isFirmwareSupported(); +} + +FirmwareImp::FirmwareImp(OsSysman *pOsSysman) { + pOsFirmware = OsFirmware::create(pOsSysman); + UNRECOVERABLE_IF(nullptr == pOsFirmware); + init(); +} + +FirmwareImp::~FirmwareImp() { + delete pOsFirmware; +} + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/firmware_imp.h b/level_zero/tools/source/sysman/firmware/firmware_imp.h new file mode 100644 index 0000000000..7633c0c0c8 --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/firmware_imp.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include + +#include "firmware.h" + +namespace L0 { + +class OsFirmware; + +class FirmwareImp : public Firmware, NEO::NonCopyableOrMovableClass { + public: + ze_result_t firmwareGetProperties(zes_firmware_properties_t *pProperties) override; + FirmwareImp() = default; + FirmwareImp(OsSysman *pOsSysman); + ~FirmwareImp() override; + OsFirmware *pOsFirmware = nullptr; + + void init(); +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/linux/CMakeLists.txt b/level_zero/tools/source/sysman/firmware/linux/CMakeLists.txt new file mode 100644 index 0000000000..cd8986d180 --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/linux/CMakeLists.txt @@ -0,0 +1,21 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +set(L0_SRCS_TOOLS_SYSMAN_FIRMWARE_LINUX + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/os_firmware_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/os_firmware_imp.h +) + +if(UNIX) + target_sources(${L0_STATIC_LIB_NAME} + PRIVATE + ${L0_SRCS_TOOLS_SYSMAN_FIRMWARE_LINUX} + ) +endif() + +# Make our source files visible to parent +set_property(GLOBAL PROPERTY L0_SRCS_TOOLS_SYSMAN_FIRMWARE_LINUX ${L0_SRCS_TOOLS_SYSMAN_FIRMWARE_LINUX}) diff --git a/level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.cpp b/level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.cpp new file mode 100644 index 0000000000..c5b17145f4 --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.h" + +namespace L0 { + +bool LinuxFirmwareImp::isFirmwareSupported(void) { + return false; +} + +LinuxFirmwareImp::LinuxFirmwareImp(OsSysman *pOsSysman) { +} + +OsFirmware *OsFirmware::create(OsSysman *pOsSysman) { + LinuxFirmwareImp *pLinuxFirmwareImp = new LinuxFirmwareImp(pOsSysman); + return static_cast(pLinuxFirmwareImp); +} + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.h b/level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.h new file mode 100644 index 0000000000..507a935b1e --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include "sysman/firmware/firmware_imp.h" +#include "sysman/firmware/os_firmware.h" +#include "sysman/linux/os_sysman_imp.h" + +namespace L0 { + +class LinuxFirmwareImp : public OsFirmware, NEO::NonCopyableOrMovableClass { + public: + bool isFirmwareSupported(void) override; + + LinuxFirmwareImp() = default; + LinuxFirmwareImp(OsSysman *pOsSysman); + ~LinuxFirmwareImp() override = default; +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/os_firmware.h b/level_zero/tools/source/sysman/firmware/os_firmware.h new file mode 100644 index 0000000000..ad3de2ddf8 --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/os_firmware.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once + +#include "level_zero/tools/source/sysman/os_sysman.h" +#include + +namespace L0 { + +class OsFirmware { + public: + virtual bool isFirmwareSupported(void) = 0; + + static OsFirmware *create(OsSysman *pOsSysman); + virtual ~OsFirmware() {} +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/windows/CMakeLists.txt b/level_zero/tools/source/sysman/firmware/windows/CMakeLists.txt new file mode 100644 index 0000000000..f62c869ceb --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/windows/CMakeLists.txt @@ -0,0 +1,20 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +set(L0_SRCS_TOOLS_SYSMAN_FIRMWARE_WINDOWS + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/os_firmware_imp.cpp +) + +if(WIN32) + target_sources(${L0_STATIC_LIB_NAME} + PRIVATE + ${L0_SRCS_TOOLS_SYSMAN_FIRMWARE_WINDOWS} + ) +endif() + +# Make our source files visible to parent +set_property(GLOBAL PROPERTY L0_SRCS_TOOLS_SYSMAN_FIRMWARE_WINDOWS ${L0_SRCS_TOOLS_SYSMAN_FIRMWARE_WINDOWS}) diff --git a/level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.cpp b/level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.cpp new file mode 100644 index 0000000000..3994ec401e --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.cpp @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.h" + +namespace L0 { + +bool WddmFirmwareImp::isFirmwareSupported(void) { + return false; +} + +OsFirmware *OsFirmware::create(OsSysman *pOsSysman) { + WddmFirmwareImp *pWddmFirmwareImp = new WddmFirmwareImp(); + return static_cast(pWddmFirmwareImp); +} + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.h b/level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.h new file mode 100644 index 0000000000..6125e24cbd --- /dev/null +++ b/level_zero/tools/source/sysman/firmware/windows/os_firmware_imp.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include "sysman/firmware/os_firmware.h" +#include "sysman/windows/os_sysman_imp.h" + +namespace L0 { +class WddmFirmwareImp : public OsFirmware { + public: + bool isFirmwareSupported(void) override; +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/sysman.h b/level_zero/tools/source/sysman/sysman.h index b566096487..ba61059739 100644 --- a/level_zero/tools/source/sysman/sysman.h +++ b/level_zero/tools/source/sysman/sysman.h @@ -10,6 +10,7 @@ #include "level_zero/tools/source/sysman/engine/engine.h" #include "level_zero/tools/source/sysman/fabric_port/fabric_port.h" #include "level_zero/tools/source/sysman/fan/fan.h" +#include "level_zero/tools/source/sysman/firmware/firmware.h" #include "level_zero/tools/source/sysman/frequency/frequency.h" #include "level_zero/tools/source/sysman/global_operations/global_operations.h" #include "level_zero/tools/source/sysman/memory/memory.h" @@ -50,6 +51,7 @@ struct SysmanDevice : _ze_device_handle_t { virtual ze_result_t rasGet(uint32_t *pCount, zes_ras_handle_t *phRas) = 0; virtual ze_result_t memoryGet(uint32_t *pCount, zes_mem_handle_t *phMemory) = 0; virtual ze_result_t fanGet(uint32_t *pCount, zes_fan_handle_t *phFan) = 0; + virtual ze_result_t firmwareGet(uint32_t *pCount, zes_firmware_handle_t *phFirmware) = 0; virtual ~SysmanDevice() = default; }; diff --git a/level_zero/tools/source/sysman/sysman_imp.cpp b/level_zero/tools/source/sysman/sysman_imp.cpp index 8fd99002c9..845411cdb3 100644 --- a/level_zero/tools/source/sysman/sysman_imp.cpp +++ b/level_zero/tools/source/sysman/sysman_imp.cpp @@ -33,10 +33,12 @@ SysmanDeviceImp::SysmanDeviceImp(ze_device_handle_t hDevice) { pMemoryHandleContext = new MemoryHandleContext(pOsSysman); pGlobalOperations = new GlobalOperationsImp(pOsSysman); pFanHandleContext = new FanHandleContext(pOsSysman); + pFirmwareHandleContext = new FirmwareHandleContext(pOsSysman); } SysmanDeviceImp::~SysmanDeviceImp() { freeResource(pFanHandleContext); + freeResource(pFirmwareHandleContext); freeResource(pGlobalOperations); freeResource(pMemoryHandleContext); freeResource(pRasHandleContext); @@ -89,6 +91,9 @@ void SysmanDeviceImp::init() { if (pFanHandleContext) { pFanHandleContext->init(); } + if (pFirmwareHandleContext) { + pFirmwareHandleContext->init(); + } } ze_result_t SysmanDeviceImp::frequencyGet(uint32_t *pCount, zes_freq_handle_t *phFrequency) { @@ -155,6 +160,10 @@ ze_result_t SysmanDeviceImp::rasGet(uint32_t *pCount, zes_ras_handle_t *phRas) { return pRasHandleContext->rasGet(pCount, phRas); } +ze_result_t SysmanDeviceImp::firmwareGet(uint32_t *pCount, zes_firmware_handle_t *phFirmware) { + return pFirmwareHandleContext->firmwareGet(pCount, phFirmware); +} + ze_result_t SysmanDeviceImp::memoryGet(uint32_t *pCount, zes_mem_handle_t *phMemory) { return pMemoryHandleContext->memoryGet(pCount, phMemory); } diff --git a/level_zero/tools/source/sysman/sysman_imp.h b/level_zero/tools/source/sysman/sysman_imp.h index 4b1cf8e875..2129c3b183 100644 --- a/level_zero/tools/source/sysman/sysman_imp.h +++ b/level_zero/tools/source/sysman/sysman_imp.h @@ -38,6 +38,7 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass { RasHandleContext *pRasHandleContext = nullptr; MemoryHandleContext *pMemoryHandleContext = nullptr; FanHandleContext *pFanHandleContext = nullptr; + FirmwareHandleContext *pFirmwareHandleContext = nullptr; ze_result_t powerGet(uint32_t *pCount, zes_pwr_handle_t *phPower) override; ze_result_t frequencyGet(uint32_t *pCount, zes_freq_handle_t *phFrequency) override; @@ -57,6 +58,7 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass { ze_result_t rasGet(uint32_t *pCount, zes_ras_handle_t *phRas) override; ze_result_t memoryGet(uint32_t *pCount, zes_mem_handle_t *phMemory) override; ze_result_t fanGet(uint32_t *pCount, zes_fan_handle_t *phFan) override; + ze_result_t firmwareGet(uint32_t *pCount, zes_firmware_handle_t *phFirmware) override; private: template diff --git a/level_zero/tools/test/unit_tests/sources/sysman/firmware/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/firmware/CMakeLists.txt new file mode 100644 index 0000000000..2cc801bbb9 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/firmware/CMakeLists.txt @@ -0,0 +1,11 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +target_sources(${TARGET_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt +) + +add_subdirectories() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/CMakeLists.txt new file mode 100644 index 0000000000..3b68530dc4 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/CMakeLists.txt @@ -0,0 +1,13 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +if(UNIX) + target_sources(${TARGET_NAME} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_zes_sysman_firmware.cpp + ) +endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/test_zes_sysman_firmware.cpp b/level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/test_zes_sysman_firmware.cpp new file mode 100644 index 0000000000..921843430e --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/firmware/linux/test_zes_sysman_firmware.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/tools/source/sysman/firmware/linux/os_firmware_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h" + +using ::testing::_; + +namespace L0 { +namespace ult { + +constexpr uint32_t mockHandleCount = 0; +class ZesFirmwareFixture : public SysmanDeviceFixture { + + protected: + zes_firmware_handle_t hSysmanFirmware = {}; + + void SetUp() override { + SysmanDeviceFixture::SetUp(); + } + void TearDown() override { + SysmanDeviceFixture::TearDown(); + } + + std::vector get_firmware_handles(uint32_t count) { + std::vector handles(count, nullptr); + EXPECT_EQ(zesDeviceEnumFirmwares(device->toHandle(), &count, handles.data()), ZE_RESULT_SUCCESS); + return handles; + } +}; + +TEST_F(ZesFirmwareFixture, GivenComponentCountZeroWhenCallingzesFirmwareGetThenZeroCountIsReturnedAndVerifyzesFirmwareGetCallSucceeds) { + std::vector firmwareHandle{}; + uint32_t count = 0; + + ze_result_t result = zesDeviceEnumFirmwares(device->toHandle(), &count, nullptr); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(count, mockHandleCount); + + uint32_t testCount = count + 1; + + result = zesDeviceEnumFirmwares(device->toHandle(), &testCount, nullptr); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(testCount, count); + + firmwareHandle.resize(count); + result = zesDeviceEnumFirmwares(device->toHandle(), &count, firmwareHandle.data()); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(nullptr, firmwareHandle.data()); + EXPECT_EQ(count, mockHandleCount); + + FirmwareImp *ptestFirmwareImp = new FirmwareImp(pSysmanDeviceImp->pFirmwareHandleContext->pOsSysman); + pSysmanDeviceImp->pFirmwareHandleContext->handleList.push_back(ptestFirmwareImp); + result = zesDeviceEnumFirmwares(device->toHandle(), &count, nullptr); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(count, mockHandleCount + 1); + + testCount = count; + + firmwareHandle.resize(testCount); + result = zesDeviceEnumFirmwares(device->toHandle(), &testCount, firmwareHandle.data()); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, firmwareHandle.data()); + EXPECT_EQ(testCount, mockHandleCount + 1); + + pSysmanDeviceImp->pFirmwareHandleContext->handleList.pop_back(); + delete ptestFirmwareImp; +} + +TEST_F(ZesFirmwareFixture, GivenValidFirmwareHandleWhenGettingFirmwarePropertiesThenUnsupportedIsReturned) { + FirmwareImp *ptestFirmwareImp = new FirmwareImp(pSysmanDeviceImp->pFirmwareHandleContext->pOsSysman); + pSysmanDeviceImp->pFirmwareHandleContext->handleList.push_back(ptestFirmwareImp); + + auto handles = get_firmware_handles(mockHandleCount + 1); + + for (auto handle : handles) { + zes_firmware_properties_t properties = {}; + EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesFirmwareGetProperties(handle, &properties)); + } + pSysmanDeviceImp->pFirmwareHandleContext->handleList.pop_back(); + delete ptestFirmwareImp; +} + +} // namespace ult +} // namespace L0