memory_manager.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <array>
  6. #include <memory>
  7. #include "common/common_types.h"
  8. #include "core/memory.h"
  9. namespace Tegra {
  10. /// Virtual addresses in the GPU's memory map are 64 bit.
  11. using GPUVAddr = u64;
  12. class MemoryManager final {
  13. public:
  14. MemoryManager() = default;
  15. PAddr AllocateSpace(u64 size, u64 align);
  16. PAddr AllocateSpace(PAddr paddr, u64 size, u64 align);
  17. PAddr MapBufferEx(VAddr vaddr, u64 size);
  18. PAddr MapBufferEx(VAddr vaddr, PAddr paddr, u64 size);
  19. VAddr PhysicalToVirtualAddress(PAddr paddr);
  20. private:
  21. boost::optional<PAddr> FindFreeBlock(u64 size, u64 align = 1);
  22. bool IsPageMapped(PAddr paddr);
  23. VAddr& PageSlot(PAddr paddr);
  24. enum class PageStatus : u64 {
  25. Unmapped = 0xFFFFFFFFFFFFFFFFULL,
  26. Allocated = 0xFFFFFFFFFFFFFFFEULL,
  27. };
  28. static constexpr u64 MAX_ADDRESS{0x10000000000ULL};
  29. static constexpr u64 PAGE_TABLE_BITS{14};
  30. static constexpr u64 PAGE_TABLE_SIZE{1 << PAGE_TABLE_BITS};
  31. static constexpr u64 PAGE_TABLE_MASK{PAGE_TABLE_SIZE - 1};
  32. static constexpr u64 PAGE_BLOCK_BITS{14};
  33. static constexpr u64 PAGE_BLOCK_SIZE{1 << PAGE_BLOCK_BITS};
  34. static constexpr u64 PAGE_BLOCK_MASK{PAGE_BLOCK_SIZE - 1};
  35. using PageBlock = std::array<VAddr, PAGE_BLOCK_SIZE>;
  36. std::array<std::unique_ptr<PageBlock>, PAGE_TABLE_SIZE> page_table{};
  37. };
  38. } // namespace Tegra