2 * Copyright (c) 2001 Jake Burkholder.
3 * Copyright (c) 2005, 2008 Marius Strobl <marius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/mutex.h>
38 #include <sys/sched.h>
40 #include <sys/sysctl.h>
41 #include <sys/timetc.h>
43 #include <dev/ofw/openfirm.h>
48 #include <machine/cpu.h>
49 #include <machine/frame.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/smp.h>
52 #include <machine/tick.h>
53 #include <machine/ver.h>
55 /* 10000 ticks proved okay for 500MHz. */
56 #define TICK_GRACE(clock) ((clock) / 1000000 * 2 * 10)
58 #define TICK_QUALITY_MP 10
59 #define TICK_QUALITY_UP 1000
61 SYSCTL_NODE(_machdep, OID_AUTO, tick, CTLFLAG_RD, 0, "tick statistics");
63 static int adjust_edges = 0;
64 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_edges, CTLFLAG_RD, &adjust_edges,
65 0, "total number of times tick interrupts got more than 12.5% behind");
67 static int adjust_excess = 0;
68 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_excess, CTLFLAG_RD, &adjust_excess,
69 0, "total number of ignored tick interrupts");
71 static int adjust_missed = 0;
72 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_missed, CTLFLAG_RD, &adjust_missed,
73 0, "total number of missed tick interrupts");
75 static int adjust_ticks = 0;
76 SYSCTL_INT(_machdep_tick, OID_AUTO, adjust_ticks, CTLFLAG_RD, &adjust_ticks,
77 0, "total number of tick interrupts with adjustment");
79 u_int hardclock_use_stick = 0;
80 SYSCTL_INT(_machdep_tick, OID_AUTO, hardclock_use_stick, CTLFLAG_RD,
81 &hardclock_use_stick, 0, "hardclock uses STICK instead of TICK timer");
83 static struct timecounter stick_tc;
84 static struct timecounter tick_tc;
85 static u_long tick_increment;
87 static uint64_t tick_cputicks(void);
88 static timecounter_get_t stick_get_timecount_up;
90 static timecounter_get_t stick_get_timecount_mp;
92 static timecounter_get_t tick_get_timecount_up;
94 static timecounter_get_t tick_get_timecount_mp;
96 static void tick_hardclock(struct trapframe *tf);
97 static void tick_hardclock_bbwar(struct trapframe *tf);
98 static inline void tick_hardclock_common(struct trapframe *tf, u_long tick,
100 static inline void tick_process(struct trapframe *tf);
101 static void stick_hardclock(struct trapframe *tf);
113 uint32_t clock, sclock;
117 clock = PCPU_GET(clock);
119 if (PCPU_GET(impl) == CPU_IMPL_SPARC64V ||
120 PCPU_GET(impl) >= CPU_IMPL_ULTRASPARCIII) {
121 if (OF_getprop(OF_peer(0), "stick-frequency", &sclock,
122 sizeof(sclock)) == -1) {
123 panic("%s: could not determine STICK frequency",
128 * Given that the STICK timers typically are driven at rather low
129 * frequencies they shouldn't be used except when really necessary.
131 if (hardclock_use_stick != 0) {
132 intr_setup(PIL_TICK, stick_hardclock, -1, NULL, NULL);
134 * We don't provide a CPU ticker as long as the frequency
135 * supplied isn't actually used per-CPU.
138 intr_setup(PIL_TICK, PCPU_GET(impl) >= CPU_IMPL_ULTRASPARCI &&
139 PCPU_GET(impl) < CPU_IMPL_ULTRASPARCIII ?
140 tick_hardclock_bbwar : tick_hardclock, -1, NULL, NULL);
141 set_cputicker(tick_cputicks, clock, 0);
143 tick_increment = clock / hz;
145 * Avoid stopping of hardclock in terms of a lost (S)TICK interrupt
146 * by ensuring that the (S)TICK period is at least TICK_GRACE ticks.
148 if (tick_increment < TICK_GRACE(clock))
149 panic("%s: HZ too high, decrease to at least %d",
150 __func__, clock / TICK_GRACE(clock));
154 * Initialize the (S)TICK-based timecounter(s).
155 * Note that we (try to) sync the (S)TICK timers of APs with the BSP
156 * during their startup but not afterwards. The resulting drift can
157 * cause problems when the time is calculated based on (S)TICK values
158 * read on different CPUs. Thus we always read the register on the
159 * BSP (if necessary via an IPI as sched_bind(9) isn't available in
160 * all circumstances) and use a low quality for the otherwise high
161 * quality (S)TICK timers in the MP case.
163 tick_tc.tc_get_timecount = tick_get_timecount_up;
164 tick_tc.tc_counter_mask = ~0u;
165 tick_tc.tc_frequency = clock;
166 tick_tc.tc_name = "tick";
167 tick_tc.tc_quality = TICK_QUALITY_UP;
169 if (cpu_mp_probe()) {
170 tick_tc.tc_get_timecount = tick_get_timecount_mp;
171 tick_tc.tc_quality = TICK_QUALITY_MP;
176 stick_tc.tc_get_timecount = stick_get_timecount_up;
177 stick_tc.tc_counter_mask = ~0u;
178 stick_tc.tc_frequency = sclock;
179 stick_tc.tc_name = "stick";
180 stick_tc.tc_quality = TICK_QUALITY_UP;
182 if (cpu_mp_probe()) {
183 stick_tc.tc_get_timecount = stick_get_timecount_mp;
184 stick_tc.tc_quality = TICK_QUALITY_MP;
192 tick_process(struct trapframe *tf)
196 hardclock(TRAPF_USERMODE(tf), TRAPF_PC(tf));
198 hardclock_cpu(TRAPF_USERMODE(tf));
200 profclock(TRAPF_USERMODE(tf), TRAPF_PC(tf));
201 statclock(TRAPF_USERMODE(tf));
205 * NB: the sequence of reading the (S)TICK register, calculating the value
206 * of the next tick and writing it to the (S)TICK_COMPARE register must not
207 * be interrupted, not even by an IPI, otherwise a value that is in the past
208 * could be written in the worst case, causing hardclock to stop.
212 tick_hardclock(struct trapframe *tf)
218 adj = PCPU_GET(tickadj);
221 wrtickcmpr(tick + tick_increment - adj, 0);
223 tick_hardclock_common(tf, tick, adj);
228 tick_hardclock_bbwar(struct trapframe *tf)
234 adj = PCPU_GET(tickadj);
237 wrtickcmpr_bbwar(tick + tick_increment - adj, 0);
239 tick_hardclock_common(tf, tick, adj);
244 stick_hardclock(struct trapframe *tf)
250 adj = PCPU_GET(tickadj);
253 wrstickcmpr(stick + tick_increment - adj, 0);
255 tick_hardclock_common(tf, stick, adj);
260 tick_hardclock_common(struct trapframe *tf, u_long tick, u_long adj)
266 ref = PCPU_GET(tickref);
269 while (delta >= tick_increment) {
271 delta -= tick_increment;
272 ref += tick_increment;
278 adjust_missed += count - 1;
279 if (delta > (tick_increment >> 3)) {
282 adj = tick_increment >> 4;
289 PCPU_SET(tickref, ref);
290 PCPU_SET(tickadj, adj);
294 stick_get_timecount_up(struct timecounter *tc)
297 return ((u_int)rdstick());
301 tick_get_timecount_up(struct timecounter *tc)
304 return ((u_int)rd(tick));
309 stick_get_timecount_mp(struct timecounter *tc)
317 ipi_wait(ipi_rd(0, tl_ipi_stick_rd, &stick));
323 tick_get_timecount_mp(struct timecounter *tc)
331 ipi_wait(ipi_rd(0, tl_ipi_tick_rd, &tick));
344 * Try to make the (S)TICK interrupts as synchronously as possible
345 * on all CPUs to avoid inaccuracies for migrating processes. Leave
346 * out one tick to make sure that it is not missed.
349 PCPU_SET(tickadj, 0);
351 if (hardclock_use_stick != 0)
355 base = roundup(base, tick_increment);
356 PCPU_SET(tickref, base);
357 if (hardclock_use_stick != 0)
358 wrstickcmpr(base + tick_increment, 0);
360 wrtickcmpr(base + tick_increment, 0);
366 tick_clear(u_int cpu_impl)
369 if (cpu_impl == CPU_IMPL_SPARC64V ||
370 cpu_impl >= CPU_IMPL_ULTRASPARCIII)
376 tick_stop(u_int cpu_impl)
379 if (cpu_impl == CPU_IMPL_SPARC64V ||
380 cpu_impl >= CPU_IMPL_ULTRASPARCIII)
381 wrstickcmpr(1L << 63, 0);
382 wrtickcmpr(1L << 63, 0);