1 /* $NetBSD: timer.c,v 1.7 2006/06/24 04:00:21 tsutsui Exp $ */ 2 /* NetBSD: clock.c,v 1.31 2001/05/27 13:53:24 sommerfeld Exp */ 3 4 /* 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department and Ralph Campbell. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah Hdr: clock.c 1.18 91/01/21 37 * 38 * @(#)clock.c 8.1 (Berkeley) 6/10/93 39 */ 40 /* 41 * Copyright (c) 1988 University of Utah. 42 * 43 * This code is derived from software contributed to Berkeley by 44 * the Systems Programming Group of the University of Utah Computer 45 * Science Department and Ralph Campbell. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. All advertising materials mentioning features or use of this software 56 * must display the following acknowledgement: 57 * This product includes software developed by the University of 58 * California, Berkeley and its contributors. 59 * 4. Neither the name of the University nor the names of its contributors 60 * may be used to endorse or promote products derived from this software 61 * without specific prior written permission. 62 * 63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 73 * SUCH DAMAGE. 74 * 75 * from: Utah Hdr: clock.c 1.18 91/01/21 76 * 77 * @(#)clock.c 8.1 (Berkeley) 6/10/93 78 */ 79 80 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 81 82 __KERNEL_RCSID(0, "$NetBSD: timer.c,v 1.7 2006/06/24 04:00:21 tsutsui Exp $"); 83 84 #include <sys/param.h> 85 #include <sys/kernel.h> 86 #include <sys/systm.h> 87 88 #include <mips/locore.h> 89 90 #include <arc/arc/timervar.h> 91 92 struct device *timerdev; 93 const struct timerfns *timerfns; 94 int timerinitted; 95 uint32_t last_cp0_count; 96 97 #ifdef ENABLE_INT5_STATCLOCK 98 /* 99 * Statistics clock variance, in usec. Variance must be a 100 * power of two. Since this gives us an even number, not an odd number, 101 * we discard one case and compensate. That is, a variance of 1024 would 102 * give us offsets in [0..1023]. Instead, we take offsets in [1..1023]. 103 * This is symmetric about the point 512, or statvar/2, and thus averages 104 * to that value (assuming uniform random numbers). 105 */ 106 static const uint32_t statvar = 1024; 107 static uint32_t statint; /* number of clock ticks for stathz */ 108 static uint32_t statmin; /* minimum stat clock count in ticks */ 109 static uint32_t statprev;/* last value of we set statclock to */ 110 static u_int statcountperusec; /* number of ticks per usec at current stathz */ 111 #endif 112 113 void 114 timerattach(struct device *dev, const struct timerfns *fns) 115 { 116 117 /* 118 * Just bookkeeping. 119 */ 120 121 if (timerfns != NULL) 122 panic("timerattach: multiple timers"); 123 timerdev = dev; 124 timerfns = fns; 125 } 126 127 /* 128 * Machine-dependent clock routines. 129 */ 130 131 /* 132 * Start the real-time and statistics clocks. Leave stathz 0 since there 133 * are no other timers available. 134 */ 135 void 136 cpu_initclocks(void) 137 { 138 139 #ifdef ENABLE_INT5_STATCLOCK 140 if (stathz == 0) 141 stathz = hz; 142 143 if (profhz == 0) 144 profhz = hz * 5; 145 146 setstatclockrate(stathz); 147 #endif 148 149 if (timerfns == NULL) 150 panic("cpu_initclocks: no timer attached"); 151 152 tick = 1000000 / hz; /* number of microseconds between interrupts */ 153 tickfix = 1000000 - (hz * tick); 154 if (tickfix) { 155 int ftp; 156 157 ftp = min(ffs(tickfix), ffs(hz)); 158 tickfix >>= (ftp - 1); 159 tickfixinterval = hz >> (ftp - 1); 160 } 161 162 /* 163 * Get the clock started. 164 */ 165 (*timerfns->tf_init)(timerdev); 166 167 #ifdef ENABLE_INT5_STATCLOCK 168 /* enable interrupts including CPU INT 5 */ 169 _splnone(); 170 #endif 171 } 172 173 /* 174 * We assume newhz is either stathz or profhz, and that neither will 175 * change after being set up above. Could recalculate intervals here 176 * but that would be a drag. 177 */ 178 void 179 setstatclockrate(int newhz) 180 { 181 #ifdef ENABLE_INT5_STATCLOCK 182 uint32_t countpersecond, statvarticks; 183 184 statprev = mips3_cp0_count_read(); 185 186 statint = ((curcpu()->ci_cpu_freq + newhz / 2) / newhz) / 2; 187 188 /* Get the total ticks a second */ 189 countpersecond = statint * newhz; 190 191 /* now work out how many ticks per usec */ 192 statcountperusec = countpersecond / 1000000; 193 194 /* calculate a variance range of statvar */ 195 statvarticks = statcountperusec * statvar; 196 197 /* minimum is statint - 50% of variant */ 198 statmin = statint - (statvarticks / 2); 199 200 mips3_cp0_compare_write(statprev + statint); 201 #endif 202 } 203 204 #ifdef ENABLE_INT5_STATCLOCK 205 void 206 statclockintr(struct clockframe *cfp) 207 { 208 uint32_t curcount, statnext, delta, r; 209 int lost; 210 211 lost = 0; 212 213 do { 214 r = (uint32_t)random() & (statvar - 1); 215 } while (r == 0); 216 statnext = statprev + statmin + (r * statcountperusec); 217 218 mips3_cp0_compare_write(statnext); 219 curcount = mips3_cp0_count_read(); 220 delta = statnext - curcount; 221 222 while (__predict_false((int32_t)delta < 0)) { 223 lost++; 224 delta += statint; 225 } 226 if (__predict_false(lost > 0)) { 227 statnext = curcount + delta; 228 mips3_cp0_compare_write(statnext); 229 for (; lost > 0; lost--) 230 statclock(cfp); 231 } 232 statclock(cfp); 233 234 statprev = statnext; 235 } 236 #endif 237 238 /* 239 * Wait "n" microseconds. 240 */ 241 void 242 delay(unsigned int n) 243 { 244 uint32_t cur, last, delta, usecs; 245 246 last = mips3_cp0_count_read(); 247 delta = usecs = 0; 248 249 while (n > usecs) { 250 cur = mips3_cp0_count_read(); 251 252 /* Check to see if the timer has wrapped around. */ 253 if (cur < last) 254 delta += ((curcpu()->ci_cycles_per_hz - last) + cur); 255 else 256 delta += (cur - last); 257 258 last = cur; 259 260 if (delta >= curcpu()->ci_divisor_delay) { 261 usecs += delta / curcpu()->ci_divisor_delay; 262 delta %= curcpu()->ci_divisor_delay; 263 } 264 } 265 } 266