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"
|
2019-06-07 18:41:55 -04:00
|
|
|
#include "core/hle/kernel/readable_event.h"
|
|
|
|
#include "core/hle/kernel/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 17:59:50 -05: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-05 21:19:31 -05:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_ctrl_gpu.h"
|
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_gpu.h"
|
2018-05-30 05:49:28 -04:00
|
|
|
#include "core/hle/service/nvdrv/devices/nvhost_nvdec.h"
|
2018-08-13 00:03:50 -04: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 02:48:02 -05:00
|
|
|
#include "core/hle/service/nvdrv/nvdrv.h"
|
2018-01-21 17:59:50 -05:00
|
|
|
#include "core/hle/service/nvdrv/nvmemp.h"
|
2020-10-27 00:58:59 -04: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-27 00:45:08 -04: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);
|
2020-10-27 00:58:59 -04:00
|
|
|
events_interface.events[i] = {Kernel::WritableEvent::CreateEventPair(kernel, event_label)};
|
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-30 00:16:24 -04: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-27 01:04:13 -04:00
|
|
|
devices["/dev/nvhost-ctrl"] =
|
|
|
|
std::make_shared<Devices::nvhost_ctrl>(system, events_interface, syncpoint_manager);
|
2020-10-26 23:07:36 -04:00
|
|
|
devices["/dev/nvhost-nvdec"] = std::make_shared<Devices::nvhost_nvdec>(system, nvmap_dev);
|
2019-06-10 12:03:30 -04:00
|
|
|
devices["/dev/nvhost-nvjpg"] = std::make_shared<Devices::nvhost_nvjpg>(system);
|
2020-10-26 23:07:36 -04:00
|
|
|
devices["/dev/nvhost-vic"] = std::make_shared<Devices::nvhost_vic>(system, nvmap_dev);
|
2018-01-15 17:39:00 -05:00
|
|
|
}
|
|
|
|
|
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-10 21:20:52 -04:00
|
|
|
Module::~Module() = default;
|
|
|
|
|
2020-11-09 23:56:41 -05:00
|
|
|
NvResult Module::VerifyFD(DeviceFD fd) const {
|
2020-11-08 03:11:34 -05: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 03:11:34 -05:00
|
|
|
const DeviceFD fd = next_fd++;
|
2018-01-15 17:39:00 -05:00
|
|
|
|
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 03:11:34 -05:00
|
|
|
NvResult Module::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-11-24 00:40:23 -05:00
|
|
|
std::vector<u8>& output, IoctlCtrl& ctrl) {
|
2020-11-08 03:11:34 -05:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:56:41 -05:00
|
|
|
const auto itr = open_files.find(fd);
|
2018-01-15 17:39:00 -05:00
|
|
|
|
2020-11-08 03:11:34 -05:00
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:40:23 -05:00
|
|
|
return itr->second->Ioctl1(command, input, output, ctrl);
|
2018-01-15 17:39:00 -05:00
|
|
|
}
|
|
|
|
|
2020-11-08 03:11:34 -05:00
|
|
|
NvResult Module::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-11-24 00:40:23 -05:00
|
|
|
const std::vector<u8>& inline_input, std::vector<u8>& output,
|
|
|
|
IoctlCtrl& ctrl) {
|
2020-11-08 03:11:34 -05:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:56:41 -05:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 03:11:34 -05:00
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:40:23 -05:00
|
|
|
return itr->second->Ioctl2(command, input, inline_input, output, ctrl);
|
2020-11-08 03:11:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
NvResult Module::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
|
2020-11-24 00:40:23 -05:00
|
|
|
std::vector<u8>& output, std::vector<u8>& inline_output, IoctlCtrl& ctrl) {
|
2020-11-08 03:11:34 -05:00
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:56:41 -05:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 03:11:34 -05:00
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:40:23 -05:00
|
|
|
return itr->second->Ioctl3(command, input, output, inline_output, ctrl);
|
2020-11-08 03:11:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
NvResult Module::Close(DeviceFD fd) {
|
|
|
|
if (fd < 0) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Invalid DeviceFD={}!", fd);
|
|
|
|
return NvResult::InvalidState;
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:56:41 -05:00
|
|
|
const auto itr = open_files.find(fd);
|
2020-11-08 03:11:34 -05:00
|
|
|
|
|
|
|
if (itr == open_files.end()) {
|
|
|
|
LOG_ERROR(Service_NVDRV, "Could not find DeviceFD={}!", fd);
|
|
|
|
return NvResult::NotImplemented;
|
|
|
|
}
|
2018-01-17 11:08:46 -05:00
|
|
|
|
|
|
|
open_files.erase(itr);
|
|
|
|
|
2020-11-08 03:11:34 -05:00
|
|
|
return NvResult::Success;
|
2018-01-17 11:08:46 -05: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);
|
2020-10-27 00:58:59 -04:00
|
|
|
events_interface.events[i].event.writable->Signal();
|
2019-06-12 07:52:49 -04:00
|
|
|
}
|
2019-06-07 18:41:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<Kernel::ReadableEvent> Module::GetEvent(const u32 event_id) const {
|
2020-10-27 00:58:59 -04:00
|
|
|
return events_interface.events[event_id].event.readable;
|
2019-06-07 18:41:55 -04:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:15:51 -05:00
|
|
|
std::shared_ptr<Kernel::WritableEvent> Module::GetEventWriteable(const u32 event_id) const {
|
2020-10-27 00:58:59 -04:00
|
|
|
return events_interface.events[event_id].event.writable;
|
2019-06-16 11:43:41 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::Nvidia
|