kern_cctr.c revision 1.1.6.2 1 1.1.6.2 skrll /* $NetBSD: kern_cctr.c,v 1.1.6.2 2007/08/15 13:49:06 skrll Exp $ */
2 1.1.6.2 skrll
3 1.1.6.2 skrll
4 1.1.6.2 skrll /*-
5 1.1.6.2 skrll * Copyright (c) 2006 The NetBSD Foundation, Inc.
6 1.1.6.2 skrll * All rights reserved.
7 1.1.6.2 skrll *
8 1.1.6.2 skrll * re-implementation of TSC for MP systems merging cc_microtime and
9 1.1.6.2 skrll * TSC for timecounters by Frank Kardel
10 1.1.6.2 skrll *
11 1.1.6.2 skrll * Redistribution and use in source and binary forms, with or without
12 1.1.6.2 skrll * modification, are permitted provided that the following conditions
13 1.1.6.2 skrll * are met:
14 1.1.6.2 skrll * 1. Redistributions of source code must retain the above copyright
15 1.1.6.2 skrll * notice, this list of conditions and the following disclaimer.
16 1.1.6.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.6.2 skrll * notice, this list of conditions and the following disclaimer in the
18 1.1.6.2 skrll * documentation and/or other materials provided with the distribution.
19 1.1.6.2 skrll * 3. All advertising materials mentioning features or use of this software
20 1.1.6.2 skrll * must display the following acknowledgement:
21 1.1.6.2 skrll * This product includes software developed by the NetBSD
22 1.1.6.2 skrll * Foundation, Inc. and its contributors.
23 1.1.6.2 skrll * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1.6.2 skrll * contributors may be used to endorse or promote products derived
25 1.1.6.2 skrll * from this software without specific prior written permission.
26 1.1.6.2 skrll *
27 1.1.6.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1.6.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1.6.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1.6.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1.6.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1.6.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1.6.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1.6.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1.6.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1.6.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1.6.2 skrll * POSSIBILITY OF SUCH DAMAGE.
38 1.1.6.2 skrll */
39 1.1.6.2 skrll
40 1.1.6.2 skrll /* basic calibration ideas are (kern_microtime.c): */
41 1.1.6.2 skrll /******************************************************************************
42 1.1.6.2 skrll * *
43 1.1.6.2 skrll * Copyright (c) David L. Mills 1993, 1994 *
44 1.1.6.2 skrll * *
45 1.1.6.2 skrll * Permission to use, copy, modify, and distribute this software and its *
46 1.1.6.2 skrll * documentation for any purpose and without fee is hereby granted, provided *
47 1.1.6.2 skrll * that the above copyright notice appears in all copies and that both the *
48 1.1.6.2 skrll * copyright notice and this permission notice appear in supporting *
49 1.1.6.2 skrll * documentation, and that the name University of Delaware not be used in *
50 1.1.6.2 skrll * advertising or publicity pertaining to distribution of the software *
51 1.1.6.2 skrll * without specific, written prior permission. The University of Delaware *
52 1.1.6.2 skrll * makes no representations about the suitability this software for any *
53 1.1.6.2 skrll * purpose. It is provided "as is" without express or implied warranty. *
54 1.1.6.2 skrll * *
55 1.1.6.2 skrll ******************************************************************************/
56 1.1.6.2 skrll
57 1.1.6.2 skrll /* reminiscents from older version of this file are: */
58 1.1.6.2 skrll /*-
59 1.1.6.2 skrll * Copyright (c) 1998-2003 Poul-Henning Kamp
60 1.1.6.2 skrll * All rights reserved.
61 1.1.6.2 skrll *
62 1.1.6.2 skrll * Redistribution and use in source and binary forms, with or without
63 1.1.6.2 skrll * modification, are permitted provided that the following conditions
64 1.1.6.2 skrll * are met:
65 1.1.6.2 skrll * 1. Redistributions of source code must retain the above copyright
66 1.1.6.2 skrll * notice, this list of conditions and the following disclaimer.
67 1.1.6.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
68 1.1.6.2 skrll * notice, this list of conditions and the following disclaimer in the
69 1.1.6.2 skrll * documentation and/or other materials provided with the distribution.
70 1.1.6.2 skrll *
71 1.1.6.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
72 1.1.6.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 1.1.6.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 1.1.6.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
75 1.1.6.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 1.1.6.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 1.1.6.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 1.1.6.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 1.1.6.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 1.1.6.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81 1.1.6.2 skrll * SUCH DAMAGE.
82 1.1.6.2 skrll */
83 1.1.6.2 skrll
84 1.1.6.2 skrll #include <sys/cdefs.h>
85 1.1.6.2 skrll /* __FBSDID("$FreeBSD: src/sys/i386/i386/tsc.c,v 1.204 2003/10/21 18:28:34 silby Exp $"); */
86 1.1.6.2 skrll __KERNEL_RCSID(0, "$NetBSD: kern_cctr.c,v 1.1.6.2 2007/08/15 13:49:06 skrll Exp $");
87 1.1.6.2 skrll
88 1.1.6.2 skrll #include "opt_multiprocessor.h"
89 1.1.6.2 skrll
90 1.1.6.2 skrll #include <sys/param.h>
91 1.1.6.2 skrll #include <sys/systm.h>
92 1.1.6.2 skrll #include <sys/sysctl.h>
93 1.1.6.2 skrll #include <sys/time.h>
94 1.1.6.2 skrll #include <sys/timetc.h>
95 1.1.6.2 skrll #include <sys/kernel.h>
96 1.1.6.2 skrll #include <sys/power.h>
97 1.1.6.2 skrll #include <machine/cpu.h>
98 1.1.6.2 skrll #include <machine/cpu_counter.h>
99 1.1.6.2 skrll
100 1.1.6.2 skrll /* XXX make cc_timecounter.tc_frequency settable by sysctl() */
101 1.1.6.2 skrll
102 1.1.6.2 skrll static timecounter_get_t cc_get_timecount;
103 1.1.6.2 skrll static timecounter_pps_t cc_calibrate;
104 1.1.6.2 skrll
105 1.1.6.2 skrll void cc_calibrate_cpu(struct cpu_info *);
106 1.1.6.2 skrll
107 1.1.6.2 skrll static int64_t cc_cal_val; /* last calibrate time stamp */
108 1.1.6.2 skrll
109 1.1.6.2 skrll static struct timecounter cc_timecounter = {
110 1.1.6.2 skrll .tc_get_timecount = cc_get_timecount,
111 1.1.6.2 skrll .tc_poll_pps = cc_calibrate,
112 1.1.6.2 skrll .tc_counter_mask = ~0u,
113 1.1.6.2 skrll .tc_frequency = 0,
114 1.1.6.2 skrll .tc_name = "unkown cycle counter",
115 1.1.6.2 skrll /*
116 1.1.6.2 skrll * don't pick cycle counter automatically
117 1.1.6.2 skrll * if frequency changes might affect cycle counter
118 1.1.6.2 skrll */
119 1.1.6.2 skrll .tc_quality = -100000,
120 1.1.6.2 skrll
121 1.1.6.2 skrll .tc_priv = NULL,
122 1.1.6.2 skrll .tc_next = NULL
123 1.1.6.2 skrll };
124 1.1.6.2 skrll
125 1.1.6.2 skrll /*
126 1.1.6.2 skrll * initialize cycle counter based timecounter
127 1.1.6.2 skrll */
128 1.1.6.2 skrll struct timecounter *
129 1.1.6.2 skrll cc_init(uint64_t freq, const char *name, int quality)
130 1.1.6.2 skrll {
131 1.1.6.2 skrll
132 1.1.6.2 skrll cc_timecounter.tc_frequency = freq;
133 1.1.6.2 skrll cc_timecounter.tc_name = name;
134 1.1.6.2 skrll cc_timecounter.tc_quality = quality;
135 1.1.6.2 skrll tc_init(&cc_timecounter);
136 1.1.6.2 skrll
137 1.1.6.2 skrll return &cc_timecounter;
138 1.1.6.2 skrll }
139 1.1.6.2 skrll
140 1.1.6.2 skrll /*
141 1.1.6.2 skrll * pick up tick count scaled to reference tick count
142 1.1.6.2 skrll */
143 1.1.6.2 skrll static u_int
144 1.1.6.2 skrll cc_get_timecount(struct timecounter *tc)
145 1.1.6.2 skrll {
146 1.1.6.2 skrll struct cpu_info *ci = curcpu();
147 1.1.6.2 skrll int64_t rcc, cc;
148 1.1.6.2 skrll u_int gen;
149 1.1.6.2 skrll
150 1.1.6.2 skrll if (ci->ci_cc.cc_denom == 0) {
151 1.1.6.2 skrll /*
152 1.1.6.2 skrll * This is our first time here on this CPU. Just
153 1.1.6.2 skrll * start with reasonable initial values.
154 1.1.6.2 skrll */
155 1.1.6.2 skrll ci->ci_cc.cc_cc = cpu_counter32();
156 1.1.6.2 skrll ci->ci_cc.cc_val = 0;
157 1.1.6.2 skrll if (ci->ci_cc.cc_gen == 0)
158 1.1.6.2 skrll ci->ci_cc.cc_gen++;
159 1.1.6.2 skrll
160 1.1.6.2 skrll ci->ci_cc.cc_denom = cpu_frequency(ci);
161 1.1.6.2 skrll if (ci->ci_cc.cc_denom == 0)
162 1.1.6.2 skrll ci->ci_cc.cc_denom = cc_timecounter.tc_frequency;
163 1.1.6.2 skrll ci->ci_cc.cc_delta = ci->ci_cc.cc_denom;
164 1.1.6.2 skrll }
165 1.1.6.2 skrll
166 1.1.6.2 skrll /*
167 1.1.6.2 skrll * read counter and re-read when the re-calibration
168 1.1.6.2 skrll * strikes inbetween
169 1.1.6.2 skrll */
170 1.1.6.2 skrll do {
171 1.1.6.2 skrll /* pick up current generation number */
172 1.1.6.2 skrll gen = ci->ci_cc.cc_gen;
173 1.1.6.2 skrll
174 1.1.6.2 skrll /* determine local delta ticks */
175 1.1.6.2 skrll cc = cpu_counter32() - ci->ci_cc.cc_cc;
176 1.1.6.2 skrll if (cc < 0)
177 1.1.6.2 skrll cc += 0x100000000LL;
178 1.1.6.2 skrll
179 1.1.6.2 skrll /* scale to primary */
180 1.1.6.2 skrll rcc = (cc * ci->ci_cc.cc_delta) / ci->ci_cc.cc_denom
181 1.1.6.2 skrll + ci->ci_cc.cc_val;
182 1.1.6.2 skrll } while (gen == 0 || gen != ci->ci_cc.cc_gen);
183 1.1.6.2 skrll
184 1.1.6.2 skrll return rcc;
185 1.1.6.2 skrll }
186 1.1.6.2 skrll
187 1.1.6.2 skrll /*
188 1.1.6.2 skrll * called once per clock tick via the pps callback
189 1.1.6.2 skrll * for the calibration of the TSC counters.
190 1.1.6.2 skrll * it is called only for the PRIMARY cpu. all
191 1.1.6.2 skrll * other cpus are called via a broadcast IPI
192 1.1.6.2 skrll * calibration interval is 1 second - we call
193 1.1.6.2 skrll * the calobration code only every hz calls
194 1.1.6.2 skrll */
195 1.1.6.2 skrll static void
196 1.1.6.2 skrll cc_calibrate(struct timecounter *tc)
197 1.1.6.2 skrll {
198 1.1.6.2 skrll static int calls;
199 1.1.6.2 skrll struct cpu_info *ci;
200 1.1.6.2 skrll
201 1.1.6.2 skrll /*
202 1.1.6.2 skrll * XXX: for high interrupt frequency
203 1.1.6.2 skrll * support: ++calls < hz / tc_tick
204 1.1.6.2 skrll */
205 1.1.6.2 skrll if (++calls < hz)
206 1.1.6.2 skrll return;
207 1.1.6.2 skrll
208 1.1.6.2 skrll calls = 0;
209 1.1.6.2 skrll ci = curcpu();
210 1.1.6.2 skrll /* pick up reference ticks */
211 1.1.6.2 skrll cc_cal_val = cpu_counter32();
212 1.1.6.2 skrll
213 1.1.6.2 skrll #if defined(MULTIPROCESSOR)
214 1.1.6.2 skrll cc_calibrate_mp(ci);
215 1.1.6.2 skrll #endif
216 1.1.6.2 skrll cc_calibrate_cpu(ci);
217 1.1.6.2 skrll }
218 1.1.6.2 skrll
219 1.1.6.2 skrll /*
220 1.1.6.2 skrll * This routine is called about once per second directly by the master
221 1.1.6.2 skrll * processor and via an interprocessor interrupt for other processors.
222 1.1.6.2 skrll * It determines the CC frequency of each processor relative to the
223 1.1.6.2 skrll * master clock and the time this determination is made. These values
224 1.1.6.2 skrll * are used by cc_get_timecount() to interpolate the ticks between
225 1.1.6.2 skrll * timer interrupts. Note that we assume the kernel variables have
226 1.1.6.2 skrll * been zeroed early in life.
227 1.1.6.2 skrll */
228 1.1.6.2 skrll void
229 1.1.6.2 skrll cc_calibrate_cpu(struct cpu_info *ci)
230 1.1.6.2 skrll {
231 1.1.6.2 skrll u_int gen;
232 1.1.6.2 skrll int64_t val;
233 1.1.6.2 skrll int64_t delta, denom;
234 1.1.6.2 skrll int s;
235 1.1.6.2 skrll #ifdef TIMECOUNTER_DEBUG
236 1.1.6.2 skrll int64_t factor, old_factor;
237 1.1.6.2 skrll #endif
238 1.1.6.2 skrll val = cc_cal_val;
239 1.1.6.2 skrll
240 1.1.6.2 skrll s = splhigh();
241 1.1.6.2 skrll /* create next generation number */
242 1.1.6.2 skrll gen = ci->ci_cc.cc_gen;
243 1.1.6.2 skrll gen++;
244 1.1.6.2 skrll if (gen == 0)
245 1.1.6.2 skrll gen++;
246 1.1.6.2 skrll
247 1.1.6.2 skrll /* update in progress */
248 1.1.6.2 skrll ci->ci_cc.cc_gen = 0;
249 1.1.6.2 skrll
250 1.1.6.2 skrll denom = ci->ci_cc.cc_cc;
251 1.1.6.2 skrll ci->ci_cc.cc_cc = cpu_counter32();
252 1.1.6.2 skrll
253 1.1.6.2 skrll if (ci->ci_cc.cc_denom == 0) {
254 1.1.6.2 skrll /*
255 1.1.6.2 skrll * This is our first time here on this CPU. Just
256 1.1.6.2 skrll * start with reasonable initial values.
257 1.1.6.2 skrll */
258 1.1.6.2 skrll ci->ci_cc.cc_val = val;
259 1.1.6.2 skrll ci->ci_cc.cc_denom = cpu_frequency(ci);
260 1.1.6.2 skrll if (ci->ci_cc.cc_denom == 0)
261 1.1.6.2 skrll ci->ci_cc.cc_denom = cc_timecounter.tc_frequency;;
262 1.1.6.2 skrll ci->ci_cc.cc_delta = ci->ci_cc.cc_denom;
263 1.1.6.2 skrll ci->ci_cc.cc_gen = gen;
264 1.1.6.2 skrll splx(s);
265 1.1.6.2 skrll return;
266 1.1.6.2 skrll }
267 1.1.6.2 skrll
268 1.1.6.2 skrll #ifdef TIMECOUNTER_DEBUG
269 1.1.6.2 skrll old_factor = (ci->ci_cc.cc_delta * 1000 ) / ci->ci_cc.cc_denom;
270 1.1.6.2 skrll #endif
271 1.1.6.2 skrll
272 1.1.6.2 skrll /* local ticks per period */
273 1.1.6.2 skrll denom = ci->ci_cc.cc_cc - denom;
274 1.1.6.2 skrll if (denom < 0)
275 1.1.6.2 skrll denom += 0x100000000LL;
276 1.1.6.2 skrll
277 1.1.6.2 skrll ci->ci_cc.cc_denom = denom;
278 1.1.6.2 skrll
279 1.1.6.2 skrll /* reference ticks per period */
280 1.1.6.2 skrll delta = val - ci->ci_cc.cc_val;
281 1.1.6.2 skrll if (delta < 0)
282 1.1.6.2 skrll delta += 0x100000000LL;
283 1.1.6.2 skrll
284 1.1.6.2 skrll ci->ci_cc.cc_val = val;
285 1.1.6.2 skrll ci->ci_cc.cc_delta = delta;
286 1.1.6.2 skrll
287 1.1.6.2 skrll /* publish new generation number */
288 1.1.6.2 skrll ci->ci_cc.cc_gen = gen;
289 1.1.6.2 skrll splx(s);
290 1.1.6.2 skrll
291 1.1.6.2 skrll #ifdef TIMECOUNTER_DEBUG
292 1.1.6.2 skrll factor = (delta * 1000) / denom - old_factor;
293 1.1.6.2 skrll if (factor < 0)
294 1.1.6.2 skrll factor = -factor;
295 1.1.6.2 skrll
296 1.1.6.2 skrll if (factor > old_factor / 10)
297 1.1.6.2 skrll printf("cc_calibrate_cpu[%lu]: 10%% exceeded - delta %"
298 1.1.6.2 skrll PRId64 ", denom %" PRId64 ", factor %" PRId64
299 1.1.6.2 skrll ", old factor %" PRId64"\n", ci->ci_cpuid,
300 1.1.6.2 skrll delta, denom, (delta * 1000) / denom, old_factor);
301 1.1.6.2 skrll #endif /* TIMECOUNTER_DEBUG */
302 1.1.6.2 skrll }
303