xref: /netbsd/sys/arch/arc/arc/timer.c (revision 6550d01e)
1 /* $NetBSD: timer.c,v 1.9 2006/09/24 02:20:48 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.9 2006/09/24 02:20:48 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 #include <mips/mips3_clock.h>
90 
91 #include <arc/arc/timervar.h>
92 
93 struct device *timerdev;
94 const struct timerfns *timerfns;
95 int timerinitted;
96 uint32_t last_cp0_count;
97 
98 #ifdef ENABLE_INT5_STATCLOCK
99 /*
100  * Statistics clock variance, in usec.  Variance must be a
101  * power of two.  Since this gives us an even number, not an odd number,
102  * we discard one case and compensate.  That is, a variance of 1024 would
103  * give us offsets in [0..1023].  Instead, we take offsets in [1..1023].
104  * This is symmetric about the point 512, or statvar/2, and thus averages
105  * to that value (assuming uniform random numbers).
106  */
107 static const uint32_t statvar = 1024;
108 static uint32_t statint;	/* number of clock ticks for stathz */
109 static uint32_t statmin;	/* minimum stat clock count in ticks */
110 static uint32_t statprev;/* last value of we set statclock to */
111 static u_int statcountperusec;	/* number of ticks per usec at current stathz */
112 #endif
113 
114 void
115 timerattach(struct device *dev, const struct timerfns *fns)
116 {
117 
118 	/*
119 	 * Just bookkeeping.
120 	 */
121 
122 	if (timerfns != NULL)
123 		panic("timerattach: multiple timers");
124 	timerdev = dev;
125 	timerfns = fns;
126 }
127 
128 /*
129  * Machine-dependent clock routines.
130  */
131 
132 /*
133  * Start the real-time and statistics clocks. Leave stathz 0 since there
134  * are no other timers available.
135  */
136 void
137 cpu_initclocks(void)
138 {
139 
140 #ifdef ENABLE_INT5_STATCLOCK
141 	if (stathz == 0)
142 		stathz = hz;
143 
144 	if (profhz == 0)
145 		profhz = hz * 5;
146 
147 	setstatclockrate(stathz);
148 #endif
149 
150 	if (timerfns == NULL)
151 		panic("cpu_initclocks: no timer attached");
152 
153 	/*
154 	 * Get the clock started.
155 	 */
156 	(*timerfns->tf_init)(timerdev);
157 
158 	/* init timecounter */
159 	mips3_init_tc();
160 
161 #ifdef ENABLE_INT5_STATCLOCK
162 	/* enable interrupts including CPU INT 5 */
163 	_splnone();
164 #endif
165 }
166 
167 /*
168  * We assume newhz is either stathz or profhz, and that neither will
169  * change after being set up above.  Could recalculate intervals here
170  * but that would be a drag.
171  */
172 void
173 setstatclockrate(int newhz)
174 {
175 #ifdef ENABLE_INT5_STATCLOCK
176 	uint32_t countpersecond, statvarticks;
177 
178 	statprev = mips3_cp0_count_read();
179 
180 	statint = ((curcpu()->ci_cpu_freq + newhz / 2) / newhz) / 2;
181 
182 	/* Get the total ticks a second */
183 	countpersecond = statint * newhz;
184 
185 	/* now work out how many ticks per usec */
186 	statcountperusec = countpersecond / 1000000;
187 
188 	/* calculate a variance range of statvar */
189 	statvarticks = statcountperusec * statvar;
190 
191 	/* minimum is statint - 50% of variant */
192 	statmin = statint - (statvarticks / 2);
193 
194 	mips3_cp0_compare_write(statprev + statint);
195 #endif
196 }
197 
198 #ifdef ENABLE_INT5_STATCLOCK
199 void
200 statclockintr(struct clockframe *cfp)
201 {
202 	uint32_t curcount, statnext, delta, r;
203 	int lost;
204 
205 	lost = 0;
206 
207 	do {
208 		r = (uint32_t)random() & (statvar - 1);
209 	} while (r == 0);
210 	statnext = statprev + statmin + (r * statcountperusec);
211 
212 	mips3_cp0_compare_write(statnext);
213 	curcount = mips3_cp0_count_read();
214 	delta = statnext - curcount;
215 
216 	while (__predict_false((int32_t)delta < 0)) {
217 		lost++;
218 		delta += statint;
219 	}
220 	if (__predict_false(lost > 0)) {
221 		statnext = curcount + delta;
222 		mips3_cp0_compare_write(statnext);
223 		for (; lost > 0; lost--)
224 			statclock(cfp);
225 	}
226 	statclock(cfp);
227 
228 	statprev = statnext;
229 }
230 #endif
231