xref: /xnu-10063.121.3/iokit/IOKit/perfcontrol/IOPerfControl.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2017 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  */
4*2c2f96dcSApple OSS Distributions 
5*2c2f96dcSApple OSS Distributions #pragma once
6*2c2f96dcSApple OSS Distributions 
7*2c2f96dcSApple OSS Distributions #ifdef KERNEL_PRIVATE
8*2c2f96dcSApple OSS Distributions #ifdef __cplusplus
9*2c2f96dcSApple OSS Distributions 
10*2c2f96dcSApple OSS Distributions #include <IOKit/IOService.h>
11*2c2f96dcSApple OSS Distributions #include <stdatomic.h>
12*2c2f96dcSApple OSS Distributions #include <kern/bits.h>
13*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSPtr.h>
14*2c2f96dcSApple OSS Distributions 
15*2c2f96dcSApple OSS Distributions struct thread_group;
16*2c2f96dcSApple OSS Distributions 
17*2c2f96dcSApple OSS Distributions enum{
18*2c2f96dcSApple OSS Distributions 	kIOPerfControlClientWorkUntracked = 0,
19*2c2f96dcSApple OSS Distributions };
20*2c2f96dcSApple OSS Distributions 
21*2c2f96dcSApple OSS Distributions /*!
22*2c2f96dcSApple OSS Distributions  * @class IOPerfControlClient : public OSObject
23*2c2f96dcSApple OSS Distributions  * @abstract Class which implements an interface allowing device drivers to participate in performance control.
24*2c2f96dcSApple OSS Distributions  * @discussion TODO
25*2c2f96dcSApple OSS Distributions  */
26*2c2f96dcSApple OSS Distributions class IOPerfControlClient final : public OSObject
27*2c2f96dcSApple OSS Distributions {
28*2c2f96dcSApple OSS Distributions 	OSDeclareDefaultStructors(IOPerfControlClient);
29*2c2f96dcSApple OSS Distributions 
30*2c2f96dcSApple OSS Distributions protected:
31*2c2f96dcSApple OSS Distributions 	virtual bool init(IOService *driver, uint64_t maxWorkCapacity);
32*2c2f96dcSApple OSS Distributions 	virtual void free() APPLE_KEXT_OVERRIDE;
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions public:
35*2c2f96dcSApple OSS Distributions /*!
36*2c2f96dcSApple OSS Distributions  * @function copyClient
37*2c2f96dcSApple OSS Distributions  * @abstract Return a retained reference to a client object, to be released by the driver. It may be
38*2c2f96dcSApple OSS Distributions  * shared with other drivers in the system.
39*2c2f96dcSApple OSS Distributions  * @param driver The device driver that will be using this interface.
40*2c2f96dcSApple OSS Distributions  * @param maxWorkCapacity The maximum number of concurrent work items supported by the device driver.
41*2c2f96dcSApple OSS Distributions  * @returns An instance of IOPerfControlClient.
42*2c2f96dcSApple OSS Distributions  */
43*2c2f96dcSApple OSS Distributions 	static IOPerfControlClient *copyClient(IOService *driver, uint64_t maxWorkCapacity);
44*2c2f96dcSApple OSS Distributions 
45*2c2f96dcSApple OSS Distributions /*!
46*2c2f96dcSApple OSS Distributions  * @function registerDevice
47*2c2f96dcSApple OSS Distributions  * @abstract Inform the system that work will be dispatched to a device in the future.
48*2c2f96dcSApple OSS Distributions  * @discussion The system will do some one-time setup work associated with the device, and may block the
49*2c2f96dcSApple OSS Distributions  * current thread during the setup. Devices should not be passed to work workSubmit, workSubmitAndBegin,
50*2c2f96dcSApple OSS Distributions  * workBegin, or workEnd until they have been successfully registered. The unregistration process happens
51*2c2f96dcSApple OSS Distributions  * automatically when the device object is deallocated.
52*2c2f96dcSApple OSS Distributions  * @param device The device object. Some platforms require device to be a specific subclass of IOService.
53*2c2f96dcSApple OSS Distributions  * @returns kIOReturnSuccess or an IOReturn error code
54*2c2f96dcSApple OSS Distributions  */
55*2c2f96dcSApple OSS Distributions 	virtual IOReturn registerDevice(IOService *driver, IOService *device);
56*2c2f96dcSApple OSS Distributions 
57*2c2f96dcSApple OSS Distributions /*!
58*2c2f96dcSApple OSS Distributions  * @function unregisterDevice
59*2c2f96dcSApple OSS Distributions  * @abstract Inform the system that work will be no longer be dispatched to a device in the future.
60*2c2f96dcSApple OSS Distributions  * @discussion This call is optional as the unregistration process happens automatically when the device
61*2c2f96dcSApple OSS Distributions  * object is deallocated. This call may block the current thread and/or acquire locks. It should not be
62*2c2f96dcSApple OSS Distributions  * called until after all submitted work has been ended using workEnd.
63*2c2f96dcSApple OSS Distributions  * @param device The device object. Some platforms require device to be a specific subclass of IOService.
64*2c2f96dcSApple OSS Distributions  */
65*2c2f96dcSApple OSS Distributions 	virtual void unregisterDevice(IOService *driver, IOService *device);
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions /*!
68*2c2f96dcSApple OSS Distributions  * @struct WorkSubmitArgs
69*2c2f96dcSApple OSS Distributions  * @discussion Drivers may submit additional device-specific arguments related to the submission of a work item
70*2c2f96dcSApple OSS Distributions  * by passing a struct with WorkSubmitArgs as its first member. Note: Drivers are responsible for publishing
71*2c2f96dcSApple OSS Distributions  * a header file describing these arguments.
72*2c2f96dcSApple OSS Distributions  */
73*2c2f96dcSApple OSS Distributions 	struct WorkSubmitArgs {
74*2c2f96dcSApple OSS Distributions 		uint32_t version;
75*2c2f96dcSApple OSS Distributions 		uint32_t size;
76*2c2f96dcSApple OSS Distributions 		uint64_t submit_time;
77*2c2f96dcSApple OSS Distributions 		uint64_t reserved[4];
78*2c2f96dcSApple OSS Distributions 		void *driver_data;
79*2c2f96dcSApple OSS Distributions 	};
80*2c2f96dcSApple OSS Distributions 
81*2c2f96dcSApple OSS Distributions /*!
82*2c2f96dcSApple OSS Distributions  * @function workSubmit
83*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that work was submitted.
84*2c2f96dcSApple OSS Distributions  * @param device The device that will execute the work. Some platforms require device to be a
85*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
86*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments related to the submission of this work item.
87*2c2f96dcSApple OSS Distributions  * @returns A token representing this work item, which must be passed to workEnd when the work is finished
88*2c2f96dcSApple OSS Distributions  * unless the token equals kIOPerfControlClientWorkUntracked. Failure to do this will result in memory leaks
89*2c2f96dcSApple OSS Distributions  * and a degradation of system performance.
90*2c2f96dcSApple OSS Distributions  */
91*2c2f96dcSApple OSS Distributions 	virtual uint64_t workSubmit(IOService *device, WorkSubmitArgs *args = nullptr);
92*2c2f96dcSApple OSS Distributions 
93*2c2f96dcSApple OSS Distributions /*!
94*2c2f96dcSApple OSS Distributions  * @struct WorkBeginArgs
95*2c2f96dcSApple OSS Distributions  * @discussion Drivers may submit additional device-specific arguments related to the start of a work item
96*2c2f96dcSApple OSS Distributions  * by passing a struct with WorkBeginArgs as its first member. Note: Drivers are responsible for publishing
97*2c2f96dcSApple OSS Distributions  * a header file describing these arguments.
98*2c2f96dcSApple OSS Distributions  */
99*2c2f96dcSApple OSS Distributions 	struct WorkBeginArgs {
100*2c2f96dcSApple OSS Distributions 		uint32_t version;
101*2c2f96dcSApple OSS Distributions 		uint32_t size;
102*2c2f96dcSApple OSS Distributions 		uint64_t begin_time;
103*2c2f96dcSApple OSS Distributions 		uint64_t reserved[4];
104*2c2f96dcSApple OSS Distributions 		void *driver_data;
105*2c2f96dcSApple OSS Distributions 	};
106*2c2f96dcSApple OSS Distributions 
107*2c2f96dcSApple OSS Distributions /*!
108*2c2f96dcSApple OSS Distributions  * @function workSubmitAndBegin
109*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that work was submitted and immediately began executing.
110*2c2f96dcSApple OSS Distributions  * @param device The device that is executing the work. Some platforms require device to be a
111*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
112*2c2f96dcSApple OSS Distributions  * @param submitArgs Optional device-specific arguments related to the submission of this work item.
113*2c2f96dcSApple OSS Distributions  * @param beginArgs Optional device-specific arguments related to the start of this work item.
114*2c2f96dcSApple OSS Distributions  * @returns A token representing this work item, which must be passed to workEnd when the work is finished
115*2c2f96dcSApple OSS Distributions  * unless the token equals kIOPerfControlClientWorkUntracked. Failure to do this will result in memory leaks
116*2c2f96dcSApple OSS Distributions  * and a degradation of system performance.
117*2c2f96dcSApple OSS Distributions  */
118*2c2f96dcSApple OSS Distributions 	virtual uint64_t workSubmitAndBegin(IOService *device, WorkSubmitArgs *submitArgs = nullptr,
119*2c2f96dcSApple OSS Distributions 	    WorkBeginArgs *beginArgs = nullptr);
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions /*!
122*2c2f96dcSApple OSS Distributions  * @function workBegin
123*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that previously submitted work began executing.
124*2c2f96dcSApple OSS Distributions  * @param device The device that is executing the work. Some platforms require device to be a
125*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
126*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments related to the start of this work item.
127*2c2f96dcSApple OSS Distributions  */
128*2c2f96dcSApple OSS Distributions 	virtual void workBegin(IOService *device, uint64_t token, WorkBeginArgs *args = nullptr);
129*2c2f96dcSApple OSS Distributions 
130*2c2f96dcSApple OSS Distributions /*!
131*2c2f96dcSApple OSS Distributions  * @struct WorkEndArgs
132*2c2f96dcSApple OSS Distributions  * @discussion Drivers may submit additional device-specific arguments related to the end of a work item
133*2c2f96dcSApple OSS Distributions  * by passing a struct with WorkEndArgs as its first member. Note: Drivers are responsible for publishing
134*2c2f96dcSApple OSS Distributions  * a header file describing these arguments.
135*2c2f96dcSApple OSS Distributions  */
136*2c2f96dcSApple OSS Distributions 	struct WorkEndArgs {
137*2c2f96dcSApple OSS Distributions 		uint32_t version;
138*2c2f96dcSApple OSS Distributions 		uint32_t size;
139*2c2f96dcSApple OSS Distributions 		uint64_t end_time;
140*2c2f96dcSApple OSS Distributions 		uint64_t reserved[4];
141*2c2f96dcSApple OSS Distributions 		void *driver_data;
142*2c2f96dcSApple OSS Distributions 	};
143*2c2f96dcSApple OSS Distributions 
144*2c2f96dcSApple OSS Distributions /*!
145*2c2f96dcSApple OSS Distributions  * @function workEnd
146*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that previously started work finished executing.
147*2c2f96dcSApple OSS Distributions  * @param device The device that executed the work. Some platforms require device to be a
148*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
149*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments related to the end of this work item.
150*2c2f96dcSApple OSS Distributions  * @param done Optional Set to false if the work has not yet completed. Drivers are then responsible for
151*2c2f96dcSApple OSS Distributions  * calling workBegin when the work resumes and workEnd with done set to True when it has completed. A workEnd() call
152*2c2f96dcSApple OSS Distributions  * without a corresponding workBegin() call is a way to cancel a work item and return token to IOPerfControl.
153*2c2f96dcSApple OSS Distributions  */
154*2c2f96dcSApple OSS Distributions 	virtual void workEnd(IOService *device, uint64_t token, WorkEndArgs *args = nullptr, bool done = true);
155*2c2f96dcSApple OSS Distributions 
156*2c2f96dcSApple OSS Distributions /*!
157*2c2f96dcSApple OSS Distributions  * @function copyWorkContext
158*2c2f96dcSApple OSS Distributions  * @abstract Return a retained reference to an opaque OSObject, to be released by the driver. This object can
159*2c2f96dcSApple OSS Distributions  * be used by IOPerfControl to track a work item. This may perform dynamic memory allocation.
160*2c2f96dcSApple OSS Distributions  * @returns A pointer to an OSObject
161*2c2f96dcSApple OSS Distributions  */
162*2c2f96dcSApple OSS Distributions 	OSPtr<OSObject> copyWorkContext();
163*2c2f96dcSApple OSS Distributions 
164*2c2f96dcSApple OSS Distributions /*!
165*2c2f96dcSApple OSS Distributions  * @function workSubmitAndBeginWithContext
166*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that work was submitted and immediately began executing
167*2c2f96dcSApple OSS Distributions  * @param device The device that is executing the work. Some platforms require device to be a
168*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
169*2c2f96dcSApple OSS Distributions  * @param context An OSObject returned by copyWorkContext(). The context object will be used by IOPerfControl to track
170*2c2f96dcSApple OSS Distributions  * this work item.
171*2c2f96dcSApple OSS Distributions  * @param submitArgs Optional device-specific arguments related to the submission of this work item.
172*2c2f96dcSApple OSS Distributions  * @param beginArgs Optional device-specific arguments related to the start of this work item.
173*2c2f96dcSApple OSS Distributions  * @returns true if IOPerfControl is tracking this work item, else false.
174*2c2f96dcSApple OSS Distributions  * @note The workEndWithContext() call is optional if the corresponding workSubmitWithContext() call returned false.
175*2c2f96dcSApple OSS Distributions  */
176*2c2f96dcSApple OSS Distributions 	bool workSubmitAndBeginWithContext(IOService *device, OSObject *context, WorkSubmitArgs *submitArgs = nullptr,
177*2c2f96dcSApple OSS Distributions 	    WorkBeginArgs *beginArgs = nullptr);
178*2c2f96dcSApple OSS Distributions 
179*2c2f96dcSApple OSS Distributions /*!
180*2c2f96dcSApple OSS Distributions  * @function workSubmitWithContext
181*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that work was submitted.
182*2c2f96dcSApple OSS Distributions  * @param device The device that will execute the work. Some platforms require device to be a
183*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
184*2c2f96dcSApple OSS Distributions  * @param context An OSObject returned by copyWorkContext(). The context object will be used by IOPerfControl to track
185*2c2f96dcSApple OSS Distributions  * this work item.
186*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments related to the submission of this work item.
187*2c2f96dcSApple OSS Distributions  * @returns true if IOPerfControl is tracking this work item, else false.
188*2c2f96dcSApple OSS Distributions  */
189*2c2f96dcSApple OSS Distributions 	bool workSubmitWithContext(IOService *device, OSObject *context, WorkSubmitArgs *args = nullptr);
190*2c2f96dcSApple OSS Distributions 
191*2c2f96dcSApple OSS Distributions /*!
192*2c2f96dcSApple OSS Distributions  * @function workBeginWithContext
193*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that previously submitted work began executing.
194*2c2f96dcSApple OSS Distributions  * @param device The device that is executing the work. Some platforms require device to be a
195*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
196*2c2f96dcSApple OSS Distributions  * @param context An OSObject returned by copyWorkContext() and provided to the previous call to workSubmitWithContext().
197*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments related to the start of this work item.
198*2c2f96dcSApple OSS Distributions  * @note The workBeginWithContext() and workEndWithContext() calls are optional if the corresponding workSubmitWithContext() call returned false.
199*2c2f96dcSApple OSS Distributions  */
200*2c2f96dcSApple OSS Distributions 	void workBeginWithContext(IOService *device, OSObject *context, WorkBeginArgs *args = nullptr);
201*2c2f96dcSApple OSS Distributions 
202*2c2f96dcSApple OSS Distributions /*!
203*2c2f96dcSApple OSS Distributions  * @function workEndWithContext
204*2c2f96dcSApple OSS Distributions  * @abstract Tell the performance controller that previously started work finished executing.
205*2c2f96dcSApple OSS Distributions  * @param device The device that executed the work. Some platforms require device to be a
206*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
207*2c2f96dcSApple OSS Distributions  * @param context An OSObject returned by copyWorkContext() and provided to the previous call to workSubmitWithContext().
208*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments related to the end of this work item.
209*2c2f96dcSApple OSS Distributions  * @param done Optional Set to false if the work has not yet completed. Drivers are then responsible for
210*2c2f96dcSApple OSS Distributions  * calling workBegin when the work resumes and workEnd with done set to True when it has completed.
211*2c2f96dcSApple OSS Distributions  * @note The workEndWithContext() call is optional if the corresponding workSubmitWithContext() call returned false. A workEndWithContext()
212*2c2f96dcSApple OSS Distributions  * call without a corresponding workBeginWithContext() call is a way to cancel a work item.
213*2c2f96dcSApple OSS Distributions  */
214*2c2f96dcSApple OSS Distributions 	void workEndWithContext(IOService *device, OSObject *context, WorkEndArgs *args = nullptr, bool done = true);
215*2c2f96dcSApple OSS Distributions 
216*2c2f96dcSApple OSS Distributions /*!
217*2c2f96dcSApple OSS Distributions  * @struct WorkUpdateArgs
218*2c2f96dcSApple OSS Distributions  * @discussion Drivers may submit additional device-specific arguments related to a work item by passing a
219*2c2f96dcSApple OSS Distributions  * struct with WorkUpdateArgs as its first member. Note: Drivers are responsible for publishing
220*2c2f96dcSApple OSS Distributions  * a header file describing these arguments.
221*2c2f96dcSApple OSS Distributions  */
222*2c2f96dcSApple OSS Distributions 	struct WorkUpdateArgs {
223*2c2f96dcSApple OSS Distributions 		uint32_t version;
224*2c2f96dcSApple OSS Distributions 		uint32_t size;
225*2c2f96dcSApple OSS Distributions 		uint64_t update_time;
226*2c2f96dcSApple OSS Distributions 		uint64_t reserved[4];
227*2c2f96dcSApple OSS Distributions 		void *driver_data;
228*2c2f96dcSApple OSS Distributions 	};
229*2c2f96dcSApple OSS Distributions 
230*2c2f96dcSApple OSS Distributions /*!
231*2c2f96dcSApple OSS Distributions  * @function workUpdateWithContext
232*2c2f96dcSApple OSS Distributions  * @abstract Provide and receive additional information from the performance controller. If this call is
233*2c2f96dcSApple OSS Distributions  * made at all, it should be between workSubmit and workEnd. The purpose and implementation of this call are
234*2c2f96dcSApple OSS Distributions  * device specific, and may do nothing on some devices.
235*2c2f96dcSApple OSS Distributions  * @param device The device that submitted the work. Some platforms require device to be a
236*2c2f96dcSApple OSS Distributions  * specific subclass of IOService.
237*2c2f96dcSApple OSS Distributions  * @param context An OSObject returned by copyWorkContext() and provided to the previous call to workSubmitWithContext().
238*2c2f96dcSApple OSS Distributions  * @param args Optional device-specific arguments.
239*2c2f96dcSApple OSS Distributions  */
240*2c2f96dcSApple OSS Distributions 	void workUpdateWithContext(IOService *device, OSObject *context, WorkUpdateArgs *args = nullptr);
241*2c2f96dcSApple OSS Distributions 
242*2c2f96dcSApple OSS Distributions /*
243*2c2f96dcSApple OSS Distributions  * Callers should always use the CURRENT version so that the kernel can detect both older
244*2c2f96dcSApple OSS Distributions  * and newer structure layouts. New callbacks should always be added at the end of the
245*2c2f96dcSApple OSS Distributions  * structure, and xnu should expect existing source recompiled against newer headers
246*2c2f96dcSApple OSS Distributions  * to pass NULL for unimplemented callbacks.
247*2c2f96dcSApple OSS Distributions  */
248*2c2f96dcSApple OSS Distributions 
249*2c2f96dcSApple OSS Distributions #define PERFCONTROL_INTERFACE_VERSION_NONE (0) /* no interface */
250*2c2f96dcSApple OSS Distributions #define PERFCONTROL_INTERFACE_VERSION_1 (1) /* up-to workEnd */
251*2c2f96dcSApple OSS Distributions #define PERFCONTROL_INTERFACE_VERSION_2 (2) /* up-to workUpdate */
252*2c2f96dcSApple OSS Distributions #define PERFCONTROL_INTERFACE_VERSION_3 (3) /* up-to (un)registerDriverDevice */
253*2c2f96dcSApple OSS Distributions #define PERFCONTROL_INTERFACE_VERSION_CURRENT PERFCONTROL_INTERFACE_VERSION_3
254*2c2f96dcSApple OSS Distributions 
255*2c2f96dcSApple OSS Distributions /*!
256*2c2f96dcSApple OSS Distributions  * @struct PerfControllerInterface
257*2c2f96dcSApple OSS Distributions  * @discussion Function pointers necessary to register a performance controller. Not for general driver use.
258*2c2f96dcSApple OSS Distributions  */
259*2c2f96dcSApple OSS Distributions 	struct PerfControllerInterface {
260*2c2f96dcSApple OSS Distributions 		struct DriverState {
261*2c2f96dcSApple OSS Distributions 			uint32_t has_target_thread_group : 1;
262*2c2f96dcSApple OSS Distributions 			uint32_t has_device_info : 1;
263*2c2f96dcSApple OSS Distributions 			uint32_t reserved : 30;
264*2c2f96dcSApple OSS Distributions 
265*2c2f96dcSApple OSS Distributions 			uint64_t target_thread_group_id;
266*2c2f96dcSApple OSS Distributions 			void *target_thread_group_data;
267*2c2f96dcSApple OSS Distributions 
268*2c2f96dcSApple OSS Distributions 			uint32_t device_type;
269*2c2f96dcSApple OSS Distributions 			uint32_t instance_id;
270*2c2f96dcSApple OSS Distributions 		};
271*2c2f96dcSApple OSS Distributions 
272*2c2f96dcSApple OSS Distributions 		struct WorkState {
273*2c2f96dcSApple OSS Distributions 			uint64_t thread_group_id;
274*2c2f96dcSApple OSS Distributions 			void *thread_group_data;
275*2c2f96dcSApple OSS Distributions 			void *work_data;
276*2c2f96dcSApple OSS Distributions 			uint32_t work_data_size;
277*2c2f96dcSApple OSS Distributions 			uint32_t started : 1;
278*2c2f96dcSApple OSS Distributions 			uint32_t reserved : 31;
279*2c2f96dcSApple OSS Distributions 			const DriverState* driver_state;
280*2c2f96dcSApple OSS Distributions 		};
281*2c2f96dcSApple OSS Distributions 
282*2c2f96dcSApple OSS Distributions 		using RegisterDeviceFunction = IOReturn (*)(IOService *);
283*2c2f96dcSApple OSS Distributions 		using RegisterDriverDeviceFunction = IOReturn (*)(IOService *, IOService *, DriverState *);
284*2c2f96dcSApple OSS Distributions 		using WorkCanSubmitFunction = bool (*)(IOService *, WorkState *, WorkSubmitArgs *);
285*2c2f96dcSApple OSS Distributions 		using WorkSubmitFunction = void (*)(IOService *, uint64_t, WorkState *, WorkSubmitArgs *);
286*2c2f96dcSApple OSS Distributions 		using WorkBeginFunction = void (*)(IOService *, uint64_t, WorkState *, WorkBeginArgs *);
287*2c2f96dcSApple OSS Distributions 		using WorkEndFunction = void (*)(IOService *, uint64_t, WorkState *, WorkEndArgs *, bool);
288*2c2f96dcSApple OSS Distributions 		using WorkUpdateFunction = void (*)(IOService *, uint64_t, WorkState *, WorkUpdateArgs *);
289*2c2f96dcSApple OSS Distributions 
290*2c2f96dcSApple OSS Distributions 		uint64_t version;
291*2c2f96dcSApple OSS Distributions 		RegisterDeviceFunction registerDevice;
292*2c2f96dcSApple OSS Distributions 		RegisterDeviceFunction unregisterDevice;
293*2c2f96dcSApple OSS Distributions 		WorkCanSubmitFunction workCanSubmit;
294*2c2f96dcSApple OSS Distributions 		WorkSubmitFunction workSubmit;
295*2c2f96dcSApple OSS Distributions 		WorkBeginFunction workBegin;
296*2c2f96dcSApple OSS Distributions 		WorkEndFunction workEnd;
297*2c2f96dcSApple OSS Distributions 		WorkUpdateFunction workUpdate;
298*2c2f96dcSApple OSS Distributions 		RegisterDriverDeviceFunction registerDriverDevice;
299*2c2f96dcSApple OSS Distributions 		RegisterDriverDeviceFunction unregisterDriverDevice;
300*2c2f96dcSApple OSS Distributions 	};
301*2c2f96dcSApple OSS Distributions 
302*2c2f96dcSApple OSS Distributions 	struct IOPerfControlClientShared {
303*2c2f96dcSApple OSS Distributions 		atomic_uint_fast8_t maxDriverIndex;
304*2c2f96dcSApple OSS Distributions 		PerfControllerInterface interface;
305*2c2f96dcSApple OSS Distributions 		IOLock *interfaceLock;
306*2c2f96dcSApple OSS Distributions 		OSSet *deviceRegistrationList;
307*2c2f96dcSApple OSS Distributions 	};
308*2c2f96dcSApple OSS Distributions 
309*2c2f96dcSApple OSS Distributions 	struct IOPerfControlClientData {
310*2c2f96dcSApple OSS Distributions 		struct thread_group *target_thread_group;
311*2c2f96dcSApple OSS Distributions 		PerfControllerInterface::DriverState driverState;
312*2c2f96dcSApple OSS Distributions 		IOService* device;
313*2c2f96dcSApple OSS Distributions 	};
314*2c2f96dcSApple OSS Distributions /*!
315*2c2f96dcSApple OSS Distributions  * @function registerPerformanceController
316*2c2f96dcSApple OSS Distributions  * @abstract Register a performance controller to receive callbacks. Not for general driver use.
317*2c2f96dcSApple OSS Distributions  * @param interface Struct containing callback functions implemented by the performance controller.
318*2c2f96dcSApple OSS Distributions  * @returns kIOReturnSuccess or kIOReturnError if the interface was already registered.
319*2c2f96dcSApple OSS Distributions  */
320*2c2f96dcSApple OSS Distributions 	virtual IOReturn registerPerformanceController(PerfControllerInterface *interface);
321*2c2f96dcSApple OSS Distributions 
322*2c2f96dcSApple OSS Distributions /*!
323*2c2f96dcSApple OSS Distributions  * @function getClientData
324*2c2f96dcSApple OSS Distributions  * @abstract Not for general driver use. Only used by registerPerformanceController(). Allows performanceController to register existing IOPerfControlClient.
325*2c2f96dcSApple OSS Distributions  * @returns IOPerfControlData associated with a IOPerfControlClient
326*2c2f96dcSApple OSS Distributions  */
327*2c2f96dcSApple OSS Distributions 	IOPerfControlClientData *
getClientData()328*2c2f96dcSApple OSS Distributions 	getClientData()
329*2c2f96dcSApple OSS Distributions 	{
330*2c2f96dcSApple OSS Distributions 		return &clientData;
331*2c2f96dcSApple OSS Distributions 	}
332*2c2f96dcSApple OSS Distributions 
333*2c2f96dcSApple OSS Distributions private:
334*2c2f96dcSApple OSS Distributions 	struct WorkTableEntry {
335*2c2f96dcSApple OSS Distributions 		struct thread_group *thread_group;
336*2c2f96dcSApple OSS Distributions 		bool started;
337*2c2f96dcSApple OSS Distributions 		uint8_t perfcontrol_data[32];
338*2c2f96dcSApple OSS Distributions 	};
339*2c2f96dcSApple OSS Distributions 
340*2c2f96dcSApple OSS Distributions 	static constexpr size_t kMaxWorkTableNumEntries = 1024;
341*2c2f96dcSApple OSS Distributions 	static constexpr size_t kWorkTableIndexBits = 24;
342*2c2f96dcSApple OSS Distributions 	static constexpr size_t kWorkTableMaxSize = (1 << kWorkTableIndexBits) - 1; // - 1 since
343*2c2f96dcSApple OSS Distributions 	// kIOPerfControlClientWorkUntracked takes number 0
344*2c2f96dcSApple OSS Distributions 	static constexpr size_t kWorkTableIndexMask = (const size_t)mask(kWorkTableIndexBits);
345*2c2f96dcSApple OSS Distributions 
346*2c2f96dcSApple OSS Distributions 	uint64_t allocateToken(thread_group *thread_group);
347*2c2f96dcSApple OSS Distributions 	void deallocateToken(uint64_t token);
348*2c2f96dcSApple OSS Distributions 	WorkTableEntry *getEntryForToken(uint64_t token);
349*2c2f96dcSApple OSS Distributions 	void markEntryStarted(uint64_t token, bool started);
350*2c2f96dcSApple OSS Distributions 	inline uint64_t tokenToGlobalUniqueToken(uint64_t token);
351*2c2f96dcSApple OSS Distributions 
352*2c2f96dcSApple OSS Distributions 	uint8_t driverIndex;
353*2c2f96dcSApple OSS Distributions 	IOPerfControlClientShared *shared;
354*2c2f96dcSApple OSS Distributions 	WorkTableEntry *workTable;
355*2c2f96dcSApple OSS Distributions 	size_t workTableLength;
356*2c2f96dcSApple OSS Distributions 	size_t workTableNextIndex;
357*2c2f96dcSApple OSS Distributions 	IOSimpleLock *workTableLock;
358*2c2f96dcSApple OSS Distributions 
359*2c2f96dcSApple OSS Distributions 	IOPerfControlClientData clientData;
360*2c2f96dcSApple OSS Distributions };
361*2c2f96dcSApple OSS Distributions 
362*2c2f96dcSApple OSS Distributions #endif /* __cplusplus */
363*2c2f96dcSApple OSS Distributions #endif /* KERNEL_PRIVATE */
364