1.\" $OpenBSD: tc_init.9,v 1.9 2019/09/13 15:47:47 schwarze Exp $ 2.\" 3.\" Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org> 4.\" 5.\" Permission to use, copy, modify, and distribute this software for any 6.\" purpose with or without fee is hereby granted, provided that the above 7.\" copyright notice and this permission notice appear in all copies. 8.\" 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16.\" 17.Dd $Mdocdate: September 13 2019 $ 18.Dt TC_INIT 9 19.Os 20.Sh NAME 21.Nm tc_init 22.Nd machine-independent binary timescale 23.Sh SYNOPSIS 24.In sys/timetc.h 25.Ft void 26.Fn tc_init "struct timecounter *tc" 27.Sh DESCRIPTION 28The timecounter interface is a machine-independent implementation 29of a binary timescale using whatever hardware support is at hand 30for tracking time. 31.Pp 32A timecounter is a binary counter which has two properties: 33.Bl -bullet -offset indent 34.It 35it runs at a fixed, known frequency 36.It 37it has sufficient bits to not roll over in less than approximately 38max(2 msec, 2/HZ seconds) (the value 2 here is really 1 + delta, for some 39indeterminate value of delta) 40.El 41.Pp 42The interface between the hardware which implements a timecounter and the 43machine-independent code which uses this to keep track of time is a 44.Va timecounter 45structure: 46.Bd -literal -offset indent 47struct timecounter { 48 timecounter_get_t *tc_get_timecount; 49 timecounter_pps_t *tc_poll_pps; 50 u_int tc_counter_mask; 51 u_int64_t tc_frequency; 52 char *tc_name; 53 int tc_quality; 54 void *tc_priv; 55 struct timecounter *tc_next; 56} 57.Ed 58.Pp 59The fields of the 60.Va timecounter 61structure are described below. 62.Bl -tag -width indent 63.It Ft u_int Fn (*tc_get_timecount) "struct timecounter *" 64This function reads the counter. 65It is not required to mask any unimplemented bits out, as long as they 66are constant. 67.It Ft void Fn (*tc_poll_pps) "struct timecounter *" 68This function is optional and can be set to NULL. 69It will be called whenever the timecounter is rewound, and is intended 70to check for PPS events. 71Normal hardware does not need it but timecounters which latch PPS in 72hardware do. 73.It Va tc_counter_mask 74This mask should mask off any unimplemented bits. 75.It Va tc_frequency 76Frequency of the counter in Hz. 77.It Va tc_name 78Name of the timecounter. 79Can be any null-terminated string. 80.It Va tc_quality 81Used to determine if this timecounter is better than another timecounter \- 82higher means better. 83If this field is negative, the counter is only used at explicit request. 84.It Va tc_priv 85Pointer to the timecounter's private parts. 86.It Va tc_next 87For internal use. 88.El 89.Pp 90To register a new timecounter, 91the hardware device driver should fill a 92.Va timecounter 93structure with appropriate values and call the 94.Fn tc_init 95function, giving a pointer to the structure as a 96.Fa tc 97parameter. 98.Sh CODE REFERENCES 99The timecounter framework is implemented in the file 100.Pa sys/kern/kern_tc.c . 101.Sh SEE ALSO 102.Xr amdpm 4 , 103.Xr elansc 4 , 104.Xr gscpm 4 , 105.Xr ichpcib 4 , 106.Xr viapm 4 , 107.Xr hz 9 , 108.Xr microtime 9 109.Rs 110.%A Poul-Henning Kamp 111.%T Timecounter: Efficient and precise timekeeping in SMP kernels 112.%J The FreeBSD Project 113.%U http://phk.freebsd.dk/pubs/timecounter.pdf 114.Re 115.Sh HISTORY 116The timecounter interface first appeared in 117.Ox 3.6 . 118