xref: /freebsd/sys/kern/kern_clocksource.c (revision 0e18987383f8efc7624b61731c42c565293be9d8)
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>
52*0e189873SAlexander Motin #include <sys/timetc.h>
5343fe7d45SAlexander Motin 
5443fe7d45SAlexander Motin #include <machine/atomic.h>
5543fe7d45SAlexander Motin #include <machine/clock.h>
5643fe7d45SAlexander Motin #include <machine/cpu.h>
5743fe7d45SAlexander Motin #include <machine/smp.h>
5843fe7d45SAlexander Motin 
5943fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS
6043fe7d45SAlexander Motin #include <sys/dtrace_bsd.h>
6143fe7d45SAlexander Motin cyclic_clock_func_t	cyclic_clock_func[MAXCPU];
6243fe7d45SAlexander Motin #endif
6343fe7d45SAlexander Motin 
64a157e425SAlexander Motin int			cpu_disable_deep_sleep = 0; /* Timer dies in C3. */
6543fe7d45SAlexander Motin 
66a157e425SAlexander Motin static void		setuptimer(void);
67a157e425SAlexander Motin static void		loadtimer(struct bintime *now, int first);
68a157e425SAlexander Motin static int		doconfigtimer(void);
69a157e425SAlexander Motin static void		configtimer(int start);
70a157e425SAlexander Motin static int		round_freq(struct eventtimer *et, int freq);
7143fe7d45SAlexander Motin 
72a157e425SAlexander Motin static void		getnextcpuevent(struct bintime *event, int idle);
73a157e425SAlexander Motin static void		getnextevent(struct bintime *event);
74a157e425SAlexander Motin static int		handleevents(struct bintime *now, int fake);
75a157e425SAlexander Motin #ifdef SMP
76a157e425SAlexander Motin static void		cpu_new_callout(int cpu, int ticks);
77a157e425SAlexander Motin #endif
7843fe7d45SAlexander Motin 
79a157e425SAlexander Motin static struct mtx	et_hw_mtx;
80a157e425SAlexander Motin 
81a157e425SAlexander Motin #define	ET_HW_LOCK(state)						\
82a157e425SAlexander Motin 	{								\
83a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
84a157e425SAlexander Motin 			mtx_lock_spin(&(state)->et_hw_mtx);		\
85a157e425SAlexander Motin 		else							\
86a157e425SAlexander Motin 			mtx_lock_spin(&et_hw_mtx);			\
87a157e425SAlexander Motin 	}
88a157e425SAlexander Motin 
89a157e425SAlexander Motin #define	ET_HW_UNLOCK(state)						\
90a157e425SAlexander Motin 	{								\
91a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
92a157e425SAlexander Motin 			mtx_unlock_spin(&(state)->et_hw_mtx);		\
93a157e425SAlexander Motin 		else							\
94a157e425SAlexander Motin 			mtx_unlock_spin(&et_hw_mtx);			\
95a157e425SAlexander Motin 	}
96a157e425SAlexander Motin 
97a157e425SAlexander Motin static struct eventtimer *timer = NULL;
98a157e425SAlexander Motin static struct bintime	timerperiod;	/* Timer period for periodic mode. */
99a157e425SAlexander Motin static struct bintime	hardperiod;	/* hardclock() events period. */
100a157e425SAlexander Motin static struct bintime	statperiod;	/* statclock() events period. */
101a157e425SAlexander Motin static struct bintime	profperiod;	/* profclock() events period. */
102a157e425SAlexander Motin static struct bintime	nexttick;	/* Next global timer tick time. */
103a157e425SAlexander Motin static u_int		busy = 0;	/* Reconfiguration is in progress. */
104a157e425SAlexander Motin static int		profiling = 0;	/* Profiling events enabled. */
105a157e425SAlexander Motin 
106a157e425SAlexander Motin static char		timername[32];	/* Wanted timer. */
107a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername));
108a157e425SAlexander Motin 
109dd9595e7SAlexander Motin static int		singlemul = 0;	/* Multiplier for periodic mode. */
11043fe7d45SAlexander Motin TUNABLE_INT("kern.eventtimer.singlemul", &singlemul);
11143fe7d45SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RW, &singlemul,
112a157e425SAlexander Motin     0, "Multiplier for periodic mode");
11343fe7d45SAlexander Motin 
114a157e425SAlexander Motin static u_int		idletick = 0;	/* Idle mode allowed. */
115a157e425SAlexander Motin TUNABLE_INT("kern.eventtimer.idletick", &idletick);
116a157e425SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RW, &idletick,
117a157e425SAlexander Motin     0, "Run periodic events when idle");
118a157e425SAlexander Motin 
119a157e425SAlexander Motin static int		periodic = 0;	/* Periodic or one-shot mode. */
120a157e425SAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &periodic);
121a157e425SAlexander Motin 
122a157e425SAlexander Motin struct pcpu_state {
123a157e425SAlexander Motin 	struct mtx	et_hw_mtx;	/* Per-CPU timer mutex. */
124a157e425SAlexander Motin 	u_int		action;		/* Reconfiguration requests. */
125a157e425SAlexander Motin 	u_int		handle;		/* Immediate handle resuests. */
126a157e425SAlexander Motin 	struct bintime	now;		/* Last tick time. */
127a157e425SAlexander Motin 	struct bintime	nextevent;	/* Next scheduled event on this CPU. */
128a157e425SAlexander Motin 	struct bintime	nexttick;	/* Next timer tick time. */
129a157e425SAlexander Motin 	struct bintime	nexthard;	/* Next hardlock() event. */
130a157e425SAlexander Motin 	struct bintime	nextstat;	/* Next statclock() event. */
131a157e425SAlexander Motin 	struct bintime	nextprof;	/* Next profclock() event. */
132a157e425SAlexander Motin 	int		ipi;		/* This CPU needs IPI. */
133a157e425SAlexander Motin 	int		idle;		/* This CPU is in idle mode. */
134a157e425SAlexander Motin };
135a157e425SAlexander Motin 
136a157e425SAlexander Motin static DPCPU_DEFINE(struct pcpu_state, timerstate);
13743fe7d45SAlexander Motin 
13843fe7d45SAlexander Motin #define FREQ2BT(freq, bt)						\
13943fe7d45SAlexander Motin {									\
14043fe7d45SAlexander Motin 	(bt)->sec = 0;							\
14143fe7d45SAlexander Motin 	(bt)->frac = ((uint64_t)0x8000000000000000  / (freq)) << 1;	\
14243fe7d45SAlexander Motin }
14351636352SAlexander Motin #define BT2FREQ(bt)							\
14451636352SAlexander Motin 	(((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) /		\
14551636352SAlexander Motin 	    ((bt)->frac >> 1))
14643fe7d45SAlexander Motin 
147a157e425SAlexander Motin /*
148a157e425SAlexander Motin  * Timer broadcast IPI handler.
149a157e425SAlexander Motin  */
150a157e425SAlexander Motin int
151a157e425SAlexander Motin hardclockintr(void)
15243fe7d45SAlexander Motin {
153a157e425SAlexander Motin 	struct bintime now;
154a157e425SAlexander Motin 	struct pcpu_state *state;
155a157e425SAlexander Motin 	int done;
15643fe7d45SAlexander Motin 
157a157e425SAlexander Motin 	if (doconfigtimer() || busy)
158a157e425SAlexander Motin 		return (FILTER_HANDLED);
159a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
160a157e425SAlexander Motin 	now = state->now;
161a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "ipi  at %d:    now  %d.%08x%08x",
162a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
163a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
164a157e425SAlexander Motin 	done = handleevents(&now, 0);
165a157e425SAlexander Motin 	return (done ? FILTER_HANDLED : FILTER_STRAY);
166a157e425SAlexander Motin }
167a157e425SAlexander Motin 
168a157e425SAlexander Motin /*
169a157e425SAlexander Motin  * Handle all events for specified time on this CPU
170a157e425SAlexander Motin  */
171a157e425SAlexander Motin static int
172a157e425SAlexander Motin handleevents(struct bintime *now, int fake)
173a157e425SAlexander Motin {
174a157e425SAlexander Motin 	struct bintime t;
175a157e425SAlexander Motin 	struct trapframe *frame;
176a157e425SAlexander Motin 	struct pcpu_state *state;
177a157e425SAlexander Motin 	uintfptr_t pc;
178a157e425SAlexander Motin 	int usermode;
179a157e425SAlexander Motin 	int done, runs;
180a157e425SAlexander Motin 
181a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "handle at %d:  now  %d.%08x%08x",
182a157e425SAlexander Motin 	    curcpu, now->sec, (unsigned int)(now->frac >> 32),
183a157e425SAlexander Motin 		     (unsigned int)(now->frac & 0xffffffff));
184a157e425SAlexander Motin 	done = 0;
185a157e425SAlexander Motin 	if (fake) {
186a157e425SAlexander Motin 		frame = NULL;
187a157e425SAlexander Motin 		usermode = 0;
188a157e425SAlexander Motin 		pc = 0;
189a157e425SAlexander Motin 	} else {
190a157e425SAlexander Motin 		frame = curthread->td_intr_frame;
191a157e425SAlexander Motin 		usermode = TRAPF_USERMODE(frame);
192a157e425SAlexander Motin 		pc = TRAPF_PC(frame);
193a157e425SAlexander Motin 	}
19443fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS
19543fe7d45SAlexander Motin 	/*
19643fe7d45SAlexander Motin 	 * If the DTrace hooks are configured and a callback function
19743fe7d45SAlexander Motin 	 * has been registered, then call it to process the high speed
19843fe7d45SAlexander Motin 	 * timers.
19943fe7d45SAlexander Motin 	 */
200a157e425SAlexander Motin 	if (!fake && cyclic_clock_func[curcpu] != NULL)
201a157e425SAlexander Motin 		(*cyclic_clock_func[curcpu])(frame);
20243fe7d45SAlexander Motin #endif
203a157e425SAlexander Motin 	runs = 0;
204a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
205a157e425SAlexander Motin 	while (bintime_cmp(now, &state->nexthard, >=)) {
206a157e425SAlexander Motin 		bintime_add(&state->nexthard, &hardperiod);
207a157e425SAlexander Motin 		runs++;
20843fe7d45SAlexander Motin 	}
209a157e425SAlexander Motin 	if (runs) {
210a157e425SAlexander Motin 		hardclock_anycpu(runs, usermode);
211a157e425SAlexander Motin 		done = 1;
21243fe7d45SAlexander Motin 	}
213a157e425SAlexander Motin 	while (bintime_cmp(now, &state->nextstat, >=)) {
214a157e425SAlexander Motin 		statclock(usermode);
215a157e425SAlexander Motin 		bintime_add(&state->nextstat, &statperiod);
216a157e425SAlexander Motin 		done = 1;
21743fe7d45SAlexander Motin 	}
218a157e425SAlexander Motin 	if (profiling) {
219a157e425SAlexander Motin 		while (bintime_cmp(now, &state->nextprof, >=)) {
220a157e425SAlexander Motin 			if (!fake)
221a157e425SAlexander Motin 				profclock(usermode, pc);
222a157e425SAlexander Motin 			bintime_add(&state->nextprof, &profperiod);
223a157e425SAlexander Motin 			done = 1;
22443fe7d45SAlexander Motin 		}
225a157e425SAlexander Motin 	} else
226a157e425SAlexander Motin 		state->nextprof = state->nextstat;
227a157e425SAlexander Motin 	getnextcpuevent(&t, 0);
228a157e425SAlexander Motin 	ET_HW_LOCK(state);
229a157e425SAlexander Motin 	if (!busy) {
230a157e425SAlexander Motin 		state->idle = 0;
231a157e425SAlexander Motin 		state->nextevent = t;
232a157e425SAlexander Motin 		loadtimer(now, 0);
23343fe7d45SAlexander Motin 	}
234a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
235a157e425SAlexander Motin 	return (done);
23643fe7d45SAlexander Motin }
23743fe7d45SAlexander Motin 
23843fe7d45SAlexander Motin /*
239a157e425SAlexander Motin  * Schedule binuptime of the next event on current CPU.
24043fe7d45SAlexander Motin  */
24143fe7d45SAlexander Motin static void
242a157e425SAlexander Motin getnextcpuevent(struct bintime *event, int idle)
24343fe7d45SAlexander Motin {
244a157e425SAlexander Motin 	struct bintime tmp;
245a157e425SAlexander Motin 	struct pcpu_state *state;
246a157e425SAlexander Motin 	int skip;
24743fe7d45SAlexander Motin 
248a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
249a157e425SAlexander Motin 	*event = state->nexthard;
250a157e425SAlexander Motin 	if (idle) { /* If CPU is idle - ask callouts for how long. */
251*0e189873SAlexander Motin 		skip = 4;
252*0e189873SAlexander Motin 		if (curcpu == CPU_FIRST() && tc_min_ticktock_freq > skip)
253*0e189873SAlexander Motin 			skip = tc_min_ticktock_freq;
254*0e189873SAlexander Motin 		skip = callout_tickstofirst(hz / skip) - 1;
255a157e425SAlexander Motin 		CTR2(KTR_SPARE2, "skip   at %d: %d", curcpu, skip);
256a157e425SAlexander Motin 		tmp = hardperiod;
257a157e425SAlexander Motin 		bintime_mul(&tmp, skip);
258a157e425SAlexander Motin 		bintime_add(event, &tmp);
259a157e425SAlexander Motin 	} else { /* If CPU is active - handle all types of events. */
260a157e425SAlexander Motin 		if (bintime_cmp(event, &state->nextstat, >))
261a157e425SAlexander Motin 			*event = state->nextstat;
262a157e425SAlexander Motin 		if (profiling &&
263a157e425SAlexander Motin 		    bintime_cmp(event, &state->nextprof, >))
264a157e425SAlexander Motin 			*event = state->nextprof;
26543fe7d45SAlexander Motin 	}
26643fe7d45SAlexander Motin }
267a157e425SAlexander Motin 
268a157e425SAlexander Motin /*
269a157e425SAlexander Motin  * Schedule binuptime of the next event on all CPUs.
270a157e425SAlexander Motin  */
271a157e425SAlexander Motin static void
272a157e425SAlexander Motin getnextevent(struct bintime *event)
273a157e425SAlexander Motin {
274a157e425SAlexander Motin 	struct pcpu_state *state;
275a157e425SAlexander Motin #ifdef SMP
276a157e425SAlexander Motin 	int	cpu;
277a157e425SAlexander Motin #endif
278a157e425SAlexander Motin 	int	c;
279a157e425SAlexander Motin 
280a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
281a157e425SAlexander Motin 	*event = state->nextevent;
282a157e425SAlexander Motin 	c = curcpu;
283a157e425SAlexander Motin #ifdef SMP
284a157e425SAlexander Motin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) {
285a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
286a157e425SAlexander Motin 			if (curcpu == cpu)
287a157e425SAlexander Motin 				continue;
288a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
289a157e425SAlexander Motin 			if (bintime_cmp(event, &state->nextevent, >)) {
290a157e425SAlexander Motin 				*event = state->nextevent;
291a157e425SAlexander Motin 				c = cpu;
29243fe7d45SAlexander Motin 			}
29343fe7d45SAlexander Motin 		}
294a157e425SAlexander Motin 	}
295a157e425SAlexander Motin #endif
296a157e425SAlexander Motin 	CTR5(KTR_SPARE2, "next at %d:    next %d.%08x%08x by %d",
297a157e425SAlexander Motin 	    curcpu, event->sec, (unsigned int)(event->frac >> 32),
298a157e425SAlexander Motin 			     (unsigned int)(event->frac & 0xffffffff), c);
299a157e425SAlexander Motin }
300a157e425SAlexander Motin 
301a157e425SAlexander Motin /* Hardware timer callback function. */
302a157e425SAlexander Motin static void
303a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg)
304a157e425SAlexander Motin {
305a157e425SAlexander Motin 	struct bintime now;
306a157e425SAlexander Motin 	struct bintime *next;
307a157e425SAlexander Motin 	struct pcpu_state *state;
308a157e425SAlexander Motin #ifdef SMP
309a157e425SAlexander Motin 	int cpu, bcast;
310a157e425SAlexander Motin #endif
311a157e425SAlexander Motin 
312a157e425SAlexander Motin 	/* Do not touch anything if somebody reconfiguring timers. */
313a157e425SAlexander Motin 	if (busy)
314a157e425SAlexander Motin 		return;
315a157e425SAlexander Motin 	/* Update present and next tick times. */
316a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
317a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_PERCPU) {
318a157e425SAlexander Motin 		next = &state->nexttick;
319a157e425SAlexander Motin 	} else
320a157e425SAlexander Motin 		next = &nexttick;
321a157e425SAlexander Motin 	if (periodic) {
322a157e425SAlexander Motin 		now = *next;	/* Ex-next tick time becomes present time. */
323a157e425SAlexander Motin 		bintime_add(next, &timerperiod); /* Next tick in 1 period. */
324a157e425SAlexander Motin 	} else {
325a157e425SAlexander Motin 		binuptime(&now);	/* Get present time from hardware. */
326a157e425SAlexander Motin 		next->sec = -1;		/* Next tick is not scheduled yet. */
327a157e425SAlexander Motin 	}
328a157e425SAlexander Motin 	state->now = now;
329a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "intr at %d:    now  %d.%08x%08x",
330a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
331a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
332a157e425SAlexander Motin 
333a157e425SAlexander Motin #ifdef SMP
334a157e425SAlexander Motin 	/* Prepare broadcasting to other CPUs for non-per-CPU timers. */
335a157e425SAlexander Motin 	bcast = 0;
336a157e425SAlexander Motin 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) {
337a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
338a157e425SAlexander Motin 			if (curcpu == cpu)
339a157e425SAlexander Motin 				continue;
340a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
341a157e425SAlexander Motin 			ET_HW_LOCK(state);
342a157e425SAlexander Motin 			state->now = now;
343a157e425SAlexander Motin 			if (bintime_cmp(&now, &state->nextevent, >=)) {
344a157e425SAlexander Motin 				state->nextevent.sec++;
345a157e425SAlexander Motin 				state->ipi = 1;
346a157e425SAlexander Motin 				bcast = 1;
347a157e425SAlexander Motin 			}
348a157e425SAlexander Motin 			ET_HW_UNLOCK(state);
349a157e425SAlexander Motin 		}
350a157e425SAlexander Motin 	}
351a157e425SAlexander Motin #endif
352a157e425SAlexander Motin 
353a157e425SAlexander Motin 	/* Handle events for this time on this CPU. */
354a157e425SAlexander Motin 	handleevents(&now, 0);
355a157e425SAlexander Motin 
356a157e425SAlexander Motin #ifdef SMP
357a157e425SAlexander Motin 	/* Broadcast interrupt to other CPUs for non-per-CPU timers. */
358a157e425SAlexander Motin 	if (bcast) {
359a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
360a157e425SAlexander Motin 			if (curcpu == cpu)
361a157e425SAlexander Motin 				continue;
362a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
363a157e425SAlexander Motin 			if (state->ipi) {
364a157e425SAlexander Motin 				state->ipi = 0;
365a157e425SAlexander Motin 				ipi_cpu(cpu, IPI_HARDCLOCK);
366a157e425SAlexander Motin 			}
367a157e425SAlexander Motin 		}
368a157e425SAlexander Motin 	}
369a157e425SAlexander Motin #endif
370a157e425SAlexander Motin }
371a157e425SAlexander Motin 
372a157e425SAlexander Motin /*
373a157e425SAlexander Motin  * Load new value into hardware timer.
374a157e425SAlexander Motin  */
375a157e425SAlexander Motin static void
376a157e425SAlexander Motin loadtimer(struct bintime *now, int start)
377a157e425SAlexander Motin {
378a157e425SAlexander Motin 	struct pcpu_state *state;
379a157e425SAlexander Motin 	struct bintime new;
380a157e425SAlexander Motin 	struct bintime *next;
381a157e425SAlexander Motin 	uint64_t tmp;
382a157e425SAlexander Motin 	int eq;
383a157e425SAlexander Motin 
384a157e425SAlexander Motin 	if (periodic) {
385a157e425SAlexander Motin 		if (start) {
386a157e425SAlexander Motin 			/*
387a157e425SAlexander Motin 			 * Try to start all periodic timers aligned
388a157e425SAlexander Motin 			 * to period to make events synchronous.
389a157e425SAlexander Motin 			 */
390a157e425SAlexander Motin 			tmp = ((uint64_t)now->sec << 36) + (now->frac >> 28);
391a157e425SAlexander Motin 			tmp = (tmp % (timerperiod.frac >> 28)) << 28;
392a157e425SAlexander Motin 			tmp = timerperiod.frac - tmp;
393a157e425SAlexander Motin 			new = timerperiod;
394a157e425SAlexander Motin 			bintime_addx(&new, tmp);
395a157e425SAlexander Motin 			CTR5(KTR_SPARE2, "load p at %d:   now %d.%08x first in %d.%08x",
396a157e425SAlexander Motin 			    curcpu, now->sec, (unsigned int)(now->frac >> 32),
397a157e425SAlexander Motin 			    new.sec, (unsigned int)(new.frac >> 32));
398a157e425SAlexander Motin 			et_start(timer, &new, &timerperiod);
399a157e425SAlexander Motin 		}
400a157e425SAlexander Motin 	} else {
401a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU) {
402a157e425SAlexander Motin 			state = DPCPU_PTR(timerstate);
403a157e425SAlexander Motin 			next = &state->nexttick;
404a157e425SAlexander Motin 		} else
405a157e425SAlexander Motin 			next = &nexttick;
406a157e425SAlexander Motin 		getnextevent(&new);
407a157e425SAlexander Motin 		eq = bintime_cmp(&new, next, ==);
408a157e425SAlexander Motin 		CTR5(KTR_SPARE2, "load at %d:    next %d.%08x%08x eq %d",
409a157e425SAlexander Motin 		    curcpu, new.sec, (unsigned int)(new.frac >> 32),
410a157e425SAlexander Motin 			     (unsigned int)(new.frac & 0xffffffff),
411a157e425SAlexander Motin 			     eq);
412a157e425SAlexander Motin 		if (!eq) {
413a157e425SAlexander Motin 			*next = new;
414a157e425SAlexander Motin 			bintime_sub(&new, now);
415a157e425SAlexander Motin 			et_start(timer, &new, NULL);
416a157e425SAlexander Motin 		}
417a157e425SAlexander Motin 	}
418a157e425SAlexander Motin }
419a157e425SAlexander Motin 
420a157e425SAlexander Motin /*
421a157e425SAlexander Motin  * Prepare event timer parameters after configuration changes.
422a157e425SAlexander Motin  */
423a157e425SAlexander Motin static void
424a157e425SAlexander Motin setuptimer(void)
425a157e425SAlexander Motin {
426a157e425SAlexander Motin 	int freq;
427a157e425SAlexander Motin 
428a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
429a157e425SAlexander Motin 		periodic = 0;
430a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
431a157e425SAlexander Motin 		periodic = 1;
432dd9595e7SAlexander Motin 	singlemul = MIN(MAX(singlemul, 1), 20);
433a157e425SAlexander Motin 	freq = hz * singlemul;
434a157e425SAlexander Motin 	while (freq < (profiling ? profhz : stathz))
435a157e425SAlexander Motin 		freq += hz;
436a157e425SAlexander Motin 	freq = round_freq(timer, freq);
437a157e425SAlexander Motin 	FREQ2BT(freq, &timerperiod);
438a157e425SAlexander Motin }
43943fe7d45SAlexander Motin 
44043fe7d45SAlexander Motin /*
44143fe7d45SAlexander Motin  * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler.
44243fe7d45SAlexander Motin  */
443a157e425SAlexander Motin static int
444a157e425SAlexander Motin doconfigtimer(void)
44543fe7d45SAlexander Motin {
446a157e425SAlexander Motin 	struct bintime now;
447a157e425SAlexander Motin 	struct pcpu_state *state;
44843fe7d45SAlexander Motin 
449a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
450a157e425SAlexander Motin 	switch (atomic_load_acq_int(&state->action)) {
451a157e425SAlexander Motin 	case 1:
452a157e425SAlexander Motin 		binuptime(&now);
453a157e425SAlexander Motin 		ET_HW_LOCK(state);
454a157e425SAlexander Motin 		loadtimer(&now, 1);
455a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
456a157e425SAlexander Motin 		state->handle = 0;
457a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
458a157e425SAlexander Motin 		return (1);
459a157e425SAlexander Motin 	case 2:
460a157e425SAlexander Motin 		ET_HW_LOCK(state);
461a157e425SAlexander Motin 		et_stop(timer);
462a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
463a157e425SAlexander Motin 		state->handle = 0;
464a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
465a157e425SAlexander Motin 		return (1);
466a157e425SAlexander Motin 	}
467a157e425SAlexander Motin 	if (atomic_readandclear_int(&state->handle) && !busy) {
468a157e425SAlexander Motin 		binuptime(&now);
469a157e425SAlexander Motin 		handleevents(&now, 0);
47043fe7d45SAlexander Motin 		return (1);
47143fe7d45SAlexander Motin 	}
47243fe7d45SAlexander Motin 	return (0);
47343fe7d45SAlexander Motin }
47443fe7d45SAlexander Motin 
47543fe7d45SAlexander Motin /*
47643fe7d45SAlexander Motin  * Reconfigure specified timer.
47743fe7d45SAlexander Motin  * For per-CPU timers use IPI to make other CPUs to reconfigure.
47843fe7d45SAlexander Motin  */
47943fe7d45SAlexander Motin static void
480a157e425SAlexander Motin configtimer(int start)
48143fe7d45SAlexander Motin {
482a157e425SAlexander Motin 	struct bintime now, next;
483a157e425SAlexander Motin 	struct pcpu_state *state;
48443fe7d45SAlexander Motin 	int cpu;
48543fe7d45SAlexander Motin 
486a157e425SAlexander Motin 	if (start) {
487a157e425SAlexander Motin 		setuptimer();
488a157e425SAlexander Motin 		binuptime(&now);
489a157e425SAlexander Motin 	}
49043fe7d45SAlexander Motin 	critical_enter();
491a157e425SAlexander Motin 	ET_HW_LOCK(DPCPU_PTR(timerstate));
492a157e425SAlexander Motin 	if (start) {
493a157e425SAlexander Motin 		/* Initialize time machine parameters. */
494a157e425SAlexander Motin 		next = now;
495a157e425SAlexander Motin 		bintime_add(&next, &timerperiod);
496a157e425SAlexander Motin 		if (periodic)
497a157e425SAlexander Motin 			nexttick = next;
49843fe7d45SAlexander Motin 		else
499a157e425SAlexander Motin 			nexttick.sec = -1;
500a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
501a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
502a157e425SAlexander Motin 			state->now = now;
503a157e425SAlexander Motin 			state->nextevent = next;
504a157e425SAlexander Motin 			if (periodic)
505a157e425SAlexander Motin 				state->nexttick = next;
506a157e425SAlexander Motin 			else
507a157e425SAlexander Motin 				state->nexttick.sec = -1;
508a157e425SAlexander Motin 			state->nexthard = next;
509a157e425SAlexander Motin 			state->nextstat = next;
510a157e425SAlexander Motin 			state->nextprof = next;
511a157e425SAlexander Motin 			hardclock_sync(cpu);
512a157e425SAlexander Motin 		}
513a157e425SAlexander Motin 		busy = 0;
514a157e425SAlexander Motin 		/* Start global timer or per-CPU timer of this CPU. */
515a157e425SAlexander Motin 		loadtimer(&now, 1);
516a157e425SAlexander Motin 	} else {
517a157e425SAlexander Motin 		busy = 1;
518a157e425SAlexander Motin 		/* Stop global timer or per-CPU timer of this CPU. */
519a157e425SAlexander Motin 		et_stop(timer);
520a157e425SAlexander Motin 	}
521a157e425SAlexander Motin 	ET_HW_UNLOCK(DPCPU_PTR(timerstate));
52243fe7d45SAlexander Motin #ifdef SMP
523a157e425SAlexander Motin 	/* If timer is global or there is no other CPUs yet - we are done. */
524a157e425SAlexander Motin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) {
52543fe7d45SAlexander Motin 		critical_exit();
52643fe7d45SAlexander Motin 		return;
52743fe7d45SAlexander Motin 	}
52843fe7d45SAlexander Motin 	/* Set reconfigure flags for other CPUs. */
52943fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
530a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
531a157e425SAlexander Motin 		atomic_store_rel_int(&state->action,
532a157e425SAlexander Motin 		    (cpu == curcpu) ? 0 : ( start ? 1 : 2));
53343fe7d45SAlexander Motin 	}
534a157e425SAlexander Motin 	/* Broadcast reconfigure IPI. */
535a157e425SAlexander Motin 	ipi_all_but_self(IPI_HARDCLOCK);
53643fe7d45SAlexander Motin 	/* Wait for reconfiguration completed. */
53743fe7d45SAlexander Motin restart:
53843fe7d45SAlexander Motin 	cpu_spinwait();
53943fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
54043fe7d45SAlexander Motin 		if (cpu == curcpu)
54143fe7d45SAlexander Motin 			continue;
542a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
543a157e425SAlexander Motin 		if (atomic_load_acq_int(&state->action))
54443fe7d45SAlexander Motin 			goto restart;
54543fe7d45SAlexander Motin 	}
54643fe7d45SAlexander Motin #endif
547a157e425SAlexander Motin 	critical_exit();
54843fe7d45SAlexander Motin }
54943fe7d45SAlexander Motin 
550a157e425SAlexander Motin /*
551a157e425SAlexander Motin  * Calculate nearest frequency supported by hardware timer.
552a157e425SAlexander Motin  */
55351636352SAlexander Motin static int
55451636352SAlexander Motin round_freq(struct eventtimer *et, int freq)
55551636352SAlexander Motin {
55651636352SAlexander Motin 	uint64_t div;
55751636352SAlexander Motin 
55851636352SAlexander Motin 	if (et->et_frequency != 0) {
559599cf0f1SAlexander Motin 		div = lmax((et->et_frequency + freq / 2) / freq, 1);
56051636352SAlexander Motin 		if (et->et_flags & ET_FLAGS_POW2DIV)
56151636352SAlexander Motin 			div = 1 << (flsl(div + div / 2) - 1);
56251636352SAlexander Motin 		freq = (et->et_frequency + div / 2) / div;
56351636352SAlexander Motin 	}
56451636352SAlexander Motin 	if (et->et_min_period.sec > 0)
56551636352SAlexander Motin 		freq = 0;
566599cf0f1SAlexander Motin 	else if (et->et_min_period.frac != 0)
56751636352SAlexander Motin 		freq = min(freq, BT2FREQ(&et->et_min_period));
56851636352SAlexander Motin 	if (et->et_max_period.sec == 0 && et->et_max_period.frac != 0)
56951636352SAlexander Motin 		freq = max(freq, BT2FREQ(&et->et_max_period));
57051636352SAlexander Motin 	return (freq);
57151636352SAlexander Motin }
57251636352SAlexander Motin 
57343fe7d45SAlexander Motin /*
574a157e425SAlexander Motin  * Configure and start event timers (BSP part).
57543fe7d45SAlexander Motin  */
57643fe7d45SAlexander Motin void
57743fe7d45SAlexander Motin cpu_initclocks_bsp(void)
57843fe7d45SAlexander Motin {
579a157e425SAlexander Motin 	struct pcpu_state *state;
580a157e425SAlexander Motin 	int base, div, cpu;
58143fe7d45SAlexander Motin 
582a157e425SAlexander Motin 	mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
583a157e425SAlexander Motin 	CPU_FOREACH(cpu) {
584a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
585a157e425SAlexander Motin 		mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
586a157e425SAlexander Motin 	}
587a157e425SAlexander Motin #ifdef SMP
588a157e425SAlexander Motin 	callout_new_inserted = cpu_new_callout;
589a157e425SAlexander Motin #endif
590a157e425SAlexander Motin 	/* Grab requested timer or the best of present. */
591a157e425SAlexander Motin 	if (timername[0])
592a157e425SAlexander Motin 		timer = et_find(timername, 0, 0);
593a157e425SAlexander Motin 	if (timer == NULL && periodic) {
594a157e425SAlexander Motin 		timer = et_find(NULL,
59543fe7d45SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
596a157e425SAlexander Motin 	}
597a157e425SAlexander Motin 	if (timer == NULL) {
598a157e425SAlexander Motin 		timer = et_find(NULL,
599a157e425SAlexander Motin 		    ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT);
600a157e425SAlexander Motin 	}
601a157e425SAlexander Motin 	if (timer == NULL && !periodic) {
602a157e425SAlexander Motin 		timer = et_find(NULL,
603a157e425SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
604a157e425SAlexander Motin 	}
605a157e425SAlexander Motin 	if (timer == NULL)
606a157e425SAlexander Motin 		panic("No usable event timer found!");
607a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
608a157e425SAlexander Motin 
609a157e425SAlexander Motin 	/* Adapt to timer capabilities. */
610a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
611a157e425SAlexander Motin 		periodic = 0;
612a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
613a157e425SAlexander Motin 		periodic = 1;
614a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
615a157e425SAlexander Motin 		cpu_disable_deep_sleep++;
616a157e425SAlexander Motin 
61743fe7d45SAlexander Motin 	/*
61843fe7d45SAlexander Motin 	 * We honor the requested 'hz' value.
61943fe7d45SAlexander Motin 	 * We want to run stathz in the neighborhood of 128hz.
62043fe7d45SAlexander Motin 	 * We would like profhz to run as often as possible.
62143fe7d45SAlexander Motin 	 */
622dd9595e7SAlexander Motin 	if (singlemul <= 0 || singlemul > 20) {
62343fe7d45SAlexander Motin 		if (hz >= 1500 || (hz % 128) == 0)
62443fe7d45SAlexander Motin 			singlemul = 1;
62543fe7d45SAlexander Motin 		else if (hz >= 750)
62643fe7d45SAlexander Motin 			singlemul = 2;
62743fe7d45SAlexander Motin 		else
62843fe7d45SAlexander Motin 			singlemul = 4;
62943fe7d45SAlexander Motin 	}
630a157e425SAlexander Motin 	if (periodic) {
631a157e425SAlexander Motin 		base = round_freq(timer, hz * singlemul);
63251636352SAlexander Motin 		singlemul = max((base + hz / 2) / hz, 1);
63351636352SAlexander Motin 		hz = (base + singlemul / 2) / singlemul;
63451636352SAlexander Motin 		if (base <= 128)
63543fe7d45SAlexander Motin 			stathz = base;
63643fe7d45SAlexander Motin 		else {
63743fe7d45SAlexander Motin 			div = base / 128;
63851636352SAlexander Motin 			if (div >= singlemul && (div % singlemul) == 0)
63943fe7d45SAlexander Motin 				div++;
64043fe7d45SAlexander Motin 			stathz = base / div;
64143fe7d45SAlexander Motin 		}
64243fe7d45SAlexander Motin 		profhz = stathz;
64351636352SAlexander Motin 		while ((profhz + stathz) <= 128 * 64)
64443fe7d45SAlexander Motin 			profhz += stathz;
645a157e425SAlexander Motin 		profhz = round_freq(timer, profhz);
64643fe7d45SAlexander Motin 	} else {
647a157e425SAlexander Motin 		hz = round_freq(timer, hz);
648a157e425SAlexander Motin 		stathz = round_freq(timer, 127);
649a157e425SAlexander Motin 		profhz = round_freq(timer, stathz * 64);
65043fe7d45SAlexander Motin 	}
651599cf0f1SAlexander Motin 	tick = 1000000 / hz;
652a157e425SAlexander Motin 	FREQ2BT(hz, &hardperiod);
653a157e425SAlexander Motin 	FREQ2BT(stathz, &statperiod);
654a157e425SAlexander Motin 	FREQ2BT(profhz, &profperiod);
65543fe7d45SAlexander Motin 	ET_LOCK();
656a157e425SAlexander Motin 	configtimer(1);
65743fe7d45SAlexander Motin 	ET_UNLOCK();
65843fe7d45SAlexander Motin }
65943fe7d45SAlexander Motin 
660a157e425SAlexander Motin /*
661a157e425SAlexander Motin  * Start per-CPU event timers on APs.
662a157e425SAlexander Motin  */
66343fe7d45SAlexander Motin void
66443fe7d45SAlexander Motin cpu_initclocks_ap(void)
66543fe7d45SAlexander Motin {
666a157e425SAlexander Motin 	struct bintime now;
667a157e425SAlexander Motin 	struct pcpu_state *state;
66843fe7d45SAlexander Motin 
669a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_PERCPU) {
670a157e425SAlexander Motin 		state = DPCPU_PTR(timerstate);
671a157e425SAlexander Motin 		binuptime(&now);
672a157e425SAlexander Motin 		ET_HW_LOCK(state);
673a157e425SAlexander Motin 		loadtimer(&now, 1);
674a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
67543fe7d45SAlexander Motin 	}
67643fe7d45SAlexander Motin }
67743fe7d45SAlexander Motin 
678a157e425SAlexander Motin /*
679a157e425SAlexander Motin  * Switch to profiling clock rates.
680a157e425SAlexander Motin  */
68143fe7d45SAlexander Motin void
68243fe7d45SAlexander Motin cpu_startprofclock(void)
68343fe7d45SAlexander Motin {
68443fe7d45SAlexander Motin 
68543fe7d45SAlexander Motin 	ET_LOCK();
686a157e425SAlexander Motin 	if (periodic) {
687a157e425SAlexander Motin 		configtimer(0);
688a157e425SAlexander Motin 		profiling = 1;
689a157e425SAlexander Motin 		configtimer(1);
690a157e425SAlexander Motin 	} else
691a157e425SAlexander Motin 		profiling = 1;
69243fe7d45SAlexander Motin 	ET_UNLOCK();
69343fe7d45SAlexander Motin }
69443fe7d45SAlexander Motin 
695a157e425SAlexander Motin /*
696a157e425SAlexander Motin  * Switch to regular clock rates.
697a157e425SAlexander Motin  */
69843fe7d45SAlexander Motin void
69943fe7d45SAlexander Motin cpu_stopprofclock(void)
70043fe7d45SAlexander Motin {
70143fe7d45SAlexander Motin 
70243fe7d45SAlexander Motin 	ET_LOCK();
703a157e425SAlexander Motin 	if (periodic) {
704a157e425SAlexander Motin 		configtimer(0);
705a157e425SAlexander Motin 		profiling = 0;
706a157e425SAlexander Motin 		configtimer(1);
707a157e425SAlexander Motin 	} else
708a157e425SAlexander Motin 		profiling = 0;
70943fe7d45SAlexander Motin 	ET_UNLOCK();
71043fe7d45SAlexander Motin }
71143fe7d45SAlexander Motin 
712a157e425SAlexander Motin /*
713a157e425SAlexander Motin  * Switch to idle mode (all ticks handled).
714a157e425SAlexander Motin  */
715a157e425SAlexander Motin void
716a157e425SAlexander Motin cpu_idleclock(void)
717a157e425SAlexander Motin {
718a157e425SAlexander Motin 	struct bintime now, t;
719a157e425SAlexander Motin 	struct pcpu_state *state;
720a157e425SAlexander Motin 
721a157e425SAlexander Motin 	if (idletick || busy ||
722a157e425SAlexander Motin 	    (periodic && (timer->et_flags & ET_FLAGS_PERCPU)))
723a157e425SAlexander Motin 		return;
724a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
725a157e425SAlexander Motin 	if (periodic)
726a157e425SAlexander Motin 		now = state->now;
727a157e425SAlexander Motin 	else
728a157e425SAlexander Motin 		binuptime(&now);
729a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "idle at %d:    now  %d.%08x%08x",
730a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
731a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
732a157e425SAlexander Motin 	getnextcpuevent(&t, 1);
733a157e425SAlexander Motin 	ET_HW_LOCK(state);
734a157e425SAlexander Motin 	state->idle = 1;
735a157e425SAlexander Motin 	state->nextevent = t;
736a157e425SAlexander Motin 	if (!periodic)
737a157e425SAlexander Motin 		loadtimer(&now, 0);
738a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
739a157e425SAlexander Motin }
740a157e425SAlexander Motin 
741a157e425SAlexander Motin /*
742a157e425SAlexander Motin  * Switch to active mode (skip empty ticks).
743a157e425SAlexander Motin  */
744a157e425SAlexander Motin void
745a157e425SAlexander Motin cpu_activeclock(void)
746a157e425SAlexander Motin {
747a157e425SAlexander Motin 	struct bintime now;
748a157e425SAlexander Motin 	struct pcpu_state *state;
749a157e425SAlexander Motin 	struct thread *td;
750a157e425SAlexander Motin 
751a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
752a157e425SAlexander Motin 	if (state->idle == 0 || busy)
753a157e425SAlexander Motin 		return;
754a157e425SAlexander Motin 	if (periodic)
755a157e425SAlexander Motin 		now = state->now;
756a157e425SAlexander Motin 	else
757a157e425SAlexander Motin 		binuptime(&now);
758a157e425SAlexander Motin 	CTR4(KTR_SPARE2, "active at %d:  now  %d.%08x%08x",
759a157e425SAlexander Motin 	    curcpu, now.sec, (unsigned int)(now.frac >> 32),
760a157e425SAlexander Motin 			     (unsigned int)(now.frac & 0xffffffff));
761a157e425SAlexander Motin 	spinlock_enter();
762a157e425SAlexander Motin 	td = curthread;
763a157e425SAlexander Motin 	td->td_intr_nesting_level++;
764a157e425SAlexander Motin 	handleevents(&now, 1);
765a157e425SAlexander Motin 	td->td_intr_nesting_level--;
766a157e425SAlexander Motin 	spinlock_exit();
767a157e425SAlexander Motin }
768a157e425SAlexander Motin 
769a157e425SAlexander Motin #ifdef SMP
770a157e425SAlexander Motin static void
771a157e425SAlexander Motin cpu_new_callout(int cpu, int ticks)
772a157e425SAlexander Motin {
773a157e425SAlexander Motin 	struct bintime tmp;
774a157e425SAlexander Motin 	struct pcpu_state *state;
775a157e425SAlexander Motin 
776a157e425SAlexander Motin 	CTR3(KTR_SPARE2, "new co at %d:    on %d in %d",
777a157e425SAlexander Motin 	    curcpu, cpu, ticks);
778a157e425SAlexander Motin 	state = DPCPU_ID_PTR(cpu, timerstate);
779a157e425SAlexander Motin 	ET_HW_LOCK(state);
780a157e425SAlexander Motin 	if (state->idle == 0 || busy) {
781a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
782a157e425SAlexander Motin 		return;
783a157e425SAlexander Motin 	}
784a157e425SAlexander Motin 	/*
785a157e425SAlexander Motin 	 * If timer is periodic - just update next event time for target CPU.
786a157e425SAlexander Motin 	 */
787a157e425SAlexander Motin 	if (periodic) {
788a157e425SAlexander Motin 		state->nextevent = state->nexthard;
789a157e425SAlexander Motin 		tmp = hardperiod;
790a157e425SAlexander Motin 		bintime_mul(&tmp, ticks - 1);
791a157e425SAlexander Motin 		bintime_add(&state->nextevent, &tmp);
792a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
793a157e425SAlexander Motin 		return;
794a157e425SAlexander Motin 	}
795a157e425SAlexander Motin 	/*
796a157e425SAlexander Motin 	 * Otherwise we have to wake that CPU up, as we can't get present
797a157e425SAlexander Motin 	 * bintime to reprogram global timer from here. If timer is per-CPU,
798a157e425SAlexander Motin 	 * we by definition can't do it from here.
799a157e425SAlexander Motin 	 */
800a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
801a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_PERCPU) {
802a157e425SAlexander Motin 		state->handle = 1;
803a157e425SAlexander Motin 		ipi_cpu(cpu, IPI_HARDCLOCK);
804a157e425SAlexander Motin 	} else {
805a157e425SAlexander Motin 		if (!cpu_idle_wakeup(cpu))
806a157e425SAlexander Motin 			ipi_cpu(cpu, IPI_AST);
807a157e425SAlexander Motin 	}
808a157e425SAlexander Motin }
809a157e425SAlexander Motin #endif
810a157e425SAlexander Motin 
811a157e425SAlexander Motin /*
812a157e425SAlexander Motin  * Report or change the active event timers hardware.
813a157e425SAlexander Motin  */
81443fe7d45SAlexander Motin static int
815a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS)
81643fe7d45SAlexander Motin {
81743fe7d45SAlexander Motin 	char buf[32];
81843fe7d45SAlexander Motin 	struct eventtimer *et;
81943fe7d45SAlexander Motin 	int error;
82043fe7d45SAlexander Motin 
82143fe7d45SAlexander Motin 	ET_LOCK();
822a157e425SAlexander Motin 	et = timer;
82343fe7d45SAlexander Motin 	snprintf(buf, sizeof(buf), "%s", et->et_name);
82443fe7d45SAlexander Motin 	ET_UNLOCK();
82543fe7d45SAlexander Motin 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
82643fe7d45SAlexander Motin 	ET_LOCK();
827a157e425SAlexander Motin 	et = timer;
82843fe7d45SAlexander Motin 	if (error != 0 || req->newptr == NULL ||
829a157e425SAlexander Motin 	    strcasecmp(buf, et->et_name) == 0) {
83043fe7d45SAlexander Motin 		ET_UNLOCK();
83143fe7d45SAlexander Motin 		return (error);
83243fe7d45SAlexander Motin 	}
833a157e425SAlexander Motin 	et = et_find(buf, 0, 0);
83443fe7d45SAlexander Motin 	if (et == NULL) {
83543fe7d45SAlexander Motin 		ET_UNLOCK();
83643fe7d45SAlexander Motin 		return (ENOENT);
83743fe7d45SAlexander Motin 	}
83843fe7d45SAlexander Motin 	configtimer(0);
839a157e425SAlexander Motin 	et_free(timer);
840a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_C3STOP)
841a157e425SAlexander Motin 		cpu_disable_deep_sleep++;
842a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
843a157e425SAlexander Motin 		cpu_disable_deep_sleep--;
844a157e425SAlexander Motin 	timer = et;
845a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
84643fe7d45SAlexander Motin 	configtimer(1);
84743fe7d45SAlexander Motin 	ET_UNLOCK();
84843fe7d45SAlexander Motin 	return (error);
84943fe7d45SAlexander Motin }
850a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer,
85143fe7d45SAlexander Motin     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
852dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer");
853a157e425SAlexander Motin 
854a157e425SAlexander Motin /*
855a157e425SAlexander Motin  * Report or change the active event timer periodicity.
856a157e425SAlexander Motin  */
857a157e425SAlexander Motin static int
858a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS)
859a157e425SAlexander Motin {
860a157e425SAlexander Motin 	int error, val;
861a157e425SAlexander Motin 
862a157e425SAlexander Motin 	val = periodic;
863a157e425SAlexander Motin 	error = sysctl_handle_int(oidp, &val, 0, req);
864a157e425SAlexander Motin 	if (error != 0 || req->newptr == NULL)
865a157e425SAlexander Motin 		return (error);
866a157e425SAlexander Motin 	ET_LOCK();
867a157e425SAlexander Motin 	configtimer(0);
868a157e425SAlexander Motin 	periodic = val;
869a157e425SAlexander Motin 	configtimer(1);
870a157e425SAlexander Motin 	ET_UNLOCK();
871a157e425SAlexander Motin 	return (error);
872a157e425SAlexander Motin }
873a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic,
874a157e425SAlexander Motin     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
875dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode");
87643fe7d45SAlexander Motin 
87743fe7d45SAlexander Motin #endif
878