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_hpet.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) 2005 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: releng/6.4/sys/dev/acpica/acpi_hpet.c 175604 2008-01-23 18:03:15Z jhb $");
   29 
   30 #include "opt_acpi.h"
   31 #include <sys/param.h>
   32 #include <sys/bus.h>
   33 #include <sys/kernel.h>
   34 #include <sys/module.h>
   35 #include <sys/rman.h>
   36 #include <sys/time.h>
   37 #include <sys/timetc.h>
   38 
   39 #include <contrib/dev/acpica/acpi.h>
   40 #include <dev/acpica/acpivar.h>
   41 #include <dev/acpica/acpi_hpet.h>
   42 
   43 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
   44 
   45 static devclass_t acpi_hpet_devclass;
   46 
   47 /* ACPI CA debugging */
   48 #define _COMPONENT      ACPI_TIMER
   49 ACPI_MODULE_NAME("HPET")
   50 
   51 struct acpi_hpet_softc {
   52         device_t                dev;
   53         struct resource         *mem_res;
   54         ACPI_HANDLE             handle;
   55 };
   56 
   57 static u_int hpet_get_timecount(struct timecounter *tc);
   58 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
   59 
   60 static char *hpet_ids[] = { "PNP0103", NULL };
   61 
   62 #define DEV_HPET(x)     (acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
   63 
   64 struct timecounter hpet_timecounter = {
   65         .tc_get_timecount =     hpet_get_timecount,
   66         .tc_counter_mask =      ~0u,
   67         .tc_name =              "HPET",
   68         .tc_quality =           900,
   69 };
   70 
   71 static u_int
   72 hpet_get_timecount(struct timecounter *tc)
   73 {
   74         struct acpi_hpet_softc *sc;
   75 
   76         sc = tc->tc_priv;
   77         return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
   78 }
   79 
   80 static void
   81 hpet_enable(struct acpi_hpet_softc *sc)
   82 {
   83         uint32_t val;
   84 
   85         val = bus_read_4(sc->mem_res, HPET_CONFIG);
   86         bus_write_4(sc->mem_res, HPET_CONFIG, val | HPET_CNF_ENABLE);
   87 }
   88 
   89 static void
   90 hpet_disable(struct acpi_hpet_softc *sc)
   91 {
   92         uint32_t val;
   93 
   94         val = bus_read_4(sc->mem_res, HPET_CONFIG);
   95         bus_write_4(sc->mem_res, HPET_CONFIG, val & ~HPET_CNF_ENABLE);
   96 }
   97 
   98 /* Temporary define for 6.x */
   99 #define ACPI_SIG_HPET "HPET"
  100 
  101 /* Discover the HPET via the ACPI table of the same name. */
  102 static void 
  103 acpi_hpet_identify(driver_t *driver, device_t parent)
  104 {
  105         HPET_TABLE *hpet;
  106         ACPI_TABLE_HEADER *hdr;
  107         ACPI_STATUS     status;
  108         device_t        child;
  109 
  110         /* Only one HPET device can be added. */
  111         if (devclass_get_device(acpi_hpet_devclass, 0))
  112                 return;
  113 
  114         /* Currently, ID and minimum clock tick info is unused. */
  115 
  116         status = AcpiGetFirmwareTable(ACPI_SIG_HPET, 1, ACPI_LOGICAL_ADDRESSING,
  117                 &hdr);
  118         if (ACPI_FAILURE(status))
  119                 return;
  120 
  121         /*
  122          * The unit number could be derived from hdr->Sequence but we only
  123          * support one HPET device.
  124          */
  125         hpet = (HPET_TABLE *)hdr;
  126         if (hpet->HpetNumber != 0)
  127                 printf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
  128                     hpet->HpetNumber);
  129         child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "acpi_hpet", 0);
  130         if (child == NULL) {
  131                 printf("%s: can't add child\n", __func__);
  132                 return;
  133         }
  134 
  135         /* Record a magic value so we can detect this device later. */
  136         acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
  137         bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->BaseAddress.Address,
  138             HPET_MEM_WIDTH);
  139 }
  140 
  141 static int
  142 acpi_hpet_probe(device_t dev)
  143 {
  144         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
  145 
  146         if (acpi_disabled("hpet"))
  147                 return (ENXIO);
  148         if (!DEV_HPET(dev) &&
  149             (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
  150             device_get_unit(dev) != 0))
  151                 return (ENXIO);
  152 
  153         device_set_desc(dev, "High Precision Event Timer");
  154         return (0);
  155 }
  156 
  157 static int
  158 acpi_hpet_attach(device_t dev)
  159 {
  160         struct acpi_hpet_softc *sc;
  161         int rid;
  162         uint32_t val, val2;
  163         uintmax_t freq;
  164 
  165         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
  166 
  167         sc = device_get_softc(dev);
  168         sc->dev = dev;
  169         sc->handle = acpi_get_handle(dev);
  170 
  171         rid = 0;
  172         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
  173             RF_ACTIVE);
  174         if (sc->mem_res == NULL)
  175                 return (ENOMEM);
  176 
  177         /* Validate that we can access the whole region. */
  178         if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
  179                 device_printf(dev, "memory region width %ld too small\n",
  180                     rman_get_size(sc->mem_res));
  181                 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
  182                 return (ENXIO);
  183         }
  184 
  185         /* Be sure timer is enabled. */
  186         hpet_enable(sc);
  187 
  188         /* Read basic statistics about the timer. */
  189         val = bus_read_4(sc->mem_res, HPET_PERIOD);
  190         if (val == 0) {
  191                 device_printf(dev, "invalid period\n");
  192                 hpet_disable(sc);
  193                 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
  194                 return (ENXIO);
  195         }
  196 
  197         freq = (1000000000000000LL + val / 2) / val;
  198         if (bootverbose) {
  199                 val = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
  200                 device_printf(dev,
  201                     "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
  202                     val >> 16, val & HPET_CAP_REV_ID,
  203                     (val & HPET_CAP_NUM_TIM) >> 8, freq,
  204                     (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
  205                     (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
  206         }
  207 
  208         if (testenv("debug.acpi.hpet_test"))
  209                 acpi_hpet_test(sc);
  210 
  211         /*
  212          * Don't attach if the timer never increments.  Since the spec
  213          * requires it to be at least 10 MHz, it has to change in 1 us.
  214          */
  215         val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
  216         DELAY(1);
  217         val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
  218         if (val == val2) {
  219                 device_printf(dev, "HPET never increments, disabling\n");
  220                 hpet_disable(sc);
  221                 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
  222                 return (ENXIO);
  223         }
  224 
  225         hpet_timecounter.tc_frequency = freq;
  226         hpet_timecounter.tc_priv = sc;
  227         tc_init(&hpet_timecounter);
  228 
  229         return (0);
  230 }
  231 
  232 static int
  233 acpi_hpet_detach(device_t dev)
  234 {
  235         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
  236 
  237         /* XXX Without a tc_remove() function, we can't detach. */
  238         return (EBUSY);
  239 }
  240 
  241 static int
  242 acpi_hpet_suspend(device_t dev)
  243 {
  244         struct acpi_hpet_softc *sc;
  245 
  246         /*
  247          * Disable the timer during suspend.  The timer will not lose
  248          * its state in S1 or S2, but we are required to disable
  249          * it.
  250          */
  251         sc = device_get_softc(dev);
  252         hpet_disable(sc);
  253 
  254         return (0);
  255 }
  256 
  257 static int
  258 acpi_hpet_resume(device_t dev)
  259 {
  260         struct acpi_hpet_softc *sc;
  261 
  262         /* Re-enable the timer after a resume to keep the clock advancing. */
  263         sc = device_get_softc(dev);
  264         hpet_enable(sc);
  265 
  266         return (0);
  267 }
  268 
  269 /* Print some basic latency/rate information to assist in debugging. */
  270 static void
  271 acpi_hpet_test(struct acpi_hpet_softc *sc)
  272 {
  273         int i;
  274         uint32_t u1, u2;
  275         struct bintime b0, b1, b2;
  276         struct timespec ts;
  277 
  278         binuptime(&b0);
  279         binuptime(&b0);
  280         binuptime(&b1);
  281         u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
  282         for (i = 1; i < 1000; i++)
  283                 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
  284         binuptime(&b2);
  285         u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
  286 
  287         bintime_sub(&b2, &b1);
  288         bintime_sub(&b1, &b0);
  289         bintime_sub(&b2, &b1);
  290         bintime2timespec(&b2, &ts);
  291 
  292         device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
  293             (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
  294 
  295         device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
  296 }
  297 
  298 static device_method_t acpi_hpet_methods[] = {
  299         /* Device interface */
  300         DEVMETHOD(device_identify, acpi_hpet_identify),
  301         DEVMETHOD(device_probe, acpi_hpet_probe),
  302         DEVMETHOD(device_attach, acpi_hpet_attach),
  303         DEVMETHOD(device_detach, acpi_hpet_detach),
  304         DEVMETHOD(device_suspend, acpi_hpet_suspend),
  305         DEVMETHOD(device_resume, acpi_hpet_resume),
  306 
  307         {0, 0}
  308 };
  309 
  310 static driver_t acpi_hpet_driver = {
  311         "acpi_hpet",
  312         acpi_hpet_methods,
  313         sizeof(struct acpi_hpet_softc),
  314 };
  315 
  316 
  317 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
  318 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);

Cache object: 3efe29829ff44d9d9f04fa49e72b27d0


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