nvdrv.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. events_interface.events[i].event = Kernel::KEvent::Create(kernel);
  41. events_interface.events[i].event->Initialize(fmt::format("NVDRV::NvEvent_{}", i));
  42. events_interface.status[i] = EventState::Free;
  43. events_interface.registered[i] = false;
  44. }
  45. auto nvmap_dev = std::make_shared<Devices::nvmap>(system);
  46. devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev);
  47. devices["/dev/nvhost-gpu"] =
  48. std::make_shared<Devices::nvhost_gpu>(system, nvmap_dev, syncpoint_manager);
  49. devices["/dev/nvhost-ctrl-gpu"] = std::make_shared<Devices::nvhost_ctrl_gpu>(system);
  50. devices["/dev/nvmap"] = nvmap_dev;
  51. devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, nvmap_dev);
  52. devices["/dev/nvhost-ctrl"] =
  53. std::make_shared<Devices::nvhost_ctrl>(system, events_interface, syncpoint_manager);
  54. devices["/dev/nvhost-nvdec"] =
  55. std::make_shared<Devices::nvhost_nvdec>(system, nvmap_dev, syncpoint_manager);
  56. devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
  57. devices["/dev/nvhost-vic"] =
  58. std::make_shared<Devices::nvhost_vic>(system, nvmap_dev, syncpoint_manager);
  59. }
  60. Module::~Module() {
  61. for (u32 i = 0; i < MaxNvEvents; i++) {
  62. events_interface.events[i].event->Close();
  63. events_interface.events[i].event = nullptr;
  64. }
  65. }
  66. NvResult Module::VerifyFD(DeviceFD fd) const {
  67. if (fd < 0) {
  68. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  69. return NvResult::InvalidState;
  70. }
  71. if (open_files.find(fd) == open_files.end()) {
  72. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  73. return NvResult::NotImplemented;
  74. }
  75. return NvResult::Success;
  76. }
  77. DeviceFD Module::Open(const std::string& device_name) {
  78. if (devices.find(device_name) == devices.end()) {
  79. LOG_ERROR(Service_NVDRV, "Trying to open unknown device {}", device_name);
  80. return INVALID_NVDRV_FD;
  81. }
  82. auto device = devices[device_name];
  83. const DeviceFD fd = next_fd++;
  84. device->OnOpen(fd);
  85. open_files[fd] = std::move(device);
  86. return fd;
  87. }
  88. NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  89. std::vector<u8>& output) {
  90. if (fd < 0) {
  91. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  92. return NvResult::InvalidState;
  93. }
  94. const auto itr = open_files.find(fd);
  95. if (itr == open_files.end()) {
  96. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  97. return NvResult::NotImplemented;
  98. }
  99. return itr->second->Ioctl1(fd, command, input, output);
  100. }
  101. NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  102. const std::vector<u8>& inline_input, std::vector<u8>& output) {
  103. if (fd < 0) {
  104. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  105. return NvResult::InvalidState;
  106. }
  107. const auto itr = open_files.find(fd);
  108. if (itr == open_files.end()) {
  109. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  110. return NvResult::NotImplemented;
  111. }
  112. return itr->second->Ioctl2(fd, command, input, inline_input, output);
  113. }
  114. NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  115. std::vector<u8>& output, std::vector<u8>& inline_output) {
  116. if (fd < 0) {
  117. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  118. return NvResult::InvalidState;
  119. }
  120. const auto itr = open_files.find(fd);
  121. if (itr == open_files.end()) {
  122. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  123. return NvResult::NotImplemented;
  124. }
  125. return itr->second->Ioctl3(fd, command, input, output, inline_output);
  126. }
  127. NvResult Module::Close(DeviceFD fd) {
  128. if (fd < 0) {
  129. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  130. return NvResult::InvalidState;
  131. }
  132. const auto itr = open_files.find(fd);
  133. if (itr == open_files.end()) {
  134. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  135. return NvResult::NotImplemented;
  136. }
  137. itr->second->OnClose(fd);
  138. open_files.erase(itr);
  139. return NvResult::Success;
  140. }
  141. void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
  142. for (u32 i = 0; i < MaxNvEvents; i++) {
  143. if (events_interface.assigned_syncpt[i] == syncpoint_id &&
  144. events_interface.assigned_value[i] == value) {
  145. events_interface.LiberateEvent(i);
  146. events_interface.events[i].event->GetWritableEvent()->Signal();
  147. }
  148. }
  149. }
  150. std::shared_ptr<Kernel::KReadableEvent> Module::GetEvent(const u32 event_id) const {
  151. return SharedFrom(events_interface.events[event_id].event->GetReadableEvent());
  152. }
  153. std::shared_ptr<Kernel::KWritableEvent> Module::GetEventWriteable(const u32 event_id) const {
  154. return events_interface.events[event_id].event->GetWritableEvent();
  155. }
  156. } // namespace Service::Nvidia