2018-09-08 19:51:50 +02:00
|
|
|
using Ryujinx.Graphics.Memory;
|
2018-08-15 20:59:51 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Nv.NvGpuAS
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
class NvGpuASCtx
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
public NvGpuVmm Vmm { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
private class Range
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
public ulong Start { get; private set; }
|
|
|
|
public ulong End { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public Range(long position, long size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Start = (ulong)position;
|
|
|
|
End = (ulong)size + Start;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class MappedMemory : Range
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
public long PhysicalAddress { get; private set; }
|
|
|
|
public bool VaAllocated { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
public MappedMemory(
|
2018-12-06 12:16:24 +01:00
|
|
|
long position,
|
|
|
|
long size,
|
|
|
|
long physicalAddress,
|
|
|
|
bool vaAllocated) : base(position, size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
PhysicalAddress = physicalAddress;
|
|
|
|
VaAllocated = vaAllocated;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private SortedList<long, Range> _maps;
|
|
|
|
private SortedList<long, Range> _reservations;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public NvGpuASCtx(ServiceCtx context)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Vmm = new NvGpuVmm(context.Memory);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_maps = new SortedList<long, Range>();
|
|
|
|
_reservations = new SortedList<long, Range>();
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool ValidateFixedBuffer(long position, long size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long mapEnd = position + size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
//Check if size is valid (0 is also not allowed).
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)mapEnd <= (ulong)position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if address is page aligned.
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((position & NvGpuVmm.PageMask) != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if region is reserved.
|
2018-12-06 12:16:24 +01:00
|
|
|
if (BinarySearch(_reservations, position) == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check for overlap with already mapped buffers.
|
2018-12-06 12:16:24 +01:00
|
|
|
Range map = BinarySearchLt(_maps, mapEnd);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (map != null && map.End > (ulong)position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddMap(
|
2018-12-06 12:16:24 +01:00
|
|
|
long position,
|
|
|
|
long size,
|
|
|
|
long physicalAddress,
|
|
|
|
bool vaAllocated)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_maps.Add(position, new MappedMemory(position, size, physicalAddress, vaAllocated));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool RemoveMap(long position, out long size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
size = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_maps.Remove(position, out Range value))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
MappedMemory map = (MappedMemory)value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (map.VaAllocated)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
size = (long)(map.End - map.Start);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool TryGetMapPhysicalAddress(long position, out long physicalAddress)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Range map = BinarySearch(_maps, position);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (map != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
physicalAddress = ((MappedMemory)map).PhysicalAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
physicalAddress = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void AddReservation(long position, long size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_reservations.Add(position, new Range(position, size));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool RemoveReservation(long position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _reservations.Remove(position);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private Range BinarySearch(SortedList<long, Range> lst, long position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int left = 0;
|
|
|
|
int right = lst.Count - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (left <= right)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int size = right - left;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int middle = left + (size >> 1);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
Range rg = lst.Values[middle];
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)position >= rg.Start && (ulong)position < rg.End)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return rg;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)position < rg.Start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
right = middle - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
left = middle + 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private Range BinarySearchLt(SortedList<long, Range> lst, long position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Range ltRg = null;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int left = 0;
|
|
|
|
int right = lst.Count - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (left <= right)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int size = right - left;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int middle = left + (size >> 1);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
Range rg = lst.Values[middle];
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)position < rg.Start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
right = middle - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
left = middle + 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)position > rg.Start)
|
2018-08-23 06:54:59 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ltRg = rg;
|
2018-08-23 06:54:59 +02:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return ltRg;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|