]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/rtclock.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / osfmk / i386 / rtclock.c
CommitLineData
1c79356b 1/*
593a1d5f 2 * Copyright (c) 2000-2008 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
1c79356b 42#include <platforms.h>
1c79356b 43#include <mach_kdb.h>
55e303ae
A
44
45#include <mach/mach_types.h>
46
1c79356b 47#include <kern/cpu_data.h>
91447636 48#include <kern/cpu_number.h>
1c79356b 49#include <kern/clock.h>
55e303ae 50#include <kern/host_notify.h>
1c79356b
A
51#include <kern/macro_help.h>
52#include <kern/misc_protos.h>
53#include <kern/spl.h>
91447636 54#include <kern/assert.h>
1c79356b
A
55#include <mach/vm_prot.h>
56#include <vm/pmap.h>
57#include <vm/vm_kern.h> /* for kernel_map */
58#include <i386/ipl.h>
0c530ab8 59#include <architecture/i386/pio.h>
1c79356b 60#include <i386/misc_protos.h>
55e303ae
A
61#include <i386/proc_reg.h>
62#include <i386/machine_cpu.h>
593a1d5f 63#include <i386/lapic.h>
91447636
A
64#include <i386/cpuid.h>
65#include <i386/cpu_data.h>
66#include <i386/cpu_threads.h>
67#include <i386/perfmon.h>
68#include <i386/machine_routines.h>
55e303ae 69#include <pexpert/pexpert.h>
91447636
A
70#include <machine/limits.h>
71#include <machine/commpage.h>
72#include <sys/kdebug.h>
0c530ab8 73#include <i386/tsc.h>
0c530ab8 74#include <i386/rtclock.h>
91447636 75
91447636
A
76#define NSEC_PER_HZ (NSEC_PER_SEC / 100) /* nsec per tick */
77
78#define UI_CPUFREQ_ROUNDING_FACTOR 10000000
1c79356b 79
0c530ab8 80int rtclock_config(void);
6601e61a 81
0c530ab8 82int rtclock_init(void);
6601e61a 83
0c530ab8 84uint64_t rtc_decrementer_min;
6601e61a 85
0c530ab8
A
86void rtclock_intr(x86_saved_state_t *regs);
87static uint64_t maxDec; /* longest interval our hardware timer can handle (nsec) */
6601e61a 88
0c530ab8
A
89static void rtc_set_timescale(uint64_t cycles);
90static uint64_t rtc_export_speed(uint64_t cycles);
8f6c56a5 91
2d21ac55 92rtc_nanotime_t rtc_nanotime_info = {0,0,0,0,1,0};
6601e61a 93
4a3eedf9
A
94/*
95 * tsc_to_nanoseconds:
96 *
97 * Basic routine to convert a raw 64 bit TSC value to a
98 * 64 bit nanosecond value. The conversion is implemented
99 * based on the scale factor and an implicit 32 bit shift.
100 */
101static inline uint64_t
102_tsc_to_nanoseconds(uint64_t value)
103{
104 asm volatile("movl %%edx,%%esi ;"
105 "mull %%ecx ;"
106 "movl %%edx,%%edi ;"
107 "movl %%esi,%%eax ;"
108 "mull %%ecx ;"
109 "addl %%edi,%%eax ;"
110 "adcl $0,%%edx "
593a1d5f
A
111 : "+A" (value)
112 : "c" (current_cpu_datap()->cpu_nanotime->scale)
113 : "esi", "edi");
4a3eedf9
A
114
115 return (value);
116}
117
91447636
A
118static uint32_t
119deadline_to_decrementer(
120 uint64_t deadline,
121 uint64_t now)
122{
123 uint64_t delta;
124
125 if (deadline <= now)
126 return rtc_decrementer_min;
127 else {
128 delta = deadline - now;
0c530ab8 129 return MIN(MAX(rtc_decrementer_min,delta),maxDec);
91447636
A
130 }
131}
132
0c530ab8 133void
6601e61a
A
134rtc_lapic_start_ticking(void)
135{
2d21ac55 136 x86_lcpu_t *lcpu = x86_lcpu();
0c530ab8
A
137
138 /*
139 * Force a complete re-evaluation of timer deadlines.
140 */
2d21ac55 141 lcpu->rtcPop = EndOfAllTime;
0c530ab8 142 etimer_resync_deadlines();
1c79356b
A
143}
144
145/*
146 * Configure the real-time clock device. Return success (1)
147 * or failure (0).
148 */
149
150int
0c530ab8 151rtclock_config(void)
1c79356b 152{
0c530ab8 153 /* nothing to do */
91447636
A
154 return (1);
155}
156
157
158/*
159 * Nanotime/mach_absolutime_time
160 * -----------------------------
0c530ab8
A
161 * The timestamp counter (TSC) - which counts cpu clock cycles and can be read
162 * efficiently by the kernel and in userspace - is the reference for all timing.
163 * The cpu clock rate is platform-dependent and may stop or be reset when the
164 * processor is napped/slept. As a result, nanotime is the software abstraction
165 * used to maintain a monotonic clock, adjusted from an outside reference as needed.
91447636
A
166 *
167 * The kernel maintains nanotime information recording:
0c530ab8 168 * - the ratio of tsc to nanoseconds
91447636
A
169 * with this ratio expressed as a 32-bit scale and shift
170 * (power of 2 divider);
0c530ab8 171 * - { tsc_base, ns_base } pair of corresponding timestamps.
6601e61a 172 *
0c530ab8
A
173 * The tuple {tsc_base, ns_base, scale, shift} is exported in the commpage
174 * for the userspace nanotime routine to read.
6601e61a 175 *
0c530ab8
A
176 * All of the routines which update the nanotime data are non-reentrant. This must
177 * be guaranteed by the caller.
91447636
A
178 */
179static inline void
180rtc_nanotime_set_commpage(rtc_nanotime_t *rntp)
181{
0c530ab8
A
182 commpage_set_nanotime(rntp->tsc_base, rntp->ns_base, rntp->scale, rntp->shift);
183}
6601e61a 184
0c530ab8
A
185/*
186 * rtc_nanotime_init:
187 *
188 * Intialize the nanotime info from the base time.
189 */
190static inline void
191_rtc_nanotime_init(rtc_nanotime_t *rntp, uint64_t base)
192{
193 uint64_t tsc = rdtsc64();
21362eb3 194
2d21ac55 195 _rtc_nanotime_store(tsc, base, rntp->scale, rntp->shift, rntp);
91447636
A
196}
197
198static void
0c530ab8 199rtc_nanotime_init(uint64_t base)
91447636 200{
593a1d5f 201 rtc_nanotime_t *rntp = current_cpu_datap()->cpu_nanotime;
91447636 202
0c530ab8
A
203 _rtc_nanotime_init(rntp, base);
204 rtc_nanotime_set_commpage(rntp);
91447636
A
205}
206
0c530ab8
A
207/*
208 * rtc_nanotime_init_commpage:
209 *
210 * Call back from the commpage initialization to
211 * cause the commpage data to be filled in once the
212 * commpages have been created.
213 */
214void
215rtc_nanotime_init_commpage(void)
91447636 216{
0c530ab8
A
217 spl_t s = splclock();
218
593a1d5f 219 rtc_nanotime_set_commpage(current_cpu_datap()->cpu_nanotime);
4452a7af 220
0c530ab8 221 splx(s);
91447636
A
222}
223
0c530ab8
A
224/*
225 * rtc_nanotime_read:
226 *
227 * Returns the current nanotime value, accessable from any
228 * context.
229 */
2d21ac55 230static inline uint64_t
91447636
A
231rtc_nanotime_read(void)
232{
2d21ac55
A
233
234#if CONFIG_EMBEDDED
235 if (gPEClockFrequencyInfo.timebase_frequency_hz > SLOW_TSC_THRESHOLD)
593a1d5f 236 return _rtc_nanotime_read(current_cpu_datap()->cpu_nanotime, 1); /* slow processor */
2d21ac55
A
237 else
238#endif
593a1d5f 239 return _rtc_nanotime_read(current_cpu_datap()->cpu_nanotime, 0); /* assume fast processor */
91447636
A
240}
241
91447636 242/*
0c530ab8
A
243 * rtc_clock_napped:
244 *
4a3eedf9
A
245 * Invoked from power management when we exit from a low C-State (>= C4)
246 * and the TSC has stopped counting. The nanotime data is updated according
247 * to the provided value which represents the new value for nanotime.
91447636 248 */
0c530ab8 249void
4a3eedf9 250rtc_clock_napped(uint64_t base, uint64_t tsc_base)
0c530ab8 251{
593a1d5f 252 rtc_nanotime_t *rntp = current_cpu_datap()->cpu_nanotime;
4a3eedf9
A
253 uint64_t oldnsecs;
254 uint64_t newnsecs;
255 uint64_t tsc;
2d21ac55
A
256
257 assert(!ml_get_interrupts_enabled());
4a3eedf9
A
258 tsc = rdtsc64();
259 oldnsecs = rntp->ns_base + _tsc_to_nanoseconds(tsc - rntp->tsc_base);
260 newnsecs = base + _tsc_to_nanoseconds(tsc - tsc_base);
261
262 /*
263 * Only update the base values if time using the new base values
264 * is later than the time using the old base values.
265 */
266 if (oldnsecs < newnsecs) {
267 _rtc_nanotime_store(tsc_base, base, rntp->scale, rntp->shift, rntp);
268 rtc_nanotime_set_commpage(rntp);
269 }
0c530ab8
A
270}
271
91447636
A
272void
273rtc_clock_stepping(__unused uint32_t new_frequency,
274 __unused uint32_t old_frequency)
275{
0c530ab8 276 panic("rtc_clock_stepping unsupported");
91447636
A
277}
278
91447636 279void
0c530ab8
A
280rtc_clock_stepped(__unused uint32_t new_frequency,
281 __unused uint32_t old_frequency)
91447636 282{
2d21ac55 283 panic("rtc_clock_stepped unsupported");
1c79356b
A
284}
285
286/*
0c530ab8
A
287 * rtc_sleep_wakeup:
288 *
289 * Invoked from power manageent when we have awoken from a sleep (S3)
290 * and the TSC has been reset. The nanotime data is updated based on
291 * the passed in value.
292 *
293 * The caller must guarantee non-reentrancy.
91447636
A
294 */
295void
0c530ab8
A
296rtc_sleep_wakeup(
297 uint64_t base)
91447636 298{
91447636
A
299 /*
300 * Reset nanotime.
301 * The timestamp counter will have been reset
302 * but nanotime (uptime) marches onward.
91447636 303 */
0c530ab8 304 rtc_nanotime_init(base);
91447636
A
305}
306
307/*
308 * Initialize the real-time clock device.
309 * In addition, various variables used to support the clock are initialized.
1c79356b
A
310 */
311int
0c530ab8 312rtclock_init(void)
1c79356b 313{
91447636
A
314 uint64_t cycles;
315
0c530ab8
A
316 assert(!ml_get_interrupts_enabled());
317
91447636 318 if (cpu_number() == master_cpu) {
0c530ab8
A
319
320 assert(tscFreq);
321 rtc_set_timescale(tscFreq);
322
91447636 323 /*
0c530ab8 324 * Adjust and set the exported cpu speed.
91447636 325 */
0c530ab8 326 cycles = rtc_export_speed(tscFreq);
91447636
A
327
328 /*
329 * Set min/max to actual.
330 * ACPI may update these later if speed-stepping is detected.
331 */
0c530ab8
A
332 gPEClockFrequencyInfo.cpu_frequency_min_hz = cycles;
333 gPEClockFrequencyInfo.cpu_frequency_max_hz = cycles;
91447636 334
0c530ab8
A
335 /*
336 * Compute the longest interval we can represent.
337 */
338 maxDec = tmrCvt(0x7fffffffULL, busFCvtt2n);
339 kprintf("maxDec: %lld\n", maxDec);
91447636
A
340
341 /* Minimum interval is 1usec */
0c530ab8 342 rtc_decrementer_min = deadline_to_decrementer(NSEC_PER_USEC, 0ULL);
91447636
A
343 /* Point LAPIC interrupts to hardclock() */
344 lapic_set_timer_func((i386_intr_func_t) rtclock_intr);
345
346 clock_timebase_init();
0c530ab8 347 ml_init_lock_timeout();
1c79356b 348 }
91447636 349
91447636
A
350 rtc_lapic_start_ticking();
351
1c79356b
A
352 return (1);
353}
354
0c530ab8
A
355// utility routine
356// Code to calculate how many processor cycles are in a second...
1c79356b 357
0c530ab8
A
358static void
359rtc_set_timescale(uint64_t cycles)
1c79356b 360{
593a1d5f
A
361 rtc_nanotime_t *rntp = current_cpu_datap()->cpu_nanotime;
362 rntp->scale = ((uint64_t)NSEC_PER_SEC << 32) / cycles;
2d21ac55
A
363
364 if (cycles <= SLOW_TSC_THRESHOLD)
593a1d5f 365 rntp->shift = cycles;
2d21ac55 366 else
593a1d5f 367 rntp->shift = 32;
1c79356b 368
0c530ab8 369 rtc_nanotime_init(0);
1c79356b
A
370}
371
91447636 372static uint64_t
0c530ab8 373rtc_export_speed(uint64_t cyc_per_sec)
9bccf70c 374{
0c530ab8 375 uint64_t cycles;
1c79356b 376
0c530ab8
A
377 /* Round: */
378 cycles = ((cyc_per_sec + (UI_CPUFREQ_ROUNDING_FACTOR/2))
91447636
A
379 / UI_CPUFREQ_ROUNDING_FACTOR)
380 * UI_CPUFREQ_ROUNDING_FACTOR;
9bccf70c 381
91447636
A
382 /*
383 * Set current measured speed.
384 */
385 if (cycles >= 0x100000000ULL) {
386 gPEClockFrequencyInfo.cpu_clock_rate_hz = 0xFFFFFFFFUL;
55e303ae 387 } else {
91447636 388 gPEClockFrequencyInfo.cpu_clock_rate_hz = (unsigned long)cycles;
9bccf70c 389 }
91447636 390 gPEClockFrequencyInfo.cpu_frequency_hz = cycles;
55e303ae 391
0c530ab8 392 kprintf("[RTCLOCK] frequency %llu (%llu)\n", cycles, cyc_per_sec);
91447636 393 return(cycles);
9bccf70c 394}
1c79356b 395
55e303ae
A
396void
397clock_get_system_microtime(
398 uint32_t *secs,
399 uint32_t *microsecs)
9bccf70c 400{
0c530ab8
A
401 uint64_t now = rtc_nanotime_read();
402 uint32_t remain;
6601e61a 403
0c530ab8
A
404 asm volatile(
405 "divl %3"
406 : "=a" (*secs), "=d" (remain)
407 : "A" (now), "r" (NSEC_PER_SEC));
408 asm volatile(
409 "divl %3"
410 : "=a" (*microsecs)
411 : "0" (remain), "d" (0), "r" (NSEC_PER_USEC));
1c79356b
A
412}
413
55e303ae
A
414void
415clock_get_system_nanotime(
416 uint32_t *secs,
417 uint32_t *nanosecs)
418{
0c530ab8 419 uint64_t now = rtc_nanotime_read();
8f6c56a5 420
0c530ab8
A
421 asm volatile(
422 "divl %3"
423 : "=a" (*secs), "=d" (*nanosecs)
424 : "A" (now), "r" (NSEC_PER_SEC));
6601e61a
A
425}
426
427void
0c530ab8
A
428clock_gettimeofday_set_commpage(
429 uint64_t abstime,
430 uint64_t epoch,
431 uint64_t offset,
432 uint32_t *secs,
433 uint32_t *microsecs)
434{
435 uint64_t now = abstime;
436 uint32_t remain;
6601e61a 437
0c530ab8 438 now += offset;
6601e61a 439
0c530ab8
A
440 asm volatile(
441 "divl %3"
442 : "=a" (*secs), "=d" (remain)
443 : "A" (now), "r" (NSEC_PER_SEC));
444 asm volatile(
445 "divl %3"
446 : "=a" (*microsecs)
447 : "0" (remain), "d" (0), "r" (NSEC_PER_USEC));
6601e61a 448
0c530ab8 449 *secs += epoch;
6601e61a 450
2d21ac55 451 commpage_set_timestamp(abstime - remain, *secs);
91447636
A
452}
453
1c79356b
A
454void
455clock_timebase_info(
456 mach_timebase_info_t info)
457{
91447636 458 info->numer = info->denom = 1;
1c79356b
A
459}
460
1c79356b 461/*
91447636 462 * Real-time clock device interrupt.
1c79356b 463 */
1c79356b 464void
0c530ab8
A
465rtclock_intr(
466 x86_saved_state_t *tregs)
1c79356b 467{
0c530ab8
A
468 uint64_t rip;
469 boolean_t user_mode = FALSE;
55e303ae 470 uint64_t abstime;
91447636 471 uint32_t latency;
2d21ac55 472 x86_lcpu_t *lcpu = x86_lcpu();
91447636
A
473
474 assert(get_preemption_level() > 0);
475 assert(!ml_get_interrupts_enabled());
476
0c530ab8 477 abstime = rtc_nanotime_read();
2d21ac55
A
478 latency = (uint32_t)(abstime - lcpu->rtcDeadline);
479 if (abstime < lcpu->rtcDeadline)
480 latency = 1;
89b3af67 481
0c530ab8
A
482 if (is_saved_state64(tregs) == TRUE) {
483 x86_saved_state64_t *regs;
484
485 regs = saved_state64(tregs);
5d5c5d0d 486
0c530ab8
A
487 user_mode = TRUE;
488 rip = regs->isf.rip;
489 } else {
490 x86_saved_state32_t *regs;
8ad349bb 491
0c530ab8 492 regs = saved_state32(tregs);
4452a7af 493
0c530ab8
A
494 if (regs->cs & 0x03)
495 user_mode = TRUE;
496 rip = regs->eip;
497 }
89b3af67 498
0c530ab8 499 /* Log the interrupt service latency (-ve value expected by tool) */
6601e61a 500 KERNEL_DEBUG_CONSTANT(
0c530ab8
A
501 MACHDBG_CODE(DBG_MACH_EXCP_DECI, 0) | DBG_FUNC_NONE,
502 -latency, (uint32_t)rip, user_mode, 0, 0);
89b3af67 503
0c530ab8
A
504 /* call the generic etimer */
505 etimer_intr(user_mode, rip);
5d5c5d0d
A
506}
507
0c530ab8
A
508/*
509 * Request timer pop from the hardware
510 */
511
512int
513setPop(
514 uint64_t time)
5d5c5d0d 515{
0c530ab8
A
516 uint64_t now;
517 uint32_t decr;
518 uint64_t count;
519
520 now = rtc_nanotime_read(); /* The time in nanoseconds */
521 decr = deadline_to_decrementer(time, now);
4452a7af 522
0c530ab8
A
523 count = tmrCvt(decr, busFCvtn2t);
524 lapic_set_timer(TRUE, one_shot, divide_by_1, (uint32_t) count);
4452a7af 525
0c530ab8 526 return decr; /* Pass back what we set */
89b3af67
A
527}
528
0c530ab8 529
6601e61a
A
530uint64_t
531mach_absolute_time(void)
4452a7af 532{
0c530ab8
A
533 return rtc_nanotime_read();
534}
535
536void
537clock_interval_to_absolutetime_interval(
538 uint32_t interval,
539 uint32_t scale_factor,
540 uint64_t *result)
541{
542 *result = (uint64_t)interval * scale_factor;
91447636
A
543}
544
545void
546absolutetime_to_microtime(
547 uint64_t abstime,
548 uint32_t *secs,
549 uint32_t *microsecs)
550{
551 uint32_t remain;
552
553 asm volatile(
554 "divl %3"
555 : "=a" (*secs), "=d" (remain)
556 : "A" (abstime), "r" (NSEC_PER_SEC));
557 asm volatile(
558 "divl %3"
559 : "=a" (*microsecs)
560 : "0" (remain), "d" (0), "r" (NSEC_PER_USEC));
1c79356b
A
561}
562
563void
0c530ab8
A
564absolutetime_to_nanotime(
565 uint64_t abstime,
566 uint32_t *secs,
567 uint32_t *nanosecs)
6601e61a 568{
0c530ab8
A
569 asm volatile(
570 "divl %3"
571 : "=a" (*secs), "=d" (*nanosecs)
572 : "A" (abstime), "r" (NSEC_PER_SEC));
6601e61a
A
573}
574
575void
0c530ab8
A
576nanotime_to_absolutetime(
577 uint32_t secs,
578 uint32_t nanosecs,
579 uint64_t *result)
1c79356b 580{
0c530ab8 581 *result = ((uint64_t)secs * NSEC_PER_SEC) + nanosecs;
1c79356b
A
582}
583
584void
585absolutetime_to_nanoseconds(
0b4e3aa0
A
586 uint64_t abstime,
587 uint64_t *result)
1c79356b 588{
0b4e3aa0 589 *result = abstime;
1c79356b
A
590}
591
592void
593nanoseconds_to_absolutetime(
0b4e3aa0
A
594 uint64_t nanoseconds,
595 uint64_t *result)
1c79356b 596{
0b4e3aa0 597 *result = nanoseconds;
1c79356b
A
598}
599
55e303ae 600void
91447636 601machine_delay_until(
55e303ae
A
602 uint64_t deadline)
603{
604 uint64_t now;
605
606 do {
607 cpu_pause();
608 now = mach_absolute_time();
609 } while (now < deadline);
610}