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/mips/mips/tick.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) 2006-2007 Bruce M. Simpson.
    3  * Copyright (c) 2003-2004 Juli Mallett.
    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 /*
   29  * Simple driver for the 32-bit interval counter built in to all
   30  * MIPS32 CPUs.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/9.2/sys/mips/mips/tick.c 217616 2011-01-19 23:00:25Z mdf $");
   35 
   36 #include "opt_cputype.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/sysctl.h>
   41 #include <sys/bus.h>
   42 #include <sys/kernel.h>
   43 #include <sys/module.h>
   44 #include <sys/rman.h>
   45 #include <sys/power.h>
   46 #include <sys/smp.h>
   47 #include <sys/time.h>
   48 #include <sys/timeet.h>
   49 #include <sys/timetc.h>
   50 
   51 #include <machine/hwfunc.h>
   52 #include <machine/clock.h>
   53 #include <machine/locore.h>
   54 #include <machine/md_var.h>
   55 
   56 uint64_t counter_freq;
   57 
   58 struct timecounter *platform_timecounter;
   59 
   60 static DPCPU_DEFINE(uint32_t, cycles_per_tick);
   61 static uint32_t cycles_per_usec;
   62 
   63 static DPCPU_DEFINE(volatile uint32_t, counter_upper);
   64 static DPCPU_DEFINE(volatile uint32_t, counter_lower_last);
   65 static DPCPU_DEFINE(uint32_t, compare_ticks);
   66 static DPCPU_DEFINE(uint32_t, lost_ticks);
   67 
   68 struct clock_softc {
   69         int intr_rid;
   70         struct resource *intr_res;
   71         void *intr_handler;
   72         struct timecounter tc;
   73         struct eventtimer et;
   74 };
   75 static struct clock_softc *softc;
   76 
   77 /*
   78  * Device methods
   79  */
   80 static int clock_probe(device_t);
   81 static void clock_identify(driver_t *, device_t);
   82 static int clock_attach(device_t);
   83 static unsigned counter_get_timecount(struct timecounter *tc);
   84 
   85 void 
   86 mips_timer_early_init(uint64_t clock_hz)
   87 {
   88         /* Initialize clock early so that we can use DELAY sooner */
   89         counter_freq = clock_hz;
   90         cycles_per_usec = (clock_hz / (1000 * 1000));
   91 }
   92 
   93 void
   94 platform_initclocks(void)
   95 {
   96 
   97         if (platform_timecounter != NULL)
   98                 tc_init(platform_timecounter);
   99 }
  100 
  101 static uint64_t
  102 tick_ticker(void)
  103 {
  104         uint64_t ret;
  105         uint32_t ticktock;
  106         uint32_t t_lower_last, t_upper;
  107 
  108         /*
  109          * Disable preemption because we are working with cpu specific data.
  110          */
  111         critical_enter();
  112 
  113         /*
  114          * Note that even though preemption is disabled, interrupts are
  115          * still enabled. In particular there is a race with clock_intr()
  116          * reading the values of 'counter_upper' and 'counter_lower_last'.
  117          *
  118          * XXX this depends on clock_intr() being executed periodically
  119          * so that 'counter_upper' and 'counter_lower_last' are not stale.
  120          */
  121         do {
  122                 t_upper = DPCPU_GET(counter_upper);
  123                 t_lower_last = DPCPU_GET(counter_lower_last);
  124         } while (t_upper != DPCPU_GET(counter_upper));
  125 
  126         ticktock = mips_rd_count();
  127 
  128         critical_exit();
  129 
  130         /* COUNT register wrapped around */
  131         if (ticktock < t_lower_last)
  132                 t_upper++;
  133 
  134         ret = ((uint64_t)t_upper << 32) | ticktock;
  135         return (ret);
  136 }
  137 
  138 void
  139 mips_timer_init_params(uint64_t platform_counter_freq, int double_count)
  140 {
  141 
  142         /*
  143          * XXX: Do not use printf here: uart code 8250 may use DELAY so this
  144          * function should  be called before cninit.
  145          */
  146         counter_freq = platform_counter_freq;
  147         /*
  148          * XXX: Some MIPS32 cores update the Count register only every two
  149          * pipeline cycles.
  150          * We know this because of status registers in CP0, make it automatic.
  151          */
  152         if (double_count != 0)
  153                 counter_freq /= 2;
  154 
  155         cycles_per_usec = counter_freq / (1 * 1000 * 1000);
  156         set_cputicker(tick_ticker, counter_freq, 1);
  157 }
  158 
  159 static int
  160 sysctl_machdep_counter_freq(SYSCTL_HANDLER_ARGS)
  161 {
  162         int error;
  163         uint64_t freq;
  164 
  165         if (softc == NULL)
  166                 return (EOPNOTSUPP);
  167         freq = counter_freq;
  168         error = sysctl_handle_64(oidp, &freq, sizeof(freq), req);
  169         if (error == 0 && req->newptr != NULL) {
  170                 counter_freq = freq;
  171                 softc->et.et_frequency = counter_freq;
  172                 softc->tc.tc_frequency = counter_freq;
  173         }
  174         return (error);
  175 }
  176 
  177 SYSCTL_PROC(_machdep, OID_AUTO, counter_freq, CTLTYPE_U64 | CTLFLAG_RW,
  178     NULL, 0, sysctl_machdep_counter_freq, "QU",
  179     "Timecounter frequency in Hz");
  180 
  181 static unsigned
  182 counter_get_timecount(struct timecounter *tc)
  183 {
  184 
  185         return (mips_rd_count());
  186 }
  187 
  188 /*
  189  * Wait for about n microseconds (at least!).
  190  */
  191 void
  192 DELAY(int n)
  193 {
  194         uint32_t cur, last, delta, usecs;
  195 
  196         /*
  197          * This works by polling the timer and counting the number of
  198          * microseconds that go by.
  199          */
  200         last = mips_rd_count();
  201         delta = usecs = 0;
  202 
  203         while (n > usecs) {
  204                 cur = mips_rd_count();
  205 
  206                 /* Check to see if the timer has wrapped around. */
  207                 if (cur < last)
  208                         delta += cur + (0xffffffff - last) + 1;
  209                 else
  210                         delta += cur - last;
  211 
  212                 last = cur;
  213 
  214                 if (delta >= cycles_per_usec) {
  215                         usecs += delta / cycles_per_usec;
  216                         delta %= cycles_per_usec;
  217                 }
  218         }
  219 }
  220 
  221 static int
  222 clock_start(struct eventtimer *et,
  223     struct bintime *first, struct bintime *period)
  224 {
  225         uint32_t fdiv, div, next;
  226 
  227         if (period != NULL) {
  228                 div = (et->et_frequency * (period->frac >> 32)) >> 32;
  229                 if (period->sec != 0)
  230                         div += et->et_frequency * period->sec;
  231         } else
  232                 div = 0;
  233         if (first != NULL) {
  234                 fdiv = (et->et_frequency * (first->frac >> 32)) >> 32;
  235                 if (first->sec != 0)
  236                         fdiv += et->et_frequency * first->sec;
  237         } else 
  238                 fdiv = div;
  239         DPCPU_SET(cycles_per_tick, div);
  240         next = mips_rd_count() + fdiv;
  241         DPCPU_SET(compare_ticks, next);
  242         mips_wr_compare(next);
  243         return (0);
  244 }
  245 
  246 static int
  247 clock_stop(struct eventtimer *et)
  248 {
  249 
  250         DPCPU_SET(cycles_per_tick, 0);
  251         mips_wr_compare(0xffffffff);
  252         return (0);
  253 }
  254 
  255 /*
  256  * Device section of file below
  257  */
  258 static int
  259 clock_intr(void *arg)
  260 {
  261         struct clock_softc *sc = (struct clock_softc *)arg;
  262         uint32_t cycles_per_tick;
  263         uint32_t count, compare_last, compare_next, lost_ticks;
  264 
  265         cycles_per_tick = DPCPU_GET(cycles_per_tick);
  266         /*
  267          * Set next clock edge.
  268          */
  269         count = mips_rd_count();
  270         compare_last = DPCPU_GET(compare_ticks);
  271         if (cycles_per_tick > 0) {
  272                 compare_next = count + cycles_per_tick;
  273                 DPCPU_SET(compare_ticks, compare_next);
  274                 mips_wr_compare(compare_next);
  275         } else  /* In one-shot mode timer should be stopped after the event. */
  276                 mips_wr_compare(0xffffffff);
  277 
  278         /* COUNT register wrapped around */
  279         if (count < DPCPU_GET(counter_lower_last)) {
  280                 DPCPU_SET(counter_upper, DPCPU_GET(counter_upper) + 1);
  281         }
  282         DPCPU_SET(counter_lower_last, count);
  283 
  284         if (cycles_per_tick > 0) {
  285 
  286                 /*
  287                  * Account for the "lost time" between when the timer interrupt
  288                  * fired and when 'clock_intr' actually started executing.
  289                  */
  290                 lost_ticks = DPCPU_GET(lost_ticks);
  291                 lost_ticks += count - compare_last;
  292         
  293                 /*
  294                  * If the COUNT and COMPARE registers are no longer in sync
  295                  * then make up some reasonable value for the 'lost_ticks'.
  296                  *
  297                  * This could happen, for e.g., after we resume normal
  298                  * operations after exiting the debugger.
  299                  */
  300                 if (lost_ticks > 2 * cycles_per_tick)
  301                         lost_ticks = cycles_per_tick;
  302 
  303                 while (lost_ticks >= cycles_per_tick) {
  304                         if (sc->et.et_active)
  305                                 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
  306                         lost_ticks -= cycles_per_tick;
  307                 }
  308                 DPCPU_SET(lost_ticks, lost_ticks);
  309         }
  310         if (sc->et.et_active)
  311                 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
  312         return (FILTER_HANDLED);
  313 }
  314 
  315 static int
  316 clock_probe(device_t dev)
  317 {
  318 
  319         if (device_get_unit(dev) != 0)
  320                 panic("can't attach more clocks");
  321 
  322         device_set_desc(dev, "Generic MIPS32 ticker");
  323         return (0);
  324 }
  325 
  326 static void
  327 clock_identify(driver_t * drv, device_t parent)
  328 {
  329 
  330         BUS_ADD_CHILD(parent, 0, "clock", 0);
  331 }
  332 
  333 static int
  334 clock_attach(device_t dev)
  335 {
  336         struct clock_softc *sc;
  337         int error;
  338 
  339         softc = sc = device_get_softc(dev);
  340         sc->intr_rid = 0;
  341         sc->intr_res = bus_alloc_resource(dev,
  342             SYS_RES_IRQ, &sc->intr_rid, 5, 5, 1, RF_ACTIVE);
  343         if (sc->intr_res == NULL) {
  344                 device_printf(dev, "failed to allocate irq\n");
  345                 return (ENXIO);
  346         }
  347         error = bus_setup_intr(dev, sc->intr_res, INTR_TYPE_CLK,
  348             clock_intr, NULL, sc, &sc->intr_handler);
  349         if (error != 0) {
  350                 device_printf(dev, "bus_setup_intr returned %d\n", error);
  351                 return (error);
  352         }
  353 
  354         sc->tc.tc_get_timecount = counter_get_timecount;
  355         sc->tc.tc_counter_mask = 0xffffffff;
  356         sc->tc.tc_frequency = counter_freq;
  357         sc->tc.tc_name = "MIPS32";
  358         sc->tc.tc_quality = 800;
  359         sc->tc.tc_priv = sc;
  360         tc_init(&sc->tc);
  361         sc->et.et_name = "MIPS32";
  362         sc->et.et_flags = ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT |
  363             ET_FLAGS_PERCPU;
  364         sc->et.et_quality = 800;
  365         sc->et.et_frequency = counter_freq;
  366         sc->et.et_min_period.sec = 0;
  367         sc->et.et_min_period.frac = 0x00004000LLU << 32; /* To be safe. */
  368         sc->et.et_max_period.sec = 0xfffffffeU / sc->et.et_frequency;
  369         sc->et.et_max_period.frac =
  370             ((0xfffffffeLLU << 32) / sc->et.et_frequency) << 32;
  371         sc->et.et_start = clock_start;
  372         sc->et.et_stop = clock_stop;
  373         sc->et.et_priv = sc;
  374         et_register(&sc->et);
  375         return (0);
  376 }
  377 
  378 static device_method_t clock_methods[] = {
  379         /* Device interface */
  380         DEVMETHOD(device_probe, clock_probe),
  381         DEVMETHOD(device_identify, clock_identify),
  382         DEVMETHOD(device_attach, clock_attach),
  383         DEVMETHOD(device_detach, bus_generic_detach),
  384         DEVMETHOD(device_shutdown, bus_generic_shutdown),
  385 
  386         {0, 0}
  387 };
  388 
  389 static driver_t clock_driver = {
  390         "clock",
  391         clock_methods,
  392         sizeof(struct clock_softc),
  393 };
  394 
  395 static devclass_t clock_devclass;
  396 
  397 DRIVER_MODULE(clock, nexus, clock_driver, clock_devclass, 0, 0);

Cache object: 76b382ecb26735715c3fa73c9450eb92


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