MemoryManager.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. using Ryujinx.Memory;
  2. using Ryujinx.Memory.Range;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Gpu.Memory
  8. {
  9. /// <summary>
  10. /// GPU memory manager.
  11. /// </summary>
  12. public class MemoryManager : IWritableBlock
  13. {
  14. private const int PtLvl0Bits = 14;
  15. private const int PtLvl1Bits = 14;
  16. public const int PtPageBits = 12;
  17. private const ulong PtLvl0Size = 1UL << PtLvl0Bits;
  18. private const ulong PtLvl1Size = 1UL << PtLvl1Bits;
  19. public const ulong PageSize = 1UL << PtPageBits;
  20. private const ulong PtLvl0Mask = PtLvl0Size - 1;
  21. private const ulong PtLvl1Mask = PtLvl1Size - 1;
  22. public const ulong PageMask = PageSize - 1;
  23. private const int PtLvl0Bit = PtPageBits + PtLvl1Bits;
  24. private const int PtLvl1Bit = PtPageBits;
  25. private const int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits;
  26. public const ulong PteUnmapped = ulong.MaxValue;
  27. private readonly ulong[][] _pageTable;
  28. public event EventHandler<UnmapEventArgs> MemoryUnmapped;
  29. /// <summary>
  30. /// Physical memory where the virtual memory is mapped into.
  31. /// </summary>
  32. internal PhysicalMemory Physical { get; }
  33. /// <summary>
  34. /// Cache of GPU counters.
  35. /// </summary>
  36. internal CounterCache CounterCache { get; }
  37. /// <summary>
  38. /// Creates a new instance of the GPU memory manager.
  39. /// </summary>
  40. /// <param name="physicalMemory">Physical memory that this memory manager will map into</param>
  41. internal MemoryManager(PhysicalMemory physicalMemory)
  42. {
  43. Physical = physicalMemory;
  44. CounterCache = new CounterCache();
  45. _pageTable = new ulong[PtLvl0Size][];
  46. MemoryUnmapped += Physical.TextureCache.MemoryUnmappedHandler;
  47. MemoryUnmapped += Physical.BufferCache.MemoryUnmappedHandler;
  48. MemoryUnmapped += CounterCache.MemoryUnmappedHandler;
  49. }
  50. /// <summary>
  51. /// Reads data from GPU mapped memory.
  52. /// </summary>
  53. /// <typeparam name="T">Type of the data</typeparam>
  54. /// <param name="va">GPU virtual address where the data is located</param>
  55. /// <param name="tracked">True if read tracking is triggered on the memory region</param>
  56. /// <returns>The data at the specified memory location</returns>
  57. public T Read<T>(ulong va, bool tracked = false) where T : unmanaged
  58. {
  59. int size = Unsafe.SizeOf<T>();
  60. if (IsContiguous(va, size))
  61. {
  62. ulong address = Translate(va);
  63. if (tracked)
  64. {
  65. return Physical.ReadTracked<T>(address);
  66. }
  67. else
  68. {
  69. return Physical.Read<T>(address);
  70. }
  71. }
  72. else
  73. {
  74. Span<byte> data = new byte[size];
  75. ReadImpl(va, data, tracked);
  76. return MemoryMarshal.Cast<byte, T>(data)[0];
  77. }
  78. }
  79. /// <summary>
  80. /// Gets a read-only span of data from GPU mapped memory.
  81. /// </summary>
  82. /// <param name="va">GPU virtual address where the data is located</param>
  83. /// <param name="size">Size of the data</param>
  84. /// <param name="tracked">True if read tracking is triggered on the span</param>
  85. /// <returns>The span of the data at the specified memory location</returns>
  86. public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
  87. {
  88. if (IsContiguous(va, size))
  89. {
  90. return Physical.GetSpan(Translate(va), size, tracked);
  91. }
  92. else
  93. {
  94. Span<byte> data = new byte[size];
  95. ReadImpl(va, data, tracked);
  96. return data;
  97. }
  98. }
  99. /// <summary>
  100. /// Gets a read-only span of data from GPU mapped memory, up to the entire range specified,
  101. /// or the last mapped page if the range is not fully mapped.
  102. /// </summary>
  103. /// <param name="va">GPU virtual address where the data is located</param>
  104. /// <param name="size">Size of the data</param>
  105. /// <param name="tracked">True if read tracking is triggered on the span</param>
  106. /// <returns>The span of the data at the specified memory location</returns>
  107. public ReadOnlySpan<byte> GetSpanMapped(ulong va, int size, bool tracked = false)
  108. {
  109. bool isContiguous = true;
  110. int mappedSize;
  111. if (ValidateAddress(va) && GetPte(va) != PteUnmapped && Physical.IsMapped(Translate(va)))
  112. {
  113. ulong endVa = va + (ulong)size;
  114. ulong endVaAligned = (endVa + PageMask) & ~PageMask;
  115. ulong currentVa = va & ~PageMask;
  116. int pages = (int)((endVaAligned - currentVa) / PageSize);
  117. for (int page = 0; page < pages - 1; page++)
  118. {
  119. ulong nextVa = currentVa + PageSize;
  120. ulong nextPa = Translate(nextVa);
  121. if (!ValidateAddress(nextVa) || GetPte(nextVa) == PteUnmapped || !Physical.IsMapped(nextPa))
  122. {
  123. break;
  124. }
  125. if (Translate(currentVa) + PageSize != nextPa)
  126. {
  127. isContiguous = false;
  128. }
  129. currentVa += PageSize;
  130. }
  131. currentVa += PageSize;
  132. if (currentVa > endVa)
  133. {
  134. currentVa = endVa;
  135. }
  136. mappedSize = (int)(currentVa - va);
  137. }
  138. else
  139. {
  140. return ReadOnlySpan<byte>.Empty;
  141. }
  142. if (isContiguous)
  143. {
  144. return Physical.GetSpan(Translate(va), mappedSize, tracked);
  145. }
  146. else
  147. {
  148. Span<byte> data = new byte[mappedSize];
  149. ReadImpl(va, data, tracked);
  150. return data;
  151. }
  152. }
  153. /// <summary>
  154. /// Reads data from a possibly non-contiguous region of GPU mapped memory.
  155. /// </summary>
  156. /// <param name="va">GPU virtual address of the data</param>
  157. /// <param name="data">Span to write the read data into</param>
  158. /// <param name="tracked">True to enable write tracking on read, false otherwise</param>
  159. private void ReadImpl(ulong va, Span<byte> data, bool tracked)
  160. {
  161. if (data.Length == 0)
  162. {
  163. return;
  164. }
  165. int offset = 0, size;
  166. if ((va & PageMask) != 0)
  167. {
  168. ulong pa = Translate(va);
  169. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  170. Physical.GetSpan(pa, size, tracked).CopyTo(data.Slice(0, size));
  171. offset += size;
  172. }
  173. for (; offset < data.Length; offset += size)
  174. {
  175. ulong pa = Translate(va + (ulong)offset);
  176. size = Math.Min(data.Length - offset, (int)PageSize);
  177. Physical.GetSpan(pa, size, tracked).CopyTo(data.Slice(offset, size));
  178. }
  179. }
  180. /// <summary>
  181. /// Gets a writable region from GPU mapped memory.
  182. /// </summary>
  183. /// <param name="va">Start address of the range</param>
  184. /// <param name="size">Size in bytes to be range</param>
  185. /// <param name="tracked">True if write tracking is triggered on the span</param>
  186. /// <returns>A writable region with the data at the specified memory location</returns>
  187. public WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
  188. {
  189. if (IsContiguous(va, size))
  190. {
  191. return Physical.GetWritableRegion(Translate(va), size, tracked);
  192. }
  193. else
  194. {
  195. Memory<byte> memory = new byte[size];
  196. GetSpan(va, size).CopyTo(memory.Span);
  197. return new WritableRegion(this, va, memory, tracked);
  198. }
  199. }
  200. /// <summary>
  201. /// Writes data to GPU mapped memory.
  202. /// </summary>
  203. /// <typeparam name="T">Type of the data</typeparam>
  204. /// <param name="va">GPU virtual address to write the value into</param>
  205. /// <param name="value">The value to be written</param>
  206. public void Write<T>(ulong va, T value) where T : unmanaged
  207. {
  208. Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  209. }
  210. /// <summary>
  211. /// Writes data to GPU mapped memory.
  212. /// </summary>
  213. /// <param name="va">GPU virtual address to write the data into</param>
  214. /// <param name="data">The data to be written</param>
  215. public void Write(ulong va, ReadOnlySpan<byte> data)
  216. {
  217. WriteImpl(va, data, Physical.Write);
  218. }
  219. /// <summary>
  220. /// Writes data to GPU mapped memory, destined for a tracked resource.
  221. /// </summary>
  222. /// <param name="va">GPU virtual address to write the data into</param>
  223. /// <param name="data">The data to be written</param>
  224. public void WriteTrackedResource(ulong va, ReadOnlySpan<byte> data)
  225. {
  226. WriteImpl(va, data, Physical.WriteTrackedResource);
  227. }
  228. /// <summary>
  229. /// Writes data to GPU mapped memory without write tracking.
  230. /// </summary>
  231. /// <param name="va">GPU virtual address to write the data into</param>
  232. /// <param name="data">The data to be written</param>
  233. public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
  234. {
  235. WriteImpl(va, data, Physical.WriteUntracked);
  236. }
  237. private delegate void WriteCallback(ulong address, ReadOnlySpan<byte> data);
  238. /// <summary>
  239. /// Writes data to possibly non-contiguous GPU mapped memory.
  240. /// </summary>
  241. /// <param name="va">GPU virtual address of the region to write into</param>
  242. /// <param name="data">Data to be written</param>
  243. /// <param name="writeCallback">Write callback</param>
  244. private void WriteImpl(ulong va, ReadOnlySpan<byte> data, WriteCallback writeCallback)
  245. {
  246. if (IsContiguous(va, data.Length))
  247. {
  248. writeCallback(Translate(va), data);
  249. }
  250. else
  251. {
  252. int offset = 0, size;
  253. if ((va & PageMask) != 0)
  254. {
  255. ulong pa = Translate(va);
  256. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  257. writeCallback(pa, data.Slice(0, size));
  258. offset += size;
  259. }
  260. for (; offset < data.Length; offset += size)
  261. {
  262. ulong pa = Translate(va + (ulong)offset);
  263. size = Math.Min(data.Length - offset, (int)PageSize);
  264. writeCallback(pa, data.Slice(offset, size));
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// Writes data to GPU mapped memory, stopping at the first unmapped page at the memory region, if any.
  270. /// </summary>
  271. /// <param name="va">GPU virtual address to write the data into</param>
  272. /// <param name="data">The data to be written</param>
  273. public void WriteMapped(ulong va, ReadOnlySpan<byte> data)
  274. {
  275. if (IsContiguous(va, data.Length))
  276. {
  277. Physical.Write(Translate(va), data);
  278. }
  279. else
  280. {
  281. int offset = 0, size;
  282. if ((va & PageMask) != 0)
  283. {
  284. ulong pa = Translate(va);
  285. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  286. if (pa != PteUnmapped && Physical.IsMapped(pa))
  287. {
  288. Physical.Write(pa, data.Slice(0, size));
  289. }
  290. offset += size;
  291. }
  292. for (; offset < data.Length; offset += size)
  293. {
  294. ulong pa = Translate(va + (ulong)offset);
  295. size = Math.Min(data.Length - offset, (int)PageSize);
  296. if (pa != PteUnmapped && Physical.IsMapped(pa))
  297. {
  298. Physical.Write(pa, data.Slice(offset, size));
  299. }
  300. }
  301. }
  302. }
  303. /// <summary>
  304. /// Maps a given range of pages to the specified CPU virtual address.
  305. /// </summary>
  306. /// <remarks>
  307. /// All addresses and sizes must be page aligned.
  308. /// </remarks>
  309. /// <param name="pa">CPU virtual address to map into</param>
  310. /// <param name="va">GPU virtual address to be mapped</param>
  311. /// <param name="size">Size in bytes of the mapping</param>
  312. /// <param name="kind">Kind of the resource located at the mapping</param>
  313. public void Map(ulong pa, ulong va, ulong size, PteKind kind)
  314. {
  315. lock (_pageTable)
  316. {
  317. MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
  318. for (ulong offset = 0; offset < size; offset += PageSize)
  319. {
  320. SetPte(va + offset, PackPte(pa + offset, kind));
  321. }
  322. }
  323. }
  324. /// <summary>
  325. /// Unmaps a given range of pages at the specified GPU virtual memory region.
  326. /// </summary>
  327. /// <param name="va">GPU virtual address to unmap</param>
  328. /// <param name="size">Size in bytes of the region being unmapped</param>
  329. public void Unmap(ulong va, ulong size)
  330. {
  331. lock (_pageTable)
  332. {
  333. // Event handlers are not expected to be thread safe.
  334. MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
  335. for (ulong offset = 0; offset < size; offset += PageSize)
  336. {
  337. SetPte(va + offset, PteUnmapped);
  338. }
  339. }
  340. }
  341. /// <summary>
  342. /// Checks if a region of GPU mapped memory is contiguous.
  343. /// </summary>
  344. /// <param name="va">GPU virtual address of the region</param>
  345. /// <param name="size">Size of the region</param>
  346. /// <returns>True if the region is contiguous, false otherwise</returns>
  347. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  348. private bool IsContiguous(ulong va, int size)
  349. {
  350. if (!ValidateAddress(va) || GetPte(va) == PteUnmapped)
  351. {
  352. return false;
  353. }
  354. ulong endVa = (va + (ulong)size + PageMask) & ~PageMask;
  355. va &= ~PageMask;
  356. int pages = (int)((endVa - va) / PageSize);
  357. for (int page = 0; page < pages - 1; page++)
  358. {
  359. if (!ValidateAddress(va + PageSize) || GetPte(va + PageSize) == PteUnmapped)
  360. {
  361. return false;
  362. }
  363. if (Translate(va) + PageSize != Translate(va + PageSize))
  364. {
  365. return false;
  366. }
  367. va += PageSize;
  368. }
  369. return true;
  370. }
  371. /// <summary>
  372. /// Gets the physical regions that make up the given virtual address region.
  373. /// </summary>
  374. /// <param name="va">Virtual address of the range</param>
  375. /// <param name="size">Size of the range</param>
  376. /// <returns>Multi-range with the physical regions</returns>
  377. public MultiRange GetPhysicalRegions(ulong va, ulong size)
  378. {
  379. if (IsContiguous(va, (int)size))
  380. {
  381. return new MultiRange(Translate(va), size);
  382. }
  383. ulong regionStart = Translate(va);
  384. ulong regionSize = Math.Min(size, PageSize - (va & PageMask));
  385. ulong endVa = va + size;
  386. ulong endVaRounded = (endVa + PageMask) & ~PageMask;
  387. va &= ~PageMask;
  388. int pages = (int)((endVaRounded - va) / PageSize);
  389. var regions = new List<MemoryRange>();
  390. for (int page = 0; page < pages - 1; page++)
  391. {
  392. ulong currPa = Translate(va);
  393. ulong newPa = Translate(va + PageSize);
  394. if ((currPa != PteUnmapped || newPa != PteUnmapped) && currPa + PageSize != newPa)
  395. {
  396. regions.Add(new MemoryRange(regionStart, regionSize));
  397. regionStart = newPa;
  398. regionSize = 0;
  399. }
  400. va += PageSize;
  401. regionSize += Math.Min(endVa - va, PageSize);
  402. }
  403. regions.Add(new MemoryRange(regionStart, regionSize));
  404. return new MultiRange(regions.ToArray());
  405. }
  406. /// <summary>
  407. /// Checks if a given GPU virtual memory range is mapped to the same physical regions
  408. /// as the specified physical memory multi-range.
  409. /// </summary>
  410. /// <param name="range">Physical memory multi-range</param>
  411. /// <param name="va">GPU virtual memory address</param>
  412. /// <returns>True if the virtual memory region is mapped into the specified physical one, false otherwise</returns>
  413. public bool CompareRange(MultiRange range, ulong va)
  414. {
  415. va &= ~PageMask;
  416. for (int i = 0; i < range.Count; i++)
  417. {
  418. MemoryRange currentRange = range.GetSubRange(i);
  419. if (currentRange.Address != PteUnmapped)
  420. {
  421. ulong address = currentRange.Address & ~PageMask;
  422. ulong endAddress = (currentRange.EndAddress + PageMask) & ~PageMask;
  423. while (address < endAddress)
  424. {
  425. if (Translate(va) != address)
  426. {
  427. return false;
  428. }
  429. va += PageSize;
  430. address += PageSize;
  431. }
  432. }
  433. else
  434. {
  435. ulong endVa = va + (((currentRange.Size) + PageMask) & ~PageMask);
  436. while (va < endVa)
  437. {
  438. if (Translate(va) != PteUnmapped)
  439. {
  440. return false;
  441. }
  442. va += PageSize;
  443. }
  444. }
  445. }
  446. return true;
  447. }
  448. /// <summary>
  449. /// Validates a GPU virtual address.
  450. /// </summary>
  451. /// <param name="va">Address to validate</param>
  452. /// <returns>True if the address is valid, false otherwise</returns>
  453. private static bool ValidateAddress(ulong va)
  454. {
  455. return va < (1UL << AddressSpaceBits);
  456. }
  457. /// <summary>
  458. /// Checks if a given page is mapped.
  459. /// </summary>
  460. /// <param name="va">GPU virtual address of the page to check</param>
  461. /// <returns>True if the page is mapped, false otherwise</returns>
  462. public bool IsMapped(ulong va)
  463. {
  464. return Translate(va) != PteUnmapped;
  465. }
  466. /// <summary>
  467. /// Translates a GPU virtual address to a CPU virtual address.
  468. /// </summary>
  469. /// <param name="va">GPU virtual address to be translated</param>
  470. /// <returns>CPU virtual address, or <see cref="PteUnmapped"/> if unmapped</returns>
  471. public ulong Translate(ulong va)
  472. {
  473. if (!ValidateAddress(va))
  474. {
  475. return PteUnmapped;
  476. }
  477. ulong pte = GetPte(va);
  478. if (pte == PteUnmapped)
  479. {
  480. return PteUnmapped;
  481. }
  482. return UnpackPaFromPte(pte) + (va & PageMask);
  483. }
  484. /// <summary>
  485. /// Gets the kind of a given memory page.
  486. /// This might indicate the type of resource that can be allocated on the page, and also texture tiling.
  487. /// </summary>
  488. /// <param name="va">GPU virtual address</param>
  489. /// <returns>Kind of the memory page</returns>
  490. public PteKind GetKind(ulong va)
  491. {
  492. if (!ValidateAddress(va))
  493. {
  494. return PteKind.Invalid;
  495. }
  496. ulong pte = GetPte(va);
  497. if (pte == PteUnmapped)
  498. {
  499. return PteKind.Invalid;
  500. }
  501. return UnpackKindFromPte(pte);
  502. }
  503. /// <summary>
  504. /// Gets the Page Table entry for a given GPU virtual address.
  505. /// </summary>
  506. /// <param name="va">GPU virtual address</param>
  507. /// <returns>Page table entry (CPU virtual address)</returns>
  508. private ulong GetPte(ulong va)
  509. {
  510. ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  511. ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  512. if (_pageTable[l0] == null)
  513. {
  514. return PteUnmapped;
  515. }
  516. return _pageTable[l0][l1];
  517. }
  518. /// <summary>
  519. /// Sets a Page Table entry at a given GPU virtual address.
  520. /// </summary>
  521. /// <param name="va">GPU virtual address</param>
  522. /// <param name="pte">Page table entry (CPU virtual address)</param>
  523. private void SetPte(ulong va, ulong pte)
  524. {
  525. ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  526. ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  527. if (_pageTable[l0] == null)
  528. {
  529. _pageTable[l0] = new ulong[PtLvl1Size];
  530. for (ulong index = 0; index < PtLvl1Size; index++)
  531. {
  532. _pageTable[l0][index] = PteUnmapped;
  533. }
  534. }
  535. _pageTable[l0][l1] = pte;
  536. }
  537. /// <summary>
  538. /// Creates a page table entry from a physical address and kind.
  539. /// </summary>
  540. /// <param name="pa">Physical address</param>
  541. /// <param name="kind">Kind</param>
  542. /// <returns>Page table entry</returns>
  543. private static ulong PackPte(ulong pa, PteKind kind)
  544. {
  545. return pa | ((ulong)kind << 56);
  546. }
  547. /// <summary>
  548. /// Unpacks kind from a page table entry.
  549. /// </summary>
  550. /// <param name="pte">Page table entry</param>
  551. /// <returns>Kind</returns>
  552. private static PteKind UnpackKindFromPte(ulong pte)
  553. {
  554. return (PteKind)(pte >> 56);
  555. }
  556. /// <summary>
  557. /// Unpacks physical address from a page table entry.
  558. /// </summary>
  559. /// <param name="pte">Page table entry</param>
  560. /// <returns>Physical address</returns>
  561. private static ulong UnpackPaFromPte(ulong pte)
  562. {
  563. return pte & 0xffffffffffffffUL;
  564. }
  565. }
  566. }