xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision c66f78a6de4de6cb520b15cf6a1b586617b9add5)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2103a8feaSLen Brown /*
3103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
434041551SLen Brown  * on modern Intel and AMD processors.
5103a8feaSLen Brown  *
6144b44b1SLen Brown  * Copyright (c) 2013 Intel Corporation.
7103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
8103a8feaSLen Brown  */
9103a8feaSLen Brown 
1088c3281fSLen Brown #define _GNU_SOURCE
11b731f311SJosh Triplett #include MSRHEADER
12869ce69eSLen Brown #include INTEL_FAMILY_HEADER
1395aebc44SJosh Triplett #include <stdarg.h>
14103a8feaSLen Brown #include <stdio.h>
15b2c95d90SJosh Triplett #include <err.h>
16103a8feaSLen Brown #include <unistd.h>
17103a8feaSLen Brown #include <sys/types.h>
18103a8feaSLen Brown #include <sys/wait.h>
19103a8feaSLen Brown #include <sys/stat.h>
20b9ad8ee0SLen Brown #include <sys/select.h>
21103a8feaSLen Brown #include <sys/resource.h>
22103a8feaSLen Brown #include <fcntl.h>
23103a8feaSLen Brown #include <signal.h>
24103a8feaSLen Brown #include <sys/time.h>
25103a8feaSLen Brown #include <stdlib.h>
26d8af6f5fSLen Brown #include <getopt.h>
27103a8feaSLen Brown #include <dirent.h>
28103a8feaSLen Brown #include <string.h>
29103a8feaSLen Brown #include <ctype.h>
3088c3281fSLen Brown #include <sched.h>
312a0609c0SLen Brown #include <time.h>
322b92865eSJosh Triplett #include <cpuid.h>
3398481e79SLen Brown #include <linux/capability.h>
3498481e79SLen Brown #include <errno.h>
359392bd98SCalvin Walton #include <math.h>
36103a8feaSLen Brown 
37103a8feaSLen Brown char *proc_stat = "/proc/stat";
38b7d8c148SLen Brown FILE *outf;
3936229897SLen Brown int *fd_percpu;
40b9ad8ee0SLen Brown struct timeval interval_tv = {5, 0};
4147936f94SArtem Bityutskiy struct timespec interval_ts = {5, 0};
42b9ad8ee0SLen Brown struct timespec one_msec = {0, 1000000};
43023fe0acSChen Yu unsigned int num_iterations;
44d8af6f5fSLen Brown unsigned int debug;
4596e47158SLen Brown unsigned int quiet;
463f44a5c6SLen Brown unsigned int shown;
470de6c0dfSLen Brown unsigned int sums_need_wide_columns;
48d8af6f5fSLen Brown unsigned int rapl_joules;
49d8af6f5fSLen Brown unsigned int summary_only;
50c8ade361SLen Brown unsigned int list_header_only;
51d8af6f5fSLen Brown unsigned int dump_only;
52103a8feaSLen Brown unsigned int do_snb_cstates;
53fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
54144b44b1SLen Brown unsigned int do_slm_cstates;
55144b44b1SLen Brown unsigned int use_c1_residency_msr;
56103a8feaSLen Brown unsigned int has_aperf;
57889facbeSLen Brown unsigned int has_epb;
585a63426eSLen Brown unsigned int do_irtl_snb;
595a63426eSLen Brown unsigned int do_irtl_hsw;
60fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
61103a8feaSLen Brown unsigned int genuine_intel;
6234041551SLen Brown unsigned int authentic_amd;
6334041551SLen Brown unsigned int max_level, max_extended_level;
64103a8feaSLen Brown unsigned int has_invariant_tsc;
65d7899447SLen Brown unsigned int do_nhm_platform_info;
66cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
67b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
68103a8feaSLen Brown double bclk;
69a2b7b749SLen Brown double base_hz;
7021ed5574SLen Brown unsigned int has_base_hz;
71a2b7b749SLen Brown double tsc_tweak = 1.0;
72c98d5d94SLen Brown unsigned int show_pkg_only;
73c98d5d94SLen Brown unsigned int show_core_only;
74c98d5d94SLen Brown char *output_buffer, *outp;
75889facbeSLen Brown unsigned int do_rapl;
76889facbeSLen Brown unsigned int do_dts;
77889facbeSLen Brown unsigned int do_ptm;
78fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
79be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
80be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
8127d47356SLen Brown unsigned int gfx_cur_mhz;
82889facbeSLen Brown unsigned int tcc_activation_temp;
83889facbeSLen Brown unsigned int tcc_activation_temp_override;
8440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
8540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
86889facbeSLen Brown double rapl_joule_counter_range;
873a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
88ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
893a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
903a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
918a5bdf41SLen Brown unsigned int crystal_hz;
928a5bdf41SLen Brown unsigned long long tsc_hz;
937ce7d5deSPrarit Bhargava int base_cpu;
9421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
957f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
967f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
977f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
987f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
997f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1007f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
10133148d67SLen Brown unsigned int has_misc_feature_control;
1024c2122d4SLen Brown unsigned int first_counter_read = 1;
103889facbeSLen Brown 
104889facbeSLen Brown #define RAPL_PKG		(1 << 0)
105e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
106e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
107e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
108e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
109e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
110e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
111e6f9bb3cSLen Brown 
112889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
113e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
114e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
115e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
116e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1170b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1180b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
119e6f9bb3cSLen Brown 
1209148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
121e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1220b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
123e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
124e6f9bb3cSLen Brown 
1250b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
126e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
127e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
128e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1299148494cSJacob Pan 
1309148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1319148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1329392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY	(1 << 10)
1339392bd98SCalvin Walton 					/* Indicates cores energy collection is per-core,
1349392bd98SCalvin Walton 					 * not per-package. */
1359392bd98SCalvin Walton #define RAPL_AMD_F17H		(1 << 11)
1369392bd98SCalvin Walton 					/* 0xc0010299 MSR_RAPL_PWR_UNIT */
1379392bd98SCalvin Walton 					/* 0xc001029a MSR_CORE_ENERGY_STAT */
1389392bd98SCalvin Walton 					/* 0xc001029b MSR_PKG_ENERGY_STAT */
1399148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
140889facbeSLen Brown #define	TJMAX_DEFAULT	100
141889facbeSLen Brown 
1429392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */
1439392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT	0xc0010299
1449392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT	0xc001029a
1459392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT	0xc001029b
1469392bd98SCalvin Walton 
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;
1909392bd98SCalvin Walton 	unsigned int core_energy;	/* MSR_CORE_ENERGY_STAT */
191c98d5d94SLen Brown 	unsigned int core_id;
192678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
193c98d5d94SLen Brown } *core_even, *core_odd;
194c98d5d94SLen Brown 
195c98d5d94SLen Brown struct pkg_data {
196c98d5d94SLen Brown 	unsigned long long pc2;
197c98d5d94SLen Brown 	unsigned long long pc3;
198c98d5d94SLen Brown 	unsigned long long pc6;
199c98d5d94SLen Brown 	unsigned long long pc7;
200ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
201ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
202ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
203be0e54c4SLen Brown 	unsigned long long cpu_lpi;
204be0e54c4SLen Brown 	unsigned long long sys_lpi;
2050b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
2060b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
2070b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
2080b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
2099185e988SLen Brown 	long long gfx_rc6_ms;
21027d47356SLen Brown 	unsigned int gfx_mhz;
211c98d5d94SLen Brown 	unsigned int package_id;
212889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
213889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
214889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
215889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
216889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
217889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
218889facbeSLen Brown 	unsigned int pkg_temp_c;
219678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
220c98d5d94SLen Brown } *package_even, *package_odd;
221c98d5d94SLen Brown 
222c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
223c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
224c98d5d94SLen Brown 
22540f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
22640f5cfe7SPrarit Bhargava 	((thread_base) +						      \
22740f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
22840f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
22940f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
23040f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
23140f5cfe7SPrarit Bhargava 	 (thread_no))
23240f5cfe7SPrarit Bhargava 
23340f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
23440f5cfe7SPrarit Bhargava 	((core_base) +							\
23540f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
23640f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
23740f5cfe7SPrarit Bhargava 	 (core_no))
23840f5cfe7SPrarit Bhargava 
23940f5cfe7SPrarit Bhargava 
240c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
241c98d5d94SLen Brown 
242388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
24341618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
244388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
245388e9c81SLen Brown 
246388e9c81SLen Brown struct msr_counter {
247388e9c81SLen Brown 	unsigned int msr_num;
248388e9c81SLen Brown 	char name[NAME_BYTES];
249495c7654SLen Brown 	char path[PATH_BYTES];
250388e9c81SLen Brown 	unsigned int width;
251388e9c81SLen Brown 	enum counter_type type;
252388e9c81SLen Brown 	enum counter_format format;
253388e9c81SLen Brown 	struct msr_counter *next;
254812db3f7SLen Brown 	unsigned int flags;
255812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
256812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
25741618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
258388e9c81SLen Brown };
259388e9c81SLen Brown 
260388e9c81SLen Brown struct sys_counters {
261678a3bd1SLen Brown 	unsigned int added_thread_counters;
262678a3bd1SLen Brown 	unsigned int added_core_counters;
263678a3bd1SLen Brown 	unsigned int added_package_counters;
264388e9c81SLen Brown 	struct msr_counter *tp;
265388e9c81SLen Brown 	struct msr_counter *cp;
266388e9c81SLen Brown 	struct msr_counter *pp;
267388e9c81SLen Brown } sys;
268388e9c81SLen Brown 
269c98d5d94SLen Brown struct system_summary {
270c98d5d94SLen Brown 	struct thread_data threads;
271c98d5d94SLen Brown 	struct core_data cores;
272c98d5d94SLen Brown 	struct pkg_data packages;
273388e9c81SLen Brown } average;
274c98d5d94SLen Brown 
2750e2d8f05SLen Brown struct cpu_topology {
2760e2d8f05SLen Brown 	int physical_package_id;
2776de68fe1SLen Brown 	int die_id;
2780e2d8f05SLen Brown 	int logical_cpu_id;
279ef605741SPrarit Bhargava 	int physical_node_id;
280ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
2810e2d8f05SLen Brown 	int physical_core_id;
2828cb48b32SPrarit Bhargava 	int thread_id;
2830e2d8f05SLen Brown 	cpu_set_t *put_ids; /* Processing Unit/Thread IDs */
2840e2d8f05SLen Brown } *cpus;
285c98d5d94SLen Brown 
286c98d5d94SLen Brown struct topo_params {
287c98d5d94SLen Brown 	int num_packages;
2886de68fe1SLen Brown 	int num_die;
289103a8feaSLen Brown 	int num_cpus;
290c98d5d94SLen Brown 	int num_cores;
291c98d5d94SLen Brown 	int max_cpu_num;
292ef605741SPrarit Bhargava 	int max_node_num;
29370a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
29470a9c6e8SPrarit Bhargava 	int cores_per_node;
29570a9c6e8SPrarit Bhargava 	int threads_per_core;
296c98d5d94SLen Brown } topo;
297103a8feaSLen Brown 
298c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
299103a8feaSLen Brown 
300562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
301562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
302562a2d37SLen Brown 
303c98d5d94SLen Brown void setup_all_buffers(void);
304103a8feaSLen Brown 
305c98d5d94SLen Brown int cpu_is_not_present(int cpu)
306d15cf7c1SLen Brown {
307c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
308c98d5d94SLen Brown }
309c98d5d94SLen Brown /*
310c98d5d94SLen Brown  * run func(thread, core, package) in topology order
311c98d5d94SLen Brown  * skip non-present cpus
312c98d5d94SLen Brown  */
313c98d5d94SLen Brown 
314c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
315c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
316c98d5d94SLen Brown {
31740f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
318c98d5d94SLen Brown 
319c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
320df2f677dSLen Brown 		for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
32170a9c6e8SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
322c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
32370a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
324c98d5d94SLen Brown 					struct thread_data *t;
325c98d5d94SLen Brown 					struct core_data *c;
326c98d5d94SLen Brown 					struct pkg_data *p;
327c98d5d94SLen Brown 
32840f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
32940f5cfe7SPrarit Bhargava 						       core_no, node_no,
33040f5cfe7SPrarit Bhargava 						       pkg_no);
331c98d5d94SLen Brown 
332c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
333c98d5d94SLen Brown 						continue;
334c98d5d94SLen Brown 
33540f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
33640f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
337c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
338c98d5d94SLen Brown 
339c98d5d94SLen Brown 					retval = func(t, c, p);
340c98d5d94SLen Brown 					if (retval)
341c98d5d94SLen Brown 						return retval;
342c98d5d94SLen Brown 				}
343c98d5d94SLen Brown 			}
344c98d5d94SLen Brown 		}
34540f5cfe7SPrarit Bhargava 	}
346d15cf7c1SLen Brown 	return 0;
347d15cf7c1SLen Brown }
348d15cf7c1SLen Brown 
34988c3281fSLen Brown int cpu_migrate(int cpu)
35088c3281fSLen Brown {
351c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
352c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
353c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
35488c3281fSLen Brown 		return -1;
35588c3281fSLen Brown 	else
35688c3281fSLen Brown 		return 0;
35788c3281fSLen Brown }
35836229897SLen Brown int get_msr_fd(int cpu)
359103a8feaSLen Brown {
360103a8feaSLen Brown 	char pathname[32];
361103a8feaSLen Brown 	int fd;
362103a8feaSLen Brown 
36336229897SLen Brown 	fd = fd_percpu[cpu];
36436229897SLen Brown 
36536229897SLen Brown 	if (fd)
36636229897SLen Brown 		return fd;
36736229897SLen Brown 
368103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
369103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
37015aaa346SLen Brown 	if (fd < 0)
37198481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
372103a8feaSLen Brown 
37336229897SLen Brown 	fd_percpu[cpu] = fd;
37436229897SLen Brown 
37536229897SLen Brown 	return fd;
37636229897SLen Brown }
37736229897SLen Brown 
37836229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
37936229897SLen Brown {
38036229897SLen Brown 	ssize_t retval;
38136229897SLen Brown 
38236229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
38315aaa346SLen Brown 
38498481e79SLen Brown 	if (retval != sizeof *msr)
385cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
38615aaa346SLen Brown 
38715aaa346SLen Brown 	return 0;
388103a8feaSLen Brown }
389103a8feaSLen Brown 
390fc04cc67SLen Brown /*
391bdd5ae3aSLen Brown  * This list matches the column headers, except
392bdd5ae3aSLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
393bdd5ae3aSLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
394bdd5ae3aSLen Brown  *    matching on them for --show and --hide.
395fc04cc67SLen Brown  */
396812db3f7SLen Brown struct msr_counter bic[] = {
3973f44a5c6SLen Brown 	{ 0x0, "usec" },
3983f44a5c6SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
399812db3f7SLen Brown 	{ 0x0, "Package" },
400bdd5ae3aSLen Brown 	{ 0x0, "Node" },
401812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
402bdd5ae3aSLen Brown 	{ 0x0, "Busy%" },
403812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
404812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
405812db3f7SLen Brown 	{ 0x0, "IRQ" },
406495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
407bdd5ae3aSLen Brown 	{ 0x0, "sysfs" },
408812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
409812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
410812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
411812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
412812db3f7SLen Brown 	{ 0x0, "ThreadC" },
413812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
414812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
415812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
416812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
417812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
418812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
419812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
420812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
421812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
4220f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
4230f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
4244bd1f8f2SLen Brown 	{ 0x0, "Pk%pc10" },
425be0e54c4SLen Brown 	{ 0x0, "CPU%LPI" },
426be0e54c4SLen Brown 	{ 0x0, "SYS%LPI" },
427812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
428812db3f7SLen Brown 	{ 0x0, "CorWatt" },
429812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
430812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
431812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
432812db3f7SLen Brown 	{ 0x0, "PKG_%" },
433812db3f7SLen Brown 	{ 0x0, "RAM_%" },
434812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
435812db3f7SLen Brown 	{ 0x0, "Cor_J" },
436812db3f7SLen Brown 	{ 0x0, "GFX_J" },
437812db3f7SLen Brown 	{ 0x0, "RAM_J" },
4380539ba11SLen Brown 	{ 0x0, "Mod%c6" },
439a99d8730SLen Brown 	{ 0x0, "Totl%C0" },
440a99d8730SLen Brown 	{ 0x0, "Any%C0" },
441a99d8730SLen Brown 	{ 0x0, "GFX%C0" },
442a99d8730SLen Brown 	{ 0x0, "CPUGFX%" },
443bdd5ae3aSLen Brown 	{ 0x0, "Core" },
444bdd5ae3aSLen Brown 	{ 0x0, "CPU" },
4454c2122d4SLen Brown 	{ 0x0, "APIC" },
4464c2122d4SLen Brown 	{ 0x0, "X2APIC" },
4476de68fe1SLen Brown 	{ 0x0, "Die" },
448812db3f7SLen Brown };
449812db3f7SLen Brown 
450812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
4513f44a5c6SLen Brown #define	BIC_USEC	(1ULL << 0)
4523f44a5c6SLen Brown #define	BIC_TOD		(1ULL << 1)
4533f44a5c6SLen Brown #define	BIC_Package	(1ULL << 2)
454bdd5ae3aSLen Brown #define	BIC_Node	(1ULL << 3)
455bdd5ae3aSLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
456bdd5ae3aSLen Brown #define	BIC_Busy	(1ULL << 5)
457bdd5ae3aSLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
458bdd5ae3aSLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
459bdd5ae3aSLen Brown #define	BIC_IRQ		(1ULL << 8)
460bdd5ae3aSLen Brown #define	BIC_SMI		(1ULL << 9)
461bdd5ae3aSLen Brown #define	BIC_sysfs	(1ULL << 10)
462bdd5ae3aSLen Brown #define	BIC_CPU_c1	(1ULL << 11)
463bdd5ae3aSLen Brown #define	BIC_CPU_c3	(1ULL << 12)
464bdd5ae3aSLen Brown #define	BIC_CPU_c6	(1ULL << 13)
465bdd5ae3aSLen Brown #define	BIC_CPU_c7	(1ULL << 14)
466bdd5ae3aSLen Brown #define	BIC_ThreadC	(1ULL << 15)
467bdd5ae3aSLen Brown #define	BIC_CoreTmp	(1ULL << 16)
468bdd5ae3aSLen Brown #define	BIC_CoreCnt	(1ULL << 17)
469bdd5ae3aSLen Brown #define	BIC_PkgTmp	(1ULL << 18)
470bdd5ae3aSLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
471bdd5ae3aSLen Brown #define	BIC_GFXMHz	(1ULL << 20)
472bdd5ae3aSLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
473bdd5ae3aSLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
474bdd5ae3aSLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
475bdd5ae3aSLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
476bdd5ae3aSLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
477bdd5ae3aSLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
478bdd5ae3aSLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
479bdd5ae3aSLen Brown #define BIC_CPU_LPI	(1ULL << 28)
480bdd5ae3aSLen Brown #define BIC_SYS_LPI	(1ULL << 29)
481bdd5ae3aSLen Brown #define	BIC_PkgWatt	(1ULL << 30)
482bdd5ae3aSLen Brown #define	BIC_CorWatt	(1ULL << 31)
483bdd5ae3aSLen Brown #define	BIC_GFXWatt	(1ULL << 32)
484bdd5ae3aSLen Brown #define	BIC_PkgCnt	(1ULL << 33)
485bdd5ae3aSLen Brown #define	BIC_RAMWatt	(1ULL << 34)
486bdd5ae3aSLen Brown #define	BIC_PKG__	(1ULL << 35)
487bdd5ae3aSLen Brown #define	BIC_RAM__	(1ULL << 36)
488bdd5ae3aSLen Brown #define	BIC_Pkg_J	(1ULL << 37)
489bdd5ae3aSLen Brown #define	BIC_Cor_J	(1ULL << 38)
490bdd5ae3aSLen Brown #define	BIC_GFX_J	(1ULL << 39)
491bdd5ae3aSLen Brown #define	BIC_RAM_J	(1ULL << 40)
492bdd5ae3aSLen Brown #define	BIC_Mod_c6	(1ULL << 41)
493bdd5ae3aSLen Brown #define	BIC_Totl_c0	(1ULL << 42)
494bdd5ae3aSLen Brown #define	BIC_Any_c0	(1ULL << 43)
495bdd5ae3aSLen Brown #define	BIC_GFX_c0	(1ULL << 44)
496bdd5ae3aSLen Brown #define	BIC_CPUGFX	(1ULL << 45)
497bdd5ae3aSLen Brown #define	BIC_Core	(1ULL << 46)
498bdd5ae3aSLen Brown #define	BIC_CPU		(1ULL << 47)
4994c2122d4SLen Brown #define	BIC_APIC	(1ULL << 48)
5004c2122d4SLen Brown #define	BIC_X2APIC	(1ULL << 49)
5016de68fe1SLen Brown #define	BIC_Die		(1ULL << 50)
502812db3f7SLen Brown 
5034c2122d4SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
5043f44a5c6SLen Brown 
5053f44a5c6SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
5064c2122d4SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
507812db3f7SLen Brown 
508812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
5093f44a5c6SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
510812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
5110f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
512812db3f7SLen Brown 
5133f44a5c6SLen Brown 
514dd778a5eSLen Brown #define MAX_DEFERRED 16
515dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
516dd778a5eSLen Brown int deferred_skip_index;
517dd778a5eSLen Brown 
518dd778a5eSLen Brown /*
519dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
520dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
521dd778a5eSLen Brown  */
522dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
523dd778a5eSLen Brown 
524dd778a5eSLen Brown void help(void)
525dd778a5eSLen Brown {
526dd778a5eSLen Brown 	fprintf(outf,
527dd778a5eSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
528dd778a5eSLen Brown 	"\n"
529dd778a5eSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
530dd778a5eSLen Brown 	"when COMMAND completes.\n"
531dd778a5eSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
532dd778a5eSLen Brown 	"to print statistics, until interrupted.\n"
533cc481650SNathan Ciobanu 	"  -a, --add	add a counter\n"
534dd778a5eSLen Brown 	"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
535cc481650SNathan Ciobanu 	"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
536dd778a5eSLen Brown 	"		  {core | package | j,k,l..m,n-p }\n"
5379ce80578SNathan Ciobanu 	"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
5389ce80578SNathan Ciobanu 	"  -D, --Dump	displays the raw counter values\n"
5399ce80578SNathan Ciobanu 	"  -e, --enable	[all | column]\n"
5409ce80578SNathan Ciobanu 	"		shows all or the specified disabled column\n"
5419ce80578SNathan Ciobanu 	"  -H, --hide [column|column,column,...]\n"
5429ce80578SNathan Ciobanu 	"		hide the specified column(s)\n"
543cc481650SNathan Ciobanu 	"  -i, --interval sec.subsec\n"
544cc481650SNathan Ciobanu 	"		Override default 5-second measurement interval\n"
5459ce80578SNathan Ciobanu 	"  -J, --Joules	displays energy in Joules instead of Watts\n"
546cc481650SNathan Ciobanu 	"  -l, --list	list column headers only\n"
547cc481650SNathan Ciobanu 	"  -n, --num_iterations num\n"
548cc481650SNathan Ciobanu 	"		number of the measurement iterations\n"
549cc481650SNathan Ciobanu 	"  -o, --out file\n"
550cc481650SNathan Ciobanu 	"		create or truncate \"file\" for all output\n"
551cc481650SNathan Ciobanu 	"  -q, --quiet	skip decoding system configuration header\n"
5529ce80578SNathan Ciobanu 	"  -s, --show [column|column,column,...]\n"
5539ce80578SNathan Ciobanu 	"		show only the specified column(s)\n"
5549ce80578SNathan Ciobanu 	"  -S, --Summary\n"
5559ce80578SNathan Ciobanu 	"		limits output to 1-line system summary per interval\n"
5569ce80578SNathan Ciobanu 	"  -T, --TCC temperature\n"
5579ce80578SNathan Ciobanu 	"		sets the Thermal Control Circuit temperature in\n"
5589ce80578SNathan Ciobanu 	"		  degrees Celsius\n"
559cc481650SNathan Ciobanu 	"  -h, --help	print this help message\n"
560cc481650SNathan Ciobanu 	"  -v, --version	print version information\n"
561dd778a5eSLen Brown 	"\n"
562dd778a5eSLen Brown 	"For more help, run \"man turbostat\"\n");
563dd778a5eSLen Brown }
564dd778a5eSLen Brown 
565812db3f7SLen Brown /*
566812db3f7SLen Brown  * bic_lookup
567812db3f7SLen Brown  * for all the strings in comma separate name_list,
568812db3f7SLen Brown  * set the approprate bit in return value.
569812db3f7SLen Brown  */
570dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
571812db3f7SLen Brown {
572812db3f7SLen Brown 	int i;
573812db3f7SLen Brown 	unsigned long long retval = 0;
574812db3f7SLen Brown 
575812db3f7SLen Brown 	while (name_list) {
576812db3f7SLen Brown 		char *comma;
577812db3f7SLen Brown 
578812db3f7SLen Brown 		comma = strchr(name_list, ',');
579812db3f7SLen Brown 
580812db3f7SLen Brown 		if (comma)
581812db3f7SLen Brown 			*comma = '\0';
582812db3f7SLen Brown 
5833f44a5c6SLen Brown 		if (!strcmp(name_list, "all"))
5843f44a5c6SLen Brown 			return ~0;
5853f44a5c6SLen Brown 
586812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
587812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
588812db3f7SLen Brown 				retval |= (1ULL << i);
589812db3f7SLen Brown 				break;
590812db3f7SLen Brown 			}
591812db3f7SLen Brown 		}
592812db3f7SLen Brown 		if (i == MAX_BIC) {
593dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
594812db3f7SLen Brown 				fprintf(stderr, "Invalid counter name: %s\n", name_list);
595812db3f7SLen Brown 				exit(-1);
596812db3f7SLen Brown 			}
597dd778a5eSLen Brown 			deferred_skip_names[deferred_skip_index++] = name_list;
598dd778a5eSLen Brown 			if (debug)
599dd778a5eSLen Brown 				fprintf(stderr, "deferred \"%s\"\n", name_list);
600dd778a5eSLen Brown 			if (deferred_skip_index >= MAX_DEFERRED) {
601dd778a5eSLen Brown 				fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
602dd778a5eSLen Brown 					MAX_DEFERRED, name_list);
603dd778a5eSLen Brown 				help();
604dd778a5eSLen Brown 				exit(1);
605dd778a5eSLen Brown 			}
606dd778a5eSLen Brown 		}
607812db3f7SLen Brown 
608812db3f7SLen Brown 		name_list = comma;
609812db3f7SLen Brown 		if (name_list)
610812db3f7SLen Brown 			name_list++;
611812db3f7SLen Brown 
612812db3f7SLen Brown 	}
613812db3f7SLen Brown 	return retval;
614812db3f7SLen Brown }
615fc04cc67SLen Brown 
616dd778a5eSLen Brown 
617c8ade361SLen Brown void print_header(char *delim)
618103a8feaSLen Brown {
619388e9c81SLen Brown 	struct msr_counter *mp;
6206168c2e0SLen Brown 	int printed = 0;
621388e9c81SLen Brown 
6223f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
6233f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
6243f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
6253f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
626812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
6276168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
6286de68fe1SLen Brown 	if (DO_BIC(BIC_Die))
6296de68fe1SLen Brown 		outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
63001235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
63101235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
632812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
6336168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
634812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
6356168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
6364c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
6374c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
6384c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
6394c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
640812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
6416168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
642812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
6436168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
644812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
6456168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
646812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
6476168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
6481cc21f7bSLen Brown 
6490de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
6500de6c0dfSLen Brown 		if (sums_need_wide_columns)
6516168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
6520de6c0dfSLen Brown 		else
6536168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
6540de6c0dfSLen Brown 	}
6550de6c0dfSLen Brown 
656812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
6576168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
6581cc21f7bSLen Brown 
659388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
660dd778a5eSLen Brown 
661388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
662388e9c81SLen Brown 			if (mp->width == 64)
663dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
664388e9c81SLen Brown 			else
665dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
666388e9c81SLen Brown 		} else {
6670de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
668dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
6690de6c0dfSLen Brown 			else
670dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
671388e9c81SLen Brown 		}
672388e9c81SLen Brown 	}
673388e9c81SLen Brown 
67441618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
6756168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
676562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
6776168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
678812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
6796168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
680812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
6816168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
682678a3bd1SLen Brown 
6830539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
6846168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
685678a3bd1SLen Brown 
686812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
6876168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
688388e9c81SLen Brown 
6899392bd98SCalvin Walton 	if (do_rapl && !rapl_joules) {
6909392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
6919392bd98SCalvin Walton 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
6929392bd98SCalvin Walton 	} else if (do_rapl && rapl_joules) {
6939392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
6949392bd98SCalvin Walton 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
6959392bd98SCalvin Walton 	}
6969392bd98SCalvin Walton 
697388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
698388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
699388e9c81SLen Brown 			if (mp->width == 64)
700c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
701388e9c81SLen Brown 			else
702c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
703388e9c81SLen Brown 		} else {
7040de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7050de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7060de6c0dfSLen Brown 			else
707c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
708388e9c81SLen Brown 		}
709388e9c81SLen Brown 	}
710388e9c81SLen Brown 
711812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
7126168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
713889facbeSLen Brown 
714812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
7156168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
716fdf676e5SLen Brown 
717812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
7186168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
71927d47356SLen Brown 
720a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
7216168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
722a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
7236168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
724a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
7256168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
726a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
7276168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
7280b2bb692SLen Brown 
7290f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
7306168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
7310f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
7326168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
7330f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
7346168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
7350f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
7366168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
7370f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
7386168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
7390f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
7406168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
7410f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
7426168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
743be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
744be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
745be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
746be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
747103a8feaSLen Brown 
7485c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
749812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
7506168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
7519392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
7526168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
753812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
7546168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
755812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
7566168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
757812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7586168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
759812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7606168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
761d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
762812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
7636168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
7649392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
7656168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
766812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
7676168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
768812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
7696168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
770812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
7716168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
772812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
7736168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
7745c56be9aSDirk Brandewie 	}
775388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
776388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
777388e9c81SLen Brown 			if (mp->width == 64)
778c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
779388e9c81SLen Brown 			else
780c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
781388e9c81SLen Brown 		} else {
7820de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
7830de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
7840de6c0dfSLen Brown 			else
785c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
786388e9c81SLen Brown 		}
787388e9c81SLen Brown 	}
788388e9c81SLen Brown 
789c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
790103a8feaSLen Brown }
791103a8feaSLen Brown 
792c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
793c98d5d94SLen Brown 	struct pkg_data *p)
794103a8feaSLen Brown {
795388e9c81SLen Brown 	int i;
796388e9c81SLen Brown 	struct msr_counter *mp;
797388e9c81SLen Brown 
7983b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
799c98d5d94SLen Brown 
800c98d5d94SLen Brown 	if (t) {
8013b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
8023b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
8033b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
8043b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
8053b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
8063b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
8076886fee4SLen Brown 
808812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
8090de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
810812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
811218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
812388e9c81SLen Brown 
813388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
814388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
815388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
816388e9c81SLen Brown 		}
817103a8feaSLen Brown 	}
818103a8feaSLen Brown 
819c98d5d94SLen Brown 	if (c) {
8203b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
8213b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
8223b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
8233b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
8243b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
8259392bd98SCalvin Walton 		outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
826388e9c81SLen Brown 
827388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
828388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
829388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
830388e9c81SLen Brown 		}
8310539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
832c98d5d94SLen Brown 	}
833103a8feaSLen Brown 
834c98d5d94SLen Brown 	if (p) {
8353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
8360b2bb692SLen Brown 
8370b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
8380b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
8390b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
8400b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
8410b2bb692SLen Brown 
8423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
8430f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
8443b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
8450f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
8463b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
8470f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
8483b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
8493b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
8503b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
8513b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
852be0e54c4SLen Brown 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
853be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
854be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
8553b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
8563b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
8573b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
8583b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
8593b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
8603b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
8613b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
8623b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
8633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
864388e9c81SLen Brown 
865388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
866388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
867388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
868388e9c81SLen Brown 		}
869c98d5d94SLen Brown 	}
8703b4d5c7fSAndy Shevchenko 
8713b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
8723b4d5c7fSAndy Shevchenko 
873c98d5d94SLen Brown 	return 0;
874103a8feaSLen Brown }
875103a8feaSLen Brown 
876e23da037SLen Brown /*
877e23da037SLen Brown  * column formatting convention & formats
878e23da037SLen Brown  */
879c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
880c98d5d94SLen Brown 	struct pkg_data *p)
881103a8feaSLen Brown {
882008d396eSLen Brown 	double interval_float, tsc;
883fc04cc67SLen Brown 	char *fmt8;
884388e9c81SLen Brown 	int i;
885388e9c81SLen Brown 	struct msr_counter *mp;
8866168c2e0SLen Brown 	char *delim = "\t";
8876168c2e0SLen Brown 	int printed = 0;
888103a8feaSLen Brown 
889c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
890c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
891c98d5d94SLen Brown 		return 0;
892c98d5d94SLen Brown 
893c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
894c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
895c98d5d94SLen Brown 		return 0;
896c98d5d94SLen Brown 
8971ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
8981ef7d21aSLen Brown 	if ((t != &average.threads) &&
8991ef7d21aSLen Brown 		(cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
9001ef7d21aSLen Brown 		return 0;
9011ef7d21aSLen Brown 
9023f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
903f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
904f4fdf2b4SLen Brown 		struct timeval tv;
905f4fdf2b4SLen Brown 
906f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
907f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
908f4fdf2b4SLen Brown 	}
909f4fdf2b4SLen Brown 
9103f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
9113f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
9123f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
9133f44a5c6SLen Brown 
914103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
915103a8feaSLen Brown 
916008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
917008d396eSLen Brown 
918c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
919c98d5d94SLen Brown 	if (t == &average.threads) {
920812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
9216168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9226de68fe1SLen Brown 		if (DO_BIC(BIC_Die))
9236de68fe1SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
92401235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
92501235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
926812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
9276168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
928812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9296168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9304c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9314c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9324c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9334c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
934103a8feaSLen Brown 	} else {
935812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
936c98d5d94SLen Brown 			if (p)
9376168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
938c98d5d94SLen Brown 			else
9396168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
940c98d5d94SLen Brown 		}
9416de68fe1SLen Brown 		if (DO_BIC(BIC_Die)) {
9426de68fe1SLen Brown 			if (c)
9436de68fe1SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
9446de68fe1SLen Brown 			else
9456de68fe1SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
9466de68fe1SLen Brown 		}
94701235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
94801235041SPrarit Bhargava 			if (t)
94901235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
95001235041SPrarit Bhargava 						(printed++ ? delim : ""),
95101235041SPrarit Bhargava 					      cpus[t->cpu_id].physical_node_id);
95201235041SPrarit Bhargava 			else
95301235041SPrarit Bhargava 				outp += sprintf(outp, "%s-",
95401235041SPrarit Bhargava 						(printed++ ? delim : ""));
95501235041SPrarit Bhargava 		}
956812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
957c98d5d94SLen Brown 			if (c)
9586168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
959c98d5d94SLen Brown 			else
9606168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
961c98d5d94SLen Brown 		}
962812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
9636168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
9644c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
9654c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
9664c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
9674c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
968103a8feaSLen Brown 	}
969fc04cc67SLen Brown 
970812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
9716168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
972fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
973fc04cc67SLen Brown 
974812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
9756168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
976103a8feaSLen Brown 
977812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
97821ed5574SLen Brown 		if (has_base_hz)
9796168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
98021ed5574SLen Brown 		else
9816168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
982008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
98321ed5574SLen Brown 	}
984103a8feaSLen Brown 
985812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
9866168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
987103a8feaSLen Brown 
988562a2d37SLen Brown 	/* IRQ */
9890de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
9900de6c0dfSLen Brown 		if (sums_need_wide_columns)
9916168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
9920de6c0dfSLen Brown 		else
9936168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
9940de6c0dfSLen Brown 	}
995562a2d37SLen Brown 
9961cc21f7bSLen Brown 	/* SMI */
997812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
9986168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
9991cc21f7bSLen Brown 
1000678a3bd1SLen Brown 	/* Added counters */
1001678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1002678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
1003678a3bd1SLen Brown 			if (mp->width == 32)
10045f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
1005678a3bd1SLen Brown 			else
10066168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1007678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10080de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10096168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
10100de6c0dfSLen Brown 			else
10116168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1012678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
101341618e63SLen Brown 			if (mp->type == COUNTER_USEC)
10146168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
101541618e63SLen Brown 			else
10166168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
1017678a3bd1SLen Brown 		}
1018678a3bd1SLen Brown 	}
1019678a3bd1SLen Brown 
102041618e63SLen Brown 	/* C1 */
102141618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
10226168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
102341618e63SLen Brown 
102441618e63SLen Brown 
1025c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1026c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1027c98d5d94SLen Brown 		goto done;
1028c98d5d94SLen Brown 
1029562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
10306168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
1031812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
10326168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
1033812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
10346168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
1035c98d5d94SLen Brown 
10360539ba11SLen Brown 	/* Mod%c6 */
10370539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
10386168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
10390539ba11SLen Brown 
1040812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
10416168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1042889facbeSLen Brown 
1043388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1044388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1045388e9c81SLen Brown 			if (mp->width == 32)
10465f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
1047388e9c81SLen Brown 			else
10486168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1049388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
10500de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10516168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
10520de6c0dfSLen Brown 			else
10536168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1054388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
10556168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
1056388e9c81SLen Brown 		}
1057388e9c81SLen Brown 	}
1058388e9c81SLen Brown 
10599392bd98SCalvin Walton 	/*
10609392bd98SCalvin Walton 	 * If measurement interval exceeds minimum RAPL Joule Counter range,
10619392bd98SCalvin Walton 	 * indicate that results are suspect by printing "**" in fraction place.
10629392bd98SCalvin Walton 	 */
10639392bd98SCalvin Walton 	if (interval_float < rapl_joule_counter_range)
10649392bd98SCalvin Walton 		fmt8 = "%s%.2f";
10659392bd98SCalvin Walton 	else
10669392bd98SCalvin Walton 		fmt8 = "%6.0f**";
10679392bd98SCalvin Walton 
10689392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
10699392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
10709392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
10719392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
10729392bd98SCalvin Walton 
1073c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1074c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1075c98d5d94SLen Brown 		goto done;
1076c98d5d94SLen Brown 
10770b2bb692SLen Brown 	/* PkgTmp */
1078812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
10796168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1080889facbeSLen Brown 
1081fdf676e5SLen Brown 	/* GFXrc6 */
1082812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1083ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
10846168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
10859185e988SLen Brown 		} else {
10866168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
10879185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
10889185e988SLen Brown 		}
10899185e988SLen Brown 	}
1090fdf676e5SLen Brown 
109127d47356SLen Brown 	/* GFXMHz */
1092812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
10936168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
109427d47356SLen Brown 
10950b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1096a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
10976168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
1098a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
10996168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
1100a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
11016168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
1102a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
11036168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
11040b2bb692SLen Brown 
11050f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
11066168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
11070f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
11086168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
11090f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
11106168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
11110f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
11126168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
11130f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
11146168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
11150f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
11166168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
11170f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
11186168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
1119889facbeSLen Brown 
1120be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1121be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1122be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1123be0e54c4SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1124be0e54c4SLen Brown 
1125812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
11266168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
11279392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
11286168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1129812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
11306168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1131812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
11326168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
1133812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
11346168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
11359392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
11366168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1137812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
11386168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1139812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
11406168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1141812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
11426168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1143812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
11446168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1145812db3f7SLen Brown 
1146388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1147388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1148388e9c81SLen Brown 			if (mp->width == 32)
11495f3aea57SLen Brown 				outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1150388e9c81SLen Brown 			else
11516168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1152388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
11530de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
11546168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
11550de6c0dfSLen Brown 			else
11566168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1157388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
11586168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1159388e9c81SLen Brown 		}
1160388e9c81SLen Brown 	}
1161388e9c81SLen Brown 
1162c98d5d94SLen Brown done:
116394d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1164c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1165c98d5d94SLen Brown 
1166c98d5d94SLen Brown 	return 0;
1167103a8feaSLen Brown }
1168103a8feaSLen Brown 
1169b7d8c148SLen Brown void flush_output_stdout(void)
1170103a8feaSLen Brown {
1171b7d8c148SLen Brown 	FILE *filep;
1172b7d8c148SLen Brown 
1173b7d8c148SLen Brown 	if (outf == stderr)
1174b7d8c148SLen Brown 		filep = stdout;
1175b7d8c148SLen Brown 	else
1176b7d8c148SLen Brown 		filep = outf;
1177b7d8c148SLen Brown 
1178b7d8c148SLen Brown 	fputs(output_buffer, filep);
1179b7d8c148SLen Brown 	fflush(filep);
1180b7d8c148SLen Brown 
1181c98d5d94SLen Brown 	outp = output_buffer;
1182c98d5d94SLen Brown }
1183b7d8c148SLen Brown void flush_output_stderr(void)
1184c98d5d94SLen Brown {
1185b7d8c148SLen Brown 	fputs(output_buffer, outf);
1186b7d8c148SLen Brown 	fflush(outf);
1187c98d5d94SLen Brown 	outp = output_buffer;
1188c98d5d94SLen Brown }
1189c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1190c98d5d94SLen Brown {
1191e23da037SLen Brown 	static int printed;
1192103a8feaSLen Brown 
1193e23da037SLen Brown 	if (!printed || !summary_only)
1194c8ade361SLen Brown 		print_header("\t");
1195103a8feaSLen Brown 
11969d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1197103a8feaSLen Brown 
1198e23da037SLen Brown 	printed = 1;
1199e23da037SLen Brown 
1200e23da037SLen Brown 	if (summary_only)
1201e23da037SLen Brown 		return;
1202e23da037SLen Brown 
1203c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1204103a8feaSLen Brown }
1205103a8feaSLen Brown 
1206889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
1207889facbeSLen Brown 	if (new > old) {			\
1208889facbeSLen Brown 		old = new - old;		\
1209889facbeSLen Brown 	} else {				\
1210889facbeSLen Brown 		old = 0x100000000 + new - old;	\
1211889facbeSLen Brown 	}
1212889facbeSLen Brown 
1213ba3dec99SLen Brown int
1214c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
1215103a8feaSLen Brown {
1216388e9c81SLen Brown 	int i;
1217388e9c81SLen Brown 	struct msr_counter *mp;
12180b2bb692SLen Brown 
1219a99d8730SLen Brown 
1220a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
12210b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1222a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
12230b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1224a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
12250b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1226a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
12270b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1228a99d8730SLen Brown 
1229c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
12300f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1231c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
12320f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1233c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
12340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1235c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1236ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1237ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1238ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1239be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1240be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1241889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1242889facbeSLen Brown 
12439185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
12449185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
12459185e988SLen Brown 		old->gfx_rc6_ms = -1;
12469185e988SLen Brown 	else
1247fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
12489185e988SLen Brown 
124927d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
125027d47356SLen Brown 
1251889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1252889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
1253889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1254889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
1255889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1256889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1257ba3dec99SLen Brown 
1258388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1259388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1260388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1261388e9c81SLen Brown 		else
1262388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1263388e9c81SLen Brown 	}
1264388e9c81SLen Brown 
1265ba3dec99SLen Brown 	return 0;
1266103a8feaSLen Brown }
1267103a8feaSLen Brown 
1268c98d5d94SLen Brown void
1269c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1270c98d5d94SLen Brown {
1271388e9c81SLen Brown 	int i;
1272388e9c81SLen Brown 	struct msr_counter *mp;
1273388e9c81SLen Brown 
1274c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1275c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1276c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1277889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
12780539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1279388e9c81SLen Brown 
12809392bd98SCalvin Walton 	DELTA_WRAP32(new->core_energy, old->core_energy);
12819392bd98SCalvin Walton 
1282388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1283388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1284388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1285388e9c81SLen Brown 		else
1286388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1287388e9c81SLen Brown 	}
1288103a8feaSLen Brown }
1289c98d5d94SLen Brown 
1290c3ae331dSLen Brown /*
1291c3ae331dSLen Brown  * old = new - old
1292c3ae331dSLen Brown  */
1293ba3dec99SLen Brown int
1294c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1295c98d5d94SLen Brown 	struct core_data *core_delta)
1296c98d5d94SLen Brown {
1297388e9c81SLen Brown 	int i;
1298388e9c81SLen Brown 	struct msr_counter *mp;
1299388e9c81SLen Brown 
13004c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
13014c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
13024c2122d4SLen Brown 		new->apic_id = old->apic_id;
13034c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
13044c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
13054c2122d4SLen Brown 
13063f44a5c6SLen Brown 	/*
13073f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
13083f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
13093f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
13103f44a5c6SLen Brown 	 */
13113f44a5c6SLen Brown 
13123f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
13133f44a5c6SLen Brown 	old->tv_end = new->tv_end;
13143f44a5c6SLen Brown 
1315c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1316c98d5d94SLen Brown 
1317103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1318b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1319b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1320b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1321b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1322103a8feaSLen Brown 
1323c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1324c98d5d94SLen Brown 
1325812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1326c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1327c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1328c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1329c98d5d94SLen Brown 		} else {
1330ba3dec99SLen Brown 			return -1;
1331103a8feaSLen Brown 		}
1332a729617cSLen Brown 	}
1333103a8feaSLen Brown 
1334c98d5d94SLen Brown 
1335144b44b1SLen Brown 	if (use_c1_residency_msr) {
1336144b44b1SLen Brown 		/*
1337144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1338144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1339144b44b1SLen Brown 		 */
1340144b44b1SLen Brown 	} else {
1341103a8feaSLen Brown 		/*
1342c3ae331dSLen Brown 		 * As counter collection is not atomic,
1343c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1344103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1345103a8feaSLen Brown 		 */
134695149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1347c98d5d94SLen Brown 			old->c1 = 0;
1348c98d5d94SLen Brown 		else {
1349c98d5d94SLen Brown 			/* normal case, derive c1 */
1350008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1351c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1352c98d5d94SLen Brown 		}
1353144b44b1SLen Brown 	}
1354c3ae331dSLen Brown 
1355c98d5d94SLen Brown 	if (old->mperf == 0) {
1356b7d8c148SLen Brown 		if (debug > 1)
1357b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1358c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1359c98d5d94SLen Brown 	}
1360103a8feaSLen Brown 
1361812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1362562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1363562a2d37SLen Brown 
1364812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
13651ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1366ba3dec99SLen Brown 
1367388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1368388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1369388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1370388e9c81SLen Brown 		else
1371388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1372388e9c81SLen Brown 	}
1373ba3dec99SLen Brown 	return 0;
1374103a8feaSLen Brown }
1375c98d5d94SLen Brown 
1376c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1377c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1378c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1379c98d5d94SLen Brown {
1380ba3dec99SLen Brown 	int retval = 0;
1381ba3dec99SLen Brown 
1382c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1383c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1384c98d5d94SLen Brown 		delta_core(c, c2);
1385c98d5d94SLen Brown 
1386c98d5d94SLen Brown 	/* always calculate thread delta */
1387ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1388ba3dec99SLen Brown 	if (retval)
1389ba3dec99SLen Brown 		return retval;
1390c98d5d94SLen Brown 
1391c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1392c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1393ba3dec99SLen Brown 		retval = delta_package(p, p2);
1394c98d5d94SLen Brown 
1395ba3dec99SLen Brown 	return retval;
1396103a8feaSLen Brown }
1397103a8feaSLen Brown 
1398c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1399103a8feaSLen Brown {
1400388e9c81SLen Brown 	int i;
1401388e9c81SLen Brown 	struct msr_counter  *mp;
1402388e9c81SLen Brown 
14033f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
14043f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
14053f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
14063f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
14073f44a5c6SLen Brown 
1408c98d5d94SLen Brown 	t->tsc = 0;
1409c98d5d94SLen Brown 	t->aperf = 0;
1410c98d5d94SLen Brown 	t->mperf = 0;
1411c98d5d94SLen Brown 	t->c1 = 0;
1412103a8feaSLen Brown 
1413562a2d37SLen Brown 	t->irq_count = 0;
1414562a2d37SLen Brown 	t->smi_count = 0;
1415562a2d37SLen Brown 
1416c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1417c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1418c98d5d94SLen Brown 
1419c98d5d94SLen Brown 	c->c3 = 0;
1420c98d5d94SLen Brown 	c->c6 = 0;
1421c98d5d94SLen Brown 	c->c7 = 0;
14220539ba11SLen Brown 	c->mc6_us = 0;
1423889facbeSLen Brown 	c->core_temp_c = 0;
14249392bd98SCalvin Walton 	c->core_energy = 0;
1425c98d5d94SLen Brown 
14260b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
14270b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
14280b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
14290b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
14300b2bb692SLen Brown 
1431c98d5d94SLen Brown 	p->pc2 = 0;
14320f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1433c98d5d94SLen Brown 		p->pc3 = 0;
14340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1435c98d5d94SLen Brown 		p->pc6 = 0;
14360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1437c98d5d94SLen Brown 		p->pc7 = 0;
1438ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1439ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1440ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1441be0e54c4SLen Brown 	p->cpu_lpi = 0;
1442be0e54c4SLen Brown 	p->sys_lpi = 0;
1443889facbeSLen Brown 
1444889facbeSLen Brown 	p->energy_pkg = 0;
1445889facbeSLen Brown 	p->energy_dram = 0;
1446889facbeSLen Brown 	p->energy_cores = 0;
1447889facbeSLen Brown 	p->energy_gfx = 0;
1448889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1449889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1450889facbeSLen Brown 	p->pkg_temp_c = 0;
145127d47356SLen Brown 
1452fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
145327d47356SLen Brown 	p->gfx_mhz = 0;
1454388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1455388e9c81SLen Brown 		t->counter[i] = 0;
1456388e9c81SLen Brown 
1457388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1458388e9c81SLen Brown 		c->counter[i] = 0;
1459388e9c81SLen Brown 
1460388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1461388e9c81SLen Brown 		p->counter[i] = 0;
1462103a8feaSLen Brown }
1463c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1464c98d5d94SLen Brown 	struct pkg_data *p)
1465103a8feaSLen Brown {
1466388e9c81SLen Brown 	int i;
1467388e9c81SLen Brown 	struct msr_counter *mp;
1468388e9c81SLen Brown 
14694c2122d4SLen Brown 	/* copy un-changing apic_id's */
14704c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
14714c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
14724c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
14734c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
14744c2122d4SLen Brown 
14753f44a5c6SLen Brown 	/* remember first tv_begin */
14763f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
14773f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
14783f44a5c6SLen Brown 
14793f44a5c6SLen Brown 	/* remember last tv_end */
14803f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
14813f44a5c6SLen Brown 
1482c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1483c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1484c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1485c98d5d94SLen Brown 	average.threads.c1 += t->c1;
148615aaa346SLen Brown 
1487562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1488562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1489562a2d37SLen Brown 
1490388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1491388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1492388e9c81SLen Brown 			continue;
1493388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1494388e9c81SLen Brown 	}
1495388e9c81SLen Brown 
1496c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1497c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1498c98d5d94SLen Brown 		return 0;
1499c98d5d94SLen Brown 
1500c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1501c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1502c98d5d94SLen Brown 	average.cores.c7 += c->c7;
15030539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1504c98d5d94SLen Brown 
1505889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1506889facbeSLen Brown 
15079392bd98SCalvin Walton 	average.cores.core_energy += c->core_energy;
15089392bd98SCalvin Walton 
1509388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1510388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1511388e9c81SLen Brown 			continue;
1512388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1513388e9c81SLen Brown 	}
1514388e9c81SLen Brown 
1515c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1516c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1517c98d5d94SLen Brown 		return 0;
1518c98d5d94SLen Brown 
1519a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15200b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1521a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15220b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1523a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15240b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1525a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15260b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
15270b2bb692SLen Brown 
1528c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
15290f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1530c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
15310f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1532c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
15330f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1534c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1535ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1536ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1537ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1538c98d5d94SLen Brown 
1539be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1540be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1541be0e54c4SLen Brown 
1542889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1543889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1544889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1545889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1546889facbeSLen Brown 
1547fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
154827d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
154927d47356SLen Brown 
1550889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1551889facbeSLen Brown 
1552889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1553889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1554388e9c81SLen Brown 
1555388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1556388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1557388e9c81SLen Brown 			continue;
1558388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1559388e9c81SLen Brown 	}
1560c98d5d94SLen Brown 	return 0;
1561c98d5d94SLen Brown }
1562c98d5d94SLen Brown /*
1563c98d5d94SLen Brown  * sum the counters for all cpus in the system
1564c98d5d94SLen Brown  * compute the weighted average
1565c98d5d94SLen Brown  */
1566c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1567c98d5d94SLen Brown 	struct pkg_data *p)
1568c98d5d94SLen Brown {
1569388e9c81SLen Brown 	int i;
1570388e9c81SLen Brown 	struct msr_counter *mp;
1571388e9c81SLen Brown 
1572c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1573c98d5d94SLen Brown 
1574c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1575c98d5d94SLen Brown 
1576c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1577c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1578c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1579c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1580c98d5d94SLen Brown 
15810de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
15820de6c0dfSLen Brown 		sums_need_wide_columns = 1;
15830de6c0dfSLen Brown 
1584c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1585c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1586c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
15870539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1588c98d5d94SLen Brown 
1589a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
15900b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1591a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
15920b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1593a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
15940b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1595a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
15960b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
15970b2bb692SLen Brown 
1598c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
15990f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1600c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
16010f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1602c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
16030f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1604c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1605ca58710fSKristen Carlson Accardi 
1606ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1607ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1608ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1609388e9c81SLen Brown 
1610388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1611388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1612388e9c81SLen Brown 			continue;
16130de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16140de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
16150de6c0dfSLen Brown 				sums_need_wide_columns = 1;
161641618e63SLen Brown 			continue;
16170de6c0dfSLen Brown 		}
1618388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1619388e9c81SLen Brown 	}
1620388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1621388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1622388e9c81SLen Brown 			continue;
16230de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16240de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
16250de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16260de6c0dfSLen Brown 		}
1627388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1628388e9c81SLen Brown 	}
1629388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1630388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1631388e9c81SLen Brown 			continue;
16320de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
16330de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
16340de6c0dfSLen Brown 				sums_need_wide_columns = 1;
16350de6c0dfSLen Brown 		}
1636388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1637388e9c81SLen Brown 	}
1638c98d5d94SLen Brown }
1639c98d5d94SLen Brown 
1640c98d5d94SLen Brown static unsigned long long rdtsc(void)
1641c98d5d94SLen Brown {
1642c98d5d94SLen Brown 	unsigned int low, high;
1643c98d5d94SLen Brown 
1644c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1645c98d5d94SLen Brown 
1646c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1647c98d5d94SLen Brown }
1648c98d5d94SLen Brown 
1649c98d5d94SLen Brown /*
1650495c7654SLen Brown  * Open a file, and exit on failure
1651495c7654SLen Brown  */
1652495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1653495c7654SLen Brown {
1654495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1655495c7654SLen Brown 
1656495c7654SLen Brown 	if (!filep)
1657495c7654SLen Brown 		err(1, "%s: open failed", path);
1658495c7654SLen Brown 	return filep;
1659495c7654SLen Brown }
1660495c7654SLen Brown /*
1661495c7654SLen Brown  * snapshot_sysfs_counter()
1662495c7654SLen Brown  *
1663495c7654SLen Brown  * return snapshot of given counter
1664495c7654SLen Brown  */
1665495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1666495c7654SLen Brown {
1667495c7654SLen Brown 	FILE *fp;
1668495c7654SLen Brown 	int retval;
1669495c7654SLen Brown 	unsigned long long counter;
1670495c7654SLen Brown 
1671495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1672495c7654SLen Brown 
1673495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1674495c7654SLen Brown 	if (retval != 1)
1675495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1676495c7654SLen Brown 
1677495c7654SLen Brown 	fclose(fp);
1678495c7654SLen Brown 
1679495c7654SLen Brown 	return counter;
1680495c7654SLen Brown }
1681495c7654SLen Brown 
1682495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1683495c7654SLen Brown {
1684495c7654SLen Brown 	if (mp->msr_num != 0) {
1685495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1686495c7654SLen Brown 			return -1;
1687495c7654SLen Brown 	} else {
168846c27978SLen Brown 		char path[128 + PATH_BYTES];
168941618e63SLen Brown 
169041618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
169141618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
169241618e63SLen Brown 				 cpu, mp->path);
169341618e63SLen Brown 
169441618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
169541618e63SLen Brown 		} else {
1696495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1697495c7654SLen Brown 		}
169841618e63SLen Brown 	}
1699495c7654SLen Brown 
1700495c7654SLen Brown 	return 0;
1701495c7654SLen Brown }
1702495c7654SLen Brown 
17034c2122d4SLen Brown void get_apic_id(struct thread_data *t)
17044c2122d4SLen Brown {
170534041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
170634041551SLen Brown 
170734041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
170834041551SLen Brown 		eax = ebx = ecx = edx = 0;
170934041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
171034041551SLen Brown 
171134041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
171234041551SLen Brown 	}
171334041551SLen Brown 
171434041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
171534041551SLen Brown 		return;
171634041551SLen Brown 
171734041551SLen Brown 	if (authentic_amd) {
171834041551SLen Brown 		unsigned int topology_extensions;
171934041551SLen Brown 
172034041551SLen Brown 		if (max_extended_level < 0x8000001e)
172134041551SLen Brown 			return;
17224c2122d4SLen Brown 
17234c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
172434041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
172534041551SLen Brown 			topology_extensions = ecx & (1 << 22);
172634041551SLen Brown 
172734041551SLen Brown 		if (topology_extensions == 0)
172834041551SLen Brown 			return;
172934041551SLen Brown 
173034041551SLen Brown 		eax = ebx = ecx = edx = 0;
173134041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
173234041551SLen Brown 
173334041551SLen Brown 		t->x2apic_id = eax;
173434041551SLen Brown 		return;
173534041551SLen Brown 	}
17364c2122d4SLen Brown 
17374c2122d4SLen Brown 	if (!genuine_intel)
17384c2122d4SLen Brown 		return;
17394c2122d4SLen Brown 
17404c2122d4SLen Brown 	if (max_level < 0xb)
17414c2122d4SLen Brown 		return;
17424c2122d4SLen Brown 
17434c2122d4SLen Brown 	ecx = 0;
17444c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
17454c2122d4SLen Brown 	t->x2apic_id = edx;
17464c2122d4SLen Brown 
174734041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
174834041551SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n",
174934041551SLen Brown 				t->cpu_id, t->apic_id, t->x2apic_id);
17504c2122d4SLen Brown }
17514c2122d4SLen Brown 
1752495c7654SLen Brown /*
1753c98d5d94SLen Brown  * get_counters(...)
1754c98d5d94SLen Brown  * migrate to cpu
1755c98d5d94SLen Brown  * acquire and record local counters for that cpu
1756c98d5d94SLen Brown  */
1757c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1758c98d5d94SLen Brown {
1759c98d5d94SLen Brown 	int cpu = t->cpu_id;
1760889facbeSLen Brown 	unsigned long long msr;
17610102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1762388e9c81SLen Brown 	struct msr_counter *mp;
1763388e9c81SLen Brown 	int i;
1764c98d5d94SLen Brown 
1765f4fdf2b4SLen Brown 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1766f4fdf2b4SLen Brown 
1767e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1768b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
176915aaa346SLen Brown 		return -1;
1770e52966c0SLen Brown 	}
177115aaa346SLen Brown 
17724c2122d4SLen Brown 	if (first_counter_read)
17734c2122d4SLen Brown 		get_apic_id(t);
17740102b067SLen Brown retry:
1775c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
177615aaa346SLen Brown 
1777812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
17780102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
17790102b067SLen Brown 
17800102b067SLen Brown 		/*
17810102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
17820102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
17830102b067SLen Brown 		 *
17840102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
17850102b067SLen Brown 		 * individual system call, so delays may occur
17860102b067SLen Brown 		 * between them.  If the time to read them
17870102b067SLen Brown 		 * varies by a large amount, we re-read them.
17880102b067SLen Brown 		 */
17890102b067SLen Brown 
17900102b067SLen Brown 		/*
17910102b067SLen Brown 		 * This initial dummy APERF read has been seen to
17920102b067SLen Brown 		 * reduce jitter in the subsequent reads.
17930102b067SLen Brown 		 */
17940102b067SLen Brown 
17959c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1796c98d5d94SLen Brown 			return -3;
17970102b067SLen Brown 
17980102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
17990102b067SLen Brown 
18000102b067SLen Brown 		tsc_before = t->tsc;
18010102b067SLen Brown 
18020102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
18030102b067SLen Brown 			return -3;
18040102b067SLen Brown 
18050102b067SLen Brown 		tsc_between = rdtsc();
18060102b067SLen Brown 
18079c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1808c98d5d94SLen Brown 			return -4;
18090102b067SLen Brown 
18100102b067SLen Brown 		tsc_after = rdtsc();
18110102b067SLen Brown 
18120102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
18130102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
18140102b067SLen Brown 
18150102b067SLen Brown 		/*
18160102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
18170102b067SLen Brown 		 * differ by more than 2x, then try again.
18180102b067SLen Brown 		 */
18190102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
18200102b067SLen Brown 			aperf_mperf_retry_count++;
18210102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
18220102b067SLen Brown 				goto retry;
18230102b067SLen Brown 			else
18240102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
18250102b067SLen Brown 					cpu, aperf_time, mperf_time);
18260102b067SLen Brown 		}
18270102b067SLen Brown 		aperf_mperf_retry_count = 0;
18280102b067SLen Brown 
1829b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1830b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
183188c3281fSLen Brown 	}
183288c3281fSLen Brown 
1833812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1834562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1835812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
18361ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
18371ed51011SLen Brown 			return -5;
18381ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
18391ed51011SLen Brown 	}
18400539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1841144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1842144b44b1SLen Brown 			return -6;
1843144b44b1SLen Brown 	}
1844144b44b1SLen Brown 
1845388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1846495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1847388e9c81SLen Brown 			return -10;
1848388e9c81SLen Brown 	}
1849388e9c81SLen Brown 
1850c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1851c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1852f4fdf2b4SLen Brown 		goto done;
1853c98d5d94SLen Brown 
1854562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3)) {
1855c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1856c98d5d94SLen Brown 			return -6;
1857144b44b1SLen Brown 	}
1858144b44b1SLen Brown 
1859812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1860c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1861c98d5d94SLen Brown 			return -7;
1862fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1863fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1864fb5d4327SDasaratharaman Chandramouli 			return -7;
1865103a8feaSLen Brown 	}
186615aaa346SLen Brown 
1867812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1868c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1869c98d5d94SLen Brown 			return -8;
1870c98d5d94SLen Brown 
18710539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
18720539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
18730539ba11SLen Brown 			return -8;
18740539ba11SLen Brown 
1875812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1876889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1877889facbeSLen Brown 			return -9;
1878889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1879889facbeSLen Brown 	}
1880889facbeSLen Brown 
18819392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
18829392bd98SCalvin Walton 		if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
18839392bd98SCalvin Walton 			return -14;
18849392bd98SCalvin Walton 		c->core_energy = msr & 0xFFFFFFFF;
18859392bd98SCalvin Walton 	}
18869392bd98SCalvin Walton 
1887388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1888495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1889388e9c81SLen Brown 			return -10;
1890388e9c81SLen Brown 	}
1891889facbeSLen Brown 
1892c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1893c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1894f4fdf2b4SLen Brown 		goto done;
189515aaa346SLen Brown 
1896a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
18970b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
18980b2bb692SLen Brown 			return -10;
1899a99d8730SLen Brown 	}
1900a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
19010b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
19020b2bb692SLen Brown 			return -11;
1903a99d8730SLen Brown 	}
1904a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
19050b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
19060b2bb692SLen Brown 			return -12;
1907a99d8730SLen Brown 	}
1908a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
19090b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
19100b2bb692SLen Brown 			return -13;
19110b2bb692SLen Brown 	}
19120f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1913c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1914c98d5d94SLen Brown 			return -9;
19150f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
19160539ba11SLen Brown 		if (do_slm_cstates) {
19170539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
19180539ba11SLen Brown 				return -10;
19190539ba11SLen Brown 		} else {
1920c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1921c98d5d94SLen Brown 				return -10;
19220539ba11SLen Brown 		}
19230539ba11SLen Brown 	}
19240539ba11SLen Brown 
19250f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1926c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1927c98d5d94SLen Brown 			return -11;
19280f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1929c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1930c98d5d94SLen Brown 			return -12;
19310f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1932ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1933ca58710fSKristen Carlson Accardi 			return -13;
19340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1935ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1936ca58710fSKristen Carlson Accardi 			return -13;
19370f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1938ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1939ca58710fSKristen Carlson Accardi 			return -13;
19400f47c08dSLen Brown 
1941be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
1942be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
1943be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
1944be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
1945be0e54c4SLen Brown 
1946889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1947889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1948889facbeSLen Brown 			return -13;
1949889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1950889facbeSLen Brown 	}
19519148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1952889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1953889facbeSLen Brown 			return -14;
1954889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1955889facbeSLen Brown 	}
1956889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1957889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1958889facbeSLen Brown 			return -15;
1959889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1960889facbeSLen Brown 	}
1961889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1962889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1963889facbeSLen Brown 			return -16;
1964889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1965889facbeSLen Brown 	}
1966889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1967889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1968889facbeSLen Brown 			return -16;
1969889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1970889facbeSLen Brown 	}
1971889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1972889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1973889facbeSLen Brown 			return -16;
1974889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1975889facbeSLen Brown 	}
19763316f99aSCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
19773316f99aSCalvin Walton 		if (get_msr(cpu, MSR_PKG_ENERGY_STAT, &msr))
19783316f99aSCalvin Walton 			return -13;
19793316f99aSCalvin Walton 		p->energy_pkg = msr & 0xFFFFFFFF;
19803316f99aSCalvin Walton 	}
1981812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1982889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1983889facbeSLen Brown 			return -17;
1984889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1985889facbeSLen Brown 	}
1986fdf676e5SLen Brown 
1987812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1988fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1989fdf676e5SLen Brown 
1990812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
199127d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
199227d47356SLen Brown 
1993388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1994495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1995388e9c81SLen Brown 			return -10;
1996388e9c81SLen Brown 	}
1997f4fdf2b4SLen Brown done:
1998f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
1999388e9c81SLen Brown 
200015aaa346SLen Brown 	return 0;
2001103a8feaSLen Brown }
2002103a8feaSLen Brown 
2003ee7e38e3SLen Brown /*
2004ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2005ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
2006ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2007ee7e38e3SLen Brown  */
2008ee7e38e3SLen Brown 
2009ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
2010ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
2011ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
2012ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
2013ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
2014ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
2015ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
2016ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
2017ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
2018ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
2019ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
2020ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
20210b2bb692SLen Brown #define PCL__8 12 /* PC8 */
20220b2bb692SLen Brown #define PCL__9 13 /* PC9 */
2023445640a5SLen Brown #define PCL_10 14 /* PC10 */
2024445640a5SLen Brown #define PCLUNL 15 /* Unlimited */
2025ee7e38e3SLen Brown 
2026ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2027ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
2028445640a5SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"};
2029ee7e38e3SLen Brown 
2030e9257f5fSLen 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};
2031e9257f5fSLen 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};
2032e9257f5fSLen 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};
20330539ba11SLen 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};
2034f2642888SLen 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};
2035e9257f5fSLen 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};
2036445640a5SLen Brown int glm_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
20372085e124SArtem 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};
2038ee7e38e3SLen Brown 
2039a2b7b749SLen Brown 
2040a2b7b749SLen Brown static void
2041a2b7b749SLen Brown calculate_tsc_tweak()
2042a2b7b749SLen Brown {
2043a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
2044a2b7b749SLen Brown }
2045a2b7b749SLen Brown 
2046fcd17211SLen Brown static void
2047fcd17211SLen Brown dump_nhm_platform_info(void)
2048103a8feaSLen Brown {
2049103a8feaSLen Brown 	unsigned long long msr;
2050103a8feaSLen Brown 	unsigned int ratio;
2051103a8feaSLen Brown 
2052ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2053103a8feaSLen Brown 
2054b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
20556574a5d5SLen Brown 
2056103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
2057710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
2058103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2059103a8feaSLen Brown 
2060103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
2061710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2062103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
2063103a8feaSLen Brown 
20647ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2065b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2066bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
206767920418SLen Brown 
2068fcd17211SLen Brown 	return;
2069fcd17211SLen Brown }
2070fcd17211SLen Brown 
2071fcd17211SLen Brown static void
2072fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
2073fcd17211SLen Brown {
2074fcd17211SLen Brown 	unsigned long long msr;
2075fcd17211SLen Brown 	unsigned int ratio;
2076fcd17211SLen Brown 
20777ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2078fcd17211SLen Brown 
2079b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2080fcd17211SLen Brown 
2081fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2082fcd17211SLen Brown 	if (ratio)
2083710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
2084fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2085fcd17211SLen Brown 
2086fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2087fcd17211SLen Brown 	if (ratio)
2088710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
2089fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
2090fcd17211SLen Brown 	return;
2091fcd17211SLen Brown }
2092fcd17211SLen Brown 
2093fcd17211SLen Brown static void
2094fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
2095fcd17211SLen Brown {
2096fcd17211SLen Brown 	unsigned long long msr;
2097fcd17211SLen Brown 	unsigned int ratio;
20986574a5d5SLen Brown 
20997ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
21006574a5d5SLen Brown 
2101b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
21026574a5d5SLen Brown 
21036574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
21046574a5d5SLen Brown 	if (ratio)
2105710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
21066574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21076574a5d5SLen Brown 
21086574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
21096574a5d5SLen Brown 	if (ratio)
2110710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
21116574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21126574a5d5SLen Brown 
21136574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
21146574a5d5SLen Brown 	if (ratio)
2115710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
21166574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21176574a5d5SLen Brown 
21186574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
21196574a5d5SLen Brown 	if (ratio)
2120710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
21216574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21226574a5d5SLen Brown 
21236574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
21246574a5d5SLen Brown 	if (ratio)
2125710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
21266574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21276574a5d5SLen Brown 
21286574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
21296574a5d5SLen Brown 	if (ratio)
2130710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
21316574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21326574a5d5SLen Brown 
21336574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
21346574a5d5SLen Brown 	if (ratio)
2135710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
21366574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
21376574a5d5SLen Brown 
21386574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
21396574a5d5SLen Brown 	if (ratio)
2140710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
21416574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
2142103a8feaSLen Brown 	return;
2143fcd17211SLen Brown }
214431e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
214531e07522SLen Brown {
214631e07522SLen Brown 
214731e07522SLen Brown 	if (!genuine_intel)
214831e07522SLen Brown 		return 0;
214931e07522SLen Brown 
215031e07522SLen Brown 	switch (model) {
215131e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
215231e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
2153f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:
215431e07522SLen Brown 		return 1;
215531e07522SLen Brown 	}
215631e07522SLen Brown 	return 0;
215731e07522SLen Brown }
2158fcd17211SLen Brown 
2159fcd17211SLen Brown static void
216031e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
2161fcd17211SLen Brown {
216231e07522SLen Brown 	unsigned long long msr, core_counts;
216331e07522SLen Brown 	unsigned int ratio, group_size;
2164103a8feaSLen Brown 
21657ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2166b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
21676574a5d5SLen Brown 
216831e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
216931e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
217031e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
217131e07522SLen Brown 	} else {
217231e07522SLen Brown 		core_counts = 0x0807060504030201;
217331e07522SLen Brown 	}
217431e07522SLen Brown 
21756574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
217631e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
21776574a5d5SLen Brown 	if (ratio)
217831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
217931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21806574a5d5SLen Brown 
21816574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
218231e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
21836574a5d5SLen Brown 	if (ratio)
218431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
218531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21866574a5d5SLen Brown 
21876574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
218831e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
21896574a5d5SLen Brown 	if (ratio)
219031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
219131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21926574a5d5SLen Brown 
21936574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
219431e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
21956574a5d5SLen Brown 	if (ratio)
219631e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
219731e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
21986574a5d5SLen Brown 
2199103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
220031e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2201103a8feaSLen Brown 	if (ratio)
220231e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220331e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2204103a8feaSLen Brown 
2205103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
220631e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2207103a8feaSLen Brown 	if (ratio)
220831e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
220931e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2210103a8feaSLen Brown 
2211103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
221231e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2213103a8feaSLen Brown 	if (ratio)
221431e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
221531e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2216103a8feaSLen Brown 
2217103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
221831e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2219103a8feaSLen Brown 	if (ratio)
222031e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
222131e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2222fcd17211SLen Brown 	return;
2223fcd17211SLen Brown }
22243a9a941dSLen Brown 
2225fcd17211SLen Brown static void
22260f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
22270f7887c4SLen Brown {
22280f7887c4SLen Brown 	unsigned long long msr;
22290f7887c4SLen Brown 	unsigned int ratio;
22300f7887c4SLen Brown 
22310f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
22320f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22330f7887c4SLen Brown 
22340f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22350f7887c4SLen Brown 	if (ratio)
22360f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
22370f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22380f7887c4SLen Brown 
22390f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22400f7887c4SLen Brown 	if (ratio)
22410f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
22420f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22430f7887c4SLen Brown 
22440f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22450f7887c4SLen Brown 	if (ratio)
22460f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
22470f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22480f7887c4SLen Brown 
22490f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
22500f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
22510f7887c4SLen Brown 
22520f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
22530f7887c4SLen Brown 	if (ratio)
22540f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
22550f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22560f7887c4SLen Brown 
22570f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
22580f7887c4SLen Brown 	if (ratio)
22590f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
22600f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22610f7887c4SLen Brown 
22620f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
22630f7887c4SLen Brown 	if (ratio)
22640f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
22650f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22660f7887c4SLen Brown 
22670f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
22680f7887c4SLen Brown 	if (ratio)
22690f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
22700f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
22710f7887c4SLen Brown }
22720f7887c4SLen Brown 
22730f7887c4SLen Brown static void
2274fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
2275fb5d4327SDasaratharaman Chandramouli {
2276cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2277cbf97abaSHubert Chrzaniuk 
2278fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2279cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2280cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2281cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2282cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2283fb5d4327SDasaratharaman Chandramouli 
2284ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2285fb5d4327SDasaratharaman Chandramouli 
2286b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2287bfae2052SLen Brown 		base_cpu, msr);
2288fb5d4327SDasaratharaman Chandramouli 
2289fb5d4327SDasaratharaman Chandramouli 	/**
2290fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2291cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2292cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2293fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2294fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2295fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2296fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2297fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2298fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2299fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2300fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2301fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2302fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2303fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2304fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2305fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2306fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2307fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2308fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2309fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2310fb5d4327SDasaratharaman Chandramouli 	 */
2311fb5d4327SDasaratharaman Chandramouli 
2312cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2313cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2314cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2315cbf97abaSHubert Chrzaniuk 
2316cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2317fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2318cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2319fb5d4327SDasaratharaman Chandramouli 
2320cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2321cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2322cbf97abaSHubert Chrzaniuk 		b_nr++;
2323fb5d4327SDasaratharaman Chandramouli 	}
2324cbf97abaSHubert Chrzaniuk 
2325cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2326cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2327b7d8c148SLen Brown 			fprintf(outf,
2328710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2329cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2330fb5d4327SDasaratharaman Chandramouli }
2331fb5d4327SDasaratharaman Chandramouli 
2332fb5d4327SDasaratharaman Chandramouli static void
2333fcd17211SLen Brown dump_nhm_cst_cfg(void)
2334fcd17211SLen Brown {
2335fcd17211SLen Brown 	unsigned long long msr;
2336fcd17211SLen Brown 
23371df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2338fcd17211SLen Brown 
23391df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2340fcd17211SLen Brown 
23413e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2342fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2343fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2344fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2345fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2346fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
23476c34f160SLen Brown 		(unsigned int)msr & 0xF,
2348fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
2349ac980e13SArtem Bityutskiy 
2350ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2351ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
2352ac980e13SArtem Bityutskiy 		fprintf(outf, ", automatic c-state conversion=%s",
2353ac980e13SArtem Bityutskiy 			(msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2354ac980e13SArtem Bityutskiy 	}
2355ac980e13SArtem Bityutskiy 
2356ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2357ac980e13SArtem Bityutskiy 
2358fcd17211SLen Brown 	return;
2359103a8feaSLen Brown }
2360103a8feaSLen Brown 
23616fb3143bSLen Brown static void
23626fb3143bSLen Brown dump_config_tdp(void)
23636fb3143bSLen Brown {
23646fb3143bSLen Brown 	unsigned long long msr;
23656fb3143bSLen Brown 
23666fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2367b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2368685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
23696fb3143bSLen Brown 
23706fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2371b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
23726fb3143bSLen Brown 	if (msr) {
2373685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2374685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2375685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2376685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
23776fb3143bSLen Brown 	}
2378b7d8c148SLen Brown 	fprintf(outf, ")\n");
23796fb3143bSLen Brown 
23806fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2381b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
23826fb3143bSLen Brown 	if (msr) {
2383685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2384685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2385685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2386685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
23876fb3143bSLen Brown 	}
2388b7d8c148SLen Brown 	fprintf(outf, ")\n");
23896fb3143bSLen Brown 
23906fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2391b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
23926fb3143bSLen Brown 	if ((msr) & 0x3)
2393b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2394b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2395b7d8c148SLen Brown 	fprintf(outf, ")\n");
23966fb3143bSLen Brown 
23976fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2398b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2399685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2400b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2401b7d8c148SLen Brown 	fprintf(outf, ")\n");
24026fb3143bSLen Brown }
24035a63426eSLen Brown 
24045a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
24055a63426eSLen Brown 
24065a63426eSLen Brown void print_irtl(void)
24075a63426eSLen Brown {
24085a63426eSLen Brown 	unsigned long long msr;
24095a63426eSLen Brown 
24105a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
24115a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
24125a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24135a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24145a63426eSLen Brown 
24155a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
24165a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
24175a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24185a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24195a63426eSLen Brown 
24205a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
24215a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
24225a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24235a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24245a63426eSLen Brown 
24255a63426eSLen Brown 	if (!do_irtl_hsw)
24265a63426eSLen Brown 		return;
24275a63426eSLen Brown 
24285a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
24295a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
24305a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24315a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24325a63426eSLen Brown 
24335a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
24345a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
24355a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24365a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24375a63426eSLen Brown 
24385a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
24395a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
24405a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
24415a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
24425a63426eSLen Brown 
24435a63426eSLen Brown }
244436229897SLen Brown void free_fd_percpu(void)
244536229897SLen Brown {
244636229897SLen Brown 	int i;
244736229897SLen Brown 
244801a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
244936229897SLen Brown 		if (fd_percpu[i] != 0)
245036229897SLen Brown 			close(fd_percpu[i]);
245136229897SLen Brown 	}
245236229897SLen Brown 
245336229897SLen Brown 	free(fd_percpu);
24546fb3143bSLen Brown }
24556fb3143bSLen Brown 
2456c98d5d94SLen Brown void free_all_buffers(void)
2457103a8feaSLen Brown {
24580e2d8f05SLen Brown 	int i;
24590e2d8f05SLen Brown 
2460c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2461c98d5d94SLen Brown 	cpu_present_set = NULL;
246236229897SLen Brown 	cpu_present_setsize = 0;
2463103a8feaSLen Brown 
2464c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2465c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2466c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2467103a8feaSLen Brown 
2468c98d5d94SLen Brown 	free(thread_even);
2469c98d5d94SLen Brown 	free(core_even);
2470c98d5d94SLen Brown 	free(package_even);
2471103a8feaSLen Brown 
2472c98d5d94SLen Brown 	thread_even = NULL;
2473c98d5d94SLen Brown 	core_even = NULL;
2474c98d5d94SLen Brown 	package_even = NULL;
2475103a8feaSLen Brown 
2476c98d5d94SLen Brown 	free(thread_odd);
2477c98d5d94SLen Brown 	free(core_odd);
2478c98d5d94SLen Brown 	free(package_odd);
2479103a8feaSLen Brown 
2480c98d5d94SLen Brown 	thread_odd = NULL;
2481c98d5d94SLen Brown 	core_odd = NULL;
2482c98d5d94SLen Brown 	package_odd = NULL;
2483103a8feaSLen Brown 
2484c98d5d94SLen Brown 	free(output_buffer);
2485c98d5d94SLen Brown 	output_buffer = NULL;
2486c98d5d94SLen Brown 	outp = NULL;
248736229897SLen Brown 
248836229897SLen Brown 	free_fd_percpu();
2489562a2d37SLen Brown 
2490562a2d37SLen Brown 	free(irq_column_2_cpu);
2491562a2d37SLen Brown 	free(irqs_per_cpu);
24920e2d8f05SLen Brown 
24930e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
24940e2d8f05SLen Brown 		if (cpus[i].put_ids)
24950e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
24960e2d8f05SLen Brown 	}
24970e2d8f05SLen Brown 	free(cpus);
2498103a8feaSLen Brown }
2499103a8feaSLen Brown 
250057a42a34SJosh Triplett 
250157a42a34SJosh Triplett /*
250295aebc44SJosh Triplett  * Parse a file containing a single int.
25036de68fe1SLen Brown  * Return 0 if file can not be opened
25046de68fe1SLen Brown  * Exit if file can be opened, but can not be parsed
250595aebc44SJosh Triplett  */
250695aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
250795aebc44SJosh Triplett {
250895aebc44SJosh Triplett 	va_list args;
250995aebc44SJosh Triplett 	char path[PATH_MAX];
251095aebc44SJosh Triplett 	FILE *filep;
251195aebc44SJosh Triplett 	int value;
251295aebc44SJosh Triplett 
251395aebc44SJosh Triplett 	va_start(args, fmt);
251495aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
251595aebc44SJosh Triplett 	va_end(args);
25166de68fe1SLen Brown 	filep = fopen(path, "r");
25176de68fe1SLen Brown 	if (!filep)
25186de68fe1SLen Brown 		return 0;
2519b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2520b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
252195aebc44SJosh Triplett 	fclose(filep);
252295aebc44SJosh Triplett 	return value;
252395aebc44SJosh Triplett }
252495aebc44SJosh Triplett 
252595aebc44SJosh Triplett /*
2526c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2527c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2528c98d5d94SLen Brown  */
2529c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2530c98d5d94SLen Brown {
253195aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2532103a8feaSLen Brown }
2533103a8feaSLen Brown 
2534103a8feaSLen Brown int get_physical_package_id(int cpu)
2535103a8feaSLen Brown {
253695aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2537103a8feaSLen Brown }
2538103a8feaSLen Brown 
25396de68fe1SLen Brown int get_die_id(int cpu)
25406de68fe1SLen Brown {
25416de68fe1SLen Brown 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
25426de68fe1SLen Brown }
25436de68fe1SLen Brown 
2544103a8feaSLen Brown int get_core_id(int cpu)
2545103a8feaSLen Brown {
254695aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2547103a8feaSLen Brown }
2548103a8feaSLen Brown 
2549ef605741SPrarit Bhargava void set_node_data(void)
2550ef605741SPrarit Bhargava {
25512ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
25522ffbb224SPrarit Bhargava 	int cpu_count;
2553ef605741SPrarit Bhargava 
25542ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
25552ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
25562ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2557ef605741SPrarit Bhargava 
25582ffbb224SPrarit Bhargava 	cpu_count = 0;
25592ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
25602ffbb224SPrarit Bhargava 		lnode = 0;
25612ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
25622ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2563ef605741SPrarit Bhargava 				continue;
25642ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
25652ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
25662ffbb224SPrarit Bhargava 				continue;
25672ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2568ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
25692ffbb224SPrarit Bhargava 			cpu_count++;
25702ffbb224SPrarit Bhargava 			/*
25712ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
25722ffbb224SPrarit Bhargava 			 * the logical_node_id
25732ffbb224SPrarit Bhargava 			 */
25742ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
25752ffbb224SPrarit Bhargava 				if ((cpus[cpux].physical_package_id == pkg) &&
25762ffbb224SPrarit Bhargava 				   (cpus[cpux].physical_node_id == node)) {
25772ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
25782ffbb224SPrarit Bhargava 					cpu_count++;
2579ef605741SPrarit Bhargava 				}
25802ffbb224SPrarit Bhargava 			}
25812ffbb224SPrarit Bhargava 			lnode++;
25822ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
25832ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
25842ffbb224SPrarit Bhargava 		}
25852ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
25862ffbb224SPrarit Bhargava 			break;
25872ffbb224SPrarit Bhargava 	}
2588ef605741SPrarit Bhargava }
2589ef605741SPrarit Bhargava 
2590ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2591c98d5d94SLen Brown {
2592c98d5d94SLen Brown 	char path[80];
2593c98d5d94SLen Brown 	FILE *filep;
25940e2d8f05SLen Brown 	int i;
25950e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2596c98d5d94SLen Brown 
25970e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
25980e2d8f05SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist",
25990e2d8f05SLen Brown 			cpu, i);
26000e2d8f05SLen Brown 		filep = fopen(path, "r");
26010e2d8f05SLen Brown 		if (!filep)
26020e2d8f05SLen Brown 			continue;
26030e2d8f05SLen Brown 		fclose(filep);
26040e2d8f05SLen Brown 		return i;
26050e2d8f05SLen Brown 	}
26060e2d8f05SLen Brown 	return -1;
2607e275b388SDasaratharaman Chandramouli }
2608c98d5d94SLen Brown 
26090e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
26100e2d8f05SLen Brown {
26110e2d8f05SLen Brown 	char path[80], character;
26120e2d8f05SLen Brown 	FILE *filep;
26130e2d8f05SLen Brown 	unsigned long map;
26148cb48b32SPrarit Bhargava 	int so, shift, sib_core;
26150e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
26160e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
26170e2d8f05SLen Brown 	size_t size;
26188cb48b32SPrarit Bhargava 	int thread_id = 0;
26190e2d8f05SLen Brown 
26200e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
26218cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
26228cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
26230e2d8f05SLen Brown 	if (!thiscpu->put_ids)
26240e2d8f05SLen Brown 		return -1;
26250e2d8f05SLen Brown 
26260e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
26270e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
26280e2d8f05SLen Brown 
26290e2d8f05SLen Brown 	sprintf(path,
26300e2d8f05SLen Brown 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
26310e2d8f05SLen Brown 	filep = fopen_or_die(path, "r");
26320e2d8f05SLen Brown 	do {
26330e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
26348173c336SBen Hutchings 		if (fscanf(filep, "%lx%c", &map, &character) != 2)
26358173c336SBen Hutchings 			err(1, "%s: failed to parse file", path);
26360e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
26370e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
26388cb48b32SPrarit Bhargava 				so = shift + offset;
26398cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
26408cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
26418cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
26428cb48b32SPrarit Bhargava 					if ((so != cpu) &&
26438cb48b32SPrarit Bhargava 					    (cpus[so].thread_id < 0))
26448cb48b32SPrarit Bhargava 						cpus[so].thread_id =
26458cb48b32SPrarit Bhargava 								    thread_id++;
26468cb48b32SPrarit Bhargava 				}
26470e2d8f05SLen Brown 			}
26480e2d8f05SLen Brown 		}
26490e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2650c98d5d94SLen Brown 	fclose(filep);
26510e2d8f05SLen Brown 
26520e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2653c98d5d94SLen Brown }
2654c98d5d94SLen Brown 
2655c98d5d94SLen Brown /*
2656c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2657c98d5d94SLen Brown  * skip non-present cpus
2658103a8feaSLen Brown  */
2659103a8feaSLen Brown 
2660c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2661c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2662c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2663c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2664c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2665c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2666c98d5d94SLen Brown {
266740f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2668c98d5d94SLen Brown 
2669c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
267040f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
267140f5cfe7SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node;
267240f5cfe7SPrarit Bhargava 			     ++core_no) {
2673c98d5d94SLen Brown 				for (thread_no = 0; thread_no <
267470a9c6e8SPrarit Bhargava 					topo.threads_per_core; ++thread_no) {
2675c98d5d94SLen Brown 					struct thread_data *t, *t2;
2676c98d5d94SLen Brown 					struct core_data *c, *c2;
2677c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2678c98d5d94SLen Brown 
267940f5cfe7SPrarit Bhargava 					t = GET_THREAD(thread_base, thread_no,
268040f5cfe7SPrarit Bhargava 						       core_no, node_no,
268140f5cfe7SPrarit Bhargava 						       pkg_no);
2682c98d5d94SLen Brown 
2683c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
2684c98d5d94SLen Brown 						continue;
2685c98d5d94SLen Brown 
268640f5cfe7SPrarit Bhargava 					t2 = GET_THREAD(thread_base2, thread_no,
268740f5cfe7SPrarit Bhargava 							core_no, node_no,
268840f5cfe7SPrarit Bhargava 							pkg_no);
2689c98d5d94SLen Brown 
269040f5cfe7SPrarit Bhargava 					c = GET_CORE(core_base, core_no,
269140f5cfe7SPrarit Bhargava 						     node_no, pkg_no);
269240f5cfe7SPrarit Bhargava 					c2 = GET_CORE(core_base2, core_no,
269340f5cfe7SPrarit Bhargava 						      node_no,
269440f5cfe7SPrarit Bhargava 						      pkg_no);
2695c98d5d94SLen Brown 
2696c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
2697c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
2698c98d5d94SLen Brown 
2699c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
2700c98d5d94SLen Brown 					if (retval)
2701c98d5d94SLen Brown 						return retval;
2702c98d5d94SLen Brown 				}
2703c98d5d94SLen Brown 			}
2704c98d5d94SLen Brown 		}
270540f5cfe7SPrarit Bhargava 	}
2706c98d5d94SLen Brown 	return 0;
2707c98d5d94SLen Brown }
2708c98d5d94SLen Brown 
2709c98d5d94SLen Brown /*
2710c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2711c98d5d94SLen Brown  * return max_cpu number
2712c98d5d94SLen Brown  */
2713c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2714103a8feaSLen Brown {
2715103a8feaSLen Brown 	FILE *fp;
2716c98d5d94SLen Brown 	int cpu_num;
2717103a8feaSLen Brown 	int retval;
2718103a8feaSLen Brown 
271957a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2720103a8feaSLen Brown 
2721103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2722b2c95d90SJosh Triplett 	if (retval != 0)
2723b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2724103a8feaSLen Brown 
2725c98d5d94SLen Brown 	while (1) {
2726c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2727103a8feaSLen Brown 		if (retval != 1)
2728103a8feaSLen Brown 			break;
2729103a8feaSLen Brown 
2730c98d5d94SLen Brown 		retval = func(cpu_num);
2731c98d5d94SLen Brown 		if (retval) {
2732c98d5d94SLen Brown 			fclose(fp);
2733c98d5d94SLen Brown 			return(retval);
2734c98d5d94SLen Brown 		}
2735103a8feaSLen Brown 	}
2736103a8feaSLen Brown 	fclose(fp);
2737c98d5d94SLen Brown 	return 0;
2738103a8feaSLen Brown }
2739103a8feaSLen Brown 
2740103a8feaSLen Brown void re_initialize(void)
2741103a8feaSLen Brown {
2742c98d5d94SLen Brown 	free_all_buffers();
2743c98d5d94SLen Brown 	setup_all_buffers();
2744c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2745103a8feaSLen Brown }
2746103a8feaSLen Brown 
2747843c5791SPrarit Bhargava void set_max_cpu_num(void)
2748843c5791SPrarit Bhargava {
2749843c5791SPrarit Bhargava 	FILE *filep;
2750843c5791SPrarit Bhargava 	unsigned long dummy;
2751843c5791SPrarit Bhargava 
2752843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
2753843c5791SPrarit Bhargava 	filep = fopen_or_die(
2754843c5791SPrarit Bhargava 			"/sys/devices/system/cpu/cpu0/topology/thread_siblings",
2755843c5791SPrarit Bhargava 			"r");
2756843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
27570e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
2758843c5791SPrarit Bhargava 	fclose(filep);
2759843c5791SPrarit Bhargava 	topo.max_cpu_num--; /* 0 based */
2760843c5791SPrarit Bhargava }
2761c98d5d94SLen Brown 
2762103a8feaSLen Brown /*
2763c98d5d94SLen Brown  * count_cpus()
2764c98d5d94SLen Brown  * remember the last one seen, it will be the max
2765103a8feaSLen Brown  */
2766c98d5d94SLen Brown int count_cpus(int cpu)
2767103a8feaSLen Brown {
2768843c5791SPrarit Bhargava 	topo.num_cpus++;
2769c98d5d94SLen Brown 	return 0;
2770103a8feaSLen Brown }
2771c98d5d94SLen Brown int mark_cpu_present(int cpu)
2772c98d5d94SLen Brown {
2773c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
277415aaa346SLen Brown 	return 0;
2775103a8feaSLen Brown }
2776103a8feaSLen Brown 
27778cb48b32SPrarit Bhargava int init_thread_id(int cpu)
27788cb48b32SPrarit Bhargava {
27798cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
27808cb48b32SPrarit Bhargava 	return 0;
27818cb48b32SPrarit Bhargava }
27828cb48b32SPrarit Bhargava 
2783562a2d37SLen Brown /*
2784562a2d37SLen Brown  * snapshot_proc_interrupts()
2785562a2d37SLen Brown  *
2786562a2d37SLen Brown  * read and record summary of /proc/interrupts
2787562a2d37SLen Brown  *
2788562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2789562a2d37SLen Brown  */
2790562a2d37SLen Brown int snapshot_proc_interrupts(void)
2791562a2d37SLen Brown {
2792562a2d37SLen Brown 	static FILE *fp;
2793562a2d37SLen Brown 	int column, retval;
2794562a2d37SLen Brown 
2795562a2d37SLen Brown 	if (fp == NULL)
2796562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2797562a2d37SLen Brown 	else
2798562a2d37SLen Brown 		rewind(fp);
2799562a2d37SLen Brown 
2800562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2801562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2802562a2d37SLen Brown 		int cpu_number;
2803562a2d37SLen Brown 
2804562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2805562a2d37SLen Brown 		if (retval != 1)
2806562a2d37SLen Brown 			break;
2807562a2d37SLen Brown 
2808562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2809562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2810562a2d37SLen Brown 			return 1;
2811562a2d37SLen Brown 		}
2812562a2d37SLen Brown 
2813562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2814562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2815562a2d37SLen Brown 	}
2816562a2d37SLen Brown 
2817562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2818562a2d37SLen Brown 	while (1) {
2819562a2d37SLen Brown 		int column;
2820562a2d37SLen Brown 		char buf[64];
2821562a2d37SLen Brown 
2822562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2823562a2d37SLen Brown 		if (retval != 1)
2824562a2d37SLen Brown 			break;
2825562a2d37SLen Brown 
2826562a2d37SLen Brown 		/* read the count per cpu */
2827562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2828562a2d37SLen Brown 
2829562a2d37SLen Brown 			int cpu_number, irq_count;
2830562a2d37SLen Brown 
2831562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2832562a2d37SLen Brown 			if (retval != 1)
2833562a2d37SLen Brown 				break;
2834562a2d37SLen Brown 
2835562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2836562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2837562a2d37SLen Brown 
2838562a2d37SLen Brown 		}
2839562a2d37SLen Brown 
2840562a2d37SLen Brown 		while (getc(fp) != '\n')
2841562a2d37SLen Brown 			;	/* flush interrupt description */
2842562a2d37SLen Brown 
2843562a2d37SLen Brown 	}
2844562a2d37SLen Brown 	return 0;
2845562a2d37SLen Brown }
284627d47356SLen Brown /*
2847fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2848fdf676e5SLen Brown  *
2849fdf676e5SLen Brown  * record snapshot of
2850fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2851fdf676e5SLen Brown  *
2852fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2853fdf676e5SLen Brown  */
2854fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2855fdf676e5SLen Brown {
2856fdf676e5SLen Brown 	FILE *fp;
2857fdf676e5SLen Brown 	int retval;
2858fdf676e5SLen Brown 
2859fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2860fdf676e5SLen Brown 
2861fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2862fdf676e5SLen Brown 	if (retval != 1)
2863fdf676e5SLen Brown 		err(1, "GFX rc6");
2864fdf676e5SLen Brown 
2865fdf676e5SLen Brown 	fclose(fp);
2866fdf676e5SLen Brown 
2867fdf676e5SLen Brown 	return 0;
2868fdf676e5SLen Brown }
2869fdf676e5SLen Brown /*
287027d47356SLen Brown  * snapshot_gfx_mhz()
287127d47356SLen Brown  *
287227d47356SLen Brown  * record snapshot of
287327d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
287427d47356SLen Brown  *
287527d47356SLen Brown  * return 1 if config change requires a restart, else return 0
287627d47356SLen Brown  */
287727d47356SLen Brown int snapshot_gfx_mhz(void)
287827d47356SLen Brown {
287927d47356SLen Brown 	static FILE *fp;
288027d47356SLen Brown 	int retval;
288127d47356SLen Brown 
288227d47356SLen Brown 	if (fp == NULL)
288327d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
288422048c54SLen Brown 	else {
288527d47356SLen Brown 		rewind(fp);
288622048c54SLen Brown 		fflush(fp);
288722048c54SLen Brown 	}
288827d47356SLen Brown 
288927d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
289027d47356SLen Brown 	if (retval != 1)
289127d47356SLen Brown 		err(1, "GFX MHz");
289227d47356SLen Brown 
289327d47356SLen Brown 	return 0;
289427d47356SLen Brown }
2895562a2d37SLen Brown 
2896562a2d37SLen Brown /*
2897be0e54c4SLen Brown  * snapshot_cpu_lpi()
2898be0e54c4SLen Brown  *
2899be0e54c4SLen Brown  * record snapshot of
2900be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
2901be0e54c4SLen Brown  *
2902be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2903be0e54c4SLen Brown  */
2904be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
2905be0e54c4SLen Brown {
2906be0e54c4SLen Brown 	FILE *fp;
2907be0e54c4SLen Brown 	int retval;
2908be0e54c4SLen Brown 
2909be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
2910be0e54c4SLen Brown 
2911be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
29125ea7647bSPrarit Bhargava 	if (retval != 1) {
29135ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle CPU output\n");
29145ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_CPU_LPI);
29155ea7647bSPrarit Bhargava 		return -1;
29165ea7647bSPrarit Bhargava 	}
2917be0e54c4SLen Brown 
2918be0e54c4SLen Brown 	fclose(fp);
2919be0e54c4SLen Brown 
2920be0e54c4SLen Brown 	return 0;
2921be0e54c4SLen Brown }
2922be0e54c4SLen Brown /*
2923be0e54c4SLen Brown  * snapshot_sys_lpi()
2924be0e54c4SLen Brown  *
2925be0e54c4SLen Brown  * record snapshot of
2926be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us
2927be0e54c4SLen Brown  *
2928be0e54c4SLen Brown  * return 1 if config change requires a restart, else return 0
2929be0e54c4SLen Brown  */
2930be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
2931be0e54c4SLen Brown {
2932be0e54c4SLen Brown 	FILE *fp;
2933be0e54c4SLen Brown 	int retval;
2934be0e54c4SLen Brown 
2935be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", "r");
2936be0e54c4SLen Brown 
2937be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
29385ea7647bSPrarit Bhargava 	if (retval != 1) {
29395ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle System output\n");
29405ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_SYS_LPI);
29415ea7647bSPrarit Bhargava 		return -1;
29425ea7647bSPrarit Bhargava 	}
2943be0e54c4SLen Brown 	fclose(fp);
2944be0e54c4SLen Brown 
2945be0e54c4SLen Brown 	return 0;
2946be0e54c4SLen Brown }
2947be0e54c4SLen Brown /*
2948562a2d37SLen Brown  * snapshot /proc and /sys files
2949562a2d37SLen Brown  *
2950562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2951562a2d37SLen Brown  */
2952562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2953562a2d37SLen Brown {
2954218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
2955562a2d37SLen Brown 		if (snapshot_proc_interrupts())
2956562a2d37SLen Brown 			return 1;
2957562a2d37SLen Brown 
2958812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2959fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2960fdf676e5SLen Brown 
2961812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
296227d47356SLen Brown 		snapshot_gfx_mhz();
296327d47356SLen Brown 
2964be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2965be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
2966be0e54c4SLen Brown 
2967be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2968be0e54c4SLen Brown 		snapshot_sys_lpi_us();
2969be0e54c4SLen Brown 
2970562a2d37SLen Brown 	return 0;
2971562a2d37SLen Brown }
2972562a2d37SLen Brown 
29738aa2ed0bSLen Brown int exit_requested;
29748aa2ed0bSLen Brown 
29758aa2ed0bSLen Brown static void signal_handler (int signal)
29768aa2ed0bSLen Brown {
29778aa2ed0bSLen Brown 	switch (signal) {
29788aa2ed0bSLen Brown 	case SIGINT:
29798aa2ed0bSLen Brown 		exit_requested = 1;
29808aa2ed0bSLen Brown 		if (debug)
29818aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
29828aa2ed0bSLen Brown 		break;
298307211960SLen Brown 	case SIGUSR1:
298407211960SLen Brown 		if (debug > 1)
298507211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
298607211960SLen Brown 		break;
29878aa2ed0bSLen Brown 	}
2988b9ad8ee0SLen Brown 	/* make sure this manually-invoked interval is at least 1ms long */
2989b9ad8ee0SLen Brown 	nanosleep(&one_msec, NULL);
29908aa2ed0bSLen Brown }
29918aa2ed0bSLen Brown 
29928aa2ed0bSLen Brown void setup_signal_handler(void)
29938aa2ed0bSLen Brown {
29948aa2ed0bSLen Brown 	struct sigaction sa;
29958aa2ed0bSLen Brown 
29968aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
29978aa2ed0bSLen Brown 
29988aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
29998aa2ed0bSLen Brown 
30008aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
30018aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
300207211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
300307211960SLen Brown 		err(1, "sigaction SIGUSR1");
30048aa2ed0bSLen Brown }
3005b9ad8ee0SLen Brown 
300647936f94SArtem Bityutskiy void do_sleep(void)
3007b9ad8ee0SLen Brown {
3008b9ad8ee0SLen Brown 	struct timeval select_timeout;
3009b9ad8ee0SLen Brown 	fd_set readfds;
3010b9ad8ee0SLen Brown 	int retval;
3011b9ad8ee0SLen Brown 
3012b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
3013b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
3014b9ad8ee0SLen Brown 
301547936f94SArtem Bityutskiy 	if (!isatty(fileno(stdin))) {
301647936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
301747936f94SArtem Bityutskiy 		return;
301847936f94SArtem Bityutskiy 	}
3019b9ad8ee0SLen Brown 
302047936f94SArtem Bityutskiy 	select_timeout = interval_tv;
3021b9ad8ee0SLen Brown 	retval = select(1, &readfds, NULL, NULL, &select_timeout);
3022b9ad8ee0SLen Brown 
3023b9ad8ee0SLen Brown 	if (retval == 1) {
3024b9ad8ee0SLen Brown 		switch (getc(stdin)) {
3025b9ad8ee0SLen Brown 		case 'q':
3026b9ad8ee0SLen Brown 			exit_requested = 1;
3027b9ad8ee0SLen Brown 			break;
3028b9ad8ee0SLen Brown 		}
3029b9ad8ee0SLen Brown 		/* make sure this manually-invoked interval is at least 1ms long */
3030b9ad8ee0SLen Brown 		nanosleep(&one_msec, NULL);
3031b9ad8ee0SLen Brown 	}
3032b9ad8ee0SLen Brown }
303347936f94SArtem Bityutskiy 
30344c2122d4SLen Brown 
3035103a8feaSLen Brown void turbostat_loop()
3036103a8feaSLen Brown {
3037c98d5d94SLen Brown 	int retval;
3038e52966c0SLen Brown 	int restarted = 0;
3039023fe0acSChen Yu 	int done_iters = 0;
3040c98d5d94SLen Brown 
30418aa2ed0bSLen Brown 	setup_signal_handler();
30428aa2ed0bSLen Brown 
3043103a8feaSLen Brown restart:
3044e52966c0SLen Brown 	restarted++;
3045e52966c0SLen Brown 
3046562a2d37SLen Brown 	snapshot_proc_sysfs_files();
3047c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
30484c2122d4SLen Brown 	first_counter_read = 0;
3049d91bb17cSLen Brown 	if (retval < -1) {
3050d91bb17cSLen Brown 		exit(retval);
3051d91bb17cSLen Brown 	} else if (retval == -1) {
3052e52966c0SLen Brown 		if (restarted > 1) {
3053e52966c0SLen Brown 			exit(retval);
3054e52966c0SLen Brown 		}
3055c98d5d94SLen Brown 		re_initialize();
3056c98d5d94SLen Brown 		goto restart;
3057c98d5d94SLen Brown 	}
3058e52966c0SLen Brown 	restarted = 0;
3059023fe0acSChen Yu 	done_iters = 0;
3060103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3061103a8feaSLen Brown 
3062103a8feaSLen Brown 	while (1) {
3063c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3064103a8feaSLen Brown 			re_initialize();
3065103a8feaSLen Brown 			goto restart;
3066103a8feaSLen Brown 		}
3067b9ad8ee0SLen Brown 		do_sleep();
3068562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3069562a2d37SLen Brown 			goto restart;
3070c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3071d91bb17cSLen Brown 		if (retval < -1) {
3072d91bb17cSLen Brown 			exit(retval);
3073d91bb17cSLen Brown 		} else if (retval == -1) {
307415aaa346SLen Brown 			re_initialize();
307515aaa346SLen Brown 			goto restart;
307615aaa346SLen Brown 		}
3077103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3078103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3079ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3080ba3dec99SLen Brown 			re_initialize();
3081ba3dec99SLen Brown 			goto restart;
3082ba3dec99SLen Brown 		}
3083c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3084c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3085b7d8c148SLen Brown 		flush_output_stdout();
30868aa2ed0bSLen Brown 		if (exit_requested)
30878aa2ed0bSLen Brown 			break;
3088023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3089023fe0acSChen Yu 			break;
3090b9ad8ee0SLen Brown 		do_sleep();
3091562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3092562a2d37SLen Brown 			goto restart;
3093c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3094d91bb17cSLen Brown 		if (retval < -1) {
3095d91bb17cSLen Brown 			exit(retval);
3096d91bb17cSLen Brown 		} else if (retval == -1) {
3097103a8feaSLen Brown 			re_initialize();
3098103a8feaSLen Brown 			goto restart;
3099103a8feaSLen Brown 		}
3100103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3101103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3102ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3103ba3dec99SLen Brown 			re_initialize();
3104ba3dec99SLen Brown 			goto restart;
3105ba3dec99SLen Brown 		}
3106c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3107c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3108b7d8c148SLen Brown 		flush_output_stdout();
31098aa2ed0bSLen Brown 		if (exit_requested)
31108aa2ed0bSLen Brown 			break;
3111023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3112023fe0acSChen Yu 			break;
3113103a8feaSLen Brown 	}
3114103a8feaSLen Brown }
3115103a8feaSLen Brown 
3116103a8feaSLen Brown void check_dev_msr()
3117103a8feaSLen Brown {
3118103a8feaSLen Brown 	struct stat sb;
31197ce7d5deSPrarit Bhargava 	char pathname[32];
3120103a8feaSLen Brown 
31217ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31227ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3123a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3124d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3125103a8feaSLen Brown }
3126103a8feaSLen Brown 
312798481e79SLen Brown void check_permissions()
3128103a8feaSLen Brown {
312998481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
313098481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
313198481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
313298481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
313398481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
313498481e79SLen Brown 	int do_exit = 0;
31357ce7d5deSPrarit Bhargava 	char pathname[32];
313698481e79SLen Brown 
313798481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
313898481e79SLen Brown 	cap_header->pid = getpid();
313998481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
314098481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
314198481e79SLen Brown 		err(-6, "capget(2) failed");
314298481e79SLen Brown 
314398481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
314498481e79SLen Brown 		do_exit++;
314598481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
314698481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
314798481e79SLen Brown 	}
314898481e79SLen Brown 
314998481e79SLen Brown 	/* test file permissions */
31507ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
31517ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
315298481e79SLen Brown 		do_exit++;
315398481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
315498481e79SLen Brown 	}
315598481e79SLen Brown 
315698481e79SLen Brown 	/* if all else fails, thell them to be root */
315798481e79SLen Brown 	if (do_exit)
3158b2c95d90SJosh Triplett 		if (getuid() != 0)
3159d7899447SLen Brown 			warnx("... or simply run as root");
316098481e79SLen Brown 
316198481e79SLen Brown 	if (do_exit)
316298481e79SLen Brown 		exit(-6);
3163103a8feaSLen Brown }
3164103a8feaSLen Brown 
3165d7899447SLen Brown /*
3166d7899447SLen Brown  * NHM adds support for additional MSRs:
3167d7899447SLen Brown  *
3168d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3169d7899447SLen Brown  *
3170ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
31711df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3172d7899447SLen Brown  *
3173cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3174cf4cbe53SLen Brown  *
3175d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3176d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3177d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3178d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3179d7899447SLen Brown  *
3180ee7e38e3SLen Brown  * Side effect:
31811df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
318233148d67SLen Brown  * sets has_misc_feature_control
3183d7899447SLen Brown  */
3184ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3185103a8feaSLen Brown {
3186ee7e38e3SLen Brown 	unsigned long long msr;
318721ed5574SLen Brown 	unsigned int base_ratio;
3188ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3189ee7e38e3SLen Brown 
3190103a8feaSLen Brown 	if (!genuine_intel)
3191103a8feaSLen Brown 		return 0;
3192103a8feaSLen Brown 
3193103a8feaSLen Brown 	if (family != 6)
3194103a8feaSLen Brown 		return 0;
3195103a8feaSLen Brown 
319621ed5574SLen Brown 	bclk = discover_bclk(family, model);
319721ed5574SLen Brown 
3198103a8feaSLen Brown 	switch (model) {
3199869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3200869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3201ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3202ee7e38e3SLen Brown 		break;
3203869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3204869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3205869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3206869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3207ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
320833148d67SLen Brown 		has_misc_feature_control = 1;
3209ee7e38e3SLen Brown 		break;
3210*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
3211869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3212869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3213*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
3214869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3215869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3216869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3217997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3218ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
321933148d67SLen Brown 		has_misc_feature_control = 1;
3220ee7e38e3SLen Brown 		break;
3221d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3222d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
322333148d67SLen Brown 		has_misc_feature_control = 1;
3224d8ebb442SLen Brown 		break;
3225f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3226cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3227f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
3228ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3229ee7e38e3SLen Brown 		break;
3230869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3231ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3232cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3233ee7e38e3SLen Brown 		break;
3234869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3235ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3236ee7e38e3SLen Brown 		break;
3237869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3238f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3239f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
3240445640a5SLen Brown 		pkg_cstate_limits = glm_pkg_cstate_limits;
3241e4085d54SLen Brown 		break;
3242103a8feaSLen Brown 	default:
3243103a8feaSLen Brown 		return 0;
3244103a8feaSLen Brown 	}
32451df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3246e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3247ee7e38e3SLen Brown 
3248ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
324921ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
325021ed5574SLen Brown 
325121ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
325221ed5574SLen Brown 	has_base_hz = 1;
3253ee7e38e3SLen Brown 	return 1;
3254103a8feaSLen Brown }
32550f7887c4SLen Brown /*
3256495c7654SLen Brown  * SLV client has support for unique MSRs:
32570f7887c4SLen Brown  *
32580f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
32590f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
32600f7887c4SLen Brown  */
32610f7887c4SLen Brown 
32620f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
32630f7887c4SLen Brown {
32640f7887c4SLen Brown 	if (!genuine_intel)
32650f7887c4SLen Brown 		return 0;
32660f7887c4SLen Brown 
32670f7887c4SLen Brown 	switch (model) {
3268f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3269f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3270f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
32710f7887c4SLen Brown 		return 1;
32720f7887c4SLen Brown 	}
32730f7887c4SLen Brown 	return 0;
32740f7887c4SLen Brown }
32757170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
32767170a374SLen Brown {
32777170a374SLen Brown 
32787170a374SLen Brown 	if (!genuine_intel)
32797170a374SLen Brown 		return 0;
32807170a374SLen Brown 
32817170a374SLen Brown 	switch (model) {
3282f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:
32837170a374SLen Brown 		return 1;
32847170a374SLen Brown 	}
32857170a374SLen Brown 	return 0;
32867170a374SLen Brown }
3287ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3288ade0ebacSLen Brown {
3289ade0ebacSLen Brown 
3290ade0ebacSLen Brown 	if (!genuine_intel)
3291ade0ebacSLen Brown 		return 0;
3292ade0ebacSLen Brown 
3293ade0ebacSLen Brown 	switch (model) {
3294ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3295ade0ebacSLen Brown 		return 1;
3296ade0ebacSLen Brown 	}
3297ade0ebacSLen Brown 	return 0;
3298ade0ebacSLen Brown }
329934c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
330034c76197SLen Brown {
330134c76197SLen Brown 
330234c76197SLen Brown 	if (!genuine_intel)
330334c76197SLen Brown 		return 0;
330434c76197SLen Brown 
330534c76197SLen Brown 	switch (model) {
330634c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
330734c76197SLen Brown 		return 1;
330834c76197SLen Brown 	}
330934c76197SLen Brown 	return 0;
331034c76197SLen Brown }
33110f7887c4SLen Brown 
331231e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3313d7899447SLen Brown {
33140f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33150f7887c4SLen Brown 		return 0;
33160f7887c4SLen Brown 
3317d7899447SLen Brown 	switch (model) {
3318d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
3319869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3320869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3321d7899447SLen Brown 		return 0;
3322d7899447SLen Brown 	default:
3323d7899447SLen Brown 		return 1;
3324d7899447SLen Brown 	}
3325d7899447SLen Brown }
33260f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
33270f7887c4SLen Brown {
33280f7887c4SLen Brown 	if (has_slv_msrs(family, model))
33290f7887c4SLen Brown 		return 1;
33300f7887c4SLen Brown 
33310f7887c4SLen Brown 	return 0;
33320f7887c4SLen Brown }
33336574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
33346574a5d5SLen Brown {
33356574a5d5SLen Brown 	if (!genuine_intel)
33366574a5d5SLen Brown 		return 0;
33376574a5d5SLen Brown 
33386574a5d5SLen Brown 	if (family != 6)
33396574a5d5SLen Brown 		return 0;
33406574a5d5SLen Brown 
33416574a5d5SLen Brown 	switch (model) {
3342869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3343869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
33446574a5d5SLen Brown 		return 1;
33456574a5d5SLen Brown 	default:
33466574a5d5SLen Brown 		return 0;
33476574a5d5SLen Brown 	}
33486574a5d5SLen Brown }
3349fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3350fcd17211SLen Brown {
3351fcd17211SLen Brown 	if (!genuine_intel)
3352fcd17211SLen Brown 		return 0;
3353fcd17211SLen Brown 
3354fcd17211SLen Brown 	if (family != 6)
3355fcd17211SLen Brown 		return 0;
3356fcd17211SLen Brown 
3357fcd17211SLen Brown 	switch (model) {
3358869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3359fcd17211SLen Brown 		return 1;
3360fcd17211SLen Brown 	default:
3361fcd17211SLen Brown 		return 0;
3362fcd17211SLen Brown 	}
3363fcd17211SLen Brown }
3364fcd17211SLen Brown 
3365fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3366fb5d4327SDasaratharaman Chandramouli {
3367fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3368fb5d4327SDasaratharaman Chandramouli 		return 0;
3369fb5d4327SDasaratharaman Chandramouli 
3370fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3371fb5d4327SDasaratharaman Chandramouli 		return 0;
3372fb5d4327SDasaratharaman Chandramouli 
3373fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3374869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3375fb5d4327SDasaratharaman Chandramouli 		return 1;
3376fb5d4327SDasaratharaman Chandramouli 	default:
3377fb5d4327SDasaratharaman Chandramouli 		return 0;
3378fb5d4327SDasaratharaman Chandramouli 	}
3379fb5d4327SDasaratharaman Chandramouli }
338031e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
338131e07522SLen Brown {
338231e07522SLen Brown 	if (!genuine_intel)
338331e07522SLen Brown 		return 0;
338431e07522SLen Brown 
338531e07522SLen Brown 	if (family != 6)
338631e07522SLen Brown 		return 0;
338731e07522SLen Brown 
338831e07522SLen Brown 	switch (model) {
338931e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
339031e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
339131e07522SLen Brown 		return 1;
339231e07522SLen Brown 	default:
339331e07522SLen Brown 		return 0;
339431e07522SLen Brown 	}
339531e07522SLen Brown }
33966fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
33976fb3143bSLen Brown {
33986fb3143bSLen Brown 	if (!genuine_intel)
33996fb3143bSLen Brown 		return 0;
34006fb3143bSLen Brown 
34016fb3143bSLen Brown 	if (family != 6)
34026fb3143bSLen Brown 		return 0;
34036fb3143bSLen Brown 
34046fb3143bSLen Brown 	switch (model) {
3405869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3406*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
3407869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3408869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3409*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
3410869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3411869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3412869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3413997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
3414869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
34156fb3143bSLen Brown 
3416869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
34176fb3143bSLen Brown 		return 1;
34186fb3143bSLen Brown 	default:
34196fb3143bSLen Brown 		return 0;
34206fb3143bSLen Brown 	}
34216fb3143bSLen Brown }
34226fb3143bSLen Brown 
3423fcd17211SLen Brown static void
34241b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3425fcd17211SLen Brown {
3426fcd17211SLen Brown 	if (!do_nhm_platform_info)
3427fcd17211SLen Brown 		return;
3428fcd17211SLen Brown 
3429fcd17211SLen Brown 	dump_nhm_platform_info();
3430fcd17211SLen Brown 
3431fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
3432fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
3433fcd17211SLen Brown 
3434fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
3435fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
3436fcd17211SLen Brown 
343731e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
343831e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
3439fcd17211SLen Brown 
34400f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
34410f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
34420f7887c4SLen Brown 
3443fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
3444fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
3445fb5d4327SDasaratharaman Chandramouli 
34466fb3143bSLen Brown 	if (has_config_tdp(family, model))
34476fb3143bSLen Brown 		dump_config_tdp();
34486fb3143bSLen Brown 
3449fcd17211SLen Brown 	dump_nhm_cst_cfg();
3450fcd17211SLen Brown }
3451fcd17211SLen Brown 
345241618e63SLen Brown static void
345341618e63SLen Brown dump_sysfs_cstate_config(void)
345441618e63SLen Brown {
345541618e63SLen Brown 	char path[64];
345641618e63SLen Brown 	char name_buf[16];
345741618e63SLen Brown 	char desc[64];
345841618e63SLen Brown 	FILE *input;
345941618e63SLen Brown 	int state;
346041618e63SLen Brown 	char *sp;
346141618e63SLen Brown 
346241618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
346341618e63SLen Brown 		return;
346441618e63SLen Brown 
346541618e63SLen Brown 	for (state = 0; state < 10; ++state) {
346641618e63SLen Brown 
346741618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
346841618e63SLen Brown 			base_cpu, state);
346941618e63SLen Brown 		input = fopen(path, "r");
347041618e63SLen Brown 		if (input == NULL)
347141618e63SLen Brown 			continue;
34728173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
34738173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
347441618e63SLen Brown 
347541618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
347641618e63SLen Brown 		sp = strchr(name_buf, '-');
347741618e63SLen Brown 		if (!sp)
347841618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
347941618e63SLen Brown 		*sp = '\0';
348041618e63SLen Brown 		fclose(input);
348141618e63SLen Brown 
348241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
348341618e63SLen Brown 			base_cpu, state);
348441618e63SLen Brown 		input = fopen(path, "r");
348541618e63SLen Brown 		if (input == NULL)
348641618e63SLen Brown 			continue;
34878173c336SBen Hutchings 		if (!fgets(desc, sizeof(desc), input))
34888173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
348941618e63SLen Brown 
349041618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
349141618e63SLen Brown 		fclose(input);
349241618e63SLen Brown 	}
349341618e63SLen Brown }
34947293fccdSLen Brown static void
34957293fccdSLen Brown dump_sysfs_pstate_config(void)
34967293fccdSLen Brown {
34977293fccdSLen Brown 	char path[64];
34987293fccdSLen Brown 	char driver_buf[64];
34997293fccdSLen Brown 	char governor_buf[64];
35007293fccdSLen Brown 	FILE *input;
35017293fccdSLen Brown 	int turbo;
35027293fccdSLen Brown 
35037293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
35047293fccdSLen Brown 			base_cpu);
35057293fccdSLen Brown 	input = fopen(path, "r");
35067293fccdSLen Brown 	if (input == NULL) {
35070a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35087293fccdSLen Brown 		return;
35097293fccdSLen Brown 	}
35108173c336SBen Hutchings 	if (!fgets(driver_buf, sizeof(driver_buf), input))
35118173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
35127293fccdSLen Brown 	fclose(input);
35137293fccdSLen Brown 
35147293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
35157293fccdSLen Brown 			base_cpu);
35167293fccdSLen Brown 	input = fopen(path, "r");
35177293fccdSLen Brown 	if (input == NULL) {
35180a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
35197293fccdSLen Brown 		return;
35207293fccdSLen Brown 	}
35218173c336SBen Hutchings 	if (!fgets(governor_buf, sizeof(governor_buf), input))
35228173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
35237293fccdSLen Brown 	fclose(input);
35247293fccdSLen Brown 
35257293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
35267293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
35277293fccdSLen Brown 
35287293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
35297293fccdSLen Brown 	input = fopen(path, "r");
35307293fccdSLen Brown 	if (input != NULL) {
35318173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
35328173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
35337293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
35347293fccdSLen Brown 		fclose(input);
35357293fccdSLen Brown 	}
35367293fccdSLen Brown 
35377293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
35387293fccdSLen Brown 	input = fopen(path, "r");
35397293fccdSLen Brown 	if (input != NULL) {
35408173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
35418173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
35427293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
35437293fccdSLen Brown 		fclose(input);
35447293fccdSLen Brown 	}
35457293fccdSLen Brown }
354641618e63SLen Brown 
35476574a5d5SLen Brown 
3548889facbeSLen Brown /*
3549889facbeSLen Brown  * print_epb()
3550889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
3551889facbeSLen Brown  */
3552889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3553889facbeSLen Brown {
3554889facbeSLen Brown 	unsigned long long msr;
3555889facbeSLen Brown 	char *epb_string;
3556889facbeSLen Brown 	int cpu;
3557889facbeSLen Brown 
3558889facbeSLen Brown 	if (!has_epb)
3559889facbeSLen Brown 		return 0;
3560889facbeSLen Brown 
3561889facbeSLen Brown 	cpu = t->cpu_id;
3562889facbeSLen Brown 
3563889facbeSLen Brown 	/* EPB is per-package */
3564889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3565889facbeSLen Brown 		return 0;
3566889facbeSLen Brown 
3567889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3568b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3569889facbeSLen Brown 		return -1;
3570889facbeSLen Brown 	}
3571889facbeSLen Brown 
3572889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3573889facbeSLen Brown 		return 0;
3574889facbeSLen Brown 
3575e9be7dd6SLen Brown 	switch (msr & 0xF) {
3576889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
3577889facbeSLen Brown 		epb_string = "performance";
3578889facbeSLen Brown 		break;
3579889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
3580889facbeSLen Brown 		epb_string = "balanced";
3581889facbeSLen Brown 		break;
3582889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
3583889facbeSLen Brown 		epb_string = "powersave";
3584889facbeSLen Brown 		break;
3585889facbeSLen Brown 	default:
3586889facbeSLen Brown 		epb_string = "custom";
3587889facbeSLen Brown 		break;
3588889facbeSLen Brown 	}
3589b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3590889facbeSLen Brown 
3591889facbeSLen Brown 	return 0;
3592889facbeSLen Brown }
35937f5c258eSLen Brown /*
35947f5c258eSLen Brown  * print_hwp()
35957f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
35967f5c258eSLen Brown  */
35977f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
35987f5c258eSLen Brown {
35997f5c258eSLen Brown 	unsigned long long msr;
36007f5c258eSLen Brown 	int cpu;
36017f5c258eSLen Brown 
36027f5c258eSLen Brown 	if (!has_hwp)
36037f5c258eSLen Brown 		return 0;
36047f5c258eSLen Brown 
36057f5c258eSLen Brown 	cpu = t->cpu_id;
36067f5c258eSLen Brown 
36077f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
36087f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36097f5c258eSLen Brown 		return 0;
36107f5c258eSLen Brown 
36117f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
3612b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
36137f5c258eSLen Brown 		return -1;
36147f5c258eSLen Brown 	}
36157f5c258eSLen Brown 
36167f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
36177f5c258eSLen Brown 		return 0;
36187f5c258eSLen Brown 
3619b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
36207f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
36217f5c258eSLen Brown 
36227f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
36237f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
36247f5c258eSLen Brown 		return 0;
36257f5c258eSLen Brown 
36267f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
36277f5c258eSLen Brown 		return 0;
36287f5c258eSLen Brown 
3629b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
36306dbd25a2SLen Brown 			"(high %d guar %d eff %d low %d)\n",
36317f5c258eSLen Brown 			cpu, msr,
36327f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
36337f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
36347f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
36357f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
36367f5c258eSLen Brown 
36377f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
36387f5c258eSLen Brown 		return 0;
36397f5c258eSLen Brown 
3640b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
36416dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
36427f5c258eSLen Brown 			cpu, msr,
36437f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36447f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36457f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36467f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36477f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
36487f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
36497f5c258eSLen Brown 
36507f5c258eSLen Brown 	if (has_hwp_pkg) {
36517f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
36527f5c258eSLen Brown 			return 0;
36537f5c258eSLen Brown 
3654b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
36556dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
36567f5c258eSLen Brown 			cpu, msr,
36577f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
36587f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
36597f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
36607f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
36617f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
36627f5c258eSLen Brown 	}
36637f5c258eSLen Brown 	if (has_hwp_notify) {
36647f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
36657f5c258eSLen Brown 			return 0;
36667f5c258eSLen Brown 
3667b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
36687f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
36697f5c258eSLen Brown 			cpu, msr,
36707f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
36717f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
36727f5c258eSLen Brown 	}
36737f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
36747f5c258eSLen Brown 		return 0;
36757f5c258eSLen Brown 
3676b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
36777f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
36787f5c258eSLen Brown 			cpu, msr,
36797f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
36807f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3681889facbeSLen Brown 
3682889facbeSLen Brown 	return 0;
3683889facbeSLen Brown }
3684889facbeSLen Brown 
36853a9a941dSLen Brown /*
36863a9a941dSLen Brown  * print_perf_limit()
36873a9a941dSLen Brown  */
36883a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
36893a9a941dSLen Brown {
36903a9a941dSLen Brown 	unsigned long long msr;
36913a9a941dSLen Brown 	int cpu;
36923a9a941dSLen Brown 
36933a9a941dSLen Brown 	cpu = t->cpu_id;
36943a9a941dSLen Brown 
36953a9a941dSLen Brown 	/* per-package */
36963a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
36973a9a941dSLen Brown 		return 0;
36983a9a941dSLen Brown 
36993a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3700b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
37013a9a941dSLen Brown 		return -1;
37023a9a941dSLen Brown 	}
37033a9a941dSLen Brown 
37043a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
37053a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3706b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3707b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3708e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
37093a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3710e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3711e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3712e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3713e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3714e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3715e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3716e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3717e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3718e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3719e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3720e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3721e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3722b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3723e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
37243a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3725e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3726e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3727e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3728e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3729e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3730e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3731e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3732e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3733e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3734e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3735e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3736e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
37373a9a941dSLen Brown 
37383a9a941dSLen Brown 	}
37393a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
37403a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3741b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3742b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
37433a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37443a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37453a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
37463a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37473a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37483a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
37493a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37503a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3751b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
37523a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37533a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37543a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
37553a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37563a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37573a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
37583a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37593a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37603a9a941dSLen Brown 	}
37613a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
37623a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3763b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3764b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
37653a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
37663a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
37673a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
37683a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
37693a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
37703a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3771b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
37723a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
37733a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
37743a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
37753a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
37763a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
37773a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
37783a9a941dSLen Brown 	}
37793a9a941dSLen Brown 	return 0;
37803a9a941dSLen Brown }
37813a9a941dSLen Brown 
3782889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3783889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3784889facbeSLen Brown 
37859392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
3786144b44b1SLen Brown {
3787144b44b1SLen Brown 	unsigned long long msr;
3788144b44b1SLen Brown 
3789144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
37907ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3791144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3792144b44b1SLen Brown 
3793144b44b1SLen Brown 	switch (model) {
3794f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3795f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:
3796144b44b1SLen Brown 		return 30.0;
3797144b44b1SLen Brown 	default:
3798144b44b1SLen Brown 		return 135.0;
3799144b44b1SLen Brown 	}
3800144b44b1SLen Brown }
3801144b44b1SLen Brown 
38029392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
38039392bd98SCalvin Walton {
38049392bd98SCalvin Walton 	switch (family) {
38059392bd98SCalvin Walton 	case 0x17:
38069392bd98SCalvin Walton 	default:
38079392bd98SCalvin Walton 		/* This is the max stock TDP of HEDT/Server Fam17h chips */
38089392bd98SCalvin Walton 		return 250.0;
38099392bd98SCalvin Walton 	}
38109392bd98SCalvin Walton }
38119392bd98SCalvin Walton 
381240ee8e3bSAndrey Semin /*
381340ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
381440ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
381540ee8e3bSAndrey Semin  */
381640ee8e3bSAndrey Semin static double
381740ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
381840ee8e3bSAndrey Semin {
381940ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
382040ee8e3bSAndrey Semin 
382140ee8e3bSAndrey Semin 	switch (model) {
3822869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3823869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3824869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
382540ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
382640ee8e3bSAndrey Semin 	default:
382740ee8e3bSAndrey Semin 		return (rapl_energy_units);
382840ee8e3bSAndrey Semin 	}
382940ee8e3bSAndrey Semin }
383040ee8e3bSAndrey Semin 
38319392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
3832889facbeSLen Brown {
3833889facbeSLen Brown 	unsigned long long msr;
3834144b44b1SLen Brown 	unsigned int time_unit;
3835889facbeSLen Brown 	double tdp;
3836889facbeSLen Brown 
3837889facbeSLen Brown 	if (family != 6)
3838889facbeSLen Brown 		return;
3839889facbeSLen Brown 
3840889facbeSLen Brown 	switch (model) {
3841869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3842869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3843*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
3844869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3845*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
3846869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3847144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3848812db3f7SLen Brown 		if (rapl_joules) {
3849812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3850812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3851812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3852812db3f7SLen Brown 		} else {
3853812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3854812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3855812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3856812db3f7SLen Brown 		}
3857889facbeSLen Brown 		break;
3858869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3859f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
3860e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3861812db3f7SLen Brown 		if (rapl_joules)
3862812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3863812db3f7SLen Brown 		else
3864812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3865e4085d54SLen Brown 		break;
3866869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3867997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
386881824921SLen 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;
3869812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3870812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3871812db3f7SLen Brown 		if (rapl_joules) {
3872812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3873812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3874812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
387581824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3876812db3f7SLen Brown 		} else {
3877812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3878812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3879812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
388081824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3881812db3f7SLen Brown 		}
38820b2bb692SLen Brown 		break;
3883869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3884869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3885869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3886869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
38870b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3888812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3889812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3890812db3f7SLen Brown 		if (rapl_joules) {
3891812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3892812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3893812db3f7SLen Brown 		} else {
3894812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3895812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3896812db3f7SLen Brown 		}
3897e6f9bb3cSLen Brown 		break;
3898869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3899869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
39000b2bb692SLen 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;
3901812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3902812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3903812db3f7SLen Brown 		if (rapl_joules) {
3904812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3905812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3906812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3907812db3f7SLen Brown 		} else {
3908812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3909812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3910812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3911812db3f7SLen Brown 		}
3912144b44b1SLen Brown 		break;
3913f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3914f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
3915144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3916812db3f7SLen Brown 		if (rapl_joules) {
3917812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3918812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3919812db3f7SLen Brown 		} else {
3920812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3921812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3922812db3f7SLen Brown 		}
3923889facbeSLen Brown 		break;
3924f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
39250f644909SJacob 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;
3926812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3927812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3928812db3f7SLen Brown 		if (rapl_joules) {
3929812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3930812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3931812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3932812db3f7SLen Brown 		} else {
3933812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3934812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3935812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3936812db3f7SLen Brown 		}
39370f644909SJacob Pan 		break;
3938889facbeSLen Brown 	default:
3939889facbeSLen Brown 		return;
3940889facbeSLen Brown 	}
3941889facbeSLen Brown 
3942889facbeSLen Brown 	/* units on package 0, verify later other packages match */
39437ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3944889facbeSLen Brown 		return;
3945889facbeSLen Brown 
3946889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3947f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
3948144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3949144b44b1SLen Brown 	else
3950889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3951889facbeSLen Brown 
395240ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
395340ee8e3bSAndrey Semin 
3954144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3955144b44b1SLen Brown 	if (time_unit == 0)
3956144b44b1SLen Brown 		time_unit = 0xA;
3957889facbeSLen Brown 
3958144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3959144b44b1SLen Brown 
39609392bd98SCalvin Walton 	tdp = get_tdp_intel(model);
3961889facbeSLen Brown 
3962889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
396396e47158SLen Brown 	if (!quiet)
3964b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
39659392bd98SCalvin Walton }
3966889facbeSLen Brown 
39679392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
39689392bd98SCalvin Walton {
39699392bd98SCalvin Walton 	unsigned long long msr;
39709392bd98SCalvin Walton 	unsigned int eax, ebx, ecx, edx;
39719392bd98SCalvin Walton 	unsigned int has_rapl = 0;
39729392bd98SCalvin Walton 	double tdp;
39739392bd98SCalvin Walton 
39749392bd98SCalvin Walton 	if (max_extended_level >= 0x80000007) {
39759392bd98SCalvin Walton 		__cpuid(0x80000007, eax, ebx, ecx, edx);
39769392bd98SCalvin Walton 		/* RAPL (Fam 17h) */
39779392bd98SCalvin Walton 		has_rapl = edx & (1 << 14);
39789392bd98SCalvin Walton 	}
39799392bd98SCalvin Walton 
39809392bd98SCalvin Walton 	if (!has_rapl)
3981889facbeSLen Brown 		return;
39829392bd98SCalvin Walton 
39839392bd98SCalvin Walton 	switch (family) {
39849392bd98SCalvin Walton 	case 0x17: /* Zen, Zen+ */
39859392bd98SCalvin Walton 		do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
39863316f99aSCalvin Walton 		if (rapl_joules) {
39873316f99aSCalvin Walton 			BIC_PRESENT(BIC_Pkg_J);
39889392bd98SCalvin Walton 			BIC_PRESENT(BIC_Cor_J);
39893316f99aSCalvin Walton 		} else {
39903316f99aSCalvin Walton 			BIC_PRESENT(BIC_PkgWatt);
39919392bd98SCalvin Walton 			BIC_PRESENT(BIC_CorWatt);
39923316f99aSCalvin Walton 		}
39939392bd98SCalvin Walton 		break;
39949392bd98SCalvin Walton 	default:
39959392bd98SCalvin Walton 		return;
39969392bd98SCalvin Walton 	}
39979392bd98SCalvin Walton 
39989392bd98SCalvin Walton 	if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
39999392bd98SCalvin Walton 		return;
40009392bd98SCalvin Walton 
40019392bd98SCalvin Walton 	rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
40029392bd98SCalvin Walton 	rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
40039392bd98SCalvin Walton 	rapl_power_units = ldexp(1.0, -(msr & 0xf));
40049392bd98SCalvin Walton 
40059392bd98SCalvin Walton 	tdp = get_tdp_amd(model);
40069392bd98SCalvin Walton 
40079392bd98SCalvin Walton 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
40089392bd98SCalvin Walton 	if (!quiet)
40099392bd98SCalvin Walton 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
40109392bd98SCalvin Walton }
40119392bd98SCalvin Walton 
40129392bd98SCalvin Walton /*
40139392bd98SCalvin Walton  * rapl_probe()
40149392bd98SCalvin Walton  *
40159392bd98SCalvin Walton  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
40169392bd98SCalvin Walton  */
40179392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
40189392bd98SCalvin Walton {
40199392bd98SCalvin Walton 	if (genuine_intel)
40209392bd98SCalvin Walton 		rapl_probe_intel(family, model);
40219392bd98SCalvin Walton 	if (authentic_amd)
40229392bd98SCalvin Walton 		rapl_probe_amd(family, model);
4023889facbeSLen Brown }
4024889facbeSLen Brown 
40251b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
40263a9a941dSLen Brown {
40273a9a941dSLen Brown 	if (!genuine_intel)
40283a9a941dSLen Brown 		return;
40293a9a941dSLen Brown 
40303a9a941dSLen Brown 	if (family != 6)
40313a9a941dSLen Brown 		return;
40323a9a941dSLen Brown 
40333a9a941dSLen Brown 	switch (model) {
4034*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
4035869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
40363a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
4037869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
40383a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
40393a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
40403a9a941dSLen Brown 	default:
40413a9a941dSLen Brown 		return;
40423a9a941dSLen Brown 	}
40433a9a941dSLen Brown }
40443a9a941dSLen Brown 
4045ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4046ac980e13SArtem Bityutskiy {
4047ac980e13SArtem Bityutskiy 	if (is_skx(family, model) || is_bdx(family, model))
4048ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
4049ac980e13SArtem Bityutskiy }
4050ac980e13SArtem Bityutskiy 
4051889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4052889facbeSLen Brown {
4053889facbeSLen Brown 	unsigned long long msr;
4054f4896fa5SLen Brown 	unsigned int dts, dts2;
4055889facbeSLen Brown 	int cpu;
4056889facbeSLen Brown 
4057889facbeSLen Brown 	if (!(do_dts || do_ptm))
4058889facbeSLen Brown 		return 0;
4059889facbeSLen Brown 
4060889facbeSLen Brown 	cpu = t->cpu_id;
4061889facbeSLen Brown 
4062889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
4063889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4064889facbeSLen Brown 		return 0;
4065889facbeSLen Brown 
4066889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4067b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4068889facbeSLen Brown 		return -1;
4069889facbeSLen Brown 	}
4070889facbeSLen Brown 
4071889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4072889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4073889facbeSLen Brown 			return 0;
4074889facbeSLen Brown 
4075889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4076b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
4077889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
4078889facbeSLen Brown 
4079889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4080889facbeSLen Brown 			return 0;
4081889facbeSLen Brown 
4082889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4083889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4084b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4085889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4086889facbeSLen Brown 	}
4087889facbeSLen Brown 
4088889facbeSLen Brown 
4089f4896fa5SLen Brown 	if (do_dts && debug) {
4090889facbeSLen Brown 		unsigned int resolution;
4091889facbeSLen Brown 
4092889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4093889facbeSLen Brown 			return 0;
4094889facbeSLen Brown 
4095889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4096889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4097b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
4098889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
4099889facbeSLen Brown 
4100889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4101889facbeSLen Brown 			return 0;
4102889facbeSLen Brown 
4103889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4104889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4105b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
4106889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
4107889facbeSLen Brown 	}
4108889facbeSLen Brown 
4109889facbeSLen Brown 	return 0;
4110889facbeSLen Brown }
4111889facbeSLen Brown 
4112889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4113889facbeSLen Brown {
4114b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
4115889facbeSLen Brown 		cpu, label,
4116889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4117889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4118889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4119889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4120889facbeSLen Brown 
4121889facbeSLen Brown 	return;
4122889facbeSLen Brown }
4123889facbeSLen Brown 
4124889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4125889facbeSLen Brown {
4126889facbeSLen Brown 	unsigned long long msr;
41279392bd98SCalvin Walton 	const char *msr_name;
4128889facbeSLen Brown 	int cpu;
4129889facbeSLen Brown 
4130889facbeSLen Brown 	if (!do_rapl)
4131889facbeSLen Brown 		return 0;
4132889facbeSLen Brown 
4133889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4134889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4135889facbeSLen Brown 		return 0;
4136889facbeSLen Brown 
4137889facbeSLen Brown 	cpu = t->cpu_id;
4138889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4139b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4140889facbeSLen Brown 		return -1;
4141889facbeSLen Brown 	}
4142889facbeSLen Brown 
41439392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
41449392bd98SCalvin Walton 		msr_name = "MSR_RAPL_PWR_UNIT";
41459392bd98SCalvin Walton 		if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
41469392bd98SCalvin Walton 			return -1;
41479392bd98SCalvin Walton 	} else {
41489392bd98SCalvin Walton 		msr_name = "MSR_RAPL_POWER_UNIT";
4149889facbeSLen Brown 		if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4150889facbeSLen Brown 			return -1;
41519392bd98SCalvin Walton 	}
4152889facbeSLen Brown 
41539392bd98SCalvin Walton 	fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4154144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
415596e47158SLen Brown 
4156144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4157144b44b1SLen Brown 
4158889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4159889facbeSLen Brown                 	return -5;
4160889facbeSLen Brown 
4161889facbeSLen Brown 
4162b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4163889facbeSLen Brown 			cpu, msr,
4164889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4165889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4166889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4167889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4168889facbeSLen Brown 
4169144b44b1SLen Brown 	}
4170144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4171144b44b1SLen Brown 
4172889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4173889facbeSLen Brown 			return -9;
4174889facbeSLen Brown 
4175b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4176889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4177889facbeSLen Brown 
4178889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
4179b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
4180889facbeSLen Brown 			cpu,
4181889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4182889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4183889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4184889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4185889facbeSLen Brown 	}
4186889facbeSLen Brown 
41870b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4188889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4189889facbeSLen Brown                 	return -6;
4190889facbeSLen Brown 
4191b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4192889facbeSLen Brown 			cpu, msr,
4193889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4194889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4195889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4196889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
41970b2bb692SLen Brown 	}
41980b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4199889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4200889facbeSLen Brown 			return -9;
4201b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4202889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4203889facbeSLen Brown 
4204889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4205889facbeSLen Brown 	}
4206144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4207889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4208889facbeSLen Brown 			return -7;
4209889facbeSLen Brown 
4210b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4211144b44b1SLen Brown 	}
42129148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4213889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4214889facbeSLen Brown 			return -9;
4215b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4216889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4217889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4218889facbeSLen Brown 	}
4219889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4220889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4221889facbeSLen Brown 			return -8;
4222889facbeSLen Brown 
4223b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4224889facbeSLen Brown 
4225889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4226889facbeSLen Brown 			return -9;
4227b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4228889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4229889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4230889facbeSLen Brown 	}
4231889facbeSLen Brown 	return 0;
4232889facbeSLen Brown }
4233889facbeSLen Brown 
4234d7899447SLen Brown /*
4235d7899447SLen Brown  * SNB adds support for additional MSRs:
4236d7899447SLen Brown  *
4237d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4238d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4239d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4240d7899447SLen Brown  */
4241103a8feaSLen Brown 
4242d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4243103a8feaSLen Brown {
4244103a8feaSLen Brown 	if (!genuine_intel)
4245103a8feaSLen Brown 		return 0;
4246103a8feaSLen Brown 
4247103a8feaSLen Brown 	switch (model) {
4248869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4249869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4250869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4251869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
4252*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
4253869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
4254869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
4255*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
4256869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
4257869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4258869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4259997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4260869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4261869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4262f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4263f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
4264103a8feaSLen Brown 		return 1;
4265103a8feaSLen Brown 	}
4266103a8feaSLen Brown 	return 0;
4267103a8feaSLen Brown }
4268103a8feaSLen Brown 
4269d7899447SLen Brown /*
4270d7899447SLen Brown  * HSW adds support for additional MSRs:
4271d7899447SLen Brown  *
4272d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4273d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4274d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
42755a63426eSLen Brown  *
42765a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
42775a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
42785a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
42795a63426eSLen Brown  *
4280d7899447SLen Brown  */
4281d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
4282ca58710fSKristen Carlson Accardi {
4283ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4284ca58710fSKristen Carlson Accardi 		return 0;
4285ca58710fSKristen Carlson Accardi 
4286ca58710fSKristen Carlson Accardi 	switch (model) {
4287*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:
4288*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
4289869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4290997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
4291869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4292f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4293ca58710fSKristen Carlson Accardi 		return 1;
4294ca58710fSKristen Carlson Accardi 	}
4295ca58710fSKristen Carlson Accardi 	return 0;
4296ca58710fSKristen Carlson Accardi }
4297ca58710fSKristen Carlson Accardi 
42980b2bb692SLen Brown /*
42990b2bb692SLen Brown  * SKL adds support for additional MSRS:
43000b2bb692SLen Brown  *
43010b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
43020b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
43030b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
43040b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
43050b2bb692SLen Brown  */
43060b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
43070b2bb692SLen Brown {
43080b2bb692SLen Brown 	if (!genuine_intel)
43090b2bb692SLen Brown 		return 0;
43100b2bb692SLen Brown 
43110b2bb692SLen Brown 	switch (model) {
4312869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4313997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE:	/* CNL */
43140b2bb692SLen Brown 		return 1;
43150b2bb692SLen Brown 	}
43160b2bb692SLen Brown 	return 0;
43170b2bb692SLen Brown }
43180b2bb692SLen Brown 
4319144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4320144b44b1SLen Brown {
4321144b44b1SLen Brown 	if (!genuine_intel)
4322144b44b1SLen Brown 		return 0;
4323144b44b1SLen Brown 	switch (model) {
4324f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
4325f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_X:	/* AVN */
4326144b44b1SLen Brown 		return 1;
4327144b44b1SLen Brown 	}
4328144b44b1SLen Brown 	return 0;
4329144b44b1SLen Brown }
4330144b44b1SLen Brown 
4331fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4332fb5d4327SDasaratharaman Chandramouli {
4333fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4334fb5d4327SDasaratharaman Chandramouli 		return 0;
4335fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4336869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4337fb5d4327SDasaratharaman Chandramouli 		return 1;
4338fb5d4327SDasaratharaman Chandramouli 	}
4339fb5d4327SDasaratharaman Chandramouli 	return 0;
4340fb5d4327SDasaratharaman Chandramouli }
4341fb5d4327SDasaratharaman Chandramouli 
4342997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
4343997e5395SSrinivas Pandruvada {
4344997e5395SSrinivas Pandruvada 	if (!genuine_intel)
4345997e5395SSrinivas Pandruvada 		return 0;
4346997e5395SSrinivas Pandruvada 
4347997e5395SSrinivas Pandruvada 	switch (model) {
4348997e5395SSrinivas Pandruvada 	case INTEL_FAM6_CANNONLAKE_MOBILE: /* CNL */
4349997e5395SSrinivas Pandruvada 		return 1;
4350997e5395SSrinivas Pandruvada 	}
4351997e5395SSrinivas Pandruvada 
4352997e5395SSrinivas Pandruvada 	return 0;
4353997e5395SSrinivas Pandruvada }
4354997e5395SSrinivas Pandruvada 
4355b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
4356b2b34dfeSHubert Chrzaniuk {
4357b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
4358b2b34dfeSHubert Chrzaniuk 		return 1024;
4359b2b34dfeSHubert Chrzaniuk 	return 1;
4360b2b34dfeSHubert Chrzaniuk }
4361b2b34dfeSHubert Chrzaniuk 
4362144b44b1SLen Brown #define SLM_BCLK_FREQS 5
4363144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
4364144b44b1SLen Brown 
4365144b44b1SLen Brown double slm_bclk(void)
4366144b44b1SLen Brown {
4367144b44b1SLen Brown 	unsigned long long msr = 3;
4368144b44b1SLen Brown 	unsigned int i;
4369144b44b1SLen Brown 	double freq;
4370144b44b1SLen Brown 
43717ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
4372b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
4373144b44b1SLen Brown 
4374144b44b1SLen Brown 	i = msr & 0xf;
4375144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
4376b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
43770a91e551SColin Ian King 		i = 3;
4378144b44b1SLen Brown 	}
4379144b44b1SLen Brown 	freq = slm_freq_table[i];
4380144b44b1SLen Brown 
438196e47158SLen Brown 	if (!quiet)
4382b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
4383144b44b1SLen Brown 
4384144b44b1SLen Brown 	return freq;
4385144b44b1SLen Brown }
4386144b44b1SLen Brown 
4387103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
4388103a8feaSLen Brown {
4389121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
4390103a8feaSLen Brown 		return 100.00;
4391144b44b1SLen Brown 	else if (is_slm(family, model))
4392144b44b1SLen Brown 		return slm_bclk();
4393103a8feaSLen Brown 	else
4394103a8feaSLen Brown 		return 133.33;
4395103a8feaSLen Brown }
4396103a8feaSLen Brown 
4397889facbeSLen Brown /*
4398889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
4399889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
4400889facbeSLen Brown  * This is usually equal to tjMax.
4401889facbeSLen Brown  *
4402889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
4403889facbeSLen Brown  * but also allow cmdline over-ride with -T.
4404889facbeSLen Brown  *
4405889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
4406889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
4407889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
4408889facbeSLen Brown  */
4409889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4410889facbeSLen Brown {
4411889facbeSLen Brown 	unsigned long long msr;
4412889facbeSLen Brown 	unsigned int target_c_local;
4413889facbeSLen Brown 	int cpu;
4414889facbeSLen Brown 
4415889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
4416889facbeSLen Brown 	if (!(do_dts || do_ptm))
4417889facbeSLen Brown 		return 0;
4418889facbeSLen Brown 
4419889facbeSLen Brown 	/* this is a per-package concept */
4420889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4421889facbeSLen Brown 		return 0;
4422889facbeSLen Brown 
4423889facbeSLen Brown 	cpu = t->cpu_id;
4424889facbeSLen Brown 	if (cpu_migrate(cpu)) {
4425b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
4426889facbeSLen Brown 		return -1;
4427889facbeSLen Brown 	}
4428889facbeSLen Brown 
4429889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
4430889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
4431b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
4432889facbeSLen Brown 			cpu, tcc_activation_temp);
4433889facbeSLen Brown 		return 0;
4434889facbeSLen Brown 	}
4435889facbeSLen Brown 
4436889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
4437d7899447SLen Brown 	if (!do_nhm_platform_info)
4438889facbeSLen Brown 		goto guess;
4439889facbeSLen Brown 
44407ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
4441889facbeSLen Brown 		goto guess;
4442889facbeSLen Brown 
44433482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
4444889facbeSLen Brown 
444596e47158SLen Brown 	if (!quiet)
4446b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
4447889facbeSLen Brown 			cpu, msr, target_c_local);
4448889facbeSLen Brown 
44493482124aSJean Delvare 	if (!target_c_local)
4450889facbeSLen Brown 		goto guess;
4451889facbeSLen Brown 
4452889facbeSLen Brown 	tcc_activation_temp = target_c_local;
4453889facbeSLen Brown 
4454889facbeSLen Brown 	return 0;
4455889facbeSLen Brown 
4456889facbeSLen Brown guess:
4457889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
4458b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
4459889facbeSLen Brown 		cpu, tcc_activation_temp);
4460889facbeSLen Brown 
4461889facbeSLen Brown 	return 0;
4462889facbeSLen Brown }
446369807a63SLen Brown 
4464aa8d8cc7SLen Brown void decode_feature_control_msr(void)
4465aa8d8cc7SLen Brown {
4466aa8d8cc7SLen Brown 	unsigned long long msr;
4467aa8d8cc7SLen Brown 
4468aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4469aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4470aa8d8cc7SLen Brown 			base_cpu, msr,
4471aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4472aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
4473aa8d8cc7SLen Brown }
4474aa8d8cc7SLen Brown 
447569807a63SLen Brown void decode_misc_enable_msr(void)
447669807a63SLen Brown {
447769807a63SLen Brown 	unsigned long long msr;
447869807a63SLen Brown 
4479f26b1519SLen Brown 	if (!genuine_intel)
4480f26b1519SLen Brown 		return;
4481f26b1519SLen Brown 
448269807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4483e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
448469807a63SLen Brown 			base_cpu, msr,
4485e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4486e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4487fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
4488e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4489e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
449069807a63SLen Brown }
449169807a63SLen Brown 
449233148d67SLen Brown void decode_misc_feature_control(void)
449333148d67SLen Brown {
449433148d67SLen Brown 	unsigned long long msr;
449533148d67SLen Brown 
449633148d67SLen Brown 	if (!has_misc_feature_control)
449733148d67SLen Brown 		return;
449833148d67SLen Brown 
449933148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
450033148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
450133148d67SLen Brown 			base_cpu, msr,
450233148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
450333148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
450433148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
450533148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
450633148d67SLen Brown }
4507f0057310SLen Brown /*
4508f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
4509f0057310SLen Brown  *
4510f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
4511f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
4512f0057310SLen Brown  * bit[1] less so...
4513f0057310SLen Brown  */
4514f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
4515f0057310SLen Brown {
4516f0057310SLen Brown 	unsigned long long msr;
4517f0057310SLen Brown 
4518f0057310SLen Brown 	if (!do_nhm_platform_info)
4519f0057310SLen Brown 		return;
4520f0057310SLen Brown 
4521cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
4522cf4cbe53SLen Brown 		return;
4523cf4cbe53SLen Brown 
4524f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4525ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4526f0057310SLen Brown 			base_cpu, msr,
4527f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
4528ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
4529ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
4530f0057310SLen Brown }
453171616c8eSLen Brown /*
453271616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
453371616c8eSLen Brown  *
453471616c8eSLen Brown  * This MSRs are present on Silvermont processors,
453571616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
453671616c8eSLen Brown  */
453771616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
453871616c8eSLen Brown {
453971616c8eSLen Brown 	unsigned long long msr;
454071616c8eSLen Brown 
454171616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
454271616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
454371616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
454471616c8eSLen Brown 
454571616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
454671616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
454771616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
454871616c8eSLen Brown }
45497f5c258eSLen Brown 
4550f5a4c76aSLen Brown /*
4551f5a4c76aSLen Brown  * When models are the same, for the purpose of turbostat, reuse
4552f5a4c76aSLen Brown  */
4553f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
4554f5a4c76aSLen Brown {
4555f5a4c76aSLen Brown 
4556f5a4c76aSLen Brown 	switch(model) {
4557f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
4558f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
4559f5a4c76aSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
4560f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
4561f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
4562f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM;
4563f5a4c76aSLen Brown 
4564f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
4565f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
4566f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM_EX;
4567f5a4c76aSLen Brown 
4568f5a4c76aSLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
4569f5a4c76aSLen Brown 		return INTEL_FAM6_XEON_PHI_KNL;
4570f5a4c76aSLen Brown 
4571f5a4c76aSLen Brown 	case INTEL_FAM6_HASWELL_ULT:
4572*c66f78a6SPeter Zijlstra 		return INTEL_FAM6_HASWELL;
4573f5a4c76aSLen Brown 
4574f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_X:
4575f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
4576f5a4c76aSLen Brown 		return INTEL_FAM6_BROADWELL_X;
4577f5a4c76aSLen Brown 
4578f5a4c76aSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:
4579*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE:
4580f5a4c76aSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:
4581*c66f78a6SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE:
4582f5a4c76aSLen Brown 		return INTEL_FAM6_SKYLAKE_MOBILE;
4583937807d3SLen Brown 
4584937807d3SLen Brown 	case INTEL_FAM6_ICELAKE_MOBILE:
4585937807d3SLen Brown 		return INTEL_FAM6_CANNONLAKE_MOBILE;
4586f5a4c76aSLen Brown 	}
4587f5a4c76aSLen Brown 	return model;
4588f5a4c76aSLen Brown }
4589fcd17211SLen Brown void process_cpuid()
4590103a8feaSLen Brown {
459134041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
459234041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
4593b3a34e93SLen Brown 	unsigned int has_turbo;
4594103a8feaSLen Brown 
4595103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
4596103a8feaSLen Brown 
45975aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
4598103a8feaSLen Brown 
459934041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
4600103a8feaSLen Brown 		genuine_intel = 1;
460134041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
460234041551SLen Brown 		authentic_amd = 1;
4603103a8feaSLen Brown 
460496e47158SLen Brown 	if (!quiet)
4605b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4606103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
4607103a8feaSLen Brown 
46085aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
4609103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
4610103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
4611103a8feaSLen Brown 	stepping = fms & 0xf;
46125aa3d1a2SCalvin Walton 	if (family == 0xf)
46135aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
46145aa3d1a2SCalvin Walton 	if (family >= 6)
4615103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
461634041551SLen Brown 	ecx_flags = ecx;
461734041551SLen Brown 	edx_flags = edx;
4618103a8feaSLen Brown 
4619103a8feaSLen Brown 	/*
4620103a8feaSLen Brown 	 * check max extended function levels of CPUID.
4621103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
4622103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
4623103a8feaSLen Brown 	 */
4624103a8feaSLen Brown 	ebx = ecx = edx = 0;
46255aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4626103a8feaSLen Brown 
462734041551SLen Brown 	if (!quiet) {
462834041551SLen Brown 		fprintf(outf, "0x%x CPUID levels; 0x%x xlevels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
462934041551SLen Brown 			max_level, max_extended_level, family, model, stepping, family, model, stepping);
463034041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
463134041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
463234041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
463334041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
463434041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
463534041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
463634041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
463734041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
463834041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
463934041551SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-",
464034041551SLen Brown 			edx_flags & (1 << 29) ? "TM" : "-");
464134041551SLen Brown 	}
4642f5a4c76aSLen Brown 	if (genuine_intel)
4643f5a4c76aSLen Brown 		model = intel_model_duplicates(model);
464434041551SLen Brown 
464534041551SLen Brown 	if (!(edx_flags & (1 << 5)))
464634041551SLen Brown 		errx(1, "CPUID: no MSR");
464734041551SLen Brown 
464861a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
4649103a8feaSLen Brown 
4650103a8feaSLen Brown 		/*
4651103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4652103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
4653103a8feaSLen Brown 		 */
46545aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
46558209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
4656d7899447SLen Brown 	}
4657103a8feaSLen Brown 
4658103a8feaSLen Brown 	/*
4659103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4660103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
4661103a8feaSLen Brown 	 */
4662103a8feaSLen Brown 
46635aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
46648209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
4665812db3f7SLen Brown 	if (has_aperf) {
4666812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
4667812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
4668812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
4669812db3f7SLen Brown 	}
4670889facbeSLen Brown 	do_dts = eax & (1 << 0);
4671812db3f7SLen Brown 	if (do_dts)
4672812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
4673b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
4674889facbeSLen Brown 	do_ptm = eax & (1 << 6);
4675812db3f7SLen Brown 	if (do_ptm)
4676812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
46777f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
46787f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
46797f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
46807f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
46817f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
4682889facbeSLen Brown 	has_epb = ecx & (1 << 3);
4683889facbeSLen Brown 
468496e47158SLen Brown 	if (!quiet)
4685b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
46867f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
46877f5c258eSLen Brown 			has_aperf ? "" : "No-",
4688b3a34e93SLen Brown 			has_turbo ? "" : "No-",
46897f5c258eSLen Brown 			do_dts ? "" : "No-",
46907f5c258eSLen Brown 			do_ptm ? "" : "No-",
46917f5c258eSLen Brown 			has_hwp ? "" : "No-",
46927f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
46937f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
46947f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
46957f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
46967f5c258eSLen Brown 			has_epb ? "" : "No-");
4697103a8feaSLen Brown 
469896e47158SLen Brown 	if (!quiet)
469969807a63SLen Brown 		decode_misc_enable_msr();
470069807a63SLen Brown 
470133148d67SLen Brown 
470296e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
4703aa8d8cc7SLen Brown 		int has_sgx;
4704aa8d8cc7SLen Brown 
4705aa8d8cc7SLen Brown 		ecx = 0;
4706aa8d8cc7SLen Brown 
4707aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4708aa8d8cc7SLen Brown 
4709aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
4710aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4711aa8d8cc7SLen Brown 
4712aa8d8cc7SLen Brown 		if (has_sgx)
4713aa8d8cc7SLen Brown 			decode_feature_control_msr();
4714aa8d8cc7SLen Brown 	}
4715aa8d8cc7SLen Brown 
471661a87ba7SLen Brown 	if (max_level >= 0x15) {
47178a5bdf41SLen Brown 		unsigned int eax_crystal;
47188a5bdf41SLen Brown 		unsigned int ebx_tsc;
47198a5bdf41SLen Brown 
47208a5bdf41SLen Brown 		/*
47218a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
47228a5bdf41SLen Brown 		 */
47238a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
47245aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
47258a5bdf41SLen Brown 
47268a5bdf41SLen Brown 		if (ebx_tsc != 0) {
47278a5bdf41SLen Brown 
472896e47158SLen Brown 			if (!quiet && (ebx != 0))
4729b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
47308a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
47318a5bdf41SLen Brown 
47328a5bdf41SLen Brown 			if (crystal_hz == 0)
47338a5bdf41SLen Brown 				switch(model) {
4734869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
4735e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
4736e8efbc80SLen Brown 					break;
4737f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_X:	/* DNV */
4738ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
4739ec53e594SLen Brown 					break;
4740869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4741f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4742e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
47438a5bdf41SLen Brown 					break;
47448a5bdf41SLen Brown 				default:
47458a5bdf41SLen Brown 					crystal_hz = 0;
47468a5bdf41SLen Brown 			}
47478a5bdf41SLen Brown 
47488a5bdf41SLen Brown 			if (crystal_hz) {
47498a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
475096e47158SLen Brown 				if (!quiet)
4751b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
47528a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
47538a5bdf41SLen Brown 			}
47548a5bdf41SLen Brown 		}
47558a5bdf41SLen Brown 	}
475661a87ba7SLen Brown 	if (max_level >= 0x16) {
475761a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
475861a87ba7SLen Brown 
475961a87ba7SLen Brown 		/*
476061a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
476161a87ba7SLen Brown 		 */
476261a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
476361a87ba7SLen Brown 
47645aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
476596e47158SLen Brown 		if (!quiet)
4766b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
476761a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
476861a87ba7SLen Brown 	}
47698a5bdf41SLen Brown 
4770b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
4771b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4772b2b34dfeSHubert Chrzaniuk 
4773812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
4774812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4775812db3f7SLen Brown 
4776812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4777812db3f7SLen Brown 		do_nhm_platform_info = 1;
4778812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4779812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4780812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4781812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4782812db3f7SLen Brown 	}
4783d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4784812db3f7SLen Brown 
4785812db3f7SLen Brown 	if (do_snb_cstates)
4786812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4787812db3f7SLen Brown 
47885a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
47890f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
47900f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
47910f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
47920f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
47930f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
47940f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
47950f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
47960f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
47970539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
47980f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
47990f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
48000f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
48010f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
48020539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
48030539ba11SLen Brown 		use_c1_residency_msr = 1;
48040539ba11SLen Brown 	}
48057170a374SLen Brown 	if (is_dnv(family, model)) {
48067170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
48077170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
48087170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
48097170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
48107170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
48117170a374SLen Brown 		use_c1_residency_msr = 1;
48127170a374SLen Brown 	}
481334c76197SLen Brown 	if (is_skx(family, model)) {
481434c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
481534c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
481634c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
481734c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
481834c76197SLen Brown 	}
4819ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4820ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4821ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4822ade0ebacSLen Brown 	}
48230f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
48240f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
48250f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
48260f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
48270f47c08dSLen Brown 	}
48285a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
4829a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
4830a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
4831a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
4832a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
4833a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
4834a99d8730SLen Brown 	}
4835144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4836fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4837103a8feaSLen Brown 
483831a1f15cSLen Brown 	if (do_slm_cstates || do_knl_cstates || is_cnl(family, model))
4839562855eeSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
4840103a8feaSLen Brown 
484196e47158SLen Brown 	if (!quiet)
4842f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4843f0057310SLen Brown 
484496e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
484571616c8eSLen Brown 		decode_c6_demotion_policy_msr();
484671616c8eSLen Brown 
4847889facbeSLen Brown 	rapl_probe(family, model);
48483a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4849ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
4850889facbeSLen Brown 
485196e47158SLen Brown 	if (!quiet)
48521b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4853fcd17211SLen Brown 
485441618e63SLen Brown 	if (!quiet)
485541618e63SLen Brown 		dump_sysfs_cstate_config();
48567293fccdSLen Brown 	if (!quiet)
48577293fccdSLen Brown 		dump_sysfs_pstate_config();
485841618e63SLen Brown 
4859a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4860a2b7b749SLen Brown 		calculate_tsc_tweak();
4861a2b7b749SLen Brown 
4862812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4863812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4864fdf676e5SLen Brown 
4865812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4866812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
486727d47356SLen Brown 
4868be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
4869be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
4870be0e54c4SLen Brown 	else
4871be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
4872be0e54c4SLen Brown 
4873be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us", R_OK))
4874be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
4875be0e54c4SLen Brown 	else
4876be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
4877be0e54c4SLen Brown 
487896e47158SLen Brown 	if (!quiet)
487933148d67SLen Brown 		decode_misc_feature_control();
488033148d67SLen Brown 
4881889facbeSLen Brown 	return;
4882103a8feaSLen Brown }
4883103a8feaSLen Brown 
4884103a8feaSLen Brown /*
4885103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4886103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4887103a8feaSLen Brown  */
4888103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4889103a8feaSLen Brown {
4890103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4891103a8feaSLen Brown 		return 1;
4892103a8feaSLen Brown 	else
4893103a8feaSLen Brown 		return 0;
4894103a8feaSLen Brown }
4895103a8feaSLen Brown 
4896103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4897103a8feaSLen Brown {
4898103a8feaSLen Brown 	return 0;
4899103a8feaSLen Brown }
4900103a8feaSLen Brown 
4901c98d5d94SLen Brown void topology_probe()
4902c98d5d94SLen Brown {
4903c98d5d94SLen Brown 	int i;
4904c98d5d94SLen Brown 	int max_core_id = 0;
4905c98d5d94SLen Brown 	int max_package_id = 0;
49066de68fe1SLen Brown 	int max_die_id = 0;
4907c98d5d94SLen Brown 	int max_siblings = 0;
4908c98d5d94SLen Brown 
4909c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4910843c5791SPrarit Bhargava 	set_max_cpu_num();
4911c98d5d94SLen Brown 	topo.num_cpus = 0;
4912c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4913c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4914812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4915c98d5d94SLen Brown 
4916d8af6f5fSLen Brown 	if (debug > 1)
4917b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4918c98d5d94SLen Brown 
4919c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4920b2c95d90SJosh Triplett 	if (cpus == NULL)
4921b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4922c98d5d94SLen Brown 
4923c98d5d94SLen Brown 	/*
4924c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4925c98d5d94SLen Brown 	 */
4926c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4927b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4928b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4929c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4930c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4931c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4932c98d5d94SLen Brown 
4933c98d5d94SLen Brown 	/*
49341ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
49351ef7d21aSLen Brown 	 */
49361ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
49371ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
49381ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
49391ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
49401ef7d21aSLen Brown 	}
49411ef7d21aSLen Brown 
49421ef7d21aSLen Brown 	/*
4943c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4944c98d5d94SLen Brown 	 */
4945c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4946b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4947b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4948c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4949c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4950c98d5d94SLen Brown 
49518cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
4952c98d5d94SLen Brown 
4953c98d5d94SLen Brown 	/*
4954c98d5d94SLen Brown 	 * For online cpus
4955c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4956c98d5d94SLen Brown 	 */
4957c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4958c98d5d94SLen Brown 		int siblings;
4959c98d5d94SLen Brown 
4960c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4961d8af6f5fSLen Brown 			if (debug > 1)
4962b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4963c98d5d94SLen Brown 			continue;
4964c98d5d94SLen Brown 		}
4965c98d5d94SLen Brown 
49660e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
49670e2d8f05SLen Brown 
49680e2d8f05SLen Brown 		/* get package information */
4969c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4970c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4971c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4972c98d5d94SLen Brown 
49736de68fe1SLen Brown 		/* get die information */
49746de68fe1SLen Brown 		cpus[i].die_id = get_die_id(i);
49756de68fe1SLen Brown 		if (cpus[i].die_id > max_die_id)
49766de68fe1SLen Brown 			max_die_id = cpus[i].die_id;
49776de68fe1SLen Brown 
49780e2d8f05SLen Brown 		/* get numa node information */
4979ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
4980ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
4981ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
49820e2d8f05SLen Brown 
49830e2d8f05SLen Brown 		/* get core information */
49840e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
49850e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
49860e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
49870e2d8f05SLen Brown 
49880e2d8f05SLen Brown 		/* get thread information */
49890e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
4990c98d5d94SLen Brown 		if (siblings > max_siblings)
4991c98d5d94SLen Brown 			max_siblings = siblings;
49924f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
49938cb48b32SPrarit Bhargava 			topo.num_cores++;
4994c98d5d94SLen Brown 	}
4995ef605741SPrarit Bhargava 
499670a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
4997d8af6f5fSLen Brown 	if (debug > 1)
4998b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
499970a9c6e8SPrarit Bhargava 			max_core_id, topo.cores_per_node);
500070a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
5001812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
5002c98d5d94SLen Brown 
50036de68fe1SLen Brown 	topo.num_die = max_die_id + 1;
50046de68fe1SLen Brown 	if (debug > 1)
50056de68fe1SLen Brown 		fprintf(outf, "max_die_id %d, sizing for %d die\n",
50066de68fe1SLen Brown 				max_die_id, topo.num_die);
50076de68fe1SLen Brown 	if (!summary_only && topo.num_die > 1)
50086de68fe1SLen Brown 		BIC_PRESENT(BIC_Die);
50096de68fe1SLen Brown 
5010c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
5011d8af6f5fSLen Brown 	if (debug > 1)
5012b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
5013c98d5d94SLen Brown 			max_package_id, topo.num_packages);
50147da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
5015812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
5016c98d5d94SLen Brown 
5017ef605741SPrarit Bhargava 	set_node_data();
5018ef605741SPrarit Bhargava 	if (debug > 1)
501970a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
502001235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
502101235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
5022ef605741SPrarit Bhargava 
502370a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
5024d8af6f5fSLen Brown 	if (debug > 1)
5025b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
50262ffbb224SPrarit Bhargava 
50272ffbb224SPrarit Bhargava 	if (debug < 1)
50282ffbb224SPrarit Bhargava 		return;
50292ffbb224SPrarit Bhargava 
50302ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
50310ec712e3SLen Brown 		if (cpu_is_not_present(i))
50320ec712e3SLen Brown 			continue;
50332ffbb224SPrarit Bhargava 		fprintf(outf,
50346de68fe1SLen Brown 			"cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
50356de68fe1SLen Brown 			i, cpus[i].physical_package_id, cpus[i].die_id,
50362ffbb224SPrarit Bhargava 			cpus[i].physical_node_id,
50372ffbb224SPrarit Bhargava 			cpus[i].logical_node_id,
50382ffbb224SPrarit Bhargava 			cpus[i].physical_core_id,
50392ffbb224SPrarit Bhargava 			cpus[i].thread_id);
50402ffbb224SPrarit Bhargava 	}
50412ffbb224SPrarit Bhargava 
5042c98d5d94SLen Brown }
5043c98d5d94SLen Brown 
5044c98d5d94SLen Brown void
504540f5cfe7SPrarit Bhargava allocate_counters(struct thread_data **t, struct core_data **c,
504640f5cfe7SPrarit Bhargava 		  struct pkg_data **p)
5047c98d5d94SLen Brown {
5048c98d5d94SLen Brown 	int i;
504940f5cfe7SPrarit Bhargava 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg *
505040f5cfe7SPrarit Bhargava 			topo.num_packages;
505140f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
5052c98d5d94SLen Brown 
505340f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
5054c98d5d94SLen Brown 	if (*t == NULL)
5055c98d5d94SLen Brown 		goto error;
5056c98d5d94SLen Brown 
505740f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
5058c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
5059c98d5d94SLen Brown 
506040f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
5061c98d5d94SLen Brown 	if (*c == NULL)
5062c98d5d94SLen Brown 		goto error;
5063c98d5d94SLen Brown 
506440f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
5065c98d5d94SLen Brown 		(*c)[i].core_id = -1;
5066c98d5d94SLen Brown 
5067678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
5068c98d5d94SLen Brown 	if (*p == NULL)
5069c98d5d94SLen Brown 		goto error;
5070c98d5d94SLen Brown 
5071c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
5072c98d5d94SLen Brown 		(*p)[i].package_id = i;
5073c98d5d94SLen Brown 
5074c98d5d94SLen Brown 	return;
5075c98d5d94SLen Brown error:
5076b2c95d90SJosh Triplett 	err(1, "calloc counters");
5077c98d5d94SLen Brown }
5078c98d5d94SLen Brown /*
5079c98d5d94SLen Brown  * init_counter()
5080c98d5d94SLen Brown  *
5081c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5082c98d5d94SLen Brown  */
5083c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
50848cb48b32SPrarit Bhargava 	struct pkg_data *pkg_base, int cpu_id)
5085c98d5d94SLen Brown {
50868cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
508740f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
50888cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
50898cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
5090c98d5d94SLen Brown 	struct thread_data *t;
5091c98d5d94SLen Brown 	struct core_data *c;
5092c98d5d94SLen Brown 	struct pkg_data *p;
5093c98d5d94SLen Brown 
509442dd4520SNathan Ciobanu 
509542dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
509642dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
509742dd4520SNathan Ciobanu 	 */
509842dd4520SNathan Ciobanu 	if (node_id < 0)
509942dd4520SNathan Ciobanu 		node_id = 0;
510042dd4520SNathan Ciobanu 
510140f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
510240f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
51038cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
5104c98d5d94SLen Brown 
5105c98d5d94SLen Brown 	t->cpu_id = cpu_id;
51068cb48b32SPrarit Bhargava 	if (thread_id == 0) {
5107c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5108c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
5109c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5110c98d5d94SLen Brown 	}
5111c98d5d94SLen Brown 
51128cb48b32SPrarit Bhargava 	c->core_id = core_id;
51138cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
5114c98d5d94SLen Brown }
5115c98d5d94SLen Brown 
5116c98d5d94SLen Brown 
5117c98d5d94SLen Brown int initialize_counters(int cpu_id)
5118c98d5d94SLen Brown {
51198cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
51208cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
5121c98d5d94SLen Brown 	return 0;
5122c98d5d94SLen Brown }
5123c98d5d94SLen Brown 
5124c98d5d94SLen Brown void allocate_output_buffer()
5125c98d5d94SLen Brown {
51263b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
5127c98d5d94SLen Brown 	outp = output_buffer;
5128b2c95d90SJosh Triplett 	if (outp == NULL)
5129b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
5130c98d5d94SLen Brown }
513136229897SLen Brown void allocate_fd_percpu(void)
513236229897SLen Brown {
513301a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
513436229897SLen Brown 	if (fd_percpu == NULL)
513536229897SLen Brown 		err(-1, "calloc fd_percpu");
513636229897SLen Brown }
5137562a2d37SLen Brown void allocate_irq_buffers(void)
5138562a2d37SLen Brown {
5139562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5140562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
5141562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
5142c98d5d94SLen Brown 
514301a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5144562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
514501a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
5146562a2d37SLen Brown }
5147c98d5d94SLen Brown void setup_all_buffers(void)
5148c98d5d94SLen Brown {
5149c98d5d94SLen Brown 	topology_probe();
5150562a2d37SLen Brown 	allocate_irq_buffers();
515136229897SLen Brown 	allocate_fd_percpu();
5152c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
5153c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
5154c98d5d94SLen Brown 	allocate_output_buffer();
5155c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
5156c98d5d94SLen Brown }
51573b4d5c7fSAndy Shevchenko 
51587ce7d5deSPrarit Bhargava void set_base_cpu(void)
51597ce7d5deSPrarit Bhargava {
51607ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
51617ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
51627ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
51637ce7d5deSPrarit Bhargava 
51647ce7d5deSPrarit Bhargava 	if (debug > 1)
5165b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
51667ce7d5deSPrarit Bhargava }
51677ce7d5deSPrarit Bhargava 
5168103a8feaSLen Brown void turbostat_init()
5169103a8feaSLen Brown {
51707ce7d5deSPrarit Bhargava 	setup_all_buffers();
51717ce7d5deSPrarit Bhargava 	set_base_cpu();
5172103a8feaSLen Brown 	check_dev_msr();
517398481e79SLen Brown 	check_permissions();
5174fcd17211SLen Brown 	process_cpuid();
5175103a8feaSLen Brown 
5176103a8feaSLen Brown 
517796e47158SLen Brown 	if (!quiet)
51787f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
51797f5c258eSLen Brown 
518096e47158SLen Brown 	if (!quiet)
5181889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5182889facbeSLen Brown 
518396e47158SLen Brown 	if (!quiet)
51843a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
51853a9a941dSLen Brown 
518696e47158SLen Brown 	if (!quiet)
5187889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5188889facbeSLen Brown 
5189889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5190889facbeSLen Brown 
519196e47158SLen Brown 	if (!quiet)
5192889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
51935a63426eSLen Brown 
519496e47158SLen Brown 	if (!quiet && do_irtl_snb)
51955a63426eSLen Brown 		print_irtl();
5196103a8feaSLen Brown }
5197103a8feaSLen Brown 
5198103a8feaSLen Brown int fork_it(char **argv)
5199103a8feaSLen Brown {
5200103a8feaSLen Brown 	pid_t child_pid;
5201d91bb17cSLen Brown 	int status;
5202d15cf7c1SLen Brown 
5203218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5204d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
52054c2122d4SLen Brown 	first_counter_read = 0;
5206d91bb17cSLen Brown 	if (status)
5207d91bb17cSLen Brown 		exit(status);
5208d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5209d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5210103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5211103a8feaSLen Brown 
5212103a8feaSLen Brown 	child_pid = fork();
5213103a8feaSLen Brown 	if (!child_pid) {
5214103a8feaSLen Brown 		/* child */
5215103a8feaSLen Brown 		execvp(argv[0], argv);
52160815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5217103a8feaSLen Brown 	} else {
5218103a8feaSLen Brown 
5219103a8feaSLen Brown 		/* parent */
5220b2c95d90SJosh Triplett 		if (child_pid == -1)
5221b2c95d90SJosh Triplett 			err(1, "fork");
5222103a8feaSLen Brown 
5223103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
5224103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
5225b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
5226b2c95d90SJosh Triplett 			err(status, "waitpid");
52272a954966SDavid Arcari 
52282a954966SDavid Arcari 		if (WIFEXITED(status))
52292a954966SDavid Arcari 			status = WEXITSTATUS(status);
5230103a8feaSLen Brown 	}
5231c98d5d94SLen Brown 	/*
5232c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
5233c98d5d94SLen Brown 	 * because re-starting is problematic when forking
5234c98d5d94SLen Brown 	 */
5235218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5236c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
5237103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
5238103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
5239ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
5240ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
5241ba3dec99SLen Brown 	else {
5242c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
5243c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
5244ba3dec99SLen Brown 	}
5245103a8feaSLen Brown 
5246b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
5247b7d8c148SLen Brown 
5248b7d8c148SLen Brown 	flush_output_stderr();
5249103a8feaSLen Brown 
5250d91bb17cSLen Brown 	return status;
5251103a8feaSLen Brown }
5252103a8feaSLen Brown 
52533b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
52543b4d5c7fSAndy Shevchenko {
52553b4d5c7fSAndy Shevchenko 	int status;
52563b4d5c7fSAndy Shevchenko 
5257218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
52583b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
52593b4d5c7fSAndy Shevchenko 	if (status)
52603b4d5c7fSAndy Shevchenko 		return status;
52613b4d5c7fSAndy Shevchenko 
52623b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
52633b4d5c7fSAndy Shevchenko 	if (status)
52643b4d5c7fSAndy Shevchenko 		return status;
52653b4d5c7fSAndy Shevchenko 
5266b7d8c148SLen Brown 	flush_output_stdout();
52673b4d5c7fSAndy Shevchenko 
52683b4d5c7fSAndy Shevchenko 	return status;
52693b4d5c7fSAndy Shevchenko }
52703b4d5c7fSAndy Shevchenko 
5271d8af6f5fSLen Brown void print_version() {
52720f71d089SLen Brown 	fprintf(outf, "turbostat version 19.03.20"
5273d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
5274d8af6f5fSLen Brown }
5275d8af6f5fSLen Brown 
5276495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
5277495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
527841618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
5279388e9c81SLen Brown {
5280388e9c81SLen Brown 	struct msr_counter *msrp;
5281388e9c81SLen Brown 
5282388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
5283388e9c81SLen Brown 	if (msrp == NULL) {
5284388e9c81SLen Brown 		perror("calloc");
5285388e9c81SLen Brown 		exit(1);
5286388e9c81SLen Brown 	}
5287388e9c81SLen Brown 
5288388e9c81SLen Brown 	msrp->msr_num = msr_num;
5289388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
5290495c7654SLen Brown 	if (path)
5291495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
5292388e9c81SLen Brown 	msrp->width = width;
5293388e9c81SLen Brown 	msrp->type = type;
5294388e9c81SLen Brown 	msrp->format = format;
529541618e63SLen Brown 	msrp->flags = flags;
5296388e9c81SLen Brown 
5297388e9c81SLen Brown 	switch (scope) {
5298388e9c81SLen Brown 
5299388e9c81SLen Brown 	case SCOPE_CPU:
5300388e9c81SLen Brown 		msrp->next = sys.tp;
5301388e9c81SLen Brown 		sys.tp = msrp;
5302678a3bd1SLen Brown 		sys.added_thread_counters++;
53030748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
5304678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
5305678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5306678a3bd1SLen Brown 			exit(-1);
5307678a3bd1SLen Brown 		}
5308388e9c81SLen Brown 		break;
5309388e9c81SLen Brown 
5310388e9c81SLen Brown 	case SCOPE_CORE:
5311388e9c81SLen Brown 		msrp->next = sys.cp;
5312388e9c81SLen Brown 		sys.cp = msrp;
5313678a3bd1SLen Brown 		sys.added_core_counters++;
5314678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
5315678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
5316678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5317678a3bd1SLen Brown 			exit(-1);
5318678a3bd1SLen Brown 		}
5319388e9c81SLen Brown 		break;
5320388e9c81SLen Brown 
5321388e9c81SLen Brown 	case SCOPE_PACKAGE:
5322388e9c81SLen Brown 		msrp->next = sys.pp;
5323388e9c81SLen Brown 		sys.pp = msrp;
5324678a3bd1SLen Brown 		sys.added_package_counters++;
5325678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
5326678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
5327678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
5328678a3bd1SLen Brown 			exit(-1);
5329678a3bd1SLen Brown 		}
5330388e9c81SLen Brown 		break;
5331388e9c81SLen Brown 	}
5332388e9c81SLen Brown 
5333388e9c81SLen Brown 	return 0;
5334388e9c81SLen Brown }
5335388e9c81SLen Brown 
5336388e9c81SLen Brown void parse_add_command(char *add_command)
5337388e9c81SLen Brown {
5338388e9c81SLen Brown 	int msr_num = 0;
5339495c7654SLen Brown 	char *path = NULL;
53400f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
5341388e9c81SLen Brown 	int width = 64;
5342388e9c81SLen Brown 	int fail = 0;
5343388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
5344388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
5345388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
5346388e9c81SLen Brown 
5347388e9c81SLen Brown 	while (add_command) {
5348388e9c81SLen Brown 
5349388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
5350388e9c81SLen Brown 			goto next;
5351388e9c81SLen Brown 
5352388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
5353388e9c81SLen Brown 			goto next;
5354388e9c81SLen Brown 
5355495c7654SLen Brown 		if (*add_command == '/') {
5356495c7654SLen Brown 			path = add_command;
5357495c7654SLen Brown 			goto next;
5358495c7654SLen Brown 		}
5359495c7654SLen Brown 
5360388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
5361388e9c81SLen Brown 			if ((width == 32) || (width == 64))
5362388e9c81SLen Brown 				goto next;
5363388e9c81SLen Brown 			width = 64;
5364388e9c81SLen Brown 		}
5365388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
5366388e9c81SLen Brown 			scope = SCOPE_CPU;
5367388e9c81SLen Brown 			goto next;
5368388e9c81SLen Brown 		}
5369388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
5370388e9c81SLen Brown 			scope = SCOPE_CORE;
5371388e9c81SLen Brown 			goto next;
5372388e9c81SLen Brown 		}
5373388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
5374388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
5375388e9c81SLen Brown 			goto next;
5376388e9c81SLen Brown 		}
5377388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
5378388e9c81SLen Brown 			type = COUNTER_CYCLES;
5379388e9c81SLen Brown 			goto next;
5380388e9c81SLen Brown 		}
5381388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
5382388e9c81SLen Brown 			type = COUNTER_SECONDS;
5383388e9c81SLen Brown 			goto next;
5384388e9c81SLen Brown 		}
538541618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
538641618e63SLen Brown 			type = COUNTER_USEC;
538741618e63SLen Brown 			goto next;
538841618e63SLen Brown 		}
5389388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
5390388e9c81SLen Brown 			format = FORMAT_RAW;
5391388e9c81SLen Brown 			goto next;
5392388e9c81SLen Brown 		}
5393388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
5394388e9c81SLen Brown 			format = FORMAT_DELTA;
5395388e9c81SLen Brown 			goto next;
5396388e9c81SLen Brown 		}
5397388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
5398388e9c81SLen Brown 			format = FORMAT_PERCENT;
5399388e9c81SLen Brown 			goto next;
5400388e9c81SLen Brown 		}
5401388e9c81SLen Brown 
5402388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
5403388e9c81SLen Brown 			char *eos;
5404388e9c81SLen Brown 
5405388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
5406388e9c81SLen Brown 			if (eos)
5407388e9c81SLen Brown 				*eos = '\0';
5408388e9c81SLen Brown 			goto next;
5409388e9c81SLen Brown 		}
5410388e9c81SLen Brown 
5411388e9c81SLen Brown next:
5412388e9c81SLen Brown 		add_command = strchr(add_command, ',');
5413495c7654SLen Brown 		if (add_command) {
5414495c7654SLen Brown 			*add_command = '\0';
5415388e9c81SLen Brown 			add_command++;
5416495c7654SLen Brown 		}
5417388e9c81SLen Brown 
5418388e9c81SLen Brown 	}
5419495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
5420495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
5421388e9c81SLen Brown 		fail++;
5422388e9c81SLen Brown 	}
5423388e9c81SLen Brown 
5424388e9c81SLen Brown 	/* generate default column header */
5425388e9c81SLen Brown 	if (*name_buffer == '\0') {
5426388e9c81SLen Brown 		if (width == 32)
54275f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5428388e9c81SLen Brown 		else
54295f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
5430388e9c81SLen Brown 	}
5431388e9c81SLen Brown 
543241618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
5433388e9c81SLen Brown 		fail++;
5434388e9c81SLen Brown 
5435388e9c81SLen Brown 	if (fail) {
5436388e9c81SLen Brown 		help();
5437388e9c81SLen Brown 		exit(1);
5438388e9c81SLen Brown 	}
5439388e9c81SLen Brown }
544041618e63SLen Brown 
5441dd778a5eSLen Brown int is_deferred_skip(char *name)
5442dd778a5eSLen Brown {
5443dd778a5eSLen Brown 	int i;
5444dd778a5eSLen Brown 
5445dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
5446dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
5447dd778a5eSLen Brown 			return 1;
5448dd778a5eSLen Brown 	return 0;
5449dd778a5eSLen Brown }
5450dd778a5eSLen Brown 
545141618e63SLen Brown void probe_sysfs(void)
545241618e63SLen Brown {
545341618e63SLen Brown 	char path[64];
545441618e63SLen Brown 	char name_buf[16];
545541618e63SLen Brown 	FILE *input;
545641618e63SLen Brown 	int state;
545741618e63SLen Brown 	char *sp;
545841618e63SLen Brown 
545941618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
546041618e63SLen Brown 		return;
546141618e63SLen Brown 
54620748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
546341618e63SLen Brown 
546441618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
546541618e63SLen Brown 			base_cpu, state);
546641618e63SLen Brown 		input = fopen(path, "r");
546741618e63SLen Brown 		if (input == NULL)
546841618e63SLen Brown 			continue;
54698173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
54708173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
547141618e63SLen Brown 
547241618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
547341618e63SLen Brown 		sp = strchr(name_buf, '-');
547441618e63SLen Brown 		if (!sp)
547541618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
547641618e63SLen Brown 		*sp = '%';
547741618e63SLen Brown 		*(sp + 1) = '\0';
547841618e63SLen Brown 
547941618e63SLen Brown 		fclose(input);
548041618e63SLen Brown 
548141618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
548241618e63SLen Brown 
5483dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5484dd778a5eSLen Brown 			continue;
5485dd778a5eSLen Brown 
548641618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
548741618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
548841618e63SLen Brown 	}
548941618e63SLen Brown 
54900748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
549141618e63SLen Brown 
549241618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
549341618e63SLen Brown 			base_cpu, state);
549441618e63SLen Brown 		input = fopen(path, "r");
549541618e63SLen Brown 		if (input == NULL)
549641618e63SLen Brown 			continue;
54978173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
54988173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
549941618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
550041618e63SLen Brown 		sp = strchr(name_buf, '-');
550141618e63SLen Brown 		if (!sp)
550241618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
550341618e63SLen Brown 		*sp = '\0';
550441618e63SLen Brown 		fclose(input);
550541618e63SLen Brown 
550641618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
550741618e63SLen Brown 
5508dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
5509dd778a5eSLen Brown 			continue;
5510dd778a5eSLen Brown 
551141618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
551241618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
551341618e63SLen Brown 	}
551441618e63SLen Brown 
551541618e63SLen Brown }
551641618e63SLen Brown 
55171ef7d21aSLen Brown 
55181ef7d21aSLen Brown /*
55191ef7d21aSLen Brown  * parse cpuset with following syntax
55201ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
55211ef7d21aSLen Brown  */
55221ef7d21aSLen Brown void parse_cpu_command(char *optarg)
55231ef7d21aSLen Brown {
55241ef7d21aSLen Brown 	unsigned int start, end;
55251ef7d21aSLen Brown 	char *next;
55261ef7d21aSLen Brown 
55274e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
55284e4e1e7cSLen Brown 		if (cpu_subset)
55294e4e1e7cSLen Brown 			goto error;
55304e4e1e7cSLen Brown 		show_core_only++;
55314e4e1e7cSLen Brown 		return;
55324e4e1e7cSLen Brown 	}
55334e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
55344e4e1e7cSLen Brown 		if (cpu_subset)
55354e4e1e7cSLen Brown 			goto error;
55364e4e1e7cSLen Brown 		show_pkg_only++;
55374e4e1e7cSLen Brown 		return;
55384e4e1e7cSLen Brown 	}
55394e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
55404e4e1e7cSLen Brown 		goto error;
55414e4e1e7cSLen Brown 
55421ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
55431ef7d21aSLen Brown 	if (cpu_subset == NULL)
55441ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
55451ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
55461ef7d21aSLen Brown 
55471ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
55481ef7d21aSLen Brown 
55491ef7d21aSLen Brown 	next = optarg;
55501ef7d21aSLen Brown 
55511ef7d21aSLen Brown 	while (next && *next) {
55521ef7d21aSLen Brown 
55531ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
55541ef7d21aSLen Brown 			goto error;
55551ef7d21aSLen Brown 
55561ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
55571ef7d21aSLen Brown 
55581ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
55591ef7d21aSLen Brown 			goto error;
55601ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
55611ef7d21aSLen Brown 
55621ef7d21aSLen Brown 		if (*next == '\0')
55631ef7d21aSLen Brown 			break;
55641ef7d21aSLen Brown 
55651ef7d21aSLen Brown 		if (*next == ',') {
55661ef7d21aSLen Brown 			next += 1;
55671ef7d21aSLen Brown 			continue;
55681ef7d21aSLen Brown 		}
55691ef7d21aSLen Brown 
55701ef7d21aSLen Brown 		if (*next == '-') {
55711ef7d21aSLen Brown 			next += 1;	/* start range */
55721ef7d21aSLen Brown 		} else if (*next == '.') {
55731ef7d21aSLen Brown 			next += 1;
55741ef7d21aSLen Brown 			if (*next == '.')
55751ef7d21aSLen Brown 				next += 1;	/* start range */
55761ef7d21aSLen Brown 			else
55771ef7d21aSLen Brown 				goto error;
55781ef7d21aSLen Brown 		}
55791ef7d21aSLen Brown 
55801ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
55811ef7d21aSLen Brown 		if (end <= start)
55821ef7d21aSLen Brown 			goto error;
55831ef7d21aSLen Brown 
55841ef7d21aSLen Brown 		while (++start <= end) {
55851ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
55861ef7d21aSLen Brown 				goto error;
55871ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
55881ef7d21aSLen Brown 		}
55891ef7d21aSLen Brown 
55901ef7d21aSLen Brown 		if (*next == ',')
55911ef7d21aSLen Brown 			next += 1;
55921ef7d21aSLen Brown 		else if (*next != '\0')
55931ef7d21aSLen Brown 			goto error;
55941ef7d21aSLen Brown 	}
55951ef7d21aSLen Brown 
55961ef7d21aSLen Brown 	return;
55971ef7d21aSLen Brown 
55981ef7d21aSLen Brown error:
55994e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
56004e4e1e7cSLen Brown 	help();
56011ef7d21aSLen Brown 	exit(-1);
56021ef7d21aSLen Brown }
56031ef7d21aSLen Brown 
5604812db3f7SLen Brown 
5605103a8feaSLen Brown void cmdline(int argc, char **argv)
5606103a8feaSLen Brown {
5607103a8feaSLen Brown 	int opt;
5608d8af6f5fSLen Brown 	int option_index = 0;
5609d8af6f5fSLen Brown 	static struct option long_options[] = {
5610388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
56111ef7d21aSLen Brown 		{"cpu",		required_argument,	0, 'c'},
5612d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
561396e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
56143f44a5c6SLen Brown 		{"enable",	required_argument,	0, 'e'},
5615d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
5616023fe0acSChen Yu 		{"num_iterations",	required_argument,	0, 'n'},
5617d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
5618812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
5619d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
5620c8ade361SLen Brown 		{"list",	no_argument,		0, 'l'},
5621b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
562296e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
5623812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
5624d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
5625d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
5626d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
5627d8af6f5fSLen Brown 		{0,		0,			0,  0 }
5628d8af6f5fSLen Brown 	};
5629103a8feaSLen Brown 
5630103a8feaSLen Brown 	progname = argv[0];
5631103a8feaSLen Brown 
5632023fe0acSChen Yu 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v",
5633d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
5634103a8feaSLen Brown 		switch (opt) {
5635388e9c81SLen Brown 		case 'a':
5636388e9c81SLen Brown 			parse_add_command(optarg);
5637388e9c81SLen Brown 			break;
56381ef7d21aSLen Brown 		case 'c':
56391ef7d21aSLen Brown 			parse_cpu_command(optarg);
56401ef7d21aSLen Brown 			break;
5641d8af6f5fSLen Brown 		case 'D':
5642d8af6f5fSLen Brown 			dump_only++;
56438e180f3cSLen Brown 			break;
56443f44a5c6SLen Brown 		case 'e':
56453f44a5c6SLen Brown 			/* --enable specified counter */
56464c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
56473f44a5c6SLen Brown 			break;
5648d8af6f5fSLen Brown 		case 'd':
5649d8af6f5fSLen Brown 			debug++;
56503f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
56512f32edf1SLen Brown 			break;
5652812db3f7SLen Brown 		case 'H':
56533f44a5c6SLen Brown 			/*
56543f44a5c6SLen Brown 			 * --hide: do not show those specified
56553f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
56563f44a5c6SLen Brown 			 */
56573f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5658812db3f7SLen Brown 			break;
5659d8af6f5fSLen Brown 		case 'h':
5660d8af6f5fSLen Brown 		default:
5661d8af6f5fSLen Brown 			help();
5662d8af6f5fSLen Brown 			exit(1);
5663d8af6f5fSLen Brown 		case 'i':
56642a0609c0SLen Brown 			{
56652a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
56662a0609c0SLen Brown 
56672a0609c0SLen Brown 				if (interval < 0.001) {
5668b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
56692a0609c0SLen Brown 						interval);
56702a0609c0SLen Brown 					exit(2);
56712a0609c0SLen Brown 				}
56722a0609c0SLen Brown 
567347936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
5674b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
567547936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
56762a0609c0SLen Brown 			}
5677889facbeSLen Brown 			break;
56785c56be9aSDirk Brandewie 		case 'J':
56795c56be9aSDirk Brandewie 			rapl_joules++;
56805c56be9aSDirk Brandewie 			break;
5681c8ade361SLen Brown 		case 'l':
56823f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5683c8ade361SLen Brown 			list_header_only++;
5684c8ade361SLen Brown 			quiet++;
5685c8ade361SLen Brown 			break;
5686b7d8c148SLen Brown 		case 'o':
5687b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
5688b7d8c148SLen Brown 			break;
568996e47158SLen Brown 		case 'q':
569096e47158SLen Brown 			quiet = 1;
569196e47158SLen Brown 			break;
5692023fe0acSChen Yu 		case 'n':
5693023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
5694023fe0acSChen Yu 
5695023fe0acSChen Yu 			if (num_iterations <= 0) {
5696023fe0acSChen Yu 				fprintf(outf, "iterations %d should be positive number\n",
5697023fe0acSChen Yu 					num_iterations);
5698023fe0acSChen Yu 				exit(2);
5699023fe0acSChen Yu 			}
5700023fe0acSChen Yu 			break;
5701812db3f7SLen Brown 		case 's':
57023f44a5c6SLen Brown 			/*
57033f44a5c6SLen Brown 			 * --show: show only those specified
57043f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
57053f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
57063f44a5c6SLen Brown 			 */
57073f44a5c6SLen Brown 			if (shown == 0)
57083f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
57093f44a5c6SLen Brown 			else
57103f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
57113f44a5c6SLen Brown 			shown = 1;
5712812db3f7SLen Brown 			break;
5713d8af6f5fSLen Brown 		case 'S':
5714d8af6f5fSLen Brown 			summary_only++;
5715d8af6f5fSLen Brown 			break;
5716d8af6f5fSLen Brown 		case 'T':
5717d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
5718d8af6f5fSLen Brown 			break;
5719d8af6f5fSLen Brown 		case 'v':
5720d8af6f5fSLen Brown 			print_version();
5721d8af6f5fSLen Brown 			exit(0);
5722d8af6f5fSLen Brown 			break;
5723103a8feaSLen Brown 		}
5724103a8feaSLen Brown 	}
5725103a8feaSLen Brown }
5726103a8feaSLen Brown 
5727103a8feaSLen Brown int main(int argc, char **argv)
5728103a8feaSLen Brown {
5729b7d8c148SLen Brown 	outf = stderr;
5730103a8feaSLen Brown 	cmdline(argc, argv);
5731103a8feaSLen Brown 
573296e47158SLen Brown 	if (!quiet)
5733d8af6f5fSLen Brown 		print_version();
5734103a8feaSLen Brown 
573541618e63SLen Brown 	probe_sysfs();
573641618e63SLen Brown 
5737103a8feaSLen Brown 	turbostat_init();
5738103a8feaSLen Brown 
57393b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
57403b4d5c7fSAndy Shevchenko 	if (dump_only)
57413b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
57423b4d5c7fSAndy Shevchenko 
5743c8ade361SLen Brown 	/* list header and exit */
5744c8ade361SLen Brown 	if (list_header_only) {
5745c8ade361SLen Brown 		print_header(",");
5746c8ade361SLen Brown 		flush_output_stdout();
5747c8ade361SLen Brown 		return 0;
5748c8ade361SLen Brown 	}
5749c8ade361SLen Brown 
5750103a8feaSLen Brown 	/*
5751103a8feaSLen Brown 	 * if any params left, it must be a command to fork
5752103a8feaSLen Brown 	 */
5753103a8feaSLen Brown 	if (argc - optind)
5754103a8feaSLen Brown 		return fork_it(argv + optind);
5755103a8feaSLen Brown 	else
5756103a8feaSLen Brown 		turbostat_loop();
5757103a8feaSLen Brown 
5758103a8feaSLen Brown 	return 0;
5759103a8feaSLen Brown }
5760