FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/tsc.c
1 /*-
2 * Copyright (c) 1998-2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_clock.h"
31 #include "opt_kdtrace.h"
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/cpu.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
38 #include <sys/sysctl.h>
39 #include <sys/time.h>
40 #include <sys/timetc.h>
41 #include <sys/kernel.h>
42 #include <sys/power.h>
43 #include <sys/smp.h>
44 #include <machine/clock.h>
45 #include <machine/md_var.h>
46 #include <machine/specialreg.h>
47
48 #ifdef KDTRACE_HOOKS
49 #include <sys/dtrace_bsd.h>
50 #endif
51
52 #include "cpufreq_if.h"
53
54 uint64_t tsc_freq;
55 int tsc_is_broken;
56 static eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag;
57
58 #ifdef SMP
59 static int smp_tsc;
60 SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0,
61 "Indicates whether the TSC is safe to use in SMP mode");
62 TUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
63 #endif
64
65 static void tsc_freq_changed(void *arg, const struct cf_level *level,
66 int status);
67 static void tsc_freq_changing(void *arg, const struct cf_level *level,
68 int *status);
69 static unsigned tsc_get_timecount(struct timecounter *tc);
70 static void tsc_levels_changed(void *arg, int unit);
71
72 static struct timecounter tsc_timecounter = {
73 tsc_get_timecount, /* get_timecount */
74 0, /* no poll_pps */
75 ~0u, /* counter_mask */
76 0, /* frequency */
77 "TSC", /* name */
78 800, /* quality (adjusted in code) */
79 };
80
81 void
82 init_TSC(void)
83 {
84 u_int64_t tscval[2];
85
86 if (bootverbose)
87 printf("Calibrating TSC clock ... ");
88
89 tscval[0] = rdtsc();
90 DELAY(1000000);
91 tscval[1] = rdtsc();
92
93 tsc_freq = tscval[1] - tscval[0];
94 if (bootverbose)
95 printf("TSC clock: %lu Hz\n", tsc_freq);
96
97 /*
98 * Inform CPU accounting about our boot-time clock rate. Once the
99 * system is finished booting, we will get the real max clock rate
100 * via tsc_freq_max(). This also will be updated if someone loads
101 * a cpufreq driver after boot that discovers a new max frequency.
102 */
103 set_cputicker(rdtsc, tsc_freq, 1);
104
105 /* Register to find out about changes in CPU frequency. */
106 tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
107 tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST);
108 tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
109 tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST);
110 tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed,
111 tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY);
112 }
113
114 void
115 init_TSC_tc(void)
116 {
117
118 #ifdef SMP
119 /*
120 * We can not use the TSC in SMP mode unless the TSCs on all CPUs
121 * are somehow synchronized. Some hardware configurations do
122 * this, but we have no way of determining whether this is the
123 * case, so we do not use the TSC in multi-processor systems
124 * unless the user indicated (by setting kern.timecounter.smp_tsc
125 * to 1) that he believes that his TSCs are synchronized.
126 */
127 if (mp_ncpus > 1 && !smp_tsc)
128 tsc_timecounter.tc_quality = -100;
129 #endif
130
131 if (tsc_freq != 0 && !tsc_is_broken) {
132 tsc_timecounter.tc_frequency = tsc_freq;
133 tc_init(&tsc_timecounter);
134 }
135 }
136
137 /*
138 * When cpufreq levels change, find out about the (new) max frequency. We
139 * use this to update CPU accounting in case it got a lower estimate at boot.
140 */
141 static void
142 tsc_levels_changed(void *arg, int unit)
143 {
144 device_t cf_dev;
145 struct cf_level *levels;
146 int count, error;
147 uint64_t max_freq;
148
149 /* Only use values from the first CPU, assuming all are equal. */
150 if (unit != 0)
151 return;
152
153 /* Find the appropriate cpufreq device instance. */
154 cf_dev = devclass_get_device(devclass_find("cpufreq"), unit);
155 if (cf_dev == NULL) {
156 printf("tsc_levels_changed() called but no cpufreq device?\n");
157 return;
158 }
159
160 /* Get settings from the device and find the max frequency. */
161 count = 64;
162 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
163 if (levels == NULL)
164 return;
165 error = CPUFREQ_LEVELS(cf_dev, levels, &count);
166 if (error == 0 && count != 0) {
167 max_freq = (uint64_t)levels[0].total_set.freq * 1000000;
168 set_cputicker(rdtsc, max_freq, 1);
169 } else
170 printf("tsc_levels_changed: no max freq found\n");
171 free(levels, M_TEMP);
172 }
173
174 /*
175 * If the TSC timecounter is in use, veto the pending change. It may be
176 * possible in the future to handle a dynamically-changing timecounter rate.
177 */
178 static void
179 tsc_freq_changing(void *arg, const struct cf_level *level, int *status)
180 {
181
182 if (*status != 0 || timecounter != &tsc_timecounter)
183 return;
184
185 printf("timecounter TSC must not be in use when "
186 "changing frequencies; change denied\n");
187 *status = EBUSY;
188 }
189
190 /* Update TSC freq with the value indicated by the caller. */
191 static void
192 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
193 {
194 /* If there was an error during the transition, don't do anything. */
195 if (status != 0)
196 return;
197
198 /* Total setting for this level gives the new frequency in MHz. */
199 tsc_freq = (uint64_t)level->total_set.freq * 1000000;
200 tsc_timecounter.tc_frequency = tsc_freq;
201 }
202
203 static int
204 sysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
205 {
206 int error;
207 uint64_t freq;
208
209 if (tsc_timecounter.tc_frequency == 0)
210 return (EOPNOTSUPP);
211 freq = tsc_freq;
212 error = sysctl_handle_quad(oidp, &freq, 0, req);
213 if (error == 0 && req->newptr != NULL) {
214 tsc_freq = freq;
215 tsc_timecounter.tc_frequency = tsc_freq;
216 }
217 return (error);
218 }
219
220 SYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_QUAD | CTLFLAG_RW,
221 0, sizeof(u_int), sysctl_machdep_tsc_freq, "QU", "");
222
223 static unsigned
224 tsc_get_timecount(struct timecounter *tc)
225 {
226 return (rdtsc());
227 }
228
229 #ifdef KDTRACE_HOOKS
230 /*
231 * DTrace needs a high resolution time function which can
232 * be called from a probe context and guaranteed not to have
233 * instrumented with probes itself.
234 *
235 * Returns nanoseconds since boot.
236 */
237 uint64_t
238 dtrace_gethrtime()
239 {
240 return (rdtsc() * (uint64_t) 1000000000 / tsc_freq);
241 }
242 #endif
Cache object: aadbf7dfb732055b2e30afa206f2dd1a
|