10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53446Smrj * Common Development and Distribution License (the "License"). 63446Smrj * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215084Sjohnlev 220Sstevel@tonic-gate /* 233446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/param.h> 310Sstevel@tonic-gate #include <sys/systm.h> 320Sstevel@tonic-gate #include <sys/disp.h> 330Sstevel@tonic-gate #include <sys/var.h> 340Sstevel@tonic-gate #include <sys/cmn_err.h> 350Sstevel@tonic-gate #include <sys/debug.h> 360Sstevel@tonic-gate #include <sys/x86_archext.h> 370Sstevel@tonic-gate #include <sys/archsystm.h> 380Sstevel@tonic-gate #include <sys/cpuvar.h> 390Sstevel@tonic-gate #include <sys/psm_defs.h> 400Sstevel@tonic-gate #include <sys/clock.h> 410Sstevel@tonic-gate #include <sys/atomic.h> 420Sstevel@tonic-gate #include <sys/lockstat.h> 430Sstevel@tonic-gate #include <sys/smp_impldefs.h> 440Sstevel@tonic-gate #include <sys/dtrace.h> 450Sstevel@tonic-gate #include <sys/time.h> 465084Sjohnlev #include <sys/panic.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* 490Sstevel@tonic-gate * Using the Pentium's TSC register for gethrtime() 500Sstevel@tonic-gate * ------------------------------------------------ 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * The Pentium family, like many chip architectures, has a high-resolution 530Sstevel@tonic-gate * timestamp counter ("TSC") which increments once per CPU cycle. The contents 540Sstevel@tonic-gate * of the timestamp counter are read with the RDTSC instruction. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * As with its UltraSPARC equivalent (the %tick register), TSC's cycle count 570Sstevel@tonic-gate * must be translated into nanoseconds in order to implement gethrtime(). 580Sstevel@tonic-gate * We avoid inducing floating point operations in this conversion by 590Sstevel@tonic-gate * implementing the same nsec_scale algorithm as that found in the sun4u 600Sstevel@tonic-gate * platform code. The sun4u NATIVE_TIME_TO_NSEC_SCALE block comment contains 610Sstevel@tonic-gate * a detailed description of the algorithm; the comment is not reproduced 620Sstevel@tonic-gate * here. This implementation differs only in its value for NSEC_SHIFT: 630Sstevel@tonic-gate * we implement an NSEC_SHIFT of 5 (instead of sun4u's 4) to allow for 640Sstevel@tonic-gate * 60 MHz Pentiums. 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * While TSC and %tick are both cycle counting registers, TSC's functionality 670Sstevel@tonic-gate * falls short in several critical ways: 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * (a) TSCs on different CPUs are not guaranteed to be in sync. While in 700Sstevel@tonic-gate * practice they often _are_ in sync, this isn't guaranteed by the 710Sstevel@tonic-gate * architecture. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * (b) The TSC cannot be reliably set to an arbitrary value. The architecture 740Sstevel@tonic-gate * only supports writing the low 32-bits of TSC, making it impractical 750Sstevel@tonic-gate * to rewrite. 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * (c) The architecture doesn't have the capacity to interrupt based on 780Sstevel@tonic-gate * arbitrary values of TSC; there is no TICK_CMPR equivalent. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * Together, (a) and (b) imply that software must track the skew between 810Sstevel@tonic-gate * TSCs and account for it (it is assumed that while there may exist skew, 820Sstevel@tonic-gate * there does not exist drift). To determine the skew between CPUs, we 830Sstevel@tonic-gate * have newly onlined CPUs call tsc_sync_slave(), while the CPU performing 840Sstevel@tonic-gate * the online operation calls tsc_sync_master(). Once both CPUs are ready, 850Sstevel@tonic-gate * the master sets a shared flag, and each reads its TSC register. To reduce 860Sstevel@tonic-gate * bias, we then wait until both CPUs are ready again, but this time the 870Sstevel@tonic-gate * slave sets the shared flag, and each reads its TSC register again. The 880Sstevel@tonic-gate * master compares the average of the two sample values, and, if observable 890Sstevel@tonic-gate * skew is found, changes the gethrtimef function pointer to point to a 900Sstevel@tonic-gate * gethrtime() implementation which will take the discovered skew into 910Sstevel@tonic-gate * consideration. 920Sstevel@tonic-gate * 930Sstevel@tonic-gate * In the absence of time-of-day clock adjustments, gethrtime() must stay in 940Sstevel@tonic-gate * sync with gettimeofday(). This is problematic; given (c), the software 950Sstevel@tonic-gate * cannot drive its time-of-day source from TSC, and yet they must somehow be 960Sstevel@tonic-gate * kept in sync. We implement this by having a routine, tsc_tick(), which 970Sstevel@tonic-gate * is called once per second from the interrupt which drives time-of-day. 980Sstevel@tonic-gate * tsc_tick() recalculates nsec_scale based on the number of the CPU cycles 990Sstevel@tonic-gate * since boot versus the number of seconds since boot. This algorithm 1000Sstevel@tonic-gate * becomes more accurate over time and converges quickly; the error in 1010Sstevel@tonic-gate * nsec_scale is typically under 1 ppm less than 10 seconds after boot, and 1020Sstevel@tonic-gate * is less than 100 ppb 1 minute after boot. 1030Sstevel@tonic-gate * 1040Sstevel@tonic-gate * Note that the hrtime base for gethrtime, tsc_hrtime_base, is modified 1050Sstevel@tonic-gate * atomically with nsec_scale under CLOCK_LOCK. This assures that time 1060Sstevel@tonic-gate * monotonically increases. 1070Sstevel@tonic-gate */ 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate #define NSEC_SHIFT 5 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate static uint_t nsec_scale; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* 1140Sstevel@tonic-gate * These two variables used to be grouped together inside of a structure that 1150Sstevel@tonic-gate * lived on a single cache line. A regression (bug ID 4623398) caused the 1160Sstevel@tonic-gate * compiler to emit code that "optimized" away the while-loops below. The 1170Sstevel@tonic-gate * result was that no synchronization between the onlining and onlined CPUs 1180Sstevel@tonic-gate * took place. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate static volatile int tsc_ready; 1210Sstevel@tonic-gate static volatile int tsc_sync_go; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate /* 1240Sstevel@tonic-gate * Used as indices into the tsc_sync_snaps[] array. 1250Sstevel@tonic-gate */ 1260Sstevel@tonic-gate #define TSC_MASTER 0 1270Sstevel@tonic-gate #define TSC_SLAVE 1 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * Used in the tsc_master_sync()/tsc_slave_sync() rendezvous. 1310Sstevel@tonic-gate */ 1320Sstevel@tonic-gate #define TSC_SYNC_STOP 1 1330Sstevel@tonic-gate #define TSC_SYNC_GO 2 1340Sstevel@tonic-gate #define TSC_SYNC_AGAIN 3 1350Sstevel@tonic-gate 1365084Sjohnlev #define TSC_CONVERT_AND_ADD(tsc, hrt, scale) { \ 1373446Smrj unsigned int *_l = (unsigned int *)&(tsc); \ 1383446Smrj (hrt) += mul32(_l[1], scale) << NSEC_SHIFT; \ 1390Sstevel@tonic-gate (hrt) += mul32(_l[0], scale) >> (32 - NSEC_SHIFT); \ 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1423446Smrj #define TSC_CONVERT(tsc, hrt, scale) { \ 1433446Smrj unsigned int *_l = (unsigned int *)&(tsc); \ 1443446Smrj (hrt) = mul32(_l[1], scale) << NSEC_SHIFT; \ 1450Sstevel@tonic-gate (hrt) += mul32(_l[0], scale) >> (32 - NSEC_SHIFT); \ 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1483446Smrj int tsc_master_slave_sync_needed = 1; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate static int tsc_max_delta; 1510Sstevel@tonic-gate static hrtime_t tsc_sync_snaps[2]; 1520Sstevel@tonic-gate static hrtime_t tsc_sync_delta[NCPU]; 1530Sstevel@tonic-gate static hrtime_t tsc_sync_tick_delta[NCPU]; 1540Sstevel@tonic-gate static hrtime_t tsc_last = 0; 1550Sstevel@tonic-gate static hrtime_t tsc_last_jumped = 0; 1560Sstevel@tonic-gate static hrtime_t tsc_hrtime_base = 0; 1570Sstevel@tonic-gate static int tsc_jumped = 0; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static hrtime_t shadow_tsc_hrtime_base; 1600Sstevel@tonic-gate static hrtime_t shadow_tsc_last; 1610Sstevel@tonic-gate static uint_t shadow_nsec_scale; 1620Sstevel@tonic-gate static uint32_t shadow_hres_lock; 163*5295Srandyf int get_tsc_ready(); 1640Sstevel@tonic-gate 1655084Sjohnlev hrtime_t 1665084Sjohnlev tsc_gethrtime(void) 1675084Sjohnlev { 1685084Sjohnlev uint32_t old_hres_lock; 1695084Sjohnlev hrtime_t tsc, hrt; 1705084Sjohnlev 1715084Sjohnlev do { 1725084Sjohnlev old_hres_lock = hres_lock; 1735084Sjohnlev 1745084Sjohnlev if ((tsc = tsc_read()) >= tsc_last) { 1755084Sjohnlev /* 1765084Sjohnlev * It would seem to be obvious that this is true 1775084Sjohnlev * (that is, the past is less than the present), 1785084Sjohnlev * but it isn't true in the presence of suspend/resume 1795084Sjohnlev * cycles. If we manage to call gethrtime() 1805084Sjohnlev * after a resume, but before the first call to 1815084Sjohnlev * tsc_tick(), we will see the jump. In this case, 1825084Sjohnlev * we will simply use the value in TSC as the delta. 1835084Sjohnlev */ 1845084Sjohnlev tsc -= tsc_last; 1855084Sjohnlev } else if (tsc >= tsc_last - 2*tsc_max_delta) { 1865084Sjohnlev /* 1875084Sjohnlev * There is a chance that tsc_tick() has just run on 1885084Sjohnlev * another CPU, and we have drifted just enough so that 1895084Sjohnlev * we appear behind tsc_last. In this case, force the 1905084Sjohnlev * delta to be zero. 1915084Sjohnlev */ 1925084Sjohnlev tsc = 0; 1935084Sjohnlev } 1945084Sjohnlev 1955084Sjohnlev hrt = tsc_hrtime_base; 1965084Sjohnlev 1975084Sjohnlev TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale); 1985084Sjohnlev } while ((old_hres_lock & ~1) != hres_lock); 1995084Sjohnlev 2005084Sjohnlev return (hrt); 2015084Sjohnlev } 2025084Sjohnlev 2035084Sjohnlev hrtime_t 2045084Sjohnlev tsc_gethrtime_delta(void) 2055084Sjohnlev { 2065084Sjohnlev uint32_t old_hres_lock; 2075084Sjohnlev hrtime_t tsc, hrt; 2085084Sjohnlev int flags; 2095084Sjohnlev 2105084Sjohnlev do { 2115084Sjohnlev old_hres_lock = hres_lock; 2125084Sjohnlev 2135084Sjohnlev /* 2145084Sjohnlev * We need to disable interrupts here to assure that we 2155084Sjohnlev * don't migrate between the call to tsc_read() and 2165084Sjohnlev * adding the CPU's TSC tick delta. Note that disabling 2175084Sjohnlev * and reenabling preemption is forbidden here because 2185084Sjohnlev * we may be in the middle of a fast trap. In the amd64 2195084Sjohnlev * kernel we cannot tolerate preemption during a fast 2205084Sjohnlev * trap. See _update_sregs(). 2215084Sjohnlev */ 2225084Sjohnlev 2235084Sjohnlev flags = clear_int_flag(); 2245084Sjohnlev tsc = tsc_read() + tsc_sync_tick_delta[CPU->cpu_id]; 2255084Sjohnlev restore_int_flag(flags); 2265084Sjohnlev 2275084Sjohnlev /* See comments in tsc_gethrtime() above */ 2285084Sjohnlev 2295084Sjohnlev if (tsc >= tsc_last) { 2305084Sjohnlev tsc -= tsc_last; 2315084Sjohnlev } else if (tsc >= tsc_last - 2 * tsc_max_delta) { 2325084Sjohnlev tsc = 0; 2335084Sjohnlev } 2345084Sjohnlev 2355084Sjohnlev hrt = tsc_hrtime_base; 2365084Sjohnlev 2375084Sjohnlev TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale); 2385084Sjohnlev } while ((old_hres_lock & ~1) != hres_lock); 2395084Sjohnlev 2405084Sjohnlev return (hrt); 2415084Sjohnlev } 2425084Sjohnlev 2435084Sjohnlev /* 2445084Sjohnlev * This is similar to the above, but it cannot actually spin on hres_lock. 2455084Sjohnlev * As a result, it caches all of the variables it needs; if the variables 2465084Sjohnlev * don't change, it's done. 2475084Sjohnlev */ 2485084Sjohnlev hrtime_t 2495084Sjohnlev dtrace_gethrtime(void) 2505084Sjohnlev { 2515084Sjohnlev uint32_t old_hres_lock; 2525084Sjohnlev hrtime_t tsc, hrt; 2535084Sjohnlev int flags; 2545084Sjohnlev 2555084Sjohnlev do { 2565084Sjohnlev old_hres_lock = hres_lock; 2575084Sjohnlev 2585084Sjohnlev /* 2595084Sjohnlev * Interrupts are disabled to ensure that the thread isn't 2605084Sjohnlev * migrated between the tsc_read() and adding the CPU's 2615084Sjohnlev * TSC tick delta. 2625084Sjohnlev */ 2635084Sjohnlev flags = clear_int_flag(); 2645084Sjohnlev 2655084Sjohnlev tsc = tsc_read(); 2665084Sjohnlev 2675084Sjohnlev if (gethrtimef == tsc_gethrtime_delta) 2685084Sjohnlev tsc += tsc_sync_tick_delta[CPU->cpu_id]; 2695084Sjohnlev 2705084Sjohnlev restore_int_flag(flags); 2715084Sjohnlev 2725084Sjohnlev /* 2735084Sjohnlev * See the comments in tsc_gethrtime(), above. 2745084Sjohnlev */ 2755084Sjohnlev if (tsc >= tsc_last) 2765084Sjohnlev tsc -= tsc_last; 2775084Sjohnlev else if (tsc >= tsc_last - 2*tsc_max_delta) 2785084Sjohnlev tsc = 0; 2795084Sjohnlev 2805084Sjohnlev hrt = tsc_hrtime_base; 2815084Sjohnlev 2825084Sjohnlev TSC_CONVERT_AND_ADD(tsc, hrt, nsec_scale); 2835084Sjohnlev 2845084Sjohnlev if ((old_hres_lock & ~1) == hres_lock) 2855084Sjohnlev break; 2865084Sjohnlev 2875084Sjohnlev /* 2885084Sjohnlev * If we're here, the clock lock is locked -- or it has been 2895084Sjohnlev * unlocked and locked since we looked. This may be due to 2905084Sjohnlev * tsc_tick() running on another CPU -- or it may be because 2915084Sjohnlev * some code path has ended up in dtrace_probe() with 2925084Sjohnlev * CLOCK_LOCK held. We'll try to determine that we're in 2935084Sjohnlev * the former case by taking another lap if the lock has 2945084Sjohnlev * changed since when we first looked at it. 2955084Sjohnlev */ 2965084Sjohnlev if (old_hres_lock != hres_lock) 2975084Sjohnlev continue; 2985084Sjohnlev 2995084Sjohnlev /* 3005084Sjohnlev * So the lock was and is locked. We'll use the old data 3015084Sjohnlev * instead. 3025084Sjohnlev */ 3035084Sjohnlev old_hres_lock = shadow_hres_lock; 3045084Sjohnlev 3055084Sjohnlev /* 3065084Sjohnlev * Again, disable interrupts to ensure that the thread 3075084Sjohnlev * isn't migrated between the tsc_read() and adding 3085084Sjohnlev * the CPU's TSC tick delta. 3095084Sjohnlev */ 3105084Sjohnlev flags = clear_int_flag(); 3115084Sjohnlev 3125084Sjohnlev tsc = tsc_read(); 3135084Sjohnlev 3145084Sjohnlev if (gethrtimef == tsc_gethrtime_delta) 3155084Sjohnlev tsc += tsc_sync_tick_delta[CPU->cpu_id]; 3165084Sjohnlev 3175084Sjohnlev restore_int_flag(flags); 3185084Sjohnlev 3195084Sjohnlev /* 3205084Sjohnlev * See the comments in tsc_gethrtime(), above. 3215084Sjohnlev */ 3225084Sjohnlev if (tsc >= shadow_tsc_last) 3235084Sjohnlev tsc -= shadow_tsc_last; 3245084Sjohnlev else if (tsc >= shadow_tsc_last - 2 * tsc_max_delta) 3255084Sjohnlev tsc = 0; 3265084Sjohnlev 3275084Sjohnlev hrt = shadow_tsc_hrtime_base; 3285084Sjohnlev 3295084Sjohnlev TSC_CONVERT_AND_ADD(tsc, hrt, shadow_nsec_scale); 3305084Sjohnlev } while ((old_hres_lock & ~1) != shadow_hres_lock); 3315084Sjohnlev 3325084Sjohnlev return (hrt); 3335084Sjohnlev } 3345084Sjohnlev 3355084Sjohnlev hrtime_t 3365084Sjohnlev tsc_gethrtimeunscaled(void) 3375084Sjohnlev { 3385084Sjohnlev uint32_t old_hres_lock; 3395084Sjohnlev hrtime_t tsc; 3405084Sjohnlev 3415084Sjohnlev do { 3425084Sjohnlev old_hres_lock = hres_lock; 3435084Sjohnlev 3445084Sjohnlev /* See tsc_tick(). */ 3455084Sjohnlev tsc = tsc_read() + tsc_last_jumped; 3465084Sjohnlev } while ((old_hres_lock & ~1) != hres_lock); 3475084Sjohnlev 3485084Sjohnlev return (tsc); 3495084Sjohnlev } 3505084Sjohnlev 3515084Sjohnlev 3525084Sjohnlev /* Convert a tsc timestamp to nanoseconds */ 3535084Sjohnlev void 3545084Sjohnlev tsc_scalehrtime(hrtime_t *tsc) 3555084Sjohnlev { 3565084Sjohnlev hrtime_t hrt; 3575084Sjohnlev hrtime_t mytsc; 3585084Sjohnlev 3595084Sjohnlev if (tsc == NULL) 3605084Sjohnlev return; 3615084Sjohnlev mytsc = *tsc; 3625084Sjohnlev 3635084Sjohnlev TSC_CONVERT(mytsc, hrt, nsec_scale); 3645084Sjohnlev *tsc = hrt; 3655084Sjohnlev } 3665084Sjohnlev 3675084Sjohnlev hrtime_t 3685084Sjohnlev tsc_gethrtimeunscaled_delta(void) 3695084Sjohnlev { 3705084Sjohnlev hrtime_t hrt; 3715084Sjohnlev int flags; 3725084Sjohnlev 3735084Sjohnlev /* 3745084Sjohnlev * Similarly to tsc_gethrtime_delta, we need to disable preemption 3755084Sjohnlev * to prevent migration between the call to tsc_gethrtimeunscaled 3765084Sjohnlev * and adding the CPU's hrtime delta. Note that disabling and 3775084Sjohnlev * reenabling preemption is forbidden here because we may be in the 3785084Sjohnlev * middle of a fast trap. In the amd64 kernel we cannot tolerate 3795084Sjohnlev * preemption during a fast trap. See _update_sregs(). 3805084Sjohnlev */ 3815084Sjohnlev 3825084Sjohnlev flags = clear_int_flag(); 3835084Sjohnlev hrt = tsc_gethrtimeunscaled() + tsc_sync_tick_delta[CPU->cpu_id]; 3845084Sjohnlev restore_int_flag(flags); 3855084Sjohnlev 3865084Sjohnlev return (hrt); 3875084Sjohnlev } 3885084Sjohnlev 3890Sstevel@tonic-gate /* 3900Sstevel@tonic-gate * Called by the master after the sync operation is complete. If the 3910Sstevel@tonic-gate * slave is discovered to lag, gethrtimef will be changed to point to 3920Sstevel@tonic-gate * tsc_gethrtime_delta(). 3930Sstevel@tonic-gate */ 3940Sstevel@tonic-gate static void 3950Sstevel@tonic-gate tsc_digest(processorid_t target) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate hrtime_t tdelta, hdelta = 0; 3980Sstevel@tonic-gate int max = tsc_max_delta; 3990Sstevel@tonic-gate processorid_t source = CPU->cpu_id; 4000Sstevel@tonic-gate int update; 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate update = tsc_sync_delta[source] != 0 || 4030Sstevel@tonic-gate gethrtimef == tsc_gethrtime_delta; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * We divide by 2 since each of the data points is the sum of two TSC 4070Sstevel@tonic-gate * reads; this takes the average of the two. 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate tdelta = (tsc_sync_snaps[TSC_SLAVE] - tsc_sync_snaps[TSC_MASTER]) / 2; 4100Sstevel@tonic-gate if ((tdelta > max) || ((tdelta >= 0) && update)) { 4110Sstevel@tonic-gate TSC_CONVERT_AND_ADD(tdelta, hdelta, nsec_scale); 4120Sstevel@tonic-gate tsc_sync_delta[target] = tsc_sync_delta[source] - hdelta; 413*5295Srandyf tsc_sync_tick_delta[target] = tsc_sync_tick_delta[source] 414*5295Srandyf -tdelta; 4150Sstevel@tonic-gate gethrtimef = tsc_gethrtime_delta; 4160Sstevel@tonic-gate gethrtimeunscaledf = tsc_gethrtimeunscaled_delta; 4170Sstevel@tonic-gate return; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate tdelta = -tdelta; 4210Sstevel@tonic-gate if ((tdelta > max) || update) { 4220Sstevel@tonic-gate TSC_CONVERT_AND_ADD(tdelta, hdelta, nsec_scale); 4230Sstevel@tonic-gate tsc_sync_delta[target] = tsc_sync_delta[source] + hdelta; 424*5295Srandyf tsc_sync_tick_delta[target] = tsc_sync_tick_delta[source] 425*5295Srandyf + tdelta; 4260Sstevel@tonic-gate gethrtimef = tsc_gethrtime_delta; 4270Sstevel@tonic-gate gethrtimeunscaledf = tsc_gethrtimeunscaled_delta; 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * Called by a CPU which has just performed an online operation on another 4340Sstevel@tonic-gate * CPU. It is expected that the newly onlined CPU will call tsc_sync_slave(). 4350Sstevel@tonic-gate */ 4360Sstevel@tonic-gate void 4370Sstevel@tonic-gate tsc_sync_master(processorid_t slave) 4380Sstevel@tonic-gate { 4393446Smrj ulong_t flags; 4400Sstevel@tonic-gate hrtime_t hrt; 4410Sstevel@tonic-gate 4423446Smrj if (!tsc_master_slave_sync_needed) 4433446Smrj return; 4443446Smrj 4450Sstevel@tonic-gate ASSERT(tsc_sync_go != TSC_SYNC_GO); 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate flags = clear_int_flag(); 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate /* 4500Sstevel@tonic-gate * Wait for the slave CPU to arrive. 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate while (tsc_ready != TSC_SYNC_GO) 4530Sstevel@tonic-gate continue; 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate /* 4560Sstevel@tonic-gate * Tell the slave CPU to begin reading its TSC; read our own. 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate tsc_sync_go = TSC_SYNC_GO; 4590Sstevel@tonic-gate hrt = tsc_read(); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* 4620Sstevel@tonic-gate * Tell the slave that we're ready, and wait for the slave to tell us 4630Sstevel@tonic-gate * to read our TSC again. 4640Sstevel@tonic-gate */ 4650Sstevel@tonic-gate tsc_ready = TSC_SYNC_AGAIN; 4660Sstevel@tonic-gate while (tsc_sync_go != TSC_SYNC_AGAIN) 4670Sstevel@tonic-gate continue; 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate hrt += tsc_read(); 4700Sstevel@tonic-gate tsc_sync_snaps[TSC_MASTER] = hrt; 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * Wait for the slave to finish reading its TSC. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate while (tsc_ready != TSC_SYNC_STOP) 4760Sstevel@tonic-gate continue; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * At this point, both CPUs have performed their tsc_read() calls. 4800Sstevel@tonic-gate * We'll digest it now before letting the slave CPU return. 4810Sstevel@tonic-gate */ 4820Sstevel@tonic-gate tsc_digest(slave); 4830Sstevel@tonic-gate tsc_sync_go = TSC_SYNC_STOP; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate restore_int_flag(flags); 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* 4890Sstevel@tonic-gate * Called by a CPU which has just been onlined. It is expected that the CPU 4900Sstevel@tonic-gate * performing the online operation will call tsc_sync_master(). 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate void 4930Sstevel@tonic-gate tsc_sync_slave(void) 4940Sstevel@tonic-gate { 4953446Smrj ulong_t flags; 4960Sstevel@tonic-gate hrtime_t hrt; 4970Sstevel@tonic-gate 4983446Smrj if (!tsc_master_slave_sync_needed) 4993446Smrj return; 5003446Smrj 5010Sstevel@tonic-gate ASSERT(tsc_sync_go != TSC_SYNC_GO); 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate flags = clear_int_flag(); 5040Sstevel@tonic-gate 5051389Sdmick /* to test tsc_gethrtime_delta, add wrmsr(REG_TSC, 0) here */ 5061389Sdmick 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * Tell the master CPU that we're ready, and wait for the master to 5090Sstevel@tonic-gate * tell us to begin reading our TSC. 5100Sstevel@tonic-gate */ 5110Sstevel@tonic-gate tsc_ready = TSC_SYNC_GO; 5120Sstevel@tonic-gate while (tsc_sync_go != TSC_SYNC_GO) 5130Sstevel@tonic-gate continue; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate hrt = tsc_read(); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * Wait for the master CPU to be ready to read its TSC again. 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate while (tsc_ready != TSC_SYNC_AGAIN) 5210Sstevel@tonic-gate continue; 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * Tell the master CPU to read its TSC again; read ours again. 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate tsc_sync_go = TSC_SYNC_AGAIN; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate hrt += tsc_read(); 5290Sstevel@tonic-gate tsc_sync_snaps[TSC_SLAVE] = hrt; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * Tell the master that we're done, and wait to be dismissed. 5330Sstevel@tonic-gate */ 5340Sstevel@tonic-gate tsc_ready = TSC_SYNC_STOP; 5350Sstevel@tonic-gate while (tsc_sync_go != TSC_SYNC_STOP) 5360Sstevel@tonic-gate continue; 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate restore_int_flag(flags); 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate /* 5423446Smrj * Called once per second on a CPU from the cyclic subsystem's 5433446Smrj * CY_HIGH_LEVEL interrupt. (No longer just cpu0-only) 5440Sstevel@tonic-gate */ 5450Sstevel@tonic-gate void 5460Sstevel@tonic-gate tsc_tick(void) 5470Sstevel@tonic-gate { 5480Sstevel@tonic-gate hrtime_t now, delta; 5490Sstevel@tonic-gate ushort_t spl; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate /* 5520Sstevel@tonic-gate * Before we set the new variables, we set the shadow values. This 5530Sstevel@tonic-gate * allows for lock free operation in dtrace_gethrtime(). 5540Sstevel@tonic-gate */ 5550Sstevel@tonic-gate lock_set_spl((lock_t *)&shadow_hres_lock + HRES_LOCK_OFFSET, 5560Sstevel@tonic-gate ipltospl(CBE_HIGH_PIL), &spl); 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate shadow_tsc_hrtime_base = tsc_hrtime_base; 5590Sstevel@tonic-gate shadow_tsc_last = tsc_last; 5600Sstevel@tonic-gate shadow_nsec_scale = nsec_scale; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate shadow_hres_lock++; 5630Sstevel@tonic-gate splx(spl); 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate CLOCK_LOCK(&spl); 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate now = tsc_read(); 5680Sstevel@tonic-gate 5691389Sdmick if (gethrtimef == tsc_gethrtime_delta) 5701389Sdmick now += tsc_sync_tick_delta[CPU->cpu_id]; 5711389Sdmick 5720Sstevel@tonic-gate if (now < tsc_last) { 5730Sstevel@tonic-gate /* 5740Sstevel@tonic-gate * The TSC has just jumped into the past. We assume that 5750Sstevel@tonic-gate * this is due to a suspend/resume cycle, and we're going 5760Sstevel@tonic-gate * to use the _current_ value of TSC as the delta. This 5770Sstevel@tonic-gate * will keep tsc_hrtime_base correct. We're also going to 5780Sstevel@tonic-gate * assume that rate of tsc does not change after a suspend 5790Sstevel@tonic-gate * resume (i.e nsec_scale remains the same). 5800Sstevel@tonic-gate */ 5810Sstevel@tonic-gate delta = now; 5820Sstevel@tonic-gate tsc_last_jumped += tsc_last; 5830Sstevel@tonic-gate tsc_jumped = 1; 5840Sstevel@tonic-gate } else { 5850Sstevel@tonic-gate /* 5860Sstevel@tonic-gate * Determine the number of TSC ticks since the last clock 5870Sstevel@tonic-gate * tick, and add that to the hrtime base. 5880Sstevel@tonic-gate */ 5890Sstevel@tonic-gate delta = now - tsc_last; 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate TSC_CONVERT_AND_ADD(delta, tsc_hrtime_base, nsec_scale); 5930Sstevel@tonic-gate tsc_last = now; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate CLOCK_UNLOCK(spl); 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5985084Sjohnlev void 5995084Sjohnlev tsc_hrtimeinit(uint64_t cpu_freq_hz) 6000Sstevel@tonic-gate { 6015084Sjohnlev extern int gethrtime_hires; 6025084Sjohnlev longlong_t tsc; 6035084Sjohnlev ulong_t flags; 6040Sstevel@tonic-gate 6055084Sjohnlev /* 6065084Sjohnlev * cpu_freq_hz is the measured cpu frequency in hertz 6075084Sjohnlev */ 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* 6105084Sjohnlev * We can't accommodate CPUs slower than 31.25 MHz. 6110Sstevel@tonic-gate */ 6125084Sjohnlev ASSERT(cpu_freq_hz > NANOSEC / (1 << NSEC_SHIFT)); 6135084Sjohnlev nsec_scale = 6145084Sjohnlev (uint_t)(((uint64_t)NANOSEC << (32 - NSEC_SHIFT)) / cpu_freq_hz); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate flags = clear_int_flag(); 6175084Sjohnlev tsc = tsc_read(); 6185084Sjohnlev (void) tsc_gethrtime(); 6195084Sjohnlev tsc_max_delta = tsc_read() - tsc; 6200Sstevel@tonic-gate restore_int_flag(flags); 6215084Sjohnlev gethrtimef = tsc_gethrtime; 6225084Sjohnlev gethrtimeunscaledf = tsc_gethrtimeunscaled; 6235084Sjohnlev scalehrtimef = tsc_scalehrtime; 6245084Sjohnlev hrtime_tick = tsc_tick; 6255084Sjohnlev gethrtime_hires = 1; 6260Sstevel@tonic-gate } 627*5295Srandyf 628*5295Srandyf int 629*5295Srandyf get_tsc_ready() 630*5295Srandyf { 631*5295Srandyf return (tsc_ready); 632*5295Srandyf } 633*5295Srandyf 634*5295Srandyf /* 635*5295Srandyf * Adjust all the deltas by adding the passed value to the array. 636*5295Srandyf * Then use the "delt" versions of the the gethrtime functions. 637*5295Srandyf * Note that 'tdelta' _could_ be a negative number, which should 638*5295Srandyf * reduce the values in the array (used, for example, if the Solaris 639*5295Srandyf * instance was moved by a virtual manager to a machine with a higher 640*5295Srandyf * value of tsc). 641*5295Srandyf */ 642*5295Srandyf void 643*5295Srandyf tsc_adjust_delta(hrtime_t tdelta) 644*5295Srandyf { 645*5295Srandyf int i; 646*5295Srandyf hrtime_t hdelta = 0; 647*5295Srandyf 648*5295Srandyf TSC_CONVERT(tdelta, hdelta, nsec_scale); 649*5295Srandyf 650*5295Srandyf for (i = 0; i < NCPU; i++) { 651*5295Srandyf tsc_sync_delta[i] += hdelta; 652*5295Srandyf tsc_sync_tick_delta[i] += tdelta; 653*5295Srandyf } 654*5295Srandyf 655*5295Srandyf gethrtimef = tsc_gethrtime_delta; 656*5295Srandyf gethrtimeunscaledf = tsc_gethrtimeunscaled_delta; 657*5295Srandyf } 658*5295Srandyf 659*5295Srandyf /* 660*5295Srandyf * Functions to manage TSC and high-res time on suspend and resume. 661*5295Srandyf */ 662*5295Srandyf 663*5295Srandyf /* 664*5295Srandyf * declarations needed for time adjustment 665*5295Srandyf */ 666*5295Srandyf extern void rtcsync(void); 667*5295Srandyf extern tod_ops_t *tod_ops; 668*5295Srandyf /* There must be a better way than exposing nsec_scale! */ 669*5295Srandyf extern uint_t nsec_scale; 670*5295Srandyf static uint64_t tsc_saved_tsc = 0; /* 1 in 2^64 chance this'll screw up! */ 671*5295Srandyf static timestruc_t tsc_saved_ts; 672*5295Srandyf static int tsc_needs_resume = 0; /* We only want to do this once. */ 673*5295Srandyf int tsc_delta_onsuspend = 0; 674*5295Srandyf int tsc_adjust_seconds = 1; 675*5295Srandyf int tsc_suspend_count = 0; 676*5295Srandyf int tsc_resume_in_cyclic = 0; 677*5295Srandyf 678*5295Srandyf /* 679*5295Srandyf * Let timestamp.c know that we are suspending. It needs to take 680*5295Srandyf * snapshots of the current time, and do any pre-suspend work. 681*5295Srandyf */ 682*5295Srandyf void 683*5295Srandyf tsc_suspend(void) 684*5295Srandyf { 685*5295Srandyf /* 686*5295Srandyf * What we need to do here, is to get the time we suspended, so that we 687*5295Srandyf * know how much we should add to the resume. 688*5295Srandyf * This routine is called by each CPU, so we need to handle reentry. 689*5295Srandyf */ 690*5295Srandyf if (tsc_gethrtime_enable) { 691*5295Srandyf /* 692*5295Srandyf * We put the tsc_read() inside the lock as it 693*5295Srandyf * as no locking constraints, and it puts the 694*5295Srandyf * aquired value closer to the time stamp (in 695*5295Srandyf * case we delay getting the lock). 696*5295Srandyf */ 697*5295Srandyf mutex_enter(&tod_lock); 698*5295Srandyf tsc_saved_tsc = tsc_read(); 699*5295Srandyf tsc_saved_ts = TODOP_GET(tod_ops); 700*5295Srandyf mutex_exit(&tod_lock); 701*5295Srandyf /* We only want to do this once. */ 702*5295Srandyf if (tsc_needs_resume == 0) { 703*5295Srandyf if (tsc_delta_onsuspend) { 704*5295Srandyf tsc_adjust_delta(tsc_saved_tsc); 705*5295Srandyf } else { 706*5295Srandyf tsc_adjust_delta(nsec_scale); 707*5295Srandyf } 708*5295Srandyf tsc_suspend_count++; 709*5295Srandyf } 710*5295Srandyf } 711*5295Srandyf 712*5295Srandyf invalidate_cache(); 713*5295Srandyf tsc_needs_resume = 1; 714*5295Srandyf } 715*5295Srandyf 716*5295Srandyf /* 717*5295Srandyf * Restore all timestamp state based on the snapshots taken at 718*5295Srandyf * suspend time. 719*5295Srandyf */ 720*5295Srandyf void 721*5295Srandyf tsc_resume(void) 722*5295Srandyf { 723*5295Srandyf /* 724*5295Srandyf * We only need to (and want to) do this once. So let the first 725*5295Srandyf * caller handle this (we are locked by the cpu lock), as it 726*5295Srandyf * is preferential that we get the earliest sync. 727*5295Srandyf */ 728*5295Srandyf if (tsc_needs_resume) { 729*5295Srandyf /* 730*5295Srandyf * If using the TSC, adjust the delta based on how long 731*5295Srandyf * we were sleeping (or away). We also adjust for 732*5295Srandyf * migration and a grown TSC. 733*5295Srandyf */ 734*5295Srandyf if (tsc_saved_tsc != 0) { 735*5295Srandyf timestruc_t ts; 736*5295Srandyf hrtime_t now, sleep_tsc = 0; 737*5295Srandyf int sleep_sec; 738*5295Srandyf extern void tsc_tick(void); 739*5295Srandyf extern uint64_t cpu_freq_hz; 740*5295Srandyf 741*5295Srandyf /* tsc_read() MUST be before TODOP_GET() */ 742*5295Srandyf mutex_enter(&tod_lock); 743*5295Srandyf now = tsc_read(); 744*5295Srandyf ts = TODOP_GET(tod_ops); 745*5295Srandyf mutex_exit(&tod_lock); 746*5295Srandyf 747*5295Srandyf /* Compute seconds of sleep time */ 748*5295Srandyf sleep_sec = ts.tv_sec - tsc_saved_ts.tv_sec; 749*5295Srandyf 750*5295Srandyf /* 751*5295Srandyf * If the saved sec is less that or equal to 752*5295Srandyf * the current ts, then there is likely a 753*5295Srandyf * problem with the clock. Assume at least 754*5295Srandyf * one second has passed, so that time goes forward. 755*5295Srandyf */ 756*5295Srandyf if (sleep_sec <= 0) { 757*5295Srandyf sleep_sec = 1; 758*5295Srandyf } 759*5295Srandyf 760*5295Srandyf /* How many TSC's should have occured while sleeping */ 761*5295Srandyf if (tsc_adjust_seconds) 762*5295Srandyf sleep_tsc = sleep_sec * cpu_freq_hz; 763*5295Srandyf 764*5295Srandyf /* 765*5295Srandyf * We also want to subtract from the "sleep_tsc" 766*5295Srandyf * the current value of tsc_read(), so that our 767*5295Srandyf * adjustment accounts for the amount of time we 768*5295Srandyf * have been resumed _or_ an adjustment based on 769*5295Srandyf * the fact that we didn't actually power off the 770*5295Srandyf * CPU (migration is another issue, but _should_ 771*5295Srandyf * also comply with this calculation). If the CPU 772*5295Srandyf * never powered off, then: 773*5295Srandyf * 'now == sleep_tsc + saved_tsc' 774*5295Srandyf * and the delta will effectively be "0". 775*5295Srandyf */ 776*5295Srandyf sleep_tsc -= now; 777*5295Srandyf if (tsc_delta_onsuspend) { 778*5295Srandyf tsc_adjust_delta(sleep_tsc); 779*5295Srandyf } else { 780*5295Srandyf tsc_adjust_delta(tsc_saved_tsc + sleep_tsc); 781*5295Srandyf } 782*5295Srandyf tsc_saved_tsc = 0; 783*5295Srandyf 784*5295Srandyf tsc_tick(); 785*5295Srandyf } 786*5295Srandyf tsc_needs_resume = 0; 787*5295Srandyf } 788*5295Srandyf 789*5295Srandyf } 790