]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/rtclock.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / osfmk / i386 / rtclock.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31
32/*
33 * File: i386/rtclock.c
34 * Purpose: Routines for handling the machine dependent
91447636
A
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.
1c79356b
A
40 */
41
55e303ae
A
42
43#include <mach/mach_types.h>
44
1c79356b 45#include <kern/cpu_data.h>
91447636 46#include <kern/cpu_number.h>
1c79356b 47#include <kern/clock.h>
55e303ae 48#include <kern/host_notify.h>
1c79356b
A
49#include <kern/macro_help.h>
50#include <kern/misc_protos.h>
51#include <kern/spl.h>
91447636 52#include <kern/assert.h>
39236c6e 53#include <kern/timer_queue.h>
1c79356b
A
54#include <mach/vm_prot.h>
55#include <vm/pmap.h>
56#include <vm/vm_kern.h> /* for kernel_map */
0c530ab8 57#include <architecture/i386/pio.h>
55e303ae 58#include <i386/machine_cpu.h>
91447636 59#include <i386/cpuid.h>
91447636 60#include <i386/cpu_threads.h>
b0d623f7 61#include <i386/mp.h>
91447636 62#include <i386/machine_routines.h>
6d2010ae 63#include <i386/pal_routines.h>
b0d623f7
A
64#include <i386/proc_reg.h>
65#include <i386/misc_protos.h>
55e303ae 66#include <pexpert/pexpert.h>
91447636
A
67#include <machine/limits.h>
68#include <machine/commpage.h>
69#include <sys/kdebug.h>
0c530ab8 70#include <i386/tsc.h>
6d2010ae 71#include <i386/rtclock_protos.h>
91447636 72#define UI_CPUFREQ_ROUNDING_FACTOR 10000000
1c79356b 73
0c530ab8 74int rtclock_init(void);
6601e61a 75
b0d623f7
A
76uint64_t tsc_rebase_abs_time = 0;
77
0c530ab8
A
78static void rtc_set_timescale(uint64_t cycles);
79static uint64_t rtc_export_speed(uint64_t cycles);
8f6c56a5 80
060df5ea
A
81void
82rtc_timer_start(void)
83{
84 /*
85 * Force a complete re-evaluation of timer deadlines.
86 */
39236c6e
A
87 x86_lcpu()->rtcDeadline = EndOfAllTime;
88 timer_resync_deadlines();
060df5ea
A
89}
90
b0d623f7
A
91static inline uint32_t
92_absolutetime_to_microtime(uint64_t abstime, clock_sec_t *secs, clock_usec_t *microsecs)
93{
94 uint32_t remain;
b0d623f7
A
95 *secs = abstime / (uint64_t)NSEC_PER_SEC;
96 remain = (uint32_t)(abstime % (uint64_t)NSEC_PER_SEC);
97 *microsecs = remain / NSEC_PER_USEC;
b0d623f7
A
98 return remain;
99}
100
101static inline void
102_absolutetime_to_nanotime(uint64_t abstime, clock_sec_t *secs, clock_usec_t *nanosecs)
103{
b0d623f7
A
104 *secs = abstime / (uint64_t)NSEC_PER_SEC;
105 *nanosecs = (clock_usec_t)(abstime % (uint64_t)NSEC_PER_SEC);
b0d623f7
A
106}
107
91447636
A
108/*
109 * Nanotime/mach_absolutime_time
110 * -----------------------------
0c530ab8
A
111 * The timestamp counter (TSC) - which counts cpu clock cycles and can be read
112 * efficiently by the kernel and in userspace - is the reference for all timing.
113 * The cpu clock rate is platform-dependent and may stop or be reset when the
114 * processor is napped/slept. As a result, nanotime is the software abstraction
115 * used to maintain a monotonic clock, adjusted from an outside reference as needed.
91447636
A
116 *
117 * The kernel maintains nanotime information recording:
0c530ab8 118 * - the ratio of tsc to nanoseconds
91447636
A
119 * with this ratio expressed as a 32-bit scale and shift
120 * (power of 2 divider);
0c530ab8 121 * - { tsc_base, ns_base } pair of corresponding timestamps.
6601e61a 122 *
0c530ab8
A
123 * The tuple {tsc_base, ns_base, scale, shift} is exported in the commpage
124 * for the userspace nanotime routine to read.
6601e61a 125 *
0c530ab8
A
126 * All of the routines which update the nanotime data are non-reentrant. This must
127 * be guaranteed by the caller.
91447636
A
128 */
129static inline void
6d2010ae 130rtc_nanotime_set_commpage(pal_rtc_nanotime_t *rntp)
91447636 131{
0c530ab8
A
132 commpage_set_nanotime(rntp->tsc_base, rntp->ns_base, rntp->scale, rntp->shift);
133}
6601e61a 134
0c530ab8
A
135/*
136 * rtc_nanotime_init:
137 *
138 * Intialize the nanotime info from the base time.
139 */
140static inline void
6d2010ae 141_rtc_nanotime_init(pal_rtc_nanotime_t *rntp, uint64_t base)
0c530ab8
A
142{
143 uint64_t tsc = rdtsc64();
21362eb3 144
6d2010ae 145 _pal_rtc_nanotime_store(tsc, base, rntp->scale, rntp->shift, rntp);
91447636
A
146}
147
39037602 148void
0c530ab8 149rtc_nanotime_init(uint64_t base)
91447636 150{
6d2010ae
A
151 _rtc_nanotime_init(&pal_rtc_nanotime_info, base);
152 rtc_nanotime_set_commpage(&pal_rtc_nanotime_info);
91447636
A
153}
154
0c530ab8
A
155/*
156 * rtc_nanotime_init_commpage:
157 *
158 * Call back from the commpage initialization to
159 * cause the commpage data to be filled in once the
160 * commpages have been created.
161 */
162void
163rtc_nanotime_init_commpage(void)
91447636 164{
0c530ab8
A
165 spl_t s = splclock();
166
6d2010ae 167 rtc_nanotime_set_commpage(&pal_rtc_nanotime_info);
0c530ab8 168 splx(s);
91447636
A
169}
170
0c530ab8
A
171/*
172 * rtc_nanotime_read:
173 *
174 * Returns the current nanotime value, accessable from any
175 * context.
176 */
2d21ac55 177static inline uint64_t
91447636
A
178rtc_nanotime_read(void)
179{
bd504ef0 180 return _rtc_nanotime_read(&pal_rtc_nanotime_info);
91447636
A
181}
182
91447636 183/*
0c530ab8
A
184 * rtc_clock_napped:
185 *
4a3eedf9
A
186 * Invoked from power management when we exit from a low C-State (>= C4)
187 * and the TSC has stopped counting. The nanotime data is updated according
188 * to the provided value which represents the new value for nanotime.
91447636 189 */
0c530ab8 190void
4a3eedf9 191rtc_clock_napped(uint64_t base, uint64_t tsc_base)
0c530ab8 192{
6d2010ae 193 pal_rtc_nanotime_t *rntp = &pal_rtc_nanotime_info;
4a3eedf9
A
194 uint64_t oldnsecs;
195 uint64_t newnsecs;
196 uint64_t tsc;
2d21ac55
A
197
198 assert(!ml_get_interrupts_enabled());
4a3eedf9 199 tsc = rdtsc64();
bd504ef0
A
200 oldnsecs = rntp->ns_base + _rtc_tsc_to_nanoseconds(tsc - rntp->tsc_base, rntp);
201 newnsecs = base + _rtc_tsc_to_nanoseconds(tsc - tsc_base, rntp);
4a3eedf9
A
202
203 /*
204 * Only update the base values if time using the new base values
205 * is later than the time using the old base values.
206 */
207 if (oldnsecs < newnsecs) {
6d2010ae 208 _pal_rtc_nanotime_store(tsc_base, base, rntp->scale, rntp->shift, rntp);
4a3eedf9
A
209 rtc_nanotime_set_commpage(rntp);
210 }
0c530ab8
A
211}
212
0b4c1975
A
213/*
214 * Invoked from power management to correct the SFLM TSC entry drift problem:
6d2010ae
A
215 * a small delta is added to the tsc_base. This is equivalent to nudgin time
216 * backwards. We require this to be on the order of a TSC quantum which won't
217 * cause callers of mach_absolute_time() to see time going backwards!
0b4c1975
A
218 */
219void
220rtc_clock_adjust(uint64_t tsc_base_delta)
221{
6d2010ae 222 pal_rtc_nanotime_t *rntp = &pal_rtc_nanotime_info;
0b4c1975 223
6d2010ae
A
224 assert(!ml_get_interrupts_enabled());
225 assert(tsc_base_delta < 100ULL); /* i.e. it's small */
226 _rtc_nanotime_adjust(tsc_base_delta, rntp);
227 rtc_nanotime_set_commpage(rntp);
0b4c1975
A
228}
229
91447636
A
230void
231rtc_clock_stepping(__unused uint32_t new_frequency,
232 __unused uint32_t old_frequency)
233{
0c530ab8 234 panic("rtc_clock_stepping unsupported");
91447636
A
235}
236
91447636 237void
0c530ab8
A
238rtc_clock_stepped(__unused uint32_t new_frequency,
239 __unused uint32_t old_frequency)
91447636 240{
2d21ac55 241 panic("rtc_clock_stepped unsupported");
1c79356b
A
242}
243
244/*
0c530ab8
A
245 * rtc_sleep_wakeup:
246 *
6d2010ae 247 * Invoked from power management when we have awoken from a sleep (S3)
bd504ef0
A
248 * and the TSC has been reset, or from Deep Idle (S0) sleep when the TSC
249 * has progressed. The nanotime data is updated based on the passed-in value.
0c530ab8
A
250 *
251 * The caller must guarantee non-reentrancy.
91447636
A
252 */
253void
0c530ab8
A
254rtc_sleep_wakeup(
255 uint64_t base)
91447636 256{
060df5ea 257 /* Set fixed configuration for lapic timers */
fe8ab488 258 rtc_timer->rtc_config();
060df5ea 259
91447636
A
260 /*
261 * Reset nanotime.
262 * The timestamp counter will have been reset
263 * but nanotime (uptime) marches onward.
91447636 264 */
0c530ab8 265 rtc_nanotime_init(base);
91447636
A
266}
267
39037602
A
268void
269rtc_decrementer_configure(void) {
270 rtc_timer->rtc_config();
271}
fe8ab488
A
272/*
273 * rtclock_early_init() is called very early at boot to
274 * establish mach_absolute_time() and set it to zero.
275 */
276void
277rtclock_early_init(void)
278{
279 assert(tscFreq);
280 rtc_set_timescale(tscFreq);
281}
282
91447636
A
283/*
284 * Initialize the real-time clock device.
285 * In addition, various variables used to support the clock are initialized.
1c79356b
A
286 */
287int
0c530ab8 288rtclock_init(void)
1c79356b 289{
91447636
A
290 uint64_t cycles;
291
0c530ab8
A
292 assert(!ml_get_interrupts_enabled());
293
91447636 294 if (cpu_number() == master_cpu) {
0c530ab8
A
295
296 assert(tscFreq);
0c530ab8 297
91447636 298 /*
0c530ab8 299 * Adjust and set the exported cpu speed.
91447636 300 */
0c530ab8 301 cycles = rtc_export_speed(tscFreq);
91447636
A
302
303 /*
304 * Set min/max to actual.
305 * ACPI may update these later if speed-stepping is detected.
306 */
0c530ab8
A
307 gPEClockFrequencyInfo.cpu_frequency_min_hz = cycles;
308 gPEClockFrequencyInfo.cpu_frequency_max_hz = cycles;
91447636 309
060df5ea 310 rtc_timer_init();
91447636 311 clock_timebase_init();
0c530ab8 312 ml_init_lock_timeout();
bd504ef0 313 ml_init_delay_spin_threshold(10);
1c79356b 314 }
91447636 315
6d2010ae 316 /* Set fixed configuration for lapic timers */
fe8ab488 317 rtc_timer->rtc_config();
060df5ea 318 rtc_timer_start();
91447636 319
1c79356b
A
320 return (1);
321}
322
0c530ab8
A
323// utility routine
324// Code to calculate how many processor cycles are in a second...
1c79356b 325
0c530ab8
A
326static void
327rtc_set_timescale(uint64_t cycles)
1c79356b 328{
6d2010ae 329 pal_rtc_nanotime_t *rntp = &pal_rtc_nanotime_info;
bd504ef0
A
330 uint32_t shift = 0;
331
332 /* the "scale" factor will overflow unless cycles>SLOW_TSC_THRESHOLD */
333
334 while ( cycles <= SLOW_TSC_THRESHOLD) {
335 shift++;
336 cycles <<= 1;
337 }
338
b0d623f7 339 rntp->scale = (uint32_t)(((uint64_t)NSEC_PER_SEC << 32) / cycles);
2d21ac55 340
bd504ef0 341 rntp->shift = shift;
1c79356b 342
15129b1c
A
343 /*
344 * On some platforms, the TSC is not reset at warm boot. But the
345 * rebase time must be relative to the current boot so we can't use
346 * mach_absolute_time(). Instead, we convert the TSC delta since boot
347 * to nanoseconds.
348 */
b0d623f7 349 if (tsc_rebase_abs_time == 0)
15129b1c
A
350 tsc_rebase_abs_time = _rtc_tsc_to_nanoseconds(
351 rdtsc64() - tsc_at_boot, rntp);
b0d623f7 352
0c530ab8 353 rtc_nanotime_init(0);
1c79356b
A
354}
355
91447636 356static uint64_t
0c530ab8 357rtc_export_speed(uint64_t cyc_per_sec)
9bccf70c 358{
fe8ab488 359 pal_rtc_nanotime_t *rntp = &pal_rtc_nanotime_info;
0c530ab8 360 uint64_t cycles;
1c79356b 361
fe8ab488
A
362 if (rntp->shift != 0 )
363 printf("Slow TSC, rtc_nanotime.shift == %d\n", rntp->shift);
364
0c530ab8
A
365 /* Round: */
366 cycles = ((cyc_per_sec + (UI_CPUFREQ_ROUNDING_FACTOR/2))
91447636
A
367 / UI_CPUFREQ_ROUNDING_FACTOR)
368 * UI_CPUFREQ_ROUNDING_FACTOR;
9bccf70c 369
91447636
A
370 /*
371 * Set current measured speed.
372 */
373 if (cycles >= 0x100000000ULL) {
374 gPEClockFrequencyInfo.cpu_clock_rate_hz = 0xFFFFFFFFUL;
55e303ae 375 } else {
91447636 376 gPEClockFrequencyInfo.cpu_clock_rate_hz = (unsigned long)cycles;
9bccf70c 377 }
91447636 378 gPEClockFrequencyInfo.cpu_frequency_hz = cycles;
55e303ae 379
0c530ab8 380 kprintf("[RTCLOCK] frequency %llu (%llu)\n", cycles, cyc_per_sec);
91447636 381 return(cycles);
9bccf70c 382}
1c79356b 383
55e303ae
A
384void
385clock_get_system_microtime(
b0d623f7
A
386 clock_sec_t *secs,
387 clock_usec_t *microsecs)
9bccf70c 388{
0c530ab8 389 uint64_t now = rtc_nanotime_read();
6601e61a 390
b0d623f7 391 _absolutetime_to_microtime(now, secs, microsecs);
1c79356b
A
392}
393
55e303ae
A
394void
395clock_get_system_nanotime(
b0d623f7
A
396 clock_sec_t *secs,
397 clock_nsec_t *nanosecs)
55e303ae 398{
0c530ab8 399 uint64_t now = rtc_nanotime_read();
8f6c56a5 400
b0d623f7 401 _absolutetime_to_nanotime(now, secs, nanosecs);
6601e61a
A
402}
403
404void
0c530ab8
A
405clock_gettimeofday_set_commpage(
406 uint64_t abstime,
407 uint64_t epoch,
408 uint64_t offset,
b0d623f7
A
409 clock_sec_t *secs,
410 clock_usec_t *microsecs)
0c530ab8 411{
b0d623f7 412 uint64_t now = abstime + offset;
0c530ab8 413 uint32_t remain;
6601e61a 414
b0d623f7 415 remain = _absolutetime_to_microtime(now, secs, microsecs);
6601e61a 416
b0d623f7 417 *secs += (clock_sec_t)epoch;
6601e61a 418
2d21ac55 419 commpage_set_timestamp(abstime - remain, *secs);
91447636
A
420}
421
1c79356b
A
422void
423clock_timebase_info(
424 mach_timebase_info_t info)
425{
91447636 426 info->numer = info->denom = 1;
1c79356b
A
427}
428
1c79356b 429/*
91447636 430 * Real-time clock device interrupt.
1c79356b 431 */
1c79356b 432void
0c530ab8
A
433rtclock_intr(
434 x86_saved_state_t *tregs)
1c79356b 435{
0c530ab8
A
436 uint64_t rip;
437 boolean_t user_mode = FALSE;
91447636
A
438
439 assert(get_preemption_level() > 0);
440 assert(!ml_get_interrupts_enabled());
441
0c530ab8
A
442 if (is_saved_state64(tregs) == TRUE) {
443 x86_saved_state64_t *regs;
444
445 regs = saved_state64(tregs);
5d5c5d0d 446
b0d623f7
A
447 if (regs->isf.cs & 0x03)
448 user_mode = TRUE;
0c530ab8
A
449 rip = regs->isf.rip;
450 } else {
451 x86_saved_state32_t *regs;
8ad349bb 452
0c530ab8 453 regs = saved_state32(tregs);
4452a7af 454
0c530ab8
A
455 if (regs->cs & 0x03)
456 user_mode = TRUE;
457 rip = regs->eip;
458 }
89b3af67 459
0c530ab8 460 /* call the generic etimer */
39236c6e 461 timer_intr(user_mode, rip);
5d5c5d0d
A
462}
463
060df5ea 464
0c530ab8
A
465/*
466 * Request timer pop from the hardware
467 */
468
060df5ea 469uint64_t
fe8ab488 470setPop(uint64_t time)
5d5c5d0d 471{
6d2010ae
A
472 uint64_t now;
473 uint64_t pop;
060df5ea
A
474
475 /* 0 and EndOfAllTime are special-cases for "clear the timer" */
6d2010ae 476 if (time == 0 || time == EndOfAllTime ) {
060df5ea
A
477 time = EndOfAllTime;
478 now = 0;
fe8ab488 479 pop = rtc_timer->rtc_set(0, 0);
060df5ea 480 } else {
6d2010ae 481 now = rtc_nanotime_read(); /* The time in nanoseconds */
fe8ab488 482 pop = rtc_timer->rtc_set(time, now);
060df5ea 483 }
4452a7af 484
6d2010ae 485 /* Record requested and actual deadlines set */
060df5ea 486 x86_lcpu()->rtcDeadline = time;
6d2010ae 487 x86_lcpu()->rtcPop = pop;
4452a7af 488
060df5ea 489 return pop - now;
89b3af67
A
490}
491
6601e61a
A
492uint64_t
493mach_absolute_time(void)
4452a7af 494{
0c530ab8
A
495 return rtc_nanotime_read();
496}
497
3e170ce0
A
498uint64_t
499mach_approximate_time(void)
500{
501 return rtc_nanotime_read();
502}
503
0c530ab8
A
504void
505clock_interval_to_absolutetime_interval(
506 uint32_t interval,
507 uint32_t scale_factor,
508 uint64_t *result)
509{
510 *result = (uint64_t)interval * scale_factor;
91447636
A
511}
512
513void
514absolutetime_to_microtime(
515 uint64_t abstime,
b0d623f7
A
516 clock_sec_t *secs,
517 clock_usec_t *microsecs)
91447636 518{
b0d623f7 519 _absolutetime_to_microtime(abstime, secs, microsecs);
1c79356b
A
520}
521
6601e61a 522void
0c530ab8 523nanotime_to_absolutetime(
b0d623f7
A
524 clock_sec_t secs,
525 clock_nsec_t nanosecs,
0c530ab8 526 uint64_t *result)
1c79356b 527{
0c530ab8 528 *result = ((uint64_t)secs * NSEC_PER_SEC) + nanosecs;
1c79356b
A
529}
530
531void
532absolutetime_to_nanoseconds(
0b4e3aa0
A
533 uint64_t abstime,
534 uint64_t *result)
1c79356b 535{
0b4e3aa0 536 *result = abstime;
1c79356b
A
537}
538
539void
540nanoseconds_to_absolutetime(
0b4e3aa0
A
541 uint64_t nanoseconds,
542 uint64_t *result)
1c79356b 543{
0b4e3aa0 544 *result = nanoseconds;
1c79356b
A
545}
546
55e303ae 547void
91447636 548machine_delay_until(
39236c6e
A
549 uint64_t interval,
550 uint64_t deadline)
55e303ae 551{
39236c6e
A
552 (void)interval;
553 while (mach_absolute_time() < deadline) {
554 cpu_pause();
555 }
55e303ae 556}