1103a8feaSLen Brown /*
2103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
3103a8feaSLen Brown  * on modern Intel turbo-capable processors.
4103a8feaSLen Brown  *
5144b44b1SLen Brown  * Copyright (c) 2013 Intel Corporation.
6103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
7103a8feaSLen Brown  *
8103a8feaSLen Brown  * This program is free software; you can redistribute it and/or modify it
9103a8feaSLen Brown  * under the terms and conditions of the GNU General Public License,
10103a8feaSLen Brown  * version 2, as published by the Free Software Foundation.
11103a8feaSLen Brown  *
12103a8feaSLen Brown  * This program is distributed in the hope it will be useful, but WITHOUT
13103a8feaSLen Brown  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14103a8feaSLen Brown  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15103a8feaSLen Brown  * more details.
16103a8feaSLen Brown  *
17103a8feaSLen Brown  * You should have received a copy of the GNU General Public License along with
18103a8feaSLen Brown  * this program; if not, write to the Free Software Foundation, Inc.,
19103a8feaSLen Brown  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20103a8feaSLen Brown  */
21103a8feaSLen Brown 
2288c3281fSLen Brown #define _GNU_SOURCE
23b731f311SJosh Triplett #include MSRHEADER
24869ce69eSLen Brown #include INTEL_FAMILY_HEADER
2595aebc44SJosh Triplett #include <stdarg.h>
26103a8feaSLen Brown #include <stdio.h>
27b2c95d90SJosh Triplett #include <err.h>
28103a8feaSLen Brown #include <unistd.h>
29103a8feaSLen Brown #include <sys/types.h>
30103a8feaSLen Brown #include <sys/wait.h>
31103a8feaSLen Brown #include <sys/stat.h>
32103a8feaSLen Brown #include <sys/resource.h>
33103a8feaSLen Brown #include <fcntl.h>
34103a8feaSLen Brown #include <signal.h>
35103a8feaSLen Brown #include <sys/time.h>
36103a8feaSLen Brown #include <stdlib.h>
37d8af6f5fSLen Brown #include <getopt.h>
38103a8feaSLen Brown #include <dirent.h>
39103a8feaSLen Brown #include <string.h>
40103a8feaSLen Brown #include <ctype.h>
4188c3281fSLen Brown #include <sched.h>
422a0609c0SLen Brown #include <time.h>
432b92865eSJosh Triplett #include <cpuid.h>
4498481e79SLen Brown #include <linux/capability.h>
4598481e79SLen Brown #include <errno.h>
46103a8feaSLen Brown 
47103a8feaSLen Brown char *proc_stat = "/proc/stat";
48b7d8c148SLen Brown FILE *outf;
4936229897SLen Brown int *fd_percpu;
502a0609c0SLen Brown struct timespec interval_ts = {5, 0};
51d8af6f5fSLen Brown unsigned int debug;
52d8af6f5fSLen Brown unsigned int rapl_joules;
53d8af6f5fSLen Brown unsigned int summary_only;
54d8af6f5fSLen Brown unsigned int dump_only;
55103a8feaSLen Brown unsigned int do_snb_cstates;
56fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
57ee7e38e3SLen Brown unsigned int do_pc2;
58ee7e38e3SLen Brown unsigned int do_pc3;
59ee7e38e3SLen Brown unsigned int do_pc6;
60ee7e38e3SLen Brown unsigned int do_pc7;
61ca58710fSKristen Carlson Accardi unsigned int do_c8_c9_c10;
620b2bb692SLen Brown unsigned int do_skl_residency;
63144b44b1SLen Brown unsigned int do_slm_cstates;
64144b44b1SLen Brown unsigned int use_c1_residency_msr;
65103a8feaSLen Brown unsigned int has_aperf;
66889facbeSLen Brown unsigned int has_epb;
675a63426eSLen Brown unsigned int do_irtl_snb;
685a63426eSLen Brown unsigned int do_irtl_hsw;
69fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
70103a8feaSLen Brown unsigned int genuine_intel;
71103a8feaSLen Brown unsigned int has_invariant_tsc;
72d7899447SLen Brown unsigned int do_nhm_platform_info;
73cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
74b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
75103a8feaSLen Brown double bclk;
76a2b7b749SLen Brown double base_hz;
7721ed5574SLen Brown unsigned int has_base_hz;
78a2b7b749SLen Brown double tsc_tweak = 1.0;
79c98d5d94SLen Brown unsigned int show_pkg_only;
80c98d5d94SLen Brown unsigned int show_core_only;
81c98d5d94SLen Brown char *output_buffer, *outp;
82889facbeSLen Brown unsigned int do_rapl;
83889facbeSLen Brown unsigned int do_dts;
84889facbeSLen Brown unsigned int do_ptm;
85fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
8627d47356SLen Brown unsigned int gfx_cur_mhz;
87889facbeSLen Brown unsigned int tcc_activation_temp;
88889facbeSLen Brown unsigned int tcc_activation_temp_override;
8940ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
9040ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
91889facbeSLen Brown double rapl_joule_counter_range;
923a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
933a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
943a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
958a5bdf41SLen Brown unsigned int crystal_hz;
968a5bdf41SLen Brown unsigned long long tsc_hz;
977ce7d5deSPrarit Bhargava int base_cpu;
9821ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
997f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
1007f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
1017f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
1027f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
1037f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1047f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
105889facbeSLen Brown 
106889facbeSLen Brown #define RAPL_PKG		(1 << 0)
107e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
108e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
109e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
110e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
111e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
112e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
113e6f9bb3cSLen Brown 
114889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
115e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
116e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
117e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
118e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1190b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1200b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
121e6f9bb3cSLen Brown 
1229148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
123e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1240b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
125e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
126e6f9bb3cSLen Brown 
1270b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
128e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
129e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
130e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1319148494cSJacob Pan 
1329148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1339148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1349148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
135889facbeSLen Brown #define	TJMAX_DEFAULT	100
136889facbeSLen Brown 
137889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
138103a8feaSLen Brown 
139388e9c81SLen Brown /*
140388e9c81SLen Brown  * buffer size used by sscanf() for added column names
141388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
142388e9c81SLen Brown  */
143388e9c81SLen Brown #define	NAME_BYTES 20
144388e9c81SLen Brown 
145103a8feaSLen Brown int backwards_count;
146103a8feaSLen Brown char *progname;
147103a8feaSLen Brown 
148c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
149c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
150678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16
151c98d5d94SLen Brown 
152c98d5d94SLen Brown struct thread_data {
153c98d5d94SLen Brown 	unsigned long long tsc;
154c98d5d94SLen Brown 	unsigned long long aperf;
155c98d5d94SLen Brown 	unsigned long long mperf;
156144b44b1SLen Brown 	unsigned long long c1;
157562a2d37SLen Brown 	unsigned int irq_count;
1581ed51011SLen Brown 	unsigned int smi_count;
159c98d5d94SLen Brown 	unsigned int cpu_id;
160c98d5d94SLen Brown 	unsigned int flags;
161c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
162c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
163678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
164c98d5d94SLen Brown } *thread_even, *thread_odd;
165c98d5d94SLen Brown 
166c98d5d94SLen Brown struct core_data {
167c98d5d94SLen Brown 	unsigned long long c3;
168c98d5d94SLen Brown 	unsigned long long c6;
169c98d5d94SLen Brown 	unsigned long long c7;
170889facbeSLen Brown 	unsigned int core_temp_c;
171c98d5d94SLen Brown 	unsigned int core_id;
172678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
173c98d5d94SLen Brown } *core_even, *core_odd;
174c98d5d94SLen Brown 
175c98d5d94SLen Brown struct pkg_data {
176c98d5d94SLen Brown 	unsigned long long pc2;
177c98d5d94SLen Brown 	unsigned long long pc3;
178c98d5d94SLen Brown 	unsigned long long pc6;
179c98d5d94SLen Brown 	unsigned long long pc7;
180ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
181ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
182ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1830b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1840b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1850b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1860b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1879185e988SLen Brown 	long long gfx_rc6_ms;
18827d47356SLen Brown 	unsigned int gfx_mhz;
189c98d5d94SLen Brown 	unsigned int package_id;
190889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
191889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
192889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
193889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
194889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
195889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
196889facbeSLen Brown 	unsigned int pkg_temp_c;
197678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
198c98d5d94SLen Brown } *package_even, *package_odd;
199c98d5d94SLen Brown 
200c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
201c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
202c98d5d94SLen Brown 
203c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
204c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
205c98d5d94SLen Brown 		topo.num_threads_per_core + \
206c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
207c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
208c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
209c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
210c98d5d94SLen Brown 
211388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
212388e9c81SLen Brown enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
213388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
214388e9c81SLen Brown 
215388e9c81SLen Brown struct msr_counter {
216388e9c81SLen Brown 	unsigned int msr_num;
217388e9c81SLen Brown 	char name[NAME_BYTES];
218388e9c81SLen Brown 	unsigned int width;
219388e9c81SLen Brown 	enum counter_type type;
220388e9c81SLen Brown 	enum counter_format format;
221388e9c81SLen Brown 	struct msr_counter *next;
222812db3f7SLen Brown 	unsigned int flags;
223812db3f7SLen Brown #define	FLAGS_HIDE	(1 << 0)
224812db3f7SLen Brown #define	FLAGS_SHOW	(1 << 1)
225388e9c81SLen Brown };
226388e9c81SLen Brown 
227388e9c81SLen Brown struct sys_counters {
228678a3bd1SLen Brown 	unsigned int added_thread_counters;
229678a3bd1SLen Brown 	unsigned int added_core_counters;
230678a3bd1SLen Brown 	unsigned int added_package_counters;
231388e9c81SLen Brown 	struct msr_counter *tp;
232388e9c81SLen Brown 	struct msr_counter *cp;
233388e9c81SLen Brown 	struct msr_counter *pp;
234388e9c81SLen Brown } sys;
235388e9c81SLen Brown 
236c98d5d94SLen Brown struct system_summary {
237c98d5d94SLen Brown 	struct thread_data threads;
238c98d5d94SLen Brown 	struct core_data cores;
239c98d5d94SLen Brown 	struct pkg_data packages;
240388e9c81SLen Brown } average;
241c98d5d94SLen Brown 
242c98d5d94SLen Brown 
243c98d5d94SLen Brown struct topo_params {
244c98d5d94SLen Brown 	int num_packages;
245103a8feaSLen Brown 	int num_cpus;
246c98d5d94SLen Brown 	int num_cores;
247c98d5d94SLen Brown 	int max_cpu_num;
248c98d5d94SLen Brown 	int num_cores_per_pkg;
249c98d5d94SLen Brown 	int num_threads_per_core;
250c98d5d94SLen Brown } topo;
251103a8feaSLen Brown 
252c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
253103a8feaSLen Brown 
254562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
255562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
256562a2d37SLen Brown 
257c98d5d94SLen Brown void setup_all_buffers(void);
258103a8feaSLen Brown 
259c98d5d94SLen Brown int cpu_is_not_present(int cpu)
260d15cf7c1SLen Brown {
261c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
262c98d5d94SLen Brown }
263c98d5d94SLen Brown /*
264c98d5d94SLen Brown  * run func(thread, core, package) in topology order
265c98d5d94SLen Brown  * skip non-present cpus
266c98d5d94SLen Brown  */
267c98d5d94SLen Brown 
268c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
269c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
270c98d5d94SLen Brown {
271c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
272c98d5d94SLen Brown 
273c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
274c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
275c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
276c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
277c98d5d94SLen Brown 				struct thread_data *t;
278c98d5d94SLen Brown 				struct core_data *c;
279c98d5d94SLen Brown 				struct pkg_data *p;
280c98d5d94SLen Brown 
281c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
282c98d5d94SLen Brown 
283c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
284c98d5d94SLen Brown 					continue;
285c98d5d94SLen Brown 
286c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
287c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
288c98d5d94SLen Brown 
289c98d5d94SLen Brown 				retval = func(t, c, p);
290c98d5d94SLen Brown 				if (retval)
291c98d5d94SLen Brown 					return retval;
292c98d5d94SLen Brown 			}
293c98d5d94SLen Brown 		}
294c98d5d94SLen Brown 	}
295d15cf7c1SLen Brown 	return 0;
296d15cf7c1SLen Brown }
297d15cf7c1SLen Brown 
29888c3281fSLen Brown int cpu_migrate(int cpu)
29988c3281fSLen Brown {
300c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
301c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
302c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
30388c3281fSLen Brown 		return -1;
30488c3281fSLen Brown 	else
30588c3281fSLen Brown 		return 0;
30688c3281fSLen Brown }
30736229897SLen Brown int get_msr_fd(int cpu)
308103a8feaSLen Brown {
309103a8feaSLen Brown 	char pathname[32];
310103a8feaSLen Brown 	int fd;
311103a8feaSLen Brown 
31236229897SLen Brown 	fd = fd_percpu[cpu];
31336229897SLen Brown 
31436229897SLen Brown 	if (fd)
31536229897SLen Brown 		return fd;
31636229897SLen Brown 
317103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
318103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
31915aaa346SLen Brown 	if (fd < 0)
32098481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
321103a8feaSLen Brown 
32236229897SLen Brown 	fd_percpu[cpu] = fd;
32336229897SLen Brown 
32436229897SLen Brown 	return fd;
32536229897SLen Brown }
32636229897SLen Brown 
32736229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
32836229897SLen Brown {
32936229897SLen Brown 	ssize_t retval;
33036229897SLen Brown 
33136229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
33215aaa346SLen Brown 
33398481e79SLen Brown 	if (retval != sizeof *msr)
334cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
33515aaa346SLen Brown 
33615aaa346SLen Brown 	return 0;
337103a8feaSLen Brown }
338103a8feaSLen Brown 
339fc04cc67SLen Brown /*
340812db3f7SLen Brown  * Each string in this array is compared in --show and --hide cmdline.
341812db3f7SLen Brown  * Thus, strings that are proper sub-sets must follow their more specific peers.
342fc04cc67SLen Brown  */
343812db3f7SLen Brown struct msr_counter bic[] = {
344812db3f7SLen Brown 	{ 0x0, "Package" },
345812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
346812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
347812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
348812db3f7SLen Brown 	{ 0x0, "IRQ" },
349812db3f7SLen Brown 	{ 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
350812db3f7SLen Brown 	{ 0x0, "Busy%" },
351812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
352812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
353812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
354812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
355812db3f7SLen Brown 	{ 0x0, "ThreadC" },
356812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
357812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
358812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
359812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
360812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
361812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
362812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
363812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
364812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
365812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
366812db3f7SLen Brown 	{ 0x0, "CorWatt" },
367812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
368812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
369812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
370812db3f7SLen Brown 	{ 0x0, "PKG_%" },
371812db3f7SLen Brown 	{ 0x0, "RAM_%" },
372812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
373812db3f7SLen Brown 	{ 0x0, "Cor_J" },
374812db3f7SLen Brown 	{ 0x0, "GFX_J" },
375812db3f7SLen Brown 	{ 0x0, "RAM_J" },
376812db3f7SLen Brown 	{ 0x0, "Core" },
377812db3f7SLen Brown 	{ 0x0, "CPU" },
378812db3f7SLen Brown };
379812db3f7SLen Brown 
380812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
381812db3f7SLen Brown #define	BIC_Package	(1ULL << 0)
382812db3f7SLen Brown #define	BIC_Avg_MHz	(1ULL << 1)
383812db3f7SLen Brown #define	BIC_Bzy_MHz	(1ULL << 2)
384812db3f7SLen Brown #define	BIC_TSC_MHz	(1ULL << 3)
385812db3f7SLen Brown #define	BIC_IRQ		(1ULL << 4)
386812db3f7SLen Brown #define	BIC_SMI		(1ULL << 5)
387812db3f7SLen Brown #define	BIC_Busy	(1ULL << 6)
388812db3f7SLen Brown #define	BIC_CPU_c1	(1ULL << 7)
389812db3f7SLen Brown #define	BIC_CPU_c3	(1ULL << 8)
390812db3f7SLen Brown #define	BIC_CPU_c6	(1ULL << 9)
391812db3f7SLen Brown #define	BIC_CPU_c7	(1ULL << 10)
392812db3f7SLen Brown #define	BIC_ThreadC	(1ULL << 11)
393812db3f7SLen Brown #define	BIC_CoreTmp	(1ULL << 12)
394812db3f7SLen Brown #define	BIC_CoreCnt	(1ULL << 13)
395812db3f7SLen Brown #define	BIC_PkgTmp	(1ULL << 14)
396812db3f7SLen Brown #define	BIC_GFX_rc6	(1ULL << 15)
397812db3f7SLen Brown #define	BIC_GFXMHz	(1ULL << 16)
398812db3f7SLen Brown #define	BIC_Pkgpc2	(1ULL << 17)
399812db3f7SLen Brown #define	BIC_Pkgpc3	(1ULL << 18)
400812db3f7SLen Brown #define	BIC_Pkgpc6	(1ULL << 19)
401812db3f7SLen Brown #define	BIC_Pkgpc7	(1ULL << 20)
402812db3f7SLen Brown #define	BIC_PkgWatt	(1ULL << 21)
403812db3f7SLen Brown #define	BIC_CorWatt	(1ULL << 22)
404812db3f7SLen Brown #define	BIC_GFXWatt	(1ULL << 23)
405812db3f7SLen Brown #define	BIC_PkgCnt	(1ULL << 24)
406812db3f7SLen Brown #define	BIC_RAMWatt	(1ULL << 27)
407812db3f7SLen Brown #define	BIC_PKG__	(1ULL << 28)
408812db3f7SLen Brown #define	BIC_RAM__	(1ULL << 29)
409812db3f7SLen Brown #define	BIC_Pkg_J	(1ULL << 30)
410812db3f7SLen Brown #define	BIC_Cor_J	(1ULL << 31)
411812db3f7SLen Brown #define	BIC_GFX_J	(1ULL << 30)
412812db3f7SLen Brown #define	BIC_RAM_J	(1ULL << 31)
413812db3f7SLen Brown #define	BIC_Core	(1ULL << 32)
414812db3f7SLen Brown #define	BIC_CPU		(1ULL << 33)
415812db3f7SLen Brown 
416812db3f7SLen Brown unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
417812db3f7SLen Brown unsigned long long bic_present;
418812db3f7SLen Brown 
419812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
420812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
421812db3f7SLen Brown 
422812db3f7SLen Brown /*
423812db3f7SLen Brown  * bic_lookup
424812db3f7SLen Brown  * for all the strings in comma separate name_list,
425812db3f7SLen Brown  * set the approprate bit in return value.
426812db3f7SLen Brown  */
427812db3f7SLen Brown unsigned long long bic_lookup(char *name_list)
428812db3f7SLen Brown {
429812db3f7SLen Brown 	int i;
430812db3f7SLen Brown 	unsigned long long retval = 0;
431812db3f7SLen Brown 
432812db3f7SLen Brown 	while (name_list) {
433812db3f7SLen Brown 		char *comma;
434812db3f7SLen Brown 
435812db3f7SLen Brown 		comma = strchr(name_list, ',');
436812db3f7SLen Brown 
437812db3f7SLen Brown 		if (comma)
438812db3f7SLen Brown 			*comma = '\0';
439812db3f7SLen Brown 
440812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
441812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
442812db3f7SLen Brown 				retval |= (1ULL << i);
443812db3f7SLen Brown 				break;
444812db3f7SLen Brown 			}
445812db3f7SLen Brown 		}
446812db3f7SLen Brown 		if (i == MAX_BIC) {
447812db3f7SLen Brown 			fprintf(stderr, "Invalid counter name: %s\n", name_list);
448812db3f7SLen Brown 			exit(-1);
449812db3f7SLen Brown 		}
450812db3f7SLen Brown 
451812db3f7SLen Brown 		name_list = comma;
452812db3f7SLen Brown 		if (name_list)
453812db3f7SLen Brown 			name_list++;
454812db3f7SLen Brown 
455812db3f7SLen Brown 	}
456812db3f7SLen Brown 	return retval;
457812db3f7SLen Brown }
458fc04cc67SLen Brown 
459a829eb4dSLen Brown void print_header(void)
460103a8feaSLen Brown {
461388e9c81SLen Brown 	struct msr_counter *mp;
462388e9c81SLen Brown 
463812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
4643d109de2SLen Brown 		outp += sprintf(outp, "\tPackage");
465812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
4663d109de2SLen Brown 		outp += sprintf(outp, "\tCore");
467812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
4683d109de2SLen Brown 		outp += sprintf(outp, "\tCPU");
469812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
4703d109de2SLen Brown 		outp += sprintf(outp, "\tAvg_MHz");
471812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
4723d109de2SLen Brown 		outp += sprintf(outp, "\tBusy%%");
473812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
4743d109de2SLen Brown 		outp += sprintf(outp, "\tBzy_MHz");
475812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
4763d109de2SLen Brown 		outp += sprintf(outp, "\tTSC_MHz");
4771cc21f7bSLen Brown 
4781cc21f7bSLen Brown 	if (!debug)
4791cc21f7bSLen Brown 		goto done;
4801cc21f7bSLen Brown 
481812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
4823d109de2SLen Brown 		outp += sprintf(outp, "\tIRQ");
483812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
4843d109de2SLen Brown 		outp += sprintf(outp, "\tSMI");
4851cc21f7bSLen Brown 
486812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c1))
4873d109de2SLen Brown 		outp += sprintf(outp, "\tCPU%%c1");
488889facbeSLen Brown 
489388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
490388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
491388e9c81SLen Brown 			if (mp->width == 64)
492388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
493388e9c81SLen Brown 			else
494388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
495388e9c81SLen Brown 		} else {
496388e9c81SLen Brown 			outp += sprintf(outp, "\t%-7.7s", mp->name);
497388e9c81SLen Brown 		}
498388e9c81SLen Brown 	}
499388e9c81SLen Brown 
500812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
501678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c3");
502812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
503678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c6");
504812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
505678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c7");
506678a3bd1SLen Brown 
507678a3bd1SLen Brown 
508812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
5093d109de2SLen Brown 		outp += sprintf(outp, "\tCoreTmp");
510388e9c81SLen Brown 
511388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
512388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
513388e9c81SLen Brown 			if (mp->width == 64)
514388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
515388e9c81SLen Brown 			else
516388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
517388e9c81SLen Brown 		} else {
518388e9c81SLen Brown 			outp += sprintf(outp, "\t%-7.7s", mp->name);
519388e9c81SLen Brown 		}
520388e9c81SLen Brown 	}
521388e9c81SLen Brown 
522812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
5233d109de2SLen Brown 		outp += sprintf(outp, "\tPkgTmp");
524889facbeSLen Brown 
525812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
5263d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%rc6");
527fdf676e5SLen Brown 
528812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
5293d109de2SLen Brown 		outp += sprintf(outp, "\tGFXMHz");
53027d47356SLen Brown 
5310b2bb692SLen Brown 	if (do_skl_residency) {
5323d109de2SLen Brown 		outp += sprintf(outp, "\tTotl%%C0");
5333d109de2SLen Brown 		outp += sprintf(outp, "\tAny%%C0");
5343d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%C0");
5353d109de2SLen Brown 		outp += sprintf(outp, "\tCPUGFX%%");
5360b2bb692SLen Brown 	}
5370b2bb692SLen Brown 
538ee7e38e3SLen Brown 	if (do_pc2)
5393d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc2");
540ee7e38e3SLen Brown 	if (do_pc3)
5413d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc3");
542ee7e38e3SLen Brown 	if (do_pc6)
5433d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc6");
544ee7e38e3SLen Brown 	if (do_pc7)
5453d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc7");
546ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
5473d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc8");
5483d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc9");
5493d109de2SLen Brown 		outp += sprintf(outp, "\tPk%%pc10");
550ca58710fSKristen Carlson Accardi 	}
551103a8feaSLen Brown 
5525c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
553812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
5543d109de2SLen Brown 			outp += sprintf(outp, "\tPkgWatt");
555812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
5563d109de2SLen Brown 			outp += sprintf(outp, "\tCorWatt");
557812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
5583d109de2SLen Brown 			outp += sprintf(outp, "\tGFXWatt");
559812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
5603d109de2SLen Brown 			outp += sprintf(outp, "\tRAMWatt");
561812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
5623d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
563812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
5643d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
565d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
566812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
5673d109de2SLen Brown 			outp += sprintf(outp, "\tPkg_J");
568812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
5693d109de2SLen Brown 			outp += sprintf(outp, "\tCor_J");
570812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
5713d109de2SLen Brown 			outp += sprintf(outp, "\tGFX_J");
572812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
5733d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_J");
574812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
5753d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
576812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
5773d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
5785c56be9aSDirk Brandewie 	}
579388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
580388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
581388e9c81SLen Brown 			if (mp->width == 64)
582388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
583388e9c81SLen Brown 			else
584388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
585388e9c81SLen Brown 		} else {
586388e9c81SLen Brown 			outp += sprintf(outp, "\t%-7.7s", mp->name);
587388e9c81SLen Brown 		}
588388e9c81SLen Brown 	}
589388e9c81SLen Brown 
5901cc21f7bSLen Brown done:
591c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
592103a8feaSLen Brown }
593103a8feaSLen Brown 
594c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
595c98d5d94SLen Brown 	struct pkg_data *p)
596103a8feaSLen Brown {
597388e9c81SLen Brown 	int i;
598388e9c81SLen Brown 	struct msr_counter *mp;
599388e9c81SLen Brown 
6003b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
601c98d5d94SLen Brown 
602c98d5d94SLen Brown 	if (t) {
6033b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
6043b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
6053b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
6063b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
6073b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
6083b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
6096886fee4SLen Brown 
610812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
611562a2d37SLen Brown 			outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
612812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
6133b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
614388e9c81SLen Brown 
615388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
616388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
617388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
618388e9c81SLen Brown 		}
619103a8feaSLen Brown 	}
620103a8feaSLen Brown 
621c98d5d94SLen Brown 	if (c) {
6223b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
6233b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
6243b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
6253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
6263b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
627388e9c81SLen Brown 
628388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
629388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
630388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
631388e9c81SLen Brown 		}
632c98d5d94SLen Brown 	}
633103a8feaSLen Brown 
634c98d5d94SLen Brown 	if (p) {
6353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
6360b2bb692SLen Brown 
6370b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
6380b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
6390b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
6400b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
6410b2bb692SLen Brown 
6423b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
643ee7e38e3SLen Brown 		if (do_pc3)
6443b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
645ee7e38e3SLen Brown 		if (do_pc6)
6463b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
647ee7e38e3SLen Brown 		if (do_pc7)
6483b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
6493b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
6503b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
6513b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
6523b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
6533b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
6543b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
6553b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
6563b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
6573b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
6583b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
6593b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
6603b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
661388e9c81SLen Brown 
662388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
663388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
664388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
665388e9c81SLen Brown 		}
666c98d5d94SLen Brown 	}
6673b4d5c7fSAndy Shevchenko 
6683b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
6693b4d5c7fSAndy Shevchenko 
670c98d5d94SLen Brown 	return 0;
671103a8feaSLen Brown }
672103a8feaSLen Brown 
673e23da037SLen Brown /*
674e23da037SLen Brown  * column formatting convention & formats
675e23da037SLen Brown  */
676c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
677c98d5d94SLen Brown 	struct pkg_data *p)
678103a8feaSLen Brown {
679103a8feaSLen Brown 	double interval_float;
680fc04cc67SLen Brown 	char *fmt8;
681388e9c81SLen Brown 	int i;
682388e9c81SLen Brown 	struct msr_counter *mp;
683103a8feaSLen Brown 
684c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
685c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
686c98d5d94SLen Brown 		return 0;
687c98d5d94SLen Brown 
688c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
689c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
690c98d5d94SLen Brown 		return 0;
691c98d5d94SLen Brown 
692103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
693103a8feaSLen Brown 
694c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
695c98d5d94SLen Brown 	if (t == &average.threads) {
696812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
6973d109de2SLen Brown 			outp += sprintf(outp, "\t-");
698812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
6993d109de2SLen Brown 			outp += sprintf(outp, "\t-");
700812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
7013d109de2SLen Brown 			outp += sprintf(outp, "\t-");
702103a8feaSLen Brown 	} else {
703812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
704c98d5d94SLen Brown 			if (p)
7053d109de2SLen Brown 				outp += sprintf(outp, "\t%d", p->package_id);
706c98d5d94SLen Brown 			else
7073d109de2SLen Brown 				outp += sprintf(outp, "\t-");
708c98d5d94SLen Brown 		}
709812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
710c98d5d94SLen Brown 			if (c)
7113d109de2SLen Brown 				outp += sprintf(outp, "\t%d", c->core_id);
712c98d5d94SLen Brown 			else
7133d109de2SLen Brown 				outp += sprintf(outp, "\t-");
714c98d5d94SLen Brown 		}
715812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
7163d109de2SLen Brown 			outp += sprintf(outp, "\t%d", t->cpu_id);
717103a8feaSLen Brown 	}
718fc04cc67SLen Brown 
719812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
7203d109de2SLen Brown 		outp += sprintf(outp, "\t%.0f",
721fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
722fc04cc67SLen Brown 
723812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
7243d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
725103a8feaSLen Brown 
726812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
72721ed5574SLen Brown 		if (has_base_hz)
7283d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
72921ed5574SLen Brown 		else
7303d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f",
73121ed5574SLen Brown 				1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
73221ed5574SLen Brown 	}
733103a8feaSLen Brown 
734812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
7353d109de2SLen Brown 		outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
736103a8feaSLen Brown 
7371cc21f7bSLen Brown 	if (!debug)
7381cc21f7bSLen Brown 		goto done;
7391cc21f7bSLen Brown 
740562a2d37SLen Brown 	/* IRQ */
741812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
7423d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->irq_count);
743562a2d37SLen Brown 
7441cc21f7bSLen Brown 	/* SMI */
745812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
7463d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->smi_count);
7471cc21f7bSLen Brown 
748678a3bd1SLen Brown 	/* C1 */
749812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c1))
7503d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
751c98d5d94SLen Brown 
752678a3bd1SLen Brown 	/* Added counters */
753678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
754678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
755678a3bd1SLen Brown 			if (mp->width == 32)
756678a3bd1SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
757678a3bd1SLen Brown 			else
758678a3bd1SLen Brown 				outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
759678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
760678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", t->counter[i]);
761678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
762678a3bd1SLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
763678a3bd1SLen Brown 		}
764678a3bd1SLen Brown 	}
765678a3bd1SLen Brown 
766c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
767c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
768c98d5d94SLen Brown 		goto done;
769c98d5d94SLen Brown 
770812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
7713d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
772812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
7733d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
774812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
7753d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
776c98d5d94SLen Brown 
777812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
7783d109de2SLen Brown 		outp += sprintf(outp, "\t%d", c->core_temp_c);
779889facbeSLen Brown 
780388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
781388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
782388e9c81SLen Brown 			if (mp->width == 32)
783388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
784388e9c81SLen Brown 			else
785388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
786388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
787678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", c->counter[i]);
788388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
789388e9c81SLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
790388e9c81SLen Brown 		}
791388e9c81SLen Brown 	}
792388e9c81SLen Brown 
793c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
794c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
795c98d5d94SLen Brown 		goto done;
796c98d5d94SLen Brown 
7970b2bb692SLen Brown 	/* PkgTmp */
798812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
7993d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->pkg_temp_c);
800889facbeSLen Brown 
801fdf676e5SLen Brown 	/* GFXrc6 */
802812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
803ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
8043d109de2SLen Brown 			outp += sprintf(outp, "\t**.**");
8059185e988SLen Brown 		} else {
8063d109de2SLen Brown 			outp += sprintf(outp, "\t%.2f",
8079185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
8089185e988SLen Brown 		}
8099185e988SLen Brown 	}
810fdf676e5SLen Brown 
81127d47356SLen Brown 	/* GFXMHz */
812812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
8133d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->gfx_mhz);
81427d47356SLen Brown 
8150b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
8160b2bb692SLen Brown 	if (do_skl_residency) {
8173d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
8183d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
8193d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
8203d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
8210b2bb692SLen Brown 	}
8220b2bb692SLen Brown 
823ee7e38e3SLen Brown 	if (do_pc2)
8243d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
825ee7e38e3SLen Brown 	if (do_pc3)
8263d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
827ee7e38e3SLen Brown 	if (do_pc6)
8283d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
829ee7e38e3SLen Brown 	if (do_pc7)
8303d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
831ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
8323d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
8333d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
8343d109de2SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
835ca58710fSKristen Carlson Accardi 	}
836889facbeSLen Brown 
837889facbeSLen Brown 	/*
838889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
839889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
840889facbeSLen Brown  	 */
841fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
8423d109de2SLen Brown 		fmt8 = "\t%.2f";
843fc04cc67SLen Brown 	else
844fc04cc67SLen Brown 		fmt8 = "%6.0f**";
845889facbeSLen Brown 
846812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
847fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
848812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
849fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
850812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
851fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
852812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
85340ee8e3bSAndrey Semin 		outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
854812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
855812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
856812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
857812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
858812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
859812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
860812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
861812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
862812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
863fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
864812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
865fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
866812db3f7SLen Brown 
867388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
868388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
869388e9c81SLen Brown 			if (mp->width == 32)
870388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
871388e9c81SLen Brown 			else
872388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
873388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
874678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", p->counter[i]);
875388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
876388e9c81SLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
877388e9c81SLen Brown 		}
878388e9c81SLen Brown 	}
879388e9c81SLen Brown 
880c98d5d94SLen Brown done:
881c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
882c98d5d94SLen Brown 
883c98d5d94SLen Brown 	return 0;
884103a8feaSLen Brown }
885103a8feaSLen Brown 
886b7d8c148SLen Brown void flush_output_stdout(void)
887103a8feaSLen Brown {
888b7d8c148SLen Brown 	FILE *filep;
889b7d8c148SLen Brown 
890b7d8c148SLen Brown 	if (outf == stderr)
891b7d8c148SLen Brown 		filep = stdout;
892b7d8c148SLen Brown 	else
893b7d8c148SLen Brown 		filep = outf;
894b7d8c148SLen Brown 
895b7d8c148SLen Brown 	fputs(output_buffer, filep);
896b7d8c148SLen Brown 	fflush(filep);
897b7d8c148SLen Brown 
898c98d5d94SLen Brown 	outp = output_buffer;
899c98d5d94SLen Brown }
900b7d8c148SLen Brown void flush_output_stderr(void)
901c98d5d94SLen Brown {
902b7d8c148SLen Brown 	fputs(output_buffer, outf);
903b7d8c148SLen Brown 	fflush(outf);
904c98d5d94SLen Brown 	outp = output_buffer;
905c98d5d94SLen Brown }
906c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
907c98d5d94SLen Brown {
908e23da037SLen Brown 	static int printed;
909103a8feaSLen Brown 
910e23da037SLen Brown 	if (!printed || !summary_only)
911103a8feaSLen Brown 		print_header();
912103a8feaSLen Brown 
913c98d5d94SLen Brown 	if (topo.num_cpus > 1)
914c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
915c98d5d94SLen Brown 			&average.packages);
916103a8feaSLen Brown 
917e23da037SLen Brown 	printed = 1;
918e23da037SLen Brown 
919e23da037SLen Brown 	if (summary_only)
920e23da037SLen Brown 		return;
921e23da037SLen Brown 
922c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
923103a8feaSLen Brown }
924103a8feaSLen Brown 
925889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
926889facbeSLen Brown 	if (new > old) {			\
927889facbeSLen Brown 		old = new - old;		\
928889facbeSLen Brown 	} else {				\
929889facbeSLen Brown 		old = 0x100000000 + new - old;	\
930889facbeSLen Brown 	}
931889facbeSLen Brown 
932ba3dec99SLen Brown int
933c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
934103a8feaSLen Brown {
935388e9c81SLen Brown 	int i;
936388e9c81SLen Brown 	struct msr_counter *mp;
9370b2bb692SLen Brown 
9380b2bb692SLen Brown 	if (do_skl_residency) {
9390b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
9400b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
9410b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
9420b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
9430b2bb692SLen Brown 	}
944c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
945ee7e38e3SLen Brown 	if (do_pc3)
946c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
947ee7e38e3SLen Brown 	if (do_pc6)
948c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
949ee7e38e3SLen Brown 	if (do_pc7)
950c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
951ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
952ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
953ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
954889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
955889facbeSLen Brown 
9569185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
9579185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
9589185e988SLen Brown 		old->gfx_rc6_ms = -1;
9599185e988SLen Brown 	else
960fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
9619185e988SLen Brown 
96227d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
96327d47356SLen Brown 
964889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
965889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
966889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
967889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
968889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
969889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
970ba3dec99SLen Brown 
971388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
972388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
973388e9c81SLen Brown 			old->counter[i] = new->counter[i];
974388e9c81SLen Brown 		else
975388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
976388e9c81SLen Brown 	}
977388e9c81SLen Brown 
978ba3dec99SLen Brown 	return 0;
979103a8feaSLen Brown }
980103a8feaSLen Brown 
981c98d5d94SLen Brown void
982c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
983c98d5d94SLen Brown {
984388e9c81SLen Brown 	int i;
985388e9c81SLen Brown 	struct msr_counter *mp;
986388e9c81SLen Brown 
987c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
988c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
989c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
990889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
991388e9c81SLen Brown 
992388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
993388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
994388e9c81SLen Brown 			old->counter[i] = new->counter[i];
995388e9c81SLen Brown 		else
996388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
997388e9c81SLen Brown 	}
998103a8feaSLen Brown }
999c98d5d94SLen Brown 
1000c3ae331dSLen Brown /*
1001c3ae331dSLen Brown  * old = new - old
1002c3ae331dSLen Brown  */
1003ba3dec99SLen Brown int
1004c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1005c98d5d94SLen Brown 	struct core_data *core_delta)
1006c98d5d94SLen Brown {
1007388e9c81SLen Brown 	int i;
1008388e9c81SLen Brown 	struct msr_counter *mp;
1009388e9c81SLen Brown 
1010c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1011c98d5d94SLen Brown 
1012103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1013b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1014b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1015b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1016b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1017103a8feaSLen Brown 
1018c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1019c98d5d94SLen Brown 
1020812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1021c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1022c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1023c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1024c98d5d94SLen Brown 		} else {
1025ba3dec99SLen Brown 			return -1;
1026103a8feaSLen Brown 		}
1027a729617cSLen Brown 	}
1028103a8feaSLen Brown 
1029c98d5d94SLen Brown 
1030144b44b1SLen Brown 	if (use_c1_residency_msr) {
1031144b44b1SLen Brown 		/*
1032144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1033144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1034144b44b1SLen Brown 		 */
1035144b44b1SLen Brown 	} else {
1036103a8feaSLen Brown 		/*
1037c3ae331dSLen Brown 		 * As counter collection is not atomic,
1038c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1039103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1040103a8feaSLen Brown 		 */
1041c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1042c98d5d94SLen Brown 			old->c1 = 0;
1043c98d5d94SLen Brown 		else {
1044c98d5d94SLen Brown 			/* normal case, derive c1 */
1045c98d5d94SLen Brown 			old->c1 = old->tsc - old->mperf - core_delta->c3
1046c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1047c98d5d94SLen Brown 		}
1048144b44b1SLen Brown 	}
1049c3ae331dSLen Brown 
1050c98d5d94SLen Brown 	if (old->mperf == 0) {
1051b7d8c148SLen Brown 		if (debug > 1)
1052b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1053c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1054c98d5d94SLen Brown 	}
1055103a8feaSLen Brown 
1056812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1057562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1058562a2d37SLen Brown 
1059812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
10601ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1061ba3dec99SLen Brown 
1062388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1063388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1064388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1065388e9c81SLen Brown 		else
1066388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1067388e9c81SLen Brown 	}
1068ba3dec99SLen Brown 	return 0;
1069103a8feaSLen Brown }
1070c98d5d94SLen Brown 
1071c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1072c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1073c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1074c98d5d94SLen Brown {
1075ba3dec99SLen Brown 	int retval = 0;
1076ba3dec99SLen Brown 
1077c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1078c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1079c98d5d94SLen Brown 		delta_core(c, c2);
1080c98d5d94SLen Brown 
1081c98d5d94SLen Brown 	/* always calculate thread delta */
1082ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1083ba3dec99SLen Brown 	if (retval)
1084ba3dec99SLen Brown 		return retval;
1085c98d5d94SLen Brown 
1086c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1087c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1088ba3dec99SLen Brown 		retval = delta_package(p, p2);
1089c98d5d94SLen Brown 
1090ba3dec99SLen Brown 	return retval;
1091103a8feaSLen Brown }
1092103a8feaSLen Brown 
1093c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1094103a8feaSLen Brown {
1095388e9c81SLen Brown 	int i;
1096388e9c81SLen Brown 	struct msr_counter  *mp;
1097388e9c81SLen Brown 
1098c98d5d94SLen Brown 	t->tsc = 0;
1099c98d5d94SLen Brown 	t->aperf = 0;
1100c98d5d94SLen Brown 	t->mperf = 0;
1101c98d5d94SLen Brown 	t->c1 = 0;
1102103a8feaSLen Brown 
1103562a2d37SLen Brown 	t->irq_count = 0;
1104562a2d37SLen Brown 	t->smi_count = 0;
1105562a2d37SLen Brown 
1106c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1107c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1108c98d5d94SLen Brown 
1109c98d5d94SLen Brown 	c->c3 = 0;
1110c98d5d94SLen Brown 	c->c6 = 0;
1111c98d5d94SLen Brown 	c->c7 = 0;
1112889facbeSLen Brown 	c->core_temp_c = 0;
1113c98d5d94SLen Brown 
11140b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
11150b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
11160b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
11170b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
11180b2bb692SLen Brown 
1119c98d5d94SLen Brown 	p->pc2 = 0;
1120ee7e38e3SLen Brown 	if (do_pc3)
1121c98d5d94SLen Brown 		p->pc3 = 0;
1122ee7e38e3SLen Brown 	if (do_pc6)
1123c98d5d94SLen Brown 		p->pc6 = 0;
1124ee7e38e3SLen Brown 	if (do_pc7)
1125c98d5d94SLen Brown 		p->pc7 = 0;
1126ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1127ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1128ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1129889facbeSLen Brown 
1130889facbeSLen Brown 	p->energy_pkg = 0;
1131889facbeSLen Brown 	p->energy_dram = 0;
1132889facbeSLen Brown 	p->energy_cores = 0;
1133889facbeSLen Brown 	p->energy_gfx = 0;
1134889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1135889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1136889facbeSLen Brown 	p->pkg_temp_c = 0;
113727d47356SLen Brown 
1138fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
113927d47356SLen Brown 	p->gfx_mhz = 0;
1140388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1141388e9c81SLen Brown 		t->counter[i] = 0;
1142388e9c81SLen Brown 
1143388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1144388e9c81SLen Brown 		c->counter[i] = 0;
1145388e9c81SLen Brown 
1146388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1147388e9c81SLen Brown 		p->counter[i] = 0;
1148103a8feaSLen Brown }
1149c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1150c98d5d94SLen Brown 	struct pkg_data *p)
1151103a8feaSLen Brown {
1152388e9c81SLen Brown 	int i;
1153388e9c81SLen Brown 	struct msr_counter *mp;
1154388e9c81SLen Brown 
1155c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1156c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1157c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1158c98d5d94SLen Brown 	average.threads.c1 += t->c1;
115915aaa346SLen Brown 
1160562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1161562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1162562a2d37SLen Brown 
1163388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1164388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1165388e9c81SLen Brown 			continue;
1166388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1167388e9c81SLen Brown 	}
1168388e9c81SLen Brown 
1169c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1170c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1171c98d5d94SLen Brown 		return 0;
1172c98d5d94SLen Brown 
1173c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1174c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1175c98d5d94SLen Brown 	average.cores.c7 += c->c7;
1176c98d5d94SLen Brown 
1177889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1178889facbeSLen Brown 
1179388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1180388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1181388e9c81SLen Brown 			continue;
1182388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1183388e9c81SLen Brown 	}
1184388e9c81SLen Brown 
1185c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1186c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1187c98d5d94SLen Brown 		return 0;
1188c98d5d94SLen Brown 
11890b2bb692SLen Brown 	if (do_skl_residency) {
11900b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
11910b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
11920b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
11930b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
11940b2bb692SLen Brown 	}
11950b2bb692SLen Brown 
1196c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
1197ee7e38e3SLen Brown 	if (do_pc3)
1198c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
1199ee7e38e3SLen Brown 	if (do_pc6)
1200c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
1201ee7e38e3SLen Brown 	if (do_pc7)
1202c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1203ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1204ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1205ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1206c98d5d94SLen Brown 
1207889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1208889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1209889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1210889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1211889facbeSLen Brown 
1212fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
121327d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
121427d47356SLen Brown 
1215889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1216889facbeSLen Brown 
1217889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1218889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1219388e9c81SLen Brown 
1220388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1221388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1222388e9c81SLen Brown 			continue;
1223388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1224388e9c81SLen Brown 	}
1225c98d5d94SLen Brown 	return 0;
1226c98d5d94SLen Brown }
1227c98d5d94SLen Brown /*
1228c98d5d94SLen Brown  * sum the counters for all cpus in the system
1229c98d5d94SLen Brown  * compute the weighted average
1230c98d5d94SLen Brown  */
1231c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1232c98d5d94SLen Brown 	struct pkg_data *p)
1233c98d5d94SLen Brown {
1234388e9c81SLen Brown 	int i;
1235388e9c81SLen Brown 	struct msr_counter *mp;
1236388e9c81SLen Brown 
1237c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1238c98d5d94SLen Brown 
1239c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1240c98d5d94SLen Brown 
1241c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1242c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1243c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1244c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1245c98d5d94SLen Brown 
1246c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1247c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1248c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
1249c98d5d94SLen Brown 
12500b2bb692SLen Brown 	if (do_skl_residency) {
12510b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
12520b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
12530b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
12540b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
12550b2bb692SLen Brown 	}
12560b2bb692SLen Brown 
1257c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
1258ee7e38e3SLen Brown 	if (do_pc3)
1259c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
1260ee7e38e3SLen Brown 	if (do_pc6)
1261c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
1262ee7e38e3SLen Brown 	if (do_pc7)
1263c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1264ca58710fSKristen Carlson Accardi 
1265ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1266ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1267ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1268388e9c81SLen Brown 
1269388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1270388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1271388e9c81SLen Brown 			continue;
1272388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1273388e9c81SLen Brown 	}
1274388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1275388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1276388e9c81SLen Brown 			continue;
1277388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1278388e9c81SLen Brown 	}
1279388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1280388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1281388e9c81SLen Brown 			continue;
1282388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1283388e9c81SLen Brown 	}
1284c98d5d94SLen Brown }
1285c98d5d94SLen Brown 
1286c98d5d94SLen Brown static unsigned long long rdtsc(void)
1287c98d5d94SLen Brown {
1288c98d5d94SLen Brown 	unsigned int low, high;
1289c98d5d94SLen Brown 
1290c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1291c98d5d94SLen Brown 
1292c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1293c98d5d94SLen Brown }
1294c98d5d94SLen Brown 
1295c98d5d94SLen Brown /*
1296c98d5d94SLen Brown  * get_counters(...)
1297c98d5d94SLen Brown  * migrate to cpu
1298c98d5d94SLen Brown  * acquire and record local counters for that cpu
1299c98d5d94SLen Brown  */
1300c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1301c98d5d94SLen Brown {
1302c98d5d94SLen Brown 	int cpu = t->cpu_id;
1303889facbeSLen Brown 	unsigned long long msr;
13040102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1305388e9c81SLen Brown 	struct msr_counter *mp;
1306388e9c81SLen Brown 	int i;
1307c98d5d94SLen Brown 
1308e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1309b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
131015aaa346SLen Brown 		return -1;
1311e52966c0SLen Brown 	}
131215aaa346SLen Brown 
13130102b067SLen Brown retry:
1314c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
131515aaa346SLen Brown 
1316812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
13170102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
13180102b067SLen Brown 
13190102b067SLen Brown 		/*
13200102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
13210102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
13220102b067SLen Brown 		 *
13230102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
13240102b067SLen Brown 		 * individual system call, so delays may occur
13250102b067SLen Brown 		 * between them.  If the time to read them
13260102b067SLen Brown 		 * varies by a large amount, we re-read them.
13270102b067SLen Brown 		 */
13280102b067SLen Brown 
13290102b067SLen Brown 		/*
13300102b067SLen Brown 		 * This initial dummy APERF read has been seen to
13310102b067SLen Brown 		 * reduce jitter in the subsequent reads.
13320102b067SLen Brown 		 */
13330102b067SLen Brown 
13349c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1335c98d5d94SLen Brown 			return -3;
13360102b067SLen Brown 
13370102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
13380102b067SLen Brown 
13390102b067SLen Brown 		tsc_before = t->tsc;
13400102b067SLen Brown 
13410102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
13420102b067SLen Brown 			return -3;
13430102b067SLen Brown 
13440102b067SLen Brown 		tsc_between = rdtsc();
13450102b067SLen Brown 
13469c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1347c98d5d94SLen Brown 			return -4;
13480102b067SLen Brown 
13490102b067SLen Brown 		tsc_after = rdtsc();
13500102b067SLen Brown 
13510102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
13520102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
13530102b067SLen Brown 
13540102b067SLen Brown 		/*
13550102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
13560102b067SLen Brown 		 * differ by more than 2x, then try again.
13570102b067SLen Brown 		 */
13580102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
13590102b067SLen Brown 			aperf_mperf_retry_count++;
13600102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
13610102b067SLen Brown 				goto retry;
13620102b067SLen Brown 			else
13630102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
13640102b067SLen Brown 					cpu, aperf_time, mperf_time);
13650102b067SLen Brown 		}
13660102b067SLen Brown 		aperf_mperf_retry_count = 0;
13670102b067SLen Brown 
1368b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1369b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
137088c3281fSLen Brown 	}
137188c3281fSLen Brown 
1372812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1373562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1374812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
13751ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
13761ed51011SLen Brown 			return -5;
13771ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
13781ed51011SLen Brown 	}
1379c98d5d94SLen Brown 
1380144b44b1SLen Brown 	if (use_c1_residency_msr) {
1381144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1382144b44b1SLen Brown 			return -6;
1383144b44b1SLen Brown 	}
1384144b44b1SLen Brown 
1385388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1386388e9c81SLen Brown 		if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1387388e9c81SLen Brown 			return -10;
1388388e9c81SLen Brown 	}
1389388e9c81SLen Brown 
1390388e9c81SLen Brown 
1391c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1392c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1393c98d5d94SLen Brown 		return 0;
1394c98d5d94SLen Brown 
1395812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1396c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1397c98d5d94SLen Brown 			return -6;
1398144b44b1SLen Brown 	}
1399144b44b1SLen Brown 
1400812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1401c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1402c98d5d94SLen Brown 			return -7;
1403fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1404fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1405fb5d4327SDasaratharaman Chandramouli 			return -7;
1406103a8feaSLen Brown 	}
140715aaa346SLen Brown 
1408812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1409c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1410c98d5d94SLen Brown 			return -8;
1411c98d5d94SLen Brown 
1412812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1413889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1414889facbeSLen Brown 			return -9;
1415889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1416889facbeSLen Brown 	}
1417889facbeSLen Brown 
1418388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1419388e9c81SLen Brown 		if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1420388e9c81SLen Brown 			return -10;
1421388e9c81SLen Brown 	}
1422889facbeSLen Brown 
1423c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1424c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1425c98d5d94SLen Brown 		return 0;
142615aaa346SLen Brown 
14270b2bb692SLen Brown 	if (do_skl_residency) {
14280b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
14290b2bb692SLen Brown 			return -10;
14300b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
14310b2bb692SLen Brown 			return -11;
14320b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
14330b2bb692SLen Brown 			return -12;
14340b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
14350b2bb692SLen Brown 			return -13;
14360b2bb692SLen Brown 	}
1437ee7e38e3SLen Brown 	if (do_pc3)
1438c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1439c98d5d94SLen Brown 			return -9;
1440ee7e38e3SLen Brown 	if (do_pc6)
1441c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1442c98d5d94SLen Brown 			return -10;
1443ee7e38e3SLen Brown 	if (do_pc2)
1444c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1445c98d5d94SLen Brown 			return -11;
1446ee7e38e3SLen Brown 	if (do_pc7)
1447c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1448c98d5d94SLen Brown 			return -12;
1449ca58710fSKristen Carlson Accardi 	if (do_c8_c9_c10) {
1450ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1451ca58710fSKristen Carlson Accardi 			return -13;
1452ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1453ca58710fSKristen Carlson Accardi 			return -13;
1454ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1455ca58710fSKristen Carlson Accardi 			return -13;
1456ca58710fSKristen Carlson Accardi 	}
1457889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1458889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1459889facbeSLen Brown 			return -13;
1460889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1461889facbeSLen Brown 	}
14629148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1463889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1464889facbeSLen Brown 			return -14;
1465889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1466889facbeSLen Brown 	}
1467889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1468889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1469889facbeSLen Brown 			return -15;
1470889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1471889facbeSLen Brown 	}
1472889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1473889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1474889facbeSLen Brown 			return -16;
1475889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1476889facbeSLen Brown 	}
1477889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1478889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1479889facbeSLen Brown 			return -16;
1480889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1481889facbeSLen Brown 	}
1482889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1483889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1484889facbeSLen Brown 			return -16;
1485889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1486889facbeSLen Brown 	}
1487812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1488889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1489889facbeSLen Brown 			return -17;
1490889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1491889facbeSLen Brown 	}
1492fdf676e5SLen Brown 
1493812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1494fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1495fdf676e5SLen Brown 
1496812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
149727d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
149827d47356SLen Brown 
1499388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1500388e9c81SLen Brown 		if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1501388e9c81SLen Brown 			return -10;
1502388e9c81SLen Brown 	}
1503388e9c81SLen Brown 
150415aaa346SLen Brown 	return 0;
1505103a8feaSLen Brown }
1506103a8feaSLen Brown 
1507ee7e38e3SLen Brown /*
1508ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1509ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1510ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1511ee7e38e3SLen Brown  */
1512ee7e38e3SLen Brown 
1513ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1514ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1515ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1516ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1517ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1518ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1519ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1520ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1521ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1522ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1523ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1524ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
15250b2bb692SLen Brown #define PCL__8 12 /* PC8 */
15260b2bb692SLen Brown #define PCL__9 13 /* PC9 */
15270b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1528ee7e38e3SLen Brown 
1529ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1530ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
15310b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1532ee7e38e3SLen Brown 
1533e9257f5fSLen 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};
1534e9257f5fSLen 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};
1535e9257f5fSLen 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};
1536e9257f5fSLen 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, PCLRSV, PCLRSV};
1537e9257f5fSLen Brown int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1538e9257f5fSLen 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};
1539e4085d54SLen Brown int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1540d8ebb442SLen Brown int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1541ee7e38e3SLen Brown 
1542a2b7b749SLen Brown 
1543a2b7b749SLen Brown static void
1544a2b7b749SLen Brown calculate_tsc_tweak()
1545a2b7b749SLen Brown {
1546a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1547a2b7b749SLen Brown }
1548a2b7b749SLen Brown 
1549fcd17211SLen Brown static void
1550fcd17211SLen Brown dump_nhm_platform_info(void)
1551103a8feaSLen Brown {
1552103a8feaSLen Brown 	unsigned long long msr;
1553103a8feaSLen Brown 	unsigned int ratio;
1554103a8feaSLen Brown 
1555ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1556103a8feaSLen Brown 
1557b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
15586574a5d5SLen Brown 
1559103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1560b7d8c148SLen Brown 	fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1561103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1562103a8feaSLen Brown 
1563103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1564b7d8c148SLen Brown 	fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
1565103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1566103a8feaSLen Brown 
15677ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1568b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1569bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
157067920418SLen Brown 
1571fcd17211SLen Brown 	return;
1572fcd17211SLen Brown }
1573fcd17211SLen Brown 
1574fcd17211SLen Brown static void
1575fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1576fcd17211SLen Brown {
1577fcd17211SLen Brown 	unsigned long long msr;
1578fcd17211SLen Brown 	unsigned int ratio;
1579fcd17211SLen Brown 
15807ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1581fcd17211SLen Brown 
1582b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1583fcd17211SLen Brown 
1584fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1585fcd17211SLen Brown 	if (ratio)
1586b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1587fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1588fcd17211SLen Brown 
1589fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1590fcd17211SLen Brown 	if (ratio)
1591b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1592fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1593fcd17211SLen Brown 	return;
1594fcd17211SLen Brown }
1595fcd17211SLen Brown 
1596fcd17211SLen Brown static void
1597fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1598fcd17211SLen Brown {
1599fcd17211SLen Brown 	unsigned long long msr;
1600fcd17211SLen Brown 	unsigned int ratio;
16016574a5d5SLen Brown 
16027ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
16036574a5d5SLen Brown 
1604b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
16056574a5d5SLen Brown 
16066574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
16076574a5d5SLen Brown 	if (ratio)
1608b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
16096574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16106574a5d5SLen Brown 
16116574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
16126574a5d5SLen Brown 	if (ratio)
1613b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
16146574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16156574a5d5SLen Brown 
16166574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
16176574a5d5SLen Brown 	if (ratio)
1618b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
16196574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16206574a5d5SLen Brown 
16216574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
16226574a5d5SLen Brown 	if (ratio)
1623b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
16246574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16256574a5d5SLen Brown 
16266574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
16276574a5d5SLen Brown 	if (ratio)
1628b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
16296574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16306574a5d5SLen Brown 
16316574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
16326574a5d5SLen Brown 	if (ratio)
1633b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
16346574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16356574a5d5SLen Brown 
16366574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
16376574a5d5SLen Brown 	if (ratio)
1638b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
16396574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16406574a5d5SLen Brown 
16416574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
16426574a5d5SLen Brown 	if (ratio)
1643b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
16446574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1645103a8feaSLen Brown 	return;
1646fcd17211SLen Brown }
1647fcd17211SLen Brown 
1648fcd17211SLen Brown static void
1649fcd17211SLen Brown dump_nhm_turbo_ratio_limits(void)
1650fcd17211SLen Brown {
1651fcd17211SLen Brown 	unsigned long long msr;
1652fcd17211SLen Brown 	unsigned int ratio;
1653103a8feaSLen Brown 
16547ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1655103a8feaSLen Brown 
1656b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
16576574a5d5SLen Brown 
16586574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
16596574a5d5SLen Brown 	if (ratio)
1660b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
16616574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16626574a5d5SLen Brown 
16636574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
16646574a5d5SLen Brown 	if (ratio)
1665b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
16666574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16676574a5d5SLen Brown 
16686574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
16696574a5d5SLen Brown 	if (ratio)
1670b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
16716574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16726574a5d5SLen Brown 
16736574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
16746574a5d5SLen Brown 	if (ratio)
1675b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
16766574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
16776574a5d5SLen Brown 
1678103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
1679103a8feaSLen Brown 	if (ratio)
1680b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1681103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1682103a8feaSLen Brown 
1683103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
1684103a8feaSLen Brown 	if (ratio)
1685b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1686103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1687103a8feaSLen Brown 
1688103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1689103a8feaSLen Brown 	if (ratio)
1690b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1691103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1692103a8feaSLen Brown 
1693103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
1694103a8feaSLen Brown 	if (ratio)
1695b7d8c148SLen Brown 		fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1696103a8feaSLen Brown 			ratio, bclk, ratio * bclk);
1697fcd17211SLen Brown 	return;
1698fcd17211SLen Brown }
16993a9a941dSLen Brown 
1700fcd17211SLen Brown static void
1701fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
1702fb5d4327SDasaratharaman Chandramouli {
1703cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
1704cbf97abaSHubert Chrzaniuk 
1705fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
1706cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
1707cbf97abaSHubert Chrzaniuk 	int i, b_nr;
1708cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
1709cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
1710fb5d4327SDasaratharaman Chandramouli 
1711ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1712fb5d4327SDasaratharaman Chandramouli 
1713b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1714bfae2052SLen Brown 		base_cpu, msr);
1715fb5d4327SDasaratharaman Chandramouli 
1716fb5d4327SDasaratharaman Chandramouli 	/**
1717fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
1718cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
1719cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
1720fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
1721fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
1722fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
1723fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
1724fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
1725fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
1726fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
1727fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
1728fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
1729fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
1730fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
1731fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
1732fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
1733fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
1734fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
1735fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
1736fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
1737fb5d4327SDasaratharaman Chandramouli 	 */
1738fb5d4327SDasaratharaman Chandramouli 
1739cbf97abaSHubert Chrzaniuk 	b_nr = 0;
1740cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
1741cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
1742cbf97abaSHubert Chrzaniuk 
1743cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
1744fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
1745cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
1746fb5d4327SDasaratharaman Chandramouli 
1747cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
1748cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1749cbf97abaSHubert Chrzaniuk 		b_nr++;
1750fb5d4327SDasaratharaman Chandramouli 	}
1751cbf97abaSHubert Chrzaniuk 
1752cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
1753cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
1754b7d8c148SLen Brown 			fprintf(outf,
1755fb5d4327SDasaratharaman Chandramouli 				"%d * %.0f = %.0f MHz max turbo %d active cores\n",
1756cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
1757fb5d4327SDasaratharaman Chandramouli }
1758fb5d4327SDasaratharaman Chandramouli 
1759fb5d4327SDasaratharaman Chandramouli static void
1760fcd17211SLen Brown dump_nhm_cst_cfg(void)
1761fcd17211SLen Brown {
1762fcd17211SLen Brown 	unsigned long long msr;
1763fcd17211SLen Brown 
17647ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1765fcd17211SLen Brown 
1766fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1767fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1768fcd17211SLen Brown 
1769b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
1770fcd17211SLen Brown 
1771b7d8c148SLen Brown 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1772fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1773fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1774fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1775fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1776fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
17776c34f160SLen Brown 		(unsigned int)msr & 0xF,
1778fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
1779fcd17211SLen Brown 	return;
1780103a8feaSLen Brown }
1781103a8feaSLen Brown 
17826fb3143bSLen Brown static void
17836fb3143bSLen Brown dump_config_tdp(void)
17846fb3143bSLen Brown {
17856fb3143bSLen Brown 	unsigned long long msr;
17866fb3143bSLen Brown 
17876fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1788b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1789685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
17906fb3143bSLen Brown 
17916fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1792b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
17936fb3143bSLen Brown 	if (msr) {
1794685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1795685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1796685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1797685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
17986fb3143bSLen Brown 	}
1799b7d8c148SLen Brown 	fprintf(outf, ")\n");
18006fb3143bSLen Brown 
18016fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1802b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
18036fb3143bSLen Brown 	if (msr) {
1804685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1805685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1806685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1807685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
18086fb3143bSLen Brown 	}
1809b7d8c148SLen Brown 	fprintf(outf, ")\n");
18106fb3143bSLen Brown 
18116fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1812b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
18136fb3143bSLen Brown 	if ((msr) & 0x3)
1814b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1815b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1816b7d8c148SLen Brown 	fprintf(outf, ")\n");
18176fb3143bSLen Brown 
18186fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1819b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1820685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
1821b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1822b7d8c148SLen Brown 	fprintf(outf, ")\n");
18236fb3143bSLen Brown }
18245a63426eSLen Brown 
18255a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
18265a63426eSLen Brown 
18275a63426eSLen Brown void print_irtl(void)
18285a63426eSLen Brown {
18295a63426eSLen Brown 	unsigned long long msr;
18305a63426eSLen Brown 
18315a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
18325a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
18335a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
18345a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
18355a63426eSLen Brown 
18365a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
18375a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
18385a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
18395a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
18405a63426eSLen Brown 
18415a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
18425a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
18435a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
18445a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
18455a63426eSLen Brown 
18465a63426eSLen Brown 	if (!do_irtl_hsw)
18475a63426eSLen Brown 		return;
18485a63426eSLen Brown 
18495a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
18505a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
18515a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
18525a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
18535a63426eSLen Brown 
18545a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
18555a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
18565a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
18575a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
18585a63426eSLen Brown 
18595a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
18605a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
18615a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
18625a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
18635a63426eSLen Brown 
18645a63426eSLen Brown }
186536229897SLen Brown void free_fd_percpu(void)
186636229897SLen Brown {
186736229897SLen Brown 	int i;
186836229897SLen Brown 
186901a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
187036229897SLen Brown 		if (fd_percpu[i] != 0)
187136229897SLen Brown 			close(fd_percpu[i]);
187236229897SLen Brown 	}
187336229897SLen Brown 
187436229897SLen Brown 	free(fd_percpu);
18756fb3143bSLen Brown }
18766fb3143bSLen Brown 
1877c98d5d94SLen Brown void free_all_buffers(void)
1878103a8feaSLen Brown {
1879c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
1880c98d5d94SLen Brown 	cpu_present_set = NULL;
188136229897SLen Brown 	cpu_present_setsize = 0;
1882103a8feaSLen Brown 
1883c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
1884c98d5d94SLen Brown 	cpu_affinity_set = NULL;
1885c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
1886103a8feaSLen Brown 
1887c98d5d94SLen Brown 	free(thread_even);
1888c98d5d94SLen Brown 	free(core_even);
1889c98d5d94SLen Brown 	free(package_even);
1890103a8feaSLen Brown 
1891c98d5d94SLen Brown 	thread_even = NULL;
1892c98d5d94SLen Brown 	core_even = NULL;
1893c98d5d94SLen Brown 	package_even = NULL;
1894103a8feaSLen Brown 
1895c98d5d94SLen Brown 	free(thread_odd);
1896c98d5d94SLen Brown 	free(core_odd);
1897c98d5d94SLen Brown 	free(package_odd);
1898103a8feaSLen Brown 
1899c98d5d94SLen Brown 	thread_odd = NULL;
1900c98d5d94SLen Brown 	core_odd = NULL;
1901c98d5d94SLen Brown 	package_odd = NULL;
1902103a8feaSLen Brown 
1903c98d5d94SLen Brown 	free(output_buffer);
1904c98d5d94SLen Brown 	output_buffer = NULL;
1905c98d5d94SLen Brown 	outp = NULL;
190636229897SLen Brown 
190736229897SLen Brown 	free_fd_percpu();
1908562a2d37SLen Brown 
1909562a2d37SLen Brown 	free(irq_column_2_cpu);
1910562a2d37SLen Brown 	free(irqs_per_cpu);
1911103a8feaSLen Brown }
1912103a8feaSLen Brown 
1913103a8feaSLen Brown /*
191457a42a34SJosh Triplett  * Open a file, and exit on failure
191557a42a34SJosh Triplett  */
191657a42a34SJosh Triplett FILE *fopen_or_die(const char *path, const char *mode)
191757a42a34SJosh Triplett {
1918b7d8c148SLen Brown 	FILE *filep = fopen(path, mode);
1919b2c95d90SJosh Triplett 	if (!filep)
1920b2c95d90SJosh Triplett 		err(1, "%s: open failed", path);
192157a42a34SJosh Triplett 	return filep;
192257a42a34SJosh Triplett }
192357a42a34SJosh Triplett 
192457a42a34SJosh Triplett /*
192595aebc44SJosh Triplett  * Parse a file containing a single int.
192695aebc44SJosh Triplett  */
192795aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
192895aebc44SJosh Triplett {
192995aebc44SJosh Triplett 	va_list args;
193095aebc44SJosh Triplett 	char path[PATH_MAX];
193195aebc44SJosh Triplett 	FILE *filep;
193295aebc44SJosh Triplett 	int value;
193395aebc44SJosh Triplett 
193495aebc44SJosh Triplett 	va_start(args, fmt);
193595aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
193695aebc44SJosh Triplett 	va_end(args);
193757a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
1938b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
1939b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
194095aebc44SJosh Triplett 	fclose(filep);
194195aebc44SJosh Triplett 	return value;
194295aebc44SJosh Triplett }
194395aebc44SJosh Triplett 
194495aebc44SJosh Triplett /*
1945e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
1946e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
1947e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
1948103a8feaSLen Brown  */
1949e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
1950103a8feaSLen Brown {
1951e275b388SDasaratharaman Chandramouli 	char path[64];
1952e275b388SDasaratharaman Chandramouli 	FILE *filep;
1953e275b388SDasaratharaman Chandramouli 	int this_cpu;
1954e275b388SDasaratharaman Chandramouli 	char character;
1955e275b388SDasaratharaman Chandramouli 	int i;
1956e275b388SDasaratharaman Chandramouli 
1957e275b388SDasaratharaman Chandramouli 	sprintf(path,
1958e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1959e275b388SDasaratharaman Chandramouli 		cpu);
1960e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
1961e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
1962e275b388SDasaratharaman Chandramouli 		perror(path);
1963e275b388SDasaratharaman Chandramouli 		exit(1);
1964e275b388SDasaratharaman Chandramouli 	}
1965e275b388SDasaratharaman Chandramouli 
1966e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
1967e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
1968e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
1969e275b388SDasaratharaman Chandramouli 			fclose(filep);
1970e275b388SDasaratharaman Chandramouli 			return i;
1971e275b388SDasaratharaman Chandramouli 		}
1972e275b388SDasaratharaman Chandramouli 
1973e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
1974e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
1975e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
1976e275b388SDasaratharaman Chandramouli 	}
1977e275b388SDasaratharaman Chandramouli 
1978e275b388SDasaratharaman Chandramouli 	fclose(filep);
1979e275b388SDasaratharaman Chandramouli 	return -1;
1980c98d5d94SLen Brown }
1981103a8feaSLen Brown 
1982c98d5d94SLen Brown /*
1983c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
1984c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
1985c98d5d94SLen Brown  */
1986c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
1987c98d5d94SLen Brown {
198895aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1989103a8feaSLen Brown }
1990103a8feaSLen Brown 
1991103a8feaSLen Brown int get_physical_package_id(int cpu)
1992103a8feaSLen Brown {
199395aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1994103a8feaSLen Brown }
1995103a8feaSLen Brown 
1996103a8feaSLen Brown int get_core_id(int cpu)
1997103a8feaSLen Brown {
199895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1999103a8feaSLen Brown }
2000103a8feaSLen Brown 
2001c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
2002c98d5d94SLen Brown {
2003c98d5d94SLen Brown 	char path[80];
2004c98d5d94SLen Brown 	FILE *filep;
2005e275b388SDasaratharaman Chandramouli 	int sib1;
2006e275b388SDasaratharaman Chandramouli 	int matches = 0;
2007c98d5d94SLen Brown 	char character;
2008e275b388SDasaratharaman Chandramouli 	char str[100];
2009e275b388SDasaratharaman Chandramouli 	char *ch;
2010c98d5d94SLen Brown 
2011c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
201257a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2013e275b388SDasaratharaman Chandramouli 
2014103a8feaSLen Brown 	/*
2015c98d5d94SLen Brown 	 * file format:
2016e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
2017e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
2018c98d5d94SLen Brown 	 */
2019e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
2020e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
2021e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
2022e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
2023e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
2024e275b388SDasaratharaman Chandramouli 		matches++;
2025e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
2026e275b388SDasaratharaman Chandramouli 	}
2027c98d5d94SLen Brown 
2028c98d5d94SLen Brown 	fclose(filep);
2029e275b388SDasaratharaman Chandramouli 	return matches+1;
2030c98d5d94SLen Brown }
2031c98d5d94SLen Brown 
2032c98d5d94SLen Brown /*
2033c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2034c98d5d94SLen Brown  * skip non-present cpus
2035103a8feaSLen Brown  */
2036103a8feaSLen Brown 
2037c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2038c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2039c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2040c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2041c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2042c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2043c98d5d94SLen Brown {
2044c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
2045c98d5d94SLen Brown 
2046c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2047c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2048c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
2049c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
2050c98d5d94SLen Brown 				struct thread_data *t, *t2;
2051c98d5d94SLen Brown 				struct core_data *c, *c2;
2052c98d5d94SLen Brown 				struct pkg_data *p, *p2;
2053c98d5d94SLen Brown 
2054c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2055c98d5d94SLen Brown 
2056c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
2057c98d5d94SLen Brown 					continue;
2058c98d5d94SLen Brown 
2059c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2060c98d5d94SLen Brown 
2061c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
2062c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
2063c98d5d94SLen Brown 
2064c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
2065c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
2066c98d5d94SLen Brown 
2067c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
2068c98d5d94SLen Brown 				if (retval)
2069c98d5d94SLen Brown 					return retval;
2070c98d5d94SLen Brown 			}
2071c98d5d94SLen Brown 		}
2072c98d5d94SLen Brown 	}
2073c98d5d94SLen Brown 	return 0;
2074c98d5d94SLen Brown }
2075c98d5d94SLen Brown 
2076c98d5d94SLen Brown /*
2077c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2078c98d5d94SLen Brown  * return max_cpu number
2079c98d5d94SLen Brown  */
2080c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2081103a8feaSLen Brown {
2082103a8feaSLen Brown 	FILE *fp;
2083c98d5d94SLen Brown 	int cpu_num;
2084103a8feaSLen Brown 	int retval;
2085103a8feaSLen Brown 
208657a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2087103a8feaSLen Brown 
2088103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2089b2c95d90SJosh Triplett 	if (retval != 0)
2090b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2091103a8feaSLen Brown 
2092c98d5d94SLen Brown 	while (1) {
2093c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2094103a8feaSLen Brown 		if (retval != 1)
2095103a8feaSLen Brown 			break;
2096103a8feaSLen Brown 
2097c98d5d94SLen Brown 		retval = func(cpu_num);
2098c98d5d94SLen Brown 		if (retval) {
2099c98d5d94SLen Brown 			fclose(fp);
2100c98d5d94SLen Brown 			return(retval);
2101c98d5d94SLen Brown 		}
2102103a8feaSLen Brown 	}
2103103a8feaSLen Brown 	fclose(fp);
2104c98d5d94SLen Brown 	return 0;
2105103a8feaSLen Brown }
2106103a8feaSLen Brown 
2107103a8feaSLen Brown void re_initialize(void)
2108103a8feaSLen Brown {
2109c98d5d94SLen Brown 	free_all_buffers();
2110c98d5d94SLen Brown 	setup_all_buffers();
2111c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2112103a8feaSLen Brown }
2113103a8feaSLen Brown 
2114c98d5d94SLen Brown 
2115103a8feaSLen Brown /*
2116c98d5d94SLen Brown  * count_cpus()
2117c98d5d94SLen Brown  * remember the last one seen, it will be the max
2118103a8feaSLen Brown  */
2119c98d5d94SLen Brown int count_cpus(int cpu)
2120103a8feaSLen Brown {
2121c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
2122c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
2123103a8feaSLen Brown 
2124c98d5d94SLen Brown 	topo.num_cpus += 1;
2125c98d5d94SLen Brown 	return 0;
2126103a8feaSLen Brown }
2127c98d5d94SLen Brown int mark_cpu_present(int cpu)
2128c98d5d94SLen Brown {
2129c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
213015aaa346SLen Brown 	return 0;
2131103a8feaSLen Brown }
2132103a8feaSLen Brown 
2133562a2d37SLen Brown /*
2134562a2d37SLen Brown  * snapshot_proc_interrupts()
2135562a2d37SLen Brown  *
2136562a2d37SLen Brown  * read and record summary of /proc/interrupts
2137562a2d37SLen Brown  *
2138562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2139562a2d37SLen Brown  */
2140562a2d37SLen Brown int snapshot_proc_interrupts(void)
2141562a2d37SLen Brown {
2142562a2d37SLen Brown 	static FILE *fp;
2143562a2d37SLen Brown 	int column, retval;
2144562a2d37SLen Brown 
2145562a2d37SLen Brown 	if (fp == NULL)
2146562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2147562a2d37SLen Brown 	else
2148562a2d37SLen Brown 		rewind(fp);
2149562a2d37SLen Brown 
2150562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2151562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2152562a2d37SLen Brown 		int cpu_number;
2153562a2d37SLen Brown 
2154562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2155562a2d37SLen Brown 		if (retval != 1)
2156562a2d37SLen Brown 			break;
2157562a2d37SLen Brown 
2158562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2159562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2160562a2d37SLen Brown 			return 1;
2161562a2d37SLen Brown 		}
2162562a2d37SLen Brown 
2163562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2164562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2165562a2d37SLen Brown 	}
2166562a2d37SLen Brown 
2167562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2168562a2d37SLen Brown 	while (1) {
2169562a2d37SLen Brown 		int column;
2170562a2d37SLen Brown 		char buf[64];
2171562a2d37SLen Brown 
2172562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2173562a2d37SLen Brown 		if (retval != 1)
2174562a2d37SLen Brown 			break;
2175562a2d37SLen Brown 
2176562a2d37SLen Brown 		/* read the count per cpu */
2177562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2178562a2d37SLen Brown 
2179562a2d37SLen Brown 			int cpu_number, irq_count;
2180562a2d37SLen Brown 
2181562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2182562a2d37SLen Brown 			if (retval != 1)
2183562a2d37SLen Brown 				break;
2184562a2d37SLen Brown 
2185562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2186562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2187562a2d37SLen Brown 
2188562a2d37SLen Brown 		}
2189562a2d37SLen Brown 
2190562a2d37SLen Brown 		while (getc(fp) != '\n')
2191562a2d37SLen Brown 			;	/* flush interrupt description */
2192562a2d37SLen Brown 
2193562a2d37SLen Brown 	}
2194562a2d37SLen Brown 	return 0;
2195562a2d37SLen Brown }
219627d47356SLen Brown /*
2197fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2198fdf676e5SLen Brown  *
2199fdf676e5SLen Brown  * record snapshot of
2200fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2201fdf676e5SLen Brown  *
2202fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2203fdf676e5SLen Brown  */
2204fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2205fdf676e5SLen Brown {
2206fdf676e5SLen Brown 	FILE *fp;
2207fdf676e5SLen Brown 	int retval;
2208fdf676e5SLen Brown 
2209fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2210fdf676e5SLen Brown 
2211fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2212fdf676e5SLen Brown 	if (retval != 1)
2213fdf676e5SLen Brown 		err(1, "GFX rc6");
2214fdf676e5SLen Brown 
2215fdf676e5SLen Brown 	fclose(fp);
2216fdf676e5SLen Brown 
2217fdf676e5SLen Brown 	return 0;
2218fdf676e5SLen Brown }
2219fdf676e5SLen Brown /*
222027d47356SLen Brown  * snapshot_gfx_mhz()
222127d47356SLen Brown  *
222227d47356SLen Brown  * record snapshot of
222327d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
222427d47356SLen Brown  *
222527d47356SLen Brown  * return 1 if config change requires a restart, else return 0
222627d47356SLen Brown  */
222727d47356SLen Brown int snapshot_gfx_mhz(void)
222827d47356SLen Brown {
222927d47356SLen Brown 	static FILE *fp;
223027d47356SLen Brown 	int retval;
223127d47356SLen Brown 
223227d47356SLen Brown 	if (fp == NULL)
223327d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
223427d47356SLen Brown 	else
223527d47356SLen Brown 		rewind(fp);
223627d47356SLen Brown 
223727d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
223827d47356SLen Brown 	if (retval != 1)
223927d47356SLen Brown 		err(1, "GFX MHz");
224027d47356SLen Brown 
224127d47356SLen Brown 	return 0;
224227d47356SLen Brown }
2243562a2d37SLen Brown 
2244562a2d37SLen Brown /*
2245562a2d37SLen Brown  * snapshot /proc and /sys files
2246562a2d37SLen Brown  *
2247562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2248562a2d37SLen Brown  */
2249562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2250562a2d37SLen Brown {
2251562a2d37SLen Brown 	if (snapshot_proc_interrupts())
2252562a2d37SLen Brown 		return 1;
2253562a2d37SLen Brown 
2254812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2255fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2256fdf676e5SLen Brown 
2257812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
225827d47356SLen Brown 		snapshot_gfx_mhz();
225927d47356SLen Brown 
2260562a2d37SLen Brown 	return 0;
2261562a2d37SLen Brown }
2262562a2d37SLen Brown 
2263103a8feaSLen Brown void turbostat_loop()
2264103a8feaSLen Brown {
2265c98d5d94SLen Brown 	int retval;
2266e52966c0SLen Brown 	int restarted = 0;
2267c98d5d94SLen Brown 
2268103a8feaSLen Brown restart:
2269e52966c0SLen Brown 	restarted++;
2270e52966c0SLen Brown 
2271562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2272c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2273d91bb17cSLen Brown 	if (retval < -1) {
2274d91bb17cSLen Brown 		exit(retval);
2275d91bb17cSLen Brown 	} else if (retval == -1) {
2276e52966c0SLen Brown 		if (restarted > 1) {
2277e52966c0SLen Brown 			exit(retval);
2278e52966c0SLen Brown 		}
2279c98d5d94SLen Brown 		re_initialize();
2280c98d5d94SLen Brown 		goto restart;
2281c98d5d94SLen Brown 	}
2282e52966c0SLen Brown 	restarted = 0;
2283103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2284103a8feaSLen Brown 
2285103a8feaSLen Brown 	while (1) {
2286c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2287103a8feaSLen Brown 			re_initialize();
2288103a8feaSLen Brown 			goto restart;
2289103a8feaSLen Brown 		}
22902a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2291562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2292562a2d37SLen Brown 			goto restart;
2293c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2294d91bb17cSLen Brown 		if (retval < -1) {
2295d91bb17cSLen Brown 			exit(retval);
2296d91bb17cSLen Brown 		} else if (retval == -1) {
229715aaa346SLen Brown 			re_initialize();
229815aaa346SLen Brown 			goto restart;
229915aaa346SLen Brown 		}
2300103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2301103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2302ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2303ba3dec99SLen Brown 			re_initialize();
2304ba3dec99SLen Brown 			goto restart;
2305ba3dec99SLen Brown 		}
2306c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2307c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2308b7d8c148SLen Brown 		flush_output_stdout();
23092a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2310562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2311562a2d37SLen Brown 			goto restart;
2312c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2313d91bb17cSLen Brown 		if (retval < -1) {
2314d91bb17cSLen Brown 			exit(retval);
2315d91bb17cSLen Brown 		} else if (retval == -1) {
2316103a8feaSLen Brown 			re_initialize();
2317103a8feaSLen Brown 			goto restart;
2318103a8feaSLen Brown 		}
2319103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2320103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2321ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2322ba3dec99SLen Brown 			re_initialize();
2323ba3dec99SLen Brown 			goto restart;
2324ba3dec99SLen Brown 		}
2325c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2326c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2327b7d8c148SLen Brown 		flush_output_stdout();
2328103a8feaSLen Brown 	}
2329103a8feaSLen Brown }
2330103a8feaSLen Brown 
2331103a8feaSLen Brown void check_dev_msr()
2332103a8feaSLen Brown {
2333103a8feaSLen Brown 	struct stat sb;
23347ce7d5deSPrarit Bhargava 	char pathname[32];
2335103a8feaSLen Brown 
23367ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
23377ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2338a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2339d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2340103a8feaSLen Brown }
2341103a8feaSLen Brown 
234298481e79SLen Brown void check_permissions()
2343103a8feaSLen Brown {
234498481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
234598481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
234698481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
234798481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
234898481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
234998481e79SLen Brown 	int do_exit = 0;
23507ce7d5deSPrarit Bhargava 	char pathname[32];
235198481e79SLen Brown 
235298481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
235398481e79SLen Brown 	cap_header->pid = getpid();
235498481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
235598481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
235698481e79SLen Brown 		err(-6, "capget(2) failed");
235798481e79SLen Brown 
235898481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
235998481e79SLen Brown 		do_exit++;
236098481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
236198481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
236298481e79SLen Brown 	}
236398481e79SLen Brown 
236498481e79SLen Brown 	/* test file permissions */
23657ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
23667ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
236798481e79SLen Brown 		do_exit++;
236898481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
236998481e79SLen Brown 	}
237098481e79SLen Brown 
237198481e79SLen Brown 	/* if all else fails, thell them to be root */
237298481e79SLen Brown 	if (do_exit)
2373b2c95d90SJosh Triplett 		if (getuid() != 0)
2374d7899447SLen Brown 			warnx("... or simply run as root");
237598481e79SLen Brown 
237698481e79SLen Brown 	if (do_exit)
237798481e79SLen Brown 		exit(-6);
2378103a8feaSLen Brown }
2379103a8feaSLen Brown 
2380d7899447SLen Brown /*
2381d7899447SLen Brown  * NHM adds support for additional MSRs:
2382d7899447SLen Brown  *
2383d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2384d7899447SLen Brown  *
2385ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
2386d7899447SLen Brown  * MSR_NHM_SNB_PKG_CST_CFG_CTL     0x000000e2
2387d7899447SLen Brown  *
2388cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
2389cf4cbe53SLen Brown  *
2390d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2391d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2392d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2393d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2394d7899447SLen Brown  *
2395ee7e38e3SLen Brown  * Side effect:
2396ee7e38e3SLen Brown  * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
2397d7899447SLen Brown  */
2398ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2399103a8feaSLen Brown {
2400ee7e38e3SLen Brown 	unsigned long long msr;
240121ed5574SLen Brown 	unsigned int base_ratio;
2402ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2403ee7e38e3SLen Brown 
2404103a8feaSLen Brown 	if (!genuine_intel)
2405103a8feaSLen Brown 		return 0;
2406103a8feaSLen Brown 
2407103a8feaSLen Brown 	if (family != 6)
2408103a8feaSLen Brown 		return 0;
2409103a8feaSLen Brown 
241021ed5574SLen Brown 	bclk = discover_bclk(family, model);
241121ed5574SLen Brown 
2412103a8feaSLen Brown 	switch (model) {
2413869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2414869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2415103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2416869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
2417869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
2418869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2419869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2420ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2421ee7e38e3SLen Brown 		break;
2422869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
2423869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
2424869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2425869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2426ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
2427ee7e38e3SLen Brown 		break;
2428869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2429869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2430869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2431869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2432869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2433869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2434869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2435869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2436869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2437869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2438869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2439869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2440ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
2441ee7e38e3SLen Brown 		break;
2442d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2443d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
2444d8ebb442SLen Brown 		break;
2445869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2446cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2447869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2448ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2449ee7e38e3SLen Brown 		break;
2450869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
2451ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2452cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2453ee7e38e3SLen Brown 		break;
2454869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
2455005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2456ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2457ee7e38e3SLen Brown 		break;
2458869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2459869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
2460e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2461e4085d54SLen Brown 		break;
2462103a8feaSLen Brown 	default:
2463103a8feaSLen Brown 		return 0;
2464103a8feaSLen Brown 	}
24657ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
2466e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2467ee7e38e3SLen Brown 
2468ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
246921ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
247021ed5574SLen Brown 
247121ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
247221ed5574SLen Brown 	has_base_hz = 1;
2473ee7e38e3SLen Brown 	return 1;
2474103a8feaSLen Brown }
2475d7899447SLen Brown int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2476d7899447SLen Brown {
2477d7899447SLen Brown 	switch (model) {
2478d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2479869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2480869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2481869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
2482005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2483d7899447SLen Brown 		return 0;
2484d7899447SLen Brown 	default:
2485d7899447SLen Brown 		return 1;
2486d7899447SLen Brown 	}
2487d7899447SLen Brown }
24886574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
24896574a5d5SLen Brown {
24906574a5d5SLen Brown 	if (!genuine_intel)
24916574a5d5SLen Brown 		return 0;
24926574a5d5SLen Brown 
24936574a5d5SLen Brown 	if (family != 6)
24946574a5d5SLen Brown 		return 0;
24956574a5d5SLen Brown 
24966574a5d5SLen Brown 	switch (model) {
2497869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2498869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
24996574a5d5SLen Brown 		return 1;
25006574a5d5SLen Brown 	default:
25016574a5d5SLen Brown 		return 0;
25026574a5d5SLen Brown 	}
25036574a5d5SLen Brown }
2504fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2505fcd17211SLen Brown {
2506fcd17211SLen Brown 	if (!genuine_intel)
2507fcd17211SLen Brown 		return 0;
2508fcd17211SLen Brown 
2509fcd17211SLen Brown 	if (family != 6)
2510fcd17211SLen Brown 		return 0;
2511fcd17211SLen Brown 
2512fcd17211SLen Brown 	switch (model) {
2513869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
2514fcd17211SLen Brown 		return 1;
2515fcd17211SLen Brown 	default:
2516fcd17211SLen Brown 		return 0;
2517fcd17211SLen Brown 	}
2518fcd17211SLen Brown }
2519fcd17211SLen Brown 
2520fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2521fb5d4327SDasaratharaman Chandramouli {
2522fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2523fb5d4327SDasaratharaman Chandramouli 		return 0;
2524fb5d4327SDasaratharaman Chandramouli 
2525fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
2526fb5d4327SDasaratharaman Chandramouli 		return 0;
2527fb5d4327SDasaratharaman Chandramouli 
2528fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2529869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2530005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2531fb5d4327SDasaratharaman Chandramouli 		return 1;
2532fb5d4327SDasaratharaman Chandramouli 	default:
2533fb5d4327SDasaratharaman Chandramouli 		return 0;
2534fb5d4327SDasaratharaman Chandramouli 	}
2535fb5d4327SDasaratharaman Chandramouli }
25366fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
25376fb3143bSLen Brown {
25386fb3143bSLen Brown 	if (!genuine_intel)
25396fb3143bSLen Brown 		return 0;
25406fb3143bSLen Brown 
25416fb3143bSLen Brown 	if (family != 6)
25426fb3143bSLen Brown 		return 0;
25436fb3143bSLen Brown 
25446fb3143bSLen Brown 	switch (model) {
2545869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2546869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2547869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2548869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2549869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2550869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2551869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2552869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2553869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2554869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2555869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2556869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2557869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2558869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
25596fb3143bSLen Brown 
2560869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2561005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
25626fb3143bSLen Brown 		return 1;
25636fb3143bSLen Brown 	default:
25646fb3143bSLen Brown 		return 0;
25656fb3143bSLen Brown 	}
25666fb3143bSLen Brown }
25676fb3143bSLen Brown 
2568fcd17211SLen Brown static void
25691b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2570fcd17211SLen Brown {
2571fcd17211SLen Brown 	if (!do_nhm_platform_info)
2572fcd17211SLen Brown 		return;
2573fcd17211SLen Brown 
2574fcd17211SLen Brown 	dump_nhm_platform_info();
2575fcd17211SLen Brown 
2576fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
2577fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
2578fcd17211SLen Brown 
2579fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
2580fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
2581fcd17211SLen Brown 
2582fcd17211SLen Brown 	if (has_nhm_turbo_ratio_limit(family, model))
2583fcd17211SLen Brown 		dump_nhm_turbo_ratio_limits();
2584fcd17211SLen Brown 
2585fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
2586fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
2587fb5d4327SDasaratharaman Chandramouli 
25886fb3143bSLen Brown 	if (has_config_tdp(family, model))
25896fb3143bSLen Brown 		dump_config_tdp();
25906fb3143bSLen Brown 
2591fcd17211SLen Brown 	dump_nhm_cst_cfg();
2592fcd17211SLen Brown }
2593fcd17211SLen Brown 
25946574a5d5SLen Brown 
2595889facbeSLen Brown /*
2596889facbeSLen Brown  * print_epb()
2597889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
2598889facbeSLen Brown  */
2599889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2600889facbeSLen Brown {
2601889facbeSLen Brown 	unsigned long long msr;
2602889facbeSLen Brown 	char *epb_string;
2603889facbeSLen Brown 	int cpu;
2604889facbeSLen Brown 
2605889facbeSLen Brown 	if (!has_epb)
2606889facbeSLen Brown 		return 0;
2607889facbeSLen Brown 
2608889facbeSLen Brown 	cpu = t->cpu_id;
2609889facbeSLen Brown 
2610889facbeSLen Brown 	/* EPB is per-package */
2611889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2612889facbeSLen Brown 		return 0;
2613889facbeSLen Brown 
2614889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2615b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2616889facbeSLen Brown 		return -1;
2617889facbeSLen Brown 	}
2618889facbeSLen Brown 
2619889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2620889facbeSLen Brown 		return 0;
2621889facbeSLen Brown 
2622e9be7dd6SLen Brown 	switch (msr & 0xF) {
2623889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
2624889facbeSLen Brown 		epb_string = "performance";
2625889facbeSLen Brown 		break;
2626889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
2627889facbeSLen Brown 		epb_string = "balanced";
2628889facbeSLen Brown 		break;
2629889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
2630889facbeSLen Brown 		epb_string = "powersave";
2631889facbeSLen Brown 		break;
2632889facbeSLen Brown 	default:
2633889facbeSLen Brown 		epb_string = "custom";
2634889facbeSLen Brown 		break;
2635889facbeSLen Brown 	}
2636b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2637889facbeSLen Brown 
2638889facbeSLen Brown 	return 0;
2639889facbeSLen Brown }
26407f5c258eSLen Brown /*
26417f5c258eSLen Brown  * print_hwp()
26427f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
26437f5c258eSLen Brown  */
26447f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
26457f5c258eSLen Brown {
26467f5c258eSLen Brown 	unsigned long long msr;
26477f5c258eSLen Brown 	int cpu;
26487f5c258eSLen Brown 
26497f5c258eSLen Brown 	if (!has_hwp)
26507f5c258eSLen Brown 		return 0;
26517f5c258eSLen Brown 
26527f5c258eSLen Brown 	cpu = t->cpu_id;
26537f5c258eSLen Brown 
26547f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
26557f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
26567f5c258eSLen Brown 		return 0;
26577f5c258eSLen Brown 
26587f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
2659b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
26607f5c258eSLen Brown 		return -1;
26617f5c258eSLen Brown 	}
26627f5c258eSLen Brown 
26637f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
26647f5c258eSLen Brown 		return 0;
26657f5c258eSLen Brown 
2666b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
26677f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
26687f5c258eSLen Brown 
26697f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
26707f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
26717f5c258eSLen Brown 		return 0;
26727f5c258eSLen Brown 
26737f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
26747f5c258eSLen Brown 		return 0;
26757f5c258eSLen Brown 
2676b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
26777f5c258eSLen Brown 			"(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
26787f5c258eSLen Brown 			cpu, msr,
26797f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
26807f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
26817f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
26827f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
26837f5c258eSLen Brown 
26847f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
26857f5c258eSLen Brown 		return 0;
26867f5c258eSLen Brown 
2687b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
26887f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
26897f5c258eSLen Brown 			cpu, msr,
26907f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
26917f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
26927f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
26937f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
26947f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
26957f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
26967f5c258eSLen Brown 
26977f5c258eSLen Brown 	if (has_hwp_pkg) {
26987f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
26997f5c258eSLen Brown 			return 0;
27007f5c258eSLen Brown 
2701b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
27027f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
27037f5c258eSLen Brown 			cpu, msr,
27047f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
27057f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
27067f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
27077f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
27087f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
27097f5c258eSLen Brown 	}
27107f5c258eSLen Brown 	if (has_hwp_notify) {
27117f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
27127f5c258eSLen Brown 			return 0;
27137f5c258eSLen Brown 
2714b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
27157f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
27167f5c258eSLen Brown 			cpu, msr,
27177f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
27187f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
27197f5c258eSLen Brown 	}
27207f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
27217f5c258eSLen Brown 		return 0;
27227f5c258eSLen Brown 
2723b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
27247f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
27257f5c258eSLen Brown 			cpu, msr,
27267f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
27277f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
2728889facbeSLen Brown 
2729889facbeSLen Brown 	return 0;
2730889facbeSLen Brown }
2731889facbeSLen Brown 
27323a9a941dSLen Brown /*
27333a9a941dSLen Brown  * print_perf_limit()
27343a9a941dSLen Brown  */
27353a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
27363a9a941dSLen Brown {
27373a9a941dSLen Brown 	unsigned long long msr;
27383a9a941dSLen Brown 	int cpu;
27393a9a941dSLen Brown 
27403a9a941dSLen Brown 	cpu = t->cpu_id;
27413a9a941dSLen Brown 
27423a9a941dSLen Brown 	/* per-package */
27433a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
27443a9a941dSLen Brown 		return 0;
27453a9a941dSLen Brown 
27463a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
2747b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
27483a9a941dSLen Brown 		return -1;
27493a9a941dSLen Brown 	}
27503a9a941dSLen Brown 
27513a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
27523a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2753b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2754b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2755e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
27563a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
2757e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
2758e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2759e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
2760e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
2761e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
2762e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
2763e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
2764e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
2765e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
2766e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
2767e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
2768e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
2769b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2770e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
27713a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
2772e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
2773e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2774e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
2775e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
2776e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
2777e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
2778e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
2779e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
2780e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
2781e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
2782e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
2783e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
27843a9a941dSLen Brown 
27853a9a941dSLen Brown 	}
27863a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
27873a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2788b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2789b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
27903a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
27913a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
27923a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
27933a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
27943a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
27953a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
27963a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
27973a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
2798b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
27993a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
28003a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
28013a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
28023a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
28033a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
28043a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
28053a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
28063a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
28073a9a941dSLen Brown 	}
28083a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
28093a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2810b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2811b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
28123a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
28133a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
28143a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
28153a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
28163a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
28173a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
2818b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
28193a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
28203a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
28213a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
28223a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
28233a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
28243a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
28253a9a941dSLen Brown 	}
28263a9a941dSLen Brown 	return 0;
28273a9a941dSLen Brown }
28283a9a941dSLen Brown 
2829889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
2830889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
2831889facbeSLen Brown 
28321b69317dSColin Ian King double get_tdp(unsigned int model)
2833144b44b1SLen Brown {
2834144b44b1SLen Brown 	unsigned long long msr;
2835144b44b1SLen Brown 
2836144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
28377ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2838144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2839144b44b1SLen Brown 
2840144b44b1SLen Brown 	switch (model) {
2841869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
2842869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
2843144b44b1SLen Brown 		return 30.0;
2844144b44b1SLen Brown 	default:
2845144b44b1SLen Brown 		return 135.0;
2846144b44b1SLen Brown 	}
2847144b44b1SLen Brown }
2848144b44b1SLen Brown 
284940ee8e3bSAndrey Semin /*
285040ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
285140ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
285240ee8e3bSAndrey Semin  */
285340ee8e3bSAndrey Semin static double
285440ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
285540ee8e3bSAndrey Semin {
285640ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
285740ee8e3bSAndrey Semin 
285840ee8e3bSAndrey Semin 	switch (model) {
2859869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2860869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2861869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2862869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
2863005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
286440ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
286540ee8e3bSAndrey Semin 	default:
286640ee8e3bSAndrey Semin 		return (rapl_energy_units);
286740ee8e3bSAndrey Semin 	}
286840ee8e3bSAndrey Semin }
286940ee8e3bSAndrey Semin 
2870144b44b1SLen Brown 
2871889facbeSLen Brown /*
2872889facbeSLen Brown  * rapl_probe()
2873889facbeSLen Brown  *
2874144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2875889facbeSLen Brown  */
2876889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
2877889facbeSLen Brown {
2878889facbeSLen Brown 	unsigned long long msr;
2879144b44b1SLen Brown 	unsigned int time_unit;
2880889facbeSLen Brown 	double tdp;
2881889facbeSLen Brown 
2882889facbeSLen Brown 	if (!genuine_intel)
2883889facbeSLen Brown 		return;
2884889facbeSLen Brown 
2885889facbeSLen Brown 	if (family != 6)
2886889facbeSLen Brown 		return;
2887889facbeSLen Brown 
2888889facbeSLen Brown 	switch (model) {
2889869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
2890869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
2891869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2892869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2893869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2894869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2895869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2896144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2897812db3f7SLen Brown 		if (rapl_joules) {
2898812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2899812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
2900812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
2901812db3f7SLen Brown 		} else {
2902812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2903812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
2904812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
2905812db3f7SLen Brown 		}
2906889facbeSLen Brown 		break;
2907869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2908e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
2909812db3f7SLen Brown 		if (rapl_joules)
2910812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2911812db3f7SLen Brown 		else
2912812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2913e4085d54SLen Brown 		break;
2914869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2915869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2916869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2917869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
29180b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2919812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
2920812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
2921812db3f7SLen Brown 		if (rapl_joules) {
2922812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2923812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
2924812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
2925812db3f7SLen Brown 		} else {
2926812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2927812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
2928812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
2929812db3f7SLen Brown 		}
29300b2bb692SLen Brown 		break;
2931869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2932869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2933869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2934869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2935869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
2936005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
29370b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2938812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
2939812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
2940812db3f7SLen Brown 		if (rapl_joules) {
2941812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2942812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
2943812db3f7SLen Brown 		} else {
2944812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2945812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
2946812db3f7SLen Brown 		}
2947e6f9bb3cSLen Brown 		break;
2948869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
2949869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
29500b2bb692SLen 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;
2951812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
2952812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
2953812db3f7SLen Brown 		if (rapl_joules) {
2954812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2955812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
2956812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
2957812db3f7SLen Brown 		} else {
2958812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2959812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
2960812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
2961812db3f7SLen Brown 		}
2962144b44b1SLen Brown 		break;
2963869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2964869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2965144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
2966812db3f7SLen Brown 		if (rapl_joules) {
2967812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2968812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
2969812db3f7SLen Brown 		} else {
2970812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2971812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
2972812db3f7SLen Brown 		}
2973889facbeSLen Brown 		break;
2974869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
29750f644909SJacob 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;
2976812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
2977812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
2978812db3f7SLen Brown 		if (rapl_joules) {
2979812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
2980812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
2981812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
2982812db3f7SLen Brown 		} else {
2983812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
2984812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
2985812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
2986812db3f7SLen Brown 		}
29870f644909SJacob Pan 		break;
2988889facbeSLen Brown 	default:
2989889facbeSLen Brown 		return;
2990889facbeSLen Brown 	}
2991889facbeSLen Brown 
2992889facbeSLen Brown 	/* units on package 0, verify later other packages match */
29937ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
2994889facbeSLen Brown 		return;
2995889facbeSLen Brown 
2996889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
2997869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
2998144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2999144b44b1SLen Brown 	else
3000889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3001889facbeSLen Brown 
300240ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
300340ee8e3bSAndrey Semin 
3004144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3005144b44b1SLen Brown 	if (time_unit == 0)
3006144b44b1SLen Brown 		time_unit = 0xA;
3007889facbeSLen Brown 
3008144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3009144b44b1SLen Brown 
3010144b44b1SLen Brown 	tdp = get_tdp(model);
3011889facbeSLen Brown 
3012889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
3013d8af6f5fSLen Brown 	if (debug)
3014b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3015889facbeSLen Brown 
3016889facbeSLen Brown 	return;
3017889facbeSLen Brown }
3018889facbeSLen Brown 
30191b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
30203a9a941dSLen Brown {
30213a9a941dSLen Brown 	if (!genuine_intel)
30223a9a941dSLen Brown 		return;
30233a9a941dSLen Brown 
30243a9a941dSLen Brown 	if (family != 6)
30253a9a941dSLen Brown 		return;
30263a9a941dSLen Brown 
30273a9a941dSLen Brown 	switch (model) {
3028869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3029869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3030869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
30313a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3032869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
30333a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
30343a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
30353a9a941dSLen Brown 	default:
30363a9a941dSLen Brown 		return;
30373a9a941dSLen Brown 	}
30383a9a941dSLen Brown }
30393a9a941dSLen Brown 
3040889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3041889facbeSLen Brown {
3042889facbeSLen Brown 	unsigned long long msr;
3043889facbeSLen Brown 	unsigned int dts;
3044889facbeSLen Brown 	int cpu;
3045889facbeSLen Brown 
3046889facbeSLen Brown 	if (!(do_dts || do_ptm))
3047889facbeSLen Brown 		return 0;
3048889facbeSLen Brown 
3049889facbeSLen Brown 	cpu = t->cpu_id;
3050889facbeSLen Brown 
3051889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3052889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3053889facbeSLen Brown 		return 0;
3054889facbeSLen Brown 
3055889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3056b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3057889facbeSLen Brown 		return -1;
3058889facbeSLen Brown 	}
3059889facbeSLen Brown 
3060889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3061889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3062889facbeSLen Brown 			return 0;
3063889facbeSLen Brown 
3064889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3065b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3066889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3067889facbeSLen Brown 
3068889facbeSLen Brown #ifdef	THERM_DEBUG
3069889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3070889facbeSLen Brown 			return 0;
3071889facbeSLen Brown 
3072889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3073889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3074b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3075889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3076889facbeSLen Brown #endif
3077889facbeSLen Brown 	}
3078889facbeSLen Brown 
3079889facbeSLen Brown 
3080889facbeSLen Brown 	if (do_dts) {
3081889facbeSLen Brown 		unsigned int resolution;
3082889facbeSLen Brown 
3083889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3084889facbeSLen Brown 			return 0;
3085889facbeSLen Brown 
3086889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3087889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3088b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3089889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3090889facbeSLen Brown 
3091889facbeSLen Brown #ifdef THERM_DEBUG
3092889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3093889facbeSLen Brown 			return 0;
3094889facbeSLen Brown 
3095889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3096889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3097b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3098889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3099889facbeSLen Brown #endif
3100889facbeSLen Brown 	}
3101889facbeSLen Brown 
3102889facbeSLen Brown 	return 0;
3103889facbeSLen Brown }
3104889facbeSLen Brown 
3105889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3106889facbeSLen Brown {
3107b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3108889facbeSLen Brown 		cpu, label,
3109889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
3110889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
3111889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3112889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
3113889facbeSLen Brown 
3114889facbeSLen Brown 	return;
3115889facbeSLen Brown }
3116889facbeSLen Brown 
3117889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3118889facbeSLen Brown {
3119889facbeSLen Brown 	unsigned long long msr;
3120889facbeSLen Brown 	int cpu;
3121889facbeSLen Brown 
3122889facbeSLen Brown 	if (!do_rapl)
3123889facbeSLen Brown 		return 0;
3124889facbeSLen Brown 
3125889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
3126889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3127889facbeSLen Brown 		return 0;
3128889facbeSLen Brown 
3129889facbeSLen Brown 	cpu = t->cpu_id;
3130889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3131b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3132889facbeSLen Brown 		return -1;
3133889facbeSLen Brown 	}
3134889facbeSLen Brown 
3135889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3136889facbeSLen Brown 		return -1;
3137889facbeSLen Brown 
3138d8af6f5fSLen Brown 	if (debug) {
3139b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
3140889facbeSLen Brown 			"(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3141144b44b1SLen Brown 			rapl_power_units, rapl_energy_units, rapl_time_units);
3142889facbeSLen Brown 	}
3143144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
3144144b44b1SLen Brown 
3145889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3146889facbeSLen Brown                 	return -5;
3147889facbeSLen Brown 
3148889facbeSLen Brown 
3149b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3150889facbeSLen Brown 			cpu, msr,
3151889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3152889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3153889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3154889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3155889facbeSLen Brown 
3156144b44b1SLen Brown 	}
3157144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
3158144b44b1SLen Brown 
3159889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3160889facbeSLen Brown 			return -9;
3161889facbeSLen Brown 
3162b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3163889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "": "UN");
3164889facbeSLen Brown 
3165889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
3166b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3167889facbeSLen Brown 			cpu,
3168889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
3169889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
3170889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3171889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
3172889facbeSLen Brown 	}
3173889facbeSLen Brown 
31740b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
3175889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3176889facbeSLen Brown                 	return -6;
3177889facbeSLen Brown 
3178b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3179889facbeSLen Brown 			cpu, msr,
3180889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3181889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3182889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3183889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
31840b2bb692SLen Brown 	}
31850b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
3186889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3187889facbeSLen Brown 			return -9;
3188b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3189889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "": "UN");
3190889facbeSLen Brown 
3191889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
3192889facbeSLen Brown 	}
3193144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
3194d8af6f5fSLen Brown 		if (debug) {
3195889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3196889facbeSLen Brown 				return -7;
3197889facbeSLen Brown 
3198b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3199144b44b1SLen Brown 		}
3200144b44b1SLen Brown 	}
32019148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3202d8af6f5fSLen Brown 		if (debug) {
3203889facbeSLen Brown 			if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3204889facbeSLen Brown 				return -9;
3205b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3206889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
3207889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "Cores Limit");
3208889facbeSLen Brown 		}
3209889facbeSLen Brown 	}
3210889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
3211d8af6f5fSLen Brown 		if (debug) {
3212889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3213889facbeSLen Brown 				return -8;
3214889facbeSLen Brown 
3215b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3216889facbeSLen Brown 
3217889facbeSLen Brown 			if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3218889facbeSLen Brown 				return -9;
3219b7d8c148SLen Brown 			fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3220889facbeSLen Brown 					cpu, msr, (msr >> 31) & 1 ? "": "UN");
3221889facbeSLen Brown 			print_power_limit_msr(cpu, msr, "GFX Limit");
3222889facbeSLen Brown 		}
3223889facbeSLen Brown 	}
3224889facbeSLen Brown 	return 0;
3225889facbeSLen Brown }
3226889facbeSLen Brown 
3227d7899447SLen Brown /*
3228d7899447SLen Brown  * SNB adds support for additional MSRs:
3229d7899447SLen Brown  *
3230d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
3231d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
3232d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
3233d7899447SLen Brown  */
3234103a8feaSLen Brown 
3235d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
3236103a8feaSLen Brown {
3237103a8feaSLen Brown 	if (!genuine_intel)
3238103a8feaSLen Brown 		return 0;
3239103a8feaSLen Brown 
3240103a8feaSLen Brown 	switch (model) {
3241869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3242869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3243869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3244869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3245869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3246869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
3247869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3248869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3249869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3250869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3251869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3252869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3253869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3254869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3255869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3256869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3257869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3258869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
32595bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3260103a8feaSLen Brown 		return 1;
3261103a8feaSLen Brown 	}
3262103a8feaSLen Brown 	return 0;
3263103a8feaSLen Brown }
3264103a8feaSLen Brown 
3265d7899447SLen Brown /*
326671616c8eSLen Brown  * SLV client has supporet for unique MSRs:
326771616c8eSLen Brown  *
326871616c8eSLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
326971616c8eSLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
327071616c8eSLen Brown  */
327171616c8eSLen Brown 
327271616c8eSLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
327371616c8eSLen Brown {
327471616c8eSLen Brown 	if (!genuine_intel)
327571616c8eSLen Brown 		return 0;
327671616c8eSLen Brown 
327771616c8eSLen Brown 	switch (model) {
327871616c8eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
327971616c8eSLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
328071616c8eSLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
328171616c8eSLen Brown 		return 1;
328271616c8eSLen Brown 	}
328371616c8eSLen Brown 	return 0;
328471616c8eSLen Brown }
328571616c8eSLen Brown 
328671616c8eSLen Brown /*
3287d7899447SLen Brown  * HSW adds support for additional MSRs:
3288d7899447SLen Brown  *
3289d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
3290d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
3291d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
32925a63426eSLen Brown  *
32935a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
32945a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
32955a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
32965a63426eSLen Brown  *
3297d7899447SLen Brown  */
3298d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
3299ca58710fSKristen Carlson Accardi {
3300ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
3301ca58710fSKristen Carlson Accardi 		return 0;
3302ca58710fSKristen Carlson Accardi 
3303ca58710fSKristen Carlson Accardi 	switch (model) {
3304869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3305869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3306869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3307869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3308869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3309869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3310869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3311ca58710fSKristen Carlson Accardi 		return 1;
3312ca58710fSKristen Carlson Accardi 	}
3313ca58710fSKristen Carlson Accardi 	return 0;
3314ca58710fSKristen Carlson Accardi }
3315ca58710fSKristen Carlson Accardi 
33160b2bb692SLen Brown /*
33170b2bb692SLen Brown  * SKL adds support for additional MSRS:
33180b2bb692SLen Brown  *
33190b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
33200b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
33210b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
33220b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
33230b2bb692SLen Brown  */
33240b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
33250b2bb692SLen Brown {
33260b2bb692SLen Brown 	if (!genuine_intel)
33270b2bb692SLen Brown 		return 0;
33280b2bb692SLen Brown 
33290b2bb692SLen Brown 	switch (model) {
3330869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3331869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3332869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3333869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
33340b2bb692SLen Brown 		return 1;
33350b2bb692SLen Brown 	}
33360b2bb692SLen Brown 	return 0;
33370b2bb692SLen Brown }
33380b2bb692SLen Brown 
33390b2bb692SLen Brown 
3340ca58710fSKristen Carlson Accardi 
3341144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
3342144b44b1SLen Brown {
3343144b44b1SLen Brown 	if (!genuine_intel)
3344144b44b1SLen Brown 		return 0;
3345144b44b1SLen Brown 	switch (model) {
3346869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3347869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3348144b44b1SLen Brown 		return 1;
3349144b44b1SLen Brown 	}
3350144b44b1SLen Brown 	return 0;
3351144b44b1SLen Brown }
3352144b44b1SLen Brown 
3353fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3354fb5d4327SDasaratharaman Chandramouli {
3355fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3356fb5d4327SDasaratharaman Chandramouli 		return 0;
3357fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3358869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3359005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3360fb5d4327SDasaratharaman Chandramouli 		return 1;
3361fb5d4327SDasaratharaman Chandramouli 	}
3362fb5d4327SDasaratharaman Chandramouli 	return 0;
3363fb5d4327SDasaratharaman Chandramouli }
3364fb5d4327SDasaratharaman Chandramouli 
3365b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3366b2b34dfeSHubert Chrzaniuk {
3367b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3368b2b34dfeSHubert Chrzaniuk 		return 1024;
3369b2b34dfeSHubert Chrzaniuk 	return 1;
3370b2b34dfeSHubert Chrzaniuk }
3371b2b34dfeSHubert Chrzaniuk 
3372144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3373144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3374144b44b1SLen Brown 
3375144b44b1SLen Brown double slm_bclk(void)
3376144b44b1SLen Brown {
3377144b44b1SLen Brown 	unsigned long long msr = 3;
3378144b44b1SLen Brown 	unsigned int i;
3379144b44b1SLen Brown 	double freq;
3380144b44b1SLen Brown 
33817ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3382b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3383144b44b1SLen Brown 
3384144b44b1SLen Brown 	i = msr & 0xf;
3385144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3386b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
33870a91e551SColin Ian King 		i = 3;
3388144b44b1SLen Brown 	}
3389144b44b1SLen Brown 	freq = slm_freq_table[i];
3390144b44b1SLen Brown 
3391*8f6196c1SLen Brown 	if (debug)
3392b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3393144b44b1SLen Brown 
3394144b44b1SLen Brown 	return freq;
3395144b44b1SLen Brown }
3396144b44b1SLen Brown 
3397103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3398103a8feaSLen Brown {
3399121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3400103a8feaSLen Brown 		return 100.00;
3401144b44b1SLen Brown 	else if (is_slm(family, model))
3402144b44b1SLen Brown 		return slm_bclk();
3403103a8feaSLen Brown 	else
3404103a8feaSLen Brown 		return 133.33;
3405103a8feaSLen Brown }
3406103a8feaSLen Brown 
3407889facbeSLen Brown /*
3408889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3409889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3410889facbeSLen Brown  * This is usually equal to tjMax.
3411889facbeSLen Brown  *
3412889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3413889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3414889facbeSLen Brown  *
3415889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3416889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3417889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3418889facbeSLen Brown  */
3419889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3420889facbeSLen Brown {
3421889facbeSLen Brown 	unsigned long long msr;
3422889facbeSLen Brown 	unsigned int target_c_local;
3423889facbeSLen Brown 	int cpu;
3424889facbeSLen Brown 
3425889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
3426889facbeSLen Brown 	if (!(do_dts || do_ptm))
3427889facbeSLen Brown 		return 0;
3428889facbeSLen Brown 
3429889facbeSLen Brown 	/* this is a per-package concept */
3430889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3431889facbeSLen Brown 		return 0;
3432889facbeSLen Brown 
3433889facbeSLen Brown 	cpu = t->cpu_id;
3434889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3435b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3436889facbeSLen Brown 		return -1;
3437889facbeSLen Brown 	}
3438889facbeSLen Brown 
3439889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
3440889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
3441b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3442889facbeSLen Brown 			cpu, tcc_activation_temp);
3443889facbeSLen Brown 		return 0;
3444889facbeSLen Brown 	}
3445889facbeSLen Brown 
3446889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
3447d7899447SLen Brown 	if (!do_nhm_platform_info)
3448889facbeSLen Brown 		goto guess;
3449889facbeSLen Brown 
34507ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3451889facbeSLen Brown 		goto guess;
3452889facbeSLen Brown 
34533482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
3454889facbeSLen Brown 
3455d8af6f5fSLen Brown 	if (debug)
3456b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3457889facbeSLen Brown 			cpu, msr, target_c_local);
3458889facbeSLen Brown 
34593482124aSJean Delvare 	if (!target_c_local)
3460889facbeSLen Brown 		goto guess;
3461889facbeSLen Brown 
3462889facbeSLen Brown 	tcc_activation_temp = target_c_local;
3463889facbeSLen Brown 
3464889facbeSLen Brown 	return 0;
3465889facbeSLen Brown 
3466889facbeSLen Brown guess:
3467889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
3468b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3469889facbeSLen Brown 		cpu, tcc_activation_temp);
3470889facbeSLen Brown 
3471889facbeSLen Brown 	return 0;
3472889facbeSLen Brown }
347369807a63SLen Brown 
3474aa8d8cc7SLen Brown void decode_feature_control_msr(void)
3475aa8d8cc7SLen Brown {
3476aa8d8cc7SLen Brown 	unsigned long long msr;
3477aa8d8cc7SLen Brown 
3478aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3479aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3480aa8d8cc7SLen Brown 			base_cpu, msr,
3481aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3482aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
3483aa8d8cc7SLen Brown }
3484aa8d8cc7SLen Brown 
348569807a63SLen Brown void decode_misc_enable_msr(void)
348669807a63SLen Brown {
348769807a63SLen Brown 	unsigned long long msr;
348869807a63SLen Brown 
348969807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3490b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
349169807a63SLen Brown 			base_cpu, msr,
349269807a63SLen Brown 			msr & (1 << 3) ? "TCC" : "",
349369807a63SLen Brown 			msr & (1 << 16) ? "EIST" : "",
349469807a63SLen Brown 			msr & (1 << 18) ? "MONITOR" : "");
349569807a63SLen Brown }
349669807a63SLen Brown 
3497f0057310SLen Brown /*
3498f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
3499f0057310SLen Brown  *
3500f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
3501f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
3502f0057310SLen Brown  * bit[1] less so...
3503f0057310SLen Brown  */
3504f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
3505f0057310SLen Brown {
3506f0057310SLen Brown 	unsigned long long msr;
3507f0057310SLen Brown 
3508f0057310SLen Brown 	if (!do_nhm_platform_info)
3509f0057310SLen Brown 		return;
3510f0057310SLen Brown 
3511cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
3512cf4cbe53SLen Brown 		return;
3513cf4cbe53SLen Brown 
3514f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3515ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
3516f0057310SLen Brown 			base_cpu, msr,
3517f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
3518ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
3519ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
3520f0057310SLen Brown }
352171616c8eSLen Brown /*
352271616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
352371616c8eSLen Brown  *
352471616c8eSLen Brown  * This MSRs are present on Silvermont processors,
352571616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
352671616c8eSLen Brown  */
352771616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
352871616c8eSLen Brown {
352971616c8eSLen Brown 	unsigned long long msr;
353071616c8eSLen Brown 
353171616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
353271616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
353371616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
353471616c8eSLen Brown 
353571616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
353671616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
353771616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
353871616c8eSLen Brown }
35397f5c258eSLen Brown 
3540fcd17211SLen Brown void process_cpuid()
3541103a8feaSLen Brown {
354261a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
3543103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
3544103a8feaSLen Brown 
3545103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
3546103a8feaSLen Brown 
35475aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
3548103a8feaSLen Brown 
3549103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3550103a8feaSLen Brown 		genuine_intel = 1;
3551103a8feaSLen Brown 
3552d8af6f5fSLen Brown 	if (debug)
3553b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
3554103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
3555103a8feaSLen Brown 
35565aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
3557103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
3558103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
3559103a8feaSLen Brown 	stepping = fms & 0xf;
3560103a8feaSLen Brown 	if (family == 6 || family == 0xf)
3561103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
3562103a8feaSLen Brown 
356369807a63SLen Brown 	if (debug) {
3564b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
3565103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
3566aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
356769807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
356869807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
3569aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
357069807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
357169807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
357269807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
357369807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
357469807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
357569807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
357669807a63SLen Brown 	}
3577103a8feaSLen Brown 
3578b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
3579b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
3580103a8feaSLen Brown 
3581103a8feaSLen Brown 	/*
3582103a8feaSLen Brown 	 * check max extended function levels of CPUID.
3583103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
3584103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
3585103a8feaSLen Brown 	 */
3586103a8feaSLen Brown 	ebx = ecx = edx = 0;
35875aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
3588103a8feaSLen Brown 
358961a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
3590103a8feaSLen Brown 
3591103a8feaSLen Brown 		/*
3592103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3593103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
3594103a8feaSLen Brown 		 */
35955aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
35968209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
3597d7899447SLen Brown 	}
3598103a8feaSLen Brown 
3599103a8feaSLen Brown 	/*
3600103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3601103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
3602103a8feaSLen Brown 	 */
3603103a8feaSLen Brown 
36045aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
36058209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
3606812db3f7SLen Brown 	if (has_aperf) {
3607812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
3608812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
3609812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
3610812db3f7SLen Brown 	}
3611889facbeSLen Brown 	do_dts = eax & (1 << 0);
3612812db3f7SLen Brown 	if (do_dts)
3613812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
3614889facbeSLen Brown 	do_ptm = eax & (1 << 6);
3615812db3f7SLen Brown 	if (do_ptm)
3616812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
36177f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
36187f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
36197f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
36207f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
36217f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
3622889facbeSLen Brown 	has_epb = ecx & (1 << 3);
3623889facbeSLen Brown 
3624d8af6f5fSLen Brown 	if (debug)
3625b7d8c148SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
36267f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
36277f5c258eSLen Brown 			has_aperf ? "" : "No-",
36287f5c258eSLen Brown 			do_dts ? "" : "No-",
36297f5c258eSLen Brown 			do_ptm ? "" : "No-",
36307f5c258eSLen Brown 			has_hwp ? "" : "No-",
36317f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
36327f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
36337f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
36347f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
36357f5c258eSLen Brown 			has_epb ? "" : "No-");
3636103a8feaSLen Brown 
363769807a63SLen Brown 	if (debug)
363869807a63SLen Brown 		decode_misc_enable_msr();
363969807a63SLen Brown 
36408ae72255SLen Brown 	if (max_level >= 0x7 && debug) {
3641aa8d8cc7SLen Brown 		int has_sgx;
3642aa8d8cc7SLen Brown 
3643aa8d8cc7SLen Brown 		ecx = 0;
3644aa8d8cc7SLen Brown 
3645aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3646aa8d8cc7SLen Brown 
3647aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
3648aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3649aa8d8cc7SLen Brown 
3650aa8d8cc7SLen Brown 		if (has_sgx)
3651aa8d8cc7SLen Brown 			decode_feature_control_msr();
3652aa8d8cc7SLen Brown 	}
3653aa8d8cc7SLen Brown 
365461a87ba7SLen Brown 	if (max_level >= 0x15) {
36558a5bdf41SLen Brown 		unsigned int eax_crystal;
36568a5bdf41SLen Brown 		unsigned int ebx_tsc;
36578a5bdf41SLen Brown 
36588a5bdf41SLen Brown 		/*
36598a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
36608a5bdf41SLen Brown 		 */
36618a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
36625aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
36638a5bdf41SLen Brown 
36648a5bdf41SLen Brown 		if (ebx_tsc != 0) {
36658a5bdf41SLen Brown 
36668a5bdf41SLen Brown 			if (debug && (ebx != 0))
3667b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
36688a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
36698a5bdf41SLen Brown 
36708a5bdf41SLen Brown 			if (crystal_hz == 0)
36718a5bdf41SLen Brown 				switch(model) {
3672869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3673869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3674869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3675869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3676e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
3677e8efbc80SLen Brown 					break;
3678869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_X:	/* SKX */
36797268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3680ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
3681ec53e594SLen Brown 					break;
3682869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3683e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
36848a5bdf41SLen Brown 					break;
36858a5bdf41SLen Brown 				default:
36868a5bdf41SLen Brown 					crystal_hz = 0;
36878a5bdf41SLen Brown 			}
36888a5bdf41SLen Brown 
36898a5bdf41SLen Brown 			if (crystal_hz) {
36908a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
36918a5bdf41SLen Brown 				if (debug)
3692b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
36938a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
36948a5bdf41SLen Brown 			}
36958a5bdf41SLen Brown 		}
36968a5bdf41SLen Brown 	}
369761a87ba7SLen Brown 	if (max_level >= 0x16) {
369861a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
369961a87ba7SLen Brown 
370061a87ba7SLen Brown 		/*
370161a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
370261a87ba7SLen Brown 		 */
370361a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
370461a87ba7SLen Brown 
37055aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
370661a87ba7SLen Brown 		if (debug)
3707b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
370861a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
370961a87ba7SLen Brown 	}
37108a5bdf41SLen Brown 
3711b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
3712b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3713b2b34dfeSHubert Chrzaniuk 
3714812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
3715812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
3716812db3f7SLen Brown 
3717812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
3718812db3f7SLen Brown 		do_nhm_platform_info = 1;
3719812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
3720812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
3721812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
3722812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
3723812db3f7SLen Brown 	}
3724d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
3725812db3f7SLen Brown 
3726812db3f7SLen Brown 	if (do_snb_cstates)
3727812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
3728812db3f7SLen Brown 
37295a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
3730ee7e38e3SLen Brown 	do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3731ee7e38e3SLen Brown 	do_pc3 = (pkg_cstate_limit >= PCL__3);
3732ee7e38e3SLen Brown 	do_pc6 = (pkg_cstate_limit >= PCL__6);
3733ee7e38e3SLen Brown 	do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
3734d7899447SLen Brown 	do_c8_c9_c10 = has_hsw_msrs(family, model);
37355a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
37360b2bb692SLen Brown 	do_skl_residency = has_skl_msrs(family, model);
3737144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
3738fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
3739103a8feaSLen Brown 
3740f0057310SLen Brown 	if (debug)
3741f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
3742f0057310SLen Brown 
374371616c8eSLen Brown 	if (debug && has_slv_msrs(family, model))
374471616c8eSLen Brown 		decode_c6_demotion_policy_msr();
374571616c8eSLen Brown 
3746889facbeSLen Brown 	rapl_probe(family, model);
37473a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
3748889facbeSLen Brown 
3749fcd17211SLen Brown 	if (debug)
37501b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
3751fcd17211SLen Brown 
3752a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
3753a2b7b749SLen Brown 		calculate_tsc_tweak();
3754a2b7b749SLen Brown 
3755812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
3756812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
3757fdf676e5SLen Brown 
3758812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
3759812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
376027d47356SLen Brown 
3761889facbeSLen Brown 	return;
3762103a8feaSLen Brown }
3763103a8feaSLen Brown 
3764d8af6f5fSLen Brown void help()
3765103a8feaSLen Brown {
3766b7d8c148SLen Brown 	fprintf(outf,
3767d8af6f5fSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3768d8af6f5fSLen Brown 	"\n"
3769d8af6f5fSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
3770d8af6f5fSLen Brown 	"when COMMAND completes.\n"
3771d8af6f5fSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3772d8af6f5fSLen Brown 	"to print statistics, until interrupted.\n"
3773388e9c81SLen Brown 	"--add		add a counter\n"
3774388e9c81SLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
3775d8af6f5fSLen Brown 	"--debug	run in \"debug\" mode\n"
3776d8af6f5fSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
3777d8af6f5fSLen Brown 	"--help		print this help message\n"
3778b7d8c148SLen Brown 	"--out file	create or truncate \"file\" for all output\n"
3779d8af6f5fSLen Brown 	"--version	print version information\n"
3780d8af6f5fSLen Brown 	"\n"
3781d8af6f5fSLen Brown 	"For more help, run \"man turbostat\"\n");
3782103a8feaSLen Brown }
3783103a8feaSLen Brown 
3784103a8feaSLen Brown 
3785103a8feaSLen Brown /*
3786103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
3787103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
3788103a8feaSLen Brown  */
3789103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
3790103a8feaSLen Brown {
3791103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
3792103a8feaSLen Brown 		return 1;
3793103a8feaSLen Brown 	else
3794103a8feaSLen Brown 		return 0;
3795103a8feaSLen Brown }
3796103a8feaSLen Brown 
3797103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
3798103a8feaSLen Brown {
3799103a8feaSLen Brown 	return 0;
3800103a8feaSLen Brown }
3801103a8feaSLen Brown 
3802c98d5d94SLen Brown void topology_probe()
3803c98d5d94SLen Brown {
3804c98d5d94SLen Brown 	int i;
3805c98d5d94SLen Brown 	int max_core_id = 0;
3806c98d5d94SLen Brown 	int max_package_id = 0;
3807c98d5d94SLen Brown 	int max_siblings = 0;
3808c98d5d94SLen Brown 	struct cpu_topology {
3809c98d5d94SLen Brown 		int core_id;
3810c98d5d94SLen Brown 		int physical_package_id;
3811c98d5d94SLen Brown 	} *cpus;
3812c98d5d94SLen Brown 
3813c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
3814c98d5d94SLen Brown 	topo.num_cpus = 0;
3815c98d5d94SLen Brown 	topo.max_cpu_num = 0;
3816c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
3817c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
3818812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
3819c98d5d94SLen Brown 
3820d8af6f5fSLen Brown 	if (debug > 1)
3821b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3822c98d5d94SLen Brown 
3823c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
3824b2c95d90SJosh Triplett 	if (cpus == NULL)
3825b2c95d90SJosh Triplett 		err(1, "calloc cpus");
3826c98d5d94SLen Brown 
3827c98d5d94SLen Brown 	/*
3828c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
3829c98d5d94SLen Brown 	 */
3830c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
3831b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
3832b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
3833c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3834c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3835c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
3836c98d5d94SLen Brown 
3837c98d5d94SLen Brown 	/*
3838c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
3839c98d5d94SLen Brown 	 */
3840c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
3841b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
3842b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
3843c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3844c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3845c98d5d94SLen Brown 
3846c98d5d94SLen Brown 
3847c98d5d94SLen Brown 	/*
3848c98d5d94SLen Brown 	 * For online cpus
3849c98d5d94SLen Brown 	 * find max_core_id, max_package_id
3850c98d5d94SLen Brown 	 */
3851c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
3852c98d5d94SLen Brown 		int siblings;
3853c98d5d94SLen Brown 
3854c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
3855d8af6f5fSLen Brown 			if (debug > 1)
3856b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
3857c98d5d94SLen Brown 			continue;
3858c98d5d94SLen Brown 		}
3859c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
3860c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
3861c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
3862c98d5d94SLen Brown 
3863c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
3864c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
3865c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
3866c98d5d94SLen Brown 
3867c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
3868c98d5d94SLen Brown 		if (siblings > max_siblings)
3869c98d5d94SLen Brown 			max_siblings = siblings;
3870d8af6f5fSLen Brown 		if (debug > 1)
3871b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
3872c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
3873c98d5d94SLen Brown 	}
3874c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
3875d8af6f5fSLen Brown 	if (debug > 1)
3876b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
3877c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
38781cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_cores_per_pkg > 1)
3879812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
3880c98d5d94SLen Brown 
3881c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
3882d8af6f5fSLen Brown 	if (debug > 1)
3883b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
3884c98d5d94SLen Brown 			max_package_id, topo.num_packages);
38851cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_packages > 1)
3886812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
3887c98d5d94SLen Brown 
3888c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
3889d8af6f5fSLen Brown 	if (debug > 1)
3890b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
3891c98d5d94SLen Brown 
3892c98d5d94SLen Brown 	free(cpus);
3893c98d5d94SLen Brown }
3894c98d5d94SLen Brown 
3895c98d5d94SLen Brown void
3896c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3897c98d5d94SLen Brown {
3898c98d5d94SLen Brown 	int i;
3899c98d5d94SLen Brown 
3900c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3901678a3bd1SLen Brown 		topo.num_packages, sizeof(struct thread_data));
3902c98d5d94SLen Brown 	if (*t == NULL)
3903c98d5d94SLen Brown 		goto error;
3904c98d5d94SLen Brown 
3905c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
3906c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
3907c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
3908c98d5d94SLen Brown 
3909c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3910678a3bd1SLen Brown 		sizeof(struct core_data));
3911c98d5d94SLen Brown 	if (*c == NULL)
3912c98d5d94SLen Brown 		goto error;
3913c98d5d94SLen Brown 
3914c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3915c98d5d94SLen Brown 		(*c)[i].core_id = -1;
3916c98d5d94SLen Brown 
3917678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
3918c98d5d94SLen Brown 	if (*p == NULL)
3919c98d5d94SLen Brown 		goto error;
3920c98d5d94SLen Brown 
3921c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
3922c98d5d94SLen Brown 		(*p)[i].package_id = i;
3923c98d5d94SLen Brown 
3924c98d5d94SLen Brown 	return;
3925c98d5d94SLen Brown error:
3926b2c95d90SJosh Triplett 	err(1, "calloc counters");
3927c98d5d94SLen Brown }
3928c98d5d94SLen Brown /*
3929c98d5d94SLen Brown  * init_counter()
3930c98d5d94SLen Brown  *
3931c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
3932c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3933c98d5d94SLen Brown  *
3934c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
3935c98d5d94SLen Brown  */
3936c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3937c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
3938c98d5d94SLen Brown 	int pkg_num, int cpu_id)
3939c98d5d94SLen Brown {
3940c98d5d94SLen Brown 	struct thread_data *t;
3941c98d5d94SLen Brown 	struct core_data *c;
3942c98d5d94SLen Brown 	struct pkg_data *p;
3943c98d5d94SLen Brown 
3944c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3945c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
3946c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
3947c98d5d94SLen Brown 
3948c98d5d94SLen Brown 	t->cpu_id = cpu_id;
3949c98d5d94SLen Brown 	if (thread_num == 0) {
3950c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3951c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
3952c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3953c98d5d94SLen Brown 	}
3954c98d5d94SLen Brown 
3955c98d5d94SLen Brown 	c->core_id = core_num;
3956c98d5d94SLen Brown 	p->package_id = pkg_num;
3957c98d5d94SLen Brown }
3958c98d5d94SLen Brown 
3959c98d5d94SLen Brown 
3960c98d5d94SLen Brown int initialize_counters(int cpu_id)
3961c98d5d94SLen Brown {
3962c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
3963c98d5d94SLen Brown 
3964c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
3965c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
3966e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
3967e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
3968c98d5d94SLen Brown 		topo.num_cores++;
3969c98d5d94SLen Brown 
3970c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3971c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3972c98d5d94SLen Brown 	return 0;
3973c98d5d94SLen Brown }
3974c98d5d94SLen Brown 
3975c98d5d94SLen Brown void allocate_output_buffer()
3976c98d5d94SLen Brown {
39773b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
3978c98d5d94SLen Brown 	outp = output_buffer;
3979b2c95d90SJosh Triplett 	if (outp == NULL)
3980b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
3981c98d5d94SLen Brown }
398236229897SLen Brown void allocate_fd_percpu(void)
398336229897SLen Brown {
398401a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
398536229897SLen Brown 	if (fd_percpu == NULL)
398636229897SLen Brown 		err(-1, "calloc fd_percpu");
398736229897SLen Brown }
3988562a2d37SLen Brown void allocate_irq_buffers(void)
3989562a2d37SLen Brown {
3990562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3991562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
3992562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
3993c98d5d94SLen Brown 
399401a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
3995562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
399601a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
3997562a2d37SLen Brown }
3998c98d5d94SLen Brown void setup_all_buffers(void)
3999c98d5d94SLen Brown {
4000c98d5d94SLen Brown 	topology_probe();
4001562a2d37SLen Brown 	allocate_irq_buffers();
400236229897SLen Brown 	allocate_fd_percpu();
4003c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4004c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4005c98d5d94SLen Brown 	allocate_output_buffer();
4006c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4007c98d5d94SLen Brown }
40083b4d5c7fSAndy Shevchenko 
40097ce7d5deSPrarit Bhargava void set_base_cpu(void)
40107ce7d5deSPrarit Bhargava {
40117ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
40127ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
40137ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
40147ce7d5deSPrarit Bhargava 
40157ce7d5deSPrarit Bhargava 	if (debug > 1)
4016b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
40177ce7d5deSPrarit Bhargava }
40187ce7d5deSPrarit Bhargava 
4019103a8feaSLen Brown void turbostat_init()
4020103a8feaSLen Brown {
40217ce7d5deSPrarit Bhargava 	setup_all_buffers();
40227ce7d5deSPrarit Bhargava 	set_base_cpu();
4023103a8feaSLen Brown 	check_dev_msr();
402498481e79SLen Brown 	check_permissions();
4025fcd17211SLen Brown 	process_cpuid();
4026103a8feaSLen Brown 
4027103a8feaSLen Brown 
4028d8af6f5fSLen Brown 	if (debug)
40297f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
40307f5c258eSLen Brown 
40317f5c258eSLen Brown 	if (debug)
4032889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
4033889facbeSLen Brown 
4034d8af6f5fSLen Brown 	if (debug)
40353a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
40363a9a941dSLen Brown 
4037d8af6f5fSLen Brown 	if (debug)
4038889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
4039889facbeSLen Brown 
4040889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
4041889facbeSLen Brown 
4042d8af6f5fSLen Brown 	if (debug)
4043889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
40445a63426eSLen Brown 
40455a63426eSLen Brown 	if (debug && do_irtl_snb)
40465a63426eSLen Brown 		print_irtl();
4047103a8feaSLen Brown }
4048103a8feaSLen Brown 
4049103a8feaSLen Brown int fork_it(char **argv)
4050103a8feaSLen Brown {
4051103a8feaSLen Brown 	pid_t child_pid;
4052d91bb17cSLen Brown 	int status;
4053d15cf7c1SLen Brown 
4054d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
4055d91bb17cSLen Brown 	if (status)
4056d91bb17cSLen Brown 		exit(status);
4057d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
4058d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4059103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
4060103a8feaSLen Brown 
4061103a8feaSLen Brown 	child_pid = fork();
4062103a8feaSLen Brown 	if (!child_pid) {
4063103a8feaSLen Brown 		/* child */
4064103a8feaSLen Brown 		execvp(argv[0], argv);
4065103a8feaSLen Brown 	} else {
4066103a8feaSLen Brown 
4067103a8feaSLen Brown 		/* parent */
4068b2c95d90SJosh Triplett 		if (child_pid == -1)
4069b2c95d90SJosh Triplett 			err(1, "fork");
4070103a8feaSLen Brown 
4071103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
4072103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
4073b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
4074b2c95d90SJosh Triplett 			err(status, "waitpid");
4075103a8feaSLen Brown 	}
4076c98d5d94SLen Brown 	/*
4077c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
4078c98d5d94SLen Brown 	 * because re-starting is problematic when forking
4079c98d5d94SLen Brown 	 */
4080c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
4081103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
4082103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
4083ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4084ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
4085ba3dec99SLen Brown 	else {
4086c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
4087c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
4088ba3dec99SLen Brown 	}
4089103a8feaSLen Brown 
4090b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4091b7d8c148SLen Brown 
4092b7d8c148SLen Brown 	flush_output_stderr();
4093103a8feaSLen Brown 
4094d91bb17cSLen Brown 	return status;
4095103a8feaSLen Brown }
4096103a8feaSLen Brown 
40973b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
40983b4d5c7fSAndy Shevchenko {
40993b4d5c7fSAndy Shevchenko 	int status;
41003b4d5c7fSAndy Shevchenko 
41013b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
41023b4d5c7fSAndy Shevchenko 	if (status)
41033b4d5c7fSAndy Shevchenko 		return status;
41043b4d5c7fSAndy Shevchenko 
41053b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
41063b4d5c7fSAndy Shevchenko 	if (status)
41073b4d5c7fSAndy Shevchenko 		return status;
41083b4d5c7fSAndy Shevchenko 
4109b7d8c148SLen Brown 	flush_output_stdout();
41103b4d5c7fSAndy Shevchenko 
41113b4d5c7fSAndy Shevchenko 	return status;
41123b4d5c7fSAndy Shevchenko }
41133b4d5c7fSAndy Shevchenko 
4114d8af6f5fSLen Brown void print_version() {
4115cf4cbe53SLen Brown 	fprintf(outf, "turbostat version 4.17 1 Jan 2017"
4116d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
4117d8af6f5fSLen Brown }
4118d8af6f5fSLen Brown 
4119388e9c81SLen Brown int add_counter(unsigned int msr_num, char *name, unsigned int width,
4120388e9c81SLen Brown 	enum counter_scope scope, enum counter_type type,
4121388e9c81SLen Brown 	enum counter_format format)
4122388e9c81SLen Brown {
4123388e9c81SLen Brown 	struct msr_counter *msrp;
4124388e9c81SLen Brown 
4125388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
4126388e9c81SLen Brown 	if (msrp == NULL) {
4127388e9c81SLen Brown 		perror("calloc");
4128388e9c81SLen Brown 		exit(1);
4129388e9c81SLen Brown 	}
4130388e9c81SLen Brown 
4131388e9c81SLen Brown 	msrp->msr_num = msr_num;
4132388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
4133388e9c81SLen Brown 	msrp->width = width;
4134388e9c81SLen Brown 	msrp->type = type;
4135388e9c81SLen Brown 	msrp->format = format;
4136388e9c81SLen Brown 
4137388e9c81SLen Brown 	switch (scope) {
4138388e9c81SLen Brown 
4139388e9c81SLen Brown 	case SCOPE_CPU:
4140388e9c81SLen Brown 		msrp->next = sys.tp;
4141388e9c81SLen Brown 		sys.tp = msrp;
4142678a3bd1SLen Brown 		sys.added_thread_counters++;
4143678a3bd1SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4144678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
4145678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4146678a3bd1SLen Brown 			exit(-1);
4147678a3bd1SLen Brown 		}
4148388e9c81SLen Brown 		break;
4149388e9c81SLen Brown 
4150388e9c81SLen Brown 	case SCOPE_CORE:
4151388e9c81SLen Brown 		msrp->next = sys.cp;
4152388e9c81SLen Brown 		sys.cp = msrp;
4153678a3bd1SLen Brown 		sys.added_core_counters++;
4154678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4155678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
4156678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4157678a3bd1SLen Brown 			exit(-1);
4158678a3bd1SLen Brown 		}
4159388e9c81SLen Brown 		break;
4160388e9c81SLen Brown 
4161388e9c81SLen Brown 	case SCOPE_PACKAGE:
4162388e9c81SLen Brown 		msrp->next = sys.pp;
4163388e9c81SLen Brown 		sys.pp = msrp;
4164678a3bd1SLen Brown 		sys.added_package_counters++;
4165678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4166678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
4167678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4168678a3bd1SLen Brown 			exit(-1);
4169678a3bd1SLen Brown 		}
4170388e9c81SLen Brown 		break;
4171388e9c81SLen Brown 	}
4172388e9c81SLen Brown 
4173388e9c81SLen Brown 	return 0;
4174388e9c81SLen Brown }
4175388e9c81SLen Brown 
4176388e9c81SLen Brown void parse_add_command(char *add_command)
4177388e9c81SLen Brown {
4178388e9c81SLen Brown 	int msr_num = 0;
4179388e9c81SLen Brown 	char name_buffer[NAME_BYTES];
4180388e9c81SLen Brown 	int width = 64;
4181388e9c81SLen Brown 	int fail = 0;
4182388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
4183388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
4184388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
4185388e9c81SLen Brown 
4186388e9c81SLen Brown 	while (add_command) {
4187388e9c81SLen Brown 
4188388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4189388e9c81SLen Brown 			goto next;
4190388e9c81SLen Brown 
4191388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
4192388e9c81SLen Brown 			goto next;
4193388e9c81SLen Brown 
4194388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
4195388e9c81SLen Brown 			if ((width == 32) || (width == 64))
4196388e9c81SLen Brown 				goto next;
4197388e9c81SLen Brown 			width = 64;
4198388e9c81SLen Brown 		}
4199388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4200388e9c81SLen Brown 			scope = SCOPE_CPU;
4201388e9c81SLen Brown 			goto next;
4202388e9c81SLen Brown 		}
4203388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
4204388e9c81SLen Brown 			scope = SCOPE_CORE;
4205388e9c81SLen Brown 			goto next;
4206388e9c81SLen Brown 		}
4207388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
4208388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
4209388e9c81SLen Brown 			goto next;
4210388e9c81SLen Brown 		}
4211388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4212388e9c81SLen Brown 			type = COUNTER_CYCLES;
4213388e9c81SLen Brown 			goto next;
4214388e9c81SLen Brown 		}
4215388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4216388e9c81SLen Brown 			type = COUNTER_SECONDS;
4217388e9c81SLen Brown 			goto next;
4218388e9c81SLen Brown 		}
4219388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
4220388e9c81SLen Brown 			format = FORMAT_RAW;
4221388e9c81SLen Brown 			goto next;
4222388e9c81SLen Brown 		}
4223388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
4224388e9c81SLen Brown 			format = FORMAT_DELTA;
4225388e9c81SLen Brown 			goto next;
4226388e9c81SLen Brown 		}
4227388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
4228388e9c81SLen Brown 			format = FORMAT_PERCENT;
4229388e9c81SLen Brown 			goto next;
4230388e9c81SLen Brown 		}
4231388e9c81SLen Brown 
4232388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
4233388e9c81SLen Brown 			char *eos;
4234388e9c81SLen Brown 
4235388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
4236388e9c81SLen Brown 			if (eos)
4237388e9c81SLen Brown 				*eos = '\0';
4238388e9c81SLen Brown 			goto next;
4239388e9c81SLen Brown 		}
4240388e9c81SLen Brown 
4241388e9c81SLen Brown next:
4242388e9c81SLen Brown 		add_command = strchr(add_command, ',');
4243388e9c81SLen Brown 		if (add_command)
4244388e9c81SLen Brown 			add_command++;
4245388e9c81SLen Brown 
4246388e9c81SLen Brown 	}
4247388e9c81SLen Brown 	if (msr_num == 0) {
4248388e9c81SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4249388e9c81SLen Brown 		fail++;
4250388e9c81SLen Brown 	}
4251388e9c81SLen Brown 
4252388e9c81SLen Brown 	/* generate default column header */
4253388e9c81SLen Brown 	if (*name_buffer == '\0') {
4254388e9c81SLen Brown 		if (format == FORMAT_RAW) {
4255388e9c81SLen Brown 			if (width == 32)
4256388e9c81SLen Brown 				sprintf(name_buffer, "msr%d", msr_num);
4257388e9c81SLen Brown 			else
4258388e9c81SLen Brown 				sprintf(name_buffer, "MSR%d", msr_num);
4259388e9c81SLen Brown 		} else if (format == FORMAT_DELTA) {
4260388e9c81SLen Brown 			if (width == 32)
4261388e9c81SLen Brown 				sprintf(name_buffer, "cnt%d", msr_num);
4262388e9c81SLen Brown 			else
4263388e9c81SLen Brown 				sprintf(name_buffer, "CNT%d", msr_num);
4264388e9c81SLen Brown 		} else if (format == FORMAT_PERCENT) {
4265388e9c81SLen Brown 			if (width == 32)
4266388e9c81SLen Brown 				sprintf(name_buffer, "msr%d%%", msr_num);
4267388e9c81SLen Brown 			else
4268388e9c81SLen Brown 				sprintf(name_buffer, "MSR%d%%", msr_num);
4269388e9c81SLen Brown 		}
4270388e9c81SLen Brown 	}
4271388e9c81SLen Brown 
4272388e9c81SLen Brown 	if (add_counter(msr_num, name_buffer, width, scope, type, format))
4273388e9c81SLen Brown 		fail++;
4274388e9c81SLen Brown 
4275388e9c81SLen Brown 	if (fail) {
4276388e9c81SLen Brown 		help();
4277388e9c81SLen Brown 		exit(1);
4278388e9c81SLen Brown 	}
4279388e9c81SLen Brown }
4280812db3f7SLen Brown /*
4281812db3f7SLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
4282812db3f7SLen Brown  * SHOW_LIST - show this list of counters, hide the rest
4283812db3f7SLen Brown  */
4284812db3f7SLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4285812db3f7SLen Brown 
4286812db3f7SLen Brown int shown;
4287812db3f7SLen Brown /*
4288812db3f7SLen Brown  * parse_show_hide() - process cmdline to set default counter action
4289812db3f7SLen Brown  */
4290812db3f7SLen Brown void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4291812db3f7SLen Brown {
4292812db3f7SLen Brown 	/*
4293812db3f7SLen Brown 	 * --show: show only those specified
4294812db3f7SLen Brown 	 *  The 1st invocation will clear and replace the enabled mask
4295812db3f7SLen Brown 	 *  subsequent invocations can add to it.
4296812db3f7SLen Brown 	 */
4297812db3f7SLen Brown 	if (new_mode == SHOW_LIST) {
4298812db3f7SLen Brown 		if (shown == 0)
4299812db3f7SLen Brown 			bic_enabled = bic_lookup(optarg);
4300812db3f7SLen Brown 		else
4301812db3f7SLen Brown 			bic_enabled |= bic_lookup(optarg);
4302812db3f7SLen Brown 		shown = 1;
4303812db3f7SLen Brown 
4304812db3f7SLen Brown 		return;
4305812db3f7SLen Brown 	}
4306812db3f7SLen Brown 
4307812db3f7SLen Brown 	/*
4308812db3f7SLen Brown 	 * --hide: do not show those specified
4309812db3f7SLen Brown 	 *  multiple invocations simply clear more bits in enabled mask
4310812db3f7SLen Brown 	 */
4311812db3f7SLen Brown 	bic_enabled &= ~bic_lookup(optarg);
4312812db3f7SLen Brown }
4313812db3f7SLen Brown 
4314103a8feaSLen Brown void cmdline(int argc, char **argv)
4315103a8feaSLen Brown {
4316103a8feaSLen Brown 	int opt;
4317d8af6f5fSLen Brown 	int option_index = 0;
4318d8af6f5fSLen Brown 	static struct option long_options[] = {
4319388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
4320d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
4321d8af6f5fSLen Brown 		{"debug",	no_argument,		0, 'd'},
4322d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
4323d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
4324812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
4325d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
4326b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
4327d8af6f5fSLen Brown 		{"Package",	no_argument,		0, 'p'},
4328d8af6f5fSLen Brown 		{"processor",	no_argument,		0, 'p'},
4329812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
4330d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
4331d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
4332d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
4333d8af6f5fSLen Brown 		{0,		0,			0,  0 }
4334d8af6f5fSLen Brown 	};
4335103a8feaSLen Brown 
4336103a8feaSLen Brown 	progname = argv[0];
4337103a8feaSLen Brown 
4338b7d8c148SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
4339d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
4340103a8feaSLen Brown 		switch (opt) {
4341388e9c81SLen Brown 		case 'a':
4342388e9c81SLen Brown 			parse_add_command(optarg);
4343388e9c81SLen Brown 			break;
4344d8af6f5fSLen Brown 		case 'D':
4345d8af6f5fSLen Brown 			dump_only++;
43468e180f3cSLen Brown 			break;
4347d8af6f5fSLen Brown 		case 'd':
4348d8af6f5fSLen Brown 			debug++;
43492f32edf1SLen Brown 			break;
4350812db3f7SLen Brown 		case 'H':
4351812db3f7SLen Brown 			parse_show_hide(optarg, HIDE_LIST);
4352812db3f7SLen Brown 			break;
4353d8af6f5fSLen Brown 		case 'h':
4354d8af6f5fSLen Brown 		default:
4355d8af6f5fSLen Brown 			help();
4356d8af6f5fSLen Brown 			exit(1);
4357d8af6f5fSLen Brown 		case 'i':
43582a0609c0SLen Brown 			{
43592a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
43602a0609c0SLen Brown 
43612a0609c0SLen Brown 				if (interval < 0.001) {
4362b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
43632a0609c0SLen Brown 						interval);
43642a0609c0SLen Brown 					exit(2);
43652a0609c0SLen Brown 				}
43662a0609c0SLen Brown 
43672a0609c0SLen Brown 				interval_ts.tv_sec = interval;
43682a0609c0SLen Brown 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
43692a0609c0SLen Brown 			}
4370889facbeSLen Brown 			break;
43715c56be9aSDirk Brandewie 		case 'J':
43725c56be9aSDirk Brandewie 			rapl_joules++;
43735c56be9aSDirk Brandewie 			break;
4374b7d8c148SLen Brown 		case 'o':
4375b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
4376b7d8c148SLen Brown 			break;
4377d8af6f5fSLen Brown 		case 'P':
4378d8af6f5fSLen Brown 			show_pkg_only++;
4379d8af6f5fSLen Brown 			break;
4380d8af6f5fSLen Brown 		case 'p':
4381d8af6f5fSLen Brown 			show_core_only++;
4382d8af6f5fSLen Brown 			break;
4383812db3f7SLen Brown 		case 's':
4384812db3f7SLen Brown 			parse_show_hide(optarg, SHOW_LIST);
4385812db3f7SLen Brown 			break;
4386d8af6f5fSLen Brown 		case 'S':
4387d8af6f5fSLen Brown 			summary_only++;
4388d8af6f5fSLen Brown 			break;
4389d8af6f5fSLen Brown 		case 'T':
4390d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
4391d8af6f5fSLen Brown 			break;
4392d8af6f5fSLen Brown 		case 'v':
4393d8af6f5fSLen Brown 			print_version();
4394d8af6f5fSLen Brown 			exit(0);
4395d8af6f5fSLen Brown 			break;
4396103a8feaSLen Brown 		}
4397103a8feaSLen Brown 	}
4398103a8feaSLen Brown }
4399103a8feaSLen Brown 
4400103a8feaSLen Brown int main(int argc, char **argv)
4401103a8feaSLen Brown {
4402b7d8c148SLen Brown 	outf = stderr;
4403b7d8c148SLen Brown 
4404103a8feaSLen Brown 	cmdline(argc, argv);
4405103a8feaSLen Brown 
4406d8af6f5fSLen Brown 	if (debug)
4407d8af6f5fSLen Brown 		print_version();
4408103a8feaSLen Brown 
4409103a8feaSLen Brown 	turbostat_init();
4410103a8feaSLen Brown 
44113b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
44123b4d5c7fSAndy Shevchenko 	if (dump_only)
44133b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
44143b4d5c7fSAndy Shevchenko 
4415103a8feaSLen Brown 	/*
4416103a8feaSLen Brown 	 * if any params left, it must be a command to fork
4417103a8feaSLen Brown 	 */
4418103a8feaSLen Brown 	if (argc - optind)
4419103a8feaSLen Brown 		return fork_it(argv + optind);
4420103a8feaSLen Brown 	else
4421103a8feaSLen Brown 		turbostat_loop();
4422103a8feaSLen Brown 
4423103a8feaSLen Brown 	return 0;
4424103a8feaSLen Brown }
4425