Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.16
      1 /* $NetBSD: clock.c,v 1.16 2011/09/08 12:10:13 jmcneill 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 <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.16 2011/09/08 12:10:13 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/proc.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 #include <sys/timetc.h>
     37 #include <sys/time.h>
     38 
     39 #include <machine/mainbus.h>
     40 #include <machine/thunk.h>
     41 
     42 #include <dev/clock_subr.h>
     43 
     44 static int	clock_match(device_t, cfdata_t, void *);
     45 static void	clock_attach(device_t, device_t, void *);
     46 
     47 static void	clock_signal(int sig);
     48 static unsigned int clock_getcounter(struct timecounter *);
     49 
     50 static int	clock_todr_gettime(struct todr_chip_handle *, struct timeval *);
     51 
     52 typedef struct clock_softc {
     53 	device_t		sc_dev;
     54 	struct todr_chip_handle	sc_todr;
     55 } clock_softc_t;
     56 
     57 static struct timecounter clock_timecounter = {
     58 	clock_getcounter,	/* get_timecount */
     59 	0,			/* no poll_pps */
     60 	~0u,			/* counter_mask */
     61 	0,			/* frequency */
     62 	"CLOCK_MONOTONIC",	/* name */
     63 	-100,			/* quality */
     64 	NULL,			/* prev */
     65 	NULL,			/* next */
     66 };
     67 
     68 static struct clock_softc *clock_sc;
     69 
     70 CFATTACH_DECL_NEW(clock, sizeof(clock_softc_t),
     71     clock_match, clock_attach, NULL, NULL);
     72 
     73 static int
     74 clock_match(device_t parent, cfdata_t match, void *opaque)
     75 {
     76 	struct thunkbus_attach_args *taa = opaque;
     77 
     78 	if (taa->taa_type != THUNKBUS_TYPE_CLOCK)
     79 		return 0;
     80 
     81 	return 1;
     82 }
     83 
     84 static void
     85 clock_attach(device_t parent, device_t self, void *opaque)
     86 {
     87 	static struct sigaction sa;
     88 	clock_softc_t *sc = device_private(self);
     89 	stack_t ss;
     90 	long tcres;
     91 
     92 	aprint_naive("\n");
     93 	aprint_normal("\n");
     94 
     95 	KASSERT(clock_sc == NULL);
     96 	clock_sc = sc;
     97 
     98 	sc->sc_dev = self;
     99 
    100 	sc->sc_todr.todr_gettime = clock_todr_gettime;
    101 	todr_attach(&sc->sc_todr);
    102 
    103 	ss.ss_sp = thunk_malloc(SIGSTKSZ);
    104 	if (ss.ss_sp == NULL)
    105 		panic("%s: couldn't allocate signal stack", __func__);
    106 	ss.ss_size = SIGSTKSZ;
    107 	ss.ss_flags = 0;
    108 	if (thunk_sigaltstack(&ss, NULL) == -1)
    109 		panic("%s: couldn't setup signal stack", __func__);
    110 
    111 	memset(&sa, 0, sizeof(sa));
    112 	sigfillset(&sa.sa_mask);
    113 	sa.sa_handler = clock_signal;
    114 	sa.sa_flags = SA_ONSTACK;
    115 	if (thunk_sigaction(SIGALRM, &sa, NULL) == -1)
    116 		panic("couldn't register SIGALRM handler : %d",
    117 		    thunk_geterrno());
    118 
    119 	tcres = thunk_clock_getres_monotonic();
    120 	if (tcres > 0) {
    121 		clock_timecounter.tc_quality = 1000;
    122 		clock_timecounter.tc_frequency = 1000000000 / tcres;
    123 	}
    124 	tc_init(&clock_timecounter);
    125 }
    126 
    127 static void
    128 clock_signal(int sig)
    129 {
    130 	struct clockframe cf;
    131 
    132 	curcpu()->ci_idepth++;
    133 
    134 	hardclock(&cf);
    135 
    136 	curcpu()->ci_idepth--;
    137 }
    138 
    139 static unsigned int
    140 clock_getcounter(struct timecounter *tc)
    141 {
    142 	return thunk_getcounter();
    143 }
    144 
    145 static int
    146 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv)
    147 {
    148 	struct thunk_timeval ttv;
    149 	int error;
    150 
    151 	error = thunk_gettimeofday(&ttv, NULL);
    152 	if (error)
    153 		return error;
    154 
    155 	tv->tv_sec = ttv.tv_sec;
    156 	tv->tv_usec = ttv.tv_usec;
    157 
    158 	return 0;
    159 }
    160