2021-07-17 18:28:04 +02:00
// SPDX-License-Identifier: MIT OR MPL-2.0
// Copyright © 2021 Skyline Team and Contributors (https://github.com/skyline-emu/)
2020-03-27 20:36:02 +01:00
2021-07-17 18:28:04 +02:00
# include <services/nvdrv/devices/deserialisation/deserialisation.h>
# include <services/nvdrv/core/nvmap.h>
2020-03-26 15:33:19 +01:00
# include "nvmap.h"
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
2020-03-24 21:17:31 +01:00
namespace skyline : : service : : nvdrv : : device {
2021-10-08 21:25:21 +02:00
NvMap : : NvMap ( const DeviceState & state , Driver & driver , Core & core , const SessionContext & ctx ) : NvDevice ( state , driver , core , ctx ) { }
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
2021-07-17 18:28:04 +02:00
PosixResult NvMap : : Create ( In < u32 > size , Out < NvMapCore : : Handle : : Id > handle ) {
2021-10-24 21:45:29 +02:00
auto handleDesc { core . nvMap . CreateHandle ( util : : AlignUp ( static_cast < u32 > ( size ) , PAGE_SIZE ) ) } ;
2021-09-21 22:01:44 +02:00
if ( handleDesc ) {
( * handleDesc ) - > origSize = size ; // Orig size is the unaligned size
handle = ( * handleDesc ) - > id ;
state . logger - > Debug ( " handle: {}, size: 0x{:X} " , ( * handleDesc ) - > id , size ) ;
2021-07-17 18:28:04 +02:00
}
2021-09-21 22:01:44 +02:00
return handleDesc ;
2021-07-17 18:28:04 +02:00
}
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
2021-07-17 18:28:04 +02:00
PosixResult NvMap : : FromId ( In < NvMapCore : : Handle : : Id > id , Out < NvMapCore : : Handle : : Id > handle ) {
2021-09-18 14:16:41 +02:00
state . logger - > Debug ( " id: {} " , id ) ;
2021-07-17 18:28:04 +02:00
// Handles and IDs are always the same value in nvmap however IDs can be used globally given the right permissions.
2021-09-21 22:01:44 +02:00
// Since we don't plan on ever supporting multiprocess we can skip implementing handle refs and so this function just does simple validation and passes through the handle id.
2021-07-17 18:28:04 +02:00
if ( ! id ) [[unlikely]]
return PosixResult : : InvalidArgument ;
2020-04-22 19:02:27 +02:00
2021-09-21 22:01:44 +02:00
auto handleDesc { core . nvMap . GetHandle ( id ) } ;
if ( ! handleDesc ) [[unlikely]]
2021-07-17 18:28:04 +02:00
return PosixResult : : InvalidArgument ;
2020-04-22 19:02:27 +02:00
2021-10-01 20:45:53 +02:00
auto result { handleDesc - > Duplicate ( ctx . internalSession ) } ;
if ( result = = PosixResult : : Success )
handle = id ;
return result ;
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
}
2021-10-24 17:36:29 +02:00
PosixResult NvMap : : Alloc ( In < NvMapCore : : Handle : : Id > handle ,
In < u32 > heapMask , In < NvMapCore : : Handle : : Flags > flags ,
InOut < u32 > align , In < u8 > kind , In < u64 > address ) {
state . logger - > Debug ( " handle: {}, flags: ( mapUncached: {}, keepUncachedAfterFree: {} ), align: 0x{:X}, kind: {}, address: 0x{:X} " ,
handle , flags . mapUncached , flags . keepUncachedAfterFree , align , kind , address ) ;
2021-09-18 14:16:41 +02:00
2021-07-17 18:28:04 +02:00
if ( ! handle ) [[unlikely]]
return PosixResult : : InvalidArgument ;
2020-04-22 19:02:27 +02:00
2021-10-30 23:43:41 +02:00
if ( ! std : : has_single_bit ( align ) ) [[unlikely]]
2021-07-17 18:28:04 +02:00
return PosixResult : : InvalidArgument ;
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
2021-07-17 18:28:04 +02:00
// Force page size alignment at a minimum
if ( align < PAGE_SIZE ) [[unlikely]]
align = PAGE_SIZE ;
2021-09-21 22:01:44 +02:00
auto handleDesc { core . nvMap . GetHandle ( handle ) } ;
if ( ! handleDesc ) [[unlikely]]
2021-07-17 18:28:04 +02:00
return PosixResult : : InvalidArgument ;
2021-09-21 22:01:44 +02:00
return handleDesc - > Alloc ( flags , align , kind , address ) ;
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
}
2021-10-24 17:36:29 +02:00
PosixResult NvMap : : Free ( In < NvMapCore : : Handle : : Id > handle ,
Out < u64 > address , Out < u32 > size ,
Out < NvMapCore : : Handle : : Flags > flags ) {
2021-09-18 14:16:41 +02:00
state . logger - > Debug ( " handle: {} " , handle ) ;
2021-07-17 18:28:04 +02:00
if ( ! handle ) [[unlikely]]
return PosixResult : : Success ;
if ( auto freeInfo { core . nvMap . FreeHandle ( handle , ctx . internalSession ) } ) {
address = freeInfo - > address ;
size = static_cast < u32 > ( freeInfo - > size ) ;
flags = NvMapCore : : Handle : : Flags { . mapUncached = freeInfo - > wasUncached } ;
2021-09-18 14:16:41 +02:00
} else {
state . logger - > Debug ( " Handle not freed " ) ;
2020-09-19 16:45:17 +02:00
}
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
}
2021-07-17 18:28:04 +02:00
PosixResult NvMap : : Param ( In < NvMapCore : : Handle : : Id > handle , In < HandleParameterType > param , Out < u32 > result ) {
2021-09-18 14:16:41 +02:00
state . logger - > Debug ( " handle: {}, param: {} " , handle , param ) ;
2021-07-17 18:28:04 +02:00
if ( ! handle )
return PosixResult : : InvalidArgument ;
2021-09-21 22:01:44 +02:00
auto handleDesc { core . nvMap . GetHandle ( handle ) } ;
if ( ! handleDesc ) [[unlikely]]
2021-07-17 18:28:04 +02:00
return PosixResult : : InvalidArgument ;
switch ( param ) {
case HandleParameterType : : Size :
2021-10-24 21:45:29 +02:00
result = static_cast < u32 > ( handleDesc - > origSize ) ;
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
case HandleParameterType : : Alignment :
2021-10-24 21:45:29 +02:00
result = static_cast < u32 > ( handleDesc - > align ) ;
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
case HandleParameterType : : Base :
2021-10-24 21:45:29 +02:00
result = static_cast < u32 > ( - static_cast < i32 > ( PosixResult : : InvalidArgument ) ) ;
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
case HandleParameterType : : Heap :
2021-09-21 22:01:44 +02:00
if ( handleDesc - > allocated )
2021-07-17 18:28:04 +02:00
result = 0x40000000 ;
else
result = 0 ;
return PosixResult : : Success ;
case HandleParameterType : : Kind :
2021-09-21 22:01:44 +02:00
result = handleDesc - > kind ;
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
case HandleParameterType : : IsSharedMemMapped :
2021-09-21 22:01:44 +02:00
result = handleDesc - > isSharedMemMapped ;
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
default :
return PosixResult : : InvalidArgument ;
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
}
}
2021-07-17 18:28:04 +02:00
PosixResult NvMap : : GetId ( Out < NvMapCore : : Handle : : Id > id , In < NvMapCore : : Handle : : Id > handle ) {
2021-09-18 14:16:41 +02:00
state . logger - > Debug ( " handle: {} " , handle ) ;
2021-07-17 18:28:04 +02:00
// See the comment in FromId for extra info on this function
if ( ! handle ) [[unlikely]]
return PosixResult : : InvalidArgument ;
2021-09-21 22:01:44 +02:00
auto handleDesc { core . nvMap . GetHandle ( handle ) } ;
if ( ! handleDesc ) [[unlikely]]
2021-07-17 18:28:04 +02:00
return PosixResult : : NotPermitted ; // This will always return EPERM irrespective of if the handle exists or not
2021-09-21 22:01:44 +02:00
id = handleDesc - > id ;
2021-07-17 18:28:04 +02:00
return PosixResult : : Success ;
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
}
2021-07-17 18:28:04 +02:00
2021-08-14 21:42:11 +02:00
# include "deserialisation/macro_def.inc"
2021-07-17 18:28:04 +02:00
static constexpr u32 NvMapMagic { 1 } ;
IOCTL_HANDLER_FUNC ( NvMap , ( {
IOCTL_CASE_ARGS ( INOUT , SIZE ( 0x8 ) , MAGIC ( NvMapMagic ) , FUNC ( 0x1 ) ,
Create , ARGS ( In < u32 > , Out < NvMapCore : : Handle : : Id > ) )
IOCTL_CASE_ARGS ( INOUT , SIZE ( 0x8 ) , MAGIC ( NvMapMagic ) , FUNC ( 0x3 ) ,
FromId , ARGS ( In < NvMapCore : : Handle : : Id > , Out < NvMapCore : : Handle : : Id > ) )
IOCTL_CASE_ARGS ( INOUT , SIZE ( 0x20 ) , MAGIC ( NvMapMagic ) , FUNC ( 0x4 ) ,
Alloc , ARGS ( In < NvMapCore : : Handle : : Id > , In < u32 > , In < NvMapCore : : Handle : : Flags > , InOut < u32 > , In < u8 > , Pad < u8 , 0x7 > , In < u64 > ) )
IOCTL_CASE_ARGS ( INOUT , SIZE ( 0x18 ) , MAGIC ( NvMapMagic ) , FUNC ( 0x5 ) ,
Free , ARGS ( In < NvMapCore : : Handle : : Id > , Pad < u32 > , Out < u64 > , Out < u32 > , Out < NvMapCore : : Handle : : Flags > ) )
IOCTL_CASE_ARGS ( INOUT , SIZE ( 0xC ) , MAGIC ( NvMapMagic ) , FUNC ( 0x9 ) ,
Param , ARGS ( In < NvMapCore : : Handle : : Id > , In < HandleParameterType > , Out < u32 > ) )
IOCTL_CASE_ARGS ( INOUT , SIZE ( 0x8 ) , MAGIC ( NvMapMagic ) , FUNC ( 0xE ) ,
GetId , ARGS ( Out < NvMapCore : : Handle : : Id > , In < NvMapCore : : Handle : : Id > ) )
} ) )
2021-08-14 21:42:11 +02:00
# include "deserialisation/macro_undef.inc"
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
}
2021-07-17 18:28:04 +02:00