NvHost: Remake Ctrl Implementation.

This commit is contained in:
Fernando Sahmkow 2021-11-01 00:51:29 +01:00
parent ac104a24d1
commit 39a5ce4e69
7 changed files with 314 additions and 172 deletions

View file

@ -1,11 +1,14 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-FileCopyrightText: 2021 yuzu emulator team and Skyline Team and Contributors
// SPDX-License-Identifier: GPL-2.0-or-later // (https://github.com/skyline-emu/)
// SPDX-License-Identifier: GPL-3.0-or-later Licensed under GPLv3
// or any later version Refer to the license.txt file included.
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "common/assert.h" #include "common/assert.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/scope_exit.h"
#include "core/core.h" #include "core/core.h"
#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/k_writable_event.h"
@ -30,9 +33,9 @@ NvResult nvhost_ctrl::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>&
case 0x1c: case 0x1c:
return IocCtrlClearEventWait(input, output); return IocCtrlClearEventWait(input, output);
case 0x1d: case 0x1d:
return IocCtrlEventWait(input, output, false);
case 0x1e:
return IocCtrlEventWait(input, output, true); return IocCtrlEventWait(input, output, true);
case 0x1e:
return IocCtrlEventWait(input, output, false);
case 0x1f: case 0x1f:
return IocCtrlEventRegister(input, output); return IocCtrlEventRegister(input, output);
case 0x20: case 0x20:
@ -71,54 +74,65 @@ NvResult nvhost_ctrl::NvOsGetConfigU32(const std::vector<u8>& input, std::vector
} }
NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector<u8>& input, std::vector<u8>& output, NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector<u8>& input, std::vector<u8>& output,
bool is_async) { bool is_allocation) {
IocCtrlEventWaitParams params{}; IocCtrlEventWaitParams params{};
std::memcpy(&params, input.data(), sizeof(params)); std::memcpy(&params, input.data(), sizeof(params));
LOG_DEBUG(Service_NVDRV, "syncpt_id={}, threshold={}, timeout={}, is_async={}", LOG_DEBUG(Service_NVDRV, "syncpt_id={}, threshold={}, timeout={}, is_allocation={}",
params.syncpt_id, params.threshold, params.timeout, is_async); params.fence.id, params.fence.value, params.timeout, is_allocation);
if (params.syncpt_id >= MaxSyncPoints) { bool must_unmark_fail = !is_allocation;
return NvResult::BadParameter; const u32 event_id = params.value.raw;
} SCOPE_EXIT({
u32 event_id = params.value & 0x00FF;
if (event_id >= MaxNvEvents) {
std::memcpy(output.data(), &params, sizeof(params));
return NvResult::BadParameter;
}
if (syncpoint_manager.IsSyncpointExpired(params.syncpt_id, params.threshold)) {
params.value = syncpoint_manager.GetSyncpointMin(params.syncpt_id);
std::memcpy(output.data(), &params, sizeof(params)); std::memcpy(output.data(), &params, sizeof(params));
if (must_unmark_fail) {
events_interface.fails[event_id] = 0; events_interface.fails[event_id] = 0;
}
});
const u32 fence_id = static_cast<u32>(params.fence.id);
if (fence_id >= MaxSyncPoints) {
return NvResult::BadParameter;
}
if (params.fence.value == 0) {
params.value.raw = syncpoint_manager.GetSyncpointMin(fence_id);
return NvResult::Success; return NvResult::Success;
} }
if (const auto new_value = syncpoint_manager.RefreshSyncpoint(params.syncpt_id); if (syncpoint_manager.IsSyncpointExpired(fence_id, params.fence.value)) {
syncpoint_manager.IsSyncpointExpired(params.syncpt_id, params.threshold)) { params.value.raw = syncpoint_manager.GetSyncpointMin(fence_id);
params.value = new_value; return NvResult::Success;
std::memcpy(output.data(), &params, sizeof(params)); }
events_interface.fails[event_id] = 0;
if (const auto new_value = syncpoint_manager.RefreshSyncpoint(fence_id);
syncpoint_manager.IsSyncpointExpired(fence_id, params.fence.value)) {
params.value.raw = new_value;
return NvResult::Success; return NvResult::Success;
} }
auto& gpu = system.GPU(); auto& gpu = system.GPU();
const u32 target_value = syncpoint_manager.GetSyncpointMax(params.syncpt_id); const u32 target_value = params.fence.value;
if (!is_async) { auto lock = events_interface.Lock();
params.value = 0;
u32 slot = [&]() {
if (is_allocation) {
params.value.raw = 0;
return events_interface.FindFreeEvent(fence_id);
} else {
return params.value.raw;
} }
}();
const auto check_failing = [&]() { const auto check_failing = [&]() {
if (events_interface.fails[event_id] > 1) { if (events_interface.fails[slot] > 1) {
{ {
auto lk = system.StallProcesses(); auto lk = system.StallProcesses();
gpu.WaitFence(params.syncpt_id, target_value); gpu.WaitFence(fence_id, target_value);
system.UnstallProcesses(); system.UnstallProcesses();
} }
std::memcpy(output.data(), &params, sizeof(params)); params.value.raw = target_value;
events_interface.fails[event_id] = 0;
return true; return true;
} }
return false; return false;
@ -131,47 +145,76 @@ NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector<u8>& input, std::vector
return NvResult::Timeout; return NvResult::Timeout;
} }
EventState status = events_interface.status[event_id]; if (slot >= MaxNvEvents) {
const bool bad_parameter = status == EventState::Busy;
if (bad_parameter) {
std::memcpy(output.data(), &params, sizeof(params));
return NvResult::BadParameter; return NvResult::BadParameter;
} }
events_interface.SetEventStatus(event_id, EventState::Waiting);
events_interface.assigned_syncpt[event_id] = params.syncpt_id; auto* event = events_interface.events[slot];
events_interface.assigned_value[event_id] = target_value;
if (is_async) { if (!event) {
params.value = params.syncpt_id << 4; return NvResult::BadParameter;
} else {
params.value = ((params.syncpt_id & 0xfff) << 16) | 0x10000000;
} }
params.value |= event_id;
if (events_interface.IsBeingUsed(slot)) {
return NvResult::BadParameter;
}
if (check_failing()) { if (check_failing()) {
return NvResult::Success; return NvResult::Success;
} }
gpu.RegisterSyncptInterrupt(params.syncpt_id, target_value);
std::memcpy(output.data(), &params, sizeof(params)); params.value.raw = 0;
events_interface.status[slot].store(EventState::Waiting, std::memory_order_release);
events_interface.assigned_syncpt[slot] = fence_id;
events_interface.assigned_value[slot] = target_value;
if (is_allocation) {
params.value.syncpoint_id_for_allocation.Assign(static_cast<u16>(fence_id));
params.value.event_allocated.Assign(1);
} else {
params.value.syncpoint_id.Assign(fence_id);
}
params.value.raw |= slot;
gpu.RegisterSyncptInterrupt(fence_id, target_value);
return NvResult::Timeout; return NvResult::Timeout;
} }
NvResult nvhost_ctrl::FreeEvent(u32 slot) {
if (slot >= MaxNvEvents) {
return NvResult::BadParameter;
}
if (!events_interface.registered[slot]) {
return NvResult::Success;
}
if (events_interface.IsBeingUsed(slot)) {
return NvResult::Busy;
}
events_interface.Free(slot);
return NvResult::Success;
}
NvResult nvhost_ctrl::IocCtrlEventRegister(const std::vector<u8>& input, std::vector<u8>& output) { NvResult nvhost_ctrl::IocCtrlEventRegister(const std::vector<u8>& input, std::vector<u8>& output) {
IocCtrlEventRegisterParams params{}; IocCtrlEventRegisterParams params{};
std::memcpy(&params, input.data(), sizeof(params)); std::memcpy(&params, input.data(), sizeof(params));
const u32 event_id = params.user_event_id & 0x00FF; const u32 event_id = params.user_event_id;
LOG_DEBUG(Service_NVDRV, " called, user_event_id: {:X}", event_id); LOG_DEBUG(Service_NVDRV, " called, user_event_id: {:X}", event_id);
if (event_id >= MaxNvEvents) { if (event_id >= MaxNvEvents) {
return NvResult::BadParameter; return NvResult::BadParameter;
} }
auto lock = events_interface.Lock();
if (events_interface.registered[event_id]) { if (events_interface.registered[event_id]) {
const auto event_state = events_interface.status[event_id]; const auto result = FreeEvent(event_id);
if (event_state != EventState::Free) { if (result != NvResult::Success) {
LOG_WARNING(Service_NVDRV, "Event already registered! Unregistering previous event"); return result;
events_interface.UnregisterEvent(event_id);
} else {
return NvResult::BadParameter;
} }
} }
events_interface.RegisterEvent(event_id); events_interface.Create(event_id);
return NvResult::Success; return NvResult::Success;
} }
@ -181,32 +224,33 @@ NvResult nvhost_ctrl::IocCtrlEventUnregister(const std::vector<u8>& input,
std::memcpy(&params, input.data(), sizeof(params)); std::memcpy(&params, input.data(), sizeof(params));
const u32 event_id = params.user_event_id & 0x00FF; const u32 event_id = params.user_event_id & 0x00FF;
LOG_DEBUG(Service_NVDRV, " called, user_event_id: {:X}", event_id); LOG_DEBUG(Service_NVDRV, " called, user_event_id: {:X}", event_id);
if (event_id >= MaxNvEvents) {
return NvResult::BadParameter; auto lock = events_interface.Lock();
} return FreeEvent(event_id);
if (!events_interface.registered[event_id]) {
return NvResult::BadParameter;
}
events_interface.UnregisterEvent(event_id);
return NvResult::Success;
} }
NvResult nvhost_ctrl::IocCtrlClearEventWait(const std::vector<u8>& input, std::vector<u8>& output) { NvResult nvhost_ctrl::IocCtrlClearEventWait(const std::vector<u8>& input, std::vector<u8>& output) {
IocCtrlEventSignalParams params{}; IocCtrlEventClearParams params{};
std::memcpy(&params, input.data(), sizeof(params)); std::memcpy(&params, input.data(), sizeof(params));
u32 event_id = params.event_id & 0x00FF; u32 event_id = params.event_id.slot;
LOG_WARNING(Service_NVDRV, "cleared event wait on, event_id: {:X}", event_id); LOG_DEBUG(Service_NVDRV, "called, event_id: {:X}", event_id);
if (event_id >= MaxNvEvents) { if (event_id >= MaxNvEvents) {
return NvResult::BadParameter; return NvResult::BadParameter;
} }
if (events_interface.status[event_id] == EventState::Waiting) {
events_interface.LiberateEvent(event_id); auto lock = events_interface.Lock();
if (events_interface.status[event_id].exchange(
EventState::Cancelling, std::memory_order_acq_rel) == EventState::Waiting) {
system.GPU().CancelSyncptInterrupt(events_interface.assigned_syncpt[event_id],
events_interface.assigned_value[event_id]);
syncpoint_manager.RefreshSyncpoint(events_interface.assigned_syncpt[event_id]);
} }
events_interface.fails[event_id]++; events_interface.fails[event_id]++;
events_interface.status[event_id].store(EventState::Cancelled, std::memory_order_release);
syncpoint_manager.RefreshSyncpoint(events_interface.events[event_id].fence.id); events_interface.events[event_id]->GetWritableEvent().Clear();
return NvResult::Success; return NvResult::Success;
} }

