memory_manager.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. GPUVAddr AllocateSpace(u64 size, u64 align);
  16. GPUVAddr AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align);
  17. GPUVAddr MapBufferEx(VAddr cpu_addr, u64 size);
  18. GPUVAddr MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size);
  19. VAddr GpuToCpuAddress(GPUVAddr gpu_addr);
  20. static constexpr u64 PAGE_BITS = 16;
  21. static constexpr u64 PAGE_SIZE = 1 << PAGE_BITS;
  22. static constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
  23. private:
  24. boost::optional<GPUVAddr> FindFreeBlock(u64 size, u64 align = 1);
  25. bool IsPageMapped(GPUVAddr gpu_addr);
  26. VAddr& PageSlot(GPUVAddr gpu_addr);
  27. enum class PageStatus : u64 {
  28. Unmapped = 0xFFFFFFFFFFFFFFFFULL,
  29. Allocated = 0xFFFFFFFFFFFFFFFEULL,
  30. };
  31. static constexpr u64 MAX_ADDRESS{0x10000000000ULL};
  32. static constexpr u64 PAGE_TABLE_BITS{10};
  33. static constexpr u64 PAGE_TABLE_SIZE{1 << PAGE_TABLE_BITS};
  34. static constexpr u64 PAGE_TABLE_MASK{PAGE_TABLE_SIZE - 1};
  35. static constexpr u64 PAGE_BLOCK_BITS{14};
  36. static constexpr u64 PAGE_BLOCK_SIZE{1 << PAGE_BLOCK_BITS};
  37. static constexpr u64 PAGE_BLOCK_MASK{PAGE_BLOCK_SIZE - 1};
  38. using PageBlock = std::array<VAddr, PAGE_BLOCK_SIZE>;
  39. std::array<std::unique_ptr<PageBlock>, PAGE_TABLE_SIZE> page_table{};
  40. };
  41. } // namespace Tegra