xref: /freebsd/sys/kern/kern_clocksource.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
143fe7d45SAlexander Motin /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
45b999a6bSDavide Italiano  * Copyright (c) 2010-2013 Alexander Motin <mav@FreeBSD.org>
543fe7d45SAlexander Motin  * All rights reserved.
643fe7d45SAlexander Motin  *
743fe7d45SAlexander Motin  * Redistribution and use in source and binary forms, with or without
843fe7d45SAlexander Motin  * modification, are permitted provided that the following conditions
943fe7d45SAlexander Motin  * are met:
1043fe7d45SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
1143fe7d45SAlexander Motin  *    notice, this list of conditions and the following disclaimer,
1243fe7d45SAlexander Motin  *    without modification, immediately at the beginning of the file.
1343fe7d45SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1443fe7d45SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1543fe7d45SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1643fe7d45SAlexander Motin  *
1743fe7d45SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1843fe7d45SAlexander Motin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1943fe7d45SAlexander Motin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2043fe7d45SAlexander Motin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2143fe7d45SAlexander Motin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2243fe7d45SAlexander Motin  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2343fe7d45SAlexander Motin  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2443fe7d45SAlexander Motin  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2543fe7d45SAlexander Motin  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2643fe7d45SAlexander Motin  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2743fe7d45SAlexander Motin  */
2843fe7d45SAlexander Motin 
2943fe7d45SAlexander Motin #include <sys/cdefs.h>
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 
66ebb3cb61SMark Johnston struct pcpu_state;
67ebb3cb61SMark Johnston static sbintime_t	getnextcpuevent(struct pcpu_state *state, int idle);
68ebb3cb61SMark Johnston static sbintime_t	getnextevent(struct pcpu_state *state);
695b999a6bSDavide Italiano static int		handleevents(sbintime_t now, int fake);
7043fe7d45SAlexander Motin 
71a157e425SAlexander Motin static struct mtx	et_hw_mtx;
72a157e425SAlexander Motin 
73a157e425SAlexander Motin #define	ET_HW_LOCK(state)						\
74a157e425SAlexander Motin 	{								\
75a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
76a157e425SAlexander Motin 			mtx_lock_spin(&(state)->et_hw_mtx);		\
77a157e425SAlexander Motin 		else							\
78a157e425SAlexander Motin 			mtx_lock_spin(&et_hw_mtx);			\
79a157e425SAlexander Motin 	}
80a157e425SAlexander Motin 
81a157e425SAlexander Motin #define	ET_HW_UNLOCK(state)						\
82a157e425SAlexander Motin 	{								\
83a157e425SAlexander Motin 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
84a157e425SAlexander Motin 			mtx_unlock_spin(&(state)->et_hw_mtx);		\
85a157e425SAlexander Motin 		else							\
86a157e425SAlexander Motin 			mtx_unlock_spin(&et_hw_mtx);			\
87a157e425SAlexander Motin 	}
88a157e425SAlexander Motin 
89a157e425SAlexander Motin static struct eventtimer *timer = NULL;
905b999a6bSDavide Italiano static sbintime_t	timerperiod;	/* Timer period for periodic mode. */
915b999a6bSDavide Italiano static sbintime_t	statperiod;	/* statclock() events period. */
925b999a6bSDavide Italiano static sbintime_t	profperiod;	/* profclock() events period. */
935b999a6bSDavide Italiano static sbintime_t	nexttick;	/* Next global timer tick time. */
945b999a6bSDavide Italiano static u_int		busy = 1;	/* Reconfiguration is in progress. */
95af3b2549SHans Petter Selasky static int		profiling;	/* Profiling events enabled. */
96a157e425SAlexander Motin 
97a157e425SAlexander Motin static char		timername[32];	/* Wanted timer. */
98a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername));
99a157e425SAlexander Motin 
100af3b2549SHans Petter Selasky static int		singlemul;	/* Multiplier for periodic mode. */
101af3b2549SHans Petter Selasky SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RWTUN, &singlemul,
102a157e425SAlexander Motin     0, "Multiplier for periodic mode");
10343fe7d45SAlexander Motin 
104af3b2549SHans Petter Selasky static u_int		idletick;	/* Run periodic events when idle. */
105af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RWTUN, &idletick,
106a157e425SAlexander Motin     0, "Run periodic events when idle");
107a157e425SAlexander Motin 
108af3b2549SHans Petter Selasky static int		periodic;	/* Periodic or one-shot mode. */
109af3b2549SHans Petter Selasky static int		want_periodic;	/* What mode to prefer. */
110afe41f2dSAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &want_periodic);
111a157e425SAlexander Motin 
112a157e425SAlexander Motin struct pcpu_state {
113a157e425SAlexander Motin 	struct mtx	et_hw_mtx;	/* Per-CPU timer mutex. */
114a157e425SAlexander Motin 	u_int		action;		/* Reconfiguration requests. */
115a157e425SAlexander Motin 	u_int		handle;		/* Immediate handle resuests. */
1165b999a6bSDavide Italiano 	sbintime_t	now;		/* Last tick time. */
1175b999a6bSDavide Italiano 	sbintime_t	nextevent;	/* Next scheduled event on this CPU. */
1185b999a6bSDavide Italiano 	sbintime_t	nexttick;	/* Next timer tick time. */
119d3e2e28eSAlexander Motin 	sbintime_t	nexthard;	/* Next hardclock() event. */
1205b999a6bSDavide Italiano 	sbintime_t	nextstat;	/* Next statclock() event. */
1215b999a6bSDavide Italiano 	sbintime_t	nextprof;	/* Next profclock() event. */
1225b999a6bSDavide Italiano 	sbintime_t	nextcall;	/* Next callout event. */
1235b999a6bSDavide Italiano 	sbintime_t	nextcallopt;	/* Next optional callout event. */
124a157e425SAlexander Motin 	int		ipi;		/* This CPU needs IPI. */
125a157e425SAlexander Motin 	int		idle;		/* This CPU is in idle mode. */
126a157e425SAlexander Motin };
127a157e425SAlexander Motin 
1282bf95012SAndrew Turner DPCPU_DEFINE_STATIC(struct pcpu_state, timerstate);
1295b999a6bSDavide Italiano DPCPU_DEFINE(sbintime_t, hardclocktime);
130fdc5dd2dSAlexander Motin 
131a157e425SAlexander Motin /*
132a157e425SAlexander Motin  * Timer broadcast IPI handler.
133a157e425SAlexander Motin  */
134a157e425SAlexander Motin int
hardclockintr(void)135a157e425SAlexander Motin hardclockintr(void)
13643fe7d45SAlexander Motin {
1375b999a6bSDavide Italiano 	sbintime_t now;
138a157e425SAlexander Motin 	struct pcpu_state *state;
139a157e425SAlexander Motin 	int done;
14043fe7d45SAlexander Motin 
141a157e425SAlexander Motin 	if (doconfigtimer() || busy)
142a157e425SAlexander Motin 		return (FILTER_HANDLED);
143a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
144a157e425SAlexander Motin 	now = state->now;
145ece453d5SMark Johnston 	CTR2(KTR_SPARE2, "ipi:    now  %d.%08x",
146ece453d5SMark Johnston 	    (int)(now >> 32), (u_int)(now & 0xffffffff));
1475b999a6bSDavide Italiano 	done = handleevents(now, 0);
148a157e425SAlexander Motin 	return (done ? FILTER_HANDLED : FILTER_STRAY);
149a157e425SAlexander Motin }
150a157e425SAlexander Motin 
151a157e425SAlexander Motin /*
152a157e425SAlexander Motin  * Handle all events for specified time on this CPU
153a157e425SAlexander Motin  */
154a157e425SAlexander Motin static int
handleevents(sbintime_t now,int fake)1555b999a6bSDavide Italiano handleevents(sbintime_t now, int fake)
156a157e425SAlexander Motin {
1575b999a6bSDavide Italiano 	sbintime_t t, *hct;
158a157e425SAlexander Motin 	struct trapframe *frame;
159a157e425SAlexander Motin 	struct pcpu_state *state;
160a157e425SAlexander Motin 	int usermode;
161a157e425SAlexander Motin 	int done, runs;
162a157e425SAlexander Motin 
163ece453d5SMark Johnston 	CTR2(KTR_SPARE2, "handle:  now  %d.%08x",
164ece453d5SMark Johnston 	    (int)(now >> 32), (u_int)(now & 0xffffffff));
165a157e425SAlexander Motin 	done = 0;
166a157e425SAlexander Motin 	if (fake) {
167a157e425SAlexander Motin 		frame = NULL;
168a157e425SAlexander Motin 		usermode = 0;
169a157e425SAlexander Motin 	} else {
170a157e425SAlexander Motin 		frame = curthread->td_intr_frame;
171a157e425SAlexander Motin 		usermode = TRAPF_USERMODE(frame);
172a157e425SAlexander Motin 	}
173dd7498aeSAndriy Gapon 
174a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
175dd7498aeSAndriy Gapon 
176bcfd016cSAlexander Motin 	runs = 0;
1775b999a6bSDavide Italiano 	while (now >= state->nexthard) {
1785b999a6bSDavide Italiano 		state->nexthard += tick_sbt;
179a157e425SAlexander Motin 		runs++;
18043fe7d45SAlexander Motin 	}
181c0722d20SAlexander Motin 	if (runs) {
1825b999a6bSDavide Italiano 		hct = DPCPU_PTR(hardclocktime);
1835b999a6bSDavide Italiano 		*hct = state->nexthard - tick_sbt;
184c0722d20SAlexander Motin 		if (fake < 2) {
185cc4f3d0aSMark Johnston 			hardclock(runs, usermode);
186a157e425SAlexander Motin 			done = 1;
18743fe7d45SAlexander Motin 		}
188c0722d20SAlexander Motin 	}
189bcfd016cSAlexander Motin 	runs = 0;
1905b999a6bSDavide Italiano 	while (now >= state->nextstat) {
1915b999a6bSDavide Italiano 		state->nextstat += statperiod;
192bcfd016cSAlexander Motin 		runs++;
193bcfd016cSAlexander Motin 	}
194bcfd016cSAlexander Motin 	if (runs && fake < 2) {
195cc4f3d0aSMark Johnston 		statclock(runs, usermode);
196a157e425SAlexander Motin 		done = 1;
19743fe7d45SAlexander Motin 	}
198a157e425SAlexander Motin 	if (profiling) {
199bcfd016cSAlexander Motin 		runs = 0;
2005b999a6bSDavide Italiano 		while (now >= state->nextprof) {
2015b999a6bSDavide Italiano 			state->nextprof += profperiod;
202bcfd016cSAlexander Motin 			runs++;
203bcfd016cSAlexander Motin 		}
204bcfd016cSAlexander Motin 		if (runs && !fake) {
205cc4f3d0aSMark Johnston 			profclock(runs, usermode, TRAPF_PC(frame));
206a157e425SAlexander Motin 			done = 1;
20743fe7d45SAlexander Motin 		}
208a157e425SAlexander Motin 	} else
209a157e425SAlexander Motin 		state->nextprof = state->nextstat;
21010c87557SHans Petter Selasky 	if (now >= state->nextcallopt || now >= state->nextcall) {
2114bc38a5aSDavide Italiano 		state->nextcall = state->nextcallopt = SBT_MAX;
2125b999a6bSDavide Italiano 		callout_process(now);
2135b999a6bSDavide Italiano 	}
214dd7498aeSAndriy Gapon 
215a157e425SAlexander Motin 	ET_HW_LOCK(state);
216a889a65bSMark Johnston 	t = getnextcpuevent(state, 0);
217a157e425SAlexander Motin 	if (!busy) {
218a157e425SAlexander Motin 		state->idle = 0;
219a157e425SAlexander Motin 		state->nextevent = t;
220e37e08c7SAlexander Motin 		loadtimer(now, (fake == 2) &&
221e37e08c7SAlexander Motin 		    (timer->et_flags & ET_FLAGS_PERCPU));
22243fe7d45SAlexander Motin 	}
223a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
224a157e425SAlexander Motin 	return (done);
22543fe7d45SAlexander Motin }
22643fe7d45SAlexander Motin 
22743fe7d45SAlexander Motin /*
228a157e425SAlexander Motin  * Schedule binuptime of the next event on current CPU.
22943fe7d45SAlexander Motin  */
2305b999a6bSDavide Italiano static sbintime_t
getnextcpuevent(struct pcpu_state * state,int idle)231ebb3cb61SMark Johnston getnextcpuevent(struct pcpu_state *state, int idle)
23243fe7d45SAlexander Motin {
2335b999a6bSDavide Italiano 	sbintime_t event;
2345b999a6bSDavide Italiano 	u_int hardfreq;
23543fe7d45SAlexander Motin 
2365b999a6bSDavide Italiano 	/* Handle hardclock() events, skipping some if CPU is idle. */
2375b999a6bSDavide Italiano 	event = state->nexthard;
2385b999a6bSDavide Italiano 	if (idle) {
239cb1f5d11SAlexander Motin 		if (tc_min_ticktock_freq > 1
2405b999a6bSDavide Italiano #ifdef SMP
2415b999a6bSDavide Italiano 		    && curcpu == CPU_FIRST()
2425b999a6bSDavide Italiano #endif
2435b999a6bSDavide Italiano 		    )
2445b999a6bSDavide Italiano 			hardfreq = hz / tc_min_ticktock_freq;
245cb1f5d11SAlexander Motin 		else
246cb1f5d11SAlexander Motin 			hardfreq = hz;
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
getnextevent(struct pcpu_state * state)266ebb3cb61SMark Johnston getnextevent(struct pcpu_state *state)
267a157e425SAlexander Motin {
2685b999a6bSDavide Italiano 	sbintime_t event;
269a157e425SAlexander Motin #ifdef SMP
270a157e425SAlexander Motin 	int	cpu;
271a157e425SAlexander Motin #endif
2725cc2d25aSMatt Macy #ifdef KTR
2735b999a6bSDavide Italiano 	int	c;
274a157e425SAlexander Motin 
2755cc2d25aSMatt Macy 	c = -1;
2765cc2d25aSMatt Macy #endif
2775b999a6bSDavide Italiano 	event = state->nextevent;
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;
2845cc2d25aSMatt Macy #ifdef KTR
285a157e425SAlexander Motin 				c = cpu;
2865cc2d25aSMatt Macy #endif
28743fe7d45SAlexander Motin 			}
28843fe7d45SAlexander Motin 		}
2892d7d1642SGrzegorz Bernacki 	}
290a157e425SAlexander Motin #endif
291ece453d5SMark Johnston 	CTR3(KTR_SPARE2, "next:    next %d.%08x by %d",
292ece453d5SMark Johnston 	    (int)(event >> 32), (u_int)(event & 0xffffffff), c);
2935b999a6bSDavide Italiano 	return (event);
294a157e425SAlexander Motin }
295a157e425SAlexander Motin 
296a157e425SAlexander Motin /* Hardware timer callback function. */
297a157e425SAlexander Motin static void
timercb(struct eventtimer * et,void * arg)298a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg)
299a157e425SAlexander Motin {
3005b999a6bSDavide Italiano 	sbintime_t now;
3015b999a6bSDavide Italiano 	sbintime_t *next;
302a157e425SAlexander Motin 	struct pcpu_state *state;
303a157e425SAlexander Motin #ifdef SMP
304a157e425SAlexander Motin 	int cpu, bcast;
305a157e425SAlexander Motin #endif
306a157e425SAlexander Motin 
307a157e425SAlexander Motin 	/* Do not touch anything if somebody reconfiguring timers. */
308a157e425SAlexander Motin 	if (busy)
309a157e425SAlexander Motin 		return;
310a157e425SAlexander Motin 	/* Update present and next tick times. */
311a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
312a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_PERCPU) {
313a157e425SAlexander Motin 		next = &state->nexttick;
314a157e425SAlexander Motin 	} else
315a157e425SAlexander Motin 		next = &nexttick;
3165b999a6bSDavide Italiano 	now = sbinuptime();
3175b999a6bSDavide Italiano 	if (periodic)
3185b999a6bSDavide Italiano 		*next = now + timerperiod;
3195b999a6bSDavide Italiano 	else
3205b999a6bSDavide Italiano 		*next = -1;	/* Next tick is not scheduled yet. */
321a157e425SAlexander Motin 	state->now = now;
322ece453d5SMark Johnston 	CTR2(KTR_SPARE2, "intr:    now  %d.%08x",
323ece453d5SMark Johnston 	    (int)(now >> 32), (u_int)(now & 0xffffffff));
324a157e425SAlexander Motin 
325a157e425SAlexander Motin #ifdef SMP
326fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
327fdce57a0SJohn Baldwin 	MPASS(mp_ncpus == 1 || smp_started);
328fdce57a0SJohn Baldwin #endif
329a157e425SAlexander Motin 	/* Prepare broadcasting to other CPUs for non-per-CPU timers. */
330a157e425SAlexander Motin 	bcast = 0;
331fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
332fdce57a0SJohn Baldwin 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0) {
333fdce57a0SJohn Baldwin #else
334a157e425SAlexander Motin 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) {
335fdce57a0SJohn Baldwin #endif
336a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
337a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
338a157e425SAlexander Motin 			ET_HW_LOCK(state);
339a157e425SAlexander Motin 			state->now = now;
3405b999a6bSDavide Italiano 			if (now >= state->nextevent) {
3415b999a6bSDavide Italiano 				state->nextevent += SBT_1S;
3428e860de4SAlexander Motin 				if (curcpu != cpu) {
343a157e425SAlexander Motin 					state->ipi = 1;
344a157e425SAlexander Motin 					bcast = 1;
345a157e425SAlexander Motin 				}
3468e860de4SAlexander Motin 			}
347a157e425SAlexander Motin 			ET_HW_UNLOCK(state);
348a157e425SAlexander Motin 		}
349a157e425SAlexander Motin 	}
350a157e425SAlexander Motin #endif
351a157e425SAlexander Motin 
352a157e425SAlexander Motin 	/* Handle events for this time on this CPU. */
3535b999a6bSDavide Italiano 	handleevents(now, 0);
354a157e425SAlexander Motin 
355a157e425SAlexander Motin #ifdef SMP
356a157e425SAlexander Motin 	/* Broadcast interrupt to other CPUs for non-per-CPU timers. */
357a157e425SAlexander Motin 	if (bcast) {
358a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
359a157e425SAlexander Motin 			if (curcpu == cpu)
360a157e425SAlexander Motin 				continue;
361a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
362a157e425SAlexander Motin 			if (state->ipi) {
363a157e425SAlexander Motin 				state->ipi = 0;
364a157e425SAlexander Motin 				ipi_cpu(cpu, IPI_HARDCLOCK);
365a157e425SAlexander Motin 			}
366a157e425SAlexander Motin 		}
367a157e425SAlexander Motin 	}
368a157e425SAlexander Motin #endif
369a157e425SAlexander Motin }
370a157e425SAlexander Motin 
371a157e425SAlexander Motin /*
372a157e425SAlexander Motin  * Load new value into hardware timer.
373a157e425SAlexander Motin  */
374a157e425SAlexander Motin static void
3755b999a6bSDavide Italiano loadtimer(sbintime_t now, int start)
376a157e425SAlexander Motin {
377a157e425SAlexander Motin 	struct pcpu_state *state;
3785b999a6bSDavide Italiano 	sbintime_t new;
3795b999a6bSDavide Italiano 	sbintime_t *next;
380a157e425SAlexander Motin 	uint64_t tmp;
381a157e425SAlexander Motin 	int eq;
382a157e425SAlexander Motin 
383c70410e6SAlexander Motin 	state = DPCPU_PTR(timerstate);
384ebb3cb61SMark Johnston 	if (timer->et_flags & ET_FLAGS_PERCPU)
385c70410e6SAlexander Motin 		next = &state->nexttick;
386ebb3cb61SMark Johnston 	else
387c70410e6SAlexander Motin 		next = &nexttick;
388a157e425SAlexander Motin 	if (periodic) {
389a157e425SAlexander Motin 		if (start) {
390a157e425SAlexander Motin 			/*
391a157e425SAlexander Motin 			 * Try to start all periodic timers aligned
392a157e425SAlexander Motin 			 * to period to make events synchronous.
393a157e425SAlexander Motin 			 */
3945b999a6bSDavide Italiano 			tmp = now % timerperiod;
3955b999a6bSDavide Italiano 			new = timerperiod - tmp;
3965b999a6bSDavide Italiano 			if (new < tmp)		/* Left less then passed. */
3975b999a6bSDavide Italiano 				new += timerperiod;
398ece453d5SMark Johnston 			CTR4(KTR_SPARE2, "load p:   now %d.%08x first in %d.%08x",
399ece453d5SMark Johnston 			    (int)(now >> 32), (u_int)(now & 0xffffffff),
4005b999a6bSDavide Italiano 			    (int)(new >> 32), (u_int)(new & 0xffffffff));
4015b999a6bSDavide Italiano 			*next = new + now;
4025b999a6bSDavide Italiano 			et_start(timer, new, timerperiod);
403a157e425SAlexander Motin 		}
404a157e425SAlexander Motin 	} else {
405ebb3cb61SMark Johnston 		new = getnextevent(state);
4065b999a6bSDavide Italiano 		eq = (new == *next);
407ece453d5SMark Johnston 		CTR3(KTR_SPARE2, "load:    next %d.%08x eq %d",
408ece453d5SMark Johnston 		    (int)(new >> 32), (u_int)(new & 0xffffffff), eq);
409a157e425SAlexander Motin 		if (!eq) {
410a157e425SAlexander Motin 			*next = new;
4115b999a6bSDavide Italiano 			et_start(timer, new - now, 0);
412a157e425SAlexander Motin 		}
413a157e425SAlexander Motin 	}
414a157e425SAlexander Motin }
415a157e425SAlexander Motin 
416a157e425SAlexander Motin /*
417a157e425SAlexander Motin  * Prepare event timer parameters after configuration changes.
418a157e425SAlexander Motin  */
419a157e425SAlexander Motin static void
420a157e425SAlexander Motin setuptimer(void)
421a157e425SAlexander Motin {
422a157e425SAlexander Motin 	int freq;
423a157e425SAlexander Motin 
424a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
425a157e425SAlexander Motin 		periodic = 0;
426a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
427a157e425SAlexander Motin 		periodic = 1;
428dd9595e7SAlexander Motin 	singlemul = MIN(MAX(singlemul, 1), 20);
429a157e425SAlexander Motin 	freq = hz * singlemul;
430a157e425SAlexander Motin 	while (freq < (profiling ? profhz : stathz))
431a157e425SAlexander Motin 		freq += hz;
432a157e425SAlexander Motin 	freq = round_freq(timer, freq);
4335b999a6bSDavide Italiano 	timerperiod = SBT_1S / freq;
434a157e425SAlexander Motin }
43543fe7d45SAlexander Motin 
43643fe7d45SAlexander Motin /*
43743fe7d45SAlexander Motin  * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler.
43843fe7d45SAlexander Motin  */
439a157e425SAlexander Motin static int
440a157e425SAlexander Motin doconfigtimer(void)
44143fe7d45SAlexander Motin {
4425b999a6bSDavide Italiano 	sbintime_t now;
443a157e425SAlexander Motin 	struct pcpu_state *state;
44443fe7d45SAlexander Motin 
445a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
446a157e425SAlexander Motin 	switch (atomic_load_acq_int(&state->action)) {
447a157e425SAlexander Motin 	case 1:
4485b999a6bSDavide Italiano 		now = sbinuptime();
449a157e425SAlexander Motin 		ET_HW_LOCK(state);
4505b999a6bSDavide Italiano 		loadtimer(now, 1);
451a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
452a157e425SAlexander Motin 		state->handle = 0;
453a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
454a157e425SAlexander Motin 		return (1);
455a157e425SAlexander Motin 	case 2:
456a157e425SAlexander Motin 		ET_HW_LOCK(state);
457a157e425SAlexander Motin 		et_stop(timer);
458a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
459a157e425SAlexander Motin 		state->handle = 0;
460a157e425SAlexander Motin 		atomic_store_rel_int(&state->action, 0);
461a157e425SAlexander Motin 		return (1);
462a157e425SAlexander Motin 	}
463a157e425SAlexander Motin 	if (atomic_readandclear_int(&state->handle) && !busy) {
4645b999a6bSDavide Italiano 		now = sbinuptime();
4655b999a6bSDavide Italiano 		handleevents(now, 0);
46643fe7d45SAlexander Motin 		return (1);
46743fe7d45SAlexander Motin 	}
46843fe7d45SAlexander Motin 	return (0);
46943fe7d45SAlexander Motin }
47043fe7d45SAlexander Motin 
47143fe7d45SAlexander Motin /*
47243fe7d45SAlexander Motin  * Reconfigure specified timer.
47343fe7d45SAlexander Motin  * For per-CPU timers use IPI to make other CPUs to reconfigure.
47443fe7d45SAlexander Motin  */
47543fe7d45SAlexander Motin static void
476a157e425SAlexander Motin configtimer(int start)
47743fe7d45SAlexander Motin {
4785b999a6bSDavide Italiano 	sbintime_t now, next;
479a157e425SAlexander Motin 	struct pcpu_state *state;
48043fe7d45SAlexander Motin 	int cpu;
48143fe7d45SAlexander Motin 
482a157e425SAlexander Motin 	if (start) {
483a157e425SAlexander Motin 		setuptimer();
4845b999a6bSDavide Italiano 		now = sbinuptime();
4855b999a6bSDavide Italiano 	} else
4865b999a6bSDavide Italiano 		now = 0;
48743fe7d45SAlexander Motin 	critical_enter();
488a157e425SAlexander Motin 	ET_HW_LOCK(DPCPU_PTR(timerstate));
489a157e425SAlexander Motin 	if (start) {
490a157e425SAlexander Motin 		/* Initialize time machine parameters. */
4915b999a6bSDavide Italiano 		next = now + timerperiod;
492a157e425SAlexander Motin 		if (periodic)
493a157e425SAlexander Motin 			nexttick = next;
49443fe7d45SAlexander Motin 		else
4955b999a6bSDavide Italiano 			nexttick = -1;
496fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
497fdce57a0SJohn Baldwin 		MPASS(mp_ncpus == 1 || smp_started);
498fdce57a0SJohn Baldwin #endif
499a157e425SAlexander Motin 		CPU_FOREACH(cpu) {
500a157e425SAlexander Motin 			state = DPCPU_ID_PTR(cpu, timerstate);
501a157e425SAlexander Motin 			state->now = now;
502fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP
5035b999a6bSDavide Italiano 			if (!smp_started && cpu != CPU_FIRST())
5044bc38a5aSDavide Italiano 				state->nextevent = SBT_MAX;
5055b999a6bSDavide Italiano 			else
506fdce57a0SJohn Baldwin #endif
507a157e425SAlexander Motin 				state->nextevent = next;
508a157e425SAlexander Motin 			if (periodic)
509a157e425SAlexander Motin 				state->nexttick = next;
510a157e425SAlexander Motin 			else
5115b999a6bSDavide Italiano 				state->nexttick = -1;
512a157e425SAlexander Motin 			state->nexthard = next;
513a157e425SAlexander Motin 			state->nextstat = next;
514a157e425SAlexander Motin 			state->nextprof = next;
5155b999a6bSDavide Italiano 			state->nextcall = next;
5165b999a6bSDavide Italiano 			state->nextcallopt = next;
517a157e425SAlexander Motin 			hardclock_sync(cpu);
518a157e425SAlexander Motin 		}
519a157e425SAlexander Motin 		busy = 0;
520a157e425SAlexander Motin 		/* Start global timer or per-CPU timer of this CPU. */
5215b999a6bSDavide Italiano 		loadtimer(now, 1);
522a157e425SAlexander Motin 	} else {
523a157e425SAlexander Motin 		busy = 1;
524a157e425SAlexander Motin 		/* Stop global timer or per-CPU timer of this CPU. */
525a157e425SAlexander Motin 		et_stop(timer);
526a157e425SAlexander Motin 	}
527a157e425SAlexander Motin 	ET_HW_UNLOCK(DPCPU_PTR(timerstate));
52843fe7d45SAlexander Motin #ifdef SMP
529fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
530fdce57a0SJohn Baldwin 	/* If timer is global we are done. */
531fdce57a0SJohn Baldwin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) {
532fdce57a0SJohn Baldwin #else
533a157e425SAlexander Motin 	/* If timer is global or there is no other CPUs yet - we are done. */
534a157e425SAlexander Motin 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) {
535fdce57a0SJohn Baldwin #endif
53643fe7d45SAlexander Motin 		critical_exit();
53743fe7d45SAlexander Motin 		return;
53843fe7d45SAlexander Motin 	}
53943fe7d45SAlexander Motin 	/* Set reconfigure flags for other CPUs. */
54043fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
541a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
542a157e425SAlexander Motin 		atomic_store_rel_int(&state->action,
543a157e425SAlexander Motin 		    (cpu == curcpu) ? 0 : ( start ? 1 : 2));
54443fe7d45SAlexander Motin 	}
545a157e425SAlexander Motin 	/* Broadcast reconfigure IPI. */
546a157e425SAlexander Motin 	ipi_all_but_self(IPI_HARDCLOCK);
54743fe7d45SAlexander Motin 	/* Wait for reconfiguration completed. */
54843fe7d45SAlexander Motin restart:
54943fe7d45SAlexander Motin 	cpu_spinwait();
55043fe7d45SAlexander Motin 	CPU_FOREACH(cpu) {
55143fe7d45SAlexander Motin 		if (cpu == curcpu)
55243fe7d45SAlexander Motin 			continue;
553a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
554a157e425SAlexander Motin 		if (atomic_load_acq_int(&state->action))
55543fe7d45SAlexander Motin 			goto restart;
55643fe7d45SAlexander Motin 	}
55743fe7d45SAlexander Motin #endif
558a157e425SAlexander Motin 	critical_exit();
55943fe7d45SAlexander Motin }
56043fe7d45SAlexander Motin 
561a157e425SAlexander Motin /*
562a157e425SAlexander Motin  * Calculate nearest frequency supported by hardware timer.
563a157e425SAlexander Motin  */
56451636352SAlexander Motin static int
56551636352SAlexander Motin round_freq(struct eventtimer *et, int freq)
56651636352SAlexander Motin {
56751636352SAlexander Motin 	uint64_t div;
56851636352SAlexander Motin 
56951636352SAlexander Motin 	if (et->et_frequency != 0) {
570599cf0f1SAlexander Motin 		div = lmax((et->et_frequency + freq / 2) / freq, 1);
57151636352SAlexander Motin 		if (et->et_flags & ET_FLAGS_POW2DIV)
57251636352SAlexander Motin 			div = 1 << (flsl(div + div / 2) - 1);
57351636352SAlexander Motin 		freq = (et->et_frequency + div / 2) / div;
57451636352SAlexander Motin 	}
575fdc5dd2dSAlexander Motin 	if (et->et_min_period > SBT_1S)
576803a9b3eSAlexander Motin 		panic("Event timer \"%s\" doesn't support sub-second periods!",
577803a9b3eSAlexander Motin 		    et->et_name);
578fdc5dd2dSAlexander Motin 	else if (et->et_min_period != 0)
579fdc5dd2dSAlexander Motin 		freq = min(freq, SBT2FREQ(et->et_min_period));
580fdc5dd2dSAlexander Motin 	if (et->et_max_period < SBT_1S && et->et_max_period != 0)
581fdc5dd2dSAlexander Motin 		freq = max(freq, SBT2FREQ(et->et_max_period));
58251636352SAlexander Motin 	return (freq);
58351636352SAlexander Motin }
58451636352SAlexander Motin 
58543fe7d45SAlexander Motin /*
586a157e425SAlexander Motin  * Configure and start event timers (BSP part).
58743fe7d45SAlexander Motin  */
58843fe7d45SAlexander Motin void
58943fe7d45SAlexander Motin cpu_initclocks_bsp(void)
59043fe7d45SAlexander Motin {
591a157e425SAlexander Motin 	struct pcpu_state *state;
592a157e425SAlexander Motin 	int base, div, cpu;
59343fe7d45SAlexander Motin 
594a157e425SAlexander Motin 	mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
595a157e425SAlexander Motin 	CPU_FOREACH(cpu) {
596a157e425SAlexander Motin 		state = DPCPU_ID_PTR(cpu, timerstate);
597a157e425SAlexander Motin 		mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
5984bc38a5aSDavide Italiano 		state->nextcall = SBT_MAX;
5994bc38a5aSDavide Italiano 		state->nextcallopt = SBT_MAX;
600a157e425SAlexander Motin 	}
601afe41f2dSAlexander Motin 	periodic = want_periodic;
602a157e425SAlexander Motin 	/* Grab requested timer or the best of present. */
603a157e425SAlexander Motin 	if (timername[0])
604a157e425SAlexander Motin 		timer = et_find(timername, 0, 0);
605a157e425SAlexander Motin 	if (timer == NULL && periodic) {
606a157e425SAlexander Motin 		timer = et_find(NULL,
60743fe7d45SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
608a157e425SAlexander Motin 	}
609a157e425SAlexander Motin 	if (timer == NULL) {
610a157e425SAlexander Motin 		timer = et_find(NULL,
611a157e425SAlexander Motin 		    ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT);
612a157e425SAlexander Motin 	}
613a157e425SAlexander Motin 	if (timer == NULL && !periodic) {
614a157e425SAlexander Motin 		timer = et_find(NULL,
615a157e425SAlexander Motin 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
616a157e425SAlexander Motin 	}
617a157e425SAlexander Motin 	if (timer == NULL)
618a157e425SAlexander Motin 		panic("No usable event timer found!");
619a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
620a157e425SAlexander Motin 
621a157e425SAlexander Motin 	/* Adapt to timer capabilities. */
622a157e425SAlexander Motin 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
623a157e425SAlexander Motin 		periodic = 0;
624a157e425SAlexander Motin 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
625a157e425SAlexander Motin 		periodic = 1;
626a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
62792597e06SJohn Baldwin 		cpu_disable_c3_sleep++;
628a157e425SAlexander Motin 
62943fe7d45SAlexander Motin 	/*
63043fe7d45SAlexander Motin 	 * We honor the requested 'hz' value.
63143fe7d45SAlexander Motin 	 * We want to run stathz in the neighborhood of 128hz.
63243fe7d45SAlexander Motin 	 * We would like profhz to run as often as possible.
63343fe7d45SAlexander Motin 	 */
634dd9595e7SAlexander Motin 	if (singlemul <= 0 || singlemul > 20) {
63543fe7d45SAlexander Motin 		if (hz >= 1500 || (hz % 128) == 0)
63643fe7d45SAlexander Motin 			singlemul = 1;
63743fe7d45SAlexander Motin 		else if (hz >= 750)
63843fe7d45SAlexander Motin 			singlemul = 2;
63943fe7d45SAlexander Motin 		else
64043fe7d45SAlexander Motin 			singlemul = 4;
64143fe7d45SAlexander Motin 	}
642a157e425SAlexander Motin 	if (periodic) {
643a157e425SAlexander Motin 		base = round_freq(timer, hz * singlemul);
64451636352SAlexander Motin 		singlemul = max((base + hz / 2) / hz, 1);
64551636352SAlexander Motin 		hz = (base + singlemul / 2) / singlemul;
64651636352SAlexander Motin 		if (base <= 128)
64743fe7d45SAlexander Motin 			stathz = base;
64843fe7d45SAlexander Motin 		else {
64943fe7d45SAlexander Motin 			div = base / 128;
65051636352SAlexander Motin 			if (div >= singlemul && (div % singlemul) == 0)
65143fe7d45SAlexander Motin 				div++;
65243fe7d45SAlexander Motin 			stathz = base / div;
65343fe7d45SAlexander Motin 		}
65443fe7d45SAlexander Motin 		profhz = stathz;
65551636352SAlexander Motin 		while ((profhz + stathz) <= 128 * 64)
65643fe7d45SAlexander Motin 			profhz += stathz;
657a157e425SAlexander Motin 		profhz = round_freq(timer, profhz);
65843fe7d45SAlexander Motin 	} else {
659a157e425SAlexander Motin 		hz = round_freq(timer, hz);
660a157e425SAlexander Motin 		stathz = round_freq(timer, 127);
661a157e425SAlexander Motin 		profhz = round_freq(timer, stathz * 64);
66243fe7d45SAlexander Motin 	}
663599cf0f1SAlexander Motin 	tick = 1000000 / hz;
6645b999a6bSDavide Italiano 	tick_sbt = SBT_1S / hz;
6655b999a6bSDavide Italiano 	tick_bt = sbttobt(tick_sbt);
6665b999a6bSDavide Italiano 	statperiod = SBT_1S / stathz;
6675b999a6bSDavide Italiano 	profperiod = SBT_1S / profhz;
66843fe7d45SAlexander Motin 	ET_LOCK();
669a157e425SAlexander Motin 	configtimer(1);
67043fe7d45SAlexander Motin 	ET_UNLOCK();
67143fe7d45SAlexander Motin }
67243fe7d45SAlexander Motin 
673a157e425SAlexander Motin /*
674a157e425SAlexander Motin  * Start per-CPU event timers on APs.
675a157e425SAlexander Motin  */
67643fe7d45SAlexander Motin void
67743fe7d45SAlexander Motin cpu_initclocks_ap(void)
67843fe7d45SAlexander Motin {
679a157e425SAlexander Motin 	struct pcpu_state *state;
6805b999a6bSDavide Italiano 	struct thread *td;
68143fe7d45SAlexander Motin 
682a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
683a157e425SAlexander Motin 	ET_HW_LOCK(state);
684a889a65bSMark Johnston 	state->now = sbinuptime();
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 
69527dca831SAndriy Gapon void
69627dca831SAndriy Gapon suspendclock(void)
69727dca831SAndriy Gapon {
69827dca831SAndriy Gapon 	ET_LOCK();
69927dca831SAndriy Gapon 	configtimer(0);
70027dca831SAndriy Gapon 	ET_UNLOCK();
70127dca831SAndriy Gapon }
70227dca831SAndriy Gapon 
70327dca831SAndriy Gapon void
70427dca831SAndriy Gapon resumeclock(void)
70527dca831SAndriy Gapon {
70627dca831SAndriy Gapon 	ET_LOCK();
70727dca831SAndriy Gapon 	configtimer(1);
70827dca831SAndriy Gapon 	ET_UNLOCK();
70927dca831SAndriy Gapon }
71027dca831SAndriy Gapon 
711a157e425SAlexander Motin /*
712a157e425SAlexander Motin  * Switch to profiling clock rates.
713a157e425SAlexander Motin  */
71443fe7d45SAlexander Motin void
71543fe7d45SAlexander Motin cpu_startprofclock(void)
71643fe7d45SAlexander Motin {
71743fe7d45SAlexander Motin 
71843fe7d45SAlexander Motin 	ET_LOCK();
7191af19ee4SAlexander Motin 	if (profiling == 0) {
720a157e425SAlexander Motin 		if (periodic) {
721a157e425SAlexander Motin 			configtimer(0);
722a157e425SAlexander Motin 			profiling = 1;
723a157e425SAlexander Motin 			configtimer(1);
724a157e425SAlexander Motin 		} else
725a157e425SAlexander Motin 			profiling = 1;
7261af19ee4SAlexander Motin 	} else
7271af19ee4SAlexander Motin 		profiling++;
72843fe7d45SAlexander Motin 	ET_UNLOCK();
72943fe7d45SAlexander Motin }
73043fe7d45SAlexander Motin 
731a157e425SAlexander Motin /*
732a157e425SAlexander Motin  * Switch to regular clock rates.
733a157e425SAlexander Motin  */
73443fe7d45SAlexander Motin void
73543fe7d45SAlexander Motin cpu_stopprofclock(void)
73643fe7d45SAlexander Motin {
73743fe7d45SAlexander Motin 
73843fe7d45SAlexander Motin 	ET_LOCK();
7391af19ee4SAlexander Motin 	if (profiling == 1) {
740a157e425SAlexander Motin 		if (periodic) {
741a157e425SAlexander Motin 			configtimer(0);
742a157e425SAlexander Motin 			profiling = 0;
743a157e425SAlexander Motin 			configtimer(1);
744a157e425SAlexander Motin 		} else
745a157e425SAlexander Motin 		profiling = 0;
7461af19ee4SAlexander Motin 	} else
7471af19ee4SAlexander Motin 		profiling--;
74843fe7d45SAlexander Motin 	ET_UNLOCK();
74943fe7d45SAlexander Motin }
75043fe7d45SAlexander Motin 
751a157e425SAlexander Motin /*
752a157e425SAlexander Motin  * Switch to idle mode (all ticks handled).
753a157e425SAlexander Motin  */
754acccf7d8SDavide Italiano sbintime_t
755a157e425SAlexander Motin cpu_idleclock(void)
756a157e425SAlexander Motin {
7575b999a6bSDavide Italiano 	sbintime_t now, t;
758a157e425SAlexander Motin 	struct pcpu_state *state;
759a157e425SAlexander Motin 
760a157e425SAlexander Motin 	if (idletick || busy ||
7619dfc483cSAlexander Motin 	    (periodic && (timer->et_flags & ET_FLAGS_PERCPU))
7629dfc483cSAlexander Motin #ifdef DEVICE_POLLING
7639dfc483cSAlexander Motin 	    || curcpu == CPU_FIRST()
7649dfc483cSAlexander Motin #endif
7659dfc483cSAlexander Motin 	    )
766acccf7d8SDavide Italiano 		return (-1);
767a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
768a889a65bSMark Johnston 	ET_HW_LOCK(state);
769a157e425SAlexander Motin 	if (periodic)
770a157e425SAlexander Motin 		now = state->now;
771a157e425SAlexander Motin 	else
7725b999a6bSDavide Italiano 		now = sbinuptime();
773ece453d5SMark Johnston 	CTR2(KTR_SPARE2, "idle:    now  %d.%08x",
774ece453d5SMark Johnston 	    (int)(now >> 32), (u_int)(now & 0xffffffff));
775ebb3cb61SMark Johnston 	t = getnextcpuevent(state, 1);
776a157e425SAlexander Motin 	state->idle = 1;
777a157e425SAlexander Motin 	state->nextevent = t;
778a157e425SAlexander Motin 	if (!periodic)
7795b999a6bSDavide Italiano 		loadtimer(now, 0);
780a157e425SAlexander Motin 	ET_HW_UNLOCK(state);
7815b999a6bSDavide Italiano 	return (MAX(t - now, 0));
782a157e425SAlexander Motin }
783a157e425SAlexander Motin 
784a157e425SAlexander Motin /*
785a157e425SAlexander Motin  * Switch to active mode (skip empty ticks).
786a157e425SAlexander Motin  */
787a157e425SAlexander Motin void
788a157e425SAlexander Motin cpu_activeclock(void)
789a157e425SAlexander Motin {
7905b999a6bSDavide Italiano 	sbintime_t now;
791a157e425SAlexander Motin 	struct pcpu_state *state;
792a157e425SAlexander Motin 	struct thread *td;
793a157e425SAlexander Motin 
794a157e425SAlexander Motin 	state = DPCPU_PTR(timerstate);
795a889a65bSMark Johnston 	if (atomic_load_int(&state->idle) == 0 || busy)
796a157e425SAlexander Motin 		return;
797a889a65bSMark Johnston 	spinlock_enter();
798a157e425SAlexander Motin 	if (periodic)
799a157e425SAlexander Motin 		now = state->now;
800a157e425SAlexander Motin 	else
8015b999a6bSDavide Italiano 		now = sbinuptime();
802ece453d5SMark Johnston 	CTR2(KTR_SPARE2, "active:  now  %d.%08x",
803ece453d5SMark Johnston 	    (int)(now >> 32), (u_int)(now & 0xffffffff));
804a157e425SAlexander Motin 	td = curthread;
805a157e425SAlexander Motin 	td->td_intr_nesting_level++;
8065b999a6bSDavide Italiano 	handleevents(now, 1);
807a157e425SAlexander Motin 	td->td_intr_nesting_level--;
808a157e425SAlexander Motin 	spinlock_exit();
809a157e425SAlexander Motin }
810a157e425SAlexander Motin 
811cfc4b56bSIan Lepore /*
812cfc4b56bSIan Lepore  * Change the frequency of the given timer.  This changes et->et_frequency and
813cfc4b56bSIan Lepore  * if et is the active timer it reconfigures the timer on all CPUs.  This is
814cfc4b56bSIan Lepore  * intended to be a private interface for the use of et_change_frequency() only.
815cfc4b56bSIan Lepore  */
816cfc4b56bSIan Lepore void
817cfc4b56bSIan Lepore cpu_et_frequency(struct eventtimer *et, uint64_t newfreq)
818cfc4b56bSIan Lepore {
819cfc4b56bSIan Lepore 
820cfc4b56bSIan Lepore 	ET_LOCK();
821cfc4b56bSIan Lepore 	if (et == timer) {
822cfc4b56bSIan Lepore 		configtimer(0);
823cfc4b56bSIan Lepore 		et->et_frequency = newfreq;
824cfc4b56bSIan Lepore 		configtimer(1);
825cfc4b56bSIan Lepore 	} else
826cfc4b56bSIan Lepore 		et->et_frequency = newfreq;
827cfc4b56bSIan Lepore 	ET_UNLOCK();
828cfc4b56bSIan Lepore }
829cfc4b56bSIan Lepore 
8305b999a6bSDavide Italiano void
8315b999a6bSDavide Italiano cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt)
832a157e425SAlexander Motin {
833a157e425SAlexander Motin 	struct pcpu_state *state;
834a157e425SAlexander Motin 
8355b999a6bSDavide Italiano 	/* Do not touch anything if somebody reconfiguring timers. */
8365b999a6bSDavide Italiano 	if (busy)
8375b999a6bSDavide Italiano 		return;
838ece453d5SMark Johnston 
839ece453d5SMark Johnston 	CTR5(KTR_SPARE2, "new co:  on %d at %d.%08x - %d.%08x",
840ece453d5SMark Johnston 	    cpu, (int)(bt_opt >> 32), (u_int)(bt_opt & 0xffffffff),
8415b999a6bSDavide Italiano 	    (int)(bt >> 32), (u_int)(bt & 0xffffffff));
842efe67753SNathan Whitehorn 
843efe67753SNathan Whitehorn 	KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu));
844a157e425SAlexander Motin 	state = DPCPU_ID_PTR(cpu, timerstate);
845a157e425SAlexander Motin 	ET_HW_LOCK(state);
8465b999a6bSDavide Italiano 
8475b999a6bSDavide Italiano 	/*
8485b999a6bSDavide Italiano 	 * If there is callout time already set earlier -- do nothing.
8495b999a6bSDavide Italiano 	 * This check may appear redundant because we check already in
8505b999a6bSDavide Italiano 	 * callout_process() but this double check guarantees we're safe
8515b999a6bSDavide Italiano 	 * with respect to race conditions between interrupts execution
8525b999a6bSDavide Italiano 	 * and scheduling.
8535b999a6bSDavide Italiano 	 */
8545b999a6bSDavide Italiano 	state->nextcallopt = bt_opt;
8555b999a6bSDavide Italiano 	if (bt >= state->nextcall)
8565b999a6bSDavide Italiano 		goto done;
8575b999a6bSDavide Italiano 	state->nextcall = bt;
8585b999a6bSDavide Italiano 	/* If there is some other event set earlier -- do nothing. */
8595b999a6bSDavide Italiano 	if (bt >= state->nextevent)
8605b999a6bSDavide Italiano 		goto done;
8615b999a6bSDavide Italiano 	state->nextevent = bt;
8625b999a6bSDavide Italiano 	/* If timer is periodic -- there is nothing to reprogram. */
8635b999a6bSDavide Italiano 	if (periodic)
8645b999a6bSDavide Italiano 		goto done;
8655b999a6bSDavide Italiano 	/* If timer is global or of the current CPU -- reprogram it. */
8665b999a6bSDavide Italiano 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || cpu == curcpu) {
8675b999a6bSDavide Italiano 		loadtimer(sbinuptime(), 0);
8685b999a6bSDavide Italiano done:
869a157e425SAlexander Motin 		ET_HW_UNLOCK(state);
870a157e425SAlexander Motin 		return;
871a157e425SAlexander Motin 	}
8725b999a6bSDavide Italiano 	/* Otherwise make other CPU to reprogram it. */
873a157e425SAlexander Motin 	state->handle = 1;
8745b999a6bSDavide Italiano 	ET_HW_UNLOCK(state);
8755b999a6bSDavide Italiano #ifdef SMP
876a157e425SAlexander Motin 	ipi_cpu(cpu, IPI_HARDCLOCK);
877a157e425SAlexander Motin #endif
8785b999a6bSDavide Italiano }
879a157e425SAlexander Motin 
880a157e425SAlexander Motin /*
881a157e425SAlexander Motin  * Report or change the active event timers hardware.
882a157e425SAlexander Motin  */
88343fe7d45SAlexander Motin static int
884a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS)
88543fe7d45SAlexander Motin {
88643fe7d45SAlexander Motin 	char buf[32];
88743fe7d45SAlexander Motin 	struct eventtimer *et;
88843fe7d45SAlexander Motin 	int error;
88943fe7d45SAlexander Motin 
89043fe7d45SAlexander Motin 	ET_LOCK();
891a157e425SAlexander Motin 	et = timer;
89243fe7d45SAlexander Motin 	snprintf(buf, sizeof(buf), "%s", et->et_name);
89343fe7d45SAlexander Motin 	ET_UNLOCK();
89443fe7d45SAlexander Motin 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
89543fe7d45SAlexander Motin 	ET_LOCK();
896a157e425SAlexander Motin 	et = timer;
89743fe7d45SAlexander Motin 	if (error != 0 || req->newptr == NULL ||
898a157e425SAlexander Motin 	    strcasecmp(buf, et->et_name) == 0) {
89943fe7d45SAlexander Motin 		ET_UNLOCK();
90043fe7d45SAlexander Motin 		return (error);
90143fe7d45SAlexander Motin 	}
902a157e425SAlexander Motin 	et = et_find(buf, 0, 0);
90343fe7d45SAlexander Motin 	if (et == NULL) {
90443fe7d45SAlexander Motin 		ET_UNLOCK();
90543fe7d45SAlexander Motin 		return (ENOENT);
90643fe7d45SAlexander Motin 	}
90743fe7d45SAlexander Motin 	configtimer(0);
908a157e425SAlexander Motin 	et_free(timer);
909a157e425SAlexander Motin 	if (et->et_flags & ET_FLAGS_C3STOP)
91092597e06SJohn Baldwin 		cpu_disable_c3_sleep++;
911a157e425SAlexander Motin 	if (timer->et_flags & ET_FLAGS_C3STOP)
91292597e06SJohn Baldwin 		cpu_disable_c3_sleep--;
913afe41f2dSAlexander Motin 	periodic = want_periodic;
914a157e425SAlexander Motin 	timer = et;
915a157e425SAlexander Motin 	et_init(timer, timercb, NULL, NULL);
91643fe7d45SAlexander Motin 	configtimer(1);
91743fe7d45SAlexander Motin 	ET_UNLOCK();
91843fe7d45SAlexander Motin 	return (error);
91943fe7d45SAlexander Motin }
920a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer,
92143fe7d45SAlexander Motin     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
922dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer");
923a157e425SAlexander Motin 
924a157e425SAlexander Motin /*
925a157e425SAlexander Motin  * Report or change the active event timer periodicity.
926a157e425SAlexander Motin  */
927a157e425SAlexander Motin static int
928a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS)
929a157e425SAlexander Motin {
930a157e425SAlexander Motin 	int error, val;
931a157e425SAlexander Motin 
932a157e425SAlexander Motin 	val = periodic;
933a157e425SAlexander Motin 	error = sysctl_handle_int(oidp, &val, 0, req);
934a157e425SAlexander Motin 	if (error != 0 || req->newptr == NULL)
935a157e425SAlexander Motin 		return (error);
936a157e425SAlexander Motin 	ET_LOCK();
937a157e425SAlexander Motin 	configtimer(0);
938afe41f2dSAlexander Motin 	periodic = want_periodic = val;
939a157e425SAlexander Motin 	configtimer(1);
940a157e425SAlexander Motin 	ET_UNLOCK();
941a157e425SAlexander Motin 	return (error);
942a157e425SAlexander Motin }
943a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic,
944a157e425SAlexander Motin     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
945dd9595e7SAlexander Motin     0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode");
9466e3bf539SKonstantin Belousov 
9476e3bf539SKonstantin Belousov #include "opt_ddb.h"
9486e3bf539SKonstantin Belousov 
9496e3bf539SKonstantin Belousov #ifdef DDB
9506e3bf539SKonstantin Belousov #include <ddb/ddb.h>
9516e3bf539SKonstantin Belousov 
9526e3bf539SKonstantin Belousov DB_SHOW_COMMAND(clocksource, db_show_clocksource)
9536e3bf539SKonstantin Belousov {
9546e3bf539SKonstantin Belousov 	struct pcpu_state *st;
9556e3bf539SKonstantin Belousov 	int c;
9566e3bf539SKonstantin Belousov 
9576e3bf539SKonstantin Belousov 	CPU_FOREACH(c) {
9586e3bf539SKonstantin Belousov 		st = DPCPU_ID_PTR(c, timerstate);
9596e3bf539SKonstantin Belousov 		db_printf(
9606e3bf539SKonstantin Belousov 		    "CPU %2d: action %d handle %d  ipi %d idle %d\n"
9616e3bf539SKonstantin Belousov 		    "        now %#jx nevent %#jx (%jd)\n"
9626e3bf539SKonstantin Belousov 		    "        ntick %#jx (%jd) nhard %#jx (%jd)\n"
9636e3bf539SKonstantin Belousov 		    "        nstat %#jx (%jd) nprof %#jx (%jd)\n"
9646e3bf539SKonstantin Belousov 		    "        ncall %#jx (%jd) ncallopt %#jx (%jd)\n",
9656e3bf539SKonstantin Belousov 		    c, st->action, st->handle, st->ipi, st->idle,
9666e3bf539SKonstantin Belousov 		    (uintmax_t)st->now,
9676e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextevent,
9686e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextevent - st->now) / tick_sbt,
9696e3bf539SKonstantin Belousov 		    (uintmax_t)st->nexttick,
9706e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nexttick - st->now) / tick_sbt,
9716e3bf539SKonstantin Belousov 		    (uintmax_t)st->nexthard,
9726e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nexthard - st->now) / tick_sbt,
9736e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextstat,
9746e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextstat - st->now) / tick_sbt,
9756e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextprof,
9766e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextprof - st->now) / tick_sbt,
9776e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextcall,
9786e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextcall - st->now) / tick_sbt,
9796e3bf539SKonstantin Belousov 		    (uintmax_t)st->nextcallopt,
9806e3bf539SKonstantin Belousov 		    (uintmax_t)(st->nextcallopt - st->now) / tick_sbt);
9816e3bf539SKonstantin Belousov 	}
9826e3bf539SKonstantin Belousov }
9836e3bf539SKonstantin Belousov 
9846e3bf539SKonstantin Belousov #endif
985