xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 7c2ccc507bd44d17227930181f937b2066565349)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2103a8feaSLen Brown /*
3103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
434041551SLen Brown  * on modern Intel and AMD processors.
5103a8feaSLen Brown  *
6144b44b1SLen Brown  * Copyright (c) 2013 Intel Corporation.
7103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
8103a8feaSLen Brown  */
9103a8feaSLen Brown 
1088c3281fSLen Brown #define _GNU_SOURCE
11b731f311SJosh Triplett #include MSRHEADER
12869ce69eSLen Brown #include INTEL_FAMILY_HEADER
1395aebc44SJosh Triplett #include <stdarg.h>
14103a8feaSLen Brown #include <stdio.h>
15b2c95d90SJosh Triplett #include <err.h>
16103a8feaSLen Brown #include <unistd.h>
17103a8feaSLen Brown #include <sys/types.h>
18103a8feaSLen Brown #include <sys/wait.h>
19103a8feaSLen Brown #include <sys/stat.h>
20b9ad8ee0SLen Brown #include <sys/select.h>
21103a8feaSLen Brown #include <sys/resource.h>
22103a8feaSLen Brown #include <fcntl.h>
23103a8feaSLen Brown #include <signal.h>
24103a8feaSLen Brown #include <sys/time.h>
25103a8feaSLen Brown #include <stdlib.h>
26d8af6f5fSLen Brown #include <getopt.h>
27103a8feaSLen Brown #include <dirent.h>
28103a8feaSLen Brown #include <string.h>
29103a8feaSLen Brown #include <ctype.h>
3088c3281fSLen Brown #include <sched.h>
312a0609c0SLen Brown #include <time.h>
322b92865eSJosh Triplett #include <cpuid.h>
33fcaa681cSLen Brown #include <sys/capability.h>
3498481e79SLen Brown #include <errno.h>
359392bd98SCalvin Walton #include <math.h>
36103a8feaSLen Brown 
37103a8feaSLen Brown char *proc_stat = "/proc/stat";
38b7d8c148SLen Brown FILE *outf;
3936229897SLen Brown int *fd_percpu;
40b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0};
4147936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0};
42023fe0acSChen Yu unsigned int num_iterations;
43d8af6f5fSLen Brown unsigned int debug;
4496e47158SLen Brown unsigned int quiet;
453f44a5c6SLen Brown unsigned int shown;
460de6c0dfSLen Brown unsigned int sums_need_wide_columns;
47d8af6f5fSLen Brown unsigned int rapl_joules;
48d8af6f5fSLen Brown unsigned int summary_only;
49c8ade361SLen Brown unsigned int list_header_only;
50d8af6f5fSLen Brown unsigned int dump_only;
51103a8feaSLen Brown unsigned int do_snb_cstates;
52fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
53144b44b1SLen Brown unsigned int do_slm_cstates;
54144b44b1SLen Brown unsigned int use_c1_residency_msr;
55103a8feaSLen Brown unsigned int has_aperf;
56889facbeSLen Brown unsigned int has_epb;
575a63426eSLen Brown unsigned int do_irtl_snb;
585a63426eSLen Brown unsigned int do_irtl_hsw;
59fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
60103a8feaSLen Brown unsigned int genuine_intel;
6134041551SLen Brown unsigned int authentic_amd;
62c1c10cc7SPu Wen unsigned int hygon_genuine;
6334041551SLen Brown unsigned int max_level, max_extended_level;
64103a8feaSLen Brown unsigned int has_invariant_tsc;
65d7899447SLen Brown unsigned int do_nhm_platform_info;
66cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
67b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
68103a8feaSLen Brown double bclk;
69a2b7b749SLen Brown double base_hz;
7021ed5574SLen Brown unsigned int has_base_hz;
71a2b7b749SLen Brown double tsc_tweak = 1.0;
72c98d5d94SLen Brown unsigned int show_pkg_only;
73c98d5d94SLen Brown unsigned int show_core_only;
74c98d5d94SLen Brown char *output_buffer, *outp;
75889facbeSLen Brown unsigned int do_rapl;
76889facbeSLen Brown unsigned int do_dts;
77889facbeSLen Brown unsigned int do_ptm;
78fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
79be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
80be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
8127d47356SLen Brown unsigned int gfx_cur_mhz;
82889facbeSLen Brown unsigned int tcc_activation_temp;
83889facbeSLen Brown unsigned int tcc_activation_temp_override;
8440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
8540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
86889facbeSLen Brown double rapl_joule_counter_range;
873a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
88ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
893a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
903a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
918a5bdf41SLen Brown unsigned int crystal_hz;
928a5bdf41SLen Brown unsigned long long tsc_hz;
937ce7d5deSPrarit Bhargava int base_cpu;
9421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
957f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
967f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
977f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
987f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
997f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1007f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
10133148d67SLen Brown unsigned int has_misc_feature_control;
1024c2122d4SLen Brown unsigned int first_counter_read = 1;
103c026c236SArtem Bityutskiy int ignore_stdin;
104889facbeSLen Brown 
105889facbeSLen Brown #define RAPL_PKG		(1 << 0)
106e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
107e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
108e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
109e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
110e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
111e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
112e6f9bb3cSLen Brown 
113889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
114e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
115e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
116e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
117e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1180b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1190b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
120e6f9bb3cSLen Brown 
1219148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
122e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1230b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
124e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
125e6f9bb3cSLen Brown 
1260b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
127e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
128e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
129e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1309148494cSJacob Pan 
1319148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1329148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1339392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY	(1 << 10)
1349392bd98SCalvin Walton 					/* Indicates cores energy collection is per-core,
1359392bd98SCalvin Walton 					 * not per-package. */
1369392bd98SCalvin Walton #define RAPL_AMD_F17H		(1 << 11)
1379392bd98SCalvin Walton 					/* 0xc0010299 MSR_RAPL_PWR_UNIT */
1389392bd98SCalvin Walton 					/* 0xc001029a MSR_CORE_ENERGY_STAT */
1399392bd98SCalvin Walton 					/* 0xc001029b MSR_PKG_ENERGY_STAT */
1409148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
141889facbeSLen Brown #define	TJMAX_DEFAULT	100
142889facbeSLen Brown 
1439392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */
1449392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT	0xc0010299
1459392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT	0xc001029a
1469392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT	0xc001029b
1479392bd98SCalvin Walton 
148889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
149103a8feaSLen Brown 
150388e9c81SLen Brown /*
151388e9c81SLen Brown  * buffer size used by sscanf() for added column names
152388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
153388e9c81SLen Brown  */
154388e9c81SLen Brown #define	NAME_BYTES 20
155495c7654SLen Brown #define PATH_BYTES 128
156388e9c81SLen Brown 
157103a8feaSLen Brown int backwards_count;
158103a8feaSLen Brown char *progname;
159103a8feaSLen Brown 
1601ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1611ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1621ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
1630748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
1640748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
1650e2d8f05SLen Brown #define BITMASK_SIZE 32
166c98d5d94SLen Brown 
167c98d5d94SLen Brown struct thread_data {
168f4fdf2b4SLen Brown 	struct timeval tv_begin;
169f4fdf2b4SLen Brown 	struct timeval tv_end;
170d4794f25SYazen Ghannam 	struct timeval tv_delta;
171c98d5d94SLen Brown 	unsigned long long tsc;
172c98d5d94SLen Brown 	unsigned long long aperf;
173c98d5d94SLen Brown 	unsigned long long mperf;
174144b44b1SLen Brown 	unsigned long long c1;
1750de6c0dfSLen Brown 	unsigned long long  irq_count;
1761ed51011SLen Brown 	unsigned int smi_count;
177c98d5d94SLen Brown 	unsigned int cpu_id;
1784c2122d4SLen Brown 	unsigned int apic_id;
1794c2122d4SLen Brown 	unsigned int x2apic_id;
180c98d5d94SLen Brown 	unsigned int flags;
181c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
182c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
1830748eaf0SLen Brown 	unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
184c98d5d94SLen Brown } *thread_even, *thread_odd;
185c98d5d94SLen Brown 
186c98d5d94SLen Brown struct core_data {
187c98d5d94SLen Brown 	unsigned long long c3;
188c98d5d94SLen Brown 	unsigned long long c6;
189c98d5d94SLen Brown 	unsigned long long c7;
1900539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
191889facbeSLen Brown 	unsigned int core_temp_c;
1929392bd98SCalvin Walton 	unsigned int core_energy;	/* MSR_CORE_ENERGY_STAT */
193c98d5d94SLen Brown 	unsigned int core_id;
194678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
195c98d5d94SLen Brown } *core_even, *core_odd;
196c98d5d94SLen Brown 
197c98d5d94SLen Brown struct pkg_data {
198c98d5d94SLen Brown 	unsigned long long pc2;
199c98d5d94SLen Brown 	unsigned long long pc3;
200c98d5d94SLen Brown 	unsigned long long pc6;
201c98d5d94SLen Brown 	unsigned long long pc7;
202ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
203ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
204ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
205be0e54c4SLen Brown 	unsigned long long cpu_lpi;
206be0e54c4SLen Brown 	unsigned long long sys_lpi;
2070b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
2080b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
2090b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
2100b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
2119185e988SLen Brown 	long long gfx_rc6_ms;
21227d47356SLen Brown 	unsigned int gfx_mhz;
213c98d5d94SLen Brown 	unsigned int package_id;
214*7c2ccc50SChen Yu 	unsigned long long energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
215*7c2ccc50SChen Yu 	unsigned long long energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
216*7c2ccc50SChen Yu 	unsigned long long energy_cores;	/* MSR_PP0_ENERGY_STATUS */
217*7c2ccc50SChen Yu 	unsigned long long energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
218*7c2ccc50SChen Yu 	unsigned long long rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
219*7c2ccc50SChen Yu 	unsigned long long rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
220889facbeSLen Brown 	unsigned int pkg_temp_c;
221678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
222c98d5d94SLen Brown } *package_even, *package_odd;
223c98d5d94SLen Brown 
224c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
225c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
226c98d5d94SLen Brown 
22740f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
22840f5cfe7SPrarit Bhargava 	((thread_base) +						      \
22940f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
23040f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
23140f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
23240f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
23340f5cfe7SPrarit Bhargava 	 (thread_no))
23440f5cfe7SPrarit Bhargava 
23540f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
23640f5cfe7SPrarit Bhargava 	((core_base) +							\
23740f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
23840f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
23940f5cfe7SPrarit Bhargava 	 (core_no))
24040f5cfe7SPrarit Bhargava 
24140f5cfe7SPrarit Bhargava 
242c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
243c98d5d94SLen Brown 
244388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
24541618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
246388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
247388e9c81SLen Brown 
248388e9c81SLen Brown struct msr_counter {
249388e9c81SLen Brown 	unsigned int msr_num;
250388e9c81SLen Brown 	char name[NAME_BYTES];
251495c7654SLen Brown 	char path[PATH_BYTES];
252388e9c81SLen Brown 	unsigned int width;
253388e9c81SLen Brown 	enum counter_type type;
254388e9c81SLen Brown 	enum counter_format format;
255388e9c81SLen Brown 	struct msr_counter *next;
256812db3f7SLen Brown 	unsigned int flags;
257812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
258812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
25941618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
260388e9c81SLen Brown };
261388e9c81SLen Brown 
262388e9c81SLen Brown struct sys_counters {
263678a3bd1SLen Brown 	unsigned int added_thread_counters;
264678a3bd1SLen Brown 	unsigned int added_core_counters;
265678a3bd1SLen Brown 	unsigned int added_package_counters;
266388e9c81SLen Brown 	struct msr_counter *tp;
267388e9c81SLen Brown 	struct msr_counter *cp;
268388e9c81SLen Brown 	struct msr_counter *pp;
269388e9c81SLen Brown } sys;
270388e9c81SLen Brown 
271c98d5d94SLen Brown struct system_summary {
272c98d5d94SLen Brown 	struct thread_data threads;
273c98d5d94SLen Brown 	struct core_data cores;
274c98d5d94SLen Brown 	struct pkg_data packages;
275388e9c81SLen Brown } average;
276c98d5d94SLen Brown 
2770e2d8f05SLen Brown struct cpu_topology {
2780e2d8f05SLen Brown 	int physical_package_id;
2796de68fe1SLen Brown 	int die_id;
2800e2d8f05SLen Brown 	int logical_cpu_id;
281ef605741SPrarit Bhargava 	int physical_node_id;
282ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
2830e2d8f05SLen Brown 	int physical_core_id;
2848cb48b32SPrarit Bhargava 	int thread_id;
2850e2d8f05SLen Brown 	cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
2860e2d8f05SLen Brown } *cpus;
287c98d5d94SLen Brown 
288c98d5d94SLen Brown struct topo_params {
289c98d5d94SLen Brown 	int num_packages;
2906de68fe1SLen Brown 	int num_die;
291103a8feaSLen Brown 	int num_cpus;
292c98d5d94SLen Brown 	int num_cores;
293c98d5d94SLen Brown 	int max_cpu_num;
294ef605741SPrarit Bhargava 	int max_node_num;
29570a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
29670a9c6e8SPrarit Bhargava 	int cores_per_node;
29770a9c6e8SPrarit Bhargava 	int threads_per_core;
298c98d5d94SLen Brown } topo;
299103a8feaSLen Brown 
300c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
301103a8feaSLen Brown 
302562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
303562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
304562a2d37SLen Brown 
305c98d5d94SLen Brown void setup_all_buffers(void);
306103a8feaSLen Brown 
3071f81c5efSLen Brown char *sys_lpi_file;
3081f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
3091f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
3101f81c5efSLen Brown 
311c98d5d94SLen Brown int cpu_is_not_present(int cpu)
312d15cf7c1SLen Brown {
313c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
314c98d5d94SLen Brown }
315c98d5d94SLen Brown /*
316c98d5d94SLen Brown  * run func(thread, core, package) in topology order
317c98d5d94SLen Brown  * skip non-present cpus
318c98d5d94SLen Brown  */
319c98d5d94SLen Brown 
320c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
321c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
322c98d5d94SLen Brown {
32340f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
324c98d5d94SLen Brown 
325c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
326df2f677dSLen Brown 		for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
32770a9c6e8SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
328c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
32970a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
330c98d5d94SLen Brown 					struct thread_data *t;
331c98d5d94SLen Brown 					struct core_data *c;
332c98d5d94SLen Brown 					struct pkg_data *p;
333c98d5d94SLen Brown 
33440f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
33540f5cfe7SPrarit Bhargava 						       core_no, node_no,
33640f5cfe7SPrarit Bhargava 						       pkg_no);
337c98d5d94SLen Brown 
338c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
339c98d5d94SLen Brown 						continue;
340c98d5d94SLen Brown 
34140f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
34240f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
343c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
344c98d5d94SLen Brown 
345c98d5d94SLen Brown 					retval = func(t, c, p);
346c98d5d94SLen Brown 					if (retval)
347c98d5d94SLen Brown 						return retval;
348c98d5d94SLen Brown 				}
349c98d5d94SLen Brown 			}
350c98d5d94SLen Brown 		}
35140f5cfe7SPrarit Bhargava 	}
352d15cf7c1SLen Brown 	return 0;
353d15cf7c1SLen Brown }
354d15cf7c1SLen Brown 
35588c3281fSLen Brown int cpu_migrate(int cpu)
35688c3281fSLen Brown {
357c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
358c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
359c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
36088c3281fSLen Brown 		return -1;
36188c3281fSLen Brown 	else
36288c3281fSLen Brown 		return 0;
36388c3281fSLen Brown }
36436229897SLen Brown int get_msr_fd(int cpu)
365103a8feaSLen Brown {
366103a8feaSLen Brown 	char pathname[32];
367103a8feaSLen Brown 	int fd;
368103a8feaSLen Brown 
36936229897SLen Brown 	fd = fd_percpu[cpu];
37036229897SLen Brown 
37136229897SLen Brown 	if (fd)
37236229897SLen Brown 		return fd;
37336229897SLen Brown 
374103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
375103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
37615aaa346SLen Brown 	if (fd < 0)
37798481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
378103a8feaSLen Brown 
37936229897SLen Brown 	fd_percpu[cpu] = fd;
38036229897SLen Brown 
38136229897SLen Brown 	return fd;
38236229897SLen Brown }
38336229897SLen Brown 
38436229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
38536229897SLen Brown {
38636229897SLen Brown 	ssize_t retval;
38736229897SLen Brown 
38836229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
38915aaa346SLen Brown 
39098481e79SLen Brown 	if (retval != sizeof *msr)
391cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
39215aaa346SLen Brown 
39315aaa346SLen Brown 	return 0;
394103a8feaSLen Brown }
395103a8feaSLen Brown 
396fc04cc67SLen Brown /*
397bdd5ae3aSLen Brown  * This list matches the column headers, except
398bdd5ae3aSLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
399bdd5ae3aSLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
400bdd5ae3aSLen Brown  *    matching on them for --show and --hide.
401fc04cc67SLen Brown  */
402812db3f7SLen Brown struct msr_counter bic[] = {
4033f44a5c6SLen Brown 	{ 0x0, "usec" },
4043f44a5c6SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
405812db3f7SLen Brown 	{ 0x0, "Package" },
406bdd5ae3aSLen Brown 	{ 0x0, "Node" },
407812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
408bdd5ae3aSLen Brown 	{ 0x0, "Busy%" },
409812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
410812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
411812db3f7SLen Brown 	{ 0x0, "IRQ" },
412495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
413bdd5ae3aSLen Brown 	{ 0x0, "sysfs" },
414812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
415812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
416812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
417812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
418812db3f7SLen Brown 	{ 0x0, "ThreadC" },
419812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
420812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
421812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
422812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
423812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
424812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
425812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
426812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
427812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
4280f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
4290f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
4304bd1f8f2SLen Brown 	{ 0x0, "Pk%pc10" },
431be0e54c4SLen Brown 	{ 0x0, "CPU%LPI" },
432be0e54c4SLen Brown 	{ 0x0, "SYS%LPI" },
433812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
434812db3f7SLen Brown 	{ 0x0, "CorWatt" },
435812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
436812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
437812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
438812db3f7SLen Brown 	{ 0x0, "PKG_%" },
439812db3f7SLen Brown 	{ 0x0, "RAM_%" },
440812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
441812db3f7SLen Brown 	{ 0x0, "Cor_J" },
442812db3f7SLen Brown 	{ 0x0, "GFX_J" },
443812db3f7SLen Brown 	{ 0x0, "RAM_J" },
4440539ba11SLen Brown 	{ 0x0, "Mod%c6" },
445a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
446a99d8730SLen Brown 	{ 0x0, "Any%C0" },
447a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
448a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
449bdd5ae3aSLen Brown 	{ 0x0, "Core" },
450bdd5ae3aSLen Brown 	{ 0x0, "CPU" },
4514c2122d4SLen Brown 	{ 0x0, "APIC" },
4524c2122d4SLen Brown 	{ 0x0, "X2APIC" },
4536de68fe1SLen Brown 	{ 0x0, "Die" },
454812db3f7SLen Brown };
455812db3f7SLen Brown 
456812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
4573f44a5c6SLen Brown #define	BIC_USEC	(1ULL << 0)
4583f44a5c6SLen Brown #define	BIC_TOD		(1ULL << 1)
4593f44a5c6SLen Brown #define	BIC_Package	(1ULL << 2)
460bdd5ae3aSLen Brown #define	BIC_Node	(1ULL << 3)
461bdd5ae3aSLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
462bdd5ae3aSLen Brown #define	BIC_Busy	(1ULL << 5)
463bdd5ae3aSLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
464bdd5ae3aSLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
465bdd5ae3aSLen Brown #define	BIC_IRQ		(1ULL << 8)
466bdd5ae3aSLen Brown #define	BIC_SMI		(1ULL << 9)
467bdd5ae3aSLen Brown #define	BIC_sysfs	(1ULL << 10)
468bdd5ae3aSLen Brown #define	BIC_CPU_c1	(1ULL << 11)
469bdd5ae3aSLen Brown #define	BIC_CPU_c3	(1ULL << 12)
470bdd5ae3aSLen Brown #define	BIC_CPU_c6	(1ULL << 13)
471bdd5ae3aSLen Brown #define	BIC_CPU_c7	(1ULL << 14)
472bdd5ae3aSLen Brown #define	BIC_ThreadC	(1ULL << 15)
473bdd5ae3aSLen Brown #define	BIC_CoreTmp	(1ULL << 16)
474bdd5ae3aSLen Brown #define	BIC_CoreCnt	(1ULL << 17)
475bdd5ae3aSLen Brown #define	BIC_PkgTmp	(1ULL << 18)
476bdd5ae3aSLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
477bdd5ae3aSLen Brown #define	BIC_GFXMHz	(1ULL << 20)
478bdd5ae3aSLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
479bdd5ae3aSLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
480bdd5ae3aSLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
481bdd5ae3aSLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
482bdd5ae3aSLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
483bdd5ae3aSLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
484bdd5ae3aSLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
485bdd5ae3aSLen Brown #define BIC_CPU_LPI	(1ULL << 28)
486bdd5ae3aSLen Brown #define BIC_SYS_LPI	(1ULL << 29)
487bdd5ae3aSLen Brown #define	BIC_PkgWatt	(1ULL << 30)
488bdd5ae3aSLen Brown #define	BIC_CorWatt	(1ULL << 31)
489bdd5ae3aSLen Brown #define	BIC_GFXWatt	(1ULL << 32)
490bdd5ae3aSLen Brown #define	BIC_PkgCnt	(1ULL << 33)
491bdd5ae3aSLen Brown #define	BIC_RAMWatt	(1ULL << 34)
492bdd5ae3aSLen Brown #define	BIC_PKG__	(1ULL << 35)
493bdd5ae3aSLen Brown #define	BIC_RAM__	(1ULL << 36)
494bdd5ae3aSLen Brown #define	BIC_Pkg_J	(1ULL << 37)
495bdd5ae3aSLen Brown #define	BIC_Cor_J	(1ULL << 38)
496bdd5ae3aSLen Brown #define	BIC_GFX_J	(1ULL << 39)
497bdd5ae3aSLen Brown #define	BIC_RAM_J	(1ULL << 40)
498bdd5ae3aSLen Brown #define	BIC_Mod_c6	(1ULL << 41)
499bdd5ae3aSLen Brown #define	BIC_Totl_c0	(1ULL << 42)
500bdd5ae3aSLen Brown #define	BIC_Any_c0	(1ULL << 43)
501bdd5ae3aSLen Brown #define	BIC_GFX_c0	(1ULL << 44)
502bdd5ae3aSLen Brown #define	BIC_CPUGFX	(1ULL << 45)
503bdd5ae3aSLen Brown #define	BIC_Core	(1ULL << 46)
504bdd5ae3aSLen Brown #define	BIC_CPU		(1ULL << 47)
5054c2122d4SLen Brown #define	BIC_APIC	(1ULL << 48)
5064c2122d4SLen Brown #define	BIC_X2APIC	(1ULL << 49)
5076de68fe1SLen Brown #define	BIC_Die		(1ULL << 50)
508812db3f7SLen Brown 
5094c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
5103f44a5c6SLen Brown 
5113f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
5124c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
513812db3f7SLen Brown 
514812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
5151e9042b9SSrinivas Pandruvada #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
5163f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
517812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
5180f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
519812db3f7SLen Brown 
5203f44a5c6SLen Brown 
521dd778a5eSLen Brown #define MAX_DEFERRED 16
522dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
523dd778a5eSLen Brown int deferred_skip_index;
524dd778a5eSLen Brown 
525dd778a5eSLen Brown /*
526dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
527dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
528dd778a5eSLen Brown  */
529dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
530dd778a5eSLen Brown 
531dd778a5eSLen Brown void help(void)
532dd778a5eSLen Brown {
533dd778a5eSLen Brown 	fprintf(outf,
534dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
535dd778a5eSLen Brown 	"\n"
536dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
537dd778a5eSLen Brown 	"when COMMAND completes.\n"
538dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
539dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
540cc481650SNathan Ciobanu 	"  -a, --add	add a counter\n"
541dd778a5eSLen Brown 	"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
542cc481650SNathan Ciobanu 	"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
543dd778a5eSLen Brown 	"		  {core | package | j,k,l..m,n-p }\n"
5449ce80578SNathan Ciobanu 	"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
5459ce80578SNathan Ciobanu 	"  -D, --Dump	displays the raw counter values\n"
5469ce80578SNathan Ciobanu 	"  -e, --enable	[all | column]\n"
5479ce80578SNathan Ciobanu 	"		shows all or the specified disabled column\n"
5489ce80578SNathan Ciobanu 	"  -H, --hide [column|column,column,...]\n"
5499ce80578SNathan Ciobanu 	"		hide the specified column(s)\n"
550cc481650SNathan Ciobanu 	"  -i, --interval sec.subsec\n"
551cc481650SNathan Ciobanu 	"		Override default 5-second measurement interval\n"
5529ce80578SNathan Ciobanu 	"  -J, --Joules	displays energy in Joules instead of Watts\n"
553cc481650SNathan Ciobanu 	"  -l, --list	list column headers only\n"
554cc481650SNathan Ciobanu 	"  -n, --num_iterations num\n"
555cc481650SNathan Ciobanu 	"		number of the measurement iterations\n"
556cc481650SNathan Ciobanu 	"  -o, --out file\n"
557cc481650SNathan Ciobanu 	"		create or truncate \"file\" for all output\n"
558cc481650SNathan Ciobanu 	"  -q, --quiet	skip decoding system configuration header\n"
5599ce80578SNathan Ciobanu 	"  -s, --show [column|column,column,...]\n"
5609ce80578SNathan Ciobanu 	"		show only the specified column(s)\n"
5619ce80578SNathan Ciobanu 	"  -S, --Summary\n"
5629ce80578SNathan Ciobanu 	"		limits output to 1-line system summary per interval\n"
5639ce80578SNathan Ciobanu 	"  -T, --TCC temperature\n"
5649ce80578SNathan Ciobanu 	"		sets the Thermal Control Circuit temperature in\n"
5659ce80578SNathan Ciobanu 	"		  degrees Celsius\n"
566cc481650SNathan Ciobanu 	"  -h, --help	print this help message\n"
567cc481650SNathan Ciobanu 	"  -v, --version	print version information\n"
568dd778a5eSLen Brown 	"\n"
569dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
570dd778a5eSLen Brown }
571dd778a5eSLen Brown 
572812db3f7SLen Brown /*
573812db3f7SLen Brown  * bic_lookup
574812db3f7SLen Brown  * for all the strings in comma separate name_list,
575812db3f7SLen Brown  * set the approprate bit in return value.
576812db3f7SLen Brown  */
577dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
578812db3f7SLen Brown {
579812db3f7SLen Brown 	int i;
580812db3f7SLen Brown 	unsigned long long retval = 0;
581812db3f7SLen Brown 
582812db3f7SLen Brown 	while (name_list) {
583812db3f7SLen Brown 		char *comma;
584812db3f7SLen Brown 
585812db3f7SLen Brown 		comma = strchr(name_list, ',');
586812db3f7SLen Brown 
587812db3f7SLen Brown 		if (comma)
588812db3f7SLen Brown 			*comma = '\0';
589812db3f7SLen Brown 
5903f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5913f44a5c6SLen Brown 			return ~0;
5923f44a5c6SLen Brown 
593812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
594812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
595812db3f7SLen Brown 				retval |= (1ULL << i);
596812db3f7SLen Brown 				break;
597812db3f7SLen Brown 			}
598812db3f7SLen Brown 		}
599812db3f7SLen Brown 		if (i == MAX_BIC) {
600dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
601812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
602812db3f7SLen Brown 				exit(-1);
603812db3f7SLen Brown 			}
604dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
605dd778a5eSLen Brown 			if (debug)
606dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
607dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
608dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
609dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
610dd778a5eSLen Brown 				help();
611dd778a5eSLen Brown 				exit(1);
612dd778a5eSLen Brown 			}
613dd778a5eSLen Brown 		}
614812db3f7SLen Brown 
615812db3f7SLen Brown 		name_list = comma;
616812db3f7SLen Brown 		if (name_list)
617812db3f7SLen Brown 			name_list++;
618812db3f7SLen Brown 
619812db3f7SLen Brown 	}
620812db3f7SLen Brown 	return retval;
621812db3f7SLen Brown }
622fc04cc67SLen Brown 
623dd778a5eSLen Brown 
624c8ade361SLen Brown void print_header(char *delim)
625103a8feaSLen Brown {
626388e9c81SLen Brown 	struct msr_counter *mp;
6276168c2e0SLen Brown 	int printed = 0;
628388e9c81SLen Brown 
6293f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
6303f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
6313f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
6323f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
633812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
6346168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
6356de68fe1SLen Brown 	if (DO_BIC(BIC_Die))
6366de68fe1SLen Brown 		outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
63701235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
63801235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
639812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
6406168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
641812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
6426168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
6434c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
6444c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
6454c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
6464c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
647812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
6486168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
649812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
6506168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
651812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
6526168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
653812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
6546168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
6551cc21f7bSLen Brown 
6560de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
6570de6c0dfSLen Brown 		if (sums_need_wide_columns)
6586168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
6590de6c0dfSLen Brown 		else
6606168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
6610de6c0dfSLen Brown 	}
6620de6c0dfSLen Brown 
663812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
6646168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
6651cc21f7bSLen Brown 
666388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
667dd778a5eSLen Brown 
668388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
669388e9c81SLen Brown 			if (mp->width == 64)
670dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
671388e9c81SLen Brown 			else
672dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
673388e9c81SLen Brown 		} else {
6740de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
675dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
6760de6c0dfSLen Brown 			else
677dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
678388e9c81SLen Brown 		}
679388e9c81SLen Brown 	}
680388e9c81SLen Brown 
68141618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
6826168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
683562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
6846168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
685812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6866168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
687812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6886168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
689678a3bd1SLen Brown 
6900539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6916168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
692678a3bd1SLen Brown 
693812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6946168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
695388e9c81SLen Brown 
6969392bd98SCalvin Walton 	if (do_rapl && !rapl_joules) {
6979392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
6989392bd98SCalvin Walton 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
6999392bd98SCalvin Walton 	} else if (do_rapl && rapl_joules) {
7009392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
7019392bd98SCalvin Walton 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
7029392bd98SCalvin Walton 	}
7039392bd98SCalvin Walton 
704388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
705388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
706388e9c81SLen Brown 			if (mp->width == 64)
707c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
708388e9c81SLen Brown 			else
709c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
710388e9c81SLen Brown 		} else {
7110de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7120de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7130de6c0dfSLen Brown 			else
714c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
715388e9c81SLen Brown 		}
716388e9c81SLen Brown 	}
717388e9c81SLen Brown 
718812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
7196168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
720889facbeSLen Brown 
721812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
7226168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
723fdf676e5SLen Brown 
724812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
7256168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
72627d47356SLen Brown 
727a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
7286168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
729a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
7306168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
731a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
7326168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
733a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
7346168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
7350b2bb692SLen Brown 
7360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
7376168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
7380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
7396168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
7400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
7416168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
7420f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
7436168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
7440f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
7456168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
7460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
7476168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
7480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
7496168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
750be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
751be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
752be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
753be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
754103a8feaSLen Brown 
7555c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
756812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
7576168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
7589392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
7596168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
760812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
7616168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
762812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
7636168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
764812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7656168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
766812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7676168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
768d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
769812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
7706168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
7719392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
7726168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
773812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
7746168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
775812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
7766168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
777812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7786168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
779812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7806168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
7815c56be9aSDirk Brandewie 	}
782388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
783388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
784388e9c81SLen Brown 			if (mp->width == 64)
785c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
786388e9c81SLen Brown 			else
787c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
788388e9c81SLen Brown 		} else {
7890de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7900de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7910de6c0dfSLen Brown 			else
792c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
793388e9c81SLen Brown 		}
794388e9c81SLen Brown 	}
795388e9c81SLen Brown 
796c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
797103a8feaSLen Brown }
798103a8feaSLen Brown 
799c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
800c98d5d94SLen Brown 	struct pkg_data *p)
801103a8feaSLen Brown {
802388e9c81SLen Brown 	int i;
803388e9c81SLen Brown 	struct msr_counter *mp;
804388e9c81SLen Brown 
8053b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
806c98d5d94SLen Brown 
807c98d5d94SLen Brown 	if (t) {
8083b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
8093b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
8103b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
8113b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
8123b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
8133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
8146886fee4SLen Brown 
815812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
8160de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
817812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
818218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
819388e9c81SLen Brown 
820388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
821388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
822388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
823388e9c81SLen Brown 		}
824103a8feaSLen Brown 	}
825103a8feaSLen Brown 
826c98d5d94SLen Brown 	if (c) {
8273b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
8283b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
8293b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
8303b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
8313b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
8329392bd98SCalvin Walton 		outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
833388e9c81SLen Brown 
834388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
835388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
836388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
837388e9c81SLen Brown 		}
8380539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
839c98d5d94SLen Brown 	}
840103a8feaSLen Brown 
841c98d5d94SLen Brown 	if (p) {
8423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
8430b2bb692SLen Brown 
8440b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
8450b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
8460b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
8470b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
8480b2bb692SLen Brown 
8493b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
8500f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
8513b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
8520f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
8533b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
8540f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
8553b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
8563b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
8573b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
8583b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
859be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
860be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
861*7c2ccc50SChen Yu 		outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
862*7c2ccc50SChen Yu 		outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
863*7c2ccc50SChen Yu 		outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
864*7c2ccc50SChen Yu 		outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
865*7c2ccc50SChen Yu 		outp += sprintf(outp, "Throttle PKG: %0llX\n",
8663b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
867*7c2ccc50SChen Yu 		outp += sprintf(outp, "Throttle RAM: %0llX\n",
8683b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
8693b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
870388e9c81SLen Brown 
871388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
872388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
873388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
874388e9c81SLen Brown 		}
875c98d5d94SLen Brown 	}
8763b4d5c7fSAndy Shevchenko 
8773b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
8783b4d5c7fSAndy Shevchenko 
879c98d5d94SLen Brown 	return 0;
880103a8feaSLen Brown }
881103a8feaSLen Brown 
882e23da037SLen Brown /*
883e23da037SLen Brown  * column formatting convention & formats
884e23da037SLen Brown  */
885c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
886c98d5d94SLen Brown 	struct pkg_data *p)
887103a8feaSLen Brown {
888008d396eSLen Brown 	double interval_float, tsc;
889fc04cc67SLen Brown 	char *fmt8;
890388e9c81SLen Brown 	int i;
891388e9c81SLen Brown 	struct msr_counter *mp;
8926168c2e0SLen Brown 	char *delim = "\t";
8936168c2e0SLen Brown 	int printed = 0;
894103a8feaSLen Brown 
895c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
896c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
897c98d5d94SLen Brown 		return 0;
898c98d5d94SLen Brown 
899c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
900c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
901c98d5d94SLen Brown 		return 0;
902c98d5d94SLen Brown 
9031ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
9041ef7d21aSLen Brown 	if ((t != &average.threads) &&
9051ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
9061ef7d21aSLen Brown 		return 0;
9071ef7d21aSLen Brown 
9083f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
909f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
910f4fdf2b4SLen Brown 		struct timeval tv;
911f4fdf2b4SLen Brown 
912f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
913f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
914f4fdf2b4SLen Brown 	}
915f4fdf2b4SLen Brown 
9163f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
9173f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
9183f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
9193f44a5c6SLen Brown 
920d4794f25SYazen Ghannam 	interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec/1000000.0;
921103a8feaSLen Brown 
922008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
923008d396eSLen Brown 
924c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
925c98d5d94SLen Brown 	if (t == &average.threads) {
926812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
9276168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9286de68fe1SLen Brown 		if (DO_BIC(BIC_Die))
9296de68fe1SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
93001235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
93101235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
932812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
9336168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
934812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9356168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9364c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9374c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9384c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9394c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
940103a8feaSLen Brown 	} else {
941812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
942c98d5d94SLen Brown 			if (p)
9436168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
944c98d5d94SLen Brown 			else
9456168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
946c98d5d94SLen Brown 		}
9476de68fe1SLen Brown 		if (DO_BIC(BIC_Die)) {
9486de68fe1SLen Brown 			if (c)
9496de68fe1SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
9506de68fe1SLen Brown 			else
9516de68fe1SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9526de68fe1SLen Brown 		}
95301235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
95401235041SPrarit Bhargava 			if (t)
95501235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
95601235041SPrarit Bhargava 						(printed++ ? delim : ""),
95701235041SPrarit Bhargava 					      cpus[t->cpu_id].physical_node_id);
95801235041SPrarit Bhargava 			else
95901235041SPrarit Bhargava 				outp += sprintf(outp, "%s-",
96001235041SPrarit Bhargava 						(printed++ ? delim : ""));
96101235041SPrarit Bhargava 		}
962812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
963c98d5d94SLen Brown 			if (c)
9646168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
965c98d5d94SLen Brown 			else
9666168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
967c98d5d94SLen Brown 		}
968812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9696168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
9704c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9714c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
9724c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9734c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
974103a8feaSLen Brown 	}
975fc04cc67SLen Brown 
976812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
9776168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
978fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
979fc04cc67SLen Brown 
980812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
9816168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
982103a8feaSLen Brown 
983812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
98421ed5574SLen Brown 		if (has_base_hz)
9856168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
98621ed5574SLen Brown 		else
9876168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
988008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
98921ed5574SLen Brown 	}
990103a8feaSLen Brown 
991812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
9926168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
993103a8feaSLen Brown 
994562a2d37SLen Brown 	/* IRQ */
9950de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
9960de6c0dfSLen Brown 		if (sums_need_wide_columns)
9976168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
9980de6c0dfSLen Brown 		else
9996168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
10000de6c0dfSLen Brown 	}
1001562a2d37SLen Brown 
10021cc21f7bSLen Brown 	/* SMI */
1003812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
10046168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
10051cc21f7bSLen Brown 
1006678a3bd1SLen Brown 	/* Added counters */
1007678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1008678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
1009678a3bd1SLen Brown 			if (mp->width == 32)
10105f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
1011678a3bd1SLen Brown 			else
10126168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1013678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10140de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10156168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
10160de6c0dfSLen Brown 			else
10176168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1018678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
101941618e63SLen Brown 			if (mp->type == COUNTER_USEC)
10206168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
102141618e63SLen Brown 			else
10226168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
1023678a3bd1SLen Brown 		}
1024678a3bd1SLen Brown 	}
1025678a3bd1SLen Brown 
102641618e63SLen Brown 	/* C1 */
102741618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
10286168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
102941618e63SLen Brown 
103041618e63SLen Brown 
1031c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1032c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1033c98d5d94SLen Brown 		goto done;
1034c98d5d94SLen Brown 
1035562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
10366168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1037812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
10386168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1039812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
10406168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1041c98d5d94SLen Brown 
10420539ba11SLen Brown 	/* Mod%c6 */
10430539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
10446168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
10450539ba11SLen Brown 
1046812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
10476168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1048889facbeSLen Brown 
1049388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1050388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1051388e9c81SLen Brown 			if (mp->width == 32)
10525f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1053388e9c81SLen Brown 			else
10546168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1055388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10560de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10576168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
10580de6c0dfSLen Brown 			else
10596168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1060388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10616168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1062388e9c81SLen Brown 		}
1063388e9c81SLen Brown 	}
1064388e9c81SLen Brown 
10659392bd98SCalvin Walton 	/*
10669392bd98SCalvin Walton 	 * If measurement interval exceeds minimum RAPL Joule Counter range,
10679392bd98SCalvin Walton 	 * indicate that results are suspect by printing "**" in fraction place.
10689392bd98SCalvin Walton 	 */
10699392bd98SCalvin Walton 	if (interval_float < rapl_joule_counter_range)
10709392bd98SCalvin Walton 		fmt8 = "%s%.2f";
10719392bd98SCalvin Walton 	else
10729392bd98SCalvin Walton 		fmt8 = "%6.0f**";
10739392bd98SCalvin Walton 
10749392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
10759392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
10769392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
10779392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
10789392bd98SCalvin Walton 
1079c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1080c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1081c98d5d94SLen Brown 		goto done;
1082c98d5d94SLen Brown 
10830b2bb692SLen Brown 	/* PkgTmp */
1084812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
10856168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1086889facbeSLen Brown 
1087fdf676e5SLen Brown 	/* GFXrc6 */
1088812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1089ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
10906168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
10919185e988SLen Brown 		} else {
10926168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
10939185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
10949185e988SLen Brown 		}
10959185e988SLen Brown 	}
1096fdf676e5SLen Brown 
109727d47356SLen Brown 	/* GFXMHz */
1098812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
10996168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
110027d47356SLen Brown 
11010b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1102a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
11036168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1104a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
11056168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1106a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
11076168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1108a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
11096168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
11100b2bb692SLen Brown 
11110f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
11126168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
11130f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
11146168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
11150f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
11166168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
11170f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
11186168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
11190f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
11206168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
11210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
11226168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
11230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
11246168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1125889facbeSLen Brown 
1126be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1127be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1128be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1129be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1130be0e54c4SLen Brown 
1131812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
11326168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
11339392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
11346168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1135812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
11366168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1137812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
11386168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1139812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
11406168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
11419392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
11426168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1143812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
11446168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1145812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
11466168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1147812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
11486168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1149812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
11506168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1151812db3f7SLen Brown 
1152388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1153388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1154388e9c81SLen Brown 			if (mp->width == 32)
11555f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1156388e9c81SLen Brown 			else
11576168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1158388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
11590de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
11606168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
11610de6c0dfSLen Brown 			else
11626168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1163388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
11646168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1165388e9c81SLen Brown 		}
1166388e9c81SLen Brown 	}
1167388e9c81SLen Brown 
1168c98d5d94SLen Brown done:
116994d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1170c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1171c98d5d94SLen Brown 
1172c98d5d94SLen Brown 	return 0;
1173103a8feaSLen Brown }
1174103a8feaSLen Brown 
1175b7d8c148SLen Brown void flush_output_stdout(void)
1176103a8feaSLen Brown {
1177b7d8c148SLen Brown 	FILE *filep;
1178b7d8c148SLen Brown 
1179b7d8c148SLen Brown 	if (outf == stderr)
1180b7d8c148SLen Brown 		filep = stdout;
1181b7d8c148SLen Brown 	else
1182b7d8c148SLen Brown 		filep = outf;
1183b7d8c148SLen Brown 
1184b7d8c148SLen Brown 	fputs(output_buffer, filep);
1185b7d8c148SLen Brown 	fflush(filep);
1186b7d8c148SLen Brown 
1187c98d5d94SLen Brown 	outp = output_buffer;
1188c98d5d94SLen Brown }
1189b7d8c148SLen Brown void flush_output_stderr(void)
1190c98d5d94SLen Brown {
1191b7d8c148SLen Brown 	fputs(output_buffer, outf);
1192b7d8c148SLen Brown 	fflush(outf);
1193c98d5d94SLen Brown 	outp = output_buffer;
1194c98d5d94SLen Brown }
1195c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1196c98d5d94SLen Brown {
1197e23da037SLen Brown 	static int printed;
1198103a8feaSLen Brown 
1199e23da037SLen Brown 	if (!printed || !summary_only)
1200c8ade361SLen Brown 		print_header("\t");
1201103a8feaSLen Brown 
12029d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1203103a8feaSLen Brown 
1204e23da037SLen Brown 	printed = 1;
1205e23da037SLen Brown 
1206e23da037SLen Brown 	if (summary_only)
1207e23da037SLen Brown 		return;
1208e23da037SLen Brown 
1209c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1210103a8feaSLen Brown }
1211103a8feaSLen Brown 
1212889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1213*7c2ccc50SChen Yu 	old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1214889facbeSLen Brown 
1215ba3dec99SLen Brown int
1216c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1217103a8feaSLen Brown {
1218388e9c81SLen Brown 	int i;
1219388e9c81SLen Brown 	struct msr_counter *mp;
12200b2bb692SLen Brown 
1221a99d8730SLen Brown 
1222a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
12230b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1224a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
12250b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1226a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
12270b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1228a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
12290b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1230a99d8730SLen Brown 
1231c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
12320f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1233c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
12340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1235c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
12360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1237c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1238ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1239ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1240ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1241be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1242be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1243889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1244889facbeSLen Brown 
12459185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
12469185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12479185e988SLen Brown 		old->gfx_rc6_ms = -1;
12489185e988SLen Brown 	else
1249fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12509185e988SLen Brown 
125127d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
125227d47356SLen Brown 
1253889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1254889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1255889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1256889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1257889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1258889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1259ba3dec99SLen Brown 
1260388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1261388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1262388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1263388e9c81SLen Brown 		else
1264388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1265388e9c81SLen Brown 	}
1266388e9c81SLen Brown 
1267ba3dec99SLen Brown 	return 0;
1268103a8feaSLen Brown }
1269103a8feaSLen Brown 
1270c98d5d94SLen Brown void
1271c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1272c98d5d94SLen Brown {
1273388e9c81SLen Brown 	int i;
1274388e9c81SLen Brown 	struct msr_counter *mp;
1275388e9c81SLen Brown 
1276c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1277c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1278c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1279889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12800539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1281388e9c81SLen Brown 
12829392bd98SCalvin Walton 	DELTA_WRAP32(new->core_energy, old->core_energy);
12839392bd98SCalvin Walton 
1284388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1285388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1286388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1287388e9c81SLen Brown 		else
1288388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1289388e9c81SLen Brown 	}
1290103a8feaSLen Brown }
1291c98d5d94SLen Brown 
12921e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic)
12931e9042b9SSrinivas Pandruvada {
12941e9042b9SSrinivas Pandruvada 	if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
12951e9042b9SSrinivas Pandruvada 		return 0;
12961e9042b9SSrinivas Pandruvada 
12971e9042b9SSrinivas Pandruvada 	return DO_BIC_READ(bic);
12981e9042b9SSrinivas Pandruvada }
12991e9042b9SSrinivas Pandruvada 
1300c3ae331dSLen Brown /*
1301c3ae331dSLen Brown  * old = new - old
1302c3ae331dSLen Brown  */
1303ba3dec99SLen Brown int
1304c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1305c98d5d94SLen Brown 	struct core_data *core_delta)
1306c98d5d94SLen Brown {
1307388e9c81SLen Brown 	int i;
1308388e9c81SLen Brown 	struct msr_counter *mp;
1309388e9c81SLen Brown 
13104c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
13114c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
13124c2122d4SLen Brown 		new->apic_id = old->apic_id;
13134c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
13144c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
13154c2122d4SLen Brown 
13163f44a5c6SLen Brown 	/*
13173f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
13183f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
13193f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
13203f44a5c6SLen Brown 	 */
13213f44a5c6SLen Brown 
1322d4794f25SYazen Ghannam 	timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
13233f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
13243f44a5c6SLen Brown 	old->tv_end = new->tv_end;
13253f44a5c6SLen Brown 
1326c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1327c98d5d94SLen Brown 
1328103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1329b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1330b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1331b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1332b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1333103a8feaSLen Brown 
1334c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1335c98d5d94SLen Brown 
13361e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
13371e9042b9SSrinivas Pandruvada 	    soft_c1_residency_display(BIC_Avg_MHz)) {
1338c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1339c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1340c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1341c98d5d94SLen Brown 		} else {
1342ba3dec99SLen Brown 			return -1;
1343103a8feaSLen Brown 		}
1344a729617cSLen Brown 	}
1345103a8feaSLen Brown 
1346c98d5d94SLen Brown 
1347144b44b1SLen Brown 	if (use_c1_residency_msr) {
1348144b44b1SLen Brown 		/*
1349144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1350144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1351144b44b1SLen Brown 		 */
1352144b44b1SLen Brown 	} else {
1353103a8feaSLen Brown 		/*
1354c3ae331dSLen Brown 		 * As counter collection is not atomic,
1355c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1356103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1357103a8feaSLen Brown 		 */
135895149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1359c98d5d94SLen Brown 			old->c1 = 0;
1360c98d5d94SLen Brown 		else {
1361c98d5d94SLen Brown 			/* normal case, derive c1 */
1362008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1363c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1364c98d5d94SLen Brown 		}
1365144b44b1SLen Brown 	}
1366c3ae331dSLen Brown 
1367c98d5d94SLen Brown 	if (old->mperf == 0) {
1368b7d8c148SLen Brown 		if (debug > 1)
1369b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1370c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1371c98d5d94SLen Brown 	}
1372103a8feaSLen Brown 
1373812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1374562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1375562a2d37SLen Brown 
1376812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13771ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1378ba3dec99SLen Brown 
1379388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1380388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1381388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1382388e9c81SLen Brown 		else
1383388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1384388e9c81SLen Brown 	}
1385ba3dec99SLen Brown 	return 0;
1386103a8feaSLen Brown }
1387c98d5d94SLen Brown 
1388c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1389c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1390c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1391c98d5d94SLen Brown {
1392ba3dec99SLen Brown 	int retval = 0;
1393ba3dec99SLen Brown 
1394c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1395c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1396c98d5d94SLen Brown 		delta_core(c, c2);
1397c98d5d94SLen Brown 
1398c98d5d94SLen Brown 	/* always calculate thread delta */
1399ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1400ba3dec99SLen Brown 	if (retval)
1401ba3dec99SLen Brown 		return retval;
1402c98d5d94SLen Brown 
1403c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1404c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1405ba3dec99SLen Brown 		retval = delta_package(p, p2);
1406c98d5d94SLen Brown 
1407ba3dec99SLen Brown 	return retval;
1408103a8feaSLen Brown }
1409103a8feaSLen Brown 
1410c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1411103a8feaSLen Brown {
1412388e9c81SLen Brown 	int i;
1413388e9c81SLen Brown 	struct msr_counter  *mp;
1414388e9c81SLen Brown 
14153f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
14163f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
14173f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
14183f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
1419d4794f25SYazen Ghannam 	t->tv_delta.tv_sec = 0;
1420d4794f25SYazen Ghannam 	t->tv_delta.tv_usec = 0;
14213f44a5c6SLen Brown 
1422c98d5d94SLen Brown 	t->tsc = 0;
1423c98d5d94SLen Brown 	t->aperf = 0;
1424c98d5d94SLen Brown 	t->mperf = 0;
1425c98d5d94SLen Brown 	t->c1 = 0;
1426103a8feaSLen Brown 
1427562a2d37SLen Brown 	t->irq_count = 0;
1428562a2d37SLen Brown 	t->smi_count = 0;
1429562a2d37SLen Brown 
1430c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1431c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1432c98d5d94SLen Brown 
1433c98d5d94SLen Brown 	c->c3 = 0;
1434c98d5d94SLen Brown 	c->c6 = 0;
1435c98d5d94SLen Brown 	c->c7 = 0;
14360539ba11SLen Brown 	c->mc6_us = 0;
1437889facbeSLen Brown 	c->core_temp_c = 0;
14389392bd98SCalvin Walton 	c->core_energy = 0;
1439c98d5d94SLen Brown 
14400b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
14410b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
14420b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
14430b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
14440b2bb692SLen Brown 
1445c98d5d94SLen Brown 	p->pc2 = 0;
14460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1447c98d5d94SLen Brown 		p->pc3 = 0;
14480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1449c98d5d94SLen Brown 		p->pc6 = 0;
14500f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1451c98d5d94SLen Brown 		p->pc7 = 0;
1452ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1453ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1454ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1455be0e54c4SLen Brown 	p->cpu_lpi = 0;
1456be0e54c4SLen Brown 	p->sys_lpi = 0;
1457889facbeSLen Brown 
1458889facbeSLen Brown 	p->energy_pkg = 0;
1459889facbeSLen Brown 	p->energy_dram = 0;
1460889facbeSLen Brown 	p->energy_cores = 0;
1461889facbeSLen Brown 	p->energy_gfx = 0;
1462889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1463889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1464889facbeSLen Brown 	p->pkg_temp_c = 0;
146527d47356SLen Brown 
1466fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
146727d47356SLen Brown 	p->gfx_mhz = 0;
1468388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1469388e9c81SLen Brown 		t->counter[i] = 0;
1470388e9c81SLen Brown 
1471388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1472388e9c81SLen Brown 		c->counter[i] = 0;
1473388e9c81SLen Brown 
1474388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1475388e9c81SLen Brown 		p->counter[i] = 0;
1476103a8feaSLen Brown }
1477c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1478c98d5d94SLen Brown 	struct pkg_data *p)
1479103a8feaSLen Brown {
1480388e9c81SLen Brown 	int i;
1481388e9c81SLen Brown 	struct msr_counter *mp;
1482388e9c81SLen Brown 
14834c2122d4SLen Brown 	/* copy un-changing apic_id's */
14844c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
14854c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
14864c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
14874c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
14884c2122d4SLen Brown 
14893f44a5c6SLen Brown 	/* remember first tv_begin */
14903f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14913f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14923f44a5c6SLen Brown 
14933f44a5c6SLen Brown 	/* remember last tv_end */
14943f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14953f44a5c6SLen Brown 
1496c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1497c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1498c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1499c98d5d94SLen Brown 	average.threads.c1 += t->c1;
150015aaa346SLen Brown 
1501562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1502562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1503562a2d37SLen Brown 
1504388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1505388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1506388e9c81SLen Brown 			continue;
1507388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1508388e9c81SLen Brown 	}
1509388e9c81SLen Brown 
1510c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1511c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1512c98d5d94SLen Brown 		return 0;
1513c98d5d94SLen Brown 
1514c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1515c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1516c98d5d94SLen Brown 	average.cores.c7 += c->c7;
15170539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1518c98d5d94SLen Brown 
1519889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1520889facbeSLen Brown 
15219392bd98SCalvin Walton 	average.cores.core_energy += c->core_energy;
15229392bd98SCalvin Walton 
1523388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1524388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1525388e9c81SLen Brown 			continue;
1526388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1527388e9c81SLen Brown 	}
1528388e9c81SLen Brown 
1529c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1530c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1531c98d5d94SLen Brown 		return 0;
1532c98d5d94SLen Brown 
1533a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15340b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1535a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15360b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1537a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15380b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1539a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15400b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
15410b2bb692SLen Brown 
1542c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
15430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1544c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
15450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1546c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
15470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1548c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1549ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1550ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1551ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1552c98d5d94SLen Brown 
1553be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1554be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1555be0e54c4SLen Brown 
1556889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1557889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1558889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1559889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1560889facbeSLen Brown 
1561fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
156227d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
156327d47356SLen Brown 
1564889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1565889facbeSLen Brown 
1566889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1567889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1568388e9c81SLen Brown 
1569388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1570388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1571388e9c81SLen Brown 			continue;
1572388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1573388e9c81SLen Brown 	}
1574c98d5d94SLen Brown 	return 0;
1575c98d5d94SLen Brown }
1576c98d5d94SLen Brown /*
1577c98d5d94SLen Brown  * sum the counters for all cpus in the system
1578c98d5d94SLen Brown  * compute the weighted average
1579c98d5d94SLen Brown  */
1580c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1581c98d5d94SLen Brown 	struct pkg_data *p)
1582c98d5d94SLen Brown {
1583388e9c81SLen Brown 	int i;
1584388e9c81SLen Brown 	struct msr_counter *mp;
1585388e9c81SLen Brown 
1586c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1587c98d5d94SLen Brown 
1588c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1589c98d5d94SLen Brown 
1590d4794f25SYazen Ghannam 	/* Use the global time delta for the average. */
1591d4794f25SYazen Ghannam 	average.threads.tv_delta = tv_delta;
1592d4794f25SYazen Ghannam 
1593c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1594c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1595c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1596c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1597c98d5d94SLen Brown 
15980de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
15990de6c0dfSLen Brown 		sums_need_wide_columns = 1;
16000de6c0dfSLen Brown 
1601c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1602c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1603c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
16040539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1605c98d5d94SLen Brown 
1606a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
16070b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1608a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
16090b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1610a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
16110b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1612a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
16130b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
16140b2bb692SLen Brown 
1615c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
16160f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1617c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
16180f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1619c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
16200f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1621c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1622ca58710fSKristen Carlson Accardi 
1623ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1624ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1625ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1626388e9c81SLen Brown 
1627388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1628388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1629388e9c81SLen Brown 			continue;
16300de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16310de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
16320de6c0dfSLen Brown 				sums_need_wide_columns = 1;
163341618e63SLen Brown 			continue;
16340de6c0dfSLen Brown 		}
1635388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1636388e9c81SLen Brown 	}
1637388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1638388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1639388e9c81SLen Brown 			continue;
16400de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16410de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
16420de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16430de6c0dfSLen Brown 		}
1644388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1645388e9c81SLen Brown 	}
1646388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1647388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1648388e9c81SLen Brown 			continue;
16490de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16500de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
16510de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16520de6c0dfSLen Brown 		}
1653388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1654388e9c81SLen Brown 	}
1655c98d5d94SLen Brown }
1656c98d5d94SLen Brown 
1657c98d5d94SLen Brown static unsigned long long rdtsc(void)
1658c98d5d94SLen Brown {
1659c98d5d94SLen Brown 	unsigned int low, high;
1660c98d5d94SLen Brown 
1661c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1662c98d5d94SLen Brown 
1663c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1664c98d5d94SLen Brown }
1665c98d5d94SLen Brown 
1666c98d5d94SLen Brown /*
1667495c7654SLen Brown  * Open a file, and exit on failure
1668495c7654SLen Brown  */
1669495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1670495c7654SLen Brown {
1671495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1672495c7654SLen Brown 
1673495c7654SLen Brown 	if (!filep)
1674495c7654SLen Brown 		err(1, "%s: open failed", path);
1675495c7654SLen Brown 	return filep;
1676495c7654SLen Brown }
1677495c7654SLen Brown /*
1678495c7654SLen Brown  * snapshot_sysfs_counter()
1679495c7654SLen Brown  *
1680495c7654SLen Brown  * return snapshot of given counter
1681495c7654SLen Brown  */
1682495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1683495c7654SLen Brown {
1684495c7654SLen Brown 	FILE *fp;
1685495c7654SLen Brown 	int retval;
1686495c7654SLen Brown 	unsigned long long counter;
1687495c7654SLen Brown 
1688495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1689495c7654SLen Brown 
1690495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1691495c7654SLen Brown 	if (retval != 1)
1692495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1693495c7654SLen Brown 
1694495c7654SLen Brown 	fclose(fp);
1695495c7654SLen Brown 
1696495c7654SLen Brown 	return counter;
1697495c7654SLen Brown }
1698495c7654SLen Brown 
1699495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1700495c7654SLen Brown {
1701495c7654SLen Brown 	if (mp->msr_num != 0) {
1702495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1703495c7654SLen Brown 			return -1;
1704495c7654SLen Brown 	} else {
170546c27978SLen Brown 		char path[128 + PATH_BYTES];
170641618e63SLen Brown 
170741618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
170841618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
170941618e63SLen Brown 				 cpu, mp->path);
171041618e63SLen Brown 
171141618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
171241618e63SLen Brown 		} else {
1713495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1714495c7654SLen Brown 		}
171541618e63SLen Brown 	}
1716495c7654SLen Brown 
1717495c7654SLen Brown 	return 0;
1718495c7654SLen Brown }
1719495c7654SLen Brown 
17204c2122d4SLen Brown void get_apic_id(struct thread_data *t)
17214c2122d4SLen Brown {
172234041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
172334041551SLen Brown 
172434041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
172534041551SLen Brown 		eax = ebx = ecx = edx = 0;
172634041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
172734041551SLen Brown 
172834041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
172934041551SLen Brown 	}
173034041551SLen Brown 
173134041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
173234041551SLen Brown 		return;
173334041551SLen Brown 
1734c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine) {
173534041551SLen Brown 		unsigned int topology_extensions;
173634041551SLen Brown 
173734041551SLen Brown 		if (max_extended_level < 0x8000001e)
173834041551SLen Brown 			return;
17394c2122d4SLen Brown 
17404c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
174134041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
174234041551SLen Brown 			topology_extensions = ecx & (1 << 22);
174334041551SLen Brown 
174434041551SLen Brown 		if (topology_extensions == 0)
174534041551SLen Brown 			return;
174634041551SLen Brown 
174734041551SLen Brown 		eax = ebx = ecx = edx = 0;
174834041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
174934041551SLen Brown 
175034041551SLen Brown 		t->x2apic_id = eax;
175134041551SLen Brown 		return;
175234041551SLen Brown 	}
17534c2122d4SLen Brown 
17544c2122d4SLen Brown 	if (!genuine_intel)
17554c2122d4SLen Brown 		return;
17564c2122d4SLen Brown 
17574c2122d4SLen Brown 	if (max_level < 0xb)
17584c2122d4SLen Brown 		return;
17594c2122d4SLen Brown 
17604c2122d4SLen Brown 	ecx = 0;
17614c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
17624c2122d4SLen Brown 	t->x2apic_id = edx;
17634c2122d4SLen Brown 
176434041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
176534041551SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
176634041551SLen Brown 				t->cpu_id, t->apic_id, t->x2apic_id);
17674c2122d4SLen Brown }
17684c2122d4SLen Brown 
1769495c7654SLen Brown /*
1770c98d5d94SLen Brown  * get_counters(...)
1771c98d5d94SLen Brown  * migrate to cpu
1772c98d5d94SLen Brown  * acquire and record local counters for that cpu
1773c98d5d94SLen Brown  */
1774c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1775c98d5d94SLen Brown {
1776c98d5d94SLen Brown 	int cpu = t->cpu_id;
1777889facbeSLen Brown 	unsigned long long msr;
17780102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1779388e9c81SLen Brown 	struct msr_counter *mp;
1780388e9c81SLen Brown 	int i;
1781c98d5d94SLen Brown 
1782e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1783b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
178415aaa346SLen Brown 		return -1;
1785e52966c0SLen Brown 	}
178615aaa346SLen Brown 
1787d4794f25SYazen Ghannam 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1788d4794f25SYazen Ghannam 
17894c2122d4SLen Brown 	if (first_counter_read)
17904c2122d4SLen Brown 		get_apic_id(t);
17910102b067SLen Brown retry:
1792c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
179315aaa346SLen Brown 
17941e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
17951e9042b9SSrinivas Pandruvada 	    soft_c1_residency_display(BIC_Avg_MHz)) {
17960102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
17970102b067SLen Brown 
17980102b067SLen Brown 		/*
17990102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
18000102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
18010102b067SLen Brown 		 *
18020102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
18030102b067SLen Brown 		 * individual system call, so delays may occur
18040102b067SLen Brown 		 * between them.  If the time to read them
18050102b067SLen Brown 		 * varies by a large amount, we re-read them.
18060102b067SLen Brown 		 */
18070102b067SLen Brown 
18080102b067SLen Brown 		/*
18090102b067SLen Brown 		 * This initial dummy APERF read has been seen to
18100102b067SLen Brown 		 * reduce jitter in the subsequent reads.
18110102b067SLen Brown 		 */
18120102b067SLen Brown 
18139c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1814c98d5d94SLen Brown 			return -3;
18150102b067SLen Brown 
18160102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
18170102b067SLen Brown 
18180102b067SLen Brown 		tsc_before = t->tsc;
18190102b067SLen Brown 
18200102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
18210102b067SLen Brown 			return -3;
18220102b067SLen Brown 
18230102b067SLen Brown 		tsc_between = rdtsc();
18240102b067SLen Brown 
18259c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1826c98d5d94SLen Brown 			return -4;
18270102b067SLen Brown 
18280102b067SLen Brown 		tsc_after = rdtsc();
18290102b067SLen Brown 
18300102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
18310102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
18320102b067SLen Brown 
18330102b067SLen Brown 		/*
18340102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
18350102b067SLen Brown 		 * differ by more than 2x, then try again.
18360102b067SLen Brown 		 */
18370102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
18380102b067SLen Brown 			aperf_mperf_retry_count++;
18390102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
18400102b067SLen Brown 				goto retry;
18410102b067SLen Brown 			else
18420102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
18430102b067SLen Brown 					cpu, aperf_time, mperf_time);
18440102b067SLen Brown 		}
18450102b067SLen Brown 		aperf_mperf_retry_count = 0;
18460102b067SLen Brown 
1847b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1848b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
184988c3281fSLen Brown 	}
185088c3281fSLen Brown 
1851812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1852562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1853812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
18541ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
18551ed51011SLen Brown 			return -5;
18561ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
18571ed51011SLen Brown 	}
18580539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1859144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1860144b44b1SLen Brown 			return -6;
1861144b44b1SLen Brown 	}
1862144b44b1SLen Brown 
1863388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1864495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1865388e9c81SLen Brown 			return -10;
1866388e9c81SLen Brown 	}
1867388e9c81SLen Brown 
1868c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1869c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1870f4fdf2b4SLen Brown 		goto done;
1871c98d5d94SLen Brown 
18721e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
1873c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1874c98d5d94SLen Brown 			return -6;
1875144b44b1SLen Brown 	}
1876144b44b1SLen Brown 
18771e9042b9SSrinivas Pandruvada 	if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
1878c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1879c98d5d94SLen Brown 			return -7;
18801e9042b9SSrinivas Pandruvada 	} else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
1881fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1882fb5d4327SDasaratharaman Chandramouli 			return -7;
1883103a8feaSLen Brown 	}
188415aaa346SLen Brown 
18851e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7))
1886c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1887c98d5d94SLen Brown 			return -8;
1888c98d5d94SLen Brown 
18890539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18900539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18910539ba11SLen Brown 			return -8;
18920539ba11SLen Brown 
1893812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1894889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1895889facbeSLen Brown 			return -9;
1896889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1897889facbeSLen Brown 	}
1898889facbeSLen Brown 
18999392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
19009392bd98SCalvin Walton 		if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
19019392bd98SCalvin Walton 			return -14;
19029392bd98SCalvin Walton 		c->core_energy = msr & 0xFFFFFFFF;
19039392bd98SCalvin Walton 	}
19049392bd98SCalvin Walton 
1905388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1906495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1907388e9c81SLen Brown 			return -10;
1908388e9c81SLen Brown 	}
1909889facbeSLen Brown 
1910c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1911c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1912f4fdf2b4SLen Brown 		goto done;
191315aaa346SLen Brown 
1914a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
19150b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
19160b2bb692SLen Brown 			return -10;
1917a99d8730SLen Brown 	}
1918a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
19190b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
19200b2bb692SLen Brown 			return -11;
1921a99d8730SLen Brown 	}
1922a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
19230b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
19240b2bb692SLen Brown 			return -12;
1925a99d8730SLen Brown 	}
1926a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
19270b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
19280b2bb692SLen Brown 			return -13;
19290b2bb692SLen Brown 	}
19300f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1931c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1932c98d5d94SLen Brown 			return -9;
19330f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
19340539ba11SLen Brown 		if (do_slm_cstates) {
19350539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
19360539ba11SLen Brown 				return -10;
19370539ba11SLen Brown 		} else {
1938c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1939c98d5d94SLen Brown 				return -10;
19400539ba11SLen Brown 		}
19410539ba11SLen Brown 	}
19420539ba11SLen Brown 
19430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1944c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1945c98d5d94SLen Brown 			return -11;
19460f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1947c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1948c98d5d94SLen Brown 			return -12;
19490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1950ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1951ca58710fSKristen Carlson Accardi 			return -13;
19520f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1953ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1954ca58710fSKristen Carlson Accardi 			return -13;
19550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1956ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1957ca58710fSKristen Carlson Accardi 			return -13;
19580f47c08dSLen Brown 
1959be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1960be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1961be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1962be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1963be0e54c4SLen Brown 
1964889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1965889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1966889facbeSLen Brown 			return -13;
1967889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1968889facbeSLen Brown 	}
19699148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1970889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1971889facbeSLen Brown 			return -14;
1972889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1973889facbeSLen Brown 	}
1974889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1975889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1976889facbeSLen Brown 			return -15;
1977889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1978889facbeSLen Brown 	}
1979889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1980889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1981889facbeSLen Brown 			return -16;
1982889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1983889facbeSLen Brown 	}
1984889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1985889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1986889facbeSLen Brown 			return -16;
1987889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1988889facbeSLen Brown 	}
1989889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1990889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1991889facbeSLen Brown 			return -16;
1992889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1993889facbeSLen Brown 	}
19943316f99aSCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
19953316f99aSCalvin Walton 		if (get_msr(cpu, MSR_PKG_ENERGY_STAT, &msr))
19963316f99aSCalvin Walton 			return -13;
19973316f99aSCalvin Walton 		p->energy_pkg = msr & 0xFFFFFFFF;
19983316f99aSCalvin Walton 	}
1999812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
2000889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2001889facbeSLen Brown 			return -17;
2002889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
2003889facbeSLen Brown 	}
2004fdf676e5SLen Brown 
2005812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2006fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
2007fdf676e5SLen Brown 
2008812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
200927d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
201027d47356SLen Brown 
2011388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2012495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
2013388e9c81SLen Brown 			return -10;
2014388e9c81SLen Brown 	}
2015f4fdf2b4SLen Brown done:
2016f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
2017388e9c81SLen Brown 
201815aaa346SLen Brown 	return 0;
2019103a8feaSLen Brown }
2020103a8feaSLen Brown 
2021ee7e38e3SLen Brown /*
2022ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2023ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
2024ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2025ee7e38e3SLen Brown  */
2026ee7e38e3SLen Brown 
2027ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
2028ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
2029ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
2030ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
2031ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
2032ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
2033ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
2034ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
2035ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
2036ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
2037ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
2038ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
20390b2bb692SLen Brown #define PCL__8 12 /* PC8 */
20400b2bb692SLen Brown #define PCL__9 13 /* PC9 */
2041445640a5SLen Brown #define PCL_10 14 /* PC10 */
2042445640a5SLen Brown #define PCLUNL 15 /* Unlimited */
2043ee7e38e3SLen Brown 
2044ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2045ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2046445640a5SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
2047ee7e38e3SLen Brown 
2048e9257f5fSLen Brown int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2049e9257f5fSLen Brown int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2050e9257f5fSLen Brown int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
20510539ba11SLen Brown int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7};
2052f2642888SLen Brown int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2053e9257f5fSLen Brown int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2054445640a5SLen Brown int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
20552085e124SArtem Bityutskiy int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
2056ee7e38e3SLen Brown 
2057a2b7b749SLen Brown 
2058a2b7b749SLen Brown static void
2059a2b7b749SLen Brown calculate_tsc_tweak()
2060a2b7b749SLen Brown {
2061a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
2062a2b7b749SLen Brown }
2063a2b7b749SLen Brown 
2064fcd17211SLen Brown static void
2065fcd17211SLen Brown dump_nhm_platform_info(void)
2066103a8feaSLen Brown {
2067103a8feaSLen Brown 	unsigned long long msr;
2068103a8feaSLen Brown 	unsigned int ratio;
2069103a8feaSLen Brown 
2070ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2071103a8feaSLen Brown 
2072b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
20736574a5d5SLen Brown 
2074103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
2075710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2076103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2077103a8feaSLen Brown 
2078103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
2079710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2080103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2081103a8feaSLen Brown 
20827ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2083b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2084bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
208567920418SLen Brown 
2086fcd17211SLen Brown 	return;
2087fcd17211SLen Brown }
2088fcd17211SLen Brown 
2089fcd17211SLen Brown static void
2090fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
2091fcd17211SLen Brown {
2092fcd17211SLen Brown 	unsigned long long msr;
2093fcd17211SLen Brown 	unsigned int ratio;
2094fcd17211SLen Brown 
20957ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2096fcd17211SLen Brown 
2097b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2098fcd17211SLen Brown 
2099fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2100fcd17211SLen Brown 	if (ratio)
2101710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2102fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2103fcd17211SLen Brown 
2104fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2105fcd17211SLen Brown 	if (ratio)
2106710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2107fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2108fcd17211SLen Brown 	return;
2109fcd17211SLen Brown }
2110fcd17211SLen Brown 
2111fcd17211SLen Brown static void
2112fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2113fcd17211SLen Brown {
2114fcd17211SLen Brown 	unsigned long long msr;
2115fcd17211SLen Brown 	unsigned int ratio;
21166574a5d5SLen Brown 
21177ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
21186574a5d5SLen Brown 
2119b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
21206574a5d5SLen Brown 
21216574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
21226574a5d5SLen Brown 	if (ratio)
2123710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
21246574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21256574a5d5SLen Brown 
21266574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
21276574a5d5SLen Brown 	if (ratio)
2128710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
21296574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21306574a5d5SLen Brown 
21316574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
21326574a5d5SLen Brown 	if (ratio)
2133710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
21346574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21356574a5d5SLen Brown 
21366574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
21376574a5d5SLen Brown 	if (ratio)
2138710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
21396574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21406574a5d5SLen Brown 
21416574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
21426574a5d5SLen Brown 	if (ratio)
2143710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
21446574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21456574a5d5SLen Brown 
21466574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
21476574a5d5SLen Brown 	if (ratio)
2148710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
21496574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21506574a5d5SLen Brown 
21516574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
21526574a5d5SLen Brown 	if (ratio)
2153710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
21546574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21556574a5d5SLen Brown 
21566574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
21576574a5d5SLen Brown 	if (ratio)
2158710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
21596574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2160103a8feaSLen Brown 	return;
2161fcd17211SLen Brown }
216231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
216331e07522SLen Brown {
216431e07522SLen Brown 
216531e07522SLen Brown 	if (!genuine_intel)
216631e07522SLen Brown 		return 0;
216731e07522SLen Brown 
216831e07522SLen Brown 	switch (model) {
216931e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
217031e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
21715ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
217231e07522SLen Brown 		return 1;
217331e07522SLen Brown 	}
217431e07522SLen Brown 	return 0;
217531e07522SLen Brown }
2176fcd17211SLen Brown 
2177fcd17211SLen Brown static void
217831e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2179fcd17211SLen Brown {
218031e07522SLen Brown 	unsigned long long msr, core_counts;
218131e07522SLen Brown 	unsigned int ratio, group_size;
2182103a8feaSLen Brown 
21837ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2184b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
21856574a5d5SLen Brown 
218631e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
218731e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
218831e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
218931e07522SLen Brown 	} else {
219031e07522SLen Brown 		core_counts = 0x0807060504030201;
219131e07522SLen Brown 	}
219231e07522SLen Brown 
21936574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
219431e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
21956574a5d5SLen Brown 	if (ratio)
219631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
219731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21986574a5d5SLen Brown 
21996574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
220031e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
22016574a5d5SLen Brown 	if (ratio)
220231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22046574a5d5SLen Brown 
22056574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
220631e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
22076574a5d5SLen Brown 	if (ratio)
220831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22106574a5d5SLen Brown 
22116574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
221231e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
22136574a5d5SLen Brown 	if (ratio)
221431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
221531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22166574a5d5SLen Brown 
2217103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
221831e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2219103a8feaSLen Brown 	if (ratio)
222031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
222131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2222103a8feaSLen Brown 
2223103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
222431e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2225103a8feaSLen Brown 	if (ratio)
222631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
222731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2228103a8feaSLen Brown 
2229103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
223031e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2231103a8feaSLen Brown 	if (ratio)
223231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
223331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2234103a8feaSLen Brown 
2235103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
223631e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2237103a8feaSLen Brown 	if (ratio)
223831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
223931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2240fcd17211SLen Brown 	return;
2241fcd17211SLen Brown }
22423a9a941dSLen Brown 
2243fcd17211SLen Brown static void
22440f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
22450f7887c4SLen Brown {
22460f7887c4SLen Brown 	unsigned long long msr;
22470f7887c4SLen Brown 	unsigned int ratio;
22480f7887c4SLen Brown 
22490f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
22500f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22510f7887c4SLen Brown 
22520f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22530f7887c4SLen Brown 	if (ratio)
22540f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
22550f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22560f7887c4SLen Brown 
22570f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22580f7887c4SLen Brown 	if (ratio)
22590f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
22600f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22610f7887c4SLen Brown 
22620f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22630f7887c4SLen Brown 	if (ratio)
22640f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
22650f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22660f7887c4SLen Brown 
22670f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
22680f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22690f7887c4SLen Brown 
22700f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
22710f7887c4SLen Brown 	if (ratio)
22720f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
22730f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22740f7887c4SLen Brown 
22750f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22760f7887c4SLen Brown 	if (ratio)
22770f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
22780f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22790f7887c4SLen Brown 
22800f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22810f7887c4SLen Brown 	if (ratio)
22820f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
22830f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22840f7887c4SLen Brown 
22850f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22860f7887c4SLen Brown 	if (ratio)
22870f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
22880f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22890f7887c4SLen Brown }
22900f7887c4SLen Brown 
22910f7887c4SLen Brown static void
2292fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2293fb5d4327SDasaratharaman Chandramouli {
2294cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2295cbf97abaSHubert Chrzaniuk 
2296fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2297cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2298cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2299cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2300cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2301fb5d4327SDasaratharaman Chandramouli 
2302ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2303fb5d4327SDasaratharaman Chandramouli 
2304b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2305bfae2052SLen Brown 		base_cpu, msr);
2306fb5d4327SDasaratharaman Chandramouli 
2307fb5d4327SDasaratharaman Chandramouli 	/**
2308fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2309cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2310cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2311fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2312fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2313fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2314fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2315fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2316fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2317fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2318fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2319fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2320fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2321fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2322fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2323fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2324fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2325fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2326fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2327fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2328fb5d4327SDasaratharaman Chandramouli 	 */
2329fb5d4327SDasaratharaman Chandramouli 
2330cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2331cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2332cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2333cbf97abaSHubert Chrzaniuk 
2334cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2335fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2336cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2337fb5d4327SDasaratharaman Chandramouli 
2338cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2339cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2340cbf97abaSHubert Chrzaniuk 		b_nr++;
2341fb5d4327SDasaratharaman Chandramouli 	}
2342cbf97abaSHubert Chrzaniuk 
2343cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2344cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2345b7d8c148SLen Brown 			fprintf(outf,
2346710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2347cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2348fb5d4327SDasaratharaman Chandramouli }
2349fb5d4327SDasaratharaman Chandramouli 
2350fb5d4327SDasaratharaman Chandramouli static void
2351fcd17211SLen Brown dump_nhm_cst_cfg(void)
2352fcd17211SLen Brown {
2353fcd17211SLen Brown 	unsigned long long msr;
2354fcd17211SLen Brown 
23551df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2356fcd17211SLen Brown 
23571df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2358fcd17211SLen Brown 
23593e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2360fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2361fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2362fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2363fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2364fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
23656c34f160SLen Brown 		(unsigned int)msr & 0xF,
2366fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2367ac980e13SArtem Bityutskiy 
2368ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2369ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2370ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2371ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2372ac980e13SArtem Bityutskiy 	}
2373ac980e13SArtem Bityutskiy 
2374ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2375ac980e13SArtem Bityutskiy 
2376fcd17211SLen Brown 	return;
2377103a8feaSLen Brown }
2378103a8feaSLen Brown 
23796fb3143bSLen Brown static void
23806fb3143bSLen Brown dump_config_tdp(void)
23816fb3143bSLen Brown {
23826fb3143bSLen Brown 	unsigned long long msr;
23836fb3143bSLen Brown 
23846fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2385b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2386685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
23876fb3143bSLen Brown 
23886fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2389b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
23906fb3143bSLen Brown 	if (msr) {
2391685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2392685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2393685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2394685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
23956fb3143bSLen Brown 	}
2396b7d8c148SLen Brown 	fprintf(outf, ")\n");
23976fb3143bSLen Brown 
23986fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2399b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
24006fb3143bSLen Brown 	if (msr) {
2401685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2402685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2403685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2404685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
24056fb3143bSLen Brown 	}
2406b7d8c148SLen Brown 	fprintf(outf, ")\n");
24076fb3143bSLen Brown 
24086fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2409b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
24106fb3143bSLen Brown 	if ((msr) & 0x3)
2411b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2412b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2413b7d8c148SLen Brown 	fprintf(outf, ")\n");
24146fb3143bSLen Brown 
24156fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2416b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2417685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2418b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2419b7d8c148SLen Brown 	fprintf(outf, ")\n");
24206fb3143bSLen Brown }
24215a63426eSLen Brown 
24225a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
24235a63426eSLen Brown 
24245a63426eSLen Brown void print_irtl(void)
24255a63426eSLen Brown {
24265a63426eSLen Brown 	unsigned long long msr;
24275a63426eSLen Brown 
24285a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
24295a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
24305a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24315a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24325a63426eSLen Brown 
24335a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
24345a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
24355a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24365a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24375a63426eSLen Brown 
24385a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
24395a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
24405a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24415a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24425a63426eSLen Brown 
24435a63426eSLen Brown 	if (!do_irtl_hsw)
24445a63426eSLen Brown 		return;
24455a63426eSLen Brown 
24465a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
24475a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
24485a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24495a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24505a63426eSLen Brown 
24515a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
24525a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
24535a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24545a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24555a63426eSLen Brown 
24565a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
24575a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
24585a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24595a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24605a63426eSLen Brown 
24615a63426eSLen Brown }
246236229897SLen Brown void free_fd_percpu(void)
246336229897SLen Brown {
246436229897SLen Brown 	int i;
246536229897SLen Brown 
246601a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
246736229897SLen Brown 		if (fd_percpu[i] != 0)
246836229897SLen Brown 			close(fd_percpu[i]);
246936229897SLen Brown 	}
247036229897SLen Brown 
247136229897SLen Brown 	free(fd_percpu);
24726fb3143bSLen Brown }
24736fb3143bSLen Brown 
2474c98d5d94SLen Brown void free_all_buffers(void)
2475103a8feaSLen Brown {
24760e2d8f05SLen Brown 	int i;
24770e2d8f05SLen Brown 
2478c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2479c98d5d94SLen Brown 	cpu_present_set = NULL;
248036229897SLen Brown 	cpu_present_setsize = 0;
2481103a8feaSLen Brown 
2482c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2483c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2484c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2485103a8feaSLen Brown 
2486c98d5d94SLen Brown 	free(thread_even);
2487c98d5d94SLen Brown 	free(core_even);
2488c98d5d94SLen Brown 	free(package_even);
2489103a8feaSLen Brown 
2490c98d5d94SLen Brown 	thread_even = NULL;
2491c98d5d94SLen Brown 	core_even = NULL;
2492c98d5d94SLen Brown 	package_even = NULL;
2493103a8feaSLen Brown 
2494c98d5d94SLen Brown 	free(thread_odd);
2495c98d5d94SLen Brown 	free(core_odd);
2496c98d5d94SLen Brown 	free(package_odd);
2497103a8feaSLen Brown 
2498c98d5d94SLen Brown 	thread_odd = NULL;
2499c98d5d94SLen Brown 	core_odd = NULL;
2500c98d5d94SLen Brown 	package_odd = NULL;
2501103a8feaSLen Brown 
2502c98d5d94SLen Brown 	free(output_buffer);
2503c98d5d94SLen Brown 	output_buffer = NULL;
2504c98d5d94SLen Brown 	outp = NULL;
250536229897SLen Brown 
250636229897SLen Brown 	free_fd_percpu();
2507562a2d37SLen Brown 
2508562a2d37SLen Brown 	free(irq_column_2_cpu);
2509562a2d37SLen Brown 	free(irqs_per_cpu);
25100e2d8f05SLen Brown 
25110e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
25120e2d8f05SLen Brown 		if (cpus[i].put_ids)
25130e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
25140e2d8f05SLen Brown 	}
25150e2d8f05SLen Brown 	free(cpus);
2516103a8feaSLen Brown }
2517103a8feaSLen Brown 
251857a42a34SJosh Triplett 
251957a42a34SJosh Triplett /*
252095aebc44SJosh Triplett  * Parse a file containing a single int.
25216de68fe1SLen Brown  * Return 0 if file can not be opened
25226de68fe1SLen Brown  * Exit if file can be opened, but can not be parsed
252395aebc44SJosh Triplett  */
252495aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
252595aebc44SJosh Triplett {
252695aebc44SJosh Triplett 	va_list args;
252795aebc44SJosh Triplett 	char path[PATH_MAX];
252895aebc44SJosh Triplett 	FILE *filep;
252995aebc44SJosh Triplett 	int value;
253095aebc44SJosh Triplett 
253195aebc44SJosh Triplett 	va_start(args, fmt);
253295aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
253395aebc44SJosh Triplett 	va_end(args);
25346de68fe1SLen Brown 	filep = fopen(path, "r");
25356de68fe1SLen Brown 	if (!filep)
25366de68fe1SLen Brown 		return 0;
2537b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2538b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
253995aebc44SJosh Triplett 	fclose(filep);
254095aebc44SJosh Triplett 	return value;
254195aebc44SJosh Triplett }
254295aebc44SJosh Triplett 
254395aebc44SJosh Triplett /*
2544c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2545c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2546c98d5d94SLen Brown  */
2547c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2548c98d5d94SLen Brown {
254995aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2550103a8feaSLen Brown }
2551103a8feaSLen Brown 
2552103a8feaSLen Brown int get_physical_package_id(int cpu)
2553103a8feaSLen Brown {
255495aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2555103a8feaSLen Brown }
2556103a8feaSLen Brown 
25576de68fe1SLen Brown int get_die_id(int cpu)
25586de68fe1SLen Brown {
25596de68fe1SLen Brown 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
25606de68fe1SLen Brown }
25616de68fe1SLen Brown 
2562103a8feaSLen Brown int get_core_id(int cpu)
2563103a8feaSLen Brown {
256495aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2565103a8feaSLen Brown }
2566103a8feaSLen Brown 
2567ef605741SPrarit Bhargava void set_node_data(void)
2568ef605741SPrarit Bhargava {
25692ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
25702ffbb224SPrarit Bhargava 	int cpu_count;
2571ef605741SPrarit Bhargava 
25722ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
25732ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
25742ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2575ef605741SPrarit Bhargava 
25762ffbb224SPrarit Bhargava 	cpu_count = 0;
25772ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
25782ffbb224SPrarit Bhargava 		lnode = 0;
25792ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
25802ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2581ef605741SPrarit Bhargava 				continue;
25822ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
25832ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
25842ffbb224SPrarit Bhargava 				continue;
25852ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2586ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
25872ffbb224SPrarit Bhargava 			cpu_count++;
25882ffbb224SPrarit Bhargava 			/*
25892ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
25902ffbb224SPrarit Bhargava 			 * the logical_node_id
25912ffbb224SPrarit Bhargava 			 */
25922ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
25932ffbb224SPrarit Bhargava 				if ((cpus[cpux].physical_package_id == pkg) &&
25942ffbb224SPrarit Bhargava 				   (cpus[cpux].physical_node_id == node)) {
25952ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
25962ffbb224SPrarit Bhargava 					cpu_count++;
2597ef605741SPrarit Bhargava 				}
25982ffbb224SPrarit Bhargava 			}
25992ffbb224SPrarit Bhargava 			lnode++;
26002ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
26012ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
26022ffbb224SPrarit Bhargava 		}
26032ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
26042ffbb224SPrarit Bhargava 			break;
26052ffbb224SPrarit Bhargava 	}
2606ef605741SPrarit Bhargava }
2607ef605741SPrarit Bhargava 
2608ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2609c98d5d94SLen Brown {
2610c98d5d94SLen Brown 	char path[80];
2611c98d5d94SLen Brown 	FILE *filep;
26120e2d8f05SLen Brown 	int i;
26130e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2614c98d5d94SLen Brown 
26150e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
26160e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
26170e2d8f05SLen Brown 			cpu, i);
26180e2d8f05SLen Brown 		filep = fopen(path, "r");
26190e2d8f05SLen Brown 		if (!filep)
26200e2d8f05SLen Brown 			continue;
26210e2d8f05SLen Brown 		fclose(filep);
26220e2d8f05SLen Brown 		return i;
26230e2d8f05SLen Brown 	}
26240e2d8f05SLen Brown 	return -1;
2625e275b388SDasaratharaman Chandramouli }
2626c98d5d94SLen Brown 
26270e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
26280e2d8f05SLen Brown {
26290e2d8f05SLen Brown 	char path[80], character;
26300e2d8f05SLen Brown 	FILE *filep;
26310e2d8f05SLen Brown 	unsigned long map;
26328cb48b32SPrarit Bhargava 	int so, shift, sib_core;
26330e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
26340e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
26350e2d8f05SLen Brown 	size_t size;
26368cb48b32SPrarit Bhargava 	int thread_id = 0;
26370e2d8f05SLen Brown 
26380e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
26398cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
26408cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
26410e2d8f05SLen Brown 	if (!thiscpu->put_ids)
26420e2d8f05SLen Brown 		return -1;
26430e2d8f05SLen Brown 
26440e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
26450e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
26460e2d8f05SLen Brown 
26470e2d8f05SLen Brown 	sprintf(path,
26480e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
26490e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
26500e2d8f05SLen Brown 	do {
26510e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
26528173c336SBen Hutchings 		if (fscanf(filep, "%lx%c", &map, &character) != 2)
26538173c336SBen Hutchings 			err(1, "%s: failed to parse file", path);
26540e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
26550e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
26568cb48b32SPrarit Bhargava 				so = shift + offset;
26578cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
26588cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
26598cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
26608cb48b32SPrarit Bhargava 					if ((so != cpu) &&
26618cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
26628cb48b32SPrarit Bhargava 						cpus[so].thread_id =
26638cb48b32SPrarit Bhargava 								    thread_id++;
26648cb48b32SPrarit Bhargava 				}
26650e2d8f05SLen Brown 			}
26660e2d8f05SLen Brown 		}
26670e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2668c98d5d94SLen Brown 	fclose(filep);
26690e2d8f05SLen Brown 
26700e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2671c98d5d94SLen Brown }
2672c98d5d94SLen Brown 
2673c98d5d94SLen Brown /*
2674c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2675c98d5d94SLen Brown  * skip non-present cpus
2676103a8feaSLen Brown  */
2677103a8feaSLen Brown 
2678c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2679c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2680c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2681c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2682c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2683c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2684c98d5d94SLen Brown {
268540f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2686c98d5d94SLen Brown 
2687c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
268840f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
268940f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
269040f5cfe7SPrarit Bhargava 			     ++core_no) {
2691c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
269270a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2693c98d5d94SLen Brown 					struct thread_data *t, *t2;
2694c98d5d94SLen Brown 					struct core_data *c, *c2;
2695c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2696c98d5d94SLen Brown 
269740f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
269840f5cfe7SPrarit Bhargava 						       core_no, node_no,
269940f5cfe7SPrarit Bhargava 						       pkg_no);
2700c98d5d94SLen Brown 
2701c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2702c98d5d94SLen Brown 						continue;
2703c98d5d94SLen Brown 
270440f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
270540f5cfe7SPrarit Bhargava 							core_no, node_no,
270640f5cfe7SPrarit Bhargava 							pkg_no);
2707c98d5d94SLen Brown 
270840f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
270940f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
271040f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
271140f5cfe7SPrarit Bhargava 						      node_no,
271240f5cfe7SPrarit Bhargava 						      pkg_no);
2713c98d5d94SLen Brown 
2714c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2715c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2716c98d5d94SLen Brown 
2717c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2718c98d5d94SLen Brown 					if (retval)
2719c98d5d94SLen Brown 						return retval;
2720c98d5d94SLen Brown 				}
2721c98d5d94SLen Brown 			}
2722c98d5d94SLen Brown 		}
272340f5cfe7SPrarit Bhargava 	}
2724c98d5d94SLen Brown 	return 0;
2725c98d5d94SLen Brown }
2726c98d5d94SLen Brown 
2727c98d5d94SLen Brown /*
2728c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2729c98d5d94SLen Brown  * return max_cpu number
2730c98d5d94SLen Brown  */
2731c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2732103a8feaSLen Brown {
2733103a8feaSLen Brown 	FILE *fp;
2734c98d5d94SLen Brown 	int cpu_num;
2735103a8feaSLen Brown 	int retval;
2736103a8feaSLen Brown 
273757a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2738103a8feaSLen Brown 
2739103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2740b2c95d90SJosh Triplett 	if (retval != 0)
2741b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2742103a8feaSLen Brown 
2743c98d5d94SLen Brown 	while (1) {
2744c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2745103a8feaSLen Brown 		if (retval != 1)
2746103a8feaSLen Brown 			break;
2747103a8feaSLen Brown 
2748c98d5d94SLen Brown 		retval = func(cpu_num);
2749c98d5d94SLen Brown 		if (retval) {
2750c98d5d94SLen Brown 			fclose(fp);
2751c98d5d94SLen Brown 			return(retval);
2752c98d5d94SLen Brown 		}
2753103a8feaSLen Brown 	}
2754103a8feaSLen Brown 	fclose(fp);
2755c98d5d94SLen Brown 	return 0;
2756103a8feaSLen Brown }
2757103a8feaSLen Brown 
2758103a8feaSLen Brown void re_initialize(void)
2759103a8feaSLen Brown {
2760c98d5d94SLen Brown 	free_all_buffers();
2761c98d5d94SLen Brown 	setup_all_buffers();
2762c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2763103a8feaSLen Brown }
2764103a8feaSLen Brown 
2765843c5791SPrarit Bhargava void set_max_cpu_num(void)
2766843c5791SPrarit Bhargava {
2767843c5791SPrarit Bhargava 	FILE *filep;
2768843c5791SPrarit Bhargava 	unsigned long dummy;
2769843c5791SPrarit Bhargava 
2770843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2771843c5791SPrarit Bhargava 	filep = fopen_or_die(
2772843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2773843c5791SPrarit Bhargava 			"r");
2774843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
27750e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2776843c5791SPrarit Bhargava 	fclose(filep);
2777843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2778843c5791SPrarit Bhargava }
2779c98d5d94SLen Brown 
2780103a8feaSLen Brown /*
2781c98d5d94SLen Brown  * count_cpus()
2782c98d5d94SLen Brown  * remember the last one seen, it will be the max
2783103a8feaSLen Brown  */
2784c98d5d94SLen Brown int count_cpus(int cpu)
2785103a8feaSLen Brown {
2786843c5791SPrarit Bhargava 	topo.num_cpus++;
2787c98d5d94SLen Brown 	return 0;
2788103a8feaSLen Brown }
2789c98d5d94SLen Brown int mark_cpu_present(int cpu)
2790c98d5d94SLen Brown {
2791c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
279215aaa346SLen Brown 	return 0;
2793103a8feaSLen Brown }
2794103a8feaSLen Brown 
27958cb48b32SPrarit Bhargava int init_thread_id(int cpu)
27968cb48b32SPrarit Bhargava {
27978cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
27988cb48b32SPrarit Bhargava 	return 0;
27998cb48b32SPrarit Bhargava }
28008cb48b32SPrarit Bhargava 
2801562a2d37SLen Brown /*
2802562a2d37SLen Brown  * snapshot_proc_interrupts()
2803562a2d37SLen Brown  *
2804562a2d37SLen Brown  * read and record summary of /proc/interrupts
2805562a2d37SLen Brown  *
2806562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2807562a2d37SLen Brown  */
2808562a2d37SLen Brown int snapshot_proc_interrupts(void)
2809562a2d37SLen Brown {
2810562a2d37SLen Brown 	static FILE *fp;
2811562a2d37SLen Brown 	int column, retval;
2812562a2d37SLen Brown 
2813562a2d37SLen Brown 	if (fp == NULL)
2814562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2815562a2d37SLen Brown 	else
2816562a2d37SLen Brown 		rewind(fp);
2817562a2d37SLen Brown 
2818562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2819562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2820562a2d37SLen Brown 		int cpu_number;
2821562a2d37SLen Brown 
2822562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2823562a2d37SLen Brown 		if (retval != 1)
2824562a2d37SLen Brown 			break;
2825562a2d37SLen Brown 
2826562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2827562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2828562a2d37SLen Brown 			return 1;
2829562a2d37SLen Brown 		}
2830562a2d37SLen Brown 
2831562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2832562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2833562a2d37SLen Brown 	}
2834562a2d37SLen Brown 
2835562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2836562a2d37SLen Brown 	while (1) {
2837562a2d37SLen Brown 		int column;
2838562a2d37SLen Brown 		char buf[64];
2839562a2d37SLen Brown 
2840562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2841562a2d37SLen Brown 		if (retval != 1)
2842562a2d37SLen Brown 			break;
2843562a2d37SLen Brown 
2844562a2d37SLen Brown 		/* read the count per cpu */
2845562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2846562a2d37SLen Brown 
2847562a2d37SLen Brown 			int cpu_number, irq_count;
2848562a2d37SLen Brown 
2849562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2850562a2d37SLen Brown 			if (retval != 1)
2851562a2d37SLen Brown 				break;
2852562a2d37SLen Brown 
2853562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2854562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2855562a2d37SLen Brown 
2856562a2d37SLen Brown 		}
2857562a2d37SLen Brown 
2858562a2d37SLen Brown 		while (getc(fp) != '\n')
2859562a2d37SLen Brown 			;	/* flush interrupt description */
2860562a2d37SLen Brown 
2861562a2d37SLen Brown 	}
2862562a2d37SLen Brown 	return 0;
2863562a2d37SLen Brown }
286427d47356SLen Brown /*
2865fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2866fdf676e5SLen Brown  *
2867fdf676e5SLen Brown  * record snapshot of
2868fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2869fdf676e5SLen Brown  *
2870fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2871fdf676e5SLen Brown  */
2872fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2873fdf676e5SLen Brown {
2874fdf676e5SLen Brown 	FILE *fp;
2875fdf676e5SLen Brown 	int retval;
2876fdf676e5SLen Brown 
2877fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2878fdf676e5SLen Brown 
2879fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2880fdf676e5SLen Brown 	if (retval != 1)
2881fdf676e5SLen Brown 		err(1, "GFX rc6");
2882fdf676e5SLen Brown 
2883fdf676e5SLen Brown 	fclose(fp);
2884fdf676e5SLen Brown 
2885fdf676e5SLen Brown 	return 0;
2886fdf676e5SLen Brown }
2887fdf676e5SLen Brown /*
288827d47356SLen Brown  * snapshot_gfx_mhz()
288927d47356SLen Brown  *
289027d47356SLen Brown  * record snapshot of
289127d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
289227d47356SLen Brown  *
289327d47356SLen Brown  * return 1 if config change requires a restart, else return 0
289427d47356SLen Brown  */
289527d47356SLen Brown int snapshot_gfx_mhz(void)
289627d47356SLen Brown {
289727d47356SLen Brown 	static FILE *fp;
289827d47356SLen Brown 	int retval;
289927d47356SLen Brown 
290027d47356SLen Brown 	if (fp == NULL)
290127d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
290222048c54SLen Brown 	else {
290327d47356SLen Brown 		rewind(fp);
290422048c54SLen Brown 		fflush(fp);
290522048c54SLen Brown 	}
290627d47356SLen Brown 
290727d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
290827d47356SLen Brown 	if (retval != 1)
290927d47356SLen Brown 		err(1, "GFX MHz");
291027d47356SLen Brown 
291127d47356SLen Brown 	return 0;
291227d47356SLen Brown }
2913562a2d37SLen Brown 
2914562a2d37SLen Brown /*
2915be0e54c4SLen Brown  * snapshot_cpu_lpi()
2916be0e54c4SLen Brown  *
2917be0e54c4SLen Brown  * record snapshot of
2918be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2919be0e54c4SLen Brown  */
2920be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2921be0e54c4SLen Brown {
2922be0e54c4SLen Brown 	FILE *fp;
2923be0e54c4SLen Brown 	int retval;
2924be0e54c4SLen Brown 
2925be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2926be0e54c4SLen Brown 
2927be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
29285ea7647bSPrarit Bhargava 	if (retval != 1) {
29295ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle CPU output\n");
29305ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_CPU_LPI);
2931605736c6SGustavo A. R. Silva 		fclose(fp);
29325ea7647bSPrarit Bhargava 		return -1;
29335ea7647bSPrarit Bhargava 	}
2934be0e54c4SLen Brown 
2935be0e54c4SLen Brown 	fclose(fp);
2936be0e54c4SLen Brown 
2937be0e54c4SLen Brown 	return 0;
2938be0e54c4SLen Brown }
2939be0e54c4SLen Brown /*
2940be0e54c4SLen Brown  * snapshot_sys_lpi()
2941be0e54c4SLen Brown  *
29421f81c5efSLen Brown  * record snapshot of sys_lpi_file
2943be0e54c4SLen Brown  */
2944be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2945be0e54c4SLen Brown {
2946be0e54c4SLen Brown 	FILE *fp;
2947be0e54c4SLen Brown 	int retval;
2948be0e54c4SLen Brown 
29491f81c5efSLen Brown 	fp = fopen_or_die(sys_lpi_file, "r");
2950be0e54c4SLen Brown 
2951be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
29525ea7647bSPrarit Bhargava 	if (retval != 1) {
29535ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle System output\n");
29545ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_SYS_LPI);
295515423b95SColin Ian King 		fclose(fp);
29565ea7647bSPrarit Bhargava 		return -1;
29575ea7647bSPrarit Bhargava 	}
2958be0e54c4SLen Brown 	fclose(fp);
2959be0e54c4SLen Brown 
2960be0e54c4SLen Brown 	return 0;
2961be0e54c4SLen Brown }
2962be0e54c4SLen Brown /*
2963562a2d37SLen Brown  * snapshot /proc and /sys files
2964562a2d37SLen Brown  *
2965562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2966562a2d37SLen Brown  */
2967562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2968562a2d37SLen Brown {
2969218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2970562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2971562a2d37SLen Brown 			return 1;
2972562a2d37SLen Brown 
2973812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2974fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2975fdf676e5SLen Brown 
2976812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
297727d47356SLen Brown 		snapshot_gfx_mhz();
297827d47356SLen Brown 
2979be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2980be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2981be0e54c4SLen Brown 
2982be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2983be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2984be0e54c4SLen Brown 
2985562a2d37SLen Brown 	return 0;
2986562a2d37SLen Brown }
2987562a2d37SLen Brown 
29888aa2ed0bSLen Brown int exit_requested;
29898aa2ed0bSLen Brown 
29908aa2ed0bSLen Brown static void signal_handler (int signal)
29918aa2ed0bSLen Brown {
29928aa2ed0bSLen Brown 	switch (signal) {
29938aa2ed0bSLen Brown 	case SIGINT:
29948aa2ed0bSLen Brown 		exit_requested = 1;
29958aa2ed0bSLen Brown 		if (debug)
29968aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
29978aa2ed0bSLen Brown 		break;
299807211960SLen Brown 	case SIGUSR1:
299907211960SLen Brown 		if (debug > 1)
300007211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
300107211960SLen Brown 		break;
30028aa2ed0bSLen Brown 	}
30038aa2ed0bSLen Brown }
30048aa2ed0bSLen Brown 
30058aa2ed0bSLen Brown void setup_signal_handler(void)
30068aa2ed0bSLen Brown {
30078aa2ed0bSLen Brown 	struct sigaction sa;
30088aa2ed0bSLen Brown 
30098aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
30108aa2ed0bSLen Brown 
30118aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
30128aa2ed0bSLen Brown 
30138aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
30148aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
301507211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
301607211960SLen Brown 		err(1, "sigaction SIGUSR1");
30178aa2ed0bSLen Brown }
3018b9ad8ee0SLen Brown 
301947936f94SArtem Bityutskiy void do_sleep(void)
3020b9ad8ee0SLen Brown {
3021c026c236SArtem Bityutskiy 	struct timeval tout;
3022c026c236SArtem Bityutskiy 	struct timespec rest;
3023b9ad8ee0SLen Brown 	fd_set readfds;
3024b9ad8ee0SLen Brown 	int retval;
3025b9ad8ee0SLen Brown 
3026b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
3027b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
3028b9ad8ee0SLen Brown 
3029c026c236SArtem Bityutskiy 	if (ignore_stdin) {
303047936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
303147936f94SArtem Bityutskiy 		return;
303247936f94SArtem Bityutskiy 	}
3033b9ad8ee0SLen Brown 
3034c026c236SArtem Bityutskiy 	tout = interval_tv;
3035c026c236SArtem Bityutskiy 	retval = select(1, &readfds, NULL, NULL, &tout);
3036b9ad8ee0SLen Brown 
3037b9ad8ee0SLen Brown 	if (retval == 1) {
3038b9ad8ee0SLen Brown 		switch (getc(stdin)) {
3039b9ad8ee0SLen Brown 		case 'q':
3040b9ad8ee0SLen Brown 			exit_requested = 1;
3041b9ad8ee0SLen Brown 			break;
3042c026c236SArtem Bityutskiy 		case EOF:
3043c026c236SArtem Bityutskiy 			/*
3044c026c236SArtem Bityutskiy 			 * 'stdin' is a pipe closed on the other end. There
3045c026c236SArtem Bityutskiy 			 * won't be any further input.
3046c026c236SArtem Bityutskiy 			 */
3047c026c236SArtem Bityutskiy 			ignore_stdin = 1;
3048c026c236SArtem Bityutskiy 			/* Sleep the rest of the time */
3049c026c236SArtem Bityutskiy 			rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3050c026c236SArtem Bityutskiy 			rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3051c026c236SArtem Bityutskiy 			nanosleep(&rest, NULL);
3052b9ad8ee0SLen Brown 		}
3053b9ad8ee0SLen Brown 	}
3054b9ad8ee0SLen Brown }
305547936f94SArtem Bityutskiy 
30564c2122d4SLen Brown 
3057103a8feaSLen Brown void turbostat_loop()
3058103a8feaSLen Brown {
3059c98d5d94SLen Brown 	int retval;
3060e52966c0SLen Brown 	int restarted = 0;
3061023fe0acSChen Yu 	int done_iters = 0;
3062c98d5d94SLen Brown 
30638aa2ed0bSLen Brown 	setup_signal_handler();
30648aa2ed0bSLen Brown 
3065103a8feaSLen Brown restart:
3066e52966c0SLen Brown 	restarted++;
3067e52966c0SLen Brown 
3068562a2d37SLen Brown 	snapshot_proc_sysfs_files();
3069c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
30704c2122d4SLen Brown 	first_counter_read = 0;
3071d91bb17cSLen Brown 	if (retval < -1) {
3072d91bb17cSLen Brown 		exit(retval);
3073d91bb17cSLen Brown 	} else if (retval == -1) {
3074e52966c0SLen Brown 		if (restarted > 1) {
3075e52966c0SLen Brown 			exit(retval);
3076e52966c0SLen Brown 		}
3077c98d5d94SLen Brown 		re_initialize();
3078c98d5d94SLen Brown 		goto restart;
3079c98d5d94SLen Brown 	}
3080e52966c0SLen Brown 	restarted = 0;
3081023fe0acSChen Yu 	done_iters = 0;
3082103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3083103a8feaSLen Brown 
3084103a8feaSLen Brown 	while (1) {
3085c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3086103a8feaSLen Brown 			re_initialize();
3087103a8feaSLen Brown 			goto restart;
3088103a8feaSLen Brown 		}
3089b9ad8ee0SLen Brown 		do_sleep();
3090562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3091562a2d37SLen Brown 			goto restart;
3092c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3093d91bb17cSLen Brown 		if (retval < -1) {
3094d91bb17cSLen Brown 			exit(retval);
3095d91bb17cSLen Brown 		} else if (retval == -1) {
309615aaa346SLen Brown 			re_initialize();
309715aaa346SLen Brown 			goto restart;
309815aaa346SLen Brown 		}
3099103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3100103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3101ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3102ba3dec99SLen Brown 			re_initialize();
3103ba3dec99SLen Brown 			goto restart;
3104ba3dec99SLen Brown 		}
3105c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3106c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3107b7d8c148SLen Brown 		flush_output_stdout();
31088aa2ed0bSLen Brown 		if (exit_requested)
31098aa2ed0bSLen Brown 			break;
3110023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3111023fe0acSChen Yu 			break;
3112b9ad8ee0SLen Brown 		do_sleep();
3113562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3114562a2d37SLen Brown 			goto restart;
3115c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3116d91bb17cSLen Brown 		if (retval < -1) {
3117d91bb17cSLen Brown 			exit(retval);
3118d91bb17cSLen Brown 		} else if (retval == -1) {
3119103a8feaSLen Brown 			re_initialize();
3120103a8feaSLen Brown 			goto restart;
3121103a8feaSLen Brown 		}
3122103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3123103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3124ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3125ba3dec99SLen Brown 			re_initialize();
3126ba3dec99SLen Brown 			goto restart;
3127ba3dec99SLen Brown 		}
3128c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3129c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3130b7d8c148SLen Brown 		flush_output_stdout();
31318aa2ed0bSLen Brown 		if (exit_requested)
31328aa2ed0bSLen Brown 			break;
3133023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3134023fe0acSChen Yu 			break;
3135103a8feaSLen Brown 	}
3136103a8feaSLen Brown }
3137103a8feaSLen Brown 
3138103a8feaSLen Brown void check_dev_msr()
3139103a8feaSLen Brown {
3140103a8feaSLen Brown 	struct stat sb;
31417ce7d5deSPrarit Bhargava 	char pathname[32];
3142103a8feaSLen Brown 
31437ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31447ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3145a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3146d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3147103a8feaSLen Brown }
3148103a8feaSLen Brown 
3149fcaa681cSLen Brown /*
3150fcaa681cSLen Brown  * check for CAP_SYS_RAWIO
3151fcaa681cSLen Brown  * return 0 on success
3152fcaa681cSLen Brown  * return 1 on fail
3153fcaa681cSLen Brown  */
3154fcaa681cSLen Brown int check_for_cap_sys_rawio(void)
3155103a8feaSLen Brown {
3156fcaa681cSLen Brown 	cap_t caps;
3157fcaa681cSLen Brown 	cap_flag_value_t cap_flag_value;
3158fcaa681cSLen Brown 
3159fcaa681cSLen Brown 	caps = cap_get_proc();
3160fcaa681cSLen Brown 	if (caps == NULL)
3161fcaa681cSLen Brown 		err(-6, "cap_get_proc\n");
3162fcaa681cSLen Brown 
3163fcaa681cSLen Brown 	if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3164fcaa681cSLen Brown 		err(-6, "cap_get\n");
3165fcaa681cSLen Brown 
3166fcaa681cSLen Brown 	if (cap_flag_value != CAP_SET) {
3167fcaa681cSLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
3168fcaa681cSLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
3169fcaa681cSLen Brown 		return 1;
3170fcaa681cSLen Brown 	}
3171fcaa681cSLen Brown 
3172fcaa681cSLen Brown 	if (cap_free(caps) == -1)
3173fcaa681cSLen Brown 		err(-6, "cap_free\n");
3174fcaa681cSLen Brown 
3175fcaa681cSLen Brown 	return 0;
3176fcaa681cSLen Brown }
3177fcaa681cSLen Brown void check_permissions(void)
3178fcaa681cSLen Brown {
317998481e79SLen Brown 	int do_exit = 0;
31807ce7d5deSPrarit Bhargava 	char pathname[32];
318198481e79SLen Brown 
318298481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
3183fcaa681cSLen Brown 	do_exit += check_for_cap_sys_rawio();
318498481e79SLen Brown 
318598481e79SLen Brown 	/* test file permissions */
31867ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31877ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
318898481e79SLen Brown 		do_exit++;
318998481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
319098481e79SLen Brown 	}
319198481e79SLen Brown 
319298481e79SLen Brown 	/* if all else fails, thell them to be root */
319398481e79SLen Brown 	if (do_exit)
3194b2c95d90SJosh Triplett 		if (getuid() != 0)
3195d7899447SLen Brown 			warnx("... or simply run as root");
319698481e79SLen Brown 
319798481e79SLen Brown 	if (do_exit)
319898481e79SLen Brown 		exit(-6);
3199103a8feaSLen Brown }
3200103a8feaSLen Brown 
3201d7899447SLen Brown /*
3202d7899447SLen Brown  * NHM adds support for additional MSRs:
3203d7899447SLen Brown  *
3204d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3205d7899447SLen Brown  *
3206ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
32071df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3208d7899447SLen Brown  *
3209cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3210cf4cbe53SLen Brown  *
3211d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3212d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3213d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3214d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3215d7899447SLen Brown  *
3216ee7e38e3SLen Brown  * Side effect:
32171df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
321833148d67SLen Brown  * sets has_misc_feature_control
3219d7899447SLen Brown  */
3220ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3221103a8feaSLen Brown {
3222ee7e38e3SLen Brown 	unsigned long long msr;
322321ed5574SLen Brown 	unsigned int base_ratio;
3224ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3225ee7e38e3SLen Brown 
3226103a8feaSLen Brown 	if (!genuine_intel)
3227103a8feaSLen Brown 		return 0;
3228103a8feaSLen Brown 
3229103a8feaSLen Brown 	if (family != 6)
3230103a8feaSLen Brown 		return 0;
3231103a8feaSLen Brown 
323221ed5574SLen Brown 	bclk = discover_bclk(family, model);
323321ed5574SLen Brown 
3234103a8feaSLen Brown 	switch (model) {
3235869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3236869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3237ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3238ee7e38e3SLen Brown 		break;
3239869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3240869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3241869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3242869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3243ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
324433148d67SLen Brown 		has_misc_feature_control = 1;
3245ee7e38e3SLen Brown 		break;
3246c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
32475e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3248869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
324977e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
3250c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
32515e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3252869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3253af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3254af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3255ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
325633148d67SLen Brown 		has_misc_feature_control = 1;
3257ee7e38e3SLen Brown 		break;
3258d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3259d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
326033148d67SLen Brown 		has_misc_feature_control = 1;
3261d8ebb442SLen Brown 		break;
3262f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3263cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
32645ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
3265ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3266ee7e38e3SLen Brown 		break;
3267869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3268ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3269cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3270ee7e38e3SLen Brown 		break;
3271869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3272ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3273ee7e38e3SLen Brown 		break;
3274869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3275f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
32765ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
3277f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
3278445640a5SLen Brown 		pkg_cstate_limits = glm_pkg_cstate_limits;
3279e4085d54SLen Brown 		break;
3280103a8feaSLen Brown 	default:
3281103a8feaSLen Brown 		return 0;
3282103a8feaSLen Brown 	}
32831df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3284e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3285ee7e38e3SLen Brown 
3286ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
328721ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
328821ed5574SLen Brown 
328921ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
329021ed5574SLen Brown 	has_base_hz = 1;
3291ee7e38e3SLen Brown 	return 1;
3292103a8feaSLen Brown }
32930f7887c4SLen Brown /*
3294495c7654SLen Brown  * SLV client has support for unique MSRs:
32950f7887c4SLen Brown  *
32960f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
32970f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
32980f7887c4SLen Brown  */
32990f7887c4SLen Brown 
33000f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
33010f7887c4SLen Brown {
33020f7887c4SLen Brown 	if (!genuine_intel)
33030f7887c4SLen Brown 		return 0;
33040f7887c4SLen Brown 
33050f7887c4SLen Brown 	switch (model) {
3306f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3307f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3308f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
33090f7887c4SLen Brown 		return 1;
33100f7887c4SLen Brown 	}
33110f7887c4SLen Brown 	return 0;
33120f7887c4SLen Brown }
33137170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
33147170a374SLen Brown {
33157170a374SLen Brown 
33167170a374SLen Brown 	if (!genuine_intel)
33177170a374SLen Brown 		return 0;
33187170a374SLen Brown 
33197170a374SLen Brown 	switch (model) {
33205ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
33217170a374SLen Brown 		return 1;
33227170a374SLen Brown 	}
33237170a374SLen Brown 	return 0;
33247170a374SLen Brown }
3325ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3326ade0ebacSLen Brown {
3327ade0ebacSLen Brown 
3328ade0ebacSLen Brown 	if (!genuine_intel)
3329ade0ebacSLen Brown 		return 0;
3330ade0ebacSLen Brown 
3331ade0ebacSLen Brown 	switch (model) {
3332ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3333ade0ebacSLen Brown 		return 1;
3334ade0ebacSLen Brown 	}
3335ade0ebacSLen Brown 	return 0;
3336ade0ebacSLen Brown }
333734c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
333834c76197SLen Brown {
333934c76197SLen Brown 
334034c76197SLen Brown 	if (!genuine_intel)
334134c76197SLen Brown 		return 0;
334234c76197SLen Brown 
334334c76197SLen Brown 	switch (model) {
334434c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
334534c76197SLen Brown 		return 1;
334634c76197SLen Brown 	}
334734c76197SLen Brown 	return 0;
334834c76197SLen Brown }
3349f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model)
3350f6708400SChen Yu {
3351f6708400SChen Yu 	if (!genuine_intel)
3352f6708400SChen Yu 		return 0;
3353f6708400SChen Yu 
3354f6708400SChen Yu 	switch (model) {
3355f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:
3356f6708400SChen Yu 		return 1;
3357f6708400SChen Yu 	}
3358f6708400SChen Yu 	return 0;
3359f6708400SChen Yu }
33600f7887c4SLen Brown 
336131e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3362d7899447SLen Brown {
33630f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33640f7887c4SLen Brown 		return 0;
33650f7887c4SLen Brown 
3366d7899447SLen Brown 	switch (model) {
3367d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3368869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3369869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3370d7899447SLen Brown 		return 0;
3371d7899447SLen Brown 	default:
3372d7899447SLen Brown 		return 1;
3373d7899447SLen Brown 	}
3374d7899447SLen Brown }
33750f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
33760f7887c4SLen Brown {
33770f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33780f7887c4SLen Brown 		return 1;
33790f7887c4SLen Brown 
33800f7887c4SLen Brown 	return 0;
33810f7887c4SLen Brown }
33826574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
33836574a5d5SLen Brown {
33846574a5d5SLen Brown 	if (!genuine_intel)
33856574a5d5SLen Brown 		return 0;
33866574a5d5SLen Brown 
33876574a5d5SLen Brown 	if (family != 6)
33886574a5d5SLen Brown 		return 0;
33896574a5d5SLen Brown 
33906574a5d5SLen Brown 	switch (model) {
3391869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3392869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
33936574a5d5SLen Brown 		return 1;
33946574a5d5SLen Brown 	default:
33956574a5d5SLen Brown 		return 0;
33966574a5d5SLen Brown 	}
33976574a5d5SLen Brown }
3398fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3399fcd17211SLen Brown {
3400fcd17211SLen Brown 	if (!genuine_intel)
3401fcd17211SLen Brown 		return 0;
3402fcd17211SLen Brown 
3403fcd17211SLen Brown 	if (family != 6)
3404fcd17211SLen Brown 		return 0;
3405fcd17211SLen Brown 
3406fcd17211SLen Brown 	switch (model) {
3407869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3408fcd17211SLen Brown 		return 1;
3409fcd17211SLen Brown 	default:
3410fcd17211SLen Brown 		return 0;
3411fcd17211SLen Brown 	}
3412fcd17211SLen Brown }
3413fcd17211SLen Brown 
3414fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3415fb5d4327SDasaratharaman Chandramouli {
3416fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3417fb5d4327SDasaratharaman Chandramouli 		return 0;
3418fb5d4327SDasaratharaman Chandramouli 
3419fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3420fb5d4327SDasaratharaman Chandramouli 		return 0;
3421fb5d4327SDasaratharaman Chandramouli 
3422fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3423869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3424fb5d4327SDasaratharaman Chandramouli 		return 1;
3425fb5d4327SDasaratharaman Chandramouli 	default:
3426fb5d4327SDasaratharaman Chandramouli 		return 0;
3427fb5d4327SDasaratharaman Chandramouli 	}
3428fb5d4327SDasaratharaman Chandramouli }
342931e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
343031e07522SLen Brown {
343131e07522SLen Brown 	if (!genuine_intel)
343231e07522SLen Brown 		return 0;
343331e07522SLen Brown 
343431e07522SLen Brown 	if (family != 6)
343531e07522SLen Brown 		return 0;
343631e07522SLen Brown 
343731e07522SLen Brown 	switch (model) {
343831e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
343931e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
344031e07522SLen Brown 		return 1;
344131e07522SLen Brown 	default:
344231e07522SLen Brown 		return 0;
344331e07522SLen Brown 	}
344431e07522SLen Brown }
34456fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
34466fb3143bSLen Brown {
34476fb3143bSLen Brown 	if (!genuine_intel)
34486fb3143bSLen Brown 		return 0;
34496fb3143bSLen Brown 
34506fb3143bSLen Brown 	if (family != 6)
34516fb3143bSLen Brown 		return 0;
34526fb3143bSLen Brown 
34536fb3143bSLen Brown 	switch (model) {
3454869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3455c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
3456869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
345777e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
34585e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3459c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
34605e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3461869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3462af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3463af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3464869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
34656fb3143bSLen Brown 
3466869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
34676fb3143bSLen Brown 		return 1;
34686fb3143bSLen Brown 	default:
34696fb3143bSLen Brown 		return 0;
34706fb3143bSLen Brown 	}
34716fb3143bSLen Brown }
34726fb3143bSLen Brown 
3473fcd17211SLen Brown static void
34741b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3475fcd17211SLen Brown {
3476fcd17211SLen Brown 	if (!do_nhm_platform_info)
3477fcd17211SLen Brown 		return;
3478fcd17211SLen Brown 
3479fcd17211SLen Brown 	dump_nhm_platform_info();
3480fcd17211SLen Brown 
3481fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3482fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3483fcd17211SLen Brown 
3484fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3485fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3486fcd17211SLen Brown 
348731e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
348831e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3489fcd17211SLen Brown 
34900f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
34910f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
34920f7887c4SLen Brown 
3493fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3494fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3495fb5d4327SDasaratharaman Chandramouli 
34966fb3143bSLen Brown 	if (has_config_tdp(family, model))
34976fb3143bSLen Brown 		dump_config_tdp();
34986fb3143bSLen Brown 
3499fcd17211SLen Brown 	dump_nhm_cst_cfg();
3500fcd17211SLen Brown }
3501fcd17211SLen Brown 
3502abdcbdb2SLen Brown static void dump_sysfs_file(char *path)
3503abdcbdb2SLen Brown {
3504abdcbdb2SLen Brown 	FILE *input;
3505abdcbdb2SLen Brown 	char cpuidle_buf[64];
3506abdcbdb2SLen Brown 
3507abdcbdb2SLen Brown 	input = fopen(path, "r");
3508abdcbdb2SLen Brown 	if (input == NULL) {
3509abdcbdb2SLen Brown 		if (debug)
3510abdcbdb2SLen Brown 			fprintf(outf, "NSFOD %s\n", path);
3511abdcbdb2SLen Brown 		return;
3512abdcbdb2SLen Brown 	}
3513abdcbdb2SLen Brown 	if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
3514abdcbdb2SLen Brown 		err(1, "%s: failed to read file", path);
3515abdcbdb2SLen Brown 	fclose(input);
3516abdcbdb2SLen Brown 
3517abdcbdb2SLen Brown 	fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
3518abdcbdb2SLen Brown }
351941618e63SLen Brown static void
352041618e63SLen Brown dump_sysfs_cstate_config(void)
352141618e63SLen Brown {
352241618e63SLen Brown 	char path[64];
352341618e63SLen Brown 	char name_buf[16];
352441618e63SLen Brown 	char desc[64];
352541618e63SLen Brown 	FILE *input;
352641618e63SLen Brown 	int state;
352741618e63SLen Brown 	char *sp;
352841618e63SLen Brown 
3529abdcbdb2SLen Brown 	if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
3530abdcbdb2SLen Brown 		fprintf(outf, "cpuidle not loaded\n");
3531abdcbdb2SLen Brown 		return;
3532abdcbdb2SLen Brown 	}
3533abdcbdb2SLen Brown 
3534abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
3535abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
3536abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
3537abdcbdb2SLen Brown 
353841618e63SLen Brown 	for (state = 0; state < 10; ++state) {
353941618e63SLen Brown 
354041618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
354141618e63SLen Brown 			base_cpu, state);
354241618e63SLen Brown 		input = fopen(path, "r");
354341618e63SLen Brown 		if (input == NULL)
354441618e63SLen Brown 			continue;
35458173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
35468173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
354741618e63SLen Brown 
354841618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
354941618e63SLen Brown 		sp = strchr(name_buf, '-');
355041618e63SLen Brown 		if (!sp)
355141618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
355241618e63SLen Brown 		*sp = '\0';
355341618e63SLen Brown 		fclose(input);
355441618e63SLen Brown 
355541618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
355641618e63SLen Brown 			base_cpu, state);
355741618e63SLen Brown 		input = fopen(path, "r");
355841618e63SLen Brown 		if (input == NULL)
355941618e63SLen Brown 			continue;
35608173c336SBen Hutchings 		if (!fgets(desc, sizeof(desc), input))
35618173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
356241618e63SLen Brown 
356341618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
356441618e63SLen Brown 		fclose(input);
356541618e63SLen Brown 	}
356641618e63SLen Brown }
35677293fccdSLen Brown static void
35687293fccdSLen Brown dump_sysfs_pstate_config(void)
35697293fccdSLen Brown {
35707293fccdSLen Brown 	char path[64];
35717293fccdSLen Brown 	char driver_buf[64];
35727293fccdSLen Brown 	char governor_buf[64];
35737293fccdSLen Brown 	FILE *input;
35747293fccdSLen Brown 	int turbo;
35757293fccdSLen Brown 
35767293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
35777293fccdSLen Brown 			base_cpu);
35787293fccdSLen Brown 	input = fopen(path, "r");
35797293fccdSLen Brown 	if (input == NULL) {
35800a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35817293fccdSLen Brown 		return;
35827293fccdSLen Brown 	}
35838173c336SBen Hutchings 	if (!fgets(driver_buf, sizeof(driver_buf), input))
35848173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
35857293fccdSLen Brown 	fclose(input);
35867293fccdSLen Brown 
35877293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
35887293fccdSLen Brown 			base_cpu);
35897293fccdSLen Brown 	input = fopen(path, "r");
35907293fccdSLen Brown 	if (input == NULL) {
35910a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35927293fccdSLen Brown 		return;
35937293fccdSLen Brown 	}
35948173c336SBen Hutchings 	if (!fgets(governor_buf, sizeof(governor_buf), input))
35958173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
35967293fccdSLen Brown 	fclose(input);
35977293fccdSLen Brown 
35987293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
35997293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
36007293fccdSLen Brown 
36017293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
36027293fccdSLen Brown 	input = fopen(path, "r");
36037293fccdSLen Brown 	if (input != NULL) {
36048173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
36058173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
36067293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
36077293fccdSLen Brown 		fclose(input);
36087293fccdSLen Brown 	}
36097293fccdSLen Brown 
36107293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
36117293fccdSLen Brown 	input = fopen(path, "r");
36127293fccdSLen Brown 	if (input != NULL) {
36138173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
36148173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
36157293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
36167293fccdSLen Brown 		fclose(input);
36177293fccdSLen Brown 	}
36187293fccdSLen Brown }
361941618e63SLen Brown 
36206574a5d5SLen Brown 
3621889facbeSLen Brown /*
3622889facbeSLen Brown  * print_epb()
3623889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3624889facbeSLen Brown  */
3625889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3626889facbeSLen Brown {
3627889facbeSLen Brown 	unsigned long long msr;
3628889facbeSLen Brown 	char *epb_string;
3629889facbeSLen Brown 	int cpu;
3630889facbeSLen Brown 
3631889facbeSLen Brown 	if (!has_epb)
3632889facbeSLen Brown 		return 0;
3633889facbeSLen Brown 
3634889facbeSLen Brown 	cpu = t->cpu_id;
3635889facbeSLen Brown 
3636889facbeSLen Brown 	/* EPB is per-package */
3637889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3638889facbeSLen Brown 		return 0;
3639889facbeSLen Brown 
3640889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3641b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3642889facbeSLen Brown 		return -1;
3643889facbeSLen Brown 	}
3644889facbeSLen Brown 
3645889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3646889facbeSLen Brown 		return 0;
3647889facbeSLen Brown 
3648e9be7dd6SLen Brown 	switch (msr & 0xF) {
3649889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3650889facbeSLen Brown 		epb_string = "performance";
3651889facbeSLen Brown 		break;
3652889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3653889facbeSLen Brown 		epb_string = "balanced";
3654889facbeSLen Brown 		break;
3655889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3656889facbeSLen Brown 		epb_string = "powersave";
3657889facbeSLen Brown 		break;
3658889facbeSLen Brown 	default:
3659889facbeSLen Brown 		epb_string = "custom";
3660889facbeSLen Brown 		break;
3661889facbeSLen Brown 	}
3662b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3663889facbeSLen Brown 
3664889facbeSLen Brown 	return 0;
3665889facbeSLen Brown }
36667f5c258eSLen Brown /*
36677f5c258eSLen Brown  * print_hwp()
36687f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
36697f5c258eSLen Brown  */
36707f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36717f5c258eSLen Brown {
36727f5c258eSLen Brown 	unsigned long long msr;
36737f5c258eSLen Brown 	int cpu;
36747f5c258eSLen Brown 
36757f5c258eSLen Brown 	if (!has_hwp)
36767f5c258eSLen Brown 		return 0;
36777f5c258eSLen Brown 
36787f5c258eSLen Brown 	cpu = t->cpu_id;
36797f5c258eSLen Brown 
36807f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
36817f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36827f5c258eSLen Brown 		return 0;
36837f5c258eSLen Brown 
36847f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3685b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36867f5c258eSLen Brown 		return -1;
36877f5c258eSLen Brown 	}
36887f5c258eSLen Brown 
36897f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
36907f5c258eSLen Brown 		return 0;
36917f5c258eSLen Brown 
3692b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
36937f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
36947f5c258eSLen Brown 
36957f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
36967f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
36977f5c258eSLen Brown 		return 0;
36987f5c258eSLen Brown 
36997f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
37007f5c258eSLen Brown 		return 0;
37017f5c258eSLen Brown 
3702b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
37036dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
37047f5c258eSLen Brown 			cpu, msr,
37057f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
37067f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
37077f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
37087f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
37097f5c258eSLen Brown 
37107f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
37117f5c258eSLen Brown 		return 0;
37127f5c258eSLen Brown 
3713b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
37146dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
37157f5c258eSLen Brown 			cpu, msr,
37167f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
37177f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
37187f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
37197f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
37207f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
37217f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
37227f5c258eSLen Brown 
37237f5c258eSLen Brown 	if (has_hwp_pkg) {
37247f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
37257f5c258eSLen Brown 			return 0;
37267f5c258eSLen Brown 
3727b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
37286dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
37297f5c258eSLen Brown 			cpu, msr,
37307f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
37317f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
37327f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
37337f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
37347f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
37357f5c258eSLen Brown 	}
37367f5c258eSLen Brown 	if (has_hwp_notify) {
37377f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
37387f5c258eSLen Brown 			return 0;
37397f5c258eSLen Brown 
3740b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
37417f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
37427f5c258eSLen Brown 			cpu, msr,
37437f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
37447f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
37457f5c258eSLen Brown 	}
37467f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
37477f5c258eSLen Brown 		return 0;
37487f5c258eSLen Brown 
3749b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
37507f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
37517f5c258eSLen Brown 			cpu, msr,
37527f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
37537f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3754889facbeSLen Brown 
3755889facbeSLen Brown 	return 0;
3756889facbeSLen Brown }
3757889facbeSLen Brown 
37583a9a941dSLen Brown /*
37593a9a941dSLen Brown  * print_perf_limit()
37603a9a941dSLen Brown  */
37613a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
37623a9a941dSLen Brown {
37633a9a941dSLen Brown 	unsigned long long msr;
37643a9a941dSLen Brown 	int cpu;
37653a9a941dSLen Brown 
37663a9a941dSLen Brown 	cpu = t->cpu_id;
37673a9a941dSLen Brown 
37683a9a941dSLen Brown 	/* per-package */
37693a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
37703a9a941dSLen Brown 		return 0;
37713a9a941dSLen Brown 
37723a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3773b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
37743a9a941dSLen Brown 		return -1;
37753a9a941dSLen Brown 	}
37763a9a941dSLen Brown 
37773a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
37783a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3779b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3780b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3781e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
37823a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3783e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3784e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3785e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3786e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3787e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3788e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3789e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3790e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3791e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3792e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3793e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3794e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3795b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3796e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
37973a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3798e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3799e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3800e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3801e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3802e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3803e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3804e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3805e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3806e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3807e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3808e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3809e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
38103a9a941dSLen Brown 
38113a9a941dSLen Brown 	}
38123a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
38133a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3814b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3815b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
38163a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
38173a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
38183a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
38193a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
38203a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
38213a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
38223a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
38233a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3824b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
38253a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
38263a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
38273a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
38283a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
38293a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
38303a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
38313a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
38323a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
38333a9a941dSLen Brown 	}
38343a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
38353a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3836b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3837b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
38383a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
38393a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
38403a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
38413a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
38423a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
38433a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3844b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
38453a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
38463a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
38473a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
38483a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
38493a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
38503a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
38513a9a941dSLen Brown 	}
38523a9a941dSLen Brown 	return 0;
38533a9a941dSLen Brown }
38543a9a941dSLen Brown 
3855889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3856889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3857889facbeSLen Brown 
38589392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
3859144b44b1SLen Brown {
3860144b44b1SLen Brown 	unsigned long long msr;
3861144b44b1SLen Brown 
3862144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
38637ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3864144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3865144b44b1SLen Brown 
3866144b44b1SLen Brown 	switch (model) {
3867f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
38685ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:
3869144b44b1SLen Brown 		return 30.0;
3870144b44b1SLen Brown 	default:
3871144b44b1SLen Brown 		return 135.0;
3872144b44b1SLen Brown 	}
3873144b44b1SLen Brown }
3874144b44b1SLen Brown 
38759392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
38769392bd98SCalvin Walton {
38779392bd98SCalvin Walton 	switch (family) {
38789392bd98SCalvin Walton 	case 0x17:
3879c1c10cc7SPu Wen 	case 0x18:
38809392bd98SCalvin Walton 	default:
38819392bd98SCalvin Walton 		/* This is the max stock TDP of HEDT/Server Fam17h chips */
38829392bd98SCalvin Walton 		return 250.0;
38839392bd98SCalvin Walton 	}
38849392bd98SCalvin Walton }
38859392bd98SCalvin Walton 
388640ee8e3bSAndrey Semin /*
388740ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
388840ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
388940ee8e3bSAndrey Semin  */
389040ee8e3bSAndrey Semin static double
389140ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
389240ee8e3bSAndrey Semin {
389340ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
389440ee8e3bSAndrey Semin 
389540ee8e3bSAndrey Semin 	switch (model) {
3896869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3897869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3898869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
389940ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
390040ee8e3bSAndrey Semin 	default:
390140ee8e3bSAndrey Semin 		return (rapl_energy_units);
390240ee8e3bSAndrey Semin 	}
390340ee8e3bSAndrey Semin }
390440ee8e3bSAndrey Semin 
39059392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
3906889facbeSLen Brown {
3907889facbeSLen Brown 	unsigned long long msr;
3908144b44b1SLen Brown 	unsigned int time_unit;
3909889facbeSLen Brown 	double tdp;
3910889facbeSLen Brown 
3911889facbeSLen Brown 	if (family != 6)
3912889facbeSLen Brown 		return;
3913889facbeSLen Brown 
3914889facbeSLen Brown 	switch (model) {
3915869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3916869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3917c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
391877e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
39195e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3920c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
39215e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3922144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3923812db3f7SLen Brown 		if (rapl_joules) {
3924812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3925812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3926812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3927812db3f7SLen Brown 		} else {
3928812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3929812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3930812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3931812db3f7SLen Brown 		}
3932889facbeSLen Brown 		break;
3933869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3934f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3935e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3936812db3f7SLen Brown 		if (rapl_joules)
3937812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3938812db3f7SLen Brown 		else
3939812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3940e4085d54SLen Brown 		break;
3941f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
3942f6708400SChen Yu 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
3943f6708400SChen Yu 		if (rapl_joules) {
3944f6708400SChen Yu 			BIC_PRESENT(BIC_Pkg_J);
3945f6708400SChen Yu 			BIC_PRESENT(BIC_Cor_J);
3946f6708400SChen Yu 			BIC_PRESENT(BIC_RAM_J);
3947f6708400SChen Yu 			BIC_PRESENT(BIC_GFX_J);
3948f6708400SChen Yu 		} else {
3949f6708400SChen Yu 			BIC_PRESENT(BIC_PkgWatt);
3950f6708400SChen Yu 			BIC_PRESENT(BIC_CorWatt);
3951f6708400SChen Yu 			BIC_PRESENT(BIC_RAMWatt);
3952f6708400SChen Yu 			BIC_PRESENT(BIC_GFXWatt);
3953f6708400SChen Yu 		}
3954f6708400SChen Yu 		break;
3955af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3956af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
395781824921SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
3958812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3959812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3960812db3f7SLen Brown 		if (rapl_joules) {
3961812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3962812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3963812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
396481824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3965812db3f7SLen Brown 		} else {
3966812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3967812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3968812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
396981824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3970812db3f7SLen Brown 		}
39710b2bb692SLen Brown 		break;
3972869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3973869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3974869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3975869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
39760b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3977812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3978812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3979812db3f7SLen Brown 		if (rapl_joules) {
3980812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3981812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3982812db3f7SLen Brown 		} else {
3983812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3984812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3985812db3f7SLen Brown 		}
3986e6f9bb3cSLen Brown 		break;
3987869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3988869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
39890b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
3990812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3991812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3992812db3f7SLen Brown 		if (rapl_joules) {
3993812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3994812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3995812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3996812db3f7SLen Brown 		} else {
3997812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3998812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3999812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4000812db3f7SLen Brown 		}
4001144b44b1SLen Brown 		break;
4002f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
40035ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
4004144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
4005812db3f7SLen Brown 		if (rapl_joules) {
4006812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4007812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4008812db3f7SLen Brown 		} else {
4009812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4010812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4011812db3f7SLen Brown 		}
4012889facbeSLen Brown 		break;
40135ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
40140f644909SJacob Pan 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
4015812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4016812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4017812db3f7SLen Brown 		if (rapl_joules) {
4018812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4019812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4020812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4021812db3f7SLen Brown 		} else {
4022812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4023812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4024812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4025812db3f7SLen Brown 		}
40260f644909SJacob Pan 		break;
4027889facbeSLen Brown 	default:
4028889facbeSLen Brown 		return;
4029889facbeSLen Brown 	}
4030889facbeSLen Brown 
4031889facbeSLen Brown 	/* units on package 0, verify later other packages match */
40327ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4033889facbeSLen Brown 		return;
4034889facbeSLen Brown 
4035889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
4036f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
4037144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4038144b44b1SLen Brown 	else
4039889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4040889facbeSLen Brown 
404140ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
404240ee8e3bSAndrey Semin 
4043144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
4044144b44b1SLen Brown 	if (time_unit == 0)
4045144b44b1SLen Brown 		time_unit = 0xA;
4046889facbeSLen Brown 
4047144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
4048144b44b1SLen Brown 
40499392bd98SCalvin Walton 	tdp = get_tdp_intel(model);
4050889facbeSLen Brown 
4051889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
405296e47158SLen Brown 	if (!quiet)
4053b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
40549392bd98SCalvin Walton }
4055889facbeSLen Brown 
40569392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
40579392bd98SCalvin Walton {
40589392bd98SCalvin Walton 	unsigned long long msr;
40599392bd98SCalvin Walton 	unsigned int eax, ebx, ecx, edx;
40609392bd98SCalvin Walton 	unsigned int has_rapl = 0;
40619392bd98SCalvin Walton 	double tdp;
40629392bd98SCalvin Walton 
40639392bd98SCalvin Walton 	if (max_extended_level >= 0x80000007) {
40649392bd98SCalvin Walton 		__cpuid(0x80000007, eax, ebx, ecx, edx);
40659392bd98SCalvin Walton 		/* RAPL (Fam 17h) */
40669392bd98SCalvin Walton 		has_rapl = edx & (1 << 14);
40679392bd98SCalvin Walton 	}
40689392bd98SCalvin Walton 
40699392bd98SCalvin Walton 	if (!has_rapl)
4070889facbeSLen Brown 		return;
40719392bd98SCalvin Walton 
40729392bd98SCalvin Walton 	switch (family) {
40739392bd98SCalvin Walton 	case 0x17: /* Zen, Zen+ */
4074c1c10cc7SPu Wen 	case 0x18: /* Hygon Dhyana */
40759392bd98SCalvin Walton 		do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
40763316f99aSCalvin Walton 		if (rapl_joules) {
40773316f99aSCalvin Walton 			BIC_PRESENT(BIC_Pkg_J);
40789392bd98SCalvin Walton 			BIC_PRESENT(BIC_Cor_J);
40793316f99aSCalvin Walton 		} else {
40803316f99aSCalvin Walton 			BIC_PRESENT(BIC_PkgWatt);
40819392bd98SCalvin Walton 			BIC_PRESENT(BIC_CorWatt);
40823316f99aSCalvin Walton 		}
40839392bd98SCalvin Walton 		break;
40849392bd98SCalvin Walton 	default:
40859392bd98SCalvin Walton 		return;
40869392bd98SCalvin Walton 	}
40879392bd98SCalvin Walton 
40889392bd98SCalvin Walton 	if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
40899392bd98SCalvin Walton 		return;
40909392bd98SCalvin Walton 
40919392bd98SCalvin Walton 	rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
40929392bd98SCalvin Walton 	rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
40939392bd98SCalvin Walton 	rapl_power_units = ldexp(1.0, -(msr & 0xf));
40949392bd98SCalvin Walton 
40959cfa8e04SPu Wen 	tdp = get_tdp_amd(family);
40969392bd98SCalvin Walton 
40979392bd98SCalvin Walton 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
40989392bd98SCalvin Walton 	if (!quiet)
40999392bd98SCalvin Walton 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
41009392bd98SCalvin Walton }
41019392bd98SCalvin Walton 
41029392bd98SCalvin Walton /*
41039392bd98SCalvin Walton  * rapl_probe()
41049392bd98SCalvin Walton  *
41059392bd98SCalvin Walton  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
41069392bd98SCalvin Walton  */
41079392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
41089392bd98SCalvin Walton {
41099392bd98SCalvin Walton 	if (genuine_intel)
41109392bd98SCalvin Walton 		rapl_probe_intel(family, model);
4111c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine)
41129392bd98SCalvin Walton 		rapl_probe_amd(family, model);
4113889facbeSLen Brown }
4114889facbeSLen Brown 
41151b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
41163a9a941dSLen Brown {
41173a9a941dSLen Brown 	if (!genuine_intel)
41183a9a941dSLen Brown 		return;
41193a9a941dSLen Brown 
41203a9a941dSLen Brown 	if (family != 6)
41213a9a941dSLen Brown 		return;
41223a9a941dSLen Brown 
41233a9a941dSLen Brown 	switch (model) {
4124c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
412577e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
41265e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
41273a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
4128869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
41293a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
41303a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
41313a9a941dSLen Brown 	default:
41323a9a941dSLen Brown 		return;
41333a9a941dSLen Brown 	}
41343a9a941dSLen Brown }
41353a9a941dSLen Brown 
4136ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4137ac980e13SArtem Bityutskiy {
4138ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
4139ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
4140ac980e13SArtem Bityutskiy }
4141ac980e13SArtem Bityutskiy 
4142889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4143889facbeSLen Brown {
4144889facbeSLen Brown 	unsigned long long msr;
4145f4896fa5SLen Brown 	unsigned int dts, dts2;
4146889facbeSLen Brown 	int cpu;
4147889facbeSLen Brown 
4148889facbeSLen Brown 	if (!(do_dts || do_ptm))
4149889facbeSLen Brown 		return 0;
4150889facbeSLen Brown 
4151889facbeSLen Brown 	cpu = t->cpu_id;
4152889facbeSLen Brown 
4153889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
4154889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4155889facbeSLen Brown 		return 0;
4156889facbeSLen Brown 
4157889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4158b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4159889facbeSLen Brown 		return -1;
4160889facbeSLen Brown 	}
4161889facbeSLen Brown 
4162889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4163889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4164889facbeSLen Brown 			return 0;
4165889facbeSLen Brown 
4166889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4167b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
4168889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
4169889facbeSLen Brown 
4170889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4171889facbeSLen Brown 			return 0;
4172889facbeSLen Brown 
4173889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4174889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4175b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4176889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4177889facbeSLen Brown 	}
4178889facbeSLen Brown 
4179889facbeSLen Brown 
4180f4896fa5SLen Brown 	if (do_dts && debug) {
4181889facbeSLen Brown 		unsigned int resolution;
4182889facbeSLen Brown 
4183889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4184889facbeSLen Brown 			return 0;
4185889facbeSLen Brown 
4186889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4187889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4188b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4189889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
4190889facbeSLen Brown 
4191889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4192889facbeSLen Brown 			return 0;
4193889facbeSLen Brown 
4194889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4195889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4196b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4197889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4198889facbeSLen Brown 	}
4199889facbeSLen Brown 
4200889facbeSLen Brown 	return 0;
4201889facbeSLen Brown }
4202889facbeSLen Brown 
4203889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4204889facbeSLen Brown {
4205b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4206889facbeSLen Brown 		cpu, label,
4207889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4208889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4209889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4210889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4211889facbeSLen Brown 
4212889facbeSLen Brown 	return;
4213889facbeSLen Brown }
4214889facbeSLen Brown 
4215889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4216889facbeSLen Brown {
4217889facbeSLen Brown 	unsigned long long msr;
42189392bd98SCalvin Walton 	const char *msr_name;
4219889facbeSLen Brown 	int cpu;
4220889facbeSLen Brown 
4221889facbeSLen Brown 	if (!do_rapl)
4222889facbeSLen Brown 		return 0;
4223889facbeSLen Brown 
4224889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4225889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4226889facbeSLen Brown 		return 0;
4227889facbeSLen Brown 
4228889facbeSLen Brown 	cpu = t->cpu_id;
4229889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4230b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4231889facbeSLen Brown 		return -1;
4232889facbeSLen Brown 	}
4233889facbeSLen Brown 
42349392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
42359392bd98SCalvin Walton 		msr_name = "MSR_RAPL_PWR_UNIT";
42369392bd98SCalvin Walton 		if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
42379392bd98SCalvin Walton 			return -1;
42389392bd98SCalvin Walton 	} else {
42399392bd98SCalvin Walton 		msr_name = "MSR_RAPL_POWER_UNIT";
4240889facbeSLen Brown 		if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4241889facbeSLen Brown 			return -1;
42429392bd98SCalvin Walton 	}
4243889facbeSLen Brown 
42449392bd98SCalvin Walton 	fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4245144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
424696e47158SLen Brown 
4247144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4248144b44b1SLen Brown 
4249889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4250889facbeSLen Brown                 	return -5;
4251889facbeSLen Brown 
4252889facbeSLen Brown 
4253b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4254889facbeSLen Brown 			cpu, msr,
4255889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4256889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4257889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4258889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4259889facbeSLen Brown 
4260144b44b1SLen Brown 	}
4261144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4262144b44b1SLen Brown 
4263889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4264889facbeSLen Brown 			return -9;
4265889facbeSLen Brown 
4266b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4267889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4268889facbeSLen Brown 
4269889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4270b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4271889facbeSLen Brown 			cpu,
4272889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4273889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4274889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4275889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4276889facbeSLen Brown 	}
4277889facbeSLen Brown 
42780b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4279889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4280889facbeSLen Brown                 	return -6;
4281889facbeSLen Brown 
4282b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4283889facbeSLen Brown 			cpu, msr,
4284889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4285889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4286889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4287889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
42880b2bb692SLen Brown 	}
42890b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4290889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4291889facbeSLen Brown 			return -9;
4292b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4293889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4294889facbeSLen Brown 
4295889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4296889facbeSLen Brown 	}
4297144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4298889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4299889facbeSLen Brown 			return -7;
4300889facbeSLen Brown 
4301b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4302144b44b1SLen Brown 	}
43039148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4304889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4305889facbeSLen Brown 			return -9;
4306b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4307889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4308889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4309889facbeSLen Brown 	}
4310889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4311889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4312889facbeSLen Brown 			return -8;
4313889facbeSLen Brown 
4314b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4315889facbeSLen Brown 
4316889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4317889facbeSLen Brown 			return -9;
4318b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4319889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4320889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4321889facbeSLen Brown 	}
4322889facbeSLen Brown 	return 0;
4323889facbeSLen Brown }
4324889facbeSLen Brown 
4325d7899447SLen Brown /*
4326d7899447SLen Brown  * SNB adds support for additional MSRs:
4327d7899447SLen Brown  *
4328d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4329d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4330d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4331d7899447SLen Brown  */
4332103a8feaSLen Brown 
4333d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4334103a8feaSLen Brown {
4335103a8feaSLen Brown 	if (!genuine_intel)
4336103a8feaSLen Brown 		return 0;
4337103a8feaSLen Brown 
4338103a8feaSLen Brown 	switch (model) {
4339869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4340869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4341869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:		/* IVB */
4342869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:		/* IVB Xeon */
4343c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:		/* HSW */
4344869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:		/* HSW */
434577e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:		/* HSW */
43465e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:		/* HSW */
4347c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:		/* BDW */
43485e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:		/* BDW */
4349869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:		/* BDX */
4350af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:		/* SKL */
4351af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:		/* CNL */
4352869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:		/* SKX */
4353869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:		/* BXT */
4354f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
43555ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
4356f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:		/* EHL */
4357103a8feaSLen Brown 		return 1;
4358103a8feaSLen Brown 	}
4359103a8feaSLen Brown 	return 0;
4360103a8feaSLen Brown }
4361103a8feaSLen Brown 
4362d7899447SLen Brown /*
4363570992fcSLen Brown  * HSW ULT added support for C8/C9/C10 MSRs:
4364d7899447SLen Brown  *
4365d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4366d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4367d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
43685a63426eSLen Brown  *
43695a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
43705a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
43715a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
43725a63426eSLen Brown  *
4373d7899447SLen Brown  */
4374570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model)
4375ca58710fSKristen Carlson Accardi {
4376ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4377ca58710fSKristen Carlson Accardi 		return 0;
4378ca58710fSKristen Carlson Accardi 
4379ca58710fSKristen Carlson Accardi 	switch (model) {
438077e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
4381c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
4382af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4383af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
4384869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4385f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4386f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
4387ca58710fSKristen Carlson Accardi 		return 1;
4388ca58710fSKristen Carlson Accardi 	}
4389ca58710fSKristen Carlson Accardi 	return 0;
4390ca58710fSKristen Carlson Accardi }
4391ca58710fSKristen Carlson Accardi 
43920b2bb692SLen Brown /*
43930b2bb692SLen Brown  * SKL adds support for additional MSRS:
43940b2bb692SLen Brown  *
43950b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
43960b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
43970b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
43980b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
43990b2bb692SLen Brown  */
44000b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
44010b2bb692SLen Brown {
44020b2bb692SLen Brown 	if (!genuine_intel)
44030b2bb692SLen Brown 		return 0;
44040b2bb692SLen Brown 
44050b2bb692SLen Brown 	switch (model) {
4406af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4407af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
44080b2bb692SLen Brown 		return 1;
44090b2bb692SLen Brown 	}
44100b2bb692SLen Brown 	return 0;
44110b2bb692SLen Brown }
44120b2bb692SLen Brown 
4413144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4414144b44b1SLen Brown {
4415144b44b1SLen Brown 	if (!genuine_intel)
4416144b44b1SLen Brown 		return 0;
4417144b44b1SLen Brown 	switch (model) {
4418f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
44195ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
4420144b44b1SLen Brown 		return 1;
4421144b44b1SLen Brown 	}
4422144b44b1SLen Brown 	return 0;
4423144b44b1SLen Brown }
4424144b44b1SLen Brown 
4425fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4426fb5d4327SDasaratharaman Chandramouli {
4427fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4428fb5d4327SDasaratharaman Chandramouli 		return 0;
4429fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4430869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4431fb5d4327SDasaratharaman Chandramouli 		return 1;
4432fb5d4327SDasaratharaman Chandramouli 	}
4433fb5d4327SDasaratharaman Chandramouli 	return 0;
4434fb5d4327SDasaratharaman Chandramouli }
4435fb5d4327SDasaratharaman Chandramouli 
4436997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4437997e5395SSrinivas Pandruvada {
4438997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4439997e5395SSrinivas Pandruvada 		return 0;
4440997e5395SSrinivas Pandruvada 
4441997e5395SSrinivas Pandruvada 	switch (model) {
4442af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4443997e5395SSrinivas Pandruvada 		return 1;
4444997e5395SSrinivas Pandruvada 	}
4445997e5395SSrinivas Pandruvada 
4446997e5395SSrinivas Pandruvada 	return 0;
4447997e5395SSrinivas Pandruvada }
4448997e5395SSrinivas Pandruvada 
4449b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4450b2b34dfeSHubert Chrzaniuk {
4451b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4452b2b34dfeSHubert Chrzaniuk 		return 1024;
4453b2b34dfeSHubert Chrzaniuk 	return 1;
4454b2b34dfeSHubert Chrzaniuk }
4455b2b34dfeSHubert Chrzaniuk 
4456144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4457144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4458144b44b1SLen Brown 
4459144b44b1SLen Brown double slm_bclk(void)
4460144b44b1SLen Brown {
4461144b44b1SLen Brown 	unsigned long long msr = 3;
4462144b44b1SLen Brown 	unsigned int i;
4463144b44b1SLen Brown 	double freq;
4464144b44b1SLen Brown 
44657ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4466b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4467144b44b1SLen Brown 
4468144b44b1SLen Brown 	i = msr & 0xf;
4469144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4470b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
44710a91e551SColin Ian King 		i = 3;
4472144b44b1SLen Brown 	}
4473144b44b1SLen Brown 	freq = slm_freq_table[i];
4474144b44b1SLen Brown 
447596e47158SLen Brown 	if (!quiet)
4476b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4477144b44b1SLen Brown 
4478144b44b1SLen Brown 	return freq;
4479144b44b1SLen Brown }
4480144b44b1SLen Brown 
4481103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4482103a8feaSLen Brown {
4483121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4484103a8feaSLen Brown 		return 100.00;
4485144b44b1SLen Brown 	else if (is_slm(family, model))
4486144b44b1SLen Brown 		return slm_bclk();
4487103a8feaSLen Brown 	else
4488103a8feaSLen Brown 		return 133.33;
4489103a8feaSLen Brown }
4490103a8feaSLen Brown 
4491889facbeSLen Brown /*
4492889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4493889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4494889facbeSLen Brown  * This is usually equal to tjMax.
4495889facbeSLen Brown  *
4496889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4497889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4498889facbeSLen Brown  *
4499889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4500889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4501889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4502889facbeSLen Brown  */
4503889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4504889facbeSLen Brown {
4505889facbeSLen Brown 	unsigned long long msr;
4506889facbeSLen Brown 	unsigned int target_c_local;
4507889facbeSLen Brown 	int cpu;
4508889facbeSLen Brown 
4509889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4510889facbeSLen Brown 	if (!(do_dts || do_ptm))
4511889facbeSLen Brown 		return 0;
4512889facbeSLen Brown 
4513889facbeSLen Brown 	/* this is a per-package concept */
4514889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4515889facbeSLen Brown 		return 0;
4516889facbeSLen Brown 
4517889facbeSLen Brown 	cpu = t->cpu_id;
4518889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4519b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4520889facbeSLen Brown 		return -1;
4521889facbeSLen Brown 	}
4522889facbeSLen Brown 
4523889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4524889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4525b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4526889facbeSLen Brown 			cpu, tcc_activation_temp);
4527889facbeSLen Brown 		return 0;
4528889facbeSLen Brown 	}
4529889facbeSLen Brown 
4530889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4531d7899447SLen Brown 	if (!do_nhm_platform_info)
4532889facbeSLen Brown 		goto guess;
4533889facbeSLen Brown 
45347ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4535889facbeSLen Brown 		goto guess;
4536889facbeSLen Brown 
45373482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4538889facbeSLen Brown 
453996e47158SLen Brown 	if (!quiet)
4540b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4541889facbeSLen Brown 			cpu, msr, target_c_local);
4542889facbeSLen Brown 
45433482124aSJean Delvare 	if (!target_c_local)
4544889facbeSLen Brown 		goto guess;
4545889facbeSLen Brown 
4546889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4547889facbeSLen Brown 
4548889facbeSLen Brown 	return 0;
4549889facbeSLen Brown 
4550889facbeSLen Brown guess:
4551889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4552b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4553889facbeSLen Brown 		cpu, tcc_activation_temp);
4554889facbeSLen Brown 
4555889facbeSLen Brown 	return 0;
4556889facbeSLen Brown }
455769807a63SLen Brown 
4558aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4559aa8d8cc7SLen Brown {
4560aa8d8cc7SLen Brown 	unsigned long long msr;
4561aa8d8cc7SLen Brown 
4562f6505c88SSean Christopherson 	if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
4563aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4564aa8d8cc7SLen Brown 			base_cpu, msr,
4565f6505c88SSean Christopherson 			msr & FEAT_CTL_LOCKED ? "" : "UN-",
4566aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4567aa8d8cc7SLen Brown }
4568aa8d8cc7SLen Brown 
456969807a63SLen Brown void decode_misc_enable_msr(void)
457069807a63SLen Brown {
457169807a63SLen Brown 	unsigned long long msr;
457269807a63SLen Brown 
4573f26b1519SLen Brown 	if (!genuine_intel)
4574f26b1519SLen Brown 		return;
4575f26b1519SLen Brown 
457669807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4577e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
457869807a63SLen Brown 			base_cpu, msr,
4579e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4580e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4581fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4582e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4583e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
458469807a63SLen Brown }
458569807a63SLen Brown 
458633148d67SLen Brown void decode_misc_feature_control(void)
458733148d67SLen Brown {
458833148d67SLen Brown 	unsigned long long msr;
458933148d67SLen Brown 
459033148d67SLen Brown 	if (!has_misc_feature_control)
459133148d67SLen Brown 		return;
459233148d67SLen Brown 
459333148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
459433148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
459533148d67SLen Brown 			base_cpu, msr,
459633148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
459733148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
459833148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
459933148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
460033148d67SLen Brown }
4601f0057310SLen Brown /*
4602f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4603f0057310SLen Brown  *
4604f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4605f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4606f0057310SLen Brown  * bit[1] less so...
4607f0057310SLen Brown  */
4608f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4609f0057310SLen Brown {
4610f0057310SLen Brown 	unsigned long long msr;
4611f0057310SLen Brown 
4612f0057310SLen Brown 	if (!do_nhm_platform_info)
4613f0057310SLen Brown 		return;
4614f0057310SLen Brown 
4615cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4616cf4cbe53SLen Brown 		return;
4617cf4cbe53SLen Brown 
4618f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4619ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4620f0057310SLen Brown 			base_cpu, msr,
4621f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4622ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4623ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4624f0057310SLen Brown }
462571616c8eSLen Brown /*
462671616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
462771616c8eSLen Brown  *
462871616c8eSLen Brown  * This MSRs are present on Silvermont processors,
462971616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
463071616c8eSLen Brown  */
463171616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
463271616c8eSLen Brown {
463371616c8eSLen Brown 	unsigned long long msr;
463471616c8eSLen Brown 
463571616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
463671616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
463771616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
463871616c8eSLen Brown 
463971616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
464071616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
464171616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
464271616c8eSLen Brown }
46437f5c258eSLen Brown 
4644f5a4c76aSLen Brown /*
4645f5a4c76aSLen Brown  * When models are the same, for the purpose of turbostat, reuse
4646f5a4c76aSLen Brown  */
4647f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
4648f5a4c76aSLen Brown {
4649f5a4c76aSLen Brown 
4650f5a4c76aSLen Brown 	switch(model) {
4651f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
4652f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
4653f5a4c76aSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
4654f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
4655f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
4656f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM;
4657f5a4c76aSLen Brown 
4658f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
4659f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
4660f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM_EX;
4661f5a4c76aSLen Brown 
4662f5a4c76aSLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4663f5a4c76aSLen Brown 		return INTEL_FAM6_XEON_PHI_KNL;
4664f5a4c76aSLen Brown 
4665f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_X:
46665ebb34edSPeter Zijlstra 	case INTEL_FAM6_BROADWELL_D:	/* BDX-DE */
4667f5a4c76aSLen Brown 		return INTEL_FAM6_BROADWELL_X;
4668f5a4c76aSLen Brown 
4669af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:
4670c66f78a6SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE:
4671af239c44SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE_L:
4672c66f78a6SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE:
4673081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE_L:
4674081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE:
4675af239c44SPeter Zijlstra 		return INTEL_FAM6_SKYLAKE_L;
4676937807d3SLen Brown 
4677af239c44SPeter Zijlstra 	case INTEL_FAM6_ICELAKE_L:
4678d93ea567SRajneesh Bhardwaj 	case INTEL_FAM6_ICELAKE_NNPI:
46794bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE_L:
46804bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE:
4681af239c44SPeter Zijlstra 		return INTEL_FAM6_CANNONLAKE_L;
4682b62b3184SZhang Rui 
468377e5517cSIngo Molnar 	case INTEL_FAM6_ATOM_TREMONT_D:
468477e5517cSIngo Molnar 		return INTEL_FAM6_ATOM_GOLDMONT_D;
468523274fafSChen Yu 
4686d7814c30SChen Yu 	case INTEL_FAM6_ATOM_TREMONT_L:
4687d7814c30SChen Yu 		return INTEL_FAM6_ATOM_TREMONT;
4688d7814c30SChen Yu 
468923274fafSChen Yu 	case INTEL_FAM6_ICELAKE_X:
469023274fafSChen Yu 		return INTEL_FAM6_SKYLAKE_X;
4691f5a4c76aSLen Brown 	}
4692f5a4c76aSLen Brown 	return model;
4693f5a4c76aSLen Brown }
4694d76bb7a0SLen Brown 
4695d76bb7a0SLen Brown void print_dev_latency(void)
4696d76bb7a0SLen Brown {
4697d76bb7a0SLen Brown 	char *path = "/dev/cpu_dma_latency";
4698d76bb7a0SLen Brown 	int fd;
4699d76bb7a0SLen Brown 	int value;
4700d76bb7a0SLen Brown 	int retval;
4701d76bb7a0SLen Brown 
4702d76bb7a0SLen Brown 	fd = open(path, O_RDONLY);
4703d76bb7a0SLen Brown 	if (fd < 0) {
4704d76bb7a0SLen Brown 		warn("fopen %s\n", path);
4705d76bb7a0SLen Brown 		return;
4706d76bb7a0SLen Brown 	}
4707d76bb7a0SLen Brown 
4708d76bb7a0SLen Brown 	retval = read(fd, (void *)&value, sizeof(int));
4709d76bb7a0SLen Brown 	if (retval != sizeof(int)) {
4710d76bb7a0SLen Brown 		warn("read %s\n", path);
4711d76bb7a0SLen Brown 		close(fd);
4712d76bb7a0SLen Brown 		return;
4713d76bb7a0SLen Brown 	}
4714d76bb7a0SLen Brown 	fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n",
4715d76bb7a0SLen Brown 		value, value == 2000000000 ? "default" : "constrained");
4716d76bb7a0SLen Brown 
4717d76bb7a0SLen Brown 	close(fd);
4718d76bb7a0SLen Brown }
4719d76bb7a0SLen Brown 
4720fcd17211SLen Brown void process_cpuid()
4721103a8feaSLen Brown {
472234041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
472334041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
4724b3a34e93SLen Brown 	unsigned int has_turbo;
4725103a8feaSLen Brown 
4726103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4727103a8feaSLen Brown 
47285aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4729103a8feaSLen Brown 
473034041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
4731103a8feaSLen Brown 		genuine_intel = 1;
473234041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
473334041551SLen Brown 		authentic_amd = 1;
4734c1c10cc7SPu Wen 	else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
4735c1c10cc7SPu Wen 		hygon_genuine = 1;
4736103a8feaSLen Brown 
473796e47158SLen Brown 	if (!quiet)
4738b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4739103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4740103a8feaSLen Brown 
47415aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4742103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4743103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4744103a8feaSLen Brown 	stepping = fms & 0xf;
47455aa3d1a2SCalvin Walton 	if (family == 0xf)
47465aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
47475aa3d1a2SCalvin Walton 	if (family >= 6)
4748103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
474934041551SLen Brown 	ecx_flags = ecx;
475034041551SLen Brown 	edx_flags = edx;
4751103a8feaSLen Brown 
4752103a8feaSLen Brown 	/*
4753103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4754103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4755103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4756103a8feaSLen Brown 	 */
4757103a8feaSLen Brown 	ebx = ecx = edx = 0;
47585aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4759103a8feaSLen Brown 
476034041551SLen Brown 	if (!quiet) {
476134041551SLen Brown 		fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
476234041551SLen Brown 			max_level, max_extended_level, family, model, stepping, family, model, stepping);
476334041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
476434041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
476534041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
476634041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
476734041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
476834041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
476934041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
477034041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
477134041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
477234041551SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-",
477334041551SLen Brown 			edx_flags & (1 << 29) ? "TM" : "-");
477434041551SLen Brown 	}
4775f5a4c76aSLen Brown 	if (genuine_intel)
4776f5a4c76aSLen Brown 		model = intel_model_duplicates(model);
477734041551SLen Brown 
477834041551SLen Brown 	if (!(edx_flags & (1 << 5)))
477934041551SLen Brown 		errx(1, "CPUID: no MSR");
478034041551SLen Brown 
478161a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4782103a8feaSLen Brown 
4783103a8feaSLen Brown 		/*
4784103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4785103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4786103a8feaSLen Brown 		 */
47875aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
47888209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4789d7899447SLen Brown 	}
4790103a8feaSLen Brown 
4791103a8feaSLen Brown 	/*
4792103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4793103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4794103a8feaSLen Brown 	 */
4795103a8feaSLen Brown 
47965aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
47978209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4798812db3f7SLen Brown 	if (has_aperf) {
4799812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4800812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4801812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4802812db3f7SLen Brown 	}
4803889facbeSLen Brown 	do_dts = eax & (1 << 0);
4804812db3f7SLen Brown 	if (do_dts)
4805812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4806b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4807889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4808812db3f7SLen Brown 	if (do_ptm)
4809812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
48107f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
48117f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
48127f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
48137f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
48147f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4815889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4816889facbeSLen Brown 
481796e47158SLen Brown 	if (!quiet)
4818b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
48197f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
48207f5c258eSLen Brown 			has_aperf ? "" : "No-",
4821b3a34e93SLen Brown 			has_turbo ? "" : "No-",
48227f5c258eSLen Brown 			do_dts ? "" : "No-",
48237f5c258eSLen Brown 			do_ptm ? "" : "No-",
48247f5c258eSLen Brown 			has_hwp ? "" : "No-",
48257f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
48267f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
48277f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
48287f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
48297f5c258eSLen Brown 			has_epb ? "" : "No-");
4830103a8feaSLen Brown 
483196e47158SLen Brown 	if (!quiet)
483269807a63SLen Brown 		decode_misc_enable_msr();
483369807a63SLen Brown 
483433148d67SLen Brown 
483596e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4836aa8d8cc7SLen Brown 		int has_sgx;
4837aa8d8cc7SLen Brown 
4838aa8d8cc7SLen Brown 		ecx = 0;
4839aa8d8cc7SLen Brown 
4840aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4841aa8d8cc7SLen Brown 
4842aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4843aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4844aa8d8cc7SLen Brown 
4845aa8d8cc7SLen Brown 		if (has_sgx)
4846aa8d8cc7SLen Brown 			decode_feature_control_msr();
4847aa8d8cc7SLen Brown 	}
4848aa8d8cc7SLen Brown 
484961a87ba7SLen Brown 	if (max_level >= 0x15) {
48508a5bdf41SLen Brown 		unsigned int eax_crystal;
48518a5bdf41SLen Brown 		unsigned int ebx_tsc;
48528a5bdf41SLen Brown 
48538a5bdf41SLen Brown 		/*
48548a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
48558a5bdf41SLen Brown 		 */
48568a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
48575aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
48588a5bdf41SLen Brown 
48598a5bdf41SLen Brown 		if (ebx_tsc != 0) {
48608a5bdf41SLen Brown 
486196e47158SLen Brown 			if (!quiet && (ebx != 0))
4862b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
48638a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
48648a5bdf41SLen Brown 
48658a5bdf41SLen Brown 			if (crystal_hz == 0)
48668a5bdf41SLen Brown 				switch(model) {
4867af239c44SPeter Zijlstra 				case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4868e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4869e8efbc80SLen Brown 					break;
48705ebb34edSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
4871ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4872ec53e594SLen Brown 					break;
4873869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4874f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4875e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
48768a5bdf41SLen Brown 					break;
48778a5bdf41SLen Brown 				default:
48788a5bdf41SLen Brown 					crystal_hz = 0;
48798a5bdf41SLen Brown 			}
48808a5bdf41SLen Brown 
48818a5bdf41SLen Brown 			if (crystal_hz) {
48828a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
488396e47158SLen Brown 				if (!quiet)
4884b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
48858a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
48868a5bdf41SLen Brown 			}
48878a5bdf41SLen Brown 		}
48888a5bdf41SLen Brown 	}
488961a87ba7SLen Brown 	if (max_level >= 0x16) {
489061a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
489161a87ba7SLen Brown 
489261a87ba7SLen Brown 		/*
489361a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
489461a87ba7SLen Brown 		 */
489561a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
489661a87ba7SLen Brown 
48975aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
489896e47158SLen Brown 		if (!quiet)
4899b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
490061a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
490161a87ba7SLen Brown 	}
49028a5bdf41SLen Brown 
4903b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4904b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4905b2b34dfeSHubert Chrzaniuk 
4906812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4907812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4908812db3f7SLen Brown 
4909812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4910812db3f7SLen Brown 		do_nhm_platform_info = 1;
4911812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4912812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4913812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4914812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4915812db3f7SLen Brown 	}
4916d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4917812db3f7SLen Brown 
4918812db3f7SLen Brown 	if (do_snb_cstates)
4919812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4920812db3f7SLen Brown 
49215a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
49220f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
49230f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
49240f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
49250f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
49260f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
49270f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
49280f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
49290f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
49300539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
49310f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
49320f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
49330f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
49340f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
49350539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
49360539ba11SLen Brown 		use_c1_residency_msr = 1;
49370539ba11SLen Brown 	}
49387170a374SLen Brown 	if (is_dnv(family, model)) {
49397170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
49407170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
49417170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
49427170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
49437170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
49447170a374SLen Brown 		use_c1_residency_msr = 1;
49457170a374SLen Brown 	}
494634c76197SLen Brown 	if (is_skx(family, model)) {
494734c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
494834c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
494934c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
495034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
495134c76197SLen Brown 	}
4952ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4953ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4954ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4955ade0ebacSLen Brown 	}
4956570992fcSLen Brown 	if (has_c8910_msrs(family, model)) {
49570f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
49580f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
49590f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
49600f47c08dSLen Brown 	}
4961570992fcSLen Brown 	do_irtl_hsw = has_c8910_msrs(family, model);
4962a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4963a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4964a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4965a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4966a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4967a99d8730SLen Brown 	}
4968144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4969fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4970103a8feaSLen Brown 
4971f6708400SChen Yu 	if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) ||
4972f6708400SChen Yu 	    is_ehl(family, model))
4973562855eeSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
4974103a8feaSLen Brown 
497596e47158SLen Brown 	if (!quiet)
4976f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4977f0057310SLen Brown 
497896e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
497971616c8eSLen Brown 		decode_c6_demotion_policy_msr();
498071616c8eSLen Brown 
4981889facbeSLen Brown 	rapl_probe(family, model);
49823a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4983ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4984889facbeSLen Brown 
498596e47158SLen Brown 	if (!quiet)
49861b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4987fcd17211SLen Brown 
498841618e63SLen Brown 	if (!quiet)
4989d76bb7a0SLen Brown 		print_dev_latency();
4990d76bb7a0SLen Brown 	if (!quiet)
499141618e63SLen Brown 		dump_sysfs_cstate_config();
49927293fccdSLen Brown 	if (!quiet)
49937293fccdSLen Brown 		dump_sysfs_pstate_config();
499441618e63SLen Brown 
4995a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4996a2b7b749SLen Brown 		calculate_tsc_tweak();
4997a2b7b749SLen Brown 
4998812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4999812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
5000fdf676e5SLen Brown 
5001812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5002812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
500327d47356SLen Brown 
5004be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5005be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
5006be0e54c4SLen Brown 	else
5007be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
5008be0e54c4SLen Brown 
50091f81c5efSLen Brown 	if (!access(sys_lpi_file_sysfs, R_OK)) {
50101f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_sysfs;
5011be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
50121f81c5efSLen Brown 	} else if (!access(sys_lpi_file_debugfs, R_OK)) {
50131f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_debugfs;
50141f81c5efSLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
50151f81c5efSLen Brown 	} else {
50161f81c5efSLen Brown 		sys_lpi_file_sysfs = NULL;
5017be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
50181f81c5efSLen Brown 	}
5019be0e54c4SLen Brown 
502096e47158SLen Brown 	if (!quiet)
502133148d67SLen Brown 		decode_misc_feature_control();
502233148d67SLen Brown 
5023889facbeSLen Brown 	return;
5024103a8feaSLen Brown }
5025103a8feaSLen Brown 
5026103a8feaSLen Brown /*
5027103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
5028103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
5029103a8feaSLen Brown  */
5030103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
5031103a8feaSLen Brown {
5032103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
5033103a8feaSLen Brown 		return 1;
5034103a8feaSLen Brown 	else
5035103a8feaSLen Brown 		return 0;
5036103a8feaSLen Brown }
5037103a8feaSLen Brown 
5038103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
5039103a8feaSLen Brown {
5040103a8feaSLen Brown 	return 0;
5041103a8feaSLen Brown }
5042103a8feaSLen Brown 
5043c98d5d94SLen Brown void topology_probe()
5044c98d5d94SLen Brown {
5045c98d5d94SLen Brown 	int i;
5046c98d5d94SLen Brown 	int max_core_id = 0;
5047c98d5d94SLen Brown 	int max_package_id = 0;
50486de68fe1SLen Brown 	int max_die_id = 0;
5049c98d5d94SLen Brown 	int max_siblings = 0;
5050c98d5d94SLen Brown 
5051c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
5052843c5791SPrarit Bhargava 	set_max_cpu_num();
5053c98d5d94SLen Brown 	topo.num_cpus = 0;
5054c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
5055c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
5056812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
5057c98d5d94SLen Brown 
5058d8af6f5fSLen Brown 	if (debug > 1)
5059b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5060c98d5d94SLen Brown 
5061c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
5062b2c95d90SJosh Triplett 	if (cpus == NULL)
5063b2c95d90SJosh Triplett 		err(1, "calloc cpus");
5064c98d5d94SLen Brown 
5065c98d5d94SLen Brown 	/*
5066c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
5067c98d5d94SLen Brown 	 */
5068c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5069b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
5070b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5071c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5072c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5073c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
5074c98d5d94SLen Brown 
5075c98d5d94SLen Brown 	/*
50761ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
50771ef7d21aSLen Brown 	 */
50781ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
50791ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
50801ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
50811ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
50821ef7d21aSLen Brown 	}
50831ef7d21aSLen Brown 
50841ef7d21aSLen Brown 	/*
5085c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
5086c98d5d94SLen Brown 	 */
5087c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5088b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
5089b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5090c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5091c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5092c98d5d94SLen Brown 
50938cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
5094c98d5d94SLen Brown 
5095c98d5d94SLen Brown 	/*
5096c98d5d94SLen Brown 	 * For online cpus
5097c98d5d94SLen Brown 	 * find max_core_id, max_package_id
5098c98d5d94SLen Brown 	 */
5099c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
5100c98d5d94SLen Brown 		int siblings;
5101c98d5d94SLen Brown 
5102c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
5103d8af6f5fSLen Brown 			if (debug > 1)
5104b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
5105c98d5d94SLen Brown 			continue;
5106c98d5d94SLen Brown 		}
5107c98d5d94SLen Brown 
51080e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
51090e2d8f05SLen Brown 
51100e2d8f05SLen Brown 		/* get package information */
5111c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
5112c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
5113c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
5114c98d5d94SLen Brown 
51156de68fe1SLen Brown 		/* get die information */
51166de68fe1SLen Brown 		cpus[i].die_id = get_die_id(i);
51176de68fe1SLen Brown 		if (cpus[i].die_id > max_die_id)
51186de68fe1SLen Brown 			max_die_id = cpus[i].die_id;
51196de68fe1SLen Brown 
51200e2d8f05SLen Brown 		/* get numa node information */
5121ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5122ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
5123ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
51240e2d8f05SLen Brown 
51250e2d8f05SLen Brown 		/* get core information */
51260e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
51270e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
51280e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
51290e2d8f05SLen Brown 
51300e2d8f05SLen Brown 		/* get thread information */
51310e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
5132c98d5d94SLen Brown 		if (siblings > max_siblings)
5133c98d5d94SLen Brown 			max_siblings = siblings;
51344f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
51358cb48b32SPrarit Bhargava 			topo.num_cores++;
5136c98d5d94SLen Brown 	}
5137ef605741SPrarit Bhargava 
513870a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
5139d8af6f5fSLen Brown 	if (debug > 1)
5140b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
514170a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
514270a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
5143812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
5144c98d5d94SLen Brown 
51456de68fe1SLen Brown 	topo.num_die = max_die_id + 1;
51466de68fe1SLen Brown 	if (debug > 1)
51476de68fe1SLen Brown 		fprintf(outf, "max_die_id %d, sizing for %d die\n",
51486de68fe1SLen Brown 				max_die_id, topo.num_die);
51496de68fe1SLen Brown 	if (!summary_only && topo.num_die > 1)
51506de68fe1SLen Brown 		BIC_PRESENT(BIC_Die);
51516de68fe1SLen Brown 
5152c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
5153d8af6f5fSLen Brown 	if (debug > 1)
5154b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
5155c98d5d94SLen Brown 			max_package_id, topo.num_packages);
51567da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
5157812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
5158c98d5d94SLen Brown 
5159ef605741SPrarit Bhargava 	set_node_data();
5160ef605741SPrarit Bhargava 	if (debug > 1)
516170a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
516201235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
516301235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
5164ef605741SPrarit Bhargava 
516570a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
5166d8af6f5fSLen Brown 	if (debug > 1)
5167b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
51682ffbb224SPrarit Bhargava 
51692ffbb224SPrarit Bhargava 	if (debug < 1)
51702ffbb224SPrarit Bhargava 		return;
51712ffbb224SPrarit Bhargava 
51722ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
51730ec712e3SLen Brown 		if (cpu_is_not_present(i))
51740ec712e3SLen Brown 			continue;
51752ffbb224SPrarit Bhargava 		fprintf(outf,
51766de68fe1SLen Brown 			"cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
51776de68fe1SLen Brown 			i, cpus[i].physical_package_id, cpus[i].die_id,
51782ffbb224SPrarit Bhargava 			cpus[i].physical_node_id,
51792ffbb224SPrarit Bhargava 			cpus[i].logical_node_id,
51802ffbb224SPrarit Bhargava 			cpus[i].physical_core_id,
51812ffbb224SPrarit Bhargava 			cpus[i].thread_id);
51822ffbb224SPrarit Bhargava 	}
51832ffbb224SPrarit Bhargava 
5184c98d5d94SLen Brown }
5185c98d5d94SLen Brown 
5186c98d5d94SLen Brown void
518740f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
518840f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
5189c98d5d94SLen Brown {
5190c98d5d94SLen Brown 	int i;
519140f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
519240f5cfe7SPrarit Bhargava 			topo.num_packages;
519340f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
5194c98d5d94SLen Brown 
519540f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
5196c98d5d94SLen Brown 	if (*t == NULL)
5197c98d5d94SLen Brown 		goto error;
5198c98d5d94SLen Brown 
519940f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
5200c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
5201c98d5d94SLen Brown 
520240f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
5203c98d5d94SLen Brown 	if (*c == NULL)
5204c98d5d94SLen Brown 		goto error;
5205c98d5d94SLen Brown 
520640f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
5207c98d5d94SLen Brown 		(*c)[i].core_id = -1;
5208c98d5d94SLen Brown 
5209678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
5210c98d5d94SLen Brown 	if (*p == NULL)
5211c98d5d94SLen Brown 		goto error;
5212c98d5d94SLen Brown 
5213c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
5214c98d5d94SLen Brown 		(*p)[i].package_id = i;
5215c98d5d94SLen Brown 
5216c98d5d94SLen Brown 	return;
5217c98d5d94SLen Brown error:
5218b2c95d90SJosh Triplett 	err(1, "calloc counters");
5219c98d5d94SLen Brown }
5220c98d5d94SLen Brown /*
5221c98d5d94SLen Brown  * init_counter()
5222c98d5d94SLen Brown  *
5223c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5224c98d5d94SLen Brown  */
5225c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
52268cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
5227c98d5d94SLen Brown {
52288cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
522940f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
52308cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
52318cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
5232c98d5d94SLen Brown 	struct thread_data *t;
5233c98d5d94SLen Brown 	struct core_data *c;
5234c98d5d94SLen Brown 	struct pkg_data *p;
5235c98d5d94SLen Brown 
523642dd4520SNathan Ciobanu 
523742dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
523842dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
523942dd4520SNathan Ciobanu 	 */
524042dd4520SNathan Ciobanu 	if (node_id < 0)
524142dd4520SNathan Ciobanu 		node_id = 0;
524242dd4520SNathan Ciobanu 
524340f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
524440f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
52458cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
5246c98d5d94SLen Brown 
5247c98d5d94SLen Brown 	t->cpu_id = cpu_id;
52488cb48b32SPrarit Bhargava 	if (thread_id == 0) {
5249c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5250c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
5251c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5252c98d5d94SLen Brown 	}
5253c98d5d94SLen Brown 
52548cb48b32SPrarit Bhargava 	c->core_id = core_id;
52558cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
5256c98d5d94SLen Brown }
5257c98d5d94SLen Brown 
5258c98d5d94SLen Brown 
5259c98d5d94SLen Brown int initialize_counters(int cpu_id)
5260c98d5d94SLen Brown {
52618cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
52628cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
5263c98d5d94SLen Brown 	return 0;
5264c98d5d94SLen Brown }
5265c98d5d94SLen Brown 
5266c98d5d94SLen Brown void allocate_output_buffer()
5267c98d5d94SLen Brown {
5268eeb71c95SNaoya Horiguchi 	output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
5269c98d5d94SLen Brown 	outp = output_buffer;
5270b2c95d90SJosh Triplett 	if (outp == NULL)
5271b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
5272c98d5d94SLen Brown }
527336229897SLen Brown void allocate_fd_percpu(void)
527436229897SLen Brown {
527501a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
527636229897SLen Brown 	if (fd_percpu == NULL)
527736229897SLen Brown 		err(-1, "calloc fd_percpu");
527836229897SLen Brown }
5279562a2d37SLen Brown void allocate_irq_buffers(void)
5280562a2d37SLen Brown {
5281562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5282562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
5283562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
5284c98d5d94SLen Brown 
528501a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5286562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
528701a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
5288562a2d37SLen Brown }
5289c98d5d94SLen Brown void setup_all_buffers(void)
5290c98d5d94SLen Brown {
5291c98d5d94SLen Brown 	topology_probe();
5292562a2d37SLen Brown 	allocate_irq_buffers();
529336229897SLen Brown 	allocate_fd_percpu();
5294c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
5295c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
5296c98d5d94SLen Brown 	allocate_output_buffer();
5297c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
5298c98d5d94SLen Brown }
52993b4d5c7fSAndy Shevchenko 
53007ce7d5deSPrarit Bhargava void set_base_cpu(void)
53017ce7d5deSPrarit Bhargava {
53027ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
53037ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
53047ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
53057ce7d5deSPrarit Bhargava 
53067ce7d5deSPrarit Bhargava 	if (debug > 1)
5307b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
53087ce7d5deSPrarit Bhargava }
53097ce7d5deSPrarit Bhargava 
5310103a8feaSLen Brown void turbostat_init()
5311103a8feaSLen Brown {
53127ce7d5deSPrarit Bhargava 	setup_all_buffers();
53137ce7d5deSPrarit Bhargava 	set_base_cpu();
5314103a8feaSLen Brown 	check_dev_msr();
531598481e79SLen Brown 	check_permissions();
5316fcd17211SLen Brown 	process_cpuid();
5317103a8feaSLen Brown 
5318103a8feaSLen Brown 
531996e47158SLen Brown 	if (!quiet)
53207f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
53217f5c258eSLen Brown 
532296e47158SLen Brown 	if (!quiet)
5323889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5324889facbeSLen Brown 
532596e47158SLen Brown 	if (!quiet)
53263a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
53273a9a941dSLen Brown 
532896e47158SLen Brown 	if (!quiet)
5329889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5330889facbeSLen Brown 
5331889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5332889facbeSLen Brown 
533396e47158SLen Brown 	if (!quiet)
5334889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
53355a63426eSLen Brown 
533696e47158SLen Brown 	if (!quiet && do_irtl_snb)
53375a63426eSLen Brown 		print_irtl();
5338103a8feaSLen Brown }
5339103a8feaSLen Brown 
5340103a8feaSLen Brown int fork_it(char **argv)
5341103a8feaSLen Brown {
5342103a8feaSLen Brown 	pid_t child_pid;
5343d91bb17cSLen Brown 	int status;
5344d15cf7c1SLen Brown 
5345218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5346d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
53474c2122d4SLen Brown 	first_counter_read = 0;
5348d91bb17cSLen Brown 	if (status)
5349d91bb17cSLen Brown 		exit(status);
5350d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5351d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5352103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5353103a8feaSLen Brown 
5354103a8feaSLen Brown 	child_pid = fork();
5355103a8feaSLen Brown 	if (!child_pid) {
5356103a8feaSLen Brown 		/* child */
5357103a8feaSLen Brown 		execvp(argv[0], argv);
53580815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5359103a8feaSLen Brown 	} else {
5360103a8feaSLen Brown 
5361103a8feaSLen Brown 		/* parent */
5362b2c95d90SJosh Triplett 		if (child_pid == -1)
5363b2c95d90SJosh Triplett 			err(1, "fork");
5364103a8feaSLen Brown 
5365103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5366103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5367b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5368b2c95d90SJosh Triplett 			err(status, "waitpid");
53692a954966SDavid Arcari 
53702a954966SDavid Arcari 		if (WIFEXITED(status))
53712a954966SDavid Arcari 			status = WEXITSTATUS(status);
5372103a8feaSLen Brown 	}
5373c98d5d94SLen Brown 	/*
5374c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5375c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5376c98d5d94SLen Brown 	 */
5377218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5378c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5379103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5380103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5381ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5382ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5383ba3dec99SLen Brown 	else {
5384c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5385c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5386ba3dec99SLen Brown 	}
5387103a8feaSLen Brown 
5388b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5389b7d8c148SLen Brown 
5390b7d8c148SLen Brown 	flush_output_stderr();
5391103a8feaSLen Brown 
5392d91bb17cSLen Brown 	return status;
5393103a8feaSLen Brown }
5394103a8feaSLen Brown 
53953b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
53963b4d5c7fSAndy Shevchenko {
53973b4d5c7fSAndy Shevchenko 	int status;
53983b4d5c7fSAndy Shevchenko 
5399218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
54003b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
54013b4d5c7fSAndy Shevchenko 	if (status)
54023b4d5c7fSAndy Shevchenko 		return status;
54033b4d5c7fSAndy Shevchenko 
54043b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
54053b4d5c7fSAndy Shevchenko 	if (status)
54063b4d5c7fSAndy Shevchenko 		return status;
54073b4d5c7fSAndy Shevchenko 
5408b7d8c148SLen Brown 	flush_output_stdout();
54093b4d5c7fSAndy Shevchenko 
54103b4d5c7fSAndy Shevchenko 	return status;
54113b4d5c7fSAndy Shevchenko }
54123b4d5c7fSAndy Shevchenko 
5413d8af6f5fSLen Brown void print_version() {
5414b95fffb9SLen Brown 	fprintf(outf, "turbostat version 20.03.20"
5415d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5416d8af6f5fSLen Brown }
5417d8af6f5fSLen Brown 
5418495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5419495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
542041618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5421388e9c81SLen Brown {
5422388e9c81SLen Brown 	struct msr_counter *msrp;
5423388e9c81SLen Brown 
5424388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5425388e9c81SLen Brown 	if (msrp == NULL) {
5426388e9c81SLen Brown 		perror("calloc");
5427388e9c81SLen Brown 		exit(1);
5428388e9c81SLen Brown 	}
5429388e9c81SLen Brown 
5430388e9c81SLen Brown 	msrp->msr_num = msr_num;
5431d8d005baSLen Brown 	strncpy(msrp->name, name, NAME_BYTES - 1);
5432495c7654SLen Brown 	if (path)
5433d8d005baSLen Brown 		strncpy(msrp->path, path, PATH_BYTES - 1);
5434388e9c81SLen Brown 	msrp->width = width;
5435388e9c81SLen Brown 	msrp->type = type;
5436388e9c81SLen Brown 	msrp->format = format;
543741618e63SLen Brown 	msrp->flags = flags;
5438388e9c81SLen Brown 
5439388e9c81SLen Brown 	switch (scope) {
5440388e9c81SLen Brown 
5441388e9c81SLen Brown 	case SCOPE_CPU:
5442388e9c81SLen Brown 		msrp->next = sys.tp;
5443388e9c81SLen Brown 		sys.tp = msrp;
5444678a3bd1SLen Brown 		sys.added_thread_counters++;
54450748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5446678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5447678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5448678a3bd1SLen Brown 			exit(-1);
5449678a3bd1SLen Brown 		}
5450388e9c81SLen Brown 		break;
5451388e9c81SLen Brown 
5452388e9c81SLen Brown 	case SCOPE_CORE:
5453388e9c81SLen Brown 		msrp->next = sys.cp;
5454388e9c81SLen Brown 		sys.cp = msrp;
5455678a3bd1SLen Brown 		sys.added_core_counters++;
5456678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5457678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5458678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5459678a3bd1SLen Brown 			exit(-1);
5460678a3bd1SLen Brown 		}
5461388e9c81SLen Brown 		break;
5462388e9c81SLen Brown 
5463388e9c81SLen Brown 	case SCOPE_PACKAGE:
5464388e9c81SLen Brown 		msrp->next = sys.pp;
5465388e9c81SLen Brown 		sys.pp = msrp;
5466678a3bd1SLen Brown 		sys.added_package_counters++;
5467678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5468678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5469678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5470678a3bd1SLen Brown 			exit(-1);
5471678a3bd1SLen Brown 		}
5472388e9c81SLen Brown 		break;
5473388e9c81SLen Brown 	}
5474388e9c81SLen Brown 
5475388e9c81SLen Brown 	return 0;
5476388e9c81SLen Brown }
5477388e9c81SLen Brown 
5478388e9c81SLen Brown void parse_add_command(char *add_command)
5479388e9c81SLen Brown {
5480388e9c81SLen Brown 	int msr_num = 0;
5481495c7654SLen Brown 	char *path = NULL;
54820f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5483388e9c81SLen Brown 	int width = 64;
5484388e9c81SLen Brown 	int fail = 0;
5485388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5486388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5487388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5488388e9c81SLen Brown 
5489388e9c81SLen Brown 	while (add_command) {
5490388e9c81SLen Brown 
5491388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5492388e9c81SLen Brown 			goto next;
5493388e9c81SLen Brown 
5494388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5495388e9c81SLen Brown 			goto next;
5496388e9c81SLen Brown 
5497495c7654SLen Brown 		if (*add_command == '/') {
5498495c7654SLen Brown 			path = add_command;
5499495c7654SLen Brown 			goto next;
5500495c7654SLen Brown 		}
5501495c7654SLen Brown 
5502388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5503388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5504388e9c81SLen Brown 				goto next;
5505388e9c81SLen Brown 			width = 64;
5506388e9c81SLen Brown 		}
5507388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5508388e9c81SLen Brown 			scope = SCOPE_CPU;
5509388e9c81SLen Brown 			goto next;
5510388e9c81SLen Brown 		}
5511388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5512388e9c81SLen Brown 			scope = SCOPE_CORE;
5513388e9c81SLen Brown 			goto next;
5514388e9c81SLen Brown 		}
5515388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5516388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5517388e9c81SLen Brown 			goto next;
5518388e9c81SLen Brown 		}
5519388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5520388e9c81SLen Brown 			type = COUNTER_CYCLES;
5521388e9c81SLen Brown 			goto next;
5522388e9c81SLen Brown 		}
5523388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5524388e9c81SLen Brown 			type = COUNTER_SECONDS;
5525388e9c81SLen Brown 			goto next;
5526388e9c81SLen Brown 		}
552741618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
552841618e63SLen Brown 			type = COUNTER_USEC;
552941618e63SLen Brown 			goto next;
553041618e63SLen Brown 		}
5531388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5532388e9c81SLen Brown 			format = FORMAT_RAW;
5533388e9c81SLen Brown 			goto next;
5534388e9c81SLen Brown 		}
5535388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5536388e9c81SLen Brown 			format = FORMAT_DELTA;
5537388e9c81SLen Brown 			goto next;
5538388e9c81SLen Brown 		}
5539388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5540388e9c81SLen Brown 			format = FORMAT_PERCENT;
5541388e9c81SLen Brown 			goto next;
5542388e9c81SLen Brown 		}
5543388e9c81SLen Brown 
5544388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5545388e9c81SLen Brown 			char *eos;
5546388e9c81SLen Brown 
5547388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5548388e9c81SLen Brown 			if (eos)
5549388e9c81SLen Brown 				*eos = '\0';
5550388e9c81SLen Brown 			goto next;
5551388e9c81SLen Brown 		}
5552388e9c81SLen Brown 
5553388e9c81SLen Brown next:
5554388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5555495c7654SLen Brown 		if (add_command) {
5556495c7654SLen Brown 			*add_command = '\0';
5557388e9c81SLen Brown 			add_command++;
5558495c7654SLen Brown 		}
5559388e9c81SLen Brown 
5560388e9c81SLen Brown 	}
5561495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5562495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5563388e9c81SLen Brown 		fail++;
5564388e9c81SLen Brown 	}
5565388e9c81SLen Brown 
5566388e9c81SLen Brown 	/* generate default column header */
5567388e9c81SLen Brown 	if (*name_buffer == '\0') {
5568388e9c81SLen Brown 		if (width == 32)
55695f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5570388e9c81SLen Brown 		else
55715f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5572388e9c81SLen Brown 	}
5573388e9c81SLen Brown 
557441618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5575388e9c81SLen Brown 		fail++;
5576388e9c81SLen Brown 
5577388e9c81SLen Brown 	if (fail) {
5578388e9c81SLen Brown 		help();
5579388e9c81SLen Brown 		exit(1);
5580388e9c81SLen Brown 	}
5581388e9c81SLen Brown }
558241618e63SLen Brown 
5583dd778a5eSLen Brown int is_deferred_skip(char *name)
5584dd778a5eSLen Brown {
5585dd778a5eSLen Brown 	int i;
5586dd778a5eSLen Brown 
5587dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5588dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5589dd778a5eSLen Brown 			return 1;
5590dd778a5eSLen Brown 	return 0;
5591dd778a5eSLen Brown }
5592dd778a5eSLen Brown 
559341618e63SLen Brown void probe_sysfs(void)
559441618e63SLen Brown {
559541618e63SLen Brown 	char path[64];
559641618e63SLen Brown 	char name_buf[16];
559741618e63SLen Brown 	FILE *input;
559841618e63SLen Brown 	int state;
559941618e63SLen Brown 	char *sp;
560041618e63SLen Brown 
560141618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
560241618e63SLen Brown 		return;
560341618e63SLen Brown 
56040748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
560541618e63SLen Brown 
560641618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
560741618e63SLen Brown 			base_cpu, state);
560841618e63SLen Brown 		input = fopen(path, "r");
560941618e63SLen Brown 		if (input == NULL)
561041618e63SLen Brown 			continue;
56118173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
56128173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
561341618e63SLen Brown 
561441618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
561541618e63SLen Brown 		sp = strchr(name_buf, '-');
561641618e63SLen Brown 		if (!sp)
561741618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
561841618e63SLen Brown 		*sp = '%';
561941618e63SLen Brown 		*(sp + 1) = '\0';
562041618e63SLen Brown 
562141618e63SLen Brown 		fclose(input);
562241618e63SLen Brown 
562341618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
562441618e63SLen Brown 
5625dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5626dd778a5eSLen Brown 			continue;
5627dd778a5eSLen Brown 
562841618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
562941618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
563041618e63SLen Brown 	}
563141618e63SLen Brown 
56320748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
563341618e63SLen Brown 
563441618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
563541618e63SLen Brown 			base_cpu, state);
563641618e63SLen Brown 		input = fopen(path, "r");
563741618e63SLen Brown 		if (input == NULL)
563841618e63SLen Brown 			continue;
56398173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
56408173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
564141618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
564241618e63SLen Brown 		sp = strchr(name_buf, '-');
564341618e63SLen Brown 		if (!sp)
564441618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
564541618e63SLen Brown 		*sp = '\0';
564641618e63SLen Brown 		fclose(input);
564741618e63SLen Brown 
564841618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
564941618e63SLen Brown 
5650dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5651dd778a5eSLen Brown 			continue;
5652dd778a5eSLen Brown 
565341618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
565441618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
565541618e63SLen Brown 	}
565641618e63SLen Brown 
565741618e63SLen Brown }
565841618e63SLen Brown 
56591ef7d21aSLen Brown 
56601ef7d21aSLen Brown /*
56611ef7d21aSLen Brown  * parse cpuset with following syntax
56621ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
56631ef7d21aSLen Brown  */
56641ef7d21aSLen Brown void parse_cpu_command(char *optarg)
56651ef7d21aSLen Brown {
56661ef7d21aSLen Brown 	unsigned int start, end;
56671ef7d21aSLen Brown 	char *next;
56681ef7d21aSLen Brown 
56694e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
56704e4e1e7cSLen Brown 		if (cpu_subset)
56714e4e1e7cSLen Brown 			goto error;
56724e4e1e7cSLen Brown 		show_core_only++;
56734e4e1e7cSLen Brown 		return;
56744e4e1e7cSLen Brown 	}
56754e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
56764e4e1e7cSLen Brown 		if (cpu_subset)
56774e4e1e7cSLen Brown 			goto error;
56784e4e1e7cSLen Brown 		show_pkg_only++;
56794e4e1e7cSLen Brown 		return;
56804e4e1e7cSLen Brown 	}
56814e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
56824e4e1e7cSLen Brown 		goto error;
56834e4e1e7cSLen Brown 
56841ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
56851ef7d21aSLen Brown 	if (cpu_subset == NULL)
56861ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
56871ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
56881ef7d21aSLen Brown 
56891ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
56901ef7d21aSLen Brown 
56911ef7d21aSLen Brown 	next = optarg;
56921ef7d21aSLen Brown 
56931ef7d21aSLen Brown 	while (next && *next) {
56941ef7d21aSLen Brown 
56951ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
56961ef7d21aSLen Brown 			goto error;
56971ef7d21aSLen Brown 
56981ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
56991ef7d21aSLen Brown 
57001ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
57011ef7d21aSLen Brown 			goto error;
57021ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
57031ef7d21aSLen Brown 
57041ef7d21aSLen Brown 		if (*next == '\0')
57051ef7d21aSLen Brown 			break;
57061ef7d21aSLen Brown 
57071ef7d21aSLen Brown 		if (*next == ',') {
57081ef7d21aSLen Brown 			next += 1;
57091ef7d21aSLen Brown 			continue;
57101ef7d21aSLen Brown 		}
57111ef7d21aSLen Brown 
57121ef7d21aSLen Brown 		if (*next == '-') {
57131ef7d21aSLen Brown 			next += 1;	/* start range */
57141ef7d21aSLen Brown 		} else if (*next == '.') {
57151ef7d21aSLen Brown 			next += 1;
57161ef7d21aSLen Brown 			if (*next == '.')
57171ef7d21aSLen Brown 				next += 1;	/* start range */
57181ef7d21aSLen Brown 			else
57191ef7d21aSLen Brown 				goto error;
57201ef7d21aSLen Brown 		}
57211ef7d21aSLen Brown 
57221ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
57231ef7d21aSLen Brown 		if (end <= start)
57241ef7d21aSLen Brown 			goto error;
57251ef7d21aSLen Brown 
57261ef7d21aSLen Brown 		while (++start <= end) {
57271ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
57281ef7d21aSLen Brown 				goto error;
57291ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
57301ef7d21aSLen Brown 		}
57311ef7d21aSLen Brown 
57321ef7d21aSLen Brown 		if (*next == ',')
57331ef7d21aSLen Brown 			next += 1;
57341ef7d21aSLen Brown 		else if (*next != '\0')
57351ef7d21aSLen Brown 			goto error;
57361ef7d21aSLen Brown 	}
57371ef7d21aSLen Brown 
57381ef7d21aSLen Brown 	return;
57391ef7d21aSLen Brown 
57401ef7d21aSLen Brown error:
57414e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
57424e4e1e7cSLen Brown 	help();
57431ef7d21aSLen Brown 	exit(-1);
57441ef7d21aSLen Brown }
57451ef7d21aSLen Brown 
5746812db3f7SLen Brown 
5747103a8feaSLen Brown void cmdline(int argc, char **argv)
5748103a8feaSLen Brown {
5749103a8feaSLen Brown 	int opt;
5750d8af6f5fSLen Brown 	int option_index = 0;
5751d8af6f5fSLen Brown 	static struct option long_options[] = {
5752388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
57531ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5754d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
575596e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
57563f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5757d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5758023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5759d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5760812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5761d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5762c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5763b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
576496e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5765812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5766d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5767d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5768d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5769d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5770d8af6f5fSLen Brown 	};
5771103a8feaSLen Brown 
5772103a8feaSLen Brown 	progname = argv[0];
5773103a8feaSLen Brown 
5774023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5775d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5776103a8feaSLen Brown 		switch (opt) {
5777388e9c81SLen Brown 		case 'a':
5778388e9c81SLen Brown 			parse_add_command(optarg);
5779388e9c81SLen Brown 			break;
57801ef7d21aSLen Brown 		case 'c':
57811ef7d21aSLen Brown 			parse_cpu_command(optarg);
57821ef7d21aSLen Brown 			break;
5783d8af6f5fSLen Brown 		case 'D':
5784d8af6f5fSLen Brown 			dump_only++;
57858e180f3cSLen Brown 			break;
57863f44a5c6SLen Brown 		case 'e':
57873f44a5c6SLen Brown 			/* --enable specified counter */
57884c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
57893f44a5c6SLen Brown 			break;
5790d8af6f5fSLen Brown 		case 'd':
5791d8af6f5fSLen Brown 			debug++;
57923f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
57932f32edf1SLen Brown 			break;
5794812db3f7SLen Brown 		case 'H':
57953f44a5c6SLen Brown 			/*
57963f44a5c6SLen Brown 			 * --hide: do not show those specified
57973f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
57983f44a5c6SLen Brown 			 */
57993f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5800812db3f7SLen Brown 			break;
5801d8af6f5fSLen Brown 		case 'h':
5802d8af6f5fSLen Brown 		default:
5803d8af6f5fSLen Brown 			help();
5804d8af6f5fSLen Brown 			exit(1);
5805d8af6f5fSLen Brown 		case 'i':
58062a0609c0SLen Brown 			{
58072a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
58082a0609c0SLen Brown 
58092a0609c0SLen Brown 				if (interval < 0.001) {
5810b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
58112a0609c0SLen Brown 						interval);
58122a0609c0SLen Brown 					exit(2);
58132a0609c0SLen Brown 				}
58142a0609c0SLen Brown 
581547936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5816b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
581747936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
58182a0609c0SLen Brown 			}
5819889facbeSLen Brown 			break;
58205c56be9aSDirk Brandewie 		case 'J':
58215c56be9aSDirk Brandewie 			rapl_joules++;
58225c56be9aSDirk Brandewie 			break;
5823c8ade361SLen Brown 		case 'l':
58243f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5825c8ade361SLen Brown 			list_header_only++;
5826c8ade361SLen Brown 			quiet++;
5827c8ade361SLen Brown 			break;
5828b7d8c148SLen Brown 		case 'o':
5829b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5830b7d8c148SLen Brown 			break;
583196e47158SLen Brown 		case 'q':
583296e47158SLen Brown 			quiet = 1;
583396e47158SLen Brown 			break;
5834023fe0acSChen Yu 		case 'n':
5835023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5836023fe0acSChen Yu 
5837023fe0acSChen Yu 			if (num_iterations <= 0) {
5838023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5839023fe0acSChen Yu 					num_iterations);
5840023fe0acSChen Yu 				exit(2);
5841023fe0acSChen Yu 			}
5842023fe0acSChen Yu 			break;
5843812db3f7SLen Brown 		case 's':
58443f44a5c6SLen Brown 			/*
58453f44a5c6SLen Brown 			 * --show: show only those specified
58463f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
58473f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
58483f44a5c6SLen Brown 			 */
58493f44a5c6SLen Brown 			if (shown == 0)
58503f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
58513f44a5c6SLen Brown 			else
58523f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
58533f44a5c6SLen Brown 			shown = 1;
5854812db3f7SLen Brown 			break;
5855d8af6f5fSLen Brown 		case 'S':
5856d8af6f5fSLen Brown 			summary_only++;
5857d8af6f5fSLen Brown 			break;
5858d8af6f5fSLen Brown 		case 'T':
5859d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5860d8af6f5fSLen Brown 			break;
5861d8af6f5fSLen Brown 		case 'v':
5862d8af6f5fSLen Brown 			print_version();
5863d8af6f5fSLen Brown 			exit(0);
5864d8af6f5fSLen Brown 			break;
5865103a8feaSLen Brown 		}
5866103a8feaSLen Brown 	}
5867103a8feaSLen Brown }
5868103a8feaSLen Brown 
5869103a8feaSLen Brown int main(int argc, char **argv)
5870103a8feaSLen Brown {
5871b7d8c148SLen Brown 	outf = stderr;
5872103a8feaSLen Brown 	cmdline(argc, argv);
5873103a8feaSLen Brown 
587496e47158SLen Brown 	if (!quiet)
5875d8af6f5fSLen Brown 		print_version();
5876103a8feaSLen Brown 
587741618e63SLen Brown 	probe_sysfs();
587841618e63SLen Brown 
5879103a8feaSLen Brown 	turbostat_init();
5880103a8feaSLen Brown 
58813b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
58823b4d5c7fSAndy Shevchenko 	if (dump_only)
58833b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
58843b4d5c7fSAndy Shevchenko 
5885c8ade361SLen Brown 	/* list header and exit */
5886c8ade361SLen Brown 	if (list_header_only) {
5887c8ade361SLen Brown 		print_header(",");
5888c8ade361SLen Brown 		flush_output_stdout();
5889c8ade361SLen Brown 		return 0;
5890c8ade361SLen Brown 	}
5891c8ade361SLen Brown 
5892103a8feaSLen Brown 	/*
5893103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5894103a8feaSLen Brown 	 */
5895103a8feaSLen Brown 	if (argc - optind)
5896103a8feaSLen Brown 		return fork_it(argv + optind);
5897103a8feaSLen Brown 	else
5898103a8feaSLen Brown 		turbostat_loop();
5899103a8feaSLen Brown 
5900103a8feaSLen Brown 	return 0;
5901103a8feaSLen Brown }
5902