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