nvmap.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <memory>
  6. #include <unordered_map>
  7. #include <vector>
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. #include "common/swap.h"
  11. #include "core/hle/service/nvdrv/devices/nvdevice.h"
  12. namespace Service::Nvidia::Devices {
  13. class nvmap final : public nvdevice {
  14. public:
  15. explicit nvmap(Core::System& system);
  16. ~nvmap() override;
  17. NvResult Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  18. std::vector<u8>& output) override;
  19. NvResult Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  20. const std::vector<u8>& inline_input, std::vector<u8>& output) override;
  21. NvResult Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  22. std::vector<u8>& output, std::vector<u8>& inline_output) override;
  23. void OnOpen(DeviceFD fd) override;
  24. void OnClose(DeviceFD fd) override;
  25. /// Returns the allocated address of an nvmap object given its handle.
  26. VAddr GetObjectAddress(u32 handle) const;
  27. /// Represents an nvmap object.
  28. struct Object {
  29. enum class Status { Created, Allocated };
  30. u32 id;
  31. u32 size;
  32. u32 flags;
  33. u32 align;
  34. u8 kind;
  35. VAddr addr;
  36. Status status;
  37. u32 refcount;
  38. u32 dma_map_addr;
  39. };
  40. std::shared_ptr<Object> GetObject(u32 handle) const {
  41. auto itr = handles.find(handle);
  42. if (itr != handles.end()) {
  43. return itr->second;
  44. }
  45. return {};
  46. }
  47. private:
  48. /// Id to use for the next handle that is created.
  49. u32 next_handle = 0;
  50. /// Id to use for the next object that is created.
  51. u32 next_id = 0;
  52. /// Mapping of currently allocated handles to the objects they represent.
  53. std::unordered_map<u32, std::shared_ptr<Object>> handles;
  54. struct IocCreateParams {
  55. // Input
  56. u32_le size{};
  57. // Output
  58. u32_le handle{};
  59. };
  60. static_assert(sizeof(IocCreateParams) == 8, "IocCreateParams has wrong size");
  61. struct IocFromIdParams {
  62. // Input
  63. u32_le id{};
  64. // Output
  65. u32_le handle{};
  66. };
  67. static_assert(sizeof(IocFromIdParams) == 8, "IocFromIdParams has wrong size");
  68. struct IocAllocParams {
  69. // Input
  70. u32_le handle{};
  71. u32_le heap_mask{};
  72. u32_le flags{};
  73. u32_le align{};
  74. u8 kind{};
  75. INSERT_PADDING_BYTES(7);
  76. u64_le addr{};
  77. };
  78. static_assert(sizeof(IocAllocParams) == 32, "IocAllocParams has wrong size");
  79. struct IocFreeParams {
  80. u32_le handle{};
  81. INSERT_PADDING_BYTES(4);
  82. u64_le address{};
  83. u32_le size{};
  84. u32_le flags{};
  85. };
  86. static_assert(sizeof(IocFreeParams) == 24, "IocFreeParams has wrong size");
  87. struct IocParamParams {
  88. // Input
  89. u32_le handle{};
  90. u32_le param{};
  91. // Output
  92. u32_le result{};
  93. };
  94. static_assert(sizeof(IocParamParams) == 12, "IocParamParams has wrong size");
  95. struct IocGetIdParams {
  96. // Output
  97. u32_le id{};
  98. // Input
  99. u32_le handle{};
  100. };
  101. static_assert(sizeof(IocGetIdParams) == 8, "IocGetIdParams has wrong size");
  102. u32 CreateObject(u32 size);
  103. NvResult IocCreate(const std::vector<u8>& input, std::vector<u8>& output);
  104. NvResult IocAlloc(const std::vector<u8>& input, std::vector<u8>& output);
  105. NvResult IocGetId(const std::vector<u8>& input, std::vector<u8>& output);
  106. NvResult IocFromId(const std::vector<u8>& input, std::vector<u8>& output);
  107. NvResult IocParam(const std::vector<u8>& input, std::vector<u8>& output);
  108. NvResult IocFree(const std::vector<u8>& input, std::vector<u8>& output);
  109. };
  110. } // namespace Service::Nvidia::Devices