Skip to content

Commit

Permalink
equeue: sceGnmGetEqEventType/sceKernelGetEventData impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenkai committed Dec 21, 2024
1 parent 8d8bb05 commit 5870d85
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
30 changes: 18 additions & 12 deletions src/core/libraries/gnmdriver/gnmdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Libraries::GnmDriver {

using namespace AmdGpu;

enum GnmEventIdents : u64 {
enum GnmEqEventType : u64 {
Compute0RelMem = 0x00,
Compute1RelMem = 0x01,
Compute2RelMem = 0x02,
Expand Down Expand Up @@ -347,8 +347,7 @@ s32 PS4_SYSV_ABI sceGnmAddEqEvent(SceKernelEqueue eq, u64 id, void* udata) {
EqueueEvent kernel_event{};
kernel_event.event.ident = id;
kernel_event.event.filter = SceKernelEvent::Filter::GraphicsCore;
// The library only sets EV_ADD but it is suspected the kernel driver forces EV_CLEAR
kernel_event.event.flags = SceKernelEvent::Flags::Clear;
kernel_event.event.flags = SceKernelEvent::Flags::Add;
kernel_event.event.fflags = 0;
kernel_event.event.data = id;
kernel_event.event.udata = udata;
Expand All @@ -357,11 +356,18 @@ s32 PS4_SYSV_ABI sceGnmAddEqEvent(SceKernelEqueue eq, u64 id, void* udata) {
Platform::IrqC::Instance()->Register(
static_cast<Platform::InterruptId>(id),
[=](Platform::InterruptId irq) {
ASSERT_MSG(irq == static_cast<Platform::InterruptId>(id),
"An unexpected IRQ occured"); // We need to convert IRQ# to event id and do
// proper filtering in trigger function
eq->TriggerEvent(static_cast<GnmEventIdents>(id), SceKernelEvent::Filter::GraphicsCore,
nullptr);
ASSERT_MSG(irq == static_cast<Platform::InterruptId>(id), "An unexpected IRQ occured");

// We need to convert IRQ# to event id
// Exclude ids we shouldn't care about as they aren't a valid EqEventType.
if ((static_cast<u32>(irq) > static_cast<u32>(Platform::InterruptId::Compute6RelMem) &&
static_cast<u32>(irq) < static_cast<u32>(Platform::InterruptId::InterruptIdMax)) ||
static_cast<u32>(irq) > static_cast<u32>(Platform::InterruptId::InterruptIdMax))
return;

// Event data is expected to be EqEventType as per sceGnmGetEqEventType.
eq->TriggerEvent(static_cast<GnmEqEventType>(id), SceKernelEvent::Filter::GraphicsCore,
reinterpret_cast<void*>(id));
},
eq);
return ORBIS_OK;
Expand Down Expand Up @@ -476,7 +482,7 @@ s32 PS4_SYSV_ABI sceGnmDeleteEqEvent(SceKernelEqueue eq, u64 id) {
return ORBIS_KERNEL_ERROR_EBADF;
}

eq->RemoveEvent(id);
eq->RemoveEvent(id, SceKernelEvent::Filter::GraphicsCore);

Platform::IrqC::Instance()->Unregister(static_cast<Platform::InterruptId>(id), eq);
return ORBIS_OK;
Expand Down Expand Up @@ -1000,9 +1006,9 @@ int PS4_SYSV_ABI sceGnmGetDebugTimestamp() {
return ORBIS_OK;
}

int PS4_SYSV_ABI sceGnmGetEqEventType() {
LOG_ERROR(Lib_GnmDriver, "(STUBBED) called");
return ORBIS_OK;
int PS4_SYSV_ABI sceGnmGetEqEventType(const SceKernelEvent* ev) {
LOG_TRACE(Lib_GnmDriver, "called");
return sceKernelGetEventData(ev);
}

int PS4_SYSV_ABI sceGnmGetEqTimeStamp() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/libraries/gnmdriver/gnmdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int PS4_SYSV_ABI sceGnmGetCoredumpMode();
int PS4_SYSV_ABI sceGnmGetCoredumpProtectionFaultTimestamp();
int PS4_SYSV_ABI sceGnmGetDbgGcHandle();
int PS4_SYSV_ABI sceGnmGetDebugTimestamp();
int PS4_SYSV_ABI sceGnmGetEqEventType();
int PS4_SYSV_ABI sceGnmGetEqEventType(const SceKernelEvent* ev);
int PS4_SYSV_ABI sceGnmGetEqTimeStamp();
int PS4_SYSV_ABI sceGnmGetGpuBlockStatus();
u32 PS4_SYSV_ABI sceGnmGetGpuCoreClockFrequency();
Expand Down
18 changes: 14 additions & 4 deletions src/core/libraries/kernel/equeue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Libraries::Kernel {

// Events are uniquely identified by id and filter.

bool EqueueInternal::AddEvent(EqueueEvent& event) {
std::scoped_lock lock{m_mutex};

Expand All @@ -25,7 +27,7 @@ bool EqueueInternal::AddEvent(EqueueEvent& event) {
return true;
}

bool EqueueInternal::RemoveEvent(u64 id) {
bool EqueueInternal::RemoveEvent(u64 id, s16 filter) {
bool has_found = false;
std::scoped_lock lock{m_mutex};

Expand Down Expand Up @@ -66,7 +68,7 @@ int EqueueInternal::WaitForEvents(SceKernelEvent* ev, int num, u32 micros) {

if (ev->flags & SceKernelEvent::Flags::OneShot) {
for (auto ev_id = 0u; ev_id < count; ++ev_id) {
RemoveEvent(ev->ident);
RemoveEvent(ev->ident, ev->filter);
}
}

Expand All @@ -92,8 +94,11 @@ int EqueueInternal::GetTriggeredEvents(SceKernelEvent* ev, int num) {
int count = 0;
for (auto& event : m_events) {
if (event.IsTriggered()) {
// Event should not trigger again
event.ResetTriggerState();

if (event.event.flags & SceKernelEvent::Flags::Clear) {
event.Reset();
event.Clear();
}
ev[count++] = event.event;
if (count == num) {
Expand Down Expand Up @@ -332,7 +337,7 @@ int PS4_SYSV_ABI sceKernelDeleteUserEvent(SceKernelEqueue eq, int id) {
return ORBIS_KERNEL_ERROR_EBADF;
}

if (!eq->RemoveEvent(id)) {
if (!eq->RemoveEvent(id, SceKernelEvent::Filter::User)) {
return ORBIS_KERNEL_ERROR_ENOENT;
}
return ORBIS_OK;
Expand All @@ -342,6 +347,10 @@ s16 PS4_SYSV_ABI sceKernelGetEventFilter(const SceKernelEvent* ev) {
return ev->filter;
}

u64 PS4_SYSV_ABI sceKernelGetEventData(const SceKernelEvent* ev) {
return ev->data;
}

void RegisterEventQueue(Core::Loader::SymbolsResolver* sym) {
LIB_FUNCTION("D0OdFMjp46I", "libkernel", 1, "libkernel", 1, 1, sceKernelCreateEqueue);
LIB_FUNCTION("jpFjmgAC5AE", "libkernel", 1, "libkernel", 1, 1, sceKernelDeleteEqueue);
Expand All @@ -354,6 +363,7 @@ void RegisterEventQueue(Core::Loader::SymbolsResolver* sym) {
LIB_FUNCTION("LJDwdSNTnDg", "libkernel", 1, "libkernel", 1, 1, sceKernelDeleteUserEvent);
LIB_FUNCTION("mJ7aghmgvfc", "libkernel", 1, "libkernel", 1, 1, sceKernelGetEventId);
LIB_FUNCTION("23CPPI1tyBY", "libkernel", 1, "libkernel", 1, 1, sceKernelGetEventFilter);
LIB_FUNCTION("kwGyyjohI50", "libkernel", 1, "libkernel", 1, 1, sceKernelGetEventData);
}

} // namespace Libraries::Kernel
11 changes: 8 additions & 3 deletions src/core/libraries/kernel/equeue.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ struct EqueueEvent {
std::chrono::steady_clock::time_point time_added;
std::unique_ptr<boost::asio::steady_timer> timer;

void Reset() {
void ResetTriggerState() {
is_triggered = false;
}

void Clear() {
event.fflags = 0;
event.data = 0;
}
Expand All @@ -83,7 +86,7 @@ struct EqueueEvent {
}

bool operator==(const EqueueEvent& ev) const {
return ev.event.ident == event.ident;
return ev.event.ident == event.ident && ev.event.filter == event.filter;
}

private:
Expand All @@ -99,7 +102,7 @@ class EqueueInternal {
}

bool AddEvent(EqueueEvent& event);
bool RemoveEvent(u64 id);
bool RemoveEvent(u64 id, s16 filter);
int WaitForEvents(SceKernelEvent* ev, int num, u32 micros);
bool TriggerEvent(u64 ident, s16 filter, void* trigger_data);
int GetTriggeredEvents(SceKernelEvent* ev, int num);
Expand All @@ -122,6 +125,8 @@ class EqueueInternal {
using SceKernelUseconds = u32;
using SceKernelEqueue = EqueueInternal*;

u64 PS4_SYSV_ABI sceKernelGetEventData(const SceKernelEvent* ev);

void RegisterEventQueue(Core::Loader::SymbolsResolver* sym);

} // namespace Libraries::Kernel

0 comments on commit 5870d85

Please sign in to comment.