yuzu/src/video_core/host1x/syncpoint_manager.h

99 lines
3.3 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
2022-01-30 09:31:13 +00:00
#pragma once
#include <array>
#include <atomic>
#include <condition_variable>
#include <functional>
#include <list>
#include <mutex>
#include "common/common_types.h"
namespace Tegra {
namespace Host1x {
class SyncpointManager {
public:
2022-09-01 03:45:22 +00:00
u32 GetGuestSyncpointValue(u32 id) const {
2022-01-30 09:31:13 +00:00
return syncpoints_guest[id].load(std::memory_order_acquire);
}
2022-09-01 03:45:22 +00:00
u32 GetHostSyncpointValue(u32 id) const {
2022-01-30 09:31:13 +00:00
return syncpoints_host[id].load(std::memory_order_acquire);
}
struct RegisteredAction {
2022-09-01 03:45:22 +00:00
explicit RegisteredAction(u32 expected_value_, std::function<void()>&& action_)
: expected_value{expected_value_}, action{std::move(action_)} {}
2022-01-30 09:31:13 +00:00
u32 expected_value;
2022-09-01 03:45:22 +00:00
std::function<void()> action;
2022-01-30 09:31:13 +00:00
};
using ActionHandle = std::list<RegisteredAction>::iterator;
template <typename Func>
ActionHandle RegisterGuestAction(u32 syncpoint_id, u32 expected_value, Func&& action) {
2022-09-01 03:45:22 +00:00
std::function<void()> func(action);
2022-01-30 09:31:13 +00:00
return RegisterAction(syncpoints_guest[syncpoint_id], guest_action_storage[syncpoint_id],
2022-09-01 03:45:22 +00:00
expected_value, std::move(func));
2022-01-30 09:31:13 +00:00
}
template <typename Func>
ActionHandle RegisterHostAction(u32 syncpoint_id, u32 expected_value, Func&& action) {
2022-09-01 03:45:22 +00:00
std::function<void()> func(action);
2022-01-30 09:31:13 +00:00
return RegisterAction(syncpoints_host[syncpoint_id], host_action_storage[syncpoint_id],
2022-09-01 03:45:22 +00:00
expected_value, std::move(func));
2022-01-30 09:31:13 +00:00
}
2022-06-16 00:12:21 +00:00
void DeregisterGuestAction(u32 syncpoint_id, ActionHandle& handle);
2022-01-30 09:31:13 +00:00
2022-06-16 00:12:21 +00:00
void DeregisterHostAction(u32 syncpoint_id, ActionHandle& handle);
2022-01-30 09:31:13 +00:00
void IncrementGuest(u32 syncpoint_id);
void IncrementHost(u32 syncpoint_id);
void WaitGuest(u32 syncpoint_id, u32 expected_value);
void WaitHost(u32 syncpoint_id, u32 expected_value);
2022-09-01 03:45:22 +00:00
bool IsReadyGuest(u32 syncpoint_id, u32 expected_value) const {
2022-01-30 09:31:13 +00:00
return syncpoints_guest[syncpoint_id].load(std::memory_order_acquire) >= expected_value;
}
2022-09-01 03:45:22 +00:00
bool IsReadyHost(u32 syncpoint_id, u32 expected_value) const {
2022-01-30 09:31:13 +00:00
return syncpoints_host[syncpoint_id].load(std::memory_order_acquire) >= expected_value;
}
private:
void Increment(std::atomic<u32>& syncpoint, std::condition_variable& wait_cv,
std::list<RegisteredAction>& action_storage);
ActionHandle RegisterAction(std::atomic<u32>& syncpoint,
std::list<RegisteredAction>& action_storage, u32 expected_value,
2022-09-01 03:45:22 +00:00
std::function<void()>&& action);
2022-01-30 09:31:13 +00:00
void DeregisterAction(std::list<RegisteredAction>& action_storage, ActionHandle& handle);
void Wait(std::atomic<u32>& syncpoint, std::condition_variable& wait_cv, u32 expected_value);
static constexpr size_t NUM_MAX_SYNCPOINTS = 192;
std::array<std::atomic<u32>, NUM_MAX_SYNCPOINTS> syncpoints_guest{};
std::array<std::atomic<u32>, NUM_MAX_SYNCPOINTS> syncpoints_host{};
std::array<std::list<RegisteredAction>, NUM_MAX_SYNCPOINTS> guest_action_storage;
std::array<std::list<RegisteredAction>, NUM_MAX_SYNCPOINTS> host_action_storage;
std::mutex guard;
std::condition_variable wait_guest_cv;
std::condition_variable wait_host_cv;
};
} // namespace Host1x
} // namespace Tegra