]>
Commit | Line | Data |
---|---|---|
6d2010ae A |
1 | /* |
2 | * Copyright (c) 2000-2010 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 | #include <platforms.h> | |
33 | #include <mach_kdb.h> | |
34 | ||
35 | #include <mach/mach_types.h> | |
36 | ||
37 | #include <architecture/i386/pio.h> | |
38 | #include <i386/machine_cpu.h> | |
39 | #include <i386/cpuid.h> | |
40 | #include <i386/cpu_threads.h> | |
41 | #include <i386/mp.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> | |
50 | #include <i386/tsc.h> | |
51 | #include <i386/rtclock_protos.h> | |
52 | #include <i386/pal_routines.h> | |
53 | #include <kern/etimer.h> | |
54 | ||
55 | static uint64_t rtc_decrementer_min; | |
56 | static uint64_t rtc_decrementer_max; | |
57 | ||
58 | static uint64_t | |
59 | deadline_to_decrementer( | |
60 | uint64_t deadline, | |
61 | uint64_t now) | |
62 | { | |
63 | uint64_t delta; | |
64 | ||
65 | if (deadline <= now) | |
66 | return rtc_decrementer_min; | |
67 | else { | |
68 | delta = deadline - now; | |
69 | return MIN(MAX(rtc_decrementer_min,delta),rtc_decrementer_max); | |
70 | } | |
71 | } | |
72 | ||
73 | static inline uint64_t | |
74 | _absolutetime_to_tsc(uint64_t ns) | |
75 | { | |
76 | uint32_t generation; | |
77 | uint64_t tsc; | |
78 | ||
79 | do { | |
80 | generation = pal_rtc_nanotime_info.generation; | |
81 | tsc = tmrCvt(ns - pal_rtc_nanotime_info.ns_base, tscFCvtn2t) | |
82 | + pal_rtc_nanotime_info.tsc_base; | |
83 | } while (generation == 0 || | |
84 | generation != pal_rtc_nanotime_info.generation); | |
85 | ||
86 | return tsc; | |
87 | } | |
88 | ||
89 | /* | |
90 | * Regular local APIC timer case: | |
91 | */ | |
92 | static void | |
93 | rtc_lapic_config_timer(void) | |
94 | { | |
95 | lapic_config_timer(TRUE, one_shot, divide_by_1); | |
96 | } | |
97 | static uint64_t | |
98 | rtc_lapic_set_timer(uint64_t deadline, uint64_t now) | |
99 | { | |
100 | uint64_t count; | |
101 | uint64_t set = 0; | |
102 | ||
103 | if (deadline > 0) { | |
104 | /* | |
105 | * Convert delta to bus ticks | |
106 | * - time now is not relevant | |
107 | */ | |
108 | count = deadline_to_decrementer(deadline, now); | |
109 | set = now + count; | |
110 | lapic_set_timer_fast((uint32_t) tmrCvt(count, busFCvtn2t)); | |
111 | } else { | |
112 | lapic_set_timer(FALSE, one_shot, divide_by_1, 0); | |
113 | } | |
114 | return set; | |
115 | } | |
116 | ||
117 | /* | |
118 | * TSC-deadline timer case: | |
119 | */ | |
120 | static void | |
121 | rtc_lapic_config_tsc_deadline_timer(void) | |
122 | { | |
123 | lapic_config_tsc_deadline_timer(); | |
124 | } | |
125 | static uint64_t | |
126 | rtc_lapic_set_tsc_deadline_timer(uint64_t deadline, uint64_t now) | |
127 | { | |
128 | uint64_t set = 0; | |
129 | ||
130 | if (deadline > 0) { | |
131 | /* | |
132 | * Convert to TSC | |
133 | */ | |
134 | set = now + deadline_to_decrementer(deadline, now); | |
135 | lapic_set_tsc_deadline_timer(_absolutetime_to_tsc(set)); | |
136 | } else { | |
137 | lapic_set_tsc_deadline_timer(0); | |
138 | } | |
139 | ||
140 | KERNEL_DEBUG_CONSTANT( | |
141 | DECR_SET_TSC_DEADLINE | DBG_FUNC_NONE, | |
142 | now, deadline, | |
143 | rdtsc64(), lapic_get_tsc_deadline_timer(), | |
144 | 0); | |
145 | ||
146 | return set; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Definitions for timer operations table | |
151 | */ | |
152 | ||
153 | rtc_timer_t rtc_timer_lapic = { | |
154 | rtc_lapic_config_timer, | |
155 | rtc_lapic_set_timer | |
156 | }; | |
157 | ||
158 | rtc_timer_t rtc_timer_tsc_deadline = { | |
159 | rtc_lapic_config_tsc_deadline_timer, | |
160 | rtc_lapic_set_tsc_deadline_timer | |
161 | }; | |
162 | ||
163 | rtc_timer_t *rtc_timer = &rtc_timer_lapic; /* defaults to LAPIC timer */ | |
164 | ||
165 | /* | |
166 | * rtc_timer_init() is called at startup on the boot processor only. | |
167 | */ | |
168 | void | |
169 | rtc_timer_init(void) | |
170 | { | |
171 | int TSC_deadline_timer = 0; | |
172 | ||
173 | /* See whether we can use the local apic in TSC-deadline mode */ | |
174 | if ((cpuid_features() & CPUID_FEATURE_TSCTMR)) { | |
175 | TSC_deadline_timer = 1; | |
176 | PE_parse_boot_argn("TSC_deadline_timer", &TSC_deadline_timer, | |
177 | sizeof(TSC_deadline_timer)); | |
178 | printf("TSC Deadline Timer supported %s enabled\n", | |
179 | TSC_deadline_timer ? "and" : "but not"); | |
180 | } | |
181 | ||
182 | if (TSC_deadline_timer) { | |
183 | rtc_timer = &rtc_timer_tsc_deadline; | |
184 | rtc_decrementer_max = UINT64_MAX; /* effectively none */ | |
185 | /* | |
186 | * The min could be as low as 1nsec, | |
187 | * but we're being conservative for now and making it the same | |
188 | * as for the local apic timer. | |
189 | */ | |
190 | rtc_decrementer_min = 1*NSEC_PER_USEC; /* 1 usec */ | |
191 | } else { | |
192 | /* | |
193 | * Compute the longest interval using LAPIC timer. | |
194 | */ | |
195 | rtc_decrementer_max = tmrCvt(0x7fffffffULL, busFCvtt2n); | |
196 | kprintf("maxDec: %lld\n", rtc_decrementer_max); | |
197 | rtc_decrementer_min = 1*NSEC_PER_USEC; /* 1 usec */ | |
198 | } | |
199 | ||
200 | /* Point LAPIC interrupts to hardclock() */ | |
201 | lapic_set_timer_func((i386_intr_func_t) rtclock_intr); | |
202 | } |