View file

@ -5,6 +5,7 @@
#include <array> #include <array>
#include <vector> #include <vector>
#include "common/bit_field.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/service/nvdrv/devices/nvdevice.h" #include "core/hle/service/nvdrv/devices/nvdevice.h"
#include "core/hle/service/nvdrv/nvdrv.h" #include "core/hle/service/nvdrv/nvdrv.h"
@ -27,6 +28,24 @@ public:
void OnOpen(DeviceFD fd) override; void OnOpen(DeviceFD fd) override;
void OnClose(DeviceFD fd) override; void OnClose(DeviceFD fd) override;
union SyncpointEventValue {
u32 raw;
union {
BitField<0, 4, u32> partial_slot;
BitField<4, 28, u32> syncpoint_id;
};
struct {
u16 slot;
union {
BitField<0, 12, u16> syncpoint_id_for_allocation;
BitField<12, 1, u16> event_allocated;
};
};
};
static_assert(sizeof(SyncpointEventValue) == sizeof(u32));
private: private:
struct IocSyncptReadParams { struct IocSyncptReadParams {
u32_le id{}; u32_le id{};
@ -83,27 +102,18 @@ private:
}; };
static_assert(sizeof(IocGetConfigParams) == 387, "IocGetConfigParams is incorrect size"); static_assert(sizeof(IocGetConfigParams) == 387, "IocGetConfigParams is incorrect size");
struct IocCtrlEventSignalParams { struct IocCtrlEventClearParams {
u32_le event_id{}; SyncpointEventValue event_id{};
}; };
static_assert(sizeof(IocCtrlEventSignalParams) == 4, static_assert(sizeof(IocCtrlEventClearParams) == 4,
"IocCtrlEventSignalParams is incorrect size"); "IocCtrlEventClearParams is incorrect size");
struct IocCtrlEventWaitParams { struct IocCtrlEventWaitParams {
u32_le syncpt_id{}; NvFence fence{};
u32_le threshold{};
s32_le timeout{};
u32_le value{};
};
static_assert(sizeof(IocCtrlEventWaitParams) == 16, "IocCtrlEventWaitParams is incorrect size");
struct IocCtrlEventWaitAsyncParams {
u32_le syncpt_id{};
u32_le threshold{};
u32_le timeout{}; u32_le timeout{};
u32_le value{}; SyncpointEventValue value{};
}; };
static_assert(sizeof(IocCtrlEventWaitAsyncParams) == 16, static_assert(sizeof(IocCtrlEventWaitParams) == 16,
"IocCtrlEventWaitAsyncParams is incorrect size"); "IocCtrlEventWaitAsyncParams is incorrect size");
struct IocCtrlEventRegisterParams { struct IocCtrlEventRegisterParams {
@ -124,11 +134,14 @@ private:
static_assert(sizeof(IocCtrlEventKill) == 8, "IocCtrlEventKill is incorrect size"); static_assert(sizeof(IocCtrlEventKill) == 8, "IocCtrlEventKill is incorrect size");
NvResult NvOsGetConfigU32(const std::vector<u8>& input, std::vector<u8>& output); NvResult NvOsGetConfigU32(const std::vector<u8>& input, std::vector<u8>& output);
NvResult IocCtrlEventWait(const std::vector<u8>& input, std::vector<u8>& output, bool is_async); NvResult IocCtrlEventWait(const std::vector<u8>& input, std::vector<u8>& output,
bool is_allocation);
NvResult IocCtrlEventRegister(const std::vector<u8>& input, std::vector<u8>& output); NvResult IocCtrlEventRegister(const std::vector<u8>& input, std::vector<u8>& output);
NvResult IocCtrlEventUnregister(const std::vector<u8>& input, std::vector<u8>& output); NvResult IocCtrlEventUnregister(const std::vector<u8>& input, std::vector<u8>& output);
NvResult IocCtrlClearEventWait(const std::vector<u8>& input, std::vector<u8>& output); NvResult IocCtrlClearEventWait(const std::vector<u8>& input, std::vector<u8>& output);
NvResult FreeEvent(u32 slot);
EventInterface& events_interface; EventInterface& events_interface;
SyncpointManager& syncpoint_manager; SyncpointManager& syncpoint_manager;
}; };

