nvdrv.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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] = Kernel::WritableEvent::CreateEventPair(kernel, event_label);
  39. events_interface.status[i] = EventState::Free;
  40. events_interface.registered[i] = false;
  41. }
  42. auto nvmap_dev = std::make_shared<Devices::nvmap>(system);
  43. devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev);
  44. devices["/dev/nvhost-gpu"] = std::make_shared<Devices::nvhost_gpu>(system, nvmap_dev);
  45. devices["/dev/nvhost-ctrl-gpu"] = std::make_shared<Devices::nvhost_ctrl_gpu>(system);
  46. devices["/dev/nvmap"] = nvmap_dev;
  47. devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, nvmap_dev);
  48. devices["/dev/nvhost-ctrl"] = std::make_shared<Devices::nvhost_ctrl>(system, events_interface);
  49. devices["/dev/nvhost-nvdec"] = std::make_shared<Devices::nvhost_nvdec>(system);
  50. devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
  51. devices["/dev/nvhost-vic"] = std::make_shared<Devices::nvhost_vic>(system);
  52. }
  53. Module::~Module() = default;
  54. u32 Module::Open(const std::string& device_name) {
  55. ASSERT_MSG(devices.find(device_name) != devices.end(), "Trying to open unknown device {}",
  56. device_name);
  57. auto device = devices[device_name];
  58. const u32 fd = next_fd++;
  59. open_files[fd] = std::move(device);
  60. return fd;
  61. }
  62. u32 Module::Ioctl(u32 fd, u32 command, const std::vector<u8>& input, const std::vector<u8>& input2,
  63. std::vector<u8>& output, std::vector<u8>& output2, IoctlCtrl& ctrl,
  64. IoctlVersion version) {
  65. auto itr = open_files.find(fd);
  66. ASSERT_MSG(itr != open_files.end(), "Tried to talk to an invalid device");
  67. auto& device = itr->second;
  68. return device->ioctl({command}, input, input2, output, output2, ctrl, version);
  69. }
  70. ResultCode Module::Close(u32 fd) {
  71. auto itr = open_files.find(fd);
  72. ASSERT_MSG(itr != open_files.end(), "Tried to talk to an invalid device");
  73. open_files.erase(itr);
  74. // TODO(flerovium): return correct result code if operation failed.
  75. return RESULT_SUCCESS;
  76. }
  77. void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
  78. for (u32 i = 0; i < MaxNvEvents; i++) {
  79. if (events_interface.assigned_syncpt[i] == syncpoint_id &&
  80. events_interface.assigned_value[i] == value) {
  81. events_interface.LiberateEvent(i);
  82. events_interface.events[i].writable->Signal();
  83. }
  84. }
  85. }
  86. std::shared_ptr<Kernel::ReadableEvent> Module::GetEvent(const u32 event_id) const {
  87. return events_interface.events[event_id].readable;
  88. }
  89. std::shared_ptr<Kernel::WritableEvent> Module::GetEventWriteable(const u32 event_id) const {
  90. return events_interface.events[event_id].writable;
  91. }
  92. } // namespace Service::Nvidia