1 /* 2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/sys/kern/kern_systimer.c,v 1.12 2007/10/16 11:12:59 sephe Exp $ 35 */ 36 37 /* 38 * WARNING! THE SYSTIMER MODULE DOES NOT OPERATE OR DISPATCH WITH THE 39 * MP LOCK HELD. ALL CODE USING THIS MODULE MUST BE MP-SAFE. 40 * 41 * This code implements a fine-grained per-cpu system timer which is 42 * ultimately based on a hardware timer. The hardware timer abstraction 43 * is sufficiently disconnected from this code to support both per-cpu 44 * hardware timers or a single system-wide hardware timer. 45 * 46 * WARNING! During early boot if a new system timer is selected, existing 47 * timeouts will not be effected and will thus occur slower or faster. 48 * periodic timers will be adjusted at the next periodic load. 49 * 50 * Notes on machine-dependant code (in arch/arch/systimer.c) 51 * 52 * cputimer_intr_reload() Reload the one-shot (per-cpu basis) 53 */ 54 55 #include <sys/param.h> 56 #include <sys/kernel.h> 57 #include <sys/systm.h> 58 #include <sys/thread.h> 59 #include <sys/globaldata.h> 60 #include <sys/systimer.h> 61 #include <sys/thread2.h> 62 63 /* 64 * Execute ready systimers. Called directly from the platform-specific 65 * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI. May 66 * be called simultaniously on multiple cpus and always operations on 67 * the current cpu's queue. Systimer functions are responsible for calling 68 * hardclock, statclock, and other finely-timed routines. 69 */ 70 void 71 systimer_intr(sysclock_t *timep, int dummy, struct intrframe *frame) 72 { 73 globaldata_t gd = mycpu; 74 sysclock_t time = *timep; 75 systimer_t info; 76 77 if (gd->gd_syst_nest) 78 return; 79 80 crit_enter(); 81 ++gd->gd_syst_nest; 82 while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) { 83 /* 84 * If we haven't reached the requested time, tell the cputimer 85 * how much is left and break out. 86 */ 87 if ((int)(info->time - time) > 0) { 88 cputimer_intr_reload(info->time - time); 89 break; 90 } 91 92 /* 93 * Dequeue and execute, detect a loss of the systimer. Note 94 * that the in-progress systimer pointer can only be used to 95 * detect a loss of the systimer, it is only useful within 96 * this code sequence and becomes stale otherwise. 97 */ 98 info->flags &= ~SYSTF_ONQUEUE; 99 TAILQ_REMOVE(info->queue, info, node); 100 gd->gd_systimer_inprog = info; 101 crit_exit(); 102 info->func(info, frame); 103 crit_enter(); 104 105 /* 106 * The caller may deleted or even re-queue the systimer itself 107 * with a delete/add sequence. If the caller does not mess with 108 * the systimer we will requeue the periodic interval automatically. 109 * 110 * If this is a non-queued periodic interrupt, do not allow multiple 111 * events to build up (used for things like the callout timer to 112 * prevent premature timeouts due to long interrupt disablements, 113 * BIOS 8254 glitching, and so forth). However, we still want to 114 * keep things synchronized between cpus for efficient handling of 115 * the timer interrupt so jump in multiples of the periodic rate. 116 */ 117 if (gd->gd_systimer_inprog == info && info->periodic) { 118 if (info->which != sys_cputimer) { 119 info->periodic = sys_cputimer->fromhz(info->freq); 120 info->which = sys_cputimer; 121 } 122 info->time += info->periodic; 123 if ((info->flags & SYSTF_NONQUEUED) && 124 (int)(info->time - time) <= 0 125 ) { 126 info->time += ((time - info->time + info->periodic - 1) / 127 info->periodic) * info->periodic; 128 } 129 systimer_add(info); 130 } 131 gd->gd_systimer_inprog = NULL; 132 } 133 --gd->gd_syst_nest; 134 crit_exit(); 135 } 136 137 void 138 systimer_add(systimer_t info) 139 { 140 struct globaldata *gd = mycpu; 141 142 KKASSERT((info->flags & SYSTF_ONQUEUE) == 0); 143 crit_enter(); 144 if (info->gd == gd) { 145 systimer_t scan1; 146 systimer_t scan2; 147 scan1 = TAILQ_FIRST(&gd->gd_systimerq); 148 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) { 149 cputimer_intr_reload(info->time - sys_cputimer->count()); 150 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node); 151 } else { 152 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq); 153 for (;;) { 154 if (scan1 == NULL) { 155 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node); 156 break; 157 } 158 if ((int)(scan1->time - info->time) > 0) { 159 TAILQ_INSERT_BEFORE(scan1, info, node); 160 break; 161 } 162 if ((int)(scan2->time - info->time) <= 0) { 163 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node); 164 break; 165 } 166 scan1 = TAILQ_NEXT(scan1, node); 167 scan2 = TAILQ_PREV(scan2, systimerq, node); 168 } 169 } 170 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING; 171 info->queue = &gd->gd_systimerq; 172 } else { 173 #ifdef SMP 174 KKASSERT((info->flags & SYSTF_IPIRUNNING) == 0); 175 info->flags |= SYSTF_IPIRUNNING; 176 lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info); 177 #else 178 panic("systimer_add: bad gd in info %p", info); 179 #endif 180 } 181 crit_exit(); 182 } 183 184 /* 185 * systimer_del() 186 * 187 * Delete a system timer. Only the owning cpu can delete a timer. 188 */ 189 void 190 systimer_del(systimer_t info) 191 { 192 struct globaldata *gd = info->gd; 193 194 KKASSERT(gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0); 195 196 crit_enter(); 197 198 if (info->flags & SYSTF_ONQUEUE) { 199 TAILQ_REMOVE(info->queue, info, node); 200 info->flags &= ~SYSTF_ONQUEUE; 201 } 202 203 /* 204 * Deal with dispatch races by clearing the in-progress systimer 205 * pointer. Only a direct pointer comparison can be used, the 206 * actual contents of the structure gd_systimer_inprog points to, 207 * if not equal to info, may be stale. 208 */ 209 if (gd->gd_systimer_inprog == info) 210 gd->gd_systimer_inprog = NULL; 211 212 crit_exit(); 213 } 214 215 /* 216 * systimer_init_periodic() 217 * 218 * Initialize a periodic timer at the specified frequency and add 219 * it to the system. The frequency is uncompensated and approximate. 220 * 221 * Try to synchronize multi registrations of the same or similar 222 * frequencies so the hardware interrupt is able to dispatch several 223 * at together by adjusting the phase of the initial interrupt. This 224 * helps SMP. Note that we are not attempting to synchronize to 225 * the realtime clock. 226 */ 227 void 228 systimer_init_periodic(systimer_t info, void *func, void *data, int hz) 229 { 230 sysclock_t base_count; 231 232 bzero(info, sizeof(struct systimer)); 233 info->periodic = sys_cputimer->fromhz(hz); 234 base_count = sys_cputimer->count(); 235 base_count = base_count - (base_count % info->periodic); 236 info->time = base_count + info->periodic; 237 info->func = func; 238 info->data = data; 239 info->freq = hz; 240 info->which = sys_cputimer; 241 info->gd = mycpu; 242 systimer_add(info); 243 } 244 245 void 246 systimer_init_periodic_nq(systimer_t info, void *func, void *data, int hz) 247 { 248 sysclock_t base_count; 249 250 bzero(info, sizeof(struct systimer)); 251 info->periodic = sys_cputimer->fromhz(hz); 252 base_count = sys_cputimer->count(); 253 base_count = base_count - (base_count % info->periodic); 254 info->time = base_count + info->periodic; 255 info->func = func; 256 info->data = data; 257 info->freq = hz; 258 info->which = sys_cputimer; 259 info->gd = mycpu; 260 info->flags |= SYSTF_NONQUEUED; 261 systimer_add(info); 262 } 263 264 /* 265 * Adjust the periodic interval for a periodic timer which is already 266 * running. The current timeout is not effected. 267 */ 268 void 269 systimer_adjust_periodic(systimer_t info, int hz) 270 { 271 crit_enter(); 272 info->periodic = sys_cputimer->fromhz(hz); 273 info->freq = hz; 274 info->which = sys_cputimer; 275 crit_exit(); 276 } 277 278 /* 279 * systimer_init_oneshot() 280 * 281 * Initialize a periodic timer at the specified frequency and add 282 * it to the system. The frequency is uncompensated and approximate. 283 */ 284 void 285 systimer_init_oneshot(systimer_t info, void *func, void *data, int us) 286 { 287 bzero(info, sizeof(struct systimer)); 288 info->time = sys_cputimer->count() + sys_cputimer->fromus(us); 289 info->func = func; 290 info->data = data; 291 info->which = sys_cputimer; 292 info->gd = mycpu; 293 systimer_add(info); 294 } 295 296