2 * Copyright (c) 1998-2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_clock.h"
32 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 #include <sys/sysctl.h>
39 #include <sys/timetc.h>
40 #include <sys/kernel.h>
41 #include <sys/power.h>
43 #include <machine/clock.h>
44 #include <machine/md_var.h>
45 #include <machine/specialreg.h>
47 #include "cpufreq_if.h"
51 static eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag;
55 SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0,
56 "Indicates whether the TSC is safe to use in SMP mode");
57 TUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
60 static void tsc_freq_changed(void *arg, const struct cf_level *level,
62 static void tsc_freq_changing(void *arg, const struct cf_level *level,
64 static unsigned tsc_get_timecount(struct timecounter *tc);
65 static void tsc_levels_changed(void *arg, int unit);
67 static struct timecounter tsc_timecounter = {
68 tsc_get_timecount, /* get_timecount */
70 ~0u, /* counter_mask */
73 800, /* quality (adjusted in code) */
82 printf("Calibrating TSC clock ... ");
88 tsc_freq = tscval[1] - tscval[0];
90 printf("TSC clock: %lu Hz\n", tsc_freq);
93 * Inform CPU accounting about our boot-time clock rate. Once the
94 * system is finished booting, we will get the real max clock rate
95 * via tsc_freq_max(). This also will be updated if someone loads
96 * a cpufreq driver after boot that discovers a new max frequency.
98 set_cputicker(rdtsc, tsc_freq, 1);
100 /* Register to find out about changes in CPU frequency. */
101 tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
102 tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST);
103 tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
104 tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST);
105 tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed,
106 tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY);
115 * We can not use the TSC in SMP mode unless the TSCs on all CPUs
116 * are somehow synchronized. Some hardware configurations do
117 * this, but we have no way of determining whether this is the
118 * case, so we do not use the TSC in multi-processor systems
119 * unless the user indicated (by setting kern.timecounter.smp_tsc
120 * to 1) that he believes that his TSCs are synchronized.
122 if (mp_ncpus > 1 && !smp_tsc)
123 tsc_timecounter.tc_quality = -100;
126 if (tsc_freq != 0 && !tsc_is_broken) {
127 tsc_timecounter.tc_frequency = tsc_freq;
128 tc_init(&tsc_timecounter);
133 * When cpufreq levels change, find out about the (new) max frequency. We
134 * use this to update CPU accounting in case it got a lower estimate at boot.
137 tsc_levels_changed(void *arg, int unit)
140 struct cf_level *levels;
144 /* Only use values from the first CPU, assuming all are equal. */
148 /* Find the appropriate cpufreq device instance. */
149 cf_dev = devclass_get_device(devclass_find("cpufreq"), unit);
150 if (cf_dev == NULL) {
151 printf("tsc_levels_changed() called but no cpufreq device?\n");
155 /* Get settings from the device and find the max frequency. */
157 levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
160 error = CPUFREQ_LEVELS(cf_dev, levels, &count);
161 if (error == 0 && count != 0) {
162 max_freq = (uint64_t)levels[0].total_set.freq * 1000000;
163 set_cputicker(rdtsc, max_freq, 1);
165 printf("tsc_levels_changed: no max freq found\n");
166 free(levels, M_TEMP);
170 * If the TSC timecounter is in use, veto the pending change. It may be
171 * possible in the future to handle a dynamically-changing timecounter rate.
174 tsc_freq_changing(void *arg, const struct cf_level *level, int *status)
177 if (*status != 0 || timecounter != &tsc_timecounter)
180 printf("timecounter TSC must not be in use when "
181 "changing frequencies; change denied\n");
185 /* Update TSC freq with the value indicated by the caller. */
187 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
189 /* If there was an error during the transition, don't do anything. */
193 /* Total setting for this level gives the new frequency in MHz. */
194 tsc_freq = (uint64_t)level->total_set.freq * 1000000;
195 tsc_timecounter.tc_frequency = tsc_freq;
199 sysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
204 if (tsc_timecounter.tc_frequency == 0)
207 error = sysctl_handle_quad(oidp, &freq, 0, req);
208 if (error == 0 && req->newptr != NULL) {
210 tsc_timecounter.tc_frequency = tsc_freq;
215 SYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_QUAD | CTLFLAG_RW,
216 0, sizeof(u_int), sysctl_machdep_tsc_freq, "QU", "");
219 tsc_get_timecount(struct timecounter *tc)