2021-12-01 19:45:29 +08:00
/*
2025-03-04 21:40:44 +08:00
* Copyright ( C ) 2021 - 2025 Intel Corporation
2021-12-01 19:45:29 +08:00
*
* SPDX - License - Identifier : MIT
*
*/
# include "shared/source/os_interface/linux/ioctl_helper.h"
2022-05-18 03:04:23 +08:00
# include "shared/source/debug_settings/debug_settings_manager.h"
2022-05-31 02:05:42 +08:00
# include "shared/source/execution_environment/root_device_environment.h"
2023-04-18 16:54:04 +08:00
# include "shared/source/helpers/compiler_product_helper.h"
2022-05-31 02:05:42 +08:00
# include "shared/source/helpers/hw_info.h"
2025-03-04 21:40:44 +08:00
# include "shared/source/memory_manager/allocation_properties.h"
2024-11-08 21:42:15 +08:00
# include "shared/source/memory_manager/gfx_partition.h"
# include "shared/source/os_interface/linux/drm_allocation.h"
# include "shared/source/os_interface/linux/drm_memory_manager.h"
2021-12-01 19:45:29 +08:00
# include "shared/source/os_interface/linux/drm_neo.h"
2022-05-18 01:16:13 +08:00
# include "shared/source/os_interface/linux/drm_wrappers.h"
2025-04-09 06:05:33 +08:00
# include "shared/source/os_interface/linux/file_descriptor.h"
2024-09-21 01:16:45 +08:00
# include "shared/source/os_interface/linux/os_context_linux.h"
2024-03-09 09:22:47 +08:00
# include "shared/source/os_interface/linux/sys_calls.h"
2023-12-28 00:23:42 +08:00
2024-03-28 21:31:57 +08:00
# include "drm.h"
2021-12-01 19:45:29 +08:00
2022-11-18 22:38:56 +08:00
# include <fcntl.h>
2022-05-18 03:04:23 +08:00
# include <sstream>
2021-12-01 19:45:29 +08:00
namespace NEO {
2024-12-18 17:45:58 +08:00
std : : optional < std : : function < std : : unique_ptr < IoctlHelper > ( Drm & drm ) > > ioctlHelperFactory [ IGFX_MAX_PRODUCT ] = { } ;
2024-09-04 01:20:54 +08:00
void IoctlHelper : : setExternalContext ( ExternalCtx * ctx ) {
externalCtx = ctx ;
}
2023-01-19 04:22:32 +08:00
int IoctlHelper : : ioctl ( DrmIoctl request , void * arg ) {
2024-09-04 01:20:54 +08:00
if ( externalCtx ) {
return externalCtx - > ioctl ( externalCtx - > handle , drm . getFileDescriptor ( ) , getIoctlRequestValue ( request ) , arg , false ) ;
}
2022-06-30 00:49:29 +08:00
return drm . ioctl ( request , arg ) ;
2021-12-01 19:45:29 +08:00
}
2024-03-09 09:22:47 +08:00
int IoctlHelper : : ioctl ( int fd , DrmIoctl request , void * arg ) {
return NEO : : SysCalls : : ioctl ( fd , getIoctlRequestValue ( request ) , arg ) ;
}
2023-05-22 23:15:17 +08:00
void IoctlHelper : : setupIpVersion ( ) {
auto & rootDeviceEnvironment = drm . getRootDeviceEnvironment ( ) ;
auto & hwInfo = * rootDeviceEnvironment . getMutableHardwareInfo ( ) ;
2023-04-18 16:54:04 +08:00
auto & compilerProductHelper = rootDeviceEnvironment . getHelper < CompilerProductHelper > ( ) ;
hwInfo . ipVersion . value = compilerProductHelper . getHwIpVersion ( hwInfo ) ;
2023-05-22 23:15:17 +08:00
}
2022-07-27 02:11:27 +08:00
uint32_t IoctlHelper : : getFlagsForPrimeHandleToFd ( ) const {
return DRM_CLOEXEC | DRM_RDWR ;
}
2025-04-09 06:05:33 +08:00
void IoctlHelper : : writeCcsMode ( const std : : string & gtFile , uint32_t ccsMode ,
std : : vector < std : : tuple < std : : string , uint32_t > > & deviceCcsModeVec ) {
std : : string ccsFile = gtFile + " /ccs_mode " ;
auto fd = FileDescriptor ( ccsFile . c_str ( ) , O_RDWR ) ;
if ( fd < 0 ) {
if ( ( errno = = - EACCES ) | | ( errno = = - EPERM ) ) {
fprintf ( stderr , " No read and write permissions for %s, System administrator needs to grant permissions to allow modification of this file from user space \n " , ccsFile . c_str ( ) ) ;
fprintf ( stdout , " No read and write permissions for %s, System administrator needs to grant permissions to allow modification of this file from user space \n " , ccsFile . c_str ( ) ) ;
}
return ;
}
uint32_t ccsValue = 0 ;
ssize_t ret = SysCalls : : read ( fd , & ccsValue , sizeof ( uint32_t ) ) ;
PRINT_DEBUG_STRING ( debugManager . flags . PrintDebugMessages . get ( ) & & ( ret < 0 ) , stderr , " read() on %s failed errno = %d | ret = %d \n " ,
ccsFile . c_str ( ) , errno , ret ) ;
if ( ( ret < 0 ) | | ( ccsValue = = ccsMode ) ) {
return ;
}
do {
ret = SysCalls : : write ( fd , & ccsMode , sizeof ( uint32_t ) ) ;
} while ( ret = = - 1 & & errno = = - EBUSY ) ;
if ( ret > 0 ) {
deviceCcsModeVec . emplace_back ( ccsFile , ccsValue ) ;
}
} ;
2022-06-02 02:53:00 +08:00
unsigned int IoctlHelper : : getIoctlRequestValueBase ( DrmIoctl ioctlRequest ) const {
switch ( ioctlRequest ) {
2023-12-12 16:48:32 +08:00
case DrmIoctl : : gemClose :
2022-06-02 02:53:00 +08:00
return DRM_IOCTL_GEM_CLOSE ;
2023-12-12 16:48:32 +08:00
case DrmIoctl : : primeFdToHandle :
2022-06-02 02:53:00 +08:00
return DRM_IOCTL_PRIME_FD_TO_HANDLE ;
2023-12-12 16:48:32 +08:00
case DrmIoctl : : primeHandleToFd :
2022-06-02 02:53:00 +08:00
return DRM_IOCTL_PRIME_HANDLE_TO_FD ;
default :
UNRECOVERABLE_IF ( true ) ;
return 0u ;
}
}
2022-07-14 23:32:26 +08:00
std : : string IoctlHelper : : getIoctlStringBase ( DrmIoctl ioctlRequest ) const {
2022-06-06 23:48:31 +08:00
switch ( ioctlRequest ) {
2023-12-12 16:48:32 +08:00
case DrmIoctl : : gemClose :
2022-06-06 23:48:31 +08:00
return " DRM_IOCTL_GEM_CLOSE " ;
2023-12-12 16:48:32 +08:00
case DrmIoctl : : primeFdToHandle :
2022-06-06 23:48:31 +08:00
return " DRM_IOCTL_PRIME_FD_TO_HANDLE " ;
2023-12-12 16:48:32 +08:00
case DrmIoctl : : primeHandleToFd :
2022-06-06 23:48:31 +08:00
return " DRM_IOCTL_PRIME_HANDLE_TO_FD " ;
2022-07-14 23:32:26 +08:00
default :
UNRECOVERABLE_IF ( true ) ;
return " " ;
2022-06-06 23:48:31 +08:00
}
}
2022-10-19 02:37:33 +08:00
bool IoctlHelper : : checkIfIoctlReinvokeRequired ( int error , DrmIoctl ioctlRequest ) const {
return ( error = = EINTR | | error = = EAGAIN | | error = = EBUSY | | error = = - EBUSY ) ;
}
2024-09-21 01:16:45 +08:00
uint64_t * IoctlHelper : : getPagingFenceAddress ( uint32_t vmHandleId , OsContextLinux * osContext ) {
if ( osContext ) {
return osContext - > getFenceAddr ( vmHandleId ) ;
} else {
return drm . getFenceAddr ( vmHandleId ) ;
}
}
2024-11-08 21:42:15 +08:00
uint64_t IoctlHelper : : acquireGpuRange ( DrmMemoryManager & memoryManager , size_t & size , uint32_t rootDeviceIndex , HeapIndex heapIndex ) {
if ( heapIndex > = HeapIndex : : totalHeaps ) {
return 0 ;
}
return memoryManager . acquireGpuRange ( size , rootDeviceIndex , heapIndex ) ;
}
void IoctlHelper : : releaseGpuRange ( DrmMemoryManager & memoryManager , void * address , size_t size , uint32_t rootDeviceIndex ) {
memoryManager . releaseGpuRange ( address , size , rootDeviceIndex ) ;
}
void * IoctlHelper : : mmapFunction ( DrmMemoryManager & memoryManager , void * ptr , size_t size , int prot , int flags , int fd , off_t offset ) {
return memoryManager . mmapFunction ( ptr , size , prot , flags , fd , offset ) ;
}
int IoctlHelper : : munmapFunction ( DrmMemoryManager & memoryManager , void * ptr , size_t size ) {
return memoryManager . munmapFunction ( ptr , size ) ;
}
void IoctlHelper : : registerMemoryToUnmap ( DrmAllocation & allocation , void * pointer , size_t size , DrmAllocation : : MemoryUnmapFunction unmapFunction ) {
return allocation . registerMemoryToUnmap ( pointer , size , unmapFunction ) ;
}
2024-11-23 01:02:47 +08:00
BufferObject * IoctlHelper : : allocUserptr ( DrmMemoryManager & memoryManager , const AllocationData & allocData , uintptr_t address , size_t size , uint32_t rootDeviceIndex ) {
2025-03-04 21:40:44 +08:00
return memoryManager . allocUserptr ( address , size , allocData . type , rootDeviceIndex ) ;
2024-11-16 04:48:49 +08:00
}
2021-12-01 19:45:29 +08:00
} // namespace NEO