interface.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. if (!is_initialized) {
  22. ServiceError(ctx, NvResult::NotInitialized);
  23. LOG_ERROR(Service_NVDRV, "NvServices is not initalized!");
  24. return;
  25. }
  26. const auto& buffer = ctx.ReadBuffer();
  27. const std::string device_name(buffer.begin(), buffer.end());
  28. DeviceFD fd = nvdrv->Open(device_name);
  29. IPC::ResponseBuilder rb{ctx, 4};
  30. rb.Push(RESULT_SUCCESS);
  31. rb.Push<DeviceFD>(fd);
  32. rb.PushEnum(fd != INVALID_NVDRV_FD ? NvResult::Success : NvResult::FileOperationFailed);
  33. }
  34. void NVDRV::ServiceError(Kernel::HLERequestContext& ctx, NvResult result) {
  35. IPC::ResponseBuilder rb{ctx, 3};
  36. rb.Push(RESULT_SUCCESS);
  37. rb.PushEnum(result);
  38. }
  39. void NVDRV::Ioctl1(Kernel::HLERequestContext& ctx) {
  40. IPC::RequestParser rp{ctx};
  41. const auto fd = rp.Pop<DeviceFD>();
  42. const auto command = rp.PopRaw<Ioctl>();
  43. LOG_DEBUG(Service_NVDRV, "called fd={}, ioctl=0x{:08X}", fd, command.raw);
  44. if (!is_initialized) {
  45. ServiceError(ctx, NvResult::NotInitialized);
  46. LOG_ERROR(Service_NVDRV, "NvServices is not initalized!");
  47. return;
  48. }
  49. // Check device
  50. std::vector<u8> output_buffer(ctx.GetWriteBufferSize(0));
  51. const auto input_buffer = ctx.ReadBuffer(0);
  52. IoctlCtrl ctrl{};
  53. const auto nv_result = nvdrv->Ioctl1(fd, command, input_buffer, output_buffer, ctrl);
  54. if (ctrl.must_delay) {
  55. ctrl.fresh_call = false;
  56. ctx.SleepClientThread(
  57. "NVServices::DelayedResponse", ctrl.timeout,
  58. [=, this](std::shared_ptr<Kernel::Thread> thread, Kernel::HLERequestContext& ctx_,
  59. Kernel::ThreadWakeupReason reason) {
  60. IoctlCtrl ctrl2{ctrl};
  61. std::vector<u8> tmp_output = output_buffer;
  62. const auto nv_result2 = nvdrv->Ioctl1(fd, command, input_buffer, tmp_output, ctrl2);
  63. if (command.is_out != 0) {
  64. ctx.WriteBuffer(tmp_output);
  65. }
  66. IPC::ResponseBuilder rb{ctx_, 3};
  67. rb.Push(RESULT_SUCCESS);
  68. rb.PushEnum(nv_result2);
  69. },
  70. nvdrv->GetEventWriteable(ctrl.event_id));
  71. } else {
  72. if (command.is_out != 0) {
  73. ctx.WriteBuffer(output_buffer);
  74. }
  75. }
  76. IPC::ResponseBuilder rb{ctx, 3};
  77. rb.Push(RESULT_SUCCESS);
  78. rb.PushEnum(nv_result);
  79. }
  80. void NVDRV::Ioctl2(Kernel::HLERequestContext& ctx) {
  81. IPC::RequestParser rp{ctx};
  82. const auto fd = rp.Pop<DeviceFD>();
  83. const auto command = rp.PopRaw<Ioctl>();
  84. LOG_DEBUG(Service_NVDRV, "called fd={}, ioctl=0x{:08X}", fd, command.raw);
  85. if (!is_initialized) {
  86. ServiceError(ctx, NvResult::NotInitialized);
  87. LOG_ERROR(Service_NVDRV, "NvServices is not initalized!");
  88. return;
  89. }
  90. const auto input_buffer = ctx.ReadBuffer(0);
  91. const auto input_inlined_buffer = ctx.ReadBuffer(1);
  92. std::vector<u8> output_buffer(ctx.GetWriteBufferSize(0));
  93. IoctlCtrl ctrl{};
  94. const auto nv_result =
  95. nvdrv->Ioctl2(fd, command, input_buffer, input_inlined_buffer, output_buffer, ctrl);
  96. if (ctrl.must_delay) {
  97. ctrl.fresh_call = false;
  98. ctx.SleepClientThread(
  99. "NVServices::DelayedResponse", ctrl.timeout,
  100. [=, this](std::shared_ptr<Kernel::Thread> thread, Kernel::HLERequestContext& ctx_,
  101. Kernel::ThreadWakeupReason reason) {
  102. IoctlCtrl ctrl2{ctrl};
  103. std::vector<u8> tmp_output = output_buffer;
  104. const auto nv_result2 = nvdrv->Ioctl2(fd, command, input_buffer,
  105. input_inlined_buffer, tmp_output, ctrl2);
  106. if (command.is_out != 0) {
  107. ctx.WriteBuffer(tmp_output);
  108. }
  109. IPC::ResponseBuilder rb{ctx_, 3};
  110. rb.Push(RESULT_SUCCESS);
  111. rb.PushEnum(nv_result2);
  112. },
  113. nvdrv->GetEventWriteable(ctrl.event_id));
  114. } else {
  115. if (command.is_out != 0) {
  116. ctx.WriteBuffer(output_buffer);
  117. }
  118. }
  119. if (command.is_out != 0) {
  120. ctx.WriteBuffer(output_buffer);
  121. }
  122. IPC::ResponseBuilder rb{ctx, 3};
  123. rb.Push(RESULT_SUCCESS);
  124. rb.PushEnum(nv_result);
  125. }
  126. void NVDRV::Ioctl3(Kernel::HLERequestContext& ctx) {
  127. IPC::RequestParser rp{ctx};
  128. const auto fd = rp.Pop<DeviceFD>();
  129. const auto command = rp.PopRaw<Ioctl>();
  130. LOG_DEBUG(Service_NVDRV, "called fd={}, ioctl=0x{:08X}", fd, command.raw);
  131. if (!is_initialized) {
  132. ServiceError(ctx, NvResult::NotInitialized);
  133. LOG_ERROR(Service_NVDRV, "NvServices is not initalized!");
  134. return;
  135. }
  136. const auto input_buffer = ctx.ReadBuffer(0);
  137. std::vector<u8> output_buffer(ctx.GetWriteBufferSize(0));
  138. std::vector<u8> output_buffer_inline(ctx.GetWriteBufferSize(1));
  139. IoctlCtrl ctrl{};
  140. const auto nv_result =
  141. nvdrv->Ioctl3(fd, command, input_buffer, output_buffer, output_buffer_inline, ctrl);
  142. if (ctrl.must_delay) {
  143. ctrl.fresh_call = false;
  144. ctx.SleepClientThread(
  145. "NVServices::DelayedResponse", ctrl.timeout,
  146. [=, this](std::shared_ptr<Kernel::Thread> thread, Kernel::HLERequestContext& ctx_,
  147. Kernel::ThreadWakeupReason reason) {
  148. IoctlCtrl ctrl2{ctrl};
  149. std::vector<u8> tmp_output = output_buffer;
  150. std::vector<u8> tmp_output2 = output_buffer;
  151. const auto nv_result2 =
  152. nvdrv->Ioctl3(fd, command, input_buffer, tmp_output, tmp_output2, ctrl2);
  153. if (command.is_out != 0) {
  154. ctx.WriteBuffer(tmp_output, 0);
  155. ctx.WriteBuffer(tmp_output2, 1);
  156. }
  157. IPC::ResponseBuilder rb{ctx_, 3};
  158. rb.Push(RESULT_SUCCESS);
  159. rb.PushEnum(nv_result2);
  160. },
  161. nvdrv->GetEventWriteable(ctrl.event_id));
  162. } else {
  163. if (command.is_out != 0) {
  164. ctx.WriteBuffer(output_buffer, 0);
  165. ctx.WriteBuffer(output_buffer_inline, 1);
  166. }
  167. }
  168. IPC::ResponseBuilder rb{ctx, 3};
  169. rb.Push(RESULT_SUCCESS);
  170. rb.PushEnum(nv_result);
  171. }
  172. void NVDRV::Close(Kernel::HLERequestContext& ctx) {
  173. LOG_DEBUG(Service_NVDRV, "called");
  174. if (!is_initialized) {
  175. ServiceError(ctx, NvResult::NotInitialized);
  176. LOG_ERROR(Service_NVDRV, "NvServices is not initalized!");
  177. return;
  178. }
  179. IPC::RequestParser rp{ctx};
  180. const auto fd = rp.Pop<DeviceFD>();
  181. const auto result = nvdrv->Close(fd);
  182. IPC::ResponseBuilder rb{ctx, 3};
  183. rb.Push(RESULT_SUCCESS);
  184. rb.PushEnum(result);
  185. }
  186. void NVDRV::Initialize(Kernel::HLERequestContext& ctx) {
  187. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  188. is_initialized = true;
  189. IPC::ResponseBuilder rb{ctx, 3};
  190. rb.Push(RESULT_SUCCESS);
  191. rb.PushEnum(NvResult::Success);
  192. }
  193. void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) {
  194. IPC::RequestParser rp{ctx};
  195. const auto fd = rp.Pop<DeviceFD>();
  196. const auto event_id = rp.Pop<u32>() & 0x00FF;
  197. LOG_WARNING(Service_NVDRV, "(STUBBED) called, fd={:X}, event_id={:X}", fd, event_id);
  198. if (!is_initialized) {
  199. ServiceError(ctx, NvResult::NotInitialized);
  200. LOG_ERROR(Service_NVDRV, "NvServices is not initalized!");
  201. return;
  202. }
  203. const auto nv_result = nvdrv->VerifyFD(fd);
  204. if (nv_result != NvResult::Success) {
  205. LOG_ERROR(Service_NVDRV, "Invalid FD specified DeviceFD={}!", fd);
  206. ServiceError(ctx, nv_result);
  207. return;
  208. }
  209. if (event_id < MaxNvEvents) {
  210. IPC::ResponseBuilder rb{ctx, 3, 1};
  211. rb.Push(RESULT_SUCCESS);
  212. auto event = nvdrv->GetEvent(event_id);
  213. event->Clear();
  214. rb.PushCopyObjects(event);
  215. rb.PushEnum(NvResult::Success);
  216. } else {
  217. IPC::ResponseBuilder rb{ctx, 3};
  218. rb.Push(RESULT_SUCCESS);
  219. rb.PushEnum(NvResult::BadParameter);
  220. }
  221. }
  222. void NVDRV::SetAruid(Kernel::HLERequestContext& ctx) {
  223. IPC::RequestParser rp{ctx};
  224. pid = rp.Pop<u64>();
  225. LOG_WARNING(Service_NVDRV, "(STUBBED) called, pid=0x{:X}", pid);
  226. IPC::ResponseBuilder rb{ctx, 3};
  227. rb.Push(RESULT_SUCCESS);
  228. rb.PushEnum(NvResult::Success);
  229. }
  230. void NVDRV::SetGraphicsFirmwareMemoryMarginEnabled(Kernel::HLERequestContext& ctx) {
  231. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  232. IPC::ResponseBuilder rb{ctx, 2};
  233. rb.Push(RESULT_SUCCESS);
  234. }
  235. void NVDRV::GetStatus(Kernel::HLERequestContext& ctx) {
  236. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  237. IPC::ResponseBuilder rb{ctx, 3};
  238. rb.Push(RESULT_SUCCESS);
  239. rb.PushEnum(NvResult::Success);
  240. }
  241. void NVDRV::DumpGraphicsMemoryInfo(Kernel::HLERequestContext& ctx) {
  242. // According to SwitchBrew, this has no inputs and no outputs, so effectively does nothing on
  243. // retail hardware.
  244. LOG_DEBUG(Service_NVDRV, "called");
  245. IPC::ResponseBuilder rb{ctx, 2};
  246. rb.Push(RESULT_SUCCESS);
  247. }
  248. NVDRV::NVDRV(std::shared_ptr<Module> nvdrv, const char* name)
  249. : ServiceFramework(name), nvdrv(std::move(nvdrv)) {
  250. static const FunctionInfo functions[] = {
  251. {0, &NVDRV::Open, "Open"},
  252. {1, &NVDRV::Ioctl1, "Ioctl"},
  253. {2, &NVDRV::Close, "Close"},
  254. {3, &NVDRV::Initialize, "Initialize"},
  255. {4, &NVDRV::QueryEvent, "QueryEvent"},
  256. {5, nullptr, "MapSharedMem"},
  257. {6, &NVDRV::GetStatus, "GetStatus"},
  258. {7, nullptr, "SetAruidForTest"},
  259. {8, &NVDRV::SetAruid, "SetAruid"},
  260. {9, &NVDRV::DumpGraphicsMemoryInfo, "DumpGraphicsMemoryInfo"},
  261. {10, nullptr, "InitializeDevtools"},
  262. {11, &NVDRV::Ioctl2, "Ioctl2"},
  263. {12, &NVDRV::Ioctl3, "Ioctl3"},
  264. {13, &NVDRV::SetGraphicsFirmwareMemoryMarginEnabled,
  265. "SetGraphicsFirmwareMemoryMarginEnabled"},
  266. };
  267. RegisterHandlers(functions);
  268. }
  269. NVDRV::~NVDRV() = default;
  270. } // namespace Service::Nvidia