View file

@ -1,5 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project // SPDX-FileCopyrightText: 2021 yuzu emulator team and Skyline Team and Contributors
// SPDX-License-Identifier: GPL-2.0-or-later // (https://github.com/skyline-emu/)
// SPDX-License-Identifier: GPL-3.0-or-later Licensed under GPLv3
// or any later version Refer to the license.txt file included.
#pragma once #pragma once
@ -78,11 +80,15 @@ enum class NvResult : u32 {
ModuleNotPresent = 0xA000E, ModuleNotPresent = 0xA000E,
}; };
// obtained from
// https://github.com/skyline-emu/skyline/blob/nvdec-dev/app/src/main/cpp/skyline/services/nvdrv/devices/nvhost/ctrl.h#L47
enum class EventState { enum class EventState {
Free = 0, Available = 0,
Registered = 1, Waiting = 1,
Waiting = 2, Cancelling = 2,
Busy = 3, Signalling = 3,
Signalled = 4,
Cancelled = 5,
}; };
union Ioctl { union Ioctl {

View file

@ -1,6 +1,9 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-FileCopyrightText: 2021 yuzu emulator team and Skyline Team and Contributors
// SPDX-License-Identifier: GPL-2.0-or-later // (https://github.com/skyline-emu/)
// SPDX-License-Identifier: GPL-3.0-or-later Licensed under GPLv3
// or any later version Refer to the license.txt file included.
#include <bit>
#include <utility> #include <utility>
#include <fmt/format.h> #include <fmt/format.h>
@ -26,6 +29,73 @@
namespace Service::Nvidia { namespace Service::Nvidia {
std::unique_lock<std::mutex> EventInterface::Lock() {
return std::unique_lock<std::mutex>(events_mutex);
}
void EventInterface::Signal(u32 event_id) {
if (status[event_id].exchange(EventState::Signalling, std::memory_order_acq_rel) ==
EventState::Waiting) {
events[event_id]->GetWritableEvent().Signal();
}
status[event_id].store(EventState::Signalled, std::memory_order_release);
}
void EventInterface::Create(u32 event_id) {
ASSERT(!events[event_id]);
ASSERT(!registered[event_id]);
ASSERT(!IsBeingUsed(event_id));
events[event_id] = backup[event_id];
status[event_id] = EventState::Available;
registered[event_id] = true;
const u64 mask = 1ULL << event_id;
fails[event_id] = 0;
events_mask |= mask;
LOG_CRITICAL(Service_NVDRV, "Created Event {}", event_id);
}
void EventInterface::Free(u32 event_id) {
ASSERT(events[event_id]);
ASSERT(registered[event_id]);
ASSERT(!IsBeingUsed(event_id));
backup[event_id]->GetWritableEvent().Clear();
events[event_id] = nullptr;
status[event_id] = EventState::Available;
registered[event_id] = false;
const u64 mask = ~(1ULL << event_id);
events_mask &= mask;
LOG_CRITICAL(Service_NVDRV, "Freed Event {}", event_id);
}
u32 EventInterface::FindFreeEvent(u32 syncpoint_id) {
u32 slot{MaxNvEvents};
u32 free_slot{MaxNvEvents};
for (u32 i = 0; i < MaxNvEvents; i++) {
if (registered[i]) {
if (!IsBeingUsed(i)) {
slot = i;
if (assigned_syncpt[i] == syncpoint_id) {
return slot;
}
}
} else if (free_slot == MaxNvEvents) {
free_slot = i;
}
}
if (free_slot < MaxNvEvents) {
Create(free_slot);
return free_slot;
}
if (slot < MaxNvEvents) {
return slot;
}
LOG_CRITICAL(Service_NVDRV, "Failed to allocate an event");
return 0;
}
void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger, void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger,
Core::System& system) { Core::System& system) {
auto module_ = std::make_shared<Module>(system); auto module_ = std::make_shared<Module>(system);
@ -38,12 +108,14 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger
} }
Module::Module(Core::System& system) Module::Module(Core::System& system)
: syncpoint_manager{system.GPU()}, service_context{system, "nvdrv"} { : syncpoint_manager{system.GPU()}, events_interface{*this}, service_context{system, "nvdrv"} {
events_interface.events_mask = 0;
for (u32 i = 0; i < MaxNvEvents; i++) { for (u32 i = 0; i < MaxNvEvents; i++) {
events_interface.events[i].event = events_interface.status[i] = EventState::Available;
service_context.CreateEvent(fmt::format("NVDRV::NvEvent_{}", i)); events_interface.events[i] = nullptr;
events_interface.status[i] = EventState::Free;
events_interface.registered[i] = false; events_interface.registered[i] = false;
events_interface.backup[i] =
service_context.CreateEvent(fmt::format("NVDRV::NvEvent_{}", i));
} }
auto nvmap_dev = std::make_shared<Devices::nvmap>(system); auto nvmap_dev = std::make_shared<Devices::nvmap>(system);
devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev); devices["/dev/nvhost-as-gpu"] = std::make_shared<Devices::nvhost_as_gpu>(system, nvmap_dev);
@ -62,8 +134,12 @@ Module::Module(Core::System& system)
} }
Module::~Module() { Module::~Module() {
auto lock = events_interface.Lock();
for (u32 i = 0; i < MaxNvEvents; i++) { for (u32 i = 0; i < MaxNvEvents; i++) {
service_context.CloseEvent(events_interface.events[i].event); if (events_interface.registered[i]) {
events_interface.Free(i);
}
service_context.CloseEvent(events_interface.backup[i]);
} }
} }
@ -169,21 +245,41 @@ NvResult Module::Close(DeviceFD fd) {
} }
void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) { void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) {
for (u32 i = 0; i < MaxNvEvents; i++) { const u32 max = MaxNvEvents - std::countl_zero(events_interface.events_mask);
if (events_interface.assigned_syncpt[i] == syncpoint_id && const u32 min = std::countr_zero(events_interface.events_mask);
for (u32 i = min; i < max; i++) {
if (events_interface.registered[i] && events_interface.assigned_syncpt[i] == syncpoint_id &&
events_interface.assigned_value[i] == value) { events_interface.assigned_value[i] == value) {
events_interface.LiberateEvent(i); events_interface.Signal(i);
events_interface.events[i].event->GetWritableEvent().Signal();
} }
} }
} }
Kernel::KReadableEvent& Module::GetEvent(const u32 event_id) { Kernel::KEvent* Module::GetEvent(u32 event_id) {
return events_interface.events[event_id].event->GetReadableEvent(); const auto event = Devices::nvhost_ctrl::SyncpointEventValue{.raw = event_id};
}
Kernel::KWritableEvent& Module::GetEventWriteable(const u32 event_id) { const bool allocated = event.event_allocated.Value() != 0;
return events_interface.events[event_id].event->GetWritableEvent(); const u32 slot{allocated ? event.partial_slot.Value() : static_cast<u32>(event.slot)};
if (slot >= MaxNvEvents) {
ASSERT(false);
return nullptr;
}
const u32 syncpoint_id{allocated ? event.syncpoint_id_for_allocation.Value()
: event.syncpoint_id.Value()};
auto lock = events_interface.Lock();
if (events_interface.registered[slot] &&
events_interface.assigned_syncpt[slot] == syncpoint_id) {
ASSERT(events_interface.events[slot]);
return events_interface.events[slot];
}
// Temporary hack.
events_interface.Create(slot);
events_interface.assigned_syncpt[slot] = syncpoint_id;
ASSERT(false);
return events_interface.events[slot];
} }
} // namespace Service::Nvidia } // namespace Service::Nvidia

