]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/sparc64/sparc64/tick.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / sparc64 / sparc64 / tick.c
1 /*-
2  * Copyright (c) 2001 Jake Burkholder.
3  * Copyright (c) 2005, 2008 Marius Strobl <marius@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/pcpu.h>
37 #include <sys/proc.h>
38 #include <sys/sched.h>
39 #include <sys/smp.h>
40 #include <sys/sysctl.h>
41 #include <sys/timetc.h>
42
43 #include <dev/ofw/openfirm.h>
44
45 #include <machine/cpu.h>
46 #include <machine/frame.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/tick.h>
49 #include <machine/ver.h>
50
51 /* 10000 ticks proved okay for 500MHz. */
52 #define TICK_GRACE(clock)       ((clock) / 1000000 * 2 * 10)
53
54 #define TICK_QUALITY_MP 10
55 #define TICK_QUALITY_UP 1000
56
57 SYSCTL_NODE(_machdep, OID_AUTO, tick, CTLFLAG_RD, 0, "tick statistics");
58
59 static int adjust_edges = 0;
60 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_edges, CTLFLAG_RD, &adjust_edges,
61     0, "total number of times tick interrupts got more than 12.5% behind");
62
63 static int adjust_excess = 0;
64 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_excess, CTLFLAG_RD, &adjust_excess,
65     0, "total number of ignored tick interrupts");
66
67 static int adjust_missed = 0;
68 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_missed, CTLFLAG_RD, &adjust_missed,
69     0, "total number of missed tick interrupts");
70
71 static int adjust_ticks = 0;
72 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_ticks, CTLFLAG_RD, &adjust_ticks,
73     0, "total number of tick interrupts with adjustment");
74
75 u_int hardclock_use_stick = 0;
76 SYSCTL_INT(_machdep_tick, OID_AUTO, hardclock_use_stick, CTLFLAG_RD,
77     &hardclock_use_stick, 0, "hardclock uses STICK instead of TICK timer");
78
79 static struct timecounter tick_tc;
80 static u_long tick_increment;
81
82 static uint64_t tick_cputicks(void);
83 static timecounter_get_t tick_get_timecount_up;
84 #ifdef SMP
85 static timecounter_get_t tick_get_timecount_mp;
86 #endif
87 static void tick_hardclock(struct trapframe *tf);
88 static void tick_hardclock_bbwar(struct trapframe *tf);
89 static inline void tick_hardclock_common(struct trapframe *tf, u_long tick,
90     u_long adj);
91 static inline void tick_process(struct trapframe *tf);
92 static void stick_hardclock(struct trapframe *tf);
93
94 static uint64_t
95 tick_cputicks(void)
96 {
97
98         return (rd(tick));
99 }
100
101 void
102 cpu_initclocks(void)
103 {
104         uint32_t clock;
105
106         stathz = hz;
107
108         /*
109          * Given that the STICK timers typically are driven at rather low
110          * frequencies they shouldn't be used except when really necessary.
111          */
112         if (hardclock_use_stick != 0) {
113                 if (OF_getprop(OF_parent(PCPU_GET(node)), "stick-frequency",
114                     &clock, sizeof(clock)) == -1)
115                 panic("%s: could not determine STICK frequency", __func__);
116                 intr_setup(PIL_TICK, stick_hardclock, -1, NULL, NULL);
117                 /*
118                  * We don't provide a CPU ticker as long as the frequency
119                  * supplied isn't actually used per-CPU.
120                  */
121         } else {
122                 clock = PCPU_GET(clock);
123                 intr_setup(PIL_TICK, cpu_impl < CPU_IMPL_ULTRASPARCIII ?
124                     tick_hardclock_bbwar : tick_hardclock, -1, NULL, NULL);
125                 set_cputicker(tick_cputicks, clock, 0);
126         }
127         tick_increment = clock / hz;
128         /*
129          * Avoid stopping of hardclock in terms of a lost (S)TICK interrupt
130          * by ensuring that the (S)TICK period is at least TICK_GRACE ticks.
131          */
132         if (tick_increment < TICK_GRACE(clock))
133                 panic("%s: HZ too high, decrease to at least %d",
134                     __func__, clock / TICK_GRACE(clock));
135         tick_start();
136
137         /*
138          * Initialize the TICK-based timecounter.  This must not happen
139          * before SI_SUB_INTRINSIC for tick_get_timecount_mp() to work.
140          */
141         tick_tc.tc_get_timecount = tick_get_timecount_up;
142         tick_tc.tc_poll_pps = NULL;
143         tick_tc.tc_counter_mask = ~0u;
144         tick_tc.tc_frequency = PCPU_GET(clock);
145         tick_tc.tc_name = "tick";
146         tick_tc.tc_quality = TICK_QUALITY_UP;
147         tick_tc.tc_priv = NULL;
148 #ifdef SMP
149         /*
150          * We (try to) sync the (S)TICK timers of APs with the BSP during
151          * their startup but not afterwards.  The resulting drift can
152          * cause problems when the time is calculated based on (S)TICK
153          * values read on different CPUs.  Thus we bind to the BSP for
154          * reading the register and use a low quality for the otherwise
155          * high quality (S)TICK timers in the MP case.
156          */
157         if (cpu_mp_probe()) {
158                 tick_tc.tc_get_timecount = tick_get_timecount_mp;
159                 tick_tc.tc_quality = TICK_QUALITY_MP;
160         }
161 #endif
162         tc_init(&tick_tc);
163 }
164
165 static inline void
166 tick_process(struct trapframe *tf)
167 {
168
169         if (curcpu == 0)
170                 hardclock(TRAPF_USERMODE(tf), TRAPF_PC(tf));
171         else
172                 hardclock_cpu(TRAPF_USERMODE(tf));
173         if (profprocs != 0)
174                 profclock(TRAPF_USERMODE(tf), TRAPF_PC(tf));
175         statclock(TRAPF_USERMODE(tf));
176 }
177
178 /*
179  * NB: the sequence of reading the (S)TICK register, calculating the value
180  * of the next tick and writing it to the (S)TICK_COMPARE register must not
181  * be interrupted, not even by an IPI, otherwise a value that is in the past
182  * could be written in the worst case, causing hardclock to stop.
183  */
184
185 static void
186 tick_hardclock(struct trapframe *tf)
187 {
188         u_long adj, tick;
189         register_t s;
190
191         critical_enter();
192         adj = PCPU_GET(tickadj);
193         s = intr_disable();
194         tick = rd(tick);
195         wr(tick_cmpr, tick + tick_increment - adj, 0);
196         intr_restore(s);
197         tick_hardclock_common(tf, tick, adj);
198         critical_exit();
199 }
200
201 static void
202 tick_hardclock_bbwar(struct trapframe *tf)
203 {
204         u_long adj, tick;
205         register_t s;
206
207         critical_enter();
208         adj = PCPU_GET(tickadj);
209         s = intr_disable();
210         tick = rd(tick);
211         wrtickcmpr(tick + tick_increment - adj, 0);
212         intr_restore(s);
213         tick_hardclock_common(tf, tick, adj);
214         critical_exit();
215 }
216
217 static void
218 stick_hardclock(struct trapframe *tf)
219 {
220         u_long adj, stick;
221         register_t s;
222
223         critical_enter();
224         adj = PCPU_GET(tickadj);
225         s = intr_disable();
226         stick = rdstick();
227         wrstickcmpr(stick + tick_increment - adj, 0);
228         intr_restore(s);
229         tick_hardclock_common(tf, stick, adj);
230         critical_exit();
231 }
232
233 static inline void
234 tick_hardclock_common(struct trapframe *tf, u_long tick, u_long adj)
235 {
236         u_long ref;
237         long delta;
238         int count;
239
240         ref = PCPU_GET(tickref);
241         delta = tick - ref;
242         count = 0;
243         while (delta >= tick_increment) {
244                 tick_process(tf);
245                 delta -= tick_increment;
246                 ref += tick_increment;
247                 if (adj != 0)
248                         adjust_ticks++;
249                 count++;
250         }
251         if (count > 0) {
252                 adjust_missed += count - 1;
253                 if (delta > (tick_increment >> 3)) {
254                         if (adj == 0)
255                                 adjust_edges++;
256                         adj = tick_increment >> 4;
257                 } else
258                         adj = 0;
259         } else {
260                 adj = 0;
261                 adjust_excess++;
262         }
263         PCPU_SET(tickref, ref);
264         PCPU_SET(tickadj, adj);
265 }
266
267 static u_int
268 tick_get_timecount_up(struct timecounter *tc)
269 {
270
271         return ((u_int)rd(tick));
272 }
273
274 #ifdef SMP
275 static u_int
276 tick_get_timecount_mp(struct timecounter *tc)
277 {
278         struct thread *td;
279         u_int tick;
280
281         td = curthread;
282         thread_lock(td);
283         sched_bind(td, 0);
284         thread_unlock(td);
285
286         tick = tick_get_timecount_up(tc);
287
288         thread_lock(td);
289         sched_unbind(td);
290         thread_unlock(td);
291
292         return (tick);
293 }
294 #endif
295
296 void
297 tick_start(void)
298 {
299         u_long base;
300         register_t s;
301
302         /*
303          * Try to make the (S)TICK interrupts as synchronously as possible
304          * on all CPUs to avoid inaccuracies for migrating processes.  Leave
305          * out one tick to make sure that it is not missed.
306          */
307         critical_enter();
308         PCPU_SET(tickadj, 0);
309         s = intr_disable();
310         if (hardclock_use_stick != 0)
311                 base = rdstick();
312         else
313                 base = rd(tick);
314         base = roundup(base, tick_increment);
315         PCPU_SET(tickref, base);
316         if (hardclock_use_stick != 0)
317                 wrstickcmpr(base + tick_increment, 0);
318         else
319                 wrtickcmpr(base + tick_increment, 0);
320         intr_restore(s);
321         critical_exit();
322 }
323
324 void
325 tick_clear(void)
326 {
327
328         if (cpu_impl >= CPU_IMPL_ULTRASPARCIII)
329                 wrstick(0, 0);
330         wrpr(tick, 0, 0);
331 }
332
333 void
334 tick_stop(void)
335 {
336
337         if (cpu_impl >= CPU_IMPL_ULTRASPARCIII)
338                 wrstickcmpr(1L << 63, 0);
339         wrtickcmpr(1L << 63, 0);
340 }