Repository navigation
Expand file tree
/
Copy pathPVGFramebufferBootstrap.cpp
More file actions
215 lines (188 loc) · 8.26 KB
/
Copy pathPVGFramebufferBootstrap.cpp
File metadata and controls
215 lines (188 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <IOKit/IOService.h>
#include <IOKit/IOTimerEventSource.h>
#include <IOKit/IOWorkLoop.h>
#include <IOKit/graphics/IOFramebuffer.h>
#include <libkern/c++/OSNumber.h>
// AppleParavirtGPU's exported framebuffer method. The prototype is recovered
// from the Big Sur 20G1426 x86_64 symbol and implementation. The provider
// object is created by AppleParavirtGPU, not by this compatibility service.
class AppleParavirtFramebuffer : public IOFramebuffer {
public:
IOReturn connectionChange(
uint32_t connection,
uint16_t vendor,
uint16_t product,
const char *model,
uint32_t modeCount,
void (^modeAtIndex)(uint32_t index, uint16_t *width,
uint16_t *height, uint32_t *refresh));
};
class PVGFramebufferBootstrap : public IOService {
OSDeclareDefaultStructors(PVGFramebufferBootstrap)
public:
bool start(IOService *provider) override;
void stop(IOService *provider) override;
private:
static void modeRepairTimerAction(OSObject *owner,
IOTimerEventSource *sender);
void repairMode(IOTimerEventSource *sender);
IOTimerEventSource *modeRepairTimer_ = nullptr;
IOWorkLoop *modeRepairWorkLoop_ = nullptr;
uint32_t modeRepairAttempts_ = 0;
};
OSDefineMetaClassAndStructors(PVGFramebufferBootstrap, IOService)
namespace {
struct Mode {
uint16_t width;
uint16_t height;
};
// Catalina's WindowServer asks Metal for a display-sized composite texture.
// Advertising the donor's maximum 3360x2100 mode makes that allocation fail
// and WindowServer abort. Start with the host console's 1024x768 mode; dynamic
// host-size negotiation can be restored separately after first presentation.
constexpr Mode kFallbackModes[] = {
{1024, 768},
};
constexpr uint32_t kRefresh60Hz16_16 = 60U << 16;
} // namespace
bool PVGFramebufferBootstrap::start(IOService *provider) {
if (!IOService::start(provider)) {
return false;
}
// Re-announcing an existing fallback table is intentional. The first
// announcement can happen before IOGraphics registers its connection
// interrupt callback; a later invocation then supplies the missed edge.
const bool reannouncing =
provider->getProperty("IOFBMemorySize") != nullptr ||
provider->getProperty("IOFBDetailedTimings") != nullptr;
auto *framebuffer = static_cast<AppleParavirtFramebuffer *>(provider);
IOReturn result = framebuffer->connectionChange(
0, 0x1006, 0, "Apple Virtual Display",
static_cast<uint32_t>(sizeof(kFallbackModes) / sizeof(kFallbackModes[0])),
^(uint32_t index, uint16_t *width, uint16_t *height,
uint32_t *refresh) {
if (index >= sizeof(kFallbackModes) / sizeof(kFallbackModes[0])) {
return;
}
if (width != nullptr) {
*width = kFallbackModes[index].width;
}
if (height != nullptr) {
*height = kFallbackModes[index].height;
}
if (refresh != nullptr) {
*refresh = kRefresh60Hz16_16;
}
});
// IOGraphics installs IOFBDetailedTimings only after this start method
// returns. Schedule the mode validation on the framebuffer's work loop so
// it runs as soon as that timing array becomes visible.
modeRepairWorkLoop_ = framebuffer->getWorkLoop();
modeRepairTimer_ = IOTimerEventSource::timerEventSource(
this, &PVGFramebufferBootstrap::modeRepairTimerAction);
if (modeRepairWorkLoop_ == nullptr || modeRepairTimer_ == nullptr ||
modeRepairWorkLoop_->addEventSource(modeRepairTimer_) !=
kIOReturnSuccess) {
setProperty("PVGFramebufferModeRepairScheduled", false);
if (modeRepairTimer_ != nullptr) {
modeRepairTimer_->release();
modeRepairTimer_ = nullptr;
}
} else {
setProperty("PVGFramebufferModeRepairScheduled", true);
modeRepairTimer_->setTimeoutMS(1);
}
setProperty("PVGFramebufferBootstrapResult",
static_cast<uint64_t>(result), 32);
setProperty("PVGFramebufferBootstrapModeCount",
static_cast<uint64_t>(sizeof(kFallbackModes) /
sizeof(kFallbackModes[0])), 32);
setProperty("PVGFramebufferBootstrapReannounced", reannouncing);
registerService();
return result == kIOReturnSuccess;
}
void PVGFramebufferBootstrap::modeRepairTimerAction(
OSObject *owner, IOTimerEventSource *sender) {
static_cast<PVGFramebufferBootstrap *>(owner)->repairMode(sender);
}
void PVGFramebufferBootstrap::repairMode(IOTimerEventSource *sender) {
constexpr uint32_t kRetryDelayMS = 10;
constexpr uint32_t kMaximumAttempts = 500;
++modeRepairAttempts_;
setProperty("PVGFramebufferModeRepairAttempts",
static_cast<uint64_t>(modeRepairAttempts_), 32);
auto *framebuffer =
static_cast<AppleParavirtFramebuffer *>(getProvider());
if (framebuffer == nullptr) {
setProperty("PVGFramebufferModeRepairProviderMissing", true);
return;
}
// AppleParavirtFramebuffer::start initializes both the startup and current
// mode IDs to -1. The donor's getDisplayModes implementation exposes the
// detailed-timing array indices verbatim (0..count-1).
const uint32_t displayModeCount = framebuffer->getDisplayModeCount();
setProperty("PVGFramebufferDisplayModeCount",
static_cast<uint64_t>(displayModeCount), 32);
if (displayModeCount == 0) {
if (modeRepairAttempts_ < kMaximumAttempts) {
sender->setTimeoutMS(kRetryDelayMS);
} else {
setProperty("PVGFramebufferModeRepairTimedOut", true);
}
return;
}
IODisplayModeID currentMode = static_cast<IODisplayModeID>(-1);
IOIndex currentDepth = 0;
IOPixelInformation pixelInfo = {};
IOReturn currentModeResult =
framebuffer->getCurrentDisplayMode(¤tMode, ¤tDepth);
IOReturn pixelInfoResult = framebuffer->getPixelInformation(
currentMode, currentDepth, 0, &pixelInfo);
setProperty("PVGFramebufferCurrentModeBefore",
static_cast<uint64_t>(currentMode), 32);
setProperty("PVGFramebufferCurrentDepthBefore",
static_cast<uint64_t>(currentDepth), 32);
setProperty("PVGFramebufferCurrentModeResultBefore",
static_cast<uint64_t>(currentModeResult), 32);
setProperty("PVGFramebufferPixelInfoResultBefore",
static_cast<uint64_t>(pixelInfoResult), 32);
IOReturn startupModeResult = kIOReturnSuccess;
IOReturn setModeResult = kIOReturnSuccess;
if (pixelInfoResult != kIOReturnSuccess) {
startupModeResult = framebuffer->setStartupDisplayMode(0, 2);
setModeResult = framebuffer->setDisplayMode(0, 2);
currentMode = static_cast<IODisplayModeID>(-1);
currentDepth = 0;
pixelInfo = {};
currentModeResult =
framebuffer->getCurrentDisplayMode(¤tMode, ¤tDepth);
pixelInfoResult = framebuffer->getPixelInformation(
currentMode, currentDepth, 0, &pixelInfo);
}
setProperty("PVGFramebufferStartupModeResult",
static_cast<uint64_t>(startupModeResult), 32);
setProperty("PVGFramebufferSetModeResult",
static_cast<uint64_t>(setModeResult), 32);
setProperty("PVGFramebufferCurrentModeAfter",
static_cast<uint64_t>(currentMode), 32);
setProperty("PVGFramebufferCurrentDepthAfter",
static_cast<uint64_t>(currentDepth), 32);
setProperty("PVGFramebufferCurrentModeResultAfter",
static_cast<uint64_t>(currentModeResult), 32);
setProperty("PVGFramebufferPixelInfoResultAfter",
static_cast<uint64_t>(pixelInfoResult), 32);
setProperty("PVGFramebufferModeRepairComplete",
pixelInfoResult == kIOReturnSuccess);
}
void PVGFramebufferBootstrap::stop(IOService *provider) {
if (modeRepairTimer_ != nullptr) {
modeRepairTimer_->cancelTimeout();
if (modeRepairWorkLoop_ != nullptr) {
modeRepairWorkLoop_->removeEventSource(modeRepairTimer_);
}
modeRepairTimer_->release();
modeRepairTimer_ = nullptr;
}
modeRepairWorkLoop_ = nullptr;
IOService::stop(provider);
}