xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision fcaa681c03ea82193e60d7f2cdfd94fbbcd4cae9)
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>
33*fcaa681cSLen 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;
214889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
215889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
216889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
217889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
218889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
219889facbeSLen Brown 	unsigned int 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);
8613b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
8623b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
8633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
8643b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
8653b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
8663b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
8673b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\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)			\
1213889facbeSLen Brown 	if (new > old) {			\
1214889facbeSLen Brown 		old = new - old;		\
1215889facbeSLen Brown 	} else {				\
1216889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1217889facbeSLen Brown 	}
1218889facbeSLen Brown 
1219ba3dec99SLen Brown int
1220c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1221103a8feaSLen Brown {
1222388e9c81SLen Brown 	int i;
1223388e9c81SLen Brown 	struct msr_counter *mp;
12240b2bb692SLen Brown 
1225a99d8730SLen Brown 
1226a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
12270b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1228a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
12290b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1230a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
12310b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1232a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
12330b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1234a99d8730SLen Brown 
1235c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
12360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1237c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
12380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1239c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
12400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1241c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1242ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1243ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1244ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1245be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1246be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1247889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1248889facbeSLen Brown 
12499185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
12509185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12519185e988SLen Brown 		old->gfx_rc6_ms = -1;
12529185e988SLen Brown 	else
1253fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12549185e988SLen Brown 
125527d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
125627d47356SLen Brown 
1257889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1258889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1259889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1260889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1261889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1262889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1263ba3dec99SLen Brown 
1264388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1265388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1266388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1267388e9c81SLen Brown 		else
1268388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1269388e9c81SLen Brown 	}
1270388e9c81SLen Brown 
1271ba3dec99SLen Brown 	return 0;
1272103a8feaSLen Brown }
1273103a8feaSLen Brown 
1274c98d5d94SLen Brown void
1275c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1276c98d5d94SLen Brown {
1277388e9c81SLen Brown 	int i;
1278388e9c81SLen Brown 	struct msr_counter *mp;
1279388e9c81SLen Brown 
1280c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1281c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1282c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1283889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12840539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1285388e9c81SLen Brown 
12869392bd98SCalvin Walton 	DELTA_WRAP32(new->core_energy, old->core_energy);
12879392bd98SCalvin Walton 
1288388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1289388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1290388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1291388e9c81SLen Brown 		else
1292388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1293388e9c81SLen Brown 	}
1294103a8feaSLen Brown }
1295c98d5d94SLen Brown 
12961e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic)
12971e9042b9SSrinivas Pandruvada {
12981e9042b9SSrinivas Pandruvada 	if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
12991e9042b9SSrinivas Pandruvada 		return 0;
13001e9042b9SSrinivas Pandruvada 
13011e9042b9SSrinivas Pandruvada 	return DO_BIC_READ(bic);
13021e9042b9SSrinivas Pandruvada }
13031e9042b9SSrinivas Pandruvada 
1304c3ae331dSLen Brown /*
1305c3ae331dSLen Brown  * old = new - old
1306c3ae331dSLen Brown  */
1307ba3dec99SLen Brown int
1308c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1309c98d5d94SLen Brown 	struct core_data *core_delta)
1310c98d5d94SLen Brown {
1311388e9c81SLen Brown 	int i;
1312388e9c81SLen Brown 	struct msr_counter *mp;
1313388e9c81SLen Brown 
13144c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
13154c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
13164c2122d4SLen Brown 		new->apic_id = old->apic_id;
13174c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
13184c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
13194c2122d4SLen Brown 
13203f44a5c6SLen Brown 	/*
13213f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
13223f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
13233f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
13243f44a5c6SLen Brown 	 */
13253f44a5c6SLen Brown 
1326d4794f25SYazen Ghannam 	timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
13273f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
13283f44a5c6SLen Brown 	old->tv_end = new->tv_end;
13293f44a5c6SLen Brown 
1330c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1331c98d5d94SLen Brown 
1332103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1333b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1334b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1335b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1336b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1337103a8feaSLen Brown 
1338c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1339c98d5d94SLen Brown 
13401e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
13411e9042b9SSrinivas Pandruvada 	    soft_c1_residency_display(BIC_Avg_MHz)) {
1342c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1343c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1344c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1345c98d5d94SLen Brown 		} else {
1346ba3dec99SLen Brown 			return -1;
1347103a8feaSLen Brown 		}
1348a729617cSLen Brown 	}
1349103a8feaSLen Brown 
1350c98d5d94SLen Brown 
1351144b44b1SLen Brown 	if (use_c1_residency_msr) {
1352144b44b1SLen Brown 		/*
1353144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1354144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1355144b44b1SLen Brown 		 */
1356144b44b1SLen Brown 	} else {
1357103a8feaSLen Brown 		/*
1358c3ae331dSLen Brown 		 * As counter collection is not atomic,
1359c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1360103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1361103a8feaSLen Brown 		 */
136295149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1363c98d5d94SLen Brown 			old->c1 = 0;
1364c98d5d94SLen Brown 		else {
1365c98d5d94SLen Brown 			/* normal case, derive c1 */
1366008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1367c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1368c98d5d94SLen Brown 		}
1369144b44b1SLen Brown 	}
1370c3ae331dSLen Brown 
1371c98d5d94SLen Brown 	if (old->mperf == 0) {
1372b7d8c148SLen Brown 		if (debug > 1)
1373b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1374c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1375c98d5d94SLen Brown 	}
1376103a8feaSLen Brown 
1377812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1378562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1379562a2d37SLen Brown 
1380812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13811ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1382ba3dec99SLen Brown 
1383388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1384388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1385388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1386388e9c81SLen Brown 		else
1387388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1388388e9c81SLen Brown 	}
1389ba3dec99SLen Brown 	return 0;
1390103a8feaSLen Brown }
1391c98d5d94SLen Brown 
1392c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1393c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1394c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1395c98d5d94SLen Brown {
1396ba3dec99SLen Brown 	int retval = 0;
1397ba3dec99SLen Brown 
1398c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1399c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1400c98d5d94SLen Brown 		delta_core(c, c2);
1401c98d5d94SLen Brown 
1402c98d5d94SLen Brown 	/* always calculate thread delta */
1403ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1404ba3dec99SLen Brown 	if (retval)
1405ba3dec99SLen Brown 		return retval;
1406c98d5d94SLen Brown 
1407c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1408c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1409ba3dec99SLen Brown 		retval = delta_package(p, p2);
1410c98d5d94SLen Brown 
1411ba3dec99SLen Brown 	return retval;
1412103a8feaSLen Brown }
1413103a8feaSLen Brown 
1414c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1415103a8feaSLen Brown {
1416388e9c81SLen Brown 	int i;
1417388e9c81SLen Brown 	struct msr_counter  *mp;
1418388e9c81SLen Brown 
14193f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
14203f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
14213f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
14223f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
1423d4794f25SYazen Ghannam 	t->tv_delta.tv_sec = 0;
1424d4794f25SYazen Ghannam 	t->tv_delta.tv_usec = 0;
14253f44a5c6SLen Brown 
1426c98d5d94SLen Brown 	t->tsc = 0;
1427c98d5d94SLen Brown 	t->aperf = 0;
1428c98d5d94SLen Brown 	t->mperf = 0;
1429c98d5d94SLen Brown 	t->c1 = 0;
1430103a8feaSLen Brown 
1431562a2d37SLen Brown 	t->irq_count = 0;
1432562a2d37SLen Brown 	t->smi_count = 0;
1433562a2d37SLen Brown 
1434c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1435c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1436c98d5d94SLen Brown 
1437c98d5d94SLen Brown 	c->c3 = 0;
1438c98d5d94SLen Brown 	c->c6 = 0;
1439c98d5d94SLen Brown 	c->c7 = 0;
14400539ba11SLen Brown 	c->mc6_us = 0;
1441889facbeSLen Brown 	c->core_temp_c = 0;
14429392bd98SCalvin Walton 	c->core_energy = 0;
1443c98d5d94SLen Brown 
14440b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
14450b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
14460b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
14470b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
14480b2bb692SLen Brown 
1449c98d5d94SLen Brown 	p->pc2 = 0;
14500f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1451c98d5d94SLen Brown 		p->pc3 = 0;
14520f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1453c98d5d94SLen Brown 		p->pc6 = 0;
14540f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1455c98d5d94SLen Brown 		p->pc7 = 0;
1456ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1457ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1458ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1459be0e54c4SLen Brown 	p->cpu_lpi = 0;
1460be0e54c4SLen Brown 	p->sys_lpi = 0;
1461889facbeSLen Brown 
1462889facbeSLen Brown 	p->energy_pkg = 0;
1463889facbeSLen Brown 	p->energy_dram = 0;
1464889facbeSLen Brown 	p->energy_cores = 0;
1465889facbeSLen Brown 	p->energy_gfx = 0;
1466889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1467889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1468889facbeSLen Brown 	p->pkg_temp_c = 0;
146927d47356SLen Brown 
1470fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
147127d47356SLen Brown 	p->gfx_mhz = 0;
1472388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1473388e9c81SLen Brown 		t->counter[i] = 0;
1474388e9c81SLen Brown 
1475388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1476388e9c81SLen Brown 		c->counter[i] = 0;
1477388e9c81SLen Brown 
1478388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1479388e9c81SLen Brown 		p->counter[i] = 0;
1480103a8feaSLen Brown }
1481c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1482c98d5d94SLen Brown 	struct pkg_data *p)
1483103a8feaSLen Brown {
1484388e9c81SLen Brown 	int i;
1485388e9c81SLen Brown 	struct msr_counter *mp;
1486388e9c81SLen Brown 
14874c2122d4SLen Brown 	/* copy un-changing apic_id's */
14884c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
14894c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
14904c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
14914c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
14924c2122d4SLen Brown 
14933f44a5c6SLen Brown 	/* remember first tv_begin */
14943f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14953f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14963f44a5c6SLen Brown 
14973f44a5c6SLen Brown 	/* remember last tv_end */
14983f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14993f44a5c6SLen Brown 
1500c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1501c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1502c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1503c98d5d94SLen Brown 	average.threads.c1 += t->c1;
150415aaa346SLen Brown 
1505562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1506562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1507562a2d37SLen Brown 
1508388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1509388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1510388e9c81SLen Brown 			continue;
1511388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1512388e9c81SLen Brown 	}
1513388e9c81SLen Brown 
1514c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1515c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1516c98d5d94SLen Brown 		return 0;
1517c98d5d94SLen Brown 
1518c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1519c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1520c98d5d94SLen Brown 	average.cores.c7 += c->c7;
15210539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1522c98d5d94SLen Brown 
1523889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1524889facbeSLen Brown 
15259392bd98SCalvin Walton 	average.cores.core_energy += c->core_energy;
15269392bd98SCalvin Walton 
1527388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1528388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1529388e9c81SLen Brown 			continue;
1530388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1531388e9c81SLen Brown 	}
1532388e9c81SLen Brown 
1533c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1534c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1535c98d5d94SLen Brown 		return 0;
1536c98d5d94SLen Brown 
1537a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15380b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1539a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15400b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1541a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15420b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1543a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15440b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
15450b2bb692SLen Brown 
1546c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
15470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1548c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
15490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1550c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
15510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1552c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1553ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1554ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1555ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1556c98d5d94SLen Brown 
1557be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1558be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1559be0e54c4SLen Brown 
1560889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1561889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1562889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1563889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1564889facbeSLen Brown 
1565fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
156627d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
156727d47356SLen Brown 
1568889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1569889facbeSLen Brown 
1570889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1571889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1572388e9c81SLen Brown 
1573388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1574388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1575388e9c81SLen Brown 			continue;
1576388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1577388e9c81SLen Brown 	}
1578c98d5d94SLen Brown 	return 0;
1579c98d5d94SLen Brown }
1580c98d5d94SLen Brown /*
1581c98d5d94SLen Brown  * sum the counters for all cpus in the system
1582c98d5d94SLen Brown  * compute the weighted average
1583c98d5d94SLen Brown  */
1584c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1585c98d5d94SLen Brown 	struct pkg_data *p)
1586c98d5d94SLen Brown {
1587388e9c81SLen Brown 	int i;
1588388e9c81SLen Brown 	struct msr_counter *mp;
1589388e9c81SLen Brown 
1590c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1591c98d5d94SLen Brown 
1592c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1593c98d5d94SLen Brown 
1594d4794f25SYazen Ghannam 	/* Use the global time delta for the average. */
1595d4794f25SYazen Ghannam 	average.threads.tv_delta = tv_delta;
1596d4794f25SYazen Ghannam 
1597c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1598c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1599c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1600c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1601c98d5d94SLen Brown 
16020de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
16030de6c0dfSLen Brown 		sums_need_wide_columns = 1;
16040de6c0dfSLen Brown 
1605c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1606c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1607c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
16080539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1609c98d5d94SLen Brown 
1610a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
16110b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1612a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
16130b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1614a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
16150b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1616a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
16170b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
16180b2bb692SLen Brown 
1619c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
16200f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1621c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
16220f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1623c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
16240f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1625c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1626ca58710fSKristen Carlson Accardi 
1627ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1628ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1629ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1630388e9c81SLen Brown 
1631388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1632388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1633388e9c81SLen Brown 			continue;
16340de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16350de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
16360de6c0dfSLen Brown 				sums_need_wide_columns = 1;
163741618e63SLen Brown 			continue;
16380de6c0dfSLen Brown 		}
1639388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1640388e9c81SLen Brown 	}
1641388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1642388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1643388e9c81SLen Brown 			continue;
16440de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16450de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
16460de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16470de6c0dfSLen Brown 		}
1648388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1649388e9c81SLen Brown 	}
1650388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1651388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1652388e9c81SLen Brown 			continue;
16530de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16540de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
16550de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16560de6c0dfSLen Brown 		}
1657388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1658388e9c81SLen Brown 	}
1659c98d5d94SLen Brown }
1660c98d5d94SLen Brown 
1661c98d5d94SLen Brown static unsigned long long rdtsc(void)
1662c98d5d94SLen Brown {
1663c98d5d94SLen Brown 	unsigned int low, high;
1664c98d5d94SLen Brown 
1665c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1666c98d5d94SLen Brown 
1667c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1668c98d5d94SLen Brown }
1669c98d5d94SLen Brown 
1670c98d5d94SLen Brown /*
1671495c7654SLen Brown  * Open a file, and exit on failure
1672495c7654SLen Brown  */
1673495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1674495c7654SLen Brown {
1675495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1676495c7654SLen Brown 
1677495c7654SLen Brown 	if (!filep)
1678495c7654SLen Brown 		err(1, "%s: open failed", path);
1679495c7654SLen Brown 	return filep;
1680495c7654SLen Brown }
1681495c7654SLen Brown /*
1682495c7654SLen Brown  * snapshot_sysfs_counter()
1683495c7654SLen Brown  *
1684495c7654SLen Brown  * return snapshot of given counter
1685495c7654SLen Brown  */
1686495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1687495c7654SLen Brown {
1688495c7654SLen Brown 	FILE *fp;
1689495c7654SLen Brown 	int retval;
1690495c7654SLen Brown 	unsigned long long counter;
1691495c7654SLen Brown 
1692495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1693495c7654SLen Brown 
1694495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1695495c7654SLen Brown 	if (retval != 1)
1696495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1697495c7654SLen Brown 
1698495c7654SLen Brown 	fclose(fp);
1699495c7654SLen Brown 
1700495c7654SLen Brown 	return counter;
1701495c7654SLen Brown }
1702495c7654SLen Brown 
1703495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1704495c7654SLen Brown {
1705495c7654SLen Brown 	if (mp->msr_num != 0) {
1706495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1707495c7654SLen Brown 			return -1;
1708495c7654SLen Brown 	} else {
170946c27978SLen Brown 		char path[128 + PATH_BYTES];
171041618e63SLen Brown 
171141618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
171241618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
171341618e63SLen Brown 				 cpu, mp->path);
171441618e63SLen Brown 
171541618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
171641618e63SLen Brown 		} else {
1717495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1718495c7654SLen Brown 		}
171941618e63SLen Brown 	}
1720495c7654SLen Brown 
1721495c7654SLen Brown 	return 0;
1722495c7654SLen Brown }
1723495c7654SLen Brown 
17244c2122d4SLen Brown void get_apic_id(struct thread_data *t)
17254c2122d4SLen Brown {
172634041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
172734041551SLen Brown 
172834041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
172934041551SLen Brown 		eax = ebx = ecx = edx = 0;
173034041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
173134041551SLen Brown 
173234041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
173334041551SLen Brown 	}
173434041551SLen Brown 
173534041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
173634041551SLen Brown 		return;
173734041551SLen Brown 
1738c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine) {
173934041551SLen Brown 		unsigned int topology_extensions;
174034041551SLen Brown 
174134041551SLen Brown 		if (max_extended_level < 0x8000001e)
174234041551SLen Brown 			return;
17434c2122d4SLen Brown 
17444c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
174534041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
174634041551SLen Brown 			topology_extensions = ecx & (1 << 22);
174734041551SLen Brown 
174834041551SLen Brown 		if (topology_extensions == 0)
174934041551SLen Brown 			return;
175034041551SLen Brown 
175134041551SLen Brown 		eax = ebx = ecx = edx = 0;
175234041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
175334041551SLen Brown 
175434041551SLen Brown 		t->x2apic_id = eax;
175534041551SLen Brown 		return;
175634041551SLen Brown 	}
17574c2122d4SLen Brown 
17584c2122d4SLen Brown 	if (!genuine_intel)
17594c2122d4SLen Brown 		return;
17604c2122d4SLen Brown 
17614c2122d4SLen Brown 	if (max_level < 0xb)
17624c2122d4SLen Brown 		return;
17634c2122d4SLen Brown 
17644c2122d4SLen Brown 	ecx = 0;
17654c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
17664c2122d4SLen Brown 	t->x2apic_id = edx;
17674c2122d4SLen Brown 
176834041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
176934041551SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
177034041551SLen Brown 				t->cpu_id, t->apic_id, t->x2apic_id);
17714c2122d4SLen Brown }
17724c2122d4SLen Brown 
1773495c7654SLen Brown /*
1774c98d5d94SLen Brown  * get_counters(...)
1775c98d5d94SLen Brown  * migrate to cpu
1776c98d5d94SLen Brown  * acquire and record local counters for that cpu
1777c98d5d94SLen Brown  */
1778c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1779c98d5d94SLen Brown {
1780c98d5d94SLen Brown 	int cpu = t->cpu_id;
1781889facbeSLen Brown 	unsigned long long msr;
17820102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1783388e9c81SLen Brown 	struct msr_counter *mp;
1784388e9c81SLen Brown 	int i;
1785c98d5d94SLen Brown 
1786e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1787b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
178815aaa346SLen Brown 		return -1;
1789e52966c0SLen Brown 	}
179015aaa346SLen Brown 
1791d4794f25SYazen Ghannam 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1792d4794f25SYazen Ghannam 
17934c2122d4SLen Brown 	if (first_counter_read)
17944c2122d4SLen Brown 		get_apic_id(t);
17950102b067SLen Brown retry:
1796c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
179715aaa346SLen Brown 
17981e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) ||
17991e9042b9SSrinivas Pandruvada 	    soft_c1_residency_display(BIC_Avg_MHz)) {
18000102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
18010102b067SLen Brown 
18020102b067SLen Brown 		/*
18030102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
18040102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
18050102b067SLen Brown 		 *
18060102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
18070102b067SLen Brown 		 * individual system call, so delays may occur
18080102b067SLen Brown 		 * between them.  If the time to read them
18090102b067SLen Brown 		 * varies by a large amount, we re-read them.
18100102b067SLen Brown 		 */
18110102b067SLen Brown 
18120102b067SLen Brown 		/*
18130102b067SLen Brown 		 * This initial dummy APERF read has been seen to
18140102b067SLen Brown 		 * reduce jitter in the subsequent reads.
18150102b067SLen Brown 		 */
18160102b067SLen Brown 
18179c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1818c98d5d94SLen Brown 			return -3;
18190102b067SLen Brown 
18200102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
18210102b067SLen Brown 
18220102b067SLen Brown 		tsc_before = t->tsc;
18230102b067SLen Brown 
18240102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
18250102b067SLen Brown 			return -3;
18260102b067SLen Brown 
18270102b067SLen Brown 		tsc_between = rdtsc();
18280102b067SLen Brown 
18299c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1830c98d5d94SLen Brown 			return -4;
18310102b067SLen Brown 
18320102b067SLen Brown 		tsc_after = rdtsc();
18330102b067SLen Brown 
18340102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
18350102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
18360102b067SLen Brown 
18370102b067SLen Brown 		/*
18380102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
18390102b067SLen Brown 		 * differ by more than 2x, then try again.
18400102b067SLen Brown 		 */
18410102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
18420102b067SLen Brown 			aperf_mperf_retry_count++;
18430102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
18440102b067SLen Brown 				goto retry;
18450102b067SLen Brown 			else
18460102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
18470102b067SLen Brown 					cpu, aperf_time, mperf_time);
18480102b067SLen Brown 		}
18490102b067SLen Brown 		aperf_mperf_retry_count = 0;
18500102b067SLen Brown 
1851b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1852b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
185388c3281fSLen Brown 	}
185488c3281fSLen Brown 
1855812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1856562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1857812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
18581ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
18591ed51011SLen Brown 			return -5;
18601ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
18611ed51011SLen Brown 	}
18620539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1863144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1864144b44b1SLen Brown 			return -6;
1865144b44b1SLen Brown 	}
1866144b44b1SLen Brown 
1867388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1868495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1869388e9c81SLen Brown 			return -10;
1870388e9c81SLen Brown 	}
1871388e9c81SLen Brown 
1872c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1873c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1874f4fdf2b4SLen Brown 		goto done;
1875c98d5d94SLen Brown 
18761e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
1877c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1878c98d5d94SLen Brown 			return -6;
1879144b44b1SLen Brown 	}
1880144b44b1SLen Brown 
18811e9042b9SSrinivas Pandruvada 	if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
1882c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1883c98d5d94SLen Brown 			return -7;
18841e9042b9SSrinivas Pandruvada 	} else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
1885fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1886fb5d4327SDasaratharaman Chandramouli 			return -7;
1887103a8feaSLen Brown 	}
188815aaa346SLen Brown 
18891e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7))
1890c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1891c98d5d94SLen Brown 			return -8;
1892c98d5d94SLen Brown 
18930539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18940539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18950539ba11SLen Brown 			return -8;
18960539ba11SLen Brown 
1897812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1898889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1899889facbeSLen Brown 			return -9;
1900889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1901889facbeSLen Brown 	}
1902889facbeSLen Brown 
19039392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
19049392bd98SCalvin Walton 		if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
19059392bd98SCalvin Walton 			return -14;
19069392bd98SCalvin Walton 		c->core_energy = msr & 0xFFFFFFFF;
19079392bd98SCalvin Walton 	}
19089392bd98SCalvin Walton 
1909388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1910495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1911388e9c81SLen Brown 			return -10;
1912388e9c81SLen Brown 	}
1913889facbeSLen Brown 
1914c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1915c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1916f4fdf2b4SLen Brown 		goto done;
191715aaa346SLen Brown 
1918a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
19190b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
19200b2bb692SLen Brown 			return -10;
1921a99d8730SLen Brown 	}
1922a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
19230b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
19240b2bb692SLen Brown 			return -11;
1925a99d8730SLen Brown 	}
1926a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
19270b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
19280b2bb692SLen Brown 			return -12;
1929a99d8730SLen Brown 	}
1930a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
19310b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
19320b2bb692SLen Brown 			return -13;
19330b2bb692SLen Brown 	}
19340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1935c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1936c98d5d94SLen Brown 			return -9;
19370f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
19380539ba11SLen Brown 		if (do_slm_cstates) {
19390539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
19400539ba11SLen Brown 				return -10;
19410539ba11SLen Brown 		} else {
1942c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1943c98d5d94SLen Brown 				return -10;
19440539ba11SLen Brown 		}
19450539ba11SLen Brown 	}
19460539ba11SLen Brown 
19470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1948c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1949c98d5d94SLen Brown 			return -11;
19500f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1951c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1952c98d5d94SLen Brown 			return -12;
19530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1954ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1955ca58710fSKristen Carlson Accardi 			return -13;
19560f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1957ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1958ca58710fSKristen Carlson Accardi 			return -13;
19590f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1960ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1961ca58710fSKristen Carlson Accardi 			return -13;
19620f47c08dSLen Brown 
1963be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1964be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1965be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1966be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1967be0e54c4SLen Brown 
1968889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1969889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1970889facbeSLen Brown 			return -13;
1971889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1972889facbeSLen Brown 	}
19739148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1974889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1975889facbeSLen Brown 			return -14;
1976889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1977889facbeSLen Brown 	}
1978889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1979889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1980889facbeSLen Brown 			return -15;
1981889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1982889facbeSLen Brown 	}
1983889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1984889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1985889facbeSLen Brown 			return -16;
1986889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1987889facbeSLen Brown 	}
1988889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1989889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1990889facbeSLen Brown 			return -16;
1991889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1992889facbeSLen Brown 	}
1993889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1994889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1995889facbeSLen Brown 			return -16;
1996889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1997889facbeSLen Brown 	}
19983316f99aSCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
19993316f99aSCalvin Walton 		if (get_msr(cpu, MSR_PKG_ENERGY_STAT, &msr))
20003316f99aSCalvin Walton 			return -13;
20013316f99aSCalvin Walton 		p->energy_pkg = msr & 0xFFFFFFFF;
20023316f99aSCalvin Walton 	}
2003812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
2004889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2005889facbeSLen Brown 			return -17;
2006889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
2007889facbeSLen Brown 	}
2008fdf676e5SLen Brown 
2009812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2010fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
2011fdf676e5SLen Brown 
2012812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
201327d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
201427d47356SLen Brown 
2015388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2016495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
2017388e9c81SLen Brown 			return -10;
2018388e9c81SLen Brown 	}
2019f4fdf2b4SLen Brown done:
2020f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
2021388e9c81SLen Brown 
202215aaa346SLen Brown 	return 0;
2023103a8feaSLen Brown }
2024103a8feaSLen Brown 
2025ee7e38e3SLen Brown /*
2026ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2027ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
2028ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2029ee7e38e3SLen Brown  */
2030ee7e38e3SLen Brown 
2031ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
2032ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
2033ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
2034ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
2035ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
2036ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
2037ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
2038ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
2039ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
2040ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
2041ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
2042ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
20430b2bb692SLen Brown #define PCL__8 12 /* PC8 */
20440b2bb692SLen Brown #define PCL__9 13 /* PC9 */
2045445640a5SLen Brown #define PCL_10 14 /* PC10 */
2046445640a5SLen Brown #define PCLUNL 15 /* Unlimited */
2047ee7e38e3SLen Brown 
2048ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2049ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2050445640a5SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
2051ee7e38e3SLen Brown 
2052e9257f5fSLen 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};
2053e9257f5fSLen 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};
2054e9257f5fSLen 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};
20550539ba11SLen 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};
2056f2642888SLen 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};
2057e9257f5fSLen 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};
2058445640a5SLen 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};
20592085e124SArtem 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};
2060ee7e38e3SLen Brown 
2061a2b7b749SLen Brown 
2062a2b7b749SLen Brown static void
2063a2b7b749SLen Brown calculate_tsc_tweak()
2064a2b7b749SLen Brown {
2065a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
2066a2b7b749SLen Brown }
2067a2b7b749SLen Brown 
2068fcd17211SLen Brown static void
2069fcd17211SLen Brown dump_nhm_platform_info(void)
2070103a8feaSLen Brown {
2071103a8feaSLen Brown 	unsigned long long msr;
2072103a8feaSLen Brown 	unsigned int ratio;
2073103a8feaSLen Brown 
2074ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2075103a8feaSLen Brown 
2076b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
20776574a5d5SLen Brown 
2078103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
2079710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2080103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2081103a8feaSLen Brown 
2082103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
2083710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2084103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2085103a8feaSLen Brown 
20867ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2087b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2088bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
208967920418SLen Brown 
2090fcd17211SLen Brown 	return;
2091fcd17211SLen Brown }
2092fcd17211SLen Brown 
2093fcd17211SLen Brown static void
2094fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
2095fcd17211SLen Brown {
2096fcd17211SLen Brown 	unsigned long long msr;
2097fcd17211SLen Brown 	unsigned int ratio;
2098fcd17211SLen Brown 
20997ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2100fcd17211SLen Brown 
2101b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2102fcd17211SLen Brown 
2103fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2104fcd17211SLen Brown 	if (ratio)
2105710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2106fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2107fcd17211SLen Brown 
2108fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2109fcd17211SLen Brown 	if (ratio)
2110710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2111fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2112fcd17211SLen Brown 	return;
2113fcd17211SLen Brown }
2114fcd17211SLen Brown 
2115fcd17211SLen Brown static void
2116fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2117fcd17211SLen Brown {
2118fcd17211SLen Brown 	unsigned long long msr;
2119fcd17211SLen Brown 	unsigned int ratio;
21206574a5d5SLen Brown 
21217ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
21226574a5d5SLen Brown 
2123b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
21246574a5d5SLen Brown 
21256574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
21266574a5d5SLen Brown 	if (ratio)
2127710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
21286574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21296574a5d5SLen Brown 
21306574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
21316574a5d5SLen Brown 	if (ratio)
2132710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
21336574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21346574a5d5SLen Brown 
21356574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
21366574a5d5SLen Brown 	if (ratio)
2137710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
21386574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21396574a5d5SLen Brown 
21406574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
21416574a5d5SLen Brown 	if (ratio)
2142710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
21436574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21446574a5d5SLen Brown 
21456574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
21466574a5d5SLen Brown 	if (ratio)
2147710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
21486574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21496574a5d5SLen Brown 
21506574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
21516574a5d5SLen Brown 	if (ratio)
2152710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
21536574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21546574a5d5SLen Brown 
21556574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
21566574a5d5SLen Brown 	if (ratio)
2157710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
21586574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21596574a5d5SLen Brown 
21606574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
21616574a5d5SLen Brown 	if (ratio)
2162710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
21636574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2164103a8feaSLen Brown 	return;
2165fcd17211SLen Brown }
216631e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
216731e07522SLen Brown {
216831e07522SLen Brown 
216931e07522SLen Brown 	if (!genuine_intel)
217031e07522SLen Brown 		return 0;
217131e07522SLen Brown 
217231e07522SLen Brown 	switch (model) {
217331e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
217431e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
21755ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
217631e07522SLen Brown 		return 1;
217731e07522SLen Brown 	}
217831e07522SLen Brown 	return 0;
217931e07522SLen Brown }
2180fcd17211SLen Brown 
2181fcd17211SLen Brown static void
218231e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2183fcd17211SLen Brown {
218431e07522SLen Brown 	unsigned long long msr, core_counts;
218531e07522SLen Brown 	unsigned int ratio, group_size;
2186103a8feaSLen Brown 
21877ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2188b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
21896574a5d5SLen Brown 
219031e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
219131e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
219231e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
219331e07522SLen Brown 	} else {
219431e07522SLen Brown 		core_counts = 0x0807060504030201;
219531e07522SLen Brown 	}
219631e07522SLen Brown 
21976574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
219831e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
21996574a5d5SLen Brown 	if (ratio)
220031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22026574a5d5SLen Brown 
22036574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
220431e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
22056574a5d5SLen Brown 	if (ratio)
220631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22086574a5d5SLen Brown 
22096574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
221031e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
22116574a5d5SLen Brown 	if (ratio)
221231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
221331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22146574a5d5SLen Brown 
22156574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
221631e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
22176574a5d5SLen Brown 	if (ratio)
221831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
221931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
22206574a5d5SLen Brown 
2221103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
222231e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2223103a8feaSLen Brown 	if (ratio)
222431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
222531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2226103a8feaSLen Brown 
2227103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
222831e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2229103a8feaSLen Brown 	if (ratio)
223031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
223131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2232103a8feaSLen Brown 
2233103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
223431e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2235103a8feaSLen Brown 	if (ratio)
223631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
223731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2238103a8feaSLen Brown 
2239103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
224031e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2241103a8feaSLen Brown 	if (ratio)
224231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
224331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2244fcd17211SLen Brown 	return;
2245fcd17211SLen Brown }
22463a9a941dSLen Brown 
2247fcd17211SLen Brown static void
22480f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
22490f7887c4SLen Brown {
22500f7887c4SLen Brown 	unsigned long long msr;
22510f7887c4SLen Brown 	unsigned int ratio;
22520f7887c4SLen Brown 
22530f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
22540f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22550f7887c4SLen Brown 
22560f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22570f7887c4SLen Brown 	if (ratio)
22580f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
22590f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22600f7887c4SLen Brown 
22610f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22620f7887c4SLen Brown 	if (ratio)
22630f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
22640f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22650f7887c4SLen Brown 
22660f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22670f7887c4SLen Brown 	if (ratio)
22680f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
22690f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22700f7887c4SLen Brown 
22710f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
22720f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22730f7887c4SLen Brown 
22740f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
22750f7887c4SLen Brown 	if (ratio)
22760f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
22770f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22780f7887c4SLen Brown 
22790f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22800f7887c4SLen Brown 	if (ratio)
22810f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
22820f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22830f7887c4SLen Brown 
22840f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22850f7887c4SLen Brown 	if (ratio)
22860f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
22870f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22880f7887c4SLen Brown 
22890f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22900f7887c4SLen Brown 	if (ratio)
22910f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
22920f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22930f7887c4SLen Brown }
22940f7887c4SLen Brown 
22950f7887c4SLen Brown static void
2296fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2297fb5d4327SDasaratharaman Chandramouli {
2298cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2299cbf97abaSHubert Chrzaniuk 
2300fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2301cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2302cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2303cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2304cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2305fb5d4327SDasaratharaman Chandramouli 
2306ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2307fb5d4327SDasaratharaman Chandramouli 
2308b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2309bfae2052SLen Brown 		base_cpu, msr);
2310fb5d4327SDasaratharaman Chandramouli 
2311fb5d4327SDasaratharaman Chandramouli 	/**
2312fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2313cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2314cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2315fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2316fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2317fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2318fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2319fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2320fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2321fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2322fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2323fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2324fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2325fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2326fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2327fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2328fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2329fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2330fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2331fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2332fb5d4327SDasaratharaman Chandramouli 	 */
2333fb5d4327SDasaratharaman Chandramouli 
2334cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2335cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2336cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2337cbf97abaSHubert Chrzaniuk 
2338cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2339fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2340cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2341fb5d4327SDasaratharaman Chandramouli 
2342cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2343cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2344cbf97abaSHubert Chrzaniuk 		b_nr++;
2345fb5d4327SDasaratharaman Chandramouli 	}
2346cbf97abaSHubert Chrzaniuk 
2347cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2348cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2349b7d8c148SLen Brown 			fprintf(outf,
2350710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2351cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2352fb5d4327SDasaratharaman Chandramouli }
2353fb5d4327SDasaratharaman Chandramouli 
2354fb5d4327SDasaratharaman Chandramouli static void
2355fcd17211SLen Brown dump_nhm_cst_cfg(void)
2356fcd17211SLen Brown {
2357fcd17211SLen Brown 	unsigned long long msr;
2358fcd17211SLen Brown 
23591df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2360fcd17211SLen Brown 
23611df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2362fcd17211SLen Brown 
23633e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2364fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2365fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2366fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2367fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2368fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
23696c34f160SLen Brown 		(unsigned int)msr & 0xF,
2370fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2371ac980e13SArtem Bityutskiy 
2372ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2373ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2374ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2375ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2376ac980e13SArtem Bityutskiy 	}
2377ac980e13SArtem Bityutskiy 
2378ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2379ac980e13SArtem Bityutskiy 
2380fcd17211SLen Brown 	return;
2381103a8feaSLen Brown }
2382103a8feaSLen Brown 
23836fb3143bSLen Brown static void
23846fb3143bSLen Brown dump_config_tdp(void)
23856fb3143bSLen Brown {
23866fb3143bSLen Brown 	unsigned long long msr;
23876fb3143bSLen Brown 
23886fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2389b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2390685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
23916fb3143bSLen Brown 
23926fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2393b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
23946fb3143bSLen Brown 	if (msr) {
2395685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2396685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2397685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2398685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
23996fb3143bSLen Brown 	}
2400b7d8c148SLen Brown 	fprintf(outf, ")\n");
24016fb3143bSLen Brown 
24026fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2403b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
24046fb3143bSLen Brown 	if (msr) {
2405685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2406685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2407685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2408685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
24096fb3143bSLen Brown 	}
2410b7d8c148SLen Brown 	fprintf(outf, ")\n");
24116fb3143bSLen Brown 
24126fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2413b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
24146fb3143bSLen Brown 	if ((msr) & 0x3)
2415b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2416b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2417b7d8c148SLen Brown 	fprintf(outf, ")\n");
24186fb3143bSLen Brown 
24196fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2420b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2421685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2422b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2423b7d8c148SLen Brown 	fprintf(outf, ")\n");
24246fb3143bSLen Brown }
24255a63426eSLen Brown 
24265a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
24275a63426eSLen Brown 
24285a63426eSLen Brown void print_irtl(void)
24295a63426eSLen Brown {
24305a63426eSLen Brown 	unsigned long long msr;
24315a63426eSLen Brown 
24325a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
24335a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
24345a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24355a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24365a63426eSLen Brown 
24375a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
24385a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
24395a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24405a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24415a63426eSLen Brown 
24425a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
24435a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
24445a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24455a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24465a63426eSLen Brown 
24475a63426eSLen Brown 	if (!do_irtl_hsw)
24485a63426eSLen Brown 		return;
24495a63426eSLen Brown 
24505a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
24515a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
24525a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24535a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24545a63426eSLen Brown 
24555a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
24565a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
24575a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24585a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24595a63426eSLen Brown 
24605a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
24615a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
24625a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24635a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24645a63426eSLen Brown 
24655a63426eSLen Brown }
246636229897SLen Brown void free_fd_percpu(void)
246736229897SLen Brown {
246836229897SLen Brown 	int i;
246936229897SLen Brown 
247001a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
247136229897SLen Brown 		if (fd_percpu[i] != 0)
247236229897SLen Brown 			close(fd_percpu[i]);
247336229897SLen Brown 	}
247436229897SLen Brown 
247536229897SLen Brown 	free(fd_percpu);
24766fb3143bSLen Brown }
24776fb3143bSLen Brown 
2478c98d5d94SLen Brown void free_all_buffers(void)
2479103a8feaSLen Brown {
24800e2d8f05SLen Brown 	int i;
24810e2d8f05SLen Brown 
2482c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2483c98d5d94SLen Brown 	cpu_present_set = NULL;
248436229897SLen Brown 	cpu_present_setsize = 0;
2485103a8feaSLen Brown 
2486c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2487c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2488c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2489103a8feaSLen Brown 
2490c98d5d94SLen Brown 	free(thread_even);
2491c98d5d94SLen Brown 	free(core_even);
2492c98d5d94SLen Brown 	free(package_even);
2493103a8feaSLen Brown 
2494c98d5d94SLen Brown 	thread_even = NULL;
2495c98d5d94SLen Brown 	core_even = NULL;
2496c98d5d94SLen Brown 	package_even = NULL;
2497103a8feaSLen Brown 
2498c98d5d94SLen Brown 	free(thread_odd);
2499c98d5d94SLen Brown 	free(core_odd);
2500c98d5d94SLen Brown 	free(package_odd);
2501103a8feaSLen Brown 
2502c98d5d94SLen Brown 	thread_odd = NULL;
2503c98d5d94SLen Brown 	core_odd = NULL;
2504c98d5d94SLen Brown 	package_odd = NULL;
2505103a8feaSLen Brown 
2506c98d5d94SLen Brown 	free(output_buffer);
2507c98d5d94SLen Brown 	output_buffer = NULL;
2508c98d5d94SLen Brown 	outp = NULL;
250936229897SLen Brown 
251036229897SLen Brown 	free_fd_percpu();
2511562a2d37SLen Brown 
2512562a2d37SLen Brown 	free(irq_column_2_cpu);
2513562a2d37SLen Brown 	free(irqs_per_cpu);
25140e2d8f05SLen Brown 
25150e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
25160e2d8f05SLen Brown 		if (cpus[i].put_ids)
25170e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
25180e2d8f05SLen Brown 	}
25190e2d8f05SLen Brown 	free(cpus);
2520103a8feaSLen Brown }
2521103a8feaSLen Brown 
252257a42a34SJosh Triplett 
252357a42a34SJosh Triplett /*
252495aebc44SJosh Triplett  * Parse a file containing a single int.
25256de68fe1SLen Brown  * Return 0 if file can not be opened
25266de68fe1SLen Brown  * Exit if file can be opened, but can not be parsed
252795aebc44SJosh Triplett  */
252895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
252995aebc44SJosh Triplett {
253095aebc44SJosh Triplett 	va_list args;
253195aebc44SJosh Triplett 	char path[PATH_MAX];
253295aebc44SJosh Triplett 	FILE *filep;
253395aebc44SJosh Triplett 	int value;
253495aebc44SJosh Triplett 
253595aebc44SJosh Triplett 	va_start(args, fmt);
253695aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
253795aebc44SJosh Triplett 	va_end(args);
25386de68fe1SLen Brown 	filep = fopen(path, "r");
25396de68fe1SLen Brown 	if (!filep)
25406de68fe1SLen Brown 		return 0;
2541b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2542b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
254395aebc44SJosh Triplett 	fclose(filep);
254495aebc44SJosh Triplett 	return value;
254595aebc44SJosh Triplett }
254695aebc44SJosh Triplett 
254795aebc44SJosh Triplett /*
2548c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2549c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2550c98d5d94SLen Brown  */
2551c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2552c98d5d94SLen Brown {
255395aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2554103a8feaSLen Brown }
2555103a8feaSLen Brown 
2556103a8feaSLen Brown int get_physical_package_id(int cpu)
2557103a8feaSLen Brown {
255895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2559103a8feaSLen Brown }
2560103a8feaSLen Brown 
25616de68fe1SLen Brown int get_die_id(int cpu)
25626de68fe1SLen Brown {
25636de68fe1SLen Brown 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
25646de68fe1SLen Brown }
25656de68fe1SLen Brown 
2566103a8feaSLen Brown int get_core_id(int cpu)
2567103a8feaSLen Brown {
256895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2569103a8feaSLen Brown }
2570103a8feaSLen Brown 
2571ef605741SPrarit Bhargava void set_node_data(void)
2572ef605741SPrarit Bhargava {
25732ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
25742ffbb224SPrarit Bhargava 	int cpu_count;
2575ef605741SPrarit Bhargava 
25762ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
25772ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
25782ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2579ef605741SPrarit Bhargava 
25802ffbb224SPrarit Bhargava 	cpu_count = 0;
25812ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
25822ffbb224SPrarit Bhargava 		lnode = 0;
25832ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
25842ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2585ef605741SPrarit Bhargava 				continue;
25862ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
25872ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
25882ffbb224SPrarit Bhargava 				continue;
25892ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2590ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
25912ffbb224SPrarit Bhargava 			cpu_count++;
25922ffbb224SPrarit Bhargava 			/*
25932ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
25942ffbb224SPrarit Bhargava 			 * the logical_node_id
25952ffbb224SPrarit Bhargava 			 */
25962ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
25972ffbb224SPrarit Bhargava 				if ((cpus[cpux].physical_package_id == pkg) &&
25982ffbb224SPrarit Bhargava 				   (cpus[cpux].physical_node_id == node)) {
25992ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
26002ffbb224SPrarit Bhargava 					cpu_count++;
2601ef605741SPrarit Bhargava 				}
26022ffbb224SPrarit Bhargava 			}
26032ffbb224SPrarit Bhargava 			lnode++;
26042ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
26052ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
26062ffbb224SPrarit Bhargava 		}
26072ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
26082ffbb224SPrarit Bhargava 			break;
26092ffbb224SPrarit Bhargava 	}
2610ef605741SPrarit Bhargava }
2611ef605741SPrarit Bhargava 
2612ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2613c98d5d94SLen Brown {
2614c98d5d94SLen Brown 	char path[80];
2615c98d5d94SLen Brown 	FILE *filep;
26160e2d8f05SLen Brown 	int i;
26170e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2618c98d5d94SLen Brown 
26190e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
26200e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
26210e2d8f05SLen Brown 			cpu, i);
26220e2d8f05SLen Brown 		filep = fopen(path, "r");
26230e2d8f05SLen Brown 		if (!filep)
26240e2d8f05SLen Brown 			continue;
26250e2d8f05SLen Brown 		fclose(filep);
26260e2d8f05SLen Brown 		return i;
26270e2d8f05SLen Brown 	}
26280e2d8f05SLen Brown 	return -1;
2629e275b388SDasaratharaman Chandramouli }
2630c98d5d94SLen Brown 
26310e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
26320e2d8f05SLen Brown {
26330e2d8f05SLen Brown 	char path[80], character;
26340e2d8f05SLen Brown 	FILE *filep;
26350e2d8f05SLen Brown 	unsigned long map;
26368cb48b32SPrarit Bhargava 	int so, shift, sib_core;
26370e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
26380e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
26390e2d8f05SLen Brown 	size_t size;
26408cb48b32SPrarit Bhargava 	int thread_id = 0;
26410e2d8f05SLen Brown 
26420e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
26438cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
26448cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
26450e2d8f05SLen Brown 	if (!thiscpu->put_ids)
26460e2d8f05SLen Brown 		return -1;
26470e2d8f05SLen Brown 
26480e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
26490e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
26500e2d8f05SLen Brown 
26510e2d8f05SLen Brown 	sprintf(path,
26520e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
26530e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
26540e2d8f05SLen Brown 	do {
26550e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
26568173c336SBen Hutchings 		if (fscanf(filep, "%lx%c", &map, &character) != 2)
26578173c336SBen Hutchings 			err(1, "%s: failed to parse file", path);
26580e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
26590e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
26608cb48b32SPrarit Bhargava 				so = shift + offset;
26618cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
26628cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
26638cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
26648cb48b32SPrarit Bhargava 					if ((so != cpu) &&
26658cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
26668cb48b32SPrarit Bhargava 						cpus[so].thread_id =
26678cb48b32SPrarit Bhargava 								    thread_id++;
26688cb48b32SPrarit Bhargava 				}
26690e2d8f05SLen Brown 			}
26700e2d8f05SLen Brown 		}
26710e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2672c98d5d94SLen Brown 	fclose(filep);
26730e2d8f05SLen Brown 
26740e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2675c98d5d94SLen Brown }
2676c98d5d94SLen Brown 
2677c98d5d94SLen Brown /*
2678c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2679c98d5d94SLen Brown  * skip non-present cpus
2680103a8feaSLen Brown  */
2681103a8feaSLen Brown 
2682c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2683c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2684c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2685c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2686c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2687c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2688c98d5d94SLen Brown {
268940f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2690c98d5d94SLen Brown 
2691c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
269240f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
269340f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
269440f5cfe7SPrarit Bhargava 			     ++core_no) {
2695c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
269670a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2697c98d5d94SLen Brown 					struct thread_data *t, *t2;
2698c98d5d94SLen Brown 					struct core_data *c, *c2;
2699c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2700c98d5d94SLen Brown 
270140f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
270240f5cfe7SPrarit Bhargava 						       core_no, node_no,
270340f5cfe7SPrarit Bhargava 						       pkg_no);
2704c98d5d94SLen Brown 
2705c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2706c98d5d94SLen Brown 						continue;
2707c98d5d94SLen Brown 
270840f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
270940f5cfe7SPrarit Bhargava 							core_no, node_no,
271040f5cfe7SPrarit Bhargava 							pkg_no);
2711c98d5d94SLen Brown 
271240f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
271340f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
271440f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
271540f5cfe7SPrarit Bhargava 						      node_no,
271640f5cfe7SPrarit Bhargava 						      pkg_no);
2717c98d5d94SLen Brown 
2718c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2719c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2720c98d5d94SLen Brown 
2721c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2722c98d5d94SLen Brown 					if (retval)
2723c98d5d94SLen Brown 						return retval;
2724c98d5d94SLen Brown 				}
2725c98d5d94SLen Brown 			}
2726c98d5d94SLen Brown 		}
272740f5cfe7SPrarit Bhargava 	}
2728c98d5d94SLen Brown 	return 0;
2729c98d5d94SLen Brown }
2730c98d5d94SLen Brown 
2731c98d5d94SLen Brown /*
2732c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2733c98d5d94SLen Brown  * return max_cpu number
2734c98d5d94SLen Brown  */
2735c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2736103a8feaSLen Brown {
2737103a8feaSLen Brown 	FILE *fp;
2738c98d5d94SLen Brown 	int cpu_num;
2739103a8feaSLen Brown 	int retval;
2740103a8feaSLen Brown 
274157a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2742103a8feaSLen Brown 
2743103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2744b2c95d90SJosh Triplett 	if (retval != 0)
2745b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2746103a8feaSLen Brown 
2747c98d5d94SLen Brown 	while (1) {
2748c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2749103a8feaSLen Brown 		if (retval != 1)
2750103a8feaSLen Brown 			break;
2751103a8feaSLen Brown 
2752c98d5d94SLen Brown 		retval = func(cpu_num);
2753c98d5d94SLen Brown 		if (retval) {
2754c98d5d94SLen Brown 			fclose(fp);
2755c98d5d94SLen Brown 			return(retval);
2756c98d5d94SLen Brown 		}
2757103a8feaSLen Brown 	}
2758103a8feaSLen Brown 	fclose(fp);
2759c98d5d94SLen Brown 	return 0;
2760103a8feaSLen Brown }
2761103a8feaSLen Brown 
2762103a8feaSLen Brown void re_initialize(void)
2763103a8feaSLen Brown {
2764c98d5d94SLen Brown 	free_all_buffers();
2765c98d5d94SLen Brown 	setup_all_buffers();
2766c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2767103a8feaSLen Brown }
2768103a8feaSLen Brown 
2769843c5791SPrarit Bhargava void set_max_cpu_num(void)
2770843c5791SPrarit Bhargava {
2771843c5791SPrarit Bhargava 	FILE *filep;
2772843c5791SPrarit Bhargava 	unsigned long dummy;
2773843c5791SPrarit Bhargava 
2774843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2775843c5791SPrarit Bhargava 	filep = fopen_or_die(
2776843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2777843c5791SPrarit Bhargava 			"r");
2778843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
27790e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2780843c5791SPrarit Bhargava 	fclose(filep);
2781843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2782843c5791SPrarit Bhargava }
2783c98d5d94SLen Brown 
2784103a8feaSLen Brown /*
2785c98d5d94SLen Brown  * count_cpus()
2786c98d5d94SLen Brown  * remember the last one seen, it will be the max
2787103a8feaSLen Brown  */
2788c98d5d94SLen Brown int count_cpus(int cpu)
2789103a8feaSLen Brown {
2790843c5791SPrarit Bhargava 	topo.num_cpus++;
2791c98d5d94SLen Brown 	return 0;
2792103a8feaSLen Brown }
2793c98d5d94SLen Brown int mark_cpu_present(int cpu)
2794c98d5d94SLen Brown {
2795c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
279615aaa346SLen Brown 	return 0;
2797103a8feaSLen Brown }
2798103a8feaSLen Brown 
27998cb48b32SPrarit Bhargava int init_thread_id(int cpu)
28008cb48b32SPrarit Bhargava {
28018cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
28028cb48b32SPrarit Bhargava 	return 0;
28038cb48b32SPrarit Bhargava }
28048cb48b32SPrarit Bhargava 
2805562a2d37SLen Brown /*
2806562a2d37SLen Brown  * snapshot_proc_interrupts()
2807562a2d37SLen Brown  *
2808562a2d37SLen Brown  * read and record summary of /proc/interrupts
2809562a2d37SLen Brown  *
2810562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2811562a2d37SLen Brown  */
2812562a2d37SLen Brown int snapshot_proc_interrupts(void)
2813562a2d37SLen Brown {
2814562a2d37SLen Brown 	static FILE *fp;
2815562a2d37SLen Brown 	int column, retval;
2816562a2d37SLen Brown 
2817562a2d37SLen Brown 	if (fp == NULL)
2818562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2819562a2d37SLen Brown 	else
2820562a2d37SLen Brown 		rewind(fp);
2821562a2d37SLen Brown 
2822562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2823562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2824562a2d37SLen Brown 		int cpu_number;
2825562a2d37SLen Brown 
2826562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2827562a2d37SLen Brown 		if (retval != 1)
2828562a2d37SLen Brown 			break;
2829562a2d37SLen Brown 
2830562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2831562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2832562a2d37SLen Brown 			return 1;
2833562a2d37SLen Brown 		}
2834562a2d37SLen Brown 
2835562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2836562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2837562a2d37SLen Brown 	}
2838562a2d37SLen Brown 
2839562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2840562a2d37SLen Brown 	while (1) {
2841562a2d37SLen Brown 		int column;
2842562a2d37SLen Brown 		char buf[64];
2843562a2d37SLen Brown 
2844562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2845562a2d37SLen Brown 		if (retval != 1)
2846562a2d37SLen Brown 			break;
2847562a2d37SLen Brown 
2848562a2d37SLen Brown 		/* read the count per cpu */
2849562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2850562a2d37SLen Brown 
2851562a2d37SLen Brown 			int cpu_number, irq_count;
2852562a2d37SLen Brown 
2853562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2854562a2d37SLen Brown 			if (retval != 1)
2855562a2d37SLen Brown 				break;
2856562a2d37SLen Brown 
2857562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2858562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2859562a2d37SLen Brown 
2860562a2d37SLen Brown 		}
2861562a2d37SLen Brown 
2862562a2d37SLen Brown 		while (getc(fp) != '\n')
2863562a2d37SLen Brown 			;	/* flush interrupt description */
2864562a2d37SLen Brown 
2865562a2d37SLen Brown 	}
2866562a2d37SLen Brown 	return 0;
2867562a2d37SLen Brown }
286827d47356SLen Brown /*
2869fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2870fdf676e5SLen Brown  *
2871fdf676e5SLen Brown  * record snapshot of
2872fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2873fdf676e5SLen Brown  *
2874fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2875fdf676e5SLen Brown  */
2876fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2877fdf676e5SLen Brown {
2878fdf676e5SLen Brown 	FILE *fp;
2879fdf676e5SLen Brown 	int retval;
2880fdf676e5SLen Brown 
2881fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2882fdf676e5SLen Brown 
2883fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2884fdf676e5SLen Brown 	if (retval != 1)
2885fdf676e5SLen Brown 		err(1, "GFX rc6");
2886fdf676e5SLen Brown 
2887fdf676e5SLen Brown 	fclose(fp);
2888fdf676e5SLen Brown 
2889fdf676e5SLen Brown 	return 0;
2890fdf676e5SLen Brown }
2891fdf676e5SLen Brown /*
289227d47356SLen Brown  * snapshot_gfx_mhz()
289327d47356SLen Brown  *
289427d47356SLen Brown  * record snapshot of
289527d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
289627d47356SLen Brown  *
289727d47356SLen Brown  * return 1 if config change requires a restart, else return 0
289827d47356SLen Brown  */
289927d47356SLen Brown int snapshot_gfx_mhz(void)
290027d47356SLen Brown {
290127d47356SLen Brown 	static FILE *fp;
290227d47356SLen Brown 	int retval;
290327d47356SLen Brown 
290427d47356SLen Brown 	if (fp == NULL)
290527d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
290622048c54SLen Brown 	else {
290727d47356SLen Brown 		rewind(fp);
290822048c54SLen Brown 		fflush(fp);
290922048c54SLen Brown 	}
291027d47356SLen Brown 
291127d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
291227d47356SLen Brown 	if (retval != 1)
291327d47356SLen Brown 		err(1, "GFX MHz");
291427d47356SLen Brown 
291527d47356SLen Brown 	return 0;
291627d47356SLen Brown }
2917562a2d37SLen Brown 
2918562a2d37SLen Brown /*
2919be0e54c4SLen Brown  * snapshot_cpu_lpi()
2920be0e54c4SLen Brown  *
2921be0e54c4SLen Brown  * record snapshot of
2922be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2923be0e54c4SLen Brown  */
2924be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2925be0e54c4SLen Brown {
2926be0e54c4SLen Brown 	FILE *fp;
2927be0e54c4SLen Brown 	int retval;
2928be0e54c4SLen Brown 
2929be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2930be0e54c4SLen Brown 
2931be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
29325ea7647bSPrarit Bhargava 	if (retval != 1) {
29335ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle CPU output\n");
29345ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_CPU_LPI);
2935605736c6SGustavo A. R. Silva 		fclose(fp);
29365ea7647bSPrarit Bhargava 		return -1;
29375ea7647bSPrarit Bhargava 	}
2938be0e54c4SLen Brown 
2939be0e54c4SLen Brown 	fclose(fp);
2940be0e54c4SLen Brown 
2941be0e54c4SLen Brown 	return 0;
2942be0e54c4SLen Brown }
2943be0e54c4SLen Brown /*
2944be0e54c4SLen Brown  * snapshot_sys_lpi()
2945be0e54c4SLen Brown  *
29461f81c5efSLen Brown  * record snapshot of sys_lpi_file
2947be0e54c4SLen Brown  */
2948be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2949be0e54c4SLen Brown {
2950be0e54c4SLen Brown 	FILE *fp;
2951be0e54c4SLen Brown 	int retval;
2952be0e54c4SLen Brown 
29531f81c5efSLen Brown 	fp = fopen_or_die(sys_lpi_file, "r");
2954be0e54c4SLen Brown 
2955be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
29565ea7647bSPrarit Bhargava 	if (retval != 1) {
29575ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle System output\n");
29585ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_SYS_LPI);
295915423b95SColin Ian King 		fclose(fp);
29605ea7647bSPrarit Bhargava 		return -1;
29615ea7647bSPrarit Bhargava 	}
2962be0e54c4SLen Brown 	fclose(fp);
2963be0e54c4SLen Brown 
2964be0e54c4SLen Brown 	return 0;
2965be0e54c4SLen Brown }
2966be0e54c4SLen Brown /*
2967562a2d37SLen Brown  * snapshot /proc and /sys files
2968562a2d37SLen Brown  *
2969562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2970562a2d37SLen Brown  */
2971562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2972562a2d37SLen Brown {
2973218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2974562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2975562a2d37SLen Brown 			return 1;
2976562a2d37SLen Brown 
2977812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2978fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2979fdf676e5SLen Brown 
2980812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
298127d47356SLen Brown 		snapshot_gfx_mhz();
298227d47356SLen Brown 
2983be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2984be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2985be0e54c4SLen Brown 
2986be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2987be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2988be0e54c4SLen Brown 
2989562a2d37SLen Brown 	return 0;
2990562a2d37SLen Brown }
2991562a2d37SLen Brown 
29928aa2ed0bSLen Brown int exit_requested;
29938aa2ed0bSLen Brown 
29948aa2ed0bSLen Brown static void signal_handler (int signal)
29958aa2ed0bSLen Brown {
29968aa2ed0bSLen Brown 	switch (signal) {
29978aa2ed0bSLen Brown 	case SIGINT:
29988aa2ed0bSLen Brown 		exit_requested = 1;
29998aa2ed0bSLen Brown 		if (debug)
30008aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
30018aa2ed0bSLen Brown 		break;
300207211960SLen Brown 	case SIGUSR1:
300307211960SLen Brown 		if (debug > 1)
300407211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
300507211960SLen Brown 		break;
30068aa2ed0bSLen Brown 	}
30078aa2ed0bSLen Brown }
30088aa2ed0bSLen Brown 
30098aa2ed0bSLen Brown void setup_signal_handler(void)
30108aa2ed0bSLen Brown {
30118aa2ed0bSLen Brown 	struct sigaction sa;
30128aa2ed0bSLen Brown 
30138aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
30148aa2ed0bSLen Brown 
30158aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
30168aa2ed0bSLen Brown 
30178aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
30188aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
301907211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
302007211960SLen Brown 		err(1, "sigaction SIGUSR1");
30218aa2ed0bSLen Brown }
3022b9ad8ee0SLen Brown 
302347936f94SArtem Bityutskiy void do_sleep(void)
3024b9ad8ee0SLen Brown {
3025c026c236SArtem Bityutskiy 	struct timeval tout;
3026c026c236SArtem Bityutskiy 	struct timespec rest;
3027b9ad8ee0SLen Brown 	fd_set readfds;
3028b9ad8ee0SLen Brown 	int retval;
3029b9ad8ee0SLen Brown 
3030b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
3031b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
3032b9ad8ee0SLen Brown 
3033c026c236SArtem Bityutskiy 	if (ignore_stdin) {
303447936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
303547936f94SArtem Bityutskiy 		return;
303647936f94SArtem Bityutskiy 	}
3037b9ad8ee0SLen Brown 
3038c026c236SArtem Bityutskiy 	tout = interval_tv;
3039c026c236SArtem Bityutskiy 	retval = select(1, &readfds, NULL, NULL, &tout);
3040b9ad8ee0SLen Brown 
3041b9ad8ee0SLen Brown 	if (retval == 1) {
3042b9ad8ee0SLen Brown 		switch (getc(stdin)) {
3043b9ad8ee0SLen Brown 		case 'q':
3044b9ad8ee0SLen Brown 			exit_requested = 1;
3045b9ad8ee0SLen Brown 			break;
3046c026c236SArtem Bityutskiy 		case EOF:
3047c026c236SArtem Bityutskiy 			/*
3048c026c236SArtem Bityutskiy 			 * 'stdin' is a pipe closed on the other end. There
3049c026c236SArtem Bityutskiy 			 * won't be any further input.
3050c026c236SArtem Bityutskiy 			 */
3051c026c236SArtem Bityutskiy 			ignore_stdin = 1;
3052c026c236SArtem Bityutskiy 			/* Sleep the rest of the time */
3053c026c236SArtem Bityutskiy 			rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3054c026c236SArtem Bityutskiy 			rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3055c026c236SArtem Bityutskiy 			nanosleep(&rest, NULL);
3056b9ad8ee0SLen Brown 		}
3057b9ad8ee0SLen Brown 	}
3058b9ad8ee0SLen Brown }
305947936f94SArtem Bityutskiy 
30604c2122d4SLen Brown 
3061103a8feaSLen Brown void turbostat_loop()
3062103a8feaSLen Brown {
3063c98d5d94SLen Brown 	int retval;
3064e52966c0SLen Brown 	int restarted = 0;
3065023fe0acSChen Yu 	int done_iters = 0;
3066c98d5d94SLen Brown 
30678aa2ed0bSLen Brown 	setup_signal_handler();
30688aa2ed0bSLen Brown 
3069103a8feaSLen Brown restart:
3070e52966c0SLen Brown 	restarted++;
3071e52966c0SLen Brown 
3072562a2d37SLen Brown 	snapshot_proc_sysfs_files();
3073c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
30744c2122d4SLen Brown 	first_counter_read = 0;
3075d91bb17cSLen Brown 	if (retval < -1) {
3076d91bb17cSLen Brown 		exit(retval);
3077d91bb17cSLen Brown 	} else if (retval == -1) {
3078e52966c0SLen Brown 		if (restarted > 1) {
3079e52966c0SLen Brown 			exit(retval);
3080e52966c0SLen Brown 		}
3081c98d5d94SLen Brown 		re_initialize();
3082c98d5d94SLen Brown 		goto restart;
3083c98d5d94SLen Brown 	}
3084e52966c0SLen Brown 	restarted = 0;
3085023fe0acSChen Yu 	done_iters = 0;
3086103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3087103a8feaSLen Brown 
3088103a8feaSLen Brown 	while (1) {
3089c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3090103a8feaSLen Brown 			re_initialize();
3091103a8feaSLen Brown 			goto restart;
3092103a8feaSLen Brown 		}
3093b9ad8ee0SLen Brown 		do_sleep();
3094562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3095562a2d37SLen Brown 			goto restart;
3096c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3097d91bb17cSLen Brown 		if (retval < -1) {
3098d91bb17cSLen Brown 			exit(retval);
3099d91bb17cSLen Brown 		} else if (retval == -1) {
310015aaa346SLen Brown 			re_initialize();
310115aaa346SLen Brown 			goto restart;
310215aaa346SLen Brown 		}
3103103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3104103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3105ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3106ba3dec99SLen Brown 			re_initialize();
3107ba3dec99SLen Brown 			goto restart;
3108ba3dec99SLen Brown 		}
3109c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3110c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3111b7d8c148SLen Brown 		flush_output_stdout();
31128aa2ed0bSLen Brown 		if (exit_requested)
31138aa2ed0bSLen Brown 			break;
3114023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3115023fe0acSChen Yu 			break;
3116b9ad8ee0SLen Brown 		do_sleep();
3117562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3118562a2d37SLen Brown 			goto restart;
3119c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3120d91bb17cSLen Brown 		if (retval < -1) {
3121d91bb17cSLen Brown 			exit(retval);
3122d91bb17cSLen Brown 		} else if (retval == -1) {
3123103a8feaSLen Brown 			re_initialize();
3124103a8feaSLen Brown 			goto restart;
3125103a8feaSLen Brown 		}
3126103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3127103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3128ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3129ba3dec99SLen Brown 			re_initialize();
3130ba3dec99SLen Brown 			goto restart;
3131ba3dec99SLen Brown 		}
3132c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3133c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3134b7d8c148SLen Brown 		flush_output_stdout();
31358aa2ed0bSLen Brown 		if (exit_requested)
31368aa2ed0bSLen Brown 			break;
3137023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3138023fe0acSChen Yu 			break;
3139103a8feaSLen Brown 	}
3140103a8feaSLen Brown }
3141103a8feaSLen Brown 
3142103a8feaSLen Brown void check_dev_msr()
3143103a8feaSLen Brown {
3144103a8feaSLen Brown 	struct stat sb;
31457ce7d5deSPrarit Bhargava 	char pathname[32];
3146103a8feaSLen Brown 
31477ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31487ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3149a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3150d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3151103a8feaSLen Brown }
3152103a8feaSLen Brown 
3153*fcaa681cSLen Brown /*
3154*fcaa681cSLen Brown  * check for CAP_SYS_RAWIO
3155*fcaa681cSLen Brown  * return 0 on success
3156*fcaa681cSLen Brown  * return 1 on fail
3157*fcaa681cSLen Brown  */
3158*fcaa681cSLen Brown int check_for_cap_sys_rawio(void)
3159103a8feaSLen Brown {
3160*fcaa681cSLen Brown 	cap_t caps;
3161*fcaa681cSLen Brown 	cap_flag_value_t cap_flag_value;
3162*fcaa681cSLen Brown 
3163*fcaa681cSLen Brown 	caps = cap_get_proc();
3164*fcaa681cSLen Brown 	if (caps == NULL)
3165*fcaa681cSLen Brown 		err(-6, "cap_get_proc\n");
3166*fcaa681cSLen Brown 
3167*fcaa681cSLen Brown 	if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3168*fcaa681cSLen Brown 		err(-6, "cap_get\n");
3169*fcaa681cSLen Brown 
3170*fcaa681cSLen Brown 	if (cap_flag_value != CAP_SET) {
3171*fcaa681cSLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
3172*fcaa681cSLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
3173*fcaa681cSLen Brown 		return 1;
3174*fcaa681cSLen Brown 	}
3175*fcaa681cSLen Brown 
3176*fcaa681cSLen Brown 	if (cap_free(caps) == -1)
3177*fcaa681cSLen Brown 		err(-6, "cap_free\n");
3178*fcaa681cSLen Brown 
3179*fcaa681cSLen Brown 	return 0;
3180*fcaa681cSLen Brown }
3181*fcaa681cSLen Brown void check_permissions(void)
3182*fcaa681cSLen Brown {
318398481e79SLen Brown 	int do_exit = 0;
31847ce7d5deSPrarit Bhargava 	char pathname[32];
318598481e79SLen Brown 
318698481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
3187*fcaa681cSLen Brown 	do_exit += check_for_cap_sys_rawio();
318898481e79SLen Brown 
318998481e79SLen Brown 	/* test file permissions */
31907ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31917ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
319298481e79SLen Brown 		do_exit++;
319398481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
319498481e79SLen Brown 	}
319598481e79SLen Brown 
319698481e79SLen Brown 	/* if all else fails, thell them to be root */
319798481e79SLen Brown 	if (do_exit)
3198b2c95d90SJosh Triplett 		if (getuid() != 0)
3199d7899447SLen Brown 			warnx("... or simply run as root");
320098481e79SLen Brown 
320198481e79SLen Brown 	if (do_exit)
320298481e79SLen Brown 		exit(-6);
3203103a8feaSLen Brown }
3204103a8feaSLen Brown 
3205d7899447SLen Brown /*
3206d7899447SLen Brown  * NHM adds support for additional MSRs:
3207d7899447SLen Brown  *
3208d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3209d7899447SLen Brown  *
3210ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
32111df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3212d7899447SLen Brown  *
3213cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3214cf4cbe53SLen Brown  *
3215d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3216d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3217d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3218d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3219d7899447SLen Brown  *
3220ee7e38e3SLen Brown  * Side effect:
32211df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
322233148d67SLen Brown  * sets has_misc_feature_control
3223d7899447SLen Brown  */
3224ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3225103a8feaSLen Brown {
3226ee7e38e3SLen Brown 	unsigned long long msr;
322721ed5574SLen Brown 	unsigned int base_ratio;
3228ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3229ee7e38e3SLen Brown 
3230103a8feaSLen Brown 	if (!genuine_intel)
3231103a8feaSLen Brown 		return 0;
3232103a8feaSLen Brown 
3233103a8feaSLen Brown 	if (family != 6)
3234103a8feaSLen Brown 		return 0;
3235103a8feaSLen Brown 
323621ed5574SLen Brown 	bclk = discover_bclk(family, model);
323721ed5574SLen Brown 
3238103a8feaSLen Brown 	switch (model) {
3239869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3240869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3241ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3242ee7e38e3SLen Brown 		break;
3243869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3244869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3245869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3246869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3247ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
324833148d67SLen Brown 		has_misc_feature_control = 1;
3249ee7e38e3SLen Brown 		break;
3250c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
32515e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3252869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
325377e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
3254c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
32555e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3256869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3257af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3258af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3259ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
326033148d67SLen Brown 		has_misc_feature_control = 1;
3261ee7e38e3SLen Brown 		break;
3262d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3263d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
326433148d67SLen Brown 		has_misc_feature_control = 1;
3265d8ebb442SLen Brown 		break;
3266f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3267cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
32685ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
3269ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3270ee7e38e3SLen Brown 		break;
3271869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3272ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3273cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3274ee7e38e3SLen Brown 		break;
3275869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3276ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3277ee7e38e3SLen Brown 		break;
3278869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3279f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
32805ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
3281f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
3282445640a5SLen Brown 		pkg_cstate_limits = glm_pkg_cstate_limits;
3283e4085d54SLen Brown 		break;
3284103a8feaSLen Brown 	default:
3285103a8feaSLen Brown 		return 0;
3286103a8feaSLen Brown 	}
32871df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3288e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3289ee7e38e3SLen Brown 
3290ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
329121ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
329221ed5574SLen Brown 
329321ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
329421ed5574SLen Brown 	has_base_hz = 1;
3295ee7e38e3SLen Brown 	return 1;
3296103a8feaSLen Brown }
32970f7887c4SLen Brown /*
3298495c7654SLen Brown  * SLV client has support for unique MSRs:
32990f7887c4SLen Brown  *
33000f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
33010f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
33020f7887c4SLen Brown  */
33030f7887c4SLen Brown 
33040f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
33050f7887c4SLen Brown {
33060f7887c4SLen Brown 	if (!genuine_intel)
33070f7887c4SLen Brown 		return 0;
33080f7887c4SLen Brown 
33090f7887c4SLen Brown 	switch (model) {
3310f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3311f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3312f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
33130f7887c4SLen Brown 		return 1;
33140f7887c4SLen Brown 	}
33150f7887c4SLen Brown 	return 0;
33160f7887c4SLen Brown }
33177170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
33187170a374SLen Brown {
33197170a374SLen Brown 
33207170a374SLen Brown 	if (!genuine_intel)
33217170a374SLen Brown 		return 0;
33227170a374SLen Brown 
33237170a374SLen Brown 	switch (model) {
33245ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
33257170a374SLen Brown 		return 1;
33267170a374SLen Brown 	}
33277170a374SLen Brown 	return 0;
33287170a374SLen Brown }
3329ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3330ade0ebacSLen Brown {
3331ade0ebacSLen Brown 
3332ade0ebacSLen Brown 	if (!genuine_intel)
3333ade0ebacSLen Brown 		return 0;
3334ade0ebacSLen Brown 
3335ade0ebacSLen Brown 	switch (model) {
3336ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3337ade0ebacSLen Brown 		return 1;
3338ade0ebacSLen Brown 	}
3339ade0ebacSLen Brown 	return 0;
3340ade0ebacSLen Brown }
334134c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
334234c76197SLen Brown {
334334c76197SLen Brown 
334434c76197SLen Brown 	if (!genuine_intel)
334534c76197SLen Brown 		return 0;
334634c76197SLen Brown 
334734c76197SLen Brown 	switch (model) {
334834c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
334934c76197SLen Brown 		return 1;
335034c76197SLen Brown 	}
335134c76197SLen Brown 	return 0;
335234c76197SLen Brown }
3353f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model)
3354f6708400SChen Yu {
3355f6708400SChen Yu 	if (!genuine_intel)
3356f6708400SChen Yu 		return 0;
3357f6708400SChen Yu 
3358f6708400SChen Yu 	switch (model) {
3359f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:
3360f6708400SChen Yu 		return 1;
3361f6708400SChen Yu 	}
3362f6708400SChen Yu 	return 0;
3363f6708400SChen Yu }
33640f7887c4SLen Brown 
336531e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3366d7899447SLen Brown {
33670f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33680f7887c4SLen Brown 		return 0;
33690f7887c4SLen Brown 
3370d7899447SLen Brown 	switch (model) {
3371d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3372869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3373869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3374d7899447SLen Brown 		return 0;
3375d7899447SLen Brown 	default:
3376d7899447SLen Brown 		return 1;
3377d7899447SLen Brown 	}
3378d7899447SLen Brown }
33790f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
33800f7887c4SLen Brown {
33810f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33820f7887c4SLen Brown 		return 1;
33830f7887c4SLen Brown 
33840f7887c4SLen Brown 	return 0;
33850f7887c4SLen Brown }
33866574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
33876574a5d5SLen Brown {
33886574a5d5SLen Brown 	if (!genuine_intel)
33896574a5d5SLen Brown 		return 0;
33906574a5d5SLen Brown 
33916574a5d5SLen Brown 	if (family != 6)
33926574a5d5SLen Brown 		return 0;
33936574a5d5SLen Brown 
33946574a5d5SLen Brown 	switch (model) {
3395869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3396869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
33976574a5d5SLen Brown 		return 1;
33986574a5d5SLen Brown 	default:
33996574a5d5SLen Brown 		return 0;
34006574a5d5SLen Brown 	}
34016574a5d5SLen Brown }
3402fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3403fcd17211SLen Brown {
3404fcd17211SLen Brown 	if (!genuine_intel)
3405fcd17211SLen Brown 		return 0;
3406fcd17211SLen Brown 
3407fcd17211SLen Brown 	if (family != 6)
3408fcd17211SLen Brown 		return 0;
3409fcd17211SLen Brown 
3410fcd17211SLen Brown 	switch (model) {
3411869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3412fcd17211SLen Brown 		return 1;
3413fcd17211SLen Brown 	default:
3414fcd17211SLen Brown 		return 0;
3415fcd17211SLen Brown 	}
3416fcd17211SLen Brown }
3417fcd17211SLen Brown 
3418fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3419fb5d4327SDasaratharaman Chandramouli {
3420fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3421fb5d4327SDasaratharaman Chandramouli 		return 0;
3422fb5d4327SDasaratharaman Chandramouli 
3423fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3424fb5d4327SDasaratharaman Chandramouli 		return 0;
3425fb5d4327SDasaratharaman Chandramouli 
3426fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3427869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3428fb5d4327SDasaratharaman Chandramouli 		return 1;
3429fb5d4327SDasaratharaman Chandramouli 	default:
3430fb5d4327SDasaratharaman Chandramouli 		return 0;
3431fb5d4327SDasaratharaman Chandramouli 	}
3432fb5d4327SDasaratharaman Chandramouli }
343331e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
343431e07522SLen Brown {
343531e07522SLen Brown 	if (!genuine_intel)
343631e07522SLen Brown 		return 0;
343731e07522SLen Brown 
343831e07522SLen Brown 	if (family != 6)
343931e07522SLen Brown 		return 0;
344031e07522SLen Brown 
344131e07522SLen Brown 	switch (model) {
344231e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
344331e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
344431e07522SLen Brown 		return 1;
344531e07522SLen Brown 	default:
344631e07522SLen Brown 		return 0;
344731e07522SLen Brown 	}
344831e07522SLen Brown }
34496fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
34506fb3143bSLen Brown {
34516fb3143bSLen Brown 	if (!genuine_intel)
34526fb3143bSLen Brown 		return 0;
34536fb3143bSLen Brown 
34546fb3143bSLen Brown 	if (family != 6)
34556fb3143bSLen Brown 		return 0;
34566fb3143bSLen Brown 
34576fb3143bSLen Brown 	switch (model) {
3458869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3459c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
3460869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
346177e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
34625e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3463c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
34645e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3465869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3466af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3467af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3468869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
34696fb3143bSLen Brown 
3470869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
34716fb3143bSLen Brown 		return 1;
34726fb3143bSLen Brown 	default:
34736fb3143bSLen Brown 		return 0;
34746fb3143bSLen Brown 	}
34756fb3143bSLen Brown }
34766fb3143bSLen Brown 
3477fcd17211SLen Brown static void
34781b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3479fcd17211SLen Brown {
3480fcd17211SLen Brown 	if (!do_nhm_platform_info)
3481fcd17211SLen Brown 		return;
3482fcd17211SLen Brown 
3483fcd17211SLen Brown 	dump_nhm_platform_info();
3484fcd17211SLen Brown 
3485fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3486fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3487fcd17211SLen Brown 
3488fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3489fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3490fcd17211SLen Brown 
349131e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
349231e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3493fcd17211SLen Brown 
34940f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
34950f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
34960f7887c4SLen Brown 
3497fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3498fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3499fb5d4327SDasaratharaman Chandramouli 
35006fb3143bSLen Brown 	if (has_config_tdp(family, model))
35016fb3143bSLen Brown 		dump_config_tdp();
35026fb3143bSLen Brown 
3503fcd17211SLen Brown 	dump_nhm_cst_cfg();
3504fcd17211SLen Brown }
3505fcd17211SLen Brown 
350641618e63SLen Brown static void
350741618e63SLen Brown dump_sysfs_cstate_config(void)
350841618e63SLen Brown {
350941618e63SLen Brown 	char path[64];
351041618e63SLen Brown 	char name_buf[16];
351141618e63SLen Brown 	char desc[64];
351241618e63SLen Brown 	FILE *input;
351341618e63SLen Brown 	int state;
351441618e63SLen Brown 	char *sp;
351541618e63SLen Brown 
351641618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
351741618e63SLen Brown 		return;
351841618e63SLen Brown 
351941618e63SLen Brown 	for (state = 0; state < 10; ++state) {
352041618e63SLen Brown 
352141618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
352241618e63SLen Brown 			base_cpu, state);
352341618e63SLen Brown 		input = fopen(path, "r");
352441618e63SLen Brown 		if (input == NULL)
352541618e63SLen Brown 			continue;
35268173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
35278173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
352841618e63SLen Brown 
352941618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
353041618e63SLen Brown 		sp = strchr(name_buf, '-');
353141618e63SLen Brown 		if (!sp)
353241618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
353341618e63SLen Brown 		*sp = '\0';
353441618e63SLen Brown 		fclose(input);
353541618e63SLen Brown 
353641618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
353741618e63SLen Brown 			base_cpu, state);
353841618e63SLen Brown 		input = fopen(path, "r");
353941618e63SLen Brown 		if (input == NULL)
354041618e63SLen Brown 			continue;
35418173c336SBen Hutchings 		if (!fgets(desc, sizeof(desc), input))
35428173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
354341618e63SLen Brown 
354441618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
354541618e63SLen Brown 		fclose(input);
354641618e63SLen Brown 	}
354741618e63SLen Brown }
35487293fccdSLen Brown static void
35497293fccdSLen Brown dump_sysfs_pstate_config(void)
35507293fccdSLen Brown {
35517293fccdSLen Brown 	char path[64];
35527293fccdSLen Brown 	char driver_buf[64];
35537293fccdSLen Brown 	char governor_buf[64];
35547293fccdSLen Brown 	FILE *input;
35557293fccdSLen Brown 	int turbo;
35567293fccdSLen Brown 
35577293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
35587293fccdSLen Brown 			base_cpu);
35597293fccdSLen Brown 	input = fopen(path, "r");
35607293fccdSLen Brown 	if (input == NULL) {
35610a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35627293fccdSLen Brown 		return;
35637293fccdSLen Brown 	}
35648173c336SBen Hutchings 	if (!fgets(driver_buf, sizeof(driver_buf), input))
35658173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
35667293fccdSLen Brown 	fclose(input);
35677293fccdSLen Brown 
35687293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
35697293fccdSLen Brown 			base_cpu);
35707293fccdSLen Brown 	input = fopen(path, "r");
35717293fccdSLen Brown 	if (input == NULL) {
35720a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35737293fccdSLen Brown 		return;
35747293fccdSLen Brown 	}
35758173c336SBen Hutchings 	if (!fgets(governor_buf, sizeof(governor_buf), input))
35768173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
35777293fccdSLen Brown 	fclose(input);
35787293fccdSLen Brown 
35797293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
35807293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
35817293fccdSLen Brown 
35827293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
35837293fccdSLen Brown 	input = fopen(path, "r");
35847293fccdSLen Brown 	if (input != NULL) {
35858173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
35868173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
35877293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
35887293fccdSLen Brown 		fclose(input);
35897293fccdSLen Brown 	}
35907293fccdSLen Brown 
35917293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
35927293fccdSLen Brown 	input = fopen(path, "r");
35937293fccdSLen Brown 	if (input != NULL) {
35948173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
35958173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
35967293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
35977293fccdSLen Brown 		fclose(input);
35987293fccdSLen Brown 	}
35997293fccdSLen Brown }
360041618e63SLen Brown 
36016574a5d5SLen Brown 
3602889facbeSLen Brown /*
3603889facbeSLen Brown  * print_epb()
3604889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3605889facbeSLen Brown  */
3606889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3607889facbeSLen Brown {
3608889facbeSLen Brown 	unsigned long long msr;
3609889facbeSLen Brown 	char *epb_string;
3610889facbeSLen Brown 	int cpu;
3611889facbeSLen Brown 
3612889facbeSLen Brown 	if (!has_epb)
3613889facbeSLen Brown 		return 0;
3614889facbeSLen Brown 
3615889facbeSLen Brown 	cpu = t->cpu_id;
3616889facbeSLen Brown 
3617889facbeSLen Brown 	/* EPB is per-package */
3618889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3619889facbeSLen Brown 		return 0;
3620889facbeSLen Brown 
3621889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3622b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3623889facbeSLen Brown 		return -1;
3624889facbeSLen Brown 	}
3625889facbeSLen Brown 
3626889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3627889facbeSLen Brown 		return 0;
3628889facbeSLen Brown 
3629e9be7dd6SLen Brown 	switch (msr & 0xF) {
3630889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3631889facbeSLen Brown 		epb_string = "performance";
3632889facbeSLen Brown 		break;
3633889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3634889facbeSLen Brown 		epb_string = "balanced";
3635889facbeSLen Brown 		break;
3636889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3637889facbeSLen Brown 		epb_string = "powersave";
3638889facbeSLen Brown 		break;
3639889facbeSLen Brown 	default:
3640889facbeSLen Brown 		epb_string = "custom";
3641889facbeSLen Brown 		break;
3642889facbeSLen Brown 	}
3643b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3644889facbeSLen Brown 
3645889facbeSLen Brown 	return 0;
3646889facbeSLen Brown }
36477f5c258eSLen Brown /*
36487f5c258eSLen Brown  * print_hwp()
36497f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
36507f5c258eSLen Brown  */
36517f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36527f5c258eSLen Brown {
36537f5c258eSLen Brown 	unsigned long long msr;
36547f5c258eSLen Brown 	int cpu;
36557f5c258eSLen Brown 
36567f5c258eSLen Brown 	if (!has_hwp)
36577f5c258eSLen Brown 		return 0;
36587f5c258eSLen Brown 
36597f5c258eSLen Brown 	cpu = t->cpu_id;
36607f5c258eSLen Brown 
36617f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
36627f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36637f5c258eSLen Brown 		return 0;
36647f5c258eSLen Brown 
36657f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3666b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36677f5c258eSLen Brown 		return -1;
36687f5c258eSLen Brown 	}
36697f5c258eSLen Brown 
36707f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
36717f5c258eSLen Brown 		return 0;
36727f5c258eSLen Brown 
3673b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
36747f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
36757f5c258eSLen Brown 
36767f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
36777f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
36787f5c258eSLen Brown 		return 0;
36797f5c258eSLen Brown 
36807f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
36817f5c258eSLen Brown 		return 0;
36827f5c258eSLen Brown 
3683b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
36846dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
36857f5c258eSLen Brown 			cpu, msr,
36867f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
36877f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
36887f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
36897f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
36907f5c258eSLen Brown 
36917f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
36927f5c258eSLen Brown 		return 0;
36937f5c258eSLen Brown 
3694b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
36956dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
36967f5c258eSLen Brown 			cpu, msr,
36977f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36987f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36997f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
37007f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
37017f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
37027f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
37037f5c258eSLen Brown 
37047f5c258eSLen Brown 	if (has_hwp_pkg) {
37057f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
37067f5c258eSLen Brown 			return 0;
37077f5c258eSLen Brown 
3708b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
37096dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
37107f5c258eSLen Brown 			cpu, msr,
37117f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
37127f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
37137f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
37147f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
37157f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
37167f5c258eSLen Brown 	}
37177f5c258eSLen Brown 	if (has_hwp_notify) {
37187f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
37197f5c258eSLen Brown 			return 0;
37207f5c258eSLen Brown 
3721b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
37227f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
37237f5c258eSLen Brown 			cpu, msr,
37247f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
37257f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
37267f5c258eSLen Brown 	}
37277f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
37287f5c258eSLen Brown 		return 0;
37297f5c258eSLen Brown 
3730b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
37317f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
37327f5c258eSLen Brown 			cpu, msr,
37337f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
37347f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3735889facbeSLen Brown 
3736889facbeSLen Brown 	return 0;
3737889facbeSLen Brown }
3738889facbeSLen Brown 
37393a9a941dSLen Brown /*
37403a9a941dSLen Brown  * print_perf_limit()
37413a9a941dSLen Brown  */
37423a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
37433a9a941dSLen Brown {
37443a9a941dSLen Brown 	unsigned long long msr;
37453a9a941dSLen Brown 	int cpu;
37463a9a941dSLen Brown 
37473a9a941dSLen Brown 	cpu = t->cpu_id;
37483a9a941dSLen Brown 
37493a9a941dSLen Brown 	/* per-package */
37503a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
37513a9a941dSLen Brown 		return 0;
37523a9a941dSLen Brown 
37533a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3754b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
37553a9a941dSLen Brown 		return -1;
37563a9a941dSLen Brown 	}
37573a9a941dSLen Brown 
37583a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
37593a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3760b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3761b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3762e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
37633a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3764e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3765e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3766e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3767e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3768e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3769e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3770e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3771e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3772e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3773e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3774e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3775e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3776b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3777e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
37783a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3779e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3780e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3781e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3782e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3783e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3784e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3785e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3786e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3787e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3788e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3789e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3790e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
37913a9a941dSLen Brown 
37923a9a941dSLen Brown 	}
37933a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
37943a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3795b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3796b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
37973a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37983a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37993a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
38003a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
38013a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
38023a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
38033a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
38043a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3805b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
38063a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
38073a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
38083a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
38093a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
38103a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
38113a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
38123a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
38133a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
38143a9a941dSLen Brown 	}
38153a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
38163a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3817b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3818b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
38193a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
38203a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
38213a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
38223a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
38233a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
38243a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3825b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
38263a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
38273a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
38283a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
38293a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
38303a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
38313a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
38323a9a941dSLen Brown 	}
38333a9a941dSLen Brown 	return 0;
38343a9a941dSLen Brown }
38353a9a941dSLen Brown 
3836889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3837889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3838889facbeSLen Brown 
38399392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
3840144b44b1SLen Brown {
3841144b44b1SLen Brown 	unsigned long long msr;
3842144b44b1SLen Brown 
3843144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
38447ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3845144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3846144b44b1SLen Brown 
3847144b44b1SLen Brown 	switch (model) {
3848f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
38495ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:
3850144b44b1SLen Brown 		return 30.0;
3851144b44b1SLen Brown 	default:
3852144b44b1SLen Brown 		return 135.0;
3853144b44b1SLen Brown 	}
3854144b44b1SLen Brown }
3855144b44b1SLen Brown 
38569392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
38579392bd98SCalvin Walton {
38589392bd98SCalvin Walton 	switch (family) {
38599392bd98SCalvin Walton 	case 0x17:
3860c1c10cc7SPu Wen 	case 0x18:
38619392bd98SCalvin Walton 	default:
38629392bd98SCalvin Walton 		/* This is the max stock TDP of HEDT/Server Fam17h chips */
38639392bd98SCalvin Walton 		return 250.0;
38649392bd98SCalvin Walton 	}
38659392bd98SCalvin Walton }
38669392bd98SCalvin Walton 
386740ee8e3bSAndrey Semin /*
386840ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
386940ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
387040ee8e3bSAndrey Semin  */
387140ee8e3bSAndrey Semin static double
387240ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
387340ee8e3bSAndrey Semin {
387440ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
387540ee8e3bSAndrey Semin 
387640ee8e3bSAndrey Semin 	switch (model) {
3877869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3878869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3879869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
388040ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
388140ee8e3bSAndrey Semin 	default:
388240ee8e3bSAndrey Semin 		return (rapl_energy_units);
388340ee8e3bSAndrey Semin 	}
388440ee8e3bSAndrey Semin }
388540ee8e3bSAndrey Semin 
38869392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
3887889facbeSLen Brown {
3888889facbeSLen Brown 	unsigned long long msr;
3889144b44b1SLen Brown 	unsigned int time_unit;
3890889facbeSLen Brown 	double tdp;
3891889facbeSLen Brown 
3892889facbeSLen Brown 	if (family != 6)
3893889facbeSLen Brown 		return;
3894889facbeSLen Brown 
3895889facbeSLen Brown 	switch (model) {
3896869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3897869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3898c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
389977e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
39005e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3901c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
39025e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3903144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3904812db3f7SLen Brown 		if (rapl_joules) {
3905812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3906812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3907812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3908812db3f7SLen Brown 		} else {
3909812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3910812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3911812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3912812db3f7SLen Brown 		}
3913889facbeSLen Brown 		break;
3914869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3915f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3916e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3917812db3f7SLen Brown 		if (rapl_joules)
3918812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3919812db3f7SLen Brown 		else
3920812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3921e4085d54SLen Brown 		break;
3922f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
3923f6708400SChen 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;
3924f6708400SChen Yu 		if (rapl_joules) {
3925f6708400SChen Yu 			BIC_PRESENT(BIC_Pkg_J);
3926f6708400SChen Yu 			BIC_PRESENT(BIC_Cor_J);
3927f6708400SChen Yu 			BIC_PRESENT(BIC_RAM_J);
3928f6708400SChen Yu 			BIC_PRESENT(BIC_GFX_J);
3929f6708400SChen Yu 		} else {
3930f6708400SChen Yu 			BIC_PRESENT(BIC_PkgWatt);
3931f6708400SChen Yu 			BIC_PRESENT(BIC_CorWatt);
3932f6708400SChen Yu 			BIC_PRESENT(BIC_RAMWatt);
3933f6708400SChen Yu 			BIC_PRESENT(BIC_GFXWatt);
3934f6708400SChen Yu 		}
3935f6708400SChen Yu 		break;
3936af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3937af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
393881824921SLen 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;
3939812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3940812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3941812db3f7SLen Brown 		if (rapl_joules) {
3942812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3943812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3944812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
394581824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3946812db3f7SLen Brown 		} else {
3947812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3948812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3949812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
395081824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3951812db3f7SLen Brown 		}
39520b2bb692SLen Brown 		break;
3953869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3954869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3955869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3956869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
39570b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | 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_RAM_J);
3963812db3f7SLen Brown 		} else {
3964812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3965812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3966812db3f7SLen Brown 		}
3967e6f9bb3cSLen Brown 		break;
3968869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3969869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
39700b2bb692SLen 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;
3971812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3972812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3973812db3f7SLen Brown 		if (rapl_joules) {
3974812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3975812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3976812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3977812db3f7SLen Brown 		} else {
3978812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3979812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3980812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3981812db3f7SLen Brown 		}
3982144b44b1SLen Brown 		break;
3983f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
39845ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
3985144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3986812db3f7SLen Brown 		if (rapl_joules) {
3987812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3988812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3989812db3f7SLen Brown 		} else {
3990812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3991812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3992812db3f7SLen Brown 		}
3993889facbeSLen Brown 		break;
39945ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
39950f644909SJacob 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;
3996812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3997812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3998812db3f7SLen Brown 		if (rapl_joules) {
3999812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4000812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4001812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4002812db3f7SLen Brown 		} else {
4003812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4004812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4005812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4006812db3f7SLen Brown 		}
40070f644909SJacob Pan 		break;
4008889facbeSLen Brown 	default:
4009889facbeSLen Brown 		return;
4010889facbeSLen Brown 	}
4011889facbeSLen Brown 
4012889facbeSLen Brown 	/* units on package 0, verify later other packages match */
40137ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4014889facbeSLen Brown 		return;
4015889facbeSLen Brown 
4016889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
4017f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
4018144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4019144b44b1SLen Brown 	else
4020889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4021889facbeSLen Brown 
402240ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
402340ee8e3bSAndrey Semin 
4024144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
4025144b44b1SLen Brown 	if (time_unit == 0)
4026144b44b1SLen Brown 		time_unit = 0xA;
4027889facbeSLen Brown 
4028144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
4029144b44b1SLen Brown 
40309392bd98SCalvin Walton 	tdp = get_tdp_intel(model);
4031889facbeSLen Brown 
4032889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
403396e47158SLen Brown 	if (!quiet)
4034b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
40359392bd98SCalvin Walton }
4036889facbeSLen Brown 
40379392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
40389392bd98SCalvin Walton {
40399392bd98SCalvin Walton 	unsigned long long msr;
40409392bd98SCalvin Walton 	unsigned int eax, ebx, ecx, edx;
40419392bd98SCalvin Walton 	unsigned int has_rapl = 0;
40429392bd98SCalvin Walton 	double tdp;
40439392bd98SCalvin Walton 
40449392bd98SCalvin Walton 	if (max_extended_level >= 0x80000007) {
40459392bd98SCalvin Walton 		__cpuid(0x80000007, eax, ebx, ecx, edx);
40469392bd98SCalvin Walton 		/* RAPL (Fam 17h) */
40479392bd98SCalvin Walton 		has_rapl = edx & (1 << 14);
40489392bd98SCalvin Walton 	}
40499392bd98SCalvin Walton 
40509392bd98SCalvin Walton 	if (!has_rapl)
4051889facbeSLen Brown 		return;
40529392bd98SCalvin Walton 
40539392bd98SCalvin Walton 	switch (family) {
40549392bd98SCalvin Walton 	case 0x17: /* Zen, Zen+ */
4055c1c10cc7SPu Wen 	case 0x18: /* Hygon Dhyana */
40569392bd98SCalvin Walton 		do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
40573316f99aSCalvin Walton 		if (rapl_joules) {
40583316f99aSCalvin Walton 			BIC_PRESENT(BIC_Pkg_J);
40599392bd98SCalvin Walton 			BIC_PRESENT(BIC_Cor_J);
40603316f99aSCalvin Walton 		} else {
40613316f99aSCalvin Walton 			BIC_PRESENT(BIC_PkgWatt);
40629392bd98SCalvin Walton 			BIC_PRESENT(BIC_CorWatt);
40633316f99aSCalvin Walton 		}
40649392bd98SCalvin Walton 		break;
40659392bd98SCalvin Walton 	default:
40669392bd98SCalvin Walton 		return;
40679392bd98SCalvin Walton 	}
40689392bd98SCalvin Walton 
40699392bd98SCalvin Walton 	if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
40709392bd98SCalvin Walton 		return;
40719392bd98SCalvin Walton 
40729392bd98SCalvin Walton 	rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
40739392bd98SCalvin Walton 	rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
40749392bd98SCalvin Walton 	rapl_power_units = ldexp(1.0, -(msr & 0xf));
40759392bd98SCalvin Walton 
40769cfa8e04SPu Wen 	tdp = get_tdp_amd(family);
40779392bd98SCalvin Walton 
40789392bd98SCalvin Walton 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
40799392bd98SCalvin Walton 	if (!quiet)
40809392bd98SCalvin Walton 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
40819392bd98SCalvin Walton }
40829392bd98SCalvin Walton 
40839392bd98SCalvin Walton /*
40849392bd98SCalvin Walton  * rapl_probe()
40859392bd98SCalvin Walton  *
40869392bd98SCalvin Walton  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
40879392bd98SCalvin Walton  */
40889392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
40899392bd98SCalvin Walton {
40909392bd98SCalvin Walton 	if (genuine_intel)
40919392bd98SCalvin Walton 		rapl_probe_intel(family, model);
4092c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine)
40939392bd98SCalvin Walton 		rapl_probe_amd(family, model);
4094889facbeSLen Brown }
4095889facbeSLen Brown 
40961b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
40973a9a941dSLen Brown {
40983a9a941dSLen Brown 	if (!genuine_intel)
40993a9a941dSLen Brown 		return;
41003a9a941dSLen Brown 
41013a9a941dSLen Brown 	if (family != 6)
41023a9a941dSLen Brown 		return;
41033a9a941dSLen Brown 
41043a9a941dSLen Brown 	switch (model) {
4105c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
410677e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
41075e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
41083a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
4109869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
41103a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
41113a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
41123a9a941dSLen Brown 	default:
41133a9a941dSLen Brown 		return;
41143a9a941dSLen Brown 	}
41153a9a941dSLen Brown }
41163a9a941dSLen Brown 
4117ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4118ac980e13SArtem Bityutskiy {
4119ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
4120ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
4121ac980e13SArtem Bityutskiy }
4122ac980e13SArtem Bityutskiy 
4123889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4124889facbeSLen Brown {
4125889facbeSLen Brown 	unsigned long long msr;
4126f4896fa5SLen Brown 	unsigned int dts, dts2;
4127889facbeSLen Brown 	int cpu;
4128889facbeSLen Brown 
4129889facbeSLen Brown 	if (!(do_dts || do_ptm))
4130889facbeSLen Brown 		return 0;
4131889facbeSLen Brown 
4132889facbeSLen Brown 	cpu = t->cpu_id;
4133889facbeSLen Brown 
4134889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
4135889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4136889facbeSLen Brown 		return 0;
4137889facbeSLen Brown 
4138889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4139b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4140889facbeSLen Brown 		return -1;
4141889facbeSLen Brown 	}
4142889facbeSLen Brown 
4143889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4144889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4145889facbeSLen Brown 			return 0;
4146889facbeSLen Brown 
4147889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4148b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
4149889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
4150889facbeSLen Brown 
4151889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4152889facbeSLen Brown 			return 0;
4153889facbeSLen Brown 
4154889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4155889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4156b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4157889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4158889facbeSLen Brown 	}
4159889facbeSLen Brown 
4160889facbeSLen Brown 
4161f4896fa5SLen Brown 	if (do_dts && debug) {
4162889facbeSLen Brown 		unsigned int resolution;
4163889facbeSLen Brown 
4164889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4165889facbeSLen Brown 			return 0;
4166889facbeSLen Brown 
4167889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4168889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4169b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4170889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
4171889facbeSLen Brown 
4172889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4173889facbeSLen Brown 			return 0;
4174889facbeSLen Brown 
4175889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4176889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4177b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4178889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4179889facbeSLen Brown 	}
4180889facbeSLen Brown 
4181889facbeSLen Brown 	return 0;
4182889facbeSLen Brown }
4183889facbeSLen Brown 
4184889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4185889facbeSLen Brown {
4186b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4187889facbeSLen Brown 		cpu, label,
4188889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4189889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4190889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4191889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4192889facbeSLen Brown 
4193889facbeSLen Brown 	return;
4194889facbeSLen Brown }
4195889facbeSLen Brown 
4196889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4197889facbeSLen Brown {
4198889facbeSLen Brown 	unsigned long long msr;
41999392bd98SCalvin Walton 	const char *msr_name;
4200889facbeSLen Brown 	int cpu;
4201889facbeSLen Brown 
4202889facbeSLen Brown 	if (!do_rapl)
4203889facbeSLen Brown 		return 0;
4204889facbeSLen Brown 
4205889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4206889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4207889facbeSLen Brown 		return 0;
4208889facbeSLen Brown 
4209889facbeSLen Brown 	cpu = t->cpu_id;
4210889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4211b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4212889facbeSLen Brown 		return -1;
4213889facbeSLen Brown 	}
4214889facbeSLen Brown 
42159392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
42169392bd98SCalvin Walton 		msr_name = "MSR_RAPL_PWR_UNIT";
42179392bd98SCalvin Walton 		if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
42189392bd98SCalvin Walton 			return -1;
42199392bd98SCalvin Walton 	} else {
42209392bd98SCalvin Walton 		msr_name = "MSR_RAPL_POWER_UNIT";
4221889facbeSLen Brown 		if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4222889facbeSLen Brown 			return -1;
42239392bd98SCalvin Walton 	}
4224889facbeSLen Brown 
42259392bd98SCalvin Walton 	fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4226144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
422796e47158SLen Brown 
4228144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4229144b44b1SLen Brown 
4230889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4231889facbeSLen Brown                 	return -5;
4232889facbeSLen Brown 
4233889facbeSLen Brown 
4234b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4235889facbeSLen Brown 			cpu, msr,
4236889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4237889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4238889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4239889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4240889facbeSLen Brown 
4241144b44b1SLen Brown 	}
4242144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4243144b44b1SLen Brown 
4244889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4245889facbeSLen Brown 			return -9;
4246889facbeSLen Brown 
4247b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4248889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4249889facbeSLen Brown 
4250889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4251b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4252889facbeSLen Brown 			cpu,
4253889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4254889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4255889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4256889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4257889facbeSLen Brown 	}
4258889facbeSLen Brown 
42590b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4260889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4261889facbeSLen Brown                 	return -6;
4262889facbeSLen Brown 
4263b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4264889facbeSLen Brown 			cpu, msr,
4265889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4266889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4267889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4268889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
42690b2bb692SLen Brown 	}
42700b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4271889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4272889facbeSLen Brown 			return -9;
4273b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4274889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4275889facbeSLen Brown 
4276889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4277889facbeSLen Brown 	}
4278144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4279889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4280889facbeSLen Brown 			return -7;
4281889facbeSLen Brown 
4282b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4283144b44b1SLen Brown 	}
42849148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4285889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4286889facbeSLen Brown 			return -9;
4287b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4288889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4289889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4290889facbeSLen Brown 	}
4291889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4292889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4293889facbeSLen Brown 			return -8;
4294889facbeSLen Brown 
4295b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4296889facbeSLen Brown 
4297889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4298889facbeSLen Brown 			return -9;
4299b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4300889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4301889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4302889facbeSLen Brown 	}
4303889facbeSLen Brown 	return 0;
4304889facbeSLen Brown }
4305889facbeSLen Brown 
4306d7899447SLen Brown /*
4307d7899447SLen Brown  * SNB adds support for additional MSRs:
4308d7899447SLen Brown  *
4309d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4310d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4311d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4312d7899447SLen Brown  */
4313103a8feaSLen Brown 
4314d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4315103a8feaSLen Brown {
4316103a8feaSLen Brown 	if (!genuine_intel)
4317103a8feaSLen Brown 		return 0;
4318103a8feaSLen Brown 
4319103a8feaSLen Brown 	switch (model) {
4320869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4321869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4322869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:		/* IVB */
4323869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:		/* IVB Xeon */
4324c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:		/* HSW */
4325869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:		/* HSW */
432677e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:		/* HSW */
43275e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:		/* HSW */
4328c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:		/* BDW */
43295e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:		/* BDW */
4330869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:		/* BDX */
4331af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:		/* SKL */
4332af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:		/* CNL */
4333869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:		/* SKX */
4334869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:		/* BXT */
4335f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
43365ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
4337f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:		/* EHL */
4338103a8feaSLen Brown 		return 1;
4339103a8feaSLen Brown 	}
4340103a8feaSLen Brown 	return 0;
4341103a8feaSLen Brown }
4342103a8feaSLen Brown 
4343d7899447SLen Brown /*
4344570992fcSLen Brown  * HSW ULT added support for C8/C9/C10 MSRs:
4345d7899447SLen Brown  *
4346d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4347d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4348d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
43495a63426eSLen Brown  *
43505a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
43515a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
43525a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
43535a63426eSLen Brown  *
4354d7899447SLen Brown  */
4355570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model)
4356ca58710fSKristen Carlson Accardi {
4357ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4358ca58710fSKristen Carlson Accardi 		return 0;
4359ca58710fSKristen Carlson Accardi 
4360ca58710fSKristen Carlson Accardi 	switch (model) {
436177e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
4362c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
4363af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4364af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
4365869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4366f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4367f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
4368ca58710fSKristen Carlson Accardi 		return 1;
4369ca58710fSKristen Carlson Accardi 	}
4370ca58710fSKristen Carlson Accardi 	return 0;
4371ca58710fSKristen Carlson Accardi }
4372ca58710fSKristen Carlson Accardi 
43730b2bb692SLen Brown /*
43740b2bb692SLen Brown  * SKL adds support for additional MSRS:
43750b2bb692SLen Brown  *
43760b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
43770b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
43780b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
43790b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
43800b2bb692SLen Brown  */
43810b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
43820b2bb692SLen Brown {
43830b2bb692SLen Brown 	if (!genuine_intel)
43840b2bb692SLen Brown 		return 0;
43850b2bb692SLen Brown 
43860b2bb692SLen Brown 	switch (model) {
4387af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4388af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
43890b2bb692SLen Brown 		return 1;
43900b2bb692SLen Brown 	}
43910b2bb692SLen Brown 	return 0;
43920b2bb692SLen Brown }
43930b2bb692SLen Brown 
4394144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4395144b44b1SLen Brown {
4396144b44b1SLen Brown 	if (!genuine_intel)
4397144b44b1SLen Brown 		return 0;
4398144b44b1SLen Brown 	switch (model) {
4399f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
44005ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
4401144b44b1SLen Brown 		return 1;
4402144b44b1SLen Brown 	}
4403144b44b1SLen Brown 	return 0;
4404144b44b1SLen Brown }
4405144b44b1SLen Brown 
4406fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4407fb5d4327SDasaratharaman Chandramouli {
4408fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4409fb5d4327SDasaratharaman Chandramouli 		return 0;
4410fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4411869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4412fb5d4327SDasaratharaman Chandramouli 		return 1;
4413fb5d4327SDasaratharaman Chandramouli 	}
4414fb5d4327SDasaratharaman Chandramouli 	return 0;
4415fb5d4327SDasaratharaman Chandramouli }
4416fb5d4327SDasaratharaman Chandramouli 
4417997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4418997e5395SSrinivas Pandruvada {
4419997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4420997e5395SSrinivas Pandruvada 		return 0;
4421997e5395SSrinivas Pandruvada 
4422997e5395SSrinivas Pandruvada 	switch (model) {
4423af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L: /* CNL */
4424997e5395SSrinivas Pandruvada 		return 1;
4425997e5395SSrinivas Pandruvada 	}
4426997e5395SSrinivas Pandruvada 
4427997e5395SSrinivas Pandruvada 	return 0;
4428997e5395SSrinivas Pandruvada }
4429997e5395SSrinivas Pandruvada 
4430b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4431b2b34dfeSHubert Chrzaniuk {
4432b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4433b2b34dfeSHubert Chrzaniuk 		return 1024;
4434b2b34dfeSHubert Chrzaniuk 	return 1;
4435b2b34dfeSHubert Chrzaniuk }
4436b2b34dfeSHubert Chrzaniuk 
4437144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4438144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4439144b44b1SLen Brown 
4440144b44b1SLen Brown double slm_bclk(void)
4441144b44b1SLen Brown {
4442144b44b1SLen Brown 	unsigned long long msr = 3;
4443144b44b1SLen Brown 	unsigned int i;
4444144b44b1SLen Brown 	double freq;
4445144b44b1SLen Brown 
44467ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4447b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4448144b44b1SLen Brown 
4449144b44b1SLen Brown 	i = msr & 0xf;
4450144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4451b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
44520a91e551SColin Ian King 		i = 3;
4453144b44b1SLen Brown 	}
4454144b44b1SLen Brown 	freq = slm_freq_table[i];
4455144b44b1SLen Brown 
445696e47158SLen Brown 	if (!quiet)
4457b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4458144b44b1SLen Brown 
4459144b44b1SLen Brown 	return freq;
4460144b44b1SLen Brown }
4461144b44b1SLen Brown 
4462103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4463103a8feaSLen Brown {
4464121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4465103a8feaSLen Brown 		return 100.00;
4466144b44b1SLen Brown 	else if (is_slm(family, model))
4467144b44b1SLen Brown 		return slm_bclk();
4468103a8feaSLen Brown 	else
4469103a8feaSLen Brown 		return 133.33;
4470103a8feaSLen Brown }
4471103a8feaSLen Brown 
4472889facbeSLen Brown /*
4473889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4474889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4475889facbeSLen Brown  * This is usually equal to tjMax.
4476889facbeSLen Brown  *
4477889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4478889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4479889facbeSLen Brown  *
4480889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4481889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4482889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4483889facbeSLen Brown  */
4484889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4485889facbeSLen Brown {
4486889facbeSLen Brown 	unsigned long long msr;
4487889facbeSLen Brown 	unsigned int target_c_local;
4488889facbeSLen Brown 	int cpu;
4489889facbeSLen Brown 
4490889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4491889facbeSLen Brown 	if (!(do_dts || do_ptm))
4492889facbeSLen Brown 		return 0;
4493889facbeSLen Brown 
4494889facbeSLen Brown 	/* this is a per-package concept */
4495889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4496889facbeSLen Brown 		return 0;
4497889facbeSLen Brown 
4498889facbeSLen Brown 	cpu = t->cpu_id;
4499889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4500b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4501889facbeSLen Brown 		return -1;
4502889facbeSLen Brown 	}
4503889facbeSLen Brown 
4504889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4505889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4506b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4507889facbeSLen Brown 			cpu, tcc_activation_temp);
4508889facbeSLen Brown 		return 0;
4509889facbeSLen Brown 	}
4510889facbeSLen Brown 
4511889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4512d7899447SLen Brown 	if (!do_nhm_platform_info)
4513889facbeSLen Brown 		goto guess;
4514889facbeSLen Brown 
45157ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4516889facbeSLen Brown 		goto guess;
4517889facbeSLen Brown 
45183482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4519889facbeSLen Brown 
452096e47158SLen Brown 	if (!quiet)
4521b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4522889facbeSLen Brown 			cpu, msr, target_c_local);
4523889facbeSLen Brown 
45243482124aSJean Delvare 	if (!target_c_local)
4525889facbeSLen Brown 		goto guess;
4526889facbeSLen Brown 
4527889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4528889facbeSLen Brown 
4529889facbeSLen Brown 	return 0;
4530889facbeSLen Brown 
4531889facbeSLen Brown guess:
4532889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4533b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4534889facbeSLen Brown 		cpu, tcc_activation_temp);
4535889facbeSLen Brown 
4536889facbeSLen Brown 	return 0;
4537889facbeSLen Brown }
453869807a63SLen Brown 
4539aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4540aa8d8cc7SLen Brown {
4541aa8d8cc7SLen Brown 	unsigned long long msr;
4542aa8d8cc7SLen Brown 
4543f6505c88SSean Christopherson 	if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
4544aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4545aa8d8cc7SLen Brown 			base_cpu, msr,
4546f6505c88SSean Christopherson 			msr & FEAT_CTL_LOCKED ? "" : "UN-",
4547aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4548aa8d8cc7SLen Brown }
4549aa8d8cc7SLen Brown 
455069807a63SLen Brown void decode_misc_enable_msr(void)
455169807a63SLen Brown {
455269807a63SLen Brown 	unsigned long long msr;
455369807a63SLen Brown 
4554f26b1519SLen Brown 	if (!genuine_intel)
4555f26b1519SLen Brown 		return;
4556f26b1519SLen Brown 
455769807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4558e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
455969807a63SLen Brown 			base_cpu, msr,
4560e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4561e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4562fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4563e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4564e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
456569807a63SLen Brown }
456669807a63SLen Brown 
456733148d67SLen Brown void decode_misc_feature_control(void)
456833148d67SLen Brown {
456933148d67SLen Brown 	unsigned long long msr;
457033148d67SLen Brown 
457133148d67SLen Brown 	if (!has_misc_feature_control)
457233148d67SLen Brown 		return;
457333148d67SLen Brown 
457433148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
457533148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
457633148d67SLen Brown 			base_cpu, msr,
457733148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
457833148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
457933148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
458033148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
458133148d67SLen Brown }
4582f0057310SLen Brown /*
4583f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4584f0057310SLen Brown  *
4585f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4586f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4587f0057310SLen Brown  * bit[1] less so...
4588f0057310SLen Brown  */
4589f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4590f0057310SLen Brown {
4591f0057310SLen Brown 	unsigned long long msr;
4592f0057310SLen Brown 
4593f0057310SLen Brown 	if (!do_nhm_platform_info)
4594f0057310SLen Brown 		return;
4595f0057310SLen Brown 
4596cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4597cf4cbe53SLen Brown 		return;
4598cf4cbe53SLen Brown 
4599f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4600ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4601f0057310SLen Brown 			base_cpu, msr,
4602f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4603ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4604ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4605f0057310SLen Brown }
460671616c8eSLen Brown /*
460771616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
460871616c8eSLen Brown  *
460971616c8eSLen Brown  * This MSRs are present on Silvermont processors,
461071616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
461171616c8eSLen Brown  */
461271616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
461371616c8eSLen Brown {
461471616c8eSLen Brown 	unsigned long long msr;
461571616c8eSLen Brown 
461671616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
461771616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
461871616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
461971616c8eSLen Brown 
462071616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
462171616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
462271616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
462371616c8eSLen Brown }
46247f5c258eSLen Brown 
4625f5a4c76aSLen Brown /*
4626f5a4c76aSLen Brown  * When models are the same, for the purpose of turbostat, reuse
4627f5a4c76aSLen Brown  */
4628f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
4629f5a4c76aSLen Brown {
4630f5a4c76aSLen Brown 
4631f5a4c76aSLen Brown 	switch(model) {
4632f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
4633f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
4634f5a4c76aSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
4635f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
4636f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
4637f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM;
4638f5a4c76aSLen Brown 
4639f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
4640f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
4641f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM_EX;
4642f5a4c76aSLen Brown 
4643f5a4c76aSLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4644f5a4c76aSLen Brown 		return INTEL_FAM6_XEON_PHI_KNL;
4645f5a4c76aSLen Brown 
4646f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_X:
46475ebb34edSPeter Zijlstra 	case INTEL_FAM6_BROADWELL_D:	/* BDX-DE */
4648f5a4c76aSLen Brown 		return INTEL_FAM6_BROADWELL_X;
4649f5a4c76aSLen Brown 
4650af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:
4651c66f78a6SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE:
4652af239c44SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE_L:
4653c66f78a6SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE:
4654081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE_L:
4655081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE:
4656af239c44SPeter Zijlstra 		return INTEL_FAM6_SKYLAKE_L;
4657937807d3SLen Brown 
4658af239c44SPeter Zijlstra 	case INTEL_FAM6_ICELAKE_L:
4659d93ea567SRajneesh Bhardwaj 	case INTEL_FAM6_ICELAKE_NNPI:
46604bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE_L:
46614bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE:
4662af239c44SPeter Zijlstra 		return INTEL_FAM6_CANNONLAKE_L;
4663b62b3184SZhang Rui 
466477e5517cSIngo Molnar 	case INTEL_FAM6_ATOM_TREMONT_D:
466577e5517cSIngo Molnar 		return INTEL_FAM6_ATOM_GOLDMONT_D;
466623274fafSChen Yu 
4667d7814c30SChen Yu 	case INTEL_FAM6_ATOM_TREMONT_L:
4668d7814c30SChen Yu 		return INTEL_FAM6_ATOM_TREMONT;
4669d7814c30SChen Yu 
467023274fafSChen Yu 	case INTEL_FAM6_ICELAKE_X:
467123274fafSChen Yu 		return INTEL_FAM6_SKYLAKE_X;
4672f5a4c76aSLen Brown 	}
4673f5a4c76aSLen Brown 	return model;
4674f5a4c76aSLen Brown }
4675fcd17211SLen Brown void process_cpuid()
4676103a8feaSLen Brown {
467734041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
467834041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
4679b3a34e93SLen Brown 	unsigned int has_turbo;
4680103a8feaSLen Brown 
4681103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4682103a8feaSLen Brown 
46835aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4684103a8feaSLen Brown 
468534041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
4686103a8feaSLen Brown 		genuine_intel = 1;
468734041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
468834041551SLen Brown 		authentic_amd = 1;
4689c1c10cc7SPu Wen 	else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
4690c1c10cc7SPu Wen 		hygon_genuine = 1;
4691103a8feaSLen Brown 
469296e47158SLen Brown 	if (!quiet)
4693b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4694103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4695103a8feaSLen Brown 
46965aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4697103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4698103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4699103a8feaSLen Brown 	stepping = fms & 0xf;
47005aa3d1a2SCalvin Walton 	if (family == 0xf)
47015aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
47025aa3d1a2SCalvin Walton 	if (family >= 6)
4703103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
470434041551SLen Brown 	ecx_flags = ecx;
470534041551SLen Brown 	edx_flags = edx;
4706103a8feaSLen Brown 
4707103a8feaSLen Brown 	/*
4708103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4709103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4710103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4711103a8feaSLen Brown 	 */
4712103a8feaSLen Brown 	ebx = ecx = edx = 0;
47135aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4714103a8feaSLen Brown 
471534041551SLen Brown 	if (!quiet) {
471634041551SLen Brown 		fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
471734041551SLen Brown 			max_level, max_extended_level, family, model, stepping, family, model, stepping);
471834041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
471934041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
472034041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
472134041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
472234041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
472334041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
472434041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
472534041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
472634041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
472734041551SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-",
472834041551SLen Brown 			edx_flags & (1 << 29) ? "TM" : "-");
472934041551SLen Brown 	}
4730f5a4c76aSLen Brown 	if (genuine_intel)
4731f5a4c76aSLen Brown 		model = intel_model_duplicates(model);
473234041551SLen Brown 
473334041551SLen Brown 	if (!(edx_flags & (1 << 5)))
473434041551SLen Brown 		errx(1, "CPUID: no MSR");
473534041551SLen Brown 
473661a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4737103a8feaSLen Brown 
4738103a8feaSLen Brown 		/*
4739103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4740103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4741103a8feaSLen Brown 		 */
47425aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
47438209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4744d7899447SLen Brown 	}
4745103a8feaSLen Brown 
4746103a8feaSLen Brown 	/*
4747103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4748103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4749103a8feaSLen Brown 	 */
4750103a8feaSLen Brown 
47515aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
47528209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4753812db3f7SLen Brown 	if (has_aperf) {
4754812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4755812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4756812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4757812db3f7SLen Brown 	}
4758889facbeSLen Brown 	do_dts = eax & (1 << 0);
4759812db3f7SLen Brown 	if (do_dts)
4760812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4761b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4762889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4763812db3f7SLen Brown 	if (do_ptm)
4764812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
47657f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
47667f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
47677f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
47687f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
47697f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4770889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4771889facbeSLen Brown 
477296e47158SLen Brown 	if (!quiet)
4773b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
47747f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
47757f5c258eSLen Brown 			has_aperf ? "" : "No-",
4776b3a34e93SLen Brown 			has_turbo ? "" : "No-",
47777f5c258eSLen Brown 			do_dts ? "" : "No-",
47787f5c258eSLen Brown 			do_ptm ? "" : "No-",
47797f5c258eSLen Brown 			has_hwp ? "" : "No-",
47807f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
47817f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
47827f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
47837f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
47847f5c258eSLen Brown 			has_epb ? "" : "No-");
4785103a8feaSLen Brown 
478696e47158SLen Brown 	if (!quiet)
478769807a63SLen Brown 		decode_misc_enable_msr();
478869807a63SLen Brown 
478933148d67SLen Brown 
479096e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4791aa8d8cc7SLen Brown 		int has_sgx;
4792aa8d8cc7SLen Brown 
4793aa8d8cc7SLen Brown 		ecx = 0;
4794aa8d8cc7SLen Brown 
4795aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4796aa8d8cc7SLen Brown 
4797aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4798aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4799aa8d8cc7SLen Brown 
4800aa8d8cc7SLen Brown 		if (has_sgx)
4801aa8d8cc7SLen Brown 			decode_feature_control_msr();
4802aa8d8cc7SLen Brown 	}
4803aa8d8cc7SLen Brown 
480461a87ba7SLen Brown 	if (max_level >= 0x15) {
48058a5bdf41SLen Brown 		unsigned int eax_crystal;
48068a5bdf41SLen Brown 		unsigned int ebx_tsc;
48078a5bdf41SLen Brown 
48088a5bdf41SLen Brown 		/*
48098a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
48108a5bdf41SLen Brown 		 */
48118a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
48125aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
48138a5bdf41SLen Brown 
48148a5bdf41SLen Brown 		if (ebx_tsc != 0) {
48158a5bdf41SLen Brown 
481696e47158SLen Brown 			if (!quiet && (ebx != 0))
4817b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
48188a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
48198a5bdf41SLen Brown 
48208a5bdf41SLen Brown 			if (crystal_hz == 0)
48218a5bdf41SLen Brown 				switch(model) {
4822af239c44SPeter Zijlstra 				case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4823e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4824e8efbc80SLen Brown 					break;
48255ebb34edSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
4826ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4827ec53e594SLen Brown 					break;
4828869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4829f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4830e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
48318a5bdf41SLen Brown 					break;
48328a5bdf41SLen Brown 				default:
48338a5bdf41SLen Brown 					crystal_hz = 0;
48348a5bdf41SLen Brown 			}
48358a5bdf41SLen Brown 
48368a5bdf41SLen Brown 			if (crystal_hz) {
48378a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
483896e47158SLen Brown 				if (!quiet)
4839b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
48408a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
48418a5bdf41SLen Brown 			}
48428a5bdf41SLen Brown 		}
48438a5bdf41SLen Brown 	}
484461a87ba7SLen Brown 	if (max_level >= 0x16) {
484561a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
484661a87ba7SLen Brown 
484761a87ba7SLen Brown 		/*
484861a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
484961a87ba7SLen Brown 		 */
485061a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
485161a87ba7SLen Brown 
48525aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
485396e47158SLen Brown 		if (!quiet)
4854b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
485561a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
485661a87ba7SLen Brown 	}
48578a5bdf41SLen Brown 
4858b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4859b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4860b2b34dfeSHubert Chrzaniuk 
4861812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4862812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4863812db3f7SLen Brown 
4864812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4865812db3f7SLen Brown 		do_nhm_platform_info = 1;
4866812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4867812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4868812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4869812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4870812db3f7SLen Brown 	}
4871d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4872812db3f7SLen Brown 
4873812db3f7SLen Brown 	if (do_snb_cstates)
4874812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4875812db3f7SLen Brown 
48765a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
48770f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
48780f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
48790f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
48800f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
48810f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
48820f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
48830f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
48840f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
48850539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
48860f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
48870f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
48880f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
48890f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
48900539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
48910539ba11SLen Brown 		use_c1_residency_msr = 1;
48920539ba11SLen Brown 	}
48937170a374SLen Brown 	if (is_dnv(family, model)) {
48947170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
48957170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
48967170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
48977170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
48987170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
48997170a374SLen Brown 		use_c1_residency_msr = 1;
49007170a374SLen Brown 	}
490134c76197SLen Brown 	if (is_skx(family, model)) {
490234c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
490334c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
490434c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
490534c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
490634c76197SLen Brown 	}
4907ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4908ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4909ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4910ade0ebacSLen Brown 	}
4911570992fcSLen Brown 	if (has_c8910_msrs(family, model)) {
49120f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
49130f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
49140f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
49150f47c08dSLen Brown 	}
4916570992fcSLen Brown 	do_irtl_hsw = has_c8910_msrs(family, model);
4917a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4918a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4919a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4920a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4921a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4922a99d8730SLen Brown 	}
4923144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4924fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4925103a8feaSLen Brown 
4926f6708400SChen Yu 	if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) ||
4927f6708400SChen Yu 	    is_ehl(family, model))
4928562855eeSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
4929103a8feaSLen Brown 
493096e47158SLen Brown 	if (!quiet)
4931f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4932f0057310SLen Brown 
493396e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
493471616c8eSLen Brown 		decode_c6_demotion_policy_msr();
493571616c8eSLen Brown 
4936889facbeSLen Brown 	rapl_probe(family, model);
49373a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4938ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4939889facbeSLen Brown 
494096e47158SLen Brown 	if (!quiet)
49411b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4942fcd17211SLen Brown 
494341618e63SLen Brown 	if (!quiet)
494441618e63SLen Brown 		dump_sysfs_cstate_config();
49457293fccdSLen Brown 	if (!quiet)
49467293fccdSLen Brown 		dump_sysfs_pstate_config();
494741618e63SLen Brown 
4948a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4949a2b7b749SLen Brown 		calculate_tsc_tweak();
4950a2b7b749SLen Brown 
4951812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4952812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4953fdf676e5SLen Brown 
4954812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4955812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
495627d47356SLen Brown 
4957be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4958be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
4959be0e54c4SLen Brown 	else
4960be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
4961be0e54c4SLen Brown 
49621f81c5efSLen Brown 	if (!access(sys_lpi_file_sysfs, R_OK)) {
49631f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_sysfs;
4964be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
49651f81c5efSLen Brown 	} else if (!access(sys_lpi_file_debugfs, R_OK)) {
49661f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_debugfs;
49671f81c5efSLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
49681f81c5efSLen Brown 	} else {
49691f81c5efSLen Brown 		sys_lpi_file_sysfs = NULL;
4970be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
49711f81c5efSLen Brown 	}
4972be0e54c4SLen Brown 
497396e47158SLen Brown 	if (!quiet)
497433148d67SLen Brown 		decode_misc_feature_control();
497533148d67SLen Brown 
4976889facbeSLen Brown 	return;
4977103a8feaSLen Brown }
4978103a8feaSLen Brown 
4979103a8feaSLen Brown /*
4980103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4981103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4982103a8feaSLen Brown  */
4983103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4984103a8feaSLen Brown {
4985103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4986103a8feaSLen Brown 		return 1;
4987103a8feaSLen Brown 	else
4988103a8feaSLen Brown 		return 0;
4989103a8feaSLen Brown }
4990103a8feaSLen Brown 
4991103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4992103a8feaSLen Brown {
4993103a8feaSLen Brown 	return 0;
4994103a8feaSLen Brown }
4995103a8feaSLen Brown 
4996c98d5d94SLen Brown void topology_probe()
4997c98d5d94SLen Brown {
4998c98d5d94SLen Brown 	int i;
4999c98d5d94SLen Brown 	int max_core_id = 0;
5000c98d5d94SLen Brown 	int max_package_id = 0;
50016de68fe1SLen Brown 	int max_die_id = 0;
5002c98d5d94SLen Brown 	int max_siblings = 0;
5003c98d5d94SLen Brown 
5004c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
5005843c5791SPrarit Bhargava 	set_max_cpu_num();
5006c98d5d94SLen Brown 	topo.num_cpus = 0;
5007c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
5008c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
5009812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
5010c98d5d94SLen Brown 
5011d8af6f5fSLen Brown 	if (debug > 1)
5012b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5013c98d5d94SLen Brown 
5014c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
5015b2c95d90SJosh Triplett 	if (cpus == NULL)
5016b2c95d90SJosh Triplett 		err(1, "calloc cpus");
5017c98d5d94SLen Brown 
5018c98d5d94SLen Brown 	/*
5019c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
5020c98d5d94SLen Brown 	 */
5021c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5022b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
5023b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5024c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5025c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5026c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
5027c98d5d94SLen Brown 
5028c98d5d94SLen Brown 	/*
50291ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
50301ef7d21aSLen Brown 	 */
50311ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
50321ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
50331ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
50341ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
50351ef7d21aSLen Brown 	}
50361ef7d21aSLen Brown 
50371ef7d21aSLen Brown 	/*
5038c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
5039c98d5d94SLen Brown 	 */
5040c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5041b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
5042b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5043c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5044c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5045c98d5d94SLen Brown 
50468cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
5047c98d5d94SLen Brown 
5048c98d5d94SLen Brown 	/*
5049c98d5d94SLen Brown 	 * For online cpus
5050c98d5d94SLen Brown 	 * find max_core_id, max_package_id
5051c98d5d94SLen Brown 	 */
5052c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
5053c98d5d94SLen Brown 		int siblings;
5054c98d5d94SLen Brown 
5055c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
5056d8af6f5fSLen Brown 			if (debug > 1)
5057b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
5058c98d5d94SLen Brown 			continue;
5059c98d5d94SLen Brown 		}
5060c98d5d94SLen Brown 
50610e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
50620e2d8f05SLen Brown 
50630e2d8f05SLen Brown 		/* get package information */
5064c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
5065c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
5066c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
5067c98d5d94SLen Brown 
50686de68fe1SLen Brown 		/* get die information */
50696de68fe1SLen Brown 		cpus[i].die_id = get_die_id(i);
50706de68fe1SLen Brown 		if (cpus[i].die_id > max_die_id)
50716de68fe1SLen Brown 			max_die_id = cpus[i].die_id;
50726de68fe1SLen Brown 
50730e2d8f05SLen Brown 		/* get numa node information */
5074ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5075ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
5076ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
50770e2d8f05SLen Brown 
50780e2d8f05SLen Brown 		/* get core information */
50790e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
50800e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
50810e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
50820e2d8f05SLen Brown 
50830e2d8f05SLen Brown 		/* get thread information */
50840e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
5085c98d5d94SLen Brown 		if (siblings > max_siblings)
5086c98d5d94SLen Brown 			max_siblings = siblings;
50874f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
50888cb48b32SPrarit Bhargava 			topo.num_cores++;
5089c98d5d94SLen Brown 	}
5090ef605741SPrarit Bhargava 
509170a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
5092d8af6f5fSLen Brown 	if (debug > 1)
5093b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
509470a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
509570a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
5096812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
5097c98d5d94SLen Brown 
50986de68fe1SLen Brown 	topo.num_die = max_die_id + 1;
50996de68fe1SLen Brown 	if (debug > 1)
51006de68fe1SLen Brown 		fprintf(outf, "max_die_id %d, sizing for %d die\n",
51016de68fe1SLen Brown 				max_die_id, topo.num_die);
51026de68fe1SLen Brown 	if (!summary_only && topo.num_die > 1)
51036de68fe1SLen Brown 		BIC_PRESENT(BIC_Die);
51046de68fe1SLen Brown 
5105c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
5106d8af6f5fSLen Brown 	if (debug > 1)
5107b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
5108c98d5d94SLen Brown 			max_package_id, topo.num_packages);
51097da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
5110812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
5111c98d5d94SLen Brown 
5112ef605741SPrarit Bhargava 	set_node_data();
5113ef605741SPrarit Bhargava 	if (debug > 1)
511470a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
511501235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
511601235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
5117ef605741SPrarit Bhargava 
511870a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
5119d8af6f5fSLen Brown 	if (debug > 1)
5120b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
51212ffbb224SPrarit Bhargava 
51222ffbb224SPrarit Bhargava 	if (debug < 1)
51232ffbb224SPrarit Bhargava 		return;
51242ffbb224SPrarit Bhargava 
51252ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
51260ec712e3SLen Brown 		if (cpu_is_not_present(i))
51270ec712e3SLen Brown 			continue;
51282ffbb224SPrarit Bhargava 		fprintf(outf,
51296de68fe1SLen Brown 			"cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
51306de68fe1SLen Brown 			i, cpus[i].physical_package_id, cpus[i].die_id,
51312ffbb224SPrarit Bhargava 			cpus[i].physical_node_id,
51322ffbb224SPrarit Bhargava 			cpus[i].logical_node_id,
51332ffbb224SPrarit Bhargava 			cpus[i].physical_core_id,
51342ffbb224SPrarit Bhargava 			cpus[i].thread_id);
51352ffbb224SPrarit Bhargava 	}
51362ffbb224SPrarit Bhargava 
5137c98d5d94SLen Brown }
5138c98d5d94SLen Brown 
5139c98d5d94SLen Brown void
514040f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
514140f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
5142c98d5d94SLen Brown {
5143c98d5d94SLen Brown 	int i;
514440f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
514540f5cfe7SPrarit Bhargava 			topo.num_packages;
514640f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
5147c98d5d94SLen Brown 
514840f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
5149c98d5d94SLen Brown 	if (*t == NULL)
5150c98d5d94SLen Brown 		goto error;
5151c98d5d94SLen Brown 
515240f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
5153c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
5154c98d5d94SLen Brown 
515540f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
5156c98d5d94SLen Brown 	if (*c == NULL)
5157c98d5d94SLen Brown 		goto error;
5158c98d5d94SLen Brown 
515940f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
5160c98d5d94SLen Brown 		(*c)[i].core_id = -1;
5161c98d5d94SLen Brown 
5162678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
5163c98d5d94SLen Brown 	if (*p == NULL)
5164c98d5d94SLen Brown 		goto error;
5165c98d5d94SLen Brown 
5166c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
5167c98d5d94SLen Brown 		(*p)[i].package_id = i;
5168c98d5d94SLen Brown 
5169c98d5d94SLen Brown 	return;
5170c98d5d94SLen Brown error:
5171b2c95d90SJosh Triplett 	err(1, "calloc counters");
5172c98d5d94SLen Brown }
5173c98d5d94SLen Brown /*
5174c98d5d94SLen Brown  * init_counter()
5175c98d5d94SLen Brown  *
5176c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5177c98d5d94SLen Brown  */
5178c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
51798cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
5180c98d5d94SLen Brown {
51818cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
518240f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
51838cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
51848cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
5185c98d5d94SLen Brown 	struct thread_data *t;
5186c98d5d94SLen Brown 	struct core_data *c;
5187c98d5d94SLen Brown 	struct pkg_data *p;
5188c98d5d94SLen Brown 
518942dd4520SNathan Ciobanu 
519042dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
519142dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
519242dd4520SNathan Ciobanu 	 */
519342dd4520SNathan Ciobanu 	if (node_id < 0)
519442dd4520SNathan Ciobanu 		node_id = 0;
519542dd4520SNathan Ciobanu 
519640f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
519740f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
51988cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
5199c98d5d94SLen Brown 
5200c98d5d94SLen Brown 	t->cpu_id = cpu_id;
52018cb48b32SPrarit Bhargava 	if (thread_id == 0) {
5202c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5203c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
5204c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5205c98d5d94SLen Brown 	}
5206c98d5d94SLen Brown 
52078cb48b32SPrarit Bhargava 	c->core_id = core_id;
52088cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
5209c98d5d94SLen Brown }
5210c98d5d94SLen Brown 
5211c98d5d94SLen Brown 
5212c98d5d94SLen Brown int initialize_counters(int cpu_id)
5213c98d5d94SLen Brown {
52148cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
52158cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
5216c98d5d94SLen Brown 	return 0;
5217c98d5d94SLen Brown }
5218c98d5d94SLen Brown 
5219c98d5d94SLen Brown void allocate_output_buffer()
5220c98d5d94SLen Brown {
5221eeb71c95SNaoya Horiguchi 	output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
5222c98d5d94SLen Brown 	outp = output_buffer;
5223b2c95d90SJosh Triplett 	if (outp == NULL)
5224b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
5225c98d5d94SLen Brown }
522636229897SLen Brown void allocate_fd_percpu(void)
522736229897SLen Brown {
522801a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
522936229897SLen Brown 	if (fd_percpu == NULL)
523036229897SLen Brown 		err(-1, "calloc fd_percpu");
523136229897SLen Brown }
5232562a2d37SLen Brown void allocate_irq_buffers(void)
5233562a2d37SLen Brown {
5234562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5235562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
5236562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
5237c98d5d94SLen Brown 
523801a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5239562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
524001a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
5241562a2d37SLen Brown }
5242c98d5d94SLen Brown void setup_all_buffers(void)
5243c98d5d94SLen Brown {
5244c98d5d94SLen Brown 	topology_probe();
5245562a2d37SLen Brown 	allocate_irq_buffers();
524636229897SLen Brown 	allocate_fd_percpu();
5247c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
5248c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
5249c98d5d94SLen Brown 	allocate_output_buffer();
5250c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
5251c98d5d94SLen Brown }
52523b4d5c7fSAndy Shevchenko 
52537ce7d5deSPrarit Bhargava void set_base_cpu(void)
52547ce7d5deSPrarit Bhargava {
52557ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
52567ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
52577ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
52587ce7d5deSPrarit Bhargava 
52597ce7d5deSPrarit Bhargava 	if (debug > 1)
5260b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
52617ce7d5deSPrarit Bhargava }
52627ce7d5deSPrarit Bhargava 
5263103a8feaSLen Brown void turbostat_init()
5264103a8feaSLen Brown {
52657ce7d5deSPrarit Bhargava 	setup_all_buffers();
52667ce7d5deSPrarit Bhargava 	set_base_cpu();
5267103a8feaSLen Brown 	check_dev_msr();
526898481e79SLen Brown 	check_permissions();
5269fcd17211SLen Brown 	process_cpuid();
5270103a8feaSLen Brown 
5271103a8feaSLen Brown 
527296e47158SLen Brown 	if (!quiet)
52737f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
52747f5c258eSLen Brown 
527596e47158SLen Brown 	if (!quiet)
5276889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5277889facbeSLen Brown 
527896e47158SLen Brown 	if (!quiet)
52793a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
52803a9a941dSLen Brown 
528196e47158SLen Brown 	if (!quiet)
5282889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5283889facbeSLen Brown 
5284889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5285889facbeSLen Brown 
528696e47158SLen Brown 	if (!quiet)
5287889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
52885a63426eSLen Brown 
528996e47158SLen Brown 	if (!quiet && do_irtl_snb)
52905a63426eSLen Brown 		print_irtl();
5291103a8feaSLen Brown }
5292103a8feaSLen Brown 
5293103a8feaSLen Brown int fork_it(char **argv)
5294103a8feaSLen Brown {
5295103a8feaSLen Brown 	pid_t child_pid;
5296d91bb17cSLen Brown 	int status;
5297d15cf7c1SLen Brown 
5298218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5299d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
53004c2122d4SLen Brown 	first_counter_read = 0;
5301d91bb17cSLen Brown 	if (status)
5302d91bb17cSLen Brown 		exit(status);
5303d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5304d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5305103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5306103a8feaSLen Brown 
5307103a8feaSLen Brown 	child_pid = fork();
5308103a8feaSLen Brown 	if (!child_pid) {
5309103a8feaSLen Brown 		/* child */
5310103a8feaSLen Brown 		execvp(argv[0], argv);
53110815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5312103a8feaSLen Brown 	} else {
5313103a8feaSLen Brown 
5314103a8feaSLen Brown 		/* parent */
5315b2c95d90SJosh Triplett 		if (child_pid == -1)
5316b2c95d90SJosh Triplett 			err(1, "fork");
5317103a8feaSLen Brown 
5318103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5319103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5320b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5321b2c95d90SJosh Triplett 			err(status, "waitpid");
53222a954966SDavid Arcari 
53232a954966SDavid Arcari 		if (WIFEXITED(status))
53242a954966SDavid Arcari 			status = WEXITSTATUS(status);
5325103a8feaSLen Brown 	}
5326c98d5d94SLen Brown 	/*
5327c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5328c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5329c98d5d94SLen Brown 	 */
5330218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5331c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5332103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5333103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5334ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5335ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5336ba3dec99SLen Brown 	else {
5337c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5338c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5339ba3dec99SLen Brown 	}
5340103a8feaSLen Brown 
5341b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5342b7d8c148SLen Brown 
5343b7d8c148SLen Brown 	flush_output_stderr();
5344103a8feaSLen Brown 
5345d91bb17cSLen Brown 	return status;
5346103a8feaSLen Brown }
5347103a8feaSLen Brown 
53483b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
53493b4d5c7fSAndy Shevchenko {
53503b4d5c7fSAndy Shevchenko 	int status;
53513b4d5c7fSAndy Shevchenko 
5352218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
53533b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
53543b4d5c7fSAndy Shevchenko 	if (status)
53553b4d5c7fSAndy Shevchenko 		return status;
53563b4d5c7fSAndy Shevchenko 
53573b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
53583b4d5c7fSAndy Shevchenko 	if (status)
53593b4d5c7fSAndy Shevchenko 		return status;
53603b4d5c7fSAndy Shevchenko 
5361b7d8c148SLen Brown 	flush_output_stdout();
53623b4d5c7fSAndy Shevchenko 
53633b4d5c7fSAndy Shevchenko 	return status;
53643b4d5c7fSAndy Shevchenko }
53653b4d5c7fSAndy Shevchenko 
5366d8af6f5fSLen Brown void print_version() {
53679eb4b518SLen Brown 	fprintf(outf, "turbostat version 19.08.31"
5368d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5369d8af6f5fSLen Brown }
5370d8af6f5fSLen Brown 
5371495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5372495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
537341618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5374388e9c81SLen Brown {
5375388e9c81SLen Brown 	struct msr_counter *msrp;
5376388e9c81SLen Brown 
5377388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5378388e9c81SLen Brown 	if (msrp == NULL) {
5379388e9c81SLen Brown 		perror("calloc");
5380388e9c81SLen Brown 		exit(1);
5381388e9c81SLen Brown 	}
5382388e9c81SLen Brown 
5383388e9c81SLen Brown 	msrp->msr_num = msr_num;
5384d8d005baSLen Brown 	strncpy(msrp->name, name, NAME_BYTES - 1);
5385495c7654SLen Brown 	if (path)
5386d8d005baSLen Brown 		strncpy(msrp->path, path, PATH_BYTES - 1);
5387388e9c81SLen Brown 	msrp->width = width;
5388388e9c81SLen Brown 	msrp->type = type;
5389388e9c81SLen Brown 	msrp->format = format;
539041618e63SLen Brown 	msrp->flags = flags;
5391388e9c81SLen Brown 
5392388e9c81SLen Brown 	switch (scope) {
5393388e9c81SLen Brown 
5394388e9c81SLen Brown 	case SCOPE_CPU:
5395388e9c81SLen Brown 		msrp->next = sys.tp;
5396388e9c81SLen Brown 		sys.tp = msrp;
5397678a3bd1SLen Brown 		sys.added_thread_counters++;
53980748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5399678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5400678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5401678a3bd1SLen Brown 			exit(-1);
5402678a3bd1SLen Brown 		}
5403388e9c81SLen Brown 		break;
5404388e9c81SLen Brown 
5405388e9c81SLen Brown 	case SCOPE_CORE:
5406388e9c81SLen Brown 		msrp->next = sys.cp;
5407388e9c81SLen Brown 		sys.cp = msrp;
5408678a3bd1SLen Brown 		sys.added_core_counters++;
5409678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5410678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5411678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5412678a3bd1SLen Brown 			exit(-1);
5413678a3bd1SLen Brown 		}
5414388e9c81SLen Brown 		break;
5415388e9c81SLen Brown 
5416388e9c81SLen Brown 	case SCOPE_PACKAGE:
5417388e9c81SLen Brown 		msrp->next = sys.pp;
5418388e9c81SLen Brown 		sys.pp = msrp;
5419678a3bd1SLen Brown 		sys.added_package_counters++;
5420678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5421678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5422678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5423678a3bd1SLen Brown 			exit(-1);
5424678a3bd1SLen Brown 		}
5425388e9c81SLen Brown 		break;
5426388e9c81SLen Brown 	}
5427388e9c81SLen Brown 
5428388e9c81SLen Brown 	return 0;
5429388e9c81SLen Brown }
5430388e9c81SLen Brown 
5431388e9c81SLen Brown void parse_add_command(char *add_command)
5432388e9c81SLen Brown {
5433388e9c81SLen Brown 	int msr_num = 0;
5434495c7654SLen Brown 	char *path = NULL;
54350f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5436388e9c81SLen Brown 	int width = 64;
5437388e9c81SLen Brown 	int fail = 0;
5438388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5439388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5440388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5441388e9c81SLen Brown 
5442388e9c81SLen Brown 	while (add_command) {
5443388e9c81SLen Brown 
5444388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5445388e9c81SLen Brown 			goto next;
5446388e9c81SLen Brown 
5447388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5448388e9c81SLen Brown 			goto next;
5449388e9c81SLen Brown 
5450495c7654SLen Brown 		if (*add_command == '/') {
5451495c7654SLen Brown 			path = add_command;
5452495c7654SLen Brown 			goto next;
5453495c7654SLen Brown 		}
5454495c7654SLen Brown 
5455388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5456388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5457388e9c81SLen Brown 				goto next;
5458388e9c81SLen Brown 			width = 64;
5459388e9c81SLen Brown 		}
5460388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5461388e9c81SLen Brown 			scope = SCOPE_CPU;
5462388e9c81SLen Brown 			goto next;
5463388e9c81SLen Brown 		}
5464388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5465388e9c81SLen Brown 			scope = SCOPE_CORE;
5466388e9c81SLen Brown 			goto next;
5467388e9c81SLen Brown 		}
5468388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5469388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5470388e9c81SLen Brown 			goto next;
5471388e9c81SLen Brown 		}
5472388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5473388e9c81SLen Brown 			type = COUNTER_CYCLES;
5474388e9c81SLen Brown 			goto next;
5475388e9c81SLen Brown 		}
5476388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5477388e9c81SLen Brown 			type = COUNTER_SECONDS;
5478388e9c81SLen Brown 			goto next;
5479388e9c81SLen Brown 		}
548041618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
548141618e63SLen Brown 			type = COUNTER_USEC;
548241618e63SLen Brown 			goto next;
548341618e63SLen Brown 		}
5484388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5485388e9c81SLen Brown 			format = FORMAT_RAW;
5486388e9c81SLen Brown 			goto next;
5487388e9c81SLen Brown 		}
5488388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5489388e9c81SLen Brown 			format = FORMAT_DELTA;
5490388e9c81SLen Brown 			goto next;
5491388e9c81SLen Brown 		}
5492388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5493388e9c81SLen Brown 			format = FORMAT_PERCENT;
5494388e9c81SLen Brown 			goto next;
5495388e9c81SLen Brown 		}
5496388e9c81SLen Brown 
5497388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5498388e9c81SLen Brown 			char *eos;
5499388e9c81SLen Brown 
5500388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5501388e9c81SLen Brown 			if (eos)
5502388e9c81SLen Brown 				*eos = '\0';
5503388e9c81SLen Brown 			goto next;
5504388e9c81SLen Brown 		}
5505388e9c81SLen Brown 
5506388e9c81SLen Brown next:
5507388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5508495c7654SLen Brown 		if (add_command) {
5509495c7654SLen Brown 			*add_command = '\0';
5510388e9c81SLen Brown 			add_command++;
5511495c7654SLen Brown 		}
5512388e9c81SLen Brown 
5513388e9c81SLen Brown 	}
5514495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5515495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5516388e9c81SLen Brown 		fail++;
5517388e9c81SLen Brown 	}
5518388e9c81SLen Brown 
5519388e9c81SLen Brown 	/* generate default column header */
5520388e9c81SLen Brown 	if (*name_buffer == '\0') {
5521388e9c81SLen Brown 		if (width == 32)
55225f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5523388e9c81SLen Brown 		else
55245f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5525388e9c81SLen Brown 	}
5526388e9c81SLen Brown 
552741618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5528388e9c81SLen Brown 		fail++;
5529388e9c81SLen Brown 
5530388e9c81SLen Brown 	if (fail) {
5531388e9c81SLen Brown 		help();
5532388e9c81SLen Brown 		exit(1);
5533388e9c81SLen Brown 	}
5534388e9c81SLen Brown }
553541618e63SLen Brown 
5536dd778a5eSLen Brown int is_deferred_skip(char *name)
5537dd778a5eSLen Brown {
5538dd778a5eSLen Brown 	int i;
5539dd778a5eSLen Brown 
5540dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5541dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5542dd778a5eSLen Brown 			return 1;
5543dd778a5eSLen Brown 	return 0;
5544dd778a5eSLen Brown }
5545dd778a5eSLen Brown 
554641618e63SLen Brown void probe_sysfs(void)
554741618e63SLen Brown {
554841618e63SLen Brown 	char path[64];
554941618e63SLen Brown 	char name_buf[16];
555041618e63SLen Brown 	FILE *input;
555141618e63SLen Brown 	int state;
555241618e63SLen Brown 	char *sp;
555341618e63SLen Brown 
555441618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
555541618e63SLen Brown 		return;
555641618e63SLen Brown 
55570748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
555841618e63SLen Brown 
555941618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
556041618e63SLen Brown 			base_cpu, state);
556141618e63SLen Brown 		input = fopen(path, "r");
556241618e63SLen Brown 		if (input == NULL)
556341618e63SLen Brown 			continue;
55648173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
55658173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
556641618e63SLen Brown 
556741618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
556841618e63SLen Brown 		sp = strchr(name_buf, '-');
556941618e63SLen Brown 		if (!sp)
557041618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
557141618e63SLen Brown 		*sp = '%';
557241618e63SLen Brown 		*(sp + 1) = '\0';
557341618e63SLen Brown 
557441618e63SLen Brown 		fclose(input);
557541618e63SLen Brown 
557641618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
557741618e63SLen Brown 
5578dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5579dd778a5eSLen Brown 			continue;
5580dd778a5eSLen Brown 
558141618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
558241618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
558341618e63SLen Brown 	}
558441618e63SLen Brown 
55850748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
558641618e63SLen Brown 
558741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
558841618e63SLen Brown 			base_cpu, state);
558941618e63SLen Brown 		input = fopen(path, "r");
559041618e63SLen Brown 		if (input == NULL)
559141618e63SLen Brown 			continue;
55928173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
55938173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
559441618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
559541618e63SLen Brown 		sp = strchr(name_buf, '-');
559641618e63SLen Brown 		if (!sp)
559741618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
559841618e63SLen Brown 		*sp = '\0';
559941618e63SLen Brown 		fclose(input);
560041618e63SLen Brown 
560141618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
560241618e63SLen Brown 
5603dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5604dd778a5eSLen Brown 			continue;
5605dd778a5eSLen Brown 
560641618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
560741618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
560841618e63SLen Brown 	}
560941618e63SLen Brown 
561041618e63SLen Brown }
561141618e63SLen Brown 
56121ef7d21aSLen Brown 
56131ef7d21aSLen Brown /*
56141ef7d21aSLen Brown  * parse cpuset with following syntax
56151ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
56161ef7d21aSLen Brown  */
56171ef7d21aSLen Brown void parse_cpu_command(char *optarg)
56181ef7d21aSLen Brown {
56191ef7d21aSLen Brown 	unsigned int start, end;
56201ef7d21aSLen Brown 	char *next;
56211ef7d21aSLen Brown 
56224e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
56234e4e1e7cSLen Brown 		if (cpu_subset)
56244e4e1e7cSLen Brown 			goto error;
56254e4e1e7cSLen Brown 		show_core_only++;
56264e4e1e7cSLen Brown 		return;
56274e4e1e7cSLen Brown 	}
56284e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
56294e4e1e7cSLen Brown 		if (cpu_subset)
56304e4e1e7cSLen Brown 			goto error;
56314e4e1e7cSLen Brown 		show_pkg_only++;
56324e4e1e7cSLen Brown 		return;
56334e4e1e7cSLen Brown 	}
56344e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
56354e4e1e7cSLen Brown 		goto error;
56364e4e1e7cSLen Brown 
56371ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
56381ef7d21aSLen Brown 	if (cpu_subset == NULL)
56391ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
56401ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
56411ef7d21aSLen Brown 
56421ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
56431ef7d21aSLen Brown 
56441ef7d21aSLen Brown 	next = optarg;
56451ef7d21aSLen Brown 
56461ef7d21aSLen Brown 	while (next && *next) {
56471ef7d21aSLen Brown 
56481ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
56491ef7d21aSLen Brown 			goto error;
56501ef7d21aSLen Brown 
56511ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
56521ef7d21aSLen Brown 
56531ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
56541ef7d21aSLen Brown 			goto error;
56551ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
56561ef7d21aSLen Brown 
56571ef7d21aSLen Brown 		if (*next == '\0')
56581ef7d21aSLen Brown 			break;
56591ef7d21aSLen Brown 
56601ef7d21aSLen Brown 		if (*next == ',') {
56611ef7d21aSLen Brown 			next += 1;
56621ef7d21aSLen Brown 			continue;
56631ef7d21aSLen Brown 		}
56641ef7d21aSLen Brown 
56651ef7d21aSLen Brown 		if (*next == '-') {
56661ef7d21aSLen Brown 			next += 1;	/* start range */
56671ef7d21aSLen Brown 		} else if (*next == '.') {
56681ef7d21aSLen Brown 			next += 1;
56691ef7d21aSLen Brown 			if (*next == '.')
56701ef7d21aSLen Brown 				next += 1;	/* start range */
56711ef7d21aSLen Brown 			else
56721ef7d21aSLen Brown 				goto error;
56731ef7d21aSLen Brown 		}
56741ef7d21aSLen Brown 
56751ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
56761ef7d21aSLen Brown 		if (end <= start)
56771ef7d21aSLen Brown 			goto error;
56781ef7d21aSLen Brown 
56791ef7d21aSLen Brown 		while (++start <= end) {
56801ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
56811ef7d21aSLen Brown 				goto error;
56821ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
56831ef7d21aSLen Brown 		}
56841ef7d21aSLen Brown 
56851ef7d21aSLen Brown 		if (*next == ',')
56861ef7d21aSLen Brown 			next += 1;
56871ef7d21aSLen Brown 		else if (*next != '\0')
56881ef7d21aSLen Brown 			goto error;
56891ef7d21aSLen Brown 	}
56901ef7d21aSLen Brown 
56911ef7d21aSLen Brown 	return;
56921ef7d21aSLen Brown 
56931ef7d21aSLen Brown error:
56944e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
56954e4e1e7cSLen Brown 	help();
56961ef7d21aSLen Brown 	exit(-1);
56971ef7d21aSLen Brown }
56981ef7d21aSLen Brown 
5699812db3f7SLen Brown 
5700103a8feaSLen Brown void cmdline(int argc, char **argv)
5701103a8feaSLen Brown {
5702103a8feaSLen Brown 	int opt;
5703d8af6f5fSLen Brown 	int option_index = 0;
5704d8af6f5fSLen Brown 	static struct option long_options[] = {
5705388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
57061ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5707d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
570896e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
57093f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5710d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5711023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5712d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5713812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5714d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5715c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5716b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
571796e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5718812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5719d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5720d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5721d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5722d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5723d8af6f5fSLen Brown 	};
5724103a8feaSLen Brown 
5725103a8feaSLen Brown 	progname = argv[0];
5726103a8feaSLen Brown 
5727023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5728d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5729103a8feaSLen Brown 		switch (opt) {
5730388e9c81SLen Brown 		case 'a':
5731388e9c81SLen Brown 			parse_add_command(optarg);
5732388e9c81SLen Brown 			break;
57331ef7d21aSLen Brown 		case 'c':
57341ef7d21aSLen Brown 			parse_cpu_command(optarg);
57351ef7d21aSLen Brown 			break;
5736d8af6f5fSLen Brown 		case 'D':
5737d8af6f5fSLen Brown 			dump_only++;
57388e180f3cSLen Brown 			break;
57393f44a5c6SLen Brown 		case 'e':
57403f44a5c6SLen Brown 			/* --enable specified counter */
57414c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
57423f44a5c6SLen Brown 			break;
5743d8af6f5fSLen Brown 		case 'd':
5744d8af6f5fSLen Brown 			debug++;
57453f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
57462f32edf1SLen Brown 			break;
5747812db3f7SLen Brown 		case 'H':
57483f44a5c6SLen Brown 			/*
57493f44a5c6SLen Brown 			 * --hide: do not show those specified
57503f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
57513f44a5c6SLen Brown 			 */
57523f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5753812db3f7SLen Brown 			break;
5754d8af6f5fSLen Brown 		case 'h':
5755d8af6f5fSLen Brown 		default:
5756d8af6f5fSLen Brown 			help();
5757d8af6f5fSLen Brown 			exit(1);
5758d8af6f5fSLen Brown 		case 'i':
57592a0609c0SLen Brown 			{
57602a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
57612a0609c0SLen Brown 
57622a0609c0SLen Brown 				if (interval < 0.001) {
5763b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
57642a0609c0SLen Brown 						interval);
57652a0609c0SLen Brown 					exit(2);
57662a0609c0SLen Brown 				}
57672a0609c0SLen Brown 
576847936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5769b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
577047936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
57712a0609c0SLen Brown 			}
5772889facbeSLen Brown 			break;
57735c56be9aSDirk Brandewie 		case 'J':
57745c56be9aSDirk Brandewie 			rapl_joules++;
57755c56be9aSDirk Brandewie 			break;
5776c8ade361SLen Brown 		case 'l':
57773f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5778c8ade361SLen Brown 			list_header_only++;
5779c8ade361SLen Brown 			quiet++;
5780c8ade361SLen Brown 			break;
5781b7d8c148SLen Brown 		case 'o':
5782b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5783b7d8c148SLen Brown 			break;
578496e47158SLen Brown 		case 'q':
578596e47158SLen Brown 			quiet = 1;
578696e47158SLen Brown 			break;
5787023fe0acSChen Yu 		case 'n':
5788023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5789023fe0acSChen Yu 
5790023fe0acSChen Yu 			if (num_iterations <= 0) {
5791023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5792023fe0acSChen Yu 					num_iterations);
5793023fe0acSChen Yu 				exit(2);
5794023fe0acSChen Yu 			}
5795023fe0acSChen Yu 			break;
5796812db3f7SLen Brown 		case 's':
57973f44a5c6SLen Brown 			/*
57983f44a5c6SLen Brown 			 * --show: show only those specified
57993f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
58003f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
58013f44a5c6SLen Brown 			 */
58023f44a5c6SLen Brown 			if (shown == 0)
58033f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
58043f44a5c6SLen Brown 			else
58053f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
58063f44a5c6SLen Brown 			shown = 1;
5807812db3f7SLen Brown 			break;
5808d8af6f5fSLen Brown 		case 'S':
5809d8af6f5fSLen Brown 			summary_only++;
5810d8af6f5fSLen Brown 			break;
5811d8af6f5fSLen Brown 		case 'T':
5812d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5813d8af6f5fSLen Brown 			break;
5814d8af6f5fSLen Brown 		case 'v':
5815d8af6f5fSLen Brown 			print_version();
5816d8af6f5fSLen Brown 			exit(0);
5817d8af6f5fSLen Brown 			break;
5818103a8feaSLen Brown 		}
5819103a8feaSLen Brown 	}
5820103a8feaSLen Brown }
5821103a8feaSLen Brown 
5822103a8feaSLen Brown int main(int argc, char **argv)
5823103a8feaSLen Brown {
5824b7d8c148SLen Brown 	outf = stderr;
5825103a8feaSLen Brown 	cmdline(argc, argv);
5826103a8feaSLen Brown 
582796e47158SLen Brown 	if (!quiet)
5828d8af6f5fSLen Brown 		print_version();
5829103a8feaSLen Brown 
583041618e63SLen Brown 	probe_sysfs();
583141618e63SLen Brown 
5832103a8feaSLen Brown 	turbostat_init();
5833103a8feaSLen Brown 
58343b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
58353b4d5c7fSAndy Shevchenko 	if (dump_only)
58363b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
58373b4d5c7fSAndy Shevchenko 
5838c8ade361SLen Brown 	/* list header and exit */
5839c8ade361SLen Brown 	if (list_header_only) {
5840c8ade361SLen Brown 		print_header(",");
5841c8ade361SLen Brown 		flush_output_stdout();
5842c8ade361SLen Brown 		return 0;
5843c8ade361SLen Brown 	}
5844c8ade361SLen Brown 
5845103a8feaSLen Brown 	/*
5846103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5847103a8feaSLen Brown 	 */
5848103a8feaSLen Brown 	if (argc - optind)
5849103a8feaSLen Brown 		return fork_it(argv + optind);
5850103a8feaSLen Brown 	else
5851103a8feaSLen Brown 		turbostat_loop();
5852103a8feaSLen Brown 
5853103a8feaSLen Brown 	return 0;
5854103a8feaSLen Brown }
5855