2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * @APPLE_FREE_COPYRIGHT@
36 * Purpose: Routines for handling the machine independent timer.
39 #include <mach/mach_types.h>
41 #include <kern/timer_queue.h>
42 #include <kern/timer_call.h>
43 #include <kern/clock.h>
44 #include <kern/thread.h>
45 #include <kern/processor.h>
46 #include <kern/macro_help.h>
48 #include <kern/timer_queue.h>
51 #include <machine/commpage.h>
52 #include <machine/machine_routines.h>
54 #include <sys/kdebug.h>
55 #include <i386/cpu_data.h>
56 #include <i386/cpu_topology.h>
57 #include <i386/cpu_threads.h>
59 uint32_t spurious_timers
;
62 * Event timer interrupt.
64 * XXX a drawback of this implementation is that events serviced earlier must not set deadlines
65 * that occur before the entire chain completes.
67 * XXX a better implementation would use a set of generic callouts and iterate over them
70 timer_intr(int user_mode
,
74 rtclock_timer_t
*mytimer
;
78 boolean_t timer_processed
= FALSE
;
80 pp
= current_cpu_datap();
82 SCHED_STATS_TIMER_POP(current_processor());
84 abstime
= mach_absolute_time(); /* Get the time now */
86 /* has a pending clock timer expired? */
87 mytimer
= &pp
->rtclock_timer
; /* Point to the event timer */
89 if ((timer_processed
= ((mytimer
->deadline
<= abstime
) ||
90 (abstime
>= (mytimer
->queue
.earliest_soft_deadline
))))) {
92 * Log interrupt service latency (-ve value expected by tool)
93 * a non-PM event is expected next.
94 * The requested deadline may be earlier than when it was set
95 * - use MAX to avoid reporting bogus latencies.
97 latency
= (int64_t) (abstime
- MAX(mytimer
->deadline
,
99 /* Log zero timer latencies when opportunistically processing
103 TCOAL_DEBUG(0xEEEE0000, abstime
, mytimer
->queue
.earliest_soft_deadline
, abstime
- mytimer
->queue
.earliest_soft_deadline
, 0, 0);
107 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
108 DECR_TRAP_LATENCY
| DBG_FUNC_NONE
,
110 ((user_mode
!= 0) ? rip
: VM_KERNEL_UNSLIDE(rip
)),
113 mytimer
->has_expired
= TRUE
; /* Remember that we popped */
114 mytimer
->deadline
= timer_queue_expire(&mytimer
->queue
, abstime
);
115 mytimer
->has_expired
= FALSE
;
117 /* Get the time again since we ran a bit */
118 abstime
= mach_absolute_time();
119 mytimer
->when_set
= abstime
;
122 /* is it time for power management state change? */
123 if ((pmdeadline
= pmCPUGetDeadline(pp
)) && (pmdeadline
<= abstime
)) {
124 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
125 DECR_PM_DEADLINE
| DBG_FUNC_START
,
128 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
129 DECR_PM_DEADLINE
| DBG_FUNC_END
,
131 timer_processed
= TRUE
;
134 /* schedule our next deadline */
135 x86_lcpu()->rtcDeadline
= EndOfAllTime
;
136 timer_resync_deadlines();
138 if (__improbable(timer_processed
== FALSE
))
143 * Set the clock deadline.
145 void timer_set_deadline(uint64_t deadline
)
147 rtclock_timer_t
*mytimer
;
151 s
= splclock(); /* no interruptions */
152 pp
= current_cpu_datap();
154 mytimer
= &pp
->rtclock_timer
; /* Point to the timer itself */
155 mytimer
->deadline
= deadline
; /* Set new expiration time */
156 mytimer
->when_set
= mach_absolute_time();
158 timer_resync_deadlines();
164 * Re-evaluate the outstanding deadlines and select the most proximate.
166 * Should be called at splclock.
169 timer_resync_deadlines(void)
171 uint64_t deadline
= EndOfAllTime
;
173 rtclock_timer_t
*mytimer
;
174 spl_t s
= splclock();
178 pp
= current_cpu_datap();
179 if (!pp
->cpu_running
)
180 /* There's really nothing to do if this processor is down */
184 * If we have a clock timer set, pick that.
186 mytimer
= &pp
->rtclock_timer
;
187 if (!mytimer
->has_expired
&&
188 0 < mytimer
->deadline
&& mytimer
->deadline
< EndOfAllTime
)
189 deadline
= mytimer
->deadline
;
192 * If we have a power management deadline, see if that's earlier.
194 pmdeadline
= pmCPUGetDeadline(pp
);
195 if (0 < pmdeadline
&& pmdeadline
< deadline
)
196 deadline
= pmdeadline
;
199 * Go and set the "pop" event.
201 decr
= (uint32_t) setPop(deadline
);
203 /* Record non-PM deadline for latency tool */
204 if (decr
!= 0 && deadline
!= pmdeadline
) {
205 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
206 DECR_SET_DEADLINE
| DBG_FUNC_NONE
,
209 mytimer
->queue
.count
, 0);
215 timer_queue_expire_local(
218 rtclock_timer_t
*mytimer
;
222 pp
= current_cpu_datap();
224 mytimer
= &pp
->rtclock_timer
;
225 abstime
= mach_absolute_time();
227 mytimer
->has_expired
= TRUE
;
228 mytimer
->deadline
= timer_queue_expire(&mytimer
->queue
, abstime
);
229 mytimer
->has_expired
= FALSE
;
230 mytimer
->when_set
= mach_absolute_time();
232 timer_resync_deadlines();
236 timer_queue_expire_rescan(
239 rtclock_timer_t
*mytimer
;
243 assert(ml_get_interrupts_enabled() == FALSE
);
244 pp
= current_cpu_datap();
246 mytimer
= &pp
->rtclock_timer
;
247 abstime
= mach_absolute_time();
249 mytimer
->has_expired
= TRUE
;
250 mytimer
->deadline
= timer_queue_expire_with_options(&mytimer
->queue
, abstime
, TRUE
);
251 mytimer
->has_expired
= FALSE
;
252 mytimer
->when_set
= mach_absolute_time();
254 timer_resync_deadlines();
257 #define TIMER_RESORT_THRESHOLD_ABSTIME (50 * NSEC_PER_MSEC)
260 int32_t nc_tcl
, rt_tcl
, bg_tcl
, kt_tcl
, fp_tcl
, ts_tcl
, qos_tcl
;
261 #define TCOAL_PRIO_STAT(x) (x++)
263 #define TCOAL_PRIO_STAT(x)
267 timer_resort_threshold(uint64_t skew
) {
268 if (skew
>= TIMER_RESORT_THRESHOLD_ABSTIME
)
275 * Return the local timer queue for a running processor
276 * else return the boot processor's timer queue.
282 cpu_data_t
*cdp
= current_cpu_datap();
283 mpqueue_head_t
*queue
;
285 if (cdp
->cpu_running
) {
286 queue
= &cdp
->rtclock_timer
.queue
;
288 if (deadline
< cdp
->rtclock_timer
.deadline
)
289 timer_set_deadline(deadline
);
292 queue
= &cpu_datap(master_cpu
)->rtclock_timer
.queue
;
299 mpqueue_head_t
*queue
,
301 uint64_t new_deadline
)
303 if (queue
== ¤t_cpu_datap()->rtclock_timer
.queue
) {
304 if (deadline
< new_deadline
)
305 timer_set_deadline(new_deadline
);
310 * timer_queue_migrate_cpu() is called from the Power-Management kext
311 * when a logical processor goes idle (in a deep C-state) with a distant
312 * deadline so that it's timer queue can be moved to another processor.
313 * This target processor should be the least idle (most busy) --
314 * currently this is the primary processor for the calling thread's package.
315 * Locking restrictions demand that the target cpu must be the boot cpu.
318 timer_queue_migrate_cpu(int target_cpu
)
320 cpu_data_t
*target_cdp
= cpu_datap(target_cpu
);
321 cpu_data_t
*cdp
= current_cpu_datap();
324 assert(!ml_get_interrupts_enabled());
325 assert(target_cpu
!= cdp
->cpu_number
);
326 assert(target_cpu
== master_cpu
);
328 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
329 DECR_TIMER_MIGRATE
| DBG_FUNC_START
,
331 cdp
->rtclock_timer
.deadline
, (cdp
->rtclock_timer
.deadline
>>32),
335 * Move timer requests from the local queue to the target processor's.
336 * The return value is the number of requests moved. If this is 0,
337 * it indicates that the first (i.e. earliest) timer is earlier than
338 * the earliest for the target processor. Since this would force a
339 * resync, the move of this and all later requests is aborted.
341 ntimers_moved
= timer_queue_migrate(&cdp
->rtclock_timer
.queue
,
342 &target_cdp
->rtclock_timer
.queue
);
345 * Assuming we moved stuff, clear local deadline.
347 if (ntimers_moved
> 0) {
348 cdp
->rtclock_timer
.deadline
= EndOfAllTime
;
349 setPop(EndOfAllTime
);
352 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
353 DECR_TIMER_MIGRATE
| DBG_FUNC_END
,
354 target_cpu
, ntimers_moved
, 0, 0, 0);
356 return ntimers_moved
;
360 timer_queue_cpu(int cpu
)
362 return &cpu_datap(cpu
)->rtclock_timer
.queue
;
366 timer_call_cpu(int cpu
, void (*fn
)(void *), void *arg
)
368 mp_cpus_call(cpu_to_cpumask(cpu
), SYNC
, fn
, arg
);
372 timer_call_nosync_cpu(int cpu
, void (*fn
)(void *), void *arg
)
374 /* XXX Needs error checking and retry */
375 mp_cpus_call(cpu_to_cpumask(cpu
), NOSYNC
, fn
, arg
);
379 static timer_coalescing_priority_params_ns_t tcoal_prio_params_init
=
381 .idle_entry_timer_processing_hdeadline_threshold_ns
= 5000ULL * NSEC_PER_USEC
,
382 .interrupt_timer_coalescing_ilat_threshold_ns
= 30ULL * NSEC_PER_USEC
,
383 .timer_resort_threshold_ns
= 50 * NSEC_PER_MSEC
,
384 .timer_coalesce_rt_shift
= 0,
385 .timer_coalesce_bg_shift
= -5,
386 .timer_coalesce_kt_shift
= 3,
387 .timer_coalesce_fp_shift
= 3,
388 .timer_coalesce_ts_shift
= 3,
389 .timer_coalesce_rt_ns_max
= 0ULL,
390 .timer_coalesce_bg_ns_max
= 100 * NSEC_PER_MSEC
,
391 .timer_coalesce_kt_ns_max
= 1 * NSEC_PER_MSEC
,
392 .timer_coalesce_fp_ns_max
= 1 * NSEC_PER_MSEC
,
393 .timer_coalesce_ts_ns_max
= 1 * NSEC_PER_MSEC
,
394 .latency_qos_scale
= {3, 2, 1, -2, -15, -15},
395 .latency_qos_ns_max
={1 * NSEC_PER_MSEC
, 5 * NSEC_PER_MSEC
, 20 * NSEC_PER_MSEC
,
396 75 * NSEC_PER_MSEC
, 10000 * NSEC_PER_MSEC
, 10000 * NSEC_PER_MSEC
},
397 .latency_tier_rate_limited
= {FALSE
, FALSE
, FALSE
, FALSE
, TRUE
, TRUE
},
400 timer_coalescing_priority_params_ns_t
* timer_call_get_priority_params(void)
402 return &tcoal_prio_params_init
;