nvdrv.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-FileCopyrightText: 2021 yuzu emulator team and Skyline Team and Contributors
  2. // (https://github.com/skyline-emu/)
  3. // SPDX-License-Identifier: GPL-3.0-or-later Licensed under GPLv3
  4. // or any later version Refer to the license.txt file included.
  5. #include <bit>
  6. #include <utility>
  7. #include <fmt/format.h>
  8. #include "core/core.h"
  9. #include "core/hle/ipc_helpers.h"
  10. #include "core/hle/kernel/k_event.h"
  11. #include "core/hle/kernel/k_writable_event.h"
  12. #include "core/hle/service/nvdrv/core/container.h"
  13. #include "core/hle/service/nvdrv/devices/nvdevice.h"
  14. #include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
  15. #include "core/hle/service/nvdrv/devices/nvhost_as_gpu.h"
  16. #include "core/hle/service/nvdrv/devices/nvhost_ctrl.h"
  17. #include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
  18. #include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
  19. #include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
  20. #include "core/hle/service/nvdrv/devices/nvhost_nvjpg.h"
  21. #include "core/hle/service/nvdrv/devices/nvhost_vic.h"
  22. #include "core/hle/service/nvdrv/devices/nvmap.h"
  23. #include "core/hle/service/nvdrv/nvdrv.h"
  24. #include "core/hle/service/nvdrv/nvdrv_interface.h"
  25. #include "core/hle/service/nvdrv/nvmemp.h"
  26. #include "core/hle/service/nvflinger/nvflinger.h"
  27. #include "video_core/gpu.h"
  28. namespace Service::Nvidia {
  29. EventInterface::EventInterface(Module& module_) : module{module_} {
  30. events_mask = 0;
  31. for (u32 i = 0; i < MaxNvEvents; i++) {
  32. status[i] = EventState::Available;
  33. events[i] = nullptr;
  34. registered[i] = false;
  35. }
  36. }
  37. EventInterface::~EventInterface() {
  38. auto lk = Lock();
  39. for (u32 i = 0; i < MaxNvEvents; i++) {
  40. if (registered[i]) {
  41. module.service_context.CloseEvent(events[i]);
  42. events[i] = nullptr;
  43. registered[i] = false;
  44. }
  45. }
  46. for (auto* event : basic_events) {
  47. module.service_context.CloseEvent(event);
  48. }
  49. }
  50. std::unique_lock<std::mutex> EventInterface::Lock() {
  51. return std::unique_lock<std::mutex>(events_mutex);
  52. }
  53. void EventInterface::Signal(u32 event_id) {
  54. if (status[event_id].exchange(EventState::Signalling, std::memory_order_acq_rel) ==
  55. EventState::Waiting) {
  56. events[event_id]->GetWritableEvent().Signal();
  57. }
  58. status[event_id].store(EventState::Signalled, std::memory_order_release);
  59. }
  60. void EventInterface::Create(u32 event_id) {
  61. ASSERT(!events[event_id]);
  62. ASSERT(!registered[event_id]);
  63. ASSERT(!IsBeingUsed(event_id));
  64. events[event_id] =
  65. module.service_context.CreateEvent(fmt::format("NVDRV::NvEvent_{}", event_id));
  66. status[event_id] = EventState::Available;
  67. registered[event_id] = true;
  68. const u64 mask = 1ULL << event_id;
  69. fails[event_id] = 0;
  70. events_mask |= mask;
  71. assigned_syncpt[event_id] = 0;
  72. }
  73. void EventInterface::Free(u32 event_id) {
  74. ASSERT(events[event_id]);
  75. ASSERT(registered[event_id]);
  76. ASSERT(!IsBeingUsed(event_id));
  77. module.service_context.CloseEvent(events[event_id]);
  78. events[event_id] = nullptr;
  79. status[event_id] = EventState::Available;
  80. registered[event_id] = false;
  81. const u64 mask = ~(1ULL << event_id);
  82. events_mask &= mask;
  83. }
  84. u32 EventInterface::FindFreeEvent(u32 syncpoint_id) {
  85. u32 slot{MaxNvEvents};
  86. u32 free_slot{MaxNvEvents};
  87. for (u32 i = 0; i < MaxNvEvents; i++) {
  88. if (registered[i]) {
  89. if (!IsBeingUsed(i)) {
  90. slot = i;
  91. if (assigned_syncpt[i] == syncpoint_id) {
  92. return slot;
  93. }
  94. }
  95. } else if (free_slot == MaxNvEvents) {
  96. free_slot = i;
  97. }
  98. }
  99. if (free_slot < MaxNvEvents) {
  100. Create(free_slot);
  101. return free_slot;
  102. }
  103. if (slot < MaxNvEvents) {
  104. return slot;
  105. }
  106. LOG_CRITICAL(Service_NVDRV, "Failed to allocate an event");
  107. return 0;
  108. }
  109. Kernel::KEvent* EventInterface::CreateNonCtrlEvent(std::string name) {
  110. Kernel::KEvent* new_event = module.service_context.CreateEvent(std::move(name));
  111. basic_events.push_back(new_event);
  112. return new_event;
  113. }
  114. void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
  115. Core::System& system) {
  116. auto module_ = std::make_shared<Module>(system);
  117. std::make_shared<NVDRV>(system, module_, "nvdrv")->InstallAsService(service_manager);
  118. std::make_shared<NVDRV>(system, module_, "nvdrv:a")->InstallAsService(service_manager);
  119. std::make_shared<NVDRV>(system, module_, "nvdrv:s")->InstallAsService(service_manager);
  120. std::make_shared<NVDRV>(system, module_, "nvdrv:t")->InstallAsService(service_manager);
  121. std::make_shared<NVMEMP>(system)->InstallAsService(service_manager);
  122. nvflinger.SetNVDrvInstance(module_);
  123. }
  124. Module::Module(Core::System& system)
  125. : service_context{system, "nvdrv"}, events_interface{*this}, container{system.GPU()} {
  126. devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, container);
  127. devices["/dev/nvhost-gpu"] =
  128. std::make_shared<Devices::nvhost_gpu>(system, events_interface, container);
  129. devices["/dev/nvhost-ctrl-gpu"] =
  130. std::make_shared<Devices::nvhost_ctrl_gpu>(system, events_interface);
  131. devices["/dev/nvmap"] = std::make_shared<Devices::nvmap>(system, container);
  132. devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, container);
  133. devices["/dev/nvhost-ctrl"] =
  134. std::make_shared<Devices::nvhost_ctrl>(system, events_interface, container);
  135. devices["/dev/nvhost-nvdec"] = std::make_shared<Devices::nvhost_nvdec>(system, container);
  136. devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
  137. devices["/dev/nvhost-vic"] = std::make_shared<Devices::nvhost_vic>(system, container);
  138. }
  139. Module::~Module() = default;
  140. NvResult Module::VerifyFD(DeviceFD fd) const {
  141. if (fd < 0) {
  142. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  143. return NvResult::InvalidState;
  144. }
  145. if (open_files.find(fd) == open_files.end()) {
  146. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  147. return NvResult::NotImplemented;
  148. }
  149. return NvResult::Success;
  150. }
  151. DeviceFD Module::Open(const std::string& device_name) {
  152. if (devices.find(device_name) == devices.end()) {
  153. LOG_ERROR(Service_NVDRV, "Trying to open unknown device {}", device_name);
  154. return INVALID_NVDRV_FD;
  155. }
  156. auto device = devices[device_name];
  157. const DeviceFD fd = next_fd++;
  158. device->OnOpen(fd);
  159. open_files[fd] = std::move(device);
  160. return fd;
  161. }
  162. NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  163. std::vector<u8>& output) {
  164. if (fd < 0) {
  165. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  166. return NvResult::InvalidState;
  167. }
  168. const auto itr = open_files.find(fd);
  169. if (itr == open_files.end()) {
  170. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  171. return NvResult::NotImplemented;
  172. }
  173. return itr->second->Ioctl1(fd, command, input, output);
  174. }
  175. NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  176. const std::vector<u8>& inline_input, std::vector<u8>& output) {
  177. if (fd < 0) {
  178. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  179. return NvResult::InvalidState;
  180. }
  181. const auto itr = open_files.find(fd);
  182. if (itr == open_files.end()) {
  183. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  184. return NvResult::NotImplemented;
  185. }
  186. return itr->second->Ioctl2(fd, command, input, inline_input, output);
  187. }
  188. NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  189. std::vector<u8>& output, std::vector<u8>& inline_output) {
  190. if (fd < 0) {
  191. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  192. return NvResult::InvalidState;
  193. }
  194. const auto itr = open_files.find(fd);
  195. if (itr == open_files.end()) {
  196. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  197. return NvResult::NotImplemented;
  198. }
  199. return itr->second->Ioctl3(fd, command, input, output, inline_output);
  200. }
  201. NvResult Module::Close(DeviceFD fd) {
  202. if (fd < 0) {
  203. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  204. return NvResult::InvalidState;
  205. }
  206. const auto itr = open_files.find(fd);
  207. if (itr == open_files.end()) {
  208. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  209. return NvResult::NotImplemented;
  210. }
  211. itr->second->OnClose(fd);
  212. open_files.erase(itr);
  213. return NvResult::Success;
  214. }
  215. void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
  216. const u32 max = MaxNvEvents - std::countl_zero(events_interface.events_mask);
  217. const u32 min = std::countr_zero(events_interface.events_mask);
  218. for (u32 i = min; i < max; i++) {
  219. if (events_interface.assigned_syncpt[i] == syncpoint_id &&
  220. events_interface.assigned_value[i] == value) {
  221. events_interface.Signal(i);
  222. }
  223. }
  224. }
  225. NvResult Module::QueryEvent(DeviceFD fd, u32 event_id, Kernel::KEvent*& event) {
  226. if (fd < 0) {
  227. LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
  228. return NvResult::InvalidState;
  229. }
  230. const auto itr = open_files.find(fd);
  231. if (itr == open_files.end()) {
  232. LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
  233. return NvResult::NotImplemented;
  234. }
  235. event = itr->second->QueryEvent(event_id);
  236. if (!event) {
  237. return NvResult::BadParameter;
  238. }
  239. return NvResult::Success;
  240. }
  241. } // namespace Service::Nvidia