From e0027eba854b9cf097360e898457e164e6ae0b4d Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Fri, 7 Jun 2019 18:41:55 -0400 Subject: nv_services: Implement NvQueryEvent, NvCtrlEventWait, NvEventRegister, NvEventUnregister --- src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp | 91 +++++++++++++++++++--- src/core/hle/service/nvdrv/devices/nvhost_ctrl.h | 7 +- src/core/hle/service/nvdrv/interface.cpp | 17 +++- src/core/hle/service/nvdrv/interface.h | 2 + src/core/hle/service/nvdrv/nvdata.h | 14 +++- src/core/hle/service/nvdrv/nvdrv.cpp | 26 ++++++- src/core/hle/service/nvdrv/nvdrv.h | 52 +++++++++++++ 7 files changed, 192 insertions(+), 17 deletions(-) diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp index b39fb9ef9..ef6731a8f 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp @@ -7,11 +7,15 @@ #include "common/assert.h" #include "common/logging/log.h" +#include "core/core.h" +#include "core/hle/kernel/readable_event.h" +#include "core/hle/kernel/writable_event.h" #include "core/hle/service/nvdrv/devices/nvhost_ctrl.h" +#include "video_core/gpu.h" namespace Service::Nvidia::Devices { -nvhost_ctrl::nvhost_ctrl() = default; +nvhost_ctrl::nvhost_ctrl(EventsInterface& events_interface) : events_interface{events_interface} {} nvhost_ctrl::~nvhost_ctrl() = default; u32 nvhost_ctrl::ioctl(Ioctl command, const std::vector& input, std::vector& output) { @@ -27,6 +31,8 @@ u32 nvhost_ctrl::ioctl(Ioctl command, const std::vector& input, std::vector< return IocCtrlEventWait(input, output, true); case IoctlCommand::IocCtrlEventRegisterCommand: return IocCtrlEventRegister(input, output); + case IoctlCommand::IocCtrlEventUnregisterCommand: + return IocCtrlEventUnregister(input, output); } UNIMPLEMENTED_MSG("Unimplemented ioctl"); return 0; @@ -44,20 +50,85 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& bool is_async) { IocCtrlEventWaitParams params{}; std::memcpy(¶ms, input.data(), sizeof(params)); - LOG_WARNING(Service_NVDRV, - "(STUBBED) called, syncpt_id={}, threshold={}, timeout={}, is_async={}", - params.syncpt_id, params.threshold, params.timeout, is_async); + LOG_DEBUG(Service_NVDRV, "syncpt_id={}, threshold={}, timeout={}, is_async={}", + params.syncpt_id, params.threshold, params.timeout, is_async); - // TODO(Subv): Implement actual syncpt waiting. - params.value = 0; + if (params.syncpt_id >= MaxSyncPoints) { + return NvResult::BadParameter; + } + + auto& gpu = Core::System::GetInstance().GPU(); + u32 current_syncpoint_value = gpu.GetSyncpointValue(params.syncpt_id); + if (current_syncpoint_value >= params.threshold) { + params.value = current_syncpoint_value; + std::memcpy(output.data(), ¶ms, sizeof(params)); + return NvResult::Success; + } + + if (!is_async) { + params.value = 0; + } + + if (params.timeout == 0) { + std::memcpy(output.data(), ¶ms, sizeof(params)); + return NvResult::Timeout; + } + + u32 event_index; + if (is_async) { + event_index = params.value; + if (event_index >= 64) { + std::memcpy(output.data(), ¶ms, sizeof(params)); + return NvResult::BadParameter; + } + } else { + event_index = events_interface.GetFreeEvent(); + } + + EventState status = events_interface.status[event_index]; + if (event_index < MaxNvEvents || status == EventState::Free || + status == EventState::Registered) { + events_interface.SetEventStatus(event_index, EventState::Waiting); + events_interface.assigned_syncpt[event_index] = params.syncpt_id; + events_interface.assigned_value[event_index] = params.threshold; + if (is_async) { + params.value = params.syncpt_id << 4; + } else { + params.value = ((params.syncpt_id & 0xfff) << 16) | 0x10000000; + } + params.value |= event_index; + gpu.RegisterEvent(event_index, params.syncpt_id, params.threshold); + std::memcpy(output.data(), ¶ms, sizeof(params)); + return NvResult::Timeout; + } std::memcpy(output.data(), ¶ms, sizeof(params)); - return 0; + return NvResult::BadParameter; } u32 nvhost_ctrl::IocCtrlEventRegister(const std::vector& input, std::vector& output) { - LOG_WARNING(Service_NVDRV, "(STUBBED) called"); - // TODO(bunnei): Implement this. - return 0; + IocCtrlEventRegisterParams params{}; + std::memcpy(¶ms, input.data(), sizeof(params)); + if (params.user_event_id >= MaxNvEvents) { + return NvResult::BadParameter; + } + if (events_interface.registered[params.user_event_id]) { + return NvResult::BadParameter; + } + events_interface.RegisterEvent(params.user_event_id); + return NvResult::Success; +} + +u32 nvhost_ctrl::IocCtrlEventUnregister(const std::vector& input, std::vector& output) { + IocCtrlEventUnregisterParams params{}; + std::memcpy(¶ms, input.data(), sizeof(params)); + if (params.user_event_id >= MaxNvEvents) { + return NvResult::BadParameter; + } + if (!events_interface.registered[params.user_event_id]) { + return NvResult::BadParameter; + } + events_interface.UnregisterEvent(params.user_event_id); + return NvResult::Success; } } // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h index 6d0de2212..2985e7f75 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.h @@ -8,12 +8,13 @@ #include #include "common/common_types.h" #include "core/hle/service/nvdrv/devices/nvdevice.h" +#include "core/hle/service/nvdrv/nvdrv.h" namespace Service::Nvidia::Devices { class nvhost_ctrl final : public nvdevice { public: - nvhost_ctrl(); + nvhost_ctrl(EventsInterface& events_interface); ~nvhost_ctrl() override; u32 ioctl(Ioctl command, const std::vector& input, std::vector& output) override; @@ -135,6 +136,10 @@ private: u32 IocCtrlEventWait(const std::vector& input, std::vector& output, bool is_async); u32 IocCtrlEventRegister(const std::vector& input, std::vector& output); + + u32 IocCtrlEventUnregister(const std::vector& input, std::vector& output); + + EventsInterface& events_interface; }; } // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/interface.cpp b/src/core/hle/service/nvdrv/interface.cpp index b60fc748b..76482d16e 100644 --- a/src/core/hle/service/nvdrv/interface.cpp +++ b/src/core/hle/service/nvdrv/interface.cpp @@ -10,10 +10,15 @@ #include "core/hle/kernel/readable_event.h" #include "core/hle/kernel/writable_event.h" #include "core/hle/service/nvdrv/interface.h" +#include "core/hle/service/nvdrv/nvdata.h" #include "core/hle/service/nvdrv/nvdrv.h" namespace Service::Nvidia { +void NVDRV::SignalGPUInterrupt(const u32 event_id) { + nvdrv->SignalEvent(event_id); +} + void NVDRV::Open(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NVDRV, "called"); @@ -66,13 +71,19 @@ void NVDRV::Initialize(Kernel::HLERequestContext& ctx) { void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; u32 fd = rp.Pop(); - u32 event_id = rp.Pop(); + // TODO(Blinkhawk): Figure the meaning of the flag at bit 16 + u32 event_id = rp.Pop() & 0x000000FF; LOG_WARNING(Service_NVDRV, "(STUBBED) called, fd={:X}, event_id={:X}", fd, event_id); IPC::ResponseBuilder rb{ctx, 3, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(query_event.readable); - rb.Push(0); + if (event_id < 64) { + rb.PushCopyObjects(nvdrv->GetEvent(event_id)); + rb.Push(NvResult::Success); + } else { + rb.Push(0); + rb.Push(NvResult::BadParameter); + } } void NVDRV::SetClientPID(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/nvdrv/interface.h b/src/core/hle/service/nvdrv/interface.h index 5b4889910..421b01017 100644 --- a/src/core/hle/service/nvdrv/interface.h +++ b/src/core/hle/service/nvdrv/interface.h @@ -19,6 +19,8 @@ public: NVDRV(std::shared_ptr nvdrv, const char* name); ~NVDRV() override; + void SignalGPUInterrupt(const u32 event_id); + private: void Open(Kernel::HLERequestContext& ctx); void Ioctl(Kernel::HLERequestContext& ctx); diff --git a/src/core/hle/service/nvdrv/nvdata.h b/src/core/hle/service/nvdrv/nvdata.h index fd5f79f36..6dbc90e4c 100644 --- a/src/core/hle/service/nvdrv/nvdata.h +++ b/src/core/hle/service/nvdrv/nvdata.h @@ -6,6 +6,7 @@ namespace Service::Nvidia { constexpr u32 MaxSyncPoints = 192; +constexpr u32 MaxNvEvents = 64; struct Fence { s32 id; @@ -19,9 +20,18 @@ struct MultiFence { std::array fences; }; -enum class NvResult : u32 { +enum NvResult : u32 { Success = 0, - TryAgain = 11, + BadParameter = 4, + Timeout = 5, + ResourceError = 15, +}; + +enum class EventState { + Free = 0, + Registered = 1, + Waiting = 2, + Busy = 3, }; } // namespace Service::Nvidia diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index 6e4b8f2c6..618bcbc7c 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -4,7 +4,10 @@ #include +#include #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/readable_event.h" +#include "core/hle/kernel/writable_event.h" #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" @@ -33,13 +36,21 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger } Module::Module() { + auto& kernel = Core::System::GetInstance().Kernel(); + for (u32 i = 0; i < MaxNvEvents; i++) { + std::string event_label = fmt::format("NVDRV::NvEvent_{}", i); + events_interface.events[i] = Kernel::WritableEvent::CreateEventPair( + kernel, Kernel::ResetType::Automatic, event_label); + events_interface.status[i] = EventState::Free; + events_interface.registered[i] = false; + } auto nvmap_dev = std::make_shared(); devices["/dev/nvhost-as-gpu"] = std::make_shared(nvmap_dev); devices["/dev/nvhost-gpu"] = std::make_shared(nvmap_dev); devices["/dev/nvhost-ctrl-gpu"] = std::make_shared(); devices["/dev/nvmap"] = nvmap_dev; devices["/dev/nvdisp_disp0"] = std::make_shared(nvmap_dev); - devices["/dev/nvhost-ctrl"] = std::make_shared(); + devices["/dev/nvhost-ctrl"] = std::make_shared(events_interface); devices["/dev/nvhost-nvdec"] = std::make_shared(); devices["/dev/nvhost-nvjpg"] = std::make_shared(); devices["/dev/nvhost-vic"] = std::make_shared(); @@ -77,4 +88,17 @@ ResultCode Module::Close(u32 fd) { return RESULT_SUCCESS; } +void Module::SignalEvent(const u32 event_id) { + if (event_id >= 64) { + LOG_ERROR(Service_NVDRV, "Unexpected Event signalled!"); + return; + } + events_interface.LiberateEvent(event_id); + events_interface.events[event_id].writable->Signal(); +} + +Kernel::SharedPtr Module::GetEvent(const u32 event_id) { + return events_interface.events[event_id].readable; +} + } // namespace Service::Nvidia diff --git a/src/core/hle/service/nvdrv/nvdrv.h b/src/core/hle/service/nvdrv/nvdrv.h index bacd7cdb7..9a4cdc60f 100644 --- a/src/core/hle/service/nvdrv/nvdrv.h +++ b/src/core/hle/service/nvdrv/nvdrv.h @@ -15,12 +15,58 @@ namespace Service::NVFlinger { class NVFlinger; } +namespace Kernel { +class WritableEvent; +} + namespace Service::Nvidia { namespace Devices { class nvdevice; } +struct EventsInterface { + u64 events_mask; + std::array events; + std::array status; + std::array registered; + std::array assigned_syncpt; + std::array assigned_value; + u32 GetFreeEvent() { + u64 mask = events_mask; + for (u32 i = 0; i < MaxNvEvents; i++) { + if (mask & 0x1) { + if (status[i] == EventState::Registered || status[i] == EventState::Free) { + return i; + } + } + mask = mask >> 1; + } + return 0xFFFFFFFF; + } + void SetEventStatus(const u32 event_id, EventState new_status) { + status[event_id] = new_status; + if (new_status == EventState::Registered) { + registered[event_id] = true; + } + } + void RegisterEvent(const u32 event_id) { + registered[event_id] = true; + if (status[event_id] == EventState::Free) { + status[event_id] = EventState::Registered; + } + } + void UnregisterEvent(const u32 event_id) { + registered[event_id] = false; + if (status[event_id] == EventState::Registered) { + status[event_id] = EventState::Free; + } + } + void LiberateEvent(const u32 event_id) { + status[event_id] = registered[event_id] ? EventState::Registered : EventState::Free; + } +}; + class Module final { public: Module(); @@ -42,6 +88,10 @@ public: /// Closes a device file descriptor and returns operation success. ResultCode Close(u32 fd); + void SignalEvent(const u32 event_id); + + Kernel::SharedPtr GetEvent(const u32 event_id); + private: /// Id to use for the next open file descriptor. u32 next_fd = 1; @@ -51,6 +101,8 @@ private: /// Mapping of device node names to their implementation. std::unordered_map> devices; + + EventsInterface events_interface; }; /// Registers all NVDRV services with the specified service manager. -- cgit v1.2.3