xref: /xnu-10063.121.3/osfmk/kern/timer_call.h (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 1993-1995, 1999-2008 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions 
29*2c2f96dcSApple OSS Distributions /*
30*2c2f96dcSApple OSS Distributions  * The timer_call system is responsible for manipulating timers that call
31*2c2f96dcSApple OSS Distributions  * callbacks at a given deadline (with or without some leeway for coalescing).
32*2c2f96dcSApple OSS Distributions  *
33*2c2f96dcSApple OSS Distributions  * Call timer_call_setup once on a timer_call structure to register the callback
34*2c2f96dcSApple OSS Distributions  * function and a context parameter that's passed to it (param0).
35*2c2f96dcSApple OSS Distributions  *
36*2c2f96dcSApple OSS Distributions  * To arm the timer to fire at a deadline, call any of the timer_call_enter
37*2c2f96dcSApple OSS Distributions  * functions.  If the function used accepts a parameter, it will be passed to
38*2c2f96dcSApple OSS Distributions  * the callback function when it fires.
39*2c2f96dcSApple OSS Distributions  *
40*2c2f96dcSApple OSS Distributions  * If the timer needs to be cancelled (like if the timer_call has been armed but
41*2c2f96dcSApple OSS Distributions  * now needs to be deallocated), call timer_call_cancel.
42*2c2f96dcSApple OSS Distributions  */
43*2c2f96dcSApple OSS Distributions 
44*2c2f96dcSApple OSS Distributions #ifndef _KERN_TIMER_CALL_H_
45*2c2f96dcSApple OSS Distributions #define _KERN_TIMER_CALL_H_
46*2c2f96dcSApple OSS Distributions 
47*2c2f96dcSApple OSS Distributions #include <mach/mach_types.h>
48*2c2f96dcSApple OSS Distributions #include <kern/kern_types.h>
49*2c2f96dcSApple OSS Distributions 
50*2c2f96dcSApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
51*2c2f96dcSApple OSS Distributions 
52*2c2f96dcSApple OSS Distributions #include <kern/simple_lock.h>
53*2c2f96dcSApple OSS Distributions 
54*2c2f96dcSApple OSS Distributions #ifdef MACH_KERNEL_PRIVATE
55*2c2f96dcSApple OSS Distributions #include <kern/queue.h>
56*2c2f96dcSApple OSS Distributions #include <kern/priority_queue.h>
57*2c2f96dcSApple OSS Distributions #include <kern/mpqueue.h>
58*2c2f96dcSApple OSS Distributions 
59*2c2f96dcSApple OSS Distributions extern boolean_t mach_timer_coalescing_enabled;
60*2c2f96dcSApple OSS Distributions extern void timer_call_queue_init(mpqueue_head_t *);
61*2c2f96dcSApple OSS Distributions #endif /* MACH_KERNEL_PRIVATE */
62*2c2f96dcSApple OSS Distributions 
63*2c2f96dcSApple OSS Distributions #if XNU_TARGET_OS_OSX
64*2c2f96dcSApple OSS Distributions #define TIMER_TRACE     1
65*2c2f96dcSApple OSS Distributions #endif
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions typedef void            *timer_call_param_t;
68*2c2f96dcSApple OSS Distributions typedef void            (*timer_call_func_t)(
69*2c2f96dcSApple OSS Distributions 	timer_call_param_t      param0,
70*2c2f96dcSApple OSS Distributions 	timer_call_param_t      param1);
71*2c2f96dcSApple OSS Distributions 
72*2c2f96dcSApple OSS Distributions typedef struct timer_call {
73*2c2f96dcSApple OSS Distributions 	uint64_t                                tc_soft_deadline;
74*2c2f96dcSApple OSS Distributions 	decl_simple_lock_data(, tc_lock);          /* protects tc_queue */
75*2c2f96dcSApple OSS Distributions 	struct priority_queue_entry_deadline    tc_pqlink;
76*2c2f96dcSApple OSS Distributions 	queue_head_t                            *tc_queue;
77*2c2f96dcSApple OSS Distributions 	queue_chain_t                           tc_qlink;
78*2c2f96dcSApple OSS Distributions 	timer_call_func_t                       tc_func;
79*2c2f96dcSApple OSS Distributions 	timer_call_param_t                      tc_param0;
80*2c2f96dcSApple OSS Distributions 	timer_call_param_t                      tc_param1;
81*2c2f96dcSApple OSS Distributions 	uint64_t                                tc_ttd; /* Time to deadline at creation */
82*2c2f96dcSApple OSS Distributions #if TIMER_TRACE
83*2c2f96dcSApple OSS Distributions 	uint64_t                                tc_entry_time;
84*2c2f96dcSApple OSS Distributions #endif
85*2c2f96dcSApple OSS Distributions 	uint32_t                                tc_flags;
86*2c2f96dcSApple OSS Distributions 	/* this field is locked by the lock in the object tc_queue points at */
87*2c2f96dcSApple OSS Distributions 	bool                                    tc_async_dequeue;
88*2c2f96dcSApple OSS Distributions } timer_call_data_t, *timer_call_t;
89*2c2f96dcSApple OSS Distributions 
90*2c2f96dcSApple OSS Distributions #define EndOfAllTime            0xFFFFFFFFFFFFFFFFULL
91*2c2f96dcSApple OSS Distributions 
92*2c2f96dcSApple OSS Distributions 
93*2c2f96dcSApple OSS Distributions /*
94*2c2f96dcSApple OSS Distributions  * Flags to alter the default timer/timeout coalescing behavior
95*2c2f96dcSApple OSS Distributions  * on a per-timer_call basis.
96*2c2f96dcSApple OSS Distributions  *
97*2c2f96dcSApple OSS Distributions  * The SYS urgency classes indicate that the timer_call is not
98*2c2f96dcSApple OSS Distributions  * directly related to the current thread at the time the timer_call
99*2c2f96dcSApple OSS Distributions  * is entered, so it is ignored in the calculation entirely (only
100*2c2f96dcSApple OSS Distributions  * the subclass specified is used).
101*2c2f96dcSApple OSS Distributions  *
102*2c2f96dcSApple OSS Distributions  * The USER flags indicate that both the current thread scheduling and QoS
103*2c2f96dcSApple OSS Distributions  * attributes, in addition to the per-timer_call urgency specification,
104*2c2f96dcSApple OSS Distributions  * are used to establish coalescing behavior.
105*2c2f96dcSApple OSS Distributions  */
106*2c2f96dcSApple OSS Distributions #define TIMER_CALL_SYS_NORMAL           TIMEOUT_URGENCY_SYS_NORMAL
107*2c2f96dcSApple OSS Distributions #define TIMER_CALL_SYS_CRITICAL         TIMEOUT_URGENCY_SYS_CRITICAL
108*2c2f96dcSApple OSS Distributions #define TIMER_CALL_SYS_BACKGROUND       TIMEOUT_URGENCY_SYS_BACKGROUND
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions #define TIMER_CALL_USER_MASK            TIMEOUT_URGENCY_USER_MASK
111*2c2f96dcSApple OSS Distributions #define TIMER_CALL_USER_NORMAL          TIMEOUT_URGENCY_USER_NORMAL
112*2c2f96dcSApple OSS Distributions #define TIMER_CALL_USER_CRITICAL        TIMEOUT_URGENCY_USER_CRITICAL
113*2c2f96dcSApple OSS Distributions #define TIMER_CALL_USER_BACKGROUND      TIMEOUT_URGENCY_USER_BACKGROUND
114*2c2f96dcSApple OSS Distributions 
115*2c2f96dcSApple OSS Distributions #define TIMER_CALL_URGENCY_MASK         TIMEOUT_URGENCY_MASK
116*2c2f96dcSApple OSS Distributions 
117*2c2f96dcSApple OSS Distributions /*
118*2c2f96dcSApple OSS Distributions  * Indicate that a specific leeway value is being provided (otherwise
119*2c2f96dcSApple OSS Distributions  * the leeway parameter is ignored).  This supplied value can currently
120*2c2f96dcSApple OSS Distributions  * only be used to extend the leeway calculated internally from the
121*2c2f96dcSApple OSS Distributions  * urgency class provided.
122*2c2f96dcSApple OSS Distributions  */
123*2c2f96dcSApple OSS Distributions #define TIMER_CALL_LEEWAY               TIMEOUT_URGENCY_LEEWAY
124*2c2f96dcSApple OSS Distributions 
125*2c2f96dcSApple OSS Distributions /*
126*2c2f96dcSApple OSS Distributions  * Non-migratable timer_call
127*2c2f96dcSApple OSS Distributions  */
128*2c2f96dcSApple OSS Distributions #define TIMER_CALL_LOCAL                TIMEOUT_URGENCY_FIRST_AVAIL
129*2c2f96dcSApple OSS Distributions #define TIMER_CALL_RATELIMITED          TIMEOUT_URGENCY_RATELIMITED
130*2c2f96dcSApple OSS Distributions extern boolean_t        timer_call_enter(
131*2c2f96dcSApple OSS Distributions 	timer_call_t    call,
132*2c2f96dcSApple OSS Distributions 	uint64_t        deadline,
133*2c2f96dcSApple OSS Distributions 	uint32_t        flags);
134*2c2f96dcSApple OSS Distributions 
135*2c2f96dcSApple OSS Distributions extern boolean_t        timer_call_enter1(
136*2c2f96dcSApple OSS Distributions 	timer_call_t            call,
137*2c2f96dcSApple OSS Distributions 	timer_call_param_t      param1,
138*2c2f96dcSApple OSS Distributions 	uint64_t                deadline,
139*2c2f96dcSApple OSS Distributions 	uint32_t                flags);
140*2c2f96dcSApple OSS Distributions 
141*2c2f96dcSApple OSS Distributions extern boolean_t        timer_call_enter_with_leeway(
142*2c2f96dcSApple OSS Distributions 	timer_call_t            call,
143*2c2f96dcSApple OSS Distributions 	timer_call_param_t      param1,
144*2c2f96dcSApple OSS Distributions 	uint64_t                deadline,
145*2c2f96dcSApple OSS Distributions 	uint64_t                leeway,
146*2c2f96dcSApple OSS Distributions 	uint32_t                flags,
147*2c2f96dcSApple OSS Distributions 	boolean_t               ratelimited);
148*2c2f96dcSApple OSS Distributions 
149*2c2f96dcSApple OSS Distributions extern boolean_t        timer_call_cancel(
150*2c2f96dcSApple OSS Distributions 	timer_call_t    call);
151*2c2f96dcSApple OSS Distributions 
152*2c2f96dcSApple OSS Distributions extern timer_call_t     timer_call_alloc(
153*2c2f96dcSApple OSS Distributions 	timer_call_func_t       func,
154*2c2f96dcSApple OSS Distributions 	timer_call_param_t      param0);
155*2c2f96dcSApple OSS Distributions 
156*2c2f96dcSApple OSS Distributions extern void             timer_call_free(
157*2c2f96dcSApple OSS Distributions 	timer_call_t            call);
158*2c2f96dcSApple OSS Distributions 
159*2c2f96dcSApple OSS Distributions extern void             timer_call_setup(
160*2c2f96dcSApple OSS Distributions 	timer_call_t            call,
161*2c2f96dcSApple OSS Distributions 	timer_call_func_t       func,
162*2c2f96dcSApple OSS Distributions 	timer_call_param_t      param0);
163*2c2f96dcSApple OSS Distributions 
164*2c2f96dcSApple OSS Distributions extern int timer_get_user_idle_level(void);
165*2c2f96dcSApple OSS Distributions extern kern_return_t timer_set_user_idle_level(int ilevel);
166*2c2f96dcSApple OSS Distributions 
167*2c2f96dcSApple OSS Distributions #define NUM_LATENCY_QOS_TIERS (6)
168*2c2f96dcSApple OSS Distributions typedef struct {
169*2c2f96dcSApple OSS Distributions 	uint32_t powergate_latency_abstime;
170*2c2f96dcSApple OSS Distributions 
171*2c2f96dcSApple OSS Distributions 	uint32_t idle_entry_timer_processing_hdeadline_threshold_abstime;
172*2c2f96dcSApple OSS Distributions 	uint32_t interrupt_timer_coalescing_ilat_threshold_abstime;
173*2c2f96dcSApple OSS Distributions 	uint32_t timer_resort_threshold_abstime;
174*2c2f96dcSApple OSS Distributions 
175*2c2f96dcSApple OSS Distributions 	int32_t timer_coalesce_rt_shift;
176*2c2f96dcSApple OSS Distributions 	int32_t timer_coalesce_bg_shift;
177*2c2f96dcSApple OSS Distributions 	int32_t timer_coalesce_kt_shift;
178*2c2f96dcSApple OSS Distributions 	int32_t timer_coalesce_fp_shift;
179*2c2f96dcSApple OSS Distributions 	int32_t timer_coalesce_ts_shift;
180*2c2f96dcSApple OSS Distributions 
181*2c2f96dcSApple OSS Distributions 	uint64_t timer_coalesce_rt_abstime_max;
182*2c2f96dcSApple OSS Distributions 	uint64_t timer_coalesce_bg_abstime_max;
183*2c2f96dcSApple OSS Distributions 	uint64_t timer_coalesce_kt_abstime_max;
184*2c2f96dcSApple OSS Distributions 	uint64_t timer_coalesce_fp_abstime_max;
185*2c2f96dcSApple OSS Distributions 	uint64_t timer_coalesce_ts_abstime_max;
186*2c2f96dcSApple OSS Distributions 
187*2c2f96dcSApple OSS Distributions 	uint32_t latency_qos_scale[NUM_LATENCY_QOS_TIERS];
188*2c2f96dcSApple OSS Distributions 	uint64_t latency_qos_abstime_max[NUM_LATENCY_QOS_TIERS];
189*2c2f96dcSApple OSS Distributions 	boolean_t latency_tier_rate_limited[NUM_LATENCY_QOS_TIERS];
190*2c2f96dcSApple OSS Distributions } timer_coalescing_priority_params_t;
191*2c2f96dcSApple OSS Distributions extern timer_coalescing_priority_params_t tcoal_prio_params;
192*2c2f96dcSApple OSS Distributions 
193*2c2f96dcSApple OSS Distributions /*
194*2c2f96dcSApple OSS Distributions  * Initialize the timer call subsystem during system startup.
195*2c2f96dcSApple OSS Distributions  */
196*2c2f96dcSApple OSS Distributions extern void timer_call_init(void);
197*2c2f96dcSApple OSS Distributions 
198*2c2f96dcSApple OSS Distributions #if MACH_KERNEL_PRIVATE
199*2c2f96dcSApple OSS Distributions 
200*2c2f96dcSApple OSS Distributions /*
201*2c2f96dcSApple OSS Distributions  * Handle deadlines in the past.
202*2c2f96dcSApple OSS Distributions  */
203*2c2f96dcSApple OSS Distributions uint64_t timer_call_past_deadline_timer_handle(uint64_t deadline,
204*2c2f96dcSApple OSS Distributions     uint64_t ctime);
205*2c2f96dcSApple OSS Distributions 
206*2c2f96dcSApple OSS Distributions /*
207*2c2f96dcSApple OSS Distributions  * Running timers are only active for a given CPU when a non-idle thread
208*2c2f96dcSApple OSS Distributions  * is running.
209*2c2f96dcSApple OSS Distributions  */
210*2c2f96dcSApple OSS Distributions 
211*2c2f96dcSApple OSS Distributions enum running_timer {
212*2c2f96dcSApple OSS Distributions 	RUNNING_TIMER_QUANTUM,
213*2c2f96dcSApple OSS Distributions 	RUNNING_TIMER_PREEMPT,
214*2c2f96dcSApple OSS Distributions #if KPERF
215*2c2f96dcSApple OSS Distributions 	RUNNING_TIMER_KPERF,
216*2c2f96dcSApple OSS Distributions #endif /* KPERF */
217*2c2f96dcSApple OSS Distributions 	RUNNING_TIMER_MAX,
218*2c2f96dcSApple OSS Distributions };
219*2c2f96dcSApple OSS Distributions 
220*2c2f96dcSApple OSS Distributions /*
221*2c2f96dcSApple OSS Distributions  * Get the earliest active deadline for this processor.
222*2c2f96dcSApple OSS Distributions  */
223*2c2f96dcSApple OSS Distributions uint64_t running_timers_deadline(processor_t processor);
224*2c2f96dcSApple OSS Distributions 
225*2c2f96dcSApple OSS Distributions /*
226*2c2f96dcSApple OSS Distributions  * Run the expire handler to process any timers past their deadline.  Returns
227*2c2f96dcSApple OSS Distributions  * true if any timer was processed, and false otherwise.
228*2c2f96dcSApple OSS Distributions  */
229*2c2f96dcSApple OSS Distributions bool running_timers_expire(processor_t processor, uint64_t now);
230*2c2f96dcSApple OSS Distributions 
231*2c2f96dcSApple OSS Distributions /*
232*2c2f96dcSApple OSS Distributions  * Set up a new deadline for the given running timer on the processor, but don't
233*2c2f96dcSApple OSS Distributions  * synchronize it with the hardware.  A subsequent call to running_timers_sync
234*2c2f96dcSApple OSS Distributions  * is necessary.  This allows thread_dispatch to batch all of the setup and only
235*2c2f96dcSApple OSS Distributions  * set the decrementer once.
236*2c2f96dcSApple OSS Distributions  */
237*2c2f96dcSApple OSS Distributions void running_timer_setup(processor_t processor, enum running_timer timer,
238*2c2f96dcSApple OSS Distributions     void *param, uint64_t deadline, uint64_t now);
239*2c2f96dcSApple OSS Distributions 
240*2c2f96dcSApple OSS Distributions /*
241*2c2f96dcSApple OSS Distributions  * Synchronize the state of any running timers that have been set up with the
242*2c2f96dcSApple OSS Distributions  * hardware.
243*2c2f96dcSApple OSS Distributions  */
244*2c2f96dcSApple OSS Distributions void running_timers_sync(void);
245*2c2f96dcSApple OSS Distributions 
246*2c2f96dcSApple OSS Distributions /*
247*2c2f96dcSApple OSS Distributions  * Enter a new deadline for the given running timer on the processor and put it
248*2c2f96dcSApple OSS Distributions  * into effect.
249*2c2f96dcSApple OSS Distributions  */
250*2c2f96dcSApple OSS Distributions void running_timer_enter(processor_t processor, enum running_timer timer,
251*2c2f96dcSApple OSS Distributions     void *param, uint64_t deadline, uint64_t now);
252*2c2f96dcSApple OSS Distributions 
253*2c2f96dcSApple OSS Distributions /*
254*2c2f96dcSApple OSS Distributions  * Clear the deadline and parameters for the given running timer on the
255*2c2f96dcSApple OSS Distributions  * processor.
256*2c2f96dcSApple OSS Distributions  */
257*2c2f96dcSApple OSS Distributions void running_timer_clear(processor_t processor, enum running_timer timer);
258*2c2f96dcSApple OSS Distributions 
259*2c2f96dcSApple OSS Distributions /*
260*2c2f96dcSApple OSS Distributions  * Cancel a running timer on the processor.
261*2c2f96dcSApple OSS Distributions  */
262*2c2f96dcSApple OSS Distributions void running_timer_cancel(processor_t processor, enum running_timer timer);
263*2c2f96dcSApple OSS Distributions 
264*2c2f96dcSApple OSS Distributions /*
265*2c2f96dcSApple OSS Distributions  * Activate the running timers for the given, current processor.  Should only be
266*2c2f96dcSApple OSS Distributions  * called by thread_dispatch.
267*2c2f96dcSApple OSS Distributions  */
268*2c2f96dcSApple OSS Distributions void running_timers_activate(processor_t processor);
269*2c2f96dcSApple OSS Distributions 
270*2c2f96dcSApple OSS Distributions /*
271*2c2f96dcSApple OSS Distributions  * Deactivate the running timers for the given, current processor.  Should only
272*2c2f96dcSApple OSS Distributions  * be called by thread_dispatch.
273*2c2f96dcSApple OSS Distributions  */
274*2c2f96dcSApple OSS Distributions void running_timers_deactivate(processor_t processor);
275*2c2f96dcSApple OSS Distributions 
276*2c2f96dcSApple OSS Distributions #endif /* MACH_KERNEL_PRIVATE */
277*2c2f96dcSApple OSS Distributions 
278*2c2f96dcSApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
279*2c2f96dcSApple OSS Distributions 
280*2c2f96dcSApple OSS Distributions #endif /* _KERN_TIMER_CALL_H_ */
281