syncpoint_manager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv3 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <condition_variable>
  8. #include <functional>
  9. #include <list>
  10. #include <mutex>
  11. #include "common/common_types.h"
  12. namespace Tegra {
  13. namespace Host1x {
  14. class SyncpointManager {
  15. public:
  16. u32 GetGuestSyncpointValue(u32 id) {
  17. return syncpoints_guest[id].load(std::memory_order_acquire);
  18. }
  19. u32 GetHostSyncpointValue(u32 id) {
  20. return syncpoints_host[id].load(std::memory_order_acquire);
  21. }
  22. struct RegisteredAction {
  23. RegisteredAction(u32 expected_value_, std::function<void(void)>& action_)
  24. : expected_value{expected_value_}, action{action_} {}
  25. u32 expected_value;
  26. std::function<void(void)> action;
  27. };
  28. using ActionHandle = std::list<RegisteredAction>::iterator;
  29. template <typename Func>
  30. ActionHandle RegisterGuestAction(u32 syncpoint_id, u32 expected_value, Func&& action) {
  31. std::function<void(void)> func(action);
  32. return RegisterAction(syncpoints_guest[syncpoint_id], guest_action_storage[syncpoint_id],
  33. expected_value, func);
  34. }
  35. template <typename Func>
  36. ActionHandle RegisterHostAction(u32 syncpoint_id, u32 expected_value, Func&& action) {
  37. std::function<void(void)> func(action);
  38. return RegisterAction(syncpoints_host[syncpoint_id], host_action_storage[syncpoint_id],
  39. expected_value, func);
  40. }
  41. void DeregisterGuestAction(u32 syncpoint_id,ActionHandle& handle);
  42. void DeregisterHostAction(u32 syncpoint_id,ActionHandle& handle);
  43. void IncrementGuest(u32 syncpoint_id);
  44. void IncrementHost(u32 syncpoint_id);
  45. void WaitGuest(u32 syncpoint_id, u32 expected_value);
  46. void WaitHost(u32 syncpoint_id, u32 expected_value);
  47. bool IsReadyGuest(u32 syncpoint_id, u32 expected_value) {
  48. return syncpoints_guest[syncpoint_id].load(std::memory_order_acquire) >= expected_value;
  49. }
  50. bool IsReadyHost(u32 syncpoint_id, u32 expected_value) {
  51. return syncpoints_host[syncpoint_id].load(std::memory_order_acquire) >= expected_value;
  52. }
  53. private:
  54. void Increment(std::atomic<u32>& syncpoint, std::condition_variable& wait_cv,
  55. std::list<RegisteredAction>& action_storage);
  56. ActionHandle RegisterAction(std::atomic<u32>& syncpoint,
  57. std::list<RegisteredAction>& action_storage, u32 expected_value,
  58. std::function<void(void)>& action);
  59. void DeregisterAction(std::list<RegisteredAction>& action_storage, ActionHandle& handle);
  60. void Wait(std::atomic<u32>& syncpoint, std::condition_variable& wait_cv, u32 expected_value);
  61. static constexpr size_t NUM_MAX_SYNCPOINTS = 192;
  62. std::array<std::atomic<u32>, NUM_MAX_SYNCPOINTS> syncpoints_guest{};
  63. std::array<std::atomic<u32>, NUM_MAX_SYNCPOINTS> syncpoints_host{};
  64. std::array<std::list<RegisteredAction>, NUM_MAX_SYNCPOINTS> guest_action_storage;
  65. std::array<std::list<RegisteredAction>, NUM_MAX_SYNCPOINTS> host_action_storage;
  66. std::mutex guard;
  67. std::condition_variable wait_guest_cv;
  68. std::condition_variable wait_host_cv;
  69. };
  70. } // namespace Host1x
  71. } // namespace Tegra