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/dev/acpica/acpi_timer.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2000, 2001 Michael Smith
    3  * Copyright (c) 2000 BSDi
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/6.4/sys/dev/acpica/acpi_timer.c 173482 2007-11-08 21:22:20Z njl $");
   30 
   31 #include "opt_acpi.h"
   32 #include <sys/param.h>
   33 #include <sys/bus.h>
   34 #include <sys/kernel.h>
   35 #include <sys/module.h>
   36 #include <sys/sysctl.h>
   37 #include <sys/timetc.h>
   38 
   39 #include <machine/bus.h>
   40 #include <machine/resource.h>
   41 #include <sys/rman.h>
   42 
   43 #include <contrib/dev/acpica/acpi.h>
   44 #include <dev/acpica/acpivar.h>
   45 #include <dev/pci/pcivar.h>
   46 
   47 /*
   48  * A timecounter based on the free-running ACPI timer.
   49  *
   50  * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
   51  */
   52 
   53 /* Hooks for the ACPI CA debugging infrastructure */
   54 #define _COMPONENT      ACPI_TIMER
   55 ACPI_MODULE_NAME("TIMER")
   56 
   57 static device_t                 acpi_timer_dev;
   58 static struct resource          *acpi_timer_reg;
   59 static bus_space_handle_t       acpi_timer_bsh;
   60 static bus_space_tag_t          acpi_timer_bst;
   61 
   62 static u_int    acpi_timer_frequency = 14318182 / 4;
   63 
   64 static void     acpi_timer_identify(driver_t *driver, device_t parent);
   65 static int      acpi_timer_probe(device_t dev);
   66 static int      acpi_timer_attach(device_t dev);
   67 static u_int    acpi_timer_get_timecount(struct timecounter *tc);
   68 static u_int    acpi_timer_get_timecount_safe(struct timecounter *tc);
   69 static int      acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
   70 static void     acpi_timer_boot_test(void);
   71 
   72 static u_int    acpi_timer_read(void);
   73 static int      acpi_timer_test(void);
   74 
   75 static device_method_t acpi_timer_methods[] = {
   76     DEVMETHOD(device_identify,  acpi_timer_identify),
   77     DEVMETHOD(device_probe,     acpi_timer_probe),
   78     DEVMETHOD(device_attach,    acpi_timer_attach),
   79 
   80     {0, 0}
   81 };
   82 
   83 static driver_t acpi_timer_driver = {
   84     "acpi_timer",
   85     acpi_timer_methods,
   86     0,
   87 };
   88 
   89 static devclass_t acpi_timer_devclass;
   90 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
   91 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
   92 
   93 static struct timecounter acpi_timer_timecounter = {
   94         acpi_timer_get_timecount_safe,  /* get_timecount function */
   95         0,                              /* no poll_pps */
   96         0,                              /* no default counter_mask */
   97         0,                              /* no default frequency */
   98         "ACPI",                         /* name */
   99         -1                              /* quality (chosen later) */
  100 };
  101 
  102 static u_int
  103 acpi_timer_read()
  104 {
  105     return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
  106 }
  107 
  108 /*
  109  * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
  110  * we will be using.
  111  */
  112 static void
  113 acpi_timer_identify(driver_t *driver, device_t parent)
  114 {
  115     device_t dev;
  116     u_long rlen, rstart;
  117     int rid, rtype;
  118 
  119     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  120 
  121     if (acpi_disabled("timer") || (acpi_quirks & ACPI_Q_TIMER) ||
  122         AcpiGbl_FADT == NULL || acpi_timer_dev)
  123         return_VOID;
  124 
  125     if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
  126         device_printf(parent, "could not add acpi_timer0\n");
  127         return_VOID;
  128     }
  129     acpi_timer_dev = dev;
  130 
  131     rid = 0;
  132     rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
  133         SYS_RES_IOPORT : SYS_RES_MEMORY;
  134     rlen = AcpiGbl_FADT->PmTmLen;
  135     rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
  136     if (bus_set_resource(dev, rtype, rid, rstart, rlen))
  137         device_printf(dev, "couldn't set resource (%s 0x%lx+0x%lx)\n",
  138             (rtype == SYS_RES_IOPORT) ? "port" : "mem", rstart, rlen);
  139     return_VOID;
  140 }
  141 
  142 static int
  143 acpi_timer_probe(device_t dev)
  144 {
  145     char desc[40];
  146     int i, j, rid, rtype;
  147 
  148     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  149 
  150     if (dev != acpi_timer_dev)
  151         return (ENXIO);
  152 
  153     rid = 0;
  154     rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
  155         SYS_RES_IOPORT : SYS_RES_MEMORY;
  156     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
  157     if (acpi_timer_reg == NULL) {
  158         device_printf(dev, "couldn't allocate resource (%s 0x%lx)\n",
  159             (rtype == SYS_RES_IOPORT) ? "port" : "mem",
  160             (u_long)AcpiGbl_FADT->XPmTmrBlk.Address);
  161         return (ENXIO);
  162     }
  163     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
  164     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
  165     if (AcpiGbl_FADT->TmrValExt != 0)
  166         acpi_timer_timecounter.tc_counter_mask = 0xffffffff;
  167     else
  168         acpi_timer_timecounter.tc_counter_mask = 0x00ffffff;
  169     acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
  170     if (testenv("debug.acpi.timer_test"))
  171         acpi_timer_boot_test();
  172 
  173     /*
  174      * If all tests of the counter succeed, use the ACPI-fast method.  If
  175      * at least one failed, default to using the safe routine, which reads
  176      * the timer multiple times to get a consistent value before returning.
  177      */
  178     j = 0;
  179     if (bootverbose)
  180         printf("ACPI timer:");
  181     for (i = 0; i < 10; i++)
  182         j += acpi_timer_test();
  183     if (bootverbose)
  184         printf(" -> %d\n", j);
  185     if (j == 10) {
  186         acpi_timer_timecounter.tc_name = "ACPI-fast";
  187         acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount;
  188         acpi_timer_timecounter.tc_quality = 1000;
  189     } else {
  190         acpi_timer_timecounter.tc_name = "ACPI-safe";
  191         acpi_timer_timecounter.tc_get_timecount = acpi_timer_get_timecount_safe;
  192         acpi_timer_timecounter.tc_quality = 850;
  193     }
  194     tc_init(&acpi_timer_timecounter);
  195 
  196     sprintf(desc, "%d-bit timer at 3.579545MHz",
  197         AcpiGbl_FADT->TmrValExt ? 32 : 24);
  198     device_set_desc_copy(dev, desc);
  199 
  200     /* Release the resource, we'll allocate it again during attach. */
  201     bus_release_resource(dev, rtype, rid, acpi_timer_reg);
  202     return (0);
  203 }
  204 
  205 static int
  206 acpi_timer_attach(device_t dev)
  207 {
  208     int rid, rtype;
  209 
  210     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  211 
  212     rid = 0;
  213     rtype = AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId ?
  214         SYS_RES_IOPORT : SYS_RES_MEMORY;
  215     acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
  216     if (acpi_timer_reg == NULL)
  217         return (ENXIO);
  218     acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
  219     acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
  220     return (0);
  221 }
  222 
  223 /*
  224  * Fetch current time value from reliable hardware.
  225  */
  226 static u_int
  227 acpi_timer_get_timecount(struct timecounter *tc)
  228 {
  229     return (acpi_timer_read());
  230 }
  231 
  232 /*
  233  * Fetch current time value from hardware that may not correctly
  234  * latch the counter.  We need to read until we have three monotonic
  235  * samples and then use the middle one, otherwise we are not protected
  236  * against the fact that the bits can be wrong in two directions.  If
  237  * we only cared about monosity, two reads would be enough.
  238  */
  239 static u_int
  240 acpi_timer_get_timecount_safe(struct timecounter *tc)
  241 {
  242     u_int u1, u2, u3;
  243 
  244     u2 = acpi_timer_read();
  245     u3 = acpi_timer_read();
  246     do {
  247         u1 = u2;
  248         u2 = u3;
  249         u3 = acpi_timer_read();
  250     } while (u1 > u2 || u2 > u3);
  251 
  252     return (u2);
  253 }
  254 
  255 /*
  256  * Timecounter freqency adjustment interface.
  257  */ 
  258 static int
  259 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
  260 {
  261     int error;
  262     u_int freq;
  263  
  264     if (acpi_timer_timecounter.tc_frequency == 0)
  265         return (EOPNOTSUPP);
  266     freq = acpi_timer_frequency;
  267     error = sysctl_handle_int(oidp, &freq, 0, req);
  268     if (error == 0 && req->newptr != NULL) {
  269         acpi_timer_frequency = freq;
  270         acpi_timer_timecounter.tc_frequency = acpi_timer_frequency;
  271     }
  272 
  273     return (error);
  274 }
  275  
  276 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
  277             0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
  278 
  279 /*
  280  * Some ACPI timers are known or believed to suffer from implementation
  281  * problems which can lead to erroneous values being read.  This function
  282  * tests for consistent results from the timer and returns 1 if it believes
  283  * the timer is consistent, otherwise it returns 0.
  284  *
  285  * It appears the cause is that the counter is not latched to the PCI bus
  286  * clock when read:
  287  *
  288  * ] 20. ACPI Timer Errata
  289  * ]
  290  * ]   Problem: The power management timer may return improper result when
  291  * ]   read. Although the timer value settles properly after incrementing,
  292  * ]   while incrementing there is a 3nS window every 69.8nS where the
  293  * ]   timer value is indeterminate (a 4.2% chance that the data will be
  294  * ]   incorrect when read). As a result, the ACPI free running count up
  295  * ]   timer specification is violated due to erroneous reads.  Implication:
  296  * ]   System hangs due to the "inaccuracy" of the timer when used by
  297  * ]   software for time critical events and delays.
  298  * ]
  299  * ] Workaround: Read the register twice and compare.
  300  * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
  301  * ] in the PIIX4M.
  302  */
  303 #define N 2000
  304 static int
  305 acpi_timer_test()
  306 {
  307     uint32_t    last, this;
  308     int         min, max, n, delta;
  309     register_t  s;
  310 
  311     min = 10000000;
  312     max = 0;
  313 
  314     /* Test the timer with interrupts disabled to get accurate results. */
  315     s = intr_disable();
  316     last = acpi_timer_read();
  317     for (n = 0; n < N; n++) {
  318         this = acpi_timer_read();
  319         delta = acpi_TimerDelta(this, last);
  320         if (delta > max)
  321             max = delta;
  322         else if (delta < min)
  323             min = delta;
  324         last = this;
  325     }
  326     intr_restore(s);
  327 
  328     if (max - min > 2)
  329         n = 0;
  330     else if (min < 0 || max == 0)
  331         n = 0;
  332     else
  333         n = 1;
  334     if (bootverbose)
  335         printf(" %d/%d", n, max-min);
  336 
  337     return (n);
  338 }
  339 #undef N
  340 
  341 /*
  342  * Test harness for verifying ACPI timer behaviour.
  343  * Boot with debug.acpi.timer_test set to invoke this.
  344  */
  345 static void
  346 acpi_timer_boot_test(void)
  347 {
  348     uint32_t u1, u2, u3;
  349 
  350     u1 = acpi_timer_read();
  351     u2 = acpi_timer_read();
  352     u3 = acpi_timer_read();
  353 
  354     device_printf(acpi_timer_dev, "timer test in progress, reboot to quit.\n");
  355     for (;;) {
  356         /*
  357          * The failure case is where u3 > u1, but u2 does not fall between
  358          * the two, ie. it contains garbage.
  359          */
  360         if (u3 > u1) {
  361             if (u2 < u1 || u2 > u3)
  362                 device_printf(acpi_timer_dev,
  363                               "timer is not monotonic: 0x%08x,0x%08x,0x%08x\n",
  364                               u1, u2, u3);
  365         }
  366         u1 = u2;
  367         u2 = u3;
  368         u3 = acpi_timer_read();
  369     }
  370 }

Cache object: 26833e1c26c2af11da1424ff6eb9976e


[ 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.