143fe7d45SAlexander Motin /*- 243fe7d45SAlexander Motin * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org> 343fe7d45SAlexander Motin * All rights reserved. 443fe7d45SAlexander Motin * 543fe7d45SAlexander Motin * Redistribution and use in source and binary forms, with or without 643fe7d45SAlexander Motin * modification, are permitted provided that the following conditions 743fe7d45SAlexander Motin * are met: 843fe7d45SAlexander Motin * 1. Redistributions of source code must retain the above copyright 943fe7d45SAlexander Motin * notice, this list of conditions and the following disclaimer, 1043fe7d45SAlexander Motin * without modification, immediately at the beginning of the file. 1143fe7d45SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 1243fe7d45SAlexander Motin * notice, this list of conditions and the following disclaimer in the 1343fe7d45SAlexander Motin * documentation and/or other materials provided with the distribution. 1443fe7d45SAlexander Motin * 1543fe7d45SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1643fe7d45SAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1743fe7d45SAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1843fe7d45SAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1943fe7d45SAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2043fe7d45SAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2143fe7d45SAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2243fe7d45SAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2343fe7d45SAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2443fe7d45SAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2543fe7d45SAlexander Motin */ 2643fe7d45SAlexander Motin 2743fe7d45SAlexander Motin #include <sys/cdefs.h> 2843fe7d45SAlexander Motin __FBSDID("$FreeBSD$"); 2943fe7d45SAlexander Motin 3043fe7d45SAlexander Motin /* 3143fe7d45SAlexander Motin * Common routines to manage event timers hardware. 3243fe7d45SAlexander Motin */ 3343fe7d45SAlexander Motin 349dfc483cSAlexander Motin #include "opt_device_polling.h" 3543fe7d45SAlexander Motin #include "opt_kdtrace.h" 3643fe7d45SAlexander Motin 3743fe7d45SAlexander Motin #include <sys/param.h> 3843fe7d45SAlexander Motin #include <sys/systm.h> 3943fe7d45SAlexander Motin #include <sys/bus.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 5743fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS 5843fe7d45SAlexander Motin #include <sys/dtrace_bsd.h> 59dd7498aeSAndriy Gapon cyclic_clock_func_t cyclic_clock_func = NULL; 6043fe7d45SAlexander Motin #endif 6143fe7d45SAlexander Motin 62a49399a9SJung-uk Kim int cpu_can_deep_sleep = 0; /* C3 state is available. */ 63a157e425SAlexander Motin int cpu_disable_deep_sleep = 0; /* Timer dies in C3. */ 6443fe7d45SAlexander Motin 65a157e425SAlexander Motin static void setuptimer(void); 66a157e425SAlexander Motin static void loadtimer(struct bintime *now, int first); 67a157e425SAlexander Motin static int doconfigtimer(void); 68a157e425SAlexander Motin static void configtimer(int start); 69a157e425SAlexander Motin static int round_freq(struct eventtimer *et, int freq); 7043fe7d45SAlexander Motin 71a157e425SAlexander Motin static void getnextcpuevent(struct bintime *event, int idle); 72a157e425SAlexander Motin static void getnextevent(struct bintime *event); 73a157e425SAlexander Motin static int handleevents(struct bintime *now, int fake); 74a157e425SAlexander Motin #ifdef SMP 75a157e425SAlexander Motin static void cpu_new_callout(int cpu, int ticks); 76a157e425SAlexander Motin #endif 7743fe7d45SAlexander Motin 78a157e425SAlexander Motin static struct mtx et_hw_mtx; 79a157e425SAlexander Motin 80a157e425SAlexander Motin #define ET_HW_LOCK(state) \ 81a157e425SAlexander Motin { \ 82a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) \ 83a157e425SAlexander Motin mtx_lock_spin(&(state)->et_hw_mtx); \ 84a157e425SAlexander Motin else \ 85a157e425SAlexander Motin mtx_lock_spin(&et_hw_mtx); \ 86a157e425SAlexander Motin } 87a157e425SAlexander Motin 88a157e425SAlexander Motin #define ET_HW_UNLOCK(state) \ 89a157e425SAlexander Motin { \ 90a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) \ 91a157e425SAlexander Motin mtx_unlock_spin(&(state)->et_hw_mtx); \ 92a157e425SAlexander Motin else \ 93a157e425SAlexander Motin mtx_unlock_spin(&et_hw_mtx); \ 94a157e425SAlexander Motin } 95a157e425SAlexander Motin 96a157e425SAlexander Motin static struct eventtimer *timer = NULL; 97a157e425SAlexander Motin static struct bintime timerperiod; /* Timer period for periodic mode. */ 98a157e425SAlexander Motin static struct bintime hardperiod; /* hardclock() events period. */ 99a157e425SAlexander Motin static struct bintime statperiod; /* statclock() events period. */ 100a157e425SAlexander Motin static struct bintime profperiod; /* profclock() events period. */ 101a157e425SAlexander Motin static struct bintime nexttick; /* Next global timer tick time. */ 102a157e425SAlexander Motin static u_int busy = 0; /* Reconfiguration is in progress. */ 103a157e425SAlexander Motin static int profiling = 0; /* Profiling events enabled. */ 104a157e425SAlexander Motin 105a157e425SAlexander Motin static char timername[32]; /* Wanted timer. */ 106a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername)); 107a157e425SAlexander Motin 108dd9595e7SAlexander Motin static int singlemul = 0; /* Multiplier for periodic mode. */ 10943fe7d45SAlexander Motin TUNABLE_INT("kern.eventtimer.singlemul", &singlemul); 11043fe7d45SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RW, &singlemul, 111a157e425SAlexander Motin 0, "Multiplier for periodic mode"); 11243fe7d45SAlexander Motin 113a157e425SAlexander Motin static u_int idletick = 0; /* Idle mode allowed. */ 114a157e425SAlexander Motin TUNABLE_INT("kern.eventtimer.idletick", &idletick); 115fbbb13f9SMatthew D Fleming SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RW, &idletick, 116a157e425SAlexander Motin 0, "Run periodic events when idle"); 117a157e425SAlexander Motin 118a157e425SAlexander Motin static int periodic = 0; /* Periodic or one-shot mode. */ 119afe41f2dSAlexander Motin static int want_periodic = 0; /* What mode to prefer. */ 120afe41f2dSAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &want_periodic); 121a157e425SAlexander Motin 122a157e425SAlexander Motin struct pcpu_state { 123a157e425SAlexander Motin struct mtx et_hw_mtx; /* Per-CPU timer mutex. */ 124a157e425SAlexander Motin u_int action; /* Reconfiguration requests. */ 125a157e425SAlexander Motin u_int handle; /* Immediate handle resuests. */ 126a157e425SAlexander Motin struct bintime now; /* Last tick time. */ 127a157e425SAlexander Motin struct bintime nextevent; /* Next scheduled event on this CPU. */ 128a157e425SAlexander Motin struct bintime nexttick; /* Next timer tick time. */ 129a157e425SAlexander Motin struct bintime nexthard; /* Next hardlock() event. */ 130a157e425SAlexander Motin struct bintime nextstat; /* Next statclock() event. */ 131a157e425SAlexander Motin struct bintime nextprof; /* Next profclock() event. */ 132dd7498aeSAndriy Gapon #ifdef KDTRACE_HOOKS 133dd7498aeSAndriy Gapon struct bintime nextcyc; /* Next OpenSolaris cyclics event. */ 134dd7498aeSAndriy Gapon #endif 135a157e425SAlexander Motin int ipi; /* This CPU needs IPI. */ 136a157e425SAlexander Motin int idle; /* This CPU is in idle mode. */ 137a157e425SAlexander Motin }; 138a157e425SAlexander Motin 1393e288e62SDimitry Andric static DPCPU_DEFINE(struct pcpu_state, timerstate); 14043fe7d45SAlexander Motin 14143fe7d45SAlexander Motin #define FREQ2BT(freq, bt) \ 14243fe7d45SAlexander Motin { \ 14343fe7d45SAlexander Motin (bt)->sec = 0; \ 14443fe7d45SAlexander Motin (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \ 14543fe7d45SAlexander Motin } 14651636352SAlexander Motin #define BT2FREQ(bt) \ 14751636352SAlexander Motin (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \ 14851636352SAlexander Motin ((bt)->frac >> 1)) 14943fe7d45SAlexander Motin 150a157e425SAlexander Motin /* 151a157e425SAlexander Motin * Timer broadcast IPI handler. 152a157e425SAlexander Motin */ 153a157e425SAlexander Motin int 154a157e425SAlexander Motin hardclockintr(void) 15543fe7d45SAlexander Motin { 156a157e425SAlexander Motin struct bintime now; 157a157e425SAlexander Motin struct pcpu_state *state; 158a157e425SAlexander Motin int done; 15943fe7d45SAlexander Motin 160a157e425SAlexander Motin if (doconfigtimer() || busy) 161a157e425SAlexander Motin return (FILTER_HANDLED); 162a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 163a157e425SAlexander Motin now = state->now; 164a157e425SAlexander Motin CTR4(KTR_SPARE2, "ipi at %d: now %d.%08x%08x", 165a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 166a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 167a157e425SAlexander Motin done = handleevents(&now, 0); 168a157e425SAlexander Motin return (done ? FILTER_HANDLED : FILTER_STRAY); 169a157e425SAlexander Motin } 170a157e425SAlexander Motin 171a157e425SAlexander Motin /* 172a157e425SAlexander Motin * Handle all events for specified time on this CPU 173a157e425SAlexander Motin */ 174a157e425SAlexander Motin static int 175a157e425SAlexander Motin handleevents(struct bintime *now, int fake) 176a157e425SAlexander Motin { 177a157e425SAlexander Motin struct bintime t; 178a157e425SAlexander Motin struct trapframe *frame; 179a157e425SAlexander Motin struct pcpu_state *state; 180a157e425SAlexander Motin uintfptr_t pc; 181a157e425SAlexander Motin int usermode; 182a157e425SAlexander Motin int done, runs; 183a157e425SAlexander Motin 184a157e425SAlexander Motin CTR4(KTR_SPARE2, "handle at %d: now %d.%08x%08x", 185a157e425SAlexander Motin curcpu, now->sec, (unsigned int)(now->frac >> 32), 186a157e425SAlexander Motin (unsigned int)(now->frac & 0xffffffff)); 187a157e425SAlexander Motin done = 0; 188a157e425SAlexander Motin if (fake) { 189a157e425SAlexander Motin frame = NULL; 190a157e425SAlexander Motin usermode = 0; 191a157e425SAlexander Motin pc = 0; 192a157e425SAlexander Motin } else { 193a157e425SAlexander Motin frame = curthread->td_intr_frame; 194a157e425SAlexander Motin usermode = TRAPF_USERMODE(frame); 195a157e425SAlexander Motin pc = TRAPF_PC(frame); 196a157e425SAlexander Motin } 197dd7498aeSAndriy Gapon 198a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 199dd7498aeSAndriy Gapon 200*bcfd016cSAlexander Motin runs = 0; 201a157e425SAlexander Motin while (bintime_cmp(now, &state->nexthard, >=)) { 202a157e425SAlexander Motin bintime_add(&state->nexthard, &hardperiod); 203a157e425SAlexander Motin runs++; 20443fe7d45SAlexander Motin } 205c70410e6SAlexander Motin if (runs && fake < 2) { 206*bcfd016cSAlexander Motin hardclock_cnt(runs, usermode); 207a157e425SAlexander Motin done = 1; 20843fe7d45SAlexander Motin } 209*bcfd016cSAlexander Motin runs = 0; 210a157e425SAlexander Motin while (bintime_cmp(now, &state->nextstat, >=)) { 211a157e425SAlexander Motin bintime_add(&state->nextstat, &statperiod); 212*bcfd016cSAlexander Motin runs++; 213*bcfd016cSAlexander Motin } 214*bcfd016cSAlexander Motin if (runs && fake < 2) { 215*bcfd016cSAlexander Motin statclock_cnt(runs, usermode); 216a157e425SAlexander Motin done = 1; 21743fe7d45SAlexander Motin } 218a157e425SAlexander Motin if (profiling) { 219*bcfd016cSAlexander Motin runs = 0; 220a157e425SAlexander Motin while (bintime_cmp(now, &state->nextprof, >=)) { 221a157e425SAlexander Motin bintime_add(&state->nextprof, &profperiod); 222*bcfd016cSAlexander Motin runs++; 223*bcfd016cSAlexander Motin } 224*bcfd016cSAlexander Motin if (runs && !fake) { 225*bcfd016cSAlexander Motin profclock_cnt(runs, usermode, pc); 226a157e425SAlexander Motin done = 1; 22743fe7d45SAlexander Motin } 228a157e425SAlexander Motin } else 229a157e425SAlexander Motin state->nextprof = state->nextstat; 230dd7498aeSAndriy Gapon 231dd7498aeSAndriy Gapon #ifdef KDTRACE_HOOKS 232dd7498aeSAndriy Gapon if (fake == 0 && cyclic_clock_func != NULL && 233dd7498aeSAndriy Gapon state->nextcyc.sec != -1 && 234dd7498aeSAndriy Gapon bintime_cmp(now, &state->nextcyc, >=)) { 235dd7498aeSAndriy Gapon state->nextcyc.sec = -1; 236dd7498aeSAndriy Gapon (*cyclic_clock_func)(frame); 237dd7498aeSAndriy Gapon } 238dd7498aeSAndriy Gapon #endif 239dd7498aeSAndriy Gapon 240a157e425SAlexander Motin getnextcpuevent(&t, 0); 241c70410e6SAlexander Motin if (fake == 2) { 242c70410e6SAlexander Motin state->nextevent = t; 243c70410e6SAlexander Motin return (done); 244c70410e6SAlexander Motin } 245a157e425SAlexander Motin ET_HW_LOCK(state); 246a157e425SAlexander Motin if (!busy) { 247a157e425SAlexander Motin state->idle = 0; 248a157e425SAlexander Motin state->nextevent = t; 249a157e425SAlexander Motin loadtimer(now, 0); 25043fe7d45SAlexander Motin } 251a157e425SAlexander Motin ET_HW_UNLOCK(state); 252a157e425SAlexander Motin return (done); 25343fe7d45SAlexander Motin } 25443fe7d45SAlexander Motin 25543fe7d45SAlexander Motin /* 256a157e425SAlexander Motin * Schedule binuptime of the next event on current CPU. 25743fe7d45SAlexander Motin */ 25843fe7d45SAlexander Motin static void 259a157e425SAlexander Motin getnextcpuevent(struct bintime *event, int idle) 26043fe7d45SAlexander Motin { 261a157e425SAlexander Motin struct bintime tmp; 262a157e425SAlexander Motin struct pcpu_state *state; 263a157e425SAlexander Motin int skip; 26443fe7d45SAlexander Motin 265a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 266a157e425SAlexander Motin *event = state->nexthard; 267a157e425SAlexander Motin if (idle) { /* If CPU is idle - ask callouts for how long. */ 2680e189873SAlexander Motin skip = 4; 2690e189873SAlexander Motin if (curcpu == CPU_FIRST() && tc_min_ticktock_freq > skip) 2700e189873SAlexander Motin skip = tc_min_ticktock_freq; 2710e189873SAlexander Motin skip = callout_tickstofirst(hz / skip) - 1; 272a157e425SAlexander Motin CTR2(KTR_SPARE2, "skip at %d: %d", curcpu, skip); 273a157e425SAlexander Motin tmp = hardperiod; 274a157e425SAlexander Motin bintime_mul(&tmp, skip); 275a157e425SAlexander Motin bintime_add(event, &tmp); 276a157e425SAlexander Motin } else { /* If CPU is active - handle all types of events. */ 277a157e425SAlexander Motin if (bintime_cmp(event, &state->nextstat, >)) 278a157e425SAlexander Motin *event = state->nextstat; 279dd7498aeSAndriy Gapon if (profiling && bintime_cmp(event, &state->nextprof, >)) 280a157e425SAlexander Motin *event = state->nextprof; 28143fe7d45SAlexander Motin } 282dd7498aeSAndriy Gapon #ifdef KDTRACE_HOOKS 283dd7498aeSAndriy Gapon if (state->nextcyc.sec != -1 && bintime_cmp(event, &state->nextcyc, >)) 284dd7498aeSAndriy Gapon *event = state->nextcyc; 285dd7498aeSAndriy Gapon #endif 28643fe7d45SAlexander Motin } 287a157e425SAlexander Motin 288a157e425SAlexander Motin /* 289a157e425SAlexander Motin * Schedule binuptime of the next event on all CPUs. 290a157e425SAlexander Motin */ 291a157e425SAlexander Motin static void 292a157e425SAlexander Motin getnextevent(struct bintime *event) 293a157e425SAlexander Motin { 294a157e425SAlexander Motin struct pcpu_state *state; 295a157e425SAlexander Motin #ifdef SMP 296a157e425SAlexander Motin int cpu; 297a157e425SAlexander Motin #endif 298a157e425SAlexander Motin int c; 299a157e425SAlexander Motin 300a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 301a157e425SAlexander Motin *event = state->nextevent; 302a157e425SAlexander Motin c = curcpu; 303a157e425SAlexander Motin #ifdef SMP 304a157e425SAlexander Motin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) { 305a157e425SAlexander Motin CPU_FOREACH(cpu) { 306a157e425SAlexander Motin if (curcpu == cpu) 307a157e425SAlexander Motin continue; 308a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 309a157e425SAlexander Motin if (bintime_cmp(event, &state->nextevent, >)) { 310a157e425SAlexander Motin *event = state->nextevent; 311a157e425SAlexander Motin c = cpu; 31243fe7d45SAlexander Motin } 31343fe7d45SAlexander Motin } 314a157e425SAlexander Motin } 315a157e425SAlexander Motin #endif 316a157e425SAlexander Motin CTR5(KTR_SPARE2, "next at %d: next %d.%08x%08x by %d", 317a157e425SAlexander Motin curcpu, event->sec, (unsigned int)(event->frac >> 32), 318a157e425SAlexander Motin (unsigned int)(event->frac & 0xffffffff), c); 319a157e425SAlexander Motin } 320a157e425SAlexander Motin 321a157e425SAlexander Motin /* Hardware timer callback function. */ 322a157e425SAlexander Motin static void 323a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg) 324a157e425SAlexander Motin { 325a157e425SAlexander Motin struct bintime now; 326a157e425SAlexander Motin struct bintime *next; 327a157e425SAlexander Motin struct pcpu_state *state; 328a157e425SAlexander Motin #ifdef SMP 329a157e425SAlexander Motin int cpu, bcast; 330a157e425SAlexander Motin #endif 331a157e425SAlexander Motin 332a157e425SAlexander Motin /* Do not touch anything if somebody reconfiguring timers. */ 333a157e425SAlexander Motin if (busy) 334a157e425SAlexander Motin return; 335a157e425SAlexander Motin /* Update present and next tick times. */ 336a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 337a157e425SAlexander Motin if (et->et_flags & ET_FLAGS_PERCPU) { 338a157e425SAlexander Motin next = &state->nexttick; 339a157e425SAlexander Motin } else 340a157e425SAlexander Motin next = &nexttick; 341a157e425SAlexander Motin if (periodic) { 342a157e425SAlexander Motin now = *next; /* Ex-next tick time becomes present time. */ 343a157e425SAlexander Motin bintime_add(next, &timerperiod); /* Next tick in 1 period. */ 344a157e425SAlexander Motin } else { 345a157e425SAlexander Motin binuptime(&now); /* Get present time from hardware. */ 346a157e425SAlexander Motin next->sec = -1; /* Next tick is not scheduled yet. */ 347a157e425SAlexander Motin } 348a157e425SAlexander Motin state->now = now; 349a157e425SAlexander Motin CTR4(KTR_SPARE2, "intr at %d: now %d.%08x%08x", 350a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 351a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 352a157e425SAlexander Motin 353a157e425SAlexander Motin #ifdef SMP 354a157e425SAlexander Motin /* Prepare broadcasting to other CPUs for non-per-CPU timers. */ 355a157e425SAlexander Motin bcast = 0; 356a157e425SAlexander Motin if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) { 357a157e425SAlexander Motin CPU_FOREACH(cpu) { 358a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 359a157e425SAlexander Motin ET_HW_LOCK(state); 360a157e425SAlexander Motin state->now = now; 361a157e425SAlexander Motin if (bintime_cmp(&now, &state->nextevent, >=)) { 362a157e425SAlexander Motin state->nextevent.sec++; 3638e860de4SAlexander Motin if (curcpu != cpu) { 364a157e425SAlexander Motin state->ipi = 1; 365a157e425SAlexander Motin bcast = 1; 366a157e425SAlexander Motin } 3678e860de4SAlexander Motin } 368a157e425SAlexander Motin ET_HW_UNLOCK(state); 369a157e425SAlexander Motin } 370a157e425SAlexander Motin } 371a157e425SAlexander Motin #endif 372a157e425SAlexander Motin 373a157e425SAlexander Motin /* Handle events for this time on this CPU. */ 374a157e425SAlexander Motin handleevents(&now, 0); 375a157e425SAlexander Motin 376a157e425SAlexander Motin #ifdef SMP 377a157e425SAlexander Motin /* Broadcast interrupt to other CPUs for non-per-CPU timers. */ 378a157e425SAlexander Motin if (bcast) { 379a157e425SAlexander Motin CPU_FOREACH(cpu) { 380a157e425SAlexander Motin if (curcpu == cpu) 381a157e425SAlexander Motin continue; 382a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 383a157e425SAlexander Motin if (state->ipi) { 384a157e425SAlexander Motin state->ipi = 0; 385a157e425SAlexander Motin ipi_cpu(cpu, IPI_HARDCLOCK); 386a157e425SAlexander Motin } 387a157e425SAlexander Motin } 388a157e425SAlexander Motin } 389a157e425SAlexander Motin #endif 390a157e425SAlexander Motin } 391a157e425SAlexander Motin 392a157e425SAlexander Motin /* 393a157e425SAlexander Motin * Load new value into hardware timer. 394a157e425SAlexander Motin */ 395a157e425SAlexander Motin static void 396a157e425SAlexander Motin loadtimer(struct bintime *now, int start) 397a157e425SAlexander Motin { 398a157e425SAlexander Motin struct pcpu_state *state; 399a157e425SAlexander Motin struct bintime new; 400a157e425SAlexander Motin struct bintime *next; 401a157e425SAlexander Motin uint64_t tmp; 402a157e425SAlexander Motin int eq; 403a157e425SAlexander Motin 404c70410e6SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) { 405c70410e6SAlexander Motin state = DPCPU_PTR(timerstate); 406c70410e6SAlexander Motin next = &state->nexttick; 407c70410e6SAlexander Motin } else 408c70410e6SAlexander Motin next = &nexttick; 409a157e425SAlexander Motin if (periodic) { 410a157e425SAlexander Motin if (start) { 411a157e425SAlexander Motin /* 412a157e425SAlexander Motin * Try to start all periodic timers aligned 413a157e425SAlexander Motin * to period to make events synchronous. 414a157e425SAlexander Motin */ 415a157e425SAlexander Motin tmp = ((uint64_t)now->sec << 36) + (now->frac >> 28); 416a157e425SAlexander Motin tmp = (tmp % (timerperiod.frac >> 28)) << 28; 417c70410e6SAlexander Motin new.sec = 0; 418c70410e6SAlexander Motin new.frac = timerperiod.frac - tmp; 419c70410e6SAlexander Motin if (new.frac < tmp) /* Left less then passed. */ 420c70410e6SAlexander Motin bintime_add(&new, &timerperiod); 421a157e425SAlexander Motin CTR5(KTR_SPARE2, "load p at %d: now %d.%08x first in %d.%08x", 422a157e425SAlexander Motin curcpu, now->sec, (unsigned int)(now->frac >> 32), 423a157e425SAlexander Motin new.sec, (unsigned int)(new.frac >> 32)); 424c70410e6SAlexander Motin *next = new; 425c70410e6SAlexander Motin bintime_add(next, now); 426a157e425SAlexander Motin et_start(timer, &new, &timerperiod); 427a157e425SAlexander Motin } 428a157e425SAlexander Motin } else { 429a157e425SAlexander Motin getnextevent(&new); 430a157e425SAlexander Motin eq = bintime_cmp(&new, next, ==); 431a157e425SAlexander Motin CTR5(KTR_SPARE2, "load at %d: next %d.%08x%08x eq %d", 432a157e425SAlexander Motin curcpu, new.sec, (unsigned int)(new.frac >> 32), 433a157e425SAlexander Motin (unsigned int)(new.frac & 0xffffffff), 434a157e425SAlexander Motin eq); 435a157e425SAlexander Motin if (!eq) { 436a157e425SAlexander Motin *next = new; 437a157e425SAlexander Motin bintime_sub(&new, now); 438a157e425SAlexander Motin et_start(timer, &new, NULL); 439a157e425SAlexander Motin } 440a157e425SAlexander Motin } 441a157e425SAlexander Motin } 442a157e425SAlexander Motin 443a157e425SAlexander Motin /* 444a157e425SAlexander Motin * Prepare event timer parameters after configuration changes. 445a157e425SAlexander Motin */ 446a157e425SAlexander Motin static void 447a157e425SAlexander Motin setuptimer(void) 448a157e425SAlexander Motin { 449a157e425SAlexander Motin int freq; 450a157e425SAlexander Motin 451a157e425SAlexander Motin if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0) 452a157e425SAlexander Motin periodic = 0; 453a157e425SAlexander Motin else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0) 454a157e425SAlexander Motin periodic = 1; 455dd9595e7SAlexander Motin singlemul = MIN(MAX(singlemul, 1), 20); 456a157e425SAlexander Motin freq = hz * singlemul; 457a157e425SAlexander Motin while (freq < (profiling ? profhz : stathz)) 458a157e425SAlexander Motin freq += hz; 459a157e425SAlexander Motin freq = round_freq(timer, freq); 460a157e425SAlexander Motin FREQ2BT(freq, &timerperiod); 461a157e425SAlexander Motin } 46243fe7d45SAlexander Motin 46343fe7d45SAlexander Motin /* 46443fe7d45SAlexander Motin * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler. 46543fe7d45SAlexander Motin */ 466a157e425SAlexander Motin static int 467a157e425SAlexander Motin doconfigtimer(void) 46843fe7d45SAlexander Motin { 469a157e425SAlexander Motin struct bintime now; 470a157e425SAlexander Motin struct pcpu_state *state; 47143fe7d45SAlexander Motin 472a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 473a157e425SAlexander Motin switch (atomic_load_acq_int(&state->action)) { 474a157e425SAlexander Motin case 1: 475a157e425SAlexander Motin binuptime(&now); 476a157e425SAlexander Motin ET_HW_LOCK(state); 477a157e425SAlexander Motin loadtimer(&now, 1); 478a157e425SAlexander Motin ET_HW_UNLOCK(state); 479a157e425SAlexander Motin state->handle = 0; 480a157e425SAlexander Motin atomic_store_rel_int(&state->action, 0); 481a157e425SAlexander Motin return (1); 482a157e425SAlexander Motin case 2: 483a157e425SAlexander Motin ET_HW_LOCK(state); 484a157e425SAlexander Motin et_stop(timer); 485a157e425SAlexander Motin ET_HW_UNLOCK(state); 486a157e425SAlexander Motin state->handle = 0; 487a157e425SAlexander Motin atomic_store_rel_int(&state->action, 0); 488a157e425SAlexander Motin return (1); 489a157e425SAlexander Motin } 490a157e425SAlexander Motin if (atomic_readandclear_int(&state->handle) && !busy) { 491a157e425SAlexander Motin binuptime(&now); 492a157e425SAlexander Motin handleevents(&now, 0); 49343fe7d45SAlexander Motin return (1); 49443fe7d45SAlexander Motin } 49543fe7d45SAlexander Motin return (0); 49643fe7d45SAlexander Motin } 49743fe7d45SAlexander Motin 49843fe7d45SAlexander Motin /* 49943fe7d45SAlexander Motin * Reconfigure specified timer. 50043fe7d45SAlexander Motin * For per-CPU timers use IPI to make other CPUs to reconfigure. 50143fe7d45SAlexander Motin */ 50243fe7d45SAlexander Motin static void 503a157e425SAlexander Motin configtimer(int start) 50443fe7d45SAlexander Motin { 505a157e425SAlexander Motin struct bintime now, next; 506a157e425SAlexander Motin struct pcpu_state *state; 50743fe7d45SAlexander Motin int cpu; 50843fe7d45SAlexander Motin 509a157e425SAlexander Motin if (start) { 510a157e425SAlexander Motin setuptimer(); 511a157e425SAlexander Motin binuptime(&now); 512a157e425SAlexander Motin } 51343fe7d45SAlexander Motin critical_enter(); 514a157e425SAlexander Motin ET_HW_LOCK(DPCPU_PTR(timerstate)); 515a157e425SAlexander Motin if (start) { 516a157e425SAlexander Motin /* Initialize time machine parameters. */ 517a157e425SAlexander Motin next = now; 518a157e425SAlexander Motin bintime_add(&next, &timerperiod); 519a157e425SAlexander Motin if (periodic) 520a157e425SAlexander Motin nexttick = next; 52143fe7d45SAlexander Motin else 522a157e425SAlexander Motin nexttick.sec = -1; 523a157e425SAlexander Motin CPU_FOREACH(cpu) { 524a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 525a157e425SAlexander Motin state->now = now; 526a157e425SAlexander Motin state->nextevent = next; 527a157e425SAlexander Motin if (periodic) 528a157e425SAlexander Motin state->nexttick = next; 529a157e425SAlexander Motin else 530a157e425SAlexander Motin state->nexttick.sec = -1; 531a157e425SAlexander Motin state->nexthard = next; 532a157e425SAlexander Motin state->nextstat = next; 533a157e425SAlexander Motin state->nextprof = next; 534a157e425SAlexander Motin hardclock_sync(cpu); 535a157e425SAlexander Motin } 536a157e425SAlexander Motin busy = 0; 537a157e425SAlexander Motin /* Start global timer or per-CPU timer of this CPU. */ 538a157e425SAlexander Motin loadtimer(&now, 1); 539a157e425SAlexander Motin } else { 540a157e425SAlexander Motin busy = 1; 541a157e425SAlexander Motin /* Stop global timer or per-CPU timer of this CPU. */ 542a157e425SAlexander Motin et_stop(timer); 543a157e425SAlexander Motin } 544a157e425SAlexander Motin ET_HW_UNLOCK(DPCPU_PTR(timerstate)); 54543fe7d45SAlexander Motin #ifdef SMP 546a157e425SAlexander Motin /* If timer is global or there is no other CPUs yet - we are done. */ 547a157e425SAlexander Motin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) { 54843fe7d45SAlexander Motin critical_exit(); 54943fe7d45SAlexander Motin return; 55043fe7d45SAlexander Motin } 55143fe7d45SAlexander Motin /* Set reconfigure flags for other CPUs. */ 55243fe7d45SAlexander Motin CPU_FOREACH(cpu) { 553a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 554a157e425SAlexander Motin atomic_store_rel_int(&state->action, 555a157e425SAlexander Motin (cpu == curcpu) ? 0 : ( start ? 1 : 2)); 55643fe7d45SAlexander Motin } 557a157e425SAlexander Motin /* Broadcast reconfigure IPI. */ 558a157e425SAlexander Motin ipi_all_but_self(IPI_HARDCLOCK); 55943fe7d45SAlexander Motin /* Wait for reconfiguration completed. */ 56043fe7d45SAlexander Motin restart: 56143fe7d45SAlexander Motin cpu_spinwait(); 56243fe7d45SAlexander Motin CPU_FOREACH(cpu) { 56343fe7d45SAlexander Motin if (cpu == curcpu) 56443fe7d45SAlexander Motin continue; 565a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 566a157e425SAlexander Motin if (atomic_load_acq_int(&state->action)) 56743fe7d45SAlexander Motin goto restart; 56843fe7d45SAlexander Motin } 56943fe7d45SAlexander Motin #endif 570a157e425SAlexander Motin critical_exit(); 57143fe7d45SAlexander Motin } 57243fe7d45SAlexander Motin 573a157e425SAlexander Motin /* 574a157e425SAlexander Motin * Calculate nearest frequency supported by hardware timer. 575a157e425SAlexander Motin */ 57651636352SAlexander Motin static int 57751636352SAlexander Motin round_freq(struct eventtimer *et, int freq) 57851636352SAlexander Motin { 57951636352SAlexander Motin uint64_t div; 58051636352SAlexander Motin 58151636352SAlexander Motin if (et->et_frequency != 0) { 582599cf0f1SAlexander Motin div = lmax((et->et_frequency + freq / 2) / freq, 1); 58351636352SAlexander Motin if (et->et_flags & ET_FLAGS_POW2DIV) 58451636352SAlexander Motin div = 1 << (flsl(div + div / 2) - 1); 58551636352SAlexander Motin freq = (et->et_frequency + div / 2) / div; 58651636352SAlexander Motin } 58751636352SAlexander Motin if (et->et_min_period.sec > 0) 58851636352SAlexander Motin freq = 0; 589599cf0f1SAlexander Motin else if (et->et_min_period.frac != 0) 59051636352SAlexander Motin freq = min(freq, BT2FREQ(&et->et_min_period)); 59151636352SAlexander Motin if (et->et_max_period.sec == 0 && et->et_max_period.frac != 0) 59251636352SAlexander Motin freq = max(freq, BT2FREQ(&et->et_max_period)); 59351636352SAlexander Motin return (freq); 59451636352SAlexander Motin } 59551636352SAlexander Motin 59643fe7d45SAlexander Motin /* 597a157e425SAlexander Motin * Configure and start event timers (BSP part). 59843fe7d45SAlexander Motin */ 59943fe7d45SAlexander Motin void 60043fe7d45SAlexander Motin cpu_initclocks_bsp(void) 60143fe7d45SAlexander Motin { 602a157e425SAlexander Motin struct pcpu_state *state; 603a157e425SAlexander Motin int base, div, cpu; 60443fe7d45SAlexander Motin 605a157e425SAlexander Motin mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN); 606a157e425SAlexander Motin CPU_FOREACH(cpu) { 607a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 608a157e425SAlexander Motin mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN); 609dd7498aeSAndriy Gapon #ifdef KDTRACE_HOOKS 610dd7498aeSAndriy Gapon state->nextcyc.sec = -1; 611dd7498aeSAndriy Gapon #endif 612a157e425SAlexander Motin } 613a157e425SAlexander Motin #ifdef SMP 614a157e425SAlexander Motin callout_new_inserted = cpu_new_callout; 615a157e425SAlexander Motin #endif 616afe41f2dSAlexander Motin periodic = want_periodic; 617a157e425SAlexander Motin /* Grab requested timer or the best of present. */ 618a157e425SAlexander Motin if (timername[0]) 619a157e425SAlexander Motin timer = et_find(timername, 0, 0); 620a157e425SAlexander Motin if (timer == NULL && periodic) { 621a157e425SAlexander Motin timer = et_find(NULL, 62243fe7d45SAlexander Motin ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); 623a157e425SAlexander Motin } 624a157e425SAlexander Motin if (timer == NULL) { 625a157e425SAlexander Motin timer = et_find(NULL, 626a157e425SAlexander Motin ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT); 627a157e425SAlexander Motin } 628a157e425SAlexander Motin if (timer == NULL && !periodic) { 629a157e425SAlexander Motin timer = et_find(NULL, 630a157e425SAlexander Motin ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); 631a157e425SAlexander Motin } 632a157e425SAlexander Motin if (timer == NULL) 633a157e425SAlexander Motin panic("No usable event timer found!"); 634a157e425SAlexander Motin et_init(timer, timercb, NULL, NULL); 635a157e425SAlexander Motin 636a157e425SAlexander Motin /* Adapt to timer capabilities. */ 637a157e425SAlexander Motin if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0) 638a157e425SAlexander Motin periodic = 0; 639a157e425SAlexander Motin else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0) 640a157e425SAlexander Motin periodic = 1; 641a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_C3STOP) 642a157e425SAlexander Motin cpu_disable_deep_sleep++; 643a157e425SAlexander Motin 64443fe7d45SAlexander Motin /* 64543fe7d45SAlexander Motin * We honor the requested 'hz' value. 64643fe7d45SAlexander Motin * We want to run stathz in the neighborhood of 128hz. 64743fe7d45SAlexander Motin * We would like profhz to run as often as possible. 64843fe7d45SAlexander Motin */ 649dd9595e7SAlexander Motin if (singlemul <= 0 || singlemul > 20) { 65043fe7d45SAlexander Motin if (hz >= 1500 || (hz % 128) == 0) 65143fe7d45SAlexander Motin singlemul = 1; 65243fe7d45SAlexander Motin else if (hz >= 750) 65343fe7d45SAlexander Motin singlemul = 2; 65443fe7d45SAlexander Motin else 65543fe7d45SAlexander Motin singlemul = 4; 65643fe7d45SAlexander Motin } 657a157e425SAlexander Motin if (periodic) { 658a157e425SAlexander Motin base = round_freq(timer, hz * singlemul); 65951636352SAlexander Motin singlemul = max((base + hz / 2) / hz, 1); 66051636352SAlexander Motin hz = (base + singlemul / 2) / singlemul; 66151636352SAlexander Motin if (base <= 128) 66243fe7d45SAlexander Motin stathz = base; 66343fe7d45SAlexander Motin else { 66443fe7d45SAlexander Motin div = base / 128; 66551636352SAlexander Motin if (div >= singlemul && (div % singlemul) == 0) 66643fe7d45SAlexander Motin div++; 66743fe7d45SAlexander Motin stathz = base / div; 66843fe7d45SAlexander Motin } 66943fe7d45SAlexander Motin profhz = stathz; 67051636352SAlexander Motin while ((profhz + stathz) <= 128 * 64) 67143fe7d45SAlexander Motin profhz += stathz; 672a157e425SAlexander Motin profhz = round_freq(timer, profhz); 67343fe7d45SAlexander Motin } else { 674a157e425SAlexander Motin hz = round_freq(timer, hz); 675a157e425SAlexander Motin stathz = round_freq(timer, 127); 676a157e425SAlexander Motin profhz = round_freq(timer, stathz * 64); 67743fe7d45SAlexander Motin } 678599cf0f1SAlexander Motin tick = 1000000 / hz; 679a157e425SAlexander Motin FREQ2BT(hz, &hardperiod); 680a157e425SAlexander Motin FREQ2BT(stathz, &statperiod); 681a157e425SAlexander Motin FREQ2BT(profhz, &profperiod); 68243fe7d45SAlexander Motin ET_LOCK(); 683a157e425SAlexander Motin configtimer(1); 68443fe7d45SAlexander Motin ET_UNLOCK(); 68543fe7d45SAlexander Motin } 68643fe7d45SAlexander Motin 687a157e425SAlexander Motin /* 688a157e425SAlexander Motin * Start per-CPU event timers on APs. 689a157e425SAlexander Motin */ 69043fe7d45SAlexander Motin void 69143fe7d45SAlexander Motin cpu_initclocks_ap(void) 69243fe7d45SAlexander Motin { 693a157e425SAlexander Motin struct bintime now; 694a157e425SAlexander Motin struct pcpu_state *state; 69543fe7d45SAlexander Motin 696a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 697a157e425SAlexander Motin binuptime(&now); 698a157e425SAlexander Motin ET_HW_LOCK(state); 699c70410e6SAlexander Motin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 && periodic) { 700c70410e6SAlexander Motin state->now = nexttick; 701c70410e6SAlexander Motin bintime_sub(&state->now, &timerperiod); 702c70410e6SAlexander Motin } else 703c70410e6SAlexander Motin state->now = now; 704c70410e6SAlexander Motin hardclock_sync(curcpu); 705c70410e6SAlexander Motin handleevents(&state->now, 2); 706c70410e6SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) 707a157e425SAlexander Motin loadtimer(&now, 1); 708a157e425SAlexander Motin ET_HW_UNLOCK(state); 70943fe7d45SAlexander Motin } 71043fe7d45SAlexander Motin 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(); 719a157e425SAlexander Motin if (periodic) { 720a157e425SAlexander Motin configtimer(0); 721a157e425SAlexander Motin profiling = 1; 722a157e425SAlexander Motin configtimer(1); 723a157e425SAlexander Motin } else 724a157e425SAlexander Motin profiling = 1; 72543fe7d45SAlexander Motin ET_UNLOCK(); 72643fe7d45SAlexander Motin } 72743fe7d45SAlexander Motin 728a157e425SAlexander Motin /* 729a157e425SAlexander Motin * Switch to regular clock rates. 730a157e425SAlexander Motin */ 73143fe7d45SAlexander Motin void 73243fe7d45SAlexander Motin cpu_stopprofclock(void) 73343fe7d45SAlexander Motin { 73443fe7d45SAlexander Motin 73543fe7d45SAlexander Motin ET_LOCK(); 736a157e425SAlexander Motin if (periodic) { 737a157e425SAlexander Motin configtimer(0); 738a157e425SAlexander Motin profiling = 0; 739a157e425SAlexander Motin configtimer(1); 740a157e425SAlexander Motin } else 741a157e425SAlexander Motin profiling = 0; 74243fe7d45SAlexander Motin ET_UNLOCK(); 74343fe7d45SAlexander Motin } 74443fe7d45SAlexander Motin 745a157e425SAlexander Motin /* 746a157e425SAlexander Motin * Switch to idle mode (all ticks handled). 747a157e425SAlexander Motin */ 748a157e425SAlexander Motin void 749a157e425SAlexander Motin cpu_idleclock(void) 750a157e425SAlexander Motin { 751a157e425SAlexander Motin struct bintime now, t; 752a157e425SAlexander Motin struct pcpu_state *state; 753a157e425SAlexander Motin 754a157e425SAlexander Motin if (idletick || busy || 7559dfc483cSAlexander Motin (periodic && (timer->et_flags & ET_FLAGS_PERCPU)) 7569dfc483cSAlexander Motin #ifdef DEVICE_POLLING 7579dfc483cSAlexander Motin || curcpu == CPU_FIRST() 7589dfc483cSAlexander Motin #endif 7599dfc483cSAlexander Motin ) 760a157e425SAlexander Motin return; 761a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 762a157e425SAlexander Motin if (periodic) 763a157e425SAlexander Motin now = state->now; 764a157e425SAlexander Motin else 765a157e425SAlexander Motin binuptime(&now); 766a157e425SAlexander Motin CTR4(KTR_SPARE2, "idle at %d: now %d.%08x%08x", 767a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 768a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 769a157e425SAlexander Motin getnextcpuevent(&t, 1); 770a157e425SAlexander Motin ET_HW_LOCK(state); 771a157e425SAlexander Motin state->idle = 1; 772a157e425SAlexander Motin state->nextevent = t; 773a157e425SAlexander Motin if (!periodic) 774a157e425SAlexander Motin loadtimer(&now, 0); 775a157e425SAlexander Motin ET_HW_UNLOCK(state); 776a157e425SAlexander Motin } 777a157e425SAlexander Motin 778a157e425SAlexander Motin /* 779a157e425SAlexander Motin * Switch to active mode (skip empty ticks). 780a157e425SAlexander Motin */ 781a157e425SAlexander Motin void 782a157e425SAlexander Motin cpu_activeclock(void) 783a157e425SAlexander Motin { 784a157e425SAlexander Motin struct bintime now; 785a157e425SAlexander Motin struct pcpu_state *state; 786a157e425SAlexander Motin struct thread *td; 787a157e425SAlexander Motin 788a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 789a157e425SAlexander Motin if (state->idle == 0 || busy) 790a157e425SAlexander Motin return; 791a157e425SAlexander Motin if (periodic) 792a157e425SAlexander Motin now = state->now; 793a157e425SAlexander Motin else 794a157e425SAlexander Motin binuptime(&now); 795a157e425SAlexander Motin CTR4(KTR_SPARE2, "active at %d: now %d.%08x%08x", 796a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 797a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 798a157e425SAlexander Motin spinlock_enter(); 799a157e425SAlexander Motin td = curthread; 800a157e425SAlexander Motin td->td_intr_nesting_level++; 801a157e425SAlexander Motin handleevents(&now, 1); 802a157e425SAlexander Motin td->td_intr_nesting_level--; 803a157e425SAlexander Motin spinlock_exit(); 804a157e425SAlexander Motin } 805a157e425SAlexander Motin 806dd7498aeSAndriy Gapon #ifdef KDTRACE_HOOKS 807dd7498aeSAndriy Gapon void 808dd7498aeSAndriy Gapon clocksource_cyc_set(const struct bintime *t) 809dd7498aeSAndriy Gapon { 810dd7498aeSAndriy Gapon struct bintime now; 811dd7498aeSAndriy Gapon struct pcpu_state *state; 812dd7498aeSAndriy Gapon 813dd7498aeSAndriy Gapon state = DPCPU_PTR(timerstate); 814dd7498aeSAndriy Gapon if (periodic) 815dd7498aeSAndriy Gapon now = state->now; 816dd7498aeSAndriy Gapon else 817dd7498aeSAndriy Gapon binuptime(&now); 818dd7498aeSAndriy Gapon 819dd7498aeSAndriy Gapon CTR4(KTR_SPARE2, "set_cyc at %d: now %d.%08x%08x", 820dd7498aeSAndriy Gapon curcpu, now.sec, (unsigned int)(now.frac >> 32), 821dd7498aeSAndriy Gapon (unsigned int)(now.frac & 0xffffffff)); 822dd7498aeSAndriy Gapon CTR4(KTR_SPARE2, "set_cyc at %d: t %d.%08x%08x", 823dd7498aeSAndriy Gapon curcpu, t->sec, (unsigned int)(t->frac >> 32), 824dd7498aeSAndriy Gapon (unsigned int)(t->frac & 0xffffffff)); 825dd7498aeSAndriy Gapon 826dd7498aeSAndriy Gapon ET_HW_LOCK(state); 827dd7498aeSAndriy Gapon if (bintime_cmp(t, &state->nextcyc, ==)) { 828dd7498aeSAndriy Gapon ET_HW_UNLOCK(state); 829dd7498aeSAndriy Gapon return; 830dd7498aeSAndriy Gapon } 831dd7498aeSAndriy Gapon state->nextcyc = *t; 832dd7498aeSAndriy Gapon if (bintime_cmp(&state->nextcyc, &state->nextevent, >=)) { 833dd7498aeSAndriy Gapon ET_HW_UNLOCK(state); 834dd7498aeSAndriy Gapon return; 835dd7498aeSAndriy Gapon } 836dd7498aeSAndriy Gapon state->nextevent = state->nextcyc; 837dd7498aeSAndriy Gapon if (!periodic) 838dd7498aeSAndriy Gapon loadtimer(&now, 0); 839dd7498aeSAndriy Gapon ET_HW_UNLOCK(state); 840dd7498aeSAndriy Gapon } 841dd7498aeSAndriy Gapon #endif 842dd7498aeSAndriy Gapon 843a157e425SAlexander Motin #ifdef SMP 844a157e425SAlexander Motin static void 845a157e425SAlexander Motin cpu_new_callout(int cpu, int ticks) 846a157e425SAlexander Motin { 847a157e425SAlexander Motin struct bintime tmp; 848a157e425SAlexander Motin struct pcpu_state *state; 849a157e425SAlexander Motin 850a157e425SAlexander Motin CTR3(KTR_SPARE2, "new co at %d: on %d in %d", 851a157e425SAlexander Motin curcpu, cpu, ticks); 852a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 853a157e425SAlexander Motin ET_HW_LOCK(state); 854a157e425SAlexander Motin if (state->idle == 0 || busy) { 855a157e425SAlexander Motin ET_HW_UNLOCK(state); 856a157e425SAlexander Motin return; 857a157e425SAlexander Motin } 858a157e425SAlexander Motin /* 859a157e425SAlexander Motin * If timer is periodic - just update next event time for target CPU. 860bcb74c4cSAlexander Motin * If timer is global - there is chance it is already programmed. 861a157e425SAlexander Motin */ 862bcb74c4cSAlexander Motin if (periodic || (timer->et_flags & ET_FLAGS_PERCPU) == 0) { 863a157e425SAlexander Motin tmp = hardperiod; 864a157e425SAlexander Motin bintime_mul(&tmp, ticks - 1); 86555c71d63SAlexander Motin bintime_add(&tmp, &state->nexthard); 86655c71d63SAlexander Motin if (bintime_cmp(&tmp, &state->nextevent, <)) 86755c71d63SAlexander Motin state->nextevent = tmp; 868bcb74c4cSAlexander Motin if (periodic || 869bcb74c4cSAlexander Motin bintime_cmp(&state->nextevent, &nexttick, >=)) { 870a157e425SAlexander Motin ET_HW_UNLOCK(state); 871a157e425SAlexander Motin return; 872a157e425SAlexander Motin } 873bcb74c4cSAlexander Motin } 874a157e425SAlexander Motin /* 875a157e425SAlexander Motin * Otherwise we have to wake that CPU up, as we can't get present 876a157e425SAlexander Motin * bintime to reprogram global timer from here. If timer is per-CPU, 877a157e425SAlexander Motin * we by definition can't do it from here. 878a157e425SAlexander Motin */ 879a157e425SAlexander Motin ET_HW_UNLOCK(state); 880a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) { 881a157e425SAlexander Motin state->handle = 1; 882a157e425SAlexander Motin ipi_cpu(cpu, IPI_HARDCLOCK); 883a157e425SAlexander Motin } else { 884a157e425SAlexander Motin if (!cpu_idle_wakeup(cpu)) 885a157e425SAlexander Motin ipi_cpu(cpu, IPI_AST); 886a157e425SAlexander Motin } 887a157e425SAlexander Motin } 888a157e425SAlexander Motin #endif 889a157e425SAlexander Motin 890a157e425SAlexander Motin /* 891a157e425SAlexander Motin * Report or change the active event timers hardware. 892a157e425SAlexander Motin */ 89343fe7d45SAlexander Motin static int 894a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS) 89543fe7d45SAlexander Motin { 89643fe7d45SAlexander Motin char buf[32]; 89743fe7d45SAlexander Motin struct eventtimer *et; 89843fe7d45SAlexander Motin int error; 89943fe7d45SAlexander Motin 90043fe7d45SAlexander Motin ET_LOCK(); 901a157e425SAlexander Motin et = timer; 90243fe7d45SAlexander Motin snprintf(buf, sizeof(buf), "%s", et->et_name); 90343fe7d45SAlexander Motin ET_UNLOCK(); 90443fe7d45SAlexander Motin error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 90543fe7d45SAlexander Motin ET_LOCK(); 906a157e425SAlexander Motin et = timer; 90743fe7d45SAlexander Motin if (error != 0 || req->newptr == NULL || 908a157e425SAlexander Motin strcasecmp(buf, et->et_name) == 0) { 90943fe7d45SAlexander Motin ET_UNLOCK(); 91043fe7d45SAlexander Motin return (error); 91143fe7d45SAlexander Motin } 912a157e425SAlexander Motin et = et_find(buf, 0, 0); 91343fe7d45SAlexander Motin if (et == NULL) { 91443fe7d45SAlexander Motin ET_UNLOCK(); 91543fe7d45SAlexander Motin return (ENOENT); 91643fe7d45SAlexander Motin } 91743fe7d45SAlexander Motin configtimer(0); 918a157e425SAlexander Motin et_free(timer); 919a157e425SAlexander Motin if (et->et_flags & ET_FLAGS_C3STOP) 920a157e425SAlexander Motin cpu_disable_deep_sleep++; 921a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_C3STOP) 922a157e425SAlexander Motin cpu_disable_deep_sleep--; 923afe41f2dSAlexander Motin periodic = want_periodic; 924a157e425SAlexander Motin timer = et; 925a157e425SAlexander Motin et_init(timer, timercb, NULL, NULL); 92643fe7d45SAlexander Motin configtimer(1); 92743fe7d45SAlexander Motin ET_UNLOCK(); 92843fe7d45SAlexander Motin return (error); 92943fe7d45SAlexander Motin } 930a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer, 93143fe7d45SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 932dd9595e7SAlexander Motin 0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer"); 933a157e425SAlexander Motin 934a157e425SAlexander Motin /* 935a157e425SAlexander Motin * Report or change the active event timer periodicity. 936a157e425SAlexander Motin */ 937a157e425SAlexander Motin static int 938a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS) 939a157e425SAlexander Motin { 940a157e425SAlexander Motin int error, val; 941a157e425SAlexander Motin 942a157e425SAlexander Motin val = periodic; 943a157e425SAlexander Motin error = sysctl_handle_int(oidp, &val, 0, req); 944a157e425SAlexander Motin if (error != 0 || req->newptr == NULL) 945a157e425SAlexander Motin return (error); 946a157e425SAlexander Motin ET_LOCK(); 947a157e425SAlexander Motin configtimer(0); 948afe41f2dSAlexander Motin periodic = want_periodic = val; 949a157e425SAlexander Motin configtimer(1); 950a157e425SAlexander Motin ET_UNLOCK(); 951a157e425SAlexander Motin return (error); 952a157e425SAlexander Motin } 953a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic, 954a157e425SAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 955dd9595e7SAlexander Motin 0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode"); 956