Refactoring HOS folder structure (#771)
* Refactoring HOS folder structure Refactoring HOS folder structure: - Added some subfolders when needed (Following structure decided in private). - Added some `Types` folders when needed. - Little cleanup here and there. - Add services placeholders for every HOS services (close #766 and #753). * Remove Types namespaces
This commit is contained in:
parent
4af3101b22
commit
a0720b5681
393 changed files with 2540 additions and 1299 deletions
|
@ -0,0 +1,330 @@
|
|||
using ARMeilleure.Memory;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.Memory;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS
|
||||
{
|
||||
class NvGpuASIoctl
|
||||
{
|
||||
private const int FlagFixedOffset = 1;
|
||||
|
||||
private const int FlagRemapSubRange = 0x100;
|
||||
|
||||
private static ConcurrentDictionary<KProcess, NvGpuASCtx> _asCtxs;
|
||||
|
||||
static NvGpuASIoctl()
|
||||
{
|
||||
_asCtxs = new ConcurrentDictionary<KProcess, NvGpuASCtx>();
|
||||
}
|
||||
|
||||
public static int ProcessIoctl(ServiceCtx context, int cmd)
|
||||
{
|
||||
switch (cmd & 0xffff)
|
||||
{
|
||||
case 0x4101: return BindChannel (context);
|
||||
case 0x4102: return AllocSpace (context);
|
||||
case 0x4103: return FreeSpace (context);
|
||||
case 0x4105: return UnmapBuffer (context);
|
||||
case 0x4106: return MapBufferEx (context);
|
||||
case 0x4108: return GetVaRegions(context);
|
||||
case 0x4109: return InitializeEx(context);
|
||||
case 0x4114: return Remap (context, cmd);
|
||||
}
|
||||
|
||||
throw new NotImplementedException(cmd.ToString("x8"));
|
||||
}
|
||||
|
||||
private static int BindChannel(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int AllocSpace(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuASAllocSpace args = MemoryHelper.Read<NvGpuASAllocSpace>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuASCtx asCtx = GetASCtx(context);
|
||||
|
||||
ulong size = (ulong)args.Pages *
|
||||
(ulong)args.PageSize;
|
||||
|
||||
int result = NvResult.Success;
|
||||
|
||||
lock (asCtx)
|
||||
{
|
||||
// Note: When the fixed offset flag is not set,
|
||||
// the Offset field holds the alignment size instead.
|
||||
if ((args.Flags & FlagFixedOffset) != 0)
|
||||
{
|
||||
args.Offset = asCtx.Vmm.ReserveFixed(args.Offset, (long)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Offset = asCtx.Vmm.Reserve((long)size, args.Offset);
|
||||
}
|
||||
|
||||
if (args.Offset < 0)
|
||||
{
|
||||
args.Offset = 0;
|
||||
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Failed to allocate size {size:x16}!");
|
||||
|
||||
result = NvResult.OutOfMemory;
|
||||
}
|
||||
else
|
||||
{
|
||||
asCtx.AddReservation(args.Offset, (long)size);
|
||||
}
|
||||
}
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int FreeSpace(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuASAllocSpace args = MemoryHelper.Read<NvGpuASAllocSpace>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuASCtx asCtx = GetASCtx(context);
|
||||
|
||||
int result = NvResult.Success;
|
||||
|
||||
lock (asCtx)
|
||||
{
|
||||
ulong size = (ulong)args.Pages *
|
||||
(ulong)args.PageSize;
|
||||
|
||||
if (asCtx.RemoveReservation(args.Offset))
|
||||
{
|
||||
asCtx.Vmm.Free(args.Offset, (long)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv,
|
||||
$"Failed to free offset 0x{args.Offset:x16} size 0x{size:x16}!");
|
||||
|
||||
result = NvResult.InvalidInput;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int UnmapBuffer(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuASUnmapBuffer args = MemoryHelper.Read<NvGpuASUnmapBuffer>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuASCtx asCtx = GetASCtx(context);
|
||||
|
||||
lock (asCtx)
|
||||
{
|
||||
if (asCtx.RemoveMap(args.Offset, out long size))
|
||||
{
|
||||
if (size != 0)
|
||||
{
|
||||
asCtx.Vmm.Free(args.Offset, size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid buffer offset {args.Offset:x16}!");
|
||||
}
|
||||
}
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int MapBufferEx(ServiceCtx context)
|
||||
{
|
||||
const string mapErrorMsg = "Failed to map fixed buffer with offset 0x{0:x16} and size 0x{1:x16}!";
|
||||
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuASMapBufferEx args = MemoryHelper.Read<NvGpuASMapBufferEx>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuASCtx asCtx = GetASCtx(context);
|
||||
|
||||
NvMapHandle map = NvMapIoctl.GetNvMapWithFb(context, args.NvMapHandle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{args.NvMapHandle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
long pa;
|
||||
|
||||
if ((args.Flags & FlagRemapSubRange) != 0)
|
||||
{
|
||||
lock (asCtx)
|
||||
{
|
||||
if (asCtx.TryGetMapPhysicalAddress(args.Offset, out pa))
|
||||
{
|
||||
long va = args.Offset + args.BufferOffset;
|
||||
|
||||
pa += args.BufferOffset;
|
||||
|
||||
if (asCtx.Vmm.Map(pa, va, args.MappingSize) < 0)
|
||||
{
|
||||
string msg = string.Format(mapErrorMsg, va, args.MappingSize);
|
||||
|
||||
Logger.PrintWarning(LogClass.ServiceNv, msg);
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Address 0x{args.Offset:x16} not mapped!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pa = map.Address + args.BufferOffset;
|
||||
|
||||
long size = args.MappingSize;
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
size = (uint)map.Size;
|
||||
}
|
||||
|
||||
int result = NvResult.Success;
|
||||
|
||||
lock (asCtx)
|
||||
{
|
||||
// Note: When the fixed offset flag is not set,
|
||||
// the Offset field holds the alignment size instead.
|
||||
bool vaAllocated = (args.Flags & FlagFixedOffset) == 0;
|
||||
|
||||
if (!vaAllocated)
|
||||
{
|
||||
if (asCtx.ValidateFixedBuffer(args.Offset, size))
|
||||
{
|
||||
args.Offset = asCtx.Vmm.Map(pa, args.Offset, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
string msg = string.Format(mapErrorMsg, args.Offset, size);
|
||||
|
||||
Logger.PrintWarning(LogClass.ServiceNv, msg);
|
||||
|
||||
result = NvResult.InvalidInput;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Offset = asCtx.Vmm.Map(pa, size);
|
||||
}
|
||||
|
||||
if (args.Offset < 0)
|
||||
{
|
||||
args.Offset = 0;
|
||||
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Failed to map size 0x{size:x16}!");
|
||||
|
||||
result = NvResult.InvalidInput;
|
||||
}
|
||||
else
|
||||
{
|
||||
asCtx.AddMap(args.Offset, size, pa, vaAllocated);
|
||||
}
|
||||
}
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int GetVaRegions(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int InitializeEx(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int Remap(ServiceCtx context, int cmd)
|
||||
{
|
||||
int count = ((cmd >> 16) & 0xff) / 0x14;
|
||||
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
|
||||
for (int index = 0; index < count; index++, inputPosition += 0x14)
|
||||
{
|
||||
NvGpuASRemap args = MemoryHelper.Read<NvGpuASRemap>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuVmm vmm = GetASCtx(context).Vmm;
|
||||
|
||||
NvMapHandle map = NvMapIoctl.GetNvMapWithFb(context, args.NvMapHandle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid NvMap handle 0x{args.NvMapHandle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
long result = vmm.Map(map.Address, (long)(uint)args.Offset << 16,
|
||||
(long)(uint)args.Pages << 16);
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv,
|
||||
$"Page 0x{args.Offset:x16} size 0x{args.Pages:x16} not allocated!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
}
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
public static NvGpuASCtx GetASCtx(ServiceCtx context)
|
||||
{
|
||||
return _asCtxs.GetOrAdd(context.Process, (key) => new NvGpuASCtx(context));
|
||||
}
|
||||
|
||||
public static void UnloadProcess(KProcess process)
|
||||
{
|
||||
_asCtxs.TryRemove(process, out _);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS
|
||||
{
|
||||
struct NvGpuASAllocSpace
|
||||
{
|
||||
public int Pages;
|
||||
public int PageSize;
|
||||
public int Flags;
|
||||
public int Padding;
|
||||
public long Offset;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,200 @@
|
|||
using Ryujinx.Graphics.Memory;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS
|
||||
{
|
||||
class NvGpuASCtx
|
||||
{
|
||||
public NvGpuVmm Vmm { get; private set; }
|
||||
|
||||
private class Range
|
||||
{
|
||||
public ulong Start { get; private set; }
|
||||
public ulong End { get; private set; }
|
||||
|
||||
public Range(long position, long size)
|
||||
{
|
||||
Start = (ulong)position;
|
||||
End = (ulong)size + Start;
|
||||
}
|
||||
}
|
||||
|
||||
private class MappedMemory : Range
|
||||
{
|
||||
public long PhysicalAddress { get; private set; }
|
||||
public bool VaAllocated { get; private set; }
|
||||
|
||||
public MappedMemory(
|
||||
long position,
|
||||
long size,
|
||||
long physicalAddress,
|
||||
bool vaAllocated) : base(position, size)
|
||||
{
|
||||
PhysicalAddress = physicalAddress;
|
||||
VaAllocated = vaAllocated;
|
||||
}
|
||||
}
|
||||
|
||||
private SortedList<long, Range> _maps;
|
||||
private SortedList<long, Range> _reservations;
|
||||
|
||||
public NvGpuASCtx(ServiceCtx context)
|
||||
{
|
||||
Vmm = new NvGpuVmm(context.Memory);
|
||||
|
||||
_maps = new SortedList<long, Range>();
|
||||
_reservations = new SortedList<long, Range>();
|
||||
}
|
||||
|
||||
public bool ValidateFixedBuffer(long position, long size)
|
||||
{
|
||||
long mapEnd = position + size;
|
||||
|
||||
// Check if size is valid (0 is also not allowed).
|
||||
if ((ulong)mapEnd <= (ulong)position)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if address is page aligned.
|
||||
if ((position & NvGpuVmm.PageMask) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if region is reserved.
|
||||
if (BinarySearch(_reservations, position) == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for overlap with already mapped buffers.
|
||||
Range map = BinarySearchLt(_maps, mapEnd);
|
||||
|
||||
if (map != null && map.End > (ulong)position)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddMap(
|
||||
long position,
|
||||
long size,
|
||||
long physicalAddress,
|
||||
bool vaAllocated)
|
||||
{
|
||||
_maps.Add(position, new MappedMemory(position, size, physicalAddress, vaAllocated));
|
||||
}
|
||||
|
||||
public bool RemoveMap(long position, out long size)
|
||||
{
|
||||
size = 0;
|
||||
|
||||
if (_maps.Remove(position, out Range value))
|
||||
{
|
||||
MappedMemory map = (MappedMemory)value;
|
||||
|
||||
if (map.VaAllocated)
|
||||
{
|
||||
size = (long)(map.End - map.Start);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetMapPhysicalAddress(long position, out long physicalAddress)
|
||||
{
|
||||
Range map = BinarySearch(_maps, position);
|
||||
|
||||
if (map != null)
|
||||
{
|
||||
physicalAddress = ((MappedMemory)map).PhysicalAddress;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
physicalAddress = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void AddReservation(long position, long size)
|
||||
{
|
||||
_reservations.Add(position, new Range(position, size));
|
||||
}
|
||||
|
||||
public bool RemoveReservation(long position)
|
||||
{
|
||||
return _reservations.Remove(position);
|
||||
}
|
||||
|
||||
private Range BinarySearch(SortedList<long, Range> lst, long position)
|
||||
{
|
||||
int left = 0;
|
||||
int right = lst.Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int size = right - left;
|
||||
|
||||
int middle = left + (size >> 1);
|
||||
|
||||
Range rg = lst.Values[middle];
|
||||
|
||||
if ((ulong)position >= rg.Start && (ulong)position < rg.End)
|
||||
{
|
||||
return rg;
|
||||
}
|
||||
|
||||
if ((ulong)position < rg.Start)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Range BinarySearchLt(SortedList<long, Range> lst, long position)
|
||||
{
|
||||
Range ltRg = null;
|
||||
|
||||
int left = 0;
|
||||
int right = lst.Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int size = right - left;
|
||||
|
||||
int middle = left + (size >> 1);
|
||||
|
||||
Range rg = lst.Values[middle];
|
||||
|
||||
if ((ulong)position < rg.Start)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
|
||||
if ((ulong)position > rg.Start)
|
||||
{
|
||||
ltRg = rg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ltRg;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS
|
||||
{
|
||||
struct NvGpuASMapBufferEx
|
||||
{
|
||||
public int Flags;
|
||||
public int Kind;
|
||||
public int NvMapHandle;
|
||||
public int PageSize;
|
||||
public long BufferOffset;
|
||||
public long MappingSize;
|
||||
public long Offset;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS
|
||||
{
|
||||
struct NvGpuASRemap
|
||||
{
|
||||
public short Flags;
|
||||
public short Kind;
|
||||
public int NvMapHandle;
|
||||
public int Padding;
|
||||
public int Offset;
|
||||
public int Pages;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS
|
||||
{
|
||||
struct NvGpuASUnmapBuffer
|
||||
{
|
||||
public long Offset;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
using ARMeilleure.Memory;
|
||||
using Ryujinx.Common.Logging;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuGpu
|
||||
{
|
||||
class NvGpuGpuIoctl
|
||||
{
|
||||
private static Stopwatch _pTimer;
|
||||
|
||||
private static double _ticksToNs;
|
||||
|
||||
static NvGpuGpuIoctl()
|
||||
{
|
||||
_pTimer = new Stopwatch();
|
||||
|
||||
_pTimer.Start();
|
||||
|
||||
_ticksToNs = (1.0 / Stopwatch.Frequency) * 1_000_000_000;
|
||||
}
|
||||
|
||||
public static int ProcessIoctl(ServiceCtx context, int cmd)
|
||||
{
|
||||
switch (cmd & 0xffff)
|
||||
{
|
||||
case 0x4701: return ZcullGetCtxSize (context);
|
||||
case 0x4702: return ZcullGetInfo (context);
|
||||
case 0x4703: return ZbcSetTable (context);
|
||||
case 0x4705: return GetCharacteristics(context);
|
||||
case 0x4706: return GetTpcMasks (context);
|
||||
case 0x4714: return GetActiveSlotMask (context);
|
||||
case 0x471c: return GetGpuTime (context);
|
||||
}
|
||||
|
||||
throw new NotImplementedException(cmd.ToString("x8"));
|
||||
}
|
||||
|
||||
private static int ZcullGetCtxSize(ServiceCtx context)
|
||||
{
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuGpuZcullGetCtxSize args = new NvGpuGpuZcullGetCtxSize
|
||||
{
|
||||
Size = 1
|
||||
};
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int ZcullGetInfo(ServiceCtx context)
|
||||
{
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuGpuZcullGetInfo args = new NvGpuGpuZcullGetInfo
|
||||
{
|
||||
WidthAlignPixels = 0x20,
|
||||
HeightAlignPixels = 0x20,
|
||||
PixelSquaresByAliquots = 0x400,
|
||||
AliquotTotal = 0x800,
|
||||
RegionByteMultiplier = 0x20,
|
||||
RegionHeaderSize = 0x20,
|
||||
SubregionHeaderSize = 0xc0,
|
||||
SubregionWidthAlignPixels = 0x20,
|
||||
SubregionHeightAlignPixels = 0x40,
|
||||
SubregionCount = 0x10
|
||||
};
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int ZbcSetTable(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetCharacteristics(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuGpuGetCharacteristics args = MemoryHelper.Read<NvGpuGpuGetCharacteristics>(context.Memory, inputPosition);
|
||||
|
||||
args.BufferSize = 0xa0;
|
||||
|
||||
args.Arch = 0x120;
|
||||
args.Impl = 0xb;
|
||||
args.Rev = 0xa1;
|
||||
args.NumGpc = 0x1;
|
||||
args.L2CacheSize = 0x40000;
|
||||
args.OnBoardVideoMemorySize = 0x0;
|
||||
args.NumTpcPerGpc = 0x2;
|
||||
args.BusType = 0x20;
|
||||
args.BigPageSize = 0x20000;
|
||||
args.CompressionPageSize = 0x20000;
|
||||
args.PdeCoverageBitCount = 0x1b;
|
||||
args.AvailableBigPageSizes = 0x30000;
|
||||
args.GpcMask = 0x1;
|
||||
args.SmArchSmVersion = 0x503;
|
||||
args.SmArchSpaVersion = 0x503;
|
||||
args.SmArchWarpCount = 0x80;
|
||||
args.GpuVaBitCount = 0x28;
|
||||
args.Reserved = 0x0;
|
||||
args.Flags = 0x55;
|
||||
args.TwodClass = 0x902d;
|
||||
args.ThreedClass = 0xb197;
|
||||
args.ComputeClass = 0xb1c0;
|
||||
args.GpfifoClass = 0xb06f;
|
||||
args.InlineToMemoryClass = 0xa140;
|
||||
args.DmaCopyClass = 0xb0b5;
|
||||
args.MaxFbpsCount = 0x1;
|
||||
args.FbpEnMask = 0x0;
|
||||
args.MaxLtcPerFbp = 0x2;
|
||||
args.MaxLtsPerLtc = 0x1;
|
||||
args.MaxTexPerTpc = 0x0;
|
||||
args.MaxGpcCount = 0x1;
|
||||
args.RopL2EnMask0 = 0x21d70;
|
||||
args.RopL2EnMask1 = 0x0;
|
||||
args.ChipName = 0x6230326d67;
|
||||
args.GrCompbitStoreBaseHw = 0x0;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetTpcMasks(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuGpuGetTpcMasks args = MemoryHelper.Read<NvGpuGpuGetTpcMasks>(context.Memory, inputPosition);
|
||||
|
||||
if (args.MaskBufferSize != 0)
|
||||
{
|
||||
args.TpcMask = 3;
|
||||
}
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetActiveSlotMask(ServiceCtx context)
|
||||
{
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvGpuGpuGetActiveSlotMask args = new NvGpuGpuGetActiveSlotMask
|
||||
{
|
||||
Slot = 0x07,
|
||||
Mask = 0x01
|
||||
};
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetGpuTime(ServiceCtx context)
|
||||
{
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
context.Memory.WriteInt64(outputPosition, GetPTimerNanoSeconds());
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static long GetPTimerNanoSeconds()
|
||||
{
|
||||
double ticks = _pTimer.ElapsedTicks;
|
||||
|
||||
return (long)(ticks * _ticksToNs) & 0xff_ffff_ffff_ffff;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuGpu
|
||||
{
|
||||
struct NvGpuGpuGetActiveSlotMask
|
||||
{
|
||||
public int Slot;
|
||||
public int Mask;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuGpu
|
||||
{
|
||||
struct NvGpuGpuGetCharacteristics
|
||||
{
|
||||
public long BufferSize;
|
||||
public long BufferAddress;
|
||||
public int Arch;
|
||||
public int Impl;
|
||||
public int Rev;
|
||||
public int NumGpc;
|
||||
public long L2CacheSize;
|
||||
public long OnBoardVideoMemorySize;
|
||||
public int NumTpcPerGpc;
|
||||
public int BusType;
|
||||
public int BigPageSize;
|
||||
public int CompressionPageSize;
|
||||
public int PdeCoverageBitCount;
|
||||
public int AvailableBigPageSizes;
|
||||
public int GpcMask;
|
||||
public int SmArchSmVersion;
|
||||
public int SmArchSpaVersion;
|
||||
public int SmArchWarpCount;
|
||||
public int GpuVaBitCount;
|
||||
public int Reserved;
|
||||
public long Flags;
|
||||
public int TwodClass;
|
||||
public int ThreedClass;
|
||||
public int ComputeClass;
|
||||
public int GpfifoClass;
|
||||
public int InlineToMemoryClass;
|
||||
public int DmaCopyClass;
|
||||
public int MaxFbpsCount;
|
||||
public int FbpEnMask;
|
||||
public int MaxLtcPerFbp;
|
||||
public int MaxLtsPerLtc;
|
||||
public int MaxTexPerTpc;
|
||||
public int MaxGpcCount;
|
||||
public int RopL2EnMask0;
|
||||
public int RopL2EnMask1;
|
||||
public long ChipName;
|
||||
public long GrCompbitStoreBaseHw;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuGpu
|
||||
{
|
||||
struct NvGpuGpuGetTpcMasks
|
||||
{
|
||||
public int MaskBufferSize;
|
||||
public int Reserved;
|
||||
public long MaskBufferAddress;
|
||||
public int TpcMask;
|
||||
public int Padding;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuGpu
|
||||
{
|
||||
struct NvGpuGpuZcullGetCtxSize
|
||||
{
|
||||
public int Size;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuGpu
|
||||
{
|
||||
struct NvGpuGpuZcullGetInfo
|
||||
{
|
||||
public int WidthAlignPixels;
|
||||
public int HeightAlignPixels;
|
||||
public int PixelSquaresByAliquots;
|
||||
public int AliquotTotal;
|
||||
public int RegionByteMultiplier;
|
||||
public int RegionHeaderSize;
|
||||
public int SubregionHeaderSize;
|
||||
public int SubregionWidthAlignPixels;
|
||||
public int SubregionHeightAlignPixels;
|
||||
public int SubregionCount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,371 @@
|
|||
using ARMeilleure.Memory;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.Memory;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvGpuAS;
|
||||
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
class NvHostChannelIoctl
|
||||
{
|
||||
private static ConcurrentDictionary<KProcess, NvChannel> _channels;
|
||||
|
||||
static NvHostChannelIoctl()
|
||||
{
|
||||
_channels = new ConcurrentDictionary<KProcess, NvChannel>();
|
||||
}
|
||||
|
||||
public static int ProcessIoctl(ServiceCtx context, int cmd)
|
||||
{
|
||||
switch (cmd & 0xffff)
|
||||
{
|
||||
case 0x0001: return Submit (context);
|
||||
case 0x0002: return GetSyncpoint (context);
|
||||
case 0x0003: return GetWaitBase (context);
|
||||
case 0x0007: return SetSubmitTimeout (context);
|
||||
case 0x0009: return MapBuffer (context);
|
||||
case 0x000a: return UnmapBuffer (context);
|
||||
case 0x4714: return SetUserData (context);
|
||||
case 0x4801: return SetNvMap (context);
|
||||
case 0x4803: return SetTimeout (context);
|
||||
case 0x4808: return SubmitGpfifo (context);
|
||||
case 0x4809: return AllocObjCtx (context);
|
||||
case 0x480b: return ZcullBind (context);
|
||||
case 0x480c: return SetErrorNotifier (context);
|
||||
case 0x480d: return SetPriority (context);
|
||||
case 0x481a: return AllocGpfifoEx2 (context);
|
||||
case 0x481b: return KickoffPbWithAttr(context);
|
||||
case 0x481d: return SetTimeslice (context);
|
||||
}
|
||||
|
||||
throw new NotImplementedException(cmd.ToString("x8"));
|
||||
}
|
||||
|
||||
private static int Submit(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostChannelSubmit args = MemoryHelper.Read<NvHostChannelSubmit>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
|
||||
|
||||
for (int index = 0; index < args.CmdBufsCount; index++)
|
||||
{
|
||||
long cmdBufOffset = inputPosition + 0x10 + index * 0xc;
|
||||
|
||||
NvHostChannelCmdBuf cmdBuf = MemoryHelper.Read<NvHostChannelCmdBuf>(context.Memory, cmdBufOffset);
|
||||
|
||||
NvMapHandle map = NvMapIoctl.GetNvMap(context, cmdBuf.MemoryId);
|
||||
|
||||
int[] cmdBufData = new int[cmdBuf.WordsCount];
|
||||
|
||||
for (int offset = 0; offset < cmdBufData.Length; offset++)
|
||||
{
|
||||
cmdBufData[offset] = context.Memory.ReadInt32(map.Address + cmdBuf.Offset + offset * 4);
|
||||
}
|
||||
|
||||
context.Device.Gpu.PushCommandBuffer(vmm, cmdBufData);
|
||||
}
|
||||
|
||||
// TODO: Relocation, waitchecks, etc.
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetSyncpoint(ServiceCtx context)
|
||||
{
|
||||
// TODO
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostChannelGetParamArg args = MemoryHelper.Read<NvHostChannelGetParamArg>(context.Memory, inputPosition);
|
||||
|
||||
args.Value = 0;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetWaitBase(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostChannelGetParamArg args = MemoryHelper.Read<NvHostChannelGetParamArg>(context.Memory, inputPosition);
|
||||
|
||||
args.Value = 0;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetSubmitTimeout(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
|
||||
GetChannel(context).SubmitTimeout = context.Memory.ReadInt32(inputPosition);
|
||||
|
||||
// TODO: Handle the timeout in the submit method.
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int MapBuffer(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostChannelMapBuffer args = MemoryHelper.Read<NvHostChannelMapBuffer>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
|
||||
|
||||
for (int index = 0; index < args.NumEntries; index++)
|
||||
{
|
||||
int handle = context.Memory.ReadInt32(inputPosition + 0xc + index * 8);
|
||||
|
||||
NvMapHandle map = NvMapIoctl.GetNvMap(context, handle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
lock (map)
|
||||
{
|
||||
if (map.DmaMapAddress == 0)
|
||||
{
|
||||
map.DmaMapAddress = vmm.MapLow(map.Address, map.Size);
|
||||
}
|
||||
|
||||
context.Memory.WriteInt32(outputPosition + 0xc + 4 + index * 8, (int)map.DmaMapAddress);
|
||||
}
|
||||
}
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int UnmapBuffer(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
|
||||
NvHostChannelMapBuffer args = MemoryHelper.Read<NvHostChannelMapBuffer>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
|
||||
|
||||
for (int index = 0; index < args.NumEntries; index++)
|
||||
{
|
||||
int handle = context.Memory.ReadInt32(inputPosition + 0xc + index * 8);
|
||||
|
||||
NvMapHandle map = NvMapIoctl.GetNvMap(context, handle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
lock (map)
|
||||
{
|
||||
if (map.DmaMapAddress != 0)
|
||||
{
|
||||
vmm.Free(map.DmaMapAddress, map.Size);
|
||||
|
||||
map.DmaMapAddress = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetUserData(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetNvMap(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetTimeout(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
|
||||
GetChannel(context).Timeout = context.Memory.ReadInt32(inputPosition);
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SubmitGpfifo(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostChannelSubmitGpfifo args = MemoryHelper.Read<NvHostChannelSubmitGpfifo>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
|
||||
|
||||
for (int index = 0; index < args.NumEntries; index++)
|
||||
{
|
||||
long gpfifo = context.Memory.ReadInt64(inputPosition + 0x18 + index * 8);
|
||||
|
||||
PushGpfifo(context, vmm, gpfifo);
|
||||
}
|
||||
|
||||
args.SyncptId = 0;
|
||||
args.SyncptValue = 0;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int AllocObjCtx(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int ZcullBind(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetErrorNotifier(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetPriority(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
|
||||
switch ((NvChannelPriority)context.Memory.ReadInt32(inputPosition))
|
||||
{
|
||||
case NvChannelPriority.Low:
|
||||
GetChannel(context).Timeslice = 1300; // Timeslice low priority in micro-seconds
|
||||
break;
|
||||
case NvChannelPriority.Medium:
|
||||
GetChannel(context).Timeslice = 2600; // Timeslice medium priority in micro-seconds
|
||||
break;
|
||||
case NvChannelPriority.High:
|
||||
GetChannel(context).Timeslice = 5200; // Timeslice high priority in micro-seconds
|
||||
break;
|
||||
default:
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
// TODO: disable and preempt channel when GPU scheduler will be implemented.
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int AllocGpfifoEx2(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int KickoffPbWithAttr(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostChannelSubmitGpfifo args = MemoryHelper.Read<NvHostChannelSubmitGpfifo>(context.Memory, inputPosition);
|
||||
|
||||
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
|
||||
|
||||
for (int index = 0; index < args.NumEntries; index++)
|
||||
{
|
||||
long gpfifo = context.Memory.ReadInt64(args.Address + index * 8);
|
||||
|
||||
PushGpfifo(context, vmm, gpfifo);
|
||||
}
|
||||
|
||||
args.SyncptId = 0;
|
||||
args.SyncptValue = 0;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SetTimeslice(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
int timeslice = context.Memory.ReadInt32(inputPosition);
|
||||
|
||||
if (timeslice < 1000 || timeslice > 50000)
|
||||
{
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
GetChannel(context).Timeslice = timeslice; // in micro-seconds
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
// TODO: disable and preempt channel when GPU scheduler will be implemented.
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static void PushGpfifo(ServiceCtx context, NvGpuVmm vmm, long gpfifo)
|
||||
{
|
||||
context.Device.Gpu.Pusher.Push(vmm, gpfifo);
|
||||
}
|
||||
|
||||
public static NvChannel GetChannel(ServiceCtx context)
|
||||
{
|
||||
return _channels.GetOrAdd(context.Process, (key) => new NvChannel());
|
||||
}
|
||||
|
||||
public static void UnloadProcess(KProcess process)
|
||||
{
|
||||
_channels.TryRemove(process, out _);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
class NvChannel
|
||||
{
|
||||
public int Timeout;
|
||||
public int SubmitTimeout;
|
||||
public int Timeslice;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
enum NvChannelPriority
|
||||
{
|
||||
Low = 50,
|
||||
Medium = 100,
|
||||
High = 150
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 8, Pack = 4)]
|
||||
struct NvHostChannelCmdBuf
|
||||
{
|
||||
public int MemoryId;
|
||||
public int Offset;
|
||||
public int WordsCount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 8, Pack = 4)]
|
||||
struct NvHostChannelGetParamArg
|
||||
{
|
||||
public int Param;
|
||||
public int Value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0xc, Pack = 4)]
|
||||
struct NvHostChannelMapBuffer
|
||||
{
|
||||
public int NumEntries;
|
||||
public int DataAddress; // Ignored by the driver.
|
||||
public bool AttachHostChDas;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 8, Pack = 4)]
|
||||
struct NvHostChannelSubmit
|
||||
{
|
||||
public int CmdBufsCount;
|
||||
public int RelocsCount;
|
||||
public int SyncptIncrsCount;
|
||||
public int WaitchecksCount;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
||||
{
|
||||
struct NvHostChannelSubmitGpfifo
|
||||
{
|
||||
public long Address;
|
||||
public int NumEntries;
|
||||
public int Flags;
|
||||
public int SyncptId;
|
||||
public int SyncptValue;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,400 @@
|
|||
using ARMeilleure.Memory;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.HOS.Services.Settings;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
class NvHostCtrlIoctl
|
||||
{
|
||||
private static ConcurrentDictionary<KProcess, NvHostCtrlUserCtx> _userCtxs;
|
||||
|
||||
private static bool _isProductionMode = true;
|
||||
|
||||
static NvHostCtrlIoctl()
|
||||
{
|
||||
_userCtxs = new ConcurrentDictionary<KProcess, NvHostCtrlUserCtx>();
|
||||
|
||||
if (NxSettings.Settings.TryGetValue("nv!rmos_set_production_mode", out object productionModeSetting))
|
||||
{
|
||||
_isProductionMode = ((string)productionModeSetting) != "0"; // Default value is ""
|
||||
}
|
||||
}
|
||||
|
||||
public static int ProcessIoctl(ServiceCtx context, int cmd)
|
||||
{
|
||||
switch (cmd & 0xffff)
|
||||
{
|
||||
case 0x0014: return SyncptRead (context);
|
||||
case 0x0015: return SyncptIncr (context);
|
||||
case 0x0016: return SyncptWait (context);
|
||||
case 0x0019: return SyncptWaitEx (context);
|
||||
case 0x001a: return SyncptReadMax (context);
|
||||
case 0x001b: return GetConfig (context);
|
||||
case 0x001d: return EventWait (context);
|
||||
case 0x001e: return EventWaitAsync(context);
|
||||
case 0x001f: return EventRegister (context);
|
||||
}
|
||||
|
||||
throw new NotImplementedException(cmd.ToString("x8"));
|
||||
}
|
||||
|
||||
private static int SyncptRead(ServiceCtx context)
|
||||
{
|
||||
return SyncptReadMinOrMax(context, max: false);
|
||||
}
|
||||
|
||||
private static int SyncptIncr(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
|
||||
int id = context.Memory.ReadInt32(inputPosition);
|
||||
|
||||
if ((uint)id >= NvHostSyncpt.SyncptsCount)
|
||||
{
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
GetUserCtx(context).Syncpt.Increment(id);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SyncptWait(ServiceCtx context)
|
||||
{
|
||||
return SyncptWait(context, extended: false);
|
||||
}
|
||||
|
||||
private static int SyncptWaitEx(ServiceCtx context)
|
||||
{
|
||||
return SyncptWait(context, extended: true);
|
||||
}
|
||||
|
||||
private static int SyncptReadMax(ServiceCtx context)
|
||||
{
|
||||
return SyncptReadMinOrMax(context, max: true);
|
||||
}
|
||||
|
||||
private static int GetConfig(ServiceCtx context)
|
||||
{
|
||||
if (!_isProductionMode)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
string domain = MemoryHelper.ReadAsciiString(context.Memory, inputPosition + 0, 0x41);
|
||||
string name = MemoryHelper.ReadAsciiString(context.Memory, inputPosition + 0x41, 0x41);
|
||||
|
||||
if (NxSettings.Settings.TryGetValue($"{domain}!{name}", out object nvSetting))
|
||||
{
|
||||
byte[] settingBuffer = new byte[0x101];
|
||||
|
||||
if (nvSetting is string stringValue)
|
||||
{
|
||||
if (stringValue.Length > 0x100)
|
||||
{
|
||||
Logger.PrintError(LogClass.ServiceNv, $"{domain}!{name} String value size is too big!");
|
||||
}
|
||||
else
|
||||
{
|
||||
settingBuffer = Encoding.ASCII.GetBytes(stringValue + "\0");
|
||||
}
|
||||
}
|
||||
|
||||
if (nvSetting is int intValue)
|
||||
{
|
||||
settingBuffer = BitConverter.GetBytes(intValue);
|
||||
}
|
||||
else if (nvSetting is bool boolValue)
|
||||
{
|
||||
settingBuffer[0] = boolValue ? (byte)1 : (byte)0;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException(nvSetting.GetType().Name);
|
||||
}
|
||||
|
||||
context.Memory.WriteBytes(outputPosition + 0x82, settingBuffer);
|
||||
|
||||
Logger.PrintDebug(LogClass.ServiceNv, $"Got setting {domain}!{name}");
|
||||
}
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
return NvResult.NotAvailableInProduction;
|
||||
}
|
||||
|
||||
private static int EventWait(ServiceCtx context)
|
||||
{
|
||||
return EventWait(context, async: false);
|
||||
}
|
||||
|
||||
private static int EventWaitAsync(ServiceCtx context)
|
||||
{
|
||||
return EventWait(context, async: true);
|
||||
}
|
||||
|
||||
private static int EventRegister(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
int eventId = context.Memory.ReadInt32(inputPosition);
|
||||
|
||||
Logger.PrintStub(LogClass.ServiceNv);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SyncptReadMinOrMax(ServiceCtx context, bool max)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostCtrlSyncptRead args = MemoryHelper.Read<NvHostCtrlSyncptRead>(context.Memory, inputPosition);
|
||||
|
||||
if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
|
||||
{
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
if (max)
|
||||
{
|
||||
args.Value = GetUserCtx(context).Syncpt.GetMax(args.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Value = GetUserCtx(context).Syncpt.GetMin(args.Id);
|
||||
}
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int SyncptWait(ServiceCtx context, bool extended)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostCtrlSyncptWait args = MemoryHelper.Read<NvHostCtrlSyncptWait>(context.Memory, inputPosition);
|
||||
|
||||
NvHostSyncpt syncpt = GetUserCtx(context).Syncpt;
|
||||
|
||||
if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
|
||||
{
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
int result;
|
||||
|
||||
if (syncpt.MinCompare(args.Id, args.Thresh))
|
||||
{
|
||||
result = NvResult.Success;
|
||||
}
|
||||
else if (args.Timeout == 0)
|
||||
{
|
||||
result = NvResult.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.PrintDebug(LogClass.ServiceNv, "Waiting syncpt with timeout of " + args.Timeout + "ms...");
|
||||
|
||||
using (ManualResetEvent waitEvent = new ManualResetEvent(false))
|
||||
{
|
||||
syncpt.AddWaiter(args.Thresh, waitEvent);
|
||||
|
||||
// Note: Negative (> INT_MAX) timeouts aren't valid on .NET,
|
||||
// in this case we just use the maximum timeout possible.
|
||||
int timeout = args.Timeout;
|
||||
|
||||
if (timeout < -1)
|
||||
{
|
||||
timeout = int.MaxValue;
|
||||
}
|
||||
|
||||
if (timeout == -1)
|
||||
{
|
||||
waitEvent.WaitOne();
|
||||
|
||||
result = NvResult.Success;
|
||||
}
|
||||
else if (waitEvent.WaitOne(timeout))
|
||||
{
|
||||
result = NvResult.Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = NvResult.TimedOut;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.PrintDebug(LogClass.ServiceNv, "Resuming...");
|
||||
}
|
||||
|
||||
if (extended)
|
||||
{
|
||||
context.Memory.WriteInt32(outputPosition + 0xc, syncpt.GetMin(args.Id));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int EventWait(ServiceCtx context, bool async)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvHostCtrlSyncptWaitEx args = MemoryHelper.Read<NvHostCtrlSyncptWaitEx>(context.Memory, inputPosition);
|
||||
|
||||
if ((uint)args.Id >= NvHostSyncpt.SyncptsCount)
|
||||
{
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
void WriteArgs()
|
||||
{
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
}
|
||||
|
||||
NvHostSyncpt syncpt = GetUserCtx(context).Syncpt;
|
||||
|
||||
if (syncpt.MinCompare(args.Id, args.Thresh))
|
||||
{
|
||||
args.Value = syncpt.GetMin(args.Id);
|
||||
|
||||
WriteArgs();
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
if (!async)
|
||||
{
|
||||
args.Value = 0;
|
||||
}
|
||||
|
||||
if (args.Timeout == 0)
|
||||
{
|
||||
WriteArgs();
|
||||
|
||||
return NvResult.TryAgain;
|
||||
}
|
||||
|
||||
NvHostEvent Event;
|
||||
|
||||
int result, eventIndex;
|
||||
|
||||
if (async)
|
||||
{
|
||||
eventIndex = args.Value;
|
||||
|
||||
if ((uint)eventIndex >= NvHostCtrlUserCtx.EventsCount)
|
||||
{
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
Event = GetUserCtx(context).Events[eventIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
Event = GetFreeEvent(context, syncpt, args.Id, out eventIndex);
|
||||
}
|
||||
|
||||
if (Event != null &&
|
||||
(Event.State == NvHostEventState.Registered ||
|
||||
Event.State == NvHostEventState.Free))
|
||||
{
|
||||
Event.Id = args.Id;
|
||||
Event.Thresh = args.Thresh;
|
||||
|
||||
Event.State = NvHostEventState.Waiting;
|
||||
|
||||
if (!async)
|
||||
{
|
||||
args.Value = ((args.Id & 0xfff) << 16) | 0x10000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Value = args.Id << 4;
|
||||
}
|
||||
|
||||
args.Value |= eventIndex;
|
||||
|
||||
result = NvResult.TryAgain;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
WriteArgs();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static NvHostEvent GetFreeEvent(
|
||||
ServiceCtx context,
|
||||
NvHostSyncpt syncpt,
|
||||
int id,
|
||||
out int eventIndex)
|
||||
{
|
||||
NvHostEvent[] events = GetUserCtx(context).Events;
|
||||
|
||||
eventIndex = NvHostCtrlUserCtx.EventsCount;
|
||||
|
||||
int nullIndex = NvHostCtrlUserCtx.EventsCount;
|
||||
|
||||
for (int index = 0; index < NvHostCtrlUserCtx.EventsCount; index++)
|
||||
{
|
||||
NvHostEvent Event = events[index];
|
||||
|
||||
if (Event != null)
|
||||
{
|
||||
if (Event.State == NvHostEventState.Registered ||
|
||||
Event.State == NvHostEventState.Free)
|
||||
{
|
||||
eventIndex = index;
|
||||
|
||||
if (Event.Id == id)
|
||||
{
|
||||
return Event;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (nullIndex == NvHostCtrlUserCtx.EventsCount)
|
||||
{
|
||||
nullIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullIndex < NvHostCtrlUserCtx.EventsCount)
|
||||
{
|
||||
eventIndex = nullIndex;
|
||||
|
||||
return events[nullIndex] = new NvHostEvent();
|
||||
}
|
||||
|
||||
if (eventIndex < NvHostCtrlUserCtx.EventsCount)
|
||||
{
|
||||
return events[eventIndex];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static NvHostCtrlUserCtx GetUserCtx(ServiceCtx context)
|
||||
{
|
||||
return _userCtxs.GetOrAdd(context.Process, (key) => new NvHostCtrlUserCtx());
|
||||
}
|
||||
|
||||
public static void UnloadProcess(KProcess process)
|
||||
{
|
||||
_userCtxs.TryRemove(process, out _);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
struct NvHostCtrlSyncptRead
|
||||
{
|
||||
public int Id;
|
||||
public int Value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
struct NvHostCtrlSyncptWait
|
||||
{
|
||||
public int Id;
|
||||
public int Thresh;
|
||||
public int Timeout;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
struct NvHostCtrlSyncptWaitEx
|
||||
{
|
||||
public int Id;
|
||||
public int Thresh;
|
||||
public int Timeout;
|
||||
public int Value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
class NvHostCtrlUserCtx
|
||||
{
|
||||
public const int LocksCount = 16;
|
||||
public const int EventsCount = 64;
|
||||
|
||||
public NvHostSyncpt Syncpt { get; private set; }
|
||||
|
||||
public NvHostEvent[] Events { get; private set; }
|
||||
|
||||
public NvHostCtrlUserCtx()
|
||||
{
|
||||
Syncpt = new NvHostSyncpt();
|
||||
|
||||
Events = new NvHostEvent[EventsCount];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
class NvHostEvent
|
||||
{
|
||||
public int Id;
|
||||
public int Thresh;
|
||||
|
||||
public NvHostEventState State;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
enum NvHostEventState
|
||||
{
|
||||
Registered = 0,
|
||||
Waiting = 1,
|
||||
Busy = 2,
|
||||
Free = 5
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
|
||||
{
|
||||
class NvHostSyncpt
|
||||
{
|
||||
public const int SyncptsCount = 192;
|
||||
|
||||
private int[] _counterMin;
|
||||
private int[] _counterMax;
|
||||
|
||||
private long _eventMask;
|
||||
|
||||
private ConcurrentDictionary<EventWaitHandle, int> _waiters;
|
||||
|
||||
public NvHostSyncpt()
|
||||
{
|
||||
_counterMin = new int[SyncptsCount];
|
||||
_counterMax = new int[SyncptsCount];
|
||||
|
||||
_waiters = new ConcurrentDictionary<EventWaitHandle, int>();
|
||||
}
|
||||
|
||||
public int GetMin(int id)
|
||||
{
|
||||
return _counterMin[id];
|
||||
}
|
||||
|
||||
public int GetMax(int id)
|
||||
{
|
||||
return _counterMax[id];
|
||||
}
|
||||
|
||||
public int Increment(int id)
|
||||
{
|
||||
if (((_eventMask >> id) & 1) != 0)
|
||||
{
|
||||
Interlocked.Increment(ref _counterMax[id]);
|
||||
}
|
||||
|
||||
return IncrementMin(id);
|
||||
}
|
||||
|
||||
public int IncrementMin(int id)
|
||||
{
|
||||
int value = Interlocked.Increment(ref _counterMin[id]);
|
||||
|
||||
WakeUpWaiters(id, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public int IncrementMax(int id)
|
||||
{
|
||||
return Interlocked.Increment(ref _counterMax[id]);
|
||||
}
|
||||
|
||||
public void AddWaiter(int threshold, EventWaitHandle waitEvent)
|
||||
{
|
||||
if (!_waiters.TryAdd(waitEvent, threshold))
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveWaiter(EventWaitHandle waitEvent)
|
||||
{
|
||||
return _waiters.TryRemove(waitEvent, out _);
|
||||
}
|
||||
|
||||
private void WakeUpWaiters(int id, int newValue)
|
||||
{
|
||||
foreach (KeyValuePair<EventWaitHandle, int> kv in _waiters)
|
||||
{
|
||||
if (MinCompare(id, newValue, _counterMax[id], kv.Value))
|
||||
{
|
||||
kv.Key.Set();
|
||||
|
||||
_waiters.TryRemove(kv.Key, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool MinCompare(int id, int threshold)
|
||||
{
|
||||
return MinCompare(id, _counterMin[id], _counterMax[id], threshold);
|
||||
}
|
||||
|
||||
private bool MinCompare(int id, int min, int max, int threshold)
|
||||
{
|
||||
int minDiff = min - threshold;
|
||||
int maxDiff = max - threshold;
|
||||
|
||||
if (((_eventMask >> id) & 1) != 0)
|
||||
{
|
||||
return minDiff >= 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (uint)maxDiff >= (uint)minDiff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
300
Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvMap/NvMapIoctl.cs
Normal file
300
Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvMap/NvMapIoctl.cs
Normal file
|
@ -0,0 +1,300 @@
|
|||
using ARMeilleure.Memory;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Graphics.Memory;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.Utilities;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
class NvMapIoctl
|
||||
{
|
||||
private const int FlagNotFreedYet = 1;
|
||||
|
||||
private static ConcurrentDictionary<KProcess, IdDictionary> _maps;
|
||||
|
||||
static NvMapIoctl()
|
||||
{
|
||||
_maps = new ConcurrentDictionary<KProcess, IdDictionary>();
|
||||
}
|
||||
|
||||
public static int ProcessIoctl(ServiceCtx context, int cmd)
|
||||
{
|
||||
switch (cmd & 0xffff)
|
||||
{
|
||||
case 0x0101: return Create(context);
|
||||
case 0x0103: return FromId(context);
|
||||
case 0x0104: return Alloc (context);
|
||||
case 0x0105: return Free (context);
|
||||
case 0x0109: return Param (context);
|
||||
case 0x010e: return GetId (context);
|
||||
}
|
||||
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Unsupported Ioctl command 0x{cmd:x8}!");
|
||||
|
||||
return NvResult.NotSupported;
|
||||
}
|
||||
|
||||
private static int Create(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvMapCreate args = MemoryHelper.Read<NvMapCreate>(context.Memory, inputPosition);
|
||||
|
||||
if (args.Size == 0)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid size 0x{args.Size:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
int size = IntUtils.AlignUp(args.Size, NvGpuVmm.PageSize);
|
||||
|
||||
args.Handle = AddNvMap(context, new NvMapHandle(size));
|
||||
|
||||
Logger.PrintInfo(LogClass.ServiceNv, $"Created map {args.Handle} with size 0x{size:x8}!");
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int FromId(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvMapFromId args = MemoryHelper.Read<NvMapFromId>(context.Memory, inputPosition);
|
||||
|
||||
NvMapHandle map = GetNvMap(context, args.Id);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
map.IncrementRefCount();
|
||||
|
||||
args.Handle = args.Id;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int Alloc(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvMapAlloc args = MemoryHelper.Read<NvMapAlloc>(context.Memory, inputPosition);
|
||||
|
||||
NvMapHandle map = GetNvMap(context, args.Handle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
if ((args.Align & (args.Align - 1)) != 0)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid alignment 0x{args.Align:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
if ((uint)args.Align < NvGpuVmm.PageSize)
|
||||
{
|
||||
args.Align = NvGpuVmm.PageSize;
|
||||
}
|
||||
|
||||
int result = NvResult.Success;
|
||||
|
||||
if (!map.Allocated)
|
||||
{
|
||||
map.Allocated = true;
|
||||
|
||||
map.Align = args.Align;
|
||||
map.Kind = (byte)args.Kind;
|
||||
|
||||
int size = IntUtils.AlignUp(map.Size, NvGpuVmm.PageSize);
|
||||
|
||||
long address = args.Address;
|
||||
|
||||
if (address == 0)
|
||||
{
|
||||
// When the address is zero, we need to allocate
|
||||
// our own backing memory for the NvMap.
|
||||
// TODO: Is this allocation inside the transfer memory?
|
||||
result = NvResult.OutOfMemory;
|
||||
}
|
||||
|
||||
if (result == NvResult.Success)
|
||||
{
|
||||
map.Size = size;
|
||||
map.Address = address;
|
||||
}
|
||||
}
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int Free(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvMapFree args = MemoryHelper.Read<NvMapFree>(context.Memory, inputPosition);
|
||||
|
||||
NvMapHandle map = GetNvMap(context, args.Handle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
if (map.DecrementRefCount() <= 0)
|
||||
{
|
||||
DeleteNvMap(context, args.Handle);
|
||||
|
||||
Logger.PrintInfo(LogClass.ServiceNv, $"Deleted map {args.Handle}!");
|
||||
|
||||
args.Address = map.Address;
|
||||
args.Flags = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
args.Address = 0;
|
||||
args.Flags = FlagNotFreedYet;
|
||||
}
|
||||
|
||||
args.Size = map.Size;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int Param(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvMapParam args = MemoryHelper.Read<NvMapParam>(context.Memory, inputPosition);
|
||||
|
||||
NvMapHandle map = GetNvMap(context, args.Handle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
switch ((NvMapHandleParam)args.Param)
|
||||
{
|
||||
case NvMapHandleParam.Size: args.Result = map.Size; break;
|
||||
case NvMapHandleParam.Align: args.Result = map.Align; break;
|
||||
case NvMapHandleParam.Heap: args.Result = 0x40000000; break;
|
||||
case NvMapHandleParam.Kind: args.Result = map.Kind; break;
|
||||
case NvMapHandleParam.Compr: args.Result = 0; break;
|
||||
|
||||
// Note: Base is not supported and returns an error.
|
||||
// Any other value also returns an error.
|
||||
default: return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int GetId(ServiceCtx context)
|
||||
{
|
||||
long inputPosition = context.Request.GetBufferType0x21().Position;
|
||||
long outputPosition = context.Request.GetBufferType0x22().Position;
|
||||
|
||||
NvMapGetId args = MemoryHelper.Read<NvMapGetId>(context.Memory, inputPosition);
|
||||
|
||||
NvMapHandle map = GetNvMap(context, args.Handle);
|
||||
|
||||
if (map == null)
|
||||
{
|
||||
Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{args.Handle:x8}!");
|
||||
|
||||
return NvResult.InvalidInput;
|
||||
}
|
||||
|
||||
args.Id = args.Handle;
|
||||
|
||||
MemoryHelper.Write(context.Memory, outputPosition, args);
|
||||
|
||||
return NvResult.Success;
|
||||
}
|
||||
|
||||
private static int AddNvMap(ServiceCtx context, NvMapHandle map)
|
||||
{
|
||||
IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>
|
||||
{
|
||||
IdDictionary newDict = new IdDictionary();
|
||||
|
||||
newDict.Add(0, new NvMapHandle());
|
||||
|
||||
return newDict;
|
||||
});
|
||||
|
||||
return dict.Add(map);
|
||||
}
|
||||
|
||||
private static bool DeleteNvMap(ServiceCtx context, int handle)
|
||||
{
|
||||
if (_maps.TryGetValue(context.Process, out IdDictionary dict))
|
||||
{
|
||||
return dict.Delete(handle) != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void InitializeNvMap(ServiceCtx context)
|
||||
{
|
||||
IdDictionary dict = _maps.GetOrAdd(context.Process, (key) =>new IdDictionary());
|
||||
|
||||
dict.Add(0, new NvMapHandle());
|
||||
}
|
||||
|
||||
public static NvMapHandle GetNvMapWithFb(ServiceCtx context, int handle)
|
||||
{
|
||||
if (_maps.TryGetValue(context.Process, out IdDictionary dict))
|
||||
{
|
||||
return dict.GetData<NvMapHandle>(handle);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static NvMapHandle GetNvMap(ServiceCtx context, int handle)
|
||||
{
|
||||
if (handle != 0 && _maps.TryGetValue(context.Process, out IdDictionary dict))
|
||||
{
|
||||
return dict.GetData<NvMapHandle>(handle);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void UnloadProcess(KProcess process)
|
||||
{
|
||||
_maps.TryRemove(process, out _);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
struct NvMapAlloc
|
||||
{
|
||||
public int Handle;
|
||||
public int HeapMask;
|
||||
public int Flags;
|
||||
public int Align;
|
||||
public long Kind;
|
||||
public long Address;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
struct NvMapCreate
|
||||
{
|
||||
public int Size;
|
||||
public int Handle;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
struct NvMapFree
|
||||
{
|
||||
public int Handle;
|
||||
public int Padding;
|
||||
public long Address;
|
||||
public int Size;
|
||||
public int Flags;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
struct NvMapFromId
|
||||
{
|
||||
public int Id;
|
||||
public int Handle;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
struct NvMapGetId
|
||||
{
|
||||
public int Id;
|
||||
public int Handle;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
class NvMapHandle
|
||||
{
|
||||
public int Handle;
|
||||
public int Id;
|
||||
public int Size;
|
||||
public int Align;
|
||||
public int Kind;
|
||||
public long Address;
|
||||
public bool Allocated;
|
||||
public long DmaMapAddress;
|
||||
|
||||
private long _dupes;
|
||||
|
||||
public NvMapHandle()
|
||||
{
|
||||
_dupes = 1;
|
||||
}
|
||||
|
||||
public NvMapHandle(int size) : this()
|
||||
{
|
||||
Size = size;
|
||||
}
|
||||
|
||||
public void IncrementRefCount()
|
||||
{
|
||||
Interlocked.Increment(ref _dupes);
|
||||
}
|
||||
|
||||
public long DecrementRefCount()
|
||||
{
|
||||
return Interlocked.Decrement(ref _dupes);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
enum NvMapHandleParam
|
||||
{
|
||||
Size = 1,
|
||||
Align = 2,
|
||||
Base = 3,
|
||||
Heap = 4,
|
||||
Kind = 5,
|
||||
Compr = 6
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
|
||||
{
|
||||
struct NvMapParam
|
||||
{
|
||||
public int Handle;
|
||||
public int Param;
|
||||
public int Result;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue