2 * Copyright (c) 2000-2012 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@
33 * File: i386/rtclock.c
34 * Purpose: Routines for handling the machine dependent
35 * real-time clock. Historically, this clock is
36 * generated by the Intel 8254 Programmable Interval
37 * Timer, but local apic timers are now used for
38 * this purpose with the master time reference being
39 * the cpu clock counted by the timestamp MSR.
42 #include <platforms.h>
44 #include <mach/mach_types.h>
46 #include <kern/cpu_data.h>
47 #include <kern/cpu_number.h>
48 #include <kern/clock.h>
49 #include <kern/host_notify.h>
50 #include <kern/macro_help.h>
51 #include <kern/misc_protos.h>
53 #include <kern/assert.h>
54 #include <kern/timer_queue.h>
55 #include <mach/vm_prot.h>
57 #include <vm/vm_kern.h> /* for kernel_map */
58 #include <architecture/i386/pio.h>
59 #include <i386/machine_cpu.h>
60 #include <i386/cpuid.h>
61 #include <i386/cpu_threads.h>
63 #include <i386/machine_routines.h>
64 #include <i386/pal_routines.h>
65 #include <i386/proc_reg.h>
66 #include <i386/misc_protos.h>
67 #include <pexpert/pexpert.h>
68 #include <machine/limits.h>
69 #include <machine/commpage.h>
70 #include <sys/kdebug.h>
72 #include <i386/rtclock_protos.h>
73 #define UI_CPUFREQ_ROUNDING_FACTOR 10000000
75 int rtclock_config(void);
77 int rtclock_init(void);
79 uint64_t tsc_rebase_abs_time
= 0;
81 static void rtc_set_timescale(uint64_t cycles
);
82 static uint64_t rtc_export_speed(uint64_t cycles
);
88 * Force a complete re-evaluation of timer deadlines.
90 x86_lcpu()->rtcDeadline
= EndOfAllTime
;
91 timer_resync_deadlines();
94 static inline uint32_t
95 _absolutetime_to_microtime(uint64_t abstime
, clock_sec_t
*secs
, clock_usec_t
*microsecs
)
98 *secs
= abstime
/ (uint64_t)NSEC_PER_SEC
;
99 remain
= (uint32_t)(abstime
% (uint64_t)NSEC_PER_SEC
);
100 *microsecs
= remain
/ NSEC_PER_USEC
;
105 _absolutetime_to_nanotime(uint64_t abstime
, clock_sec_t
*secs
, clock_usec_t
*nanosecs
)
107 *secs
= abstime
/ (uint64_t)NSEC_PER_SEC
;
108 *nanosecs
= (clock_usec_t
)(abstime
% (uint64_t)NSEC_PER_SEC
);
112 * Configure the real-time clock device. Return success (1)
125 * Nanotime/mach_absolutime_time
126 * -----------------------------
127 * The timestamp counter (TSC) - which counts cpu clock cycles and can be read
128 * efficiently by the kernel and in userspace - is the reference for all timing.
129 * The cpu clock rate is platform-dependent and may stop or be reset when the
130 * processor is napped/slept. As a result, nanotime is the software abstraction
131 * used to maintain a monotonic clock, adjusted from an outside reference as needed.
133 * The kernel maintains nanotime information recording:
134 * - the ratio of tsc to nanoseconds
135 * with this ratio expressed as a 32-bit scale and shift
136 * (power of 2 divider);
137 * - { tsc_base, ns_base } pair of corresponding timestamps.
139 * The tuple {tsc_base, ns_base, scale, shift} is exported in the commpage
140 * for the userspace nanotime routine to read.
142 * All of the routines which update the nanotime data are non-reentrant. This must
143 * be guaranteed by the caller.
146 rtc_nanotime_set_commpage(pal_rtc_nanotime_t
*rntp
)
148 commpage_set_nanotime(rntp
->tsc_base
, rntp
->ns_base
, rntp
->scale
, rntp
->shift
);
154 * Intialize the nanotime info from the base time.
157 _rtc_nanotime_init(pal_rtc_nanotime_t
*rntp
, uint64_t base
)
159 uint64_t tsc
= rdtsc64();
161 _pal_rtc_nanotime_store(tsc
, base
, rntp
->scale
, rntp
->shift
, rntp
);
165 rtc_nanotime_init(uint64_t base
)
167 _rtc_nanotime_init(&pal_rtc_nanotime_info
, base
);
168 rtc_nanotime_set_commpage(&pal_rtc_nanotime_info
);
172 * rtc_nanotime_init_commpage:
174 * Call back from the commpage initialization to
175 * cause the commpage data to be filled in once the
176 * commpages have been created.
179 rtc_nanotime_init_commpage(void)
181 spl_t s
= splclock();
183 rtc_nanotime_set_commpage(&pal_rtc_nanotime_info
);
190 * Returns the current nanotime value, accessable from any
193 static inline uint64_t
194 rtc_nanotime_read(void)
196 return _rtc_nanotime_read(&pal_rtc_nanotime_info
);
202 * Invoked from power management when we exit from a low C-State (>= C4)
203 * and the TSC has stopped counting. The nanotime data is updated according
204 * to the provided value which represents the new value for nanotime.
207 rtc_clock_napped(uint64_t base
, uint64_t tsc_base
)
209 pal_rtc_nanotime_t
*rntp
= &pal_rtc_nanotime_info
;
214 assert(!ml_get_interrupts_enabled());
216 oldnsecs
= rntp
->ns_base
+ _rtc_tsc_to_nanoseconds(tsc
- rntp
->tsc_base
, rntp
);
217 newnsecs
= base
+ _rtc_tsc_to_nanoseconds(tsc
- tsc_base
, rntp
);
220 * Only update the base values if time using the new base values
221 * is later than the time using the old base values.
223 if (oldnsecs
< newnsecs
) {
224 _pal_rtc_nanotime_store(tsc_base
, base
, rntp
->scale
, rntp
->shift
, rntp
);
225 rtc_nanotime_set_commpage(rntp
);
230 * Invoked from power management to correct the SFLM TSC entry drift problem:
231 * a small delta is added to the tsc_base. This is equivalent to nudgin time
232 * backwards. We require this to be on the order of a TSC quantum which won't
233 * cause callers of mach_absolute_time() to see time going backwards!
236 rtc_clock_adjust(uint64_t tsc_base_delta
)
238 pal_rtc_nanotime_t
*rntp
= &pal_rtc_nanotime_info
;
240 assert(!ml_get_interrupts_enabled());
241 assert(tsc_base_delta
< 100ULL); /* i.e. it's small */
242 _rtc_nanotime_adjust(tsc_base_delta
, rntp
);
243 rtc_nanotime_set_commpage(rntp
);
247 rtc_clock_stepping(__unused
uint32_t new_frequency
,
248 __unused
uint32_t old_frequency
)
250 panic("rtc_clock_stepping unsupported");
254 rtc_clock_stepped(__unused
uint32_t new_frequency
,
255 __unused
uint32_t old_frequency
)
257 panic("rtc_clock_stepped unsupported");
263 * Invoked from power management when we have awoken from a sleep (S3)
264 * and the TSC has been reset, or from Deep Idle (S0) sleep when the TSC
265 * has progressed. The nanotime data is updated based on the passed-in value.
267 * The caller must guarantee non-reentrancy.
273 /* Set fixed configuration for lapic timers */
278 * The timestamp counter will have been reset
279 * but nanotime (uptime) marches onward.
281 rtc_nanotime_init(base
);
285 * Initialize the real-time clock device.
286 * In addition, various variables used to support the clock are initialized.
293 assert(!ml_get_interrupts_enabled());
295 if (cpu_number() == master_cpu
) {
298 rtc_set_timescale(tscFreq
);
301 * Adjust and set the exported cpu speed.
303 cycles
= rtc_export_speed(tscFreq
);
306 * Set min/max to actual.
307 * ACPI may update these later if speed-stepping is detected.
309 gPEClockFrequencyInfo
.cpu_frequency_min_hz
= cycles
;
310 gPEClockFrequencyInfo
.cpu_frequency_max_hz
= cycles
;
313 clock_timebase_init();
314 ml_init_lock_timeout();
315 ml_init_delay_spin_threshold(10);
318 /* Set fixed configuration for lapic timers */
326 // Code to calculate how many processor cycles are in a second...
329 rtc_set_timescale(uint64_t cycles
)
331 pal_rtc_nanotime_t
*rntp
= &pal_rtc_nanotime_info
;
334 /* the "scale" factor will overflow unless cycles>SLOW_TSC_THRESHOLD */
336 while ( cycles
<= SLOW_TSC_THRESHOLD
) {
342 printf("Slow TSC, rtc_nanotime.shift == %d\n", shift
);
344 rntp
->scale
= (uint32_t)(((uint64_t)NSEC_PER_SEC
<< 32) / cycles
);
349 * On some platforms, the TSC is not reset at warm boot. But the
350 * rebase time must be relative to the current boot so we can't use
351 * mach_absolute_time(). Instead, we convert the TSC delta since boot
354 if (tsc_rebase_abs_time
== 0)
355 tsc_rebase_abs_time
= _rtc_tsc_to_nanoseconds(
356 rdtsc64() - tsc_at_boot
, rntp
);
358 rtc_nanotime_init(0);
362 rtc_export_speed(uint64_t cyc_per_sec
)
367 cycles
= ((cyc_per_sec
+ (UI_CPUFREQ_ROUNDING_FACTOR
/2))
368 / UI_CPUFREQ_ROUNDING_FACTOR
)
369 * UI_CPUFREQ_ROUNDING_FACTOR
;
372 * Set current measured speed.
374 if (cycles
>= 0x100000000ULL
) {
375 gPEClockFrequencyInfo
.cpu_clock_rate_hz
= 0xFFFFFFFFUL
;
377 gPEClockFrequencyInfo
.cpu_clock_rate_hz
= (unsigned long)cycles
;
379 gPEClockFrequencyInfo
.cpu_frequency_hz
= cycles
;
381 kprintf("[RTCLOCK] frequency %llu (%llu)\n", cycles
, cyc_per_sec
);
386 clock_get_system_microtime(
388 clock_usec_t
*microsecs
)
390 uint64_t now
= rtc_nanotime_read();
392 _absolutetime_to_microtime(now
, secs
, microsecs
);
396 clock_get_system_nanotime(
398 clock_nsec_t
*nanosecs
)
400 uint64_t now
= rtc_nanotime_read();
402 _absolutetime_to_nanotime(now
, secs
, nanosecs
);
406 clock_gettimeofday_set_commpage(
411 clock_usec_t
*microsecs
)
413 uint64_t now
= abstime
+ offset
;
416 remain
= _absolutetime_to_microtime(now
, secs
, microsecs
);
418 *secs
+= (clock_sec_t
)epoch
;
420 commpage_set_timestamp(abstime
- remain
, *secs
);
425 mach_timebase_info_t info
)
427 info
->numer
= info
->denom
= 1;
431 * Real-time clock device interrupt.
435 x86_saved_state_t
*tregs
)
438 boolean_t user_mode
= FALSE
;
440 assert(get_preemption_level() > 0);
441 assert(!ml_get_interrupts_enabled());
443 if (is_saved_state64(tregs
) == TRUE
) {
444 x86_saved_state64_t
*regs
;
446 regs
= saved_state64(tregs
);
448 if (regs
->isf
.cs
& 0x03)
452 x86_saved_state32_t
*regs
;
454 regs
= saved_state32(tregs
);
461 /* call the generic etimer */
462 timer_intr(user_mode
, rip
);
467 * Request timer pop from the hardware
477 /* 0 and EndOfAllTime are special-cases for "clear the timer" */
478 if (time
== 0 || time
== EndOfAllTime
) {
481 pop
= rtc_timer
->set(0, 0);
483 now
= rtc_nanotime_read(); /* The time in nanoseconds */
484 pop
= rtc_timer
->set(time
, now
);
487 /* Record requested and actual deadlines set */
488 x86_lcpu()->rtcDeadline
= time
;
489 x86_lcpu()->rtcPop
= pop
;
495 mach_absolute_time(void)
497 return rtc_nanotime_read();
501 clock_interval_to_absolutetime_interval(
503 uint32_t scale_factor
,
506 *result
= (uint64_t)interval
* scale_factor
;
510 absolutetime_to_microtime(
513 clock_usec_t
*microsecs
)
515 _absolutetime_to_microtime(abstime
, secs
, microsecs
);
519 absolutetime_to_nanotime(
522 clock_nsec_t
*nanosecs
)
524 _absolutetime_to_nanotime(abstime
, secs
, nanosecs
);
528 nanotime_to_absolutetime(
530 clock_nsec_t nanosecs
,
533 *result
= ((uint64_t)secs
* NSEC_PER_SEC
) + nanosecs
;
537 absolutetime_to_nanoseconds(
545 nanoseconds_to_absolutetime(
546 uint64_t nanoseconds
,
549 *result
= nanoseconds
;
558 while (mach_absolute_time() < deadline
) {