The init version of amd_smi

The init version includes the amd_smi.h header, an example uses the
amd_smi, folder structure and CMake files.

Add the support to libdrm.

Change-Id: I779e55e4cf9491c61dc226a30d24e96be9bc6016
Cette révision appartient à :
Bill(Shuzhou) Liu
2022-06-03 09:11:21 -04:00
Parent 44ea49eb01
révision 91ad08aa65
20 fichiers modifiés avec 9019 ajouts et 3 suppressions
+19 -1
Voir le fichier
@@ -113,9 +113,13 @@ endif ()
set(COMMON_SRC_DIR "${PROJECT_SOURCE_DIR}/src")
set(COMMON_INC_DIR "${PROJECT_SOURCE_DIR}/include/rocm_smi")
set(SHR_MUTEX_DIR "${PROJECT_SOURCE_DIR}/third_party/shared_mutex")
set(AMDSMI_SRC_DIR "${PROJECT_SOURCE_DIR}/amd_smi/src")
set(AMDSMI_INC_DIR "${PROJECT_SOURCE_DIR}/amd_smi/include")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/third_party/shared_mutex)
${CMAKE_CURRENT_SOURCE_DIR}/third_party/shared_mutex
${CMAKE_CURRENT_SOURCE_DIR}/amd_smi/include
/usr/include/libdrm)
set(CMN_SRC_LIST "${COMMON_SRC_DIR}/rocm_smi_device.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${COMMON_SRC_DIR}/rocm_smi_main.cc")
@@ -128,6 +132,12 @@ set(CMN_SRC_LIST ${CMN_SRC_LIST} "${COMMON_SRC_DIR}/rocm_smi_io_link.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${COMMON_SRC_DIR}/rocm_smi_gpu_metrics.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${COMMON_SRC_DIR}/rocm_smi.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${SHR_MUTEX_DIR}/shared_mutex.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_SRC_DIR}/amd_smi.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_SRC_DIR}/amd_smi_common.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_SRC_DIR}/amd_smi_gpu_device.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_SRC_DIR}/amd_smi_socket.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_SRC_DIR}/amd_smi_system.cc")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_SRC_DIR}/amd_smi_drm.cc")
set(CMN_INC_LIST "${COMMON_INC_DIR}/rocm_smi_device.h")
set(CMN_INC_LIST ${CMN_INC_LIST} "${COMMON_INC_DIR}/rocm_smi_main.h")
@@ -141,8 +151,16 @@ set(CMN_INC_LIST ${CMN_INC_LIST} "${COMMON_INC_DIR}/rocm_smi_kfd.h")
set(CMN_INC_LIST ${CMN_INC_LIST} "${COMMON_INC_DIR}/rocm_smi_io_link.h")
set(CMN_INC_LIST ${CMN_INC_LIST} "${COMMON_INC_DIR}/rocm_smi.h")
set(CMN_INC_LIST ${CMN_INC_LIST} "${SHR_MUTEX_DIR}/shared_mutex.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/amd_smi.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/impl/amd_smi_common.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/impl/amd_smi_device.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/impl/amd_smi_gpu_device.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/impl/amd_smi_socket.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/impl/amd_smi_system.h")
set(CMN_SRC_LIST ${CMN_SRC_LIST} "${AMDSMI_INC_DIR}/impl/amd_smi_drm.h")
add_subdirectory("rocm_smi")
add_subdirectory("amd_smi")
add_subdirectory("oam")
option(FILE_REORG_BACKWARD_COMPATIBILITY "Enable File Reorg with backward compatibility" ON)
Fichier exécutable
+58
Voir le fichier
@@ -0,0 +1,58 @@
#
# Minimum version of cmake required
#
message("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
message(" CMake AMD SMI (Library) ")
message("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
## Verbose output.
set(CMAKE_VERBOSE_MAKEFILE on)
# Required Defines first:
message("")
message("Build Configuration:")
message("--------Proj Src Dir: " ${PROJECT_SOURCE_DIR})
# Will still use the rocm_smi lib for now.
set(ROCM_SMI "rocm_smi")
set(ROCM_SMI_COMPONENT "lib${ROCM_SMI}")
set(ROCM_SMI_TARGET "${ROCM_SMI}64")
set(SMI_EXAMPLE_EXE "amd_smi_ex")
add_executable(${SMI_EXAMPLE_EXE} "example/amd_smi_example.cc")
target_link_libraries(${SMI_EXAMPLE_EXE} ${ROCM_SMI_TARGET})
# Generate Doxygen documentation
find_package(Doxygen)
find_package(LATEX COMPONENTS PDFLATEX)
if (DOXYGEN_FOUND AND LATEX_FOUND)
set (RSMI_MANUAL_NAME "AMD_SMI_Manual")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/amd_smi_doxygen.cfg
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/latex/refman.tex
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/docs/amd_smi_doxygen.cfg
"${AMDSMI_INC_DIR}/amd_smi.h"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/latex/refman.pdf
COMMAND make > /dev/null
COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/latex/refman.pdf
${CMAKE_CURRENT_SOURCE_DIR}/docs/${RSMI_MANUAL_NAME}_new.pdf
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/latex/refman.tex
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/latex)
add_custom_target(amdsmi_docs DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/latex/refman.pdf)
add_dependencies(${ROCM_SMI_TARGET} amdsmi_docs)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/latex/refman.pdf
DESTINATION share/doc/${ROCM_SMI} RENAME ${RSMI_MANUAL_NAME}.pdf)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../README.md
DESTINATION share/doc/${ROCM_SMI}/)
else()
message("Doxygen or Latex is not found. Will not generate documents.")
endif(DOXYGEN_FOUND AND LATEX_FOUND)
Fichier diff supprimé car celui-ci est trop grand Voir la Diff
+153
Voir le fichier
@@ -0,0 +1,153 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
#include <vector>
#include <iostream>
#include <bitset>
#include "amd_smi.h"
#define CHK_AMDSMI_RET(RET) { \
if (RET != AMDSMI_STATUS_SUCCESS) { \
const char *err_str; \
std::cout << "AMDSMI call returned " << RET \
<< " at line " << __LINE__ << std::endl; \
amdsmi_status_string(RET, &err_str); \
std::cout << err_str << std::endl; \
return RET; \
} \
}
int main() {
amdsmi_status_t ret;
// Init amdsmi for sockets and devices.
// Here we are only interested in AMD_GPUS.
ret = amdsmi_init(AMD_SMI_INIT_AMD_GPUS);
CHK_AMDSMI_RET(ret)
// Get all sockets
uint32_t socket_count = 0;
amdsmi_socket_handle* sockets = nullptr;
ret = amdsmi_get_socket_handles(&socket_count, &sockets);
CHK_AMDSMI_RET(ret)
std::cout << "Total Socket: " << socket_count << std::endl;
// For each socket, get identifier and devices
for (uint32_t i=0; i < socket_count; i++) {
// Get Socket identifier
char socket_name[128];
ret = amdsmi_get_socket_identifier(sockets[i], socket_name, 128);
CHK_AMDSMI_RET(ret)
std::cout << "Socket " << socket_name << std::endl;
// Get all devices of the socket
uint32_t device_count = 0;
amdsmi_device_handle* device_handles = nullptr;
ret = amdsmi_get_device_handles(sockets[i],
&device_count, &device_handles);
CHK_AMDSMI_RET(ret)
// For each device of the socket, get name and temperature.
for (uint32_t j=0; j < device_count; j++) {
// Get device type. Since the amdsmi is initialized with
// AMD_SMI_INIT_AMD_GPUS, the device_type must be AMD_GPU.
device_type_t device_type;
ret = amdsmi_get_device_type(device_handles[j], &device_type);
CHK_AMDSMI_RET(ret)
if (device_type != AMD_GPU) {
std::cout << "Expect AMD_GPU device type!\n";
return 1;
}
// Get device name
char name[128];
ret = amdsmi_dev_name_get(device_handles[j], name, 128);
CHK_AMDSMI_RET(ret)
std::cout << "\tdevice "
<< j <<"\n\t\tName:" << name << std::endl;
// Get temperature
int64_t val_i64 = 0;
ret = amdsmi_dev_temp_metric_get(device_handles[j], 0,
AMDSMI_TEMP_CURRENT, &val_i64);
CHK_AMDSMI_RET(ret)
std::cout << "\t\tTemperature: " << val_i64/1000 << "C" << std::endl;
// Get frame buffer
uint32_t fb_total = 0;
uint32_t fb_used = 0;
ret = amdsmi_fb_usage_get(device_handles[j], &fb_total, &fb_used);
CHK_AMDSMI_RET(ret)
std::cout << "\t\tFrame buffer usage (MB): " << fb_used << "/"
<< fb_total << std::endl;
// Get Cap info
struct smi_gpu_caps caps_info = {};
ret = amdsmi_get_caps_info(device_handles[j], &caps_info);
CHK_AMDSMI_RET(ret)
std::cout << "\t\tGFX IP Major: " << caps_info.gfx.gfxip_major << "\n";
std::cout << "\t\tGFX IP Minor: " << caps_info.gfx.gfxip_minor << "\n";
std::cout << "\t\tCU IP Count: " << caps_info.gfx.gfxip_cu_count << "\n";
std::cout << "\t\tDMA IP Count: " << caps_info.dma_ip_count << "\n";
std::cout << "\t\tGFX IP Count: " << caps_info.gfx_ip_count << "\n";
std::cout << "\t\tMM IP Count: " << int(caps_info.mm.mm_ip_count) << "\n";
std::cout << "\t\tXGMI supported: " << (caps_info.supported_fields_flags
& XGMI_FLAG ? "TRUE" : "FALSE") << "\n";
std::cout << "\t\tMM METRICS supported: " <<
(caps_info.supported_fields_flags & MM_METRICS_FLAG ? "TRUE" : "FALSE")
<< "\n";
}
}
// Clean up resources allocated at amdsmi_init. It will invalidate sockets
// and devices pointers
ret = amdsmi_shut_down();
CHK_AMDSMI_RET(ret)
return 0;
}
+3882
Voir le fichier
Fichier diff supprimé car celui-ci est trop grand Voir la Diff
+48
Voir le fichier
@@ -0,0 +1,48 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#ifndef AMD_SMI_INCLUDE_AMD_SMI_COMMON_H_
#define AMD_SMI_INCLUDE_AMD_SMI_COMMON_H_
#endif // AMD_SMI_INCLUDE_AMD_SMI_COMMON_H_
+65
Voir le fichier
@@ -0,0 +1,65 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#ifndef AMD_SMI_INCLUDE_AMD_SMI_DEVICE_H_
#define AMD_SMI_INCLUDE_AMD_SMI_DEVICE_H_
#include "amd_smi.h"
namespace amd {
namespace smi {
class AMDSmiDevice {
public:
explicit AMDSmiDevice(device_type_t device) : device_type_(device) {}
virtual ~AMDSmiDevice() {}
device_type_t get_device_type() const { return device_type_;}
private:
device_type_t device_type_;
};
} // namespace smi
} // namespace amd
#endif // AMD_SMI_INCLUDE_AMD_SMI_DEVICE_H_
+78
Voir le fichier
@@ -0,0 +1,78 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#ifndef AMD_SMI_INCLUDE_IMPL_AMD_SMI_DRM_H_
#define AMD_SMI_INCLUDE_IMPL_AMD_SMI_DRM_H_
#include <unistd.h>
#include <vector>
#include <memory>
#include <mutex> // NOLINT
#include "amd_smi.h"
namespace amd {
namespace smi {
class AMDSmiDrm {
public:
amdsmi_status_t init();
amdsmi_status_t cleanup();
int get_drm_fd_by_index(uint32_t gpu_index) const;
int amdgpu_query_info(int fd, unsigned info_id,
unsigned size, void *value);
int amdgpu_query_fw(int fd, unsigned info_id, unsigned fw_type,
unsigned size, void *value);
int amdgpu_query_hw_ip(int fd, unsigned info_id, unsigned hw_ip_type,
unsigned size, void *value);
int amdgpu_query_vbios(int fd, void *info);
private:
std::vector<int> drm_fds_; // drm file descriptor by gpu_index
std::mutex drm_mutex_;
};
} // namespace smi
} // namespace amd
#endif // AMD_SMI_INCLUDE_IMPL_AMD_SMI_DRM_H_
+76
Voir le fichier
@@ -0,0 +1,76 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#ifndef AMD_SMI_INCLUDE_IMPL_AMD_SMI_GPU_DEVICE_H_
#define AMD_SMI_INCLUDE_IMPL_AMD_SMI_GPU_DEVICE_H_
#include "amd_smi.h"
#include "impl/amd_smi_device.h"
#include "impl/amd_smi_drm.h"
namespace amd {
namespace smi {
class AMDSmiGPUDevice: public AMDSmiDevice {
public:
explicit AMDSmiGPUDevice(uint32_t gpu_id, AMDSmiDrm& drm):
AMDSmiDevice(AMD_GPU), gpu_id_(gpu_id), drm_(drm) {}
uint32_t get_gpu_id() const;
int amdgpu_query_info(unsigned info_id,
unsigned size, void *value) const;
int amdgpu_query_hw_ip(unsigned info_id, unsigned hw_ip_type,
unsigned size, void *value) const;
int amdgpu_query_fw(unsigned info_id, unsigned fw_type,
unsigned size, void *value) const;
int amdgpu_query_vbios(void *info) const;
private:
uint32_t gpu_id_;
AMDSmiDrm& drm_;
};
} // namespace smi
} // namespace amd
#endif // AMD_SMI_INCLUDE_IMPL_AMD_SMI_GPU_DEVICE_H_
+71
Voir le fichier
@@ -0,0 +1,71 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#ifndef AMD_SMI_INCLUDE_AMD_SMI_SOCKET_H_
#define AMD_SMI_INCLUDE_AMD_SMI_SOCKET_H_
#include <string>
#include <algorithm>
#include <vector>
#include "amd_smi.h"
#include "impl/amd_smi_device.h"
namespace amd {
namespace smi {
class AMDSmiSocket {
public:
explicit AMDSmiSocket(const std::string& id) : socket_identifier_(id) {}
~AMDSmiSocket();
const std::string& get_socket_id() const { return socket_identifier_;}
void add_device(AMDSmiDevice* device) { devices_.push_back(device); }
std::vector<AMDSmiDevice*>& get_devices() { return devices_;}
private:
std::string socket_identifier_;
std::vector<AMDSmiDevice*> devices_;
};
} // namespace smi
} // namespace amd
#endif // AMD_SMI_INCLUDE_AMD_SMI_SOCKET_H_
+88
Voir le fichier
@@ -0,0 +1,88 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#ifndef AMD_SMI_INCLUDE_AMD_SMI_SYSTEM_H_
#define AMD_SMI_INCLUDE_AMD_SMI_SYSTEM_H_
#include <vector>
#include <set>
#include "amd_smi.h"
#include "impl/amd_smi_socket.h"
#include "impl/amd_smi_device.h"
#include "impl/amd_smi_drm.h"
namespace amd {
namespace smi {
// Singleton: Only one system in an application
class AMDSmiSystem {
public:
static AMDSmiSystem& getInstance() {
static AMDSmiSystem instance;
return instance;
}
amdsmi_status_t init(uint64_t flags);
amdsmi_status_t cleanup();
std::vector<AMDSmiSocket*>& get_sockets() {return sockets_;}
amdsmi_status_t handle_to_socket(amdsmi_socket_handle socket_handle,
AMDSmiSocket** socket);
amdsmi_status_t handle_to_device(amdsmi_device_handle device_handle,
AMDSmiDevice** device);
private:
AMDSmiSystem() : init_flag_(AMD_SMI_INIT_ALL_DEVICES) {}
uint64_t init_flag_;
AMDSmiDrm drm_;
std::vector<AMDSmiSocket*> sockets_;
std::set<AMDSmiDevice*> devices_; // Track valid devices
};
} // namespace smi
} // namespace amd
#endif // AMD_SMI_INCLUDE_AMD_SMI_SYSTEM_H_
Fichier diff supprimé car celui-ci est trop grand Voir la Diff
+333
Voir le fichier
@@ -0,0 +1,333 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <assert.h>
#include <errno.h>
#include <sys/utsname.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <vector>
#include <set>
#include <memory>
#include <xf86drm.h>
#include "amd_smi.h"
#include "impl/amd_smi_common.h"
#include "impl/amd_smi_system.h"
#include "impl/amd_smi_socket.h"
#include "impl/amd_smi_gpu_device.h"
#include "rocm_smi/rocm_smi.h"
#include "impl/amdgpu_drm.h"
template <typename F, typename ...Args>
amdsmi_status_t rsmi_wrapper(F && f,
amdsmi_device_handle device_handle, Args &&... args) {
if (device_handle == nullptr) return AMDSMI_STATUS_INVALID_ARGS;
amd::smi::AMDSmiDevice* device = nullptr;
amdsmi_status_t r = amd::smi::AMDSmiSystem::getInstance()
.handle_to_device(device_handle, &device);
if (r != AMDSMI_STATUS_SUCCESS) return r;
if (device->get_device_type() == AMD_GPU) {
amd::smi::AMDSmiGPUDevice* gpu_device =
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
uint32_t gpu_index = gpu_device->get_gpu_id();
auto r = std::forward<F>(f)(gpu_index,
std::forward<Args>(args)...);
return static_cast<amdsmi_status_t>(r);
}
return AMDSMI_STATUS_NOT_SUPPORTED;
}
amdsmi_status_t
amdsmi_init(uint64_t flags) {
return amd::smi::AMDSmiSystem::getInstance().init(flags);
}
amdsmi_status_t
amdsmi_shut_down() {
return amd::smi::AMDSmiSystem::getInstance().cleanup();
}
amdsmi_status_t
amdsmi_status_string(amdsmi_status_t status, const char **status_string) {
return static_cast<amdsmi_status_t>(
rsmi_status_string(static_cast<rsmi_status_t>(status), status_string));
}
amdsmi_status_t amdsmi_get_socket_handles(uint32_t *socket_count,
amdsmi_socket_handle* socket_handles[]) {
if (socket_count == nullptr || socket_handles == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
std::vector<amd::smi::AMDSmiSocket*>& sockets
= amd::smi::AMDSmiSystem::getInstance().get_sockets();
*socket_count = static_cast<uint32_t>(sockets.size());
*socket_handles = reinterpret_cast<amdsmi_socket_handle*>(sockets.data());
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t amdsmi_get_socket_identifier(
amdsmi_socket_handle socket_handle,
char *name, size_t len) {
if (socket_handle == nullptr || name == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
amd::smi::AMDSmiSocket* socket = nullptr;
amdsmi_status_t r = amd::smi::AMDSmiSystem::getInstance()
.handle_to_socket(socket_handle, &socket);
if (r != AMDSMI_STATUS_SUCCESS) return r;
strncpy(name, socket->get_socket_id().c_str(), len);
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t amdsmi_get_device_handles(amdsmi_socket_handle socket_handle,
uint32_t *device_count,
amdsmi_device_handle* device_handles[]) {
if (device_count == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
amd::smi::AMDSmiSocket* socket = nullptr;
amdsmi_status_t r = amd::smi::AMDSmiSystem::getInstance()
.handle_to_socket(socket_handle, &socket);
if (r != AMDSMI_STATUS_SUCCESS) return r;
*device_count = static_cast<uint32_t>(socket->get_devices().size());
*device_handles = reinterpret_cast<amdsmi_device_handle*>(
socket->get_devices().data());
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t amdsmi_get_device_type(amdsmi_device_handle device_handle ,
device_type_t* device_type) {
if (device_type == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
amd::smi::AMDSmiDevice* device = nullptr;
amdsmi_status_t r = amd::smi::AMDSmiSystem::getInstance()
.handle_to_device(device_handle, &device);
if (r != AMDSMI_STATUS_SUCCESS) return r;
*device_type = device->get_device_type();
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t amdsmi_dev_name_get(amdsmi_device_handle device_handle,
char *name, size_t len) {
if (name == nullptr || len == 0) {
return AMDSMI_STATUS_INVALID_ARGS;
}
return rsmi_wrapper(rsmi_dev_name_get, device_handle, name, len);
}
amdsmi_status_t amdsmi_dev_temp_metric_get(amdsmi_device_handle device_handle,
uint32_t sensor_type,
amdsmi_temperature_metric_t metric, int64_t *temperature) {
if (temperature == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
return rsmi_wrapper(rsmi_dev_temp_metric_get, device_handle, sensor_type,
static_cast<rsmi_temperature_metric_t>(metric), temperature);
}
amdsmi_status_t amdsmi_fb_usage_get(amdsmi_device_handle device_handle,
uint32_t *fb_total, uint32_t *fb_used) {
if (fb_total == nullptr || fb_used == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
amd::smi::AMDSmiDevice* device = nullptr;
amdsmi_status_t r = amd::smi::AMDSmiSystem::getInstance()
.handle_to_device(device_handle, &device);
if (r != AMDSMI_STATUS_SUCCESS) return r;
if (device->get_device_type() != AMD_GPU) {
return AMDSMI_STATUS_NOT_SUPPORTED;
}
amd::smi::AMDSmiGPUDevice* gpu_device =
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
int ret = 0;
struct drm_amdgpu_info_vram_gtt gtt;
uint64_t vram_used = 0;
ret = gpu_device->amdgpu_query_info(AMDGPU_INFO_VRAM_GTT,
sizeof(struct drm_amdgpu_memory_info), &gtt);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
*fb_total = static_cast<uint32_t>(gtt.vram_size / (1024 * 1024));
ret = gpu_device->amdgpu_query_info(AMDGPU_INFO_VRAM_USAGE,
sizeof(vram_used), &vram_used);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
*fb_used = static_cast<uint32_t>(vram_used / (1024 * 1024));
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t amdsmi_get_caps_info(amdsmi_device_handle device_handle,
struct smi_gpu_caps *info) {
if (info == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
amd::smi::AMDSmiDevice* amd_device = nullptr;
amdsmi_status_t r = amd::smi::AMDSmiSystem::getInstance()
.handle_to_device(device_handle, &amd_device);
if (r != AMDSMI_STATUS_SUCCESS) return r;
if (amd_device->get_device_type() != AMD_GPU) {
return AMDSMI_STATUS_NOT_SUPPORTED;
}
amd::smi::AMDSmiGPUDevice* gpu_device =
static_cast<amd::smi::AMDSmiGPUDevice*>(device_handle);
unsigned uvd, vce, uvd_enc, vcn_enc;
struct drm_amdgpu_info_hw_ip ip;
struct drm_amdgpu_info_device device;
unsigned count, j;
int ret = gpu_device->amdgpu_query_info(AMDGPU_INFO_DEV_INFO,
sizeof(struct drm_amdgpu_info_device), &device);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
info->gfx.gfxip_cu_count = device.cu_active_number;
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_INFO,
AMDGPU_HW_IP_GFX, sizeof(ip), &ip);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
info->gfx.gfxip_major = ip.hw_ip_version_major;
info->gfx.gfxip_minor = ip.hw_ip_version_minor;
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_COUNT,
AMDGPU_HW_IP_GFX, sizeof(unsigned), &count);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
info->gfx_ip_count = count;
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_COUNT,
AMDGPU_HW_IP_DMA, sizeof(unsigned), &count);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
info->dma_ip_count = count;
count = 0;
/* Count multimedia engines */
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_COUNT,
AMDGPU_HW_IP_UVD, sizeof(struct drm_amdgpu_info_device), &uvd);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
for (j = 0; j < uvd; j++)
info->mm.mm_ip_list[count++] = MM_UVD;
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_COUNT,
AMDGPU_HW_IP_UVD_ENC, sizeof(struct drm_amdgpu_info_device), &uvd_enc);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
for (j = 0; j < uvd_enc; j++)
info->mm.mm_ip_list[count++] = MM_UVD;
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_COUNT,
AMDGPU_HW_IP_VCE, sizeof(struct drm_amdgpu_info_device), &vce);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
for (j = 0; j < vce; j++)
info->mm.mm_ip_list[count++] = MM_VCE;
/* VCN is shared DEC/ENC check only ENC */
ret = gpu_device->amdgpu_query_hw_ip(AMDGPU_INFO_HW_IP_COUNT,
AMDGPU_HW_IP_VCN_ENC, sizeof(struct drm_amdgpu_info_device),
&vcn_enc);
if (ret) {
return AMDSMI_STATUS_INTERNAL_EXCEPTION;
}
for (j = 0; j < vcn_enc; j++)
info->mm.mm_ip_list[count++] = MM_VCN;
info->mm.mm_ip_count = static_cast<uint8_t>(count);
info->supported_fields_flags = 0;
info->supported_fields_flags |= POWER_GFX_VOLTAGE_FLAG;
info->supported_fields_flags |= MEM_USAGE_FLAG;
info->supported_fields_flags |= MM_METRICS_FLAG;
info->ras_supported = false;
return AMDSMI_STATUS_SUCCESS;
}
+52
Voir le fichier
@@ -0,0 +1,52 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <functional>
#include "impl/amd_smi_common.h"
namespace amd {
namespace smi {
} // namespace smi
} // namespace amd
+160
Voir le fichier
@@ -0,0 +1,160 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <xf86drm.h>
#include <string.h>
#include <memory>
#include "impl/amd_smi_drm.h"
#include "impl/amdgpu_drm.h"
namespace amd {
namespace smi {
amdsmi_status_t AMDSmiDrm::init() {
// A few RAII handler
using dir_ptr = std::unique_ptr<DIR, decltype(&closedir)>;
using drm_version_ptr = std::unique_ptr<drmVersion,
decltype(&drmFreeVersion)>;
struct dirent *dir = nullptr;
int fd = -1;
auto d = dir_ptr(opendir("/dev/dri/"), &closedir);
if (d == nullptr) return AMDSMI_STATUS_INIT_ERROR;
while ((dir = readdir(d.get())) != NULL) {
char* name_cstr = new char[sizeof(dir->d_name) + 10];
auto name = std::unique_ptr<char[]>(name_cstr);
snprintf(name.get(), sizeof(dir->d_name) + 10,
"/dev/dri/%s", dir->d_name);
fd = open(name.get(), O_RDWR | O_CLOEXEC);
if (fd < 0) continue;
auto version = drm_version_ptr(drmGetVersion(fd), &drmFreeVersion);
if (strcmp("amdgpu", version->name) ||
strstr(name.get(), "render") == nullptr) {
close(fd);
continue;
}
drm_fds_.push_back(fd);
}
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t AMDSmiDrm::cleanup() {
for (unsigned int i=0; i < drm_fds_.size(); i++) {
close(drm_fds_[i]);
}
drm_fds_.clear();
return AMDSMI_STATUS_SUCCESS;
}
int AMDSmiDrm::amdgpu_query_info(int fd, unsigned info_id,
unsigned size, void *value) {
std::lock_guard<std::mutex> guard(drm_mutex_);
struct drm_amdgpu_info request;
memset(&request, 0, sizeof(request));
request.return_pointer = (uintptr_t)value;
request.return_size = size;
request.query = info_id;
return drmCommandWrite(fd, DRM_AMDGPU_INFO,
&request, sizeof(struct drm_amdgpu_info));
}
int AMDSmiDrm::amdgpu_query_fw(int fd, unsigned info_id, unsigned fw_type,
unsigned size, void *value) {
std::lock_guard<std::mutex> guard(drm_mutex_);
struct drm_amdgpu_info request;
memset(&request, 0, sizeof(request));
request.return_pointer = (uintptr_t)value;
request.return_size = size;
request.query = info_id;
request.query_fw.fw_type = fw_type;
return drmCommandWrite(fd, DRM_AMDGPU_INFO, &request,
sizeof(struct drm_amdgpu_info));
}
int AMDSmiDrm::amdgpu_query_hw_ip(int fd, unsigned info_id, unsigned hw_ip_type,
unsigned size, void *value) {
std::lock_guard<std::mutex> guard(drm_mutex_);
struct drm_amdgpu_info request;
memset(&request, 0, sizeof(request));
request.return_pointer = (uintptr_t)value;
request.return_size = size;
request.query = info_id;
request.query_hw_ip.type = hw_ip_type;
return drmCommandWrite(fd, DRM_AMDGPU_INFO, &request,
sizeof(struct drm_amdgpu_info));
}
int AMDSmiDrm::amdgpu_query_vbios(int fd, void *info) {
std::lock_guard<std::mutex> guard(drm_mutex_);
struct drm_amdgpu_info request;
memset(&request, 0, sizeof request);
request.return_pointer = (uint64_t) info;
request.return_size = sizeof(drm_amdgpu_info_vbios);
request.query = AMDGPU_INFO_VBIOS;
request.vbios_info.type = AMDGPU_INFO_VBIOS_INFO;
return drmCommandWrite(fd, DRM_AMDGPU_INFO, &request,
sizeof(struct drm_amdgpu_info));
}
int AMDSmiDrm::get_drm_fd_by_index(uint32_t gpu_index) const {
if (gpu_index + 1 > drm_fds_.size()) return -1;
return drm_fds_[gpu_index];
}
} // namespace smi
} // namespace amd
+86
Voir le fichier
@@ -0,0 +1,86 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <functional>
#include "impl/amd_smi_gpu_device.h"
namespace amd {
namespace smi {
uint32_t AMDSmiGPUDevice::get_gpu_id() const {
return gpu_id_;
}
int AMDSmiGPUDevice::amdgpu_query_info(unsigned info_id,
unsigned size, void *value) const {
int fd = drm_.get_drm_fd_by_index(gpu_id_);
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
return drm_.amdgpu_query_info(fd, info_id, size, value);
}
int AMDSmiGPUDevice::amdgpu_query_hw_ip(unsigned info_id,
unsigned hw_ip_type, unsigned size, void *value) const {
int fd = drm_.get_drm_fd_by_index(gpu_id_);
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
return drm_.amdgpu_query_hw_ip(fd, info_id, hw_ip_type, size, value);
}
int AMDSmiGPUDevice::amdgpu_query_fw(unsigned info_id, unsigned fw_type,
unsigned size, void *value) const {
int fd = drm_.get_drm_fd_by_index(gpu_id_);
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
return drm_.amdgpu_query_fw(fd, info_id, fw_type, size, value);
}
int AMDSmiGPUDevice::amdgpu_query_vbios(void *info) const {
int fd = drm_.get_drm_fd_by_index(gpu_id_);
if (fd == -1) return AMDSMI_STATUS_NOT_SUPPORTED;
return drm_.amdgpu_query_vbios(fd, info);
}
} // namespace smi
} // namespace amd
+60
Voir le fichier
@@ -0,0 +1,60 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <functional>
#include "impl/amd_smi_socket.h"
namespace amd {
namespace smi {
AMDSmiSocket::~AMDSmiSocket() {
for (uint32_t i = 0; i < devices_.size(); i++) {
delete devices_[i];
}
devices_.clear();
}
} // namespace smi
} // namespace amd
+162
Voir le fichier
@@ -0,0 +1,162 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <sstream>
#include <iomanip>
#include "impl/amd_smi_system.h"
#include "impl/amd_smi_gpu_device.h"
#include "rocm_smi/rocm_smi.h"
namespace amd {
namespace smi {
amdsmi_status_t AMDSmiSystem::init(uint64_t flags) {
init_flag_ = flags;
// populate sockets and devices
if (flags & AMD_SMI_INIT_AMD_GPUS) {
drm_.init();
// init rsmi
rsmi_status_t ret = rsmi_init(flags);
if (ret != RSMI_STATUS_SUCCESS) {
return static_cast<amdsmi_status_t>(ret);
}
uint32_t device_count = 0;
ret = rsmi_num_monitor_devices(&device_count);
if (ret != RSMI_STATUS_SUCCESS) {
return static_cast<amdsmi_status_t>(ret);
}
for (uint32_t i=0; i < device_count; i++) {
uint64_t bdfid = 0;
ret = rsmi_dev_pci_id_get(i, &bdfid);
if (ret != RSMI_STATUS_SUCCESS) {
return static_cast<amdsmi_status_t>(ret);
}
uint64_t domain = (bdfid >> 32) & 0xffffffff;
uint64_t bus = (bdfid >> 8) & 0xff;
uint64_t device_id = (bdfid >> 3) & 0x1f;
uint64_t function = bdfid & 0x7;
std::stringstream ss;
ss << std::setfill('0') << std::uppercase << std::hex
<< std::setw(4) << domain << ":" << std::setw(2) << bus << ":"
<< std::setw(2) << device_id << "." << std::setw(2) << function;
// Multiple devices may share the same socket
auto socket_id = ss.str();
AMDSmiSocket* socket = nullptr;
for (unsigned int j=0; j < sockets_.size(); j++) {
if (sockets_[j]->get_socket_id() == socket_id) {
socket = sockets_[j];
break;
}
}
if (socket == nullptr) {
socket = new AMDSmiSocket(ss.str());
sockets_.push_back(socket);
}
AMDSmiDevice* device = new AMDSmiGPUDevice(i, drm_);
socket->add_device(device);
devices_.insert(device);
}
} else {
return AMDSMI_STATUS_NOT_SUPPORTED;
}
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t AMDSmiSystem::cleanup() {
for (uint32_t i = 0; i < sockets_.size(); i++) {
delete sockets_[i];
}
devices_.clear();
sockets_.clear();
init_flag_ = AMD_SMI_INIT_ALL_DEVICES;
rsmi_status_t ret = rsmi_shut_down();
if (ret != RSMI_STATUS_SUCCESS) {
return static_cast<amdsmi_status_t>(ret);
}
drm_.cleanup();
return AMDSMI_STATUS_SUCCESS;
}
amdsmi_status_t AMDSmiSystem::handle_to_socket(
amdsmi_socket_handle socket_handle,
AMDSmiSocket** socket) {
if (socket_handle == nullptr || socket == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
*socket = static_cast<AMDSmiSocket*>(socket_handle);
// double check handlers is here
if (std::find(sockets_.begin(), sockets_.end(), *socket)
!= sockets_.end()) {
return AMDSMI_STATUS_SUCCESS;
}
return AMDSMI_STATUS_INVALID_ARGS;
}
amdsmi_status_t AMDSmiSystem::handle_to_device(
amdsmi_device_handle device_handle,
AMDSmiDevice** device) {
if (device_handle == nullptr || device == nullptr) {
return AMDSMI_STATUS_INVALID_ARGS;
}
*device = static_cast<AMDSmiDevice*>(device_handle);
// double check handlers is here
if (std::find(devices_.begin(), devices_.end(), *device)
!= devices_.end()) {
return AMDSMI_STATUS_SUCCESS;
}
return AMDSMI_STATUS_INVALID_ARGS;
}
} // namespace smi
} // namespace amd
+1 -1
Voir le fichier
@@ -72,7 +72,7 @@ target_include_directories(${OAM_EXAMPLE_EXE} PRIVATE ${OAM_INC_LIST})
target_link_libraries(${OAM_EXAMPLE_EXE} ${OAM_TARGET})
add_library(${OAM_TARGET} ${CMN_SRC_LIST} ${OAM_SRC_LIST}
${CMN_INC_LIST} ${OAM_INC_LIST})
target_link_libraries(${OAM_TARGET} pthread rt)
target_link_libraries(${OAM_TARGET} pthread rt drm)
target_include_directories(${OAM_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include ${COMMON_PROJ_ROOT}/common/shared_mutex)
+1 -1
Voir le fichier
@@ -76,7 +76,7 @@ add_executable(${SMI_EXAMPLE_EXE} "example/rocm_smi_example.cc")
target_link_libraries(${SMI_EXAMPLE_EXE} ${ROCM_SMI_TARGET})
add_library(${ROCM_SMI_TARGET} ${CMN_SRC_LIST} ${SMI_SRC_LIST}
${CMN_INC_LIST} ${SMI_INC_LIST})
target_link_libraries(${ROCM_SMI_TARGET} pthread rt)
target_link_libraries(${ROCM_SMI_TARGET} pthread rt drm)
target_include_directories(${ROCM_SMI_TARGET} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR} ${COMMON_PROJ_ROOT}/common/shared_mutex)