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