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