2 * Copyright (c) 2000-2011 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@
32 #include <platforms.h>
35 #include <mach/mach_types.h>
37 #include <architecture/i386/pio.h>
38 #include <i386/machine_cpu.h>
39 #include <i386/cpuid.h>
40 #include <i386/cpu_threads.h>
42 #include <i386/machine_routines.h>
43 #include <i386/pal_routines.h>
44 #include <i386/proc_reg.h>
45 #include <i386/misc_protos.h>
46 #include <i386/lapic.h>
47 #include <pexpert/pexpert.h>
48 #include <machine/limits.h>
49 #include <sys/kdebug.h>
51 #include <i386/rtclock_protos.h>
52 #include <i386/pal_routines.h>
53 #include <kern/etimer.h>
55 static uint64_t rtc_decrementer_min
;
56 static uint64_t rtc_decrementer_max
;
59 deadline_to_decrementer(
66 return rtc_decrementer_min
;
68 delta
= deadline
- now
;
69 return MIN(MAX(rtc_decrementer_min
,delta
),rtc_decrementer_max
);
75 * Regular local APIC timer case:
78 rtc_lapic_config_timer(void)
80 lapic_config_timer(TRUE
, one_shot
, divide_by_1
);
83 rtc_lapic_set_timer(uint64_t deadline
, uint64_t now
)
90 * Convert delta to bus ticks
91 * - time now is not relevant
93 count
= deadline_to_decrementer(deadline
, now
);
95 lapic_set_timer_fast((uint32_t) tmrCvt(count
, busFCvtn2t
));
97 lapic_set_timer(FALSE
, one_shot
, divide_by_1
, 0);
103 * TSC-deadline timer case:
106 rtc_lapic_config_tsc_deadline_timer(void)
108 lapic_config_tsc_deadline_timer();
111 rtc_lapic_set_tsc_deadline_timer(uint64_t deadline
, uint64_t now
)
115 uint64_t tsc
= rdtsc64();
122 delta
= deadline_to_decrementer(deadline
, now
);
124 delta_tsc
= tmrCvt(delta
, tscFCvtn2t
);
125 lapic_set_tsc_deadline_timer(tsc
+ delta_tsc
);
127 lapic_set_tsc_deadline_timer(0);
130 KERNEL_DEBUG_CONSTANT(
131 DECR_SET_TSC_DEADLINE
| DBG_FUNC_NONE
,
133 tsc
, lapic_get_tsc_deadline_timer(),
140 * Definitions for timer operations table
143 rtc_timer_t rtc_timer_lapic
= {
144 rtc_lapic_config_timer
,
148 rtc_timer_t rtc_timer_tsc_deadline
= {
149 rtc_lapic_config_tsc_deadline_timer
,
150 rtc_lapic_set_tsc_deadline_timer
153 rtc_timer_t
*rtc_timer
= &rtc_timer_lapic
; /* defaults to LAPIC timer */
156 * rtc_timer_init() is called at startup on the boot processor only.
161 int TSC_deadline_timer
= 0;
163 /* See whether we can use the local apic in TSC-deadline mode */
164 if ((cpuid_features() & CPUID_FEATURE_TSCTMR
)) {
165 TSC_deadline_timer
= 1;
166 PE_parse_boot_argn("TSC_deadline_timer", &TSC_deadline_timer
,
167 sizeof(TSC_deadline_timer
));
168 printf("TSC Deadline Timer supported %s enabled\n",
169 TSC_deadline_timer
? "and" : "but not");
172 if (TSC_deadline_timer
) {
173 rtc_timer
= &rtc_timer_tsc_deadline
;
174 rtc_decrementer_max
= UINT64_MAX
; /* effectively none */
176 * The min could be as low as 1nsec,
177 * but we're being conservative for now and making it the same
178 * as for the local apic timer.
180 rtc_decrementer_min
= 1*NSEC_PER_USEC
; /* 1 usec */
183 * Compute the longest interval using LAPIC timer.
185 rtc_decrementer_max
= tmrCvt(0x7fffffffULL
, busFCvtt2n
);
186 kprintf("maxDec: %lld\n", rtc_decrementer_max
);
187 rtc_decrementer_min
= 1*NSEC_PER_USEC
; /* 1 usec */
190 /* Point LAPIC interrupts to hardclock() */
191 lapic_set_timer_func((i386_intr_func_t
) rtclock_intr
);