xref: /freebsd/sys/kern/kern_clocksource.c (revision efe67753ccbbbba0013dac241c79d2dc124d76a2)
143fe7d45SAlexander Motin /*-
25b999a6bSDavide Italiano  * Copyright (c) 2010-2013 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 
349dfc483cSAlexander Motin #include "opt_device_polling.h"
3543fe7d45SAlexander Motin 
3643fe7d45SAlexander Motin #include <sys/param.h>
3743fe7d45SAlexander Motin #include <sys/systm.h>
3843fe7d45SAlexander Motin #include <sys/bus.h>
395b999a6bSDavide Italiano #include <sys/limits.h>
4043fe7d45SAlexander Motin #include <sys/lock.h>
4143fe7d45SAlexander Motin #include <sys/kdb.h>
42a157e425SAlexander Motin #include <sys/ktr.h>
4343fe7d45SAlexander Motin #include <sys/mutex.h>
4443fe7d45SAlexander Motin #include <sys/proc.h>
4543fe7d45SAlexander Motin #include <sys/kernel.h>
4643fe7d45SAlexander Motin #include <sys/sched.h>
4743fe7d45SAlexander Motin #include <sys/smp.h>
4843fe7d45SAlexander Motin #include <sys/sysctl.h>
4943fe7d45SAlexander Motin #include <sys/timeet.h>
500e189873SAlexander Motin #include <sys/timetc.h>
5143fe7d45SAlexander Motin 
5243fe7d45SAlexander Motin #include <machine/atomic.h>
5343fe7d45SAlexander Motin #include <machine/clock.h>
5443fe7d45SAlexander Motin #include <machine/cpu.h>
5543fe7d45SAlexander Motin #include <machine/smp.h>
5643fe7d45SAlexander Motin 
5792597e06SJohn Baldwin int			cpu_disable_c2_sleep = 0; /* Timer dies in C2. */
5892597e06SJohn Baldwin int			cpu_disable_c3_sleep = 0; /* Timer dies in C3. */
5943fe7d45SAlexander Motin 
60a157e425SAlexander Motin static void		setuptimer(void);
615b999a6bSDavide Italiano static void		loadtimer(sbintime_t now, int first);
62a157e425SAlexander Motin static int		doconfigtimer(void);
63a157e425SAlexander Motin static void		configtimer(int start);
64a157e425SAlexander Motin static int		round_freq(struct eventtimer *et, int freq);
6543fe7d45SAlexander Motin 
665b999a6bSDavide Italiano static sbintime_t	getnextcpuevent(int idle);
675b999a6bSDavide Italiano static sbintime_t	getnextevent(void);
685b999a6bSDavide Italiano static int		handleevents(sbintime_t now, int fake);
6943fe7d45SAlexander Motin 
70a157e425SAlexander Motin static struct mtx	et_hw_mtx;
71a157e425SAlexander Motin 
72a157e425SAlexander Motin #define	ET_HW_LOCK(state)						\
73a157e425SAlexander Motin 	{								\
74a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
75a157e425SAlexander Motin 			mtx_lock_spin(&(state)->et_hw_mtx);		\
76a157e425SAlexander Motin 		else							\
77a157e425SAlexander Motin 			mtx_lock_spin(&et_hw_mtx);			\
78a157e425SAlexander Motin 	}
79a157e425SAlexander Motin 
80a157e425SAlexander Motin #define	ET_HW_UNLOCK(state)						\
81a157e425SAlexander Motin 	{								\
82a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
83a157e425SAlexander Motin 			mtx_unlock_spin(&(state)->et_hw_mtx);		\
84a157e425SAlexander Motin 		else							\
85a157e425SAlexander Motin 			mtx_unlock_spin(&et_hw_mtx);			\
86a157e425SAlexander Motin 	}
87a157e425SAlexander Motin 
88a157e425SAlexander Motin static struct eventtimer *timer = NULL;
895b999a6bSDavide Italiano static sbintime_t	timerperiod;	/* Timer period for periodic mode. */
905b999a6bSDavide Italiano static sbintime_t	statperiod;	/* statclock() events period. */
915b999a6bSDavide Italiano static sbintime_t	profperiod;	/* profclock() events period. */
925b999a6bSDavide Italiano static sbintime_t	nexttick;	/* Next global timer tick time. */
935b999a6bSDavide Italiano static u_int		busy = 1;	/* Reconfiguration is in progress. */
94af3b2549SHans Petter Selasky static int		profiling;	/* Profiling events enabled. */
95a157e425SAlexander Motin 
96a157e425SAlexander Motin static char		timername[32];	/* Wanted timer. */
97a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername));
98a157e425SAlexander Motin 
99af3b2549SHans Petter Selasky static int		singlemul;	/* Multiplier for periodic mode. */
100af3b2549SHans Petter Selasky SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RWTUN, &singlemul,
101a157e425SAlexander Motin     0, "Multiplier for periodic mode");
10243fe7d45SAlexander Motin 
103af3b2549SHans Petter Selasky static u_int		idletick;	/* Run periodic events when idle. */
104af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RWTUN, &idletick,
105a157e425SAlexander Motin     0, "Run periodic events when idle");
106a157e425SAlexander Motin 
107af3b2549SHans Petter Selasky static int		periodic;	/* Periodic or one-shot mode. */
108af3b2549SHans Petter Selasky static int		want_periodic;	/* What mode to prefer. */
109afe41f2dSAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &want_periodic);
110a157e425SAlexander Motin 
111a157e425SAlexander Motin struct pcpu_state {
112a157e425SAlexander Motin 	struct mtx	et_hw_mtx;	/* Per-CPU timer mutex. */
113a157e425SAlexander Motin 	u_int		action;		/* Reconfiguration requests. */
114a157e425SAlexander Motin 	u_int		handle;		/* Immediate handle resuests. */
1155b999a6bSDavide Italiano 	sbintime_t	now;		/* Last tick time. */
1165b999a6bSDavide Italiano 	sbintime_t	nextevent;	/* Next scheduled event on this CPU. */
1175b999a6bSDavide Italiano 	sbintime_t	nexttick;	/* Next timer tick time. */
118d3e2e28eSAlexander Motin 	sbintime_t	nexthard;	/* Next hardclock() event. */
1195b999a6bSDavide Italiano 	sbintime_t	nextstat;	/* Next statclock() event. */
1205b999a6bSDavide Italiano 	sbintime_t	nextprof;	/* Next profclock() event. */
1215b999a6bSDavide Italiano 	sbintime_t	nextcall;	/* Next callout event. */
1225b999a6bSDavide Italiano 	sbintime_t	nextcallopt;	/* Next optional callout event. */
123a157e425SAlexander Motin 	int		ipi;		/* This CPU needs IPI. */
124a157e425SAlexander Motin 	int		idle;		/* This CPU is in idle mode. */
125a157e425SAlexander Motin };
126a157e425SAlexander Motin 
1273e288e62SDimitry Andric static DPCPU_DEFINE(struct pcpu_state, timerstate);
1285b999a6bSDavide Italiano DPCPU_DEFINE(sbintime_t, hardclocktime);
129fdc5dd2dSAlexander Motin 
130a157e425SAlexander Motin /*
131a157e425SAlexander Motin  * Timer broadcast IPI handler.
132a157e425SAlexander Motin  */
133a157e425SAlexander Motin int
134a157e425SAlexander Motin hardclockintr(void)
13543fe7d45SAlexander Motin {
1365b999a6bSDavide Italiano 	sbintime_t now;
137a157e425SAlexander Motin 	struct pcpu_state *state;
138a157e425SAlexander Motin 	int done;
13943fe7d45SAlexander Motin 
140a157e425SAlexander Motin 	if (doconfigtimer() || busy)
141a157e425SAlexander Motin 		return (FILTER_HANDLED);
142a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
143a157e425SAlexander Motin 	now = state->now;
1445b999a6bSDavide Italiano 	CTR3(KTR_SPARE2, "ipi  at %d:    now  %d.%08x",
1455b999a6bSDavide Italiano 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
1465b999a6bSDavide Italiano 	done = handleevents(now, 0);
147a157e425SAlexander Motin 	return (done ? FILTER_HANDLED : FILTER_STRAY);
148a157e425SAlexander Motin }
149a157e425SAlexander Motin 
150a157e425SAlexander Motin /*
151a157e425SAlexander Motin  * Handle all events for specified time on this CPU
152a157e425SAlexander Motin  */
153a157e425SAlexander Motin static int
1545b999a6bSDavide Italiano handleevents(sbintime_t now, int fake)
155a157e425SAlexander Motin {
1565b999a6bSDavide Italiano 	sbintime_t t, *hct;
157a157e425SAlexander Motin 	struct trapframe *frame;
158a157e425SAlexander Motin 	struct pcpu_state *state;
159a157e425SAlexander Motin 	int usermode;
160a157e425SAlexander Motin 	int done, runs;
161a157e425SAlexander Motin 
1625b999a6bSDavide Italiano 	CTR3(KTR_SPARE2, "handle at %d:  now  %d.%08x",
1635b999a6bSDavide Italiano 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
164a157e425SAlexander Motin 	done = 0;
165a157e425SAlexander Motin 	if (fake) {
166a157e425SAlexander Motin 		frame = NULL;
167a157e425SAlexander Motin 		usermode = 0;
168a157e425SAlexander Motin 	} else {
169a157e425SAlexander Motin 		frame = curthread->td_intr_frame;
170a157e425SAlexander Motin 		usermode = TRAPF_USERMODE(frame);
171a157e425SAlexander Motin 	}
172dd7498aeSAndriy Gapon 
173a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
174dd7498aeSAndriy Gapon 
175bcfd016cSAlexander Motin 	runs = 0;
1765b999a6bSDavide Italiano 	while (now >= state->nexthard) {
1775b999a6bSDavide Italiano 		state->nexthard += tick_sbt;
178a157e425SAlexander Motin 		runs++;
17943fe7d45SAlexander Motin 	}
180c0722d20SAlexander Motin 	if (runs) {
1815b999a6bSDavide Italiano 		hct = DPCPU_PTR(hardclocktime);
1825b999a6bSDavide Italiano 		*hct = state->nexthard - tick_sbt;
183c0722d20SAlexander Motin 		if (fake < 2) {
184bcfd016cSAlexander Motin 			hardclock_cnt(runs, usermode);
185a157e425SAlexander Motin 			done = 1;
18643fe7d45SAlexander Motin 		}
187c0722d20SAlexander Motin 	}
188bcfd016cSAlexander Motin 	runs = 0;
1895b999a6bSDavide Italiano 	while (now >= state->nextstat) {
1905b999a6bSDavide Italiano 		state->nextstat += statperiod;
191bcfd016cSAlexander Motin 		runs++;
192bcfd016cSAlexander Motin 	}
193bcfd016cSAlexander Motin 	if (runs && fake < 2) {
194bcfd016cSAlexander Motin 		statclock_cnt(runs, usermode);
195a157e425SAlexander Motin 		done = 1;
19643fe7d45SAlexander Motin 	}
197a157e425SAlexander Motin 	if (profiling) {
198bcfd016cSAlexander Motin 		runs = 0;
1995b999a6bSDavide Italiano 		while (now >= state->nextprof) {
2005b999a6bSDavide Italiano 			state->nextprof += profperiod;
201bcfd016cSAlexander Motin 			runs++;
202bcfd016cSAlexander Motin 		}
203bcfd016cSAlexander Motin 		if (runs && !fake) {
2045b999a6bSDavide Italiano 			profclock_cnt(runs, usermode, TRAPF_PC(frame));
205a157e425SAlexander Motin 			done = 1;
20643fe7d45SAlexander Motin 		}
207a157e425SAlexander Motin 	} else
208a157e425SAlexander Motin 		state->nextprof = state->nextstat;
20910c87557SHans Petter Selasky 	if (now >= state->nextcallopt || now >= state->nextcall) {
2104bc38a5aSDavide Italiano 		state->nextcall = state->nextcallopt = SBT_MAX;
2115b999a6bSDavide Italiano 		callout_process(now);
2125b999a6bSDavide Italiano 	}
213dd7498aeSAndriy Gapon 
2145b999a6bSDavide Italiano 	t = getnextcpuevent(0);
215a157e425SAlexander Motin 	ET_HW_LOCK(state);
216a157e425SAlexander Motin 	if (!busy) {
217a157e425SAlexander Motin 		state->idle = 0;
218a157e425SAlexander Motin 		state->nextevent = t;
219e37e08c7SAlexander Motin 		loadtimer(now, (fake == 2) &&
220e37e08c7SAlexander Motin 		    (timer->et_flags & ET_FLAGS_PERCPU));
22143fe7d45SAlexander Motin 	}
222a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
223a157e425SAlexander Motin 	return (done);
22443fe7d45SAlexander Motin }
22543fe7d45SAlexander Motin 
22643fe7d45SAlexander Motin /*
227a157e425SAlexander Motin  * Schedule binuptime of the next event on current CPU.
22843fe7d45SAlexander Motin  */
2295b999a6bSDavide Italiano static sbintime_t
2305b999a6bSDavide Italiano getnextcpuevent(int idle)
23143fe7d45SAlexander Motin {
2325b999a6bSDavide Italiano 	sbintime_t event;
233a157e425SAlexander Motin 	struct pcpu_state *state;
2345b999a6bSDavide Italiano 	u_int hardfreq;
23543fe7d45SAlexander Motin 
236a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
2375b999a6bSDavide Italiano 	/* Handle hardclock() events, skipping some if CPU is idle. */
2385b999a6bSDavide Italiano 	event = state->nexthard;
2395b999a6bSDavide Italiano 	if (idle) {
2405b999a6bSDavide Italiano 		hardfreq = (u_int)hz / 2;
2415b999a6bSDavide Italiano 		if (tc_min_ticktock_freq > 2
2425b999a6bSDavide Italiano #ifdef SMP
2435b999a6bSDavide Italiano 		    && curcpu == CPU_FIRST()
2445b999a6bSDavide Italiano #endif
2455b999a6bSDavide Italiano 		    )
2465b999a6bSDavide Italiano 			hardfreq = hz / tc_min_ticktock_freq;
2475b999a6bSDavide Italiano 		if (hardfreq > 1)
2485b999a6bSDavide Italiano 			event += tick_sbt * (hardfreq - 1);
249fd053faeSAlexander Motin 	}
2505b999a6bSDavide Italiano 	/* Handle callout events. */
2515b999a6bSDavide Italiano 	if (event > state->nextcall)
2525b999a6bSDavide Italiano 		event = state->nextcall;
253fd053faeSAlexander Motin 	if (!idle) { /* If CPU is active - handle other types of events. */
2545b999a6bSDavide Italiano 		if (event > state->nextstat)
2555b999a6bSDavide Italiano 			event = state->nextstat;
2565b999a6bSDavide Italiano 		if (profiling && event > state->nextprof)
2575b999a6bSDavide Italiano 			event = state->nextprof;
25843fe7d45SAlexander Motin 	}
2595b999a6bSDavide Italiano 	return (event);
26043fe7d45SAlexander Motin }
261a157e425SAlexander Motin 
262a157e425SAlexander Motin /*
263a157e425SAlexander Motin  * Schedule binuptime of the next event on all CPUs.
264a157e425SAlexander Motin  */
2655b999a6bSDavide Italiano static sbintime_t
2665b999a6bSDavide Italiano getnextevent(void)
267a157e425SAlexander Motin {
268a157e425SAlexander Motin 	struct pcpu_state *state;
2695b999a6bSDavide Italiano 	sbintime_t event;
270a157e425SAlexander Motin #ifdef SMP
271a157e425SAlexander Motin 	int	cpu;
272a157e425SAlexander Motin #endif
2735b999a6bSDavide Italiano 	int	c;
274a157e425SAlexander Motin 
275a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
2765b999a6bSDavide Italiano 	event = state->nextevent;
2775b999a6bSDavide Italiano 	c = -1;
278fd053faeSAlexander Motin #ifdef SMP
2795b999a6bSDavide Italiano 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) {
280a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
281a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
2825b999a6bSDavide Italiano 			if (event > state->nextevent) {
2835b999a6bSDavide Italiano 				event = state->nextevent;
284a157e425SAlexander Motin 				c = cpu;
28543fe7d45SAlexander Motin 			}
28643fe7d45SAlexander Motin 		}
2872d7d1642SGrzegorz Bernacki 	}
288a157e425SAlexander Motin #endif
2895b999a6bSDavide Italiano 	CTR4(KTR_SPARE2, "next at %d:    next %d.%08x by %d",
2905b999a6bSDavide Italiano 	    curcpu, (int)(event >> 32), (u_int)(event & 0xffffffff), c);
2915b999a6bSDavide Italiano 	return (event);
292a157e425SAlexander Motin }
293a157e425SAlexander Motin 
294a157e425SAlexander Motin /* Hardware timer callback function. */
295a157e425SAlexander Motin static void
296a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg)
297a157e425SAlexander Motin {
2985b999a6bSDavide Italiano 	sbintime_t now;
2995b999a6bSDavide Italiano 	sbintime_t *next;
300a157e425SAlexander Motin 	struct pcpu_state *state;
301a157e425SAlexander Motin #ifdef SMP
302a157e425SAlexander Motin 	int cpu, bcast;
303a157e425SAlexander Motin #endif
304a157e425SAlexander Motin 
305a157e425SAlexander Motin 	/* Do not touch anything if somebody reconfiguring timers. */
306a157e425SAlexander Motin 	if (busy)
307a157e425SAlexander Motin 		return;
308a157e425SAlexander Motin 	/* Update present and next tick times. */
309a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
310a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_PERCPU) {
311a157e425SAlexander Motin 		next = &state->nexttick;
312a157e425SAlexander Motin 	} else
313a157e425SAlexander Motin 		next = &nexttick;
3145b999a6bSDavide Italiano 	now = sbinuptime();
3155b999a6bSDavide Italiano 	if (periodic)
3165b999a6bSDavide Italiano 		*next = now + timerperiod;
3175b999a6bSDavide Italiano 	else
3185b999a6bSDavide Italiano 		*next = -1;	/* Next tick is not scheduled yet. */
319a157e425SAlexander Motin 	state->now = now;
3205b999a6bSDavide Italiano 	CTR3(KTR_SPARE2, "intr at %d:    now  %d.%08x",
3215b999a6bSDavide Italiano 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
322a157e425SAlexander Motin 
323a157e425SAlexander Motin #ifdef SMP
324fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
325fdce57a0SJohn Baldwin 	MPASS(mp_ncpus == 1 || smp_started);
326fdce57a0SJohn Baldwin #endif
327a157e425SAlexander Motin 	/* Prepare broadcasting to other CPUs for non-per-CPU timers. */
328a157e425SAlexander Motin 	bcast = 0;
329fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
330fdce57a0SJohn Baldwin 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0) {
331fdce57a0SJohn Baldwin #else
332a157e425SAlexander Motin 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) {
333fdce57a0SJohn Baldwin #endif
334a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
335a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
336a157e425SAlexander Motin 			ET_HW_LOCK(state);
337a157e425SAlexander Motin 			state->now = now;
3385b999a6bSDavide Italiano 			if (now >= state->nextevent) {
3395b999a6bSDavide Italiano 				state->nextevent += SBT_1S;
3408e860de4SAlexander Motin 				if (curcpu != cpu) {
341a157e425SAlexander Motin 					state->ipi = 1;
342a157e425SAlexander Motin 					bcast = 1;
343a157e425SAlexander Motin 				}
3448e860de4SAlexander Motin 			}
345a157e425SAlexander Motin 			ET_HW_UNLOCK(state);
346a157e425SAlexander Motin 		}
347a157e425SAlexander Motin 	}
348a157e425SAlexander Motin #endif
349a157e425SAlexander Motin 
350a157e425SAlexander Motin 	/* Handle events for this time on this CPU. */
3515b999a6bSDavide Italiano 	handleevents(now, 0);
352a157e425SAlexander Motin 
353a157e425SAlexander Motin #ifdef SMP
354a157e425SAlexander Motin 	/* Broadcast interrupt to other CPUs for non-per-CPU timers. */
355a157e425SAlexander Motin 	if (bcast) {
356a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
357a157e425SAlexander Motin 			if (curcpu == cpu)
358a157e425SAlexander Motin 				continue;
359a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
360a157e425SAlexander Motin 			if (state->ipi) {
361a157e425SAlexander Motin 				state->ipi = 0;
362a157e425SAlexander Motin 				ipi_cpu(cpu, IPI_HARDCLOCK);
363a157e425SAlexander Motin 			}
364a157e425SAlexander Motin 		}
365a157e425SAlexander Motin 	}
366a157e425SAlexander Motin #endif
367a157e425SAlexander Motin }
368a157e425SAlexander Motin 
369a157e425SAlexander Motin /*
370a157e425SAlexander Motin  * Load new value into hardware timer.
371a157e425SAlexander Motin  */
372a157e425SAlexander Motin static void
3735b999a6bSDavide Italiano loadtimer(sbintime_t now, int start)
374a157e425SAlexander Motin {
375a157e425SAlexander Motin 	struct pcpu_state *state;
3765b999a6bSDavide Italiano 	sbintime_t new;
3775b999a6bSDavide Italiano 	sbintime_t *next;
378a157e425SAlexander Motin 	uint64_t tmp;
379a157e425SAlexander Motin 	int eq;
380a157e425SAlexander Motin 
381c70410e6SAlexander Motin 	if (timer->et_flags & ET_FLAGS_PERCPU) {
382c70410e6SAlexander Motin 		state = DPCPU_PTR(timerstate);
383c70410e6SAlexander Motin 		next = &state->nexttick;
384c70410e6SAlexander Motin 	} else
385c70410e6SAlexander Motin 		next = &nexttick;
386a157e425SAlexander Motin 	if (periodic) {
387a157e425SAlexander Motin 		if (start) {
388a157e425SAlexander Motin 			/*
389a157e425SAlexander Motin 			 * Try to start all periodic timers aligned
390a157e425SAlexander Motin 			 * to period to make events synchronous.
391a157e425SAlexander Motin 			 */
3925b999a6bSDavide Italiano 			tmp = now % timerperiod;
3935b999a6bSDavide Italiano 			new = timerperiod - tmp;
3945b999a6bSDavide Italiano 			if (new < tmp)		/* Left less then passed. */
3955b999a6bSDavide Italiano 				new += timerperiod;
396a157e425SAlexander Motin 			CTR5(KTR_SPARE2, "load p at %d:   now %d.%08x first in %d.%08x",
3975b999a6bSDavide Italiano 			    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff),
3985b999a6bSDavide Italiano 			    (int)(new >> 32), (u_int)(new & 0xffffffff));
3995b999a6bSDavide Italiano 			*next = new + now;
4005b999a6bSDavide Italiano 			et_start(timer, new, timerperiod);
401a157e425SAlexander Motin 		}
402a157e425SAlexander Motin 	} else {
4035b999a6bSDavide Italiano 		new = getnextevent();
4045b999a6bSDavide Italiano 		eq = (new == *next);
4055b999a6bSDavide Italiano 		CTR4(KTR_SPARE2, "load at %d:    next %d.%08x eq %d",
4065b999a6bSDavide Italiano 		    curcpu, (int)(new >> 32), (u_int)(new & 0xffffffff), eq);
407a157e425SAlexander Motin 		if (!eq) {
408a157e425SAlexander Motin 			*next = new;
4095b999a6bSDavide Italiano 			et_start(timer, new - now, 0);
410a157e425SAlexander Motin 		}
411a157e425SAlexander Motin 	}
412a157e425SAlexander Motin }
413a157e425SAlexander Motin 
414a157e425SAlexander Motin /*
415a157e425SAlexander Motin  * Prepare event timer parameters after configuration changes.
416a157e425SAlexander Motin  */
417a157e425SAlexander Motin static void
418a157e425SAlexander Motin setuptimer(void)
419a157e425SAlexander Motin {
420a157e425SAlexander Motin 	int freq;
421a157e425SAlexander Motin 
422a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
423a157e425SAlexander Motin 		periodic = 0;
424a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
425a157e425SAlexander Motin 		periodic = 1;
426dd9595e7SAlexander Motin 	singlemul = MIN(MAX(singlemul, 1), 20);
427a157e425SAlexander Motin 	freq = hz * singlemul;
428a157e425SAlexander Motin 	while (freq < (profiling ? profhz : stathz))
429a157e425SAlexander Motin 		freq += hz;
430a157e425SAlexander Motin 	freq = round_freq(timer, freq);
4315b999a6bSDavide Italiano 	timerperiod = SBT_1S / freq;
432a157e425SAlexander Motin }
43343fe7d45SAlexander Motin 
43443fe7d45SAlexander Motin /*
43543fe7d45SAlexander Motin  * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler.
43643fe7d45SAlexander Motin  */
437a157e425SAlexander Motin static int
438a157e425SAlexander Motin doconfigtimer(void)
43943fe7d45SAlexander Motin {
4405b999a6bSDavide Italiano 	sbintime_t now;
441a157e425SAlexander Motin 	struct pcpu_state *state;
44243fe7d45SAlexander Motin 
443a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
444a157e425SAlexander Motin 	switch (atomic_load_acq_int(&state->action)) {
445a157e425SAlexander Motin 	case 1:
4465b999a6bSDavide Italiano 		now = sbinuptime();
447a157e425SAlexander Motin 		ET_HW_LOCK(state);
4485b999a6bSDavide Italiano 		loadtimer(now, 1);
449a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
450a157e425SAlexander Motin 		state->handle = 0;
451a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
452a157e425SAlexander Motin 		return (1);
453a157e425SAlexander Motin 	case 2:
454a157e425SAlexander Motin 		ET_HW_LOCK(state);
455a157e425SAlexander Motin 		et_stop(timer);
456a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
457a157e425SAlexander Motin 		state->handle = 0;
458a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
459a157e425SAlexander Motin 		return (1);
460a157e425SAlexander Motin 	}
461a157e425SAlexander Motin 	if (atomic_readandclear_int(&state->handle) && !busy) {
4625b999a6bSDavide Italiano 		now = sbinuptime();
4635b999a6bSDavide Italiano 		handleevents(now, 0);
46443fe7d45SAlexander Motin 		return (1);
46543fe7d45SAlexander Motin 	}
46643fe7d45SAlexander Motin 	return (0);
46743fe7d45SAlexander Motin }
46843fe7d45SAlexander Motin 
46943fe7d45SAlexander Motin /*
47043fe7d45SAlexander Motin  * Reconfigure specified timer.
47143fe7d45SAlexander Motin  * For per-CPU timers use IPI to make other CPUs to reconfigure.
47243fe7d45SAlexander Motin  */
47343fe7d45SAlexander Motin static void
474a157e425SAlexander Motin configtimer(int start)
47543fe7d45SAlexander Motin {
4765b999a6bSDavide Italiano 	sbintime_t now, next;
477a157e425SAlexander Motin 	struct pcpu_state *state;
47843fe7d45SAlexander Motin 	int cpu;
47943fe7d45SAlexander Motin 
480a157e425SAlexander Motin 	if (start) {
481a157e425SAlexander Motin 		setuptimer();
4825b999a6bSDavide Italiano 		now = sbinuptime();
4835b999a6bSDavide Italiano 	} else
4845b999a6bSDavide Italiano 		now = 0;
48543fe7d45SAlexander Motin 	critical_enter();
486a157e425SAlexander Motin 	ET_HW_LOCK(DPCPU_PTR(timerstate));
487a157e425SAlexander Motin 	if (start) {
488a157e425SAlexander Motin 		/* Initialize time machine parameters. */
4895b999a6bSDavide Italiano 		next = now + timerperiod;
490a157e425SAlexander Motin 		if (periodic)
491a157e425SAlexander Motin 			nexttick = next;
49243fe7d45SAlexander Motin 		else
4935b999a6bSDavide Italiano 			nexttick = -1;
494fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
495fdce57a0SJohn Baldwin 		MPASS(mp_ncpus == 1 || smp_started);
496fdce57a0SJohn Baldwin #endif
497a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
498a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
499a157e425SAlexander Motin 			state->now = now;
500fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP
5015b999a6bSDavide Italiano 			if (!smp_started && cpu != CPU_FIRST())
5024bc38a5aSDavide Italiano 				state->nextevent = SBT_MAX;
5035b999a6bSDavide Italiano 			else
504fdce57a0SJohn Baldwin #endif
505a157e425SAlexander Motin 				state->nextevent = next;
506a157e425SAlexander Motin 			if (periodic)
507a157e425SAlexander Motin 				state->nexttick = next;
508a157e425SAlexander Motin 			else
5095b999a6bSDavide Italiano 				state->nexttick = -1;
510a157e425SAlexander Motin 			state->nexthard = next;
511a157e425SAlexander Motin 			state->nextstat = next;
512a157e425SAlexander Motin 			state->nextprof = next;
5135b999a6bSDavide Italiano 			state->nextcall = next;
5145b999a6bSDavide Italiano 			state->nextcallopt = next;
515a157e425SAlexander Motin 			hardclock_sync(cpu);
516a157e425SAlexander Motin 		}
517a157e425SAlexander Motin 		busy = 0;
518a157e425SAlexander Motin 		/* Start global timer or per-CPU timer of this CPU. */
5195b999a6bSDavide Italiano 		loadtimer(now, 1);
520a157e425SAlexander Motin 	} else {
521a157e425SAlexander Motin 		busy = 1;
522a157e425SAlexander Motin 		/* Stop global timer or per-CPU timer of this CPU. */
523a157e425SAlexander Motin 		et_stop(timer);
524a157e425SAlexander Motin 	}
525a157e425SAlexander Motin 	ET_HW_UNLOCK(DPCPU_PTR(timerstate));
52643fe7d45SAlexander Motin #ifdef SMP
527fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
528fdce57a0SJohn Baldwin 	/* If timer is global we are done. */
529fdce57a0SJohn Baldwin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) {
530fdce57a0SJohn Baldwin #else
531a157e425SAlexander Motin 	/* If timer is global or there is no other CPUs yet - we are done. */
532a157e425SAlexander Motin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) {
533fdce57a0SJohn Baldwin #endif
53443fe7d45SAlexander Motin 		critical_exit();
53543fe7d45SAlexander Motin 		return;
53643fe7d45SAlexander Motin 	}
53743fe7d45SAlexander Motin 	/* Set reconfigure flags for other CPUs. */
53843fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
539a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
540a157e425SAlexander Motin 		atomic_store_rel_int(&state->action,
541a157e425SAlexander Motin 		    (cpu == curcpu) ? 0 : ( start ? 1 : 2));
54243fe7d45SAlexander Motin 	}
543a157e425SAlexander Motin 	/* Broadcast reconfigure IPI. */
544a157e425SAlexander Motin 	ipi_all_but_self(IPI_HARDCLOCK);
54543fe7d45SAlexander Motin 	/* Wait for reconfiguration completed. */
54643fe7d45SAlexander Motin restart:
54743fe7d45SAlexander Motin 	cpu_spinwait();
54843fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
54943fe7d45SAlexander Motin 		if (cpu == curcpu)
55043fe7d45SAlexander Motin 			continue;
551a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
552a157e425SAlexander Motin 		if (atomic_load_acq_int(&state->action))
55343fe7d45SAlexander Motin 			goto restart;
55443fe7d45SAlexander Motin 	}
55543fe7d45SAlexander Motin #endif
556a157e425SAlexander Motin 	critical_exit();
55743fe7d45SAlexander Motin }
55843fe7d45SAlexander Motin 
559a157e425SAlexander Motin /*
560a157e425SAlexander Motin  * Calculate nearest frequency supported by hardware timer.
561a157e425SAlexander Motin  */
56251636352SAlexander Motin static int
56351636352SAlexander Motin round_freq(struct eventtimer *et, int freq)
56451636352SAlexander Motin {
56551636352SAlexander Motin 	uint64_t div;
56651636352SAlexander Motin 
56751636352SAlexander Motin 	if (et->et_frequency != 0) {
568599cf0f1SAlexander Motin 		div = lmax((et->et_frequency + freq / 2) / freq, 1);
56951636352SAlexander Motin 		if (et->et_flags & ET_FLAGS_POW2DIV)
57051636352SAlexander Motin 			div = 1 << (flsl(div + div / 2) - 1);
57151636352SAlexander Motin 		freq = (et->et_frequency + div / 2) / div;
57251636352SAlexander Motin 	}
573fdc5dd2dSAlexander Motin 	if (et->et_min_period > SBT_1S)
574803a9b3eSAlexander Motin 		panic("Event timer \"%s\" doesn't support sub-second periods!",
575803a9b3eSAlexander Motin 		    et->et_name);
576fdc5dd2dSAlexander Motin 	else if (et->et_min_period != 0)
577fdc5dd2dSAlexander Motin 		freq = min(freq, SBT2FREQ(et->et_min_period));
578fdc5dd2dSAlexander Motin 	if (et->et_max_period < SBT_1S && et->et_max_period != 0)
579fdc5dd2dSAlexander Motin 		freq = max(freq, SBT2FREQ(et->et_max_period));
58051636352SAlexander Motin 	return (freq);
58151636352SAlexander Motin }
58251636352SAlexander Motin 
58343fe7d45SAlexander Motin /*
584a157e425SAlexander Motin  * Configure and start event timers (BSP part).
58543fe7d45SAlexander Motin  */
58643fe7d45SAlexander Motin void
58743fe7d45SAlexander Motin cpu_initclocks_bsp(void)
58843fe7d45SAlexander Motin {
589a157e425SAlexander Motin 	struct pcpu_state *state;
590a157e425SAlexander Motin 	int base, div, cpu;
59143fe7d45SAlexander Motin 
592a157e425SAlexander Motin 	mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
593a157e425SAlexander Motin 	CPU_FOREACH(cpu) {
594a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
595a157e425SAlexander Motin 		mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
5964bc38a5aSDavide Italiano 		state->nextcall = SBT_MAX;
5974bc38a5aSDavide Italiano 		state->nextcallopt = SBT_MAX;
598a157e425SAlexander Motin 	}
599afe41f2dSAlexander Motin 	periodic = want_periodic;
600a157e425SAlexander Motin 	/* Grab requested timer or the best of present. */
601a157e425SAlexander Motin 	if (timername[0])
602a157e425SAlexander Motin 		timer = et_find(timername, 0, 0);
603a157e425SAlexander Motin 	if (timer == NULL && periodic) {
604a157e425SAlexander Motin 		timer = et_find(NULL,
60543fe7d45SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
606a157e425SAlexander Motin 	}
607a157e425SAlexander Motin 	if (timer == NULL) {
608a157e425SAlexander Motin 		timer = et_find(NULL,
609a157e425SAlexander Motin 		    ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT);
610a157e425SAlexander Motin 	}
611a157e425SAlexander Motin 	if (timer == NULL && !periodic) {
612a157e425SAlexander Motin 		timer = et_find(NULL,
613a157e425SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
614a157e425SAlexander Motin 	}
615a157e425SAlexander Motin 	if (timer == NULL)
616a157e425SAlexander Motin 		panic("No usable event timer found!");
617a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
618a157e425SAlexander Motin 
619a157e425SAlexander Motin 	/* Adapt to timer capabilities. */
620a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
621a157e425SAlexander Motin 		periodic = 0;
622a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
623a157e425SAlexander Motin 		periodic = 1;
624a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
62592597e06SJohn Baldwin 		cpu_disable_c3_sleep++;
626a157e425SAlexander Motin 
62743fe7d45SAlexander Motin 	/*
62843fe7d45SAlexander Motin 	 * We honor the requested 'hz' value.
62943fe7d45SAlexander Motin 	 * We want to run stathz in the neighborhood of 128hz.
63043fe7d45SAlexander Motin 	 * We would like profhz to run as often as possible.
63143fe7d45SAlexander Motin 	 */
632dd9595e7SAlexander Motin 	if (singlemul <= 0 || singlemul > 20) {
63343fe7d45SAlexander Motin 		if (hz >= 1500 || (hz % 128) == 0)
63443fe7d45SAlexander Motin 			singlemul = 1;
63543fe7d45SAlexander Motin 		else if (hz >= 750)
63643fe7d45SAlexander Motin 			singlemul = 2;
63743fe7d45SAlexander Motin 		else
63843fe7d45SAlexander Motin 			singlemul = 4;
63943fe7d45SAlexander Motin 	}
640a157e425SAlexander Motin 	if (periodic) {
641a157e425SAlexander Motin 		base = round_freq(timer, hz * singlemul);
64251636352SAlexander Motin 		singlemul = max((base + hz / 2) / hz, 1);
64351636352SAlexander Motin 		hz = (base + singlemul / 2) / singlemul;
64451636352SAlexander Motin 		if (base <= 128)
64543fe7d45SAlexander Motin 			stathz = base;
64643fe7d45SAlexander Motin 		else {
64743fe7d45SAlexander Motin 			div = base / 128;
64851636352SAlexander Motin 			if (div >= singlemul && (div % singlemul) == 0)
64943fe7d45SAlexander Motin 				div++;
65043fe7d45SAlexander Motin 			stathz = base / div;
65143fe7d45SAlexander Motin 		}
65243fe7d45SAlexander Motin 		profhz = stathz;
65351636352SAlexander Motin 		while ((profhz + stathz) <= 128 * 64)
65443fe7d45SAlexander Motin 			profhz += stathz;
655a157e425SAlexander Motin 		profhz = round_freq(timer, profhz);
65643fe7d45SAlexander Motin 	} else {
657a157e425SAlexander Motin 		hz = round_freq(timer, hz);
658a157e425SAlexander Motin 		stathz = round_freq(timer, 127);
659a157e425SAlexander Motin 		profhz = round_freq(timer, stathz * 64);
66043fe7d45SAlexander Motin 	}
661599cf0f1SAlexander Motin 	tick = 1000000 / hz;
6625b999a6bSDavide Italiano 	tick_sbt = SBT_1S / hz;
6635b999a6bSDavide Italiano 	tick_bt = sbttobt(tick_sbt);
6645b999a6bSDavide Italiano 	statperiod = SBT_1S / stathz;
6655b999a6bSDavide Italiano 	profperiod = SBT_1S / profhz;
66643fe7d45SAlexander Motin 	ET_LOCK();
667a157e425SAlexander Motin 	configtimer(1);
66843fe7d45SAlexander Motin 	ET_UNLOCK();
66943fe7d45SAlexander Motin }
67043fe7d45SAlexander Motin 
671a157e425SAlexander Motin /*
672a157e425SAlexander Motin  * Start per-CPU event timers on APs.
673a157e425SAlexander Motin  */
67443fe7d45SAlexander Motin void
67543fe7d45SAlexander Motin cpu_initclocks_ap(void)
67643fe7d45SAlexander Motin {
6775b999a6bSDavide Italiano 	sbintime_t now;
678a157e425SAlexander Motin 	struct pcpu_state *state;
6795b999a6bSDavide Italiano 	struct thread *td;
68043fe7d45SAlexander Motin 
681a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
6825b999a6bSDavide Italiano 	now = sbinuptime();
683a157e425SAlexander Motin 	ET_HW_LOCK(state);
684c70410e6SAlexander Motin 	state->now = now;
685c70410e6SAlexander Motin 	hardclock_sync(curcpu);
6865b999a6bSDavide Italiano 	spinlock_enter();
687a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
6885b999a6bSDavide Italiano 	td = curthread;
6895b999a6bSDavide Italiano 	td->td_intr_nesting_level++;
6905b999a6bSDavide Italiano 	handleevents(state->now, 2);
6915b999a6bSDavide Italiano 	td->td_intr_nesting_level--;
6925b999a6bSDavide Italiano 	spinlock_exit();
69343fe7d45SAlexander Motin }
69443fe7d45SAlexander Motin 
695a157e425SAlexander Motin /*
696a157e425SAlexander Motin  * Switch to profiling clock rates.
697a157e425SAlexander Motin  */
69843fe7d45SAlexander Motin void
69943fe7d45SAlexander Motin cpu_startprofclock(void)
70043fe7d45SAlexander Motin {
70143fe7d45SAlexander Motin 
70243fe7d45SAlexander Motin 	ET_LOCK();
7031af19ee4SAlexander Motin 	if (profiling == 0) {
704a157e425SAlexander Motin 		if (periodic) {
705a157e425SAlexander Motin 			configtimer(0);
706a157e425SAlexander Motin 			profiling = 1;
707a157e425SAlexander Motin 			configtimer(1);
708a157e425SAlexander Motin 		} else
709a157e425SAlexander Motin 			profiling = 1;
7101af19ee4SAlexander Motin 	} else
7111af19ee4SAlexander Motin 		profiling++;
71243fe7d45SAlexander Motin 	ET_UNLOCK();
71343fe7d45SAlexander Motin }
71443fe7d45SAlexander Motin 
715a157e425SAlexander Motin /*
716a157e425SAlexander Motin  * Switch to regular clock rates.
717a157e425SAlexander Motin  */
71843fe7d45SAlexander Motin void
71943fe7d45SAlexander Motin cpu_stopprofclock(void)
72043fe7d45SAlexander Motin {
72143fe7d45SAlexander Motin 
72243fe7d45SAlexander Motin 	ET_LOCK();
7231af19ee4SAlexander Motin 	if (profiling == 1) {
724a157e425SAlexander Motin 		if (periodic) {
725a157e425SAlexander Motin 			configtimer(0);
726a157e425SAlexander Motin 			profiling = 0;
727a157e425SAlexander Motin 			configtimer(1);
728a157e425SAlexander Motin 		} else
729a157e425SAlexander Motin 		profiling = 0;
7301af19ee4SAlexander Motin 	} else
7311af19ee4SAlexander Motin 		profiling--;
73243fe7d45SAlexander Motin 	ET_UNLOCK();
73343fe7d45SAlexander Motin }
73443fe7d45SAlexander Motin 
735a157e425SAlexander Motin /*
736a157e425SAlexander Motin  * Switch to idle mode (all ticks handled).
737a157e425SAlexander Motin  */
738acccf7d8SDavide Italiano sbintime_t
739a157e425SAlexander Motin cpu_idleclock(void)
740a157e425SAlexander Motin {
7415b999a6bSDavide Italiano 	sbintime_t now, t;
742a157e425SAlexander Motin 	struct pcpu_state *state;
743a157e425SAlexander Motin 
744a157e425SAlexander Motin 	if (idletick || busy ||
7459dfc483cSAlexander Motin 	    (periodic && (timer->et_flags & ET_FLAGS_PERCPU))
7469dfc483cSAlexander Motin #ifdef DEVICE_POLLING
7479dfc483cSAlexander Motin 	    || curcpu == CPU_FIRST()
7489dfc483cSAlexander Motin #endif
7499dfc483cSAlexander Motin 	    )
750acccf7d8SDavide Italiano 		return (-1);
751a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
752a157e425SAlexander Motin 	if (periodic)
753a157e425SAlexander Motin 		now = state->now;
754a157e425SAlexander Motin 	else
7555b999a6bSDavide Italiano 		now = sbinuptime();
7565b999a6bSDavide Italiano 	CTR3(KTR_SPARE2, "idle at %d:    now  %d.%08x",
7575b999a6bSDavide Italiano 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
7585b999a6bSDavide Italiano 	t = getnextcpuevent(1);
759a157e425SAlexander Motin 	ET_HW_LOCK(state);
760a157e425SAlexander Motin 	state->idle = 1;
761a157e425SAlexander Motin 	state->nextevent = t;
762a157e425SAlexander Motin 	if (!periodic)
7635b999a6bSDavide Italiano 		loadtimer(now, 0);
764a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
7655b999a6bSDavide Italiano 	return (MAX(t - now, 0));
766a157e425SAlexander Motin }
767a157e425SAlexander Motin 
768a157e425SAlexander Motin /*
769a157e425SAlexander Motin  * Switch to active mode (skip empty ticks).
770a157e425SAlexander Motin  */
771a157e425SAlexander Motin void
772a157e425SAlexander Motin cpu_activeclock(void)
773a157e425SAlexander Motin {
7745b999a6bSDavide Italiano 	sbintime_t now;
775a157e425SAlexander Motin 	struct pcpu_state *state;
776a157e425SAlexander Motin 	struct thread *td;
777a157e425SAlexander Motin 
778a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
779a157e425SAlexander Motin 	if (state->idle == 0 || busy)
780a157e425SAlexander Motin 		return;
781a157e425SAlexander Motin 	if (periodic)
782a157e425SAlexander Motin 		now = state->now;
783a157e425SAlexander Motin 	else
7845b999a6bSDavide Italiano 		now = sbinuptime();
7855b999a6bSDavide Italiano 	CTR3(KTR_SPARE2, "active at %d:  now  %d.%08x",
7865b999a6bSDavide Italiano 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
787a157e425SAlexander Motin 	spinlock_enter();
788a157e425SAlexander Motin 	td = curthread;
789a157e425SAlexander Motin 	td->td_intr_nesting_level++;
7905b999a6bSDavide Italiano 	handleevents(now, 1);
791a157e425SAlexander Motin 	td->td_intr_nesting_level--;
792a157e425SAlexander Motin 	spinlock_exit();
793a157e425SAlexander Motin }
794a157e425SAlexander Motin 
795cfc4b56bSIan Lepore /*
796cfc4b56bSIan Lepore  * Change the frequency of the given timer.  This changes et->et_frequency and
797cfc4b56bSIan Lepore  * if et is the active timer it reconfigures the timer on all CPUs.  This is
798cfc4b56bSIan Lepore  * intended to be a private interface for the use of et_change_frequency() only.
799cfc4b56bSIan Lepore  */
800cfc4b56bSIan Lepore void
801cfc4b56bSIan Lepore cpu_et_frequency(struct eventtimer *et, uint64_t newfreq)
802cfc4b56bSIan Lepore {
803cfc4b56bSIan Lepore 
804cfc4b56bSIan Lepore 	ET_LOCK();
805cfc4b56bSIan Lepore 	if (et == timer) {
806cfc4b56bSIan Lepore 		configtimer(0);
807cfc4b56bSIan Lepore 		et->et_frequency = newfreq;
808cfc4b56bSIan Lepore 		configtimer(1);
809cfc4b56bSIan Lepore 	} else
810cfc4b56bSIan Lepore 		et->et_frequency = newfreq;
811cfc4b56bSIan Lepore 	ET_UNLOCK();
812cfc4b56bSIan Lepore }
813cfc4b56bSIan Lepore 
8145b999a6bSDavide Italiano void
8155b999a6bSDavide Italiano cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt)
816a157e425SAlexander Motin {
817a157e425SAlexander Motin 	struct pcpu_state *state;
818a157e425SAlexander Motin 
8195b999a6bSDavide Italiano 	/* Do not touch anything if somebody reconfiguring timers. */
8205b999a6bSDavide Italiano 	if (busy)
8215b999a6bSDavide Italiano 		return;
8225b999a6bSDavide Italiano 	CTR6(KTR_SPARE2, "new co at %d:    on %d at %d.%08x - %d.%08x",
8235b999a6bSDavide Italiano 	    curcpu, cpu, (int)(bt_opt >> 32), (u_int)(bt_opt & 0xffffffff),
8245b999a6bSDavide Italiano 	    (int)(bt >> 32), (u_int)(bt & 0xffffffff));
825*efe67753SNathan Whitehorn 
826*efe67753SNathan Whitehorn 	KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu));
827a157e425SAlexander Motin 	state = DPCPU_ID_PTR(cpu, timerstate);
828a157e425SAlexander Motin 	ET_HW_LOCK(state);
8295b999a6bSDavide Italiano 
8305b999a6bSDavide Italiano 	/*
8315b999a6bSDavide Italiano 	 * If there is callout time already set earlier -- do nothing.
8325b999a6bSDavide Italiano 	 * This check may appear redundant because we check already in
8335b999a6bSDavide Italiano 	 * callout_process() but this double check guarantees we're safe
8345b999a6bSDavide Italiano 	 * with respect to race conditions between interrupts execution
8355b999a6bSDavide Italiano 	 * and scheduling.
8365b999a6bSDavide Italiano 	 */
8375b999a6bSDavide Italiano 	state->nextcallopt = bt_opt;
8385b999a6bSDavide Italiano 	if (bt >= state->nextcall)
8395b999a6bSDavide Italiano 		goto done;
8405b999a6bSDavide Italiano 	state->nextcall = bt;
8415b999a6bSDavide Italiano 	/* If there is some other event set earlier -- do nothing. */
8425b999a6bSDavide Italiano 	if (bt >= state->nextevent)
8435b999a6bSDavide Italiano 		goto done;
8445b999a6bSDavide Italiano 	state->nextevent = bt;
8455b999a6bSDavide Italiano 	/* If timer is periodic -- there is nothing to reprogram. */
8465b999a6bSDavide Italiano 	if (periodic)
8475b999a6bSDavide Italiano 		goto done;
8485b999a6bSDavide Italiano 	/* If timer is global or of the current CPU -- reprogram it. */
8495b999a6bSDavide Italiano 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || cpu == curcpu) {
8505b999a6bSDavide Italiano 		loadtimer(sbinuptime(), 0);
8515b999a6bSDavide Italiano done:
852a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
853a157e425SAlexander Motin 		return;
854a157e425SAlexander Motin 	}
8555b999a6bSDavide Italiano 	/* Otherwise make other CPU to reprogram it. */
856a157e425SAlexander Motin 	state->handle = 1;
8575b999a6bSDavide Italiano 	ET_HW_UNLOCK(state);
8585b999a6bSDavide Italiano #ifdef SMP
859a157e425SAlexander Motin 	ipi_cpu(cpu, IPI_HARDCLOCK);
860a157e425SAlexander Motin #endif
8615b999a6bSDavide Italiano }
862a157e425SAlexander Motin 
863a157e425SAlexander Motin /*
864a157e425SAlexander Motin  * Report or change the active event timers hardware.
865a157e425SAlexander Motin  */
86643fe7d45SAlexander Motin static int
867a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS)
86843fe7d45SAlexander Motin {
86943fe7d45SAlexander Motin 	char buf[32];
87043fe7d45SAlexander Motin 	struct eventtimer *et;
87143fe7d45SAlexander Motin 	int error;
87243fe7d45SAlexander Motin 
87343fe7d45SAlexander Motin 	ET_LOCK();
874a157e425SAlexander Motin 	et = timer;
87543fe7d45SAlexander Motin 	snprintf(buf, sizeof(buf), "%s", et->et_name);
87643fe7d45SAlexander Motin 	ET_UNLOCK();
87743fe7d45SAlexander Motin 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
87843fe7d45SAlexander Motin 	ET_LOCK();
879a157e425SAlexander Motin 	et = timer;
88043fe7d45SAlexander Motin 	if (error != 0 || req->newptr == NULL ||
881a157e425SAlexander Motin 	    strcasecmp(buf, et->et_name) == 0) {
88243fe7d45SAlexander Motin 		ET_UNLOCK();
88343fe7d45SAlexander Motin 		return (error);
88443fe7d45SAlexander Motin 	}
885a157e425SAlexander Motin 	et = et_find(buf, 0, 0);
88643fe7d45SAlexander Motin 	if (et == NULL) {
88743fe7d45SAlexander Motin 		ET_UNLOCK();
88843fe7d45SAlexander Motin 		return (ENOENT);
88943fe7d45SAlexander Motin 	}
89043fe7d45SAlexander Motin 	configtimer(0);
891a157e425SAlexander Motin 	et_free(timer);
892a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_C3STOP)
89392597e06SJohn Baldwin 		cpu_disable_c3_sleep++;
894a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
89592597e06SJohn Baldwin 		cpu_disable_c3_sleep--;
896afe41f2dSAlexander Motin 	periodic = want_periodic;
897a157e425SAlexander Motin 	timer = et;
898a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
89943fe7d45SAlexander Motin 	configtimer(1);
90043fe7d45SAlexander Motin 	ET_UNLOCK();
90143fe7d45SAlexander Motin 	return (error);
90243fe7d45SAlexander Motin }
903a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer,
90443fe7d45SAlexander Motin     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
905dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer");
906a157e425SAlexander Motin 
907a157e425SAlexander Motin /*
908a157e425SAlexander Motin  * Report or change the active event timer periodicity.
909a157e425SAlexander Motin  */
910a157e425SAlexander Motin static int
911a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS)
912a157e425SAlexander Motin {
913a157e425SAlexander Motin 	int error, val;
914a157e425SAlexander Motin 
915a157e425SAlexander Motin 	val = periodic;
916a157e425SAlexander Motin 	error = sysctl_handle_int(oidp, &val, 0, req);
917a157e425SAlexander Motin 	if (error != 0 || req->newptr == NULL)
918a157e425SAlexander Motin 		return (error);
919a157e425SAlexander Motin 	ET_LOCK();
920a157e425SAlexander Motin 	configtimer(0);
921afe41f2dSAlexander Motin 	periodic = want_periodic = val;
922a157e425SAlexander Motin 	configtimer(1);
923a157e425SAlexander Motin 	ET_UNLOCK();
924a157e425SAlexander Motin 	return (error);
925a157e425SAlexander Motin }
926a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic,
927a157e425SAlexander Motin     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
928dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode");
9296e3bf539SKonstantin Belousov 
9306e3bf539SKonstantin Belousov #include "opt_ddb.h"
9316e3bf539SKonstantin Belousov 
9326e3bf539SKonstantin Belousov #ifdef DDB
9336e3bf539SKonstantin Belousov #include <ddb/ddb.h>
9346e3bf539SKonstantin Belousov 
9356e3bf539SKonstantin Belousov DB_SHOW_COMMAND(clocksource, db_show_clocksource)
9366e3bf539SKonstantin Belousov {
9376e3bf539SKonstantin Belousov 	struct pcpu_state *st;
9386e3bf539SKonstantin Belousov 	int c;
9396e3bf539SKonstantin Belousov 
9406e3bf539SKonstantin Belousov 	CPU_FOREACH(c) {
9416e3bf539SKonstantin Belousov 		st = DPCPU_ID_PTR(c, timerstate);
9426e3bf539SKonstantin Belousov 		db_printf(
9436e3bf539SKonstantin Belousov 		    "CPU %2d: action %d handle %d  ipi %d idle %d\n"
9446e3bf539SKonstantin Belousov 		    "        now %#jx nevent %#jx (%jd)\n"
9456e3bf539SKonstantin Belousov 		    "        ntick %#jx (%jd) nhard %#jx (%jd)\n"
9466e3bf539SKonstantin Belousov 		    "        nstat %#jx (%jd) nprof %#jx (%jd)\n"
9476e3bf539SKonstantin Belousov 		    "        ncall %#jx (%jd) ncallopt %#jx (%jd)\n",
9486e3bf539SKonstantin Belousov 		    c, st->action, st->handle, st->ipi, st->idle,
9496e3bf539SKonstantin Belousov 		    (uintmax_t)st->now,
9506e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextevent,
9516e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextevent - st->now) / tick_sbt,
9526e3bf539SKonstantin Belousov 		    (uintmax_t)st->nexttick,
9536e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nexttick - st->now) / tick_sbt,
9546e3bf539SKonstantin Belousov 		    (uintmax_t)st->nexthard,
9556e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nexthard - st->now) / tick_sbt,
9566e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextstat,
9576e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextstat - st->now) / tick_sbt,
9586e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextprof,
9596e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextprof - st->now) / tick_sbt,
9606e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextcall,
9616e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextcall - st->now) / tick_sbt,
9626e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextcallopt,
9636e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextcallopt - st->now) / tick_sbt);
9646e3bf539SKonstantin Belousov 	}
9656e3bf539SKonstantin Belousov }
9666e3bf539SKonstantin Belousov 
9676e3bf539SKonstantin Belousov #endif
968