Home | History | Annotate | Line # | Download | only in dev
clock.c revision 1.23
      1 /* $NetBSD: clock.c,v 1.23 2011/12/15 03:42:32 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 "opt_hz.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.23 2011/12/15 03:42:32 jmcneill 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 void	clock(void);
     54 static void	clock_signal(int sig, siginfo_t *info, void *ctx);
     55 static unsigned int clock_getcounter(struct timecounter *);
     56 
     57 static int	clock_todr_gettime(struct todr_chip_handle *, struct timeval *);
     58 
     59 struct clock_softc {
     60 	device_t		sc_dev;
     61 	struct todr_chip_handle	sc_todr;
     62 };
     63 
     64 static struct timecounter clock_timecounter = {
     65 	clock_getcounter,	/* get_timecount */
     66 	0,			/* no poll_pps */
     67 	~0u,			/* counter_mask */
     68 	1000000000ULL,		/* frequency */
     69 	"CLOCK_MONOTONIC",	/* name */
     70 	-100,			/* quality */
     71 	NULL,			/* prev */
     72 	NULL,			/* next */
     73 };
     74 
     75 timer_t clock_timerid;
     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 	static struct sigaction sa;
     95 	struct clock_softc *sc = device_private(self);
     96 
     97 	aprint_naive("\n");
     98 	aprint_normal("\n");
     99 
    100 	sc->sc_dev = self;
    101 
    102 	sc->sc_todr.todr_gettime = clock_todr_gettime;
    103 	todr_attach(&sc->sc_todr);
    104 
    105 	memset(&sa, 0, sizeof(sa));
    106 	thunk_sigemptyset(&sa.sa_mask);
    107 	sa.sa_sigaction = clock_signal;
    108 	sa.sa_flags = SA_RESTART | SA_ONSTACK;
    109 	if (thunk_sigaction(SIGALRM, &sa, NULL) == -1)
    110 		panic("couldn't register SIGALRM handler : %d",
    111 		    thunk_geterrno());
    112 
    113 	clock_timerid = thunk_timer_attach();
    114 
    115 	clock_timecounter.tc_quality = 1000;
    116 	tc_init(&clock_timecounter);
    117 }
    118 
    119 static void
    120 clock_intr(void *priv)
    121 {
    122 	struct clockframe cf;
    123 	int nticks = thunk_timer_getoverrun(clock_timerid) + 1;
    124 
    125 	while (nticks-- > 0) {
    126 		hardclock(&cf);
    127 	};
    128 }
    129 
    130 static void
    131 clock(void)
    132 {
    133 	curcpu()->ci_idepth++;
    134 	spl_intr(IPL_SOFTCLOCK, clock_intr, NULL);
    135 	curcpu()->ci_idepth--;
    136 }
    137 
    138 static void
    139 clock_signal(int sig, siginfo_t *info, void *ctx)
    140 {
    141 #if 0
    142 	ucontext_t *uct = ctx;
    143 	struct lwp *l;
    144 	struct pcb *pcb;
    145 
    146 	l = curlwp;
    147 	pcb = lwp_getpcb(l);
    148 
    149 	/* copy this state as where the lwp was XXX NEEDED? */
    150 	memcpy(&pcb->pcb_ucp, uct, sizeof(ucontext_t));
    151 #endif
    152 
    153 	clock();
    154 }
    155 
    156 static unsigned int
    157 clock_getcounter(struct timecounter *tc)
    158 {
    159 	return thunk_getcounter();
    160 }
    161 
    162 static int
    163 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv)
    164 {
    165 	struct thunk_timeval ttv;
    166 	int error;
    167 
    168 	error = thunk_gettimeofday(&ttv, NULL);
    169 	if (error)
    170 		return error;
    171 
    172 	tv->tv_sec = ttv.tv_sec;
    173 	tv->tv_usec = ttv.tv_usec;
    174 
    175 	return 0;
    176 }
    177