| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- using Ryujinx.Memory;
- using Ryujinx.Memory.Range;
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- namespace Ryujinx.Graphics.Gpu.Memory
- {
- /// <summary>
- /// GPU memory manager.
- /// </summary>
- public class MemoryManager : IWritableBlock
- {
- private const int PtLvl0Bits = 14;
- private const int PtLvl1Bits = 14;
- public const int PtPageBits = 12;
- 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;
- private const int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits;
- public const ulong PteUnmapped = ulong.MaxValue;
- private readonly ulong[][] _pageTable;
- public event EventHandler<UnmapEventArgs> MemoryUnmapped;
- /// <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; }
- /// <summary>
- /// Creates a new instance of the GPU memory manager.
- /// </summary>
- /// <param name="physicalMemory">Physical memory that this memory manager will map into</param>
- internal MemoryManager(PhysicalMemory physicalMemory)
- {
- Physical = physicalMemory;
- CounterCache = new CounterCache();
- _pageTable = new ulong[PtLvl0Size][];
- MemoryUnmapped += Physical.TextureCache.MemoryUnmappedHandler;
- MemoryUnmapped += Physical.BufferCache.MemoryUnmappedHandler;
- MemoryUnmapped += CounterCache.MemoryUnmappedHandler;
- }
- /// <summary>
- /// Reads data from GPU mapped memory.
- /// </summary>
- /// <typeparam name="T">Type of the data</typeparam>
- /// <param name="va">GPU virtual address where the data is located</param>
- /// <param name="tracked">True if read tracking is triggered on the memory region</param>
- /// <returns>The data at the specified memory location</returns>
- public T Read<T>(ulong va, bool tracked = false) where T : unmanaged
- {
- 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];
- }
- }
- /// <summary>
- /// Gets a read-only span of data from GPU mapped memory.
- /// </summary>
- /// <param name="va">GPU virtual address where the data is located</param>
- /// <param name="size">Size of the data</param>
- /// <param name="tracked">True if read tracking is triggered on the span</param>
- /// <returns>The span of the data at the specified memory location</returns>
- public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
- {
- if (IsContiguous(va, size))
- {
- return Physical.GetSpan(Translate(va), size, tracked);
- }
- else
- {
- Span<byte> data = new byte[size];
- ReadImpl(va, data, tracked);
- return data;
- }
- }
- /// <summary>
- /// Gets a read-only span of data from GPU mapped memory, up to the entire range specified,
- /// or the last mapped page if the range is not fully mapped.
- /// </summary>
- /// <param name="va">GPU virtual address where the data is located</param>
- /// <param name="size">Size of the data</param>
- /// <param name="tracked">True if read tracking is triggered on the span</param>
- /// <returns>The span of the data at the specified memory location</returns>
- public ReadOnlySpan<byte> GetSpanMapped(ulong va, int size, bool tracked = false)
- {
- bool isContiguous = true;
- int mappedSize;
- if (ValidateAddress(va) && GetPte(va) != PteUnmapped && Physical.IsMapped(Translate(va)))
- {
- ulong endVa = va + (ulong)size;
- ulong endVaAligned = (endVa + PageMask) & ~PageMask;
- ulong currentVa = va & ~PageMask;
- int pages = (int)((endVaAligned - currentVa) / PageSize);
- for (int page = 0; page < pages - 1; page++)
- {
- ulong nextVa = currentVa + PageSize;
- ulong nextPa = Translate(nextVa);
- if (!ValidateAddress(nextVa) || GetPte(nextVa) == PteUnmapped || !Physical.IsMapped(nextPa))
- {
- break;
- }
- if (Translate(currentVa) + PageSize != nextPa)
- {
- isContiguous = false;
- }
- currentVa += PageSize;
- }
- currentVa += PageSize;
- if (currentVa > endVa)
- {
- currentVa = endVa;
- }
- mappedSize = (int)(currentVa - va);
- }
- else
- {
- return ReadOnlySpan<byte>.Empty;
- }
- if (isContiguous)
- {
- return Physical.GetSpan(Translate(va), mappedSize, tracked);
- }
- else
- {
- Span<byte> data = new byte[mappedSize];
- 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));
- Physical.GetSpan(pa, size, tracked).CopyTo(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);
- Physical.GetSpan(pa, size, tracked).CopyTo(data.Slice(offset, size));
- }
- }
- /// <summary>
- /// Gets a writable region from GPU mapped memory.
- /// </summary>
- /// <param name="va">Start address of the range</param>
- /// <param name="size">Size in bytes to be range</param>
- /// <param name="tracked">True if write tracking is triggered on the span</param>
- /// <returns>A writable region with the data at the specified memory location</returns>
- public WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
- {
- if (IsContiguous(va, size))
- {
- return Physical.GetWritableRegion(Translate(va), size, tracked);
- }
- else
- {
- Memory<byte> memory = new byte[size];
- GetSpan(va, size).CopyTo(memory.Span);
- return new WritableRegion(this, va, memory, tracked);
- }
- }
- /// <summary>
- /// Writes data to GPU mapped memory.
- /// </summary>
- /// <typeparam name="T">Type of the data</typeparam>
- /// <param name="va">GPU virtual address to write the value into</param>
- /// <param name="value">The value to be written</param>
- public void Write<T>(ulong va, T value) where T : unmanaged
- {
- Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
- }
- /// <summary>
- /// Writes data to GPU mapped memory.
- /// </summary>
- /// <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)
- {
- WriteImpl(va, data, Physical.Write);
- }
- /// <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);
- }
- /// <summary>
- /// Writes data to GPU mapped memory without write tracking.
- /// </summary>
- /// <param name="va">GPU virtual address to write the data into</param>
- /// <param name="data">The data to be written</param>
- public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
- {
- WriteImpl(va, data, Physical.WriteUntracked);
- }
- 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)
- {
- 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));
- 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));
- }
- }
- }
- /// <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));
- }
- }
- }
- }
- /// <summary>
- /// Maps a given range of pages to the specified CPU virtual address.
- /// </summary>
- /// <remarks>
- /// All addresses and sizes must be page aligned.
- /// </remarks>
- /// <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>
- /// <param name="kind">Kind of the resource located at the mapping</param>
- public void Map(ulong pa, ulong va, ulong size, PteKind kind)
- {
- lock (_pageTable)
- {
- MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
- for (ulong offset = 0; offset < size; offset += PageSize)
- {
- SetPte(va + offset, PackPte(pa + offset, kind));
- }
- }
- }
- /// <summary>
- /// Unmaps a given range of pages at the specified GPU virtual memory region.
- /// </summary>
- /// <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)
- {
- lock (_pageTable)
- {
- // Event handlers are not expected to be thread safe.
- MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
- for (ulong offset = 0; offset < size; offset += PageSize)
- {
- SetPte(va + offset, PteUnmapped);
- }
- }
- }
- /// <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++)
- {
- ulong currPa = Translate(va);
- ulong newPa = Translate(va + PageSize);
- if ((currPa != PteUnmapped || newPa != PteUnmapped) && currPa + PageSize != newPa)
- {
- 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());
- }
- /// <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);
- if (currentRange.Address != PteUnmapped)
- {
- ulong address = currentRange.Address & ~PageMask;
- ulong endAddress = (currentRange.EndAddress + PageMask) & ~PageMask;
- while (address < endAddress)
- {
- if (Translate(va) != address)
- {
- return false;
- }
- va += PageSize;
- address += PageSize;
- }
- }
- else
- {
- ulong endVa = va + (((currentRange.Size) + PageMask) & ~PageMask);
- while (va < endVa)
- {
- if (Translate(va) != PteUnmapped)
- {
- return false;
- }
- va += PageSize;
- }
- }
- }
- return true;
- }
- /// <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);
- }
- /// <summary>
- /// Checks if a given page is mapped.
- /// </summary>
- /// <param name="va">GPU virtual address of the page to check</param>
- /// <returns>True if the page is mapped, false otherwise</returns>
- public bool IsMapped(ulong va)
- {
- return Translate(va) != PteUnmapped;
- }
- /// <summary>
- /// Translates a GPU virtual address to a CPU virtual address.
- /// </summary>
- /// <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)
- {
- if (!ValidateAddress(va))
- {
- return PteUnmapped;
- }
- ulong pte = GetPte(va);
- if (pte == PteUnmapped)
- {
- return PteUnmapped;
- }
- 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);
- }
- /// <summary>
- /// Gets the Page Table entry for a given GPU virtual address.
- /// </summary>
- /// <param name="va">GPU virtual address</param>
- /// <returns>Page table entry (CPU virtual address)</returns>
- private ulong GetPte(ulong va)
- {
- ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
- ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
- if (_pageTable[l0] == null)
- {
- return PteUnmapped;
- }
- return _pageTable[l0][l1];
- }
- /// <summary>
- /// Sets a Page Table entry at a given GPU virtual address.
- /// </summary>
- /// <param name="va">GPU virtual address</param>
- /// <param name="pte">Page table entry (CPU virtual address)</param>
- private void SetPte(ulong va, ulong pte)
- {
- ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
- ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
- if (_pageTable[l0] == null)
- {
- _pageTable[l0] = new ulong[PtLvl1Size];
- for (ulong index = 0; index < PtLvl1Size; index++)
- {
- _pageTable[l0][index] = PteUnmapped;
- }
- }
- _pageTable[l0][l1] = pte;
- }
- /// <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;
- }
- }
- }
|