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

Cache object: 8b696d428399bed05065e6cdb8e6c1a6


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