xref: /freebsd/sys/kern/kern_clocksource.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * Copyright (c) 2010-2013 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Common routines to manage event timers hardware.
32  */
33 
34 #include "opt_device_polling.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/kdb.h>
42 #include <sys/ktr.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/sched.h>
47 #include <sys/smp.h>
48 #include <sys/sysctl.h>
49 #include <sys/timeet.h>
50 #include <sys/timetc.h>
51 
52 #include <machine/atomic.h>
53 #include <machine/clock.h>
54 #include <machine/cpu.h>
55 #include <machine/smp.h>
56 
57 int			cpu_deepest_sleep = 0;	/* Deepest Cx state available. */
58 int			cpu_disable_c2_sleep = 0; /* Timer dies in C2. */
59 int			cpu_disable_c3_sleep = 0; /* Timer dies in C3. */
60 
61 static void		setuptimer(void);
62 static void		loadtimer(sbintime_t now, int first);
63 static int		doconfigtimer(void);
64 static void		configtimer(int start);
65 static int		round_freq(struct eventtimer *et, int freq);
66 
67 static sbintime_t	getnextcpuevent(int idle);
68 static sbintime_t	getnextevent(void);
69 static int		handleevents(sbintime_t now, int fake);
70 
71 static struct mtx	et_hw_mtx;
72 
73 #define	ET_HW_LOCK(state)						\
74 	{								\
75 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
76 			mtx_lock_spin(&(state)->et_hw_mtx);		\
77 		else							\
78 			mtx_lock_spin(&et_hw_mtx);			\
79 	}
80 
81 #define	ET_HW_UNLOCK(state)						\
82 	{								\
83 		if (timer->et_flags & ET_FLAGS_PERCPU)			\
84 			mtx_unlock_spin(&(state)->et_hw_mtx);		\
85 		else							\
86 			mtx_unlock_spin(&et_hw_mtx);			\
87 	}
88 
89 static struct eventtimer *timer = NULL;
90 static sbintime_t	timerperiod;	/* Timer period for periodic mode. */
91 static sbintime_t	statperiod;	/* statclock() events period. */
92 static sbintime_t	profperiod;	/* profclock() events period. */
93 static sbintime_t	nexttick;	/* Next global timer tick time. */
94 static u_int		busy = 1;	/* Reconfiguration is in progress. */
95 static int		profiling;	/* Profiling events enabled. */
96 
97 static char		timername[32];	/* Wanted timer. */
98 TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername));
99 
100 static int		singlemul;	/* Multiplier for periodic mode. */
101 SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RWTUN, &singlemul,
102     0, "Multiplier for periodic mode");
103 
104 static u_int		idletick;	/* Run periodic events when idle. */
105 SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RWTUN, &idletick,
106     0, "Run periodic events when idle");
107 
108 static int		periodic;	/* Periodic or one-shot mode. */
109 static int		want_periodic;	/* What mode to prefer. */
110 TUNABLE_INT("kern.eventtimer.periodic", &want_periodic);
111 
112 struct pcpu_state {
113 	struct mtx	et_hw_mtx;	/* Per-CPU timer mutex. */
114 	u_int		action;		/* Reconfiguration requests. */
115 	u_int		handle;		/* Immediate handle resuests. */
116 	sbintime_t	now;		/* Last tick time. */
117 	sbintime_t	nextevent;	/* Next scheduled event on this CPU. */
118 	sbintime_t	nexttick;	/* Next timer tick time. */
119 	sbintime_t	nexthard;	/* Next hardclock() event. */
120 	sbintime_t	nextstat;	/* Next statclock() event. */
121 	sbintime_t	nextprof;	/* Next profclock() event. */
122 	sbintime_t	nextcall;	/* Next callout event. */
123 	sbintime_t	nextcallopt;	/* Next optional callout event. */
124 	int		ipi;		/* This CPU needs IPI. */
125 	int		idle;		/* This CPU is in idle mode. */
126 };
127 
128 static DPCPU_DEFINE(struct pcpu_state, timerstate);
129 DPCPU_DEFINE(sbintime_t, hardclocktime);
130 
131 /*
132  * Timer broadcast IPI handler.
133  */
134 int
135 hardclockintr(void)
136 {
137 	sbintime_t now;
138 	struct pcpu_state *state;
139 	int done;
140 
141 	if (doconfigtimer() || busy)
142 		return (FILTER_HANDLED);
143 	state = DPCPU_PTR(timerstate);
144 	now = state->now;
145 	CTR3(KTR_SPARE2, "ipi  at %d:    now  %d.%08x",
146 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
147 	done = handleevents(now, 0);
148 	return (done ? FILTER_HANDLED : FILTER_STRAY);
149 }
150 
151 /*
152  * Handle all events for specified time on this CPU
153  */
154 static int
155 handleevents(sbintime_t now, int fake)
156 {
157 	sbintime_t t, *hct;
158 	struct trapframe *frame;
159 	struct pcpu_state *state;
160 	int usermode;
161 	int done, runs;
162 
163 	CTR3(KTR_SPARE2, "handle at %d:  now  %d.%08x",
164 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
165 	done = 0;
166 	if (fake) {
167 		frame = NULL;
168 		usermode = 0;
169 	} else {
170 		frame = curthread->td_intr_frame;
171 		usermode = TRAPF_USERMODE(frame);
172 	}
173 
174 	state = DPCPU_PTR(timerstate);
175 
176 	runs = 0;
177 	while (now >= state->nexthard) {
178 		state->nexthard += tick_sbt;
179 		runs++;
180 	}
181 	if (runs) {
182 		hct = DPCPU_PTR(hardclocktime);
183 		*hct = state->nexthard - tick_sbt;
184 		if (fake < 2) {
185 			hardclock_cnt(runs, usermode);
186 			done = 1;
187 		}
188 	}
189 	runs = 0;
190 	while (now >= state->nextstat) {
191 		state->nextstat += statperiod;
192 		runs++;
193 	}
194 	if (runs && fake < 2) {
195 		statclock_cnt(runs, usermode);
196 		done = 1;
197 	}
198 	if (profiling) {
199 		runs = 0;
200 		while (now >= state->nextprof) {
201 			state->nextprof += profperiod;
202 			runs++;
203 		}
204 		if (runs && !fake) {
205 			profclock_cnt(runs, usermode, TRAPF_PC(frame));
206 			done = 1;
207 		}
208 	} else
209 		state->nextprof = state->nextstat;
210 	if (now >= state->nextcallopt) {
211 		state->nextcall = state->nextcallopt = SBT_MAX;
212 		callout_process(now);
213 	}
214 
215 	t = getnextcpuevent(0);
216 	ET_HW_LOCK(state);
217 	if (!busy) {
218 		state->idle = 0;
219 		state->nextevent = t;
220 		loadtimer(now, (fake == 2) &&
221 		    (timer->et_flags & ET_FLAGS_PERCPU));
222 	}
223 	ET_HW_UNLOCK(state);
224 	return (done);
225 }
226 
227 /*
228  * Schedule binuptime of the next event on current CPU.
229  */
230 static sbintime_t
231 getnextcpuevent(int idle)
232 {
233 	sbintime_t event;
234 	struct pcpu_state *state;
235 	u_int hardfreq;
236 
237 	state = DPCPU_PTR(timerstate);
238 	/* Handle hardclock() events, skipping some if CPU is idle. */
239 	event = state->nexthard;
240 	if (idle) {
241 		hardfreq = (u_int)hz / 2;
242 		if (tc_min_ticktock_freq > 2
243 #ifdef SMP
244 		    && curcpu == CPU_FIRST()
245 #endif
246 		    )
247 			hardfreq = hz / tc_min_ticktock_freq;
248 		if (hardfreq > 1)
249 			event += tick_sbt * (hardfreq - 1);
250 	}
251 	/* Handle callout events. */
252 	if (event > state->nextcall)
253 		event = state->nextcall;
254 	if (!idle) { /* If CPU is active - handle other types of events. */
255 		if (event > state->nextstat)
256 			event = state->nextstat;
257 		if (profiling && event > state->nextprof)
258 			event = state->nextprof;
259 	}
260 	return (event);
261 }
262 
263 /*
264  * Schedule binuptime of the next event on all CPUs.
265  */
266 static sbintime_t
267 getnextevent(void)
268 {
269 	struct pcpu_state *state;
270 	sbintime_t event;
271 #ifdef SMP
272 	int	cpu;
273 #endif
274 	int	c;
275 
276 	state = DPCPU_PTR(timerstate);
277 	event = state->nextevent;
278 	c = -1;
279 #ifdef SMP
280 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) {
281 		CPU_FOREACH(cpu) {
282 			state = DPCPU_ID_PTR(cpu, timerstate);
283 			if (event > state->nextevent) {
284 				event = state->nextevent;
285 				c = cpu;
286 			}
287 		}
288 	}
289 #endif
290 	CTR4(KTR_SPARE2, "next at %d:    next %d.%08x by %d",
291 	    curcpu, (int)(event >> 32), (u_int)(event & 0xffffffff), c);
292 	return (event);
293 }
294 
295 /* Hardware timer callback function. */
296 static void
297 timercb(struct eventtimer *et, void *arg)
298 {
299 	sbintime_t now;
300 	sbintime_t *next;
301 	struct pcpu_state *state;
302 #ifdef SMP
303 	int cpu, bcast;
304 #endif
305 
306 	/* Do not touch anything if somebody reconfiguring timers. */
307 	if (busy)
308 		return;
309 	/* Update present and next tick times. */
310 	state = DPCPU_PTR(timerstate);
311 	if (et->et_flags & ET_FLAGS_PERCPU) {
312 		next = &state->nexttick;
313 	} else
314 		next = &nexttick;
315 	now = sbinuptime();
316 	if (periodic)
317 		*next = now + timerperiod;
318 	else
319 		*next = -1;	/* Next tick is not scheduled yet. */
320 	state->now = now;
321 	CTR3(KTR_SPARE2, "intr at %d:    now  %d.%08x",
322 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
323 
324 #ifdef SMP
325 	/* Prepare broadcasting to other CPUs for non-per-CPU timers. */
326 	bcast = 0;
327 	if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) {
328 		CPU_FOREACH(cpu) {
329 			state = DPCPU_ID_PTR(cpu, timerstate);
330 			ET_HW_LOCK(state);
331 			state->now = now;
332 			if (now >= state->nextevent) {
333 				state->nextevent += SBT_1S;
334 				if (curcpu != cpu) {
335 					state->ipi = 1;
336 					bcast = 1;
337 				}
338 			}
339 			ET_HW_UNLOCK(state);
340 		}
341 	}
342 #endif
343 
344 	/* Handle events for this time on this CPU. */
345 	handleevents(now, 0);
346 
347 #ifdef SMP
348 	/* Broadcast interrupt to other CPUs for non-per-CPU timers. */
349 	if (bcast) {
350 		CPU_FOREACH(cpu) {
351 			if (curcpu == cpu)
352 				continue;
353 			state = DPCPU_ID_PTR(cpu, timerstate);
354 			if (state->ipi) {
355 				state->ipi = 0;
356 				ipi_cpu(cpu, IPI_HARDCLOCK);
357 			}
358 		}
359 	}
360 #endif
361 }
362 
363 /*
364  * Load new value into hardware timer.
365  */
366 static void
367 loadtimer(sbintime_t now, int start)
368 {
369 	struct pcpu_state *state;
370 	sbintime_t new;
371 	sbintime_t *next;
372 	uint64_t tmp;
373 	int eq;
374 
375 	if (timer->et_flags & ET_FLAGS_PERCPU) {
376 		state = DPCPU_PTR(timerstate);
377 		next = &state->nexttick;
378 	} else
379 		next = &nexttick;
380 	if (periodic) {
381 		if (start) {
382 			/*
383 			 * Try to start all periodic timers aligned
384 			 * to period to make events synchronous.
385 			 */
386 			tmp = now % timerperiod;
387 			new = timerperiod - tmp;
388 			if (new < tmp)		/* Left less then passed. */
389 				new += timerperiod;
390 			CTR5(KTR_SPARE2, "load p at %d:   now %d.%08x first in %d.%08x",
391 			    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff),
392 			    (int)(new >> 32), (u_int)(new & 0xffffffff));
393 			*next = new + now;
394 			et_start(timer, new, timerperiod);
395 		}
396 	} else {
397 		new = getnextevent();
398 		eq = (new == *next);
399 		CTR4(KTR_SPARE2, "load at %d:    next %d.%08x eq %d",
400 		    curcpu, (int)(new >> 32), (u_int)(new & 0xffffffff), eq);
401 		if (!eq) {
402 			*next = new;
403 			et_start(timer, new - now, 0);
404 		}
405 	}
406 }
407 
408 /*
409  * Prepare event timer parameters after configuration changes.
410  */
411 static void
412 setuptimer(void)
413 {
414 	int freq;
415 
416 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
417 		periodic = 0;
418 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
419 		periodic = 1;
420 	singlemul = MIN(MAX(singlemul, 1), 20);
421 	freq = hz * singlemul;
422 	while (freq < (profiling ? profhz : stathz))
423 		freq += hz;
424 	freq = round_freq(timer, freq);
425 	timerperiod = SBT_1S / freq;
426 }
427 
428 /*
429  * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler.
430  */
431 static int
432 doconfigtimer(void)
433 {
434 	sbintime_t now;
435 	struct pcpu_state *state;
436 
437 	state = DPCPU_PTR(timerstate);
438 	switch (atomic_load_acq_int(&state->action)) {
439 	case 1:
440 		now = sbinuptime();
441 		ET_HW_LOCK(state);
442 		loadtimer(now, 1);
443 		ET_HW_UNLOCK(state);
444 		state->handle = 0;
445 		atomic_store_rel_int(&state->action, 0);
446 		return (1);
447 	case 2:
448 		ET_HW_LOCK(state);
449 		et_stop(timer);
450 		ET_HW_UNLOCK(state);
451 		state->handle = 0;
452 		atomic_store_rel_int(&state->action, 0);
453 		return (1);
454 	}
455 	if (atomic_readandclear_int(&state->handle) && !busy) {
456 		now = sbinuptime();
457 		handleevents(now, 0);
458 		return (1);
459 	}
460 	return (0);
461 }
462 
463 /*
464  * Reconfigure specified timer.
465  * For per-CPU timers use IPI to make other CPUs to reconfigure.
466  */
467 static void
468 configtimer(int start)
469 {
470 	sbintime_t now, next;
471 	struct pcpu_state *state;
472 	int cpu;
473 
474 	if (start) {
475 		setuptimer();
476 		now = sbinuptime();
477 	} else
478 		now = 0;
479 	critical_enter();
480 	ET_HW_LOCK(DPCPU_PTR(timerstate));
481 	if (start) {
482 		/* Initialize time machine parameters. */
483 		next = now + timerperiod;
484 		if (periodic)
485 			nexttick = next;
486 		else
487 			nexttick = -1;
488 		CPU_FOREACH(cpu) {
489 			state = DPCPU_ID_PTR(cpu, timerstate);
490 			state->now = now;
491 			if (!smp_started && cpu != CPU_FIRST())
492 				state->nextevent = SBT_MAX;
493 			else
494 				state->nextevent = next;
495 			if (periodic)
496 				state->nexttick = next;
497 			else
498 				state->nexttick = -1;
499 			state->nexthard = next;
500 			state->nextstat = next;
501 			state->nextprof = next;
502 			state->nextcall = next;
503 			state->nextcallopt = next;
504 			hardclock_sync(cpu);
505 		}
506 		busy = 0;
507 		/* Start global timer or per-CPU timer of this CPU. */
508 		loadtimer(now, 1);
509 	} else {
510 		busy = 1;
511 		/* Stop global timer or per-CPU timer of this CPU. */
512 		et_stop(timer);
513 	}
514 	ET_HW_UNLOCK(DPCPU_PTR(timerstate));
515 #ifdef SMP
516 	/* If timer is global or there is no other CPUs yet - we are done. */
517 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) {
518 		critical_exit();
519 		return;
520 	}
521 	/* Set reconfigure flags for other CPUs. */
522 	CPU_FOREACH(cpu) {
523 		state = DPCPU_ID_PTR(cpu, timerstate);
524 		atomic_store_rel_int(&state->action,
525 		    (cpu == curcpu) ? 0 : ( start ? 1 : 2));
526 	}
527 	/* Broadcast reconfigure IPI. */
528 	ipi_all_but_self(IPI_HARDCLOCK);
529 	/* Wait for reconfiguration completed. */
530 restart:
531 	cpu_spinwait();
532 	CPU_FOREACH(cpu) {
533 		if (cpu == curcpu)
534 			continue;
535 		state = DPCPU_ID_PTR(cpu, timerstate);
536 		if (atomic_load_acq_int(&state->action))
537 			goto restart;
538 	}
539 #endif
540 	critical_exit();
541 }
542 
543 /*
544  * Calculate nearest frequency supported by hardware timer.
545  */
546 static int
547 round_freq(struct eventtimer *et, int freq)
548 {
549 	uint64_t div;
550 
551 	if (et->et_frequency != 0) {
552 		div = lmax((et->et_frequency + freq / 2) / freq, 1);
553 		if (et->et_flags & ET_FLAGS_POW2DIV)
554 			div = 1 << (flsl(div + div / 2) - 1);
555 		freq = (et->et_frequency + div / 2) / div;
556 	}
557 	if (et->et_min_period > SBT_1S)
558 		panic("Event timer \"%s\" doesn't support sub-second periods!",
559 		    et->et_name);
560 	else if (et->et_min_period != 0)
561 		freq = min(freq, SBT2FREQ(et->et_min_period));
562 	if (et->et_max_period < SBT_1S && et->et_max_period != 0)
563 		freq = max(freq, SBT2FREQ(et->et_max_period));
564 	return (freq);
565 }
566 
567 /*
568  * Configure and start event timers (BSP part).
569  */
570 void
571 cpu_initclocks_bsp(void)
572 {
573 	struct pcpu_state *state;
574 	int base, div, cpu;
575 
576 	mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
577 	CPU_FOREACH(cpu) {
578 		state = DPCPU_ID_PTR(cpu, timerstate);
579 		mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN);
580 		state->nextcall = SBT_MAX;
581 		state->nextcallopt = SBT_MAX;
582 	}
583 	periodic = want_periodic;
584 	/* Grab requested timer or the best of present. */
585 	if (timername[0])
586 		timer = et_find(timername, 0, 0);
587 	if (timer == NULL && periodic) {
588 		timer = et_find(NULL,
589 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
590 	}
591 	if (timer == NULL) {
592 		timer = et_find(NULL,
593 		    ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT);
594 	}
595 	if (timer == NULL && !periodic) {
596 		timer = et_find(NULL,
597 		    ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC);
598 	}
599 	if (timer == NULL)
600 		panic("No usable event timer found!");
601 	et_init(timer, timercb, NULL, NULL);
602 
603 	/* Adapt to timer capabilities. */
604 	if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0)
605 		periodic = 0;
606 	else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0)
607 		periodic = 1;
608 	if (timer->et_flags & ET_FLAGS_C3STOP)
609 		cpu_disable_c3_sleep++;
610 
611 	/*
612 	 * We honor the requested 'hz' value.
613 	 * We want to run stathz in the neighborhood of 128hz.
614 	 * We would like profhz to run as often as possible.
615 	 */
616 	if (singlemul <= 0 || singlemul > 20) {
617 		if (hz >= 1500 || (hz % 128) == 0)
618 			singlemul = 1;
619 		else if (hz >= 750)
620 			singlemul = 2;
621 		else
622 			singlemul = 4;
623 	}
624 	if (periodic) {
625 		base = round_freq(timer, hz * singlemul);
626 		singlemul = max((base + hz / 2) / hz, 1);
627 		hz = (base + singlemul / 2) / singlemul;
628 		if (base <= 128)
629 			stathz = base;
630 		else {
631 			div = base / 128;
632 			if (div >= singlemul && (div % singlemul) == 0)
633 				div++;
634 			stathz = base / div;
635 		}
636 		profhz = stathz;
637 		while ((profhz + stathz) <= 128 * 64)
638 			profhz += stathz;
639 		profhz = round_freq(timer, profhz);
640 	} else {
641 		hz = round_freq(timer, hz);
642 		stathz = round_freq(timer, 127);
643 		profhz = round_freq(timer, stathz * 64);
644 	}
645 	tick = 1000000 / hz;
646 	tick_sbt = SBT_1S / hz;
647 	tick_bt = sbttobt(tick_sbt);
648 	statperiod = SBT_1S / stathz;
649 	profperiod = SBT_1S / profhz;
650 	ET_LOCK();
651 	configtimer(1);
652 	ET_UNLOCK();
653 }
654 
655 /*
656  * Start per-CPU event timers on APs.
657  */
658 void
659 cpu_initclocks_ap(void)
660 {
661 	sbintime_t now;
662 	struct pcpu_state *state;
663 	struct thread *td;
664 
665 	state = DPCPU_PTR(timerstate);
666 	now = sbinuptime();
667 	ET_HW_LOCK(state);
668 	state->now = now;
669 	hardclock_sync(curcpu);
670 	spinlock_enter();
671 	ET_HW_UNLOCK(state);
672 	td = curthread;
673 	td->td_intr_nesting_level++;
674 	handleevents(state->now, 2);
675 	td->td_intr_nesting_level--;
676 	spinlock_exit();
677 }
678 
679 /*
680  * Switch to profiling clock rates.
681  */
682 void
683 cpu_startprofclock(void)
684 {
685 
686 	ET_LOCK();
687 	if (profiling == 0) {
688 		if (periodic) {
689 			configtimer(0);
690 			profiling = 1;
691 			configtimer(1);
692 		} else
693 			profiling = 1;
694 	} else
695 		profiling++;
696 	ET_UNLOCK();
697 }
698 
699 /*
700  * Switch to regular clock rates.
701  */
702 void
703 cpu_stopprofclock(void)
704 {
705 
706 	ET_LOCK();
707 	if (profiling == 1) {
708 		if (periodic) {
709 			configtimer(0);
710 			profiling = 0;
711 			configtimer(1);
712 		} else
713 		profiling = 0;
714 	} else
715 		profiling--;
716 	ET_UNLOCK();
717 }
718 
719 /*
720  * Switch to idle mode (all ticks handled).
721  */
722 sbintime_t
723 cpu_idleclock(void)
724 {
725 	sbintime_t now, t;
726 	struct pcpu_state *state;
727 
728 	if (idletick || busy ||
729 	    (periodic && (timer->et_flags & ET_FLAGS_PERCPU))
730 #ifdef DEVICE_POLLING
731 	    || curcpu == CPU_FIRST()
732 #endif
733 	    )
734 		return (-1);
735 	state = DPCPU_PTR(timerstate);
736 	if (periodic)
737 		now = state->now;
738 	else
739 		now = sbinuptime();
740 	CTR3(KTR_SPARE2, "idle at %d:    now  %d.%08x",
741 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
742 	t = getnextcpuevent(1);
743 	ET_HW_LOCK(state);
744 	state->idle = 1;
745 	state->nextevent = t;
746 	if (!periodic)
747 		loadtimer(now, 0);
748 	ET_HW_UNLOCK(state);
749 	return (MAX(t - now, 0));
750 }
751 
752 /*
753  * Switch to active mode (skip empty ticks).
754  */
755 void
756 cpu_activeclock(void)
757 {
758 	sbintime_t now;
759 	struct pcpu_state *state;
760 	struct thread *td;
761 
762 	state = DPCPU_PTR(timerstate);
763 	if (state->idle == 0 || busy)
764 		return;
765 	if (periodic)
766 		now = state->now;
767 	else
768 		now = sbinuptime();
769 	CTR3(KTR_SPARE2, "active at %d:  now  %d.%08x",
770 	    curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff));
771 	spinlock_enter();
772 	td = curthread;
773 	td->td_intr_nesting_level++;
774 	handleevents(now, 1);
775 	td->td_intr_nesting_level--;
776 	spinlock_exit();
777 }
778 
779 /*
780  * Change the frequency of the given timer.  This changes et->et_frequency and
781  * if et is the active timer it reconfigures the timer on all CPUs.  This is
782  * intended to be a private interface for the use of et_change_frequency() only.
783  */
784 void
785 cpu_et_frequency(struct eventtimer *et, uint64_t newfreq)
786 {
787 
788 	ET_LOCK();
789 	if (et == timer) {
790 		configtimer(0);
791 		et->et_frequency = newfreq;
792 		configtimer(1);
793 	} else
794 		et->et_frequency = newfreq;
795 	ET_UNLOCK();
796 }
797 
798 void
799 cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt)
800 {
801 	struct pcpu_state *state;
802 
803 	/* Do not touch anything if somebody reconfiguring timers. */
804 	if (busy)
805 		return;
806 	CTR6(KTR_SPARE2, "new co at %d:    on %d at %d.%08x - %d.%08x",
807 	    curcpu, cpu, (int)(bt_opt >> 32), (u_int)(bt_opt & 0xffffffff),
808 	    (int)(bt >> 32), (u_int)(bt & 0xffffffff));
809 	state = DPCPU_ID_PTR(cpu, timerstate);
810 	ET_HW_LOCK(state);
811 
812 	/*
813 	 * If there is callout time already set earlier -- do nothing.
814 	 * This check may appear redundant because we check already in
815 	 * callout_process() but this double check guarantees we're safe
816 	 * with respect to race conditions between interrupts execution
817 	 * and scheduling.
818 	 */
819 	state->nextcallopt = bt_opt;
820 	if (bt >= state->nextcall)
821 		goto done;
822 	state->nextcall = bt;
823 	/* If there is some other event set earlier -- do nothing. */
824 	if (bt >= state->nextevent)
825 		goto done;
826 	state->nextevent = bt;
827 	/* If timer is periodic -- there is nothing to reprogram. */
828 	if (periodic)
829 		goto done;
830 	/* If timer is global or of the current CPU -- reprogram it. */
831 	if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || cpu == curcpu) {
832 		loadtimer(sbinuptime(), 0);
833 done:
834 		ET_HW_UNLOCK(state);
835 		return;
836 	}
837 	/* Otherwise make other CPU to reprogram it. */
838 	state->handle = 1;
839 	ET_HW_UNLOCK(state);
840 #ifdef SMP
841 	ipi_cpu(cpu, IPI_HARDCLOCK);
842 #endif
843 }
844 
845 /*
846  * Report or change the active event timers hardware.
847  */
848 static int
849 sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS)
850 {
851 	char buf[32];
852 	struct eventtimer *et;
853 	int error;
854 
855 	ET_LOCK();
856 	et = timer;
857 	snprintf(buf, sizeof(buf), "%s", et->et_name);
858 	ET_UNLOCK();
859 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
860 	ET_LOCK();
861 	et = timer;
862 	if (error != 0 || req->newptr == NULL ||
863 	    strcasecmp(buf, et->et_name) == 0) {
864 		ET_UNLOCK();
865 		return (error);
866 	}
867 	et = et_find(buf, 0, 0);
868 	if (et == NULL) {
869 		ET_UNLOCK();
870 		return (ENOENT);
871 	}
872 	configtimer(0);
873 	et_free(timer);
874 	if (et->et_flags & ET_FLAGS_C3STOP)
875 		cpu_disable_c3_sleep++;
876 	if (timer->et_flags & ET_FLAGS_C3STOP)
877 		cpu_disable_c3_sleep--;
878 	periodic = want_periodic;
879 	timer = et;
880 	et_init(timer, timercb, NULL, NULL);
881 	configtimer(1);
882 	ET_UNLOCK();
883 	return (error);
884 }
885 SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer,
886     CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
887     0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer");
888 
889 /*
890  * Report or change the active event timer periodicity.
891  */
892 static int
893 sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS)
894 {
895 	int error, val;
896 
897 	val = periodic;
898 	error = sysctl_handle_int(oidp, &val, 0, req);
899 	if (error != 0 || req->newptr == NULL)
900 		return (error);
901 	ET_LOCK();
902 	configtimer(0);
903 	periodic = want_periodic = val;
904 	configtimer(1);
905 	ET_UNLOCK();
906 	return (error);
907 }
908 SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic,
909     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
910     0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode");
911 
912 #include "opt_ddb.h"
913 
914 #ifdef DDB
915 #include <ddb/ddb.h>
916 
917 DB_SHOW_COMMAND(clocksource, db_show_clocksource)
918 {
919 	struct pcpu_state *st;
920 	int c;
921 
922 	CPU_FOREACH(c) {
923 		st = DPCPU_ID_PTR(c, timerstate);
924 		db_printf(
925 		    "CPU %2d: action %d handle %d  ipi %d idle %d\n"
926 		    "        now %#jx nevent %#jx (%jd)\n"
927 		    "        ntick %#jx (%jd) nhard %#jx (%jd)\n"
928 		    "        nstat %#jx (%jd) nprof %#jx (%jd)\n"
929 		    "        ncall %#jx (%jd) ncallopt %#jx (%jd)\n",
930 		    c, st->action, st->handle, st->ipi, st->idle,
931 		    (uintmax_t)st->now,
932 		    (uintmax_t)st->nextevent,
933 		    (uintmax_t)(st->nextevent - st->now) / tick_sbt,
934 		    (uintmax_t)st->nexttick,
935 		    (uintmax_t)(st->nexttick - st->now) / tick_sbt,
936 		    (uintmax_t)st->nexthard,
937 		    (uintmax_t)(st->nexthard - st->now) / tick_sbt,
938 		    (uintmax_t)st->nextstat,
939 		    (uintmax_t)(st->nextstat - st->now) / tick_sbt,
940 		    (uintmax_t)st->nextprof,
941 		    (uintmax_t)(st->nextprof - st->now) / tick_sbt,
942 		    (uintmax_t)st->nextcall,
943 		    (uintmax_t)(st->nextcall - st->now) / tick_sbt,
944 		    (uintmax_t)st->nextcallopt,
945 		    (uintmax_t)(st->nextcallopt - st->now) / tick_sbt);
946 	}
947 }
948 
949 #endif
950