Implement GPU syncpoints (#980)

* Implement GPU syncpoints

This adds support for GPU syncpoints on the GPU backend & nvservices.

Everything that was implemented here is based on my researches,
hardware testing of the GM20B and reversing of nvservices (8.1.0).

Thanks to @fincs for the informations about some behaviours of the pusher
and for the initial informations about syncpoints.

* syncpoint: address gdkchan's comments

* Add some missing logic to handle SubmitGpfifo correctly

* Handle the NV event API correctly

* evnt => hostEvent

* Finish addressing gdkchan's comments

* nvservices: write the output buffer even when an error is returned

* dma pusher: Implemnet prefetch barrier

lso fix when the commands should be prefetch.

* Partially fix prefetch barrier

* Add a missing syncpoint check in QueryEvent of NvHostSyncPt

* Address Ac_K's comments and fix GetSyncpoint for ChannelResourcePolicy == Channel

* fix SyncptWait & SyncptWaitEx cmds logic

* Address ripinperi's comments

* Address gdkchan's comments

* Move user event management to the control channel

* Fix mm implementation, nvdec works again

* Address ripinperi's comments

* Address gdkchan's comments

* Implement nvhost-ctrl close accurately + make nvservices dispose channels when stopping the emulator

* Fix typo in MultiMediaOperationType
This commit is contained in:
Thog 2020-04-19 03:25:57 +02:00 committed by GitHub
parent 4960ab85f8
commit 644de99e86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 1576 additions and 386 deletions

View file

@ -264,7 +264,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv
errorCode = ConvertInternalErrorCode(internalResult);
if (errorCode == NvResult.Success && (ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
{
context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
}
@ -452,7 +452,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv
errorCode = ConvertInternalErrorCode(internalResult);
if (errorCode == NvResult.Success && (ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
{
context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
}
@ -497,7 +497,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv
errorCode = ConvertInternalErrorCode(internalResult);
if (errorCode == NvResult.Success && (ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0)
{
context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray());
context.Memory.WriteBytes(inlineOutBufferPosition, inlineOutBuffer.ToArray());
@ -519,5 +519,17 @@ namespace Ryujinx.HLE.HOS.Services.Nv
return ResultCode.Success;
}
public static void Destroy()
{
foreach (object entry in _deviceFileIdRegistry.Values)
{
NvDeviceFile deviceFile = (NvDeviceFile)entry;
deviceFile.Close();
}
_deviceFileIdRegistry.Clear();
}
}
}

View file

@ -1,8 +1,9 @@
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Gpu;
using Ryujinx.Graphics.Gpu.Memory;
using Ryujinx.HLE.HOS.Services.Nv.Types;
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostAsGpu;
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel.Types;
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl;
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
using System;
using System.Runtime.CompilerServices;
@ -12,21 +13,42 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
{
class NvHostChannelDeviceFile : NvDeviceFile
{
private const uint MaxModuleSyncpoint = 16;
private uint _timeout;
private uint _submitTimeout;
private uint _timeslice;
private GpuContext _gpu;
private Switch _device;
private ARMeilleure.Memory.MemoryManager _memory;
public enum ResourcePolicy
{
Device,
Channel
}
protected static uint[] DeviceSyncpoints = new uint[MaxModuleSyncpoint];
protected uint[] ChannelSyncpoints;
protected static ResourcePolicy ChannelResourcePolicy = ResourcePolicy.Device;
private NvFence _channelSyncpoint;
public NvHostChannelDeviceFile(ServiceCtx context) : base(context)
{
_gpu = context.Device.Gpu;
_device = context.Device;
_memory = context.Memory;
_timeout = 3000;
_submitTimeout = 0;
_timeslice = 0;
ChannelSyncpoints = new uint[MaxModuleSyncpoint];
_channelSyncpoint.Id = _device.System.HostSyncpoint.AllocateSyncpoint(false);
_channelSyncpoint.UpdateValue(_device.System.HostSyncpoint);
}
public override NvInternalResult Ioctl(NvIoctl command, Span<byte> arguments)
@ -132,9 +154,24 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
private NvInternalResult GetSyncpoint(ref GetParameterArguments arguments)
{
arguments.Value = 0;
if (arguments.Parameter >= MaxModuleSyncpoint)
{
return NvInternalResult.InvalidInput;
}
Logger.PrintStub(LogClass.ServiceNv);
if (ChannelResourcePolicy == ResourcePolicy.Device)
{
arguments.Value = GetSyncpointDevice(_device.System.HostSyncpoint, arguments.Parameter, false);
}
else
{
arguments.Value = GetSyncpointChannel(arguments.Parameter, false);
}
if (arguments.Value == 0)
{
return NvInternalResult.TryAgain;
}
return NvInternalResult.Success;
}
@ -293,6 +330,10 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
private NvInternalResult AllocGpfifoEx(ref AllocGpfifoExArguments arguments)
{
_channelSyncpoint.UpdateValue(_device.System.HostSyncpoint);
arguments.Fence = _channelSyncpoint;
Logger.PrintStub(LogClass.ServiceNv);
return NvInternalResult.Success;
@ -300,6 +341,10 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
private NvInternalResult AllocGpfifoEx2(ref AllocGpfifoExArguments arguments)
{
_channelSyncpoint.UpdateValue(_device.System.HostSyncpoint);
arguments.Fence = _channelSyncpoint;
Logger.PrintStub(LogClass.ServiceNv);
return NvInternalResult.Success;
@ -330,17 +375,125 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
protected NvInternalResult SubmitGpfifo(ref SubmitGpfifoArguments header, Span<ulong> entries)
{
foreach (ulong entry in entries)
if (header.Flags.HasFlag(SubmitGpfifoFlags.FenceWait) && header.Flags.HasFlag(SubmitGpfifoFlags.IncrementWithValue))
{
_gpu.DmaPusher.Push(entry);
return NvInternalResult.InvalidInput;
}
header.Fence.Id = 0;
header.Fence.Value = 0;
if (header.Flags.HasFlag(SubmitGpfifoFlags.FenceWait) && !_device.System.HostSyncpoint.IsSyncpointExpired(header.Fence.Id, header.Fence.Value))
{
_device.Gpu.DmaPusher.PushHostCommandBuffer(CreateWaitCommandBuffer(header.Fence));
}
_device.Gpu.DmaPusher.PushEntries(entries);
header.Fence.Id = _channelSyncpoint.Id;
if (header.Flags.HasFlag(SubmitGpfifoFlags.FenceIncrement) || header.Flags.HasFlag(SubmitGpfifoFlags.IncrementWithValue))
{
uint incrementCount = header.Flags.HasFlag(SubmitGpfifoFlags.FenceIncrement) ? 2u : 0u;
if (header.Flags.HasFlag(SubmitGpfifoFlags.IncrementWithValue))
{
incrementCount += header.Fence.Value;
}
header.Fence.Value = _device.System.HostSyncpoint.IncrementSyncpointMaxExt(header.Fence.Id, (int)incrementCount);
}
else
{
header.Fence.Value = _device.System.HostSyncpoint.ReadSyncpointMaxValue(header.Fence.Id);
}
if (header.Flags.HasFlag(SubmitGpfifoFlags.FenceIncrement))
{
_device.Gpu.DmaPusher.PushHostCommandBuffer(CreateIncrementCommandBuffer(ref header.Fence, header.Flags));
}
header.Flags = SubmitGpfifoFlags.None;
_device.Gpu.DmaPusher.SignalNewEntries();
return NvInternalResult.Success;
}
public uint GetSyncpointChannel(uint index, bool isClientManaged)
{
if (ChannelSyncpoints[index] != 0)
{
return ChannelSyncpoints[index];
}
ChannelSyncpoints[index] = _device.System.HostSyncpoint.AllocateSyncpoint(isClientManaged);
return ChannelSyncpoints[index];
}
public static uint GetSyncpointDevice(NvHostSyncpt syncpointManager, uint index, bool isClientManaged)
{
if (DeviceSyncpoints[index] != 0)
{
return DeviceSyncpoints[index];
}
DeviceSyncpoints[index] = syncpointManager.AllocateSyncpoint(isClientManaged);
return DeviceSyncpoints[index];
}
private static int[] CreateWaitCommandBuffer(NvFence fence)
{
int[] commandBuffer = new int[4];
// SyncpointValue = fence.Value;
commandBuffer[0] = 0x2001001C;
commandBuffer[1] = (int)fence.Value;
// SyncpointAction(fence.id, increment: false, switch_en: true);
commandBuffer[2] = 0x2001001D;
commandBuffer[3] = (((int)fence.Id << 8) | (0 << 0) | (1 << 4));
return commandBuffer;
}
private int[] CreateIncrementCommandBuffer(ref NvFence fence, SubmitGpfifoFlags flags)
{
bool hasWfi = !flags.HasFlag(SubmitGpfifoFlags.SuppressWfi);
int[] commandBuffer;
int offset = 0;
if (hasWfi)
{
commandBuffer = new int[8];
// WaitForInterrupt(handle)
commandBuffer[offset++] = 0x2001001E;
commandBuffer[offset++] = 0x0;
}
else
{
commandBuffer = new int[6];
}
// SyncpointValue = 0x0;
commandBuffer[offset++] = 0x2001001C;
commandBuffer[offset++] = 0x0;
// Increment the syncpoint 2 times. (mitigate a hardware bug)
// SyncpointAction(fence.id, increment: true, switch_en: false);
commandBuffer[offset++] = 0x2001001D;
commandBuffer[offset++] = (((int)fence.Id << 8) | (1 << 0) | (0 << 4));
// SyncpointAction(fence.id, increment: true, switch_en: false);
commandBuffer[offset++] = 0x2001001D;
commandBuffer[offset++] = (((int)fence.Id << 8) | (1 << 0) | (0 << 4));
return commandBuffer;
}
public override void Close() { }
}
}

View file

@ -6,9 +6,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel.Types
[StructLayout(LayoutKind.Sequential)]
struct SubmitGpfifoArguments
{
public long Address;
public int NumEntries;
public int Flags;
public NvFence Fence;
public long Address;
public int NumEntries;
public SubmitGpfifoFlags Flags;
public NvFence Fence;
}
}

View file

@ -0,0 +1,15 @@
using System;
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel.Types
{
[Flags]
enum SubmitGpfifoFlags : uint
{
None,
FenceWait = 1 << 0,
FenceIncrement = 1 << 1,
HwFormat = 1 << 2,
SuppressWfi = 1 << 4,
IncrementWithValue = 1 << 8,
}
}

View file

@ -1,4 +1,5 @@
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Gpu.Synchronization;
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl.Types;
@ -13,12 +14,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
{
internal class NvHostCtrlDeviceFile : NvDeviceFile
{
private const int EventsCount = 64;
public const int EventsCount = 64;
private bool _isProductionMode;
private NvHostSyncpt _syncpt;
private Switch _device;
private NvHostEvent[] _events;
private KEvent _dummyEvent;
public NvHostCtrlDeviceFile(ServiceCtx context) : base(context)
{
@ -31,9 +31,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
_isProductionMode = true;
}
_syncpt = new NvHostSyncpt();
_events = new NvHostEvent[EventsCount];
_dummyEvent = new KEvent(context.Device.System);
_device = context.Device;
_events = new NvHostEvent[EventsCount];
}
public override NvInternalResult Ioctl(NvIoctl command, Span<byte> arguments)
@ -69,6 +69,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
configArgument.CopyTo(arguments);
}
break;
case 0x1c:
result = CallIoctlMethod<uint>(EventSignal, arguments);
break;
case 0x1d:
result = CallIoctlMethod<EventWaitArguments>(EventWait, arguments);
break;
@ -78,16 +81,45 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
case 0x1f:
result = CallIoctlMethod<uint>(EventRegister, arguments);
break;
case 0x20:
result = CallIoctlMethod<uint>(EventUnregister, arguments);
break;
case 0x21:
result = CallIoctlMethod<ulong>(EventKill, arguments);
break;
}
}
return result;
}
private KEvent QueryEvent(uint eventId)
{
uint eventSlot;
uint syncpointId;
if ((eventId >> 28) == 1)
{
eventSlot = eventId & 0xFFFF;
syncpointId = (eventId >> 16) & 0xFFF;
}
else
{
eventSlot = eventId & 0xFF;
syncpointId = eventId >> 4;
}
if (eventSlot >= EventsCount || _events[eventSlot] == null || _events[eventSlot].Fence.Id != syncpointId)
{
return null;
}
return _events[eventSlot].Event;
}
public override NvInternalResult QueryEvent(out int eventHandle, uint eventId)
{
// TODO: implement SyncPts <=> KEvent logic accurately. For now we return a dummy event.
KEvent targetEvent = _dummyEvent;
KEvent targetEvent = QueryEvent(eventId);
if (targetEvent != null)
{
@ -113,24 +145,26 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
private NvInternalResult SyncptIncr(ref uint id)
{
if (id >= NvHostSyncpt.SyncptsCount)
if (id >= SynchronizationManager.MaxHardwareSyncpoints)
{
return NvInternalResult.InvalidInput;
}
_syncpt.Increment((int)id);
_device.System.HostSyncpoint.Increment(id);
return NvInternalResult.Success;
}
private NvInternalResult SyncptWait(ref SyncptWaitArguments arguments)
{
return SyncptWait(ref arguments, out _);
uint dummyValue = 0;
return EventWait(ref arguments.Fence, ref dummyValue, arguments.Timeout, isWaitEventAsyncCmd: false, isWaitEventCmd: false);
}
private NvInternalResult SyncptWaitEx(ref SyncptWaitExArguments arguments)
{
return SyncptWait(ref arguments.Input, out arguments.Value);
return EventWait(ref arguments.Input.Fence, ref arguments.Value, arguments.Input.Timeout, isWaitEventAsyncCmd: false, isWaitEventCmd: false);
}
private NvInternalResult SyncptReadMax(ref NvFence arguments)
@ -182,194 +216,237 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
private NvInternalResult EventWait(ref EventWaitArguments arguments)
{
return EventWait(ref arguments, async: false);
return EventWait(ref arguments.Fence, ref arguments.Value, arguments.Timeout, isWaitEventAsyncCmd: false, isWaitEventCmd: true);
}
private NvInternalResult EventWaitAsync(ref EventWaitArguments arguments)
{
return EventWait(ref arguments, async: true);
return EventWait(ref arguments.Fence, ref arguments.Value, arguments.Timeout, isWaitEventAsyncCmd: true, isWaitEventCmd: false);
}
private NvInternalResult EventRegister(ref uint userEventId)
{
Logger.PrintStub(LogClass.ServiceNv);
NvInternalResult result = EventUnregister(ref userEventId);
if (result == NvInternalResult.Success)
{
_events[userEventId] = new NvHostEvent(_device.System.HostSyncpoint, userEventId, _device.System);
}
return result;
}
private NvInternalResult EventUnregister(ref uint userEventId)
{
if (userEventId >= EventsCount)
{
return NvInternalResult.InvalidInput;
}
NvHostEvent hostEvent = _events[userEventId];
if (hostEvent == null)
{
return NvInternalResult.Success;
}
if (hostEvent.State == NvHostEventState.Available ||
hostEvent.State == NvHostEventState.Cancelled ||
hostEvent.State == NvHostEventState.Signaled)
{
_events[userEventId].Dispose();
_events[userEventId] = null;
return NvInternalResult.Success;
}
return NvInternalResult.Busy;
}
private NvInternalResult EventKill(ref ulong eventMask)
{
NvInternalResult result = NvInternalResult.Success;
for (uint eventId = 0; eventId < EventsCount; eventId++)
{
if ((eventMask & (1UL << (int)eventId)) != 0)
{
NvInternalResult tmp = EventUnregister(ref eventId);
if (tmp != NvInternalResult.Success)
{
result = tmp;
}
}
}
return result;
}
private NvInternalResult EventSignal(ref uint userEventId)
{
uint eventId = userEventId & ushort.MaxValue;
if (eventId >= EventsCount)
{
return NvInternalResult.InvalidInput;
}
NvHostEvent hostEvent = _events[eventId];
if (hostEvent == null)
{
return NvInternalResult.InvalidInput;
}
NvHostEventState oldState = hostEvent.State;
if (oldState == NvHostEventState.Waiting)
{
hostEvent.State = NvHostEventState.Cancelling;
hostEvent.Cancel(_device.Gpu);
}
hostEvent.State = NvHostEventState.Cancelled;
return NvInternalResult.Success;
}
private NvInternalResult SyncptReadMinOrMax(ref NvFence arguments, bool max)
{
if (arguments.Id >= NvHostSyncpt.SyncptsCount)
if (arguments.Id >= SynchronizationManager.MaxHardwareSyncpoints)
{
return NvInternalResult.InvalidInput;
}
if (max)
{
arguments.Value = (uint)_syncpt.GetMax((int)arguments.Id);
arguments.Value = _device.System.HostSyncpoint.ReadSyncpointMaxValue(arguments.Id);
}
else
{
arguments.Value = (uint)_syncpt.GetMin((int)arguments.Id);
arguments.Value = _device.System.HostSyncpoint.ReadSyncpointValue(arguments.Id);
}
return NvInternalResult.Success;
}
private NvInternalResult SyncptWait(ref SyncptWaitArguments arguments, out int value)
private NvInternalResult EventWait(ref NvFence fence, ref uint value, int timeout, bool isWaitEventAsyncCmd, bool isWaitEventCmd)
{
if (arguments.Id >= NvHostSyncpt.SyncptsCount)
{
value = 0;
return NvInternalResult.InvalidInput;
}
NvInternalResult result;
if (_syncpt.MinCompare((int)arguments.Id, arguments.Thresh))
{
result = NvInternalResult.Success;
}
else if (arguments.Timeout == 0)
{
result = NvInternalResult.TryAgain;
}
else
{
Logger.PrintDebug(LogClass.ServiceNv, $"Waiting syncpt with timeout of {arguments.Timeout}ms...");
using (ManualResetEvent waitEvent = new ManualResetEvent(false))
{
_syncpt.AddWaiter(arguments.Thresh, waitEvent);
// Note: Negative (> INT_MAX) timeouts aren't valid on .NET,
// in this case we just use the maximum timeout possible.
int timeout = arguments.Timeout;
if (timeout < -1)
{
timeout = int.MaxValue;
}
if (timeout == -1)
{
waitEvent.WaitOne();
result = NvInternalResult.Success;
}
else if (waitEvent.WaitOne(timeout))
{
result = NvInternalResult.Success;
}
else
{
result = NvInternalResult.TimedOut;
}
}
Logger.PrintDebug(LogClass.ServiceNv, "Resuming...");
}
value = _syncpt.GetMin((int)arguments.Id);
return result;
}
private NvInternalResult EventWait(ref EventWaitArguments arguments, bool async)
{
if (arguments.Id >= NvHostSyncpt.SyncptsCount)
if (fence.Id >= SynchronizationManager.MaxHardwareSyncpoints)
{
return NvInternalResult.InvalidInput;
}
if (_syncpt.MinCompare(arguments.Id, arguments.Thresh))
// First try to check if the syncpoint is already expired on the CPU side
if (_device.System.HostSyncpoint.IsSyncpointExpired(fence.Id, fence.Value))
{
arguments.Value = _syncpt.GetMin(arguments.Id);
value = _device.System.HostSyncpoint.ReadSyncpointMinValue(fence.Id);
return NvInternalResult.Success;
}
if (!async)
// Try to invalidate the CPU cache and check for expiration again.
uint newCachedSyncpointValue = _device.System.HostSyncpoint.UpdateMin(fence.Id);
// Has the fence already expired?
if (_device.System.HostSyncpoint.IsSyncpointExpired(fence.Id, fence.Value))
{
arguments.Value = 0;
value = newCachedSyncpointValue;
return NvInternalResult.Success;
}
if (arguments.Timeout == 0)
// If the timeout is 0, directly return.
if (timeout == 0)
{
return NvInternalResult.TryAgain;
}
NvHostEvent Event;
// The syncpoint value isn't at the fence yet, we need to wait.
if (!isWaitEventAsyncCmd)
{
value = 0;
}
NvHostEvent hostEvent;
NvInternalResult result;
int eventIndex;
uint eventIndex;
if (async)
if (isWaitEventAsyncCmd)
{
eventIndex = arguments.Value;
eventIndex = value;
if ((uint)eventIndex >= EventsCount)
if (eventIndex >= EventsCount)
{
return NvInternalResult.InvalidInput;
}
Event = _events[eventIndex];
hostEvent = _events[eventIndex];
}
else
{
Event = GetFreeEvent(arguments.Id, out eventIndex);
hostEvent = GetFreeEvent(fence.Id, out eventIndex);
}
if (Event != null &&
(Event.State == NvHostEventState.Registered ||
Event.State == NvHostEventState.Free))
if (hostEvent != null &&
(hostEvent.State == NvHostEventState.Available ||
hostEvent.State == NvHostEventState.Signaled ||
hostEvent.State == NvHostEventState.Cancelled))
{
Event.Id = arguments.Id;
Event.Thresh = arguments.Thresh;
hostEvent.Wait(_device.Gpu, fence);
Event.State = NvHostEventState.Waiting;
if (!async)
if (isWaitEventCmd)
{
arguments.Value = ((arguments.Id & 0xfff) << 16) | 0x10000000;
value = ((fence.Id & 0xfff) << 16) | 0x10000000;
}
else
{
arguments.Value = arguments.Id << 4;
value = fence.Id << 4;
}
arguments.Value |= eventIndex;
value |= eventIndex;
result = NvInternalResult.TryAgain;
}
else
{
Logger.PrintError(LogClass.ServiceNv, $"Invalid Event at index {eventIndex} (isWaitEventAsyncCmd: {isWaitEventAsyncCmd}, isWaitEventCmd: {isWaitEventCmd})");
if (hostEvent != null)
{
Logger.PrintError(LogClass.ServiceNv, hostEvent.DumpState(_device.Gpu));
}
result = NvInternalResult.InvalidInput;
}
return result;
}
private NvHostEvent GetFreeEvent(int id, out int eventIndex)
public NvHostEvent GetFreeEvent(uint id, out uint eventIndex)
{
eventIndex = EventsCount;
int nullIndex = EventsCount;
uint nullIndex = EventsCount;
for (int index = 0; index < EventsCount; index++)
for (uint index = 0; index < EventsCount; index++)
{
NvHostEvent Event = _events[index];
if (Event != null)
{
if (Event.State == NvHostEventState.Registered ||
Event.State == NvHostEventState.Free)
if (Event.State == NvHostEventState.Available ||
Event.State == NvHostEventState.Signaled ||
Event.State == NvHostEventState.Cancelled)
{
eventIndex = index;
if (Event.Id == id)
if (Event.Fence.Id == id)
{
return Event;
}
@ -385,7 +462,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
{
eventIndex = nullIndex;
return _events[nullIndex] = new NvHostEvent();
EventRegister(ref eventIndex);
return _events[nullIndex];
}
if (eventIndex < EventsCount)
@ -396,6 +475,44 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
return null;
}
public override void Close() { }
public override void Close()
{
Logger.PrintWarning(LogClass.ServiceNv, "Closing channel");
// If the device file need to be closed, cancel all user events and dispose events.
for (int i = 0; i < _events.Length; i++)
{
NvHostEvent evnt = _events[i];
if (evnt != null)
{
if (evnt.State == NvHostEventState.Waiting)
{
evnt.State = NvHostEventState.Cancelling;
evnt.Cancel(_device.Gpu);
}
else if (evnt.State == NvHostEventState.Signaling)
{
// Wait at max 9ms if the guest app is trying to signal the event while closing it..
int retryCount = 0;
do
{
if (retryCount++ > 9)
{
break;
}
// TODO: This should be handled by the kernel (reschedule the current thread ect), waiting for Kernel decoupling work.
Thread.Sleep(1);
} while (evnt.State != NvHostEventState.Signaled);
}
evnt.Dispose();
_events[i] = null;
}
}
}
}
}

View file

@ -1,13 +1,13 @@
using System.Runtime.InteropServices;
using Ryujinx.HLE.HOS.Services.Nv.Types;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl.Types
{
[StructLayout(LayoutKind.Sequential)]
struct EventWaitArguments
{
public int Id;
public int Thresh;
public int Timeout;
public int Value;
public NvFence Fence;
public int Timeout;
public uint Value;
}
}

View file

@ -1,10 +1,101 @@
using Ryujinx.Graphics.Gpu;
using Ryujinx.Graphics.Gpu.Synchronization;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Nv.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
{
class NvHostEvent
class NvHostEvent : IDisposable
{
public int Id;
public int Thresh;
public NvFence Fence;
public NvHostEventState State;
public KEvent Event;
private uint _eventId;
private NvHostSyncpt _syncpointManager;
private SyncpointWaiterHandle _waiterInformation;
public NvHostEvent(NvHostSyncpt syncpointManager, uint eventId, Horizon system)
{
Fence.Id = 0;
State = NvHostEventState.Available;
Event = new KEvent(system);
_eventId = eventId;
_syncpointManager = syncpointManager;
}
public void Reset()
{
Fence.Id = NvFence.InvalidSyncPointId;
Fence.Value = 0;
State = NvHostEventState.Available;
}
private void Signal()
{
NvHostEventState oldState = State;
State = NvHostEventState.Signaling;
if (oldState == NvHostEventState.Waiting)
{
Event.WritableEvent.Signal();
}
State = NvHostEventState.Signaled;
}
private void GpuSignaled()
{
Signal();
}
public void Cancel(GpuContext gpuContext)
{
if (_waiterInformation != null)
{
gpuContext.Synchronization.UnregisterCallback(Fence.Id, _waiterInformation);
Signal();
}
Event.WritableEvent.Clear();
}
public void Wait(GpuContext gpuContext, NvFence fence)
{
Fence = fence;
State = NvHostEventState.Waiting;
_waiterInformation = gpuContext.Synchronization.RegisterCallbackOnSyncpoint(Fence.Id, Fence.Value, GpuSignaled);
}
public string DumpState(GpuContext gpuContext)
{
string res = $"\nNvHostEvent {_eventId}:\n";
res += $"\tState: {State}\n";
if (State == NvHostEventState.Waiting)
{
res += "\tFence:\n";
res += $"\t\tId : {Fence.Id}\n";
res += $"\t\tThreshold : {Fence.Value}\n";
res += $"\t\tCurrent Value : {gpuContext.Synchronization.GetSyncpointValue(Fence.Id)}\n";
res += $"\t\tWaiter Valid : {_waiterInformation != null}\n";
}
return res;
}
public void Dispose()
{
Event.ReadableEvent.DecrementReferenceCount();
Event.WritableEvent.DecrementReferenceCount();
}
}
}

View file

@ -2,9 +2,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl
{
enum NvHostEventState
{
Registered = 0,
Available = 0,
Waiting = 1,
Busy = 2,
Free = 5
Cancelling = 2,
Signaling = 3,
Signaled = 4,
Cancelled = 5
}
}

View file

@ -1,100 +1,181 @@
using Ryujinx.Common.Logging;
using Ryujinx.Graphics.Gpu.Synchronization;
using Ryujinx.HLE.HOS.Kernel.Threading;
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 bool[] _clientManaged;
private bool[] _assigned;
private int[] _counterMin;
private int[] _counterMax;
private Switch _device;
private long _eventMask;
private object _syncpointAllocatorLock = new object();
private ConcurrentDictionary<EventWaitHandle, int> _waiters;
public NvHostSyncpt()
public NvHostSyncpt(Switch device)
{
_counterMin = new int[SyncptsCount];
_counterMax = new int[SyncptsCount];
_waiters = new ConcurrentDictionary<EventWaitHandle, int>();
_device = device;
_counterMin = new int[SynchronizationManager.MaxHardwareSyncpoints];
_counterMax = new int[SynchronizationManager.MaxHardwareSyncpoints];
_clientManaged = new bool[SynchronizationManager.MaxHardwareSyncpoints];
_assigned = new bool[SynchronizationManager.MaxHardwareSyncpoints];
}
public int GetMin(int id)
private void ReserveSyncpointLocked(uint id, bool isClientManaged)
{
return _counterMin[id];
}
public int GetMax(int id)
{
return _counterMax[id];
}
public int Increment(int id)
{
if (((_eventMask >> id) & 1) != 0)
if (id >= SynchronizationManager.MaxHardwareSyncpoints || _assigned[id])
{
Interlocked.Increment(ref _counterMax[id]);
throw new ArgumentOutOfRangeException(nameof(id));
}
return IncrementMin(id);
_assigned[id] = true;
_clientManaged[id] = isClientManaged;
}
public int IncrementMin(int id)
public uint AllocateSyncpoint(bool isClientManaged)
{
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))
lock (_syncpointAllocatorLock)
{
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))
for (uint i = 1; i < SynchronizationManager.MaxHardwareSyncpoints; i++)
{
kv.Key.Set();
_waiters.TryRemove(kv.Key, out _);
if (!_assigned[i])
{
ReserveSyncpointLocked(i, isClientManaged);
return i;
}
}
}
Logger.PrintError(LogClass.ServiceNv, "Cannot allocate a new syncpoint!");
return 0;
}
public bool MinCompare(int id, int threshold)
public void ReleaseSyncpoint(uint id)
{
return MinCompare(id, _counterMin[id], _counterMax[id], threshold);
if (id == 0)
{
return;
}
lock (_syncpointAllocatorLock)
{
if (id >= SynchronizationManager.MaxHardwareSyncpoints || !_assigned[id])
{
throw new ArgumentOutOfRangeException(nameof(id));
}
_assigned[id] = false;
_clientManaged[id] = false;
SetSyncpointMinEqualSyncpointMax(id);
}
}
private bool MinCompare(int id, int min, int max, int threshold)
public void SetSyncpointMinEqualSyncpointMax(uint id)
{
if (id >= SynchronizationManager.MaxHardwareSyncpoints)
{
throw new ArgumentOutOfRangeException(nameof(id));
}
int value = (int)ReadSyncpointValue(id);
Interlocked.Exchange(ref _counterMax[id], value);
}
public uint ReadSyncpointValue(uint id)
{
return UpdateMin(id);
}
public uint ReadSyncpointMinValue(uint id)
{
return (uint)_counterMin[id];
}
public uint ReadSyncpointMaxValue(uint id)
{
return (uint)_counterMax[id];
}
private bool IsClientManaged(uint id)
{
if (id >= SynchronizationManager.MaxHardwareSyncpoints)
{
return false;
}
return _clientManaged[id];
}
public void Increment(uint id)
{
if (IsClientManaged(id))
{
IncrementSyncpointMax(id);
}
IncrementSyncpointGPU(id);
}
public uint UpdateMin(uint id)
{
uint newValue = _device.Gpu.Synchronization.GetSyncpointValue(id);
Interlocked.Exchange(ref _counterMin[id], (int)newValue);
return newValue;
}
private void IncrementSyncpointGPU(uint id)
{
_device.Gpu.Synchronization.IncrementSyncpoint(id);
}
public void IncrementSyncpointMin(uint id)
{
Interlocked.Increment(ref _counterMin[id]);
}
public uint IncrementSyncpointMaxExt(uint id, int count)
{
if (count == 0)
{
return ReadSyncpointMaxValue(id);
}
uint result = 0;
for (int i = 0; i < count; i++)
{
result = IncrementSyncpointMax(id);
}
return result;
}
private uint IncrementSyncpointMax(uint id)
{
return (uint)Interlocked.Increment(ref _counterMax[id]);
}
public bool IsSyncpointExpired(uint id, uint threshold)
{
return MinCompare(id, _counterMin[id], _counterMax[id], (int)threshold);
}
private bool MinCompare(uint id, int min, int max, int threshold)
{
int minDiff = min - threshold;
int maxDiff = max - threshold;
if (((_eventMask >> id) & 1) != 0)
if (IsClientManaged(id))
{
return minDiff >= 0;
}

View file

@ -1,12 +1,12 @@
using System.Runtime.InteropServices;
using Ryujinx.HLE.HOS.Services.Nv.Types;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl.Types
{
[StructLayout(LayoutKind.Sequential)]
struct SyncptWaitArguments
{
public uint Id;
public int Thresh;
public int Timeout;
public NvFence Fence;
public int Timeout;
}
}

View file

@ -6,6 +6,6 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl.Types
struct SyncptWaitExArguments
{
public SyncptWaitArguments Input;
public int Value;
public uint Value;
}
}

View file

@ -1,11 +1,36 @@
using System.Runtime.InteropServices;
using Ryujinx.Graphics.Gpu;
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl;
using System;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Nv.Types
{
[StructLayout(LayoutKind.Sequential, Size = 0x8)]
internal struct NvFence
{
public const uint InvalidSyncPointId = uint.MaxValue;
public uint Id;
public uint Value;
public bool IsValid()
{
return Id != InvalidSyncPointId;
}
public void UpdateValue(NvHostSyncpt hostSyncpt)
{
Value = hostSyncpt.ReadSyncpointValue(Id);
}
public bool Wait(GpuContext gpuContext, TimeSpan timeout)
{
if (IsValid())
{
return gpuContext.Synchronization.WaitOnSyncpoint(Id, Value, timeout);
}
return false;
}
}
}