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