xref: /xnu-8796.121.2/iokit/Kernel/IOPerfControl.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2017 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  */
4*c54f35caSApple OSS Distributions 
5*c54f35caSApple OSS Distributions #include <IOKit/perfcontrol/IOPerfControl.h>
6*c54f35caSApple OSS Distributions 
7*c54f35caSApple OSS Distributions #include <stdatomic.h>
8*c54f35caSApple OSS Distributions 
9*c54f35caSApple OSS Distributions #include <kern/thread_group.h>
10*c54f35caSApple OSS Distributions 
11*c54f35caSApple OSS Distributions #undef super
12*c54f35caSApple OSS Distributions #define super OSObject
13*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructors(IOPerfControlClient, OSObject);
14*c54f35caSApple OSS Distributions 
15*c54f35caSApple OSS Distributions static IOPerfControlClient::IOPerfControlClientShared *_Atomic gIOPerfControlClientShared;
16*c54f35caSApple OSS Distributions 
17*c54f35caSApple OSS Distributions bool
init(IOService * driver,uint64_t maxWorkCapacity)18*c54f35caSApple OSS Distributions IOPerfControlClient::init(IOService *driver, uint64_t maxWorkCapacity)
19*c54f35caSApple OSS Distributions {
20*c54f35caSApple OSS Distributions 	// TODO: Remove this limit and implement dynamic table growth if workloads are found that exceed this
21*c54f35caSApple OSS Distributions 	if (maxWorkCapacity > kMaxWorkTableNumEntries) {
22*c54f35caSApple OSS Distributions 		maxWorkCapacity = kMaxWorkTableNumEntries;
23*c54f35caSApple OSS Distributions 	}
24*c54f35caSApple OSS Distributions 
25*c54f35caSApple OSS Distributions 	if (!super::init()) {
26*c54f35caSApple OSS Distributions 		return false;
27*c54f35caSApple OSS Distributions 	}
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions 	shared = atomic_load_explicit(&gIOPerfControlClientShared, memory_order_acquire);
30*c54f35caSApple OSS Distributions 	if (shared == nullptr) {
31*c54f35caSApple OSS Distributions 		IOPerfControlClient::IOPerfControlClientShared *expected = shared;
32*c54f35caSApple OSS Distributions 		shared = kalloc_type(IOPerfControlClientShared, Z_WAITOK);
33*c54f35caSApple OSS Distributions 		if (!shared) {
34*c54f35caSApple OSS Distributions 			return false;
35*c54f35caSApple OSS Distributions 		}
36*c54f35caSApple OSS Distributions 
37*c54f35caSApple OSS Distributions 		atomic_init(&shared->maxDriverIndex, 0);
38*c54f35caSApple OSS Distributions 
39*c54f35caSApple OSS Distributions 		shared->interface = PerfControllerInterface{
40*c54f35caSApple OSS Distributions 			.version = PERFCONTROL_INTERFACE_VERSION_NONE,
41*c54f35caSApple OSS Distributions 			.registerDevice =
42*c54f35caSApple OSS Distributions 		    [](IOService *device) {
43*c54f35caSApple OSS Distributions 			    return kIOReturnSuccess;
44*c54f35caSApple OSS Distributions 		    },
45*c54f35caSApple OSS Distributions 			.unregisterDevice =
46*c54f35caSApple OSS Distributions 			    [](IOService *device) {
47*c54f35caSApple OSS Distributions 				    return kIOReturnSuccess;
48*c54f35caSApple OSS Distributions 			    },
49*c54f35caSApple OSS Distributions 			.workCanSubmit =
50*c54f35caSApple OSS Distributions 			    [](IOService *device, PerfControllerInterface::WorkState *state, WorkSubmitArgs *args) {
51*c54f35caSApple OSS Distributions 				    return false;
52*c54f35caSApple OSS Distributions 			    },
53*c54f35caSApple OSS Distributions 			.workSubmit =
54*c54f35caSApple OSS Distributions 			    [](IOService *device, uint64_t token, PerfControllerInterface::WorkState *state, WorkSubmitArgs *args) {
55*c54f35caSApple OSS Distributions 			    },
56*c54f35caSApple OSS Distributions 			.workBegin =
57*c54f35caSApple OSS Distributions 			    [](IOService *device, uint64_t token, PerfControllerInterface::WorkState *state, WorkBeginArgs *args) {
58*c54f35caSApple OSS Distributions 			    },
59*c54f35caSApple OSS Distributions 			.workEnd =
60*c54f35caSApple OSS Distributions 			    [](IOService *device, uint64_t token, PerfControllerInterface::WorkState *state, WorkEndArgs *args, bool done) {
61*c54f35caSApple OSS Distributions 			    },
62*c54f35caSApple OSS Distributions 			.workUpdate =
63*c54f35caSApple OSS Distributions 			    [](IOService *device, uint64_t token, PerfControllerInterface::WorkState *state, WorkUpdateArgs *args) {
64*c54f35caSApple OSS Distributions 			    },
65*c54f35caSApple OSS Distributions 		};
66*c54f35caSApple OSS Distributions 
67*c54f35caSApple OSS Distributions 		shared->interfaceLock = IOLockAlloc();
68*c54f35caSApple OSS Distributions 		if (!shared->interfaceLock) {
69*c54f35caSApple OSS Distributions 			goto shared_init_error;
70*c54f35caSApple OSS Distributions 		}
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions 		shared->deviceRegistrationList = OSSet::withCapacity(4);
73*c54f35caSApple OSS Distributions 		if (!shared->deviceRegistrationList) {
74*c54f35caSApple OSS Distributions 			goto shared_init_error;
75*c54f35caSApple OSS Distributions 		}
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions 		if (!atomic_compare_exchange_strong_explicit(&gIOPerfControlClientShared, &expected, shared, memory_order_acq_rel,
78*c54f35caSApple OSS Distributions 		    memory_order_acquire)) {
79*c54f35caSApple OSS Distributions 			IOLockFree(shared->interfaceLock);
80*c54f35caSApple OSS Distributions 			shared->deviceRegistrationList->release();
81*c54f35caSApple OSS Distributions 			kfree_type(IOPerfControlClientShared, shared);
82*c54f35caSApple OSS Distributions 			shared = expected;
83*c54f35caSApple OSS Distributions 		}
84*c54f35caSApple OSS Distributions 	}
85*c54f35caSApple OSS Distributions 	workTable = NULL;
86*c54f35caSApple OSS Distributions 	workTableLock = NULL;
87*c54f35caSApple OSS Distributions 
88*c54f35caSApple OSS Distributions 	// Note: driverIndex is not guaranteed to be unique if maxDriverIndex wraps around. It is intended for debugging only.
89*c54f35caSApple OSS Distributions 	driverIndex = atomic_fetch_add_explicit(&shared->maxDriverIndex, 1, memory_order_relaxed) + 1;
90*c54f35caSApple OSS Distributions 
91*c54f35caSApple OSS Distributions 	// + 1 since index 0 is unused for kIOPerfControlClientWorkUntracked
92*c54f35caSApple OSS Distributions 	workTableLength = maxWorkCapacity + 1;
93*c54f35caSApple OSS Distributions 	assertf(workTableLength <= kWorkTableMaxSize, "%zu exceeds max allowed capacity of %zu", workTableLength, kWorkTableMaxSize);
94*c54f35caSApple OSS Distributions 	if (maxWorkCapacity > 0) {
95*c54f35caSApple OSS Distributions 		workTable = kalloc_type(WorkTableEntry, workTableLength, Z_WAITOK_ZERO);
96*c54f35caSApple OSS Distributions 		if (!workTable) {
97*c54f35caSApple OSS Distributions 			goto error;
98*c54f35caSApple OSS Distributions 		}
99*c54f35caSApple OSS Distributions 		workTableNextIndex = 1;
100*c54f35caSApple OSS Distributions 
101*c54f35caSApple OSS Distributions 		workTableLock = IOSimpleLockAlloc();
102*c54f35caSApple OSS Distributions 		if (!workTableLock) {
103*c54f35caSApple OSS Distributions 			goto error;
104*c54f35caSApple OSS Distributions 		}
105*c54f35caSApple OSS Distributions 	}
106*c54f35caSApple OSS Distributions 
107*c54f35caSApple OSS Distributions 	bzero(&clientData, sizeof(clientData));
108*c54f35caSApple OSS Distributions 
109*c54f35caSApple OSS Distributions 	return true;
110*c54f35caSApple OSS Distributions 
111*c54f35caSApple OSS Distributions error:
112*c54f35caSApple OSS Distributions 	if (workTable) {
113*c54f35caSApple OSS Distributions 		kfree_type(WorkTableEntry, workTableLength, workTable);
114*c54f35caSApple OSS Distributions 		workTable = NULL;
115*c54f35caSApple OSS Distributions 	}
116*c54f35caSApple OSS Distributions 	if (workTableLock) {
117*c54f35caSApple OSS Distributions 		IOSimpleLockFree(workTableLock);
118*c54f35caSApple OSS Distributions 		workTableLock = NULL;
119*c54f35caSApple OSS Distributions 	}
120*c54f35caSApple OSS Distributions 	return false;
121*c54f35caSApple OSS Distributions shared_init_error:
122*c54f35caSApple OSS Distributions 	if (shared) {
123*c54f35caSApple OSS Distributions 		if (shared->interfaceLock) {
124*c54f35caSApple OSS Distributions 			IOLockFree(shared->interfaceLock);
125*c54f35caSApple OSS Distributions 		}
126*c54f35caSApple OSS Distributions 		if (shared->deviceRegistrationList) {
127*c54f35caSApple OSS Distributions 			shared->deviceRegistrationList->release();
128*c54f35caSApple OSS Distributions 		}
129*c54f35caSApple OSS Distributions 		kfree_type(IOPerfControlClientShared, shared);
130*c54f35caSApple OSS Distributions 		shared = nullptr;
131*c54f35caSApple OSS Distributions 	}
132*c54f35caSApple OSS Distributions 	return false;
133*c54f35caSApple OSS Distributions }
134*c54f35caSApple OSS Distributions 
135*c54f35caSApple OSS Distributions void
free()136*c54f35caSApple OSS Distributions IOPerfControlClient::free()
137*c54f35caSApple OSS Distributions {
138*c54f35caSApple OSS Distributions 	if (workTable) {
139*c54f35caSApple OSS Distributions 		kfree_type(WorkTableEntry, workTableLength, workTable);
140*c54f35caSApple OSS Distributions 	}
141*c54f35caSApple OSS Distributions 	if (workTableLock) {
142*c54f35caSApple OSS Distributions 		IOSimpleLockFree(workTableLock);
143*c54f35caSApple OSS Distributions 	}
144*c54f35caSApple OSS Distributions 	super::free();
145*c54f35caSApple OSS Distributions }
146*c54f35caSApple OSS Distributions 
147*c54f35caSApple OSS Distributions IOPerfControlClient *
copyClient(IOService * driver,uint64_t maxWorkCapacity)148*c54f35caSApple OSS Distributions IOPerfControlClient::copyClient(IOService *driver, uint64_t maxWorkCapacity)
149*c54f35caSApple OSS Distributions {
150*c54f35caSApple OSS Distributions 	IOPerfControlClient *client = new IOPerfControlClient;
151*c54f35caSApple OSS Distributions 	if (!client || !client->init(driver, maxWorkCapacity)) {
152*c54f35caSApple OSS Distributions 		panic("could not create IOPerfControlClient");
153*c54f35caSApple OSS Distributions 	}
154*c54f35caSApple OSS Distributions 	return client;
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions 
157*c54f35caSApple OSS Distributions /* Convert the per driver token into a globally unique token for the performance
158*c54f35caSApple OSS Distributions  * controller's consumption. This is achieved by setting the driver's unique
159*c54f35caSApple OSS Distributions  * index onto the high order bits. The performance controller is shared between
160*c54f35caSApple OSS Distributions  * all drivers and must track all instances separately, while each driver has
161*c54f35caSApple OSS Distributions  * its own token table, so this step is needed to avoid token collisions between
162*c54f35caSApple OSS Distributions  * drivers.
163*c54f35caSApple OSS Distributions  */
164*c54f35caSApple OSS Distributions inline uint64_t
tokenToGlobalUniqueToken(uint64_t token)165*c54f35caSApple OSS Distributions IOPerfControlClient::tokenToGlobalUniqueToken(uint64_t token)
166*c54f35caSApple OSS Distributions {
167*c54f35caSApple OSS Distributions 	return token | (static_cast<uint64_t>(driverIndex) << kWorkTableIndexBits);
168*c54f35caSApple OSS Distributions }
169*c54f35caSApple OSS Distributions 
170*c54f35caSApple OSS Distributions /* With this implementation, tokens returned to the driver differ from tokens
171*c54f35caSApple OSS Distributions  * passed to the performance controller. This implementation has the nice
172*c54f35caSApple OSS Distributions  * property that tokens returns to the driver will aways be between 1 and
173*c54f35caSApple OSS Distributions  * the value of maxWorkCapacity passed by the driver to copyClient. The tokens
174*c54f35caSApple OSS Distributions  * the performance controller sees will match on the lower order bits and have
175*c54f35caSApple OSS Distributions  * the driver index set on the high order bits.
176*c54f35caSApple OSS Distributions  */
177*c54f35caSApple OSS Distributions uint64_t
allocateToken(thread_group * thread_group)178*c54f35caSApple OSS Distributions IOPerfControlClient::allocateToken(thread_group *thread_group)
179*c54f35caSApple OSS Distributions {
180*c54f35caSApple OSS Distributions 	uint64_t token = kIOPerfControlClientWorkUntracked;
181*c54f35caSApple OSS Distributions 
182*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
183*c54f35caSApple OSS Distributions 	auto s = IOSimpleLockLockDisableInterrupt(workTableLock);
184*c54f35caSApple OSS Distributions 
185*c54f35caSApple OSS Distributions 	uint64_t num_tries = 0;
186*c54f35caSApple OSS Distributions 	size_t index = workTableNextIndex;
187*c54f35caSApple OSS Distributions 	// - 1 since entry 0 is for kIOPerfControlClientWorkUntracked
188*c54f35caSApple OSS Distributions 	while (num_tries < workTableLength - 1) {
189*c54f35caSApple OSS Distributions 		if (workTable[index].thread_group == nullptr) {
190*c54f35caSApple OSS Distributions 			thread_group_retain(thread_group);
191*c54f35caSApple OSS Distributions 			workTable[index].thread_group = thread_group;
192*c54f35caSApple OSS Distributions 			token = index;
193*c54f35caSApple OSS Distributions 			// next integer between 1 and workTableLength - 1
194*c54f35caSApple OSS Distributions 			workTableNextIndex = (index % (workTableLength - 1)) + 1;
195*c54f35caSApple OSS Distributions 			break;
196*c54f35caSApple OSS Distributions 		}
197*c54f35caSApple OSS Distributions 		// next integer between 1 and workTableLength - 1
198*c54f35caSApple OSS Distributions 		index = (index % (workTableLength - 1)) + 1;
199*c54f35caSApple OSS Distributions 		num_tries += 1;
200*c54f35caSApple OSS Distributions 	}
201*c54f35caSApple OSS Distributions #if (DEVELOPMENT || DEBUG)
202*c54f35caSApple OSS Distributions 	if (token == kIOPerfControlClientWorkUntracked) {
203*c54f35caSApple OSS Distributions 		/* When investigating a panic here, first check that the driver is not leaking tokens.
204*c54f35caSApple OSS Distributions 		 * If the driver is not leaking tokens and maximum is less than kMaxWorkTableNumEntries,
205*c54f35caSApple OSS Distributions 		 * the driver should be modified to pass a larger value to copyClient.
206*c54f35caSApple OSS Distributions 		 * If the driver is not leaking tokens and maximum is equal to kMaxWorkTableNumEntries,
207*c54f35caSApple OSS Distributions 		 * this code will have to be modified to support dynamic table growth to support larger
208*c54f35caSApple OSS Distributions 		 * numbers of tokens.
209*c54f35caSApple OSS Distributions 		 */
210*c54f35caSApple OSS Distributions 		panic("Tokens allocated for this device exceeded maximum of %zu.",
211*c54f35caSApple OSS Distributions 		    workTableLength - 1); // - 1 since entry 0 is for kIOPerfControlClientWorkUntracked
212*c54f35caSApple OSS Distributions 	}
213*c54f35caSApple OSS Distributions #endif
214*c54f35caSApple OSS Distributions 
215*c54f35caSApple OSS Distributions 	IOSimpleLockUnlockEnableInterrupt(workTableLock, s);
216*c54f35caSApple OSS Distributions #endif
217*c54f35caSApple OSS Distributions 
218*c54f35caSApple OSS Distributions 	return token;
219*c54f35caSApple OSS Distributions }
220*c54f35caSApple OSS Distributions 
221*c54f35caSApple OSS Distributions void
deallocateToken(uint64_t token)222*c54f35caSApple OSS Distributions IOPerfControlClient::deallocateToken(uint64_t token)
223*c54f35caSApple OSS Distributions {
224*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
225*c54f35caSApple OSS Distributions 	assertf(token != kIOPerfControlClientWorkUntracked, "Attempt to deallocate token kIOPerfControlClientWorkUntracked\n");
226*c54f35caSApple OSS Distributions 	assertf(token <= workTableLength, "Attempt to deallocate token %llu which is greater than the table size of %zu\n", token, workTableLength);
227*c54f35caSApple OSS Distributions 	auto s = IOSimpleLockLockDisableInterrupt(workTableLock);
228*c54f35caSApple OSS Distributions 
229*c54f35caSApple OSS Distributions 	auto &entry = workTable[token];
230*c54f35caSApple OSS Distributions 	auto *thread_group = entry.thread_group;
231*c54f35caSApple OSS Distributions 	bzero(&entry, sizeof(entry));
232*c54f35caSApple OSS Distributions 	workTableNextIndex = token;
233*c54f35caSApple OSS Distributions 
234*c54f35caSApple OSS Distributions 	IOSimpleLockUnlockEnableInterrupt(workTableLock, s);
235*c54f35caSApple OSS Distributions 
236*c54f35caSApple OSS Distributions 	// This can call into the performance controller if the last reference is dropped here. Are we sure
237*c54f35caSApple OSS Distributions 	// the driver isn't holding any locks? If not, we may want to async this to another context.
238*c54f35caSApple OSS Distributions 	thread_group_release(thread_group);
239*c54f35caSApple OSS Distributions #endif
240*c54f35caSApple OSS Distributions }
241*c54f35caSApple OSS Distributions 
242*c54f35caSApple OSS Distributions IOPerfControlClient::WorkTableEntry *
getEntryForToken(uint64_t token)243*c54f35caSApple OSS Distributions IOPerfControlClient::getEntryForToken(uint64_t token)
244*c54f35caSApple OSS Distributions {
245*c54f35caSApple OSS Distributions 	if (token == kIOPerfControlClientWorkUntracked) {
246*c54f35caSApple OSS Distributions 		return nullptr;
247*c54f35caSApple OSS Distributions 	}
248*c54f35caSApple OSS Distributions 
249*c54f35caSApple OSS Distributions 	if (token >= workTableLength) {
250*c54f35caSApple OSS Distributions 		panic("Invalid work token (%llu): index out of bounds.", token);
251*c54f35caSApple OSS Distributions 	}
252*c54f35caSApple OSS Distributions 
253*c54f35caSApple OSS Distributions 	WorkTableEntry *entry = &workTable[token];
254*c54f35caSApple OSS Distributions 	assertf(entry->thread_group, "Invalid work token: %llu", token);
255*c54f35caSApple OSS Distributions 	return entry;
256*c54f35caSApple OSS Distributions }
257*c54f35caSApple OSS Distributions 
258*c54f35caSApple OSS Distributions void
markEntryStarted(uint64_t token,bool started)259*c54f35caSApple OSS Distributions IOPerfControlClient::markEntryStarted(uint64_t token, bool started)
260*c54f35caSApple OSS Distributions {
261*c54f35caSApple OSS Distributions 	if (token == kIOPerfControlClientWorkUntracked) {
262*c54f35caSApple OSS Distributions 		return;
263*c54f35caSApple OSS Distributions 	}
264*c54f35caSApple OSS Distributions 
265*c54f35caSApple OSS Distributions 	if (token >= workTableLength) {
266*c54f35caSApple OSS Distributions 		panic("Invalid work token (%llu): index out of bounds.", token);
267*c54f35caSApple OSS Distributions 	}
268*c54f35caSApple OSS Distributions 
269*c54f35caSApple OSS Distributions 	workTable[token].started = started;
270*c54f35caSApple OSS Distributions }
271*c54f35caSApple OSS Distributions 
272*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
273*c54f35caSApple OSS Distributions 
274*c54f35caSApple OSS Distributions static struct thread_group *
threadGroupForDextService(IOService * device)275*c54f35caSApple OSS Distributions threadGroupForDextService(IOService *device)
276*c54f35caSApple OSS Distributions {
277*c54f35caSApple OSS Distributions 	assert(device);
278*c54f35caSApple OSS Distributions 
279*c54f35caSApple OSS Distributions 	if (!device->hasUserServer()) {
280*c54f35caSApple OSS Distributions 		return NULL;
281*c54f35caSApple OSS Distributions 	}
282*c54f35caSApple OSS Distributions 
283*c54f35caSApple OSS Distributions 	// Devices associated with a dext driver, must be called from dext
284*c54f35caSApple OSS Distributions 	// context to ensure that thread_group reference is valid.
285*c54f35caSApple OSS Distributions 	thread_t thread = current_thread();
286*c54f35caSApple OSS Distributions 	assert(get_threadtask(thread) != kernel_task);
287*c54f35caSApple OSS Distributions 	struct thread_group * thread_group = thread_group_get(thread);
288*c54f35caSApple OSS Distributions 	assert(thread_group != nullptr);
289*c54f35caSApple OSS Distributions 	return thread_group;
290*c54f35caSApple OSS Distributions }
291*c54f35caSApple OSS Distributions 
292*c54f35caSApple OSS Distributions #endif /* CONFIG_THREAD_GROUPS */
293*c54f35caSApple OSS Distributions 
294*c54f35caSApple OSS Distributions IOReturn
registerDevice(IOService * driver,IOService * device)295*c54f35caSApple OSS Distributions IOPerfControlClient::registerDevice(IOService *driver, IOService *device)
296*c54f35caSApple OSS Distributions {
297*c54f35caSApple OSS Distributions 	IOReturn ret = kIOReturnSuccess;
298*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
299*c54f35caSApple OSS Distributions 	IOLockLock(shared->interfaceLock);
300*c54f35caSApple OSS Distributions 
301*c54f35caSApple OSS Distributions 	clientData.device = device;
302*c54f35caSApple OSS Distributions 
303*c54f35caSApple OSS Distributions 	if (device) {
304*c54f35caSApple OSS Distributions 		struct thread_group *dext_thread_group = threadGroupForDextService(device);
305*c54f35caSApple OSS Distributions 		if (dext_thread_group) {
306*c54f35caSApple OSS Distributions 			if (clientData.driverState.has_target_thread_group) {
307*c54f35caSApple OSS Distributions 				panic("driverState has already been initialized");
308*c54f35caSApple OSS Distributions 			}
309*c54f35caSApple OSS Distributions 			clientData.driverState.has_target_thread_group = true;
310*c54f35caSApple OSS Distributions 			clientData.driverState.target_thread_group_id = thread_group_get_id(dext_thread_group);
311*c54f35caSApple OSS Distributions 			clientData.driverState.target_thread_group_data = thread_group_get_machine_data(dext_thread_group);
312*c54f35caSApple OSS Distributions 
313*c54f35caSApple OSS Distributions 			clientData.target_thread_group = dext_thread_group;
314*c54f35caSApple OSS Distributions 			thread_group_retain(dext_thread_group);
315*c54f35caSApple OSS Distributions 		}
316*c54f35caSApple OSS Distributions 	}
317*c54f35caSApple OSS Distributions 
318*c54f35caSApple OSS Distributions 	if (shared->interface.version >= PERFCONTROL_INTERFACE_VERSION_3) {
319*c54f35caSApple OSS Distributions 		ret = shared->interface.registerDriverDevice(driver, device, &clientData.driverState);
320*c54f35caSApple OSS Distributions 	} else if (shared->interface.version >= PERFCONTROL_INTERFACE_VERSION_1) {
321*c54f35caSApple OSS Distributions 		ret = shared->interface.registerDevice(device);
322*c54f35caSApple OSS Distributions 	} else {
323*c54f35caSApple OSS Distributions 		shared->deviceRegistrationList->setObject(this);
324*c54f35caSApple OSS Distributions 	}
325*c54f35caSApple OSS Distributions 
326*c54f35caSApple OSS Distributions 	IOLockUnlock(shared->interfaceLock);
327*c54f35caSApple OSS Distributions #endif
328*c54f35caSApple OSS Distributions 	return ret;
329*c54f35caSApple OSS Distributions }
330*c54f35caSApple OSS Distributions 
331*c54f35caSApple OSS Distributions void
unregisterDevice(IOService * driver,IOService * device)332*c54f35caSApple OSS Distributions IOPerfControlClient::unregisterDevice(IOService *driver, IOService *device)
333*c54f35caSApple OSS Distributions {
334*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
335*c54f35caSApple OSS Distributions 	IOLockLock(shared->interfaceLock);
336*c54f35caSApple OSS Distributions 
337*c54f35caSApple OSS Distributions 	if (shared->interface.version >= PERFCONTROL_INTERFACE_VERSION_3) {
338*c54f35caSApple OSS Distributions 		shared->interface.unregisterDriverDevice(driver, device, &clientData.driverState);
339*c54f35caSApple OSS Distributions 	} else if (shared->interface.version >= PERFCONTROL_INTERFACE_VERSION_1) {
340*c54f35caSApple OSS Distributions 		shared->interface.unregisterDevice(device);
341*c54f35caSApple OSS Distributions 	} else {
342*c54f35caSApple OSS Distributions 		shared->deviceRegistrationList->removeObject(this);
343*c54f35caSApple OSS Distributions 	}
344*c54f35caSApple OSS Distributions 
345*c54f35caSApple OSS Distributions 	if (clientData.driverState.has_target_thread_group) {
346*c54f35caSApple OSS Distributions 		thread_group_release(clientData.target_thread_group);
347*c54f35caSApple OSS Distributions 		clientData.target_thread_group = nullptr;
348*c54f35caSApple OSS Distributions 
349*c54f35caSApple OSS Distributions 		clientData.driverState.has_target_thread_group = false;
350*c54f35caSApple OSS Distributions 		clientData.driverState.target_thread_group_id = ~0ull;
351*c54f35caSApple OSS Distributions 		clientData.driverState.target_thread_group_data = nullptr;
352*c54f35caSApple OSS Distributions 	}
353*c54f35caSApple OSS Distributions 
354*c54f35caSApple OSS Distributions 	clientData.device = nullptr;
355*c54f35caSApple OSS Distributions 
356*c54f35caSApple OSS Distributions 	IOLockUnlock(shared->interfaceLock);
357*c54f35caSApple OSS Distributions #endif
358*c54f35caSApple OSS Distributions }
359*c54f35caSApple OSS Distributions 
360*c54f35caSApple OSS Distributions uint64_t
workSubmit(IOService * device,WorkSubmitArgs * args)361*c54f35caSApple OSS Distributions IOPerfControlClient::workSubmit(IOService *device, WorkSubmitArgs *args)
362*c54f35caSApple OSS Distributions {
363*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
364*c54f35caSApple OSS Distributions 	auto *thread_group = thread_group_get(current_thread());
365*c54f35caSApple OSS Distributions 	if (!thread_group) {
366*c54f35caSApple OSS Distributions 		return kIOPerfControlClientWorkUntracked;
367*c54f35caSApple OSS Distributions 	}
368*c54f35caSApple OSS Distributions 
369*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
370*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(thread_group),
371*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(thread_group),
372*c54f35caSApple OSS Distributions 		.work_data = nullptr,
373*c54f35caSApple OSS Distributions 		.work_data_size = 0,
374*c54f35caSApple OSS Distributions 		.started = false,
375*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
376*c54f35caSApple OSS Distributions 	};
377*c54f35caSApple OSS Distributions 	if (!shared->interface.workCanSubmit(device, &state, args)) {
378*c54f35caSApple OSS Distributions 		return kIOPerfControlClientWorkUntracked;
379*c54f35caSApple OSS Distributions 	}
380*c54f35caSApple OSS Distributions 
381*c54f35caSApple OSS Distributions 	uint64_t token = allocateToken(thread_group);
382*c54f35caSApple OSS Distributions 	if (token != kIOPerfControlClientWorkUntracked) {
383*c54f35caSApple OSS Distributions 		state.work_data = &workTable[token].perfcontrol_data;
384*c54f35caSApple OSS Distributions 		state.work_data_size = sizeof(workTable[token].perfcontrol_data);
385*c54f35caSApple OSS Distributions 		shared->interface.workSubmit(device, tokenToGlobalUniqueToken(token), &state, args);
386*c54f35caSApple OSS Distributions 	}
387*c54f35caSApple OSS Distributions 	return token;
388*c54f35caSApple OSS Distributions #else
389*c54f35caSApple OSS Distributions 	return kIOPerfControlClientWorkUntracked;
390*c54f35caSApple OSS Distributions #endif
391*c54f35caSApple OSS Distributions }
392*c54f35caSApple OSS Distributions 
393*c54f35caSApple OSS Distributions uint64_t
workSubmitAndBegin(IOService * device,WorkSubmitArgs * submitArgs,WorkBeginArgs * beginArgs)394*c54f35caSApple OSS Distributions IOPerfControlClient::workSubmitAndBegin(IOService *device, WorkSubmitArgs *submitArgs, WorkBeginArgs *beginArgs)
395*c54f35caSApple OSS Distributions {
396*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
397*c54f35caSApple OSS Distributions 	auto *thread_group = thread_group_get(current_thread());
398*c54f35caSApple OSS Distributions 	if (!thread_group) {
399*c54f35caSApple OSS Distributions 		return kIOPerfControlClientWorkUntracked;
400*c54f35caSApple OSS Distributions 	}
401*c54f35caSApple OSS Distributions 
402*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
403*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(thread_group),
404*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(thread_group),
405*c54f35caSApple OSS Distributions 		.work_data = nullptr,
406*c54f35caSApple OSS Distributions 		.work_data_size = 0,
407*c54f35caSApple OSS Distributions 		.started = false,
408*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
409*c54f35caSApple OSS Distributions 	};
410*c54f35caSApple OSS Distributions 	if (!shared->interface.workCanSubmit(device, &state, submitArgs)) {
411*c54f35caSApple OSS Distributions 		return kIOPerfControlClientWorkUntracked;
412*c54f35caSApple OSS Distributions 	}
413*c54f35caSApple OSS Distributions 
414*c54f35caSApple OSS Distributions 	uint64_t token = allocateToken(thread_group);
415*c54f35caSApple OSS Distributions 	if (token != kIOPerfControlClientWorkUntracked) {
416*c54f35caSApple OSS Distributions 		auto &entry = workTable[token];
417*c54f35caSApple OSS Distributions 		state.work_data = &entry.perfcontrol_data;
418*c54f35caSApple OSS Distributions 		state.work_data_size = sizeof(workTable[token].perfcontrol_data);
419*c54f35caSApple OSS Distributions 		shared->interface.workSubmit(device, tokenToGlobalUniqueToken(token), &state, submitArgs);
420*c54f35caSApple OSS Distributions 		state.started = true;
421*c54f35caSApple OSS Distributions 		shared->interface.workBegin(device, tokenToGlobalUniqueToken(token), &state, beginArgs);
422*c54f35caSApple OSS Distributions 		markEntryStarted(token, true);
423*c54f35caSApple OSS Distributions 	}
424*c54f35caSApple OSS Distributions 	return token;
425*c54f35caSApple OSS Distributions #else
426*c54f35caSApple OSS Distributions 	return kIOPerfControlClientWorkUntracked;
427*c54f35caSApple OSS Distributions #endif
428*c54f35caSApple OSS Distributions }
429*c54f35caSApple OSS Distributions 
430*c54f35caSApple OSS Distributions void
workBegin(IOService * device,uint64_t token,WorkBeginArgs * args)431*c54f35caSApple OSS Distributions IOPerfControlClient::workBegin(IOService *device, uint64_t token, WorkBeginArgs *args)
432*c54f35caSApple OSS Distributions {
433*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
434*c54f35caSApple OSS Distributions 	WorkTableEntry *entry = getEntryForToken(token);
435*c54f35caSApple OSS Distributions 	if (entry == nullptr) {
436*c54f35caSApple OSS Distributions 		return;
437*c54f35caSApple OSS Distributions 	}
438*c54f35caSApple OSS Distributions 
439*c54f35caSApple OSS Distributions 	assertf(!entry->started, "Work for token %llu was already started", token);
440*c54f35caSApple OSS Distributions 
441*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
442*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(entry->thread_group),
443*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(entry->thread_group),
444*c54f35caSApple OSS Distributions 		.work_data = &entry->perfcontrol_data,
445*c54f35caSApple OSS Distributions 		.work_data_size = sizeof(entry->perfcontrol_data),
446*c54f35caSApple OSS Distributions 		.started = true,
447*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
448*c54f35caSApple OSS Distributions 	};
449*c54f35caSApple OSS Distributions 	shared->interface.workBegin(device, tokenToGlobalUniqueToken(token), &state, args);
450*c54f35caSApple OSS Distributions 	markEntryStarted(token, true);
451*c54f35caSApple OSS Distributions #endif
452*c54f35caSApple OSS Distributions }
453*c54f35caSApple OSS Distributions 
454*c54f35caSApple OSS Distributions void
workEnd(IOService * device,uint64_t token,WorkEndArgs * args,bool done)455*c54f35caSApple OSS Distributions IOPerfControlClient::workEnd(IOService *device, uint64_t token, WorkEndArgs *args, bool done)
456*c54f35caSApple OSS Distributions {
457*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
458*c54f35caSApple OSS Distributions 	WorkTableEntry *entry = getEntryForToken(token);
459*c54f35caSApple OSS Distributions 	if (entry == nullptr) {
460*c54f35caSApple OSS Distributions 		return;
461*c54f35caSApple OSS Distributions 	}
462*c54f35caSApple OSS Distributions 
463*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
464*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(entry->thread_group),
465*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(entry->thread_group),
466*c54f35caSApple OSS Distributions 		.work_data = &entry->perfcontrol_data,
467*c54f35caSApple OSS Distributions 		.work_data_size = sizeof(entry->perfcontrol_data),
468*c54f35caSApple OSS Distributions 		.started = entry->started,
469*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
470*c54f35caSApple OSS Distributions 	};
471*c54f35caSApple OSS Distributions 	shared->interface.workEnd(device, tokenToGlobalUniqueToken(token), &state, args, done);
472*c54f35caSApple OSS Distributions 
473*c54f35caSApple OSS Distributions 	if (done) {
474*c54f35caSApple OSS Distributions 		deallocateToken(token);
475*c54f35caSApple OSS Distributions 	} else {
476*c54f35caSApple OSS Distributions 		markEntryStarted(token, false);
477*c54f35caSApple OSS Distributions 	}
478*c54f35caSApple OSS Distributions #endif
479*c54f35caSApple OSS Distributions }
480*c54f35caSApple OSS Distributions 
481*c54f35caSApple OSS Distributions static _Atomic uint64_t unique_work_context_id = 1ull;
482*c54f35caSApple OSS Distributions 
483*c54f35caSApple OSS Distributions class IOPerfControlWorkContext : public OSObject
484*c54f35caSApple OSS Distributions {
485*c54f35caSApple OSS Distributions 	OSDeclareDefaultStructors(IOPerfControlWorkContext);
486*c54f35caSApple OSS Distributions 
487*c54f35caSApple OSS Distributions public:
488*c54f35caSApple OSS Distributions 	uint64_t id;
489*c54f35caSApple OSS Distributions 	struct thread_group *thread_group;
490*c54f35caSApple OSS Distributions 	bool started;
491*c54f35caSApple OSS Distributions 	uint8_t perfcontrol_data[32];
492*c54f35caSApple OSS Distributions 
493*c54f35caSApple OSS Distributions 	bool init() override;
494*c54f35caSApple OSS Distributions 	void reset();
495*c54f35caSApple OSS Distributions 	void free() override;
496*c54f35caSApple OSS Distributions };
497*c54f35caSApple OSS Distributions 
498*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructors(IOPerfControlWorkContext, OSObject);
499*c54f35caSApple OSS Distributions 
500*c54f35caSApple OSS Distributions bool
init()501*c54f35caSApple OSS Distributions IOPerfControlWorkContext::init()
502*c54f35caSApple OSS Distributions {
503*c54f35caSApple OSS Distributions 	if (!super::init()) {
504*c54f35caSApple OSS Distributions 		return false;
505*c54f35caSApple OSS Distributions 	}
506*c54f35caSApple OSS Distributions 	id = atomic_fetch_add_explicit(&unique_work_context_id, 1, memory_order_relaxed) + 1;
507*c54f35caSApple OSS Distributions 	reset();
508*c54f35caSApple OSS Distributions 	return true;
509*c54f35caSApple OSS Distributions }
510*c54f35caSApple OSS Distributions 
511*c54f35caSApple OSS Distributions void
reset()512*c54f35caSApple OSS Distributions IOPerfControlWorkContext::reset()
513*c54f35caSApple OSS Distributions {
514*c54f35caSApple OSS Distributions 	thread_group = nullptr;
515*c54f35caSApple OSS Distributions 	started = false;
516*c54f35caSApple OSS Distributions 	bzero(perfcontrol_data, sizeof(perfcontrol_data));
517*c54f35caSApple OSS Distributions }
518*c54f35caSApple OSS Distributions 
519*c54f35caSApple OSS Distributions void
free()520*c54f35caSApple OSS Distributions IOPerfControlWorkContext::free()
521*c54f35caSApple OSS Distributions {
522*c54f35caSApple OSS Distributions 	assertf(thread_group == nullptr, "IOPerfControlWorkContext ID %llu being released without calling workEnd!\n", id);
523*c54f35caSApple OSS Distributions 	super::free();
524*c54f35caSApple OSS Distributions }
525*c54f35caSApple OSS Distributions 
526*c54f35caSApple OSS Distributions OSObject *
copyWorkContext()527*c54f35caSApple OSS Distributions IOPerfControlClient::copyWorkContext()
528*c54f35caSApple OSS Distributions {
529*c54f35caSApple OSS Distributions 	IOPerfControlWorkContext *context = new IOPerfControlWorkContext;
530*c54f35caSApple OSS Distributions 
531*c54f35caSApple OSS Distributions 	if (context == nullptr) {
532*c54f35caSApple OSS Distributions 		return nullptr;
533*c54f35caSApple OSS Distributions 	}
534*c54f35caSApple OSS Distributions 
535*c54f35caSApple OSS Distributions 	if (!context->init()) {
536*c54f35caSApple OSS Distributions 		context->free();
537*c54f35caSApple OSS Distributions 		return nullptr;
538*c54f35caSApple OSS Distributions 	}
539*c54f35caSApple OSS Distributions 
540*c54f35caSApple OSS Distributions 	return context;
541*c54f35caSApple OSS Distributions }
542*c54f35caSApple OSS Distributions 
543*c54f35caSApple OSS Distributions bool
workSubmitAndBeginWithContext(IOService * device,OSObject * context,WorkSubmitArgs * submitArgs,WorkBeginArgs * beginArgs)544*c54f35caSApple OSS Distributions IOPerfControlClient::workSubmitAndBeginWithContext(IOService *device, OSObject *context, WorkSubmitArgs *submitArgs, WorkBeginArgs *beginArgs)
545*c54f35caSApple OSS Distributions {
546*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
547*c54f35caSApple OSS Distributions 
548*c54f35caSApple OSS Distributions 	if (workSubmitWithContext(device, context, submitArgs) == false) {
549*c54f35caSApple OSS Distributions 		return false;
550*c54f35caSApple OSS Distributions 	}
551*c54f35caSApple OSS Distributions 
552*c54f35caSApple OSS Distributions 	IOPerfControlWorkContext *work_context = OSDynamicCast(IOPerfControlWorkContext, context);
553*c54f35caSApple OSS Distributions 
554*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
555*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(work_context->thread_group),
556*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(work_context->thread_group),
557*c54f35caSApple OSS Distributions 		.work_data = &work_context->perfcontrol_data,
558*c54f35caSApple OSS Distributions 		.work_data_size = sizeof(work_context->perfcontrol_data),
559*c54f35caSApple OSS Distributions 		.started = true,
560*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
561*c54f35caSApple OSS Distributions 	};
562*c54f35caSApple OSS Distributions 
563*c54f35caSApple OSS Distributions 	shared->interface.workBegin(device, work_context->id, &state, beginArgs);
564*c54f35caSApple OSS Distributions 
565*c54f35caSApple OSS Distributions 	work_context->started = true;
566*c54f35caSApple OSS Distributions 
567*c54f35caSApple OSS Distributions 	return true;
568*c54f35caSApple OSS Distributions #else
569*c54f35caSApple OSS Distributions 	return false;
570*c54f35caSApple OSS Distributions #endif
571*c54f35caSApple OSS Distributions }
572*c54f35caSApple OSS Distributions 
573*c54f35caSApple OSS Distributions bool
workSubmitWithContext(IOService * device,OSObject * context,WorkSubmitArgs * args)574*c54f35caSApple OSS Distributions IOPerfControlClient::workSubmitWithContext(IOService *device, OSObject *context, WorkSubmitArgs *args)
575*c54f35caSApple OSS Distributions {
576*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
577*c54f35caSApple OSS Distributions 	IOPerfControlWorkContext *work_context = OSDynamicCast(IOPerfControlWorkContext, context);
578*c54f35caSApple OSS Distributions 
579*c54f35caSApple OSS Distributions 	if (work_context == nullptr) {
580*c54f35caSApple OSS Distributions 		return false;
581*c54f35caSApple OSS Distributions 	}
582*c54f35caSApple OSS Distributions 
583*c54f35caSApple OSS Distributions 	auto *thread_group = thread_group_get(current_thread());
584*c54f35caSApple OSS Distributions 	assert(thread_group != nullptr);
585*c54f35caSApple OSS Distributions 
586*c54f35caSApple OSS Distributions 	assertf(!work_context->started, "IOPerfControlWorkContext ID %llu was already started", work_context->id);
587*c54f35caSApple OSS Distributions 	assertf(work_context->thread_group == nullptr, "IOPerfControlWorkContext ID %llu has already taken a refcount on TG 0x%p \n", work_context->id, (void *)(work_context->thread_group));
588*c54f35caSApple OSS Distributions 
589*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
590*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(thread_group),
591*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(thread_group),
592*c54f35caSApple OSS Distributions 		.work_data = nullptr,
593*c54f35caSApple OSS Distributions 		.work_data_size = 0,
594*c54f35caSApple OSS Distributions 		.started = false,
595*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
596*c54f35caSApple OSS Distributions 	};
597*c54f35caSApple OSS Distributions 	if (!shared->interface.workCanSubmit(device, &state, args)) {
598*c54f35caSApple OSS Distributions 		return false;
599*c54f35caSApple OSS Distributions 	}
600*c54f35caSApple OSS Distributions 
601*c54f35caSApple OSS Distributions 	work_context->thread_group = thread_group_retain(thread_group);
602*c54f35caSApple OSS Distributions 
603*c54f35caSApple OSS Distributions 	state.work_data = &work_context->perfcontrol_data;
604*c54f35caSApple OSS Distributions 	state.work_data_size = sizeof(work_context->perfcontrol_data);
605*c54f35caSApple OSS Distributions 
606*c54f35caSApple OSS Distributions 	shared->interface.workSubmit(device, work_context->id, &state, args);
607*c54f35caSApple OSS Distributions 
608*c54f35caSApple OSS Distributions 	return true;
609*c54f35caSApple OSS Distributions #else
610*c54f35caSApple OSS Distributions 	return false;
611*c54f35caSApple OSS Distributions #endif
612*c54f35caSApple OSS Distributions }
613*c54f35caSApple OSS Distributions 
614*c54f35caSApple OSS Distributions void
workUpdateWithContext(IOService * device,OSObject * context,WorkUpdateArgs * args)615*c54f35caSApple OSS Distributions IOPerfControlClient::workUpdateWithContext(IOService *device, OSObject *context, WorkUpdateArgs *args)
616*c54f35caSApple OSS Distributions {
617*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
618*c54f35caSApple OSS Distributions 	IOPerfControlWorkContext *work_context = OSDynamicCast(IOPerfControlWorkContext, context);
619*c54f35caSApple OSS Distributions 
620*c54f35caSApple OSS Distributions 	if (work_context == nullptr) {
621*c54f35caSApple OSS Distributions 		return;
622*c54f35caSApple OSS Distributions 	}
623*c54f35caSApple OSS Distributions 
624*c54f35caSApple OSS Distributions 	if (work_context->thread_group == nullptr) {
625*c54f35caSApple OSS Distributions 		// This Work Context has not taken a refcount on a TG
626*c54f35caSApple OSS Distributions 		return;
627*c54f35caSApple OSS Distributions 	}
628*c54f35caSApple OSS Distributions 
629*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
630*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(work_context->thread_group),
631*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(work_context->thread_group),
632*c54f35caSApple OSS Distributions 		.work_data = &work_context->perfcontrol_data,
633*c54f35caSApple OSS Distributions 		.work_data_size = sizeof(work_context->perfcontrol_data),
634*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
635*c54f35caSApple OSS Distributions 	};
636*c54f35caSApple OSS Distributions 	shared->interface.workUpdate(device, work_context->id, &state, args);
637*c54f35caSApple OSS Distributions #endif
638*c54f35caSApple OSS Distributions }
639*c54f35caSApple OSS Distributions 
640*c54f35caSApple OSS Distributions void
workBeginWithContext(IOService * device,OSObject * context,WorkBeginArgs * args)641*c54f35caSApple OSS Distributions IOPerfControlClient::workBeginWithContext(IOService *device, OSObject *context, WorkBeginArgs *args)
642*c54f35caSApple OSS Distributions {
643*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
644*c54f35caSApple OSS Distributions 	IOPerfControlWorkContext *work_context = OSDynamicCast(IOPerfControlWorkContext, context);
645*c54f35caSApple OSS Distributions 
646*c54f35caSApple OSS Distributions 	if (work_context == nullptr) {
647*c54f35caSApple OSS Distributions 		return;
648*c54f35caSApple OSS Distributions 	}
649*c54f35caSApple OSS Distributions 
650*c54f35caSApple OSS Distributions 	if (work_context->thread_group == nullptr) {
651*c54f35caSApple OSS Distributions 		// This Work Context has not taken a refcount on a TG
652*c54f35caSApple OSS Distributions 		return;
653*c54f35caSApple OSS Distributions 	}
654*c54f35caSApple OSS Distributions 
655*c54f35caSApple OSS Distributions 	assertf(!work_context->started, "IOPerfControlWorkContext %llu was already started", work_context->id);
656*c54f35caSApple OSS Distributions 
657*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
658*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(work_context->thread_group),
659*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(work_context->thread_group),
660*c54f35caSApple OSS Distributions 		.work_data = &work_context->perfcontrol_data,
661*c54f35caSApple OSS Distributions 		.work_data_size = sizeof(work_context->perfcontrol_data),
662*c54f35caSApple OSS Distributions 		.started = true,
663*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
664*c54f35caSApple OSS Distributions 	};
665*c54f35caSApple OSS Distributions 	shared->interface.workBegin(device, work_context->id, &state, args);
666*c54f35caSApple OSS Distributions 
667*c54f35caSApple OSS Distributions 	work_context->started = true;
668*c54f35caSApple OSS Distributions #endif
669*c54f35caSApple OSS Distributions }
670*c54f35caSApple OSS Distributions 
671*c54f35caSApple OSS Distributions void
workEndWithContext(IOService * device,OSObject * context,WorkEndArgs * args,bool done)672*c54f35caSApple OSS Distributions IOPerfControlClient::workEndWithContext(IOService *device, OSObject *context, WorkEndArgs *args, bool done)
673*c54f35caSApple OSS Distributions {
674*c54f35caSApple OSS Distributions #if CONFIG_THREAD_GROUPS
675*c54f35caSApple OSS Distributions 	IOPerfControlWorkContext *work_context = OSDynamicCast(IOPerfControlWorkContext, context);
676*c54f35caSApple OSS Distributions 
677*c54f35caSApple OSS Distributions 	if (work_context == nullptr) {
678*c54f35caSApple OSS Distributions 		return;
679*c54f35caSApple OSS Distributions 	}
680*c54f35caSApple OSS Distributions 
681*c54f35caSApple OSS Distributions 	if (work_context->thread_group == nullptr) {
682*c54f35caSApple OSS Distributions 		return;
683*c54f35caSApple OSS Distributions 	}
684*c54f35caSApple OSS Distributions 
685*c54f35caSApple OSS Distributions 	PerfControllerInterface::WorkState state{
686*c54f35caSApple OSS Distributions 		.thread_group_id = thread_group_get_id(work_context->thread_group),
687*c54f35caSApple OSS Distributions 		.thread_group_data = thread_group_get_machine_data(work_context->thread_group),
688*c54f35caSApple OSS Distributions 		.work_data = &work_context->perfcontrol_data,
689*c54f35caSApple OSS Distributions 		.work_data_size = sizeof(work_context->perfcontrol_data),
690*c54f35caSApple OSS Distributions 		.started = work_context->started,
691*c54f35caSApple OSS Distributions 		.driver_state = &clientData.driverState
692*c54f35caSApple OSS Distributions 	};
693*c54f35caSApple OSS Distributions 
694*c54f35caSApple OSS Distributions 	shared->interface.workEnd(device, work_context->id, &state, args, done);
695*c54f35caSApple OSS Distributions 
696*c54f35caSApple OSS Distributions 	if (done) {
697*c54f35caSApple OSS Distributions 		thread_group_release(work_context->thread_group);
698*c54f35caSApple OSS Distributions 		work_context->reset();
699*c54f35caSApple OSS Distributions 	} else {
700*c54f35caSApple OSS Distributions 		work_context->started = false;
701*c54f35caSApple OSS Distributions 	}
702*c54f35caSApple OSS Distributions 
703*c54f35caSApple OSS Distributions 	return;
704*c54f35caSApple OSS Distributions #else
705*c54f35caSApple OSS Distributions 	return;
706*c54f35caSApple OSS Distributions #endif
707*c54f35caSApple OSS Distributions }
708*c54f35caSApple OSS Distributions 
709*c54f35caSApple OSS Distributions IOReturn
registerPerformanceController(PerfControllerInterface * pci)710*c54f35caSApple OSS Distributions IOPerfControlClient::registerPerformanceController(PerfControllerInterface *pci)
711*c54f35caSApple OSS Distributions {
712*c54f35caSApple OSS Distributions 	IOReturn result = kIOReturnError;
713*c54f35caSApple OSS Distributions 
714*c54f35caSApple OSS Distributions 	IOLockLock(shared->interfaceLock);
715*c54f35caSApple OSS Distributions 
716*c54f35caSApple OSS Distributions 	if (shared->interface.version == PERFCONTROL_INTERFACE_VERSION_NONE) {
717*c54f35caSApple OSS Distributions 		shared->interface.version = pci->version;
718*c54f35caSApple OSS Distributions 
719*c54f35caSApple OSS Distributions 		if (pci->version >= PERFCONTROL_INTERFACE_VERSION_1) {
720*c54f35caSApple OSS Distributions 			assert(pci->registerDevice && pci->unregisterDevice && pci->workCanSubmit && pci->workSubmit && pci->workBegin && pci->workEnd);
721*c54f35caSApple OSS Distributions 			shared->interface.registerDevice = pci->registerDevice;
722*c54f35caSApple OSS Distributions 			shared->interface.unregisterDevice = pci->unregisterDevice;
723*c54f35caSApple OSS Distributions 			shared->interface.workCanSubmit = pci->workCanSubmit;
724*c54f35caSApple OSS Distributions 			shared->interface.workSubmit = pci->workSubmit;
725*c54f35caSApple OSS Distributions 			shared->interface.workBegin = pci->workBegin;
726*c54f35caSApple OSS Distributions 			shared->interface.workEnd = pci->workEnd;
727*c54f35caSApple OSS Distributions 		}
728*c54f35caSApple OSS Distributions 
729*c54f35caSApple OSS Distributions 		if (pci->version >= PERFCONTROL_INTERFACE_VERSION_2) {
730*c54f35caSApple OSS Distributions 			if (pci->workUpdate != nullptr) {
731*c54f35caSApple OSS Distributions 				shared->interface.workUpdate = pci->workUpdate;
732*c54f35caSApple OSS Distributions 			}
733*c54f35caSApple OSS Distributions 		}
734*c54f35caSApple OSS Distributions 
735*c54f35caSApple OSS Distributions 		if (pci->version >= PERFCONTROL_INTERFACE_VERSION_3) {
736*c54f35caSApple OSS Distributions 			assert(pci->registerDriverDevice && pci->unregisterDriverDevice);
737*c54f35caSApple OSS Distributions 			shared->interface.registerDriverDevice = pci->registerDriverDevice;
738*c54f35caSApple OSS Distributions 			shared->interface.unregisterDriverDevice = pci->unregisterDriverDevice;
739*c54f35caSApple OSS Distributions 		}
740*c54f35caSApple OSS Distributions 
741*c54f35caSApple OSS Distributions 		result = kIOReturnSuccess;
742*c54f35caSApple OSS Distributions 
743*c54f35caSApple OSS Distributions 		OSObject *obj;
744*c54f35caSApple OSS Distributions 		while ((obj = shared->deviceRegistrationList->getAnyObject())) {
745*c54f35caSApple OSS Distributions 			IOPerfControlClient *client = OSDynamicCast(IOPerfControlClient, obj);
746*c54f35caSApple OSS Distributions 			IOPerfControlClientData *clientData = client->getClientData();
747*c54f35caSApple OSS Distributions 			if (clientData && clientData->device) {
748*c54f35caSApple OSS Distributions 				if (pci->version >= PERFCONTROL_INTERFACE_VERSION_3) {
749*c54f35caSApple OSS Distributions 					pci->registerDriverDevice(clientData->device->getProvider(), clientData->device, &(clientData->driverState));
750*c54f35caSApple OSS Distributions 				} else if (pci->version >= PERFCONTROL_INTERFACE_VERSION_1) {
751*c54f35caSApple OSS Distributions 					pci->registerDevice(clientData->device);
752*c54f35caSApple OSS Distributions 				}
753*c54f35caSApple OSS Distributions 			}
754*c54f35caSApple OSS Distributions 			shared->deviceRegistrationList->removeObject(obj);
755*c54f35caSApple OSS Distributions 		}
756*c54f35caSApple OSS Distributions 	}
757*c54f35caSApple OSS Distributions 
758*c54f35caSApple OSS Distributions 	IOLockUnlock(shared->interfaceLock);
759*c54f35caSApple OSS Distributions 
760*c54f35caSApple OSS Distributions 	return result;
761*c54f35caSApple OSS Distributions }
762