143fe7d45SAlexander Motin /*- 243fe7d45SAlexander Motin * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org> 343fe7d45SAlexander Motin * All rights reserved. 443fe7d45SAlexander Motin * 543fe7d45SAlexander Motin * Redistribution and use in source and binary forms, with or without 643fe7d45SAlexander Motin * modification, are permitted provided that the following conditions 743fe7d45SAlexander Motin * are met: 843fe7d45SAlexander Motin * 1. Redistributions of source code must retain the above copyright 943fe7d45SAlexander Motin * notice, this list of conditions and the following disclaimer, 1043fe7d45SAlexander Motin * without modification, immediately at the beginning of the file. 1143fe7d45SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 1243fe7d45SAlexander Motin * notice, this list of conditions and the following disclaimer in the 1343fe7d45SAlexander Motin * documentation and/or other materials provided with the distribution. 1443fe7d45SAlexander Motin * 1543fe7d45SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1643fe7d45SAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1743fe7d45SAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1843fe7d45SAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1943fe7d45SAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2043fe7d45SAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2143fe7d45SAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2243fe7d45SAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2343fe7d45SAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2443fe7d45SAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2543fe7d45SAlexander Motin */ 2643fe7d45SAlexander Motin 2743fe7d45SAlexander Motin #include <sys/cdefs.h> 2843fe7d45SAlexander Motin __FBSDID("$FreeBSD$"); 2943fe7d45SAlexander Motin 3043fe7d45SAlexander Motin /* 3143fe7d45SAlexander Motin * Common routines to manage event timers hardware. 3243fe7d45SAlexander Motin */ 3343fe7d45SAlexander Motin 3443fe7d45SAlexander Motin /* XEN has own timer routines now. */ 3543fe7d45SAlexander Motin #ifndef XEN 3643fe7d45SAlexander Motin 37*9dfc483cSAlexander Motin #include "opt_device_polling.h" 3843fe7d45SAlexander Motin #include "opt_kdtrace.h" 3943fe7d45SAlexander Motin 4043fe7d45SAlexander Motin #include <sys/param.h> 4143fe7d45SAlexander Motin #include <sys/systm.h> 4243fe7d45SAlexander Motin #include <sys/bus.h> 4343fe7d45SAlexander Motin #include <sys/lock.h> 4443fe7d45SAlexander Motin #include <sys/kdb.h> 45a157e425SAlexander Motin #include <sys/ktr.h> 4643fe7d45SAlexander Motin #include <sys/mutex.h> 4743fe7d45SAlexander Motin #include <sys/proc.h> 4843fe7d45SAlexander Motin #include <sys/kernel.h> 4943fe7d45SAlexander Motin #include <sys/sched.h> 5043fe7d45SAlexander Motin #include <sys/smp.h> 5143fe7d45SAlexander Motin #include <sys/sysctl.h> 5243fe7d45SAlexander Motin #include <sys/timeet.h> 530e189873SAlexander Motin #include <sys/timetc.h> 5443fe7d45SAlexander Motin 5543fe7d45SAlexander Motin #include <machine/atomic.h> 5643fe7d45SAlexander Motin #include <machine/clock.h> 5743fe7d45SAlexander Motin #include <machine/cpu.h> 5843fe7d45SAlexander Motin #include <machine/smp.h> 5943fe7d45SAlexander Motin 6043fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS 6143fe7d45SAlexander Motin #include <sys/dtrace_bsd.h> 6243fe7d45SAlexander Motin cyclic_clock_func_t cyclic_clock_func[MAXCPU]; 6343fe7d45SAlexander Motin #endif 6443fe7d45SAlexander Motin 65a157e425SAlexander Motin int cpu_disable_deep_sleep = 0; /* Timer dies in C3. */ 6643fe7d45SAlexander Motin 67a157e425SAlexander Motin static void setuptimer(void); 68a157e425SAlexander Motin static void loadtimer(struct bintime *now, int first); 69a157e425SAlexander Motin static int doconfigtimer(void); 70a157e425SAlexander Motin static void configtimer(int start); 71a157e425SAlexander Motin static int round_freq(struct eventtimer *et, int freq); 7243fe7d45SAlexander Motin 73a157e425SAlexander Motin static void getnextcpuevent(struct bintime *event, int idle); 74a157e425SAlexander Motin static void getnextevent(struct bintime *event); 75a157e425SAlexander Motin static int handleevents(struct bintime *now, int fake); 76a157e425SAlexander Motin #ifdef SMP 77a157e425SAlexander Motin static void cpu_new_callout(int cpu, int ticks); 78a157e425SAlexander Motin #endif 7943fe7d45SAlexander Motin 80a157e425SAlexander Motin static struct mtx et_hw_mtx; 81a157e425SAlexander Motin 82a157e425SAlexander Motin #define ET_HW_LOCK(state) \ 83a157e425SAlexander Motin { \ 84a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) \ 85a157e425SAlexander Motin mtx_lock_spin(&(state)->et_hw_mtx); \ 86a157e425SAlexander Motin else \ 87a157e425SAlexander Motin mtx_lock_spin(&et_hw_mtx); \ 88a157e425SAlexander Motin } 89a157e425SAlexander Motin 90a157e425SAlexander Motin #define ET_HW_UNLOCK(state) \ 91a157e425SAlexander Motin { \ 92a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) \ 93a157e425SAlexander Motin mtx_unlock_spin(&(state)->et_hw_mtx); \ 94a157e425SAlexander Motin else \ 95a157e425SAlexander Motin mtx_unlock_spin(&et_hw_mtx); \ 96a157e425SAlexander Motin } 97a157e425SAlexander Motin 98a157e425SAlexander Motin static struct eventtimer *timer = NULL; 99a157e425SAlexander Motin static struct bintime timerperiod; /* Timer period for periodic mode. */ 100a157e425SAlexander Motin static struct bintime hardperiod; /* hardclock() events period. */ 101a157e425SAlexander Motin static struct bintime statperiod; /* statclock() events period. */ 102a157e425SAlexander Motin static struct bintime profperiod; /* profclock() events period. */ 103a157e425SAlexander Motin static struct bintime nexttick; /* Next global timer tick time. */ 104a157e425SAlexander Motin static u_int busy = 0; /* Reconfiguration is in progress. */ 105a157e425SAlexander Motin static int profiling = 0; /* Profiling events enabled. */ 106a157e425SAlexander Motin 107a157e425SAlexander Motin static char timername[32]; /* Wanted timer. */ 108a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername)); 109a157e425SAlexander Motin 110dd9595e7SAlexander Motin static int singlemul = 0; /* Multiplier for periodic mode. */ 11143fe7d45SAlexander Motin TUNABLE_INT("kern.eventtimer.singlemul", &singlemul); 11243fe7d45SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RW, &singlemul, 113a157e425SAlexander Motin 0, "Multiplier for periodic mode"); 11443fe7d45SAlexander Motin 115a157e425SAlexander Motin static u_int idletick = 0; /* Idle mode allowed. */ 116a157e425SAlexander Motin TUNABLE_INT("kern.eventtimer.idletick", &idletick); 117a157e425SAlexander Motin SYSCTL_INT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RW, &idletick, 118a157e425SAlexander Motin 0, "Run periodic events when idle"); 119a157e425SAlexander Motin 120a157e425SAlexander Motin static int periodic = 0; /* Periodic or one-shot mode. */ 121afe41f2dSAlexander Motin static int want_periodic = 0; /* What mode to prefer. */ 122afe41f2dSAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &want_periodic); 123a157e425SAlexander Motin 124a157e425SAlexander Motin struct pcpu_state { 125a157e425SAlexander Motin struct mtx et_hw_mtx; /* Per-CPU timer mutex. */ 126a157e425SAlexander Motin u_int action; /* Reconfiguration requests. */ 127a157e425SAlexander Motin u_int handle; /* Immediate handle resuests. */ 128a157e425SAlexander Motin struct bintime now; /* Last tick time. */ 129a157e425SAlexander Motin struct bintime nextevent; /* Next scheduled event on this CPU. */ 130a157e425SAlexander Motin struct bintime nexttick; /* Next timer tick time. */ 131a157e425SAlexander Motin struct bintime nexthard; /* Next hardlock() event. */ 132a157e425SAlexander Motin struct bintime nextstat; /* Next statclock() event. */ 133a157e425SAlexander Motin struct bintime nextprof; /* Next profclock() event. */ 134a157e425SAlexander Motin int ipi; /* This CPU needs IPI. */ 135a157e425SAlexander Motin int idle; /* This CPU is in idle mode. */ 136a157e425SAlexander Motin }; 137a157e425SAlexander Motin 138a157e425SAlexander Motin static DPCPU_DEFINE(struct pcpu_state, timerstate); 13943fe7d45SAlexander Motin 14043fe7d45SAlexander Motin #define FREQ2BT(freq, bt) \ 14143fe7d45SAlexander Motin { \ 14243fe7d45SAlexander Motin (bt)->sec = 0; \ 14343fe7d45SAlexander Motin (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \ 14443fe7d45SAlexander Motin } 14551636352SAlexander Motin #define BT2FREQ(bt) \ 14651636352SAlexander Motin (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \ 14751636352SAlexander Motin ((bt)->frac >> 1)) 14843fe7d45SAlexander Motin 149a157e425SAlexander Motin /* 150a157e425SAlexander Motin * Timer broadcast IPI handler. 151a157e425SAlexander Motin */ 152a157e425SAlexander Motin int 153a157e425SAlexander Motin hardclockintr(void) 15443fe7d45SAlexander Motin { 155a157e425SAlexander Motin struct bintime now; 156a157e425SAlexander Motin struct pcpu_state *state; 157a157e425SAlexander Motin int done; 15843fe7d45SAlexander Motin 159a157e425SAlexander Motin if (doconfigtimer() || busy) 160a157e425SAlexander Motin return (FILTER_HANDLED); 161a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 162a157e425SAlexander Motin now = state->now; 163a157e425SAlexander Motin CTR4(KTR_SPARE2, "ipi at %d: now %d.%08x%08x", 164a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 165a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 166a157e425SAlexander Motin done = handleevents(&now, 0); 167a157e425SAlexander Motin return (done ? FILTER_HANDLED : FILTER_STRAY); 168a157e425SAlexander Motin } 169a157e425SAlexander Motin 170a157e425SAlexander Motin /* 171a157e425SAlexander Motin * Handle all events for specified time on this CPU 172a157e425SAlexander Motin */ 173a157e425SAlexander Motin static int 174a157e425SAlexander Motin handleevents(struct bintime *now, int fake) 175a157e425SAlexander Motin { 176a157e425SAlexander Motin struct bintime t; 177a157e425SAlexander Motin struct trapframe *frame; 178a157e425SAlexander Motin struct pcpu_state *state; 179a157e425SAlexander Motin uintfptr_t pc; 180a157e425SAlexander Motin int usermode; 181a157e425SAlexander Motin int done, runs; 182a157e425SAlexander Motin 183a157e425SAlexander Motin CTR4(KTR_SPARE2, "handle at %d: now %d.%08x%08x", 184a157e425SAlexander Motin curcpu, now->sec, (unsigned int)(now->frac >> 32), 185a157e425SAlexander Motin (unsigned int)(now->frac & 0xffffffff)); 186a157e425SAlexander Motin done = 0; 187a157e425SAlexander Motin if (fake) { 188a157e425SAlexander Motin frame = NULL; 189a157e425SAlexander Motin usermode = 0; 190a157e425SAlexander Motin pc = 0; 191a157e425SAlexander Motin } else { 192a157e425SAlexander Motin frame = curthread->td_intr_frame; 193a157e425SAlexander Motin usermode = TRAPF_USERMODE(frame); 194a157e425SAlexander Motin pc = TRAPF_PC(frame); 195a157e425SAlexander Motin } 19643fe7d45SAlexander Motin #ifdef KDTRACE_HOOKS 19743fe7d45SAlexander Motin /* 19843fe7d45SAlexander Motin * If the DTrace hooks are configured and a callback function 19943fe7d45SAlexander Motin * has been registered, then call it to process the high speed 20043fe7d45SAlexander Motin * timers. 20143fe7d45SAlexander Motin */ 202a157e425SAlexander Motin if (!fake && cyclic_clock_func[curcpu] != NULL) 203a157e425SAlexander Motin (*cyclic_clock_func[curcpu])(frame); 20443fe7d45SAlexander Motin #endif 205a157e425SAlexander Motin runs = 0; 206a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 207a157e425SAlexander Motin while (bintime_cmp(now, &state->nexthard, >=)) { 208a157e425SAlexander Motin bintime_add(&state->nexthard, &hardperiod); 209a157e425SAlexander Motin runs++; 21043fe7d45SAlexander Motin } 211a157e425SAlexander Motin if (runs) { 212a157e425SAlexander Motin hardclock_anycpu(runs, usermode); 213a157e425SAlexander Motin done = 1; 21443fe7d45SAlexander Motin } 215a157e425SAlexander Motin while (bintime_cmp(now, &state->nextstat, >=)) { 216a157e425SAlexander Motin statclock(usermode); 217a157e425SAlexander Motin bintime_add(&state->nextstat, &statperiod); 218a157e425SAlexander Motin done = 1; 21943fe7d45SAlexander Motin } 220a157e425SAlexander Motin if (profiling) { 221a157e425SAlexander Motin while (bintime_cmp(now, &state->nextprof, >=)) { 222a157e425SAlexander Motin if (!fake) 223a157e425SAlexander Motin profclock(usermode, pc); 224a157e425SAlexander Motin bintime_add(&state->nextprof, &profperiod); 225a157e425SAlexander Motin done = 1; 22643fe7d45SAlexander Motin } 227a157e425SAlexander Motin } else 228a157e425SAlexander Motin state->nextprof = state->nextstat; 229a157e425SAlexander Motin getnextcpuevent(&t, 0); 230a157e425SAlexander Motin ET_HW_LOCK(state); 231a157e425SAlexander Motin if (!busy) { 232a157e425SAlexander Motin state->idle = 0; 233a157e425SAlexander Motin state->nextevent = t; 234a157e425SAlexander Motin loadtimer(now, 0); 23543fe7d45SAlexander Motin } 236a157e425SAlexander Motin ET_HW_UNLOCK(state); 237a157e425SAlexander Motin return (done); 23843fe7d45SAlexander Motin } 23943fe7d45SAlexander Motin 24043fe7d45SAlexander Motin /* 241a157e425SAlexander Motin * Schedule binuptime of the next event on current CPU. 24243fe7d45SAlexander Motin */ 24343fe7d45SAlexander Motin static void 244a157e425SAlexander Motin getnextcpuevent(struct bintime *event, int idle) 24543fe7d45SAlexander Motin { 246a157e425SAlexander Motin struct bintime tmp; 247a157e425SAlexander Motin struct pcpu_state *state; 248a157e425SAlexander Motin int skip; 24943fe7d45SAlexander Motin 250a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 251a157e425SAlexander Motin *event = state->nexthard; 252a157e425SAlexander Motin if (idle) { /* If CPU is idle - ask callouts for how long. */ 2530e189873SAlexander Motin skip = 4; 2540e189873SAlexander Motin if (curcpu == CPU_FIRST() && tc_min_ticktock_freq > skip) 2550e189873SAlexander Motin skip = tc_min_ticktock_freq; 2560e189873SAlexander Motin skip = callout_tickstofirst(hz / skip) - 1; 257a157e425SAlexander Motin CTR2(KTR_SPARE2, "skip at %d: %d", curcpu, skip); 258a157e425SAlexander Motin tmp = hardperiod; 259a157e425SAlexander Motin bintime_mul(&tmp, skip); 260a157e425SAlexander Motin bintime_add(event, &tmp); 261a157e425SAlexander Motin } else { /* If CPU is active - handle all types of events. */ 262a157e425SAlexander Motin if (bintime_cmp(event, &state->nextstat, >)) 263a157e425SAlexander Motin *event = state->nextstat; 264a157e425SAlexander Motin if (profiling && 265a157e425SAlexander Motin bintime_cmp(event, &state->nextprof, >)) 266a157e425SAlexander Motin *event = state->nextprof; 26743fe7d45SAlexander Motin } 26843fe7d45SAlexander Motin } 269a157e425SAlexander Motin 270a157e425SAlexander Motin /* 271a157e425SAlexander Motin * Schedule binuptime of the next event on all CPUs. 272a157e425SAlexander Motin */ 273a157e425SAlexander Motin static void 274a157e425SAlexander Motin getnextevent(struct bintime *event) 275a157e425SAlexander Motin { 276a157e425SAlexander Motin struct pcpu_state *state; 277a157e425SAlexander Motin #ifdef SMP 278a157e425SAlexander Motin int cpu; 279a157e425SAlexander Motin #endif 280a157e425SAlexander Motin int c; 281a157e425SAlexander Motin 282a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 283a157e425SAlexander Motin *event = state->nextevent; 284a157e425SAlexander Motin c = curcpu; 285a157e425SAlexander Motin #ifdef SMP 286a157e425SAlexander Motin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) { 287a157e425SAlexander Motin CPU_FOREACH(cpu) { 288a157e425SAlexander Motin if (curcpu == cpu) 289a157e425SAlexander Motin continue; 290a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 291a157e425SAlexander Motin if (bintime_cmp(event, &state->nextevent, >)) { 292a157e425SAlexander Motin *event = state->nextevent; 293a157e425SAlexander Motin c = cpu; 29443fe7d45SAlexander Motin } 29543fe7d45SAlexander Motin } 296a157e425SAlexander Motin } 297a157e425SAlexander Motin #endif 298a157e425SAlexander Motin CTR5(KTR_SPARE2, "next at %d: next %d.%08x%08x by %d", 299a157e425SAlexander Motin curcpu, event->sec, (unsigned int)(event->frac >> 32), 300a157e425SAlexander Motin (unsigned int)(event->frac & 0xffffffff), c); 301a157e425SAlexander Motin } 302a157e425SAlexander Motin 303a157e425SAlexander Motin /* Hardware timer callback function. */ 304a157e425SAlexander Motin static void 305a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg) 306a157e425SAlexander Motin { 307a157e425SAlexander Motin struct bintime now; 308a157e425SAlexander Motin struct bintime *next; 309a157e425SAlexander Motin struct pcpu_state *state; 310a157e425SAlexander Motin #ifdef SMP 311a157e425SAlexander Motin int cpu, bcast; 312a157e425SAlexander Motin #endif 313a157e425SAlexander Motin 314a157e425SAlexander Motin /* Do not touch anything if somebody reconfiguring timers. */ 315a157e425SAlexander Motin if (busy) 316a157e425SAlexander Motin return; 317a157e425SAlexander Motin /* Update present and next tick times. */ 318a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 319a157e425SAlexander Motin if (et->et_flags & ET_FLAGS_PERCPU) { 320a157e425SAlexander Motin next = &state->nexttick; 321a157e425SAlexander Motin } else 322a157e425SAlexander Motin next = &nexttick; 323a157e425SAlexander Motin if (periodic) { 324a157e425SAlexander Motin now = *next; /* Ex-next tick time becomes present time. */ 325a157e425SAlexander Motin bintime_add(next, &timerperiod); /* Next tick in 1 period. */ 326a157e425SAlexander Motin } else { 327a157e425SAlexander Motin binuptime(&now); /* Get present time from hardware. */ 328a157e425SAlexander Motin next->sec = -1; /* Next tick is not scheduled yet. */ 329a157e425SAlexander Motin } 330a157e425SAlexander Motin state->now = now; 331a157e425SAlexander Motin CTR4(KTR_SPARE2, "intr at %d: now %d.%08x%08x", 332a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 333a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 334a157e425SAlexander Motin 335a157e425SAlexander Motin #ifdef SMP 336a157e425SAlexander Motin /* Prepare broadcasting to other CPUs for non-per-CPU timers. */ 337a157e425SAlexander Motin bcast = 0; 338a157e425SAlexander Motin if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) { 339a157e425SAlexander Motin CPU_FOREACH(cpu) { 340a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 341a157e425SAlexander Motin ET_HW_LOCK(state); 342a157e425SAlexander Motin state->now = now; 343a157e425SAlexander Motin if (bintime_cmp(&now, &state->nextevent, >=)) { 344a157e425SAlexander Motin state->nextevent.sec++; 3458e860de4SAlexander Motin if (curcpu != cpu) { 346a157e425SAlexander Motin state->ipi = 1; 347a157e425SAlexander Motin bcast = 1; 348a157e425SAlexander Motin } 3498e860de4SAlexander Motin } 350a157e425SAlexander Motin ET_HW_UNLOCK(state); 351a157e425SAlexander Motin } 352a157e425SAlexander Motin } 353a157e425SAlexander Motin #endif 354a157e425SAlexander Motin 355a157e425SAlexander Motin /* Handle events for this time on this CPU. */ 356a157e425SAlexander Motin handleevents(&now, 0); 357a157e425SAlexander Motin 358a157e425SAlexander Motin #ifdef SMP 359a157e425SAlexander Motin /* Broadcast interrupt to other CPUs for non-per-CPU timers. */ 360a157e425SAlexander Motin if (bcast) { 361a157e425SAlexander Motin CPU_FOREACH(cpu) { 362a157e425SAlexander Motin if (curcpu == cpu) 363a157e425SAlexander Motin continue; 364a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 365a157e425SAlexander Motin if (state->ipi) { 366a157e425SAlexander Motin state->ipi = 0; 367a157e425SAlexander Motin ipi_cpu(cpu, IPI_HARDCLOCK); 368a157e425SAlexander Motin } 369a157e425SAlexander Motin } 370a157e425SAlexander Motin } 371a157e425SAlexander Motin #endif 372a157e425SAlexander Motin } 373a157e425SAlexander Motin 374a157e425SAlexander Motin /* 375a157e425SAlexander Motin * Load new value into hardware timer. 376a157e425SAlexander Motin */ 377a157e425SAlexander Motin static void 378a157e425SAlexander Motin loadtimer(struct bintime *now, int start) 379a157e425SAlexander Motin { 380a157e425SAlexander Motin struct pcpu_state *state; 381a157e425SAlexander Motin struct bintime new; 382a157e425SAlexander Motin struct bintime *next; 383a157e425SAlexander Motin uint64_t tmp; 384a157e425SAlexander Motin int eq; 385a157e425SAlexander Motin 386a157e425SAlexander Motin if (periodic) { 387a157e425SAlexander Motin if (start) { 388a157e425SAlexander Motin /* 389a157e425SAlexander Motin * Try to start all periodic timers aligned 390a157e425SAlexander Motin * to period to make events synchronous. 391a157e425SAlexander Motin */ 392a157e425SAlexander Motin tmp = ((uint64_t)now->sec << 36) + (now->frac >> 28); 393a157e425SAlexander Motin tmp = (tmp % (timerperiod.frac >> 28)) << 28; 394a157e425SAlexander Motin tmp = timerperiod.frac - tmp; 395a157e425SAlexander Motin new = timerperiod; 396a157e425SAlexander Motin bintime_addx(&new, tmp); 397a157e425SAlexander Motin CTR5(KTR_SPARE2, "load p at %d: now %d.%08x first in %d.%08x", 398a157e425SAlexander Motin curcpu, now->sec, (unsigned int)(now->frac >> 32), 399a157e425SAlexander Motin new.sec, (unsigned int)(new.frac >> 32)); 400a157e425SAlexander Motin et_start(timer, &new, &timerperiod); 401a157e425SAlexander Motin } 402a157e425SAlexander Motin } else { 403a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) { 404a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 405a157e425SAlexander Motin next = &state->nexttick; 406a157e425SAlexander Motin } else 407a157e425SAlexander Motin next = &nexttick; 408a157e425SAlexander Motin getnextevent(&new); 409a157e425SAlexander Motin eq = bintime_cmp(&new, next, ==); 410a157e425SAlexander Motin CTR5(KTR_SPARE2, "load at %d: next %d.%08x%08x eq %d", 411a157e425SAlexander Motin curcpu, new.sec, (unsigned int)(new.frac >> 32), 412a157e425SAlexander Motin (unsigned int)(new.frac & 0xffffffff), 413a157e425SAlexander Motin eq); 414a157e425SAlexander Motin if (!eq) { 415a157e425SAlexander Motin *next = new; 416a157e425SAlexander Motin bintime_sub(&new, now); 417a157e425SAlexander Motin et_start(timer, &new, NULL); 418a157e425SAlexander Motin } 419a157e425SAlexander Motin } 420a157e425SAlexander Motin } 421a157e425SAlexander Motin 422a157e425SAlexander Motin /* 423a157e425SAlexander Motin * Prepare event timer parameters after configuration changes. 424a157e425SAlexander Motin */ 425a157e425SAlexander Motin static void 426a157e425SAlexander Motin setuptimer(void) 427a157e425SAlexander Motin { 428a157e425SAlexander Motin int freq; 429a157e425SAlexander Motin 430a157e425SAlexander Motin if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0) 431a157e425SAlexander Motin periodic = 0; 432a157e425SAlexander Motin else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0) 433a157e425SAlexander Motin periodic = 1; 434dd9595e7SAlexander Motin singlemul = MIN(MAX(singlemul, 1), 20); 435a157e425SAlexander Motin freq = hz * singlemul; 436a157e425SAlexander Motin while (freq < (profiling ? profhz : stathz)) 437a157e425SAlexander Motin freq += hz; 438a157e425SAlexander Motin freq = round_freq(timer, freq); 439a157e425SAlexander Motin FREQ2BT(freq, &timerperiod); 440a157e425SAlexander Motin } 44143fe7d45SAlexander Motin 44243fe7d45SAlexander Motin /* 44343fe7d45SAlexander Motin * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler. 44443fe7d45SAlexander Motin */ 445a157e425SAlexander Motin static int 446a157e425SAlexander Motin doconfigtimer(void) 44743fe7d45SAlexander Motin { 448a157e425SAlexander Motin struct bintime now; 449a157e425SAlexander Motin struct pcpu_state *state; 45043fe7d45SAlexander Motin 451a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 452a157e425SAlexander Motin switch (atomic_load_acq_int(&state->action)) { 453a157e425SAlexander Motin case 1: 454a157e425SAlexander Motin binuptime(&now); 455a157e425SAlexander Motin ET_HW_LOCK(state); 456a157e425SAlexander Motin loadtimer(&now, 1); 457a157e425SAlexander Motin ET_HW_UNLOCK(state); 458a157e425SAlexander Motin state->handle = 0; 459a157e425SAlexander Motin atomic_store_rel_int(&state->action, 0); 460a157e425SAlexander Motin return (1); 461a157e425SAlexander Motin case 2: 462a157e425SAlexander Motin ET_HW_LOCK(state); 463a157e425SAlexander Motin et_stop(timer); 464a157e425SAlexander Motin ET_HW_UNLOCK(state); 465a157e425SAlexander Motin state->handle = 0; 466a157e425SAlexander Motin atomic_store_rel_int(&state->action, 0); 467a157e425SAlexander Motin return (1); 468a157e425SAlexander Motin } 469a157e425SAlexander Motin if (atomic_readandclear_int(&state->handle) && !busy) { 470a157e425SAlexander Motin binuptime(&now); 471a157e425SAlexander Motin handleevents(&now, 0); 47243fe7d45SAlexander Motin return (1); 47343fe7d45SAlexander Motin } 47443fe7d45SAlexander Motin return (0); 47543fe7d45SAlexander Motin } 47643fe7d45SAlexander Motin 47743fe7d45SAlexander Motin /* 47843fe7d45SAlexander Motin * Reconfigure specified timer. 47943fe7d45SAlexander Motin * For per-CPU timers use IPI to make other CPUs to reconfigure. 48043fe7d45SAlexander Motin */ 48143fe7d45SAlexander Motin static void 482a157e425SAlexander Motin configtimer(int start) 48343fe7d45SAlexander Motin { 484a157e425SAlexander Motin struct bintime now, next; 485a157e425SAlexander Motin struct pcpu_state *state; 48643fe7d45SAlexander Motin int cpu; 48743fe7d45SAlexander Motin 488a157e425SAlexander Motin if (start) { 489a157e425SAlexander Motin setuptimer(); 490a157e425SAlexander Motin binuptime(&now); 491a157e425SAlexander Motin } 49243fe7d45SAlexander Motin critical_enter(); 493a157e425SAlexander Motin ET_HW_LOCK(DPCPU_PTR(timerstate)); 494a157e425SAlexander Motin if (start) { 495a157e425SAlexander Motin /* Initialize time machine parameters. */ 496a157e425SAlexander Motin next = now; 497a157e425SAlexander Motin bintime_add(&next, &timerperiod); 498a157e425SAlexander Motin if (periodic) 499a157e425SAlexander Motin nexttick = next; 50043fe7d45SAlexander Motin else 501a157e425SAlexander Motin nexttick.sec = -1; 502a157e425SAlexander Motin CPU_FOREACH(cpu) { 503a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 504a157e425SAlexander Motin state->now = now; 505a157e425SAlexander Motin state->nextevent = next; 506a157e425SAlexander Motin if (periodic) 507a157e425SAlexander Motin state->nexttick = next; 508a157e425SAlexander Motin else 509a157e425SAlexander Motin state->nexttick.sec = -1; 510a157e425SAlexander Motin state->nexthard = next; 511a157e425SAlexander Motin state->nextstat = next; 512a157e425SAlexander Motin state->nextprof = next; 513a157e425SAlexander Motin hardclock_sync(cpu); 514a157e425SAlexander Motin } 515a157e425SAlexander Motin busy = 0; 516a157e425SAlexander Motin /* Start global timer or per-CPU timer of this CPU. */ 517a157e425SAlexander Motin loadtimer(&now, 1); 518a157e425SAlexander Motin } else { 519a157e425SAlexander Motin busy = 1; 520a157e425SAlexander Motin /* Stop global timer or per-CPU timer of this CPU. */ 521a157e425SAlexander Motin et_stop(timer); 522a157e425SAlexander Motin } 523a157e425SAlexander Motin ET_HW_UNLOCK(DPCPU_PTR(timerstate)); 52443fe7d45SAlexander Motin #ifdef SMP 525a157e425SAlexander Motin /* If timer is global or there is no other CPUs yet - we are done. */ 526a157e425SAlexander Motin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) { 52743fe7d45SAlexander Motin critical_exit(); 52843fe7d45SAlexander Motin return; 52943fe7d45SAlexander Motin } 53043fe7d45SAlexander Motin /* Set reconfigure flags for other CPUs. */ 53143fe7d45SAlexander Motin CPU_FOREACH(cpu) { 532a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 533a157e425SAlexander Motin atomic_store_rel_int(&state->action, 534a157e425SAlexander Motin (cpu == curcpu) ? 0 : ( start ? 1 : 2)); 53543fe7d45SAlexander Motin } 536a157e425SAlexander Motin /* Broadcast reconfigure IPI. */ 537a157e425SAlexander Motin ipi_all_but_self(IPI_HARDCLOCK); 53843fe7d45SAlexander Motin /* Wait for reconfiguration completed. */ 53943fe7d45SAlexander Motin restart: 54043fe7d45SAlexander Motin cpu_spinwait(); 54143fe7d45SAlexander Motin CPU_FOREACH(cpu) { 54243fe7d45SAlexander Motin if (cpu == curcpu) 54343fe7d45SAlexander Motin continue; 544a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 545a157e425SAlexander Motin if (atomic_load_acq_int(&state->action)) 54643fe7d45SAlexander Motin goto restart; 54743fe7d45SAlexander Motin } 54843fe7d45SAlexander Motin #endif 549a157e425SAlexander Motin critical_exit(); 55043fe7d45SAlexander Motin } 55143fe7d45SAlexander Motin 552a157e425SAlexander Motin /* 553a157e425SAlexander Motin * Calculate nearest frequency supported by hardware timer. 554a157e425SAlexander Motin */ 55551636352SAlexander Motin static int 55651636352SAlexander Motin round_freq(struct eventtimer *et, int freq) 55751636352SAlexander Motin { 55851636352SAlexander Motin uint64_t div; 55951636352SAlexander Motin 56051636352SAlexander Motin if (et->et_frequency != 0) { 561599cf0f1SAlexander Motin div = lmax((et->et_frequency + freq / 2) / freq, 1); 56251636352SAlexander Motin if (et->et_flags & ET_FLAGS_POW2DIV) 56351636352SAlexander Motin div = 1 << (flsl(div + div / 2) - 1); 56451636352SAlexander Motin freq = (et->et_frequency + div / 2) / div; 56551636352SAlexander Motin } 56651636352SAlexander Motin if (et->et_min_period.sec > 0) 56751636352SAlexander Motin freq = 0; 568599cf0f1SAlexander Motin else if (et->et_min_period.frac != 0) 56951636352SAlexander Motin freq = min(freq, BT2FREQ(&et->et_min_period)); 57051636352SAlexander Motin if (et->et_max_period.sec == 0 && et->et_max_period.frac != 0) 57151636352SAlexander Motin freq = max(freq, BT2FREQ(&et->et_max_period)); 57251636352SAlexander Motin return (freq); 57351636352SAlexander Motin } 57451636352SAlexander Motin 57543fe7d45SAlexander Motin /* 576a157e425SAlexander Motin * Configure and start event timers (BSP part). 57743fe7d45SAlexander Motin */ 57843fe7d45SAlexander Motin void 57943fe7d45SAlexander Motin cpu_initclocks_bsp(void) 58043fe7d45SAlexander Motin { 581a157e425SAlexander Motin struct pcpu_state *state; 582a157e425SAlexander Motin int base, div, cpu; 58343fe7d45SAlexander Motin 584a157e425SAlexander Motin mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN); 585a157e425SAlexander Motin CPU_FOREACH(cpu) { 586a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 587a157e425SAlexander Motin mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN); 588a157e425SAlexander Motin } 589a157e425SAlexander Motin #ifdef SMP 590a157e425SAlexander Motin callout_new_inserted = cpu_new_callout; 591a157e425SAlexander Motin #endif 592afe41f2dSAlexander Motin periodic = want_periodic; 593a157e425SAlexander Motin /* Grab requested timer or the best of present. */ 594a157e425SAlexander Motin if (timername[0]) 595a157e425SAlexander Motin timer = et_find(timername, 0, 0); 596a157e425SAlexander Motin if (timer == NULL && periodic) { 597a157e425SAlexander Motin timer = et_find(NULL, 59843fe7d45SAlexander Motin ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); 599a157e425SAlexander Motin } 600a157e425SAlexander Motin if (timer == NULL) { 601a157e425SAlexander Motin timer = et_find(NULL, 602a157e425SAlexander Motin ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT); 603a157e425SAlexander Motin } 604a157e425SAlexander Motin if (timer == NULL && !periodic) { 605a157e425SAlexander Motin timer = et_find(NULL, 606a157e425SAlexander Motin ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); 607a157e425SAlexander Motin } 608a157e425SAlexander Motin if (timer == NULL) 609a157e425SAlexander Motin panic("No usable event timer found!"); 610a157e425SAlexander Motin et_init(timer, timercb, NULL, NULL); 611a157e425SAlexander Motin 612a157e425SAlexander Motin /* Adapt to timer capabilities. */ 613a157e425SAlexander Motin if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0) 614a157e425SAlexander Motin periodic = 0; 615a157e425SAlexander Motin else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0) 616a157e425SAlexander Motin periodic = 1; 617a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_C3STOP) 618a157e425SAlexander Motin cpu_disable_deep_sleep++; 619a157e425SAlexander Motin 62043fe7d45SAlexander Motin /* 62143fe7d45SAlexander Motin * We honor the requested 'hz' value. 62243fe7d45SAlexander Motin * We want to run stathz in the neighborhood of 128hz. 62343fe7d45SAlexander Motin * We would like profhz to run as often as possible. 62443fe7d45SAlexander Motin */ 625dd9595e7SAlexander Motin if (singlemul <= 0 || singlemul > 20) { 62643fe7d45SAlexander Motin if (hz >= 1500 || (hz % 128) == 0) 62743fe7d45SAlexander Motin singlemul = 1; 62843fe7d45SAlexander Motin else if (hz >= 750) 62943fe7d45SAlexander Motin singlemul = 2; 63043fe7d45SAlexander Motin else 63143fe7d45SAlexander Motin singlemul = 4; 63243fe7d45SAlexander Motin } 633a157e425SAlexander Motin if (periodic) { 634a157e425SAlexander Motin base = round_freq(timer, hz * singlemul); 63551636352SAlexander Motin singlemul = max((base + hz / 2) / hz, 1); 63651636352SAlexander Motin hz = (base + singlemul / 2) / singlemul; 63751636352SAlexander Motin if (base <= 128) 63843fe7d45SAlexander Motin stathz = base; 63943fe7d45SAlexander Motin else { 64043fe7d45SAlexander Motin div = base / 128; 64151636352SAlexander Motin if (div >= singlemul && (div % singlemul) == 0) 64243fe7d45SAlexander Motin div++; 64343fe7d45SAlexander Motin stathz = base / div; 64443fe7d45SAlexander Motin } 64543fe7d45SAlexander Motin profhz = stathz; 64651636352SAlexander Motin while ((profhz + stathz) <= 128 * 64) 64743fe7d45SAlexander Motin profhz += stathz; 648a157e425SAlexander Motin profhz = round_freq(timer, profhz); 64943fe7d45SAlexander Motin } else { 650a157e425SAlexander Motin hz = round_freq(timer, hz); 651a157e425SAlexander Motin stathz = round_freq(timer, 127); 652a157e425SAlexander Motin profhz = round_freq(timer, stathz * 64); 65343fe7d45SAlexander Motin } 654599cf0f1SAlexander Motin tick = 1000000 / hz; 655a157e425SAlexander Motin FREQ2BT(hz, &hardperiod); 656a157e425SAlexander Motin FREQ2BT(stathz, &statperiod); 657a157e425SAlexander Motin FREQ2BT(profhz, &profperiod); 65843fe7d45SAlexander Motin ET_LOCK(); 659a157e425SAlexander Motin configtimer(1); 66043fe7d45SAlexander Motin ET_UNLOCK(); 66143fe7d45SAlexander Motin } 66243fe7d45SAlexander Motin 663a157e425SAlexander Motin /* 664a157e425SAlexander Motin * Start per-CPU event timers on APs. 665a157e425SAlexander Motin */ 66643fe7d45SAlexander Motin void 66743fe7d45SAlexander Motin cpu_initclocks_ap(void) 66843fe7d45SAlexander Motin { 669a157e425SAlexander Motin struct bintime now; 670a157e425SAlexander Motin struct pcpu_state *state; 67143fe7d45SAlexander Motin 672a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) { 673a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 674a157e425SAlexander Motin binuptime(&now); 675a157e425SAlexander Motin ET_HW_LOCK(state); 676a157e425SAlexander Motin loadtimer(&now, 1); 677a157e425SAlexander Motin ET_HW_UNLOCK(state); 67843fe7d45SAlexander Motin } 67943fe7d45SAlexander Motin } 68043fe7d45SAlexander Motin 681a157e425SAlexander Motin /* 682a157e425SAlexander Motin * Switch to profiling clock rates. 683a157e425SAlexander Motin */ 68443fe7d45SAlexander Motin void 68543fe7d45SAlexander Motin cpu_startprofclock(void) 68643fe7d45SAlexander Motin { 68743fe7d45SAlexander Motin 68843fe7d45SAlexander Motin ET_LOCK(); 689a157e425SAlexander Motin if (periodic) { 690a157e425SAlexander Motin configtimer(0); 691a157e425SAlexander Motin profiling = 1; 692a157e425SAlexander Motin configtimer(1); 693a157e425SAlexander Motin } else 694a157e425SAlexander Motin profiling = 1; 69543fe7d45SAlexander Motin ET_UNLOCK(); 69643fe7d45SAlexander Motin } 69743fe7d45SAlexander Motin 698a157e425SAlexander Motin /* 699a157e425SAlexander Motin * Switch to regular clock rates. 700a157e425SAlexander Motin */ 70143fe7d45SAlexander Motin void 70243fe7d45SAlexander Motin cpu_stopprofclock(void) 70343fe7d45SAlexander Motin { 70443fe7d45SAlexander Motin 70543fe7d45SAlexander Motin ET_LOCK(); 706a157e425SAlexander Motin if (periodic) { 707a157e425SAlexander Motin configtimer(0); 708a157e425SAlexander Motin profiling = 0; 709a157e425SAlexander Motin configtimer(1); 710a157e425SAlexander Motin } else 711a157e425SAlexander Motin profiling = 0; 71243fe7d45SAlexander Motin ET_UNLOCK(); 71343fe7d45SAlexander Motin } 71443fe7d45SAlexander Motin 715a157e425SAlexander Motin /* 716a157e425SAlexander Motin * Switch to idle mode (all ticks handled). 717a157e425SAlexander Motin */ 718a157e425SAlexander Motin void 719a157e425SAlexander Motin cpu_idleclock(void) 720a157e425SAlexander Motin { 721a157e425SAlexander Motin struct bintime now, t; 722a157e425SAlexander Motin struct pcpu_state *state; 723a157e425SAlexander Motin 724a157e425SAlexander Motin if (idletick || busy || 725*9dfc483cSAlexander Motin (periodic && (timer->et_flags & ET_FLAGS_PERCPU)) 726*9dfc483cSAlexander Motin #ifdef DEVICE_POLLING 727*9dfc483cSAlexander Motin || curcpu == CPU_FIRST() 728*9dfc483cSAlexander Motin #endif 729*9dfc483cSAlexander Motin ) 730a157e425SAlexander Motin return; 731a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 732a157e425SAlexander Motin if (periodic) 733a157e425SAlexander Motin now = state->now; 734a157e425SAlexander Motin else 735a157e425SAlexander Motin binuptime(&now); 736a157e425SAlexander Motin CTR4(KTR_SPARE2, "idle at %d: now %d.%08x%08x", 737a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 738a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 739a157e425SAlexander Motin getnextcpuevent(&t, 1); 740a157e425SAlexander Motin ET_HW_LOCK(state); 741a157e425SAlexander Motin state->idle = 1; 742a157e425SAlexander Motin state->nextevent = t; 743a157e425SAlexander Motin if (!periodic) 744a157e425SAlexander Motin loadtimer(&now, 0); 745a157e425SAlexander Motin ET_HW_UNLOCK(state); 746a157e425SAlexander Motin } 747a157e425SAlexander Motin 748a157e425SAlexander Motin /* 749a157e425SAlexander Motin * Switch to active mode (skip empty ticks). 750a157e425SAlexander Motin */ 751a157e425SAlexander Motin void 752a157e425SAlexander Motin cpu_activeclock(void) 753a157e425SAlexander Motin { 754a157e425SAlexander Motin struct bintime now; 755a157e425SAlexander Motin struct pcpu_state *state; 756a157e425SAlexander Motin struct thread *td; 757a157e425SAlexander Motin 758a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 759a157e425SAlexander Motin if (state->idle == 0 || busy) 760a157e425SAlexander Motin return; 761a157e425SAlexander Motin if (periodic) 762a157e425SAlexander Motin now = state->now; 763a157e425SAlexander Motin else 764a157e425SAlexander Motin binuptime(&now); 765a157e425SAlexander Motin CTR4(KTR_SPARE2, "active at %d: now %d.%08x%08x", 766a157e425SAlexander Motin curcpu, now.sec, (unsigned int)(now.frac >> 32), 767a157e425SAlexander Motin (unsigned int)(now.frac & 0xffffffff)); 768a157e425SAlexander Motin spinlock_enter(); 769a157e425SAlexander Motin td = curthread; 770a157e425SAlexander Motin td->td_intr_nesting_level++; 771a157e425SAlexander Motin handleevents(&now, 1); 772a157e425SAlexander Motin td->td_intr_nesting_level--; 773a157e425SAlexander Motin spinlock_exit(); 774a157e425SAlexander Motin } 775a157e425SAlexander Motin 776a157e425SAlexander Motin #ifdef SMP 777a157e425SAlexander Motin static void 778a157e425SAlexander Motin cpu_new_callout(int cpu, int ticks) 779a157e425SAlexander Motin { 780a157e425SAlexander Motin struct bintime tmp; 781a157e425SAlexander Motin struct pcpu_state *state; 782a157e425SAlexander Motin 783a157e425SAlexander Motin CTR3(KTR_SPARE2, "new co at %d: on %d in %d", 784a157e425SAlexander Motin curcpu, cpu, ticks); 785a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 786a157e425SAlexander Motin ET_HW_LOCK(state); 787a157e425SAlexander Motin if (state->idle == 0 || busy) { 788a157e425SAlexander Motin ET_HW_UNLOCK(state); 789a157e425SAlexander Motin return; 790a157e425SAlexander Motin } 791a157e425SAlexander Motin /* 792a157e425SAlexander Motin * If timer is periodic - just update next event time for target CPU. 793bcb74c4cSAlexander Motin * If timer is global - there is chance it is already programmed. 794a157e425SAlexander Motin */ 795bcb74c4cSAlexander Motin if (periodic || (timer->et_flags & ET_FLAGS_PERCPU) == 0) { 796a157e425SAlexander Motin state->nextevent = state->nexthard; 797a157e425SAlexander Motin tmp = hardperiod; 798a157e425SAlexander Motin bintime_mul(&tmp, ticks - 1); 799a157e425SAlexander Motin bintime_add(&state->nextevent, &tmp); 800bcb74c4cSAlexander Motin if (periodic || 801bcb74c4cSAlexander Motin bintime_cmp(&state->nextevent, &nexttick, >=)) { 802a157e425SAlexander Motin ET_HW_UNLOCK(state); 803a157e425SAlexander Motin return; 804a157e425SAlexander Motin } 805bcb74c4cSAlexander Motin } 806a157e425SAlexander Motin /* 807a157e425SAlexander Motin * Otherwise we have to wake that CPU up, as we can't get present 808a157e425SAlexander Motin * bintime to reprogram global timer from here. If timer is per-CPU, 809a157e425SAlexander Motin * we by definition can't do it from here. 810a157e425SAlexander Motin */ 811a157e425SAlexander Motin ET_HW_UNLOCK(state); 812a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) { 813a157e425SAlexander Motin state->handle = 1; 814a157e425SAlexander Motin ipi_cpu(cpu, IPI_HARDCLOCK); 815a157e425SAlexander Motin } else { 816a157e425SAlexander Motin if (!cpu_idle_wakeup(cpu)) 817a157e425SAlexander Motin ipi_cpu(cpu, IPI_AST); 818a157e425SAlexander Motin } 819a157e425SAlexander Motin } 820a157e425SAlexander Motin #endif 821a157e425SAlexander Motin 822a157e425SAlexander Motin /* 823a157e425SAlexander Motin * Report or change the active event timers hardware. 824a157e425SAlexander Motin */ 82543fe7d45SAlexander Motin static int 826a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS) 82743fe7d45SAlexander Motin { 82843fe7d45SAlexander Motin char buf[32]; 82943fe7d45SAlexander Motin struct eventtimer *et; 83043fe7d45SAlexander Motin int error; 83143fe7d45SAlexander Motin 83243fe7d45SAlexander Motin ET_LOCK(); 833a157e425SAlexander Motin et = timer; 83443fe7d45SAlexander Motin snprintf(buf, sizeof(buf), "%s", et->et_name); 83543fe7d45SAlexander Motin ET_UNLOCK(); 83643fe7d45SAlexander Motin error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 83743fe7d45SAlexander Motin ET_LOCK(); 838a157e425SAlexander Motin et = timer; 83943fe7d45SAlexander Motin if (error != 0 || req->newptr == NULL || 840a157e425SAlexander Motin strcasecmp(buf, et->et_name) == 0) { 84143fe7d45SAlexander Motin ET_UNLOCK(); 84243fe7d45SAlexander Motin return (error); 84343fe7d45SAlexander Motin } 844a157e425SAlexander Motin et = et_find(buf, 0, 0); 84543fe7d45SAlexander Motin if (et == NULL) { 84643fe7d45SAlexander Motin ET_UNLOCK(); 84743fe7d45SAlexander Motin return (ENOENT); 84843fe7d45SAlexander Motin } 84943fe7d45SAlexander Motin configtimer(0); 850a157e425SAlexander Motin et_free(timer); 851a157e425SAlexander Motin if (et->et_flags & ET_FLAGS_C3STOP) 852a157e425SAlexander Motin cpu_disable_deep_sleep++; 853a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_C3STOP) 854a157e425SAlexander Motin cpu_disable_deep_sleep--; 855afe41f2dSAlexander Motin periodic = want_periodic; 856a157e425SAlexander Motin timer = et; 857a157e425SAlexander Motin et_init(timer, timercb, NULL, NULL); 85843fe7d45SAlexander Motin configtimer(1); 85943fe7d45SAlexander Motin ET_UNLOCK(); 86043fe7d45SAlexander Motin return (error); 86143fe7d45SAlexander Motin } 862a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer, 86343fe7d45SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 864dd9595e7SAlexander Motin 0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer"); 865a157e425SAlexander Motin 866a157e425SAlexander Motin /* 867a157e425SAlexander Motin * Report or change the active event timer periodicity. 868a157e425SAlexander Motin */ 869a157e425SAlexander Motin static int 870a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS) 871a157e425SAlexander Motin { 872a157e425SAlexander Motin int error, val; 873a157e425SAlexander Motin 874a157e425SAlexander Motin val = periodic; 875a157e425SAlexander Motin error = sysctl_handle_int(oidp, &val, 0, req); 876a157e425SAlexander Motin if (error != 0 || req->newptr == NULL) 877a157e425SAlexander Motin return (error); 878a157e425SAlexander Motin ET_LOCK(); 879a157e425SAlexander Motin configtimer(0); 880afe41f2dSAlexander Motin periodic = want_periodic = val; 881a157e425SAlexander Motin configtimer(1); 882a157e425SAlexander Motin ET_UNLOCK(); 883a157e425SAlexander Motin return (error); 884a157e425SAlexander Motin } 885a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic, 886a157e425SAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 887dd9595e7SAlexander Motin 0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode"); 88843fe7d45SAlexander Motin 88943fe7d45SAlexander Motin #endif 890