2021-06-24 00:51:41 +01:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.Gpu;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel.Types;
|
2020-04-19 02:25:57 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostCtrl;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap;
|
2020-07-12 04:07:01 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nv.Types;
|
2020-12-01 23:23:43 +00:00
|
|
|
|
using Ryujinx.Memory;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
using System;
|
2021-06-29 18:32:02 +01:00
|
|
|
|
using System.Collections.Concurrent;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel
|
|
|
|
|
{
|
|
|
|
|
class NvHostChannelDeviceFile : NvDeviceFile
|
|
|
|
|
{
|
2022-02-09 20:18:07 +00:00
|
|
|
|
private static readonly ConcurrentDictionary<ulong, Host1xContext> _host1xContextRegistry = new();
|
2021-06-29 18:32:02 +01:00
|
|
|
|
|
2020-04-19 02:25:57 +01:00
|
|
|
|
private const uint MaxModuleSyncpoint = 16;
|
|
|
|
|
|
2020-01-06 01:04:37 +00:00
|
|
|
|
private uint _timeout;
|
|
|
|
|
private uint _submitTimeout;
|
|
|
|
|
private uint _timeslice;
|
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
|
private readonly Switch _device;
|
2020-01-06 01:04:37 +00:00
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
|
private readonly IVirtualMemoryManager _memory;
|
2021-06-29 18:32:02 +01:00
|
|
|
|
private readonly Host1xContext _host1xContext;
|
2021-09-28 23:43:40 +01:00
|
|
|
|
private readonly long _contextId;
|
2021-06-29 18:32:02 +01:00
|
|
|
|
|
|
|
|
|
public GpuChannel Channel { get; }
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
2020-04-19 02:25:57 +01:00
|
|
|
|
public enum ResourcePolicy
|
|
|
|
|
{
|
|
|
|
|
Device,
|
|
|
|
|
Channel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static uint[] DeviceSyncpoints = new uint[MaxModuleSyncpoint];
|
|
|
|
|
|
|
|
|
|
protected uint[] ChannelSyncpoints;
|
|
|
|
|
|
|
|
|
|
protected static ResourcePolicy ChannelResourcePolicy = ResourcePolicy.Device;
|
|
|
|
|
|
|
|
|
|
private NvFence _channelSyncpoint;
|
|
|
|
|
|
2022-02-09 20:18:07 +00:00
|
|
|
|
public NvHostChannelDeviceFile(ServiceCtx context, IVirtualMemoryManager memory, ulong owner) : base(context, owner)
|
2019-11-02 22:47:56 +00:00
|
|
|
|
{
|
2021-06-29 18:32:02 +01:00
|
|
|
|
_device = context.Device;
|
|
|
|
|
_memory = memory;
|
|
|
|
|
_timeout = 3000;
|
|
|
|
|
_submitTimeout = 0;
|
|
|
|
|
_timeslice = 0;
|
|
|
|
|
_host1xContext = GetHost1XContext(context.Device.Gpu, owner);
|
2021-09-28 23:43:40 +01:00
|
|
|
|
_contextId = _host1xContext.Host1x.CreateContext();
|
2021-06-29 18:32:02 +01:00
|
|
|
|
Channel = _device.Gpu.CreateChannel();
|
2020-04-19 02:25:57 +01:00
|
|
|
|
|
2021-07-11 21:20:40 +01:00
|
|
|
|
ChannelInitialization.InitializeState(Channel);
|
|
|
|
|
|
2020-04-19 02:25:57 +01:00
|
|
|
|
ChannelSyncpoints = new uint[MaxModuleSyncpoint];
|
|
|
|
|
|
|
|
|
|
_channelSyncpoint.Id = _device.System.HostSyncpoint.AllocateSyncpoint(false);
|
|
|
|
|
_channelSyncpoint.UpdateValue(_device.System.HostSyncpoint);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override NvInternalResult Ioctl(NvIoctl command, Span<byte> arguments)
|
|
|
|
|
{
|
|
|
|
|
NvInternalResult result = NvInternalResult.NotImplemented;
|
|
|
|
|
|
|
|
|
|
if (command.Type == NvIoctl.NvHostCustomMagic)
|
|
|
|
|
{
|
|
|
|
|
switch (command.Number)
|
|
|
|
|
{
|
|
|
|
|
case 0x01:
|
|
|
|
|
result = Submit(arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x02:
|
|
|
|
|
result = CallIoctlMethod<GetParameterArguments>(GetSyncpoint, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x03:
|
|
|
|
|
result = CallIoctlMethod<GetParameterArguments>(GetWaitBase, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x07:
|
|
|
|
|
result = CallIoctlMethod<uint>(SetSubmitTimeout, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x09:
|
|
|
|
|
result = MapCommandBuffer(arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x0a:
|
|
|
|
|
result = UnmapCommandBuffer(arguments);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (command.Type == NvIoctl.NvHostMagic)
|
|
|
|
|
{
|
|
|
|
|
switch (command.Number)
|
|
|
|
|
{
|
|
|
|
|
case 0x01:
|
|
|
|
|
result = CallIoctlMethod<int>(SetNvMapFd, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x03:
|
|
|
|
|
result = CallIoctlMethod<uint>(SetTimeout, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x08:
|
|
|
|
|
result = SubmitGpfifo(arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x09:
|
|
|
|
|
result = CallIoctlMethod<AllocObjCtxArguments>(AllocObjCtx, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x0b:
|
|
|
|
|
result = CallIoctlMethod<ZcullBindArguments>(ZcullBind, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x0c:
|
|
|
|
|
result = CallIoctlMethod<SetErrorNotifierArguments>(SetErrorNotifier, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x0d:
|
|
|
|
|
result = CallIoctlMethod<NvChannelPriority>(SetPriority, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x18:
|
|
|
|
|
result = CallIoctlMethod<AllocGpfifoExArguments>(AllocGpfifoEx, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x1a:
|
|
|
|
|
result = CallIoctlMethod<AllocGpfifoExArguments>(AllocGpfifoEx2, arguments);
|
|
|
|
|
break;
|
|
|
|
|
case 0x1d:
|
|
|
|
|
result = CallIoctlMethod<uint>(SetTimeslice, arguments);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (command.Type == NvIoctl.NvGpuMagic)
|
|
|
|
|
{
|
|
|
|
|
switch (command.Number)
|
|
|
|
|
{
|
|
|
|
|
case 0x14:
|
|
|
|
|
result = CallIoctlMethod<ulong>(SetUserData, arguments);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult Submit(Span<byte> arguments)
|
|
|
|
|
{
|
2021-10-24 22:34:44 +01:00
|
|
|
|
SubmitArguments submitHeader = GetSpanAndSkip<SubmitArguments>(ref arguments, 1)[0];
|
|
|
|
|
Span<CommandBuffer> commandBuffers = GetSpanAndSkip<CommandBuffer>(ref arguments, submitHeader.CmdBufsCount);
|
|
|
|
|
Span<Reloc> relocs = GetSpanAndSkip<Reloc>(ref arguments, submitHeader.RelocsCount);
|
|
|
|
|
Span<uint> relocShifts = GetSpanAndSkip<uint>(ref arguments, submitHeader.RelocsCount);
|
|
|
|
|
Span<SyncptIncr> syncptIncrs = GetSpanAndSkip<SyncptIncr>(ref arguments, submitHeader.SyncptIncrsCount);
|
|
|
|
|
Span<uint> fenceThresholds = GetSpanAndSkip<uint>(ref arguments, submitHeader.FencesCount);
|
2020-07-12 04:07:01 +01:00
|
|
|
|
|
|
|
|
|
lock (_device)
|
2019-11-02 22:47:56 +00:00
|
|
|
|
{
|
2020-07-12 04:07:01 +01:00
|
|
|
|
for (int i = 0; i < syncptIncrs.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
SyncptIncr syncptIncr = syncptIncrs[i];
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
2020-07-12 04:07:01 +01:00
|
|
|
|
uint id = syncptIncr.Id;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
2021-10-24 22:34:44 +01:00
|
|
|
|
fenceThresholds[i] = Context.Device.System.HostSyncpoint.IncrementSyncpointMax(id, syncptIncr.Incrs);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 04:07:01 +01:00
|
|
|
|
foreach (CommandBuffer commandBuffer in commandBuffers)
|
|
|
|
|
{
|
|
|
|
|
NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(Owner, commandBuffer.Mem);
|
|
|
|
|
|
2021-01-01 22:03:33 +00:00
|
|
|
|
var data = _memory.GetSpan(map.Address + commandBuffer.Offset, commandBuffer.WordsCount * 4);
|
2020-07-12 04:07:01 +01:00
|
|
|
|
|
2021-09-28 23:43:40 +01:00
|
|
|
|
_host1xContext.Host1x.Submit(MemoryMarshal.Cast<byte, int>(data), _contextId);
|
2020-07-12 04:07:01 +01:00
|
|
|
|
}
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 04:07:01 +01:00
|
|
|
|
private Span<T> GetSpanAndSkip<T>(ref Span<byte> arguments, int count) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
Span<T> output = MemoryMarshal.Cast<byte, T>(arguments).Slice(0, count);
|
|
|
|
|
|
|
|
|
|
arguments = arguments.Slice(Unsafe.SizeOf<T>() * count);
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 22:47:56 +00:00
|
|
|
|
private NvInternalResult GetSyncpoint(ref GetParameterArguments arguments)
|
|
|
|
|
{
|
2020-04-19 02:25:57 +01:00
|
|
|
|
if (arguments.Parameter >= MaxModuleSyncpoint)
|
|
|
|
|
{
|
|
|
|
|
return NvInternalResult.InvalidInput;
|
|
|
|
|
}
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
2020-04-19 02:25:57 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult GetWaitBase(ref GetParameterArguments arguments)
|
|
|
|
|
{
|
|
|
|
|
arguments.Value = 0;
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetSubmitTimeout(ref uint submitTimeout)
|
|
|
|
|
{
|
|
|
|
|
_submitTimeout = submitTimeout;
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult MapCommandBuffer(Span<byte> arguments)
|
|
|
|
|
{
|
|
|
|
|
int headerSize = Unsafe.SizeOf<MapCommandBufferArguments>();
|
|
|
|
|
MapCommandBufferArguments commandBufferHeader = MemoryMarshal.Cast<byte, MapCommandBufferArguments>(arguments)[0];
|
|
|
|
|
Span<CommandBufferHandle> commandBufferEntries = MemoryMarshal.Cast<byte, CommandBufferHandle>(arguments.Slice(headerSize)).Slice(0, commandBufferHeader.NumEntries);
|
|
|
|
|
|
|
|
|
|
foreach (ref CommandBufferHandle commandBufferEntry in commandBufferEntries)
|
|
|
|
|
{
|
|
|
|
|
NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(Owner, commandBufferEntry.MapHandle);
|
|
|
|
|
|
|
|
|
|
if (map == null)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{commandBufferEntry.MapHandle:x8}!");
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.InvalidInput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (map)
|
|
|
|
|
{
|
|
|
|
|
if (map.DmaMapAddress == 0)
|
|
|
|
|
{
|
2021-06-29 18:32:02 +01:00
|
|
|
|
ulong va = _host1xContext.MemoryAllocator.GetFreeAddress((ulong)map.Size, out ulong freeAddressStartPosition, 1, MemoryManager.PageSize);
|
2020-12-09 22:26:05 +00:00
|
|
|
|
|
|
|
|
|
if (va != NvMemoryAllocator.PteUnmapped && va <= uint.MaxValue && (va + (uint)map.Size) <= uint.MaxValue)
|
|
|
|
|
{
|
2021-06-29 18:32:02 +01:00
|
|
|
|
_host1xContext.MemoryAllocator.AllocateRange(va, (uint)map.Size, freeAddressStartPosition);
|
|
|
|
|
_host1xContext.Smmu.Map(map.Address, va, (uint)map.Size);
|
2021-01-01 22:03:33 +00:00
|
|
|
|
map.DmaMapAddress = va;
|
2020-12-09 22:26:05 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-01-01 22:03:33 +00:00
|
|
|
|
map.DmaMapAddress = NvMemoryAllocator.PteUnmapped;
|
2020-12-09 22:26:05 +00:00
|
|
|
|
}
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commandBufferEntry.MapAddress = (int)map.DmaMapAddress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult UnmapCommandBuffer(Span<byte> arguments)
|
|
|
|
|
{
|
|
|
|
|
int headerSize = Unsafe.SizeOf<MapCommandBufferArguments>();
|
|
|
|
|
MapCommandBufferArguments commandBufferHeader = MemoryMarshal.Cast<byte, MapCommandBufferArguments>(arguments)[0];
|
|
|
|
|
Span<CommandBufferHandle> commandBufferEntries = MemoryMarshal.Cast<byte, CommandBufferHandle>(arguments.Slice(headerSize)).Slice(0, commandBufferHeader.NumEntries);
|
|
|
|
|
|
|
|
|
|
foreach (ref CommandBufferHandle commandBufferEntry in commandBufferEntries)
|
|
|
|
|
{
|
|
|
|
|
NvMapHandle map = NvMapDeviceFile.GetMapFromHandle(Owner, commandBufferEntry.MapHandle);
|
|
|
|
|
|
|
|
|
|
if (map == null)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{commandBufferEntry.MapHandle:x8}!");
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.InvalidInput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (map)
|
|
|
|
|
{
|
|
|
|
|
if (map.DmaMapAddress != 0)
|
|
|
|
|
{
|
2020-07-12 04:07:01 +01:00
|
|
|
|
// FIXME:
|
|
|
|
|
// To make unmapping work, we need separate address space per channel.
|
|
|
|
|
// Right now NVDEC and VIC share the GPU address space which is not correct at all.
|
|
|
|
|
|
2021-06-29 18:32:02 +01:00
|
|
|
|
// _host1xContext.MemoryAllocator.Free((ulong)map.DmaMapAddress, (uint)map.Size);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
2020-07-12 04:07:01 +01:00
|
|
|
|
// map.DmaMapAddress = 0;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetNvMapFd(ref int nvMapFd)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetTimeout(ref uint timeout)
|
|
|
|
|
{
|
|
|
|
|
_timeout = timeout;
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SubmitGpfifo(Span<byte> arguments)
|
|
|
|
|
{
|
|
|
|
|
int headerSize = Unsafe.SizeOf<SubmitGpfifoArguments>();
|
|
|
|
|
SubmitGpfifoArguments gpfifoSubmissionHeader = MemoryMarshal.Cast<byte, SubmitGpfifoArguments>(arguments)[0];
|
2019-10-13 07:02:07 +01:00
|
|
|
|
Span<ulong> gpfifoEntries = MemoryMarshal.Cast<byte, ulong>(arguments.Slice(headerSize)).Slice(0, gpfifoSubmissionHeader.NumEntries);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return SubmitGpfifo(ref gpfifoSubmissionHeader, gpfifoEntries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult AllocObjCtx(ref AllocObjCtxArguments arguments)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult ZcullBind(ref ZcullBindArguments arguments)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetErrorNotifier(ref SetErrorNotifierArguments arguments)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetPriority(ref NvChannelPriority priority)
|
|
|
|
|
{
|
|
|
|
|
switch (priority)
|
|
|
|
|
{
|
|
|
|
|
case NvChannelPriority.Low:
|
|
|
|
|
_timeslice = 1300; // Timeslice low priority in micro-seconds
|
|
|
|
|
break;
|
|
|
|
|
case NvChannelPriority.Medium:
|
|
|
|
|
_timeslice = 2600; // Timeslice medium priority in micro-seconds
|
|
|
|
|
break;
|
|
|
|
|
case NvChannelPriority.High:
|
|
|
|
|
_timeslice = 5200; // Timeslice high priority in micro-seconds
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return NvInternalResult.InvalidInput;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
// TODO: disable and preempt channel when GPU scheduler will be implemented.
|
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult AllocGpfifoEx(ref AllocGpfifoExArguments arguments)
|
|
|
|
|
{
|
2020-04-19 02:25:57 +01:00
|
|
|
|
_channelSyncpoint.UpdateValue(_device.System.HostSyncpoint);
|
|
|
|
|
|
|
|
|
|
arguments.Fence = _channelSyncpoint;
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult AllocGpfifoEx2(ref AllocGpfifoExArguments arguments)
|
|
|
|
|
{
|
2020-04-19 02:25:57 +01:00
|
|
|
|
_channelSyncpoint.UpdateValue(_device.System.HostSyncpoint);
|
|
|
|
|
|
|
|
|
|
arguments.Fence = _channelSyncpoint;
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetTimeslice(ref uint timeslice)
|
|
|
|
|
{
|
|
|
|
|
if (timeslice < 1000 || timeslice > 50000)
|
|
|
|
|
{
|
|
|
|
|
return NvInternalResult.InvalidInput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timeslice = timeslice; // in micro-seconds
|
|
|
|
|
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
// TODO: disable and preempt channel when GPU scheduler will be implemented.
|
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NvInternalResult SetUserData(ref ulong userData)
|
|
|
|
|
{
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNv);
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
|
protected NvInternalResult SubmitGpfifo(ref SubmitGpfifoArguments header, Span<ulong> entries)
|
2019-11-02 22:47:56 +00:00
|
|
|
|
{
|
2020-04-19 02:25:57 +01:00
|
|
|
|
if (header.Flags.HasFlag(SubmitGpfifoFlags.FenceWait) && header.Flags.HasFlag(SubmitGpfifoFlags.IncrementWithValue))
|
2019-11-02 22:47:56 +00:00
|
|
|
|
{
|
2020-04-19 02:25:57 +01:00
|
|
|
|
return NvInternalResult.InvalidInput;
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:25:57 +01:00
|
|
|
|
if (header.Flags.HasFlag(SubmitGpfifoFlags.FenceWait) && !_device.System.HostSyncpoint.IsSyncpointExpired(header.Fence.Id, header.Fence.Value))
|
|
|
|
|
{
|
2021-06-29 18:32:02 +01:00
|
|
|
|
Channel.PushHostCommandBuffer(CreateWaitCommandBuffer(header.Fence));
|
2020-04-19 02:25:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 18:32:02 +01:00
|
|
|
|
Channel.PushEntries(entries);
|
2020-04-19 02:25:57 +01:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2021-06-29 18:32:02 +01:00
|
|
|
|
Channel.PushHostCommandBuffer(CreateIncrementCommandBuffer(ref header.Fence, header.Flags));
|
2020-04-19 02:25:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header.Flags = SubmitGpfifoFlags.None;
|
|
|
|
|
|
2020-07-24 03:53:25 +01:00
|
|
|
|
_device.Gpu.GPFifo.SignalNewEntries();
|
2019-11-02 22:47:56 +00:00
|
|
|
|
|
|
|
|
|
return NvInternalResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:25:57 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 00:51:41 +01:00
|
|
|
|
public override void Close()
|
|
|
|
|
{
|
2021-09-28 23:43:40 +01:00
|
|
|
|
_host1xContext.Host1x.DestroyContext(_contextId);
|
2021-06-29 18:32:02 +01:00
|
|
|
|
Channel.Dispose();
|
2021-11-04 23:27:21 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MaxModuleSyncpoint; i++)
|
|
|
|
|
{
|
|
|
|
|
if (ChannelSyncpoints[i] != 0)
|
|
|
|
|
{
|
|
|
|
|
_device.System.HostSyncpoint.ReleaseSyncpoint(ChannelSyncpoints[i]);
|
|
|
|
|
ChannelSyncpoints[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_device.System.HostSyncpoint.ReleaseSyncpoint(_channelSyncpoint.Id);
|
|
|
|
|
_channelSyncpoint.Id = 0;
|
2021-06-29 18:32:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 20:18:07 +00:00
|
|
|
|
private static Host1xContext GetHost1XContext(GpuContext gpu, ulong pid)
|
2021-06-29 18:32:02 +01:00
|
|
|
|
{
|
2022-02-09 20:18:07 +00:00
|
|
|
|
return _host1xContextRegistry.GetOrAdd(pid, (ulong key) => new Host1xContext(gpu, key));
|
2021-06-29 18:32:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Destroy()
|
|
|
|
|
{
|
|
|
|
|
foreach (Host1xContext host1xContext in _host1xContextRegistry.Values)
|
|
|
|
|
{
|
|
|
|
|
host1xContext.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_host1xContextRegistry.Clear();
|
2021-06-24 00:51:41 +01:00
|
|
|
|
}
|
2019-11-02 22:47:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|