interface.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include "common/logging/log.h"
  6. #include "core/core.h"
  7. #include "core/hle/ipc_helpers.h"
  8. #include "core/hle/kernel/kernel.h"
  9. #include "core/hle/kernel/readable_event.h"
  10. #include "core/hle/kernel/thread.h"
  11. #include "core/hle/kernel/writable_event.h"
  12. #include "core/hle/service/nvdrv/interface.h"
  13. #include "core/hle/service/nvdrv/nvdata.h"
  14. #include "core/hle/service/nvdrv/nvdrv.h"
  15. namespace Service::Nvidia {
  16. void NVDRV::SignalGPUInterruptSyncpt(const u32 syncpoint_id, const u32 value) {
  17. nvdrv->SignalSyncpt(syncpoint_id, value);
  18. }
  19. void NVDRV::Open(Kernel::HLERequestContext& ctx) {
  20. LOG_DEBUG(Service_NVDRV, "called");
  21. const auto& buffer = ctx.ReadBuffer();
  22. std::string device_name(buffer.begin(), buffer.end());
  23. u32 fd = nvdrv->Open(device_name);
  24. IPC::ResponseBuilder rb{ctx, 4};
  25. rb.Push(RESULT_SUCCESS);
  26. rb.Push<u32>(fd);
  27. rb.Push<u32>(0);
  28. }
  29. void NVDRV::IoctlBase(Kernel::HLERequestContext& ctx, IoctlVersion version) {
  30. IPC::RequestParser rp{ctx};
  31. u32 fd = rp.Pop<u32>();
  32. u32 command = rp.Pop<u32>();
  33. /// Ioctl 3 has 2 outputs, first in the input params, second is the result
  34. std::vector<u8> output(ctx.GetWriteBufferSize(0));
  35. std::vector<u8> output2;
  36. if (version == IoctlVersion::Version3) {
  37. output2.resize((ctx.GetWriteBufferSize(1)));
  38. }
  39. /// Ioctl2 has 2 inputs. It's used to pass data directly instead of providing a pointer.
  40. /// KickOfPB uses this
  41. auto input = ctx.ReadBuffer(0);
  42. std::vector<u8> input2;
  43. if (version == IoctlVersion::Version2) {
  44. input2 = ctx.ReadBuffer(1);
  45. }
  46. IoctlCtrl ctrl{};
  47. u32 result = nvdrv->Ioctl(fd, command, input, input2, output, output2, ctrl, version);
  48. if (ctrl.must_delay) {
  49. ctrl.fresh_call = false;
  50. ctx.SleepClientThread(
  51. "NVServices::DelayedResponse", ctrl.timeout,
  52. [=, this](std::shared_ptr<Kernel::Thread> thread, Kernel::HLERequestContext& ctx_,
  53. Kernel::ThreadWakeupReason reason) {
  54. IoctlCtrl ctrl2{ctrl};
  55. std::vector<u8> tmp_output = output;
  56. std::vector<u8> tmp_output2 = output2;
  57. const u32 ioctl_result = nvdrv->Ioctl(fd, command, input, input2, tmp_output,
  58. tmp_output2, ctrl2, version);
  59. ctx_.WriteBuffer(tmp_output, 0);
  60. if (version == IoctlVersion::Version3) {
  61. ctx_.WriteBuffer(tmp_output2, 1);
  62. }
  63. IPC::ResponseBuilder rb{ctx_, 3};
  64. rb.Push(RESULT_SUCCESS);
  65. rb.Push(ioctl_result);
  66. },
  67. nvdrv->GetEventWriteable(ctrl.event_id));
  68. } else {
  69. ctx.WriteBuffer(output);
  70. if (version == IoctlVersion::Version3) {
  71. ctx.WriteBuffer(output2, 1);
  72. }
  73. }
  74. IPC::ResponseBuilder rb{ctx, 3};
  75. rb.Push(RESULT_SUCCESS);
  76. rb.Push(result);
  77. }
  78. void NVDRV::Ioctl(Kernel::HLERequestContext& ctx) {
  79. LOG_DEBUG(Service_NVDRV, "called");
  80. IoctlBase(ctx, IoctlVersion::Version1);
  81. }
  82. void NVDRV::Ioctl2(Kernel::HLERequestContext& ctx) {
  83. LOG_DEBUG(Service_NVDRV, "called");
  84. IoctlBase(ctx, IoctlVersion::Version2);
  85. }
  86. void NVDRV::Ioctl3(Kernel::HLERequestContext& ctx) {
  87. LOG_DEBUG(Service_NVDRV, "called");
  88. IoctlBase(ctx, IoctlVersion::Version3);
  89. }
  90. void NVDRV::Close(Kernel::HLERequestContext& ctx) {
  91. LOG_DEBUG(Service_NVDRV, "called");
  92. IPC::RequestParser rp{ctx};
  93. u32 fd = rp.Pop<u32>();
  94. auto result = nvdrv->Close(fd);
  95. IPC::ResponseBuilder rb{ctx, 2};
  96. rb.Push(result);
  97. }
  98. void NVDRV::Initialize(Kernel::HLERequestContext& ctx) {
  99. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  100. IPC::ResponseBuilder rb{ctx, 3};
  101. rb.Push(RESULT_SUCCESS);
  102. rb.Push<u32>(0);
  103. }
  104. void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) {
  105. IPC::RequestParser rp{ctx};
  106. u32 fd = rp.Pop<u32>();
  107. // TODO(Blinkhawk): Figure the meaning of the flag at bit 16
  108. u32 event_id = rp.Pop<u32>() & 0x000000FF;
  109. LOG_WARNING(Service_NVDRV, "(STUBBED) called, fd={:X}, event_id={:X}", fd, event_id);
  110. IPC::ResponseBuilder rb{ctx, 3, 1};
  111. rb.Push(RESULT_SUCCESS);
  112. if (event_id < MaxNvEvents) {
  113. auto event = nvdrv->GetEvent(event_id);
  114. event->Clear();
  115. rb.PushCopyObjects(event);
  116. rb.Push<u32>(NvResult::Success);
  117. } else {
  118. rb.Push<u32>(0);
  119. rb.Push<u32>(NvResult::BadParameter);
  120. }
  121. }
  122. void NVDRV::SetAruid(Kernel::HLERequestContext& ctx) {
  123. IPC::RequestParser rp{ctx};
  124. pid = rp.Pop<u64>();
  125. LOG_WARNING(Service_NVDRV, "(STUBBED) called, pid=0x{:X}", pid);
  126. IPC::ResponseBuilder rb{ctx, 3};
  127. rb.Push(RESULT_SUCCESS);
  128. rb.Push<u32>(0);
  129. }
  130. void NVDRV::SetGraphicsFirmwareMemoryMarginEnabled(Kernel::HLERequestContext& ctx) {
  131. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  132. IPC::ResponseBuilder rb{ctx, 2};
  133. rb.Push(RESULT_SUCCESS);
  134. }
  135. void NVDRV::GetStatus(Kernel::HLERequestContext& ctx) {
  136. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  137. IPC::ResponseBuilder rb{ctx, 2};
  138. rb.Push(RESULT_SUCCESS);
  139. }
  140. void NVDRV::DumpGraphicsMemoryInfo(Kernel::HLERequestContext& ctx) {
  141. // According to SwitchBrew, this has no inputs and no outputs, so effectively does nothing on
  142. // retail hardware.
  143. LOG_DEBUG(Service_NVDRV, "called");
  144. IPC::ResponseBuilder rb{ctx, 2};
  145. rb.Push(RESULT_SUCCESS);
  146. }
  147. NVDRV::NVDRV(std::shared_ptr<Module> nvdrv, const char* name)
  148. : ServiceFramework(name), nvdrv(std::move(nvdrv)) {
  149. static const FunctionInfo functions[] = {
  150. {0, &NVDRV::Open, "Open"},
  151. {1, &NVDRV::Ioctl, "Ioctl"},
  152. {2, &NVDRV::Close, "Close"},
  153. {3, &NVDRV::Initialize, "Initialize"},
  154. {4, &NVDRV::QueryEvent, "QueryEvent"},
  155. {5, nullptr, "MapSharedMem"},
  156. {6, &NVDRV::GetStatus, "GetStatus"},
  157. {7, nullptr, "SetAruidForTest"},
  158. {8, &NVDRV::SetAruid, "SetAruid"},
  159. {9, &NVDRV::DumpGraphicsMemoryInfo, "DumpGraphicsMemoryInfo"},
  160. {10, nullptr, "InitializeDevtools"},
  161. {11, &NVDRV::Ioctl2, "Ioctl2"},
  162. {12, &NVDRV::Ioctl3, "Ioctl3"},
  163. {13, &NVDRV::SetGraphicsFirmwareMemoryMarginEnabled,
  164. "SetGraphicsFirmwareMemoryMarginEnabled"},
  165. };
  166. RegisterHandlers(functions);
  167. }
  168. NVDRV::~NVDRV() = default;
  169. } // namespace Service::Nvidia