xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 0ec712e36c1d5bafe6e65e53a19f136b778866cd)
1103a8feaSLen Brown /*
2103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
334041551SLen Brown  * on modern Intel and AMD processors.
4103a8feaSLen Brown  *
5144b44b1SLen Brown  * Copyright (c) 2013 Intel Corporation.
6103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
7103a8feaSLen Brown  *
8103a8feaSLen Brown  * This program is free software; you can redistribute it and/or modify it
9103a8feaSLen Brown  * under the terms and conditions of the GNU General Public License,
10103a8feaSLen Brown  * version 2, as published by the Free Software Foundation.
11103a8feaSLen Brown  *
12103a8feaSLen Brown  * This program is distributed in the hope it will be useful, but WITHOUT
13103a8feaSLen Brown  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14103a8feaSLen Brown  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15103a8feaSLen Brown  * more details.
16103a8feaSLen Brown  *
17103a8feaSLen Brown  * You should have received a copy of the GNU General Public License along with
18103a8feaSLen Brown  * this program; if not, write to the Free Software Foundation, Inc.,
19103a8feaSLen Brown  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20103a8feaSLen Brown  */
21103a8feaSLen Brown 
2288c3281fSLen Brown #define _GNU_SOURCE
23b731f311SJosh Triplett #include MSRHEADER
24869ce69eSLen Brown #include INTEL_FAMILY_HEADER
2595aebc44SJosh Triplett #include <stdarg.h>
26103a8feaSLen Brown #include <stdio.h>
27b2c95d90SJosh Triplett #include <err.h>
28103a8feaSLen Brown #include <unistd.h>
29103a8feaSLen Brown #include <sys/types.h>
30103a8feaSLen Brown #include <sys/wait.h>
31103a8feaSLen Brown #include <sys/stat.h>
32b9ad8ee0SLen Brown #include <sys/select.h>
33103a8feaSLen Brown #include <sys/resource.h>
34103a8feaSLen Brown #include <fcntl.h>
35103a8feaSLen Brown #include <signal.h>
36103a8feaSLen Brown #include <sys/time.h>
37103a8feaSLen Brown #include <stdlib.h>
38d8af6f5fSLen Brown #include <getopt.h>
39103a8feaSLen Brown #include <dirent.h>
40103a8feaSLen Brown #include <string.h>
41103a8feaSLen Brown #include <ctype.h>
4288c3281fSLen Brown #include <sched.h>
432a0609c0SLen Brown #include <time.h>
442b92865eSJosh Triplett #include <cpuid.h>
4598481e79SLen Brown #include <linux/capability.h>
4698481e79SLen Brown #include <errno.h>
47103a8feaSLen Brown 
48103a8feaSLen Brown char *proc_stat = "/proc/stat";
49b7d8c148SLen Brown FILE *outf;
5036229897SLen Brown int *fd_percpu;
51b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0};
5247936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0};
53b9ad8ee0SLen Brown struct timespec one_msec = {0, 1000000};
54023fe0acSChen Yu unsigned int num_iterations;
55d8af6f5fSLen Brown unsigned int debug;
5696e47158SLen Brown unsigned int quiet;
573f44a5c6SLen Brown unsigned int shown;
580de6c0dfSLen Brown unsigned int sums_need_wide_columns;
59d8af6f5fSLen Brown unsigned int rapl_joules;
60d8af6f5fSLen Brown unsigned int summary_only;
61c8ade361SLen Brown unsigned int list_header_only;
62d8af6f5fSLen Brown unsigned int dump_only;
63103a8feaSLen Brown unsigned int do_snb_cstates;
64fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
65144b44b1SLen Brown unsigned int do_slm_cstates;
66997e5395SSrinivas Pandruvada unsigned int do_cnl_cstates;
67144b44b1SLen Brown unsigned int use_c1_residency_msr;
68103a8feaSLen Brown unsigned int has_aperf;
69889facbeSLen Brown unsigned int has_epb;
705a63426eSLen Brown unsigned int do_irtl_snb;
715a63426eSLen Brown unsigned int do_irtl_hsw;
72fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
73103a8feaSLen Brown unsigned int genuine_intel;
7434041551SLen Brown unsigned int authentic_amd;
7534041551SLen Brown unsigned int max_level, max_extended_level;
76103a8feaSLen Brown unsigned int has_invariant_tsc;
77d7899447SLen Brown unsigned int do_nhm_platform_info;
78cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
79b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
80103a8feaSLen Brown double bclk;
81a2b7b749SLen Brown double base_hz;
8221ed5574SLen Brown unsigned int has_base_hz;
83a2b7b749SLen Brown double tsc_tweak = 1.0;
84c98d5d94SLen Brown unsigned int show_pkg_only;
85c98d5d94SLen Brown unsigned int show_core_only;
86c98d5d94SLen Brown char *output_buffer, *outp;
87889facbeSLen Brown unsigned int do_rapl;
88889facbeSLen Brown unsigned int do_dts;
89889facbeSLen Brown unsigned int do_ptm;
90fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
91be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
92be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
9327d47356SLen Brown unsigned int gfx_cur_mhz;
94889facbeSLen Brown unsigned int tcc_activation_temp;
95889facbeSLen Brown unsigned int tcc_activation_temp_override;
9640ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
9740ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
98889facbeSLen Brown double rapl_joule_counter_range;
993a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
100ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
1013a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
1023a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
1038a5bdf41SLen Brown unsigned int crystal_hz;
1048a5bdf41SLen Brown unsigned long long tsc_hz;
1057ce7d5deSPrarit Bhargava int base_cpu;
10621ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
1077f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1087f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1097f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1107f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1117f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1127f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
11333148d67SLen Brown unsigned int has_misc_feature_control;
1144c2122d4SLen Brown unsigned int first_counter_read = 1;
115889facbeSLen Brown 
116889facbeSLen Brown #define RAPL_PKG		(1 << 0)
117e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
118e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
119e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
120e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
121e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
122e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
123e6f9bb3cSLen Brown 
124889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
125e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
126e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
127e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
128e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1290b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1300b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
131e6f9bb3cSLen Brown 
1329148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
133e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1340b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
135e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
136e6f9bb3cSLen Brown 
1370b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
138e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
139e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
140e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1419148494cSJacob Pan 
1429148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1439148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1449148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
145889facbeSLen Brown #define	TJMAX_DEFAULT	100
146889facbeSLen Brown 
147889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
148103a8feaSLen Brown 
149388e9c81SLen Brown /*
150388e9c81SLen Brown  * buffer size used by sscanf() for added column names
151388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
152388e9c81SLen Brown  */
153388e9c81SLen Brown #define	NAME_BYTES 20
154495c7654SLen Brown #define PATH_BYTES 128
155388e9c81SLen Brown 
156103a8feaSLen Brown int backwards_count;
157103a8feaSLen Brown char *progname;
158103a8feaSLen Brown 
1591ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
1601ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
1611ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
1620748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
1630748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
1640e2d8f05SLen Brown #define BITMASK_SIZE 32
165c98d5d94SLen Brown 
166c98d5d94SLen Brown struct thread_data {
167f4fdf2b4SLen Brown 	struct timeval tv_begin;
168f4fdf2b4SLen Brown 	struct timeval tv_end;
169c98d5d94SLen Brown 	unsigned long long tsc;
170c98d5d94SLen Brown 	unsigned long long aperf;
171c98d5d94SLen Brown 	unsigned long long mperf;
172144b44b1SLen Brown 	unsigned long long c1;
1730de6c0dfSLen Brown 	unsigned long long  irq_count;
1741ed51011SLen Brown 	unsigned int smi_count;
175c98d5d94SLen Brown 	unsigned int cpu_id;
1764c2122d4SLen Brown 	unsigned int apic_id;
1774c2122d4SLen Brown 	unsigned int x2apic_id;
178c98d5d94SLen Brown 	unsigned int flags;
179c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
180c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
1810748eaf0SLen Brown 	unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
182c98d5d94SLen Brown } *thread_even, *thread_odd;
183c98d5d94SLen Brown 
184c98d5d94SLen Brown struct core_data {
185c98d5d94SLen Brown 	unsigned long long c3;
186c98d5d94SLen Brown 	unsigned long long c6;
187c98d5d94SLen Brown 	unsigned long long c7;
1880539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
189889facbeSLen Brown 	unsigned int core_temp_c;
190c98d5d94SLen Brown 	unsigned int core_id;
191678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
192c98d5d94SLen Brown } *core_even, *core_odd;
193c98d5d94SLen Brown 
194c98d5d94SLen Brown struct pkg_data {
195c98d5d94SLen Brown 	unsigned long long pc2;
196c98d5d94SLen Brown 	unsigned long long pc3;
197c98d5d94SLen Brown 	unsigned long long pc6;
198c98d5d94SLen Brown 	unsigned long long pc7;
199ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
200ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
201ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
202be0e54c4SLen Brown 	unsigned long long cpu_lpi;
203be0e54c4SLen Brown 	unsigned long long sys_lpi;
2040b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
2050b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
2060b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
2070b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
2089185e988SLen Brown 	long long gfx_rc6_ms;
20927d47356SLen Brown 	unsigned int gfx_mhz;
210c98d5d94SLen Brown 	unsigned int package_id;
211889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
212889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
213889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
214889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
215889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
216889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
217889facbeSLen Brown 	unsigned int pkg_temp_c;
218678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
219c98d5d94SLen Brown } *package_even, *package_odd;
220c98d5d94SLen Brown 
221c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
222c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
223c98d5d94SLen Brown 
22440f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
22540f5cfe7SPrarit Bhargava 	((thread_base) +						      \
22640f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
22740f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
22840f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
22940f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
23040f5cfe7SPrarit Bhargava 	 (thread_no))
23140f5cfe7SPrarit Bhargava 
23240f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
23340f5cfe7SPrarit Bhargava 	((core_base) +							\
23440f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
23540f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
23640f5cfe7SPrarit Bhargava 	 (core_no))
23740f5cfe7SPrarit Bhargava 
23840f5cfe7SPrarit Bhargava 
239c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
240c98d5d94SLen Brown 
241388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
24241618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
243388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
244388e9c81SLen Brown 
245388e9c81SLen Brown struct msr_counter {
246388e9c81SLen Brown 	unsigned int msr_num;
247388e9c81SLen Brown 	char name[NAME_BYTES];
248495c7654SLen Brown 	char path[PATH_BYTES];
249388e9c81SLen Brown 	unsigned int width;
250388e9c81SLen Brown 	enum counter_type type;
251388e9c81SLen Brown 	enum counter_format format;
252388e9c81SLen Brown 	struct msr_counter *next;
253812db3f7SLen Brown 	unsigned int flags;
254812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
255812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
25641618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
257388e9c81SLen Brown };
258388e9c81SLen Brown 
259388e9c81SLen Brown struct sys_counters {
260678a3bd1SLen Brown 	unsigned int added_thread_counters;
261678a3bd1SLen Brown 	unsigned int added_core_counters;
262678a3bd1SLen Brown 	unsigned int added_package_counters;
263388e9c81SLen Brown 	struct msr_counter *tp;
264388e9c81SLen Brown 	struct msr_counter *cp;
265388e9c81SLen Brown 	struct msr_counter *pp;
266388e9c81SLen Brown } sys;
267388e9c81SLen Brown 
268c98d5d94SLen Brown struct system_summary {
269c98d5d94SLen Brown 	struct thread_data threads;
270c98d5d94SLen Brown 	struct core_data cores;
271c98d5d94SLen Brown 	struct pkg_data packages;
272388e9c81SLen Brown } average;
273c98d5d94SLen Brown 
2740e2d8f05SLen Brown struct cpu_topology {
2750e2d8f05SLen Brown 	int physical_package_id;
2760e2d8f05SLen Brown 	int logical_cpu_id;
277ef605741SPrarit Bhargava 	int physical_node_id;
278ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
2790e2d8f05SLen Brown 	int physical_core_id;
2808cb48b32SPrarit Bhargava 	int thread_id;
2810e2d8f05SLen Brown 	cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
2820e2d8f05SLen Brown } *cpus;
283c98d5d94SLen Brown 
284c98d5d94SLen Brown struct topo_params {
285c98d5d94SLen Brown 	int num_packages;
286103a8feaSLen Brown 	int num_cpus;
287c98d5d94SLen Brown 	int num_cores;
288c98d5d94SLen Brown 	int max_cpu_num;
289ef605741SPrarit Bhargava 	int max_node_num;
29070a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
29170a9c6e8SPrarit Bhargava 	int cores_per_node;
29270a9c6e8SPrarit Bhargava 	int threads_per_core;
293c98d5d94SLen Brown } topo;
294103a8feaSLen Brown 
295c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
296103a8feaSLen Brown 
297562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
298562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
299562a2d37SLen Brown 
300c98d5d94SLen Brown void setup_all_buffers(void);
301103a8feaSLen Brown 
302c98d5d94SLen Brown int cpu_is_not_present(int cpu)
303d15cf7c1SLen Brown {
304c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
305c98d5d94SLen Brown }
306c98d5d94SLen Brown /*
307c98d5d94SLen Brown  * run func(thread, core, package) in topology order
308c98d5d94SLen Brown  * skip non-present cpus
309c98d5d94SLen Brown  */
310c98d5d94SLen Brown 
311c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
312c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
313c98d5d94SLen Brown {
31440f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
315c98d5d94SLen Brown 
316c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
31770a9c6e8SPrarit Bhargava 		for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
31840f5cfe7SPrarit Bhargava 			for (node_no = 0; node_no < topo.nodes_per_pkg;
31940f5cfe7SPrarit Bhargava 			     node_no++) {
320c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
32170a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
322c98d5d94SLen Brown 					struct thread_data *t;
323c98d5d94SLen Brown 					struct core_data *c;
324c98d5d94SLen Brown 					struct pkg_data *p;
325c98d5d94SLen Brown 
32640f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
32740f5cfe7SPrarit Bhargava 						       core_no, node_no,
32840f5cfe7SPrarit Bhargava 						       pkg_no);
329c98d5d94SLen Brown 
330c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
331c98d5d94SLen Brown 						continue;
332c98d5d94SLen Brown 
33340f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
33440f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
335c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
336c98d5d94SLen Brown 
337c98d5d94SLen Brown 					retval = func(t, c, p);
338c98d5d94SLen Brown 					if (retval)
339c98d5d94SLen Brown 						return retval;
340c98d5d94SLen Brown 				}
341c98d5d94SLen Brown 			}
342c98d5d94SLen Brown 		}
34340f5cfe7SPrarit Bhargava 	}
344d15cf7c1SLen Brown 	return 0;
345d15cf7c1SLen Brown }
346d15cf7c1SLen Brown 
34788c3281fSLen Brown int cpu_migrate(int cpu)
34888c3281fSLen Brown {
349c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
350c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
351c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
35288c3281fSLen Brown 		return -1;
35388c3281fSLen Brown 	else
35488c3281fSLen Brown 		return 0;
35588c3281fSLen Brown }
35636229897SLen Brown int get_msr_fd(int cpu)
357103a8feaSLen Brown {
358103a8feaSLen Brown 	char pathname[32];
359103a8feaSLen Brown 	int fd;
360103a8feaSLen Brown 
36136229897SLen Brown 	fd = fd_percpu[cpu];
36236229897SLen Brown 
36336229897SLen Brown 	if (fd)
36436229897SLen Brown 		return fd;
36536229897SLen Brown 
366103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
367103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
36815aaa346SLen Brown 	if (fd < 0)
36998481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
370103a8feaSLen Brown 
37136229897SLen Brown 	fd_percpu[cpu] = fd;
37236229897SLen Brown 
37336229897SLen Brown 	return fd;
37436229897SLen Brown }
37536229897SLen Brown 
37636229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
37736229897SLen Brown {
37836229897SLen Brown 	ssize_t retval;
37936229897SLen Brown 
38036229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
38115aaa346SLen Brown 
38298481e79SLen Brown 	if (retval != sizeof *msr)
383cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
38415aaa346SLen Brown 
38515aaa346SLen Brown 	return 0;
386103a8feaSLen Brown }
387103a8feaSLen Brown 
388fc04cc67SLen Brown /*
389bdd5ae3aSLen Brown  * This list matches the column headers, except
390bdd5ae3aSLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
391bdd5ae3aSLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
392bdd5ae3aSLen Brown  *    matching on them for --show and --hide.
393fc04cc67SLen Brown  */
394812db3f7SLen Brown struct msr_counter bic[] = {
3953f44a5c6SLen Brown 	{ 0x0, "usec" },
3963f44a5c6SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
397812db3f7SLen Brown 	{ 0x0, "Package" },
398bdd5ae3aSLen Brown 	{ 0x0, "Node" },
399812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
400bdd5ae3aSLen Brown 	{ 0x0, "Busy%" },
401812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
402812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
403812db3f7SLen Brown 	{ 0x0, "IRQ" },
404495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
405bdd5ae3aSLen Brown 	{ 0x0, "sysfs" },
406812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
407812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
408812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
409812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
410812db3f7SLen Brown 	{ 0x0, "ThreadC" },
411812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
412812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
413812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
414812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
415812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
416812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
417812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
418812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
419812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
4200f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
4210f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
4224bd1f8f2SLen Brown 	{ 0x0, "Pk%pc10" },
423be0e54c4SLen Brown 	{ 0x0, "CPU%LPI" },
424be0e54c4SLen Brown 	{ 0x0, "SYS%LPI" },
425812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
426812db3f7SLen Brown 	{ 0x0, "CorWatt" },
427812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
428812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
429812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
430812db3f7SLen Brown 	{ 0x0, "PKG_%" },
431812db3f7SLen Brown 	{ 0x0, "RAM_%" },
432812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
433812db3f7SLen Brown 	{ 0x0, "Cor_J" },
434812db3f7SLen Brown 	{ 0x0, "GFX_J" },
435812db3f7SLen Brown 	{ 0x0, "RAM_J" },
4360539ba11SLen Brown 	{ 0x0, "Mod%c6" },
437a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
438a99d8730SLen Brown 	{ 0x0, "Any%C0" },
439a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
440a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
441bdd5ae3aSLen Brown 	{ 0x0, "Core" },
442bdd5ae3aSLen Brown 	{ 0x0, "CPU" },
4434c2122d4SLen Brown 	{ 0x0, "APIC" },
4444c2122d4SLen Brown 	{ 0x0, "X2APIC" },
445812db3f7SLen Brown };
446812db3f7SLen Brown 
447812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
4483f44a5c6SLen Brown #define	BIC_USEC	(1ULL << 0)
4493f44a5c6SLen Brown #define	BIC_TOD		(1ULL << 1)
4503f44a5c6SLen Brown #define	BIC_Package	(1ULL << 2)
451bdd5ae3aSLen Brown #define	BIC_Node	(1ULL << 3)
452bdd5ae3aSLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
453bdd5ae3aSLen Brown #define	BIC_Busy	(1ULL << 5)
454bdd5ae3aSLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
455bdd5ae3aSLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
456bdd5ae3aSLen Brown #define	BIC_IRQ		(1ULL << 8)
457bdd5ae3aSLen Brown #define	BIC_SMI		(1ULL << 9)
458bdd5ae3aSLen Brown #define	BIC_sysfs	(1ULL << 10)
459bdd5ae3aSLen Brown #define	BIC_CPU_c1	(1ULL << 11)
460bdd5ae3aSLen Brown #define	BIC_CPU_c3	(1ULL << 12)
461bdd5ae3aSLen Brown #define	BIC_CPU_c6	(1ULL << 13)
462bdd5ae3aSLen Brown #define	BIC_CPU_c7	(1ULL << 14)
463bdd5ae3aSLen Brown #define	BIC_ThreadC	(1ULL << 15)
464bdd5ae3aSLen Brown #define	BIC_CoreTmp	(1ULL << 16)
465bdd5ae3aSLen Brown #define	BIC_CoreCnt	(1ULL << 17)
466bdd5ae3aSLen Brown #define	BIC_PkgTmp	(1ULL << 18)
467bdd5ae3aSLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
468bdd5ae3aSLen Brown #define	BIC_GFXMHz	(1ULL << 20)
469bdd5ae3aSLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
470bdd5ae3aSLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
471bdd5ae3aSLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
472bdd5ae3aSLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
473bdd5ae3aSLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
474bdd5ae3aSLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
475bdd5ae3aSLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
476bdd5ae3aSLen Brown #define BIC_CPU_LPI	(1ULL << 28)
477bdd5ae3aSLen Brown #define BIC_SYS_LPI	(1ULL << 29)
478bdd5ae3aSLen Brown #define	BIC_PkgWatt	(1ULL << 30)
479bdd5ae3aSLen Brown #define	BIC_CorWatt	(1ULL << 31)
480bdd5ae3aSLen Brown #define	BIC_GFXWatt	(1ULL << 32)
481bdd5ae3aSLen Brown #define	BIC_PkgCnt	(1ULL << 33)
482bdd5ae3aSLen Brown #define	BIC_RAMWatt	(1ULL << 34)
483bdd5ae3aSLen Brown #define	BIC_PKG__	(1ULL << 35)
484bdd5ae3aSLen Brown #define	BIC_RAM__	(1ULL << 36)
485bdd5ae3aSLen Brown #define	BIC_Pkg_J	(1ULL << 37)
486bdd5ae3aSLen Brown #define	BIC_Cor_J	(1ULL << 38)
487bdd5ae3aSLen Brown #define	BIC_GFX_J	(1ULL << 39)
488bdd5ae3aSLen Brown #define	BIC_RAM_J	(1ULL << 40)
489bdd5ae3aSLen Brown #define	BIC_Mod_c6	(1ULL << 41)
490bdd5ae3aSLen Brown #define	BIC_Totl_c0	(1ULL << 42)
491bdd5ae3aSLen Brown #define	BIC_Any_c0	(1ULL << 43)
492bdd5ae3aSLen Brown #define	BIC_GFX_c0	(1ULL << 44)
493bdd5ae3aSLen Brown #define	BIC_CPUGFX	(1ULL << 45)
494bdd5ae3aSLen Brown #define	BIC_Core	(1ULL << 46)
495bdd5ae3aSLen Brown #define	BIC_CPU		(1ULL << 47)
4964c2122d4SLen Brown #define	BIC_APIC	(1ULL << 48)
4974c2122d4SLen Brown #define	BIC_X2APIC	(1ULL << 49)
498812db3f7SLen Brown 
4994c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
5003f44a5c6SLen Brown 
5013f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
5024c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
503812db3f7SLen Brown 
504812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
5053f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
506812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
5070f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
508812db3f7SLen Brown 
5093f44a5c6SLen Brown 
510dd778a5eSLen Brown #define MAX_DEFERRED 16
511dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
512dd778a5eSLen Brown int deferred_skip_index;
513dd778a5eSLen Brown 
514dd778a5eSLen Brown /*
515dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
516dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
517dd778a5eSLen Brown  */
518dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
519dd778a5eSLen Brown 
520dd778a5eSLen Brown void help(void)
521dd778a5eSLen Brown {
522dd778a5eSLen Brown 	fprintf(outf,
523dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
524dd778a5eSLen Brown 	"\n"
525dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
526dd778a5eSLen Brown 	"when COMMAND completes.\n"
527dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
528dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
529cc481650SNathan Ciobanu 	"  -a, --add	add a counter\n"
530dd778a5eSLen Brown 	"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
531cc481650SNathan Ciobanu 	"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
532dd778a5eSLen Brown 	"		  {core | package | j,k,l..m,n-p }\n"
5339ce80578SNathan Ciobanu 	"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
5349ce80578SNathan Ciobanu 	"  -D, --Dump	displays the raw counter values\n"
5359ce80578SNathan Ciobanu 	"  -e, --enable	[all | column]\n"
5369ce80578SNathan Ciobanu 	"		shows all or the specified disabled column\n"
5379ce80578SNathan Ciobanu 	"  -H, --hide [column|column,column,...]\n"
5389ce80578SNathan Ciobanu 	"		hide the specified column(s)\n"
539cc481650SNathan Ciobanu 	"  -i, --interval sec.subsec\n"
540cc481650SNathan Ciobanu 	"		Override default 5-second measurement interval\n"
5419ce80578SNathan Ciobanu 	"  -J, --Joules	displays energy in Joules instead of Watts\n"
542cc481650SNathan Ciobanu 	"  -l, --list	list column headers only\n"
543cc481650SNathan Ciobanu 	"  -n, --num_iterations num\n"
544cc481650SNathan Ciobanu 	"		number of the measurement iterations\n"
545cc481650SNathan Ciobanu 	"  -o, --out file\n"
546cc481650SNathan Ciobanu 	"		create or truncate \"file\" for all output\n"
547cc481650SNathan Ciobanu 	"  -q, --quiet	skip decoding system configuration header\n"
5489ce80578SNathan Ciobanu 	"  -s, --show [column|column,column,...]\n"
5499ce80578SNathan Ciobanu 	"		show only the specified column(s)\n"
5509ce80578SNathan Ciobanu 	"  -S, --Summary\n"
5519ce80578SNathan Ciobanu 	"		limits output to 1-line system summary per interval\n"
5529ce80578SNathan Ciobanu 	"  -T, --TCC temperature\n"
5539ce80578SNathan Ciobanu 	"		sets the Thermal Control Circuit temperature in\n"
5549ce80578SNathan Ciobanu 	"		  degrees Celsius\n"
555cc481650SNathan Ciobanu 	"  -h, --help	print this help message\n"
556cc481650SNathan Ciobanu 	"  -v, --version	print version information\n"
557dd778a5eSLen Brown 	"\n"
558dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
559dd778a5eSLen Brown }
560dd778a5eSLen Brown 
561812db3f7SLen Brown /*
562812db3f7SLen Brown  * bic_lookup
563812db3f7SLen Brown  * for all the strings in comma separate name_list,
564812db3f7SLen Brown  * set the approprate bit in return value.
565812db3f7SLen Brown  */
566dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
567812db3f7SLen Brown {
568812db3f7SLen Brown 	int i;
569812db3f7SLen Brown 	unsigned long long retval = 0;
570812db3f7SLen Brown 
571812db3f7SLen Brown 	while (name_list) {
572812db3f7SLen Brown 		char *comma;
573812db3f7SLen Brown 
574812db3f7SLen Brown 		comma = strchr(name_list, ',');
575812db3f7SLen Brown 
576812db3f7SLen Brown 		if (comma)
577812db3f7SLen Brown 			*comma = '\0';
578812db3f7SLen Brown 
5793f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5803f44a5c6SLen Brown 			return ~0;
5813f44a5c6SLen Brown 
582812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
583812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
584812db3f7SLen Brown 				retval |= (1ULL << i);
585812db3f7SLen Brown 				break;
586812db3f7SLen Brown 			}
587812db3f7SLen Brown 		}
588812db3f7SLen Brown 		if (i == MAX_BIC) {
589dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
590812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
591812db3f7SLen Brown 				exit(-1);
592812db3f7SLen Brown 			}
593dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
594dd778a5eSLen Brown 			if (debug)
595dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
596dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
597dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
598dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
599dd778a5eSLen Brown 				help();
600dd778a5eSLen Brown 				exit(1);
601dd778a5eSLen Brown 			}
602dd778a5eSLen Brown 		}
603812db3f7SLen Brown 
604812db3f7SLen Brown 		name_list = comma;
605812db3f7SLen Brown 		if (name_list)
606812db3f7SLen Brown 			name_list++;
607812db3f7SLen Brown 
608812db3f7SLen Brown 	}
609812db3f7SLen Brown 	return retval;
610812db3f7SLen Brown }
611fc04cc67SLen Brown 
612dd778a5eSLen Brown 
613c8ade361SLen Brown void print_header(char *delim)
614103a8feaSLen Brown {
615388e9c81SLen Brown 	struct msr_counter *mp;
6166168c2e0SLen Brown 	int printed = 0;
617388e9c81SLen Brown 
6183f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
6193f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
6203f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
6213f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
622812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
6236168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
62401235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
62501235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
626812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
6276168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
628812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
6296168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
6304c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
6314c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
6324c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
6334c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
634812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
6356168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
636812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
6376168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
638812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
6396168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
640812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
6416168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
6421cc21f7bSLen Brown 
6430de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
6440de6c0dfSLen Brown 		if (sums_need_wide_columns)
6456168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
6460de6c0dfSLen Brown 		else
6476168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
6480de6c0dfSLen Brown 	}
6490de6c0dfSLen Brown 
650812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
6516168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
6521cc21f7bSLen Brown 
653388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
654dd778a5eSLen Brown 
655388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
656388e9c81SLen Brown 			if (mp->width == 64)
657dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
658388e9c81SLen Brown 			else
659dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
660388e9c81SLen Brown 		} else {
6610de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
662dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
6630de6c0dfSLen Brown 			else
664dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
665388e9c81SLen Brown 		}
666388e9c81SLen Brown 	}
667388e9c81SLen Brown 
66841618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
6696168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
670997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
6716168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
672812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6736168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
674812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6756168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
676678a3bd1SLen Brown 
6770539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6786168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
679678a3bd1SLen Brown 
680812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6816168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
682388e9c81SLen Brown 
683388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
684388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
685388e9c81SLen Brown 			if (mp->width == 64)
686c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
687388e9c81SLen Brown 			else
688c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
689388e9c81SLen Brown 		} else {
6900de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
6910de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
6920de6c0dfSLen Brown 			else
693c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
694388e9c81SLen Brown 		}
695388e9c81SLen Brown 	}
696388e9c81SLen Brown 
697812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
6986168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
699889facbeSLen Brown 
700812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
7016168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
702fdf676e5SLen Brown 
703812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
7046168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
70527d47356SLen Brown 
706a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
7076168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
708a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
7096168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
710a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
7116168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
712a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
7136168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
7140b2bb692SLen Brown 
7150f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
7166168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
7170f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
7186168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
7190f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
7206168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
7210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
7226168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
7230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
7246168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
7250f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
7266168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
7270f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
7286168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
729be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
730be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
731be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
732be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
733103a8feaSLen Brown 
7345c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
735812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
7366168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
737812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
7386168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
739812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
7406168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
741812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
7426168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
743812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7446168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
745812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7466168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
747d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
748812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
7496168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
750812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
7516168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
752812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
7536168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
754812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
7556168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
756812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7576168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
758812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7596168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
7605c56be9aSDirk Brandewie 	}
761388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
762388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
763388e9c81SLen Brown 			if (mp->width == 64)
764c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
765388e9c81SLen Brown 			else
766c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
767388e9c81SLen Brown 		} else {
7680de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7690de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7700de6c0dfSLen Brown 			else
771c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
772388e9c81SLen Brown 		}
773388e9c81SLen Brown 	}
774388e9c81SLen Brown 
775c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
776103a8feaSLen Brown }
777103a8feaSLen Brown 
778c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
779c98d5d94SLen Brown 	struct pkg_data *p)
780103a8feaSLen Brown {
781388e9c81SLen Brown 	int i;
782388e9c81SLen Brown 	struct msr_counter *mp;
783388e9c81SLen Brown 
7843b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
785c98d5d94SLen Brown 
786c98d5d94SLen Brown 	if (t) {
7873b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
7883b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
7893b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
7903b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
7913b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
7923b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
7936886fee4SLen Brown 
794812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
7950de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
796812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
797218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
798388e9c81SLen Brown 
799388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
800388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
801388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
802388e9c81SLen Brown 		}
803103a8feaSLen Brown 	}
804103a8feaSLen Brown 
805c98d5d94SLen Brown 	if (c) {
8063b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
8073b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
8083b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
8093b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
8103b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
811388e9c81SLen Brown 
812388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
813388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
814388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
815388e9c81SLen Brown 		}
8160539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
817c98d5d94SLen Brown 	}
818103a8feaSLen Brown 
819c98d5d94SLen Brown 	if (p) {
8203b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
8210b2bb692SLen Brown 
8220b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
8230b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
8240b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
8250b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
8260b2bb692SLen Brown 
8273b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
8280f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
8293b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
8300f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
8313b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
8320f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
8333b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
8343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
8353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
8363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
837be0e54c4SLen Brown 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
838be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
839be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
8403b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
8413b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
8423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
8433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
8443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
8453b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
8463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
8473b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
8483b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
849388e9c81SLen Brown 
850388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
851388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
852388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
853388e9c81SLen Brown 		}
854c98d5d94SLen Brown 	}
8553b4d5c7fSAndy Shevchenko 
8563b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
8573b4d5c7fSAndy Shevchenko 
858c98d5d94SLen Brown 	return 0;
859103a8feaSLen Brown }
860103a8feaSLen Brown 
861e23da037SLen Brown /*
862e23da037SLen Brown  * column formatting convention & formats
863e23da037SLen Brown  */
864c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
865c98d5d94SLen Brown 	struct pkg_data *p)
866103a8feaSLen Brown {
867008d396eSLen Brown 	double interval_float, tsc;
868fc04cc67SLen Brown 	char *fmt8;
869388e9c81SLen Brown 	int i;
870388e9c81SLen Brown 	struct msr_counter *mp;
8716168c2e0SLen Brown 	char *delim = "\t";
8726168c2e0SLen Brown 	int printed = 0;
873103a8feaSLen Brown 
874c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
875c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
876c98d5d94SLen Brown 		return 0;
877c98d5d94SLen Brown 
878c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
879c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
880c98d5d94SLen Brown 		return 0;
881c98d5d94SLen Brown 
8821ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
8831ef7d21aSLen Brown 	if ((t != &average.threads) &&
8841ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
8851ef7d21aSLen Brown 		return 0;
8861ef7d21aSLen Brown 
8873f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
888f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
889f4fdf2b4SLen Brown 		struct timeval tv;
890f4fdf2b4SLen Brown 
891f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
892f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
893f4fdf2b4SLen Brown 	}
894f4fdf2b4SLen Brown 
8953f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
8963f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
8973f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
8983f44a5c6SLen Brown 
899103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
900103a8feaSLen Brown 
901008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
902008d396eSLen Brown 
903c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
904c98d5d94SLen Brown 	if (t == &average.threads) {
905812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
9066168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
90701235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
90801235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
909812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
9106168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
911812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9126168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9134c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9144c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9154c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9164c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
917103a8feaSLen Brown 	} else {
918812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
919c98d5d94SLen Brown 			if (p)
9206168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
921c98d5d94SLen Brown 			else
9226168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
923c98d5d94SLen Brown 		}
92401235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
92501235041SPrarit Bhargava 			if (t)
92601235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
92701235041SPrarit Bhargava 						(printed++ ? delim : ""),
92801235041SPrarit Bhargava 					      cpus[t->cpu_id].physical_node_id);
92901235041SPrarit Bhargava 			else
93001235041SPrarit Bhargava 				outp += sprintf(outp, "%s-",
93101235041SPrarit Bhargava 						(printed++ ? delim : ""));
93201235041SPrarit Bhargava 		}
933812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
934c98d5d94SLen Brown 			if (c)
9356168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
936c98d5d94SLen Brown 			else
9376168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
938c98d5d94SLen Brown 		}
939812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9406168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
9414c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9424c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
9434c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9444c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
945103a8feaSLen Brown 	}
946fc04cc67SLen Brown 
947812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
9486168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
949fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
950fc04cc67SLen Brown 
951812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
9526168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
953103a8feaSLen Brown 
954812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
95521ed5574SLen Brown 		if (has_base_hz)
9566168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
95721ed5574SLen Brown 		else
9586168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
959008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
96021ed5574SLen Brown 	}
961103a8feaSLen Brown 
962812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
9636168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
964103a8feaSLen Brown 
965562a2d37SLen Brown 	/* IRQ */
9660de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
9670de6c0dfSLen Brown 		if (sums_need_wide_columns)
9686168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
9690de6c0dfSLen Brown 		else
9706168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
9710de6c0dfSLen Brown 	}
972562a2d37SLen Brown 
9731cc21f7bSLen Brown 	/* SMI */
974812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
9756168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
9761cc21f7bSLen Brown 
977678a3bd1SLen Brown 	/* Added counters */
978678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
979678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
980678a3bd1SLen Brown 			if (mp->width == 32)
9815f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
982678a3bd1SLen Brown 			else
9836168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
984678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
9850de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9866168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
9870de6c0dfSLen Brown 			else
9886168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
989678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
99041618e63SLen Brown 			if (mp->type == COUNTER_USEC)
9916168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
99241618e63SLen Brown 			else
9936168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
994678a3bd1SLen Brown 		}
995678a3bd1SLen Brown 	}
996678a3bd1SLen Brown 
99741618e63SLen Brown 	/* C1 */
99841618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
9996168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
100041618e63SLen Brown 
100141618e63SLen Brown 
1002c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1003c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1004c98d5d94SLen Brown 		goto done;
1005c98d5d94SLen Brown 
1006997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates)
10076168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1008812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
10096168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1010812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
10116168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1012c98d5d94SLen Brown 
10130539ba11SLen Brown 	/* Mod%c6 */
10140539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
10156168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
10160539ba11SLen Brown 
1017812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
10186168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1019889facbeSLen Brown 
1020388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1021388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1022388e9c81SLen Brown 			if (mp->width == 32)
10235f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1024388e9c81SLen Brown 			else
10256168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1026388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10270de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10286168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
10290de6c0dfSLen Brown 			else
10306168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1031388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10326168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1033388e9c81SLen Brown 		}
1034388e9c81SLen Brown 	}
1035388e9c81SLen Brown 
1036c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1037c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1038c98d5d94SLen Brown 		goto done;
1039c98d5d94SLen Brown 
10400b2bb692SLen Brown 	/* PkgTmp */
1041812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
10426168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1043889facbeSLen Brown 
1044fdf676e5SLen Brown 	/* GFXrc6 */
1045812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1046ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
10476168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
10489185e988SLen Brown 		} else {
10496168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
10509185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
10519185e988SLen Brown 		}
10529185e988SLen Brown 	}
1053fdf676e5SLen Brown 
105427d47356SLen Brown 	/* GFXMHz */
1055812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
10566168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
105727d47356SLen Brown 
10580b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1059a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10606168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1061a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10626168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1063a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
10646168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1065a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
10666168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
10670b2bb692SLen Brown 
10680f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
10696168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
10700f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
10716168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
10720f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
10736168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
10740f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
10756168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
10760f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
10776168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
10780f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
10796168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
10800f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
10816168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1082889facbeSLen Brown 
1083be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1084be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1085be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1086be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1087be0e54c4SLen Brown 
1088889facbeSLen Brown 	/*
1089889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
1090889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
1091889facbeSLen Brown  	 */
1092fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
10936168c2e0SLen Brown 		fmt8 = "%s%.2f";
1094fc04cc67SLen Brown 	else
1095fc04cc67SLen Brown 		fmt8 = "%6.0f**";
1096889facbeSLen Brown 
1097812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
10986168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1099812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
11006168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1101812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
11026168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1103812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
11046168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1105812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
11066168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
1107812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
11086168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1109812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
11106168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1111812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
11126168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1113812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
11146168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1115812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
11166168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1117812db3f7SLen Brown 
1118388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1119388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1120388e9c81SLen Brown 			if (mp->width == 32)
11215f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1122388e9c81SLen Brown 			else
11236168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1124388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
11250de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
11266168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
11270de6c0dfSLen Brown 			else
11286168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1129388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
11306168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1131388e9c81SLen Brown 		}
1132388e9c81SLen Brown 	}
1133388e9c81SLen Brown 
1134c98d5d94SLen Brown done:
113594d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1136c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1137c98d5d94SLen Brown 
1138c98d5d94SLen Brown 	return 0;
1139103a8feaSLen Brown }
1140103a8feaSLen Brown 
1141b7d8c148SLen Brown void flush_output_stdout(void)
1142103a8feaSLen Brown {
1143b7d8c148SLen Brown 	FILE *filep;
1144b7d8c148SLen Brown 
1145b7d8c148SLen Brown 	if (outf == stderr)
1146b7d8c148SLen Brown 		filep = stdout;
1147b7d8c148SLen Brown 	else
1148b7d8c148SLen Brown 		filep = outf;
1149b7d8c148SLen Brown 
1150b7d8c148SLen Brown 	fputs(output_buffer, filep);
1151b7d8c148SLen Brown 	fflush(filep);
1152b7d8c148SLen Brown 
1153c98d5d94SLen Brown 	outp = output_buffer;
1154c98d5d94SLen Brown }
1155b7d8c148SLen Brown void flush_output_stderr(void)
1156c98d5d94SLen Brown {
1157b7d8c148SLen Brown 	fputs(output_buffer, outf);
1158b7d8c148SLen Brown 	fflush(outf);
1159c98d5d94SLen Brown 	outp = output_buffer;
1160c98d5d94SLen Brown }
1161c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1162c98d5d94SLen Brown {
1163e23da037SLen Brown 	static int printed;
1164103a8feaSLen Brown 
1165e23da037SLen Brown 	if (!printed || !summary_only)
1166c8ade361SLen Brown 		print_header("\t");
1167103a8feaSLen Brown 
11689d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1169103a8feaSLen Brown 
1170e23da037SLen Brown 	printed = 1;
1171e23da037SLen Brown 
1172e23da037SLen Brown 	if (summary_only)
1173e23da037SLen Brown 		return;
1174e23da037SLen Brown 
1175c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1176103a8feaSLen Brown }
1177103a8feaSLen Brown 
1178889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1179889facbeSLen Brown 	if (new > old) {			\
1180889facbeSLen Brown 		old = new - old;		\
1181889facbeSLen Brown 	} else {				\
1182889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1183889facbeSLen Brown 	}
1184889facbeSLen Brown 
1185ba3dec99SLen Brown int
1186c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1187103a8feaSLen Brown {
1188388e9c81SLen Brown 	int i;
1189388e9c81SLen Brown 	struct msr_counter *mp;
11900b2bb692SLen Brown 
1191a99d8730SLen Brown 
1192a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
11930b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1194a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
11950b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1196a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
11970b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1198a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
11990b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1200a99d8730SLen Brown 
1201c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
12020f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1203c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
12040f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1205c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
12060f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1207c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1208ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1209ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1210ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1211be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1212be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1213889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1214889facbeSLen Brown 
12159185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
12169185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12179185e988SLen Brown 		old->gfx_rc6_ms = -1;
12189185e988SLen Brown 	else
1219fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12209185e988SLen Brown 
122127d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
122227d47356SLen Brown 
1223889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1224889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1225889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1226889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1227889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1228889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1229ba3dec99SLen Brown 
1230388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1231388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1232388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1233388e9c81SLen Brown 		else
1234388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1235388e9c81SLen Brown 	}
1236388e9c81SLen Brown 
1237ba3dec99SLen Brown 	return 0;
1238103a8feaSLen Brown }
1239103a8feaSLen Brown 
1240c98d5d94SLen Brown void
1241c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1242c98d5d94SLen Brown {
1243388e9c81SLen Brown 	int i;
1244388e9c81SLen Brown 	struct msr_counter *mp;
1245388e9c81SLen Brown 
1246c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1247c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1248c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1249889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12500539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1251388e9c81SLen Brown 
1252388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1253388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1254388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1255388e9c81SLen Brown 		else
1256388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1257388e9c81SLen Brown 	}
1258103a8feaSLen Brown }
1259c98d5d94SLen Brown 
1260c3ae331dSLen Brown /*
1261c3ae331dSLen Brown  * old = new - old
1262c3ae331dSLen Brown  */
1263ba3dec99SLen Brown int
1264c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1265c98d5d94SLen Brown 	struct core_data *core_delta)
1266c98d5d94SLen Brown {
1267388e9c81SLen Brown 	int i;
1268388e9c81SLen Brown 	struct msr_counter *mp;
1269388e9c81SLen Brown 
12704c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
12714c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
12724c2122d4SLen Brown 		new->apic_id = old->apic_id;
12734c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
12744c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
12754c2122d4SLen Brown 
12763f44a5c6SLen Brown 	/*
12773f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
12783f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
12793f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
12803f44a5c6SLen Brown 	 */
12813f44a5c6SLen Brown 
12823f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
12833f44a5c6SLen Brown 	old->tv_end = new->tv_end;
12843f44a5c6SLen Brown 
1285c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1286c98d5d94SLen Brown 
1287103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1288b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1289b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1290b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1291b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1292103a8feaSLen Brown 
1293c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1294c98d5d94SLen Brown 
1295812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1296c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1297c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1298c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1299c98d5d94SLen Brown 		} else {
1300ba3dec99SLen Brown 			return -1;
1301103a8feaSLen Brown 		}
1302a729617cSLen Brown 	}
1303103a8feaSLen Brown 
1304c98d5d94SLen Brown 
1305144b44b1SLen Brown 	if (use_c1_residency_msr) {
1306144b44b1SLen Brown 		/*
1307144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1308144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1309144b44b1SLen Brown 		 */
1310144b44b1SLen Brown 	} else {
1311103a8feaSLen Brown 		/*
1312c3ae331dSLen Brown 		 * As counter collection is not atomic,
1313c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1314103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1315103a8feaSLen Brown 		 */
131695149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1317c98d5d94SLen Brown 			old->c1 = 0;
1318c98d5d94SLen Brown 		else {
1319c98d5d94SLen Brown 			/* normal case, derive c1 */
1320008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1321c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1322c98d5d94SLen Brown 		}
1323144b44b1SLen Brown 	}
1324c3ae331dSLen Brown 
1325c98d5d94SLen Brown 	if (old->mperf == 0) {
1326b7d8c148SLen Brown 		if (debug > 1)
1327b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1328c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1329c98d5d94SLen Brown 	}
1330103a8feaSLen Brown 
1331812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1332562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1333562a2d37SLen Brown 
1334812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13351ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1336ba3dec99SLen Brown 
1337388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1338388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1339388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1340388e9c81SLen Brown 		else
1341388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1342388e9c81SLen Brown 	}
1343ba3dec99SLen Brown 	return 0;
1344103a8feaSLen Brown }
1345c98d5d94SLen Brown 
1346c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1347c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1348c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1349c98d5d94SLen Brown {
1350ba3dec99SLen Brown 	int retval = 0;
1351ba3dec99SLen Brown 
1352c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1353c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1354c98d5d94SLen Brown 		delta_core(c, c2);
1355c98d5d94SLen Brown 
1356c98d5d94SLen Brown 	/* always calculate thread delta */
1357ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1358ba3dec99SLen Brown 	if (retval)
1359ba3dec99SLen Brown 		return retval;
1360c98d5d94SLen Brown 
1361c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1362c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1363ba3dec99SLen Brown 		retval = delta_package(p, p2);
1364c98d5d94SLen Brown 
1365ba3dec99SLen Brown 	return retval;
1366103a8feaSLen Brown }
1367103a8feaSLen Brown 
1368c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1369103a8feaSLen Brown {
1370388e9c81SLen Brown 	int i;
1371388e9c81SLen Brown 	struct msr_counter  *mp;
1372388e9c81SLen Brown 
13733f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
13743f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
13753f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
13763f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
13773f44a5c6SLen Brown 
1378c98d5d94SLen Brown 	t->tsc = 0;
1379c98d5d94SLen Brown 	t->aperf = 0;
1380c98d5d94SLen Brown 	t->mperf = 0;
1381c98d5d94SLen Brown 	t->c1 = 0;
1382103a8feaSLen Brown 
1383562a2d37SLen Brown 	t->irq_count = 0;
1384562a2d37SLen Brown 	t->smi_count = 0;
1385562a2d37SLen Brown 
1386c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1387c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1388c98d5d94SLen Brown 
1389c98d5d94SLen Brown 	c->c3 = 0;
1390c98d5d94SLen Brown 	c->c6 = 0;
1391c98d5d94SLen Brown 	c->c7 = 0;
13920539ba11SLen Brown 	c->mc6_us = 0;
1393889facbeSLen Brown 	c->core_temp_c = 0;
1394c98d5d94SLen Brown 
13950b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
13960b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
13970b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
13980b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
13990b2bb692SLen Brown 
1400c98d5d94SLen Brown 	p->pc2 = 0;
14010f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1402c98d5d94SLen Brown 		p->pc3 = 0;
14030f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1404c98d5d94SLen Brown 		p->pc6 = 0;
14050f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1406c98d5d94SLen Brown 		p->pc7 = 0;
1407ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1408ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1409ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1410be0e54c4SLen Brown 	p->cpu_lpi = 0;
1411be0e54c4SLen Brown 	p->sys_lpi = 0;
1412889facbeSLen Brown 
1413889facbeSLen Brown 	p->energy_pkg = 0;
1414889facbeSLen Brown 	p->energy_dram = 0;
1415889facbeSLen Brown 	p->energy_cores = 0;
1416889facbeSLen Brown 	p->energy_gfx = 0;
1417889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1418889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1419889facbeSLen Brown 	p->pkg_temp_c = 0;
142027d47356SLen Brown 
1421fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
142227d47356SLen Brown 	p->gfx_mhz = 0;
1423388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1424388e9c81SLen Brown 		t->counter[i] = 0;
1425388e9c81SLen Brown 
1426388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1427388e9c81SLen Brown 		c->counter[i] = 0;
1428388e9c81SLen Brown 
1429388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1430388e9c81SLen Brown 		p->counter[i] = 0;
1431103a8feaSLen Brown }
1432c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1433c98d5d94SLen Brown 	struct pkg_data *p)
1434103a8feaSLen Brown {
1435388e9c81SLen Brown 	int i;
1436388e9c81SLen Brown 	struct msr_counter *mp;
1437388e9c81SLen Brown 
14384c2122d4SLen Brown 	/* copy un-changing apic_id's */
14394c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
14404c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
14414c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
14424c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
14434c2122d4SLen Brown 
14443f44a5c6SLen Brown 	/* remember first tv_begin */
14453f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14463f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14473f44a5c6SLen Brown 
14483f44a5c6SLen Brown 	/* remember last tv_end */
14493f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14503f44a5c6SLen Brown 
1451c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1452c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1453c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1454c98d5d94SLen Brown 	average.threads.c1 += t->c1;
145515aaa346SLen Brown 
1456562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1457562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1458562a2d37SLen Brown 
1459388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1460388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1461388e9c81SLen Brown 			continue;
1462388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1463388e9c81SLen Brown 	}
1464388e9c81SLen Brown 
1465c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1466c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1467c98d5d94SLen Brown 		return 0;
1468c98d5d94SLen Brown 
1469c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1470c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1471c98d5d94SLen Brown 	average.cores.c7 += c->c7;
14720539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1473c98d5d94SLen Brown 
1474889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1475889facbeSLen Brown 
1476388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1477388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1478388e9c81SLen Brown 			continue;
1479388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1480388e9c81SLen Brown 	}
1481388e9c81SLen Brown 
1482c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1483c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1484c98d5d94SLen Brown 		return 0;
1485c98d5d94SLen Brown 
1486a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
14870b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1488a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
14890b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1490a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14910b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1492a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14930b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
14940b2bb692SLen Brown 
1495c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
14960f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1497c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
14980f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1499c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
15000f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1501c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1502ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1503ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1504ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1505c98d5d94SLen Brown 
1506be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1507be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1508be0e54c4SLen Brown 
1509889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1510889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1511889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1512889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1513889facbeSLen Brown 
1514fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
151527d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
151627d47356SLen Brown 
1517889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1518889facbeSLen Brown 
1519889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1520889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1521388e9c81SLen Brown 
1522388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1523388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1524388e9c81SLen Brown 			continue;
1525388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1526388e9c81SLen Brown 	}
1527c98d5d94SLen Brown 	return 0;
1528c98d5d94SLen Brown }
1529c98d5d94SLen Brown /*
1530c98d5d94SLen Brown  * sum the counters for all cpus in the system
1531c98d5d94SLen Brown  * compute the weighted average
1532c98d5d94SLen Brown  */
1533c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1534c98d5d94SLen Brown 	struct pkg_data *p)
1535c98d5d94SLen Brown {
1536388e9c81SLen Brown 	int i;
1537388e9c81SLen Brown 	struct msr_counter *mp;
1538388e9c81SLen Brown 
1539c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1540c98d5d94SLen Brown 
1541c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1542c98d5d94SLen Brown 
1543c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1544c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1545c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1546c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1547c98d5d94SLen Brown 
15480de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
15490de6c0dfSLen Brown 		sums_need_wide_columns = 1;
15500de6c0dfSLen Brown 
1551c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1552c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1553c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
15540539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1555c98d5d94SLen Brown 
1556a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15570b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1558a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15590b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1560a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15610b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1562a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15630b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
15640b2bb692SLen Brown 
1565c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
15660f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1567c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
15680f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1569c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
15700f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1571c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1572ca58710fSKristen Carlson Accardi 
1573ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1574ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1575ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1576388e9c81SLen Brown 
1577388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1578388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1579388e9c81SLen Brown 			continue;
15800de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15810de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
15820de6c0dfSLen Brown 				sums_need_wide_columns = 1;
158341618e63SLen Brown 			continue;
15840de6c0dfSLen Brown 		}
1585388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1586388e9c81SLen Brown 	}
1587388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1588388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1589388e9c81SLen Brown 			continue;
15900de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
15910de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
15920de6c0dfSLen Brown 				sums_need_wide_columns = 1;
15930de6c0dfSLen Brown 		}
1594388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1595388e9c81SLen Brown 	}
1596388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1597388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1598388e9c81SLen Brown 			continue;
15990de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16000de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
16010de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16020de6c0dfSLen Brown 		}
1603388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1604388e9c81SLen Brown 	}
1605c98d5d94SLen Brown }
1606c98d5d94SLen Brown 
1607c98d5d94SLen Brown static unsigned long long rdtsc(void)
1608c98d5d94SLen Brown {
1609c98d5d94SLen Brown 	unsigned int low, high;
1610c98d5d94SLen Brown 
1611c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1612c98d5d94SLen Brown 
1613c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1614c98d5d94SLen Brown }
1615c98d5d94SLen Brown 
1616c98d5d94SLen Brown /*
1617495c7654SLen Brown  * Open a file, and exit on failure
1618495c7654SLen Brown  */
1619495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1620495c7654SLen Brown {
1621495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1622495c7654SLen Brown 
1623495c7654SLen Brown 	if (!filep)
1624495c7654SLen Brown 		err(1, "%s: open failed", path);
1625495c7654SLen Brown 	return filep;
1626495c7654SLen Brown }
1627495c7654SLen Brown /*
1628495c7654SLen Brown  * snapshot_sysfs_counter()
1629495c7654SLen Brown  *
1630495c7654SLen Brown  * return snapshot of given counter
1631495c7654SLen Brown  */
1632495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1633495c7654SLen Brown {
1634495c7654SLen Brown 	FILE *fp;
1635495c7654SLen Brown 	int retval;
1636495c7654SLen Brown 	unsigned long long counter;
1637495c7654SLen Brown 
1638495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1639495c7654SLen Brown 
1640495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1641495c7654SLen Brown 	if (retval != 1)
1642495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1643495c7654SLen Brown 
1644495c7654SLen Brown 	fclose(fp);
1645495c7654SLen Brown 
1646495c7654SLen Brown 	return counter;
1647495c7654SLen Brown }
1648495c7654SLen Brown 
1649495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1650495c7654SLen Brown {
1651495c7654SLen Brown 	if (mp->msr_num != 0) {
1652495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1653495c7654SLen Brown 			return -1;
1654495c7654SLen Brown 	} else {
165546c27978SLen Brown 		char path[128 + PATH_BYTES];
165641618e63SLen Brown 
165741618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
165841618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
165941618e63SLen Brown 				 cpu, mp->path);
166041618e63SLen Brown 
166141618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
166241618e63SLen Brown 		} else {
1663495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1664495c7654SLen Brown 		}
166541618e63SLen Brown 	}
1666495c7654SLen Brown 
1667495c7654SLen Brown 	return 0;
1668495c7654SLen Brown }
1669495c7654SLen Brown 
16704c2122d4SLen Brown void get_apic_id(struct thread_data *t)
16714c2122d4SLen Brown {
167234041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
167334041551SLen Brown 
167434041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
167534041551SLen Brown 		eax = ebx = ecx = edx = 0;
167634041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
167734041551SLen Brown 
167834041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
167934041551SLen Brown 	}
168034041551SLen Brown 
168134041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
168234041551SLen Brown 		return;
168334041551SLen Brown 
168434041551SLen Brown 	if (authentic_amd) {
168534041551SLen Brown 		unsigned int topology_extensions;
168634041551SLen Brown 
168734041551SLen Brown 		if (max_extended_level < 0x8000001e)
168834041551SLen Brown 			return;
16894c2122d4SLen Brown 
16904c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
169134041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
169234041551SLen Brown 			topology_extensions = ecx & (1 << 22);
169334041551SLen Brown 
169434041551SLen Brown 		if (topology_extensions == 0)
169534041551SLen Brown 			return;
169634041551SLen Brown 
169734041551SLen Brown 		eax = ebx = ecx = edx = 0;
169834041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
169934041551SLen Brown 
170034041551SLen Brown 		t->x2apic_id = eax;
170134041551SLen Brown 		return;
170234041551SLen Brown 	}
17034c2122d4SLen Brown 
17044c2122d4SLen Brown 	if (!genuine_intel)
17054c2122d4SLen Brown 		return;
17064c2122d4SLen Brown 
17074c2122d4SLen Brown 	if (max_level < 0xb)
17084c2122d4SLen Brown 		return;
17094c2122d4SLen Brown 
17104c2122d4SLen Brown 	ecx = 0;
17114c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
17124c2122d4SLen Brown 	t->x2apic_id = edx;
17134c2122d4SLen Brown 
171434041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
171534041551SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
171634041551SLen Brown 				t->cpu_id, t->apic_id, t->x2apic_id);
17174c2122d4SLen Brown }
17184c2122d4SLen Brown 
1719495c7654SLen Brown /*
1720c98d5d94SLen Brown  * get_counters(...)
1721c98d5d94SLen Brown  * migrate to cpu
1722c98d5d94SLen Brown  * acquire and record local counters for that cpu
1723c98d5d94SLen Brown  */
1724c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1725c98d5d94SLen Brown {
1726c98d5d94SLen Brown 	int cpu = t->cpu_id;
1727889facbeSLen Brown 	unsigned long long msr;
17280102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1729388e9c81SLen Brown 	struct msr_counter *mp;
1730388e9c81SLen Brown 	int i;
1731c98d5d94SLen Brown 
1732f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1733f4fdf2b4SLen Brown 
1734e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1735b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
173615aaa346SLen Brown 		return -1;
1737e52966c0SLen Brown 	}
173815aaa346SLen Brown 
17394c2122d4SLen Brown 	if (first_counter_read)
17404c2122d4SLen Brown 		get_apic_id(t);
17410102b067SLen Brown retry:
1742c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
174315aaa346SLen Brown 
1744812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
17450102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
17460102b067SLen Brown 
17470102b067SLen Brown 		/*
17480102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
17490102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
17500102b067SLen Brown 		 *
17510102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
17520102b067SLen Brown 		 * individual system call, so delays may occur
17530102b067SLen Brown 		 * between them.  If the time to read them
17540102b067SLen Brown 		 * varies by a large amount, we re-read them.
17550102b067SLen Brown 		 */
17560102b067SLen Brown 
17570102b067SLen Brown 		/*
17580102b067SLen Brown 		 * This initial dummy APERF read has been seen to
17590102b067SLen Brown 		 * reduce jitter in the subsequent reads.
17600102b067SLen Brown 		 */
17610102b067SLen Brown 
17629c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1763c98d5d94SLen Brown 			return -3;
17640102b067SLen Brown 
17650102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
17660102b067SLen Brown 
17670102b067SLen Brown 		tsc_before = t->tsc;
17680102b067SLen Brown 
17690102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
17700102b067SLen Brown 			return -3;
17710102b067SLen Brown 
17720102b067SLen Brown 		tsc_between = rdtsc();
17730102b067SLen Brown 
17749c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1775c98d5d94SLen Brown 			return -4;
17760102b067SLen Brown 
17770102b067SLen Brown 		tsc_after = rdtsc();
17780102b067SLen Brown 
17790102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
17800102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
17810102b067SLen Brown 
17820102b067SLen Brown 		/*
17830102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
17840102b067SLen Brown 		 * differ by more than 2x, then try again.
17850102b067SLen Brown 		 */
17860102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
17870102b067SLen Brown 			aperf_mperf_retry_count++;
17880102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
17890102b067SLen Brown 				goto retry;
17900102b067SLen Brown 			else
17910102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
17920102b067SLen Brown 					cpu, aperf_time, mperf_time);
17930102b067SLen Brown 		}
17940102b067SLen Brown 		aperf_mperf_retry_count = 0;
17950102b067SLen Brown 
1796b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1797b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
179888c3281fSLen Brown 	}
179988c3281fSLen Brown 
1800812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1801562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1802812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
18031ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
18041ed51011SLen Brown 			return -5;
18051ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
18061ed51011SLen Brown 	}
18070539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1808144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1809144b44b1SLen Brown 			return -6;
1810144b44b1SLen Brown 	}
1811144b44b1SLen Brown 
1812388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1813495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1814388e9c81SLen Brown 			return -10;
1815388e9c81SLen Brown 	}
1816388e9c81SLen Brown 
1817c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1818c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1819f4fdf2b4SLen Brown 		goto done;
1820c98d5d94SLen Brown 
1821997e5395SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates && !do_cnl_cstates) {
1822c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1823c98d5d94SLen Brown 			return -6;
1824144b44b1SLen Brown 	}
1825144b44b1SLen Brown 
1826812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1827c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1828c98d5d94SLen Brown 			return -7;
1829fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1830fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1831fb5d4327SDasaratharaman Chandramouli 			return -7;
1832103a8feaSLen Brown 	}
183315aaa346SLen Brown 
1834812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1835c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1836c98d5d94SLen Brown 			return -8;
1837c98d5d94SLen Brown 
18380539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18390539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18400539ba11SLen Brown 			return -8;
18410539ba11SLen Brown 
1842812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1843889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1844889facbeSLen Brown 			return -9;
1845889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1846889facbeSLen Brown 	}
1847889facbeSLen Brown 
1848388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1849495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1850388e9c81SLen Brown 			return -10;
1851388e9c81SLen Brown 	}
1852889facbeSLen Brown 
1853c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1854c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1855f4fdf2b4SLen Brown 		goto done;
185615aaa346SLen Brown 
1857a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
18580b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
18590b2bb692SLen Brown 			return -10;
1860a99d8730SLen Brown 	}
1861a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
18620b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
18630b2bb692SLen Brown 			return -11;
1864a99d8730SLen Brown 	}
1865a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
18660b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
18670b2bb692SLen Brown 			return -12;
1868a99d8730SLen Brown 	}
1869a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
18700b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
18710b2bb692SLen Brown 			return -13;
18720b2bb692SLen Brown 	}
18730f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1874c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1875c98d5d94SLen Brown 			return -9;
18760f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
18770539ba11SLen Brown 		if (do_slm_cstates) {
18780539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
18790539ba11SLen Brown 				return -10;
18800539ba11SLen Brown 		} else {
1881c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1882c98d5d94SLen Brown 				return -10;
18830539ba11SLen Brown 		}
18840539ba11SLen Brown 	}
18850539ba11SLen Brown 
18860f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1887c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1888c98d5d94SLen Brown 			return -11;
18890f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1890c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1891c98d5d94SLen Brown 			return -12;
18920f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1893ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1894ca58710fSKristen Carlson Accardi 			return -13;
18950f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1896ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1897ca58710fSKristen Carlson Accardi 			return -13;
18980f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1899ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1900ca58710fSKristen Carlson Accardi 			return -13;
19010f47c08dSLen Brown 
1902be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1903be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1904be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1905be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1906be0e54c4SLen Brown 
1907889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1908889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1909889facbeSLen Brown 			return -13;
1910889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1911889facbeSLen Brown 	}
19129148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1913889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1914889facbeSLen Brown 			return -14;
1915889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1916889facbeSLen Brown 	}
1917889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1918889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1919889facbeSLen Brown 			return -15;
1920889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1921889facbeSLen Brown 	}
1922889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1923889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1924889facbeSLen Brown 			return -16;
1925889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1926889facbeSLen Brown 	}
1927889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1928889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1929889facbeSLen Brown 			return -16;
1930889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1931889facbeSLen Brown 	}
1932889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1933889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1934889facbeSLen Brown 			return -16;
1935889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1936889facbeSLen Brown 	}
1937812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1938889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1939889facbeSLen Brown 			return -17;
1940889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1941889facbeSLen Brown 	}
1942fdf676e5SLen Brown 
1943812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1944fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1945fdf676e5SLen Brown 
1946812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
194727d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
194827d47356SLen Brown 
1949388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1950495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1951388e9c81SLen Brown 			return -10;
1952388e9c81SLen Brown 	}
1953f4fdf2b4SLen Brown done:
1954f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1955388e9c81SLen Brown 
195615aaa346SLen Brown 	return 0;
1957103a8feaSLen Brown }
1958103a8feaSLen Brown 
1959ee7e38e3SLen Brown /*
1960ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1961ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1962ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1963ee7e38e3SLen Brown  */
1964ee7e38e3SLen Brown 
1965ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1966ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1967ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1968ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1969ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1970ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1971ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1972ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1973ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1974ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1975ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1976ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
19770b2bb692SLen Brown #define PCL__8 12 /* PC8 */
19780b2bb692SLen Brown #define PCL__9 13 /* PC9 */
19790b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1980ee7e38e3SLen Brown 
1981ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1982ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
19830b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1984ee7e38e3SLen Brown 
1985e9257f5fSLen 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};
1986e9257f5fSLen 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};
1987e9257f5fSLen 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};
19880539ba11SLen 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};
1989f2642888SLen 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};
1990e9257f5fSLen 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};
1991e4085d54SLen Brown int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
19922085e124SArtem 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};
1993ee7e38e3SLen Brown 
1994a2b7b749SLen Brown 
1995a2b7b749SLen Brown static void
1996a2b7b749SLen Brown calculate_tsc_tweak()
1997a2b7b749SLen Brown {
1998a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1999a2b7b749SLen Brown }
2000a2b7b749SLen Brown 
2001fcd17211SLen Brown static void
2002fcd17211SLen Brown dump_nhm_platform_info(void)
2003103a8feaSLen Brown {
2004103a8feaSLen Brown 	unsigned long long msr;
2005103a8feaSLen Brown 	unsigned int ratio;
2006103a8feaSLen Brown 
2007ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2008103a8feaSLen Brown 
2009b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
20106574a5d5SLen Brown 
2011103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
2012710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2013103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2014103a8feaSLen Brown 
2015103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
2016710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2017103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2018103a8feaSLen Brown 
20197ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2020b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2021bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
202267920418SLen Brown 
2023fcd17211SLen Brown 	return;
2024fcd17211SLen Brown }
2025fcd17211SLen Brown 
2026fcd17211SLen Brown static void
2027fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
2028fcd17211SLen Brown {
2029fcd17211SLen Brown 	unsigned long long msr;
2030fcd17211SLen Brown 	unsigned int ratio;
2031fcd17211SLen Brown 
20327ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2033fcd17211SLen Brown 
2034b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2035fcd17211SLen Brown 
2036fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2037fcd17211SLen Brown 	if (ratio)
2038710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2039fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2040fcd17211SLen Brown 
2041fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2042fcd17211SLen Brown 	if (ratio)
2043710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2044fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2045fcd17211SLen Brown 	return;
2046fcd17211SLen Brown }
2047fcd17211SLen Brown 
2048fcd17211SLen Brown static void
2049fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2050fcd17211SLen Brown {
2051fcd17211SLen Brown 	unsigned long long msr;
2052fcd17211SLen Brown 	unsigned int ratio;
20536574a5d5SLen Brown 
20547ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
20556574a5d5SLen Brown 
2056b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
20576574a5d5SLen Brown 
20586574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
20596574a5d5SLen Brown 	if (ratio)
2060710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
20616574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20626574a5d5SLen Brown 
20636574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
20646574a5d5SLen Brown 	if (ratio)
2065710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
20666574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20676574a5d5SLen Brown 
20686574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
20696574a5d5SLen Brown 	if (ratio)
2070710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
20716574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20726574a5d5SLen Brown 
20736574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
20746574a5d5SLen Brown 	if (ratio)
2075710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
20766574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20776574a5d5SLen Brown 
20786574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
20796574a5d5SLen Brown 	if (ratio)
2080710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
20816574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20826574a5d5SLen Brown 
20836574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
20846574a5d5SLen Brown 	if (ratio)
2085710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
20866574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20876574a5d5SLen Brown 
20886574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
20896574a5d5SLen Brown 	if (ratio)
2090710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
20916574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
20926574a5d5SLen Brown 
20936574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
20946574a5d5SLen Brown 	if (ratio)
2095710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
20966574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2097103a8feaSLen Brown 	return;
2098fcd17211SLen Brown }
209931e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
210031e07522SLen Brown {
210131e07522SLen Brown 
210231e07522SLen Brown 	if (!genuine_intel)
210331e07522SLen Brown 		return 0;
210431e07522SLen Brown 
210531e07522SLen Brown 	switch (model) {
210631e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
210731e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
2108f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:
210931e07522SLen Brown 		return 1;
211031e07522SLen Brown 	}
211131e07522SLen Brown 	return 0;
211231e07522SLen Brown }
2113fcd17211SLen Brown 
2114fcd17211SLen Brown static void
211531e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2116fcd17211SLen Brown {
211731e07522SLen Brown 	unsigned long long msr, core_counts;
211831e07522SLen Brown 	unsigned int ratio, group_size;
2119103a8feaSLen Brown 
21207ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2121b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
21226574a5d5SLen Brown 
212331e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
212431e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
212531e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
212631e07522SLen Brown 	} else {
212731e07522SLen Brown 		core_counts = 0x0807060504030201;
212831e07522SLen Brown 	}
212931e07522SLen Brown 
21306574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
213131e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
21326574a5d5SLen Brown 	if (ratio)
213331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
213431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21356574a5d5SLen Brown 
21366574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
213731e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
21386574a5d5SLen Brown 	if (ratio)
213931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
214031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21416574a5d5SLen Brown 
21426574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
214331e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
21446574a5d5SLen Brown 	if (ratio)
214531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
214631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21476574a5d5SLen Brown 
21486574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
214931e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
21506574a5d5SLen Brown 	if (ratio)
215131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
215231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21536574a5d5SLen Brown 
2154103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
215531e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2156103a8feaSLen Brown 	if (ratio)
215731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
215831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2159103a8feaSLen Brown 
2160103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
216131e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2162103a8feaSLen Brown 	if (ratio)
216331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
216431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2165103a8feaSLen Brown 
2166103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
216731e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2168103a8feaSLen Brown 	if (ratio)
216931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
217031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2171103a8feaSLen Brown 
2172103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
217331e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2174103a8feaSLen Brown 	if (ratio)
217531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
217631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2177fcd17211SLen Brown 	return;
2178fcd17211SLen Brown }
21793a9a941dSLen Brown 
2180fcd17211SLen Brown static void
21810f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
21820f7887c4SLen Brown {
21830f7887c4SLen Brown 	unsigned long long msr;
21840f7887c4SLen Brown 	unsigned int ratio;
21850f7887c4SLen Brown 
21860f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
21870f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
21880f7887c4SLen Brown 
21890f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
21900f7887c4SLen Brown 	if (ratio)
21910f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
21920f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21930f7887c4SLen Brown 
21940f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
21950f7887c4SLen Brown 	if (ratio)
21960f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
21970f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
21980f7887c4SLen Brown 
21990f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22000f7887c4SLen Brown 	if (ratio)
22010f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
22020f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22030f7887c4SLen Brown 
22040f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
22050f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22060f7887c4SLen Brown 
22070f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
22080f7887c4SLen Brown 	if (ratio)
22090f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
22100f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22110f7887c4SLen Brown 
22120f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22130f7887c4SLen Brown 	if (ratio)
22140f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
22150f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22160f7887c4SLen Brown 
22170f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22180f7887c4SLen Brown 	if (ratio)
22190f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
22200f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22210f7887c4SLen Brown 
22220f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22230f7887c4SLen Brown 	if (ratio)
22240f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
22250f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22260f7887c4SLen Brown }
22270f7887c4SLen Brown 
22280f7887c4SLen Brown static void
2229fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2230fb5d4327SDasaratharaman Chandramouli {
2231cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2232cbf97abaSHubert Chrzaniuk 
2233fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2234cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2235cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2236cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2237cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2238fb5d4327SDasaratharaman Chandramouli 
2239ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2240fb5d4327SDasaratharaman Chandramouli 
2241b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2242bfae2052SLen Brown 		base_cpu, msr);
2243fb5d4327SDasaratharaman Chandramouli 
2244fb5d4327SDasaratharaman Chandramouli 	/**
2245fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2246cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2247cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2248fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2249fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2250fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2251fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2252fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2253fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2254fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2255fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2256fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2257fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2258fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2259fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2260fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2261fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2262fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2263fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2264fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2265fb5d4327SDasaratharaman Chandramouli 	 */
2266fb5d4327SDasaratharaman Chandramouli 
2267cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2268cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2269cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2270cbf97abaSHubert Chrzaniuk 
2271cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2272fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2273cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2274fb5d4327SDasaratharaman Chandramouli 
2275cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2276cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2277cbf97abaSHubert Chrzaniuk 		b_nr++;
2278fb5d4327SDasaratharaman Chandramouli 	}
2279cbf97abaSHubert Chrzaniuk 
2280cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2281cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2282b7d8c148SLen Brown 			fprintf(outf,
2283710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2284cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2285fb5d4327SDasaratharaman Chandramouli }
2286fb5d4327SDasaratharaman Chandramouli 
2287fb5d4327SDasaratharaman Chandramouli static void
2288fcd17211SLen Brown dump_nhm_cst_cfg(void)
2289fcd17211SLen Brown {
2290fcd17211SLen Brown 	unsigned long long msr;
2291fcd17211SLen Brown 
22921df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2293fcd17211SLen Brown 
22941df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2295fcd17211SLen Brown 
22963e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2297fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2298fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2299fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2300fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2301fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
23026c34f160SLen Brown 		(unsigned int)msr & 0xF,
2303fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2304ac980e13SArtem Bityutskiy 
2305ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2306ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2307ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2308ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2309ac980e13SArtem Bityutskiy 	}
2310ac980e13SArtem Bityutskiy 
2311ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2312ac980e13SArtem Bityutskiy 
2313fcd17211SLen Brown 	return;
2314103a8feaSLen Brown }
2315103a8feaSLen Brown 
23166fb3143bSLen Brown static void
23176fb3143bSLen Brown dump_config_tdp(void)
23186fb3143bSLen Brown {
23196fb3143bSLen Brown 	unsigned long long msr;
23206fb3143bSLen Brown 
23216fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2322b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2323685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
23246fb3143bSLen Brown 
23256fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2326b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
23276fb3143bSLen Brown 	if (msr) {
2328685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2329685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2330685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2331685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
23326fb3143bSLen Brown 	}
2333b7d8c148SLen Brown 	fprintf(outf, ")\n");
23346fb3143bSLen Brown 
23356fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2336b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
23376fb3143bSLen Brown 	if (msr) {
2338685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2339685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2340685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2341685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
23426fb3143bSLen Brown 	}
2343b7d8c148SLen Brown 	fprintf(outf, ")\n");
23446fb3143bSLen Brown 
23456fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2346b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
23476fb3143bSLen Brown 	if ((msr) & 0x3)
2348b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2349b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2350b7d8c148SLen Brown 	fprintf(outf, ")\n");
23516fb3143bSLen Brown 
23526fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2353b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2354685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2355b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2356b7d8c148SLen Brown 	fprintf(outf, ")\n");
23576fb3143bSLen Brown }
23585a63426eSLen Brown 
23595a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
23605a63426eSLen Brown 
23615a63426eSLen Brown void print_irtl(void)
23625a63426eSLen Brown {
23635a63426eSLen Brown 	unsigned long long msr;
23645a63426eSLen Brown 
23655a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
23665a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
23675a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23685a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23695a63426eSLen Brown 
23705a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
23715a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
23725a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23735a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23745a63426eSLen Brown 
23755a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
23765a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
23775a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23785a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23795a63426eSLen Brown 
23805a63426eSLen Brown 	if (!do_irtl_hsw)
23815a63426eSLen Brown 		return;
23825a63426eSLen Brown 
23835a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
23845a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
23855a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23865a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23875a63426eSLen Brown 
23885a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
23895a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
23905a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23915a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23925a63426eSLen Brown 
23935a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
23945a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
23955a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
23965a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
23975a63426eSLen Brown 
23985a63426eSLen Brown }
239936229897SLen Brown void free_fd_percpu(void)
240036229897SLen Brown {
240136229897SLen Brown 	int i;
240236229897SLen Brown 
240301a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
240436229897SLen Brown 		if (fd_percpu[i] != 0)
240536229897SLen Brown 			close(fd_percpu[i]);
240636229897SLen Brown 	}
240736229897SLen Brown 
240836229897SLen Brown 	free(fd_percpu);
24096fb3143bSLen Brown }
24106fb3143bSLen Brown 
2411c98d5d94SLen Brown void free_all_buffers(void)
2412103a8feaSLen Brown {
24130e2d8f05SLen Brown 	int i;
24140e2d8f05SLen Brown 
2415c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2416c98d5d94SLen Brown 	cpu_present_set = NULL;
241736229897SLen Brown 	cpu_present_setsize = 0;
2418103a8feaSLen Brown 
2419c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2420c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2421c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2422103a8feaSLen Brown 
2423c98d5d94SLen Brown 	free(thread_even);
2424c98d5d94SLen Brown 	free(core_even);
2425c98d5d94SLen Brown 	free(package_even);
2426103a8feaSLen Brown 
2427c98d5d94SLen Brown 	thread_even = NULL;
2428c98d5d94SLen Brown 	core_even = NULL;
2429c98d5d94SLen Brown 	package_even = NULL;
2430103a8feaSLen Brown 
2431c98d5d94SLen Brown 	free(thread_odd);
2432c98d5d94SLen Brown 	free(core_odd);
2433c98d5d94SLen Brown 	free(package_odd);
2434103a8feaSLen Brown 
2435c98d5d94SLen Brown 	thread_odd = NULL;
2436c98d5d94SLen Brown 	core_odd = NULL;
2437c98d5d94SLen Brown 	package_odd = NULL;
2438103a8feaSLen Brown 
2439c98d5d94SLen Brown 	free(output_buffer);
2440c98d5d94SLen Brown 	output_buffer = NULL;
2441c98d5d94SLen Brown 	outp = NULL;
244236229897SLen Brown 
244336229897SLen Brown 	free_fd_percpu();
2444562a2d37SLen Brown 
2445562a2d37SLen Brown 	free(irq_column_2_cpu);
2446562a2d37SLen Brown 	free(irqs_per_cpu);
24470e2d8f05SLen Brown 
24480e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
24490e2d8f05SLen Brown 		if (cpus[i].put_ids)
24500e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
24510e2d8f05SLen Brown 	}
24520e2d8f05SLen Brown 	free(cpus);
2453103a8feaSLen Brown }
2454103a8feaSLen Brown 
245557a42a34SJosh Triplett 
245657a42a34SJosh Triplett /*
245795aebc44SJosh Triplett  * Parse a file containing a single int.
245895aebc44SJosh Triplett  */
245995aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
246095aebc44SJosh Triplett {
246195aebc44SJosh Triplett 	va_list args;
246295aebc44SJosh Triplett 	char path[PATH_MAX];
246395aebc44SJosh Triplett 	FILE *filep;
246495aebc44SJosh Triplett 	int value;
246595aebc44SJosh Triplett 
246695aebc44SJosh Triplett 	va_start(args, fmt);
246795aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
246895aebc44SJosh Triplett 	va_end(args);
246957a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2470b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2471b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
247295aebc44SJosh Triplett 	fclose(filep);
247395aebc44SJosh Triplett 	return value;
247495aebc44SJosh Triplett }
247595aebc44SJosh Triplett 
247695aebc44SJosh Triplett /*
2477c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2478c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2479c98d5d94SLen Brown  */
2480c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2481c98d5d94SLen Brown {
248295aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2483103a8feaSLen Brown }
2484103a8feaSLen Brown 
2485103a8feaSLen Brown int get_physical_package_id(int cpu)
2486103a8feaSLen Brown {
248795aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2488103a8feaSLen Brown }
2489103a8feaSLen Brown 
2490103a8feaSLen Brown int get_core_id(int cpu)
2491103a8feaSLen Brown {
249295aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2493103a8feaSLen Brown }
2494103a8feaSLen Brown 
2495ef605741SPrarit Bhargava void set_node_data(void)
2496ef605741SPrarit Bhargava {
24972ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
24982ffbb224SPrarit Bhargava 	int cpu_count;
2499ef605741SPrarit Bhargava 
25002ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
25012ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
25022ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2503ef605741SPrarit Bhargava 
25042ffbb224SPrarit Bhargava 	cpu_count = 0;
25052ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
25062ffbb224SPrarit Bhargava 		lnode = 0;
25072ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
25082ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2509ef605741SPrarit Bhargava 				continue;
25102ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
25112ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
25122ffbb224SPrarit Bhargava 				continue;
25132ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2514ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
25152ffbb224SPrarit Bhargava 			cpu_count++;
25162ffbb224SPrarit Bhargava 			/*
25172ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
25182ffbb224SPrarit Bhargava 			 * the logical_node_id
25192ffbb224SPrarit Bhargava 			 */
25202ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
25212ffbb224SPrarit Bhargava 				if ((cpus[cpux].physical_package_id == pkg) &&
25222ffbb224SPrarit Bhargava 				   (cpus[cpux].physical_node_id == node)) {
25232ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
25242ffbb224SPrarit Bhargava 					cpu_count++;
2525ef605741SPrarit Bhargava 				}
25262ffbb224SPrarit Bhargava 			}
25272ffbb224SPrarit Bhargava 			lnode++;
25282ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
25292ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
25302ffbb224SPrarit Bhargava 		}
25312ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
25322ffbb224SPrarit Bhargava 			break;
25332ffbb224SPrarit Bhargava 	}
2534ef605741SPrarit Bhargava }
2535ef605741SPrarit Bhargava 
2536ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2537c98d5d94SLen Brown {
2538c98d5d94SLen Brown 	char path[80];
2539c98d5d94SLen Brown 	FILE *filep;
25400e2d8f05SLen Brown 	int i;
25410e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2542c98d5d94SLen Brown 
25430e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
25440e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
25450e2d8f05SLen Brown 			cpu, i);
25460e2d8f05SLen Brown 		filep = fopen(path, "r");
25470e2d8f05SLen Brown 		if (!filep)
25480e2d8f05SLen Brown 			continue;
25490e2d8f05SLen Brown 		fclose(filep);
25500e2d8f05SLen Brown 		return i;
25510e2d8f05SLen Brown 	}
25520e2d8f05SLen Brown 	return -1;
2553e275b388SDasaratharaman Chandramouli }
2554c98d5d94SLen Brown 
25550e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
25560e2d8f05SLen Brown {
25570e2d8f05SLen Brown 	char path[80], character;
25580e2d8f05SLen Brown 	FILE *filep;
25590e2d8f05SLen Brown 	unsigned long map;
25608cb48b32SPrarit Bhargava 	int so, shift, sib_core;
25610e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
25620e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
25630e2d8f05SLen Brown 	size_t size;
25648cb48b32SPrarit Bhargava 	int thread_id = 0;
25650e2d8f05SLen Brown 
25660e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
25678cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
25688cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
25690e2d8f05SLen Brown 	if (!thiscpu->put_ids)
25700e2d8f05SLen Brown 		return -1;
25710e2d8f05SLen Brown 
25720e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
25730e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
25740e2d8f05SLen Brown 
25750e2d8f05SLen Brown 	sprintf(path,
25760e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
25770e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
25780e2d8f05SLen Brown 	do {
25790e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
25800e2d8f05SLen Brown 		fscanf(filep, "%lx%c", &map, &character);
25810e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
25820e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
25838cb48b32SPrarit Bhargava 				so = shift + offset;
25848cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
25858cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
25868cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
25878cb48b32SPrarit Bhargava 					if ((so != cpu) &&
25888cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
25898cb48b32SPrarit Bhargava 						cpus[so].thread_id =
25908cb48b32SPrarit Bhargava 								    thread_id++;
25918cb48b32SPrarit Bhargava 				}
25920e2d8f05SLen Brown 			}
25930e2d8f05SLen Brown 		}
25940e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2595c98d5d94SLen Brown 	fclose(filep);
25960e2d8f05SLen Brown 
25970e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2598c98d5d94SLen Brown }
2599c98d5d94SLen Brown 
2600c98d5d94SLen Brown /*
2601c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2602c98d5d94SLen Brown  * skip non-present cpus
2603103a8feaSLen Brown  */
2604103a8feaSLen Brown 
2605c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2606c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2607c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2608c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2609c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2610c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2611c98d5d94SLen Brown {
261240f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2613c98d5d94SLen Brown 
2614c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
261540f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
261640f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
261740f5cfe7SPrarit Bhargava 			     ++core_no) {
2618c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
261970a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2620c98d5d94SLen Brown 					struct thread_data *t, *t2;
2621c98d5d94SLen Brown 					struct core_data *c, *c2;
2622c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2623c98d5d94SLen Brown 
262440f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
262540f5cfe7SPrarit Bhargava 						       core_no, node_no,
262640f5cfe7SPrarit Bhargava 						       pkg_no);
2627c98d5d94SLen Brown 
2628c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2629c98d5d94SLen Brown 						continue;
2630c98d5d94SLen Brown 
263140f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
263240f5cfe7SPrarit Bhargava 							core_no, node_no,
263340f5cfe7SPrarit Bhargava 							pkg_no);
2634c98d5d94SLen Brown 
263540f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
263640f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
263740f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
263840f5cfe7SPrarit Bhargava 						      node_no,
263940f5cfe7SPrarit Bhargava 						      pkg_no);
2640c98d5d94SLen Brown 
2641c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2642c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2643c98d5d94SLen Brown 
2644c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2645c98d5d94SLen Brown 					if (retval)
2646c98d5d94SLen Brown 						return retval;
2647c98d5d94SLen Brown 				}
2648c98d5d94SLen Brown 			}
2649c98d5d94SLen Brown 		}
265040f5cfe7SPrarit Bhargava 	}
2651c98d5d94SLen Brown 	return 0;
2652c98d5d94SLen Brown }
2653c98d5d94SLen Brown 
2654c98d5d94SLen Brown /*
2655c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2656c98d5d94SLen Brown  * return max_cpu number
2657c98d5d94SLen Brown  */
2658c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2659103a8feaSLen Brown {
2660103a8feaSLen Brown 	FILE *fp;
2661c98d5d94SLen Brown 	int cpu_num;
2662103a8feaSLen Brown 	int retval;
2663103a8feaSLen Brown 
266457a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2665103a8feaSLen Brown 
2666103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2667b2c95d90SJosh Triplett 	if (retval != 0)
2668b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2669103a8feaSLen Brown 
2670c98d5d94SLen Brown 	while (1) {
2671c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2672103a8feaSLen Brown 		if (retval != 1)
2673103a8feaSLen Brown 			break;
2674103a8feaSLen Brown 
2675c98d5d94SLen Brown 		retval = func(cpu_num);
2676c98d5d94SLen Brown 		if (retval) {
2677c98d5d94SLen Brown 			fclose(fp);
2678c98d5d94SLen Brown 			return(retval);
2679c98d5d94SLen Brown 		}
2680103a8feaSLen Brown 	}
2681103a8feaSLen Brown 	fclose(fp);
2682c98d5d94SLen Brown 	return 0;
2683103a8feaSLen Brown }
2684103a8feaSLen Brown 
2685103a8feaSLen Brown void re_initialize(void)
2686103a8feaSLen Brown {
2687c98d5d94SLen Brown 	free_all_buffers();
2688c98d5d94SLen Brown 	setup_all_buffers();
2689c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2690103a8feaSLen Brown }
2691103a8feaSLen Brown 
2692843c5791SPrarit Bhargava void set_max_cpu_num(void)
2693843c5791SPrarit Bhargava {
2694843c5791SPrarit Bhargava 	FILE *filep;
2695843c5791SPrarit Bhargava 	unsigned long dummy;
2696843c5791SPrarit Bhargava 
2697843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2698843c5791SPrarit Bhargava 	filep = fopen_or_die(
2699843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2700843c5791SPrarit Bhargava 			"r");
2701843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
27020e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2703843c5791SPrarit Bhargava 	fclose(filep);
2704843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2705843c5791SPrarit Bhargava }
2706c98d5d94SLen Brown 
2707103a8feaSLen Brown /*
2708c98d5d94SLen Brown  * count_cpus()
2709c98d5d94SLen Brown  * remember the last one seen, it will be the max
2710103a8feaSLen Brown  */
2711c98d5d94SLen Brown int count_cpus(int cpu)
2712103a8feaSLen Brown {
2713843c5791SPrarit Bhargava 	topo.num_cpus++;
2714c98d5d94SLen Brown 	return 0;
2715103a8feaSLen Brown }
2716c98d5d94SLen Brown int mark_cpu_present(int cpu)
2717c98d5d94SLen Brown {
2718c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
271915aaa346SLen Brown 	return 0;
2720103a8feaSLen Brown }
2721103a8feaSLen Brown 
27228cb48b32SPrarit Bhargava int init_thread_id(int cpu)
27238cb48b32SPrarit Bhargava {
27248cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
27258cb48b32SPrarit Bhargava 	return 0;
27268cb48b32SPrarit Bhargava }
27278cb48b32SPrarit Bhargava 
2728562a2d37SLen Brown /*
2729562a2d37SLen Brown  * snapshot_proc_interrupts()
2730562a2d37SLen Brown  *
2731562a2d37SLen Brown  * read and record summary of /proc/interrupts
2732562a2d37SLen Brown  *
2733562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2734562a2d37SLen Brown  */
2735562a2d37SLen Brown int snapshot_proc_interrupts(void)
2736562a2d37SLen Brown {
2737562a2d37SLen Brown 	static FILE *fp;
2738562a2d37SLen Brown 	int column, retval;
2739562a2d37SLen Brown 
2740562a2d37SLen Brown 	if (fp == NULL)
2741562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2742562a2d37SLen Brown 	else
2743562a2d37SLen Brown 		rewind(fp);
2744562a2d37SLen Brown 
2745562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2746562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2747562a2d37SLen Brown 		int cpu_number;
2748562a2d37SLen Brown 
2749562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2750562a2d37SLen Brown 		if (retval != 1)
2751562a2d37SLen Brown 			break;
2752562a2d37SLen Brown 
2753562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2754562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2755562a2d37SLen Brown 			return 1;
2756562a2d37SLen Brown 		}
2757562a2d37SLen Brown 
2758562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2759562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2760562a2d37SLen Brown 	}
2761562a2d37SLen Brown 
2762562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2763562a2d37SLen Brown 	while (1) {
2764562a2d37SLen Brown 		int column;
2765562a2d37SLen Brown 		char buf[64];
2766562a2d37SLen Brown 
2767562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2768562a2d37SLen Brown 		if (retval != 1)
2769562a2d37SLen Brown 			break;
2770562a2d37SLen Brown 
2771562a2d37SLen Brown 		/* read the count per cpu */
2772562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2773562a2d37SLen Brown 
2774562a2d37SLen Brown 			int cpu_number, irq_count;
2775562a2d37SLen Brown 
2776562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2777562a2d37SLen Brown 			if (retval != 1)
2778562a2d37SLen Brown 				break;
2779562a2d37SLen Brown 
2780562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2781562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2782562a2d37SLen Brown 
2783562a2d37SLen Brown 		}
2784562a2d37SLen Brown 
2785562a2d37SLen Brown 		while (getc(fp) != '\n')
2786562a2d37SLen Brown 			;	/* flush interrupt description */
2787562a2d37SLen Brown 
2788562a2d37SLen Brown 	}
2789562a2d37SLen Brown 	return 0;
2790562a2d37SLen Brown }
279127d47356SLen Brown /*
2792fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2793fdf676e5SLen Brown  *
2794fdf676e5SLen Brown  * record snapshot of
2795fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2796fdf676e5SLen Brown  *
2797fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2798fdf676e5SLen Brown  */
2799fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2800fdf676e5SLen Brown {
2801fdf676e5SLen Brown 	FILE *fp;
2802fdf676e5SLen Brown 	int retval;
2803fdf676e5SLen Brown 
2804fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2805fdf676e5SLen Brown 
2806fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2807fdf676e5SLen Brown 	if (retval != 1)
2808fdf676e5SLen Brown 		err(1, "GFX rc6");
2809fdf676e5SLen Brown 
2810fdf676e5SLen Brown 	fclose(fp);
2811fdf676e5SLen Brown 
2812fdf676e5SLen Brown 	return 0;
2813fdf676e5SLen Brown }
2814fdf676e5SLen Brown /*
281527d47356SLen Brown  * snapshot_gfx_mhz()
281627d47356SLen Brown  *
281727d47356SLen Brown  * record snapshot of
281827d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
281927d47356SLen Brown  *
282027d47356SLen Brown  * return 1 if config change requires a restart, else return 0
282127d47356SLen Brown  */
282227d47356SLen Brown int snapshot_gfx_mhz(void)
282327d47356SLen Brown {
282427d47356SLen Brown 	static FILE *fp;
282527d47356SLen Brown 	int retval;
282627d47356SLen Brown 
282727d47356SLen Brown 	if (fp == NULL)
282827d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
282922048c54SLen Brown 	else {
283027d47356SLen Brown 		rewind(fp);
283122048c54SLen Brown 		fflush(fp);
283222048c54SLen Brown 	}
283327d47356SLen Brown 
283427d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
283527d47356SLen Brown 	if (retval != 1)
283627d47356SLen Brown 		err(1, "GFX MHz");
283727d47356SLen Brown 
283827d47356SLen Brown 	return 0;
283927d47356SLen Brown }
2840562a2d37SLen Brown 
2841562a2d37SLen Brown /*
2842be0e54c4SLen Brown  * snapshot_cpu_lpi()
2843be0e54c4SLen Brown  *
2844be0e54c4SLen Brown  * record snapshot of
2845be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2846be0e54c4SLen Brown  *
2847be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2848be0e54c4SLen Brown  */
2849be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2850be0e54c4SLen Brown {
2851be0e54c4SLen Brown 	FILE *fp;
2852be0e54c4SLen Brown 	int retval;
2853be0e54c4SLen Brown 
2854be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2855be0e54c4SLen Brown 
2856be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
2857be0e54c4SLen Brown 	if (retval != 1)
2858be0e54c4SLen Brown 		err(1, "CPU LPI");
2859be0e54c4SLen Brown 
2860be0e54c4SLen Brown 	fclose(fp);
2861be0e54c4SLen Brown 
2862be0e54c4SLen Brown 	return 0;
2863be0e54c4SLen Brown }
2864be0e54c4SLen Brown /*
2865be0e54c4SLen Brown  * snapshot_sys_lpi()
2866be0e54c4SLen Brown  *
2867be0e54c4SLen Brown  * record snapshot of
2868be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2869be0e54c4SLen Brown  *
2870be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2871be0e54c4SLen Brown  */
2872be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2873be0e54c4SLen Brown {
2874be0e54c4SLen Brown 	FILE *fp;
2875be0e54c4SLen Brown 	int retval;
2876be0e54c4SLen Brown 
2877be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2878be0e54c4SLen Brown 
2879be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
2880be0e54c4SLen Brown 	if (retval != 1)
2881be0e54c4SLen Brown 		err(1, "SYS LPI");
2882be0e54c4SLen Brown 
2883be0e54c4SLen Brown 	fclose(fp);
2884be0e54c4SLen Brown 
2885be0e54c4SLen Brown 	return 0;
2886be0e54c4SLen Brown }
2887be0e54c4SLen Brown /*
2888562a2d37SLen Brown  * snapshot /proc and /sys files
2889562a2d37SLen Brown  *
2890562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2891562a2d37SLen Brown  */
2892562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2893562a2d37SLen Brown {
2894218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2895562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2896562a2d37SLen Brown 			return 1;
2897562a2d37SLen Brown 
2898812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2899fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2900fdf676e5SLen Brown 
2901812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
290227d47356SLen Brown 		snapshot_gfx_mhz();
290327d47356SLen Brown 
2904be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2905be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2906be0e54c4SLen Brown 
2907be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2908be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2909be0e54c4SLen Brown 
2910562a2d37SLen Brown 	return 0;
2911562a2d37SLen Brown }
2912562a2d37SLen Brown 
29138aa2ed0bSLen Brown int exit_requested;
29148aa2ed0bSLen Brown 
29158aa2ed0bSLen Brown static void signal_handler (int signal)
29168aa2ed0bSLen Brown {
29178aa2ed0bSLen Brown 	switch (signal) {
29188aa2ed0bSLen Brown 	case SIGINT:
29198aa2ed0bSLen Brown 		exit_requested = 1;
29208aa2ed0bSLen Brown 		if (debug)
29218aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
29228aa2ed0bSLen Brown 		break;
292307211960SLen Brown 	case SIGUSR1:
292407211960SLen Brown 		if (debug > 1)
292507211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
292607211960SLen Brown 		break;
29278aa2ed0bSLen Brown 	}
2928b9ad8ee0SLen Brown 	/* make sure this manually-invoked interval is at least 1ms long */
2929b9ad8ee0SLen Brown 	nanosleep(&one_msec, NULL);
29308aa2ed0bSLen Brown }
29318aa2ed0bSLen Brown 
29328aa2ed0bSLen Brown void setup_signal_handler(void)
29338aa2ed0bSLen Brown {
29348aa2ed0bSLen Brown 	struct sigaction sa;
29358aa2ed0bSLen Brown 
29368aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
29378aa2ed0bSLen Brown 
29388aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
29398aa2ed0bSLen Brown 
29408aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
29418aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
294207211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
294307211960SLen Brown 		err(1, "sigaction SIGUSR1");
29448aa2ed0bSLen Brown }
2945b9ad8ee0SLen Brown 
294647936f94SArtem Bityutskiy void do_sleep(void)
2947b9ad8ee0SLen Brown {
2948b9ad8ee0SLen Brown 	struct timeval select_timeout;
2949b9ad8ee0SLen Brown 	fd_set readfds;
2950b9ad8ee0SLen Brown 	int retval;
2951b9ad8ee0SLen Brown 
2952b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
2953b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
2954b9ad8ee0SLen Brown 
295547936f94SArtem Bityutskiy 	if (!isatty(fileno(stdin))) {
295647936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
295747936f94SArtem Bityutskiy 		return;
295847936f94SArtem Bityutskiy 	}
2959b9ad8ee0SLen Brown 
296047936f94SArtem Bityutskiy 	select_timeout = interval_tv;
2961b9ad8ee0SLen Brown 	retval = select(1, &readfds, NULL, NULL, &select_timeout);
2962b9ad8ee0SLen Brown 
2963b9ad8ee0SLen Brown 	if (retval == 1) {
2964b9ad8ee0SLen Brown 		switch (getc(stdin)) {
2965b9ad8ee0SLen Brown 		case 'q':
2966b9ad8ee0SLen Brown 			exit_requested = 1;
2967b9ad8ee0SLen Brown 			break;
2968b9ad8ee0SLen Brown 		}
2969b9ad8ee0SLen Brown 		/* make sure this manually-invoked interval is at least 1ms long */
2970b9ad8ee0SLen Brown 		nanosleep(&one_msec, NULL);
2971b9ad8ee0SLen Brown 	}
2972b9ad8ee0SLen Brown }
297347936f94SArtem Bityutskiy 
29744c2122d4SLen Brown 
2975103a8feaSLen Brown void turbostat_loop()
2976103a8feaSLen Brown {
2977c98d5d94SLen Brown 	int retval;
2978e52966c0SLen Brown 	int restarted = 0;
2979023fe0acSChen Yu 	int done_iters = 0;
2980c98d5d94SLen Brown 
29818aa2ed0bSLen Brown 	setup_signal_handler();
29828aa2ed0bSLen Brown 
2983103a8feaSLen Brown restart:
2984e52966c0SLen Brown 	restarted++;
2985e52966c0SLen Brown 
2986562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2987c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
29884c2122d4SLen Brown 	first_counter_read = 0;
2989d91bb17cSLen Brown 	if (retval < -1) {
2990d91bb17cSLen Brown 		exit(retval);
2991d91bb17cSLen Brown 	} else if (retval == -1) {
2992e52966c0SLen Brown 		if (restarted > 1) {
2993e52966c0SLen Brown 			exit(retval);
2994e52966c0SLen Brown 		}
2995c98d5d94SLen Brown 		re_initialize();
2996c98d5d94SLen Brown 		goto restart;
2997c98d5d94SLen Brown 	}
2998e52966c0SLen Brown 	restarted = 0;
2999023fe0acSChen Yu 	done_iters = 0;
3000103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3001103a8feaSLen Brown 
3002103a8feaSLen Brown 	while (1) {
3003c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3004103a8feaSLen Brown 			re_initialize();
3005103a8feaSLen Brown 			goto restart;
3006103a8feaSLen Brown 		}
3007b9ad8ee0SLen Brown 		do_sleep();
3008562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3009562a2d37SLen Brown 			goto restart;
3010c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3011d91bb17cSLen Brown 		if (retval < -1) {
3012d91bb17cSLen Brown 			exit(retval);
3013d91bb17cSLen Brown 		} else if (retval == -1) {
301415aaa346SLen Brown 			re_initialize();
301515aaa346SLen Brown 			goto restart;
301615aaa346SLen Brown 		}
3017103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3018103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3019ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3020ba3dec99SLen Brown 			re_initialize();
3021ba3dec99SLen Brown 			goto restart;
3022ba3dec99SLen Brown 		}
3023c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3024c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3025b7d8c148SLen Brown 		flush_output_stdout();
30268aa2ed0bSLen Brown 		if (exit_requested)
30278aa2ed0bSLen Brown 			break;
3028023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3029023fe0acSChen Yu 			break;
3030b9ad8ee0SLen Brown 		do_sleep();
3031562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3032562a2d37SLen Brown 			goto restart;
3033c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3034d91bb17cSLen Brown 		if (retval < -1) {
3035d91bb17cSLen Brown 			exit(retval);
3036d91bb17cSLen Brown 		} else if (retval == -1) {
3037103a8feaSLen Brown 			re_initialize();
3038103a8feaSLen Brown 			goto restart;
3039103a8feaSLen Brown 		}
3040103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3041103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3042ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3043ba3dec99SLen Brown 			re_initialize();
3044ba3dec99SLen Brown 			goto restart;
3045ba3dec99SLen Brown 		}
3046c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3047c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3048b7d8c148SLen Brown 		flush_output_stdout();
30498aa2ed0bSLen Brown 		if (exit_requested)
30508aa2ed0bSLen Brown 			break;
3051023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3052023fe0acSChen Yu 			break;
3053103a8feaSLen Brown 	}
3054103a8feaSLen Brown }
3055103a8feaSLen Brown 
3056103a8feaSLen Brown void check_dev_msr()
3057103a8feaSLen Brown {
3058103a8feaSLen Brown 	struct stat sb;
30597ce7d5deSPrarit Bhargava 	char pathname[32];
3060103a8feaSLen Brown 
30617ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
30627ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3063a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3064d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3065103a8feaSLen Brown }
3066103a8feaSLen Brown 
306798481e79SLen Brown void check_permissions()
3068103a8feaSLen Brown {
306998481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
307098481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
307198481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
307298481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
307398481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
307498481e79SLen Brown 	int do_exit = 0;
30757ce7d5deSPrarit Bhargava 	char pathname[32];
307698481e79SLen Brown 
307798481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
307898481e79SLen Brown 	cap_header->pid = getpid();
307998481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
308098481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
308198481e79SLen Brown 		err(-6, "capget(2) failed");
308298481e79SLen Brown 
308398481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
308498481e79SLen Brown 		do_exit++;
308598481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
308698481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
308798481e79SLen Brown 	}
308898481e79SLen Brown 
308998481e79SLen Brown 	/* test file permissions */
30907ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
30917ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
309298481e79SLen Brown 		do_exit++;
309398481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
309498481e79SLen Brown 	}
309598481e79SLen Brown 
309698481e79SLen Brown 	/* if all else fails, thell them to be root */
309798481e79SLen Brown 	if (do_exit)
3098b2c95d90SJosh Triplett 		if (getuid() != 0)
3099d7899447SLen Brown 			warnx("... or simply run as root");
310098481e79SLen Brown 
310198481e79SLen Brown 	if (do_exit)
310298481e79SLen Brown 		exit(-6);
3103103a8feaSLen Brown }
3104103a8feaSLen Brown 
3105d7899447SLen Brown /*
3106d7899447SLen Brown  * NHM adds support for additional MSRs:
3107d7899447SLen Brown  *
3108d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3109d7899447SLen Brown  *
3110ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
31111df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3112d7899447SLen Brown  *
3113cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3114cf4cbe53SLen Brown  *
3115d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3116d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3117d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3118d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3119d7899447SLen Brown  *
3120ee7e38e3SLen Brown  * Side effect:
31211df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
312233148d67SLen Brown  * sets has_misc_feature_control
3123d7899447SLen Brown  */
3124ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3125103a8feaSLen Brown {
3126ee7e38e3SLen Brown 	unsigned long long msr;
312721ed5574SLen Brown 	unsigned int base_ratio;
3128ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3129ee7e38e3SLen Brown 
3130103a8feaSLen Brown 	if (!genuine_intel)
3131103a8feaSLen Brown 		return 0;
3132103a8feaSLen Brown 
3133103a8feaSLen Brown 	if (family != 6)
3134103a8feaSLen Brown 		return 0;
3135103a8feaSLen Brown 
313621ed5574SLen Brown 	bclk = discover_bclk(family, model);
313721ed5574SLen Brown 
3138103a8feaSLen Brown 	switch (model) {
3139869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
3140869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3141103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
3142869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
3143869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
3144869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3145869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
3146ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3147ee7e38e3SLen Brown 		break;
3148869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3149869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3150869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3151869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3152ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
315333148d67SLen Brown 		has_misc_feature_control = 1;
3154ee7e38e3SLen Brown 		break;
3155869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3156869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3157869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3158869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3159869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3160869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3161869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3162869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3163869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3164869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3165869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3166869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3167997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3168ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
316933148d67SLen Brown 		has_misc_feature_control = 1;
3170ee7e38e3SLen Brown 		break;
3171d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3172d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
317333148d67SLen Brown 		has_misc_feature_control = 1;
3174d8ebb442SLen Brown 		break;
3175f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3176cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3177f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
3178ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3179ee7e38e3SLen Brown 		break;
3180869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3181ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3182cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3183ee7e38e3SLen Brown 		break;
3184869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3185005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3186ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3187ee7e38e3SLen Brown 		break;
3188869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3189f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3190f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
3191e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
3192e4085d54SLen Brown 		break;
3193103a8feaSLen Brown 	default:
3194103a8feaSLen Brown 		return 0;
3195103a8feaSLen Brown 	}
31961df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3197e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3198ee7e38e3SLen Brown 
3199ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
320021ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
320121ed5574SLen Brown 
320221ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
320321ed5574SLen Brown 	has_base_hz = 1;
3204ee7e38e3SLen Brown 	return 1;
3205103a8feaSLen Brown }
32060f7887c4SLen Brown /*
3207495c7654SLen Brown  * SLV client has support for unique MSRs:
32080f7887c4SLen Brown  *
32090f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
32100f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
32110f7887c4SLen Brown  */
32120f7887c4SLen Brown 
32130f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
32140f7887c4SLen Brown {
32150f7887c4SLen Brown 	if (!genuine_intel)
32160f7887c4SLen Brown 		return 0;
32170f7887c4SLen Brown 
32180f7887c4SLen Brown 	switch (model) {
3219f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3220f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3221f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
32220f7887c4SLen Brown 		return 1;
32230f7887c4SLen Brown 	}
32240f7887c4SLen Brown 	return 0;
32250f7887c4SLen Brown }
32267170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
32277170a374SLen Brown {
32287170a374SLen Brown 
32297170a374SLen Brown 	if (!genuine_intel)
32307170a374SLen Brown 		return 0;
32317170a374SLen Brown 
32327170a374SLen Brown 	switch (model) {
3233f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:
32347170a374SLen Brown 		return 1;
32357170a374SLen Brown 	}
32367170a374SLen Brown 	return 0;
32377170a374SLen Brown }
3238ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3239ade0ebacSLen Brown {
3240ade0ebacSLen Brown 
3241ade0ebacSLen Brown 	if (!genuine_intel)
3242ade0ebacSLen Brown 		return 0;
3243ade0ebacSLen Brown 
3244ade0ebacSLen Brown 	switch (model) {
3245ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3246ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
3247ade0ebacSLen Brown 		return 1;
3248ade0ebacSLen Brown 	}
3249ade0ebacSLen Brown 	return 0;
3250ade0ebacSLen Brown }
325134c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
325234c76197SLen Brown {
325334c76197SLen Brown 
325434c76197SLen Brown 	if (!genuine_intel)
325534c76197SLen Brown 		return 0;
325634c76197SLen Brown 
325734c76197SLen Brown 	switch (model) {
325834c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
325934c76197SLen Brown 		return 1;
326034c76197SLen Brown 	}
326134c76197SLen Brown 	return 0;
326234c76197SLen Brown }
32630f7887c4SLen Brown 
326431e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3265d7899447SLen Brown {
32660f7887c4SLen Brown 	if (has_slv_msrs(family, model))
32670f7887c4SLen Brown 		return 0;
32680f7887c4SLen Brown 
3269d7899447SLen Brown 	switch (model) {
3270d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3271869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3272869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
3273869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3274005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3275d7899447SLen Brown 		return 0;
3276d7899447SLen Brown 	default:
3277d7899447SLen Brown 		return 1;
3278d7899447SLen Brown 	}
3279d7899447SLen Brown }
32800f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
32810f7887c4SLen Brown {
32820f7887c4SLen Brown 	if (has_slv_msrs(family, model))
32830f7887c4SLen Brown 		return 1;
32840f7887c4SLen Brown 
32850f7887c4SLen Brown 	return 0;
32860f7887c4SLen Brown }
32876574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
32886574a5d5SLen Brown {
32896574a5d5SLen Brown 	if (!genuine_intel)
32906574a5d5SLen Brown 		return 0;
32916574a5d5SLen Brown 
32926574a5d5SLen Brown 	if (family != 6)
32936574a5d5SLen Brown 		return 0;
32946574a5d5SLen Brown 
32956574a5d5SLen Brown 	switch (model) {
3296869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3297869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
32986574a5d5SLen Brown 		return 1;
32996574a5d5SLen Brown 	default:
33006574a5d5SLen Brown 		return 0;
33016574a5d5SLen Brown 	}
33026574a5d5SLen Brown }
3303fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3304fcd17211SLen Brown {
3305fcd17211SLen Brown 	if (!genuine_intel)
3306fcd17211SLen Brown 		return 0;
3307fcd17211SLen Brown 
3308fcd17211SLen Brown 	if (family != 6)
3309fcd17211SLen Brown 		return 0;
3310fcd17211SLen Brown 
3311fcd17211SLen Brown 	switch (model) {
3312869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3313fcd17211SLen Brown 		return 1;
3314fcd17211SLen Brown 	default:
3315fcd17211SLen Brown 		return 0;
3316fcd17211SLen Brown 	}
3317fcd17211SLen Brown }
3318fcd17211SLen Brown 
3319fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3320fb5d4327SDasaratharaman Chandramouli {
3321fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3322fb5d4327SDasaratharaman Chandramouli 		return 0;
3323fb5d4327SDasaratharaman Chandramouli 
3324fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3325fb5d4327SDasaratharaman Chandramouli 		return 0;
3326fb5d4327SDasaratharaman Chandramouli 
3327fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3328869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3329005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3330fb5d4327SDasaratharaman Chandramouli 		return 1;
3331fb5d4327SDasaratharaman Chandramouli 	default:
3332fb5d4327SDasaratharaman Chandramouli 		return 0;
3333fb5d4327SDasaratharaman Chandramouli 	}
3334fb5d4327SDasaratharaman Chandramouli }
333531e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
333631e07522SLen Brown {
333731e07522SLen Brown 	if (!genuine_intel)
333831e07522SLen Brown 		return 0;
333931e07522SLen Brown 
334031e07522SLen Brown 	if (family != 6)
334131e07522SLen Brown 		return 0;
334231e07522SLen Brown 
334331e07522SLen Brown 	switch (model) {
334431e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
334531e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
334631e07522SLen Brown 		return 1;
334731e07522SLen Brown 	default:
334831e07522SLen Brown 		return 0;
334931e07522SLen Brown 	}
335031e07522SLen Brown }
33516fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
33526fb3143bSLen Brown {
33536fb3143bSLen Brown 	if (!genuine_intel)
33546fb3143bSLen Brown 		return 0;
33556fb3143bSLen Brown 
33566fb3143bSLen Brown 	if (family != 6)
33576fb3143bSLen Brown 		return 0;
33586fb3143bSLen Brown 
33596fb3143bSLen Brown 	switch (model) {
3360869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3361869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3362869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3363869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3364869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3365869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3366869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3367869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3368869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3369869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3370869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3371869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3372869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3373997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3374869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
33756fb3143bSLen Brown 
3376869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3377005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
33786fb3143bSLen Brown 		return 1;
33796fb3143bSLen Brown 	default:
33806fb3143bSLen Brown 		return 0;
33816fb3143bSLen Brown 	}
33826fb3143bSLen Brown }
33836fb3143bSLen Brown 
3384fcd17211SLen Brown static void
33851b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3386fcd17211SLen Brown {
3387fcd17211SLen Brown 	if (!do_nhm_platform_info)
3388fcd17211SLen Brown 		return;
3389fcd17211SLen Brown 
3390fcd17211SLen Brown 	dump_nhm_platform_info();
3391fcd17211SLen Brown 
3392fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3393fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3394fcd17211SLen Brown 
3395fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3396fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3397fcd17211SLen Brown 
339831e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
339931e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3400fcd17211SLen Brown 
34010f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
34020f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
34030f7887c4SLen Brown 
3404fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3405fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3406fb5d4327SDasaratharaman Chandramouli 
34076fb3143bSLen Brown 	if (has_config_tdp(family, model))
34086fb3143bSLen Brown 		dump_config_tdp();
34096fb3143bSLen Brown 
3410fcd17211SLen Brown 	dump_nhm_cst_cfg();
3411fcd17211SLen Brown }
3412fcd17211SLen Brown 
341341618e63SLen Brown static void
341441618e63SLen Brown dump_sysfs_cstate_config(void)
341541618e63SLen Brown {
341641618e63SLen Brown 	char path[64];
341741618e63SLen Brown 	char name_buf[16];
341841618e63SLen Brown 	char desc[64];
341941618e63SLen Brown 	FILE *input;
342041618e63SLen Brown 	int state;
342141618e63SLen Brown 	char *sp;
342241618e63SLen Brown 
342341618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
342441618e63SLen Brown 		return;
342541618e63SLen Brown 
342641618e63SLen Brown 	for (state = 0; state < 10; ++state) {
342741618e63SLen Brown 
342841618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
342941618e63SLen Brown 			base_cpu, state);
343041618e63SLen Brown 		input = fopen(path, "r");
343141618e63SLen Brown 		if (input == NULL)
343241618e63SLen Brown 			continue;
343341618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
343441618e63SLen Brown 
343541618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
343641618e63SLen Brown 		sp = strchr(name_buf, '-');
343741618e63SLen Brown 		if (!sp)
343841618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
343941618e63SLen Brown 		*sp = '\0';
344041618e63SLen Brown 
344141618e63SLen Brown 		fclose(input);
344241618e63SLen Brown 
344341618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
344441618e63SLen Brown 			base_cpu, state);
344541618e63SLen Brown 		input = fopen(path, "r");
344641618e63SLen Brown 		if (input == NULL)
344741618e63SLen Brown 			continue;
344841618e63SLen Brown 		fgets(desc, sizeof(desc), input);
344941618e63SLen Brown 
345041618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
345141618e63SLen Brown 		fclose(input);
345241618e63SLen Brown 	}
345341618e63SLen Brown }
34547293fccdSLen Brown static void
34557293fccdSLen Brown dump_sysfs_pstate_config(void)
34567293fccdSLen Brown {
34577293fccdSLen Brown 	char path[64];
34587293fccdSLen Brown 	char driver_buf[64];
34597293fccdSLen Brown 	char governor_buf[64];
34607293fccdSLen Brown 	FILE *input;
34617293fccdSLen Brown 	int turbo;
34627293fccdSLen Brown 
34637293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
34647293fccdSLen Brown 			base_cpu);
34657293fccdSLen Brown 	input = fopen(path, "r");
34667293fccdSLen Brown 	if (input == NULL) {
34677293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
34687293fccdSLen Brown 		return;
34697293fccdSLen Brown 	}
34707293fccdSLen Brown 	fgets(driver_buf, sizeof(driver_buf), input);
34717293fccdSLen Brown 	fclose(input);
34727293fccdSLen Brown 
34737293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
34747293fccdSLen Brown 			base_cpu);
34757293fccdSLen Brown 	input = fopen(path, "r");
34767293fccdSLen Brown 	if (input == NULL) {
34777293fccdSLen Brown 		fprintf(stderr, "NSFOD %s\n", path);
34787293fccdSLen Brown 		return;
34797293fccdSLen Brown 	}
34807293fccdSLen Brown 	fgets(governor_buf, sizeof(governor_buf), input);
34817293fccdSLen Brown 	fclose(input);
34827293fccdSLen Brown 
34837293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
34847293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
34857293fccdSLen Brown 
34867293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
34877293fccdSLen Brown 	input = fopen(path, "r");
34887293fccdSLen Brown 	if (input != NULL) {
34897293fccdSLen Brown 		fscanf(input, "%d", &turbo);
34907293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
34917293fccdSLen Brown 		fclose(input);
34927293fccdSLen Brown 	}
34937293fccdSLen Brown 
34947293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
34957293fccdSLen Brown 	input = fopen(path, "r");
34967293fccdSLen Brown 	if (input != NULL) {
34977293fccdSLen Brown 		fscanf(input, "%d", &turbo);
34987293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
34997293fccdSLen Brown 		fclose(input);
35007293fccdSLen Brown 	}
35017293fccdSLen Brown }
350241618e63SLen Brown 
35036574a5d5SLen Brown 
3504889facbeSLen Brown /*
3505889facbeSLen Brown  * print_epb()
3506889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3507889facbeSLen Brown  */
3508889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3509889facbeSLen Brown {
3510889facbeSLen Brown 	unsigned long long msr;
3511889facbeSLen Brown 	char *epb_string;
3512889facbeSLen Brown 	int cpu;
3513889facbeSLen Brown 
3514889facbeSLen Brown 	if (!has_epb)
3515889facbeSLen Brown 		return 0;
3516889facbeSLen Brown 
3517889facbeSLen Brown 	cpu = t->cpu_id;
3518889facbeSLen Brown 
3519889facbeSLen Brown 	/* EPB is per-package */
3520889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3521889facbeSLen Brown 		return 0;
3522889facbeSLen Brown 
3523889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3524b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3525889facbeSLen Brown 		return -1;
3526889facbeSLen Brown 	}
3527889facbeSLen Brown 
3528889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3529889facbeSLen Brown 		return 0;
3530889facbeSLen Brown 
3531e9be7dd6SLen Brown 	switch (msr & 0xF) {
3532889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3533889facbeSLen Brown 		epb_string = "performance";
3534889facbeSLen Brown 		break;
3535889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3536889facbeSLen Brown 		epb_string = "balanced";
3537889facbeSLen Brown 		break;
3538889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3539889facbeSLen Brown 		epb_string = "powersave";
3540889facbeSLen Brown 		break;
3541889facbeSLen Brown 	default:
3542889facbeSLen Brown 		epb_string = "custom";
3543889facbeSLen Brown 		break;
3544889facbeSLen Brown 	}
3545b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3546889facbeSLen Brown 
3547889facbeSLen Brown 	return 0;
3548889facbeSLen Brown }
35497f5c258eSLen Brown /*
35507f5c258eSLen Brown  * print_hwp()
35517f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
35527f5c258eSLen Brown  */
35537f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
35547f5c258eSLen Brown {
35557f5c258eSLen Brown 	unsigned long long msr;
35567f5c258eSLen Brown 	int cpu;
35577f5c258eSLen Brown 
35587f5c258eSLen Brown 	if (!has_hwp)
35597f5c258eSLen Brown 		return 0;
35607f5c258eSLen Brown 
35617f5c258eSLen Brown 	cpu = t->cpu_id;
35627f5c258eSLen Brown 
35637f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
35647f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
35657f5c258eSLen Brown 		return 0;
35667f5c258eSLen Brown 
35677f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3568b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
35697f5c258eSLen Brown 		return -1;
35707f5c258eSLen Brown 	}
35717f5c258eSLen Brown 
35727f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
35737f5c258eSLen Brown 		return 0;
35747f5c258eSLen Brown 
3575b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
35767f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
35777f5c258eSLen Brown 
35787f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
35797f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
35807f5c258eSLen Brown 		return 0;
35817f5c258eSLen Brown 
35827f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
35837f5c258eSLen Brown 		return 0;
35847f5c258eSLen Brown 
3585b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
35866dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
35877f5c258eSLen Brown 			cpu, msr,
35887f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
35897f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
35907f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
35917f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
35927f5c258eSLen Brown 
35937f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
35947f5c258eSLen Brown 		return 0;
35957f5c258eSLen Brown 
3596b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
35976dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
35987f5c258eSLen Brown 			cpu, msr,
35997f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36007f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36017f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36027f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36037f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
36047f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
36057f5c258eSLen Brown 
36067f5c258eSLen Brown 	if (has_hwp_pkg) {
36077f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
36087f5c258eSLen Brown 			return 0;
36097f5c258eSLen Brown 
3610b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
36116dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
36127f5c258eSLen Brown 			cpu, msr,
36137f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36147f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36157f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36167f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36177f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
36187f5c258eSLen Brown 	}
36197f5c258eSLen Brown 	if (has_hwp_notify) {
36207f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
36217f5c258eSLen Brown 			return 0;
36227f5c258eSLen Brown 
3623b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
36247f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
36257f5c258eSLen Brown 			cpu, msr,
36267f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
36277f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
36287f5c258eSLen Brown 	}
36297f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
36307f5c258eSLen Brown 		return 0;
36317f5c258eSLen Brown 
3632b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
36337f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
36347f5c258eSLen Brown 			cpu, msr,
36357f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
36367f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3637889facbeSLen Brown 
3638889facbeSLen Brown 	return 0;
3639889facbeSLen Brown }
3640889facbeSLen Brown 
36413a9a941dSLen Brown /*
36423a9a941dSLen Brown  * print_perf_limit()
36433a9a941dSLen Brown  */
36443a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36453a9a941dSLen Brown {
36463a9a941dSLen Brown 	unsigned long long msr;
36473a9a941dSLen Brown 	int cpu;
36483a9a941dSLen Brown 
36493a9a941dSLen Brown 	cpu = t->cpu_id;
36503a9a941dSLen Brown 
36513a9a941dSLen Brown 	/* per-package */
36523a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36533a9a941dSLen Brown 		return 0;
36543a9a941dSLen Brown 
36553a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3656b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36573a9a941dSLen Brown 		return -1;
36583a9a941dSLen Brown 	}
36593a9a941dSLen Brown 
36603a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
36613a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3662b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3663b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3664e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
36653a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3666e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3667e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3668e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3669e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3670e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3671e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3672e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3673e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3674e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3675e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3676e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3677e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3678b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3679e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
36803a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3681e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3682e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3683e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3684e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3685e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3686e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3687e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3688e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3689e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3690e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3691e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3692e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
36933a9a941dSLen Brown 
36943a9a941dSLen Brown 	}
36953a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
36963a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3697b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3698b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
36993a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37003a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37013a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
37023a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37033a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37043a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
37053a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37063a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3707b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
37083a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37093a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37103a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
37113a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37123a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37133a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
37143a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37153a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37163a9a941dSLen Brown 	}
37173a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
37183a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3719b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3720b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
37213a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37223a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37233a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37243a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37253a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37263a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3727b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
37283a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37293a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37303a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37313a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37323a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37333a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37343a9a941dSLen Brown 	}
37353a9a941dSLen Brown 	return 0;
37363a9a941dSLen Brown }
37373a9a941dSLen Brown 
3738889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3739889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3740889facbeSLen Brown 
37411b69317dSColin Ian King double get_tdp(unsigned int model)
3742144b44b1SLen Brown {
3743144b44b1SLen Brown 	unsigned long long msr;
3744144b44b1SLen Brown 
3745144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
37467ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3747144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3748144b44b1SLen Brown 
3749144b44b1SLen Brown 	switch (model) {
3750f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3751f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:
3752144b44b1SLen Brown 		return 30.0;
3753144b44b1SLen Brown 	default:
3754144b44b1SLen Brown 		return 135.0;
3755144b44b1SLen Brown 	}
3756144b44b1SLen Brown }
3757144b44b1SLen Brown 
375840ee8e3bSAndrey Semin /*
375940ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
376040ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
376140ee8e3bSAndrey Semin  */
376240ee8e3bSAndrey Semin static double
376340ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
376440ee8e3bSAndrey Semin {
376540ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
376640ee8e3bSAndrey Semin 
376740ee8e3bSAndrey Semin 	switch (model) {
3768869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3769869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3770869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3771869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3772005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
377340ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
377440ee8e3bSAndrey Semin 	default:
377540ee8e3bSAndrey Semin 		return (rapl_energy_units);
377640ee8e3bSAndrey Semin 	}
377740ee8e3bSAndrey Semin }
377840ee8e3bSAndrey Semin 
3779144b44b1SLen Brown 
3780889facbeSLen Brown /*
3781889facbeSLen Brown  * rapl_probe()
3782889facbeSLen Brown  *
3783144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3784889facbeSLen Brown  */
3785889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3786889facbeSLen Brown {
3787889facbeSLen Brown 	unsigned long long msr;
3788144b44b1SLen Brown 	unsigned int time_unit;
3789889facbeSLen Brown 	double tdp;
3790889facbeSLen Brown 
3791889facbeSLen Brown 	if (!genuine_intel)
3792889facbeSLen Brown 		return;
3793889facbeSLen Brown 
3794889facbeSLen Brown 	if (family != 6)
3795889facbeSLen Brown 		return;
3796889facbeSLen Brown 
3797889facbeSLen Brown 	switch (model) {
3798869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3799869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3800869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3801869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3802869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3803869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3804869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3805144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3806812db3f7SLen Brown 		if (rapl_joules) {
3807812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3808812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3809812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3810812db3f7SLen Brown 		} else {
3811812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3812812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3813812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3814812db3f7SLen Brown 		}
3815889facbeSLen Brown 		break;
3816869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3817f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3818e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3819812db3f7SLen Brown 		if (rapl_joules)
3820812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3821812db3f7SLen Brown 		else
3822812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3823e4085d54SLen Brown 		break;
3824869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3825869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3826869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3827869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3828997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
382981824921SLen 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;
3830812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3831812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3832812db3f7SLen Brown 		if (rapl_joules) {
3833812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3834812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3835812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
383681824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3837812db3f7SLen Brown 		} else {
3838812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3839812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3840812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
384181824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3842812db3f7SLen Brown 		}
38430b2bb692SLen Brown 		break;
3844869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3845869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3846869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3847869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3848869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3849005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
38500b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3851812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3852812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3853812db3f7SLen Brown 		if (rapl_joules) {
3854812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3855812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3856812db3f7SLen Brown 		} else {
3857812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3858812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3859812db3f7SLen Brown 		}
3860e6f9bb3cSLen Brown 		break;
3861869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3862869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
38630b2bb692SLen 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;
3864812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3865812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3866812db3f7SLen Brown 		if (rapl_joules) {
3867812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3868812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3869812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3870812db3f7SLen Brown 		} else {
3871812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3872812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3873812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3874812db3f7SLen Brown 		}
3875144b44b1SLen Brown 		break;
3876f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3877f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
3878144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3879812db3f7SLen Brown 		if (rapl_joules) {
3880812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3881812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3882812db3f7SLen Brown 		} else {
3883812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3884812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3885812db3f7SLen Brown 		}
3886889facbeSLen Brown 		break;
3887f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
38880f644909SJacob 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;
3889812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3890812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3891812db3f7SLen Brown 		if (rapl_joules) {
3892812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3893812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3894812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3895812db3f7SLen Brown 		} else {
3896812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3897812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3898812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3899812db3f7SLen Brown 		}
39000f644909SJacob Pan 		break;
3901889facbeSLen Brown 	default:
3902889facbeSLen Brown 		return;
3903889facbeSLen Brown 	}
3904889facbeSLen Brown 
3905889facbeSLen Brown 	/* units on package 0, verify later other packages match */
39067ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3907889facbeSLen Brown 		return;
3908889facbeSLen Brown 
3909889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3910f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
3911144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3912144b44b1SLen Brown 	else
3913889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3914889facbeSLen Brown 
391540ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
391640ee8e3bSAndrey Semin 
3917144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3918144b44b1SLen Brown 	if (time_unit == 0)
3919144b44b1SLen Brown 		time_unit = 0xA;
3920889facbeSLen Brown 
3921144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3922144b44b1SLen Brown 
3923144b44b1SLen Brown 	tdp = get_tdp(model);
3924889facbeSLen Brown 
3925889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
392696e47158SLen Brown 	if (!quiet)
3927b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3928889facbeSLen Brown 
3929889facbeSLen Brown 	return;
3930889facbeSLen Brown }
3931889facbeSLen Brown 
39321b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
39333a9a941dSLen Brown {
39343a9a941dSLen Brown 	if (!genuine_intel)
39353a9a941dSLen Brown 		return;
39363a9a941dSLen Brown 
39373a9a941dSLen Brown 	if (family != 6)
39383a9a941dSLen Brown 		return;
39393a9a941dSLen Brown 
39403a9a941dSLen Brown 	switch (model) {
3941869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3942869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3943869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
39443a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3945869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
39463a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
39473a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
39483a9a941dSLen Brown 	default:
39493a9a941dSLen Brown 		return;
39503a9a941dSLen Brown 	}
39513a9a941dSLen Brown }
39523a9a941dSLen Brown 
3953ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
3954ac980e13SArtem Bityutskiy {
3955ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
3956ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
3957ac980e13SArtem Bityutskiy }
3958ac980e13SArtem Bityutskiy 
3959889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3960889facbeSLen Brown {
3961889facbeSLen Brown 	unsigned long long msr;
3962f4896fa5SLen Brown 	unsigned int dts, dts2;
3963889facbeSLen Brown 	int cpu;
3964889facbeSLen Brown 
3965889facbeSLen Brown 	if (!(do_dts || do_ptm))
3966889facbeSLen Brown 		return 0;
3967889facbeSLen Brown 
3968889facbeSLen Brown 	cpu = t->cpu_id;
3969889facbeSLen Brown 
3970889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3971889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3972889facbeSLen Brown 		return 0;
3973889facbeSLen Brown 
3974889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3975b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3976889facbeSLen Brown 		return -1;
3977889facbeSLen Brown 	}
3978889facbeSLen Brown 
3979889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3980889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3981889facbeSLen Brown 			return 0;
3982889facbeSLen Brown 
3983889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3984b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3985889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3986889facbeSLen Brown 
3987889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3988889facbeSLen Brown 			return 0;
3989889facbeSLen Brown 
3990889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3991889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3992b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3993889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3994889facbeSLen Brown 	}
3995889facbeSLen Brown 
3996889facbeSLen Brown 
3997f4896fa5SLen Brown 	if (do_dts && debug) {
3998889facbeSLen Brown 		unsigned int resolution;
3999889facbeSLen Brown 
4000889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4001889facbeSLen Brown 			return 0;
4002889facbeSLen Brown 
4003889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4004889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4005b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4006889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
4007889facbeSLen Brown 
4008889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4009889facbeSLen Brown 			return 0;
4010889facbeSLen Brown 
4011889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4012889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4013b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4014889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4015889facbeSLen Brown 	}
4016889facbeSLen Brown 
4017889facbeSLen Brown 	return 0;
4018889facbeSLen Brown }
4019889facbeSLen Brown 
4020889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4021889facbeSLen Brown {
4022b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4023889facbeSLen Brown 		cpu, label,
4024889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4025889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4026889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4027889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4028889facbeSLen Brown 
4029889facbeSLen Brown 	return;
4030889facbeSLen Brown }
4031889facbeSLen Brown 
4032889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4033889facbeSLen Brown {
4034889facbeSLen Brown 	unsigned long long msr;
4035889facbeSLen Brown 	int cpu;
4036889facbeSLen Brown 
4037889facbeSLen Brown 	if (!do_rapl)
4038889facbeSLen Brown 		return 0;
4039889facbeSLen Brown 
4040889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4041889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4042889facbeSLen Brown 		return 0;
4043889facbeSLen Brown 
4044889facbeSLen Brown 	cpu = t->cpu_id;
4045889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4046b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4047889facbeSLen Brown 		return -1;
4048889facbeSLen Brown 	}
4049889facbeSLen Brown 
4050889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4051889facbeSLen Brown 		return -1;
4052889facbeSLen Brown 
405396e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
4054144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
405596e47158SLen Brown 
4056144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4057144b44b1SLen Brown 
4058889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4059889facbeSLen Brown                 	return -5;
4060889facbeSLen Brown 
4061889facbeSLen Brown 
4062b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4063889facbeSLen Brown 			cpu, msr,
4064889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4065889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4066889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4067889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4068889facbeSLen Brown 
4069144b44b1SLen Brown 	}
4070144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4071144b44b1SLen Brown 
4072889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4073889facbeSLen Brown 			return -9;
4074889facbeSLen Brown 
4075b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4076889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4077889facbeSLen Brown 
4078889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4079b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4080889facbeSLen Brown 			cpu,
4081889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4082889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4083889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4084889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4085889facbeSLen Brown 	}
4086889facbeSLen Brown 
40870b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4088889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4089889facbeSLen Brown                 	return -6;
4090889facbeSLen Brown 
4091b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4092889facbeSLen Brown 			cpu, msr,
4093889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4094889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4095889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4096889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
40970b2bb692SLen Brown 	}
40980b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4099889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4100889facbeSLen Brown 			return -9;
4101b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4102889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4103889facbeSLen Brown 
4104889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4105889facbeSLen Brown 	}
4106144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4107889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4108889facbeSLen Brown 			return -7;
4109889facbeSLen Brown 
4110b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4111144b44b1SLen Brown 	}
41129148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4113889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4114889facbeSLen Brown 			return -9;
4115b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4116889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4117889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4118889facbeSLen Brown 	}
4119889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4120889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4121889facbeSLen Brown 			return -8;
4122889facbeSLen Brown 
4123b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4124889facbeSLen Brown 
4125889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4126889facbeSLen Brown 			return -9;
4127b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4128889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4129889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4130889facbeSLen Brown 	}
4131889facbeSLen Brown 	return 0;
4132889facbeSLen Brown }
4133889facbeSLen Brown 
4134d7899447SLen Brown /*
4135d7899447SLen Brown  * SNB adds support for additional MSRs:
4136d7899447SLen Brown  *
4137d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4138d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4139d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4140d7899447SLen Brown  */
4141103a8feaSLen Brown 
4142d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4143103a8feaSLen Brown {
4144103a8feaSLen Brown 	if (!genuine_intel)
4145103a8feaSLen Brown 		return 0;
4146103a8feaSLen Brown 
4147103a8feaSLen Brown 	switch (model) {
4148869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4149869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4150869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4151869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
4152869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
4153869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
4154869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
4155869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
4156869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4157869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
4158869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4159869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
4160869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4161869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4162869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4163869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4164997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4165869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4166869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4167f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4168f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
4169103a8feaSLen Brown 		return 1;
4170103a8feaSLen Brown 	}
4171103a8feaSLen Brown 	return 0;
4172103a8feaSLen Brown }
4173103a8feaSLen Brown 
4174d7899447SLen Brown /*
4175d7899447SLen Brown  * HSW adds support for additional MSRs:
4176d7899447SLen Brown  *
4177d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4178d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4179d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
41805a63426eSLen Brown  *
41815a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
41825a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
41835a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
41845a63426eSLen Brown  *
4185d7899447SLen Brown  */
4186d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
4187ca58710fSKristen Carlson Accardi {
4188ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4189ca58710fSKristen Carlson Accardi 		return 0;
4190ca58710fSKristen Carlson Accardi 
4191ca58710fSKristen Carlson Accardi 	switch (model) {
4192869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
4193869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
4194869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4195869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4196869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4197869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4198997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4199869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4200f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4201ca58710fSKristen Carlson Accardi 		return 1;
4202ca58710fSKristen Carlson Accardi 	}
4203ca58710fSKristen Carlson Accardi 	return 0;
4204ca58710fSKristen Carlson Accardi }
4205ca58710fSKristen Carlson Accardi 
42060b2bb692SLen Brown /*
42070b2bb692SLen Brown  * SKL adds support for additional MSRS:
42080b2bb692SLen Brown  *
42090b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
42100b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
42110b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
42120b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
42130b2bb692SLen Brown  */
42140b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
42150b2bb692SLen Brown {
42160b2bb692SLen Brown 	if (!genuine_intel)
42170b2bb692SLen Brown 		return 0;
42180b2bb692SLen Brown 
42190b2bb692SLen Brown 	switch (model) {
4220869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4221869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4222869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4223869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4224997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
42250b2bb692SLen Brown 		return 1;
42260b2bb692SLen Brown 	}
42270b2bb692SLen Brown 	return 0;
42280b2bb692SLen Brown }
42290b2bb692SLen Brown 
4230144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4231144b44b1SLen Brown {
4232144b44b1SLen Brown 	if (!genuine_intel)
4233144b44b1SLen Brown 		return 0;
4234144b44b1SLen Brown 	switch (model) {
4235f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
4236f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
4237144b44b1SLen Brown 		return 1;
4238144b44b1SLen Brown 	}
4239144b44b1SLen Brown 	return 0;
4240144b44b1SLen Brown }
4241144b44b1SLen Brown 
4242fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4243fb5d4327SDasaratharaman Chandramouli {
4244fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4245fb5d4327SDasaratharaman Chandramouli 		return 0;
4246fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4247869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4248005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4249fb5d4327SDasaratharaman Chandramouli 		return 1;
4250fb5d4327SDasaratharaman Chandramouli 	}
4251fb5d4327SDasaratharaman Chandramouli 	return 0;
4252fb5d4327SDasaratharaman Chandramouli }
4253fb5d4327SDasaratharaman Chandramouli 
4254997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4255997e5395SSrinivas Pandruvada {
4256997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4257997e5395SSrinivas Pandruvada 		return 0;
4258997e5395SSrinivas Pandruvada 
4259997e5395SSrinivas Pandruvada 	switch (model) {
4260997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4261997e5395SSrinivas Pandruvada 		return 1;
4262997e5395SSrinivas Pandruvada 	}
4263997e5395SSrinivas Pandruvada 
4264997e5395SSrinivas Pandruvada 	return 0;
4265997e5395SSrinivas Pandruvada }
4266997e5395SSrinivas Pandruvada 
4267b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4268b2b34dfeSHubert Chrzaniuk {
4269b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4270b2b34dfeSHubert Chrzaniuk 		return 1024;
4271b2b34dfeSHubert Chrzaniuk 	return 1;
4272b2b34dfeSHubert Chrzaniuk }
4273b2b34dfeSHubert Chrzaniuk 
4274144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4275144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4276144b44b1SLen Brown 
4277144b44b1SLen Brown double slm_bclk(void)
4278144b44b1SLen Brown {
4279144b44b1SLen Brown 	unsigned long long msr = 3;
4280144b44b1SLen Brown 	unsigned int i;
4281144b44b1SLen Brown 	double freq;
4282144b44b1SLen Brown 
42837ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4284b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4285144b44b1SLen Brown 
4286144b44b1SLen Brown 	i = msr & 0xf;
4287144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4288b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
42890a91e551SColin Ian King 		i = 3;
4290144b44b1SLen Brown 	}
4291144b44b1SLen Brown 	freq = slm_freq_table[i];
4292144b44b1SLen Brown 
429396e47158SLen Brown 	if (!quiet)
4294b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4295144b44b1SLen Brown 
4296144b44b1SLen Brown 	return freq;
4297144b44b1SLen Brown }
4298144b44b1SLen Brown 
4299103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4300103a8feaSLen Brown {
4301121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4302103a8feaSLen Brown 		return 100.00;
4303144b44b1SLen Brown 	else if (is_slm(family, model))
4304144b44b1SLen Brown 		return slm_bclk();
4305103a8feaSLen Brown 	else
4306103a8feaSLen Brown 		return 133.33;
4307103a8feaSLen Brown }
4308103a8feaSLen Brown 
4309889facbeSLen Brown /*
4310889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4311889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4312889facbeSLen Brown  * This is usually equal to tjMax.
4313889facbeSLen Brown  *
4314889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4315889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4316889facbeSLen Brown  *
4317889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4318889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4319889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4320889facbeSLen Brown  */
4321889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4322889facbeSLen Brown {
4323889facbeSLen Brown 	unsigned long long msr;
4324889facbeSLen Brown 	unsigned int target_c_local;
4325889facbeSLen Brown 	int cpu;
4326889facbeSLen Brown 
4327889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4328889facbeSLen Brown 	if (!(do_dts || do_ptm))
4329889facbeSLen Brown 		return 0;
4330889facbeSLen Brown 
4331889facbeSLen Brown 	/* this is a per-package concept */
4332889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4333889facbeSLen Brown 		return 0;
4334889facbeSLen Brown 
4335889facbeSLen Brown 	cpu = t->cpu_id;
4336889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4337b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4338889facbeSLen Brown 		return -1;
4339889facbeSLen Brown 	}
4340889facbeSLen Brown 
4341889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4342889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4343b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4344889facbeSLen Brown 			cpu, tcc_activation_temp);
4345889facbeSLen Brown 		return 0;
4346889facbeSLen Brown 	}
4347889facbeSLen Brown 
4348889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4349d7899447SLen Brown 	if (!do_nhm_platform_info)
4350889facbeSLen Brown 		goto guess;
4351889facbeSLen Brown 
43527ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4353889facbeSLen Brown 		goto guess;
4354889facbeSLen Brown 
43553482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4356889facbeSLen Brown 
435796e47158SLen Brown 	if (!quiet)
4358b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4359889facbeSLen Brown 			cpu, msr, target_c_local);
4360889facbeSLen Brown 
43613482124aSJean Delvare 	if (!target_c_local)
4362889facbeSLen Brown 		goto guess;
4363889facbeSLen Brown 
4364889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4365889facbeSLen Brown 
4366889facbeSLen Brown 	return 0;
4367889facbeSLen Brown 
4368889facbeSLen Brown guess:
4369889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4370b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4371889facbeSLen Brown 		cpu, tcc_activation_temp);
4372889facbeSLen Brown 
4373889facbeSLen Brown 	return 0;
4374889facbeSLen Brown }
437569807a63SLen Brown 
4376aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4377aa8d8cc7SLen Brown {
4378aa8d8cc7SLen Brown 	unsigned long long msr;
4379aa8d8cc7SLen Brown 
4380aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4381aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4382aa8d8cc7SLen Brown 			base_cpu, msr,
4383aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4384aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4385aa8d8cc7SLen Brown }
4386aa8d8cc7SLen Brown 
438769807a63SLen Brown void decode_misc_enable_msr(void)
438869807a63SLen Brown {
438969807a63SLen Brown 	unsigned long long msr;
439069807a63SLen Brown 
4391f26b1519SLen Brown 	if (!genuine_intel)
4392f26b1519SLen Brown 		return;
4393f26b1519SLen Brown 
439469807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4395e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
439669807a63SLen Brown 			base_cpu, msr,
4397e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4398e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4399fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4400e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4401e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
440269807a63SLen Brown }
440369807a63SLen Brown 
440433148d67SLen Brown void decode_misc_feature_control(void)
440533148d67SLen Brown {
440633148d67SLen Brown 	unsigned long long msr;
440733148d67SLen Brown 
440833148d67SLen Brown 	if (!has_misc_feature_control)
440933148d67SLen Brown 		return;
441033148d67SLen Brown 
441133148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
441233148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
441333148d67SLen Brown 			base_cpu, msr,
441433148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
441533148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
441633148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
441733148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
441833148d67SLen Brown }
4419f0057310SLen Brown /*
4420f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4421f0057310SLen Brown  *
4422f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4423f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4424f0057310SLen Brown  * bit[1] less so...
4425f0057310SLen Brown  */
4426f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4427f0057310SLen Brown {
4428f0057310SLen Brown 	unsigned long long msr;
4429f0057310SLen Brown 
4430f0057310SLen Brown 	if (!do_nhm_platform_info)
4431f0057310SLen Brown 		return;
4432f0057310SLen Brown 
4433cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4434cf4cbe53SLen Brown 		return;
4435cf4cbe53SLen Brown 
4436f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4437ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4438f0057310SLen Brown 			base_cpu, msr,
4439f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4440ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4441ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4442f0057310SLen Brown }
444371616c8eSLen Brown /*
444471616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
444571616c8eSLen Brown  *
444671616c8eSLen Brown  * This MSRs are present on Silvermont processors,
444771616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
444871616c8eSLen Brown  */
444971616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
445071616c8eSLen Brown {
445171616c8eSLen Brown 	unsigned long long msr;
445271616c8eSLen Brown 
445371616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
445471616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
445571616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
445671616c8eSLen Brown 
445771616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
445871616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
445971616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
446071616c8eSLen Brown }
44617f5c258eSLen Brown 
4462fcd17211SLen Brown void process_cpuid()
4463103a8feaSLen Brown {
446434041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
446534041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
4466b3a34e93SLen Brown 	unsigned int has_turbo;
4467103a8feaSLen Brown 
4468103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4469103a8feaSLen Brown 
44705aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4471103a8feaSLen Brown 
447234041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
4473103a8feaSLen Brown 		genuine_intel = 1;
447434041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
447534041551SLen Brown 		authentic_amd = 1;
4476103a8feaSLen Brown 
447796e47158SLen Brown 	if (!quiet)
4478b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4479103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4480103a8feaSLen Brown 
44815aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4482103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4483103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4484103a8feaSLen Brown 	stepping = fms & 0xf;
44855aa3d1a2SCalvin Walton 	if (family == 0xf)
44865aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
44875aa3d1a2SCalvin Walton 	if (family >= 6)
4488103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
448934041551SLen Brown 	ecx_flags = ecx;
449034041551SLen Brown 	edx_flags = edx;
4491103a8feaSLen Brown 
4492103a8feaSLen Brown 	/*
4493103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4494103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4495103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4496103a8feaSLen Brown 	 */
4497103a8feaSLen Brown 	ebx = ecx = edx = 0;
44985aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4499103a8feaSLen Brown 
450034041551SLen Brown 	if (!quiet) {
450134041551SLen Brown 		fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
450234041551SLen Brown 			max_level, max_extended_level, family, model, stepping, family, model, stepping);
450334041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
450434041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
450534041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
450634041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
450734041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
450834041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
450934041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
451034041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
451134041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
451234041551SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-",
451334041551SLen Brown 			edx_flags & (1 << 29) ? "TM" : "-");
451434041551SLen Brown 	}
451534041551SLen Brown 
451634041551SLen Brown 	if (!(edx_flags & (1 << 5)))
451734041551SLen Brown 		errx(1, "CPUID: no MSR");
451834041551SLen Brown 
451961a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4520103a8feaSLen Brown 
4521103a8feaSLen Brown 		/*
4522103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4523103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4524103a8feaSLen Brown 		 */
45255aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
45268209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4527d7899447SLen Brown 	}
4528103a8feaSLen Brown 
4529103a8feaSLen Brown 	/*
4530103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4531103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4532103a8feaSLen Brown 	 */
4533103a8feaSLen Brown 
45345aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
45358209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4536812db3f7SLen Brown 	if (has_aperf) {
4537812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4538812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4539812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4540812db3f7SLen Brown 	}
4541889facbeSLen Brown 	do_dts = eax & (1 << 0);
4542812db3f7SLen Brown 	if (do_dts)
4543812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4544b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4545889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4546812db3f7SLen Brown 	if (do_ptm)
4547812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
45487f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
45497f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
45507f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
45517f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
45527f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4553889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4554889facbeSLen Brown 
455596e47158SLen Brown 	if (!quiet)
4556b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
45577f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
45587f5c258eSLen Brown 			has_aperf ? "" : "No-",
4559b3a34e93SLen Brown 			has_turbo ? "" : "No-",
45607f5c258eSLen Brown 			do_dts ? "" : "No-",
45617f5c258eSLen Brown 			do_ptm ? "" : "No-",
45627f5c258eSLen Brown 			has_hwp ? "" : "No-",
45637f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
45647f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
45657f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
45667f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
45677f5c258eSLen Brown 			has_epb ? "" : "No-");
4568103a8feaSLen Brown 
456996e47158SLen Brown 	if (!quiet)
457069807a63SLen Brown 		decode_misc_enable_msr();
457169807a63SLen Brown 
457233148d67SLen Brown 
457396e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4574aa8d8cc7SLen Brown 		int has_sgx;
4575aa8d8cc7SLen Brown 
4576aa8d8cc7SLen Brown 		ecx = 0;
4577aa8d8cc7SLen Brown 
4578aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4579aa8d8cc7SLen Brown 
4580aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4581aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4582aa8d8cc7SLen Brown 
4583aa8d8cc7SLen Brown 		if (has_sgx)
4584aa8d8cc7SLen Brown 			decode_feature_control_msr();
4585aa8d8cc7SLen Brown 	}
4586aa8d8cc7SLen Brown 
458761a87ba7SLen Brown 	if (max_level >= 0x15) {
45888a5bdf41SLen Brown 		unsigned int eax_crystal;
45898a5bdf41SLen Brown 		unsigned int ebx_tsc;
45908a5bdf41SLen Brown 
45918a5bdf41SLen Brown 		/*
45928a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
45938a5bdf41SLen Brown 		 */
45948a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
45955aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
45968a5bdf41SLen Brown 
45978a5bdf41SLen Brown 		if (ebx_tsc != 0) {
45988a5bdf41SLen Brown 
459996e47158SLen Brown 			if (!quiet && (ebx != 0))
4600b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
46018a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
46028a5bdf41SLen Brown 
46038a5bdf41SLen Brown 			if (crystal_hz == 0)
46048a5bdf41SLen Brown 				switch(model) {
4605869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4606869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
4607869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
4608869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
4609e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4610e8efbc80SLen Brown 					break;
4611f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
4612ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4613ec53e594SLen Brown 					break;
4614869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4615f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4616e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
46178a5bdf41SLen Brown 					break;
46188a5bdf41SLen Brown 				default:
46198a5bdf41SLen Brown 					crystal_hz = 0;
46208a5bdf41SLen Brown 			}
46218a5bdf41SLen Brown 
46228a5bdf41SLen Brown 			if (crystal_hz) {
46238a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
462496e47158SLen Brown 				if (!quiet)
4625b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
46268a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
46278a5bdf41SLen Brown 			}
46288a5bdf41SLen Brown 		}
46298a5bdf41SLen Brown 	}
463061a87ba7SLen Brown 	if (max_level >= 0x16) {
463161a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
463261a87ba7SLen Brown 
463361a87ba7SLen Brown 		/*
463461a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
463561a87ba7SLen Brown 		 */
463661a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
463761a87ba7SLen Brown 
46385aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
463996e47158SLen Brown 		if (!quiet)
4640b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
464161a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
464261a87ba7SLen Brown 	}
46438a5bdf41SLen Brown 
4644b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4645b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4646b2b34dfeSHubert Chrzaniuk 
4647812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4648812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4649812db3f7SLen Brown 
4650812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4651812db3f7SLen Brown 		do_nhm_platform_info = 1;
4652812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4653812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4654812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4655812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4656812db3f7SLen Brown 	}
4657d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4658812db3f7SLen Brown 
4659812db3f7SLen Brown 	if (do_snb_cstates)
4660812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4661812db3f7SLen Brown 
46625a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
46630f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
46640f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
46650f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
46660f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
46670f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
46680f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
46690f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
46700f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
46710539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
46720f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
46730f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
46740f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
46750f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
46760539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
46770539ba11SLen Brown 		use_c1_residency_msr = 1;
46780539ba11SLen Brown 	}
46797170a374SLen Brown 	if (is_dnv(family, model)) {
46807170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
46817170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
46827170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
46837170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
46847170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
46857170a374SLen Brown 		use_c1_residency_msr = 1;
46867170a374SLen Brown 	}
468734c76197SLen Brown 	if (is_skx(family, model)) {
468834c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
468934c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
469034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
469134c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
469234c76197SLen Brown 	}
4693ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4694ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4695ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4696ade0ebacSLen Brown 	}
46970f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
46980f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
46990f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
47000f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
47010f47c08dSLen Brown 	}
47025a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4703a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4704a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4705a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4706a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4707a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4708a99d8730SLen Brown 	}
4709144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4710fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4711997e5395SSrinivas Pandruvada 	do_cnl_cstates = is_cnl(family, model);
4712103a8feaSLen Brown 
471396e47158SLen Brown 	if (!quiet)
4714f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4715f0057310SLen Brown 
471696e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
471771616c8eSLen Brown 		decode_c6_demotion_policy_msr();
471871616c8eSLen Brown 
4719889facbeSLen Brown 	rapl_probe(family, model);
47203a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4721ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4722889facbeSLen Brown 
472396e47158SLen Brown 	if (!quiet)
47241b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4725fcd17211SLen Brown 
472641618e63SLen Brown 	if (!quiet)
472741618e63SLen Brown 		dump_sysfs_cstate_config();
47287293fccdSLen Brown 	if (!quiet)
47297293fccdSLen Brown 		dump_sysfs_pstate_config();
473041618e63SLen Brown 
4731a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4732a2b7b749SLen Brown 		calculate_tsc_tweak();
4733a2b7b749SLen Brown 
4734812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4735812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4736fdf676e5SLen Brown 
4737812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4738812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
473927d47356SLen Brown 
4740be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4741be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
4742be0e54c4SLen Brown 	else
4743be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
4744be0e54c4SLen Brown 
4745be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4746be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
4747be0e54c4SLen Brown 	else
4748be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
4749be0e54c4SLen Brown 
475096e47158SLen Brown 	if (!quiet)
475133148d67SLen Brown 		decode_misc_feature_control();
475233148d67SLen Brown 
4753889facbeSLen Brown 	return;
4754103a8feaSLen Brown }
4755103a8feaSLen Brown 
4756103a8feaSLen Brown /*
4757103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4758103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4759103a8feaSLen Brown  */
4760103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4761103a8feaSLen Brown {
4762103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4763103a8feaSLen Brown 		return 1;
4764103a8feaSLen Brown 	else
4765103a8feaSLen Brown 		return 0;
4766103a8feaSLen Brown }
4767103a8feaSLen Brown 
4768103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4769103a8feaSLen Brown {
4770103a8feaSLen Brown 	return 0;
4771103a8feaSLen Brown }
4772103a8feaSLen Brown 
4773c98d5d94SLen Brown void topology_probe()
4774c98d5d94SLen Brown {
4775c98d5d94SLen Brown 	int i;
4776c98d5d94SLen Brown 	int max_core_id = 0;
4777c98d5d94SLen Brown 	int max_package_id = 0;
4778c98d5d94SLen Brown 	int max_siblings = 0;
4779c98d5d94SLen Brown 
4780c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4781843c5791SPrarit Bhargava 	set_max_cpu_num();
4782c98d5d94SLen Brown 	topo.num_cpus = 0;
4783c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4784c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4785812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4786c98d5d94SLen Brown 
4787d8af6f5fSLen Brown 	if (debug > 1)
4788b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4789c98d5d94SLen Brown 
4790c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4791b2c95d90SJosh Triplett 	if (cpus == NULL)
4792b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4793c98d5d94SLen Brown 
4794c98d5d94SLen Brown 	/*
4795c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4796c98d5d94SLen Brown 	 */
4797c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4798b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4799b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4800c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4801c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4802c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4803c98d5d94SLen Brown 
4804c98d5d94SLen Brown 	/*
48051ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
48061ef7d21aSLen Brown 	 */
48071ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
48081ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
48091ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
48101ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
48111ef7d21aSLen Brown 	}
48121ef7d21aSLen Brown 
48131ef7d21aSLen Brown 	/*
4814c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4815c98d5d94SLen Brown 	 */
4816c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4817b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4818b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4819c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4820c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4821c98d5d94SLen Brown 
48228cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
4823c98d5d94SLen Brown 
4824c98d5d94SLen Brown 	/*
4825c98d5d94SLen Brown 	 * For online cpus
4826c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4827c98d5d94SLen Brown 	 */
4828c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4829c98d5d94SLen Brown 		int siblings;
4830c98d5d94SLen Brown 
4831c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4832d8af6f5fSLen Brown 			if (debug > 1)
4833b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4834c98d5d94SLen Brown 			continue;
4835c98d5d94SLen Brown 		}
4836c98d5d94SLen Brown 
48370e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
48380e2d8f05SLen Brown 
48390e2d8f05SLen Brown 		/* get package information */
4840c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4841c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4842c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4843c98d5d94SLen Brown 
48440e2d8f05SLen Brown 		/* get numa node information */
4845ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4846ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
4847ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
48480e2d8f05SLen Brown 
48490e2d8f05SLen Brown 		/* get core information */
48500e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
48510e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
48520e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
48530e2d8f05SLen Brown 
48540e2d8f05SLen Brown 		/* get thread information */
48550e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
4856c98d5d94SLen Brown 		if (siblings > max_siblings)
4857c98d5d94SLen Brown 			max_siblings = siblings;
48584f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
48598cb48b32SPrarit Bhargava 			topo.num_cores++;
4860c98d5d94SLen Brown 	}
4861ef605741SPrarit Bhargava 
486270a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
4863d8af6f5fSLen Brown 	if (debug > 1)
4864b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
486570a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
486670a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
4867812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4868c98d5d94SLen Brown 
4869c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4870d8af6f5fSLen Brown 	if (debug > 1)
4871b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4872c98d5d94SLen Brown 			max_package_id, topo.num_packages);
48737da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
4874812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4875c98d5d94SLen Brown 
4876ef605741SPrarit Bhargava 	set_node_data();
4877ef605741SPrarit Bhargava 	if (debug > 1)
487870a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
487901235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
488001235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
4881ef605741SPrarit Bhargava 
488270a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
4883d8af6f5fSLen Brown 	if (debug > 1)
4884b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
48852ffbb224SPrarit Bhargava 
48862ffbb224SPrarit Bhargava 	if (debug < 1)
48872ffbb224SPrarit Bhargava 		return;
48882ffbb224SPrarit Bhargava 
48892ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4890*0ec712e3SLen Brown 		if (cpu_is_not_present(i))
4891*0ec712e3SLen Brown 			continue;
48922ffbb224SPrarit Bhargava 		fprintf(outf,
48932ffbb224SPrarit Bhargava 			"cpu %d pkg %d node %d lnode %d core %d thread %d\n",
48942ffbb224SPrarit Bhargava 			i, cpus[i].physical_package_id,
48952ffbb224SPrarit Bhargava 			cpus[i].physical_node_id,
48962ffbb224SPrarit Bhargava 			cpus[i].logical_node_id,
48972ffbb224SPrarit Bhargava 			cpus[i].physical_core_id,
48982ffbb224SPrarit Bhargava 			cpus[i].thread_id);
48992ffbb224SPrarit Bhargava 	}
49002ffbb224SPrarit Bhargava 
4901c98d5d94SLen Brown }
4902c98d5d94SLen Brown 
4903c98d5d94SLen Brown void
490440f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
490540f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
4906c98d5d94SLen Brown {
4907c98d5d94SLen Brown 	int i;
490840f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
490940f5cfe7SPrarit Bhargava 			topo.num_packages;
491040f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
4911c98d5d94SLen Brown 
491240f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
4913c98d5d94SLen Brown 	if (*t == NULL)
4914c98d5d94SLen Brown 		goto error;
4915c98d5d94SLen Brown 
491640f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
4917c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4918c98d5d94SLen Brown 
491940f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
4920c98d5d94SLen Brown 	if (*c == NULL)
4921c98d5d94SLen Brown 		goto error;
4922c98d5d94SLen Brown 
492340f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
4924c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4925c98d5d94SLen Brown 
4926678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4927c98d5d94SLen Brown 	if (*p == NULL)
4928c98d5d94SLen Brown 		goto error;
4929c98d5d94SLen Brown 
4930c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4931c98d5d94SLen Brown 		(*p)[i].package_id = i;
4932c98d5d94SLen Brown 
4933c98d5d94SLen Brown 	return;
4934c98d5d94SLen Brown error:
4935b2c95d90SJosh Triplett 	err(1, "calloc counters");
4936c98d5d94SLen Brown }
4937c98d5d94SLen Brown /*
4938c98d5d94SLen Brown  * init_counter()
4939c98d5d94SLen Brown  *
4940c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4941c98d5d94SLen Brown  */
4942c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
49438cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
4944c98d5d94SLen Brown {
49458cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
494640f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
49478cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
49488cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
4949c98d5d94SLen Brown 	struct thread_data *t;
4950c98d5d94SLen Brown 	struct core_data *c;
4951c98d5d94SLen Brown 	struct pkg_data *p;
4952c98d5d94SLen Brown 
495342dd4520SNathan Ciobanu 
495442dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
495542dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
495642dd4520SNathan Ciobanu 	 */
495742dd4520SNathan Ciobanu 	if (node_id < 0)
495842dd4520SNathan Ciobanu 		node_id = 0;
495942dd4520SNathan Ciobanu 
496040f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
496140f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
49628cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
4963c98d5d94SLen Brown 
4964c98d5d94SLen Brown 	t->cpu_id = cpu_id;
49658cb48b32SPrarit Bhargava 	if (thread_id == 0) {
4966c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4967c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4968c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4969c98d5d94SLen Brown 	}
4970c98d5d94SLen Brown 
49718cb48b32SPrarit Bhargava 	c->core_id = core_id;
49728cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
4973c98d5d94SLen Brown }
4974c98d5d94SLen Brown 
4975c98d5d94SLen Brown 
4976c98d5d94SLen Brown int initialize_counters(int cpu_id)
4977c98d5d94SLen Brown {
49788cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
49798cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
4980c98d5d94SLen Brown 	return 0;
4981c98d5d94SLen Brown }
4982c98d5d94SLen Brown 
4983c98d5d94SLen Brown void allocate_output_buffer()
4984c98d5d94SLen Brown {
49853b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4986c98d5d94SLen Brown 	outp = output_buffer;
4987b2c95d90SJosh Triplett 	if (outp == NULL)
4988b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4989c98d5d94SLen Brown }
499036229897SLen Brown void allocate_fd_percpu(void)
499136229897SLen Brown {
499201a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
499336229897SLen Brown 	if (fd_percpu == NULL)
499436229897SLen Brown 		err(-1, "calloc fd_percpu");
499536229897SLen Brown }
4996562a2d37SLen Brown void allocate_irq_buffers(void)
4997562a2d37SLen Brown {
4998562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4999562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
5000562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
5001c98d5d94SLen Brown 
500201a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5003562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
500401a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
5005562a2d37SLen Brown }
5006c98d5d94SLen Brown void setup_all_buffers(void)
5007c98d5d94SLen Brown {
5008c98d5d94SLen Brown 	topology_probe();
5009562a2d37SLen Brown 	allocate_irq_buffers();
501036229897SLen Brown 	allocate_fd_percpu();
5011c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
5012c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
5013c98d5d94SLen Brown 	allocate_output_buffer();
5014c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
5015c98d5d94SLen Brown }
50163b4d5c7fSAndy Shevchenko 
50177ce7d5deSPrarit Bhargava void set_base_cpu(void)
50187ce7d5deSPrarit Bhargava {
50197ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
50207ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
50217ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
50227ce7d5deSPrarit Bhargava 
50237ce7d5deSPrarit Bhargava 	if (debug > 1)
5024b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
50257ce7d5deSPrarit Bhargava }
50267ce7d5deSPrarit Bhargava 
5027103a8feaSLen Brown void turbostat_init()
5028103a8feaSLen Brown {
50297ce7d5deSPrarit Bhargava 	setup_all_buffers();
50307ce7d5deSPrarit Bhargava 	set_base_cpu();
5031103a8feaSLen Brown 	check_dev_msr();
503298481e79SLen Brown 	check_permissions();
5033fcd17211SLen Brown 	process_cpuid();
5034103a8feaSLen Brown 
5035103a8feaSLen Brown 
503696e47158SLen Brown 	if (!quiet)
50377f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
50387f5c258eSLen Brown 
503996e47158SLen Brown 	if (!quiet)
5040889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5041889facbeSLen Brown 
504296e47158SLen Brown 	if (!quiet)
50433a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
50443a9a941dSLen Brown 
504596e47158SLen Brown 	if (!quiet)
5046889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5047889facbeSLen Brown 
5048889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5049889facbeSLen Brown 
505096e47158SLen Brown 	if (!quiet)
5051889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
50525a63426eSLen Brown 
505396e47158SLen Brown 	if (!quiet && do_irtl_snb)
50545a63426eSLen Brown 		print_irtl();
5055103a8feaSLen Brown }
5056103a8feaSLen Brown 
5057103a8feaSLen Brown int fork_it(char **argv)
5058103a8feaSLen Brown {
5059103a8feaSLen Brown 	pid_t child_pid;
5060d91bb17cSLen Brown 	int status;
5061d15cf7c1SLen Brown 
5062218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5063d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
50644c2122d4SLen Brown 	first_counter_read = 0;
5065d91bb17cSLen Brown 	if (status)
5066d91bb17cSLen Brown 		exit(status);
5067d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5068d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5069103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5070103a8feaSLen Brown 
5071103a8feaSLen Brown 	child_pid = fork();
5072103a8feaSLen Brown 	if (!child_pid) {
5073103a8feaSLen Brown 		/* child */
5074103a8feaSLen Brown 		execvp(argv[0], argv);
50750815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5076103a8feaSLen Brown 	} else {
5077103a8feaSLen Brown 
5078103a8feaSLen Brown 		/* parent */
5079b2c95d90SJosh Triplett 		if (child_pid == -1)
5080b2c95d90SJosh Triplett 			err(1, "fork");
5081103a8feaSLen Brown 
5082103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5083103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5084b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5085b2c95d90SJosh Triplett 			err(status, "waitpid");
5086103a8feaSLen Brown 	}
5087c98d5d94SLen Brown 	/*
5088c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5089c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5090c98d5d94SLen Brown 	 */
5091218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5092c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5093103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5094103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5095ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5096ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5097ba3dec99SLen Brown 	else {
5098c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5099c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5100ba3dec99SLen Brown 	}
5101103a8feaSLen Brown 
5102b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5103b7d8c148SLen Brown 
5104b7d8c148SLen Brown 	flush_output_stderr();
5105103a8feaSLen Brown 
5106d91bb17cSLen Brown 	return status;
5107103a8feaSLen Brown }
5108103a8feaSLen Brown 
51093b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
51103b4d5c7fSAndy Shevchenko {
51113b4d5c7fSAndy Shevchenko 	int status;
51123b4d5c7fSAndy Shevchenko 
5113218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
51143b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
51153b4d5c7fSAndy Shevchenko 	if (status)
51163b4d5c7fSAndy Shevchenko 		return status;
51173b4d5c7fSAndy Shevchenko 
51183b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
51193b4d5c7fSAndy Shevchenko 	if (status)
51203b4d5c7fSAndy Shevchenko 		return status;
51213b4d5c7fSAndy Shevchenko 
5122b7d8c148SLen Brown 	flush_output_stdout();
51233b4d5c7fSAndy Shevchenko 
51243b4d5c7fSAndy Shevchenko 	return status;
51253b4d5c7fSAndy Shevchenko }
51263b4d5c7fSAndy Shevchenko 
5127d8af6f5fSLen Brown void print_version() {
5128538c48f2SLen Brown 	fprintf(outf, "turbostat version 18.07.27"
5129d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5130d8af6f5fSLen Brown }
5131d8af6f5fSLen Brown 
5132495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5133495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
513441618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5135388e9c81SLen Brown {
5136388e9c81SLen Brown 	struct msr_counter *msrp;
5137388e9c81SLen Brown 
5138388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5139388e9c81SLen Brown 	if (msrp == NULL) {
5140388e9c81SLen Brown 		perror("calloc");
5141388e9c81SLen Brown 		exit(1);
5142388e9c81SLen Brown 	}
5143388e9c81SLen Brown 
5144388e9c81SLen Brown 	msrp->msr_num = msr_num;
5145388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
5146495c7654SLen Brown 	if (path)
5147495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
5148388e9c81SLen Brown 	msrp->width = width;
5149388e9c81SLen Brown 	msrp->type = type;
5150388e9c81SLen Brown 	msrp->format = format;
515141618e63SLen Brown 	msrp->flags = flags;
5152388e9c81SLen Brown 
5153388e9c81SLen Brown 	switch (scope) {
5154388e9c81SLen Brown 
5155388e9c81SLen Brown 	case SCOPE_CPU:
5156388e9c81SLen Brown 		msrp->next = sys.tp;
5157388e9c81SLen Brown 		sys.tp = msrp;
5158678a3bd1SLen Brown 		sys.added_thread_counters++;
51590748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5160678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5161678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5162678a3bd1SLen Brown 			exit(-1);
5163678a3bd1SLen Brown 		}
5164388e9c81SLen Brown 		break;
5165388e9c81SLen Brown 
5166388e9c81SLen Brown 	case SCOPE_CORE:
5167388e9c81SLen Brown 		msrp->next = sys.cp;
5168388e9c81SLen Brown 		sys.cp = msrp;
5169678a3bd1SLen Brown 		sys.added_core_counters++;
5170678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5171678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5172678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5173678a3bd1SLen Brown 			exit(-1);
5174678a3bd1SLen Brown 		}
5175388e9c81SLen Brown 		break;
5176388e9c81SLen Brown 
5177388e9c81SLen Brown 	case SCOPE_PACKAGE:
5178388e9c81SLen Brown 		msrp->next = sys.pp;
5179388e9c81SLen Brown 		sys.pp = msrp;
5180678a3bd1SLen Brown 		sys.added_package_counters++;
5181678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5182678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5183678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5184678a3bd1SLen Brown 			exit(-1);
5185678a3bd1SLen Brown 		}
5186388e9c81SLen Brown 		break;
5187388e9c81SLen Brown 	}
5188388e9c81SLen Brown 
5189388e9c81SLen Brown 	return 0;
5190388e9c81SLen Brown }
5191388e9c81SLen Brown 
5192388e9c81SLen Brown void parse_add_command(char *add_command)
5193388e9c81SLen Brown {
5194388e9c81SLen Brown 	int msr_num = 0;
5195495c7654SLen Brown 	char *path = NULL;
51960f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5197388e9c81SLen Brown 	int width = 64;
5198388e9c81SLen Brown 	int fail = 0;
5199388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5200388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5201388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5202388e9c81SLen Brown 
5203388e9c81SLen Brown 	while (add_command) {
5204388e9c81SLen Brown 
5205388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5206388e9c81SLen Brown 			goto next;
5207388e9c81SLen Brown 
5208388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5209388e9c81SLen Brown 			goto next;
5210388e9c81SLen Brown 
5211495c7654SLen Brown 		if (*add_command == '/') {
5212495c7654SLen Brown 			path = add_command;
5213495c7654SLen Brown 			goto next;
5214495c7654SLen Brown 		}
5215495c7654SLen Brown 
5216388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5217388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5218388e9c81SLen Brown 				goto next;
5219388e9c81SLen Brown 			width = 64;
5220388e9c81SLen Brown 		}
5221388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5222388e9c81SLen Brown 			scope = SCOPE_CPU;
5223388e9c81SLen Brown 			goto next;
5224388e9c81SLen Brown 		}
5225388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5226388e9c81SLen Brown 			scope = SCOPE_CORE;
5227388e9c81SLen Brown 			goto next;
5228388e9c81SLen Brown 		}
5229388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5230388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5231388e9c81SLen Brown 			goto next;
5232388e9c81SLen Brown 		}
5233388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5234388e9c81SLen Brown 			type = COUNTER_CYCLES;
5235388e9c81SLen Brown 			goto next;
5236388e9c81SLen Brown 		}
5237388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5238388e9c81SLen Brown 			type = COUNTER_SECONDS;
5239388e9c81SLen Brown 			goto next;
5240388e9c81SLen Brown 		}
524141618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
524241618e63SLen Brown 			type = COUNTER_USEC;
524341618e63SLen Brown 			goto next;
524441618e63SLen Brown 		}
5245388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5246388e9c81SLen Brown 			format = FORMAT_RAW;
5247388e9c81SLen Brown 			goto next;
5248388e9c81SLen Brown 		}
5249388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5250388e9c81SLen Brown 			format = FORMAT_DELTA;
5251388e9c81SLen Brown 			goto next;
5252388e9c81SLen Brown 		}
5253388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5254388e9c81SLen Brown 			format = FORMAT_PERCENT;
5255388e9c81SLen Brown 			goto next;
5256388e9c81SLen Brown 		}
5257388e9c81SLen Brown 
5258388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5259388e9c81SLen Brown 			char *eos;
5260388e9c81SLen Brown 
5261388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5262388e9c81SLen Brown 			if (eos)
5263388e9c81SLen Brown 				*eos = '\0';
5264388e9c81SLen Brown 			goto next;
5265388e9c81SLen Brown 		}
5266388e9c81SLen Brown 
5267388e9c81SLen Brown next:
5268388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5269495c7654SLen Brown 		if (add_command) {
5270495c7654SLen Brown 			*add_command = '\0';
5271388e9c81SLen Brown 			add_command++;
5272495c7654SLen Brown 		}
5273388e9c81SLen Brown 
5274388e9c81SLen Brown 	}
5275495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5276495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5277388e9c81SLen Brown 		fail++;
5278388e9c81SLen Brown 	}
5279388e9c81SLen Brown 
5280388e9c81SLen Brown 	/* generate default column header */
5281388e9c81SLen Brown 	if (*name_buffer == '\0') {
5282388e9c81SLen Brown 		if (width == 32)
52835f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5284388e9c81SLen Brown 		else
52855f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5286388e9c81SLen Brown 	}
5287388e9c81SLen Brown 
528841618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5289388e9c81SLen Brown 		fail++;
5290388e9c81SLen Brown 
5291388e9c81SLen Brown 	if (fail) {
5292388e9c81SLen Brown 		help();
5293388e9c81SLen Brown 		exit(1);
5294388e9c81SLen Brown 	}
5295388e9c81SLen Brown }
529641618e63SLen Brown 
5297dd778a5eSLen Brown int is_deferred_skip(char *name)
5298dd778a5eSLen Brown {
5299dd778a5eSLen Brown 	int i;
5300dd778a5eSLen Brown 
5301dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5302dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5303dd778a5eSLen Brown 			return 1;
5304dd778a5eSLen Brown 	return 0;
5305dd778a5eSLen Brown }
5306dd778a5eSLen Brown 
530741618e63SLen Brown void probe_sysfs(void)
530841618e63SLen Brown {
530941618e63SLen Brown 	char path[64];
531041618e63SLen Brown 	char name_buf[16];
531141618e63SLen Brown 	FILE *input;
531241618e63SLen Brown 	int state;
531341618e63SLen Brown 	char *sp;
531441618e63SLen Brown 
531541618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
531641618e63SLen Brown 		return;
531741618e63SLen Brown 
53180748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
531941618e63SLen Brown 
532041618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
532141618e63SLen Brown 			base_cpu, state);
532241618e63SLen Brown 		input = fopen(path, "r");
532341618e63SLen Brown 		if (input == NULL)
532441618e63SLen Brown 			continue;
532541618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
532641618e63SLen Brown 
532741618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
532841618e63SLen Brown 		sp = strchr(name_buf, '-');
532941618e63SLen Brown 		if (!sp)
533041618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
533141618e63SLen Brown 		*sp = '%';
533241618e63SLen Brown 		*(sp + 1) = '\0';
533341618e63SLen Brown 
533441618e63SLen Brown 		fclose(input);
533541618e63SLen Brown 
533641618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
533741618e63SLen Brown 
5338dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5339dd778a5eSLen Brown 			continue;
5340dd778a5eSLen Brown 
534141618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
534241618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
534341618e63SLen Brown 	}
534441618e63SLen Brown 
53450748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
534641618e63SLen Brown 
534741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
534841618e63SLen Brown 			base_cpu, state);
534941618e63SLen Brown 		input = fopen(path, "r");
535041618e63SLen Brown 		if (input == NULL)
535141618e63SLen Brown 			continue;
535241618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
535341618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
535441618e63SLen Brown 		sp = strchr(name_buf, '-');
535541618e63SLen Brown 		if (!sp)
535641618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
535741618e63SLen Brown 		*sp = '\0';
535841618e63SLen Brown 		fclose(input);
535941618e63SLen Brown 
536041618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
536141618e63SLen Brown 
5362dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5363dd778a5eSLen Brown 			continue;
5364dd778a5eSLen Brown 
536541618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
536641618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
536741618e63SLen Brown 	}
536841618e63SLen Brown 
536941618e63SLen Brown }
537041618e63SLen Brown 
53711ef7d21aSLen Brown 
53721ef7d21aSLen Brown /*
53731ef7d21aSLen Brown  * parse cpuset with following syntax
53741ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
53751ef7d21aSLen Brown  */
53761ef7d21aSLen Brown void parse_cpu_command(char *optarg)
53771ef7d21aSLen Brown {
53781ef7d21aSLen Brown 	unsigned int start, end;
53791ef7d21aSLen Brown 	char *next;
53801ef7d21aSLen Brown 
53814e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
53824e4e1e7cSLen Brown 		if (cpu_subset)
53834e4e1e7cSLen Brown 			goto error;
53844e4e1e7cSLen Brown 		show_core_only++;
53854e4e1e7cSLen Brown 		return;
53864e4e1e7cSLen Brown 	}
53874e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
53884e4e1e7cSLen Brown 		if (cpu_subset)
53894e4e1e7cSLen Brown 			goto error;
53904e4e1e7cSLen Brown 		show_pkg_only++;
53914e4e1e7cSLen Brown 		return;
53924e4e1e7cSLen Brown 	}
53934e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
53944e4e1e7cSLen Brown 		goto error;
53954e4e1e7cSLen Brown 
53961ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
53971ef7d21aSLen Brown 	if (cpu_subset == NULL)
53981ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
53991ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
54001ef7d21aSLen Brown 
54011ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
54021ef7d21aSLen Brown 
54031ef7d21aSLen Brown 	next = optarg;
54041ef7d21aSLen Brown 
54051ef7d21aSLen Brown 	while (next && *next) {
54061ef7d21aSLen Brown 
54071ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
54081ef7d21aSLen Brown 			goto error;
54091ef7d21aSLen Brown 
54101ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
54111ef7d21aSLen Brown 
54121ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
54131ef7d21aSLen Brown 			goto error;
54141ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
54151ef7d21aSLen Brown 
54161ef7d21aSLen Brown 		if (*next == '\0')
54171ef7d21aSLen Brown 			break;
54181ef7d21aSLen Brown 
54191ef7d21aSLen Brown 		if (*next == ',') {
54201ef7d21aSLen Brown 			next += 1;
54211ef7d21aSLen Brown 			continue;
54221ef7d21aSLen Brown 		}
54231ef7d21aSLen Brown 
54241ef7d21aSLen Brown 		if (*next == '-') {
54251ef7d21aSLen Brown 			next += 1;	/* start range */
54261ef7d21aSLen Brown 		} else if (*next == '.') {
54271ef7d21aSLen Brown 			next += 1;
54281ef7d21aSLen Brown 			if (*next == '.')
54291ef7d21aSLen Brown 				next += 1;	/* start range */
54301ef7d21aSLen Brown 			else
54311ef7d21aSLen Brown 				goto error;
54321ef7d21aSLen Brown 		}
54331ef7d21aSLen Brown 
54341ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
54351ef7d21aSLen Brown 		if (end <= start)
54361ef7d21aSLen Brown 			goto error;
54371ef7d21aSLen Brown 
54381ef7d21aSLen Brown 		while (++start <= end) {
54391ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
54401ef7d21aSLen Brown 				goto error;
54411ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
54421ef7d21aSLen Brown 		}
54431ef7d21aSLen Brown 
54441ef7d21aSLen Brown 		if (*next == ',')
54451ef7d21aSLen Brown 			next += 1;
54461ef7d21aSLen Brown 		else if (*next != '\0')
54471ef7d21aSLen Brown 			goto error;
54481ef7d21aSLen Brown 	}
54491ef7d21aSLen Brown 
54501ef7d21aSLen Brown 	return;
54511ef7d21aSLen Brown 
54521ef7d21aSLen Brown error:
54534e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
54544e4e1e7cSLen Brown 	help();
54551ef7d21aSLen Brown 	exit(-1);
54561ef7d21aSLen Brown }
54571ef7d21aSLen Brown 
5458812db3f7SLen Brown 
5459103a8feaSLen Brown void cmdline(int argc, char **argv)
5460103a8feaSLen Brown {
5461103a8feaSLen Brown 	int opt;
5462d8af6f5fSLen Brown 	int option_index = 0;
5463d8af6f5fSLen Brown 	static struct option long_options[] = {
5464388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
54651ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5466d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
546796e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
54683f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5469d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5470023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5471d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5472812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5473d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5474c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5475b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
547696e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5477812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5478d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5479d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5480d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5481d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5482d8af6f5fSLen Brown 	};
5483103a8feaSLen Brown 
5484103a8feaSLen Brown 	progname = argv[0];
5485103a8feaSLen Brown 
5486023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5487d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5488103a8feaSLen Brown 		switch (opt) {
5489388e9c81SLen Brown 		case 'a':
5490388e9c81SLen Brown 			parse_add_command(optarg);
5491388e9c81SLen Brown 			break;
54921ef7d21aSLen Brown 		case 'c':
54931ef7d21aSLen Brown 			parse_cpu_command(optarg);
54941ef7d21aSLen Brown 			break;
5495d8af6f5fSLen Brown 		case 'D':
5496d8af6f5fSLen Brown 			dump_only++;
54978e180f3cSLen Brown 			break;
54983f44a5c6SLen Brown 		case 'e':
54993f44a5c6SLen Brown 			/* --enable specified counter */
55004c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
55013f44a5c6SLen Brown 			break;
5502d8af6f5fSLen Brown 		case 'd':
5503d8af6f5fSLen Brown 			debug++;
55043f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
55052f32edf1SLen Brown 			break;
5506812db3f7SLen Brown 		case 'H':
55073f44a5c6SLen Brown 			/*
55083f44a5c6SLen Brown 			 * --hide: do not show those specified
55093f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
55103f44a5c6SLen Brown 			 */
55113f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5512812db3f7SLen Brown 			break;
5513d8af6f5fSLen Brown 		case 'h':
5514d8af6f5fSLen Brown 		default:
5515d8af6f5fSLen Brown 			help();
5516d8af6f5fSLen Brown 			exit(1);
5517d8af6f5fSLen Brown 		case 'i':
55182a0609c0SLen Brown 			{
55192a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
55202a0609c0SLen Brown 
55212a0609c0SLen Brown 				if (interval < 0.001) {
5522b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
55232a0609c0SLen Brown 						interval);
55242a0609c0SLen Brown 					exit(2);
55252a0609c0SLen Brown 				}
55262a0609c0SLen Brown 
552747936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5528b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
552947936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
55302a0609c0SLen Brown 			}
5531889facbeSLen Brown 			break;
55325c56be9aSDirk Brandewie 		case 'J':
55335c56be9aSDirk Brandewie 			rapl_joules++;
55345c56be9aSDirk Brandewie 			break;
5535c8ade361SLen Brown 		case 'l':
55363f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5537c8ade361SLen Brown 			list_header_only++;
5538c8ade361SLen Brown 			quiet++;
5539c8ade361SLen Brown 			break;
5540b7d8c148SLen Brown 		case 'o':
5541b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5542b7d8c148SLen Brown 			break;
554396e47158SLen Brown 		case 'q':
554496e47158SLen Brown 			quiet = 1;
554596e47158SLen Brown 			break;
5546023fe0acSChen Yu 		case 'n':
5547023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5548023fe0acSChen Yu 
5549023fe0acSChen Yu 			if (num_iterations <= 0) {
5550023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5551023fe0acSChen Yu 					num_iterations);
5552023fe0acSChen Yu 				exit(2);
5553023fe0acSChen Yu 			}
5554023fe0acSChen Yu 			break;
5555812db3f7SLen Brown 		case 's':
55563f44a5c6SLen Brown 			/*
55573f44a5c6SLen Brown 			 * --show: show only those specified
55583f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
55593f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
55603f44a5c6SLen Brown 			 */
55613f44a5c6SLen Brown 			if (shown == 0)
55623f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
55633f44a5c6SLen Brown 			else
55643f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
55653f44a5c6SLen Brown 			shown = 1;
5566812db3f7SLen Brown 			break;
5567d8af6f5fSLen Brown 		case 'S':
5568d8af6f5fSLen Brown 			summary_only++;
5569d8af6f5fSLen Brown 			break;
5570d8af6f5fSLen Brown 		case 'T':
5571d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5572d8af6f5fSLen Brown 			break;
5573d8af6f5fSLen Brown 		case 'v':
5574d8af6f5fSLen Brown 			print_version();
5575d8af6f5fSLen Brown 			exit(0);
5576d8af6f5fSLen Brown 			break;
5577103a8feaSLen Brown 		}
5578103a8feaSLen Brown 	}
5579103a8feaSLen Brown }
5580103a8feaSLen Brown 
5581103a8feaSLen Brown int main(int argc, char **argv)
5582103a8feaSLen Brown {
5583b7d8c148SLen Brown 	outf = stderr;
5584103a8feaSLen Brown 	cmdline(argc, argv);
5585103a8feaSLen Brown 
558696e47158SLen Brown 	if (!quiet)
5587d8af6f5fSLen Brown 		print_version();
5588103a8feaSLen Brown 
558941618e63SLen Brown 	probe_sysfs();
559041618e63SLen Brown 
5591103a8feaSLen Brown 	turbostat_init();
5592103a8feaSLen Brown 
55933b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
55943b4d5c7fSAndy Shevchenko 	if (dump_only)
55953b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
55963b4d5c7fSAndy Shevchenko 
5597c8ade361SLen Brown 	/* list header and exit */
5598c8ade361SLen Brown 	if (list_header_only) {
5599c8ade361SLen Brown 		print_header(",");
5600c8ade361SLen Brown 		flush_output_stdout();
5601c8ade361SLen Brown 		return 0;
5602c8ade361SLen Brown 	}
5603c8ade361SLen Brown 
5604103a8feaSLen Brown 	/*
5605103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5606103a8feaSLen Brown 	 */
5607103a8feaSLen Brown 	if (argc - optind)
5608103a8feaSLen Brown 		return fork_it(argv + optind);
5609103a8feaSLen Brown 	else
5610103a8feaSLen Brown 		turbostat_loop();
5611103a8feaSLen Brown 
5612103a8feaSLen Brown 	return 0;
5613103a8feaSLen Brown }
5614