-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathkern_igfx_pm.cpp
More file actions
328 lines (265 loc) · 10.3 KB
/
Copy pathkern_igfx_pm.cpp
File metadata and controls
328 lines (265 loc) · 10.3 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// kern_igfx_pm.cpp
// WhateverGreen
//
// Created by Pb on 22/06/2020.
// Copyright © 2020 vit9696. All rights reserved.
//
/*
* Portions Copyright © 2013 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <IOKit/IOService.h>
#include <Headers/kern_patcher.hpp>
#include <Headers/kern_devinfo.hpp>
#include <Headers/kern_cpu.hpp>
#include <Headers/kern_disasm.hpp>
#include <IOKit/IOLib.h>
#include <IOKit/IOMessage.h>
#include <mach/clock.h>
#include "kern_igfx.hpp"
namespace {
constexpr const char* log = "igfx_pm";
// For debugging
struct [[gnu::packed]] IGHwCsDesc {
char type;
char gap[4];
char *title;
char unk0[48];
char unk1[12];
};
constexpr uint32_t MCHBAR_MIRROR_BASE_SNB = 0x140000;
constexpr uint32_t GEN6_RP_STATE_CAP = MCHBAR_MIRROR_BASE_SNB + 0x5998;
constexpr uint32_t GEN9_FREQUENCY_SHIFT = 23;
constexpr uint32_t GEN9_FREQ_SCALER = 3;
constexpr uint32_t FORCEWAKE_KERNEL_FALLBACK = 1 << 15;
constexpr uint32_t FORCEWAKE_ACK_TIMEOUT_MS = 50;
constexpr uint32_t FORCEWAKE_MEDIA_GEN9 = 0xa270;
constexpr uint32_t FORCEWAKE_RENDER_GEN9 = 0xa278;
constexpr uint32_t FORCEWAKE_BLITTER_GEN9 = 0xa188;
constexpr uint32_t FORCEWAKE_ACK_MEDIA_GEN9 = 0x0D88;
constexpr uint32_t FORCEWAKE_ACK_RENDER_GEN9 = 0x0D84;
constexpr uint32_t FORCEWAKE_ACK_BLITTER_GEN9 = 0x130044;
enum FORCEWAKE_DOM_BITS : unsigned {
DOM_RENDER = 0b001,
DOM_MEDIA = 0b010,
DOM_BLITTER = 0b100,
DOM_LAST = DOM_BLITTER,
DOM_FIRST = DOM_RENDER
};
constexpr uint32_t regForDom(unsigned d) {
if (d == DOM_RENDER)
return FORCEWAKE_RENDER_GEN9;
if (d == DOM_MEDIA)
return FORCEWAKE_MEDIA_GEN9;
if (d == DOM_BLITTER)
return FORCEWAKE_BLITTER_GEN9;
assertf(false, "Unknown force wake domain %d", d);
return 0;
}
constexpr uint32_t ackForDom(unsigned d) {
if (d == DOM_RENDER)
return FORCEWAKE_ACK_RENDER_GEN9;
if (d == DOM_MEDIA)
return FORCEWAKE_ACK_MEDIA_GEN9;
if (d == DOM_BLITTER)
return FORCEWAKE_ACK_BLITTER_GEN9;
assertf(false, "Unknown force wake domain %d", d);
return 0;
}
constexpr const char* const strForDom(unsigned d) {
if (d == DOM_RENDER)
return "Render";
if (d == DOM_MEDIA)
return "Media";
if (d == DOM_BLITTER)
return "Blitter";
return "(unk)";
}
constexpr uint32_t masked_field(uint32_t mask, uint32_t value) {
return (mask << 16) | value;
}
constexpr uint32_t fw_set(uint32_t v) {
return masked_field(v, v);
}
constexpr uint32_t fw_clear(uint32_t v) {
return masked_field(v, 0);
}
}
// MARK: - RPS Control Patch
void IGFX::RPSControlPatch::init() {
// We need to patch both drivers
requiresPatchingGraphics = true;
requiresPatchingFramebuffer = true;
// Requires access to global framebuffer controllers
requiresGlobalFramebufferControllersAccess = true;
// Requires read access to MMIO registers
requiresMMIORegistersReadAccess = true;
}
void IGFX::RPSControlPatch::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
uint32_t rpsc = 0;
if (PE_parse_boot_argn("igfxrpsc", &rpsc, sizeof(rpsc)) ||
WIOKit::getOSDataValue(info->videoBuiltin, "rps-control", rpsc)) {
enabled = rpsc > 0 && available;
DBGLOG("weg", "RPS control patch overriden (%u) availabile %d", rpsc, available);
}
}
void IGFX::RPSControlPatch::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::RouteRequest routeRequest = {
"__ZL15pmNotifyWrapperjjPyPj",
wrapPmNotifyWrapper,
orgPmNotifyWrapper
};
if (!patcher.routeMultiple(index, &routeRequest, 1, address, size))
SYSLOG(log, "Failed to route pmNotifyWrapper.");
}
void IGFX::RPSControlPatch::processGraphicsKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
// Address of `IGHardwareCommandStreamer2::submitExecList`
mach_vm_address_t orgSubmitExecList;
KernelPatcher::SolveRequest request = {
getKernelVersion() >= KernelVersion::Catalina ? "__ZN26IGHardwareCommandStreamer514submitExecListEj" : "__ZN26IGHardwareCommandStreamer214submitExecListEj",
orgSubmitExecList
};
if (!patcher.solveMultiple(index, &request, 1, address, size)) {
SYSLOG(log, "Failed to solve the symbol for submitExecList.");
return;
}
// `submitExecList()` only controls RPS for RCS type streamers
// Patch it to enable control for any kind of streamer
if (!patchRCSCheck(orgSubmitExecList))
SYSLOG(log, "Failed to patch RCS check.");
}
int IGFX::RPSControlPatch::wrapPmNotifyWrapper(unsigned int a0, unsigned int a1, unsigned long long *a2, unsigned int *freq) {
// Request the maximum RPS at exec list submission
// While this sounds dangerous, we are still getting proper power management due to force wake clears.
uint32_t cfreq = 0;
callbackIGFX->modRPSControlPatch.orgPmNotifyWrapper(a0, a1, a2, &cfreq);
if (!callbackIGFX->modRPSControlPatch.freq_max) {
callbackIGFX->modRPSControlPatch.freq_max = callbackIGFX->readRegister32(callbackIGFX->defaultController(), GEN6_RP_STATE_CAP) & 0xFF;
DBGLOG("log", "Read RP0 %d", callbackIGFX->modRPSControlPatch.freq_max);
}
*freq = (GEN9_FREQ_SCALER << GEN9_FREQUENCY_SHIFT) * callbackIGFX->modRPSControlPatch.freq_max;
return 0;
}
bool IGFX::RPSControlPatch::patchRCSCheck(mach_vm_address_t& start) {
constexpr unsigned ninsts_max {256};
hde64s dis;
bool found_cmp = false;
bool found_jmp = false;
for (size_t i = 0; i < ninsts_max; i++) {
auto sz = Disassembler::hdeDisasm(start, &dis);
if (dis.flags & F_ERROR) {
SYSLOG(log, "Error disassembling submitExecList");
break;
}
/* cmp byte ptr [rcx], 0 */
if (!found_cmp && dis.opcode == 0x80 && dis.modrm_reg == 7 && dis.modrm_rm == 1)
found_cmp = true;
/* jnz rel32 */
if (found_cmp && dis.opcode == 0x0f && dis.opcode2 == 0x85) {
found_jmp = true;
break;
}
start += sz;
}
if (found_jmp) {
auto status = MachInfo::setKernelWriting(true, KernelPatcher::kernelWriteLock);
if (status == KERN_SUCCESS) {
constexpr uint8_t nop6[] {0x90, 0x90, 0x90, 0x90, 0x90, 0x90};
lilu_os_memcpy(reinterpret_cast<void*>(start), nop6, arrsize(nop6));
MachInfo::setKernelWriting(false, KernelPatcher::kernelWriteLock);
DBGLOG(log, "Patched submitExecList");
return true;
} else {
DBGLOG(log, "Failed to set kernel writing");
return false;
}
} else {
SYSLOG(log, "jnz in submitExecList not found");
return false;
}
}
// MARK: - Force Wake Workaround
bool IGFX::ForceWakeWorkaround::pollRegister(uint32_t reg, uint32_t val, uint32_t mask, uint32_t timeout) {
AbsoluteTime now, deadline;
clock_interval_to_deadline(timeout, kMillisecondScale, &deadline);
for (clock_get_uptime(&now); now < deadline; clock_get_uptime(&now)) {
auto rd = callbackIGFX->readRegister32(callbackIGFX->defaultController(), reg);
// DBGLOG(log, "Rd 0x%x = 0x%x, expected 0x%x", reg, rd, val);
if ((rd & mask) == val)
return true;
}
return false;
}
bool IGFX::ForceWakeWorkaround::forceWakeWaitAckFallback(uint32_t d, uint32_t val, uint32_t mask) {
unsigned pass = 1;
bool ack = false;
auto controller = callbackIGFX->defaultController();
do {
pollRegister(ackForDom(d), 0, FORCEWAKE_KERNEL_FALLBACK, FORCEWAKE_ACK_TIMEOUT_MS);
callbackIGFX->writeRegister32(controller, regForDom(d), fw_set(FORCEWAKE_KERNEL_FALLBACK));
IODelay(10 * pass);
pollRegister(ackForDom(d), FORCEWAKE_KERNEL_FALLBACK, FORCEWAKE_KERNEL_FALLBACK, FORCEWAKE_ACK_TIMEOUT_MS);
ack = (callbackIGFX->readRegister32(controller, ackForDom(d)) & mask) == val;
callbackIGFX->writeRegister32(controller, regForDom(d), fw_clear(FORCEWAKE_KERNEL_FALLBACK));
} while (!ack && pass++ < 10);
// DBGLOG(log, "Force wake fallback used to %s %s in %u passes", set ? "set" : "clear", strForDom(d), pass);
return ack;
}
/**
* Port of i915 force wake. The difference with Apple code is as follows:
* 1. 50 ms ACK timeouts, see https://patchwork.kernel.org/patch/7057561/
* Apple code uses 90 ms.
* 2. Use reserve bit as a fallback at primary ACK timeout, see https://patchwork.kernel.org/patch/10029821/
*/
// NOTE: We are either in IRQ context, or in a spinlock critical section
void IGFX::ForceWakeWorkaround::forceWake(void*, uint8_t set, uint32_t dom, uint32_t ctx) {
// ctx 2: IRQ, 1: normal
uint32_t ack_exp = set << ctx;
uint32_t mask = 1 << ctx;
uint32_t wr = ack_exp | (1 << ctx << 16);
for (unsigned d = DOM_FIRST; d <= DOM_LAST; d <<= 1)
if (dom & d) {
callbackIGFX->writeRegister32(callbackIGFX->defaultController(), regForDom(d), wr);
IOPause(100);
if (!pollRegister(ackForDom(d), ack_exp, mask, FORCEWAKE_ACK_TIMEOUT_MS) &&
!forceWakeWaitAckFallback(d, ack_exp, mask) &&
!pollRegister(ackForDom(d), ack_exp, mask, FORCEWAKE_ACK_TIMEOUT_MS))
PANIC(log, "ForceWake timeout for domain %s, expected 0x%x", strForDom(dom), ack_exp);
}
}
void IGFX::ForceWakeWorkaround::init() {
// We only need to patch the acceleration driver
requiresPatchingGraphics = true;
// Requires access to global framebuffer controllers
requiresGlobalFramebufferControllersAccess = true;
// Requires read and write access to MMIO registers
requiresMMIORegistersReadAccess = true;
requiresMMIORegistersWriteAccess = true;
}
void IGFX::ForceWakeWorkaround::processGraphicsKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::RouteRequest request = {
"__ZN16IntelAccelerator26SafeForceWakeMultithreadedEbjj",
forceWake
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "Failed to route SafeForceWake.");
}