From f86f62b3f7d93b0be3424031dc99025d1242207c Mon Sep 17 00:00:00 2001 From: "Bill(Shuzhou) Liu" Date: Wed, 13 Sep 2023 15:38:20 -0500 Subject: [PATCH] Return the driver loading status When init the library, it could return status whether the driver is loaded or not. Change-Id: Id26b8058e32881ebe2514067a639a2a871d1f252 --- include/amd_smi/amdsmi.h | 1 + rocm_smi/include/rocm_smi/rocm_smi.h | 21 +++++++++++++++++++++ rocm_smi/src/rocm_smi.cc | 27 +++++++++++++++++++++++++++ src/amd_smi/amd_smi_system.cc | 5 +++++ 4 files changed, 54 insertions(+) diff --git a/include/amd_smi/amdsmi.h b/include/amd_smi/amdsmi.h index 78a240a2d5..3405fddac7 100644 --- a/include/amd_smi/amdsmi.h +++ b/include/amd_smi/amdsmi.h @@ -178,6 +178,7 @@ typedef enum { AMDSMI_STATUS_NOT_FOUND = 31, //!< Device Not found AMDSMI_STATUS_NOT_INIT = 32, //!< Device not initialized AMDSMI_STATUS_NO_SLOT = 33, //!< No more free slot + AMDSMI_STATUS_DRIVER_NOT_LOADED = 34, //!< Processor driver not loaded // Data and size errors AMDSMI_STATUS_NO_DATA = 40, //!< No data was found for a given input AMDSMI_STATUS_INSUFFICIENT_SIZE = 41, //!< Not enough resources were available for the operation diff --git a/rocm_smi/include/rocm_smi/rocm_smi.h b/rocm_smi/include/rocm_smi/rocm_smi.h index 1d95ed00ac..37d1c6d013 100755 --- a/rocm_smi/include/rocm_smi/rocm_smi.h +++ b/rocm_smi/include/rocm_smi/rocm_smi.h @@ -150,6 +150,20 @@ typedef enum { RSMI_INIT_FLAG_RESRV_TEST1 = 0x800000000000000, //!< Reserved for test } rsmi_init_flags_t; +/** + * @brief Driver loading status + * + * The driver loading status from initState sysfs + */ + +typedef enum { + RSMI_DRIVER_NOT_FOUND = 0, //!< Cannot find the driver + RSMI_DRIVER_MODULE_STATE_LIVE, //!< Driver loaded and live + RSMI_DRIVER_MODULE_STATE_LOADING, //!< Driver is loading(coming) + RSMI_DRIVER_MODULE_STATE_UNLOADING, //!< Driver is unloading(going) + RSMI_DRIVER_MODULE_STATE_UNKNOWN, //!< Driver state unknown +} rsmi_driver_state_t; + /** * @brief PowerPlay performance levels */ @@ -1038,6 +1052,13 @@ rsmi_status_t rsmi_init(uint64_t init_flags); */ rsmi_status_t rsmi_shut_down(void); +/** + * @brief Get driver loading status + * + * @details The status could be not found, live, loading, unloading. + */ +rsmi_status_t rsmi_driver_status(rsmi_driver_state_t* state); + /** @} */ // end of InitShut /*****************************************************************************/ diff --git a/rocm_smi/src/rocm_smi.cc b/rocm_smi/src/rocm_smi.cc index 8a62ace6d1..e52851a10f 100755 --- a/rocm_smi/src/rocm_smi.cc +++ b/rocm_smi/src/rocm_smi.cc @@ -508,6 +508,33 @@ rsmi_shut_down(void) { CATCH } + +rsmi_status_t rsmi_driver_status(rsmi_driver_state_t* state) { + TRY + if (state == nullptr) { + return RSMI_STATUS_INVALID_ARGS; + } + + // live, coming, going + static const char *kDevInitStateID = "/sys/module/amdgpu/initstate"; + std::ifstream infile(kDevInitStateID); + if (!infile) { + *state = RSMI_DRIVER_NOT_FOUND; + return RSMI_STATUS_SUCCESS; + } + + std::string stat_str; + infile >> stat_str; + + *state = RSMI_DRIVER_MODULE_STATE_UNKNOWN; + if (stat_str == "live") *state = RSMI_DRIVER_MODULE_STATE_LIVE; + if (stat_str == "coming") *state = RSMI_DRIVER_MODULE_STATE_LOADING; + if (stat_str == "going") *state = RSMI_DRIVER_MODULE_STATE_UNLOADING; + + return RSMI_STATUS_SUCCESS; + CATCH +} + rsmi_status_t rsmi_num_monitor_devices(uint32_t *num_devices) { TRY diff --git a/src/amd_smi/amd_smi_system.cc b/src/amd_smi/amd_smi_system.cc index ff73d6b559..a0520fdddc 100644 --- a/src/amd_smi/amd_smi_system.cc +++ b/src/amd_smi/amd_smi_system.cc @@ -208,8 +208,13 @@ amdsmi_status_t AMDSmiSystem::get_cpu_model(uint32_t cpu_model) { amdsmi_status_t AMDSmiSystem::populate_amd_gpu_devices() { // init rsmi + rsmi_driver_state_t state; rsmi_status_t ret = rsmi_init(0); if (ret != RSMI_STATUS_SUCCESS) { + if (rsmi_driver_status(&state) == RSMI_STATUS_SUCCESS && + state != RSMI_DRIVER_MODULE_STATE_LIVE) { + return AMDSMI_STATUS_DRIVER_NOT_LOADED; + } return amd::smi::rsmi_to_amdsmi_status(ret); }