1*339a7e8cScheloha.\" $OpenBSD: tc_init.9,v 1.12 2023/04/02 00:02:26 cheloha Exp $ 286e8949aSgrange.\" 386e8949aSgrange.\" Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> 4*339a7e8cScheloha.\" Copyright (c) 2023 Scott Cheloha <cheloha@openbsd.org> 586e8949aSgrange.\" 686e8949aSgrange.\" Permission to use, copy, modify, and distribute this software for any 786e8949aSgrange.\" purpose with or without fee is hereby granted, provided that the above 886e8949aSgrange.\" copyright notice and this permission notice appear in all copies. 986e8949aSgrange.\" 1086e8949aSgrange.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1186e8949aSgrange.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1286e8949aSgrange.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1386e8949aSgrange.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1486e8949aSgrange.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1586e8949aSgrange.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1686e8949aSgrange.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1786e8949aSgrange.\" 18*339a7e8cScheloha.Dd $Mdocdate: April 2 2023 $ 1986e8949aSgrange.Dt TC_INIT 9 2086e8949aSgrange.Os 2186e8949aSgrange.Sh NAME 2286e8949aSgrange.Nm tc_init 23*339a7e8cScheloha.Nd timecounting subsystem 2486e8949aSgrange.Sh SYNOPSIS 25dddd2645Sschwarze.In sys/timetc.h 2686e8949aSgrange.Ft void 2786e8949aSgrange.Fn tc_init "struct timecounter *tc" 2886e8949aSgrange.Sh DESCRIPTION 29*339a7e8cSchelohaThe 30*339a7e8cScheloha.Sy timecounting 31*339a7e8cSchelohasubsystem implements a uniform interface to timekeeping hardware, 32*339a7e8cSchelohameasures the passage of time, 33*339a7e8cSchelohaand implements the kernel's software clocks 34*339a7e8cScheloha.Po see 35*339a7e8cScheloha.Xr microtime 9 36*339a7e8cSchelohafor details 37*339a7e8cScheloha.Pc . 3886e8949aSgrange.Pp 39*339a7e8cSchelohaA hardware clock is suitable for counting time if it meets the following 40*339a7e8cScheloharequirements: 41*339a7e8cScheloha.Bl -enum -offset indent 4286e8949aSgrange.It 43*339a7e8cSchelohaIt is a binary counter. 4486e8949aSgrange.It 45*339a7e8cSchelohaIt advances at a fixed, known frequency. 46*339a7e8cScheloha.It 47*339a7e8cSchelohaIts count is synchronized between all CPUs on the system. 48*339a7e8cScheloha.It 49*339a7e8cSchelohaIt continues counting when it rolls over. 50*339a7e8cScheloha.It 51*339a7e8cSchelohaIf 52*339a7e8cScheloha.Xr hz 9 53*339a7e8cSchelohais less than or equal to one millisecond, 54*339a7e8cSchelohathe counter does not roll over in less than two milliseconds. 55*339a7e8cSchelohaIf 56*339a7e8cScheloha.Xr hz 9 57*339a7e8cSchelohaexceeds one millisecond, 58*339a7e8cSchelohathe counter does not roll over in less than 59*339a7e8cScheloha.Pq 2 / Va hz 60*339a7e8cSchelohaseconds. 6186e8949aSgrange.El 6286e8949aSgrange.Pp 63*339a7e8cSchelohaHardware clocks are described with a 6486e8949aSgrange.Va timecounter 6586e8949aSgrangestructure: 6686e8949aSgrange.Bd -literal -offset indent 6786e8949aSgrangestruct timecounter { 68*339a7e8cScheloha u_int (*tc_get_timecount)(struct timecounter *); 6986e8949aSgrange u_int tc_counter_mask; 7086e8949aSgrange u_int64_t tc_frequency; 7186e8949aSgrange char *tc_name; 7286e8949aSgrange int tc_quality; 7386e8949aSgrange void *tc_priv; 74*339a7e8cScheloha u_int tc_user; 75*339a7e8cScheloha}; 7686e8949aSgrange.Ed 7786e8949aSgrange.Bl -tag -width indent 78811648c4Sschwarze.It Ft u_int Fn (*tc_get_timecount) "struct timecounter *" 79*339a7e8cSchelohaReads the hardware clock and returns its count. 80*339a7e8cSchelohaAny unimplemented bits only need to be masked if they are not constant. 81*339a7e8cSchelohaIf the counter is larger than 32 bits, 82*339a7e8cSchelohathis function must return a 32-bit subset. 83*339a7e8cSchelohaThe subsystem requires an upward count; 84*339a7e8cSchelohadownward counts must be inverted before they are returned. 8586e8949aSgrange.It Va tc_counter_mask 86*339a7e8cSchelohaThe mask of implemented bits. 87*339a7e8cSchelohaUsed to discard unimplemented bits from 88*339a7e8cScheloha.Fn tc_get_timecount . 8986e8949aSgrange.It Va tc_frequency 90*339a7e8cSchelohaThe counter's fixed frequency. 9186e8949aSgrange.It Va tc_name 92*339a7e8cSchelohaThe counter's unique name. 93*339a7e8cSchelohaA 94*339a7e8cScheloha.Dv NUL Ns -terminated string. 9586e8949aSgrange.It Va tc_quality 96*339a7e8cSchelohaA relative quality metric used to compare counters. 97*339a7e8cSchelohaHigher values indicate a better counter. 98*339a7e8cSchelohaA negative value indicates that the counter is non-monotonic 99*339a7e8cSchelohaor otherwise deficient. 100*339a7e8cSchelohaThe system will only use negative-quality counters if requested. 10186e8949aSgrange.It Va tc_priv 102*339a7e8cSchelohaMay point to anything the driver needs during 103*339a7e8cScheloha.Fn tc_get_timecount . 104*339a7e8cScheloha.It Va tc_user 105*339a7e8cSchelohaIf non-zero, 106*339a7e8cSchelohaa unique value identifying the userspace implementation of 107*339a7e8cScheloha.Fn tc_get_timecount . 10886e8949aSgrange.El 10986e8949aSgrange.Pp 110*339a7e8cSchelohaTo register a timecounter, 111*339a7e8cSchelohaa device driver initializes the above-described fields of a 11286e8949aSgrange.Va timecounter 113*339a7e8cSchelohastructure and calls 11486e8949aSgrange.Fn tc_init 115*339a7e8cSchelohawith a pointer to that structure as argument. 116*339a7e8cScheloha.Sh CONTEXT 117*339a7e8cScheloha.Fn tc_init 118*339a7e8cSchelohamay only be called during autoconf. 11986e8949aSgrange.Sh CODE REFERENCES 120*339a7e8cScheloha.Pa sys/kern/kern_tc.c 12186e8949aSgrange.Sh SEE ALSO 12286e8949aSgrange.Xr amdpm 4 , 12386e8949aSgrange.Xr gscpm 4 , 12486e8949aSgrange.Xr ichpcib 4 , 12586410eadSshadchin.Xr viapm 4 , 12686e8949aSgrange.Xr hz 9 , 12786e8949aSgrange.Xr microtime 9 12886e8949aSgrange.Rs 12986e8949aSgrange.%A Poul-Henning Kamp 13086e8949aSgrange.%T Timecounter: Efficient and precise timekeeping in SMP kernels 13186e8949aSgrange.%J The FreeBSD Project 132*339a7e8cScheloha.%D 2002 133*339a7e8cScheloha.%U https://papers.freebsd.org/2002/phk-timecounters.files/timecounter.pdf 13486e8949aSgrange.Re 13586e8949aSgrange.Sh HISTORY 136*339a7e8cSchelohaThe timecounting subsystem first appeared in 137*339a7e8cScheloha.Fx 3.0 . 138*339a7e8cSchelohaIt was ported to 13986e8949aSgrange.Ox 3.6 . 140*339a7e8cScheloha.Sh AUTHORS 141*339a7e8cScheloha.An Poul-Henning Kamp 142