2020-12-02 00:23:43 +01:00
|
|
|
using Ryujinx.Memory;
|
2021-01-17 19:44:34 +01:00
|
|
|
using Ryujinx.Memory.Range;
|
2020-04-22 08:00:11 +02:00
|
|
|
using System;
|
2021-01-17 19:44:34 +01:00
|
|
|
using System.Collections.Generic;
|
2020-07-12 05:07:01 +02:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
2020-04-22 08:00:11 +02:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
namespace Ryujinx.Graphics.Gpu.Memory
|
|
|
|
{
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU memory manager.
|
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
public class MemoryManager : IWritableBlock
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
private const int PtLvl0Bits = 14;
|
|
|
|
private const int PtLvl1Bits = 14;
|
2019-10-26 19:50:52 +02:00
|
|
|
public const int PtPageBits = 12;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
private const ulong PtLvl0Size = 1UL << PtLvl0Bits;
|
|
|
|
private const ulong PtLvl1Size = 1UL << PtLvl1Bits;
|
|
|
|
public const ulong PageSize = 1UL << PtPageBits;
|
|
|
|
|
|
|
|
private const ulong PtLvl0Mask = PtLvl0Size - 1;
|
|
|
|
private const ulong PtLvl1Mask = PtLvl1Size - 1;
|
|
|
|
public const ulong PageMask = PageSize - 1;
|
|
|
|
|
|
|
|
private const int PtLvl0Bit = PtPageBits + PtLvl1Bits;
|
|
|
|
private const int PtLvl1Bit = PtPageBits;
|
2021-01-17 19:44:34 +01:00
|
|
|
private const int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2022-02-22 17:34:16 +01:00
|
|
|
public const ulong PteUnmapped = ulong.MaxValue;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-04-22 08:00:11 +02:00
|
|
|
private readonly ulong[][] _pageTable;
|
|
|
|
|
|
|
|
public event EventHandler<UnmapEventArgs> MemoryUnmapped;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Physical memory where the virtual memory is mapped into.
|
|
|
|
/// </summary>
|
|
|
|
internal PhysicalMemory Physical { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Cache of GPU counters.
|
|
|
|
/// </summary>
|
|
|
|
internal CounterCache CounterCache { get; }
|
2020-07-12 05:07:01 +02:00
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU memory manager.
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
/// <param name="physicalMemory">Physical memory that this memory manager will map into</param>
|
|
|
|
internal MemoryManager(PhysicalMemory physicalMemory)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
Physical = physicalMemory;
|
|
|
|
CounterCache = new CounterCache();
|
2019-10-13 08:02:07 +02:00
|
|
|
_pageTable = new ulong[PtLvl0Size][];
|
2021-06-29 19:32:02 +02:00
|
|
|
MemoryUnmapped += Physical.TextureCache.MemoryUnmappedHandler;
|
|
|
|
MemoryUnmapped += Physical.BufferCache.MemoryUnmappedHandler;
|
|
|
|
MemoryUnmapped += CounterCache.MemoryUnmappedHandler;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Reads data from GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of the data</typeparam>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address where the data is located</param>
|
2021-08-29 21:52:38 +02:00
|
|
|
/// <param name="tracked">True if read tracking is triggered on the memory region</param>
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <returns>The data at the specified memory location</returns>
|
2021-08-29 21:52:38 +02:00
|
|
|
public T Read<T>(ulong va, bool tracked = false) where T : unmanaged
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2021-08-29 21:52:38 +02:00
|
|
|
int size = Unsafe.SizeOf<T>();
|
|
|
|
|
|
|
|
if (IsContiguous(va, size))
|
|
|
|
{
|
|
|
|
ulong address = Translate(va);
|
|
|
|
|
|
|
|
if (tracked)
|
|
|
|
{
|
|
|
|
return Physical.ReadTracked<T>(address);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Physical.Read<T>(address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Span<byte> data = new byte[size];
|
|
|
|
|
|
|
|
ReadImpl(va, data, tracked);
|
|
|
|
|
|
|
|
return MemoryMarshal.Cast<byte, T>(data)[0];
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a read-only span of data from GPU mapped memory.
|
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address where the data is located</param>
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <param name="size">Size of the data</param>
|
2021-01-17 21:08:06 +01:00
|
|
|
/// <param name="tracked">True if read tracking is triggered on the span</param>
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <returns>The span of the data at the specified memory location</returns>
|
2021-01-17 19:44:34 +01:00
|
|
|
public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
if (IsContiguous(va, size))
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
return Physical.GetSpan(Translate(va), size, tracked);
|
2021-01-17 19:44:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Span<byte> data = new byte[size];
|
2020-07-12 05:07:01 +02:00
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
ReadImpl(va, data, tracked);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Reads data from a possibly non-contiguous region of GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address of the data</param>
|
|
|
|
/// <param name="data">Span to write the read data into</param>
|
|
|
|
/// <param name="tracked">True to enable write tracking on read, false otherwise</param>
|
|
|
|
private void ReadImpl(ulong va, Span<byte> data, bool tracked)
|
|
|
|
{
|
|
|
|
if (data.Length == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = 0, size;
|
|
|
|
|
|
|
|
if ((va & PageMask) != 0)
|
|
|
|
{
|
|
|
|
ulong pa = Translate(va);
|
|
|
|
|
|
|
|
size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
Physical.GetSpan(pa, size, tracked).CopyTo(data.Slice(0, size));
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; offset < data.Length; offset += size)
|
|
|
|
{
|
|
|
|
ulong pa = Translate(va + (ulong)offset);
|
|
|
|
|
|
|
|
size = Math.Min(data.Length - offset, (int)PageSize);
|
|
|
|
|
2021-06-29 19:32:02 +02:00
|
|
|
Physical.GetSpan(pa, size, tracked).CopyTo(data.Slice(offset, size));
|
2021-01-17 19:44:34 +01:00
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a writable region from GPU mapped memory.
|
|
|
|
/// </summary>
|
2022-02-22 15:21:29 +01:00
|
|
|
/// <param name="va">Start address of the range</param>
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <param name="size">Size in bytes to be range</param>
|
2022-02-22 15:21:29 +01:00
|
|
|
/// <param name="tracked">True if write tracking is triggered on the span</param>
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <returns>A writable region with the data at the specified memory location</returns>
|
2022-02-22 15:21:29 +01:00
|
|
|
public WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
if (IsContiguous(va, size))
|
|
|
|
{
|
2022-02-22 15:21:29 +01:00
|
|
|
return Physical.GetWritableRegion(Translate(va), size, tracked);
|
2021-01-17 19:44:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Memory<byte> memory = new byte[size];
|
|
|
|
|
|
|
|
GetSpan(va, size).CopyTo(memory.Span);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
2022-02-22 15:21:29 +01:00
|
|
|
return new WritableRegion(this, va, memory, tracked);
|
2021-01-17 19:44:34 +01:00
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 08:39:45 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of the data</typeparam>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address to write the value into</param>
|
2020-07-25 08:39:45 +02:00
|
|
|
/// <param name="value">The value to be written</param>
|
2021-01-17 19:44:34 +01:00
|
|
|
public void Write<T>(ulong va, T value) where T : unmanaged
|
2020-07-25 08:39:45 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
|
2020-07-25 08:39:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to GPU mapped memory.
|
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address to write the data into</param>
|
|
|
|
/// <param name="data">The data to be written</param>
|
|
|
|
public void Write(ulong va, ReadOnlySpan<byte> data)
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
WriteImpl(va, data, Physical.Write);
|
2021-01-17 19:44:34 +01:00
|
|
|
}
|
|
|
|
|
Replace CacheResourceWrite with more general "precise" write (#2684)
* Replace CacheResourceWrite with more general "precise" write
The goal of CacheResourceWrite was to notify GPU resources when they were modified directly, by looking up the modified address/size in a structure and calling a method on each resource. The downside of this is that each resource cache has to be queried individually, they all have to implement their own way to do this, and it can only signal to resources using the same PhysicalMemory instance.
This PR adds the ability to signal a write as "precise" on the tracking, which signals a special handler (if present) which can be used to avoid unnecessary flush actions, or maybe even more. For buffers, precise writes specifically do not flush, and instead punch a hole in the modified range list to indicate that the data on GPU has been replaced.
The downside is that precise actions must ignore the page protection bits and always signal - as they need to notify the target resource to ignore the sequence number optimization.
I had to reintroduce the sequence number increment after I2M, as removing it was causing issues in rabbids kingdom battle. However - all resources modified by I2M are notified directly to lower their sequence number, so the problem is likely that another unrelated resource is not being properly updated. Thankfully, doing this does not affect performance in the games I tested.
This should fix regressions from #2624. Test any games that were broken by that. (RF4, rabbids kingdom battle)
I've also added a sequence number increment to ThreedClass.IncrementSyncpoint, as it seems to fix buffer corruption in OpenGL homebrew. (this was a regression from removing sequence number increment from constant buffer update - another unrelated resource thing)
* Add tests.
* Add XML docs for GpuRegionHandle
* Skip UpdateProtection if only precise actions were called
This allows precise actions to skip reprotection costs.
2021-09-29 02:27:03 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to GPU mapped memory, destined for a tracked resource.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address to write the data into</param>
|
|
|
|
/// <param name="data">The data to be written</param>
|
|
|
|
public void WriteTrackedResource(ulong va, ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
WriteImpl(va, data, Physical.WriteTrackedResource);
|
|
|
|
}
|
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to GPU mapped memory without write tracking.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address to write the data into</param>
|
2020-07-12 05:07:01 +02:00
|
|
|
/// <param name="data">The data to be written</param>
|
2021-01-17 19:44:34 +01:00
|
|
|
public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
WriteImpl(va, data, Physical.WriteUntracked);
|
2021-01-17 19:44:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private delegate void WriteCallback(ulong address, ReadOnlySpan<byte> data);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Writes data to possibly non-contiguous GPU mapped memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address of the region to write into</param>
|
|
|
|
/// <param name="data">Data to be written</param>
|
|
|
|
/// <param name="writeCallback">Write callback</param>
|
|
|
|
private void WriteImpl(ulong va, ReadOnlySpan<byte> data, WriteCallback writeCallback)
|
2020-07-12 05:07:01 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
if (IsContiguous(va, data.Length))
|
|
|
|
{
|
|
|
|
writeCallback(Translate(va), data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int offset = 0, size;
|
|
|
|
|
|
|
|
if ((va & PageMask) != 0)
|
|
|
|
{
|
|
|
|
ulong pa = Translate(va);
|
|
|
|
|
|
|
|
size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
|
|
|
|
|
|
|
|
writeCallback(pa, data.Slice(0, size));
|
2020-07-12 05:07:01 +02:00
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; offset < data.Length; offset += size)
|
|
|
|
{
|
|
|
|
ulong pa = Translate(va + (ulong)offset);
|
|
|
|
|
|
|
|
size = Math.Min(data.Length - offset, (int)PageSize);
|
|
|
|
|
|
|
|
writeCallback(pa, data.Slice(offset, size));
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 21:09:32 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Writes data to GPU mapped memory, stopping at the first unmapped page at the memory region, if any.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address to write the data into</param>
|
|
|
|
/// <param name="data">The data to be written</param>
|
|
|
|
public void WriteMapped(ulong va, ReadOnlySpan<byte> data)
|
|
|
|
{
|
|
|
|
if (IsContiguous(va, data.Length))
|
|
|
|
{
|
|
|
|
Physical.Write(Translate(va), data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int offset = 0, size;
|
|
|
|
|
|
|
|
if ((va & PageMask) != 0)
|
|
|
|
{
|
|
|
|
ulong pa = Translate(va);
|
|
|
|
|
|
|
|
size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
|
|
|
|
|
|
|
|
if (pa != PteUnmapped && Physical.IsMapped(pa))
|
|
|
|
{
|
|
|
|
Physical.Write(pa, data.Slice(0, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; offset < data.Length; offset += size)
|
|
|
|
{
|
|
|
|
ulong pa = Translate(va + (ulong)offset);
|
|
|
|
|
|
|
|
size = Math.Min(data.Length - offset, (int)PageSize);
|
|
|
|
|
|
|
|
if (pa != PteUnmapped && Physical.IsMapped(pa))
|
|
|
|
{
|
|
|
|
Physical.Write(pa, data.Slice(offset, size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Maps a given range of pages to the specified CPU virtual address.
|
|
|
|
/// </summary>
|
2020-01-01 16:39:09 +01:00
|
|
|
/// <remarks>
|
|
|
|
/// All addresses and sizes must be page aligned.
|
|
|
|
/// </remarks>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="pa">CPU virtual address to map into</param>
|
|
|
|
/// <param name="va">GPU virtual address to be mapped</param>
|
|
|
|
/// <param name="size">Size in bytes of the mapping</param>
|
2022-03-20 17:55:07 +01:00
|
|
|
/// <param name="kind">Kind of the resource located at the mapping</param>
|
|
|
|
public void Map(ulong pa, ulong va, ulong size, PteKind kind)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
lock (_pageTable)
|
|
|
|
{
|
Memory Read/Write Tracking using Region Handles (#1272)
* WIP Range Tracking
- Texture invalidation seems to have large problems
- Buffer/Pool invalidation may have problems
- Mirror memory tracking puts an additional `add` in compiled code, we likely just want to make HLE access slower if this is the final solution.
- Native project is in the messiest possible location.
- [HACK] JIT memory access always uses native "fast" path
- [HACK] Trying some things with texture invalidation and views.
It works :)
Still a few hacks, messy things, slow things
More work in progress stuff (also move to memory project)
Quite a bit faster now.
- Unmapping GPU VA and CPU VA will now correctly update write tracking regions, and invalidate textures for the former.
- The Virtual range list is now non-overlapping like the physical one.
- Fixed some bugs where regions could leak.
- Introduced a weird bug that I still need to track down (consistent invalid buffer in MK8 ribbon road)
Move some stuff.
I think we'll eventually just put the dll and so for this in a nuget package.
Fix rebase.
[WIP] MultiRegionHandle variable size ranges
- Avoid reprotecting regions that change often (needs some tweaking)
- There's still a bug in buffers, somehow.
- Might want different api for minimum granularity
Fix rebase issue
Commit everything needed for software only tracking.
Remove native components.
Remove more native stuff.
Cleanup
Use a separate window for the background context, update opentk. (fixes linux)
Some experimental changes
Should get things working up to scratch - still need to try some things with flush/modification and res scale.
Include address with the region action.
Initial work to make range tracking work
Still a ton of bugs
Fix some issues with the new stuff.
* Fix texture flush instability
There's still some weird behaviour, but it's much improved without this. (textures with cpu modified data were flushing over it)
* Find the destination texture for Buffer->Texture full copy
Greatly improves performance for nvdec videos (with range tracking)
* Further improve texture tracking
* Disable Memory Tracking for view parents
This is a temporary approach to better match behaviour on master (where invalidations would be soaked up by views, rather than trigger twice)
The assumption is that when views are created to a texture, they will cover all of its data anyways. Of course, this can easily be improved in future.
* Introduce some tracking tests.
WIP
* Complete base tests.
* Add more tests for multiregion, fix existing test.
* Cleanup Part 1
* Remove unnecessary code from memory tracking
* Fix some inconsistencies with 3D texture rule.
* Add dispose tests.
* Use a background thread for the background context.
Rather than setting and unsetting a context as current, doing the work on a dedicated thread with signals seems to be a bit faster.
Also nerf the multithreading test a bit.
* Copy to texture with matching alignment
This extends the copy to work for some videos with unusual size, such as tutorial videos in SMO. It will only occur if the destination texture already exists at XCount size.
* Track reads for buffer copies. Synchronize new buffers before copying overlaps.
* Remove old texture flushing mechanisms.
Range tracking all the way, baby.
* Wake the background thread when disposing.
Avoids a deadlock when games are closed.
* Address Feedback 1
* Separate TextureCopy instance for background thread
Also `BackgroundContextWorker.InBackground` for a more sensible idenfifier for if we're in a background thread.
* Add missing XML docs.
* Address Feedback
* Maybe I should start drinking coffee.
* Some more feedback.
* Remove flush warning, Refocus window after making background context
2020-10-16 22:18:35 +02:00
|
|
|
MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
for (ulong offset = 0; offset < size; offset += PageSize)
|
|
|
|
{
|
2022-03-20 17:55:07 +01:00
|
|
|
SetPte(va + offset, PackPte(pa + offset, kind));
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
2021-01-10 00:11:31 +01:00
|
|
|
/// Unmaps a given range of pages at the specified GPU virtual memory region.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
2021-01-10 00:11:31 +01:00
|
|
|
/// <param name="va">GPU virtual address to unmap</param>
|
|
|
|
/// <param name="size">Size in bytes of the region being unmapped</param>
|
|
|
|
public void Unmap(ulong va, ulong size)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
lock (_pageTable)
|
|
|
|
{
|
2020-05-04 04:24:59 +02:00
|
|
|
// Event handlers are not expected to be thread safe.
|
|
|
|
MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
for (ulong offset = 0; offset < size; offset += PageSize)
|
|
|
|
{
|
|
|
|
SetPte(va + offset, PteUnmapped);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if a region of GPU mapped memory is contiguous.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address of the region</param>
|
|
|
|
/// <param name="size">Size of the region</param>
|
|
|
|
/// <returns>True if the region is contiguous, false otherwise</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private bool IsContiguous(ulong va, int size)
|
|
|
|
{
|
|
|
|
if (!ValidateAddress(va) || GetPte(va) == PteUnmapped)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong endVa = (va + (ulong)size + PageMask) & ~PageMask;
|
|
|
|
|
|
|
|
va &= ~PageMask;
|
|
|
|
|
|
|
|
int pages = (int)((endVa - va) / PageSize);
|
|
|
|
|
|
|
|
for (int page = 0; page < pages - 1; page++)
|
|
|
|
{
|
|
|
|
if (!ValidateAddress(va + PageSize) || GetPte(va + PageSize) == PteUnmapped)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Translate(va) + PageSize != Translate(va + PageSize))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
va += PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the physical regions that make up the given virtual address region.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">Virtual address of the range</param>
|
|
|
|
/// <param name="size">Size of the range</param>
|
|
|
|
/// <returns>Multi-range with the physical regions</returns>
|
|
|
|
public MultiRange GetPhysicalRegions(ulong va, ulong size)
|
|
|
|
{
|
|
|
|
if (IsContiguous(va, (int)size))
|
|
|
|
{
|
|
|
|
return new MultiRange(Translate(va), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong regionStart = Translate(va);
|
|
|
|
ulong regionSize = Math.Min(size, PageSize - (va & PageMask));
|
|
|
|
|
|
|
|
ulong endVa = va + size;
|
|
|
|
ulong endVaRounded = (endVa + PageMask) & ~PageMask;
|
|
|
|
|
|
|
|
va &= ~PageMask;
|
|
|
|
|
|
|
|
int pages = (int)((endVaRounded - va) / PageSize);
|
|
|
|
|
|
|
|
var regions = new List<MemoryRange>();
|
|
|
|
|
|
|
|
for (int page = 0; page < pages - 1; page++)
|
|
|
|
{
|
2022-02-22 17:34:16 +01:00
|
|
|
ulong currPa = Translate(va);
|
2021-01-17 19:44:34 +01:00
|
|
|
ulong newPa = Translate(va + PageSize);
|
|
|
|
|
2022-02-22 17:34:16 +01:00
|
|
|
if ((currPa != PteUnmapped || newPa != PteUnmapped) && currPa + PageSize != newPa)
|
2021-01-17 19:44:34 +01:00
|
|
|
{
|
|
|
|
regions.Add(new MemoryRange(regionStart, regionSize));
|
|
|
|
regionStart = newPa;
|
|
|
|
regionSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
va += PageSize;
|
|
|
|
regionSize += Math.Min(endVa - va, PageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
regions.Add(new MemoryRange(regionStart, regionSize));
|
|
|
|
|
|
|
|
return new MultiRange(regions.ToArray());
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:38:00 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if a given GPU virtual memory range is mapped to the same physical regions
|
|
|
|
/// as the specified physical memory multi-range.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="range">Physical memory multi-range</param>
|
|
|
|
/// <param name="va">GPU virtual memory address</param>
|
|
|
|
/// <returns>True if the virtual memory region is mapped into the specified physical one, false otherwise</returns>
|
|
|
|
public bool CompareRange(MultiRange range, ulong va)
|
|
|
|
{
|
|
|
|
va &= ~PageMask;
|
|
|
|
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
|
|
|
{
|
|
|
|
MemoryRange currentRange = range.GetSubRange(i);
|
|
|
|
|
2022-02-22 17:34:16 +01:00
|
|
|
if (currentRange.Address != PteUnmapped)
|
2021-01-23 13:38:00 +01:00
|
|
|
{
|
2022-02-22 17:34:16 +01:00
|
|
|
ulong address = currentRange.Address & ~PageMask;
|
|
|
|
ulong endAddress = (currentRange.EndAddress + PageMask) & ~PageMask;
|
|
|
|
|
|
|
|
while (address < endAddress)
|
2021-01-23 13:38:00 +01:00
|
|
|
{
|
2022-02-22 17:34:16 +01:00
|
|
|
if (Translate(va) != address)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
va += PageSize;
|
|
|
|
address += PageSize;
|
2021-01-23 13:38:00 +01:00
|
|
|
}
|
2022-02-22 17:34:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ulong endVa = va + (((currentRange.Size) + PageMask) & ~PageMask);
|
2021-01-23 13:38:00 +01:00
|
|
|
|
2022-02-22 17:34:16 +01:00
|
|
|
while (va < endVa)
|
|
|
|
{
|
|
|
|
if (Translate(va) != PteUnmapped)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
va += PageSize;
|
|
|
|
}
|
2021-01-23 13:38:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Validates a GPU virtual address.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">Address to validate</param>
|
|
|
|
/// <returns>True if the address is valid, false otherwise</returns>
|
|
|
|
private static bool ValidateAddress(ulong va)
|
|
|
|
{
|
|
|
|
return va < (1UL << AddressSpaceBits);
|
|
|
|
}
|
|
|
|
|
2020-10-03 07:43:33 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if a given page is mapped.
|
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address of the page to check</param>
|
2020-10-03 07:43:33 +02:00
|
|
|
/// <returns>True if the page is mapped, false otherwise</returns>
|
2021-01-17 19:44:34 +01:00
|
|
|
public bool IsMapped(ulong va)
|
2020-10-03 07:43:33 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
return Translate(va) != PteUnmapped;
|
2020-10-03 07:43:33 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
2020-01-02 00:14:18 +01:00
|
|
|
/// Translates a GPU virtual address to a CPU virtual address.
|
2019-12-31 04:22:58 +01:00
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address to be translated</param>
|
|
|
|
/// <returns>CPU virtual address, or <see cref="PteUnmapped"/> if unmapped</returns>
|
|
|
|
public ulong Translate(ulong va)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
if (!ValidateAddress(va))
|
|
|
|
{
|
|
|
|
return PteUnmapped;
|
|
|
|
}
|
|
|
|
|
2022-03-20 17:55:07 +01:00
|
|
|
ulong pte = GetPte(va);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2022-03-20 17:55:07 +01:00
|
|
|
if (pte == PteUnmapped)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
return PteUnmapped;
|
|
|
|
}
|
|
|
|
|
2022-03-20 17:55:07 +01:00
|
|
|
return UnpackPaFromPte(pte) + (va & PageMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the kind of a given memory page.
|
|
|
|
/// This might indicate the type of resource that can be allocated on the page, and also texture tiling.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="va">GPU virtual address</param>
|
|
|
|
/// <returns>Kind of the memory page</returns>
|
|
|
|
public PteKind GetKind(ulong va)
|
|
|
|
{
|
|
|
|
if (!ValidateAddress(va))
|
|
|
|
{
|
|
|
|
return PteKind.Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong pte = GetPte(va);
|
|
|
|
|
|
|
|
if (pte == PteUnmapped)
|
|
|
|
{
|
|
|
|
return PteKind.Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnpackKindFromPte(pte);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the Page Table entry for a given GPU virtual address.
|
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <returns>Page table entry (CPU virtual address)</returns>
|
2021-01-17 19:44:34 +01:00
|
|
|
private ulong GetPte(ulong va)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
|
|
|
|
ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (_pageTable[l0] == null)
|
|
|
|
{
|
|
|
|
return PteUnmapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _pageTable[l0][l1];
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets a Page Table entry at a given GPU virtual address.
|
|
|
|
/// </summary>
|
2021-01-17 19:44:34 +01:00
|
|
|
/// <param name="va">GPU virtual address</param>
|
2019-12-31 04:22:58 +01:00
|
|
|
/// <param name="pte">Page table entry (CPU virtual address)</param>
|
2021-01-17 19:44:34 +01:00
|
|
|
private void SetPte(ulong va, ulong pte)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-01-17 19:44:34 +01:00
|
|
|
ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
|
|
|
|
ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (_pageTable[l0] == null)
|
|
|
|
{
|
|
|
|
_pageTable[l0] = new ulong[PtLvl1Size];
|
|
|
|
|
|
|
|
for (ulong index = 0; index < PtLvl1Size; index++)
|
|
|
|
{
|
|
|
|
_pageTable[l0][index] = PteUnmapped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 04:22:58 +01:00
|
|
|
_pageTable[l0][l1] = pte;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2022-03-20 17:55:07 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a page table entry from a physical address and kind.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pa">Physical address</param>
|
|
|
|
/// <param name="kind">Kind</param>
|
|
|
|
/// <returns>Page table entry</returns>
|
|
|
|
private static ulong PackPte(ulong pa, PteKind kind)
|
|
|
|
{
|
|
|
|
return pa | ((ulong)kind << 56);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unpacks kind from a page table entry.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pte">Page table entry</param>
|
|
|
|
/// <returns>Kind</returns>
|
|
|
|
private static PteKind UnpackKindFromPte(ulong pte)
|
|
|
|
{
|
|
|
|
return (PteKind)(pte >> 56);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unpacks physical address from a page table entry.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pte">Page table entry</param>
|
|
|
|
/// <returns>Physical address</returns>
|
|
|
|
private static ulong UnpackPaFromPte(ulong pte)
|
|
|
|
{
|
|
|
|
return pte & 0xffffffffffffffUL;
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|