2018-01-13 16:22:39 -05:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2018-01-07 21:25:57 -05:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
2018-07-25 17:29:49 -04:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-06-07 18:41:55 -04:00
|
|
|
#include <fmt/format.h>
|
2020-11-08 15:49:45 -05:00
|
|
|
#include "core/core.h"
|
2018-01-15 18:30:49 -05:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2021-01-31 01:38:57 -08:00
|
|
|
#include "core/hle/kernel/k_event.h"
|
2021-01-29 22:48:06 -08:00
|
|
|
#include "core/hle/kernel/k_readable_event.h"
|
2021-01-29 23:51:40 -08:00
|
|
|
#include "core/hle/kernel/k_writable_event.h"
|
2018-01-15 17:39:00 -05:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvdevice.h"
|
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
|
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_as_gpu.h"
|
2018-01-21 14:59:50 -08:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_ctrl.h"
|
2018-02-05 18:19:31 -08:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
|
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
|
2018-05-30 12:49:28 +03:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
|
2018-08-13 14:03:50 +10:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_nvjpg.h"
|
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_vic.h"
|
2018-01-15 17:39:00 -05:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvmap.h"
|
2018-01-15 18:30:49 -05:00
|
|
|
#include "core/hle/service/nvdrv/interface.h"
|
2018-01-20 00:48:02 -07:00
|
|
|
#include "core/hle/service/nvdrv/nvdrv.h"
|
2018-01-21 14:59:50 -08:00
|
|
|
#include "core/hle/service/nvdrv/nvmemp.h"
|
2020-10-26 21:58:59 -07:00
|
|
|
#include "core/hle/service/nvdrv/syncpoint_manager.h"
|
2018-08-07 09:17:09 -04:00
|
|
|
#include "core/hle/service/nvflinger/nvflinger.h"
|
2018-01-07 21:25:57 -05:00
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
namespace Service::Nvidia {
|
2018-01-07 21:25:57 -05:00
|
|
|
|
2019-06-10 12:03:30 -04:00
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
|
|
|
|
|
Core::System& system) {
|
|
|
|
|
auto module_ = std::make_shared<Module>(system);
|
2020-11-26 15:19:08 -05:00
|
|
|
std::make_shared<NVDRV>(system, module_, "nvdrv")->InstallAsService(service_manager);
|
|
|
|
|
std::make_shared<NVDRV>(system, module_, "nvdrv:a")->InstallAsService(service_manager);
|
|
|
|
|
std::make_shared<NVDRV>(system, module_, "nvdrv:s")->InstallAsService(service_manager);
|
|
|
|
|
std::make_shared<NVDRV>(system, module_, "nvdrv:t")->InstallAsService(service_manager);
|
|
|
|
|
std::make_shared<NVMEMP>(system)->InstallAsService(service_manager);
|
2018-08-07 09:17:09 -04:00
|
|
|
nvflinger.SetNVDrvInstance(module_);
|
2018-01-07 21:25:57 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-26 21:45:08 -07:00
|
|
|
Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} {
|
2019-06-10 12:03:30 -04:00
|
|
|
auto& kernel = system.Kernel();
|
2019-06-07 18:41:55 -04:00
|
|
|
for (u32 i = 0; i < MaxNvEvents; i++) {
|
|
|
|
|
std::string event_label = fmt::format("NVDRV::NvEvent_{}", i);
|
2021-01-31 01:38:57 -08:00
|
|
|
events_interface.events[i] = {Kernel::KEvent::Create(kernel, std::move(event_label))};
|
|
|
|
|
events_interface.events[i].event->Initialize();
|
2019-06-07 18:41:55 -04:00
|
|
|
events_interface.status[i] = EventState::Free;
|
|
|
|
|
events_interface.registered[i] = false;
|
|
|
|
|
}
|
2019-06-10 12:03:30 -04:00
|
|
|
auto nvmap_dev = std::make_shared<Devices::nvmap>(system);
|
|
|
|
|
devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev);
|
2020-10-29 21:16:24 -07:00
|
|
|
devices["/dev/nvhost-gpu"] =
|
|
|
|
|
std::make_shared<Devices::nvhost_gpu>(system, nvmap_dev, syncpoint_manager);
|
2019-06-10 12:03:30 -04:00
|
|
|
devices["/dev/nvhost-ctrl-gpu"] = std::make_shared<Devices::nvhost_ctrl_gpu>(system);
|
2018-01-15 17:39:00 -05:00
|
|
|
devices["/dev/nvmap"] = nvmap_dev;
|
2019-06-10 12:03:30 -04:00
|
|
|
devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, nvmap_dev);
|
2020-10-26 22:04:13 -07:00
|
|
|
devices["/dev/nvhost-ctrl"] =
|
|
|
|
|
std::make_shared<Devices::nvhost_ctrl>(system, events_interface, syncpoint_manager);
|
2020-12-28 01:02:06 -05:00
|
|
|
devices["/dev/nvhost-nvdec"] =
|
|
|
|
|
std::make_shared<Devices::nvhost_nvdec>(system, nvmap_dev, syncpoint_manager);
|
2019-06-10 12:03:30 -04:00
|
|
|
devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
|
2020-12-28 01:02:06 -05:00
|
|
|
devices["/dev/nvhost-vic"] =
|
|
|
|
|
std::make_shared<Devices::nvhost_vic>(system, nvmap_dev, syncpoint_manager);
|
2018-01-15 17:39:00 -05:00
|
|
|
}
|
|
|
|
|
|
2018-09-10 21:20:52 -04:00
|
|
|
Module::~Module() = default;
|
|
|
|
|
|
2020-11-10 15:56:41 +11:00
|
|
|
NvResult Module::VerifyFD(DeviceFD fd) const {
|
2020-11-08 19:11:34 +11:00
|
|
|
if (fd < 0) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (open_files.find(fd) == open_files.end()) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NvResult::Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeviceFD Module::Open(const std::string& device_name) {
|
|
|
|
|
if (devices.find(device_name) == devices.end()) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Trying to open unknown device {}", device_name);
|
|
|
|
|
return INVALID_NVDRV_FD;
|
|
|
|
|
}
|
2018-01-15 17:39:00 -05:00
|
|
|
|
|
|
|
|
auto device = devices[device_name];
|
2020-11-08 19:11:34 +11:00
|
|
|
const DeviceFD fd = next_fd++;
|
2018-01-15 17:39:00 -05:00
|
|
|
|
2021-03-25 12:56:42 +11:00
|
|
|
device->OnOpen(fd);
|
|
|
|
|
|
2018-07-25 17:29:49 -04:00
|
|
|
open_files[fd] = std::move(device);
|
2018-01-15 17:39:00 -05:00
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 19:11:34 +11:00
|
|
|
NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-12-11 16:04:46 -08:00
|
|
|
std::vector<u8>& output) {
|
2020-11-08 19:11:34 +11:00
|
|
|
if (fd < 0) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:56:41 +11:00
|
|
|
const auto itr = open_files.find(fd);
|
2018-01-15 17:39:00 -05:00
|
|
|
|
2020-11-08 19:11:34 +11:00
|
|
|
if (itr == open_files.end()) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 12:56:42 +11:00
|
|
|
return itr->second->Ioctl1(fd, command, input, output);
|
2018-01-15 17:39:00 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-08 19:11:34 +11:00
|
|
|
NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-12-11 16:04:46 -08:00
|
|
|
const std::vector<u8>& inline_input, std::vector<u8>& output) {
|
2020-11-08 19:11:34 +11:00
|
|
|
if (fd < 0) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:56:41 +11:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 19:11:34 +11:00
|
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 12:56:42 +11:00
|
|
|
return itr->second->Ioctl2(fd, command, input, inline_input, output);
|
2020-11-08 19:11:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-12-11 16:04:46 -08:00
|
|
|
std::vector<u8>& output, std::vector<u8>& inline_output) {
|
2020-11-08 19:11:34 +11:00
|
|
|
if (fd < 0) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:56:41 +11:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 19:11:34 +11:00
|
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 12:56:42 +11:00
|
|
|
return itr->second->Ioctl3(fd, command, input, output, inline_output);
|
2020-11-08 19:11:34 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NvResult Module::Close(DeviceFD fd) {
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 15:56:41 +11:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 19:11:34 +11:00
|
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
|
}
|
2018-01-17 17:08:46 +01:00
|
|
|
|
2021-03-25 12:56:42 +11:00
|
|
|
itr->second->OnClose(fd);
|
|
|
|
|
|
2018-01-17 17:08:46 +01:00
|
|
|
open_files.erase(itr);
|
|
|
|
|
|
2020-11-08 19:11:34 +11:00
|
|
|
return NvResult::Success;
|
2018-01-17 17:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-12 07:52:49 -04:00
|
|
|
void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
|
|
|
|
|
for (u32 i = 0; i < MaxNvEvents; i++) {
|
|
|
|
|
if (events_interface.assigned_syncpt[i] == syncpoint_id &&
|
|
|
|
|
events_interface.assigned_value[i] == value) {
|
|
|
|
|
events_interface.LiberateEvent(i);
|
2021-01-31 01:38:57 -08:00
|
|
|
events_interface.events[i].event->GetWritableEvent()->Signal();
|
2019-06-12 07:52:49 -04:00
|
|
|
}
|
2019-06-07 18:41:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 22:48:06 -08:00
|
|
|
std::shared_ptr<Kernel::KReadableEvent> Module::GetEvent(const u32 event_id) const {
|
2021-01-31 01:38:57 -08:00
|
|
|
return events_interface.events[event_id].event->GetReadableEvent();
|
2019-06-07 18:41:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 23:51:40 -08:00
|
|
|
std::shared_ptr<Kernel::KWritableEvent> Module::GetEventWriteable(const u32 event_id) const {
|
2021-01-31 01:38:57 -08:00
|
|
|
return events_interface.events[event_id].event->GetWritableEvent();
|
2019-06-16 11:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::Nvidia
|