143fe7d45SAlexander Motin /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 45b999a6bSDavide Italiano * Copyright (c) 2010-2013 Alexander Motin <mav@FreeBSD.org> 543fe7d45SAlexander Motin * All rights reserved. 643fe7d45SAlexander Motin * 743fe7d45SAlexander Motin * Redistribution and use in source and binary forms, with or without 843fe7d45SAlexander Motin * modification, are permitted provided that the following conditions 943fe7d45SAlexander Motin * are met: 1043fe7d45SAlexander Motin * 1. Redistributions of source code must retain the above copyright 1143fe7d45SAlexander Motin * notice, this list of conditions and the following disclaimer, 1243fe7d45SAlexander Motin * without modification, immediately at the beginning of the file. 1343fe7d45SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 1443fe7d45SAlexander Motin * notice, this list of conditions and the following disclaimer in the 1543fe7d45SAlexander Motin * documentation and/or other materials provided with the distribution. 1643fe7d45SAlexander Motin * 1743fe7d45SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1843fe7d45SAlexander Motin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1943fe7d45SAlexander Motin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2043fe7d45SAlexander Motin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2143fe7d45SAlexander Motin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2243fe7d45SAlexander Motin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2343fe7d45SAlexander Motin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2443fe7d45SAlexander Motin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2543fe7d45SAlexander Motin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2643fe7d45SAlexander Motin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2743fe7d45SAlexander Motin */ 2843fe7d45SAlexander Motin 2943fe7d45SAlexander Motin #include <sys/cdefs.h> 3043fe7d45SAlexander Motin __FBSDID("$FreeBSD$"); 3143fe7d45SAlexander Motin 3243fe7d45SAlexander Motin /* 3343fe7d45SAlexander Motin * Common routines to manage event timers hardware. 3443fe7d45SAlexander Motin */ 3543fe7d45SAlexander Motin 369dfc483cSAlexander Motin #include "opt_device_polling.h" 3743fe7d45SAlexander Motin 3843fe7d45SAlexander Motin #include <sys/param.h> 3943fe7d45SAlexander Motin #include <sys/systm.h> 4043fe7d45SAlexander Motin #include <sys/bus.h> 415b999a6bSDavide Italiano #include <sys/limits.h> 4243fe7d45SAlexander Motin #include <sys/lock.h> 4343fe7d45SAlexander Motin #include <sys/kdb.h> 44a157e425SAlexander Motin #include <sys/ktr.h> 4543fe7d45SAlexander Motin #include <sys/mutex.h> 4643fe7d45SAlexander Motin #include <sys/proc.h> 4743fe7d45SAlexander Motin #include <sys/kernel.h> 4843fe7d45SAlexander Motin #include <sys/sched.h> 4943fe7d45SAlexander Motin #include <sys/smp.h> 5043fe7d45SAlexander Motin #include <sys/sysctl.h> 5143fe7d45SAlexander Motin #include <sys/timeet.h> 520e189873SAlexander Motin #include <sys/timetc.h> 5343fe7d45SAlexander Motin 5443fe7d45SAlexander Motin #include <machine/atomic.h> 5543fe7d45SAlexander Motin #include <machine/clock.h> 5643fe7d45SAlexander Motin #include <machine/cpu.h> 5743fe7d45SAlexander Motin #include <machine/smp.h> 5843fe7d45SAlexander Motin 5992597e06SJohn Baldwin int cpu_disable_c2_sleep = 0; /* Timer dies in C2. */ 6092597e06SJohn Baldwin int cpu_disable_c3_sleep = 0; /* Timer dies in C3. */ 6143fe7d45SAlexander Motin 62a157e425SAlexander Motin static void setuptimer(void); 635b999a6bSDavide Italiano static void loadtimer(sbintime_t now, int first); 64a157e425SAlexander Motin static int doconfigtimer(void); 65a157e425SAlexander Motin static void configtimer(int start); 66a157e425SAlexander Motin static int round_freq(struct eventtimer *et, int freq); 6743fe7d45SAlexander Motin 685b999a6bSDavide Italiano static sbintime_t getnextcpuevent(int idle); 695b999a6bSDavide Italiano static sbintime_t getnextevent(void); 705b999a6bSDavide Italiano static int handleevents(sbintime_t now, int fake); 7143fe7d45SAlexander Motin 72a157e425SAlexander Motin static struct mtx et_hw_mtx; 73a157e425SAlexander Motin 74a157e425SAlexander Motin #define ET_HW_LOCK(state) \ 75a157e425SAlexander Motin { \ 76a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) \ 77a157e425SAlexander Motin mtx_lock_spin(&(state)->et_hw_mtx); \ 78a157e425SAlexander Motin else \ 79a157e425SAlexander Motin mtx_lock_spin(&et_hw_mtx); \ 80a157e425SAlexander Motin } 81a157e425SAlexander Motin 82a157e425SAlexander Motin #define ET_HW_UNLOCK(state) \ 83a157e425SAlexander Motin { \ 84a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) \ 85a157e425SAlexander Motin mtx_unlock_spin(&(state)->et_hw_mtx); \ 86a157e425SAlexander Motin else \ 87a157e425SAlexander Motin mtx_unlock_spin(&et_hw_mtx); \ 88a157e425SAlexander Motin } 89a157e425SAlexander Motin 90a157e425SAlexander Motin static struct eventtimer *timer = NULL; 915b999a6bSDavide Italiano static sbintime_t timerperiod; /* Timer period for periodic mode. */ 925b999a6bSDavide Italiano static sbintime_t statperiod; /* statclock() events period. */ 935b999a6bSDavide Italiano static sbintime_t profperiod; /* profclock() events period. */ 945b999a6bSDavide Italiano static sbintime_t nexttick; /* Next global timer tick time. */ 955b999a6bSDavide Italiano static u_int busy = 1; /* Reconfiguration is in progress. */ 96af3b2549SHans Petter Selasky static int profiling; /* Profiling events enabled. */ 97a157e425SAlexander Motin 98a157e425SAlexander Motin static char timername[32]; /* Wanted timer. */ 99a157e425SAlexander Motin TUNABLE_STR("kern.eventtimer.timer", timername, sizeof(timername)); 100a157e425SAlexander Motin 101af3b2549SHans Petter Selasky static int singlemul; /* Multiplier for periodic mode. */ 102af3b2549SHans Petter Selasky SYSCTL_INT(_kern_eventtimer, OID_AUTO, singlemul, CTLFLAG_RWTUN, &singlemul, 103a157e425SAlexander Motin 0, "Multiplier for periodic mode"); 10443fe7d45SAlexander Motin 105af3b2549SHans Petter Selasky static u_int idletick; /* Run periodic events when idle. */ 106af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_eventtimer, OID_AUTO, idletick, CTLFLAG_RWTUN, &idletick, 107a157e425SAlexander Motin 0, "Run periodic events when idle"); 108a157e425SAlexander Motin 109af3b2549SHans Petter Selasky static int periodic; /* Periodic or one-shot mode. */ 110af3b2549SHans Petter Selasky static int want_periodic; /* What mode to prefer. */ 111afe41f2dSAlexander Motin TUNABLE_INT("kern.eventtimer.periodic", &want_periodic); 112a157e425SAlexander Motin 113a157e425SAlexander Motin struct pcpu_state { 114a157e425SAlexander Motin struct mtx et_hw_mtx; /* Per-CPU timer mutex. */ 115a157e425SAlexander Motin u_int action; /* Reconfiguration requests. */ 116a157e425SAlexander Motin u_int handle; /* Immediate handle resuests. */ 1175b999a6bSDavide Italiano sbintime_t now; /* Last tick time. */ 1185b999a6bSDavide Italiano sbintime_t nextevent; /* Next scheduled event on this CPU. */ 1195b999a6bSDavide Italiano sbintime_t nexttick; /* Next timer tick time. */ 120d3e2e28eSAlexander Motin sbintime_t nexthard; /* Next hardclock() event. */ 1215b999a6bSDavide Italiano sbintime_t nextstat; /* Next statclock() event. */ 1225b999a6bSDavide Italiano sbintime_t nextprof; /* Next profclock() event. */ 1235b999a6bSDavide Italiano sbintime_t nextcall; /* Next callout event. */ 1245b999a6bSDavide Italiano sbintime_t nextcallopt; /* Next optional callout event. */ 125a157e425SAlexander Motin int ipi; /* This CPU needs IPI. */ 126a157e425SAlexander Motin int idle; /* This CPU is in idle mode. */ 127a157e425SAlexander Motin }; 128a157e425SAlexander Motin 1292bf95012SAndrew Turner DPCPU_DEFINE_STATIC(struct pcpu_state, timerstate); 1305b999a6bSDavide Italiano DPCPU_DEFINE(sbintime_t, hardclocktime); 131fdc5dd2dSAlexander Motin 132a157e425SAlexander Motin /* 133a157e425SAlexander Motin * Timer broadcast IPI handler. 134a157e425SAlexander Motin */ 135a157e425SAlexander Motin int 136a157e425SAlexander Motin hardclockintr(void) 13743fe7d45SAlexander Motin { 1385b999a6bSDavide Italiano sbintime_t now; 139a157e425SAlexander Motin struct pcpu_state *state; 140a157e425SAlexander Motin int done; 14143fe7d45SAlexander Motin 142a157e425SAlexander Motin if (doconfigtimer() || busy) 143a157e425SAlexander Motin return (FILTER_HANDLED); 144a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 145a157e425SAlexander Motin now = state->now; 1465b999a6bSDavide Italiano CTR3(KTR_SPARE2, "ipi at %d: now %d.%08x", 1475b999a6bSDavide Italiano curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff)); 1485b999a6bSDavide Italiano done = handleevents(now, 0); 149a157e425SAlexander Motin return (done ? FILTER_HANDLED : FILTER_STRAY); 150a157e425SAlexander Motin } 151a157e425SAlexander Motin 152a157e425SAlexander Motin /* 153a157e425SAlexander Motin * Handle all events for specified time on this CPU 154a157e425SAlexander Motin */ 155a157e425SAlexander Motin static int 1565b999a6bSDavide Italiano handleevents(sbintime_t now, int fake) 157a157e425SAlexander Motin { 1585b999a6bSDavide Italiano sbintime_t t, *hct; 159a157e425SAlexander Motin struct trapframe *frame; 160a157e425SAlexander Motin struct pcpu_state *state; 161a157e425SAlexander Motin int usermode; 162a157e425SAlexander Motin int done, runs; 163a157e425SAlexander Motin 1645b999a6bSDavide Italiano CTR3(KTR_SPARE2, "handle at %d: now %d.%08x", 1655b999a6bSDavide Italiano curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff)); 166a157e425SAlexander Motin done = 0; 167a157e425SAlexander Motin if (fake) { 168a157e425SAlexander Motin frame = NULL; 169a157e425SAlexander Motin usermode = 0; 170a157e425SAlexander Motin } else { 171a157e425SAlexander Motin frame = curthread->td_intr_frame; 172a157e425SAlexander Motin usermode = TRAPF_USERMODE(frame); 173a157e425SAlexander Motin } 174dd7498aeSAndriy Gapon 175a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 176dd7498aeSAndriy Gapon 177bcfd016cSAlexander Motin runs = 0; 1785b999a6bSDavide Italiano while (now >= state->nexthard) { 1795b999a6bSDavide Italiano state->nexthard += tick_sbt; 180a157e425SAlexander Motin runs++; 18143fe7d45SAlexander Motin } 182c0722d20SAlexander Motin if (runs) { 1835b999a6bSDavide Italiano hct = DPCPU_PTR(hardclocktime); 1845b999a6bSDavide Italiano *hct = state->nexthard - tick_sbt; 185c0722d20SAlexander Motin if (fake < 2) { 186cc4f3d0aSMark Johnston hardclock(runs, usermode); 187a157e425SAlexander Motin done = 1; 18843fe7d45SAlexander Motin } 189c0722d20SAlexander Motin } 190bcfd016cSAlexander Motin runs = 0; 1915b999a6bSDavide Italiano while (now >= state->nextstat) { 1925b999a6bSDavide Italiano state->nextstat += statperiod; 193bcfd016cSAlexander Motin runs++; 194bcfd016cSAlexander Motin } 195bcfd016cSAlexander Motin if (runs && fake < 2) { 196cc4f3d0aSMark Johnston statclock(runs, usermode); 197a157e425SAlexander Motin done = 1; 19843fe7d45SAlexander Motin } 199a157e425SAlexander Motin if (profiling) { 200bcfd016cSAlexander Motin runs = 0; 2015b999a6bSDavide Italiano while (now >= state->nextprof) { 2025b999a6bSDavide Italiano state->nextprof += profperiod; 203bcfd016cSAlexander Motin runs++; 204bcfd016cSAlexander Motin } 205bcfd016cSAlexander Motin if (runs && !fake) { 206cc4f3d0aSMark Johnston profclock(runs, usermode, TRAPF_PC(frame)); 207a157e425SAlexander Motin done = 1; 20843fe7d45SAlexander Motin } 209a157e425SAlexander Motin } else 210a157e425SAlexander Motin state->nextprof = state->nextstat; 21110c87557SHans Petter Selasky if (now >= state->nextcallopt || now >= state->nextcall) { 2124bc38a5aSDavide Italiano state->nextcall = state->nextcallopt = SBT_MAX; 2135b999a6bSDavide Italiano callout_process(now); 2145b999a6bSDavide Italiano } 215dd7498aeSAndriy Gapon 2165b999a6bSDavide Italiano t = getnextcpuevent(0); 217a157e425SAlexander Motin ET_HW_LOCK(state); 218a157e425SAlexander Motin if (!busy) { 219a157e425SAlexander Motin state->idle = 0; 220a157e425SAlexander Motin state->nextevent = t; 221e37e08c7SAlexander Motin loadtimer(now, (fake == 2) && 222e37e08c7SAlexander Motin (timer->et_flags & ET_FLAGS_PERCPU)); 22343fe7d45SAlexander Motin } 224a157e425SAlexander Motin ET_HW_UNLOCK(state); 225a157e425SAlexander Motin return (done); 22643fe7d45SAlexander Motin } 22743fe7d45SAlexander Motin 22843fe7d45SAlexander Motin /* 229a157e425SAlexander Motin * Schedule binuptime of the next event on current CPU. 23043fe7d45SAlexander Motin */ 2315b999a6bSDavide Italiano static sbintime_t 2325b999a6bSDavide Italiano getnextcpuevent(int idle) 23343fe7d45SAlexander Motin { 2345b999a6bSDavide Italiano sbintime_t event; 235a157e425SAlexander Motin struct pcpu_state *state; 2365b999a6bSDavide Italiano u_int hardfreq; 23743fe7d45SAlexander Motin 238a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 2395b999a6bSDavide Italiano /* Handle hardclock() events, skipping some if CPU is idle. */ 2405b999a6bSDavide Italiano event = state->nexthard; 2415b999a6bSDavide Italiano if (idle) { 242*cb1f5d11SAlexander Motin if (tc_min_ticktock_freq > 1 2435b999a6bSDavide Italiano #ifdef SMP 2445b999a6bSDavide Italiano && curcpu == CPU_FIRST() 2455b999a6bSDavide Italiano #endif 2465b999a6bSDavide Italiano ) 2475b999a6bSDavide Italiano hardfreq = hz / tc_min_ticktock_freq; 248*cb1f5d11SAlexander Motin else 249*cb1f5d11SAlexander Motin hardfreq = hz; 2505b999a6bSDavide Italiano if (hardfreq > 1) 2515b999a6bSDavide Italiano event += tick_sbt * (hardfreq - 1); 252fd053faeSAlexander Motin } 2535b999a6bSDavide Italiano /* Handle callout events. */ 2545b999a6bSDavide Italiano if (event > state->nextcall) 2555b999a6bSDavide Italiano event = state->nextcall; 256fd053faeSAlexander Motin if (!idle) { /* If CPU is active - handle other types of events. */ 2575b999a6bSDavide Italiano if (event > state->nextstat) 2585b999a6bSDavide Italiano event = state->nextstat; 2595b999a6bSDavide Italiano if (profiling && event > state->nextprof) 2605b999a6bSDavide Italiano event = state->nextprof; 26143fe7d45SAlexander Motin } 2625b999a6bSDavide Italiano return (event); 26343fe7d45SAlexander Motin } 264a157e425SAlexander Motin 265a157e425SAlexander Motin /* 266a157e425SAlexander Motin * Schedule binuptime of the next event on all CPUs. 267a157e425SAlexander Motin */ 2685b999a6bSDavide Italiano static sbintime_t 2695b999a6bSDavide Italiano getnextevent(void) 270a157e425SAlexander Motin { 271a157e425SAlexander Motin struct pcpu_state *state; 2725b999a6bSDavide Italiano sbintime_t event; 273a157e425SAlexander Motin #ifdef SMP 274a157e425SAlexander Motin int cpu; 275a157e425SAlexander Motin #endif 2765cc2d25aSMatt Macy #ifdef KTR 2775b999a6bSDavide Italiano int c; 278a157e425SAlexander Motin 2795cc2d25aSMatt Macy c = -1; 2805cc2d25aSMatt Macy #endif 281a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 2825b999a6bSDavide Italiano event = state->nextevent; 283fd053faeSAlexander Motin #ifdef SMP 2845b999a6bSDavide Italiano if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) { 285a157e425SAlexander Motin CPU_FOREACH(cpu) { 286a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 2875b999a6bSDavide Italiano if (event > state->nextevent) { 2885b999a6bSDavide Italiano event = state->nextevent; 2895cc2d25aSMatt Macy #ifdef KTR 290a157e425SAlexander Motin c = cpu; 2915cc2d25aSMatt Macy #endif 29243fe7d45SAlexander Motin } 29343fe7d45SAlexander Motin } 2942d7d1642SGrzegorz Bernacki } 295a157e425SAlexander Motin #endif 2965b999a6bSDavide Italiano CTR4(KTR_SPARE2, "next at %d: next %d.%08x by %d", 2975b999a6bSDavide Italiano curcpu, (int)(event >> 32), (u_int)(event & 0xffffffff), c); 2985b999a6bSDavide Italiano return (event); 299a157e425SAlexander Motin } 300a157e425SAlexander Motin 301a157e425SAlexander Motin /* Hardware timer callback function. */ 302a157e425SAlexander Motin static void 303a157e425SAlexander Motin timercb(struct eventtimer *et, void *arg) 304a157e425SAlexander Motin { 3055b999a6bSDavide Italiano sbintime_t now; 3065b999a6bSDavide Italiano sbintime_t *next; 307a157e425SAlexander Motin struct pcpu_state *state; 308a157e425SAlexander Motin #ifdef SMP 309a157e425SAlexander Motin int cpu, bcast; 310a157e425SAlexander Motin #endif 311a157e425SAlexander Motin 312a157e425SAlexander Motin /* Do not touch anything if somebody reconfiguring timers. */ 313a157e425SAlexander Motin if (busy) 314a157e425SAlexander Motin return; 315a157e425SAlexander Motin /* Update present and next tick times. */ 316a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 317a157e425SAlexander Motin if (et->et_flags & ET_FLAGS_PERCPU) { 318a157e425SAlexander Motin next = &state->nexttick; 319a157e425SAlexander Motin } else 320a157e425SAlexander Motin next = &nexttick; 3215b999a6bSDavide Italiano now = sbinuptime(); 3225b999a6bSDavide Italiano if (periodic) 3235b999a6bSDavide Italiano *next = now + timerperiod; 3245b999a6bSDavide Italiano else 3255b999a6bSDavide Italiano *next = -1; /* Next tick is not scheduled yet. */ 326a157e425SAlexander Motin state->now = now; 3275b999a6bSDavide Italiano CTR3(KTR_SPARE2, "intr at %d: now %d.%08x", 3285b999a6bSDavide Italiano curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff)); 329a157e425SAlexander Motin 330a157e425SAlexander Motin #ifdef SMP 331fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 332fdce57a0SJohn Baldwin MPASS(mp_ncpus == 1 || smp_started); 333fdce57a0SJohn Baldwin #endif 334a157e425SAlexander Motin /* Prepare broadcasting to other CPUs for non-per-CPU timers. */ 335a157e425SAlexander Motin bcast = 0; 336fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 337fdce57a0SJohn Baldwin if ((et->et_flags & ET_FLAGS_PERCPU) == 0) { 338fdce57a0SJohn Baldwin #else 339a157e425SAlexander Motin if ((et->et_flags & ET_FLAGS_PERCPU) == 0 && smp_started) { 340fdce57a0SJohn Baldwin #endif 341a157e425SAlexander Motin CPU_FOREACH(cpu) { 342a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 343a157e425SAlexander Motin ET_HW_LOCK(state); 344a157e425SAlexander Motin state->now = now; 3455b999a6bSDavide Italiano if (now >= state->nextevent) { 3465b999a6bSDavide Italiano state->nextevent += SBT_1S; 3478e860de4SAlexander Motin if (curcpu != cpu) { 348a157e425SAlexander Motin state->ipi = 1; 349a157e425SAlexander Motin bcast = 1; 350a157e425SAlexander Motin } 3518e860de4SAlexander Motin } 352a157e425SAlexander Motin ET_HW_UNLOCK(state); 353a157e425SAlexander Motin } 354a157e425SAlexander Motin } 355a157e425SAlexander Motin #endif 356a157e425SAlexander Motin 357a157e425SAlexander Motin /* Handle events for this time on this CPU. */ 3585b999a6bSDavide Italiano handleevents(now, 0); 359a157e425SAlexander Motin 360a157e425SAlexander Motin #ifdef SMP 361a157e425SAlexander Motin /* Broadcast interrupt to other CPUs for non-per-CPU timers. */ 362a157e425SAlexander Motin if (bcast) { 363a157e425SAlexander Motin CPU_FOREACH(cpu) { 364a157e425SAlexander Motin if (curcpu == cpu) 365a157e425SAlexander Motin continue; 366a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 367a157e425SAlexander Motin if (state->ipi) { 368a157e425SAlexander Motin state->ipi = 0; 369a157e425SAlexander Motin ipi_cpu(cpu, IPI_HARDCLOCK); 370a157e425SAlexander Motin } 371a157e425SAlexander Motin } 372a157e425SAlexander Motin } 373a157e425SAlexander Motin #endif 374a157e425SAlexander Motin } 375a157e425SAlexander Motin 376a157e425SAlexander Motin /* 377a157e425SAlexander Motin * Load new value into hardware timer. 378a157e425SAlexander Motin */ 379a157e425SAlexander Motin static void 3805b999a6bSDavide Italiano loadtimer(sbintime_t now, int start) 381a157e425SAlexander Motin { 382a157e425SAlexander Motin struct pcpu_state *state; 3835b999a6bSDavide Italiano sbintime_t new; 3845b999a6bSDavide Italiano sbintime_t *next; 385a157e425SAlexander Motin uint64_t tmp; 386a157e425SAlexander Motin int eq; 387a157e425SAlexander Motin 388c70410e6SAlexander Motin if (timer->et_flags & ET_FLAGS_PERCPU) { 389c70410e6SAlexander Motin state = DPCPU_PTR(timerstate); 390c70410e6SAlexander Motin next = &state->nexttick; 391c70410e6SAlexander Motin } else 392c70410e6SAlexander Motin next = &nexttick; 393a157e425SAlexander Motin if (periodic) { 394a157e425SAlexander Motin if (start) { 395a157e425SAlexander Motin /* 396a157e425SAlexander Motin * Try to start all periodic timers aligned 397a157e425SAlexander Motin * to period to make events synchronous. 398a157e425SAlexander Motin */ 3995b999a6bSDavide Italiano tmp = now % timerperiod; 4005b999a6bSDavide Italiano new = timerperiod - tmp; 4015b999a6bSDavide Italiano if (new < tmp) /* Left less then passed. */ 4025b999a6bSDavide Italiano new += timerperiod; 403a157e425SAlexander Motin CTR5(KTR_SPARE2, "load p at %d: now %d.%08x first in %d.%08x", 4045b999a6bSDavide Italiano curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff), 4055b999a6bSDavide Italiano (int)(new >> 32), (u_int)(new & 0xffffffff)); 4065b999a6bSDavide Italiano *next = new + now; 4075b999a6bSDavide Italiano et_start(timer, new, timerperiod); 408a157e425SAlexander Motin } 409a157e425SAlexander Motin } else { 4105b999a6bSDavide Italiano new = getnextevent(); 4115b999a6bSDavide Italiano eq = (new == *next); 4125b999a6bSDavide Italiano CTR4(KTR_SPARE2, "load at %d: next %d.%08x eq %d", 4135b999a6bSDavide Italiano curcpu, (int)(new >> 32), (u_int)(new & 0xffffffff), eq); 414a157e425SAlexander Motin if (!eq) { 415a157e425SAlexander Motin *next = new; 4165b999a6bSDavide Italiano et_start(timer, new - now, 0); 417a157e425SAlexander Motin } 418a157e425SAlexander Motin } 419a157e425SAlexander Motin } 420a157e425SAlexander Motin 421a157e425SAlexander Motin /* 422a157e425SAlexander Motin * Prepare event timer parameters after configuration changes. 423a157e425SAlexander Motin */ 424a157e425SAlexander Motin static void 425a157e425SAlexander Motin setuptimer(void) 426a157e425SAlexander Motin { 427a157e425SAlexander Motin int freq; 428a157e425SAlexander Motin 429a157e425SAlexander Motin if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0) 430a157e425SAlexander Motin periodic = 0; 431a157e425SAlexander Motin else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0) 432a157e425SAlexander Motin periodic = 1; 433dd9595e7SAlexander Motin singlemul = MIN(MAX(singlemul, 1), 20); 434a157e425SAlexander Motin freq = hz * singlemul; 435a157e425SAlexander Motin while (freq < (profiling ? profhz : stathz)) 436a157e425SAlexander Motin freq += hz; 437a157e425SAlexander Motin freq = round_freq(timer, freq); 4385b999a6bSDavide Italiano timerperiod = SBT_1S / freq; 439a157e425SAlexander Motin } 44043fe7d45SAlexander Motin 44143fe7d45SAlexander Motin /* 44243fe7d45SAlexander Motin * Reconfigure specified per-CPU timer on other CPU. Called from IPI handler. 44343fe7d45SAlexander Motin */ 444a157e425SAlexander Motin static int 445a157e425SAlexander Motin doconfigtimer(void) 44643fe7d45SAlexander Motin { 4475b999a6bSDavide Italiano sbintime_t now; 448a157e425SAlexander Motin struct pcpu_state *state; 44943fe7d45SAlexander Motin 450a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 451a157e425SAlexander Motin switch (atomic_load_acq_int(&state->action)) { 452a157e425SAlexander Motin case 1: 4535b999a6bSDavide Italiano now = sbinuptime(); 454a157e425SAlexander Motin ET_HW_LOCK(state); 4555b999a6bSDavide Italiano loadtimer(now, 1); 456a157e425SAlexander Motin ET_HW_UNLOCK(state); 457a157e425SAlexander Motin state->handle = 0; 458a157e425SAlexander Motin atomic_store_rel_int(&state->action, 0); 459a157e425SAlexander Motin return (1); 460a157e425SAlexander Motin case 2: 461a157e425SAlexander Motin ET_HW_LOCK(state); 462a157e425SAlexander Motin et_stop(timer); 463a157e425SAlexander Motin ET_HW_UNLOCK(state); 464a157e425SAlexander Motin state->handle = 0; 465a157e425SAlexander Motin atomic_store_rel_int(&state->action, 0); 466a157e425SAlexander Motin return (1); 467a157e425SAlexander Motin } 468a157e425SAlexander Motin if (atomic_readandclear_int(&state->handle) && !busy) { 4695b999a6bSDavide Italiano now = sbinuptime(); 4705b999a6bSDavide Italiano handleevents(now, 0); 47143fe7d45SAlexander Motin return (1); 47243fe7d45SAlexander Motin } 47343fe7d45SAlexander Motin return (0); 47443fe7d45SAlexander Motin } 47543fe7d45SAlexander Motin 47643fe7d45SAlexander Motin /* 47743fe7d45SAlexander Motin * Reconfigure specified timer. 47843fe7d45SAlexander Motin * For per-CPU timers use IPI to make other CPUs to reconfigure. 47943fe7d45SAlexander Motin */ 48043fe7d45SAlexander Motin static void 481a157e425SAlexander Motin configtimer(int start) 48243fe7d45SAlexander Motin { 4835b999a6bSDavide Italiano sbintime_t now, next; 484a157e425SAlexander Motin struct pcpu_state *state; 48543fe7d45SAlexander Motin int cpu; 48643fe7d45SAlexander Motin 487a157e425SAlexander Motin if (start) { 488a157e425SAlexander Motin setuptimer(); 4895b999a6bSDavide Italiano now = sbinuptime(); 4905b999a6bSDavide Italiano } else 4915b999a6bSDavide Italiano now = 0; 49243fe7d45SAlexander Motin critical_enter(); 493a157e425SAlexander Motin ET_HW_LOCK(DPCPU_PTR(timerstate)); 494a157e425SAlexander Motin if (start) { 495a157e425SAlexander Motin /* Initialize time machine parameters. */ 4965b999a6bSDavide Italiano next = now + timerperiod; 497a157e425SAlexander Motin if (periodic) 498a157e425SAlexander Motin nexttick = next; 49943fe7d45SAlexander Motin else 5005b999a6bSDavide Italiano nexttick = -1; 501fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 502fdce57a0SJohn Baldwin MPASS(mp_ncpus == 1 || smp_started); 503fdce57a0SJohn Baldwin #endif 504a157e425SAlexander Motin CPU_FOREACH(cpu) { 505a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 506a157e425SAlexander Motin state->now = now; 507fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP 5085b999a6bSDavide Italiano if (!smp_started && cpu != CPU_FIRST()) 5094bc38a5aSDavide Italiano state->nextevent = SBT_MAX; 5105b999a6bSDavide Italiano else 511fdce57a0SJohn Baldwin #endif 512a157e425SAlexander Motin state->nextevent = next; 513a157e425SAlexander Motin if (periodic) 514a157e425SAlexander Motin state->nexttick = next; 515a157e425SAlexander Motin else 5165b999a6bSDavide Italiano state->nexttick = -1; 517a157e425SAlexander Motin state->nexthard = next; 518a157e425SAlexander Motin state->nextstat = next; 519a157e425SAlexander Motin state->nextprof = next; 5205b999a6bSDavide Italiano state->nextcall = next; 5215b999a6bSDavide Italiano state->nextcallopt = next; 522a157e425SAlexander Motin hardclock_sync(cpu); 523a157e425SAlexander Motin } 524a157e425SAlexander Motin busy = 0; 525a157e425SAlexander Motin /* Start global timer or per-CPU timer of this CPU. */ 5265b999a6bSDavide Italiano loadtimer(now, 1); 527a157e425SAlexander Motin } else { 528a157e425SAlexander Motin busy = 1; 529a157e425SAlexander Motin /* Stop global timer or per-CPU timer of this CPU. */ 530a157e425SAlexander Motin et_stop(timer); 531a157e425SAlexander Motin } 532a157e425SAlexander Motin ET_HW_UNLOCK(DPCPU_PTR(timerstate)); 53343fe7d45SAlexander Motin #ifdef SMP 534fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP 535fdce57a0SJohn Baldwin /* If timer is global we are done. */ 536fdce57a0SJohn Baldwin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0) { 537fdce57a0SJohn Baldwin #else 538a157e425SAlexander Motin /* If timer is global or there is no other CPUs yet - we are done. */ 539a157e425SAlexander Motin if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || !smp_started) { 540fdce57a0SJohn Baldwin #endif 54143fe7d45SAlexander Motin critical_exit(); 54243fe7d45SAlexander Motin return; 54343fe7d45SAlexander Motin } 54443fe7d45SAlexander Motin /* Set reconfigure flags for other CPUs. */ 54543fe7d45SAlexander Motin CPU_FOREACH(cpu) { 546a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 547a157e425SAlexander Motin atomic_store_rel_int(&state->action, 548a157e425SAlexander Motin (cpu == curcpu) ? 0 : ( start ? 1 : 2)); 54943fe7d45SAlexander Motin } 550a157e425SAlexander Motin /* Broadcast reconfigure IPI. */ 551a157e425SAlexander Motin ipi_all_but_self(IPI_HARDCLOCK); 55243fe7d45SAlexander Motin /* Wait for reconfiguration completed. */ 55343fe7d45SAlexander Motin restart: 55443fe7d45SAlexander Motin cpu_spinwait(); 55543fe7d45SAlexander Motin CPU_FOREACH(cpu) { 55643fe7d45SAlexander Motin if (cpu == curcpu) 55743fe7d45SAlexander Motin continue; 558a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 559a157e425SAlexander Motin if (atomic_load_acq_int(&state->action)) 56043fe7d45SAlexander Motin goto restart; 56143fe7d45SAlexander Motin } 56243fe7d45SAlexander Motin #endif 563a157e425SAlexander Motin critical_exit(); 56443fe7d45SAlexander Motin } 56543fe7d45SAlexander Motin 566a157e425SAlexander Motin /* 567a157e425SAlexander Motin * Calculate nearest frequency supported by hardware timer. 568a157e425SAlexander Motin */ 56951636352SAlexander Motin static int 57051636352SAlexander Motin round_freq(struct eventtimer *et, int freq) 57151636352SAlexander Motin { 57251636352SAlexander Motin uint64_t div; 57351636352SAlexander Motin 57451636352SAlexander Motin if (et->et_frequency != 0) { 575599cf0f1SAlexander Motin div = lmax((et->et_frequency + freq / 2) / freq, 1); 57651636352SAlexander Motin if (et->et_flags & ET_FLAGS_POW2DIV) 57751636352SAlexander Motin div = 1 << (flsl(div + div / 2) - 1); 57851636352SAlexander Motin freq = (et->et_frequency + div / 2) / div; 57951636352SAlexander Motin } 580fdc5dd2dSAlexander Motin if (et->et_min_period > SBT_1S) 581803a9b3eSAlexander Motin panic("Event timer \"%s\" doesn't support sub-second periods!", 582803a9b3eSAlexander Motin et->et_name); 583fdc5dd2dSAlexander Motin else if (et->et_min_period != 0) 584fdc5dd2dSAlexander Motin freq = min(freq, SBT2FREQ(et->et_min_period)); 585fdc5dd2dSAlexander Motin if (et->et_max_period < SBT_1S && et->et_max_period != 0) 586fdc5dd2dSAlexander Motin freq = max(freq, SBT2FREQ(et->et_max_period)); 58751636352SAlexander Motin return (freq); 58851636352SAlexander Motin } 58951636352SAlexander Motin 59043fe7d45SAlexander Motin /* 591a157e425SAlexander Motin * Configure and start event timers (BSP part). 59243fe7d45SAlexander Motin */ 59343fe7d45SAlexander Motin void 59443fe7d45SAlexander Motin cpu_initclocks_bsp(void) 59543fe7d45SAlexander Motin { 596a157e425SAlexander Motin struct pcpu_state *state; 597a157e425SAlexander Motin int base, div, cpu; 59843fe7d45SAlexander Motin 599a157e425SAlexander Motin mtx_init(&et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN); 600a157e425SAlexander Motin CPU_FOREACH(cpu) { 601a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 602a157e425SAlexander Motin mtx_init(&state->et_hw_mtx, "et_hw_mtx", NULL, MTX_SPIN); 6034bc38a5aSDavide Italiano state->nextcall = SBT_MAX; 6044bc38a5aSDavide Italiano state->nextcallopt = SBT_MAX; 605a157e425SAlexander Motin } 606afe41f2dSAlexander Motin periodic = want_periodic; 607a157e425SAlexander Motin /* Grab requested timer or the best of present. */ 608a157e425SAlexander Motin if (timername[0]) 609a157e425SAlexander Motin timer = et_find(timername, 0, 0); 610a157e425SAlexander Motin if (timer == NULL && periodic) { 611a157e425SAlexander Motin timer = et_find(NULL, 61243fe7d45SAlexander Motin ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); 613a157e425SAlexander Motin } 614a157e425SAlexander Motin if (timer == NULL) { 615a157e425SAlexander Motin timer = et_find(NULL, 616a157e425SAlexander Motin ET_FLAGS_ONESHOT, ET_FLAGS_ONESHOT); 617a157e425SAlexander Motin } 618a157e425SAlexander Motin if (timer == NULL && !periodic) { 619a157e425SAlexander Motin timer = et_find(NULL, 620a157e425SAlexander Motin ET_FLAGS_PERIODIC, ET_FLAGS_PERIODIC); 621a157e425SAlexander Motin } 622a157e425SAlexander Motin if (timer == NULL) 623a157e425SAlexander Motin panic("No usable event timer found!"); 624a157e425SAlexander Motin et_init(timer, timercb, NULL, NULL); 625a157e425SAlexander Motin 626a157e425SAlexander Motin /* Adapt to timer capabilities. */ 627a157e425SAlexander Motin if (periodic && (timer->et_flags & ET_FLAGS_PERIODIC) == 0) 628a157e425SAlexander Motin periodic = 0; 629a157e425SAlexander Motin else if (!periodic && (timer->et_flags & ET_FLAGS_ONESHOT) == 0) 630a157e425SAlexander Motin periodic = 1; 631a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_C3STOP) 63292597e06SJohn Baldwin cpu_disable_c3_sleep++; 633a157e425SAlexander Motin 63443fe7d45SAlexander Motin /* 63543fe7d45SAlexander Motin * We honor the requested 'hz' value. 63643fe7d45SAlexander Motin * We want to run stathz in the neighborhood of 128hz. 63743fe7d45SAlexander Motin * We would like profhz to run as often as possible. 63843fe7d45SAlexander Motin */ 639dd9595e7SAlexander Motin if (singlemul <= 0 || singlemul > 20) { 64043fe7d45SAlexander Motin if (hz >= 1500 || (hz % 128) == 0) 64143fe7d45SAlexander Motin singlemul = 1; 64243fe7d45SAlexander Motin else if (hz >= 750) 64343fe7d45SAlexander Motin singlemul = 2; 64443fe7d45SAlexander Motin else 64543fe7d45SAlexander Motin singlemul = 4; 64643fe7d45SAlexander Motin } 647a157e425SAlexander Motin if (periodic) { 648a157e425SAlexander Motin base = round_freq(timer, hz * singlemul); 64951636352SAlexander Motin singlemul = max((base + hz / 2) / hz, 1); 65051636352SAlexander Motin hz = (base + singlemul / 2) / singlemul; 65151636352SAlexander Motin if (base <= 128) 65243fe7d45SAlexander Motin stathz = base; 65343fe7d45SAlexander Motin else { 65443fe7d45SAlexander Motin div = base / 128; 65551636352SAlexander Motin if (div >= singlemul && (div % singlemul) == 0) 65643fe7d45SAlexander Motin div++; 65743fe7d45SAlexander Motin stathz = base / div; 65843fe7d45SAlexander Motin } 65943fe7d45SAlexander Motin profhz = stathz; 66051636352SAlexander Motin while ((profhz + stathz) <= 128 * 64) 66143fe7d45SAlexander Motin profhz += stathz; 662a157e425SAlexander Motin profhz = round_freq(timer, profhz); 66343fe7d45SAlexander Motin } else { 664a157e425SAlexander Motin hz = round_freq(timer, hz); 665a157e425SAlexander Motin stathz = round_freq(timer, 127); 666a157e425SAlexander Motin profhz = round_freq(timer, stathz * 64); 66743fe7d45SAlexander Motin } 668599cf0f1SAlexander Motin tick = 1000000 / hz; 6695b999a6bSDavide Italiano tick_sbt = SBT_1S / hz; 6705b999a6bSDavide Italiano tick_bt = sbttobt(tick_sbt); 6715b999a6bSDavide Italiano statperiod = SBT_1S / stathz; 6725b999a6bSDavide Italiano profperiod = SBT_1S / profhz; 67343fe7d45SAlexander Motin ET_LOCK(); 674a157e425SAlexander Motin configtimer(1); 67543fe7d45SAlexander Motin ET_UNLOCK(); 67643fe7d45SAlexander Motin } 67743fe7d45SAlexander Motin 678a157e425SAlexander Motin /* 679a157e425SAlexander Motin * Start per-CPU event timers on APs. 680a157e425SAlexander Motin */ 68143fe7d45SAlexander Motin void 68243fe7d45SAlexander Motin cpu_initclocks_ap(void) 68343fe7d45SAlexander Motin { 6845b999a6bSDavide Italiano sbintime_t now; 685a157e425SAlexander Motin struct pcpu_state *state; 6865b999a6bSDavide Italiano struct thread *td; 68743fe7d45SAlexander Motin 688a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 6895b999a6bSDavide Italiano now = sbinuptime(); 690a157e425SAlexander Motin ET_HW_LOCK(state); 691c70410e6SAlexander Motin state->now = now; 692c70410e6SAlexander Motin hardclock_sync(curcpu); 6935b999a6bSDavide Italiano spinlock_enter(); 694a157e425SAlexander Motin ET_HW_UNLOCK(state); 6955b999a6bSDavide Italiano td = curthread; 6965b999a6bSDavide Italiano td->td_intr_nesting_level++; 6975b999a6bSDavide Italiano handleevents(state->now, 2); 6985b999a6bSDavide Italiano td->td_intr_nesting_level--; 6995b999a6bSDavide Italiano spinlock_exit(); 70043fe7d45SAlexander Motin } 70143fe7d45SAlexander Motin 70227dca831SAndriy Gapon void 70327dca831SAndriy Gapon suspendclock(void) 70427dca831SAndriy Gapon { 70527dca831SAndriy Gapon ET_LOCK(); 70627dca831SAndriy Gapon configtimer(0); 70727dca831SAndriy Gapon ET_UNLOCK(); 70827dca831SAndriy Gapon } 70927dca831SAndriy Gapon 71027dca831SAndriy Gapon void 71127dca831SAndriy Gapon resumeclock(void) 71227dca831SAndriy Gapon { 71327dca831SAndriy Gapon ET_LOCK(); 71427dca831SAndriy Gapon configtimer(1); 71527dca831SAndriy Gapon ET_UNLOCK(); 71627dca831SAndriy Gapon } 71727dca831SAndriy Gapon 718a157e425SAlexander Motin /* 719a157e425SAlexander Motin * Switch to profiling clock rates. 720a157e425SAlexander Motin */ 72143fe7d45SAlexander Motin void 72243fe7d45SAlexander Motin cpu_startprofclock(void) 72343fe7d45SAlexander Motin { 72443fe7d45SAlexander Motin 72543fe7d45SAlexander Motin ET_LOCK(); 7261af19ee4SAlexander Motin if (profiling == 0) { 727a157e425SAlexander Motin if (periodic) { 728a157e425SAlexander Motin configtimer(0); 729a157e425SAlexander Motin profiling = 1; 730a157e425SAlexander Motin configtimer(1); 731a157e425SAlexander Motin } else 732a157e425SAlexander Motin profiling = 1; 7331af19ee4SAlexander Motin } else 7341af19ee4SAlexander Motin profiling++; 73543fe7d45SAlexander Motin ET_UNLOCK(); 73643fe7d45SAlexander Motin } 73743fe7d45SAlexander Motin 738a157e425SAlexander Motin /* 739a157e425SAlexander Motin * Switch to regular clock rates. 740a157e425SAlexander Motin */ 74143fe7d45SAlexander Motin void 74243fe7d45SAlexander Motin cpu_stopprofclock(void) 74343fe7d45SAlexander Motin { 74443fe7d45SAlexander Motin 74543fe7d45SAlexander Motin ET_LOCK(); 7461af19ee4SAlexander Motin if (profiling == 1) { 747a157e425SAlexander Motin if (periodic) { 748a157e425SAlexander Motin configtimer(0); 749a157e425SAlexander Motin profiling = 0; 750a157e425SAlexander Motin configtimer(1); 751a157e425SAlexander Motin } else 752a157e425SAlexander Motin profiling = 0; 7531af19ee4SAlexander Motin } else 7541af19ee4SAlexander Motin profiling--; 75543fe7d45SAlexander Motin ET_UNLOCK(); 75643fe7d45SAlexander Motin } 75743fe7d45SAlexander Motin 758a157e425SAlexander Motin /* 759a157e425SAlexander Motin * Switch to idle mode (all ticks handled). 760a157e425SAlexander Motin */ 761acccf7d8SDavide Italiano sbintime_t 762a157e425SAlexander Motin cpu_idleclock(void) 763a157e425SAlexander Motin { 7645b999a6bSDavide Italiano sbintime_t now, t; 765a157e425SAlexander Motin struct pcpu_state *state; 766a157e425SAlexander Motin 767a157e425SAlexander Motin if (idletick || busy || 7689dfc483cSAlexander Motin (periodic && (timer->et_flags & ET_FLAGS_PERCPU)) 7699dfc483cSAlexander Motin #ifdef DEVICE_POLLING 7709dfc483cSAlexander Motin || curcpu == CPU_FIRST() 7719dfc483cSAlexander Motin #endif 7729dfc483cSAlexander Motin ) 773acccf7d8SDavide Italiano return (-1); 774a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 775a157e425SAlexander Motin if (periodic) 776a157e425SAlexander Motin now = state->now; 777a157e425SAlexander Motin else 7785b999a6bSDavide Italiano now = sbinuptime(); 7795b999a6bSDavide Italiano CTR3(KTR_SPARE2, "idle at %d: now %d.%08x", 7805b999a6bSDavide Italiano curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff)); 7815b999a6bSDavide Italiano t = getnextcpuevent(1); 782a157e425SAlexander Motin ET_HW_LOCK(state); 783a157e425SAlexander Motin state->idle = 1; 784a157e425SAlexander Motin state->nextevent = t; 785a157e425SAlexander Motin if (!periodic) 7865b999a6bSDavide Italiano loadtimer(now, 0); 787a157e425SAlexander Motin ET_HW_UNLOCK(state); 7885b999a6bSDavide Italiano return (MAX(t - now, 0)); 789a157e425SAlexander Motin } 790a157e425SAlexander Motin 791a157e425SAlexander Motin /* 792a157e425SAlexander Motin * Switch to active mode (skip empty ticks). 793a157e425SAlexander Motin */ 794a157e425SAlexander Motin void 795a157e425SAlexander Motin cpu_activeclock(void) 796a157e425SAlexander Motin { 7975b999a6bSDavide Italiano sbintime_t now; 798a157e425SAlexander Motin struct pcpu_state *state; 799a157e425SAlexander Motin struct thread *td; 800a157e425SAlexander Motin 801a157e425SAlexander Motin state = DPCPU_PTR(timerstate); 802a157e425SAlexander Motin if (state->idle == 0 || busy) 803a157e425SAlexander Motin return; 804a157e425SAlexander Motin if (periodic) 805a157e425SAlexander Motin now = state->now; 806a157e425SAlexander Motin else 8075b999a6bSDavide Italiano now = sbinuptime(); 8085b999a6bSDavide Italiano CTR3(KTR_SPARE2, "active at %d: now %d.%08x", 8095b999a6bSDavide Italiano curcpu, (int)(now >> 32), (u_int)(now & 0xffffffff)); 810a157e425SAlexander Motin spinlock_enter(); 811a157e425SAlexander Motin td = curthread; 812a157e425SAlexander Motin td->td_intr_nesting_level++; 8135b999a6bSDavide Italiano handleevents(now, 1); 814a157e425SAlexander Motin td->td_intr_nesting_level--; 815a157e425SAlexander Motin spinlock_exit(); 816a157e425SAlexander Motin } 817a157e425SAlexander Motin 818cfc4b56bSIan Lepore /* 819cfc4b56bSIan Lepore * Change the frequency of the given timer. This changes et->et_frequency and 820cfc4b56bSIan Lepore * if et is the active timer it reconfigures the timer on all CPUs. This is 821cfc4b56bSIan Lepore * intended to be a private interface for the use of et_change_frequency() only. 822cfc4b56bSIan Lepore */ 823cfc4b56bSIan Lepore void 824cfc4b56bSIan Lepore cpu_et_frequency(struct eventtimer *et, uint64_t newfreq) 825cfc4b56bSIan Lepore { 826cfc4b56bSIan Lepore 827cfc4b56bSIan Lepore ET_LOCK(); 828cfc4b56bSIan Lepore if (et == timer) { 829cfc4b56bSIan Lepore configtimer(0); 830cfc4b56bSIan Lepore et->et_frequency = newfreq; 831cfc4b56bSIan Lepore configtimer(1); 832cfc4b56bSIan Lepore } else 833cfc4b56bSIan Lepore et->et_frequency = newfreq; 834cfc4b56bSIan Lepore ET_UNLOCK(); 835cfc4b56bSIan Lepore } 836cfc4b56bSIan Lepore 8375b999a6bSDavide Italiano void 8385b999a6bSDavide Italiano cpu_new_callout(int cpu, sbintime_t bt, sbintime_t bt_opt) 839a157e425SAlexander Motin { 840a157e425SAlexander Motin struct pcpu_state *state; 841a157e425SAlexander Motin 8425b999a6bSDavide Italiano /* Do not touch anything if somebody reconfiguring timers. */ 8435b999a6bSDavide Italiano if (busy) 8445b999a6bSDavide Italiano return; 8455b999a6bSDavide Italiano CTR6(KTR_SPARE2, "new co at %d: on %d at %d.%08x - %d.%08x", 8465b999a6bSDavide Italiano curcpu, cpu, (int)(bt_opt >> 32), (u_int)(bt_opt & 0xffffffff), 8475b999a6bSDavide Italiano (int)(bt >> 32), (u_int)(bt & 0xffffffff)); 848efe67753SNathan Whitehorn 849efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu)); 850a157e425SAlexander Motin state = DPCPU_ID_PTR(cpu, timerstate); 851a157e425SAlexander Motin ET_HW_LOCK(state); 8525b999a6bSDavide Italiano 8535b999a6bSDavide Italiano /* 8545b999a6bSDavide Italiano * If there is callout time already set earlier -- do nothing. 8555b999a6bSDavide Italiano * This check may appear redundant because we check already in 8565b999a6bSDavide Italiano * callout_process() but this double check guarantees we're safe 8575b999a6bSDavide Italiano * with respect to race conditions between interrupts execution 8585b999a6bSDavide Italiano * and scheduling. 8595b999a6bSDavide Italiano */ 8605b999a6bSDavide Italiano state->nextcallopt = bt_opt; 8615b999a6bSDavide Italiano if (bt >= state->nextcall) 8625b999a6bSDavide Italiano goto done; 8635b999a6bSDavide Italiano state->nextcall = bt; 8645b999a6bSDavide Italiano /* If there is some other event set earlier -- do nothing. */ 8655b999a6bSDavide Italiano if (bt >= state->nextevent) 8665b999a6bSDavide Italiano goto done; 8675b999a6bSDavide Italiano state->nextevent = bt; 8685b999a6bSDavide Italiano /* If timer is periodic -- there is nothing to reprogram. */ 8695b999a6bSDavide Italiano if (periodic) 8705b999a6bSDavide Italiano goto done; 8715b999a6bSDavide Italiano /* If timer is global or of the current CPU -- reprogram it. */ 8725b999a6bSDavide Italiano if ((timer->et_flags & ET_FLAGS_PERCPU) == 0 || cpu == curcpu) { 8735b999a6bSDavide Italiano loadtimer(sbinuptime(), 0); 8745b999a6bSDavide Italiano done: 875a157e425SAlexander Motin ET_HW_UNLOCK(state); 876a157e425SAlexander Motin return; 877a157e425SAlexander Motin } 8785b999a6bSDavide Italiano /* Otherwise make other CPU to reprogram it. */ 879a157e425SAlexander Motin state->handle = 1; 8805b999a6bSDavide Italiano ET_HW_UNLOCK(state); 8815b999a6bSDavide Italiano #ifdef SMP 882a157e425SAlexander Motin ipi_cpu(cpu, IPI_HARDCLOCK); 883a157e425SAlexander Motin #endif 8845b999a6bSDavide Italiano } 885a157e425SAlexander Motin 886a157e425SAlexander Motin /* 887a157e425SAlexander Motin * Report or change the active event timers hardware. 888a157e425SAlexander Motin */ 88943fe7d45SAlexander Motin static int 890a157e425SAlexander Motin sysctl_kern_eventtimer_timer(SYSCTL_HANDLER_ARGS) 89143fe7d45SAlexander Motin { 89243fe7d45SAlexander Motin char buf[32]; 89343fe7d45SAlexander Motin struct eventtimer *et; 89443fe7d45SAlexander Motin int error; 89543fe7d45SAlexander Motin 89643fe7d45SAlexander Motin ET_LOCK(); 897a157e425SAlexander Motin et = timer; 89843fe7d45SAlexander Motin snprintf(buf, sizeof(buf), "%s", et->et_name); 89943fe7d45SAlexander Motin ET_UNLOCK(); 90043fe7d45SAlexander Motin error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 90143fe7d45SAlexander Motin ET_LOCK(); 902a157e425SAlexander Motin et = timer; 90343fe7d45SAlexander Motin if (error != 0 || req->newptr == NULL || 904a157e425SAlexander Motin strcasecmp(buf, et->et_name) == 0) { 90543fe7d45SAlexander Motin ET_UNLOCK(); 90643fe7d45SAlexander Motin return (error); 90743fe7d45SAlexander Motin } 908a157e425SAlexander Motin et = et_find(buf, 0, 0); 90943fe7d45SAlexander Motin if (et == NULL) { 91043fe7d45SAlexander Motin ET_UNLOCK(); 91143fe7d45SAlexander Motin return (ENOENT); 91243fe7d45SAlexander Motin } 91343fe7d45SAlexander Motin configtimer(0); 914a157e425SAlexander Motin et_free(timer); 915a157e425SAlexander Motin if (et->et_flags & ET_FLAGS_C3STOP) 91692597e06SJohn Baldwin cpu_disable_c3_sleep++; 917a157e425SAlexander Motin if (timer->et_flags & ET_FLAGS_C3STOP) 91892597e06SJohn Baldwin cpu_disable_c3_sleep--; 919afe41f2dSAlexander Motin periodic = want_periodic; 920a157e425SAlexander Motin timer = et; 921a157e425SAlexander Motin et_init(timer, timercb, NULL, NULL); 92243fe7d45SAlexander Motin configtimer(1); 92343fe7d45SAlexander Motin ET_UNLOCK(); 92443fe7d45SAlexander Motin return (error); 92543fe7d45SAlexander Motin } 926a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, timer, 92743fe7d45SAlexander Motin CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, 928dd9595e7SAlexander Motin 0, 0, sysctl_kern_eventtimer_timer, "A", "Chosen event timer"); 929a157e425SAlexander Motin 930a157e425SAlexander Motin /* 931a157e425SAlexander Motin * Report or change the active event timer periodicity. 932a157e425SAlexander Motin */ 933a157e425SAlexander Motin static int 934a157e425SAlexander Motin sysctl_kern_eventtimer_periodic(SYSCTL_HANDLER_ARGS) 935a157e425SAlexander Motin { 936a157e425SAlexander Motin int error, val; 937a157e425SAlexander Motin 938a157e425SAlexander Motin val = periodic; 939a157e425SAlexander Motin error = sysctl_handle_int(oidp, &val, 0, req); 940a157e425SAlexander Motin if (error != 0 || req->newptr == NULL) 941a157e425SAlexander Motin return (error); 942a157e425SAlexander Motin ET_LOCK(); 943a157e425SAlexander Motin configtimer(0); 944afe41f2dSAlexander Motin periodic = want_periodic = val; 945a157e425SAlexander Motin configtimer(1); 946a157e425SAlexander Motin ET_UNLOCK(); 947a157e425SAlexander Motin return (error); 948a157e425SAlexander Motin } 949a157e425SAlexander Motin SYSCTL_PROC(_kern_eventtimer, OID_AUTO, periodic, 950a157e425SAlexander Motin CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 951dd9595e7SAlexander Motin 0, 0, sysctl_kern_eventtimer_periodic, "I", "Enable event timer periodic mode"); 9526e3bf539SKonstantin Belousov 9536e3bf539SKonstantin Belousov #include "opt_ddb.h" 9546e3bf539SKonstantin Belousov 9556e3bf539SKonstantin Belousov #ifdef DDB 9566e3bf539SKonstantin Belousov #include <ddb/ddb.h> 9576e3bf539SKonstantin Belousov 9586e3bf539SKonstantin Belousov DB_SHOW_COMMAND(clocksource, db_show_clocksource) 9596e3bf539SKonstantin Belousov { 9606e3bf539SKonstantin Belousov struct pcpu_state *st; 9616e3bf539SKonstantin Belousov int c; 9626e3bf539SKonstantin Belousov 9636e3bf539SKonstantin Belousov CPU_FOREACH(c) { 9646e3bf539SKonstantin Belousov st = DPCPU_ID_PTR(c, timerstate); 9656e3bf539SKonstantin Belousov db_printf( 9666e3bf539SKonstantin Belousov "CPU %2d: action %d handle %d ipi %d idle %d\n" 9676e3bf539SKonstantin Belousov " now %#jx nevent %#jx (%jd)\n" 9686e3bf539SKonstantin Belousov " ntick %#jx (%jd) nhard %#jx (%jd)\n" 9696e3bf539SKonstantin Belousov " nstat %#jx (%jd) nprof %#jx (%jd)\n" 9706e3bf539SKonstantin Belousov " ncall %#jx (%jd) ncallopt %#jx (%jd)\n", 9716e3bf539SKonstantin Belousov c, st->action, st->handle, st->ipi, st->idle, 9726e3bf539SKonstantin Belousov (uintmax_t)st->now, 9736e3bf539SKonstantin Belousov (uintmax_t)st->nextevent, 9746e3bf539SKonstantin Belousov (uintmax_t)(st->nextevent - st->now) / tick_sbt, 9756e3bf539SKonstantin Belousov (uintmax_t)st->nexttick, 9766e3bf539SKonstantin Belousov (uintmax_t)(st->nexttick - st->now) / tick_sbt, 9776e3bf539SKonstantin Belousov (uintmax_t)st->nexthard, 9786e3bf539SKonstantin Belousov (uintmax_t)(st->nexthard - st->now) / tick_sbt, 9796e3bf539SKonstantin Belousov (uintmax_t)st->nextstat, 9806e3bf539SKonstantin Belousov (uintmax_t)(st->nextstat - st->now) / tick_sbt, 9816e3bf539SKonstantin Belousov (uintmax_t)st->nextprof, 9826e3bf539SKonstantin Belousov (uintmax_t)(st->nextprof - st->now) / tick_sbt, 9836e3bf539SKonstantin Belousov (uintmax_t)st->nextcall, 9846e3bf539SKonstantin Belousov (uintmax_t)(st->nextcall - st->now) / tick_sbt, 9856e3bf539SKonstantin Belousov (uintmax_t)st->nextcallopt, 9866e3bf539SKonstantin Belousov (uintmax_t)(st->nextcallopt - st->now) / tick_sbt); 9876e3bf539SKonstantin Belousov } 9886e3bf539SKonstantin Belousov } 9896e3bf539SKonstantin Belousov 9906e3bf539SKonstantin Belousov #endif 991