xref: /freebsd/sys/kern/kern_et.c (revision 41059135ce931c0f1014a999ffabc6bc470ce856)
1 /*-
2  * Copyright (c) 2010-2013 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
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 ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/sbuf.h>
33 #include <sys/sysctl.h>
34 #include <sys/systm.h>
35 #include <sys/queue.h>
36 #include <sys/timeet.h>
37 
38 #include "opt_timer.h"
39 
40 SLIST_HEAD(et_eventtimers_list, eventtimer);
41 static struct et_eventtimers_list eventtimers = SLIST_HEAD_INITIALIZER(et_eventtimers);
42 
43 struct mtx	et_eventtimers_mtx;
44 MTX_SYSINIT(et_eventtimers_init, &et_eventtimers_mtx, "et_mtx", MTX_DEF);
45 
46 SYSCTL_NODE(_kern, OID_AUTO, eventtimer, CTLFLAG_RW, 0, "Event timers");
47 static SYSCTL_NODE(_kern_eventtimer, OID_AUTO, et, CTLFLAG_RW, 0, "");
48 
49 /*
50  * Register a new event timer hardware.
51  */
52 int
53 et_register(struct eventtimer *et)
54 {
55 	struct eventtimer *tmp, *next;
56 
57 	if (et->et_quality >= 0 || bootverbose) {
58 		if (et->et_frequency == 0) {
59 			printf("Event timer \"%s\" quality %d\n",
60 			    et->et_name, et->et_quality);
61 		} else {
62 			printf("Event timer \"%s\" "
63 			    "frequency %ju Hz quality %d\n",
64 			    et->et_name, (uintmax_t)et->et_frequency,
65 			    et->et_quality);
66 		}
67 	}
68 	KASSERT(et->et_start, ("et_register: timer has no start function"));
69 	et->et_sysctl = SYSCTL_ADD_NODE_WITH_LABEL(NULL,
70 	    SYSCTL_STATIC_CHILDREN(_kern_eventtimer_et), OID_AUTO, et->et_name,
71 	    CTLFLAG_RW, 0, "event timer description", "eventtimer");
72 	SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(et->et_sysctl), OID_AUTO,
73 	    "flags", CTLFLAG_RD, &(et->et_flags), 0,
74 	    "Event timer capabilities");
75 	SYSCTL_ADD_UQUAD(NULL, SYSCTL_CHILDREN(et->et_sysctl), OID_AUTO,
76 	    "frequency", CTLFLAG_RD, &(et->et_frequency),
77 	    "Event timer base frequency");
78 	SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(et->et_sysctl), OID_AUTO,
79 	    "quality", CTLFLAG_RD, &(et->et_quality), 0,
80 	    "Goodness of event timer");
81 	ET_LOCK();
82 	if (SLIST_EMPTY(&eventtimers) ||
83 	    SLIST_FIRST(&eventtimers)->et_quality < et->et_quality) {
84 		SLIST_INSERT_HEAD(&eventtimers, et, et_all);
85 	} else {
86 		SLIST_FOREACH(tmp, &eventtimers, et_all) {
87 			next = SLIST_NEXT(tmp, et_all);
88 			if (next == NULL || next->et_quality < et->et_quality) {
89 				SLIST_INSERT_AFTER(tmp, et, et_all);
90 				break;
91 			}
92 		}
93 	}
94 	ET_UNLOCK();
95 	return (0);
96 }
97 
98 /*
99  * Deregister event timer hardware.
100  */
101 int
102 et_deregister(struct eventtimer *et)
103 {
104 	int err = 0;
105 
106 	if (et->et_deregister_cb != NULL) {
107 		if ((err = et->et_deregister_cb(et, et->et_arg)) != 0)
108 			return (err);
109 	}
110 
111 	ET_LOCK();
112 	SLIST_REMOVE(&eventtimers, et, eventtimer, et_all);
113 	ET_UNLOCK();
114 	sysctl_remove_oid(et->et_sysctl, 1, 1);
115 	return (0);
116 }
117 
118 /*
119  * Change the frequency of the given timer.  If it is the active timer,
120  * reconfigure it on all CPUs (reschedules all current events based on the new
121  * timer frequency).
122  */
123 void
124 et_change_frequency(struct eventtimer *et, uint64_t newfreq)
125 {
126 
127 #ifndef NO_EVENTTIMERS
128 	cpu_et_frequency(et, newfreq);
129 #endif
130 }
131 
132 /*
133  * Find free event timer hardware with specified parameters.
134  */
135 struct eventtimer *
136 et_find(const char *name, int check, int want)
137 {
138 	struct eventtimer *et = NULL;
139 
140 	SLIST_FOREACH(et, &eventtimers, et_all) {
141 		if (et->et_active)
142 			continue;
143 		if (name != NULL && strcasecmp(et->et_name, name) != 0)
144 			continue;
145 		if (name == NULL && et->et_quality < 0)
146 			continue;
147 		if ((et->et_flags & check) != want)
148 			continue;
149 		break;
150 	}
151 	return (et);
152 }
153 
154 /*
155  * Initialize event timer hardware. Set callbacks.
156  */
157 int
158 et_init(struct eventtimer *et, et_event_cb_t *event,
159     et_deregister_cb_t *deregister, void *arg)
160 {
161 
162 	if (event == NULL)
163 		return (EINVAL);
164 	if (et->et_active)
165 		return (EBUSY);
166 
167 	et->et_active = 1;
168 	et->et_event_cb = event;
169 	et->et_deregister_cb = deregister;
170 	et->et_arg = arg;
171 	return (0);
172 }
173 
174 /*
175  * Start event timer hardware.
176  * first - delay before first tick.
177  * period - period of subsequent periodic ticks.
178  */
179 int
180 et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
181 {
182 
183 	if (!et->et_active)
184 		return (ENXIO);
185 	KASSERT(period >= 0, ("et_start: negative period"));
186 	KASSERT((et->et_flags & ET_FLAGS_PERIODIC) || period == 0,
187 		("et_start: period specified for oneshot-only timer"));
188 	KASSERT((et->et_flags & ET_FLAGS_ONESHOT) || period != 0,
189 		("et_start: period not specified for periodic-only timer"));
190 	if (period != 0) {
191 		if (period < et->et_min_period)
192 		        period = et->et_min_period;
193 		else if (period > et->et_max_period)
194 		        period = et->et_max_period;
195 	}
196 	if (period == 0 || first != 0) {
197 		if (first < et->et_min_period)
198 		        first = et->et_min_period;
199 		else if (first > et->et_max_period)
200 		        first = et->et_max_period;
201 	}
202 	return (et->et_start(et, first, period));
203 }
204 
205 /* Stop event timer hardware. */
206 int
207 et_stop(struct eventtimer *et)
208 {
209 
210 	if (!et->et_active)
211 		return (ENXIO);
212 	if (et->et_stop)
213 		return (et->et_stop(et));
214 	return (0);
215 }
216 
217 /* Mark event timer hardware as broken. */
218 int
219 et_ban(struct eventtimer *et)
220 {
221 
222 	et->et_flags &= ~(ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT);
223 	return (0);
224 }
225 
226 /* Free event timer hardware. */
227 int
228 et_free(struct eventtimer *et)
229 {
230 
231 	if (!et->et_active)
232 		return (ENXIO);
233 
234 	et->et_active = 0;
235 	return (0);
236 }
237 
238 /* Report list of supported event timer hardware via sysctl. */
239 static int
240 sysctl_kern_eventtimer_choice(SYSCTL_HANDLER_ARGS)
241 {
242 	struct sbuf sb;
243 	struct eventtimer *et;
244 	int error;
245 
246 	sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
247 
248 	ET_LOCK();
249 	SLIST_FOREACH(et, &eventtimers, et_all) {
250 		if (et != SLIST_FIRST(&eventtimers))
251 			sbuf_putc(&sb, ' ');
252 		sbuf_printf(&sb, "%s(%d)", et->et_name, et->et_quality);
253 	}
254 	ET_UNLOCK();
255 
256 	error = sbuf_finish(&sb);
257 	if (error == 0)
258 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
259 	sbuf_delete(&sb);
260 	return (error);
261 }
262 SYSCTL_PROC(_kern_eventtimer, OID_AUTO, choice,
263     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
264     0, 0, sysctl_kern_eventtimer_choice, "A", "Present event timers");
265 
266