xref: /illumos-gate/usr/src/uts/i86pc/sys/tsc.h (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2020 Joyent, Inc.
14  */
15 
16 #ifndef _TSC_H
17 #define	_TSC_H
18 
19 #ifndef _ASM
20 #include <sys/linker_set.h>
21 #include <sys/types.h>
22 #endif
23 
24 /*
25  * flags to patch tsc_read routine.
26  */
27 #define	TSC_NONE		0x0
28 #define	TSC_RDTSC_CPUID		0x1
29 /* formerly TSC_RDTSC_MFENCE	0x2 */
30 #define	TSC_RDTSC_LFENCE	0x3
31 #define	TSC_TSCP		0x4
32 
33 #ifndef _ASM
34 
35 /*
36  * To register a TSC calibration source, a tsc_calibrate_t instance
37  * should be created for the source, and then use
38  * `TSC_CALIBRATION_SOURCE(<name_of_tsc_calibrate_t_instance_for_source>);`
39  * to include it in the list of known sources.
40  */
41 typedef struct tsc_calibrate {
42 	/*
43 	 * A descriptive name for the source. While this is mostly for the
44 	 * benefit of an operator, it may also be used to explicitly pick
45 	 * a specific source (vs. trying sources in order of preference).
46 	 * Each name should be unique (ignoring case).
47 	 */
48 	const char	*tscc_source;
49 
50 	/*
51 	 * A preference value for this source. These values are largely
52 	 * arbitrary and are just to impose an order the sequence of
53 	 * sources to try (higher values of preference are tried before
54 	 * lower values of preference).
55 	 *
56 	 * Typically, any hypervisor provided sources will be preferred
57 	 * over hardware provided sources (i.e. cpuid), and higher precision
58 	 * hardware counters will be preferred over lower precision counters
59 	 * (e.g. HPET over PIT).
60 	 */
61 	uint_t		tscc_preference;
62 
63 	/*
64 	 * The function that attempts calibration of the TSC. If the source
65 	 * cannot calibrate the TSC for any reason (e.g. the calibration source
66 	 * is not present or not supported on this machine), it should return
67 	 * B_FALSE.
68 	 *
69 	 * If the source is successful in measuring the TSC frequency, it
70 	 * should write the frequency of the TSC (in Hz) into the argument
71 	 * passed, e.g.
72 	 *
73 	 * boolean_t
74 	 * my_source(uint64_t *freq)
75 	 * {
76 	 *	...
77 	 *	*freq = measured_tsc_frequency;
78 	 *	return (B_TRUE);
79 	 * }
80 	 *
81 	 */
82 	boolean_t	(*tscc_calibrate)(uint64_t *);
83 } tsc_calibrate_t;
84 #define	TSC_CALIBRATION_SOURCE(x) DATA_SET(tsc_calibration_set, x)
85 
86 uint64_t tsc_calibrate(void);
87 uint64_t tsc_get_freq(void);
88 
89 #endif /* _ASM */
90 
91 #endif /* _TSC_H */
92