2023-07-09 13:27:58 +00:00
/*
* Copyright ( C ) 2023 Intel Corporation
*
* SPDX - License - Identifier : MIT
*
*/
2023-07-17 15:34:01 +00:00
# include "level_zero/sysman/source/shared/linux/sysman_kmd_interface.h"
2023-07-09 13:27:58 +00:00
2023-08-14 16:02:44 +05:30
# include "shared/source/debug_settings/debug_settings_manager.h"
2023-07-21 11:52:19 +00:00
# include "shared/source/execution_environment/root_device_environment.h"
# include "shared/source/helpers/hw_info.h"
2023-07-09 13:27:58 +00:00
# include "shared/source/os_interface/linux/drm_neo.h"
2023-08-14 16:02:44 +05:30
# include "shared/source/os_interface/linux/engine_info.h"
2023-07-25 18:10:45 +00:00
2023-11-08 14:43:37 +00:00
# include "level_zero/sysman/source/shared/linux/pmu/sysman_pmu_imp.h"
2023-08-09 09:03:06 +00:00
# include "level_zero/sysman/source/shared/linux/sysman_fs_access_interface.h"
2023-07-09 13:27:58 +00:00
2023-10-30 07:01:53 +00:00
# include "drm/i915_drm.h"
2023-07-09 13:27:58 +00:00
namespace L0 {
namespace Sysman {
2023-10-30 07:01:53 +00:00
const std : : string deviceDir ( " device " ) ;
const std : : string sysDevicesDir ( " /sys/devices/ " ) ;
2023-07-09 13:27:58 +00:00
2023-11-22 09:39:57 +05:30
const std : : map < uint16_t , std : : string > SysmanKmdInterfaceI915 : : i915EngineClassToSysfsEngineMap = {
2023-08-14 16:02:44 +05:30
{ drm_i915_gem_engine_class : : I915_ENGINE_CLASS_RENDER , " rcs " } ,
2023-11-22 09:39:57 +05:30
{ static_cast < uint16_t > ( drm_i915_gem_engine_class : : I915_ENGINE_CLASS_COMPUTE ) , " ccs " } ,
2023-08-14 16:02:44 +05:30
{ drm_i915_gem_engine_class : : I915_ENGINE_CLASS_COPY , " bcs " } ,
{ drm_i915_gem_engine_class : : I915_ENGINE_CLASS_VIDEO , " vcs " } ,
{ drm_i915_gem_engine_class : : I915_ENGINE_CLASS_VIDEO_ENHANCE , " vecs " } } ;
static const std : : multimap < zes_engine_type_flag_t , std : : string > level0EngineTypeToSysfsEngineMap = {
{ ZES_ENGINE_TYPE_FLAG_RENDER , " rcs " } ,
{ ZES_ENGINE_TYPE_FLAG_COMPUTE , " ccs " } ,
{ ZES_ENGINE_TYPE_FLAG_DMA , " bcs " } ,
{ ZES_ENGINE_TYPE_FLAG_MEDIA , " vcs " } ,
{ ZES_ENGINE_TYPE_FLAG_OTHER , " vecs " } } ;
2023-11-22 09:39:57 +05:30
SysmanKmdInterface : : SysmanKmdInterface ( ) = default ;
SysmanKmdInterface : : ~ SysmanKmdInterface ( ) = default ;
2023-08-09 09:03:06 +00:00
2023-11-22 09:39:57 +05:30
std : : unique_ptr < SysmanKmdInterface > SysmanKmdInterface : : create ( NEO : : Drm & drm ) {
2023-07-09 13:27:58 +00:00
std : : unique_ptr < SysmanKmdInterface > pSysmanKmdInterface ;
auto drmVersion = drm . getDrmVersion ( drm . getFileDescriptor ( ) ) ;
2023-07-21 11:52:19 +00:00
auto pHwInfo = drm . getRootDeviceEnvironment ( ) . getHardwareInfo ( ) ;
const auto productFamily = pHwInfo - > platform . eProductFamily ;
2023-07-09 13:27:58 +00:00
if ( " xe " = = drmVersion ) {
2023-07-21 11:52:19 +00:00
pSysmanKmdInterface = std : : make_unique < SysmanKmdInterfaceXe > ( productFamily ) ;
2023-07-09 13:27:58 +00:00
} else {
2023-11-22 09:39:57 +05:30
std : : string prelimVersion ;
drm . getPrelimVersion ( prelimVersion ) ;
if ( prelimVersion = = " " ) {
pSysmanKmdInterface = std : : make_unique < SysmanKmdInterfaceI915Upstream > ( productFamily ) ;
} else {
pSysmanKmdInterface = std : : make_unique < SysmanKmdInterfaceI915Prelim > ( productFamily ) ;
}
2023-07-09 13:27:58 +00:00
}
2023-07-21 11:52:19 +00:00
2023-07-09 13:27:58 +00:00
return pSysmanKmdInterface ;
}
2023-11-30 16:56:59 +00:00
ze_result_t SysmanKmdInterface : : initFsAccessInterface ( const NEO : : Drm & drm ) {
2023-10-30 07:01:53 +00:00
pFsAccess = FsAccessInterface : : create ( ) ;
pProcfsAccess = ProcFsAccessInterface : : create ( ) ;
std : : string deviceName ;
2023-11-30 16:56:59 +00:00
auto result = pProcfsAccess - > getFileName ( pProcfsAccess - > myProcessId ( ) , drm . getFileDescriptor ( ) , deviceName ) ;
if ( result ! = ZE_RESULT_SUCCESS ) {
NEO : : printDebugString ( NEO : : debugManager . flags . PrintDebugMessages . get ( ) , stderr , " Error@ %s(): Failed to device name and returning error:0x%x \n " , __FUNCTION__ , result ) ;
return result ;
}
2023-10-30 07:01:53 +00:00
pSysfsAccess = SysFsAccessInterface : : create ( deviceName ) ;
2023-11-30 16:56:59 +00:00
return result ;
2023-10-30 07:01:53 +00:00
}
2023-08-09 09:03:06 +00:00
2023-10-30 07:01:53 +00:00
FsAccessInterface * SysmanKmdInterface : : getFsAccess ( ) {
2023-08-09 09:03:06 +00:00
UNRECOVERABLE_IF ( nullptr = = pFsAccess . get ( ) ) ;
return pFsAccess . get ( ) ;
}
ProcFsAccessInterface * SysmanKmdInterface : : getProcFsAccess ( ) {
UNRECOVERABLE_IF ( nullptr = = pProcfsAccess . get ( ) ) ;
return pProcfsAccess . get ( ) ;
}
2023-10-30 07:01:53 +00:00
SysFsAccessInterface * SysmanKmdInterface : : getSysFsAccess ( ) {
2023-08-09 09:03:06 +00:00
UNRECOVERABLE_IF ( nullptr = = pSysfsAccess . get ( ) ) ;
return pSysfsAccess . get ( ) ;
}
2023-11-22 09:39:57 +05:30
ze_result_t SysmanKmdInterface : : getNumEngineTypeAndInstancesForDevice ( std : : string engineDir , std : : map < zes_engine_type_flag_t , std : : vector < std : : string > > & mapOfEngines ,
SysFsAccessInterface * pSysfsAccess ) {
2023-08-14 16:02:44 +05:30
std : : vector < std : : string > localListOfAllEngines = { } ;
auto result = pSysfsAccess - > scanDirEntries ( engineDir , localListOfAllEngines ) ;
if ( ZE_RESULT_SUCCESS ! = result ) {
if ( result = = ZE_RESULT_ERROR_NOT_AVAILABLE ) {
result = ZE_RESULT_ERROR_UNSUPPORTED_FEATURE ;
}
2023-11-30 08:32:25 +00:00
NEO : : printDebugString ( NEO : : debugManager . flags . PrintDebugMessages . get ( ) , stderr , " Error@ %s(): Failed to scan directory entries to list all engines and returning error:0x%x \n " , __FUNCTION__ , result ) ;
2023-08-14 16:02:44 +05:30
return result ;
}
for_each ( localListOfAllEngines . begin ( ) , localListOfAllEngines . end ( ) ,
[ & ] ( std : : string & mappedEngine ) {
for ( auto itr = level0EngineTypeToSysfsEngineMap . begin ( ) ; itr ! = level0EngineTypeToSysfsEngineMap . end ( ) ; itr + + ) {
char digits [ ] = " 0123456789 " ;
auto mappedEngineName = mappedEngine . substr ( 0 , mappedEngine . find_first_of ( digits , 0 ) ) ;
if ( 0 = = mappedEngineName . compare ( itr - > second . c_str ( ) ) ) {
auto ret = mapOfEngines . find ( itr - > first ) ;
if ( ret ! = mapOfEngines . end ( ) ) {
ret - > second . push_back ( mappedEngine ) ;
} else {
std : : vector < std : : string > engineVec = { } ;
engineVec . push_back ( mappedEngine ) ;
mapOfEngines . emplace ( itr - > first , engineVec ) ;
}
}
}
} ) ;
return result ;
}
SysmanKmdInterface : : SysfsValueUnit SysmanKmdInterface : : getNativeUnit ( const SysfsName sysfsName ) {
auto sysfsNameToNativeUnitMap = getSysfsNameToNativeUnitMap ( ) ;
if ( sysfsNameToNativeUnitMap . find ( sysfsName ) ! = sysfsNameToNativeUnitMap . end ( ) ) {
return sysfsNameToNativeUnitMap [ sysfsName ] ;
}
// Entries are expected to be available at sysfsNameToNativeUnitMap
DEBUG_BREAK_IF ( true ) ;
return unAvailable ;
}
void SysmanKmdInterface : : convertSysfsValueUnit ( const SysfsValueUnit dstUnit , const SysfsValueUnit srcUnit , const uint64_t srcValue , uint64_t & dstValue ) const {
dstValue = srcValue ;
if ( dstUnit ! = srcUnit ) {
if ( dstUnit = = SysfsValueUnit : : milliSecond & & srcUnit = = SysfsValueUnit : : microSecond ) {
dstValue = srcValue / 1000u ;
} else if ( dstUnit = = SysfsValueUnit : : microSecond & & srcUnit = = SysfsValueUnit : : milliSecond ) {
dstValue = srcValue * 1000u ;
}
}
}
2023-10-30 07:01:53 +00:00
uint32_t SysmanKmdInterface : : getEventTypeImpl ( std : : string & dirName , const bool isIntegratedDevice ) {
auto pSysFsAccess = getSysFsAccess ( ) ;
auto pFsAccess = getFsAccess ( ) ;
if ( ! isIntegratedDevice ) {
std : : string bdfDir ;
ze_result_t result = pSysFsAccess - > readSymLink ( deviceDir , bdfDir ) ;
if ( ZE_RESULT_SUCCESS ! = result ) {
return 0 ;
}
const auto loc = bdfDir . find_last_of ( ' / ' ) ;
auto bdf = bdfDir . substr ( loc + 1 ) ;
std : : replace ( bdf . begin ( ) , bdf . end ( ) , ' : ' , ' _ ' ) ;
dirName = dirName + " _ " + bdf ;
}
const std : : string eventTypeSysfsNode = sysDevicesDir + dirName + " / " + " type " ;
auto eventTypeVal = 0u ;
if ( ZE_RESULT_SUCCESS ! = pFsAccess - > read ( eventTypeSysfsNode , eventTypeVal ) ) {
return 0 ;
}
return eventTypeVal ;
}
2023-11-22 09:39:57 +05:30
std : : string SysmanKmdInterfaceI915 : : getBasePathI915 ( uint32_t subDeviceId ) {
return " gt/gt " + std : : to_string ( subDeviceId ) + " / " ;
2023-10-30 07:01:53 +00:00
}
2023-11-22 09:39:57 +05:30
std : : string SysmanKmdInterfaceI915 : : getHwmonNameI915 ( uint32_t subDeviceId , bool isSubdevice ) {
std : : string filePath = isSubdevice ? " i915_gt " + std : : to_string ( subDeviceId ) : " i915 " ;
return filePath ;
}
std : : string SysmanKmdInterfaceI915 : : getEngineBasePathI915 ( uint32_t subDeviceId ) {
return " engine " ;
}
std : : optional < std : : string > SysmanKmdInterfaceI915 : : getEngineClassStringI915 ( uint16_t engineClass ) {
auto sysfEngineString = i915EngineClassToSysfsEngineMap . find ( static_cast < drm_i915_gem_engine_class > ( engineClass ) ) ;
if ( sysfEngineString = = i915EngineClassToSysfsEngineMap . end ( ) ) {
DEBUG_BREAK_IF ( true ) ;
return { } ;
}
return sysfEngineString - > second ;
2023-10-30 07:01:53 +00:00
}
2023-07-09 13:27:58 +00:00
} // namespace Sysman
2023-08-14 16:02:44 +05:30
} // namespace L0