xref: /freebsd/sys/kern/kern_clocksource.c (revision dd9595e7fa6cb5054f14ebc5fdd2f0a77939b58e)
143fe7d45SAlexander Motin /*-
243fe7d45SAlexander Motin  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
343fe7d45SAlexander Motin  * All rights reserved.
443fe7d45SAlexander Motin  *
543fe7d45SAlexander Motin  * Redistribution and use in source and binary forms, with or without
643fe7d45SAlexander Motin  * modification, are permitted provided that the following conditions
743fe7d45SAlexander Motin  * are met:
843fe7d45SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
943fe7d45SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
1043fe7d45SAlexander Motin  *    without modification, immediately at the beginning of the file.
1143fe7d45SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1243fe7d45SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1343fe7d45SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1443fe7d45SAlexander Motin  *
1543fe7d45SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1643fe7d45SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1743fe7d45SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1843fe7d45SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1943fe7d45SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2043fe7d45SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2143fe7d45SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2243fe7d45SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2343fe7d45SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2443fe7d45SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2543fe7d45SAlexander Motin  */
2643fe7d45SAlexander Motin 
2743fe7d45SAlexander Motin #include <sys/cdefs.h>
2843fe7d45SAlexander Motin __FBSDID("$FreeBSD$");
2943fe7d45SAlexander Motin 
3043fe7d45SAlexander Motin /*
3143fe7d45SAlexander Motin  * Common routines to manage event timers hardware.
3243fe7d45SAlexander Motin  */
3343fe7d45SAlexander Motin 
3443fe7d45SAlexander Motin /* XEN has own timer routines now. */
3543fe7d45SAlexander Motin #ifndef XEN
3643fe7d45SAlexander Motin 
3743fe7d45SAlexander Motin #include "opt_kdtrace.h"
3843fe7d45SAlexander Motin 
3943fe7d45SAlexander Motin #include <sys/param.h>
4043fe7d45SAlexander Motin #include <sys/systm.h>
4143fe7d45SAlexander Motin #include <sys/bus.h>
4243fe7d45SAlexander Motin #include <sys/lock.h>
4343fe7d45SAlexander Motin #include <sys/kdb.h>
44a157e425SAlexander Motin #include <sys/ktr.h>
4543fe7d45SAlexander Motin #include <sys/mutex.h>
4643fe7d45SAlexander Motin #include <sys/proc.h>
4743fe7d45SAlexander Motin #include <sys/kernel.h>
4843fe7d45SAlexander Motin #include <sys/sched.h>
4943fe7d45SAlexander Motin #include <sys/smp.h>
5043fe7d45SAlexander Motin #include <sys/sysctl.h>
5143fe7d45SAlexander Motin #include <sys/timeet.h>
5243fe7d45SAlexander Motin 
5343fe7d45SAlexander Motin #include <machine/atomic.h>
5443fe7d45SAlexander Motin #include <machine/clock.h>
5543fe7d45SAlexander Motin #include <machine/cpu.h>
5643fe7d45SAlexander Motin #include <machine/smp.h>
5743fe7d45SAlexander Motin 
5843fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS
5943fe7d45SAlexander Motin #include <sys/dtrace_bsd.h>
6043fe7d45SAlexander Motin cyclic_clock_func_t	cyclic_clock_func[MAXCPU];
6143fe7d45SAlexander Motin #endif
6243fe7d45SAlexander Motin 
63a157e425SAlexander Motin int			cpu_disable_deep_sleep = 0; /* Timer dies in C3. */
6443fe7d45SAlexander Motin 
65a157e425SAlexander Motin static void		setuptimer(void);
66a157e425SAlexander Motin static void		loadtimer(struct bintime *now, int first);
67a157e425SAlexander Motin static int		doconfigtimer(void);
68a157e425SAlexander Motin static void		configtimer(int start);
69a157e425SAlexander Motin static int		round_freq(struct eventtimer *et, int freq);
7043fe7d45SAlexander Motin 
71a157e425SAlexander Motin static void		getnextcpuevent(struct bintime *event, int idle);
72a157e425SAlexander Motin static void		getnextevent(struct bintime *event);
73a157e425SAlexander Motin static int		handleevents(struct bintime *now, int fake);
74a157e425SAlexander Motin #ifdef SMP
75a157e425SAlexander Motin static void		cpu_new_callout(int cpu, int ticks);
76a157e425SAlexander Motin #endif
7743fe7d45SAlexander Motin 
78a157e425SAlexander Motin static struct mtx	et_hw_mtx;
79a157e425SAlexander Motin 
80a157e425SAlexander Motin #define	ET_HW_LOCK(state)						\
81a157e425SAlexander Motin 	{								\
82a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
83a157e425SAlexander Motin 			mtx_lock_spin(&(state)->et_hw_mtx);		\
84a157e425SAlexander Motin 		else							\
85a157e425SAlexander Motin 			mtx_lock_spin(&et_hw_mtx);			\
86a157e425SAlexander Motin 	}
87a157e425SAlexander Motin 
88a157e425SAlexander Motin #define	ET_HW_UNLOCK(state)						\
89a157e425SAlexander Motin 	{								\
90a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
91a157e425SAlexander Motin 			mtx_unlock_spin(&(state)->et_hw_mtx);		\
92a157e425SAlexander Motin 		else							\
93a157e425SAlexander Motin 			mtx_unlock_spin(&et_hw_mtx);			\
94a157e425SAlexander Motin 	}
95a157e425SAlexander Motin 
96a157e425SAlexander Motin static struct eventtimer *timer = NULL;
97a157e425SAlexander Motin static struct bintime	timerperiod;	/* Timer period for periodic mode. */
98a157e425SAlexander Motin static struct bintime	hardperiod;	/* hardclock() events period. */
99a157e425SAlexander Motin static struct bintime	statperiod;	/* statclock() events period. */
100a157e425SAlexander Motin static struct bintime	profperiod;	/* profclock() events period. */
101a157e425SAlexander Motin static struct bintime	nexttick;	/* Next global timer tick time. */
102a157e425SAlexander Motin static u_int		busy = 0;	/* Reconfiguration is in progress. */
103a157e425SAlexander Motin static int		profiling = 0;	/* Profiling events enabled. */
104a157e425SAlexander Motin 
105a157e425SAlexander Motin static char		timername[32];	/* Wanted timer. */
106a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername));
107a157e425SAlexander Motin 
108*dd9595e7SAlexander Motin static int		singlemul = 0;	/* Multiplier for periodic mode. */
10943fe7d45SAlexander Motin TUNABLE_INT("kern.eventtimer.singlemul", &singlemul);
11043fe7d45SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RW, &singlemul,
111a157e425SAlexander Motin     0, "Multiplier for periodic mode");
11243fe7d45SAlexander Motin 
113a157e425SAlexander Motin static u_int		idletick = 0;	/* Idle mode allowed. */
114a157e425SAlexander Motin TUNABLE_INT("kern.eventtimer.idletick", &idletick);
115a157e425SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RW, &idletick,
116a157e425SAlexander Motin     0, "Run periodic events when idle");
117a157e425SAlexander Motin 
118a157e425SAlexander Motin static int		periodic = 0;	/* Periodic or one-shot mode. */
119a157e425SAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &periodic);
120a157e425SAlexander Motin 
121a157e425SAlexander Motin struct pcpu_state {
122a157e425SAlexander Motin 	struct mtx	et_hw_mtx;	/* Per-CPU timer mutex. */
123a157e425SAlexander Motin 	u_int		action;		/* Reconfiguration requests. */
124a157e425SAlexander Motin 	u_int		handle;		/* Immediate handle resuests. */
125a157e425SAlexander Motin 	struct bintime	now;		/* Last tick time. */
126a157e425SAlexander Motin 	struct bintime	nextevent;	/* Next scheduled event on this CPU. */
127a157e425SAlexander Motin 	struct bintime	nexttick;	/* Next timer tick time. */
128a157e425SAlexander Motin 	struct bintime	nexthard;	/* Next hardlock() event. */
129a157e425SAlexander Motin 	struct bintime	nextstat;	/* Next statclock() event. */
130a157e425SAlexander Motin 	struct bintime	nextprof;	/* Next profclock() event. */
131a157e425SAlexander Motin 	int		ipi;		/* This CPU needs IPI. */
132a157e425SAlexander Motin 	int		idle;		/* This CPU is in idle mode. */
133a157e425SAlexander Motin };
134a157e425SAlexander Motin 
135a157e425SAlexander Motin static DPCPU_DEFINE(struct pcpu_state, timerstate);
13643fe7d45SAlexander Motin 
13743fe7d45SAlexander Motin #define FREQ2BT(freq, bt)						\
13843fe7d45SAlexander Motin {									\
13943fe7d45SAlexander Motin 	(bt)->sec = 0;							\
14043fe7d45SAlexander Motin 	(bt)->frac = ((uint64_t)0x8000000000000000  / (freq)) << 1;	\
14143fe7d45SAlexander Motin }
14251636352SAlexander Motin #define BT2FREQ(bt)							\
14351636352SAlexander Motin 	(((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) /		\
14451636352SAlexander Motin 	    ((bt)->frac >> 1))
14543fe7d45SAlexander Motin 
146a157e425SAlexander Motin /*
147a157e425SAlexander Motin  * Timer broadcast IPI handler.
148a157e425SAlexander Motin  */
149a157e425SAlexander Motin int
150a157e425SAlexander Motin hardclockintr(void)
15143fe7d45SAlexander Motin {
152a157e425SAlexander Motin 	struct bintime now;
153a157e425SAlexander Motin 	struct pcpu_state *state;
154a157e425SAlexander Motin 	int done;
15543fe7d45SAlexander Motin 
156a157e425SAlexander Motin 	if (doconfigtimer() || busy)
157a157e425SAlexander Motin 		return (FILTER_HANDLED);
158a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
159a157e425SAlexander Motin 	now = state->now;
160a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "ipi  at %d:    now  %d.%08x%08x",
161a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
162a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
163a157e425SAlexander Motin 	done = handleevents(&now, 0);
164a157e425SAlexander Motin 	return (done ? FILTER_HANDLED : FILTER_STRAY);
165a157e425SAlexander Motin }
166a157e425SAlexander Motin 
167a157e425SAlexander Motin /*
168a157e425SAlexander Motin  * Handle all events for specified time on this CPU
169a157e425SAlexander Motin  */
170a157e425SAlexander Motin static int
171a157e425SAlexander Motin handleevents(struct bintime *now, int fake)
172a157e425SAlexander Motin {
173a157e425SAlexander Motin 	struct bintime t;
174a157e425SAlexander Motin 	struct trapframe *frame;
175a157e425SAlexander Motin 	struct pcpu_state *state;
176a157e425SAlexander Motin 	uintfptr_t pc;
177a157e425SAlexander Motin 	int usermode;
178a157e425SAlexander Motin 	int done, runs;
179a157e425SAlexander Motin 
180a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "handle at %d:  now  %d.%08x%08x",
181a157e425SAlexander Motin 	    curcpu, now->sec, (unsigned int)(now->frac >> 32),
182a157e425SAlexander Motin 		     (unsigned int)(now->frac & 0xffffffff));
183a157e425SAlexander Motin 	done = 0;
184a157e425SAlexander Motin 	if (fake) {
185a157e425SAlexander Motin 		frame = NULL;
186a157e425SAlexander Motin 		usermode = 0;
187a157e425SAlexander Motin 		pc = 0;
188a157e425SAlexander Motin 	} else {
189a157e425SAlexander Motin 		frame = curthread->td_intr_frame;
190a157e425SAlexander Motin 		usermode = TRAPF_USERMODE(frame);
191a157e425SAlexander Motin 		pc = TRAPF_PC(frame);
192a157e425SAlexander Motin 	}
19343fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS
19443fe7d45SAlexander Motin 	/*
19543fe7d45SAlexander Motin 	 * If the DTrace hooks are configured and a callback function
19643fe7d45SAlexander Motin 	 * has been registered, then call it to process the high speed
19743fe7d45SAlexander Motin 	 * timers.
19843fe7d45SAlexander Motin 	 */
199a157e425SAlexander Motin 	if (!fake && cyclic_clock_func[curcpu] != NULL)
200a157e425SAlexander Motin 		(*cyclic_clock_func[curcpu])(frame);
20143fe7d45SAlexander Motin #endif
202a157e425SAlexander Motin 	runs = 0;
203a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
204a157e425SAlexander Motin 	while (bintime_cmp(now, &state->nexthard, >=)) {
205a157e425SAlexander Motin 		bintime_add(&state->nexthard, &hardperiod);
206a157e425SAlexander Motin 		runs++;
20743fe7d45SAlexander Motin 	}
208a157e425SAlexander Motin 	if (runs) {
209a157e425SAlexander Motin 		hardclock_anycpu(runs, usermode);
210a157e425SAlexander Motin 		done = 1;
21143fe7d45SAlexander Motin 	}
212a157e425SAlexander Motin 	while (bintime_cmp(now, &state->nextstat, >=)) {
213a157e425SAlexander Motin 		statclock(usermode);
214a157e425SAlexander Motin 		bintime_add(&state->nextstat, &statperiod);
215a157e425SAlexander Motin 		done = 1;
21643fe7d45SAlexander Motin 	}
217a157e425SAlexander Motin 	if (profiling) {
218a157e425SAlexander Motin 		while (bintime_cmp(now, &state->nextprof, >=)) {
219a157e425SAlexander Motin 			if (!fake)
220a157e425SAlexander Motin 				profclock(usermode, pc);
221a157e425SAlexander Motin 			bintime_add(&state->nextprof, &profperiod);
222a157e425SAlexander Motin 			done = 1;
22343fe7d45SAlexander Motin 		}
224a157e425SAlexander Motin 	} else
225a157e425SAlexander Motin 		state->nextprof = state->nextstat;
226a157e425SAlexander Motin 	getnextcpuevent(&t, 0);
227a157e425SAlexander Motin 	ET_HW_LOCK(state);
228a157e425SAlexander Motin 	if (!busy) {
229a157e425SAlexander Motin 		state->idle = 0;
230a157e425SAlexander Motin 		state->nextevent = t;
231a157e425SAlexander Motin 		loadtimer(now, 0);
23243fe7d45SAlexander Motin 	}
233a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
234a157e425SAlexander Motin 	return (done);
23543fe7d45SAlexander Motin }
23643fe7d45SAlexander Motin 
23743fe7d45SAlexander Motin /*
238a157e425SAlexander Motin  * Schedule binuptime of the next event on current CPU.
23943fe7d45SAlexander Motin  */
24043fe7d45SAlexander Motin static void
241a157e425SAlexander Motin getnextcpuevent(struct bintime *event, int idle)
24243fe7d45SAlexander Motin {
243a157e425SAlexander Motin 	struct bintime tmp;
244a157e425SAlexander Motin 	struct pcpu_state *state;
245a157e425SAlexander Motin 	int skip;
24643fe7d45SAlexander Motin 
247a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
248a157e425SAlexander Motin 	*event = state->nexthard;
249a157e425SAlexander Motin 	if (idle) { /* If CPU is idle - ask callouts for how long. */
250a157e425SAlexander Motin 		skip = callout_tickstofirst() - 1;
251a157e425SAlexander Motin 		CTR2(KTR_SPARE2, "skip   at %d: %d", curcpu, skip);
252a157e425SAlexander Motin 		tmp = hardperiod;
253a157e425SAlexander Motin 		bintime_mul(&tmp, skip);
254a157e425SAlexander Motin 		bintime_add(event, &tmp);
255a157e425SAlexander Motin 	} else { /* If CPU is active - handle all types of events. */
256a157e425SAlexander Motin 		if (bintime_cmp(event, &state->nextstat, >))
257a157e425SAlexander Motin 			*event = state->nextstat;
258a157e425SAlexander Motin 		if (profiling &&
259a157e425SAlexander Motin 		    bintime_cmp(event, &state->nextprof, >))
260a157e425SAlexander Motin 			*event = state->nextprof;
26143fe7d45SAlexander Motin 	}
26243fe7d45SAlexander Motin }
263a157e425SAlexander Motin 
264a157e425SAlexander Motin /*
265a157e425SAlexander Motin  * Schedule binuptime of the next event on all CPUs.
266a157e425SAlexander Motin  */
267a157e425SAlexander Motin static void
268a157e425SAlexander Motin getnextevent(struct bintime *event)
269a157e425SAlexander Motin {
270a157e425SAlexander Motin 	struct pcpu_state *state;
271a157e425SAlexander Motin #ifdef SMP
272a157e425SAlexander Motin 	int	cpu;
273a157e425SAlexander Motin #endif
274a157e425SAlexander Motin 	int	c;
275a157e425SAlexander Motin 
276a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
277a157e425SAlexander Motin 	*event = state->nextevent;
278a157e425SAlexander Motin 	c = curcpu;
279a157e425SAlexander Motin #ifdef SMP
280a157e425SAlexander Motin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) {
281a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
282a157e425SAlexander Motin 			if (curcpu == cpu)
283a157e425SAlexander Motin 				continue;
284a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
285a157e425SAlexander Motin 			if (bintime_cmp(event, &state->nextevent, >)) {
286a157e425SAlexander Motin 				*event = state->nextevent;
287a157e425SAlexander Motin 				c = cpu;
28843fe7d45SAlexander Motin 			}
28943fe7d45SAlexander Motin 		}
290a157e425SAlexander Motin 	}
291a157e425SAlexander Motin #endif
292a157e425SAlexander Motin 	CTR5(KTR_SPARE2, "next at %d:    next %d.%08x%08x by %d",
293a157e425SAlexander Motin 	    curcpu, event->sec, (unsigned int)(event->frac >> 32),
294a157e425SAlexander Motin 			     (unsigned int)(event->frac & 0xffffffff), c);
295a157e425SAlexander Motin }
296a157e425SAlexander Motin 
297a157e425SAlexander Motin /* Hardware timer callback function. */
298a157e425SAlexander Motin static void
299a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg)
300a157e425SAlexander Motin {
301a157e425SAlexander Motin 	struct bintime now;
302a157e425SAlexander Motin 	struct bintime *next;
303a157e425SAlexander Motin 	struct pcpu_state *state;
304a157e425SAlexander Motin #ifdef SMP
305a157e425SAlexander Motin 	int cpu, bcast;
306a157e425SAlexander Motin #endif
307a157e425SAlexander Motin 
308a157e425SAlexander Motin 	/* Do not touch anything if somebody reconfiguring timers. */
309a157e425SAlexander Motin 	if (busy)
310a157e425SAlexander Motin 		return;
311a157e425SAlexander Motin 	/* Update present and next tick times. */
312a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
313a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_PERCPU) {
314a157e425SAlexander Motin 		next = &state->nexttick;
315a157e425SAlexander Motin 	} else
316a157e425SAlexander Motin 		next = &nexttick;
317a157e425SAlexander Motin 	if (periodic) {
318a157e425SAlexander Motin 		now = *next;	/* Ex-next tick time becomes present time. */
319a157e425SAlexander Motin 		bintime_add(next, &timerperiod); /* Next tick in 1 period. */
320a157e425SAlexander Motin 	} else {
321a157e425SAlexander Motin 		binuptime(&now);	/* Get present time from hardware. */
322a157e425SAlexander Motin 		next->sec = -1;		/* Next tick is not scheduled yet. */
323a157e425SAlexander Motin 	}
324a157e425SAlexander Motin 	state->now = now;
325a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "intr at %d:    now  %d.%08x%08x",
326a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
327a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
328a157e425SAlexander Motin 
329a157e425SAlexander Motin #ifdef SMP
330a157e425SAlexander Motin 	/* Prepare broadcasting to other CPUs for non-per-CPU timers. */
331a157e425SAlexander Motin 	bcast = 0;
332a157e425SAlexander Motin 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) {
333a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
334a157e425SAlexander Motin 			if (curcpu == cpu)
335a157e425SAlexander Motin 				continue;
336a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
337a157e425SAlexander Motin 			ET_HW_LOCK(state);
338a157e425SAlexander Motin 			state->now = now;
339a157e425SAlexander Motin 			if (bintime_cmp(&now, &state->nextevent, >=)) {
340a157e425SAlexander Motin 				state->nextevent.sec++;
341a157e425SAlexander Motin 				state->ipi = 1;
342a157e425SAlexander Motin 				bcast = 1;
343a157e425SAlexander Motin 			}
344a157e425SAlexander Motin 			ET_HW_UNLOCK(state);
345a157e425SAlexander Motin 		}
346a157e425SAlexander Motin 	}
347a157e425SAlexander Motin #endif
348a157e425SAlexander Motin 
349a157e425SAlexander Motin 	/* Handle events for this time on this CPU. */
350a157e425SAlexander Motin 	handleevents(&now, 0);
351a157e425SAlexander Motin 
352a157e425SAlexander Motin #ifdef SMP
353a157e425SAlexander Motin 	/* Broadcast interrupt to other CPUs for non-per-CPU timers. */
354a157e425SAlexander Motin 	if (bcast) {
355a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
356a157e425SAlexander Motin 			if (curcpu == cpu)
357a157e425SAlexander Motin 				continue;
358a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
359a157e425SAlexander Motin 			if (state->ipi) {
360a157e425SAlexander Motin 				state->ipi = 0;
361a157e425SAlexander Motin 				ipi_cpu(cpu, IPI_HARDCLOCK);
362a157e425SAlexander Motin 			}
363a157e425SAlexander Motin 		}
364a157e425SAlexander Motin 	}
365a157e425SAlexander Motin #endif
366a157e425SAlexander Motin }
367a157e425SAlexander Motin 
368a157e425SAlexander Motin /*
369a157e425SAlexander Motin  * Load new value into hardware timer.
370a157e425SAlexander Motin  */
371a157e425SAlexander Motin static void
372a157e425SAlexander Motin loadtimer(struct bintime *now, int start)
373a157e425SAlexander Motin {
374a157e425SAlexander Motin 	struct pcpu_state *state;
375a157e425SAlexander Motin 	struct bintime new;
376a157e425SAlexander Motin 	struct bintime *next;
377a157e425SAlexander Motin 	uint64_t tmp;
378a157e425SAlexander Motin 	int eq;
379a157e425SAlexander Motin 
380a157e425SAlexander Motin 	if (periodic) {
381a157e425SAlexander Motin 		if (start) {
382a157e425SAlexander Motin 			/*
383a157e425SAlexander Motin 			 * Try to start all periodic timers aligned
384a157e425SAlexander Motin 			 * to period to make events synchronous.
385a157e425SAlexander Motin 			 */
386a157e425SAlexander Motin 			tmp = ((uint64_t)now->sec << 36) + (now->frac >> 28);
387a157e425SAlexander Motin 			tmp = (tmp % (timerperiod.frac >> 28)) << 28;
388a157e425SAlexander Motin 			tmp = timerperiod.frac - tmp;
389a157e425SAlexander Motin 			new = timerperiod;
390a157e425SAlexander Motin 			bintime_addx(&new, tmp);
391a157e425SAlexander Motin 			CTR5(KTR_SPARE2, "load p at %d:   now %d.%08x first in %d.%08x",
392a157e425SAlexander Motin 			    curcpu, now->sec, (unsigned int)(now->frac >> 32),
393a157e425SAlexander Motin 			    new.sec, (unsigned int)(new.frac >> 32));
394a157e425SAlexander Motin 			et_start(timer, &new, &timerperiod);
395a157e425SAlexander Motin 		}
396a157e425SAlexander Motin 	} else {
397a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU) {
398a157e425SAlexander Motin 			state = DPCPU_PTR(timerstate);
399a157e425SAlexander Motin 			next = &state->nexttick;
400a157e425SAlexander Motin 		} else
401a157e425SAlexander Motin 			next = &nexttick;
402a157e425SAlexander Motin 		getnextevent(&new);
403a157e425SAlexander Motin 		eq = bintime_cmp(&new, next, ==);
404a157e425SAlexander Motin 		CTR5(KTR_SPARE2, "load at %d:    next %d.%08x%08x eq %d",
405a157e425SAlexander Motin 		    curcpu, new.sec, (unsigned int)(new.frac >> 32),
406a157e425SAlexander Motin 			     (unsigned int)(new.frac & 0xffffffff),
407a157e425SAlexander Motin 			     eq);
408a157e425SAlexander Motin 		if (!eq) {
409a157e425SAlexander Motin 			*next = new;
410a157e425SAlexander Motin 			bintime_sub(&new, now);
411a157e425SAlexander Motin 			et_start(timer, &new, NULL);
412a157e425SAlexander Motin 		}
413a157e425SAlexander Motin 	}
414a157e425SAlexander Motin }
415a157e425SAlexander Motin 
416a157e425SAlexander Motin /*
417a157e425SAlexander Motin  * Prepare event timer parameters after configuration changes.
418a157e425SAlexander Motin  */
419a157e425SAlexander Motin static void
420a157e425SAlexander Motin setuptimer(void)
421a157e425SAlexander Motin {
422a157e425SAlexander Motin 	int freq;
423a157e425SAlexander Motin 
424a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
425a157e425SAlexander Motin 		periodic = 0;
426a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
427a157e425SAlexander Motin 		periodic = 1;
428*dd9595e7SAlexander Motin 	singlemul = MIN(MAX(singlemul, 1), 20);
429a157e425SAlexander Motin 	freq = hz * singlemul;
430a157e425SAlexander Motin 	while (freq < (profiling ? profhz : stathz))
431a157e425SAlexander Motin 		freq += hz;
432a157e425SAlexander Motin 	freq = round_freq(timer, freq);
433a157e425SAlexander Motin 	FREQ2BT(freq, &timerperiod);
434a157e425SAlexander Motin }
43543fe7d45SAlexander Motin 
43643fe7d45SAlexander Motin /*
43743fe7d45SAlexander Motin  * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler.
43843fe7d45SAlexander Motin  */
439a157e425SAlexander Motin static int
440a157e425SAlexander Motin doconfigtimer(void)
44143fe7d45SAlexander Motin {
442a157e425SAlexander Motin 	struct bintime now;
443a157e425SAlexander Motin 	struct pcpu_state *state;
44443fe7d45SAlexander Motin 
445a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
446a157e425SAlexander Motin 	switch (atomic_load_acq_int(&state->action)) {
447a157e425SAlexander Motin 	case 1:
448a157e425SAlexander Motin 		binuptime(&now);
449a157e425SAlexander Motin 		ET_HW_LOCK(state);
450a157e425SAlexander Motin 		loadtimer(&now, 1);
451a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
452a157e425SAlexander Motin 		state->handle = 0;
453a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
454a157e425SAlexander Motin 		return (1);
455a157e425SAlexander Motin 	case 2:
456a157e425SAlexander Motin 		ET_HW_LOCK(state);
457a157e425SAlexander Motin 		et_stop(timer);
458a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
459a157e425SAlexander Motin 		state->handle = 0;
460a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
461a157e425SAlexander Motin 		return (1);
462a157e425SAlexander Motin 	}
463a157e425SAlexander Motin 	if (atomic_readandclear_int(&state->handle) && !busy) {
464a157e425SAlexander Motin 		binuptime(&now);
465a157e425SAlexander Motin 		handleevents(&now, 0);
46643fe7d45SAlexander Motin 		return (1);
46743fe7d45SAlexander Motin 	}
46843fe7d45SAlexander Motin 	return (0);
46943fe7d45SAlexander Motin }
47043fe7d45SAlexander Motin 
47143fe7d45SAlexander Motin /*
47243fe7d45SAlexander Motin  * Reconfigure specified timer.
47343fe7d45SAlexander Motin  * For per-CPU timers use IPI to make other CPUs to reconfigure.
47443fe7d45SAlexander Motin  */
47543fe7d45SAlexander Motin static void
476a157e425SAlexander Motin configtimer(int start)
47743fe7d45SAlexander Motin {
478a157e425SAlexander Motin 	struct bintime now, next;
479a157e425SAlexander Motin 	struct pcpu_state *state;
48043fe7d45SAlexander Motin 	int cpu;
48143fe7d45SAlexander Motin 
482a157e425SAlexander Motin 	if (start) {
483a157e425SAlexander Motin 		setuptimer();
484a157e425SAlexander Motin 		binuptime(&now);
485a157e425SAlexander Motin 	}
48643fe7d45SAlexander Motin 	critical_enter();
487a157e425SAlexander Motin 	ET_HW_LOCK(DPCPU_PTR(timerstate));
488a157e425SAlexander Motin 	if (start) {
489a157e425SAlexander Motin 		/* Initialize time machine parameters. */
490a157e425SAlexander Motin 		next = now;
491a157e425SAlexander Motin 		bintime_add(&next, &timerperiod);
492a157e425SAlexander Motin 		if (periodic)
493a157e425SAlexander Motin 			nexttick = next;
49443fe7d45SAlexander Motin 		else
495a157e425SAlexander Motin 			nexttick.sec = -1;
496a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
497a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
498a157e425SAlexander Motin 			state->now = now;
499a157e425SAlexander Motin 			state->nextevent = next;
500a157e425SAlexander Motin 			if (periodic)
501a157e425SAlexander Motin 				state->nexttick = next;
502a157e425SAlexander Motin 			else
503a157e425SAlexander Motin 				state->nexttick.sec = -1;
504a157e425SAlexander Motin 			state->nexthard = next;
505a157e425SAlexander Motin 			state->nextstat = next;
506a157e425SAlexander Motin 			state->nextprof = next;
507a157e425SAlexander Motin 			hardclock_sync(cpu);
508a157e425SAlexander Motin 		}
509a157e425SAlexander Motin 		busy = 0;
510a157e425SAlexander Motin 		/* Start global timer or per-CPU timer of this CPU. */
511a157e425SAlexander Motin 		loadtimer(&now, 1);
512a157e425SAlexander Motin 	} else {
513a157e425SAlexander Motin 		busy = 1;
514a157e425SAlexander Motin 		/* Stop global timer or per-CPU timer of this CPU. */
515a157e425SAlexander Motin 		et_stop(timer);
516a157e425SAlexander Motin 	}
517a157e425SAlexander Motin 	ET_HW_UNLOCK(DPCPU_PTR(timerstate));
51843fe7d45SAlexander Motin #ifdef SMP
519a157e425SAlexander Motin 	/* If timer is global or there is no other CPUs yet - we are done. */
520a157e425SAlexander Motin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) {
52143fe7d45SAlexander Motin 		critical_exit();
52243fe7d45SAlexander Motin 		return;
52343fe7d45SAlexander Motin 	}
52443fe7d45SAlexander Motin 	/* Set reconfigure flags for other CPUs. */
52543fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
526a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
527a157e425SAlexander Motin 		atomic_store_rel_int(&state->action,
528a157e425SAlexander Motin 		    (cpu == curcpu) ? 0 : ( start ? 1 : 2));
52943fe7d45SAlexander Motin 	}
530a157e425SAlexander Motin 	/* Broadcast reconfigure IPI. */
531a157e425SAlexander Motin 	ipi_all_but_self(IPI_HARDCLOCK);
53243fe7d45SAlexander Motin 	/* Wait for reconfiguration completed. */
53343fe7d45SAlexander Motin restart:
53443fe7d45SAlexander Motin 	cpu_spinwait();
53543fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
53643fe7d45SAlexander Motin 		if (cpu == curcpu)
53743fe7d45SAlexander Motin 			continue;
538a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
539a157e425SAlexander Motin 		if (atomic_load_acq_int(&state->action))
54043fe7d45SAlexander Motin 			goto restart;
54143fe7d45SAlexander Motin 	}
54243fe7d45SAlexander Motin #endif
543a157e425SAlexander Motin 	critical_exit();
54443fe7d45SAlexander Motin }
54543fe7d45SAlexander Motin 
546a157e425SAlexander Motin /*
547a157e425SAlexander Motin  * Calculate nearest frequency supported by hardware timer.
548a157e425SAlexander Motin  */
54951636352SAlexander Motin static int
55051636352SAlexander Motin round_freq(struct eventtimer *et, int freq)
55151636352SAlexander Motin {
55251636352SAlexander Motin 	uint64_t div;
55351636352SAlexander Motin 
55451636352SAlexander Motin 	if (et->et_frequency != 0) {
555599cf0f1SAlexander Motin 		div = lmax((et->et_frequency + freq / 2) / freq, 1);
55651636352SAlexander Motin 		if (et->et_flags & ET_FLAGS_POW2DIV)
55751636352SAlexander Motin 			div = 1 << (flsl(div + div / 2) - 1);
55851636352SAlexander Motin 		freq = (et->et_frequency + div / 2) / div;
55951636352SAlexander Motin 	}
56051636352SAlexander Motin 	if (et->et_min_period.sec > 0)
56151636352SAlexander Motin 		freq = 0;
562599cf0f1SAlexander Motin 	else if (et->et_min_period.frac != 0)
56351636352SAlexander Motin 		freq = min(freq, BT2FREQ(&et->et_min_period));
56451636352SAlexander Motin 	if (et->et_max_period.sec == 0 && et->et_max_period.frac != 0)
56551636352SAlexander Motin 		freq = max(freq, BT2FREQ(&et->et_max_period));
56651636352SAlexander Motin 	return (freq);
56751636352SAlexander Motin }
56851636352SAlexander Motin 
56943fe7d45SAlexander Motin /*
570a157e425SAlexander Motin  * Configure and start event timers (BSP part).
57143fe7d45SAlexander Motin  */
57243fe7d45SAlexander Motin void
57343fe7d45SAlexander Motin cpu_initclocks_bsp(void)
57443fe7d45SAlexander Motin {
575a157e425SAlexander Motin 	struct pcpu_state *state;
576a157e425SAlexander Motin 	int base, div, cpu;
57743fe7d45SAlexander Motin 
578a157e425SAlexander Motin 	mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
579a157e425SAlexander Motin 	CPU_FOREACH(cpu) {
580a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
581a157e425SAlexander Motin 		mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
582a157e425SAlexander Motin 	}
583a157e425SAlexander Motin #ifdef SMP
584a157e425SAlexander Motin 	callout_new_inserted = cpu_new_callout;
585a157e425SAlexander Motin #endif
586a157e425SAlexander Motin 	/* Grab requested timer or the best of present. */
587a157e425SAlexander Motin 	if (timername[0])
588a157e425SAlexander Motin 		timer = et_find(timername, 0, 0);
589a157e425SAlexander Motin 	if (timer == NULL && periodic) {
590a157e425SAlexander Motin 		timer = et_find(NULL,
59143fe7d45SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
592a157e425SAlexander Motin 	}
593a157e425SAlexander Motin 	if (timer == NULL) {
594a157e425SAlexander Motin 		timer = et_find(NULL,
595a157e425SAlexander Motin 		    ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT);
596a157e425SAlexander Motin 	}
597a157e425SAlexander Motin 	if (timer == NULL && !periodic) {
598a157e425SAlexander Motin 		timer = et_find(NULL,
599a157e425SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
600a157e425SAlexander Motin 	}
601a157e425SAlexander Motin 	if (timer == NULL)
602a157e425SAlexander Motin 		panic("No usable event timer found!");
603a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
604a157e425SAlexander Motin 
605a157e425SAlexander Motin 	/* Adapt to timer capabilities. */
606a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
607a157e425SAlexander Motin 		periodic = 0;
608a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
609a157e425SAlexander Motin 		periodic = 1;
610a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
611a157e425SAlexander Motin 		cpu_disable_deep_sleep++;
612a157e425SAlexander Motin 
61343fe7d45SAlexander Motin 	/*
61443fe7d45SAlexander Motin 	 * We honor the requested 'hz' value.
61543fe7d45SAlexander Motin 	 * We want to run stathz in the neighborhood of 128hz.
61643fe7d45SAlexander Motin 	 * We would like profhz to run as often as possible.
61743fe7d45SAlexander Motin 	 */
618*dd9595e7SAlexander Motin 	if (singlemul <= 0 || singlemul > 20) {
61943fe7d45SAlexander Motin 		if (hz >= 1500 || (hz % 128) == 0)
62043fe7d45SAlexander Motin 			singlemul = 1;
62143fe7d45SAlexander Motin 		else if (hz >= 750)
62243fe7d45SAlexander Motin 			singlemul = 2;
62343fe7d45SAlexander Motin 		else
62443fe7d45SAlexander Motin 			singlemul = 4;
62543fe7d45SAlexander Motin 	}
626a157e425SAlexander Motin 	if (periodic) {
627a157e425SAlexander Motin 		base = round_freq(timer, hz * singlemul);
62851636352SAlexander Motin 		singlemul = max((base + hz / 2) / hz, 1);
62951636352SAlexander Motin 		hz = (base + singlemul / 2) / singlemul;
63051636352SAlexander Motin 		if (base <= 128)
63143fe7d45SAlexander Motin 			stathz = base;
63243fe7d45SAlexander Motin 		else {
63343fe7d45SAlexander Motin 			div = base / 128;
63451636352SAlexander Motin 			if (div >= singlemul && (div % singlemul) == 0)
63543fe7d45SAlexander Motin 				div++;
63643fe7d45SAlexander Motin 			stathz = base / div;
63743fe7d45SAlexander Motin 		}
63843fe7d45SAlexander Motin 		profhz = stathz;
63951636352SAlexander Motin 		while ((profhz + stathz) <= 128 * 64)
64043fe7d45SAlexander Motin 			profhz += stathz;
641a157e425SAlexander Motin 		profhz = round_freq(timer, profhz);
64243fe7d45SAlexander Motin 	} else {
643a157e425SAlexander Motin 		hz = round_freq(timer, hz);
644a157e425SAlexander Motin 		stathz = round_freq(timer, 127);
645a157e425SAlexander Motin 		profhz = round_freq(timer, stathz * 64);
64643fe7d45SAlexander Motin 	}
647599cf0f1SAlexander Motin 	tick = 1000000 / hz;
648a157e425SAlexander Motin 	FREQ2BT(hz, &hardperiod);
649a157e425SAlexander Motin 	FREQ2BT(stathz, &statperiod);
650a157e425SAlexander Motin 	FREQ2BT(profhz, &profperiod);
65143fe7d45SAlexander Motin 	ET_LOCK();
652a157e425SAlexander Motin 	configtimer(1);
65343fe7d45SAlexander Motin 	ET_UNLOCK();
65443fe7d45SAlexander Motin }
65543fe7d45SAlexander Motin 
656a157e425SAlexander Motin /*
657a157e425SAlexander Motin  * Start per-CPU event timers on APs.
658a157e425SAlexander Motin  */
65943fe7d45SAlexander Motin void
66043fe7d45SAlexander Motin cpu_initclocks_ap(void)
66143fe7d45SAlexander Motin {
662a157e425SAlexander Motin 	struct bintime now;
663a157e425SAlexander Motin 	struct pcpu_state *state;
66443fe7d45SAlexander Motin 
665a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_PERCPU) {
666a157e425SAlexander Motin 		state = DPCPU_PTR(timerstate);
667a157e425SAlexander Motin 		binuptime(&now);
668a157e425SAlexander Motin 		ET_HW_LOCK(state);
669a157e425SAlexander Motin 		loadtimer(&now, 1);
670a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
67143fe7d45SAlexander Motin 	}
67243fe7d45SAlexander Motin }
67343fe7d45SAlexander Motin 
674a157e425SAlexander Motin /*
675a157e425SAlexander Motin  * Switch to profiling clock rates.
676a157e425SAlexander Motin  */
67743fe7d45SAlexander Motin void
67843fe7d45SAlexander Motin cpu_startprofclock(void)
67943fe7d45SAlexander Motin {
68043fe7d45SAlexander Motin 
68143fe7d45SAlexander Motin 	ET_LOCK();
682a157e425SAlexander Motin 	if (periodic) {
683a157e425SAlexander Motin 		configtimer(0);
684a157e425SAlexander Motin 		profiling = 1;
685a157e425SAlexander Motin 		configtimer(1);
686a157e425SAlexander Motin 	} else
687a157e425SAlexander Motin 		profiling = 1;
68843fe7d45SAlexander Motin 	ET_UNLOCK();
68943fe7d45SAlexander Motin }
69043fe7d45SAlexander Motin 
691a157e425SAlexander Motin /*
692a157e425SAlexander Motin  * Switch to regular clock rates.
693a157e425SAlexander Motin  */
69443fe7d45SAlexander Motin void
69543fe7d45SAlexander Motin cpu_stopprofclock(void)
69643fe7d45SAlexander Motin {
69743fe7d45SAlexander Motin 
69843fe7d45SAlexander Motin 	ET_LOCK();
699a157e425SAlexander Motin 	if (periodic) {
700a157e425SAlexander Motin 		configtimer(0);
701a157e425SAlexander Motin 		profiling = 0;
702a157e425SAlexander Motin 		configtimer(1);
703a157e425SAlexander Motin 	} else
704a157e425SAlexander Motin 		profiling = 0;
70543fe7d45SAlexander Motin 	ET_UNLOCK();
70643fe7d45SAlexander Motin }
70743fe7d45SAlexander Motin 
708a157e425SAlexander Motin /*
709a157e425SAlexander Motin  * Switch to idle mode (all ticks handled).
710a157e425SAlexander Motin  */
711a157e425SAlexander Motin void
712a157e425SAlexander Motin cpu_idleclock(void)
713a157e425SAlexander Motin {
714a157e425SAlexander Motin 	struct bintime now, t;
715a157e425SAlexander Motin 	struct pcpu_state *state;
716a157e425SAlexander Motin 
717a157e425SAlexander Motin 	if (idletick || busy ||
718a157e425SAlexander Motin 	    (periodic && (timer->et_flags & ET_FLAGS_PERCPU)))
719a157e425SAlexander Motin 		return;
720a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
721a157e425SAlexander Motin 	if (periodic)
722a157e425SAlexander Motin 		now = state->now;
723a157e425SAlexander Motin 	else
724a157e425SAlexander Motin 		binuptime(&now);
725a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "idle at %d:    now  %d.%08x%08x",
726a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
727a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
728a157e425SAlexander Motin 	getnextcpuevent(&t, 1);
729a157e425SAlexander Motin 	ET_HW_LOCK(state);
730a157e425SAlexander Motin 	state->idle = 1;
731a157e425SAlexander Motin 	state->nextevent = t;
732a157e425SAlexander Motin 	if (!periodic)
733a157e425SAlexander Motin 		loadtimer(&now, 0);
734a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
735a157e425SAlexander Motin }
736a157e425SAlexander Motin 
737a157e425SAlexander Motin /*
738a157e425SAlexander Motin  * Switch to active mode (skip empty ticks).
739a157e425SAlexander Motin  */
740a157e425SAlexander Motin void
741a157e425SAlexander Motin cpu_activeclock(void)
742a157e425SAlexander Motin {
743a157e425SAlexander Motin 	struct bintime now;
744a157e425SAlexander Motin 	struct pcpu_state *state;
745a157e425SAlexander Motin 	struct thread *td;
746a157e425SAlexander Motin 
747a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
748a157e425SAlexander Motin 	if (state->idle == 0 || busy)
749a157e425SAlexander Motin 		return;
750a157e425SAlexander Motin 	if (periodic)
751a157e425SAlexander Motin 		now = state->now;
752a157e425SAlexander Motin 	else
753a157e425SAlexander Motin 		binuptime(&now);
754a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "active at %d:  now  %d.%08x%08x",
755a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
756a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
757a157e425SAlexander Motin 	spinlock_enter();
758a157e425SAlexander Motin 	td = curthread;
759a157e425SAlexander Motin 	td->td_intr_nesting_level++;
760a157e425SAlexander Motin 	handleevents(&now, 1);
761a157e425SAlexander Motin 	td->td_intr_nesting_level--;
762a157e425SAlexander Motin 	spinlock_exit();
763a157e425SAlexander Motin }
764a157e425SAlexander Motin 
765a157e425SAlexander Motin #ifdef SMP
766a157e425SAlexander Motin static void
767a157e425SAlexander Motin cpu_new_callout(int cpu, int ticks)
768a157e425SAlexander Motin {
769a157e425SAlexander Motin 	struct bintime tmp;
770a157e425SAlexander Motin 	struct pcpu_state *state;
771a157e425SAlexander Motin 
772a157e425SAlexander Motin 	CTR3(KTR_SPARE2, "new co at %d:    on %d in %d",
773a157e425SAlexander Motin 	    curcpu, cpu, ticks);
774a157e425SAlexander Motin 	state = DPCPU_ID_PTR(cpu, timerstate);
775a157e425SAlexander Motin 	ET_HW_LOCK(state);
776a157e425SAlexander Motin 	if (state->idle == 0 || busy) {
777a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
778a157e425SAlexander Motin 		return;
779a157e425SAlexander Motin 	}
780a157e425SAlexander Motin 	/*
781a157e425SAlexander Motin 	 * If timer is periodic - just update next event time for target CPU.
782a157e425SAlexander Motin 	 */
783a157e425SAlexander Motin 	if (periodic) {
784a157e425SAlexander Motin 		state->nextevent = state->nexthard;
785a157e425SAlexander Motin 		tmp = hardperiod;
786a157e425SAlexander Motin 		bintime_mul(&tmp, ticks - 1);
787a157e425SAlexander Motin 		bintime_add(&state->nextevent, &tmp);
788a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
789a157e425SAlexander Motin 		return;
790a157e425SAlexander Motin 	}
791a157e425SAlexander Motin 	/*
792a157e425SAlexander Motin 	 * Otherwise we have to wake that CPU up, as we can't get present
793a157e425SAlexander Motin 	 * bintime to reprogram global timer from here. If timer is per-CPU,
794a157e425SAlexander Motin 	 * we by definition can't do it from here.
795a157e425SAlexander Motin 	 */
796a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
797a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_PERCPU) {
798a157e425SAlexander Motin 		state->handle = 1;
799a157e425SAlexander Motin 		ipi_cpu(cpu, IPI_HARDCLOCK);
800a157e425SAlexander Motin 	} else {
801a157e425SAlexander Motin 		if (!cpu_idle_wakeup(cpu))
802a157e425SAlexander Motin 			ipi_cpu(cpu, IPI_AST);
803a157e425SAlexander Motin 	}
804a157e425SAlexander Motin }
805a157e425SAlexander Motin #endif
806a157e425SAlexander Motin 
807a157e425SAlexander Motin /*
808a157e425SAlexander Motin  * Report or change the active event timers hardware.
809a157e425SAlexander Motin  */
81043fe7d45SAlexander Motin static int
811a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS)
81243fe7d45SAlexander Motin {
81343fe7d45SAlexander Motin 	char buf[32];
81443fe7d45SAlexander Motin 	struct eventtimer *et;
81543fe7d45SAlexander Motin 	int error;
81643fe7d45SAlexander Motin 
81743fe7d45SAlexander Motin 	ET_LOCK();
818a157e425SAlexander Motin 	et = timer;
81943fe7d45SAlexander Motin 	snprintf(buf, sizeof(buf), "%s", et->et_name);
82043fe7d45SAlexander Motin 	ET_UNLOCK();
82143fe7d45SAlexander Motin 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
82243fe7d45SAlexander Motin 	ET_LOCK();
823a157e425SAlexander Motin 	et = timer;
82443fe7d45SAlexander Motin 	if (error != 0 || req->newptr == NULL ||
825a157e425SAlexander Motin 	    strcasecmp(buf, et->et_name) == 0) {
82643fe7d45SAlexander Motin 		ET_UNLOCK();
82743fe7d45SAlexander Motin 		return (error);
82843fe7d45SAlexander Motin 	}
829a157e425SAlexander Motin 	et = et_find(buf, 0, 0);
83043fe7d45SAlexander Motin 	if (et == NULL) {
83143fe7d45SAlexander Motin 		ET_UNLOCK();
83243fe7d45SAlexander Motin 		return (ENOENT);
83343fe7d45SAlexander Motin 	}
83443fe7d45SAlexander Motin 	configtimer(0);
835a157e425SAlexander Motin 	et_free(timer);
836a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_C3STOP)
837a157e425SAlexander Motin 		cpu_disable_deep_sleep++;
838a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
839a157e425SAlexander Motin 		cpu_disable_deep_sleep--;
840a157e425SAlexander Motin 	timer = et;
841a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
84243fe7d45SAlexander Motin 	configtimer(1);
84343fe7d45SAlexander Motin 	ET_UNLOCK();
84443fe7d45SAlexander Motin 	return (error);
84543fe7d45SAlexander Motin }
846a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer,
84743fe7d45SAlexander Motin     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
848*dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer");
849a157e425SAlexander Motin 
850a157e425SAlexander Motin /*
851a157e425SAlexander Motin  * Report or change the active event timer periodicity.
852a157e425SAlexander Motin  */
853a157e425SAlexander Motin static int
854a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS)
855a157e425SAlexander Motin {
856a157e425SAlexander Motin 	int error, val;
857a157e425SAlexander Motin 
858a157e425SAlexander Motin 	val = periodic;
859a157e425SAlexander Motin 	error = sysctl_handle_int(oidp, &val, 0, req);
860a157e425SAlexander Motin 	if (error != 0 || req->newptr == NULL)
861a157e425SAlexander Motin 		return (error);
862a157e425SAlexander Motin 	ET_LOCK();
863a157e425SAlexander Motin 	configtimer(0);
864a157e425SAlexander Motin 	periodic = val;
865a157e425SAlexander Motin 	configtimer(1);
866a157e425SAlexander Motin 	ET_UNLOCK();
867a157e425SAlexander Motin 	return (error);
868a157e425SAlexander Motin }
869a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic,
870a157e425SAlexander Motin     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
871*dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode");
87243fe7d45SAlexander Motin 
87343fe7d45SAlexander Motin #endif
874