Home | History | Annotate | Line # | Download | only in dev
      1 /* $NetBSD: clock.c,v 1.27 2020/05/29 12:30:41 rin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_hz.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.27 2020/05/29 12:30:41 rin Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/proc.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/lwp.h>
     39 #include <sys/cpu.h>
     40 #include <sys/malloc.h>
     41 #include <sys/timetc.h>
     42 #include <sys/time.h>
     43 
     44 #include <machine/pcb.h>
     45 #include <machine/mainbus.h>
     46 #include <machine/thunk.h>
     47 
     48 #include <dev/clock_subr.h>
     49 
     50 static int	clock_match(device_t, cfdata_t, void *);
     51 static void	clock_attach(device_t, device_t, void *);
     52 
     53 static unsigned int clock_getcounter(struct timecounter *);
     54 
     55 static int	clock_todr_gettime(struct todr_chip_handle *, struct timeval *);
     56 
     57 extern void setup_clock_intr(void);
     58 void clock_intr(void *priv);
     59 
     60 
     61 struct clock_softc {
     62 	device_t		sc_dev;
     63 	struct todr_chip_handle	sc_todr;
     64 };
     65 
     66 static struct timecounter clock_timecounter = {
     67 	.tc_get_timecount = clock_getcounter,
     68 	.tc_counter_mask = ~0u,
     69 	.tc_frequency = 1000000000ULL,
     70 	.tc_name = "CLOCK_MONOTONIC",
     71 	.tc_quality = -100,
     72 };
     73 
     74 timer_t clock_timerid;
     75 int clock_running = 0;
     76 
     77 CFATTACH_DECL_NEW(clock, sizeof(struct clock_softc),
     78     clock_match, clock_attach, NULL, NULL);
     79 
     80 static int
     81 clock_match(device_t parent, cfdata_t match, void *opaque)
     82 {
     83 	struct thunkbus_attach_args *taa = opaque;
     84 
     85 	if (taa->taa_type != THUNKBUS_TYPE_CLOCK)
     86 		return 0;
     87 
     88 	return 1;
     89 }
     90 
     91 static void
     92 clock_attach(device_t parent, device_t self, void *opaque)
     93 {
     94 	struct clock_softc *sc = device_private(self);
     95 
     96 	aprint_naive("\n");
     97 	aprint_normal("\n");
     98 
     99 	sc->sc_dev = self;
    100 
    101 	sc->sc_todr.todr_gettime = clock_todr_gettime;
    102 	todr_attach(&sc->sc_todr);
    103 
    104 	clock_timerid = thunk_timer_attach();
    105 	clock_timecounter.tc_quality = 1000;
    106 	tc_init(&clock_timecounter);
    107 
    108 	setup_clock_intr();
    109 	clock_running = 1;
    110 }
    111 
    112 void
    113 clock_intr(void *priv)
    114 {
    115 	struct clockframe cf;
    116 	int nticks = thunk_timer_getoverrun(clock_timerid) + 1;
    117 
    118 	while (nticks-- > 0) {
    119 		hardclock(&cf);
    120 	}
    121 }
    122 
    123 
    124 static unsigned int
    125 clock_getcounter(struct timecounter *tc)
    126 {
    127 	return thunk_getcounter();
    128 }
    129 
    130 static int
    131 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv)
    132 {
    133 	struct thunk_timeval ttv;
    134 	int error;
    135 
    136 	error = thunk_gettimeofday(&ttv, NULL);
    137 	if (error)
    138 		return error;
    139 
    140 	tv->tv_sec = ttv.tv_sec;
    141 	tv->tv_usec = ttv.tv_usec;
    142 
    143 	return 0;
    144 }
    145