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.6 2005/03/27 19:25:09 dillon 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 * Notes on machine-dependant code (in arch/arch/systimer.c) 47 * 48 * cputimer_intr_reload() Reload the one-shot (per-cpu basis) 49 * 50 * cputimer_count() Get the current absolute sysclock_t value. 51 */ 52 53 #include <sys/param.h> 54 #include <sys/kernel.h> 55 #include <sys/systm.h> 56 #include <sys/thread.h> 57 #include <sys/globaldata.h> 58 #include <sys/systimer.h> 59 #include <sys/thread2.h> 60 61 /* 62 * Execute ready systimers. Called directly from the platform-specific 63 * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI. May 64 * be called simultaniously on multiple cpus and always operations on 65 * the current cpu's queue. Systimer functions are responsible for calling 66 * hardclock, statclock, and other finely-timed routines. 67 */ 68 void 69 systimer_intr(sysclock_t *timep, struct intrframe *frame) 70 { 71 globaldata_t gd = mycpu; 72 sysclock_t time = *timep; 73 systimer_t info; 74 75 if (gd->gd_syst_nest) 76 return; 77 78 crit_enter(); 79 ++gd->gd_syst_nest; 80 while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) { 81 /* 82 * If we haven't reached the requested time, tell the cputimer 83 * how much is left and break out. 84 */ 85 if ((int)(info->time - time) > 0) { 86 cputimer_intr_reload(info->time - time); 87 break; 88 } 89 90 /* 91 * Dequeue and execute 92 */ 93 info->flags &= ~SYSTF_ONQUEUE; 94 TAILQ_REMOVE(info->queue, info, node); 95 crit_exit(); 96 info->func(info, frame); 97 crit_enter(); 98 99 /* 100 * Reinstall if periodic. If this is a non-queued periodic 101 * interrupt do not allow multiple events to build up (used 102 * for things like the callout timer to prevent premature timeouts 103 * due to long interrupt disablements, BIOS 8254 glitching, and so 104 * forth). However, we still want to keep things synchronized between 105 * cpus for efficient handling of the timer interrupt so jump in 106 * multiples of the periodic rate. 107 */ 108 if (info->periodic) { 109 info->time += info->periodic; 110 if ((info->flags & SYSTF_NONQUEUED) && 111 (int)(info->time - time) <= 0 112 ) { 113 info->time += ((time - info->time + info->periodic - 1) / 114 info->periodic) * info->periodic; 115 } 116 systimer_add(info); 117 } 118 } 119 --gd->gd_syst_nest; 120 crit_exit(); 121 } 122 123 void 124 systimer_add(systimer_t info) 125 { 126 struct globaldata *gd = mycpu; 127 128 KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0); 129 crit_enter(); 130 if (info->gd == gd) { 131 systimer_t scan1; 132 systimer_t scan2; 133 scan1 = TAILQ_FIRST(&gd->gd_systimerq); 134 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) { 135 cputimer_intr_reload(info->time - cputimer_count()); 136 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node); 137 } else { 138 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq); 139 for (;;) { 140 if (scan1 == NULL) { 141 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node); 142 break; 143 } 144 if ((int)(scan1->time - info->time) > 0) { 145 TAILQ_INSERT_BEFORE(scan1, info, node); 146 break; 147 } 148 if ((int)(scan2->time - info->time) <= 0) { 149 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node); 150 break; 151 } 152 scan1 = TAILQ_NEXT(scan1, node); 153 scan2 = TAILQ_PREV(scan2, systimerq, node); 154 } 155 } 156 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING; 157 info->queue = &gd->gd_systimerq; 158 } else { 159 info->flags |= SYSTF_IPIRUNNING; 160 lwkt_send_ipiq(info->gd, (ipifunc_t)systimer_add, info); 161 } 162 crit_exit(); 163 } 164 165 /* 166 * systimer_del() 167 * 168 * Delete a system timer. Only the owning cpu can delete a timer. 169 */ 170 void 171 systimer_del(systimer_t info) 172 { 173 KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0); 174 crit_enter(); 175 if (info->flags & SYSTF_ONQUEUE) { 176 TAILQ_REMOVE(info->queue, info, node); 177 info->flags &= ~SYSTF_ONQUEUE; 178 } 179 crit_exit(); 180 } 181 182 /* 183 * systimer_init_periodic() 184 * 185 * Initialize a periodic timer at the specified frequency and add 186 * it to the system. The frequency is uncompensated and approximate. 187 * 188 * Try to synchronize multi registrations of the same or similar 189 * frequencies so the hardware interrupt is able to dispatch several 190 * at together by adjusting the phase of the initial interrupt. This 191 * helps SMP. Note that we are not attempting to synchronize to 192 * the realtime clock. 193 */ 194 void 195 systimer_init_periodic(systimer_t info, void *func, void *data, int hz) 196 { 197 sysclock_t base_count; 198 199 bzero(info, sizeof(struct systimer)); 200 info->periodic = cputimer_fromhz(hz); 201 base_count = cputimer_count(); 202 base_count = base_count - (base_count % info->periodic); 203 info->time = base_count + info->periodic; 204 info->func = func; 205 info->data = data; 206 info->gd = mycpu; 207 systimer_add(info); 208 } 209 210 void 211 systimer_init_periodic_nq(systimer_t info, void *func, void *data, int hz) 212 { 213 sysclock_t base_count; 214 215 bzero(info, sizeof(struct systimer)); 216 info->periodic = cputimer_fromhz(hz); 217 base_count = cputimer_count(); 218 base_count = base_count - (base_count % info->periodic); 219 info->time = base_count + info->periodic; 220 info->func = func; 221 info->data = data; 222 info->gd = mycpu; 223 info->flags |= SYSTF_NONQUEUED; 224 systimer_add(info); 225 } 226 227 /* 228 * systimer_init_oneshot() 229 * 230 * Initialize a periodic timer at the specified frequency and add 231 * it to the system. The frequency is uncompensated and approximate. 232 */ 233 void 234 systimer_init_oneshot(systimer_t info, void *func, void *data, int us) 235 { 236 bzero(info, sizeof(struct systimer)); 237 info->time = cputimer_count() + cputimer_fromus(us); 238 info->func = func; 239 info->data = data; 240 info->gd = mycpu; 241 systimer_add(info); 242 } 243 244