The Pedigree Project 0.1
VmwareSvgaState.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef VMWARE_SVGA_STATE_H
9#define VMWARE_SVGA_STATE_H
10
11#include "pedigree/kernel/processor/types.h"
12
13namespace VmwareSvgaState {
14constexpr uint64_t FifoSyncTimeout = 1000000000ULL;
15constexpr size_t FifoSyncPollLimit = 10000000;
16
18 public:
19 explicit PollBudget(uint64_t started, uint64_t timeout = FifoSyncTimeout,
20 size_t pollLimit = FifoSyncPollLimit)
21 : m_Started(started), m_Timeout(timeout), m_PollLimit(pollLimit), m_Polls(0) {}
22
23 bool keepPolling(uint64_t now) {
24 if (m_Polls >= m_PollLimit)
25 return false;
26
27 ++m_Polls;
28 return (m_Polls < m_PollLimit) && ((now - m_Started) < m_Timeout);
29 }
30
31 size_t polls() const {
32 return m_Polls;
33 }
34
35 private:
36 uint64_t m_Started;
37 uint64_t m_Timeout;
38 size_t m_PollLimit;
39 size_t m_Polls;
40};
41
42struct FifoLayout {
43 uint32_t min;
44 uint32_t max;
45 uint32_t next;
46 uint32_t stop;
47};
48
49inline bool valid(const FifoLayout& layout) {
50 constexpr uint32_t alignmentMask = sizeof(uint32_t) - 1;
51 return !(layout.min & alignmentMask) && !(layout.max & alignmentMask) &&
52 !(layout.next & alignmentMask) && !(layout.stop & alignmentMask) &&
53 (layout.max > layout.min) && ((layout.max - layout.min) >= (2 * sizeof(uint32_t))) &&
54 (layout.next >= layout.min) && (layout.next < layout.max) && (layout.stop >= layout.min) &&
55 (layout.stop < layout.max);
56}
57
58inline bool valid(const FifoLayout& layout, size_t headerFloor, size_t apertureBytes) {
59 return valid(layout) && (layout.min >= headerFloor) && (layout.max <= apertureBytes);
60}
61
62inline size_t freeBytes(const FifoLayout& layout) {
63 if (!valid(layout))
64 return 0;
65
66 if (layout.next >= layout.stop) {
67 return (layout.max - layout.next) + (layout.stop - layout.min) - sizeof(uint32_t);
68 }
69
70 return layout.stop - layout.next - sizeof(uint32_t);
71}
72
73inline bool canFit(const FifoLayout& layout, size_t words) {
74 if (!words || (words > (~static_cast<size_t>(0) / sizeof(uint32_t))))
75 return false;
76
77 return (words * sizeof(uint32_t)) <= freeBytes(layout);
78}
79} // namespace VmwareSvgaState
80
81#endif