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