2018-01-13 22:22:39 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2018-01-08 03:25:57 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-25 23:29:49 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2019-06-08 00:41:55 +02:00
|
|
|
#include <fmt/format.h>
|
2020-11-08 21:49:45 +01:00
|
|
|
#include "core/core.h"
|
2018-01-16 00:30:49 +01:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2021-01-31 10:38:57 +01:00
|
|
|
#include "core/hle/kernel/k_event.h"
|
2021-01-30 07:48:06 +01:00
|
|
|
#include "core/hle/kernel/k_readable_event.h"
|
2021-01-30 08:51:40 +01:00
|
|
|
#include "core/hle/kernel/k_writable_event.h"
|
2018-01-15 23:39:00 +01: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 23:59:50 +01:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_ctrl.h"
|
Extra nvdrv support (#162)
* FinishInitalize needed for 3.0.1+ games
* nvdrv:s and nvdrv:t both use NVDRV
* Most settings return 0 on hardware, disabled NV_MEMORY_PROFILER for now.
NVN_THROUGH_OPENGL & NVRM_GPU_PREVENT_USE are a few interesting settings to look at. Carefully choosing settings can help with drawing graphics later on
* Initial /dev/nvhost-gpu support
* ZCullBind
* Stubbed SetErrorNotifier
* Fixed SetErrorNotifier log, Added SetChannelPriority
* Allocate GPFIFO Ex2, Allocate Obj Ctx, Submit GPFIFO
* oops
* Fixed up naming/structs/enums. Used vector instead of array for "gpfifo_entry"
* Added missing fixes
* /dev/nvhost-ctrl-gpu
* unneeded struct
* Forgot u32 in enum class
* Automatic descriptor swapping for ioctls, fixed nvgpu_gpu_get_tpc_masks_args being incorrect size
* nvdrv#QueryEvent
* Renamed logs for nvdrv
* Refactor ioctl so nv_result isn't needed
* /dev/nvhost-as-gpu
* Fixed Log service naming, CtxObjects now u32, renamed all structs, added static_asserts to structs, used INSERT_PADDING_WORDS instead of u32s
* nvdevices now uses "Ioctl" union,
* IoctlGpfifoEntry now uses bit field
* final changes
2018-02-06 03:19:31 +01:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
|
2018-05-30 11:49:28 +02:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
|
2018-08-13 06:03:50 +02:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_nvjpg.h"
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_vic.h"
|
2018-01-15 23:39:00 +01:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvmap.h"
|
2018-01-16 00:30:49 +01:00
|
|
|
#include "core/hle/service/nvdrv/interface.h"
|
2018-01-20 08:48:02 +01:00
|
|
|
#include "core/hle/service/nvdrv/nvdrv.h"
|
2018-01-21 23:59:50 +01:00
|
|
|
#include "core/hle/service/nvdrv/nvmemp.h"
|
2020-10-27 05:58:59 +01:00
|
|
|
#include "core/hle/service/nvdrv/syncpoint_manager.h"
|
2018-08-07 15:17:09 +02:00
|
|
|
#include "core/hle/service/nvflinger/nvflinger.h"
|
2018-01-08 03:25:57 +01:00
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
namespace Service::Nvidia {
|
2018-01-08 03:25:57 +01:00
|
|
|
|
2019-06-10 18:03:30 +02:00
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
|
|
|
|
Core::System& system) {
|
|
|
|
auto module_ = std::make_shared<Module>(system);
|
2020-11-26 21:19:08 +01: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 15:17:09 +02:00
|
|
|
nvflinger.SetNVDrvInstance(module_);
|
2018-01-08 03:25:57 +01:00
|
|
|
}
|
|
|
|
|
2020-10-27 05:45:08 +01:00
|
|
|
Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} {
|
2019-06-10 18:03:30 +02:00
|
|
|
auto& kernel = system.Kernel();
|
2019-06-08 00:41:55 +02:00
|
|
|
for (u32 i = 0; i < MaxNvEvents; i++) {
|
2021-04-10 07:42:23 +02:00
|
|
|
events_interface.events[i].event = Kernel::KEvent::Create(kernel);
|
2021-04-04 09:56:09 +02:00
|
|
|
events_interface.events[i].event->Initialize(fmt::format("NVDRV::NvEvent_{}", i));
|
2019-06-08 00:41:55 +02:00
|
|
|
events_interface.status[i] = EventState::Free;
|
|
|
|
events_interface.registered[i] = false;
|
|
|
|
}
|
2019-06-10 18:03:30 +02: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-30 05:16:24 +01:00
|
|
|
devices["/dev/nvhost-gpu"] =
|
|
|
|
std::make_shared<Devices::nvhost_gpu>(system, nvmap_dev, syncpoint_manager);
|
2019-06-10 18:03:30 +02:00
|
|
|
devices["/dev/nvhost-ctrl-gpu"] = std::make_shared<Devices::nvhost_ctrl_gpu>(system);
|
2018-01-15 23:39:00 +01:00
|
|
|
devices["/dev/nvmap"] = nvmap_dev;
|
2019-06-10 18:03:30 +02:00
|
|
|
devices["/dev/nvdisp_disp0"] = std::make_shared<Devices::nvdisp_disp0>(system, nvmap_dev);
|
2020-10-27 06:04:13 +01:00
|
|
|
devices["/dev/nvhost-ctrl"] =
|
|
|
|
std::make_shared<Devices::nvhost_ctrl>(system, events_interface, syncpoint_manager);
|
2020-12-28 07:02:06 +01:00
|
|
|
devices["/dev/nvhost-nvdec"] =
|
|
|
|
std::make_shared<Devices::nvhost_nvdec>(system, nvmap_dev, syncpoint_manager);
|
2019-06-10 18:03:30 +02:00
|
|
|
devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
|
2020-12-28 07:02:06 +01:00
|
|
|
devices["/dev/nvhost-vic"] =
|
|
|
|
std::make_shared<Devices::nvhost_vic>(system, nvmap_dev, syncpoint_manager);
|
2018-01-15 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2021-04-04 09:56:09 +02:00
|
|
|
Module::~Module() {
|
|
|
|
for (u32 i = 0; i < MaxNvEvents; i++) {
|
|
|
|
events_interface.events[i].event->Close();
|
|
|
|
events_interface.events[i].event = nullptr;
|
|
|
|
}
|
|
|
|
}
|
hle/service: Default constructors and destructors in the cpp file where applicable
When a destructor isn't defaulted into a cpp file, it can cause the use
of forward declarations to seemingly fail to compile for non-obvious
reasons. It also allows inlining of the construction/destruction logic
all over the place where a constructor or destructor is invoked, which
can lead to code bloat. This isn't so much a worry here, given the
services won't be created and destroyed frequently.
The cause of the above mentioned non-obvious errors can be demonstrated
as follows:
------- Demonstrative example, if you know how the described error happens, skip forwards -------
Assume we have the following in the header, which we'll call "thing.h":
\#include <memory>
// Forward declaration. For example purposes, assume the definition
// of Object is in some header named "object.h"
class Object;
class Thing {
public:
// assume no constructors or destructors are specified here,
// or the constructors/destructors are defined as:
//
// Thing() = default;
// ~Thing() = default;
//
// ... Some interface member functions would be defined here
private:
std::shared_ptr<Object> obj;
};
If this header is included in a cpp file, (which we'll call "main.cpp"),
this will result in a compilation error, because even though no
destructor is specified, the destructor will still need to be generated by
the compiler because std::shared_ptr's destructor is *not* trivial (in
other words, it does something other than nothing), as std::shared_ptr's
destructor needs to do two things:
1. Decrement the shared reference count of the object being pointed to,
and if the reference count decrements to zero,
2. Free the Object instance's memory (aka deallocate the memory it's
pointing to).
And so the compiler generates the code for the destructor doing this inside main.cpp.
Now, keep in mind, the Object forward declaration is not a complete type. All it
does is tell the compiler "a type named Object exists" and allows us to
use the name in certain situations to avoid a header dependency. So the
compiler needs to generate destruction code for Object, but the compiler
doesn't know *how* to destruct it. A forward declaration doesn't tell
the compiler anything about Object's constructor or destructor. So, the
compiler will issue an error in this case because it's undefined
behavior to try and deallocate (or construct) an incomplete type and
std::shared_ptr and std::unique_ptr make sure this isn't the case
internally.
Now, if we had defaulted the destructor in "thing.cpp", where we also
include "object.h", this would never be an issue, as the destructor
would only have its code generated in one place, and it would be in a
place where the full class definition of Object would be visible to the
compiler.
---------------------- End example ----------------------------
Given these service classes are more than certainly going to change in
the future, this defaults the constructors and destructors into the
relevant cpp files to make the construction and destruction of all of
the services consistent and unlikely to run into cases where forward
declarations are indirectly causing compilation errors. It also has the
plus of avoiding the need to rebuild several services if destruction
logic changes, since it would only be necessary to recompile the single
cpp file.
2018-09-11 03:20:52 +02:00
|
|
|
|
2020-11-10 05:56:41 +01:00
|
|
|
NvResult Module::VerifyFD(DeviceFD fd) const {
|
2020-11-08 09:11:34 +01: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 23:39:00 +01:00
|
|
|
|
|
|
|
auto device = devices[device_name];
|
2020-11-08 09:11:34 +01:00
|
|
|
const DeviceFD fd = next_fd++;
|
2018-01-15 23:39:00 +01:00
|
|
|
|
2021-03-25 02:56:42 +01:00
|
|
|
device->OnOpen(fd);
|
|
|
|
|
2018-07-25 23:29:49 +02:00
|
|
|
open_files[fd] = std::move(device);
|
2018-01-15 23:39:00 +01:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-11-08 09:11:34 +01:00
|
|
|
NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-12-12 01:04:46 +01:00
|
|
|
std::vector<u8>& output) {
|
2020-11-08 09:11:34 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:56:41 +01:00
|
|
|
const auto itr = open_files.find(fd);
|
2018-01-15 23:39:00 +01:00
|
|
|
|
2020-11-08 09:11:34 +01:00
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
|
|
|
|
2021-03-25 02:56:42 +01:00
|
|
|
return itr->second->Ioctl1(fd, command, input, output);
|
2018-01-15 23:39:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 09:11:34 +01:00
|
|
|
NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-12-12 01:04:46 +01:00
|
|
|
const std::vector<u8>& inline_input, std::vector<u8>& output) {
|
2020-11-08 09:11:34 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:56:41 +01:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 09:11:34 +01:00
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
|
|
|
|
2021-03-25 02:56:42 +01:00
|
|
|
return itr->second->Ioctl2(fd, command, input, inline_input, output);
|
2020-11-08 09:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-12-12 01:04:46 +01:00
|
|
|
std::vector<u8>& output, std::vector<u8>& inline_output) {
|
2020-11-08 09:11:34 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:56:41 +01:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 09:11:34 +01:00
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
|
|
|
|
2021-03-25 02:56:42 +01:00
|
|
|
return itr->second->Ioctl3(fd, command, input, output, inline_output);
|
2020-11-08 09:11:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NvResult Module::Close(DeviceFD fd) {
|
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-10 05:56:41 +01:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 09:11:34 +01: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 02:56:42 +01:00
|
|
|
itr->second->OnClose(fd);
|
|
|
|
|
2018-01-17 17:08:46 +01:00
|
|
|
open_files.erase(itr);
|
|
|
|
|
2020-11-08 09:11:34 +01:00
|
|
|
return NvResult::Success;
|
2018-01-17 17:08:46 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 13:52:49 +02: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-04-10 11:34:26 +02:00
|
|
|
events_interface.events[i].event->GetWritableEvent().Signal();
|
2019-06-12 13:52:49 +02:00
|
|
|
}
|
2019-06-08 00:41:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:34:26 +02:00
|
|
|
Kernel::KReadableEvent& Module::GetEvent(const u32 event_id) {
|
|
|
|
return events_interface.events[event_id].event->GetReadableEvent();
|
2019-06-08 00:41:55 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 11:34:26 +02:00
|
|
|
Kernel::KWritableEvent& Module::GetEventWriteable(const u32 event_id) {
|
2021-01-31 10:38:57 +01:00
|
|
|
return events_interface.events[event_id].event->GetWritableEvent();
|
2019-06-16 17:43:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
} // namespace Service::Nvidia
|