1 /* 2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $DragonFly: src/sys/kern/kern_systimer.c,v 1.1 2004/01/30 05:42:17 dillon Exp $ 27 */ 28 29 /* 30 * This code implements a fine-grained per-cpu system timer which is 31 * ultimately based on a hardware timer. The hardware timer abstraction 32 * is sufficiently disconnected from this code to support both per-cpu 33 * hardware timers or a single system-wide hardware timer. 34 * 35 * Notes on machine-dependant code (in arch/arch/systimer.c) 36 * 37 * cputimer_intr_reload() Reload the one-shot (per-cpu basis) 38 * 39 * cputimer_count() Get the current absolute sysclock_t value. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/systm.h> 45 #include <sys/thread.h> 46 #include <sys/globaldata.h> 47 #include <sys/systimer.h> 48 #include <sys/thread2.h> 49 50 /* 51 * Execute ready systimers. Called directly from the platform-specific 52 * one-shot timer clock interrupt (e.g. clkintr()). Systimer functions are 53 * responsible for calling hardclock, statclock, and other finely-timed 54 * routines. 55 */ 56 void 57 systimer_intr(sysclock_t *timep, struct intrframe *frame) 58 { 59 globaldata_t gd = mycpu; 60 sysclock_t time = *timep; 61 systimer_t info; 62 63 if (gd->gd_syst_nest) 64 return; 65 66 crit_enter(); 67 ++gd->gd_syst_nest; 68 while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) { 69 /* 70 * If we haven't reached the requested time, tell the cputimer 71 * how much is left and break out. 72 */ 73 if ((int)(info->time - time) > 0) { 74 cputimer_intr_reload(info->time - time); 75 break; 76 } 77 78 /* 79 * Dequeue and execute 80 */ 81 info->flags &= ~SYSTF_ONQUEUE; 82 TAILQ_REMOVE(info->queue, info, node); 83 crit_exit(); 84 info->func(info, frame); 85 crit_enter(); 86 87 /* 88 * Reinstall if periodic 89 */ 90 if (info->periodic) { 91 info->time += info->periodic; 92 systimer_add(info); 93 } 94 } 95 if (info) 96 gd->gd_nextclock = info->time; 97 else 98 gd->gd_nextclock = 0; 99 --gd->gd_syst_nest; 100 crit_exit(); 101 } 102 103 void 104 systimer_add(systimer_t info) 105 { 106 struct globaldata *gd = mycpu; 107 108 KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0); 109 crit_enter(); 110 if (info->gd == gd) { 111 systimer_t scan1; 112 systimer_t scan2; 113 scan1 = TAILQ_FIRST(&gd->gd_systimerq); 114 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) { 115 gd->gd_nextclock = info->time; 116 cputimer_intr_reload(info->time - cputimer_count()); 117 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node); 118 } else { 119 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq); 120 for (;;) { 121 if (scan1 == NULL) { 122 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node); 123 break; 124 } 125 if ((int)(scan1->time - info->time) > 0) { 126 TAILQ_INSERT_BEFORE(scan1, info, node); 127 break; 128 } 129 if ((int)(scan2->time - info->time) <= 0) { 130 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node); 131 break; 132 } 133 scan1 = TAILQ_NEXT(scan1, node); 134 scan2 = TAILQ_PREV(scan2, systimerq, node); 135 } 136 } 137 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING; 138 info->queue = &gd->gd_systimerq; 139 } else { 140 info->flags |= SYSTF_IPIRUNNING; 141 lwkt_send_ipiq(info->gd->gd_cpuid, (ipifunc_t)systimer_add, info); 142 } 143 crit_exit(); 144 } 145 146 /* 147 * systimer_del() 148 * 149 * Delete a system timer. Only the owning cpu can delete a timer. 150 */ 151 void 152 systimer_del(systimer_t info) 153 { 154 KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0); 155 crit_enter(); 156 if (info->flags & SYSTF_ONQUEUE) { 157 TAILQ_REMOVE(info->queue, info, node); 158 info->flags &= ~SYSTF_ONQUEUE; 159 } 160 crit_exit(); 161 } 162 163 /* 164 * systimer_init_periodic() 165 * 166 * Initialize a periodic timer at the specified frequency and add 167 * it to the system. The frequency is uncompensated and approximate. 168 * 169 * Try to synchronize multi registrations of the same or similar 170 * frequencies so the hardware interrupt is able to dispatch several 171 * at together by adjusting the phase of the initial interrupt. This 172 * helps SMP. Note that we are not attempting to synchronize to 173 * the realtime clock. 174 */ 175 void 176 systimer_init_periodic(systimer_t info, void *func, void *data, int hz) 177 { 178 sysclock_t base_count; 179 180 bzero(info, sizeof(struct systimer)); 181 info->periodic = cputimer_fromhz(hz); 182 base_count = cputimer_count(); 183 base_count = base_count - (base_count % info->periodic); 184 info->time = base_count + info->periodic; 185 info->func = func; 186 info->data = data; 187 info->gd = mycpu; 188 systimer_add(info); 189 } 190 191 /* 192 * systimer_init_oneshot() 193 * 194 * Initialize a periodic timer at the specified frequency and add 195 * it to the system. The frequency is uncompensated and approximate. 196 */ 197 void 198 systimer_init_oneshot(systimer_t info, void *func, void *data, int us) 199 { 200 bzero(info, sizeof(struct systimer)); 201 info->time = cputimer_count() + cputimer_fromus(us); 202 info->func = func; 203 info->data = data; 204 info->gd = mycpu; 205 systimer_add(info); 206 } 207 208