xref: /netbsd-src/sys/arch/usermode/dev/clock.c (revision e8bec33be121040b935e764acaa45ddbf4d7353c)
1 /* $NetBSD: clock.c,v 1.25 2012/01/14 21:42:51 reinoud Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Jared D. McNeill <jmcneill@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.25 2012/01/14 21:42:51 reinoud 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_signal(int sig, siginfo_t *info, void *ctx);
54 static unsigned int clock_getcounter(struct timecounter *);
55 
56 static int	clock_todr_gettime(struct todr_chip_handle *, struct timeval *);
57 
58 struct clock_softc {
59 	device_t		sc_dev;
60 	struct todr_chip_handle	sc_todr;
61 };
62 
63 static struct timecounter clock_timecounter = {
64 	clock_getcounter,	/* get_timecount */
65 	0,			/* no poll_pps */
66 	~0u,			/* counter_mask */
67 	1000000000ULL,		/* frequency */
68 	"CLOCK_MONOTONIC",	/* name */
69 	-100,			/* quality */
70 	NULL,			/* prev */
71 	NULL,			/* next */
72 };
73 
74 timer_t clock_timerid;
75 
76 CFATTACH_DECL_NEW(clock, sizeof(struct clock_softc),
77     clock_match, clock_attach, NULL, NULL);
78 
79 static int
80 clock_match(device_t parent, cfdata_t match, void *opaque)
81 {
82 	struct thunkbus_attach_args *taa = opaque;
83 
84 	if (taa->taa_type != THUNKBUS_TYPE_CLOCK)
85 		return 0;
86 
87 	return 1;
88 }
89 
90 static void
91 clock_attach(device_t parent, device_t self, void *opaque)
92 {
93 	static struct sigaction sa;
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 	memset(&sa, 0, sizeof(sa));
105 	thunk_sigemptyset(&sa.sa_mask);
106 	sa.sa_sigaction = clock_signal;
107 	sa.sa_flags = SA_RESTART | SA_ONSTACK;
108 	if (thunk_sigaction(SIGALRM, &sa, NULL) == -1)
109 		panic("couldn't register SIGALRM handler : %d",
110 		    thunk_geterrno());
111 
112 	clock_timerid = thunk_timer_attach();
113 
114 	clock_timecounter.tc_quality = 1000;
115 	tc_init(&clock_timecounter);
116 }
117 
118 static void
119 clock_intr(void *priv)
120 {
121 	struct clockframe cf;
122 	int nticks = thunk_timer_getoverrun(clock_timerid) + 1;
123 
124 	while (nticks-- > 0) {
125 		hardclock(&cf);
126 	}
127 }
128 
129 static void
130 clock_signal(int sig, siginfo_t *info, void *ctx)
131 {
132 	curcpu()->ci_idepth++;
133 	spl_intr(IPL_SOFTCLOCK, clock_intr, NULL);
134 	curcpu()->ci_idepth--;
135 }
136 
137 static unsigned int
138 clock_getcounter(struct timecounter *tc)
139 {
140 	return thunk_getcounter();
141 }
142 
143 static int
144 clock_todr_gettime(struct todr_chip_handle *tch, struct timeval *tv)
145 {
146 	struct thunk_timeval ttv;
147 	int error;
148 
149 	error = thunk_gettimeofday(&ttv, NULL);
150 	if (error)
151 		return error;
152 
153 	tv->tv_sec = ttv.tv_sec;
154 	tv->tv_usec = ttv.tv_usec;
155 
156 	return 0;
157 }
158