nvdrv.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/hle/ipc_helpers.h"
  7. #include "core/hle/kernel/readable_event.h"
  8. #include "core/hle/kernel/writable_event.h"
  9. #include "core/hle/service/nvdrv/devices/nvdevice.h"
  10. #include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
  11. #include "core/hle/service/nvdrv/devices/nvhost_as_gpu.h"
  12. #include "core/hle/service/nvdrv/devices/nvhost_ctrl.h"
  13. #include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
  14. #include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
  15. #include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
  16. #include "core/hle/service/nvdrv/devices/nvhost_nvjpg.h"
  17. #include "core/hle/service/nvdrv/devices/nvhost_vic.h"
  18. #include "core/hle/service/nvdrv/devices/nvmap.h"
  19. #include "core/hle/service/nvdrv/interface.h"
  20. #include "core/hle/service/nvdrv/nvdrv.h"
  21. #include "core/hle/service/nvdrv/nvmemp.h"
  22. #include "core/hle/service/nvflinger/nvflinger.h"
  23. namespace Service::Nvidia {
  24. void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
  25. Core::System& system) {
  26. auto module_ = std::make_shared<Module>(system);
  27. std::make_shared<NVDRV>(module_, "nvdrv")->InstallAsService(service_manager);
  28. std::make_shared<NVDRV>(module_, "nvdrv:a")->InstallAsService(service_manager);
  29. std::make_shared<NVDRV>(module_, "nvdrv:s")->InstallAsService(service_manager);
  30. std::make_shared<NVDRV>(module_, "nvdrv:t")->InstallAsService(service_manager);
  31. std::make_shared<NVMEMP>()->InstallAsService(service_manager);
  32. nvflinger.SetNVDrvInstance(module_);
  33. }
  34. Module::Module(Core::System& system) {
  35. auto& kernel = system.Kernel();
  36. for (u32 i = 0; i < MaxNvEvents; i++) {
  37. std::string event_label = fmt::format("NVDRV::NvEvent_{}", i);
  38. events_interface.events[i] =
  39. Kernel::WritableEvent::CreateEventPair(kernel, Kernel::ResetType::Manual, event_label);
  40. events_interface.status[i] = EventState::Free;
  41. events_interface.registered[i] = false;
  42. }
  43. auto nvmap_dev = std::make_shared<Devices::nvmap>(system);
  44. devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev);
  45. devices["/dev/nvhost-gpu"] = std::make_shared<Devices::nvhost_gpu>(system, nvmap_dev);
  46. devices["/dev/nvhost-ctrl-gpu"] = std::make_shared<Devices::nvhost_ctrl_gpu>(system);
  47. devices["/dev/nvmap"] = nvmap_dev;
  48. devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, nvmap_dev);
  49. devices["/dev/nvhost-ctrl"] = std::make_shared<Devices::nvhost_ctrl>(system, events_interface);
  50. devices["/dev/nvhost-nvdec"] = std::make_shared<Devices::nvhost_nvdec>(system);
  51. devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
  52. devices["/dev/nvhost-vic"] = std::make_shared<Devices::nvhost_vic>(system);
  53. }
  54. Module::~Module() = default;
  55. u32 Module::Open(const std::string& device_name) {
  56. ASSERT_MSG(devices.find(device_name) != devices.end(), "Trying to open unknown device {}",
  57. device_name);
  58. auto device = devices[device_name];
  59. const u32 fd = next_fd++;
  60. open_files[fd] = std::move(device);
  61. return fd;
  62. }
  63. u32 Module::Ioctl(u32 fd, u32 command, const std::vector<u8>& input, const std::vector<u8>& input2,
  64. std::vector<u8>& output, std::vector<u8>& output2, IoctlCtrl& ctrl,
  65. IoctlVersion version) {
  66. auto itr = open_files.find(fd);
  67. ASSERT_MSG(itr != open_files.end(), "Tried to talk to an invalid device");
  68. auto& device = itr->second;
  69. return device->ioctl({command}, input, input2, output, output2, ctrl, version);
  70. }
  71. ResultCode Module::Close(u32 fd) {
  72. auto itr = open_files.find(fd);
  73. ASSERT_MSG(itr != open_files.end(), "Tried to talk to an invalid device");
  74. open_files.erase(itr);
  75. // TODO(flerovium): return correct result code if operation failed.
  76. return RESULT_SUCCESS;
  77. }
  78. void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
  79. for (u32 i = 0; i < MaxNvEvents; i++) {
  80. if (events_interface.assigned_syncpt[i] == syncpoint_id &&
  81. events_interface.assigned_value[i] == value) {
  82. events_interface.LiberateEvent(i);
  83. events_interface.events[i].writable->Signal();
  84. }
  85. }
  86. }
  87. Kernel::SharedPtr<Kernel::ReadableEvent> Module::GetEvent(const u32 event_id) const {
  88. return events_interface.events[event_id].readable;
  89. }
  90. Kernel::SharedPtr<Kernel::WritableEvent> Module::GetEventWriteable(const u32 event_id) const {
  91. return events_interface.events[event_id].writable;
  92. }
  93. } // namespace Service::Nvidia