xref: /titanic_41/usr/src/uts/i86pc/os/timestamp.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/disp.h>
33 #include <sys/var.h>
34 #include <sys/cmn_err.h>
35 #include <sys/debug.h>
36 #include <sys/x86_archext.h>
37 #include <sys/archsystm.h>
38 #include <sys/cpuvar.h>
39 #include <sys/psm_defs.h>
40 #include <sys/clock.h>
41 #include <sys/atomic.h>
42 #include <sys/lockstat.h>
43 #include <sys/smp_impldefs.h>
44 #include <sys/dtrace.h>
45 #include <sys/time.h>
46 
47 /*
48  * Using the Pentium's TSC register for gethrtime()
49  * ------------------------------------------------
50  *
51  * The Pentium family, like many chip architectures, has a high-resolution
52  * timestamp counter ("TSC") which increments once per CPU cycle.  The contents
53  * of the timestamp counter are read with the RDTSC instruction.
54  *
55  * As with its UltraSPARC equivalent (the %tick register), TSC's cycle count
56  * must be translated into nanoseconds in order to implement gethrtime().
57  * We avoid inducing floating point operations in this conversion by
58  * implementing the same nsec_scale algorithm as that found in the sun4u
59  * platform code.  The sun4u NATIVE_TIME_TO_NSEC_SCALE block comment contains
60  * a detailed description of the algorithm; the comment is not reproduced
61  * here.  This implementation differs only in its value for NSEC_SHIFT:
62  * we implement an NSEC_SHIFT of 5 (instead of sun4u's 4) to allow for
63  * 60 MHz Pentiums.
64  *
65  * While TSC and %tick are both cycle counting registers, TSC's functionality
66  * falls short in several critical ways:
67  *
68  *  (a)	TSCs on different CPUs are not guaranteed to be in sync.  While in
69  *	practice they often _are_ in sync, this isn't guaranteed by the
70  *	architecture.
71  *
72  *  (b)	The TSC cannot be reliably set to an arbitrary value.  The architecture
73  *	only supports writing the low 32-bits of TSC, making it impractical
74  *	to rewrite.
75  *
76  *  (c)	The architecture doesn't have the capacity to interrupt based on
77  *	arbitrary values of TSC; there is no TICK_CMPR equivalent.
78  *
79  * Together, (a) and (b) imply that software must track the skew between
80  * TSCs and account for it (it is assumed that while there may exist skew,
81  * there does not exist drift).  To determine the skew between CPUs, we
82  * have newly onlined CPUs call tsc_sync_slave(), while the CPU performing
83  * the online operation calls tsc_sync_master().  Once both CPUs are ready,
84  * the master sets a shared flag, and each reads its TSC register.  To reduce
85  * bias, we then wait until both CPUs are ready again, but this time the
86  * slave sets the shared flag, and each reads its TSC register again. The
87  * master compares the average of the two sample values, and, if observable
88  * skew is found, changes the gethrtimef function pointer to point to a
89  * gethrtime() implementation which will take the discovered skew into
90  * consideration.
91  *
92  * In the absence of time-of-day clock adjustments, gethrtime() must stay in
93  * sync with gettimeofday().  This is problematic; given (c), the software
94  * cannot drive its time-of-day source from TSC, and yet they must somehow be
95  * kept in sync.  We implement this by having a routine, tsc_tick(), which
96  * is called once per second from the interrupt which drives time-of-day.
97  * tsc_tick() recalculates nsec_scale based on the number of the CPU cycles
98  * since boot versus the number of seconds since boot.  This algorithm
99  * becomes more accurate over time and converges quickly; the error in
100  * nsec_scale is typically under 1 ppm less than 10 seconds after boot, and
101  * is less than 100 ppb 1 minute after boot.
102  *
103  * Note that the hrtime base for gethrtime, tsc_hrtime_base, is modified
104  * atomically with nsec_scale under CLOCK_LOCK.  This assures that time
105  * monotonically increases.
106  */
107 
108 #define	NSEC_SHIFT 5
109 
110 static uint_t nsec_scale;
111 
112 /*
113  * These two variables used to be grouped together inside of a structure that
114  * lived on a single cache line. A regression (bug ID 4623398) caused the
115  * compiler to emit code that "optimized" away the while-loops below. The
116  * result was that no synchronization between the onlining and onlined CPUs
117  * took place.
118  */
119 static volatile int tsc_ready;
120 static volatile int tsc_sync_go;
121 
122 /*
123  * Used as indices into the tsc_sync_snaps[] array.
124  */
125 #define	TSC_MASTER		0
126 #define	TSC_SLAVE		1
127 
128 /*
129  * Used in the tsc_master_sync()/tsc_slave_sync() rendezvous.
130  */
131 #define	TSC_SYNC_STOP		1
132 #define	TSC_SYNC_GO		2
133 #define	TSC_SYNC_AGAIN		3
134 
135 /*
136  * XX64	Is the faster way to do this with a 64-bit ABI?
137  */
138 #define	TSC_CONVERT_AND_ADD(tsc, hrt, scale) { \
139 	unsigned int *_l = (unsigned int *)&(tsc); \
140 	(hrt) += mul32(_l[1], scale) << NSEC_SHIFT; \
141 	(hrt) += mul32(_l[0], scale) >> (32 - NSEC_SHIFT); \
142 }
143 
144 #define	TSC_CONVERT(tsc, hrt, scale) { \
145 	unsigned int *_l = (unsigned int *)&(tsc); \
146 	(hrt) = mul32(_l[1], scale) << NSEC_SHIFT; \
147 	(hrt) += mul32(_l[0], scale) >> (32 - NSEC_SHIFT); \
148 }
149 
150 
151 
152 static int	tsc_max_delta;
153 static hrtime_t tsc_sync_snaps[2];
154 static hrtime_t tsc_sync_delta[NCPU];
155 static hrtime_t tsc_sync_tick_delta[NCPU];
156 static hrtime_t	tsc_last = 0;
157 static hrtime_t	tsc_last_jumped = 0;
158 static hrtime_t	tsc_hrtime_base = 0;
159 static int	tsc_jumped = 0;
160 
161 static hrtime_t	shadow_tsc_hrtime_base;
162 static hrtime_t	shadow_tsc_last;
163 static uint_t	shadow_nsec_scale;
164 static uint32_t	shadow_hres_lock;
165 
166 /*
167  * Called by the master after the sync operation is complete.  If the
168  * slave is discovered to lag, gethrtimef will be changed to point to
169  * tsc_gethrtime_delta().
170  */
171 static void
172 tsc_digest(processorid_t target)
173 {
174 	hrtime_t tdelta, hdelta = 0;
175 	int max = tsc_max_delta;
176 	processorid_t source = CPU->cpu_id;
177 	int update;
178 
179 	update = tsc_sync_delta[source] != 0 ||
180 	    gethrtimef == tsc_gethrtime_delta;
181 
182 	/*
183 	 * We divide by 2 since each of the data points is the sum of two TSC
184 	 * reads; this takes the average of the two.
185 	 */
186 	tdelta = (tsc_sync_snaps[TSC_SLAVE] - tsc_sync_snaps[TSC_MASTER]) / 2;
187 	if ((tdelta > max) || ((tdelta >= 0) && update)) {
188 		TSC_CONVERT_AND_ADD(tdelta, hdelta, nsec_scale);
189 		tsc_sync_delta[target] = tsc_sync_delta[source] - hdelta;
190 		tsc_sync_tick_delta[target] = -tdelta;
191 		gethrtimef = tsc_gethrtime_delta;
192 		gethrtimeunscaledf = tsc_gethrtimeunscaled_delta;
193 		return;
194 	}
195 
196 	tdelta = -tdelta;
197 	if ((tdelta > max) || update) {
198 		TSC_CONVERT_AND_ADD(tdelta, hdelta, nsec_scale);
199 		tsc_sync_delta[target] = tsc_sync_delta[source] + hdelta;
200 		tsc_sync_tick_delta[target] = tdelta;
201 		gethrtimef = tsc_gethrtime_delta;
202 		gethrtimeunscaledf = tsc_gethrtimeunscaled_delta;
203 	}
204 
205 }
206 
207 /*
208  * Called by a CPU which has just performed an online operation on another
209  * CPU.  It is expected that the newly onlined CPU will call tsc_sync_slave().
210  */
211 void
212 tsc_sync_master(processorid_t slave)
213 {
214 	int flags;
215 	hrtime_t hrt;
216 
217 	ASSERT(tsc_sync_go != TSC_SYNC_GO);
218 
219 	flags = clear_int_flag();
220 
221 	/*
222 	 * Wait for the slave CPU to arrive.
223 	 */
224 	while (tsc_ready != TSC_SYNC_GO)
225 		continue;
226 
227 	/*
228 	 * Tell the slave CPU to begin reading its TSC; read our own.
229 	 */
230 	tsc_sync_go = TSC_SYNC_GO;
231 	hrt = tsc_read();
232 
233 	/*
234 	 * Tell the slave that we're ready, and wait for the slave to tell us
235 	 * to read our TSC again.
236 	 */
237 	tsc_ready = TSC_SYNC_AGAIN;
238 	while (tsc_sync_go != TSC_SYNC_AGAIN)
239 		continue;
240 
241 	hrt += tsc_read();
242 	tsc_sync_snaps[TSC_MASTER] = hrt;
243 
244 	/*
245 	 * Wait for the slave to finish reading its TSC.
246 	 */
247 	while (tsc_ready != TSC_SYNC_STOP)
248 		continue;
249 
250 	/*
251 	 * At this point, both CPUs have performed their tsc_read() calls.
252 	 * We'll digest it now before letting the slave CPU return.
253 	 */
254 	tsc_digest(slave);
255 	tsc_sync_go = TSC_SYNC_STOP;
256 
257 	restore_int_flag(flags);
258 }
259 
260 /*
261  * Called by a CPU which has just been onlined.  It is expected that the CPU
262  * performing the online operation will call tsc_sync_master().
263  */
264 void
265 tsc_sync_slave(void)
266 {
267 	int flags;
268 	hrtime_t hrt;
269 
270 	ASSERT(tsc_sync_go != TSC_SYNC_GO);
271 
272 	flags = clear_int_flag();
273 
274 	/* to test tsc_gethrtime_delta, add wrmsr(REG_TSC, 0) here */
275 
276 	/*
277 	 * Tell the master CPU that we're ready, and wait for the master to
278 	 * tell us to begin reading our TSC.
279 	 */
280 	tsc_ready = TSC_SYNC_GO;
281 	while (tsc_sync_go != TSC_SYNC_GO)
282 		continue;
283 
284 	hrt = tsc_read();
285 
286 	/*
287 	 * Wait for the master CPU to be ready to read its TSC again.
288 	 */
289 	while (tsc_ready != TSC_SYNC_AGAIN)
290 		continue;
291 
292 	/*
293 	 * Tell the master CPU to read its TSC again; read ours again.
294 	 */
295 	tsc_sync_go = TSC_SYNC_AGAIN;
296 
297 	hrt += tsc_read();
298 	tsc_sync_snaps[TSC_SLAVE] = hrt;
299 
300 	/*
301 	 * Tell the master that we're done, and wait to be dismissed.
302 	 */
303 	tsc_ready = TSC_SYNC_STOP;
304 	while (tsc_sync_go != TSC_SYNC_STOP)
305 		continue;
306 
307 	restore_int_flag(flags);
308 }
309 
310 void
311 tsc_hrtimeinit(uint64_t cpu_freq_hz)
312 {
313 	longlong_t tsc;
314 	int flags;
315 
316 	/*
317 	 * cpu_freq_hz is the measured cpu frequency in hertz
318 	 */
319 
320 	/*
321 	 * We can't accommodate CPUs slower than 31.25 MHz.
322 	 */
323 	ASSERT(cpu_freq_hz > NANOSEC / (1 << NSEC_SHIFT));
324 	nsec_scale =
325 	    (uint_t)
326 		(((uint64_t)NANOSEC << (32 - NSEC_SHIFT)) / cpu_freq_hz);
327 
328 	flags = clear_int_flag();
329 	tsc = tsc_read();
330 	(void) tsc_gethrtime();
331 	tsc_max_delta = tsc_read() - tsc;
332 	restore_int_flag(flags);
333 }
334 
335 /*
336  * Called once per second on some CPU from the cyclic subsystem's
337  * CY_HIGH_LEVEL interrupt.  (no longer CPU0-only)
338  */
339 void
340 tsc_tick(void)
341 {
342 	hrtime_t now, delta;
343 	ushort_t spl;
344 
345 	/*
346 	 * Before we set the new variables, we set the shadow values.  This
347 	 * allows for lock free operation in dtrace_gethrtime().
348 	 */
349 	lock_set_spl((lock_t *)&shadow_hres_lock + HRES_LOCK_OFFSET,
350 	    ipltospl(CBE_HIGH_PIL), &spl);
351 
352 	shadow_tsc_hrtime_base = tsc_hrtime_base;
353 	shadow_tsc_last = tsc_last;
354 	shadow_nsec_scale = nsec_scale;
355 
356 	shadow_hres_lock++;
357 	splx(spl);
358 
359 	CLOCK_LOCK(&spl);
360 
361 	now = tsc_read();
362 
363 	if (gethrtimef == tsc_gethrtime_delta)
364 		now += tsc_sync_tick_delta[CPU->cpu_id];
365 
366 	if (now < tsc_last) {
367 		/*
368 		 * The TSC has just jumped into the past.  We assume that
369 		 * this is due to a suspend/resume cycle, and we're going
370 		 * to use the _current_ value of TSC as the delta.  This
371 		 * will keep tsc_hrtime_base correct.  We're also going to
372 		 * assume that rate of tsc does not change after a suspend
373 		 * resume (i.e nsec_scale remains the same).
374 		 */
375 		delta = now;
376 		tsc_last_jumped += tsc_last;
377 		tsc_jumped = 1;
378 	} else {
379 		/*
380 		 * Determine the number of TSC ticks since the last clock
381 		 * tick, and add that to the hrtime base.
382 		 */
383 		delta = now - tsc_last;
384 	}
385 
386 	TSC_CONVERT_AND_ADD(delta, tsc_hrtime_base, nsec_scale);
387 	tsc_last = now;
388 
389 	CLOCK_UNLOCK(spl);
390 }
391 
392 hrtime_t
393 tsc_gethrtime(void)
394 {
395 	uint32_t old_hres_lock;
396 	hrtime_t tsc, hrt;
397 
398 	do {
399 		old_hres_lock = hres_lock;
400 
401 		if ((tsc = tsc_read()) >= tsc_last) {
402 			/*
403 			 * It would seem to be obvious that this is true
404 			 * (that is, the past is less than the present),
405 			 * but it isn't true in the presence of suspend/resume
406 			 * cycles.  If we manage to call gethrtime()
407 			 * after a resume, but before the first call to
408 			 * tsc_tick(), we will see the jump.  In this case,
409 			 * we will simply use the value in TSC as the delta.
410 			 */
411 			tsc -= tsc_last;
412 		} else if (tsc >= tsc_last - 2*tsc_max_delta) {
413 			/*
414 			 * There is a chance that tsc_tick() has just run on
415 			 * another CPU, and we have drifted just enough so that
416 			 * we appear behind tsc_last.  In this case, force the
417 			 * delta to be zero.
418 			 */
419 			tsc = 0;
420 		}
421 
422 		hrt = tsc_hrtime_base;
423 
424 		TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale);
425 	} while ((old_hres_lock & ~1) != hres_lock);
426 
427 	return (hrt);
428 }
429 
430 /*
431  * This is similar to the above, but it cannot actually spin on hres_lock.
432  * As a result, it caches all of the variables it needs; if the variables
433  * don't change, it's done.
434  */
435 hrtime_t
436 dtrace_gethrtime(void)
437 {
438 	uint32_t old_hres_lock;
439 	hrtime_t tsc, hrt;
440 
441 	do {
442 		old_hres_lock = hres_lock;
443 
444 		/*
445 		 * See the comments in tsc_gethrtime(), above.
446 		 */
447 		if ((tsc = tsc_read()) >= tsc_last)
448 			tsc -= tsc_last;
449 		else if (tsc >= tsc_last - 2*tsc_max_delta)
450 			tsc = 0;
451 
452 		hrt = tsc_hrtime_base;
453 
454 		TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale);
455 
456 		if ((old_hres_lock & ~1) == hres_lock)
457 			break;
458 
459 		/*
460 		 * If we're here, the clock lock is locked -- or it has been
461 		 * unlocked and locked since we looked.  This may be due to
462 		 * tsc_tick() running on another CPU -- or it may be because
463 		 * some code path has ended up in dtrace_probe() with
464 		 * CLOCK_LOCK held.  We'll try to determine that we're in
465 		 * the former case by taking another lap if the lock has
466 		 * changed since when we first looked at it.
467 		 */
468 		if (old_hres_lock != hres_lock)
469 			continue;
470 
471 		/*
472 		 * So the lock was and is locked.  We'll use the old data
473 		 * instead.
474 		 */
475 		old_hres_lock = shadow_hres_lock;
476 
477 		/*
478 		 * See the comments in tsc_gethrtime(), above.
479 		 */
480 		if ((tsc = tsc_read()) >= shadow_tsc_last)
481 			tsc -= shadow_tsc_last;
482 		else if (tsc >= shadow_tsc_last - 2 * tsc_max_delta)
483 			tsc = 0;
484 
485 		hrt = shadow_tsc_hrtime_base;
486 
487 		TSC_CONVERT_AND_ADD(tsc, hrt, shadow_nsec_scale);
488 	} while ((old_hres_lock & ~1) != shadow_hres_lock);
489 
490 	return (hrt);
491 }
492 
493 hrtime_t
494 tsc_gethrtime_delta(void)
495 {
496 	uint32_t old_hres_lock;
497 	hrtime_t tsc, hrt;
498 	int flags;
499 
500 	do {
501 		old_hres_lock = hres_lock;
502 
503 		/*
504 		 * We need to disable interrupts here to assure that we
505 		 * don't migrate between the call to tsc_read() and
506 		 * adding the CPU's TSC tick delta. Note that disabling
507 		 * and reenabling preemption is forbidden here because
508 		 * we may be in the middle of a fast trap. In the amd64
509 		 * kernel we cannot tolerate preemption during a fast
510 		 * trap. See _update_sregs().
511 		 */
512 
513 		flags = clear_int_flag();
514 		tsc = tsc_read() + tsc_sync_tick_delta[CPU->cpu_id];
515 		restore_int_flag(flags);
516 
517 		/* See comments in tsc_gethrtime() above */
518 
519 		if (tsc >= tsc_last) {
520 			tsc -= tsc_last;
521 		} else if (tsc >= tsc_last - 2 * tsc_max_delta) {
522 			tsc = 0;
523 		}
524 
525 		hrt = tsc_hrtime_base;
526 
527 		TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale);
528 	} while ((old_hres_lock & ~1) != hres_lock);
529 
530 	return (hrt);
531 }
532 
533 extern uint64_t cpu_freq_hz;
534 extern int tsc_gethrtime_enable;
535 
536 /*
537  * The following converts nanoseconds of highres-time to ticks
538  */
539 
540 static uint64_t
541 hrtime2tick(hrtime_t ts)
542 {
543 	hrtime_t q = ts / NANOSEC;
544 	hrtime_t r = ts - (q * NANOSEC);
545 
546 	return (q * cpu_freq_hz + ((r * cpu_freq_hz) / NANOSEC));
547 }
548 
549 /*
550  * This is used to convert scaled high-res time from nanoseconds to
551  * unscaled hardware ticks.  (Read from hardware timestamp counter)
552  */
553 
554 uint64_t
555 unscalehrtime(hrtime_t ts)
556 {
557 	if (tsc_gethrtime_enable) {
558 		uint64_t unscale = 0;
559 		hrtime_t rescale;
560 		hrtime_t diff = ts;
561 
562 		while (diff > (nsec_per_tick)) {
563 			unscale += hrtime2tick(diff);
564 			rescale = unscale;
565 			scalehrtime(&rescale);
566 			diff = ts - rescale;
567 		}
568 
569 		return (unscale);
570 	}
571 	return (0);
572 }
573 
574 
575 hrtime_t
576 tsc_gethrtimeunscaled(void)
577 {
578 	uint32_t old_hres_lock;
579 	hrtime_t tsc;
580 
581 	do {
582 		old_hres_lock = hres_lock;
583 
584 		if ((tsc = tsc_read()) < tsc_last) {
585 			/*
586 			 * see comments in tsc_gethrtime
587 			 */
588 			tsc += tsc_last_jumped;
589 		}
590 
591 	} while ((old_hres_lock & ~1) != hres_lock);
592 
593 	return (tsc);
594 }
595 
596 
597 /* Convert a tsc timestamp to nanoseconds */
598 void
599 tsc_scalehrtime(hrtime_t *tsc)
600 {
601 	hrtime_t hrt;
602 	hrtime_t mytsc;
603 
604 	if (tsc == NULL)
605 		return;
606 	mytsc = *tsc;
607 
608 	TSC_CONVERT(mytsc, hrt, nsec_scale);
609 	*tsc  = hrt;
610 }
611 
612 hrtime_t
613 tsc_gethrtimeunscaled_delta(void)
614 {
615 	hrtime_t hrt;
616 	int flags;
617 
618 	/*
619 	 * Similarly to tsc_gethrtime_delta, we need to disable preemption
620 	 * to prevent migration between the call to tsc_gethrtimeunscaled
621 	 * and adding the CPU's hrtime delta. Note that disabling and
622 	 * reenabling preemption is forbidden here because we may be in the
623 	 * middle of a fast trap. In the amd64 kernel we cannot tolerate
624 	 * preemption during a fast trap. See _update_sregs().
625 	 */
626 
627 	flags = clear_int_flag();
628 	hrt = tsc_gethrtimeunscaled() + tsc_sync_tick_delta[CPU->cpu_id];
629 	restore_int_flag(flags);
630 
631 	return (hrt);
632 }
633