nvdrv.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <utility>
  5. #include <fmt/format.h>
  6. #include "core/core.h"
  7. #include "core/hle/ipc_helpers.h"
  8. #include "core/hle/kernel/k_event.h"
  9. #include "core/hle/kernel/k_readable_event.h"
  10. #include "core/hle/kernel/k_writable_event.h"
  11. #include "core/hle/service/nvdrv/devices/nvdevice.h"
  12. #include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
  13. #include "core/hle/service/nvdrv/devices/nvhost_as_gpu.h"
  14. #include "core/hle/service/nvdrv/devices/nvhost_ctrl.h"
  15. #include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
  16. #include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
  17. #include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
  18. #include "core/hle/service/nvdrv/devices/nvhost_nvjpg.h"
  19. #include "core/hle/service/nvdrv/devices/nvhost_vic.h"
  20. #include "core/hle/service/nvdrv/devices/nvmap.h"
  21. #include "core/hle/service/nvdrv/interface.h"
  22. #include "core/hle/service/nvdrv/nvdrv.h"
  23. #include "core/hle/service/nvdrv/nvmemp.h"
  24. #include "core/hle/service/nvdrv/syncpoint_manager.h"
  25. #include "core/hle/service/nvflinger/nvflinger.h"
  26. namespace Service::Nvidia {
  27. void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
  28. Core::System& system) {
  29. auto module_ = std::make_shared<Module>(system);
  30. std::make_shared<NVDRV>(system, module_, "nvdrv")->InstallAsService(service_manager);
  31. std::make_shared<NVDRV>(system, module_, "nvdrv:a")->InstallAsService(service_manager);
  32. std::make_shared<NVDRV>(system, module_, "nvdrv:s")->InstallAsService(service_manager);
  33. std::make_shared<NVDRV>(system, module_, "nvdrv:t")->InstallAsService(service_manager);
  34. std::make_shared<NVMEMP>(system)->InstallAsService(service_manager);
  35. nvflinger.SetNVDrvInstance(module_);
  36. }
  37. Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} {
  38. auto& kernel = system.Kernel();
  39. for (u32 i = 0; i < MaxNvEvents; i++) {
  40. std::string event_label = fmt::format("NVDRV::NvEvent_{}", i);
  41. events_interface.events[i] = {Kernel::KEvent::Create(kernel, std::move(event_label))};
  42. events_interface.events[i].event->Initialize();
  43. events_interface.status[i] = EventState::Free;
  44. events_interface.registered[i] = false;
  45. }
  46. auto nvmap_dev = std::make_shared<Devices::nvmap>(system);
  47. devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev);
  48. devices["/dev/nvhost-gpu"] =
  49. std::make_shared<Devices::nvhost_gpu>(system, nvmap_dev, syncpoint_manager);
  50. devices["/dev/nvhost-ctrl-gpu"] = std::make_shared<Devices::nvhost_ctrl_gpu>(system);
  51. devices["/dev/nvmap"] = nvmap_dev;
  52. devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, nvmap_dev);
  53. devices["/dev/nvhost-ctrl"] =
  54. std::make_shared<Devices::nvhost_ctrl>(system, events_interface, syncpoint_manager);
  55. devices["/dev/nvhost-nvdec"] =
  56. std::make_shared<Devices::nvhost_nvdec>(system, nvmap_dev, syncpoint_manager);
  57. devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
  58. devices["/dev/nvhost-vic"] =
  59. std::make_shared<Devices::nvhost_vic>(system, nvmap_dev, syncpoint_manager);
  60. }
  61. Module::~Module() = default;
  62. NvResult Module::VerifyFD(DeviceFD fd) const {
  63. if (fd < 0) {
  64. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  65. return NvResult::InvalidState;
  66. }
  67. if (open_files.find(fd) == open_files.end()) {
  68. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  69. return NvResult::NotImplemented;
  70. }
  71. return NvResult::Success;
  72. }
  73. DeviceFD Module::Open(const std::string& device_name) {
  74. if (devices.find(device_name) == devices.end()) {
  75. LOG_ERROR(Service_NVDRV, "Trying to open unknown device {}", device_name);
  76. return INVALID_NVDRV_FD;
  77. }
  78. auto device = devices[device_name];
  79. const DeviceFD fd = next_fd++;
  80. device->OnOpen(fd);
  81. open_files[fd] = std::move(device);
  82. return fd;
  83. }
  84. NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  85. std::vector<u8>& output) {
  86. if (fd < 0) {
  87. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  88. return NvResult::InvalidState;
  89. }
  90. const auto itr = open_files.find(fd);
  91. if (itr == open_files.end()) {
  92. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  93. return NvResult::NotImplemented;
  94. }
  95. return itr->second->Ioctl1(fd, command, input, output);
  96. }
  97. NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  98. const std::vector<u8>& inline_input, std::vector<u8>& output) {
  99. if (fd < 0) {
  100. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  101. return NvResult::InvalidState;
  102. }
  103. const auto itr = open_files.find(fd);
  104. if (itr == open_files.end()) {
  105. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  106. return NvResult::NotImplemented;
  107. }
  108. return itr->second->Ioctl2(fd, command, input, inline_input, output);
  109. }
  110. NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  111. std::vector<u8>& output, std::vector<u8>& inline_output) {
  112. if (fd < 0) {
  113. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  114. return NvResult::InvalidState;
  115. }
  116. const auto itr = open_files.find(fd);
  117. if (itr == open_files.end()) {
  118. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  119. return NvResult::NotImplemented;
  120. }
  121. return itr->second->Ioctl3(fd, command, input, output, inline_output);
  122. }
  123. NvResult Module::Close(DeviceFD fd) {
  124. if (fd < 0) {
  125. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  126. return NvResult::InvalidState;
  127. }
  128. const auto itr = open_files.find(fd);
  129. if (itr == open_files.end()) {
  130. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  131. return NvResult::NotImplemented;
  132. }
  133. itr->second->OnClose(fd);
  134. open_files.erase(itr);
  135. return NvResult::Success;
  136. }
  137. void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
  138. for (u32 i = 0; i < MaxNvEvents; i++) {
  139. if (events_interface.assigned_syncpt[i] == syncpoint_id &&
  140. events_interface.assigned_value[i] == value) {
  141. events_interface.LiberateEvent(i);
  142. events_interface.events[i].event->GetWritableEvent()->Signal();
  143. }
  144. }
  145. }
  146. std::shared_ptr<Kernel::KReadableEvent> Module::GetEvent(const u32 event_id) const {
  147. return SharedFrom(events_interface.events[event_id].event->GetReadableEvent());
  148. }
  149. std::shared_ptr<Kernel::KWritableEvent> Module::GetEventWriteable(const u32 event_id) const {
  150. return events_interface.events[event_id].event->GetWritableEvent();
  151. }
  152. } // namespace Service::Nvidia