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