2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2010-2013 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/param.h>
30 #include <sys/kernel.h>
32 #include <sys/sysctl.h>
33 #include <sys/systm.h>
34 #include <sys/queue.h>
35 #include <sys/timeet.h>
37 SLIST_HEAD(et_eventtimers_list, eventtimer);
38 static struct et_eventtimers_list eventtimers = SLIST_HEAD_INITIALIZER(et_eventtimers);
40 struct mtx et_eventtimers_mtx;
41 MTX_SYSINIT(et_eventtimers_init, &et_eventtimers_mtx, "et_mtx", MTX_DEF);
43 SYSCTL_NODE(_kern, OID_AUTO, eventtimer, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
45 static SYSCTL_NODE(_kern_eventtimer, OID_AUTO, et,
46 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
50 * Register a new event timer hardware.
53 et_register(struct eventtimer *et)
55 struct eventtimer *tmp, *next;
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);
62 printf("Event timer \"%s\" "
63 "frequency %ju Hz quality %d\n",
64 et->et_name, (uintmax_t)et->et_frequency,
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");
83 if (SLIST_EMPTY(&eventtimers) ||
84 SLIST_FIRST(&eventtimers)->et_quality < et->et_quality) {
85 SLIST_INSERT_HEAD(&eventtimers, et, et_all);
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);
100 * Deregister event timer hardware.
103 et_deregister(struct eventtimer *et)
107 if (et->et_deregister_cb != NULL) {
108 if ((err = et->et_deregister_cb(et, et->et_arg)) != 0)
113 SLIST_REMOVE(&eventtimers, et, eventtimer, et_all);
115 sysctl_remove_oid(et->et_sysctl, 1, 1);
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
125 et_change_frequency(struct eventtimer *et, uint64_t newfreq)
128 cpu_et_frequency(et, newfreq);
132 * Find free event timer hardware with specified parameters.
135 et_find(const char *name, int check, int want)
137 struct eventtimer *et = NULL;
139 SLIST_FOREACH(et, &eventtimers, et_all) {
142 if (name != NULL && strcasecmp(et->et_name, name) != 0)
144 if (name == NULL && et->et_quality < 0)
146 if ((et->et_flags & check) != want)
154 * Initialize event timer hardware. Set callbacks.
157 et_init(struct eventtimer *et, et_event_cb_t *event,
158 et_deregister_cb_t *deregister, void *arg)
167 et->et_event_cb = event;
168 et->et_deregister_cb = deregister;
174 * Start event timer hardware.
175 * first - delay before first tick.
176 * period - period of subsequent periodic ticks.
179 et_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
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"));
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;
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;
201 return (et->et_start(et, first, period));
204 /* Stop event timer hardware. */
206 et_stop(struct eventtimer *et)
212 return (et->et_stop(et));
216 /* Mark event timer hardware as broken. */
218 et_ban(struct eventtimer *et)
221 et->et_flags &= ~(ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT);
225 /* Free event timer hardware. */
227 et_free(struct eventtimer *et)
237 /* Report list of supported event timer hardware via sysctl. */
239 sysctl_kern_eventtimer_choice(SYSCTL_HANDLER_ARGS)
242 struct eventtimer *et;
245 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
248 SLIST_FOREACH(et, &eventtimers, et_all) {
249 if (et != SLIST_FIRST(&eventtimers))
251 sbuf_printf(&sb, "%s(%d)", et->et_name, et->et_quality);
255 error = sbuf_finish(&sb);
257 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
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");