View file

@ -1,5 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-FileCopyrightText: 2021 yuzu emulator team and Skyline Team and Contributors
// SPDX-License-Identifier: GPL-2.0-or-later // (https://github.com/skyline-emu/)
// SPDX-License-Identifier: GPL-3.0-or-later Licensed under GPLv3
// or any later version Refer to the license.txt file included.
#pragma once #pragma once
@ -34,19 +36,20 @@ namespace Devices {
class nvdevice; class nvdevice;
} }
/// Represents an Nvidia event class Module;
struct NvEvent {
Kernel::KEvent* event{};
NvFence fence{};
};
struct EventInterface { class EventInterface {
// Mask representing currently busy events public:
EventInterface(Module& module_) : module{module_} {}
// Mask representing registered events
u64 events_mask{}; u64 events_mask{};
// Each kernel event associated to an NV event // Each kernel event associated to an NV event
std::array<NvEvent, MaxNvEvents> events; std::array<Kernel::KEvent*, MaxNvEvents> events{};
// Backup NV event
std::array<Kernel::KEvent*, MaxNvEvents> backup{};
// The status of the current NVEvent // The status of the current NVEvent
std::array<EventState, MaxNvEvents> status{}; std::array<std::atomic<EventState>, MaxNvEvents> status{};
// Tells if an NVEvent is registered or not // Tells if an NVEvent is registered or not
std::array<bool, MaxNvEvents> registered{}; std::array<bool, MaxNvEvents> registered{};
// Tells the NVEvent that it has failed. // Tells the NVEvent that it has failed.
@ -59,50 +62,26 @@ struct EventInterface {
std::array<u32, MaxNvEvents> assigned_value{}; std::array<u32, MaxNvEvents> assigned_value{};
// Constant to denote an unasigned syncpoint. // Constant to denote an unasigned syncpoint.
static constexpr u32 unassigned_syncpt = 0xFFFFFFFF; static constexpr u32 unassigned_syncpt = 0xFFFFFFFF;
std::optional<u32> GetFreeEvent() const {
u64 mask = events_mask; bool IsBeingUsed(u32 event_id) {
for (u32 i = 0; i < MaxNvEvents; i++) { const auto current_status = status[event_id].load(std::memory_order_acquire);
const bool is_free = (mask & 0x1) == 0; return current_status == EventState::Waiting || current_status == EventState::Cancelling ||
if (is_free) { current_status == EventState::Signalling;
if (status[i] == EventState::Registered || status[i] == EventState::Free) {
return {i};
}
}
mask = mask >> 1;
}
return std::nullopt;
}
void SetEventStatus(const u32 event_id, EventState new_status) {
EventState old_status = status[event_id];
if (old_status == new_status) {
return;
}
status[event_id] = new_status;
if (new_status == EventState::Registered) {
registered[event_id] = true;
}
if (new_status == EventState::Waiting || new_status == EventState::Busy) {
events_mask |= (1ULL << event_id);
}
}
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;
events_mask &= ~(1ULL << event_id);
assigned_syncpt[event_id] = unassigned_syncpt;
assigned_value[event_id] = 0;
} }
std::unique_lock<std::mutex> Lock();
void Signal(u32 event_id);
void Create(u32 event_id);
void Free(u32 event_id);
u32 FindFreeEvent(u32 syncpoint_id);
private:
std::mutex events_mutex;
Module& module;
}; };
class Module final { class Module final {
@ -139,11 +118,11 @@ public:
void SignalSyncpt(const u32 syncpoint_id, const u32 value); void SignalSyncpt(const u32 syncpoint_id, const u32 value);
Kernel::KReadableEvent& GetEvent(u32 event_id); Kernel::KEvent* GetEvent(u32 event_id);
Kernel::KWritableEvent& GetEventWriteable(u32 event_id);
private: private:
friend class EventInterface;
/// Manages syncpoints on the host /// Manages syncpoints on the host
SyncpointManager syncpoint_manager; SyncpointManager syncpoint_manager;
@ -159,6 +138,9 @@ private:
EventInterface events_interface; EventInterface events_interface;
KernelHelpers::ServiceContext service_context; KernelHelpers::ServiceContext service_context;
void CreateEvent(u32 event_id);
void FreeEvent(u32 event_id);
}; };
/// Registers all NVDRV services with the specified service manager. /// Registers all NVDRV services with the specified service manager.

