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-05 01:52:39 +01:00
|
|
|
public Range(long Position, long Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +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-05 01:52:39 +01:00
|
|
|
long Position,
|
|
|
|
long Size,
|
|
|
|
long PhysicalAddress,
|
|
|
|
bool VaAllocated) : base(Position, Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
this.PhysicalAddress = PhysicalAddress;
|
|
|
|
this.VaAllocated = VaAllocated;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private SortedList<long, Range> Maps;
|
|
|
|
private SortedList<long, Range> Reservations;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public NvGpuASCtx(ServiceCtx Context)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Vmm = new NvGpuVmm(Context.Memory);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Maps = new SortedList<long, Range>();
|
|
|
|
Reservations = new SortedList<long, Range>();
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool ValidateFixedBuffer(long Position, long Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +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-05 01:52:39 +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-05 01:52:39 +01:00
|
|
|
if ((Position & NvGpuVmm.PageMask) != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if region is reserved.
|
2018-12-05 01:52:39 +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-05 01:52:39 +01:00
|
|
|
Range Map = BinarySearchLt(Maps, MapEnd);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +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-05 01:52:39 +01:00
|
|
|
long Position,
|
|
|
|
long Size,
|
|
|
|
long PhysicalAddress,
|
|
|
|
bool VaAllocated)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Maps.Add(Position, new MappedMemory(Position, Size, PhysicalAddress, VaAllocated));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool RemoveMap(long Position, out long Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Size = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Maps.Remove(Position, out Range Value))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
MappedMemory Map = (MappedMemory)Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Map.VaAllocated)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Size = (long)(Map.End - Map.Start);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool TryGetMapPhysicalAddress(long Position, out long PhysicalAddress)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Range Map = BinarySearch(Maps, Position);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Map != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
PhysicalAddress = ((MappedMemory)Map).PhysicalAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
PhysicalAddress = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void AddReservation(long Position, long Size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Reservations.Add(Position, new Range(Position, Size));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool RemoveReservation(long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Reservations.Remove(Position);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private Range BinarySearch(SortedList<long, Range> Lst, long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Left = 0;
|
|
|
|
int Right = Lst.Count - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
while (Left <= Right)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Size = Right - Left;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int Middle = Left + (Size >> 1);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Range Rg = Lst.Values[Middle];
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if ((ulong)Position >= Rg.Start && (ulong)Position < Rg.End)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Rg;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if ((ulong)Position < Rg.Start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Right = Middle - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Left = Middle + 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private Range BinarySearchLt(SortedList<long, Range> Lst, long Position)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Range LtRg = null;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int Left = 0;
|
|
|
|
int Right = Lst.Count - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
while (Left <= Right)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
int Size = Right - Left;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
int Middle = Left + (Size >> 1);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Range Rg = Lst.Values[Middle];
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if ((ulong)Position < Rg.Start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Right = Middle - 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Left = Middle + 1;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if ((ulong)Position > Rg.Start)
|
2018-08-23 06:54:59 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
LtRg = Rg;
|
2018-08-23 06:54:59 +02:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return LtRg;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|