memory_manager.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <map>
  6. #include <optional>
  7. #include "common/common_types.h"
  8. #include "common/page_table.h"
  9. namespace VideoCore {
  10. class RasterizerInterface;
  11. }
  12. namespace Tegra {
  13. /**
  14. * Represents a VMA in an address space. A VMA is a contiguous region of virtual addressing space
  15. * with homogeneous attributes across its extents. In this particular implementation each VMA is
  16. * also backed by a single host memory allocation.
  17. */
  18. struct VirtualMemoryArea {
  19. enum class Type : u8 {
  20. Unmapped,
  21. Allocated,
  22. Mapped,
  23. };
  24. /// Virtual base address of the region.
  25. GPUVAddr base{};
  26. /// Size of the region.
  27. u64 size{};
  28. /// Memory area mapping type.
  29. Type type{Type::Unmapped};
  30. /// CPU memory mapped address corresponding to this memory area.
  31. VAddr backing_addr{};
  32. /// Offset into the backing_memory the mapping starts from.
  33. std::size_t offset{};
  34. /// Pointer backing this VMA.
  35. u8* backing_memory{};
  36. /// Tests if this area can be merged to the right with `next`.
  37. bool CanBeMergedWith(const VirtualMemoryArea& next) const;
  38. };
  39. class MemoryManager final {
  40. public:
  41. MemoryManager(VideoCore::RasterizerInterface& rasterizer);
  42. GPUVAddr AllocateSpace(u64 size, u64 align);
  43. GPUVAddr AllocateSpace(GPUVAddr addr, u64 size, u64 align);
  44. GPUVAddr MapBufferEx(VAddr cpu_addr, u64 size);
  45. GPUVAddr MapBufferEx(VAddr cpu_addr, GPUVAddr addr, u64 size);
  46. GPUVAddr UnmapBuffer(GPUVAddr addr, u64 size);
  47. std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  48. template <typename T>
  49. T Read(GPUVAddr addr) const;
  50. template <typename T>
  51. void Write(GPUVAddr addr, T data);
  52. u8* GetPointer(GPUVAddr addr);
  53. const u8* GetPointer(GPUVAddr addr) const;
  54. void ReadBlock(GPUVAddr src_addr, void* dest_buffer, std::size_t size) const;
  55. void WriteBlock(GPUVAddr dest_addr, const void* src_buffer, std::size_t size);
  56. void ReadBlockUnsafe(GPUVAddr src_addr, void* dest_buffer, std::size_t size) const;
  57. void WriteBlockUnsafe(GPUVAddr dest_addr, const void* src_buffer, std::size_t size);
  58. void CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size);
  59. private:
  60. using VMAMap = std::map<GPUVAddr, VirtualMemoryArea>;
  61. using VMAHandle = VMAMap::const_iterator;
  62. using VMAIter = VMAMap::iterator;
  63. bool IsAddressValid(GPUVAddr addr) const;
  64. void MapPages(GPUVAddr base, u64 size, u8* memory, Common::PageType type,
  65. VAddr backing_addr = 0);
  66. void MapMemoryRegion(GPUVAddr base, u64 size, u8* target, VAddr backing_addr);
  67. void UnmapRegion(GPUVAddr base, u64 size);
  68. /// Finds the VMA in which the given address is included in, or `vma_map.end()`.
  69. VMAHandle FindVMA(GPUVAddr target) const;
  70. VMAHandle AllocateMemory(GPUVAddr target, std::size_t offset, u64 size);
  71. /**
  72. * Maps an unmanaged host memory pointer at a given address.
  73. *
  74. * @param target The guest address to start the mapping at.
  75. * @param memory The memory to be mapped.
  76. * @param size Size of the mapping.
  77. * @param state MemoryState tag to attach to the VMA.
  78. */
  79. VMAHandle MapBackingMemory(GPUVAddr target, u8* memory, u64 size, VAddr backing_addr);
  80. /// Unmaps a range of addresses, splitting VMAs as necessary.
  81. void UnmapRange(GPUVAddr target, u64 size);
  82. /// Converts a VMAHandle to a mutable VMAIter.
  83. VMAIter StripIterConstness(const VMAHandle& iter);
  84. /// Marks as the specfied VMA as allocated.
  85. VMAIter Allocate(VMAIter vma);
  86. /**
  87. * Carves a VMA of a specific size at the specified address by splitting Free VMAs while doing
  88. * the appropriate error checking.
  89. */
  90. VMAIter CarveVMA(GPUVAddr base, u64 size);
  91. /**
  92. * Splits the edges of the given range of non-Free VMAs so that there is a VMA split at each
  93. * end of the range.
  94. */
  95. VMAIter CarveVMARange(GPUVAddr base, u64 size);
  96. /**
  97. * Splits a VMA in two, at the specified offset.
  98. * @returns the right side of the split, with the original iterator becoming the left side.
  99. */
  100. VMAIter SplitVMA(VMAIter vma, u64 offset_in_vma);
  101. /**
  102. * Checks for and merges the specified VMA with adjacent ones if possible.
  103. * @returns the merged VMA or the original if no merging was possible.
  104. */
  105. VMAIter MergeAdjacent(VMAIter vma);
  106. /// Updates the pages corresponding to this VMA so they match the VMA's attributes.
  107. void UpdatePageTableForVMA(const VirtualMemoryArea& vma);
  108. /// Finds a free (unmapped region) of the specified size starting at the specified address.
  109. GPUVAddr FindFreeRegion(GPUVAddr region_start, u64 size) const;
  110. private:
  111. static constexpr u64 page_bits{16};
  112. static constexpr u64 page_size{1 << page_bits};
  113. static constexpr u64 page_mask{page_size - 1};
  114. /// Address space in bits, this is fairly arbitrary but sufficiently large.
  115. static constexpr u32 address_space_width{39};
  116. /// Start address for mapping, this is fairly arbitrary but must be non-zero.
  117. static constexpr GPUVAddr address_space_base{0x100000};
  118. /// End of address space, based on address space in bits.
  119. static constexpr GPUVAddr address_space_end{1ULL << address_space_width};
  120. Common::PageTable page_table{page_bits};
  121. VMAMap vma_map;
  122. VideoCore::RasterizerInterface& rasterizer;
  123. };
  124. } // namespace Tegra