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 * $FreeBSD: releng/5.1/sys/amd64/amd64/tsc.c 114349 2003-05-01 01:05:25Z peter $
27 */
28
29 #include "opt_clock.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysctl.h>
34 #include <sys/time.h>
35 #include <sys/timetc.h>
36 #include <sys/kernel.h>
37 #include <sys/power.h>
38 #include <sys/smp.h>
39 #include <machine/clock.h>
40 #include <machine/md_var.h>
41 #include <machine/specialreg.h>
42
43 uint64_t tsc_freq;
44 int tsc_is_broken;
45 u_int tsc_present = 1;
46
47 #ifdef SMP
48 static int smp_tsc;
49 SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RD, &smp_tsc, 0,
50 "Indicates whether the TSC is safe to use in SMP mode");
51 TUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
52 #endif
53
54 static unsigned tsc_get_timecount(struct timecounter *tc);
55
56 static struct timecounter tsc_timecounter = {
57 tsc_get_timecount, /* get_timecount */
58 0, /* no poll_pps */
59 ~0u, /* counter_mask */
60 0, /* frequency */
61 "TSC" /* name */
62 };
63
64 void
65 init_TSC(void)
66 {
67 u_int64_t tscval[2];
68
69 if (bootverbose)
70 printf("Calibrating TSC clock ... ");
71
72 tscval[0] = rdtsc();
73 DELAY(1000000);
74 tscval[1] = rdtsc();
75
76 tsc_freq = tscval[1] - tscval[0];
77 if (bootverbose)
78 printf("TSC clock: %lu Hz\n", tsc_freq);
79
80 if (tsc_freq != 0 && !tsc_is_broken) {
81 tsc_timecounter.tc_frequency = tsc_freq;
82 tc_init(&tsc_timecounter);
83 }
84
85 return;
86 }
87
88 static int
89 sysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
90 {
91 int error;
92 uint64_t freq;
93
94 if (tsc_timecounter.tc_frequency == 0)
95 return (EOPNOTSUPP);
96 freq = tsc_freq;
97 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
98 if (error == 0 && req->newptr != NULL) {
99 tsc_freq = freq;
100 tsc_timecounter.tc_frequency = tsc_freq;
101 }
102 return (error);
103 }
104
105 SYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_LONG | CTLFLAG_RW,
106 0, sizeof(u_int), sysctl_machdep_tsc_freq, "IU", "");
107
108 static unsigned
109 tsc_get_timecount(struct timecounter *tc)
110 {
111 return (rdtsc());
112 }
Cache object: c319883308b8ff0c5b080cace79939a5
|