kern_et.c revision 209371
1/*- 2 * Copyright (c) 2010 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: head/sys/kern/kern_et.c 209371 2010-06-20 21:33:29Z mav $"); 29 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/sysctl.h> 33#include <sys/systm.h> 34#include <sys/queue.h> 35#include <sys/timeet.h> 36 37SLIST_HEAD(et_eventtimers_list, eventtimer); 38static struct et_eventtimers_list eventtimers = SLIST_HEAD_INITIALIZER(et_eventtimers); 39 40struct mtx et_eventtimers_mtx; 41MTX_SYSINIT(et_eventtimers_init, &et_eventtimers_mtx, "et_mtx", MTX_SPIN); 42 43SYSCTL_NODE(_kern, OID_AUTO, eventtimer, CTLFLAG_RW, 0, "Event timers"); 44SYSCTL_NODE(_kern_eventtimer, OID_AUTO, et, CTLFLAG_RW, 0, ""); 45 46/* 47 * Register a new event timer hardware. 48 */ 49int 50et_register(struct eventtimer *et) 51{ 52 struct eventtimer *tmp, *next; 53 54 if (et->et_quality >= 0 || bootverbose) { 55 printf("Event timer \"%s\" frequency %ju Hz quality %d\n", 56 et->et_name, (uintmax_t)et->et_frequency, 57 et->et_quality); 58 } 59 et->et_sysctl = SYSCTL_ADD_NODE(NULL, 60 SYSCTL_STATIC_CHILDREN(_kern_eventtimer_et), OID_AUTO, et->et_name, 61 CTLFLAG_RW, 0, "event timer description"); 62 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(et->et_sysctl), OID_AUTO, 63 "flags", CTLFLAG_RD, &(et->et_flags), 0, 64 "Event timer capabilities"); 65 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(et->et_sysctl), OID_AUTO, 66 "frequency", CTLFLAG_RD, &(et->et_frequency), 0, 67 "Event timer base frequency"); 68 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(et->et_sysctl), OID_AUTO, 69 "quality", CTLFLAG_RD, &(et->et_quality), 0, 70 "Goodness of event timer"); 71 ET_LOCK(); 72 if (SLIST_EMPTY(&eventtimers) || 73 SLIST_FIRST(&eventtimers)->et_quality < et->et_quality) { 74 SLIST_INSERT_HEAD(&eventtimers, et, et_all); 75 } else { 76 SLIST_FOREACH(tmp, &eventtimers, et_all) { 77 next = SLIST_NEXT(tmp, et_all); 78 if (next == NULL || next->et_quality < et->et_quality) { 79 SLIST_INSERT_AFTER(tmp, et, et_all); 80 break; 81 } 82 } 83 } 84 ET_UNLOCK(); 85 return (0); 86} 87 88/* 89 * Deregister event timer hardware. 90 */ 91int 92et_deregister(struct eventtimer *et) 93{ 94 int err = 0; 95 96 if (et->et_deregister_cb != NULL) { 97 if ((err = et->et_deregister_cb(et, et->et_arg)) != 0) 98 return (err); 99 } 100 101 ET_LOCK(); 102 SLIST_REMOVE(&eventtimers, et, eventtimer, et_all); 103 ET_UNLOCK(); 104 sysctl_remove_oid(et->et_sysctl, 1, 1); 105 return (0); 106} 107 108/* 109 * Find free event timer hardware with specified parameters. 110 */ 111struct eventtimer * 112et_find(const char *name, int check, int want) 113{ 114 struct eventtimer *et = NULL; 115 116 SLIST_FOREACH(et, &eventtimers, et_all) { 117 if (et->et_active) 118 continue; 119 if (name != NULL && strcasecmp(et->et_name, name) != 0) 120 continue; 121 if (name == NULL && et->et_quality < 0) 122 continue; 123 if ((et->et_flags & check) != want) 124 continue; 125 break; 126 } 127 return (et); 128} 129 130/* 131 * Initialize event timer hardware. Set callbacks. 132 */ 133int 134et_init(struct eventtimer *et, et_event_cb_t *event, 135 et_deregister_cb_t *deregister, void *arg) 136{ 137 138 if (event == NULL) 139 return (EINVAL); 140 if (et->et_active) 141 return (EBUSY); 142 143 et->et_active = 1; 144 et->et_event_cb = event; 145 et->et_deregister_cb = deregister; 146 et->et_arg = arg; 147 return (0); 148} 149 150/* 151 * Start event timer hardware. 152 * first - delay before first tick. 153 * period - period of subsequent periodic ticks. 154 */ 155int 156et_start(struct eventtimer *et, 157 struct bintime *first, struct bintime *period) 158{ 159 160 if (!et->et_active) 161 return (ENXIO); 162 if (first == NULL && period == NULL) 163 return (EINVAL); 164 if ((et->et_flags & ET_FLAGS_PERIODIC) == 0 && 165 period != NULL) 166 return (ENODEV); 167 if ((et->et_flags & ET_FLAGS_ONESHOT) == 0 && 168 period == NULL) 169 return (ENODEV); 170 if (et->et_start) 171 return (et->et_start(et, first, period)); 172 return (0); 173} 174 175/* Stop event timer hardware. */ 176int 177et_stop(struct eventtimer *et) 178{ 179 180 if (!et->et_active) 181 return (ENXIO); 182 if (et->et_stop) 183 return (et->et_stop(et)); 184 return (0); 185} 186 187/* Mark event timer hardware as broken. */ 188int 189et_ban(struct eventtimer *et) 190{ 191 192 et->et_flags &= ~(ET_FLAGS_PERIODIC | ET_FLAGS_ONESHOT); 193 return (0); 194} 195 196/* Free event timer hardware. */ 197int 198et_free(struct eventtimer *et) 199{ 200 201 if (!et->et_active) 202 return (ENXIO); 203 204 et->et_active = 0; 205 return (0); 206} 207 208/* Report list of supported event timers hardware via sysctl. */ 209static int 210sysctl_kern_eventtimer_choice(SYSCTL_HANDLER_ARGS) 211{ 212 char buf[512], *spc; 213 struct eventtimer *et; 214 int error, off; 215 216 spc = ""; 217 error = 0; 218 off = 0; 219 ET_LOCK(); 220 SLIST_FOREACH(et, &eventtimers, et_all) { 221 off += snprintf(buf + off, sizeof(buf) - off, "%s%s(%d)", 222 spc, et->et_name, et->et_quality); 223 spc = " "; 224 } 225 ET_UNLOCK(); 226 error = SYSCTL_OUT(req, buf, strlen(buf)); 227 return (error); 228} 229SYSCTL_PROC(_kern_eventtimer, OID_AUTO, choice, 230 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 231 0, 0, sysctl_kern_eventtimer_choice, "A", "Present event timers"); 232 233