yuzu/src/core/hle/service/nvdrv/nvdrv.cpp

227 lines
8.0 KiB
C++
Raw Normal View History

2022-07-02 01:36:29 +04:00
// SPDX-FileCopyrightText: 2021 yuzu Emulator Project
// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
2020-12-28 19:15:37 +04:00
#include <utility>
#include <fmt/format.h>
#include "core/core.h"
#include "core/hle/ipc_helpers.h"
2021-02-04 03:41:27 +04:00
#include "core/hle/kernel/k_event.h"
2022-06-16 05:46:18 +04:00
#include "core/hle/service/nvdrv/core/container.h"
2020-12-28 19:15:37 +04: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"
#include "core/hle/service/nvdrv/devices/nvhost_ctrl.h"
#include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
#include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
#include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
2022-06-16 05:46:18 +04:00
#include "core/hle/service/nvdrv/devices/nvhost_nvdec_common.h"
2020-12-28 19:15:37 +04:00
#include "core/hle/service/nvdrv/devices/nvhost_nvjpg.h"
#include "core/hle/service/nvdrv/devices/nvhost_vic.h"
#include "core/hle/service/nvdrv/devices/nvmap.h"
#include "core/hle/service/nvdrv/nvdrv.h"
2021-07-15 07:24:54 +04:00
#include "core/hle/service/nvdrv/nvdrv_interface.h"
2020-12-28 19:15:37 +04:00
#include "core/hle/service/nvdrv/nvmemp.h"
#include "core/hle/service/nvflinger/nvflinger.h"
2022-06-16 05:46:18 +04:00
#include "video_core/gpu.h"
2020-12-28 19:15:37 +04:00
namespace Service::Nvidia {
2022-06-16 05:46:18 +04:00
EventInterface::EventInterface(Module& module_) : module{module_}, guard{}, on_signal{} {}
EventInterface::~EventInterface() = default;
Kernel::KEvent* EventInterface::CreateEvent(std::string name) {
Kernel::KEvent* new_event = module.service_context.CreateEvent(std::move(name));
return new_event;
}
void EventInterface::FreeEvent(Kernel::KEvent* event) {
module.service_context.CloseEvent(event);
}
2020-12-28 19:15:37 +04:00
void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
Core::System& system) {
auto module_ = std::make_shared<Module>(system);
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);
nvflinger.SetNVDrvInstance(module_);
}
2021-07-03 13:00:06 +04:00
Module::Module(Core::System& system)
2022-10-24 06:19:27 +04:00
: container{system.Host1x()}, service_context{system, "nvdrv"}, events_interface{*this} {
2022-06-16 05:46:18 +04:00
builders["/dev/nvhost-as-gpu"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvhost_as_gpu>(system, *this, container);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvhost-gpu"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvhost_gpu>(system, events_interface, container);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvhost-ctrl-gpu"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvhost_ctrl_gpu>(system, events_interface);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvmap"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvmap>(system, container);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvdisp_disp0"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvdisp_disp0>(system, container);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvhost-ctrl"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvhost_ctrl>(system, events_interface, container);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvhost-nvdec"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvhost_nvdec>(system, container);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvhost-nvjpg"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device = std::make_shared<Devices::nvhost_nvjpg>(system);
return open_files.emplace(fd, device).first;
};
builders["/dev/nvhost-vic"] = [this, &system](DeviceFD fd) {
std::shared_ptr<Devices::nvdevice> device =
std::make_shared<Devices::nvhost_vic>(system, container);
return open_files.emplace(fd, device).first;
};
2020-12-28 19:15:37 +04:00
}
2022-09-01 11:37:35 +04:00
Module::~Module() {}
2020-12-28 19:15:37 +04:00
NvResult Module::VerifyFD(DeviceFD fd) const {
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) {
2022-06-16 05:46:18 +04:00
auto it = builders.find(device_name);
if (it == builders.end()) {
2020-12-28 19:15:37 +04:00
LOG_ERROR(Service_NVDRV, "Trying to open unknown device {}", device_name);
return INVALID_NVDRV_FD;
}
const DeviceFD fd = next_fd++;
2022-06-16 05:46:18 +04:00
auto& builder = it->second;
auto device = builder(fd)->second;
2020-12-28 19:15:37 +04:00
2021-03-27 09:41:28 +04:00
device->OnOpen(fd);
2020-12-28 19:15:37 +04:00
return fd;
}
NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
2020-12-30 05:38:14 +04:00
std::vector<u8>& output) {
2020-12-28 19:15:37 +04:00
if (fd < 0) {
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
return NvResult::InvalidState;
}
const auto itr = open_files.find(fd);
if (itr == open_files.end()) {
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
return NvResult::NotImplemented;
}
2021-03-27 09:41:28 +04:00
return itr->second->Ioctl1(fd, command, input, output);
2020-12-28 19:15:37 +04:00
}
NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
2020-12-30 05:38:14 +04:00
const std::vector<u8>& inline_input, std::vector<u8>& output) {
2020-12-28 19:15:37 +04:00
if (fd < 0) {
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
return NvResult::InvalidState;
}
const auto itr = open_files.find(fd);
if (itr == open_files.end()) {
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
return NvResult::NotImplemented;
}
2021-03-27 09:41:28 +04:00
return itr->second->Ioctl2(fd, command, input, inline_input, output);
2020-12-28 19:15:37 +04:00
}
NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
2020-12-30 05:38:14 +04:00
std::vector<u8>& output, std::vector<u8>& inline_output) {
2020-12-28 19:15:37 +04:00
if (fd < 0) {
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
return NvResult::InvalidState;
}
const auto itr = open_files.find(fd);
if (itr == open_files.end()) {
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
return NvResult::NotImplemented;
}
2021-03-27 09:41:28 +04:00
return itr->second->Ioctl3(fd, command, input, output, inline_output);
2020-12-28 19:15:37 +04:00
}
NvResult Module::Close(DeviceFD fd) {
if (fd < 0) {
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
return NvResult::InvalidState;
}
const auto itr = open_files.find(fd);
if (itr == open_files.end()) {
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
return NvResult::NotImplemented;
}
2021-03-27 09:41:28 +04:00
itr->second->OnClose(fd);
2020-12-28 19:15:37 +04:00
open_files.erase(itr);
return NvResult::Success;
}
2022-06-16 05:46:18 +04:00
NvResult Module::QueryEvent(DeviceFD fd, u32 event_id, Kernel::KEvent*& event) {
if (fd < 0) {
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
return NvResult::InvalidState;
2020-12-28 19:15:37 +04:00
}
2022-06-16 05:46:18 +04:00
const auto itr = open_files.find(fd);
2020-12-28 19:15:37 +04:00
2022-06-16 05:46:18 +04:00
if (itr == open_files.end()) {
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
return NvResult::NotImplemented;
}
event = itr->second->QueryEvent(event_id);
if (!event) {
return NvResult::BadParameter;
}
return NvResult::Success;
2020-12-28 19:15:37 +04:00
}
} // namespace Service::Nvidia