The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/tsc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_clock.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/bus.h>
   34 #include <sys/cpu.h>
   35 #include <sys/malloc.h>
   36 #include <sys/systm.h>
   37 #include <sys/sysctl.h>
   38 #include <sys/time.h>
   39 #include <sys/timetc.h>
   40 #include <sys/kernel.h>
   41 #include <sys/power.h>
   42 #include <sys/smp.h>
   43 #include <machine/clock.h>
   44 #include <machine/md_var.h>
   45 #include <machine/specialreg.h>
   46 
   47 #include "cpufreq_if.h"
   48 
   49 uint64_t        tsc_freq;
   50 int             tsc_is_broken;
   51 int             tsc_is_invariant;
   52 u_int           tsc_present;
   53 static eventhandler_tag tsc_levels_tag, tsc_pre_tag, tsc_post_tag;
   54 
   55 SYSCTL_INT(_kern_timecounter, OID_AUTO, invariant_tsc, CTLFLAG_RDTUN,
   56     &tsc_is_invariant, 0, "Indicates whether the TSC is P-state invariant");
   57 TUNABLE_INT("kern.timecounter.invariant_tsc", &tsc_is_invariant);
   58 
   59 #ifdef SMP
   60 static int      smp_tsc;
   61 SYSCTL_INT(_kern_timecounter, OID_AUTO, smp_tsc, CTLFLAG_RDTUN, &smp_tsc, 0,
   62     "Indicates whether the TSC is safe to use in SMP mode");
   63 TUNABLE_INT("kern.timecounter.smp_tsc", &smp_tsc);
   64 #endif
   65 
   66 static void tsc_freq_changed(void *arg, const struct cf_level *level,
   67     int status);
   68 static void tsc_freq_changing(void *arg, const struct cf_level *level,
   69     int *status);
   70 static  unsigned tsc_get_timecount(struct timecounter *tc);
   71 static void tsc_levels_changed(void *arg, int unit);
   72 
   73 static struct timecounter tsc_timecounter = {
   74         tsc_get_timecount,      /* get_timecount */
   75         0,                      /* no poll_pps */
   76         ~0u,                    /* counter_mask */
   77         0,                      /* frequency */
   78         "TSC",                  /* name */
   79         800,                    /* quality (adjusted in code) */
   80 };
   81 
   82 void
   83 init_TSC(void)
   84 {
   85         u_int64_t tscval[2];
   86 
   87         if (cpu_feature & CPUID_TSC)
   88                 tsc_present = 1;
   89         else
   90                 tsc_present = 0;
   91 
   92         if (!tsc_present) 
   93                 return;
   94 
   95         if (bootverbose)
   96                 printf("Calibrating TSC clock ... ");
   97 
   98         tscval[0] = rdtsc();
   99         DELAY(1000000);
  100         tscval[1] = rdtsc();
  101 
  102         tsc_freq = tscval[1] - tscval[0];
  103         if (bootverbose)
  104                 printf("TSC clock: %ju Hz\n", (intmax_t)tsc_freq);
  105 
  106         /*
  107          * Inform CPU accounting about our boot-time clock rate.  Once the
  108          * system is finished booting, we will get the real max clock rate
  109          * via tsc_freq_max().  This also will be updated if someone loads
  110          * a cpufreq driver after boot that discovers a new max frequency.
  111          */
  112         set_cputicker(rdtsc, tsc_freq, 1);
  113 
  114         /* Register to find out about changes in CPU frequency. */
  115         tsc_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
  116             tsc_freq_changing, NULL, EVENTHANDLER_PRI_FIRST);
  117         tsc_post_tag = EVENTHANDLER_REGISTER(cpufreq_post_change,
  118             tsc_freq_changed, NULL, EVENTHANDLER_PRI_FIRST);
  119         tsc_levels_tag = EVENTHANDLER_REGISTER(cpufreq_levels_changed,
  120             tsc_levels_changed, NULL, EVENTHANDLER_PRI_ANY);
  121 }
  122 
  123 void
  124 init_TSC_tc(void)
  125 {
  126         /*
  127          * We can not use the TSC if we support APM.  Precise timekeeping
  128          * on an APM'ed machine is at best a fools pursuit, since 
  129          * any and all of the time spent in various SMM code can't 
  130          * be reliably accounted for.  Reading the RTC is your only
  131          * source of reliable time info.  The i8254 loses too, of course,
  132          * but we need to have some kind of time...
  133          * We don't know at this point whether APM is going to be used
  134          * or not, nor when it might be activated.  Play it safe.
  135          */
  136         if (power_pm_get_type() == POWER_PM_TYPE_APM) {
  137                 tsc_timecounter.tc_quality = -1000;
  138                 if (bootverbose)
  139                         printf("TSC timecounter disabled: APM enabled.\n");
  140         }
  141 
  142 #ifdef SMP
  143         /*
  144          * We can not use the TSC in SMP mode unless the TSCs on all CPUs
  145          * are somehow synchronized.  Some hardware configurations do
  146          * this, but we have no way of determining whether this is the
  147          * case, so we do not use the TSC in multi-processor systems
  148          * unless the user indicated (by setting kern.timecounter.smp_tsc
  149          * to 1) that he believes that his TSCs are synchronized.
  150          */
  151         if (mp_ncpus > 1 && !smp_tsc)
  152                 tsc_timecounter.tc_quality = -100;
  153 #endif
  154 
  155         if (tsc_present && tsc_freq != 0 && !tsc_is_broken) {
  156                 tsc_timecounter.tc_frequency = tsc_freq;
  157                 tc_init(&tsc_timecounter);
  158         }
  159 }
  160 
  161 /*
  162  * When cpufreq levels change, find out about the (new) max frequency.  We
  163  * use this to update CPU accounting in case it got a lower estimate at boot.
  164  */
  165 static void
  166 tsc_levels_changed(void *arg, int unit)
  167 {
  168         device_t cf_dev;
  169         struct cf_level *levels;
  170         int count, error;
  171         uint64_t max_freq;
  172 
  173         if (tsc_is_invariant)
  174                 return;
  175 
  176         /* Only use values from the first CPU, assuming all are equal. */
  177         if (unit != 0)
  178                 return;
  179 
  180         /* Find the appropriate cpufreq device instance. */
  181         cf_dev = devclass_get_device(devclass_find("cpufreq"), unit);
  182         if (cf_dev == NULL) {
  183                 printf("tsc_levels_changed() called but no cpufreq device?\n");
  184                 return;
  185         }
  186 
  187         /* Get settings from the device and find the max frequency. */
  188         count = 64;
  189         levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
  190         if (levels == NULL)
  191                 return;
  192         error = CPUFREQ_LEVELS(cf_dev, levels, &count);
  193         if (error == 0 && count != 0) {
  194                 max_freq = (uint64_t)levels[0].total_set.freq * 1000000;
  195                 set_cputicker(rdtsc, max_freq, 1);
  196         } else
  197                 printf("tsc_levels_changed: no max freq found\n");
  198         free(levels, M_TEMP);
  199 }
  200 
  201 /*
  202  * If the TSC timecounter is in use, veto the pending change.  It may be
  203  * possible in the future to handle a dynamically-changing timecounter rate.
  204  */
  205 static void
  206 tsc_freq_changing(void *arg, const struct cf_level *level, int *status)
  207 {
  208 
  209         if (*status != 0 || timecounter != &tsc_timecounter ||
  210             tsc_is_invariant)
  211                 return;
  212 
  213         printf("timecounter TSC must not be in use when "
  214             "changing frequencies; change denied\n");
  215         *status = EBUSY;
  216 }
  217 
  218 /* Update TSC freq with the value indicated by the caller. */
  219 static void
  220 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
  221 {
  222         /*
  223          * If there was an error during the transition or
  224          * TSC is P-state invariant, don't do anything.
  225          */
  226         if (status != 0 || tsc_is_invariant)
  227                 return;
  228 
  229         /* Total setting for this level gives the new frequency in MHz. */
  230         tsc_freq = (uint64_t)level->total_set.freq * 1000000;
  231         tsc_timecounter.tc_frequency = tsc_freq;
  232 }
  233 
  234 static int
  235 sysctl_machdep_tsc_freq(SYSCTL_HANDLER_ARGS)
  236 {
  237         int error;
  238         uint64_t freq;
  239 
  240         if (tsc_timecounter.tc_frequency == 0)
  241                 return (EOPNOTSUPP);
  242         freq = tsc_freq;
  243         error = sysctl_handle_quad(oidp, &freq, 0, req);
  244         if (error == 0 && req->newptr != NULL) {
  245                 tsc_freq = freq;
  246                 tsc_timecounter.tc_frequency = tsc_freq;
  247         }
  248         return (error);
  249 }
  250 
  251 SYSCTL_PROC(_machdep, OID_AUTO, tsc_freq, CTLTYPE_QUAD | CTLFLAG_RW,
  252     0, sizeof(u_int), sysctl_machdep_tsc_freq, "QU", "");
  253 
  254 static unsigned
  255 tsc_get_timecount(struct timecounter *tc)
  256 {
  257         return (rdtsc());
  258 }

Cache object: 5389138fc06975b98c9d384fdf81eabf


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.