View file

@ -5,6 +5,7 @@
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/core.h" #include "core/core.h"
#include "core/hle/ipc_helpers.h" #include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_readable_event.h"
#include "core/hle/service/nvdrv/nvdata.h" #include "core/hle/service/nvdrv/nvdata.h"
#include "core/hle/service/nvdrv/nvdrv.h" #include "core/hle/service/nvdrv/nvdrv.h"
@ -164,8 +165,7 @@ void NVDRV::Initialize(Kernel::HLERequestContext& ctx) {
void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) { void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx}; IPC::RequestParser rp{ctx};
const auto fd = rp.Pop<DeviceFD>(); const auto fd = rp.Pop<DeviceFD>();
const auto event_id = rp.Pop<u32>() & 0x00FF; const auto event_id = rp.Pop<u32>();
LOG_WARNING(Service_NVDRV, "(STUBBED) called, fd={:X}, event_id={:X}", fd, event_id);
if (!is_initialized) { if (!is_initialized) {
ServiceError(ctx, NvResult::NotInitialized); ServiceError(ctx, NvResult::NotInitialized);
@ -180,12 +180,13 @@ void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) {
return; return;
} }
if (event_id < MaxNvEvents) { auto* event = nvdrv->GetEvent(event_id);
if (event) {
IPC::ResponseBuilder rb{ctx, 3, 1}; IPC::ResponseBuilder rb{ctx, 3, 1};
rb.Push(ResultSuccess); rb.Push(ResultSuccess);
auto& event = nvdrv->GetEvent(event_id); auto& readable_event = event->GetReadableEvent();
event.Clear(); rb.PushCopyObjects(readable_event);
rb.PushCopyObjects(event);
rb.PushEnum(NvResult::Success); rb.PushEnum(NvResult::Success);
} else { } else {
IPC::ResponseBuilder rb{ctx, 3}; IPC::ResponseBuilder rb{ctx, 3};

View file

@ -201,7 +201,7 @@ public:
void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value); void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value);
[[nodiscard]] bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value); bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value);
[[nodiscard]] u64 GetTicks() const; [[nodiscard]] u64 GetTicks() const;