2018-11-17 05:01:31 +01:00
|
|
|
using Ryujinx.Graphics.Memory;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics
|
|
|
|
{
|
|
|
|
public class DmaPusher
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
private ConcurrentQueue<(NvGpuVmm, long)> _ibBuffer;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private long _dmaPut;
|
|
|
|
private long _dmaGet;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
private struct DmaState
|
|
|
|
{
|
|
|
|
public int Method;
|
|
|
|
public int SubChannel;
|
|
|
|
public int MethodCount;
|
|
|
|
public bool NonIncrementing;
|
|
|
|
public bool IncrementOnce;
|
|
|
|
public int LengthPending;
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private DmaState _state;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private bool _sliEnable;
|
|
|
|
private bool _sliActive;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private bool _ibEnable;
|
|
|
|
private bool _nonMain;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private long _dmaMGet;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private NvGpuVmm _vmm;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private NvGpu _gpu;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private AutoResetEvent _event;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public DmaPusher(NvGpu gpu)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_gpu = gpu;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_ibBuffer = new ConcurrentQueue<(NvGpuVmm, long)>();
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_ibEnable = true;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_event = new AutoResetEvent(false);
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
public void Push(NvGpuVmm vmm, long entry)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_ibBuffer.Enqueue((vmm, entry));
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_event.Set();
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool WaitForCommands()
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
return _event.WaitOne(8);
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DispatchCalls()
|
|
|
|
{
|
|
|
|
while (Step());
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool Step()
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_dmaGet != _dmaPut)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
int word = _vmm.ReadInt32(_dmaGet);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_dmaGet += 4;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (!_nonMain)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_dmaMGet = _dmaGet;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_state.LengthPending != 0)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.LengthPending = 0;
|
|
|
|
_state.MethodCount = word & 0xffffff;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
else if (_state.MethodCount != 0)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (!_sliEnable || _sliActive)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
CallMethod(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (!_state.NonIncrementing)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method++;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_state.IncrementOnce)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = true;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.MethodCount--;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
int sumissionMode = (word >> 29) & 7;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
switch (sumissionMode)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
//Incrementing.
|
2019-03-04 02:45:25 +01:00
|
|
|
SetNonImmediateState(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = false;
|
|
|
|
_state.IncrementOnce = false;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
//Non-incrementing.
|
2019-03-04 02:45:25 +01:00
|
|
|
SetNonImmediateState(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = true;
|
|
|
|
_state.IncrementOnce = false;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
//Immediate.
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method = (word >> 0) & 0x1fff;
|
|
|
|
_state.SubChannel = (word >> 13) & 7;
|
|
|
|
_state.NonIncrementing = true;
|
|
|
|
_state.IncrementOnce = false;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
CallMethod((word >> 16) & 0x1fff);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
//Increment-once.
|
2019-03-04 02:45:25 +01:00
|
|
|
SetNonImmediateState(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = false;
|
|
|
|
_state.IncrementOnce = true;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
else if (_ibEnable && _ibBuffer.TryDequeue(out (NvGpuVmm Vmm, long Entry) tuple))
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_vmm = tuple.Vmm;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
long entry = tuple.Entry;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
int length = (int)(entry >> 42) & 0x1fffff;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_dmaGet = entry & 0xfffffffffc;
|
|
|
|
_dmaPut = _dmaGet + length * 4;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_nonMain = (entry & (1L << 41)) != 0;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_gpu.ResourceManager.ClearPbCache();
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private void SetNonImmediateState(int word)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method = (word >> 0) & 0x1fff;
|
|
|
|
_state.SubChannel = (word >> 13) & 7;
|
|
|
|
_state.MethodCount = (word >> 16) & 0x1fff;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private void CallMethod(int argument)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_gpu.Fifo.CallMethod(_vmm, new GpuMethodCall(
|
|
|
|
_state.Method,
|
|
|
|
argument,
|
|
|
|
_state.SubChannel,
|
|
|
|
_state.MethodCount));
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|